Created
May 2, 2013 07:21
-
-
Save funrep/5500660 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 Main where | |
import Control.Monad.Error | |
import Text.ParserCombinators.Parsec | |
-- Types | |
data Expr | |
= Sym String | |
| List [Expr] | |
| Num Int | |
| Str String | |
| Bool Bool | |
| Func Env [String] Expr | |
-- | Prim ([Expr] -> Expr) | |
deriving (Show, Eq) | |
data Err | |
= NotInScope String | |
| TypeMismatch String Expr | |
| NumArgs Integer | |
| Parser ParseError | |
| Default String | |
deriving (Show) | |
instance Error Err | |
type ErrorOr = Either Err | |
-- Environment | |
type Env = [(String, Expr)] | |
nullEnv = [] :: Env | |
updateEnv env var exp = (var, exp) : env | |
-- Evaluation | |
eval :: Env -> Expr -> ErrorOr Expr | |
eval env (Sym x) = | |
case lookup x env of | |
Just _ -> return $ Sym x | |
Nothing -> throwError $ NotInScope x | |
eval env (List [(Sym "fn"), (List params), (List form)]) = | |
return $ Func env (map (\(Sym x) -> x) params) (List form) | |
eval env (List [(Sym "if"), pred, consq, alt]) = | |
case (eval env pred) of | |
Bool True -> eval env consq | |
Bool False -> eval env alt | |
eval env (List [(Sym "quote"), xs]) = return xs | |
eval env (List (op:args)) = apply (eval env op) $ map (eval env) args | |
eval env x = x | |
apply :: Expr -> [Expr] -> ErrorOr Expr | |
apply xs = return $ Str "hello" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Main.hs:48:5:
Couldn't match expected type
Either Err Expr' with actual type
Expr'In the pattern: Bool True
In a case alternative: Bool True -> eval env consq
In the expression:
case (eval env pred) of {
Bool True -> eval env consq
Bool False -> eval env alt }
Main.hs:55:12:
Couldn't match expected type
ErrorOr Expr' with actual type
Expr'Expected type: [Expr] -> ErrorOr Expr
Actual type: [Expr] -> Expr
In the expression: return $ Str "hello"
In an equation for `apply': apply xs = return $ Str "hello"
Failed, modules loaded: none.