Ocaml representation of values - Atoms

2019-06-24 02:41发布

问题:

I looked at the internal representation of some OCaml values. The representation of an empty array is an atom(0), i.e. a block with tag=0 and size=0. Empty arrays of floats are represented by an atom(0) too.

Is there any OCaml value represented by an atom with tag > 0? If not: for what purpose the OCaml bytecode set contains the ATOM n instruction?

回答1:

A tag > 0 is used for constructors with arguments, which would make them not atoms. Constructors without arguments on the other hand are stored as int instead of blocks so again not atoms. So I think atom(0) is not used. Except ...

What about having a constructor with inline record that is empty?

# type t = A of int | B of { };;
Error: Syntax error

Seems empty records are not allowed. I can't think of another way to create a 0 size block with tag other than creating such a block directly. But that wouldn't be using the ATOM instruction.