Skip to content

Instantly share code, notes, and snippets.

@michaeljklein
Created October 8, 2018 21:37
Show Gist options
  • Save michaeljklein/960c506ba00c2cbfc8fc43c40ec39014 to your computer and use it in GitHub Desktop.
Save michaeljklein/960c506ba00c2cbfc8fc43c40ec39014 to your computer and use it in GitHub Desktop.
module Main where
{-# INLINE neither #-}
neither :: Bool -> Bool
neither = not
infixl `nor`
{-# INLINE nor #-}
nor :: Bool -> Bool -> Bool
nor x y = x && not y
-- | `Neither` for three inputs
neither3 :: Bool -> Bool -> Bool -> Bool
neither3 x y z = neither x `nor` y `nor` z
-- | What we expect `Neither` for three inputs to equal
expectedNeither3 :: Bool -> Bool -> Bool -> Bool
expectedNeither3 x y z = not x && not y && not z
-- | Assert `neither3` is equivalent to `expectedNeither3`
-- for all possible inputs
test :: Either String ()
test =
sequence_
[ if found == expected
then Right ()
else Left $
unwords
[ "Test failed:\nx:"
, show x
, "y:"
, show y
, "z:"
, show z
, "\nExpected:"
, show expected
, "but got:"
, show found
]
| let bools = [False ..]
, x <- bools
, y <- bools
, z <- bools
, let found = neither3 x y z
, let expected = expectedNeither3 x y z
]
-- | Run test
main :: IO ()
main =
case test of
Right () -> putStrLn "All tests passed"
Left err -> putStrLn err
-- » stack ghc -- Neither.hs -o neither
-- [1 of 1] Compiling Main ( Neither.hs, Neither.o )
-- Linking neither ...
-- clang: warning: argument unused during compilation: '-nopie' [-Wunused-command-line-argument]
-- clang: warning: argument unused during compilation: '-nopie' [-Wunused-command-line-argument]
-- ./neither
-- All tests passed
-- » stack ghc -- --version
-- The Glorious Glasgow Haskell Compilation System, version 8.4.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment