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 fromConstr c = case constrIndex c of
62 _ -> error "fromConstr"
63 dataTypeOf _ = boolDataType
66 ------------------------------------------------------------------------------
69 charType = mkStringType "Prelude.Char"
71 instance Data Char where
72 toConstr x = mkStringConstr charType [x]
73 fromConstr con = case constrRep con of
74 (StringConstr [x]) -> x
75 _ -> error "fromConstr"
76 dataTypeOf _ = charType
79 ------------------------------------------------------------------------------
82 floatType = mkFloatType "Prelude.Float"
84 instance Data Float where
85 toConstr x = mkFloatConstr floatType (realToFrac x)
86 fromConstr con = case constrRep con of
87 (FloatConstr x) -> realToFrac x
88 _ -> error "fromConstr"
89 dataTypeOf _ = floatType
92 ------------------------------------------------------------------------------
95 doubleType = mkFloatType "Prelude.Double"
97 instance Data Double where
98 toConstr = mkFloatConstr floatType
99 fromConstr con = case constrRep con of
101 _ -> error "fromConstr"
102 dataTypeOf _ = doubleType
105 ------------------------------------------------------------------------------
108 intType = mkIntType "Prelude.Int"
110 instance Data Int where
111 toConstr x = mkIntConstr intType (fromIntegral x)
112 fromConstr con = case constrRep con of
113 (IntConstr x) -> fromIntegral x
114 _ -> error "fromConstr"
115 dataTypeOf _ = intType
118 ------------------------------------------------------------------------------
121 integerType = mkIntType "Prelude.Integer"
123 instance Data Integer where
124 toConstr = mkIntConstr integerType
125 fromConstr con = case constrRep con of
127 _ -> error "fromConstr"
128 dataTypeOf _ = integerType
131 ------------------------------------------------------------------------------
134 int8Type = mkIntType "Data.Int.Int8"
136 instance Data Int8 where
137 toConstr x = mkIntConstr int8Type (fromIntegral x)
138 fromConstr con = case constrRep con of
139 (IntConstr x) -> fromIntegral x
140 _ -> error "fromConstr"
141 dataTypeOf _ = int8Type
144 ------------------------------------------------------------------------------
147 int16Type = mkIntType "Data.Int.Int16"
149 instance Data Int16 where
150 toConstr x = mkIntConstr int16Type (fromIntegral x)
151 fromConstr con = case constrRep con of
152 (IntConstr x) -> fromIntegral x
153 _ -> error "fromConstr"
154 dataTypeOf _ = int16Type
157 ------------------------------------------------------------------------------
160 int32Type = mkIntType "Data.Int.Int32"
162 instance Data Int32 where
163 toConstr x = mkIntConstr int32Type (fromIntegral x)
164 fromConstr con = case constrRep con of
165 (IntConstr x) -> fromIntegral x
166 _ -> error "fromConstr"
167 dataTypeOf _ = int32Type
170 ------------------------------------------------------------------------------
173 int64Type = mkIntType "Data.Int.Int64"
175 instance Data Int64 where
176 toConstr x = mkIntConstr int64Type (fromIntegral x)
177 fromConstr con = case constrRep con of
178 (IntConstr x) -> fromIntegral x
179 _ -> error "fromConstr"
180 dataTypeOf _ = int64Type
183 ------------------------------------------------------------------------------
186 wordType = mkIntType "Data.Word.Word"
188 instance Data Word where
189 toConstr x = mkIntConstr wordType (fromIntegral x)
190 fromConstr con = case constrRep con of
191 (IntConstr x) -> fromIntegral x
192 _ -> error "fromConstr"
193 dataTypeOf _ = wordType
196 ------------------------------------------------------------------------------
199 word8Type = mkIntType "Data.Word.Word8"
201 instance Data Word8 where
202 toConstr x = mkIntConstr word8Type (fromIntegral x)
203 fromConstr con = case constrRep con of
204 (IntConstr x) -> fromIntegral x
205 _ -> error "fromConstr"
206 dataTypeOf _ = word8Type
209 ------------------------------------------------------------------------------
212 word16Type = mkIntType "Data.Word.Word16"
214 instance Data Word16 where
215 toConstr x = mkIntConstr word16Type (fromIntegral x)
216 fromConstr con = case constrRep con of
217 (IntConstr x) -> fromIntegral x
218 _ -> error "fromConstr"
219 dataTypeOf _ = word16Type
222 ------------------------------------------------------------------------------
225 word32Type = mkIntType "Data.Word.Word32"
227 instance Data Word32 where
228 toConstr x = mkIntConstr word32Type (fromIntegral x)
229 fromConstr con = case constrRep con of
230 (IntConstr x) -> fromIntegral x
231 _ -> error "fromConstr"
232 dataTypeOf _ = word32Type
235 ------------------------------------------------------------------------------
238 word64Type = mkIntType "Data.Word.Word64"
240 instance Data Word64 where
241 toConstr x = mkIntConstr word64Type (fromIntegral x)
242 fromConstr con = case constrRep con of
243 (IntConstr x) -> fromIntegral x
244 _ -> error "fromConstr"
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 fromConstr c | constrIndex c == 1 = undefined :% undefined
257 fromConstr _ = error "fromConstr"
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 fromConstr c = case constrIndex c of
275 2 -> undefined:undefined
276 _ -> error "fromConstr"
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 fromConstr c = case constrIndex c of
307 _ -> error "fromConstr"
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 fromConstr c = case constrIndex c of
331 _ -> error "fromConstr"
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 fromConstr c = case constrIndex c of
350 _ -> error "fromConstr"
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 fromConstr _ = error "fromConstr"
365 dataTypeOf _ = mkNorepType "Prelude.(->)"
369 ------------------------------------------------------------------------------
372 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
373 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
375 instance Data () where
376 toConstr _ = tuple0Constr
377 fromConstr c | constrIndex c == 1 = ()
378 fromConstr _ = error "fromConstr"
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 fromConstr c | constrIndex c == 1 = (undefined,undefined)
392 fromConstr _ = error "fromConstr"
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 fromConstr c | constrIndex c == 1 = (undefined,undefined,undefined)
407 fromConstr _ = error "fromConstr"
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 fromConstr c = case constrIndex c of
422 1 -> (undefined,undefined,undefined,undefined)
423 _ -> error "fromConstr"
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 fromConstr c = case constrIndex c of
438 1 -> (undefined,undefined,undefined,undefined,undefined)
439 _ -> error "fromConstr"
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
454 case constrIndex c of
455 1 -> (undefined,undefined,undefined,undefined,undefined,undefined)
456 _ -> error "fromConstr"
457 dataTypeOf _ = tuple6DataType
460 ------------------------------------------------------------------------------
463 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
464 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
466 instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
467 => Data (a,b,c,d,e,f,g) where
468 gfoldl f z (a,b,c,d,e,f',g) =
469 z (,,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f' `f` g
470 toConstr _ = tuple7Constr
471 fromConstr c = case constrIndex c of
472 1 -> (undefined,undefined,undefined,undefined,undefined,undefined,undefined)
473 _ -> error "fromConstr"
474 dataTypeOf _ = tuple7DataType
477 ------------------------------------------------------------------------------
480 instance Data TypeRep where
481 toConstr _ = error "toConstr"
482 fromConstr _ = error "fromConstr"
483 dataTypeOf _ = mkNorepType "Data.Typeable.TypeRep"
486 ------------------------------------------------------------------------------
489 instance Data TyCon where
490 toConstr _ = error "toConstr"
491 fromConstr _ = error "fromConstr"
492 dataTypeOf _ = mkNorepType "Data.Typeable.TyCon"
495 ------------------------------------------------------------------------------
498 INSTANCE_TYPEABLE0(DataType,dataTypeTc,"DataType")
500 instance Data DataType where
501 toConstr _ = error "toConstr"
502 fromConstr _ = error "fromConstr"
503 dataTypeOf _ = mkNorepType "Data.Generics.Basics.DataType"
506 ------------------------------------------------------------------------------
509 instance Typeable a => Data (IO a) where
510 toConstr _ = error "toConstr"
511 fromConstr _ = error "fromConstr"
512 dataTypeOf _ = mkNorepType "GHC.IOBase.IO"
515 ------------------------------------------------------------------------------
518 instance Data Handle where
519 toConstr _ = error "toConstr"
520 fromConstr _ = error "fromConstr"
521 dataTypeOf _ = mkNorepType "GHC.IOBase.Handle"
524 ------------------------------------------------------------------------------
527 instance Typeable a => Data (Ptr a) where
528 toConstr _ = error "toConstr"
529 fromConstr _ = error "fromConstr"
530 dataTypeOf _ = mkNorepType "GHC.Ptr.Ptr"
533 ------------------------------------------------------------------------------
536 instance Typeable a => Data (StablePtr a) where
537 toConstr _ = error "toConstr"
538 fromConstr _ = error "fromConstr"
539 dataTypeOf _ = mkNorepType "GHC.Stable.StablePtr"
542 ------------------------------------------------------------------------------
545 instance Typeable a => Data (IORef a) where
546 toConstr _ = error "toConstr"
547 fromConstr _ = error "fromConstr"
548 dataTypeOf _ = mkNorepType "GHC.IOBase.IORef"
551 ------------------------------------------------------------------------------