Hi,
I want to know how to stop the evaluation of expressions after substituting in a value using pattern replacement. I have tried combinations of Unevaluated, Hold, etc. however they seem only to be effective at halting the evaluation of the pattern rule and/or replacement before the substitution happens (that is, in the testing I have tried). A first simple example:
In[26]:= exp1 = a^2
Out[26]= a^2
In[27]:= exp1 /. a -> 2
Out[27]= 4
Semi-solution: Ugly because the Hold remains part of the output. I fear any attempt to remove the Hold (using pattern replacement) will cause the expression to evaluate. Any suggestions on solving this sort of problem would be great.
In[34]:= exp1 /. a -> Hold[2]
Out[34]= Hold[2]^2
Second example (mainly included because of my lack of experience with how matching needs to be done on the alternate forms of representing the Derivative of a function; i.e. Derivative[1][y][x] vs. D[y[x],x] vs y'[x])
In[41]:= D[y[x], x] /. y[x] -> x^2
Out[41]= Derivative[1][y][x]
In[47]:= FullForm[D[y[x], x]]
Out[47]//FullForm= Derivative[1][y][x]
In[36]:= ReleaseHold[D[Hold[y[x]], x] /. y[x] -> Hold[x^2]]
Out[36]= PartialD[x, Hold[x^2]] (Note: Not correct formatting; code below will type format correctly in Mathematica)
\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\(Hold[
\*SuperscriptBox[\(x\), \(2\)]]\)\)
This example is motivated by my wanting to create a tool to help go through the steps of checking whether solutions to algebraic and differential equations are correct. I am aware of using Trace to go through the evaluation of the expressions; which incidently keeps the 2^2 unevaluated without a Hold[2]^2 in the expression tree for the code below (Unfortunately, Out[48] needs to be copy and pasted into Mathematica to get the correct typesetting.)
In[48]:= Trace[exp1 /. a -> 2]
Out[48]= {{\!(* TagBox["exp1", HoldForm]), \!(* TagBox[ SuperscriptBox["a", "2"], HoldForm])}, {\!(* TagBox[ RowBox[{"a", "->", "2"}], HoldForm]), \!(* TagBox[ RowBox[{"a", "->", "2"}], HoldForm])}, \!(* TagBox[ RowBox[{ SuperscriptBox["a", "2"], "/.", " ", RowBox[{"a", "->", "2"}]}], HoldForm]), \!(* TagBox[ SuperscriptBox["2", "2"], HoldForm]), \!(* TagBox["4", HoldForm])}
I however at some point I would like to increase the granularity of the number of steps to show during the evaluation process, because showing all levels of the trace for complicated expressions can get unwieldy (for instance if I included every level with Plus and Times). If anyone has any comments or suggestions (or know if this is already done in some open source code/example), it would be greatly appreciated.
Thanks in advance, Joe