How do I insert OwnValues inside a held expression without evaluating it? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-21T18:38:37Z https://mathematica.stackexchange.com/feeds/question/118030 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/118030 9 How do I insert OwnValues inside a held expression without evaluating it? QuantumDot https://mathematica.stackexchange.com/users/2048 2016-06-09T12:02:12Z 2016-06-15T14:56:07Z <p>Here is a very long and complicated expression, which we abbreviate as <code>a</code>. I store it using <code>SetDelayed</code> because I want to perform algebraic manipulations on it:</p> <pre><code>ClearAll[a]; a := 1 + 1 </code></pre> <p>Here is a really complicated function <code>f</code> with attributes <code>HoldFirst</code> that operates on its first argument. It counts the number of times <code>1</code> appears in its first argument.</p> <pre><code>ClearAll[f]; SetAttributes[f, HoldFirst]; f[input_] := Module[{expr=Hold[input]}, Count[expr,1,{0,Infinity}] ] </code></pre> <p>As you can see, directly inserting the complicated expression works, but not if you insert the abbreviation <code>a</code>:</p> <pre><code>f[1+1] (*2*) (* good *) f[a] (*0*) (* not good *) </code></pre> <p>The reason it doesn't work is because <code>Hold</code> doesn't allow inserting a definition. So, in the second example, <code>Count</code> is seeing the symbol <code>a</code> and not the expression <code>1+1</code> to which it points.</p> <p><strong>Question:</strong> How do I insert <code>OwnValues</code> verbatim inside a held expression without evaluating it?</p> <hr> <p><em>SIMPLE EXAMPLE</em></p> <pre><code>ClearAll[a]; a := 1 + 1 </code></pre> <p>Here is a sample held expression containing symbols which may or may not have <code>OwnValues</code>:</p> <pre><code>Hold[a + b + c] </code></pre> <p>How do I insert the RHS of the definition of <code>a</code> verbatim into the held expression, so that the result is this?:</p> <pre><code>Hold[(1 + 1) + b + c] </code></pre> <p>I have the following (which may or may not be fruitful):</p> <pre><code>Hold[a + b + c] /. (symb_Symbol /; OwnValues[symb] =!= {} :&gt; RuleCondition[First[OwnValues[symb]]]) (* Hold[(HoldPattern[a] :&gt; 1 + 1) + b + c] *) </code></pre> https://mathematica.stackexchange.com/questions/118030/-/118034#118034 10 Answer by Kuba for How do I insert OwnValues inside a held expression without evaluating it? Kuba https://mathematica.stackexchange.com/users/5478 2016-06-09T12:22:19Z 2016-06-09T17:44:56Z <pre><code>ClearAll[a, b]; a := 1 + 1 b = Sqrt </code></pre> <p>Is this acceptable?</p> <pre><code>foo = # /. Join @@ Cases[#, s_Symbol :&gt; OwnValues[s], ∞, Heads-&gt;True] &amp;; foo @ Hold[a + b[c]] </code></pre> <blockquote> <pre><code>Hold[(1 + 1) + Sqrt[c]] </code></pre> </blockquote> <hr> <h3>Update</h3> <p>As OP has noticed I've missed the fact that <code>ReadProtected</code> symbols won't show its <code>OwnValues</code>.</p> <p>We could do something like <code>s_Symbol /; FreeQ[ Attributes[s], ReadProtected]</code> but why should we skip that symbol if we can just evaluate it to get its <code>OwnValue</code>?</p> <p>Here's alternative approach:</p> <pre><code>ClearAll[a, b]; SetAttributes[{b, d}, ReadProtected] a := 1 + 1 b = Sqrt d := 1 + 2 foo = # /. Join @@ Cases[#, s_Symbol :&gt; If[ FreeQ[Attributes[s], ReadProtected], OwnValues[s], {HoldPattern[s] :&gt; Evaluate@s} ], \[Infinity], Heads -&gt; True] &amp;; foo@Hold[a + b[c] + I + d] </code></pre> <blockquote> <pre><code>Hold[(1 + 1) + Sqrt[c] + I + 3] </code></pre> </blockquote> <p>As you can see <code>d</code> is inserted but not as <code>1+2</code>, that's the price of <code>ReadProtected</code>. We could <code>Unprotect</code> but it wouldn't work for <code>Locked</code> symbols. So at the end it's up to OP how to handle those cases.</p>