[Top][All Lists]

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

bug#30237: Generalizing ‘and=>’

From: Mark H Weaver
Subject: bug#30237: Generalizing ‘and=>’
Date: Wed, 24 Jan 2018 10:01:44 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)

Hi Mathieu,

Mathieu Lirzin <address@hidden> writes:

> Here is a proposal for generalizing ‘and=>’ to a pipeline of procedures.
> It acts like a “bind” operator in an ad-hoc “Maybe” 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=> value procedure)
>     (and value (procedure value)))
> Here is my proposition:
>   (define-syntax and=>
>     (syntax-rules ()
>       ((_) #t)
>       ((_ val) val)
>       ((_ val proc)
>        (and val (proc val)))
>       ((_ val proc proc* ...)
>        (and=> (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=> (compile-webkitgtk)

expands into:

  (and=> (and (compile-webkitgtk)
              (test (compile-webkitgtk)))

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.

The other problem is that 'and=>' 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=>' is a procedure, you can write:

  (map and=> vals procs)

but you can't do this if 'and=>' is a macro.

The other problem with changing 'and=>' 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=>' were
compiled to generate a normal procedure call for 'and=>', and if that
procedure no longer exists then the code will break.  This change
requires all users of 'and=>' to be recompiled.

> 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=>' to be a first-class
procedure, we might want to arrange for a bare 'and=>' to expand into a
reference to a first-class procedure that does the same job, similar to
what we do with 'define-inlinable'.

The other option would be to implement your enhanced 'and=>' as a normal
procedure using case-lambda, maybe something like this:

  (define and=>
      ((val proc) (and val (proc val)))
      ((val . procs)
       (let loop ((val val) (procs procs))
         (if (null? procs)
             (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=>', 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=>' is defined early
in boot-9.scm, we must restrict ourselves to core functionality in its

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.

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=>' makes life easier?

Do you know about SRFI-2 (and-let*)?  How would that work for your use


reply via email to

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