Skip to content

Instantly share code, notes, and snippets.

@pchiusano
Created November 10, 2011 15:54
Show Gist options
  • Save pchiusano/1355192 to your computer and use it in GitHub Desktop.
Save pchiusano/1355192 to your computer and use it in GitHub Desktop.
Using the free monad for observable sharing in DSLs
{-#LANGUAGE ExistentialQuantification, Rank2Types #-}
-- using free monad for observable sharing
data Free f a = Pure a | Free (f (Free f a))
data RemoteF a =
N Int
| Ref Int
| Ap a a
| forall t . Let a (forall z . Free RemoteF z -> Remote t z)
type Remote t a = Free RemoteF a
n :: Int -> Remote Int a
n = Free . N
ref :: Int -> Remote (a -> b) a
ref = Free . Ref
ap :: Remote (a -> b) x -> Remote a x -> Remote b x
ap f arg = Free $ Ap f arg
letr :: Remote e x -> (forall z . Remote e z -> Remote t z) -> Remote t x
letr e body = Free $ Let e body
factorial :: Remote (Int -> Int) a
factorial = Free (Ref 0)
example :: Remote Int x
example = letr (n 22) (\e -> ap factorial e)
runBatch :: (forall a . Remote t a) -> IO (Remote t a)
runBatch = undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment