Debian Codegraph @

src/Function/Nary/NonDependent/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Heterogeneous N-ary Functions: basic types and operations
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Function.Nary.NonDependent.Base where
10
11 ------------------------------------------------------------------------
12 -- Concrete examples can be found in README.Nary. This file's comments
13 -- are more focused on the implementation details and the motivations
14 -- behind the design decisions.
15 ------------------------------------------------------------------------
16
17 open import Level using (Level; 0ℓ; _⊔_)
18 open import Data.Nat.Base using (ℕ; zero; suc)
19 open import Data.Product.Base using (_×_; _,_)
20 open import Data.Unit.Polymorphic.Base using (⊤; tt)
21 open import Function.Base using (_∘′_; _$′_; const; flip)
22 open import Data.Fin.Base using (Fin; zero; suc)
23 open import Relation.Unary using (IUniversal)
24
25 private
26 variable
27 a b c : Level
28 A : Set a
29 B : Set b
30 C : Set c
31
32 ------------------------------------------------------------------------
33 -- Type Definitions
34
35 -- We want to define n-ary function spaces and generic n-ary operations
36 -- on them such as (un)currying, zipWith, alignWith, etc.
37
38 -- We want users to be able to use these seamlessly whenever n is concrete.
39
40 -- In other words, we want Agda to infer the sets `A₁, ⋯, Aₙ` when we
41 -- write `uncurryₙ n f` where `f` has type `A₁ → ⋯ → Aₙ → B`. For this
42 -- to happen, we need the structure in which these Sets are stored to
43 -- effectively η-expand to `A₁, ⋯, Aₙ` when the parameter `n` is known.
44
45 -- Hence the following definitions:
46 ------------------------------------------------------------------------
47
48 -- First, a "vector" of `n` Levels (defined by induction on n so that it
49 -- may be built by η-expansion and unification). Each Level will be that
50 -- of one of the Sets we want to take the n-ary product of.
51
52 Levels : ℕ → Set
53 Levels zero = ⊤
54 Levels (suc n) = Level × Levels n
55
56 -- The overall Level of a `n` Sets of respective levels `l₁, ⋯, lₙ` will
57 -- be the least upper bound `l₁ ⊔ ⋯ ⊔ lₙ` of all of the Levels involved.
58 -- Hence the following definition of n-ary least upper bound:
59
60 ⨆ : ∀ n → Levels n → Level
61 ⨆ zero _ = Level.zero
62 ⨆ (suc n) (l , ls) = l ⊔ (⨆ n ls)
63
64 -- Second, a "vector" of `n` Sets whose respective Levels are determined
65 -- by the `Levels n` input.
66
67 Sets : ∀ n (ls : Levels n) → Set (Level.suc (⨆ n ls))
68 Sets zero _ = ⊤
69 Sets (suc n) (l , ls) = Set l × Sets n ls
70
71 -- Third, a function type whose domains are given by a "vector" of `n`
72 -- Sets `A₁, ⋯, Aₙ` and whose codomain is `B`. `Arrows` forms such a
73 -- type of shape `A₁ → ⋯ → Aₙ → B` by induction on `n`.
74
75 Arrows : ∀ n {r ls} → Sets n ls → Set r → Set (r ⊔ (⨆ n ls))
76 Arrows zero _ b = b
77 Arrows (suc n) (a , as) b = a → Arrows n as b
78
79 -- We introduce a notation for this definition
80
81 infixr 0 _⇉_
82 _⇉_ : ∀ {n ls r} → Sets n ls → Set r → Set (r ⊔ (⨆ n ls))
83 _⇉_ = Arrows _
84
85 ------------------------------------------------------------------------
86 -- Operations on Levels
87
88 lmap : (Level → Level) → ∀ n → Levels n → Levels n
89 lmap f zero ls = _
90 lmap f (suc n) (l , ls) = f l , lmap f n ls
91
92 ltabulate : ∀ n → (Fin n → Level) → Levels n
93 ltabulate zero f = _
94 ltabulate (suc n) f = f zero , ltabulate n (f ∘′ suc)
95
96 lreplicate : ∀ n → Level → Levels n
97 lreplicate n ℓ = ltabulate n (const ℓ)
98
99 0ℓs : ∀[ Levels ]
100 0ℓs = lreplicate _ 0ℓ
101
102 -- Note that you might think that `lconst n l ≡ l`
103 -- but unfortunately this isn't true for `n = 0`.
104 lconst : ℕ → Level → Level
105 lconst l n = ⨆ l (lreplicate l n)
106
107 ------------------------------------------------------------------------
108 -- Operations on Sets
109
110 -- Level-respecting map
111
112 infixr -1 _<$>_
113
114 _<$>_ : (∀ {l} → Set l → Set l) →
115 ∀ {n ls} → Sets n ls → Sets n ls
116 _<$>_ f {zero} as = _
117 _<$>_ f {suc n} (a , as) = f a , (f <$> as)
118
119 -- Level-modifying generalised map
120
121 smap : ∀ f → (∀ {l} → Set l → Set (f l)) →
122 ∀ n {ls} → Sets n ls → Sets n (lmap f n ls)
123 smap f F zero as = _
124 smap f F (suc n) (a , as) = F a , smap f F n as
125
126 stabulate : ∀ n →
127 (f : Fin n → Level) →
128 (g : (i : Fin n) → Set (f i)) →
129 Sets n (ltabulate n f)
130 stabulate ℕ.zero f g = _
131 stabulate (suc n) f g = g zero , stabulate n (f ∘′ suc) (λ u → g (suc u))
132
133 sreplicate : ∀ n {a} → Set a → Sets n (lreplicate n a)
134 sreplicate n A = stabulate n (const _) (const A)
135
136 ------------------------------------------------------------------------
137 -- Operations on Functions
138
139 -- mapping under n arguments
140
141 mapₙ : ∀ n {ls} {as : Sets n ls} → (B → C) → as ⇉ B → as ⇉ C
142 mapₙ zero f v = f v
143 mapₙ (suc n) f g = mapₙ n f ∘′ g
144
145 -- compose function at the n-th position
146
147 infix 1 _%=_⊢_
148
149 _%=_⊢_ : ∀ n {ls} {as : Sets n ls} → (A → B) → as ⇉ (B → C) → as ⇉ (A → C)
150 n %= f ⊢ g = mapₙ n (_∘′ f) g
151
152 -- partially apply function at the n-th position
153
154 infix 1 _∷=_⊢_
155
156 _∷=_⊢_ : ∀ n {ls} {as : Sets n ls} → A → as ⇉ (A → B) → as ⇉ B
157 n ∷= x ⊢ g = mapₙ n (_$′ x) g
158
159 -- hole at the n-th position
160
161 holeₙ : ∀ n {ls} {as : Sets n ls} → (A → as ⇉ B) → as ⇉ (A → B)
162 holeₙ zero f = f
163 holeₙ (suc n) f = holeₙ n ∘′ flip f
164
165 -- function constant in its n first arguments
166
167 -- Note that its type will only be inferred if it is used in a context
168 -- specifying what the type of the function ought to be. Just like the
169 -- usual const: there is no way to infer its domain from its argument.
170
171 constₙ : ∀ n {ls r} {as : Sets n ls} {b : Set r} → b → as ⇉ b
172 constₙ zero v = v
173 constₙ (suc n) v = const (constₙ n v)