My background is Javascript, Python & a bit of Haskell. I am trying to understand callCC, there are many explanations but I can't find them trivial & I came across this https://www.cs.bham.ac.uk/~hxt/research/Logiccolumn8.pdf and I felt like I almost got it but I need some help to understand C code.
Below is the Code for Jumping back into a function in GNU C.
void *label_as_result() {
return &&L;
L: printf("Jumped back into the function. \n");
}
main () {
void *p;
p = label_as_result();
printf("The function returned; now jump back into it.\n");
goto *p;
}
What does return statement do in label_as_result function? Does p in main store the stack frame in heap & the instruction line where it is stopped? Jump back to function means create a stack frame again and continue from where we left?
Below this code there is paragraph
But in a language with first-class functions and callCC, no such implementation restrictions apply. Just as with the label as a result in C, we can return a continuation introduced by callCC from a function so as to jump back into the function. When we did this with goto, the stack was smashed, but with callCC the function just returns again. Consider the following function
λ(). callCC(λk. λx. throw k (λy. x))
The continuation k is returned as part of the result, roughly analogous to returning a label as the result in C.
What does stack was smashed, he meant stackoverflow can happen if you use goto? How callCC is getting around this problem using trampolining?
As many say callCC gives Early return semantics that means is it like yield in python or Javascript? Is it possible to write callCC in Javascript using yield?
How I conceive the above code in Javascript
function* label_as_result() {
yield () => console.log("Jumped back into the function.");
}
p = label_as_result().next().value;
console.log("The function returned; now jump back into it.");
p();
It can even simple be written without any generators concept as
function label_as_result() {
return () => console.log("Jumped back into the function.");
}
p = label_as_result();
console.log("The function returned; now jump back into it.");
p();
That means callCC is a function that returns a continuation but all other functions take continuations. Continuations are like Undecided code that need to be performed in future but callCC is like predefined code that needs to be performed in Future? (I am talking in perspective of a framework and the user's code)