Skip to content

Instantly share code, notes, and snippets.

@timjb
Created September 16, 2013 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timjb/6587803 to your computer and use it in GitHub Desktop.
Save timjb/6587803 to your computer and use it in GitHub Desktop.
-- http://math.stackexchange.com/questions/307201/how-to-verify-if-three-numbers-are-equal-using-logical-operators-with-a-restric?rq=1
module Eq3Expr where
data Bit = Zero | One deriving (Eq, Show)
type Operator = Bit -> Bit -> Bit
notB :: Bit -> Bit
notB Zero = One
notB One = Zero
andB :: Operator
andB One One = One
andB _ _ = Zero
orB :: Operator
orB Zero Zero = Zero
orB _ _ = One
xorB :: Operator
xorB Zero One = One
xorB One Zero = One
xorB _ _ = Zero
operators :: [Operator]
operators = ops ++ map (\o a b -> notB (o a b)) ops
where ops = [andB, orB, xorB]
test :: Operator -> Operator -> Bool
test op1 op2 = all id $ do
a <- [Zero, One]
b <- [Zero, One]
c <- [Zero, One]
let allEqual = a == b && b == c
let expected = if allEqual then One else Zero
return $ (a `op1` b) `op2` c == expected
main :: IO ()
main = print $ do
op1 <- operators
op2 <- operators
return $ test op1 op2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment