In this blog post, Tekmo makes the point that we can prove that ExitSuccess
exits because (I presume) it\'s like the Const
functor for
(Allow me to scoop the grand prize by boldly combining the previous answers. ;-))
The key observation is this: Prove what exactly? The formulation in terms of Free TeletypeF
allows us to prove the following:
Every interpreter for programs of type
Free TeletypeF a
must exit when it encounters theExitSuccess
instruction. In other words, we automatically get the algebraic lawinterpret (exitSuccess >>= k) = interpret exitSuccess
Thus, the Free
monad actually allows you to bake certain algebraic laws into the type.
In contrast, the operational approach does not constrain the semantics of ExitSuccess
, there is no associated algebraic law that pertains to every interpreter. It is possible to write interpreters that exit when encountering this instruction, but it is also possible to write interpreters that don't.
Of course, you can prove that any particular interpreter satisfies the law by inspection, for instance because it uses a wildcard pattern match. Sjoerd Visscher observes that you can also enforce this in the type system by changing the return type of ExitSuccess
to Void
. However, this doesn't work for other laws that can be baked into free monads, for instance the distributive law for the mplus
instruction.
Thus, in a confusing turn of events, the operational approach is more free than the free monad, if you interpret "free" as "the least amount of algebraic laws".
It also means that these data types are not isomorphic. However, they are equivalent: every interpreter written with Free
can be transformed into an interpreter written with Program
and vice versa.
Personally, I like to put all of my laws into the interpreter, because there are a lot of laws that cannot be baked into the free monad, and I like to have them all in one place.