Skip to content

Commit 747351e

Browse files
authored
[ fixed #1452 ] more universe-polymorphic combinators for indexed containers (#1458)
1 parent f5809ab commit 747351e

File tree

4 files changed

+134
-67
lines changed

4 files changed

+134
-67
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Bug-fixes
3535
`Relation.Binary.Reasoning.Base.Partial`, they will need to adjust their additional
3636
combinators to use the new `singleStep`/`multiStep` constructors of `_IsRelatedTo_`.
3737

38+
* The sum operator `_⊎_` in `Data.Container.Indexed.Combinator` was not as universe
39+
polymorphic as it should have been. This has been fixed. The old, less universe
40+
polymorphic variant is still available under the new name `_⊎′_`.
41+
3842
* The proof `isEquivalence` in `Function.Properties.(Equivalence/Inverse)` used to be
3943
defined in an anonymous module that took two unneccessary `Setoid` arguments:
4044
```agda
@@ -178,6 +182,11 @@ New modules
178182
Data.List.Relation.Binary.Pointwise.Properties
179183
```
180184

185+
* Heterogeneous `All` predicate for disjoint sums:
186+
```
187+
Data.Sum.Relation.Unary.All
188+
```
189+
181190
* Broke up `Codata.Musical.Colist` into a multitude of modules:
182191
```
183192
Codata.Musical.Colist.Base

src/Data/Container/Indexed/Combinator.agda

Lines changed: 85 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ open import Data.Empty.Polymorphic using (⊥; ⊥-elim)
1414
open import Data.Unit.Polymorphic.Base using (⊤)
1515
open import Data.Product as Prod hiding (Σ) renaming (_×_ to _⟨×⟩_)
1616
open import Data.Sum renaming (_⊎_ to _⟨⊎⟩_)
17+
open import Data.Sum.Relation.Unary.All as All using (All)
1718
open import Function as F hiding (id; const) renaming (_∘_ to _⟨∘⟩_)
1819
open import Function.Inverse using (_↔̇_; inverse)
1920
open import Level
@@ -22,51 +23,30 @@ open import Relation.Unary using (Pred; _⊆_; _∪_; _∩_; ⋃; ⋂)
2223
open import Relation.Binary.PropositionalEquality as P
2324
using (_≗_; refl)
2425

26+
private
27+
variable
28+
ℓ ℓ₁ ℓ₂ i j k o c c₁ c₂ r r₁ r₂ x z : Level
29+
I J K O X Z : Set _
30+
2531
------------------------------------------------------------------------
2632
-- Combinators
2733

2834

2935
-- Identity.
3036

31-
id : {o c r} {O : Set o} Container O O c r
37+
id : Container O O c r
3238
id = F.const ⊤ ◃ F.const ⊤ / (λ {o} _ _ o)
3339

3440
-- Constant.
3541

36-
const : {i o c r} {I : Set i} {O : Set o}
37-
Pred O c Container I O c r
42+
const : Pred O c Container I O c r
3843
const X = X ◃ F.const ⊥ / F.const ⊥-elim
3944

40-
-- Duality.
41-
42-
_^⊥ : {i o c r} {I : Set i} {O : Set o}
43-
Container I O c r Container I O (c ⊔ r) c
44-
(C ◃ R / n) ^⊥ = record
45-
{ Command = λ o (c : C o) R c
46-
; Response = λ {o} _ C o
47-
; next = λ f c n c (f c)
48-
}
49-
50-
-- Strength.
51-
52-
infixl 3 _⋊_
53-
54-
_⋊_ : {i o c r z} {I : Set i} {O : Set o} (C : Container I O c r)
55-
(Z : Set z) Container (I ⟨×⟩ Z) (O ⟨×⟩ Z) c r
56-
C ◃ R / n ⋊ Z = C ⟨∘⟩ proj₁ ◃ R / λ {oz} c r n c r , proj₂ oz
57-
58-
infixr 3 _⋉_
59-
60-
_⋉_ : {i o z c r} {I : Set i} {O : Set o} (Z : Set z)
61-
(C : Container I O c r) Container (Z ⟨×⟩ I) (Z ⟨×⟩ O) c r
62-
Z ⋉ C ◃ R / n = C ⟨∘⟩ proj₂ ◃ R / λ {zo} c r proj₁ zo , n c r
63-
6445
-- Composition.
6546

6647
infixr 9 _∘_
6748

68-
_∘_ : {i j k c r} {I : Set i} {J : Set j} {K : Set k}
69-
Container J K c r Container I J c r Container I K _ _
49+
_∘_ : Container J K c₁ r₁ Container I J c₂ r₂ Container I K _ _
7050
C₁ ∘ C₂ = C ◃ R / n
7151
where
7252
C : k Set _
@@ -78,13 +58,37 @@ C₁ ∘ C₂ = C ◃ R / n
7858
n : {k} (c : ⟦ C₁ ⟧ (Command C₂) k) R c _
7959
n (_ , f) (r₁ , r₂) = next C₂ (f r₁) r₂
8060

81-
-- Product. (Note that, up to isomorphism, this is a special case of
82-
-- indexed product.)
61+
-- Duality.
62+
63+
_^⊥ : Container I O c r Container I O (c ⊔ r) c
64+
(C ^⊥) .Command o = (c : C .Command o) C .Response c
65+
(C ^⊥) .Response {o} _ = C .Command o
66+
(C ^⊥) .next f c = C .next c (f c)
67+
68+
-- Strength.
69+
70+
infixl 3 _⋊_
71+
72+
_⋊_ : Container I O c r (Z : Set z) Container (I ⟨×⟩ Z) (O ⟨×⟩ Z) c r
73+
(C ⋊ Z) .Command (o , z) = C .Command o
74+
(C ⋊ Z) .Response = C .Response
75+
(C ⋊ Z) .next {o , z} c r = C .next c r , z
76+
77+
infixr 3 _⋉_
78+
79+
_⋉_ : (Z : Set z) Container I O c r Container (Z ⟨×⟩ I) (Z ⟨×⟩ O) c r
80+
(Z ⋉ C) .Command (z , o) = C .Command o
81+
(Z ⋉ C) .Response = C .Response
82+
(Z ⋉ C) .next {z , o} c r = z , C .next c r
83+
84+
85+
86+
-- Product. (Note that, up to isomorphism, and ignoring universe level
87+
-- issues, this is a special case of indexed product.)
8388

8489
infixr 2 _×_
8590

86-
_×_ : {i o c r} {I : Set i} {O : Set o}
87-
Container I O c r Container I O c r Container I O c r
91+
_×_ : Container I O c₁ r₁ Container I O c₂ r₂ Container I O _ _
8892
(C₁ ◃ R₁ / n₁) × (C₂ ◃ R₂ / n₂) = record
8993
{ Command = C₁ ∩ C₂
9094
; Response = R₁ ⟪⊙⟫ R₂
@@ -93,31 +97,35 @@ _×_ : ∀ {i o c r} {I : Set i} {O : Set o} →
9397

9498
-- Indexed product.
9599

96-
Π : {x i o c r} {X : Set x} {I : Set i} {O : Set o}
97-
(X Container I O c r) Container I O _ _
100+
Π : (X Container I O c r) Container I O _ _
98101
Π {X = X} C = record
99102
{ Command = ⋂ X (Command ⟨∘⟩ C)
100103
; Response = ⋃[ x ∶ X ] λ c Response (C x) (c x)
101104
; next = λ { c (x , r) next (C x) (c x) r }
102105
}
103106

104-
-- Sum. (Note that, up to isomorphism, this is a special case of
105-
-- indexed sum.)
107+
-- Sum. (Note that, up to isomorphism, and ignoring universe level
108+
-- issues, this is a special case of indexed sum.)
109+
110+
infixr 1 _⊎_ _⊎′_
106111

107-
infixr 1 _⊎_
112+
_⊎_ : Container I O c₁ r₁ Container I O c₂ r₂ Container I O _ _
113+
(C₁ ⊎ C₂) .Command = C₁ .Command ∪ C₂ .Command
114+
(C₁ ⊎ C₂) .Response = All (C₁ .Response) (C₂ .Response)
115+
(C₁ ⊎ C₂) .next = All.[ C₁ .next , C₂ .next ]
108116

109-
_⊎_ : {i o c r} {I : Set i} {O : Set o}
110-
Container I O c r Container I O c r Container I O _ _
111-
(C₁ ◃ R₁ / n₁) ⊎ (C₂ ◃ R₂ / n₂) = record
117+
-- A simplified version for responses at the same level r:
118+
119+
_⊎′_ : Container I O c₁ r Container I O c₂ r Container I O _ r
120+
(C₁ ◃ R₁ / n₁) ⊎′ (C₂ ◃ R₂ / n₂) = record
112121
{ Command = C₁ ∪ C₂
113-
; Response = R₁ ⟪⊎⟫ R₂
122+
; Response = [ R₁ , R₂ ]
114123
; next = [ n₁ , n₂ ]
115124
}
116125

117126
-- Indexed sum.
118127

119-
Σ : {x i o c r} {X : Set x} {I : Set i} {O : Set o}
120-
(X Container I O c r) Container I O _ r
128+
Σ : (X Container I O c r) Container I O _ r
121129
Σ {X = X} C = record
122130
{ Command = ⋃ X (Command ⟨∘⟩ C)
123131
; Response = λ { (x , c) Response (C x) c }
@@ -129,17 +137,15 @@ _⊎_ : ∀ {i o c r} {I : Set i} {O : Set o} →
129137

130138
infix 0 const[_]⟶_
131139

132-
const[_]⟶_ : {i o c r ℓ} {I : Set i} {O : Set o}
133-
Set Container I O c r Container I O _ _
140+
const[_]⟶_ : (X : Set ℓ) Container I O c r Container I O _ _
134141
const[ X ]⟶ C = Π {X = X} (F.const C)
135142

136143
------------------------------------------------------------------------
137144
-- Correctness proofs
138145

139146
module Identity where
140147

141-
correct : {o ℓ c r} {O : Set o} {X : Pred O ℓ}
142-
⟦ id {c = c}{r} ⟧ X ↔̇ F.id X
148+
correct : {X : Pred O ℓ} ⟦ id {c = c}{r} ⟧ X ↔̇ F.id X
143149
correct {X = X} = inverse to from (λ _ refl) (λ _ refl)
144150
where
145151
to : {x} ⟦ id ⟧ X x F.id X x
@@ -150,8 +156,7 @@ module Identity where
150156

151157
module Constant (ext : {ℓ} Extensionality ℓ ℓ) where
152158

153-
correct : {i o ℓ₁ ℓ₂} {I : Set i} {O : Set o} (X : Pred O ℓ₁)
154-
{Y : Pred O ℓ₂} ⟦ const X ⟧ Y ↔̇ F.const X Y
159+
correct : (X : Pred O ℓ₁) {Y : Pred O ℓ₂} ⟦ const X ⟧ Y ↔̇ F.const X Y
155160
correct X {Y} = record
156161
{ to = P.→-to-⟶ to
157162
; from = P.→-to-⟶ from
@@ -172,16 +177,14 @@ module Constant (ext : ∀ {ℓ} → Extensionality ℓ ℓ) where
172177

173178
module Duality where
174179

175-
correct : {i o c r ℓ} {I : Set i} {O : Set o}
176-
(C : Container I O c r) (X : Pred I ℓ)
180+
correct : (C : Container I O c r) (X : Pred I ℓ)
177181
⟦ C ^⊥ ⟧ X ↔̇ (λ o (c : Command C o) λ r X (next C c r))
178182
correct C X = inverse (λ { (f , g) < f , g > }) (λ f proj₁ ⟨∘⟩ f , proj₂ ⟨∘⟩ f)
179183
(λ _ refl) (λ _ refl)
180184

181185
module Composition where
182186

183-
correct : {i j k ℓ c r} {I : Set i} {J : Set j} {K : Set k}
184-
(C₁ : Container J K c r) (C₂ : Container I J c r)
187+
correct : (C₁ : Container J K c r) (C₂ : Container I J c r)
185188
{X : Pred I ℓ} ⟦ C₁ ∘ C₂ ⟧ X ↔̇ (⟦ C₁ ⟧ ⟨∘⟩ ⟦ C₂ ⟧) X
186189
correct C₁ C₂ {X} = inverse to from (λ _ refl) (λ _ refl)
187190
where
@@ -193,8 +196,7 @@ module Composition where
193196

194197
module Product (ext : {ℓ} Extensionality ℓ ℓ) where
195198

196-
correct : {i o c r} {I : Set i} {O : Set o}
197-
(C₁ C₂ : Container I O c r) {X}
199+
correct : (C₁ C₂ : Container I O c r) {X : Pred I _}
198200
⟦ C₁ × C₂ ⟧ X ↔̇ (⟦ C₁ ⟧ X ∩ ⟦ C₂ ⟧ X)
199201
correct C₁ C₂ {X} = inverse to from from∘to (λ _ refl)
200202
where
@@ -210,8 +212,7 @@ module Product (ext : ∀ {ℓ} → Extensionality ℓ ℓ) where
210212

211213
module IndexedProduct where
212214

213-
correct : {x i o c r ℓ} {X : Set x} {I : Set i} {O : Set o}
214-
(C : X Container I O c r) {Y : Pred I ℓ}
215+
correct : (C : X Container I O c r) {Y : Pred I ℓ}
215216
⟦ Π C ⟧ Y ↔̇ ⋂[ x ∶ X ] ⟦ C x ⟧ Y
216217
correct {X = X} C {Y} = inverse to from (λ _ refl) (λ _ refl)
217218
where
@@ -221,18 +222,38 @@ module IndexedProduct where
221222
from : ⋂[ x ∶ X ] ⟦ C x ⟧ Y ⊆ ⟦ Π C ⟧ Y
222223
from f = (proj₁ ⟨∘⟩ f , uncurry (proj₂ ⟨∘⟩ f))
223224

224-
module Sum where
225+
module Sum (ext : {ℓ₁ ℓ₂} Extensionality ℓ₁ ℓ₂) where
225226

226-
correct : {i o c r ℓ} {I : Set i} {O : Set o}
227-
(C₁ C₂ : Container I O c r) {X : Pred I ℓ}
227+
correct : (C₁ C₂ : Container I O c r) {X : Pred I ℓ}
228228
⟦ C₁ ⊎ C₂ ⟧ X ↔̇ (⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X)
229229
correct C₁ C₂ {X} = inverse to from from∘to to∘from
230230
where
231231
to : ⟦ C₁ ⊎ C₂ ⟧ X ⊆ ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X
232+
to (inj₁ c₁ , k) = inj₁ (c₁ , λ r k (All.inj₁ r))
233+
to (inj₂ c₂ , k) = inj₂ (c₂ , λ r k (All.inj₂ r))
234+
235+
from : ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X ⊆ ⟦ C₁ ⊎ C₂ ⟧ X
236+
from (inj₁ (c , f)) = inj₁ c , λ{ (All.inj₁ r) f r}
237+
from (inj₂ (c , f)) = inj₂ c , λ{ (All.inj₂ r) f r}
238+
239+
from∘to : from ⟨∘⟩ to ≗ F.id
240+
from∘to (inj₁ _ , _) = P.cong (inj₁ _ ,_) (ext λ{ (All.inj₁ r) refl})
241+
from∘to (inj₂ _ , _) = P.cong (inj₂ _ ,_) (ext λ{ (All.inj₂ r) refl})
242+
243+
to∘from : to ⟨∘⟩ from ≗ F.id
244+
to∘from = [ (λ _ refl) , (λ _ refl) ]
245+
246+
module Sum′ where
247+
248+
correct : (C₁ C₂ : Container I O c r) {X : Pred I ℓ}
249+
⟦ C₁ ⊎′ C₂ ⟧ X ↔̇ (⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X)
250+
correct C₁ C₂ {X} = inverse to from from∘to to∘from
251+
where
252+
to : ⟦ C₁ ⊎′ C₂ ⟧ X ⊆ ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X
232253
to (inj₁ c₁ , k) = inj₁ (c₁ , k)
233254
to (inj₂ c₂ , k) = inj₂ (c₂ , k)
234255

235-
from : ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X ⊆ ⟦ C₁ ⊎ C₂ ⟧ X
256+
from : ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X ⊆ ⟦ C₁ ⊎ C₂ ⟧ X
236257
from = [ Prod.map inj₁ F.id , Prod.map inj₂ F.id ]
237258

238259
from∘to : from ⟨∘⟩ to ≗ F.id
@@ -244,8 +265,7 @@ module Sum where
244265

245266
module IndexedSum where
246267

247-
correct : {x i o c r ℓ} {X : Set x} {I : Set i} {O : Set o}
248-
(C : X Container I O c r) {Y : Pred I ℓ}
268+
correct : (C : X Container I O c r) {Y : Pred I ℓ}
249269
⟦ Σ C ⟧ Y ↔̇ ⋃[ x ∶ X ] ⟦ C x ⟧ Y
250270
correct {X = X} C {Y} = inverse to from (λ _ refl) (λ _ refl)
251271
where
@@ -257,7 +277,6 @@ module IndexedSum where
257277

258278
module ConstantExponentiation where
259279

260-
correct : {x i o c r ℓ} {X : Set x} {I : Set i} {O : Set o}
261-
(C : Container I O c r) {Y : Pred I ℓ}
280+
correct : (C : Container I O c r) {Y : Pred I ℓ}
262281
⟦ const[ X ]⟶ C ⟧ Y ↔̇ (⋂ X (F.const (⟦ C ⟧ Y)))
263282
correct C {Y} = IndexedProduct.correct (F.const C) {Y}

src/Data/Container/Indexed/FreeMonad.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ infix 9 _⋆_
2828

2929
_⋆C_ : {i o c r} {I : Set i} {O : Set o}
3030
Container I O c r Pred O c Container I O _ _
31-
C ⋆C X = const X ⊎ C
31+
C ⋆C X = const X ⊎ C
3232

3333
_⋆_ : {ℓ} {O : Set ℓ} Container O O ℓ ℓ Pt O ℓ
3434
C ⋆ X = μ (C ⋆C X)

src/Data/Sum/Relation/Unary/All.agda

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Heterogeneous `All` predicate for disjoint sums
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
module Data.Sum.Relation.Unary.All where
10+
11+
open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
12+
open import Level using (Level; _⊔_)
13+
open import Relation.Unary using (Pred)
14+
15+
private
16+
variable
17+
a b c p q : Level
18+
A B : Set _
19+
P Q : Pred A p
20+
21+
------------------------------------------------------------------------
22+
-- Definition
23+
24+
data All {A : Set a} {B : Set b} (P : Pred A p) (Q : Pred B q)
25+
: Pred (A ⊎ B) (a ⊔ b ⊔ p ⊔ q) where
26+
inj₁ : {a} P a All P Q (inj₁ a)
27+
inj₂ : {b} Q b All P Q (inj₂ b)
28+
29+
------------------------------------------------------------------------
30+
-- Operations
31+
32+
-- Elimination
33+
34+
[_,_] : {C : (x : A ⊎ B) All P Q x Set c}
35+
((x : A) (y : P x) C (inj₁ x) (inj₁ y))
36+
((x : B) (y : Q x) C (inj₂ x) (inj₂ y))
37+
(x : A ⊎ B) (y : All P Q x) C x y
38+
[ f , g ] (inj₁ x) (inj₁ y) = f x y
39+
[ f , g ] (inj₂ x) (inj₂ y) = g x y

0 commit comments

Comments
 (0)