-- | Type classes for random generation of values.
--
-- __Note__: the contents of this module are re-exported by
-- "Test.QuickCheck". You do not need to import it directly.
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
#ifndef NO_GENERICS
{-# LANGUAGE DefaultSignatures, FlexibleContexts, TypeOperators #-}
{-# LANGUAGE FlexibleInstances, KindSignatures, ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
#if __GLASGOW_HASKELL__ >= 710
#define OVERLAPPING_ {-# OVERLAPPING #-}
#else
{-# LANGUAGE OverlappingInstances  #-}
#define OVERLAPPING_
#endif
#endif
#ifndef NO_POLYKINDS
{-# LANGUAGE PolyKinds #-}
#endif
#ifndef NO_SAFE_HASKELL
{-# LANGUAGE Trustworthy #-}
#endif
#ifndef NO_NEWTYPE_DERIVING
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
#endif
module Test.QuickCheck.Arbitrary
  (
  -- * Arbitrary and CoArbitrary classes
    Arbitrary(..)
  , CoArbitrary(..)

  -- ** Unary and Binary classes
  , Arbitrary1(..)
  , arbitrary1
  , shrink1
  , Arbitrary2(..)
  , arbitrary2
  , shrink2

  -- ** Helper functions for implementing arbitrary
  , applyArbitrary2
  , applyArbitrary3
  , applyArbitrary4
  , arbitrarySizedIntegral        -- :: Integral a => Gen a
  , arbitrarySizedNatural         -- :: Integral a => Gen a
  , arbitraryBoundedIntegral      -- :: (Bounded a, Integral a) => Gen a
  , arbitrarySizedBoundedIntegral -- :: (Bounded a, Integral a) => Gen a
  , arbitrarySizedFractional      -- :: Fractional a => Gen a
  , arbitraryBoundedRandom        -- :: (Bounded a, Random a) => Gen a
  , arbitraryBoundedEnum          -- :: (Bounded a, Enum a) => Gen a
  -- ** Generators for various kinds of character
  , arbitraryUnicodeChar   -- :: Gen Char
  , arbitraryASCIIChar     -- :: Gen Char
  , arbitraryPrintableChar -- :: Gen Char
  -- ** Helper functions for implementing shrink
#ifndef NO_GENERICS
  , RecursivelyShrink
  , GSubterms
  , genericShrink      -- :: (Generic a, Arbitrary a, RecursivelyShrink (Rep a), GSubterms (Rep a) a) => a -> [a]
  , subterms           -- :: (Generic a, Arbitrary a, GSubterms (Rep a) a) => a -> [a]
  , recursivelyShrink  -- :: (Generic a, RecursivelyShrink (Rep a)) => a -> [a]
  , genericCoarbitrary -- :: (Generic a, GCoArbitrary (Rep a)) => a -> Gen b -> Gen b
#endif
  , shrinkNothing            -- :: a -> [a]
  , shrinkList               -- :: (a -> [a]) -> [a] -> [[a]]
  , shrinkMap                -- :: Arbitrary a -> (a -> b) -> (b -> a) -> b -> [b]
  , shrinkMapBy              -- :: (a -> b) -> (b -> a) -> (a -> [a]) -> b -> [b]
  , shrinkIntegral           -- :: Integral a => a -> [a]
  , shrinkRealFrac           -- :: RealFrac a => a -> [a]
  , shrinkBoundedEnum        -- :: (Bounded a, Enum a) => a -> [a]
  , shrinkDecimal            -- :: RealFrac a => a -> [a]
  -- ** Helper functions for implementing coarbitrary
  , coarbitraryIntegral      -- :: Integral a => a -> Gen b -> Gen b
  , coarbitraryReal          -- :: Real a => a -> Gen b -> Gen b
  , coarbitraryShow          -- :: Show a => a -> Gen b -> Gen b
  , coarbitraryEnum          -- :: Enum a => a -> Gen b -> Gen b
  , (><)

  -- ** Generators which use arbitrary
  , vector       -- :: Arbitrary a => Int -> Gen [a]
  , orderedList  -- :: (Ord a, Arbitrary a) => Gen [a]
  , infiniteList -- :: Arbitrary a => Gen [a]
  )
 where

--------------------------------------------------------------------------
-- imports

import Control.Applicative
import Data.Foldable(toList)
import System.Random(Random)
import Test.QuickCheck.Gen
import Test.QuickCheck.Random
import Test.QuickCheck.Gen.Unsafe

{-
import Data.Generics
  ( (:*:)(..)
  , (:+:)(..)
  , Unit(..)
  )
-}

import Data.Char
  ( ord
  , isLower
  , isUpper
  , toLower
  , isDigit
  , isSpace
  , isPrint
  , generalCategory
  , GeneralCategory(..)
  )

#ifndef NO_FIXED
import Data.Fixed
  ( Fixed
  , HasResolution
  )
#endif

import Data.Ratio
  ( Ratio
  , (%)
  , numerator
  , denominator
  )

import Data.Complex
  ( Complex((:+)) )

import Data.List
  ( sort
  , nub
  )

import Data.Version (Version (..))

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,2,0)
import System.IO
  ( Newline(..)
  , NewlineMode(..)
  )
#endif
#endif

import Control.Monad
  ( liftM
  , liftM2
  , liftM3
  , liftM4
  , liftM5
  )

import Data.Int(Int8, Int16, Int32, Int64)
import Data.Word(Word, Word8, Word16, Word32, Word64)
import System.Exit (ExitCode(..))
import Foreign.C.Types

#ifndef NO_GENERICS
import GHC.Generics
#endif

import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified Data.IntSet as IntSet
import qualified Data.IntMap as IntMap
import qualified Data.Sequence as Sequence
import qualified Data.Tree as Tree
import Data.Bits

import qualified Data.Monoid as Monoid

#ifndef NO_TRANSFORMERS
import Data.Functor.Identity
import Data.Functor.Constant
import Data.Functor.Compose
import Data.Functor.Product
#endif

--------------------------------------------------------------------------
-- ** class Arbitrary

-- | Random generation and shrinking of values.
--
-- QuickCheck provides @Arbitrary@ instances for most types in @base@,
-- except those which incur extra dependencies.
-- For a wider range of @Arbitrary@ instances see the
-- <http://hackage.haskell.org/package/quickcheck-instances quickcheck-instances>
-- package.
class Arbitrary a where
  -- | A generator for values of the given type.
  --
  -- It is worth spending time thinking about what sort of test data
  -- you want - good generators are often the difference between
  -- finding bugs and not finding them. You can use 'sample',
  -- 'label' and 'classify' to check the quality of your test data.
  --
  -- There is no generic @arbitrary@ implementation included because we don't
  -- know how to make a high-quality one. If you want one, consider using the
  -- <http://hackage.haskell.org/package/testing-feat testing-feat> or
  -- <http://hackage.haskell.org/package/generic-random generic-random> packages.
  --
  -- The <http://www.cse.chalmers.se/~rjmh/QuickCheck/manual.html QuickCheck manual>
  -- goes into detail on how to write good generators. Make sure to look at it,
  -- especially if your type is recursive!
  arbitrary :: Gen a

  -- | Produces a (possibly) empty list of all the possible
  -- immediate shrinks of the given value.
  --
  -- The default implementation returns the empty list, so will not try to
  -- shrink the value. If your data type has no special invariants, you can
  -- enable shrinking by defining @shrink = 'genericShrink'@, but by customising
  -- the behaviour of @shrink@ you can often get simpler counterexamples.
  --
  -- Most implementations of 'shrink' should try at least three things:
  --
  -- 1. Shrink a term to any of its immediate subterms.
  --    You can use 'subterms' to do this.
  --
  -- 2. Recursively apply 'shrink' to all immediate subterms.
  --    You can use 'recursivelyShrink' to do this.
  --
  -- 3. Type-specific shrinkings such as replacing a constructor by a
  --    simpler constructor.
  --
  -- For example, suppose we have the following implementation of binary trees:
  --
  -- > data Tree a = Nil | Branch a (Tree a) (Tree a)
  --
  -- We can then define 'shrink' as follows:
  --
  -- > shrink Nil = []
  -- > shrink (Branch x l r) =
  -- >   -- shrink Branch to Nil
  -- >   [Nil] ++
  -- >   -- shrink to subterms
  -- >   [l, r] ++
  -- >   -- recursively shrink subterms
  -- >   [Branch x' l' r' | (x', l', r') <- shrink (x, l, r)]
  --
  -- There are a couple of subtleties here:
  --
  -- * QuickCheck tries the shrinking candidates in the order they
  --   appear in the list, so we put more aggressive shrinking steps
  --   (such as replacing the whole tree by @Nil@) before smaller
  --   ones (such as recursively shrinking the subtrees).
  --
  -- * It is tempting to write the last line as
  --   @[Branch x' l' r' | x' <- shrink x, l' <- shrink l, r' <- shrink r]@
  --   but this is the /wrong thing/! It will force QuickCheck to shrink
  --   @x@, @l@ and @r@ in tandem, and shrinking will stop once /one/ of
  --   the three is fully shrunk.
  --
  -- There is a fair bit of boilerplate in the code above.
  -- We can avoid it with the help of some generic functions.
  -- The function 'genericShrink' tries shrinking a term to all of its
  -- subterms and, failing that, recursively shrinks the subterms.
  -- Using it, we can define 'shrink' as:
  --
  -- > shrink x = shrinkToNil x ++ genericShrink x
  -- >   where
  -- >     shrinkToNil Nil = []
  -- >     shrinkToNil (Branch _ l r) = [Nil]
  --
  -- 'genericShrink' is a combination of 'subterms', which shrinks
  -- a term to any of its subterms, and 'recursivelyShrink', which shrinks
  -- all subterms of a term. These may be useful if you need a bit more
  -- control over shrinking than 'genericShrink' gives you.
  --
  -- A final gotcha: we cannot define 'shrink' as simply @'shrink' x = Nil:'genericShrink' x@
  -- as this shrinks @Nil@ to @Nil@, and shrinking will go into an
  -- infinite loop.
  --
  -- If all this leaves you bewildered, you might try @'shrink' = 'genericShrink'@ to begin with,
  -- after deriving @Generic@ for your type. However, if your data type has any
  -- special invariants, you will need to check that 'genericShrink' can't break those invariants.
  shrink :: a -> [a]
  shrink a
_ = []

-- | Lifting of the 'Arbitrary' class to unary type constructors.
class Arbitrary1 f where
  liftArbitrary :: Gen a -> Gen (f a)
  liftShrink    :: (a -> [a]) -> f a -> [f a]
  liftShrink a -> [a]
_ f a
_ = []

arbitrary1 :: (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1 :: forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1 = forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary forall a. Arbitrary a => Gen a
arbitrary

shrink1 :: (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1 :: forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1 = forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink forall a. Arbitrary a => a -> [a]
shrink

-- | Lifting of the 'Arbitrary' class to binary type constructors.
class Arbitrary2 f where
  liftArbitrary2 :: Gen a -> Gen b -> Gen (f a b)
  liftShrink2    :: (a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
  liftShrink2 a -> [a]
_ b -> [b]
_ f a b
_ = []

arbitrary2 :: (Arbitrary2 f, Arbitrary a, Arbitrary b) => Gen (f a b)
arbitrary2 :: forall (f :: * -> * -> *) a b.
(Arbitrary2 f, Arbitrary a, Arbitrary b) =>
Gen (f a b)
arbitrary2 = forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary

shrink2 :: (Arbitrary2 f, Arbitrary a, Arbitrary b) => f a b -> [f a b]
shrink2 :: forall (f :: * -> * -> *) a b.
(Arbitrary2 f, Arbitrary a, Arbitrary b) =>
f a b -> [f a b]
shrink2 = forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 forall a. Arbitrary a => a -> [a]
shrink forall a. Arbitrary a => a -> [a]
shrink

#ifndef NO_GENERICS
-- | Shrink a term to any of its immediate subterms,
-- and also recursively shrink all subterms.
genericShrink :: (Generic a, RecursivelyShrink (Rep a), GSubterms (Rep a) a) => a -> [a]
genericShrink :: forall a.
(Generic a, RecursivelyShrink (Rep a), GSubterms (Rep a) a) =>
a -> [a]
genericShrink a
x = forall a. (Generic a, GSubterms (Rep a) a) => a -> [a]
subterms a
x forall a. [a] -> [a] -> [a]
++ forall a. (Generic a, RecursivelyShrink (Rep a)) => a -> [a]
recursivelyShrink a
x

-- | Recursively shrink all immediate subterms.
recursivelyShrink :: (Generic a, RecursivelyShrink (Rep a)) => a -> [a]
recursivelyShrink :: forall a. (Generic a, RecursivelyShrink (Rep a)) => a -> [a]
recursivelyShrink = forall a b. (a -> b) -> [a] -> [b]
map forall a x. Generic a => Rep a x -> a
to forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k).
RecursivelyShrink f =>
f a -> [f a]
grecursivelyShrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a x. Generic a => a -> Rep a x
from

class RecursivelyShrink f where
  grecursivelyShrink :: f a -> [f a]

instance (RecursivelyShrink f, RecursivelyShrink g) => RecursivelyShrink (f :*: g) where
  grecursivelyShrink :: forall (a :: k). (:*:) f g a -> [(:*:) f g a]
grecursivelyShrink (f a
x :*: g a
y) =
    [f a
x' forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g a
y | f a
x' <- forall {k} (f :: k -> *) (a :: k).
RecursivelyShrink f =>
f a -> [f a]
grecursivelyShrink f a
x] forall a. [a] -> [a] -> [a]
++
    [f a
x forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g a
y' | g a
y' <- forall {k} (f :: k -> *) (a :: k).
RecursivelyShrink f =>
f a -> [f a]
grecursivelyShrink g a
y]

instance (RecursivelyShrink f, RecursivelyShrink g) => RecursivelyShrink (f :+: g) where
  grecursivelyShrink :: forall (a :: k). (:+:) f g a -> [(:+:) f g a]
grecursivelyShrink (L1 f a
x) = forall a b. (a -> b) -> [a] -> [b]
map forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (forall {k} (f :: k -> *) (a :: k).
RecursivelyShrink f =>
f a -> [f a]
grecursivelyShrink f a
x)
  grecursivelyShrink (R1 g a
x) = forall a b. (a -> b) -> [a] -> [b]
map forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (forall {k} (f :: k -> *) (a :: k).
RecursivelyShrink f =>
f a -> [f a]
grecursivelyShrink g a
x)

instance RecursivelyShrink f => RecursivelyShrink (M1 i c f) where
  grecursivelyShrink :: forall (a :: k). M1 i c f a -> [M1 i c f a]
grecursivelyShrink (M1 f a
x) = forall a b. (a -> b) -> [a] -> [b]
map forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (forall {k} (f :: k -> *) (a :: k).
RecursivelyShrink f =>
f a -> [f a]
grecursivelyShrink f a
x)

instance Arbitrary a => RecursivelyShrink (K1 i a) where
  grecursivelyShrink :: forall (a :: k). K1 i a a -> [K1 i a a]
grecursivelyShrink (K1 a
x) = forall a b. (a -> b) -> [a] -> [b]
map forall k i c (p :: k). c -> K1 i c p
K1 (forall a. Arbitrary a => a -> [a]
shrink a
x)

instance RecursivelyShrink U1 where
  grecursivelyShrink :: forall (a :: k). U1 a -> [U1 a]
grecursivelyShrink U1 a
U1 = []

instance RecursivelyShrink V1 where
  -- The empty type can't be shrunk to anything.
  grecursivelyShrink :: forall (a :: k). V1 a -> [V1 a]
grecursivelyShrink V1 a
_ = []


-- | All immediate subterms of a term.
subterms :: (Generic a, GSubterms (Rep a) a) => a -> [a]
subterms :: forall a. (Generic a, GSubterms (Rep a) a) => a -> [a]
subterms = forall (f :: * -> *) a. GSubterms f a => f a -> [a]
gSubterms forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a x. Generic a => a -> Rep a x
from


class GSubterms f a where
  -- | Provides the immediate subterms of a term that are of the same type
  -- as the term itself.
  --
  -- Requires a constructor to be stripped off; this means it skips through
  -- @M1@ wrappers and returns @[]@ on everything that's not `(:*:)` or `(:+:)`.
  --
  -- Once a `(:*:)` or `(:+:)` constructor has been reached, this function
  -- delegates to `gSubtermsIncl` to return the immediately next constructor
  -- available.
  gSubterms :: f a -> [a]

instance GSubterms V1 a where
  -- The empty type can't be shrunk to anything.
  gSubterms :: V1 a -> [a]
gSubterms V1 a
_ = []

instance GSubterms U1 a where
  gSubterms :: U1 a -> [a]
gSubterms U1 a
U1 = []

instance (GSubtermsIncl f a, GSubtermsIncl g a) => GSubterms (f :*: g) a where
  gSubterms :: (:*:) f g a -> [a]
gSubterms (f a
l :*: g a
r) = forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl f a
l forall a. [a] -> [a] -> [a]
++ forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl g a
r

instance (GSubtermsIncl f a, GSubtermsIncl g a) => GSubterms (f :+: g) a where
  gSubterms :: (:+:) f g a -> [a]
gSubterms (L1 f a
x) = forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl f a
x
  gSubterms (R1 g a
x) = forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl g a
x

instance GSubterms f a => GSubterms (M1 i c f) a where
  gSubterms :: M1 i c f a -> [a]
gSubterms (M1 f a
x) = forall (f :: * -> *) a. GSubterms f a => f a -> [a]
gSubterms f a
x

instance GSubterms (K1 i a) b where
  gSubterms :: K1 i a b -> [b]
gSubterms (K1 a
_) = []


class GSubtermsIncl f a where
  -- | Provides the immediate subterms of a term that are of the same type
  -- as the term itself.
  --
  -- In contrast to `gSubterms`, this returns the immediate next constructor
  -- available.
  gSubtermsIncl :: f a -> [a]

instance GSubtermsIncl V1 a where
  -- The empty type can't be shrunk to anything.
  gSubtermsIncl :: V1 a -> [a]
gSubtermsIncl V1 a
_ = []

instance GSubtermsIncl U1 a where
  gSubtermsIncl :: U1 a -> [a]
gSubtermsIncl U1 a
U1 = []

instance (GSubtermsIncl f a, GSubtermsIncl g a) => GSubtermsIncl (f :*: g) a where
  gSubtermsIncl :: (:*:) f g a -> [a]
gSubtermsIncl (f a
l :*: g a
r) = forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl f a
l forall a. [a] -> [a] -> [a]
++ forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl g a
r

instance (GSubtermsIncl f a, GSubtermsIncl g a) => GSubtermsIncl (f :+: g) a where
  gSubtermsIncl :: (:+:) f g a -> [a]
gSubtermsIncl (L1 f a
x) = forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl f a
x
  gSubtermsIncl (R1 g a
x) = forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl g a
x

instance GSubtermsIncl f a => GSubtermsIncl (M1 i c f) a where
  gSubtermsIncl :: M1 i c f a -> [a]
gSubtermsIncl (M1 f a
x) = forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl f a
x

-- This is the important case: We've found a term of the same type.
instance OVERLAPPING_ GSubtermsIncl (K1 i a) a where
  gSubtermsIncl :: K1 i a a -> [a]
gSubtermsIncl (K1 a
x) = [a
x]

instance OVERLAPPING_ GSubtermsIncl (K1 i a) b where
  gSubtermsIncl :: K1 i a b -> [b]
gSubtermsIncl (K1 a
_) = []

#endif

-- instances

instance (CoArbitrary a) => Arbitrary1 ((->) a) where
  liftArbitrary :: forall a. Gen a -> Gen (a -> a)
liftArbitrary Gen a
arbB = forall (m :: * -> *) a. Monad m => m (Gen a) -> Gen (m a)
promote (forall a b. CoArbitrary a => a -> Gen b -> Gen b
`coarbitrary` Gen a
arbB)

instance (CoArbitrary a, Arbitrary b) => Arbitrary (a -> b) where
  arbitrary :: Gen (a -> b)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1

instance Arbitrary () where
  arbitrary :: Gen ()
arbitrary = forall (m :: * -> *) a. Monad m => a -> m a
return ()

instance Arbitrary Bool where
  arbitrary :: Gen Bool
arbitrary = forall a. Enum a => (a, a) -> Gen a
chooseEnum (Bool
False,Bool
True)
  shrink :: Bool -> [Bool]
shrink Bool
True = [Bool
False]
  shrink Bool
False = []

instance Arbitrary Ordering where
  arbitrary :: Gen Ordering
arbitrary = forall a. [a] -> Gen a
elements [Ordering
LT, Ordering
EQ, Ordering
GT]
  shrink :: Ordering -> [Ordering]
shrink Ordering
GT = [Ordering
EQ, Ordering
LT]
  shrink Ordering
LT = [Ordering
EQ]
  shrink Ordering
EQ = []

instance Arbitrary1 Maybe where
  liftArbitrary :: forall a. Gen a -> Gen (Maybe a)
liftArbitrary Gen a
arb = forall a. [(Int, Gen a)] -> Gen a
frequency [(Int
1, forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing), (Int
3, forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a. a -> Maybe a
Just Gen a
arb)]

  liftShrink :: forall a. (a -> [a]) -> Maybe a -> [Maybe a]
liftShrink a -> [a]
shr (Just a
x) = forall a. Maybe a
Nothing forall a. a -> [a] -> [a]
: [ forall a. a -> Maybe a
Just a
x' | a
x' <- a -> [a]
shr a
x ]
  liftShrink a -> [a]
_   Maybe a
Nothing  = []

instance Arbitrary a => Arbitrary (Maybe a) where
  arbitrary :: Gen (Maybe a)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Maybe a -> [Maybe a]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1

instance Arbitrary2 Either where
  liftArbitrary2 :: forall a b. Gen a -> Gen b -> Gen (Either a b)
liftArbitrary2 Gen a
arbA Gen b
arbB = forall a. [Gen a] -> Gen a
oneof [forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. a -> Either a b
Left Gen a
arbA, forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. b -> Either a b
Right Gen b
arbB]

  liftShrink2 :: forall a b. (a -> [a]) -> (b -> [b]) -> Either a b -> [Either a b]
liftShrink2 a -> [a]
shrA b -> [b]
_ (Left a
x)  = [ forall a b. a -> Either a b
Left  a
x' | a
x' <- a -> [a]
shrA a
x ]
  liftShrink2 a -> [a]
_ b -> [b]
shrB (Right b
y) = [ forall a b. b -> Either a b
Right b
y' | b
y' <- b -> [b]
shrB b
y ]

instance Arbitrary a => Arbitrary1 (Either a) where
  liftArbitrary :: forall a. Gen a -> Gen (Either a a)
liftArbitrary = forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 forall a. Arbitrary a => Gen a
arbitrary
  liftShrink :: forall a. (a -> [a]) -> Either a a -> [Either a a]
liftShrink = forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 forall a. Arbitrary a => a -> [a]
shrink

instance (Arbitrary a, Arbitrary b) => Arbitrary (Either a b) where
  arbitrary :: Gen (Either a b)
arbitrary = forall (f :: * -> * -> *) a b.
(Arbitrary2 f, Arbitrary a, Arbitrary b) =>
Gen (f a b)
arbitrary2
  shrink :: Either a b -> [Either a b]
shrink = forall (f :: * -> * -> *) a b.
(Arbitrary2 f, Arbitrary a, Arbitrary b) =>
f a b -> [f a b]
shrink2

instance Arbitrary1 [] where
  liftArbitrary :: forall a. Gen a -> Gen [a]
liftArbitrary = forall a. Gen a -> Gen [a]
listOf
  liftShrink :: forall a. (a -> [a]) -> [a] -> [[a]]
liftShrink = forall a. (a -> [a]) -> [a] -> [[a]]
shrinkList

instance Arbitrary a => Arbitrary [a] where
  arbitrary :: Gen [a]
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: [a] -> [[a]]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1

-- | Shrink a list of values given a shrinking function for individual values.
shrinkList :: (a -> [a]) -> [a] -> [[a]]
shrinkList :: forall a. (a -> [a]) -> [a] -> [[a]]
shrinkList a -> [a]
shr [a]
xs = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ forall {a}. Int -> Int -> [a] -> [[a]]
removes Int
k Int
n [a]
xs | Int
k <- forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Ord a => a -> a -> Bool
>Int
0) (forall a. (a -> a) -> a -> [a]
iterate (forall a. Integral a => a -> a -> a
`div`Int
2) Int
n) ]
                 forall a. [a] -> [a] -> [a]
++ [a] -> [[a]]
shrinkOne [a]
xs
 where
  n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs

  shrinkOne :: [a] -> [[a]]
shrinkOne []     = []
  shrinkOne (a
x:[a]
xs) = [ a
x'forall a. a -> [a] -> [a]
:[a]
xs | a
x'  <- a -> [a]
shr a
x ]
                  forall a. [a] -> [a] -> [a]
++ [ a
xforall a. a -> [a] -> [a]
:[a]
xs' | [a]
xs' <- [a] -> [[a]]
shrinkOne [a]
xs ]

  removes :: Int -> Int -> [a] -> [[a]]
removes Int
k Int
n [a]
xs
    | Int
k forall a. Ord a => a -> a -> Bool
> Int
n     = []
    | forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs2  = [[]]
    | Bool
otherwise = [a]
xs2 forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map ([a]
xs1 forall a. [a] -> [a] -> [a]
++) (Int -> Int -> [a] -> [[a]]
removes Int
k (Int
nforall a. Num a => a -> a -> a
-Int
k) [a]
xs2)
   where
    xs1 :: [a]
xs1 = forall a. Int -> [a] -> [a]
take Int
k [a]
xs
    xs2 :: [a]
xs2 = forall a. Int -> [a] -> [a]
drop Int
k [a]
xs

{-
  -- "standard" definition for lists:
  shrink []     = []
  shrink (x:xs) = [ xs ]
               ++ [ x:xs' | xs' <- shrink xs ]
               ++ [ x':xs | x'  <- shrink x ]
-}

instance Integral a => Arbitrary (Ratio a) where
  arbitrary :: Gen (Ratio a)
arbitrary = forall a. Fractional a => Gen a
arbitrarySizedFractional
  shrink :: Ratio a -> [Ratio a]
shrink    = forall a. RealFrac a => a -> [a]
shrinkRealFrac

#if defined(MIN_VERSION_base) && MIN_VERSION_base(4,4,0)
instance Arbitrary a => Arbitrary (Complex a) where
#else
instance (RealFloat a, Arbitrary a) => Arbitrary (Complex a) where
#endif
  arbitrary :: Gen (Complex a)
arbitrary = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall a. a -> a -> Complex a
(:+) forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Complex a -> [Complex a]
shrink (a
x :+ a
y) = [ a
x' forall a. a -> a -> Complex a
:+ a
y | a
x' <- forall a. Arbitrary a => a -> [a]
shrink a
x ] forall a. [a] -> [a] -> [a]
++
                    [ a
x forall a. a -> a -> Complex a
:+ a
y' | a
y' <- forall a. Arbitrary a => a -> [a]
shrink a
y ]

#ifndef NO_FIXED
instance HasResolution a => Arbitrary (Fixed a) where
  arbitrary :: Gen (Fixed a)
arbitrary = forall a. Fractional a => Gen a
arbitrarySizedFractional
  shrink :: Fixed a -> [Fixed a]
shrink    = forall a. RealFrac a => a -> [a]
shrinkDecimal
#endif

instance Arbitrary2 (,) where
  liftArbitrary2 :: forall a b. Gen a -> Gen b -> Gen (a, b)
liftArbitrary2 = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (,)
  liftShrink2 :: forall a b. (a -> [a]) -> (b -> [b]) -> (a, b) -> [(a, b)]
liftShrink2 a -> [a]
shrA b -> [b]
shrB (a
x, b
y) =
       [ (a
x', b
y) | a
x' <- a -> [a]
shrA a
x ]
    forall a. [a] -> [a] -> [a]
++ [ (a
x, b
y') | b
y' <- b -> [b]
shrB b
y ]

instance (Arbitrary a) => Arbitrary1 ((,) a) where
  liftArbitrary :: forall a. Gen a -> Gen (a, a)
liftArbitrary = forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 forall a. Arbitrary a => Gen a
arbitrary
  liftShrink :: forall a. (a -> [a]) -> (a, a) -> [(a, a)]
liftShrink = forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 forall a. Arbitrary a => a -> [a]
shrink

instance (Arbitrary a, Arbitrary b) => Arbitrary (a,b) where
  arbitrary :: Gen (a, b)
arbitrary = forall (f :: * -> * -> *) a b.
(Arbitrary2 f, Arbitrary a, Arbitrary b) =>
Gen (f a b)
arbitrary2
  shrink :: (a, b) -> [(a, b)]
shrink = forall (f :: * -> * -> *) a b.
(Arbitrary2 f, Arbitrary a, Arbitrary b) =>
f a b -> [f a b]
shrink2

instance (Arbitrary a, Arbitrary b, Arbitrary c)
      => Arbitrary (a,b,c)
 where
  arbitrary :: Gen (a, b, c)
arbitrary = forall (m :: * -> *) a1 a2 a3 r.
Monad m =>
(a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
liftM3 (,,) forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary

  shrink :: (a, b, c) -> [(a, b, c)]
shrink (a
x, b
y, c
z) =
    [ (a
x', b
y', c
z')
    | (a
x', (b
y', c
z')) <- forall a. Arbitrary a => a -> [a]
shrink (a
x, (b
y, c
z)) ]

instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d)
      => Arbitrary (a,b,c,d)
 where
  arbitrary :: Gen (a, b, c, d)
arbitrary = forall (m :: * -> *) a1 a2 a3 a4 r.
Monad m =>
(a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
liftM4 (,,,) forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary

  shrink :: (a, b, c, d) -> [(a, b, c, d)]
shrink (a
w, b
x, c
y, d
z) =
    [ (a
w', b
x', c
y', d
z')
    | (a
w', (b
x', (c
y', d
z'))) <- forall a. Arbitrary a => a -> [a]
shrink (a
w, (b
x, (c
y, d
z))) ]

instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e)
      => Arbitrary (a,b,c,d,e)
 where
  arbitrary :: Gen (a, b, c, d, e)
arbitrary = forall (m :: * -> *) a1 a2 a3 a4 a5 r.
Monad m =>
(a1 -> a2 -> a3 -> a4 -> a5 -> r)
-> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
liftM5 (,,,,) forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary

  shrink :: (a, b, c, d, e) -> [(a, b, c, d, e)]
shrink (a
v, b
w, c
x, d
y, e
z) =
    [ (a
v', b
w', c
x', d
y', e
z')
    | (a
v', (b
w', (c
x', (d
y', e
z')))) <- forall a. Arbitrary a => a -> [a]
shrink (a
v, (b
w, (c
x, (d
y, e
z)))) ]

instance ( Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e
         , Arbitrary f
         )
      => Arbitrary (a,b,c,d,e,f)
 where
  arbitrary :: Gen (a, b, c, d, e, f)
arbitrary = forall (m :: * -> *) a. Monad m => a -> m a
return (,,,,,)
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary

  shrink :: (a, b, c, d, e, f) -> [(a, b, c, d, e, f)]
shrink (a
u, b
v, c
w, d
x, e
y, f
z) =
    [ (a
u', b
v', c
w', d
x', e
y', f
z')
    | (a
u', (b
v', (c
w', (d
x', (e
y', f
z'))))) <- forall a. Arbitrary a => a -> [a]
shrink (a
u, (b
v, (c
w, (d
x, (e
y, f
z))))) ]

instance ( Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e
         , Arbitrary f, Arbitrary g
         )
      => Arbitrary (a,b,c,d,e,f,g)
 where
  arbitrary :: Gen (a, b, c, d, e, f, g)
arbitrary = forall (m :: * -> *) a. Monad m => a -> m a
return (,,,,,,)
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary

  shrink :: (a, b, c, d, e, f, g) -> [(a, b, c, d, e, f, g)]
shrink (a
t, b
u, c
v, d
w, e
x, f
y, g
z) =
    [ (a
t', b
u', c
v', d
w', e
x', f
y', g
z')
    | (a
t', (b
u', (c
v', (d
w', (e
x', (f
y', g
z')))))) <- forall a. Arbitrary a => a -> [a]
shrink (a
t, (b
u, (c
v, (d
w, (e
x, (f
y, g
z)))))) ]

instance ( Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e
         , Arbitrary f, Arbitrary g, Arbitrary h
         )
      => Arbitrary (a,b,c,d,e,f,g,h)
 where
  arbitrary :: Gen (a, b, c, d, e, f, g, h)
arbitrary = forall (m :: * -> *) a. Monad m => a -> m a
return (,,,,,,,)
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary

  shrink :: (a, b, c, d, e, f, g, h) -> [(a, b, c, d, e, f, g, h)]
shrink (a
s, b
t, c
u, d
v, e
w, f
x, g
y, h
z) =
    [ (a
s', b
t', c
u', d
v', e
w', f
x', g
y', h
z')
    | (a
s', (b
t', (c
u', (d
v', (e
w', (f
x', (g
y', h
z')))))))
      <- forall a. Arbitrary a => a -> [a]
shrink (a
s, (b
t, (c
u, (d
v, (e
w, (f
x, (g
y, h
z))))))) ]

instance ( Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e
         , Arbitrary f, Arbitrary g, Arbitrary h, Arbitrary i
         )
      => Arbitrary (a,b,c,d,e,f,g,h,i)
 where
  arbitrary :: Gen (a, b, c, d, e, f, g, h, i)
arbitrary = forall (m :: * -> *) a. Monad m => a -> m a
return (,,,,,,,,)
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary

  shrink :: (a, b, c, d, e, f, g, h, i) -> [(a, b, c, d, e, f, g, h, i)]
shrink (a
r, b
s, c
t, d
u, e
v, f
w, g
x, h
y, i
z) =
    [ (a
r', b
s', c
t', d
u', e
v', f
w', g
x', h
y', i
z')
    | (a
r', (b
s', (c
t', (d
u', (e
v', (f
w', (g
x', (h
y', i
z'))))))))
      <- forall a. Arbitrary a => a -> [a]
shrink (a
r, (b
s, (c
t, (d
u, (e
v, (f
w, (g
x, (h
y, i
z)))))))) ]

instance ( Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e
         , Arbitrary f, Arbitrary g, Arbitrary h, Arbitrary i, Arbitrary j
         )
      => Arbitrary (a,b,c,d,e,f,g,h,i,j)
 where
  arbitrary :: Gen (a, b, c, d, e, f, g, h, i, j)
arbitrary = forall (m :: * -> *) a. Monad m => a -> m a
return (,,,,,,,,,)
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary
          forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary

  shrink :: (a, b, c, d, e, f, g, h, i, j) -> [(a, b, c, d, e, f, g, h, i, j)]
shrink (a
q, b
r, c
s, d
t, e
u, f
v, g
w, h
x, i
y, j
z) =
    [ (a
q', b
r', c
s', d
t', e
u', f
v', g
w', h
x', i
y', j
z')
    | (a
q', (b
r', (c
s', (d
t', (e
u', (f
v', (g
w', (h
x', (i
y', j
z')))))))))
      <- forall a. Arbitrary a => a -> [a]
shrink (a
q, (b
r, (c
s, (d
t, (e
u, (f
v, (g
w, (h
x, (i
y, j
z))))))))) ]

-- typical instance for primitive (numerical) types

instance Arbitrary Integer where
  arbitrary :: Gen Integer
arbitrary = forall a. Integral a => Gen a
arbitrarySizedIntegral
  shrink :: Integer -> [Integer]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Int where
  arbitrary :: Gen Int
arbitrary = forall a. Integral a => Gen a
arbitrarySizedIntegral
  shrink :: Int -> [Int]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Int8 where
  arbitrary :: Gen Int8
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: Int8 -> [Int8]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Int16 where
  arbitrary :: Gen Int16
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: Int16 -> [Int16]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Int32 where
  arbitrary :: Gen Int32
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: Int32 -> [Int32]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Int64 where
  arbitrary :: Gen Int64
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: Int64 -> [Int64]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Word where
  arbitrary :: Gen Word
arbitrary = forall a. Integral a => Gen a
arbitrarySizedNatural
  shrink :: Word -> [Word]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Word8 where
  arbitrary :: Gen Word8
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: Word8 -> [Word8]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Word16 where
  arbitrary :: Gen Word16
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: Word16 -> [Word16]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Word32 where
  arbitrary :: Gen Word32
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: Word32 -> [Word32]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Word64 where
  arbitrary :: Gen Word64
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: Word64 -> [Word64]
shrink    = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary Char where
  arbitrary :: Gen Char
arbitrary =
    forall a. [(Int, Gen a)] -> Gen a
frequency
      [(Int
3, Gen Char
arbitraryASCIIChar),
       (Int
1, Gen Char
arbitraryUnicodeChar)]

  shrink :: Char -> [Char]
shrink Char
c = forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> Char -> Bool
<. Char
c) forall a b. (a -> b) -> a -> b
$ forall a. Eq a => [a] -> [a]
nub
            forall a b. (a -> b) -> a -> b
$ [Char
'a',Char
'b',Char
'c']
            forall a. [a] -> [a] -> [a]
++ [ Char -> Char
toLower Char
c | Char -> Bool
isUpper Char
c ]
            forall a. [a] -> [a] -> [a]
++ [Char
'A',Char
'B',Char
'C']
            forall a. [a] -> [a] -> [a]
++ [Char
'1',Char
'2',Char
'3']
            forall a. [a] -> [a] -> [a]
++ [Char
' ',Char
'\n']
     where
      Char
a <. :: Char -> Char -> Bool
<. Char
b  = Char -> ((Bool, Bool, Bool), (Bool, Bool, Char))
stamp Char
a forall a. Ord a => a -> a -> Bool
< Char -> ((Bool, Bool, Bool), (Bool, Bool, Char))
stamp Char
b
      stamp :: Char -> ((Bool, Bool, Bool), (Bool, Bool, Char))
stamp Char
a = ( (Bool -> Bool
not (Char -> Bool
isLower Char
a)
                , Bool -> Bool
not (Char -> Bool
isUpper Char
a)
                , Bool -> Bool
not (Char -> Bool
isDigit Char
a))
                , (Bool -> Bool
not (Char
aforall a. Eq a => a -> a -> Bool
==Char
' ')
                , Bool -> Bool
not (Char -> Bool
isSpace Char
a)
                , Char
a)
                )

instance Arbitrary Float where
  arbitrary :: Gen Float
arbitrary = forall a. [Gen a] -> Gen a
oneof
    -- generate 0..1 numbers with full precision
    [ Gen Float
genFloat
    -- generate integral numbers
    , forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. Arbitrary a => Gen a
arbitrary :: Gen Int)
    -- generate fractions with small denominators
    , Gen Float
smallDenominators
    -- uniform -size..size with with denominators ~ size
    , Gen Float
uniform
    -- and uniform -size..size with higher precision
    , forall a. Fractional a => Gen a
arbitrarySizedFractional
    ]
    where
      smallDenominators :: Gen Float
smallDenominators = forall a. (Int -> Gen a) -> Gen a
sized forall a b. (a -> b) -> a -> b
$ \Int
n -> do
        Int
i <- (Int, Int) -> Gen Int
chooseInt (Int
0, Int
n)
        forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. Fractional a => Rational -> a
fromRational (forall a. Int -> Stream a -> a
streamNth Int
i Stream Rational
rationalUniverse))

      uniform :: Gen Float
uniform = forall a. (Int -> Gen a) -> Gen a
sized forall a b. (a -> b) -> a -> b
$ \Int
n -> do
        let n' :: Integer
n' = forall a. Integral a => a -> Integer
toInteger Int
n
        Integer
b <- (Integer, Integer) -> Gen Integer
chooseInteger (Integer
1, forall a. Ord a => a -> a -> a
max Integer
1 Integer
n')
        Integer
a <- (Integer, Integer) -> Gen Integer
chooseInteger ((-Integer
n') forall a. Num a => a -> a -> a
* Integer
b, Integer
n' forall a. Num a => a -> a -> a
* Integer
b)
        forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Fractional a => Rational -> a
fromRational (Integer
a forall a. Integral a => a -> a -> Ratio a
% Integer
b))

  shrink :: Float -> [Float]
shrink    = forall a. RealFrac a => a -> [a]
shrinkDecimal

instance Arbitrary Double where
  arbitrary :: Gen Double
arbitrary = forall a. [Gen a] -> Gen a
oneof
    -- generate 0..1 numbers with full precision
    [ Gen Double
genDouble
    -- generate integral numbers
    , forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. Arbitrary a => Gen a
arbitrary :: Gen Int)
    -- generate fractions with small denominators
    , Gen Double
smallDenominators
    -- uniform -size..size with with denominators ~ size
    , Gen Double
uniform
    -- and uniform -size..size with higher precision
    , forall a. Fractional a => Gen a
arbitrarySizedFractional
    ]
    where
      smallDenominators :: Gen Double
smallDenominators = forall a. (Int -> Gen a) -> Gen a
sized forall a b. (a -> b) -> a -> b
$ \Int
n -> do
        Int
i <- (Int, Int) -> Gen Int
chooseInt (Int
0, Int
n)
        forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. Fractional a => Rational -> a
fromRational (forall a. Int -> Stream a -> a
streamNth Int
i Stream Rational
rationalUniverse))

      uniform :: Gen Double
uniform = forall a. (Int -> Gen a) -> Gen a
sized forall a b. (a -> b) -> a -> b
$ \Int
n -> do
        let n' :: Integer
n' = forall a. Integral a => a -> Integer
toInteger Int
n
        Integer
b <- (Integer, Integer) -> Gen Integer
chooseInteger (Integer
1, forall a. Ord a => a -> a -> a
max Integer
1 Integer
n')
        Integer
a <- (Integer, Integer) -> Gen Integer
chooseInteger ((-Integer
n') forall a. Num a => a -> a -> a
* Integer
b, Integer
n' forall a. Num a => a -> a -> a
* Integer
b)
        forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Fractional a => Rational -> a
fromRational (Integer
a forall a. Integral a => a -> a -> Ratio a
% Integer
b))

  shrink :: Double -> [Double]
shrink    = forall a. RealFrac a => a -> [a]
shrinkDecimal

instance Arbitrary CChar where
  arbitrary :: Gen CChar
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CChar -> [CChar]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CSChar where
  arbitrary :: Gen CSChar
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CSChar -> [CSChar]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CUChar where
  arbitrary :: Gen CUChar
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CUChar -> [CUChar]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CShort where
  arbitrary :: Gen CShort
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CShort -> [CShort]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CUShort where
  arbitrary :: Gen CUShort
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CUShort -> [CUShort]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CInt where
  arbitrary :: Gen CInt
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CInt -> [CInt]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CUInt where
  arbitrary :: Gen CUInt
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CUInt -> [CUInt]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CLong where
  arbitrary :: Gen CLong
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CLong -> [CLong]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CULong where
  arbitrary :: Gen CULong
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CULong -> [CULong]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CPtrdiff where
  arbitrary :: Gen CPtrdiff
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CPtrdiff -> [CPtrdiff]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CSize where
  arbitrary :: Gen CSize
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CSize -> [CSize]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CWchar where
  arbitrary :: Gen CWchar
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CWchar -> [CWchar]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CSigAtomic where
  arbitrary :: Gen CSigAtomic
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CSigAtomic -> [CSigAtomic]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CLLong where
  arbitrary :: Gen CLLong
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CLLong -> [CLLong]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CULLong where
  arbitrary :: Gen CULLong
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CULLong -> [CULLong]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CIntPtr where
  arbitrary :: Gen CIntPtr
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CIntPtr -> [CIntPtr]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CUIntPtr where
  arbitrary :: Gen CUIntPtr
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CUIntPtr -> [CUIntPtr]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CIntMax where
  arbitrary :: Gen CIntMax
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CIntMax -> [CIntMax]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

instance Arbitrary CUIntMax where
  arbitrary :: Gen CUIntMax
arbitrary = forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CUIntMax -> [CUIntMax]
shrink = forall a. Integral a => a -> [a]
shrinkIntegral

#ifndef NO_CTYPES_CONSTRUCTORS
-- The following four types have no Bounded instance,
-- so we fake it by discovering the bounds at runtime.
instance Arbitrary CClock where
  arbitrary :: Gen CClock
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int64 -> CClock
CClock forall a. Arbitrary a => Gen a
arbitrary
  shrink :: CClock -> [CClock]
shrink (CClock Int64
x) = forall a b. (a -> b) -> [a] -> [b]
map Int64 -> CClock
CClock (forall a. Arbitrary a => a -> [a]
shrink Int64
x)

instance Arbitrary CTime where
  arbitrary :: Gen CTime
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int64 -> CTime
CTime forall a. Arbitrary a => Gen a
arbitrary
  shrink :: CTime -> [CTime]
shrink (CTime Int64
x) = forall a b. (a -> b) -> [a] -> [b]
map Int64 -> CTime
CTime (forall a. Arbitrary a => a -> [a]
shrink Int64
x)

#ifndef NO_FOREIGN_C_USECONDS
instance Arbitrary CUSeconds where
  arbitrary :: Gen CUSeconds
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word32 -> CUSeconds
CUSeconds forall a. Arbitrary a => Gen a
arbitrary
  shrink :: CUSeconds -> [CUSeconds]
shrink (CUSeconds Word32
x) = forall a b. (a -> b) -> [a] -> [b]
map Word32 -> CUSeconds
CUSeconds (forall a. Arbitrary a => a -> [a]
shrink Word32
x)

instance Arbitrary CSUSeconds where
  arbitrary :: Gen CSUSeconds
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int64 -> CSUSeconds
CSUSeconds forall a. Arbitrary a => Gen a
arbitrary
  shrink :: CSUSeconds -> [CSUSeconds]
shrink (CSUSeconds Int64
x) = forall a b. (a -> b) -> [a] -> [b]
map Int64 -> CSUSeconds
CSUSeconds (forall a. Arbitrary a => a -> [a]
shrink Int64
x)
#endif
#endif

instance Arbitrary CFloat where
  arbitrary :: Gen CFloat
arbitrary = forall a. Fractional a => Gen a
arbitrarySizedFractional
  shrink :: CFloat -> [CFloat]
shrink = forall a. RealFrac a => a -> [a]
shrinkDecimal

instance Arbitrary CDouble where
  arbitrary :: Gen CDouble
arbitrary = forall a. Fractional a => Gen a
arbitrarySizedFractional
  shrink :: CDouble -> [CDouble]
shrink = forall a. RealFrac a => a -> [a]
shrinkDecimal

-- Arbitrary instances for container types
instance (Ord a, Arbitrary a) => Arbitrary (Set.Set a) where
  arbitrary :: Gen (Set a)
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Ord a => [a] -> Set a
Set.fromList forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Set a -> [Set a]
shrink = forall a b. (a -> b) -> [a] -> [b]
map forall a. Ord a => [a] -> Set a
Set.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Set a -> [a]
Set.toList
instance (Ord k, Arbitrary k) => Arbitrary1 (Map.Map k) where
  liftArbitrary :: forall a. Gen a -> Gen (Map k a)
liftArbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary
  liftShrink :: forall a. (a -> [a]) -> Map k a -> [Map k a]
liftShrink a -> [a]
shr = forall a b. (a -> b) -> [a] -> [b]
map forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink (forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [(k, a)]
Map.toList
instance (Ord k, Arbitrary k, Arbitrary v) => Arbitrary (Map.Map k v) where
  arbitrary :: Gen (Map k v)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Map k v -> [Map k v]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1
instance Arbitrary IntSet.IntSet where
  arbitrary :: Gen IntSet
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Int] -> IntSet
IntSet.fromList forall a. Arbitrary a => Gen a
arbitrary
  shrink :: IntSet -> [IntSet]
shrink = forall a b. (a -> b) -> [a] -> [b]
map [Int] -> IntSet
IntSet.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntSet -> [Int]
IntSet.toList
instance Arbitrary1 IntMap.IntMap where
  liftArbitrary :: forall a. Gen a -> Gen (IntMap a)
liftArbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. [(Int, a)] -> IntMap a
IntMap.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary
  liftShrink :: forall a. (a -> [a]) -> IntMap a -> [IntMap a]
liftShrink a -> [a]
shr = forall a b. (a -> b) -> [a] -> [b]
map forall a. [(Int, a)] -> IntMap a
IntMap.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink (forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> [(Int, a)]
IntMap.toList
instance Arbitrary a => Arbitrary (IntMap.IntMap a) where
  arbitrary :: Gen (IntMap a)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: IntMap a -> [IntMap a]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1
instance Arbitrary1 Sequence.Seq where
  liftArbitrary :: forall a. Gen a -> Gen (Seq a)
liftArbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. [a] -> Seq a
Sequence.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary
  liftShrink :: forall a. (a -> [a]) -> Seq a -> [Seq a]
liftShrink a -> [a]
shr = forall a b. (a -> b) -> [a] -> [b]
map forall a. [a] -> Seq a
Sequence.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
instance Arbitrary a => Arbitrary (Sequence.Seq a) where
  arbitrary :: Gen (Seq a)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Seq a -> [Seq a]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1
instance Arbitrary1 Tree.Tree where
    liftArbitrary :: forall a. Gen a -> Gen (Tree a)
liftArbitrary Gen a
arb = forall a. (Int -> Gen a) -> Gen a
sized forall a b. (a -> b) -> a -> b
$ \Int
n -> do
        Int
k <- (Int, Int) -> Gen Int
chooseInt (Int
0, Int
n)
        Int -> Gen (Tree a)
go Int
k
      where
        go :: Int -> Gen (Tree a)
go Int
n = do -- n is the size of the trees.
            a
value <- Gen a
arb
            [Int]
pars <- Int -> Gen [Int]
arbPartition (Int
n forall a. Num a => a -> a -> a
- Int
1) -- can go negative!
            [Tree a]
forest <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Int -> Gen (Tree a)
go [Int]
pars
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> [Tree a] -> Tree a
Tree.Node a
value [Tree a]
forest

        arbPartition :: Int -> Gen [Int]
        arbPartition :: Int -> Gen [Int]
arbPartition Int
k = case forall a. Ord a => a -> a -> Ordering
compare Int
k Int
1 of
            Ordering
LT -> forall (f :: * -> *) a. Applicative f => a -> f a
pure []
            Ordering
EQ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure [Int
1]
            Ordering
GT -> do
                Int
first <- (Int, Int) -> Gen Int
chooseInt (Int
1, Int
k)
                [Int]
rest <- Int -> Gen [Int]
arbPartition forall a b. (a -> b) -> a -> b
$ Int
k forall a. Num a => a -> a -> a
- Int
first
                forall a. [a] -> Gen [a]
shuffle (Int
first forall a. a -> [a] -> [a]
: [Int]
rest)

    liftShrink :: forall a. (a -> [a]) -> Tree a -> [Tree a]
liftShrink a -> [a]
shr = Tree a -> [Tree a]
go
      where
        go :: Tree a -> [Tree a]
go (Tree.Node a
val [Tree a]
forest) = [Tree a]
forest forall a. [a] -> [a] -> [a]
++
            [ forall a. a -> [Tree a] -> Tree a
Tree.Node a
e [Tree a]
fs
            | (a
e, [Tree a]
fs) <- forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
shr (forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink Tree a -> [Tree a]
go) (a
val, [Tree a]
forest)
            ]
instance Arbitrary a => Arbitrary (Tree.Tree a) where
  arbitrary :: Gen (Tree a)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Tree a -> [Tree a]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1

-- Arbitrary instance for Ziplist
instance Arbitrary1 ZipList where
  liftArbitrary :: forall a. Gen a -> Gen (ZipList a)
liftArbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. [a] -> ZipList a
ZipList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary
  liftShrink :: forall a. (a -> [a]) -> ZipList a -> [ZipList a]
liftShrink a -> [a]
shr = forall a b. (a -> b) -> [a] -> [b]
map forall a. [a] -> ZipList a
ZipList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ZipList a -> [a]
getZipList
instance Arbitrary a => Arbitrary (ZipList a) where
  arbitrary :: Gen (ZipList a)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: ZipList a -> [ZipList a]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1

#ifndef NO_TRANSFORMERS
-- Arbitrary instance for transformers' Functors
instance Arbitrary1 Identity where
  liftArbitrary :: forall a. Gen a -> Gen (Identity a)
liftArbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Identity a
Identity
  liftShrink :: forall a. (a -> [a]) -> Identity a -> [Identity a]
liftShrink a -> [a]
shr = forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Identity a
Identity forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Identity a -> a
runIdentity
instance Arbitrary a => Arbitrary (Identity a) where
  arbitrary :: Gen (Identity a)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Identity a -> [Identity a]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1

instance Arbitrary2 Constant where
  liftArbitrary2 :: forall a b. Gen a -> Gen b -> Gen (Constant a b)
liftArbitrary2 Gen a
arbA Gen b
_ = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} a (b :: k). a -> Constant a b
Constant Gen a
arbA
  liftShrink2 :: forall a b.
(a -> [a]) -> (b -> [b]) -> Constant a b -> [Constant a b]
liftShrink2 a -> [a]
shrA b -> [b]
_ = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} a (b :: k). a -> Constant a b
Constant forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shrA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} a (b :: k). Constant a b -> a
getConstant
instance Arbitrary a => Arbitrary1 (Constant a) where
  liftArbitrary :: forall a. Gen a -> Gen (Constant a a)
liftArbitrary = forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 forall a. Arbitrary a => Gen a
arbitrary
  liftShrink :: forall a. (a -> [a]) -> Constant a a -> [Constant a a]
liftShrink = forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 forall a. Arbitrary a => a -> [a]
shrink
-- Have to be defined explicitly, as Constant is kind polymorphic
instance Arbitrary a => Arbitrary (Constant a b) where
  arbitrary :: Gen (Constant a b)
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} a (b :: k). a -> Constant a b
Constant forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Constant a b -> [Constant a b]
shrink = forall a b. (a -> b) -> [a] -> [b]
map forall {k} a (b :: k). a -> Constant a b
Constant forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} a (b :: k). Constant a b -> a
getConstant

instance (Arbitrary1 f, Arbitrary1 g) => Arbitrary1 (Product f g) where
  liftArbitrary :: forall a. Gen a -> Gen (Product f g a)
liftArbitrary Gen a
arb = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall {k} (f :: k -> *) (g :: k -> *) (a :: k).
f a -> g a -> Product f g a
Pair (forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary Gen a
arb) (forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary Gen a
arb)
  liftShrink :: forall a. (a -> [a]) -> Product f g a -> [Product f g a]
liftShrink a -> [a]
shr (Pair f a
f g a
g) =
    [ forall {k} (f :: k -> *) (g :: k -> *) (a :: k).
f a -> g a -> Product f g a
Pair f a
f' g a
g | f a
f' <- forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr f a
f ] forall a. [a] -> [a] -> [a]
++
    [ forall {k} (f :: k -> *) (g :: k -> *) (a :: k).
f a -> g a -> Product f g a
Pair f a
f g a
g' | g a
g' <- forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr g a
g ]
instance (Arbitrary1 f, Arbitrary1 g, Arbitrary a) => Arbitrary (Product f g a) where
  arbitrary :: Gen (Product f g a)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Product f g a -> [Product f g a]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1

instance (Arbitrary1 f, Arbitrary1 g) => Arbitrary1 (Compose f g) where
  liftArbitrary :: forall a. Gen a -> Gen (Compose f g a)
liftArbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary
  liftShrink :: forall a. (a -> [a]) -> Compose f g a -> [Compose f g a]
liftShrink a -> [a]
shr = forall a b. (a -> b) -> [a] -> [b]
map forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink (forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose
instance (Arbitrary1 f, Arbitrary1 g, Arbitrary a) => Arbitrary (Compose f g a) where
  arbitrary :: Gen (Compose f g a)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Compose f g a -> [Compose f g a]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1
#endif

-- Arbitrary instance for Const
instance Arbitrary2 Const where
  liftArbitrary2 :: forall a b. Gen a -> Gen b -> Gen (Const a b)
liftArbitrary2 Gen a
arbA Gen b
_ = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} a (b :: k). a -> Const a b
Const Gen a
arbA
  liftShrink2 :: forall a b. (a -> [a]) -> (b -> [b]) -> Const a b -> [Const a b]
liftShrink2 a -> [a]
shrA b -> [b]
_ = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} a (b :: k). a -> Const a b
Const forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shrA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} a (b :: k). Const a b -> a
getConst
instance Arbitrary a => Arbitrary1 (Const a) where
  liftArbitrary :: forall a. Gen a -> Gen (Const a a)
liftArbitrary = forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 forall a. Arbitrary a => Gen a
arbitrary
  liftShrink :: forall a. (a -> [a]) -> Const a a -> [Const a a]
liftShrink = forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 forall a. Arbitrary a => a -> [a]
shrink
-- Have to be defined explicitly, as Const is kind polymorphic
instance Arbitrary a => Arbitrary (Const a b) where
  arbitrary :: Gen (Const a b)
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} a (b :: k). a -> Const a b
Const forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Const a b -> [Const a b]
shrink = forall a b. (a -> b) -> [a] -> [b]
map forall {k} a (b :: k). a -> Const a b
Const forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} a (b :: k). Const a b -> a
getConst

instance Arbitrary (m a) => Arbitrary (WrappedMonad m a) where
  arbitrary :: Gen (WrappedMonad m a)
arbitrary = forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Arbitrary a => Gen a
arbitrary
  shrink :: WrappedMonad m a -> [WrappedMonad m a]
shrink (WrapMonad m a
a) = forall a b. (a -> b) -> [a] -> [b]
map forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (forall a. Arbitrary a => a -> [a]
shrink m a
a)

instance Arbitrary (a b c) => Arbitrary (WrappedArrow a b c) where
  arbitrary :: Gen (WrappedArrow a b c)
arbitrary = forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Arbitrary a => Gen a
arbitrary
  shrink :: WrappedArrow a b c -> [WrappedArrow a b c]
shrink (WrapArrow a b c
a) = forall a b. (a -> b) -> [a] -> [b]
map forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (forall a. Arbitrary a => a -> [a]
shrink a b c
a)

-- Arbitrary instances for Monoid
instance Arbitrary a => Arbitrary (Monoid.Dual a) where
  arbitrary :: Gen (Dual a)
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Dual a
Monoid.Dual forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Dual a -> [Dual a]
shrink = forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Dual a
Monoid.Dual forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Dual a -> a
Monoid.getDual

instance (Arbitrary a, CoArbitrary a) => Arbitrary (Monoid.Endo a) where
  arbitrary :: Gen (Endo a)
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. (a -> a) -> Endo a
Monoid.Endo forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Endo a -> [Endo a]
shrink = forall a b. (a -> b) -> [a] -> [b]
map forall a. (a -> a) -> Endo a
Monoid.Endo forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Endo a -> a -> a
Monoid.appEndo

instance Arbitrary Monoid.All where
  arbitrary :: Gen All
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> All
Monoid.All forall a. Arbitrary a => Gen a
arbitrary
  shrink :: All -> [All]
shrink = forall a b. (a -> b) -> [a] -> [b]
map Bool -> All
Monoid.All forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. All -> Bool
Monoid.getAll

instance Arbitrary Monoid.Any where
  arbitrary :: Gen Any
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> Any
Monoid.Any forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Any -> [Any]
shrink = forall a b. (a -> b) -> [a] -> [b]
map Bool -> Any
Monoid.Any forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. Any -> Bool
Monoid.getAny

instance Arbitrary a => Arbitrary (Monoid.Sum a) where
  arbitrary :: Gen (Sum a)
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Sum a
Monoid.Sum forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Sum a -> [Sum a]
shrink = forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Sum a
Monoid.Sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Sum a -> a
Monoid.getSum

instance Arbitrary a => Arbitrary (Monoid.Product a) where
  arbitrary :: Gen (Product a)
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Product a
Monoid.Product  forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Product a -> [Product a]
shrink = forall a b. (a -> b) -> [a] -> [b]
map forall a. a -> Product a
Monoid.Product  forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Product a -> a
Monoid.getProduct

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(3,0,0)
instance Arbitrary a => Arbitrary (Monoid.First a) where
  arbitrary :: Gen (First a)
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Maybe a -> First a
Monoid.First forall a. Arbitrary a => Gen a
arbitrary
  shrink :: First a -> [First a]
shrink = forall a b. (a -> b) -> [a] -> [b]
map forall a. Maybe a -> First a
Monoid.First forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. First a -> Maybe a
Monoid.getFirst

instance Arbitrary a => Arbitrary (Monoid.Last a) where
  arbitrary :: Gen (Last a)
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Maybe a -> Last a
Monoid.Last forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Last a -> [Last a]
shrink = forall a b. (a -> b) -> [a] -> [b]
map forall a. Maybe a -> Last a
Monoid.Last forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Last a -> Maybe a
Monoid.getLast
#endif

#if MIN_VERSION_base(4,8,0)
instance Arbitrary (f a) => Arbitrary (Monoid.Alt f a) where
  arbitrary :: Gen (Alt f a)
arbitrary = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} (f :: k -> *) (a :: k). f a -> Alt f a
Monoid.Alt forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Alt f a -> [Alt f a]
shrink = forall a b. (a -> b) -> [a] -> [b]
map forall {k} (f :: k -> *) (a :: k). f a -> Alt f a
Monoid.Alt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Arbitrary a => a -> [a]
shrink forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k). Alt f a -> f a
Monoid.getAlt
#endif
#endif

-- | Generates 'Version' with non-empty non-negative @versionBranch@, and empty @versionTags@
instance Arbitrary Version where
  arbitrary :: Gen Version
arbitrary = forall a. (Int -> Gen a) -> Gen a
sized forall a b. (a -> b) -> a -> b
$ \Int
n ->
    do Int
k <- (Int, Int) -> Gen Int
chooseInt (Int
0, Int -> Int
log2 Int
n)
       [Int]
xs <- forall a. Int -> Gen a -> Gen [a]
vectorOf (Int
kforall a. Num a => a -> a -> a
+Int
1) forall a. Integral a => Gen a
arbitrarySizedNatural
       forall (m :: * -> *) a. Monad m => a -> m a
return ([Int] -> [[Char]] -> Version
Version [Int]
xs [])
    where
      log2 :: Int -> Int
      log2 :: Int -> Int
log2 Int
n | Int
n forall a. Ord a => a -> a -> Bool
<= Int
1 = Int
0
             | Bool
otherwise = Int
1 forall a. Num a => a -> a -> a
+ Int -> Int
log2 (Int
n forall a. Integral a => a -> a -> a
`div` Int
2)

  shrink :: Version -> [Version]
shrink (Version [Int]
xs [[Char]]
_) =
    [ [Int] -> [[Char]] -> Version
Version [Int]
xs' []
    | [Int]
xs' <- forall a. Arbitrary a => a -> [a]
shrink [Int]
xs
    , forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
xs' forall a. Ord a => a -> a -> Bool
> Int
0
    , forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall a. Ord a => a -> a -> Bool
>=Int
0) [Int]
xs'
    ]

instance Arbitrary QCGen where
  arbitrary :: Gen QCGen
arbitrary = forall a. (QCGen -> Int -> a) -> Gen a
MkGen (\QCGen
g Int
_ -> QCGen
g)

instance Arbitrary ExitCode where
  arbitrary :: Gen ExitCode
arbitrary = forall a. [(Int, Gen a)] -> Gen a
frequency [(Int
1, forall (m :: * -> *) a. Monad m => a -> m a
return ExitCode
ExitSuccess), (Int
3, forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Int -> ExitCode
ExitFailure forall a. Arbitrary a => Gen a
arbitrary)]

  shrink :: ExitCode -> [ExitCode]
shrink (ExitFailure Int
x) = ExitCode
ExitSuccess forall a. a -> [a] -> [a]
: [ Int -> ExitCode
ExitFailure Int
x' | Int
x' <- forall a. Arbitrary a => a -> [a]
shrink Int
x ]
  shrink ExitCode
_        = []

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,2,0)
instance Arbitrary Newline where
  arbitrary :: Gen Newline
arbitrary = forall a. [a] -> Gen a
elements [Newline
LF, Newline
CRLF]

  -- The behavior of code for LF is generally simpler than for CRLF
  -- See the documentation for this type, which states that Haskell
  -- Internally always assumes newlines are \n and this type represents
  -- how to translate that to and from the outside world, where LF means
  -- no translation.
  shrink :: Newline -> [Newline]
shrink Newline
LF = []
  shrink Newline
CRLF = [Newline
LF]

instance Arbitrary NewlineMode where
  arbitrary :: Gen NewlineMode
arbitrary = Newline -> Newline -> NewlineMode
NewlineMode forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Arbitrary a => Gen a
arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Arbitrary a => Gen a
arbitrary

  shrink :: NewlineMode -> [NewlineMode]
shrink (NewlineMode Newline
inNL Newline
outNL) = [Newline -> Newline -> NewlineMode
NewlineMode Newline
inNL' Newline
outNL' | (Newline
inNL', Newline
outNL') <- forall a. Arbitrary a => a -> [a]
shrink (Newline
inNL, Newline
outNL)]
#endif
#endif

-- ** Helper functions for implementing arbitrary

-- | Apply a binary function to random arguments.
applyArbitrary2 :: (Arbitrary a, Arbitrary b) => (a -> b -> r) -> Gen r
applyArbitrary2 :: forall a b r. (Arbitrary a, Arbitrary b) => (a -> b -> r) -> Gen r
applyArbitrary2 a -> b -> r
f = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> r
f forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary

-- | Apply a ternary function to random arguments.
applyArbitrary3
  :: (Arbitrary a, Arbitrary b, Arbitrary c)
  => (a -> b -> c -> r) -> Gen r
applyArbitrary3 :: forall a b c r.
(Arbitrary a, Arbitrary b, Arbitrary c) =>
(a -> b -> c -> r) -> Gen r
applyArbitrary3 a -> b -> c -> r
f = forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 a -> b -> c -> r
f forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary

-- | Apply a function of arity 4 to random arguments.
applyArbitrary4
  :: (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d)
  => (a -> b -> c -> d -> r) -> Gen r
applyArbitrary4 :: forall a b c d r.
(Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d) =>
(a -> b -> c -> d -> r) -> Gen r
applyArbitrary4 a -> b -> c -> d -> r
f = forall a b c r.
(Arbitrary a, Arbitrary b, Arbitrary c) =>
(a -> b -> c -> r) -> Gen r
applyArbitrary3 (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> c -> d -> r
f)

-- | Generates an integral number. The number can be positive or negative
-- and its maximum absolute value depends on the size parameter.
arbitrarySizedIntegral :: Integral a => Gen a
arbitrarySizedIntegral :: forall a. Integral a => Gen a
arbitrarySizedIntegral =
  forall a. (Int -> Gen a) -> Gen a
sized forall a b. (a -> b) -> a -> b
$ \Int
n ->
  forall a. Integral a => (Int -> a) -> Gen Int -> Gen a
inBounds forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Int, Int) -> Gen Int
chooseInt (-Int
n, Int
n))

-- | Generates a natural number. The number's maximum value depends on
-- the size parameter.
arbitrarySizedNatural :: Integral a => Gen a
arbitrarySizedNatural :: forall a. Integral a => Gen a
arbitrarySizedNatural =
  forall a. (Int -> Gen a) -> Gen a
sized forall a b. (a -> b) -> a -> b
$ \Int
n ->
  forall a. Integral a => (Int -> a) -> Gen Int -> Gen a
inBounds forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Int, Int) -> Gen Int
chooseInt (Int
0, Int
n))

inBounds :: Integral a => (Int -> a) -> Gen Int -> Gen a
inBounds :: forall a. Integral a => (Int -> a) -> Gen Int -> Gen a
inBounds Int -> a
fi Gen Int
g = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> a
fi (Gen Int
g forall a. Gen a -> (a -> Bool) -> Gen a
`suchThat` (\Int
x -> forall a. Integral a => a -> Integer
toInteger Int
x forall a. Eq a => a -> a -> Bool
== forall a. Integral a => a -> Integer
toInteger (Int -> a
fi Int
x)))

-- | Uniformly generates a fractional number. The number can be positive or negative
-- and its maximum absolute value depends on the size parameter.
arbitrarySizedFractional :: Fractional a => Gen a
arbitrarySizedFractional :: forall a. Fractional a => Gen a
arbitrarySizedFractional =
  forall a. (Int -> Gen a) -> Gen a
sized forall a b. (a -> b) -> a -> b
$ \Int
n -> do
    Int
denom <- (Int, Int) -> Gen Int
chooseInt (Int
1, forall a. Ord a => a -> a -> a
max Int
1 Int
n)
    Int
numer <- (Int, Int) -> Gen Int
chooseInt (-Int
nforall a. Num a => a -> a -> a
*Int
denom, Int
nforall a. Num a => a -> a -> a
*Int
denom)
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
numer forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
denom

-- Useful for getting at minBound and maxBound without having to
-- fiddle around with asTypeOf.
{-# INLINE withBounds #-}
withBounds :: Bounded a => (a -> a -> Gen a) -> Gen a
withBounds :: forall a. Bounded a => (a -> a -> Gen a) -> Gen a
withBounds a -> a -> Gen a
k = a -> a -> Gen a
k forall a. Bounded a => a
minBound forall a. Bounded a => a
maxBound

-- | Generates an integral number. The number is chosen uniformly from
-- the entire range of the type. You may want to use
-- 'arbitrarySizedBoundedIntegral' instead.
arbitraryBoundedIntegral :: (Bounded a, Integral a) => Gen a
arbitraryBoundedIntegral :: forall a. (Bounded a, Integral a) => Gen a
arbitraryBoundedIntegral = forall a. (Bounded a, Integral a) => (a, a) -> Gen a
chooseBoundedIntegral (forall a. Bounded a => a
minBound, forall a. Bounded a => a
maxBound)

-- | Generates an element of a bounded type. The element is
-- chosen from the entire range of the type.
arbitraryBoundedRandom :: (Bounded a, Random a) => Gen a
arbitraryBoundedRandom :: forall a. (Bounded a, Random a) => Gen a
arbitraryBoundedRandom = forall a. Random a => (a, a) -> Gen a
choose (forall a. Bounded a => a
minBound,forall a. Bounded a => a
maxBound)

-- | Generates an element of a bounded enumeration.
arbitraryBoundedEnum :: (Bounded a, Enum a) => Gen a
arbitraryBoundedEnum :: forall a. (Bounded a, Enum a) => Gen a
arbitraryBoundedEnum = forall a. Enum a => (a, a) -> Gen a
chooseEnum (forall a. Bounded a => a
minBound, forall a. Bounded a => a
maxBound)

-- | Generates an integral number from a bounded domain. The number is
-- chosen from the entire range of the type, but small numbers are
-- generated more often than big numbers. Inspired by demands from
-- Phil Wadler.
arbitrarySizedBoundedIntegral :: (Bounded a, Integral a) => Gen a
-- INLINEABLE so that this combinator gets specialised at each type,
-- which means that the constant 'bits' in the let-block below will
-- only be computed once.
{-# INLINEABLE arbitrarySizedBoundedIntegral #-}
arbitrarySizedBoundedIntegral :: forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral =
  forall a. Bounded a => (a -> a -> Gen a) -> Gen a
withBounds forall a b. (a -> b) -> a -> b
$ \a
mn a
mx ->
  let ilog2 :: t -> a
ilog2 t
1 = a
0
      ilog2 t
n | t
n forall a. Ord a => a -> a -> Bool
> t
0 = a
1 forall a. Num a => a -> a -> a
+ t -> a
ilog2 (t
n forall a. Integral a => a -> a -> a
`div` t
2)

      -- How many bits are needed to represent this type?
      -- (This number is an upper bound, not exact.)
      bits :: Int
bits = forall {t} {a}. (Num a, Integral t) => t -> a
ilog2 (forall a. Integral a => a -> Integer
toInteger a
mx forall a. Num a => a -> a -> a
- forall a. Integral a => a -> Integer
toInteger a
mn forall a. Num a => a -> a -> a
+ Integer
1) in
  forall a. (Int -> Gen a) -> Gen a
sized forall a b. (a -> b) -> a -> b
$ \Int
k ->
    let
      -- Reach maximum size by k=80, or quicker for small integer types
      power :: Int
power = ((Int
bits forall a. Ord a => a -> a -> a
`max` Int
40) forall a. Num a => a -> a -> a
* Int
k) forall a. Integral a => a -> a -> a
`div` Int
80

      -- Bounds should be 2^power, but:
      --   * clamp the result to minBound/maxBound
      --   * clamp power to 'bits', in case k is a huge number
      lo :: Integer
lo = forall a. Integral a => a -> Integer
toInteger a
mn forall a. Ord a => a -> a -> a
`max` (-Integer
1 forall a. Bits a => a -> Int -> a
`shiftL` (Int
power forall a. Ord a => a -> a -> a
`min` Int
bits))
      hi :: Integer
hi = forall a. Integral a => a -> Integer
toInteger a
mx forall a. Ord a => a -> a -> a
`min` (Integer
1 forall a. Bits a => a -> Int -> a
`shiftL` (Int
power forall a. Ord a => a -> a -> a
`min` Int
bits)) in
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => Integer -> a
fromInteger ((Integer, Integer) -> Gen Integer
chooseInteger (Integer
lo, Integer
hi))

-- ** Generators for various kinds of character

-- | Generates any Unicode character (but not a surrogate)
arbitraryUnicodeChar :: Gen Char
arbitraryUnicodeChar :: Gen Char
arbitraryUnicodeChar =
  forall a. (Bounded a, Enum a) => Gen a
arbitraryBoundedEnum forall a. Gen a -> (a -> Bool) -> Gen a
`suchThat` Char -> Bool
isValidUnicode
  where
    isValidUnicode :: Char -> Bool
isValidUnicode Char
c = case Char -> GeneralCategory
generalCategory Char
c of
      GeneralCategory
Surrogate -> Bool
False
      GeneralCategory
NotAssigned -> Bool
False
      GeneralCategory
_ -> Bool
True

-- | Generates a random ASCII character (0-127).
arbitraryASCIIChar :: Gen Char
arbitraryASCIIChar :: Gen Char
arbitraryASCIIChar = forall a. Enum a => (a, a) -> Gen a
chooseEnum (Char
'\0', Char
'\127')

-- | Generates a printable Unicode character.
arbitraryPrintableChar :: Gen Char
arbitraryPrintableChar :: Gen Char
arbitraryPrintableChar = forall a. Arbitrary a => Gen a
arbitrary forall a. Gen a -> (a -> Bool) -> Gen a
`suchThat` Char -> Bool
isPrint

-- ** Helper functions for implementing shrink

-- | Returns no shrinking alternatives.
shrinkNothing :: a -> [a]
shrinkNothing :: forall a. a -> [a]
shrinkNothing a
_ = []

-- | Map a shrink function to another domain. This is handy if your data type
-- has special invariants, but is /almost/ isomorphic to some other type.
--
-- @
-- shrinkOrderedList :: (Ord a, Arbitrary a) => [a] -> [[a]]
-- shrinkOrderedList = shrinkMap sort id
--
-- shrinkSet :: (Ord a, Arbitrary a) => Set a -> [Set a]
-- shrinkSet = shrinkMap fromList toList
-- @
shrinkMap :: Arbitrary a => (a -> b) -> (b -> a) -> b -> [b]
shrinkMap :: forall a b. Arbitrary a => (a -> b) -> (b -> a) -> b -> [b]
shrinkMap a -> b
f b -> a
g = forall a b. (a -> b) -> (b -> a) -> (a -> [a]) -> b -> [b]
shrinkMapBy a -> b
f b -> a
g forall a. Arbitrary a => a -> [a]
shrink

-- | Non-overloaded version of `shrinkMap`.
shrinkMapBy :: (a -> b) -> (b -> a) -> (a -> [a]) -> b -> [b]
shrinkMapBy :: forall a b. (a -> b) -> (b -> a) -> (a -> [a]) -> b -> [b]
shrinkMapBy a -> b
f b -> a
g a -> [a]
shr = forall a b. (a -> b) -> [a] -> [b]
map a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shr forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
g

-- | Shrink an integral number.
shrinkIntegral :: Integral a => a -> [a]
shrinkIntegral :: forall a. Integral a => a -> [a]
shrinkIntegral a
x =
  forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$
  [ -a
x
  | a
x forall a. Ord a => a -> a -> Bool
< a
0, -a
x forall a. Ord a => a -> a -> Bool
> a
x
  ] forall a. [a] -> [a] -> [a]
++
  [ a
x'
  | a
x' <- forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall {a}. (Ord a, Num a) => a -> a -> Bool
<< a
x) (a
0forall a. a -> [a] -> [a]
:[ a
x forall a. Num a => a -> a -> a
- a
i | a
i <- forall a. [a] -> [a]
tail (forall a. (a -> a) -> a -> [a]
iterate (forall a. Integral a => a -> a -> a
`quot` a
2) a
x) ])
  ]
 where
   -- a << b is "morally" abs a < abs b, but taking care of overflow.
   a
a << :: a -> a -> Bool
<< a
b = case (a
a forall a. Ord a => a -> a -> Bool
>= a
0, a
b forall a. Ord a => a -> a -> Bool
>= a
0) of
            (Bool
True,  Bool
True)  -> a
a forall a. Ord a => a -> a -> Bool
< a
b
            (Bool
False, Bool
False) -> a
a forall a. Ord a => a -> a -> Bool
> a
b
            (Bool
True,  Bool
False) -> a
a forall a. Num a => a -> a -> a
+ a
b forall a. Ord a => a -> a -> Bool
< a
0
            (Bool
False, Bool
True)  -> a
a forall a. Num a => a -> a -> a
+ a
b forall a. Ord a => a -> a -> Bool
> a
0

-- | Shrink an element of a bounded enumeration.
--
-- === __Example__
--
-- @
-- data MyEnum = E0 | E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9
--    deriving (Bounded, Enum, Eq, Ord, Show)
-- @
--
-- >>> shrinkBoundedEnum E9
-- [E0,E5,E7,E8]
--
-- >>> shrinkBoundedEnum E5
-- [E0,E3,E4]
--
-- >>> shrinkBoundedEnum E0
-- []
--
shrinkBoundedEnum :: (Bounded a, Enum a, Eq a) => a -> [a]
shrinkBoundedEnum :: forall a. (Bounded a, Enum a, Eq a) => a -> [a]
shrinkBoundedEnum a
a
  | a
a forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
minBound =
    []
  | Bool
otherwise =
    forall a. Enum a => Int -> a
toEnum forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Ord a => a -> a -> Bool
>= Int
minBoundInt) (forall a. Integral a => a -> [a]
shrinkIntegral forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> Int
fromEnum a
a)
  where
    minBoundInt :: Int
    minBoundInt :: Int
minBoundInt = forall a. Enum a => a -> Int
fromEnum (forall a. Bounded a => a
minBound forall a. a -> a -> a
`asTypeOf` a
a)

-- | Shrink a fraction, preferring numbers with smaller
-- numerators or denominators. See also 'shrinkDecimal'.
shrinkRealFrac :: RealFrac a => a -> [a]
shrinkRealFrac :: forall a. RealFrac a => a -> [a]
shrinkRealFrac a
x
  | Bool -> Bool
not (a
x forall a. Eq a => a -> a -> Bool
== a
x)  = a
0 forall a. a -> [a] -> [a]
: forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Ord a => a -> a -> Bool
< a
1000) [a]
numbers -- NaN
  | a
