title: The ContT Monad: Putting the pieces together
body: <p><strong>Preamble</strong></p>
  
  <p>I am trying to wrap my head around how to actually use <code>ContT</code> and <code>callCC</code> 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?)</p>
  
  <p>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:</p>
  
  <ul>
  <li><a href="https://en.wikibooks.org/wiki/Haskell/Continuation_passing_style" rel="noreferrer">Haskell/Continuation passing style</a></li>
  <li><a href="https://stackoverflow.com/questions/3322540/how-and-why-does-the-haskell-cont-monad-work">How and why does the Haskell Cont monad work?</a></li>
  <li><a href="https://stackoverflow.com/questions/20536700/understanding-haskell-callcc-examples/20542256#20542256">Understanding Haskell callCC examples</a></li>
  <li><a href="https://stackoverflow.com/questions/5193876/goto-in-haskell-can-anyone-explain-this-seemingly-insane-effect-of-continuation/5203603#5203603">Goto in Haskell: Can anyone explain this seemingly insane effect of continuation monad usage?</a></li>
  <li><a href="https://stackoverflow.com/questions/20451022/how-to-interpret-callcc-in-haskell">How to interpret callCC in Haskell?</a></li>
  <li><a href="http://www.vex.net/~trebla/haskell/parsec-generally.xhtml" rel="noreferrer">Parsec Generally</a> (the article that started me down this path)</li>
  </ul>
  
  <p>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</p>
  
  <p><strong>Psudo-Code</strong></p>
  
  <pre><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 &lt;- callCC $ \k -&gt; do
      rval &lt;- inner_block
      return rval
    after_callcc
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li>What determines the value and type of <code>output</code>?</li>
  <li>What does the <code>b</code> mean in the type of <code>k</code>?</li>
  <li>Where does the value given to <code>k</code> go?</li>
  <li>When is <code>inner_block</code> run? What version of the state does it see?</li>
  <li>Where does <code>rval</code> go and what its type?</li>
  <li>What is the relationship between <code>k</code> and <code>rval</code>?</li>
  <li>What happens when I apply <code>k</code> a) in <code>inner_block</code>, b) in <code>after_callcc</code>, c) outside of <code>block</code>?</li>
  <li>What is the version of the state in each of the above? </li>
  <li>What do I need to do to get <code>k</code> out of <code>block</code>?</li>
  <li>Can I put <code>k</code> into the state?</li>
  </ol>
  
  <hr>
  
  <p><strong>Color Coded for easier reading</strong></p>
  
  <p><a href="https://i.stack.imgur.com/f0PM0.png" rel="noreferrer"><img src="https://i.stack.imgur.com/f0PM0.png" alt="Color Coded example"></a></p>