guile-user
[Top][All Lists]
Advanced

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

Way to control the order of macro expansion


From: Panicz Maciej Godek
Subject: Way to control the order of macro expansion
Date: Wed, 1 Jan 2014 23:46:57 +0100

Hi,
is there any way to control the order of macro expansion?
Let's consider a particular problem.
I'm trying to write a macro to control the visibility
of certain definitions. I wrote it once, using define-macro,
but it seemed to loose some relevant information,
so I decided to rewrite it using define-syntax macros.

The idea is that the code
(publish
  (define (f x) (+ a x))
  (define (g y) (* a y))
 where
  (define a 5))

should expand to

(begin
  (define f (and (defined? 'f) f))
  (define g (and (defined? 'g) g))
  (let ()
    (define a 5)
    (set! f (let () (define (f x) (+ a x)) f))
    (set! g (let () (define (g x) (* a y)) g))))

The first thing that needs to be done is to split the
definitions between the public (before "where") and
private ones.

It can be done by introducing a helper macro
that will be called by our publish macro:

(define-syntax-rule (publish definitions ...)
  (publisher (definitions ...) ()))

(define-syntax publisher
  (syntax-rules (where)
    ((_ (where private ...) (public ...))
     (private+public (private ...) (public ...)))
    ((_ (new defs ...) (approved ...))
     (publisher (defs ...)
                (approved ... new)))))

It works fine, but a simple question arises:
how to make publisher visible only within the
scope of publish macro? (I have tried many
combinations, and all of them failed, although
I don't know why)

Now, we need the definition for the private+public
syntax, that will do the actual conversion.

I believe it should look more or less like this:

(define-syntax-rule (private+public (private ...) ((df iface body ...) ...))
  (letrec-syntax
      ((name
        (syntax-rules ()
          ((_ (head . tail))
           (name head))
          ((_ atom)
           atom))))
    (begin
      (define (name iface) (and (defined? '(name iface)) iface))
      ...
      (let ()
        private ...
        (set! (name iface) (let () (df iface body ...) (name iface)))
        ...))))

Unfortunately, there are some problems with that definition,
the most annoying being the fact that it doesn't work ;]

There are three problems, namely -- that quote, define and
set! are special forms themselves, and as such they prevent
the "name" local syntax to expand. (This can be observed
easily, because when the names "define" and "set!" and
the ' operator are replaced with some unbound identifiers,
the macro expands just as I want it to).

It seems that the expander works in the order from the
outermost to the innermost level, and from left to right.

Is there any way to force the expander to evaluate
some innermost forms first?

regards,
M.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]