Expanding a form triggers compile-time evaluation in the sense of
running macros. Currently, though, compilation treats changing the set
of bindings at the top level as a kind of run-time effect (to be
avoided at compile time).

For example, compiling `(define x 5)` does not change the current
binding of `x` at the top level:

 > (require (only-in racket [+ x]))
 > (define code (compile #'(define x 5)))
 > (x 1 2)
 3

Evaluating the compiled form of `(define x 5)` does change the binding f `x`:

 > (eval code)
 > x
 5

It seems clear that top-level binding needs to be a run-time effect in
that latter sense, but maybe it could also happen as a compile-time
effect. That would be a significant change, though; some things would
break. As it happens, the new expander at first leaked a binding effect
when compiling `define` --- until Leif reported it as a bug and I fixed
it.

Neither the current interaction of `compile` and binding or the
alternative seems obviously right to me, though. As you know, I've
given up on finding completely satisfactory rules for the top level.

At Mon, 21 Dec 2015 23:15:58 -0500, Alex Knauth wrote:
> 
> > On Dec 21, 2015, at 10:53 PM, Leif Andersen <l...@leifandersen.net> wrote:
> 
> > But `compile` is not supposed to evaluate any code, it just compiles
> > it. Which is why it fails to compile that code. But if you interleave
> > it with evals, it will run the require code, which gives the phase
> > level 2 code some meaning.
> 
> I understand that `compile` isn't supposed to evaluate any _runtime_ code, 
> but 
> I thought it had to evaluate the compile-time code for it to compile the 
> program?
> 
> Am I misunderstanding what `compile` is supposed to do? Or what compile-time 
> code is, or?
> 
> > Does that make any sense, or was it too rambly.
> > 
> > ~Leif Andersen
> > 
> > 
> > On Mon, Dec 21, 2015 at 8:24 PM, Alex Knauth <alexan...@knauth.org> wrote:
> >> I get that `compile` doesn't evaluate the require form, but why doesn't it 
> evaluate what's needed for compile time? I thought that was the reason for 
> needing require as a macro instead of a function. Or am I getting the purpose 
> of compile wrong?
> >> 
> >> Alex Knauth
> >> 
> >>> On Dec 21, 2015, at 7:47 PM, Matthew Flatt <mfl...@cs.utah.edu> wrote:
> >>> 
> >>> In the REPL, `begin` splices at the granularity of expansion and
> >>> evaluation. That is, the first of the two forms grouped by `begin` is
> >>> compiled and evaluated, and only afterward the second one is compiled
> >>> and evaluated.
> >>> 
> >>> The `compile` function can't do that, because it's only supposed to
> >>> compile -- not evaluate.
> >>> 
> >>> The `syntax/toplevel` library provides
> >>> 
> >>> expand-syntax-top-level-with-compile-time-evals
> >>> 
> >>> as an intermediate point between those: it pulls apart `begin` and
> >>> `expands` (which is the interesting part of `compile`) the forms in
> >>> sequence, but it evaluates only compile-time parts of the given forms.
> >>> That works for many cases, including your example.
> >>> 
> >>> At Mon, 21 Dec 2015 17:35:22 -0700, Leif Andersen wrote:
> >>>> So, I was under the impression that the REPL was the toplevel (unless
> >>>> you entered a module context or something like that). But I just had
> >>>> something challenge that assumption and so now I'm a bit confused.
> >>>> 
> >>>> I have the following top level program:
> >>>> 
> >>>> (begin
> >>>> (require (for-meta 1 racket)
> >>>>              (for-meta 2 racket))
> >>>> (begin-for-syntax
> >>>>   (begin-for-syntax
> >>>>     (define x 5))))
> >>>> 
> >>>> When I run this top level program in the REPL, it works just fine. And
> >>>> I can even reference x in later evaluations. (Provided I'm at the
> >>>> meta-level of 2).
> >>>> 
> >>>> However, when I pass this top level program into compile like so:
> >>>> http://pasterack.org/pastes/38556
> >>>> 
> >>>> (compile #'(begin
> >>>>            (require (for-meta 1 racket)
> >>>>                     (for-meta 2 racket))
> >>>>            (begin-for-syntax
> >>>>              (begin-for-syntax
> >>>>                (define x 5)))))
> >>>> 
> >>>> 
> >>>> I get the following error:
> >>>> 
> >>>> define: unbound identifier at phase 2;
> >>>> also, no #%app syntax transformer is bound in: define
> >>>> 
> >>>> Can someone give me the reason (or at least some intuition) as to why
> >>>> this works in the repl, but not at the top level?
> >>>> 
> >>>> Thank you.
> >>>> 
> >>>> ~Leif Andersen
> >>>> 
> >>>> --
> >>>> You received this message because you are subscribed to the Google Groups
> >>>> "Racket Users" group.
> >>>> To unsubscribe from this group and stop receiving emails from it, send an
> >>>> email to racket-users+unsubscr...@googlegroups.com.
> >>>> For more options, visit https://groups.google.com/d/optout.
> >>> 
> >>> --
> >>> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> >>> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> >>> For more options, visit https://groups.google.com/d/optout.
> >> 

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to