{-# LANGUAGE CPP              #-}
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Test.QuickCheck.Instances.These () where

import Prelude ()
import Test.QuickCheck.Instances.CustomPrelude

import Test.QuickCheck

import Data.Functor.These (These1 (..))
import Data.These         (These (..))

-------------------------------------------------------------------------------
-- These
-------------------------------------------------------------------------------

-- | @since 0.3.23
instance Arbitrary2 These where
    liftArbitrary2 :: forall a b. Gen a -> Gen b -> Gen (These a b)
liftArbitrary2 Gen a
arbA Gen b
arbB = forall a. [Gen a] -> Gen a
oneof
        [ forall a b. a -> These a b
This forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen a
arbA
        , forall a b. b -> These a b
That forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen b
arbB
        , forall a b. a -> b -> These a b
These forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen a
arbA forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen b
arbB
        ]

    liftShrink2 :: forall a b. (a -> [a]) -> (b -> [b]) -> These a b -> [These a b]
liftShrink2  a -> [a]
shrA b -> [b]
_shrB (This a
x) = forall a b. a -> These a b
This forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> [a]
shrA a
x
    liftShrink2 a -> [a]
_shrA  b -> [b]
shrB (That b
y) = forall a b. b -> These a b
That forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> [b]
shrB b
y
    liftShrink2  a -> [a]
shrA  b -> [b]
shrB (These a
x b
y) =
        [forall a b. a -> These a b
This a
x, forall a b. b -> These a b
That b
y] forall a. [a] -> [a] -> [a]
++ [forall a b. a -> b -> These a b
These a
x' b
y' | (a
x', b
y') <- forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 a -> [a]
shrA b -> [b]
shrB (a
x, b
y)]

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

-- | @since 0.3.23
instance (Arbitrary a, Arbitrary b) => Arbitrary (These a b) where
    arbitrary :: Gen (These a b)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
    shrink :: These a b -> [These a b]
shrink = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1

-- | @since 0.3.23
instance (Function a, Function b) => Function (These a b) where
  function :: forall b. (These a b -> b) -> These a b :-> b
function = forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap forall {a} {b}. These a b -> Either a (Either b (a, b))
g forall {a} {b}. Either a (Either b (a, b)) -> These a b
f
    where
      g :: These a b -> Either a (Either b (a, b))
g (This a
a)    = forall a b. a -> Either a b
Left a
a
      g (That b
b)    = forall a b. b -> Either a b
Right (forall a b. a -> Either a b
Left b
b)
      g (These a
a b
b) = forall a b. b -> Either a b
Right (forall a b. b -> Either a b
Right (a
a, b
b))

      f :: Either a (Either b (a, b)) -> These a b
f (Left a
a)               = forall a b. a -> These a b
This a
a
      f (Right (Left b
b))       = forall a b. b -> These a b
That b
b
      f (Right (Right (a
a, b
b))) = forall a b. a -> b -> These a b
These a
a b
b

-- | @since 0.3.23
instance (CoArbitrary a, CoArbitrary b) => CoArbitrary (These a b)

-------------------------------------------------------------------------------
-- These1
-------------------------------------------------------------------------------

-- | @since 0.3.23
instance (Arbitrary1 f, Arbitrary1 g) => Arbitrary1 (These1 f g) where
    liftArbitrary :: forall a. Gen a -> Gen (These1 f g a)
liftArbitrary Gen a
arb = forall a. [Gen a] -> Gen a
oneof
        [ forall (f :: * -> *) (g :: * -> *) a. f a -> These1 f g a
This1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary Gen a
arb
        , forall (f :: * -> *) (g :: * -> *) a. g a -> These1 f g a
That1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary Gen a
arb
        , forall (f :: * -> *) (g :: * -> *) a. f a -> g a -> These1 f g a
These1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary Gen a
arb forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary Gen a
arb
        ]

    liftShrink :: forall a. (a -> [a]) -> These1 f g a -> [These1 f g a]
liftShrink a -> [a]
shr (This1 f a
x) = forall (f :: * -> *) (g :: * -> *) a. f a -> These1 f g a
This1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr f a
x
    liftShrink a -> [a]
shr (That1 g a
y) = forall (f :: * -> *) (g :: * -> *) a. g a -> These1 f g a
That1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr g a
y
    liftShrink a -> [a]
shr (These1 f a
x g a
y) =
        [ forall (f :: * -> *) (g :: * -> *) a. f a -> These1 f g a
This1 f a
x, forall (f :: * -> *) (g :: * -> *) a. g a -> These1 f g a
That1 g a
y ] forall a. [a] -> [a] -> [a]
++
        [ forall (f :: * -> *) (g :: * -> *) a. f a -> g a -> These1 f g a
These1 f a
x' g a
y'
        | (f a
x', g a
y') <- forall (f :: * -> * -> *) a b.
Arbitrary2 f =>
(a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 (forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr) (forall (f :: * -> *) a. Arbitrary1 f => (a -> [a]) -> f a -> [f a]
liftShrink a -> [a]
shr) (f a
x, g a
y)
        ]

-- | @since 0.3.23
instance (Arbitrary1 f, Arbitrary1 g, Arbitrary a) => Arbitrary (These1 f g a) where
    arbitrary :: Gen (These1 f g a)
arbitrary = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1
    shrink :: These1 f g a -> [These1 f g a]
shrink    = forall (f :: * -> *) a. (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1