peroxide icon indicating copy to clipboard operation
peroxide copied to clipboard

Discussion about syntax-case

Open mnieper opened this issue 2 years ago • 3 comments

I would like to address some inaccuracies in the discussion on macro systems in Scheme: https://github.com/MattX/peroxide/blob/master/doc/macros.md, so let me give a number of facts:

  • syntax-case is hygienic by default; however, using datum->syntax can be used to break hygiene explicitly. In particular, one cannot write an unhygienic macro by accident. (Compare with ER macros, where one has to be very careful to maintain hygiene.
  • That no one cared about R6RS is utterly wrong. People still care about R6RS because it was not succeeded by R7RS, which is to be seen as an independent successor of R5RS.
  • R7RS did not supersede R6RS.
  • syntax-case is no more complex to implement than syntactic closures, for example.
  • syntax-case will be part of the large language of R7RS.
  • It is not possible to implement syntax-case on top of syntactic closures. I wrote Chibi's implementation but that implementation is not fully compliant (it cannot be because of the limitations of syntactic closures).
  • It is not possible to write unhygienic macros that work in all contexts with ER macros.
  • Unhygienic macros written with syntactic closures are incompatible with hygienic syntax-rules macros.
  • Syntactic closures are strictly less powerful than syntax-case; for example, the unhygienic define-record-type of SRFI 99 cannot be expressed with syntactic closures.

The upshot is that from the set of discussed macro systems, only syntax-case turns out to be a viable choice.

mnieper avatar Apr 16 '23 09:04 mnieper

PS The low-level macro system found in the appendix of R4RS, which was cited, is basically the syntax-case system but without the high-level syntax-case pattern matcher.

mnieper avatar Apr 16 '23 09:04 mnieper

Thanks for the comments! This doc was mostly meant as quick research notes while trying to figure out what to do and probably shouldn't have stayed on the repo as-is. I turned these notes into a hopefully more accurate and less hyperbolic summary at https://terbium.io/2020/05/macros-scheme/ (which, funnily enough, mentions you when talking about syntactic closure limitations). I will replace the discussion in this repo with the version on my website.

I didn't actually take a deep look at the low-level R4RS macro system at the time, and didn't realize it was related to syntax-case -- that might be a good thing to add to the post.

MattX avatar Apr 28 '23 02:04 MattX

Thanks for pointing out your blog post! It is always great to see people promoting Scheme! If you allow me two more comments: At the end, you cite my Chibi patch to prove that syntax-case can be implemented on top of modified ER macros. Unfortunately, this is not entirely true. My implementation is just an approximation to syntax-case. One of the features of syntax-case is that it is fully lexically scoped (as one would expect in Scheme). For example, the following just works:

(library (lib)
  (export foo bar)
  (import (rnrs))
  (define bar 1)
  (define foo #'bar))

(import (rnrs) (lib))
(define-syntax quux
  (lambda (x)
    foo))
quux ; => 1

Here, the syntax object to which #'bar evaluates picks up the lexical environment of where it appears, that is, the top-level environment of the helper library, which includes a binding for bar. It would not work if the environment of define-syntax were picked up.

This cannot be emulated with an ER or SC system (this also means that they are less suited for writing large hygienic macros, whereby large, I mean a macro whose transformer picks up pieces from different libraries or helper procedures).

It would greatly improve peroxide if it were built on a native implementation of syntax-case. The system's core is no more complicated than syntactic closures (see [1] for a leisure introduction to syntax-case and how it is implemented). I will happily answer any questions.

The second comment is about the "excellent message by Alex Shinn", whom I otherwise greatly respect for his contributions to Scheme. It is obvious that he didn't fully understand syntax-case at the time when he wrote that post (which was long ago). Some things in this post are just plain false (like the claim of "implicit unhygienic interaction between syntax-case and syntax).

-- [1] https://legacy.cs.indiana.edu/~dyb/pubs/bc-syntax-case.pdf

mnieper avatar Apr 28 '23 06:04 mnieper