[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Exceptions that pass continuations
From: |
Taylan Ulrich B. |
Subject: |
Re: Exceptions that pass continuations |
Date: |
Fri, 19 Jul 2013 16:12:40 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (berkeley-unix) |
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