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

Trampolines (or some other way of avoiding RecursionError on long compositions of Reader or State) #11

Closed
suned opened this issue Sep 11, 2019 · 2 comments
Labels
enhancement New feature or request

Comments

@suned
Copy link
Owner

suned commented Sep 11, 2019

Creating a long chain of and_then calls to the same monad can in theory overflow the stack. The common solution to this problem is trampolines.

I'm not sure how big a of a problem this will be in practice, since I think combining imperative style with functional style can in many cases alleviate the problem (see e.g the implementation of util.sequence_.)

Edit:
It will be a big problem.

@suned suned added the enhancement New feature or request label Sep 11, 2019
@suned suned changed the title Trampolines Trampolines (or some other way of avoiding RecursionError on long compositions of Readers and Writers) Sep 13, 2019
@suned suned changed the title Trampolines (or some other way of avoiding RecursionError on long compositions of Readers and Writers) Trampolines (or some other way of avoiding RecursionError on long compositions of Reader or State) Sep 13, 2019
@suned
Copy link
Owner Author

suned commented Sep 13, 2019

Did a first (failed) attempt in /suned/pfun/experiment/trampolines. Main challenge is that python does not support tail call optimisation what so ever. There are multiple hacks for self tail calls, but so far no luck in making it work with e.g State. I think the problem is that the recursion involves a number of intermediate calls that are not part of the mutual recursion.

One other interesting idea is to implement the recursive structures in C since gcc supports tail call optimisation. Cython may be an option, but its unclear whether the compiled code will be tail recursive.

@suned
Copy link
Owner Author

suned commented Sep 14, 2019

Fixed :)

@suned suned closed this as completed Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant