unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: ludo@gnu.org (Ludovic Courtès)
To: Chris Marusich <cmmarusich@gmail.com>
Cc: guix-devel@gnu.org
Subject: Re: Mysterious error while refactoring guix/scripts/system.scm
Date: Fri, 30 Sep 2016 22:36:29 +0200	[thread overview]
Message-ID: <87eg41jfaq.fsf@gnu.org> (raw)
In-Reply-To: <87zimxdqfo.fsf@gmail.com> (Chris Marusich's message of "Sat, 24 Sep 2016 12:54:19 -0700")

Hello,

Chris Marusich <cmmarusich@gmail.com> skribis:

> The reason I wanted to perform this refactoring in the first place is
> because I'd like to add a new procedure to guix/scripts/system.scm
> called 'switch-to-system-generation'.  Because this new procedure calls
> other procedures which seem to require access to the store, I thought I
> would need to call 'switch-to-system-generation' via 'run-with-store'.
> Am I just confused?

Essentially, to write a procedure that needs to access the store, you
need to write a “monadic procedure”—i.e., a procedure that returns a
value in the “store monad” instead of a “normal value.”

To do that, you would write:

  (define (switch-to-system-generation …)
    (with-monad %store-monad
      (return 'some-value)))    ;return this symbol as a monadic value

or:

  (define (switch-to-system-generation …)
     (some-monadic-procedure x y z))  ;tail-call a monadic procedure

The caller of a monadic procedure must be prepared to “unpack” its
value, using the monadic “bind” operator.  The ‘mlet’ form does exactly
that:

  (define (the-caller …)
    (mlet %store-monad ((result (switch-to-system-generation …)))
      …))

At the bottom, there must be somewhere a call to ‘run-with-store’ to
“run” the monadic value in the monad (info "(guix) The Store Monad").
This call is already in ‘process-action’ in (guix scripts system).

> In particular, 'switch-to-system-generation' will eventually call the
> existing procedure 'grub-configuration-file' (defined in
> gnu/system/grub.scm).  As I understand it, 'grub-configuration-file'
> returns a derivation that builds a GRUB configuration file.  This
> existing 'grub-configuration-file' procedure does a lot with the store
> and gexps.  I thought that if I didn't use 'run-with-store' to run
> 'switch-to-system-generation', it wouldn't work because
> 'grub-configuration-file' wouldn't work.

‘grub-configuration-file’ is a monadic procedure.  Thus, to “unpack” its
return value, you need to bind it, for instance with ‘mlet’:

  (mlet %store-monad ((file (grub-configuration-file …)))
    …)

or:

  (with-monad %store-monad
    (>>= (grub-configuration-file …)
         (lambda (file)
           …)))

HTH!

Ludo’.

      reply	other threads:[~2016-09-30 20:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-04  6:20 Mysterious error while refactoring guix/scripts/system.scm Chris Marusich
2016-08-04  7:56 ` Andy Wingo
2016-08-10  7:23   ` Chris Marusich
2016-08-29 15:53     ` Ludovic Courtès
2016-09-24 19:54       ` Chris Marusich
2016-09-30 20:36         ` Ludovic Courtès [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87eg41jfaq.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=cmmarusich@gmail.com \
    --cc=guix-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).