GNU bug report logs - #50614
[PATCH core-updates] build: utils: Add ‘optional’ macro.

Previous Next

Package: guix-patches;

Reported by: Xinglu Chen <public <at> yoctocell.xyz>

Date: Thu, 16 Sep 2021 07:12:02 UTC

Severity: normal

Tags: patch

To reply to this bug, email your comments to 50614 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Thu, 16 Sep 2021 07:12:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Xinglu Chen <public <at> yoctocell.xyz>:
New bug report received and forwarded. Copy sent to guix-patches <at> gnu.org. (Thu, 16 Sep 2021 07:12:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: guix-patches <at> gnu.org
Subject: [PATCH core-updates]
 build: utils: Add ‘optional’ macro.
Date: Thu, 16 Sep 2021 09:11:22 +0200
* guix/build/utils.scm (optional): New syntax
* tests/build-utils.scm ("optional: expr1 is non-#f", optional: expr1 is #f"):
  Test it.
* guix.texi (Build Utilities): Document it.
---
A common idiom I have seen is

  (if EXPR1
      EXPR2
      '())

with the ‘optional’ macro, one can just write

  (optional EXPR1
            EXPR2)

I am not sure if ‘optional’ is the best name (it was inspired by
‘lib.optional’ in Nixpkgs), feedback welcome!

 doc/guix.texi         | 35 +++++++++++++++++++++++++++++++++++
 guix/build/utils.scm  | 17 ++++++++++++++++-
 tests/build-utils.scm |  8 ++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 9a3e8ae12c..1bb9ddb397 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -8801,6 +8801,41 @@ in a build phase of the @code{wireguard-tools} package:
         `("PATH" ":" prefix ,(list coreutils))))))
 @end lisp
 
+@subsection Miscellaneous build utilities
+
+@cindex miscellaneous build utilities
+This section documents some miscellaneous utilities that are useful to
+have.
+
+@deffn {Scheme Syntax} optional @var{test} @var{consequent}
+Like @code{when} (@pxref{Conditionals,,, guile, GNU Guile Reference
+Manual}), but if @var{test} evaluates to false, return the empty list.
+This is replaces the following idiom:
+@end deffn
+
+@lisp
+(if @var{test}
+    @var{consequent}
+    '())
+@end lisp
+
+with this:
+
+@lisp
+(optional @var{test}
+          @var{consequent})
+@end lisp          
+
+It can be useful when certain targets require an additional configure
+flags, e.g.,
+
+@lisp
+(arguments
+ `(#:configure-flags (list "--localstatedir=/var"
+                           "--sysconfdir=/etc"
+                           ,@@(optional (hurd-target?) '("--with-courage"))))
+@end lisp
+
 @subsection Build Phases
 
 @cindex build phases
diff --git a/guix/build/utils.scm b/guix/build/utils.scm
index 3beb7da67a..ecf834461f 100644
--- a/guix/build/utils.scm
+++ b/guix/build/utils.scm
@@ -8,6 +8,7 @@
 ;;; Copyright © 2020 Efraim Flashner <efraim <at> flashner.co.il>
 ;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
 ;;; Copyright © 2021 Maxime Devos <maximedevos <at> telenet.be>
+;;; Copyright © 2021 Xinglu Chen <public <at> yoctocell.xyz>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -123,7 +124,9 @@
 
             make-desktop-entry-file
 
-            locale-category->string))
+            locale-category->string
+
+            optional))
 
 
 ;;;
@@ -1613,6 +1616,18 @@ returned."
              LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE
              LC_TIME)))
 
+
+;;;
+;;; Misc.
+;;;
+
+;; If EXPR1 evaluates to a non-#f value, return EXPR2.  Otherwise, return an
+;; empty list.
+(define-syntax optional
+  (syntax-rules ()
+    ((_ expr1 expr2)
+     (if expr1 expr2 '()))))
+
 ;;; Local Variables:
 ;;; eval: (put 'call-with-output-file/atomic 'scheme-indent-function 1)
 ;;; eval: (put 'call-with-ascii-input-file 'scheme-indent-function 1)
diff --git a/tests/build-utils.scm b/tests/build-utils.scm
index 6b131c0af8..44ad9bafc0 100644
--- a/tests/build-utils.scm
+++ b/tests/build-utils.scm
@@ -3,6 +3,7 @@
 ;;; Copyright © 2019 Ricardo Wurmus <rekado <at> elephly.net>
 ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
 ;;; Copyright © 2021 Maxime Devos <maximedevos <at> telenet.be>
+;;; Copyright © 2021 Xinglu Chen <public <at> yoctocell.xyz>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -287,5 +288,12 @@ print('hello world')"))
           ("guile/bin" . ,(dirname (which "guile"))))
         "guile"))))
 
