-- | 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 = Gen a -> Gen (f a)
forall a. Gen a -> Gen (f a)
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary Gen a
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 = (a -> [a]) -> f a -> [f a]
forall a. (a -> [a]) -> f a -> [f a]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
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 = Gen a -> Gen b -> Gen (f a b)
forall a b. Gen a -> Gen b -> Gen (f a b)
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen b
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 = (a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
forall a b. (a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
forall a. Arbitrary a => a -> [a]
shrink b -> [b]
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 = a -> [a]
forall a. (Generic a, GSubterms (Rep a) a) => a -> [a]
subterms a
x [a] -> [a] -> [a]
forall a. [a] -> [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 = (Rep a Any -> a) -> [Rep a Any] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Rep a Any -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to ([Rep a Any] -> [a]) -> (a -> [Rep a Any]) -> a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rep a Any -> [Rep a Any]
forall a. Rep a a -> [Rep a a]
forall {k} (f :: k -> *) (a :: k).
RecursivelyShrink f =>
f a -> [f a]
grecursivelyShrink (Rep a Any -> [Rep a Any]) -> (a -> Rep a Any) -> a -> [Rep a Any]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a Any
forall x. a -> Rep a x
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' f a -> g a -> (:*:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g a
y | f a
x' <- f a -> [f a]
forall (a :: k). f a -> [f a]
forall {k} (f :: k -> *) (a :: k).
RecursivelyShrink f =>
f a -> [f a]
grecursivelyShrink f a
x] [(:*:) f g a] -> [(:*:) f g a] -> [(:*:) f g a]
forall a. [a] -> [a] -> [a]
++
    [f a
x f a -> g a -> (:*:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g a
y' | g a
y' <- g a -> [g a]
forall (a :: k). g a -> [g a]
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) = (f a -> (:+:) f g a) -> [f a] -> [(:+:) f g a]
forall a b. (a -> b) -> [a] -> [b]
map f a -> (:+:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (f a -> [f a]
forall (a :: k). f a -> [f a]
forall {k} (f :: k -> *) (a :: k).
RecursivelyShrink f =>
f a -> [f a]
grecursivelyShrink f a
x)
  grecursivelyShrink (R1 g a
x) = (g a -> (:+:) f g a) -> [g a] -> [(:+:) f g a]
forall a b. (a -> b) -> [a] -> [b]
map g a -> (:+:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (g a -> [g a]
forall (a :: k). g a -> [g a]
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) = (f a -> M1 i c f a) -> [f a] -> [M1 i c f a]
forall a b. (a -> b) -> [a] -> [b]
map f a -> M1 i c f a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (f a -> [f a]
forall (a :: k). f a -> [f a]
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) = (a -> K1 i a a) -> [a] -> [K1 i a a]
forall a b. (a -> b) -> [a] -> [b]
map a -> K1 i a a
forall k i c (p :: k). c -> K1 i c p
K1 (a -> [a]
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 = Rep a a -> [a]
forall (f :: * -> *) a. GSubterms f a => f a -> [a]
gSubterms (Rep a a -> [a]) -> (a -> Rep a a) -> a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a a
forall x. a -> Rep a x
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) = f a -> [a]
forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl f a
l [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ g 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) = f a -> [a]
forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl f a
x
  gSubterms (R1 g a
x) = g a -> [a]
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) = f a -> [a]
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) = f a -> [a]
forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl f a
l [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ g 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) = f a -> [a]
forall (f :: * -> *) a. GSubtermsIncl f a => f a -> [a]
gSubtermsIncl f a
x
  gSubtermsIncl (R1 g a
x) = g a -> [a]
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) = f a -> [a]
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 = (a -> Gen a) -> Gen (a -> a)
forall (m :: * -> *) a. Monad m => m (Gen a) -> Gen (m a)
promote (a -> Gen a -> Gen a
forall b. a -> Gen b -> Gen b
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 = Gen (a -> b)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1

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

instance Arbitrary Bool where
  arbitrary :: Gen Bool
arbitrary = (Bool, Bool) -> Gen Bool
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 = [Ordering] -> Gen Ordering
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 = [(Int, Gen (Maybe a))] -> Gen (Maybe a)
forall a. [(Int, Gen a)] -> Gen a
frequency [(Int
1, Maybe a -> Gen (Maybe a)
forall a. a -> Gen a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing), (Int
3, (a -> Maybe a) -> Gen a -> Gen (Maybe a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> Maybe a
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) = Maybe a
forall a. Maybe a
Nothing Maybe a -> [Maybe a] -> [Maybe a]
forall a. a -> [a] -> [a]
: [ a -> Maybe 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 = Gen (Maybe a)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Maybe a -> [Maybe a]
shrink = Maybe a -> [Maybe a]
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 = [Gen (Either a b)] -> Gen (Either a b)
forall a. [Gen a] -> Gen a
oneof [(a -> Either a b) -> Gen a -> Gen (Either a b)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> Either a b
forall a b. a -> Either a b
Left Gen a
arbA, (b -> Either a b) -> Gen b -> Gen (Either a b)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM b -> Either a b
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)  = [ a -> Either a b
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) = [ b -> Either a b
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 = Gen a -> Gen a -> Gen (Either a a)
forall a b. Gen a -> Gen b -> Gen (Either a b)
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 Gen a
forall a. Arbitrary a => Gen a
arbitrary
  liftShrink :: forall a. (a -> [a]) -> Either a a -> [Either a a]
liftShrink = (a -> [a]) -> (a -> [a]) -> Either a a -> [Either a a]
forall a b. (a -> [a]) -> (b -> [b]) -> Either a b -> [Either a b]
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
forall a. Arbitrary a => a -> [a]
shrink

instance (Arbitrary a, Arbitrary b) => Arbitrary (Either a b) where
  arbitrary :: Gen (Either a b)
arbitrary = Gen (Either a b)
forall (f :: * -> * -> *) a b.
(Arbitrary2 f, Arbitrary a, Arbitrary b) =>
Gen (f a b)
arbitrary2
  shrink :: Either a b -> [Either a b]
shrink = Either a b -> [Either a b]
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 = Gen a -> Gen [a]
forall a. Gen a -> Gen [a]
listOf
  liftShrink :: forall a. (a -> [a]) -> [a] -> [[a]]
liftShrink = (a -> [a]) -> [a] -> [[a]]
forall a. (a -> [a]) -> [a] -> [[a]]
shrinkList

instance Arbitrary a => Arbitrary [a] where
  arbitrary :: Gen [a]
arbitrary = Gen [a]
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: [a] -> [[a]]
shrink = [a] -> [[a]]
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 = [[[a]]] -> [[a]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ Int -> Int -> [a] -> [[a]]
forall {a}. Int -> Int -> [a] -> [[a]]
removes Int
k Int
n [a]
xs | Int
k <- (Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>Int
0) ((Int -> Int) -> Int -> [Int]
forall a. (a -> a) -> a -> [a]
iterate (Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div`Int
2) Int
n) ]
                 [[a]] -> [[a]] -> [[a]]
forall a. [a] -> [a] -> [a]
++ [a] -> [[a]]
shrinkOne [a]
xs
 where
  n :: Int
n = [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs

  shrinkOne :: [a] -> [[a]]
shrinkOne []     = []
  shrinkOne (a
x:[a]
xs) = [ a
x'a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs | a
x'  <- a -> [a]
shr a
x ]
                  [[a]] -> [[a]] -> [[a]]
forall a. [a] -> [a] -> [a]
++ [ a
xa -> [a] -> [a]
forall 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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n     = []
    | [a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs2  = [[]]
    | Bool
otherwise = [a]
xs2 [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: ([a] -> [a]) -> [[a]] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map ([a]
xs1 [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++) (Int -> Int -> [a] -> [[a]]
removes Int
k (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
k) [a]
xs2)
   where
    xs1 :: [a]
xs1 = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
k [a]
xs
    xs2 :: [a]
xs2 = Int -> [a] -> [a]
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 = Gen (Ratio a)
forall a. Fractional a => Gen a
arbitrarySizedFractional
  shrink :: Ratio a -> [Ratio a]
shrink    = Ratio a -> [Ratio a]
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 = (a -> a -> Complex a) -> Gen a -> Gen a -> Gen (Complex a)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a -> a -> Complex a
forall a. a -> a -> Complex a
(:+) Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen a
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Complex a -> [Complex a]
shrink (a
x :+ a
y) = [ a
x' a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
y | a
x' <- a -> [a]
forall a. Arbitrary a => a -> [a]
shrink a
x ] [Complex a] -> [Complex a] -> [Complex a]
forall a. [a] -> [a] -> [a]
++
                    [ a
x a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
y' | a
y' <- a -> [a]
forall a. Arbitrary a => a -> [a]
shrink a
y ]

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

instance Arbitrary2 (,) where
  liftArbitrary2 :: forall a b. Gen a -> Gen b -> Gen (a, b)
liftArbitrary2 = (a -> b -> (a, b)) -> Gen a -> Gen b -> Gen (a, b)
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 ]
    [(a, b)] -> [(a, b)] -> [(a, b)]
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 = Gen a -> Gen a -> Gen (a, a)
forall a b. Gen a -> Gen b -> Gen (a, b)
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 Gen a
forall a. Arbitrary a => Gen a
arbitrary
  liftShrink :: forall a. (a -> [a]) -> (a, a) -> [(a, a)]
liftShrink = (a -> [a]) -> (a -> [a]) -> (a, a) -> [(a, a)]
forall a b. (a -> [a]) -> (b -> [b]) -> (a, b) -> [(a, b)]
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
forall a. Arbitrary a => a -> [a]
shrink

instance (Arbitrary a, Arbitrary b) => Arbitrary (a,b) where
  arbitrary :: Gen (a, b)
arbitrary = Gen (a, b)
forall (f :: * -> * -> *) a b.
(Arbitrary2 f, Arbitrary a, Arbitrary b) =>
Gen (f a b)
arbitrary2
  shrink :: (a, b) -> [(a, b)]
shrink = (a, b) -> [(a, b)]
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 = (a -> b -> c -> (a, b, c))
-> Gen a -> Gen b -> Gen c -> Gen (a, b, c)
forall (m :: * -> *) a1 a2 a3 r.
Monad m =>
(a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
liftM3 (,,) Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen b
forall a. Arbitrary a => Gen a
arbitrary Gen c
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')) <- (a, (b, c)) -> [(a, (b, c))]
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 = (a -> b -> c -> d -> (a, b, c, d))
-> Gen a -> Gen b -> Gen c -> Gen d -> Gen (a, b, c, d)
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 (,,,) Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen b
forall a. Arbitrary a => Gen a
arbitrary Gen c
forall a. Arbitrary a => Gen a
arbitrary Gen d
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'))) <- (a, (b, (c, d))) -> [(a, (b, (c, d)))]
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 = (a -> b -> c -> d -> e -> (a, b, c, d, e))
-> Gen a -> Gen b -> Gen c -> Gen d -> Gen e -> Gen (a, b, c, d, e)
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 (,,,,) Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen b
forall a. Arbitrary a => Gen a
arbitrary Gen c
forall a. Arbitrary a => Gen a
arbitrary Gen d
forall a. Arbitrary a => Gen a
arbitrary Gen e
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')))) <- (a, (b, (c, (d, e)))) -> [(a, (b, (c, (d, e))))]
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 = (a -> b -> c -> d -> e -> f -> (a, b, c, d, e, f))
-> Gen (a -> b -> c -> d -> e -> f -> (a, b, c, d, e, f))
forall a. a -> Gen a
forall (m :: * -> *) a. Monad m => a -> m a
return (,,,,,)
          Gen (a -> b -> c -> d -> e -> f -> (a, b, c, d, e, f))
-> Gen a -> Gen (b -> c -> d -> e -> f -> (a, b, c, d, e, f))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen (b -> c -> d -> e -> f -> (a, b, c, d, e, f))
-> Gen b -> Gen (c -> d -> e -> f -> (a, b, c, d, e, f))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen b
forall a. Arbitrary a => Gen a
arbitrary Gen (c -> d -> e -> f -> (a, b, c, d, e, f))
-> Gen c -> Gen (d -> e -> f -> (a, b, c, d, e, f))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen c
forall a. Arbitrary a => Gen a
arbitrary Gen (d -> e -> f -> (a, b, c, d, e, f))
-> Gen d -> Gen (e -> f -> (a, b, c, d, e, f))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen d
forall a. Arbitrary a => Gen a
arbitrary
          Gen (e -> f -> (a, b, c, d, e, f))
-> Gen e -> Gen (f -> (a, b, c, d, e, f))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen e
forall a. Arbitrary a => Gen a
arbitrary Gen (f -> (a, b, c, d, e, f)) -> Gen f -> Gen (a, b, c, d, e, f)
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen f
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'))))) <- (a, (b, (c, (d, (e, f))))) -> [(a, (b, (c, (d, (e, f)))))]
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 = (a -> b -> c -> d -> e -> f -> g -> (a, b, c, d, e, f, g))
-> Gen (a -> b -> c -> d -> e -> f -> g -> (a, b, c, d, e, f, g))
forall a. a -> Gen a
forall (m :: * -> *) a. Monad m => a -> m a
return (,,,,,,)
          Gen (a -> b -> c -> d -> e -> f -> g -> (a, b, c, d, e, f, g))
-> Gen a
-> Gen (b -> c -> d -> e -> f -> g -> (a, b, c, d, e, f, g))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen (b -> c -> d -> e -> f -> g -> (a, b, c, d, e, f, g))
-> Gen b -> Gen (c -> d -> e -> f -> g -> (a, b, c, d, e, f, g))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen b
forall a. Arbitrary a => Gen a
arbitrary Gen (c -> d -> e -> f -> g -> (a, b, c, d, e, f, g))
-> Gen c -> Gen (d -> e -> f -> g -> (a, b, c, d, e, f, g))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen c
forall a. Arbitrary a => Gen a
arbitrary Gen (d -> e -> f -> g -> (a, b, c, d, e, f, g))
-> Gen d -> Gen (e -> f -> g -> (a, b, c, d, e, f, g))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen d
forall a. Arbitrary a => Gen a
arbitrary
          Gen (e -> f -> g -> (a, b, c, d, e, f, g))
-> Gen e -> Gen (f -> g -> (a, b, c, d, e, f, g))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen e
forall a. Arbitrary a => Gen a
arbitrary Gen (f -> g -> (a, b, c, d, e, f, g))
-> Gen f -> Gen (g -> (a, b, c, d, e, f, g))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen f
forall a. Arbitrary a => Gen a
arbitrary Gen (g -> (a, b, c, d, e, f, g))
-> Gen g -> Gen (a, b, c, d, e, f, g)
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen g
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')))))) <- (a, (b, (c, (d, (e, (f, g))))))
-> [(a, (b, (c, (d, (e, (f, g))))))]
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 = (a -> b -> c -> d -> e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
-> Gen
     (a -> b -> c -> d -> e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
forall a. a -> Gen a
forall (m :: * -> *) a. Monad m => a -> m a
return (,,,,,,,)
          Gen
  (a -> b -> c -> d -> e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
-> Gen a
-> Gen
     (b -> c -> d -> e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen (b -> c -> d -> e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
-> Gen b
-> Gen (c -> d -> e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen b
forall a. Arbitrary a => Gen a
arbitrary Gen (c -> d -> e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
-> Gen c -> Gen (d -> e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen c
forall a. Arbitrary a => Gen a
arbitrary Gen (d -> e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
-> Gen d -> Gen (e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen d
forall a. Arbitrary a => Gen a
arbitrary
          Gen (e -> f -> g -> h -> (a, b, c, d, e, f, g, h))
-> Gen e -> Gen (f -> g -> h -> (a, b, c, d, e, f, g, h))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen e
forall a. Arbitrary a => Gen a
arbitrary Gen (f -> g -> h -> (a, b, c, d, e, f, g, h))
-> Gen f -> Gen (g -> h -> (a, b, c, d, e, f, g, h))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen f
forall a. Arbitrary a => Gen a
arbitrary Gen (g -> h -> (a, b, c, d, e, f, g, h))
-> Gen g -> Gen (h -> (a, b, c, d, e, f, g, h))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen g
forall a. Arbitrary a => Gen a
arbitrary Gen (h -> (a, b, c, d, e, f, g, h))
-> Gen h -> Gen (a, b, c, d, e, f, g, h)
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen h
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')))))))
      <- (a, (b, (c, (d, (e, (f, (g, h)))))))
-> [(a, (b, (c, (d, (e, (f, (g, h)))))))]
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 = (a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> (a, b, c, d, e, f, g, h, i))
-> Gen
     (a
      -> b
      -> c
      -> d
      -> e
      -> f
      -> g
      -> h
      -> i
      -> (a, b, c, d, e, f, g, h, i))
forall a. a -> Gen a
forall (m :: * -> *) a. Monad m => a -> m a
return (,,,,,,,,)
          Gen
  (a
   -> b
   -> c
   -> d
   -> e
   -> f
   -> g
   -> h
   -> i
   -> (a, b, c, d, e, f, g, h, i))
-> Gen a
-> Gen
     (b
      -> c -> d -> e -> f -> g -> h -> i -> (a, b, c, d, e, f, g, h, i))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen
  (b
   -> c -> d -> e -> f -> g -> h -> i -> (a, b, c, d, e, f, g, h, i))
-> Gen b
-> Gen
     (c -> d -> e -> f -> g -> h -> i -> (a, b, c, d, e, f, g, h, i))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen b
forall a. Arbitrary a => Gen a
arbitrary Gen
  (c -> d -> e -> f -> g -> h -> i -> (a, b, c, d, e, f, g, h, i))
-> Gen c
-> Gen (d -> e -> f -> g -> h -> i -> (a, b, c, d, e, f, g, h, i))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen c
forall a. Arbitrary a => Gen a
arbitrary Gen (d -> e -> f -> g -> h -> i -> (a, b, c, d, e, f, g, h, i))
-> Gen d
-> Gen (e -> f -> g -> h -> i -> (a, b, c, d, e, f, g, h, i))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen d
forall a. Arbitrary a => Gen a
arbitrary
          Gen (e -> f -> g -> h -> i -> (a, b, c, d, e, f, g, h, i))
-> Gen e -> Gen (f -> g -> h -> i -> (a, b, c, d, e, f, g, h, i))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen e
forall a. Arbitrary a => Gen a
arbitrary Gen (f -> g -> h -> i -> (a, b, c, d, e, f, g, h, i))
-> Gen f -> Gen (g -> h -> i -> (a, b, c, d, e, f, g, h, i))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen f
forall a. Arbitrary a => Gen a
arbitrary Gen (g -> h -> i -> (a, b, c, d, e, f, g, h, i))
-> Gen g -> Gen (h -> i -> (a, b, c, d, e, f, g, h, i))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen g
forall a. Arbitrary a => Gen a
arbitrary Gen (h -> i -> (a, b, c, d, e, f, g, h, i))
-> Gen h -> Gen (i -> (a, b, c, d, e, f, g, h, i))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen h
forall a. Arbitrary a => Gen a
arbitrary
          Gen (i -> (a, b, c, d, e, f, g, h, i))
-> Gen i -> Gen (a, b, c, d, e, f, g, h, i)
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen i
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'))))))))
      <- (a, (b, (c, (d, (e, (f, (g, (h, i))))))))
-> [(a, (b, (c, (d, (e, (f, (g, (h, i))))))))]
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 = (a
 -> b
 -> c
 -> d
 -> e
 -> f
 -> g
 -> h
 -> i
 -> j
 -> (a, b, c, d, e, f, g, h, i, j))
-> Gen
     (a
      -> b
      -> c
      -> d
      -> e
      -> f
      -> g
      -> h
      -> i
      -> j
      -> (a, b, c, d, e, f, g, h, i, j))
forall a. a -> Gen a
forall (m :: * -> *) a. Monad m => a -> m a
return (,,,,,,,,,)
          Gen
  (a
   -> b
   -> c
   -> d
   -> e
   -> f
   -> g
   -> h
   -> i
   -> j
   -> (a, b, c, d, e, f, g, h, i, j))
-> Gen a
-> Gen
     (b
      -> c
      -> d
      -> e
      -> f
      -> g
      -> h
      -> i
      -> j
      -> (a, b, c, d, e, f, g, h, i, j))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen
  (b
   -> c
   -> d
   -> e
   -> f
   -> g
   -> h
   -> i
   -> j
   -> (a, b, c, d, e, f, g, h, i, j))
-> Gen b
-> Gen
     (c
      -> d
      -> e
      -> f
      -> g
      -> h
      -> i
      -> j
      -> (a, b, c, d, e, f, g, h, i, j))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen b
forall a. Arbitrary a => Gen a
arbitrary Gen
  (c
   -> d
   -> e
   -> f
   -> g
   -> h
   -> i
   -> j
   -> (a, b, c, d, e, f, g, h, i, j))
-> Gen c
-> Gen
     (d -> e -> f -> g -> h -> i -> j -> (a, b, c, d, e, f, g, h, i, j))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen c
forall a. Arbitrary a => Gen a
arbitrary Gen
  (d -> e -> f -> g -> h -> i -> j -> (a, b, c, d, e, f, g, h, i, j))
-> Gen d
-> Gen
     (e -> f -> g -> h -> i -> j -> (a, b, c, d, e, f, g, h, i, j))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen d
forall a. Arbitrary a => Gen a
arbitrary
          Gen (e -> f -> g -> h -> i -> j -> (a, b, c, d, e, f, g, h, i, j))
-> Gen e
-> Gen (f -> g -> h -> i -> j -> (a, b, c, d, e, f, g, h, i, j))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen e
forall a. Arbitrary a => Gen a
arbitrary Gen (f -> g -> h -> i -> j -> (a, b, c, d, e, f, g, h, i, j))
-> Gen f
-> Gen (g -> h -> i -> j -> (a, b, c, d, e, f, g, h, i, j))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen f
forall a. Arbitrary a => Gen a
arbitrary Gen (g -> h -> i -> j -> (a, b, c, d, e, f, g, h, i, j))
-> Gen g -> Gen (h -> i -> j -> (a, b, c, d, e, f, g, h, i, j))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen g
forall a. Arbitrary a => Gen a
arbitrary Gen (h -> i -> j -> (a, b, c, d, e, f, g, h, i, j))
-> Gen h -> Gen (i -> j -> (a, b, c, d, e, f, g, h, i, j))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen h
forall a. Arbitrary a => Gen a
arbitrary
          Gen (i -> j -> (a, b, c, d, e, f, g, h, i, j))
-> Gen i -> Gen (j -> (a, b, c, d, e, f, g, h, i, j))
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen i
forall a. Arbitrary a => Gen a
arbitrary Gen (j -> (a, b, c, d, e, f, g, h, i, j))
-> Gen j -> Gen (a, b, c, d, e, f, g, h, i, j)
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen j
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')))))))))
      <- (a, (b, (c, (d, (e, (f, (g, (h, (i, j)))))))))
-> [(a, (b, (c, (d, (e, (f, (g, (h, (i, j)))))))))]
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 = Gen Integer
forall a. Integral a => Gen a
arbitrarySizedIntegral
  shrink :: Integer -> [Integer]
shrink    = Integer -> [Integer]
forall a. Integral a => a -> [a]
shrinkIntegral

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

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

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

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

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

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

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

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

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

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

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

  shrink :: Char -> [Char]
shrink Char
c = (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> Char -> Bool
<. Char
c) ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char]
forall a. Eq a => [a] -> [a]
nub
            ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char
'a',Char
'b',Char
'c']
            [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [ Char -> Char
toLower Char
c | Char -> Bool
isUpper Char
c ]
            [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char
'A',Char
'B',Char
'C']
            [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char
'1',Char
'2',Char
'3']
            [Char] -> [Char] -> [Char]
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 ((Bool, Bool, Bool), (Bool, Bool, Char))
-> ((Bool, Bool, Bool), (Bool, Bool, Char)) -> Bool
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
aChar -> Char -> Bool
forall 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 = [Gen Float] -> Gen Float
forall a. [Gen a] -> Gen a
oneof
    -- generate 0..1 numbers with full precision
    [ Gen Float
genFloat
    -- generate integral numbers
    , Int -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Float) -> Gen Int -> Gen Float
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Gen Int
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
    , Gen Float
forall a. Fractional a => Gen a
arbitrarySizedFractional
    ]
    where
      smallDenominators :: Gen Float
smallDenominators = (Int -> Gen Float) -> Gen Float
forall a. (Int -> Gen a) -> Gen a
sized ((Int -> Gen Float) -> Gen Float)
-> (Int -> Gen Float) -> Gen Float
forall a b. (a -> b) -> a -> b
$ \Int
n -> do
        Int
i <- (Int, Int) -> Gen Int
chooseInt (Int
0, Int
n)
        Float -> Gen Float
forall a. a -> Gen a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Rational -> Float
forall a. Fractional a => Rational -> a
fromRational (Int -> Stream Rational -> Rational
forall a. Int -> Stream a -> a
streamNth Int
i Stream Rational
rationalUniverse))

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

instance Arbitrary CUIntMax where
  arbitrary :: Gen CUIntMax
arbitrary = Gen CUIntMax
forall a. (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral
  shrink :: CUIntMax -> [CUIntMax]
shrink = CUIntMax -> [CUIntMax]
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 = (Int64 -> CClock) -> Gen Int64 -> Gen CClock
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int64 -> CClock
CClock Gen Int64
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: CClock -> [CClock]
shrink (CClock Int64
x) = (Int64 -> CClock) -> [Int64] -> [CClock]
forall a b. (a -> b) -> [a] -> [b]
map Int64 -> CClock
CClock (Int64 -> [Int64]
forall a. Arbitrary a => a -> [a]
shrink Int64
x)

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

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

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

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

instance Arbitrary CDouble where
  arbitrary :: Gen CDouble
arbitrary = Gen CDouble
forall a. Fractional a => Gen a
arbitrarySizedFractional
  shrink :: CDouble -> [CDouble]
shrink = CDouble -> [CDouble]
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 = ([a] -> Set a) -> Gen [a] -> Gen (Set a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList Gen [a]
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Set a -> [Set a]
shrink = ([a] -> Set a) -> [[a]] -> [Set a]
forall a b. (a -> b) -> [a] -> [b]
map [a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList ([[a]] -> [Set a]) -> (Set a -> [[a]]) -> Set a -> [Set a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [[a]]
forall a. Arbitrary a => a -> [a]
shrink ([a] -> [[a]]) -> (Set a -> [a]) -> Set a -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set a -> [a]
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 = ([(k, a)] -> Map k a) -> Gen [(k, a)] -> Gen (Map k a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList (Gen [(k, a)] -> Gen (Map k a))
-> (Gen a -> Gen [(k, a)]) -> Gen a -> Gen (Map k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Gen (k, a) -> Gen [(k, a)]
forall a. Gen a -> Gen [a]
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary (Gen (k, a) -> Gen [(k, a)])
-> (Gen a -> Gen (k, a)) -> Gen a -> Gen [(k, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Gen a -> Gen (k, a)
forall a. Gen a -> Gen (k, a)
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 = ([(k, a)] -> Map k a) -> [[(k, a)]] -> [Map k a]
forall a b. (a -> b) -> [a] -> [b]
map [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([[(k, a)]] -> [Map k a])
-> (Map k a -> [[(k, a)]]) -> Map k a -> [Map k a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((k, a) -> [(k, a)]) -> [(k, a)] -> [[(k, a)]]
forall a. (a -> [a]) -> [a] -> [[a]]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink ((a -> [a]) -> (k, a) -> [(k, a)]
forall a. (a -> [a]) -> (k, a) -> [(k, a)]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr) ([(k, a)] -> [[(k, a)]])
-> (Map k a -> [(k, a)]) -> Map k a -> [[(k, a)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k a -> [(k, a)]
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 = Gen (Map k v)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Map k v -> [Map k v]
shrink = Map k v -> [Map k v]
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1
instance Arbitrary IntSet.IntSet where
  arbitrary :: Gen IntSet
arbitrary = ([Int] -> IntSet) -> Gen [Int] -> Gen IntSet
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Int] -> IntSet
IntSet.fromList Gen [Int]
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: IntSet -> [IntSet]
shrink = ([Int] -> IntSet) -> [[Int]] -> [IntSet]
forall a b. (a -> b) -> [a] -> [b]
map [Int] -> IntSet
IntSet.fromList ([[Int]] -> [IntSet]) -> (IntSet -> [[Int]]) -> IntSet -> [IntSet]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> [[Int]]
forall a. Arbitrary a => a -> [a]
shrink ([Int] -> [[Int]]) -> (IntSet -> [Int]) -> IntSet -> [[Int]]
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 = ([(Int, a)] -> IntMap a) -> Gen [(Int, a)] -> Gen (IntMap a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
IntMap.fromList (Gen [(Int, a)] -> Gen (IntMap a))
-> (Gen a -> Gen [(Int, a)]) -> Gen a -> Gen (IntMap a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Gen (Int, a) -> Gen [(Int, a)]
forall a. Gen a -> Gen [a]
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary (Gen (Int, a) -> Gen [(Int, a)])
-> (Gen a -> Gen (Int, a)) -> Gen a -> Gen [(Int, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Gen a -> Gen (Int, a)
forall a. Gen a -> Gen (Int, a)
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary
  liftShrink :: forall a. (a -> [a]) -> IntMap a -> [IntMap a]
liftShrink a -> [a]
shr = ([(Int, a)] -> IntMap a) -> [[(Int, a)]] -> [IntMap a]
forall a b. (a -> b) -> [a] -> [b]
map [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
IntMap.fromList ([[(Int, a)]] -> [IntMap a])
-> (IntMap a -> [[(Int, a)]]) -> IntMap a -> [IntMap a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, a) -> [(Int, a)]) -> [(Int, a)] -> [[(Int, a)]]
forall a. (a -> [a]) -> [a] -> [[a]]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink ((a -> [a]) -> (Int, a) -> [(Int, a)]
forall a. (a -> [a]) -> (Int, a) -> [(Int, a)]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr) ([(Int, a)] -> [[(Int, a)]])
-> (IntMap a -> [(Int, a)]) -> IntMap a -> [[(Int, a)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap a -> [(Int, a)]
forall a. IntMap a -> [(Int, a)]
IntMap.toList
instance Arbitrary a => Arbitrary (IntMap.IntMap a) where
  arbitrary :: Gen (IntMap a)
arbitrary = Gen (IntMap a)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: IntMap a -> [IntMap a]
shrink = IntMap a -> [IntMap a]
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 = ([a] -> Seq a) -> Gen [a] -> Gen (Seq a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> Seq a
forall a. [a] -> Seq a
Sequence.fromList (Gen [a] -> Gen (Seq a))
-> (Gen a -> Gen [a]) -> Gen a -> Gen (Seq a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Gen a -> Gen [a]
forall a. Gen a -> Gen [a]
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary
  liftShrink :: forall a. (a -> [a]) -> Seq a -> [Seq a]
liftShrink a -> [a]
shr = ([a] -> Seq a) -> [[a]] -> [Seq a]
forall a b. (a -> b) -> [a] -> [b]
map [a] -> Seq a
forall a. [a] -> Seq a
Sequence.fromList ([[a]] -> [Seq a]) -> (Seq a -> [[a]]) -> Seq a -> [Seq a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> [a]) -> [a] -> [[a]]
forall a. (a -> [a]) -> [a] -> [[a]]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr ([a] -> [[a]]) -> (Seq a -> [a]) -> Seq a -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq a -> [a]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
instance Arbitrary a => Arbitrary (Sequence.Seq a) where
  arbitrary :: Gen (Seq a)
arbitrary = Gen (Seq a)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Seq a -> [Seq a]
shrink = Seq a -> [Seq a]
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 = (Int -> Gen (Tree a)) -> Gen (Tree a)
forall a. (Int -> Gen a) -> Gen a
sized ((Int -> Gen (Tree a)) -> Gen (Tree a))
-> (Int -> Gen (Tree a)) -> Gen (Tree a)
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) -- can go negative!
            [Tree a]
forest <- (Int -> Gen (Tree a)) -> [Int] -> Gen [Tree a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Int -> Gen (Tree a)
go [Int]
pars
            Tree a -> Gen (Tree a)
forall a. a -> Gen a
forall (m :: * -> *) a. Monad m => a -> m a
return (Tree a -> Gen (Tree a)) -> Tree a -> Gen (Tree a)
forall a b. (a -> b) -> a -> b
$ a -> [Tree a] -> Tree a
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 Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
k Int
1 of
            Ordering
LT -> [Int] -> Gen [Int]
forall a. a -> Gen a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
            Ordering
EQ -> [Int] -> Gen [Int]
forall a. a -> Gen a
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 (Int -> Gen [Int]) -> Int -> Gen [Int]
forall a b. (a -> b) -> a -> b
$ Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
first
                [Int] -> Gen [Int]
forall a. [a] -> Gen [a]
shuffle (Int
first Int -> [Int] -> [Int]
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 [Tree a] -> [Tree a] -> [Tree a]
forall a. [a] -> [a] -> [a]
++
            [ a -> [Tree a] -> Tree a
forall a. a -> [Tree a] -> Tree a
Tree.Node a
e [Tree a]
fs
            | (a
e, [Tree a]
fs) <- (a -> [a])
-> ([Tree a] -> [[Tree a]]) -> (a, [Tree a]) -> [(a, [Tree a])]
forall a b. (a -> [a]) -> (b -> [b]) -> (a, b) -> [(a, b)]
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
shr ((Tree a -> [Tree a]) -> [Tree a] -> [[Tree a]]
forall a. (a -> [a]) -> [a] -> [[a]]
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 = Gen (Tree a)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Tree a -> [Tree a]
shrink = Tree a -> [Tree a]
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 = ([a] -> ZipList a) -> Gen [a] -> Gen (ZipList a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList (Gen [a] -> Gen (ZipList a))
-> (Gen a -> Gen [a]) -> Gen a -> Gen (ZipList a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Gen a -> Gen [a]
forall a. Gen a -> Gen [a]
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary
  liftShrink :: forall a. (a -> [a]) -> ZipList a -> [ZipList a]
liftShrink a -> [a]
shr = ([a] -> ZipList a) -> [[a]] -> [ZipList a]
forall a b. (a -> b) -> [a] -> [b]
map [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList ([[a]] -> [ZipList a])
-> (ZipList a -> [[a]]) -> ZipList a -> [ZipList a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> [a]) -> [a] -> [[a]]
forall a. (a -> [a]) -> [a] -> [[a]]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr ([a] -> [[a]]) -> (ZipList a -> [a]) -> ZipList a -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ZipList a -> [a]
forall a. ZipList a -> [a]
getZipList
instance Arbitrary a => Arbitrary (ZipList a) where
  arbitrary :: Gen (ZipList a)
arbitrary = Gen (ZipList a)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: ZipList a -> [ZipList a]
shrink = ZipList a -> [ZipList a]
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 = (a -> Identity a) -> Gen a -> Gen (Identity a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Identity a
forall a. a -> Identity a
Identity
  liftShrink :: forall a. (a -> [a]) -> Identity a -> [Identity a]
liftShrink a -> [a]
shr = (a -> Identity a) -> [a] -> [Identity a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Identity a
forall a. a -> Identity a
Identity ([a] -> [Identity a])
-> (Identity a -> [a]) -> Identity a -> [Identity a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shr (a -> [a]) -> (Identity a -> a) -> Identity a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
forall a. Identity a -> a
runIdentity
instance Arbitrary a => Arbitrary (Identity a) where
  arbitrary :: Gen (Identity a)
arbitrary = Gen (Identity a)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Identity a -> [Identity a]
shrink = Identity a -> [Identity a]
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
_ = (a -> Constant a b) -> Gen a -> Gen (Constant a b)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Constant a b
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]
_ = (a -> Constant a b) -> [a] -> [Constant a b]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Constant a b
forall {k} a (b :: k). a -> Constant a b
Constant ([a] -> [Constant a b])
-> (Constant a b -> [a]) -> Constant a b -> [Constant a b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shrA (a -> [a]) -> (Constant a b -> a) -> Constant a b -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Constant a b -> a
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 = Gen a -> Gen a -> Gen (Constant a a)
forall a b. Gen a -> Gen b -> Gen (Constant a b)
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 Gen a
forall a. Arbitrary a => Gen a
arbitrary
  liftShrink :: forall a. (a -> [a]) -> Constant a a -> [Constant a a]
liftShrink = (a -> [a]) -> (a -> [a]) -> Constant a a -> [Constant a a]
forall a b.
(a -> [a]) -> (b -> [b]) -> Constant a b -> [Constant a b]
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
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 = (a -> Constant a b) -> Gen a -> Gen (Constant a b)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Constant a b
forall {k} a (b :: k). a -> Constant a b
Constant Gen a
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Constant a b -> [Constant a b]
shrink = (a -> Constant a b) -> [a] -> [Constant a b]
forall a b. (a -> b) -> [a] -> [b]
map a -> Constant a b
forall {k} a (b :: k). a -> Constant a b
Constant ([a] -> [Constant a b])
-> (Constant a b -> [a]) -> Constant a b -> [Constant a b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. Arbitrary a => a -> [a]
shrink (a -> [a]) -> (Constant a b -> a) -> Constant a b -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Constant a b -> a
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 = (f a -> g a -> Product f g a)
-> Gen (f a) -> Gen (g a) -> Gen (Product f g a)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 f a -> g a -> Product f g a
forall {k} (f :: k -> *) (g :: k -> *) (a :: k).
f a -> g a -> Product f g a
Pair (Gen a -> Gen (f a)
forall a. Gen a -> Gen (f a)
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary Gen a
arb) (Gen a -> Gen (g a)
forall a. Gen a -> Gen (g a)
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) =
    [ f a -> g a -> Product f g a
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' <- (a -> [a]) -> f a -> [f a]
forall a. (a -> [a]) -> f a -> [f a]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr f a
f ] [Product f g a] -> [Product f g a] -> [Product f g a]
forall a. [a] -> [a] -> [a]
++
    [ f a -> g a -> Product f g 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' <- (a -> [a]) -> g a -> [g a]
forall a. (a -> [a]) -> g a -> [g a]
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 = Gen (Product f g a)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Product f g a -> [Product f g a]
shrink = Product f g a -> [Product f g a]
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 = (f (g a) -> Compose f g a) -> Gen (f (g a)) -> Gen (Compose f g a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f (g a) -> Compose f g a
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Gen (f (g a)) -> Gen (Compose f g a))
-> (Gen a -> Gen (f (g a))) -> Gen a -> Gen (Compose f g a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Gen (g a) -> Gen (f (g a))
forall a. Gen a -> Gen (f a)
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary (Gen (g a) -> Gen (f (g a)))
-> (Gen a -> Gen (g a)) -> Gen a -> Gen (f (g a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Gen a -> Gen (g a)
forall a. Gen a -> Gen (g a)
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 = (f (g a) -> Compose f g a) -> [f (g a)] -> [Compose f g a]
forall a b. (a -> b) -> [a] -> [b]
map f (g a) -> Compose f g a
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose ([f (g a)] -> [Compose f g a])
-> (Compose f g a -> [f (g a)]) -> Compose f g a -> [Compose f g a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (g a -> [g a]) -> f (g a) -> [f (g a)]
forall a. (a -> [a]) -> f a -> [f a]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink ((a -> [a]) -> g a -> [g a]
forall a. (a -> [a]) -> g a -> [g a]
forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr) (f (g a) -> [f (g a)])
-> (Compose f g a -> f (g a)) -> Compose f g a -> [f (g a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compose f g a -> f (g a)
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 = Gen (Compose f g a)
forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
  shrink :: Compose f g a -> [Compose f g a]
shrink = Compose f g a -> [Compose f g a]
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
_ = (a -> Const a b) -> Gen a -> Gen (Const a b)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Const a b
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]
_ = (a -> Const a b) -> [a] -> [Const a b]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Const a b
forall {k} a (b :: k). a -> Const a b
Const ([a] -> [Const a b])
-> (Const a b -> [a]) -> Const a b -> [Const a b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shrA (a -> [a]) -> (Const a b -> a) -> Const a b -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Const a b -> a
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 = Gen a -> Gen a -> Gen (Const a a)
forall a b. Gen a -> Gen b -> Gen (Const a b)
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
Gen a -> Gen b -> Gen (f a b)
liftArbitrary2 Gen a
forall a. Arbitrary a => Gen a
arbitrary
  liftShrink :: forall a. (a -> [a]) -> Const a a -> [Const a a]
liftShrink = (a -> [a]) -> (a -> [a]) -> Const a a -> [Const a a]
forall a b. (a -> [a]) -> (b -> [b]) -> Const a b -> [Const a b]
forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
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 = (a -> Const a b) -> Gen a -> Gen (Const a b)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Const a b
forall {k} a (b :: k). a -> Const a b
Const Gen a
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Const a b -> [Const a b]
shrink = (a -> Const a b) -> [a] -> [Const a b]
forall a b. (a -> b) -> [a] -> [b]
map a -> Const a b
forall {k} a (b :: k). a -> Const a b
Const ([a] -> [Const a b])
-> (Const a b -> [a]) -> Const a b -> [Const a b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. Arbitrary a => a -> [a]
shrink (a -> [a]) -> (Const a b -> a) -> Const a b -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Const a b -> a
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 = m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m a -> WrappedMonad m a) -> Gen (m a) -> Gen (WrappedMonad m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (m a)
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: WrappedMonad m a -> [WrappedMonad m a]
shrink (WrapMonad m a
a) = (m a -> WrappedMonad m a) -> [m a] -> [WrappedMonad m a]
forall a b. (a -> b) -> [a] -> [b]
map m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad (m a -> [m a]
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 = a b c -> WrappedArrow a b c
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b c -> WrappedArrow a b c)
-> Gen (a b c) -> Gen (WrappedArrow a b c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (a b c)
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: WrappedArrow a b c -> [WrappedArrow a b c]
shrink (WrapArrow a b c
a) = (a b c -> WrappedArrow a b c) -> [a b c] -> [WrappedArrow a b c]
forall a b. (a -> b) -> [a] -> [b]
map a b c -> WrappedArrow a b c
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow (a b c -> [a b c]
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 = (a -> Dual a) -> Gen a -> Gen (Dual a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Dual a
forall a. a -> Dual a
Monoid.Dual Gen a
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Dual a -> [Dual a]
shrink = (a -> Dual a) -> [a] -> [Dual a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Dual a
forall a. a -> Dual a
Monoid.Dual ([a] -> [Dual a]) -> (Dual a -> [a]) -> Dual a -> [Dual a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. Arbitrary a => a -> [a]
shrink (a -> [a]) -> (Dual a -> a) -> Dual a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dual a -> a
forall a. Dual a -> a
Monoid.getDual

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

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

instance Arbitrary Monoid.Any where
  arbitrary :: Gen Any
arbitrary = (Bool -> Any) -> Gen Bool -> Gen Any
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> Any
Monoid.Any Gen Bool
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Any -> [Any]
shrink = (Bool -> Any) -> [Bool] -> [Any]
forall a b. (a -> b) -> [a] -> [b]
map Bool -> Any
Monoid.Any ([Bool] -> [Any]) -> (Any -> [Bool]) -> Any -> [Any]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> [Bool]
forall a. Arbitrary a => a -> [a]
shrink (Bool -> [Bool]) -> (Any -> Bool) -> Any -> [Bool]
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 = (a -> Sum a) -> Gen a -> Gen (Sum a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Sum a
forall a. a -> Sum a
Monoid.Sum Gen a
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Sum a -> [Sum a]
shrink = (a -> Sum a) -> [a] -> [Sum a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Sum a
forall a. a -> Sum a
Monoid.Sum ([a] -> [Sum a]) -> (Sum a -> [a]) -> Sum a -> [Sum a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. Arbitrary a => a -> [a]
shrink (a -> [a]) -> (Sum a -> a) -> Sum a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sum a -> a
forall a. Sum a -> a
Monoid.getSum

instance Arbitrary a => Arbitrary (Monoid.Product a) where
  arbitrary :: Gen (Product a)
arbitrary = (a -> Product a) -> Gen a -> Gen (Product a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Product a
forall a. a -> Product a
Monoid.Product  Gen a
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Product a -> [Product a]
shrink = (a -> Product a) -> [a] -> [Product a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Product a
forall a. a -> Product a
Monoid.Product  ([a] -> [Product a])
-> (Product a -> [a]) -> Product a -> [Product a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. Arbitrary a => a -> [a]
shrink (a -> [a]) -> (Product a -> a) -> Product a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Product a -> a
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 = (Maybe a -> First a) -> Gen (Maybe a) -> Gen (First a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe a -> First a
forall a. Maybe a -> First a
Monoid.First Gen (Maybe a)
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: First a -> [First a]
shrink = (Maybe a -> First a) -> [Maybe a] -> [First a]
forall a b. (a -> b) -> [a] -> [b]
map Maybe a -> First a
forall a. Maybe a -> First a
Monoid.First ([Maybe a] -> [First a])
-> (First a -> [Maybe a]) -> First a -> [First a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> [Maybe a]
forall a. Arbitrary a => a -> [a]
shrink (Maybe a -> [Maybe a])
-> (First a -> Maybe a) -> First a -> [Maybe a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. First a -> Maybe a
forall a. First a -> Maybe a
Monoid.getFirst

instance Arbitrary a => Arbitrary (Monoid.Last a) where
  arbitrary :: Gen (Last a)
arbitrary = (Maybe a -> Last a) -> Gen (Maybe a) -> Gen (Last a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe a -> Last a
forall a. Maybe a -> Last a
Monoid.Last Gen (Maybe a)
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Last a -> [Last a]
shrink = (Maybe a -> Last a) -> [Maybe a] -> [Last a]
forall a b. (a -> b) -> [a] -> [b]
map Maybe a -> Last a
forall a. Maybe a -> Last a
Monoid.Last ([Maybe a] -> [Last a])
-> (Last a -> [Maybe a]) -> Last a -> [Last a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> [Maybe a]
forall a. Arbitrary a => a -> [a]
shrink (Maybe a -> [Maybe a])
-> (Last a -> Maybe a) -> Last a -> [Maybe a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Last a -> Maybe a
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 = (f a -> Alt f a) -> Gen (f a) -> Gen (Alt f a)
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f a -> Alt f a
forall {k} (f :: k -> *) (a :: k). f a -> Alt f a
Monoid.Alt Gen (f a)
forall a. Arbitrary a => Gen a
arbitrary
  shrink :: Alt f a -> [Alt f a]
shrink = (f a -> Alt f a) -> [f a] -> [Alt f a]
forall a b. (a -> b) -> [a] -> [b]
map f a -> Alt f a
forall {k} (f :: k -> *) (a :: k). f a -> Alt f a
Monoid.Alt ([f a] -> [Alt f a]) -> (Alt f a -> [f a]) -> Alt f a -> [Alt f a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [f a]
forall a. Arbitrary a => a -> [a]
shrink (f a -> [f a]) -> (Alt f a -> f a) -> Alt f a -> [f a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Alt f a -> f a
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 = (Int -> Gen Version) -> Gen Version
forall a. (Int -> Gen a) -> Gen a
sized ((Int -> Gen Version) -> Gen Version)
-> (Int -> Gen Version) -> Gen Version
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 <- Int -> Gen Int -> Gen [Int]
forall a. Int -> Gen a -> Gen [a]
vectorOf (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Gen Int
forall a. Integral a => Gen a
arbitrarySizedNatural
       Version -> Gen Version
forall a. a -> Gen a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1 = Int
0
             | Bool
otherwise = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int
log2 (Int
n Int -> Int -> Int
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' <- [Int] -> [[Int]]
forall a. Arbitrary a => a -> [a]
shrink [Int]
xs
    , [Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
xs' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
    , (Int -> Bool) -> [Int] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>=Int
0) [Int]
xs'
    ]

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

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

  shrink :: ExitCode -> [ExitCode]
shrink (ExitFailure Int
x) = ExitCode
ExitSuccess ExitCode -> [ExitCode] -> [ExitCode]
forall a. a -> [a] -> [a]
: [ Int -> ExitCode
ExitFailure Int
x' | Int
x' <- Int -> [Int]
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 = [Newline] -> Gen Newline
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 (Newline -> Newline -> NewlineMode)
-> Gen Newline -> Gen (Newline -> NewlineMode)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Newline
forall a. Arbitrary a => Gen a
arbitrary Gen (Newline -> NewlineMode) -> Gen Newline -> Gen NewlineMode
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Newline
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') <- (Newline, Newline) -> [(Newline, Newline)]
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 = (a -> b -> r) -> Gen a -> Gen b -> Gen r
forall a b c. (a -> b -> c) -> Gen a -> Gen b -> Gen c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> r
f Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen b
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 = (a -> b -> c -> r) -> Gen a -> Gen b -> Gen c -> Gen r
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 Gen a
forall a. Arbitrary a => Gen a
arbitrary Gen b
forall a. Arbitrary a => Gen a
arbitrary Gen c
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 = ((a, b) -> c -> d -> r) -> Gen r
forall a b c r.
(Arbitrary a, Arbitrary b, Arbitrary c) =>
(a -> b -> c -> r) -> Gen r
applyArbitrary3 ((a -> b -> c -> d -> r) -> (a, b) -> c -> d -> r
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 =
  (Int -> Gen a) -> Gen a
forall a. (Int -> Gen a) -> Gen a
sized ((Int -> Gen a) -> Gen a) -> (Int -> Gen a) -> Gen a
forall a b. (a -> b) -> a -> b
$ \Int
n ->
  (Int -> a) -> Gen Int -> Gen a
forall a. Integral a => (Int -> a) -> Gen Int -> Gen a
inBounds Int -> a
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 =
  (Int -> Gen a) -> Gen a
forall a. (Int -> Gen a) -> Gen a
sized ((Int -> Gen a) -> Gen a) -> (Int -> Gen a) -> Gen a
forall a b. (a -> b) -> a -> b
$ \Int
n ->
  (Int -> a) -> Gen Int -> Gen a
forall a. Integral a => (Int -> a) -> Gen Int -> Gen a
inBounds Int -> a
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 = (Int -> a) -> Gen Int -> Gen a
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> a
fi (Gen Int
g Gen Int -> (Int -> Bool) -> Gen Int
forall a. Gen a -> (a -> Bool) -> Gen a
`suchThat` (\Int
x -> Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== a -> Integer
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 =
  (Int -> Gen a) -> Gen a
forall a. (Int -> Gen a) -> Gen a
sized ((Int -> Gen a) -> Gen a) -> (Int -> Gen a) -> Gen a
forall a b. (a -> b) -> a -> b
$ \Int
n -> do
    Int
denom <- (Int, Int) -> Gen Int
chooseInt (Int
1, Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 Int
n)
    Int
numer <- (Int, Int) -> Gen Int
chooseInt (-Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
denom, Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
denom)
    a -> Gen a
forall a. a -> Gen a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Gen a) -> a -> Gen a
forall a b. (a -> b) -> a -> b
$ Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
numer a -> a -> a
forall a. Fractional a => a -> a -> a
/ Int -> 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 a
forall a. Bounded a => a
minBound a
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 = (a, a) -> Gen a
forall a. (Bounded a, Integral a) => (a, a) -> Gen a
chooseBoundedIntegral (a
forall a. Bounded a => a
minBound, a
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 = (a, a) -> Gen a
forall a. Random a => (a, a) -> Gen a
choose (a
forall a. Bounded a => a
minBound,a
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 = (a, a) -> Gen a
forall a. Enum a => (a, a) -> Gen a
chooseEnum (a
forall a. Bounded a => a
minBound, a
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 =
  (a -> a -> Gen a) -> Gen a
forall a. Bounded a => (a -> a -> Gen a) -> Gen a
withBounds ((a -> a -> Gen a) -> Gen a) -> (a -> a -> Gen a) -> Gen a
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 t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
0 = a
1 a -> a -> a
forall a. Num a => a -> a -> a
+ t -> a
ilog2 (t
n t -> t -> t
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 = Integer -> Int
forall {t} {a}. (Num a, Integral t) => t -> a
ilog2 (a -> Integer
forall a. Integral a => a -> Integer
toInteger a
mx Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- a -> Integer
forall a. Integral a => a -> Integer
toInteger a
mn Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) in
  (Int -> Gen a) -> Gen a
forall a. (Int -> Gen a) -> Gen a
sized ((Int -> Gen a) -> Gen a) -> (Int -> Gen a) -> Gen a
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 Int -> Int -> Int
forall a. Ord a => a -> a -> a
`max` Int
40) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
k) Int -> Int -> Int
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 = a -> Integer
forall a. Integral a => a -> Integer
toInteger a
mn Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
`max` (-Integer
1 Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftL` (Int
power Int -> Int -> Int
forall a. Ord a => a -> a -> a
`min` Int
bits))
      hi :: Integer
hi = a -> Integer
forall a. Integral a => a -> Integer
toInteger a
mx Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
`min` (Integer
1 Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftL` (Int
power Int -> Int -> Int
forall a. Ord a => a -> a -> a
`min` Int
bits)) in
    (Integer -> a) -> Gen Integer -> Gen a
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> a
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 =
  Gen Char
forall a. (Bounded a, Enum a) => Gen a
arbitraryBoundedEnum Gen Char -> (Char -> Bool) -> Gen Char
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 = (Char, Char) -> Gen Char
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 = Gen Char
forall a. Arbitrary a => Gen a
arbitrary Gen Char -> (Char -> Bool) -> Gen Char
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 = (a -> b) -> (b -> a) -> (a -> [a]) -> b -> [b]
forall a b. (a -> b) -> (b -> a) -> (a -> [a]) -> b -> [b]
shrinkMapBy a -> b
f b -> a
g a -> [a]
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 = (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
f ([a] -> [b]) -> (b -> [a]) -> b -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shr (a -> [a]) -> (b -> a) -> b -> [a]
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 =
  [a] -> [a]
forall a. Eq a => [a] -> [a]
nub ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$
  [ -a
x
  | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0, -a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
x
  ] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++
  [ a
x'
  | a
x' <- (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (a -> a -> Bool
forall {a}. (Ord a, Num a) => a -> a -> Bool
<< a
x) (a
0a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[ a
x a -> a -> a
forall a. Num a => a -> a -> a
- a
i | a
i <- [a] -> [a]
forall a. HasCallStack => [a] -> [a]
tail ((a -> a) -> a -> [a]
forall a. (a -> a) -> a -> [a]
iterate (a -> a -> a
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 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0, a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0) of
            (Bool
True,  Bool
True)  -> a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
b
            (Bool
False, Bool
False) -> a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
b
            (Bool
True,  Bool
False) -> a
a a -> a -> a
forall a. Num a => a -> a -> a
+ a
b a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0
            (Bool
False, Bool
True)  -> a
a a -> a -> a
forall a. Num a => a -> a -> a
+ a
b a -> a -> Bool
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 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Bounded a => a
minBound =
    []
  | Bool
otherwise =
    Int -> a
forall a. Enum a => Int -> a
toEnum (Int -> a) -> [Int] -> [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> Bool) -> [Int] -> [Int]
forall a. (a -> Bool) -> [a] -> [a]
filter (Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
minBoundInt) (Int -> [Int]
forall a. Integral a => a -> [a]
shrinkIntegral (Int -> [Int]) -> Int -> [Int]
forall a b. (a -> b) -> a -> b
$ a -> Int
forall a. Enum a => a -> Int
fromEnum a
a)
  where
    minBoundInt :: Int
    minBoundInt :: Int
minBoundInt = a -> Int
forall a. Enum a => a -> Int
fromEnum (a
forall a. Bounded a => a
minBound a -> a -> a
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 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x)  = a
0 a -> [a] -> [a]
forall a. a -> [a] -> [a]
: (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
1000) [a]
numbers -- NaN
  | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 Bool -> Bool -> Bool
&& Bool -> Bool
not (a
2a -> a -> a
forall a. Num a => a -> a -> a
*a
xa -> a -> a
forall a. Num a => a -> a -> a
+a
1a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a
x) = a
0 a -> [a] -> [a]
forall a. a -> [a] -> [a]
: (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
x) [a]
numbers -- infinity
  | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = a -> a
forall a. Num a => a -> a
negate a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:(a -> a) -> [a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map a -> a
forall a. Num a => a -> a
negate (a -> [a]
forall a. RealFrac a => a -> [a]
shrinkRealFrac (a -> a
forall a. Num a => a -> a
negate a
x))
  | Bool
otherwise = -- x is finite and >= 0
    -- To ensure termination
    (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
filter (\a
y -> a -> a
forall a. Num a => a -> a
abs a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a -> a
forall a. Num a => a -> a
abs a
x) ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$
      -- Try shrinking to an integer first
      (Integer -> a) -> [Integer] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> a
forall a. Num a => Integer -> a
fromInteger (Integer -> [Integer]
forall a. Arbitrary a => a -> [a]
shrink (a -> Integer
forall b. Integral b => a -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate a
x) [Integer] -> [Integer] -> [Integer]
forall a. [a] -> [a] -> [a]
++ [a -> Integer
forall b. Integral b => a -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate a
x]) [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++
      -- Shrink the numerator
      [Rational -> a
forall a. Fractional a => Rational -> a
fromRational (Integer
num' Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
denom) | Integer
num' <- Integer -> [Integer]
forall a. Arbitrary a => a -> [a]
shrink Integer
num] [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++
      -- Shrink the denominator, and keep the fraction as close
      -- to the original as possible, rounding towards zero
      [Rational -> a
forall a. Fractional a => Rational -> a
fromRational (Rational -> Integer
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate (Integer
num Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
denom' Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
denom) Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
denom')
      | Integer
denom' <- Integer -> [Integer]
forall a. Arbitrary a => a -> [a]
shrink Integer
denom, Integer
denom' Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0 ]
  where
    num :: Integer
num = Rational -> Integer
forall a. Ratio a -> a
numerator (a -> Rational
forall a. Real a => a -> Rational
toRational a
x)
    denom :: Integer
denom = Rational -> Integer
forall a. Ratio a -> a
denominator (a -> Rational
forall a. Real a => a -> Rational
toRational a
x)
    numbers :: [a]
numbers = (a -> a) -> a -> [a]
forall a. (a -> a) -> a -> [a]
iterate (a -> a -> a
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 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x)  = a
0 a -> [a] -> [a]
forall a. a -> [a] -> [a]
: (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
1000) [a]
numbers -- NaN
  | Bool -> Bool
not (a
2a -> a -> a
forall a. Num a => a -> a -> a
*a -> a
forall a. Num a => a -> a
abs a
xa -> a -> a
forall a. Num a => a -> a -> a
+a
1a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a -> a
forall a. Num a => a -> a
abs a
x) = a
0 a -> [a] -> [a]
forall a. a -> [a] -> [a]
: (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<a
x) [a]
numbers -- infinity
  | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = a -> a
forall a. Num a => a -> a
negate a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:(a -> a) -> [a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map a -> a
forall a. Num a => a -> a
negate (a -> [a]
forall a. RealFrac a => a -> [a]
shrinkDecimal (a -> a
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 <- Int -> [Rational] -> [Rational]
forall a. Int -> [a] -> [a]
take Int
6 ((Rational -> Rational) -> Rational -> [Rational]
forall a. (a -> a) -> a -> [a]
iterate (Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
*Rational
10) Rational
1),
      let m :: Integer
m = Rational -> Integer
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (a -> Rational
forall a. Real a => a -> Rational
toRational a
x Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
* Rational
precision),
      Rational
precision Rational -> Rational -> Bool
forall a. Eq a => a -> a -> Bool
== Rational
1 Bool -> Bool -> Bool
|| Integer
m Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
10 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0, -- don't allow shrinking to increase digits
      Integer
n <- Integer
mInteger -> [Integer] -> [Integer]
forall a. a -> [a] -> [a]
:Integer -> [Integer]
forall a. Arbitrary a => a -> [a]
shrink Integer
m,
      let y :: a
y = Rational -> a
forall a. Fractional a => Rational -> a
fromRational (Integer -> Rational
forall a. Num a => Integer -> a
fromInteger Integer
n Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational
precision),
      a -> a
forall a. Num a => a -> a
abs a
y a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a -> a
forall a. Num a => a -> a
abs a
x ]
  where
    -- 1, 2, 3, ..., 10, 20, 30, ..., 100, 200, 300, etc.
    numbers :: [a]
numbers = [[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[a]] -> [a]) -> [[a]] -> [a]
forall a b. (a -> b) -> a -> b
$ ([a] -> [a]) -> [a] -> [[a]]
forall a. (a -> a) -> a -> [a]
iterate ((a -> a) -> [a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (a -> a -> a
forall a. Num a => a -> a -> a
*a
10)) ((Integer -> a) -> [Integer] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> a
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 = a -> Gen b -> Gen b
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 = Rep a Any -> Gen b -> Gen b
forall a b. Rep a a -> Gen b -> Gen b
forall {k} (f :: k -> *) (a :: k) b.
GCoArbitrary f =>
f a -> Gen b -> Gen b
gCoarbitrary (Rep a Any -> Gen b -> Gen b)
-> (a -> Rep a Any) -> a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a Any
forall x. a -> Rep a x
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 = Gen b -> Gen b
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) = f a -> Gen b -> Gen b
forall (a :: k) b. f a -> Gen b -> Gen b
forall {k} (f :: k -> *) (a :: k) b.
GCoArbitrary f =>
f a -> Gen b -> Gen b
gCoarbitrary f a
l (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. g a -> Gen b -> Gen b
forall (a :: k) b. g a -> Gen b -> Gen b
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) = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0 (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> Gen b -> Gen b
forall (a :: k) b. f a -> Gen b -> Gen b
forall {k} (f :: k -> *) (a :: k) b.
GCoArbitrary f =>
f a -> Gen b -> Gen b
gCoarbitrary f a
x
  gCoarbitrary (R1 g a
x) = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1 (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. g a -> Gen b -> Gen b
forall (a :: k) b. g a -> Gen b -> Gen b
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) = f a -> Gen b -> Gen b
forall (a :: k) b. f a -> Gen b -> Gen b
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) = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
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
(><) = (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 <- Gen [a]
forall a. Arbitrary a => Gen a
arbitrary
       [b] -> Gen b -> Gen b
forall b. [b] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary ((a -> b) -> [a] -> [b]
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 ()
_ = Gen b -> Gen b
forall a. a -> a
id

instance CoArbitrary Bool where
  coarbitrary :: forall b. Bool -> Gen b -> Gen b
coarbitrary Bool
False = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
  coarbitrary Bool
True  = Integer -> Gen b -> Gen b
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 = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
  coarbitrary Ordering
EQ = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1
  coarbitrary Ordering
LT = Integer -> Gen b -> Gen b
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  = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
  coarbitrary (Just a
x) = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1 (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
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)  = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0 (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
  coarbitrary (Right b
y) = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1 (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Gen b -> Gen b
forall b. b -> Gen b -> Gen b
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 []     = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
  coarbitrary (a
x:[a]
xs) = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
1 (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, [a]) -> Gen b -> Gen b
forall b. (a, [a]) -> Gen b -> Gen b
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 = (a, a) -> Gen b -> Gen b
forall b. (a, a) -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Ratio a -> a
forall a. Ratio a -> a
numerator Ratio a
r,Ratio a -> a
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 = Fixed a -> Gen b -> Gen b
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) = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
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) = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
                    (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Gen b -> Gen b
forall b. b -> Gen b -> Gen b
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) = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
                      (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Gen b -> Gen b
forall b. b -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary b
y
                      (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> Gen b -> Gen b
forall b. c -> Gen b -> Gen b
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) = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
                        (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Gen b -> Gen b
forall b. b -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary b
y
                        (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> Gen b -> Gen b
forall b. c -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary c
z
                        (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. d -> Gen b -> Gen b
forall b. d -> Gen b -> Gen b
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) = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
x
                          (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Gen b -> Gen b
forall b. b -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary b
y
                          (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> Gen b -> Gen b
forall b. c -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary c
z
                          (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. d -> Gen b -> Gen b
forall b. d -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary d
v
                          (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> Gen b -> Gen b
forall b. e -> Gen b -> Gen b
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 = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
coarbitraryIntegral

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

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

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

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

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

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

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

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

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

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

instance CoArbitrary Char where
  coarbitrary :: forall b. Char -> Gen b -> Gen b
coarbitrary = Int -> Gen b -> Gen b
forall b. Int -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Int -> Gen b -> Gen b) -> (Char -> Int) -> Char -> Gen b -> Gen b
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 = Float -> Gen b -> Gen b
forall a b. Real a => a -> Gen b -> Gen b
coarbitraryReal

instance CoArbitrary Double where
  coarbitrary :: forall b. Double -> Gen b -> Gen b
coarbitrary = Double -> Gen b -> Gen b
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 = [a] -> Gen b -> Gen b
forall b. [a] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary([a] -> Gen b -> Gen b)
-> (Set a -> [a]) -> Set a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set a -> [a]
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 = [(k, v)] -> Gen b -> Gen b
forall b. [(k, v)] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary ([(k, v)] -> Gen b -> Gen b)
-> (Map k v -> [(k, v)]) -> Map k v -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k v -> [(k, v)]
forall k a. Map k a -> [(k, a)]
Map.toList
instance CoArbitrary IntSet.IntSet where
  coarbitrary :: forall b. IntSet -> Gen b -> Gen b
coarbitrary = [Int] -> Gen b -> Gen b
forall b. [Int] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary ([Int] -> Gen b -> Gen b)
-> (IntSet -> [Int]) -> IntSet -> Gen b -> Gen b
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 = [(Int, a)] -> Gen b -> Gen b
forall b. [(Int, a)] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary ([(Int, a)] -> Gen b -> Gen b)
-> (IntMap a -> [(Int, a)]) -> IntMap a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap a -> [(Int, a)]
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 = [a] -> Gen b -> Gen b
forall b. [a] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary ([a] -> Gen b -> Gen b)
-> (Seq a -> [a]) -> Seq a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq a -> [a]
forall a. Seq a -> [a]
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) = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary a
val (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Tree a] -> Gen b -> Gen b
forall b. [Tree a] -> Gen b -> Gen b
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 = [a] -> Gen b -> Gen b
forall b. [a] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary ([a] -> Gen b -> Gen b)
-> (ZipList a -> [a]) -> ZipList a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ZipList a -> [a]
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 = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (a -> Gen b -> Gen b)
-> (Identity a -> a) -> Identity a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
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 = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (a -> Gen b -> Gen b)
-> (Constant a b -> a) -> Constant a b -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Constant a b -> a
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 = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (a -> Gen b -> Gen b)
-> (Const a b -> a) -> Const a b -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Const a b -> a
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 = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (a -> Gen b -> Gen b) -> (Dual a -> a) -> Dual a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dual a -> a
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 = (a -> a) -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
forall b. (a -> a) -> Gen b -> Gen b
coarbitrary ((a -> a) -> Gen b -> Gen b)
-> (Endo a -> a -> a) -> Endo a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Endo a -> a -> a
forall a. Endo a -> a -> a
Monoid.appEndo

instance CoArbitrary Monoid.All where
  coarbitrary :: forall b. All -> Gen b -> Gen b
coarbitrary = Bool -> Gen b -> Gen b
forall b. Bool -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Bool -> Gen b -> Gen b) -> (All -> Bool) -> All -> Gen b -> Gen b
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 = Bool -> Gen b -> Gen b
forall b. Bool -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Bool -> Gen b -> Gen b) -> (Any -> Bool) -> Any -> Gen b -> Gen b
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 = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (a -> Gen b -> Gen b) -> (Sum a -> a) -> Sum a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sum a -> a
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 = a -> Gen b -> Gen b
forall b. a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (a -> Gen b -> Gen b)
-> (Product a -> a) -> Product a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Product a -> a
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 = Maybe a -> Gen b -> Gen b
forall b. Maybe a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Maybe a -> Gen b -> Gen b)
-> (First a -> Maybe a) -> First a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. First a -> Maybe a
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 = Maybe a -> Gen b -> Gen b
forall b. Maybe a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (Maybe a -> Gen b -> Gen b)
-> (Last a -> Maybe a) -> Last a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Last a -> Maybe a
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 = f a -> Gen b -> Gen b
forall b. f a -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (f a -> Gen b -> Gen b)
-> (Alt f a -> f a) -> Alt f a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Alt f a -> f a
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) = ([Int], [[Char]]) -> Gen b -> Gen b
forall b. ([Int], [[Char]]) -> Gen b -> Gen 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 = Integer -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant Integer
0
  coarbitrary Newline
CRLF = Integer -> Gen b -> Gen b
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) = Newline -> Gen b -> Gen b
forall b. Newline -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary Newline
inNL (Gen b -> Gen b) -> (Gen b -> Gen b) -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Newline -> Gen b -> Gen b
forall b. Newline -> Gen b -> Gen b
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 = a -> Gen b -> Gen b
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 = Rational -> Gen b -> Gen b
forall b. Rational -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (a -> Rational
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 = [Char] -> Gen b -> Gen b
forall b. [Char] -> Gen b -> Gen b
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (a -> [Char]
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 = Int -> Gen b -> Gen b
forall n a. Integral n => n -> Gen a -> Gen a
variant (Int -> Gen b -> Gen b) -> (a -> Int) -> a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
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 = Int -> Gen a -> Gen [a]
forall a. Int -> Gen a -> Gen [a]
vectorOf Int
k Gen a
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 = [a] -> [a]
forall a. Ord a => [a] -> [a]
sort ([a] -> [a]) -> Gen [a] -> Gen [a]
forall a b. (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Gen [a]
forall a. Arbitrary a => Gen a
arbitrary

-- | Generates an infinite list.
infiniteList :: Arbitrary a => Gen [a]
infiniteList :: forall a. Arbitrary a => Gen [a]
infiniteList = Gen a -> Gen [a]
forall a. Gen a -> Gen [a]
infiniteListOf Gen a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0    = a
x
                      | Bool
otherwise = Int -> Stream a -> a
forall a. Int -> Stream a -> a
streamNth (Int
n Int -> Int -> Int
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 Rational -> Stream Rational -> Stream Rational
forall a. a -> Stream a -> Stream a
:< Rational
1 Rational -> Stream Rational -> Stream Rational
forall a. a -> Stream a -> Stream a
:< (-Rational
1) Rational -> Stream Rational -> Stream Rational
forall a. a -> Stream a -> Stream a
:< Stream Rational -> Stream Rational
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 = a -> a
forall a. Fractional a => a -> a
recip a
x
          nrx :: a
nrx = -a
rx
      in a
nx a -> Stream a -> Stream a
forall a b. a -> b -> b
`seq` a
rx a -> Stream a -> Stream a
forall a b. a -> b -> b
`seq` a
nrx a -> Stream a -> Stream a
forall a b. a -> b -> b
`seq` (a
x a -> Stream a -> Stream a
forall a. a -> Stream a -> Stream a
:< a
rx a -> Stream a -> Stream a
forall a. a -> Stream a -> Stream a
:< a
nx a -> Stream a -> Stream a
forall a. a -> Stream a -> Stream a
:< a
nrx a -> Stream a -> Stream a
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 Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
2) Rational -> Stream Rational -> Stream Rational
forall a. a -> Stream a -> Stream a
:< Stream Rational -> Stream Rational
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 Ratio a -> Stream (Ratio a) -> Stream (Ratio a)
forall a b. a -> b -> b
`seq` Ratio a
rChild Ratio a -> Stream (Ratio a) -> Stream (Ratio a)
forall a b. a -> b -> b
`seq`
        (Ratio a
lChild Ratio a -> Stream (Ratio a) -> Stream (Ratio a)
forall a. a -> Stream a -> Stream a
:< Ratio a
rChild Ratio a -> Stream (Ratio a) -> Stream (Ratio a)
forall a. a -> Stream a -> Stream a
:< Stream (Ratio a) -> Stream (Ratio a)
go Stream (Ratio a)
xs)
      where
        nd :: a
nd = Ratio a -> a
forall a. Ratio a -> a
numerator Ratio a
x a -> a -> a
forall a. Num a => a -> a -> a
+ Ratio a -> a
forall a. Ratio a -> a
denominator Ratio a
x
        lChild :: Ratio a
lChild = Ratio a -> a
forall a. Ratio a -> a
numerator Ratio a
x a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
% a
nd
        rChild :: Ratio a
rChild = a
nd a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
% Ratio a -> a
forall a. Ratio a -> a
denominator Ratio a
x

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