Hello, Chris Marusich <cmmarus...@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’.