[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Recursive Macros generating Definitions
From: |
Maxime Devos |
Subject: |
Re: Recursive Macros generating Definitions |
Date: |
Mon, 3 Oct 2022 14:48:39 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 |
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.
OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key
OpenPGP_signature
Description: OpenPGP digital signature