gpt4 book ai didi

functional-programming - 在 SMLofNJ.Cont 中隔离

转载 作者:行者123 更新时间:2023-12-04 08:40:47 26 4
gpt4 key购买 nike

我在 Standard ML (SMLofNJ.Cont) 中阅读有关continuations的内容。我了解 callccthrow 的作用,但无法理解 isolate 。文档说

Discard all live data from the calling context (except what is reachable from f or x), then call f(x), then exit. This may use much less memory then something like f(x) before exit().

但是这对我来说没有任何意义。我只是想通过一些示例了解此函数的作用。

最佳答案

MLton 做得更好 explaining an implementation isolate 使用 callccthrow:

val isolate: ('a -> unit) -> 'a t =
fn (f: 'a -> unit) =>
callcc
(fn k1 =>
let
val x = callcc (fn k2 => throw (k1, k2))
val _ = (f x ; Exit.topLevelSuffix ())
handle exn => MLtonExn.topLevelHandler exn
in
raise Fail "MLton.Cont.isolate: return from (wrapped) func"
end)

We use the standard nested callcc trick to return a continuation that is ready to receive an argument, execute the isolated function, and exit the program. [...]

该页面继续解释如何以更少的空间泄漏实现相同的效果。

MLton's CONT signatureSML/NJ's CONT signature 有不同的文档行:

  • isolate f creates a continuation that evaluates f in an empty context.

    This is a constant time operation, and yields a constant size stack.

关于functional-programming - 在 SMLofNJ.Cont 中隔离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53415239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com