GNU bug report logs - #61214
[PATCH guix-artwork] website: posts: Add Dissecting Guix, Part 2: The Store Monad.

Previous Next

Package: guix-patches;

Reported by: "(" <paren <at> disroot.org>

Date: Wed, 1 Feb 2023 17:29:01 UTC

Severity: normal

Tags: patch

Done: Tobias Geerinckx-Rice <me <at> tobias.gr>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 61214 in the body.
You can then email your comments to 61214 AT debbugs.gnu.org in the normal way.

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#61214; Package guix-patches. (Wed, 01 Feb 2023 17:29:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to "(" <paren <at> disroot.org>:
New bug report received and forwarded. Copy sent to guix-patches <at> gnu.org. (Wed, 01 Feb 2023 17:29:01 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: guix-patches <at> gnu.org
Cc: "\(" <paren <at> disroot.org>
Subject: [PATCH guix-artwork] website: posts: Add Dissecting Guix,
 Part 2: The Store Monad.
Date: Wed,  1 Feb 2023 17:28:21 +0000
* website/posts/dissecting-guix-2-store-monad.md: New blog post.
---
Heya Guix!

At long last, the second Dissecting Guix is complete :)

This one is about monads, the Guix monad API, and the %STORE-MONAD.  Hopefully
it's not too confusing, but if you find it hard to follow, please let me know!

  -- (

 .../posts/dissecting-guix-2-store-monad.md    | 498 ++++++++++++++++++
 1 file changed, 498 insertions(+)
 create mode 100644 website/posts/dissecting-guix-2-store-monad.md

diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md
new file mode 100644
index 0000000..83f2e69
--- /dev/null
+++ b/website/posts/dissecting-guix-2-store-monad.md
@@ -0,0 +1,498 @@
+title: Dissecting Guix, Part 2: The Store Monad
+date: TBC
+author: (
+tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API
+---
+Hello again!
+
+In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/),
+we briefly mentioned the `with-store` and `run-with-store` APIs.  Today, we'll
+be looking at those in further detail, along with the related monad API and the
+`%store-monad`!
+
+Monads are a little hard to explain, and from a distance, they seem more than a
+bit confusing.  So, I want you to erase monads from your mind for now.  We'll
+come back to them later.
+
+# Yes, No, Maybe So
+
+Let's instead implement another M of functional programming, _`maybe`_ values,
+representing a value that may or may not exist.  `maybe` is a very common
+feature of strongly-typed functional languages, and you'll see it all over the
+place in Haskell and OCaml code. However, Guile is not strongly typed, so we
+usually use ad-hoc `#f`s and `'()`s for null values instead of a proper
+"optional" value.
+
+Just for fun, though, we'll implement a proper `maybe` in Guile.  Fire up that
+REPL once again, and let's import a bunch of modules that we'll need:
+
+```scheme
+(use-modules (ice-9 match)
+             (srfi srfi-9))
+```
+
+We'll implement `maybe` as a record with two fields, `is?` and `value`.  If the
+value contains something, `is?` will be `#t` and `value` will contain the thing
+in question, and if it's empty, `is?`'ll be `#f`.
+
+```scheme
+(define-record-type <maybe>
+  (make-maybe is? value)
+  maybe?
+  (is? maybe-is?)
+  (value maybe-value))
+```
+
+Now we'll define constructors for the two possible states:
+
+```scheme
+(define (something value)
+  (make-maybe #t value))
+
+(define (nothing)
+  (make-maybe #f #f))
+```
+
+And make some silly functions that return optional values:
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+      
+(remove-a "ahh")
+;; #<<maybe> is?: #t value: "hh">
+
+(remove-a "ooh")
+;; #<<maybe> is?: #f value: #f>
+
+(remove-b "bad")
+;; #<<maybe> is?: #t value: "ad">
+```
+
+But what if we want to compose the results of these functions?
+
+# Keeping Your Composure
+
+As you might have guessed, this is not fun.  Cosplaying as a compiler backend
+typically isn't.
+
+```scheme
+(let ((t1 (remove-a "abcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+;; #<<maybe> is?: #t value: "cd">
+
+(let ((t1 (remove-a "bbcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+;; #<<maybe> is?: #f value: #f>
+```
+
+I can almost hear the heckling.  Even worse, chaining three:
+
+```scheme
+(let* ((t1 (remove-a "abad"))
+       (t2 (if (maybe-is? t1)
+               (remove-b (maybe-value t1))
+               (nothing))))
+  (if (maybe-is? t2)
+      (remove-a (maybe-value t2))
+      (nothing)))
+;; #<<maybe> is?: #t value: "d">
+```
+
+So, how do we go about making this more bearable?  Well, one way could be to
+make `remove-a` and `remove-b` accept `maybe`s:
+
+```scheme
+(define (remove-a ?str)
+  (if (maybe-is? ?str)
+      (let ((str (maybe-value ?str)))
+        (if (eq? (string-ref str 0) #\a)
+            (something (substring str 1))
+            (nothing)))
+      (nothing)))
+
+(define (remove-b ?str)
+  (if (maybe-is? ?str)
+      (let ((str (maybe-value ?str)))
+        (if (eq? (string-ref str 0) #\b)
+            (something (substring str 1))
+            (nothing)))
+      (nothing)))
+```
+
+Not at all pretty, but it works!
+
+```
+(remove-b (remove-a (something "abc")))
+;; #<<maybe> is?: #t value: "c">
+```
+
+Still, our procedures now require quite a bit of boilerplate.  Might there be a
+better way?
+
+# The Ties That `>>=` Us
+
+First of all, we'll revert to our original definitions of `remove-a` and
+`remove-b`, that is to say, the ones that take a regular value and return a
+`maybe`.
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+```
+
+What if tried introducing higher-order procedures (procedures that accept other
+procedures as arguments) into the equation?  Because we're functional
+programmers and we're somewhat obsessed with that kind of thing.
+
+```scheme
+(define (maybe-chain maybe proc)
+  (if (maybe-is? maybe)
+      (proc (maybe-value maybe))
+      (nothing)))
+
+(maybe-chain (something "abc")
+             remove-a)
+;; #<<maybe> is?: #t value: "bc">
+
+(maybe-chain (nothing)
+             remove-a)
+;; #<<maybe> is?: #f value: #f>
+```
+
+It lives!  To make it easier to chain procedures like this, we'll define a macro
+that allows us to perform any number of sequenced operations with only one
+chaining form:
+
+```scheme
+(define-syntax maybe-chain*
+  (syntax-rules ()
+    ((_ maybe proc)
+     (maybe-chain maybe proc))
+    ((_ maybe proc rest ...)
+     (maybe-chain* (maybe-chain maybe proc)
+                   rest ...))))
+
+(maybe-chain* (something "abad")
+              remove-a
+              remove-b
+              remove-a)
+;; #<<maybe> is?: #t value: "d">
+```
+
+Congratulations, you've reinvented `bind`, commonly written as the `>>=`
+operator.  And it turns out that a monadic type is just a container type that
+can be used with `>>=`!
+
+# New Wheel, Old Wheel
+
+Now that we've reinvented the wheel, we'd better learn to use the original
+wheel.  Guix provides a generic, high-level monads API, along with three monads,
+though `maybe` is not one of them, so let's integrate it into the Guix monad
+system!
+
+First we'll make the API available:
+
+```scheme
+(use-modules (guix monads))
+```
+
+To define a monad's API in Guix, we simply use the `define-monad` macro, and
+provide two procedures: `bind`, and `return`.
+
+```scheme
+(define-monad %maybe-monad
+  (bind maybe-chain)
+  (return something))
+```
+
+`bind` is just the procedure that we use to chain monadic procedure calls
+together, and `return` is a procedure that takes a non-monadic value and wraps
+it up in the most basic form possible of the monad.
+
+Now we can use the `with-monad` macro to tell Guix to use this specific `bind`
+and `return`, and the `>>=` macro to thread monads through procedure calls!
+
+```scheme
+(with-monad %maybe-monad
+  (>>= (something "aabbc")
+       remove-a
+       remove-a
+       remove-b
+       remove-b))
+;; #<<maybe> is?: #t value: "c">
+```
+
+We can also now use `return`:
+
+```scheme
+(with-monad %maybe-monad
+  (return 32))
+;; #<<maybe> is?: #t value: 32>
+```
+
+But Guix provides many higher-level APIs than `>>=` and `return`, as we will
+see.  There's `mbegin`, which evaluates monadic expressions without binding them
+to symbols, returning the last one:
+
+```scheme
+(mbegin %maybe-monad
+  (remove-a "abc"))
+;; #<<maybe> is?: #t value: "bc">
+```
+
+And there's `mlet` and `mlet*`, which can bind them, and is essentially
+equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`:
+
+```scheme
+(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol
+                     (str1 (remove-a str))
+                     (str2 (remove-b str)))
+  (remove-a str))
+;; #<<maybe> is?: #t value: "d">
+```
+
+Various abstractions over these two exist too, such as `mwhen` (a `when` plus an
+`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize`
+(dynamically-scoped value rebinding, like `parameterize`, in a monadic context).
+`lift` takes a procedure and a monad and creates a new procedure that returns
+a monadic value.
+
+There are also APIs for manipulating lists wrapped in monads; `mlist` creates
+such a list, `anym` is a monadic `any`, `sequence` turns a list of monads into a
+monadic list, `mapm` is a monadic `map`, and `foldm` is a monadic `fold`.
+
+This is all well and good, you may be thinking, but why does Guix need a monad
+API?  The answer is technically that it doesn't.  But building on the monad API
+makes a lot of things much easier, and to learn why, we're going to look at one
+of Guix's built-in monads.
+
+# In a State
+
+Guix implements a monad called `%state-monad`, and it works with single-argument
+procedures returning two values.  Behold:
+
+```scheme
+(with-monad %state-monad
+  (return 33))
+;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)>
+```
+
+The `run-with-state` value turns this procedure into an actually useful value,
+or, rather, two values:
+
+```scheme
+(run-with-state (with-monad %state-monad (return 33))
+  (list "foo" "bar" "baz"))
+;; 33
+;; ("foo" "bar" "baz")
+```
+
+What can this actually do for us, though? Well, it gets interesting if we do
+some `>>=`ing:
+
+```scheme
+(define state-seq
+  (mlet* ((number (return 33)))
+    (state-push number)))
+result
+;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)>
+
+(run-with-state state-seq (list 32))
+;; (32)
+;; (33 32)
+
+(run-with-state state-seq (list 30 99))
+;; (30 99)
+;; (33 30 99)
+```
+
+What is `state-push`?  It's a monadic procedure for `%state-monad` that takes
+whatever's currently in the first value (the primary value) and pushes it onto
+the second value (the state value), which is assumed to be a list, returning the
+old state value as the primary value and the new list as the state value.
+
+So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as
+the initial state value, and then the `>>=` form passes that and `33` to
+`state-push`.  What `%state-monad` allows us to do is thread together some
+procedures that require some kind of state, while pretending the state isn't
+there, and then retrieve both the final state and the result at the end!
+
+If you're a bit confused, don't worry.  We'll write some of our own
+`%state-monad`-based monadic procedures and hopefully all will become clear.
+
+```scheme
+(define (fibonacci-thing value)
+  (lambda (state)
+    (values (+ value state)
+            value)))
+
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1)))
+      (fibonacci-thing n2))
+  0)
+;; 3
+;; 2
+
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1))
+                         (n3 (fibonacci-thing n2))
+                         (n4 (fibonacci-thing n3))
+                         (n5 (fibonacci-thing n4)))
+      (fibonacci-thing n5))
+  0)
+;; 13
+;; 8
+```
+
+The `fibonacci-thing` monadic procedure takes the number passed, makes it the
+current state, and outputs the sum of the state and the number passed.  This
+gives us a sort of Fibonacci-sequence-like behaviour, where the next number in
+the sequence is given by the sum of the two before.
+
+This is all very nifty, and possibly useful in general, but what does this have
+to do with Guix?  Well, many Guix store-based operations are meant to be used
+in concert with yet another monad, called the `%store-monad`.  But if we look at
+`(guix store)`, where `%store-monad` is defined...
+
+```scheme
+(define-alias %store-monad %state-monad)
+(define-alias store-return state-return)
+(define-alias store-bind state-bind)
+```
+
+It was all a shallow façade!  All the "store monad" is is a special case of the
+state monad, where a value representing the store is passed as the state value.
+
+# Lies, Damned Lies, and Abstractions
+
+We mentioned that, technically, we didn't need monads for Guix.  Indeed, many
+(now deprecated) procedures take a store value as the argument:
+
+```scheme
+(use-modules (guix derivations)
+             (guix store))
+
+(with-store store ;remember this?
+  (build-expression->derivation store NAME EXPRESSION ...))
+```
+
+This procedure, being deprecated, should never of course be used.  For one
+thing, it uses the "quoted build expression" style, rather than gexps, which we
+will discuss another time.  The best way to create a derivation from some basic
+build code is to use the new-fangled `gexp->derivation` procedure, which happens
+to be monadic!
+
+```scheme
+(use-modules (guix gexp)
+             (gnu packages irc))
+
+(define symlink-irssi
+  (gexp->derivation "link-to-irssi"
+    #~(symlink #$(file-append irssi "/bin/irssi") #$output)))
+;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)>
+```
+
+You don't have to understand the `#~(...)` form yet, only everything surrounding
+it.  We can see that this `gexp->derivation` returns a procedure taking the
+initial state (store), just like our `%state-monad` procedures did.  And to pass
+this initial state, we used `run-with-state`.  The equivalent for working with
+the store is our old friend `run-with-store`!
+
+```scheme
+(define symlink-irssi-drv
+  (with-store store
+    (run-with-store store
+      symlink-irssi)))
+;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0>
+```
+
+Let's just check this derivation is as expected by reading the code from the
+builder script.
+
+```scheme
+(define symlink-irssi-builder
+  (list-ref (derivation-builder-arguments symlink-irssi-drv) 1))
+
+(call-with-input-file symlink-irssi-builder
+  (lambda (port)
+    (read port)))
+    
+;; (symlink
+;;  "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi"
+;;  ((@ (guile) getenv) "out"))
+```
+
+And indeed, it symlinks the `irssi` binary to the output path.  Some other,
+higher-level, monadic procedures include `interned-file`, which copies a file
+from outside the store into it, and `text-file`, which copies some text into it.
+Generally, these procedures aren't used, as there are higher-level procedures
+that perform similar functions (which we will discuss later), but for the sake
+of this blog post, here's an example:
+
+```scheme
+(with-store store
+  (run-with-store store
+    (text-file "unmatched-paren"
+      "( <paren <at> disroot.org>")))
+;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren"
+```
+
+# Conclusion
+
+What have we learned about monads?  The key points we can take away are:
+
+1. Monads are a way of chaining together procedures and values that are wrapped
+   in containers that give them extra context, like `maybe` values.
+2. Guix provides a high-level monad API that compensates for Guile's lack of
+   strong types or an interface-like system.
+3. This API provides the state monad, which allows you to thread state through
+   procedures such that you can pretend it doesn't exist.
+4. Guix uses the store monad frequently to thread a store connection through
+   procedures that need it.
+5. The store monad is really just the state monad in disguise, where the state
+   value is used to thread the store object through monadic procedures.
+
+If you've read this post in its entirety but still don't yet quite get it, don't
+worry.  Try to modify and tinker about with the examples, and hopefully it will
+all click eventually!
+
+#### About GNU Guix
+
+[GNU Guix](https://guix.gnu.org) is a transactional package manager and
+an advanced distribution of the GNU system that [respects user
+freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html).
+Guix can be used on top of any system running the Hurd or the Linux
+kernel, or it can be used as a standalone operating system distribution
+for i686, x86_64, ARMv7, AArch64 and POWER9 machines.
+
+In addition to standard package management features, Guix supports
+transactional upgrades and roll-backs, unprivileged package management,
+per-user profiles, and garbage collection.  When used as a standalone
+GNU/Linux distribution, Guix offers a declarative, stateless approach to
+operating system configuration management.  Guix is highly customizable
+and hackable through [Guile](https://www.gnu.org/software/guile)
+programming interfaces and extensions to the
+[Scheme](http://schemers.org) language.

base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3
-- 
2.39.1





Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Thu, 02 Feb 2023 08:19:02 GMT) Full text and rfc822 format available.

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

From: Simon Tournier <zimon.toutoune <at> gmail.com>
To: "( via Guix-patches via" <guix-patches <at> gnu.org>, 61214 <at> debbugs.gnu.org
Cc: "\(" <paren <at> disroot.org>
Subject: Re: [bug#61214] [PATCH guix-artwork] website: posts: Add Dissecting
 Guix, Part 2: The Store Monad.
Date: Thu, 02 Feb 2023 09:17:54 +0100
Hi,

Nice!  Some minor comments which can be trashed if they are not
helpful. :-)


On Wed, 01 Feb 2023 at 17:28, "\( via Guix-patches" via <guix-patches <at> gnu.org> wrote:

> +Let's instead implement another M of functional programming, _`maybe`_ values,
> +representing a value that may or may not exist.  `maybe` is a very common
> +feature of strongly-typed functional languages, and you'll see it all over the
> +place in Haskell and OCaml code. However, Guile is not strongly typed, so we

I would say “Guile is dynamically typed“ or “is not statically typed”
instead of “is not strongly typed”.  Because the terminology “strongly
typed” is probably confusing, for instance, quoting Wikipedia [1]:

        Smalltalk, Ruby, Python, and Self are all "strongly typed" in
        the sense that …

        The Lisp family of languages are all "strongly typed" in the
        sense that …

        Standard ML, F#, OCaml, Haskell, Go and Rust are statically
        type-checked, …

1: <https://en.wikipedia.org/wiki/Strong_and_weak_typing>

> +(define (nothing)
> +  (make-maybe #f #f))
> +```

Here, I would mention that the value ‘#f‘ is picked but anything else
would also work as 'nothing.  Well, to make the distinction with the
previous part about #f or () for null values, I would use the symbol
'no-value or something like that.  In all cases, I would mention that
this #f is just useless.


> +(define (remove-a ?str)
> +  (if (maybe-is? ?str)
> +      (let ((str (maybe-value ?str)))
> +        (if (eq? (string-ref str 0) #\a)
> +            (something (substring str 1))
> +            (nothing)))
> +      (nothing)))

Well, my personal preference would be a ’match’ instead of ’let’. :-)

Maybe, it is for smoothly introducing mlet? :-)


> +Congratulations, you've reinvented `bind`, commonly written as the `>>=`
> +operator.  And it turns out that a monadic type is just a container type that
> +can be used with `>>=`!

From my experience, what is often confusing for newcomer with “monadic
notation“ is that ’bind’ (>>=) appears magic since it is the same symbol
for all the types.  When it is attached to one type; here ‘maybe‘.

Well, for what it is worth, it is the same kind of issue when one
presents well-known arithmetic operators.  At first, it is confusing
that the symbol + in 1+2 and 1.2+3.4 does not have the same meaning and
e.g. 1+2.3 is a third meaning.

Well, I would write: ”Congratulations, you've implemented `bind` for the
record `maybe`.  It is commonly denoted as the `>>=` operator.  And it
turns out that a monadic type is just a container type that can be used
with this `>>=` operator defined for such type!“


> +# New Wheel, Old Wheel
> +
> +Now that we've reinvented the wheel, we'd better learn to use the original
> +wheel.  Guix provides a generic, high-level monads API, along with three monads,
> +though `maybe` is not one of them, so let's integrate it into the Guix monad
> +system!

I think it is confusing because from my point of view, a piece is
missing.  Well, for what it is worth, I would end the previous section
with a rough definition of monad:

 1. one type constructor
 2. one ‘bind‘ operator
 3. one ’return’ function

and that ’bind’ and ’return’ must satisfy the 3 laws of monad.  Using
’maybe’ as example, all is explicit and more or less concrete.  Maybe,
the type constructor could be omitted; without loss in generality.

Therefore, “along with three monads” means that the API already provides
a type constructor, ’bind’ and ’return’ for each.  I would write “along
with the three monads commonly named identity and state monads”.

What is the third? :-)


> +First we'll make the API available:
> +
> +```scheme
> +(use-modules (guix monads))
> +```
> +
> +To define a monad's API in Guix, we simply use the `define-monad` macro, and
> +provide two procedures: `bind`, and `return`.
> +
> +```scheme
> +(define-monad %maybe-monad
> +  (bind maybe-chain)
> +  (return something))
> +```

IMHO, ending the previous section with a short paragraph explaining that
a monad is a mathematical object defined with three/two components
(type, bind and return) makes this define-monad API more meaningful;
again IMHO.

> +`bind` is just the procedure that we use to chain monadic procedure calls

Instead of chain, I would write compose.  Elsewhere too.

> +There are also APIs for manipulating lists wrapped in monads; `mlist` creates

Do you mean ’listm’?

> +such a list, `anym` is a monadic `any`, `sequence` turns a list of monads into a
> +monadic list, `mapm` is a monadic `map`, and `foldm` is a monadic `fold`.

Well, here we are touching the limit of what Scheme can express. :-)
Because for instance, reading,

    (mapm %state-monad (lift1 1+ %state-monad) '(0 1 2))

it does not appear clear to me what monad provides compared to just pass
arguments around. ;-)



> +# In a State
> +
> +Guix implements a monad called `%state-monad`, and it works with single-argument
> +procedures returning two values.  Behold:
> +
> +```scheme
> +(with-monad %state-monad
> +  (return 33))
> +;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)>
> +```
>
> +The `run-with-state` value turns this procedure into an actually useful value,
> +or, rather, two values:
> +
> +```scheme
> +(run-with-state (with-monad %state-monad (return 33))
> +  (list "foo" "bar" "baz"))
> +;; 33
> +;; ("foo" "bar" "baz")
> +```
> +
> +What can this actually do for us, though? Well, it gets interesting if we do
> +some `>>=`ing:

What appears to me hard to follow is that >>= is not explicitly used in
the following.  See below.

> +
> +```scheme
> +(define state-seq
> +  (mlet* ((number (return 33)))
> +    (state-push number)))

I guess %state-monad is missing in mlet*.

> +result
> +;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)>
> +
> +(run-with-state state-seq (list 32))
> +;; (32)
> +;; (33 32)
> +
> +(run-with-state state-seq (list 30 99))
> +;; (30 99)
> +;; (33 30 99)
> +```
> +
> +What is `state-push`?  It's a monadic procedure for `%state-monad` that takes
> +whatever's currently in the first value (the primary value) and pushes it onto
> +the second value (the state value), which is assumed to be a list, returning the
> +old state value as the primary value and the new list as the state value.
> +
> +So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as
> +the initial state value, and then the `>>=` form passes that and `33` to

Well, maybe instead of mlet*, it would ease the reading if state-seq
explicitly uses >>= and mention again that instead it could be written
using mlet*.  It appears to me clearer with the text to have:

        (define state-seq*
         (with-monad %state-monad
           (>>= (return 33)
                state-push)))

Or instead, maybe earlier, something like:

--8<---------------cut here---------------start------------->8---
And there's `mlet` and `mlet*`, which can bind them, and is essentially
equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`.  Other
said,

(with-monad %maybe-monad
 (>>= (return "abad")
      remove-a
      remove-b
      remove-a))

is equivalent to:

(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol
                     (str1 (remove-a str))
                     (str2 (remove-b str)))
  (remove-a str))
--8<---------------cut here---------------end--------------->8---

Maybe instead of some unrelated example earlier, I would write:

--8<---------------cut here---------------start------------->8---
Now we can use the `with-monad` macro to tell Guix to use this specific `bind`
and `return`, and the `>>=` macro to thread monads through procedure calls!

```scheme
(with-monad %maybe-monad
  (>>= (something "aabbc")
       remove-a
       remove-a
       remove-b
       remove-b))
;; #<<maybe> is?: #t value: "c">
```

We can also now use `return`:

```scheme
(with-monad %maybe-monad
  (return 32))
;; #<<maybe> is?: #t value: 32>
```

But Guix provides many higher-level APIs than `>>=` and `return`, as we will
see.  There's `mbegin`, which evaluates monadic expressions without binding them
to symbols, returning the last one:

```scheme
(mbegin %maybe-monad
  (remove-a "abc"))
;; #<<maybe> is?: #t value: "bc">
```

And there's `mlet` and `mlet*`, which can bind them, and is essentially
equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`,
rewriting the previous example using the operator `>>=`:

```scheme
(mlet* %maybe-monad ((str -> "aabbc") ;non-monadic binding uses the -> symbol
                     (str1 (remove-a str))
                     (str2 (remove-a str1))
                     (str3 (remove-b str2)))                     
  (remove-b str3))
;; #<<maybe> is?: #t value: "c">
```
--8<---------------cut here---------------end--------------->8---


> +`state-push`.  What `%state-monad` allows us to do is thread together some
> +procedures that require some kind of state, while pretending the state isn't
> +there, and then retrieve both the final state and the result at the end!
> +
> +If you're a bit confused, don't worry.  We'll write some of our own
> +`%state-monad`-based monadic procedures and hopefully all will become clear.

I would add something along these lines for some context:

--8<---------------cut here---------------start------------->8---
Consider the (Fibonacci
sequence)[https://en.wikipedia.org/wiki/Fibonacci_number] where the
next value is computed using the previous values.  Let consider one
`state` that stores the previous computed value, for instance,

```scheme
(define (fibonacci-thing value)
  (lambda (state)
    (values (+ value state)
            value)))
```

Now, let’s evaluate the three first values of the sequence, starting
with 1 and 0.

(run-with-state
  (with-monad %state-monad
    (>>= (return 1)
         fibonacci-thing
         fibonacci-thing
         fibonacci-thing))
   0)
;; 3
;; 2

Similarly, it is possible to compute the 6th and 7th term of the
sequence (remind the 0th and first are given by 0 and 1), using
equivalently mlet*.

(run-with-state
    (mlet* %state-monad ((starting (return 1))
                         (n1 (fibonacci-thing starting))
                         (n2 (fibonacci-thing n1))
                         (n3 (fibonacci-thing n2))
                         (n4 (fibonacci-thing n3))
                         (n5 (fibonacci-thing n4)))
      (fibonacci-thing n5))
  0)
;; 13
;; 8
```

The `fibonacci-thing` monadic procedure takes the number passed, makes it the
current state, and outputs the sum of the state and the number passed.  This
gives us a sort of Fibonacci-sequence-like behaviour, where the next number in
the sequence is given by the sum of the two before.
--8<---------------cut here---------------end--------------->8---

> +This is all very nifty, and possibly useful in general, but what does this have
> +to do with Guix?  Well, many Guix store-based operations are meant to be used
> +in concert with yet another monad, called the `%store-monad`.  But if we look at
> +`(guix store)`, where `%store-monad` is defined...
> +
> +```scheme
> +(define-alias %store-monad %state-monad)
> +(define-alias store-return state-return)
> +(define-alias store-bind state-bind)
> +```
> +
> +It was all a shallow façade!  All the "store monad" is is a special case of the
> +state monad, where a value representing the store is passed as the state value.
> +
> +# Lies, Damned Lies, and Abstractions
> +
> +We mentioned that, technically, we didn't need monads for Guix.  Indeed, many
> +(now deprecated) procedures take a store value as the argument:

I think it is worth to mention that monad provides the correct framework
to compose in a pure world some impure functions.  The pure world is an
isolated environment but that alone is useless.  What make things useful
is to have inputs from the outside impure world.  All the question is
thus to keep the control of impurity when composing.

Well, this short video [1] is about Haskell but all the arguments apply,
IMHO.  I mean the kind of diagram seems to also make sense here.

1: <https://youtu.be/iSmkqocn0oQ>

> +```scheme
> +(use-modules (guix derivations)
> +             (guix store))
> +
> +(with-store store ;remember this?
> +  (build-expression->derivation store NAME EXPRESSION ...))
> +```
> +
> +This procedure, being deprecated, should never of course be used.  For one
> +thing, it uses the "quoted build expression" style, rather than gexps, which we
> +will discuss another time.  The best way to create a derivation from some basic
> +build code is to use the new-fangled `gexp->derivation` procedure, which happens
> +to be monadic!

This paragraph appears to me confusing because it is not clear what is
deprecated; with-store or build-expression->derivation?

Well, I would drop build-expression->derivation.

Instead, I would use this pattern:

--8<---------------cut here---------------start------------->8---
Consider that we would like to build something and have it in the
store, for instance some text file.  Well, the procedure `text-file`,

(define* (text-file name
                    text ;string
                    #:optional (references '()))
  "Return as a monadic value the absolute file name in the store of the file
containing TEXT, a string.

seems what we want.  Therefore, we want to evaluate this monadic value
in the context of the store, similarly as the previous run-with-state.
`guix repl` provides a nice interface with the command `,run-in-store`:

scheme@(guix-user)> ,run-in-store
   (text-file "unmatched-paren"
      "( <paren <at> disroot.org>")
$1 = "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren"

Unwrapping the machinery, it would reads,

(with-store store
  (run-with-store store
    (text-file "unmatched-paren"
      "( <paren <at> disroot.org>")))
;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren"

where `with-store` acts similarly as `with-monad` and `run-with-store`
similarly as `run-with-state`.  Note the order is reversed because the
store is somehow unique.

Now, consider we want to build a derivation.  Let define one:

```scheme
(use-modules (guix gexp)
             (gnu packages irc))

(define symlink-irssi
  (gexp->derivation "link-to-irssi"
    #~(symlink #$(file-append irssi "/bin/irssi") #$output)))
;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)>
```

Remember that store and state are related.

You don't have to understand the `#~(...)` form yet, only everything surrounding
it.  We can see that this `gexp->derivation` returns a procedure taking the
initial state (store), just like our `%state-monad` procedures did.  And to pass
this initial state, we used `run-with-state`.  The equivalent for working with
the store is our old friend `run-with-store`!

  (with-store store
    (run-with-store store
      symlink-irssi)))
;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0>
--8<---------------cut here---------------end--------------->8---

My 2 cents.

Nice read of an hard topic.  Well done! :-)

Cheers,
simon




Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Thu, 02 Feb 2023 08:19:03 GMT) Full text and rfc822 format available.

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Thu, 02 Feb 2023 13:07:01 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: "Simon Tournier" <zimon.toutoune <at> gmail.com>, "( via Guix-patches via"
 <guix-patches <at> gnu.org>, <61214 <at> debbugs.gnu.org>
Subject: Re: [bug#61214] [PATCH guix-artwork] website: posts: Add Dissecting
 Guix, Part 2: The Store Monad.
Date: Thu, 02 Feb 2023 13:06:48 +0000
[Message part 1 (text/plain, inline)]
Heya,

On Thu Feb 2, 2023 at 8:17 AM GMT, Simon Tournier wrote:
> Nice!  Some minor comments which can be trashed if they are not
> helpful. :-)

You raise some good points; I'll send a v2 in a moment.

> Nice read of an hard topic.  Well done! :-)