+(test-equal "optional: expr1 is non-#f"
+  'bar
+  (optional 'foo 'bar))
+
+(test-equal "optional: expr1 is #f"
+  '()
+  (optional #f 'bar))
 
 (test-end)

base-commit: 22f7d4bce1e694b7ac38e62410d76a6d46d96c5d
-- 
2.33.0







Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Thu, 16 Sep 2021 16:23:03 GMT) Full text and rfc822 format available.

Message #8 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
To: Xinglu Chen <public <at> yoctocell.xyz>, 50614 <at> debbugs.gnu.org
Subject: Re: [PATCH core-updates] build: utils: Add
 ‘optional’ macro.
Date: Thu, 16 Sep 2021 18:21:54 +0200
Hi,

Am Donnerstag, den 16.09.2021, 09:11 +0200 schrieb Xinglu Chen:
> * guix/build/utils.scm (optional): New syntax
> * tests/build-utils.scm ("optional: expr1 is non-#f", optional: expr1
> is #f"):
>   Test it.
> * guix.texi (Build Utilities): Document it.
> ---
> A common idiom I have seen is
> 
>   (if EXPR1
>       EXPR2
>       '())
> 
> with the ‘optional’ macro, one can just write
> 
>   (optional EXPR1
>             EXPR2)
> 
> I am not sure if ‘optional’ is the best name (it was inspired by
> ‘lib.optional’ in Nixpkgs), feedback welcome!
> 
>  doc/guix.texi         | 35 +++++++++++++++++++++++++++++++++++
>  guix/build/utils.scm  | 17 ++++++++++++++++-
>  tests/build-utils.scm |  8 ++++++++
>  3 files changed, 59 insertions(+), 1 deletion(-)
> 
> diff --git a/doc/guix.texi b/doc/guix.texi
> index 9a3e8ae12c..1bb9ddb397 100644
> --- a/doc/guix.texi
> +++ b/doc/guix.texi
> @@ -8801,6 +8801,41 @@ in a build phase of the @code{wireguard-tools} 
> package:
>          `("PATH" ":" prefix ,(list coreutils))))))
>  @end lisp
>  
> +@subsection Miscellaneous build utilities
> +
> +@cindex miscellaneous build utilities
> +This section documents some miscellaneous utilities that are useful
> to +have.
> +
> +@deffn {Scheme Syntax} optional @var{test} @var{consequent}
> +Like @code{when} (@pxref{Conditionals,,, guile, GNU Guile Reference
> +Manual}), but if @var{test} evaluates to false, return the empty
> list.
> +This is replaces the following idiom:
> +@end deffn
> +
> +@lisp
> +(if @var{test}
> +    @var{consequent}
> +    '())
> +@end lisp
> +
> +with this:
> +
> +@lisp
> +(optional @var{test}
> +          @var{consequent})
> +@end lisp          
> +
> +It can be useful when certain targets require an additional
> configure
> +flags, e.g.,
> +
> +@lisp
> +(arguments
> + `(#:configure-flags (list "--localstatedir=/var"
> +                           "--sysconfdir=/etc"
> +                           ,@@(optional (hurd-target?) '("--with-
> courage"))))
> +@end lisp
> +
>  @subsection Build Phases
>  
>  @cindex build phases
> diff --git a/guix/build/utils.scm b/guix/build/utils.scm
> index 3beb7da67a..ecf834461f 100644
> --- a/guix/build/utils.scm
> +++ b/guix/build/utils.scm
> @@ -8,6 +8,7 @@
>  ;;; Copyright © 2020 Efraim Flashner <efraim <at> flashner.co.il>
>  ;;; Copyright © 2020, 2021 Maxim Cournoyer <
> maxim.cournoyer <at> gmail.com>
>  ;;; Copyright © 2021 Maxime Devos <maximedevos <at> telenet.be>
> +;;; Copyright © 2021 Xinglu Chen <public <at> yoctocell.xyz>
>  ;;;
>  ;;; This file is part of GNU Guix.
>  ;;;
> @@ -123,7 +124,9 @@
>  
>              make-desktop-entry-file
>  
> -            locale-category->string))
> +            locale-category->string
> +
> +            optional))
>  
>  
>  ;;;
> @@ -1613,6 +1616,18 @@ returned."
>               LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE
>               LC_TIME)))
>  
> +
> +;;;
> +;;; Misc.
> +;;;
> +
> +;; If EXPR1 evaluates to a non-#f value, return EXPR2.  Otherwise,
> return an
> +;; empty list.
> +(define-syntax optional
> +  (syntax-rules ()
> +    ((_ expr1 expr2)
> +     (if expr1 expr2 '()))))
> +
>  ;;; Local Variables:
>  ;;; eval: (put 'call-with-output-file/atomic 'scheme-indent-function 
> 1)
>  ;;; eval: (put 'call-with-ascii-input-file 'scheme-indent-function
> 1)
> diff --git a/tests/build-utils.scm b/tests/build-utils.scm
> index 6b131c0af8..44ad9bafc0 100644
> --- a/tests/build-utils.scm
> +++ b/tests/build-utils.scm
> @@ -3,6 +3,7 @@
>  ;;; Copyright © 2019 Ricardo Wurmus <rekado <at> elephly.net>
>  ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
>  ;;; Copyright © 2021 Maxime Devos <maximedevos <at> telenet.be>
> +;;; Copyright © 2021 Xinglu Chen <public <at> yoctocell.xyz>
>  ;;;
>  ;;; This file is part of GNU Guix.
>  ;;;
> @@ -287,5 +288,12 @@ print('hello world')"))
>            ("guile/bin" . ,(dirname (which "guile"))))
>          "guile"))))
>  
> +(test-equal "optional: expr1 is non-#f"
> +  'bar
> +  (optional 'foo 'bar))
> +
> +(test-equal "optional: expr1 is #f"
> +  '()
> +  (optional #f 'bar))
>  
>  (test-end)
> 
> base-commit: 22f7d4bce1e694b7ac38e62410d76a6d46d96c5d

Naming choice aside, I don't think I like the asymmetry behind having a
plain symbol on the one side vs. an empty list.  What about 
(list-if condition . rest), which evaluates to rest if condition is
true and otherwise nil?





Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Fri, 17 Sep 2021 00:38:02 GMT) Full text and rfc822 format available.

Message #11 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Sarah Morgensen <iskarian <at> mgsn.dev>
To: Xinglu Chen <public <at> yoctocell.xyz>
Cc: 50614 <at> debbugs.gnu.org
Subject: Re: [bug#50614] [PATCH core-updates] build: utils: Add
 ‘optional’ macro.
Date: Thu, 16 Sep 2021 17:37:24 -0700
Hi,

Xinglu Chen <public <at> yoctocell.xyz> writes:

> * guix/build/utils.scm (optional): New syntax
> * tests/build-utils.scm ("optional: expr1 is non-#f", optional: expr1 is #f"):
>   Test it.
> * guix.texi (Build Utilities): Document it.
> ---
> A common idiom I have seen is
>
>   (if EXPR1
>       EXPR2
>       '())
>
> with the ‘optional’ macro, one can just write
>
>   (optional EXPR1
>             EXPR2)
>
> I am not sure if ‘optional’ is the best name (it was inspired by
> ‘lib.optional’ in Nixpkgs), feedback welcome!

I like the idea!  But perhaps if we're getting rid of boilerplate, we
should go all the way and make it so that instead of

  (if EXPR1
      `(EXPR2 EXPR3)
      '())

we can write

  (optional EXPR1 EXPR2 EXPR3)

What do you think?

Also, maybe 'list-when' for the name?

> +@deffn {Scheme Syntax} optional @var{test} @var{consequent}
> +Like @code{when} (@pxref{Conditionals,,, guile, GNU Guile Reference
> +Manual}), but if @var{test} evaluates to false, return the empty list.
> +This is replaces the following idiom:
        ^ no "is"

> +@end deffn
> +
> +@lisp
> +(if @var{test}
> +    @var{consequent}
> +    '())
> +@end lisp
> +
> +with this:
> +
> +@lisp
> +(optional @var{test}
> +          @var{consequent})
> +@end lisp          
> +
> +It can be useful when certain targets require an additional configure
                                                 ^ no "an"

> +flags, e.g.,
> +
> +@lisp
> +(arguments
> + `(#:configure-flags (list "--localstatedir=/var"
> +                           "--sysconfdir=/etc"
> +                           ,@@(optional (hurd-target?) '("--with-courage"))))
                               ^ extra "@"?

--
Sarah




Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Fri, 17 Sep 2021 08:27:01 GMT) Full text and rfc822 format available.

Message #14 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 50614 <at> debbugs.gnu.org
Cc: Sarah Morgensen <iskarian <at> mgsn.dev>,
 Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: [PATCH] build: utils: Add ‘list-when’ macro.
Date: Fri, 17 Sep 2021 10:26:41 +0200
* guix/build/utils.scm (list-when): New syntax
* tests/build-utils.scm ("list-when: expr1 is non-#f", list-when: expr1 is #f"):
  Test it.
* guix.texi (Build Utilities): Document it.
---
Changes since v1:

* Rename ‘optional’ to ‘list-when’.

* Make

    (list-when test consequent ...)

  equivalent to

    (if tests
        (list consequent ...)
        '())

 doc/guix.texi         | 35 +++++++++++++++++++++++++++++++++++
 guix/build/utils.scm  | 17 ++++++++++++++++-
 tests/build-utils.scm |  8 ++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 9a3e8ae12c..3b469c04c9 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -8801,6 +8801,41 @@ in a build phase of the @code{wireguard-tools} package:
         `("PATH" ":" prefix ,(list coreutils))))))
 @end lisp
 
+@subsection Miscellaneous
+
+@cindex miscellaneous build utilities
+This section documents some miscellaneous utilities that are useful to
+have.
+
+@deffn {Scheme Syntax} list-when @var{test} @var{consequent} @dots{}
+Like @code{when} (@pxref{Conditionals,,, guile, GNU Guile Reference
+Manual}), but if @var{test} evaluates to true, return @code{(list
+@var{consequent} @dots{})}, and if @var{test} evaluates to false, return
+the empty list.  This is replaces the following idiom:
+@end deffn
+
+@lisp
+(if @var{test}
+    (list @var{consequent} @dots{})
+    '())
+@end lisp
+
+with this:
+
+@lisp
+(list-when @var{test} @var{consequent} @dots{})
+@end lisp
+
+It can be useful when certain targets require an additional configure
+flags, e.g.,
+
+@lisp
+(arguments
+ `(#:configure-flags (list "--localstatedir=/var"
+                           "--sysconfdir=/etc"
+                           ,@@(list-when (hurd-target?) "--with-courage"))))
+@end lisp
+
 @subsection Build Phases
 
 @cindex build phases
diff --git a/guix/build/utils.scm b/guix/build/utils.scm
index 3beb7da67a..d3fb207ee5 100644
--- a/guix/build/utils.scm
+++ b/guix/build/utils.scm
@@ -8,6 +8,7 @@
 ;;; Copyright © 2020 Efraim Flashner <efraim <at> flashner.co.il>
 ;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
 ;;; Copyright © 2021 Maxime Devos <maximedevos <at> telenet.be>
+;;; Copyright © 2021 Xinglu Chen <public <at> yoctocell.xyz>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -123,7 +124,9 @@
 
             make-desktop-entry-file
 
-            locale-category->string))
+            locale-category->string
+
+            list-when))
 
 
 ;;;
@@ -1613,6 +1616,18 @@ returned."
              LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE
              LC_TIME)))
 
