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.Stable -- So we can give Data instance for StablePtr
43 ------------------------------------------------------------------------------
45 -- Instances of the Data class for Prelude-like types.
46 -- We define top-level definitions for representations.
48 ------------------------------------------------------------------------------
51 falseConstr = mkConstr boolDataType "False" [] Prefix
52 trueConstr = mkConstr boolDataType "True" [] Prefix
53 boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
56 instance Data Bool where
57 toConstr False = falseConstr
58 toConstr True = trueConstr
59 gunfold k z c = case constrIndex c of
63 dataTypeOf _ = boolDataType
66 ------------------------------------------------------------------------------
69 charType = mkStringType "Prelude.Char"
71 instance Data Char where
72 toConstr x = mkStringConstr charType [x]
73 gunfold k z c = case constrRep c of
74 (StringConstr [x]) -> z x
76 dataTypeOf _ = charType
79 ------------------------------------------------------------------------------
82 floatType = mkFloatType "Prelude.Float"
84 instance Data Float where
85 toConstr x = mkFloatConstr floatType (realToFrac x)
86 gunfold k z c = case constrRep c of
87 (FloatConstr x) -> z (realToFrac x)
89 dataTypeOf _ = floatType
92 ------------------------------------------------------------------------------
95 doubleType = mkFloatType "Prelude.Double"
97 instance Data Double where
98 toConstr = mkFloatConstr floatType
99 gunfold k z c = case constrRep c of
100 (FloatConstr x) -> z x
102 dataTypeOf _ = doubleType
105 ------------------------------------------------------------------------------
108 intType = mkIntType "Prelude.Int"
110 instance Data Int where
111 toConstr x = mkIntConstr intType (fromIntegral x)
112 gunfold k z c = case constrRep c of
113 (IntConstr x) -> z (fromIntegral x)
115 dataTypeOf _ = intType
118 ------------------------------------------------------------------------------
121 integerType = mkIntType "Prelude.Integer"
123 instance Data Integer where
124 toConstr = mkIntConstr integerType
125 gunfold k z c = case constrRep c of
128 dataTypeOf _ = integerType
131 ------------------------------------------------------------------------------
134 int8Type = mkIntType "Data.Int.Int8"
136 instance Data Int8 where
137 toConstr x = mkIntConstr int8Type (fromIntegral x)
138 gunfold k z c = case constrRep c of
139 (IntConstr x) -> z (fromIntegral x)
141 dataTypeOf _ = int8Type
144 ------------------------------------------------------------------------------
147 int16Type = mkIntType "Data.Int.Int16"
149 instance Data Int16 where
150 toConstr x = mkIntConstr int16Type (fromIntegral x)
151 gunfold k z c = case constrRep c of
152 (IntConstr x) -> z (fromIntegral x)
154 dataTypeOf _ = int16Type
157 ------------------------------------------------------------------------------
160 int32Type = mkIntType "Data.Int.Int32"
162 instance Data Int32 where
163 toConstr x = mkIntConstr int32Type (fromIntegral x)
164 gunfold k z c = case constrRep c of
165 (IntConstr x) -> z (fromIntegral x)
167 dataTypeOf _ = int32Type
170 ------------------------------------------------------------------------------
173 int64Type = mkIntType "Data.Int.Int64"
175 instance Data Int64 where
176 toConstr x = mkIntConstr int64Type (fromIntegral x)
177 gunfold k z c = case constrRep c of
178 (IntConstr x) -> z (fromIntegral x)
180 dataTypeOf _ = int64Type
183 ------------------------------------------------------------------------------
186 wordType = mkIntType "Data.Word.Word"
188 instance Data Word where
189 toConstr x = mkIntConstr wordType (fromIntegral x)
190 gunfold k z c = case constrRep c of
191 (IntConstr x) -> z (fromIntegral x)
193 dataTypeOf _ = wordType
196 ------------------------------------------------------------------------------
199 word8Type = mkIntType "Data.Word.Word8"
201 instance Data Word8 where
202 toConstr x = mkIntConstr word8Type (fromIntegral x)
203 gunfold k z c = case constrRep c of
204 (IntConstr x) -> z (fromIntegral x)
206 dataTypeOf _ = word8Type
209 ------------------------------------------------------------------------------
212 word16Type = mkIntType "Data.Word.Word16"
214 instance Data Word16 where
215 toConstr x = mkIntConstr word16Type (fromIntegral x)
216 gunfold k z c = case constrRep c of
217 (IntConstr x) -> z (fromIntegral x)
219 dataTypeOf _ = word16Type
222 ------------------------------------------------------------------------------
225 word32Type = mkIntType "Data.Word.Word32"
227 instance Data Word32 where
228 toConstr x = mkIntConstr word32Type (fromIntegral x)
229 gunfold k z c = case constrRep c of
230 (IntConstr x) -> z (fromIntegral x)
232 dataTypeOf _ = word32Type
235 ------------------------------------------------------------------------------
238 word64Type = mkIntType "Data.Word.Word64"
240 instance Data Word64 where
241 toConstr x = mkIntConstr word64Type (fromIntegral x)
242 gunfold k z c = case constrRep c of
243 (IntConstr x) -> z (fromIntegral x)
245 dataTypeOf _ = word64Type
248 ------------------------------------------------------------------------------
251 ratioConstr = mkConstr ratioDataType ":%" [] Infix
252 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
254 instance (Data a, Integral a) => Data (Ratio a) where
255 toConstr _ = ratioConstr
256 gunfold k z c | constrIndex c == 1 = k (k (z (:%)))
257 gunfold _ _ _ = error "gunfold"
258 dataTypeOf _ = ratioDataType
261 ------------------------------------------------------------------------------
264 nilConstr = mkConstr listDataType "[]" [] Prefix
265 consConstr = mkConstr listDataType "(:)" [] Infix
266 listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
268 instance Data a => Data [a] where
270 gfoldl f z (x:xs) = z (:) `f` x `f` xs
271 toConstr [] = nilConstr
272 toConstr (_:_) = consConstr
273 gunfold k z c = case constrIndex c of
277 dataTypeOf _ = listDataType
281 -- The gmaps are given as an illustration.
282 -- This shows that the gmaps for lists are different from list maps.
285 gmapT f (x:xs) = (f x:f xs)
287 gmapQ f (x:xs) = [f x,f xs]
288 gmapM f [] = return []
289 gmapM f (x:xs) = f x >>= \x' -> f xs >>= \xs' -> return (x':xs')
292 ------------------------------------------------------------------------------
295 nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix
296 justConstr = mkConstr maybeDataType "Just" [] Prefix
297 maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]
299 instance Data a => Data (Maybe a) where
300 gfoldl f z Nothing = z Nothing
301 gfoldl f z (Just x) = z Just `f` x
302 toConstr Nothing = nothingConstr
303 toConstr (Just _) = justConstr
304 gunfold k z c = case constrIndex c of
308 dataTypeOf _ = maybeDataType
312 ------------------------------------------------------------------------------
315 ltConstr = mkConstr orderingDataType "LT" [] Prefix
316 eqConstr = mkConstr orderingDataType "EQ" [] Prefix
317 gtConstr = mkConstr orderingDataType "GT" [] Prefix
318 orderingDataType = mkDataType "Prelude.Ordering" [ltConstr,eqConstr,gtConstr]
320 instance Data Ordering where
324 toConstr LT = ltConstr
325 toConstr EQ = eqConstr
326 toConstr GT = gtConstr
327 gunfold k z c = case constrIndex c of
332 dataTypeOf _ = orderingDataType
335 ------------------------------------------------------------------------------
338 leftConstr = mkConstr eitherDataType "Left" [] Prefix
339 rightConstr = mkConstr eitherDataType "Right" [] Prefix
340 eitherDataType = mkDataType "Prelude.Either" [leftConstr,rightConstr]
342 instance (Data a, Data b) => Data (Either a b) where
343 gfoldl f z (Left a) = z Left `f` a
344 gfoldl f z (Right a) = z Right `f` a
345 toConstr (Left _) = leftConstr
346 toConstr (Right _) = rightConstr
347 gunfold k z c = case constrIndex c of
351 dataTypeOf _ = eitherDataType
355 ------------------------------------------------------------------------------
359 -- A last resort for functions
362 instance (Data a, Data b) => Data (a -> b) where
363 toConstr _ = error "toConstr"
364 gunfold _ _ = error "gunfold"
365 dataTypeOf _ = mkNorepType "Prelude.(->)"
369 ------------------------------------------------------------------------------
372 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
373 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
375 instance Data () where
376 toConstr _ = tuple0Constr
377 gunfold k z c | constrIndex c == 1 = z ()
378 gunfold _ _ _ = error "gunfold"
379 dataTypeOf _ = tuple0DataType
382 ------------------------------------------------------------------------------
385 tuple2Constr = mkConstr tuple2DataType "(,)" [] Infix
386 tuple2DataType = mkDataType "Prelude.(,)" [tuple2Constr]
388 instance (Data a, Data b) => Data (a,b) where
389 gfoldl f z (a,b) = z (,) `f` a `f` b
390 toConstr _ = tuple2Constr
391 gunfold k z c | constrIndex c == 1 = k (k (z (,)))
392 gunfold _ _ _ = error "gunfold"
393 dataTypeOf _ = tuple2DataType
397 ------------------------------------------------------------------------------
400 tuple3Constr = mkConstr tuple3DataType "(,,)" [] Infix
401 tuple3DataType = mkDataType "Prelude.(,)" [tuple3Constr]
403 instance (Data a, Data b, Data c) => Data (a,b,c) where
404 gfoldl f z (a,b,c) = z (,,) `f` a `f` b `f` c
405 toConstr _ = tuple3Constr
406 gunfold k z c | constrIndex c == 1 = k (k (k (z (,,))))
407 gunfold _ _ _ = error "gunfold"
408 dataTypeOf _ = tuple3DataType
411 ------------------------------------------------------------------------------
414 tuple4Constr = mkConstr tuple4DataType "(,,,)" [] Infix
415 tuple4DataType = mkDataType "Prelude.(,,,)" [tuple4Constr]
417 instance (Data a, Data b, Data c, Data d)
418 => Data (a,b,c,d) where
419 gfoldl f z (a,b,c,d) = z (,,,) `f` a `f` b `f` c `f` d
420 toConstr _ = tuple4Constr
421 gunfold k z c = case constrIndex c of
422 1 -> k (k (k (k (z (,,,)))))
424 dataTypeOf _ = tuple4DataType
427 ------------------------------------------------------------------------------
430 tuple5Constr = mkConstr tuple5DataType "(,,,,)" [] Infix
431 tuple5DataType = mkDataType "Prelude.(,,,,)" [tuple5Constr]
433 instance (Data a, Data b, Data c, Data d, Data e)
434 => Data (a,b,c,d,e) where
435 gfoldl f z (a,b,c,d,e) = z (,,,,) `f` a `f` b `f` c `f` d `f` e
436 toConstr _ = tuple5Constr
437 gunfold k z c = case constrIndex c of
438 1 -> k (k (k (k (k (z (,,,,))))))
440 dataTypeOf _ = tuple5DataType
443 ------------------------------------------------------------------------------
446 tuple6Constr = mkConstr tuple6DataType "(,,,,,)" [] Infix
447 tuple6DataType = mkDataType "Prelude.(,,,,,)" [tuple6Constr]
449 instance (Data a, Data b, Data c, Data d, Data e, Data f)
450 => Data (a,b,c,d,e,f) where
451 gfoldl f z (a,b,c,d,e,f') = z (,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f'
452 toConstr _ = tuple6Constr
453 gunfold k z c = case constrIndex c of
454 1 -> k (k (k (k (k (k (z (,,,,,)))))))
456 dataTypeOf _ = tuple6DataType
459 ------------------------------------------------------------------------------
462 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
463 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
465 instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
466 => Data (a,b,c,d,e,f,g) where
467 gfoldl f z (a,b,c,d,e,f',g) =
468 z (,,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f' `f` g
469 toConstr _ = tuple7Constr
470 gunfold k z c = case constrIndex c of
471 1 -> k (k (k (k (k (k (k (z (,,,,,,))))))))
473 dataTypeOf _ = tuple7DataType
476 ------------------------------------------------------------------------------
479 instance Data TypeRep where
480 toConstr _ = error "toConstr"
481 gunfold _ _ = error "gunfold"
482 dataTypeOf _ = mkNorepType "Data.Typeable.TypeRep"
485 ------------------------------------------------------------------------------
488 instance Data TyCon where
489 toConstr _ = error "toConstr"
490 gunfold _ _ = error "gunfold"
491 dataTypeOf _ = mkNorepType "Data.Typeable.TyCon"
494 ------------------------------------------------------------------------------
497 INSTANCE_TYPEABLE0(DataType,dataTypeTc,"DataType")
499 instance Data DataType where
500 toConstr _ = error "toConstr"
501 gunfold _ _ = error "gunfold"
502 dataTypeOf _ = mkNorepType "Data.Generics.Basics.DataType"
505 ------------------------------------------------------------------------------
508 instance Typeable a => Data (IO a) where
509 toConstr _ = error "toConstr"
510 gunfold _ _ = error "gunfold"
511 dataTypeOf _ = mkNorepType "GHC.IOBase.IO"
514 ------------------------------------------------------------------------------
517 instance Data Handle where
518 toConstr _ = error "toConstr"
519 gunfold _ _ = error "gunfold"
520 dataTypeOf _ = mkNorepType "GHC.IOBase.Handle"
523 ------------------------------------------------------------------------------
526 instance Typeable a => Data (Ptr a) where
527 toConstr _ = error "toConstr"
528 gunfold _ _ = error "gunfold"
529 dataTypeOf _ = mkNorepType "GHC.Ptr.Ptr"
532 ------------------------------------------------------------------------------
535 instance Typeable a => Data (StablePtr a) where
536 toConstr _ = error "toConstr"
537 gunfold _ _ = error "gunfold"
538 dataTypeOf _ = mkNorepType "GHC.Stable.StablePtr"
541 ------------------------------------------------------------------------------
544 instance Typeable a => Data (IORef a) where
545 toConstr _ = error "toConstr"
546 gunfold _ _ = error "gunfold"
547 dataTypeOf _ = mkNorepType "GHC.IOBase.IORef"
550 ------------------------------------------------------------------------------