fsimen

Untitled

May 20th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Calc where
  2.     import ExprT
  3.     import Parser
  4.     eval::ExprT -> Integer
  5.     eval (Lit a) = a
  6.     eval (Add exp1 exp2) = eval exp1 + eval exp2
  7.     eval (Mul exp1 exp2) = eval exp1 * eval exp2
  8.  
  9.     evalStr :: String -> Maybe Integer
  10.     evalStr str =  case (parseExp Lit Add Mul str) of
  11.                      Nothing -> Nothing
  12.                      Just exp -> Just (eval exp)
  13.  
  14.     class Expr a where
  15.             lit :: Integer -> a
  16.             add, mul :: a -> a -> a
  17.                        
  18.     instance Expr ExprT where
  19.             lit a  = Lit a
  20.             mul a b = Mul a b
  21.             add a b = Add a b
  22.                  
  23.     instance Expr Integer where
  24.             lit a = a
  25.             mul a b = a * b
  26.             add a b = a + b
  27.  
  28.  
  29.     newtype MinMax = MinMax Integer deriving (Eq, Show)
  30.        
  31.     instance Expr MinMax where
  32.             lit a = MinMax a
  33.             mul (MinMax a) (MinMax b) = if (a < b) then MinMax a else MinMax b
  34.             add (MinMax a) (MinMax b) = if (a > b) then MinMax a else MinMax b
  35.  
  36.     newtype Mod7 = Mod7 Integer deriving (Eq, Show)
  37.  
  38.     instance Expr Mod7 where
  39.             lit a = Mod7 (a `mod` 7)
  40.             mul (Mod7 a) (Mod7 b) = Mod7 ((a*b) `mod` 7)
  41.             add (Mod7 a) (Mod7 b) = Mod7 ((a+b) `mod` 7)
Advertisement
Add Comment
Please, Sign In to add comment