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

Suggestion: rcontext.resume-shallow should not run the return clauses #510

Open
anfelor opened this issue Apr 24, 2024 · 0 comments
Open

Comments

@anfelor
Copy link
Collaborator

anfelor commented Apr 24, 2024

Unlike Frank, Koka does not remove the return clause when resuming a shallow handler (this was originally spotted by @jasigal). This is currently necessary since resume-shallow has the same return type as resume (which runs the potentially-type-changing return clause): https://koka-lang.github.io/koka/doc/std_core_hnd.html#type_space_resume_context

fun resume( r : resume-context<a,e,e1,b>, x : a ) : e b
fun resume-shallow( r : resume-context<a,e,e1,b>, x : a ) : e1 b

However, this is quite annoying in practice. For example, we could attempt to get Frank-style pattern matching on shallow handlers by writing:

effect send<a>
  ctl send(x : a) : unit

type ssend<a,e,b> { SDone(b : b); Send(x : a, k : () -> <send<a>|e> b) }

fun canthappen() : a
  fun go() { go() }
  unsafe-total(go)

fun ssend(f : () -> <send<a>|e> b) : e ssend<a,e,b>
  with handler
    return(x) { SDone(x) }
    raw ctl send(x)
      Send(x, fn()
        match rcontext.resume-shallow(()) // we would expect to get 'b' but get 'ssend<a,e,b>'
          SDone(y) -> y                   // unwrap the return clause
          Send(_, _) -> canthappen()      // we removed the effect handler so no Send constructor can happen
      )
  f()

This is not just a bit ugly, but also less efficient since resume-shallow(()) should really be a tail-call here.

I would guess that to fix this the resume-context should carry two different types: The return type before running the return() clause and the type after running the return() clause. Then in https://koka-lang.github.io/koka/doc/std_core_hnd-source.html#prompt the case:

  Shallow x ->
    yield-bind( cont({x}), fn(y) ret(y) )

should really be:

  Shallow x ->
    cont( {x} )
@TimWhiting TimWhiting added the bug label May 2, 2024
@anfelor anfelor changed the title rcontext.resume-shallow runs the return clauses Suggestion: rcontext.resume-shallow should not run the return clauses May 14, 2024
@anfelor anfelor added enhancement and removed bug labels May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants