From: Panicz Maciej Godek <godek.maciek@gmail.com>
To: "Ludovic Courtès" <ludo@gnu.org>
Cc: guile-user@gnu.org
Subject: Re: procedure-source availability
Date: Mon, 8 Oct 2012 22:11:59 +0200 [thread overview]
Message-ID: <CAMFYt2Z4B9nPBJ=D1=Sq+D=Pzvgbma4TP8Apd4vE41j6+W=RSw@mail.gmail.com> (raw)
In-Reply-To: <878vbi3tp1.fsf@gnu.org>
Hellu!
>> Yes, you're right.
>> I even went a little further with that and now I also capture lexical
>> environment:
>> (use-modules (system syntax) (ice-9 local-eval))
>>
>> (define-macro (function 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))
>>
>> (where ``lexical-names'' returns the car-s of ``lexicals'', as defined
>> at the bottom of
>> http://www.gnu.org/software/guile/manual/html_node/Syntax-Transformer-Helpers.html#Syntax-Transformer-Helpers
>> )
>>
>> So in addition to the source of the procedure, the lexical environment
>> can also be retrieved:
>>
>> (define (procedure-lexicals proc)
>> (map (lambda(symbol)
>> (cons symbol
>> (local-eval symbol (procedure-property proc 'environment))))
>> (procedure-property proc 'lexical-names)))
>
> I don’t want to sound too dogmatic, but I think you really don’t want to
> take that route. ;-)
>
> The issues with ‘local-eval’ have been discussed at length a year ago or
> so on guile-devel. Basically, the problem is that it plays badly with
> compilation, and there are usually nicer way to achieve what you want.
Fortunately, calling local-eval isn't strictly necessary, as long as
the exciting and powerful `lexicals' macro is allowed. Then, the
`function' macro can be defined easily as:
(define-syntax function
(lambda (x)
(syntax-case x ()
((function args body ...)
#'(let ((procedure (lambda args body ...))
(get-lexicals (lambda()(lexicals function))))
(set-procedure-property! procedure 'source '(function args body ...))
(set-procedure-property! procedure 'get-lexicals get-lexicals)
procedure)))))
(define (procedure-lexicals proc)
((procedure-property proc 'get-lexicals)))
I don't fully understand why it works, as syntax-case is still a
mystery to me, but I wrote it and it works perfectly, not to mention
that the expanded code is much cleaner now, without the-environment.
next prev parent reply other threads:[~2012-10-08 20:11 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 [this message]
2012-10-08 17:57 ` Mark H Weaver
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='CAMFYt2Z4B9nPBJ=D1=Sq+D=Pzvgbma4TP8Apd4vE41j6+W=RSw@mail.gmail.com' \
--to=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).