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 ------------------------------------------------------------------------------
28 import Data.Generics.Basics
29 import Data.Generics.Aliases
30 import Data.Generics.Counts
34 -- Generic type functions,
35 -- i.e., functions mapping types to values
37 type GTypeFun r = forall a. Typeable a => TypeFun a r
41 ------------------------------------------------------------------------------
43 -- Compute arity of a constructor against a type argument
45 ------------------------------------------------------------------------------
48 constrArity :: Data a => (a -> ()) -> Constr -> Int
49 constrArity ta c = glength $ withType (fromConstr c) ta
52 ------------------------------------------------------------------------------
54 -- Reachability relation on types
56 ------------------------------------------------------------------------------
59 -- Test if nodes of type "a" are reachable from nodes of type "b".
60 -- This is a naive, inefficient encoding.
61 -- As of writing, it does not even cope with recursive types.
63 typeReachableFrom :: (Data a, Data b) => TypeVal a -> TypeVal b -> Bool
64 typeReachableFrom (a::TypeVal a) (b::TypeVal b) =
66 : map (recurse . fromConstr) (dataTypeCons $ dataTypeOf b)
70 -- See if a is reachable from immediate subterms of a kind of b
73 . gmapL ( typeReachableFrom a