module Cat.Cartesian.Solver where
module NbE
{o β} {C : Precategory o β}
(term : Terminal C)
(prod : Binary-products C) where
open Cat.Reasoning C
open Binary-products prod
open Terminal term
data βObβ : Type (o β β) where
βtopβ : βObβ
_βββ_ : βObβ β βObβ β βObβ
β_β : Ob β βObβ
sem-ob : βObβ β Ob
sem-ob βtopβ = top
sem-ob (X βββ Y) = sem-ob X ββ sem-ob Y
sem-ob β X β = X
instance
Brackets-βObβ : β¦β§-notation βObβ
Brackets-βObβ .β¦β§-notation.lvl = o
Brackets-βObβ .β¦β§-notation.Sem = Ob
Brackets-βObβ .β¦β§-notation.β¦_β§ = sem-ob
data βHomβ : βObβ β βObβ β Type (o β β) where
βidβ : β {X} β βHomβ X X
_βββ_ : β {X Y Z} β βHomβ Y Z β βHomβ X Y β βHomβ X Z
β!β : β {X} β βHomβ X βtopβ
βΟββ : β {X Y} β βHomβ (X βββ Y) X
βΟββ : β {X Y} β βHomβ (X βββ Y) Y
ββ¨_,_β©β : β {X Y Z} β βHomβ X Y β βHomβ X Z β βHomβ X (Y βββ Z)
β_β : β {X Y} β Hom β¦ X β§ β¦ Y β§ β βHomβ X Y
sem-hom : β {X Y} β βHomβ X Y β Hom β¦ X β§ β¦ Y β§
sem-hom βidβ = id
sem-hom (f βββ g) = sem-hom f β sem-hom g
sem-hom β!β = !
sem-hom βΟββ = Οβ
sem-hom βΟββ = Οβ
sem-hom ββ¨ f , g β©β = β¨ sem-hom f , sem-hom g β©
sem-hom β f β = f
instance
Brackets-βHomβ : β {X Y} β β¦β§-notation (βHomβ X Y)
Brackets-βHomβ .β¦β§-notation.lvl = _
Brackets-βHomβ .β¦β§-notation.Sem = _
Brackets-βHomβ .β¦β§-notation.β¦_β§ = sem-hom
data Value : βObβ β βObβ β Type (o β β) where
vhom : β {X Y} β Hom β¦ X β§ β¦ Y β§ β Value X Y
vbang : β {X} β Value X βtopβ
vpair : β {X Y Z} β Value X Y β Value X Z β Value X (Y βββ Z)
vfst : β {X Y Z} β Value X (Y βββ Z) β Value X Y
vfst (vhom f) = vhom (Οβ β f)
vfst (vpair v1 v2) = v1
vsnd : β {X Y Z} β Value X (Y βββ Z) β Value X Z
vsnd (vhom f) = vhom (Οβ β f)
vsnd (vpair v1 v2) = v2
vid : β {X} β Value X X
vid = vhom id
reflect : β X Y β Value X Y β Hom β¦ X β§ β¦ Y β§
reflect X βtopβ v = !
reflect X (Y βββ Z) v = β¨ (reflect X Y (vfst v)) , reflect X Z (vsnd v) β©
reflect X β Y β (vhom f) = f
eval : β {X Y Z} β βHomβ Y Z β Value X Y β Value X Z
eval βidβ v = v
eval (e1 βββ e2) v = eval e1 (eval e2 v)
eval β!β v = vbang
eval βΟββ v = vfst v
eval βΟββ v = vsnd v
eval ββ¨ e1 , e2 β©β v = vpair (eval e1 v) (eval e2 v)
eval β f β v = vhom (f β reflect _ _ v)
nf : β X Y β βHomβ X Y β Hom β¦ X β§ β¦ Y β§
nf X Y e = reflect X Y (eval e vid)
vhom-sound : β X Y β (f : Hom β¦ X β§ β¦ Y β§) β reflect X Y (vhom f) β‘ f
vhom-sound X βtopβ f = !-unique f
vhom-sound X (Y βββ Z) f =
β¨ reflect X Y (vhom (Οβ β f)) , reflect X Z (vhom (Οβ β f)) β© β‘β¨ apβ β¨_,_β© (vhom-sound X Y (Οβ β f)) (vhom-sound X Z (Οβ β f)) β©
β¨ Οβ β f , Οβ β f β© β‘Λβ¨ β¨β©-unique refl refl β©
f β
vhom-sound X β Y β f = refl
vfst-sound : β X Y Z β (v : Value X (Y βββ Z)) β reflect X Y (vfst v) β‘ Οβ β reflect X (Y βββ Z) v
vfst-sound X Y Z (vhom f) =
reflect X Y (vhom (Οβ β f)) β‘β¨ vhom-sound X Y (Οβ β f) β©
Οβ β f β‘Λβ¨ reflβ©ββ¨ vhom-sound X (Y βββ Z) f β©
Οβ β reflect X (Y βββ Z) (vhom f) β
vfst-sound X Y Z (vpair v1 v2) =
reflect X Y v1 β‘Λβ¨ Οβββ¨β© β©
Οβ β β¨ (reflect X Y v1) , (reflect X Z v2) β© β
vsnd-sound : β X Y Z β (v : Value X (Y βββ Z)) β reflect X Z (vsnd v) β‘ Οβ β reflect X (Y βββ Z) v
vsnd-sound X Y Z (vhom f) =
reflect X Z (vhom (Οβ β f)) β‘β¨ vhom-sound X Z (Οβ β f) β©
Οβ β f β‘Λβ¨ reflβ©ββ¨ vhom-sound X (Y βββ Z) f β©
Οβ β reflect X (Y βββ Z) (vhom f) β
vsnd-sound X Y Z (vpair v1 v2) =
reflect X Z v2 β‘Λβ¨ Οβββ¨β© β©
Οβ β β¨ (reflect X Y v1) , (reflect X Z v2) β© β
sound-k : β X Y Z β (e : βHomβ Y Z) β (v : Value X Y)
β reflect X Z (eval e v) β‘ β¦ e β§ β reflect X Y v
sound-k X Y Y βidβ v = sym (idl _)
sound-k X Y Z (e1 βββ e2) v =
reflect X Z (eval e1 (eval e2 v)) β‘β¨ sound-k X _ Z e1 (eval e2 v) β©
β¦ e1 β§ β reflect X _ (eval e2 v) β‘β¨ reflβ©ββ¨ sound-k X Y _ e2 v β©
β¦ e1 β§ β β¦ e2 β§ β reflect X Y v β‘β¨ assoc _ _ _ β©
β¦ e1 βββ e2 β§ β reflect X Y v β
sound-k X Y βtopβ β!β v = !-unique (! β reflect X Y v)
sound-k X (Y βββ Z) Y βΟββ v = vfst-sound X Y Z v
sound-k X (Y βββ Z) Z βΟββ v = vsnd-sound X Y Z v
sound-k X Y (Z1 βββ Z2) ββ¨ e1 , e2 β©β v =
β¨ reflect X Z1 (eval e1 v) , reflect X Z2 (eval e2 v) β© β‘β¨ apβ β¨_,_β© (sound-k X Y Z1 e1 v) (sound-k X Y Z2 e2 v) β©
β¨ β¦ e1 β§ β reflect X Y v , β¦ e2 β§ β reflect X Y v β© β‘Λβ¨ β¨β©β _ β©
β¨ β¦ e1 β§ , β¦ e2 β§ β© β reflect X Y v β
sound-k X Y Z β x β v = vhom-sound X Z _
sound : β X Y β (e : βHomβ X Y) β nf X Y e β‘ β¦ e β§
sound X Y e = sound-k X X Y e vid β elimr (vhom-sound X X id)
abstract
solve : β X Y β (e1 e2 : βHomβ X Y) β nf X Y e1 β‘ nf X Y e2 β β¦ e1 β§ β‘ β¦ e2 β§
solve X Y e1 e2 p = sym (sound X Y e1) Β·Β· p Β·Β· sound X Y e2
module _
{o β} {C : Precategory o β}
{term : Terminal C}
{prod : Binary-products C} where
open Cat.Reasoning C
open Binary-products prod
open Terminal term
open NbE term prod
record Cartesian-ob (X : Ob) : TypeΟ where
field
βobβ : βObβ
ob-repr : β¦ βobβ β§ β‘α΅’ X
βobβ : (X : Ob) β β¦ βXβ : Cartesian-ob X β¦ β βObβ
βobβ X β¦ βXβ β¦ = Cartesian-ob.βobβ βXβ
ob-repr : (X : Ob) β β¦ βXβ : Cartesian-ob X β¦ β β¦ βobβ X β§ β‘α΅’ X
ob-repr X β¦ βXβ β¦ = Cartesian-ob.ob-repr βXβ
record Cartesian-hom
{X Y : Ob}
β¦ βXβ : Cartesian-ob X β¦ β¦ βYβ : Cartesian-ob Y β¦
(f : Hom X Y) : TypeΟ where
field
βhomβ : βHomβ (βobβ X) (βobβ Y)
βhomβ
: β {X Y : Ob} β (f : Hom X Y)
β β¦ βXβ : Cartesian-ob X β¦ β¦ βYβ : Cartesian-ob Y β¦
β β¦ βfβ : Cartesian-hom f β¦
β βHomβ (βobβ X) (βobβ Y)
βhomβ f β¦ βfβ = βfβ β¦ = Cartesian-hom.βhomβ βfβ
instance
Cartesian-ob-Default
: β {X} β Cartesian-ob X
Cartesian-ob-Default {X = X} .Cartesian-ob.βobβ = NbE.β X β
Cartesian-ob-Default .Cartesian-ob.ob-repr = reflα΅’
{-# INCOHERENT Cartesian-ob-Default #-}
Cartesian-ob-top
: Cartesian-ob top
Cartesian-ob-top .Cartesian-ob.βobβ = βtopβ
Cartesian-ob-top .Cartesian-ob.ob-repr = reflα΅’
Cartesian-ob-ββ
: β {X Y}
β β¦ βXβ : Cartesian-ob X β¦ β¦ βYβ : Cartesian-ob Y β¦
β Cartesian-ob (X ββ Y)
Cartesian-ob-ββ {X = X} {Y = Y} .Cartesian-ob.βobβ =
βobβ X βββ βobβ Y
Cartesian-ob-ββ {X = X} {Y = Y} .Cartesian-ob.ob-repr =
apβα΅’ _ββ_ (ob-repr X) (ob-repr Y)
Cartesian-hom-Default
: β {X Y} {f : Hom X Y}
β β¦ βXβ : Cartesian-ob X β¦ β¦ βYβ : Cartesian-ob Y β¦
β Cartesian-hom f
Cartesian-hom-Default {X = X} {Y = Y} {f = f} .Cartesian-hom.βhomβ =
β substα΅’ (Ξ» X β Hom X β¦ βobβ Y β§) (symα΅’ (ob-repr X)) (substα΅’ (Ξ» Y β Hom X Y) (symα΅’ (ob-repr Y)) f) β
{-# INCOHERENT Cartesian-hom-Default #-}
Cartesian-hom-id
: β {X}
β β¦ βXβ : Cartesian-ob X β¦
β Cartesian-hom (id {X})
Cartesian-hom-id {X = X} .Cartesian-hom.βhomβ = βidβ {X = βobβ X}
Cartesian-hom-β
: β {X Y Z} {f : Hom Y Z} {g : Hom X Y}
β β¦ βXβ : Cartesian-ob X β¦ β¦ βYβ : Cartesian-ob Y β¦ β¦ βZβ : Cartesian-ob Z β¦
β β¦ βfβ : Cartesian-hom f β¦ β¦ βgβ : Cartesian-hom g β¦
β Cartesian-hom (f β g)
Cartesian-hom-β {f = f} {g = g} .Cartesian-hom.βhomβ = βhomβ f βββ βhomβ g
Cartesian-hom-!
: β {X}
β β¦ βXβ : Cartesian-ob X β¦
β Cartesian-hom (! {X})
Cartesian-hom-! .Cartesian-hom.βhomβ = β!β
Cartesian-hom-Οβ
: β {X Y}
β β¦ βXβ : Cartesian-ob X β¦ β¦ βYβ : Cartesian-ob Y β¦
β Cartesian-hom (Οβ {X} {Y})
Cartesian-hom-Οβ .Cartesian-hom.βhomβ = βΟββ
Cartesian-hom-Οβ
: β {X Y}
β β¦ βXβ : Cartesian-ob X β¦ β¦ βYβ : Cartesian-ob Y β¦
β Cartesian-hom (Οβ {X} {Y})
Cartesian-hom-Οβ .Cartesian-hom.βhomβ = βΟββ
Cartesian-hom-β¨β©
: β {X Y Z} {f : Hom X Y} {g : Hom X Z}
β β¦ βXβ : Cartesian-ob X β¦ β¦ βYβ : Cartesian-ob Y β¦ β¦ βZβ : Cartesian-ob Z β¦
β β¦ βfβ : Cartesian-hom f β¦ β¦ βgβ : Cartesian-hom g β¦
β Cartesian-hom β¨ f , g β©
Cartesian-hom-β¨β© {f = f} {g = g} .Cartesian-hom.βhomβ = ββ¨ βhomβ f , βhomβ g β©β
abstract
solve-cartesian
: β {o h} {C : Precategory o h} (term : Terminal C) (prod : Binary-products C)
β (let open Precategory C) (let open NbE term prod)
β β {X Y}
β (f g : Hom X Y)
β β¦ βXβ : Cartesian-ob X β¦ β¦ βYβ : Cartesian-ob Y β¦
β β¦ βfβ : Cartesian-hom f β¦ β¦ βgβ : Cartesian-hom g β¦
β nf (βobβ X) (βobβ Y) (βhomβ f) β‘ nf (βobβ X) (βobβ Y) (βhomβ g)
β β¦ βhomβ f β§ β‘ β¦ βhomβ g β§
solve-cartesian term prod {X = X} {Y = Y} f g p =
sym (NbE.sound term prod (βobβ X) (βobβ Y) (βhomβ f))
Β·Β· p
Β·Β· NbE.sound term prod (βobβ X) (βobβ Y) (βhomβ g)
macro
cartesian!
: β {o β} {C : Precategory o β}
β Terminal C
β Binary-products C
β Term β TC β€
cartesian! term prod hole =
withNormalisation false $ do
goal β infer-type hole >>= reduce
just (lhs , rhs) β get-boundary goal
where nothing β typeError $ strErr "Can't determine boundary: " β·
termErr goal β· []
βtermβ β quoteTC term
βprodβ β quoteTC prod
unify hole (def (quote solve-cartesian) (βtermβ vβ· βprodβ vβ· lhs vβ· rhs vβ· βreflβ vβ· []))