[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hygiene when using multiple instances of a macro..?



On Mon, 8 Aug 2005, Panu wrote:

I'll try to write a macro definition to show the situation I was talking
about:

(define-syntax (can-we-stand-duplicates a-macro)
 (quasisyntax
   (if ,a-macro
     (let ((x 3)) (,a-macro #f))
     x)))

(define-syntax (test)
 (quasisyntax (can-we-stand-duplicates can-we-stand-duplicates)))

(test)

Thank you for the example, but shouldn't that be instead:

  (define-syntax (can-we-stand-duplicates a-macro)
    (quasisyntax
     (if ',a-macro                     ; note quote
         (let ((x 3)) (,a-macro #f))
         x)))

  (define-syntax (test)
    (quasisyntax (can-we-stand-duplicates can-we-stand-duplicates)))

  (test)    ;==> reference to unidentified identifier: x#top

... if it works wrong, it expands to (something that evaluates to) 3.
If it works right, it expands to something that has an unbound
identifier.
The reason I suspected the wrong behavior is that in some rewrite-based
systems, the fact the both x's are created in the same context (here, in
the same quasisyntax) suffices to make them identical, even though they
should not be identical across different invocations of the macro.

We do get the right behaviour, since the two x's are created in different /evaluations/ of the quasisyntax form, which suffices to make them different:

  (expand (syntax (test))

 ==> (if 'can-we-stand-duplicates
         ((lambda (@x8165) (if '#f
                               ((lambda (@x8168) (#f #f)) 3)
                               x#top))
          3)
         x#top)

Cheers
Andre