Good day,
I thought earlier that Mathematica creates new symbols in the current $Context
at the stage of converting of the input string (that is assigned to InString
) to input expression (that is assigned to In
). But one simple example has broken this explanation:
In[1]:= ?f
During evaluation of In[1]:= Information::notfound: Symbol f not found. >>
In[2]:= Names["`*"]
Out[2]= {}
In[3]:= DownValues[In]//First
InString[1]
Names["`*"]
Out[3]= HoldPattern[In[1]]:>Information[f,LongForm->False]
Out[4]= \(? f\)
Out[5]= {}
You can see that there is no symbol f
in the $ContextPath
although it is already used inside definition for In[1]
.
This example shows that it is in principle possible in Mathematica to make definitions with symbols that do not exist in the $ContextPath
without creating them. This could be interesting alternative to the method of avoiding symbol creation using Symbol
:
In[9]:= ff := Symbol["f"]
Names["`*"]
Out[10]= {"ff"}
Can anybody explain at which conditions and at which stage of the evaluation process Mathematica creates new Symbols?
EDIT
As Sasha have noticed in the comment to this question, in really I was spoofed by default ShowStringCharacters->False
settings for the Output cells in the default stylesheet Core.nb and missed the FullForm
of the output for DownValues[In]//First
. In really symbol f
is not used in the definition for In[1]
as we can see also by using InputForm
:
In[1]:= ?f
DownValues[In]//First//InputForm
During evaluation of In[1]:= Information::notfound: Symbol f not found. >>
Out[2]//InputForm=
HoldPattern[In[1]] :> Information["f", LongForm -> False]
Sorry for hasty statement.
So the question now is just about the stage at which Mathematica decides to create new Symbol
and how we can prevent it?
For example, in the above example we input f
as Symbol
but Mathematica converts it to String
without creating new symbol. This is built-in behavior of MakeExpression
:
In[1]:= ?f
InputForm[MakeExpression[ToExpression@InString[1], StandardForm]]
During evaluation of In[1]:= Information::notfound: Symbol f not found. >>
Out[2]//InputForm=
HoldComplete[Information["f", LongForm -> False]]
Probably it is possible to define some type of syntactic construct that will prevent symbol creation until the evaluation time.
About stage of evaluation when new symbol is created
We can see that incrementing $Line
happens before calling MakeExpression
but new Symbol
creation and assigning new value for InString
and In
variables happens after calling MakeExpression
:
In[1]:= MakeExpression[My`boxes_,My`f_]/;!TrueQ[My`$InsideMakeExpression]:=Block[{My`$InsideMakeExpression=True},Print[$Line];Print[DownValues[InString][[All,1]]];Print[DownValues[In][[All,1]]];Print[Names["`*"]];MakeExpression[My`boxes,My`f]];
In[2]:= a
During evaluation of In[2]:= 2
During evaluation of In[2]:= {HoldPattern[InString[1]]}
During evaluation of In[2]:= {HoldPattern[In[1]]}
During evaluation of In[2]:= {}
Out[2]= a
The same we can say about $PreRead
and $NewSymbol
call time:
In[1]:= $NewSymbol:=Print["Names[\"`*\"]=",Names["`*"],"\nDownValues[InString]=",DownValues[InString][[All,1]],"\nDownValues[In]=",DownValues[In][[All,1]],"\nName: ",#1,"\tContext: ",#2]&
In[2]:= a
During evaluation of In[2]:= Names["`*"]={}
DownValues[InString]={HoldPattern[InString[1]]}
DownValues[In]={HoldPattern[In[1]]}
Name: a Context: Global`
Out[2]= a
$Pre
executes after new assignment to In
is made and after creating all new Symbol
s in the current $Context
:
In[1]:= $Pre := (Print[Names["`*"]];
Print[DownValues[In][[All, 1]]]; ##) &
In[2]:= a
During evaluation of In[2]:= {a}
During evaluation of In[2]:= {HoldPattern[In[1]],HoldPattern[In[2]]}
Out[2]= a
It seems that it is not possible to intercept assigning new value for In
variable.
The conclusion: new Symbol
s are created after calling $PreRead
, MakeExpression
and $NewSymbol
but before calling $Pre
.