| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Some code related to indexed containers that uses heterogeneous |
| 5 |
-- equality |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
-- The notation and presentation here is perhaps close to those used |
| 9 |
-- by Hancock and Hyvernat in "Programming interfaces and basic |
| 10 |
-- topology" (2006). |
| 11 |
|
| 12 |
{-# OPTIONS --with-K --safe #-} |
| 13 |
|
| 14 |
module Data.Container.Indexed.WithK where |
| 15 |
|
| 16 |
open import Axiom.Extensionality.Heterogeneous using (Extensionality) |
| 17 |
open import Data.Container.Indexed hiding (module PlainMorphism) |
| 18 |
open import Data.Product.Base |
| 19 |
using (_,_; -,_; _×_; ∃; proj₁; proj₂; Σ-syntax) |
| 20 |
open import Function.Base renaming (id to ⟨id⟩; _∘_ to _⟨∘⟩_) |
| 21 |
open import Level using (Level; _⊔_) |
| 22 |
open import Relation.Unary using (Pred; _⊆_) |
| 23 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_; refl) |
| 24 |
open import Relation.Binary.HeterogeneousEquality as ≅ using (_≅_; refl) |
| 25 |
open import Relation.Binary.Indexed.Heterogeneous |
| 26 |
using (IndexedSetoid; Transitive; Symmetric; IREL; IRel) |
| 27 |
|
| 28 |
------------------------------------------------------------------------ |
| 29 |
|
| 30 |
-- Equality, parametrised on an underlying relation. |
| 31 |
|
| 32 |
Eq : ∀ {i o c r ℓ} {I : Set i} {O : Set o} (C : Container I O c r) |
| 33 |
(X Y : Pred I ℓ) → IREL X Y ℓ → IREL (⟦ C ⟧ X) (⟦ C ⟧ Y) _ |
| 34 |
Eq C _ _ _≈_ {o₁} {o₂} (c , k) (c′ , k′) = |
| 35 |
o₁ ≡ o₂ × c ≅ c′ × (∀ r r′ → r ≅ r′ → k r ≈ k′ r′) |
| 36 |
|
| 37 |
private |
| 38 |
|
| 39 |
-- Note that, if propositional equality were extensional, then Eq _≅_ |
| 40 |
-- and _≅_ would coincide. |
| 41 |
|
| 42 |
Eq⇒≅ : ∀ {i o c r ℓ} {I : Set i} {O : Set o} |
| 43 |
{C : Container I O c r} {X : Pred I ℓ} {o₁ o₂ : O} |
| 44 |
{xs : ⟦ C ⟧ X o₁} {ys : ⟦ C ⟧ X o₂} → Extensionality r ℓ → |
| 45 |
Eq C X X (λ x₁ x₂ → x₁ ≅ x₂) xs ys → xs ≅ ys |
| 46 |
Eq⇒≅ {xs = c , k} {.c , k′} ext (refl , refl , k≈k′) = |
| 47 |
≅.cong (_,_ c) (ext (λ _ → refl) (λ r → k≈k′ r r refl)) |
| 48 |
|
| 49 |
setoid : ∀ {i o c r s} {I : Set i} {O : Set o} → |
| 50 |
Container I O c r → IndexedSetoid I s _ → IndexedSetoid O _ _ |
| 51 |
setoid C X = record |
| 52 |
{ Carrier = ⟦ C ⟧ X.Carrier |
| 53 |
; _≈_ = _≈_ |
| 54 |
; isEquivalence = record |
| 55 |
{ refl = refl , refl , λ { r .r refl → X.refl } |
| 56 |
; sym = sym |
| 57 |
; trans = λ { {_} {i = xs} {ys} {zs} → trans {_} {i = xs} {ys} {zs} } |
| 58 |
} |
| 59 |
} |
| 60 |
where |
| 61 |
module X = IndexedSetoid X |
| 62 |
|
| 63 |
_≈_ : IRel (⟦ C ⟧ X.Carrier) _ |
| 64 |
_≈_ = Eq C X.Carrier X.Carrier X._≈_ |
| 65 |
|
| 66 |
sym : Symmetric (⟦ C ⟧ X.Carrier) _≈_ |
| 67 |
sym {_} {._} {_ , _} {._ , _} (refl , refl , k) = |
| 68 |
refl , refl , λ { r .r refl → X.sym (k r r refl) } |
| 69 |
|
| 70 |
trans : Transitive (⟦ C ⟧ X.Carrier) _≈_ |
| 71 |
trans {._} {_} {._} {_ , _} {._ , _} {._ , _} |
| 72 |
(refl , refl , k) (refl , refl , k′) = |
| 73 |
refl , refl , λ { r .r refl → X.trans (k r r refl) (k′ r r refl) } |
| 74 |
|
| 75 |
------------------------------------------------------------------------ |
| 76 |
-- Functoriality |
| 77 |
|
| 78 |
module Map where |
| 79 |
|
| 80 |
identity : ∀ {i o c r s} {I : Set i} {O : Set o} (C : Container I O c r) |
| 81 |
(X : IndexedSetoid I s _) → let module X = IndexedSetoid X in |
| 82 |
∀ {o} {xs : ⟦ C ⟧ X.Carrier o} → Eq C X.Carrier X.Carrier |
| 83 |
X._≈_ xs (map C {X.Carrier} ⟨id⟩ xs) |
| 84 |
identity C X = IndexedSetoid.refl (setoid C X) |
| 85 |
|
| 86 |
composition : ∀ {i o c r s ℓ₁ ℓ₂} {I : Set i} {O : Set o} |
| 87 |
(C : Container I O c r) {X : Pred I ℓ₁} {Y : Pred I ℓ₂} |
| 88 |
(Z : IndexedSetoid I s _) → let module Z = IndexedSetoid Z in |
| 89 |
{f : Y ⊆ Z.Carrier} {g : X ⊆ Y} {o : O} {xs : ⟦ C ⟧ X o} → |
| 90 |
Eq C Z.Carrier Z.Carrier Z._≈_ |
| 91 |
(map C {Y} f (map C {X} g xs)) |
| 92 |
(map C {X} (f ⟨∘⟩ g) xs) |
| 93 |
composition C Z = IndexedSetoid.refl (setoid C Z) |
| 94 |
|
| 95 |
------------------------------------------------------------------------ |
| 96 |
-- Plain morphisms |
| 97 |
|
| 98 |
module PlainMorphism {i o c r} {I : Set i} {O : Set o} where |
| 99 |
|
| 100 |
open Data.Container.Indexed.PlainMorphism |
| 101 |
|
| 102 |
-- Naturality. |
| 103 |
|
| 104 |
Natural : ∀ {ℓ} {C₁ C₂ : Container I O c r} → |
| 105 |
((X : Pred I ℓ) → ⟦ C₁ ⟧ X ⊆ ⟦ C₂ ⟧ X) → Set _ |
| 106 |
Natural {C₁ = C₁} {C₂} m = |
| 107 |
∀ {X} Y → let module Y = IndexedSetoid Y in (f : X ⊆ Y.Carrier) → |
| 108 |
∀ {o} (xs : ⟦ C₁ ⟧ X o) → |
| 109 |
Eq C₂ Y.Carrier Y.Carrier Y._≈_ |
| 110 |
(m Y.Carrier $ map C₁ {X} f xs) (map C₂ {X} f $ m X xs) |
| 111 |
|
| 112 |
-- Natural transformations. |
| 113 |
|
| 114 |
NT : ∀ {ℓ} (C₁ C₂ : Container I O c r) → Set _ |
| 115 |
NT {ℓ} C₁ C₂ = ∃ λ (m : (X : Pred I ℓ) → ⟦ C₁ ⟧ X ⊆ ⟦ C₂ ⟧ X) → |
| 116 |
Natural m |
| 117 |
|
| 118 |
-- Container morphisms are natural. |
| 119 |
|
| 120 |
natural : ∀ {ℓ} (C₁ C₂ : Container I O c r) (m : C₁ ⇒ C₂) → Natural {ℓ} ⟪ m ⟫ |
| 121 |
natural _ _ m {X} Y f _ = refl , refl , λ { r .r refl → lemma (coherent m) } |
| 122 |
where |
| 123 |
module Y = IndexedSetoid Y |
| 124 |
|
| 125 |
lemma : ∀ {i j} (eq : i ≡ j) {x} → |
| 126 |
≡.subst Y.Carrier eq (f x) Y.≈ f (≡.subst X eq x) |
| 127 |
lemma refl = Y.refl |
| 128 |
|
| 129 |
-- In fact, all natural functions of the right type are container |
| 130 |
-- morphisms. |
| 131 |
|
| 132 |
complete : ∀ {C₁ C₂ : Container I O c r} (nt : NT C₁ C₂) → |
| 133 |
∃ λ m → (X : IndexedSetoid I _ _) → |
| 134 |
let module X = IndexedSetoid X in |
| 135 |
∀ {o} (xs : ⟦ C₁ ⟧ X.Carrier o) → |
| 136 |
Eq C₂ X.Carrier X.Carrier X._≈_ |
| 137 |
(proj₁ nt X.Carrier xs) (⟪ m ⟫ X.Carrier {o} xs) |
| 138 |
complete {C₁} {C₂} (nt , nat) = m , (λ X xs → nat X |
| 139 |
(λ { (r , eq) → ≡.subst (IndexedSetoid.Carrier X) eq (proj₂ xs r) }) |
| 140 |
(proj₁ xs , (λ r → r , refl))) |
| 141 |
where |
| 142 |
|
| 143 |
m : C₁ ⇒ C₂ |
| 144 |
m = record |
| 145 |
{ command = λ c₁ → proj₁ (lemma c₁) |
| 146 |
; response = λ {_} {c₁} r₂ → proj₁ (proj₂ (lemma c₁) r₂) |
| 147 |
; coherent = λ {_} {c₁} {r₂} → proj₂ (proj₂ (lemma c₁) r₂) |
| 148 |
} |
| 149 |
where |
| 150 |
lemma : ∀ {o} (c₁ : Command C₁ o) → Σ[ c₂ ∈ Command C₂ o ] |
| 151 |
((r₂ : Response C₂ c₂) → Σ[ r₁ ∈ Response C₁ c₁ ] |
| 152 |
next C₁ c₁ r₁ ≡ next C₂ c₂ r₂) |
| 153 |
lemma c₁ = nt (λ i → Σ[ r₁ ∈ Response C₁ c₁ ] next C₁ c₁ r₁ ≡ i) |
| 154 |
(c₁ , λ r₁ → r₁ , refl) |
| 155 |
|
| 156 |
-- Composition commutes with ⟪_⟫. |
| 157 |
|
| 158 |
∘-correct : {C₁ C₂ C₃ : Container I O c r} |
| 159 |
(f : C₂ ⇒ C₃) (g : C₁ ⇒ C₂) (X : IndexedSetoid I (c ⊔ r) _) → |
| 160 |
let module X = IndexedSetoid X in |
| 161 |
∀ {o} {xs : ⟦ C₁ ⟧ X.Carrier o} → |
| 162 |
Eq C₃ X.Carrier X.Carrier X._≈_ |
| 163 |
(⟪ f ∘ g ⟫ X.Carrier xs) |
| 164 |
(⟪ f ⟫ X.Carrier (⟪ g ⟫ X.Carrier xs)) |
| 165 |
∘-correct f g X = refl , refl , λ { r .r refl → lemma (coherent g) |
| 166 |
(coherent f) } |
| 167 |
where |
| 168 |
module X = IndexedSetoid X |
| 169 |
|
| 170 |
lemma : ∀ {i j k} (eq₁ : i ≡ j) (eq₂ : j ≡ k) {x} → |
| 171 |
≡.subst X.Carrier (≡.trans eq₁ eq₂) x |
| 172 |
X.≈ |
| 173 |
≡.subst X.Carrier eq₂ (≡.subst X.Carrier eq₁ x) |
| 174 |
lemma refl refl = X.refl |
| 175 |
|
| 176 |
------------------------------------------------------------------------ |
| 177 |
-- All and any |
| 178 |
|
| 179 |
-- Membership. |
| 180 |
|
| 181 |
infix 4 _∈_ |
| 182 |
|
| 183 |
_∈_ : ∀ {i o c r ℓ} {I : Set i} {O : Set o} |
| 184 |
{C : Container I O c r} {X : Pred I (i ⊔ ℓ)} → IREL X (⟦ C ⟧ X) _ |
| 185 |
_∈_ {C = C} {X} x xs = ◇ C {X = X} ((x ≅_) ⟨∘⟩ proj₂) (-, xs) |