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
: Expr A A
`id _`∘_ : 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:
: Expr A B → Hom A B
embed = id
embed `id (f ↑) = f
embed (f `∘ g) = embed f ∘ embed g embed
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:
: Expr B C → Hom A B → Hom A C
eval = f
eval `id f (f ↑) g = f ∘ g
eval (f `∘ g) h = eval f (eval g h)
eval
: Expr A B → Hom A B
nf = eval e id nf e
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
:
: (e : Expr B C) (f : Hom A B) → eval e f ≡ embed e ∘ f
eval-sound-k = sym (idl _) -- f ≡ id ∘ f
eval-sound-k `id f (f `∘ g) h =
eval-sound-k (eval g h) ≡⟨ eval-sound-k f _ ⟩
eval f (embed f ∘_) (eval-sound-k g _) ⟩
embed f ∘ eval g h ≡⟨ ap _ _ _ ⟩
embed f ∘ embed g ∘ h ≡⟨ assoc (embed f ∘ embed g) ∘ h ∎
(x ↑) f = refl -- x ∘ f ≡ x ∘ f
eval-sound-k
: (e : Expr A B) → nf e ≡ embed e
eval-sound = eval-sound-k e id ∙ idr _ eval-sound e
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
: (f g : Expr A B) → nf f ≡ nf g → embed f ≡ embed g
solve = sym (eval-sound f) ·· p ·· (eval-sound g)
solve f g p
: (f g : Expr A B) → (p : nf f ≡ nf g) → Square (eval-sound f) p (solve f g p) (eval-sound g)
solve-filler = ··-filler (sym (eval-sound f)) p (eval-sound g) j i solve-filler f g p j i
The cursed part🔗
module Reflection where
pattern category-args xs =
_ hm∷ _ hm∷ _ v∷ xs
pattern “id” =
(quote Precategory.id) (category-args (_ h∷ []))
def
pattern “∘” f g =
(quote Precategory._∘_) (category-args (_ h∷ _ h∷ _ h∷ f v∷ g v∷ []))
def
: Term → List (Arg Term) → List (Arg Term)
mk-category-args = unknown h∷ unknown h∷ cat v∷ xs
mk-category-args cat xs
: Term → Term → Term → Term
“solve” = def (quote NbE.solve) (mk-category-args cat $ infer-hidden 2 $ lhs v∷ rhs v∷ def (quote refl) [] v∷ [])
“solve” cat lhs rhs
: Term → Term → Term
“nf” = def (quote NbE.nf) (mk-category-args cat $ infer-hidden 2 $ e v∷ [])
“nf” cat e
: Term → Term
build-expr = con (quote NbE.`id) []
build-expr “id” (“∘” f g) = con (quote NbE._`∘_) (build-expr f v∷ build-expr g v∷ [] )
build-expr = con (quote NbE._↑) (f v∷ [])
build-expr f
: List Name
dont-reduce = quote Precategory.id ∷ quote Precategory._∘_ ∷ []
dont-reduce
: Term → SimpleSolver
cat-solver .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
cat-solver cat
: Term → Term → Term → TC ⊤
repr-macro _ =
repr-macro cat f (cat-solver cat) f
mk-simple-repr
: Term → Term → Term → TC ⊤
simplify-macro =
simplify-macro cat f hole (cat-solver cat) f hole
mk-simple-normalise
: Term → Term → TC ⊤
solve-macro =
solve-macro cat hole (cat-solver cat) hole
mk-simple-solver
macro
: Term → Term → Term → TC ⊤
repr-cat! = Reflection.repr-macro cat f
repr-cat! cat f
: Term → Term → Term → TC ⊤
simpl-cat! = Reflection.simplify-macro cat f
simpl-cat! cat f
: Term → Term → TC ⊤
cat! = Reflection.solve-macro cat!
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
: C.Ob
A B : C.Hom A B
a b c d
: a C.∘ (b C.∘ (c C.∘ C.id) C.∘ C.id C.∘ (d C.∘ C.id))
test .∘ b C.∘ c C.∘ d
≡ a C= cat! C test