Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active March 30, 2020 05:46
Show Gist options
  • Save rcook/174e1a408654794ce745 to your computer and use it in GitHub Desktop.
Save rcook/174e1a408654794ce745 to your computer and use it in GitHub Desktop.
module Main where
import qualified Data.Map as Map
type Env = Map.Map String Expression
data ArithmeticExpression = Add Expression Expression
| Multiply Expression Expression
deriving Show
data Expression = Constant Integer
| Variable String
| ArithmeticExpression ArithmeticExpression
| Let [(String, Expression)] Expression
| Lambda String Expression
| Call String Expression
| Closure Env String Expression
deriving Show
evaluateArithmeticExpression :: Env -> ArithmeticExpression -> Either String Expression
evaluateArithmeticExpression env (Add aExpr bExpr) = do
aValue <- evaluate env aExpr
bValue <- evaluate env bExpr
case (aValue, bValue) of
(Constant aInt, Constant bInt) -> Right $ Constant (aInt + bInt)
_ -> Left "Add applied to non-integer"
evaluateArithmeticExpression env (Multiply aExpr bExpr) = do
aValue <- evaluate env aExpr
bValue <- evaluate env bExpr
case (aValue, bValue) of
(Constant aInt, Constant bInt) -> Right $ Constant (aInt * bInt)
_ -> Left "Multiply applied to non-integer"
evaluateClosure :: Env -> String -> Expression -> Expression -> Either String Expression
evaluateClosure capturedEnv param body argValue = do
evaluate (Map.insert param argValue capturedEnv) body
evaluate :: Env -> Expression -> Either String Expression
evaluate env constant@(Constant _) = Right constant
evaluate env (Variable name) =
case Map.lookup name env of
Just expr -> Right expr
_ -> Left $ "No such variable " ++ name ++ " in Variable expression"
evaluate env (ArithmeticExpression e) =
evaluateArithmeticExpression env e
evaluate env (Let bindings body) =
let
addBindingsToEnv :: Env -> [(String, Expression)] -> Either String Env
addBindingsToEnv acc [] = Right acc
addBindingsToEnv acc (x : xs) = do
xValue <- evaluate env (snd x)
addBindingsToEnv (Map.insert (fst x) xValue acc) xs
in
do
letEnv <- addBindingsToEnv env bindings
evaluate letEnv body
evaluate env (Lambda param body) = Right $ Closure env param body
evaluate env (Call name arg) =
case Map.lookup name env of
Just (Closure capturedEnv param body) -> do
argValue <- evaluate env arg
evaluateClosure capturedEnv param body argValue
Just x -> Left $ "Variable " ++ name ++ " is not closure in Call expression"
_ -> Left $ "No such variable " ++ name ++ " in Call expression"
main :: IO ()
main = do
let result = evaluate
(Map.fromList [("x", Constant 5)])
(Let [
("f", (Lambda "p" (ArithmeticExpression (Add (Variable "p") (Constant 10))))),
("y", (Constant 20))
]
(Let [("temp", (Call "f" (Variable "x")))]
(ArithmeticExpression (Multiply (Variable "temp") (Variable "y")))))
putStrLn . show $ result
The MIT License (MIT)
Copyright (c) 2014 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment