Safe Haskell | None |
---|
Everything you need to construct an enumeration for an algebraic type. Just define each constructor using pure for nullary constructors and unary and funcurry for positive arity constructors, then combine the constructors with consts. Example:
instance Enumerable a => Enumerable [a] where enumerate = consts [unary (funcurry (:)), pure []]
There's also a handy Template Haskell function for automatic derivation.
- class Typeable a => Enumerable a where
- type Constructor = Enumerate
- nullary :: a -> Constructor a
- unary :: Enumerable a => (a -> b) -> Constructor b
- funcurry :: (a -> b -> c) -> FreePair a b -> c
- consts :: [Constructor a] -> Enumerate a
- optimised :: Enumerable a => Enumerate a
- newtype FreePair a b = Free {
- free :: (a, b)
- deriveEnumerable :: Name -> Q [Dec]
Documentation
class Typeable a => Enumerable a whereSource
A class of functionally enumerable types
enumerate :: Enumerate aSource
This is the interface for defining an instance. Memoisation needs to
be ensured e.g. using mempay
but sharing is handled automatically by
the default implementation of shared
.
Version of enumerate that ensures it is shared between all accessing functions. Should alwasy be used when combining enumerations. Should typically be left to default behaviour.
Building instances
type Constructor = EnumerateSource
nullary :: a -> Constructor aSource
For nullary constructors such as True
and []
.
unary :: Enumerable a => (a -> b) -> Constructor bSource
For any non-nullary constructor. Apply funcurry
until the type of
the result is unary (i.e. n-1 times where n is the number of fields
of the constructor).
funcurry :: (a -> b -> c) -> FreePair a b -> cSource
Uncurry a function (typically a constructor) to a function on free pairs.
consts :: [Constructor a] -> Enumerate aSource
Produces the enumeration of a type given the enumerators for each of its
constructors. The result of unary
should typically not be used
directly in an instance even if it only has one constructor. So you
should apply consts even in that case.
Accessing the enumerator of an instance
optimised :: Enumerable a => Enumerate aSource
An optimised version of enumerate. Used by all
library functions that access enumerated values (but not
by combining functions). Library functions should ensure that
optimised
is not reevaluated.
Free pairs
A free pair constructor. The cost of constructing a free pair is equal to the sum of the costs of its components.
Typeable2 FreePair | |
(Show a, Show b) => Show (FreePair a b) | |
(Enumerable a, Enumerable b) => Enumerable (FreePair a b) |
Deriving instances with template haskell
deriveEnumerable :: Name -> Q [Dec]Source
Derive an instance of Enumberable with Template Haskell.