From: Justin Veilleux <terramorpha@cock.li>
To: guix-devel@gnu.org
Subject: Alternative syntax for interacting with the store monad
Date: Sat, 30 Nov 2024 17:50:14 -0500 [thread overview]
Message-ID: <875xo49w0p.fsf@cock.li> (raw)
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?
next reply other threads:[~2024-11-30 22:51 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-30 22:50 Justin Veilleux [this message]
2024-12-02 17:15 ` Alternative syntax for interacting with the store monad Simon Tournier
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=875xo49w0p.fsf@cock.li \
--to=terramorpha@cock.li \
--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 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.