Thank you! :)

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

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Thu, 02 Feb 2023 13:08:02 GMT) Full text and rfc822 format available.

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Thu, 02 Feb 2023 15:01:02 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: 61214 <at> debbugs.gnu.org
Cc: "\(" <paren <at> disroot.org>
Subject: [PATCH guix-artwork v2] website: posts: Add Dissecting Guix,
 Part 2: The Store Monad.
Date: Thu,  2 Feb 2023 15:00:28 +0000
* website/posts/dissecting-guix-2-store-monad.md: New blog post.
---
Heya,

Taking into account zimoun's critiques, here's a v2.

* Say 'dynamically typed' rather than 'not statically typed'.
* Say 'compose' rather than 'chain'.
* Mention that the first #F in the definition of NOTHING could be any other
  value.
* Use MATCH in the second implementations of REMOVE-A and REMOVE-B.
* Note that we've only implemented >>= for <MAYBE>.
* Add a more formal description of a monad to 'The Ties That `>>=` Us'.
* Mention %IDENTITY-MONAD, %STATE-MONAD, and %STORE-MONAD at the start of
  'New Wheel, Old Wheel'.
* Take the reader through the monad laws in 'New Wheel, Old Wheel'.
* Show what the first MLET* example expands to.
* Fix typo by changing MLIST to LISTM.
* Go into slightly more detail about what the monadic higher-order list
  procedure variants do.
* Add missing %STATE-MONAD to the MLET* in STATE-SEQ.
* Move explanation of the Fibonacci example before the code snippet, and
  clarify it.
* Remove example for BUILD-EXPRESSION->DERIVATION.

 .../posts/dissecting-guix-2-store-monad.md    | 555 ++++++++++++++++++
 1 file changed, 555 insertions(+)
 create mode 100644 website/posts/dissecting-guix-2-store-monad.md

diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md
new file mode 100644
index 0000000..7c7c074
--- /dev/null
+++ b/website/posts/dissecting-guix-2-store-monad.md
@@ -0,0 +1,556 @@
+title: Dissecting Guix, Part 2: The Store Monad
+date: TBC
+author: (
+tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API
+---
+Hello again!
+
+In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/),
+we briefly mentioned the `with-store` and `run-with-store` APIs.  Today, we'll
+be looking at those in further detail, along with the related monad API and the
+`%store-monad`!
+
+Monads are a little hard to explain, and from a distance, they seem more than a
+bit confusing.  So, I want you to erase monads from your mind for now.  We'll
+come back to them later.
+
+# Yes, No, Maybe So
+
+Let's instead implement another M of functional programming, _`maybe`_ values,
+representing a value that may or may not exist.  `maybe` is a very common
+feature of strongly-typed functional languages, and you'll see it all over the
+place in Haskell and OCaml code. However, Guile is dynamically typed, so we
+usually use ad-hoc `#f`s and `'()`s for null values instead of a proper
+"optional" value.
+
+Just for fun, though, we'll implement a proper `maybe` in Guile.  Fire up that
+REPL once again, and let's import a bunch of modules that we'll need:
+
+```scheme
+(use-modules (ice-9 match)
+             (srfi srfi-9))
+```
+
+We'll implement `maybe` as a record with two fields, `is?` and `value`.  If the
+value contains something, `is?` will be `#t` and `value` will contain the thing
+in question, and if it's empty, `is?`'ll be `#f`.
+
+```scheme
+(define-record-type <maybe>
+  (make-maybe is? value)
+  maybe?
+  (is? maybe-is?)
+  (value maybe-value))
+```
+
+Now we'll define constructors for the two possible states:
+
+```scheme
+(define (something value)
+  (make-maybe #t value))
+
+(define (nothing)
+  (make-maybe #f #f)) ;the value here doesn't matter; we'll just use #f
+```
+
+And make some silly functions that return optional values:
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+      
+(remove-a "ahh")
+;; #<<maybe> is?: #t value: "hh">
+
+(remove-a "ooh")
+;; #<<maybe> is?: #f value: #f>
+
+(remove-b "bad")
+;; #<<maybe> is?: #t value: "ad">
+```
+
+But what if we want to compose the results of these functions?
+
+# Keeping Your Composure
+
+As you might have guessed, this is not fun.  Cosplaying as a compiler backend
+typically isn't.
+
+```scheme
+(let ((t1 (remove-a "abcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+;; #<<maybe> is?: #t value: "cd">
+
+(let ((t1 (remove-a "bbcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+;; #<<maybe> is?: #f value: #f>
+```
+
+I can almost hear the heckling.  Even worse, composing three:
+
+```scheme
+(let* ((t1 (remove-a "abad"))
+       (t2 (if (maybe-is? t1)
+               (remove-b (maybe-value t1))
+               (nothing))))
+  (if (maybe-is? t2)
+      (remove-a (maybe-value t2))
+      (nothing)))
+;; #<<maybe> is?: #t value: "d">
+```
+
+So, how do we go about making this more bearable?  Well, one way could be to
+make `remove-a` and `remove-b` accept `maybe`s:
+
+```scheme
+(define (remove-a ?str)
+  (match ?str
+    (($ <maybe> #t str)
+     (if (eq? (string-ref str 0) #\a)
+         (something (substring str 1))
+         (nothing)))
+    (_ (nothing))))
+
+(define (remove-b ?str)
+  (match ?str
+    (($ <maybe> #t str)
+     (if (eq? (string-ref str 0) #\b)
+         (something (substring str 1))
+         (nothing)))
+    (_ (nothing))))
+```
+
+Not at all pretty, but it works!
+
+```
+(remove-b (remove-a (something "abc")))
+;; #<<maybe> is?: #t value: "c">
+```
+
+Still, our procedures now require quite a bit of boilerplate.  Might there be a
+better way?
+
+# The Ties That `>>=` Us
+
+First of all, we'll revert to our original definitions of `remove-a` and
+`remove-b`, that is to say, the ones that take a regular value and return a
+`maybe`.
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+```
+
+What if tried introducing higher-order procedures (procedures that accept other
+procedures as arguments) into the equation?  Because we're functional
+programmers and we have an unhealthy obsession with that sort of thing.
+
+```scheme
+(define (maybe-chain maybe proc)
+  (if (maybe-is? maybe)
+      (proc (maybe-value maybe))
+      (nothing)))
+
+(maybe-chain (something "abc")
+             remove-a)
+;; #<<maybe> is?: #t value: "bc">
+
+(maybe-chain (nothing)
+             remove-a)
+;; #<<maybe> is?: #f value: #f>
+```
+
+It lives!  To make it easier to compose procedures like this, we'll define a
+macro that allows us to perform any number of sequenced operations with only one
+composition form:
+
+```scheme
+(define-syntax maybe-chain*
+  (syntax-rules ()
+    ((_ maybe proc)
+     (maybe-chain maybe proc))
+    ((_ maybe proc rest ...)
+     (maybe-chain* (maybe-chain maybe proc)
+                   rest ...))))
+
+(maybe-chain* (something "abad")
+              remove-a
+              remove-b
+              remove-a)
+;; #<<maybe> is?: #t value: "d">
+```
+
+Congratulations, you've just implemented the `bind` operation, commonly written
+as `>>=`, for our `maybe` type.  And it turns out that a monad is just any
+container-like value for which `>>=` (along with another procedure called
+`return`, which wraps a given value in the simplest possible form of a monad)
+has been implemented.
+
+A more formal definition would be that a monad is a mathematical object composed
+of three parts: a type, a `bind` function, and a `return` function.  So, how do
+monads relate to Guix?
+
+# New Wheel, Old Wheel
+
+Now that we've reinvented the wheel, we'd better learn to use the original
+wheel.  Guix provides a generic, high-level monads API, along with the two
+generic monads `%identity-monad` and `%state-monad`, and the Guix-specific
+`%store-monad`.  Since `maybe` is not one of them, let's integrate our version
+into the Guix monad system!
+
+First we'll make the API available:
+
+```scheme
+(use-modules (guix monads))
+```
+
+To define a monad's API in Guix, we simply use the `define-monad` macro, and
+provide two procedures: `bind`, and `return`.
+
+```scheme
+(define-monad %maybe-monad
+  (bind maybe-chain)
+  (return something))
+```
+
+`bind` is just the procedure that we use to compose monadic procedure calls
+together, and `return` is the procedure that wraps values in the most basic form
+of the monad.  A properly implemented `bind` and `return` must follow these
+laws:
+
+1. `(bind (return x) proc)` must be equivalent to `(proc x)`.
+2. `(bind monad return)` must be equivalent to just `monad`.
+3. `(bind (bind monad proc-1) proc-2)` must be equivalent to
+   `(bind monad (lambda (x) (bind (proc-1 x) proc-2)))`.
+
+Let's verify that our `maybe-chain` and `something` procedures adhere to the
+monad laws:
+
+```scheme
+(define (mlaws-proc-1 x)
+  (something (+ x 1)))
+
+(define (mlaws-proc-2 x)
+  (something (+ x 2)))
+  
+;; First law: the left identity.
+(equal? (maybe-chain (something 0)
+                     mlaws-proc-1)
+        (mlaws-proc-1 0))
+;; #t
+ 
+;; Second law: the right identity.
+(equal? (maybe-chain (something 0)
+                     something)
+        (something 0))
+;; #t
+
+;; Third law: associativity.
+(equal? (maybe-chain (maybe-chain (something 0)
+                                  mlaws-proc-1)
+                     mlaws-proc-2)
+        (maybe-chain (something 0)
+                     (lambda (x)
+                       (maybe-chain (mlaws-proc-1 x)
+                                    mlaws-proc-2))))
+;; #t
+```
+
+Now that we know they're valid, we can use the `with-monad` macro to tell Guix
+to use these specific implementations of `bind` and `return`, and the `>>=`
+macro to thread monads through procedure calls!
+
+```scheme
+(with-monad %maybe-monad
+  (>>= (something "aabbc")
+       remove-a
+       remove-a
+       remove-b
+       remove-b))
+;; #<<maybe> is?: #t value: "c">
+```
+
+We can also now use `return`:
+
+```scheme
+(with-monad %maybe-monad
+  (return 32))
+;; #<<maybe> is?: #t value: 32>
+```
+
+But Guix provides many higher-level APIs than `>>=` and `return`, as we will
+see.  There's `mbegin`, which evaluates monadic expressions without binding them
+to symbols, returning the last one:
+
+```scheme
+(mbegin %maybe-monad
+  (remove-a "abc"))
+;; #<<maybe> is?: #t value: "bc">
+```
+
+And there's `mlet` and `mlet*`, which can bind them, and are essentially
+equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`:
+
+```scheme
+;; This is equivalent...
+(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol
+                     (str1 (remove-a str))
+                     (str2 (remove-b str)))
+  (remove-a str))
+;; #<<maybe> is?: #t value: "d">
+
+;; ...to this:
+(with-monad %maybe-monad
+  (>>= (return "abad")
+       (lambda (str)
+         (remove-a str))
+       (lambda (str1)
+         (remove-b str))
+       (lambda (str2)
+         (remove-a str))))
+```
+
+Various abstractions over these two exist too, such as `mwhen` (a `when` plus an
+`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize`
+(dynamically-scoped value rebinding, like `parameterize`, in a monadic context).
+`lift` takes a procedure and a monad and creates a new procedure that returns
+a monadic value.
+
+There are also APIs for manipulating lists wrapped in monads; `listm` creates
+such a list, `sequence` turns a list of monads into a list wrapped in a monad,
+and the `anym`, `mapm`, and `foldm` procedures are like their non-monadic
+equivalents, except that they return lists wrapped in monads.
+
+This is all well and good, you may be thinking, but why does Guix need a monad
+API?  The answer is technically that it doesn't.  But building on the monad API
+makes a lot of things much easier, and to learn why, we're going to look at one
+of Guix's built-in monads.
+
+# In a State
+
+Guix implements a monad called `%state-monad`, and it works with single-argument
+procedures returning two values.  Behold:
+
+```scheme
+(with-monad %state-monad
+  (return 33))
+;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)>
+```
+
+The `run-with-state` value turns this procedure into an actually useful value,
+or, rather, two values:
+
+```scheme
+(run-with-state (with-monad %state-monad (return 33))
+  (list "foo" "bar" "baz"))
+;; 33
+;; ("foo" "bar" "baz")
+```
+
+What can this actually do for us, though? Well, it gets interesting if we do
+some `>>=`ing:
+
+```scheme
+(define state-seq
+  (mlet* %state-monad ((number (return 33)))
+    (state-push number)))
+result
+;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)>
+
+(run-with-state state-seq (list 32))
+;; (32)
+;; (33 32)
+
+(run-with-state state-seq (list 30 99))
+;; (30 99)
+;; (33 30 99)
+```
+
+What is `state-push`?  It's a monadic procedure for `%state-monad` that takes
+whatever's currently in the first value (the primary value) and pushes it onto
+the second value (the state value), which is assumed to be a list, returning the
+old state value as the primary value and the new list as the state value.
+
+So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as
+the initial state value, and then the `>>=` form passes that and `33` to
+`state-push`.  What `%state-monad` allows us to do is thread together some
+procedures that require some kind of state, while pretending the state isn't
+there, and then retrieve both the final state and the result at the end!
+
+If you're a bit confused, don't worry.  We'll write some of our own
+`%state-monad`-based monadic procedures and hopefully all will become clear.
+Consider, for instance, the
+[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in which
+each value is computed by adding the previous two.  We could use the
+`%state-monad` to compute Fibonacci numbers by storing the previous number as
+the primary value and the number before that as the state value:
+
+```scheme
+(define (fibonacci-thing value)
+  (lambda (state)
+    (values (+ value state)
+            value)))
+```
+
+Now we can feed our Fibonacci-generating procedure the first value using
+`run-with-state` and the second using `return`:
+
+```scheme
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1)))
+      (fibonacci-thing n2))
+  0)
+;; 3
+;; 2
+
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1))
+                         (n3 (fibonacci-thing n2))
+                         (n4 (fibonacci-thing n3))
+                         (n5 (fibonacci-thing n4)))
+      (fibonacci-thing n5))
+  0)
+;; 13
+;; 8
+```
+
+This is all very nifty, and possibly useful in general, but what does this have
+to do with Guix?  Well, many Guix store-based operations are meant to be used
+in concert with yet another monad, called the `%store-monad`.  But if we look at
+`(guix store)`, where `%store-monad` is defined...
+
+```scheme
+(define-alias %store-monad %state-monad)
+(define-alias store-return state-return)
+(define-alias store-bind state-bind)
+```
+
+It was all a shallow façade!  All the "store monad" is is a special case of the
+state monad, where a value representing the store is passed as the state value.
+
+# Lies, Damned Lies, and Abstractions
+
+We mentioned that, technically, we didn't need monads for Guix.  Indeed, many
+(now deprecated) procedures take a store value as the argument, such as
+`build-expression->derivation`.  However, using monads both helps ensure purity
+and simply looks nicer.
+
+`build-expression->derivation`, being deprecated, should never of course be
+used.  For one thing, it uses the "quoted build expression" style, rather than
+G-expressions (we'll discuss gexps another time).  The best way to create a
+derivation from some basic build code is to use the new-fangled
+`gexp->derivation` procedure:
+
+```scheme
+(use-modules (guix gexp)
+             (gnu packages irc))
+
+(define symlink-irssi
+  (gexp->derivation "link-to-irssi"
+    #~(symlink #$(file-append irssi "/bin/irssi") #$output)))
+;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)>
+```
+
+You don't have to understand the `#~(...)` form yet, only everything surrounding
+it.  We can see that this `gexp->derivation` returns a procedure taking the
+initial state (store), just like our `%state-monad` procedures did.  And to pass
+this initial state, we used `run-with-state`.  The equivalent for working with
+the store is our old friend `run-with-store`!
+
+```scheme
+(define symlink-irssi-drv
+  (with-store store
+    (run-with-store store
+      symlink-irssi)))
+;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0>
+```
+
+Let's just check this derivation is as expected by reading the code from the
+builder script.
+
+```scheme
+(define symlink-irssi-builder
+  (list-ref (derivation-builder-arguments symlink-irssi-drv) 1))
+
+(call-with-input-file symlink-irssi-builder
+  (lambda (port)
+    (read port)))
+    
+;; (symlink
+;;  "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi"
+;;  ((@ (guile) getenv) "out"))
+```
+
+And indeed, it symlinks the `irssi` binary to the output path.  Some other,
+higher-level, monadic procedures include `interned-file`, which copies a file
+from outside the store into it, and `text-file`, which copies some text into it.
+Generally, these procedures aren't used, as there are higher-level procedures
+that perform similar functions (which we will discuss later), but for the sake
+of this blog post, here's an example:
+
+```scheme
+(with-store store
+  (run-with-store store
+    (text-file "unmatched-paren"
+      "( <paren <at> disroot.org>")))
+;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren"
+```
+
+# Conclusion
+
+What have we learned about monads?  The key points we can take away are:
+
+1. Monads are a way of composing together procedures and values that are wrapped
+   in containers that give them extra context, like `maybe` values.
+2. Guix provides a high-level monad API that compensates for Guile's lack of
+   strong types or an interface-like system.
+3. This API provides the state monad, which allows you to thread state through
+   procedures such that you can pretend it doesn't exist.
+4. Guix uses the store monad frequently to thread a store connection through
+   procedures that need it.
+5. The store monad is really just the state monad in disguise, where the state
+   value is used to thread the store object through monadic procedures.
+
+If you've read this post in its entirety but still don't yet quite get it, don't
+worry.  Try to modify and tinker about with the examples, and hopefully it will
+all click eventually!
+
+#### About GNU Guix
+
+[GNU Guix](https://guix.gnu.org) is a transactional package manager and
+an advanced distribution of the GNU system that [respects user
+freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html).
+Guix can be used on top of any system running the Hurd or the Linux
+kernel, or it can be used as a standalone operating system distribution
+for i686, x86_64, ARMv7, AArch64 and POWER9 machines.
+
+In addition to standard package management features, Guix supports
+transactional upgrades and roll-backs, unprivileged package management,
+per-user profiles, and garbage collection.  When used as a standalone
+GNU/Linux distribution, Guix offers a declarative, stateless approach to
+operating system configuration management.  Guix is highly customizable
+and hackable through [Guile](https://www.gnu.org/software/guile)
+programming interfaces and extensions to the
+[Scheme](http://schemers.org) language.

base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3
-- 
2.39.1





Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Thu, 02 Feb 2023 15:05:02 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: "(" <paren <at> disroot.org>, <61214 <at> debbugs.gnu.org>
Cc: zimoun <zimon.toutoune <at> gmail.com>
Subject: Re: [PATCH guix-artwork v2] website: posts: Add Dissecting Guix,
 Part 2: The Store Monad.
Date: Thu, 02 Feb 2023 15:04:17 +0000
[Message part 1 (text/plain, inline)]
CC zimoun.

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

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Fri, 03 Feb 2023 01:57:01 GMT) Full text and rfc822 format available.

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

From: Feng Shu <tumashu <at> 163.com>
To: 61214 <at> debbugs.gnu.org
Subject: run-with-state and run-with-store
Date: Fri, 03 Feb 2023 09:55:51 +0800
> And to pass this initial state, we used `run-with-state`.  The equivalent for working with
> the store is our old friend `run-with-store`!

For my poor English, I do not understand this well, does this mean
"run-with-store will call run-with-state, we just use run-with-store
generally." or "run-with-store is similer with run-with-state, they can
replace each other"?



-- 





Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Fri, 03 Feb 2023 05:32:02 GMT) Full text and rfc822 format available.

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

From: 宋文武 <iyzsong <at> envs.net>
To: Feng Shu <tumashu <at> 163.com>
Cc: 61214 <at> debbugs.gnu.org
Subject: Re: bug#61214: [PATCH guix-artwork] website: posts: Add Dissecting
 Guix, Part 2: The Store Monad.
Date: Fri, 03 Feb 2023 13:31:28 +0800
Feng Shu <tumashu <at> 163.com> writes:

>> And to pass this initial state, we used `run-with-state`.  The equivalent for working with
>> the store is our old friend `run-with-store`!
>
> For my poor English, I do not understand this well, does this mean
> "run-with-store will call run-with-state, we just use run-with-store
> generally." or "run-with-store is similer with run-with-state, they can
> replace each other"?

Well, "The equivalent for working with the store ..." should read as
"And to pass this initial store, we should use `run-with-store`".

> We can see that this `gexp->derivation` returns a procedure taking the
> initial state (store), just like our `%state-monad` procedures did.
> And to pass this initial state, we used `run-with-state`.

Maybe "And to pass the initial state for `%state-monad`, we used
`run-with-state`.  So to pass the initial for `%store-monad`, we will
use `run-with-store`!" is better?




Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Fri, 03 Feb 2023 06:36:01 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: "Feng Shu" <tumashu <at> 163.com>, <61214 <at> debbugs.gnu.org>
Subject: Re: [bug#61214] run-with-state and run-with-store
Date: Fri, 03 Feb 2023 06:35:07 +0000
[Message part 1 (text/plain, inline)]
Heya,

On Fri Feb 3, 2023 at 1:55 AM GMT, Feng Shu wrote:
> For my poor English, I do not understand this well, does this mean
> "run-with-store will call run-with-state, we just use run-with-store
> generally." or "run-with-store is similer with run-with-state, they can
> replace each other"?

Oops :) It's supposed to mean something like:

> Just like we use `run-with-state` to pass an initial state to a monadic
> value that uses `%state-monad`, we use our old friend `run-with-store`
> when the value uses the `%store-monad`.

Since it's unclear at present, I'll change it in a moment.

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

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Fri, 03 Feb 2023 07:37:01 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: 61214 <at> debbugs.gnu.org
Cc: "\(" <paren <at> disroot.org>
Subject: [PATCH guix-artwork v3] website: posts: Add Dissecting Guix,
 Part 2: The Store Monad.
Date: Fri,  3 Feb 2023 07:36:24 +0000
* website/posts/dissecting-guix-2-store-monad.md: New blog post.
---
Heya,

Here's a v3.

* Make purpose of `run-with-store` versus `run-with-state` clearer.

  -- (

 .../posts/dissecting-guix-2-store-monad.md    | 557 ++++++++++++++++++
 1 file changed, 557 insertions(+)
 create mode 100644 website/posts/dissecting-guix-2-store-monad.md

diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md
new file mode 100644
index 0000000..a27a28b
--- /dev/null
+++ b/website/posts/dissecting-guix-2-store-monad.md
@@ -0,0 +1,557 @@
+title: Dissecting Guix, Part 2: The Store Monad
+date: TBC
+author: (
+tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API
+---
+Hello again!
+
+In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/),
+we briefly mentioned the `with-store` and `run-with-store` APIs.  Today, we'll
+be looking at those in further detail, along with the related monad API and the
+`%store-monad`!
+
+Monads are a little hard to explain, and from a distance, they seem more than a
+bit confusing.  So, I want you to erase monads from your mind for now.  We'll
+come back to them later.
+
+# Yes, No, Maybe So
+
+Let's instead implement another M of functional programming, _`maybe`_ values,
+representing a value that may or may not exist.  `maybe` is a very common
+feature of strongly-typed functional languages, and you'll see it all over the
+place in Haskell and OCaml code. However, Guile is dynamically typed, so we
+usually use ad-hoc `#f`s and `'()`s for null values instead of a proper
+"optional" value.
+
+Just for fun, though, we'll implement a proper `maybe` in Guile.  Fire up that
+REPL once again, and let's import a bunch of modules that we'll need:
+
+```scheme
+(use-modules (ice-9 match)
+             (srfi srfi-9))
+```
+
+We'll implement `maybe` as a record with two fields, `is?` and `value`.  If the
+value contains something, `is?` will be `#t` and `value` will contain the thing
+in question, and if it's empty, `is?`'ll be `#f`.
+
+```scheme
+(define-record-type <maybe>
+  (make-maybe is? value)
+  maybe?
+  (is? maybe-is?)
+  (value maybe-value))
+```
+
+Now we'll define constructors for the two possible states:
+
+```scheme
+(define (something value)
+  (make-maybe #t value))
+
+(define (nothing)
+  (make-maybe #f #f)) ;the value here doesn't matter; we'll just use #f
+```
+
+And make some silly functions that return optional values:
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+      
+(remove-a "ahh")
+;; #<<maybe> is?: #t value: "hh">
+
+(remove-a "ooh")
+;; #<<maybe> is?: #f value: #f>
+
+(remove-b "bad")
+;; #<<maybe> is?: #t value: "ad">
+```
+
+But what if we want to compose the results of these functions?
+
+# Keeping Your Composure
+
+As you might have guessed, this is not fun.  Cosplaying as a compiler backend
+typically isn't.
+
+```scheme
+(let ((t1 (remove-a "abcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+;; #<<maybe> is?: #t value: "cd">
+
+(let ((t1 (remove-a "bbcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+;; #<<maybe> is?: #f value: #f>
+```
+
+I can almost hear the heckling.  Even worse, composing three:
+
+```scheme
+(let* ((t1 (remove-a "abad"))
+       (t2 (if (maybe-is? t1)
+               (remove-b (maybe-value t1))
+               (nothing))))
+  (if (maybe-is? t2)
+      (remove-a (maybe-value t2))
+      (nothing)))
+;; #<<maybe> is?: #t value: "d">
+```
+
+So, how do we go about making this more bearable?  Well, one way could be to
+make `remove-a` and `remove-b` accept `maybe`s:
+
+```scheme
+(define (remove-a ?str)
+  (match ?str
+    (($ <maybe> #t str)
+     (if (eq? (string-ref str 0) #\a)
+         (something (substring str 1))
+         (nothing)))
+    (_ (nothing))))
+
+(define (remove-b ?str)
+  (match ?str
+    (($ <maybe> #t str)
+     (if (eq? (string-ref str 0) #\b)
+         (something (substring str 1))
+         (nothing)))
+    (_ (nothing))))
+```
+
+Not at all pretty, but it works!
+
+```
+(remove-b (remove-a (something "abc")))
+;; #<<maybe> is?: #t value: "c">
+```
+
+Still, our procedures now require quite a bit of boilerplate.  Might there be a
+better way?
+
+# The Ties That `>>=` Us
+
+First of all, we'll revert to our original definitions of `remove-a` and
+`remove-b`, that is to say, the ones that take a regular value and return a
+`maybe`.
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+```
+
+What if tried introducing higher-order procedures (procedures that accept other
+procedures as arguments) into the equation?  Because we're functional
+programmers and we have an unhealthy obsession with that sort of thing.
+
+```scheme
+(define (maybe-chain maybe proc)
+  (if (maybe-is? maybe)
+      (proc (maybe-value maybe))
+      (nothing)))
+
+(maybe-chain (something "abc")
+             remove-a)
+;; #<<maybe> is?: #t value: "bc">
+
+(maybe-chain (nothing)
+             remove-a)
+;; #<<maybe> is?: #f value: #f>
+```
+
+It lives!  To make it easier to compose procedures like this, we'll define a
+macro that allows us to perform any number of sequenced operations with only one
+composition form:
+
+```scheme
+(define-syntax maybe-chain*
+  (syntax-rules ()
+    ((_ maybe proc)
+     (maybe-chain maybe proc))
+    ((_ maybe proc rest ...)
+     (maybe-chain* (maybe-chain maybe proc)
+                   rest ...))))
+
+(maybe-chain* (something "abad")
+              remove-a
+              remove-b
+              remove-a)
+;; #<<maybe> is?: #t value: "d">
+```
+
+Congratulations, you've just implemented the `bind` operation, commonly written
+as `>>=`, for our `maybe` type.  And it turns out that a monad is just any
+container-like value for which `>>=` (along with another procedure called
+`return`, which wraps a given value in the simplest possible form of a monad)
+has been implemented.
+
+A more formal definition would be that a monad is a mathematical object composed
+of three parts: a type, a `bind` function, and a `return` function.  So, how do
+monads relate to Guix?
+
+# New Wheel, Old Wheel
+
+Now that we've reinvented the wheel, we'd better learn to use the original
+wheel.  Guix provides a generic, high-level monads API, along with the two
+generic monads `%identity-monad` and `%state-monad`, and the Guix-specific
+`%store-monad`.  Since `maybe` is not one of them, let's integrate our version
+into the Guix monad system!
+
+First we'll make the API available:
+
+```scheme
+(use-modules (guix monads))
+```
+
+To define a monad's API in Guix, we simply use the `define-monad` macro, and
+provide two procedures: `bind`, and `return`.
+
+```scheme
+(define-monad %maybe-monad
+  (bind maybe-chain)
+  (return something))
+```
+
+`bind` is just the procedure that we use to compose monadic procedure calls
+together, and `return` is the procedure that wraps values in the most basic form
+of the monad.  A properly implemented `bind` and `return` must follow these
+laws:
+
+1. `(bind (return x) proc)` must be equivalent to `(proc x)`.
+2. `(bind monad return)` must be equivalent to just `monad`.
+3. `(bind (bind monad proc-1) proc-2)` must be equivalent to
+   `(bind monad (lambda (x) (bind (proc-1 x) proc-2)))`.
+
+Let's verify that our `maybe-chain` and `something` procedures adhere to the
+monad laws:
+
+```scheme
+(define (mlaws-proc-1 x)
+  (something (+ x 1)))
+
+(define (mlaws-proc-2 x)
+  (something (+ x 2)))
+  
+;; First law: the left identity.
+(equal? (maybe-chain (something 0)
+                     mlaws-proc-1)
+        (mlaws-proc-1 0))
+;; #t
+ 
+;; Second law: the right identity.
+(equal? (maybe-chain (something 0)
+                     something)
+        (something 0))
+;; #t
+
+;; Third law: associativity.
+(equal? (maybe-chain (maybe-chain (something 0)
+                                  mlaws-proc-1)
+                     mlaws-proc-2)
+        (maybe-chain (something 0)
+                     (lambda (x)
+                       (maybe-chain (mlaws-proc-1 x)
+                                    mlaws-proc-2))))
+;; #t
+```
+
+Now that we know they're valid, we can use the `with-monad` macro to tell Guix
+to use these specific implementations of `bind` and `return`, and the `>>=`
+macro to thread monads through procedure calls!
+
+```scheme
+(with-monad %maybe-monad
+  (>>= (something "aabbc")
+       remove-a
+       remove-a
+       remove-b
+       remove-b))
+;; #<<maybe> is?: #t value: "c">
+```
+
+We can also now use `return`:
+
+```scheme
+(with-monad %maybe-monad
+  (return 32))
+;; #<<maybe> is?: #t value: 32>
+```
+
+But Guix provides many higher-level APIs than `>>=` and `return`, as we will
+see.  There's `mbegin`, which evaluates monadic expressions without binding them
+to symbols, returning the last one:
+
+```scheme
+(mbegin %maybe-monad
+  (remove-a "abc"))
+;; #<<maybe> is?: #t value: "bc">
+```
+
+And there's `mlet` and `mlet*`, which can bind them, and are essentially
+equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`:
+
+```scheme
+;; This is equivalent...
+(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol
+                     (str1 (remove-a str))
+                     (str2 (remove-b str)))
+  (remove-a str))
+;; #<<maybe> is?: #t value: "d">
+
+;; ...to this:
+(with-monad %maybe-monad
+  (>>= (return "abad")
+       (lambda (str)
+         (remove-a str))
+       (lambda (str1)
+         (remove-b str))
+       (lambda (str2)
+         (remove-a str))))
+```
+
+Various abstractions over these two exist too, such as `mwhen` (a `when` plus an
+`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize`
+(dynamically-scoped value rebinding, like `parameterize`, in a monadic context).
+`lift` takes a procedure and a monad and creates a new procedure that returns
+a monadic value.
+
+There are also APIs for manipulating lists wrapped in monads; `listm` creates
+such a list, `sequence` turns a list of monads into a list wrapped in a monad,
+and the `anym`, `mapm`, and `foldm` procedures are like their non-monadic
+equivalents, except that they return lists wrapped in monads.
+
+This is all well and good, you may be thinking, but why does Guix need a monad
+API?  The answer is technically that it doesn't.  But building on the monad API
+makes a lot of things much easier, and to learn why, we're going to look at one
+of Guix's built-in monads.
+
+# In a State
+
+Guix implements a monad called `%state-monad`, and it works with single-argument
+procedures returning two values.  Behold:
+
+```scheme
+(with-monad %state-monad
+  (return 33))
+;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)>
+```
+
+The `run-with-state` value turns this procedure into an actually useful value,
+or, rather, two values:
+
+```scheme
+(run-with-state (with-monad %state-monad (return 33))
+  (list "foo" "bar" "baz"))
+;; 33
+;; ("foo" "bar" "baz")
+```
+
+What can this actually do for us, though? Well, it gets interesting if we do
+some `>>=`ing:
+
+```scheme
+(define state-seq
+  (mlet* %state-monad ((number (return 33)))
+    (state-push number)))
+result
+;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)>
+
+(run-with-state state-seq (list 32))
+;; (32)
+;; (33 32)
+
+(run-with-state state-seq (list 30 99))
+;; (30 99)
+;; (33 30 99)
+```
+
+What is `state-push`?  It's a monadic procedure for `%state-monad` that takes
+whatever's currently in the first value (the primary value) and pushes it onto
+the second value (the state value), which is assumed to be a list, returning the
+old state value as the primary value and the new list as the state value.
+
+So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as
+the initial state value, and then the `>>=` form passes that and `33` to
+`state-push`.  What `%state-monad` allows us to do is thread together some
+procedures that require some kind of state, while pretending the state isn't
+there, and then retrieve both the final state and the result at the end!
+
+If you're a bit confused, don't worry.  We'll write some of our own
+`%state-monad`-based monadic procedures and hopefully all will become clear.
+Consider, for instance, the
+[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in which
+each value is computed by adding the previous two.  We could use the
+`%state-monad` to compute Fibonacci numbers by storing the previous number as
+the primary value and the number before that as the state value:
+
+```scheme
+(define (fibonacci-thing value)
+  (lambda (state)
+    (values (+ value state)
+            value)))
+```
+
+Now we can feed our Fibonacci-generating procedure the first value using
+`run-with-state` and the second using `return`:
+
+```scheme
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1)))
+      (fibonacci-thing n2))
+  0)
+;; 3
+;; 2
+
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1))
+                         (n3 (fibonacci-thing n2))
+                         (n4 (fibonacci-thing n3))
+                         (n5 (fibonacci-thing n4)))
+      (fibonacci-thing n5))
+  0)
+;; 13
+;; 8
+```
+
+This is all very nifty, and possibly useful in general, but what does this have
+to do with Guix?  Well, many Guix store-based operations are meant to be used
+in concert with yet another monad, called the `%store-monad`.  But if we look at
+`(guix store)`, where `%store-monad` is defined...
+
+```scheme
+(define-alias %store-monad %state-monad)
+(define-alias store-return state-return)
+(define-alias store-bind state-bind)
+```
+
+It was all a shallow façade!  All the "store monad" is is a special case of the
+state monad, where a value representing the store is passed as the state value.
+
+# Lies, Damned Lies, and Abstractions
+
+We mentioned that, technically, we didn't need monads for Guix.  Indeed, many
+(now deprecated) procedures take a store value as the argument, such as
+`build-expression->derivation`.  However, using monads both helps ensure purity
+and simply looks nicer.
+
+`build-expression->derivation`, being deprecated, should never of course be
+used.  For one thing, it uses the "quoted build expression" style, rather than
+G-expressions (we'll discuss gexps another time).  The best way to create a
+derivation from some basic build code is to use the new-fangled
+`gexp->derivation` procedure:
+
+```scheme
+(use-modules (guix gexp)
+             (gnu packages irc))
+
+(define symlink-irssi
+  (gexp->derivation "link-to-irssi"
+    #~(symlink #$(file-append irssi "/bin/irssi") #$output)))
+;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)>
+```
+
+You don't have to understand the `#~(...)` form yet, only everything surrounding
+it.  We can see that this `gexp->derivation` returns a procedure taking the
+initial state (store), just like our `%state-monad` procedures did, and like we
+used `run-with-state` to pass the initial state to a `%state-monad` monadic
+value, we use our old friend `run-with-store` when we have a `%store-monad`
+monadic value!
+
+```scheme
+(define symlink-irssi-drv
+  (with-store store
+    (run-with-store store
+      symlink-irssi)))
+;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0>
+```
+
+Let's just check this derivation is as expected by reading the code from the
+builder script.
+
+```scheme
+(define symlink-irssi-builder
+  (list-ref (derivation-builder-arguments symlink-irssi-drv) 1))
+
+(call-with-input-file symlink-irssi-builder
+  (lambda (port)
+    (read port)))
+    
+;; (symlink
+;;  "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi"
+;;  ((@ (guile) getenv) "out"))
+```
+
+And indeed, it symlinks the `irssi` binary to the output path.  Some other,
+higher-level, monadic procedures include `interned-file`, which copies a file
+from outside the store into it, and `text-file`, which copies some text into it.
+Generally, these procedures aren't used, as there are higher-level procedures
+that perform similar functions (which we will discuss later), but for the sake
+of this blog post, here's an example:
+
+```scheme
+(with-store store
+  (run-with-store store
+    (text-file "unmatched-paren"
+      "( <paren <at> disroot.org>")))
+;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren"
+```
+
+# Conclusion
+
+What have we learned about monads?  The key points we can take away are:
+
+1. Monads are a way of composing together procedures and values that are wrapped
+   in containers that give them extra context, like `maybe` values.
+2. Guix provides a high-level monad API that compensates for Guile's lack of
+   strong types or an interface-like system.
+3. This API provides the state monad, which allows you to thread state through
+   procedures such that you can pretend it doesn't exist.
+4. Guix uses the store monad frequently to thread a store connection through
+   procedures that need it.
+5. The store monad is really just the state monad in disguise, where the state
+   value is used to thread the store object through monadic procedures.
+
+If you've read this post in its entirety but still don't yet quite get it, don't
+worry.  Try to modify and tinker about with the examples, and hopefully it will
+all click eventually!
+
+#### About GNU Guix
+
+[GNU Guix](https://guix.gnu.org) is a transactional package manager and
+an advanced distribution of the GNU system that [respects user
+freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html).
+Guix can be used on top of any system running the Hurd or the Linux
+kernel, or it can be used as a standalone operating system distribution
+for i686, x86_64, ARMv7, AArch64 and POWER9 machines.
+
+In addition to standard package management features, Guix supports
+transactional upgrades and roll-backs, unprivileged package management,
+per-user profiles, and garbage collection.  When used as a standalone
+GNU/Linux distribution, Guix offers a declarative, stateless approach to
+operating system configuration management.  Guix is highly customizable
+and hackable through [Guile](https://www.gnu.org/software/guile)
+programming interfaces and extensions to the
+[Scheme](http://schemers.org) language.

base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3
-- 
2.39.1





Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Mon, 06 Feb 2023 18:21:01 GMT) Full text and rfc822 format available.

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

From: Simon Tournier <zimon.toutoune <at> gmail.com>
To: "( via Guix-patches via" <guix-patches <at> gnu.org>, 61214 <at> debbugs.gnu.org
Cc: "\(" <paren <at> disroot.org>
Subject: Re: [bug#61214] [PATCH guix-artwork v2] website: posts: Add
 Dissecting Guix, Part 2: The Store Monad.
Date: Mon, 06 Feb 2023 16:38:18 +0100
Hi,

On jeu., 02 févr. 2023 at 15:00, "\( via Guix-patches" via <guix-patches <at> gnu.org> wrote:

>  .../posts/dissecting-guix-2-store-monad.md    | 555 ++++++++++++++++++
>  1 file changed, 555 insertions(+)
>  create mode 100644 website/posts/dissecting-guix-2-store-monad.md

Well done!  Nice reading, I like it. :-)

My English is not enough good to evaluate if there is no grammar
mistakes.  Other said, LGTM!

Cheers,
simon




Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Mon, 06 Feb 2023 18:21:03 GMT) Full text and rfc822 format available.

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Sun, 12 Feb 2023 12:01:02 GMT) Full text and rfc822 format available.

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

From: Christopher Baines <mail <at> cbaines.net>
To: "(" <paren <at> disroot.org>
Cc: 61214 <at> debbugs.gnu.org, guix-patches <at> gnu.org
Subject: Re: [bug#61214] [PATCH guix-artwork v3] website: posts: Add
 Dissecting Guix, Part 2: The Store Monad.
Date: Sun, 12 Feb 2023 10:47:11 +0000
[Message part 1 (text/plain, inline)]
"( via Guix-patches" via <guix-patches <at> gnu.org> writes:

> +Hello again!
> +
> +In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/),
> +we briefly mentioned the `with-store` and `run-with-store` APIs.  Today, we'll
> +be looking at those in further detail, along with the related monad API and the
> +`%store-monad`!
> +
> +Monads are a little hard to explain, and from a distance, they seem more than a
> +bit confusing.  So, I want you to erase monads from your mind for now.  We'll
> +come back to them later.

I think there's some room to improve the introduction here. Linking to
the previous post in the series is fine, but what I think is missing is
some context around the topic and setting some expectations for the
reader.

I'm not sure who you're pitching this post at, but I'll assume that you
want it to be accessible and interesting to people who don't know
anything about Guix, but maybe have some programing experience.

I think this introduction here [1] is a really good one. It's not too
long, but it puts the topic in some context, sets expectations, and does
all of that in a way that I think would be understood by someone who
doesn't know about Guix.

1: https://guix.gnu.org/en/blog/2021/the-big-change/

> +# Yes, No, Maybe So
> +
> +Let's instead implement another M of functional programming, _`maybe`_ values,
> +representing a value that may or may not exist.  `maybe` is a very common
> +feature of strongly-typed functional languages, and you'll see it all over the
> +place in Haskell and OCaml code. However, Guile is dynamically typed, so we
> +usually use ad-hoc `#f`s and `'()`s for null values instead of a proper
> +"optional" value.

I think the s's after the `#f` and `'()` here don't aid
readability. Something like:

  usually use ad-hoc `#false` and `'()` (empty list) values instead

> +Just for fun, though, we'll implement a proper `maybe` in Guile.  Fire up that
> +REPL once again, and let's import a bunch of modules that we'll need:

...

> +A more formal definition would be that a monad is a mathematical object composed
> +of three parts: a type, a `bind` function, and a `return` function.  So, how do
> +monads relate to Guix?
> +
> +# New Wheel, Old Wheel
> +
> +Now that we've reinvented the wheel, we'd better learn to use the original
> +wheel.  Guix provides a generic, high-level monads API, along with the two
> +generic monads `%identity-monad` and `%state-monad`, and the Guix-specific
> +`%store-monad`.  Since `maybe` is not one of them, let's integrate our version
> +into the Guix monad system!
> +
> +First we'll make the API available:
> +
> +```scheme
> +(use-modules (guix monads))
> +```
> +
> +To define a monad's API in Guix, we simply use the `define-monad` macro, and
> +provide two procedures: `bind`, and `return`.

At least when I read this, I'm drawn to the use of "API" numerous times
and keeping track of what's being talked about.

- Guix provides a generic, high-level monads API

Maybe "Guix includes a generic monads module providing syntax and types,
along with the two generic monads ..." would be more informative here.

- we'll make the API available

I'm not too fussed about this.

- To define a monad's API in Guix, we

Maybe API here refers to the same API as just mentioned previously, but
I guess you're now talking about a different API, but this is confusing.

I think it would be clearer to say "To define the maybe monad, we use
the define-monad macro.", then there's no need to keep track of what API
is being discussed. I'm also not sure it's useful to talk about things
within Guix as APIs unless you're talking about a specific case of using
Guix from some external program/software.

> +```scheme
> +(define-monad %maybe-monad
> +  (bind maybe-chain)
> +  (return something))
> +```
> +
> +`bind` is just the procedure that we use to compose monadic procedure calls
> +together, and `return` is the procedure that wraps values in the most basic form
> +of the monad.  A properly implemented `bind` and `return` must follow these
> +laws:

I think this would be confusing for someone who's encountering monads
for the first time. I think it's good to try and avoid going to deep,
but if there's mention of the "laws", I think it's important to say that
these laws come from category theory.

...

> +But Guix provides many higher-level APIs than `>>=` and `return`, as we will
> +see.  There's `mbegin`, which evaluates monadic expressions without binding them
> +to symbols, returning the last one:
> +
> +```scheme
> +(mbegin %maybe-monad
> +  (remove-a "abc"))
> +;; #<<maybe> is?: #t value: "bc">
> +```

This is stretching my understanding of monads here, but would this
example be better if the (mbegin bit included two expressions rather
than one?

> +And there's `mlet` and `mlet*`, which can bind them, and are essentially
> +equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`:

...

> +This is all well and good, you may be thinking, but why does Guix need a monad
> +API?  The answer is technically that it doesn't.  But building on the monad API
> +makes a lot of things much easier, and to learn why, we're going to look at one
> +of Guix's built-in monads.

The "API" returns. At least when I think of an "API" in the context of
Guix, I'm thinking of that interface providing a way to use Guix, from
an external prospective. Obviously that doesn't really match up with
what's going on here.

I think the point is still good here, but maybe it's simpler to say "but
why does Guix use monads?".

> +# In a State
> +
> +Guix implements a monad called `%state-monad`, and it works with single-argument
> +procedures returning two values.  Behold:
> +
> +```scheme
> +(with-monad %state-monad
> +  (return 33))
> +;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)>
> +```
> +
> +The `run-with-state` value turns this procedure into an actually useful value,
> +or, rather, two values:
> +
> +```scheme
> +(run-with-state (with-monad %state-monad (return 33))
> +  (list "foo" "bar" "baz"))
> +;; 33
> +;; ("foo" "bar" "baz")
> +```
> +
> +What can this actually do for us, though? Well, it gets interesting if we do
> +some `>>=`ing:
> +
> +```scheme
> +(define state-seq
> +  (mlet* %state-monad ((number (return 33)))
> +    (state-push number)))
> +result
> +;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)>
> +
> +(run-with-state state-seq (list 32))
> +;; (32)
> +;; (33 32)
> +
> +(run-with-state state-seq (list 30 99))
> +;; (30 99)
> +;; (33 30 99)
> +```
> +
> +What is `state-push`?  It's a monadic procedure for `%state-monad` that takes
> +whatever's currently in the first value (the primary value) and pushes it onto
> +the second value (the state value), which is assumed to be a list, returning the
> +old state value as the primary value and the new list as the state value.
> +
> +So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as
> +the initial state value, and then the `>>=` form passes that and `33` to
> +`state-push`.  What `%state-monad` allows us to do is thread together some
> +procedures that require some kind of state, while pretending the state isn't
> +there, and then retrieve both the final state and the result at the end!

I'm not sure the "pretending the state isn't there" but is helpful here,
if you're pretending the state doesn't exist, why is writing monadic
code helpful?

> +If you're a bit confused, don't worry.  We'll write some of our own
> +`%state-monad`-based monadic procedures and hopefully all will become clear.
> +Consider, for instance, the
> +[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in which
> +each value is computed by adding the previous two.  We could use the
> +`%state-monad` to compute Fibonacci numbers by storing the previous number as
> +the primary value and the number before that as the state value:

...

> +This is all very nifty, and possibly useful in general, but what does this have
> +to do with Guix?  Well, many Guix store-based operations are meant to be used
> +in concert with yet another monad, called the `%store-monad`.  But if we look at
> +`(guix store)`, where `%store-monad` is defined...
> +
> +```scheme
> +(define-alias %store-monad %state-monad)
> +(define-alias store-return state-return)
> +(define-alias store-bind state-bind)
> +```
> +
> +It was all a shallow façade!  All the "store monad" is is a special case of the
> +state monad, where a value representing the store is passed as the state value.
> +
> +# Lies, Damned Lies, and Abstractions
> +
> +We mentioned that, technically, we didn't need monads for Guix.  Indeed, many
> +(now deprecated) procedures take a store value as the argument, such as
> +`build-expression->derivation`.  However, using monads both helps ensure purity
> +and simply looks nicer.

I'm not sure what you mean by purity here?

> +`build-expression->derivation`, being deprecated, should never of course be
> +used.  For one thing, it uses the "quoted build expression" style, rather than
> +G-expressions (we'll discuss gexps another time).  The best way to create a
> +derivation from some basic build code is to use the new-fangled
> +`gexp->derivation` procedure:
> +
> +```scheme
> +(use-modules (guix gexp)
> +             (gnu packages irc))
> +
> +(define symlink-irssi
> +  (gexp->derivation "link-to-irssi"
> +    #~(symlink #$(file-append irssi "/bin/irssi") #$output)))
> +;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)>
> +```
> +
> +You don't have to understand the `#~(...)` form yet, only everything surrounding
> +it.  We can see that this `gexp->derivation` returns a procedure taking the
> +initial state (store), just like our `%state-monad` procedures did, and like we
> +used `run-with-state` to pass the initial state to a `%state-monad` monadic
> +value, we use our old friend `run-with-store` when we have a `%store-monad`
> +monadic value!
> +
> +```scheme
> +(define symlink-irssi-drv
> +  (with-store store
> +    (run-with-store store
> +      symlink-irssi)))
> +;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0>
> +```
> +
> +Let's just check this derivation is as expected by reading the code from the
> +builder script.
> +
> +```scheme
> +(define symlink-irssi-builder
> +  (list-ref (derivation-builder-arguments symlink-irssi-drv) 1))
> +
> +(call-with-input-file symlink-irssi-builder
> +  (lambda (port)
> +    (read port)))
> +    
> +;; (symlink
> +;;  "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi"
> +;;  ((@ (guile) getenv) "out"))
> +```
> +
> +And indeed, it symlinks the `irssi` binary to the output path.  Some other,
> +higher-level, monadic procedures include `interned-file`, which copies a file
> +from outside the store into it, and `text-file`, which copies some text into it.
> +Generally, these procedures aren't used, as there are higher-level procedures
> +that perform similar functions (which we will discuss later), but for the sake
> +of this blog post, here's an example:
> +
> +```scheme
> +(with-store store
> +  (run-with-store store
> +    (text-file "unmatched-paren"
> +      "( <paren <at> disroot.org>")))
> +;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren"
> +```

I think the build up to this section is pretty good, but then I'm not
sure what this last section is trying to explain.

Maybe at this point it would be good to leave the REPL and give some
concrete examples of non-trivial monadic code in Guix, and discuss what
that would look like if implemented without using monads.

> +# Conclusion
> +
> +What have we learned about monads?  The key points we can take away are:
> +
> +1. Monads are a way of composing together procedures and values that are wrapped
> +   in containers that give them extra context, like `maybe` values.
> +2. Guix provides a high-level monad API that compensates for Guile's lack of
> +   strong types or an interface-like system.

I'd say that Guile is a strongly typed language. I'm also not sure what
the point about compensating for something lacking in Guile means.

> +3. This API provides the state monad, which allows you to thread state through
> +   procedures such that you can pretend it doesn't exist.
> +4. Guix uses the store monad frequently to thread a store connection through
> +   procedures that need it.
> +5. The store monad is really just the state monad in disguise, where the state
> +   value is used to thread the store object through monadic procedures.

4 and 5 here are observations, but not very useful conclusions. I think
the more interesting question to ask is why are things implemented this
way?

Ideally the closing points would be well made in the previous section,
and this final bit would be a summary.

> +If you've read this post in its entirety but still don't yet quite get it, don't
> +worry.  Try to modify and tinker about with the examples, and hopefully it will
> +all click eventually!

Maybe this could be a call to get involved in the community (talk on IRC
or the mailing list?
[signature.asc (application/pgp-signature, inline)]

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Sun, 12 Feb 2023 12:01:02 GMT) Full text and rfc822 format available.

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Sun, 12 Feb 2023 14:18:01 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: "Christopher Baines" <mail <at> cbaines.net>
Cc: 61214 <at> debbugs.gnu.org, guix-patches <at> gnu.org
Subject: Re: [bug#61214] [PATCH guix-artwork v3] website: posts: Add
 Dissecting Guix, Part 2: The Store Monad.
Date: Sun, 12 Feb 2023 14:17:47 +0000
[Message part 1 (text/plain, inline)]
Hi,

On Sun Feb 12, 2023 at 10:47 AM GMT, Christopher Baines wrote:
> I think there's some room to improve the introduction here. Linking to
> the previous post in the series is fine, but what I think is missing is
> some context around the topic and setting some expectations for the
> reader.

> I'm not sure who you're pitching this post at

People who have used Guix and know basic Scheme but haven't delved into Guix's
interiors yet :)

> I think the s's after the `#f` and `'()` here don't aid
> readability. Something like:

Fair.

> I think it would be clearer to say "To define the maybe monad, we use
> the define-monad macro.", then there's no need to keep track of what API
> is being discussed. I'm also not sure it's useful to talk about things
> within Guix as APIs unless you're talking about a specific case of using
> Guix from some external program/software.

Good point.  Maybe I could say something like:

  "To define the maybe monad's behaviour, we use the define-monad macro."

using "behaviour" to describe the specifics of a monad.

> I think this would be confusing for someone who's encountering monads
> for the first time. I think it's good to try and avoid going to deep,
> but if there's mention of the "laws", I think it's important to say that
> these laws come from category theory.

Yeah, okay.

> > +
> > +```scheme
> > +(mbegin %maybe-monad
> > +  (remove-a "abc"))
> > +;; #<<maybe> is?: #t value: "bc">
> > +```
>
> This is stretching my understanding of monads here, but would this
> example be better if the (mbegin bit included two expressions rather
> than one?

I might just remove the MBEGIN example entirely.  I have no idea why MBEGIN exists,
or what advantages it confers, so I just included it for the sake of completeness
-.o.-  If someone could elaborate on what MBEGIN is for I would very much appreciate
it.

> I think the point is still good here, but maybe it's simpler to say "but
> why does Guix use monads?".

Okay.

> > +So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as
> > +the initial state value, and then the `>>=` form passes that and `33` to
> > +`state-push`.  What `%state-monad` allows us to do is thread together some
> > +procedures that require some kind of state, while pretending the state isn't
> > +there, and then retrieve both the final state and the result at the end!
>
> I'm not sure the "pretending the state isn't there" but is helpful here,
> if you're pretending the state doesn't exist, why is writing monadic
> code helpful?

Yeah, this doesn't really get across the point I'm trying to make.  I'm not sure
how else to word it, though...

> > +We mentioned that, technically, we didn't need monads for Guix.  Indeed, many
> > +(now deprecated) procedures take a store value as the argument, such as
> > +`build-expression->derivation`.  However, using monads both helps ensure purity
> > +and simply looks nicer.
>
> I'm not sure what you mean by purity here?

Me neither :P  Simon mentioned something about monads ensuring purity in their
review, which I didn't quite understand, so I just wrote something vague about it
(which I shouldn't have done).

> > +And indeed, it symlinks the `irssi` binary to the output path.  Some other,
> > +higher-level, monadic procedures include `interned-file`, which copies a file
> > +from outside the store into it, and `text-file`, which copies some text into it.
> > +Generally, these procedures aren't used, as there are higher-level procedures
> > +that perform similar functions (which we will discuss later), but for the sake
> > +of this blog post, here's an example:
> > +
> > +```scheme
> > +(with-store store
> > +  (run-with-store store
> > +    (text-file "unmatched-paren"
> > +      "( <paren <at> disroot.org>")))
> > +;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren"
> > +```
>
> I think the build up to this section is pretty good, but then I'm not
> sure what this last section is trying to explain.

It's just showing an example of the TEXT-FILE procedure, that's all :)

> Maybe at this point it would be good to leave the REPL and give some
> concrete examples of non-trivial monadic code in Guix, and discuss what
> that would look like if implemented without using monads.

Good idea! :)

> > +# Conclusion
> > +
> > +What have we learned about monads?  The key points we can take away are:
> > +
> > +1. Monads are a way of composing together procedures and values that are wrapped
> > +   in containers that give them extra context, like `maybe` values.
> > +2. Guix provides a high-level monad API that compensates for Guile's lack of
> > +   strong types or an interface-like system.
>
> I'd say that Guile is a strongly typed language. I'm also not sure what
> the point about compensating for something lacking in Guile means.

Guile doesn't have type definitions and it can't "fix" values to types.  I'd
consider that to be weak typing, personally :)

Regarding the point: it's supposed to say something like

> > +4. Guix uses the store monad frequently to thread a store connection through
> > +   procedures that need it.
> > +5. The store monad is really just the state monad in disguise, where the state
> > +   value is used to thread the store object through monadic procedures.
>
> 4 and 5 here are observations, but not very useful conclusions. I think
> the more interesting question to ask is why are things implemented this
> way?

> Ideally the closing points would be well made in the previous section,
> and this final bit would be a summary.

They're supposed to be a short summary of the main lessons the blog post
attempts to teach, but I'll consider removing them.

> > +If you've read this post in its entirety but still don't yet quite get it, don't
> > +worry.  Try to modify and tinker about with the examples, and hopefully it will
> > +all click eventually!
>
> Maybe this could be a call to get involved in the community (talk on IRC
> or the mailing list?

Yeah, good idea :)

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

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Sun, 12 Feb 2023 14:19:02 GMT) Full text and rfc822 format available.

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Sun, 12 Feb 2023 18:06:02 GMT) Full text and rfc822 format available.

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

From: Ludovic Courtès <ludo <at> gnu.org>
To: "(" <paren <at> disroot.org>
Cc: 61214 <at> debbugs.gnu.org
Subject: Re: bug#61214: [PATCH guix-artwork] website: posts: Add Dissecting
 Guix, Part 2: The Store Monad.
Date: Sun, 12 Feb 2023 19:05:43 +0100
Hello!

"(" <paren <at> disroot.org> skribis:

> * website/posts/dissecting-guix-2-store-monad.md: New blog post.

Nice work again!  Some comments below:

> +In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/),
> +we briefly mentioned the `with-store` and `run-with-store` APIs.  Today, we'll
> +be looking at those in further detail, along with the related monad API and the
> +`%store-monad`!

Like Chris, I’m wary of acronyms (they can easily make things
impenetrable) so I’d write:

  - the `with-store` macro and the `run-with-store` procedure
  - the related monad interface

> +Monads are a little hard to explain, and from a distance, they seem more than a
> +bit confusing.  So, I want you to erase monads from your mind for now.  We'll
> +come back to them later.

I agree with Chris’s comment: a few sentences of a higher-level intro
showing where this fits in the big picture would be great!

It would be nice to stress, also, that one doesn’t _need_ to know about
monads to use the various programming interfaces of Guix; instead, it’s
a thing for someone who’d like to get a deep dive into the internals of
Guix.

I’m saying this because we FP people, especially in the Haskell camp,
are sometimes very good at making things look fancy and complicated.
The last thing we’d want is to make it sound like this whole thing
targets an audience of people with a PhD in the field.  :-)

> +# Yes, No, Maybe So
> +
> +Let's instead implement another M of functional programming, _`maybe`_ values,
> +representing a value that may or may not exist.  `maybe` is a very common
                                                  ^
Maybe add something like “For example, one might write a function that
divides two integers such that it returns special value `nothing` when
the divisor is zero, and `maybe 5` when passed `15` and `3`.”  (I
couldn’t think of a better example but you get the idea.  :-))

> +feature of strongly-typed functional languages, and you'll see it all over the

s/strongly/statically/  :-)

> +place in Haskell and OCaml code. However, Guile is dynamically typed, so we
> +usually use ad-hoc `#f`s and `'()`s for null values instead of a proper
> +"optional" value.

In Scheme we use #f, not '(), to denote “Nothing”.

> +# New Wheel, Old Wheel
> +
> +Now that we've reinvented the wheel, we'd better learn to use the original
> +wheel.  Guix provides a generic, high-level monads API, along with the two
> +generic monads `%identity-monad` and `%state-monad`, and the Guix-specific
> +`%store-monad`.  Since `maybe` is not one of them, let's integrate our version
> +into the Guix monad system!
> +
> +First we'll make the API available:

Maybe “First, let’s import that module:”?

> +```scheme
> +(use-modules (guix monads))
> +```
> +
> +To define a monad's API in Guix, we simply use the `define-monad` macro, and

“To define a monad in Guix, we use `define-monad`”

(In general, I’d avoid “simply” because whether it’s “simple” depends on
one’s background.)

> +`bind` is just the procedure that we use to compose monadic procedure calls
> +together, and `return` is the procedure that wraps values in the most basic form
> +of the monad.  A properly implemented `bind` and `return` must follow these
> +laws:

s/these laws/the so-called _monad laws_/

(since you use that term below)

> +;; First law: the left identity.
> +(equal? (maybe-chain (something 0)
> +                     mlaws-proc-1)
> +        (mlaws-proc-1 0))
> +;; #t

Should it be “⇒ #t” instead of “;; #t”, to follow the convention used in
the manual and in other places?

> +What have we learned about monads?  The key points we can take away are:
> +
> +1. Monads are a way of composing together procedures and values that are wrapped
> +   in containers that give them extra context, like `maybe` values.
> +2. Guix provides a high-level monad API that compensates for Guile's lack of

s/monad API/monad module/ (or “monad library”)

> +   strong types or an interface-like system.

“static types”?

> +3. This API provides the state monad, which allows you to thread state through

s/This API/The `(guix monads)` module/

> +   procedures such that you can pretend it doesn't exist.
> +4. Guix uses the store monad frequently to thread a store connection through
> +   procedures that need it.
> +5. The store monad is really just the state monad in disguise, where the state
> +   value is used to thread the store object through monadic procedures.
> +
> +If you've read this post in its entirety but still don't yet quite get it, don't
> +worry.  Try to modify and tinker about with the examples, and hopefully it will
> +all click eventually!

Maybe link to the “The Store Monad” section of the manual here or
earlier?

I feel we’re asking a lot of work from you, but hopefully the result
will be even more pleasant.  I guess v4 will be ready to go!

Thanks for all the work,
Ludo’.




Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Sun, 12 Feb 2023 20:39:01 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: Ludovic Courtès <ludo <at> gnu.org>
Cc: 61214 <at> debbugs.gnu.org
Subject: Re: bug#61214: [PATCH guix-artwork] website: posts: Add Dissecting
 Guix, Part 2: The Store Monad.
Date: Sun, 12 Feb 2023 20:38:45 +0000
[Message part 1 (text/plain, inline)]
Heya,

On Sun Feb 12, 2023 at 6:05 PM GMT, Ludovic Courtès wrote:
> Like Chris, I’m wary of acronyms (they can easily make things
> impenetrable) so I’d write:
>
>   - the `with-store` macro and the `run-with-store` procedure
>   - the related monad interface

Wow, I really did use 'API' quite a lot... Oops :)

> I agree with Chris’s comment: a few sentences of a higher-level intro
> showing where this fits in the big picture would be great!

Okay, I'll try to figure something out for that.

> It would be nice to stress, also, that one doesn’t _need_ to know about
> monads to use the various programming interfaces of Guix; instead, it’s
> a thing for someone who’d like to get a deep dive into the internals of
> Guix.

Fair enough :)

> Maybe add something like “For example, one might write a function that
> divides two integers such that it returns special value `nothing` when
> the divisor is zero, and `maybe 5` when passed `15` and `3`.”  (I
> couldn’t think of a better example but you get the idea.  :-))

