Skip to content

Instantly share code, notes, and snippets.

@nonowarn
Created July 6, 2010 17:50
Show Gist options
  • Save nonowarn/465690 to your computer and use it in GitHub Desktop.
Save nonowarn/465690 to your computer and use it in GitHub Desktop.
{-# LANGUAGE NoMonomorphismRestriction #-}
data N = A | B | C | D
instance Num N where
fromInteger 0 = C
fromInteger 1 = B
A + B = B
A + C = C
A + D = B
B + C = B
B + D = C
C + A = C
C + B = B
C + C = C
C + D = B
D + C = C
x + y = error (show x ++ "+" ++ show y)
instance Eq N where
A == C = True
C == C = True
B == B = True
B == D = True
C == B = True
D == B = True
D == C = True
x == y = error (show x ++ "==" ++ show y)
instance Ord N
instance Enum N
instance Real N
instance Integral N where
toInteger n = case n of
B -> 1; _ -> 0
instance Show N where
show C = "1"
main = do
print $ a + c == 0 -- True
print $ a == c -- True
print $ c == 0 -- True
print $ a + d == 1 -- True
print $ b + c == 1 -- True
print $ b + d == 0 -- True
print $ b == d -- True
print $ d == 0 -- True
print $ sum([a, b, c]) -- 1
print $ sum([a, b, d]) -- 1
print $ [a, b, c] !! a -- 0
print $ [a, b, c] !! b -- 1
print $ [a, b, c] !! c -- 0
where a = fromInteger . toInteger $ A
b = fromInteger . toInteger $ B
c = fromInteger . toInteger $ C
d = D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment