Ok, so the goal is to visualize the execution of this expression
foo /@ {3 + 1, bar /@ {1 + 2, {4 + 7}}}
in the following fashion using nested rectangles with mouseover highlighting and tooltips that show the highlighted subexpression's values.
Ok, that works and looks great, but the code is ugly:
Clear[foo, bar];
expr = MapAll[h1,
Unevaluated[foo /@ {3 + 1, bar /@ {1 + 2, {4 + 7}}}],
Heads -> True];
expr = expr //. h1[x_] :> h2[HoldForm[x], h3[x]];
expr = expr //.
h2[x_, y_] :>
Mouseover[
Tooltip[Framed[x, Background -> White, FrameMargins -> 10,
RoundingRadius -> 10], y],
Tooltip[Framed[x, Background -> Hue[0.6, 1, 1, 0.1],
FrameMargins -> 10, RoundingRadius -> 10], y]];
Cleanh2[x___] :=
Module[{xx = {x}},
xx = xx //. {Framed[a_] :> a, Tooltip[b_, c_] :> c,
Verbatim[HoldForm][d__] :> d, Mouseover[e_, f_] :> e};
Sequence @@ xx]
expr = expr //. h3 -> Cleanh2;
MouseAppearance[
Magnify[Dynamic[Evaluate[expr] //. Verbatim[HoldForm][d___] :> d],
1], "+"]
and quickly breaks:
expr = MapAll[h1,
Unevaluated[
Module[{t = RandomInteger[1, 10]},
MapThread[f, {t, RotateLeft[t] + RotateRight[t]}]]],
Heads -> True];
expr = expr //. h1[x_] :> h2[HoldForm[x], h3[x]];
expr = expr //.
h2[x_, y_] :>
Mouseover[
Tooltip[Framed[x, Background -> White, FrameMargins -> 10,
RoundingRadius -> 10], y],
Tooltip[Framed[x, Background -> Hue[0.6, 1, 1, 0.1],
FrameMargins -> 10, RoundingRadius -> 10], y]];
expr = expr //. h3 -> Cleanh2;
MouseAppearance[
Magnify[Dynamic[Evaluate[expr] //. Verbatim[HoldForm][d___] :> d],
1], "+"]
How can I do this cleanly in a general fashion? There must be a way...
Trace
works because nothing is evaluated there, and the output ofTrace
reflects the actual evaluation process. Other approach would be one of traditional debuggers. But for that, you need execution context. Probably doable, but lots of work. $\endgroup$