Safe Haskell | None |
---|---|
Language | Haskell2010 |
generics-eot
tries to be a library for datatype generic programming
that is easy to understand. "eot" stands for "eithers of tuples".
A tutorial on how to use generics-eot
can be found here:
Generics.Eot.Tutorial.
- class HasEot a where
- data Datatype = Datatype {}
- data Constructor = Constructor {}
- data Fields
- = Selectors [String]
- | NoSelectors Int
- | NoFields
- data Void
- class Generic a
- data Proxy t :: k -> * = Proxy
Documentation
An instance (
) allows us toHasEot
a
- convert values of an arbitrary algebraic datatype
a
to and from a generic representation (
) (seeEot
atoEot
andfromEot
). - extract meta information about the type
a
(seedatatype
).
Once an algebraic datatype has an instance for Generic
it
automatically gets one for HasEot
.
Eot
is a type level function that maps arbitrary ADTs to isomorphic
generic representations. Here's an example:
data Foo = A Int Bool | B String
would be mapped to:
Either (Int, (Bool, ())) (Either (String, ()) Void)
These representations follow these rules:
- The choice between constructors is mapped to right-nested
Either
s. - There's always a so-called end-marker
Void
. It's an invalid choice (andVoid
is uninhabited to make sure you don't accidentally create such a value). So e.g.data Foo = A
would be mapped toEither () Void
, and a type with no constructors is mapped toVoid
. - The fields of one constructor are mapped to right-nested tuples.
- Again there's always an end-marker, this time of type
()
. A constructor with three fieldsa
,b
,c
is mapped to(a, (b, (c, ())))
, one fielda
is mapped to(a, ())
, and no fields are mapped to()
(just the end-marker).
These rules (and the end-markers) are necessary to make sure generic functions know exactly which parts of the generic representation are field types and which parts belong to the generic skeleton.
Meta Information
Type for meta information about ADTs.
Datatype | |
|
data Constructor Source
Type that represents meta information about fields of one constructor.
Selectors [String] | Record constructor, containing the list of the selector names. |
NoSelectors Int | Constructor with fields, but without selector names. The argument gives the number of fields. |
NoFields | Constructor without fields. |
Void
Uninhabited type.
Useful Re-exports
class Generic a
Representable types of kind *. This class is derivable in GHC with the DeriveGeneric flag on.
Generic Bool | |
Generic Char | |
Generic Double | |
Generic Float | |
Generic Int | |
Generic Ordering | |
Generic () | |
Generic All | |
Generic Any | |
Generic Arity | |
Generic Fixity | |
Generic Associativity | |
Generic Void | |
Generic C | |
Generic NoSelectors | |
Generic Person | |
Generic B | |
Generic A | |
Generic [a] | |
Generic (U1 p) | |
Generic (Par1 p) | |
Generic (ZipList a) | |
Generic (Dual a) | |
Generic (Endo a) | |
Generic (Sum a) | |
Generic (Product a) | |
Generic (First a) | |
Generic (Last a) | |
Generic (Maybe a) | |
Generic (Either a b) | |
Generic (Rec1 f p) | |
Generic (a, b) | |
Generic (Const a b) | |
Generic (WrappedMonad m a) | |
Generic (Proxy * t) | |
Generic (K1 i c p) | |
Generic ((:+:) f g p) | |
Generic ((:*:) f g p) | |
Generic ((:.:) f g p) | |
Generic (a, b, c) | |
Generic (WrappedArrow a b c) | |
Generic (Alt k f a) | |
Generic (M1 i c f p) | |
Generic (a, b, c, d) | |
Generic (a, b, c, d, e) | |
Generic (a, b, c, d, e, f) | |
Generic (a, b, c, d, e, f, g) |
data Proxy t :: k -> *
A concrete, poly-kinded proxy type