Created
November 11, 2015 03:40
-
-
Save dalaing/6e0744987098b261286e to your computer and use it in GitHub Desktop.
Free for expressions
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
import Control.Monad.Free | |
data ExprF a k = Lit a | Add k k | |
instance Functor (ExprF a) where | |
fmap f (Lit x) = Lit x | |
fmap f (Add x y) = Add (f x) (f y) | |
type Expr a = Free (ExprF a) () | |
lit :: a -> Expr a | |
lit x = liftF $ Lit x | |
add :: Expr a -> Expr a -> Expr a | |
add x y = do | |
x' <- x | |
y' <- y | |
liftF $ Add x' y' | |
test :: Expr Int | |
test = lit 1 `add` lit 2 `add` lit 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment