[project @ 2004-03-16 13:46:07 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 --
15 -----------------------------------------------------------------------------
16
17 module Data.Generics.Instances 
18 where
19
20
21 ------------------------------------------------------------------------------
22
23 #ifdef __HADDOCK__
24 import Prelude
25 #endif
26
27 import Data.Generics.Basics
28
29 import Data.Typeable
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
36
37 #include "Typeable.h"
38
39
40  
41 ------------------------------------------------------------------------------
42 --
43 --      Instances of the Data class for Prelude-like types.
44 --      We define top-level definitions for representations.
45 --
46 ------------------------------------------------------------------------------
47
48
49 falseConstr  = mkConstr boolDataType "False" [] Prefix
50 trueConstr   = mkConstr boolDataType "True"  [] Prefix
51 boolDataType = mkDataType "Prelude.Bool" [falseConstr,trueConstr]
52
53 instance Data Bool where
54   toConstr False = falseConstr
55   toConstr True  = trueConstr
56   fromConstr c = case constrIndex c of
57                    1 -> False
58                    2 -> True
59                    _ -> error "fromConstr"
60   dataTypeOf _ = boolDataType
61
62
63 ------------------------------------------------------------------------------
64
65
66 charType = mkStringType "Prelude.Char"
67
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
74
75
76 ------------------------------------------------------------------------------
77
78
79 floatType = mkFloatType "Prelude.Float"
80
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
87
88
89 ------------------------------------------------------------------------------
90
91
92 doubleType = mkFloatType "Prelude.Double"
93
94 instance Data Double where
95   toConstr = mkFloatConstr floatType
96   fromConstr con = case constrRep con of
97                      (FloatConstr x) -> x
98                      _ -> error "fromConstr"
99   dataTypeOf _ = doubleType
100
101
102 ------------------------------------------------------------------------------
103
104
105 intType = mkIntType "Prelude.Int"
106
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
113
114
115 ------------------------------------------------------------------------------
116
117
118 integerType = mkIntType "Prelude.Integer"
119
120 instance Data Integer where
121   toConstr = mkIntConstr integerType
122   fromConstr con = case constrRep con of
123                      (IntConstr x) -> x
124                      _ -> error "fromConstr"
125   dataTypeOf _ = integerType
126
127
128 ------------------------------------------------------------------------------
129
130
131 int8Type = mkIntType "Data.Int.Int8"
132
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
139
140
141 ------------------------------------------------------------------------------
142
143
144 int16Type = mkIntType "Data.Int.Int16"
145
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
152
153
154 ------------------------------------------------------------------------------
155
156
157 int32Type = mkIntType "Data.Int.Int32"
158
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
165
166
167 ------------------------------------------------------------------------------
168
169
170 int64Type = mkIntType "Data.Int.Int64"
171
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
178
179
180 ------------------------------------------------------------------------------
181
182
183 wordType = mkIntType "Data.Word.Word"
184
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
191
192
193 ------------------------------------------------------------------------------
194
195
196 word8Type = mkIntType "Data.Word.Word8"
197
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
204
205
206 ------------------------------------------------------------------------------
207
208
209 word16Type = mkIntType "Data.Word.Word16"
210
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
217
218
219 ------------------------------------------------------------------------------
220
221
222 word32Type = mkIntType "Data.Word.Word32"
223
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
230
231
232 ------------------------------------------------------------------------------
233
234
235 word64Type = mkIntType "Data.Word.Word64"
236
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
243
244
245 ------------------------------------------------------------------------------
246
247
248 ratioConstr = mkConstr ratioDataType ":%" [] Infix
249 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
250
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
256
257
258 ------------------------------------------------------------------------------
259
260
261 nilConstr    = mkConstr listDataType "[]"  [] Prefix
262 consConstr   = mkConstr listDataType "(:)" [] Infix
263 listDataType = mkDataType "Prelude.[]" [nilConstr,consConstr]
264
265 instance Data a => Data [a] where
266   gfoldl f z []     = z []
267   gfoldl f z (x:xs) = z (:) `f` x `f` xs
268   toConstr []    = nilConstr
269   toConstr (_:_) = consConstr
270   fromConstr c = case constrIndex c of
271                    1 -> []
272                    2 -> undefined:undefined
273                    _ -> error "fromConstr"
274   dataTypeOf _ = listDataType
275   dataCast1    = gcast1
276
277 --
278 -- The gmaps are given as an illustration.
279 -- This shows that the gmaps for lists are different from list maps.
280 --
281   gmapT  f   []     = []
282   gmapT  f   (x:xs) = (f x:f xs)
283   gmapQ  f   []     = []
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')
287
288
289 ------------------------------------------------------------------------------
290
291
292 nothingConstr = mkConstr maybeDataType "Nothing" [] Prefix
293 justConstr    = mkConstr maybeDataType "Just"    [] Prefix
294 maybeDataType = mkDataType "Prelude.Maybe" [nothingConstr,justConstr]
295
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
302                    1 -> Nothing
303                    2 -> Just undefined
304                    _ -> error "fromConstr"
305   dataTypeOf _ = maybeDataType
306   dataCast1    = gcast1
307
308
309 ------------------------------------------------------------------------------
310
311
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]
316
317 instance Data Ordering where
318   gfoldl f z LT  = z LT
319   gfoldl f z EQ  = z EQ
320   gfoldl f z GT  = z GT
321   toConstr LT  = ltConstr
322   toConstr EQ  = eqConstr
323   toConstr GT  = gtConstr
324   fromConstr c = case constrIndex c of
325                    1 -> LT
326                    2 -> EQ
327                    3 -> GT
328                    _ -> error "fromConstr"
329   dataTypeOf _ = orderingDataType
330
331
332 ------------------------------------------------------------------------------
333
334
335 leftConstr     = mkConstr eitherDataType "Left"  [] Prefix
336 rightConstr    = mkConstr eitherDataType "Right" [] Prefix
337 eitherDataType = mkDataType "Prelude.Either" [leftConstr,rightConstr]
338
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
345                    1 -> Left undefined
346                    2 -> Right undefined
347                    _ -> error "fromConstr"
348   dataTypeOf _ = eitherDataType
349   dataCast2    = gcast2
350
351
352 ------------------------------------------------------------------------------
353
354
355 --
356 -- A last resort for functions
357 --
358
359 instance (Data a, Data b) => Data (a -> b) where
360   toConstr _   = error "toConstr"
361   fromConstr _ = error "fromConstr"
362   dataTypeOf _ = mkNorepType "Prelude.(->)"
363   dataCast2    = gcast2
364
365
366 ------------------------------------------------------------------------------
367
368
369 tuple0Constr = mkConstr tuple0DataType "()" [] Prefix
370 tuple0DataType = mkDataType "Prelude.()" [tuple0Constr]
371
372 instance Data () where
373   toConstr _ = tuple0Constr
374   fromConstr c | constrIndex c == 1 = ()  
375   fromConstr _ = error "fromConstr"
376   dataTypeOf _ = tuple0DataType
377
378
379 ------------------------------------------------------------------------------
380
381
382 tuple2Constr = mkConstr tuple2DataType "(,)" [] Infix
383 tuple2DataType = mkDataType "Prelude.(,)" [tuple2Constr]
384
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
391   dataCast2    = gcast2
392
393
394 ------------------------------------------------------------------------------
395
396
397 tuple3Constr = mkConstr tuple3DataType "(,,)" [] Infix
398 tuple3DataType = mkDataType "Prelude.(,)" [tuple3Constr]
399
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
406
407
408 ------------------------------------------------------------------------------
409
410
411 tuple4Constr = mkConstr tuple4DataType "(,,,)" [] Infix
412 tuple4DataType = mkDataType "Prelude.(,,,)" [tuple4Constr]
413
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
422
423
424 ------------------------------------------------------------------------------
425
426
427 tuple5Constr = mkConstr tuple5DataType "(,,,,)" [] Infix
428 tuple5DataType = mkDataType "Prelude.(,,,,)" [tuple5Constr]
429
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
438
439
440 ------------------------------------------------------------------------------
441
442
443 tuple6Constr = mkConstr tuple6DataType "(,,,,,)" [] Infix
444 tuple6DataType = mkDataType "Prelude.(,,,,,)" [tuple6Constr]
445
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
450   fromConstr c =
451     case constrIndex c of
452            1 -> (undefined,undefined,undefined,undefined,undefined,undefined)
453            _ -> error "fromConstr"
454   dataTypeOf _ = tuple6DataType
455
456
457 ------------------------------------------------------------------------------
458
459
460 tuple7Constr = mkConstr tuple7DataType "(,,,,,,)" [] Infix
461 tuple7DataType = mkDataType "Prelude.(,,,,,,)" [tuple7Constr]
462
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
472
473
474 ------------------------------------------------------------------------------
475
476
477 instance Data TypeRep where
478   toConstr _   = error "toConstr"
479   fromConstr _ = error "fromConstr"
480   dataTypeOf _ = mkNorepType "Data.Typeable.TypeRep"
481
482
483 ------------------------------------------------------------------------------
484
485
486 instance Data TyCon where
487   toConstr _   = error "toConstr"
488   fromConstr _ = error "fromConstr"
489   dataTypeOf _ = mkNorepType "Data.Typeable.TyCon"
490
491
492 ------------------------------------------------------------------------------
493
494
495 INSTANCE_TYPEABLE0(DataType,dataTypeTc,"DataType")
496
497 instance Data DataType where
498   toConstr _   = error "toConstr"
499   fromConstr _ = error "fromConstr"
500   dataTypeOf _ = mkNorepType "Data.Generics.Basics.DataType"
501
502
503 ------------------------------------------------------------------------------
504
505
506 instance Typeable a => Data (IO a) where
507   toConstr _   = error "toConstr"
508   fromConstr _ = error "fromConstr"
509   dataTypeOf _ = mkNorepType "GHC.IOBase.IO"
510
511
512 ------------------------------------------------------------------------------
513
514
515 instance Data Handle where
516   toConstr _   = error "toConstr"
517   fromConstr _ = error "fromConstr"
518   dataTypeOf _ = mkNorepType "GHC.IOBase.Handle"
519
520
521 ------------------------------------------------------------------------------
522
523
524 instance Typeable a => Data (Ptr a) where
525   toConstr _   = error "toConstr"
526   fromConstr _ = error "fromConstr"
527   dataTypeOf _ = mkNorepType "GHC.Ptr.Ptr"
528
529
530 ------------------------------------------------------------------------------
531
532
533 instance Typeable a => Data (StablePtr a) where
534   toConstr _   = error "toConstr"
535   fromConstr _ = error "fromConstr"
536   dataTypeOf _ = mkNorepType "GHC.Stable.StablePtr"
537
538
539 ------------------------------------------------------------------------------
540
541
542 instance Typeable a => Data (IORef a) where
543   toConstr _   = error "toConstr"
544   fromConstr _ = error "fromConstr"
545   dataTypeOf _ = mkNorepType "GHC.IOBase.IORef"
546
547
548 ------------------------------------------------------------------------------