1 -----------------------------------------------------------------------------
3 -- Module : Data.Generics.Instances
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 (uses Data.Generics.Basics)
11 -- \"Scrap your boilerplate\" --- Generic programming in Haskell
12 -- See <http://www.cs.vu.nl/boilerplate/>. The present module
13 -- instantiates the class Data for Prelude-like datatypes.
14 -- (This module does not export anything. It really just defines instances.)
16 -----------------------------------------------------------------------------
18 {-# OPTIONS_GHC -fno-warn-orphans #-}
19 module Data.Generics.Instances where
22 ------------------------------------------------------------------------------
28 import Data.Generics.Basics
31 import Data.Int -- So we can give Data instance for Int8, ...
32 import Data.Word -- So we can give Data instance for Word8, ...
33 import GHC.Real( Ratio(..) ) -- So we can give Data instance for Ratio
34 import GHC.IOBase -- So we can give Data instance for IO, Handle
35 import GHC.Ptr -- So we can give Data instance for Ptr
36 import GHC.ForeignPtr -- So we can give Data instance for ForeignPtr
37 import GHC.Stable -- So we can give Data instance for StablePtr
38 import GHC.ST -- So we can give Data instance for ST
39 import GHC.Conc -- So we can give Data instance for MVar & Co.
40 import GHC.Arr -- So we can give Data instance for Array
45 ------------------------------------------------------------------------------
47 -- Instances of the Data class for Prelude-like types.
48 -- We define top-level definitions for representations.
50 ------------------------------------------------------------------------------
54 falseConstr = mkConstr boolDataType "False" [] Prefix
56 trueConstr = mkConstr boolDataType "True" [] Prefix
58 boolDataType :: DataType
59 boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
61 instance Data Bool where
62 toConstr False = falseConstr
63 toConstr True = trueConstr
64 gunfold _ z c = case constrIndex c of
68 dataTypeOf _ = boolDataType
71 ------------------------------------------------------------------------------
74 charType = mkStringType "Prelude.Char"
76 instance Data Char where
77 toConstr x = mkStringConstr charType [x]
78 gunfold _ z c = case constrRep c of
79 (StringConstr [x]) -> z x
81 dataTypeOf _ = charType
84 ------------------------------------------------------------------------------
87 floatType = mkFloatType "Prelude.Float"
89 instance Data Float where
90 toConstr x = mkFloatConstr floatType (realToFrac x)
91 gunfold _ z c = case constrRep c of
92 (FloatConstr x) -> z (realToFrac x)
94 dataTypeOf _ = floatType
97 ------------------------------------------------------------------------------
99 doubleType :: DataType
100 doubleType = mkFloatType "Prelude.Double"
102 instance Data Double where
103 toConstr = mkFloatConstr floatType
104 gunfold _ z c = case constrRep c of
105 (FloatConstr x) -> z x
107 dataTypeOf _ = doubleType
110 ------------------------------------------------------------------------------
113 intType = mkIntType "Prelude.Int"
115 instance Data Int where
116 toConstr x = mkIntConstr intType (fromIntegral x)
117 gunfold _ z c = case constrRep c of
118 (IntConstr x) -> z (fromIntegral x)
120 dataTypeOf _ = intType
123 ------------------------------------------------------------------------------
125 integerType :: DataType
126 integerType = mkIntType "Prelude.Integer"
128 instance Data Integer where
129 toConstr = mkIntConstr integerType
130 gunfold _ z c = case constrRep c of
133 dataTypeOf _ = integerType
136 ------------------------------------------------------------------------------
139 int8Type = mkIntType "Data.Int.Int8"
141 instance Data Int8 where
142 toConstr x = mkIntConstr int8Type (fromIntegral x)
143 gunfold _ z c = case constrRep c of
144 (IntConstr x) -> z (fromIntegral x)
146 dataTypeOf _ = int8Type
149 ------------------------------------------------------------------------------
151 int16Type :: DataType
152 int16Type = mkIntType "Data.Int.Int16"
154 instance Data Int16 where
155 toConstr x = mkIntConstr int16Type (fromIntegral x)
156 gunfold _ z c = case constrRep c of
157 (IntConstr x) -> z (fromIntegral x)
159 dataTypeOf _ = int16Type
162 ------------------------------------------------------------------------------
164 int32Type :: DataType
165 int32Type = mkIntType "Data.Int.Int32"
167 instance Data Int32 where
168 toConstr x = mkIntConstr int32Type (fromIntegral x)
169 gunfold _ z c = case constrRep c of
170 (IntConstr x) -> z (fromIntegral x)
172 dataTypeOf _ = int32Type
175 ------------------------------------------------------------------------------
177 int64Type :: DataType
178 int64Type = mkIntType "Data.Int.Int64"
180 instance Data Int64 where
181 toConstr x = mkIntConstr int64Type (fromIntegral x)
182 gunfold _ z c = case constrRep c of
183 (IntConstr x) -> z (fromIntegral x)
185 dataTypeOf _ = int64Type
188 ------------------------------------------------------------------------------
191 wordType = mkIntType "Data.Word.Word"
193 instance Data Word where
194 toConstr x = mkIntConstr wordType (fromIntegral x)
195 gunfold _ z c = case constrRep c of
196 (IntConstr x) -> z (fromIntegral x)
198 dataTypeOf _ = wordType
201 ------------------------------------------------------------------------------
203 word8Type :: DataType
204 word8Type = mkIntType "Data.Word.Word8"
206 instance Data Word8 where
207 toConstr x = mkIntConstr word8Type (fromIntegral x)
208 gunfold _ z c = case constrRep c of
209 (IntConstr x) -> z (fromIntegral x)
211 dataTypeOf _ = word8Type
214 ------------------------------------------------------------------------------
216 word16Type :: DataType
217 word16Type = mkIntType "Data.Word.Word16"
219 instance Data Word16 where
220 toConstr x = mkIntConstr word16Type (fromIntegral x)
221 gunfold _ z c = case constrRep c of
222 (IntConstr x) -> z (fromIntegral x)
224 dataTypeOf _ = word16Type
227 ------------------------------------------------------------------------------
229 word32Type :: DataType
230 word32Type = mkIntType "Data.Word.Word32"
232 instance Data Word32 where
233 toConstr x = mkIntConstr word32Type (fromIntegral x)
234 gunfold _ z c = case constrRep c of
235 (IntConstr x) -> z (fromIntegral x)
237 dataTypeOf _ = word32Type
240 ------------------------------------------------------------------------------
242 word64Type :: DataType
243 word64Type = mkIntType "Data.Word.Word64"
245 instance Data Word64 where
246 toConstr x = mkIntConstr word64Type (fromIntegral x)
247 gunfold _ z c = case constrRep c of
248 (IntConstr x) -> z (fromIntegral x)
250 dataTypeOf _ = word64Type
253 ------------------------------------------------------------------------------
255 ratioConstr :: Constr
256 ratioConstr = mkConstr ratioDataType ":%" [] Infix
258 ratioDataType :: DataType
259 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
261 instance (Data a, Integral a) => Data (Ratio a) where
262 toConstr _ = ratioConstr
263 gunfold k z c | constrIndex c == 1 = k (k (z (:%)))
264 gunfold _ _ _ = error "gunfold"
265 dataTypeOf _ = ratioDataType
268 ------------------------------------------------------------------------------
271 nilConstr = mkConstr listDataType "[]" [] Prefix
273 consConstr = mkConstr listDataType "(:)" [] Infix
275 listDataType :: DataType
276 listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
278 instance Data a => Data [a] where
280 gfoldl f z (x:xs) = z (:) `f` x `f` xs
281 toConstr [] = nilConstr
282 toConstr (_:_) = consConstr
283 gunfold k z c = case constrIndex c of
287 dataTypeOf _ = listDataType
288 dataCast1 f = gcast1 f
291 -- The gmaps are given as an illustration.
292 -- This shows that the gmaps for lists are different from list maps.
295 gmapT f (x:xs) = (f x:f xs)
297 gmapQ f (x:xs) = [f x,f xs]
298 gmapM _ [] = return []
299 gmapM f (x:xs) = f x >>= \x' -> f xs >>= \xs' -> return (x':xs')
302 ------------------------------------------------------------------------------
304 nothingConstr :: Constr
305 nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix
307 justConstr = mkConstr maybeDataType "Just" [] Prefix
309 maybeDataType :: DataType
310 maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]
312 instance Data a => Data (Maybe a) where
313 gfoldl _ z Nothing = z Nothing
314 gfoldl f z (Just x) = z Just `f` x
315 toConstr Nothing = nothingConstr
316 toConstr (Just _) = justConstr
317 gunfold k z c = case constrIndex c of
321 dataTypeOf _ = maybeDataType
322 dataCast1 f = gcast1 f
325 ------------------------------------------------------------------------------
328 ltConstr = mkConstr orderingDataType "LT" [] Prefix
330 eqConstr = mkConstr orderingDataType "EQ" [] Prefix
332 gtConstr = mkConstr orderingDataType "GT" [] Prefix
334 orderingDataType :: DataType
335 orderingDataType = mkDataType "Prelude.Ordering" [ltConstr,eqConstr,gtConstr]
337 instance Data Ordering where
341 toConstr LT = ltConstr
342 toConstr EQ = eqConstr
343 toConstr GT = gtConstr
344 gunfold _ z c = case constrIndex c of
349 dataTypeOf _ = orderingDataType
352 ------------------------------------------------------------------------------
355 leftConstr = mkConstr eitherDataType "Left" [] Prefix
357 rightConstr :: Constr
358 rightConstr = mkConstr eitherDataType "Right" [] Prefix
360 eitherDataType :: DataType
361 eitherDataType = mkDataType "Prelude.Either" [leftConstr,rightConstr]
363 instance (Data a, Data b) => Data (Either a b) where
364 gfoldl f z (Left a) = z Left `f` a
365 gfoldl f z (Right a) = z Right `f` a
366 toConstr (Left _) = leftConstr
367 toConstr (Right _) = rightConstr
368 gunfold k z c = case constrIndex c of
372 dataTypeOf _ = eitherDataType
373 dataCast2 f = gcast2 f
376 ------------------------------------------------------------------------------
380 -- A last resort for functions
383 instance (Data a, Data b) => Data (a -> b) where
384 toConstr _ = error "toConstr"
385 gunfold _ _ = error "gunfold"
386 dataTypeOf _ = mkNorepType "Prelude.(->)"
387 dataCast2 f = gcast2 f
390 ------------------------------------------------------------------------------
392 tuple0Constr :: Constr
393 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
395 tuple0DataType :: DataType
396 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
398 instance Data () where
399 toConstr () = tuple0Constr
400 gunfold _ z c | constrIndex c == 1 = z ()
401 gunfold _ _ _ = error "gunfold"
402 dataTypeOf _ = tuple0DataType
405 ------------------------------------------------------------------------------
407 tuple2Constr :: Constr
408 tuple2Constr = mkConstr tuple2DataType "(,)" [] Infix
410 tuple2DataType :: DataType
411 tuple2DataType = mkDataType "Prelude.(,)" [tuple2Constr]
413 instance (Data a, Data b) => Data (a,b) where
414 gfoldl f z (a,b) = z (,) `f` a `f` b
415 toConstr (_,_) = tuple2Constr
416 gunfold k z c | constrIndex c == 1 = k (k (z (,)))
417 gunfold _ _ _ = error "gunfold"
418 dataTypeOf _ = tuple2DataType
419 dataCast2 f = gcast2 f
422 ------------------------------------------------------------------------------
424 tuple3Constr :: Constr
425 tuple3Constr = mkConstr tuple3DataType "(,,)" [] Infix
427 tuple3DataType :: DataType
428 tuple3DataType = mkDataType "Prelude.(,)" [tuple3Constr]
430 instance (Data a, Data b, Data c) => Data (a,b,c) where
431 gfoldl f z (a,b,c) = z (,,) `f` a `f` b `f` c
432 toConstr (_,_,_) = tuple3Constr
433 gunfold k z c | constrIndex c == 1 = k (k (k (z (,,))))
434 gunfold _ _ _ = error "gunfold"
435 dataTypeOf _ = tuple3DataType
438 ------------------------------------------------------------------------------
440 tuple4Constr :: Constr
441 tuple4Constr = mkConstr tuple4DataType "(,,,)" [] Infix
443 tuple4DataType :: DataType
444 tuple4DataType = mkDataType "Prelude.(,,,)" [tuple4Constr]
446 instance (Data a, Data b, Data c, Data d)
447 => Data (a,b,c,d) where
448 gfoldl f z (a,b,c,d) = z (,,,) `f` a `f` b `f` c `f` d
449 toConstr (_,_,_,_) = tuple4Constr
450 gunfold k z c = case constrIndex c of
451 1 -> k (k (k (k (z (,,,)))))
453 dataTypeOf _ = tuple4DataType
456 ------------------------------------------------------------------------------
458 tuple5Constr :: Constr
459 tuple5Constr = mkConstr tuple5DataType "(,,,,)" [] Infix
461 tuple5DataType :: DataType
462 tuple5DataType = mkDataType "Prelude.(,,,,)" [tuple5Constr]
464 instance (Data a, Data b, Data c, Data d, Data e)
465 => Data (a,b,c,d,e) where
466 gfoldl f z (a,b,c,d,e) = z (,,,,) `f` a `f` b `f` c `f` d `f` e
467 toConstr (_,_,_,_,_) = tuple5Constr
468 gunfold k z c = case constrIndex c of
469 1 -> k (k (k (k (k (z (,,,,))))))
471 dataTypeOf _ = tuple5DataType
474 ------------------------------------------------------------------------------
476 tuple6Constr :: Constr
477 tuple6Constr = mkConstr tuple6DataType "(,,,,,)" [] Infix
479 tuple6DataType :: DataType
480 tuple6DataType = mkDataType "Prelude.(,,,,,)" [tuple6Constr]
482 instance (Data a, Data b, Data c, Data d, Data e, Data f)
483 => Data (a,b,c,d,e,f) where
484 gfoldl f z (a,b,c,d,e,f') = z (,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f'
485 toConstr (_,_,_,_,_,_) = tuple6Constr
486 gunfold k z c = case constrIndex c of
487 1 -> k (k (k (k (k (k (z (,,,,,)))))))
489 dataTypeOf _ = tuple6DataType
492 ------------------------------------------------------------------------------
494 tuple7Constr :: Constr
495 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
497 tuple7DataType :: DataType
498 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
500 instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
501 => Data (a,b,c,d,e,f,g) where
502 gfoldl f z (a,b,c,d,e,f',g) =
503 z (,,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f' `f` g
504 toConstr (_,_,_,_,_,_,_) = tuple7Constr
505 gunfold k z c = case constrIndex c of
506 1 -> k (k (k (k (k (k (k (z (,,,,,,))))))))
508 dataTypeOf _ = tuple7DataType
511 ------------------------------------------------------------------------------
514 instance Data TypeRep where
515 toConstr _ = error "toConstr"
516 gunfold _ _ = error "gunfold"
517 dataTypeOf _ = mkNorepType "Data.Typeable.TypeRep"
520 ------------------------------------------------------------------------------
523 instance Data TyCon where
524 toConstr _ = error "toConstr"
525 gunfold _ _ = error "gunfold"
526 dataTypeOf _ = mkNorepType "Data.Typeable.TyCon"
529 ------------------------------------------------------------------------------
532 INSTANCE_TYPEABLE0(DataType,dataTypeTc,"DataType")
534 instance Data DataType where
535 toConstr _ = error "toConstr"
536 gunfold _ _ = error "gunfold"
537 dataTypeOf _ = mkNorepType "Data.Generics.Basics.DataType"
540 ------------------------------------------------------------------------------
543 instance Typeable a => Data (IO a) where
544 toConstr _ = error "toConstr"
545 gunfold _ _ = error "gunfold"
546 dataTypeOf _ = mkNorepType "GHC.IOBase.IO"
549 ------------------------------------------------------------------------------
552 instance Data Handle where
553 toConstr _ = error "toConstr"
554 gunfold _ _ = error "gunfold"
555 dataTypeOf _ = mkNorepType "GHC.IOBase.Handle"
558 ------------------------------------------------------------------------------
561 instance Typeable a => Data (Ptr a) where
562 toConstr _ = error "toConstr"
563 gunfold _ _ = error "gunfold"
564 dataTypeOf _ = mkNorepType "GHC.Ptr.Ptr"
567 ------------------------------------------------------------------------------
570 instance Typeable a => Data (StablePtr a) where
571 toConstr _ = error "toConstr"
572 gunfold _ _ = error "gunfold"
573 dataTypeOf _ = mkNorepType "GHC.Stable.StablePtr"
576 ------------------------------------------------------------------------------
579 instance Typeable a => Data (IORef a) where
580 toConstr _ = error "toConstr"
581 gunfold _ _ = error "gunfold"
582 dataTypeOf _ = mkNorepType "GHC.IOBase.IORef"
585 ------------------------------------------------------------------------------
588 instance Typeable a => Data (ForeignPtr a) where
589 toConstr _ = error "toConstr"
590 gunfold _ _ = error "gunfold"
591 dataTypeOf _ = mkNorepType "GHC.ForeignPtr.ForeignPtr"
594 ------------------------------------------------------------------------------
597 instance (Typeable s, Typeable a) => Data (ST s a) where
598 toConstr _ = error "toConstr"
599 gunfold _ _ = error "gunfold"
600 dataTypeOf _ = mkNorepType "GHC.ST.ST"
603 ------------------------------------------------------------------------------
606 instance Data ThreadId where
607 toConstr _ = error "toConstr"
608 gunfold _ _ = error "gunfold"
609 dataTypeOf _ = mkNorepType "GHC.Conc.ThreadId"
612 ------------------------------------------------------------------------------
615 instance Typeable a => Data (TVar a) where
616 toConstr _ = error "toConstr"
617 gunfold _ _ = error "gunfold"
618 dataTypeOf _ = mkNorepType "GHC.Conc.TVar"
621 ------------------------------------------------------------------------------
624 instance Typeable a => Data (MVar a) where
625 toConstr _ = error "toConstr"
626 gunfold _ _ = error "gunfold"
627 dataTypeOf _ = mkNorepType "GHC.Conc.MVar"
630 ------------------------------------------------------------------------------
633 instance Typeable a => Data (STM a) where
634 toConstr _ = error "toConstr"
635 gunfold _ _ = error "gunfold"
636 dataTypeOf _ = mkNorepType "GHC.Conc.STM"
639 ------------------------------------------------------------------------------
640 -- The Data instance for Array preserves data abstraction at the cost of inefficiency.
641 -- We omit reflection services for the sake of data abstraction.
642 instance (Typeable a, Data b, Ix a) => Data (Array a b)
644 gfoldl f z a = z (listArray (bounds a)) `f` (elems a)
645 toConstr _ = error "toConstr"
646 gunfold _ _ = error "gunfold"
647 dataTypeOf _ = mkNorepType "Data.Array.Array"