[plt-scheme] Using syntax before define-syntax is weird

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Thu Apr 8 00:39:31 EDT 2010

On Thu, Apr 8, 2010 at 12:16 AM, Neil Toronto <ntoronto at cs.byu.edu> wrote:
> DrScheme 4.2.5. This:
>
> (define-syntax (bob stx)
>  (syntax-case stx ()
>    [(_)  #'3]
>    [_  #'4]))
>
> (bob)
>
>
> outputs 3 as it should. This:
>
> (bob)
>
> (define-syntax (bob stx)
>  (syntax-case stx ()
>    [(_)  #'3]
>    [_  #'4]))
>
>
> results in "procedure application: expected procedure, given: 4 (no
> arguments)" The macro stepper verifies that (bob) is getting expanded to
> (4). IOW, the expander never passes #'(bob) to "bob".
>
> Is this intended behavior?
>
> Neil T

Yup.  There are two passes to expansion: head expansion, then full
expansion.  The expander has to find out if "bob" defines syntax that
the subsequent form will use, or variables it will reference.  So it
expands it far enough to find out if it's a definition or expression,
but doesn't do anything more with the body unless it's a syntax
definition.

In this case, it doesn't know what "bob" is during head expansion, so
it gives up.  If you had (list (bob)), it'd work fine.  It'd say "oh,
list?  that's an expression", save the rest of the expression
expansion for later, and get to the define-syntax.  Then when it got
back to the rest of the expression, it'd know all about bob.

--Carl


Posted on the users mailing list.