x forall a. Ord a => a -> a -> Bool
> a
0 Bool -> Bool -> Bool
&& Bool -> Bool
not (a
2forall a. Num a => a -> a -> a
*a
xforall a. Num a => a -> a -> a
+a
1forall a. Ord a => a -> a -> Bool
>a
x) = a
0 forall a. a -> [a] -> [a]
: forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Ord a => a -> a -> Bool
<a
x) [a]
numbers -- infinity
  | a
x forall a. Ord a => a -> a -> Bool
< a
0 = forall a. Num a => a -> a
negate a
xforall a. a -> [a] -> [a]
:forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => a -> a
negate (forall a. RealFrac a => a -> [a]
shrinkRealFrac (forall a. Num a => a -> a
negate a
x))
  | Bool
otherwise = -- x is finite and >= 0
    -- To ensure termination
    forall a. (a -> Bool) -> [a] -> [a]
filter (\a
y -> forall a. Num a => a -> a
abs a
y forall a. Ord a => a -> a -> Bool
< forall a. Num a => a -> a
abs a
x) forall a b. (a -> b) -> a -> b
$
      -- Try shrinking to an integer first
      forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger (forall a. Arbitrary a => a -> [a]
shrink (forall a b. (RealFrac a, Integral b) => a -> b
truncate a
x) forall a. [a] -> [a] -> [a]
++ [forall a b. (RealFrac a, Integral b) => a -> b
truncate a
x]) forall a. [a] -> [a] -> [a]
++
      -- Shrink the numerator
      [forall a. Fractional a => Rational -> a
fromRational (Integer
num' forall a. Integral a => a -> a -> Ratio a
% Integer
denom) | Integer
num' <- forall a. Arbitrary a => a -> [a]
shrink Integer
num] forall a. [a] -> [a] -> [a]
++
      -- Shrink the denominator, and keep the fraction as close
      -- to the original as possible, rounding towards zero
      [forall a. Fractional a => Rational -> a
fromRational (forall a b. (RealFrac a, Integral b) => a -> b
truncate (Integer
num forall a. Num a => a -> a -> a
* Integer
denom' forall a. Integral a => a -> a -> Ratio a
% Integer
denom) forall a. Integral a => a -> a -> Ratio a
% Integer
denom')
      | Integer
denom' <- forall a. Arbitrary a => a -> [a]
shrink Integer
denom, Integer
denom' forall a. Eq a => a -> a -> Bool
/= Integer
0 ]
  where
    num :: Integer
num = forall a. Ratio a -> a
numerator (forall a. Real a => a -> Rational
toRational a
x)
    denom :: Integer
denom = forall a. Ratio a -> a
denominator (forall a. Real a => a -> Rational
toRational a
x)
    numbers :: [a]
numbers = forall a. (a -> a) -> a -> [a]
iterate (forall a. Num a => a -> a -> a
*a
2) a
1

-- | Shrink a real number, preferring numbers with shorter
-- decimal representations. See also 'shrinkRealFrac'.
shrinkDecimal :: RealFrac a => a -> [a]
shrinkDecimal :: forall a. RealFrac a => a -> [a]
shrinkDecimal a
x
  | Bool -> Bool
not (a
x forall a. Eq a => a -> a -> Bool
== a
x)  = a
0 forall a. a -> [a] -> [a]
: forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Ord a => a -> a -> Bool
< a
1000) [a]
numbers -- NaN
  | Bool -> Bool
