Remove redundant constraints from libraries, discovered by -fwarn-redundant-constraints
[ghc.git] / libraries / base / Data / Data.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE RankNTypes, ScopedTypeVariables, PolyKinds, StandaloneDeriving,
3 AutoDeriveTypeable, TypeOperators, GADTs, FlexibleInstances #-}
4 {-# LANGUAGE NoImplicitPrelude #-}
5
6 -----------------------------------------------------------------------------
7 -- |
8 -- Module : Data.Data
9 -- Copyright : (c) The University of Glasgow, CWI 2001--2004
10 -- License : BSD-style (see the file libraries/base/LICENSE)
11 --
12 -- Maintainer : libraries@haskell.org
13 -- Stability : experimental
14 -- Portability : non-portable (local universal quantification)
15 --
16 -- \"Scrap your boilerplate\" --- Generic programming in Haskell. See
17 -- <http://www.haskell.org/haskellwiki/Research_papers/Generics#Scrap_your_boilerplate.21>.
18 -- This module provides the 'Data' class with its primitives for
19 -- generic programming, along with instances for many datatypes. It
20 -- corresponds to a merge between the previous "Data.Generics.Basics"
21 -- and almost all of "Data.Generics.Instances". The instances that are
22 -- not present in this module were moved to the
23 -- @Data.Generics.Instances@ module in the @syb@ package.
24 --
25 -- For more information, please visit the new
26 -- SYB wiki: <http://www.cs.uu.nl/wiki/bin/view/GenericProgramming/SYB>.
27 --
28 -----------------------------------------------------------------------------
29
30 module Data.Data (
31
32 -- * Module Data.Typeable re-exported for convenience
33 module Data.Typeable,
34
35 -- * The Data class for processing constructor applications
36 Data(
37 gfoldl,
38 gunfold,
39 toConstr,
40 dataTypeOf,
41 dataCast1, -- mediate types and unary type constructors
42 dataCast2, -- mediate types and binary type constructors
43 -- Generic maps defined in terms of gfoldl
44 gmapT,
45 gmapQ,
46 gmapQl,
47 gmapQr,
48 gmapQi,
49 gmapM,
50 gmapMp,
51 gmapMo
52 ),
53
54 -- * Datatype representations
55 DataType, -- abstract
56 -- ** Constructors
57 mkDataType,
58 mkIntType,
59 mkFloatType,
60 mkCharType,
61 mkNoRepType,
62 -- ** Observers
63 dataTypeName,
64 DataRep(..),
65 dataTypeRep,
66 -- ** Convenience functions
67 repConstr,
68 isAlgType,
69 dataTypeConstrs,
70 indexConstr,
71 maxConstrIndex,
72 isNorepType,
73
74 -- * Data constructor representations
75 Constr, -- abstract
76 ConIndex, -- alias for Int, start at 1
77 Fixity(..),
78 -- ** Constructors
79 mkConstr,
80 mkIntegralConstr,
81 mkRealConstr,
82 mkCharConstr,
83 -- ** Observers
84 constrType,
85 ConstrRep(..),
86 constrRep,
87 constrFields,
88 constrFixity,
89 -- ** Convenience function: algebraic data types
90 constrIndex,
91 -- ** From strings to constructors and vice versa: all data types
92 showConstr,
93 readConstr,
94
95 -- * Convenience functions: take type constructors apart
96 tyconUQname,
97 tyconModule,
98
99 -- * Generic operations defined in terms of 'gunfold'
100 fromConstr,
101 fromConstrB,
102 fromConstrM
103
104 ) where
105
106
107 ------------------------------------------------------------------------------
108
109 import Data.Either
110 import Data.Eq
111 import Data.Maybe
112 import Data.Ord
113 import Data.Typeable
114 import Data.Version( Version(..) )
115 import GHC.Base
116 import GHC.List
117 import GHC.Num
118 import GHC.Read
119 import GHC.Show
120 import Text.Read( reads )
121
122 -- Imports for the instances
123 import Data.Int -- So we can give Data instance for Int8, ...
124 import Data.Type.Coercion
125 import Data.Word -- So we can give Data instance for Word8, ...
126 import GHC.Real -- So we can give Data instance for Ratio
127 --import GHC.IOBase -- So we can give Data instance for IO, Handle
128 import GHC.Ptr -- So we can give Data instance for Ptr
129 import GHC.ForeignPtr -- So we can give Data instance for ForeignPtr
130 --import GHC.Stable -- So we can give Data instance for StablePtr
131 --import GHC.ST -- So we can give Data instance for ST
132 --import GHC.Conc -- So we can give Data instance for MVar & Co.
133 import GHC.Arr -- So we can give Data instance for Array
134
135
136 ------------------------------------------------------------------------------
137 --
138 -- The Data class
139 --
140 ------------------------------------------------------------------------------
141
142 {- |
143 The 'Data' class comprehends a fundamental primitive 'gfoldl' for
144 folding over constructor applications, say terms. This primitive can
145 be instantiated in several ways to map over the immediate subterms
146 of a term; see the @gmap@ combinators later in this class. Indeed, a
147 generic programmer does not necessarily need to use the ingenious gfoldl
148 primitive but rather the intuitive @gmap@ combinators. The 'gfoldl'
149 primitive is completed by means to query top-level constructors, to
150 turn constructor representations into proper terms, and to list all
151 possible datatype constructors. This completion allows us to serve
152 generic programming scenarios like read, show, equality, term generation.
153
154 The combinators 'gmapT', 'gmapQ', 'gmapM', etc are all provided with
155 default definitions in terms of 'gfoldl', leaving open the opportunity
156 to provide datatype-specific definitions.
157 (The inclusion of the @gmap@ combinators as members of class 'Data'
158 allows the programmer or the compiler to derive specialised, and maybe
159 more efficient code per datatype. /Note/: 'gfoldl' is more higher-order
160 than the @gmap@ combinators. This is subject to ongoing benchmarking
161 experiments. It might turn out that the @gmap@ combinators will be
162 moved out of the class 'Data'.)
163
164 Conceptually, the definition of the @gmap@ combinators in terms of the
165 primitive 'gfoldl' requires the identification of the 'gfoldl' function
166 arguments. Technically, we also need to identify the type constructor
167 @c@ for the construction of the result type from the folded term type.
168
169 In the definition of @gmapQ@/x/ combinators, we use phantom type
170 constructors for the @c@ in the type of 'gfoldl' because the result type
171 of a query does not involve the (polymorphic) type of the term argument.
172 In the definition of 'gmapQl' we simply use the plain constant type
173 constructor because 'gfoldl' is left-associative anyway and so it is
174 readily suited to fold a left-associative binary operation over the
175 immediate subterms. In the definition of gmapQr, extra effort is
176 needed. We use a higher-order accumulation trick to mediate between
177 left-associative constructor application vs. right-associative binary
178 operation (e.g., @(:)@). When the query is meant to compute a value
179 of type @r@, then the result type withing generic folding is @r -> r@.
180 So the result of folding is a function to which we finally pass the
181 right unit.
182
183 With the @-XDeriveDataTypeable@ option, GHC can generate instances of the
184 'Data' class automatically. For example, given the declaration
185
186 > data T a b = C1 a b | C2 deriving (Typeable, Data)
187
188 GHC will generate an instance that is equivalent to
189
190 > instance (Data a, Data b) => Data (T a b) where
191 > gfoldl k z (C1 a b) = z C1 `k` a `k` b
192 > gfoldl k z C2 = z C2
193 >
194 > gunfold k z c = case constrIndex c of
195 > 1 -> k (k (z C1))
196 > 2 -> z C2
197 >
198 > toConstr (C1 _ _) = con_C1
199 > toConstr C2 = con_C2
200 >
201 > dataTypeOf _ = ty_T
202 >
203 > con_C1 = mkConstr ty_T "C1" [] Prefix
204 > con_C2 = mkConstr ty_T "C2" [] Prefix
205 > ty_T = mkDataType "Module.T" [con_C1, con_C2]
206
207 This is suitable for datatypes that are exported transparently.
208
209 -}
210
211 class Typeable a => Data a where
212
213 -- | Left-associative fold operation for constructor applications.
214 --
215 -- The type of 'gfoldl' is a headache, but operationally it is a simple
216 -- generalisation of a list fold.
217 --
218 -- The default definition for 'gfoldl' is @'const' 'id'@, which is
219 -- suitable for abstract datatypes with no substructures.
220 gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
221 -- ^ defines how nonempty constructor applications are
222 -- folded. It takes the folded tail of the constructor
223 -- application and its head, i.e., an immediate subterm,
224 -- and combines them in some way.
225 -> (forall g. g -> c g)
226 -- ^ defines how the empty constructor application is
227 -- folded, like the neutral \/ start element for list
228 -- folding.
229 -> a
230 -- ^ structure to be folded.
231 -> c a
232 -- ^ result, with a type defined in terms of @a@, but
233 -- variability is achieved by means of type constructor
234 -- @c@ for the construction of the actual result type.
235
236 -- See the 'Data' instances in this file for an illustration of 'gfoldl'.
237
238 gfoldl _ z = z
239
240 -- | Unfolding constructor applications
241 gunfold :: (forall b r. Data b => c (b -> r) -> c r)
242 -> (forall r. r -> c r)
243 -> Constr
244 -> c a
245
246 -- | Obtaining the constructor from a given datum.
247 -- For proper terms, this is meant to be the top-level constructor.
248 -- Primitive datatypes are here viewed as potentially infinite sets of
249 -- values (i.e., constructors).
250 toConstr :: a -> Constr
251
252
253 -- | The outer type constructor of the type
254 dataTypeOf :: a -> DataType
255
256
257
258 ------------------------------------------------------------------------------
259 --
260 -- Mediate types and type constructors
261 --
262 ------------------------------------------------------------------------------
263
264 -- | Mediate types and unary type constructors.
265 -- In 'Data' instances of the form @T a@, 'dataCast1' should be defined
266 -- as 'gcast1'.
267 --
268 -- The default definition is @'const' 'Nothing'@, which is appropriate
269 -- for non-unary type constructors.
270 dataCast1 :: Typeable t
271 => (forall d. Data d => c (t d))
272 -> Maybe (c a)
273 dataCast1 _ = Nothing
274
275 -- | Mediate types and binary type constructors.
276 -- In 'Data' instances of the form @T a b@, 'dataCast2' should be
277 -- defined as 'gcast2'.
278 --
279 -- The default definition is @'const' 'Nothing'@, which is appropriate
280 -- for non-binary type constructors.
281 dataCast2 :: Typeable t
282 => (forall d e. (Data d, Data e) => c (t d e))
283 -> Maybe (c a)
284 dataCast2 _ = Nothing
285
286
287
288 ------------------------------------------------------------------------------
289 --
290 -- Typical generic maps defined in terms of gfoldl
291 --
292 ------------------------------------------------------------------------------
293
294
295 -- | A generic transformation that maps over the immediate subterms
296 --
297 -- The default definition instantiates the type constructor @c@ in the
298 -- type of 'gfoldl' to an identity datatype constructor, using the
299 -- isomorphism pair as injection and projection.
300 gmapT :: (forall b. Data b => b -> b) -> a -> a
301
302 -- Use an identity datatype constructor ID (see below)
303 -- to instantiate the type constructor c in the type of gfoldl,
304 -- and perform injections ID and projections unID accordingly.
305 --
306 gmapT f x0 = unID (gfoldl k ID x0)
307 where
308 k :: Data d => ID (d->b) -> d -> ID b
309 k (ID c) x = ID (c (f x))
310
311
312 -- | A generic query with a left-associative binary operator
313 gmapQl :: forall r r'. (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
314 gmapQl o r f = unCONST . gfoldl k z
315 where
316 k :: Data d => CONST r (d->b) -> d -> CONST r b
317 k c x = CONST $ (unCONST c) `o` f x
318 z :: g -> CONST r g
319 z _ = CONST r
320
321 -- | A generic query with a right-associative binary operator
322 gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r
323 gmapQr o r0 f x0 = unQr (gfoldl k (const (Qr id)) x0) r0
324 where
325 k :: Data d => Qr r (d->b) -> d -> Qr r b
326 k (Qr c) x = Qr (\r -> c (f x `o` r))
327
328
329 -- | A generic query that processes the immediate subterms and returns a list
330 -- of results. The list is given in the same order as originally specified
331 -- in the declaration of the data constructors.
332 gmapQ :: (forall d. Data d => d -> u) -> a -> [u]
333 gmapQ f = gmapQr (:) [] f
334
335
336 -- | A generic query that processes one child by index (zero-based)
337 gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> a -> u
338 gmapQi i f x = case gfoldl k z x of { Qi _ q -> fromJust q }
339 where
340 k :: Data d => Qi u (d -> b) -> d -> Qi u b
341 k (Qi i' q) a = Qi (i'+1) (if i==i' then Just (f a) else q)
342 z :: g -> Qi q g
343 z _ = Qi 0 Nothing
344
345
346 -- | A generic monadic transformation that maps over the immediate subterms
347 --
348 -- The default definition instantiates the type constructor @c@ in
349 -- the type of 'gfoldl' to the monad datatype constructor, defining
350 -- injection and projection using 'return' and '>>='.
351 gmapM :: forall m. Monad m => (forall d. Data d => d -> m d) -> a -> m a
352
353 -- Use immediately the monad datatype constructor
354 -- to instantiate the type constructor c in the type of gfoldl,
355 -- so injection and projection is done by return and >>=.
356 --
357 gmapM f = gfoldl k return
358 where
359 k :: Data d => m (d -> b) -> d -> m b
360 k c x = do c' <- c
361 x' <- f x
362 return (c' x')
363
364
365 -- | Transformation of at least one immediate subterm does not fail
366 gmapMp :: forall m. MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a
367
368 {-
369
370 The type constructor that we use here simply keeps track of the fact
371 if we already succeeded for an immediate subterm; see Mp below. To
372 this end, we couple the monadic computation with a Boolean.
373
374 -}
375
376 gmapMp f x = unMp (gfoldl k z x) >>= \(x',b) ->
377 if b then return x' else mzero
378 where
379 z :: g -> Mp m g
380 z g = Mp (return (g,False))
381 k :: Data d => Mp m (d -> b) -> d -> Mp m b
382 k (Mp c) y
383 = Mp ( c >>= \(h, b) ->
384 (f y >>= \y' -> return (h y', True))
385 `mplus` return (h y, b)
386 )
387
388 -- | Transformation of one immediate subterm with success
389 gmapMo :: forall m. MonadPlus m => (forall d. Data d => d -> m d) -> a -> m a
390
391 {-
392
393 We use the same pairing trick as for gmapMp,
394 i.e., we use an extra Bool component to keep track of the
395 fact whether an immediate subterm was processed successfully.
396 However, we cut of mapping over subterms once a first subterm
397 was transformed successfully.
398
399 -}
400
401 gmapMo f x = unMp (gfoldl k z x) >>= \(x',b) ->
402 if b then return x' else mzero
403 where
404 z :: g -> Mp m g
405 z g = Mp (return (g,False))
406 k :: Data d => Mp m (d -> b) -> d -> Mp m b
407 k (Mp c) y
408 = Mp ( c >>= \(h,b) -> if b
409 then return (h y, b)
410 else (f y >>= \y' -> return (h y',True))
411 `mplus` return (h y, b)
412 )
413
414
415 -- | The identity type constructor needed for the definition of gmapT
416 newtype ID x = ID { unID :: x }
417
418
419 -- | The constant type constructor needed for the definition of gmapQl
420 newtype CONST c a = CONST { unCONST :: c }
421
422
423 -- | Type constructor for adding counters to queries
424 data Qi q a = Qi Int (Maybe q)
425
426
427 -- | The type constructor used in definition of gmapQr
428 newtype Qr r a = Qr { unQr :: r -> r }
429
430
431 -- | The type constructor used in definition of gmapMp
432 newtype Mp m x = Mp { unMp :: m (x, Bool) }
433
434
435
436 ------------------------------------------------------------------------------
437 --
438 -- Generic unfolding
439 --
440 ------------------------------------------------------------------------------
441
442
443 -- | Build a term skeleton
444 fromConstr :: Data a => Constr -> a
445 fromConstr = fromConstrB (error "Data.Data.fromConstr")
446
447
448 -- | Build a term and use a generic function for subterms
449 fromConstrB :: Data a
450 => (forall d. Data d => d)
451 -> Constr
452 -> a
453 fromConstrB f = unID . gunfold k z
454 where
455 k :: forall b r. Data b => ID (b -> r) -> ID r
456 k c = ID (unID c f)
457
458 z :: forall r. r -> ID r
459 z = ID
460
461
462 -- | Monadic variation on 'fromConstrB'
463 fromConstrM :: forall m a. (Monad m, Data a)
464 => (forall d. Data d => m d)
465 -> Constr
466 -> m a
467 fromConstrM f = gunfold k z
468 where
469 k :: forall b r. Data b => m (b -> r) -> m r
470 k c = do { c' <- c; b <- f; return (c' b) }
471
472 z :: forall r. r -> m r
473 z = return
474
475
476
477 ------------------------------------------------------------------------------
478 --
479 -- Datatype and constructor representations
480 --
481 ------------------------------------------------------------------------------
482
483
484 --
485 -- | Representation of datatypes.
486 -- A package of constructor representations with names of type and module.
487 --
488 data DataType = DataType
489 { tycon :: String
490 , datarep :: DataRep
491 }
492
493 deriving Show
494
495 -- | Representation of constructors. Note that equality on constructors
496 -- with different types may not work -- i.e. the constructors for 'False' and
497 -- 'Nothing' may compare equal.
498 data Constr = Constr
499 { conrep :: ConstrRep
500 , constring :: String
501 , confields :: [String] -- for AlgRep only
502 , confixity :: Fixity -- for AlgRep only
503 , datatype :: DataType
504 }
505
506 instance Show Constr where
507 show = constring
508
509
510 -- | Equality of constructors
511 instance Eq Constr where
512 c == c' = constrRep c == constrRep c'
513
514
515 -- | Public representation of datatypes
516 data DataRep = AlgRep [Constr]
517 | IntRep
518 | FloatRep
519 | CharRep
520 | NoRep
521
522 deriving (Eq,Show)
523 -- The list of constructors could be an array, a balanced tree, or others.
524
525
526 -- | Public representation of constructors
527 data ConstrRep = AlgConstr ConIndex
528 | IntConstr Integer
529 | FloatConstr Rational
530 | CharConstr Char
531
532 deriving (Eq,Show)
533
534
535 -- | Unique index for datatype constructors,
536 -- counting from 1 in the order they are given in the program text.
537 type ConIndex = Int
538
539
540 -- | Fixity of constructors
541 data Fixity = Prefix
542 | Infix -- Later: add associativity and precedence
543
544 deriving (Eq,Show)
545
546
547 ------------------------------------------------------------------------------
548 --
549 -- Observers for datatype representations
550 --
551 ------------------------------------------------------------------------------
552
553
554 -- | Gets the type constructor including the module
555 dataTypeName :: DataType -> String
556 dataTypeName = tycon
557
558
559
560 -- | Gets the public presentation of a datatype
561 dataTypeRep :: DataType -> DataRep
562 dataTypeRep = datarep
563
564
565 -- | Gets the datatype of a constructor
566 constrType :: Constr -> DataType
567 constrType = datatype
568
569
570 -- | Gets the public presentation of constructors
571 constrRep :: Constr -> ConstrRep
572 constrRep = conrep
573
574
575 -- | Look up a constructor by its representation
576 repConstr :: DataType -> ConstrRep -> Constr
577 repConstr dt cr =
578 case (dataTypeRep dt, cr) of
579 (AlgRep cs, AlgConstr i) -> cs !! (i-1)
580 (IntRep, IntConstr i) -> mkIntegralConstr dt i
581 (FloatRep, FloatConstr f) -> mkRealConstr dt f
582 (CharRep, CharConstr c) -> mkCharConstr dt c
583 _ -> error "Data.Data.repConstr: The given ConstrRep does not fit to the given DataType."
584
585
586
587 ------------------------------------------------------------------------------
588 --
589 -- Representations of algebraic data types
590 --
591 ------------------------------------------------------------------------------
592
593
594 -- | Constructs an algebraic datatype
595 mkDataType :: String -> [Constr] -> DataType
596 mkDataType str cs = DataType
597 { tycon = str
598 , datarep = AlgRep cs
599 }
600
601
602 -- | Constructs a constructor
603 mkConstr :: DataType -> String -> [String] -> Fixity -> Constr
604 mkConstr dt str fields fix =
605 Constr
606 { conrep = AlgConstr idx
607 , constring = str
608 , confields = fields
609 , confixity = fix
610 , datatype = dt
611 }
612 where
613 idx = head [ i | (c,i) <- dataTypeConstrs dt `zip` [1..],
614 showConstr c == str ]
615
616
617 -- | Gets the constructors of an algebraic datatype
618 dataTypeConstrs :: DataType -> [Constr]
619 dataTypeConstrs dt = case datarep dt of
620 (AlgRep cons) -> cons
621 _ -> error $ "Data.Data.dataTypeConstrs is not supported for "
622 ++ dataTypeName dt ++
623 ", as it is not an algebraic data type."
624
625
626 -- | Gets the field labels of a constructor. The list of labels
627 -- is returned in the same order as they were given in the original
628 -- constructor declaration.
629 constrFields :: Constr -> [String]
630 constrFields = confields
631
632
633 -- | Gets the fixity of a constructor
634 constrFixity :: Constr -> Fixity
635 constrFixity = confixity
636
637
638
639 ------------------------------------------------------------------------------
640 --
641 -- From strings to constr's and vice versa: all data types
642 --
643 ------------------------------------------------------------------------------
644
645
646 -- | Gets the string for a constructor
647 showConstr :: Constr -> String
648 showConstr = constring
649
650
651 -- | Lookup a constructor via a string
652 readConstr :: DataType -> String -> Maybe Constr
653 readConstr dt str =
654 case dataTypeRep dt of
655 AlgRep cons -> idx cons
656 IntRep -> mkReadCon (\i -> (mkPrimCon dt str (IntConstr i)))
657 FloatRep -> mkReadCon ffloat
658 CharRep -> mkReadCon (\c -> (mkPrimCon dt str (CharConstr c)))
659 NoRep -> Nothing
660 where
661
662 -- Read a value and build a constructor
663 mkReadCon :: Read t => (t -> Constr) -> Maybe Constr
664 mkReadCon f = case (reads str) of
665 [(t,"")] -> Just (f t)
666 _ -> Nothing
667
668 -- Traverse list of algebraic datatype constructors
669 idx :: [Constr] -> Maybe Constr
670 idx cons = let fit = filter ((==) str . showConstr) cons
671 in if fit == []
672 then Nothing
673 else Just (head fit)
674
675 ffloat :: Double -> Constr
676 ffloat = mkPrimCon dt str . FloatConstr . toRational
677
678 ------------------------------------------------------------------------------
679 --
680 -- Convenience funtions: algebraic data types
681 --
682 ------------------------------------------------------------------------------
683
684
685 -- | Test for an algebraic type
686 isAlgType :: DataType -> Bool
687 isAlgType dt = case datarep dt of
688 (AlgRep _) -> True
689 _ -> False
690
691
692 -- | Gets the constructor for an index (algebraic datatypes only)
693 indexConstr :: DataType -> ConIndex -> Constr
694 indexConstr dt idx = case datarep dt of
695 (AlgRep cs) -> cs !! (idx-1)
696 _ -> error $ "Data.Data.indexConstr is not supported for "
697 ++ dataTypeName dt ++
698 ", as it is not an algebraic data type."
699
700
701 -- | Gets the index of a constructor (algebraic datatypes only)
702 constrIndex :: Constr -> ConIndex
703 constrIndex con = case constrRep con of
704 (AlgConstr idx) -> idx
705 _ -> error $ "Data.Data.constrIndex is not supported for "
706 ++ dataTypeName (constrType con) ++
707 ", as it is not an algebraic data type."
708
709
710 -- | Gets the maximum constructor index of an algebraic datatype
711 maxConstrIndex :: DataType -> ConIndex
712 maxConstrIndex dt = case dataTypeRep dt of
713 AlgRep cs -> length cs
714 _ -> error $ "Data.Data.maxConstrIndex is not supported for "
715 ++ dataTypeName dt ++
716 ", as it is not an algebraic data type."
717
718
719
720 ------------------------------------------------------------------------------
721 --
722 -- Representation of primitive types
723 --
724 ------------------------------------------------------------------------------
725
726
727 -- | Constructs the 'Int' type
728 mkIntType :: String -> DataType
729 mkIntType = mkPrimType IntRep
730
731
732 -- | Constructs the 'Float' type
733 mkFloatType :: String -> DataType
734 mkFloatType = mkPrimType FloatRep
735
736
737 -- | Constructs the 'Char' type
738 mkCharType :: String -> DataType
739 mkCharType = mkPrimType CharRep
740
741
742 -- | Helper for 'mkIntType', 'mkFloatType'
743 mkPrimType :: DataRep -> String -> DataType
744 mkPrimType dr str = DataType
745 { tycon = str
746 , datarep = dr
747 }
748
749
750 -- Makes a constructor for primitive types
751 mkPrimCon :: DataType -> String -> ConstrRep -> Constr
752 mkPrimCon dt str cr = Constr
753 { datatype = dt
754 , conrep = cr
755 , constring = str
756 , confields = error "Data.Data.confields"
757 , confixity = error "Data.Data.confixity"
758 }
759
760 mkIntegralConstr :: (Integral a, Show a) => DataType -> a -> Constr
761 mkIntegralConstr dt i = case datarep dt of
762 IntRep -> mkPrimCon dt (show i) (IntConstr (toInteger i))
763 _ -> error $ "Data.Data.mkIntegralConstr is not supported for "
764 ++ dataTypeName dt ++
765 ", as it is not an Integral data type."
766
767 mkRealConstr :: (Real a, Show a) => DataType -> a -> Constr
768 mkRealConstr dt f = case datarep dt of
769 FloatRep -> mkPrimCon dt (show f) (FloatConstr (toRational f))
770 _ -> error $ "Data.Data.mkRealConstr is not supported for "
771 ++ dataTypeName dt ++
772 ", as it is not an Real data type."
773
774 -- | Makes a constructor for 'Char'.
775 mkCharConstr :: DataType -> Char -> Constr
776 mkCharConstr dt c = case datarep dt of
777 CharRep -> mkPrimCon dt (show c) (CharConstr c)
778 _ -> error $ "Data.Data.mkCharConstr is not supported for "
779 ++ dataTypeName dt ++
780 ", as it is not an Char data type."
781
782
783 ------------------------------------------------------------------------------
784 --
785 -- Non-representations for non-representable types
786 --
787 ------------------------------------------------------------------------------
788
789
790 -- | Constructs a non-representation for a non-representable type
791 mkNoRepType :: String -> DataType
792 mkNoRepType str = DataType
793 { tycon = str
794 , datarep = NoRep
795 }
796
797 -- | Test for a non-representable type
798 isNorepType :: DataType -> Bool
799 isNorepType dt = case datarep dt of
800 NoRep -> True
801 _ -> False
802
803
804
805 ------------------------------------------------------------------------------
806 --
807 -- Convenience for qualified type constructors
808 --
809 ------------------------------------------------------------------------------
810
811
812 -- | Gets the unqualified type constructor:
813 -- drop *.*.*... before name
814 --
815 tyconUQname :: String -> String
816 tyconUQname x = let x' = dropWhile (not . (==) '.') x
817 in if x' == [] then x else tyconUQname (tail x')
818
819
820 -- | Gets the module of a type constructor:
821 -- take *.*.*... before name
822 tyconModule :: String -> String
823 tyconModule x = let (a,b) = break ((==) '.') x
824 in if b == ""
825 then b
826 else a ++ tyconModule' (tail b)
827 where
828 tyconModule' y = let y' = tyconModule y
829 in if y' == "" then "" else ('.':y')
830
831
832
833
834 ------------------------------------------------------------------------------
835 ------------------------------------------------------------------------------
836 --
837 -- Instances of the Data class for Prelude-like types.
838 -- We define top-level definitions for representations.
839 --
840 ------------------------------------------------------------------------------
841
842
843 falseConstr :: Constr
844 falseConstr = mkConstr boolDataType "False" [] Prefix
845 trueConstr :: Constr
846 trueConstr = mkConstr boolDataType "True" [] Prefix
847
848 boolDataType :: DataType
849 boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
850
851 instance Data Bool where
852 toConstr False = falseConstr
853 toConstr True = trueConstr
854 gunfold _ z c = case constrIndex c of
855 1 -> z False
856 2 -> z True
857 _ -> error $ "Data.Data.gunfold: Constructor "
858 ++ show c
859 ++ " is not of type Bool."
860 dataTypeOf _ = boolDataType
861
862
863 ------------------------------------------------------------------------------
864
865 charType :: DataType
866 charType = mkCharType "Prelude.Char"
867
868 instance Data Char where
869 toConstr x = mkCharConstr charType x
870 gunfold _ z c = case constrRep c of
871 (CharConstr x) -> z x
872 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
873 ++ " is not of type Char."
874 dataTypeOf _ = charType
875
876
877 ------------------------------------------------------------------------------
878
879 floatType :: DataType
880 floatType = mkFloatType "Prelude.Float"
881
882 instance Data Float where
883 toConstr = mkRealConstr floatType
884 gunfold _ z c = case constrRep c of
885 (FloatConstr x) -> z (realToFrac x)
886 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
887 ++ " is not of type Float."
888 dataTypeOf _ = floatType
889
890
891 ------------------------------------------------------------------------------
892
893 doubleType :: DataType
894 doubleType = mkFloatType "Prelude.Double"
895
896 instance Data Double where
897 toConstr = mkRealConstr doubleType
898 gunfold _ z c = case constrRep c of
899 (FloatConstr x) -> z (realToFrac x)
900 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
901 ++ " is not of type Double."
902 dataTypeOf _ = doubleType
903
904
905 ------------------------------------------------------------------------------
906
907 intType :: DataType
908 intType = mkIntType "Prelude.Int"
909
910 instance Data Int where
911 toConstr x = mkIntegralConstr intType x
912 gunfold _ z c = case constrRep c of
913 (IntConstr x) -> z (fromIntegral x)
914 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
915 ++ " is not of type Int."
916 dataTypeOf _ = intType
917
918
919 ------------------------------------------------------------------------------
920
921 integerType :: DataType
922 integerType = mkIntType "Prelude.Integer"
923
924 instance Data Integer where
925 toConstr = mkIntegralConstr integerType
926 gunfold _ z c = case constrRep c of
927 (IntConstr x) -> z x
928 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
929 ++ " is not of type Integer."
930 dataTypeOf _ = integerType
931
932
933 ------------------------------------------------------------------------------
934
935 int8Type :: DataType
936 int8Type = mkIntType "Data.Int.Int8"
937
938 instance Data Int8 where
939 toConstr x = mkIntegralConstr int8Type x
940 gunfold _ z c = case constrRep c of
941 (IntConstr x) -> z (fromIntegral x)
942 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
943 ++ " is not of type Int8."
944 dataTypeOf _ = int8Type
945
946
947 ------------------------------------------------------------------------------
948
949 int16Type :: DataType
950 int16Type = mkIntType "Data.Int.Int16"
951
952 instance Data Int16 where
953 toConstr x = mkIntegralConstr int16Type x
954 gunfold _ z c = case constrRep c of
955 (IntConstr x) -> z (fromIntegral x)
956 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
957 ++ " is not of type Int16."
958 dataTypeOf _ = int16Type
959
960
961 ------------------------------------------------------------------------------
962
963 int32Type :: DataType
964 int32Type = mkIntType "Data.Int.Int32"
965
966 instance Data Int32 where
967 toConstr x = mkIntegralConstr int32Type x
968 gunfold _ z c = case constrRep c of
969 (IntConstr x) -> z (fromIntegral x)
970 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
971 ++ " is not of type Int32."
972 dataTypeOf _ = int32Type
973
974
975 ------------------------------------------------------------------------------
976
977 int64Type :: DataType
978 int64Type = mkIntType "Data.Int.Int64"
979
980 instance Data Int64 where
981 toConstr x = mkIntegralConstr int64Type x
982 gunfold _ z c = case constrRep c of
983 (IntConstr x) -> z (fromIntegral x)
984 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
985 ++ " is not of type Int64."
986 dataTypeOf _ = int64Type
987
988
989 ------------------------------------------------------------------------------
990
991 wordType :: DataType
992 wordType = mkIntType "Data.Word.Word"
993
994 instance Data Word where
995 toConstr x = mkIntegralConstr wordType x
996 gunfold _ z c = case constrRep c of
997 (IntConstr x) -> z (fromIntegral x)
998 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
999 ++ " is not of type Word"
1000 dataTypeOf _ = wordType
1001
1002
1003 ------------------------------------------------------------------------------
1004
1005 word8Type :: DataType
1006 word8Type = mkIntType "Data.Word.Word8"
1007
1008 instance Data Word8 where
1009 toConstr x = mkIntegralConstr word8Type x
1010 gunfold _ z c = case constrRep c of
1011 (IntConstr x) -> z (fromIntegral x)
1012 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
1013 ++ " is not of type Word8."
1014 dataTypeOf _ = word8Type
1015
1016
1017 ------------------------------------------------------------------------------
1018
1019 word16Type :: DataType
1020 word16Type = mkIntType "Data.Word.Word16"
1021
1022 instance Data Word16 where
1023 toConstr x = mkIntegralConstr word16Type x
1024 gunfold _ z c = case constrRep c of
1025 (IntConstr x) -> z (fromIntegral x)
1026 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
1027 ++ " is not of type Word16."
1028 dataTypeOf _ = word16Type
1029
1030
1031 ------------------------------------------------------------------------------
1032
1033 word32Type :: DataType
1034 word32Type = mkIntType "Data.Word.Word32"
1035
1036 instance Data Word32 where
1037 toConstr x = mkIntegralConstr word32Type x
1038 gunfold _ z c = case constrRep c of
1039 (IntConstr x) -> z (fromIntegral x)
1040 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
1041 ++ " is not of type Word32."
1042 dataTypeOf _ = word32Type
1043
1044
1045 ------------------------------------------------------------------------------
1046
1047 word64Type :: DataType
1048 word64Type = mkIntType "Data.Word.Word64"
1049
1050 instance Data Word64 where
1051 toConstr x = mkIntegralConstr word64Type x
1052 gunfold _ z c = case constrRep c of
1053 (IntConstr x) -> z (fromIntegral x)
1054 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c
1055 ++ " is not of type Word64."
1056 dataTypeOf _ = word64Type
1057
1058
1059 ------------------------------------------------------------------------------
1060
1061 ratioConstr :: Constr
1062 ratioConstr = mkConstr ratioDataType ":%" [] Infix
1063
1064 ratioDataType :: DataType
1065 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
1066
1067 instance Data a => Data (Ratio a) where
1068 gfoldl k z (a :% b) = z (:%) `k` a `k` b
1069 toConstr _ = ratioConstr
1070 gunfold k z c | constrIndex c == 1 = k (k (z (:%)))
1071 gunfold _ _ _ = error "Data.Data.gunfold(Ratio)"
1072 dataTypeOf _ = ratioDataType
1073
1074
1075 ------------------------------------------------------------------------------
1076
1077 nilConstr :: Constr
1078 nilConstr = mkConstr listDataType "[]" [] Prefix
1079 consConstr :: Constr
1080 consConstr = mkConstr listDataType "(:)" [] Infix
1081
1082 listDataType :: DataType
1083 listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
1084
1085 instance Data a => Data [a] where
1086 gfoldl _ z [] = z []
1087 gfoldl f z (x:xs) = z (:) `f` x `f` xs
1088 toConstr [] = nilConstr
1089 toConstr (_:_) = consConstr
1090 gunfold k z c = case constrIndex c of
1091 1 -> z []
1092 2 -> k (k (z (:)))
1093 _ -> error "Data.Data.gunfold(List)"
1094 dataTypeOf _ = listDataType
1095 dataCast1 f = gcast1 f
1096
1097 --
1098 -- The gmaps are given as an illustration.
1099 -- This shows that the gmaps for lists are different from list maps.
1100 --
1101 gmapT _ [] = []
1102 gmapT f (x:xs) = (f x:f xs)
1103 gmapQ _ [] = []
1104 gmapQ f (x:xs) = [f x,f xs]
1105 gmapM _ [] = return []
1106 gmapM f (x:xs) = f x >>= \x' -> f xs >>= \xs' -> return (x':xs')
1107
1108
1109 ------------------------------------------------------------------------------
1110
1111 nothingConstr :: Constr
1112 nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix
1113 justConstr :: Constr
1114 justConstr = mkConstr maybeDataType "Just" [] Prefix
1115
1116 maybeDataType :: DataType
1117 maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]
1118
1119 instance Data a => Data (Maybe a) where
1120 gfoldl _ z Nothing = z Nothing
1121 gfoldl f z (Just x) = z Just `f` x
1122 toConstr Nothing = nothingConstr
1123 toConstr (Just _) = justConstr
1124 gunfold k z c = case constrIndex c of
1125 1 -> z Nothing
1126 2 -> k (z Just)
1127 _ -> error "Data.Data.gunfold(Maybe)"
1128 dataTypeOf _ = maybeDataType
1129 dataCast1 f = gcast1 f
1130
1131
1132 ------------------------------------------------------------------------------
1133
1134 ltConstr :: Constr
1135 ltConstr = mkConstr orderingDataType "LT" [] Prefix
1136 eqConstr :: Constr
1137 eqConstr = mkConstr orderingDataType "EQ" [] Prefix
1138 gtConstr :: Constr
1139 gtConstr = mkConstr orderingDataType "GT" [] Prefix
1140
1141 orderingDataType :: DataType
1142 orderingDataType = mkDataType "Prelude.Ordering" [ltConstr,eqConstr,gtConstr]
1143
1144 instance Data Ordering where
1145 gfoldl _ z LT = z LT
1146 gfoldl _ z EQ = z EQ
1147 gfoldl _ z GT = z GT
1148 toConstr LT = ltConstr
1149 toConstr EQ = eqConstr
1150 toConstr GT = gtConstr
1151 gunfold _ z c = case constrIndex c of
1152 1 -> z LT
1153 2 -> z EQ
1154 3 -> z GT
1155 _ -> error "Data.Data.gunfold(Ordering)"
1156 dataTypeOf _ = orderingDataType
1157
1158
1159 ------------------------------------------------------------------------------
1160
1161 leftConstr :: Constr
1162 leftConstr = mkConstr eitherDataType "Left" [] Prefix
1163
1164 rightConstr :: Constr
1165 rightConstr = mkConstr eitherDataType "Right" [] Prefix
1166
1167 eitherDataType :: DataType
1168 eitherDataType = mkDataType "Prelude.Either" [leftConstr,rightConstr]
1169
1170 instance (Data a, Data b) => Data (Either a b) where
1171 gfoldl f z (Left a) = z Left `f` a
1172 gfoldl f z (Right a) = z Right `f` a
1173 toConstr (Left _) = leftConstr
1174 toConstr (Right _) = rightConstr
1175 gunfold k z c = case constrIndex c of
1176 1 -> k (z Left)
1177 2 -> k (z Right)
1178 _ -> error "Data.Data.gunfold(Either)"
1179 dataTypeOf _ = eitherDataType
1180 dataCast2 f = gcast2 f
1181
1182
1183 ------------------------------------------------------------------------------
1184
1185 tuple0Constr :: Constr
1186 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
1187
1188 tuple0DataType :: DataType
1189 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
1190
1191 instance Data () where
1192 toConstr () = tuple0Constr
1193 gunfold _ z c | constrIndex c == 1 = z ()
1194 gunfold _ _ _ = error "Data.Data.gunfold(unit)"
1195 dataTypeOf _ = tuple0DataType
1196
1197
1198 ------------------------------------------------------------------------------
1199
1200 tuple2Constr :: Constr
1201 tuple2Constr = mkConstr tuple2DataType "(,)" [] Infix
1202
1203 tuple2DataType :: DataType
1204 tuple2DataType = mkDataType "Prelude.(,)" [tuple2Constr]
1205
1206 instance (Data a, Data b) => Data (a,b) where
1207 gfoldl f z (a,b) = z (,) `f` a `f` b
1208 toConstr (_,_) = tuple2Constr
1209 gunfold k z c | constrIndex c == 1 = k (k (z (,)))
1210 gunfold _ _ _ = error "Data.Data.gunfold(tup2)"
1211 dataTypeOf _ = tuple2DataType
1212 dataCast2 f = gcast2 f
1213
1214
1215 ------------------------------------------------------------------------------
1216
1217 tuple3Constr :: Constr
1218 tuple3Constr = mkConstr tuple3DataType "(,,)" [] Infix
1219
1220 tuple3DataType :: DataType
1221 tuple3DataType = mkDataType "Prelude.(,,)" [tuple3Constr]
1222
1223 instance (Data a, Data b, Data c) => Data (a,b,c) where
1224 gfoldl f z (a,b,c) = z (,,) `f` a `f` b `f` c
1225 toConstr (_,_,_) = tuple3Constr
1226 gunfold k z c | constrIndex c == 1 = k (k (k (z (,,))))
1227 gunfold _ _ _ = error "Data.Data.gunfold(tup3)"
1228 dataTypeOf _ = tuple3DataType
1229
1230
1231 ------------------------------------------------------------------------------
1232
1233 tuple4Constr :: Constr
1234 tuple4Constr = mkConstr tuple4DataType "(,,,)" [] Infix
1235
1236 tuple4DataType :: DataType
1237 tuple4DataType = mkDataType "Prelude.(,,,)" [tuple4Constr]
1238
1239 instance (Data a, Data b, Data c, Data d)
1240 => Data (a,b,c,d) where
1241 gfoldl f z (a,b,c,d) = z (,,,) `f` a `f` b `f` c `f` d
1242 toConstr (_,_,_,_) = tuple4Constr
1243 gunfold k z c = case constrIndex c of
1244 1 -> k (k (k (k (z (,,,)))))
1245 _ -> error "Data.Data.gunfold(tup4)"
1246 dataTypeOf _ = tuple4DataType
1247
1248
1249 ------------------------------------------------------------------------------
1250
1251 tuple5Constr :: Constr
1252 tuple5Constr = mkConstr tuple5DataType "(,,,,)" [] Infix
1253
1254 tuple5DataType :: DataType
1255 tuple5DataType = mkDataType "Prelude.(,,,,)" [tuple5Constr]
1256
1257 instance (Data a, Data b, Data c, Data d, Data e)
1258 => Data (a,b,c,d,e) where
1259 gfoldl f z (a,b,c,d,e) = z (,,,,) `f` a `f` b `f` c `f` d `f` e
1260 toConstr (_,_,_,_,_) = tuple5Constr
1261 gunfold k z c = case constrIndex c of
1262 1 -> k (k (k (k (k (z (,,,,))))))
1263 _ -> error "Data.Data.gunfold(tup5)"
1264 dataTypeOf _ = tuple5DataType
1265
1266
1267 ------------------------------------------------------------------------------
1268
1269 tuple6Constr :: Constr
1270 tuple6Constr = mkConstr tuple6DataType "(,,,,,)" [] Infix
1271
1272 tuple6DataType :: DataType
1273 tuple6DataType = mkDataType "Prelude.(,,,,,)" [tuple6Constr]
1274
1275 instance (Data a, Data b, Data c, Data d, Data e, Data f)
1276 => Data (a,b,c,d,e,f) where
1277 gfoldl f z (a,b,c,d,e,f') = z (,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f'
1278 toConstr (_,_,_,_,_,_) = tuple6Constr
1279 gunfold k z c = case constrIndex c of
1280 1 -> k (k (k (k (k (k (z (,,,,,)))))))
1281 _ -> error "Data.Data.gunfold(tup6)"
1282 dataTypeOf _ = tuple6DataType
1283
1284
1285 ------------------------------------------------------------------------------
1286
1287 tuple7Constr :: Constr
1288 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
1289
1290 tuple7DataType :: DataType
1291 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
1292
1293 instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
1294 => Data (a,b,c,d,e,f,g) where
1295 gfoldl f z (a,b,c,d,e,f',g) =
1296 z (,,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f' `f` g
1297 toConstr (_,_,_,_,_,_,_) = tuple7Constr
1298 gunfold k z c = case constrIndex c of
1299 1 -> k (k (k (k (k (k (k (z (,,,,,,))))))))
1300 _ -> error "Data.Data.gunfold(tup7)"
1301 dataTypeOf _ = tuple7DataType
1302
1303
1304 ------------------------------------------------------------------------------
1305
1306 instance Data a => Data (Ptr a) where
1307 toConstr _ = error "Data.Data.toConstr(Ptr)"
1308 gunfold _ _ = error "Data.Data.gunfold(Ptr)"
1309 dataTypeOf _ = mkNoRepType "GHC.Ptr.Ptr"
1310 dataCast1 x = gcast1 x
1311
1312 ------------------------------------------------------------------------------
1313
1314 instance Data a => Data (ForeignPtr a) where
1315 toConstr _ = error "Data.Data.toConstr(ForeignPtr)"
1316 gunfold _ _ = error "Data.Data.gunfold(ForeignPtr)"
1317 dataTypeOf _ = mkNoRepType "GHC.ForeignPtr.ForeignPtr"
1318 dataCast1 x = gcast1 x
1319
1320 ------------------------------------------------------------------------------
1321 -- The Data instance for Array preserves data abstraction at the cost of
1322 -- inefficiency. We omit reflection services for the sake of data abstraction.
1323 instance (Data a, Data b, Ix a) => Data (Array a b)
1324 where
1325 gfoldl f z a = z (listArray (bounds a)) `f` (elems a)
1326 toConstr _ = error "Data.Data.toConstr(Array)"
1327 gunfold _ _ = error "Data.Data.gunfold(Array)"
1328 dataTypeOf _ = mkNoRepType "Data.Array.Array"
1329 dataCast2 x = gcast2 x
1330
1331 ----------------------------------------------------------------------------
1332 -- Data instance for Proxy
1333
1334 proxyConstr :: Constr
1335 proxyConstr = mkConstr proxyDataType "Proxy" [] Prefix
1336
1337 proxyDataType :: DataType
1338 proxyDataType = mkDataType "Data.Proxy.Proxy" [proxyConstr]
1339
1340 instance (Data t) => Data (Proxy t) where
1341 gfoldl _ z Proxy = z Proxy
1342 toConstr Proxy = proxyConstr
1343 gunfold _ z c = case constrIndex c of
1344 1 -> z Proxy
1345 _ -> error "Data.Data.gunfold(Proxy)"
1346 dataTypeOf _ = proxyDataType
1347 dataCast1 f = gcast1 f
1348
1349 -----------------------------------------------------------------------
1350 -- instance for (:~:)
1351
1352 reflConstr :: Constr
1353 reflConstr = mkConstr equalityDataType "Refl" [] Prefix
1354
1355 equalityDataType :: DataType
1356 equalityDataType = mkDataType "Data.Type.Equality.(:~:)" [reflConstr]
1357
1358 instance (a ~ b, Data a) => Data (a :~: b) where
1359 gfoldl _ z Refl = z Refl
1360 toConstr Refl = reflConstr
1361 gunfold _ z c = case constrIndex c of
1362 1 -> z Refl
1363 _ -> error "Data.Data.gunfold(:~:)"
1364 dataTypeOf _ = equalityDataType
1365 dataCast2 f = gcast2 f
1366
1367 -----------------------------------------------------------------------
1368 -- instance for Coercion
1369
1370 coercionConstr :: Constr
1371 coercionConstr = mkConstr equalityDataType "Coercion" [] Prefix
1372
1373 coercionDataType :: DataType
1374 coercionDataType = mkDataType "Data.Type.Coercion.Coercion" [coercionConstr]
1375
1376 instance (Coercible a b, Data a, Data b) => Data (Coercion a b) where
1377 gfoldl _ z Coercion = z Coercion
1378 toConstr Coercion = coercionConstr
1379 gunfold _ z c = case constrIndex c of
1380 1 -> z Coercion
1381 _ -> error "Data.Data.gunfold(Coercion)"
1382 dataTypeOf _ = coercionDataType
1383 dataCast2 f = gcast2 f
1384
1385 -----------------------------------------------------------------------
1386 -- instance for Data.Version
1387
1388 versionConstr :: Constr
1389 versionConstr = mkConstr versionDataType "Version" ["versionBranch","versionTags"] Prefix
1390
1391 versionDataType :: DataType
1392 versionDataType = mkDataType "Data.Version.Version" [versionConstr]
1393
1394 instance Data Version where
1395 gfoldl k z (Version bs ts) = z Version `k` bs `k` ts
1396 toConstr (Version _ _) = versionConstr
1397 gunfold k z c = case constrIndex c of
1398 1 -> k (k (z Version))
1399 _ -> error "Data.Data.gunfold(Version)"
1400 dataTypeOf _ = versionDataType