Here is a very long and complicated expression, which we abbreviate as a. I store it using SetDelayed because I want to perform algebraic manipulations on it:
ClearAll[a];
a := 1 + 1
Here is a really complicated function f with attributes HoldFirst that operates on its first argument. It counts the number of times 1 appears in its first argument.
ClearAll[f];
SetAttributes[f, HoldFirst];
f[input_] := Module[{expr=Hold[input]},
Count[expr,1,{0,Infinity}]
]
As you can see, directly inserting the complicated expression works, but not if you insert the abbreviation a:
f[1+1]
(*2*) (* good *)
f[a]
(*0*) (* not good *)
The reason it doesn't work is because Hold doesn't allow inserting a definition. So, in the second example, Count is seeing the symbol a and not the expression 1+1 to which it points.
Question: How do I insert OwnValues verbatim inside a held expression without evaluating it?
SIMPLE EXAMPLE
ClearAll[a];
a := 1 + 1
Here is a sample held expression containing symbols which may or may not have OwnValues:
Hold[a + b + c]
How do I insert the RHS of the definition of a verbatim into the held expression, so that the result is this?:
Hold[(1 + 1) + b + c]
I have the following (which may or may not be fruitful):
Hold[a + b + c] /. (symb_Symbol /; OwnValues[symb] =!= {} :>
RuleCondition[First[OwnValues[symb]]])
(* Hold[(HoldPattern[a] :> 1 + 1) + b + c] *)
UpValuesDownValuesSubValuesetc.. should not be inserted into the held expression. – QuantumDot Jun 09 '16 at 12:05Hold[a+b+c]/.OwnValues[a]? – Leonid Shifrin Jun 09 '16 at 12:05ais the symbol withOwnValues. How do I make this replacement for all symbols which haveOwnValueswithout knowing beforehand which ones have them? – QuantumDot Jun 09 '16 at 12:12