unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Maxime Devos <maximedevos@telenet.be>
To: Frank Terbeck <ft@bewatermyfriend.org>, guile-devel@gnu.org
Subject: Re: Recursive Macros generating Definitions
Date: Mon, 3 Oct 2022 14:48:39 +0200	[thread overview]
Message-ID: <939c81ae-5b12-1910-5fbd-8ad99a697da6@telenet.be> (raw)
In-Reply-To: <87zgedkuqc.fsf@ft.bewatermyfriend.org>


[-- Attachment #1.1.1: Type: text/plain, Size: 2314 bytes --]



On 03-10-2022 13:32, Frank Terbeck wrote:
> When looking at  this, I also saw the following,  which might be related
> if ‘syntax-rules’ is implemented using  ‘syntax-case’

It is, IIRC.

> (I didn't check if
> this is the case):
> 
>      (define-syntax-rule (foobar n) (define quux n))
>      ,exp (foobar 23)
>    → (define quux-ea7bdcf8675f4a4 23)

This is correct (as in, functioning as intended and not a bug) to my 
understanding -- in the match expression of 'foobar', 'quux' does not 
appear, so the for hygiene, the 'quux' inside shouldn't be the quux outside.

Compare:

(define-syntax-rule (define-pair-contents pair the-car the-cdr)
   (begin
     (define p pair) ; only compute it once.  Due to lexical hygiene, 
this won't interfere with any 'p' in the environment.
     (define the-car (car pair))
     (define the-cdr (cdr pair)))).

-- this shouldn't be expanded to

(define p pair)
(define the-car (car p))
(define the-cdr (cdr p))

because of hygiene (the environment might already be using 'p' for 
something else).

It's sometimes a bit inconvenient -- sometimes you _want_ to define 
'quux' (and not just only available to the macro), but that's easily 
resolved by adding an additional 'quux' argument to 'foobar':

       (define-syntax-rule (foobar quux n) (define quux n))
       ,exp (foobar quux 23)

 > (define-syntax generate-shorthands [...]

Your recursive macro is, well, recursive.  This is fine, but IIUC a 
consequence of this is that the recursive 'call' to generate-shorthands 
is a new lexical lexical environment (hence, hygience, so -?????? stuff).

As such, I consider this not a bug in Guile, but a bug in your code.

My proposal would be to change the 'x' in (datum->syntax x) -- instead 
of using #'x (which refers to the whole expression, which in a recursive 
call has an undesired lexical environment), use something of the 
'end-user' of generate-shorthands, say, #'s (i.e., SEMANTICS-SYMBOL) 
(for the right lexical environment).

If I make that change, I get some reasonable output (no -????? suffixes):

$1 = (begin
   (define (varint:sint32-decode bv)
     (varint-decode bv 32 zig-zag))
   (define (varint:sint32-encode n)
     (varint-encode n 32 zig-zag)))

Greetings,
Maxime.

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 929 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

  reply	other threads:[~2022-10-03 12:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-03 11:32 Recursive Macros generating Definitions Frank Terbeck
2022-10-03 12:48 ` Maxime Devos [this message]
2022-10-03 13:41   ` Frank Terbeck
2022-10-03 18:42     ` Jean Abou Samra
2022-10-03 20:29       ` Frank Terbeck

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=939c81ae-5b12-1910-5fbd-8ad99a697da6@telenet.be \
    --to=maximedevos@telenet.be \
    --cc=ft@bewatermyfriend.org \
    --cc=guile-devel@gnu.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).