base-3.0.0.0: Basic librariesContentsIndex
Data.Generics.Basics
Portabilitynon-portable (local universal quantification)
Stabilityexperimental
Maintainerlibraries@haskell.org
Contents
Module Data.Typeable re-exported for convenience
The Data class for processing constructor applications
Datatype representations
Constructors
Observers
Convenience functions
Data constructor representations
Constructors
Observers
Convenience function: algebraic data types
From strings to constructors and vice versa: all data types
Convenience functions: take type constructors apart
Generic operations defined in terms of gunfold
Description
"Scrap your boilerplate" --- Generic programming in Haskell. See http://www.cs.vu.nl/boilerplate/. This module provides the Data class with its primitives for generic programming.
Synopsis
module Data.Typeable
class Typeable a => Data a where
gfoldl :: (forall a b . Data a => c (a -> b) -> a -> c b) -> (forall g . g -> c g) -> a -> c a
gunfold :: (forall b r . Data b => c (b -> r) -> c r) -> (forall r . r -> c r) -> Constr -> c a
toConstr :: a -> Constr
dataTypeOf :: a -> DataType
dataCast1 :: Typeable1 t => (forall a . Data a => c (t a)) -> Maybe (c a)
dataCast2 :: Typeable2 t => (forall a b . (Data a, Data b) => c (t a b)) -> Maybe (c a)
gmapT :: (forall b . Data b => b -> b) -> a -> a
gmapQl :: (r -> r' -> r) -> r -> (forall a . Data a => a -> r') -> a -> r
gmapQr :: (r' -> r -> r) -> r -> (forall a . Data a => a -> r') -> a -> r
gmapQ :: (forall a . Data a => a -> u) -> a -> [u]
gmapQi :: Int -> (forall a . Data a => a -> u) -> a -> u
gmapM :: Monad m => (forall a . Data a => a -> m a) -> a -> m a
gmapMp :: MonadPlus m => (forall a . Data a => a -> m a) -> a -> m a
gmapMo :: MonadPlus m => (forall a . Data a => a -> m a) -> a -> m a
data DataType
mkDataType :: String -> [Constr] -> DataType
mkIntType :: String -> DataType
mkFloatType :: String -> DataType
mkStringType :: String -> DataType
mkNorepType :: String -> DataType
dataTypeName :: DataType -> String
data DataRep
= AlgRep [Constr]
| IntRep
| FloatRep
| StringRep
| NoRep
dataTypeRep :: DataType -> DataRep
repConstr :: DataType -> ConstrRep -> Constr
isAlgType :: DataType -> Bool
dataTypeConstrs :: DataType -> [Constr]
indexConstr :: DataType -> ConIndex -> Constr
maxConstrIndex :: DataType -> ConIndex
isNorepType :: DataType -> Bool
data Constr
type ConIndex = Int
data Fixity
= Prefix
| Infix
mkConstr :: DataType -> String -> [String] -> Fixity -> Constr
mkIntConstr :: DataType -> Integer -> Constr
mkFloatConstr :: DataType -> Double -> Constr
mkStringConstr :: DataType -> String -> Constr
constrType :: Constr -> DataType
data ConstrRep
= AlgConstr ConIndex
| IntConstr Integer
| FloatConstr Double
| StringConstr String
constrRep :: Constr -> ConstrRep
constrFields :: Constr -> [String]
constrFixity :: Constr -> Fixity
constrIndex :: Constr -> ConIndex
showConstr :: Constr -> String
readConstr :: DataType -> String -> Maybe Constr
tyconUQname :: String -> String
tyconModule :: String -> String
fromConstr :: Data a => Constr -> a
fromConstrB :: Data a => (forall a . Data a => a) -> Constr -> a
fromConstrM :: (Monad m, Data a) => (forall a . Data a => m a) -> Constr -> m a
Module Data.Typeable re-exported for convenience
module Data.Typeable
The Data class for processing constructor applications
class Typeable a => Data a where

The Data class comprehends a fundamental primitive gfoldl for folding over constructor applications, say terms. This primitive can be instantiated in several ways to map over the immediate subterms of a term; see the gmap combinators later in this class. Indeed, a generic programmer does not necessarily need to use the ingenious gfoldl primitive but rather the intuitive gmap combinators. The gfoldl primitive is completed by means to query top-level constructors, to turn constructor representations into proper terms, and to list all possible datatype constructors. This completion allows us to serve generic programming scenarios like read, show, equality, term generation.

The combinators gmapT, gmapQ, gmapM, etc are all provided with default definitions in terms of gfoldl, leaving open the opportunity to provide datatype-specific definitions. (The inclusion of the gmap combinators as members of class Data allows the programmer or the compiler to derive specialised, and maybe more efficient code per datatype. Note: gfoldl is more higher-order than the gmap combinators. This is subject to ongoing benchmarking experiments. It might turn out that the gmap combinators will be moved out of the class Data.)

