This project is read-only.
1

Closed

Closuures

description

The following closure seems to behave like a delay and promise.

(define fnc (lambda () (lambda () x)))
(define closure (fnc))
(define x 1)
(closure)

(set! x 2)
(closure)

The lexical scope of the inner procedure includes the top level namespace.
Unlike lambda expressions the define syntax form can be used anywhere in
the top level. This example deliberately exploits this allowance.

The first time closure is applied it returns 1. The second time it still returns 1.

If we behave more politely and define the x variable ahead of the closure creation then
it behaves as expected.

(define x 1)
(define fnc (lambda () (lambda () x)))
(define closure (fnc))
(closure)
(set! x 2)
(closure)

returning 1 the first time and 2 the second time.

Is this a bug?

file attachments

Closed Mar 25, 2014 at 8:03 AM by leppie
REPL != top-level

comments

leppie wrote Mar 25, 2014 at 7:53 AM

Hi

This seems to be similar to this issue.

I suspect it is affected by the same codegen "optimization"...

I will have a look at the generated code later this week to confirm.

Thanks

leppie

wrote Mar 25, 2014 at 8:01 AM

leppie wrote Mar 25, 2014 at 8:01 AM

Hi

This issue is only present in the REPL, and thus it is a non-issue (all bets are off in the REPL).

Running it as a program produces the correct result.

Same program (test.ss):
(import (ironscheme))

(define fnc (lambda () (lambda () x)))
(define closure (fnc))
(define x 1)
(printf "~s\n" (closure))

(set! x 2)
(closure)
(printf "~s\n" (closure))
IronScheme-Console test.ss
1
2
Cheers

leppie

wrote Mar 25, 2014 at 8:03 AM