[bug#75010,1/7] monads: Add 'mmatch'.

Message ID 4bfa279cae2316d7a7a4e0640d13a0763ad86f92.1734798943.git.herman@rimm.ee
State New
Headers
Series Roll back when deployment fails. |

Commit Message

Herman Rimm Dec. 21, 2024, 5:04 p.m. UTC
  * doc/guix.texi (The Store Monad): Document mmatch.
* guix/monads.scm (mmatch): Add macro.

Change-Id: I558f8e025f6cf788c9fc475e99d49690d7a98f41
---
 doc/guix.texi   |  6 ++++++
 guix/monads.scm | 11 +++++++++++
 2 files changed, 17 insertions(+)
  

Comments

Ludovic Courtès Dec. 30, 2024, 12:34 p.m. UTC | #1
Herman Rimm <herman@rimm.ee> skribis:

> * doc/guix.texi (The Store Monad): Document mmatch.
> * guix/monads.scm (mmatch): Add macro.
>
> Change-Id: I558f8e025f6cf788c9fc475e99d49690d7a98f41

[...]

> +@defmac mmatch monad mexp (pattern body) @dots{}
> +Match monadic object @var{mexp} against clause @var{pattern}s, in the

I’m not convinced by this one: usually, monadic procedures take a
“normal” value and return a monadic value.  So the style of this macro
is quite unusual.  Also it doesn’t save much typing compared to an
‘mlet’ followed by ‘match’.

WDYT?

Ludo’.
  

Patch

diff --git a/doc/guix.texi b/doc/guix.texi
index f7b7569887..c86f644360 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -11814,6 +11814,12 @@  The Store Monad
 (@pxref{Local Bindings,,, guile, GNU Guile Reference Manual}).
 @end defmac
 
+@defmac mmatch monad mexp (pattern body) @dots{}
+Match monadic object @var{mexp} against clause @var{pattern}s, in the
+order in which they appear.  The last expression of each clause
+@var{body} must be a monadic expression.
+@end defmac
+
 @defmac mbegin monad mexp @dots{}
 Bind @var{mexp} and the following monadic expressions in sequence,
 returning the result of the last expression.  Every expression in the
diff --git a/guix/monads.scm b/guix/monads.scm
index 0bd8ac9315..0e8ca868ce 100644
--- a/guix/monads.scm
+++ b/guix/monads.scm
@@ -1,5 +1,6 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2013, 2014, 2015, 2017, 2022 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2024 Herman Rimm <herman@rimm.ee>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -37,6 +38,7 @@  (define-module (guix monads)
             with-monad
             mlet
             mlet*
+            mmatch
             mbegin
             mwhen
             munless
@@ -355,6 +357,15 @@  (define-syntax mlet
              (let ((var temp) ...)
                body ...)))))))
 
+(define-syntax mmatch
+  (syntax-rules ()
+    "Match the monadic object MEXP against the patterns of CLAUSES ...
+in the order in which they appear.  The last expression of each clause
+body must be a monadic expression."
+    ((_ monad mexp clauses ...)
+     (with-monad monad
+       (>>= mexp (match-lambda clauses ...))))))
+
 (define-syntax mbegin
   (syntax-rules (%current-monad)
     "Bind MEXP and the following monadic expressions in sequence, returning