# How can I get the right hand side of a delayed expression?

Imagine there is a given function f, defined with SetDelayed, say

f[x_] := Sin[x]^2 + Cos[x]^2.


Is it possible to get rhs of this function before evaluation? For instance of f I want to get expression

Sin^2 + Cos^2.

• Somebody had nice suggestion with DownValues, but it disappeared. DownValues is very cool, but I guess my example is too simple. Because I want to extract previous inputs which obviously have executions. In:=(Print; 2+2), where I would like to get the full form of expression, e.g Print; 2+2 without evaluating it. – Vladimir Mar 21 '13 at 8:57
• See my answer - it gives you that. If you are interested in the input, use DownValues[In]. – Leonid Shifrin Mar 21 '13 at 8:58
• @LeonidShifrin Yes, off course, I was just playing in Mathematica with your suggestion. Thank you! Sorry for my delay :) – Vladimir Mar 21 '13 at 9:15
• Nothing to be sorry about :-). There is no rush with anything here on SE, including accepting an answer. It is actually usually better to wait for some time, to give people an incentive to post alternative solutions. – Leonid Shifrin Mar 21 '13 at 9:20
• You can see at mathematica.stackexchange.com/questions/20236/… – minthao_2011 Mar 21 '13 at 12:21

You can do

Hold[f] /. DownValues[f]


which would return to you the r.h.s. wrapped in Hold - which you can then strip or do anything else with it.

Note that while this is a useful technique, in many cases it is not really needed, so I'd first reconsider the design of your functions, and only use the above if it is really necessary.As a light-weight alternative, you can use local rules (RuleDelayed), e.g.

ClearAll[f];
myFormula = f[x_] :> Sin[x]^2 + Cos[x]^2;


Then, you get what you need simply by

f /. myFormula


The general from of this question is what prompted my question and self-answer:
How do I evaluate only one step of an expression?

Using my step function from that answer this is simply:

step @ f

Sin^2 + Cos^2.


The output is wrapped in HoldForm. You could use e.g. Defer @@ if you wish to change the head.

Again, the advantage to this method over working with DownValues is that it is a more general construct, and will work with other kinds of sets that produce UpValues or SubValues etc., as well as internal functions to the limited extent that is possible.

• +1. But you should also mention a disadvantage: generally a single evaluation step will not be what we think it is - there is no easy and general mapping between the chunks of evaluation that we consider a single step, and the steps of Mathematica evaluator. – Leonid Shifrin Mar 24 '13 at 16:01
• @Leonid conceptually this is true, but using my definition: the first step that transforms the entire expression, this has been acceptably predictable for me. Can you give a counter-example where it behaves differently? – Mr.Wizard Mar 24 '13 at 16:03
• Nothing comes to mind now, but I recall that we discussed some such examples in the context of safe ValueQ function. – Leonid Shifrin Mar 24 '13 at 16:10
• B.t.w., I have some time to chat now, if you wish. – Leonid Shifrin Mar 24 '13 at 16:11
• @Mr.Wizard regarding a counter-example, if you do step@f[5+5], you get f, whereas with the method of Leonid, you get the right hand side of f applied to 5+5. In order to get the same result with step, f should have a HoldAll attribute. – faysou Mar 24 '13 at 17:34