title: How does OCaml represent lazy values at runtime?
body: <p>This <a href="http://dev.realworldocaml.org/runtime-memory-layout.html" rel="nofollow noreferrer">chapter</a> in Real World OCaml describes the runtime memory layout of different data types. However, there is no discussion on lazy values.</p>
  
  <ul>
  <li>How is <code>lazy_t</code> implemented, i.e., what does its runtime representation and what the key operations that are compiler built-ins? Links to the source code would be appreciated. I looked at the <a href="https://github.com/ocaml/ocaml/blob/cbd0753ec62c9e4e71e502c85af0d38c7dfe851c/stdlib/camlinternalLazy.ml" rel="nofollow noreferrer">CamlinternalLazy</a> module, but the actual representation seems hard to decipher just based on calls to functions in the <code>Obj</code> module there.</li>
  <li>Could someone provide a summary of the changes made to the representation/operations to make it thread safe for the OCaml multicore project? This <a href="https://github.com/ocaml-multicore/ocaml-multicore/pull/226/commits/c52c79038567fa65279e68c7626edbc3e07efe2d" rel="nofollow noreferrer">commit</a> seems to be the one with the implementation, but it seems a bit opaque to me as an outsider.</li>
  </ul>
  
  <p>Note: This question is about <em>the</em> OCaml compiler/runtime. I understand that there is no standard specification for how lazy values should be implemented by <em>an</em> OCaml compiler/runtime.</p>