proarrow
Safe HaskellNone
LanguageGHC2024

Proarrow.Tools.HypergraphRewrite.Completion

Description

Critical overlaps and bounded Knuth–Bendix-style completion. Convex rewriting works with monogamous acyclic string diagrams and checks all maximal path contexts. Hypergraph completion uses the ordinary matcher with shared Frobenius wiring. Port completion checks schematic overlaps with captured wire sequences.

See Proarrow.Tools.KnuthBendix for completion of typed paths.

Synopsis

Overlaps and path contexts

data CriticalOverlap key s l where Source Github #

Two one-step reductions of a common diagram. The rule keys identify the two steps. The source and both reducts share an ordered boundary.

Constructors

CriticalOverlap :: forall {key} {s} {l} (as :: [s]) (bs :: [s]). key -> key -> ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l)) -> ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l)) -> ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l)) -> CriticalOverlap key s l 

criticalOverlaps :: (Eq l, DecidableIso s) => [(key, Rule s l)] -> Either String [CriticalOverlap key s l] Source Github #

Construct the overlapping pre-critical pairs of fixed, left-connected string-diagram rules. The sides must have acyclic, monogamous wiring, and every input of a left-hand side must reach every output. Rule instances with captures or parameters are outside this fragment. Parallel disjoint steps commute and are omitted. Boundaries are ordered by node number. See String Diagram Rewrite Theory III, §5.4.

hypergraphOverlaps :: (Eq l, DecidableIso s) => [(key, Rule s l)] -> Either String [CriticalOverlap key s l] Source Github #

Enumerate overlaps of fixed literal hypergraph rules under the ordinary matcher. Glue compatible box occurrences and their ports, then consider additional sort-preserving node identifications. Retain pairs that share a box, delete a node used by the other match, or identify two nodes of the other match in a replacement. Each witness exposes the nodes preserved by both steps as inputs, in node order.

data HypergraphConfluenceResult key s l Source Github #

The outcome of checking local confluence for single-step hypergraph rewriting. Success counts the overlaps checked. Failure returns a fork whose branches have disjoint sets of reducts, and a depth limit returns an unresolved fork.

checkHypergraphConfluence :: (Ord key, Eq l, DecidableIso s) => Maybe Natural -> [(key, Rule s l)] -> Either String (HypergraphConfluenceResult key s l) Source Github #

Check local confluence of fixed literal rules with unique keys. The depth bound applies separately to each side of an overlap. Use Nothing for an unbounded search, which can diverge on a nonterminating system.

Any two matches in a host restrict to their union. A host boundary port or an outside box can meet that union only at a node preserved by both steps. Those nodes are exactly the boundary exposed by hypergraphOverlaps. A joining derivation tracks each attachment through the boundary map, so it lifts back into the host. Even when it merges two boundary nodes, adding back the context leaves the current overlap graph embedded injectively: the context started with a distinct node for each attachment. Omitted pairs commute because their boxes and deletions do not interfere and neither replacement folds the other match. See the clipping and embedding argument in String Diagram Rewrite Theory III, §3.

This check concerns the single-step relation of rewritesCompiled. For a terminating system, local confluence implies confluence.

data PathContextResult key s l Source Github #

The result of testing all maximal path contexts. A failed join contains its extended source and both reducts. Left labels in that witness are the numbered formal path boxes, and Right labels are original generators. Failure concerns the extended signature that includes the formal boxes.

checkPathContexts :: (Eq l, DecidableIso s) => Maybe Natural -> [(key, Rule s l)] -> Either String (PathContextResult key s l) Source Github #

Check path joinability for fixed, left-linear string-diagram rules. Includes parallel pairs, which can interfere under convex matching. Both sides of each rule must be monogamous and acyclic. The optional bound is the maximum search depth from each reduct, for each path context. Use Nothing for a terminating rule set to exhaust the search. See String Diagram Rewrite Theory III, §5.5.

Variadic overlaps

portOverlapTemplates :: Eq l => [Rule () l] -> Either String [PortGraph () l] Source Github #

Enumerate schematic sources where two literal port rules share one or two boxes, or share interface wires that one rule can merge and the other needs to keep distinct. Each left port list allows at most one capture in each gap between numbered ports, including before the first port and after the last. Each capture must occur once on the left, and every numbered node must occur at a box port. Captures stand for external leg sequences.

