guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Unwind-protect


From: Maxime Devos
Subject: RE: Unwind-protect
Date: Mon, 28 Oct 2024 18:20:11 +0100

>Unwind-protect and dynamic-wind are not the same thing. See
>http://www.nhplace.com/kent/PFAQ/unwind-protect-vs-continuations-original.html
>
>      - Tommi

Err, that web page is simply incorrect on this matter, unless Lisp and Scheme 
are assigning different meanings to the same words. (If they do mean something 
different, you’ll need to tell what semantics you want the unwind-protect 
equivalent to have.)
Let’s look at the semantics of unwind-protect: 
https://lisp-docs.github.io/cl-language-reference/chap-5/f-d-dictionary/unwind-protect_special-operator:

>unwind-protect evaluates protected-form and guarantees that cleanup-forms are 
>executed before unwind-protect exits, whether it terminates normally or is 
>aborted by a control transfer of some kind. 
>[…]
>unwind-protect protects against *all* attempts to exit from protected-form, 
>including go, handler-case, ignore-errors, restart-case, return-from, throw, 
>and with-simple-restart.

(emphasis added)

Now compare it to the web page:

>Sometimes when I cite this problem, people tell me that dynamic-wind is the 
>solution. It is not. dynamic-wind exists to solve a completely different 
>problem. The purpose of dynamic-wind is to taken action on *every* entry and 
>every exit to a dynamic contour for effects that can be undone and redone. For 
>example, dynamic-wind can be used to implement what Common Lisp calls special 
>variables (i.e., "dynamic" variables).

These descriptions of dynamic-wind and unwind-protect describe the same 
sematics.

That said, the web page is correct that dynamic-wind often isn’t a proper 
solution. But likewise, neither would unwind-protect be a proper solution (in 
the presence of continuations).

Under the assumption that continuations are used as a form of userspace 
context-switching and that dynamic-wind is used for cleanup (and maybe 
acquisition if you want to) of resources (and this cleanup shouldn’t be caused 
by context switching), I do have a solution (LGPL3+, albeit perhaps with an 
argument for triviality):

https://github.com/wingo/fibers/blob/f92e5cb4f78e7e3d3537bfc9622bc59ea99fe9a7/fibers/scheduler.scm#L281

It’s a bit ad-hoc currently, but I imagine it would be possible to rename the 
currently-existing dynamic-wind to primitive-dynamic-wind and rename 
dynamic-wind


-- 
Kotisivu / Homepage: http://www.iki.fi/tohoyn/
Sähköposti / E-Mail: tommi.hoynalanmaa@iki.fi
GPG-sormenjälki / GPG fingerprint:
55F4 2477 7155 3528 5CB2 2B7A BB86 1FDE 4046 0F83
FT, Debian-ylläpitäjä / PhD, Debian Maintainer





reply via email to

[Prev in Thread] Current Thread [Next in Thread]