嵌套定界延续转换

Nested delimited continuations transformations

我正在尝试理解定界延续,我正在阅读这篇文章:

http://community.schemewiki.org/?composable-continuations-tutorial

而且我发现了这个reset/shift转换

 (reset (...A... (shift V E) ...B...)) 
 ; --> 
 (let ((V (lambda (x) (...A... x ...B...)))) 
   E)

例如,我尝试对这个表达式进行转换(我认为append-map来自Racket)

(reset (list (
(lambda (x) (* x x)) (shift k (append-map k '(1 2))) )))

得到这个

(append-map 
(lambda (y) (list ((lambda (x) (* x x)) y))) '(1 2))

结果相同'(1 4)

我想知道是否可以将相同类型的转换(将消除 reset/shift)应用于这样的表达式:

(reset (list (+ 
(shift k (append-map k '(1 2))) 
(shift k (append-map k '(3 4))) )))

结果如何(计算结果为 '(4 5 5 6))。

看看那个页面上的脚注:

[2] This transformation is not strictly correct. In actuality, there are a few more resets placed throughout the output. The details of this fall outside the scope of this introductory text, however, and it is relevant only if we were to use nested shift and reset.

For the sake of completeness, though, here is the correct transformation, if you want it:

 (reset (...A... (shift K E) ...B...)) 
 ; --> 
 (let ((K (lambda (x) (reset (...A... x ...B...))))) 
   (reset E)) 

 (reset E) 
 ; --> 
 E 

所以:

(reset (list (+ (shift k (append-map k '(1 2))) (shift k (append-map k '(3 4))))))

转换为:

(let ((k (lambda (x) (reset (list (+ x (shift k (append-map k '(3 4))))))))) 
  (reset (append-map k '(1 2))))

(reset (list (+ x (shift k (append-map k '(3 4))))))

又转化为:

(let ((c (lambda (y) (reset (list (+ x y)))))) 
  (reset (append-map c '(3 4)))) 

通过使用第二条规则删除 reset,我们有:

(let ((k (lambda (x) (let ((c (lambda (y) (list (+ x y))))) 
                       (append-map c '(3 4))) ))) 
  (append-map k '(1 2)))

作为最终结果。