I have a following Koka snippet here and I would like for someone to explain what happens to stackframes when handlers are invoked. I've tried to make handlers stack frames also visible by printing values & global counters and I have desugared when expression.
effect foo<a> { control foo() : a }
fun main() {
var c:int := 0
val r = (handler {
return(x:int) { println("in RET ctrl: " ++ x.show); x*2 }
control foo() {
c := c + 1
val this_c:int = c
println("in FOO ctrl_1. c is: " ++ c.show)
val r1 = resume(3)
println("in FOO ctrl_2, r1: " ++ r1.show ++ " this_c is: " ++ this_c.show)
r1*3
}
})(fn(){
println("throw first foo")
val first:int = foo()
println("throw second foo, first: " ++ first.show)
val snd:int = foo()
println("got second: " ++ snd.show ++ " RET SUM: " ++ (first + snd).show)
(first + snd)
})
println(r)
}
The result is following:
throw first foo
in FOO ctrl_1. c is: 1
throw second foo, first: 3
in FOO ctrl_1. c is: 2
got second: 3 RET SUM: 6
in RET ctrl: 6
in FOO ctrl_2, r1: 12 this_c is: 2
in FOO ctrl_2, r1: 36 this_c is: 1
108
How TWO "FOO_CTRL" handler frames are now underneath the original FN() invocation as well as RET handler?