1 -----------------------------------------------------------------------------
3 -- Module : Data.Generics.Counts
4 -- Copyright : (c) The University of Glasgow, CWI 2001--2003
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable
11 -- "Scrap your boilerplate" --- Generic programming in Haskell
12 -- See <http://www.cs.vu.nl/boilerplate/>.
14 -----------------------------------------------------------------------------
16 module Data.Generics.Counts (
18 -- * Generic operations for counting terms
26 ------------------------------------------------------------------------------
29 import Data.Generics.Basics
30 import Data.Generics.Aliases
31 import Data.Generics.Schemes
34 ------------------------------------------------------------------------------
36 -- Generic operations for counting terms
38 ------------------------------------------------------------------------------
41 -- | Count the number of immediate subterms of the given term
42 glength :: GenericQ Int
43 glength = length . gmapL (const ())
46 -- | Determine the number of all suitable nodes in a given term
47 gcount :: GenericQ Bool -> GenericQ Int
48 gcount p = everything (+) (\x -> if p x then 1 else 0)
51 -- | Determine the number of all nodes in a given term
52 gnodecount :: GenericQ Int
53 gnodecount = gcount (const True)
56 -- | Determine the number of nodes of a given type in a given term
57 gtypecount :: Typeable a => (a -> ()) -> GenericQ Int
58 gtypecount f = gcount (False `mkQ` (const True . f))