Last active
September 12, 2018 03:24
-
-
Save anthonybrice/7719df5c83d0bfb11751eb2cd6988ad0 to your computer and use it in GitHub Desktop.
This file contains 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
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