{-# LANGUAGE AllowAmbiguousTypes #-}
module Proarrow.Tools.KnuthBendix
( Equation
, ProEquation ((:=:))
, Rule
, withRule
, System
, rules
, normalise
, rewrite
, equivalent
, Compiled
, compile
, normaliseCompiled
, rewriteCompiled
, criticalPairs
, Limits (..)
, Limit (..)
, Completion (..)
, complete
, completeWithOrder
) where
import Data.IntMap.Strict qualified as IM
import Data.IntSet qualified as IS
import Data.Kind (Type)
import Data.List (findIndex, sort)
import Data.Maybe (fromMaybe, isJust, listToMaybe, mapMaybe)
import Data.Type.Equality ((:~:) (..))
import Data.Vector qualified as V
import Prelude
import Proarrow.Category.Instance.Paths (EqGen (..), PATHS (..), Paths (..), pathLength)
import Proarrow.Core (CAT, CategoryOf (Ob))
import Proarrow.Tools.Laws (ProEquation (..))
import Proarrow.Tools.Laws qualified as Laws
type Equation :: forall {k}. CAT k -> Type
type Equation p = ProEquation (Paths :: CAT (PATHS p))
type Rule :: forall {k}. CAT k -> Type
data Rule p where
Rule :: Paths (a :: PATHS p) b -> Paths a b -> Rule p
withRule :: Rule p -> (forall a b. Paths (a :: PATHS p) b -> Paths a b -> r) -> r
withRule :: forall {k} (p :: CAT k) r.
Rule p
-> (forall (a :: PATHS p) (b :: PATHS p).
Paths a b -> Paths a b -> r)
-> r
withRule (Rule Paths a b
l Paths a b
r) forall (a :: PATHS p) (b :: PATHS p). Paths a b -> Paths a b -> r
f = Paths a b -> Paths a b -> r
forall (a :: PATHS p) (b :: PATHS p). Paths a b -> Paths a b -> r
f Paths a b
l Paths a b
r
type System :: forall {k}. CAT k -> Type
data System p where
System :: (EqGen p) => [Rule p] -> Maybe (Compiled p) -> System p
rules :: System p -> [Rule p]
rules :: forall {k} (p :: CAT k). System p -> [Rule p]
rules (System [Rule p]
rs Maybe (Compiled p)
_) = [Rule p]
rs
append :: Paths a b -> Paths x a -> Paths x b
append :: forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
Paths a b -> Paths x a -> Paths x b
append Paths a b
PNil Paths x a
q = Paths x a
Paths x b
q
append (PCons p a1 b1
p Paths a (PTH a1)
more) Paths x a
q = p a1 b1 -> Paths x (PTH a1) -> Paths x (PTH b1)
forall {k} (a1 :: k) (b1 :: k) (p :: CAT k) (a :: PATHS p).
(Ob a1, Ob b1) =>
p a1 b1 -> Paths a (PTH a1) -> Paths a (PTH b1)
PCons p a1 b1
p (Paths a (PTH a1) -> Paths x a -> Paths x (PTH a1)
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
Paths a b -> Paths x a -> Paths x b
append Paths a (PTH a1)
more Paths x a
q)
sourceIdentity :: Paths a b -> Paths a a
sourceIdentity :: forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p).
Paths a b -> Paths a a
sourceIdentity Paths a b
PNil = Paths a a
Paths (PTH a1) (PTH a1)
forall {k} {p :: CAT k} (a1 :: k). Ob a1 => Paths (PTH a1) (PTH a1)
PNil
sourceIdentity (PCons p a1 b1
_ Paths a (PTH a1)
more) = Paths a (PTH a1) -> Paths a a
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p).
Paths a b -> Paths a a
sourceIdentity Paths a (PTH a1)
more
comparePaths
:: forall {k} (p :: CAT k) key a b c d
. (Ord key) => (forall x y. p x y -> key) -> Paths (a :: PATHS p) b -> Paths (c :: PATHS p) d -> Ordering
comparePaths :: forall {k} (p :: CAT k) key (a :: PATHS p) (b :: PATHS p)
(c :: PATHS p) (d :: PATHS p).
Ord key =>
(forall (x :: k) (y :: k). p x y -> key)
-> Paths a b -> Paths c d -> Ordering
comparePaths forall (x :: k) (y :: k). p x y -> key
rank Paths a b
a Paths c d
b = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Paths a b -> Int
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p).
Paths a b -> Int
pathLength Paths a b
a) (Paths c d -> Int
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p).
Paths a b -> Int
pathLength Paths c d
b) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> [key] -> [key] -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Paths a b -> [key]
forall (x :: PATHS p) (y :: PATHS p). Paths x y -> [key]
ranks Paths a b
a) (Paths c d -> [key]
forall (x :: PATHS p) (y :: PATHS p). Paths x y -> [key]
ranks Paths c d
b)
where
ranks :: Paths (x :: PATHS p) y -> [key]
ranks :: forall (x :: PATHS p) (y :: PATHS p). Paths x y -> [key]
ranks Paths x y
PNil = []
ranks (PCons p a1 b1
p Paths x (PTH a1)
more) = p a1 b1 -> key
forall (x :: k) (y :: k). p x y -> key
rank p a1 b1
p key -> [key] -> [key]
forall a. a -> [a] -> [a]
: Paths x (PTH a1) -> [key]
forall (x :: PATHS p) (y :: PATHS p). Paths x y -> [key]
ranks Paths x (PTH a1)
more
strip :: (EqGen p) => Paths (a :: PATHS p) b -> Paths x b -> Maybe (Paths x a)
strip :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
EqGen p =>
Paths a b -> Paths x b -> Maybe (Paths x a)
strip Paths a b
PNil Paths x b
rest = Paths x a -> Maybe (Paths x a)
forall a. a -> Maybe a
Just Paths x a
Paths x b
rest
strip (PCons p a1 b1
p Paths a (PTH a1)
more) (PCons p a1 b1
q Paths x (PTH a1)
rest) = case p a1 b1 -> p a1 b1 -> Maybe ('(a1, b1) :~: '(a1, b1))
forall (a :: k) (b :: k) (c :: k) (d :: k).
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
forall k (p :: CAT k) (a :: k) (b :: k) (c :: k) (d :: k).
EqGen p =>
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
eqGen p a1 b1
p p a1 b1
q of
Just '(a1, b1) :~: '(a1, b1)
Refl -> Paths a (PTH a1) -> Paths x (PTH a1) -> Maybe (Paths x a)
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
EqGen p =>
Paths a b -> Paths x b -> Maybe (Paths x a)
strip Paths a (PTH a1)
more Paths x (PTH a1)
Paths x (PTH a1)
rest
Maybe ('(a1, b1) :~: '(a1, b1))
_ -> Maybe (Paths x a)
forall a. Maybe a
Nothing
strip Paths a b
_ Paths x b
PNil = Maybe (Paths x a)
forall a. Maybe a
Nothing
normaliseOnto :: System p -> Paths (a :: PATHS p) b -> Paths x a -> Paths x b
normaliseOnto :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
System p -> Paths a b -> Paths x a -> Paths x b
normaliseOnto System p
_ Paths a b
PNil Paths x a
rest = Paths x a
Paths x b
rest
normaliseOnto System p
system (PCons p a1 b1
p Paths a (PTH a1)
more) Paths x a
rest = System p -> p a1 b1 -> Paths x (PTH a1) -> Paths x (PTH b1)
forall {k} (a :: k) (b :: k) (p :: CAT k) (i :: PATHS p).
(Ob a, Ob b) =>
System p -> p a b -> Paths i (PTH a) -> Paths i (PTH b)
rewrite System p
system p a1 b1
p (System p -> Paths a (PTH a1) -> Paths x a -> Paths x (PTH a1)
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
System p -> Paths a b -> Paths x a -> Paths x b
normaliseOnto System p
system Paths a (PTH a1)
more Paths x a
rest)
rewriteRoot
:: (EqGen p)
=> (forall x y z. Paths (x :: PATHS p) y -> Paths z x -> Paths z y)
-> Rule p
-> Paths (a :: PATHS p) b
-> Maybe (Paths a b)
rewriteRoot :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
EqGen p =>
(forall (x :: PATHS p) (y :: PATHS p) (z :: PATHS p).
Paths x y -> Paths z x -> Paths z y)
-> Rule p -> Paths a b -> Maybe (Paths a b)
rewriteRoot forall (x :: PATHS p) (y :: PATHS p) (z :: PATHS p).
Paths x y -> Paths z x -> Paths z y
insert (Rule (PCons p a1 b1
p Paths a (PTH a1)
more) Paths a b
rhs) (PCons p a1 b1
q Paths a (PTH a1)
rest) = case p a1 b1 -> p a1 b1 -> Maybe ('(a1, b1) :~: '(a1, b1))
forall (a :: k) (b :: k) (c :: k) (d :: k).
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
forall k (p :: CAT k) (a :: k) (b :: k) (c :: k) (d :: k).
EqGen p =>
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
eqGen p a1 b1
p p a1 b1
q of
Just '(a1, b1) :~: '(a1, b1)
Refl -> Paths a b -> Paths a a -> Paths a b
forall (x :: PATHS p) (y :: PATHS p) (z :: PATHS p).
Paths x y -> Paths z x -> Paths z y
insert Paths a b
Paths a b
rhs (Paths a a -> Paths a b) -> Maybe (Paths a a) -> Maybe (Paths a b)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Paths a (PTH a1) -> Paths a (PTH a1) -> Maybe (Paths a a)
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
EqGen p =>
Paths a b -> Paths x b -> Maybe (Paths x a)
strip Paths a (PTH a1)
more Paths a (PTH a1)
Paths a (PTH a1)
rest
Maybe ('(a1, b1) :~: '(a1, b1))
_ -> Maybe (Paths a b)
forall a. Maybe a
Nothing
rewriteRoot forall (x :: PATHS p) (y :: PATHS p) (z :: PATHS p).
Paths x y -> Paths z x -> Paths z y
_ Rule p
_ Paths a b
_ = Maybe (Paths a b)
forall a. Maybe a
Nothing
rewrite
:: (Ob a, Ob b)
=> System p -> p a b -> Paths i (PTH a :: PATHS p) -> Paths i (PTH b)
rewrite :: forall {k} (a :: k) (b :: k) (p :: CAT k) (i :: PATHS p).
(Ob a, Ob b) =>
System p -> p a b -> Paths i (PTH a) -> Paths i (PTH b)
rewrite (System [Rule p]
_ (Just Compiled p
compiled)) p a b
p Paths i (PTH a)
more = Compiled p -> p a b -> Paths i (PTH a) -> Paths i (PTH b)
forall {k} (a :: k) (b :: k) (p :: CAT k) (i :: PATHS p).
(Ob a, Ob b) =>
Compiled p -> p a b -> Paths i (PTH a) -> Paths i (PTH b)
rewriteCompiled Compiled p
compiled p a b
p Paths i (PTH a)
more
rewrite system :: System p
system@(System [Rule p]
rs Maybe (Compiled p)
Nothing) p a b
p Paths i (PTH a)
more =
let path :: Paths i (PTH b)
path = p a b -> Paths i (PTH a) -> Paths i (PTH b)
forall {k} (a1 :: k) (b1 :: k) (p :: CAT k) (a :: PATHS p).
(Ob a1, Ob b1) =>
p a1 b1 -> Paths a (PTH a1) -> Paths a (PTH b1)
PCons p a b
p Paths i (PTH a)
more
in Paths i (PTH b) -> Maybe (Paths i (PTH b)) -> Paths i (PTH b)
forall a. a -> Maybe a -> a
fromMaybe Paths i (PTH b)
path ([Paths i (PTH b)] -> Maybe (Paths i (PTH b))
forall a. [a] -> Maybe a
listToMaybe ((Rule p -> Maybe (Paths i (PTH b)))
-> [Rule p] -> [Paths i (PTH b)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (\Rule p
r -> (forall (x :: PATHS p) (y :: PATHS p) (z :: PATHS p).
Paths x y -> Paths z x -> Paths z y)
-> Rule p -> Paths i (PTH b) -> Maybe (Paths i (PTH b))
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
EqGen p =>
(forall (x :: PATHS p) (y :: PATHS p) (z :: PATHS p).
Paths x y -> Paths z x -> Paths z y)
-> Rule p -> Paths a b -> Maybe (Paths a b)
rewriteRoot (System p -> Paths x y -> Paths z x -> Paths z y
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
System p -> Paths a b -> Paths x a -> Paths x b
normaliseOnto System p
system) Rule p
r Paths i (PTH b)
path) [Rule p]
rs))
normalise :: System p -> Paths (a :: PATHS p) b -> Paths a b
normalise :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
System p -> Paths a b -> Paths a b
normalise System p
_ Paths a b
PNil = Paths a b
Paths (PTH a1) (PTH a1)
forall {k} {p :: CAT k} (a1 :: k). Ob a1 => Paths (PTH a1) (PTH a1)
PNil
normalise System p
system (PCons p a1 b1
p Paths a (PTH a1)
more) = System p -> p a1 b1 -> Paths a (PTH a1) -> Paths a (PTH b1)
forall {k} (a :: k) (b :: k) (p :: CAT k) (i :: PATHS p).
(Ob a, Ob b) =>
System p -> p a b -> Paths i (PTH a) -> Paths i (PTH b)
rewrite System p
system p a1 b1
p (System p -> Paths a (PTH a1) -> Paths a (PTH a1)
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
System p -> Paths a b -> Paths a b
normalise System p
system Paths a (PTH a1)
more)
equivalent :: System p -> Paths (a :: PATHS p) b -> Paths a b -> Bool
equivalent :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
System p -> Paths a b -> Paths a b -> Bool
equivalent system :: System p
system@System{} Paths a b
a Paths a b
b = System p -> Paths a b -> Paths a b
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
System p -> Paths a b -> Paths a b
normalise System p
system Paths a b
a Paths a b -> Paths a b -> Bool
forall a. Eq a => a -> a -> Bool
== System p -> Paths a b -> Paths a b
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
System p -> Paths a b -> Paths a b
normalise System p
system Paths a b
b
type Compiled :: forall {k}. CAT k -> Type
data Compiled p where
Compiled :: (EqGen p) => GeneratorKey p -> Trie p -> Compiled p
type Trie :: forall {k}. CAT k -> Type
data Trie p = Trie (Maybe (Rule p)) (IM.IntMap (Trie p))
compile :: (Ord key) => (forall a b. p a b -> key) -> System p -> Compiled p
compile :: forall {k} key (p :: CAT k).
Ord key =>
(forall (a :: k) (b :: k). p a b -> key) -> System p -> Compiled p
compile forall (a :: k) (b :: k). p a b -> key
rank (System [Rule p]
rs Maybe (Compiled p)
_) = GeneratorKey p -> [Rule p] -> Compiled p
forall {k} (p :: CAT k).
EqGen p =>
GeneratorKey p -> [Rule p] -> Compiled p
compileWithKey ((forall (a :: k) (b :: k). p a b -> key)
-> [Generator p] -> GeneratorKey p
forall {k} key (p :: CAT k).
Ord key =>
(forall (a :: k) (b :: k). p a b -> key)
-> [Generator p] -> GeneratorKey p
orderedKey p a b -> key
forall (a :: k) (b :: k). p a b -> key
rank [Generator p]
alphabet) [Rule p]
rs
where
alphabet :: [Generator p]
alphabet = [Generator p] -> [Generator p]
forall {k} (p :: CAT k). EqGen p => [Generator p] -> [Generator p]
uniqueGenerators [Generator p
g | Rule Paths a b
l Paths a b
_ <- [Rule p]
rs, Generator p
g <- Paths a b -> [Generator p]
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Paths a b -> [Generator p]
generators Paths a b
l]
compileWithKey :: (EqGen p) => GeneratorKey p -> [Rule p] -> Compiled p
compileWithKey :: forall {k} (p :: CAT k).
EqGen p =>
GeneratorKey p -> [Rule p] -> Compiled p
compileWithKey GeneratorKey p
key [Rule p]
rs = GeneratorKey p -> Trie p -> Compiled p
forall {k} (p :: CAT k).
EqGen p =>
GeneratorKey p -> Trie p -> Compiled p
Compiled p a b -> Int
GeneratorKey p
key ([([Int], Rule p)] -> Trie p
forall {k} {p :: CAT k}. [([Int], Rule p)] -> Trie p
build [(GeneratorKey p -> Rule p -> [Int]
forall {k} (p :: CAT k). GeneratorKey p -> Rule p -> [Int]
leftKeys p a b -> Int
GeneratorKey p
key Rule p
r, Rule p
r) | Rule p
r <- [Rule p]
rs])
where
build :: [([Int], Rule p)] -> Trie p
build [([Int], Rule p)]
entries =
let children :: IntMap [([Int], Rule p)]
children = ([([Int], Rule p)] -> [([Int], Rule p)] -> [([Int], Rule p)])
-> [(Int, [([Int], Rule p)])] -> IntMap [([Int], Rule p)]
forall a. (a -> a -> a) -> [(Int, a)] -> IntMap a
IM.fromListWith [([Int], Rule p)] -> [([Int], Rule p)] -> [([Int], Rule p)]
forall a. [a] -> [a] -> [a]
(++) [(Int
k, [([Int]
more, Rule p
r)]) | (Int
k : [Int]
more, Rule p
r) <- [([Int], Rule p)]
entries]
in Maybe (Rule p) -> IntMap (Trie p) -> Trie p
forall {k} (p :: CAT k).
Maybe (Rule p) -> IntMap (Trie p) -> Trie p
Trie ([Rule p] -> Maybe (Rule p)
forall a. [a] -> Maybe a
listToMaybe [Rule p
r | ([], Rule p
r) <- [([Int], Rule p)]
entries]) (([([Int], Rule p)] -> Trie p)
-> IntMap [([Int], Rule p)] -> IntMap (Trie p)
forall a b. (a -> b) -> IntMap a -> IntMap b
IM.map [([Int], Rule p)] -> Trie p
build IntMap [([Int], Rule p)]
children)
orderedKey :: (Ord key) => (forall a b. p a b -> key) -> [Generator p] -> GeneratorKey p
orderedKey :: forall {k} key (p :: CAT k).
Ord key =>
(forall (a :: k) (b :: k). p a b -> key)
-> [Generator p] -> GeneratorKey p
orderedKey forall (a :: k) (b :: k). p a b -> key
rank [Generator p]
alphabet = \p a b
p ->
let input :: key
input = p a b -> key
forall (a :: k) (b :: k). p a b -> key
rank p a b
p
search :: Int -> Int -> Int
search Int
lo Int
hi
| Int
lo Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
hi = -Int
1
| Bool
otherwise =
let mid :: Int
mid = (Int
lo Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
hi) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
in case key -> key -> Ordering
forall a. Ord a => a -> a -> Ordering
compare key
input (Vector key
sorted Vector key -> Int -> key
forall a. Vector a -> Int -> a
V.! Int
mid) of
Ordering
LT -> Int -> Int -> Int
search Int
lo Int
mid
Ordering
EQ -> Int
mid
Ordering
GT -> Int -> Int -> Int
search (Int
mid Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
hi
in Int -> Int -> Int
search Int
0 (Vector key -> Int
forall a. Vector a -> Int
V.length Vector key
sorted)
where
sorted :: Vector key
sorted = [key] -> Vector key
forall a. [a] -> Vector a
V.fromList ([key] -> [key]
forall a. Ord a => [a] -> [a]
sort [p a b -> key
forall (a :: k) (b :: k). p a b -> key
rank p a b
p | Generator p a b
p <- [Generator p]
alphabet])
matchTrie :: GeneratorKey p -> Trie p -> Paths (a :: PATHS p) b -> Maybe (Rule p)
matchTrie :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
GeneratorKey p -> Trie p -> Paths a b -> Maybe (Rule p)
matchTrie GeneratorKey p
_ (Trie (Just Rule p
r) IntMap (Trie p)
_) Paths a b
_ = Rule p -> Maybe (Rule p)
forall a. a -> Maybe a
Just Rule p
r
matchTrie GeneratorKey p
key (Trie Maybe (Rule p)
_ IntMap (Trie p)
branches) (PCons p a1 b1
p Paths a (PTH a1)
more) = do
child <- Int -> IntMap (Trie p) -> Maybe (Trie p)
forall a. Int -> IntMap a -> Maybe a
IM.lookup (p a1 b1 -> Int
GeneratorKey p
key p a1 b1
p) IntMap (Trie p)
branches
matchTrie key child more
matchTrie GeneratorKey p
_ Trie p
_ Paths a b
PNil = Maybe (Rule p)
forall a. Maybe a
Nothing
normaliseCompiledOnto :: Compiled p -> Paths (a :: PATHS p) b -> Paths x a -> Paths x b
normaliseCompiledOnto :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
Compiled p -> Paths a b -> Paths x a -> Paths x b
normaliseCompiledOnto Compiled p
_ Paths a b
PNil Paths x a
rest = Paths x a
Paths x b
rest
normaliseCompiledOnto Compiled p
compiled (PCons p a1 b1
p Paths a (PTH a1)
more) Paths x a
rest = Compiled p -> p a1 b1 -> Paths x (PTH a1) -> Paths x (PTH b1)
forall {k} (a :: k) (b :: k) (p :: CAT k) (i :: PATHS p).
(Ob a, Ob b) =>
Compiled p -> p a b -> Paths i (PTH a) -> Paths i (PTH b)
rewriteCompiled Compiled p
compiled p a1 b1
p (Compiled p -> Paths a (PTH a1) -> Paths x a -> Paths x (PTH a1)
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
Compiled p -> Paths a b -> Paths x a -> Paths x b
normaliseCompiledOnto Compiled p
compiled Paths a (PTH a1)
more Paths x a
rest)
rewriteCompiled
:: (Ob a, Ob b)
=> Compiled p -> p a b -> Paths i (PTH a :: PATHS p) -> Paths i (PTH b)
rewriteCompiled :: forall {k} (a :: k) (b :: k) (p :: CAT k) (i :: PATHS p).
(Ob a, Ob b) =>
Compiled p -> p a b -> Paths i (PTH a) -> Paths i (PTH b)
rewriteCompiled compiled :: Compiled p
compiled@(Compiled GeneratorKey p
key Trie p
trie) p a b
p Paths i (PTH a)
more =
let path :: Paths i (PTH b)
path = p a b -> Paths i (PTH a) -> Paths i (PTH b)
forall {k} (a1 :: k) (b1 :: k) (p :: CAT k) (a :: PATHS p).
(Ob a1, Ob b1) =>
p a1 b1 -> Paths a (PTH a1) -> Paths a (PTH b1)
PCons p a b
p Paths i (PTH a)
more
in Paths i (PTH b) -> Maybe (Paths i (PTH b)) -> Paths i (PTH b)
forall a. a -> Maybe a -> a
fromMaybe Paths i (PTH b)
path (Maybe (Paths i (PTH b)) -> Paths i (PTH b))
-> Maybe (Paths i (PTH b)) -> Paths i (PTH b)
forall a b. (a -> b) -> a -> b
$ do
r <- GeneratorKey p -> Trie p -> Paths i (PTH b) -> Maybe (Rule p)
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
GeneratorKey p -> Trie p -> Paths a b -> Maybe (Rule p)
matchTrie p a b -> Int
GeneratorKey p
key Trie p
trie Paths i (PTH b)
path
rewriteRoot (normaliseCompiledOnto compiled) r path
normaliseCompiled :: Compiled p -> Paths (a :: PATHS p) b -> Paths a b
normaliseCompiled :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Compiled p -> Paths a b -> Paths a b
normaliseCompiled Compiled p
_ Paths a b
PNil = Paths a b
Paths (PTH a1) (PTH a1)
forall {k} {p :: CAT k} (a1 :: k). Ob a1 => Paths (PTH a1) (PTH a1)
PNil
normaliseCompiled Compiled p
compiled (PCons p a1 b1
p Paths a (PTH a1)
more) = Compiled p -> p a1 b1 -> Paths a (PTH a1) -> Paths a (PTH b1)
forall {k} (a :: k) (b :: k) (p :: CAT k) (i :: PATHS p).
(Ob a, Ob b) =>
Compiled p -> p a b -> Paths i (PTH a) -> Paths i (PTH b)
rewriteCompiled Compiled p
compiled p a1 b1
p (Compiled p -> Paths a (PTH a1) -> Paths a (PTH a1)
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Compiled p -> Paths a b -> Paths a b
normaliseCompiled Compiled p
compiled Paths a (PTH a1)
more)
type Cut :: CAT (PATHS p)
data Cut a b where
Cut :: Paths m b -> Paths a m -> Cut a b
cuts :: Paths a b -> [Cut a b]
cuts :: forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p).
Paths a b -> [Cut a b]
cuts Paths a b
PNil = []
cuts whole :: Paths a b
whole@(PCons p a1 b1
p Paths a (PTH a1)
more) = Paths (PTH b1) b -> Paths a (PTH b1) -> Cut a b
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p)
(a :: PATHS p).
Paths a b -> Paths a a -> Cut a b
Cut Paths (PTH b1) b
Paths (PTH b1) (PTH b1)
forall {k} {p :: CAT k} (a1 :: k). Ob a1 => Paths (PTH a1) (PTH a1)
PNil Paths a b
Paths a (PTH b1)
whole Cut a b -> [Cut a b] -> [Cut a b]
forall a. a -> [a] -> [a]
: [Paths m b -> Paths a m -> Cut a b
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p)
(a :: PATHS p).
Paths a b -> Paths a a -> Cut a b
Cut (p a1 b1 -> Paths m (PTH a1) -> Paths m (PTH b1)
forall {k} (a1 :: k) (b1 :: k) (p :: CAT k) (a :: PATHS p).
(Ob a1, Ob b1) =>
p a1 b1 -> Paths a (PTH a1) -> Paths a (PTH b1)
PCons p a1 b1
p Paths m (PTH a1)
before) Paths a m
after | Cut Paths m (PTH a1)
before Paths a m
after <- Paths a (PTH a1) -> [Cut a (PTH a1)]
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p).
Paths a b -> [Cut a b]
cuts Paths a (PTH a1)
more]
type Overlap :: forall {k} {p :: CAT k}. PATHS p -> PATHS p -> PATHS p -> PATHS p -> Type
data Overlap a b c d where
Overlap :: Paths x a -> Paths x c -> Overlap a b c b
align :: (EqGen p) => Paths (a :: PATHS p) b -> Paths c b -> Maybe (Overlap a b c b)
align :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(c :: PATHS p).
EqGen p =>
Paths a b -> Paths c b -> Maybe (Overlap a b c b)
align Paths a b
PNil Paths c b
q = Overlap a b c b -> Maybe (Overlap a b c b)
forall a. a -> Maybe a
Just (Paths c a -> Paths c c -> Overlap a b c b
forall {k} {p :: CAT k} (a :: PATHS p) (a :: PATHS p)
(c :: PATHS p) (b :: PATHS p).
Paths a a -> Paths a c -> Overlap a b c b
Overlap Paths c a
Paths c b
q (Paths c b -> Paths c c
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p).
Paths a b -> Paths a a
sourceIdentity Paths c b
q))
align Paths a b
p Paths c b
PNil = Overlap a b c b -> Maybe (Overlap a b c b)
forall a. a -> Maybe a
Just (Paths a a -> Paths a c -> Overlap a b c b
forall {k} {p :: CAT k} (a :: PATHS p) (a :: PATHS p)
(c :: PATHS p) (b :: PATHS p).
Paths a a -> Paths a c -> Overlap a b c b
Overlap (Paths a b -> Paths a a
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p).
Paths a b -> Paths a a
sourceIdentity Paths a b
p) Paths a b
Paths a c
p)
align (PCons p a1 b1
p Paths a (PTH a1)
more) (PCons p a1 b1
q Paths c (PTH a1)
rest) = case p a1 b1 -> p a1 b1 -> Maybe ('(a1, b1) :~: '(a1, b1))
forall (a :: k) (b :: k) (c :: k) (d :: k).
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
forall k (p :: CAT k) (a :: k) (b :: k) (c :: k) (d :: k).
EqGen p =>
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
eqGen p a1 b1
p p a1 b1
q of
Just '(a1, b1) :~: '(a1, b1)
Refl -> do
Overlap afterP afterQ <- Paths a (PTH a1)
-> Paths c (PTH a1) -> Maybe (Overlap a (PTH a1) c (PTH a1))
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(c :: PATHS p).
EqGen p =>
Paths a b -> Paths c b -> Maybe (Overlap a b c b)
align Paths a (PTH a1)
more Paths c (PTH a1)
Paths c (PTH a1)
rest
pure (Overlap afterP afterQ)
Maybe ('(a1, b1) :~: '(a1, b1))
_ -> Maybe (Overlap a b c b)
forall a. Maybe a
Nothing
overlap :: (EqGen p) => Paths (a :: PATHS p) b -> Paths (c :: PATHS p) d -> Maybe (Overlap a b c d)
overlap :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(c :: PATHS p) (d :: PATHS p).
EqGen p =>
Paths a b -> Paths c d -> Maybe (Overlap a b c d)
overlap (PCons p a1 b1
p Paths a (PTH a1)
more) (PCons p a1 b1
q Paths c (PTH a1)
rest) = case p a1 b1 -> p a1 b1 -> Maybe ('(a1, b1) :~: '(a1, b1))
forall (a :: k) (b :: k) (c :: k) (d :: k).
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
forall k (p :: CAT k) (a :: k) (b :: k) (c :: k) (d :: k).
EqGen p =>
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
eqGen p a1 b1
p p a1 b1
q of
Just '(a1, b1) :~: '(a1, b1)
Refl -> do
Overlap afterP afterQ <- Paths a (PTH a1)
-> Paths c (PTH a1) -> Maybe (Overlap a (PTH a1) c (PTH a1))
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p)
(c :: PATHS p).
EqGen p =>
Paths a b -> Paths c b -> Maybe (Overlap a b c b)
align Paths a (PTH a1)
more Paths c (PTH a1)
Paths c (PTH a1)
rest
pure (Overlap afterP afterQ)
Maybe ('(a1, b1) :~: '(a1, b1))
_ -> Maybe (Overlap a b c d)
forall a. Maybe a
Nothing
overlap Paths a b
_ Paths c d
_ = Maybe (Overlap a b c d)
forall a. Maybe a
Nothing
pairs :: (EqGen p) => Rule p -> Rule p -> [Equation p]
pairs :: forall {k} (p :: CAT k).
EqGen p =>
Rule p -> Rule p -> [Equation p]
pairs (Rule Paths a b
l Paths a b
r) (Rule Paths a b
l' Paths a b
r') = do
Cut before after <- Paths a b -> [Cut a b]
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p).
Paths a b -> [Cut a b]
cuts Paths a b
l
Overlap afterL afterR <- maybe [] pure (overlap after l')
let a = Paths a b -> Paths x a -> Paths x b
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
Paths a b -> Paths x a -> Paths x b
append Paths a b
r Paths x a
afterL
b = Paths m b -> Paths x m -> Paths x b
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
Paths a b -> Paths x a -> Paths x b
append Paths m b
before (Paths a m -> Paths x a -> Paths x m
forall {k} {p :: CAT k} (a :: PATHS p) (b :: PATHS p)
(x :: PATHS p).
Paths a b -> Paths x a -> Paths x b
append Paths a b
Paths a m
r' Paths x a
afterR)
if a == b then [] else pure (a :=: b)
criticalPairs :: System p -> [Equation p]
criticalPairs :: forall {k} (p :: CAT k). System p -> [Equation p]
criticalPairs (System [Rule p]
rs Maybe (Compiled p)
_) = GeneratorKey p -> [Rule p] -> [Equation p]
forall {k} (p :: CAT k).
EqGen p =>
GeneratorKey p -> [Rule p] -> [Equation p]
indexedPairs ([Generator p] -> GeneratorKey p
forall {k} (p :: CAT k). EqGen p => [Generator p] -> GeneratorKey p
generatorKey [Generator p]
alphabet) [Rule p]
rs
where
alphabet :: [Generator p]
alphabet = [Generator p] -> [Generator p]
forall {k} (p :: CAT k). EqGen p => [Generator p] -> [Generator p]
uniqueGenerators [Generator p
g | Rule Paths a b
l Paths a b
_ <- [Rule p]
rs, Generator p
g <- Paths a b -> [Generator p]
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Paths a b -> [Generator p]
generators Paths a b
l]
type OverlapIndex :: forall {k}. CAT k -> Type
data OverlapIndex p = OverlapIndex Int (IM.IntMap (IM.IntMap (Rule p))) (IM.IntMap (IM.IntMap (Rule p)))
emptyIndex :: OverlapIndex p
emptyIndex :: forall {k} (p :: CAT k). OverlapIndex p
emptyIndex = Int
-> IntMap (IntMap (Rule p))
-> IntMap (IntMap (Rule p))
-> OverlapIndex p
forall {k} (p :: CAT k).
Int
-> IntMap (IntMap (Rule p))
-> IntMap (IntMap (Rule p))
-> OverlapIndex p
OverlapIndex Int
0 IntMap (IntMap (Rule p))
forall a. IntMap a
IM.empty IntMap (IntMap (Rule p))
forall a. IntMap a
IM.empty
type GeneratorKey :: forall {k}. CAT k -> Type
type GeneratorKey p = forall a b. p a b -> Int
leftKeys :: GeneratorKey p -> Rule p -> [Int]
leftKeys :: forall {k} (p :: CAT k). GeneratorKey p -> Rule p -> [Int]
leftKeys GeneratorKey p
key (Rule Paths a b
l Paths a b
_) = [p a b -> Int
GeneratorKey p
key p a b
p | Generator p a b
p <- Paths a b -> [Generator p]
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Paths a b -> [Generator p]
generators Paths a b
l]
indexRule :: GeneratorKey p -> Rule p -> OverlapIndex p -> OverlapIndex p
indexRule :: forall {k} (p :: CAT k).
GeneratorKey p -> Rule p -> OverlapIndex p -> OverlapIndex p
indexRule GeneratorKey p
key Rule p
r index :: OverlapIndex p
index@(OverlapIndex Int
n IntMap (IntMap (Rule p))
starts IntMap (IntMap (Rule p))
occurrences) = case GeneratorKey p -> Rule p -> [Int]
forall {k} (p :: CAT k). GeneratorKey p -> Rule p -> [Int]
leftKeys p a b -> Int
GeneratorKey p
key Rule p
r of
[] -> OverlapIndex p
index
ks :: [Int]
ks@(Int
first : [Int]
_) ->
let entry :: IntMap (Rule p)
entry = Int -> Rule p -> IntMap (Rule p)
forall a. Int -> a -> IntMap a
IM.singleton Int
n Rule p
r
add :: Int -> IntMap (IntMap (Rule p)) -> IntMap (IntMap (Rule p))
add Int
k = (IntMap (Rule p) -> IntMap (Rule p) -> IntMap (Rule p))
-> Int
-> IntMap (Rule p)
-> IntMap (IntMap (Rule p))
-> IntMap (IntMap (Rule p))
forall a. (a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
IM.insertWith IntMap (Rule p) -> IntMap (Rule p) -> IntMap (Rule p)
forall a. IntMap a -> IntMap a -> IntMap a
IM.union Int
k IntMap (Rule p)
entry
in Int
-> IntMap (IntMap (Rule p))
-> IntMap (IntMap (Rule p))
-> OverlapIndex p
forall {k} (p :: CAT k).
Int
-> IntMap (IntMap (Rule p))
-> IntMap (IntMap (Rule p))
-> OverlapIndex p
OverlapIndex (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Int -> IntMap (IntMap (Rule p)) -> IntMap (IntMap (Rule p))
add Int
first IntMap (IntMap (Rule p))
starts) ((Int -> IntMap (IntMap (Rule p)) -> IntMap (IntMap (Rule p)))
-> IntMap (IntMap (Rule p)) -> IntSet -> IntMap (IntMap (Rule p))
forall b. (Int -> b -> b) -> b -> IntSet -> b
IS.foldr Int -> IntMap (IntMap (Rule p)) -> IntMap (IntMap (Rule p))
add IntMap (IntMap (Rule p))
occurrences ([Int] -> IntSet
IS.fromList [Int]
ks))
overlapsWith :: (EqGen p) => GeneratorKey p -> OverlapIndex p -> Rule p -> [Equation p]
overlapsWith :: forall {k} (p :: CAT k).
EqGen p =>
GeneratorKey p -> OverlapIndex p -> Rule p -> [Equation p]
overlapsWith GeneratorKey p
key (OverlapIndex Int
_ IntMap (IntMap (Rule p))
starts IntMap (IntMap (Rule p))
occurrences) Rule p
r = case GeneratorKey p -> Rule p -> [Int]
forall {k} (p :: CAT k). GeneratorKey p -> Rule p -> [Int]
leftKeys p a b -> Int
GeneratorKey p
key Rule p
r of
[] -> []
ks :: [Int]
ks@(Int
first : [Int]
_) ->
let candidates :: IntMap (Rule p)
candidates = [IntMap (Rule p)] -> IntMap (Rule p)
forall (f :: Type -> Type) a.
Foldable f =>
f (IntMap a) -> IntMap a
IM.unions [IntMap (Rule p)
-> Int -> IntMap (IntMap (Rule p)) -> IntMap (Rule p)
forall a. a -> Int -> IntMap a -> a
IM.findWithDefault IntMap (Rule p)
forall a. IntMap a
IM.empty Int
k IntMap (IntMap (Rule p))
starts | Int
k <- IntSet -> [Int]
IS.toList ([Int] -> IntSet
IS.fromList [Int]
ks)]
containers :: IntMap (Rule p)
containers = IntMap (Rule p)
-> Int -> IntMap (IntMap (Rule p)) -> IntMap (Rule p)
forall a. a -> Int -> IntMap a -> a
IM.findWithDefault IntMap (Rule p)
forall a. IntMap a
IM.empty Int
first IntMap (IntMap (Rule p))
occurrences
in (Rule p -> [Equation p]) -> [Rule p] -> [Equation p]
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> [b]) -> t a -> [b]
concatMap (Rule p -> Rule p -> [Equation p]
forall {k} (p :: CAT k).
EqGen p =>
Rule p -> Rule p -> [Equation p]
pairs Rule p
r) (IntMap (Rule p) -> [Rule p]
forall a. IntMap a -> [a]
IM.elems IntMap (Rule p)
candidates) [Equation p] -> [Equation p] -> [Equation p]
forall a. [a] -> [a] -> [a]
++ (Rule p -> [Equation p]) -> [Rule p] -> [Equation p]
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> [b]) -> t a -> [b]
concatMap (Rule p -> Rule p -> [Equation p]
forall {k} (p :: CAT k).
EqGen p =>
Rule p -> Rule p -> [Equation p]
`pairs` Rule p
r) (IntMap (Rule p) -> [Rule p]
forall a. IntMap a -> [a]
IM.elems IntMap (Rule p)
containers)
indexedPairs :: (EqGen p) => GeneratorKey p -> [Rule p] -> [Equation p]
indexedPairs :: forall {k} (p :: CAT k).
EqGen p =>
GeneratorKey p -> [Rule p] -> [Equation p]
indexedPairs GeneratorKey p
key = OverlapIndex p -> [Rule p] -> [Equation p]
go OverlapIndex p
forall {k} (p :: CAT k). OverlapIndex p
emptyIndex
where
go :: OverlapIndex p -> [Rule p] -> [Equation p]
go OverlapIndex p
_ [] = []
go OverlapIndex p
index (Rule p
r : [Rule p]
rs) = Rule p -> Rule p -> [Equation p]
forall {k} (p :: CAT k).
EqGen p =>
Rule p -> Rule p -> [Equation p]
pairs Rule p
r Rule p
r [Equation p] -> [Equation p] -> [Equation p]
forall a. [a] -> [a] -> [a]
++ GeneratorKey p -> OverlapIndex p -> Rule p -> [Equation p]
forall {k} (p :: CAT k).
EqGen p =>
GeneratorKey p -> OverlapIndex p -> Rule p -> [Equation p]
overlapsWith p a b -> Int
GeneratorKey p
key OverlapIndex p
index Rule p
r [Equation p] -> [Equation p] -> [Equation p]
forall a. [a] -> [a] -> [a]
++ OverlapIndex p -> [Rule p] -> [Equation p]
go (GeneratorKey p -> Rule p -> OverlapIndex p -> OverlapIndex p
forall {k} (p :: CAT k).
GeneratorKey p -> Rule p -> OverlapIndex p -> OverlapIndex p
indexRule p a b -> Int
GeneratorKey p
key Rule p
r OverlapIndex p
index) [Rule p]
rs
data Limits = Limits {Limits -> Int
maxRules :: Int, Limits -> Int
maxEquations :: Int} deriving (Limits -> Limits -> Bool
(Limits -> Limits -> Bool)
-> (Limits -> Limits -> Bool) -> Eq Limits
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Limits -> Limits -> Bool
== :: Limits -> Limits -> Bool
$c/= :: Limits -> Limits -> Bool
/= :: Limits -> Limits -> Bool
Eq, Int -> Limits -> ShowS
[Limits] -> ShowS
Limits -> String
(Int -> Limits -> ShowS)
-> (Limits -> String) -> ([Limits] -> ShowS) -> Show Limits
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Limits -> ShowS
showsPrec :: Int -> Limits -> ShowS
$cshow :: Limits -> String
show :: Limits -> String
$cshowList :: [Limits] -> ShowS
showList :: [Limits] -> ShowS
Show)
data Limit = RuleLimit | EquationLimit deriving (Limit -> Limit -> Bool
(Limit -> Limit -> Bool) -> (Limit -> Limit -> Bool) -> Eq Limit
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Limit -> Limit -> Bool
== :: Limit -> Limit -> Bool
$c/= :: Limit -> Limit -> Bool
/= :: Limit -> Limit -> Bool
Eq, Int -> Limit -> ShowS
[Limit] -> ShowS
Limit -> String
(Int -> Limit -> ShowS)
-> (Limit -> String) -> ([Limit] -> ShowS) -> Show Limit
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Limit -> ShowS
showsPrec :: Int -> Limit -> ShowS
$cshow :: Limit -> String
show :: Limit -> String
$cshowList :: [Limit] -> ShowS
showList :: [Limit] -> ShowS
Show)
type Completion :: forall {k}. CAT k -> Type
data Completion p
= Complete (System p)
| Incomplete Limit (System p) [Equation p]
complete :: forall {k} (p :: CAT k). (EqGen p) => Limits -> [Equation p] -> Completion p
complete :: forall {k} (p :: CAT k).
EqGen p =>
Limits -> [Equation p] -> Completion p
complete Limits
limits [Equation p]
equations = [Generator p]
-> (forall (a :: k) (b :: k). p a b -> Int)
-> (forall (a :: k) (b :: k). p a b -> Int)
-> Limits
-> [Equation p]
-> Completion p
forall {k} (p :: CAT k) key.
(EqGen p, Ord key) =>
[Generator p]
-> (forall (a :: k) (b :: k). p a b -> key)
-> GeneratorKey p
-> Limits
-> [Equation p]
-> Completion p
completeWithAlphabet [Generator p]
alphabet p a b -> Int
forall (a :: k) (b :: k). p a b -> Int
key p a b -> Int
forall (a :: k) (b :: k). p a b -> Int
key Limits
limits [Equation p]
equations
where
alphabet :: [Generator p]
alphabet = [Equation p] -> [Generator p]
forall {k} (p :: CAT k). EqGen p => [Equation p] -> [Generator p]
presentationAlphabet [Equation p]
equations
key :: GeneratorKey p
key :: forall (a :: k) (b :: k). p a b -> Int
key = [Generator p] -> forall (a :: k) (b :: k). p a b -> Int
forall {k} (p :: CAT k). EqGen p => [Generator p] -> GeneratorKey p
generatorKey [Generator p]
alphabet
type Generator :: forall {k}. CAT k -> Type
data Generator p where
Generator :: p a b -> Generator p
generators :: Paths (a :: PATHS p) b -> [Generator p]
generators :: forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Paths a b -> [Generator p]
generators Paths a b
PNil = []
generators (PCons p a1 b1
p Paths a (PTH a1)
more) = p a1 b1 -> Generator p
forall {k} (p :: CAT k) (a :: k) (b :: k). p a b -> Generator p
Generator p a1 b1
p Generator p -> [Generator p] -> [Generator p]
forall a. a -> [a] -> [a]
: Paths a (PTH a1) -> [Generator p]
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Paths a b -> [Generator p]
generators Paths a (PTH a1)
more
presentationAlphabet :: (EqGen p) => [Equation p] -> [Generator p]
presentationAlphabet :: forall {k} (p :: CAT k). EqGen p => [Equation p] -> [Generator p]
presentationAlphabet = [Generator p] -> [Generator p]
forall {k} (p :: CAT k). EqGen p => [Generator p] -> [Generator p]
uniqueGenerators ([Generator p] -> [Generator p])
-> ([Equation p] -> [Generator p]) -> [Equation p] -> [Generator p]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Equation p -> [Generator p]) -> [Equation p] -> [Generator p]
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> [b]) -> t a -> [b]
concatMap (\Equation p
e -> Equation (PATHS p)
-> (forall (a :: PATHS p) (b :: PATHS p).
(a ~> b) -> (a ~> b) -> [Generator p])
-> [Generator p]
forall {k} r.
Equation k
-> (forall (a :: k) (b :: k). (a ~> b) -> (a ~> b) -> r) -> r
Laws.withSides Equation (PATHS p)
Equation p
e (\a ~> b
l a ~> b
r -> Paths a b -> [Generator p]
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Paths a b -> [Generator p]
generators a ~> b
Paths a b
l [Generator p] -> [Generator p] -> [Generator p]
forall a. [a] -> [a] -> [a]
++ Paths a b -> [Generator p]
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Paths a b -> [Generator p]
generators a ~> b
Paths a b
r))
uniqueGenerators :: (EqGen p) => [Generator p] -> [Generator p]
uniqueGenerators :: forall {k} (p :: CAT k). EqGen p => [Generator p] -> [Generator p]
uniqueGenerators = [Generator p] -> [Generator p]
forall a. [a] -> [a]
reverse ([Generator p] -> [Generator p])
-> ([Generator p] -> [Generator p])
-> [Generator p]
-> [Generator p]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Generator p] -> Generator p -> [Generator p])
-> [Generator p] -> [Generator p] -> [Generator p]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: Type -> Type) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' [Generator p] -> Generator p -> [Generator p]
forall {k} {p :: CAT k}.
EqGen p =>
[Generator p] -> Generator p -> [Generator p]
add []
where
add :: [Generator p] -> Generator p -> [Generator p]
add [Generator p]
seen g :: Generator p
g@(Generator p a b
p)
| (Generator p -> Bool) -> [Generator p] -> Bool
forall (t :: Type -> Type) a.
Foldable t =>
(a -> Bool) -> t a -> Bool
any (\(Generator p a b
q) -> Maybe ('(a, b) :~: '(a, b)) -> Bool
forall a. Maybe a -> Bool
isJust (p a b -> p a b -> Maybe ('(a, b) :~: '(a, b))
forall (a :: k) (b :: k) (c :: k) (d :: k).
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
forall k (p :: CAT k) (a :: k) (b :: k) (c :: k) (d :: k).
EqGen p =>
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
eqGen p a b
p p a b
q)) [Generator p]
seen = [Generator p]
seen
| Bool
otherwise = Generator p
g Generator p -> [Generator p] -> [Generator p]
forall a. a -> [a] -> [a]
: [Generator p]
seen
generatorKey :: (EqGen p) => [Generator p] -> GeneratorKey p
generatorKey :: forall {k} (p :: CAT k). EqGen p => [Generator p] -> GeneratorKey p
generatorKey [Generator p]
alphabet p a b
p = Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe (-Int
1) ((Generator p -> Bool) -> [Generator p] -> Maybe Int
forall a. (a -> Bool) -> [a] -> Maybe Int
findIndex (\(Generator p a b
q) -> Maybe ('(a, b) :~: '(a, b)) -> Bool
forall a. Maybe a -> Bool
isJust (p a b -> p a b -> Maybe ('(a, b) :~: '(a, b))
forall (a :: k) (b :: k) (c :: k) (d :: k).
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
forall k (p :: CAT k) (a :: k) (b :: k) (c :: k) (d :: k).
EqGen p =>
p a b -> p c d -> Maybe ('(a, b) :~: '(c, d))
eqGen p a b
p p a b
q)) [Generator p]
alphabet)
interreduce :: Rule p -> System p -> (System p, [Equation p], Bool)
interreduce :: forall {k} (p :: CAT k).
Rule p -> System p -> (System p, [Equation p], Bool)
interreduce Rule p
new (System [Rule p]
old Maybe (Compiled p)
_) =
let single :: System p
single = [Rule p] -> Maybe (Compiled p) -> System p
forall {k} (p :: CAT k).
EqGen p =>
[Rule p] -> Maybe (Compiled p) -> System p
System [Rule p
new] Maybe (Compiled p)
forall a. Maybe a
Nothing
combined :: System p
combined = [Rule p] -> Maybe (Compiled p) -> System p
forall {k} (p :: CAT k).
EqGen p =>
[Rule p] -> Maybe (Compiled p) -> System p
System (Rule p
new Rule p -> [Rule p] -> [Rule p]
forall a. a -> [a] -> [a]
: [Rule p]
old) Maybe (Compiled p)
forall a. Maybe a
Nothing
reduce :: Rule p
-> ([Rule p], [Equation p], Bool) -> ([Rule p], [Equation p], Bool)
reduce oldRule :: Rule p
oldRule@(Rule Paths a b
l Paths a b
r) ([Rule p]
kept, [Equation p]
pending, Bool
changed) =
let l' :: Paths a b
l' = System p -> Paths a b -> Paths a b
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
System p -> Paths a b -> Paths a b
normalise System p
single Paths a b
l
in if Paths a b
l' Paths a b -> Paths a b -> Bool
forall a. Eq a => a -> a -> Bool
/= Paths a b
l
then ([Rule p]
kept, (Paths a b
l' Paths a b -> Paths a b -> Equation p
forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
p a b -> p a b -> ProEquation p
:=: Paths a b
r) Equation p -> [Equation p] -> [Equation p]
forall a. a -> [a] -> [a]
: [Equation p]
pending, Bool
True)
else
let r' :: Paths a b
r' = System p -> Paths a b -> Paths a b
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
System p -> Paths a b -> Paths a b
normalise System p
single Paths a b
r
in if Paths a b
r' Paths a b -> Paths a b -> Bool
forall a. Eq a => a -> a -> Bool
== Paths a b
r
then (Rule p
oldRule Rule p -> [Rule p] -> [Rule p]
forall a. a -> [a] -> [a]
: [Rule p]
kept, [Equation p]
pending, Bool
changed)
else (Paths a b -> Paths a b -> Rule p
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Paths a b -> Paths a b -> Rule p
Rule Paths a b
l (System p -> Paths a b -> Paths a b
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
System p -> Paths a b -> Paths a b
normalise System p
combined Paths a b
r') Rule p -> [Rule p] -> [Rule p]
forall a. a -> [a] -> [a]
: [Rule p]
kept, [Equation p]
pending, Bool
True)
([Rule p]
survivors, [Equation p]
collapsed, Bool
changedRules) = (Rule p
-> ([Rule p], [Equation p], Bool)
-> ([Rule p], [Equation p], Bool))
-> ([Rule p], [Equation p], Bool)
-> [Rule p]
-> ([Rule p], [Equation p], Bool)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Rule p
-> ([Rule p], [Equation p], Bool) -> ([Rule p], [Equation p], Bool)
reduce ([], [], Bool
False) [Rule p]
old
in ([Rule p] -> Maybe (Compiled p) -> System p
forall {k} (p :: CAT k).
EqGen p =>
[Rule p] -> Maybe (Compiled p) -> System p
System (Rule p
new Rule p -> [Rule p] -> [Rule p]
forall a. a -> [a] -> [a]
: [Rule p]
survivors) Maybe (Compiled p)
forall a. Maybe a
Nothing, [Equation p]
collapsed, Bool
changedRules)
completeWithOrder :: (EqGen p, Ord key) => (forall a b. p a b -> key) -> Limits -> [Equation p] -> Completion p
completeWithOrder :: forall {k} (p :: CAT k) key.
(EqGen p, Ord key) =>
(forall (a :: k) (b :: k). p a b -> key)
-> Limits -> [Equation p] -> Completion p
completeWithOrder forall (a :: k) (b :: k). p a b -> key
rank Limits
limits [Equation p]
equations = [Generator p]
-> (forall (a :: k) (b :: k). p a b -> key)
-> GeneratorKey p
-> Limits
-> [Equation p]
-> Completion p
forall {k} (p :: CAT k) key.
(EqGen p, Ord key) =>
[Generator p]
-> (forall (a :: k) (b :: k). p a b -> key)
-> GeneratorKey p
-> Limits
-> [Equation p]
-> Completion p
completeWithAlphabet [Generator p]
alphabet p a b -> key
forall (a :: k) (b :: k). p a b -> key
rank ((forall (a :: k) (b :: k). p a b -> key)
-> [Generator p] -> GeneratorKey p
forall {k} key (p :: CAT k).
Ord key =>
(forall (a :: k) (b :: k). p a b -> key)
-> [Generator p] -> GeneratorKey p
orderedKey p a b -> key
forall (a :: k) (b :: k). p a b -> key
rank [Generator p]
alphabet) Limits
limits [Equation p]
equations
where
alphabet :: [Generator p]
alphabet = [Equation p] -> [Generator p]
forall {k} (p :: CAT k). EqGen p => [Equation p] -> [Generator p]
presentationAlphabet [Equation p]
equations
completeWithAlphabet
:: forall {k} (p :: CAT k) key
. (EqGen p, Ord key)
=> [Generator p] -> (forall a b. p a b -> key) -> GeneratorKey p -> Limits -> [Equation p] -> Completion p
completeWithAlphabet :: forall {k} (p :: CAT k) key.
(EqGen p, Ord key) =>
[Generator p]
-> (forall (a :: k) (b :: k). p a b -> key)
-> GeneratorKey p
-> Limits
-> [Equation p]
-> Completion p
completeWithAlphabet [Generator p]
alphabet forall (a :: k) (b :: k). p a b -> key
rank GeneratorKey p
dispatchKey Limits
limits [Equation p]
equations = Int
-> Bool
-> System p
-> OverlapIndex p
-> [Equation p]
-> ([Equation p] -> [Equation p])
-> Completion p
go Int
0 Bool
False ([Rule p] -> Maybe (Compiled p) -> System p
forall {k} (p :: CAT k).
EqGen p =>
[Rule p] -> Maybe (Compiled p) -> System p
System [] Maybe (Compiled p)
forall a. Maybe a
Nothing) OverlapIndex p
forall {k} (p :: CAT k). OverlapIndex p
emptyIndex [Equation p]
equations [Equation p] -> [Equation p]
forall a. a -> a
id
where
key :: GeneratorKey p
key :: GeneratorKey p
key = [Generator p] -> GeneratorKey p
forall {k} (p :: CAT k). EqGen p => [Generator p] -> GeneratorKey p
generatorKey [Generator p]
alphabet
recheck :: Bool -> System p -> [Equation p]
recheck Bool
changed System p
system = if Bool
changed then GeneratorKey p -> [Rule p] -> [Equation p]
forall {k} (p :: CAT k).
EqGen p =>
GeneratorKey p -> [Rule p] -> [Equation p]
indexedPairs p a b -> Int
GeneratorKey p
key (System p -> [Rule p]
forall {k} (p :: CAT k). System p -> [Rule p]
rules System p
system) else []
stop :: Limit
-> Bool
-> System p
-> [Equation p]
-> ([Equation p] -> [Equation p])
-> Completion p
stop Limit
reason Bool
changed System p
system [Equation p]
pending [Equation p] -> [Equation p]
next =
Limit -> System p -> [Equation p] -> Completion p
forall {k} (p :: CAT k).
Limit -> System p -> [Equation p] -> Completion p
Incomplete Limit
reason System p
system ([Equation p]
pending [Equation p] -> [Equation p] -> [Equation p]
forall a. [a] -> [a] -> [a]
++ [Equation p] -> [Equation p]
next (Bool -> System p -> [Equation p]
recheck Bool
changed System p
system))
go :: Int
-> Bool
-> System p
-> OverlapIndex p
-> [Equation p]
-> ([Equation p] -> [Equation p])
-> Completion p
go Int
n Bool
changed System p
system OverlapIndex p
index [] [Equation p] -> [Equation p]
next = case [Equation p] -> [Equation p]
next (Bool -> System p -> [Equation p]
recheck Bool
changed System p
system) of
[] ->
let rs :: [Rule p]
rs = System p -> [Rule p]
forall {k} (p :: CAT k). System p -> [Rule p]
rules System p
system
compiled :: Compiled p
compiled = GeneratorKey p -> [Rule p] -> Compiled p
forall {k} (p :: CAT k).
EqGen p =>
GeneratorKey p -> [Rule p] -> Compiled p
compileWithKey p a b -> Int
GeneratorKey p
dispatchKey [Rule p]
rs
in Compiled p
compiled Compiled p -> Completion p -> Completion p
forall a b. a -> b -> b
`seq` System p -> Completion p
forall {k} (p :: CAT k). System p -> Completion p
Complete ([Rule p] -> Maybe (Compiled p) -> System p
forall {k} (p :: CAT k).
EqGen p =>
[Rule p] -> Maybe (Compiled p) -> System p
System [Rule p]
rs (Compiled p -> Maybe (Compiled p)
forall a. a -> Maybe a
Just Compiled p
compiled))
[Equation p]
pending -> Int
-> Bool
-> System p
-> OverlapIndex p
-> [Equation p]
-> ([Equation p] -> [Equation p])
-> Completion p
go Int
n Bool
False System p
system OverlapIndex p
index [Equation p]
pending [Equation p] -> [Equation p]
forall a. a -> a
id
go Int
n Bool
changed System p
system OverlapIndex p
index pending :: [Equation p]
pending@(Equation p
equation : [Equation p]
rest) [Equation p] -> [Equation p]
next
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Limits -> Int
maxEquations Limits
limits) = Limit
-> Bool
-> System p
-> [Equation p]
-> ([Equation p] -> [Equation p])
-> Completion p
stop Limit
EquationLimit Bool
changed System p
system [Equation p]
pending [Equation p] -> [Equation p]
next
| Bool
otherwise = Equation (PATHS p)
-> (forall (a :: PATHS p) (b :: PATHS p).
(a ~> b) -> (a ~> b) -> Completion p)
-> Completion p
forall {k} r.
Equation k
-> (forall (a :: k) (b :: k). (a ~> b) -> (a ~> b) -> r) -> r
Laws.withSides Equation (PATHS p)
Equation p
equation ((forall (a :: PATHS p) (b :: PATHS p).
(a ~> b) -> (a ~> b) -> Completion p)
-> Completion p)
-> (forall (a :: PATHS p) (b :: PATHS p).
(a ~> b) -> (a ~> b) -> Completion p)
-> Completion p
forall a b. (a -> b) -> a -> b
$ \a ~> b
l a ~> b
r ->
let a :: Paths a b
a = System p -> Paths a b -> Paths a b
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
System p -> Paths a b -> Paths a b
normalise System p
system a ~> b
Paths a b
l
b :: Paths a b
b = System p -> Paths a b -> Paths a b
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
System p -> Paths a b -> Paths a b
normalise System p
system a ~> b
Paths a b
r
in case (forall (a :: k) (b :: k). p a b -> key)
-> Paths a b -> Paths a b -> Ordering
forall {k} (p :: CAT k) key (a :: PATHS p) (b :: PATHS p)
(c :: PATHS p) (d :: PATHS p).
Ord key =>
(forall (x :: k) (y :: k). p x y -> key)
-> Paths a b -> Paths c d -> Ordering
comparePaths p x y -> key
forall (a :: k) (b :: k). p a b -> key
rank Paths a b
a Paths a b
b of
Ordering
EQ -> Int
-> Bool
-> System p
-> OverlapIndex p
-> [Equation p]
-> ([Equation p] -> [Equation p])
-> Completion p
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Bool
changed System p
system OverlapIndex p
index [Equation p]
rest [Equation p] -> [Equation p]
next
Ordering
direction ->
let new :: Rule p
new = if Ordering
direction Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
GT then Paths a b -> Paths a b -> Rule p
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Paths a b -> Paths a b -> Rule p
Rule Paths a b
a Paths a b
b else Paths a b -> Paths a b -> Rule p
forall {k} (p :: CAT k) (a :: PATHS p) (b :: PATHS p).
Paths a b -> Paths a b -> Rule p
Rule Paths a b
b Paths a b
a
(System p
reduced, [Equation p]
collapsed, Bool
simplified) = Rule p -> System p -> (System p, [Equation p], Bool)
forall {k} (p :: CAT k).
Rule p -> System p -> (System p, [Equation p], Bool)
interreduce Rule p
new System p
system
oldIndex :: OverlapIndex p
oldIndex = if Bool
simplified then (Rule p -> OverlapIndex p -> OverlapIndex p)
-> OverlapIndex p -> [Rule p] -> OverlapIndex p
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (GeneratorKey p -> Rule p -> OverlapIndex p -> OverlapIndex p
forall {k} (p :: CAT k).
GeneratorKey p -> Rule p -> OverlapIndex p -> OverlapIndex p
indexRule p a b -> Int
GeneratorKey p
key) OverlapIndex p
forall {k} (p :: CAT k). OverlapIndex p
emptyIndex (Int -> [Rule p] -> [Rule p]
forall a. Int -> [a] -> [a]
drop Int
1 (System p -> [Rule p]
forall {k} (p :: CAT k). System p -> [Rule p]
rules System p
reduced)) else OverlapIndex p
index
additions :: [Equation p]
additions = Rule p -> Rule p -> [Equation p]
forall {k} (p :: CAT k).
EqGen p =>
Rule p -> Rule p -> [Equation p]
pairs Rule p
new Rule p
new [Equation p] -> [Equation p] -> [Equation p]
forall a. [a] -> [a] -> [a]
++ GeneratorKey p -> OverlapIndex p -> Rule p -> [Equation p]
forall {k} (p :: CAT k).
EqGen p =>
GeneratorKey p -> OverlapIndex p -> Rule p -> [Equation p]
overlapsWith p a b -> Int
GeneratorKey p
key OverlapIndex p
oldIndex Rule p
new
in if [Rule p] -> Int
forall a. [a] -> Int
forall (t :: Type -> Type) a. Foldable t => t a -> Int
length (System p -> [Rule p]
forall {k} (p :: CAT k). System p -> [Rule p]
rules System p
reduced) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Limits -> Int
maxRules Limits
limits)
then Limit
-> Bool
-> System p
-> [Equation p]
-> ([Equation p] -> [Equation p])
-> Completion p
stop Limit
RuleLimit Bool
changed System p
system [Equation p]
pending [Equation p] -> [Equation p]
next
else
Int
-> Bool
-> System p
-> OverlapIndex p
-> [Equation p]
-> ([Equation p] -> [Equation p])
-> Completion p
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Bool
changed Bool -> Bool -> Bool
|| Bool
simplified) System p
reduced (GeneratorKey p -> Rule p -> OverlapIndex p -> OverlapIndex p
forall {k} (p :: CAT k).
GeneratorKey p -> Rule p -> OverlapIndex p -> OverlapIndex p
indexRule p a b -> Int
GeneratorKey p
key Rule p
new OverlapIndex p
oldIndex) [Equation p]
rest ([Equation p] -> [Equation p]
next ([Equation p] -> [Equation p])
-> ([Equation p] -> [Equation p]) -> [Equation p] -> [Equation p]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Equation p]
collapsed [Equation p] -> [Equation p] -> [Equation p]
forall a. [a] -> [a] -> [a]
++) ([Equation p] -> [Equation p])
-> ([Equation p] -> [Equation p]) -> [Equation p] -> [Equation p]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Equation p]
additions [Equation p] -> [Equation p] -> [Equation p]
forall a. [a] -> [a] -> [a]
++))