I am having trouble implementing exceptions (handle
and raise
) using a dynamically scoped variable and abort
.
This question came from reading the paper A syntactic theory of dynamic binding, section 6 figure 7.
What I have attempted seems to work correctly for throwing an exception inside a try block and also throwing an exception inside a try block inside a try block.
What doesn't work correctly is throwing an exception from inside a handler. It should abort to the next try block up in this case.
You can see my work here in racket scheme, along with 2 test programs. test-1 is working and test-2 is failing.
#lang racket
;; A Syntactic Theory of Dynamic Binding - Luc Moreau
;; https://link.springer.com/content/pdf/10.1007%2FBFb0030637.pdf
(require racket/control)
(define x_ed (make-parameter 'x_ed))
; NOTE: (abort ..) is the same as (shift k ..) where you don't use k
; NOTE: in (handle f M) we call f the handler and M the try block
; v1
;(define-syntax handle
; (syntax-rules ()
; ((handle f M)
; (parameterize ((x_ed (lambda (v) (abort (f v))))) M))))
; v2
;(define-syntax handle
; (syntax-rules ()
; ((handle f M)
; (reset (parameterize ((x_ed (lambda (v) (abort (f v))))) M)))))
; v3
;(define-syntax handle
; (syntax-rules ()
; ((handle f M)
; (parameterize ((x_ed (lambda (v) (abort (f v))))) (reset M)))))
; v4
(define-syntax handle
(syntax-rules ()
((handle f M)
(let ((old-x_ed (x_ed)))
(parameterize ((x_ed (lambda (v)
(abort (parameterize ((x_ed old-x_ed))
(f v))))))
(reset M))))))
(define-syntax raise
(syntax-rules ()
((raise v) ((x_ed) v))))
(define (print x) (write x) (newline))
(define (test-1)
(print "level-1 open")
(handle (lambda (v)
(print "level-1 caught"))
(begin
(print "level-2 open")
(handle (lambda (v)
(print "level-2 caught"))
(begin
(print "level-3 open")
(raise #t)
(print "level-3 close")))
(print "level-2 close")))
(print "level-1 close"))
(define (test-2)
(print "level-1 open")
(handle (lambda (v)
(print "level-1 caught"))
(begin
(print "level-2 open")
(handle (lambda (v)
(print "level-2 caught")
(raise #t))
(begin
(print "level-3 open")
(raise #t)
(print "level-3 close")))
(print "level-2 close")))
(print "level-1 close"))
;v1
;> (test-1)
;"level-1 open"
;"level-2 open"
;"level-3 open"
;"level-2 caught"
;v2 and v3
;> (test-1)
;"level-1 open"
;"level-2 open"
;"level-3 open"
;"level-2 caught"
;"level-2 close"
;"level-1 close"
;v2 and v3
;> (test-2)
;...
;"level-2 caught"
;"level-2 caught"
; infinite loop
;v4
;> (test-2)
;"level-1 open"
;"level-2 open"
;"level-3 open"
;"level-2 caught"
;"level-1 caught"
;"level-2 close" <--- we don't want this to happen
;"level-1 close"
I was able to come up with this working version thanks to the answer:
(define-syntax handle
(syntax-rules ()
((handle f M)
(prompt0
(parameterize ((x_ed (lambda (v)
(control0 k (f v)))))
M)))))