Skip to content

Commit 915d81c

Browse files
Some extra support for homogeneous n-ary products (#2736)
* Some extra support for homogeneous n-ary products * Update src/Data/Product/Nary/NonDependent.agda Co-authored-by: jamesmckinna <[email protected]> --------- Co-authored-by: jamesmckinna <[email protected]>
1 parent 2d5732c commit 915d81c

File tree

4 files changed

+86
-34
lines changed

4 files changed

+86
-34
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ Deprecated names
150150
left-inverse ↦ rightInverse
151151
```
152152

153+
* In `Data.Product.Nary.NonDependent`:
154+
```agda
155+
Allₙ ↦ Pointwiseₙ
156+
```
157+
153158
New modules
154159
-----------
155160

@@ -355,6 +360,12 @@ Additions to existing modules
355360
LeftInverse (I ×ₛ A) (J ×ₛ B)
356361
```
357362

363+
* In `Data.Product.Nary.NonDependent`:
364+
```agda
365+
HomoProduct′ n f = Product n (stabulate n (const _) f)
366+
HomoProduct n A = HomoProduct′ n (const A)
367+
```
368+
358369
* In `Data.Vec.Relation.Binary.Pointwise.Inductive`:
359370
```agda
360371
zipWith-assoc : Associative _∼_ f → Associative (Pointwise _∼_) (zipWith {n = n} f)
@@ -364,6 +375,13 @@ Additions to existing modules
364375
zipWith-cong : Congruent₂ _∼_ f → Pointwise _∼_ ws xs → Pointwise _∼_ ys zs → Pointwise _∼_ (zipWith f ws ys) (zipWith f xs zs)
365376
```
366377

378+
* In `Function.Nary.NonDependent.Base`:
379+
```agda
380+
lconst l n = ⨆ l (lreplicate l n)
381+
stabulate : ∀ n → (f : Fin n → Level) → (g : (i : Fin n) → Set (f i)) → Sets n (ltabulate n f)
382+
sreplicate : ∀ n → Set a → Sets n (lreplicate n a)
383+
```
384+
367385
* In `Relation.Binary.Construct.Add.Infimum.Strict`:
368386
```agda
369387
<₋-accessible-⊥₋ : Acc _<₋_ ⊥₋

src/Data/Product/Nary/NonDependent.agda

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,39 @@ Product 0 _ = ⊤
4646
Product 1 (a , _) = a
4747
Product (suc n) (a , as) = a × Product n as
4848

49-
-- Pointwise lifting of a relation on products
49+
-- An n-ary product where every element of the product lives at the same universe level.
5050

51-
Allₙ : ( {a} {A : Set a} Rel A a)
52-
n {ls} {as : Sets n ls} (l r : Product n as) Sets n ls
53-
Allₙ R 0 l r = _
54-
Allₙ R 1 a b = R a b , _
55-
Allₙ R (suc n@(suc _)) (a , l) (b , r) = R a b , Allₙ R n l r
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
5668

5769
Equalₙ : n {ls} {as : Sets n ls} (l r : Product n as) Sets n ls
58-
Equalₙ = Allₙ _≡_
70+
Equalₙ = Pointwiseₙ _≡_
5971

6072
------------------------------------------------------------------------
6173
-- Generic Programs
74+
------------------------------------------------------------------------
6275

6376
-- Once we have these type definitions, we can write generic programs
6477
-- over them. They will typically be split into two or three definitions:
6578

6679
-- 1. action on the vector of n levels (if any)
6780
-- 2. action on the corresponding vector of n Sets
6881
-- 3. actual program, typed thank to the function defined in step 2.
69-
------------------------------------------------------------------------
7082

7183
-- see Relation.Binary.PropositionalEquality for congₙ and substₙ, two
7284
-- equality-related generic programs.
@@ -168,7 +180,6 @@ projₙ : ∀ n {ls} {as : Sets n ls} k → Product n as → Projₙ as k
168180
projₙ 1 zero v = v
169181
projₙ (suc n@(suc _)) zero (v , _) = v
170182
projₙ (suc n@(suc _)) (suc k) (_ , vs) = projₙ n k vs
171-
projₙ 1 (suc ()) v
172183

173184
------------------------------------------------------------------------
174185
-- zip
@@ -188,41 +199,35 @@ zipWith (suc n@(suc _)) f (v , vs) (w , ws) =
188199
Levelₙ⁻ : {n} Levels n Fin n Levels (pred n)
189200
Levelₙ⁻ (_ , ls) zero = ls
190201
Levelₙ⁻ {suc (suc _)} (l , ls) (suc k) = l , Levelₙ⁻ ls k
191-
Levelₙ⁻ {1} _ (suc ())
192202

193203
Removeₙ : {n ls} Sets n ls k Sets (pred n) (Levelₙ⁻ ls k)
194204
Removeₙ (_ , as) zero = as
195205
Removeₙ {suc (suc _)} (a , as) (suc k) = a , Removeₙ as k
196-
Removeₙ {1} _ (suc ())
197206

198207
removeₙ : n {ls} {as : Sets n ls} k
199208
Product n as Product (pred n) (Removeₙ as k)
200209
removeₙ (suc zero) zero _ = _
201210
removeₙ (suc (suc _)) zero (_ , vs) = vs
202211
removeₙ (suc (suc zero)) (suc k) (v , _) = v
203212
removeₙ (suc (suc (suc _))) (suc k) (v , vs) = v , removeₙ _ k vs
204-
removeₙ (suc zero) (suc ()) _
205213

206214
------------------------------------------------------------------------
207215
-- insertion of a k-th component
208216

209217
Levelₙ⁺ : {n} Levels n Fin (suc n) Level Levels (suc n)
210218
Levelₙ⁺ ls zero l⁺ = l⁺ , ls
211219
Levelₙ⁺ {suc _} (l , ls) (suc k) l⁺ = l , Levelₙ⁺ ls k l⁺
212-
Levelₙ⁺ {0} _ (suc ())
213220

214221
Insertₙ : {n ls l⁺} Sets n ls k (a⁺ : Set l⁺) Sets (suc n) (Levelₙ⁺ ls k l⁺)
215222
Insertₙ as zero a⁺ = a⁺ , as
216223
Insertₙ {suc _} (a , as) (suc k) a⁺ = a , Insertₙ as k a⁺
217-
Insertₙ {zero} _ (suc ()) _
218224

219225
insertₙ : n {ls l⁺} {as : Sets n ls} {a⁺ : Set l⁺} k (v⁺ : a⁺)
220226
Product n as Product (suc n) (Insertₙ as k a⁺)
221227
insertₙ 0 zero v⁺ vs = v⁺
222228
insertₙ (suc n) zero v⁺ vs = v⁺ , vs
223229
insertₙ 1 (suc k) v⁺ vs = vs , insertₙ 0 k v⁺ _
224230
insertₙ (suc n@(suc _)) (suc k) v⁺ (v , vs) = v , insertₙ n k v⁺ vs
225-
insertₙ 0 (suc ()) _ _
226231

227232
------------------------------------------------------------------------
228233
-- update of a k-th component
@@ -240,8 +245,20 @@ updateₙ : ∀ n {ls lᵘ} {as : Sets n ls} k {aᵘ : _ → Set lᵘ} (f : ∀
240245
updateₙ 1 zero f v = f v
241246
updateₙ (suc (suc _)) zero f (v , vs) = f v , vs
242247
updateₙ (suc n@(suc _)) (suc k) f (v , vs) = v , updateₙ n k f vs
243-
updateₙ 1 (suc ()) _ _
244248

245249
updateₙ′ : n {ls lᵘ} {as : Sets n ls} k {aᵘ : Set lᵘ} (f : Projₙ as k aᵘ)
246250
Product n as Product n (Updateₙ as k aᵘ)
247251
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+
#-}

src/Function/Nary/NonDependent.agda

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ open import Data.Product.Nary.NonDependent
2222
using (Product; uncurryₙ; Equalₙ; curryₙ; fromEqualₙ; toEqualₙ)
2323
open import Function.Base using (_∘′_; _$′_; const; flip)
2424
open import Relation.Unary using (IUniversal)
25-
open import Relation.Binary.PropositionalEquality.Core using (_≡_; cong)
25+
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; subst; cong)
2626

2727
private
2828
variable
@@ -35,19 +35,6 @@ private
3535

3636
open import Function.Nary.NonDependent.Base public
3737

38-
------------------------------------------------------------------------
39-
-- Additional operations on Levels
40-
41-
ltabulate : n (Fin n Level) Levels n
42-
ltabulate zero f = _
43-
ltabulate (suc n) f = f zero , ltabulate n (f ∘′ suc)
44-
45-
lreplicate : n Level Levels n
46-
lreplicate n ℓ = ltabulate n (const ℓ)
47-
48-
0ℓs : ∀[ Levels ]
49-
0ℓs = lreplicate _ 0ℓ
50-
5138
------------------------------------------------------------------------
5239
-- Congruence
5340

src/Function/Nary/NonDependent/Base.agda

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ open import Data.Nat.Base using (ℕ; zero; suc)
1919
open import Data.Product.Base using (_×_; _,_)
2020
open import Data.Unit.Polymorphic.Base using (⊤; tt)
2121
open import Function.Base using (_∘′_; _$′_; const; flip)
22+
open import Data.Fin.Base using (Fin; zero; suc)
23+
open import Relation.Unary using (IUniversal)
2224

2325
private
2426
variable
@@ -80,6 +82,28 @@ infixr 0 _⇉_
8082
_⇉_ : {n ls r} Sets n ls Set r Set (r ⊔ (⨆ n ls))
8183
_⇉_ = Arrows _
8284

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+
83107
------------------------------------------------------------------------
84108
-- Operations on Sets
85109

@@ -94,15 +118,21 @@ _<$>_ f {suc n} (a , as) = f a , (f <$> as)
94118

95119
-- Level-modifying generalised map
96120

97-
lmap : (Level Level) n Levels n Levels n
98-
lmap f zero ls = _
99-
lmap f (suc n) (l , ls) = f l , lmap f n ls
100-
101121
smap : f ( {l} Set l Set (f l))
102122
n {ls} Sets n ls Sets n (lmap f n ls)
103123
smap f F zero as = _
104124
smap f F (suc n) (a , as) = F a , smap f F n as
105125

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+
106136
------------------------------------------------------------------------
107137
-- Operations on Functions
108138

0 commit comments

Comments
 (0)