From: taylanbayirli@gmail.com (Taylan Ulrich B.)
To: Panicz Maciej Godek <godek.maciek@gmail.com>
Cc: "guile-user@gnu.org" <guile-user@gnu.org>
Subject: Re: Exceptions that pass continuations
Date: Fri, 19 Jul 2013 16:12:40 +0200 [thread overview]
Message-ID: <871u6uabaf.fsf@taylan.uni.cx> (raw)
In-Reply-To: <8738ra3drr.fsf@zigzag.favinet> (Thien-Thi Nguyen's message of "Fri, 19 Jul 2013 15:00:56 +0200")
I agree with Thien on that all resource-management should be delegated
to the garbage collector via smob types.
However, I found this supply-demand pattern quite neat (could have other
uses perhaps), was kind of bored, with a little free time on my hands,
and enjoy an occasional exercise in delimited continuations, so here's a
cleaned-up version of the code using prompts. :D But see the note at
the end.
(define prompt-tag (make-prompt-tag "supply"))
(define-syntax supply
(syntax-rules ()
((_ (((action-name . action-args) abody abody* ...) ...) body body* ...)
(let ((actions (alist->hash-table
`((,action-name . ,(lambda action-args
abody abody* ...))
...))))
(call-with-prompt
prompt-tag
(lambda () body body* ...)
(lambda (cont demanded-action . args)
(let ((action (hash-table-ref/default actions demanded-action #f)))
(if action
(cont (apply action args))
(apply throw 'demand-not-supplied demanded-action args)))))))))
(define (demand action-name . args)
(apply abort-to-prompt prompt-tag action-name args))
;;; It composes neatly:
(supply ((('foo . bar) bar)) (demand 'foo 0 1 2)) ;=> (0 1 2)
Now there's something funny to notice here. I was uncomfortable forcing
the action-names to be literals, because then one couldn't rename them
and thus different modules would get name-clashes; so I don't
automatically quote the action-name, and would expect modules to export
the "names" of the actions they demand as variables holding unique
objects (e.g. a uniquely allocated cons cell), so those variables can be
renamed and the action they refer to is still unique. And at that
point, we pretty much re-implemented parameters! In a much worse way
than their Guile-native implementation of course, and forcing their
values to be procedures.
Long story short, use parameters for this pattern. :)
Taylan
prev parent reply other threads:[~2013-07-19 14:12 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-19 10:39 Exceptions that pass continuations Panicz Maciej Godek
2013-07-19 13:00 ` Thien-Thi Nguyen
2013-07-19 13:52 ` Panicz Maciej Godek
2013-07-19 16:14 ` Thien-Thi Nguyen
2013-07-19 14:12 ` Taylan Ulrich B. [this message]
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
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=871u6uabaf.fsf@taylan.uni.cx \
--to=taylanbayirli@gmail.com \
--cc=godek.maciek@gmail.com \
--cc=guile-user@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.
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).