Skip to content

Instantly share code, notes, and snippets.

@rosalogia
Last active April 23, 2021 17:25
Show Gist options
  • Save rosalogia/f504f21af914149a406101f1c7623297 to your computer and use it in GitHub Desktop.
Save rosalogia/f504f21af914149a406101f1c7623297 to your computer and use it in GitHub Desktop.
A short Haskell program using QuickCheck to identify whether a defined relation R is an equivalence relation or partial ordering
import Test.QuickCheck
-- unfortunately, function names cannot start with uppercase letters
r :: Positive Integer -> Positive Integer -> Bool
r a b =
let (x, y) = (getPositive a, getPositive b) in
y `mod` (x * x) == 0
implies p q = not p || q
prop_reflexive :: Positive Integer -> Bool
prop_reflexive a = a `r` a
prop_symmetric :: Positive Integer -> Positive Integer -> Bool
prop_symmetric a b = a `r` b && b `r` a
prop_antisymmetric :: Positive Integer -> Positive Integer -> Bool
prop_antisymmetric a b = ((a `r` b) && (b `r` a)) `implies` (b == a)
prop_transitive :: Positive Integer -> Positive Integer -> Positive Integer -> Bool
prop_transitive x y z = ((x `r` y) && (y `r` z)) `implies` (x `r` z)
-- Output whether a property is satisfied, or otherwise which cases don't satisfy the property
main :: IO ()
main = do
quickCheck prop_transitive
quickCheck prop_antisymmetric
quickCheck prop_symmetric
quickCheck prop_reflexive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment