From: Panicz Maciej Godek <godek.maciek@gmail.com>
To: "guile-user@gnu.org" <guile-user@gnu.org>
Subject: Way to control the order of macro expansion
Date: Wed, 1 Jan 2014 23:46:57 +0100 [thread overview]
Message-ID: <CAMFYt2Z1K=CO32D_hv2m7zUh7EXxeqx0bRjuq00O6psbxSP9rQ@mail.gmail.com> (raw)
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.
next reply other threads:[~2014-01-01 22:46 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-01 22:46 Panicz Maciej Godek [this message]
2014-01-02 14:30 ` Way to control the order of macro expansion Panicz Maciej Godek
2014-01-02 16:17 ` Chris Vine
2014-01-03 0:21 ` Panicz Maciej Godek
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='CAMFYt2Z1K=CO32D_hv2m7zUh7EXxeqx0bRjuq00O6psbxSP9rQ@mail.gmail.com' \
--to=godek.maciek@gmail.com \
--cc=guile-user@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).