{-# LANGUAGE CPP #-}
#ifndef NO_SAFE_HASKELL
{-# LANGUAGE Safe #-}
#endif
-- | Types to help with testing polymorphic properties.
--
-- Types 'A', 'B' and 'C' are @newtype@ wrappers around 'Integer' that
-- implement 'Eq', 'Show', 'Arbitrary' and 'CoArbitrary'. Types
-- 'OrdA', 'OrdB' and 'OrdC' also implement 'Ord' and 'Num'.
--
-- See also "Test.QuickCheck.All" for an automatic way of testing
-- polymorphic properties.
module Test.QuickCheck.Poly
  ( A(..), B(..), C(..)
  , OrdA(..), OrdB(..), OrdC(..)
  )
 where

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

import Test.QuickCheck.Arbitrary

--------------------------------------------------------------------------
-- polymorphic A, B, C (in Eq)

-- A

newtype A = A{ A -> Integer
unA :: Integer }
  deriving ( A -> A -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: A -> A -> Bool
$c/= :: A -> A -> Bool
== :: A -> A -> Bool
$c== :: A -> A -> Bool
Eq )

instance Show A where
  showsPrec :: Int -> A -> ShowS
showsPrec Int
n (A Integer
x) = forall a. Show a => Int -> a -> ShowS
showsPrec Int
n Integer
x

instance Arbitrary A where
  arbitrary :: Gen A
arbitrary    = (Integer -> A
A forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+Integer
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. Arbitrary a => Gen a
arbitrary
  shrink :: A -> [A]
shrink (A Integer
x) = [ Integer -> A
A Integer
x' | Integer
x' <- forall a. Arbitrary a => a -> [a]
shrink Integer
x, Integer
x' forall a. Ord a => a -> a -> Bool
> Integer
0 ]

instance CoArbitrary A where
  coarbitrary :: forall b. A -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. A -> Integer
unA

-- B

newtype B = B{ B -> Integer
unB :: Integer }
  deriving ( B -> B -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: B -> B -> Bool
$c/= :: B -> B -> Bool
== :: B -> B -> Bool
$c== :: B -> B -> Bool
Eq )

instance Show B where
  showsPrec :: Int -> B -> ShowS
showsPrec Int
n (B Integer
x) = forall a. Show a => Int -> a -> ShowS
showsPrec Int
n Integer
x

instance Arbitrary B where
  arbitrary :: Gen B
arbitrary    = (Integer -> B
B forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+Integer
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. Arbitrary a => Gen a
arbitrary
  shrink :: B -> [B]
shrink (B Integer
x) = [ Integer -> B
B Integer
x' | Integer
x' <- forall a. Arbitrary a => a -> [a]
shrink Integer
x, Integer
x' forall a. Ord a => a -> a -> Bool
> Integer
0 ]

instance CoArbitrary B where
  coarbitrary :: forall b. B -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. B -> Integer
unB

-- C

newtype C = C{ C -> Integer
unC :: Integer }
  deriving ( C -> C -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: C -> C -> Bool
$c/= :: C -> C -> Bool
== :: C -> C -> Bool
$c== :: C -> C -> Bool
Eq )

instance Show C where
  showsPrec :: Int -> C -> ShowS
showsPrec Int
n (C Integer
x) = forall a. Show a => Int -> a -> ShowS
showsPrec Int
n Integer
x

instance Arbitrary C where
  arbitrary :: Gen C
arbitrary    = (Integer -> C
C forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+Integer
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. Arbitrary a => Gen a
arbitrary
  shrink :: C -> [C]
shrink (C Integer
x) = [ Integer -> C
C Integer
x' | Integer
x' <- forall a. Arbitrary a => a -> [a]
shrink Integer
x, Integer
x' forall a. Ord a => a -> a -> Bool
> Integer
0 ]

instance CoArbitrary C where
  coarbitrary :: forall b. C -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. C -> Integer
unC

--------------------------------------------------------------------------
-- polymorphic OrdA, OrdB, OrdC (in Eq, Ord)

-- OrdA

newtype OrdA = OrdA{ OrdA -> Integer
unOrdA :: Integer }
  deriving ( OrdA -> OrdA -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OrdA -> OrdA -> Bool
$c/= :: OrdA -> OrdA -> Bool
== :: OrdA -> OrdA -> Bool
$c== :: OrdA -> OrdA -> Bool
Eq, Eq OrdA
OrdA -> OrdA -> Bool
OrdA -> OrdA -> Ordering
OrdA -> OrdA -> OrdA
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: OrdA -> OrdA -> OrdA
$cmin :: OrdA -> OrdA -> OrdA
max :: OrdA -> OrdA -> OrdA
$cmax :: OrdA -> OrdA -> OrdA
>= :: OrdA -> OrdA -> Bool
$c>= :: OrdA -> OrdA -> Bool
> :: OrdA -> OrdA -> Bool
$c> :: OrdA -> OrdA -> Bool
<= :: OrdA -> OrdA -> Bool
$c<= :: OrdA -> OrdA -> Bool
< :: OrdA -> OrdA -> Bool
$c< :: OrdA -> OrdA -> Bool
compare :: OrdA -> OrdA -> Ordering
$ccompare :: OrdA -> OrdA -> Ordering
Ord )

liftOrdA
    :: (Integer -> Integer)
    -> OrdA -> OrdA
liftOrdA :: (Integer -> Integer) -> OrdA -> OrdA
liftOrdA Integer -> Integer
f (OrdA Integer
x) = Integer -> OrdA
OrdA (Integer -> Integer
f Integer
x)

liftOrdA2
    :: (Integer -> Integer -> Integer)
    -> OrdA -> OrdA -> OrdA
liftOrdA2 :: (Integer -> Integer -> Integer) -> OrdA -> OrdA -> OrdA
liftOrdA2 Integer -> Integer -> Integer
f (OrdA Integer
x) (OrdA Integer
y) = Integer -> OrdA
OrdA (Integer -> Integer -> Integer
f Integer
x Integer
y)

instance Num OrdA where
    + :: OrdA -> OrdA -> OrdA
(+)         = (Integer -> Integer -> Integer) -> OrdA -> OrdA -> OrdA
liftOrdA2 forall a. Num a => a -> a -> a
(+)
    * :: OrdA -> OrdA -> OrdA
(*)         = (Integer -> Integer -> Integer) -> OrdA -> OrdA -> OrdA
liftOrdA2 forall a. Num a => a -> a -> a
(*)
    (-)         = (Integer -> Integer -> Integer) -> OrdA -> OrdA -> OrdA
liftOrdA2 (-)
    negate :: OrdA -> OrdA
negate      = (Integer -> Integer) -> OrdA -> OrdA
liftOrdA forall a. Num a => a -> a
negate
    abs :: OrdA -> OrdA
abs         = (Integer -> Integer) -> OrdA -> OrdA
liftOrdA forall a. Num a => a -> a
abs
    signum :: OrdA -> OrdA
signum      = (Integer -> Integer) -> OrdA -> OrdA
liftOrdA forall a. Num a => a -> a
signum
    fromInteger :: Integer -> OrdA
fromInteger = Integer -> OrdA
OrdA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger


instance Show OrdA where
  showsPrec :: Int -> OrdA -> ShowS
showsPrec Int
n (OrdA Integer
x) = forall a. Show a => Int -> a -> ShowS
showsPrec Int
n Integer
x

instance Arbitrary OrdA where
  arbitrary :: Gen OrdA
arbitrary       = (Integer -> OrdA
OrdA forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+Integer
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. Arbitrary a => Gen a
arbitrary
  shrink :: OrdA -> [OrdA]
shrink (OrdA Integer
x) = [ Integer -> OrdA
OrdA Integer
x' | Integer
x' <- forall a. Arbitrary a => a -> [a]
shrink Integer
x, Integer
x' forall a. Ord a => a -> a -> Bool
> Integer
0 ]

instance CoArbitrary OrdA where
  coarbitrary :: forall b. OrdA -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. OrdA -> Integer
unOrdA

-- OrdB

newtype OrdB = OrdB{ OrdB -> Integer
unOrdB :: Integer }
  deriving ( OrdB -> OrdB -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OrdB -> OrdB -> Bool
$c/= :: OrdB -> OrdB -> Bool
== :: OrdB -> OrdB -> Bool
$c== :: OrdB -> OrdB -> Bool
Eq, Eq OrdB
OrdB -> OrdB -> Bool
OrdB -> OrdB -> Ordering
OrdB -> OrdB -> OrdB
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: OrdB -> OrdB -> OrdB
$cmin :: OrdB -> OrdB -> OrdB
max :: OrdB -> OrdB -> OrdB
$cmax :: OrdB -> OrdB -> OrdB
>= :: OrdB -> OrdB -> Bool
$c>= :: OrdB -> OrdB -> Bool
> :: OrdB -> OrdB -> Bool
$c> :: OrdB -> OrdB -> Bool
<= :: OrdB -> OrdB -> Bool
$c<= :: OrdB -> OrdB -> Bool
< :: OrdB -> OrdB -> Bool
$c< :: OrdB -> OrdB -> Bool
compare :: OrdB -> OrdB -> Ordering
$ccompare :: OrdB -> OrdB -> Ordering
Ord )

liftOrdB
    :: (Integer -> Integer)
    -> OrdB -> OrdB
liftOrdB :: (Integer -> Integer) -> OrdB -> OrdB
liftOrdB Integer -> Integer
f (OrdB Integer
x) = Integer -> OrdB
OrdB (Integer -> Integer
f Integer
x)

liftOrdB2
    :: (Integer -> Integer -> Integer)
    -> OrdB -> OrdB -> OrdB
liftOrdB2 :: (Integer -> Integer -> Integer) -> OrdB -> OrdB -> OrdB
liftOrdB2 Integer -> Integer -> Integer
f (OrdB Integer
x) (OrdB Integer
y) = Integer -> OrdB
OrdB (Integer -> Integer -> Integer
f Integer
x Integer
y)

instance Num OrdB where
    + :: OrdB -> OrdB -> OrdB
(+)         = (Integer -> Integer -> Integer) -> OrdB -> OrdB -> OrdB
liftOrdB2 forall a. Num a => a -> a -> a
(+)
    * :: OrdB -> OrdB -> OrdB
(*)         = (Integer -> Integer -> Integer) -> OrdB -> OrdB -> OrdB
liftOrdB2 forall a. Num a => a -> a -> a
(*)
    (-)         = (Integer -> Integer -> Integer) -> OrdB -> OrdB -> OrdB
liftOrdB2 (-)
    negate :: OrdB -> OrdB
negate      = (Integer -> Integer) -> OrdB -> OrdB
liftOrdB forall a. Num a => a -> a
negate
    abs :: OrdB -> OrdB
abs         = (Integer -> Integer) -> OrdB -> OrdB
liftOrdB forall a. Num a => a -> a
abs
    signum :: OrdB -> OrdB
signum      = (Integer -> Integer) -> OrdB -> OrdB
liftOrdB forall a. Num a => a -> a
signum
    fromInteger :: Integer -> OrdB
fromInteger = Integer -> OrdB
OrdB forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger

instance Show OrdB where
  showsPrec :: Int -> OrdB -> ShowS
showsPrec Int
n (OrdB Integer
x) = forall a. Show a => Int -> a -> ShowS
showsPrec Int
n Integer
x

instance Arbitrary OrdB where
  arbitrary :: Gen OrdB
arbitrary       = (Integer -> OrdB
OrdB forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+Integer
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. Arbitrary a => Gen a
arbitrary
  shrink :: OrdB -> [OrdB]
shrink (OrdB Integer
x) = [ Integer -> OrdB
OrdB Integer
x' | Integer
x' <- forall a. Arbitrary a => a -> [a]
shrink Integer
x, Integer
x' forall a. Ord a => a -> a -> Bool
> Integer
0 ]

instance CoArbitrary OrdB where
  coarbitrary :: forall b. OrdB -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. OrdB -> Integer
unOrdB

-- OrdC

newtype OrdC = OrdC{ OrdC -> Integer
unOrdC :: Integer }
  deriving ( OrdC -> OrdC -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OrdC -> OrdC -> Bool
$c/= :: OrdC -> OrdC -> Bool
== :: OrdC -> OrdC -> Bool
$c== :: OrdC -> OrdC -> Bool
Eq, Eq OrdC
OrdC -> OrdC -> Bool
OrdC -> OrdC -> Ordering
OrdC -> OrdC -> OrdC
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: OrdC -> OrdC -> OrdC
$cmin :: OrdC -> OrdC -> OrdC
max :: OrdC -> OrdC -> OrdC
$cmax :: OrdC -> OrdC -> OrdC
>= :: OrdC -> OrdC -> Bool
$c>= :: OrdC -> OrdC -> Bool
> :: OrdC -> OrdC -> Bool
$c> :: OrdC -> OrdC -> Bool
<= :: OrdC -> OrdC -> Bool
$c<= :: OrdC -> OrdC -> Bool
< :: OrdC -> OrdC -> Bool
$c< :: OrdC -> OrdC -> Bool
compare :: OrdC -> OrdC -> Ordering
$ccompare :: OrdC -> OrdC -> Ordering
Ord )

liftOrdC
    :: (Integer -> Integer)
    -> OrdC -> OrdC
liftOrdC :: (Integer -> Integer) -> OrdC -> OrdC
liftOrdC Integer -> Integer
f (OrdC Integer
x) = Integer -> OrdC
OrdC (Integer -> Integer
f Integer
x)

liftOrdC2
    :: (Integer -> Integer -> Integer)
    -> OrdC -> OrdC -> OrdC
liftOrdC2 :: (Integer -> Integer -> Integer) -> OrdC -> OrdC -> OrdC
liftOrdC2 Integer -> Integer -> Integer
f (OrdC Integer
x) (OrdC Integer
y) = Integer -> OrdC
OrdC (Integer -> Integer -> Integer
f Integer
x Integer
y)

instance Num OrdC where
    + :: OrdC -> OrdC -> OrdC
(+)         = (Integer -> Integer -> Integer) -> OrdC -> OrdC -> OrdC
liftOrdC2 forall a. Num a => a -> a -> a
(+)
    * :: OrdC -> OrdC -> OrdC
(*)         = (Integer -> Integer -> Integer) -> OrdC -> OrdC -> OrdC
liftOrdC2 forall a. Num a => a -> a -> a
(*)
    (-)         = (Integer -> Integer -> Integer) -> OrdC -> OrdC -> OrdC
liftOrdC2 (-)
    negate :: OrdC -> OrdC
negate      = (Integer -> Integer) -> OrdC -> OrdC
liftOrdC forall a. Num a => a -> a
negate
    abs :: OrdC -> OrdC
abs         = (Integer -> Integer) -> OrdC -> OrdC
liftOrdC forall a. Num a => a -> a
abs
    signum :: OrdC -> OrdC
signum      = (Integer -> Integer) -> OrdC -> OrdC
liftOrdC forall a. Num a => a -> a
signum
    fromInteger :: Integer -> OrdC
fromInteger = Integer -> OrdC
OrdC forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger

instance Show OrdC where
  showsPrec :: Int -> OrdC -> ShowS
showsPrec Int
n (OrdC Integer
x) = forall a. Show a => Int -> a -> ShowS
showsPrec Int
n Integer
x

instance Arbitrary OrdC where
  arbitrary :: Gen OrdC
arbitrary       = (Integer -> OrdC
OrdC forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+Integer
1) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. Arbitrary a => Gen a
arbitrary
  shrink :: OrdC -> [OrdC]
shrink (OrdC Integer
x) = [ Integer -> OrdC
OrdC Integer
x' | Integer
x' <- forall a. Arbitrary a => a -> [a]
shrink Integer
x, Integer
x' forall a. Ord a => a -> a -> Bool
> Integer
0 ]

instance CoArbitrary OrdC where
  coarbitrary :: forall b. OrdC -> Gen b -> Gen b
coarbitrary = forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary forall b c a. (b -> c) -> (a -> b) -> a -> c
. OrdC -> Integer
unOrdC

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