module Language.Syntactic.Traversal
( gmapQ
, gmapT
, everywhereUp
, everywhereDown
, Args (..)
, listArgs
, mapArgs
, mapArgsA
, mapArgsM
, appArgs
, listFold
, match
, query
, simpleMatch
, fold
, simpleFold
, matchTrans
, WrapFull (..)
, toTree
) where
import Control.Applicative
import Data.Tree
import Language.Syntactic.Syntax
gmapT :: forall dom
. (forall a . ASTF dom a -> ASTF dom a)
-> (forall a . ASTF dom a -> ASTF dom a)
gmapT f a = go a
where
go :: forall a . AST dom a -> AST dom a
go (s :$ a) = go s :$ f a
go s = s
gmapQ :: forall dom b
. (forall a . ASTF dom a -> b)
-> (forall a . ASTF dom a -> [b])
gmapQ f a = go a
where
go :: forall a . AST dom a -> [b]
go (s :$ a) = f a : go s
go _ = []
everywhereUp
:: (forall a . ASTF dom a -> ASTF dom a)
-> (forall a . ASTF dom a -> ASTF dom a)
everywhereUp f = f . gmapT (everywhereUp f)
everywhereDown
:: (forall a . ASTF dom a -> ASTF dom a)
-> (forall a . ASTF dom a -> ASTF dom a)
everywhereDown f = gmapT (everywhereDown f) . f
data Args c sig
where
Nil :: Args c (Full a)
(:*) :: c (Full a) -> Args c sig -> Args c (a :-> sig)
infixr :*
listArgs :: (forall a . c (Full a) -> b) -> Args c sig -> [b]
listArgs f Nil = []
listArgs f (a :* as) = f a : listArgs f as
mapArgs
:: (forall a . c1 (Full a) -> c2 (Full a))
-> (forall sig . Args c1 sig -> Args c2 sig)
mapArgs f Nil = Nil
mapArgs f (a :* as) = f a :* mapArgs f as
mapArgsA :: Applicative f
=> (forall a . c1 (Full a) -> f (c2 (Full a)))
-> (forall sig . Args c1 sig -> f (Args c2 sig))
mapArgsA f Nil = pure Nil
mapArgsA f (a :* as) = (:*) <$> f a <*> mapArgsA f as
mapArgsM :: Monad m
=> (forall a . c1 (Full a) -> m (c2 (Full a)))
-> (forall sig . Args c1 sig -> m (Args c2 sig))
mapArgsM f = unwrapMonad . mapArgsA (WrapMonad . f)
foldrArgs
:: (forall a . c (Full a) -> b -> b)
-> b
-> (forall sig . Args c sig -> b)
foldrArgs f b Nil = b
foldrArgs f b (a :* as) = f a (foldrArgs f b as)
appArgs :: AST dom sig -> Args (AST dom) sig -> ASTF dom (DenResult sig)
appArgs a Nil = a
appArgs s (a :* as) = appArgs (s :$ a) as
match :: forall dom a c
. ( forall sig . (a ~ DenResult sig) =>
dom sig -> Args (AST dom) sig -> c (Full a)
)
-> ASTF dom a
-> c (Full a)
match f a = go a Nil
where
go :: (a ~ DenResult sig) => AST dom sig -> Args (AST dom) sig -> c (Full a)
go (Sym a) as = f a as
go (s :$ a) as = go s (a :* as)
query :: forall dom a c
. ( forall sig . (a ~ DenResult sig) =>
dom sig -> Args (AST dom) sig -> c (Full a)
)
-> ASTF dom a
-> c (Full a)
query = match
simpleMatch :: forall dom a b
. (forall sig . (a ~ DenResult sig) => dom sig -> Args (AST dom) sig -> b)
-> ASTF dom a
-> b
simpleMatch f = getConst . match (\s -> Const . f s)
fold :: forall dom c
. (forall sig . dom sig -> Args c sig -> c (Full (DenResult sig)))
-> (forall a . ASTF dom a -> c (Full a))
fold f = match (\s -> f s . mapArgs (fold f))
simpleFold :: forall dom b
. (forall sig . dom sig -> Args (Const b) sig -> b)
-> (forall a . ASTF dom a -> b)
simpleFold f = getConst . fold (\s -> Const . f s)
listFold :: forall dom b
. (forall sig . dom sig -> [b] -> b)
-> (forall a . ASTF dom a -> b)
listFold f = simpleFold (\s -> f s . listArgs getConst)
newtype WrapAST c dom sig = WrapAST { unWrapAST :: c (AST dom sig) }
matchTrans :: forall dom dom' c a
. ( forall sig . (a ~ DenResult sig) =>
dom sig -> Args (AST dom) sig -> c (ASTF dom' a)
)
-> ASTF dom a
-> c (ASTF dom' a)
matchTrans f = unWrapAST . match (\s -> WrapAST . f s)
data WrapFull c a
where
WrapFull :: { unwrapFull :: c a } -> WrapFull c (Full a)
toTree :: forall dom a b . (forall sig . dom sig -> b) -> ASTF dom a -> Tree b
toTree f = listFold (Node . f)