{-# LANGUAGE UnicodeSyntax, TypeSynonymInstances, FlexibleInstances, MultiParamTypeClasses, FlexibleContexts #-}
-- | Rewrite rules are represented as nested monads: a 'Rule' is a 'Pattern' that returns a 'Rewrite' the latter directly defining the transformation of the graph.
--
-- For rule construction a few functions a provided: The most basic one is 'rewrite'. But in most cases 'erase', 'rewire', and 'replace*' should be more convenient. These functions express rewrites that /replace/ the matched nodes of the 'Pattern', which comes quite close to the @L -> R@ form in which graph rewriting rules are usually expressed.
module GraphRewriting.Rule where

import Prelude.Unicode
import GraphRewriting.Graph
import GraphRewriting.Graph.Internal (Port (Edge))
import GraphRewriting.Graph.Write
import GraphRewriting.Rule.Internal
import GraphRewriting.Pattern
import Control.Monad.State
import Control.Monad.Reader
import Data.List (nub)
import Data.Either


-- | A rewriting rule is defined as a 'Pattern' that returns a 'Rewrite'
type Rule n = Pattern n (Rewrite n ())

-- | Apply rule at an arbitrary position if applicable
apply  Rule n  Rewrite n ()
apply r = do
	contractions  liftM (evalPattern r) ask
	when (not $ null contractions) (head contractions >> return ())

-- rule construction ---------------------------------------------------------

-- | primitive rule construction with the matched nodes of the left hand side as a parameter
rewrite  (Match  Rewrite n a)  Rule n
rewrite r = do
	h  history
	return $ r h >> return ()

-- | constructs a rule that deletes all of the matched nodes from the graph
erase  View [Port] n  Rule n
erase = rewrite $ mapM_ deleteNode . nub

-- | Constructs a rule from a list of rewirings. Each rewiring specifies a list of hyperedges that are to be merged into a single hyperedge. All matched nodes of the left-hand side are removed.
rewire  View [Port] n  [[Edge]]  Rule n
rewire ess = rewrite $ \hist  do
	mapM_ mergeEs $ joinEdges ess
	mapM_ deleteNode $ nub hist

data RHS v = Node v | Wire Edge Edge | Merge [Edge]

-- | Constructs a rule that replaces the matched nodes of the left-hand side by new nodes and rewirings. It generates an amount of new edges specified by the 'Int'. In most cases the functions below named @replace*@ should be sufficient.
replace  (View [Port] n, View v n)  Int  ([Edge]  [RHS v])  Rule n
replace n rhs = do
	let vs = fst $ partition (replicate n $ Edge 0)
	lhsNodes  liftM nub history
	when (null lhsNodes  not (null vs)) (fail "need at least one matching node to clone new nodes from")
	return $ do
		es  replicateM n newEdge
		let (vs,ess) = partition es
		zipWithM_ copyNode (cycle lhsNodes) vs
		mapM_ mergeEs $ joinEdges ess
		mapM_ deleteNode lhsNodes
	where partition es = partitionEithers $ map splitRHS (rhs es) where
		splitRHS (Node v) = Left v
		splitRHS (Wire e1 e2) = Right [e1,e2]
		splitRHS (Merge es) = if length es < 2
			then error "Merge requires list length >= 2"
			else Right es

-- | Replaces the matched nodes by a list of new nodes and rewirings.
replace0 vs = replace 0 $ \[]                         vs
-- | Replaces the matched nodes by a list of new nodes and rewirings. It also generates one new edge.
replace1 vs = replace 1 $ \[e1]                       vs e1
-- | Replaces the matched nodes by a list of new nodes and rewirings. It also generates two new edges.
replace2 vs = replace 2 $ \[e1,e2]                    vs e1 e2
-- | You get the idea.
replace3 vs = replace 3 $ \[e1,e2,e3]                 vs e1 e2 e3
replace4 vs = replace 4 $ \[e1,e2,e3,e4]              vs e1 e2 e3 e4
replace5 vs = replace 5 $ \[e1,e2,e3,e4,e5]           vs e1 e2 e3 e4 e5
replace6 vs = replace 6 $ \[e1,e2,e3,e4,e5,e6]        vs e1 e2 e3 e4 e5 e6
replace7 vs = replace 7 $ \[e1,e2,e3,e4,e5,e6,e7]     vs e1 e2 e3 e4 e5 e6 e7
replace8 vs = replace 8 $ \[e1,e2,e3,e4,e5,e6,e7,e8]  vs e1 e2 e3 e4 e5 e6 e7 e8

-- combinators ---------------------------------------------------------------

-- | Apply two rules consecutively. Second rule is only applied if first one succeeds. Fails if (and only if) first rule fails.
(>>>)  Rule n  Rule n  Rule n
r1 >>> r2 = do
	rw1  r1
	return $ rw1 >> apply r2

-- | Make a rule exhaustive, i.e. such that (when applied) it reduces redexes until no redexes are occur in the graph.
exhaustive  Rule n  Rule n
exhaustive = foldr1 (>>>) . repeat

-- | Make a rule parallel, i.e. such that (when applied) all current redexes are contracted one by one. Neither new redexes or destroyed redexes are reduced.
everywhere  Rule n  Rule n
everywhere r = do
	ms  amnesia $ matches r
	exhaustive $ restrictOverlap (\hist future  future  ms) r