Function Repository Resource:

FormalizeSymbols

Source Notebook

Convert symbols in an expression into formal symbols

Contributed by: Paco Jain (Wolfram Research)

ResourceFunction["FormalizeSymbols"][expr]

converts all symbols in expr whose context is $Context into unique formal symbols.

ResourceFunction["FormalizeSymbols"][expr,"Rules"]

returns the rules that would be used for replacement in ResourceFunction["FormalizeSymbols"][expr].

Details and Options

Formal symbols like a are standard Wolfram Language symbols that can not be assigned values. Using formal symbols prevents symbolic expression from unintentionally using values from the local environment.
Symbols are replaced with their formal counterparts when possible (and when doing so will not cause collisions between different symbols). If no formal counterpart exists, the symbol is not replaced unless the option setting "ExtraReplacements"True is used. In that case, a random formal symbol not already used will be chosen as the replacement symbol.
ResourceFunction["FormalizeSymbols"] accepts the following options:
"EvaluateOnce"Falsewhether to allow symbols with OwnValues to evaluate exactly once
"ExcludeInputSymbols{}a list of symbols to exclude from the left-hand side of replacement rules
"ExcludeTargetSymbols"{}a list of symbols to exclude from the right-hand side of replacement rules
"ExtraReplacements"Falsewhether to replace symbols that lack a direct formal counterpart
"ReplacementContexts"Automatica list of contexts whose symbols are to be replaced (or All)
"Verbose"Falsewhether to print a message when non-standard replacments are made
ResourceFunction["FormalizeSymbols"] has the HoldFirst attribute to prevent evaluation of defined symbols in expr prior to replacement. However, in the special case that ResourceFunction["FormalizeSymbols"] is called directly on a bare symbol having an ownvalue, with the setting "EvaluateOnce"True, the argument is allowed to evaluate exactly once to allow for intermediate variable assignment (see the second Properties and Relations example).
By default, ResourceFunction["FormalizeSymbols"] only replaces symbols in the current context, as given by $Context (typically having value "Global`"). To replace symbols from other contexts, use the option "ReplacementContexts".
Replaced symbols will always be in the System` context in the output regardless of the context in which they began.

Examples

Basic Examples (4) 

Convert symbols to their formal counterparts:

In[1]:=
ResourceFunction["FormalizeSymbols"][a + b]
Out[1]=

FormalizeSymbols works on Latin, Greek and other symbols for which there exists a formal version in the Wolfram Language:

In[2]:=
ResourceFunction["FormalizeSymbols"][\[Phi]/8]
Out[2]=
In[3]:=
ResourceFunction["FormalizeSymbols"][Cos[\[Sampi]]]
Out[3]=

FormalizeSymbols works on both uppercase and lowercase letters:

In[4]:=
ResourceFunction[
 "FormalizeSymbols"][(r/A) \[Lambda] + \[CapitalLambda]]
Out[4]=

FormalizeSymbols[expr,"Rules"] returns the rules that would be used for replacement in formalizing an expression:

In[5]:=
ResourceFunction[
 "FormalizeSymbols"][(r/A) \[Lambda] + \[CapitalLambda], "Rules"]
Out[5]=

Scope (2) 

By default, FormalizeSymbols does not affect symbols with multi-character symbol names:

In[6]:=
ResourceFunction["FormalizeSymbols"][a + blah]
Out[6]=

To replace multi-character symbols as well, use the setting "ExtraReplacements"True:

In[7]:=
ResourceFunction["FormalizeSymbols"][a + blah, "ExtraReplacements" -> True]
Out[7]=

By default, FormalizeSymbols only affects symbols in the current context (here "Global`"). To allow replacement of symbols in other contexts, use the option "ReplacementContexts":

In[8]:=
ResourceFunction["FormalizeSymbols"][myContext`a + b]
Out[8]=
In[9]:=
ResourceFunction["FormalizeSymbols"][myContext`a + b, "ReplacementContexts" -> All]
Out[9]=

Options (11) 

EvaluateOnce (2) 

In the special case that FormalizeSymbols is directly called on a bare symbol having an own value, its argument can be allowed to evaluate exactly once using the option setting "EvaluateOnce"True, as in the following case:

In[10]:=
Clear[a, b]
x = a + b;
ResourceFunction["FormalizeSymbols"][x, "EvaluateOnce" -> True]
Out[12]=

Without this top-level evaluation, the above inputs would result in x:

In[13]:=
ResourceFunction["FormalizeSymbols"][x]
Out[13]=

With "EvaluateOnce"True, evaluation does not proceed beyond top-level:

In[14]:=
Clear[a, b, x]
z = x;
x = a + b;
ResourceFunction["FormalizeSymbols"][z, "EvaluateOnce" -> True]
Out[17]=

