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
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 module Data.Generics.Instances
23 ------------------------------------------------------------------------------
29 import Data.Generics.Basics
32 import Data.Int -- So we can give Data instance for Int8, ...
33 import Data.Word -- So we can give Data instance for Word8, ...
34 import GHC.Real( Ratio(..) ) -- So we can give Data instance for Ratio
35 import GHC.IOBase -- So we can give Data instance for IO, Handle
36 import GHC.Ptr -- So we can give Data instance for Ptr
37 import GHC.ForeignPtr -- So we can give Data instance for ForeignPtr
38 import GHC.Stable -- So we can give Data instance for StablePtr
39 import GHC.ST -- So we can give Data instance for ST
40 import GHC.Conc -- So we can give Data instance for MVar & Co.
46 ------------------------------------------------------------------------------
48 -- Instances of the Data class for Prelude-like types.
49 -- We define top-level definitions for representations.
51 ------------------------------------------------------------------------------
54 falseConstr = mkConstr boolDataType "False" [] Prefix
55 trueConstr = mkConstr boolDataType "True" [] Prefix
56 boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
59 instance Data Bool where
60 toConstr False = falseConstr
61 toConstr True = trueConstr
62 gunfold k z c = case constrIndex c of
66 dataTypeOf _ = boolDataType
69 ------------------------------------------------------------------------------
72 charType = mkStringType "Prelude.Char"
74 instance Data Char where
75 toConstr x = mkStringConstr charType [x]
76 gunfold k z c = case constrRep c of
77 (StringConstr [x]) -> z x
79 dataTypeOf _ = charType
82 ------------------------------------------------------------------------------
85 floatType = mkFloatType "Prelude.Float"
87 instance Data Float where
88 toConstr x = mkFloatConstr floatType (realToFrac x)
89 gunfold k z c = case constrRep c of
90 (FloatConstr x) -> z (realToFrac x)
92 dataTypeOf _ = floatType
95 ------------------------------------------------------------------------------
98 doubleType = mkFloatType "Prelude.Double"
100 instance Data Double where
101 toConstr = mkFloatConstr floatType
102 gunfold k z c = case constrRep c of
103 (FloatConstr x) -> z x
105 dataTypeOf _ = doubleType
108 ------------------------------------------------------------------------------
111 intType = mkIntType "Prelude.Int"
113 instance Data Int where
114 toConstr x = mkIntConstr intType (fromIntegral x)
115 gunfold k z c = case constrRep c of
116 (IntConstr x) -> z (fromIntegral x)
118 dataTypeOf _ = intType
121 ------------------------------------------------------------------------------
124 integerType = mkIntType "Prelude.Integer"
126 instance Data Integer where
127 toConstr = mkIntConstr integerType
128 gunfold k z c = case constrRep c of
131 dataTypeOf _ = integerType
134 ------------------------------------------------------------------------------
137 int8Type = mkIntType "Data.Int.Int8"
139 instance Data Int8 where
140 toConstr x = mkIntConstr int8Type (fromIntegral x)
141 gunfold k z c = case constrRep c of
142 (IntConstr x) -> z (fromIntegral x)
144 dataTypeOf _ = int8Type
147 ------------------------------------------------------------------------------
150 int16Type = mkIntType "Data.Int.Int16"
152 instance Data Int16 where
153 toConstr x = mkIntConstr int16Type (fromIntegral x)
154 gunfold k z c = case constrRep c of
155 (IntConstr x) -> z (fromIntegral x)
157 dataTypeOf _ = int16Type
160 ------------------------------------------------------------------------------
163 int32Type = mkIntType "Data.Int.Int32"
165 instance Data Int32 where
166 toConstr x = mkIntConstr int32Type (fromIntegral x)
167 gunfold k z c = case constrRep c of
168 (IntConstr x) -> z (fromIntegral x)
170 dataTypeOf _ = int32Type
173 ------------------------------------------------------------------------------
176 int64Type = mkIntType "Data.Int.Int64"
178 instance Data Int64 where
179 toConstr x = mkIntConstr int64Type (fromIntegral x)
180 gunfold k z c = case constrRep c of
181 (IntConstr x) -> z (fromIntegral x)
183 dataTypeOf _ = int64Type
186 ------------------------------------------------------------------------------
189 wordType = mkIntType "Data.Word.Word"
191 instance Data Word where
192 toConstr x = mkIntConstr wordType (fromIntegral x)
193 gunfold k z c = case constrRep c of
194 (IntConstr x) -> z (fromIntegral x)
196 dataTypeOf _ = wordType
199 ------------------------------------------------------------------------------
202 word8Type = mkIntType "Data.Word.Word8"
204 instance Data Word8 where
205 toConstr x = mkIntConstr word8Type (fromIntegral x)
206 gunfold k z c = case constrRep c of
207 (IntConstr x) -> z (fromIntegral x)
209 dataTypeOf _ = word8Type
212 ------------------------------------------------------------------------------
215 word16Type = mkIntType "Data.Word.Word16"
217 instance Data Word16 where
218 toConstr x = mkIntConstr word16Type (fromIntegral x)
219 gunfold k z c = case constrRep c of
220 (IntConstr x) -> z (fromIntegral x)
222 dataTypeOf _ = word16Type
225 ------------------------------------------------------------------------------
228 word32Type = mkIntType "Data.Word.Word32"
230 instance Data Word32 where
231 toConstr x = mkIntConstr word32Type (fromIntegral x)
232 gunfold k z c = case constrRep c of
233 (IntConstr x) -> z (fromIntegral x)
235 dataTypeOf _ = word32Type
238 ------------------------------------------------------------------------------
241 word64Type = mkIntType "Data.Word.Word64"
243 instance Data Word64 where
244 toConstr x = mkIntConstr word64Type (fromIntegral x)
245 gunfold k z c = case constrRep c of
246 (IntConstr x) -> z (fromIntegral x)
248 dataTypeOf _ = word64Type
251 ------------------------------------------------------------------------------
254 ratioConstr = mkConstr ratioDataType ":%" [] Infix
255 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
257 instance (Data a, Integral a) => Data (Ratio a) where
258 toConstr _ = ratioConstr
259 gunfold k z c | constrIndex c == 1 = k (k (z (:%)))
260 gunfold _ _ _ = error "gunfold"
261 dataTypeOf _ = ratioDataType
264 ------------------------------------------------------------------------------
267 nilConstr = mkConstr listDataType "[]" [] Prefix
268 consConstr = mkConstr listDataType "(:)" [] Infix
269 listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
271 instance Data a => Data [a] where
273 gfoldl f z (x:xs) = z (:) `f` x `f` xs
274 toConstr [] = nilConstr
275 toConstr (_:_) = consConstr
276 gunfold k z c = case constrIndex c of
280 dataTypeOf _ = listDataType
284 -- The gmaps are given as an illustration.
285 -- This shows that the gmaps for lists are different from list maps.
288 gmapT f (x:xs) = (f x:f xs)
290 gmapQ f (x:xs) = [f x,f xs]
291 gmapM f [] = return []
292 gmapM f (x:xs) = f x >>= \x' -> f xs >>= \xs' -> return (x':xs')
295 ------------------------------------------------------------------------------
298 nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix
299 justConstr = mkConstr maybeDataType "Just" [] Prefix
300 maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]
302 instance Data a => Data (Maybe a) where
303 gfoldl f z Nothing = z Nothing
304 gfoldl f z (Just x) = z Just `f` x
305 toConstr Nothing = nothingConstr
306 toConstr (Just _) = justConstr
307 gunfold k z c = case constrIndex c of
311 dataTypeOf _ = maybeDataType
315 ------------------------------------------------------------------------------
318 ltConstr = mkConstr orderingDataType "LT" [] Prefix
319 eqConstr = mkConstr orderingDataType "EQ" [] Prefix
320 gtConstr = mkConstr orderingDataType "GT" [] Prefix
321 orderingDataType = mkDataType "Prelude.Ordering" [ltConstr,eqConstr,gtConstr]
323 instance Data Ordering where
327 toConstr LT = ltConstr
328 toConstr EQ = eqConstr
329 toConstr GT = gtConstr
330 gunfold k z c = case constrIndex c of
335 dataTypeOf _ = orderingDataType
338 ------------------------------------------------------------------------------
341 leftConstr = mkConstr eitherDataType "Left" [] Prefix
342 rightConstr = mkConstr eitherDataType "Right" [] Prefix
343 eitherDataType = mkDataType "Prelude.Either" [leftConstr,rightConstr]
345 instance (Data a, Data b) => Data (Either a b) where
346 gfoldl f z (Left a) = z Left `f` a
347 gfoldl f z (Right a) = z Right `f` a
348 toConstr (Left _) = leftConstr
349 toConstr (Right _) = rightConstr
350 gunfold k z c = case constrIndex c of
354 dataTypeOf _ = eitherDataType
358 ------------------------------------------------------------------------------
362 -- A last resort for functions
365 instance (Data a, Data b) => Data (a -> b) where
366 toConstr _ = error "toConstr"
367 gunfold _ _ = error "gunfold"
368 dataTypeOf _ = mkNorepType "Prelude.(->)"
372 ------------------------------------------------------------------------------
375 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
376 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
378 instance Data () where
379 toConstr _ = tuple0Constr
380 gunfold k z c | constrIndex c == 1 = z ()
381 gunfold _ _ _ = error "gunfold"
382 dataTypeOf _ = tuple0DataType
385 ------------------------------------------------------------------------------
388 tuple2Constr = mkConstr tuple2DataType "(,)" [] Infix
389 tuple2DataType = mkDataType "Prelude.(,)" [tuple2Constr]
391 instance (Data a, Data b) => Data (a,b) where
392 gfoldl f z (a,b) = z (,) `f` a `f` b
393 toConstr _ = tuple2Constr
394 gunfold k z c | constrIndex c == 1 = k (k (z (,)))
395 gunfold _ _ _ = error "gunfold"
396 dataTypeOf _ = tuple2DataType
400 ------------------------------------------------------------------------------
403 tuple3Constr = mkConstr tuple3DataType "(,,)" [] Infix
404 tuple3DataType = mkDataType "Prelude.(,)" [tuple3Constr]
406 instance (Data a, Data b, Data c) => Data (a,b,c) where
407 gfoldl f z (a,b,c) = z (,,) `f` a `f` b `f` c
408 toConstr _ = tuple3Constr
409 gunfold k z c | constrIndex c == 1 = k (k (k (z (,,))))
410 gunfold _ _ _ = error "gunfold"
411 dataTypeOf _ = tuple3DataType
414 ------------------------------------------------------------------------------
417 tuple4Constr = mkConstr tuple4DataType "(,,,)" [] Infix
418 tuple4DataType = mkDataType "Prelude.(,,,)" [tuple4Constr]
420 instance (Data a, Data b, Data c, Data d)
421 => Data (a,b,c,d) where
422 gfoldl f z (a,b,c,d) = z (,,,) `f` a `f` b `f` c `f` d
423 toConstr _ = tuple4Constr
424 gunfold k z c = case constrIndex c of
425 1 -> k (k (k (k (z (,,,)))))
427 dataTypeOf _ = tuple4DataType
430 ------------------------------------------------------------------------------
433 tuple5Constr = mkConstr tuple5DataType "(,,,,)" [] Infix
434 tuple5DataType = mkDataType "Prelude.(,,,,)" [tuple5Constr]
436 instance (Data a, Data b, Data c, Data d, Data e)
437 => Data (a,b,c,d,e) where
438 gfoldl f z (a,b,c,d,e) = z (,,,,) `f` a `f` b `f` c `f` d `f` e
439 toConstr _ = tuple5Constr
440 gunfold k z c = case constrIndex c of
441 1 -> k (k (k (k (k (z (,,,,))))))
443 dataTypeOf _ = tuple5DataType
446 ------------------------------------------------------------------------------
449 tuple6Constr = mkConstr tuple6DataType "(,,,,,)" [] Infix
450 tuple6DataType = mkDataType "Prelude.(,,,,,)" [tuple6Constr]
452 instance (Data a, Data b, Data c, Data d, Data e, Data f)
453 => Data (a,b,c,d,e,f) where
454 gfoldl f z (a,b,c,d,e,f') = z (,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f'
455 toConstr _ = tuple6Constr
456 gunfold k z c = case constrIndex c of
457 1 -> k (k (k (k (k (k (z (,,,,,)))))))
459 dataTypeOf _ = tuple6DataType
462 ------------------------------------------------------------------------------
465 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
466 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
468 instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
469 => Data (a,b,c,d,e,f,g) where
470 gfoldl f z (a,b,c,d,e,f',g) =
471 z (,,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f' `f` g
472 toConstr _ = tuple7Constr
473 gunfold k z c = case constrIndex c of
474 1 -> k (k (k (k (k (k (k (z (,,,,,,))))))))
476 dataTypeOf _ = tuple7DataType
479 ------------------------------------------------------------------------------
482 instance Data TypeRep where
483 toConstr _ = error "toConstr"
484 gunfold _ _ = error "gunfold"
485 dataTypeOf _ = mkNorepType "Data.Typeable.TypeRep"
488 ------------------------------------------------------------------------------
491 instance Data TyCon where
492 toConstr _ = error "toConstr"
493 gunfold _ _ = error "gunfold"
494 dataTypeOf _ = mkNorepType "Data.Typeable.TyCon"
497 ------------------------------------------------------------------------------
500 INSTANCE_TYPEABLE0(DataType,dataTypeTc,"DataType")
502 instance Data DataType where
503 toConstr _ = error "toConstr"
504 gunfold _ _ = error "gunfold"
505 dataTypeOf _ = mkNorepType "Data.Generics.Basics.DataType"
508 ------------------------------------------------------------------------------
511 instance Typeable a => Data (IO a) where
512 toConstr _ = error "toConstr"
513 gunfold _ _ = error "gunfold"
514 dataTypeOf _ = mkNorepType "GHC.IOBase.IO"
517 ------------------------------------------------------------------------------
520 instance Data Handle where
521 toConstr _ = error "toConstr"
522 gunfold _ _ = error "gunfold"
523 dataTypeOf _ = mkNorepType "GHC.IOBase.Handle"
526 ------------------------------------------------------------------------------
529 instance Typeable a => Data (Ptr a) where
530 toConstr _ = error "toConstr"
531 gunfold _ _ = error "gunfold"
532 dataTypeOf _ = mkNorepType "GHC.Ptr.Ptr"
535 ------------------------------------------------------------------------------
538 instance Typeable a => Data (StablePtr a) where
539 toConstr _ = error "toConstr"
540 gunfold _ _ = error "gunfold"
541 dataTypeOf _ = mkNorepType "GHC.Stable.StablePtr"
544 ------------------------------------------------------------------------------
547 instance Typeable a => Data (IORef a) where
548 toConstr _ = error "toConstr"
549 gunfold _ _ = error "gunfold"
550 dataTypeOf _ = mkNorepType "GHC.IOBase.IORef"
553 ------------------------------------------------------------------------------
556 instance Typeable a => Data (ForeignPtr a) where
557 toConstr _ = error "toConstr"
558 gunfold _ _ = error "gunfold"
559 dataTypeOf _ = mkNorepType "GHC.ForeignPtr.ForeignPtr"
562 ------------------------------------------------------------------------------
565 instance (Typeable s, Typeable a) => Data (ST s a) where
566 toConstr _ = error "toConstr"
567 gunfold _ _ = error "gunfold"
568 dataTypeOf _ = mkNorepType "GHC.ST.ST"
571 ------------------------------------------------------------------------------
574 instance Data ThreadId where
575 toConstr _ = error "toConstr"
576 gunfold _ _ = error "gunfold"
577 dataTypeOf _ = mkNorepType "GHC.Conc.ThreadId"
580 ------------------------------------------------------------------------------
583 instance Typeable a => Data (TVar a) where
584 toConstr _ = error "toConstr"
585 gunfold _ _ = error "gunfold"
586 dataTypeOf _ = mkNorepType "GHC.Conc.TVar"
589 ------------------------------------------------------------------------------
592 instance Typeable a => Data (MVar a) where
593 toConstr _ = error "toConstr"
594 gunfold _ _ = error "gunfold"
595 dataTypeOf _ = mkNorepType "GHC.Conc.MVar"
598 ------------------------------------------------------------------------------
601 instance Typeable a => Data (STM a) where
602 toConstr _ = error "toConstr"
603 gunfold _ _ = error "gunfold"
604 dataTypeOf _ = mkNorepType "GHC.Conc.STM"
607 ------------------------------------------------------------------------------