3
$\begingroup$

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},{x$32240=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}, {{{xn$14 = 3/2}}, {{xn$14 = 17/2}}, {{xn$14 = 577/408}}, {{xn$14 = 665857/470832}}}}
$\endgroup$
  • $\begingroup$ Problem persists in MMA12. $\endgroup$ – user58955 Apr 21 at 15:35
4
$\begingroup$

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},{x$13372=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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.