♻ major refactor of example

This commit is contained in:
Leon Vatthauer 2023-12-21 14:22:18 +01:00
parent 7265de98d0
commit 395f548334
Signed by: leonv
SSH key fingerprint: SHA256:G4+ddwoZmhLPRB1agvXzZMXIzkVJ36dUYZXf5NxT+u8

View file

@ -12,7 +12,7 @@ open import Categories.Category.Cocartesian
open import Categories.Object.Terminal open import Categories.Object.Terminal
open import Function.Equality as SΠ renaming (id to idₛ) open import Function.Equality as SΠ renaming (id to idₛ)
import Categories.Morphism.Reasoning as MR import Categories.Morphism.Reasoning as MR
open import Relation.Binary open import Relation.Binary using (Setoid; IsEquivalence)
open import Data.Sum using (_⊎_; inj₁; inj₂) open import Data.Sum using (_⊎_; inj₁; inj₂)
open import Data.Sum.Function.Setoid open import Data.Sum.Function.Setoid
open import Data.Sum.Relation.Binary.Pointwise open import Data.Sum.Relation.Binary.Pointwise
@ -24,7 +24,7 @@ import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_) open Eq using (_≡_)
open import Data.Product using (Σ; _,_; ∃; Σ-syntax; ∃-syntax) open import Data.Product using (Σ; _,_; ∃; Σ-syntax; ∃-syntax)
open import Codata.Musical.Notation open import Codata.Musical.Notation
import Category.Monad.Partiality -- import Category.Monad.Partiality
open import Category.Ambient.Setoids open import Category.Ambient.Setoids
``` ```
@ -37,211 +37,212 @@ module Monad.Instance.K.Instance.Delay {c } where
# Capretta's Delay Monad is an Instance of K in the Category of Setoids # Capretta's Delay Monad is an Instance of K in the Category of Setoids
```agda ```agda
open Setoid using () renaming (Carrier to _) open Setoid using () renaming (Carrier to _; _≈_ to [_][_≡_])
open module eq (S : Setoid c (c ⊔ )) = IsEquivalence (Setoid.isEquivalence S) using () renaming (refl to ≡-refl; sym to ≡-sym; trans to ≡-trans)
data _⊥ (A : Set c) : Set c where data _⊥ (A : Set c) : Set c where
now : A → A ⊥ now : A → A ⊥
later : ∞ (A ⊥) → A ⊥ later : ∞ (A ⊥) → A ⊥
module Equality {A : Setoid c (c ⊔ )} where module StrongBisimilarity (A : Setoid c (c ⊔ )) where
open Setoid A renaming (Carrier to C; _≈_ to __)
open IsEquivalence (Setoid.isEquivalence A) renaming (refl to -refl; sym to -sym; trans to -trans)
data _↓_ : C ⊥ → C → Set (c ⊔ ) where data __ : A ⊥ → A ⊥ → Set (c ⊔ ) where
now↓ : ∀ {x y} (xy : x y) → now x ↓ y now : ∀ {x y : A } → [ A ][ x ≡ y ] → now x now y
later : ∀ {x y} → ∞ (♭ x ♭ y) → later x later y
-refl : ∀ {x} → x x
-refl {now x} = now (≡-refl A)
-refl {later x} = later (♯ -refl)
-sym : ∀ {x y} → x y → y x
-sym {.(now _)} {.(now _)} (now x≡y) = now (≡-sym A x≡y)
-sym {.(later _)} {.(later _)} (later xy) = later (♯ -sym (♭ xy))
-trans : ∀ {x y z} → x y → y z → x z
-trans {.(now _)} {.(now _)} {.(now _)} (now x≡y) (now y≡z) = now (≡-trans A x≡y y≡z)
-trans {.(later _)} {.(later _)} {.(later _)} (later xy) (later yz) = later (♯ -trans (♭ xy) (♭ yz))
open StrongBisimilarity renaming (__ to [_][__])
module WeakBisimilarity (A : Setoid c (c ⊔ )) where
data _↓_ : A ⊥ → A → Set (c ⊔ ) where
now↓ : ∀ {x y} (x≡y : [ A ][ x ≡ y ]) → now x ↓ y
later↓ : ∀ {x y} (x↓y : ♭ x ↓ y) → later x ↓ y later↓ : ∀ {x y} (x↓y : ♭ x ↓ y) → later x ↓ y
unique↓ : ∀ {c : C ⊥} {x y : C} → c ↓ x → c ↓ y → x y unique↓ : ∀ {a : A ⊥} {x y : A } → a ↓ x → a ↓ y → [ A ][ x ≡ y ]
unique↓ (now↓ eq₁) (now↓ eq₂) = -trans (-sym eq₁) eq₂ unique↓ (now↓ a≡x) (now↓ a≡y) = ≡-trans A (≡-sym A a≡x) a≡y
unique↓ (later↓ p) (later↓ q) = unique↓ p q unique↓ (later↓ a↓x) (later↓ a↓y) = unique↓ a↓x a↓y
data _≈_ : C ⊥ → C ⊥ → Set (c ⊔ ) where data _≈_ : A ⊥ → A ⊥ → Set (c ⊔ ) where
↓≈ : ∀ {x y a b} (ab : a b) (x↓a : x ↓ a) (y↓b : y ↓ b) → x ≈ y ↓≈ : ∀ {x y a b} (a≡b : [ A ][ a ≡ b ]) (x↓a : x ↓ a) (y↓b : y ↓ b) → x ≈ y
later≈ : ∀ {x y} (x≈y : ∞ (♭ x ≈ ♭ y)) → later x ≈ later y later≈ : ∀ {x y} (x≈y : ∞ (♭ x ≈ ♭ y)) → later x ≈ later y
≈-refl : ∀ {x} → x ≈ x ≈-refl : ∀ {x} → x ≈ x
≈-refl {now x} = ↓≈ -refl (now↓ -refl) (now↓ -refl) ≈-refl {now x} = ↓≈ (≡-refl A) (now↓ (≡-refl A)) (now↓ (≡-refl A))
≈-refl {later x} = later≈ (♯ ≈-refl) ≈-refl {later x} = later≈ (♯ ≈-refl)
≈-sym : ∀ {x y} → x ≈ y → y ≈ x ≈-sym : ∀ {x y} → x ≈ y → y ≈ x
≈-sym (↓≈ ab x↓a y↓b) = ↓≈ (-sym ab) y↓b x↓a ≈-sym (↓≈ a≡b x↓a y↓b) = ↓≈ (≡-sym A a≡b) y↓b x↓a
≈-sym (later≈ x≈y) = later≈ (♯ ≈-sym (♭ x≈y)) ≈-sym (later≈ x≈y) = later≈ (♯ ≈-sym (♭ x≈y))
∼↓ : ∀ {x y : C} {z : C ⊥} → x y → z ↓ x → z ↓ y ≡↓ : ∀ {x y : A } {z : A ⊥} → [ A ][ x ≡ y ] → z ↓ x → z ↓ y
∼↓ {x} {y} {.(now _)} xy (now↓ ax) = now↓ (-trans ax xy) ≡↓ {x} {y} {.(now _)} x≡y (now↓ a≡x) = now↓ (≡-trans A a≡x x≡y)
∼↓ {x} {y} {.(later _)} xy (later↓ z↓x) = later↓ (∼↓ xy z↓x) ≡↓ {x} {y} {.(later _)} x≡y (later↓ z↓x) = later↓ (≡↓ x≡y z↓x)
≈↓ : ∀ {x y : C ⊥} {z : C} → x ≈ y → x ↓ z → y ↓ z ≈↓ : ∀ {x y : A ⊥} {z : A } → x ≈ y → x ↓ z → y ↓ z
≈↓ (↓≈ ab (now↓ xa) y↓b) (now↓ xz) = ∼↓ (-trans (-sym ab) (-trans (-sym xa) xz)) y↓b ≈↓ (↓≈ a≡b (now↓ x≡a) y↓b) (now↓ x≡z) = ≡↓ (≡-trans A (≡-sym A a≡b) (≡-trans A (≡-sym A x≡a) x≡z)) y↓b
≈↓ (↓≈ ab (later↓ x↓a) y↓b) (later↓ x↓z) with unique↓ x↓a x↓z ≈↓ (↓≈ ab (later↓ x↓a) y↓b) (later↓ x↓z) with unique↓ x↓a x↓z
... | az = ∼↓ (-trans (-sym ab) az) y↓b ... | az = ≡↓ (≡-trans A (≡-sym A a≡b) az) y↓b
≈↓ (later≈ x) (later↓ x↓z) = later↓ (≈↓ (♭ x) x↓z) ≈↓ (later≈ x) (later↓ x↓z) = later↓ (≈↓ (♭ x) x↓z)
≈-trans : ∀ {x y z} → x ≈ y → y ≈ z → x ≈ z ≈-trans : ∀ {x y z} → x ≈ y → y ≈ z → x ≈ z
≈-trans (↓≈ ab x↓a y↓b) (↓≈ cd y↓c z↓d) with unique↓ y↓b y↓c ≈-trans (↓≈ a≡b x↓a y↓b) (↓≈ c≡d y↓c z↓d) with unique↓ y↓b y↓c
... | bc = ↓≈ (-trans (-trans ab bc) cd) x↓a z↓d ... | b≡c = ↓≈ (≡-trans A (≡-trans A a≡b b≡c) c≡d) x↓a z↓d
≈-trans (↓≈ ab z↓a (later↓ x↓b)) (later≈ x≈y) = ↓≈ ab z↓a (later↓ (≈↓ (♭ x≈y) x↓b)) ≈-trans (↓≈ a≡b z↓a (later↓ x↓b)) (later≈ x≈y) = ↓≈ a≡b z↓a (later↓ (≈↓ (♭ x≈y) x↓b))
≈-trans (later≈ x≈y) (↓≈ ab (later↓ y↓a) z↓b) = ↓≈ ab (later↓ (≈↓ (≈-sym (♭ x≈y)) y↓a)) z↓b ≈-trans (later≈ x≈y) (↓≈ a≡b (later↓ y↓a) z↓b) = ↓≈ a≡b (later↓ (≈↓ (≈-sym (♭ x≈y)) y↓a)) z↓b
≈-trans (later≈ x≈y) (later≈ y≈z) = later≈ (♯ ≈-trans (♭ x≈y) (♭ y≈z)) ≈-trans (later≈ x≈y) (later≈ y≈z) = later≈ (♯ ≈-trans (♭ x≈y) (♭ y≈z))
-- _⊥ₛ open WeakBisimilarity renaming (_≈_ to [_][_≈_]; _↓_ to [_][_↓_])
∼⇒≈ : ∀ {A : Setoid c (c ⊔ )} {x y : A ⊥} → [ A ][ x y ] → [ A ][ x ≈ y ]
∼⇒≈ {A} {.(now _)} {.(now _)} (now a≡b) = ↓≈ a≡b (now↓ (≡-refl A)) (now↓ (≡-refl A))
∼⇒≈ {A} {.(later _)} {.(later _)} (later xy) = later≈ (♯ ∼⇒≈ (♭ xy))
<_> = Π._⟨$⟩_
_⊥ₛ : Setoid c (c ⊔ ) → Setoid c (c ⊔ ) _⊥ₛ : Setoid c (c ⊔ ) → Setoid c (c ⊔ )
_⊥ₛ A = record { Carrier = A ⊥ ; _≈_ = _≈_ ; isEquivalence = record { refl = ≈-refl ; sym = ≈-sym ; trans = ≈-trans } } _⊥ₛ A = record { Carrier = A ⊥ ; _≈_ = [_][_≈_] A ; isEquivalence = record { refl = ≈-refl A ; sym = ≈-sym A ; trans = ≈-trans A } }
where
open Equality {A}
open Equality using (↓≈; later≈; now↓; later↓; _↓_; ≈↓; ∼↓; ≈-refl; ≈-sym; ≈-trans)
_[_≈_] : ∀ (A : Setoid c (c ⊔ )) → (x y : A ⊥) → Set (c ⊔ ) now-cong : ∀ {A : Setoid c (c ⊔ )} {x y : A } → [ A ][ x ≡ y ] → [ A ][ now x ≈ now y ]
A [ x ≈ y ] = Equality._≈_ {A} x y now-cong {A} {x} {y} xy = ↓≈ xy (now↓ (≡-refl A)) (now↓ (≡-refl A))
_[__] : ∀ (A : Setoid c (c ⊔ )) → (x y : A ) → Set (c ⊔ ) now-inj : ∀ {A : Setoid c (c ⊔ )} {x y : A } → [ A ][ now x ≈ now y ] → [ A ][ x ≡ y ]
A [ x y ] = Setoid._≈_ A x y now-inj {A} {x} {y} (↓≈ a≡b (now↓ x≡a) (now↓ y≡b)) = ≡-trans A x≡a (≡-trans A a≡b (≡-sym A y≡b))
-refl : ∀ (A : Setoid c (c ⊔ )) {x : A } → A [ x x ] liftF : ∀ {A B : Set c} → (A → B) → A ⊥ → B ⊥
-refl A = IsEquivalence.refl (Setoid.isEquivalence A) liftF f (now x) = now (f x)
-sym : ∀ (A : Setoid c (c ⊔ )) {x y : A } → A [ x y ] → A [ y x ]
-sym A = IsEquivalence.sym (Setoid.isEquivalence A)
-trans : ∀ (A : Setoid c (c ⊔ )) {x y z : A } → A [ x y ] → A [ y z ] → A [ x z ]
-trans A = IsEquivalence.trans (Setoid.isEquivalence A)
now-cong : ∀ {A : Setoid c (c ⊔ )} {x y : A } → A [ x y ] → A [ now x ≈ now y ]
now-cong {A} {x} {y} xy = ↓≈ xy (now↓ (-refl A)) (now↓ (-refl A))
-- slightly different types than later≈
-- TODO remove this is useless
later-cong : ∀ {A : Setoid c (c ⊔ )} {x y : A ⊥} → A [ x ≈ y ] → A [ later (♯ x) ≈ later (♯ y) ]
later-cong {A} {x} {y} x≈y = later≈ (♯ x≈y)
now-inj : ∀ {A : Setoid c (c ⊔ )} {x y : A } → A [ now x ≈ now y ] → A [ x y ]
now-inj {A} {x} {y} (↓≈ ab (now↓ xa) (now↓ yb)) = -trans A xa (-trans A ab (-sym A yb))
liftF : ∀ {A B : Setoid c (c ⊔ )} → A ⟶ B → A ⊥ → B
liftF f (now x) = now (f ⟨$⟩ x)
liftF f (later x) = later (♯ liftF f (♭ x)) liftF f (later x) = later (♯ liftF f (♭ x))
delayFun : ∀ {A B : Setoid c (c ⊔ )} → A ⟶ B → A ⊥ₛ ⟶ B ⊥ₛ lift↓ : ∀ {A B : Setoid c (c ⊔ )} (f : A ⟶ B) {x : A ⊥} {b : A } → [ A ][ x ↓ b ] → [ B ][ liftF (f ._⟨$⟩_) x ↓ f ⟨$⟩ b ]
delayFun {A} {B} f = record { _⟨$⟩_ = liftF f ; cong = cong' } lift↓ {A} {B} f {now x} {b} (now↓ x≡b) = now↓ (cong f x≡b)
where lift↓ {A} {B} f {later x} {b} (later↓ x↓b) = later↓ (lift↓ f x↓b)
↓-cong : ∀ {x : A ⊥} {b : A } → _↓_ {A} x b → _↓_ {B} (liftF f x) (f ⟨$⟩ b)
↓-cong {now x} {b} (now↓ xy) = now↓ (cong f xy)
↓-cong {later x} {b} (later↓ x↓b) = later↓ (↓-cong x↓b)
cong' : ∀ {x y : A ⊥} → A [ x ≈ y ] → B [ liftF f x ≈ liftF f y ]
cong' {now x} {now y} (↓≈ ab (now↓ xa) (now↓ yb)) = now-cong (cong f (-trans A xa (-trans A ab (-sym A yb))))
cong' {now x} {later y} (↓≈ ab (now↓ xa) (later↓ y↓b)) = ↓≈ (cong f (-trans A xa ab)) (now↓ (-refl B)) (later↓ (↓-cong y↓b))
cong' {later x} {now y} (↓≈ ab (later↓ x↓a) (now↓ yb)) = ↓≈ (cong f (-trans A ab (-sym A yb))) (later↓ (↓-cong x↓a)) (now↓ (-refl B))
cong' {later x} {later y} (↓≈ ab (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ cong' (↓≈ ab x↓a y↓b))
cong' {later x} {later y} (later≈ x≈y) = later≈ (♯ cong' (♭ x≈y))
-- this needs polymorphic universe levels lift-cong : ∀ {A B : Setoid c (c ⊔ )} (f : A ⟶ B) {x y : A ⊥} → [ A ][ x ≈ y ] → [ B ][ liftF < f > x ≈ liftF < f > y ]
lift-cong {A} {B} f {now x} {now y} (↓≈ a≡b (now↓ x≡a) (now↓ y≡b)) = now-cong (cong f (≡-trans A x≡a (≡-trans A a≡b (≡-sym A y≡b))))
lift-cong {A} {B} f {now x} {later y} (↓≈ a≡b (now↓ x≡a) (later↓ y↓b)) = ↓≈ (cong f (≡-trans A x≡a a≡b)) (now↓ (≡-refl B)) (later↓ (lift↓ f y↓b))
lift-cong {A} {B} f {later x} {now y} (↓≈ a≡b (later↓ x↓a) (now↓ y≡b)) = ↓≈ (cong f (≡-trans A a≡b (≡-sym A y≡b))) (later↓ (lift↓ f x↓a)) (now↓ (≡-refl B))
lift-cong {A} {B} f {later x} {later y} (↓≈ a≡b (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ lift-cong f (↓≈ a≡b x↓a y↓b))
lift-cong {A} {B} f {later x} {later y} (later≈ x≈y) = later≈ (♯ lift-cong f (♭ x≈y))
liftFₛ : ∀ {A B : Setoid c (c ⊔ )} → A ⟶ B → A ⊥ₛ ⟶ B ⊥ₛ
liftFₛ {A} {B} f = record { _⟨$⟩_ = liftF < f > ; cong = lift-cong f }
-- -- this needs polymorphic universe levels
_≋_ : ∀ {c' '} {A B : Setoid c' '} → A ⟶ B → A ⟶ B → Set (c' ⊔ ') _≋_ : ∀ {c' '} {A B : Setoid c' '} → A ⟶ B → A ⟶ B → Set (c' ⊔ ')
_≋_ {c'} {'} {A} {B} f g = Setoid._≈_ (A ⇨ B) f g _≋_ {c'} {'} {A} {B} f g = Setoid._≈_ (A ⇨ B) f g
later-eq : ∀ {A : Setoid c (c ⊔ )} {x y : A ⊥} → A [ later (♯ x) ≈ y ] → A [ x ≈ y ] later-eq : ∀ {A : Setoid c (c ⊔ )} {x y : A ⊥} → [ A ][ later (♯ x) ≈ y ] → [ A ][ x ≈ y ]
later-eq {A} {x} {y} (↓≈ ab (later↓ x↓a) y↓b) = ↓≈ ab x↓a y↓b later-eq {A} {x} {y} (↓≈ a≡b (later↓ x↓a) y↓b) = ↓≈ a≡b x↓a y↓b
later-eq {A} {now x} {later y} (later≈ x≈y) = ↓≈ (-refl A) (now↓ (-refl A)) (later↓ (≈↓ (♭ x≈y) (now↓ (-refl A)))) later-eq {A} {now x} {later y} (later≈ x≈y) = ↓≈ (≡-refl A) (now↓ (≡-refl A)) (later↓ (≈↓ A (♭ x≈y) (now↓ (≡-refl A))))
later-eq {A} {later x} {later y} (later≈ x≈y) = later≈ (♯ later-eq (≈-trans (later≈ (♯ ≈-refl)) (♭ x≈y))) later-eq {A} {later x} {later y} (later≈ x≈y) = later≈ (♯ later-eq (≈-trans A (later≈ (♯ ≈-refl A)) (♭ x≈y)))
later-self : ∀ {A : Setoid c (c ⊔ )} {x : A ⊥} → A [ x ≈ later (♯ x) ] later-self : ∀ {A : Setoid c (c ⊔ )} {x : A ⊥} → [ A ][ x ≈ later (♯ x) ]
later-self {A} {now x} = ↓≈ (-refl A) (now↓ (-refl A)) (later↓ (now↓ (-refl A))) later-self {A} {now x} = ↓≈ (≡-refl A) (now↓ (≡-refl A)) (later↓ (now↓ (≡-refl A)))
later-self {A} {later x} = later-eq (later≈ (♯ ≈-refl)) later-self {A} {later x} = later-eq (later≈ (♯ ≈-refl A))
delayFun↓ : ∀ {A B : Setoid c (c ⊔ )} (f : A ⟶ B) {x : A ⊥} {y : A } → _↓_ {A} x y → _↓_ {B} (delayFun f ⟨$⟩ x) (f ⟨$⟩ y) lift-id : ∀ {A : Setoid c (c ⊔ )} → (liftFₛ idₛ) ≋ (idₛ {A = A ⊥ₛ})
delayFun↓ {A} {B} f {now x} {y} (now↓ xy) = now↓ (cong f xy) lift-id {A} {now x} {now y} (↓≈ a≡b (now↓ x≡a) (now↓ y≡b)) = ↓≈ a≡b (now↓ x≡a) (now↓ y≡b)
delayFun↓ {A} {B} f {later x} {y} (later↓ x↓y) = later↓ (delayFun↓ f x↓y) lift-id {A} {now x} {later y} x≈y = x≈y
lift-id {A} {later x} {now y} (↓≈ a≡b (later↓ x↓a) (now↓ y≡b)) = ↓≈ a≡b (later↓ (lift↓ idₛ x↓a)) (now↓ y≡b)
lift-id {A} {later x} {later y} (↓≈ a≡b (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ lift-id (↓≈ a≡b x↓a y↓b))
lift-id {A} {later x} {later y} (later≈ x≈y) = later≈ (♯ lift-id (♭ x≈y))
delayFun-id : ∀ {A : Setoid c (c ⊔ )} → (delayFun idₛ) ≋ (idₛ {A = A ⊥ₛ}) lift-comp : ∀ {A B C : Setoid c (c ⊔ )} {f : A ⟶ B} {g : B ⟶ C} → liftFₛ (g ∘ f) ≋ (liftFₛ g ∘ liftFₛ f)
delayFun-id {A} {now x} {now y} (↓≈ ab (now↓ xa) (now↓ yb)) = ↓≈ ab (now↓ xa) (now↓ yb) lift-comp {A} {B} {C} {f} {g} {now x} {now y} (↓≈ a≡b (now↓ x≡a) (now↓ y≡b)) = now-cong (cong g (cong f (≡-trans A x≡a (≡-trans A a≡b (≡-sym A y≡b)))))
delayFun-id {A} {now x} {later y} x≈y = x≈y lift-comp {A} {B} {C} {f} {g} {now x} {later y} (↓≈ a≡b (now↓ x≡a) (later↓ y↓b)) = ↓≈ (cong g (cong f (≡-trans A x≡a a≡b))) (now↓ (≡-refl C)) (later↓ (lift↓ g (lift↓ f y↓b)))
delayFun-id {A} {later x} {now y} (↓≈ ab (later↓ x↓a) (now↓ yb)) = ↓≈ ab (later↓ (delayFun↓ idₛ x↓a)) (now↓ yb) lift-comp {A} {B} {C} {f} {g} {later x} {now y} (↓≈ a≡b (later↓ x↓a) (now↓ y≡b)) = ↓≈ (cong g (cong f (≡-trans A a≡b (≡-sym A y≡b)))) (later↓ (lift↓ (g ∘ f) x↓a)) (now↓ (≡-refl C))
delayFun-id {A} {later x} {later y} (↓≈ ab (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ delayFun-id (↓≈ ab x↓a y↓b)) lift-comp {A} {B} {C} {f} {g} {later x} {later y} (↓≈ a≡b (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ lift-comp {A} {B} {C} {f} {g} (↓≈ a≡b x↓a y↓b))
delayFun-id {A} {later x} {later y} (later≈ x≈y) = later≈ (♯ delayFun-id (♭ x≈y)) lift-comp {A} {B} {C} {f} {g} {later x} {later y} (later≈ x≈y) = later≈ (♯ lift-comp {A} {B} {C} {f} {g} (♭ x≈y))
delayFun-comp : ∀ {A B C : Setoid c (c ⊔ )} {f : A ⟶ B} {g : B ⟶ C} → delayFun (g ∘ f) ≋ (delayFun g ∘ delayFun f) lift-resp-≈ : ∀ {A B : Setoid c (c ⊔ )} {f g : A ⟶ B} → f ≋ g → liftFₛ f ≋ liftFₛ g
delayFun-comp {A} {B} {C} {f} {g} {now x} {now y} (↓≈ ab (now↓ xa) (now↓ yb)) = now-cong (cong g (cong f (-trans A xa (-trans A ab (-sym A yb))))) lift-resp-≈ {A} {B} {f} {g} f≋g {now x} {now y} x≈y = now-cong (f≋g (now-inj x≈y))
delayFun-comp {A} {B} {C} {f} {g} {now x} {later y} (↓≈ ab (now↓ xa) (later↓ y↓b)) = ↓≈ (cong g (cong f (-trans A xa ab))) (now↓ (-refl C)) (later↓ (delayFun↓ g (delayFun↓ f y↓b))) lift-resp-≈ {A} {B} {f} {g} f≋g {now x} {later y} (↓≈ a≡b (now↓ x≡a) (later↓ y↓b)) = ↓≈ (f≋g (≡-trans A x≡a a≡b)) (now↓ (≡-refl B)) (later↓ (lift↓ g y↓b))
delayFun-comp {A} {B} {C} {f} {g} {later x} {now y} (↓≈ ab (later↓ x↓a) (now↓ yb)) = ↓≈ (cong g (cong f (-trans A ab (-sym A yb)))) (later↓ (delayFun↓ (g ∘ f) x↓a)) (now↓ (-refl C)) lift-resp-≈ {A} {B} {f} {g} f≋g {later x} {now y} (↓≈ a≡b (later↓ x↓a) (now↓ y≡b)) = ↓≈ (f≋g (≡-trans A a≡b (≡-sym A y≡b))) (later↓ (lift↓ f x↓a)) (now↓ (≡-refl B))
delayFun-comp {A} {B} {C} {f} {g} {later x} {later y} (↓≈ ab (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ delayFun-comp (↓≈ ab x↓a y↓b)) lift-resp-≈ {A} {B} {f} {g} f≋g {later x} {later y} (↓≈ a≡b (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ lift-resp-≈ {A} {B} {f} {g} f≋g (↓≈ a≡b x↓a y↓b))
delayFun-comp {A} {B} {C} {f} {g} {later x} {later y} (later≈ x≈y) = later≈ (♯ delayFun-comp (♭ x≈y)) lift-resp-≈ {A} {B} {f} {g} f≋g {later x} {later y} (later≈ x≈y) = later≈ (♯ lift-resp-≈ {A} {B} {f} {g} f≋g (♭ x≈y))
delayFun-resp-≈ : ∀ {A B : Setoid c (c ⊔ )} {f g : A ⟶ B} → f ≋ g → delayFun f ≋ delayFun g
delayFun-resp-≈ {A} {B} {f} {g} f≋g {now x} {now y} x≈y = now-cong (f≋g (now-inj x≈y))
delayFun-resp-≈ {A} {B} {f} {g} f≋g {now x} {later y} (↓≈ ab (now↓ xa) (later↓ y↓b)) = ↓≈ (f≋g (-trans A xa ab)) (now↓ (-refl B)) (later↓ (delayFun↓ g y↓b))
delayFun-resp-≈ {A} {B} {f} {g} f≋g {later x} {now y} (↓≈ ab (later↓ x↓a) (now↓ yb)) = ↓≈ (f≋g (-trans A ab (-sym A yb))) (later↓ (delayFun↓ f x↓a)) (now↓ (-refl B))
delayFun-resp-≈ {A} {B} {f} {g} f≋g {later x} {later y} (↓≈ ab (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ delayFun-resp-≈ f≋g (↓≈ ab x↓a y↓b))
delayFun-resp-≈ {A} {B} {f} {g} f≋g {later x} {later y} (later≈ x≈y) = later≈ (♯ delayFun-resp-≈ f≋g (♭ x≈y))
η : ∀ (A : Setoid c (c ⊔ )) → A ⟶ A ⊥ₛ η : ∀ (A : Setoid c (c ⊔ )) → A ⟶ A ⊥ₛ
η A = record { _⟨$⟩_ = now ; cong = id λ xy → now-cong xy } η A = record { _⟨$⟩_ = now ; cong = id λ x≡y → now-cong x≡y }
η↓ : ∀ {A : Setoid c (c ⊔ )} {x y : A } → A [ x y ] → _↓_ {A} ((η A) ⟨$⟩ x) y η↓ : ∀ {A : Setoid c (c ⊔ )} {x y : A } → [ A ][ x ≡ y ] → [ A ][ ((η A) ⟨$⟩ x) ↓ y ]
η↓ {A} {x} {y} xy = now↓ xy η↓ {A} {x} {y} x≡y = now↓ x≡y
η-natural : ∀ {A B : Setoid c (c ⊔ )} (f : A ⟶ B) → (η B ∘ f) ≋ (delayFun f ∘ η A) η-natural : ∀ {A B : Setoid c (c ⊔ )} (f : A ⟶ B) → (η B ∘ f) ≋ (liftFₛ f ∘ η A)
η-natural {A} {B} f {x} {y} x≈y = now-cong (cong f x≈y) η-natural {A} {B} f {x} {y} x≈y = now-cong (cong f x≈y)
private μ : ∀ {A : Setoid c (c ⊔ )} → (A ⊥ₛ) ⊥ₛ (A ⊥ₛ)
μ' : ∀ {A : Setoid c (c ⊔ )} → (A ⊥ₛ) ⊥ₛ (A ⊥ₛ) μ {A} (now x) = x
μ' {A} (now x) = x μ {A} (later x) = later (♯ μ {A} (♭ x))
μ' {A} (later x) = later (♯ μ' {A} (♭ x))
μ↓ : ∀ {A : Setoid c (c ⊔ )} {x : ( A ⊥) ⊥} {y : A ⊥} → _↓_ {A ⊥ₛ} x y → A [ (μ' {A} x) ≈ y ] μ↓ : ∀ {A : Setoid c (c ⊔ )} {x : ( A ⊥) ⊥} {y : A ⊥} → [ A ⊥ₛ ][ x ↓ y ] → [ A ][ (μ {A} x) ≈ y ]
μ↓ {A} {now x} {y} (now↓ x≈y) = x≈y μ↓ {A} {now x} {y} (now↓ x≈y) = x≈y
μ↓ {A} {later x} {y} (later↓ x↓y) = ≈-trans (later≈ (♯ μ↓ x↓y)) (≈-sym later-self) μ↓ {A} {later x} {y} (later↓ x↓y) = ≈-trans A (later≈ (♯ μ↓ x↓y)) (≈-sym A later-self)
-- TODO do delayFun and η in the same style as μ μ-cong : ∀ (A : Setoid c (c ⊔ )) {x y : ( A ⊥) ⊥} → [ A ⊥ₛ ][ x ≈ y ] → [ A ][ μ {A} x ≈ μ {A} y ]
μ-cong A {x} {now y} (↓≈ a≈b x↓a (now↓ y≈b)) = μ↓ (≡↓ (A ⊥ₛ) (≈-trans A a≈b (≈-sym A y≈b)) x↓a)
μ-cong A {now x} {later y} (↓≈ a≈b (now↓ x≈a) (later↓ y↓b)) = ≈-sym A (μ↓ (≡↓ (A ⊥ₛ) (≈-trans A (≈-sym A a≈b) (≈-sym A x≈a)) (later↓ y↓b)))
μ-cong A {later x} {later y} (↓≈ a≈b (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ μ-cong A (↓≈ a≈b x↓a y↓b))
μ-cong A {later x} {later y} (later≈ x≈y) = later≈ (♯ μ-cong A (♭ x≈y))
μ : ∀ (A : Setoid c (c ⊔ )) → (A ⊥ₛ) ⊥ₛ ⟶ A ⊥ₛ μₛ : ∀ (A : Setoid c (c ⊔ )) → (A ⊥ₛ) ⊥ₛ ⟶ A ⊥ₛ
μ A = record { _⟨$⟩_ = μ' {A} ; cong = cong' } μₛ A = record { _⟨$⟩_ = μ {A} ; cong = μ-cong A }
where
cong' : ∀ {x y : ( A ⊥) ⊥} → (A ⊥ₛ) [ x ≈ y ] → A [ μ' {A} x ≈ μ' {A} y ]
cong' {x} {now y} (↓≈ a≈b x↓a (now↓ y≈b)) = μ↓ (∼↓ (≈-trans a≈b (≈-sym y≈b)) x↓a)
cong' {now x} {later y} (↓≈ a≈b (now↓ x≈a) (later↓ y↓b)) = ≈-sym (μ↓ (∼↓ (≈-trans (≈-sym a≈b) (≈-sym x≈a)) (later↓ y↓b)))
cong' {later x} {later y} (↓≈ a≈b (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ cong' (↓≈ a≈b x↓a y↓b))
cong' {later x} {later y} (later≈ x≈y) = later≈ (♯ cong' (♭ x≈y))
μ-natural : ∀ {A B : Setoid c (c ⊔ )} (f : A ⟶ B) → (μ B ∘ delayFun (delayFun f)) ≋ (delayFun f ∘ μ A) μ-natural : ∀ {A B : Setoid c (c ⊔ )} (f : A ⟶ B) → (μₛ B ∘ liftFₛ (liftFₛ f)) ≋ (liftFₛ f ∘ μₛ A)
μ-natural {A} {B} f {now x} {now y} nx≈ny = cong (delayFun f) (now-inj nx≈ny) μ-natural {A} {B} f {now x} {now y} nx≈ny = cong (liftFₛ f) (now-inj nx≈ny)
μ-natural {A} {B} f {now x} {later y} (↓≈ a≈b (now↓ x≈a) (later↓ y↓b)) = later-eq (later≈ (♯ cong (delayFun f) (≈-sym (μ↓ (∼↓ (≈-trans (≈-sym a≈b) (≈-sym x≈a)) y↓b))))) μ-natural {A} {B} f {now x} {later y} (↓≈ a≈b (now↓ x≈a) (later↓ y↓b)) = later-eq (later≈ (♯ cong (liftFₛ f) (≈-sym A (μ↓ (≡↓ (A ⊥ₛ) (≈-trans A (≈-sym A a≈b) (≈-sym A x≈a)) y↓b)))))
μ-natural {A} {B} f {later x} {now y} (↓≈ a≈b (later↓ x↓a) (now↓ y≈b)) = ≈-sym (later-eq (later≈ (♯ ≈-sym (μ↓ (∼↓ (cong (delayFun f) (≈-trans a≈b (≈-sym y≈b))) (delayFun↓ (delayFun f) x↓a)))))) μ-natural {A} {B} f {later x} {now y} (↓≈ a≈b (later↓ x↓a) (now↓ y≈b)) = ≈-sym B (later-eq (later≈ (♯ ≈-sym B (μ↓ (≡↓ (B ⊥ₛ) (cong (liftFₛ f) (≈-trans A a≈b (≈-sym A y≈b))) (lift↓ (liftFₛ f) x↓a))))))
μ-natural {A} {B} f {later x} {later y} (↓≈ a≈b (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ μ-natural f (↓≈ a≈b x↓a y↓b)) μ-natural {A} {B} f {later x} {later y} (↓≈ a≈b (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ μ-natural f (↓≈ a≈b x↓a y↓b))
μ-natural {A} {B} f {later x} {later y} (later≈ x≈y) = later≈ (♯ μ-natural f (♭ x≈y)) μ-natural {A} {B} f {later x} {later y} (later≈ x≈y) = later≈ (♯ μ-natural f (♭ x≈y))
μ-assoc↓ : ∀ {A : Setoid c (c ⊔ )} {x : ( A ⊥) ⊥} {y : A ⊥} → _↓_ {A ⊥ₛ} x y → A [ (μ A) ⟨$⟩ x ≈ y ] μ-assoc↓ : ∀ {A : Setoid c (c ⊔ )} {x : ( A ⊥) ⊥} {y : A ⊥} → [ A ⊥ₛ ][ x ↓ y ] → [ A ][ (μₛ A) ⟨$⟩ x ≈ y ]
μ-assoc↓ {A} {now x} {y} (now↓ x≈y) = x≈y μ-assoc↓ {A} {now x} {y} (now↓ x≈y) = x≈y
μ-assoc↓ {A} {later x} {y} (later↓ x↓y) = ≈-sym (later-eq (later≈ (♯ ≈-sym (μ-assoc↓ x↓y)))) μ-assoc↓ {A} {later x} {y} (later↓ x↓y) = ≈-sym A (later-eq (later≈ (♯ ≈-sym A (μ-assoc↓ x↓y))))
μ↓-trans : ∀ {A : Setoid c (c ⊔ )} {x : ( A ⊥) ⊥} {y : A ⊥} {z : A } → [ A ⊥ₛ ][ x ↓ y ] → [ A ][ y ↓ z ] → [ A ][ (μₛ A ⟨$⟩ x) ↓ z ]
-- TODO needs another μ↓-trans for when multiple μ's are concerned, i.e. a chain with 3 conditions... μ↓-trans {A} {now x} {y} {z} (now↓ x≈y) y↓z = ≈↓ A (≈-sym A x≈y) y↓z
μ↓-trans : ∀ {A : Setoid c (c ⊔ )} {x : ( A ⊥) ⊥} {y : A ⊥} {z : A } → _↓_ {A ⊥ₛ} x y → _↓_ {A} y z → _↓_ {A} (μ A ⟨$⟩ x) z
μ↓-trans {A} {now x} {y} {z} (now↓ x≈y) y↓z = ≈↓ (≈-sym x≈y) y↓z
μ↓-trans {A} {later x} {y} {z} (later↓ x↓y) y↓z = later↓ (μ↓-trans x↓y y↓z) μ↓-trans {A} {later x} {y} {z} (later↓ x↓y) y↓z = later↓ (μ↓-trans x↓y y↓z)
-- μ↓-trans² : ∀ {A : Setoid c (c ⊔ )} {x : Delay (Delay ( A ⊥))} {y : Delay ( A ⊥)} {z : A ⊥} → _↓_ {delaySetoid (A ⊥ₛ)} x y → _↓_ {A ⊥ₛ} y z → _↓_ {A ⊥ₛ} ((delayFun (μ A)) ⟨$⟩ x) z -- -- μ↓-trans² : ∀ {A : Setoid c (c ⊔ )} {x : Delay (Delay ( A ⊥))} {y : Delay ( A ⊥)} {z : A ⊥} → _↓_ {delaySetoid (A ⊥ₛ)} x y → _↓_ {A ⊥ₛ} y z → _↓_ {A ⊥ₛ} ((delayFun (μ A)) ⟨$⟩ x) z
-- μ↓-trans² {A} {now x} {y} {now x₁} (now↓ xy) y↓z = now↓ (↓≈ {! !} (μ↓-trans {! !} {! y↓z !}) (now↓ (-refl A))) -- -- μ↓-trans² {A} {now x} {y} {now x₁} (now↓ xy) y↓z = now↓ (↓≈ {! !} (μ↓-trans {! !} {! y↓z !}) (now↓ (-refl A)))
-- μ↓-trans² {A} {now x} {y} {later x₁} (now↓ xy) y↓z = now↓ (↓≈ {! !} {! !} {! !}) -- -- μ↓-trans² {A} {now x} {y} {later x₁} (now↓ xy) y↓z = now↓ (↓≈ {! !} {! !} {! !})
-- μ↓-trans² {A} {later x} {y} {z} x↓y y↓z = {! !} -- -- μ↓-trans² {A} {later x} {y} {z} x↓y y↓z = {! !}
μ-assoc' : ∀ {A : Setoid c (c ⊔ )} {x : (( A ⊥)⊥)⊥} → [ A ][ (μₛ A ∘ (liftFₛ (μₛ A))) ⟨$⟩ x (μₛ A ∘ μₛ (A ⊥ₛ)) ⟨$⟩ x ]
μ-assoc' {A} {now x} = -refl A
μ-assoc' {A} {later x} = later (♯ μ-assoc' {A} {♭ x})
μ-assoc : ∀ {A : Setoid c (c ⊔ )} → (μ A ∘ (delayFun (μ A))) ≋ (μ A ∘ μ (A ⊥ₛ)) -- TODO μ-assoc follows from this!!
μ-assoc {A} {now x} {now y} (↓≈ a≈b (now↓ x≈a) (now↓ y≈b)) = cong (μ A) (≈-trans x≈a (≈-trans a≈b (≈-sym y≈b)))
μ-assoc {A} {now x} {later y} (↓≈ a≈b (now↓ x≈a) (later↓ y↓b)) = later-eq (later≈ (♯ cong (μ A) (≈-sym (μ↓ (∼↓ (≈-sym (≈-trans x≈a a≈b)) y↓b))))) -- y , liftF (μ A) (♭ x)
μ-assoc {A} {later x} {now y} (↓≈ {_} {_} {a} {b} (↓≈ c≈d a↓c b↓d) (later↓ x↓a) (now↓ y≈b)) = ≈-sym (later-eq (later≈ (♯ cong (μ A) {y} {liftF (μ A) (♭ x)} (↓≈ (≈-sym c≈d) (≈↓ (≈-sym y≈b) b↓d) {! delayFun↓ (μ A) {♭ x} x↓a !})))) -- (↓≈ (≈-sym c≈d) (≈↓ (≈-sym y≈b) b↓d) (μ↓-trans {! x↓a !} a↓c))
μ-assoc {A} {later x} {now y} (↓≈ (later≈ x≈y) (later↓ x↓a) (now↓ y≈b)) = {! !}
μ-assoc {A} {later x} {later y} (↓≈ a≈b (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ μ-assoc (↓≈ a≈b x↓a y↓b))
μ-assoc {A} {later x} {later y} (later≈ x≈y) = later≈ (♯ μ-assoc (♭ x≈y))
identityˡ↓ : ∀ {A : Setoid c (c ⊔ )} {x : A ⊥} {y : A } → _↓_ {A} x y → _↓_ {A} ((μ A) ⟨$⟩ ((delayFun (η A)) ⟨$⟩ x)) y -- μ-assoc : ∀ {A : Setoid c (c ⊔ )} → (μ A ∘ (delayFun (μ A))) ≋ (μ A ∘ μ (A ⊥ₛ))
-- μ-assoc {A} {now x} {now y} (↓≈ a≈b (now↓ x≈a) (now↓ y≈b)) = cong (μ A) (≈-trans x≈a (≈-trans a≈b (≈-sym y≈b)))
-- μ-assoc {A} {now x} {later y} (↓≈ a≈b (now↓ x≈a) (later↓ y↓b)) = later-eq (later≈ (♯ cong (μ A) (≈-sym (μ↓ (∼↓ (≈-sym (≈-trans x≈a a≈b)) y↓b))))) -- y , liftF (μ A) (♭ x)
-- μ-assoc {A} {later x} {now y} (↓≈ {_} {_} {a} {b} (↓≈ c≈d a↓c b↓d) (later↓ x↓a) (now↓ y≈b)) = ≈-sym (later-eq (later≈ (♯ cong (μ A) {y} {liftF (μ A) (♭ x)} (↓≈ (≈-sym c≈d) (≈↓ (≈-sym y≈b) b↓d) {! delayFun↓ (μ A) {♭ x} x↓a !})))) -- (↓≈ (≈-sym c≈d) (≈↓ (≈-sym y≈b) b↓d) (μ↓-trans {! x↓a !} a↓c))
-- μ-assoc {A} {later x} {now y} (↓≈ (later≈ x≈y) (later↓ x↓a) (now↓ y≈b)) = {! !}
-- μ-assoc {A} {later x} {later y} (↓≈ a≈b (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ μ-assoc (↓≈ a≈b x↓a y↓b))
-- μ-assoc {A} {later x} {later y} (later≈ x≈y) = later≈ (♯ μ-assoc (♭ x≈y))
identityˡ↓ : ∀ {A : Setoid c (c ⊔ )} {x : A ⊥} {y : A } → [ A ][ x ↓ y ] → [ A ][ ((μₛ A) ⟨$⟩ ((liftFₛ (η A)) ⟨$⟩ x)) ↓ y ]
identityˡ↓ {A} {now x} {y} x↓y = x↓y identityˡ↓ {A} {now x} {y} x↓y = x↓y
identityˡ↓ {A} {later x} {y} (later↓ x↓y) = later↓ (identityˡ↓ x↓y) identityˡ↓ {A} {later x} {y} (later↓ x↓y) = later↓ (identityˡ↓ x↓y)
identityˡ : ∀ {A : Setoid c (c ⊔ )} → (μ A ∘ delayFun (η A)) ≋ idₛ identityˡ : ∀ {A : Setoid c (c ⊔ )} → (μₛ A ∘ liftFₛ (η A)) ≋ idₛ
identityˡ {A} {now x} {y} x≈y = x≈y identityˡ {A} {now x} {y} x≈y = x≈y
identityˡ {A} {later x} {now y} (↓≈ ab (later↓ x↓a) (now↓ yb)) = ↓≈ (-refl A) (later↓ (identityˡ↓ x↓a)) (now↓ (-trans A yb (-sym A ab))) identityˡ {A} {later x} {now y} (↓≈ ab (later↓ x↓a) (now↓ yb)) = ↓≈ (≡-refl A) (later↓ (identityˡ↓ x↓a)) (now↓ (≡-trans A yb (≡-sym A ab)))
identityˡ {A} {later x} {later y} (↓≈ ab (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ identityˡ (↓≈ ab x↓a y↓b)) identityˡ {A} {later x} {later y} (↓≈ ab (later↓ x↓a) (later↓ y↓b)) = later≈ (♯ identityˡ (↓≈ ab x↓a y↓b))
identityˡ {A} {later x} {.(later _)} (later≈ x≈y) = later≈ (♯ identityˡ (♭ x≈y)) identityˡ {A} {later x} {.(later _)} (later≈ x≈y) = later≈ (♯ identityˡ (♭ x≈y))
identityʳ : ∀ {A : Setoid c (c ⊔ )} → (μ A ∘ η (A ⊥ₛ)) ≋ idₛ identityʳ : ∀ {A : Setoid c (c ⊔ )} → (μ A ∘ η (A ⊥ₛ)) ≋ idₛ
identityʳ {A} {now x} {y} x≈y = x≈y identityʳ {A} {now x} {y} x≈y = x≈y
identityʳ {A} {later x} {y} x≈y = x≈y identityʳ {A} {later x} {y} x≈y = x≈y
@ -249,14 +250,14 @@ module Monad.Instance.K.Instance.Delay {c } where
delayMonad = record delayMonad = record
{ F = record { F = record
{ F₀ = _⊥ₛ { F₀ = _⊥ₛ
; F₁ = delayFun ; F₁ = liftFₛ
; identity = delayFun-id ; identity = lift-id
; homomorphism = delayFun-comp ; homomorphism = λ {X} {Y} {Z} {f} {g} → lift-comp {X} {Y} {Z} {f} {g}
; F-resp-≈ = delayFun-resp-≈ ; F-resp-≈ = λ {A} {B} {f} {g} → lift-resp-≈ {A} {B} {f} {g}
} }
; η = ntHelper (record { η = η ; commute = η-natural }) ; η = ntHelper (record { η = η ; commute = η-natural })
; μ = ntHelper (record { η = μ ; commute = μ-natural }) ; μ = ntHelper (record { η = μ ; commute = μ-natural })
; assoc = μ-assoc ; assoc = {! !}
; sym-assoc = {! !} ; sym-assoc = {! !}
; identityˡ = identityˡ ; identityˡ = identityˡ
; identityʳ = identityʳ ; identityʳ = identityʳ