
Thank you for the amazing sample code; I am still in the process of studying it. I will be posting followup questions to this in the following days.

Meanwhile, I am reading up on precisely what let%sub, let%map, let%arr, match%sub, match%arr
desugars to, to better understand what is going on.
Quoting https://github.com/janestreet/bonsai/blob/master/ppx_bonsai/readme.md?plain=1#L8L59 I want to focus on the last part:
The difference is that the second option binds a
and b
to the
computations that map temp_var
to its components, but the first option
binds a
and b
to the components after the mappings have occurred.
Conceptually this means that for the first, correct desugaring, reusing a
and
b
does not duplicate the mapping computation, but for the second desugaring, every
usage of a
or b
refers to a duplicate of its computation.
This seems a bit over dramatic to me. My understanding is that even in the âincorrectâ usage of:
let%sub temp_var = c in
let a = map ~f:(fun (a, _) > a) temp_var in
let b = map ~f:(fun (_, b) > a) temp_var in
BODY
even in this âincorrectâ usage, the c/temp_var is only computed once. The only duplicated work on reusing a/b are the:
fun (a, _) > a
fun (_, b) > a
I understand that when we âreuseâ a Value.t, we have to redo the work â but in this case, it seems this is not a very convincing examples, as the âredo workâ is very cheap.
What this example be better written as
let%sub temp_var = c in
let a = map ~f:(fun (a, _) > some_expensive_func1 a) temp_var in
let b = map ~f:(fun (_, b) > some_expensive_func2 a) temp_var in
BODY
note the addition of the âsome_expensive_func1/2â â in this case, weâd want to use a %sub/return to cache the result instead of recomputing on it on the reuse of a/b.
However, this would no longer fit into the point of explaining let%sub (a, b) = c
.
My current confusion here is Iâm not sure if the situation is:

I am misunderstanding something OR

The example in the highlighted section is not very convincing, as all the âextra workâ is merely pulling the first/second elem of a tuple.
Thanks!