From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Panicz Maciej Godek Newsgroups: gmane.lisp.guile.user Subject: Re: procedure-source availability Date: Mon, 8 Oct 2012 22:11:59 +0200 Message-ID: References: <506910C0.7060108@netris.org> <87y5jp9de5.fsf@gnu.org> <87txuakouv.fsf@gnu.org> <878vbi3tp1.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1349727130 19746 80.91.229.3 (8 Oct 2012 20:12:10 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 8 Oct 2012 20:12:10 +0000 (UTC) Cc: guile-user@gnu.org To: =?ISO-8859-1?Q?Ludovic_Court=E8s?= Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Oct 08 22:12:17 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 1TLJgV-0002AK-SM for guile-user@m.gmane.org; Mon, 08 Oct 2012 22:12:16 +0200 Original-Received: from localhost ([::1]:60345 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TLJgP-0004fg-JQ for guile-user@m.gmane.org; Mon, 08 Oct 2012 16:12:09 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:56952) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TLJgK-0004fZ-L2 for guile-user@gnu.org; Mon, 08 Oct 2012 16:12:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TLJgJ-00049K-Gq for guile-user@gnu.org; Mon, 08 Oct 2012 16:12:04 -0400 Original-Received: from mail-lb0-f169.google.com ([209.85.217.169]:50629) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TLJgJ-00048x-9W; Mon, 08 Oct 2012 16:12:03 -0400 Original-Received: by mail-lb0-f169.google.com with SMTP id k6so3438749lbo.0 for ; Mon, 08 Oct 2012 13:12:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=9KdWB0ICp3E5742raguu2ZDbOEq39bh5R6HYV456/Yg=; b=j+pNkxcuTlVA0psjk0yQqsvRc2Cesu/6ctKs7xrrV593O7BqjNSAnNZJ9KBKCv76e/ iW3mHf+e/dN8+JwRH2A3L1UvaRupxVSX6KUxIHA0yTrNkijdUM4Okkj+qhxUfqVs1sZk sFajeXnu+Khf3DPzqYi8JMDvwBcc3+O9CB1Jlow2MXGI9xiDs3tPg60UitBZm++b0E3v 4DDl0wOpBJRmfi4Os81zq06g7idvkA3EokViS8p2nOvp/RD72gze4wC/M3u5Nu/wq5mp dUtj93U2D0NtsyIC4u+OMAYanX/A7j9y2bGq/m+8gWfxJVL0zo3BEmXNxeAQFZWT0jTr av/A== Original-Received: by 10.112.99.106 with SMTP id ep10mr7270421lbb.49.1349727120020; Mon, 08 Oct 2012 13:12:00 -0700 (PDT) Original-Received: by 10.152.20.3 with HTTP; Mon, 8 Oct 2012 13:11:59 -0700 (PDT) In-Reply-To: <878vbi3tp1.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.217.169 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:9614 Archived-At: 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-He= lpers.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=92t want to sound too dogmatic, but I think you really don=92t want= to > take that route. ;-) > > The issues with =91local-eval=92 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.