Is there a way to efficiently "augment" or attach some more information without much boilerplate?
That is, given types
type orig =
| A
| B of orig
| C of orig * orig
and type dat = int
and type aug =
| AugA of dat
| AugB of dat * aug
| AugC of dat * aug * aug
and functions
let _cnt = ref 0
let gen_dat () =
let _ = _cnt := !_cnt + 1 in
!_cnt
let rec augment (o: orig) : ao =
match o with
| A -> AugA (gen_dat ())
| B o -> AugB (gen_dat (), augment o)
| C (o1, o2) -> AugC (gen_dat (), augment o1, augment o2)
I think this is a trivial structural recursion on its components, and I have to do repeat this type of identical manipulations on several types with 10+ patterns (Multiple aug
s each with its own augment
function associated).
This leads to a long, tedious boilerplate code. Is there a way to abstract away this manipulation in OCaml, e.g., with help of ppx?