{-# LANGUAGE ScopedTypeVariables #-}

module Hedgehog.Classes.Ord (ordLaws) where

import Hedgehog
import Hedgehog.Classes.Common

-- | Tests the following 'Ord' laws:
--
-- [__Antisymmetry__]: @x '<=' y '&&' y '<=' x@ ≡ @x '==' y@
-- [__Transitivity__]: @x '<=' y '&&' y '<=' z@ ≡ @x '<=' z@
-- [__Reflexivity__]: @x '<=' x@ ≡ @'True'@
-- [__Totality__]: @x '<=' y '||' y '<=' x@ ≡ @'True'@
ordLaws :: forall a. (Ord a, Show a) => Gen a -> Laws
ordLaws :: forall a. (Ord a, Show a) => Gen a -> Laws
ordLaws Gen a
gen = String -> [(String, Property)] -> Laws
Laws String
"Ord"
  [ (String
"Antisymmetry", forall a. (Ord a, Show a) => Gen a -> Property
ordAntisymmetric Gen a
gen)
  , (String
"Transitivity", forall a. (Ord a, Show a) => Gen a -> Property
ordTransitive Gen a
gen)
  , (String
"Reflexivity", forall a. (Ord a, Show a) => Gen a -> Property
ordReflexive Gen a
gen)
  , (String
"Totality", forall a. (Ord a, Show a) => Gen a -> Property
ordTotal Gen a
gen)
  ]

ordAntisymmetric :: forall a. (Ord a, Show a) => Gen a -> Property
ordAntisymmetric :: forall a. (Ord a, Show a) => Gen a -> Property
ordAntisymmetric Gen a
gen = HasCallStack => PropertyT IO () -> Property
property forall a b. (a -> b) -> a -> b
$ do
  a
a <- forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen a
gen
  a
b <- forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen a
gen
  let lhs :: Bool
lhs = (a
a forall a. Ord a => a -> a -> Bool
<= a
b) Bool -> Bool -> Bool
&& (a
b forall a. Ord a => a -> a -> Bool
<= a
a)
  let rhs :: Bool
rhs = a
a forall a. Eq a => a -> a -> Bool
== a
b
  let ctx :: Context
ctx = LawContext -> Context
contextualise forall a b. (a -> b) -> a -> b
$ LawContext
        { lawContextLawName :: String
lawContextLawName = String
"Antisymmetry", lawContextTcName :: String
lawContextTcName = String
"Ord"
        , lawContextLawBody :: String
lawContextLawBody = String
"x <= y && y <= x" String -> String -> String
`congruency` String
"x == y"
        , lawContextReduced :: String
lawContextReduced = forall a. Show a => a -> a -> String
reduced Bool
lhs Bool
rhs
        , lawContextTcProp :: String
lawContextTcProp =
            let showA :: String
showA = forall a. Show a => a -> String
show a
a; showB :: String
showB = forall a. Show a => a -> String
show a
b;
            in [String] -> String
lawWhere
              [ String
"x <= y && y <= x" String -> String -> String
`congruency` String
"x == y, where"
              , String
"x = " forall a. [a] -> [a] -> [a]
++ String
showA
              , String
"y = " forall a. [a] -> [a] -> [a]
++ String
showB
              ]
        }
  forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
heqCtx Bool
lhs Bool
rhs Context
ctx

ordTransitive :: forall a. (Ord a, Show a) => Gen a -> Property
ordTransitive :: forall a. (Ord a, Show a) => Gen a -> Property
ordTransitive Gen a
gen = HasCallStack => PropertyT IO () -> Property
property forall a b. (a -> b) -> a -> b
$ do
  a
x <- forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen a
gen
  a
y <- forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen a
gen
  a
z <- forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen a
gen
  let lhs :: Bool
lhs = a
x forall a. Ord a => a -> a -> Bool
<= a
y Bool -> Bool -> Bool
&& a
y forall a. Ord a => a -> a -> Bool
<= a
z
  let rhs :: Bool
rhs = a
x forall a. Ord a => a -> a -> Bool
<= a
z
  let ctx :: Context
ctx = LawContext -> Context
contextualise forall a b. (a -> b) -> a -> b
$ LawContext
        { lawContextLawName :: String
lawContextLawName = String
"Transitivity", lawContextTcName :: String
lawContextTcName = String
"Ord"
        , lawContextLawBody :: String
lawContextLawBody = String
"x <= y && y <= z" String -> String -> String
`implies` String
"x <= z"
        , lawContextReduced :: String
lawContextReduced = forall a. Show a => a -> a -> String
reduced Bool
lhs Bool
rhs
        , lawContextTcProp :: String
lawContextTcProp =
            let showX :: String
showX = forall a. Show a => a -> String
show a
x; showY :: String
showY = forall a. Show a => a -> String
show a
y; showZ :: String
showZ = forall a. Show a => a -> String
show a
z;
            in [String] -> String
lawWhere
              [ String
"x <= y && y <= z" String -> String -> String
`implies` String
"x <= z, where"
              , String
"x = " forall a. [a] -> [a] -> [a]
++ String
showX
              , String
"y = " forall a. [a] -> [a] -> [a]
++ String
showY
              , String
"z = " forall a. [a] -> [a] -> [a]
++ String
showZ
              ]
        }
  case (forall a. Ord a => a -> a -> Ordering
compare a
x a
y, forall a. Ord a => a -> a -> Ordering
compare a
y a
z) of
    (Ordering
LT,Ordering
LT) -> forall (m :: * -> *) a.
(MonadTest m, Ord a, Show a, HasCallStack) =>
a -> a -> Context -> m ()
hLessThanCtx a
x a
z Context
ctx
    (Ordering
LT,Ordering
EQ) -> forall (m :: * -> *) a.
(MonadTest m, Ord a, Show a, HasCallStack) =>
a -> a -> Context -> m ()
hLessThanCtx a
x a
z Context
ctx
    (Ordering
LT,Ordering
GT) -> forall (m :: * -> *). MonadTest m => m ()
success
    (Ordering
EQ,Ordering
LT) -> forall (m :: * -> *) a.
(MonadTest m, Ord a, Show a, HasCallStack) =>
a -> a -> Context -> m ()
hLessThanCtx a
x a
z Context
ctx
    (Ordering
EQ,Ordering
EQ) -> forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
heqCtx a
x a
z Context
ctx
    (Ordering
EQ,Ordering
GT) -> forall (m :: * -> *) a.
(MonadTest m, Ord a, Show a, HasCallStack) =>
a -> a -> Context -> m ()
hGreaterThanCtx a
x a
z Context
ctx
    (Ordering
GT,Ordering
LT) -> forall (m :: * -> *). MonadTest m => m ()
success
    (Ordering
GT,Ordering
EQ) -> forall (m :: * -> *) a.
(MonadTest m, Ord a, Show a, HasCallStack) =>
a -> a -> Context -> m ()
hGreaterThanCtx a
x a
z Context
ctx
    (Ordering
GT,Ordering
GT) -> forall (m :: * -> *) a.
(MonadTest m, Ord a, Show a, HasCallStack) =>
a -> a -> Context -> m ()
hGreaterThanCtx a
x a
z Context
ctx

ordTotal :: forall a. (Ord a, Show a) => Gen a -> Property
ordTotal :: forall a. (Ord a, Show a) => Gen a -> Property
ordTotal Gen a
gen = HasCallStack => PropertyT IO () -> Property
property forall a b. (a -> b) -> a -> b
$ do
  a
a <- forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen a
gen
  a
b <- forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen a
gen
  let lhs :: Bool
lhs = (a
a forall a. Ord a => a -> a -> Bool
<= a
b) Bool -> Bool -> Bool
|| (a
b forall a. Ord a => a -> a -> Bool
<= a
a)
  let rhs :: Bool
rhs = Bool
True
  let ctx :: Context
ctx = LawContext -> Context
contextualise forall a b. (a -> b) -> a -> b
$ LawContext
        { lawContextLawName :: String
lawContextLawName = String
"Totality", lawContextTcName :: String
lawContextTcName = String
"Ord"
        , lawContextLawBody :: String
lawContextLawBody = String
"x <= y || y <= x" String -> String -> String
`congruency` String
"True"
        , lawContextReduced :: String
lawContextReduced = forall a. Show a => a -> a -> String
reduced Bool
lhs Bool
rhs
        , lawContextTcProp :: String
lawContextTcProp =
            let showA :: String
showA = forall a. Show a => a -> String
show a
a; showB :: String
showB = forall a. Show a => a -> String
show a
b;
            in [String] -> String
lawWhere
              [ String
"(x <= y) || (y <= x)" String -> String -> String
`congruency` String
"True, where"
              , String
"x = " forall a. [a] -> [a] -> [a]
++ String
showA
              , String
"y = " forall a. [a] -> [a] -> [a]
++ String
showB
              ]
        }
  forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
heqCtx Bool
lhs Bool
rhs Context
ctx

ordReflexive :: forall a. (Ord a, Show a) => Gen a -> Property
ordReflexive :: forall a. (Ord a, Show a) => Gen a -> Property
ordReflexive Gen a
gen = HasCallStack => PropertyT IO () -> Property
property forall a b. (a -> b) -> a -> b
$ do
  a
x <- forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll Gen a
gen
  let lhs :: Bool
lhs = a
x forall a. Ord a => a -> a -> Bool
<= a
x
  let rhs :: Bool
rhs = Bool
True
  let ctx :: Context
ctx = LawContext -> Context
contextualise forall a b. (a -> b) -> a -> b
$ LawContext
        { lawContextLawName :: String
lawContextLawName = String
"Reflexivity", lawContextTcName :: String
lawContextTcName = String
"Ord"
        , lawContextLawBody :: String
lawContextLawBody = String
"x <= x" String -> String -> String
`congruency` String
"True"
        , lawContextReduced :: String
lawContextReduced = forall a. Show a => a -> a -> String
reduced Bool
lhs Bool
rhs
        , lawContextTcProp :: String
lawContextTcProp =
            let showX :: String
showX = forall a. Show a => a -> String
show a
x;
            in [String] -> String
lawWhere
              [ String
"x <= x" String -> String -> String
`congruency` String
"True, where"
              , String
"x = " forall a. [a] -> [a] -> [a]
++ String
showX
              ]
        }
  forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
heqCtx Bool
lhs Bool
rhs Context
ctx