module Data.Rewriting.Rule.Ops (
funs,
funsDL,
vars,
varsDL,
left,
right,
rename,
both,
isLinear, isLeftLinear, isRightLinear,
isGround, isLeftGround, isRightGround,
isErasing,
isCreating,
isDuplicating,
isCollapsing,
isExpanding,
isValid,
isInstanceOf,
isVariantOf,
) where
import Data.Rewriting.Rule.Type
import Data.Rewriting.Substitution (match, merge)
import qualified Data.Rewriting.Term as Term
import qualified Data.Set as S
import qualified Data.MultiSet as MS
import Data.Maybe
both :: (Term f v -> Bool) -> Rule f v -> Bool
both :: forall f v. (Term f v -> Bool) -> Rule f v -> Bool
both Term f v -> Bool
p Rule f v
r = Term f v -> Bool
p (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
lhs Rule f v
r) Bool -> Bool -> Bool
&& Term f v -> Bool
p (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
rhs Rule f v
r)
left :: (Term f v -> a) -> Rule f v -> a
left :: forall f v a. (Term f v -> a) -> Rule f v -> a
left Term f v -> a
f = Term f v -> a
f (Term f v -> a) -> (Rule f v -> Term f v) -> Rule f v -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rule f v -> Term f v
forall f v. Rule f v -> Term f v
lhs
right :: (Term f v -> a) -> Rule f v -> a
right :: forall f v a. (Term f v -> a) -> Rule f v -> a
right Term f v -> a
f = Term f v -> a
f (Term f v -> a) -> (Rule f v -> Term f v) -> Rule f v -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rule f v -> Term f v
forall f v. Rule f v -> Term f v
rhs
rename :: (v -> v') -> Rule f v -> Rule f v'
rename :: forall v v' f. (v -> v') -> Rule f v -> Rule f v'
rename v -> v'
f Rule f v
rl = Term f v' -> Term f v' -> Rule f v'
forall f v. Term f v -> Term f v -> Rule f v
Rule ((Term f v -> Term f v') -> Rule f v -> Term f v'
forall f v a. (Term f v -> a) -> Rule f v -> a
left ((v -> v') -> Term f v -> Term f v'
forall v v' f. (v -> v') -> Term f v -> Term f v'
Term.rename v -> v'
f) Rule f v
rl) ((Term f v -> Term f v') -> Rule f v -> Term f v'
forall f v a. (Term f v -> a) -> Rule f v -> a
right ((v -> v') -> Term f v -> Term f v'
forall v v' f. (v -> v') -> Term f v -> Term f v'
Term.rename v -> v'
f) Rule f v
rl)
funs :: Rule f v -> [f]
funs :: forall f v. Rule f v -> [f]
funs = (Rule f v -> [f] -> [f]) -> [f] -> Rule f v -> [f]
forall a b c. (a -> b -> c) -> b -> a -> c
flip Rule f v -> [f] -> [f]
forall f v. Rule f v -> [f] -> [f]
funsDL []
funsDL :: Rule f v -> [f] -> [f]
funsDL :: forall f v. Rule f v -> [f] -> [f]
funsDL Rule f v
r = Term f v -> [f] -> [f]
forall f v. Term f v -> [f] -> [f]
Term.funsDL (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
lhs Rule f v
r) ([f] -> [f]) -> ([f] -> [f]) -> [f] -> [f]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term f v -> [f] -> [f]
forall f v. Term f v -> [f] -> [f]
Term.funsDL (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
rhs Rule f v
r)
vars :: Rule f v -> [v]
vars :: forall f v. Rule f v -> [v]
vars = (Rule f v -> [v] -> [v]) -> [v] -> Rule f v -> [v]
forall a b c. (a -> b -> c) -> b -> a -> c
flip Rule f v -> [v] -> [v]
forall f v. Rule f v -> [v] -> [v]
varsDL []
varsDL :: Rule f v -> [v] -> [v]
varsDL :: forall f v. Rule f v -> [v] -> [v]
varsDL Rule f v
r = Term f v -> [v] -> [v]
forall f v. Term f v -> [v] -> [v]
Term.varsDL (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
lhs Rule f v
r) ([v] -> [v]) -> ([v] -> [v]) -> [v] -> [v]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term f v -> [v] -> [v]
forall f v. Term f v -> [v] -> [v]
Term.varsDL (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
rhs Rule f v
r)
isLinear :: Ord v => Rule f v -> Bool
isLinear :: forall v f. Ord v => Rule f v -> Bool
isLinear = (Term f v -> Bool) -> Rule f v -> Bool
forall f v. (Term f v -> Bool) -> Rule f v -> Bool
both Term f v -> Bool
forall v f. Ord v => Term f v -> Bool
Term.isLinear
isLeftLinear :: Ord v => Rule f v -> Bool
isLeftLinear :: forall v f. Ord v => Rule f v -> Bool
isLeftLinear = (Term f v -> Bool) -> Rule f v -> Bool
forall f v a. (Term f v -> a) -> Rule f v -> a
left Term f v -> Bool
forall v f. Ord v => Term f v -> Bool
Term.isLinear
isRightLinear :: Ord v => Rule f v -> Bool
isRightLinear :: forall v f. Ord v => Rule f v -> Bool
isRightLinear = (Term f v -> Bool) -> Rule f v -> Bool
forall f v a. (Term f v -> a) -> Rule f v -> a
right Term f v -> Bool
forall v f. Ord v => Term f v -> Bool
Term.isLinear
isGround :: Rule f v -> Bool
isGround :: forall f v. Rule f v -> Bool
isGround = (Term f v -> Bool) -> Rule f v -> Bool
forall f v. (Term f v -> Bool) -> Rule f v -> Bool
both Term f v -> Bool
forall f v. Term f v -> Bool
Term.isGround
isLeftGround :: Rule f v -> Bool
isLeftGround :: forall f v. Rule f v -> Bool
isLeftGround = (Term f v -> Bool) -> Rule f v -> Bool
forall f v a. (Term f v -> a) -> Rule f v -> a
left Term f v -> Bool
forall f v. Term f v -> Bool
Term.isGround
isRightGround :: Rule f v -> Bool
isRightGround :: forall f v. Rule f v -> Bool
isRightGround = (Term f v -> Bool) -> Rule f v -> Bool
forall f v a. (Term f v -> a) -> Rule f v -> a
right Term f v -> Bool
forall f v. Term f v -> Bool
Term.isGround
varsS :: Ord v => Term f v -> S.Set v
= [v] -> Set v
forall a. Ord a => [a] -> Set a
S.fromList ([v] -> Set v) -> (Term f v -> [v]) -> Term f v -> Set v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term f v -> [v]
forall f v. Term f v -> [v]
Term.vars
isErasing :: Ord v => Rule f v -> Bool
isErasing :: forall v f. Ord v => Rule f v -> Bool
isErasing Rule f v
r = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Term f v -> Set v
forall v f. Ord v => Term f v -> Set v
varsS (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
lhs Rule f v
r) Set v -> Set v -> Bool
forall a. Ord a => Set a -> Set a -> Bool
`S.isSubsetOf` Term f v -> Set v
forall v f. Ord v => Term f v -> Set v
varsS (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
rhs Rule f v
r)
isCreating :: Ord v => Rule f v -> Bool
isCreating :: forall v f. Ord v => Rule f v -> Bool
isCreating Rule f v
r = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Term f v -> Set v
forall v f. Ord v => Term f v -> Set v
varsS (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
rhs Rule f v
r) Set v -> Set v -> Bool
forall a. Ord a => Set a -> Set a -> Bool
`S.isSubsetOf` Term f v -> Set v
forall v f. Ord v => Term f v -> Set v
varsS (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
lhs Rule f v
r)
varsMS :: Ord v => Term f v -> MS.MultiSet v
varsMS :: forall v f. Ord v => Term f v -> MultiSet v
varsMS = [v] -> MultiSet v
forall a. Ord a => [a] -> MultiSet a
MS.fromList ([v] -> MultiSet v) -> (Term f v -> [v]) -> Term f v -> MultiSet v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term f v -> [v]
forall f v. Term f v -> [v]
Term.vars
isDuplicating :: Ord v => Rule f v -> Bool
isDuplicating :: forall v f. Ord v => Rule f v -> Bool
isDuplicating Rule f v
r = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Term f v -> MultiSet v
forall v f. Ord v => Term f v -> MultiSet v
varsMS (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
rhs Rule f v
r) MultiSet v -> MultiSet v -> Bool
forall a. Ord a => MultiSet a -> MultiSet a -> Bool
`MS.isSubsetOf` Term f v -> MultiSet v
forall v f. Ord v => Term f v -> MultiSet v
varsMS (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
lhs Rule f v
r)
isCollapsing :: Rule f v -> Bool
isCollapsing :: forall f v. Rule f v -> Bool
isCollapsing = Term f v -> Bool
forall f v. Term f v -> Bool
Term.isVar (Term f v -> Bool) -> (Rule f v -> Term f v) -> Rule f v -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rule f v -> Term f v
forall f v. Rule f v -> Term f v
rhs
isExpanding :: Rule f v -> Bool
isExpanding :: forall f v. Rule f v -> Bool
isExpanding = Term f v -> Bool
forall f v. Term f v -> Bool
Term.isVar (Term f v -> Bool) -> (Rule f v -> Term f v) -> Rule f v -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rule f v -> Term f v
forall f v. Rule f v -> Term f v
lhs
isValid :: Ord v => Rule f v -> Bool
isValid :: forall v f. Ord v => Rule f v -> Bool
isValid Rule f v
r = Bool -> Bool
not (Rule f v -> Bool
forall v f. Ord v => Rule f v -> Bool
isCreating Rule f v
r) Bool -> Bool -> Bool
&& Bool -> Bool
not (Rule f v -> Bool
forall f v. Rule f v -> Bool
isExpanding Rule f v
r)
isInstanceOf :: (Eq f, Ord v, Ord v') => Rule f v -> Rule f v' -> Bool
isInstanceOf :: forall f v v'.
(Eq f, Ord v, Ord v') =>
Rule f v -> Rule f v' -> Bool
isInstanceOf Rule f v
r Rule f v'
r' = case (Term f v' -> Term f v -> Maybe (GSubst v' f v)
forall f v v'.
(Eq f, Ord v, Eq v') =>
Term f v -> Term f v' -> Maybe (GSubst v f v')
match (Rule f v' -> Term f v'
forall f v. Rule f v -> Term f v
lhs Rule f v'
r') (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
lhs Rule f v
r), Term f v' -> Term f v -> Maybe (GSubst v' f v)
forall f v v'.
(Eq f, Ord v, Eq v') =>
Term f v -> Term f v' -> Maybe (GSubst v f v')
match (Rule f v' -> Term f v'
forall f v. Rule f v -> Term f v
rhs Rule f v'
r') (Rule f v -> Term f v
forall f v. Rule f v -> Term f v
rhs Rule f v
r)) of
(Just GSubst v' f v
s, Just GSubst v' f v
s') -> Maybe (GSubst v' f v) -> Bool
forall a. Maybe a -> Bool
isJust (GSubst v' f v -> GSubst v' f v -> Maybe (GSubst v' f v)
forall v f v'.
(Ord v, Eq f, Eq v') =>
GSubst v f v' -> GSubst v f v' -> Maybe (GSubst v f v')
merge GSubst v' f v
s GSubst v' f v
s')
(Maybe (GSubst v' f v), Maybe (GSubst v' f v))
_ -> Bool
False
isVariantOf :: (Eq f, Ord v, Ord v') => Rule f v -> Rule f v' -> Bool
isVariantOf :: forall f v v'.
(Eq f, Ord v, Ord v') =>
Rule f v -> Rule f v' -> Bool
isVariantOf Rule f v
t Rule f v'
u = Rule f v -> Rule f v' -> Bool
forall f v v'.
(Eq f, Ord v, Ord v') =>
Rule f v -> Rule f v' -> Bool
isInstanceOf Rule f v
t Rule f v'
u Bool -> Bool -> Bool
&& Rule f v' -> Rule f v -> Bool
forall f v v'.
(Eq f, Ord v, Ord v') =>
Rule f v -> Rule f v' -> Bool
isInstanceOf Rule f v'
u Rule f v
t