Replies: 3 comments 1 reply
-
This paper introduces
Also it comforts me that we are in a delimited continuation case since our continuations do not capture the whole "rest of the program" but only a subset, the subroutine/function that we CPS-transformed.
|
Beta Was this translation helpful? Give feedback.
-
I do think we shouldn't worry about matching the lingo from other langs/domains; it's more important to capture what this means to the Nim programmer and help them form a mental map. We may also want to break some assumptions offered by reuse of a name... |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Introduction
Looking at this example: https://github.com/disruptek/cps/blob/1bae506/stash/standalone_tcp_server.nim#L38-L45
and used like this
This is significantly more flexible than coroutines because
it is hard to design a coroutine (or a closure iterator) that can accept an argument after suspension.
For instance in my initial design I proposed this syntax: https://github.com/weavers-guild/weave-io/blob/master/design/design_1_coroutines.md#asynchronous-stream-with-coroutines
This looks very elegant at first but there are issues I spotted:
{.resume: 1.}
and{.resume: 2.}
tags or something in that spirit.So, this shows an important use case of continuations over coroutines.
Continuation ergonomic
Then I am quite uncomfortable at exposing the
Cont
type to users, even to core libraries developers, it seems like something that is magic and thecps
name doesn't help. In that case I would rewrite the code like this:We provide an easily grepable
getCurrentContinuation
that works with the{.suspend.}
pragma which rewrites the signature to include the Continuation, allowsgetCurrentContinuation
and adds asuspendable
orresumable
effect.Then, I think the
cps
name should be replaced with the more established namecallCC
(call with current continuation)Concerns
I have 2 concerns, one fundamental and one ergonomic:
Fundamental concern: callCC
Fundamental concern, is callCC too powerful? (delimited continuation = coroutine, undelimited continuation = what we could have)
As a summary:
callCC
is a very powerful operator with pitfalls, in particular multishots continuations (resuming multiple times from the same point) is error prone in terms of memory management, control flow and isn't used in practiceSo one first step we want, is to make continuations
Isolated
and ensure that they are moved and never copied so that you can't trigger a continuation twice which is likely to lead to double-free and use-after-free bugs and other hard to debug problems, making it mutable and replacing itself works as well. But is it enough?Note: I do believe we should still provide coroutine besides first-class continuations, they are more familiar to people, and would make it easy to port asyncdispatch and Chronos to it.
Important: I'm not totally clear whether my callCC proposal is an undelimited continuation like Scheme, or some form of delimited continuation that no language as implemented up to now. If the latter maybe we should change the name to something else. This is because it seems like a delimitation continuation is "a proc + a mutable cell" while undelimited continuation are naked? No type/functional/language theorist so hopefully I convey my confusion right.
Ergonomic concern:
{.suspend.}
Ergonomic concern, the
suspend
pragma here assumes that there is no continuation returned. And that the case where we want to chain continuations are only for proc generated by the CPS-transform. I will look into that, if I'm wrong this means we need 2 variations of the suspend pragma, with and without continuation.Beta Was this translation helpful? Give feedback.
All reactions