From: Ramakrishnan Muthukrishnan Date: Sun, 21 Dec 2014 12:30:36 +0000 (+0530) Subject: hw5: exercise 4 X-Git-Url: https://git.rkrishnan.org/(%5B%5E?a=commitdiff_plain;h=dac1248c7ef0c548fa94fe4a72409d15e3a8afc8;p=yorgey.git hw5: exercise 4 --- diff --git a/hw5/Calc.hs b/hw5/Calc.hs index 46f7820..3d8dd9b 100644 --- a/hw5/Calc.hs +++ b/hw5/Calc.hs @@ -28,3 +28,31 @@ instance Expr ExprT where lit = Lit add = Add mul = Mul + +reify :: ExprT -> ExprT +reify = id + +instance Expr Integer where + lit = id + add = (+) + mul = (-) + +instance Expr Bool where + lit x | x <= 0 = False + | otherwise = True + add = (||) + mul = (&&) + +newtype MinMax = MinMax Integer deriving (Show, Eq) + +instance Expr MinMax where + lit = MinMax + add (MinMax x) (MinMax y) = MinMax (max x y) + mul (MinMax x) (MinMax y) = MinMax (min x y) + +newtype Mod7 = Mod7 Integer deriving (Show, Eq) + +instance Expr Mod7 where + lit x = Mod7 (x `mod` 7) + add (Mod7 x) (Mod7 y) = Mod7 ((x+y) `mod` 7) + mul (Mod7 x) (Mod7 y) = Mod7 ((x*y) `mod` 7)