Okay.

> > +feature of strongly-typed functional languages, and you'll see it all over the
>
> s/strongly/statically/  :-)

Ahh, that's why everyone was pointing out the wording :)

> In Scheme we use #f, not '(), to denote “Nothing”.

Mhm, not sure why I added '() in retrospect.

> Maybe “First, let’s import that module:”?

Too much 'API'... :)

> “To define a monad in Guix, we use `define-monad`”
>
> (In general, I’d avoid “simply” because whether it’s “simple” depends on
> one’s background.)

Ah, of course.  I should've remembered this from last time :)

> s/these laws/the so-called _monad laws_/

Good idea.

> Should it be “⇒ #t” instead of “;; #t”, to follow the convention used in
> the manual and in other places?

That's much nicer, yeah.

> s/monad API/monad module/ (or “monad library”)

(insert thumbs up emoji here)

> > +   strong types or an interface-like system.
>
> “static types”?

Yep.

> s/This API/The `(guix monads)` module/

Okay.

> Maybe link to the “The Store Monad” section of the manual here or
> earlier?

Oh, did I not link to it before, in the "Lies, Damned Lies, and Abstractions"
section!?

* unmatched-paren checks

...oops.

> I feel we’re asking a lot of work from you, but hopefully the result
> will be even more pleasant.  I guess v4 will be ready to go!