A numbered port can align with another numbered port or occupy a capture on the other side. A deleted node must stay within the deleting match and cannot also occur in one of that match's captures. Preserved nodes can be identified even when they do not meet at a shared port. Numbered nodes preserved by both matches are exposed as the template's boundary. Retain a common symbolic sequence wherever both lists allow a capture. Different embeddings can give the same template. Pass the generated sources to checkPortTemplate to check their joins.

parameterPortOverlapTemplates :: (Traversable f, Eq (f ()), Eq (f a), Eq a) => (Int -> a) -> [Rule () (f a)] -> Either String [PortGraph () (f a)] Source Github #

Generate overlaps from rules built with parameterPortRule or parameterPortRuleWith. Shared boxes unify their label parameters. Literal constraints propagate to every occurrence of the same variable in its rule, and conflicting constraints discard the overlap. Supply a distinct symbolic value for each free variable class, disjoint from the literal values in the rules. Computed replacements can overlap when the template fixes every capture length and both matches instantiate successfully. The template specialises shared gaps and captures on unshared computed boxes to empty. Wire-only overlaps use the computed interface at those empty capture lengths.

data PortTemplateResult key l Source Github #

The result of checking all pairs of symbolic steps from a supplied template. Witnesses represent each named sequence by one extra input node after the template's explicit boundary, in first-occurrence order.

checkPortTemplate :: Eq l => Maybe Natural -> [(key, Rule () l)] -> PortGraph () l -> Either String (PortTemplateResult key l) Source Github #

Check the forks of a schematic source with unit-sorted wires. Fixed replacements may refer to explicit interface nodes, fresh numbered nodes or whole captures. Interface positions may be identified in a replacement. Label parameters are evaluated in the label type of the caller-supplied source.

Each sequence is represented by a distinct protected boundary node. A numbered port cannot match a sequence marker, even at the rule interface. Captures may consume entire markers, and fixed replacements copy their sequences in order. A computed replacement can be applied when every capture contains only numbered wires, so the lengths and individual ports are known. Consequently a symbolic join lifts to any substitution of the markers by port sequences (including empty sequences and aliases), provided those ports stay outside the template's numbered nodes. Any numbered nodes introduced along the derivation are fresh for the substitution. The result concerns the forks represented this way in the supplied template. Concrete instances may have additional matches, including computed replacements whose captures include sequence markers in the template.

data PortSymmetry Source Github #

Permitted permutations of a box's ports. Inputs and outputs permute independently.

checkPortTemplateWithSymmetry :: Eq l => (l -> PortSymmetry) -> Maybe Natural -> [(key, Rule () l)] -> PortGraph () l -> Either String (PortTemplateResult key l) Source Github #

Like checkPortTemplate, allowing permutations of the indicated ports when testing whether branches meet. Boundary order and port multiplicity are preserved. The search still applies the supplied rules to ordered ports and uses exact isomorphism to identify visited states.

checkPortTemplateWithScalars :: (Eq l, Eq m, Monoid m) => (l -> PortSymmetry) -> (l -> Maybe m) -> Maybe Natural -> [(key, Rule () l)] -> PortGraph () l -> Either String (PortTemplateResult key l) Source Github #

Compare reducts up to port symmetries and products of nullary scalars. The classifier selects scalar labels and supplies their values in a commutative monoid. A label with ports remains part of the graph. Scalar products affect only the comparison between branches.

Variadic completion

orientPortOverlap :: Eq l => (PortGraph () l -> PortGraph () l -> Ordering) -> PortGraph () l -> PortTemplateResult key l -> Either String (Rule () l) Source Github #

Turn an unjoinable symbolic fork into a literal port rule. The input template supplies the names and order of the sequences in the witness. The ordering must strictly decrease after every allowed capture substitution, including empty sequences. Orientation rejects a rule whose two sides are isomorphic with all captures empty. The resulting rule uses the labels from the witness. The rule can factor through one port per left interface node if the right branch preserves every left identification and the factored rule still decreases. The right branch may identify additional positions. Adjacent captures that occur once and stay together in the same order can become a single capture.

