Skip to content

Incorrectly applies tail resumption optimization under var scope #1153

@timsueberkrueb

Description

@timsueberkrueb

Effekt's optimizations are currently not semantics-preserving for the following program:

effect flip(): Bool

def all{prog: => Unit / {flip}} = {
  try {
    prog()
  } with flip {
    resume(true)
    resume(false)
  }
}

interface State[T] {
  def get(): T
  def set(x: T): Unit
}

interface allocate {
  def fresh[T, R](init: T){k: {State[T]} => R}: R
}

def newRegion[T]{prog: {r: allocate} => T}: T = {
  try {
    prog{r}
  } with r: allocate {
    def fresh[T, R](init) = {
      var x = init
      def s = new State[T] {
        def get() = x
        def set(y) = {
          x = y
        }
      }
      resume{{k} => k{s}}
    }
  }
}

def main() = {
  newRegion { {r} =>
    all {
      with def x = r.fresh(0)
      if (do flip()) {
        x.set(3)
      } else {
      }
      println(x.get())
    }
  }
}

with optimizations:

> effekt regions.effekt
3
0

without optimizations:

> effekt regions.effekt --no-optimize
3
3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions