module Data.Rewriting.Context.Ops (
apply,
compose,
ofTerm,
) where
import Control.Monad
import Data.Rewriting.Pos
import Data.Rewriting.Term.Type
import Data.Rewriting.Context.Type
apply :: Ctxt f v -> Term f v -> Term f v
apply Hole t = t
apply (Ctxt f ts1 ctxt ts2) t = Fun f (ts1 ++ apply ctxt t : ts2)
compose :: Ctxt f v -> Ctxt f v -> Ctxt f v
compose Hole c2 = c2
compose (Ctxt f ts1 c1 ts2) c2 = Ctxt f ts1 (c1 `compose` c2) ts2
ofTerm :: Term f v -> Pos -> Maybe (Ctxt f v)
ofTerm _ [] = Just Hole
ofTerm (Fun f ts) (i:p) = do
guard (i >= 0 && i < length ts)
let (ts1, t:ts2) = splitAt i ts
ctxt <- ofTerm t p
return (Ctxt f ts1 ctxt ts2)
ofTerm _ _ = Nothing