[racket] interaction vs definition window: difference in semantics?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Feb 14 08:05:30 EST 2014

In the definitions window,

   (define eval (lambda () eval))

is a definition that refers to itself, because the definition shadows
the `eval` that is imported by `#lang racket`.

In the interactions window, the `(lambda () eval)` part is compiled
before the definition takes effect, so that the definition doesn't
shadow as early. That is, `(lambda () eval)` first becomes a thunk that
refers to the `eval` from `racket`, and only afterward is the thunk
bound to `eval`, so that future references to `eval` in the
interactions window see the shadowing binding, instead of the import.

The definitions-window behavior is the "module" behavior (because the
definition window's content is in a module), and the
interactions-window behavior is the "top level" (because it corresponds
to a namespace that lives outside of all modules, roughly).

The module behavior is clearly better, and it works because all
bindings can be determined before anything is evaluated. The top level
is not able to behave that way, in general, because it must interleave
compilation/evaluation with binding.

You can imagine tweaking the way the top level works to get closer to
the module behavior (e.g., by having the compiler notice that it's
compiling a definition of `eval` and make the binding take place
early). The strategy gets ever more complex the more case you try to
cover, however; as far as we can tell, you can never reach the same
behavior as the module window due to the interleaved nature of the top
level.

Our shorthand for this situation is "the top level is hopeless".

At Fri, 14 Feb 2014 13:15:10 +0100, Mattias De Wael wrote:
> Dear all
> 
> I was wondering if anyone could explain me what the difference is in semantics
> between evaluating a function definition in the "interactions window" versus
> evaluating the same definition in the "definitions window"?
> 
> The motivation of my question is the following small Racket program, which
> behaves differently depending on where I evaluated the definition:
> 
> ==================================
> | DrRacket 
> ==================================
> #lang racket
> ; Definitions window
> (define eval (lambda () eval))
> ==================================
> > (eq? eval (eval))
> #t
> ==================================
> 
> 
> 
> ==================================
> | DrRacket 
> ==================================
> #lang racket
> ; Definitions window
> ; empty
> ==================================
> > (define eval (lambda () eval))
> > (eq? eval (eval))
> #f
> ==================================
> 
> Is this behaviour intended?
> 
> Sincerely yours,
> 
> Mattias 
> 
> 
> --
> Mattias De Wael
> Vrije Universiteit Brussel
> Faculty of Sciences, DINF - SOFT
> Room 10F719, Pleinlaan 2, B-1050 Brussels, Belgium
> e-mail: madewael at vub.ac.be
> 
> 
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.