Am So., 28. Jan. 2024 um 15:09 Uhr schrieb Jakub T. Jankiewicz <xxxxxx@onet.pl>:
Only Racket give an error when there are no define-syntax-parameter, the
other implementations mention in SRFI (Guile and Chibi) doesn't require it
to work. So I just leave it out. The code works the same if there
define-syntax-parameter.

If an implementation allows arbitrary keywords to be syntax-parameterized, referential transparency of macros is violated.  E.g., some macro, say `when` may expand into code involving `if` and would break if `if` were syntax-parameterized.

So what should be the output of there is define-syntax-parameter?

The following would be correct:

(define (print x . rest)
  (apply display x rest)
  (newline))

(define-syntax foo
  (syntax-rules ()
    ((_ body ...)
     (begin
       (define-syntax-parameter it (syntax-rules ()))
       (syntax-parameterize
        ((it (syntax-rules ()
               ((_) "hello world"))))
        (print it)
        body ...)))))

(let ((it 10))
  (foo (print it)))

The final expression would first expand into

(let ((it 10))
  (begin
    (define-syntax-parameter it* (syntax-rules ()))
    (syntax-parameterize
        ((it* (syntax-rules () ((_) "hello world"))))
      (print it*)
      (print it)))

where `it*` stands for the renamed `it`.  The expression `it*` in the line `(print it*)` is now a syntax error because `it*` is not valid syntax for the syntax-rules syntax transformer associated with `it*` (syntax-rules do not allow identifier syntax).  In other words, you get again undefined behaviour in R7RS.

PS Unless you have specific reasons to stick to R7RS, you will probably be more helped by switching to R6RS, which doesn't have all this undefined behaviour that is in R7RS and where experimenting with specific implementations doesn't tell you much about the semantics.


 

On Sun, 28 Jan 2024 13:31:27 +0100
Marc Nieper-Wißkirchen <xxxxxx@gmail.com> wrote:

> Your code is an error, so any behaviour is allowed (according to the
> R7RS).  The problem with your code is that `syntax-parameterize` can only
> be applied to identifiers that are bound by `define-syntax-parameter`.  In
> your code, the identifier `it` in the macro body (which becomes
> hygienically renamed to, say, `it*`) is unbound.
>
>
> Am So., 28. Jan. 2024 um 12:46 Uhr schrieb Jakub T. Jankiewicz - jcubic at
> onet.pl (via srfi-discuss list) <xxxxxx@srfi.schemers.org>:
>
> > Hi,
> >
> > What should be the output of this code:
> >
> > (define (print x . rest)
> >   (apply display x rest)
> >   (newline))
> >
> > (define-syntax foo
> >   (syntax-rules ()
> >     ((_ body ...)
> >      (begin
> >        (syntax-parameterize
> >         ((it (syntax-rules ()
> >                ((_) "hello world"))))
> >         (print it)
> >         body ...)))))
> >
> > (let ((it 10))
> >   (foo (print it)))
> >
> >
> > I only tested in two implementations Chibi and Guile.
> > Guile give an error but Chibi first print 10.
> >
> > My implementation prints 10 10.
> >
> > Jakub
> >
> > --
> > Jakub T. Jankiewicz, Senior Front-End Developer
> > https://jcubic.pl/me
> > https://lips.js.org
> > https://koduj.org
> > 

--
Jakub T. Jankiewicz, Senior Front-End Developer
https://jcubic.pl/me
https://lips.js.org
https://koduj.org