generic-enumeration-0.1.0.1: Generically derived enumerations.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Enumeration.Generic

Description

This module provides a way to generically obtain every possible value of a type, provided the generic representation of the type is compatible.

Probably the main reason this is useful is, unlike the builtin Haskell deriving Enum capability, this package understands non-nullary data constructors. So you can, for instance, enumerate something like

data Foo = Foo Bool Bool deriving (Generic)

This module does not provide a way to manually provide an enumeration by instantiating a type class. Enumerations must be obtained generically. Therefore, it is not enough that your type be an instance of Generic. Any types which it references must also be instances of Generic.

In GHCI:

λ: :set +m
λ: :set prompt "λ: "
λ: :set prompt-cont "λ.. "
λ: :set -XDeriveGeneric
λ:
λ: :{
λ.. data Foo
λ..   = A Bar
λ..   | B
λ..   | C Bool
λ..   deriving (Show, Generic)
λ..
λ.. data Bar
λ..   = X
λ..   | Y
λ..   | Z
λ..   deriving (Show, Generic)
λ.. :}
λ:
λ: enumeration :: [Foo]
[A X,A Y,A Z,B,C False,C True]
λ:
Synopsis

Documentation

enumeration :: (Generic a, HasFirst (Rep a), HasSuccessor (Rep a)) => [a] Source #

Produce a list of every possible value.

predMay :: (Generic a, HasPredecessor (Rep a)) => a -> Maybe a Source #

Return the preceding value, if there is one.

succMay :: (Generic a, HasSuccessor (Rep a)) => a -> Maybe a Source #

Return the succeeding value, if there is one.

type HasPredecessor = GenericPred Source #

type HasSuccessor = GenericSucc Source #

type HasFirst = GenericFirst Source #