[project @ 2003-07-08 15:46:40 by panne]
[haskell-directory.git] / Data / Generics.hs
index 0f79a34..c14d965 100644 (file)
@@ -9,7 +9,7 @@
 -- Portability :  non-portable
 --
 -- Generic programming in Haskell; 
--- see <http://www.cs.vu.nl/boilerplate>.
+-- see <http://www.cs.vu.nl/boilerplate/>.
 --
 -----------------------------------------------------------------------------
 
@@ -51,6 +51,7 @@ module Data.Generics (
         everywhereM,
         somewhere,
        everything,
+       listify,
         something,
        synthesize,
 
@@ -106,7 +107,7 @@ import Control.Monad
 type GenericT = forall a. Data a => a -> a
 
 
--- | Generic queries of type "r",
+-- | Generic queries of type \"r\",
 --   i.e., take any \"a\" and return an \"r\"
 --
 type GenericQ r = forall a. Data a => a -> r
@@ -173,7 +174,7 @@ use a point-free style whenever possible.
 
 
 -- | Make a generic monadic transformation for MonadPlus;
---   use "const mzero" (i.e., failure) instead of return as default.
+--   use \"const mzero\" (i.e., failure) instead of return as default.
 --
 mkF :: (Typeable a, Typeable b, Typeable (m a), Typeable (m b), MonadPlus m)
     => (b -> m b) -> a -> m a
@@ -509,7 +510,13 @@ everything :: (r -> r -> r) -> GenericQ r -> GenericQ r
 -- use ordinary foldl to reduce list of intermediate results
 -- 
 everything k f x 
-     = foldl k (f x) (gmapQ (everything k f) x)
+  = foldl k (f x) (gmapQ (everything k f) x)
+
+
+-- | Get a list of all entities that meet a predicate
+listify :: Typeable r => (r -> Bool) -> GenericQ [r]
+listify p
+  = everything (++) ([] `mkQ` (\x -> if p x then [x] else []))
 
 
 -- | Look up a subterm by means of a maybe-typed filter
@@ -636,7 +643,7 @@ gtypecount :: Typeable a => (a -> ()) -> GenericQ Int
 gtypecount f = gcount (False `mkQ` (const True . f))
 
 
--- | Generic show: an alternative to "deriving Show"
+-- | Generic show: an alternative to \"deriving Show\"
 gshow :: Data a => a -> String
 
 -- This is a prefix-show using surrounding "(" and ")",
@@ -650,7 +657,7 @@ gshow = ( \t ->
         ) `extQ` (show :: String -> String)
 
 
--- | Generic equality: an alternative to "deriving Eq"
+-- | Generic equality: an alternative to \"deriving Eq\"
 geq :: Data a => a -> a -> Bool
 
 {-
@@ -695,7 +702,7 @@ newtype GRead i a = GRead (i -> Maybe (a, i))
 unGRead (GRead x) = x
 
 
--- | Generic read: an alternative to "deriving Read"
+-- | Generic read: an alternative to \"deriving Read\"
 gread :: GenericB Maybe String
 
 {-