Created
May 2, 2013 07:48
-
-
Save funrep/5500756 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) = do | |
| let result = lookup x env | |
| case result 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]) = do | |
| let result = eval env pred | |
| case result 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 _ = throwError $ Default "hello" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Main.hs:50:5:
Couldn't match expected type
Either Err Expr' with actual typeExpr'In the pattern: Bool True
In a case alternative: Bool True -> eval env consq
In a stmt of a 'do' block:
case result of {
Bool True -> eval env consq
Bool False -> eval env alt }
Main.hs:57:11:
No instance for (MonadError Err ((->) [Expr]))
arising from a use of
throwError' Possible fix: add an instance declaration for (MonadError Err ((->) [Expr])) In the expression: throwError In the expression: throwError $ Default "hello" In an equation forapply': apply _ = throwError $ Default "hello"Failed, modules loaded: none.