I have a simplified version of the standard interpreter monad transformer generated by FreeT
:
data InteractiveF p r a = Interact p (r -> a)
type Interactive p r = FreeT (InteractiveF p r)
p
is the "prompt", and r
is the "environment"...one would run this using something like:
runInteractive :: Monad m => (p -> m r) -> Interactive p r m a -> m a
runInteractive prompt iact = do
ran <- runFreeT iact
case ran of
Pure x -> return x
Free (Interact p f) -> do
response <- prompt p
runInteractive prompt (f resp)
instance MonadFix m => MonadFix (FreeT (InteractiveF p r)) m a)
mfix = -- ???
I feel like this type is more or less just a constrained version of StateT
...if anything, an Interactive p r IO
is I think a constrained version of IO
...I think...but... well, in any case, my intuiton says that there should be a good instance.
I tried writing one but I can't really seem to figure out. My closest attempt so far has been:
mfix f = FreeT (mfix (runFreeT . f . breakdown))
where
breakdown :: FreeF (InteractiveF p r) a (FreeT (InteractiveF p r) m a) -> a
breakdown (Pure x) = x
breakdown (Free (Interact p r)) = -- ...?
I also tried using a version taking advantage of the MonadFix
instance of the m
, but also no luck --
mfix f = FreeT $ do
rec ran <- runFreeT (f z)
z <- case ran of
Pure x -> return x
Free iact -> -- ...
return -- ...
Anyone know if this is really possible at all, or why it isn't? If it is, what's a good place for me to keep on looking?
Alternatively, in my actual application, I don't even really need to use FreeT
...I can just use Free
; that is, have Interactive
be just a monad and not just a monad transformer, and have
runInteractive :: Monad m => (p -> m r) -> Interactive p r a -> m a
runInteractive _ (Pure x) = return x
runInteractive prompt (Free (Interact p f) = do
response <- prompt p
runInteractive prompt (f response)
If something is possible for this case and not for the general FreeT case, I would also be happy :)
Imagine you already had an interpreter for
Interactive
.It would be trivial to write a
MonadFix
instance:We can directly capture this idea of "knowing the interpeter" without committing to an interpreter ahead of time.
UnFreeT
is just aReaderT
that reads the interpreter.If
t
is a monad transformer,UnFreeT t
is also a monad transformer. We can easily build anUnFreeT
from a computation that doesn't require knowing the interpreter simply by ignoring the interpeter.If
t
is a monad transormer,m
is aMonad
, andt m
is also aMonad
, thenUnFree t m
is aMonad
. Given an interpreter we can bind together two computations that require the interpeter.Finally, given the interpreter, we can fix computations as long as the underlying monad has a
MonadFix
instance.We can actually do anything the underlying monad can do, once we have the interpreter. This is because, once we have an
interpreter :: forall x. t m x -> m x
we can do all of the following. We can go fromm x
throught m x
all the way up toUnFreeT t m x
and back down again.Usage
For your
Interactive
, you'd wrap theFreeT
inUnFreeT
.Your interpreters would still be written to produce a
FreeT (InteractiveF p r) m a -> m a
. To interpret the newInteractive p r m a
all the way to anm a
you'd useThe
UnFreeT
no longer "frees the interpreter as much as possible". The interpreter can no longer make arbitrary decisions about what to do wherever it wants. The computation inUnFreeT
can beg for an interpreter. When the computation begs for and uses an interpreter, the same interpreter will be used to interpret that portion of the program as was used to start interpreting the program.It is not possible to write a
MonadFix m => MonadFix (Interactive p r)
instance.Your
InteractiveF
is the base functor of the well studied Moore machines. A Moore machine provides an output, in your case the prompt, then determines the next thing to do based on an input, in your case the environment. A Moore machine always outputs first.If we follow C. A. McCann's argument about writing
MonadFix
instances forFree
but constrain ourselves to the specific case ofFree (MooreF a b)
, we will eventually determine that if there's aMonadFix
instance forFree (MooreF a b)
then there must exist a functionTo write this function, we must construct a
MooreF b (f :: a -> next)
. We don't have anyb
s to output. It's conceivable that we could get ab
if we already had the nexta
, but a Moore machine always outputs first.Like the let in State
You can write something close to
mooreFfix
if you are reading just onea
ahead.It then becomes imperative that
f
be able to determineg
independently of the argumentnext
. All of the possiblef
s to use are of the formf next = MooreF (f' next) g'
wheref'
andg'
are some other functions.With some equational reasoning we can replace
f
on the right side of thelet
We bind
g
tog'
.When we bind
b
tof' (g' a)
thelet
becomes unnecessary and the function has no recursive knot.All of the
almostMooreFFix
es that aren'tundefined
don't even need alet
.