How does Haskell runtime represent lazy values?

124 views Asked by At

I don't even know how to express this question. A assume there's a pointer to a non evaluated expression. If it's requested (by some strict function that coerces it) then the pointer value is replaced by the value evaluated. Right? Am I wrong?

So I assume every pointer has a flag stating if it has been evaluated or not.

And what if the evaluation is undefined, like the head of an empty list? What is stored in the "pointer"?

1

There are 1 answers

0
Li-yao Xia On BEST ANSWER

I assume there's a pointer to a non evaluated expression. If it's requested (by some strict function that coerces it) then the pointer value is replaced by the value evaluated. Right? Am I wrong?

That's the gist of it.

So I assume every pointer has a flag stating if it has been evaluated or not.

Every pointer points to some structure where you can find that kind of information.

And what if the evaluation is undefined, like the head of an empty list? What is stored in the "pointer"?

The pointer points to an expression whose evaluation throws an exception.

The details are in the following page of the GHC wiki; see in particular "Types of objects": https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/rts/storage/heap-objects

Data constructors, function closures, thunks ("unevaluated expressions") are the main ones.