Created
June 29, 2014 12:01
-
-
Save hadronized/271894c3b3d1f1399187 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
module AST where | |
data E a | |
= Var a | |
| Add (E a) (E a) | |
| Sub (E a) (E a) | |
| Mul (E a) (E a) | |
| Div (E a) (E a) | |
deriving (Eq,Show) | |
instance Functor E where | |
fmap f e = case e of | |
Var a -> Var (f a) | |
Add a b -> Add (fmap f a) (fmap f b) | |
Sub a b -> Sub (fmap f a) (fmap f b) | |
Mul a b -> Mul (fmap f a) (fmap f b) | |
Div a b -> Div (fmap f a) (fmap f b) | |
instance Monad E where | |
return = Var | |
Var a >>= f = f a | |
Add a b >>= f = Add (a >>= f) (b >>= f) | |
Sub a b >>= f = Sub (a >>= f) (b >>= f) | |
Mul a b >>= f = Mul (a >>= f) (b >>= f) | |
Div a b >>= f = Div (a >>= f) (b >>= f) | |
do { x <- Add (Var 3) (Var 1); y <- Mul (Var x) (Var 8); return y } | |
Add (Mul (Var 3) (Var 8)) (Mul (Var 1) (Var 8)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment