From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Chris Vine Newsgroups: gmane.lisp.guile.user Subject: Re: dynamic-wind Date: Sun, 2 Jul 2017 12:58:31 +0100 Message-ID: <20170702125831.192ddaec@bother.homenet> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: blaine.gmane.org 1498996742 9341 195.159.176.226 (2 Jul 2017 11:59:02 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 2 Jul 2017 11:59:02 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sun Jul 02 13:58:58 2017 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dRdWk-0002DT-LA for guile-user@m.gmane.org; Sun, 02 Jul 2017 13:58:58 +0200 Original-Received: from localhost ([::1]:57612 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRdWp-0005m0-UG for guile-user@m.gmane.org; Sun, 02 Jul 2017 07:59:03 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40196) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRdWR-0005iK-DS for guile-user@gnu.org; Sun, 02 Jul 2017 07:58:40 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dRdWO-0006WE-Cd for guile-user@gnu.org; Sun, 02 Jul 2017 07:58:39 -0400 Original-Received: from mail-wr0-x234.google.com ([2a00:1450:400c:c0c::234]:34718) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dRdWO-0006VF-5w for guile-user@gnu.org; Sun, 02 Jul 2017 07:58:36 -0400 Original-Received: by mail-wr0-x234.google.com with SMTP id 77so222263540wrb.1 for ; Sun, 02 Jul 2017 04:58:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=0ixVC5LWRr8bUxMS4BjHEKYfwxXwNASBTe//+5WTktw=; b=oF/CroND9rHBZZfp3riSDKfUjkYMd4U5bYzlWXS0XEXIfnW3hBX24xqgmX0Ojiwc12 9lYSj8p4+Rm89fvqhcm7bpgoXRDnMrh621LpgishdoIE31gq2Dfvmj1hk2QOnjTCNUWf 4CkU3EicoJyT5mpsN5z1fewDP8tEfCuc8ovzRCmys2Y/TZVjQhD77CwuX6BBsBm3tfFS LCrcofm+3HvrmWx95pCZUt0W29F/cL1gdnGvwLT8DV5caoqx2Zyava19UxnPleJO4rbj vS+nwijiRSfp9a3U5Esc51ZnFDDjsqdOt+A/W8zwBQWjbBt6MdXhH4rDGcprpe11YORK tQMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0ixVC5LWRr8bUxMS4BjHEKYfwxXwNASBTe//+5WTktw=; b=kPwWW/ivbosQ6+4dfFO3LV5gy7PxK+2d6nwNEOICi7rzfI9COpjgrSI9DhylP/q2vE UIvMyzE3XV5OgqkIW/6/RpHW7+yJu5DG+fJX/o/bXixvdGTupp1kOtO3DQGmN55IVnLo NR/toXPl09/++tNU+z4OhXMAvZFg8oDCDbdeCOjl7c2EqKG7P3udHgLYO0RRTuo/Fg+3 8K+kcDoueg18xxqm3ijgzkACGVjyH4eW+6COVVJ1XLD3lF226tKpxOCTjQlZhBWZuTXP U0ot964mNDzgmz5ATBpO8K95Tm+oJ/rQLwuSOKQSYxMc6D4Cg7U686rR6eF+ypk9QO4o /ngg== X-Gm-Message-State: AKS2vOzm9fGWi6RvkseHPPkPPy7Hn56hK7OEOlSNMA7lQ1OkCFBYNzVF conOzgF4gTGb0Lif X-Received: by 10.223.157.4 with SMTP id k4mr32933750wre.43.1498996713154; Sun, 02 Jul 2017 04:58:33 -0700 (PDT) Original-Received: from bother.homenet ([95.146.111.187]) by smtp.gmail.com with ESMTPSA id 63sm11912709wmi.8.2017.07.02.04.58.32 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 02 Jul 2017 04:58:32 -0700 (PDT) Original-Received: from bother.homenet (localhost [IPv6:::1]) by bother.homenet (Postfix) with ESMTP id 9F40E2612D7 for ; Sun, 2 Jul 2017 12:58:31 +0100 (BST) In-Reply-To: X-Mailer: Claws Mail 3.14.1 (GTK+ 2.24.31; x86_64-unknown-linux-gnu) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::234 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 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" Xref: news.gmane.org gmane.lisp.guile.user:13894 Archived-At: On Sun, 2 Jul 2017 08:00:58 +0200 Catonano wrote: > 2017-06-30 23:48 GMT+02:00 Panicz Maciej Godek > : > > It's very simple (at least from the point of view of a user) > > When it is tempting to write something like > > > > (define (within-context action) > > (enter-context) > > (action) > > (leave-context)) > > > > you simply change it to > > > > (define (within-context action) > > (dynamic-wind > > (lambda () (enter-context)) > > action > > (lambda () (leave-context)))) > > > > The thing is, that in general (action) may transfer control outside > > of the scope of that particular context (like, using call/cc or > > exceptions) -- and in such situations, we would like the > > (leave-context) handler to be invoked. If and we ever get back > > there, we wish that the (enter-context) were invoked again. [snip] > > Here's a simple real life example in Scheme: > > > > (define current-working-directory getcwd)(define change-directory > > chdir) > > > > (define (with-changed-working-directory dir thunk) (let ((cwd > > (current-working-directory))) (dynamic-wind (lambda () > > (change-directory dir)) thunk > > (lambda () (change-directory cwd))))) > > aahh I see now > > Well. the manual is not clear enough, I think, in this regard [snip] > I think that the use case should be explicitly stated, in the manual, > as you did here > > Also the example should be about some db or network connection, not > about call/cc > > So that understanding dynamic-wind and its use case wouldn t require > understanding call/cc (which I still don t get) Possibly the wording of the manual about dynamic-wind at https://www.gnu.org/software/guile/manual/html_node/Dynamic-Wind.html#Dynamic-Wind could be improved. Stating that "If, any time during the execution of thunk, the dynamic extent of the dynamic-wind expression is escaped non-locally, out_guard is called" is a somewhat incomplete description of when the out guard is called, since it is also invoked when the body of the dynamic wind returns normally. If you think the wording could be improved I suggest you file a bug with a suggested wording. Having said that, I think that you are getting unnecessarily tied up with call/cc rather than concentrating on the principle. dynamic-wind is much more general than just for dealing with database or network connections, which I think would be a poor focus for the manual. dynamic-wind is relevant whenever control leaves the dynamic wind block in question, either normally or by an exception or by invocation of a specific continuation object such as via call/cc, call/ec or abort-to-prompt. Many languages have something similar - it is analogous in some sense to a C# or Java finally block, which executes whenever control leaves the associated try block, whether normally or by virtue of an exception or a return, break, continue or goto statement. The somewhat more unusual feature of dynamic-wind it that, because the scheme language permits a dynamic context to be re-entered by the invocation of a continuation object (say, as obtained by call/cc), there is also an in guard made available to deal with entry into the dynamic wind block. Having said all that, dynamic-wind is not the answer to all cases where control leaves a block of code non-locally. It is best suited to cases where invocation of a continuation object or the raising of an exception fall to be dealt with in the same way, say by releasing a resource such as by unlocking a mutex or closing a port. But that is by no means always the case - the invocation of a continuation object is usually a deliberate programmatic strategy, whereas the raising of an exception is usually not so and instead represents an unexpected failure event of some kind in the program. Chris