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

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

import Control.Applicative (liftA2)
import Data.List.NonEmpty  (NonEmpty (..), nonEmpty)
import Data.Maybe          (mapMaybe)

import Test.QuickCheck

import qualified Data.Semigroup as Semi

-------------------------------------------------------------------------------
-- semigroups
-------------------------------------------------------------------------------

instance Arbitrary1 NonEmpty where
  liftArbitrary :: forall a. Gen a -> Gen (NonEmpty a)
liftArbitrary Gen a
arb = (a -> [a] -> NonEmpty a) -> Gen a -> Gen [a] -> Gen (NonEmpty a)
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 -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
(:|) Gen a
arb (Gen a -> Gen [a]
forall a. Gen a -> Gen [a]
forall (f :: * -> *) a. Arbitrary1 f => Gen a -> Gen (f a)
liftArbitrary Gen a
arb)
  liftShrink :: forall a. (a -> [a]) -> NonEmpty a -> [NonEmpty a]
liftShrink a -> [a]
shr (a
x :| [a]
xs) = ([a] -> Maybe (NonEmpty a)) -> [[a]] -> [NonEmpty a]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe [a] -> Maybe (NonEmpty a)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty ([[a]] -> [NonEmpty a]) -> ([a] -> [[a]]) -> [a] -> [NonEmpty 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] -> [NonEmpty a]) -> [a] -> [NonEmpty a]
forall a b. (a -> b) -> a -> b
$ a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs

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

instance CoArbitrary a => CoArbitrary (NonEmpty a) where
  coarbitrary :: forall b. NonEmpty a -> Gen b -> Gen b
