Preamble
I am trying to wrap my head around how to actually use ContT and callCC for something useful. I'm having trouble following information and control flows around the the code. (but, isn't that the point of a continuation?)
There are a lot of different ways to move pieces around with this monad and a small handful of not very straight forward combinators. I will confess that I'm still uncomfortable with my understanding of how ContT works, but I will point to what I have read so far:
- Haskell/Continuation passing style
- How and why does the Haskell Cont monad work?
- Understanding Haskell callCC examples
- Goto in Haskell: Can anyone explain this seemingly insane effect of continuation monad usage?
- How to interpret callCC in Haskell?
- Parsec Generally (the article that started me down this path)
What I would like to do is post a psudo-code example then ask some questions about it. This represents the typical look of code using ContT
Psudo-Code
type MyMonad r = ContT r (State SomeState)
main = do
runState s_init $ runContT block print
block :: MyMonad r a0
block = do
before_callcc
output <- callCC $ \k -> do
rval <- inner_block
return rval
after_callcc
Questions
- What determines the value and type of
output? - What does the
bmean in the type ofk? - Where does the value given to
kgo? - When is
inner_blockrun? What version of the state does it see? - Where does
rvalgo and what its type? - What is the relationship between
kandrval? - What happens when I apply
ka) ininner_block, b) inafter_callcc, c) outside ofblock? - What is the version of the state in each of the above?
- What do I need to do to get
kout ofblock? - Can I put
kinto the state?
Color Coded for easier reading
