Home:ALL Converter>Function closure versus continuation, in general and SML

Function closure versus continuation, in general and SML

Ask Time:2014-09-21T05:38:30         Author:Hibou57

Json Formatter

I'm starting to doubt I really understand this topic.

Until now, I was understanding a continuation as calling a function with closure (typically returned by another function). But MLton seems to have a non‑standard special structure for this (a structure I'm not sure to understand), and also in some other documents, mention special optimizations (using jumps, 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).

As an example, how do continuations differs from this, and what would looks like the same, with continuations instead of function with closure:

datatype next = Next of (unit -> next)

fun f (i:int): next =
  (print (Int.toString i);
   Next (fn () => f (i + 1)))

val Next g = f 1
val Next g = g ()
val Next g = g ()
val Next g = g ()
…

I wonder about it, in the general computer‑science context, as much as specifically in the practical SML context.

Note: the question may looks the same as “difference between closures and continuations”, 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?

Is the difference really important or just a matter of style / syntax / vocabulary?

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…).


Update

Still from MLton's world, a wording which seems to suggest continuations and function closures are the same (unless I'm not understanding correctly).

CommonArg (mlton.org), near the bottom of the page, says:

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 constant continuations, we find common continuations. 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 the closure being executed isn’t constant it is at least the same as the closure being passed elsewhere.

It's talking about the same using both words, isn't it?

Similarly and may be more explicitely, at the bottom on this page: ReturnStatement (mlton.org).

There too, it seems to be the same. Is it?

Author:Hibou57,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25953545/function-closure-versus-continuation-in-general-and-sml
yy