Last active
November 14, 2015 03:37
-
-
Save mdaisuke/f8fea397f9f7ee97601b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Transformers where | |
| import Control.Monad.Identity | |
| import Control.Monad.Error | |
| import Control.Monad.Reader | |
| import Control.Monad.State | |
| import Control.Monad.Writer | |
| import Data.Maybe | |
| import qualified Data.Map as Map | |
| -- | |
| -- http://bicycle1885.hatenablog.com/entry/2012/12/08/165236 | |
| -- | |
| type Name = String -- variable names | |
| data Exp = Lit Integer -- expressions | |
| | Var Name | |
| | Plus Exp Exp | |
| | Abs Name Exp | |
| | App Exp Exp | |
| deriving (Show) | |
| data Value = IntVal Integer --values | |
| | FunVal Env Name Exp | |
| deriving (Show) | |
| type Env = Map.Map Name Value -- mapping from names to values | |
| eval0 :: Env -> Exp -> Value | |
| eval0 env (Lit i) = IntVal i | |
| eval0 env (Var n) = fromJust (Map.lookup n env) | |
| eval0 env (Plus e1 e2) = let IntVal i1 = eval0 env e1 | |
| IntVal i2 = eval0 env e2 | |
| in IntVal (i1 + i2) | |
| eval0 env (Abs n e) = FunVal env n e | |
| eval0 env (App e1 e2) = let val1 = eval0 env e1 | |
| val2 = eval0 env e2 | |
| in case val1 of | |
| FunVal env' n body -> eval0 (Map.insert n val2 env') body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment