[project @ 2004-03-17 23:22:51 by ralf]
[ghc-base.git] / Data / Generics / Instances.hs
1 -----------------------------------------------------------------------------
2 -- |
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)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  non-portable
10 --
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.)
15 --
16 -----------------------------------------------------------------------------
17
18 module Data.Generics.Instances 
19
20 where
21
22
23 ------------------------------------------------------------------------------
24
25 #ifdef __HADDOCK__
26 import Prelude
27 #endif
28
29 import Data.Generics.Basics
30
31 import Data.Typeable
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
38
39 #include "Typeable.h"
40
41
42  
43 ------------------------------------------------------------------------------
44 --
45 --      Instances of the Data class for Prelude-like types.
46 --      We define top-level definitions for representations.
47 --
48 ------------------------------------------------------------------------------
49
50
51 falseConstr  = mkConstr boolDataType "False" [] Prefix
52 trueConstr   = mkConstr boolDataType "True"  [] Prefix
53 boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
54
55
56 instance Data Bool where
57   toConstr False = falseConstr
58   toConstr True  = trueConstr
59   fromConstr c = case constrIndex c of
60                    1 -> False
61                    2 -> True
62                    _ -> error "fromConstr"
63   dataTypeOf _ = boolDataType
64
65
66 ------------------------------------------------------------------------------
67
68
69 charType = mkStringType "Prelude.Char"
70
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
77
78
79 ------------------------------------------------------------------------------
80
81
82 floatType = mkFloatType "Prelude.Float"
83
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
90
91
92 ------------------------------------------------------------------------------
93
94
95 doubleType = mkFloatType "Prelude.Double"
96
97 instance Data Double where
98   toConstr = mkFloatConstr floatType
99   fromConstr con = case constrRep con of
100                      (FloatConstr x) -> x
101                      _ -> error "fromConstr"
102   dataTypeOf _ = doubleType
103
104
105 ------------------------------------------------------------------------------
106
107
108 intType = mkIntType "Prelude.Int"
109
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
116
117
118 ------------------------------------------------------------------------------
119
120
121 integerType = mkIntType "Prelude.Integer"
122
123 instance Data Integer where
124   toConstr = mkIntConstr integerType
125   fromConstr con = case constrRep con of
126                      (IntConstr x) -> x
127                      _ -> error "fromConstr"
128   dataTypeOf _ = integerType
129
130
131 ------------------------------------------------------------------------------
132
133
134 int8Type = mkIntType "Data.Int.Int8"
135
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
142
143
144 ------------------------------------------------------------------------------
145
146
147 int16Type = mkIntType "Data.Int.Int16"
148
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
155
156
157 ------------------------------------------------------------------------------
158
159
160 int32Type = mkIntType "Data.Int.Int32"
161
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
168
169
170 ------------------------------------------------------------------------------
171
172
173 int64Type = mkIntType "Data.Int.Int64"
174
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
181
182
183 ------------------------------------------------------------------------------
184
185
186 wordType = mkIntType "Data.Word.Word"
187
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
194
195
196 ------------------------------------------------------------------------------
197
198
199 word8Type = mkIntType "Data.Word.Word8"
200
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
207
208
209 ------------------------------------------------------------------------------
210
211
212 word16Type = mkIntType "Data.Word.Word16"
213
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
220
221
222 ------------------------------------------------------------------------------
223
224
225 word32Type = mkIntType "Data.Word.Word32"
226
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
233
234
235 ------------------------------------------------------------------------------
236
237
238 word64Type = mkIntType "Data.Word.Word64"
239
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
246
247
248 ------------------------------------------------------------------------------
249
250
251 ratioConstr = mkConstr ratioDataType ":%" [] Infix
252 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
253
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
259
260
261 ------------------------------------------------------------------------------
262
263
264 nilConstr    = mkConstr listDataType "[]" [] Prefix
265 consConstr   = mkConstr listDataType "(:)" [] Infix
266 listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
267
268 instance Data a => Data [a] where
269   gfoldl f z []     = z []
270   gfoldl f z (x:xs) = z (:) `f` x `f` xs
271   toConstr []    = nilConstr
272   toConstr (_:_) = consConstr
273   fromConstr c = case constrIndex c of
274                    1 -> []
275                    2 -> undefined:undefined
276                    _ -> error "fromConstr"
277   dataTypeOf _ = listDataType
278   dataCast1    = gcast1
279
280 --
281 -- The gmaps are given as an illustration.
282 -- This shows that the gmaps for lists are different from list maps.
283 --
284   gmapT  f   []     = []
285   gmapT  f   (x:xs) = (f x:f xs)
286   gmapQ  f   []     = []
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')
290
291
292 ------------------------------------------------------------------------------
293
294
295 nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix
296 justConstr    = mkConstr maybeDataType "Just"    [] Prefix
297 maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]
298
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
305                    1 -> Nothing
306                    2 -> Just undefined
307                    _ -> error "fromConstr"
308   dataTypeOf _ = maybeDataType
309   dataCast1    = gcast1
310
311
312 ------------------------------------------------------------------------------
313
314
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]
319
320 instance Data Ordering where
321   gfoldl f z LT  = z LT
322   gfoldl f z EQ  = z EQ
323   gfoldl f z GT  = z GT
324   toConstr LT  = ltConstr
325   toConstr EQ  = eqConstr
326   toConstr GT  = gtConstr
327   fromConstr c = case constrIndex c of
328                    1 -> LT
329                    2 -> EQ
330                    3 -> GT
331                    _ -> error "fromConstr"
332   dataTypeOf _ = orderingDataType
333
334
335 ------------------------------------------------------------------------------
336
337
338 leftConstr     = mkConstr eitherDataType "Left"  [] Prefix
339 rightConstr    = mkConstr eitherDataType "Right" [] Prefix
340 eitherDataType = mkDataType "Prelude.Either" [leftConstr,rightConstr]
341
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
348                    1 -> Left undefined
349                    2 -> Right undefined
350                    _ -> error "fromConstr"
351   dataTypeOf _ = eitherDataType
352   dataCast2    = gcast2
353
354
355 ------------------------------------------------------------------------------
356
357
358 --
359 -- A last resort for functions
360 --
361
362 instance (Data a, Data b) => Data (a -> b) where
363   toConstr _   = error "toConstr"
364   fromConstr _ = error "fromConstr"
365   dataTypeOf _ = mkNorepType "Prelude.(->)"
366   dataCast2    = gcast2
367
368
369 ------------------------------------------------------------------------------
370
371
372 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
373 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
374
375 instance Data () where
376   toConstr _ = tuple0Constr
377   fromConstr c | constrIndex c == 1 = ()  
378   fromConstr _ = error "fromConstr"
379   dataTypeOf _ = tuple0DataType
380
381
382 ------------------------------------------------------------------------------
383
384
385 tuple2Constr = mkConstr tuple2DataType "(,)" [] Infix
386 tuple2DataType = mkDataType "Prelude.(,)" [tuple2Constr]
387
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
394   dataCast2    = gcast2
395
396
397 ------------------------------------------------------------------------------
398
399
400 tuple3Constr = mkConstr tuple3DataType "(,,)" [] Infix
401 tuple3DataType = mkDataType "Prelude.(,)" [tuple3Constr]
402
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
409
410
411 ------------------------------------------------------------------------------
412
413
414 tuple4Constr = mkConstr tuple4DataType "(,,,)" [] Infix
415 tuple4DataType = mkDataType "Prelude.(,,,)" [tuple4Constr]
416
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
425
426
427 ------------------------------------------------------------------------------
428
429
430 tuple5Constr = mkConstr tuple5DataType "(,,,,)" [] Infix
431 tuple5DataType = mkDataType "Prelude.(,,,,)" [tuple5Constr]
432
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
441
442
443 ------------------------------------------------------------------------------
444
445
446 tuple6Constr = mkConstr tuple6DataType "(,,,,,)" [] Infix
447 tuple6DataType = mkDataType "Prelude.(,,,,,)" [tuple6Constr]
448
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   fromConstr c =
454     case constrIndex c of
455            1 -> (undefined,undefined,undefined,undefined,undefined,undefined)
456            _ -> error "fromConstr"
457   dataTypeOf _ = tuple6DataType
458
459
460 ------------------------------------------------------------------------------
461
462
463 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
464 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
465
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
475
476
477 ------------------------------------------------------------------------------
478
479
480 instance Data TypeRep where
481   toConstr _   = error "toConstr"
482   fromConstr _ = error "fromConstr"
483   dataTypeOf _ = mkNorepType "Data.Typeable.TypeRep"
484
485
486 ------------------------------------------------------------------------------
487
488
489 instance Data TyCon where
490   toConstr _   = error "toConstr"
491   fromConstr _ = error "fromConstr"
492   dataTypeOf _ = mkNorepType "Data.Typeable.TyCon"
493
494
495 ------------------------------------------------------------------------------
496
497
498 INSTANCE_TYPEABLE0(DataType,dataTypeTc,"DataType")
499
500 instance Data DataType where
501   toConstr _   = error "toConstr"
502   fromConstr _ = error "fromConstr"
503   dataTypeOf _ = mkNorepType "Data.Generics.Basics.DataType"
504
505
506 ------------------------------------------------------------------------------
507
508
509 instance Typeable a => Data (IO a) where
510   toConstr _   = error "toConstr"
511   fromConstr _ = error "fromConstr"
512   dataTypeOf _ = mkNorepType "GHC.IOBase.IO"
513
514
515 ------------------------------------------------------------------------------
516
517
518 instance Data Handle where
519   toConstr _   = error "toConstr"
520   fromConstr _ = error "fromConstr"
521   dataTypeOf _ = mkNorepType "GHC.IOBase.Handle"
522
523
524 ------------------------------------------------------------------------------
525
526
527 instance Typeable a => Data (Ptr a) where
528   toConstr _   = error "toConstr"
529   fromConstr _ = error "fromConstr"
530   dataTypeOf _ = mkNorepType "GHC.Ptr.Ptr"
531
532
533 ------------------------------------------------------------------------------
534
535
536 instance Typeable a => Data (StablePtr a) where
537   toConstr _   = error "toConstr"
538   fromConstr _ = error "fromConstr"
539   dataTypeOf _ = mkNorepType "GHC.Stable.StablePtr"
540
541
542 ------------------------------------------------------------------------------
543
544
545 instance Typeable a => Data (IORef a) where
546   toConstr _   = error "toConstr"
547   fromConstr _ = error "fromConstr"
548   dataTypeOf _ = mkNorepType "GHC.IOBase.IORef"
549
550
551 ------------------------------------------------------------------------------