Hopefully! :)

> Thanks for all the work,

And thanks for all your own work! :)

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

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Mon, 13 Feb 2023 12:11:01 GMT) Full text and rfc822 format available.

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

From: zimoun <zimon.toutoune <at> gmail.com>
To: Christopher Baines <mail <at> cbaines.net>, "(" <paren <at> disroot.org>
Cc: 61214 <at> debbugs.gnu.org
Subject: Re: [bug#61214] [PATCH guix-artwork v3] website: posts: Add
 Dissecting Guix, Part 2: The Store Monad.
Date: Mon, 13 Feb 2023 13:08:33 +0100
Hi Chris,

On Sun, 12 Feb 2023 at 10:47, Christopher Baines <mail <at> cbaines.net> wrote:

> I'd say that Guile is a strongly typed language. I'm also not sure what
> the point about compensating for something lacking in Guile means.

From my understanding, “strongly typed” is poorly defined and there is
no strong consensus.  The Guile compiler accepts this,

    (define (bang) (+ 1 "0"))

and typed language folks say it should not be possible for a decent
compiler supporting “strongly typed” language.

From my point of view, it is better to speak about dynamically typed and
statically typed where definition is clearer.

About the remark about “compensating“, I guess the point is that using
language with a powerful type system, this monad story is somehow
included in the type system machinery; for instance Haskell.  Since
Guile does not have such type system – but instead it has macros ;-) –
then the monad story needs an implementation for its own.

(Aside, is monad another way to see macro? [1] :-))

Cheers,
simon


1: <http://kawagner.blogspot.com/2007/02/understanding-monads-for-real.html>




Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Tue, 14 Feb 2023 07:32:01 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: 61214 <at> debbugs.gnu.org
Cc: "\(" <paren <at> disroot.org>, ludo <at> gnu.org, mail <at> cbaines.net,
 zimoun.toutoune <at> gmail.com
Subject: [PATCH guix-artwork v4] website: posts: Add Dissecting Guix,
 Part 2: The Store Monad.
Date: Tue, 14 Feb 2023 07:30:49 +0000
* website/posts/dissecting-guix-2-store-monad.md: New blog post.
---
Heya,

Addressing criticism from Chris and Ludo:

* Don't use 'API' ad nauseum.
* Use ⇒ instead of ;; for return values.
* Provide an example of where 'maybe' could be useful when introducing it.
* Say 'statically-typed' rather than 'strongly-typed'.
* Add high-level explanation of the purpose of monads to the introduction.
* Don't say that we sometimes use '() as a 'nothing' value.
* Talk about 'the so-called monads laws' rather than 'these laws'.
* Link to the 'The Store Monad' section of the manual in the introduction.
* Remove the MBEGIN example, as it's essentially pointless.  Explain how it's
  only useful if the operations have side effects.
* Don't say we ignore the state; instead, we basically pretend it's a global
  variable.
* Just say that monads are more elegant rather than more pure.
* Note that you can ask any questions on IRC or the mailing list :)

 .../posts/dissecting-guix-2-store-monad.md    | 557 ++++++++++++++++++
 1 file changed, 557 insertions(+)
 create mode 100644 website/posts/dissecting-guix-2-store-monad.md

diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md
new file mode 100644
index 0000000..a27a28b
--- /dev/null
+++ b/website/posts/dissecting-guix-2-store-monad.md
@@ -0,0 +1,557 @@
+title: Dissecting Guix, Part 2: The Store Monad
+date: TBC
+author: (
+tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API
+---
+Hello again!
+
+In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/),
+we briefly mentioned the `with-store` and `run-with-store` APIs.  Today, we'll
+be looking at those in further detail, along with the related monad API and the
+`%store-monad`!
+
+Monads are a little hard to explain, and from a distance, they seem more than a
+bit confusing.  So, I want you to erase monads from your mind for now.  We'll
+come back to them later.
+
+# Yes, No, Maybe So
+
+Let's instead implement another M of functional programming, _`maybe`_ values,
+representing a value that may or may not exist.  `maybe` is a very common
+feature of strongly-typed functional languages, and you'll see it all over the
+place in Haskell and OCaml code. However, Guile is dynamically typed, so we
+usually use ad-hoc `#f`s and `'()`s for null values instead of a proper
+"optional" value.
+
+Just for fun, though, we'll implement a proper `maybe` in Guile.  Fire up that
+REPL once again, and let's import a bunch of modules that we'll need:
+
+```scheme
+(use-modules (ice-9 match)
+             (srfi srfi-9))
+```
+
+We'll implement `maybe` as a record with two fields, `is?` and `value`.  If the
+value contains something, `is?` will be `#t` and `value` will contain the thing
+in question, and if it's empty, `is?`'ll be `#f`.
+
+```scheme
+(define-record-type <maybe>
+  (make-maybe is? value)
+  maybe?
+  (is? maybe-is?)
+  (value maybe-value))
+```
+
+Now we'll define constructors for the two possible states:
+
+```scheme
+(define (something value)
+  (make-maybe #t value))
+
+(define (nothing)
+  (make-maybe #f #f)) ;the value here doesn't matter; we'll just use #f
+```
+
+And make some silly functions that return optional values:
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+      
+(remove-a "ahh")
+;; #<<maybe> is?: #t value: "hh">
+
+(remove-a "ooh")
+;; #<<maybe> is?: #f value: #f>
+
+(remove-b "bad")
+;; #<<maybe> is?: #t value: "ad">
+```
+
+But what if we want to compose the results of these functions?
+
+# Keeping Your Composure
+
+As you might have guessed, this is not fun.  Cosplaying as a compiler backend
+typically isn't.
+
+```scheme
+(let ((t1 (remove-a "abcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+;; #<<maybe> is?: #t value: "cd">
+
+(let ((t1 (remove-a "bbcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+;; #<<maybe> is?: #f value: #f>
+```
+
+I can almost hear the heckling.  Even worse, composing three:
+
+```scheme
+(let* ((t1 (remove-a "abad"))
+       (t2 (if (maybe-is? t1)
+               (remove-b (maybe-value t1))
+               (nothing))))
+  (if (maybe-is? t2)
+      (remove-a (maybe-value t2))
+      (nothing)))
+;; #<<maybe> is?: #t value: "d">
+```
+
+So, how do we go about making this more bearable?  Well, one way could be to
+make `remove-a` and `remove-b` accept `maybe`s:
+
+```scheme
+(define (remove-a ?str)
+  (match ?str
+    (($ <maybe> #t str)
+     (if (eq? (string-ref str 0) #\a)
+         (something (substring str 1))
+         (nothing)))
+    (_ (nothing))))
+
+(define (remove-b ?str)
+  (match ?str
+    (($ <maybe> #t str)
+     (if (eq? (string-ref str 0) #\b)
+         (something (substring str 1))
+         (nothing)))
+    (_ (nothing))))
+```
+
+Not at all pretty, but it works!
+
+```
+(remove-b (remove-a (something "abc")))
+;; #<<maybe> is?: #t value: "c">
+```
+
+Still, our procedures now require quite a bit of boilerplate.  Might there be a
+better way?
+
+# The Ties That `>>=` Us
+
+First of all, we'll revert to our original definitions of `remove-a` and
+`remove-b`, that is to say, the ones that take a regular value and return a
+`maybe`.
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+```
+
+What if tried introducing higher-order procedures (procedures that accept other
+procedures as arguments) into the equation?  Because we're functional
+programmers and we have an unhealthy obsession with that sort of thing.
+
+```scheme
+(define (maybe-chain maybe proc)
+  (if (maybe-is? maybe)
+      (proc (maybe-value maybe))
+      (nothing)))
+
+(maybe-chain (something "abc")
+             remove-a)
+;; #<<maybe> is?: #t value: "bc">
+
+(maybe-chain (nothing)
+             remove-a)
+;; #<<maybe> is?: #f value: #f>
+```
+
+It lives!  To make it easier to compose procedures like this, we'll define a
+macro that allows us to perform any number of sequenced operations with only one
+composition form:
+
+```scheme
+(define-syntax maybe-chain*
+  (syntax-rules ()
+    ((_ maybe proc)
+     (maybe-chain maybe proc))
+    ((_ maybe proc rest ...)
+     (maybe-chain* (maybe-chain maybe proc)
+                   rest ...))))
+
+(maybe-chain* (something "abad")
+              remove-a
+              remove-b
+              remove-a)
+;; #<<maybe> is?: #t value: "d">
+```
+
+Congratulations, you've just implemented the `bind` operation, commonly written
+as `>>=`, for our `maybe` type.  And it turns out that a monad is just any
+container-like value for which `>>=` (along with another procedure called
+`return`, which wraps a given value in the simplest possible form of a monad)
+has been implemented.
+
+A more formal definition would be that a monad is a mathematical object composed
+of three parts: a type, a `bind` function, and a `return` function.  So, how do
+monads relate to Guix?
+
+# New Wheel, Old Wheel
+
+Now that we've reinvented the wheel, we'd better learn to use the original
+wheel.  Guix provides a generic, high-level monads API, along with the two
+generic monads `%identity-monad` and `%state-monad`, and the Guix-specific
+`%store-monad`.  Since `maybe` is not one of them, let's integrate our version
+into the Guix monad system!
+
+First we'll make the API available:
+
+```scheme
+(use-modules (guix monads))
+```
+
+To define a monad's API in Guix, we simply use the `define-monad` macro, and
+provide two procedures: `bind`, and `return`.
+
+```scheme
+(define-monad %maybe-monad
+  (bind maybe-chain)
+  (return something))
+```
+
+`bind` is just the procedure that we use to compose monadic procedure calls
+together, and `return` is the procedure that wraps values in the most basic form
+of the monad.  A properly implemented `bind` and `return` must follow these
+laws:
+
+1. `(bind (return x) proc)` must be equivalent to `(proc x)`.
+2. `(bind monad return)` must be equivalent to just `monad`.
+3. `(bind (bind monad proc-1) proc-2)` must be equivalent to
+   `(bind monad (lambda (x) (bind (proc-1 x) proc-2)))`.
+
+Let's verify that our `maybe-chain` and `something` procedures adhere to the
+monad laws:
+
+```scheme
+(define (mlaws-proc-1 x)
+  (something (+ x 1)))
+
+(define (mlaws-proc-2 x)
+  (something (+ x 2)))
+  
+;; First law: the left identity.
+(equal? (maybe-chain (something 0)
+                     mlaws-proc-1)
+        (mlaws-proc-1 0))
+;; #t
+ 
+;; Second law: the right identity.
+(equal? (maybe-chain (something 0)
+                     something)
+        (something 0))
+;; #t
+
+;; Third law: associativity.
+(equal? (maybe-chain (maybe-chain (something 0)
+                                  mlaws-proc-1)
+                     mlaws-proc-2)
+        (maybe-chain (something 0)
+                     (lambda (x)
+                       (maybe-chain (mlaws-proc-1 x)
+                                    mlaws-proc-2))))
+;; #t
+```
+
+Now that we know they're valid, we can use the `with-monad` macro to tell Guix
+to use these specific implementations of `bind` and `return`, and the `>>=`
+macro to thread monads through procedure calls!
+
+```scheme
+(with-monad %maybe-monad
+  (>>= (something "aabbc")
+       remove-a
+       remove-a
+       remove-b
+       remove-b))
+;; #<<maybe> is?: #t value: "c">
+```
+
+We can also now use `return`:
+
+```scheme
+(with-monad %maybe-monad
+  (return 32))
+;; #<<maybe> is?: #t value: 32>
+```
+
+But Guix provides many higher-level APIs than `>>=` and `return`, as we will
+see.  There's `mbegin`, which evaluates monadic expressions without binding them
+to symbols, returning the last one:
+
+```scheme
+(mbegin %maybe-monad
+  (remove-a "abc"))
+;; #<<maybe> is?: #t value: "bc">
+```
+
+And there's `mlet` and `mlet*`, which can bind them, and are essentially
+equivalent to a chain of `(>>= MEXPR (lambda (BINDING) ...))`:
+
+```scheme
+;; This is equivalent...
+(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol
+                     (str1 (remove-a str))
+                     (str2 (remove-b str)))
+  (remove-a str))
+;; #<<maybe> is?: #t value: "d">
+
+;; ...to this:
+(with-monad %maybe-monad
+  (>>= (return "abad")
+       (lambda (str)
+         (remove-a str))
+       (lambda (str1)
+         (remove-b str))
+       (lambda (str2)
+         (remove-a str))))
+```
+
+Various abstractions over these two exist too, such as `mwhen` (a `when` plus an
+`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize`
+(dynamically-scoped value rebinding, like `parameterize`, in a monadic context).
+`lift` takes a procedure and a monad and creates a new procedure that returns
+a monadic value.
+
+There are also APIs for manipulating lists wrapped in monads; `listm` creates
+such a list, `sequence` turns a list of monads into a list wrapped in a monad,
+and the `anym`, `mapm`, and `foldm` procedures are like their non-monadic
+equivalents, except that they return lists wrapped in monads.
+
+This is all well and good, you may be thinking, but why does Guix need a monad
+API?  The answer is technically that it doesn't.  But building on the monad API
+makes a lot of things much easier, and to learn why, we're going to look at one
+of Guix's built-in monads.
+
+# In a State
+
+Guix implements a monad called `%state-monad`, and it works with single-argument
+procedures returning two values.  Behold:
+
+```scheme
+(with-monad %state-monad
+  (return 33))
+;; #<procedure 21dc9a0 at <unknown port>:1106:22 (state)>
+```
+
+The `run-with-state` value turns this procedure into an actually useful value,
+or, rather, two values:
+
+```scheme
+(run-with-state (with-monad %state-monad (return 33))
+  (list "foo" "bar" "baz"))
+;; 33
+;; ("foo" "bar" "baz")
+```
+
+What can this actually do for us, though? Well, it gets interesting if we do
+some `>>=`ing:
+
+```scheme
+(define state-seq
+  (mlet* %state-monad ((number (return 33)))
+    (state-push number)))
+result
+;; #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)>
+
+(run-with-state state-seq (list 32))
+;; (32)
+;; (33 32)
+
+(run-with-state state-seq (list 30 99))
+;; (30 99)
+;; (33 30 99)
+```
+
+What is `state-push`?  It's a monadic procedure for `%state-monad` that takes
+whatever's currently in the first value (the primary value) and pushes it onto
+the second value (the state value), which is assumed to be a list, returning the
+old state value as the primary value and the new list as the state value.
+
+So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as
+the initial state value, and then the `>>=` form passes that and `33` to
+`state-push`.  What `%state-monad` allows us to do is thread together some
+procedures that require some kind of state, while pretending the state isn't
+there, and then retrieve both the final state and the result at the end!
+
+If you're a bit confused, don't worry.  We'll write some of our own
+`%state-monad`-based monadic procedures and hopefully all will become clear.
+Consider, for instance, the
+[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in which
+each value is computed by adding the previous two.  We could use the
+`%state-monad` to compute Fibonacci numbers by storing the previous number as
+the primary value and the number before that as the state value:
+
+```scheme
+(define (fibonacci-thing value)
+  (lambda (state)
+    (values (+ value state)
+            value)))
+```
+
+Now we can feed our Fibonacci-generating procedure the first value using
+`run-with-state` and the second using `return`:
+
+```scheme
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1)))
+      (fibonacci-thing n2))
+  0)
+;; 3
+;; 2
+
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1))
+                         (n3 (fibonacci-thing n2))
+                         (n4 (fibonacci-thing n3))
+                         (n5 (fibonacci-thing n4)))
+      (fibonacci-thing n5))
+  0)
+;; 13
+;; 8
+```
+
+This is all very nifty, and possibly useful in general, but what does this have
+to do with Guix?  Well, many Guix store-based operations are meant to be used
+in concert with yet another monad, called the `%store-monad`.  But if we look at
+`(guix store)`, where `%store-monad` is defined...
+
+```scheme
+(define-alias %store-monad %state-monad)
+(define-alias store-return state-return)
+(define-alias store-bind state-bind)
+```
+
+It was all a shallow façade!  All the "store monad" is is a special case of the
+state monad, where a value representing the store is passed as the state value.
+
+# Lies, Damned Lies, and Abstractions
+
+We mentioned that, technically, we didn't need monads for Guix.  Indeed, many
+(now deprecated) procedures take a store value as the argument, such as
+`build-expression->derivation`.  However, using monads both helps ensure purity
+and simply looks nicer.
+
+`build-expression->derivation`, being deprecated, should never of course be
+used.  For one thing, it uses the "quoted build expression" style, rather than
+G-expressions (we'll discuss gexps another time).  The best way to create a
+derivation from some basic build code is to use the new-fangled
+`gexp->derivation` procedure:
+
+```scheme
+(use-modules (guix gexp)
+             (gnu packages irc))
+
+(define symlink-irssi
+  (gexp->derivation "link-to-irssi"
+    #~(symlink #$(file-append irssi "/bin/irssi") #$output)))
+;; #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)>
+```
+
+You don't have to understand the `#~(...)` form yet, only everything surrounding
+it.  We can see that this `gexp->derivation` returns a procedure taking the
+initial state (store), just like our `%state-monad` procedures did, and like we
+used `run-with-state` to pass the initial state to a `%state-monad` monadic
+value, we use our old friend `run-with-store` when we have a `%store-monad`
+monadic value!
+
+```scheme
+(define symlink-irssi-drv
+  (with-store store
+    (run-with-store store
+      symlink-irssi)))
+;; #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0>
+```
+
+Let's just check this derivation is as expected by reading the code from the
+builder script.
+
+```scheme
+(define symlink-irssi-builder
+  (list-ref (derivation-builder-arguments symlink-irssi-drv) 1))
+
+(call-with-input-file symlink-irssi-builder
+  (lambda (port)
+    (read port)))
+    
+;; (symlink
+;;  "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi"
+;;  ((@ (guile) getenv) "out"))
+```
+
+And indeed, it symlinks the `irssi` binary to the output path.  Some other,
+higher-level, monadic procedures include `interned-file`, which copies a file
+from outside the store into it, and `text-file`, which copies some text into it.
+Generally, these procedures aren't used, as there are higher-level procedures
+that perform similar functions (which we will discuss later), but for the sake
+of this blog post, here's an example:
+
+```scheme
+(with-store store
+  (run-with-store store
+    (text-file "unmatched-paren"
+      "( <paren <at> disroot.org>")))
+;; "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren"
+```
+
+# Conclusion
+
+What have we learned about monads?  The key points we can take away are:
+
+1. Monads are a way of composing together procedures and values that are wrapped
+   in containers that give them extra context, like `maybe` values.
+2. Guix provides a high-level monad API that compensates for Guile's lack of
+   strong types or an interface-like system.
+3. This API provides the state monad, which allows you to thread state through
+   procedures such that you can pretend it doesn't exist.
+4. Guix uses the store monad frequently to thread a store connection through
+   procedures that need it.
+5. The store monad is really just the state monad in disguise, where the state
+   value is used to thread the store object through monadic procedures.
+
+If you've read this post in its entirety but still don't yet quite get it, don't
+worry.  Try to modify and tinker about with the examples, and hopefully it will
+all click eventually!
+
+#### About GNU Guix
+
+[GNU Guix](https://guix.gnu.org) is a transactional package manager and
+an advanced distribution of the GNU system that [respects user
+freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html).
+Guix can be used on top of any system running the Hurd or the Linux
+kernel, or it can be used as a standalone operating system distribution
+for i686, x86_64, ARMv7, AArch64 and POWER9 machines.
+
+In addition to standard package management features, Guix supports
+transactional upgrades and roll-backs, unprivileged package management,
+per-user profiles, and garbage collection.  When used as a standalone
+GNU/Linux distribution, Guix offers a declarative, stateless approach to
+operating system configuration management.  Guix is highly customizable
+and hackable through [Guile](https://www.gnu.org/software/guile)
+programming interfaces and extensions to the
+[Scheme](http://schemers.org) language.

base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3
-- 
2.39.1





Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Tue, 14 Feb 2023 07:34:02 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: 61214 <at> debbugs.gnu.org
Cc: "\(" <paren <at> disroot.org>, ludo <at> gnu.org, mail <at> cbaines.net,
 zimoun.toutoune <at> gmail.com
Subject: [PATCH guix-artwork v5] website: posts: Add Dissecting Guix,
 Part 2: The Store Monad.
Date: Tue, 14 Feb 2023 07:33:14 +0000
* website/posts/dissecting-guix-2-store-monad.md: New blog post.
---
Oops, forgot to ``git commit -a''... :/  See v4 for the changelog.

 .../posts/dissecting-guix-2-store-monad.md    | 567 ++++++++++++++++++
 1 file changed, 567 insertions(+)
 create mode 100644 website/posts/dissecting-guix-2-store-monad.md

diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md
new file mode 100644
index 0000000..13b9cbb
--- /dev/null
+++ b/website/posts/dissecting-guix-2-store-monad.md
@@ -0,0 +1,567 @@
+title: Dissecting Guix, Part 2: The Store Monad
+date: TBC
+author: (
+tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API
+---
+Hello again!
+
+In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/),
+we briefly mentioned the `with-store` and `run-with-store` macros.  Today, we'll
+be looking at those in further detail, along with the related monad library and
+the [`%store-monad`](https://guix.gnu.org/manual/devel/en/html_node/The-Store-Monad.html)!
+
+Typically, we use monads to chain operations together, and the `%store-monad` is
+no different; it's used to combine operations that work on the Guix store (for
+instance, creating derivations, building derivations, or adding data files to
+the store).
+
+However, monads are a little hard to explain, and from a distance, they seem to
+be quite incomprehensible.  So, I want you to erase them from your mind for now.
+We'll come back to them later.  And be aware that if you can't seem to get your
+head around them, it's okay; you can understand most of the architecture of Guix
+without understanding monads.
+
+# Yes, No, Maybe So
+
+Let's instead implement another M of functional programming, _`maybe`_ values,
+representing a value that may or may not exist.  For instance, there could be a
+procedure that attempts to pop a stack, returing the result if there is one, or
+`nothing` if the stack has no elements.
+
+`maybe` is a very common feature of statically-typed functional languages, and
+you'll see it all over the place in Haskell and OCaml code. However, Guile is
+dynamically typed, so we usually use ad-hoc `#f` values as the "null value"
+instead of a proper "nothing" or "none".
+
+Just for fun, though, we'll implement a proper `maybe` in Guile.  Fire up that
+REPL once again, and let's import a bunch of modules that we'll need:
+
+```scheme
+(use-modules (ice-9 match)
+             (srfi srfi-9))
+```
+
+We'll implement `maybe` as a record with two fields, `is?` and `value`.  If the
+value contains something, `is?` will be `#t` and `value` will contain the thing
+in question, and if it's empty, `is?`'ll be `#f`.
+
+```scheme
+(define-record-type <maybe>
+  (make-maybe is? value)
+  maybe?
+  (is? maybe-is?)
+  (value maybe-value))
+```
+
+Now we'll define constructors for the two possible states:
+
+```scheme
+(define (something value)
+  (make-maybe #t value))
+
+(define (nothing)
+  (make-maybe #f #f)) ;the value here doesn't matter; we'll just use #f
+```
+
+And make some silly functions that return optional values:
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+      
+(remove-a "ahh")
+⇒ #<<maybe> is?: #t value: "hh">
+
+(remove-a "ooh")
+⇒ #<<maybe> is?: #f value: #f>
+
+(remove-b "bad")
+⇒ #<<maybe> is?: #t value: "ad">
+```
+
+But what if we want to compose the results of these functions?
+
+# Keeping Your Composure
+
+As you might have guessed, this is not fun.  Cosplaying as a compiler backend
+typically isn't.
+
+```scheme
+(let ((t1 (remove-a "abcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+⇒ #<<maybe> is?: #t value: "cd">
+
+(let ((t1 (remove-a "bbcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+⇒ #<<maybe> is?: #f value: #f>
+```
+
+I can almost hear the heckling.  Even worse, composing three:
+
+```scheme
+(let* ((t1 (remove-a "abad"))
+       (t2 (if (maybe-is? t1)
+               (remove-b (maybe-value t1))
+               (nothing))))
+  (if (maybe-is? t2)
+      (remove-a (maybe-value t2))
+      (nothing)))
+⇒ #<<maybe> is?: #t value: "d">
+```
+
+So, how do we go about making this more bearable?  Well, one way could be to
+make `remove-a` and `remove-b` accept `maybe`s:
+
+```scheme
+(define (remove-a ?str)
+  (match ?str
+    (($ <maybe> #t str)
+     (if (eq? (string-ref str 0) #\a)
+         (something (substring str 1))
+         (nothing)))
+    (_ (nothing))))
+
+(define (remove-b ?str)
+  (match ?str
+    (($ <maybe> #t str)
+     (if (eq? (string-ref str 0) #\b)
+         (something (substring str 1))
+         (nothing)))
+    (_ (nothing))))
+```
+
+Not at all pretty, but it works!
+
+```scheme
+(remove-b (remove-a (something "abc")))
+⇒ #<<maybe> is?: #t value: "c">
+```
+
+Still, our procedures now require quite a bit of boilerplate.  Might there be a
+better way?
+
+# The Ties That `>>=` Us
+
+First of all, we'll revert to our original definitions of `remove-a` and
+`remove-b`, that is to say, the ones that take a regular value and return a
+`maybe`.
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+```
+
+What if tried introducing higher-order procedures (procedures that accept other
+procedures as arguments) into the equation?  Because we're functional
+programmers and we have an unhealthy obsession with that sort of thing.
+
+```scheme
+(define (maybe-chain maybe proc)
+  (if (maybe-is? maybe)
+      (proc (maybe-value maybe))
+      (nothing)))
+
+(maybe-chain (something "abc")
+             remove-a)
+⇒ #<<maybe> is?: #t value: "bc">
+
+(maybe-chain (nothing)
+             remove-a)
+⇒ #<<maybe> is?: #f value: #f>
+```
+
+It lives!  To make it easier to compose procedures like this, we'll define a
+macro that allows us to perform any number of sequenced operations with only one
+composition form:
+
+```scheme
+(define-syntax maybe-chain*
+  (syntax-rules ()
+    ((_ maybe proc)
+     (maybe-chain maybe proc))
+    ((_ maybe proc rest ...)
+     (maybe-chain* (maybe-chain maybe proc)
+                   rest ...))))
+
+(maybe-chain* (something "abad")
+              remove-a
+              remove-b
+              remove-a)
+⇒ #<<maybe> is?: #t value: "d">
+```
+
+Congratulations, you've just implemented the `bind` operation, commonly written
+as `>>=`, for our `maybe` type.  And it turns out that a monad is just any
+container-like value for which `>>=` (along with another procedure called
+`return`, which wraps a given value in the simplest possible form of a monad)
+has been implemented.
+
+A more formal definition would be that a monad is a mathematical object composed
+of three parts: a type, a `bind` function, and a `return` function.  So, how do
+monads relate to Guix?
+
+# New Wheel, Old Wheel
+
+Now that we've reinvented the wheel, we'd better learn to use the original
+wheel.  Guix provides a generic, high-level monads library, along with the two
+generic monads `%identity-monad` and `%state-monad`, and the Guix-specific
+`%store-monad`.  Since `maybe` is not one of them, let's integrate our version
+into the Guix monad system!
+
+First we'll import the module that provides the aforementioned library:
+
+```scheme
+(use-modules (guix monads))
+```
+
+To define a monad's behaviour in Guix, we simply use the `define-monad` macro,
+and provide two procedures: `bind`, and `return`.
+
+```scheme
+(define-monad %maybe-monad
+  (bind maybe-chain)
+  (return something))
+```
+
+`bind` is just the procedure that we use to compose monadic procedure calls
+together, and `return` is the procedure that wraps values in the most basic form
+of the monad.  A properly implemented `bind` and `return` must follow the
+so-called _monad laws_:
+
+1. `(bind (return x) proc)` must be equivalent to `(proc x)`.
+2. `(bind monad return)` must be equivalent to just `monad`.
+3. `(bind (bind monad proc-1) proc-2)` must be equivalent to
+   `(bind monad (lambda (x) (bind (proc-1 x) proc-2)))`.
+
+Let's verify that our `maybe-chain` and `something` procedures adhere to the
+monad laws:
+
+```scheme
+(define (mlaws-proc-1 x)
+  (something (+ x 1)))
+
+(define (mlaws-proc-2 x)
+  (something (+ x 2)))
+  
+;; First law: the left identity.
+(equal? (maybe-chain (something 0)
+                     mlaws-proc-1)
+        (mlaws-proc-1 0))
+⇒ #t
+ 
+;; Second law: the right identity.
+(equal? (maybe-chain (something 0)
+                     something)
+        (something 0))
+⇒ #t
+
+;; Third law: associativity.
+(equal? (maybe-chain (maybe-chain (something 0)
+                                  mlaws-proc-1)
+                     mlaws-proc-2)
+        (maybe-chain (something 0)
+                     (lambda (x)
+                       (maybe-chain (mlaws-proc-1 x)
+                                    mlaws-proc-2))))
+⇒ #t
+```
+
+Now that we know they're valid, we can use the `with-monad` macro to tell Guix
+to use these specific implementations of `bind` and `return`, and the `>>=`
+macro to thread monads through procedure calls!
+
+```scheme
+(with-monad %maybe-monad
+  (>>= (something "aabbc")
+       remove-a
+       remove-a
+       remove-b
+       remove-b))
+⇒ #<<maybe> is?: #t value: "c">
+```
+
+We can also now use `return`:
+
+```scheme
+(with-monad %maybe-monad
+  (return 32))
+⇒ #<<maybe> is?: #t value: 32>
+```
+
+But Guix provides many higher-level interfaces than `>>=` and `return`, as we
+will see.  There's `mbegin`, which evaluates monadic expressions without binding
+them to symbols, returning the last one.  This, however, isn't particularly
+useful with our `%maybe-monad`, as it's only really usable if the monadic
+operations within have side effects, just like the non-monadic `begin`.
+
+There's also `mlet` and `mlet*`, which _do_ bind the results of monadic
+expressions to symbols, and are essentially equivalent to a chain of
+`(>>= MEXPR (lambda (BINDING) ...))`:
+
+```scheme
+;; This is equivalent...
+(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol
+                     (str1 (remove-a str))
+                     (str2 (remove-b str)))
+  (remove-a str))
+⇒ #<<maybe> is?: #t value: "d">
+
+;; ...to this:
+(with-monad %maybe-monad
+  (>>= (return "abad")
+       (lambda (str)
+         (remove-a str))
+       (lambda (str1)
+         (remove-b str))
+       (lambda (str2)
+         (remove-a str))))
+```
+
+Various abstractions over these two exist too, such as `mwhen` (a `when` plus an
+`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize`
+(dynamically-scoped value rebinding, like `parameterize`, in a monadic context).
+`lift` takes a procedure and a monad and creates a new procedure that returns
+a monadic value.
+
+There are also interfaces for manipulating lists wrapped in monads; `listm`
+creates such a list, `sequence` turns a list of monads into a list wrapped in a
+monad, and the `anym`, `mapm`, and `foldm` procedures are like their non-monadic
+equivalents, except that they return lists wrapped in monads.
+
+This is all well and good, you may be thinking, but why does Guix need a monad
+library, anyway?  The answer is technically that it doesn't.  But building on
+the monad API makes a lot of things much easier, and to learn why, we're going
+to look at one of Guix's built-in monads.
+
+# In a State
+
+Guix implements a monad called `%state-monad`, and it works with single-argument
+procedures returning two values.  Behold:
+
+```scheme
+(with-monad %state-monad
+  (return 33))
+⇒ #<procedure 21dc9a0 at <unknown port>:1106:22 (state)>
+```
+
+The `run-with-state` value turns this procedure into an actually useful value,
+or, rather, two values:
+
+```scheme
+(run-with-state (with-monad %state-monad (return 33))
+  (list "foo" "bar" "baz"))
+⇒ 33
+⇒ ("foo" "bar" "baz")
+```
+
+What can this actually do for us, though? Well, it gets interesting if we do
+some `>>=`ing:
+
+```scheme
+(define state-seq
+  (mlet* %state-monad ((number (return 33)))
+    (state-push number)))
+result
+⇒ #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)>
+
+(run-with-state state-seq (list 32))
+⇒ (32)
+⇒ (33 32)
+
+(run-with-state state-seq (list 30 99))
+⇒ (30 99)
+⇒ (33 30 99)
+```
+
+What is `state-push`?  It's a monadic procedure for `%state-monad` that takes
+whatever's currently in the first value (the primary value) and pushes it onto
+the second value (the state value), which is assumed to be a list, returning the
+old state value as the primary value and the new list as the state value.
+
+So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as
+the initial state value, and then the `>>=` form passes that and `33` to
+`state-push`.  What `%state-monad` allows us to do is thread together some
+procedures that require some kind of state, while essentially pretending the
+state value is stored globally, like you might do in, say, C, and then retrieve
+both the final state and the result at the end!
+
+If you're a bit confused, don't worry.  We'll write some of our own
+`%state-monad`-based monadic procedures and hopefully all will become clear.
+Consider, for instance, the
+[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in which
+each value is computed by adding the previous two.  We could use the
+`%state-monad` to compute Fibonacci numbers by storing the previous number as
+the primary value and the number before that as the state value:
+
+```scheme
+(define (fibonacci-thing value)
+  (lambda (state)
+    (values (+ value state)
+            value)))
+```
+
+Now we can feed our Fibonacci-generating procedure the first value using
+`run-with-state` and the second using `return`:
+
+```scheme
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1)))
+      (fibonacci-thing n2))
+  0)
+⇒ 3
+⇒ 2
+
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1))
+                         (n3 (fibonacci-thing n2))
+                         (n4 (fibonacci-thing n3))
+                         (n5 (fibonacci-thing n4)))
+      (fibonacci-thing n5))
+  0)
+⇒ 13
+⇒ 8
+```
+
+This is all very nifty, and possibly useful in general, but what does this have
+to do with Guix?  Well, many Guix store-based operations are meant to be used
+in concert with yet another monad, called the `%store-monad`.  But if we look at
+`(guix store)`, where `%store-monad` is defined...
+
+```scheme
+(define-alias %store-monad %state-monad)
+(define-alias store-return state-return)
+(define-alias store-bind state-bind)
+```
+
+It was all a shallow façade!  All the "store monad" is is a special case of the
+state monad, where a value representing the store is passed as the state value.
+
+# Lies, Damned Lies, and Abstractions
+
+We mentioned that, technically, we didn't need monads for Guix.  Indeed, many
+(now deprecated) procedures take a store value as the argument, such as
+`build-expression->derivation`.  However, monads are far more elegant and
+simplify store code by quite a bit.
+
+`build-expression->derivation`, being deprecated, should never of course be
+used.  For one thing, it uses the "quoted build expression" style, rather than
+G-expressions (we'll discuss gexps another time).  The best way to create a
+derivation from some basic build code is to use the new-fangled
+`gexp->derivation` procedure:
+
+```scheme
+(use-modules (guix gexp)
+             (gnu packages irc))
+
+(define symlink-irssi
+  (gexp->derivation "link-to-irssi"
+    #~(symlink #$(file-append irssi "/bin/irssi") #$output)))
+⇒ #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)>
+```
+
+You don't have to understand the `#~(...)` form yet, only everything surrounding
+it.  We can see that this `gexp->derivation` returns a procedure taking the
+initial state (store), just like our `%state-monad` procedures did, and like we
+used `run-with-state` to pass the initial state to a `%state-monad` monadic
+value, we use our old friend `run-with-store` when we have a `%store-monad`
+monadic value!
+
+```scheme
+(define symlink-irssi-drv
+  (with-store store
+    (run-with-store store
+      symlink-irssi)))
+⇒ #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0>
+```
+
+Let's just check this derivation is as expected by reading the code from the
+builder script.
+
+```scheme
+(define symlink-irssi-builder
+  (list-ref (derivation-builder-arguments symlink-irssi-drv) 1))
+
+(call-with-input-file symlink-irssi-builder
+  (lambda (port)
+    (read port)))
+    
+⇒ (symlink
+   "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi"
+   ((@ (guile) getenv) "out"))
+```
+
+And indeed, it symlinks the `irssi` binary to the output path.  Some other,
+higher-level, monadic procedures include `interned-file`, which copies a file
+from outside the store into it, and `text-file`, which copies some text into it.
+Generally, these procedures aren't used, as there are higher-level procedures
+that perform similar functions (which we will discuss later), but for the sake
+of this blog post, here's an example:
+
+```scheme
+(with-store store
+  (run-with-store store
+    (text-file "unmatched-paren"
+      "( <paren <at> disroot.org>")))
+⇒ "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren"
+```
+
+# Conclusion
+
+What have we learned about monads?  The key points we can take away are:
+
+1. Monads are a way of composing together procedures and values that are wrapped
+   in containers that give them extra context, like `maybe` values.
+2. Guix provides a high-level monad library that compensates for Guile's lack of
+   static typing or an interface-like system.
+3. The `(guix monads)` module provides the state monad, which allows you to
+   thread state through procedures, allowing you to essentially pretend it's a
+   global variable that's modified by each procedure.
+4. Guix uses the store monad frequently to thread a store connection through
+   procedures that need it.
+5. The store monad is really just the state monad in disguise, where the state
+   value is used to thread the store object through monadic procedures.
+
+If you've read this post in its entirety but still don't yet quite get it, don't
+worry.  Try to modify and tinker about with the examples, and ask any questions
+on the IRC channel `#guix:libera.chat` and mailing list at `help-guix <at> gnu.org`,
+and hopefully it will all click eventually!
+
+#### About GNU Guix
+
+[GNU Guix](https://guix.gnu.org) is a transactional package manager and
+an advanced distribution of the GNU system that [respects user
+freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html).
+Guix can be used on top of any system running the Hurd or the Linux
+kernel, or it can be used as a standalone operating system distribution
+for i686, x86_64, ARMv7, AArch64 and POWER9 machines.
+
+In addition to standard package management features, Guix supports
+transactional upgrades and roll-backs, unprivileged package management,
+per-user profiles, and garbage collection.  When used as a standalone
+GNU/Linux distribution, Guix offers a declarative, stateless approach to
+operating system configuration management.  Guix is highly customizable
+and hackable through [Guile](https://www.gnu.org/software/guile)
+programming interfaces and extensions to the
+[Scheme](http://schemers.org) language.

base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3
-- 
2.39.1





Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Tue, 14 Feb 2023 19:26:01 GMT) Full text and rfc822 format available.

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

From: Simon Tournier <zimon.toutoune <at> gmail.com>
To: "( via Guix-patches via" <guix-patches <at> gnu.org>, 61214 <at> debbugs.gnu.org
Cc: "\(" <paren <at> disroot.org>, ludo <at> gnu.org, mail <at> cbaines.net,
 zimoun.toutoune <at> gmail.com
Subject: Re: [bug#61214] [PATCH guix-artwork v5] website: posts: Add
 Dissecting Guix, Part 2: The Store Monad.
Date: Tue, 14 Feb 2023 19:01:46 +0100
Hi,

On mar., 14 févr. 2023 at 07:33, "\( via Guix-patches" via <guix-patches <at> gnu.org> wrote:

> +# Yes, No, Maybe So
> +
> +Let's instead implement another M of functional programming, _`maybe`_ values,
> +representing a value that may or may not exist.  For instance, there could be a
> +procedure that attempts to pop a stack, returing the result if there is one, or
                                            --^
                                          Typo

s/returing/returning


Cheers,
simon




Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Tue, 14 Feb 2023 19:26:02 GMT) Full text and rfc822 format available.

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Tue, 14 Feb 2023 19:27:01 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: "Simon Tournier" <zimon.toutoune <at> gmail.com>, "( via Guix-patches via"
 <guix-patches <at> gnu.org>, <61214 <at> debbugs.gnu.org>
Cc: ludo <at> gnu.org, mail <at> cbaines.net, zimoun.toutoune <at> gmail.com
Subject: Re: [bug#61214] [PATCH guix-artwork v5] website: posts: Add
 Dissecting Guix, Part 2: The Store Monad.
Date: Tue, 14 Feb 2023 19:26:30 +0000
[Message part 1 (text/plain, inline)]
On Tue Feb 14, 2023 at 6:01 PM GMT, Simon Tournier wrote:
>                                             --^
>                                           Typo
>
> s/returing/returning

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

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

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Tue, 14 Feb 2023 19:27:02 GMT) Full text and rfc822 format available.

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Thu, 16 Feb 2023 17:01:02 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: 61214 <at> debbugs.gnu.org
Cc: "\(" <paren <at> disroot.org>, ludo <at> gnu.org, mail <at> cbaines.net,
 zimoun.toutoune <at> gmail.com
Subject: [PATCH guix-artwork v6] website: posts: Add Dissecting Guix,
 Part 2: The Store Monad.
Date: Thu, 16 Feb 2023 17:00:15 +0000
* website/posts/dissecting-guix-2-store-monad.md: New blog post.
---
* Fix typo (change "returing" to "returning").

 .../posts/dissecting-guix-2-store-monad.md    | 567 ++++++++++++++++++
 1 file changed, 567 insertions(+)
 create mode 100644 website/posts/dissecting-guix-2-store-monad.md

diff --git a/website/posts/dissecting-guix-2-store-monad.md b/website/posts/dissecting-guix-2-store-monad.md
new file mode 100644
index 0000000..7536733
--- /dev/null
+++ b/website/posts/dissecting-guix-2-store-monad.md
@@ -0,0 +1,567 @@
+title: Dissecting Guix, Part 2: The Store Monad
+date: TBC
+author: (
+tags: Dissecting Guix, Functional package management, Programming interfaces, Scheme API
+---
+Hello again!
+
+In [the last post](https://guix.gnu.org/en/blog/2023/dissecting-guix-part-1-derivations/),
+we briefly mentioned the `with-store` and `run-with-store` macros.  Today, we'll
+be looking at those in further detail, along with the related monad library and
+the [`%store-monad`](https://guix.gnu.org/manual/devel/en/html_node/The-Store-Monad.html)!
+
+Typically, we use monads to chain operations together, and the `%store-monad` is
+no different; it's used to combine operations that work on the Guix store (for
+instance, creating derivations, building derivations, or adding data files to
+the store).
+
+However, monads are a little hard to explain, and from a distance, they seem to
+be quite incomprehensible.  So, I want you to erase them from your mind for now.
+We'll come back to them later.  And be aware that if you can't seem to get your
+head around them, it's okay; you can understand most of the architecture of Guix
+without understanding monads.
+
+# Yes, No, Maybe So
+
+Let's instead implement another M of functional programming, _`maybe`_ values,
+representing a value that may or may not exist.  For instance, there could be a
+procedure that attempts to pop a stack, returning the result if there is one, or
+`nothing` if the stack has no elements.
+
+`maybe` is a very common feature of statically-typed functional languages, and
+you'll see it all over the place in Haskell and OCaml code. However, Guile is
+dynamically typed, so we usually use ad-hoc `#f` values as the "null value"
+instead of a proper "nothing" or "none".
+
+Just for fun, though, we'll implement a proper `maybe` in Guile.  Fire up that
+REPL once again, and let's import a bunch of modules that we'll need:
+
+```scheme
+(use-modules (ice-9 match)
+             (srfi srfi-9))
+```
+
+We'll implement `maybe` as a record with two fields, `is?` and `value`.  If the
+value contains something, `is?` will be `#t` and `value` will contain the thing
+in question, and if it's empty, `is?`'ll be `#f`.
+
+```scheme
+(define-record-type <maybe>
+  (make-maybe is? value)
+  maybe?
+  (is? maybe-is?)
+  (value maybe-value))
+```
+
+Now we'll define constructors for the two possible states:
+
+```scheme
+(define (something value)
+  (make-maybe #t value))
+
+(define (nothing)
+  (make-maybe #f #f)) ;the value here doesn't matter; we'll just use #f
+```
+
+And make some silly functions that return optional values:
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+      
+(remove-a "ahh")
+⇒ #<<maybe> is?: #t value: "hh">
+
+(remove-a "ooh")
+⇒ #<<maybe> is?: #f value: #f>
+
+(remove-b "bad")
+⇒ #<<maybe> is?: #t value: "ad">
+```
+
+But what if we want to compose the results of these functions?
+
+# Keeping Your Composure
+
+As you might have guessed, this is not fun.  Cosplaying as a compiler backend
+typically isn't.
+
+```scheme
+(let ((t1 (remove-a "abcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+⇒ #<<maybe> is?: #t value: "cd">
+
+(let ((t1 (remove-a "bbcd")))
+  (if (maybe-is? t1)
+      (remove-b (maybe-value t1))
+      (nothing)))
+⇒ #<<maybe> is?: #f value: #f>
+```
+
+I can almost hear the heckling.  Even worse, composing three:
+
+```scheme
+(let* ((t1 (remove-a "abad"))
+       (t2 (if (maybe-is? t1)
+               (remove-b (maybe-value t1))
+               (nothing))))
+  (if (maybe-is? t2)
+      (remove-a (maybe-value t2))
+      (nothing)))
+⇒ #<<maybe> is?: #t value: "d">
+```
+
+So, how do we go about making this more bearable?  Well, one way could be to
+make `remove-a` and `remove-b` accept `maybe`s:
+
+```scheme
+(define (remove-a ?str)
+  (match ?str
+    (($ <maybe> #t str)
+     (if (eq? (string-ref str 0) #\a)
+         (something (substring str 1))
+         (nothing)))
+    (_ (nothing))))
+
+(define (remove-b ?str)
+  (match ?str
+    (($ <maybe> #t str)
+     (if (eq? (string-ref str 0) #\b)
+         (something (substring str 1))
+         (nothing)))
+    (_ (nothing))))
+```
+
+Not at all pretty, but it works!
+
+```scheme
+(remove-b (remove-a (something "abc")))
+⇒ #<<maybe> is?: #t value: "c">
+```
+
+Still, our procedures now require quite a bit of boilerplate.  Might there be a
+better way?
+
+# The Ties That `>>=` Us
+
+First of all, we'll revert to our original definitions of `remove-a` and
+`remove-b`, that is to say, the ones that take a regular value and return a
+`maybe`.
+
+```scheme
+(define (remove-a str)
+  (if (eq? (string-ref str 0) #\a)
+      (something (substring str 1))
+      (nothing)))
+
+(define (remove-b str)
+  (if (eq? (string-ref str 0) #\b)
+      (something (substring str 1))
+      (nothing)))
+```
+
+What if tried introducing higher-order procedures (procedures that accept other
+procedures as arguments) into the equation?  Because we're functional
+programmers and we have an unhealthy obsession with that sort of thing.
+
+```scheme
+(define (maybe-chain maybe proc)
+  (if (maybe-is? maybe)
+      (proc (maybe-value maybe))
+      (nothing)))
+
+(maybe-chain (something "abc")
+             remove-a)
+⇒ #<<maybe> is?: #t value: "bc">
+
+(maybe-chain (nothing)
+             remove-a)
+⇒ #<<maybe> is?: #f value: #f>
+```
+
+It lives!  To make it easier to compose procedures like this, we'll define a
+macro that allows us to perform any number of sequenced operations with only one
+composition form:
+
+```scheme
+(define-syntax maybe-chain*
+  (syntax-rules ()
+    ((_ maybe proc)
+     (maybe-chain maybe proc))
+    ((_ maybe proc rest ...)
+     (maybe-chain* (maybe-chain maybe proc)
+                   rest ...))))
+
+(maybe-chain* (something "abad")
+              remove-a
+              remove-b
+              remove-a)
+⇒ #<<maybe> is?: #t value: "d">
+```
+
+Congratulations, you've just implemented the `bind` operation, commonly written
+as `>>=`, for our `maybe` type.  And it turns out that a monad is just any
+container-like value for which `>>=` (along with another procedure called
+`return`, which wraps a given value in the simplest possible form of a monad)
+has been implemented.
+
+A more formal definition would be that a monad is a mathematical object composed
+of three parts: a type, a `bind` function, and a `return` function.  So, how do
+monads relate to Guix?
+
+# New Wheel, Old Wheel
+
+Now that we've reinvented the wheel, we'd better learn to use the original
+wheel.  Guix provides a generic, high-level monads library, along with the two
+generic monads `%identity-monad` and `%state-monad`, and the Guix-specific
+`%store-monad`.  Since `maybe` is not one of them, let's integrate our version
+into the Guix monad system!
+
+First we'll import the module that provides the aforementioned library:
+
+```scheme
+(use-modules (guix monads))
+```
+
+To define a monad's behaviour in Guix, we simply use the `define-monad` macro,
+and provide two procedures: `bind`, and `return`.
+
+```scheme
+(define-monad %maybe-monad
+  (bind maybe-chain)
+  (return something))
+```
+
+`bind` is just the procedure that we use to compose monadic procedure calls
+together, and `return` is the procedure that wraps values in the most basic form
+of the monad.  A properly implemented `bind` and `return` must follow the
+so-called _monad laws_:
+
+1. `(bind (return x) proc)` must be equivalent to `(proc x)`.
+2. `(bind monad return)` must be equivalent to just `monad`.
+3. `(bind (bind monad proc-1) proc-2)` must be equivalent to
+   `(bind monad (lambda (x) (bind (proc-1 x) proc-2)))`.
+
+Let's verify that our `maybe-chain` and `something` procedures adhere to the
+monad laws:
+
+```scheme
+(define (mlaws-proc-1 x)
+  (something (+ x 1)))
+
+(define (mlaws-proc-2 x)
+  (something (+ x 2)))
+  
+;; First law: the left identity.
+(equal? (maybe-chain (something 0)
+                     mlaws-proc-1)
+        (mlaws-proc-1 0))
+⇒ #t
+ 
+;; Second law: the right identity.
+(equal? (maybe-chain (something 0)
+                     something)
+        (something 0))
+⇒ #t
+
+;; Third law: associativity.
+(equal? (maybe-chain (maybe-chain (something 0)
+                                  mlaws-proc-1)
+                     mlaws-proc-2)
+        (maybe-chain (something 0)
+                     (lambda (x)
+                       (maybe-chain (mlaws-proc-1 x)
+                                    mlaws-proc-2))))
+⇒ #t
+```
+
+Now that we know they're valid, we can use the `with-monad` macro to tell Guix
+to use these specific implementations of `bind` and `return`, and the `>>=`
+macro to thread monads through procedure calls!
+
+```scheme
+(with-monad %maybe-monad
+  (>>= (something "aabbc")
+       remove-a
+       remove-a
+       remove-b
+       remove-b))
+⇒ #<<maybe> is?: #t value: "c">
+```
+
+We can also now use `return`:
+
+```scheme
+(with-monad %maybe-monad
+  (return 32))
+⇒ #<<maybe> is?: #t value: 32>
+```
+
+But Guix provides many higher-level interfaces than `>>=` and `return`, as we
+will see.  There's `mbegin`, which evaluates monadic expressions without binding
+them to symbols, returning the last one.  This, however, isn't particularly
+useful with our `%maybe-monad`, as it's only really usable if the monadic
+operations within have side effects, just like the non-monadic `begin`.
+
+There's also `mlet` and `mlet*`, which _do_ bind the results of monadic
+expressions to symbols, and are essentially equivalent to a chain of
+`(>>= MEXPR (lambda (BINDING) ...))`:
+
+```scheme
+;; This is equivalent...
+(mlet* %maybe-monad ((str -> "abad") ;non-monadic binding uses the -> symbol
+                     (str1 (remove-a str))
+                     (str2 (remove-b str)))
+  (remove-a str))
+⇒ #<<maybe> is?: #t value: "d">
+
+;; ...to this:
+(with-monad %maybe-monad
+  (>>= (return "abad")
+       (lambda (str)
+         (remove-a str))
+       (lambda (str1)
+         (remove-b str))
+       (lambda (str2)
+         (remove-a str))))
+```
+
+Various abstractions over these two exist too, such as `mwhen` (a `when` plus an
+`mbegin`), `munless` (an `unless` plus an `mbegin`), and `mparameterize`
+(dynamically-scoped value rebinding, like `parameterize`, in a monadic context).
+`lift` takes a procedure and a monad and creates a new procedure that returns
+a monadic value.
+
+There are also interfaces for manipulating lists wrapped in monads; `listm`
+creates such a list, `sequence` turns a list of monads into a list wrapped in a
+monad, and the `anym`, `mapm`, and `foldm` procedures are like their non-monadic
+equivalents, except that they return lists wrapped in monads.
+
+This is all well and good, you may be thinking, but why does Guix need a monad
+library, anyway?  The answer is technically that it doesn't.  But building on
+the monad API makes a lot of things much easier, and to learn why, we're going
+to look at one of Guix's built-in monads.
+
+# In a State
+
+Guix implements a monad called `%state-monad`, and it works with single-argument
+procedures returning two values.  Behold:
+
+```scheme
+(with-monad %state-monad
+  (return 33))
+⇒ #<procedure 21dc9a0 at <unknown port>:1106:22 (state)>
+```
+
+The `run-with-state` value turns this procedure into an actually useful value,
+or, rather, two values:
+
+```scheme
+(run-with-state (with-monad %state-monad (return 33))
+  (list "foo" "bar" "baz"))
+⇒ 33
+⇒ ("foo" "bar" "baz")
+```
+
+What can this actually do for us, though? Well, it gets interesting if we do
+some `>>=`ing:
+
+```scheme
+(define state-seq
+  (mlet* %state-monad ((number (return 33)))
+    (state-push number)))
+result
+⇒ #<procedure 7fcb6f466960 at <unknown port>:1484:24 (state)>
+
+(run-with-state state-seq (list 32))
+⇒ (32)
+⇒ (33 32)
+
+(run-with-state state-seq (list 30 99))
+⇒ (30 99)
+⇒ (33 30 99)
+```
+
+What is `state-push`?  It's a monadic procedure for `%state-monad` that takes
+whatever's currently in the first value (the primary value) and pushes it onto
+the second value (the state value), which is assumed to be a list, returning the
+old state value as the primary value and the new list as the state value.
+
+So, when we do `(run-with-state result (list 32))`, we're passing `(list 32)` as
+the initial state value, and then the `>>=` form passes that and `33` to
+`state-push`.  What `%state-monad` allows us to do is thread together some
+procedures that require some kind of state, while essentially pretending the
+state value is stored globally, like you might do in, say, C, and then retrieve
+both the final state and the result at the end!
+
+If you're a bit confused, don't worry.  We'll write some of our own
+`%state-monad`-based monadic procedures and hopefully all will become clear.
+Consider, for instance, the
+[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), in which
+each value is computed by adding the previous two.  We could use the
+`%state-monad` to compute Fibonacci numbers by storing the previous number as
+the primary value and the number before that as the state value:
+
+```scheme
+(define (fibonacci-thing value)
+  (lambda (state)
+    (values (+ value state)
+            value)))
+```
+
+Now we can feed our Fibonacci-generating procedure the first value using
+`run-with-state` and the second using `return`:
+
+```scheme
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1)))
+      (fibonacci-thing n2))
+  0)
+⇒ 3
+⇒ 2
+
+(run-with-state
+    (mlet* %state-monad ((starting (return 1))
+                         (n1 (fibonacci-thing starting))
+                         (n2 (fibonacci-thing n1))
+                         (n3 (fibonacci-thing n2))
+                         (n4 (fibonacci-thing n3))
+                         (n5 (fibonacci-thing n4)))
+      (fibonacci-thing n5))
+  0)
+⇒ 13
+⇒ 8
+```
+
+This is all very nifty, and possibly useful in general, but what does this have
+to do with Guix?  Well, many Guix store-based operations are meant to be used
+in concert with yet another monad, called the `%store-monad`.  But if we look at
+`(guix store)`, where `%store-monad` is defined...
+
+```scheme
+(define-alias %store-monad %state-monad)
+(define-alias store-return state-return)
+(define-alias store-bind state-bind)
+```
+
+It was all a shallow façade!  All the "store monad" is is a special case of the
+state monad, where a value representing the store is passed as the state value.
+
+# Lies, Damned Lies, and Abstractions
+
+We mentioned that, technically, we didn't need monads for Guix.  Indeed, many
+(now deprecated) procedures take a store value as the argument, such as
+`build-expression->derivation`.  However, monads are far more elegant and
+simplify store code by quite a bit.
+
+`build-expression->derivation`, being deprecated, should never of course be
+used.  For one thing, it uses the "quoted build expression" style, rather than
+G-expressions (we'll discuss gexps another time).  The best way to create a
+derivation from some basic build code is to use the new-fangled
+`gexp->derivation` procedure:
+
+```scheme
+(use-modules (guix gexp)
+             (gnu packages irc))
+
+(define symlink-irssi
+  (gexp->derivation "link-to-irssi"
+    #~(symlink #$(file-append irssi "/bin/irssi") #$output)))
+⇒ #<procedure 7fddcc7b81e0 at guix/gexp.scm:1180:2 (state)>
+```
+
+You don't have to understand the `#~(...)` form yet, only everything surrounding
+it.  We can see that this `gexp->derivation` returns a procedure taking the
+initial state (store), just like our `%state-monad` procedures did, and like we
+used `run-with-state` to pass the initial state to a `%state-monad` monadic
+value, we use our old friend `run-with-store` when we have a `%store-monad`
+monadic value!
+
+```scheme
+(define symlink-irssi-drv
+  (with-store store
+    (run-with-store store
+      symlink-irssi)))
+⇒ #<derivation /gnu/store/q7kwwl4z6psifnv4di1p1kpvlx06fmyq-link-to-irssi.drv => /gnu/store/6a94niigx4ii0ldjdy33wx9anhifr25x-link-to-irssi 7fddb7ef52d0>
+```
+
+Let's just check this derivation is as expected by reading the code from the
+builder script.
+
+```scheme
+(define symlink-irssi-builder
+  (list-ref (derivation-builder-arguments symlink-irssi-drv) 1))
+
+(call-with-input-file symlink-irssi-builder
+  (lambda (port)
+    (read port)))
+    
+⇒ (symlink
+   "/gnu/store/hrlmypx1lrdjlxpkqy88bfrzg5p0bn6d-irssi-1.4.3/bin/irssi"
+   ((@ (guile) getenv) "out"))
+```
+
+And indeed, it symlinks the `irssi` binary to the output path.  Some other,
+higher-level, monadic procedures include `interned-file`, which copies a file
+from outside the store into it, and `text-file`, which copies some text into it.
+Generally, these procedures aren't used, as there are higher-level procedures
+that perform similar functions (which we will discuss later), but for the sake
+of this blog post, here's an example:
+
+```scheme
+(with-store store
+  (run-with-store store
+    (text-file "unmatched-paren"
+      "( <paren <at> disroot.org>")))
+⇒ "/gnu/store/v6smacxvdk4yvaa3s3wmd54lixn1dp3y-unmatched-paren"
+```
+
+# Conclusion
+
+What have we learned about monads?  The key points we can take away are:
+
+1. Monads are a way of composing together procedures and values that are wrapped
+   in containers that give them extra context, like `maybe` values.
+2. Guix provides a high-level monad library that compensates for Guile's lack of
+   static typing or an interface-like system.
+3. The `(guix monads)` module provides the state monad, which allows you to
+   thread state through procedures, allowing you to essentially pretend it's a
+   global variable that's modified by each procedure.
+4. Guix uses the store monad frequently to thread a store connection through
+   procedures that need it.
+5. The store monad is really just the state monad in disguise, where the state
+   value is used to thread the store object through monadic procedures.
+
+If you've read this post in its entirety but still don't yet quite get it, don't
+worry.  Try to modify and tinker about with the examples, and ask any questions
+on the IRC channel `#guix:libera.chat` and mailing list at `help-guix <at> gnu.org`,
+and hopefully it will all click eventually!
+
+#### About GNU Guix
+
+[GNU Guix](https://guix.gnu.org) is a transactional package manager and
+an advanced distribution of the GNU system that [respects user
+freedom](https://www.gnu.org/distros/free-system-distribution-guidelines.html).
+Guix can be used on top of any system running the Hurd or the Linux
+kernel, or it can be used as a standalone operating system distribution
+for i686, x86_64, ARMv7, AArch64 and POWER9 machines.
+
+In addition to standard package management features, Guix supports
+transactional upgrades and roll-backs, unprivileged package management,
+per-user profiles, and garbage collection.  When used as a standalone
+GNU/Linux distribution, Guix offers a declarative, stateless approach to
+operating system configuration management.  Guix is highly customizable
+and hackable through [Guile](https://www.gnu.org/software/guile)
+programming interfaces and extensions to the
+[Scheme](http://schemers.org) language.

base-commit: fe113595b6f7d8a1e1a0b814521f02783f9209c3
-- 
2.39.1





Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Mon, 20 Feb 2023 21:27:01 GMT) Full text and rfc822 format available.

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

From: Tobias Geerinckx-Rice <me <at> tobias.gr>
To: "(" <paren <at> disroot.org>
Cc: ludo <at> gnu.org, mail <at> cbaines.net, 61214-done <at> debbugs.gnu.org,
 guix-patches <at> gnu.org, zimoun.toutoune <at> gmail.com
Subject: Re: [bug#61214] [PATCH guix-artwork v6] website: posts: Add
 Dissecting Guix, Part 2: The Store Monad.
Date: Mon, 20 Feb 2023 22:23:12 +0100
[Message part 1 (text/plain, inline)]
(,

As promised in #guix — and because I think it's ready; any further 
suggestions can still be addressed if needed — I've pushed this 
upstream.

Thanks for all the effort you've put in so far, and thanks to 
everyone who contributed.

I've really enjoyed this series so far and look forward to the 
next installment.

),

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

Reply sent to Tobias Geerinckx-Rice <me <at> tobias.gr>:
You have taken responsibility. (Mon, 20 Feb 2023 21:27:02 GMT) Full text and rfc822 format available.

Notification sent to "(" <paren <at> disroot.org>:
bug acknowledged by developer. (Mon, 20 Feb 2023 21:27:02 GMT) Full text and rfc822 format available.

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Mon, 20 Feb 2023 22:05:01 GMT) Full text and rfc822 format available.

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

From: "(" <paren <at> disroot.org>
To: "Tobias Geerinckx-Rice" <me <at> tobias.gr>
Cc: ludo <at> gnu.org, mail <at> cbaines.net, 61214-done <at> debbugs.gnu.org,
 guix-patches <at> gnu.org, zimoun.toutoune <at> gmail.com
Subject: Re: [bug#61214] [PATCH guix-artwork v6] website: posts: Add
 Dissecting Guix, Part 2: The Store Monad.
Date: Mon, 20 Feb 2023 22:04:20 +0000
[Message part 1 (text/plain, inline)]
On Mon Feb 20, 2023 at 9:23 PM GMT, Tobias Geerinckx-Rice wrote:
> As promised in #guix — and because I think it's ready; any further 
> suggestions can still be addressed if needed — I've pushed this 
> upstream.

Yey :) Thanks!

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

Information forwarded to guix-patches <at> gnu.org:
bug#61214; Package guix-patches. (Mon, 20 Feb 2023 22:05:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Tue, 21 Mar 2023 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 36 days ago.

Previous Next


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