unofficial mirror of guix-devel@gnu.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
  2024-12-14 23:41 ` Ludovic Courtès
  0 siblings, 2 replies; 3+ 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] 3+ messages in thread

end of thread, other threads:[~2024-12-14 23:42 UTC | newest]

Thread overview: 3+ 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
2024-12-14 23:41 ` Ludovic Courtès

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).