| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Indexed containers aka interaction structures aka polynomial |
| 5 |
-- functors. The notation and presentation here is closest to that of |
| 6 |
-- Hancock and Hyvernat in "Programming interfaces and basic topology" |
| 7 |
-- (2006/9). |
| 8 |
------------------------------------------------------------------------ |
| 9 |
|
| 10 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 11 |
|
| 12 |
module Data.Container.Indexed where |
| 13 |
|
| 14 |
open import Level using (Level; zero; _⊔_) |
| 15 |
open import Data.Product.Base as Prod hiding (map) |
| 16 |
open import Data.W.Indexed using (W) |
| 17 |
open import Function.Base renaming (id to ⟨id⟩; _∘_ to _⟨∘⟩_) |
| 18 |
open import Function.Bundles using (_↔_; Inverse) |
| 19 |
open import Relation.Unary using (Pred; _⊆_) |
| 20 |
open import Relation.Binary.Core using (Rel; REL) |
| 21 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; _≗_; refl; trans; subst) |
| 22 |
|
| 23 |
------------------------------------------------------------------------ |
| 24 |
|
| 25 |
-- The type and its semantics ("extension"). |
| 26 |
|
| 27 |
open import Data.Container.Indexed.Core public |
| 28 |
open Container public |
| 29 |
|
| 30 |
-- Abbreviation for the commonly used level one version of indexed |
| 31 |
-- containers. |
| 32 |
|
| 33 |
infix 5 _▷_ |
| 34 |
|
| 35 |
_▷_ : Set → Set → Set₁ |
| 36 |
I ▷ O = Container I O zero zero |
| 37 |
|
| 38 |
-- The least and greatest fixpoint. |
| 39 |
|
| 40 |
μ : ∀ {o c r} {O : Set o} → Container O O c r → Pred O _ |
| 41 |
μ = W |
| 42 |
|
| 43 |
-- An equivalence relation is defined in Data.Container.Indexed.WithK. |
| 44 |
|
| 45 |
------------------------------------------------------------------------ |
| 46 |
-- Functoriality |
| 47 |
|
| 48 |
-- Indexed containers are functors. |
| 49 |
|
| 50 |
map : ∀ {i o c r ℓ₁ ℓ₂} {I : Set i} {O : Set o} |
| 51 |
(C : Container I O c r) {X : Pred I ℓ₁} {Y : Pred I ℓ₂} → |
| 52 |
X ⊆ Y → ⟦ C ⟧ X ⊆ ⟦ C ⟧ Y |
| 53 |
map _ f = Prod.map ⟨id⟩ (λ g → f ⟨∘⟩ g) |
| 54 |
|
| 55 |
-- Some properties are proved in Data.Container.Indexed.WithK. |
| 56 |
|
| 57 |
------------------------------------------------------------------------ |
| 58 |
-- Container morphisms |
| 59 |
|
| 60 |
module _ {i₁ i₂ o₁ o₂} |
| 61 |
{I₁ : Set i₁} {I₂ : Set i₂} {O₁ : Set o₁} {O₂ : Set o₂} where |
| 62 |
|
| 63 |
-- General container morphism. |
| 64 |
|
| 65 |
record ContainerMorphism {c₁ c₂ r₁ r₂ ℓ₁ ℓ₂} |
| 66 |
(C₁ : Container I₁ O₁ c₁ r₁) (C₂ : Container I₂ O₂ c₂ r₂) |
| 67 |
(f : I₁ → I₂) (g : O₁ → O₂) |
| 68 |
(_∼_ : Rel I₂ ℓ₁) (_≈_ : REL (Set r₂) (Set r₁) ℓ₂) |
| 69 |
(_·_ : ∀ {A B} → A ≈ B → A → B) : |
| 70 |
Set (i₁ ⊔ i₂ ⊔ o₁ ⊔ o₂ ⊔ c₁ ⊔ c₂ ⊔ r₁ ⊔ r₂ ⊔ ℓ₁ ⊔ ℓ₂) where |
| 71 |
field |
| 72 |
command : Command C₁ ⊆ Command C₂ ⟨∘⟩ g |
| 73 |
response : ∀ {o} {c₁ : Command C₁ o} → |
| 74 |
Response C₂ (command c₁) ≈ Response C₁ c₁ |
| 75 |
coherent : ∀ {o} {c₁ : Command C₁ o} {r₂ : Response C₂ (command c₁)} → |
| 76 |
f (next C₁ c₁ (response · r₂)) ∼ next C₂ (command c₁) r₂ |
| 77 |
|
| 78 |
open ContainerMorphism public |
| 79 |
|
| 80 |
-- Plain container morphism. |
| 81 |
|
| 82 |
_⇒[_/_]_ : ∀ {c₁ c₂ r₁ r₂} → |
| 83 |
Container I₁ O₁ c₁ r₁ → (I₁ → I₂) → (O₁ → O₂) → |
| 84 |
Container I₂ O₂ c₂ r₂ → Set _ |
| 85 |
C₁ ⇒[ f / g ] C₂ = ContainerMorphism C₁ C₂ f g _≡_ (λ R₂ R₁ → R₂ → R₁) _$_ |
| 86 |
|
| 87 |
-- Linear container morphism. |
| 88 |
|
| 89 |
_⊸[_/_]_ : ∀ {c₁ c₂ r₁ r₂} → |
| 90 |
Container I₁ O₁ c₁ r₁ → (I₁ → I₂) → (O₁ → O₂) → |
| 91 |
Container I₂ O₂ c₂ r₂ → Set _ |
| 92 |
C₁ ⊸[ f / g ] C₂ = ContainerMorphism C₁ C₂ f g _≡_ _↔_ |
| 93 |
(λ r₂↔r₁ r₂ → Inverse.to r₂↔r₁ r₂) |
| 94 |
|
| 95 |
-- Cartesian container morphism. |
| 96 |
|
| 97 |
_⇒C[_/_]_ : ∀ {c₁ c₂ r} → |
| 98 |
Container I₁ O₁ c₁ r → (I₁ → I₂) → (O₁ → O₂) → |
| 99 |
Container I₂ O₂ c₂ r → Set _ |
| 100 |
C₁ ⇒C[ f / g ] C₂ = ContainerMorphism C₁ C₂ f g _≡_ (λ R₂ R₁ → R₂ ≡ R₁) |
| 101 |
(λ r₂≡r₁ r₂ → subst ⟨id⟩ r₂≡r₁ r₂) |
| 102 |
|
| 103 |
-- Degenerate cases where no reindexing is performed. |
| 104 |
|
| 105 |
module _ {i o c r} {I : Set i} {O : Set o} where |
| 106 |
|
| 107 |
infixr 8 _⇒_ _⊸_ _⇒C_ |
| 108 |
|
| 109 |
_⇒_ : Rel (Container I O c r) _ |
| 110 |
C₁ ⇒ C₂ = C₁ ⇒[ ⟨id⟩ / ⟨id⟩ ] C₂ |
| 111 |
|
| 112 |
_⊸_ : Rel (Container I O c r) _ |
| 113 |
C₁ ⊸ C₂ = C₁ ⊸[ ⟨id⟩ / ⟨id⟩ ] C₂ |
| 114 |
|
| 115 |
_⇒C_ : Rel (Container I O c r) _ |
| 116 |
C₁ ⇒C C₂ = C₁ ⇒C[ ⟨id⟩ / ⟨id⟩ ] C₂ |
| 117 |
|
| 118 |
------------------------------------------------------------------------ |
| 119 |
-- Plain morphisms |
| 120 |
|
| 121 |
-- Interpretation of _⇒_. |
| 122 |
|
| 123 |
⟪_⟫ : ∀ {i o c r ℓ} {I : Set i} {O : Set o} {C₁ C₂ : Container I O c r} → |
| 124 |
C₁ ⇒ C₂ → (X : Pred I ℓ) → ⟦ C₁ ⟧ X ⊆ ⟦ C₂ ⟧ X |
| 125 |
⟪ m ⟫ X (c , k) = command m c , λ r₂ → |
| 126 |
subst X (coherent m) (k (response m r₂)) |
| 127 |
|
| 128 |
module PlainMorphism {i o c r} {I : Set i} {O : Set o} where |
| 129 |
|
| 130 |
-- Identity. |
| 131 |
|
| 132 |
id : (C : Container I O c r) → C ⇒ C |
| 133 |
id _ = record |
| 134 |
{ command = ⟨id⟩ |
| 135 |
; response = ⟨id⟩ |
| 136 |
; coherent = refl |
| 137 |
} |
| 138 |
|
| 139 |
-- Composition. |
| 140 |
|
| 141 |
infixr 9 _∘_ |
| 142 |
|
| 143 |
_∘_ : {C₁ C₂ C₃ : Container I O c r} → |
| 144 |
C₂ ⇒ C₃ → C₁ ⇒ C₂ → C₁ ⇒ C₃ |
| 145 |
f ∘ g = record |
| 146 |
{ command = command f ⟨∘⟩ command g |
| 147 |
; response = response g ⟨∘⟩ response f |
| 148 |
; coherent = coherent g ⟨ trans ⟩ coherent f |
| 149 |
} |
| 150 |
|
| 151 |
-- Identity commutes with ⟪_⟫. |
| 152 |
|
| 153 |
id-correct : ∀ {ℓ} {C : Container I O c r} → ∀ {X : Pred I ℓ} {o} → |
| 154 |
⟪ id C ⟫ X {o} ≗ ⟨id⟩ |
| 155 |
id-correct _ = refl |
| 156 |
|
| 157 |
-- More properties are proved in Data.Container.Indexed.WithK. |
| 158 |
|
| 159 |
------------------------------------------------------------------------ |
| 160 |
-- Linear container morphisms |
| 161 |
|
| 162 |
module LinearMorphism |
| 163 |
{i o c r} {I : Set i} {O : Set o} {C₁ C₂ : Container I O c r} |
| 164 |
(m : C₁ ⊸ C₂) |
| 165 |
where |
| 166 |
|
| 167 |
morphism : C₁ ⇒ C₂ |
| 168 |
morphism = record |
| 169 |
{ command = command m |
| 170 |
; response = Inverse.to (response m) |
| 171 |
; coherent = coherent m |
| 172 |
} |
| 173 |
|
| 174 |
⟪_⟫⊸ : ∀ {ℓ} (X : Pred I ℓ) → ⟦ C₁ ⟧ X ⊆ ⟦ C₂ ⟧ X |
| 175 |
⟪_⟫⊸ = ⟪ morphism ⟫ |
| 176 |
|
| 177 |
open LinearMorphism public using (⟪_⟫⊸) |
| 178 |
|
| 179 |
------------------------------------------------------------------------ |
| 180 |
-- Cartesian morphisms |
| 181 |
|
| 182 |
module CartesianMorphism |
| 183 |
{i o c r} {I : Set i} {O : Set o} {C₁ C₂ : Container I O c r} |
| 184 |
(m : C₁ ⇒C C₂) |
| 185 |
where |
| 186 |
|
| 187 |
morphism : C₁ ⇒ C₂ |
| 188 |
morphism = record |
| 189 |
{ command = command m |
| 190 |
; response = subst ⟨id⟩ (response m) |
| 191 |
; coherent = coherent m |
| 192 |
} |
| 193 |
|
| 194 |
⟪_⟫C : ∀ {ℓ} (X : Pred I ℓ) → ⟦ C₁ ⟧ X ⊆ ⟦ C₂ ⟧ X |
| 195 |
⟪_⟫C = ⟪ morphism ⟫ |
| 196 |
|
| 197 |
open CartesianMorphism public using (⟪_⟫C) |
| 198 |
|
| 199 |
------------------------------------------------------------------------ |
| 200 |
-- All and any |
| 201 |
|
| 202 |
-- □ and ◇ are defined in the core module. |
| 203 |
|
| 204 |
module _ {i o c r ℓ₁ ℓ₂} {I : Set i} {O : Set o} (C : Container I O c r) |
| 205 |
{X : Pred I ℓ₁} {P Q : Pred (Σ I X) ℓ₂} where |
| 206 |
|
| 207 |
-- All. |
| 208 |
|
| 209 |
□-map : P ⊆ Q → □ C P ⊆ □ C Q |
| 210 |
□-map P⊆Q = _⟨∘⟩_ P⊆Q |
| 211 |
|
| 212 |
-- Any. |
| 213 |
|
| 214 |
◇-map : P ⊆ Q → ◇ C P ⊆ ◇ C Q |
| 215 |
◇-map P⊆Q = Prod.map ⟨id⟩ P⊆Q |
| 216 |
|
| 217 |
-- Membership is defined in Data.Container.Indexed.WithK. |