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

Stack safe recursion #259

Draft
wants to merge 76 commits into
base: hkmc2
Choose a base branch
from
Draft

Conversation

CAG2Mark
Copy link
Contributor

@CAG2Mark CAG2Mark commented Jan 7, 2025

Uses effect handlers to implement stack safe recursion.

Depends on #257, which should be merged first.

TODO:

  • Write more tests and check for bugs
  • __Return is currently leaked, which should be fixed
  • Don't rewrite trivial functions

@LPTK
Copy link
Contributor

LPTK commented Jan 8, 2025

Notes from today's meeting

Example that does not work:

:stackSafe 100
:handler
fun foo(f) = print(f(f)) // or just `f(f)`
foo(foo)
//│ FAILURE: Unexpected runtime error
//│ ═══[RUNTIME ERROR] RangeError: Maximum call stack size exceeded
//│     at foo (REPL11:1:60)
//│     at foo (REPL11:1:3053)
//│     at foo (REPL11:1:3053)
//│     at foo (REPL11:1:3053)
//│     at foo (REPL11:1:3053)
//│     at foo (REPL11:1:3053)
//│     at foo (REPL11:1:3053)
//│     at foo (REPL11:1:3053)
//│     at foo (REPL11:1:3053)
//│     at foo (REPL11:1:3053)

To preserve tail calls, we want to update the stack depth global variable on function calls:

let tmp = __stackDepth
set __stackDepth = tmp + 1
tailCall()
let tmp = __stackDepth
set __stackDepth = tmp + 1
nonTailCall()
__stackDepth = tmp

However, the __stackDepth = tmp will interact poorly with effect resumptions. How to deal with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants