johnynek / bosatsu

A python-ish pure and total functional programming language
Apache License 2.0
224 stars 11 forks source link

compiling with thunks #978

Open johnynek opened 1 year ago

johnynek commented 1 year ago

In a pure total language, evaluation order in strict vs lazy gives the same result, although the runtime or space requirements may be different.

We support recursion, but not all recursion can be expressed in a tail-recursive manner. In order to make non-tail-recursive functions safe, we could compile those using thunks for the recursion, pushing the stack space consumption into allocated memory rather than directly on the stack.

An important non-tail recursive function is fold-right. Although, we can implement that one by doing reverse then implementing fold-left, but that does lose some of the short-circuiting that we get in a few cases with fold-right (e.g. checking if all elements or any element of a list satisfies a predicate).

This relates to #966 since doing that potentially deoptimizes a lot of safe recursions into fold-right.