# DownValues when using Set and SetDelayed

It is known from several questions asked in SE that one of the basic differences between Set and SetDelayed is that the former evaluates the right hand side at the time of assignment whereas the other is not.

When I looked at the DownValues in both cases I found that both definitions result into same DownValues structures.

For example:

t = x + y
f[x_, y_] := 2 x t
g[x_, y_] = 2 x t


Now:

DownValues/@{f,g}

(*{{HoldPattern[f[x_, y_]] :> 2 x t}, {HoldPattern[g[x_, y_]] :> 2 x (x + y)}}*)


As you can see here that both Set and SetDelayed transformed into same DownValues structures (HoldPattern[function pattern] :> right hand side)

My questions are:

1-Is it correct that internally both will be treated equally based on DownValues structure.

2- If I want to define function using SetDelayed I can do it like this:

DownValues[h]={HoldPattern[h[x_, y_]] :> 2 x t}; Now how to do same thing with Set

Thanks

• @Guess who it is, nice catch. It is embarrassing to make mistake with function name that you continually use. -:) – Algohi Jul 13 '15 at 10:11
• You can actually just DownValues[h]={HoldPattern[h[x_, y_]] -> 2 x t}; to emulate Set. When you look at DownValues, it will always be shown as :> and never as ->. But when you set it you can use ->. When storing it, :> is necessary because the downvalue should not change just because t was assigned a different value after the downvalue was set. Mathematica does keep track of whether a (downvalue) definition was made with = or :=, and does print it differently with Information or Defitnition. I think I once asked about where this information is stored, but ... – Szabolcs Jul 13 '15 at 11:40
• ... I forgot the answer and cannot find the questin anymore. – Szabolcs Jul 13 '15 at 11:41
• @Szabolcs that explain things better. When you say MMA trucks whether a DownValues definition was mad with = or :=, what makes difference then if at the end the substitution will look at DownValues only? In another words why does MMA keep truck the definitions? – Algohi Jul 14 '15 at 9:56
• Strongly related SO thread: stackoverflow.com/q/5919284/590388 – Alexey Popkov Nov 10 '15 at 12:08

To do same thing with Set
DownValues[h] = {First[Hold[HoldPattern[h[x_, y_]] :> 2 x t] /. t -> x + y ]}; 