leancheck-0.3.0: Cholesterol-free property-based testing

Safe HaskellNone
LanguageHaskell2010

Test.Check.ShowFunction

Contents

Description

This module exports the ShowFunction typeclass, its instances and related functions.

Using this module, it is possible to implement a Show instance for functions:

import Test.Check.ShowFunction
instance (Show a, Listable a, ShowFunction b) => Show (a->b) where
  show = showFunction 8

This shows functions as a case pattern with up to 8 cases.

The module Test.Check.Function.Show (Show) exports an instance like the one above.

Synopsis

Documentation

showFunction :: ShowFunction a => Int -> a -> String Source

Given a number of patterns to show, shows a ShowFunction value.

showFunction undefined True == "True"
showFunction 3 (id::Int) == "\\x -> case x of\n\
                             \        0 -> 0\n\
                             \        1 -> 1\n\
                             \        -1 -> -1\n\
                             \        ...\n"
showFunction 4 (&&) == "\\x y -> case (x,y) of\n\
                        \          (False,False) -> False\n\
                        \          (False,True) -> False\n\
                        \          (True,False) -> False\n\
                        \          (True,True) -> True\n"

This can be used as an implementation of show for functions:

instance (Show a, Listable a, ShowFunction b) => Show (a->b) where
  show = showFunction 8

showFunctionLine :: ShowFunction a => Int -> a -> String Source

Same as showFunction, but has no line breaks.

showFunction 2 (id::Int) == "\\x -> case x of 0 -> 0; 1 -> 1; ..."

type Binding = ([String], Maybe String) Source

A functional binding in a showable format.

bindings :: ShowFunction a => a -> [Binding] Source

Given a ShowFunction value, return a list of bindings for printing. Examples:

bindings True == [([],True)]
bindings (id::Int) == [(["0"],"0"), (["1"],"1"), (["-1"],"-1"), ...
bindings (&&) == [ (["False","False"], "False")
                 , (["False","True"], "False")
                 , (["True","False"], "False")
                 , (["True","True"], "True")
                 ]

class ShowFunction a where Source

ShowFunction values are those for which we can return a list of functional bindings.

As a user, you probably want showFunction and showFunctionLine.

Non functional instances should be defined by:

instance ShowFunction Ty where tBindings = tBindingsShow

Methods

tBindings :: a -> [[Binding]] Source

Instances

ShowFunction Bool Source 
ShowFunction Char Source 
ShowFunction Int Source 
ShowFunction () Source 
Show a => ShowFunction [a] Source 
Show a => ShowFunction (Maybe a) Source 
(Show a, Listable a, ShowFunction b) => ShowFunction (a -> b) Source 
(Show a, Show b) => ShowFunction (a, b) Source 
(Show a, Show b, Show c) => ShowFunction (a, b, c) Source 
(Show a, Show b, Show c, Show d) => ShowFunction (a, b, c, d) Source 
(Show a, Show b, Show c, Show d, Show e) => ShowFunction (a, b, c, d, e) Source 
(Show a, Show b, Show c, Show d, Show e, Show f) => ShowFunction (a, b, c, d, e, f) Source 
(Show a, Show b, Show c, Show d, Show e, Show f, Show g) => ShowFunction (a, b, c, d, e, f, g) Source 
(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h) => ShowFunction (a, b, c, d, e, f, g, h) Source 

tBindingsShow :: Show a => a -> [[Binding]] Source

A default implementation of tBindings for already Show-able types.

Re-exports

class Listable a Source

A type is Listable when there exists a function that is able to list (ideally all of) its values.

Ideally, this type should be defined by a tiers function that returns a (possibly infinite) list of finite sub-lists (tiers): the first sub-list contains elements of size 0, the second sub-list contains elements of size 1 and so on. Size here is defined by the implementor of the type-class instance.

For algebraic data types, the general form for tiers is:

tiers = consN ConstructorA
     \/ consN ConstructorB
     \/ consN ConstructorC
     \/ ...

When defined by list, each sub-list in tiers is a singleton list (each element of list has +1 size).

The function deriveListable from Test.Check.Derive can automatically derive instances of this typeclass.

A Listable instance for functions is also available but is not exported by default. Import Test.Check.Function for that. (Test.Check.Function.Show for a Show instance for functions)

Minimal complete definition

list | tiers