aboutsummaryrefslogtreecommitdiff
path: root/guix/monads.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2014-12-02 10:11:11 +0100
committerLudovic Courtès <ludo@gnu.org>2014-12-02 16:51:43 +0100
commit21caa6deebee28f07467c5fd1dcd5b8997393ca4 (patch)
tree03e256559c894771b87bea0cdd7e35fa9df9e937 /guix/monads.scm
parentcc7fa5929c26fcdd53ce83ce7a46de4dfc7af1a2 (diff)
downloadgnu-guix-21caa6deebee28f07467c5fd1dcd5b8997393ca4.tar
gnu-guix-21caa6deebee28f07467c5fd1dcd5b8997393ca4.tar.gz
monads: Add 'mwhen' and 'munless'.
* guix/monads.scm (mbegin): Add special '%current-monad' syntactic keyword. (mwhen, munless): New macros.
Diffstat (limited to 'guix/monads.scm')
-rw-r--r--guix/monads.scm30
1 files changed, 29 insertions, 1 deletions
diff --git a/guix/monads.scm b/guix/monads.scm
index b419ba066a..52cb3f5ed0 100644
--- a/guix/monads.scm
+++ b/guix/monads.scm
@@ -39,6 +39,8 @@
mlet
mlet*
mbegin
+ mwhen
+ munless
lift1 lift2 lift3 lift4 lift5 lift6 lift7 lift
listm
foldm
@@ -173,9 +175,15 @@ form is (VAR -> VAL), bind VAR to the non-monadic value VAL in the same way as
body ...)))))))
(define-syntax mbegin
- (syntax-rules ()
+ (syntax-rules (%current-monad)
"Bind the given monadic expressions in sequence, returning the result of
the last one."
+ ((_ %current-monad mexp)
+ mexp)
+ ((_ %current-monad mexp rest ...)
+ (>>= mexp
+ (lambda (unused-value)
+ (mbegin %current-monad rest ...))))
((_ monad mexp)
(with-monad monad
mexp))
@@ -185,6 +193,26 @@ the last one."
(lambda (unused-value)
(mbegin monad rest ...)))))))
+(define-syntax mwhen
+ (syntax-rules ()
+ "When CONDITION is true, evaluate EXP0..EXP* as in an 'mbegin'. When
+CONDITION is false, return *unspecified* in the current monad."
+ ((_ condition exp0 exp* ...)
+ (if condition
+ (mbegin %current-monad
+ exp0 exp* ...)
+ (return *unspecified*)))))
+
+(define-syntax munless
+ (syntax-rules ()
+ "When CONDITION is false, evaluate EXP0..EXP* as in an 'mbegin'. When
+CONDITION is true, return *unspecified* in the current monad."
+ ((_ condition exp0 exp* ...)
+ (if condition
+ (return *unspecified*)
+ (mbegin %current-monad
+ exp0 exp* ...)))))
+
(define-syntax define-lift
(syntax-rules ()
((_ liftn (args ...))