Added an augmented version of Adam's gmapMFor that uses the same generics techniques, but also gives a route to the node to the transformation function

This commit is contained in:
Neil Brown 2008-11-06 18:32:35 +00:00
parent 31091a5795
commit d12b2178de
2 changed files with 31 additions and 0 deletions

View File

@ -27,8 +27,11 @@ module GenericUtils (
, TypeSet, makeTypeSet
, containsTypes
, gmapMFor
, gmapMForRoute
) where
import Control.Monad.Identity()
import Control.Monad.State
import Data.Generics
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
@ -39,6 +42,7 @@ import Data.Typeable
import System.IO.Unsafe
import qualified AST as A
import TreeUtils
import Utils
-- | A type identifier.
@ -153,3 +157,29 @@ gmapMFor typeset f = gmapM (each f)
Just Through -> gmapM (each f) x
Just Miss -> return x
Nothing -> return x
gmapMForRoute :: forall m t. (Monad m, Data t) =>
TypeSet ->
(forall s. Data s => (s, (forall n. Monad n => (s -> n s) -> (t -> n t))) -> m s)
-> (t -> m t)
gmapMForRoute typeset f = gmapMWithRoute (each f)
where
each :: Data u => (forall s. Data s => (s, (forall n. Monad n => (s -> n s) -> (t -> n t))) -> m s)
-> ((u, (forall n. Monad n => (u -> n u) -> (t -> n t))) -> m u)
each f (x, route)
= case IntMap.lookup (typeKey x) typeset of
Just Hit -> f (x, route)
Just Through -> gmapMWithRoute (\(y, route') -> each f (y, route . route')) x
Just Miss -> return x
Nothing -> return x
gmapMWithRoute :: forall a m. (Monad m, Data a) => (forall b. Data b => (b, (forall n. Monad n => (b -> n b) ->
(a -> n a))) -> m b) -> a -> m a
gmapMWithRoute f = gmapFuncs [GM {unGM = f' n} | n <- [0..]]
where
f' :: Int -> (forall b. Data b => b -> m b)
f' n x = f (x, makeRoute n)
-- Given a number, makes a route function for that child:
makeRoute :: (Data s, Data t, Monad m) => Int -> (s -> m s) -> (t -> m t)
makeRoute target f = gmapFuncs [mkM' (if n == target then f else return) | n <- [0..]]

View File

@ -26,6 +26,7 @@ module TreeUtils (
checkTreeForConstr,
con0, con1, con2, con3, con4, con5, con6, con7
, decomp1, decomp2, decomp3, decomp4, decomp5
, mkM', gmapFuncs
) where
import Control.Monad.State