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.
15 -----------------------------------------------------------------------------
17 module Data.Generics.Instances
21 ------------------------------------------------------------------------------
27 import Data.Generics.Basics
30 import Data.Int -- So we can give Data instance for Int8, ...
31 import Data.Word -- So we can give Data instance for Word8, ...
32 import GHC.Real( Ratio(..) ) -- So we can give Data instance for Ratio
33 import GHC.IOBase -- So we can give Data instance for IO, Handle
34 import GHC.Ptr -- So we can give Data instance for Ptr
35 import GHC.Stable -- So we can give Data instance for StablePtr
41 ------------------------------------------------------------------------------
43 -- Instances of the Data class for Prelude-like types.
44 -- We define top-level definitions for representations.
46 ------------------------------------------------------------------------------
49 falseConstr = mkConstr boolDataType "False" [] Prefix
50 trueConstr = mkConstr boolDataType "True" [] Prefix
51 boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
53 instance Data Bool where
54 toConstr False = falseConstr
55 toConstr True = trueConstr
56 fromConstr c = case constrIndex c of
59 _ -> error "fromConstr"
60 dataTypeOf _ = boolDataType
63 ------------------------------------------------------------------------------
66 charType = mkStringType "Prelude.Char"
68 instance Data Char where
69 toConstr x = mkStringConstr charType [x]
70 fromConstr con = case constrRep con of
71 (StringConstr [x]) -> x
72 _ -> error "fromConstr"
73 dataTypeOf _ = charType
76 ------------------------------------------------------------------------------
79 floatType = mkFloatType "Prelude.Float"
81 instance Data Float where
82 toConstr x = mkFloatConstr floatType (realToFrac x)
83 fromConstr con = case constrRep con of
84 (FloatConstr x) -> realToFrac x
85 _ -> error "fromConstr"
86 dataTypeOf _ = floatType
89 ------------------------------------------------------------------------------
92 doubleType = mkFloatType "Prelude.Double"
94 instance Data Double where
95 toConstr = mkFloatConstr floatType
96 fromConstr con = case constrRep con of
98 _ -> error "fromConstr"
99 dataTypeOf _ = doubleType
102 ------------------------------------------------------------------------------
105 intType = mkIntType "Prelude.Int"
107 instance Data Int where
108 toConstr x = mkIntConstr intType (fromIntegral x)
109 fromConstr con = case constrRep con of
110 (IntConstr x) -> fromIntegral x
111 _ -> error "fromConstr"
112 dataTypeOf _ = intType
115 ------------------------------------------------------------------------------
118 integerType = mkIntType "Prelude.Integer"
120 instance Data Integer where
121 toConstr = mkIntConstr integerType
122 fromConstr con = case constrRep con of
124 _ -> error "fromConstr"
125 dataTypeOf _ = integerType
128 ------------------------------------------------------------------------------
131 int8Type = mkIntType "Data.Int.Int8"
133 instance Data Int8 where
134 toConstr x = mkIntConstr int8Type (fromIntegral x)
135 fromConstr con = case constrRep con of
136 (IntConstr x) -> fromIntegral x
137 _ -> error "fromConstr"
138 dataTypeOf _ = int8Type
141 ------------------------------------------------------------------------------
144 int16Type = mkIntType "Data.Int.Int16"
146 instance Data Int16 where
147 toConstr x = mkIntConstr int16Type (fromIntegral x)
148 fromConstr con = case constrRep con of
149 (IntConstr x) -> fromIntegral x
150 _ -> error "fromConstr"
151 dataTypeOf _ = int16Type
154 ------------------------------------------------------------------------------
157 int32Type = mkIntType "Data.Int.Int32"
159 instance Data Int32 where
160 toConstr x = mkIntConstr int32Type (fromIntegral x)
161 fromConstr con = case constrRep con of
162 (IntConstr x) -> fromIntegral x
163 _ -> error "fromConstr"
164 dataTypeOf _ = int32Type
167 ------------------------------------------------------------------------------
170 int64Type = mkIntType "Data.Int.Int64"
172 instance Data Int64 where
173 toConstr x = mkIntConstr int64Type (fromIntegral x)
174 fromConstr con = case constrRep con of
175 (IntConstr x) -> fromIntegral x
176 _ -> error "fromConstr"
177 dataTypeOf _ = int64Type
180 ------------------------------------------------------------------------------
183 wordType = mkIntType "Data.Word.Word"
185 instance Data Word where
186 toConstr x = mkIntConstr wordType (fromIntegral x)
187 fromConstr con = case constrRep con of
188 (IntConstr x) -> fromIntegral x
189 _ -> error "fromConstr"
190 dataTypeOf _ = wordType
193 ------------------------------------------------------------------------------
196 word8Type = mkIntType "Data.Word.Word8"
198 instance Data Word8 where
199 toConstr x = mkIntConstr word8Type (fromIntegral x)
200 fromConstr con = case constrRep con of
201 (IntConstr x) -> fromIntegral x
202 _ -> error "fromConstr"
203 dataTypeOf _ = word8Type
206 ------------------------------------------------------------------------------
209 word16Type = mkIntType "Data.Word.Word16"
211 instance Data Word16 where
212 toConstr x = mkIntConstr word16Type (fromIntegral x)
213 fromConstr con = case constrRep con of
214 (IntConstr x) -> fromIntegral x
215 _ -> error "fromConstr"
216 dataTypeOf _ = word16Type
219 ------------------------------------------------------------------------------
222 word32Type = mkIntType "Data.Word.Word32"
224 instance Data Word32 where
225 toConstr x = mkIntConstr word32Type (fromIntegral x)
226 fromConstr con = case constrRep con of
227 (IntConstr x) -> fromIntegral x
228 _ -> error "fromConstr"
229 dataTypeOf _ = word32Type
232 ------------------------------------------------------------------------------
235 word64Type = mkIntType "Data.Word.Word64"
237 instance Data Word64 where
238 toConstr x = mkIntConstr word64Type (fromIntegral x)
239 fromConstr con = case constrRep con of
240 (IntConstr x) -> fromIntegral x
241 _ -> error "fromConstr"
242 dataTypeOf _ = word64Type
245 ------------------------------------------------------------------------------
248 ratioConstr = mkConstr ratioDataType ":%" [] Infix
249 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
251 instance (Data a, Integral a) => Data (Ratio a) where
252 toConstr _ = ratioConstr
253 fromConstr c | constrIndex c == 1 = undefined :% undefined
254 fromConstr _ = error "fromConstr"
255 dataTypeOf _ = ratioDataType
258 ------------------------------------------------------------------------------
261 nilConstr = mkConstr listDataType "[]" [] Prefix
262 consConstr = mkConstr listDataType "(:)" [] Infix
263 listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
265 instance Data a => Data [a] where
267 gfoldl f z (x:xs) = z (:) `f` x `f` xs
268 toConstr [] = nilConstr
269 toConstr (_:_) = consConstr
270 fromConstr c = case constrIndex c of
272 2 -> undefined:undefined
273 _ -> error "fromConstr"
274 dataTypeOf _ = listDataType
278 -- The gmaps are given as an illustration.
279 -- This shows that the gmaps for lists are different from list maps.
282 gmapT f (x:xs) = (f x:f xs)
284 gmapQ f (x:xs) = [f x,f xs]
285 gmapM f [] = return []
286 gmapM f (x:xs) = f x >>= \x' -> f xs >>= \xs' -> return (x':xs')
289 ------------------------------------------------------------------------------
292 nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix
293 justConstr = mkConstr maybeDataType "Just" [] Prefix
294 maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]
296 instance Data a => Data (Maybe a) where
297 gfoldl f z Nothing = z Nothing
298 gfoldl f z (Just x) = z Just `f` x
299 toConstr Nothing = nothingConstr
300 toConstr (Just _) = justConstr
301 fromConstr c = case constrIndex c of
304 _ -> error "fromConstr"
305 dataTypeOf _ = maybeDataType
309 ------------------------------------------------------------------------------
312 ltConstr = mkConstr orderingDataType "LT" [] Prefix
313 eqConstr = mkConstr orderingDataType "EQ" [] Prefix
314 gtConstr = mkConstr orderingDataType "GT" [] Prefix
315 orderingDataType = mkDataType "Prelude.Ordering" [ltConstr,eqConstr,gtConstr]
317 instance Data Ordering where
321 toConstr LT = ltConstr
322 toConstr EQ = eqConstr
323 toConstr GT = gtConstr
324 fromConstr c = case constrIndex c of
328 _ -> error "fromConstr"
329 dataTypeOf _ = orderingDataType
332 ------------------------------------------------------------------------------
335 leftConstr = mkConstr eitherDataType "Left" [] Prefix
336 rightConstr = mkConstr eitherDataType "Right" [] Prefix
337 eitherDataType = mkDataType "Prelude.Either" [leftConstr,rightConstr]
339 instance (Data a, Data b) => Data (Either a b) where
340 gfoldl f z (Left a) = z Left `f` a
341 gfoldl f z (Right a) = z Right `f` a
342 toConstr (Left _) = leftConstr
343 toConstr (Right _) = rightConstr
344 fromConstr c = case constrIndex c of
347 _ -> error "fromConstr"
348 dataTypeOf _ = eitherDataType
352 ------------------------------------------------------------------------------
356 -- A last resort for functions
359 instance (Data a, Data b) => Data (a -> b) where
360 toConstr _ = error "toConstr"
361 fromConstr _ = error "fromConstr"
362 dataTypeOf _ = mkNorepType "Prelude.(->)"
366 ------------------------------------------------------------------------------
369 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
370 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
372 instance Data () where
373 toConstr _ = tuple0Constr
374 fromConstr c | constrIndex c == 1 = ()
375 fromConstr _ = error "fromConstr"
376 dataTypeOf _ = tuple0DataType
379 ------------------------------------------------------------------------------
382 tuple2Constr = mkConstr tuple2DataType "(,)" [] Infix
383 tuple2DataType = mkDataType "Prelude.(,)" [tuple2Constr]
385 instance (Data a, Data b) => Data (a,b) where
386 gfoldl f z (a,b) = z (,) `f` a `f` b
387 toConstr _ = tuple2Constr
388 fromConstr c | constrIndex c == 1 = (undefined,undefined)
389 fromConstr _ = error "fromConstr"
390 dataTypeOf _ = tuple2DataType
394 ------------------------------------------------------------------------------
397 tuple3Constr = mkConstr tuple3DataType "(,,)" [] Infix
398 tuple3DataType = mkDataType "Prelude.(,)" [tuple3Constr]
400 instance (Data a, Data b, Data c) => Data (a,b,c) where
401 gfoldl f z (a,b,c) = z (,,) `f` a `f` b `f` c
402 toConstr _ = tuple3Constr
403 fromConstr c | constrIndex c == 1 = (undefined,undefined,undefined)
404 fromConstr _ = error "fromConstr"
405 dataTypeOf _ = tuple3DataType
408 ------------------------------------------------------------------------------
411 tuple4Constr = mkConstr tuple4DataType "(,,,)" [] Infix
412 tuple4DataType = mkDataType "Prelude.(,,,)" [tuple4Constr]
414 instance (Data a, Data b, Data c, Data d)
415 => Data (a,b,c,d) where
416 gfoldl f z (a,b,c,d) = z (,,,) `f` a `f` b `f` c `f` d
417 toConstr _ = tuple4Constr
418 fromConstr c = case constrIndex c of
419 1 -> (undefined,undefined,undefined,undefined)
420 _ -> error "fromConstr"
421 dataTypeOf _ = tuple4DataType
424 ------------------------------------------------------------------------------
427 tuple5Constr = mkConstr tuple5DataType "(,,,,)" [] Infix
428 tuple5DataType = mkDataType "Prelude.(,,,,)" [tuple5Constr]
430 instance (Data a, Data b, Data c, Data d, Data e)
431 => Data (a,b,c,d,e) where
432 gfoldl f z (a,b,c,d,e) = z (,,,,) `f` a `f` b `f` c `f` d `f` e
433 toConstr _ = tuple5Constr
434 fromConstr c = case constrIndex c of
435 1 -> (undefined,undefined,undefined,undefined,undefined)
436 _ -> error "fromConstr"
437 dataTypeOf _ = tuple5DataType
440 ------------------------------------------------------------------------------
443 tuple6Constr = mkConstr tuple6DataType "(,,,,,)" [] Infix
444 tuple6DataType = mkDataType "Prelude.(,,,,,)" [tuple6Constr]
446 instance (Data a, Data b, Data c, Data d, Data e, Data f)
447 => Data (a,b,c,d,e,f) where
448 gfoldl f z (a,b,c,d,e,f') = z (,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f'
449 toConstr _ = tuple6Constr
451 case constrIndex c of
452 1 -> (undefined,undefined,undefined,undefined,undefined,undefined)
453 _ -> error "fromConstr"
454 dataTypeOf _ = tuple6DataType
457 ------------------------------------------------------------------------------
460 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
461 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
463 instance (Data a, Data b, Data c, Data d, Data e, Data f, Data g)
464 => Data (a,b,c,d,e,f,g) where
465 gfoldl f z (a,b,c,d,e,f',g) =
466 z (,,,,,,) `f` a `f` b `f` c `f` d `f` e `f` f' `f` g
467 toConstr _ = tuple7Constr
468 fromConstr c = case constrIndex c of
469 1 -> (undefined,undefined,undefined,undefined,undefined,undefined,undefined)
470 _ -> error "fromConstr"
471 dataTypeOf _ = tuple7DataType
474 ------------------------------------------------------------------------------
477 instance Data TypeRep where
478 toConstr _ = error "toConstr"
479 fromConstr _ = error "fromConstr"
480 dataTypeOf _ = mkNorepType "Data.Typeable.TypeRep"
483 ------------------------------------------------------------------------------
486 instance Data TyCon where
487 toConstr _ = error "toConstr"
488 fromConstr _ = error "fromConstr"
489 dataTypeOf _ = mkNorepType "Data.Typeable.TyCon"
492 ------------------------------------------------------------------------------
495 INSTANCE_TYPEABLE0(DataType,dataTypeTc,"DataType")
497 instance Data DataType where
498 toConstr _ = error "toConstr"
499 fromConstr _ = error "fromConstr"
500 dataTypeOf _ = mkNorepType "Data.Generics.Basics.DataType"
503 ------------------------------------------------------------------------------
506 instance Typeable a => Data (IO a) where
507 toConstr _ = error "toConstr"
508 fromConstr _ = error "fromConstr"
509 dataTypeOf _ = mkNorepType "GHC.IOBase.IO"
512 ------------------------------------------------------------------------------
515 instance Data Handle where
516 toConstr _ = error "toConstr"
517 fromConstr _ = error "fromConstr"
518 dataTypeOf _ = mkNorepType "GHC.IOBase.Handle"
521 ------------------------------------------------------------------------------
524 instance Typeable a => Data (Ptr a) where
525 toConstr _ = error "toConstr"
526 fromConstr _ = error "fromConstr"
527 dataTypeOf _ = mkNorepType "GHC.Ptr.Ptr"
530 ------------------------------------------------------------------------------
533 instance Typeable a => Data (StablePtr a) where
534 toConstr _ = error "toConstr"
535 fromConstr _ = error "fromConstr"
536 dataTypeOf _ = mkNorepType "GHC.Stable.StablePtr"
539 ------------------------------------------------------------------------------
542 instance Typeable a => Data (IORef a) where
543 toConstr _ = error "toConstr"
544 fromConstr _ = error "fromConstr"
545 dataTypeOf _ = mkNorepType "GHC.IOBase.IORef"
548 ------------------------------------------------------------------------------