[plt-scheme] expansion of internal define v. set!

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Tue Nov 11 10:52:26 EST 2003

On Tuesday, November 11, 2003, at 12:14 AM, David Van Horn wrote:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> (define-syntax f
>   (syntax-rules ()
>     ((f e) (define e 'by-syntax))))
> (define-syntax f
>   (syntax-rules ()
>     ((f e) (set! e 'by-syntax))))
> (let ((x 'undefined))
>   (define (f _) (set! x 'by-procedure))

This is a Scheme design problem.
At this point, you need to check whether (f x) represents a definition 
or not.
If it does, it belongs to the internal definitions and needs to go into 
a letrec.
If it doesn't, it is the beginning of the command/expression sequence 
does not belong to the definitions.

How do you decide whether (f x) is a definition? Bunch of options, all 
  (1) You say it doesn't look like one, so skip it.
  (2) You say f is define-syntax'ed so it must be expanded, at least 
until you
  know whether it's a definition or not.
  (3) You say that f is define-syntax'ed but you also know that (define 
(f x) ...)
  binds f and therefore shadows everything following the (define ...). 
Of course
  this option presumes that you understand (a portion of) the binding 
  of the program before you have finished expanding the whole thing. 
  lexical analysis is performed at the same time as parsing, which at 
least on
  the surface sounds horribly wrong.
I guess that Matthew chose option 2 because define-syntax'es must be
recognized during expansion no matter what, unlike lexical scoping. So
(f x) looks like a macro app then. Option 1 is almost inconsistent with 

In any case, Scheme is not well-defined for this corner (I am cc'ing 
Will Clinger
who is the language lawyer, aka, editor, just in case I am wrong). It 
shows one
more time how much a full definition matters, and probably how bad 
definitions are. But I am using them, too, though at other places.

>   (f x)
>   x)
> Using the first syntax definition of f results in 'by-syntax, but 
> using the
> second results in 'by-procedure.  I'm not sure what the right behavior 
> is, but
> it seems like either 'by-syntax or 'by-procedure should be returned in 
> both
> cases.  Is this a bug or am I missing some subtlety in macros that 
> expand into
> definitions?  (I tried this in a module and observed the same 
> behavior.)
> (The other Schemes I have lying around, MIT and 48, return 
> 'by-procedure in
> both cases.)

-- Matthias

Posted on the users mailing list.