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, 9 Jul 2017 15:49:28 +0100 Message-ID: <20170709154928.170385a5@bother.homenet> References: <20170709135921.0f4890ad@bother.homenet> <59623915.6070104@sapo.pt> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1499611811 5485 195.159.176.226 (9 Jul 2017 14:50:11 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 9 Jul 2017 14:50:11 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sun Jul 09 16:50:06 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 1dUDX4-0000lU-L1 for guile-user@m.gmane.org; Sun, 09 Jul 2017 16:49:58 +0200 Original-Received: from localhost ([::1]:36394 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dUDXA-0005za-7H for guile-user@m.gmane.org; Sun, 09 Jul 2017 10:50:04 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47705) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dUDWi-0005y4-9G for guile-user@gnu.org; Sun, 09 Jul 2017 10:49:37 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dUDWe-0005x7-Bs for guile-user@gnu.org; Sun, 09 Jul 2017 10:49:36 -0400 Original-Received: from mail-wr0-x244.google.com ([2a00:1450:400c:c0c::244]:35638) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dUDWe-0005wh-4k for guile-user@gnu.org; Sun, 09 Jul 2017 10:49:32 -0400 Original-Received: by mail-wr0-x244.google.com with SMTP id z45so19230747wrb.2 for ; Sun, 09 Jul 2017 07:49:31 -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=VmbS+SQXXXAccpoyv+Py9zwB2YRY2OFmn9uEW841LnA=; b=SvLwec02tGGQoS9CG7VG4r/wxbwN6OBi+L6DYSAfl6QTL5i5qLHDHmpFH2Ttg2xTsU ARHy6Hh4HP2rJCbabS870yA1FaKpyBuZj9S35tH+PLEemrbiZVNY82re13/RcBu6OzYc xQ9XgabWXHnKKUwww6JkmgA7zekH1OxTiPf2knM/K9w7ohZhY2ZLQ1vDIgoB0Y6YUoVe kiBggXwmSgU513kAT7S+6/fHe1xbyfiR2awFWBSIury5Ti7roUPf/jLnTGxrjYM7L2Xr 9zPch5iNr7d2GalxXK7a3CCeyZMHyfK1eOuHcQi8CBmvBOTaf13xYsSpgPRpJyQh+k+o rGww== 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=VmbS+SQXXXAccpoyv+Py9zwB2YRY2OFmn9uEW841LnA=; b=tKPfGfymQ+wNrf2I2glcHiysMPhszuwXqF6Gy2csE0bT8EMo2DRdw0U3mG+O8cO/4Y W3QbxLms9Pyrc/tN7x3Xo2nHf3CWGy0j1JEK2m3oaAtQvWoYPi5aiw63pg2N3k532L5U 9NzonkEm0KXeE4OHDKhk57UvOPeBMYM0jj4O/Pt0cZ7MgFzjkRCAdTKNqVxB5w+RrXwS 6th33T8T21NqeUMikZLjBbD7W1OLN+yrlAoTlR0n8aPrTDvsfIofUtfxGel8e7LvPo3V ssKmeZT7+/hi5+K/U5mXIA+BJ8sFhRZ/s1bIMwvifGCrQJUbdTUGRmml1NLm+h+boRPL ezDw== X-Gm-Message-State: AIVw112DLjYJrqbNpiaCVHr8NcDnUuJaIGYFsQ7pe5Ez+FA+VnI2NV4R ahgS8JZCQaMt5Zln X-Received: by 10.28.229.207 with SMTP id c198mr4799540wmh.108.1499611770718; Sun, 09 Jul 2017 07:49:30 -0700 (PDT) Original-Received: from bother.homenet ([95.146.108.69]) by smtp.gmail.com with ESMTPSA id t62sm3850147wmb.24.2017.07.09.07.49.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 09 Jul 2017 07:49:29 -0700 (PDT) Original-Received: from bother.homenet (localhost [IPv6:::1]) by bother.homenet (Postfix) with ESMTP id 1E4A1260401 for ; Sun, 9 Jul 2017 15:49:28 +0100 (BST) In-Reply-To: <59623915.6070104@sapo.pt> 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::244 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:13912 Archived-At: On Sun, 9 Jul 2017 11:09:25 -0300 V=C3=ADtor De Ara=C3=BAjo wrote: > On 09/07/2017 09:59, Chris Vine wrote: > > On Sun, 09 Jul 2017 00:34:13 +0300 > > Marko Rauhamaa writes: =20 > >> Hm. Python's try/finally has several uses in virtually every > >> program. > >> > >> Trouble is, Scheme's continuations make it impossible to know when > >> something is really final. =20 > >> > >> In fact, implementing coroutines and cooperative multitasking using > >> continuations almost guarantee a repeated back-and-forth through > >> dynamic-wind. > >> > >> I strongly suspect Scheme's continuations are more trouble than > >> they are worth. =20 > >=20 > > I disagree with that. On the first point, you know that a > > dynamic-wind block can no longer be re-entered (if that is what you > > mean by "really final") when the continuation object concerned is > > no longer accessible. At that point it, and all references to > > other objects encapsulated by the continuation, will be released in > > the ordinary way. You also know the same when your continuation is > > only an escape continuation. =20 >=20 > That helps the implementation know if a continuation will not be > entered again, but it does not help when you want to do the kinds of > things you do with unwind-protect or try/finally in other languages. > For example, with unwind-protect, you can open a port or another > resource and ensure it will be closed if control escapes the > unwind-protect form. You can do that with dynamic-wind, but it is > less meaningful to do so because control can be re-entered again. > There is no language construct (as far as I know =E2=80=93 maybe there is= in > Guile) that can detect that flow has exited the form and *will never > enter it again*. So the presence of continuations make operations > like unwind-protect less meaningful. I don't know what is the Scheme > way to address these situations. unwind-protect is a dynamic-wind without the in guard. The scheme way of dealing with re-entry (which is not possible in common lisp) is to initialize in the in guard the resources to be released in the out guard. There are two problems: first it may be you cannot do that - you may need to initialize the resources elsewhere and it may not be meaningful anyway to re-initialize them - see below. Secondly there is no uncomplicated way of distinguishing between non-local exit on invocation of a continuation object, non-local exit on the throwing of an exception and a normal exit, but the required treatment may be different in each case. Obviously you can distinguish using flags and so forth, but it is a pain. There is a language construct in guile for knowing whether a continuation object is a full continuation or an escape continuation: call/cc produces full continuations and call/ec produces escape continuations (which cannot be re-entered). call-with-prompt produces re-enterable delimited continuations. As I said, dynamic-wind is a very blunt instrument. I find it useful only for very local resources of a particular kind. Releasing a mutex is a common case where dynamic-winds are useful (guile has the with-mutex form). Ports, as in your example, are not as clear cut. Sure, you may want to close the port when you exit a particular block in the case of a normal exit, an exception or an escape continuation, but do you necessarily want to do so if you might re-enter later and closing the port is a destructive operation (it vacates input buffers)?