[racket] confusion about shadowing predefined functions in interactions window

From: Dan Grossman (djg at cs.washington.edu)
Date: Tue Oct 23 13:22:59 EDT 2012

Yes, in my non-Racket-expert view, the core issue is certainly interactions
between toplevel and the module system.  It seems unfortunate at best that
we have:

Welcome to DrRacket, version 5.3 [3m].
Language: racket; memory limit: 512 MB.
> (define (range lo hi)
     (print "hi")
     (if (> lo hi) null (cons lo (range (+ 1 lo) hi))))
> (range 3 7)
"hi"'(3 4 5 6)

but:

Welcome to DrRacket, version 5.3 [3m].
Language: racket; memory limit: 512 MB.
> (define range 0)
> (define (range lo hi)
     (print "hi")
     (if (> lo hi) null (cons lo (range (+ 1 lo) hi))))
> (range 3 7)
"hi""hi""hi""hi""hi""hi"'(3 4 5 6 7)
>

I'm sure this is an old topic, but would there be fewer gotchas if the REPL
were in its own module, importing whatever was defined by the definitions
window?

(I note that range was not defined in Racket 5.2, but we seem to have the
same behavior with map, so this is not a new behavior with respect to the
top-level.)

--Dan

On Tue, Oct 23, 2012 at 10:02 AM, Jens Axel Søgaard
<jensaxel at soegaard.net>wrote:

> I forgot to add one rationale for having a context dependent meaning
> of define-values. With the current setup, you can write
>
> (module foo some-lang
>    (define (range ...) ...))
>
> without worrying about whether range is defined in some-lang or not.
>
> --
> Jens Axel Søgaard
>
>
> 2012/10/23 Jens Axel Søgaard <jensaxel at soegaard.net>:
> > 2012/10/23 Dan Grossman <djg at cs.washington.edu>:
> >>
> >> Thanks, David.  I would be interested in someone walking through how
> this
> >> behavior arises -- as well as the design issue regarding the "hopeless
> >> top-level": Is this the least-bad option available or an unexpected
> >> consequence?
> >>
> >> --Dan
> >
> > The expansion in the repl is:
> >
> >> (syntax->datum
> >    (expand
> >     '(define (range lo)
> >        (lambda (hi) (if (> lo hi) null (cons lo ((range (+ 1 lo))
> hi)))))))
> >
> > '(define-values
> >   (range)
> >   (lambda (lo)
> >      (lambda (hi)
> >         (if (#%app > lo hi)
> >             null
> >              (#%app cons lo (#%app (#%app range (#%app + '1 lo)) hi))))))
> >
> > The tricky thing here is that the meaning of a define-values form is
> > dependent on context:
> >
> >     In an internal-definition context (see Internal Definitions), a
> > define-values form
> >     introduces local bindings.
> >
> >    At the top level, the top-level binding for each id is created
> > after evaluating expr,
> >    if it does not exist already, and the top-level mapping of each id
> >    (in the namespace linked with the compiled definition) is set to the
> binding
> >    at the same time.
> >
> > An the documentation on an internal-definition context says:
> >
> >     A define-values form: The lexical context of all syntax objects
> > for the body
> >     sequence is immediately enriched with bindings for the define-values
> form.
> >
> >
> http://docs.racket-lang.org/reference/define.html?q=define-values&q=repl#(form._((quote._~23~25kernel)._define-values))
> >
> > --
> > Jens Axel Søgaard
> >
> >
> > --
> > --
> > Jens Axel Søgaard
>
>
>
> --
> --
> Jens Axel Søgaard
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121023/1a2299b8/attachment.html>

Posted on the users mailing list.