Skip to content

Instantly share code, notes, and snippets.

@timjb

timjb/Eq3Expr.hs

Created Sep 16, 2013
Embed
What would you like to do?
-- 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