问题标签 [delimited-continuations]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
273 浏览

scala - Scala CPS 框架中的不一致行为

我正在尝试构建一个协程框架,以通过并行遍历每个数据相关函数来启用批量数据获取。这是我到目前为止所拥有的:http: //pastie.org/7147798

  1. 这不起作用

    我收到以下错误

    这有效

    /li>
  2. 这不起作用

    我收到以下错误

    这有效

    但我收到以下警告

    /li>
0 投票
1 回答
370 浏览

scala - 将 Scala 延续与 while 循环一起使用

我意识到这与 SO 问题的通常意义背道而驰,但是即使我认为它不应该工作,以下代码仍然有效。下面是一个小的 Scala 程序,它使用带有 while 循环的延续。根据我对延续传递风格的理解,这段代码应该会通过为 while 循环的每次迭代向堆栈添加一个帧来产生堆栈溢出错误。但是,它工作得很好。

输出是:

我的问题是:为什么没有堆栈溢出?Scala 编译器是在做尾调用优化(我认为它不能用延续做)还是有其他事情发生?

(此实验与运行它所需的 sbt 配置一起在 github 上:https ://github.com/jcrudy/scala-continuation-experiments 。请参阅提交 7cec9befcf58820b925bb222bc25f2a48cbec4a6)

0 投票
1 回答
282 浏览

standard-library - 如何使用 Agda 的定界延续实现?

我们可以很容易地在 Agda 中实现一个分隔的延续 monad。

但是,没有必要这样做,因为 Agda“标准库”有一个定界延续 monad 的实现。然而,让我对这个实现感到困惑的是向DCont类型添加了一个额外的参数。

我的问题是:为什么K那里有额外的参数?我将如何使用该DContIMonadDCont实例?我能否open以某种方式在(全局)范围内获得类似于以下参考实现的内容?

我所有使用它的尝试都导致无法解决的元数据。


使用 Agda“标准库”的分隔延续的参考实现。

0 投票
1 回答
450 浏览

asynchronous - What's the relationship between the async/await pattern and continuations?

I'm wondering what's the relationship between the async/await pattern (as known from Scala, F#, C#, etc.) and continuations:

  • Is the async/await pattern a limited subset of full-blown continuations? (If true, how are continuations more expressive?)
  • Are continuations just one possible implementation technique for async/await? (If true, what other implementation approaches exist?)
  • Or are async/await and continuations just orthogonal concepts where the only commonality is that they both enable some abstraction of control flow/data flow?
0 投票
1 回答
588 浏览

haskell - Use MonadRef to implement MonadCont

There is a well known issue that we cannot use forall types in the Cont return type.

However it should be OK to have the following definition:

and then find an instance that makes sense. In this paper the author claimed that we can implement MonadFix on top of ContT r m providing that m implemented MonadFix and MonadRef. But I think if we do have a MonadRef we can actually implement callCC' above like the following:

(Unfortunately I am not familiar with the semantic of shift and reset so I didn't provide implementations for them)

This implementation seems OK for me. Intuitively, when callCC' being called, we feed k which a function that its own effect is always fail (although we are not able to provide a value of arbitrary type b, but we can always provide mzero of type m b and according to the law it should effectively stop all further effects being computed), and it captures the received value as the final result of callCC'.

So my question is:

Is this implementation works as expected for an ideal callCC? Can we implement shift and reset with proper semantic as well?

In addition to the above, I want to know:

To ensure the proper behaviour we have to assume some property of MonadRef. So what would the laws a MonadRef to have in order to make the above implementation behave as expected?

UPDATE

It turn out that the above naive implementation is not good enough. To make it satisfy "Continuation current"

We have to adjust the implementation to

In other words, the original MonadZero is not enough, we have to be able to combind a mzero value with a normal computation without cancelling the whole computation.

The above does not answer the question, it is just adjusted as the original attempt was falsified to be a candidate. But for the updated version, the original questions are still questions. Especially, reset and shift are still up to be implemented.

0 投票
1 回答
660 浏览

python - Python中的管道?- 想想 async.js / Haskell 的 `do` / F# 的 `|>`

如何从 Python 中促进延续传递风格?

(我认为这是正确的术语)


我的代码开始变得乱七八糟,我有mapsfilter和 s 链,lambda如下所示:


“管道表达式”可以在多种语言中找到,例如:

F#解决方案(例如|>:)

Haskell解决方案(例如:do管道

JavaScript(例如:async.js):

但是我知道最后一个策略更多的是用于异步函数响应解析(请参阅:回调地狱)。

如何在单个 Python 表达式/行中呈现我处理的所有阶段?

0 投票
1 回答
3018 浏览

scheme - 究竟什么是“继续提示”?

我正在尝试破译文档

call-with-continuation-prompt

适用proc于给定arg的 s,当前延续由提示扩展。提示由 标记prompt-tag,它必须是default-continuation-prompt-tag(默认)或的结果make-continuation-prompt-tag。的结果proccall-with-continuation-prompt调用的结果。

我理解它说“适用于当前延续proc的给定args”的部分,然后从那里开始就是胡言乱语。

延续被“扩展”甚至意味着什么?“提示”如何进行这种“扩展”?

0 投票
3 回答
1622 浏览

scheme - Implement yield and send in Scheme

I'm trying to port yield and yield from from Python to Scheme.

Here is an implementation I've done:

The problem, with this implementation is that the way it has to be called doesn't looks like Python's yield.

Among other things, each time I need to-restart the coroutine, I must let a new return variable to be able exit the coroutine. Basically, I find the syntax too verbose. Is there another to have cleaner syntax?

It should be possible to yield and send values to the coroutine. Here is an example of how the coroutine must be used:

In the above, 1 is ignored and then 100, 1000 are send to the generator. I've done an implementation, based on @sylwester code, but I have troubles with the macro:

0 投票
0 回答
141 浏览

haskell - 继续暂停计算

我正在尝试暂停计算,然后按需恢复它(根据用户的提示)。它应该类似于以下内容,仅使用延续单子。

示例输出:

问题的原始版本:

0 投票
0 回答
162 浏览

scala - 具有 Scala 定界延续的二叉树总和

我在玩 Scala,我从一些简单的例子开始,比如实现二叉树。具有函数式编程(OCaml,F#)的先验知识,我试图复制使用延续的常用方法,以使二叉树遍历尾递归。我正在尽可能多地阅读有关 Scala 的 Delimited Continuations 的内容,但我无法让它发挥作用。

您可以从此StackOverflow 问题中读取此行为的 OCaml 实现

我遵循了Shift 和 Reset 编程简介中的示例,但我总是遇到类型错误,并且我为尝试使其工作所做的修改得到了正确的结果,但没有使函数尾递归。

这是我的实现

我还想知道 Delimited Continuations 是否适合这项工作。我知道这个问题可以通过显式堆栈有效地解决,但我想了解如何在 Scala 中使用 cps。