module Cat.Solver where
Solver for categoriesπ
This module is split pretty cleanly into two halves: the first half implements an algorithm for reducing, in a systematic way, problems involving associativity and identity of composition in a precategory. The latter half, significantly more cursed, uses this infrastructure to automatically solve equality goals of this form.
With a precategory in hand, we start by defining a language of composition.
module NbE (Cat : Precategory o h) where open Precategory Cat
data Expr : Ob β Ob β Type (o β h) where `id : Expr A A _`β_ : Expr B C β Expr A B β Expr A C _β : Hom A B β Expr A B infixr 40 _`β_ infix 50 _β
A term of type Expr
represents, in a symbolic
way, a composite of morphisms in our category
What this means is that, while
is some unknowable inhabitant of Hom
,
represents an inhabitant of Hom
which is known to be a composition of (the trees that
represent)
and
We can now define βtwoβ ways of computing the morphism that an Expr
represents. The first is a
straightforward embed
ding:
embed : Expr A B β Hom A B embed `id = id embed (f β) = f embed (f `β g) = embed f β embed g
The second computation is a bit less obvious. If youβre a programmer, it should be familiar under the name βcontinuation passing styleβ. Categorically, it can be seen as embedding into the presheaf category of In either case, the difference is that instead of computing a single morphism, we compute a transformation of hom-spaces:
eval : Expr B C β Hom A B β Hom A C eval `id f = f eval (f β) g = f β g eval (f `β g) h = eval f (eval g h) nf : Expr A B β Hom A B nf e = eval e id
Working this out in a back-of-the-envelope calculation, one sees that
eval f id
should compute the same morphism as
embed f
. Indeed, thatβs the case! Since embed
is the βintended
semanticsβ, and eval
is an βoptimised
evaluatorβ, we call this result soundness. We can prove it by
induction on the expression, by first generalising over id
:
eval-sound-k : (e : Expr B C) (f : Hom A B) β eval e f β‘ β¦ e β§ β f eval-sound-k `id f = sym (idl _) -- f β‘ id β f eval-sound-k (f `β g) h = eval f (eval g h) β‘β¨ eval-sound-k f _ β©β‘ embed f β eval g h β‘β¨ ap (embed f β_) (eval-sound-k g _) β©β‘ embed f β embed g β h β‘β¨ assoc _ _ _ β©β‘ (embed f β embed g) β h β eval-sound-k (x β) f = refl -- x β f β‘ x β f eval-sound : (e : Expr A B) β nf e β‘ β¦ e β§ eval-sound e = eval-sound-k e id β idr _
We now have a general theorem for solving associativity and identity problems! If two expressions compute the same transformation of hom-sets, then they represent the same morphism.
abstract solve : (f g : Expr A B) β nf f β‘ nf g β β¦ f β§ β‘ β¦ g β§ solve f g p = sym (eval-sound f) Β·Β· p Β·Β· (eval-sound g) solve-filler : (f g : Expr A B) β (p : nf f β‘ nf g) β Square (eval-sound f) p (solve f g p) (eval-sound g) solve-filler f g p j i = Β·Β·-filler (sym (eval-sound f)) p (eval-sound g) j i
The cursed partπ
module Reflection where pattern category-args xs = _ hmβ· _ hmβ· _ vβ· xs pattern βidβ = def (quote Precategory.id) (category-args (_ hβ· [])) pattern βββ f g = def (quote Precategory._β_) (category-args (_ hβ· _ hβ· _ hβ· f vβ· g vβ· [])) mk-category-args : Term β List (Arg Term) β List (Arg Term) mk-category-args cat xs = unknown hβ· unknown hβ· cat vβ· xs βsolveβ : Term β Term β Term β Term βsolveβ cat lhs rhs = def (quote NbE.solve) (mk-category-args cat $ infer-hidden 2 $ lhs vβ· rhs vβ· def (quote refl) [] vβ· []) βnfβ : Term β Term β Term βnfβ cat e = def (quote NbE.nf) (mk-category-args cat $ infer-hidden 2 $ e vβ· []) build-expr : Term β Term build-expr βidβ = con (quote NbE.`id) [] build-expr (βββ f g) = con (quote NbE._`β_) (build-expr f vβ· build-expr g vβ· [] ) build-expr f = con (quote NbE._β) (f vβ· []) dont-reduce : List Name dont-reduce = quote Precategory.id β· quote Precategory._β_ β· [] cat-solver : Term β SimpleSolver cat-solver cat .SimpleSolver.dont-reduce = dont-reduce cat-solver cat .SimpleSolver.build-expr tm = pure $ build-expr tm cat-solver cat .SimpleSolver.invoke-solver = βsolveβ cat cat-solver cat .SimpleSolver.invoke-normaliser = βnfβ cat repr-macro : Term β Term β Term β TC β€ repr-macro cat f _ = mk-simple-repr (cat-solver cat) f simplify-macro : Term β Term β Term β TC β€ simplify-macro cat f hole = mk-simple-normalise (cat-solver cat) f hole solve-macro : Term β Term β TC β€ solve-macro cat hole = mk-simple-solver (cat-solver cat) hole macro repr-cat! : Term β Term β Term β TC β€ repr-cat! cat f = Reflection.repr-macro cat f simpl-cat! : Term β Term β Term β TC β€ simpl-cat! cat f = Reflection.simplify-macro cat f cat! : Term β Term β TC β€ cat! = Reflection.solve-macro
Demoπ
As a quick demonstration (and sanity check/future proofing/integration testing/what have you):
module _ (C : Precategory o h) where private module C = Precategory C variable A B : C.Ob a b c d : C.Hom A B test : a C.β (b C.β (c C.β C.id) C.β C.id C.β (d C.β C.id)) β‘ a C.β b C.β c C.β d test = cat! C