+
+;;;
+;;; Misc.
+;;;
+
+;; If EXPR1 evaluates to a non-#f value, return '(EXPR2 ...).  Otherwise,
+;; return an empty list.
+(define-syntax list-when
+  (syntax-rules ()
+    ((_ expr1 expr2 ...)
+     (if expr1 (list expr2 ...) '()))))
+
 ;;; Local Variables:
 ;;; eval: (put 'call-with-output-file/atomic 'scheme-indent-function 1)
 ;;; eval: (put 'call-with-ascii-input-file 'scheme-indent-function 1)
diff --git a/tests/build-utils.scm b/tests/build-utils.scm
index 6b131c0af8..b558feb47d 100644
--- a/tests/build-utils.scm
+++ b/tests/build-utils.scm
@@ -3,6 +3,7 @@
 ;;; Copyright © 2019 Ricardo Wurmus <rekado <at> elephly.net>
 ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
 ;;; Copyright © 2021 Maxime Devos <maximedevos <at> telenet.be>
+;;; Copyright © 2021 Xinglu Chen <public <at> yoctocell.xyz>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -287,5 +288,12 @@ print('hello world')"))
           ("guile/bin" . ,(dirname (which "guile"))))
         "guile"))))
 
+(test-equal "list-when: expr1 is non-#f"
+  (list 3 'bar)
+  (list-when 'foo (+ 1 2) 'bar))
+
+(test-equal "list-when: expr1 is #f"
+  '()
+  (list-when #f (+ 2 3) 'bar))
 
 (test-end)

base-commit: 22f7d4bce1e694b7ac38e62410d76a6d46d96c5d
-- 
2.33.0







Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Fri, 17 Sep 2021 17:57:01 GMT) Full text and rfc822 format available.

Message #17 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
To: Xinglu Chen <public <at> yoctocell.xyz>, 50614 <at> debbugs.gnu.org
Cc: Sarah Morgensen <iskarian <at> mgsn.dev>
Subject: Re: [PATCH] build: utils: Add
 ‘list-when’ macro.
Date: Fri, 17 Sep 2021 19:55:52 +0200
Am Freitag, den 17.09.2021, 10:26 +0200 schrieb Xinglu Chen:
> * guix/build/utils.scm (list-when): New syntax
> * tests/build-utils.scm ("list-when: expr1 is non-#f", list-when:
> expr1 is #f"):
>   Test it.
> * guix.texi (Build Utilities): Document it.
> ---
> Changes since v1:
> 
> * Rename ‘optional’ to ‘list-when’.
> 
> * Make
> 
>     (list-when test consequent ...)
> 
>   equivalent to
> 
>     (if tests
>         (list consequent ...)
>         '())
LGTM, but let's wait on more opinions.  IIRC, changing (guix build)
causes a world rebuild, no?  So this patch might go to core-updates
first.





Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Fri, 17 Sep 2021 19:16:02 GMT) Full text and rfc822 format available.

Message #20 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Sarah Morgensen <iskarian <at> mgsn.dev>
To: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Cc: 50614 <at> debbugs.gnu.org, Xinglu Chen <public <at> yoctocell.xyz>
Subject: Re: [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
Date: Fri, 17 Sep 2021 12:15:03 -0700
Hi,

Liliana Marie Prikler <liliana.prikler <at> gmail.com> writes:

> Am Freitag, den 17.09.2021, 10:26 +0200 schrieb Xinglu Chen:
>> * guix/build/utils.scm (list-when): New syntax
>> * tests/build-utils.scm ("list-when: expr1 is non-#f", list-when:
>> expr1 is #f"):
>>   Test it.
>> * guix.texi (Build Utilities): Document it.
>> ---
>> Changes since v1:
>> 
>> * Rename ‘optional’ to ‘list-when’.
>> 
>> * Make
>> 
>>     (list-when test consequent ...)
>> 
>>   equivalent to
>> 
>>     (if tests
>>         (list consequent ...)
>>         '())
> LGTM, but let's wait on more opinions.  IIRC, changing (guix build)
> causes a world rebuild, no?  So this patch might go to core-updates
> first.

Do we actually use this idiom anywhere build-side?  (If not, does it
belong in (guix utils)?  Or should we stick it in (guix build utils)
just to be safe?)

--
Sarah




Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Fri, 17 Sep 2021 19:20:01 GMT) Full text and rfc822 format available.

Message #23 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Sarah Morgensen <iskarian <at> mgsn.dev>
To: Xinglu Chen <public <at> yoctocell.xyz>
Cc: 50614 <at> debbugs.gnu.org, Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: Re: [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
Date: Fri, 17 Sep 2021 12:19:16 -0700
Hi,

Looking at this again I have a couple more comments.

Xinglu Chen <public <at> yoctocell.xyz> writes:
> +@cindex miscellaneous build utilities
> +This section documents some miscellaneous utilities that are useful to
> +have.

"that are useful to have" is implied by its presence in the manual, so
it's unnecessary to write it.

Also, you might consider adding a rule to .dir-locals.el:

  (eval . (put list-when 'scheme-indent-function 1))

which would cause it to be indented like this:

  (list-when expr1
    expr2
    expr3)

(I pointed out a couple other minor grammar corrections in the last
review--did you see those?)

--
Sarah




Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Fri, 17 Sep 2021 19:32:02 GMT) Full text and rfc822 format available.

Message #26 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
To: Sarah Morgensen <iskarian <at> mgsn.dev>
Cc: 50614 <at> debbugs.gnu.org, Xinglu Chen <public <at> yoctocell.xyz>
Subject: Re: [bug#50614] [PATCH] build: utils: Add
 ‘list-when’ macro.
Date: Fri, 17 Sep 2021 21:31:02 +0200
Am Freitag, den 17.09.2021, 12:15 -0700 schrieb Sarah Morgensen:
> Do we actually use this idiom anywhere build-side?  (If not, does it
> belong in (guix utils)?  Or should we stick it in (guix build utils)
> just to be safe?)
I'm pretty sure that this would mostly be used in quasi-quoted contexts
outside of the actual build to build #:configure-flags, #:make-flags
and inputs.  However, I can't really say there'd be no use of it inside
builds, even if builds typically don't need to check for conditionals
other than tests?





Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Sat, 18 Sep 2021 14:42:01 GMT) Full text and rfc822 format available.

Message #29 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Maxime Devos <maximedevos <at> telenet.be>
To: Liliana Marie Prikler <liliana.prikler <at> gmail.com>, Xinglu Chen
 <public <at> yoctocell.xyz>, 50614 <at> debbugs.gnu.org
Cc: Sarah Morgensen <iskarian <at> mgsn.dev>
Subject: Re: [bug#50614] [PATCH] build: utils: Add
 ‘list-when’ macro.
Date: Sat, 18 Sep 2021 16:41:42 +0200
[Message part 1 (text/plain, inline)]
Liliana Marie Prikler schreef op vr 17-09-2021 om 19:55 [+0200]:
> Am Freitag, den 17.09.2021, 10:26 +0200 schrieb Xinglu Chen:
> > * guix/build/utils.scm (list-when): New syntax
> > * tests/build-utils.scm ("list-when: expr1 is non-#f", list-when:
> > expr1 is #f"):
> >   Test it.
> > * guix.texi (Build Utilities): Document it.
> > ---
> > Changes since v1:
> > 
> > * Rename ‘optional’ to ‘list-when’.
> > 
> > * Make
> > 
> >     (list-when test consequent ...)
> > 
> >   equivalent to
> > 
> >     (if tests
> >         (list consequent ...)
> >         '())
> LGTM, but let's wait on more opinions.

It looks nice to me, though you might want an opinion from someone who
is new to Scheme.

Greetings,
Maxime.
[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Sun, 19 Sep 2021 13:40:02 GMT) Full text and rfc822 format available.

Message #32 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: Liliana Marie Prikler <liliana.prikler <at> gmail.com>, 50614 <at> debbugs.gnu.org
Cc: Sarah Morgensen <iskarian <at> mgsn.dev>
Subject: Re: [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
Date: Sun, 19 Sep 2021 15:38:26 +0200
[Message part 1 (text/plain, inline)]
On Fri, Sep 17 2021, Liliana Marie Prikler wrote:

> Am Freitag, den 17.09.2021, 10:26 +0200 schrieb Xinglu Chen:
>> * guix/build/utils.scm (list-when): New syntax
>> * tests/build-utils.scm ("list-when: expr1 is non-#f", list-when:
>> expr1 is #f"):
>>   Test it.
>> * guix.texi (Build Utilities): Document it.
>> ---
>> Changes since v1:
>> 
>> * Rename ‘optional’ to ‘list-when’.
>> 
>> * Make
>> 
>>     (list-when test consequent ...)
>> 
>>   equivalent to
>> 
>>     (if tests
>>         (list consequent ...)
>>         '())
> LGTM, but let's wait on more opinions.  IIRC, changing (guix build)
> causes a world rebuild, no?  So this patch might go to core-updates
> first.

Sorry, I forgot to add ‘core-updates’ to the subject prefix.
[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Sun, 19 Sep 2021 13:42:02 GMT) Full text and rfc822 format available.

Message #35 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: Sarah Morgensen <iskarian <at> mgsn.dev>
Cc: 50614 <at> debbugs.gnu.org, Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: Re: [bug#50614] [PATCH] build: utils: Add ‘list-when’ macro.
Date: Sun, 19 Sep 2021 15:41:26 +0200
[Message part 1 (text/plain, inline)]
On Fri, Sep 17 2021, Sarah Morgensen wrote:

> Hi,
>
> Looking at this again I have a couple more comments.
>
> Xinglu Chen <public <at> yoctocell.xyz> writes:
>> +@cindex miscellaneous build utilities
>> +This section documents some miscellaneous utilities that are useful to
>> +have.
>
> "that are useful to have" is implied by its presence in the manual, so
> it's unnecessary to write it.

Good point.

> Also, you might consider adding a rule to .dir-locals.el:
>
>   (eval . (put list-when 'scheme-indent-function 1))
>
> which would cause it to be indented like this:
>
>   (list-when expr1
>     expr2
>     expr3)

Yes, that would be a good idea.

> (I pointed out a couple other minor grammar corrections in the last
> review--did you see those?)

Oops, looks like I missed those.  I will send a v2 with the suggestions
applied.  :-)

[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Sun, 19 Sep 2021 13:51:01 GMT) Full text and rfc822 format available.

Message #38 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Xinglu Chen <public <at> yoctocell.xyz>
To: 50614 <at> debbugs.gnu.org
Cc: Sarah Morgensen <iskarian <at> mgsn.dev>,
 Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: [PATCH core-updates v2] build: utils: Add ‘list-when’ macro.
Date: Sun, 19 Sep 2021 15:50:41 +0200
* guix/build/utils.scm (list-when): New syntax
* tests/build-utils.scm ("list-when: expr1 is non-#f", list-when: expr1 is #f"):
  Test it.
* guix.texi (Build Utilities): Document it.
* .dir-locals.el: Set ‘scheme-indent-function’ to 1 for ‘list-when’.
---
Changes since v1:

* Add an entry for ‘list-when’ in .dir-locals.el.

* Some fixes to the documentation.

 .dir-locals.el        |  1 +
 doc/guix.texi         | 34 ++++++++++++++++++++++++++++++++++
 guix/build/utils.scm  | 17 ++++++++++++++++-
 tests/build-utils.scm |  8 ++++++++
 4 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/.dir-locals.el b/.dir-locals.el
index 919ed1d1c4..554044ee8c 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -56,6 +56,7 @@ ((nil
    (eval . (put 'lambda* 'scheme-indent-function 1))
    (eval . (put 'substitute* 'scheme-indent-function 1))
    (eval . (put 'match-record 'scheme-indent-function 2))
+   (eval . (put 'list-when 'scheme-indent-function 1))
 
    ;; 'modify-inputs' and its keywords.
    (eval . (put 'modify-inputs 'scheme-indent-function 1))
diff --git a/doc/guix.texi b/doc/guix.texi
index 9a3e8ae12c..4bea366b8e 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -8801,6 +8801,40 @@
         `("PATH" ":" prefix ,(list coreutils))))))
 @end lisp
 
+@subsection Miscellaneous
+
+@cindex miscellaneous build utilities
+This section documents some miscellaneous utilities.
+
+@deffn {Scheme Syntax} list-when @var{test} @var{consequent} @dots{}
+Like @code{when} (@pxref{Conditionals,,, guile, GNU Guile Reference
+Manual}), but if @var{test} evaluates to true, return @code{(list
+@var{consequent} @dots{})}, and if @var{test} evaluates to false, return
+the empty list.  This replaces the following idiom:
+@end deffn
+
+@lisp
+(if @var{test}
+    (list @var{consequent} @dots{})
+    '())
+@end lisp
+
+with this:
+
+@lisp
+(list-when @var{test} @var{consequent} @dots{})
+@end lisp
+
+It can be useful when certain targets require additional configure
+flags, e.g.,
+
+@lisp
+(arguments
+ `(#:configure-flags (list "--localstatedir=/var"
+                           "--sysconfdir=/etc"
+                           ,@@(list-when (hurd-target?) "--with-courage"))))
+@end lisp
+
 @subsection Build Phases
 
 @cindex build phases
diff --git a/guix/build/utils.scm b/guix/build/utils.scm
index 3beb7da67a..d3fb207ee5 100644
--- a/guix/build/utils.scm
+++ b/guix/build/utils.scm
@@ -8,6 +8,7 @@
 ;;; Copyright © 2020 Efraim Flashner <efraim <at> flashner.co.il>
 ;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
 ;;; Copyright © 2021 Maxime Devos <maximedevos <at> telenet.be>
+;;; Copyright © 2021 Xinglu Chen <public <at> yoctocell.xyz>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -123,7 +124,9 @@ (define-module (guix build utils)
 
             make-desktop-entry-file
 
-            locale-category->string))
+            locale-category->string
+
+            list-when))
 
 
 ;;;
@@ -1613,6 +1616,18 @@ (define (locale-category->string category)
              LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE
              LC_TIME)))
 
+
+;;;
+;;; Misc.
+;;;
+
+;; If EXPR1 evaluates to a non-#f value, return '(EXPR2 ...).  Otherwise,
+;; return an empty list.
+(define-syntax list-when
+  (syntax-rules ()
+    ((_ expr1 expr2 ...)
+     (if expr1 (list expr2 ...) '()))))
+
 ;;; Local Variables:
 ;;; eval: (put 'call-with-output-file/atomic 'scheme-indent-function 1)
 ;;; eval: (put 'call-with-ascii-input-file 'scheme-indent-function 1)
diff --git a/tests/build-utils.scm b/tests/build-utils.scm
index 6b131c0af8..b558feb47d 100644
--- a/tests/build-utils.scm
+++ b/tests/build-utils.scm
@@ -3,6 +3,7 @@
 ;;; Copyright © 2019 Ricardo Wurmus <rekado <at> elephly.net>
 ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer <at> gmail.com>
 ;;; Copyright © 2021 Maxime Devos <maximedevos <at> telenet.be>
+;;; Copyright © 2021 Xinglu Chen <public <at> yoctocell.xyz>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -287,5 +288,12 @@ (define-module (test build-utils)
           ("guile/bin" . ,(dirname (which "guile"))))
         "guile"))))
 
+(test-equal "list-when: expr1 is non-#f"
+  (list 3 'bar)
+  (list-when 'foo (+ 1 2) 'bar))
+
+(test-equal "list-when: expr1 is #f"
+  '()
+  (list-when #f (+ 2 3) 'bar))
 
 (test-end)

base-commit: 22f7d4bce1e694b7ac38e62410d76a6d46d96c5d
-- 
2.33.0







Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Sun, 19 Sep 2021 14:36:01 GMT) Full text and rfc822 format available.

Message #41 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: Maxime Devos <maximedevos <at> telenet.be>
To: Xinglu Chen <public <at> yoctocell.xyz>, 50614 <at> debbugs.gnu.org
Cc: Sarah Morgensen <iskarian <at> mgsn.dev>,
 Liliana Marie Prikler <liliana.prikler <at> gmail.com>
Subject: Re: [bug#50614] [PATCH core-updates v2] build: utils: Add
 ‘list-when’ macro.
Date: Sun, 19 Sep 2021 16:35:33 +0200
[Message part 1 (text/plain, inline)]
Xinglu Chen schreef op zo 19-09-2021 om 15:50 [+0200]:
> * guix/build/utils.scm (list-when): New syntax
> * tests/build-utils.scm ("list-when: expr1 is non-#f", list-when: expr1 is #f"):
>   Test it.
> * guix.texi (Build Utilities): Document it.
> * .dir-locals.el: Set ‘scheme-indent-function’ to 1 for ‘list-when’.
> ---

If you will be doing this in (guix build utils), leading to a world-rebuild,
you light want to define list-unless as well.  E.g., 'boost' has

    (native-inputs
     `(("perl" ,perl)
       ,@(if (%current-target-system)
             '()
             `(("python" ,python-wrapper)))
       ("tcsh" ,tcsh)))

with 'list-when', this could be rewritten to

    (native-inputs
     `(("perl" ,perl)
       ,@(list-when (not (%current-target-system))
           `("python" ,python-wrapper))
       ("tcsh" ,tcsh)))

but '(when (not ...) EXP ...)' is simply '(unless EXP ...)',
so it would be nice to be able to do

    (native-inputs
     `(("perl" ,perl)
       ,@(list-unless (%current-target-system)
           `("python" ,python-wrapper))
       ("tcsh" ,tcsh)))

Greetings,
Maxime.
[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#50614; Package guix-patches. (Mon, 20 Sep 2021 11:04:02 GMT) Full text and rfc822 format available.

Message #44 received at 50614 <at> debbugs.gnu.org (full text, mbox):

From: zimoun <zimon.toutoune <at> gmail.com>
To: Xinglu Chen <public <at> yoctocell.xyz>
Cc: 50614 <at> debbugs.gnu.org, Liliana Marie Prikler <liliana.prikler <at> gmail.com>,
 Sarah Morgensen <iskarian <at> mgsn.dev>
Subject: Re: bug#50614: [PATCH core-updates] build: utils: Add
 ‘optional’ macro.
Date: Mon, 20 Sep 2021 13:03:34 +0200
Hi,

On Sun, 19 Sep 2021 at 15:50, Xinglu Chen <public <at> yoctocell.xyz> wrote:

> +@lisp
> +(if @var{test}
> +    (list @var{consequent} @dots{})
> +    '())
> +@end lisp

[...]

> +@lisp
> +(arguments
> + `(#:configure-flags (list "--localstatedir=/var"
> +                           "--sysconfdir=/etc"
> +                           ,@@(list-when (hurd-target?) "--with-courage"))))
> +@end lisp

Personally, I am not convinced it helps the readibility.  But that’s a
matter of taste. :-)

My concern is about the coherence.  First, ’list-unless’ is also
required by a similar pattern, see for instance:

--8<---------------cut here---------------start------------->8---
gnu/packages/guile.scm:162:             ,@(if (target-mingw?) '() `(("bash" ,bash-minimal)))))
--8<---------------cut here---------------end--------------->8---

And second, these 2 patterns ’list-when’ and ’list-unless’ are used in
by many files, see guix/build-system, guix/import, guix/describe,
gnu/packages, gnu/system, gnu/services files.  For the oneline pattern:
“ag --scheme '@\(if' | grep '()'”.  All should be replaced; which
implies a world-rebuild I guess.

Well, all in all, I am not convinced that all this work is worth for a
small debatable readibility improvement. :-)

All the best,
simon




This bug report was last modified 3 years and 266 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.