From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Maciek Godek" Newsgroups: gmane.lisp.guile.user Subject: Closure? Date: Fri, 11 Jul 2008 16:48:06 +0200 Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1215787708 14041 80.91.229.12 (11 Jul 2008 14:48:28 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 11 Jul 2008 14:48:28 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Jul 11 16:49:16 2008 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KHJvq-0002yA-Fx for guile-user@m.gmane.org; Fri, 11 Jul 2008 16:49:10 +0200 Original-Received: from localhost ([127.0.0.1]:42171 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KHJuy-0001It-PF for guile-user@m.gmane.org; Fri, 11 Jul 2008 10:48:16 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KHJus-0001IM-NF for guile-user@gnu.org; Fri, 11 Jul 2008 10:48:10 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KHJur-0001I0-9e for guile-user@gnu.org; Fri, 11 Jul 2008 10:48:10 -0400 Original-Received: from [199.232.76.173] (port=49123 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KHJur-0001Hx-6e for guile-user@gnu.org; Fri, 11 Jul 2008 10:48:09 -0400 Original-Received: from an-out-0708.google.com ([209.85.132.241]:52048) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KHJuq-0006Nm-Ra for guile-user@gnu.org; Fri, 11 Jul 2008 10:48:09 -0400 Original-Received: by an-out-0708.google.com with SMTP id c38so708471ana.84 for ; Fri, 11 Jul 2008 07:48:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=LBsm94p4seKDXQxgXBr2FTnEd+qsj9otf0oa1bXCkj4=; b=p0nz+PsBl/eFY8F7/eq2I3/46u1SQjAyO9FhOSWRqp8ynf9+VTu4Voyhrna/nxjQqQ eEZ8IuBaOS6H6l0efdA0wa7feJzCeWQBlFfO+6Q1pNdXoxX++2V77y1MUJU9+qLyUKT7 WDL9n/+7+zYtIYQx7HvhUyJkLNNvJH7VCP++0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=jEv0f8oa1wBu7NIc9bSVH1bv2wk/TT/9yfuKkHCzNMKWMKTfHw+tut+oKw2Wbm7tQr 1vyZ9EIE9tNn/g+DuT/AELpCwrFgoQmma9E8cWalo1xurZhVycvmz7BbY26v02raS19G TyYDGvtCQdF2kY/7IRKzwXLKqc3e8Ym3NAqdY= Original-Received: by 10.100.105.15 with SMTP id d15mr7640079anc.137.1215787686546; Fri, 11 Jul 2008 07:48:06 -0700 (PDT) Original-Received: by 10.100.44.13 with HTTP; Fri, 11 Jul 2008 07:48:06 -0700 (PDT) Content-Disposition: inline X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 2) X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:6639 Archived-At: Hi folks, I've been wondering if there's any way to recall (or get inside) an environment of a closure (= to directly access variables bound to a closure) And here's what I mean exactly: (define ++ (let ((c 0)) (lambda()(set! c (1+ c))c))) ; we now have a closure (procedure-environment ++) ;-> ((c . 0) #) (++) ;-> 1 ; Now I would like to be able to do the following: (with (procedure-environment ++) (set! c 20)) ; the c variable in closure ++ is set to 20 (define -- (with (procedure-environment ++) (lambda()(set! c (1- c)) c))) ; here we have a new function bound to the same closure (--) ;-> 19 Is there any way to achieve this in guile? (it is especially important for me to make it efficiently doable from C). I've tried some funny combinations of eval and procedure-environment, but they didn't work out. Additionaly, it would be nice to see the possibility of explicit definitions of environments, like: (define env (make-closure (a . 1)(b . 2)) (with env (define c 3)) so that we could define the aforementioned counter as: (define counter-env (make-closure (c . 0))) (define ++ (with counter-env (lambda()(set! c (1+ c))c))) and so forth. By the way, the let special form could then be defined using this make-environment function (define-macro (let bindings body) (with (make-closure* bindings) body)) (I've suffixed the name with asterisk as some transformations between the let bindings and make-closure bindings are required to make it work properly) Hope I'm not reinventing the wheel here; it would be nice to be able to use guile in such way, though (and to make sure that it is efficient from the C level as well). It would also make scheme oop more intuitive, as method names could be hidden inside a closure and so it would allow the following syntax: (with object (method args)) which corresponds to the famous dot syntax known from many oo-languages like c++ or python: object.method(args) Would it be difficult to implement such functionality in guile? (if there's none implemented yet -- I didn't find anything in the documentation) tia4i (thanks in advance for implementation :) Maciek Godek