{-# OPTIONS --safe --cubical-compatible --guardedness --exact-split --double-check #-}
module ALMA.SecondPass.Lambek where
open import Agda.Primitive using (Level; lsuc; _⊔_)
open import Agda.Builtin.Equality using (_≡_; refl)
open import Agda.Builtin.Sigma using (_,_)
open import Relation.Binary.PropositionalEquality.Core using (cong; sym; trans)
open import Relation.Binary.PropositionalEquality.Properties using (module ≡-Reasoning)
import Relation.Binary.Reasoning.Setoid as SetoidReasoning
open import Relation.Binary.Bundles using (Setoid)
open import Function.Base using (_∘_; id)
open import Function.Bundles using (Func)
open import Categories.Category.Core using (Category)
open import Categories.Functor.Core using (Functor)
open import ALMA.Base.Equivalence using (module SubstTransport-Left)
open import ALMA.Cosmos.ContCategory using (ContCat)
open import ALMA.SecondPass.Unfolding using (Unfolding; mapUnfolding; mapUnfolding-∘; module UnfoldingSetoid)
open import ALMA.SecondPass.Cosmos using (Cosmos; out)
open import ALMA.SecondPass.Terminal
using (Coalgebra; CoalgHom; cosmosCoalg; ana-hom; unique-ana; ana-to; _≈C_; ≈C-sym; cosmosSetoid)
module _ {o h e s p : Level}
{C : Category o h e}
{FC : Functor C (ContCat s p)} where
private
L = o ⊔ h ⊔ e ⊔ s ⊔ p
Lʳ = o ⊔ s ⊔ p
module US = UnfoldingSetoid
{o = o} {h = h} {e = e} {s = s} {p = p} {C = C} {F = FC}
T = Cosmos C FC
FT : Set L
FT = Unfolding FC T
FFT : Set L
FFT = Unfolding FC FT
CS : Setoid L Lʳ
CS = cosmosSetoid {o = o} {h = h} {e = e} {s = s} {p = p} {C = C} {FC = FC}
CC : Coalgebra Lʳ
CC = cosmosCoalg {o = o} {h = h} {e = e} {s = s} {p = p} {C = C} {FC = FC}
outF : Func CS (US.unfoldingSetoid CS)
outF = Coalgebra.α CC
FT-Setoid : Setoid L L
FT-Setoid = record
{ Carrier = FT
; _≈_ = _≡_ {A = FT}
; isEquivalence = record { refl = refl ; sym = sym ; trans = trans }
}
≡→≈F-FT : ∀ {d₁ d₂ : FFT} → d₁ ≡ d₂ → US._≈U_ {X = FT-Setoid} d₁ d₂
≡→≈F-FT refl = US.≈U-refl {X = FT-Setoid}
FT-α : Func FT-Setoid (US.unfoldingSetoid FT-Setoid)
FT-α = record
{ to = mapUnfolding out
; cong = λ {c₁} {c₂} c₁≡c₂ → ≡→≈F-FT (cong (mapUnfolding out) c₁≡c₂)
}
FT-Coalg : Coalgebra L
FT-Coalg = record { Carrier = FT-Setoid ; α = FT-α }
in-hom : CoalgHom {r₁ = L} {r₂ = Lʳ} FT-Coalg CC
in-hom = ana-hom FT-Coalg
in-F : FT → T
in-F = Func.to (CoalgHom.f in-hom)
in-F-resp-≈F : ∀ {c₁ c₂} → US._≈U_ {X = CS} c₁ c₂ → in-F c₁ ≈C in-F c₂
in-F-resp-≈F {c₁} {c₂} eq = helper eq
where
open _≈C_
helper : ∀ {d₁ d₂} → US._≈U_ {X = CS} d₁ d₂ → in-F d₁ ≈C in-F d₂
helper {d₁} {d₂} eq .unfoldFunctor₀-eq {A} s =
SubstTransport-Left._≈sl_.shape-eq
(US._≈U_.sl-eq eq) (A , s)
helper {d₁} {d₂} eq .pos-to-shape-eq {A} s p =
SubstTransport-Left._≈sl_.position-eq
(US._≈U_.sl-eq eq) (A , s) p
helper {d₁} {d₂} eq .unfold-next-eq {A} s =
helper (Func.cong outF (US._≈U_.unfold-next-eq eq s))
private
in∘out-f : Func CS CS
in∘out-f = record
{ to = in-F ∘ out
; cong = λ {x} {y} x≈y → in-F-resp-≈F (Func.cong outF x≈y)
}
in∘out-hom : CoalgHom CC CC
in∘out-hom = record { f = in∘out-f ; commute = commute-proof }
where
commute-proof : ∀ x → mapUnfolding (in-F ∘ out) (out x) ≡ out (in-F (out x))
commute-proof x = let open ≡-Reasoning in begin
mapUnfolding (in-F ∘ out) (out x)
≡⟨ mapUnfolding-∘ in-F out (out x) ⟩
mapUnfolding in-F (mapUnfolding out (out x))
≡⟨ CoalgHom.commute in-hom (out x) ⟩
out (in-F (out x))
∎
idFunc : Func CS CS
idFunc = record { to = id ; cong = λ x≈y → x≈y }
id-hom : CoalgHom CC CC
id-hom = record { f = idFunc ; commute = λ _ → refl }
in∘out≈id : ∀ x → in-F (out x) ≈C x
in∘out≈id x =
let open SetoidReasoning CS in begin
in-F (out x) ≈⟨ unique-ana CC in∘out-hom x ⟩
ana-to CC x ≈⟨ ≈C-sym (unique-ana CC id-hom x) ⟩
x ∎
out∘in≡F∘ : ∀ y → out (in-F y) ≡ mapUnfolding (in-F ∘ out) y
out∘in≡F∘ y = let open ≡-Reasoning in begin
out (in-F y)
≡⟨ sym (CoalgHom.commute in-hom y) ⟩
mapUnfolding in-F (mapUnfolding out y)
≡⟨ sym (mapUnfolding-∘ in-F out y) ⟩
mapUnfolding (in-F ∘ out) y
∎
private
open Unfolding using (unfold-next)
F∘≈Fid : ∀ y → US._≈U_ {X = CS} (mapUnfolding (in-F ∘ out) y) y
F∘≈Fid y = record
{ sl-eq = record
{ shape-eq = λ _ → refl
; position-eq = λ _ _ → refl
}
; unfold-next-eq = λ {A} s →
in∘out≈id (unfold-next y s)
}
out∘in≈Fid : ∀ y → US._≈U_ {X = CS} (out (in-F y)) y
out∘in≈Fid y =
let open SetoidReasoning (US.unfoldingSetoid CS) in begin
out (in-F y) ≡⟨ out∘in≡F∘ y ⟩
mapUnfolding (in-F ∘ out) y ≈⟨ F∘≈Fid y ⟩
y ∎
record LambekIso : Set (lsuc L) where
field
inverse : FT → T
inverse-left : ∀ x → inverse (out x) ≈C x
inverse-right : ∀ y → US._≈U_ {X = CS} (out (inverse y)) y
lambekIso : LambekIso
lambekIso = record
{ inverse = in-F
; inverse-left = in∘out≈id
; inverse-right = out∘in≈Fid
}