[plt-scheme] macros: incompatible ellipsis match counts
The rules for ellipses always take a while for me to page back in...
At Mon, 12 Feb 2007 14:50:58 -0800 (PST), Ryan Culpepper wrote:
> > (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.
It's ok to have extra ellipses after a pattern variable in a template.
The rule is that the innermost ellipses are treated in the usual way,
and then the overall result is repeated as many times as necessary for
remaining ellipses (where the repeat count for each remaining ellipses
is determined by some other variable).
Meanwhile, the count for each ellipses must be determined by at least
one pattern variable. Incompatible-count errors are possible if the
count is determined by more than one variable.
In this case:
(begin
(begin z f ...) ...)
...)
The count for the first `...' is determined by `f', the count for the
second `...' is determined by both `z' and `(f ...)', and the count for
the last is determined by `(z ...)'.
In a use
(foo3 ((1 2 3) 4 5 6))
There are 3 `z's (for the first and only `(z ...)') but only one `(f
...)', so the counts are incompatible.
In contrast, a use
(foo3 ((1 2 3) 4 5 6) ((4 5 6) 0) ((7 8 9) 10))
works: the number of `z's in each `(z ...)' matches the number of
overall `(f ...)'s.
In general, you can't wrap a new set of ellipses around both the
pattern and template, because the new ellipses in the pattern might get
balanced by one of the existing ellipses in the template for some
variable, thus creating new constraints on match counts. But it
generally works to add a new set of *innermost* ellipses for a variable
in both the pattern and template.
Matthew