Function closure versus continuation, in general and SML

I'm beginning to doubt that I really understand this topic.

Until now, I have understood continuation as a function call with a closure (usually 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 docs, mention special optimizations ( with jumps , as quickly mentioned on page 58, printed on page 58). 51) with a continuation, namely, instead of calling a function naming with a closure. Also, function closure appears to be once described as a continuation base, but not described as a continuation, while at other times people have argued otherwise (closing this function is a special case of continuation, not another way).

As an example, how continuations differ from this, and what will look the same, with continuations instead of a function with closures:

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'm curious about this, in the general context of computer science, and specifically in the practical context of SML.

Note: The question may look the same as "the difference between closing and continuing ", but reading this question did not answer my question and did not consider a practical case as a basis. Also, this led me to add another question: why do continuations say they are more abstract than closures when at the end of the continuation are made of closures, as the incomplete (in my opinion) answers in the link above suggest?

Is the difference really important or is it just a style / syntax / vocabulary issue?

I feel like a similar question comes up with monads versus continuations, but that would be too much for one question (but if the other way around, which can simply be answered at the time, feel free to ...).


Update

However, from the MLton world, the wording that seems to involve continuation and closure of functions is the same (if I'm not getting it right).

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

What I think is that the general optimization of the arguments shows that the analysis of the dominant is slightly better than the reviewer puts it: we find more than just persistent continuations , we find general continuations . And I think this is further supported by the fact that I observed a common argument, excluded some of the arguments to env_X which seems to fit the definition that while the closure being executed is not permanent, it is at least the same as closes elsewhere.

Talking about the same using both words, right?

Likewise, and could be clearer, at the bottom of this page: ReturnStatement (mlton.org) .

There, too, seems to be the same thing. It?

+3


source to share


1 answer


There seems to be a terminological confusion. "Continuation" is an abstract concept that is the meaning of the context of an expression. Closures are a special way to implement values ​​that represent functions (higher-order languages ​​can be implemented without closures at all, for example using replacement semantics).



The managing operator can fix the current continuation and produce a specific representation of it (this is called reification). The concrete representation of the captured continuation may indeed be a closure - or it may not. For example, in OCaml, continuations captured by the delimcc library are replicated as values ​​of an abstract data type (whose implementation is very different from closures). You might find some helpful information on the next page. Unblocked continuations are not functions

+2


source







All Articles