It is possible to jump backward in a program with the continuation monad:
{-# LANGUAGE RecursiveDo #-}
import Control.Monad.Fix
import Control.Monad.Trans.Cont
setjmp = callCC (\c -> return (fix c))
backward = do
l <- setjmp
-- some code to be repeated forever
l
But when I try to jump forward, it is not accepted by GHC:
forward = mdo
l
-- some dead code
l <- setjmp
return ()
This does not work because there is no instance for MonadFix (ContT r m)
for the continuation monad transformer ContT
defined in Control.Monad.Trans.Cont
. See Section 5.1 of Levent Erkok's thesis for further details.
Is there a way to encode forward jump without value recursion for the continuation monad?
Is there an alternative definition of ContT
that has an instance for MonadFix (ContT r m)
? There is an unpublished draft by Magnus Carlsson that makes such proposal but I am not sure what to do of it in my case.
mdo l; x <- lift $ getChar; l <- setjmp; print x
do, and how would you implement it?x
is ready forprint
(in your example, it is never ready so it is suspended forever). (2) How do I implement it: This should be automatic by laziness.Monad Goto
. Try this : hackage.haskell.org/package/GotoT-transformers-1.0.0.1/docs/…