From: Andreas Rottmann <a.rottmann@gmx.at>
To: Josef Wolf <jw@raven.inka.de>
Cc: guile-user@gnu.org
Subject: Re: Need help to understand a macro
Date: Mon, 22 Mar 2010 22:50:25 +0100 [thread overview]
Message-ID: <87iq8ojaam.fsf@gmx.at> (raw)
In-Reply-To: <20100322195518.GD31143@raven.wolf.lan> (Josef Wolf's message of "Mon, 22 Mar 2010 20:55:18 +0100")
Josef Wolf <jw@raven.inka.de> writes:
> On Fri, Mar 19, 2010 at 05:15:57PM +0100, Andreas Rottmann wrote:
>> defmacros are bound to bite you in the proverbial ass real hard when you
>> combine them with modules, so it's best to avoid them like the
>> plague. At least that's my experience, FWIW.
>
> Thanks for the warning. I'd like to understand why this is dangerous, though.
> Do you have an example for me?
>
Yes, consider the following two modules:
;;---------------8<----------------
(define-module (test a)
#:export-syntax (test-a)
#:use-module (srfi srfi-34)
#:use-module (srfi srfi-35))
(define (funny-condition? c)
(and (message-condition? c)
(string-prefix? "FUNNY: " (condition-message c))))
(define-syntax test-a
(syntax-rules ()
((test-a expr)
(guard (c ((funny-condition? c) 'funny))
expr))))
(define-module (test b)
#:use-module (srfi srfi-34)
#:use-module (srfi srfi-35)
#:use-module (test a))
(test-a
(raise (condition (&message (message (string-append "FUNNY: "))))))
;;---------------8<----------------
Now running it causes this:
WARNING: (test b): imported module (srfi srfi-34) overrides core binding `raise'
Throw to key `unbound-variable':
ERROR: In procedure module-lookup:
ERROR: Unbound variable: funny-condition?
Entering the debugger. Type `bt' for a backtrace or `c' to continue.
Now why doesn't this work, and return 'funny in module B's body? It's
because currently, the `guard' macro from SRFI-34 is implemented with
`define-macro', and it hence throws away (among other things) the
information what `funny-condition?' refers to when being expanded.
The fully expanded code of the `test-a' form now contains a free
reference to `funny-condition?', while it should have a bound reference
referring to the `funny-condition?' defined in module (test a).
As you can see, the use of a single defmacro can poison your otherwise
perfectly fine hygienic, referentially transparent, syntax-rules macros.
> Do common-lisp macros suffer from the same problems or is this a scheme
> specific problem?
>
I've just superficial knowledge of CL, but AFAIK, Common Lisp macros
still have similiar issues, but they are (largely) mitigated by the fact
that CL has a function namespace separate from the variable namespace.
> Please note that I am very new to lisp, so please don't beat me if I ask
> dumb questions :-)
>
They were actually very reasonable questions!
Regards, Rotty
--
Andreas Rottmann -- <http://rotty.yi.org/>
next prev parent reply other threads:[~2010-03-22 21:50 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-03-19 8:57 Need help to understand a macro Josef Wolf
2010-03-19 12:54 ` Ken Raeburn
2010-03-22 19:25 ` Josef Wolf
2010-03-22 20:16 ` Andy Wingo
2010-03-22 20:54 ` Josef Wolf
2010-03-19 14:28 ` Andy Wingo
2010-03-19 16:15 ` Andreas Rottmann
2010-03-22 19:55 ` Josef Wolf
2010-03-22 21:50 ` Andreas Rottmann [this message]
2010-03-22 19:50 ` Josef Wolf
2010-03-22 20:36 ` Andy Wingo
2010-03-22 21:01 ` Josef Wolf
2010-03-23 0:50 ` Neil Jerram
2010-03-20 14:13 ` szgyg
2010-03-22 21:06 ` Josef Wolf
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=87iq8ojaam.fsf@gmx.at \
--to=a.rottmann@gmx.at \
--cc=guile-user@gnu.org \
--cc=jw@raven.inka.de \
/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).