QuickCheck-2.14.3: Automatic testing of Haskell programs

Safe Haskell | Safe
Language | Haskell2010

Test.QuickCheck.Function

Description

Generation of random shrinkable, showable functions. See the paper "Shrinking and showing functions" by Koen Claessen.

Note: most of the contents of this module are re-exported by Test.QuickCheck. You probably do not need to import it directly.

Example of use:

    >>> :{
    >>> let prop :: Fun String Integer -> Bool
    >>> prop (Fun _ f) = f "monkey" == f "banana" || f "banana" == f "elephant"
    >>> :}
    >>> quickCheck prop
    *** Failed! Falsified (after 3 tests and 134 shrinks):
    {"elephant"->1, "monkey"->1, _->0}

To generate random values of type ``Fun` a b`, you must have an instance ``Function` a`. If your type has a `Show` instance, you can use `functionShow` to write the instance; otherwise, use `functionMap` to give a bijection between your type and a type that is already an instance of `Function`. See the ``Function` [a]` instance for an example of the latter.

Synopsis

- data Fun a b = Fun (a :-> b, b, Shrunk) (a -> b)
- applyFun :: Fun a b -> a -> b
- apply :: Fun a b -> a -> b
- applyFun2 :: Fun (a, b) c -> a -> b -> c
- applyFun3 :: Fun (a, b, c) d -> a -> b -> c -> d
- data a :-> c
- class Function a where
  - function :: (a -> b) -> a :-> b
- functionMap :: Function b => (a -> b) -> (b -> a) -> (a -> c) -> a :-> c
- functionShow :: (Show a, Read a) => (a -> c) -> a :-> c
- functionIntegral :: Integral a => (a -> b) -> a :-> b
- functionRealFrac :: RealFrac a => (a -> b) -> a :-> b
- functionBoundedEnum :: (Eq a, Bounded a, Enum a) => (a -> b) -> a :-> b
- functionElements :: Eq a => [a] -> (a -> b) -> a :-> b
- functionVoid :: (forall b. void -> b) -> void :-> c
- functionMapWith :: ((b -> c) -> b :-> c) -> (a -> b) -> (b -> a) -> (a -> c) -> a :-> c
- functionEitherWith :: ((a -> c) -> a :-> c) -> ((b -> c) -> b :-> c) -> (Either a b -> c) -> Either a b :-> c
- functionPairWith :: ((a -> b -> c) -> a :-> (b -> c)) -> ((b -> c) -> b :-> c) -> ((a, b) -> c) -> (a, b) :-> c
- pattern Fn :: (a -> b) -> Fun a b
- pattern Fn2 :: (a -> b -> c) -> Fun (a, b) c
- pattern Fn3 :: (a -> b -> c -> d) -> Fun (a, b, c) d

# Documentation

data Fun a b #

Generation of random shrinkable, showable functions.

To generate random values of type ``Fun` a b`, you must have an instance ``Function` a`.

See also `applyFun`, and `Fn` with GHC >= 7.8.

Constructors

Fun (a :-> b, b, Shrunk) (a -> b) |

#### Instances

Instances details

Functor (Fun a) # |
Instance details Defined in Test.QuickCheck.Function Methods fmap :: (a0 -> b) -> Fun a a0 -> Fun a b (<$) :: a0 -> Fun a b -> Fun a a0
(Function a, CoArbitrary a, Arbitrary b) => Arbitrary (Fun a b) # |
Instance details Defined in Test.QuickCheck.Function Methods arbitrary :: Gen (Fun a b) # shrink :: Fun a b -> [Fun a b] #
(Show a, Show b) => Show (Fun a b) # |
Instance details Defined in Test.QuickCheck.Function Methods showsPrec :: Int -> Fun a b -> ShowS show :: Fun a b -> String showList :: [Fun a b] -> ShowS

applyFun :: Fun a b -> a -> b #

Extracts the value of a function.

`Fn` is the pattern equivalent of this function.

    prop :: Fun String Integer -> Bool
    prop f = applyFun f "banana" == applyFun f "monkey"
          || applyFun f "banana" == applyFun f "elephant"

apply :: Fun a b -> a -> b #

Alias to `applyFun`.

applyFun2 :: Fun (a, b) c -> a -> b -> c #

Extracts the value of a binary function.

