From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.user Subject: Re: procedure-source availability Date: Mon, 08 Oct 2012 13:57:27 -0400 Message-ID: <87mwzwn8xk.fsf@tines.lan> References: <506910C0.7060108@netris.org> <87y5jp9de5.fsf@gnu.org> <87txuakouv.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1349719069 12354 80.91.229.3 (8 Oct 2012 17:57:49 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 8 Oct 2012 17:57:49 +0000 (UTC) Cc: Ludovic =?utf-8?Q?Court=C3=A8s?= , guile-user@gnu.org To: Panicz Maciej Godek Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Oct 08 19:57:55 2012 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TLHaU-0002RX-Jt for guile-user@m.gmane.org; Mon, 08 Oct 2012 19:57:54 +0200 Original-Received: from localhost ([::1]:39310 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TLHaO-0000Aw-Ij for guile-user@m.gmane.org; Mon, 08 Oct 2012 13:57:48 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:41504) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TLHaJ-0000Ah-Ue for guile-user@gnu.org; Mon, 08 Oct 2012 13:57:45 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TLHaI-0006MY-Nl for guile-user@gnu.org; Mon, 08 Oct 2012 13:57:43 -0400 Original-Received: from world.peace.net ([96.39.62.75]:48265) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TLHaI-0006MM-Ir; Mon, 08 Oct 2012 13:57:42 -0400 Original-Received: from 209-6-91-212.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com ([209.6.91.212] helo=tines.lan) by world.peace.net with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1TLHaB-00030c-1q; Mon, 08 Oct 2012 13:57:35 -0400 In-Reply-To: (Panicz Maciej Godek's message of "Sun, 7 Oct 2012 02:07:20 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 96.39.62.75 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:9613 Archived-At: Panicz Maciej Godek 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