Wolfram Research, Inc.

2.13.1 The Main Loop

In any interactive session, Mathematica effectively operates in a loop. It waits for your input, processes the input, prints the result, then goes back to waiting for input again. As part of this "main loop", Mathematica maintains and uses various global objects. You will often find it useful to work with these objects.

You should realize, however, that if you use Mathematica through a special front end, your front end may set up its own main loop, and what is said in this section may not apply.

Input and output expressions.

In a standard interactive session, there is a sequence of input and output lines. Mathematica stores the values of the expressions on these lines in In[n] and Out[n].

As indicated by the usual In[n]:= prompt, the input expressions are stored with delayed assignments. This means that whenever you ask for In[n], the input expression will always be re-evaluated in your current environment.

This assigns a value to x.

In[1]:= x = 7

Out[1]=

Now the value for x is used.

In[2]:= x - x^2 + 5x - 1

Out[2]=

This removes the value assigned to x.

In[3]:= x =.

This is re-evaluated in your current environment, where there is no value assigned to x.

In[4]:= In[2]

Out[4]=

This gives the textual form of the second input line, appropriate for editing or other textual manipulation.

In[5]:= InString[2] // InputForm

Out[5]//InputForm= "x - x^2 + 5x - 1"

Specifying the length of session history to keep.

Mathematica by default stores all your input and output lines for the duration of the session. In a very long session, this may take up a large amount of computer memory. You can nevertheless get rid of the input and output lines by explicitly clearing the values of In and Out, using Unprotect[In, Out], followed by Clear[In, Out]. You can also tell Mathematica to keep only a limited number of lines of history by setting the global variable $HistoryLength.

Note that at any point in a session, you can reset the line number counter $Line, so that for example new lines are numbered so as to overwrite previous ones.

Global functions used in the main loop.

Mathematica provides a variety of "hooks" that allow you to insert functions to be applied to expressions at various stages in the main loop. Thus, for example, any function you assign as the value of the global variable $Pre will automatically be applied before evaluation to any expression you give as input.

For a particular input line, the standard main loop begins by getting a text string of input. Particularly if you need to deal with special characters, you may want to modify this text string before it is further processed by Mathematica. You can do this by assigning a function as the value of the global variable $PreRead. This function will be applied to the text string, and the result will be used as the actual input string for the particular input line.

This tells Mathematica to replace << ... >> by ... in every input string.

In[6]:= $PreRead = StringReplace[#, {"<<" -> "{", ">>" -> "}"}]&

Out[6]=

You can now enter braces as double angle brackets.

In[7]:= <<4, 5, 6>>

Out[7]=

You can remove the value for $PreRead like this, at least so long as your definition for $PreRead does not modify this very input string.

In[8]:= $PreRead =.

Once any $PreRead processing on an input string is finished, the string is read by Mathematica. At this point, Mathematica may find that there is a syntax error in the string. If this happens, then Mathematica calls whatever function you have specified as the value of $SyntaxHandler. It supplies two arguments: the input string, and the character position at which the syntax error was detected. With $SyntaxHandler you can, for example, generate an analysis of the syntax error, or call an editor. If your function returns a string, then Mathematica will use this string as a new input string.

This specifies what Mathematica should do when it gets a syntax error.

In[9]:= $SyntaxHandler =

(Print[StringForm["Error at char `1` in `2`",

#2, #1]]; $Failed)&

Out[9]=

This input generates a syntax error.

In[10]:= 3 +/+ 5

Syntax::sntxf: "3 +" cannot be followed by "/+ 5".

Once Mathematica has successfully read an input expression, it then evaluates this expression. Before doing the evaluation, Mathematica applies any function you have specified as the value of $Pre, and after the evaluation, it applies any function specified as the value of $Post. Note that unless the $Pre function holds its arguments unevaluated, the function will have exactly the same effect as $Post.

$Post allows you to specify arbitrary "post processing" to be done on results obtained from Mathematica. Thus, for example, to make Mathematica get a numerical approximation to every result it generates, all you need do is to set $Post = N.

This tells Mathematica to apply N to every result it generates.

In[11]:= $Post = N

Out[10]=

Now Mathematica gets a numerical approximation to anything you type in.

In[12]:= Sqrt[7]

Out[11]=

This removes the post-processing function you specified.

In[13]:= $Post =.

As soon as Mathematica has generated a result, and applied any $Post function you have specified, it takes the result, and assigns it as the value of Out[$Line]. The next step is for Mathematica to print the result. However, before doing this, it applies any function you have specified as the value of $PrePrint.

This tells Mathematica to shorten all output to two lines.

In[14]:= $PrePrint = Short[#, 2]& ;

Only a two-line version of the output is now shown.

In[15]:= Expand[(x + y)^40]

Out[14]=

This removes the value you assigned to $PrePrint.

In[16]:= $PrePrint =.

There are various kinds of output generated in a typical Mathematica session. In general, each kind of output is sent to a definite output channel, as discussed in Section 2.11.3. Associated with each output channel, there is a global variable which gives a list of the output streams to be included in that output channel.

Output channels in a standard Mathematica session.

By modifying the list of streams in a given output channel, you can redirect or copy particular kinds of Mathematica output. Thus, for example, by opening an output stream to a file, and including that stream in the $Echo list, you can get each piece of input you give to Mathematica saved in a file.

Open streams in a Mathematica session.

The function Streams shows you all the input, output and other streams that are open at a particular point in a Mathematica session. The variable $Input gives the name of the current stream from which Mathematica input is being taken at a particular point. $Input is reset, for example, during the execution of a Get command.

Parameters for messages.

There are various global parameters which determine the form of messages generated by Mathematica.

As discussed in Section 2.8.21, typical messages include a sequence of expressions which are combined with the text of the message through StringForm. $MessagePrePrint gives a function to be applied to the expressions before they are printed. The default value of $MessagePrePrint is Short.

As discussed in Section 2.8.22, Mathematica allows you to specify the language in which you want messages to be produced. In a particular Mathematica session, you can assign a list of language names as the value of $Language.

Terminating Mathematica sessions.

Mathematica will continue in its main loop until you explicitly tell it to exit. Most Mathematica interfaces provide special ways to do this. Nevertheless, you can always do it by explicitly calling Exit or Quit.

Mathematica allows you to give a value to the global variable $Epilog to specify operations to perform just before Mathematica actually exits. In this way, you can for example make Mathematica always save certain objects before exiting.

A global variable that determines the treatment of end-of-file characters.

As discussed in Section 2.7.5, Mathematica usually does not treat special characters in a special way. There is one potential exception, however. With the default setting $IgnoreEOF = False, Mathematica recognizes end-of-file characters. If Mathematica receives an end-of-file character as the only thing on a particular input line in a standard interactive Mathematica session, then it will exit the session.

Exactly how you enter an end-of-file character depends on the computer system you are using. Under Unix, for example, you typically press Control-D.

Note that if you use Mathematica in a "batch mode", with all its input coming from a file, then it will automatically exit when it reaches the end of the file, regardless of the value of $IgnoreEOF.