derive-enumerable-0.1.1.0: Generic instances for enumerating complex data types

CopyrightMaciej Goszczycki 2015
LicenseGPL-3
Maintainermgoszcz2@gmail.com
Stabilityexperimental
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

Data.Enumerable.Generic

Contents

Description

Enumerable provides a generalized equivalent to doing `[minBound..maxBound]` but on complex types.

([minBound..maxBound] :: [Word8]) == allEnums

Synopsis

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

Minimal complete definition

Nothing

Methods

per :: x -> (x, Bool) Source

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

Symmetric per. Only enumerate if the carry boolean is True

next :: x -> x Source

Next enumeration. Call per discarding the carry boolean

allNext :: x -> [x] Source

Return all enumerations of a starting value

class Defaults x where Source

Class for easier enumeration of multi-constructor types

Methods

defs :: [x] Source

Extra Default instances

class Default a where

A class for types with a default value.

Methods

def :: a

The default value for this type.

Instances

Default Bool

Missing Bool Default instance

Default Char

Default Char is NULL

Default Double 
Default Float 
Default Int 
Default Int8 
Default Int16 
Default Int32 
Default Int64 
Default Integer 
Default Ordering 
Default Word 
Default Word8 
Default Word16 
Default Word32 
Default Word64 
Default () 
Default All 
Default Any 
Default IntSet 
Default TimeLocale 
Default [a] 
Integral a => Default (Ratio a) 
Default a => Default (IO a) 
(Default a, RealFloat a) => Default (Complex a) 
Default a => Default (Dual a) 
Default (Endo a) 
Num a => Default (Sum a) 
Num a => Default (Product a) 
Default (First a) 
Default (Last a) 
Default (Maybe a) 
Default (IntMap v) 
Default (Set v) 
Default a => Default (Tree a) 
Default (Seq a) 
Default (DList a) 
Default r => Default (e -> r) 
(Default a, Default b) => Default (Either a b)

Default Either is Left (a false value just like Default Bool)

(Default a, Default b) => Default (a, b) 
Default (Map k v) 
(Default a, Default b, Default c) => Default (a, b, c) 
(Default a, Default b, Default c, Default d) => Default (a, b, c, d) 
(Default a, Default b, Default c, Default d, Default e) => Default (a, b, c, d, e) 
(Default a, Default b, Default c, Default d, Default e, Default f) => Default (a, b, c, d, e, f) 
(Default a, Default b, Default c, Default d, Default e, Default f, Default g) => Default (a, b, c, d, e, f, g)