not (a
2forall a. Num a => a -> a -> a
*forall a. Num a => a -> a
abs a
xforall a. Num a => a -> a -> a
+a
1forall a. Ord a => a -> a -> Bool
>forall a. Num a => a -> a
abs a
x) = a
0 forall a. a -> [a] -> [a]
: forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Ord a => a -> a -> Bool
<a
x) [a]
numbers -- infinity
  | a
x forall a. Ord a => a -> a -> Bool
< a
0 = forall a. Num a => a -> a
negate a
xforall a. a -> [a] -> [a]
:forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => a -> a
negate (forall a. RealFrac a => a -> [a]
shrinkDecimal (forall a. Num a => a -> a
negate a
x))
  | Bool
otherwise = -- x is finite and >= 0
    -- e.g. shrink pi =
    --   shrink 3 ++ map (/ 10) (shrink 31) ++
    --   map (/ 100) (shrink 314) + ...,
    -- where the inner calls to shrink use integer shrinking.
    [ a
y
    | Rational
precision <- forall a. Int -> [a] -> [a]
take Int
6 (forall a. (a -> a) -> a -> [a]
iterate (forall a. Num a => a -> a -> a
*Rational
10) Rational
1),
      let m :: Integer
m = forall a b. (RealFrac a, Integral b) => a -> b
round (forall a. Real a => a -> Rational
toRational a
x forall a. Num a => a -> a -> a
* Rational
precision),
      Rational
precision forall a. Eq a => a -> a -> Bool
== Rational
1 Bool -> Bool -> Bool
|| Integer
m forall a. Integral a => a -> a -> a
`mod` Integer
10 forall a. Eq a => a -> a -> Bool
/= Integer
0, -- don't allow shrinking to increase digits
      Integer
n <- Integer
mforall a. a -> [a] -> [a]
:forall a. Arbitrary a => a -> [a]
shrink Integer
m,
      let y :: a
y = forall a. Fractional a => Rational -> a
fromRational (forall a. Num a => Integer -> a
fromInteger Integer
n forall a. Fractional a => a -> a -> a
/ Rational
precision),
      forall a. Num a => a -> a
abs a
y forall a. Ord a => a -> a -> Bool
< forall a. Num a => a -> a
abs a
x ]
  where
    -- 1, 2, 3, ..., 10, 20, 30, ..., 100, 200, 300, etc.
    numbers :: [a]
numbers = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a. (a -> a) -> a -> [a]
iterate (forall a b. (a -> b) -> [a] -> [b]
map (forall a. Num a => a -> a -> a
*a
10)) (forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger [Integer
1..Integer
9])

--------------------------------------------------------------------------
-- ** CoArbitrary

#ifndef NO_GENERICS
-- | Used for random generation of functions.
-- You should consider using 'Test.QuickCheck.Fun' instead, which
-- can show the generated functions as strings.
--
-- If you are using a recent GHC, there is a default definition of
-- 'coarbitrary' using 'genericCoarbitrary', so if your type has a
-- 'Generic' instance it's enough to say
--
-- > instance CoArbitrary MyType
--
-- You should only use 'genericCoarbitrary' for data types where
-- equality is structural, i.e. if you can't have two different
-- representations of the same value. An example where it's not
-- safe is sets implemented using binary search trees: the same
-- set can be represented as several different trees.
-- Here you would have to explicitly define
-- @coarbitrary s = coarbitrary (toList s)@.
#else
-- | Used for random generation of functions.
#endif
class CoArbitrary a where
  -- | Used to generate a function of type @a -> b@.
  -- The first argument is a value, the second a generator.
  -- You should use 'variant' to perturb the random generator;
  -- the goal is that different values for the first argument will
  -- lead to different calls to 'variant'. An example will help:
  --
  -- @
  -- instance CoArbitrary a => CoArbitrary [a] where
  --   coarbitrary []     = 'variant' 0
  --   coarbitrary (x:xs) = 'variant' 1 . coarbitrary (x,xs)
  -- @
  coarbitrary :: a -> Gen b -> Gen b
