{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}

#if HAVE_QUANTIFIED_CONSTRAINTS
{-# LANGUAGE QuantifiedConstraints #-}
#endif

{-# OPTIONS_GHC -Wall #-}

module Test.QuickCheck.Classes.Alternative
  (
#if HAVE_UNARY_LAWS
    alternativeLaws
#endif
  ) where

import Control.Applicative (Alternative(..))
import Test.QuickCheck hiding ((.&.))
#if HAVE_UNARY_LAWS
import Test.QuickCheck.Arbitrary (Arbitrary1(..))
import Data.Functor.Classes (Eq1,Show1)
#endif
import Test.QuickCheck.Property (Property)

import Test.QuickCheck.Classes.Internal

#if HAVE_UNARY_LAWS

-- | Tests the following alternative properties:
--
-- [/Left Identity/]
--   @'empty' '<|>' x ≡ x@
-- [/Right Identity/]
--   @x '<|>' 'empty' ≡ x@
-- [/Associativity/]
--   @a '<|>' (b '<|>' c) ≡ (a '<|>' b) '<|>' c)@
alternativeLaws ::
#if HAVE_QUANTIFIED_CONSTRAINTS
  (Alternative f, forall a. Eq a => Eq (f a), forall a. Show a => Show (f a), forall a. Arbitrary a => Arbitrary (f a))
#else
  (Alternative f, Eq1 f, Show1 f, Arbitrary1 f)
#endif
  => proxy f -> Laws
alternativeLaws :: proxy f -> Laws
alternativeLaws proxy f
p = String -> [(String, Property)] -> Laws
Laws String
"Alternative"
  [ (String
"Left Identity", proxy f -> Property
forall (proxy :: (* -> *) -> *) (f :: * -> *).
(Alternative f, forall a. Eq a => Eq (f a),
 forall a. Show a => Show (f a),
 forall a. Arbitrary a => Arbitrary (f a)) =>
proxy f -> Property
alternativeLeftIdentity proxy f
p)
  , (String
"Right Identity", proxy f -> Property
forall (proxy :: (* -> *) -> *) (f :: * -> *).
(Alternative f, forall a. Eq a => Eq (f a),
 forall a. Show a => Show (f a),
 forall a. Arbitrary a => Arbitrary (f a)) =>
proxy f -> Property
alternativeRightIdentity proxy f
p)
  , (String
"Associativity", proxy f -> Property
forall (proxy :: (* -> *) -> *) (f :: * -> *).
(Alternative f, forall a. Eq a => Eq (f a),
 forall a. Show a => Show (f a),
 forall a. Arbitrary a => Arbitrary (f a)) =>
proxy f -> Property
alternativeAssociativity proxy f
p)
  ]

alternativeLeftIdentity :: forall proxy f.
#if HAVE_QUANTIFIED_CONSTRAINTS
  (Alternative f, forall a. Eq a => Eq (f a), forall a. Show a => Show (f a), forall a. Arbitrary a => Arbitrary (f a))
#else
  (Alternative f, Eq1 f, Show1 f, Arbitrary1 f)
#endif
  => proxy f -> Property
alternativeLeftIdentity :: proxy f -> Property
alternativeLeftIdentity proxy f
_ = (Apply f Integer -> Bool) -> Property
forall prop. Testable prop => prop -> Property
property ((Apply f Integer -> Bool) -> Property)
-> (Apply f Integer -> Bool) -> Property
forall a b. (a -> b) -> a -> b
$ \(Apply (f Integer
a :: f Integer)) -> (f Integer -> f Integer -> Bool
forall (f :: * -> *) a.
(forall x. Eq x => Eq (f x), Eq a) =>
f a -> f a -> Bool
eq1 (f Integer
forall (f :: * -> *) a. Alternative f => f a
empty f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> f Integer
a) f Integer
a)

alternativeRightIdentity :: forall proxy f.
#if HAVE_QUANTIFIED_CONSTRAINTS
  (Alternative f, forall a. Eq a => Eq (f a), forall a. Show a => Show (f a), forall a. Arbitrary a => Arbitrary (f a))
#else
  (Alternative f, Eq1 f, Show1 f, Arbitrary1 f)
#endif
  => proxy f -> Property
alternativeRightIdentity :: proxy f -> Property
alternativeRightIdentity proxy f
_ = (Apply f Integer -> Bool) -> Property
forall prop. Testable prop => prop -> Property
property ((Apply f Integer -> Bool) -> Property)
-> (Apply f Integer -> Bool) -> Property
forall a b. (a -> b) -> a -> b
$ \(Apply (f Integer
a :: f Integer)) -> (f Integer -> f Integer -> Bool
forall (f :: * -> *) a.
(forall x. Eq x => Eq (f x), Eq a) =>
f a -> f a -> Bool
eq1 f Integer
a (f Integer
forall (f :: * -> *) a. Alternative f => f a
empty f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> f Integer
a))

alternativeAssociativity :: forall proxy f.
#if HAVE_QUANTIFIED_CONSTRAINTS
  (Alternative f, forall a. Eq a => Eq (f a), forall a. Show a => Show (f a), forall a. Arbitrary a => Arbitrary (f a))
#else
  (Alternative f, Eq1 f, Show1 f, Arbitrary1 f)
#endif
  => proxy f -> Property
alternativeAssociativity :: proxy f -> Property
alternativeAssociativity proxy f
_ = (Apply f Integer -> Apply f Integer -> Apply f Integer -> Bool)
-> Property
forall prop. Testable prop => prop -> Property
property ((Apply f Integer -> Apply f Integer -> Apply f Integer -> Bool)
 -> Property)
-> (Apply f Integer -> Apply f Integer -> Apply f Integer -> Bool)
-> Property
forall a b. (a -> b) -> a -> b
$ \(Apply (f Integer
a :: f Integer)) (Apply (f Integer
b :: f Integer)) (Apply (f Integer
c :: f Integer)) -> f Integer -> f Integer -> Bool
forall (f :: * -> *) a.
(forall x. Eq x => Eq (f x), Eq a) =>
f a -> f a -> Bool
eq1 (f Integer
a f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (f Integer
b f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> f Integer
c)) ((f Integer
a f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> f Integer
b) f Integer -> f Integer -> f Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> f Integer
c)

#endif