Modulesmallcheck-1.2.1.1Haskell2010
Test.SmallCheck.Series
You need this module if you want to generate test values of your own types.
You'll typically need the following extensions:
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}SmallCheck itself defines data generators for all the data types used by the Prelude.
In order to generate values and functions of your own types, you need to make them instances of Serial (for values) and CoSerial (for functions). There are two main ways to do so: using Generics or writing the instances by hand.
- 6 types
- 2 classes
- 32 values
- Packagesmallcheck-1.2.1.1
- Exports41
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceSeries.hs
Generic instances
0 declarationsThe easiest way to create the necessary instances is to use GHC generics (available starting with GHC 7.2.1).
Here's a complete example:
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}
import Test.SmallCheck.Series
import GHC.Generics
data Tree a = Null | Fork (Tree a) a (Tree a)
deriving Generic
instance Serial m a => Serial m (Tree a)Here we enable the DeriveGeneric extension which allows to derive Generic
instance for our data type. Then we declare that Tree a is an instance of
Serial, but do not provide any definitions. This causes GHC to use the
default definitions that use the Generic instance.
One minor limitation of generic instances is that there's currently no way to distinguish newtypes and datatypes. Thus, newtype constructors will also count as one level of depth.
Data Generators
0 declarationsWriting Serial instances for application-specific types is
straightforward. You need to define a series generator, typically using
consN family of generic combinators where N is constructor arity.
For example:
data Tree a = Null | Fork (Tree a) a (Tree a)
instance Serial m a => Serial m (Tree a) where
series = cons0 Null \/ cons3 ForkFor newtypes use newtypeCons instead of cons1. The difference is that cons1 is counts as one level of depth, while newtypeCons doesn't affect the depth.
newtype Light a = Light a
instance Serial m a => Serial m (Light a) where
series = newtypeCons LightFor data types with more than 6 fields define consN as
consN f = decDepth $
f <$> series
<~> series
<~> series
<~> ... {- series repeated N times in total -}What does consN do, exactly?
consN has type
(Serial t₁, ..., Serial tₙ) => (t₁ -> ... -> tₙ -> t) -> Series t.
consN f is a series which, for a given depth d > 0, produces values of the
form
f x₁ ... xₙwhere xₖ ranges over all values of type tₖ of depth up to d-1
(as defined by the series functions for tₖ).
consN functions also ensure that xₖ are enumerated in the
breadth-first order. Thus, combinations of smaller depth come first
(assuming the same is true for tₖ).
If d \le 0, no values are produced.
Same as cons1, but preserves the depth.
Function Generators
0 declarationsTo generate functions of an application-specific argument type, make the type an instance of CoSerial.
Again there is a standard pattern, this time using the altsN
combinators where again N is constructor arity. Here are Tree and
Light instances:
instance CoSerial m a => CoSerial m (Tree a) where
coseries rs =
alts0 rs >>- \z ->
alts3 rs >>- \f ->
return $ \t ->
case t of
Null -> z
Fork t1 x t2 -> f t1 x t2instance CoSerial m a => CoSerial m (Light a) where
coseries rs =
newtypeAlts rs >>- \f ->
return $ \l ->
case l of
Light x -> f xFor data types with more than 6 fields define altsN as
altsN rs = do
rs <- fixDepth rs
decDepthChecked
(constM $ constM $ ... $ constM rs)
(coseries $ coseries $ ... $ coseries rs)
{- constM and coseries are repeated N times each -}What does altsN do, exactly?
altsN has type
(Serial t₁, ..., Serial tₙ) => Series t -> Series (t₁ -> ... -> tₙ -> t).
altsN s is a series which, for a given depth d , produces functions of
type
t₁ -> ... -> tₙ -> tIf d \le 0 , these are constant functions, one for each value produced
by s.
If d > 0 , these functions inspect each of their arguments up to the depth
d-1 (as defined by the coseries functions for the corresponding
types) and return values produced by s. The depth to which the
values are enumerated does not depend on the depth of inspection.
Same as alts1, but preserves the depth.
Basic definitions
4 declarationsMaximum depth of generated test values.
For data values, it is the depth of nested constructor applications.
For functional values, it is both the depth of nested case analysis and the depth of results.
Series is a MonadLogic action that enumerates values of a certain type, up to some depth.
The depth bound is tracked in the Series monad and can be extracted using getDepth and changed using localDepth.
To manipulate series at the lowest level you can use its Monad, MonadPlus and MonadLogic instances. This module provides some higher-level combinators which simplify creating series.
A proper Series should be monotonic with respect to the depth — i.e.
localDepth (+1) s should emit all the values that s emits (and
possibly some more).
It is also desirable that values of smaller depth come before the values of greater depth.
Instances7MonadTrans, Monad, Functor, Applicative, Alternative, MonadPlus, …
MonadTrans SeriesDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonadMonad (Series m)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonadFunctor (Series m)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonadApplicative (Series m)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonadAlternative (Series m)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonadMonadPlus (Series m)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonadMonad m => MonadLogic (Series m)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad
Instances64Serial, …
Monad m => Serial m IntegerDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m NaturalDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m VoidDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CBoolDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CCharDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CClockDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CDoubleDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CFloatDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CIntDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CIntMaxDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CIntPtrDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CLLongDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CLongDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CPtrdiffDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CSCharDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CSUSecondsDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CShortDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CSigAtomicDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CSizeDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CTimeDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CUCharDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CUIntDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CUIntMaxDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CUIntPtrDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CULLongDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CULongDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CUSecondsDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CUShortDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CWcharDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m Int16Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m Int32Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m Int64Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m Int8Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m Word16Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m Word32Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m Word64Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m Word8Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m BoolDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m CharDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m DoubleDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m FloatDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m IntDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m OrderingDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m WordDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => Serial m ()Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesSerial m a => Serial m (Complex a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesSerial m a => Serial m (NonEmpty a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesSerial m a => Serial m (Maybe a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesSerial m a => Serial m (NonEmpty a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesSerial m a => Serial m [a]Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Num a, Enum a, Monad m) => Serial m (M a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Num a, Enum a, Serial m a) => Serial m (N a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Num a, Ord a, Serial m a) => Serial m (NonNegative a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Num a, Ord a, Serial m a) => Serial m (NonZero a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Num a, Ord a, Serial m a) => Serial m (Positive a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Integral i, Serial m i) => Serial m (Ratio i)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(CoSerial m a, Serial m b) => Serial m (a -> b)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Serial m a, Serial m b) => Serial m (Either a b)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Serial m a, Serial m b) => Serial m (a, b)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Serial m a, Serial m b, Serial m c) => Serial m (a, b, c)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Serial m a, Serial m b, Serial m c, Serial m d) => Serial m (a, b, c, d)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Monad m, Serial m (f (g a))) => Serial m (Compose f g a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Serial m a, Serial m b, Serial m c, Serial m d, Serial m e) => Serial m (a, b, c, d, e)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Serial m a, Serial m b, Serial m c, Serial m d, Serial m e, Serial m f) => Serial m (a, b, c, d, e, f)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series
Instances60CoSerial, …
Monad m => CoSerial m IntegerDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m NaturalDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m VoidDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CBoolDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CCharDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CClockDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CDoubleDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CFloatDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CIntDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CIntMaxDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CIntPtrDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CLLongDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CLongDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CPtrdiffDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CSCharDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CSUSecondsDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CShortDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CSigAtomicDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CSizeDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CTimeDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CUCharDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CUIntDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CUIntMaxDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CUIntPtrDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CULLongDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CULongDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CUSecondsDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CUShortDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CWcharDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m Int16Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m Int32Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m Int64Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m Int8Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m Word16Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m Word32Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m Word64Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m Word8Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m BoolDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m CharDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m DoubleDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m FloatDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m IntDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m OrderingDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m WordDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesMonad m => CoSerial m ()Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesCoSerial m a => CoSerial m (Complex a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesCoSerial m a => CoSerial m (NonEmpty a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesCoSerial m a => CoSerial m (Maybe a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesCoSerial m a => CoSerial m [a]Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Integral a, Monad m) => CoSerial m (N a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Integral i, CoSerial m i) => CoSerial m (Ratio i)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Ord a, Num a, Monad m) => CoSerial m (M a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(CoSerial m a, CoSerial m b) => CoSerial m (Either a b)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(CoSerial m a, CoSerial m b) => CoSerial m (a, b)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Serial m a, CoSerial m a, Serial m b, CoSerial m b) => CoSerial m (a -> b)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(CoSerial m a, CoSerial m b, CoSerial m c) => CoSerial m (a, b, c)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(CoSerial m a, CoSerial m b, CoSerial m c, CoSerial m d) => CoSerial m (a, b, c, d)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Monad m, CoSerial m (f (g a))) => CoSerial m (Compose f g a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(CoSerial m a, CoSerial m b, CoSerial m c, CoSerial m d, CoSerial m e) => CoSerial m (a, b, c, d, e)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(CoSerial m a, CoSerial m b, CoSerial m c, CoSerial m d, CoSerial m e, CoSerial m f) => CoSerial m (a, b, c, d, e, f)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series
Generic implementations
2 declarationsConvenient wrappers
4 declarationsPositive x guarantees that x > 0 .
Constructors
PositivegetPositive :: a
Instances12Functor, Foldable, Traversable, Serial, Bounded, Enum, …
Functor PositiveDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesFoldable PositiveDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesTraversable PositiveDefined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Num a, Ord a, Serial m a) => Serial m (Positive a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Num a, Bounded a) => Bounded (Positive a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesEnum a => Enum (Positive a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesEq a => Eq (Positive a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesIntegral a => Integral (Positive a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesNum a => Num (Positive a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesOrd a => Ord (Positive a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesReal a => Real (Positive a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesShow a => Show (Positive a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series
NonNegative x guarantees that x \ge 0 .
Constructors
Instances12Functor, Foldable, Traversable, Serial, Bounded, Enum, …
Functor NonNegativeDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesFoldable NonNegativeDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesTraversable NonNegativeDefined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Num a, Ord a, Serial m a) => Serial m (NonNegative a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Num a, Bounded a) => Bounded (NonNegative a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesEnum a => Enum (NonNegative a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesEq a => Eq (NonNegative a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesIntegral a => Integral (NonNegative a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesNum a => Num (NonNegative a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesOrd a => Ord (NonNegative a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesReal a => Real (NonNegative a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesShow a => Show (NonNegative a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series
NonZero x guarantees that x \ne 0 .
Constructors
NonZerogetNonZero :: a
Instances12Functor, Foldable, Traversable, Serial, Bounded, Enum, …
Functor NonZeroDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesFoldable NonZeroDefined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesTraversable NonZeroDefined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Num a, Ord a, Serial m a) => Serial m (NonZero a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series(Eq a, Num a, Bounded a) => Bounded (NonZero a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesEnum a => Enum (NonZero a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesEq a => Eq (NonZero a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesIntegral a => Integral (NonZero a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesNum a => Num (NonZero a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesOrd a => Ord (NonZero a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesReal a => Real (NonZero a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.SeriesShow a => Show (NonZero a)Defined in smallcheck-1.2.1.1 · Test.SmallCheck.Series
NonEmpty xs guarantees that xs is not null.
Constructors
NonEmptygetNonEmpty :: [a]
Other useful definitions
15 declarationsSum (union) of series.
Product of series
Fair version of Control.Applicative.ap and <*>.
Fair conjunction. Similarly to the previous function, consider the distributivity law, naturally expected from MonadPlus:
(a <|> b) >>= k = (a >>= k) <|> (b >>= k)If a >>= k can backtrack arbitrarily many times, b >>= k
may never be considered. In logic statements,
"backtracking" is the process of discarding the current
possible solution value and returning to a previous decision
point where a new value can be obtained and tried. For
example:
do { x <- pure 0 <|> pure 1 <|> pure 2; if even x then pure x else empty } :: [Int][0,2]
Here, the x value can be produced three times, where
<|> represents the decision points of that
production. The subsequent if statement specifies
empty (fail)
if x is odd, causing it to be discarded and a return
to an <|> decision point to get the next x.
The statement "a >>= k can backtrack arbitrarily many
times" means that the computation is resulting in empty and
that a has an infinite number of <|> applications to
return to. This is called a conjunctive computation because
the logic for a and k must both succeed (i.e. pure
a value instead of empty).
Similar to the way interleave allows both branches of a disjunctive computation, the >>- operator takes care to consider both branches of a conjunctive computation.
Consider the operation:
odds = pure 1 <|> fmap (2 +) odds
oddsPlus n = odds >>= \a -> pure (a + n)
g = do x <- (pure 0 <|> pure 1) >>= oddsPlus
if even x then pure x else emptyobserveMany 3 g...never completes...
This will never produce any value because all values produced
by the do program come from the pure 1 driven operation
(adding one to the sequence of odd values, resulting in the
even values that are allowed by the test in the second line),
but the pure 0 input to oddsPlus generates an infinite
number of empty failures so the even values generated by
the pure 1 alternative are never seen. Using
interleave here instead of <|> does not help due
to the aforementioned distributivity law.
Also note that the do notation desugars to >>= bind
operations, so the following would also fail:
do a <- pure 0 <|> pure 1
x <- oddsPlus a
if even x then pure x else emptyThe solution is to use the >>- in place of the normal monadic bind operation >>= when fairness between alternative productions is needed in a conjunction of statements (rules):
h = do x <- (pure 0 <|> pure 1) >>- oddsPlus
if even x then pure x else emptyobserveMany 3 h[2,4,6]
However, a bit of care is needed when using >>- because, unlike >>=, it is not associative. For example:
let m = [2,7] :: [Int]let k x = [x, x + 1]let h x = [x, x * 2]m >>= (\x -> k x >>= h)[2,4,3,6,7,14,8,16](m >>= k) >>= h -- same as above[2,4,3,6,7,14,8,16]m >>- (\x -> k x >>- h)[2,7,3,8,4,14,6,16](m >>- k) >>- h -- central elements are different[2,7,4,3,14,8,6,16]
This means that the following will be productive:
(pure 0 <|> pure 1) >>-
oddsPlus >>-
\x -> if even x then pure x else emptyWhich is equivalent to
((pure 0 <|> pure 1) >>- oddsPlus) >>-
(\x -> if even x then pure x else empty)But the following will not be productive:
(pure 0 <|> pure 1) >>-
(\a -> (oddsPlus a >>- \x -> if even x then pure x else empty))Since do notation desugaring results in the latter, the
RebindableSyntax or QualifiedDo language pragmas cannot easily be used
either. Instead, it is recommended to carefully use explicit
>>- only when needed.
Here is an action of (>>-) on lists:
take 20 $ [100,200..500] >>- (\x -> map (x +) [1..])[101,201,102,301,103,202,104,401,105,203,106,302,107,204,108,501,109,205,110,303]
The result is map (100 +) [1..] interleaved
with [200,300..500] >>- (x -> map (x +) [1..]).
You can see that a half of the numbers starts from 1,
a quarter starts from 2, and so on exponentially.
One could argue that (>>-) is a very unfair conjunction!
Run a series with a modified depth.
Query the current depth.
A simple series specified by a function from depth to the list of values up to that depth.
Limit a Series to its first n elements.
Given a depth, return the list of values generated by a Serial instance.
For example, list all integers up to depth 1:
listSeries 1 :: [Int] -- returns [0,1,-1]
Return the list of values generated by a Series. Useful for debugging Serial instances.
Examples:
list 3 series :: [Int] -- returns [0,1,-1,2,-2,3,-3]list 3 (series :: SeriesData.Functor.IdentityInt) -- returns [0,1,-1,2,-2,3,-3]list 2 series :: [[Bool]] -- returns [[],[True],[False]]
The first two are equivalent. The second has a more explicit type binding.
Monadic version of list.
Fix the depth of a series at the current level. The resulting series will no longer depend on the "ambient" depth.
If the current depth is 0, evaluate the first argument. Otherwise, evaluate the second argument with decremented depth.