coarbitrary (a
x :| [a]
xs) = (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 Function a => Function (NonEmpty a) where
  function :: forall b. (NonEmpty a -> b) -> NonEmpty a :-> b
function = (NonEmpty a -> (a, [a]))
-> ((a, [a]) -> NonEmpty a)
-> (NonEmpty a -> b)
-> NonEmpty a :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap NonEmpty a -> (a, [a])
forall {a}. NonEmpty a -> (a, [a])
g (a, [a]) -> NonEmpty a
forall {a}. (a, [a]) -> NonEmpty a
h
   where
     g :: NonEmpty a -> (a, [a])
g (a
x :| [a]
xs) = (a
x,   [a]
xs)
     h :: (a, [a]) -> NonEmpty a
h (a
x,   [a]
xs) =  a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
xs


instance Arbitrary1 Semi.Min where
    liftArbitrary :: forall a. Gen a -> Gen (Min a)
liftArbitrary Gen a
arb = a -> Min a
forall a. a -> Min a
Semi.Min (a -> Min a) -> Gen a -> Gen (Min a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen a
arb
    liftShrink :: forall a. (a -> [a]) -> Min a -> [Min a]
liftShrink a -> [a]
shr = (a -> Min a) -> [a] -> [Min a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Min a
forall a. a -> Min a
Semi.Min ([a] -> [Min a]) -> (Min a -> [a]) -> Min a -> [Min a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shr (a -> [a]) -> (Min a -> a) -> Min a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Min a -> a
forall a. Min a -> a
Semi.getMin

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

instance CoArbitrary a => CoArbitrary (Semi.Min a) where
    coarbitrary :: forall b. Min 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) -> (Min a -> a) -> Min a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Min a -> a
forall a. Min a -> a
Semi.getMin

instance Function a => Function (Semi.Min a) where
    function :: forall b. (Min a -> b) -> Min a :-> b
function = (Min a -> a) -> (a -> Min a) -> (Min a -> b) -> Min a :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap Min a -> a
forall a. Min a -> a
Semi.getMin a -> Min a
forall a. a -> Min a
Semi.Min


instance Arbitrary1 Semi.Max where
    liftArbitrary :: forall a. Gen a -> Gen (Max a)
liftArbitrary Gen a
arb = a -> Max a
forall a. a -> Max a
Semi.Max (a -> Max a) -> Gen a -> Gen (Max a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen a
arb
    liftShrink :: forall a. (a -> [a]) -> Max a -> [Max a]
liftShrink a -> [a]
shr = (a -> Max a) -> [a] -> [Max a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Max a
forall a. a -> Max a
Semi.Max ([a] -> [Max a]) -> (Max a -> [a]) -> Max a -> [Max a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shr (a -> [a]) -> (Max a -> a) -> Max a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Max a -> a
forall a. Max a -> a
Semi.getMax

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

instance CoArbitrary a => CoArbitrary (Semi.Max a) where
    coarbitrary :: forall b. Max 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) -> (Max a -> a) -> Max a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Max a -> a
forall a. Max a -> a
Semi.getMax

instance Function a => Function (Semi.Max a) where
    function :: forall b. (Max a -> b) -> Max a :-> b
function = (Max a -> a) -> (a -> Max a) -> (Max a -> b) -> Max a :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap Max a -> a
forall a. Max a -> a
Semi.getMax a -> Max a
forall a. a -> Max a
Semi.Max


instance Arbitrary1 Semi.First where
    liftArbitrary :: forall a. Gen a -> Gen (First a)
liftArbitrary Gen a
arb = a -> First a
forall a. a -> First a
Semi.First (a -> First a) -> Gen a -> Gen (First a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen a
arb
    liftShrink :: forall a. (a -> [a]) -> First a -> [First a]
liftShrink a -> [a]
shr = (a -> First a) -> [a] -> [First a]
forall a b. (a -> b) -> [a] -> [b]
map a -> First a
forall a. a -> First a
Semi.First ([a] -> [First a]) -> (First a -> [a]) -> First a -> [First a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shr (a -> [a]) -> (First a -> a) -> First a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. First a -> a
forall a. First a -> a
Semi.getFirst

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

instance CoArbitrary a => CoArbitrary (Semi.First a) where
    coarbitrary :: forall b. First 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)
-> (First a -> a) -> First a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. First a -> a
forall a. First a -> a
Semi.getFirst

instance Function a => Function (Semi.First a) where
    function :: forall b. (First a -> b) -> First a :-> b
function = (First a -> a) -> (a -> First a) -> (First a -> b) -> First a :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap First a -> a
forall a. First a -> a
Semi.getFirst a -> First a
forall a. a -> First a
Semi.First


instance Arbitrary1 Semi.Last where
    liftArbitrary :: forall a. Gen a -> Gen (Last a)
liftArbitrary Gen a
arb = a -> Last a
forall a. a -> Last a
Semi.Last (a -> Last a) -> Gen a -> Gen (Last a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen a
arb
    liftShrink :: forall a. (a -> [a]) -> Last a -> [Last a]
liftShrink a -> [a]
shr = (a -> Last a) -> [a] -> [Last a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Last a
forall a. a -> Last a
Semi.Last ([a] -> [Last a]) -> (Last a -> [a]) -> Last a -> [Last a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shr (a -> [a]) -> (Last a -> a) -> Last a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Last a -> a
forall a. Last a -> a
Semi.getLast

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

instance CoArbitrary a => CoArbitrary (Semi.Last a) where
    coarbitrary :: forall b. Last 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) -> (Last a -> a) -> Last a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Last a -> a
forall a. Last a -> a
Semi.getLast

instance Function a => Function (Semi.Last a) where
    function :: forall b. (Last a -> b) -> Last a :-> b
function = (Last a -> a) -> (a -> Last a) -> (Last a -> b) -> Last a :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap Last a -> a
forall a. Last a -> a
Semi.getLast a -> Last a
forall a. a -> Last a
Semi.Last


instance Arbitrary1 Semi.WrappedMonoid where
    liftArbitrary :: forall a. Gen a -> Gen (WrappedMonoid a)
liftArbitrary Gen a
arb = a -> WrappedMonoid a
forall m. m -> WrappedMonoid m
Semi.WrapMonoid (a -> WrappedMonoid a) -> Gen a -> Gen (WrappedMonoid a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen a
arb
    liftShrink :: forall a. (a -> [a]) -> WrappedMonoid a -> [WrappedMonoid a]
liftShrink a -> [a]
shr = (a -> WrappedMonoid a) -> [a] -> [WrappedMonoid a]
forall a b. (a -> b) -> [a] -> [b]
map a -> WrappedMonoid a
forall m. m -> WrappedMonoid m
Semi.WrapMonoid ([a] -> [WrappedMonoid a])
-> (WrappedMonoid a -> [a]) -> WrappedMonoid a -> [WrappedMonoid a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
shr (a -> [a]) -> (WrappedMonoid a -> a) -> WrappedMonoid a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WrappedMonoid a -> a
forall m. WrappedMonoid m -> m
Semi.unwrapMonoid

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

instance CoArbitrary a => CoArbitrary (Semi.WrappedMonoid a) where
    coarbitrary :: forall b. WrappedMonoid 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)
-> (WrappedMonoid a -> a) -> WrappedMonoid a -> Gen b -> Gen b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WrappedMonoid a -> a
forall m. WrappedMonoid m -> m
Semi.unwrapMonoid

instance Function a => Function (Semi.WrappedMonoid a) where
    function :: forall b. (WrappedMonoid a -> b) -> WrappedMonoid a :-> b
function = (WrappedMonoid a -> a)
-> (a -> WrappedMonoid a)
-> (WrappedMonoid a -> b)
-> WrappedMonoid a :-> b
forall b a c.
Function b =>
(a -> b) -> (b -> a) -> (a -> c) -> a :-> c
functionMap WrappedMonoid a -> a
forall m. WrappedMonoid m -> m
Semi.unwrapMonoid a -> WrappedMonoid a
forall m. m -> WrappedMonoid m
Semi.WrapMonoid

#if !(MIN_VERSION_base(4,16,0))
instance Arbitrary1 Semi.Option where
    liftArbitrary arb = Semi.Option <$> liftArbitrary arb
    liftShrink shr = map Semi.Option . liftShrink shr . Semi.getOption

instance Arbitrary a => Arbitrary (Semi.Option a) where
    arbitrary = arbitrary1
    shrink = shrink1

instance CoArbitrary a => CoArbitrary (Semi.Option a) where
    coarbitrary = coarbitrary . Semi.getOption

instance Function a => Function (Semi.Option a) where
    function = functionMap Semi.getOption Semi.Option
#endif