From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mathieu Lirzin Newsgroups: gmane.lisp.guile.bugs Subject: bug#30237: Generalizing =?UTF-8?Q?=E2=80=98and=3D>=E2=80=99?= Date: Wed, 24 Jan 2018 21:08:25 +0100 Message-ID: <87y3kn10li.fsf@gnu.org> References: <877es731ar.fsf@gnu.org> <877es7ti5j.fsf@netris.org> 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 1516824439 15035 195.159.176.226 (24 Jan 2018 20:07:19 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 24 Jan 2018 20:07:19 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) Cc: 30237@debbugs.gnu.org To: Mark H Weaver Original-X-From: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Wed Jan 24 21:07:15 2018 Return-path: Envelope-to: guile-bugs@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 1eeRKA-0003K7-Lv for guile-bugs@m.gmane.org; Wed, 24 Jan 2018 21:07:10 +0100 Original-Received: from localhost ([::1]:42796 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eeRMB-0002wg-5t for guile-bugs@m.gmane.org; Wed, 24 Jan 2018 15:09:15 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:35069) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eeRM0-0002t7-C1 for bug-guile@gnu.org; Wed, 24 Jan 2018 15:09:05 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eeRLx-0000q3-Vf for bug-guile@gnu.org; Wed, 24 Jan 2018 15:09:03 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:33441) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eeRLx-0000pv-RB for bug-guile@gnu.org; Wed, 24 Jan 2018 15:09:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1eeRLx-00019F-Le for bug-guile@gnu.org; Wed, 24 Jan 2018 15:09:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Mathieu Lirzin Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Wed, 24 Jan 2018 20:09:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 30237 X-GNU-PR-Package: guile X-GNU-PR-Keywords: Original-Received: via spool by 30237-submit@debbugs.gnu.org id=B30237.15168245194385 (code B ref 30237); Wed, 24 Jan 2018 20:09:01 +0000 Original-Received: (at 30237) by debbugs.gnu.org; 24 Jan 2018 20:08:39 +0000 Original-Received: from localhost ([127.0.0.1]:41338 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eeRLa-00018e-VS for submit@debbugs.gnu.org; Wed, 24 Jan 2018 15:08:39 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:44012) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eeRLY-00018R-RY for 30237@debbugs.gnu.org; Wed, 24 Jan 2018 15:08:37 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eeRLS-0000X5-36 for 30237@debbugs.gnu.org; Wed, 24 Jan 2018 15:08:31 -0500 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:54479) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eeRLR-0000X0-Uq; Wed, 24 Jan 2018 15:08:30 -0500 Original-Received: from [2a01:e35:2ec2:e580:7d5f:f616:fc6f:3970] (port=47266 helo=godel) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1eeRLR-0001MB-9l; Wed, 24 Jan 2018 15:08:29 -0500 In-Reply-To: <877es7ti5j.fsf@netris.org> (Mark H. Weaver's message of "Wed, 24 Jan 2018 10:01:44 -0500") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.org gmane.lisp.guile.bugs:8981 Archived-At: Hello Mark, Mark H Weaver writes: > Mathieu Lirzin writes: > >> Here is a proposal for generalizing =E2=80=98and=3D>=E2=80=99 to a pipel= ine of procedures. >> It acts like a =E2=80=9Cbind=E2=80=9D operator in an ad-hoc =E2=80=9CMay= be=E2=80=9D monad which uses #f >> to represent the absence of value. Not sure if it is useful in >> practice, but it feels like a natural generalization. >> >> The current definition is the following: >> >> (define (and=3D> value procedure) >> (and value (procedure value))) >> >> Here is my proposition: >> >> (define-syntax and=3D> >> (syntax-rules () >> ((_) #t) >> ((_ val) val) >> ((_ val proc) >> (and val (proc val))) >> ((_ val proc proc* ...) >> (and=3D> (and val (proc val)) proc* ...)))) > > Be careful, macros are different than procedures! Instead of binding > values to variables, they bind _unevaluated_ forms to pattern variables. > So 'val' is a misleading name for the first operand of the macro above. > In fact, what's being bound there is an _expression_, and you are then > expanding this into something that contains two copies of that > expression. So, for example: > > (and=3D> (compile-webkitgtk) > test > install) > > expands into: > > (and=3D> (and (compile-webkitgtk) > (test (compile-webkitgtk))) > install) > > which expands into: > > (and (and (compile-webkitgtk) > (test (compile-webkitgtk))) > (install (and (compile-webkitgtk) > (test (compile-webkitgtk))))) > > So you end up compiling webkitgtk 4 times, and testing it twice, before > finally installing it. More generally, if you pass N+1 operands, the > first operand will be evaluated 2^N times. Indeed I overlooked that. > The other problem is that 'and=3D>' is currently a procedure, and you're > replacing it with a macro. There are a couple of issues with this. One > is that procedures are first-class objects in Scheme, but macros aren't. > Procedures can be passed as arguments to procedures, stored in data > structures, etc, but macros cannot. > > For example, if 'and=3D>' is a procedure, you can write: > > (map and=3D> vals procs) > > but you can't do this if 'and=3D>' is a macro. > > The other problem with changing 'and=3D>' to a macro is that this > effectively changes the ABI of Guile, which we can't do within a stable > release series (2.2.x). Existing .go files that use 'and=3D>' were > compiled to generate a normal procedure call for 'and=3D>', and if that > procedure no longer exists then the code will break. This change > requires all users of 'and=3D>' to be recompiled. I initially implemented it as a procedure but move to the macro side of thing in an attempt to generate more efficient code while handling procedures that return multiple values. But this can't work given the issue you described above. >> Let me know if such change is welcome or not, so I can provide a >> complete patch including documentation. > > I think it's worth considering something along these lines for the > 'master' branch which will eventually become Guile 3, although in order > to support existing callers that expect 'and=3D>' to be a first-class > procedure, we might want to arrange for a bare 'and=3D>' to expand into a > reference to a first-class procedure that does the same job, similar to > what we do with 'define-inlinable'. I don't think making it a macro worths the breaking change. > The other option would be to implement your enhanced 'and=3D>' as a normal > procedure using case-lambda, maybe something like this: > > (define and=3D> > (case-lambda > ((val proc) (and val (proc val))) > ((val . procs) > (let loop ((val val) (procs procs)) > (if (null? procs) > val > (and val (loop ((car procs) val) > (cdr procs)))))) > (() #t))) > > The first case is not strictly needed, but is included as an > optimization to avoid heap-allocating a list for the 'procs' rest > argument in the common case of two arguments. > > The second case is implemented as a loop instead of a recursive call to > 'and=3D>', to prevent repeatedly heap-allocating the rest list on each > iteration, which would lead to O(N^2) allocations for N arguments. It > would be nicer to use 'match' here, but since 'and=3D>' is defined early > in boot-9.scm, we must restrict ourselves to core functionality in its > implementation. That's a clear explanation. IMO It would be nice if multiple values were handled too. here is one solution inspired by =E2=80=98compose=E2=80= =99. (define and=3D> (case-lambda ((val proc) (and val (proc val))) ((val proc . procs) (let loop ((p proc) (ps procs)) (if (null? ps) (p val) (loop (lambda args (call-with-values (lambda () (apply p args)) (lambda (arg0 . args*) (and arg0 (apply (car ps) arg0 args*))))) (cdr ps))))))) I am not sure if there is a way to avoid constructing the composition of functions. > I'm not sure if it makes sense to include the final (nullary) case. > Unlike 'and', 'or', '+', '*' and similar operations where every argument > is treated uniformally, in this case the first argument is qualitatively > different than the others. Therefore, it seems to me that this > procedure naturally generalizes down to 1 argument, but no further. I don't think so, I defined it only because I took inspiration from =E2=80=98and=E2=80=99. > Finally, there still some question in my mind whether this > generalization would be useful in practice. Have you found a real-world > use case where this generalized 'and=3D>' makes life easier? Not really. :-) > Do you know about SRFI-2 (and-let*)? How would that work for your use > case? Yes I know about =E2=80=98and-let*=E2=80=99 which is a proper way to do pip= elining while checking for #f. My motivation for extending =E2=80=98and=3D>=E2=80=99 was= to have something similar but without the special syntax. Thanks for the in-depth review. --=20 Mathieu Lirzin GPG: F2A3 8D7E EB2B 6640 5761 070D 0ADE E100 9460 4D37