Wolfram Computation Meets Knowledge

SymbolDependencies

Contributed by: Richard Hennigan (Wolfram Research)

Get a list of symbols that an expression depends on

ResourceFunction["SymbolDependencies"][sym]

finds symbols that appear in the definition of sym.

ResourceFunction["SymbolDependencies"][sym,h]

wraps the head h around each symbol before returning.

ResourceFunction["SymbolDependencies"][sym,h,n]

recursively finds symbols using up to n steps.

Details and Options

ResourceFunction["SymbolDependencies"][sym] is equivalent to ResourceFunction["SymbolDependencies"][sym,Identity].
In ResourceFunction["SymbolDependencies"][sym,h,n],n can be a positive integer or All.
ResourceFunction["SymbolDependencies"] has the following options:
"IgnoringUndefined" False whether to return undefined symbols
"IgnoreModuleSymbols" True whether to return symbols that were created by Module
"Self" False decide if a symbol depends on itself
"ExcludedContexts" Automatic which contexts to ignore when looking for symbols

Examples

Basic Examples

Retrieve the ResourceFunction:

In[1]:=
ResourceFunction["SymbolDependencies"]
Out[1]=

Get the dependencies of a symbol:

In[2]:=
f[x_] := g[x];
g[x_] := h[x + 1];
h[x_] := 2 x;
ResourceFunction["SymbolDependencies"][f]
Out[5]=

Apply a wrapper to each symbol:

In[6]:=
x = 1;
ResourceFunction["SymbolDependencies"][f, Hold]
Out[7]=

Specify a level:

In[8]:=
ResourceFunction["SymbolDependencies"][f, Hold, 2]
Out[8]=

Get full dependencies:

In[9]:=
a := b; b := c; c := d; d := e;
ResourceFunction["SymbolDependencies"][a, Hold, All]
Out[10]=

Scope

Any expression can be used in the first argument:

In[11]:=
ResourceFunction["SymbolDependencies"][f[y, z]]
Out[11]=

Options

IgnoringUndefined

Ignore undefined symbols:

In[12]:=
ResourceFunction["SymbolDependencies"][f[y, z], "IgnoringUndefined" -> True]
Out[12]=

IgnoreModuleSymbols

By default local symbols leftover from Module evaluations are ignored:

In[13]:=
expr = Module[{f}, f[x_] := x + 1; f]
Out[13]=
In[14]:=
ResourceFunction["SymbolDependencies"][expr]
Out[14]=

Include Module symbols:

In[15]:=
ResourceFunction["SymbolDependencies"][expr, "IgnoreModuleSymbols" -> False]
Out[15]=

Self

By default, the given symbol won’t be included in the output:

In[16]:=
ClearAll[f, x];
f[0] = 1;
f[x_] := x*f[x - 1];
ResourceFunction["SymbolDependencies"][f]
Out[19]=

Include the symbol:

In[20]:=
ResourceFunction["SymbolDependencies"][f, "Self" -> True]
Out[20]=

ExcludedContexts

Exclude symbols from specific contexts:

In[21]:=
ResourceFunction["SymbolDependencies"][MyContext`g[f[n]], "ExcludedContexts" -> {"System`", "Global`"}]
Out[21]=

Resource History