all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Alternative syntax for interacting with the store monad
@ 2024-11-30 22:50 Justin Veilleux
  2024-12-02 17:15 ` Simon Tournier
  0 siblings, 1 reply; 2+ messages in thread
From: Justin Veilleux @ 2024-11-30 22:50 UTC (permalink / raw)
  To: guix-devel


Hello.

I was reading
https://guix.gnu.org/en/blog/2023/dissecting-guix-part-2-the-store-monad/ and
was wondering if using Guile scheme's ~call-with-prompt~ for interacting with the
store monad had been considered. With ~call-with-prompt~, one can recreate an
~async-await~-like syntax in scheme. I think using this syntax (instead of the
~mlet~ macros and ~store-bind~) might be easier to use for beginners as many people
already intuitively know how to use ~async&await~ which is not the case for the
"explicitely monadic" operations.

Here is a snippet of code illustrating how one could use any monad (with
functions ~bind-fn~ and ~pure-fn~) through the more readable ~async&await~ forms.

#+begin_src scheme
(define-syntax-parameter bind
  (lambda (s)
    (syntax-violation 'bind "bind used outside `with-monad`" s)))

(define-syntax-parameter pure
  (lambda (s)
    (syntax-violation 'pure "pure used outside `with-monad`" s)))

(define-syntax-rule (with-monad bind-fn pure-fn body body* ...)
  (let ((bind-fn bind-fn)
        (pure-fn pure-fn)
        (prompt-tag (make-prompt-tag)))
    (syntax-parameterize
        ((bind (lambda (s)
                (syntax-case s ()
                  ((_ x) #'(abort-to-prompt prompt-tag x)))))
         (pure (lambda (s)
                 (syntax-case s ()
                   ((_ x) #'(pure-fn x))))))

      (define (handler kont mval)
        (bind-fn
         mval
         (lambda (x)
           (call-with-prompt prompt-tag
             (lambda () (kont x))
             handler))))

      (call-with-prompt prompt-tag
        (lambda ()
          body body* ...)
        handler))))

(define (list-bind xs f)
  (apply append! (map f xs)))

(define list-pure list)

(with-monad list-bind list-pure
            (let ((x (bind '(1 2))))
              (pure
               (list
                x
                (bind '("thing1" "thing2"))))))

;; => ((1 "thing1") (1 "thing2") (2 "thing1") (2 "thing2"))

#+end_src

Maybe the the performance costs associated with using delimited continuations
are too high?

What do you think?



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Alternative syntax for interacting with the store monad
  2024-11-30 22:50 Alternative syntax for interacting with the store monad Justin Veilleux
@ 2024-12-02 17:15 ` Simon Tournier
  0 siblings, 0 replies; 2+ messages in thread
From: Simon Tournier @ 2024-12-02 17:15 UTC (permalink / raw)
  To: Justin Veilleux, guix-devel

Hi,

On Sat, 30 Nov 2024 at 17:50, Justin Veilleux <terramorpha@cock.li> wrote:

> (define (list-bind xs f)
>   (apply append! (map f xs)))
>
> (define list-pure list)
>
> (with-monad list-bind list-pure
>             (let ((x (bind '(1 2))))
>               (pure
>                (list
>                 x
>                 (bind '("thing1" "thing2"))))))

In [1], Josselin proposed something à la Haskell:

    (mdo %store-monad
      (drv <- (gexp-derivation "myderivation" test-gexp))
      (output <- (return (derivation->output-path drv)))
      (built-derivations (list drv))
      (return (format #t "~a~%" output)))

Well, the conclusion seems that « it’s normally not necessary to use the
monadic interface unless you’re getting into internals or writing a new
tool. »  Therefore, I do not know if improving the syntax is worth.

Cheers,
simon


1: Re: Viewing derivation output in the store
Josselin Poiret <dev@jpoiret.xyz>
Thu, 21 Apr 2022 09:44:05 +0200
id:87v8v2g9tm.fsf@jpoiret.xyz
https://lists.gnu.org/archive/html/guix-devel/2022-04
https://yhetil.org/guix/87v8v2g9tm.fsf@jpoiret.xyz


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-12-02 19:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-30 22:50 Alternative syntax for interacting with the store monad Justin Veilleux
2024-12-02 17:15 ` Simon Tournier

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.