Abstract the set of types that gmapMFor takes out to a type.

(No change in behaviour, yet.)
This commit is contained in:
Adam Sampson 2008-04-08 23:13:58 +00:00
parent 441b2ee344
commit 0886ab9f9b
2 changed files with 13 additions and 2 deletions

View File

@ -24,6 +24,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
-- added to in the definition of 'contains').
module GenericUtils (
TypeKey, typeKey
, TypeSet, makeTypeSet
, containsTypes
, gmapMFor
) where
@ -101,11 +102,18 @@ containsTypes x targets
Just set -> target `IntSet.member` set
Nothing -> True -- can't tell, so it might be
-- | A set of type information for use by 'gmapMFor'.
type TypeSet = [TypeKey]
-- | Make a 'TypeSet' from a list of 'TypeKey's.
makeTypeSet :: [TypeKey] -> TypeSet
makeTypeSet tks = tks
-- | Type-smart generic mapM.
-- This is like 'gmapM', but it only applies the function to arguments that
-- could contain any of the target types.
gmapMFor :: (Monad m, Data t) =>
[TypeKey] -- ^ Target types
TypeSet -- ^ Target types
-> (forall s. Data s => s -> m s) -- ^ Function to apply
-> (t -> m t) -- ^ Generic operation
gmapMFor targets f = gmapM (each f)

View File

@ -79,8 +79,11 @@ extC info f = extD info (checkToTransform f)
applyX :: Data s => InfoX -> s -> PassM s
applyX info@(tks, g) = g doGeneric doGeneric
where
ts :: TypeSet
ts = makeTypeSet tks
doGeneric :: Data t => t -> PassM t
doGeneric = gmapMFor tks (applyX info)
doGeneric = gmapMFor ts (applyX info)
-- | Apply a transformation, recursing depth-first.
applyDepthM :: forall t1 s. (Data t1, Data s) =>