1 -----------------------------------------------------------------------------
3 -- Module : Data.Generics.Types
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.Types (
18 -- * Generic operations to reify types
25 ------------------------------------------------------------------------------
29 import Data.Generics.Basics
30 import Data.Generics.Aliases
31 import Data.Generics.Counts
35 -- Generic type functions,
36 -- i.e., functions mapping types to values
38 type GTypeFun r = forall a. Typeable a => TypeFun a r
42 ------------------------------------------------------------------------------
44 -- Compute arity of a constructor against a type argument
46 ------------------------------------------------------------------------------
49 constrArity :: Data a => (a -> ()) -> Constr -> Int
50 constrArity ta c = glength $ withType (fromConstr c) ta
53 ------------------------------------------------------------------------------
55 -- Reachability relation on types
57 ------------------------------------------------------------------------------
60 -- Test if nodes of type "a" are reachable from nodes of type "b".
61 -- This is a naive, inefficient encoding.
62 -- As of writing, it does not even cope with recursive types.
64 typeReachableFrom :: (Data a, Data b) => TypeVal a -> TypeVal b -> Bool
65 typeReachableFrom (a::TypeVal a) (b::TypeVal b) =
67 : map (recurse . fromConstr) (dataTypeCons $ dataTypeOf b)
71 -- See if a is reachable from immediate subterms of a kind of b
74 . gmapL ( typeReachableFrom a