Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental control-flow operator #368

Merged
merged 45 commits into from
Mar 26, 2021
Merged

Conversation

jsiek
Copy link
Contributor

@jsiek jsiek commented Mar 9, 2021

This adds "delimited continuations" with delimit, yield and resume features that provide a kind of resumable exception with first-class continuations. The point of adding this feature is not to say that we want delimited continuations in Carbon, but this represents a place-holder for other powerful control-flow features that might eventually be in Carbon, such as coroutines, threads, exceptions, etc. As we refactor the executable semantics, having this feature in place will keep us honest and prevent us from accidentally simplifying the interpreter to the point where it can't handle features like this one.

Instead of delimited continuations, we could have instead done regular continuations with callcc. However, there seems to be a consensus amongst the experts that delimited continuations are better than regular ones.

This implementation needs more testing. However, the examples from the literature that I'd like to use as tests all use lambda, so I'm going to do an experimental lambda and then circle back to add more tests for the delimited continuations.

Fixes #349

@jsiek jsiek added the explorer Action items related to Carbon explorer code label Mar 9, 2021
@jsiek jsiek requested a review from a team as a code owner March 9, 2021 13:38
@google-cla google-cla bot added the cla: yes PR meets CLA requirements according to bot. label Mar 9, 2021
@dabrahams
Copy link

@jsiek Note my edit to the description that cross-references the issue.

@jsiek
Copy link
Contributor Author

jsiek commented Mar 9, 2021

Thanks Dave!

Copy link

@dabrahams dabrahams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a hassle, but comments on all the new entities is basically a prerequisite for getting a useful review out of me. Those statements are how I evaluate whether a thing does/is what it's supposed to be. Please do that at a minimum and ask me to re-review.

Thanks!

executable_semantics/ast/expression.cpp Outdated Show resolved Hide resolved
executable_semantics/ast/expression.h Outdated Show resolved Hide resolved
executable_semantics/ast/expression.h Outdated Show resolved Hide resolved
executable_semantics/interpreter/interpreter.cpp Outdated Show resolved Hide resolved
executable_semantics/interpreter/interpreter.cpp Outdated Show resolved Hide resolved
executable_semantics/interpreter/interpreter.cpp Outdated Show resolved Hide resolved
executable_semantics/interpreter/interpreter.cpp Outdated Show resolved Hide resolved
@jsiek
Copy link
Contributor Author

jsiek commented Mar 9, 2021

Thanks for the feedback and reminders Dave!
I've added comments, especially for the hairy chunks of code.

Copy link
Contributor

@geoffromer geoffromer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be a lot easier to review (and a lot easier for future maintainers to read) if there's some kind of prose description of this feature from the point of view of the language user. It doesn't have to be super detailed, since the whole point of this code is to act as a specification, but just a quick introduction to the core vocabulary (e.g. "continuation values"), syntax, and the broad strokes of the semantics. The best place would probably be executable_semantics/README.md, where it can also act as a "this is a stress-test of the executable_semantics architecture, not a planned Carbon feature" disclaimer.

@jsiek
Copy link
Contributor Author

jsiek commented Mar 9, 2021

@geoffromer good idea, I'll add an introduction to this in the README. Thanks!

@jsiek
Copy link
Contributor Author

jsiek commented Mar 21, 2021

I've implemented an imperative variant of the shift/reset style of continuations.
The README file is updated with a short description and there are three new test cases.
So far so good. I think this holds together OK and gives us a peek at what coroutines might
look like for Carbon.
The next thing on my todo list for this is to add more test cases to really kick the tires.
BTW, this design is still amenable to value semantics... we just need to tweak what happens
when a continuation object is copied. (Actually, I'm not sure what happens there at the moment.)

@jsiek
Copy link
Contributor Author

jsiek commented Mar 22, 2021

Copying a continuation now does a deep copy of its stack.

@jsiek jsiek marked this pull request as ready for review March 22, 2021 21:56
@jsiek jsiek requested a review from geoffromer March 22, 2021 21:57
@jsiek
Copy link
Contributor Author

jsiek commented Mar 23, 2021

After more thinking and more test cases, I flipped back to shallow copy for continuations. Some of the behavior from allowing copies is just too weird, such as double deletes of a local variable (the now deleted test testdata/experimental_continuation9.6c).

executable_semantics/ast/expression.cpp Show resolved Hide resolved
executable_semantics/ast/statement.h Outdated Show resolved Hide resolved
executable_semantics/interpreter/action.h Outdated Show resolved Hide resolved
executable_semantics/interpreter/interpreter.cpp Outdated Show resolved Hide resolved
executable_semantics/interpreter/interpreter.cpp Outdated Show resolved Hide resolved
executable_semantics/interpreter/value.h Outdated Show resolved Hide resolved
executable_semantics/interpreter/value.h Outdated Show resolved Hide resolved
executable_semantics/testdata/experimental_delimit1.6c Outdated Show resolved Hide resolved
executable_semantics/testdata/lifetime_assignment1.6c Outdated Show resolved Hide resolved
@jsiek jsiek requested a review from geoffromer March 24, 2021 19:26
@jsiek jsiek requested a review from geoffromer March 24, 2021 21:14
@jsiek jsiek requested a review from dabrahams March 26, 2021 13:23
@jsiek jsiek merged commit f3fd9f7 into carbon-language:trunk Mar 26, 2021
@jsiek jsiek deleted the demo-control branch March 26, 2021 15:22
chandlerc pushed a commit that referenced this pull request Jun 28, 2022
* AST and syntax for delimited control

* stashing for later

* a little more progress

* progress on delimited continuations

* delimit, suspend, and resume implemented (draft)

* example that generates the natural numbers

* fixes

* tinkering

* changed demo to experimental

* comments and name changes

* describe delimited continuations in the README

* renamed Snapshot to Continuation, edits to comments

* Update executable_semantics/ast/statement.h

improve comment for MakeDelimitStmt

Co-authored-by: Dave Abrahams <dabrahams@google.com>

* Update executable_semantics/interpreter/interpreter.cpp

remove snake_case

Co-authored-by: Dave Abrahams <dabrahams@google.com>

* edits to comments, change name of variable

* updates to handle review edits

* trailing whitespace

* fixes to delimited continuations, added more tests, also fixed assignment to do a copy

* improvements from Geoffrey

* new test from Geoffrey, fix for empty blocks

* more suggestions from Geoffrey

* more tests for delimited continuations, renaming some of them

* renamed test files

* improve a comment

* sketch of creating continuation

* initial implementation of shift/reset style continuations

* more documentation

* fix some camel case

* implemented deep copy of continuations, added a test case for it

* fixed a bug and got the recursive test case working

* removed __delimit, polished up __continuation

* back to shallow copy for continuations

* suggestions from Geoffrey

* removed structured binding (for now)

* Update executable_semantics/ast/expression.cpp

Co-authored-by: Geoff Romer <gromer@google.com>

* responses to Geoffrey

Co-authored-by: Dave Abrahams <dabrahams@google.com>
Co-authored-by: Geoff Romer <gromer@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes PR meets CLA requirements according to bot. explorer Action items related to Carbon explorer code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Demo control-flow operator
3 participants