module Data.Rewriting.Rules where
import Control.Applicative (pure)
import qualified Data.Foldable as Fold
import Data.Traversable (Traversable (traverse))
import Data.Comp
import Data.Comp.Derive
import Data.Comp.Ops
import Data.Patch
data Rule lhs rhs
where
Rule :: lhs a -> rhs a -> Rule lhs rhs
(===>) :: lhs a -> rhs a -> Rule lhs rhs
(===>) = Rule
infix 1 ===>
class WildCard r
where
__ :: r a
data MetaExp r a
where
MVar :: MetaRep r a -> MetaExp r a
MApp :: MetaExp r (a -> b) -> MetaArg r a -> MetaExp r b
class MetaVar r
where
type MetaRep r :: * -> *
type MetaArg r :: * -> *
metaExp :: MetaExp r a -> r a
mvar :: MetaVar r => MetaRep r a -> r a
mvar = metaExp . MVar
($$) :: MetaExp r (a -> b) -> MetaArg r a -> MetaExp r b
($$) = MApp
($-) :: MetaVar r => MetaExp r (a -> b) -> MetaArg r a -> r b
f $- a = metaExp (MApp f a)
(-$) :: MetaRep r (a -> b) -> MetaArg r a -> MetaExp r b
f -$ a = MApp (MVar f) a
(-$-) :: MetaVar r => MetaRep r (a -> b) -> MetaArg r a -> r b
f -$- a = metaExp (MApp (MVar f) a)
infixl 2 $$, $-, -$, -$-
type Name = Integer
newtype MetaId a = MetaId Name
deriving (Eq, Show, Ord, Num, Enum, Real, Integral)
class Quantifiable rule
where
type RuleType rule
quantify' :: Name -> rule -> RuleType rule
instance Quantifiable (Rule lhs rhs)
where
type RuleType (Rule lhs rhs) = Rule lhs rhs
quantify' _ = id
instance (Quantifiable rule, m ~ MetaId a) => Quantifiable (m -> rule)
where
type RuleType (m -> rule) = RuleType rule
quantify' i rule = quantify' (i+1) (rule (MetaId i))
quantify :: (Quantifiable rule, RuleType rule ~ Rule lhs rhs) => rule -> Rule lhs rhs
quantify = quantify' 0
data WILD a = WildCard
deriving (Eq, Show, Functor, Foldable, Traversable)
data META r a = forall b . Meta (MetaExp r b)
instance Functor (META r) where fmap f (Meta m) = Meta m
instance Foldable (META r) where foldr _ a _ = a
instance Traversable (META r) where traverse _ (Meta m) = pure (Meta m)
newtype LHS f a = LHS { unLHS :: Term (WILD :+: META (LHS f) :+: f) }
newtype RHS f a = RHS { unRHS :: Term (META (RHS f) :+: f) }
instance WildCard (LHS f)
where
__ = LHS $ Term $ Inl WildCard
type family Var (r :: * -> *) :: * -> *
instance MetaVar (LHS f)
where
type MetaRep (LHS f) = MetaId
type MetaArg (LHS f) = Var (LHS f)
metaExp = LHS . Term . Inr . Inl . Meta
instance MetaVar (RHS f)
where
type MetaRep (RHS f) = MetaId
type MetaArg (RHS f) = RHS f
metaExp = RHS . Term . Inl . Meta
class Rep r
where
type PF r :: * -> *
toRep :: Term (PF r) -> r a
fromRep :: r a -> Term (PF r)
instance Rep (LHS f)
where
type PF (LHS f) = WILD :+: META (LHS f) :+: f
toRep = LHS
fromRep = unLHS
instance Rep (RHS f)
where
type PF (RHS f) = META (RHS f) :+: f
toRep = RHS
fromRep = unRHS
tRule :: Patch (Rule (LHS f) (RHS f)) (Rule (LHS f) (RHS f))
tRule = id
data A = A deriving (Eq)
data B = B deriving (Eq)
data C = C deriving (Eq)
tA :: Patch A A
tA = id
tB :: Patch B B
tB = id
tC :: Patch C C
tC = id
type Subst f = [(Name, Term f)]