| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Indexed container combinators |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Container.Indexed.Combinator where |
| 10 |
|
| 11 |
open import Axiom.Extensionality.Propositional using (Extensionality) |
| 12 |
open import Data.Container.Indexed using (Container; _◃_/_; ⟦_⟧; |
| 13 |
Command; Response; ◇; next) |
| 14 |
open import Data.Empty.Polymorphic using (⊥; ⊥-elim) |
| 15 |
open import Data.Unit.Polymorphic.Base using (⊤) |
| 16 |
open import Data.Product.Base as Prod hiding (Σ) renaming (_×_ to _⟨×⟩_) |
| 17 |
open import Data.Sum.Base renaming (_⊎_ to _⟨⊎⟩_) |
| 18 |
open import Data.Sum.Relation.Unary.All as All using (All) |
| 19 |
open import Function.Base as F hiding (id; const) renaming (_∘_ to _⟨∘⟩_) |
| 20 |
open import Function.Bundles using (mk↔ₛ′) |
| 21 |
open import Function.Indexed.Bundles using (_↔ᵢ_) |
| 22 |
open import Level using (Level; _⊔_) |
| 23 |
open import Relation.Unary using (Pred; _⊆_; _∪_; _∩_; ⋃; ⋂) |
| 24 |
renaming (_⟨×⟩_ to _⟪×⟫_; _⟨⊙⟩_ to _⟪⊙⟫_; _⟨⊎⟩_ to _⟪⊎⟫_) |
| 25 |
open import Relation.Binary.PropositionalEquality.Core |
| 26 |
using (_≗_; refl; cong) |
| 27 |
|
| 28 |
private |
| 29 |
variable |
| 30 |
ℓ ℓ₁ ℓ₂ i j k o c c₁ c₂ r r₁ r₂ x z : Level |
| 31 |
I J K O X Z : Set _ |
| 32 |
|
| 33 |
------------------------------------------------------------------------ |
| 34 |
-- Combinators |
| 35 |
|
| 36 |
|
| 37 |
-- Identity. |
| 38 |
|
| 39 |
id : Container O O c r |
| 40 |
id = F.const ⊤ ◃ F.const ⊤ / (λ {o} _ _ → o) |
| 41 |
|
| 42 |
-- Constant. |
| 43 |
|
| 44 |
const : Pred O c → Container I O c r |
| 45 |
const X = X ◃ F.const ⊥ / F.const ⊥-elim |
| 46 |
|
| 47 |
-- Composition. |
| 48 |
|
| 49 |
infixr 9 _∘_ |
| 50 |
|
| 51 |
_∘_ : Container J K c₁ r₁ → Container I J c₂ r₂ → Container I K _ _ |
| 52 |
C₁ ∘ C₂ = C ◃ R / n |
| 53 |
where |
| 54 |
C : ∀ k → Set _ |
| 55 |
C = ⟦ C₁ ⟧ (Command C₂) |
| 56 |
|
| 57 |
R : ∀ {k} → ⟦ C₁ ⟧ (Command C₂) k → Set _ |
| 58 |
R (c , k) = ◇ C₁ {X = Command C₂} (Response C₂ ⟨∘⟩ proj₂) (_ , c , k) |
| 59 |
|
| 60 |
n : ∀ {k} (c : ⟦ C₁ ⟧ (Command C₂) k) → R c → _ |
| 61 |
n (_ , f) (r₁ , r₂) = next C₂ (f r₁) r₂ |
| 62 |
|
| 63 |
-- Duality. |
| 64 |
|
| 65 |
_^⊥ : Container I O c r → Container I O (c ⊔ r) c |
| 66 |
(C ^⊥) .Command o = (c : C .Command o) → C .Response c |
| 67 |
(C ^⊥) .Response {o} _ = C .Command o |
| 68 |
(C ^⊥) .next f c = C .next c (f c) |
| 69 |
|
| 70 |
-- Strength. |
| 71 |
|
| 72 |
infixl 3 _⋊_ |
| 73 |
|
| 74 |
_⋊_ : Container I O c r → (Z : Set z) → Container (I ⟨×⟩ Z) (O ⟨×⟩ Z) c r |
| 75 |
(C ⋊ Z) .Command (o , z) = C .Command o |
| 76 |
(C ⋊ Z) .Response = C .Response |
| 77 |
(C ⋊ Z) .next {o , z} c r = C .next c r , z |
| 78 |
|
| 79 |
infixr 3 _⋉_ |
| 80 |
|
| 81 |
_⋉_ : (Z : Set z) → Container I O c r → Container (Z ⟨×⟩ I) (Z ⟨×⟩ O) c r |
| 82 |
(Z ⋉ C) .Command (z , o) = C .Command o |
| 83 |
(Z ⋉ C) .Response = C .Response |
| 84 |
(Z ⋉ C) .next {z , o} c r = z , C .next c r |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
-- Product. (Note that, up to isomorphism, and ignoring universe level |
| 89 |
-- issues, this is a special case of indexed product.) |
| 90 |
|
| 91 |
infixr 2 _×_ |
| 92 |
|
| 93 |
_×_ : Container I O c₁ r₁ → Container I O c₂ r₂ → Container I O _ _ |
| 94 |
(C₁ ◃ R₁ / n₁) × (C₂ ◃ R₂ / n₂) = record |
| 95 |
{ Command = C₁ ∩ C₂ |
| 96 |
; Response = R₁ ⟪⊙⟫ R₂ |
| 97 |
; next = λ { (c₁ , c₂) → [ n₁ c₁ , n₂ c₂ ] } |
| 98 |
} |
| 99 |
|
| 100 |
-- Indexed product. |
| 101 |
|
| 102 |
Π : (X → Container I O c r) → Container I O _ _ |
| 103 |
Π {X = X} C = record |
| 104 |
{ Command = ⋂ X (Command ⟨∘⟩ C) |
| 105 |
; Response = ⋃[ x ∶ X ] λ c → Response (C x) (c x) |
| 106 |
; next = λ { c (x , r) → next (C x) (c x) r } |
| 107 |
} |
| 108 |
|
| 109 |
-- Sum. (Note that, up to isomorphism, and ignoring universe level |
| 110 |
-- issues, this is a special case of indexed sum.) |
| 111 |
|
| 112 |
infixr 1 _⊎_ _⊎′_ |
| 113 |
|
| 114 |
_⊎_ : Container I O c₁ r₁ → Container I O c₂ r₂ → Container I O _ _ |
| 115 |
(C₁ ⊎ C₂) .Command = C₁ .Command ∪ C₂ .Command |
| 116 |
(C₁ ⊎ C₂) .Response = All (C₁ .Response) (C₂ .Response) |
| 117 |
(C₁ ⊎ C₂) .next = All.[ C₁ .next , C₂ .next ] |
| 118 |
|
| 119 |
-- A simplified version for responses at the same level r: |
| 120 |
|
| 121 |
_⊎′_ : Container I O c₁ r → Container I O c₂ r → Container I O _ r |
| 122 |
(C₁ ◃ R₁ / n₁) ⊎′ (C₂ ◃ R₂ / n₂) = record |
| 123 |
{ Command = C₁ ∪ C₂ |
| 124 |
; Response = [ R₁ , R₂ ] |
| 125 |
; next = [ n₁ , n₂ ] |
| 126 |
} |
| 127 |
|
| 128 |
-- Indexed sum. |
| 129 |
|
| 130 |
Σ : (X → Container I O c r) → Container I O _ r |
| 131 |
Σ {X = X} C = record |
| 132 |
{ Command = ⋃ X (Command ⟨∘⟩ C) |
| 133 |
; Response = λ { (x , c) → Response (C x) c } |
| 134 |
; next = λ { (x , c) r → next (C x) c r } |
| 135 |
} |
| 136 |
|
| 137 |
-- Constant exponentiation. (Note that this is a special case of |
| 138 |
-- indexed product.) |
| 139 |
|
| 140 |
infix 0 const[_]⟶_ |
| 141 |
|
| 142 |
const[_]⟶_ : (X : Set ℓ) → Container I O c r → Container I O _ _ |
| 143 |
const[ X ]⟶ C = Π {X = X} (F.const C) |
| 144 |
|
| 145 |
------------------------------------------------------------------------ |
| 146 |
-- Correctness proofs |
| 147 |
|
| 148 |
module Identity where |
| 149 |
|
| 150 |
correct : {X : Pred O ℓ} → ⟦ id {c = c}{r} ⟧ X ↔ᵢ F.id X |
| 151 |
correct {X = X} = mk↔ₛ′ to from (λ _ → refl) (λ _ → refl) |
| 152 |
where |
| 153 |
to : ∀ {x} → ⟦ id ⟧ X x → F.id X x |
| 154 |
to xs = proj₂ xs _ |
| 155 |
|
| 156 |
from : ∀ {x} → F.id X x → ⟦ id ⟧ X x |
| 157 |
from x = (_ , λ _ → x) |
| 158 |
|
| 159 |
module Constant (ext : ∀ {ℓ} → Extensionality ℓ ℓ) where |
| 160 |
|
| 161 |
correct : (X : Pred O ℓ₁) {Y : Pred O ℓ₂} → ⟦ const X ⟧ Y ↔ᵢ F.const X Y |
| 162 |
correct X {Y} = mk↔ₛ′ to from (λ _ → refl) to∘from |
| 163 |
where |
| 164 |
to : ⟦ const X ⟧ Y ⊆ X |
| 165 |
to = proj₁ |
| 166 |
|
| 167 |
from : X ⊆ ⟦ const X ⟧ Y |
| 168 |
from = < F.id , F.const ⊥-elim > |
| 169 |
|
| 170 |
to∘from : _ |
| 171 |
to∘from xs = cong (proj₁ xs ,_) (ext ⊥-elim) |
| 172 |
|
| 173 |
module Duality where |
| 174 |
|
| 175 |
correct : (C : Container I O c r) (X : Pred I ℓ) → |
| 176 |
⟦ C ^⊥ ⟧ X ↔ᵢ (λ o → (c : Command C o) → ∃ λ r → X (next C c r)) |
| 177 |
correct C X = mk↔ₛ′ (λ { (f , g) → < f , g > }) (λ f → proj₁ ⟨∘⟩ f , proj₂ ⟨∘⟩ f) |
| 178 |
(λ _ → refl) (λ _ → refl) |
| 179 |
|
| 180 |
module Composition where |
| 181 |
|
| 182 |
correct : (C₁ : Container J K c r) (C₂ : Container I J c r) → |
| 183 |
{X : Pred I ℓ} → ⟦ C₁ ∘ C₂ ⟧ X ↔ᵢ (⟦ C₁ ⟧ ⟨∘⟩ ⟦ C₂ ⟧) X |
| 184 |
correct C₁ C₂ {X} = mk↔ₛ′ to from (λ _ → refl) (λ _ → refl) |
| 185 |
where |
| 186 |
to : ⟦ C₁ ∘ C₂ ⟧ X ⊆ ⟦ C₁ ⟧ (⟦ C₂ ⟧ X) |
| 187 |
to ((c , f) , g) = (c , < f , curry g >) |
| 188 |
|
| 189 |
from : ⟦ C₁ ⟧ (⟦ C₂ ⟧ X) ⊆ ⟦ C₁ ∘ C₂ ⟧ X |
| 190 |
from (c , f) = ((c , proj₁ ⟨∘⟩ f) , uncurry (proj₂ ⟨∘⟩ f)) |
| 191 |
|
| 192 |
module Product (ext : ∀ {ℓ} → Extensionality ℓ ℓ) where |
| 193 |
|
| 194 |
correct : (C₁ C₂ : Container I O c r) {X : Pred I _} → |
| 195 |
⟦ C₁ × C₂ ⟧ X ↔ᵢ (⟦ C₁ ⟧ X ∩ ⟦ C₂ ⟧ X) |
| 196 |
correct C₁ C₂ {X} = mk↔ₛ′ to from (λ _ → refl) from∘to |
| 197 |
where |
| 198 |
to : ⟦ C₁ × C₂ ⟧ X ⊆ ⟦ C₁ ⟧ X ∩ ⟦ C₂ ⟧ X |
| 199 |
to ((c₁ , c₂) , k) = ((c₁ , k ⟨∘⟩ inj₁) , (c₂ , k ⟨∘⟩ inj₂)) |
| 200 |
|
| 201 |
from : ⟦ C₁ ⟧ X ∩ ⟦ C₂ ⟧ X ⊆ ⟦ C₁ × C₂ ⟧ X |
| 202 |
from ((c₁ , k₁) , (c₂ , k₂)) = ((c₁ , c₂) , [ k₁ , k₂ ]) |
| 203 |
|
| 204 |
from∘to : from ⟨∘⟩ to ≗ F.id |
| 205 |
from∘to (c , _) = |
| 206 |
cong (c ,_) (ext [ (λ _ → refl) , (λ _ → refl) ]) |
| 207 |
|
| 208 |
module IndexedProduct where |
| 209 |
|
| 210 |
correct : (C : X → Container I O c r) {Y : Pred I ℓ} → |
| 211 |
⟦ Π C ⟧ Y ↔ᵢ ⋂[ x ∶ X ] ⟦ C x ⟧ Y |
| 212 |
correct {X = X} C {Y} = mk↔ₛ′ to from (λ _ → refl) (λ _ → refl) |
| 213 |
where |
| 214 |
to : ⟦ Π C ⟧ Y ⊆ ⋂[ x ∶ X ] ⟦ C x ⟧ Y |
| 215 |
to (c , k) = λ x → (c x , λ r → k (x , r)) |
| 216 |
|
| 217 |
from : ⋂[ x ∶ X ] ⟦ C x ⟧ Y ⊆ ⟦ Π C ⟧ Y |
| 218 |
from f = (proj₁ ⟨∘⟩ f , uncurry (proj₂ ⟨∘⟩ f)) |
| 219 |
|
| 220 |
module Sum (ext : ∀ {ℓ₁ ℓ₂} → Extensionality ℓ₁ ℓ₂) where |
| 221 |
|
| 222 |
correct : (C₁ C₂ : Container I O c r) {X : Pred I ℓ} → |
| 223 |
⟦ C₁ ⊎ C₂ ⟧ X ↔ᵢ (⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X) |
| 224 |
correct C₁ C₂ {X} = mk↔ₛ′ to from to∘from from∘to |
| 225 |
where |
| 226 |
to : ⟦ C₁ ⊎ C₂ ⟧ X ⊆ ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X |
| 227 |
to (inj₁ c₁ , k) = inj₁ (c₁ , λ r → k (All.inj₁ r)) |
| 228 |
to (inj₂ c₂ , k) = inj₂ (c₂ , λ r → k (All.inj₂ r)) |
| 229 |
|
| 230 |
from : ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X ⊆ ⟦ C₁ ⊎ C₂ ⟧ X |
| 231 |
from (inj₁ (c , f)) = inj₁ c , λ{ (All.inj₁ r) → f r} |
| 232 |
from (inj₂ (c , f)) = inj₂ c , λ{ (All.inj₂ r) → f r} |
| 233 |
|
| 234 |
from∘to : from ⟨∘⟩ to ≗ F.id |
| 235 |
from∘to (inj₁ _ , _) = cong (inj₁ _ ,_) (ext λ{ (All.inj₁ r) → refl}) |
| 236 |
from∘to (inj₂ _ , _) = cong (inj₂ _ ,_) (ext λ{ (All.inj₂ r) → refl}) |
| 237 |
|
| 238 |
to∘from : to ⟨∘⟩ from ≗ F.id |
| 239 |
to∘from = [ (λ _ → refl) , (λ _ → refl) ] |
| 240 |
|
| 241 |
module Sum′ where |
| 242 |
|
| 243 |
correct : (C₁ C₂ : Container I O c r) {X : Pred I ℓ} → |
| 244 |
⟦ C₁ ⊎′ C₂ ⟧ X ↔ᵢ (⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X) |
| 245 |
correct C₁ C₂ {X} = mk↔ₛ′ to from to∘from from∘to |
| 246 |
where |
| 247 |
to : ⟦ C₁ ⊎′ C₂ ⟧ X ⊆ ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X |
| 248 |
to (inj₁ c₁ , k) = inj₁ (c₁ , k) |
| 249 |
to (inj₂ c₂ , k) = inj₂ (c₂ , k) |
| 250 |
|
| 251 |
from : ⟦ C₁ ⟧ X ∪ ⟦ C₂ ⟧ X ⊆ ⟦ C₁ ⊎′ C₂ ⟧ X |
| 252 |
from = [ Prod.map inj₁ F.id , Prod.map inj₂ F.id ] |
| 253 |
|
| 254 |
from∘to : from ⟨∘⟩ to ≗ F.id |
| 255 |
from∘to (inj₁ _ , _) = refl |
| 256 |
from∘to (inj₂ _ , _) = refl |
| 257 |
|
| 258 |
to∘from : to ⟨∘⟩ from ≗ F.id |
| 259 |
to∘from = [ (λ _ → refl) , (λ _ → refl) ] |
| 260 |
|
| 261 |
module IndexedSum where |
| 262 |
|
| 263 |
correct : (C : X → Container I O c r) {Y : Pred I ℓ} → |
| 264 |
⟦ Σ C ⟧ Y ↔ᵢ ⋃[ x ∶ X ] ⟦ C x ⟧ Y |
| 265 |
correct {X = X} C {Y} = mk↔ₛ′ to from (λ _ → refl) (λ _ → refl) |
| 266 |
where |
| 267 |
to : ⟦ Σ C ⟧ Y ⊆ ⋃[ x ∶ X ] ⟦ C x ⟧ Y |
| 268 |
to ((x , c) , k) = (x , (c , k)) |
| 269 |
|
| 270 |
from : ⋃[ x ∶ X ] ⟦ C x ⟧ Y ⊆ ⟦ Σ C ⟧ Y |
| 271 |
from (x , (c , k)) = ((x , c) , k) |
| 272 |
|
| 273 |
module ConstantExponentiation where |
| 274 |
|
| 275 |
correct : (C : Container I O c r) {Y : Pred I ℓ} → |
| 276 |
⟦ const[ X ]⟶ C ⟧ Y ↔ᵢ (⋂ X (F.const (⟦ C ⟧ Y))) |
| 277 |
correct C {Y} = IndexedProduct.correct (F.const C) {Y} |