From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: taylanbayirli@gmail.com (Taylan Ulrich B.) Newsgroups: gmane.lisp.guile.user Subject: Re: Exceptions that pass continuations Date: Fri, 19 Jul 2013 16:12:40 +0200 Message-ID: <871u6uabaf.fsf@taylan.uni.cx> References: <8738ra3drr.fsf@zigzag.favinet> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1374243179 31396 80.91.229.3 (19 Jul 2013 14:12:59 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 19 Jul 2013 14:12:59 +0000 (UTC) Cc: "guile-user@gnu.org" To: Panicz Maciej Godek Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Jul 19 16:13:01 2013 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 1V0BQa-0002D8-RL for guile-user@m.gmane.org; Fri, 19 Jul 2013 16:13:00 +0200 Original-Received: from localhost ([::1]:37557 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V0BQa-0001Pw-8z for guile-user@m.gmane.org; Fri, 19 Jul 2013 10:13:00 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37248) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V0BQN-0001IY-90 for guile-user@gnu.org; Fri, 19 Jul 2013 10:12:50 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V0BQK-000216-Ah for guile-user@gnu.org; Fri, 19 Jul 2013 10:12:47 -0400 Original-Received: from mail-ea0-x229.google.com ([2a00:1450:4013:c01::229]:43520) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V0BQK-00020o-44 for guile-user@gnu.org; Fri, 19 Jul 2013 10:12:44 -0400 Original-Received: by mail-ea0-f169.google.com with SMTP id h15so2435140eak.0 for ; Fri, 19 Jul 2013 07:12:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=u7lQQMNvOLmahRXQwCHngl6wlXBqonDdPxCOVJeyClQ=; b=TL2THRVEChbBlnMqZu3MbHysvktrSk5F6dEVBk214sRgk7LGBFv0j/xOBn4Owiev/6 bsFTgqpvckmGlAOp6kArBfmtb5dDw34tnF03ywJobbuPwDXyl2RAvc9CR8YeQKgNpgcD Du/zd4xpBGAZxQXvgvqxpoBAbxUW6+HQQThNJLlPKMG6qUr8HHNYKwE9Us7PcJBAgv/C PHpdsQwOVs3l+QaoVTP/SuQNlUyfWTQKq2BcPHh6W8sliFY1Uee2n6GpW3RapPPpIMY8 CscYsTpn//3LbWtDU1p7Q2+NsPi2xUBt1kPg/mO8w+4fjtK8Rxf3Cx5cyfpTDtsXL/Kg OXEw== X-Received: by 10.14.7.2 with SMTP id 2mr16156569eeo.145.1374243162882; Fri, 19 Jul 2013 07:12:42 -0700 (PDT) Original-Received: from taylan.uni.cx (p4FD92ED9.dip0.t-ipconnect.de. [79.217.46.217]) by mx.google.com with ESMTPSA id ci50sm27697324eeb.12.2013.07.19.07.12.41 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 19 Jul 2013 07:12:42 -0700 (PDT) In-Reply-To: <8738ra3drr.fsf@zigzag.favinet> (Thien-Thi Nguyen's message of "Fri, 19 Jul 2013 15:00:56 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (berkeley-unix) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4013:c01::229 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:10558 Archived-At: I agree with Thien on that all resource-management should be delegated to the garbage collector via smob types. However, I found this supply-demand pattern quite neat (could have other uses perhaps), was kind of bored, with a little free time on my hands, and enjoy an occasional exercise in delimited continuations, so here's a cleaned-up version of the code using prompts. :D But see the note at the end. (define prompt-tag (make-prompt-tag "supply")) (define-syntax supply (syntax-rules () ((_ (((action-name . action-args) abody abody* ...) ...) body body* ...) (let ((actions (alist->hash-table `((,action-name . ,(lambda action-args abody abody* ...)) ...)))) (call-with-prompt prompt-tag (lambda () body body* ...) (lambda (cont demanded-action . args) (let ((action (hash-table-ref/default actions demanded-action #f))) (if action (cont (apply action args)) (apply throw 'demand-not-supplied demanded-action args))))))))) (define (demand action-name . args) (apply abort-to-prompt prompt-tag action-name args)) ;;; It composes neatly: (supply ((('foo . bar) bar)) (demand 'foo 0 1 2)) ;=> (0 1 2) Now there's something funny to notice here. I was uncomfortable forcing the action-names to be literals, because then one couldn't rename them and thus different modules would get name-clashes; so I don't automatically quote the action-name, and would expect modules to export the "names" of the actions they demand as variables holding unique objects (e.g. a uniquely allocated cons cell), so those variables can be renamed and the action they refer to is still unique. And at that point, we pretty much re-implemented parameters! In a much worse way than their Guile-native implementation of course, and forcing their values to be procedures. Long story short, use parameters for this pattern. :) Taylan