From: Mark H Weaver <mhw@netris.org>
To: Panicz Maciej Godek <godek.maciek@gmail.com>
Cc: "Ludovic Courtès" <ludo@gnu.org>, guile-user@gnu.org
Subject: Re: procedure-source availability
Date: Mon, 08 Oct 2012 13:57:27 -0400 [thread overview]
Message-ID: <87mwzwn8xk.fsf@tines.lan> (raw)
In-Reply-To: <CAMFYt2bYd1cOJT0JACywBXxs4_VzUxN1M=B-MzS8AJ9kbsHEDQ@mail.gmail.com> (Panicz Maciej Godek's message of "Sun, 7 Oct 2012 02:07:20 +0200")
Panicz Maciej Godek <godek.maciek@gmail.com> writes:
> The strange thing was that I had to define the macro ``function''
> using define-macro -- the define-syntax counterpart for some reason
> wouldn't work. So for example, if I wrote
> (define f (let ((y 5)) (function x (set! y (apply + y x)) y))
> then if ``function'' was defined by means of define-syntax/syntax-rules, ie
>
> (define-syntax function
> (syntax-rules ()
> ((_ args body ...)
> (let ((environment (the-environment))
> (lexical-names (lexical-names))
> (procedure (lambda args body ...)))
> (set-procedure-property! procedure 'source '(function args body ...))
> (set-procedure-property! procedure 'environment environment)
> (set-procedure-property! procedure 'lexical-names lexical-names)
> procedure))))
>
> then the ``environment'' variable wouldn't capture the ``y'' (or
> anything else, for that matter).
(the-environment) captures the lexical environment where it is found,
and in this case that means the lexical environment of this macro
definition.
Although it is not documented, 'the-environment' accepts an optional
argument, which must be an identifier. If provided, the lexical
environment captured is the one where that identifier was created.
In theory, this means that this should do what you want:
--8<---------------cut here---------------start------------->8---
(define-syntax function
(syntax-rules ()
((function args body ...)
(let ((environment (the-environment function))
(lexical-names (map car (lexicals function)))
(procedure (lambda args body ...)))
(set-procedure-property! procedure 'source '(function args body ...))
(set-procedure-property! procedure 'environment environment)
(set-procedure-property! procedure 'lexical-names lexical-names)
procedure))))
--8<---------------cut here---------------end--------------->8---
Unfortunately, our definition of 'syntax-rules' needlessly discards the
keyword identifier. This should probably be fixed, but in the meantime
you can use 'syntax-case' instead:
--8<---------------cut here---------------start------------->8---
(define-syntax function
(lambda (x)
(syntax-case x ()
((function args body ...)
#'(let ((environment (the-environment function))
(lexical-names (map car (lexicals function)))
(procedure (lambda args body ...)))
(set-procedure-property! procedure 'source '(function args body ...))
(set-procedure-property! procedure 'environment environment)
(set-procedure-property! procedure 'lexical-names lexical-names)
procedure)))))
--8<---------------cut here---------------end--------------->8---
Having said all this, I agree with Ludovic that this is probably not a
good approach. 'the-environment' inhibits almost all optimizations in
the compiler.
Regards,
Mark
next prev parent reply other threads:[~2012-10-08 17:57 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-23 19:05 procedure-source availability Panicz Maciej Godek
2012-09-30 20:11 ` Panicz Maciej Godek
2012-10-01 3:40 ` Mark H Weaver
2012-10-01 16:31 ` Panicz Maciej Godek
2012-10-02 14:06 ` Ludovic Courtès
2012-10-02 19:29 ` Panicz Maciej Godek
2012-10-03 1:03 ` Daniel Hartwig
2012-10-03 16:27 ` Panicz Maciej Godek
2012-10-04 19:40 ` Ludovic Courtès
2012-10-07 0:07 ` Panicz Maciej Godek
2012-10-07 20:36 ` Ludovic Courtès
2012-10-08 20:11 ` Panicz Maciej Godek
2012-10-08 17:57 ` Mark H Weaver [this message]
2012-10-04 16:16 ` Ludovic Courtès
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=87mwzwn8xk.fsf@tines.lan \
--to=mhw@netris.org \
--cc=godek.maciek@gmail.com \
--cc=guile-user@gnu.org \
--cc=ludo@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).