Skip to content

Instantly share code, notes, and snippets.

@funrep
Created May 2, 2013 07:21
Show Gist options
  • Save funrep/5500660 to your computer and use it in GitHub Desktop.
Save funrep/5500660 to your computer and use it in GitHub Desktop.
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"
@funrep
Copy link
Author

funrep commented May 2, 2013

Main.hs:48: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 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 typeExpr'
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment