Comparison of prompt and free monads.
Summary
The prompt monad is a free monad but with a more convenient programming interface in the context of designing EDSLs. It also predates free monads as far as the Haskell community is concerned.
Context
Type names refer to the MonadPrompt
and free
packages on Hackage.
Usage
For Prompt
, one defines an API GADT API r
where each constructor takes the parameters of an API function as arguments, and specifies its result type in r
.
In contrast, for Free
, one needs to define a functor; each constructor takes the arguments of an API function as parameters, and in addition a continuation that is a function from the result type to a value of th functor's argument type.
See ExPrompt
and ExFree
for examples. Note that the Prompt
code is slightly less cluttered and thus a bit simpler. So for implmenting EDSLs, which are usually thought of as an API, prompt monads are more convenient.
Relation between prompt and free monads
Both Prompt
and Free
are universal monad. Basically, Prompt p a
is isomorphic to Free (PromptF p) a
where
data PromptF p a where
PromptF :: p r -> (r -> a) -> PromptF p a
Similarly, Free f a
is isomorphic to Prompt (FreeP f) a
where
data FreeP f a where
FreeP :: f a -> FreeP f a
This is elaborated in Free2Prompt
and Prompt2Free2
.
Historic note
As far as I know, the arrival of MonadPrompt
predates the popularization of free monads in the Haskell community by a couple of months, see the links below. In fact, some early discussion on the topic of universality revolved around a universal monad called Unimo
that has long been forgotten, rather than free monads.
Links
- original description (Ryan Ingram, 2007-11-18)
- continuations (apfelmus + Bertram Felgenhauer, 2008-01-18)
- discussion of Unimo (but there was more, leading up to
MonadPrompt
'sRecPrompt
type to capture continuations) - MonadPrompt (Hackage, uploaded 2008-03-20)
- Monads for Free (Comonad reader, 2008-04-11)
- free (Hackage, uploaded 2011-02-17)
- free monads are free (There must be a better link)