defmacro/define-macro using syntax-case 
Author Message
 defmacro/define-macro using syntax-case

Is it possible to implement defmacro and define-macro using the
syntax-case macro expansion system?

I came up with the following:

(define-syntax defmacro
   (lambda (x)
     (syntax-case x ()
       ((_ name args . body)
        (syntax
        (define-syntax name
          (lambda (y)
            (datum->syntax-object
             (syntax name)
             (apply (lambda args . body)
                    (cdr (syntax-object->datum y)))))))))))

(defmacro define-macro (bindings . body)
   (if (pair? bindings)

       (let ((rest (gensym)))

;;the latter definition I found in Oleg's ssax

The tests I have done on this seem to indicate that the above is doing
"the right thing". I haven't conducted any complex tests though.

Has anyone else tried doing this?

matthias



Sat, 10 Jul 2004 17:01:09 GMT  
 defmacro/define-macro using syntax-case

Quote:

> The tests I have done on this seem to indicate that the above is doing
> "the right thing". I haven't conducted any complex tests though.

Try this:

(define-macro (foo) 'bar)
(define bar 'a)

Now what does (foo) return? What does (let ((bar 'b)) (foo))?

The problem is in (syntax name) that you use to define the scope of the
generated syntax object. You should instead use the syntax object you
got as an argument (ie. y in your code), not a local syntax object. It
makes a difference in scoping. Without the change, your version is still
too hygienical. :)

FWIW here's my version:

  (define-syntax (define-macro stx)
    (syntax-case stx ()
      ((_ (macro . args) . body)
       (syntax (define-macro macro (lambda args . body))))
      ((_ macro transformer)
       (syntax
        (define-syntax (macro stx2)
          (let ((v (syntax-object->datum stx2)))
            (datum->syntax-object stx2 (apply transformer (cdr v)))))))))

Lauri Alanko



Mon, 12 Jul 2004 05:02:17 GMT  
 defmacro/define-macro using syntax-case

Quote:

> Try this:

> (define-macro (foo) 'bar)
> (define bar 'a)

> Now what does (foo) return? What does (let ((bar 'b)) (foo))?

> The problem is in (syntax name) that you use to define the scope of the
> generated syntax object. You should instead use the syntax object you
> got as an argument (ie. y in your code), not a local syntax object. It
> makes a difference in scoping. Without the change, your version is still
> too hygienical. :)

Thanks for pointing that out. I have never used define-macro so wasn't
quite sure how dirty it should be :)

I had actually tried using "y" as the scope but the version of
datum->syntax-object I'm using won't accept that. See below for a
workaround.

Quote:
> FWIW here's my version:

>   (define-syntax (define-macro stx)
>     (syntax-case stx ()
>       ((_ (macro . args) . body)
>        (syntax (define-macro macro (lambda args . body))))
>       ((_ macro transformer)
>        (syntax
>         (define-syntax (macro stx2)
>           (let ((v (syntax-object->datum stx2)))
>             (datum->syntax-object stx2 (apply transformer (cdr v)))))))))

The version of syntax-case I'm using doesn't accept the extended
parameter syntax, i.e. it only accepts (define-syntax name ...) and not
(define-syntax (name obj) ...). There is also the problem I mentioned
above. The following version works around both and seems to be both
properly dirty and referentially non-transparent:

(define-syntax define-macro
  (lambda (x)
    (syntax-case x ()
      ((_ (name . args) . body)
       (syntax (define-macro name (lambda args . body))))
      ((_ name transformer)
       (syntax
        (define-syntax name
          (lambda (y)
            (syntax-case y ()
               ((_ . args)
                (datum->syntax-object
                 (syntax _)
                 (apply transformer (syntax-object->datum
                                     (syntax args)))))))))))))

;;TESTS;;
(define bar 1)
(define-macro (foo) 'bar)
(foo) ;=> 1
(let ((bar 2)) (foo)) ;=> 2
(define-macro (baz x) `(let ((bar 2)) (list bar ,x)))
(baz 1) ;=> (2 1)
(baz bar) ;=> (2 2)

Matthias



Tue, 13 Jul 2004 03:05:05 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Macros defining macros with define-syntax

2. define-macro -> define-syntax

3. define-macro vs define-syntax

4. is define-syntax inside define-syntax OK?

5. R4RS Macros possible using something like syntax-lambda ?

6. R4RS Macros implemented in defmacro (or for Elk)?

7. How to write seemingly unhygienic macros using syntax-rules

8. Scheme macro source: define/keyed, for defining keyword-triggered arguments

9. Question about a macro-defining macro

10. Macro-Defining Macros

11. help sought for macro defining macro

12. Load-time problem with a macro that defines a macro

 

 
Powered by phpBB® Forum Software