`Fn2` is the pattern equivalent of this function.

    prop_zipWith :: Fun (Int, Bool) Char -> [Int] -> [Bool] -> Bool
    prop_zipWith f xs ys = zipWith (applyFun2 f) xs ys == [ applyFun2 f x y | (x, y) <- zip xs ys]

applyFun3 :: Fun (a, b, c) d -> a -> b -> c -> d #

Extracts the value of a ternary function. `Fn3` is the pattern equivalent of this function.

data a :-> c #

The type of possibly partial concrete functions

#### Instances

Instances details

Functor ((:->) a) # |
Instance details Defined in Test.QuickCheck.Function Methods fmap :: (a0 -> b) -> (a :-> a0) -> a :-> b (<$) :: a0 -> (a :-> b) -> a :-> a0
(Function a, CoArbitrary a, Arbitrary b) => Arbitrary (a :-> b) # |
Instance details Defined in Test.QuickCheck.Function Methods arbitrary :: Gen (a :-> b) # shrink :: (a :-> b) -> [a :-> b] #
(Show a, Show b) => Show (a :-> b) # |
Instance details Defined in Test.QuickCheck.Function Methods showsPrec :: Int -> (a :-> b) -> ShowS show :: (a :-> b) -> String showList :: [a :-> b] -> ShowS

class Function a where #

The class `Function a` is used for random generation of showable functions of type `a -> b`.

There is a default implementation for `function`, which you can use if your type has structural equality. Otherwise, you can normally use `functionMap` or `functionShow`.

Minimal complete definition

Nothing

Methods

function :: (a -> b) -> a :-> b #

default function :: (Generic a, GFunction (Rep a)) => (a -> b) -> a :-> b #

#### Instances

Instances details

Function A # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (A -> b) -> A :-> b #
Function B # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (B -> b) -> B :-> b #
Function C # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (C -> b) -> C :-> b #
Function OrdA # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (OrdA -> b) -> OrdA :-> b #
Function OrdB # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (OrdB -> b) -> OrdB :-> b #
Function OrdC # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (OrdC -> b) -> OrdC :-> b #
Function All # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (All -> b) -> All :-> b #
Function Any # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Any -> b) -> Any :-> b #
Function Newline # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Newline -> b) -> Newline :-> b #
Function NewlineMode # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (NewlineMode -> b) -> NewlineMode :-> b #
Function Int16 # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Int16 -> b) -> Int16 :-> b #
Function Int32 # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Int32 -> b) -> Int32 :-> b #
Function Int64 # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Int64 -> b) -> Int64 :-> b #
Function Int8 # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Int8 -> b) -> Int8 :-> b #
Function Word16 # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Word16 -> b) -> Word16 :-> b #
Function Word32 # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Word32 -> b) -> Word32 :-> b #
Function Word64 # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Word64 -> b) -> Word64 :-> b #
Function Word8 # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Word8 -> b) -> Word8 :-> b #
Function IntSet # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (IntSet -> b) -> IntSet :-> b #
Function Ordering # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Ordering -> b) -> Ordering :-> b #
Function Integer # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Integer -> b) -> Integer :-> b #
Function () # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (() -> b) -> () :-> b #
Function Bool # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Bool -> b) -> Bool :-> b #
Function Char # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Char -> b) -> Char :-> b #
Function Double # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Double -> b) -> Double :-> b #
Function Float # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Float -> b) -> Float :-> b #
Function Int # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Int -> b) -> Int :-> b #
Function Word # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Word -> b) -> Word :-> b #
(RealFloat a, Function a) => Function (Complex a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Complex a -> b) -> Complex a :-> b #
Function a => Function (Identity a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Identity a -> b) -> Identity a :-> b #
Function a => Function (First a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (First a -> b) -> First a :-> b #
Function a => Function (Last a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Last a -> b) -> Last a :-> b #
Function a => Function (Dual a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Dual a -> b) -> Dual a :-> b #
Function a => Function (Product a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Product a -> b) -> Product a :-> b #
Function a => Function (Sum a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Sum a -> b) -> Sum a :-> b #
(Integral a, Function a) => Function (Ratio a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Ratio a -> b) -> Ratio a :-> b #
Function a => Function (IntMap a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (IntMap a -> b) -> IntMap a :-> b #
Function a => Function (Seq a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Seq a -> b) -> Seq a :-> b #
(Ord a, Function a) => Function (Set a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Set a -> b) -> Set a :-> b #
Function a => Function (Tree a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Tree a -> b) -> Tree a :-> b #
Function a => Function (Maybe a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Maybe a -> b) -> Maybe a :-> b #
Function a => Function [a] # |
Instance details Defined in Test.QuickCheck.Function Methods function :: ([a] -> b) -> [a] :-> b #
(Function a, Function b) => Function (Either a b) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Either a b -> b0) -> Either a b :-> b0 #
HasResolution a => Function (Fixed a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Fixed a -> b) -> Fixed a :-> b #
(Ord a, Function a, Function b) => Function (Map a b) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Map a b -> b0) -> Map a b :-> b0 #
(Function a, Function b) => Function (a, b) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: ((a, b) -> b0) -> (a, b) :-> b0 #
Function a => Function (Const a b) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Const a b -> b0) -> Const a b :-> b0 #
Function (f a) => Function (Alt f a) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: (Alt f a -> b) -> Alt f a :-> b #
(Function a, Function b, Function c) => Function (a, b, c) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: ((a, b, c) -> b0) -> (a, b, c) :-> b0 #
(Function a, Function b, Function c, Function d) => Function (a, b, c, d) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: ((a, b, c, d) -> b0) -> (a, b, c, d) :-> b0 #
(Function a, Function b, Function c, Function d, Function e) => Function (a, b, c, d, e) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: ((a, b, c, d, e) -> b0) -> (a, b, c, d, e) :-> b0 #
(Function a, Function b, Function c, Function d, Function e, Function f) => Function (a, b, c, d, e, f) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: ((a, b, c, d, e, f) -> b0) -> (a, b, c, d, e, f) :-> b0 #
(Function a, Function b, Function c, Function d, Function e, Function f, Function g) => Function (a, b, c, d, e, f, g) # |
Instance details Defined in Test.QuickCheck.Function Methods function :: ((a, b, c, d, e, f, g) -> b0) -> (a, b, c, d, e, f, g) :-> b0 #

