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 within generic folding is r -> r.
So the result of folding is a function to which we finally pass the
right unit.
With the -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.
Methods
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> a -> c agunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c aUnfolding constructor applications
toConstr :: a -> ConstrObtaining 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 -> DataTypeThe outer type constructor of the type
dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c a)dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a)gmapT :: (forall b. Data b => b -> b) -> a -> aA generic transformation that maps over the immediate subterms
The default definition instantiates the type constructor
cin the type of gfoldl to an identity datatype constructor, using the isomorphism pair as injection and projection.gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> rA generic query with a left-associative binary operator
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> rA generic query with a right-associative binary operator
gmapQ :: (forall d. Data d => d -> 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 declaration of the data constructors.
gmapQi :: Int -> (forall d. Data d => d -> u) -> a -> uA generic query that processes one child by index (zero-based)
gmapM :: Monad m => (forall d. Data d => d -> m d) -> a -> m agmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> a -> m aTransformation of at least one immediate subterm does not fail
gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> a -> m aTransformation of one immediate subterm with success
Instances85Data, …
Data ByteArrayDefined in base-4.20.2.0 · Data.Array.ByteData IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData VoidDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData VersionDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData SpecConstrAnnotationDefined in ghc-internal-9.1003.0 · GHC.Internal.ExtsData IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData AssociativityDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData DecidedStrictnessDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData FixityDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData SourceStrictnessDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData SourceUnpackednessDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData BoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData CharDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData IntDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData OrderingDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData WordDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData ()Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (Complex a)Defined in base-4.20.2.0 · Data.ComplexData a => Data (First a)Defined in base-4.20.2.0 · Data.SemigroupData a => Data (Last a)Defined in base-4.20.2.0 · Data.SemigroupData a => Data (Max a)Defined in base-4.20.2.0 · Data.SemigroupData a => Data (Min a)Defined in base-4.20.2.0 · Data.SemigroupData a => Data (NonEmpty a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (Identity a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (First a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (Last a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (Down a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (Dual a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (ConstPtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (ForeignPtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (ZipList a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListData a => Data (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (Ptr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data (a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData a => Data [a]Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataFor historical reasons, the constructor name used for
(:)is"(:)". In a derived instance, it would be":".Data m => Data (WrappedMonoid m)Defined in base-4.20.2.0 · Data.SemigroupData p => Data (Par1 p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataTypeable s => Data (MutableByteArray s)Defined in base-4.20.2.0 · Data.Array.Byte(Data a, Integral a) => Data (Ratio a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData p => Data (U1 p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData p => Data (V1 p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataData t => Data (Proxy t)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data a, Data b) => Data (Arg a b)Defined in base-4.20.2.0 · Data.Semigroup(Data a, Data b) => Data (Either a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data a, Data b) => Data (a, b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data a, Data b, Ix a) => Data (Array a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Typeable k, Typeable a) => Data (Fixed a)Defined in base-4.20.2.0 · Data.Fixed(Typeable m, Typeable a, Data (m a)) => Data (WrappedMonad m a)Defined in base-4.20.2.0 · Control.Applicative(Data (f a), Data a, Typeable f) => Data (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data (f a), Data a, Typeable f) => Data (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data (f p), Typeable f, Data p) => Data (Rec1 f p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data a, Data b, Data c) => Data (a, b, c)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Typeable a, Typeable b, Typeable c, Data (a b c)) => Data (WrappedArrow a b c)Defined in base-4.20.2.0 · Control.Applicative(Typeable k, Data a, Typeable b) => Data (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Coercible a b, Data a, Data b) => Data (Coercion a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(a ~ b, Data a) => Data (a :~: b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data a, Data b, Data c, Data d) => Data (a, b, c, d)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Typeable a, Typeable f, Typeable g, Typeable k, Data (f a), Data (g a)) => Data (Product f g a)Defined in base-4.20.2.0 · Data.Functor.Product(Typeable a, Typeable f, Typeable g, Typeable k, Data (f a), Data (g a)) => Data (Sum f g a)Defined in base-4.20.2.0 · Data.Functor.Sum(Typeable f, Typeable g, Data p, Data (f p), Data (g p)) => Data ((:*:) f g p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Typeable f, Typeable g, Data p, Data (f p), Data (g p)) => Data ((:+:) f g p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Typeable i, Data p, Data c) => Data (K1 i c p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Typeable i, Typeable j, Typeable a, Typeable b, a ~~ b) => Data (a :~~: b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data a, Data b, Data c, Data d, Data e) => Data (a, b, c, d, e)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data p, Data (f p), Typeable c, Typeable i, Typeable f) => Data (M1 i c f p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Typeable a, Typeable f, Typeable g, Typeable k1, Typeable k2, Data (f (g a))) => Data (Compose f g a)Defined in base-4.20.2.0 · Data.Functor.Compose(Typeable f, Typeable g, Data p, Data (f (g p))) => Data ((:.:) f g p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data a, Data b, Data c, Data d, Data e, Data f) => Data (a, b, c, d, e, f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data(Data a, Data b, Data c, Data d, Data e, Data f, Data g) => Data (a, b, c, d, e, f, g)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data