Add Ord instances for GHC.Generics (#7263)
[ghc.git] / libraries / base / GHC / Generics.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP #-}
3 {-# LANGUAGE NoImplicitPrelude #-}
4 {-# LANGUAGE TypeSynonymInstances #-}
5 {-# LANGUAGE TypeOperators #-}
6 {-# LANGUAGE KindSignatures #-}
7 {-# LANGUAGE TypeFamilies #-}
8 {-# LANGUAGE StandaloneDeriving #-}
9 {-# LANGUAGE DeriveGeneric #-}
10
11 -----------------------------------------------------------------------------
12 -- |
13 -- Module : GHC.Generics
14 -- Copyright : (c) Universiteit Utrecht 2010-2011, University of Oxford 2012-2013
15 -- License : see libraries/base/LICENSE
16 --
17 -- Maintainer : libraries@haskell.org
18 -- Stability : internal
19 -- Portability : non-portable
20 --
21 -- /Since: 4.6.0.0/
22 --
23 -- If you're using @GHC.Generics@, you should consider using the
24 -- <http://hackage.haskell.org/package/generic-deriving> package, which
25 -- contains many useful generic functions.
26
27 module GHC.Generics (
28 -- * Introduction
29 --
30 -- |
31 --
32 -- Datatype-generic functions are are based on the idea of converting values of
33 -- a datatype @T@ into corresponding values of a (nearly) isomorphic type @'Rep' T@.
34 -- The type @'Rep' T@ is
35 -- built from a limited set of type constructors, all provided by this module. A
36 -- datatype-generic function is then an overloaded function with instances
37 -- for most of these type constructors, together with a wrapper that performs
38 -- the mapping between @T@ and @'Rep' T@. By using this technique, we merely need
39 -- a few generic instances in order to implement functionality that works for any
40 -- representable type.
41 --
42 -- Representable types are collected in the 'Generic' class, which defines the
43 -- associated type 'Rep' as well as conversion functions 'from' and 'to'.
44 -- Typically, you will not define 'Generic' instances by hand, but have the compiler
45 -- derive them for you.
46
47 -- ** Representing datatypes
48 --
49 -- |
50 --
51 -- The key to defining your own datatype-generic functions is to understand how to
52 -- represent datatypes using the given set of type constructors.
53 --
54 -- Let us look at an example first:
55 --
56 -- @
57 -- data Tree a = Leaf a | Node (Tree a) (Tree a)
58 -- deriving 'Generic'
59 -- @
60 --
61 -- The above declaration (which requires the language pragma @DeriveGeneric@)
62 -- causes the following representation to be generated:
63 --
64 -- @
65 -- instance 'Generic' (Tree a) where
66 -- type 'Rep' (Tree a) =
67 -- 'D1' D1Tree
68 -- ('C1' C1_0Tree
69 -- ('S1' 'NoSelector' ('Par0' a))
70 -- ':+:'
71 -- 'C1' C1_1Tree
72 -- ('S1' 'NoSelector' ('Rec0' (Tree a))
73 -- ':*:'
74 -- 'S1' 'NoSelector' ('Rec0' (Tree a))))
75 -- ...
76 -- @
77 --
78 -- /Hint:/ You can obtain information about the code being generated from GHC by passing
79 -- the @-ddump-deriv@ flag. In GHCi, you can expand a type family such as 'Rep' using
80 -- the @:kind!@ command.
81 --
82 #if 0
83 -- /TODO:/ Newer GHC versions abandon the distinction between 'Par0' and 'Rec0' and will
84 -- use 'Rec0' everywhere.
85 --
86 #endif
87 -- This is a lot of information! However, most of it is actually merely meta-information
88 -- that makes names of datatypes and constructors and more available on the type level.
89 --
90 -- Here is a reduced representation for 'Tree' with nearly all meta-information removed,
91 -- for now keeping only the most essential aspects:
92 --
93 -- @
94 -- instance 'Generic' (Tree a) where
95 -- type 'Rep' (Tree a) =
96 -- 'Par0' a
97 -- ':+:'
98 -- ('Rec0' (Tree a) ':*:' 'Rec0' (Tree a))
99 -- @
100 --
101 -- The @Tree@ datatype has two constructors. The representation of individual constructors
102 -- is combined using the binary type constructor ':+:'.
103 --
104 -- The first constructor consists of a single field, which is the parameter @a@. This is
105 -- represented as @'Par0' a@.
106 --
107 -- The second constructor consists of two fields. Each is a recursive field of type @Tree a@,
108 -- represented as @'Rec0' (Tree a)@. Representations of individual fields are combined using
109 -- the binary type constructor ':*:'.
110 --
111 -- Now let us explain the additional tags being used in the complete representation:
112 --
113 -- * The @'S1' 'NoSelector'@ indicates that there is no record field selector associated with
114 -- this field of the constructor.
115 --
116 -- * The @'C1' C1_0Tree@ and @'C1' C1_1Tree@ invocations indicate that the enclosed part is
117 -- the representation of the first and second constructor of datatype @Tree@, respectively.
118 -- Here, @C1_0Tree@ and @C1_1Tree@ are datatypes generated by the compiler as part of
119 -- @deriving 'Generic'@. These datatypes are proxy types with no values. They are useful
120 -- because they are instances of the type class 'Constructor'. This type class can be used
121 -- to obtain information about the constructor in question, such as its name
122 -- or infix priority.
123 --
124 -- * The @'D1' D1Tree@ tag indicates that the enclosed part is the representation of the
125 -- datatype @Tree@. Again, @D1Tree@ is a datatype generated by the compiler. It is a
126 -- proxy type, and is useful by being an instance of class 'Datatype', which
127 -- can be used to obtain the name of a datatype, the module it has been defined in, and
128 -- whether it has been defined using @data@ or @newtype@.
129
130 -- ** Derived and fundamental representation types
131 --
132 -- |
133 --
134 -- There are many datatype-generic functions that do not distinguish between positions that
135 -- are parameters or positions that are recursive calls. There are also many datatype-generic
136 -- functions that do not care about the names of datatypes and constructors at all. To keep
137 -- the number of cases to consider in generic functions in such a situation to a minimum,
138 -- it turns out that many of the type constructors introduced above are actually synonyms,
139 -- defining them to be variants of a smaller set of constructors.
140
141 -- *** Individual fields of constructors: 'K1'
142 --
143 -- |
144 --
145 -- The type constructors 'Par0' and 'Rec0' are variants of 'K1':
146 --
147 -- @
148 -- type 'Par0' = 'K1' 'P'
149 -- type 'Rec0' = 'K1' 'R'
150 -- @
151 --
152 -- Here, 'P' and 'R' are type-level proxies again that do not have any associated values.
153
154 -- *** Meta information: 'M1'
155 --
156 -- |
157 --
158 -- The type constructors 'S1', 'C1' and 'D1' are all variants of 'M1':
159 --
160 -- @
161 -- type 'S1' = 'M1' 'S'
162 -- type 'C1' = 'M1' 'C'
163 -- type 'D1' = 'M1' 'D'
164 -- @
165 --
166 -- The types 'S', 'C' and 'R' are once again type-level proxies, just used to create
167 -- several variants of 'M1'.
168
169 -- *** Additional generic representation type constructors
170 --
171 -- |
172 --
173 -- Next to 'K1', 'M1', ':+:' and ':*:' there are a few more type constructors that occur
174 -- in the representations of other datatypes.
175
176 -- **** Empty datatypes: 'V1'
177 --
178 -- |
179 --
180 -- For empty datatypes, 'V1' is used as a representation. For example,
181 --
182 -- @
183 -- data Empty deriving 'Generic'
184 -- @
185 --
186 -- yields
187 --
188 -- @
189 -- instance 'Generic' Empty where
190 -- type 'Rep' Empty = 'D1' D1Empty 'V1'
191 -- @
192
193 -- **** Constructors without fields: 'U1'
194 --
195 -- |
196 --
197 -- If a constructor has no arguments, then 'U1' is used as its representation. For example
198 -- the representation of 'Bool' is
199 --
200 -- @
201 -- instance 'Generic' Bool where
202 -- type 'Rep' Bool =
203 -- 'D1' D1Bool
204 -- ('C1' C1_0Bool 'U1' ':+:' 'C1' C1_1Bool 'U1')
205 -- @
206
207 -- *** Representation of types with many constructors or many fields
208 --
209 -- |
210 --
211 -- As ':+:' and ':*:' are just binary operators, one might ask what happens if the
212 -- datatype has more than two constructors, or a constructor with more than two
213 -- fields. The answer is simple: the operators are used several times, to combine
214 -- all the constructors and fields as needed. However, users /should not rely on
215 -- a specific nesting strategy/ for ':+:' and ':*:' being used. The compiler is
216 -- free to choose any nesting it prefers. (In practice, the current implementation
217 -- tries to produce a more or less balanced nesting, so that the traversal of the
218 -- structure of the datatype from the root to a particular component can be performed
219 -- in logarithmic rather than linear time.)
220
221 -- ** Defining datatype-generic functions
222 --
223 -- |
224 --
225 -- A datatype-generic function comprises two parts:
226 --
227 -- 1. /Generic instances/ for the function, implementing it for most of the representation
228 -- type constructors introduced above.
229 --
230 -- 2. A /wrapper/ that for any datatype that is in `Generic`, performs the conversion
231 -- between the original value and its `Rep`-based representation and then invokes the
232 -- generic instances.
233 --
234 -- As an example, let us look at a function 'encode' that produces a naive, but lossless
235 -- bit encoding of values of various datatypes. So we are aiming to define a function
236 --
237 -- @
238 -- encode :: 'Generic' a => a -> [Bool]
239 -- @
240 --
241 -- where we use 'Bool' as our datatype for bits.
242 --
243 -- For part 1, we define a class @Encode'@. Perhaps surprisingly, this class is parameterized
244 -- over a type constructor @f@ of kind @* -> *@. This is a technicality: all the representation
245 -- type constructors operate with kind @* -> *@ as base kind. But the type argument is never
246 -- being used. This may be changed at some point in the future. The class has a single method,
247 -- and we use the type we want our final function to have, but we replace the occurrences of
248 -- the generic type argument @a@ with @f p@ (where the @p@ is any argument; it will not be used).
249 --
250 -- > class Encode' f where
251 -- > encode' :: f p -> [Bool]
252 --
253 -- With the goal in mind to make @encode@ work on @Tree@ and other datatypes, we now define
254 -- instances for the representation type constructors 'V1', 'U1', ':+:', ':*:', 'K1', and 'M1'.
255
256 -- *** Definition of the generic representation types
257 --
258 -- |
259 --
260 -- In order to be able to do this, we need to know the actual definitions of these types:
261 --
262 -- @
263 -- data 'V1' p -- lifted version of Empty
264 -- data 'U1' p = 'U1' -- lifted version of ()
265 -- data (':+:') f g p = 'L1' (f p) | 'R1' (g p) -- lifted version of 'Either'
266 -- data (':*:') f g p = (f p) ':*:' (g p) -- lifted version of (,)
267 -- newtype 'K1' i c p = 'K1' { 'unK1' :: c } -- a container for a c
268 -- newtype 'M1' i t f p = 'M1' { 'unM1' :: f p } -- a wrapper
269 -- @
270 --
271 -- So, 'U1' is just the unit type, ':+:' is just a binary choice like 'Either',
272 -- ':*:' is a binary pair like the pair constructor @(,)@, and 'K1' is a value
273 -- of a specific type @c@, and 'M1' wraps a value of the generic type argument,
274 -- which in the lifted world is an @f p@ (where we do not care about @p@).
275
276 -- *** Generic instances
277 --
278 -- |
279 --
280 -- The instance for 'V1' is slightly awkward (but also rarely used):
281 --
282 -- @
283 -- instance Encode' 'V1' where
284 -- encode' x = undefined
285 -- @
286 --
287 -- There are no values of type @V1 p@ to pass (except undefined), so this is
288 -- actually impossible. One can ask why it is useful to define an instance for
289 -- 'V1' at all in this case? Well, an empty type can be used as an argument to
290 -- a non-empty type, and you might still want to encode the resulting type.
291 -- As a somewhat contrived example, consider @[Empty]@, which is not an empty
292 -- type, but contains just the empty list. The 'V1' instance ensures that we
293 -- can call the generic function on such types.
294 --
295 -- There is exactly one value of type 'U1', so encoding it requires no
296 -- knowledge, and we can use zero bits:
297 --
298 -- @
299 -- instance Encode' 'U1' where
300 -- encode' 'U1' = []
301 -- @
302 --
303 -- In the case for ':+:', we produce 'False' or 'True' depending on whether
304 -- the constructor of the value provided is located on the left or on the right:
305 --
306 -- @
307 -- instance (Encode' f, Encode' g) => Encode' (f ':+:' g) where
308 -- encode' ('L1' x) = False : encode' x
309 -- encode' ('R1' x) = True : encode' x
310 -- @
311 --
312 -- In the case for ':*:', we append the encodings of the two subcomponents:
313 --
314 -- @
315 -- instance (Encode' f, Encode' g) => Encode' (f ':*:' g) where
316 -- encode' (x ':*:' y) = encode' x ++ encode' y
317 -- @
318 --
319 -- The case for 'K1' is rather interesting. Here, we call the final function
320 -- 'encode' that we yet have to define, recursively. We will use another type
321 -- class 'Encode' for that function:
322 --
323 -- @
324 -- instance (Encode c) => Encode' ('K1' i c) where
325 -- encode' ('K1' x) = encode x
326 -- @
327 --
328 -- Note how 'Par0' and 'Rec0' both being mapped to 'K1' allows us to define
329 -- a uniform instance here.
330 --
331 -- Similarly, we can define a uniform instance for 'M1', because we completely
332 -- disregard all meta-information:
333 --
334 -- @
335 -- instance (Encode' f) => Encode' ('M1' i t f) where
336 -- encode' ('M1' x) = encode' x
337 -- @
338 --
339 -- Unlike in 'K1', the instance for 'M1' refers to 'encode'', not 'encode'.
340
341 -- *** The wrapper and generic default
342 --
343 -- |
344 --
345 -- We now define class 'Encode' for the actual 'encode' function:
346 --
347 -- @
348 -- class Encode a where
349 -- encode :: a -> [Bool]
350 -- default encode :: ('Generic' a) => a -> [Bool]
351 -- encode x = encode' ('from' x)
352 -- @
353 --
354 -- The incoming 'x' is converted using 'from', then we dispatch to the
355 -- generic instances using 'encode''. We use this as a default definition
356 -- for 'encode'. We need the 'default encode' signature because ordinary
357 -- Haskell default methods must not introduce additional class constraints,
358 -- but our generic default does.
359 --
360 -- Defining a particular instance is now as simple as saying
361 --
362 -- @
363 -- instance (Encode a) => Encode (Tree a)
364 -- @
365 --
366 #if 0
367 -- /TODO:/ Add usage example?
368 --
369 #endif
370 -- The generic default is being used. In the future, it will hopefully be
371 -- possible to use @deriving Encode@ as well, but GHC does not yet support
372 -- that syntax for this situation.
373 --
374 -- Having 'Encode' as a class has the advantage that we can define
375 -- non-generic special cases, which is particularly useful for abstract
376 -- datatypes that have no structural representation. For example, given
377 -- a suitable integer encoding function 'encodeInt', we can define
378 --
379 -- @
380 -- instance Encode Int where
381 -- encode = encodeInt
382 -- @
383
384 -- *** Omitting generic instances
385 --
386 -- |
387 --
388 -- It is not always required to provide instances for all the generic
389 -- representation types, but omitting instances restricts the set of
390 -- datatypes the functions will work for:
391 --
392 -- * If no ':+:' instance is given, the function may still work for
393 -- empty datatypes or datatypes that have a single constructor,
394 -- but will fail on datatypes with more than one constructor.
395 --
396 -- * If no ':*:' instance is given, the function may still work for
397 -- datatypes where each constructor has just zero or one field,
398 -- in particular for enumeration types.
399 --
400 -- * If no 'K1' instance is given, the function may still work for
401 -- enumeration types, where no constructor has any fields.
402 --
403 -- * If no 'V1' instance is given, the function may still work for
404 -- any datatype that is not empty.
405 --
406 -- * If no 'U1' instance is given, the function may still work for
407 -- any datatype where each constructor has at least one field.
408 --
409 -- An 'M1' instance is always required (but it can just ignore the
410 -- meta-information, as is the case for 'encode' above).
411 #if 0
412 -- *** Using meta-information
413 --
414 -- |
415 --
416 -- TODO
417 #endif
418 -- ** Generic constructor classes
419 --
420 -- |
421 --
422 -- Datatype-generic functions as defined above work for a large class
423 -- of datatypes, including parameterized datatypes. (We have used 'Tree'
424 -- as our example above, which is of kind @* -> *@.) However, the
425 -- 'Generic' class ranges over types of kind @*@, and therefore, the
426 -- resulting generic functions (such as 'encode') must be parameterized
427 -- by a generic type argument of kind @*@.
428 --
429 -- What if we want to define generic classes that range over type
430 -- constructors (such as 'Functor', 'Traversable', or 'Foldable')?
431
432 -- *** The 'Generic1' class
433 --
434 -- |
435 --
436 -- Like 'Generic', there is a class 'Generic1' that defines a
437 -- representation 'Rep1' and conversion functions 'from1' and 'to1',
438 -- only that 'Generic1' ranges over types of kind @* -> *@.
439 -- The 'Generic1' class is also derivable.
440 --
441 -- The representation 'Rep1' is ever so slightly different from 'Rep'.
442 -- Let us look at 'Tree' as an example again:
443 --
444 -- @
445 -- data Tree a = Leaf a | Node (Tree a) (Tree a)
446 -- deriving 'Generic1'
447 -- @
448 --
449 -- The above declaration causes the following representation to be generated:
450 --
451 -- instance 'Generic1' Tree where
452 -- type 'Rep1' Tree =
453 -- 'D1' D1Tree
454 -- ('C1' C1_0Tree
455 -- ('S1' 'NoSelector' 'Par1')
456 -- ':+:'
457 -- 'C1' C1_1Tree
458 -- ('S1' 'NoSelector' ('Rec1' Tree)
459 -- ':*:'
460 -- 'S1' 'NoSelector' ('Rec1' Tree)))
461 -- ...
462 --
463 -- The representation reuses 'D1', 'C1', 'S1' (and thereby 'M1') as well
464 -- as ':+:' and ':*:' from 'Rep'. (This reusability is the reason that we
465 -- carry around the dummy type argument for kind-@*@-types, but there are
466 -- already enough different names involved without duplicating each of
467 -- these.)
468 --
469 -- What's different is that we now use 'Par1' to refer to the parameter
470 -- (and that parameter, which used to be @a@), is not mentioned explicitly
471 -- by name anywhere; and we use 'Rec1' to refer to a recursive use of @Tree a@.
472
473 -- *** Representation of @* -> *@ types
474 --
475 -- |
476 --
477 -- Unlike 'Par0' and 'Rec0', the 'Par1' and 'Rec1' type constructors do not
478 -- map to 'K1'. They are defined directly, as follows:
479 --
480 -- @
481 -- newtype 'Par1' p = 'Par1' { 'unPar1' :: p } -- gives access to parameter p
482 -- newtype 'Rec1' f p = 'Rec1' { 'unRec1' :: f p } -- a wrapper
483 -- @
484 --
485 -- In 'Par1', the parameter @p@ is used for the first time, whereas 'Rec1' simply
486 -- wraps an application of @f@ to @p@.
487 --
488 -- Note that 'K1' (in the guise of 'Rec0') can still occur in a 'Rep1' representation,
489 -- namely when the datatype has a field that does not mention the parameter.
490 --
491 -- The declaration
492 --
493 -- @
494 -- data WithInt a = WithInt Int a
495 -- deriving 'Generic1'
496 -- @
497 --
498 -- yields
499 --
500 -- @
501 -- class 'Rep1' WithInt where
502 -- type 'Rep1' WithInt =
503 -- 'D1' D1WithInt
504 -- ('C1' C1_0WithInt
505 -- ('S1' 'NoSelector' ('Rec0' Int)
506 -- ':*:'
507 -- 'S1' 'NoSelector' 'Par1'))
508 -- @
509 --
510 -- If the parameter @a@ appears underneath a composition of other type constructors,
511 -- then the representation involves composition, too:
512 --
513 -- @
514 -- data Rose a = Fork a [Rose a]
515 -- @
516 --
517 -- yields
518 --
519 -- @
520 -- class 'Rep1' Rose where
521 -- type 'Rep1' Rose =
522 -- 'D1' D1Rose
523 -- ('C1' C1_0Rose
524 -- ('S1' 'NoSelector' 'Par1'
525 -- ':*:'
526 -- 'S1' 'NoSelector' ([] ':.:' 'Rec1' Rose)
527 -- @
528 --
529 -- where
530 --
531 -- @
532 -- newtype (':.:') f g p = 'Comp1' { 'unComp1' :: f (g p) }
533 -- @
534 #if 0
535 -- *** Limitations
536 --
537 -- |
538 --
539 -- /TODO/
540 --
541 -- /TODO:/ Also clear up confusion about 'Rec0' and 'Rec1' not really indicating recursion.
542 --
543 #endif
544 -----------------------------------------------------------------------------
545
546 -- * Generic representation types
547 V1, U1(..), Par1(..), Rec1(..), K1(..), M1(..)
548 , (:+:)(..), (:*:)(..), (:.:)(..)
549
550 -- ** Synonyms for convenience
551 , Rec0, Par0, R, P
552 , D1, C1, S1, D, C, S
553
554 -- * Meta-information
555 , Datatype(..), Constructor(..), Selector(..), NoSelector
556 , Fixity(..), Associativity(..), Arity(..), prec
557
558 -- * Generic type classes
559 , Generic(..), Generic1(..)
560
561 ) where
562
563 -- We use some base types
564 import GHC.Types
565 import Data.Maybe ( Maybe(..) )
566 import Data.Either ( Either(..) )
567
568 -- Needed for instances
569 import GHC.Classes ( Eq, Ord )
570 import GHC.Read ( Read )
571 import GHC.Show ( Show )
572 import Data.Proxy
573
574 --------------------------------------------------------------------------------
575 -- Representation types
576 --------------------------------------------------------------------------------
577
578 -- | Void: used for datatypes without constructors
579 data V1 p
580
581 -- | Unit: used for constructors without arguments
582 data U1 p = U1
583 deriving (Eq, Ord, Read, Show, Generic)
584
585 -- | Used for marking occurrences of the parameter
586 newtype Par1 p = Par1 { unPar1 :: p }
587 deriving (Eq, Ord, Read, Show, Generic)
588
589 -- | Recursive calls of kind * -> *
590 newtype Rec1 f p = Rec1 { unRec1 :: f p }
591 deriving (Eq, Ord, Read, Show, Generic)
592
593 -- | Constants, additional parameters and recursion of kind *
594 newtype K1 i c p = K1 { unK1 :: c }
595 deriving (Eq, Ord, Read, Show, Generic)
596
597 -- | Meta-information (constructor names, etc.)
598 newtype M1 i c f p = M1 { unM1 :: f p }
599 deriving (Eq, Ord, Read, Show, Generic)
600
601 -- | Sums: encode choice between constructors
602 infixr 5 :+:
603 data (:+:) f g p = L1 (f p) | R1 (g p)
604 deriving (Eq, Ord, Read, Show, Generic)
605
606 -- | Products: encode multiple arguments to constructors
607 infixr 6 :*:
608 data (:*:) f g p = f p :*: g p
609 deriving (Eq, Ord, Read, Show, Generic)
610
611 -- | Composition of functors
612 infixr 7 :.:
613 newtype (:.:) f g p = Comp1 { unComp1 :: f (g p) }
614 deriving (Eq, Ord, Read, Show, Generic)
615
616 -- | Tag for K1: recursion (of kind *)
617 data R
618 -- | Tag for K1: parameters (other than the last)
619 data P
620
621 -- | Type synonym for encoding recursion (of kind *)
622 type Rec0 = K1 R
623 -- | Type synonym for encoding parameters (other than the last)
624 type Par0 = K1 P
625 {-# DEPRECATED Par0 "Par0 is no longer used; use Rec0 instead" #-} -- deprecated in 7.6
626 {-# DEPRECATED P "P is no longer used; use R instead" #-} -- deprecated in 7.6
627
628 -- | Tag for M1: datatype
629 data D
630 -- | Tag for M1: constructor
631 data C
632 -- | Tag for M1: record selector
633 data S
634
635 -- | Type synonym for encoding meta-information for datatypes
636 type D1 = M1 D
637
638 -- | Type synonym for encoding meta-information for constructors
639 type C1 = M1 C
640
641 -- | Type synonym for encoding meta-information for record selectors
642 type S1 = M1 S
643
644
645 -- | Class for datatypes that represent datatypes
646 class Datatype d where
647 -- | The name of the datatype (unqualified)
648 datatypeName :: t d (f :: * -> *) a -> [Char]
649 -- | The fully-qualified name of the module where the type is declared
650 moduleName :: t d (f :: * -> *) a -> [Char]
651 -- | Marks if the datatype is actually a newtype
652 isNewtype :: t d (f :: * -> *) a -> Bool
653 isNewtype _ = False
654
655
656 -- | Class for datatypes that represent records
657 class Selector s where
658 -- | The name of the selector
659 selName :: t s (f :: * -> *) a -> [Char]
660
661 -- | Used for constructor fields without a name
662 data NoSelector
663
664 instance Selector NoSelector where selName _ = ""
665
666 -- | Class for datatypes that represent data constructors
667 class Constructor c where
668 -- | The name of the constructor
669 conName :: t c (f :: * -> *) a -> [Char]
670
671 -- | The fixity of the constructor
672 conFixity :: t c (f :: * -> *) a -> Fixity
673 conFixity _ = Prefix
674
675 -- | Marks if this constructor is a record
676 conIsRecord :: t c (f :: * -> *) a -> Bool
677 conIsRecord _ = False
678
679
680 -- | Datatype to represent the arity of a tuple.
681 data Arity = NoArity | Arity Int
682 deriving (Eq, Show, Ord, Read, Generic)
683
684 -- | Datatype to represent the fixity of a constructor. An infix
685 -- | declaration directly corresponds to an application of 'Infix'.
686 data Fixity = Prefix | Infix Associativity Int
687 deriving (Eq, Show, Ord, Read, Generic)
688
689 -- | Get the precedence of a fixity value.
690 prec :: Fixity -> Int
691 prec Prefix = 10
692 prec (Infix _ n) = n
693
694 -- | Datatype to represent the associativity of a constructor
695 data Associativity = LeftAssociative
696 | RightAssociative
697 | NotAssociative
698 deriving (Eq, Show, Ord, Read, Generic)
699
700 -- | Representable types of kind *.
701 -- This class is derivable in GHC with the DeriveGeneric flag on.
702 class Generic a where
703 -- | Generic representation type
704 type Rep a :: * -> *
705 -- | Convert from the datatype to its representation
706 from :: a -> (Rep a) x
707 -- | Convert from the representation to the datatype
708 to :: (Rep a) x -> a
709
710
711 -- | Representable types of kind * -> *.
712 -- This class is derivable in GHC with the DeriveGeneric flag on.
713 class Generic1 f where
714 -- | Generic representation type
715 type Rep1 f :: * -> *
716 -- | Convert from the datatype to its representation
717 from1 :: f a -> (Rep1 f) a
718 -- | Convert from the representation to the datatype
719 to1 :: (Rep1 f) a -> f a
720
721
722 --------------------------------------------------------------------------------
723 -- Derived instances
724 --------------------------------------------------------------------------------
725 deriving instance Generic [a]
726 deriving instance Generic (Maybe a)
727 deriving instance Generic (Either a b)
728 deriving instance Generic Bool
729 deriving instance Generic Ordering
730 deriving instance Generic ()
731 deriving instance Generic ((,) a b)
732 deriving instance Generic ((,,) a b c)
733 deriving instance Generic ((,,,) a b c d)
734 deriving instance Generic ((,,,,) a b c d e)
735 deriving instance Generic ((,,,,,) a b c d e f)
736 deriving instance Generic ((,,,,,,) a b c d e f g)
737
738 deriving instance Generic1 []
739 deriving instance Generic1 Maybe
740 deriving instance Generic1 (Either a)
741 deriving instance Generic1 ((,) a)
742 deriving instance Generic1 ((,,) a b)
743 deriving instance Generic1 ((,,,) a b c)
744 deriving instance Generic1 ((,,,,) a b c d)
745 deriving instance Generic1 ((,,,,,) a b c d e)
746 deriving instance Generic1 ((,,,,,,) a b c d e f)
747
748 --------------------------------------------------------------------------------
749 -- Primitive representations
750 --------------------------------------------------------------------------------
751
752 -- Int
753 data D_Int
754 data C_Int
755
756 instance Datatype D_Int where
757 datatypeName _ = "Int"
758 moduleName _ = "GHC.Int"
759
760 instance Constructor C_Int where
761 conName _ = "" -- JPM: I'm not sure this is the right implementation...
762
763 instance Generic Int where
764 type Rep Int = D1 D_Int (C1 C_Int (S1 NoSelector (Rec0 Int)))
765 from x = M1 (M1 (M1 (K1 x)))
766 to (M1 (M1 (M1 (K1 x)))) = x
767
768
769 -- Float
770 data D_Float
771 data C_Float
772
773 instance Datatype D_Float where
774 datatypeName _ = "Float"
775 moduleName _ = "GHC.Float"
776
777 instance Constructor C_Float where
778 conName _ = "" -- JPM: I'm not sure this is the right implementation...
779
780 instance Generic Float where
781 type Rep Float = D1 D_Float (C1 C_Float (S1 NoSelector (Rec0 Float)))
782 from x = M1 (M1 (M1 (K1 x)))
783 to (M1 (M1 (M1 (K1 x)))) = x
784
785
786 -- Double
787 data D_Double
788 data C_Double
789
790 instance Datatype D_Double where
791 datatypeName _ = "Double"
792 moduleName _ = "GHC.Float"
793
794 instance Constructor C_Double where
795 conName _ = "" -- JPM: I'm not sure this is the right implementation...
796
797 instance Generic Double where
798 type Rep Double = D1 D_Double (C1 C_Double (S1 NoSelector (Rec0 Double)))
799 from x = M1 (M1 (M1 (K1 x)))
800 to (M1 (M1 (M1 (K1 x)))) = x
801
802
803 -- Char
804 data D_Char
805 data C_Char
806
807 instance Datatype D_Char where
808 datatypeName _ = "Char"
809 moduleName _ = "GHC.Base"
810
811 instance Constructor C_Char where
812 conName _ = "" -- JPM: I'm not sure this is the right implementation...
813
814 instance Generic Char where
815 type Rep Char = D1 D_Char (C1 C_Char (S1 NoSelector (Rec0 Char)))
816 from x = M1 (M1 (M1 (K1 x)))
817 to (M1 (M1 (M1 (K1 x)))) = x
818
819 deriving instance Generic (Proxy t)