functionMap :: Function b => (a -> b) -> (b -> a) -> (a -> c) -> a :-> c #

The basic building block for `Function` instances. Provides a `Function` instance by mapping to and from a type that already has a `Function` instance.

functionShow :: (Show a, Read a) => (a -> c) -> a :-> c #

Provides a `Function` instance for types with `Show` and `Read`.

functionIntegral :: Integral a => (a -> b) -> a :-> b #

Provides a `Function` instance for types with `Integral`.

functionRealFrac :: RealFrac a => (a -> b) -> a :-> b #

Provides a `Function` instance for types with `RealFrac`.

functionBoundedEnum :: (Eq a, Bounded a, Enum a) => (a -> b) -> a :-> b #

Provides a `Function` instance for types with `Bounded` and `Enum`. Use only for small types (i.e. not integers): creates the list `[`minBound`..`maxBound`]`!

functionElements :: Eq a => [a] -> (a -> b) -> a :-> b #

Provides a `Function` instance for small finite types.

functionVoid :: (forall b. void -> b) -> void :-> c #

Provides a `Function` instance for types isomorphic to `Void`.

An actual ``Function` `Void`` instance is defined in `quickcheck-instances`.

functionMapWith :: ((b -> c) -> b :-> c) -> (a -> b) -> (b -> a) -> (a -> c) -> a :-> c #

Since: 2.13.3

functionEitherWith :: ((a -> c) -> a :-> c) -> ((b -> c) -> b :-> c) -> (Either a b -> c) -> Either a b :-> c #

Since: 2.13.3

functionPairWith :: ((a -> b -> c) -> a :-> (b -> c)) -> ((b -> c) -> b :-> c) -> ((a, b) -> c) -> (a, b) :-> c #

Since: 2.13.3

pattern Fn :: (a -> b) -> Fun a b #

A modifier for testing functions.

    prop :: Fun String Integer -> Bool
    prop (Fn f) = f "banana" == f "monkey"
               || f "banana" == f "elephant"

pattern Fn2 :: (a -> b -> c) -> Fun (a, b) c #

A modifier for testing binary functions.

    prop_zipWith :: Fun (Int, Bool) Char -> [Int] -> [Bool] -> Bool
    prop_zipWith (Fn2 f) xs ys = zipWith f xs ys == [ f x y | (x, y) <- zip xs ys]

pattern Fn3 :: (a -> b -> c -> d) -> Fun (a, b, c) d #

A modifier for testing ternary functions.