I read lots of article already on how to translate call/cc into equivalent CPS style and kinda know the basic already but currently I don't understand how racket perform transformation with call/comp (a.k.a call-with-composable-continuation) and stuck here.
What I did first is attempt to translate this racket piece of code:
#lang racket/base
(let ([tag (make-continuation-prompt-tag)])
(println "111")
(println (string-append "222"
(call-with-continuation-prompt
(lambda ()
(println (string-append "333"
(call-with-composable-continuation
(lambda (k)
(println (string-append "444" (k "555")))
;; (abort-current-continuation tag "bbb")
"666")
tag)))
"777")
tag
(lambda (k)
(println (string-append "888" k))
"aaa"
)))))
Into equivalent JS code. Here my current attempt so far:
var prompt = (fn, tag, r0) => { // call-with-continuation-prompt
// Incorrect
// r0(fn((p0) => {
// r0(p0);
// }));
};
var comp = (fn, tag, r0) => { // call-with-composable-continuation
// Incorrect
// fn((pass, r1) => {
// r0(r1(pass));
// });
};
var abort = () => { // abort-current-continuation
};
((r0) => {
console.log("111");
((r1) => {
prompt((r2) => {
((r3)=>{
comp((k, r4) => {
k("555", (r5) => {
console.log("444" + r5);
r4("666");
});
}, "tag", r3);
})
((p3) => {
console.log("333" + p3);
r2("777");
});
}, "tag", r1);
})
((p1) => {
console.log("222" + p1);
r0();
});
})
((p0) => {
throw 0; // throw 0 to exit early
});
How should I fill prompt
and comp
here? Did I missed a CPS step? Is the transformation even possible?