orientParameterPortOverlap :: (Traversable f, Typeable f, Eq (f ()), Eq (f symbolic), Eq a) => (symbolic -> Parameter a) -> (PortGraph () (f symbolic) -> PortGraph () (f symbolic) -> Ordering) -> PortGraph () (f symbolic) -> PortTemplateResult key (f symbolic) -> Either String (Rule () (f a)) Source Github #

Orient a symbolic fork into a parameterized port rule. The caller converts values from the symbolic algebra into label parameters. Variables must use the same names in both branches. The oriented left side can only contain variables and literals, and the right side can compute expressions from the variables bound on the left. The comparison follows the ordering requirements of completeParameterPorts.

data PortCompletion l Source Github #

Bounded completion of port rules on the generated templates. The returned rules are in input and discovery order.

Constructors

PortComplete [Rule () l] (CompiledRules Int () l) 
PortIncomplete [Rule () l] (PortCompletionStop l) 

data PortCompletionStop l Source Github #

The unresolved template and its fork. Keys index the returned rules.

completePorts :: Eq l => Natural -> Natural -> (PortGraph () l -> PortGraph () l -> Ordering) -> [Rule () l] -> Either String (PortCompletion l) Source Github #

Complete fixed literal port rules within bounds on the number of learned rules and the search depth from either branch. The caller supplies a well-founded comparison preserved by contexts. Seed and learned rules must strictly decrease after every allowed capture substitution, including empty sequences. Each learned rule orients the simplified branches of a fork and is rejected if setting all captures to empty makes it a no-op. Success covers the templates generated for the final rules under the symbolic semantics of checkPortTemplate.

completeParameterPorts :: (Traversable f, Typeable f, Eq (f ()), Eq (f a), Eq a) => (Int -> a) -> (a -> Parameter a) -> Natural -> Natural -> (PortGraph () (f a) -> PortGraph () (f a) -> Ordering) -> [Rule () (f a)] -> Either String (PortCompletion (f a)) Source Github #

Like completePorts, with label parameters interpreted in a symbolic algebra. The first argument supplies fresh variables for overlap generation. The second reifies symbolic values into parameters that evaluate in the same algebra. It must preserve variable names, literals and expressions between the two branches. The learned rules are used in subsequent rounds, and the compiled result rewrites labels in that algebra. The comparison must also be preserved under substitution of label parameters.

completeParameterPortsFor :: (Traversable f, Typeable f, Eq (f ()), Eq (f symbolic), Eq symbolic, Eq a) => (Int -> symbolic) -> (symbolic -> Parameter symbolic) -> (symbolic -> Parameter a) -> Natural -> Natural -> (PortGraph () (f symbolic) -> PortGraph () (f symbolic) -> Ordering) -> [Rule () (f symbolic)] -> Either String (PortCompletion (f symbolic), [Rule () (f a)]) Source Github #

Like completeParameterPorts, and also emit the learned rules for a concrete parameter type. The two reifiers must interpret the same expressions in their respective algebras. The emitted rules are in discovery order, including those learned before a bounded stop. Append these to the corresponding concrete seed rules to use the same priorities.

completeParameterPortsForWithScalars :: (Traversable f, Typeable f, Eq (f ()), Eq (f symbolic), Eq symbolic, Eq a, Eq m, Monoid m) => (f symbolic -> PortSymmetry) -> (f symbolic -> Maybe m) -> (Int -> symbolic) -> (symbolic -> Parameter symbolic) -> (symbolic -> Parameter a) -> Natural -> Natural -> (PortGraph () (f symbolic) -> PortGraph () (f symbolic) -> Ordering) -> [Rule () (f symbolic)] -> Either String (PortCompletion (f symbolic), [Rule () (f a)]) Source Github #

Like completeParameterPortsFor, comparing branches using the port symmetries and scalar products of checkPortTemplateWithScalars. The declared symmetries can also bring captures together on a replacement box and must hold under parameter substitution.

completeParameterPortsReparameterised :: (Traversable f, Typeable f, Eq (f ()), Eq (f symbolic), Eq symbolic, Eq a, Eq m, Monoid m) => (f symbolic -> PortSymmetry) -> (f () -> PortSymmetry) -> (f symbolic -> Maybe m) -> ([symbolic] -> [symbolic] -> symbolic -> symbolic) -> (Int -> symbolic) -> (symbolic -> Parameter symbolic) -> (symbolic -> Parameter a) -> Natural -> Natural -> (PortGraph () (f symbolic) -> PortGraph () (f symbolic) -> Ordering) -> [Rule () (f symbolic)] -> Either String (PortCompletion (f symbolic), [Rule () (f a)]) Source Github #

