|Copyright||(c) The University of Glasgow, CWI 2001--2004|
|License||BSD-style (see the file libraries/base/LICENSE)|
|Portability||non-portable (local universal quantification)|
"Scrap your boilerplate" --- Generic programming in Haskell. See http://www.haskell.org/haskellwiki/Research_papers/Generics#Scrap_your_boilerplate.21. This module provides the
Data class with its primitives for generic programming, along with instances for many datatypes. It corresponds to a merge between the previous Data.Generics.Basics and almost all of Data.Generics.Instances. The instances that are not present in this module were moved to the
Data.Generics.Instances module in the
For more information, please visit the new SYB wiki: http://www.cs.uu.nl/wiki/bin/view/GenericProgramming/SYB.
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.
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
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.
-XDeriveDataTypeable 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.
|:: (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.
structure to be folded.
|-> c a||
result, with a type defined in terms of
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.
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).
The outer type constructor of the type
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.
A generic query with a left-associative binary operator
A generic query with a right-associative binary operator
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 declaration of the data constructors.
A generic query that processes one child by index (zero-based)
A generic monadic transformation that maps over the immediate subterms
Transformation of at least one immediate subterm does not fail
Transformation of one immediate subterm with success
|Data a => Data [a]|
|(Data a, Integral a) => Data (Ratio a)|
|(Data a, Typeable * a) => Data (Ptr a)|
|Data a => Data (Maybe a)|
|(Data a, Typeable * a) => Data (ForeignPtr a)|
|Data a => Data (Complex a)|
|Typeable * a => Data (Fixed a)|
|Data a => Data (Identity a)|
|(Data a, Data b) => Data (Either a b)|
|(Data a, Data b) => Data (a, b)|
|Data t => Data (Proxy * t)|
|(Data a, Data b, Data c) => Data (a, b, c)|
|((~) * a b, Data a) => Data ((:~:) * a b)|
|(Coercible * a b, Data a, Data b) => Data (Coercion * a b)|
|(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.
Constructs an algebraic datatype
Constructs a non-representation for a non-representable type
Gets the type constructor including the module
Public representation of datatypes
Gets the public presentation of a datatype
Look up a constructor by its representation
Test for an algebraic type
Gets the constructors of an algebraic datatype
Gets the constructor for an index (algebraic datatypes only)
Gets the maximum constructor index of an algebraic datatype
Test for a non-representable type
Unique index for datatype constructors, counting from 1 in the order they are given in the program text.
Fixity of constructors
Constructs a constructor
Makes a constructor for
Gets the datatype of a constructor
Public representation of constructors
Gets the public presentation of constructors
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.
Gets the fixity of a constructor
Gets the index of a constructor (algebraic datatypes only)
Gets the string for a constructor
Lookup a constructor via a string
Gets the unqualified type constructor: drop *.*.*... before name
Gets the module of a type constructor: take *.*.*... before name
Build a term skeleton
Build a term and use a generic function for subterms
Monadic variation on
© The University of Glasgow and others
Licensed under a BSD-style license (see top of the page).