3

In Chibi and CHICKEN, the following syntax-rules expression evaluates to a procedure:

(syntax-rules () ((_) #f))

Is this just an artifact of how these particular implementations are written? The Scheme language specs do not seem to call out syntax-rules as being able to evaluate to a value.


Update

It seems this may depend upon the version of Scheme?

From the R6RS Spec:

Semantics: An instance of syntax-rules evaluates, at macro-expansion time, to a new macro transformer by specifying a sequence of hygienic rewrite rules. A use of a macro whose keyword is associated with a transformer specified by syntax-rules is matched against the patterns contained in the s, beginning with the leftmost . When a match is found, the macro use is transcribed hygienically according to the template. It is a syntax violation when no match is found.

From the R5RS Spec and the R7RS Spec:

Semantics: An instance of syntax-rules produces a new macro transformer by specifying a sequence of hygienic rewrite rules. A use of a macro whose keyword is associated with a transformer specified by syntax-rules is matched against the patterns contained in the syntax rules, beginning with the leftmost syntax rule. When a match is found, the macro use is transcribed hygienically according to the template.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • Where do you see the difference? In both specs, it evaluates to a transformer. – uselpa Jun 24 '14 at 18:15
  • @uselpa - I think you're right, though it's curious that r6rs explicitly states it evaluates whereas r5rs and r7rs are more ambiguous. – Justin Ethier Jun 24 '14 at 18:17
  • I see you changed the title (and a little bit the sense) of the question... in Scheme a procedure is a value, so yes, `syntax-rules` evaluates to a value, specifically a procedure. – uselpa Jun 24 '14 at 18:31
  • It never occurred to me that I can write `syntax-rules` that are not within a `define-syntax`. `((syntax-rules () ((_) #f)) #'(x))` actually produces a value in #!racket :) – Sylwester Jun 25 '14 at 23:25
  • @Sylwester - I'm not sure it's a good idea to write code that takes advantage of this, but I agree it's nice to know :) – Justin Ethier Jun 26 '14 at 02:42

1 Answers1

4

syntax-rules returns a transformer, which is a procedure used by the expander to transform a syntactic extension into another syntactic extension. See also here.

So no, it's not a special form (this has disappeared from your question in the meantime), and yes, it evaluates to a value, since Scheme has first-class procedures and therefore procedures are values.

This is Scheme standard behaviour and not an implementation detail of Chicken or Chibi.

uselpa
  • 18,732
  • 2
  • 34
  • 52
  • Furthermore, a syntax transformer function is simply... a function from syntax to syntax. Sure, _normally_ you `define-syntax` such functions to be used at compile time by the expander (i.e. for "macros"). However you _can_ define and use such functions at run time (if you're doing something where a syntax-object is a convenient way to represent something). So it totally makes sense that `syntax-rules` (and `syntax-case`, etc.) return a procedure value as, in the question's example. – Greg Hendershott Jul 10 '14 at 02:05