Continuation versus call stack in Haskell

283 views Asked by At

How does Haskell (GHC) runtime know what code should be executed next after evaluation of a thunk?

On a conceptual level, how is that different from call stack in other programming languages (other than storing closed variables on heap and having tail recursion)?

1

There are 1 answers

2
MathematicalOrchid On

GHC (or rather, the GHC RTS) has a normal call stack, like most things.

What's different is the contents of this stack. It doesn't match what you might expect.

Suppose that function foo calls function bar which calls function baz. You might be expecting the call stack to look like

foo
bar
baz

at some point. But actually, when foo calls bar, all the "call" really does is create a thunk and return instantly. So bar doesn't appear on that stack at that point. But when foo returns some data to the caller, and the caller decides to do something with it, at that point bar may appear on the call stack, even though foo is nowhere to be seen.

In short, the order of stuff on the call stack is unrelated to who calls who. It's determined by who looks at what.