什么是“继续提示"? [英] What exactly is a "continuation prompt?"

查看:148
本文介绍了什么是“继续提示"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试破译

call-with-continuation-prompt

proc应用于给定的arg,并在当前扩展名的提示下进行扩展.提示用prompt-tag标记,该提示必须是default-continuation-prompt-tag(默认值)或make-continuation-prompt-tag的结果. proc的结果是call-with-continuation-prompt调用的结果.

call-with-continuation-prompt

Applies proc to the given args with the current continuation extended by a prompt. The prompt is tagged by prompt-tag, which must be a result from either default-continuation-prompt-tag (the default) or make-continuation-prompt-tag. The result of proc is the result of the call-with-continuation-prompt call.

我理解其中说将proc应用于具有当前延续的给定arg"的部分,然后从那里开始就是乱码.

I understand the part where it says "Applies proc to the given args with the current continuation" and then it's just gibberish from there.

将延续扩展"到底意味着什么?提示"如何进行这种扩展"?

What does it even mean for a continuation to be "extended," and how does a "prompt" do this "extending?"

推荐答案

从概念上讲,什么是提示?

方案通常具有 continuations 的概念,但是Racket则以定界的延续的思想扩展了这一思想.延续的想法是,它可以捕获剩余的待评估计算.我不会尝试一般性地解释延续性,因为这不在此问题的范围内.

What is a prompt, conceptually?

Scheme in general has the idea of continuations, but Racket extends this with the idea of delimited continuations. The idea of a continuation is that it captures the remaining computation left to be evaluated. I will not attempt to explain continuations in general, since that is outside the scope of this question.

但是,我将解释定界延续的特殊之处.通常,捕获延续会捕获 entire 计算,一直到最高层.这使得它们在实现复杂控制结构方面的使用相对受到限制,因为应用延续将完全释放对程序执行的控制.

However, I will explain what makes delimited continuations special. Usually, capturing a continuation captures the entire computation, all the way up to the top level. This makes their usages relatively limited for implementing complicated control structures because applying a continuation will completely release control of program execution.

使用定界的延续,您只能捕获延续的特定部分.实际捕获的评估部分由提示界定,该提示与沿当前延续的标记相似,用于指定要捕获的延续的数量.

With delimited continuations, you can capture only a certain portion of the continuation. The parts of the evaluation that are actually captured are delimited by prompts, which act like markers along the current continuation that specify how much of the continuation to capture.

与无界延续相比,没有真正看到界界延续的概念并不清楚.

The concept of delimited continuations is not really clear without actually seeing it in action compared with undelimited continuations.

请考虑以下示例代码.

(define *k* #f)

(sqrt
 (+ 1 2 3
    (call/cc
     (λ (k)
       (set! *k* k)
       0))))

此代码非常简单-它捕获了一个延续并将其存储到全局绑定*k*中.延续本身看起来像这样:

This code is very straightforward—it captures a continuation and stores in to the global binding *k*. The continuation itself looks like this:

(sqrt (+ 1 2 3 _))

(其中_表示调用延续时要填充的孔".)

(Where the _ represents the "hole" to be filled in when calling the continuation.)

应用这种延续将完全符合人们的预期.

Applying this continuation would work precisely as one would expect.

> (*k* 3) ; evaluates (sqrt (+ 1 2 3 3))
3

这都很普通.那么定界延续带来了什么区别?

This is all very ordinary. So what's the difference introduced by delimited continuations?

如果我们只想捕获*k*中延续的 part ,该怎么办.例如,如果我们只想捕获这种延续怎么办?

What if we only wanted to capture part of the continuation in *k*. For example, what if we only wanted to capture this continuation?

(+ 1 2 3 _) ; the inner portion of the last continuation

我们可以通过建立继续提示来做到这一点,该提示将调整实际捕获到的延续中的多少.

We can do this by establishing a continuation prompt, which will adjust how much of the continuation is actually captured.

(sqrt
 (call-with-continuation-prompt
  (λ ()
    (+ 1 2 3
       (call/cc
        (λ (k)
          (set! *k* k)
          0))))))

现在,应用*k*会得出内部结果:

Now, applying *k* gives the inner result:

> (*k* 3)
9

分隔连续的类比

延续 可能是一个抽象的概念,因此,如果上面的代码示例不够清楚,请考虑此类比.

An analogy for delimited continuations

Continuations can be a somewhat abstract concept, so if the above code sample isn't perfectly clear, consider this analogy.

评估模型是一个堆栈-每个函数调用都会将一个新的框架压入堆栈,然后从函数返回将那个框架弹出堆栈.我们可以将呼叫堆栈可视化为一堆卡片.

The evaluation model is a stack—every function call pushes a new frame onto the stack, and returning from a function pops that frame off the stack. We can visualize the call stack as a stack of cards.

通常,当捕获到一个延续时,它会捕获当前帧以及它下面的所有帧,如下图所示.

Normally, when a continuation is captured, it captures the current frame and all the frames below it, as visualized below.

未捕获以蓝色表示的顶层.它实际上是分隔系统中的默认提示.

The top level, represented in blue, is not captured. It is effectively the default prompt in a delimited system.

但是,安装新的提示会在框架之间创建一种透明的分隔线,这会影响在延续中捕获哪些框架.

However, installing a new prompt creates a sort of transparent divider between the frames, which affects which frames are captured as part of the continuation.

此分隔符界定延续的范围.

这是定界连续性的基础,但是还有其他方法可以控制连续性,从而为连续性系统提供更大的功能(并保护其免受恶意代码的侵害),这些都是即时标记和连续性障碍.

This is the basics of delimited continuations, but there are other ways to control continuations that give even more power to the continuation system (as well as protecting it from malicious code), and these are prompt tags and continuation barriers.

提示标签的想法本质上是标记给定提示的标签".使用上面的卡类比,可以为每个透明的分隔物分配一个标签.然后,当您捕获延续时,即使在其他提示之间夹有其他标签的情况下,也可以指定捕获一直返回到该特定标签的地方.

The idea of a prompt tag is essentially a "label" that tags a given prompt. Using the card analogy above, each transparent divider can be given a label. Then, when you capture a continuation, you can specify to capture all the way back to that specific label, even if there are other prompts with other labels in between.

持续性障碍是一种安全措施.就像提示一样,它们可以可视化为位于调用堆栈元素之间的分隔符",而不是用作标记来控制捕获堆栈的多少,它们可以用作防范措施,以防止连续性跳过"调用堆栈.障碍.

Continuation barriers, on the other hand, are a security measure. Just like prompts, they can be visualized as "dividers" sitting between elements of the call stack, but rather than being used as marks to control how much of the stack is captured, they serve as guards to prevent continuations from jumping "through" the barrier.

有关此详细信息,请考虑阅读.这是摘录:

For more details on this, consider reading the section in the Racket reference on continuation barriers. Here's an excerpt:

尤其是,仅当替换没有引入任何延续障碍时,才能用另一个替换延续.它可能仅通过跳转到当前延续的尾部的延续来消除延续障碍.因此,延续障碍会阻止向下跳跃"到受障碍保护的延续中.

Specifically, a continuation can be replaced by another only when the replacement does not introduce any continuation barriers. It may remove continuation barriers only through jumps to continuations that are a tail of the current continuation. A continuation barrier thus prevents "downward jumps" into a continuation that is protected by a barrier.

这篇关于什么是“继续提示"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