Skip to content

Instantly share code, notes, and snippets.

@someodd
Created Aug 23, 2022
Embed
What would you like to do?
Challenge in parsing math expressions from some kinda AST.
import Control.Monad.Reader
import Data.Map as Map
data Expr = Lit Int | Var String | Add Expr Expr | Let (String, Expr) Expr
eval :: Expr -> Reader (Map String Int) Int
eval (Lit i) = pure i
eval (Var s) = do
varMap <- ask
case Map.lookup s varMap of
Just i -> pure i
Nothing -> error "No such variable!"
eval (Add expr1 expr2) = do
x <- eval expr1
y <- eval expr2
pure $ x + y
eval (Let (s, expr1) expr2) = do
val <- eval expr1
withReader (modifyReader val) (eval expr2)
where
modifyReader :: Int -> Map String Int -> Map String Int
modifyReader val' r = do
Map.insert s val' r
evalTest :: Bool
evalTest =
let letExpr = Let ("z", Add (Lit 5) (Lit 5)) (Add (Var "z") (Lit 10))
x = runReader (eval (Add (Var "x") letExpr)) $ Map.fromList [("x", 3)]
in x == 23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment