koka-lang / koka

Koka language compiler and interpreter

Home Page:http://koka-lang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why named handlers raise div?

Lysxia opened this issue · comments

When I use a named handler, I get both exn and div. I get why we need exn, because names can escape their scope. But why div?

For concreteness, not minimal but kinda small example:

value type option<a>
  None
  Some{x : a}

fun iter(xs : list<a>, f : a -> e ()) : e ()
  match xs
    Nil -> ()
    Cons(x, xs) -> { f(x) ; iter(xs, f) }

named effect found'<a>
  ctl found'(x : a) : b

fun find3(xs : list<a>, f : a -> e bool) : <div,exn|e> option<a>
  with h <- named handler ctl found'(x) Some(x)
  fun f'(x) { if mask<div>(fn() -> mask<exn>(fn() -> f(x)))
              then h.found'(x) }
  iter(xs, f') ; None

In line 1068 of the Parser

           -- for now add a divergence effect to named effects/resources when there are type variables...
           -- this is too conservative though; we should generate the `ediv` constraint instead but
           -- that is a TODO for now

And this is from Koka's core.kk

// The predicate `:ediv<x,a,e>` signifies that if the type `:a` contains a reference to effect constant `:x",
// then the effect `:e` must contain the divergent effect (`:div`). This constraint is generated
// for operations on first-class effects where code can diverge through operations.
pub type ediv :: (X,V,E) -> P