Lately I have been interested in better understanding the optimizations done by compiler back-ends. I thought that using Mathematica to explore this might be helpful because it makes creating, displaying and manipulating syntax trees pretty easy.
I thought I would start simple and look at constant propagation. So I wrote a simple function and tried to look at the syntax tree.
f2[x_, y_] := Module[{temp1},
temp1 = 5;
Return[(x + temp1)*y];
]
FullForm[f2]
The result of FullForm[f2], however, was just f2. I know that the right hand side of this expression has to be stored somewhere in Mathematica, so my question is where is it and is it possible to modify it after creating this rule using the SetDelayed operator “:=”?
In the mean time I have discovered that I can use the Function symbol to achieve what I am aiming for, but I would still like to understand what Mathematica is doing a little better.
f1 = Function[{x, y}, Module[{temp1},
temp1 = 5;
Return[(x + temp1)*y]
]
]
TreeForm[f1]

SetandSetDelayeddefinitions with patterns produce so-called down-value definitions that can be accessed withDownValues:You can even assign back to
DownValues[f2]:Simple assignments produce own-value definitions that can be accessed and manipulated with
OwnValues:Here’s a tutorial with more information about value lists.
HTH!