proarrow
Safe HaskellNone
LanguageGHC2024

Proarrow.Tools.KnuthBendix

Description

Knuth–Bendix completion for typed paths over fixed generators. Associativity is built into Paths spines and identities are empty paths. Rules contain no arrow or object metavariables. Overlaps are the ordinary inclusion and crossing overlaps of words, restricted to composable paths.

complete numbers generators by first appearance in the presentation. completeWithOrder accepts ordered keys whose equality must agree with EqGen. Shortlex makes every rule decrease, including in surrounding paths. Complete means all nontrivial critical pairs have been processed; under these assumptions its system terminates, is confluent, and presents the input equations. Incomplete retains the outstanding obligations. Rules are interreduced as they are added. Collapsed left sides return to the equation queue, and surviving right sides are normalised. Overlap candidates are indexed by first generators and left-side occurrences. Successful completion compiles a matcher used by normalise and rewrite; rules still exposes the completed rules for inspection.

This is the string-completion procedure described in the KBMAG manual, with typed generators supplying endpoint equalities during matching.

Synopsis

Documentation

type Equation (p :: CAT k) = ProEquation (Paths :: PATHS p -> PATHS p -> Type) Source Github #

data ProEquation (p :: j +-> k) where Source Github #

Two parallel elements of a profunctor claimed to be equal, or an equation between arrows of its codomain (InK) or domain (InJ).

Constructors

(:=:) :: forall {j} {k} (p :: j +-> k) (a :: k) (b :: j). p a b -> p a b -> ProEquation p infix 1 

Instances

Instances details
ArrowEquation j (ProEquation p) Source Github # 
Instance details

Defined in Proarrow.Tools.Laws

ArrowEquation k (ProEquation p) Source Github # 
Instance details

Defined in Proarrow.Tools.Laws

data Rule (p :: CAT k) Source Github #

withRule :: forall {k} (p :: CAT k) r. Rule p -> (forall (a :: PATHS p) (b :: PATHS p). Paths a b -> Paths a b -> r) -> r Source Github #

data System (p :: CAT k) Source Github #

rules :: forall {k} (p :: CAT k). System p -> [Rule p] Source Github #

normalise :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p). System p -> Paths a b -> Paths a b Source Github #

Normalise a raw path from its input end, reusing each already-normal tail. Every step strictly decreases shortlex. This bypasses the user's Rewrite instance; completion inputs should use PNil and PCons to avoid invoking that instance while building its rules. Completed systems use their compiled matcher; incomplete systems scan rules.

rewrite :: forall {k} (a :: k) (b :: k) p (i :: PATHS p). (Ob a, Ob b) => System p -> p a b -> Paths i ('PTH a :: PATHS p) -> Paths i ('PTH b :: PATHS p) Source Github #

Prepend one generator to an already-normal tail, checking only the new boundary. A replacement is inserted incrementally, so redexes created within it or across its boundary are also reduced. Suitable for implementing Rewrite instances.

equivalent :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p). System p -> Paths a b -> Paths a b -> Bool Source Github #

Equality of normal forms. This decides the input equational theory only after Complete; with a partial system it establishes equality when True, but False can mean that completion still needs more rules.

data Compiled (p :: CAT k) Source Github #

A fixed system compiled into a prefix trie with integer dispatch. Complete systems already use a compiled matcher through normalise and rewrite. compile also permits compiling an incomplete system explicitly.

compile :: forall {k} key p. Ord key => (forall (a :: k) (b :: k). p a b -> key) -> System p -> Compiled p Source Github #

Compile a snapshot of the rules. Ordered keys are used only for dispatch; equal keys must agree with EqGen on rule and future input generators. The result has the same normal forms as normalise. Compiling an incomplete system does not establish confluence or completeness.

>>> import Proarrow.Category.Instance.Unit (Unit(..))
>>> let step = PCons Unit PNil
>>> let run s = pathLength (normaliseCompiled (compile (const ()) s) (PCons Unit step))
>>> case complete (Limits 4 20) [step :=: PNil] of { Complete s -> run s; Incomplete{} -> -1 }
0

normaliseCompiled :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p). Compiled p -> Paths a b -> Paths a b Source Github #

Normalise with a compiled prefix matcher, bypassing the user's Rewrite instance as normalise does.

rewriteCompiled :: forall {k} (a :: k) (b :: k) p (i :: PATHS p). (Ob a, Ob b) => Compiled p -> p a b -> Paths i ('PTH a :: PATHS p) -> Paths i ('PTH b :: PATHS p) Source Github #

The compiled counterpart of rewrite, with the same already-normal-tail precondition. Suitable for a Rewrite instance.

criticalPairs :: forall {k} (p :: CAT k). System p -> [Equation p] Source Github #

All nontrivial, well-typed inclusion and crossing overlaps, including self-overlaps. Symmetric copies can occur. Empty overlaps are independent rewrites and are omitted; empty left sides cannot arise under shortlex. Enumeration order is unspecified.

data Limits Source Github #

Constructors

Limits 

Fields

Instances

Instances details
Show Limits Source Github # 
Instance details

Defined in Proarrow.Tools.KnuthBendix

Eq Limits Source Github # 
Instance details

Defined in Proarrow.Tools.KnuthBendix

data Limit Source Github #

Constructors

RuleLimit 
EquationLimit 

Instances

Instances details
Show Limit Source Github # 
Instance details

Defined in Proarrow.Tools.KnuthBendix

Eq Limit Source Github # 
Instance details

Defined in Proarrow.Tools.KnuthBendix

Methods

(==) :: Limit -> Limit -> Bool Github #

(/=) :: Limit -> Limit -> Bool Github #

data Completion (p :: CAT k) Source Github #

Constructors

Complete (System p) 
Incomplete Limit (System p) [Equation p] 

complete :: forall {k} (p :: CAT k). EqGen p => Limits -> [Equation p] -> Completion p Source Github #

Complete using shortlex, numbering generators by first appearance on the left and then right of each input equation, in spine (display) order. Only typed equality is needed; no order or runtime object reflection is required. Reordering the presentation can change the chosen normal forms and the amount of completion work. Normalisation also accepts generators absent from the presentation: they are compared using EqGen, not alphabet ranks.

>>> import Proarrow.Category.Instance.Unit (Unit(..))
>>> let step = PCons Unit PNil
>>> case complete (Limits 4 20) [step :=: PNil] of { Complete s -> pathLength (normalise s (PCons Unit step)); Incomplete{} -> -1 }
0

completeWithOrder :: (EqGen p, Ord key) => (forall (a :: k) (b :: k). p a b -> key) -> Limits -> [Equation p] -> Completion p Source Github #

Complete with ordered generator keys. Equal keys must mean that eqGen succeeds, including equality of both endpoints. Shorter paths always come first; equal lengths compare keys from the output end of the spine. Both limits include the initial presentation; maxEquations counts processed obligations, including ones that already join. A limit result retains its partial rules and all unprocessed equations, including collapsed rules. The rule limit applies to the active, interreduced system. Successful completion compiles a matcher whose alphabet lookup also uses the supplied keys on future input generators, consistently with EqGen.