libraries and syntax-rules

142 views
Skip to first unread message

Jun Sheng

unread,
May 15, 2017, 12:27:52 PM5/15/17
to chez-scheme
Hi, 
when I experimenting with chez's library and syntax-rules, I found this library can't be imported:
;; $ cat foo/bar.ss 
(library (foo bar)
  (export bar-stx)
  (import  (chezscheme))
  (define-syntax bar-stx
    (syntax-rules (@)
      ((_ @ e1 e2 ...)
       (list e1 e2 ...)))))
;; end of library

When I import the library from  REPL:
$ scheme
Chez Scheme Version 9.4.1
Copyright 1984-2016 Cisco Systems, Inc.

> (import (foo bar))
Exception in read: @ symbol syntax is not allowed in #!r6rs mode at line 5, char 20 of foo/bar.ss
Type (debug) to enter the debugger.

But if I type in the syntax definition in REPL, 
it just works:
> (define-syntax bar-stx
      (syntax-rules (@)
        ((_ @ e1 e2 ...)
         (list e1 e2 ...))))
> (bar-stx @ 1 2 3)
(1 2 3)
> (bar-stx 1 2 3)
Exception: invalid syntax (bar-stx 1 2 3)
Type (debug) to enter the debugger.

I am confused by the different behavior between REPL and library importing.

Further  more, 
I changed the library code as following:
;;
(library (foo bar2)
  (export bar-stx
 lst)
  (import  (rnrs))
  (define lst)
  (define-syntax bar-stx
    (syntax-rules (lst)
      ((_ lst e1 e2 ...)
       (list e1 e2 ...)))))
;;end of code.
I found that I must define the syntax literal `lst' as well as export `lst' to get the macro work.

I am not sure whether these two problems above are bugs or just desired.

Andy Keep

unread,
May 15, 2017, 1:06:07 PM5/15/17
to chez-scheme, Jun Sheng
Hey Jun,

The reader on the REPL is running in Chez Scheme mode, which is more permissive for the syntax of identifiers, while the an R6RS library is processed in r6rs reader mode, which is more restrictive (as you've noticed).  It is worth noting that the REPL uses a different mode, because the R6RS does not define a REPL or how it should operate.

You can change the operation of the reader by using the #!r6rs and #!chezscheme reader modifiers.  So, at the top of you library file you can add #!chezscheme on it's own line to indicate that the reader should shift to chezscheme mode:

#!chezscheme
(library (foo bar)
  ---)

You need this to be an expression all by itself so that it will be evaluated before the reader begins working on the next expression in the file.

You can also change the handling of case by using #!fold-case to indicate the language should be case-insensitive (as Scheme was prior to R6RS) or #!no-fold-case (the default) for turn it off.

This is described in Section 1.1 of the Chez Scheme User's Guide: http://cisco.github.io/ChezScheme/csug9.4/intro.html#./intro:h1

As far as the need to export an auxiliary keyword to use it from the REPL, this is again an R6RS-ism, which requires these identifiers are free-identifier=?, because identifiers are handled slightly differently in the REPL and the library they do not compare to be the same (effectively, they are not both unbound in the syntactic environment).  This is why if you want the macro to work the same on the REPL, you need to define an export the identifier.  I usually do something like:

(define-syntax lst (lambda (x) (syntax-violation x "misplaced aux keyword")))

-andy:)
--
You received this message because you are subscribed to the Google Groups "chez-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chez-scheme...@googlegroups.com.
To post to this group, send email to chez-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chez-scheme/CAAYzEVDDfOJCX6Cv7LZt_3DQCtTyfdj4ODt%3Dm7hoaimZV0vx8A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Jun Sheng

unread,
May 15, 2017, 10:07:35 PM5/15/17
to Andy Keep, chez-scheme
Hey Andy, 
Many thanks to you.
Your answer is so helpful.


Reply all
Reply to author
Forward
0 new messages