Copyright | Maciej Goszczycki 2015 |
---|---|
License | GPL-3 |
Maintainer | mgoszcz2@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Enumerable
provides a generalized equivalent to doing `[minBound..maxBound]` but on complex types.
([minBound..maxBound] :: [Word8]) == allEnums
- allEnum :: (Default x, Enumerable x) => [x]
- allConstEnum :: Enumerable x => [x] -> [x]
- allDefsEnum :: (Defaults x, Enumerable x) => [x]
- defEnumerable :: (Eq x, Enum x, Bounded x) => x -> (x, Bool)
- class Enumerable x where
- class Defaults x where
- defs :: [x]
- class Default a where
- def :: a
Example usage
λ :set -XDeriveGeneric λ data Flag = Flag Bool Word8 deriving (Show, Generic) λ instance Enumerable Flag λ instance Default Flag where def = Flag False 0 λ allEnum :: [Flag] [Flag False 0,Flag True 0,Flag False 1,Flag True 1, (..snip..) Flag False 255,Flag True 255]
Multi-consturctor types
λ data Value = A Bool | B | C deriving (Show, Generic) λ instance Enumerable Value λ allConstEnum [A False, B, C] [A False,A True,B,C] λ instance Defaults Value where defs = [A False, B, C] λ allDefsEnum :: [Value] [A False,A True,B,C]
Without Default
instances
λ next False True λ next $ 8086 :: Int 8087 λ next $ Flag True 42 Flag False 43
Caveats & Extending
Defaults
class is just a way of providing default arguments to allConstEnum
Elegantly handling multi-constructor types with type-variables and implementing
the corresponding Enumerable
instance is left to the user. Implementations for Maybe
and Either
and provided out of the box
Convenience functions
allEnum :: (Default x, Enumerable x) => [x] Source
Generate all possible variations of a type
allConstEnum :: Enumerable x => [x] -> [x] Source
Generate all possible variations of a type given a list of default values of constructors
allDefsEnum :: (Defaults x, Enumerable x) => [x] Source
Same as allConstEnum
but automatically get constructors from Defaults
class
defEnumerable :: (Eq x, Enum x, Bounded x) => x -> (x, Bool) Source
Default implementation of per
used by all simple Enumerable
instances
that are also instances of Eq
, Enum
and Bounded
Type-classes
class Enumerable x where Source
Class for generating enumerations of arbitrary data types
Nothing
Minimum definition of Enumerable
. Due to haskell's inability to
deal with overlapping instances, only defined by GEnumerable
derived types
Given a value returns next enumeration and a carry boolean, set to True
if returning final enumeration
sper :: (x, Bool) -> (x, Bool) Source
Next enumeration. Call per
discarding the carry boolean
Return all enumerations of a starting value
Enumerable Bool | |
Enumerable Char | |
Enumerable Int | |
Enumerable Int8 | |
Enumerable Int16 | |
Enumerable Int32 | |
Enumerable Int64 | |
Enumerable Ordering | |
Enumerable Word | |
Enumerable Word8 | |
Enumerable Word16 | |
Enumerable Word32 | |
Enumerable Word64 | |
Enumerable () | |
(Default x, Enumerable x) => Enumerable (Maybe x) | |
(Default a, Default b, Enumerable a, Enumerable b) => Enumerable (Either a b) | |
(Enumerable a, Enumerable b) => Enumerable (a, b) | Enumerates left followed by right |
Class for easier enumeration of multi-constructor types
Extra Default
instances
class Default a where
A class for types with a default value.
def :: a
The default value for this type.