Like completeParameterPortsForWithScalars, with an optional algebraic change of variables before reifying a learned rule. The callback sees the parameter values of the oriented left and right branches. It returns a map applied to both branches, for both symbolic and concrete rule emission. The caller must ensure the new equation holds for all its parameter values. The second symmetry policy selects fixed port lists to expand for matching new rules, as in parameterPortVariants. The order must respect these symmetries. One learned rule consumes one budget slot and emits all its ordered variants.

Convex rewriting and completion

withFixedRule :: Rule s l -> (forall (as :: [s]) (bs :: [s]). ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l)) -> ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l)) -> r) -> Either String r Source Github #

Inspect the parallel sides of a fixed string-diagram rule. Both sides must have monogamous acyclic wiring, as in compileConvexRules.

data ConvexRules s l Source Github #

Validated fixed rules for convex string-diagram rewriting.

compileConvexRules :: [Rule s l] -> Either String (ConvexRules s l) Source Github #

Validate fixed rules with monogamous acyclic sides. Rule order determines which match normaliseConvex takes when several are available.

normaliseConvex :: forall {s} l (as :: [s]) (bs :: [s]). (Eq l, DecidableIso s) => RewriteLimits -> ConvexRules s l -> ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l)) -> Either String (NormalisationResult ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l))) Source Github #

Normalise a monogamous acyclic string diagram using one convex match at a time. Each successful step counts as one rewrite and one batch. Use unlimitedRewrites when the rules are known to terminate, and checkPathContexts to check whether different rewrite choices rejoin.

data ConvexCompletion s l Source Github #

The rules returned by bounded convex completion. Supplied rules come before learned rules. On success, redundant rules may be removed and retained right sides may be simplified. All path contexts of the resulting system rejoin.

data ConvexCompletionStop s l Source Github #

Why completion stopped. Rule keys in witnesses are zero-based positions in the returned rule list. Left labels denote formal path boxes.

completeConvex :: forall s l. (Eq l, DecidableIso s) => Natural -> Natural -> (forall (as :: [s]) (bs :: [s]). ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l)) -> ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l)) -> Ordering) -> [Rule s l] -> Either String (ConvexCompletion s l) Source Github #

Knuth–Bendix-style completion of a fixed string-diagram presentation, bounded by the number of new rules and the search depth from each reduct. On a failed join in the empty context, orient the two deterministic normal forms using the supplied comparison. The caller supplies a well-founded order invariant under isomorphism and preserved by contexts, and ensures the initial rules decrease in that order. Failures in formal path contexts are returned with their witnesses. After success, use the other rules to remove redundant rules and simplify right sides. Keep each change only when the resulting system passes the path-context check within the same depth bound.

Hypergraph completion

data HypergraphCompletion s l Source Github #

The result of checking and completing the overlaps enumerated by hypergraphOverlaps. The rules are listed in input and discovery order, with redundant rules removed and right sides simplified on success.

data HypergraphCompletionStop s l Source Github #

The outstanding overlap when bounded hypergraph completion stops. Rule keys are zero-based positions in the returned rule list.

completeHypergraph :: forall s l. (Eq l, DecidableIso s) => Natural -> Natural -> (forall (as :: [s]) (bs :: [s]). ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l)) -> ((Wires as :: OPENHG s l) ~> (Wires bs :: OPENHG s l)) -> Ordering) -> [Rule s l] -> Either String (HypergraphCompletion s l) Source Github #

Bounded Knuth–Bendix-style completion of fixed literal hypergraph rules. The two bounds limit the number of learned rules and the search depth from each reduct. As with completeConvex, the caller supplies a well-founded comparison that decreases on rules and is preserved by contexts. Success establishes local confluence for the resulting single-step system by the same check as checkHypergraphConfluence. After a successful join check, remove redundant rules and simplify right sides using the other rules. Each change must pass the overlap check within the same depth bound. A learned rule with repeated left boundary nodes uses one port per shared node when both sides identify the same ports and the factored rule still decreases. Otherwise completion returns HypergraphUnorientable with the original overlap witness.