module Proarrow.Tools.HypergraphRewrite
(
Rule
, rule
, (==>)
, Parameter (Literal, Variable)
, mapParameter
, zipParameters
, parameterRule
, subgraphRule
, withSortParameters
, Port (..)
, PortSymmetry (..)
, PortGraph (..)
, portRule
, parameterPortRule
, parameterPortExample
, parameterPortVariants
, parameterPortRuleWith
, rewrites
, rewriteBatch
, CompiledRules
, compileRules
, compileReducingRules
, rewritesCompiled
, rewriteBatchCompiled
, RewriteLimits (..)
, unlimitedRewrites
, NormalisationStatus (..)
, NormalisationResult (..)
, normalise
) where
import Data.Vector qualified as V
import Prelude
import Proarrow.Category.Instance.OpenHypergraph (OPENHG, WireSorts, Wires, unsafeOpenHypergraph)
import Proarrow.Core (CategoryOf (..), (//))
import Proarrow.Optic.Iso (DecidableIso)
import Proarrow.Tools.HypergraphRewrite.Internal
( Graph (..)
, NormalisationResult (..)
, NormalisationStatus (..)
, Parameter (Literal, Variable)
, Port (..)
, PortGraph (..)
, PortSymmetry (..)
, Prepared
, RewriteLimits (..)
, Rule (..)
, deletedImages
, graph
, mapParameter
, matches
, nonConflictingBy
, normaliseBy
, parameterPortExample
, parameterPortRule
, parameterPortRuleWith
, parameterPortVariants
, parameterRule
, portRule
, replaceMany
, replaceSelected
, rule
, subgraphRule
, unlimitedRewrites
, withSortParameters
, zipParameters
)
import Proarrow.Tools.HypergraphRewrite.Internal.Matcher (CompiledRules (..), compileRuleSet, compiledMatches)
infix 1 ==>
(==>) :: (a :: OPENHG s l) ~> b -> a ~> b -> Either String (Rule s l)
==> :: forall (s :: Kind) (l :: Kind) (a :: OPENHG s l) (b :: OPENHG s l).
(a ~> b) -> (a ~> b) -> Either String (Rule s l)
(==>) = (a ~> b) -> (a ~> b) -> Either String (Rule s l)
forall (s :: Kind) (l :: Kind) (a :: OPENHG s l) (b :: OPENHG s l).
(a ~> b) -> (a ~> b) -> Either String (Rule s l)
rule
rewrites
:: forall {s} l a b
. (Eq l, DecidableIso s)
=> Rule s l
-> (a :: OPENHG s l) ~> b
-> [Wires (WireSorts a) ~> (Wires (WireSorts b) :: OPENHG s l)]
rewrites :: forall {s :: Kind} (l :: Kind) (a :: OPENHG s l) (b :: OPENHG s l).
(Eq l, DecidableIso s) =>
Rule s l
-> (a ~> b) -> [Wires (WireSorts a) ~> Wires (WireSorts b)]
rewrites (Rule Graph s (LabelTest l)
lhs [l] -> Maybe (Graph s l)
instantiate) a ~> b
host =
let g :: Graph s l
g = (a ~> b) -> Graph s l
forall (s :: Kind) (l :: Kind) (a :: OPENHG s l) (b :: OPENHG s l).
(a ~> b) -> Graph s l
graph a ~> b
host
in a ~> b
DecCospan a b
host DecCospan a b
-> ((Ob a, Ob b) =>
[DecCospan
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b)))))))])
-> [DecCospan
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b)))))))]
forall {k1 :: Kind} {k2 :: Kind} (p :: k1 +-> k2) (a :: k2)
(b :: k1) (r :: Kind).
Profunctor p =>
p a b -> ((Ob a, Ob b) => r) -> r
//
[ [SomeSort s]
-> [Int]
-> [Int]
-> [Box l Int]
-> Wires (PortSorts (FH (UN FH (UN SUB (UN DC a)))))
~> Wires (PortSorts (FH (UN FH (UN SUB (UN DC b)))))
forall {s :: Kind} (l :: Kind) (as :: [s]) (bs :: [s]).
(SortList as, SortList bs) =>
[SomeSort s]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
unsafeOpenHypergraph [SomeSort s]
nodes [Int]
ins [Int]
outs [Box l Int]
boxes
| (Match
match, [l]
labels) <- Graph s (LabelTest l) -> Graph s l -> [(Match, [l])]
forall (l :: Kind) (s :: Kind).
(Eq l, DecidableIso s) =>
Graph s (LabelTest l) -> Graph s l -> [(Match, [l])]
matches Graph s (LabelTest l)
lhs Graph s l
g
, Just Graph s l
rhs <- [[l] -> Maybe (Graph s l)
instantiate [l]
labels]
, let Graph [SomeSort s]
nodes [Int]
ins [Int]
outs [Box l Int]
boxes = Graph s (LabelTest l)
-> Graph s l -> Graph s l -> [Match] -> Graph s l
forall (s :: Kind) (p :: Kind) (l :: Kind).
Graph s p -> Graph s l -> Graph s l -> [Match] -> Graph s l
replaceMany Graph s (LabelTest l)
lhs Graph s l
rhs Graph s l
g [Match
match]
]
rewrites Rule s l
r a ~> b
host = (((),
DecCospan
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b))))))))
-> DecCospan
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b))))))))
-> [((),
DecCospan
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b))))))))]
-> [DecCospan
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b)))))))]
forall (a :: Kind) (b :: Kind). (a -> b) -> [a] -> [b]
map ((),
DecCospan
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b))))))))
-> DecCospan
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b)))))))
forall (a :: Kind) (b :: Kind). (a, b) -> b
snd (CompiledRules () s l
-> (a ~> b)
-> [((),
DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a))))))
~> DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b)))))))]
forall {s :: Kind} (key :: Kind) (l :: Kind) (a :: OPENHG s l)
(b :: OPENHG s l).
(Eq l, DecidableIso s) =>
CompiledRules key s l
-> (a ~> b) -> [(key, Wires (WireSorts a) ~> Wires (WireSorts b))]
rewritesCompiled ((String -> CompiledRules () s l)
-> (CompiledRules () s l -> CompiledRules () s l)
-> Either String (CompiledRules () s l)
-> CompiledRules () s l
forall (a :: Kind) (c :: Kind) (b :: Kind).
(a -> c) -> (b -> c) -> Either a b -> c
either String -> CompiledRules () s l
forall a. HasCallStack => String -> a
error CompiledRules () s l -> CompiledRules () s l
forall (a :: Kind). a -> a
id ([((), Rule s l)] -> Either String (CompiledRules () s l)
forall (key :: Kind) (l :: Kind) (s :: Kind).
(Ord key, Eq l, DecidableIso s) =>
[(key, Rule s l)] -> Either String (CompiledRules key s l)
compileRules [((), Rule s l
r)])) a ~> b
host)
rewriteBatch
:: forall {s} l as bs
. (Eq l, DecidableIso s)
=> Rule s l
-> (Wires as :: OPENHG s l) ~> Wires bs
-> (Int, Wires as ~> (Wires bs :: OPENHG s l))
rewriteBatch :: forall {s :: Kind} (l :: Kind) (as :: [s]) (bs :: [s]).
(Eq l, DecidableIso s) =>
Rule s l -> (Wires as ~> Wires bs) -> (Int, Wires as ~> Wires bs)
rewriteBatch (Rule Graph s (LabelTest l)
lhs [l] -> Maybe (Graph s l)
instantiate) Wires as ~> Wires bs
host =
let g :: Graph s l
g = (Wires as ~> Wires bs) -> Graph s l
forall (s :: Kind) (l :: Kind) (a :: OPENHG s l) (b :: OPENHG s l).
(a ~> b) -> Graph s l
graph Wires as ~> Wires bs
host
candidates :: [(Graph s l, Match)]
candidates = [(Graph s l
rhs, Match
match) | (Match
match, [l]
labels) <- Graph s (LabelTest l) -> Graph s l -> [(Match, [l])]
forall (l :: Kind) (s :: Kind).
(Eq l, DecidableIso s) =>
Graph s (LabelTest l) -> Graph s l -> [(Match, [l])]
matches Graph s (LabelTest l)
lhs Graph s l
g, Just Graph s l
rhs <- [[l] -> Maybe (Graph s l)
instantiate [l]
labels]]
chosen :: [(Graph s l, Match)]
chosen = (Graph s l -> Match -> IntSet)
-> [(Graph s l, Match)] -> [(Graph s l, Match)]
forall (a :: Kind).
(a -> Match -> IntSet) -> [(a, Match)] -> [(a, Match)]
nonConflictingBy (\Graph s l
_ (IntMap Int
m, IntSet
_) -> Graph s (LabelTest l) -> IntMap Int -> IntSet
forall (s :: Kind) (l :: Kind). Graph s l -> IntMap Int -> IntSet
deletedImages Graph s (LabelTest l)
lhs IntMap Int
m) [(Graph s l, Match)]
candidates
in case [(Graph s l, Match)]
chosen of
[] -> (Int
0, Wires as ~> Wires bs
host)
[(Graph s l, Match)]
_ ->
Wires as ~> Wires bs
DecCospan (Wires as) (Wires bs)
host DecCospan (Wires as) (Wires bs)
-> ((Ob (Wires as), Ob (Wires bs)) =>
(Int, DecCospan (Wires as) (Wires bs)))
-> (Int, DecCospan (Wires as) (Wires bs))
forall {k1 :: Kind} {k2 :: Kind} (p :: k1 +-> k2) (a :: k2)
(b :: k1) (r :: Kind).
Profunctor p =>
p a b -> ((Ob a, Ob b) => r) -> r
//
let Graph [SomeSort s]
nodes [Int]
ins [Int]
outs [Box l Int]
boxes = Graph s l
-> [(Graph s (LabelTest l), Graph s l, Match)] -> Graph s l
forall (s :: Kind) (l :: Kind) (p :: Kind).
Graph s l -> [(Graph s p, Graph s l, Match)] -> Graph s l
replaceSelected Graph s l
g [(Graph s (LabelTest l)
lhs, Graph s l
rhs, Match
match) | (Graph s l
rhs, Match
match) <- [(Graph s l, Match)]
chosen]
in ([(Graph s l, Match)] -> Int
forall (a :: Kind). [a] -> Int
forall (t :: Kind -> Kind) (a :: Kind). Foldable t => t a -> Int
length [(Graph s l, Match)]
chosen, [SomeSort s]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
forall {s :: Kind} (l :: Kind) (as :: [s]) (bs :: [s]).
(SortList as, SortList bs) =>
[SomeSort s]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
unsafeOpenHypergraph [SomeSort s]
nodes [Int]
ins [Int]
outs [Box l Int]
boxes)
rewriteBatch Rule s l
r Wires as ~> Wires bs
host = CompiledRules () s l
-> (Wires as ~> Wires bs) -> (Int, Wires as ~> Wires bs)
forall {s :: Kind} (key :: Kind) (l :: Kind) (as :: [s])
(bs :: [s]).
(Eq l, DecidableIso s) =>
CompiledRules key s l
-> (Wires as ~> Wires bs) -> (Int, Wires as ~> Wires bs)
rewriteBatchCompiled ((String -> CompiledRules () s l)
-> (CompiledRules () s l -> CompiledRules () s l)
-> Either String (CompiledRules () s l)
-> CompiledRules () s l
forall (a :: Kind) (c :: Kind) (b :: Kind).
(a -> c) -> (b -> c) -> Either a b -> c
either String -> CompiledRules () s l
forall a. HasCallStack => String -> a
error CompiledRules () s l -> CompiledRules () s l
forall (a :: Kind). a -> a
id ([((), Rule s l)] -> Either String (CompiledRules () s l)
forall (key :: Kind) (l :: Kind) (s :: Kind).
(Ord key, Eq l, DecidableIso s) =>
[(key, Rule s l)] -> Either String (CompiledRules key s l)
compileRules [((), Rule s l
r)])) Wires as ~> Wires bs
host
compileRules
:: (Ord key, Eq l, DecidableIso s)
=> [(key, Rule s l)] -> Either String (CompiledRules key s l)
compileRules :: forall (key :: Kind) (l :: Kind) (s :: Kind).
(Ord key, Eq l, DecidableIso s) =>
[(key, Rule s l)] -> Either String (CompiledRules key s l)
compileRules = Bool -> [(key, Rule s l)] -> Either String (CompiledRules key s l)
forall (key :: Kind) (l :: Kind) (s :: Kind).
(Ord key, Eq l, DecidableIso s) =>
Bool -> [(key, Rule s l)] -> Either String (CompiledRules key s l)
compileRuleSet Bool
False
compileReducingRules
:: (Ord key, Eq l, DecidableIso s)
=> [(key, Rule s l)] -> Either String (CompiledRules key s l)
compileReducingRules :: forall (key :: Kind) (l :: Kind) (s :: Kind).
(Ord key, Eq l, DecidableIso s) =>
[(key, Rule s l)] -> Either String (CompiledRules key s l)
compileReducingRules = Bool -> [(key, Rule s l)] -> Either String (CompiledRules key s l)
forall (key :: Kind) (l :: Kind) (s :: Kind).
(Ord key, Eq l, DecidableIso s) =>
Bool -> [(key, Rule s l)] -> Either String (CompiledRules key s l)
compileRuleSet Bool
True
rewritesCompiled
:: forall {s} key l a b
. (Eq l, DecidableIso s)
=> CompiledRules key s l
-> (a :: OPENHG s l) ~> b
-> [(key, Wires (WireSorts a) ~> (Wires (WireSorts b) :: OPENHG s l))]
rewritesCompiled :: forall {s :: Kind} (key :: Kind) (l :: Kind) (a :: OPENHG s l)
(b :: OPENHG s l).
(Eq l, DecidableIso s) =>
CompiledRules key s l
-> (a ~> b) -> [(key, Wires (WireSorts a) ~> Wires (WireSorts b))]
rewritesCompiled compiled :: CompiledRules key s l
compiled@(CompiledRules Vector (key, Rule s l)
rules Vector (Signature l)
_ Net s
_ Bool
_) a ~> b
host =
a ~> b
DecCospan a b
host DecCospan a b
-> ((Ob a, Ob b) =>
[(key,
DecCospan
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b))))))))])
-> [(key,
DecCospan
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC a)))))))
(DC (SUB (FH (Port (PortSorts (UN SUB (UN DC b))))))))]
forall {k1 :: Kind} {k2 :: Kind} (p :: k1 +-> k2) (a :: k2)
(b :: k1) (r :: Kind).
Profunctor p =>
p a b -> ((Ob a, Ob b) => r) -> r
//
[ (key
key, [SomeSort s]
-> [Int]
-> [Int]
-> [Box l Int]
-> Wires (PortSorts (FH (UN FH (UN SUB (UN DC a)))))
~> Wires (PortSorts (FH (UN FH (UN SUB (UN DC b)))))
forall {s :: Kind} (l :: Kind) (as :: [s]) (bs :: [s]).
(SortList as, SortList bs) =>
[SomeSort s]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
unsafeOpenHypergraph [SomeSort s]
ns [Int]
ins [Int]
outs [Box l Int]
bs)
| (Int
i, (Graph s (LabelTest l)
lhs, Graph s l
rhs, Match
match)) <- CompiledRules key s l -> Graph s l -> [(Int, Prepared s l)]
forall (l :: Kind) (s :: Kind) (key :: Kind).
(Eq l, DecidableIso s) =>
CompiledRules key s l -> Graph s l -> [(Int, Prepared s l)]
compiledMatches CompiledRules key s l
compiled Graph s l
g
, let (key
key, Rule s l
_) = Vector (key, Rule s l)
rules Vector (key, Rule s l) -> Int -> (key, Rule s l)
forall (a :: Kind). Vector a -> Int -> a
V.! Int
i
Graph [SomeSort s]
ns [Int]
ins [Int]
outs [Box l Int]
bs = Graph s (LabelTest l)
-> Graph s l -> Graph s l -> [Match] -> Graph s l
forall (s :: Kind) (p :: Kind) (l :: Kind).
Graph s p -> Graph s l -> Graph s l -> [Match] -> Graph s l
replaceMany Graph s (LabelTest l)
lhs Graph s l
rhs Graph s l
g [Match
match]
]
where
g :: Graph s l
g = (a ~> b) -> Graph s l
forall (s :: Kind) (l :: Kind) (a :: OPENHG s l) (b :: OPENHG s l).
(a ~> b) -> Graph s l
graph a ~> b
host
rewriteBatchCompiled
:: forall {s} key l as bs
. (Eq l, DecidableIso s)
=> CompiledRules key s l
-> (Wires as :: OPENHG s l) ~> Wires bs
-> (Int, Wires as ~> (Wires bs :: OPENHG s l))
rewriteBatchCompiled :: forall {s :: Kind} (key :: Kind) (l :: Kind) (as :: [s])
(bs :: [s]).
(Eq l, DecidableIso s) =>
CompiledRules key s l
-> (Wires as ~> Wires bs) -> (Int, Wires as ~> Wires bs)
rewriteBatchCompiled CompiledRules key s l
compiled Wires as ~> Wires bs
host =
let g :: Graph s l
g = (Wires as ~> Wires bs) -> Graph s l
forall (s :: Kind) (l :: Kind) (a :: OPENHG s l) (b :: OPENHG s l).
(a ~> b) -> Graph s l
graph Wires as ~> Wires bs
host
chosen :: [Prepared s l]
chosen = CompiledRules key s l -> Graph s l -> [Prepared s l]
forall (l :: Kind) (s :: Kind) (key :: Kind).
(Eq l, DecidableIso s) =>
CompiledRules key s l -> Graph s l -> [Prepared s l]
batchMatches CompiledRules key s l
compiled Graph s l
g
in case [Prepared s l]
chosen of
[] -> (Int
0, Wires as ~> Wires bs
host)
[Prepared s l]
_ ->
Wires as ~> Wires bs
DecCospan (Wires as) (Wires bs)
host DecCospan (Wires as) (Wires bs)
-> ((Ob (Wires as), Ob (Wires bs)) =>
(Int, DecCospan (Wires as) (Wires bs)))
-> (Int, DecCospan (Wires as) (Wires bs))
forall {k1 :: Kind} {k2 :: Kind} (p :: k1 +-> k2) (a :: k2)
(b :: k1) (r :: Kind).
Profunctor p =>
p a b -> ((Ob a, Ob b) => r) -> r
//
let Graph [SomeSort s]
ns [Int]
ins [Int]
outs [Box l Int]
bs = Graph s l -> [Prepared s l] -> Graph s l
forall (s :: Kind) (l :: Kind) (p :: Kind).
Graph s l -> [(Graph s p, Graph s l, Match)] -> Graph s l
replaceSelected Graph s l
g [Prepared s l]
chosen
in ([Prepared s l] -> Int
forall (a :: Kind). [a] -> Int
forall (t :: Kind -> Kind) (a :: Kind). Foldable t => t a -> Int
length [Prepared s l]
chosen, [SomeSort s]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
forall {s :: Kind} (l :: Kind) (as :: [s]) (bs :: [s]).
(SortList as, SortList bs) =>
[SomeSort s]
-> [Int] -> [Int] -> [Box l Int] -> Wires as ~> Wires bs
unsafeOpenHypergraph [SomeSort s]
ns [Int]
ins [Int]
outs [Box l Int]
bs)
normalise
:: forall {s} key l as bs
. (Eq l, DecidableIso s)
=> RewriteLimits
-> CompiledRules key s l
-> (Wires as :: OPENHG s l) ~> Wires bs
-> NormalisationResult (Wires as ~> (Wires bs :: OPENHG s l))
normalise :: forall {s :: Kind} (key :: Kind) (l :: Kind) (as :: [s])
(bs :: [s]).
(Eq l, DecidableIso s) =>
RewriteLimits
-> CompiledRules key s l
-> (Wires as ~> Wires bs)
-> NormalisationResult (Wires as ~> Wires bs)
normalise RewriteLimits
limits CompiledRules key s l
compiled = RewriteLimits
-> (Graph s l -> [Prepared s l])
-> (DC (SUB (FH (Port as))) ~> DC (SUB (FH (Port bs))))
-> NormalisationResult
(DC (SUB (FH (Port as))) ~> DC (SUB (FH (Port bs))))
forall {s :: Kind} (l :: Kind) (as :: [s]) (bs :: [s]).
RewriteLimits
-> (Graph s l -> [Prepared s l])
-> (Wires as ~> Wires bs)
-> NormalisationResult (Wires as ~> Wires bs)
normaliseBy RewriteLimits
limits (CompiledRules key s l -> Graph s l -> [Prepared s l]
forall (l :: Kind) (s :: Kind) (key :: Kind).
(Eq l, DecidableIso s) =>
CompiledRules key s l -> Graph s l -> [Prepared s l]
batchMatches CompiledRules key s l
compiled)
batchMatches :: (Eq l, DecidableIso s) => CompiledRules key s l -> Graph s l -> [Prepared s l]
batchMatches :: forall (l :: Kind) (s :: Kind) (key :: Kind).
(Eq l, DecidableIso s) =>
CompiledRules key s l -> Graph s l -> [Prepared s l]
batchMatches CompiledRules key s l
compiled Graph s l
g =
[ (Graph s (LabelTest l)
lhs, Graph s l
rhs, Match
match)
| ((Graph s (LabelTest l)
lhs, Graph s l
rhs), Match
match) <-
((Graph s (LabelTest l), Graph s l) -> Match -> IntSet)
-> [((Graph s (LabelTest l), Graph s l), Match)]
-> [((Graph s (LabelTest l), Graph s l), Match)]
forall (a :: Kind).
(a -> Match -> IntSet) -> [(a, Match)] -> [(a, Match)]
nonConflictingBy
(\(Graph s (LabelTest l)
lhs, Graph s l
_) (IntMap Int
m, IntSet
_) -> Graph s (LabelTest l) -> IntMap Int -> IntSet
forall (s :: Kind) (l :: Kind). Graph s l -> IntMap Int -> IntSet
deletedImages Graph s (LabelTest l)
lhs IntMap Int
m)
[((Graph s (LabelTest l)
lhs, Graph s l
rhs), Match
match) | (Int
_, (Graph s (LabelTest l)
lhs, Graph s l
rhs, Match
match)) <- CompiledRules key s l -> Graph s l -> [(Int, Prepared s l)]
forall (l :: Kind) (s :: Kind) (key :: Kind).
(Eq l, DecidableIso s) =>
CompiledRules key s l -> Graph s l -> [(Int, Prepared s l)]
compiledMatches CompiledRules key s l
compiled Graph s l
g]
]