packages feed

graph-rewriting-0.5: GraphRewriting/Pattern/Internal.hs

{-# LANGUAGE UnicodeSyntax #-}
module GraphRewriting.Pattern.Internal where

import GraphRewriting.Graph.Types
import Control.Monad.Reader


-- | A pattern represents a graph scrutinisation that memorises all the scrutinised nodes during matching.
newtype Pattern n a = Pattern {pattern ∷ Match → ReaderT (Graph n) [] (Match, a)}

type Match = [Node]

-- | Something like an implicit monadic map
branch ∷ [a] → Pattern n a
branch xs = Pattern $ \m → lift [([],x) | x ← xs]

visit ∷ Node → Pattern n ()
visit n = Pattern $ \m → lift [([n],())]

-- | From a graph scrutinisation returning a list of results make a 'Pattern' that branches on that list
liftList ∷ Reader (Graph n) [a] → Pattern n a
liftList r = Pattern $ \m → do
	xs ← liftM (runReader r) ask
	lift [([],x) | x ← xs]

-- | From a graph scrutinisation returning a list of nodes make a 'Pattern' that branches on these nodes.
-- For each branch the node matched is added to the history.
liftMatches ∷ Reader (Graph n) [Node] → Pattern n Node
liftMatches r = Pattern $ \m → do
	ns ← liftM (runReader r) ask
	lift [([n],n) | n ← ns]