1 -----------------------------------------------------------------------------
4 -- Copyright : (c) The University of Glasgow, CWI 2001--2004
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable (local universal quantification)
11 -- \"Scrap your boilerplate\" --- Generic programming in Haskell.
12 -- See <http://www.cs.vu.nl/boilerplate/>. This module provides
13 -- the 'Data' class with its primitives for generic programming, along
14 -- with instances for many datatypes. It corresponds to a merge between
15 -- the previous "Data.Generics.Basics" and almost all of
16 -- "Data.Generics.Instances". The instances that are not present
17 -- in this module were moved to the @Data.Generics.Instances@ module
18 -- in the @syb@ package.
20 -- For more information, please visit the new
21 -- SYB wiki: <http://www.cs.uu.nl/wiki/bin/view/GenericProgramming/SYB>.
24 -----------------------------------------------------------------------------
28 -- * Module Data.Typeable re-exported for convenience
31 -- * The Data class for processing constructor applications
33 gfoldl, -- :: ... -> a -> c a
34 gunfold, -- :: ... -> Constr -> c a
35 toConstr, -- :: a -> Constr
36 dataTypeOf, -- :: a -> DataType
37 dataCast1, -- mediate types and unary type constructors
38 dataCast2, -- mediate types and binary type constructors
39 -- Generic maps defined in terms of gfoldl
50 -- * Datatype representations
51 DataType, -- abstract, instance of: Show
53 mkDataType, -- :: String -> [Constr] -> DataType
54 mkIntType, -- :: String -> DataType
55 mkFloatType, -- :: String -> DataType
56 mkStringType, -- :: String -> DataType
57 mkNorepType, -- :: String -> DataType
59 dataTypeName, -- :: DataType -> String
60 DataRep(..), -- instance of: Eq, Show
61 dataTypeRep, -- :: DataType -> DataRep
62 -- ** Convenience functions
63 repConstr, -- :: DataType -> ConstrRep -> Constr
64 isAlgType, -- :: DataType -> Bool
65 dataTypeConstrs,-- :: DataType -> [Constr]
66 indexConstr, -- :: DataType -> ConIndex -> Constr
67 maxConstrIndex, -- :: DataType -> ConIndex
68 isNorepType, -- :: DataType -> Bool
70 -- * Data constructor representations
71 Constr, -- abstract, instance of: Eq, Show
72 ConIndex, -- alias for Int, start at 1
73 Fixity(..), -- instance of: Eq, Show
75 mkConstr, -- :: DataType -> String -> Fixity -> Constr
76 mkIntConstr, -- :: DataType -> Integer -> Constr
77 mkFloatConstr, -- :: DataType -> Double -> Constr
78 mkStringConstr, -- :: DataType -> String -> Constr
80 constrType, -- :: Constr -> DataType
81 ConstrRep(..), -- instance of: Eq, Show
82 constrRep, -- :: Constr -> ConstrRep
83 constrFields, -- :: Constr -> [String]
84 constrFixity, -- :: Constr -> Fixity
85 -- ** Convenience function: algebraic data types
86 constrIndex, -- :: Constr -> ConIndex
87 -- ** From strings to constructors and vice versa: all data types
88 showConstr, -- :: Constr -> String
89 readConstr, -- :: DataType -> String -> Maybe Constr
91 -- * Convenience functions: take type constructors apart
92 tyconUQname, -- :: String -> String
93 tyconModule, -- :: String -> String
95 -- * Generic operations defined in terms of 'gunfold'
96 fromConstr, -- :: Constr -> a
97 fromConstrB, -- :: ... -> Constr -> a
98 fromConstrM -- :: Monad m => ... -> Constr -> m a
103 ------------------------------------------------------------------------------
105 import Prelude -- necessary to get dependencies right
111 -- Imports for the instances
113 import Data.Int -- So we can give Data instance for Int8, ...
114 import Data.Word -- So we can give Data instance for Word8, ...
115 #ifdef __GLASGOW_HASKELL__
116 import GHC.Real( Ratio(..) ) -- So we can give Data instance for Ratio
117 --import GHC.IOBase -- So we can give Data instance for IO, Handle
118 import GHC.Ptr -- So we can give Data instance for Ptr
119 import GHC.ForeignPtr -- So we can give Data instance for ForeignPtr
120 --import GHC.Stable -- So we can give Data instance for StablePtr
121 --import GHC.ST -- So we can give Data instance for ST
122 --import GHC.Conc -- So we can give Data instance for MVar & Co.
123 import GHC.Arr -- So we can give Data instance for Array
126 import Hugs.Prelude( Ratio(..) )
129 import Foreign.ForeignPtr
133 #include "Typeable.h"
137 ------------------------------------------------------------------------------
141 ------------------------------------------------------------------------------
144 The 'Data' class comprehends a fundamental primitive 'gfoldl' for
145 folding over constructor applications, say terms. This primitive can
146 be instantiated in several ways to map over the immediate subterms
147 of a term; see the @gmap@ combinators later in this class. Indeed, a
148 generic programmer does not necessarily need to use the ingenious gfoldl
149 primitive but rather the intuitive @gmap@ combinators. The 'gfoldl'
150 primitive is completed by means to query top-level constructors, to
151 turn constructor representations into proper terms, and to list all
152 possible datatype constructors. This completion allows us to serve
153 generic programming scenarios like read, show, equality, term generation.
155 The combinators 'gmapT', 'gmapQ', 'gmapM', etc are all provided with
156 default definitions in terms of 'gfoldl', leaving open the opportunity
157 to provide datatype-specific definitions.
158 (The inclusion of the @gmap@ combinators as members of class 'Data'
159 allows the programmer or the compiler to derive specialised, and maybe
160 more efficient code per datatype. /Note/: 'gfoldl' is more higher-order
161 than the @gmap@ combinators. This is subject to ongoing benchmarking
162 experiments. It might turn out that the @gmap@ combinators will be
163 moved out of the class 'Data'.)
165 Conceptually, the definition of the @gmap@ combinators in terms of the
166 primitive 'gfoldl' requires the identification of the 'gfoldl' function
167 arguments. Technically, we also need to identify the type constructor
168 @c@ for the construction of the result type from the folded term type.
170 In the definition of @gmapQ@/x/ combinators, we use phantom type
171 constructors for the @c@ in the type of 'gfoldl' because the result type
172 of a query does not involve the (polymorphic) type of the term argument.
173 In the definition of 'gmapQl' we simply use the plain constant type
174 constructor because 'gfoldl' is left-associative anyway and so it is
175 readily suited to fold a left-associative binary operation over the
176 immediate subterms. In the definition of gmapQr, extra effort is
177 needed. We use a higher-order accumulation trick to mediate between
178 left-associative constructor application vs. right-associative binary
179 operation (e.g., @(:)@). When the query is meant to compute a value
180 of type @r@, then the result type withing generic folding is @r -> r@.
181 So the result of folding is a function to which we finally pass the
184 With the @-XDeriveDataTypeable@ option, GHC can generate instances of the
185 'Data' class automatically. For example, given the declaration
187 > data T a b = C1 a b | C2 deriving (Typeable, Data)
189 GHC will generate an instance that is equivalent to
191 > instance (Data a, Data b) => Data (T a b) where
192 > gfoldl k z (C1 a b) = z C1 `k` a `k` b
193 > gfoldl k z C2 = z C2
195 > gunfold k z c = case constrIndex c of
199 > toConstr (C1 _ _) = con_C1
200 > toConstr C2 = con_C2
202 > dataTypeOf _ = ty_T
204 > con_C1 = mkConstr ty_T "C1" [] Prefix
205 > con_C2 = mkConstr ty_T "C2" [] Prefix
206 > ty_T = mkDataType "Module.T" [con_C1, con_C2]
208 This is suitable for datatypes that are exported transparently.
212 class Typeable a => Data a where
214 -- | Left-associative fold operation for constructor applications.
216 -- The type of 'gfoldl' is a headache, but operationally it is a simple
217 -- generalisation of a list fold.
219 -- The default definition for 'gfoldl' is @'const' 'id'@, which is
220 -- suitable for abstract datatypes with no substructures.
221 gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
222 -- ^ defines how nonempty constructor applications are
223 -- folded. It takes the folded tail of the constructor
224 -- application and its head, i.e., an immediate subterm,
225 -- and combines them in some way.
226 -> (forall g. g -> c g)
227 -- ^ defines how the empty constructor application is
228 -- folded, like the neutral \/ start element for list
231 -- ^ structure to be folded.
233 -- ^ result, with a type defined in terms of @a@, but
234 -- variability is achieved by means of type constructor
235 -- @c@ for the construction of the actual result type.
237 -- See the 'Data' instances in this file for an illustration of 'gfoldl'.
241 -- | Unfolding constructor applications
242 gunfold :: (forall b r. Data b => c (b -> r) -> c r)
243 -> (forall r. r -> c r)
247 -- | Obtaining the constructor from a given datum.
248 -- For proper terms, this is meant to be the top-level constructor.
249 -- Primitive datatypes are here viewed as potentially infinite sets of
250 -- values (i.e., constructors).
251 toConstr :: a -> Constr
254 -- | The outer type constructor of the type
255 dataTypeOf :: a -> DataType
259 ------------------------------------------------------------------------------
261 -- Mediate types and type constructors
263 ------------------------------------------------------------------------------
265 -- | Mediate types and unary type constructors.
266 -- In 'Data' instances of the form @T a@, 'dataCast1' should be defined
269 -- The default definition is @'const' 'Nothing'@, which is appropriate
270 -- for non-unary type constructors.
271 dataCast1 :: Typeable1 t
272 => (forall d. Data d => c (t d))
274 dataCast1 _ = Nothing
276 -- | Mediate types and binary type constructors.
277 -- In 'Data' instances of the form @T a b@, 'dataCast2' should be
278 -- defined as 'gcast2'.
280 -- The default definition is @'const' 'Nothing'@, which is appropriate
281 -- for non-binary type constructors.
282 dataCast2 :: Typeable2 t
283 => (forall d e. (Data d, Data e) => c (t d e))
285 dataCast2 _ = Nothing
289 ------------------------------------------------------------------------------
291 -- Typical generic maps defined in terms of gfoldl
293 ------------------------------------------------------------------------------
296 -- | A generic transformation that maps over the immediate subterms
298 -- The default definition instantiates the type constructor @c@ in the
299 -- type of 'gfoldl' to an identity datatype constructor, using the
300 -- isomorphism pair as injection and projection.
301 gmapT :: (forall b. Data b => b -> b) -> a -> a
303 -- Use an identity datatype constructor ID (see below)
304 -- to instantiate the type constructor c in the type of gfoldl,
305 -- and perform injections ID and projections unID accordingly.
307 gmapT f x0 = unID (gfoldl k ID x0)
309 k (ID c) x = ID (c (f x))
312 -- | A generic query with a left-associative binary operator
313 gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
314 gmapQl o r f = unCONST . gfoldl k z
316 k c x = CONST $ (unCONST c) `o` f x
319 -- | A generic query with a right-associative binary operator
320 gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
321 gmapQr o r0 f x0 = unQr (gfoldl k (const (Qr id)) x0) r0
323 k (Qr c) x = Qr (\r -> c (f x `o` r))
326 -- | A generic query that processes the immediate subterms and returns a list
327 -- of results. The list is given in the same order as originally specified
328 -- in the declaratoin of the data constructors.
329 gmapQ :: (forall d. Data d => d -> u) -> a -> [u]
330 gmapQ f = gmapQr (:) [] f
333 -- | A generic query that processes one child by index (zero-based)
334 gmapQi :: Int -> (forall d. Data d => d -> u) -> a -> u
335 gmapQi i f x = case gfoldl k z x of { Qi _ q -> fromJust q }
337 k (Qi i' q) a = Qi (i'+1) (if i==i' then Just (f a) else q)
341 -- | A generic monadic transformation that maps over the immediate subterms
343 -- The default definition instantiates the type constructor @c@ in
344 -- the type of 'gfoldl' to the monad datatype constructor, defining
345 -- injection and projection using 'return' and '>>='.
346 gmapM :: Monad m => (forall d. Data d => d -> m d) -> a -> m a
348 -- Use immediately the monad datatype constructor
349 -- to instantiate the type constructor c in the type of gfoldl,
350 -- so injection and projection is done by return and >>=.
352 gmapM f = gfoldl k return
359 -- | Transformation of at least one immediate subterm does not fail
360 gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a
364 The type constructor that we use here simply keeps track of the fact
365 if we already succeeded for an immediate subterm; see Mp below. To
366 this end, we couple the monadic computation with a Boolean.
370 gmapMp f x = unMp (gfoldl k z x) >>= \(x',b) ->
371 if b then return x' else mzero
373 z g = Mp (return (g,False))
375 = Mp ( c >>= \(h, b) ->
376 (f y >>= \y' -> return (h y', True))
377 `mplus` return (h y, b)
380 -- | Transformation of one immediate subterm with success
381 gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a
385 We use the same pairing trick as for gmapMp,
386 i.e., we use an extra Bool component to keep track of the
387 fact whether an immediate subterm was processed successfully.
388 However, we cut of mapping over subterms once a first subterm
389 was transformed successfully.
393 gmapMo f x = unMp (gfoldl k z x) >>= \(x',b) ->
394 if b then return x' else mzero
396 z g = Mp (return (g,False))
398 = Mp ( c >>= \(h,b) -> if b
400 else (f y >>= \y' -> return (h y',True))
401 `mplus` return (h y, b)
405 -- | The identity type constructor needed for the definition of gmapT
406 newtype ID x = ID { unID :: x }
409 -- | The constant type constructor needed for the definition of gmapQl
410 newtype CONST c a = CONST { unCONST :: c }
413 -- | Type constructor for adding counters to queries
414 data Qi q a = Qi Int (Maybe q)
417 -- | The type constructor used in definition of gmapQr
418 newtype Qr r a = Qr { unQr :: r -> r }
421 -- | The type constructor used in definition of gmapMp
422 newtype Mp m x = Mp { unMp :: m (x, Bool) }
426 ------------------------------------------------------------------------------
430 ------------------------------------------------------------------------------
433 -- | Build a term skeleton
434 fromConstr :: Data a => Constr -> a
435 fromConstr = fromConstrB (error "Data.Data.fromConstr")
438 -- | Build a term and use a generic function for subterms
439 fromConstrB :: Data a
440 => (forall d. Data d => d)
443 fromConstrB f = unID . gunfold k z
449 -- | Monadic variation on 'fromConstrB'
450 fromConstrM :: (Monad m, Data a)
451 => (forall d. Data d => m d)
454 fromConstrM f = gunfold k z
456 k c = do { c' <- c; b <- f; return (c' b) }
461 ------------------------------------------------------------------------------
463 -- Datatype and constructor representations
465 ------------------------------------------------------------------------------
469 -- | Representation of datatypes.
470 -- A package of constructor representations with names of type and module.
472 data DataType = DataType
480 -- | Representation of constructors
482 { conrep :: ConstrRep
483 , constring :: String
484 , confields :: [String] -- for AlgRep only
485 , confixity :: Fixity -- for AlgRep only
486 , datatype :: DataType
489 instance Show Constr where
493 -- | Equality of constructors
494 instance Eq Constr where
495 c == c' = constrRep c == constrRep c'
498 -- | Public representation of datatypes
499 data DataRep = AlgRep [Constr]
506 -- The list of constructors could be an array, a balanced tree, or others.
509 -- | Public representation of constructors
510 data ConstrRep = AlgConstr ConIndex
513 | StringConstr String
518 -- | Unique index for datatype constructors,
519 -- counting from 1 in the order they are given in the program text.
523 -- | Fixity of constructors
525 | Infix -- Later: add associativity and precedence
530 ------------------------------------------------------------------------------
532 -- Observers for datatype representations
534 ------------------------------------------------------------------------------
537 -- | Gets the type constructor including the module
538 dataTypeName :: DataType -> String
543 -- | Gets the public presentation of a datatype
544 dataTypeRep :: DataType -> DataRep
545 dataTypeRep = datarep
548 -- | Gets the datatype of a constructor
549 constrType :: Constr -> DataType
550 constrType = datatype
553 -- | Gets the public presentation of constructors
554 constrRep :: Constr -> ConstrRep
558 -- | Look up a constructor by its representation
559 repConstr :: DataType -> ConstrRep -> Constr
561 case (dataTypeRep dt, cr) of
562 (AlgRep cs, AlgConstr i) -> cs !! (i-1)
563 (IntRep, IntConstr i) -> mkIntConstr dt i
564 (FloatRep, FloatConstr f) -> mkFloatConstr dt f
565 (StringRep, StringConstr str) -> mkStringConstr dt str
566 _ -> error "repConstr"
570 ------------------------------------------------------------------------------
572 -- Representations of algebraic data types
574 ------------------------------------------------------------------------------
577 -- | Constructs an algebraic datatype
578 mkDataType :: String -> [Constr] -> DataType
579 mkDataType str cs = DataType
581 , datarep = AlgRep cs
585 -- | Constructs a constructor
586 mkConstr :: DataType -> String -> [String] -> Fixity -> Constr
587 mkConstr dt str fields fix =
589 { conrep = AlgConstr idx
596 idx = head [ i | (c,i) <- dataTypeConstrs dt `zip` [1..],
597 showConstr c == str ]
600 -- | Gets the constructors of an algebraic datatype
601 dataTypeConstrs :: DataType -> [Constr]
602 dataTypeConstrs dt = case datarep dt of
603 (AlgRep cons) -> cons
604 _ -> error "dataTypeConstrs"
607 -- | Gets the field labels of a constructor. The list of labels
608 -- is returned in the same order as they were given in the original
609 -- constructor declaration.
610 constrFields :: Constr -> [String]
611 constrFields = confields
614 -- | Gets the fixity of a constructor
615 constrFixity :: Constr -> Fixity
616 constrFixity = confixity
620 ------------------------------------------------------------------------------
622 -- From strings to constr's and vice versa: all data types
624 ------------------------------------------------------------------------------
627 -- | Gets the string for a constructor
628 showConstr :: Constr -> String
629 showConstr = constring
632 -- | Lookup a constructor via a string
633 readConstr :: DataType -> String -> Maybe Constr
635 case dataTypeRep dt of
636 AlgRep cons -> idx cons
637 IntRep -> mkReadCon (\i -> (mkPrimCon dt str (IntConstr i)))
638 FloatRep -> mkReadCon (\f -> (mkPrimCon dt str (FloatConstr f)))
639 StringRep -> Just (mkStringConstr dt str)
643 -- Read a value and build a constructor
644 mkReadCon :: Read t => (t -> Constr) -> Maybe Constr
645 mkReadCon f = case (reads str) of
646 [(t,"")] -> Just (f t)
649 -- Traverse list of algebraic datatype constructors
650 idx :: [Constr] -> Maybe Constr
651 idx cons = let fit = filter ((==) str . showConstr) cons
657 ------------------------------------------------------------------------------
659 -- Convenience funtions: algebraic data types
661 ------------------------------------------------------------------------------
664 -- | Test for an algebraic type
665 isAlgType :: DataType -> Bool
666 isAlgType dt = case datarep dt of
671 -- | Gets the constructor for an index (algebraic datatypes only)
672 indexConstr :: DataType -> ConIndex -> Constr
673 indexConstr dt idx = case datarep dt of
674 (AlgRep cs) -> cs !! (idx-1)
675 _ -> error "indexConstr"
678 -- | Gets the index of a constructor (algebraic datatypes only)
679 constrIndex :: Constr -> ConIndex
680 constrIndex con = case constrRep con of
681 (AlgConstr idx) -> idx
682 _ -> error "constrIndex"
685 -- | Gets the maximum constructor index of an algebraic datatype
686 maxConstrIndex :: DataType -> ConIndex
687 maxConstrIndex dt = case dataTypeRep dt of
688 AlgRep cs -> length cs
689 _ -> error "maxConstrIndex"
693 ------------------------------------------------------------------------------
695 -- Representation of primitive types
697 ------------------------------------------------------------------------------
700 -- | Constructs the 'Int' type
701 mkIntType :: String -> DataType
702 mkIntType = mkPrimType IntRep
705 -- | Constructs the 'Float' type
706 mkFloatType :: String -> DataType
707 mkFloatType = mkPrimType FloatRep
710 -- | Constructs the 'String' type
711 mkStringType :: String -> DataType
712 mkStringType = mkPrimType StringRep
715 -- | Helper for 'mkIntType', 'mkFloatType', 'mkStringType'
716 mkPrimType :: DataRep -> String -> DataType
717 mkPrimType dr str = DataType
723 -- Makes a constructor for primitive types
724 mkPrimCon :: DataType -> String -> ConstrRep -> Constr
725 mkPrimCon dt str cr = Constr
729 , confields = error "constrFields"
730 , confixity = error "constrFixity"
734 mkIntConstr :: DataType -> Integer -> Constr
735 mkIntConstr dt i = case datarep dt of
736 IntRep -> mkPrimCon dt (show i) (IntConstr i)
737 _ -> error "mkIntConstr"
740 mkFloatConstr :: DataType -> Double -> Constr
741 mkFloatConstr dt f = case datarep dt of
742 FloatRep -> mkPrimCon dt (show f) (FloatConstr f)
743 _ -> error "mkFloatConstr"
746 mkStringConstr :: DataType -> String -> Constr
747 mkStringConstr dt str = case datarep dt of
748 StringRep -> mkPrimCon dt str (StringConstr str)
749 _ -> error "mkStringConstr"
752 ------------------------------------------------------------------------------
754 -- Non-representations for non-presentable types
756 ------------------------------------------------------------------------------
759 -- | Constructs a non-representation for a non-presentable type
760 mkNorepType :: String -> DataType
761 mkNorepType str = DataType
767 -- | Test for a non-representable type
768 isNorepType :: DataType -> Bool
769 isNorepType dt = case datarep dt of
775 ------------------------------------------------------------------------------
777 -- Convenience for qualified type constructors
779 ------------------------------------------------------------------------------
782 -- | Gets the unqualified type constructor:
783 -- drop *.*.*... before name
785 tyconUQname :: String -> String
786 tyconUQname x = let x' = dropWhile (not . (==) '.') x
787 in if x' == [] then x else tyconUQname (tail x')
790 -- | Gets the module of a type constructor:
791 -- take *.*.*... before name
792 tyconModule :: String -> String
793 tyconModule x = let (a,b) = break ((==) '.') x
796 else a ++ tyconModule' (tail b)
798 tyconModule' y = let y' = tyconModule y
799 in if y' == "" then "" else ('.':y')
804 ------------------------------------------------------------------------------
805 ------------------------------------------------------------------------------
807 -- Instances of the Data class for Prelude-like types.
808 -- We define top-level definitions for representations.
810 ------------------------------------------------------------------------------
813 falseConstr :: Constr
814 falseConstr = mkConstr boolDataType "False" [] Prefix
816 trueConstr = mkConstr boolDataType "True" [] Prefix
818 boolDataType :: DataType
819 boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
821 instance Data Bool where
822 toConstr False = falseConstr
823 toConstr True = trueConstr
824 gunfold _ z c = case constrIndex c of
828 dataTypeOf _ = boolDataType
831 ------------------------------------------------------------------------------
834 charType = mkStringType "Prelude.Char"
836 instance Data Char where
837 toConstr x = mkStringConstr charType [x]
838 gunfold _ z c = case constrRep c of
839 (StringConstr [x]) -> z x
841 dataTypeOf _ = charType
844 ------------------------------------------------------------------------------
846 floatType :: DataType
847 floatType = mkFloatType "Prelude.Float"
849 instance Data Float where
850 toConstr x = mkFloatConstr floatType (realToFrac x)
851 gunfold _ z c = case constrRep c of
852 (FloatConstr x) -> z (realToFrac x)
854 dataTypeOf _ = floatType
857 ------------------------------------------------------------------------------
859 doubleType :: DataType
860 doubleType = mkFloatType "Prelude.Double"
862 instance Data Double where
863 toConstr = mkFloatConstr floatType
864 gunfold _ z c = case constrRep c of
865 (FloatConstr x) -> z x
867 dataTypeOf _ = doubleType
870 ------------------------------------------------------------------------------
873 intType = mkIntType "Prelude.Int"
875 instance Data Int where
876 toConstr x = mkIntConstr intType (fromIntegral x)
877 gunfold _ z c = case constrRep c of
878 (IntConstr x) -> z (fromIntegral x)
880 dataTypeOf _ = intType
883 ------------------------------------------------------------------------------
885 integerType :: DataType
886 integerType = mkIntType "Prelude.Integer"
888 instance Data Integer where
889 toConstr = mkIntConstr integerType
890 gunfold _ z c = case constrRep c of
893 dataTypeOf _ = integerType
896 ------------------------------------------------------------------------------
899 int8Type = mkIntType "Data.Int.Int8"
901 instance Data Int8 where
902 toConstr x = mkIntConstr int8Type (fromIntegral x)
903 gunfold _ z c = case constrRep c of
904 (IntConstr x) -> z (fromIntegral x)
906 dataTypeOf _ = int8Type
909 ------------------------------------------------------------------------------
911 int16Type :: DataType
912 int16Type = mkIntType "Data.Int.Int16"
914 instance Data Int16 where
915 toConstr x = mkIntConstr int16Type (fromIntegral x)
916 gunfold _ z c = case constrRep c of
917 (IntConstr x) -> z (fromIntegral x)
919 dataTypeOf _ = int16Type
922 ------------------------------------------------------------------------------
924 int32Type :: DataType
925 int32Type = mkIntType "Data.Int.Int32"
927 instance Data Int32 where
928 toConstr x = mkIntConstr int32Type (fromIntegral x)
929 gunfold _ z c = case constrRep c of
930 (IntConstr x) -> z (fromIntegral x)
932 dataTypeOf _ = int32Type
935 ------------------------------------------------------------------------------
937 int64Type :: DataType
938 int64Type = mkIntType "Data.Int.Int64"
940 instance Data Int64 where
941 toConstr x = mkIntConstr int64Type (fromIntegral x)
942 gunfold _ z c = case constrRep c of
943 (IntConstr x) -> z (fromIntegral x)
945 dataTypeOf _ = int64Type
948 ------------------------------------------------------------------------------
951 wordType = mkIntType "Data.Word.Word"
953 instance Data Word where
954 toConstr x = mkIntConstr wordType (fromIntegral x)
955 gunfold _ z c = case constrRep c of
956 (IntConstr x) -> z (fromIntegral x)
958 dataTypeOf _ = wordType
961 ------------------------------------------------------------------------------
963 word8Type :: DataType
964 word8Type = mkIntType "Data.Word.Word8"
966 instance Data Word8 where
967 toConstr x = mkIntConstr word8Type (fromIntegral x)
968 gunfold _ z c = case constrRep c of
969 (IntConstr x) -> z (fromIntegral x)
971 dataTypeOf _ = word8Type
974 ------------------------------------------------------------------------------
976 word16Type :: DataType
977 word16Type = mkIntType "Data.Word.Word16"
979 instance Data Word16 where
980 toConstr x = mkIntConstr word16Type (fromIntegral x)
981 gunfold _ z c = case constrRep c of
982 (IntConstr x) -> z (fromIntegral x)
984 dataTypeOf _ = word16Type
987 ------------------------------------------------------------------------------
989 word32Type :: DataType
990 word32Type = mkIntType "Data.Word.Word32"
992 instance Data Word32 where
993 toConstr x = mkIntConstr word32Type (fromIntegral x)
994 gunfold _ z c = case constrRep c of
995 (IntConstr x) -> z (fromIntegral x)
997 dataTypeOf _ = word32Type
1000 ------------------------------------------------------------------------------
1002 word64Type :: DataType
1003 word64Type = mkIntType "Data.Word.Word64"
1005 instance Data Word64 where
1006 toConstr x = mkIntConstr word64Type (fromIntegral x)
1007 gunfold _ z c = case constrRep c of
1008 (IntConstr x) -> z (fromIntegral x)
1009 _ -> error "gunfold"
1010 dataTypeOf _ = word64Type
1013 ------------------------------------------------------------------------------
1015 ratioConstr :: Constr
1016 ratioConstr = mkConstr ratioDataType ":%" [] Infix
1018 ratioDataType :: DataType
1019 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
1021 instance (Data a, Integral a) => Data (Ratio a) where
1022 gfoldl k z (a :% b) = z (:%) `k` a `k` b
1023 toConstr _ = ratioConstr
1024 gunfold k z c | constrIndex c == 1 = k (k (z (:%)))
1025 gunfold _ _ _ = error "gunfold"
1026 dataTypeOf _ = ratioDataType
1029 ------------------------------------------------------------------------------
1032 nilConstr = mkConstr listDataType "[]" [] Prefix
1033 consConstr :: Constr
1034 consConstr = mkConstr listDataType "(:)" [] Infix
1036 listDataType :: DataType
1037 listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
1039 instance Data a => Data [a] where
1040 gfoldl _ z [] = z []
1041 gfoldl f z (x:xs) = z (:) `f` x `f` xs
1042 toConstr [] = nilConstr
1043 toConstr (_:_) = consConstr
1044 gunfold k z c = case constrIndex c of
1047 _ -> error "gunfold"
1048 dataTypeOf _ = listDataType
1049 dataCast1 f = gcast1 f
1052 -- The gmaps are given as an illustration.
1053 -- This shows that the gmaps for lists are different from list maps.
1056 gmapT f (x:xs) = (f x:f xs)
1058 gmapQ f (x:xs) = [f x,f xs]
1059 gmapM _ [] = return []
1060 gmapM f (x:xs) = f x >>= \x' -> f xs >>= \xs' -> return (x':xs')
1063 ------------------------------------------------------------------------------
1065 nothingConstr :: Constr
1066 nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix
1067 justConstr :: Constr
1068 justConstr = mkConstr maybeDataType "Just" [] Prefix
1070 maybeDataType :: DataType
1071 maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]
1073 instance Data a => Data (Maybe a) where
1074 gfoldl _ z Nothing = z Nothing
1075 gfoldl f z (Just x) = z Just `f` x
1076 toConstr Nothing = nothingConstr
1077 toConstr (Just _) = justConstr
1078 gunfold k z c = case constrIndex c of
1081 _ -> error "gunfold"
1082 dataTypeOf _ = maybeDataType
1083 dataCast1 f = gcast1 f
1086 ------------------------------------------------------------------------------
1089 ltConstr = mkConstr orderingDataType "LT" [] Prefix
1091 eqConstr = mkConstr orderingDataType "EQ" [] Prefix
1093 gtConstr = mkConstr orderingDataType "GT" [] Prefix
1095 orderingDataType :: DataType
1096 orderingDataType = mkDataType "Prelude.Ordering" [ltConstr,eqConstr,gtConstr]
1098 instance Data Ordering where
1099 gfoldl _ z LT = z LT
1100 gfoldl _ z EQ = z EQ
1101 gfoldl _ z GT = z GT
1102 toConstr LT = ltConstr
1103 toConstr EQ = eqConstr
1104 toConstr GT = gtConstr
1105 gunfold _ z c = case constrIndex c of
1109 _ -> error "gunfold"
1110 dataTypeOf _ = orderingDataType
1113 ------------------------------------------------------------------------------
1115 leftConstr :: Constr
1116 leftConstr = mkConstr eitherDataType "Left" [] Prefix
1118 rightConstr :: Constr
1119 rightConstr = mkConstr eitherDataType "Right" [] Prefix
1121 eitherDataType :: DataType
1122 eitherDataType = mkDataType "Prelude.Either" [leftConstr,rightConstr]
1124 instance (Data a, Data b) => Data (Either a b) where
1125 gfoldl f z (Left a) = z Left `f` a
1126 gfoldl f z (Right a) = z Right `f` a
1127 toConstr (Left _) = leftConstr
1128 toConstr (Right _) = rightConstr
1129 gunfold k z c = case constrIndex c of
1132 _ -> error "gunfold"
1133 dataTypeOf _ = eitherDataType
1134 dataCast2 f = gcast2 f
1137 ------------------------------------------------------------------------------
1139 tuple0Constr :: Constr
1140 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
1142 tuple0DataType :: DataType
1143 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
1145 instance Data () where
1146 toConstr () = tuple0Constr
1147 gunfold _ z c | constrIndex c == 1 = z ()
1148 gunfold _ _ _ = error "gunfold"
1149 dataTypeOf _ = tuple0DataType
1152 ------------------------------------------------------------------------------
1154 tuple2Constr :: Constr
1155 tuple2Constr = mkConstr tuple2DataType "(,)" [] Infix
1157 tuple2DataType :: DataType
1158 tuple2DataType = mkDataType "Prelude.(,)" [tuple2Constr]
1160 instance (Data a, Data b) => Data (a,b) where
1161 gfoldl f z (a,b) = z (,) `f` a `f` b
1162 toConstr (_,_) = tuple2Constr
1163 gunfold k z c | constrIndex c == 1 = k (k (z (,)))
1164 gunfold _ _ _ = error "gunfold"
1165 dataTypeOf _ = tuple2DataType
1166 dataCast2 f = gcast2 f
1169 ------------------------------------------------------------------------------
1171 tuple3Constr :: Constr
1172 tuple3Constr = mkConstr tuple3DataType "(,,)" [] Infix
1174 tuple3DataType :: DataType
1175 tuple3DataType = mkDataType "Prelude.(,)" [tuple3Constr]
1177 instance (Data a, Data b, Data c) => Data (a,b,c) where
1178 gfoldl f z (a,b,c) = z (,,) `f` a `f` b `f` c
1179 toConstr (_,_,_) = tuple3Constr
1180 gunfold k z c | constrIndex c == 1 = k (k (k (z (,,))))
1181 gunfold _ _ _ = error "gunfold"
1182 dataTypeOf _ = tuple3DataType
1185 ------------------------------------------------------------------------------
1187 tuple4Constr :: Constr
1188 tuple4Constr = mkConstr tuple4DataType "(,,,)" [] Infix
1190 tuple4DataType :: DataType
1191 tuple4DataType = mkDataType "Prelude.(,,,)" [tuple4Constr]
1193 instance (Data a, Data b, Data c, Data d)
1194 => Data (a,b,c,d) where
1195 gfoldl f z (a,b,c,d) = z (,,,) `f` a `f` b `f` c `f` d
1196 toConstr (_,_,_,_) = tuple4Constr
1197 gunfold k z c = case constrIndex c of
1198 1 -> k (k (k (k (z (,,,)))))
1199 _ -> error "gunfold"
1200 dataTypeOf _ = tuple4DataType
1203 ------------------------------------------------------------------------------
1205 tuple5Constr :: Constr
1206 tuple5Constr = mkConstr tuple5DataType "(,,,,)" [] Infix
1208 tuple5DataType :: DataType
1209 tuple5DataType = mkDataType "Prelude.(,,,,)" [tuple5Constr]
1211 instance (Data a, Data b, Data c, Data d, Data e)
1212 => Data (a,b,c,d,e) where
1213 gfoldl f z (a,b,c,d,e) = z (,,,,) `f` a `f` b `f` c `f` d `f` e
1214 toConstr (_,_,_,_,_) = tuple5Constr
1215 gunfold k z c = case constrIndex c of
1216 1 -> k (k (k (k (k (z (,,,,))))))
1217 _ -> error "gunfold"
1218 dataTypeOf _ = tuple5DataType
1221 ------------------------------------------------------------------------------
1223 tuple6Constr :: Constr
1224 tuple6Constr = mkConstr tuple6DataType "(,,,,,)" [] Infix
1226 tuple6DataType :: DataType
1227 tuple6DataType = mkDataType "Prelude.(,,,,,)" [tuple6Constr]
1229 instance (Data a, Data b, Data c, Data d, Data e, Data f)
1230 => Data (a,b,c,d,e,f) where
1231 gfoldl f z (a,b,c,d,e,f') = z (,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f'
1232 toConstr (_,_,_,_,_,_) = tuple6Constr
1233 gunfold k z c = case constrIndex c of
1234 1 -> k (k (k (k (k (k (z (,,,,,)))))))
1235 _ -> error "gunfold"
1236 dataTypeOf _ = tuple6DataType
1239 ------------------------------------------------------------------------------
1241 tuple7Constr :: Constr
1242 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
1244 tuple7DataType :: DataType
1245 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
1247 instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
1248 => Data (a,b,c,d,e,f,g) where
1249 gfoldl f z (a,b,c,d,e,f',g) =
1250 z (,,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f' `f` g
1251 toConstr (_,_,_,_,_,_,_) = tuple7Constr
1252 gunfold k z c = case constrIndex c of
1253 1 -> k (k (k (k (k (k (k (z (,,,,,,))))))))
1254 _ -> error "gunfold"
1255 dataTypeOf _ = tuple7DataType
1258 ------------------------------------------------------------------------------
1260 instance Typeable a => Data (Ptr a) where
1261 toConstr _ = error "toConstr"
1262 gunfold _ _ = error "gunfold"
1263 dataTypeOf _ = mkNorepType "GHC.Ptr.Ptr"
1266 ------------------------------------------------------------------------------
1268 instance Typeable a => Data (ForeignPtr a) where
1269 toConstr _ = error "toConstr"
1270 gunfold _ _ = error "gunfold"
1271 dataTypeOf _ = mkNorepType "GHC.ForeignPtr.ForeignPtr"
1274 ------------------------------------------------------------------------------
1275 -- The Data instance for Array preserves data abstraction at the cost of
1276 -- inefficiency. We omit reflection services for the sake of data abstraction.
1277 instance (Typeable a, Data b, Ix a) => Data (Array a b)
1279 gfoldl f z a = z (listArray (bounds a)) `f` (elems a)
1280 toConstr _ = error "toConstr"
1281 gunfold _ _ = error "gunfold"
1282 dataTypeOf _ = mkNorepType "Data.Array.Array"