#ifndef NO_GENERICS
  default coarbitrary :: (Generic a, GCoArbitrary (Rep a)) => a -> Gen b -> Gen b
  coarbitrary = forall a b.
(Generic a, GCoArbitrary (Rep a)) =>
a -> Gen b -> Gen b
genericCoarbitrary

-- | Generic CoArbitrary implementation.
genericCoarbitrary :: (Generic a, GCoArbitrary (Rep a)) => a -> Gen b -> Gen b
genericCoarbitrary :: forall a b.
(Generic a, GCoArbitrary (Rep a)) =>
a -> Gen b -> Gen b
genericCoarbitrary = forall {k} (f :: k -> *) (a :: k) b.
GCoArbitrary f =>
f a -> Gen b -> Gen b
gCoarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a x. Generic a => a -> Rep a x
from

class GCoArbitrary f where
  gCoarbitrary :: f a -> Gen b -> Gen b

instance GCoArbitrary U1 where
  gCoarbitrary :: forall (a :: k) b. U1 a -> Gen b -> Gen b
gCoarbitrary U1 a
U1 = forall a. a -> a
id

instance (GCoArbitrary f, GCoArbitrary g) => GCoArbitrary (f :*: g) where
  -- Like the instance for tuples.
  gCoarbitrary :: forall (a :: k) b. (:*:) f g a -> Gen b -> Gen b
