cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.89k stars 983 forks source link

set! creates new bindings #850

Open ak-1 opened 4 days ago

ak-1 commented 4 days ago

My expectation was that

set! does not establish a new binding for var but rather alters the value of an existing binding.

However:

$ scheme
Chez Scheme Version 10.0.0
Copyright 1984-2024 Cisco Systems, Inc.

> x
Exception: variable x is not bound
Type (debug) to enter the debugger.
> (set! x 123)
> x
123
> (let () y)
Exception: variable y is not bound
Type (debug) to enter the debugger.
> (let () (set! y 456) y)
456
> 
jltaylor-us commented 4 days ago

There are several differences between the Interaction Environment and RnRS environments. This is one of them.

All identifiers not bound in the initial interaction environment and not defined by the programmer are treated as "potentially bound" as variables to facilitate the definition of mutually recursive procedures.

ak-1 commented 4 days ago
All identifiers not bound in the initial interaction environment and not defined by the programmer are
treated as "potentially bound" as variables to facilitate the definition of mutually recursive procedures.

After reading this I still would not expect this set! behavior.

I looked at various other scheme implementations. At least racket, guile and mit-scheme all allow mutually recursive definitions like yin / yang in their interaction environments, but do not allow to set! an unbound identifier.

If this is really intended in ChezScheme, maybe it should be stated explicitly in the user guide.