| Portability | non-portable (local universal quantification) |
|---|---|
| Stability | experimental |
| Maintainer | libraries@haskell.org |
Data.Data
Contents
Description
"Scrap your boilerplate" --- Generic programming in Haskell. Seehttp://www.cs.vu.nl/boilerplate/. This module provides theData class with its primitives for generic programming, along with instances for many datatypes. It corresponds to a merge between the previousData.Generics.Basics and almost all ofData.Generics.Instances. The instances that are not present in this module were moved to theData.Generics.Instances module in thesyb package.
For more information, please visit the new SYB wiki:http://www.cs.uu.nl/wiki/bin/view/GenericProgramming/SYB.
Synopsis
moduleData.Typeable
classTypeable a =>Data awhereSource
TheData class comprehends a fundamental primitivegfoldl forfolding over constructor applications, say terms. This primitive canbe instantiated in several ways to map over the immediate subtermsof a term; see thegmap combinators later in this class. Indeed, ageneric programmer does not necessarily need to use the ingenious gfoldlprimitive but rather the intuitivegmap combinators. Thegfoldlprimitive is completed by means to query top-level constructors, toturn constructor representations into proper terms, and to list allpossible datatype constructors. This completion allows us to servegeneric programming scenarios like read, show, equality, term generation.
The combinatorsgmapT,gmapQ,gmapM, etc are all provided withdefault definitions in terms ofgfoldl, leaving open the opportunityto provide datatype-specific definitions.(The inclusion of thegmap combinators as members of classDataallows the programmer or the compiler to derive specialised, and maybemore efficient code per datatype.Note:gfoldl is more higher-orderthan thegmap combinators. This is subject to ongoing benchmarkingexperiments. It might turn out that thegmap combinators will bemoved out of the classData.)
Conceptually, the definition of thegmap combinators in terms of theprimitivegfoldl requires the identification of thegfoldl functionarguments. Technically, we also need to identify the type constructorc for the construction of the result type from the folded term type.
In the definition ofgmapQx combinators, we use phantom typeconstructors for thec in the type ofgfoldl because the result typeof a query does not involve the (polymorphic) type of the term argument.In the definition ofgmapQl we simply use the plain constant typeconstructor becausegfoldl is left-associative anyway and so it isreadily suited to fold a left-associative binary operation over theimmediate subterms. In the definition of gmapQr, extra effort isneeded. We use a higher-order accumulation trick to mediate betweenleft-associative constructor application vs. right-associative binaryoperation (e.g.,(:)). When the query is meant to compute a valueof typer, then the result type withing generic folding isr -> r.So the result of folding is a function to which we finally pass theright unit.
With the-XDeriveDataTypeable option, GHC can generate instances of theData 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
Arguments
| :: (forall d b.Data d => c (d -> b) -> d -> 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. |
| -> a | structure to be folded. |
| -> c a | result, with a type defined in terms of |
Left-associative fold operation for constructor applications.
The type ofgfoldl is a headache, but operationally it is a simple generalisation of a list fold.
The default definition forgfoldl is, which is suitable for abstract datatypes with no substructures.constid
gunfold :: (forall b r.Data b => c (b -> r) -> c r) -> (forall r. r -> c r) ->Constr -> c aSource
Unfolding constructor applications
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 ->DataTypeSource
The outer type constructor of the type
dataCast1 ::Typeable1 t => (forall d.Data d => c (t d)) ->Maybe (c a)Source
Mediate types and unary type constructors. InData instances of the formT a,dataCast1 should be defined asgcast1.
The default definition is, which is appropriate for non-unary type constructors.constNothing
dataCast2 ::Typeable2 t => (forall d e. (Data d,Data e) => c (t d e)) ->Maybe (c a)Source
Mediate types and binary type constructors. InData instances of the formT a b,dataCast2 should be defined asgcast2.
The default definition is, which is appropriate for non-binary type constructors.constNothing
gmapT :: (forall b.Data b => b -> b) -> a -> aSource
A generic transformation that maps over the immediate subterms
The default definition instantiates the type constructorc in the type ofgfoldl to an identity datatype constructor, using the isomorphism pair as injection and projection.
gmapQl ::forall r r'. (r -> r' -> r) -> r -> (forall d.Data d => d -> r') -> a -> rSource
A generic query with a left-associative binary operator
gmapQr ::forall r r'. (r' -> r -> r) -> r -> (forall d.Data d => d -> r') -> a -> rSource
A generic query with a right-associative binary operator
gmapQ :: (forall d.Data d => d -> u) -> a -> [u]Source
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 ::forall u.Int -> (forall d.Data d => d -> u) -> a -> uSource
A generic query that processes one child by index (zero-based)
gmapM ::forall m.Monad m => (forall d.Data d => d -> m d) -> a -> m aSource
A generic monadic transformation that maps over the immediate subterms
The default definition instantiates the type constructorc in the type ofgfoldl to the monad datatype constructor, defining injection and projection usingreturn and>>=.
gmapMp ::forall m.MonadPlus m => (forall d.Data d => d -> m d) -> a -> m aSource
Transformation of at least one immediate subterm does not fail
gmapMo ::forall m.MonadPlus m => (forall d.Data d => d -> m d) -> a -> m aSource
Transformation of one immediate subterm with success
Instances
| DataBool | |
| DataChar | |
| DataDouble | |
| DataFloat | |
| DataInt | |
| DataInt8 | |
| DataInt16 | |
| DataInt32 | |
| DataInt64 | |
| DataInteger | |
| DataOrdering | |
| DataWord | |
| DataWord8 | |
| DataWord16 | |
| DataWord32 | |
| DataWord64 | |
| Data () | |
| DataSpecConstrAnnotation | |
| Data a =>Data [a] | |
| (Data a,Integral a) =>Data (Ratio a) | |
| Typeable a =>Data (Ptr a) | |
| Data a =>Data (Maybe a) | |
| Typeable a =>Data (ForeignPtr a) | |
| Data a =>Data (Complex a) | |
| Typeable a =>Data (Fixed a) | |
| (Data a,Data b) =>Data (Either a b) | |
| (Data a,Data b) =>Data (a, b) | |
| (Typeable a,Data b,Ix a) =>Data (Array 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) |
Representation of datatypes. A package of constructor representations with names of type and module.
mkDataType ::String -> [Constr] ->DataTypeSource
Constructs an algebraic datatype
mkFloatType ::String ->DataTypeSource
Constructs theFloat type
mkStringType ::String ->DataTypeSource
This function is now deprecated. Please usemkCharType instead.
mkCharType ::String ->DataTypeSource
Constructs theChar type
mkNoRepType ::String ->DataTypeSource
Constructs a non-representation for a non-presentable type
mkNorepType ::String ->DataTypeSource
Deprecated version (misnamed)
dataTypeName ::DataType ->StringSource
Gets the type constructor including the module
Public representation of datatypes
dataTypeRep ::DataType ->DataRepSource
Gets the public presentation of a datatype
dataTypeConstrs ::DataType -> [Constr]Source
Gets the constructors of an algebraic datatype
indexConstr ::DataType ->ConIndex ->ConstrSource
Gets the constructor for an index (algebraic datatypes only)
maxConstrIndex ::DataType ->ConIndexSource
Gets the maximum constructor index of an algebraic datatype
isNorepType ::DataType ->BoolSource
Test for a non-representable type
Unique index for datatype constructors, counting from 1 in the order they are given in the program text.
mkIntConstr ::DataType ->Integer ->ConstrSource
This function is now deprecated. Please usemkIntegralConstr instead.
mkFloatConstr ::DataType ->Double ->ConstrSource
This function is now deprecated. Please usemkRealConstr instead.
mkIntegralConstr ::Integral a =>DataType -> a ->ConstrSource
mkRealConstr ::Real a =>DataType -> a ->ConstrSource
mkStringConstr ::DataType ->String ->ConstrSource
This function is now deprecated. Please usemkCharConstr instead.
constrType ::Constr ->DataTypeSource
Gets the datatype of a constructor
Public representation of constructors
constrFields ::Constr -> [String]Source
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 ->FixitySource
Gets the fixity of a constructor
constrIndex ::Constr ->ConIndexSource
Gets the index of a constructor (algebraic datatypes only)
showConstr ::Constr ->StringSource
Gets the string for a constructor
tyconUQname ::String ->StringSource
Gets the unqualified type constructor: drop *.*.*... before name
tyconModule ::String ->StringSource
Gets the module of a type constructor: take *.*.*... before name
gunfoldfromConstr ::Data a =>Constr -> aSource
Build a term skeleton
fromConstrB ::Data a => (forall d.Data d => d) ->Constr -> aSource
Build a term and use a generic function for subterms
fromConstrM ::forall m a. (Monad m,Data a) => (forall d.Data d => m d) ->Constr -> m aSource
Monadic variation onfromConstrB
Produced byHaddock version 2.9.2