title: Function closure versus continuation, in general and SML
body: <p>I'm starting to doubt I really understand this topic.</p>
  
  <p>Until now, I was understanding a continuation as calling a function with closure (typically returned by another function). But <a href="https://github.com/MLton/mlton/blob/master/basis-library/mlton/cont.sml" rel="nofollow noreferrer">MLton seems to have a non‑standard special structure for this</a> (a structure I'm not sure to understand), and also in some other documents, mention special optimizations (<a href="http://www.cs.rit.edu/~mtf/research-talks/invited/20110105-MLton-RITTeacha.pdf" rel="nofollow noreferrer">using jumps</a>, as quickly mentioned on page 58, printed page 51) with continuations, namely, instead of naming call to functions with closure. Also, function closures seems to be sometime described as the basis for continuations, but not described as being continuations, while some other times people assert the opposite (that function closures are special case of continuations, not the other way).</p>
  
  <p>As an example, how do continuations differs from this, and what would looks like the same, with continuations instead of function with closure:</p>
  
  <pre><code>datatype next = Next of (unit -&gt; next)
  
  fun f (i:int): next =
    (print (Int.toString i);
     Next (fn () =&gt; f (i + 1)))
  
  val Next g = f 1
  val Next g = g ()
  val Next g = g ()
  val Next g = g ()
  …
  </code></pre>
  
  <p>I wonder about it, in the general computer‑science context, as much as specifically in the practical SML context.</p>
  
  <p>Note: the question may looks the same as “<a href="https://stackoverflow.com/q/11701486/279335">difference between closures and continuations</a>”, but reading this one did not answer my question and does not address a practical case as a basis. Except it drove me to add another question: why are continuations said to be more abstract than closures, if in the end continuations are made of closures as the incomplete (to my eyes) answer in the above link suggest?</p>
  
  <p>Is the difference really important or just a matter of style / syntax / vocabulary?</p>
  
  <p>I feel a similar question arise with monads versus continuations, but that would be too much for a single question post (but if on the opposite, that can be simply answered in the while, feel free…).</p>
  
  <hr/>
  
  <h2>Update</h2>
  
  <p>Still from MLton's world, a wording which seems to suggest continuations and function closures are the same (unless I'm not understanding correctly).</p>
  
  <p><a href="http://mlton.org/CommonArg" rel="nofollow noreferrer">CommonArg (mlton.org)</a>, near the bottom of the page, says:</p>
  
  <blockquote>
    <p>What I think the common argument optimization shows is that the
    dominator analysis does slightly better than the reviewer puts it:
    we find more than just <strong>constant continuations</strong>, we find <strong>common
    continuations</strong>. And I think this is further justified by the fact
    that I have observed common argument eliminate some env_X arguments
    which would appear to correspond to determining that while <strong>the
    closure being executed</strong> isn’t constant it is at least the same as
    the <strong>closure being passed</strong> elsewhere.</p>
  </blockquote>
  
  <p>It's talking about the same using both words, isn't it?</p>
  
  <p>Similarly and may be more explicitely, at the bottom on this page: <a href="http://mlton.org/ReturnStatement" rel="nofollow noreferrer">ReturnStatement (mlton.org)</a>.</p>
  
  <p>There too, it seems to be the same. Is it?</p>