# How to trace value assigned to local variable(variables in Module)?

Bug introduced in v10.1 or earlier and persists through v11.3

CASE:4240757

Here is my code:

Trace[Module[{x}, x = 1; x = 2; x = 3; j = 6; j = 7], x = _, MatchLocalNames -> True]

{}


I want to trace the value set to local variable x, however, it seems that the pattern x = _ could not match.

And if I change the pattern to _ = _, it does.

Trace[Module[{x}, x = 1; x = 2; x = 3; j = 6; j = 7], _ = _, MatchLocalNames -> True]

{{{x$$32240=1},{x32240=2},{x$$32240=3},{j=6},{j=7}}}


And if I trace the variable j:

Trace[Module[{x}, x = 1; x = 2; x = 3; j = 6; j = 7], j = _, MatchLocalNames -> True]

 {{{j=6},{j=7}}}


So, it seems that the pattern x = _ could not match the local variables x inside the Module? I have look at the documentation and set the MatchLocalNames to True, but it doesn't make use (though it is set to True by default). The version is 11.3.

I get this problem when read through the book Power Programming With Mathematica page 415. The code is:

iterSqrt[x_] := Module[{i, j, xn = 1}, While[Abs[xn^2 - x] > 10^-6, i = x/xn;j = xn + i;xn = j/2];xn]


And when trace the variable assigned to xn: Trace[iterSqrt[2], xn = _] , I get {}. And it is not as same as that answers on book:

{{xn$$14 = 1}, {{{xn14 = 3/2}}, {{xn14 = 17/2}}, {{xn14 = 577/408}}, {{xn$$14 = 665857/470832}}}}

• Problem persists in MMA12. Apr 21, 2019 at 15:35

Maybe I missed something more straightforward, anyway:

Trace[
Module[{x}, x = 1; x = 2; x = 3; j = 6; j = 7],
HoldPattern[s_Symbol = _] /; StringStartsQ["x"] @ SymbolName @ Unevaluated @ s
]


{{{x$$13372=1},{x13372=2},{x$$13372=3}}}

I reported Trace[Module[{x}, x = 1], x = _] as a generic example and it was confirmed as a bug:

It does appear that Trace[] is not behaving as described in the documentation. I have forwarded an issue report to our developers with the information you provided.