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

Add multi-shot generators #80

Open
Technologicat opened this issue Jun 23, 2021 · 4 comments
Open

Add multi-shot generators #80

Technologicat opened this issue Jun 23, 2021 · 4 comments
Assignees
Labels
documentation Non-executable English, for humans enhancement New feature or request
Milestone

Comments

@Technologicat
Copy link
Owner

As of v0.15.0, there is a toy implementation of multi-shot generators (i.e. re-resumable generators) buried within the automated tests, see unpythonic.syntax.tests.test_conts_gen.

Playing around with the idea, multi-shot generators seem a much nicer API to use continuations with than the low-level call_cc[].

Thus, we could extract it into a proper mcpyrate macro, and add it to the public macro API.

We should caution in the docs that it'll only work inside a with continuations block, since doing things like this in Python requires a CPS conversion. Or maybe make this another, alternative multi-shot-continuations API, and internally compile into a with continuations.

This requires first fleshing out some details. Ideally, a multi-shot generator should look similar to a classical Python generator, with only the difference that a multi-shot generator can resume again from an earlier yield (arbitrarily many times).

@Technologicat Technologicat added the enhancement New feature or request label Jun 23, 2021
@Technologicat Technologicat added this to the 0.15.x milestone Jun 23, 2021
@Technologicat Technologicat modified the milestones: 0.15.x, 0.15.2 Jan 28, 2022
@Technologicat
Copy link
Owner Author

Supporting all of the generator API is a large project, so maybe just implement the very basics, showing how it could be done.

With the new get_cc function in 0.15.1, and the multi-phase compiler in mcpyrate, we can write a lot cleaner demo than what was possible before.

So, let's include such a short demo in 0.15.2.

Technologicat added a commit that referenced this issue Jan 28, 2022
See #80.

This still needs to be moved into its own module.
@Technologicat
Copy link
Owner Author

Example moved into unpythonic.syntax.tests.test_conts_multishot.

@Technologicat Technologicat self-assigned this Jan 30, 2022
@Technologicat
Copy link
Owner Author

Now we just need to update the documentation, particularly the section on continuations in doc/macros.md.

  • Document get_cc and the pattern k = call_cc[get_cc()]. Material exists in the docstring of get_cc.
  • Document how to use this pattern to make multi-shot generators. Refer to unpythonic.syntax.tests.test_conts_multishot, which demonstrates this and packages it into a @multishot decorator macro, which can be used lexically inside a with continuations block.

@Technologicat Technologicat added the documentation Non-executable English, for humans label Jan 31, 2022
Technologicat added a commit that referenced this issue Jan 31, 2022
@Technologicat
Copy link
Owner Author

Now the demo includes also a MultishotIterator that shows how to make a @multishot multi-shot generator conform to the most often used parts of Python's generator API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Non-executable English, for humans enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant