suned / pfun

Functional, composable, asynchronous, type-safe Python.

Home Page:https://pfun.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

suned opened this issue · comments

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.

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.

Fixed :)