gCoarbitrary (f a
l :*: g a
r) = forall {k} (f :: k -> *) (a :: k) b.
GCoArbitrary f =>
f a -> Gen b -> Gen b
gCoarbitrary f a
l forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k) b.
GCoArbitrary f =>
f a -> Gen b -> Gen b
gCoarbitrary g a
r

instance (GCoArbitrary f, GCoArbitrary g) => GCoArbitrary (f :+: g) where
  -- Like the instance for Either.
  gCoarbitrary :: forall (a :: k) b. (:+:) f g a -> Gen b -> Gen b
gCoarbitrary (L1 f a
x) = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k) b.
GCoArbitrary f =>
f a -> Gen b -> Gen b
gCoarbitrary f a
x
  gCoarbitrary (R1 g a
x) = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k) b.
GCoArbitrary f =>
f a -> Gen b -> Gen b
gCoarbitrary g a
x

instance GCoArbitrary f => GCoArbitrary (M1 i c f) where
  gCoarbitrary :: forall (a :: k) b. M1 i c f a -> Gen b -> Gen b
gCoarbitrary (M1 f a
x) = forall {k} (f :: k -> *) (a :: k) b.
GCoArbitrary f =>
f a -> Gen b -> Gen b
gCoarbitrary f a
x

instance CoArbitrary a => GCoArbitrary (K1 i a) where
  gCoarbitrary :: forall (a :: k) b. K1 i a a -> Gen b -> Gen b
gCoarbitrary (K1 a
x) = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
#endif

{-# DEPRECATED (><) "Use ordinary function composition instead" #-}
-- | Combine two generator perturbing functions, for example the
-- results of calls to 'variant' or 'coarbitrary'.
(><) :: (Gen a -> Gen a) -> (Gen a -> Gen a) -> (Gen a -> Gen a)
>< :: forall a. (Gen a -> Gen a) -> (Gen a -> Gen a) -> Gen a -> Gen a
(><) = forall b c a. (b -> c) -> (a -> b) -> a -> c
(.)

instance (Arbitrary a, CoArbitrary b) => CoArbitrary (a -> b) where
  coarbitrary :: forall b. (a -> b) -> Gen b -> Gen b
coarbitrary a -> b
f Gen b
gen =
    do [a]
xs <- forall a. Arbitrary a => Gen a
arbitrary
       forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (forall a b. (a -> b) -> [a] -> [b]
map a -> b
f [a]
xs) Gen b
gen

instance CoArbitrary () where
  coarbitrary :: forall b. () -> Gen b -> Gen b
coarbitrary ()
_ = forall a. a -> a
id

instance CoArbitrary Bool where
  coarbitrary :: forall b. Bool -> Gen b -> Gen b
coarbitrary Bool
False = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
  coarbitrary Bool
True  = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1

instance CoArbitrary Ordering where
  coarbitrary :: forall b. Ordering -> Gen b -> Gen b
coarbitrary Ordering
GT = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
  coarbitrary Ordering
EQ = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1
  coarbitrary Ordering
LT = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
2

instance CoArbitrary a => CoArbitrary (Maybe a) where
  coarbitrary :: forall b. Maybe a -> Gen b -> Gen b
coarbitrary Maybe a
Nothing  = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
  coarbitrary (Just a
x) = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x

instance (CoArbitrary a, CoArbitrary b) => CoArbitrary (Either a b) where
  coarbitrary :: forall b. Either a b -> Gen b -> Gen b
coarbitrary (Left a
x)  = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
  coarbitrary (Right b
y) = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary b
y

instance CoArbitrary a => CoArbitrary [a] where
  coarbitrary :: forall b. [a] -> Gen b -> Gen b
coarbitrary []     = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
  coarbitrary (a
x:[a]
xs) = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (a
x,[a]
xs)

instance (Integral a, CoArbitrary a) => CoArbitrary (Ratio a) where
  coarbitrary :: forall b. Ratio a -> Gen b -> Gen b
coarbitrary Ratio a
r = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (forall a. Ratio a -> a
numerator Ratio a
r,forall a. Ratio a -> a
denominator Ratio a
r)

#ifndef NO_FIXED
instance HasResolution a => CoArbitrary (Fixed a) where
  coarbitrary :: forall b. Fixed a -> Gen b -> Gen b
coarbitrary = forall a b. Real a => a -> Gen b -> Gen b
coarbitraryReal
#endif

#if defined(MIN_VERSION_base) && MIN_VERSION_base(4,4,0)
instance CoArbitrary a => CoArbitrary (Complex a) where
#else
instance (RealFloat a, CoArbitrary a) => CoArbitrary (Complex a) where
#endif
  coarbitrary :: forall b. Complex a -> Gen b -> Gen b
coarbitrary (a
x :+ a
y) = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
y

instance (CoArbitrary a, CoArbitrary b)
      => CoArbitrary (a,b)
 where
  coarbitrary :: forall b. (a, b) -> Gen b -> Gen b
coarbitrary (a
x,b
y) = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
                    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary b
y

instance (CoArbitrary a, CoArbitrary b, CoArbitrary c)
      => CoArbitrary (a,b,c)
 where
  coarbitrary :: forall b. (a, b, c) -> Gen b -> Gen b
coarbitrary (a
x,b
y,c
z) = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
                      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary b
y
                      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary c
z

instance (CoArbitrary a, CoArbitrary b, CoArbitrary c, CoArbitrary d)
      => CoArbitrary (a,b,c,d)
 where
  coarbitrary :: forall b. (a, b, c, d) -> Gen b -> Gen b
coarbitrary (a
x,b
y,c
z,d
v) = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
                        forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary b
y
                        forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary c
z
                        forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary d
v

instance (CoArbitrary a, CoArbitrary b, CoArbitrary c, CoArbitrary d, CoArbitrary e)
      => CoArbitrary (a,b,c,d,e)
 where
  coarbitrary :: forall b. (a, b, c, d, e) -> Gen b -> Gen b
coarbitrary (a
x,b
y,c
z,d
v,e
w) = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
                          forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary b
y
                          forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary c
z
                          forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary d
v
                          forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary e
w

-- typical instance for primitive (numerical) types

instance CoArbitrary Integer where
  coarbitrary :: forall b. Integer -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Int where
  coarbitrary :: forall b. Int -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Int8 where
  coarbitrary :: forall b. Int8 -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Int16 where
  coarbitrary :: forall b. Int16 -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Int32 where
  coarbitrary :: forall b. Int32 -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Int64 where
  coarbitrary :: forall b. Int64 -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Word where
  coarbitrary :: forall b. Word -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Word8 where
  coarbitrary :: forall b. Word8 -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Word16 where
  coarbitrary :: forall b. Word16 -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Word32 where
  coarbitrary :: forall b. Word32 -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Word64 where
  coarbitrary :: forall b. Word64 -> Gen b -> Gen b
coarbitrary = forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

instance CoArbitrary Char where
  coarbitrary :: forall b. Char -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord

instance CoArbitrary Float where
  coarbitrary :: forall b. Float -> Gen b -> Gen b
coarbitrary = forall a b. Real a => a -> Gen b -> Gen b
coarbitraryReal

instance CoArbitrary Double where
  coarbitrary :: forall b. Double -> Gen b -> Gen b
coarbitrary = forall a b. Real a => a -> Gen b -> Gen b
coarbitraryReal

-- Coarbitrary instances for container types
instance CoArbitrary a => CoArbitrary (Set.Set a) where
  coarbitrary :: forall b. Set a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitraryforall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Set a -> [a]
Set.toList
instance (CoArbitrary k, CoArbitrary v) => CoArbitrary (Map.Map k v) where
  coarbitrary :: forall b. Map k v -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [(k, a)]
Map.toList
instance CoArbitrary IntSet.IntSet where
  coarbitrary :: forall b. IntSet -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntSet -> [Int]
IntSet.toList
instance CoArbitrary a => CoArbitrary (IntMap.IntMap a) where
  coarbitrary :: forall b. IntMap a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IntMap a -> [(Int, a)]
IntMap.toList
instance CoArbitrary a => CoArbitrary (Sequence.Seq a) where
  coarbitrary :: forall b. Seq a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
instance CoArbitrary a => CoArbitrary (Tree.Tree a) where
  coarbitrary :: forall b. Tree a -> Gen b -> Gen b
coarbitrary (Tree.Node a
val [Tree a]
forest) = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
val forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary [Tree a]
forest

-- CoArbitrary instance for Ziplist
instance CoArbitrary a => CoArbitrary (ZipList a) where
  coarbitrary :: forall b. ZipList a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ZipList a -> [a]
getZipList

#ifndef NO_TRANSFORMERS
-- CoArbitrary instance for transformers' Functors
instance CoArbitrary a => CoArbitrary (Identity a) where
  coarbitrary :: forall b. Identity a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Identity a -> a
runIdentity

instance CoArbitrary a => CoArbitrary (Constant a b) where
  coarbitrary :: forall b. Constant a b -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} a (b :: k). Constant a b -> a
getConstant
#endif

-- CoArbitrary instance for Const
instance CoArbitrary a => CoArbitrary (Const a b) where
  coarbitrary :: forall b. Const a b -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} a (b :: k). Const a b -> a
getConst

-- CoArbitrary instances for Monoid
instance CoArbitrary a => CoArbitrary (Monoid.Dual a) where
  coarbitrary :: forall b. Dual a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Dual a -> a
Monoid.getDual

instance (Arbitrary a, CoArbitrary a) => CoArbitrary (Monoid.Endo a) where
  coarbitrary :: forall b. Endo a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Endo a -> a -> a
Monoid.appEndo

instance CoArbitrary Monoid.All where
  coarbitrary :: forall b. All -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. All -> Bool
Monoid.getAll

instance CoArbitrary Monoid.Any where
  coarbitrary :: forall b. Any -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. Any -> Bool
Monoid.getAny

instance CoArbitrary a => CoArbitrary (Monoid.Sum a) where
  coarbitrary :: forall b. Sum a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Sum a -> a
Monoid.getSum

instance CoArbitrary a => CoArbitrary (Monoid.Product a) where
  coarbitrary :: forall b. Product a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Product a -> a
Monoid.getProduct

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(3,0,0)
instance CoArbitrary a => CoArbitrary (Monoid.First a) where
  coarbitrary :: forall b. First a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. First a -> Maybe a
Monoid.getFirst

instance CoArbitrary a => CoArbitrary (Monoid.Last a) where
  coarbitrary :: forall b. Last a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Last a -> Maybe a
Monoid.getLast
#endif

#if MIN_VERSION_base(4,8,0)
instance CoArbitrary (f a) => CoArbitrary (Monoid.Alt f a) where
  coarbitrary :: forall b. Alt f a -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k). Alt f a -> f a
Monoid.getAlt
#endif
#endif

instance CoArbitrary Version where
  coarbitrary :: forall b. Version -> Gen b -> Gen b
coarbitrary (Version [Int]
a [[Char]]
b) = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary ([Int]
a, [[Char]]
b)

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,2,0)
instance CoArbitrary Newline where
  coarbitrary :: forall b. Newline -> Gen b -> Gen b
coarbitrary Newline
LF = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
  coarbitrary Newline
CRLF = forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1

instance CoArbitrary NewlineMode where
  coarbitrary :: forall b. NewlineMode -> Gen b -> Gen b
coarbitrary (NewlineMode Newline
inNL Newline
outNL) = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Newline
inNL forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Newline
outNL
#endif
#endif

-- ** Helpers for implementing coarbitrary

-- | A 'coarbitrary' implementation for integral numbers.
coarbitraryIntegral :: Integral a => a -> Gen b -> Gen b
coarbitraryIntegral :: forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral = forall n a. Integral n => n -> Gen a -> Gen a
variant

-- | A 'coarbitrary' implementation for real numbers.
coarbitraryReal :: Real a => a -> Gen b -> Gen b
coarbitraryReal :: forall a b. Real a => a -> Gen b -> Gen b
coarbitraryReal a
x = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (forall a. Real a => a -> Rational
toRational a
x)

-- | 'coarbitrary' helper for lazy people :-).
coarbitraryShow :: Show a => a -> Gen b -> Gen b
coarbitraryShow :: forall a b. Show a => a -> Gen b -> Gen b
coarbitraryShow a
x = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (forall a. Show a => a -> [Char]
show a
x)

-- | A 'coarbitrary' implementation for enums.
coarbitraryEnum :: Enum a => a -> Gen b -> Gen b
coarbitraryEnum :: forall a b. Enum a => a -> Gen b -> Gen b
coarbitraryEnum = forall n a. Integral n => n -> Gen a -> Gen a
variant forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum

--------------------------------------------------------------------------
-- ** arbitrary generators

-- these are here and not in Gen because of the Arbitrary class constraint

-- | Generates a list of a given length.
vector :: Arbitrary a => Int -> Gen [a]
vector :: forall a. Arbitrary a => Int -> Gen [a]
vector Int
k = forall a. Int -> Gen a -> Gen [a]
vectorOf Int
k forall a. Arbitrary a => Gen a
arbitrary

-- | Generates an ordered list.
orderedList :: (Ord a, Arbitrary a) => Gen [a]
orderedList :: forall a. (Ord a, Arbitrary a) => Gen [a]
orderedList = forall a. Ord a => [a] -> [a]
sort forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. Arbitrary a => Gen a
arbitrary

-- | Generates an infinite list.
infiniteList :: Arbitrary a => Gen [a]
infiniteList :: forall a. Arbitrary a => Gen [a]
infiniteList = forall a. Gen a -> Gen [a]
infiniteListOf forall a. Arbitrary a => Gen a
arbitrary


--------------------------------------------------------------------------
-- ** Rational helper

infixr 5 :<
data Stream a = !a :< Stream a

streamNth :: Int -> Stream a -> a
streamNth :: forall a. Int -> Stream a -> a
streamNth Int
n (a
x :< Stream a
xs) | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0    = a
x
                      | Bool
otherwise = forall a. Int -> Stream a -> a
streamNth (Int
n forall a. Num a => a -> a -> a
- Int
1) Stream a
xs

-- We read into this stream only with ~size argument,
-- so it's ok to have it as CAF.
--
rationalUniverse :: Stream Rational
rationalUniverse :: Stream Rational
rationalUniverse = Rational
0 forall a. a -> Stream a -> Stream a
:< Rational
1 forall a. a -> Stream a -> Stream a
:< (-Rational
1) forall a. a -> Stream a -> Stream a
:< forall {a}. Fractional a => Stream a -> Stream a
go Stream Rational
leftSideStream
  where
    go :: Stream a -> Stream a
go (a
x :< Stream a
xs) =
      let nx :: a
nx = -a
x
          rx :: a
rx = forall a. Fractional a => a -> a
recip a
x
          nrx :: a
nrx = -a
rx
      in a
nx seq :: forall a b. a -> b -> b
`seq` a
rx seq :: forall a b. a -> b -> b
`seq` a
nrx seq :: forall a b. a -> b -> b
`seq` (a
x forall a. a -> Stream a -> Stream a
:< a
rx forall a. a -> Stream a -> Stream a
:< a
nx forall a. a -> Stream a -> Stream a
:< a
nrx forall a. a -> Stream a -> Stream a
:< Stream a -> Stream a
go Stream a
xs)

-- All the rational numbers on the left side of the Calkin-Wilf tree,
-- in breadth-first order.
leftSideStream :: Stream Rational
leftSideStream :: Stream Rational
leftSideStream = (Integer
1 forall a. Integral a => a -> a -> Ratio a
% Integer
2) forall a. a -> Stream a -> Stream a
:< forall {a}. Integral a => Stream (Ratio a) -> Stream (Ratio a)
go Stream Rational
leftSideStream
  where
    go :: Stream (Ratio a) -> Stream (Ratio a)
go (Ratio a
x :< Stream (Ratio a)
xs) =
        Ratio a
lChild seq :: forall a b. a -> b -> b
`seq` Ratio a
rChild seq :: forall a b. a -> b -> b
`seq`
        (Ratio a
lChild forall a. a -> Stream a -> Stream a
:< Ratio a
rChild forall a. a -> Stream a -> Stream a
:< Stream (Ratio a) -> Stream (Ratio a)
go Stream (Ratio a)
xs)
      where
        nd :: a
nd = forall a. Ratio a -> a
numerator Ratio a
x forall a. Num a => a -> a -> a
+ forall a. Ratio a -> a
denominator Ratio a
x
        lChild :: Ratio a
lChild = forall a. Ratio a -> a
numerator Ratio a
x forall a. Integral a => a -> a -> Ratio a
% a
nd
        rChild :: Ratio a
rChild = a
nd forall a. Integral a => a -> a -> Ratio a
% forall a. Ratio a -> a
denominator Ratio a
x

--------------------------------------------------------------------------
-- the end.