# How to debug error triggered by replacement rule?

The second line below produces the errors shown:

p = LogLogPlot[Abs[10^2/((I ω)^2 + 100)], {ω, 10^0, 10^5}];
p /. x_ /; And @@ NumericQ /@ x :> x[[0]];
Part::pspec: Part specification True is neither a machine-sized integer nor a list of machine-sized integers. >>
Part::pspec: Part specification True is neither a machine-sized integer nor a list of machine-sized integers. >>


I have run out of ideas for pinpointing the specific reason for the error (let alone fixing the rule to avoid it).

Is there some command or tool or systematic way to debugging such errors?

-
InputForm[p] gives the expression and Short[InputForm[p], 10] gives the short form of p. Please look at it and say, what do you need to find/extract/replace. – Vahagn Poghosyan Sep 21 '13 at 0:02
@VahagnPoghosyan: The code in the second line comes from mathematica.stackexchange.com/a/32720/2464 – kjo Sep 21 '13 at 0:06
In any case, if you need to extract the data points, I would suggest you to use p /. Line[x_List] :> (data = x); – Vahagn Poghosyan Sep 21 '13 at 0:13

The debugger in the notebook interface can give more information about errors of this kind. Granted, that information is sometimes cryptic -- but it is better than no information at all.

Let's try it with the expressions from the question.

We'll start by going to the Evaluation menu to enable the Debugger option:

In the Debug palette that appears, click on Show Stack and tick the Break at Messages checkbox:

Now, evaluate the expressions. The first message will appear, the evaluation will be suspended, and the Stack window will show where the error occurred:

The bottom expression in the Stack window is Message, which generated the message that we see. Above that is the offending expression, False[[True]]. This was the result of evaluating the rather strange expression NumericQ[#1][[NumericQ[2]]]. We can see that the head of this expression was generated from NumericQ /@ x where x is #1.

The strange expression is unexpected and is a strong clue about what has gone wrong. Apparently, x is being bound to all kinds of expressions, not just lists. Our pattern is not restrictive enough. We certainly want to restrict it to lists (e.g. x_List), but I suspect that the condition needs to be stronger as well (depending on the goal).

Having concluded our analysis, we can either choose the Abort or Finish option from the Debug palette, as suits our fancy.

It is not entirely clear to me what the purpose of these expressions are. If you are trying to extract the data from the plot then Cases might be more appropriate than /., perhaps something like:

First @ Cases[p, Line[pts_] :> pts, Infinity, 1]


If the intent is to make minor tweaks to the Graphics expression, then the pattern and condition will need to be restrictive enough to match only the desired list or lists of numbers.

-

The error occures, since the replacement operator scans not only lists, but also other expressions. The following expression doesn't give errors:

p = LogLogPlot[Abs[10^2/((I \[Omega])^2 + 100)], {\[Omega], 10^0, 10^5}];
p /. x_List /; And @@ NumericQ /@ x :> x[[0]];

-