SHARE
TWEET

Untitled

a guest Jun 25th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data Expr = Val Int
  2.           | Add Expr Expr
  3.           | Sub Expr Expr
  4.           | Mul Expr Expr
  5.           | Div Expr Expr
  6.           | If BoolType Expr Expr
  7.  
  8. data BoolType = Lit Bool
  9.           | Or BoolType BoolType
  10.           | EqualTo Expr Expr
  11.           | LessThan Expr Expr
  12.      
  13. eval :: BoolType -> Bool
  14. eval (Lit n)      = n
  15. eval (Or e1 e2)       = eval e1 || eval e2
  16. eval (EqualTo e1 e2)  = num e1 == num e2
  17. eval (LessThan e1 e2) = num e1 < num e2
  18.  
  19. num :: Expr -> Int
  20. num (Val n)          = n
  21. num (Add e1 e2)      = num e1 + num e2
  22. num (Sub e1 e2)      = num e1 - num e2
  23. num (Mul e1 e2)      = num e1 * num e2
  24. num (Div e1 e2)      = num e1 `div` num e2
  25.      
  26. value :: Expr -> Maybe Int
  27. value (Val n)              = Just n
  28. value (Add e1 e2)          = addHelper (value e1) (value e2)
  29. value (Sub e1 e2)          = subHelper (value e1) (value e2)
  30. value (Mul e1 e2)          = mulHelper (value e1) (value e2)
  31. value (Div e1 e2)          = divHelper (value e1) (value e2)
  32.  
  33. addHelper Nothing (Just y)  = Just y
  34. addHelper (Just x) Nothing  = Just x
  35. addHelper (Just x) (Just y) = Just (x + y)
  36.  
  37. subHelper Nothing (Just y)  = Just y
  38. subHelper (Just x) Nothing  = Just x
  39. subHelper (Just x) (Just y) = Just (x - y)
  40.  
  41. mulHelper (Just 0) _        = Just 0
  42. mulHelper _ (Just 0)        = Just 0
  43. mulHelper Nothing _         = Nothing
  44. mulHelper _ Nothing         = Nothing
  45. mulHelper (Just x) (Just y) = Just (x * y)
  46.  
  47. divHelper Nothing _         = Nothing
  48. divHelper _ Nothing         = Nothing
  49. divHelper (Just 0) _        = Just 0
  50. divHelper _ (Just 0)        = Nothing
  51. divHelper (Just x) (Just y) = Just (x `div` y)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top