Debian Codegraph @

src/Data/Product/Nary/NonDependent.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Nondependent heterogeneous N-ary products
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Product.Nary.NonDependent 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)
18 open import Data.Product.Base as Prod
19 import Data.Product.Properties as Prodₚ
20 open import Data.Sum.Base using (_⊎_)
21 open import Data.Nat.Base using (ℕ; zero; suc; pred)
22 open import Data.Fin.Base using (Fin; zero; suc)
23 open import Data.Unit.Base using (⊤)
24 open import Function.Base using (const; _∘′_; _∘_)
25 open import Relation.Nullary.Decidable.Core using (Dec; yes; no; _×-dec_)
26 open import Relation.Binary.Core using (Rel)
27 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong₂)
28
29 open import Function.Nary.NonDependent.Base
30
31 -- Provided n Levels and a corresponding "vector" of `n` Sets, we can
32 -- build a big right-nested product type packing a value for each one
33 -- of these Sets.
34 -- We have two distinct but equivalent definitions:
35 -- the first which is always ⊤-terminated
36 -- the other which has a special case for n = 1 because we want our
37 -- `(un)curryₙ` functions to work for user-written functions and
38 -- products and they rarely are ⊤-terminated.
39
40 Product⊤ : ∀ n {ls} → Sets n ls → Set (⨆ n ls)
41 Product⊤ zero as = ⊤
42 Product⊤ (suc n) (a , as) = a × Product⊤ n as
43
44 Product : ∀ n {ls} → Sets n ls → Set (⨆ n ls)
45 Product 0 _ = ⊤
46 Product 1 (a , _) = a
47 Product (suc n) (a , as) = a × Product n as
48
49 -- An n-ary product where every element of the product lives at the same universe level.
50
51 HomoProduct′ : ∀ n {a} → (Fin n → Set a) → Set (lconst n a)
52 HomoProduct′ n f = Product n (stabulate n (const _) f)
53
54 -- An n-ary product where every element of the product lives in the same type.
55
56 HomoProduct : ∀ n {a} → Set a → Set (lconst n a)
57 HomoProduct n A = HomoProduct′ n (const A)
58
59 -- Pointwise lifting of a relation over n-ary products
60
61 Pointwiseₙ : (∀ {a} {A : Set a} → Rel A a) →
62 ∀ n {ls} {as : Sets n ls} (l r : Product n as) → Sets n ls
63 Pointwiseₙ R 0 l r = _
64 Pointwiseₙ R 1 a b = R a b , _
65 Pointwiseₙ R (suc n@(suc _)) (a , l) (b , r) = R a b , Pointwiseₙ R n l r
66
67 -- Pointwise lifting of propositional equality over n-ary products
68
69 Equalₙ : ∀ n {ls} {as : Sets n ls} (l r : Product n as) → Sets n ls
70 Equalₙ = Pointwiseₙ _≡_
71
72 ------------------------------------------------------------------------
73 -- Generic Programs
74 ------------------------------------------------------------------------
75
76 -- Once we have these type definitions, we can write generic programs
77 -- over them. They will typically be split into two or three definitions:
78
79 -- 1. action on the vector of n levels (if any)
80 -- 2. action on the corresponding vector of n Sets
81 -- 3. actual program, typed thank to the function defined in step 2.
82
83 -- see Relation.Binary.PropositionalEquality for congₙ and substₙ, two
84 -- equality-related generic programs.
85
86 ------------------------------------------------------------------------
87 -- equivalence of Product and Product⊤
88
89 toProduct : ∀ n {ls} {as : Sets n ls} → Product⊤ n as → Product n as
90 toProduct 0 _ = _
91 toProduct 1 (v , _) = v
92 toProduct (suc n@(suc _)) (v , vs) = v , toProduct n vs
93
94 toProduct⊤ : ∀ n {ls} {as : Sets n ls} → Product n as → Product⊤ n as
95 toProduct⊤ 0 _ = _
96 toProduct⊤ 1 v = v , _
97 toProduct⊤ (suc n@(suc _)) (v , vs) = v , toProduct⊤ n vs
98
99 ------------------------------------------------------------------------
100 -- (un)curry
101
102 -- We start by defining `curryₙ` and `uncurryₙ` converting back and forth
103 -- between `A₁ → ⋯ → Aₙ → B` and `(A₁ × ⋯ × Aₙ) → B` by induction on `n`.
104
105 curryₙ : ∀ n {ls} {as : Sets n ls} {r} {b : Set r} →
106 (Product n as → b) → as ⇉ b
107 curryₙ 0 f = f _
108 curryₙ 1 f = f
109 curryₙ (suc n@(suc _)) f = curryₙ n ∘′ curry f
110
111 uncurryₙ : ∀ n {ls} {as : Sets n ls} {r} {b : Set r} →
112 as ⇉ b → (Product n as → b)
113 uncurryₙ 0 f = const f
114 uncurryₙ 1 f = f
115 uncurryₙ (suc n@(suc _)) f = uncurry (uncurryₙ n ∘′ f)
116
117 -- Variants for Product⊤
118
119 curry⊤ₙ : ∀ n {ls} {as : Sets n ls} {r} {b : Set r} →
120 (Product⊤ n as → b) → as ⇉ b
121 curry⊤ₙ zero f = f _
122 curry⊤ₙ (suc n) f = curry⊤ₙ n ∘′ curry f
123
124 uncurry⊤ₙ : ∀ n {ls} {as : Sets n ls} {r} {b : Set r} →
125 as ⇉ b → (Product⊤ n as → b)
126 uncurry⊤ₙ zero f = const f
127 uncurry⊤ₙ (suc n) f = uncurry (uncurry⊤ₙ n ∘′ f)
128
129 ------------------------------------------------------------------------
130 -- decidability
131
132 Product⊤-dec : ∀ n {ls} {as : Sets n ls} → Product⊤ n (Dec <$> as) → Dec (Product⊤ n as)
133 Product⊤-dec zero _ = yes _
134 Product⊤-dec (suc n) (p? , ps?) = p? ×-dec Product⊤-dec n ps?
135
136 Product-dec : ∀ n {ls} {as : Sets n ls} → Product n (Dec <$> as) → Dec (Product n as)
137 Product-dec 0 _ = yes _
138 Product-dec 1 p? = p?
139 Product-dec (suc n@(suc _)) (p? , ps?) = p? ×-dec Product-dec n ps?
140
141 ------------------------------------------------------------------------
142 -- pointwise liftings
143
144 toEqualₙ : ∀ n {ls} {as : Sets n ls} {l r : Product n as} →
145 l ≡ r → Product n (Equalₙ n l r)
146 toEqualₙ 0 eq = _
147 toEqualₙ 1 eq = eq
148 toEqualₙ (suc n@(suc _)) eq = Prod.map₂ (toEqualₙ n) (Prodₚ.,-injective eq)
149
150 fromEqualₙ : ∀ n {ls} {as : Sets n ls} {l r : Product n as} →
151 Product n (Equalₙ n l r) → l ≡ r
152 fromEqualₙ 0 eq = refl
153 fromEqualₙ 1 eq = eq
154 fromEqualₙ (suc n@(suc _)) eq = uncurry (cong₂ _,_) (Prod.map₂ (fromEqualₙ n) eq)
155
156 ------------------------------------------------------------------------
157 -- projection of the k-th component
158
159 -- To know at which Set level the k-th projection out of an n-ary
160 -- product lives, we need to extract said level, by induction on k.
161
162 Levelₙ : ∀ {n} → Levels n → Fin n → Level
163 Levelₙ (l , _) zero = l
164 Levelₙ (_ , ls) (suc k) = Levelₙ ls k
165
166 -- Once we have the Sets used in the product, we can extract the one we
167 -- are interested in, once more by induction on k.
168
169 Projₙ : ∀ {n ls} → Sets n ls → ∀ k → Set (Levelₙ ls k)
170 Projₙ (a , _) zero = a
171 Projₙ (_ , as) (suc k) = Projₙ as k
172
173 -- Finally, provided a Product of these sets, we can extract the k-th
174 -- value. `projₙ` takes both `n` and `k` explicitly because we expect
175 -- the user will be using a concrete `k` (potentially manufactured
176 -- using `Data.Fin`'s `#_`) and it will not be possible to infer `n`
177 -- from it.
178
179 projₙ : ∀ n {ls} {as : Sets n ls} k → Product n as → Projₙ as k
180 projₙ 1 zero v = v
181 projₙ (suc n@(suc _)) zero (v , _) = v
182 projₙ (suc n@(suc _)) (suc k) (_ , vs) = projₙ n k vs
183
184 ------------------------------------------------------------------------
185 -- zip
186
187 zipWith : ∀ n {lsa lsb lsc}
188 {as : Sets n lsa} {bs : Sets n lsb} {cs : Sets n lsc} →
189 (∀ k → Projₙ as k → Projₙ bs k → Projₙ cs k) →
190 Product n as → Product n bs → Product n cs
191 zipWith 0 f _ _ = _
192 zipWith 1 f v w = f zero v w
193 zipWith (suc n@(suc _)) f (v , vs) (w , ws) =
194 f zero v w , zipWith n (λ k → f (suc k)) vs ws
195
196 ------------------------------------------------------------------------
197 -- removal of the k-th component
198
199 Levelₙ⁻ : ∀ {n} → Levels n → Fin n → Levels (pred n)
200 Levelₙ⁻ (_ , ls) zero = ls
201 Levelₙ⁻ {suc (suc _)} (l , ls) (suc k) = l , Levelₙ⁻ ls k
202
203 Removeₙ : ∀ {n ls} → Sets n ls → ∀ k → Sets (pred n) (Levelₙ⁻ ls k)
204 Removeₙ (_ , as) zero = as
205 Removeₙ {suc (suc _)} (a , as) (suc k) = a , Removeₙ as k
206
207 removeₙ : ∀ n {ls} {as : Sets n ls} k →
208 Product n as → Product (pred n) (Removeₙ as k)
209 removeₙ (suc zero) zero _ = _
210 removeₙ (suc (suc _)) zero (_ , vs) = vs
211 removeₙ (suc (suc zero)) (suc k) (v , _) = v
212 removeₙ (suc (suc (suc _))) (suc k) (v , vs) = v , removeₙ _ k vs
213
214 ------------------------------------------------------------------------
215 -- insertion of a k-th component
216
217 Levelₙ⁺ : ∀ {n} → Levels n → Fin (suc n) → Level → Levels (suc n)
218 Levelₙ⁺ ls zero l⁺ = l⁺ , ls
219 Levelₙ⁺ {suc _} (l , ls) (suc k) l⁺ = l , Levelₙ⁺ ls k l⁺
220
221 Insertₙ : ∀ {n ls l⁺} → Sets n ls → ∀ k (a⁺ : Set l⁺) → Sets (suc n) (Levelₙ⁺ ls k l⁺)
222 Insertₙ as zero a⁺ = a⁺ , as
223 Insertₙ {suc _} (a , as) (suc k) a⁺ = a , Insertₙ as k a⁺
224
225 insertₙ : ∀ n {ls l⁺} {as : Sets n ls} {a⁺ : Set l⁺} k (v⁺ : a⁺) →
226 Product n as → Product (suc n) (Insertₙ as k a⁺)
227 insertₙ 0 zero v⁺ vs = v⁺
228 insertₙ (suc n) zero v⁺ vs = v⁺ , vs
229 insertₙ 1 (suc k) v⁺ vs = vs , insertₙ 0 k v⁺ _
230 insertₙ (suc n@(suc _)) (suc k) v⁺ (v , vs) = v , insertₙ n k v⁺ vs
231
232 ------------------------------------------------------------------------
233 -- update of a k-th component
234
235 Levelₙᵘ : ∀ {n} → Levels n → Fin n → Level → Levels n
236 Levelₙᵘ (_ , ls) zero lᵘ = lᵘ , ls
237 Levelₙᵘ (l , ls) (suc k) lᵘ = l , Levelₙᵘ ls k lᵘ
238
239 Updateₙ : ∀ {n ls lᵘ} (as : Sets n ls) k (aᵘ : Set lᵘ) → Sets n (Levelₙᵘ ls k lᵘ)
240 Updateₙ (_ , as) zero aᵘ = aᵘ , as
241 Updateₙ (a , as) (suc k) aᵘ = a , Updateₙ as k aᵘ
242
243 updateₙ : ∀ n {ls lᵘ} {as : Sets n ls} k {aᵘ : _ → Set lᵘ} (f : ∀ v → aᵘ v)
244 (vs : Product n as) → Product n (Updateₙ as k (aᵘ (projₙ n k vs)))
245 updateₙ 1 zero f v = f v
246 updateₙ (suc (suc _)) zero f (v , vs) = f v , vs
247 updateₙ (suc n@(suc _)) (suc k) f (v , vs) = v , updateₙ n k f vs
248
249 updateₙ′ : ∀ n {ls lᵘ} {as : Sets n ls} k {aᵘ : Set lᵘ} (f : Projₙ as k → aᵘ) →
250 Product n as → Product n (Updateₙ as k aᵘ)
251 updateₙ′ n k = updateₙ n k
252
253 ------------------------------------------------------------------------
254 -- DEPRECATED NAMES
255 ------------------------------------------------------------------------
256 -- Please use the new names as continuing support for the old names is
257 -- not guaranteed.
258
259 -- Version 2.3
260
261 Allₙ = Pointwiseₙ
262 {-# WARNING_ON_USAGE Allₙ
263 "Warning: Allₙ was deprecated in v2.3. Please use Pointwiseₙ instead."
264 #-}