Skip to content

val binding causes unexpected type escape or effect mismatch in presence of local effect #732

@amistozy

Description

@amistozy

Description:
When I write the following code:

fun test(action)
  var i := 0
  val res = action()
  res

I get the following compiler error:

abstract type(s) escape(s) into the context
term         :   var i := 0
                 val res = action()
                 res
inferred type: () -> <local<$h>|_e> _a
hint         : give a higher-rank type annotation to a function parameter?

So I try to fix it by adding a type annotation:

fun test(action : () -> e a) : e a
  var i := 0
  val res = action()
  res

However, this gives a different error:

effects do not match
context        :   var i := 0
                   val res = action()
                   res
term           :   val res = action()
                   res
inferred effect: $e
expected effect: <local<_h>|_e1>

Surprisingly, if I inline the call to action() without a val binding, the code compiles and runs fine:

fun test(action)
  var i := 0
  action()

This behavior is confusing.

Expected behavior:
The presence or absence of a val binding for the result of action() should not affect whether type or effect inference succeeds. The two forms should be equivalent from the perspective of effects and region scoping.

Environment:

  • Koka version: v3.1.3-alpha14
  • OS: Windows 11

Minimal reproduction:

fun test(action)
  var i := 0
  val res = action()
  res

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions