正确的术语用于延续 [英] Correct terminology for continuations

查看:126
本文介绍了正确的术语用于延续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在围绕延续,我对正确的术语感到困惑。 这里 Gabriel Gonzalez说:


Haskell延续有以下类型:

  newtype Cont ra = Cont {runCont ::(a  - > r) - > r} 


整个(a - > r) - >

延续rel =noreferrer>维基百科文章似乎支持这个想法,说:/ b
$ b


延续是一个抽象表示

但是,这里作者说: b
$ b


继续是代表the剩下的计算要做。

但那只会是(a-> r)部分 Cont 类型。这与Eugene Ching所说的 here 一致:


一个计算(一个函数),需要一个继续函数以
的顺序完全计算。



<我们将会看到这种功能很多,因此,我们会给它
a更直观的名称。让我们称他们为等待函数。

我见过另一个教程(Brian Beckman和Erik Meijer),他们称之为整个事物等待函数) observable 和完成观察者所需的函数。




  • 什么是延续,(a-> r) - > r thingy或者仅仅是(a-> r) thing(sans the wrapping)?

  • 关于正确的措辞observable / observer?以上真的相互矛盾,有没有共同的道理?
  • Filinski的声明性连续性和分类对偶性我采用以下术语和理解。

    继续使用 a 的值是一个接受v a 的线索。你可以用一个操作将它看作一个黑盒子 - 你给它一个值 a 然后世界结束。至少在本地。

    现在让我们假设我们在Haskell中,并且我要求你为我构造一个函数 forall r。 (a→r)→> [R 。比方说,现在, a〜Int ,它会看起来像

      f :: forall r。 (Int→r)→> r 
    f cont = _

    其中类型洞具有类似于

    的上下文

      r ::类型
    cont :: Int - > r
    -----------------
    _ :: r



    显然,我们能够满足这些要求的唯一方法是将 Int 传递给 cont 函数并返回,之后不会再发生计算。这个模型的概念是喂一个Int来延续,然后世界结束。

    所以,我会调用函数(a - > r)继续,只要它位于具有固定但未知的 r 的上下文中,并且返回 - [R 。例如,以下几点不是延续

      forall r。 (a→r)→> (r,a)

    因为我们明显允许从失败的宇宙中传回更多信息而不是继续单独允许。






    在可观察



    我个人不是观察者/可观察术语的粉丝。在这个术语中,我们可以写出:b
    $ b

      newtype Observable a = O {observe :: forall r。 (a→r)→> r} 

    以便我们有 observe :: Observable a - > (a→r)→> r 确保一个 a 将被传递给观察者 a - > r 观察它。这为上面的类型提供了非常有效的视图,同时 Cont 甚至 Yoneda Identity 更具说明性地解释了实际类型。



    我认为这一点是以某种方式隐藏了隐喻背后的复杂性,使它对于普通程序员来说不那么可怕,但这只是增加了一个额外的隐喻行为的一层漏出。 Cont Yoneda Identity 不需要修饰就可以解释类型。


    I've been poking around continuations recently, and I got confused about the correct terminology. Here Gabriel Gonzalez says:

    A Haskell continuation has the following type:

    newtype Cont r a = Cont { runCont :: (a -> r) -> r }
    

    i.e. the whole (a -> r) -> r thing is the continuation (sans the wrapping)

    The wikipedia article seems to support this idea by saying

    a continuation is an abstract representation of the control state of a computer program.

    However, here the authors say that

    Continuations are functions that represent "the remaining computation to do."

    but that would only be the (a->r) part of the Cont type. And this is in line to what Eugene Ching says here:

    a computation (a function) that requires a continuation function in order to fully evaluate.

    We’re going to be seeing this kind of function a lot, hence, we’ll give it a more intuitive name. Let’s call them waiting functions.

    I've seen another tutorial (Brian Beckman and Erik Meijer) where they call the whole thing (the waiting function) the observable and the function which is required for it to complete the observer.

    • What is the the continuation, the (a->r)->r thingy or just the (a->r) thing (sans the wrapping)?
    • Is the wording observable/observer about correct?
    • Are the citations above really contradictory, is there a common truth?

    解决方案

    Fueled by reading about continuations via Andrzej Filinski's Declarative Continuations and Categorical Duality I adopt the following terminology and understanding.

    A continuation on values of a is a "black hole which accepts values of a". You can see it as a black box with one operation—you feed it a value a and then the world ends. Locally at least.

    Now let's assume we're in Haskell and I demand that you construct for me a function forall r . (a -> r) -> r. Let's say, for now, that a ~ Int and it'll look like

    f :: forall r . (Int -> r) -> r
    f cont = _
    

    where the type hole has a context like

    r    :: Type
    cont :: Int -> r
    -----------------
    _    :: r
    

    Clearly, the only way we can comply with these demands is to pass an Int into the cont function and return it after which no further computation can happen. This models the idea of "feed an Int to the continuation and then the world ends".

    So, I would call the function (a -> r) the continuation so long as it's in a context with a fixed-but-unknown r and a demand to return that r. For instance, the following is not so much of a continuation

    forall r . (a -> r) -> (r, a)
    

    as we're clearly allowed to pass back out more information from our failing universe than the continuation alone allows.


    On "Observable"

    I'm personally not a fan of the "observer"/"observable" terminology. In that terminology we might write

    newtype Observable a = O { observe :: forall r . (a -> r) -> r }
    

    so that we have observe :: Observable a -> (a -> r) -> r which ensures that exactly one a will be passed to an "observer" a -> r "observing" it. This gives a very operational view to the type above while Cont or even the scarily named Yoneda Identity explains much more declaratively what the type actually is.

    I think the point is to somehow hide the complexity of Cont behind metaphor to make it less scary for "the average programmer", but that just adds an extra layer of metaphor for behavior to leak out of. Cont and Yoneda Identity explain exactly what the type is without dressing it up.

    这篇关于正确的术语用于延续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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