From: ludo@gnu.org (Ludovic Courtès)
To: guix-devel@gnu.org
Subject: Re: The store monad
Date: Wed, 02 Oct 2013 23:45:34 +0200 [thread overview]
Message-ID: <8761tfqrcx.fsf@gnu.org> (raw)
In-Reply-To: <87r4c4acfx.fsf@gnu.org> ("Ludovic \=\?utf-8\?Q\?Court\=C3\=A8s\=22'\?\= \=\?utf-8\?Q\?s\?\= message of "Tue, 01 Oct 2013 23:49:54 +0200")
Today’s monadic news is that monads are “inlined”. Before, something like:
(mlet %store-monad ((x y))
(return (+ 1 y)))
would expand to:
((struct-ref %store-monad 0) y
(lambda (x)
((struct-ref %store-monad 1) x)))
With inlining, the above doesn’t expand to references to the
‘%store-monad’ record, and instead directly expands to:
(store-bind y (lambda (x) (store-return (+ 1 x))))
If in addition ‘store-bind’ and ‘store-return’ are defined with
‘define-inlinable’, then the whole shebang is inlined, allowing for
better optimizations:
--8<---------------cut here---------------start------------->8---
scheme@(guix monads)> ,expand (with-monad %identity-monad (>>= (return 2) (lambda (x) (return (1+ x)))))
$17 = (let ((mvalue (let ((value 2)) value))
(mproc (lambda (x) (let ((value (#{1+}# x))) value))))
(mproc mvalue))
scheme@(guix monads)> ,optimize (with-monad %identity-monad (>>= (return 2) (lambda (x) (return (1+ x)))))
$18 = 3
scheme@(guix monads)> ,optimize (mlet* %identity-monad ((x (return 1))(y (return 2))(z (return (+ x y)))) z)
$19 = 3
[...]
scheme@(guix monads)> ,optimize (mlet* %store-monad ((x (return 1))(y (return 2))(z (return (+ x y)))) (return z))
$22 = (lambda (store) (let mresult ((store store)) 3))
--8<---------------cut here---------------end--------------->8---
This significantly reduces the cost of using bind/return all over the
place.
Ludo’.
next prev parent reply other threads:[~2013-10-02 21:50 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-10-01 21:49 The store monad Ludovic Courtès
2013-10-02 8:39 ` Nikita Karetnikov
2013-10-02 12:10 ` Ludovic Courtès
2013-10-02 21:45 ` Ludovic Courtès [this message]
2013-10-03 21:15 ` Ludovic Courtès
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=8761tfqrcx.fsf@gnu.org \
--to=ludo@gnu.org \
--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.