First of all, delta
is out (should not match alpha
) because it clearly lexically binds alpha
to another binding than the one under which your sytnax-rules
appears. The interesting ones are beta
and gamma
.
As per section 5.2.2. of R4RS (p. 13) and R5RS (p. 16), section 5.3.2. of R7RS (p. 26), and section 11.3. of R6RS (p. 32), the "region" of a binding established through an internal definition is the entire <body>
in which the definition appears. And your macro call to foo
is clearly within the same <body>
as those internal definitions.
R7RS also goes a little further and warns us:
Note that such a body [i.e. one which contains internal definitions] might not be apparent until after expansion of other syntax.
So the messed up order of events is admitted, but there is no ambiguity; your syntax-rules
should not match the alpha
branch if there's a binding for alpha
by any internal definition in the same <body>
as the call to the macro. Therefore, beta
and gamma
are out as well.
Appendix A
If we complicated the situation further, and your macro itself conditionally bound alpha
, like
(syntax-rules (alpha)
((_ alpha x) (define alpha x)))
then it seems truly ambiguous at first thought, but I believe this is solved through the fact that the macro expander will rename the defined alpha
identifier as per hygiene, meaning we would not be shadowing the alpha
which we are matching as a literal, so matching it is fine, and the above will simply create a binding for a renamed alpha
that's unreachable outside the macro body.
Appendix B
There is a constraint in the end of section 5.3. of R5RS (p. 17), the end of section 5.4. of R7RS (p. 26), and the middle of section 10. in R6RS (p. 30), which mention that a sequence of definitions shall not contain a definition which changes the meaning of any of them. (It's actually a little more complicated, all three standards using different wording, but that should be a reasonable summary.)
In your example, it is not clear to me whether the possibility of your syntax-rules
expanding to a syntax error counts as an ambiguation of its "meaning." If one considers it an ambiguation, then your beta
and gamma
are "errors" (undefined behavior) as per R5RS and R7RS, and a "syntax violation" as per R6RS.
If your example contained another binding in the second branch of your syntax-rules
(ideally a definition for the same variable even), then this nitpick would not apply, so your question stands.