Eigenstate: myrddin-dev mailing list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Questions on closure environment capture


Hi,

I'm trying to use the closure in Myrddin, but I'm confused about the
implementation of it and the so-called environment capture.

Here are some code in my program. 

============ code begins ================

use std

const f_ = {t
    -> {x; -> x+t}
}

const main = {
    var i, j
    var f: (x:int -> int)[8]
    var g

    for i = 0; i < 4; i++
        f[i] = {x
            -> x+i
        }
    ;;

    for i = 5; i < 8; i++
        f[i] = f_(i)
    ;;
   
    i = 100
    g = {x; -> x+i}
   
    for j = 0; j < 8; j++
        std.put("i = {}\n", i)
        std.put("{}\n", f[j](4))
    ;;
    std.put("{}\n", g(4))
}

================= code ends ==================

In f[0] to f[4], the result is {x; ->x+3}, I think that's because the
`i` in the function binds to the last `i` defined. So I tried to use
something like `(lambda t. (lambda x. x+t)) i` so that I can make
multiple f(x)=x+i functions with different `i`. However, when the
program tried to call f[5](4), I got a segfault.

So what is the semantic and implementation of closure and environment
capture? Is there an easy way to implement the functions I said above?


Thanks,

Iru


Follow-Ups:
Re: Questions on closure environment captureQuentin Carbonneaux <quentin@xxxxxx>