Work on delay monad

This commit is contained in:
Leon Vatthauer 2023-12-13 20:53:33 +01:00
parent 4c3f3923a1
commit 17ecc55223
Signed by: leonv
SSH key fingerprint: SHA256:G4+ddwoZmhLPRB1agvXzZMXIzkVJ36dUYZXf5NxT+u8
3 changed files with 142 additions and 8 deletions

View file

@ -26,8 +26,9 @@ open Eq using (_≡_)
Most of the required properties are already proven in the agda-categories library, we are only left to construct the natural numbers object. Most of the required properties are already proven in the agda-categories library, we are only left to construct the natural numbers object.
```agda ```agda
module Category.Ambient.Setoid {} where module Category.Ambient.Setoids {} where
-- equality on setoid functions -- equality on setoid functions
private
_≋_ : ∀ {A B : Setoid } → A ⟶ B → A ⟶ B → Set _≋_ : ∀ {A B : Setoid } → A ⟶ B → A ⟶ B → Set
_≋_ {A} {B} f g = Setoid._≈_ (A ⇨ B) f g _≋_ {A} {B} f g = Setoid._≈_ (A ⇨ B) f g
≋-sym : ∀ {A B : Setoid } {f g : A ⟶ B} → f ≋ g → g ≋ f ≋-sym : ∀ {A B : Setoid } {f g : A ⟶ B} → f ≋ g → g ≋ f

View file