Contrast this result with using Evaluate, which triggers full evaluation:

In[18]:=
ResourceFunction["FormalizeSymbols"][Evaluate[z]]
Out[18]=

ExcludeInputSymbols (1) 

To exclude a given set of symbols from being replaced, use the option "ExcludeInputSymbols". Compare:

In[19]:=
ResourceFunction["FormalizeSymbols"][x + i y]
Out[19]=
In[20]:=
ResourceFunction["FormalizeSymbols"][x + i y, "ExcludeInputSymbols" -> {i}]
Out[20]=

ExcludeTargetSymbols (1) 

To exclude a given set of formal symbols as possible replacement characters, use the option "ExcludeTargetSymbols". Compare:

In[21]:=
ResourceFunction["FormalizeSymbols"][x + i y]
Out[21]=
In[22]:=
ResourceFunction["FormalizeSymbols"][x + i y, "ExcludeTargetSymbols" -> {\[FormalI]}]
Out[22]=

ExtraReplacements (2) 

By default, FormalizeSymbols does not replace symbols that lack a direct formal equivalent:

In[23]:=
ResourceFunction["FormalizeSymbols"][a + blah]
Out[23]=

To replace all symbols, regardless of name, use the setting "ExtraReplacements"True:

In[24]:=
ResourceFunction["FormalizeSymbols"][a + blah, "ExtraReplacements" -> True]
Out[24]=

ReplacementContexts (4) 

The default setting of "ReplacementContexts"Automatic replaces only symbols in $Context (here Global`):

In[25]:=
ResourceFunction["FormalizeSymbols"][myContext`a + b]
Out[25]=

The "ReplacementContexts" option can be set to a list of contexts for which symbols should be formalized:

In[26]:=
ResourceFunction["FormalizeSymbols"][myContext`a + yourContext`b, "ReplacementContexts" -> {"myContext`"}]
Out[26]=

With the setting "ReplacementContexts"All, symbols in any context other than System` are replaced:

In[27]:=
ResourceFunction["FormalizeSymbols"][myContext`a + yourContext`b, "ReplacementContexts" -> All]
Out[27]=

FormalizeSymbols automatically avoids symbol collisions:

In[28]:=
ResourceFunction["FormalizeSymbols"][myContext`a + yourContext`a, "ReplacementContexts" -> All]
Out[28]=

With the option setting "Verbose"True a warning is issued about potential collisions:

In[29]:=
ResourceFunction["FormalizeSymbols"][myContext`a + yourContext`a, "ReplacementContexts" -> All, "Verbose" -> True]
Out[29]=

Verbose (1) 

With the setting "Verbose" True, a message is issued whenever a “non-standard” replacement is made, either due to potential collisions or due to replacement of a symbol that lacks a direct formal equivalent (as possible when "ExtraReplacements"True):

In[30]:=
ResourceFunction["FormalizeSymbols"][a + blah, "ExtraReplacements" -> True, "Verbose" -> True]
Out[30]=
In[31]:=
ResourceFunction["FormalizeSymbols"][myContext`a + yourContext`a, "ReplacementContexts" -> All, "Verbose" -> True]
Out[31]=

Properties and Relations (3) 

FormalizeSymbols has the HoldFirst attribute to prevent (with default option settings) evaluation of defined symbols prior to replacement:

In[32]:=
Clear[a, b]
a = 2;
ResourceFunction["FormalizeSymbols"][a + b]
Out[33]=

For the special case of pure symbols having OwnValues, pure HoldFirst behavior can be overridden using the "EvaluateOnce" option:

In[34]:=
Clear[a, b]
a = b;
b = 2;
ResourceFunction["FormalizeSymbols"][a]
Out[35]=
In[36]:=
ResourceFunction["FormalizeSymbols"][a, "EvaluateOnce" -> True]
Out[36]=

Contrast this result with using Evaluate, which triggers full evaluation:

In[37]:=
ResourceFunction["FormalizeSymbols"][Evaluate[a]]
Out[37]=

Symbols within the System` context are never replaced. In particular, this includes Pi, N, C, D, E, I, K, and O

In[38]:=
ResourceFunction["FormalizeSymbols"][a \[Pi]/2, "ReplacementContexts" -> All]
Out[38]=
In[39]:=
ResourceFunction["FormalizeSymbols"][Hold[N[a + b]], "ReplacementContexts" -> All]
Out[39]=
In[40]:=
ResourceFunction["FormalizeSymbols"][C[1] + \[Lambda], "ReplacementContexts" -> All]
Out[40]=

Publisher

Wolfram|Alpha Math Team

Version History

  • 2.4.0 – 21 July 2023
  • 2.3.0 – 28 June 2023
  • 2.2.0 – 24 June 2021

Related Resources

License Information