@@ -14,6 +14,7 @@ open import Data.Empty.Polymorphic using (⊥; ⊥-elim)
14
14
open import Data.Unit.Polymorphic.Base using (⊤)
15
15
open import Data.Product as Prod hiding (Σ) renaming (_×_ to _⟨×⟩_)
16
16
open import Data.Sum renaming (_⊎_ to _⟨⊎⟩_)
17
+ open import Data.Sum.Relation.Unary.All as All using (All)
17
18
open import Function as F hiding (id; const) renaming (_∘_ to _⟨∘⟩_)
18
19
open import Function.Inverse using (_↔̇_; inverse)
19
20
open import Level
@@ -22,51 +23,30 @@ open import Relation.Unary using (Pred; _⊆_; _∪_; _∩_; ⋃; ⋂)
22
23
open import Relation.Binary.PropositionalEquality as P
23
24
using (_≗_; refl)
24
25
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
+
25
31
------------------------------------------------------------------------
26
32
-- Combinators
27
33
28
34
29
35
-- Identity.
30
36
31
- id : ∀ {o c r} {O : Set o} → Container O O c r
37
+ id : Container O O c r
32
38
id = F.const ⊤ ◃ F.const ⊤ / (λ {o} _ _ → o)
33
39
34
40
-- Constant.
35
41
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
38
43
const X = X ◃ F.const ⊥ / F.const ⊥-elim
39
44
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
-
64
45
-- Composition.
65
46
66
47
infixr 9 _∘_
67
48
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 _ _
70
50
C₁ ∘ C₂ = C ◃ R / n
71
51
where
72
52
C : ∀ k → Set _
@@ -78,13 +58,37 @@ C₁ ∘ C₂ = C ◃ R / n
78
58
n : ∀ {k} (c : ⟦ C₁ ⟧ (Command C₂) k) → R c → _
79
59
n (_ , f) (r₁ , r₂) = next C₂ (f r₁) r₂
80
60
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.)
83
88
84
89
infixr 2 _×_
85
90
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 _ _
88
92
(C₁ ◃ R₁ / n₁) × (C₂ ◃ R₂ / n₂) = record
89
93
{ Command = C₁ ∩ C₂
90
94
; Response = R₁ ⟪⊙⟫ R₂
@@ -93,31 +97,35 @@ _×_ : ∀ {i o c r} {I : Set i} {O : Set o} →
93
97
94
98
-- Indexed product.
95
99
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 _ _
98
101
Π {X = X} C = record
99
102
{ Command = ⋂ X (Command ⟨∘⟩ C)
100
103
; Response = ⋃[ x ∶ X ] λ c → Response (C x) (c x)
101
104
; next = λ { c (x , r) → next (C x) (c x) r }
102
105
}
103
106
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 _⊎_ _⊎′_
106
111
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 ]
108
116
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
112
121
{ Command = C₁ ∪ C₂
113
- ; Response = R₁ ⟪⊎⟫ R₂
122
+ ; Response = [ R₁ , R₂ ]
114
123
; next = [ n₁ , n₂ ]
115
124
}
116
125
117
126
-- Indexed sum.
118
127
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
121
129
Σ {X = X} C = record
122
130
{ Command = ⋃ X (Command ⟨∘⟩ C)
123
131
; Response = λ { (x , c) → Response (C x) c }
@@ -129,17 +137,15 @@ _⊎_ : ∀ {i o c r} {I : Set i} {O : Set o} →
129
137
130
138
infix 0 const[_]⟶_
131
139
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 _ _
134
141
const[ X ]⟶ C = Π {X = X} (F.const C)
135
142
136
143
------------------------------------------------------------------------
137
144
-- Correctness proofs
138
145
139
146
module Identity where
140
147
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
143
149
correct {X = X} = inverse to from (λ _ → refl) (λ _ → refl)
144
150
where
145
151
to : ∀ {x} → ⟦ id ⟧ X x → F.id X x
@@ -150,8 +156,7 @@ module Identity where
150
156
151
157
module Constant (ext : ∀ {ℓ} → Extensionality ℓ ℓ) where
152
158
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
155
160
correct X {Y} = record
156
161
{ to = P.→-to-⟶ to
157
162
; from = P.→-to-⟶ from
@@ -172,16 +177,14 @@ module Constant (ext : ∀ {ℓ} → Extensionality ℓ ℓ) where
172
177
173
178
module Duality where
174
179
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 ℓ) →
177
181
⟦ C ^⊥ ⟧ X ↔̇ (λ o → (c : Command C o) → ∃ λ r → X (next C c r))
178
182
correct C X = inverse (λ { (f , g) → < f , g > }) (λ f → proj₁ ⟨∘⟩ f , proj₂ ⟨∘⟩ f)
179
183
(λ _ → refl) (λ _ → refl)
180
184
181
185
module Composition where
182
186
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) →
185
188
{X : Pred I ℓ} → ⟦ C₁ ∘ C₂ ⟧ X ↔̇ (⟦ C₁ ⟧ ⟨∘⟩ ⟦ C₂ ⟧) X
186
189
correct C₁ C₂ {X} = inverse to from (λ _ → refl) (λ _ → refl)
187
190
where
@@ -193,8 +196,7 @@ module Composition where
193
196
194
197
module Product (ext : ∀ {ℓ} → Extensionality ℓ ℓ) where
195
198
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 _} →
198
200
⟦ C₁ × C₂ ⟧ X ↔̇ (⟦ C₁ ⟧ X ∩ ⟦ C₂ ⟧ X)
199
201
correct C₁ C₂ {X} = inverse to from from∘to (λ _ → refl)
200
202
where
@@ -210,8 +212,7 @@ module Product (ext : ∀ {ℓ} → Extensionality ℓ ℓ) where
210
212
211
213
module IndexedProduct where
212
214
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 ℓ} →
215
216
⟦ Π C ⟧ Y ↔̇ ⋂[ x ∶ X ] ⟦ C x ⟧ Y
216
217
correct {X = X} C {Y} = inverse to from (λ _ → refl) (λ _ → refl)
217
218
where
@@ -221,18 +222,38 @@ module IndexedProduct where
221
222
from : ⋂[ x ∶ X ] ⟦ C x ⟧ Y ⊆ ⟦ Π C ⟧ Y
222
223
from f = (proj₁ ⟨∘⟩ f , uncurry (proj₂ ⟨∘⟩ f))
223
224
224
- module Sum where
225
+ module Sum (ext : ∀ {ℓ₁ ℓ₂} → Extensionality ℓ₁ ℓ₂) where
225
226
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 ℓ} →
228
228
⟦ C₁ ⊎ C₂ ⟧ X ↔̇ (⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X)
229
229
correct C₁ C₂ {X} = inverse to from from∘to to∘from
230
230
where
231
231
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
232
253
to (inj₁ c₁ , k) = inj₁ (c₁ , k)
233
254
to (inj₂ c₂ , k) = inj₂ (c₂ , k)
234
255
235
- from : ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X ⊆ ⟦ C₁ ⊎ C₂ ⟧ X
256
+ from : ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X ⊆ ⟦ C₁ ⊎′ C₂ ⟧ X
236
257
from = [ Prod.map inj₁ F.id , Prod.map inj₂ F.id ]
237
258
238
259
from∘to : from ⟨∘⟩ to ≗ F.id
@@ -244,8 +265,7 @@ module Sum where
244
265
245
266
module IndexedSum where
246
267
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 ℓ} →
249
269
⟦ Σ C ⟧ Y ↔̇ ⋃[ x ∶ X ] ⟦ C x ⟧ Y
250
270
correct {X = X} C {Y} = inverse to from (λ _ → refl) (λ _ → refl)
251
271
where
@@ -257,7 +277,6 @@ module IndexedSum where
257
277
258
278
module ConstantExponentiation where
259
279
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 ℓ} →
262
281
⟦ const[ X ]⟶ C ⟧ Y ↔̇ (⋂ X (F.const (⟦ C ⟧ Y)))
263
282
correct C {Y} = IndexedProduct.correct (F.const C) {Y}
0 commit comments