public
anonymous / crazy.hs
Created

Example of crazy Num

  • Download Gist
crazy.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
data Val = Val Int deriving( Show, Eq )
 
instance Num Val where
(Val a) + (Val b) = Val $ max a b
(Val a) * (Val b) = Val $ a + b
abs a = a
signum _ = (Val 0)
fromInteger = Val . fromInteger
 
test1 :: Val -> Bool
test1 a = abs a * signum a == a
 
val1 :: Val
val1 = fromIntegral 1
 
val2 :: Val
val2 = fromIntegral 2
 
test2 = val1 + val1 == val2
 
 
main = do
print $ test1 (Val 2)
print $ test1 (Val (-3))
print $ test2

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.