QuickCheck-2.11.3: Automatic testing of Haskell programs

Safe HaskellSafe
LanguageHaskell98

Test.QuickCheck.Function

Description

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

Example of use:

>>> :{
>>> let prop :: Fun String Integer -> Bool
>>> prop (Fun _ f) = f "monkey" == f "banana" || f "banana" == f "elephant"
>>> :}
>>> quickCheck prop
*** Failed! Falsifiable (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

Documentation

data Fun a b Source #

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

(Show a, Show b) => Show (Fun a b) Source # 

Methods

showsPrec :: Int -> Fun a b -> ShowS #

show :: Fun a b -> String #

showList :: [Fun a b] -> ShowS #

(Function a, CoArbitrary a, Arbitrary b) => Arbitrary (Fun a b) Source # 

Methods

arbitrary :: Gen (Fun a b) Source #

shrink :: Fun a b -> [Fun a b] Source #

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

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

Alias to applyFun.

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

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

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

data a :-> c Source #

The type of possibly partial concrete functions

Instances

Functor ((:->) a) Source # 

Methods

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

(<$) :: a -> (a :-> b) -> a :-> a #

(Show a, Show b) => Show ((:->) a b) Source # 

Methods

showsPrec :: Int -> (a :-> b) -> ShowS #

show :: (a :-> b) -> String #

showList :: [a :-> b] -> ShowS #

(Function a, CoArbitrary a, Arbitrary b) => Arbitrary ((:->) a b) Source # 

Methods

arbitrary :: Gen (a :-> b) Source #

shrink :: (a :-> b) -> [a :-> b] Source #

class Function a where Source #

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.

Methods

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

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

Instances

Function Bool Source # 

Methods

function :: (Bool -> b) -> Bool :-> b Source #

Function Char Source # 

Methods

function :: (Char -> b) -> Char :-> b Source #

Function Double Source # 

Methods

function :: (Double -> b) -> Double :-> b Source #

Function Float Source # 

Methods

function :: (Float -> b) -> Float :-> b Source #

Function Int Source # 

Methods

function :: (Int -> b) -> Int :-> b Source #

Function Int8 Source # 

Methods

function :: (Int8 -> b) -> Int8 :-> b Source #

Function Int16 Source # 

Methods

function :: (Int16 -> b) -> Int16 :-> b Source #

Function Int32 Source # 

Methods

function :: (Int32 -> b) -> Int32 :-> b Source #

Function Int64 Source # 

Methods

function :: (Int64 -> b) -> Int64 :-> b Source #

Function Integer Source # 

Methods

function :: (Integer -> b) -> Integer :-> b Source #

Function Ordering Source # 

Methods

function :: (Ordering -> b) -> Ordering :-> b Source #

Function Word Source # 

Methods

function :: (Word -> b) -> Word :-> b Source #

Function Word8 Source # 

Methods

function :: (Word8 -> b) -> Word8 :-> b Source #

Function Word16 Source # 

Methods

function :: (Word16 -> b) -> Word16 :-> b Source #

Function Word32 Source # 

Methods

function :: (Word32 -> b) -> Word32 :-> b Source #

Function Word64 Source # 

Methods

function :: (Word64 -> b) -> Word64 :-> b Source #

Function () Source # 

Methods

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

Function IntSet Source # 

Methods

function :: (IntSet -> b) -> IntSet :-> b Source #

Function OrdC Source # 

Methods

function :: (OrdC -> b) -> OrdC :-> b Source #

Function OrdB Source # 

Methods

function :: (OrdB -> b) -> OrdB :-> b Source #

Function OrdA Source # 

Methods

function :: (OrdA -> b) -> OrdA :-> b Source #

Function C Source # 

Methods

function :: (C -> b) -> C :-> b Source #

Function B Source # 

Methods

function :: (B -> b) -> B :-> b Source #

Function A Source # 

Methods

function :: (A -> b) -> A :-> b Source #

Function a => Function [a] Source # 

Methods

function :: ([a] -> b) -> [a] :-> b Source #

Function a => Function (Maybe a) Source # 

Methods

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

(Integral a, Function a) => Function (Ratio a) Source # 

Methods

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

(RealFloat a, Function a) => Function (Complex a) Source # 

Methods

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

HasResolution a => Function (Fixed a) Source # 

Methods

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

Function a => Function (IntMap a) Source # 

Methods

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

Function a => Function (Seq a) Source # 

Methods

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

(Ord a, Function a) => Function (Set a) Source # 

Methods

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

(Function a, Function b) => Function (Either a b) Source # 

Methods

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

(Function a, Function b) => Function (a, b) Source # 

Methods

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

(Ord a, Function a, Function b) => Function (Map a b) Source # 

Methods

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

(Function a, Function b, Function c) => Function (a, b, c) Source # 

Methods

function :: ((a, b, c) -> b) -> (a, b, c) :-> b Source #

(Function a, Function b, Function c, Function d) => Function (a, b, c, d) Source # 

Methods

function :: ((a, b, c, d) -> b) -> (a, b, c, d) :-> b Source #

(Function a, Function b, Function c, Function d, Function e) => Function (a, b, c, d, e) Source # 

Methods

function :: ((a, b, c, d, e) -> b) -> (a, b, c, d, e) :-> b Source #

(Function a, Function b, Function c, Function d, Function e, Function f) => Function (a, b, c, d, e, f) Source # 

Methods

function :: ((a, b, c, d, e, f) -> b) -> (a, b, c, d, e, f) :-> b Source #

(Function a, Function b, Function c, Function d, Function e, Function f, Function g) => Function (a, b, c, d, e, f, g) Source # 

Methods

function :: ((a, b, c, d, e, f, g) -> b) -> (a, b, c, d, e, f, g) :-> b Source #

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

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

Provides a Function instance for types with Show and Read.

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

Provides a Function instance for types with Integral.

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

Provides a Function instance for types with RealFrac.

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

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

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

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

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

A modifier for testing ternary functions.