[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Scheme-reports] 4.2.7. Exception Handling
On Wed 18 May 2011 21:03, John Cowan <cowan@x> writes:
>> "That implicit `cond' expression is evaluated with the continuation
>> and dynamic extent of the `guard' expression"
>
> The idea is that "guard" installs a handler which first unwinds and then
> evaluates the cond, rewinding if it doesn't find an appropriate clause.
> This provides termination semantics in the style of C++, Java, C#, etc.
> but with the possibility of carrying on if the guard gets something it
> doesn't expect. If you want instead to get control in the scope of the
> raiser, don't use "guard" but rather "with-exception-handler" to install
> your own handler.
Thanks for the feedback.
To reply in this thread: I still think this rewinding behavior is
suboptimal. It forces implementations to reify full continuations
instead of using one-shot continuations for exceptions.
Here is an implementation of `guard' which does evaluate the predicates
in the raise handler, the bodies with the continuation of the `guard',
and re-raises from within `raise', but without rewinding.
(define-syntax clause-helper
(syntax-rules (=> else)
((_ k) #f) ; fall through to re-raise
((_ k (test) clause ...)
(let ((t test))
(if t (k (lambda () t)) (clause-helper k clause ...))))
((_ k (test => e) clause ...)
(let ((t test))
(if t (k (lambda () (e t))) (clause-helper k clause ...))))
((_ k (test e e* ...) clause ...)
(if test (k (lambda () e e* ...)) (clause-helper k clause ...)))
((_ k (else e e* ...))
(k (lambda () e e* ...)))))
(define-syntax guard
(syntax-rules ()
((_ (var clause clause* ...) body body* ...)
((call/cc
(lambda (k)
(with-exception-handler
(lambda (err)
(clause-helper k clause clause* ...))
(lambda ()
(lambda () body body* ...)))))))))
Suggestion: that this be a legal implementation of `guard'.
Andy
--
http://wingolog.org/
_______________________________________________
Scheme-reports mailing list
Scheme-reports@x
http://lists.scheme-reports.org/cgi-bin/mailman/listinfo/scheme-reports