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.