module Generics.Regular.Transformations.RewriteRules (
Transform, Transformation, apply
) where
import Generics.Regular
import Generics.Regular.Rewriting
import Generics.Regular.Zipper
import Control.Monad ( foldM )
class (Regular a, Rewrite a, Zipper (PF a)) => Transform a
instance Transform a => Rewrite a
type Transformation a = [ ( Loc a -> Maybe (Loc a)
, Rule a) ]
apply :: forall a. (Transform a) => Transformation a -> a -> Maybe a
apply rs = fmap leave . flip (foldM appRule) rs . enter
where appRule a (l,r) = l a >>= updateM (rewriteM r)