Conceptually, the definition of the gmap combinators in terms of the primitive gfoldl requires the identification of the gfoldl function arguments. Technically, we also need to identify the type constructor c for the construction of the result type from the folded term type.

In the definition of gmapQx combinators, we use phantom type constructors for the c in the type of gfoldl because the result type of a query does not involve the (polymorphic) type of the term argument. In the definition of gmapQl we simply use the plain constant type constructor because gfoldl is left-associative anyway and so it is readily suited to fold a left-associative binary operation over the immediate subterms. In the definition of gmapQr, extra effort is needed. We use a higher-order accumulation trick to mediate between left-associative constructor application vs. right-associative binary operation (e.g., (:)). When the query is meant to compute a value of type r, then the result type withing generic folding is r -> r. So the result of folding is a function to which we finally pass the right unit.

With the -fglasgow-exts option, GHC can generate instances of the Data class automatically. For example, given the declaration

 data T a b = C1 a b | C2 deriving (Typeable, Data)

GHC will generate an instance that is equivalent to

 instance (Data a, Data b) => Data (T a b) where
     gfoldl k z (C1 a b) = z C1 `k` a `k` b
     gfoldl k z C2       = z C2

     gunfold k z c = case constrIndex c of
                         1 -> k (k (z C1))
                         2 -> z C2

     toConstr (C1 _ _) = con_C1
     toConstr C2       = con_C2

     dataTypeOf _ = ty_T

 con_C1 = mkConstr ty_T "C1" [] Prefix
 con_C2 = mkConstr ty_T "C2" [] Prefix
 ty_T   = mkDataType "Module.T" [con_C1, con_C2]

This is suitable for datatypes that are exported transparently.

Methods
gfoldl
:: (forall a b . Data a => c (a -> b) -> a -> c b)defines how nonempty constructor applications are folded. It takes the folded tail of the constructor application and its head, i.e., an immediate subterm, and combines them in some way.
-> (forall g . g -> c g)defines how the empty constructor application is folded, like the neutral / start element for list folding.
-> astructure to be folded.
-> c aresult, with a type defined in terms of a, but variability is achieved by means of type constructor c for the construction of the actual result type.

Left-associative fold operation for constructor applications.

The type of gfoldl is a headache, but operationally it is a simple generalisation of a list fold.

The default definition for gfoldl is const id, which is suitable for abstract datatypes with no substructures.

gunfold :: (forall b r . Data b => c (b -> r) -> c r) -> (forall r . r -> c r) -> Constr -> c a
Unfolding constructor applications
toConstr :: a -> Constr
Obtaining the constructor from a given datum. For proper terms, this is meant to be the top-level constructor. Primitive datatypes are here viewed as potentially infinite sets of values (i.e., constructors).
dataTypeOf :: a -> DataType
The outer type constructor of the type
dataCast1 :: Typeable1 t => (forall a . Data a => c (t a)) -> Maybe (c a)

Mediate types and unary type constructors. In Data instances of the form T a, dataCast1 should be defined as gcast1.

The default definition is const Nothing, which is appropriate for non-unary type constructors.

dataCast2 :: Typeable2 t => (forall a b . (Data a, Data b) => c (t a b)) -> Maybe (c a)

Mediate types and binary type constructors. In Data instances of the form T a b, dataCast2 should be defined as gcast2.

The default definition is const Nothing, which is appropriate for non-binary type constructors.

gmapT :: (forall b . Data b => b -> b) -> a -> a

A generic transformation that maps over the immediate subterms

The default definition instantiates the type constructor c in the type of gfoldl to an identity datatype constructor, using the isomorphism pair as injection and projection.

gmapQl :: (r -> r' -> r) -> r -> (forall a . Data a => a -> r') -> a -> r
A generic query with a left-associative binary operator
gmapQr :: (r' -> r -> r) -> r -> (forall a . Data a => a -> r') -> a -> r
A generic query with a right-associative binary operator
gmapQ :: (forall a . Data a => a -> u) -> a -> [u]
A generic query that processes the immediate subterms and returns a list of results. The list is given in the same order as originally specified in the declaratoin of the data constructors.
gmapQi :: Int -> (forall a . Data a => a -> u) -> a -> u
A generic query that processes one child by index (zero-based)
gmapM :: Monad m => (forall a . Data a => a -> m a) -> a -> m a

A generic monadic transformation that maps over the immediate subterms

The default definition instantiates the type constructor c in the type of gfoldl to the monad datatype constructor, defining injection and projection using return and >>=.

