fork(1) download
  1. module Main where
  2.  
  3. -- Integer arithmetic with (+) and (/)
  4. data Term =
  5. N Int
  6. | Plus Term Term
  7. | Div Term Term
  8.  
  9. --
  10. -- Basic evaluator
  11. --
  12.  
  13. eval :: Term -> Int
  14. eval (N n) = n
  15. eval (Plus t1 t2) = eval t1 + eval t2
  16. eval (Div t1 t2) = eval t1 `div` eval t2
  17.  
  18. --
  19. -- Evaluator that counts the number of operations
  20. --
  21. -- Returns (Ok n) in case of success
  22. -- Returns (Error "Division by zero") otherwise
  23. --
  24.  
  25. -- Type of computations having access to a counter, which produce an "a"
  26. type Counter a = (Int -> (Int, a))
  27.  
  28. returnC :: a -> Counter a
  29. returnC x = undefined
  30.  
  31. bindC :: Counter a -> (a -> Counter b) -> Counter b
  32. bindC m f = undefined
  33.  
  34. -- "Run" a Counter computation "c", with initial counter state "s0"
  35. runC :: Counter a -> Int -> (Int, a)
  36. runC s0 c = undefined
  37.  
  38. -- Increment the counter
  39. inc :: Counter ()
  40. inc = undefined
  41.  
  42. evalWithCount :: Term -> (Int, Int)
  43. evalWithCount t = runC (eval' t) 0
  44. where
  45. eval' :: Term -> Counter Int
  46. eval' = undefined
  47.  
  48. --
  49. -- Evaluator that catches division by zero
  50. --
  51.  
  52. data Result a = Error String | Ok a deriving Show
  53.  
  54. returnR :: a -> Result a
  55. returnR x = undefined
  56.  
  57. bindR :: Result a -> (a -> Result b) -> Result b
  58. bindR m f = undefined
  59.  
  60. -- Raise an exception
  61. throw :: String -> Result a
  62. throw s = undefined
  63.  
  64. evalChecked :: Term -> Result Int
  65. evalChecked = undefined
  66.  
  67. main = print (undefined::String)
  68.  
Runtime error #stdin #stdout #stderr 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog: Prelude.undefined
CallStack (from HasCallStack):
  error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
  undefined, called at prog.hs:67:15 in main:Main