guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Alternative syntax for interacting with the store monad


From: Justin Veilleux
Subject: Alternative syntax for interacting with the store monad
Date: Sat, 30 Nov 2024 17:50:14 -0500

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?




reply via email to

[Prev in Thread] Current Thread [Next in Thread]