unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: "Dr. Arne Babenhauserheide" <arne_bab@web.de>
To: Walter Lewis <wklew@mailbox.org>
Cc: guile-user@gnu.org
Subject: Re: Breaking hygiene with syntax-rules?
Date: Fri, 11 Aug 2023 07:07:17 +0200	[thread overview]
Message-ID: <87bkfe16xn.fsf@web.de> (raw)
In-Reply-To: <dada39cb-05ad-0770-be80-87597b215d16@mailbox.org>

[-- Attachment #1: Type: text/plain, Size: 2960 bytes --]


Walter Lewis via General Guile related discussions <guile-user@gnu.org> writes:

> (define-syntax unhygienic
>   (syntax-rules ()
>     ((_ the-pair fetch)
>      (begin
>        (define the-head (car the-pair))
>        (define (fetch) the-head)))))
>
> (unhygienic '(1) fetch1)
> (unhygienic '(2) fetch2)
>
> (fetch1)
>
> ;; => 2

I just tested this and see the same problem:

(unhygienic '(a) name1)
(unhygienic '(b) name2)
(name1) ;; => b
(name2) ;; => b

So it’s reproduced. Thank you for the minimal example!

> It seems that the second call to unhygienic shadows the previous
> definition of the-head. I expected syntax-rules to generate fresh
> names for it each time.
>
> Is this expected, and if so, is there any way to generate an internal
> definition using syntax-rules? For my case, I was writing a macro to
> generate SRFI-9 record definitions, but I noticed some of the getters
> and setters which I intended to be private were shadowing each other.

I did not expect this.

To track this:

(define-syntax unhygienic
  (syntax-rules ()
    ((_ the-pair fetch)
     (begin
       (define the-head (car the-pair))
       (define (the-proc) the-head)
       (define (fetch) the-head)
       (display the-proc)))))

(display the-head)
;; => error Unbound variable: the-head (step out of the debugger with C-d)
(unhygienic '(a) name1)
;; => #<procedure the-proc-1d2f8a1cb6ff0af6 ()> (the proc has a long suffix, looks good)
(display the-head)
;; => error: Unbound variable: the-head (looks good?)
(name1) ;; => a
(unhygienic '(b) name2)
;; #<procedure the-proc-1d2f8a1cb6ff0af6 ()> (the suffix of the proc is the same as for the one in a?)
(name1) ;; => b

Maybe this is just, because the procedure is *the same*?
Let’s use fetch in the-proc:

(define-syntax unhygienic
  (syntax-rules ()
    ((_ the-pair fetch)
     (begin
       (define the-head (car the-pair))
       (define (the-proc) fetch)   
       (define (fetch) the-head)
       (display the-proc)))))
(unhygienic '(a) name1)
;; => #<procedure the-proc-7f18b8db60e30e7 ()>
(unhygienic '(b) name2)
;; => #<procedure the-proc-ca383a2334aaa56 ()>
(name1)
;; => b


Now the proc is no longer the same, but the variable still collides.

Auto-completion in the REPL
the-head-<TAB>
;; => the-head-3f6c11022fffe02

the-head also has a suffix, as it should have, but there is only one.

Does the counter go wrong?

One more check:

(define-syntax unhygienic
  (syntax-rules ()
    ((_ the-pair fetch)
     (begin
       (define the-head fetch)
       (define (the-proc) fetch)   
       (define (fetch) the-head)
       (display the-proc)))))

Now using fetch in the-head I get two different variables in the shell.

Maybe detection of external data is missing that the-pair is external?

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
draketo.de

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

  reply	other threads:[~2023-08-11  5:07 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-11  2:35 Breaking hygiene with syntax-rules? Walter Lewis via General Guile related discussions
2023-08-11  5:07 ` Dr. Arne Babenhauserheide [this message]
2023-08-11  8:00 ` Jean Abou Samra
2023-08-11  8:33   ` Dr. Arne Babenhauserheide
2023-08-11 23:57     ` Jean Abou Samra
2023-08-12  5:57       ` Dr. Arne Babenhauserheide
2023-08-11 13:34   ` Walter Lewis via General Guile related discussions
2023-08-11 22:50     ` Walter Lewis via General Guile related discussions
2023-08-11 23:55       ` Jean Abou Samra
2023-08-11 23:57         ` Jean Abou Samra
2023-08-12  0:19           ` Walter Lewis
2023-08-12  0:58             ` Jean Abou Samra
2023-08-12  5:59             ` Dr. Arne Babenhauserheide
2023-08-14 19:26               ` Thompson, David

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bkfe16xn.fsf@web.de \
    --to=arne_bab@web.de \
    --cc=guile-user@gnu.org \
    --cc=wklew@mailbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).