gmapMp :: MonadPlus m => (forall a . Data a => a -> m a) -> a -> m a
Transformation of at least one immediate subterm does not fail
gmapMo :: MonadPlus m => (forall a . Data a => a -> m a) -> a -> m a
Transformation of one immediate subterm with success
show/hide Instances
Data Bool
Data Char
Data DataType
Data Double
Data Float
Data Handle
Data Int
Data Int16
Data Int32
Data Int64
Data Int8
Data Integer
Data Ordering
Data ThreadId
Data TyCon
Data TypeRep
Data Word
Data Word16
Data Word32
Data Word64
Data Word8
Data ()
(Data a, Data b) => Data (a, b)
(Data a, Data b, Data c) => Data (a, b, c)
(Data a, Data b, Data c, Data d) => Data (a, b, c, d)
(Data a, Data b, Data c, Data d, Data e) => Data (a, b, c, d, e)
(Data a, Data b, Data c, Data d, Data e, Data f) => Data (a, b, c, d, e, f)
(Data a, Data b, Data c, Data d, Data e, Data f, Data g) => Data (a, b, c, d, e, f, g)
(Data a, Data b) => Data (a -> b)
(RealFloat a, Data a) => Data (Complex a)
Typeable a => Data (ForeignPtr a)
Typeable a => Data (IO a)
Typeable a => Data (IORef a)
Typeable a => Data (MVar a)
Data a => Data (Maybe a)
Typeable a => Data (Ptr a)
(Data a, Integral a) => Data (Ratio a)
Typeable a => Data (STM a)
Typeable a => Data (StablePtr a)
Typeable a => Data (TVar a)
Data a => Data [a]
(Typeable a, Data b, Ix a) => Data (Array a b)
(Data a, Data b) => Data (Either a b)
(Typeable s, Typeable a) => Data (ST s a)
Datatype representations
data DataType
Representation of datatypes. A package of constructor representations with names of type and module.
show/hide Instances
Constructors
mkDataType :: String -> [Constr] -> DataType
Constructs an algebraic datatype
mkIntType :: String -> DataType
Constructs the Int type
mkFloatType :: String -> DataType
Constructs the Float type
mkStringType :: String -> DataType
Constructs the String type
mkNorepType :: String -> DataType
Constructs a non-representation for a non-presentable type
Observers
dataTypeName :: DataType -> String
Gets the type constructor including the module
data DataRep
Public representation of datatypes
Constructors
AlgRep [Constr]
IntRep
FloatRep
StringRep
NoRep
show/hide Instances
dataTypeRep :: DataType -> DataRep
Gets the public presentation of a datatype
Convenience functions
repConstr :: DataType -> ConstrRep -> Constr
Look up a constructor by its representation
isAlgType :: DataType -> Bool
Test for an algebraic type
dataTypeConstrs :: DataType -> [Constr]
Gets the constructors of an algebraic datatype
indexConstr :: DataType -> ConIndex -> Constr
Gets the constructor for an index (algebraic datatypes only)
maxConstrIndex :: DataType -> ConIndex
Gets the maximum constructor index of an algebraic datatype
isNorepType :: DataType -> Bool
Test for a non-representable type
Data constructor representations
data Constr
Representation of constructors
show/hide Instances
type ConIndex = Int
Unique index for datatype constructors, counting from 1 in the order they are given in the program text.
data Fixity
Fixity of constructors
Constructors
Prefix
Infix
show/hide Instances
Constructors
mkConstr :: DataType -> String -> [String] -> Fixity -> Constr
Constructs a constructor
mkIntConstr :: DataType -> Integer -> Constr
mkFloatConstr :: DataType -> Double -> Constr
mkStringConstr :: DataType -> String -> Constr
Observers
constrType :: Constr -> DataType
Gets the datatype of a constructor
data ConstrRep
Public representation of constructors
Constructors
AlgConstr ConIndex
IntConstr Integer
FloatConstr Double
StringConstr String
show/hide Instances
constrRep :: Constr -> ConstrRep
Gets the public presentation of constructors
constrFields :: Constr -> [String]
Gets the field labels of a constructor. The list of labels is returned in the same order as they were given in the original constructor declaration.
constrFixity :: Constr -> Fixity
Gets the fixity of a constructor
Convenience function: algebraic data types
constrIndex :: Constr -> ConIndex
Gets the index of a constructor (algebraic datatypes only)
From strings to constructors and vice versa: all data types
showConstr :: Constr -> String
Gets the string for a constructor
readConstr :: DataType -> String -> Maybe Constr
Lookup a constructor via a string
Convenience functions: take type constructors apart
tyconUQname :: String -> String
Gets the unqualified type constructor: drop *.*.*... before name
tyconModule :: String -> String
Gets the module of a type constructor: take *.*.*... before name
Generic operations defined in terms of gunfold
fromConstr :: Data a => Constr -> a
Build a term skeleton
fromConstrB :: Data a => (forall a . Data a => a) -> Constr -> a
Build a term and use a generic function for subterms
fromConstrM :: (Monad m, Data a) => (forall a . Data a => m a) -> Constr -> m a
Monadic variation on fromConstrB
Produced by Haddock version 0.8