Wolfram Research

Function Repository Resource:

SymbolDependencies (1.0.0) current version: 1.0.1 »

Source Notebook

Get a list of symbols that an expression depends on

Contributed by: Richard Hennigan (Wolfram Research)

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:


Examples

Basic Examples (2) 

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 (1) 

Any expression can be used in the first argument:

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

Options (6) 

IgnoringUndefined (1) 

Ignore undefined symbols:

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

IgnoreModuleSymbols (2) 

By default, local symbols left over 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 (2) 

By default, the given symbol will not 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 (1) 

Exclude symbols from specific contexts:

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

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.1 – 09 January 2023
  • 1.0.0 – 11 October 2018

Related Resources

License Information