Deriving types for tree <-> list

Consider the following non sense type

type t =
  | Int of int
  | String of string
  | Plus of (t * t)
  | Neg of t

it forms a ‘tree’. I want to auto derive the following type

type t2 =
  | Int of int
  | String of string
  | Plus of (idx, idx)
  | Neg of idx

and the corresponding conversion functions for t <-> t2 list

Of course need t to be non-cyclic, and not all valid t2 list are valid t.

Questions:

  1. Is there a name for this tree <-> list transform ?

  2. Is there an ocaml ppx to auto derive the types ?

Thanks!

Some people call this flattening (except they use arrays…), see this post about Rust:

Flattening uses an arena that only holds one type, so it’s actually just a plain array, and you can use array indices where you would otherwise need pointers.