@ -25,6 +25,8 @@ 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
``` ```
--> -->
@ -105,6 +107,11 @@ module Monad.Instance.K.Instance.Delay {c } where
now-cong : ∀ {A : Setoid c (c ⊔ )} {x y : Setoid.Carrier A} → A [ x y ] → A [ now x ≈ now y ] now-cong : ∀ {A : Setoid c (c ⊔ )} {x y : Setoid.Carrier A} → A [ x y ] → A [ now x ≈ now y ]
now-cong {A} {x} {y} xy = ↓≈ xy (now↓ (-refl A)) (now↓ (-refl A)) 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 : Delay (Setoid.Carrier 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 : Setoid.Carrier A} → A [ now x ≈ now y ] → A [ x y ] now-inj : ∀ {A : Setoid c (c ⊔ )} {x y : Setoid.Carrier 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)) now-inj {A} {x} {y} (↓≈ ab (now↓ xa) (now↓ yb)) = -trans A xa (-trans A ab (-sym A yb))
@ -239,7 +246,6 @@ module Monad.Instance.K.Instance.Delay {c } where
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
delayMonad : Monad (Setoids c (c ⊔ )) delayMonad : Monad (Setoids c (c ⊔ ))
delayMonad = record delayMonad = record
{ F = record { F = record

View file

@ -0,0 +1,127 @@
<!--
```agda
{-# OPTIONS --allow-unsolved-metas --guardedness #-}
open import Level
open import Relation.Binary
open import Data.Sum using (_⊎_; inj₁; inj₂; [_,_])
open import Data.Sum.Function.Setoid
open import Data.Sum.Relation.Binary.Pointwise
open import Function.Equality as SΠ renaming (id to idₛ)
open import Codata.Musical.Notation
open import Function using () renaming (_∘_ to _∘f_)
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_) renaming (sym to ≡-sym)
```
-->
# The delay monad on the category of setoids is pre-Elgot
```agda
module Monad.Instance.Setoids.Delay.PreElgot { : Level} where
open import Monad.Instance.K.Instance.Delay {} {}
open import Category.Ambient.Setoids
open import Algebra.Elgot (setoidAmbient {})
open import Monad.PreElgot (setoidAmbient {})
open Equality
conflict : ∀ {''} (X Y : Setoid ) {Z : Set ''}
{x : Setoid.Carrier X} {y : Setoid.Carrier Y } → (X ⊎ₛ Y) [ inj₁ x inj₂ y ] → Z
conflict X Y ()
iter : ∀ {A X : Setoid } → (Setoid.Carrier X → (Delay (Setoid.Carrier A) ⊎ Setoid.Carrier X)) → Setoid.Carrier X → Delay (Setoid.Carrier A)
iter {A} {X} f x with f x
... | inj₁ a = a
... | inj₂ b = later (♯ (iter {A} {X} f b))
inj₁-helper : ∀ {X Y : Setoid } (f : X ⟶ Y ⊎ₛ X) {x y : Setoid.Carrier X} {a b : Setoid.Carrier Y} → X [ x y ] → f ⟨$⟩ x ≡ inj₁ a → f ⟨$⟩ y ≡ inj₁ b → Y [ a b ]
inj₁-helper {X} {Y} f {x} {y} {a} {b} xy fi₁ fi₂ = drop-inj₁ {x = a} {y = b} helper
where
helper : (Y ⊎ₛ X) [ inj₁ a inj₁ b ]
helper rewrite (≡-sym fi₁) | (≡-sym fi₂) = cong f xy
inj₂-helper : ∀ {X Y : Setoid } (f : X ⟶ Y ⊎ₛ X) {x y : Setoid.Carrier X} {a b : Setoid.Carrier X} → X [ x y ] → f ⟨$⟩ x ≡ inj₂ a → f ⟨$⟩ y ≡ inj₂ b → X [ a b ]
inj₂-helper {X} {Y} f {x} {y} {a} {b} xy fi₁ fi₂ = drop-inj₂ {x = a} {y = b} helper
where
helper : (Y ⊎ₛ X) [ inj₂ a inj₂ b ]
helper rewrite (≡-sym fi₁) | (≡-sym fi₂) = cong f xy
absurd-helper : ∀ {'} {X Y : Setoid } {A : Set '} (f : X ⟶ Y ⊎ₛ X) {x y : Setoid.Carrier X} {a : Setoid.Carrier Y} {b : Setoid.Carrier X} → X [ x y ] → f ⟨$⟩ x ≡ inj₁ a → f ⟨$⟩ y ≡ inj₂ b → A
absurd-helper {'} {X} {Y} {A} f {x} {y} {a} {b} xy fi₁ fi₂ = conflict Y X helper
where
helper : (Y ⊎ₛ X) [ inj₁ a inj₂ b ]
helper rewrite (≡-sym fi₁) | (≡-sym fi₂) = cong f xy
iter-cong : ∀ {A X : Setoid } (f : X ⟶ (delaySetoid A ⊎ₛ X)) {x y : Setoid.Carrier X} → X [ x y ] → A [ iter {A} {X} (f ._⟨$⟩_) x ≈ iter {A} {X} (f ._⟨$⟩_) y ]
iter-cong {A} {X} f {x} {y} xy with (f ._⟨$⟩_) x in eqx | (f ._⟨$⟩_) y in eqy
... | inj₁ a | inj₁ b = inj₁-helper f xy eqx eqy
... | inj₁ a | inj₂ b = absurd-helper f xy eqx eqy
... | inj₂ a | inj₁ b = absurd-helper f (-sym X xy) eqy eqx
... | inj₂ a | inj₂ b = later≈ (♯ iter-cong {A} {X} f (inj₂-helper f xy eqx eqy))
iter-fixpoint : ∀ {A X : Setoid } {f : X ⟶ (delaySetoid A ⊎ₛ X)} {x y : Setoid.Carrier X} → X [ x y ] → A [ iter {A} {X} (f ._⟨$⟩_) x ≈ [ Function.id , iter {A} {X} (f ._⟨$⟩_) ] ((f ._⟨$⟩_) y) ]
iter-fixpoint {A} {X} {f} {x} {y} xy with (f ._⟨$⟩_) x in eqx | (f ._⟨$⟩_) y in eqy
... | inj₁ a | inj₁ b = inj₁-helper f xy eqx eqy
... | inj₁ a | inj₂ b = absurd-helper f xy eqx eqy
... | inj₂ a | inj₁ b = absurd-helper f (-sym X xy) eqy eqx
... | inj₂ a | inj₂ b = ≈-sym (later-eq (later≈ (♯ iter-cong f (-sym X (inj₂-helper f xy eqx eqy)))))
iter-uni : ∀ {A X Y : Setoid } {f : X ⟶ (delaySetoid A ⊎ₛ X)} {g : Y ⟶ (delaySetoid A ⊎ₛ Y)} {h : X ⟶ Y} → ([ inj₁ₛ ∘ idₛ , inj₂ₛ ∘ h ]ₛ ∘ f) ≋ (g ∘ h) → ∀ {x y : Setoid.Carrier X} → X [ x y ] → A [ iter {A} {X} (f ._⟨$⟩_) x ≈ (iter {A} {Y} (g ._⟨$⟩_)) (h ⟨$⟩ y) ]
iter-uni {A} {X} {Y} {f} {g} {h} hf≈gh {x} {y} xy with (f ._⟨$⟩_) x in eqx | (f ._⟨$⟩_) y in eqy
... | inj₁ a | inj₁ b with g ⟨$⟩ (h ⟨$⟩ y) in eqc
... | inj₁ c = drop-inj₁ {x = a} {y = c} helper
where
helper'' : (delaySetoid A ⊎ₛ Y) [ inj₁ a [ inj₁ , (λ x₁ → inj₂ (h ⟨$⟩ x₁)) ] (f ⟨$⟩ x) ]
helper'' rewrite eqx = ⊎-refl ≈-refl (-refl Y)
helper' : (delaySetoid A ⊎ₛ Y) [ inj₁ a (g ⟨$⟩ (h ⟨$⟩ y)) ]
helper' = -trans ((delaySetoid A) ⊎ₛ Y) helper'' (hf≈gh {x} {y} xy)
helper : (delaySetoid A ⊎ₛ Y) [ inj₁ a inj₁ c ]
helper rewrite (≡-sym eqc) = helper'
... | inj₂ c = conflict (delaySetoid A) Y helper
where
helper'' : (delaySetoid A ⊎ₛ Y) [ inj₁ a [ inj₁ , (λ x₁ → inj₂ (h ⟨$⟩ x₁)) ] (f ⟨$⟩ x) ]
helper'' rewrite eqx = ⊎-refl ≈-refl (-refl Y)
helper' : (delaySetoid A ⊎ₛ Y) [ inj₁ a (g ⟨$⟩ (h ⟨$⟩ y)) ]
helper' = -trans ((delaySetoid A) ⊎ₛ Y) helper'' (hf≈gh {x} {y} xy)
helper : (delaySetoid A ⊎ₛ Y) [ inj₁ a inj₂ c ]
helper rewrite (≡-sym eqc) = helper'
iter-uni {A} {X} {Y} {f} {g} {h} hf≈gh {x} {y} xy | inj₁ a | inj₂ b = absurd-helper f xy eqx eqy
iter-uni {A} {X} {Y} {f} {g} {h} hf≈gh {x} {y} xy | inj₂ a | inj₁ b = absurd-helper f (-sym X xy) eqy eqx
iter-uni {A} {X} {Y} {f} {g} {h} hf≈gh {x} {y} xy | inj₂ a | inj₂ b with g ⟨$⟩ (h ⟨$⟩ y) in eqc -- = ≈-sym (later-eq (later≈ (♯ {! !}))) -- (iter-uni {A} {X} {Y} {f} {{! !}} {h} hf≈gh ((inj₂-helper f xy eqx eqy)))
... | inj₁ c = {! !} -- absurd, probably...
... | inj₂ c = later≈ (♯ {! !})
-- why does this not terminate??
-- | inj₂ c = ≈-trans (later≈ {A} {_} {♯ (iter {A} {Y} (g ._⟨$⟩_)) (h ⟨$⟩ a)} (♯ (iter-uni {A} {X} {Y} {f} {g} {h} hf≈gh (-refl X)))) (later≈ (♯ iter-cong g (-sym Y helper)))
where
helper''' : (delaySetoid A ⊎ₛ Y) [ inj₂ c g ⟨$⟩ (h ⟨$⟩ y) ]
helper''' rewrite eqc = -refl (delaySetoid A ⊎ₛ Y)
helper'' : (delaySetoid A ⊎ₛ Y) [ g ⟨$⟩ (h ⟨$⟩ y) ([ inj₁ , (λ x₂ → inj₂ (h ⟨$⟩ x₂)) ] (f ⟨$⟩ x)) ]
helper'' = -sym (delaySetoid A ⊎ₛ Y) (hf≈gh xy)
helper' : (delaySetoid A ⊎ₛ Y) [ ([ inj₁ , (λ x₂ → inj₂ (h ⟨$⟩ x₂)) ] (f ⟨$⟩ x)) inj₂ (h ⟨$⟩ a) ]
helper' rewrite eqx = -refl (delaySetoid A ⊎ₛ Y)
helper : Y [ c h ⟨$⟩ a ]
helper = drop-inj₂ {x = c} {y = h ⟨$⟩ a} (-trans (delaySetoid A ⊎ₛ Y) helper''' (-trans (delaySetoid A ⊎ₛ Y) helper'' helper'))
help : A [ later (♯ iter {A} {Y} (g ._⟨$⟩_) (h ⟨$⟩ a)) ≈ later (♯ (iter {A} {Y} (g ._⟨$⟩_) c)) ]
help = later≈ (♯ iter-cong g (-sym Y helper))
delay-algebras : ∀ {A : Setoid } → Elgot-Algebra-on (delaySetoid A)
delay-algebras {A} = record
{ _# = λ {X} f → record { _⟨$⟩_ = iter {A} {X} (f ._⟨$⟩_) ; cong = λ {x} {y} xy → iter-cong {A} {X} f {x} {y} xy }
; #-Fixpoint = λ {X} {f} → iter-fixpoint {A} {X} {f}
; #-Uniformity = λ {X} {Y} {f} {g} {h} → iter-uni {A} {X} {Y} {f} {g} {h}
; #-Folding = {! !}
; #-resp-≈ = {! !}
}
where
iterr : ∀ {X : Setoid } → X ⟶ ((delaySetoid A) ⊎ₛ X) → X ⟶ (delaySetoid A)
iterr {X} f = record { _⟨$⟩_ = {! !} ; cong = {! !} }
delayPreElgot : IsPreElgot delayMonad
delayPreElgot = record
{ elgotalgebras = delay-algebras
; extend-preserves = {! !}
}
```