[plt-scheme] macros: incompatible ellipsis match counts
--- Jon Rafkind <workmin at ccs.neu.edu> wrote:
> I was playing around with macros and am having trouble
> understanding
> whats going wrong here. This macro works
>
> (define-syntax foo2
> (syntax-rules ()
> ((_ (z ...) f ...)
> (begin
> (begin z f ...) ...))))
>
> (foo2 (1 2 3) 4 5 6)
>
> The macro stepper says it evaluates to
> (begin (begin 1 4 5 6) (begin 2 4 5 6) (begin 3 4 5 6))
>
> But if I put another ... after (z ...) f ... then things break.
>
> (define-syntax foo3
> (syntax-rules ()
> ((_ ((z ...) f ...) ...)
> (begin
> (begin
> (begin z f ...) ...)
> ...)
> )))
This 'syntax-rules' expression is wrong. The 'z' pattern variable has
an ellipsis-depth of 2, and the 'f' pattern variable also has an
ellipsis-depth of 2. In the template, though, you're using 'f' at a
depth of 3. Usually that kind of error is caught at compile time. You
seem to have found a bug in mzscheme's syntax-rules/syntax-case.
>
> (foo3 ((1 2 3) 4 5 6))
>
> syntax: incompatible ellipsis match counts for template in: ...
>
> Somehow this works if I don't have the ... after f, only using the
> ...
> after the inner-most begin to match all the parts.
>
> (define-syntax foo3
> (syntax-rules ()
> ((_ ((z ...) f ...) ...)
> (begin
> (begin
> (begin z f) ...)
> ...))))
Here both 'z' and 'f' have ellipsis-depth 2 in both the pattern and
the template. So this is okay. This macro basically zips the z-list
and the f-list together...
>
> Which the macro stepper says is
> (begin (begin (begin 1 4) (begin 2 5) (begin 3 6)))
>
> Which makes sense but that only works if the z's and f's have the
> same arity. If I use
> (foo3 ((1 2 3) 4 5))
>
> then it breaks:
> syntax: incompatible ellipsis match counts for template in: ...
... which is why it breaks if you have different numbers. The macro
is just pairing the 'f's and the 'z's together, and it complains if
they don't match up.
Ryan
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>