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 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.
41 import GHC.Arr -- So we can give Data instance for Array
47 ------------------------------------------------------------------------------
49 -- Instances of the Data class for Prelude-like types.
50 -- We define top-level definitions for representations.
52 ------------------------------------------------------------------------------
55 falseConstr = mkConstr boolDataType "False" [] Prefix
56 trueConstr = mkConstr boolDataType "True" [] Prefix
57 boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
60 instance Data Bool where
61 toConstr False = falseConstr
62 toConstr True = trueConstr
63 gunfold k z c = case constrIndex c of
67 dataTypeOf _ = boolDataType
70 ------------------------------------------------------------------------------
73 charType = mkStringType "Prelude.Char"
75 instance Data Char where
76 toConstr x = mkStringConstr charType [x]
77 gunfold k z c = case constrRep c of
78 (StringConstr [x]) -> z x
80 dataTypeOf _ = charType
83 ------------------------------------------------------------------------------
86 floatType = mkFloatType "Prelude.Float"
88 instance Data Float where
89 toConstr x = mkFloatConstr floatType (realToFrac x)
90 gunfold k z c = case constrRep c of
91 (FloatConstr x) -> z (realToFrac x)
93 dataTypeOf _ = floatType
96 ------------------------------------------------------------------------------
99 doubleType = mkFloatType "Prelude.Double"
101 instance Data Double where
102 toConstr = mkFloatConstr floatType
103 gunfold k z c = case constrRep c of
104 (FloatConstr x) -> z x
106 dataTypeOf _ = doubleType
109 ------------------------------------------------------------------------------
112 intType = mkIntType "Prelude.Int"
114 instance Data Int where
115 toConstr x = mkIntConstr intType (fromIntegral x)
116 gunfold k z c = case constrRep c of
117 (IntConstr x) -> z (fromIntegral x)
119 dataTypeOf _ = intType
122 ------------------------------------------------------------------------------
125 integerType = mkIntType "Prelude.Integer"
127 instance Data Integer where
128 toConstr = mkIntConstr integerType
129 gunfold k z c = case constrRep c of
132 dataTypeOf _ = integerType
135 ------------------------------------------------------------------------------
138 int8Type = mkIntType "Data.Int.Int8"
140 instance Data Int8 where
141 toConstr x = mkIntConstr int8Type (fromIntegral x)
142 gunfold k z c = case constrRep c of
143 (IntConstr x) -> z (fromIntegral x)
145 dataTypeOf _ = int8Type
148 ------------------------------------------------------------------------------
151 int16Type = mkIntType "Data.Int.Int16"
153 instance Data Int16 where
154 toConstr x = mkIntConstr int16Type (fromIntegral x)
155 gunfold k z c = case constrRep c of
156 (IntConstr x) -> z (fromIntegral x)
158 dataTypeOf _ = int16Type
161 ------------------------------------------------------------------------------
164 int32Type = mkIntType "Data.Int.Int32"
166 instance Data Int32 where
167 toConstr x = mkIntConstr int32Type (fromIntegral x)
168 gunfold k z c = case constrRep c of
169 (IntConstr x) -> z (fromIntegral x)
171 dataTypeOf _ = int32Type
174 ------------------------------------------------------------------------------
177 int64Type = mkIntType "Data.Int.Int64"
179 instance Data Int64 where
180 toConstr x = mkIntConstr int64Type (fromIntegral x)
181 gunfold k z c = case constrRep c of
182 (IntConstr x) -> z (fromIntegral x)
184 dataTypeOf _ = int64Type
187 ------------------------------------------------------------------------------
190 wordType = mkIntType "Data.Word.Word"
192 instance Data Word where
193 toConstr x = mkIntConstr wordType (fromIntegral x)
194 gunfold k z c = case constrRep c of
195 (IntConstr x) -> z (fromIntegral x)
197 dataTypeOf _ = wordType
200 ------------------------------------------------------------------------------
203 word8Type = mkIntType "Data.Word.Word8"
205 instance Data Word8 where
206 toConstr x = mkIntConstr word8Type (fromIntegral x)
207 gunfold k z c = case constrRep c of
208 (IntConstr x) -> z (fromIntegral x)
210 dataTypeOf _ = word8Type
213 ------------------------------------------------------------------------------
216 word16Type = mkIntType "Data.Word.Word16"
218 instance Data Word16 where
219 toConstr x = mkIntConstr word16Type (fromIntegral x)
220 gunfold k z c = case constrRep c of
221 (IntConstr x) -> z (fromIntegral x)
223 dataTypeOf _ = word16Type
226 ------------------------------------------------------------------------------
229 word32Type = mkIntType "Data.Word.Word32"
231 instance Data Word32 where
232 toConstr x = mkIntConstr word32Type (fromIntegral x)
233 gunfold k z c = case constrRep c of
234 (IntConstr x) -> z (fromIntegral x)
236 dataTypeOf _ = word32Type
239 ------------------------------------------------------------------------------
242 word64Type = mkIntType "Data.Word.Word64"
244 instance Data Word64 where
245 toConstr x = mkIntConstr word64Type (fromIntegral x)
246 gunfold k z c = case constrRep c of
247 (IntConstr x) -> z (fromIntegral x)
249 dataTypeOf _ = word64Type
252 ------------------------------------------------------------------------------
255 ratioConstr = mkConstr ratioDataType ":%" [] Infix
256 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
258 instance (Data a, Integral a) => Data (Ratio a) where
259 toConstr _ = ratioConstr
260 gunfold k z c | constrIndex c == 1 = k (k (z (:%)))
261 gunfold _ _ _ = error "gunfold"
262 dataTypeOf _ = ratioDataType
265 ------------------------------------------------------------------------------
268 nilConstr = mkConstr listDataType "[]" [] Prefix
269 consConstr = mkConstr listDataType "(:)" [] Infix
270 listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
272 instance Data a => Data [a] where
274 gfoldl f z (x:xs) = z (:) `f` x `f` xs
275 toConstr [] = nilConstr
276 toConstr (_:_) = consConstr
277 gunfold k z c = case constrIndex c of
281 dataTypeOf _ = listDataType
282 dataCast1 f = gcast1 f
285 -- The gmaps are given as an illustration.
286 -- This shows that the gmaps for lists are different from list maps.
289 gmapT f (x:xs) = (f x:f xs)
291 gmapQ f (x:xs) = [f x,f xs]
292 gmapM f [] = return []
293 gmapM f (x:xs) = f x >>= \x' -> f xs >>= \xs' -> return (x':xs')
296 ------------------------------------------------------------------------------
299 nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix
300 justConstr = mkConstr maybeDataType "Just" [] Prefix
301 maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]
303 instance Data a => Data (Maybe a) where
304 gfoldl f z Nothing = z Nothing
305 gfoldl f z (Just x) = z Just `f` x
306 toConstr Nothing = nothingConstr
307 toConstr (Just _) = justConstr
308 gunfold k z c = case constrIndex c of
312 dataTypeOf _ = maybeDataType
313 dataCast1 f = gcast1 f
316 ------------------------------------------------------------------------------
319 ltConstr = mkConstr orderingDataType "LT" [] Prefix
320 eqConstr = mkConstr orderingDataType "EQ" [] Prefix
321 gtConstr = mkConstr orderingDataType "GT" [] Prefix
322 orderingDataType = mkDataType "Prelude.Ordering" [ltConstr,eqConstr,gtConstr]
324 instance Data Ordering where
328 toConstr LT = ltConstr
329 toConstr EQ = eqConstr
330 toConstr GT = gtConstr
331 gunfold k z c = case constrIndex c of
336 dataTypeOf _ = orderingDataType
339 ------------------------------------------------------------------------------
342 leftConstr = mkConstr eitherDataType "Left" [] Prefix
343 rightConstr = mkConstr eitherDataType "Right" [] Prefix
344 eitherDataType = mkDataType "Prelude.Either" [leftConstr,rightConstr]
346 instance (Data a, Data b) => Data (Either a b) where
347 gfoldl f z (Left a) = z Left `f` a
348 gfoldl f z (Right a) = z Right `f` a
349 toConstr (Left _) = leftConstr
350 toConstr (Right _) = rightConstr
351 gunfold k z c = case constrIndex c of
355 dataTypeOf _ = eitherDataType
356 dataCast2 f = gcast2 f
359 ------------------------------------------------------------------------------
363 -- A last resort for functions
366 instance (Data a, Data b) => Data (a -> b) where
367 toConstr _ = error "toConstr"
368 gunfold _ _ = error "gunfold"
369 dataTypeOf _ = mkNorepType "Prelude.(->)"
370 dataCast2 f = gcast2 f
373 ------------------------------------------------------------------------------
376 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
377 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
379 instance Data () where
380 toConstr () = tuple0Constr
381 gunfold k z c | constrIndex c == 1 = z ()
382 gunfold _ _ _ = error "gunfold"
383 dataTypeOf _ = tuple0DataType
386 ------------------------------------------------------------------------------
389 tuple2Constr = mkConstr tuple2DataType "(,)" [] Infix
390 tuple2DataType = mkDataType "Prelude.(,)" [tuple2Constr]
392 instance (Data a, Data b) => Data (a,b) where
393 gfoldl f z (a,b) = z (,) `f` a `f` b
394 toConstr (a,b) = tuple2Constr
395 gunfold k z c | constrIndex c == 1 = k (k (z (,)))
396 gunfold _ _ _ = error "gunfold"
397 dataTypeOf _ = tuple2DataType
398 dataCast2 f = gcast2 f
401 ------------------------------------------------------------------------------
404 tuple3Constr = mkConstr tuple3DataType "(,,)" [] Infix
405 tuple3DataType = mkDataType "Prelude.(,)" [tuple3Constr]
407 instance (Data a, Data b, Data c) => Data (a,b,c) where
408 gfoldl f z (a,b,c) = z (,,) `f` a `f` b `f` c
409 toConstr (a,b,c) = tuple3Constr
410 gunfold k z c | constrIndex c == 1 = k (k (k (z (,,))))
411 gunfold _ _ _ = error "gunfold"
412 dataTypeOf _ = tuple3DataType
415 ------------------------------------------------------------------------------
418 tuple4Constr = mkConstr tuple4DataType "(,,,)" [] Infix
419 tuple4DataType = mkDataType "Prelude.(,,,)" [tuple4Constr]
421 instance (Data a, Data b, Data c, Data d)
422 => Data (a,b,c,d) where
423 gfoldl f z (a,b,c,d) = z (,,,) `f` a `f` b `f` c `f` d
424 toConstr (a,b,c,d) = tuple4Constr
425 gunfold k z c = case constrIndex c of
426 1 -> k (k (k (k (z (,,,)))))
428 dataTypeOf _ = tuple4DataType
431 ------------------------------------------------------------------------------
434 tuple5Constr = mkConstr tuple5DataType "(,,,,)" [] Infix
435 tuple5DataType = mkDataType "Prelude.(,,,,)" [tuple5Constr]
437 instance (Data a, Data b, Data c, Data d, Data e)
438 => Data (a,b,c,d,e) where
439 gfoldl f z (a,b,c,d,e) = z (,,,,) `f` a `f` b `f` c `f` d `f` e
440 toConstr (a,b,c,d,e) = tuple5Constr
441 gunfold k z c = case constrIndex c of
442 1 -> k (k (k (k (k (z (,,,,))))))
444 dataTypeOf _ = tuple5DataType
447 ------------------------------------------------------------------------------
450 tuple6Constr = mkConstr tuple6DataType "(,,,,,)" [] Infix
451 tuple6DataType = mkDataType "Prelude.(,,,,,)" [tuple6Constr]
453 instance (Data a, Data b, Data c, Data d, Data e, Data f)
454 => Data (a,b,c,d,e,f) where
455 gfoldl f z (a,b,c,d,e,f') = z (,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f'
456 toConstr (a,b,c,d,e,f) = tuple6Constr
457 gunfold k z c = case constrIndex c of
458 1 -> k (k (k (k (k (k (z (,,,,,)))))))
460 dataTypeOf _ = tuple6DataType
463 ------------------------------------------------------------------------------
466 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
467 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
469 instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
470 => Data (a,b,c,d,e,f,g) where
471 gfoldl f z (a,b,c,d,e,f',g) =
472 z (,,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f' `f` g
473 toConstr (a,b,c,d,e,f,g) = tuple7Constr
474 gunfold k z c = case constrIndex c of
475 1 -> k (k (k (k (k (k (k (z (,,,,,,))))))))
477 dataTypeOf _ = tuple7DataType
480 ------------------------------------------------------------------------------
483 instance Data TypeRep where
484 toConstr _ = error "toConstr"
485 gunfold _ _ = error "gunfold"
486 dataTypeOf _ = mkNorepType "Data.Typeable.TypeRep"
489 ------------------------------------------------------------------------------
492 instance Data TyCon where
493 toConstr _ = error "toConstr"
494 gunfold _ _ = error "gunfold"
495 dataTypeOf _ = mkNorepType "Data.Typeable.TyCon"
498 ------------------------------------------------------------------------------
501 INSTANCE_TYPEABLE0(DataType,dataTypeTc,"DataType")
503 instance Data DataType where
504 toConstr _ = error "toConstr"
505 gunfold _ _ = error "gunfold"
506 dataTypeOf _ = mkNorepType "Data.Generics.Basics.DataType"
509 ------------------------------------------------------------------------------
512 instance Typeable a => Data (IO a) where
513 toConstr _ = error "toConstr"
514 gunfold _ _ = error "gunfold"
515 dataTypeOf _ = mkNorepType "GHC.IOBase.IO"
518 ------------------------------------------------------------------------------
521 instance Data Handle where
522 toConstr _ = error "toConstr"
523 gunfold _ _ = error "gunfold"
524 dataTypeOf _ = mkNorepType "GHC.IOBase.Handle"
527 ------------------------------------------------------------------------------
530 instance Typeable a => Data (Ptr a) where
531 toConstr _ = error "toConstr"
532 gunfold _ _ = error "gunfold"
533 dataTypeOf _ = mkNorepType "GHC.Ptr.Ptr"
536 ------------------------------------------------------------------------------
539 instance Typeable a => Data (StablePtr a) where
540 toConstr _ = error "toConstr"
541 gunfold _ _ = error "gunfold"
542 dataTypeOf _ = mkNorepType "GHC.Stable.StablePtr"
545 ------------------------------------------------------------------------------
548 instance Typeable a => Data (IORef a) where
549 toConstr _ = error "toConstr"
550 gunfold _ _ = error "gunfold"
551 dataTypeOf _ = mkNorepType "GHC.IOBase.IORef"
554 ------------------------------------------------------------------------------
557 instance Typeable a => Data (ForeignPtr a) where
558 toConstr _ = error "toConstr"
559 gunfold _ _ = error "gunfold"
560 dataTypeOf _ = mkNorepType "GHC.ForeignPtr.ForeignPtr"
563 ------------------------------------------------------------------------------
566 instance (Typeable s, Typeable a) => Data (ST s a) where
567 toConstr _ = error "toConstr"
568 gunfold _ _ = error "gunfold"
569 dataTypeOf _ = mkNorepType "GHC.ST.ST"
572 ------------------------------------------------------------------------------
575 instance Data ThreadId where
576 toConstr _ = error "toConstr"
577 gunfold _ _ = error "gunfold"
578 dataTypeOf _ = mkNorepType "GHC.Conc.ThreadId"
581 ------------------------------------------------------------------------------
584 instance Typeable a => Data (TVar a) where
585 toConstr _ = error "toConstr"
586 gunfold _ _ = error "gunfold"
587 dataTypeOf _ = mkNorepType "GHC.Conc.TVar"
590 ------------------------------------------------------------------------------
593 instance Typeable a => Data (MVar a) where
594 toConstr _ = error "toConstr"
595 gunfold _ _ = error "gunfold"
596 dataTypeOf _ = mkNorepType "GHC.Conc.MVar"
599 ------------------------------------------------------------------------------
602 instance Typeable a => Data (STM a) where
603 toConstr _ = error "toConstr"
604 gunfold _ _ = error "gunfold"
605 dataTypeOf _ = mkNorepType "GHC.Conc.STM"
608 ------------------------------------------------------------------------------
609 -- The Data instance for Array preserves data abstraction at the cost of inefficiency.
610 -- We omit reflection services for the sake of data abstraction.
611 instance (Typeable a, Data b, Ix a) => Data (Array a b)
613 gfoldl f z a = z (listArray (bounds a)) `f` (elems a)
614 toConstr _ = error "toConstr"
615 gunfold _ _ = error "gunfold"
616 dataTypeOf _ = mkNorepType "Data.Array.Array"