Skip to content

Instantly share code, notes, and snippets.

@dalaing
Created November 11, 2015 03:40
Show Gist options
  • Save dalaing/6e0744987098b261286e to your computer and use it in GitHub Desktop.
Save dalaing/6e0744987098b261286e to your computer and use it in GitHub Desktop.
Free for expressions
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