testing-feat-0.4.0.3: Functional Enumeration of Algebraic Types

Safe HaskellNone
LanguageHaskell98

Test.Feat.Class

Contents

Description

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.

Synopsis

Documentation

class Typeable a => Enumerable a where Source

A class of functionally enumerable types

Methods

enumerate :: Enumerate a Source

This is the interface for defining an instance. When combining enumerations use shared instead and when accessing the data of enumerations use optimal.

Instances

Enumerable Bool Source 
Enumerable Char Source

Contains only ASCII characters

Enumerable Double Source

Not injective

Enumerable Float Source

Not injective

Enumerable Int Source 
Enumerable Int8 Source 
Enumerable Int16 Source 
Enumerable Int32 Source 
Enumerable Int64 Source 
Enumerable Integer Source 
Enumerable Ordering Source 
Enumerable Word Source 
Enumerable Word8 Source 
Enumerable Word16 Source 
Enumerable Word32 Source 
Enumerable Word64 Source 
Enumerable () Source 
Enumerable Printable Source 
Enumerable Unicode Source 
Enumerable a_12 => Enumerable [a_12] Source 
(Infinite a, Enumerable a) => Enumerable (Ratio a) Source

Not injective

Enumerable a_a1aW => Enumerable (Maybe a_a1aW) Source 
(Infinite a, Enumerable a) => Enumerable (NonZero a) Source 
Infinite a => Enumerable (Nat a) Source 
Enumerable a => Enumerable (NonEmpty a) Source 
(Enumerable a_acKx, Enumerable b_acKy) => Enumerable (Either a_acKx b_acKy) Source 
(Enumerable a_12, Enumerable b_13) => Enumerable (a_12, b_13) Source 
(Enumerable a, Enumerable b) => Enumerable (FreePair a b) Source 
(Enumerable a_12, Enumerable b_13, Enumerable c_14) => Enumerable (a_12, b_13, c_14) Source 
(Enumerable a_12, Enumerable b_13, Enumerable c_14, Enumerable d_15) => Enumerable (a_12, b_13, c_14, d_15) Source 
(Enumerable a_12, Enumerable b_13, Enumerable c_14, Enumerable d_15, Enumerable e_16) => Enumerable (a_12, b_13, c_14, d_15, e_16) Source 
(Enumerable a_12, Enumerable b_13, Enumerable c_14, Enumerable d_15, Enumerable e_16, Enumerable f_17) => Enumerable (a_12, b_13, c_14, d_15, e_16, f_17) Source 
(Enumerable a_12, Enumerable b_13, Enumerable c_14, Enumerable d_15, Enumerable e_16, Enumerable f_17, Enumerable g_18) => Enumerable (a_12, b_13, c_14, d_15, e_16, f_17, g_18) Source 

Building instances

nullary :: a -> Constructor a Source

For nullary constructors such as True and [].

unary :: Enumerable a => (a -> b) -> Constructor b Source

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 -> c Source

Uncurry a function (typically a constructor) to a function on free pairs.

consts :: [Constructor a] -> Enumerate a Source

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

shared :: Enumerable a => Enumerate a Source

Version of enumerate that ensures that the enumeration is shared between all accesses. Should always be used when combining enumerations.

optimal :: Enumerable a => Enumerate a Source

An optimal version of enumerate. Used by all library functions that access enumerated values (but not by combining functions). Library functions should ensure that optimal is not reevaluated.

Free pairs

newtype FreePair a b Source

A free pair constructor. The cost of constructing a free pair is equal to the sum of the costs of its components.

Constructors

Free 

Fields

free :: (a, b)
 

Instances

Deriving instances with template Haskell

deriveEnumerable :: Name -> Q [Dec] Source

Derive an instance of Enumberable with Template Haskell. To derive an instance for Enumerable A, just put this as a top level declaration in your module (with the TemplateHaskell extension enabled):

  deriveEnumerable ''A

deriveEnumerable' :: ConstructorDeriv -> Q [Dec] Source

Derive an instance of Enumberable with Template Haskell, with rules for some specific constructors