module Data.Container where
open import Data.M
open import Data.Product as Prod hiding (map)
open import Data.W
open import Function renaming (id to ⟨id⟩; _∘_ to _⟨∘⟩_)
open import Function.Equality using (_⟨$⟩_)
open import Function.Inverse using (_↔_; module Inverse)
import Function.Related as Related
open import Level
open import Relation.Binary
using (Setoid; module Setoid; Preorder; module Preorder)
open import Relation.Binary.PropositionalEquality as P
using (_≡_; _≗_; refl)
open import Relation.Unary using (_⊆_)
infix 5 _▷_
record Container (ℓ : Level) : Set (suc ℓ) where
constructor _▷_
field
Shape : Set ℓ
Position : Shape → Set ℓ
open Container public
⟦_⟧ : ∀ {ℓ₁ ℓ₂} → Container ℓ₁ → Set ℓ₂ → Set (ℓ₁ ⊔ ℓ₂)
⟦ C ⟧ X = Σ[ s ∈ Shape C ] (Position C s → X)
μ : ∀ {ℓ} → Container ℓ → Set ℓ
μ C = W (Shape C) (Position C)
ν : ∀ {ℓ} → Container ℓ → Set ℓ
ν C = M (Shape C) (Position C)
Eq : ∀ {c ℓ} {C : Container c} {X Y : Set c} →
(X → Y → Set ℓ) → ⟦ C ⟧ X → ⟦ C ⟧ Y → Set (c ⊔ ℓ)
Eq {C = C} _≈_ (s , f) (s′ , f′) =
Σ[ eq ∈ s ≡ s′ ] (∀ p → f p ≈ f′ (P.subst (Position C) eq p))
private
Eq⇒≡ : ∀ {c} {C : Container c} {X : Set c} {xs ys : ⟦ C ⟧ X} →
P.Extensionality c c → Eq _≡_ xs ys → xs ≡ ys
Eq⇒≡ {xs = s , f} {ys = .s , f′} ext (refl , f≈f′) =
P.cong (_,_ s) (ext f≈f′)
setoid : ∀ {ℓ} → Container ℓ → Setoid ℓ ℓ → Setoid ℓ ℓ
setoid C X = record
{ Carrier = ⟦ C ⟧ X.Carrier
; _≈_ = _≈_
; isEquivalence = record
{ refl = (refl , λ _ → X.refl)
; sym = sym
; trans = λ {_ _ zs} → trans zs
}
}
where
module X = Setoid X
_≈_ = Eq X._≈_
sym : {xs ys : ⟦ C ⟧ X.Carrier} → xs ≈ ys → ys ≈ xs
sym {_ , _} {._ , _} (refl , f) = (refl , X.sym ⟨∘⟩ f)
trans : ∀ {xs ys : ⟦ C ⟧ X.Carrier} zs → xs ≈ ys → ys ≈ zs → xs ≈ zs
trans {_ , _} {._ , _} (._ , _) (refl , f₁) (refl , f₂) =
(refl , λ p → X.trans (f₁ p) (f₂ p))
map : ∀ {c ℓ} {C : Container c} {X Y : Set ℓ} → (X → Y) → ⟦ C ⟧ X → ⟦ C ⟧ Y
map f = Prod.map ⟨id⟩ (λ g → f ⟨∘⟩ g)
module Map where
identity : ∀ {c} {C : Container c} X →
let module X = Setoid X in
(xs : ⟦ C ⟧ X.Carrier) → Eq X._≈_ (map ⟨id⟩ xs) xs
identity {C = C} X xs = Setoid.refl (setoid C X)
composition : ∀ {c} {C : Container c} {X Y : Set c} Z →
let module Z = Setoid Z in
(f : Y → Z.Carrier) (g : X → Y) (xs : ⟦ C ⟧ X) →
Eq Z._≈_ (map f (map g xs)) (map (f ⟨∘⟩ g) xs)
composition {C = C} Z f g xs = Setoid.refl (setoid C Z)
record _⇒_ {c} (C₁ C₂ : Container c) : Set c where
field
shape : Shape C₁ → Shape C₂
position : ∀ {s} → Position C₂ (shape s) → Position C₁ s
open _⇒_ public
⟪_⟫ : ∀ {c ℓ} {C₁ C₂ : Container c} →
C₁ ⇒ C₂ → {X : Set ℓ} → ⟦ C₁ ⟧ X → ⟦ C₂ ⟧ X
⟪ m ⟫ xs = (shape m (proj₁ xs) , proj₂ xs ⟨∘⟩ position m)
module Morphism where
Natural : ∀ {c} {C₁ C₂ : Container c} →
(∀ {X} → ⟦ C₁ ⟧ X → ⟦ C₂ ⟧ X) → Set (suc c)
Natural {c} {C₁} m =
∀ {X} (Y : Setoid c c) → let module Y = Setoid Y in
(f : X → Y.Carrier) (xs : ⟦ C₁ ⟧ X) →
Eq Y._≈_ (m $ map f xs) (map f $ m xs)
NT : ∀ {c} (C₁ C₂ : Container c) → Set (suc c)
NT C₁ C₂ = ∃ λ (m : ∀ {X} → ⟦ C₁ ⟧ X → ⟦ C₂ ⟧ X) → Natural m
natural : ∀ {c} {C₁ C₂ : Container c}
(m : C₁ ⇒ C₂) → Natural ⟪ m ⟫
natural {C₂ = C₂} m Y f xs = Setoid.refl (setoid C₂ Y)
complete : ∀ {c} {C₁ C₂ : Container c} →
(nt : NT C₁ C₂) →
∃ λ m → (X : Setoid c c) →
let module X = Setoid X in
(xs : ⟦ C₁ ⟧ X.Carrier) →
Eq X._≈_ (proj₁ nt xs) (⟪ m ⟫ xs)
complete (nt , nat) =
(m , λ X xs → nat X (proj₂ xs) (proj₁ xs , ⟨id⟩))
where
m = record { shape = λ s → proj₁ (nt (s , ⟨id⟩))
; position = λ {s} → proj₂ (nt (s , ⟨id⟩))
}
id : ∀ {c} (C : Container c) → C ⇒ C
id _ = record {shape = ⟨id⟩; position = ⟨id⟩}
infixr 9 _∘_
_∘_ : ∀ {c} {C₁ C₂ C₃ : Container c} → C₂ ⇒ C₃ → C₁ ⇒ C₂ → C₁ ⇒ C₃
f ∘ g = record
{ shape = shape f ⟨∘⟩ shape g
; position = position g ⟨∘⟩ position f
}
id-correct : ∀ {c} {C : Container c} {X : Set c} →
⟪ id C ⟫ {X} ≗ ⟨id⟩
id-correct xs = refl
∘-correct : ∀ {c} {C₁ C₂ C₃ : Container c}
(f : C₂ ⇒ C₃) (g : C₁ ⇒ C₂) {X : Set c} →
⟪ f ∘ g ⟫ {X} ≗ (⟪ f ⟫ ⟨∘⟩ ⟪ g ⟫)
∘-correct f g xs = refl
record _⊸_ {c} (C₁ C₂ : Container c) : Set c where
field
shape⊸ : Shape C₁ → Shape C₂
position⊸ : ∀ {s} → Position C₂ (shape⊸ s) ↔ Position C₁ s
morphism : C₁ ⇒ C₂
morphism = record
{ shape = shape⊸
; position = _⟨$⟩_ (Inverse.to position⊸)
}
⟪_⟫⊸ : ∀ {ℓ} {X : Set ℓ} → ⟦ C₁ ⟧ X → ⟦ C₂ ⟧ X
⟪_⟫⊸ = ⟪ morphism ⟫
open _⊸_ public using (shape⊸; position⊸; ⟪_⟫⊸)
□ : ∀ {c} {C : Container c} {X : Set c} →
(X → Set c) → (⟦ C ⟧ X → Set c)
□ P (s , f) = ∀ p → P (f p)
□-map : ∀ {c} {C : Container c} {X : Set c} {P Q : X → Set c} →
P ⊆ Q → □ {C = C} P ⊆ □ Q
□-map P⊆Q = _⟨∘⟩_ P⊆Q
◇ : ∀ {c} {C : Container c} {X : Set c} →
(X → Set c) → (⟦ C ⟧ X → Set c)
◇ P (s , f) = ∃ λ p → P (f p)
◇-map : ∀ {c} {C : Container c} {X : Set c} {P Q : X → Set c} →
P ⊆ Q → ◇ {C = C} P ⊆ ◇ Q
◇-map P⊆Q = Prod.map ⟨id⟩ P⊆Q
infix 4 _∈_
_∈_ : ∀ {c} {C : Container c} {X : Set c} →
X → ⟦ C ⟧ X → Set c
x ∈ xs = ◇ (_≡_ x) xs
open Related public
using (Kind; Symmetric-kind)
renaming ( implication to subset
; reverse-implication to superset
; equivalence to set
; injection to subbag
; reverse-injection to superbag
; bijection to bag
)
[_]-Order : ∀ {ℓ} → Kind → Container ℓ → Set ℓ → Preorder ℓ ℓ ℓ
[ k ]-Order C X = Related.InducedPreorder₂ k (_∈_ {C = C} {X = X})
[_]-Equality : ∀ {ℓ} → Symmetric-kind → Container ℓ → Set ℓ → Setoid ℓ ℓ
[ k ]-Equality C X = Related.InducedEquivalence₂ k (_∈_ {C = C} {X = X})
infix 4 _∼[_]_
_∼[_]_ : ∀ {c} {C : Container c} {X : Set c} →
⟦ C ⟧ X → Kind → ⟦ C ⟧ X → Set c
_∼[_]_ {C = C} {X} xs k ys = Preorder._∼_ ([ k ]-Order C X) xs ys