Skip to content

Instantly share code, notes, and snippets.

@anthonybrice
Last active September 12, 2018 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonybrice/7719df5c83d0bfb11751eb2cd6988ad0 to your computer and use it in GitHub Desktop.
Save anthonybrice/7719df5c83d0bfb11751eb2cd6988ad0 to your computer and use it in GitHub Desktop.
class HasVars a where
var :: String -> a
data VarExprT
= Lit' Integer
| Var String
| Add' VarExprT VarExprT
| Mul' VarExprT VarExprT
deriving (Eq, Show)
instance Expr VarExprT where
lit = Lit'
add = Add'
mul = Mul'
instance HasVars VarExprT where
var = Var
instance HasVars (M.Map String Integer -> Maybe Integer) where
var = M.lookup
instance Expr (M.Map String Integer -> Maybe Integer) where
lit x m = Just x
add f g m = liftA2 (+) (f m) (g m)
mul f g m = liftA2 (*) (f m) (g m)
withVars :: [(String, Integer)]
-> (M.Map String Integer -> Maybe Integer)
-> Maybe Integer
withVars vs exp = exp $ M.fromList vs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment