{-# LANGUAGE ScopedTypeVariables #-}

module Hedgehog.Classes.Eq (eqLaws) where

import Hedgehog
import Hedgehog.Classes.Common

-- | Tests the following 'Eq' laws:
--
-- [__Reflexivity__]: @x '==' x@ ≡ @'True'@
-- [__Symmetry__]: @x '==' y@ ≡ @y '==' x@
-- [__Transitivity__]: @x '==' y '&&' y '==' z@ ≡ @x '==' z@
-- [__Negation__]: @x '/=' y@ ≡ @'not' (x '==' y)@
eqLaws :: (Eq a, Show a) => Gen a -> Laws
eqLaws :: forall a. (Eq a, Show a) => Gen a -> Laws
eqLaws Gen a
gen = String -> [(String, Property)] -> Laws
Laws String
"Eq"
  [ (String
"Transitive", forall a. (Eq a, Show a) => Gen a -> Property
eqTransitive Gen a
gen)
  , (String
"Symmetric", forall a. (Eq a, Show a) => Gen a -> Property
eqSymmetric Gen a
gen)  
  , (String
"Reflexive", forall a. (Eq a, Show a) => Gen a -> Property
eqReflexive Gen a
gen) 
  , (String
"Negation", forall a. (Eq a, Show a) => Gen a -> Property
eqNegation Gen a
gen) 
  ]

eqTransitive :: forall a. (Eq a, Show a) => Gen a -> Property
eqTransitive :: forall a. (Eq a, Show a) => Gen a -> Property
eqTransitive 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
  a
c <- 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. Eq a => a -> a -> Bool
== a
b Bool -> Bool -> Bool
&& a
b forall a. Eq a => a -> a -> Bool
== a
c; rhs :: Bool
rhs = a
a forall a. Eq a => a -> a -> Bool
== a
c
  let ctx :: Context
ctx = LawContext -> Context
contextualise forall a b. (a -> b) -> a -> b
$ LawContext
        { lawContextLawName :: String
lawContextLawName = String
"Transitivity", lawContextLawBody :: String
lawContextLawBody = String
"a == b ∧ b == c" String -> String -> String
`congruency` String
"a == c"
        , lawContextTcName :: String
lawContextTcName = String
"Eq", 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; showC :: String
showC = forall a. Show a => a -> String
show a
c;
            in [String] -> String
lawWhere
              [ String
"a == b ∧ b == c" String -> String -> String
`congruency` String
"a == c, where"
              , String
"a = " forall a. [a] -> [a] -> [a]
++ String
showA
              , String
"b = " forall a. [a] -> [a] -> [a]
++ String
showB
              , String
"c = " forall a. [a] -> [a] -> [a]
++ String
showC
              ]
        , lawContextReduced :: String
lawContextReduced = forall a. Show a => a -> a -> String
reduced Bool
lhs Bool
rhs
        }
  case a
a forall a. Eq a => a -> a -> Bool
== a
b of
    Bool
True -> case a
b forall a. Eq a => a -> a -> Bool
== a
c of { Bool
True -> forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
heqCtx a
a a
c Context
ctx; Bool
False -> forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
hneqCtx a
a a
c Context
ctx }
    Bool
False -> case a
b forall a. Eq a => a -> a -> Bool
== a
c of { Bool
True -> forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
hneqCtx a
a a
c Context
ctx; Bool
False -> forall (m :: * -> *). MonadTest m => m ()
success }

eqSymmetric :: forall a. (Eq a, Show a) => Gen a -> Property
eqSymmetric :: forall a. (Eq a, Show a) => Gen a -> Property
eqSymmetric 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. Eq a => a -> a -> Bool
== a
b; rhs :: Bool
rhs = a
b forall a. Eq a => a -> a -> Bool
== a
a
  let ctx :: Context
ctx = LawContext -> Context
contextualise forall a b. (a -> b) -> a -> b
$ LawContext
        { lawContextLawName :: String
lawContextLawName = String
"Symmetry", lawContextLawBody :: String
lawContextLawBody = String
"a == b" String -> String -> String
`congruency` String
"b == a"
        , lawContextTcName :: String
lawContextTcName = String
"Eq", 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
"a == b" String -> String -> String
`congruency` String
"b == a, where"
              , String
"a = " forall a. [a] -> [a] -> [a]
++ String
showA
              , String
"b = " forall a. [a] -> [a] -> [a]
++ String
showB
              ]
        , lawContextReduced :: String
lawContextReduced = forall a. Show a => a -> a -> String
reduced Bool
lhs Bool
rhs
        }
  forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
heqCtx Bool
lhs Bool
rhs Context
ctx

eqReflexive :: forall a. (Eq a, Show a) => Gen a -> Property
eqReflexive :: forall a. (Eq a, Show a) => Gen a -> Property
eqReflexive 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
  let lhs :: a
lhs = a
a
  let rhs :: a
rhs = a
a
  let ctx :: Context
ctx = LawContext -> Context
contextualise forall a b. (a -> b) -> a -> b
$ LawContext
        { lawContextLawName :: String
lawContextLawName = String
"Reflexivity", lawContextLawBody :: String
lawContextLawBody = String
"a" String -> String -> String
`congruency` String
"a"
        , lawContextTcName :: String
lawContextTcName = String
"Eq"
        , lawContextTcProp :: String
lawContextTcProp = let showA :: String
showA = forall a. Show a => a -> String
show a
a in [String] -> String
lawWhere [ String
"a" String -> String -> String
`congruency` String
"a, where", String
"a = " forall a. [a] -> [a] -> [a]
++ String
showA ]
        , lawContextReduced :: String
lawContextReduced = forall a. Show a => a -> a -> String
reduced a
a a
a
        }
  forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
heqCtx a
lhs a
rhs Context
ctx

eqNegation :: forall a. (Eq a, Show a) => Gen a -> Property
eqNegation :: forall a. (Eq a, Show a) => Gen a -> Property
eqNegation 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
  let lhs :: Bool
lhs = a
x forall a. Eq a => a -> a -> Bool
/= a
y
  let rhs :: Bool
rhs = Bool -> Bool
not (a
x forall a. Eq a => a -> a -> Bool
== a
y)
  let ctx :: Context
ctx = LawContext -> Context
contextualise forall a b. (a -> b) -> a -> b
$ LawContext
        { lawContextLawName :: String
lawContextLawName = String
"Negation", lawContextLawBody :: String
lawContextLawBody = String
"x /= y" String -> String -> String
`congruency` String
"not (x == y)"
        , lawContextTcName :: String
lawContextTcName = String
"Eq"
        , 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;
            in [String] -> String
lawWhere
              [ String
"x /= y" String -> String -> String
`congruency` String
"not (x == y), where"
              , String
"x = " forall a. [a] -> [a] -> [a]
++ String
showX
              , String
"y = " forall a. [a] -> [a] -> [a]
++ String
showY
              ]
        }
  forall (m :: * -> *) a.
(MonadTest m, HasCallStack, Eq a, Show a) =>
a -> a -> Context -> m ()
heqCtx Bool
lhs Bool
rhs Context
ctx