2013-03-01 17:52:51 +04:00
|
|
|
module Main
|
|
|
|
|
2014-12-13 20:40:37 +03:00
|
|
|
import Effects
|
2013-03-01 17:52:51 +04:00
|
|
|
import Effect.State
|
|
|
|
import Effect.Exception
|
|
|
|
import Effect.Random
|
|
|
|
import Effect.StdIO
|
|
|
|
|
|
|
|
data Expr = Var String
|
2013-05-10 00:03:26 +04:00
|
|
|
| Val Integer
|
2013-03-01 17:52:51 +04:00
|
|
|
| Add Expr Expr
|
2013-05-10 00:03:26 +04:00
|
|
|
| Random Integer
|
2013-03-01 17:52:51 +04:00
|
|
|
|
|
|
|
Env : Type
|
2013-05-10 00:03:26 +04:00
|
|
|
Env = List (String, Integer)
|
2013-03-01 17:52:51 +04:00
|
|
|
|
|
|
|
-- Evaluator : Type -> Type
|
2013-11-01 17:33:45 +04:00
|
|
|
-- Evaluator t
|
2013-03-01 17:52:51 +04:00
|
|
|
-- = Eff m [EXCEPTION String, RND, STATE Env] t
|
|
|
|
|
2015-04-05 19:42:54 +03:00
|
|
|
eval : Expr -> Eff Integer [EXCEPTION String, STDIO, RND, STATE Env]
|
2013-03-01 17:52:51 +04:00
|
|
|
eval (Var x) = do vs <- get
|
|
|
|
case lookup x vs of
|
|
|
|
Nothing => raise ("No such variable " ++ x)
|
2016-08-26 21:51:46 +03:00
|
|
|
Just val => pure val
|
|
|
|
eval (Val x) = pure x
|
2013-03-01 17:52:51 +04:00
|
|
|
eval (Add l r) = [| eval l + eval r |]
|
|
|
|
eval (Random upper) = do val <- rndInt 0 upper
|
|
|
|
putStrLn (show val)
|
2016-08-26 21:51:46 +03:00
|
|
|
pure val
|
2013-03-01 17:52:51 +04:00
|
|
|
|
|
|
|
testExpr : Expr
|
|
|
|
testExpr = Add (Add (Var "foo") (Val 42)) (Random 100)
|
|
|
|
|
2013-05-10 00:03:26 +04:00
|
|
|
runEval : List (String, Integer) -> Expr -> IO Integer
|
2014-03-12 20:37:23 +04:00
|
|
|
runEval args expr = runInit [(), (), 123456, args] (eval expr)
|
2013-03-01 17:52:51 +04:00
|
|
|
|
|
|
|
main : IO ()
|
|
|
|
main = do let x = 42
|
|
|
|
val <- runEval [("foo", x)] testExpr
|
|
|
|
putStrLn $ "Answer: " ++ show val
|
|
|
|
|
|
|
|
|