[plt-scheme] Problem with syntax-case macro under MzScheme 352

From: Ryan Culpepper (ryan_sml at yahoo.com)
Date: Sun Sep 10 15:55:53 EDT 2006

First, let me say a little more about what happened with the first
version of this macro. When you left out the explicit
datum->syntax-object, you let the macro produce a definition of a
marked identifier 'gen:g'.

If you've got a nightly build, you can see this by putting your
original macro definition in a buffer along with the use '(m4 gen)'.
Click the Macro Stepper button, then click Next Term to skip past the
macro definition to the use. The use '(m4 gen)' expands into two
definititions. The first definition is for 'gen:g', but it's in some
color (probably green), whereas the second definition, for 'gen', is
in black. The macro stepper colors syntax to show that it has marks
on it. The definition of 'gen:g' will only bind references that are
also colored green, not references in unmarked black. If you correct
the macro definition, you'll see 'gen:g' in black, too.

Second, it's true that Scheme macro systems are complicated and have
sharp edges and rough patches to them. To make them approachable to
beginners, we need language tools like the macro stepper and language
levels, and we also need programmer guidance like tutorials and
design recipes. The macro stepper only helps if you know how to
interpret what it shows you---that is, if you know that green
(marked) definitions won't bind black (unmarked) variable references.

The macro stepper is one major step towards making macros easier to
program with. I'd be grateful for feedback about whether the macro
stepper is helpful to you as well as suggestions for improvement.


--- Blake McBride <blake at mcbride.name> wrote:

> Never mind, I figured it out.  The macro should have been:
> (define-syntax m4
>    (lambda (x)
>      (syntax-case x ()
>                   ((_ v)
>                    (with-syntax ((name (datum->syntax-object
> (syntax 
> v) (string->symbol (string-append (symbol->string 
> (syntax-object->datum (syntax v))) ":g")))))
>                                 (syntax (begin
>                                           (define name '())
>                                           (define v '()))))))))
> I figured it out by using Chez Scheme.  It gave me a meaningful
> message with the wrong macro.  MzScheme lets it run and hints that
> it's right.
> I can't hardly imagine making it any more complicated.  The
> simplest things are made rocket science with define-syntax and
> friends. Lisp macros may not be perfect but normal people (those
> who don't devote their life to understanding the intricacies of
> syntax-case) can use and understand it.  Syntax-case is killing
> scheme.
> Blake McBride
> At 09:06 AM 9/9/2006, Blake McBride wrote:
> >Greetings,
> >
> >I am attempting to create a macro using define-syntax, syntax-case
> and
> >with-syntax under MzScheme 352.  I believe my macro definition is
> >correct but it just doesn't work under MzScheme.  The reason I
> believe
> >my macro definition is correct is because when I cause the macro
> to
> >quote it's output (to prevent evaluation) it returns the correct
> >expression.  When I try the expression outside the macro system it
> >works.
> >
> >The following example and corresponding macro are simplified
> versions
> >of what I am actually doing but does demonstrate the problem.
> >Here is the example:
> >
> >Input is:
> >
> >(m4 gen)
> >
> >Expected code to be executes is:
> >
> >(begin
> >    (define  gen:g  '())
> >    (define  gen    '()))
> >
> >So, I should end up with two variables being defined.  The macro
> >generates the correct code, but when it is run gen gets defined
> but
> >gen:g does not.  If I execute the above code manually, it works. 
> Why
> >doesn't it work through the macro?
> >
> >The macro is:
> >
> >(define-syntax m4
> >   (lambda (x)
> >     (syntax-case x ()
> >                 ((_ v)
> >                   (with-syntax ((name (string->symbol 
> > (string-append (symbol->string (syntax-object->datum (syntax v)))
> ":g"))))
> >                                (syntax (begin
> >                                         (define name '())
> >                                         (define v '()))))))))
> >
> >Thanks.
> >Blake McBride
> > _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.