The Main Loop

In any interactive session, the Wolfram System 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", the Wolfram System 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 the Wolfram System through a special front end, your front end may set up its own main loop, and what is said here may not apply.

Input and output expressions.

In a standard interactive session, there is a sequence of input and output lines. The Wolfram System stores the values of the expressions on these lines in (RefLink[In, paclet:ref/In])[n] and (RefLink[Out, paclet:ref/Out])[n].

As indicated by the usual (RefLink[In, paclet:ref/In])[n]:= prompt, the input expressions are stored with delayed assignments. This means that whenever you ask for (RefLink[In, paclet:ref/In])[n], the input expression will always be reevaluated in your current environment.

This assigns a value to x:

x=7

Now the value for x is used:

x-x^2+5x-1

This removes the value assigned to x:

x=.

This is reevaluated in your current environment, where there is no value assigned to x:

In[2]

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

InString[2]//InputForm

Specifying the length of session history to keep.

The Wolfram System 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 (RefLink[In, paclet:ref/In]) and (RefLink[Out, paclet:ref/Out]), using (RefLink[Unprotect, paclet:ref/Unprotect])[(RefLink[In, paclet:ref/In]),(RefLink[Out, paclet:ref/Out])], followed by (RefLink[Clear, paclet:ref/Clear])[(RefLink[In, paclet:ref/In]),(RefLink[Out, paclet:ref/Out])]. You can also tell the Wolfram System to keep only a limited number of lines of history by setting the global variable (RefLink[$HistoryLength, paclet:ref/$HistoryLength]).

Note that at any point in a session, you can reset the line number counter (RefLink[$Line, paclet:ref/$Line]), so that, for example, new lines are numbered so as to overwrite previous ones.

Global functions used in the main loop.

The Wolfram System 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 (RefLink[$Pre, paclet:ref/$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 the Wolfram System. You can do this by assigning a function as the value of the global variable (RefLink[$PreRead, paclet:ref/$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 the Wolfram System to replace listHead by {…} in every input string:

$PreRead=(ReplaceAll[#,"listHead":>"List"]&)

You can now enter lists as listHead expressions:

listHead[a,b,c]

You can remove the value for (RefLink[$PreRead, paclet:ref/$PreRead]) like this, at least so long as your definition for (RefLink[$PreRead, paclet:ref/$PreRead]) does not modify this very input string:

$PreRead=.

Once the Wolfram System has successfully read an input expression, it then evaluates this expression. Before doing the evaluation, the Wolfram System applies any function you have specified as the value of (RefLink[$Pre, paclet:ref/$Pre]), and after the evaluation, it applies any function specified as the value of (RefLink[$Post, paclet:ref/$Post]). Note that unless the (RefLink[$Pre, paclet:ref/$Pre]) function holds its arguments unevaluated, the function will have exactly the same effect as (RefLink[$Post, paclet:ref/$Post]).

(RefLink[$Post, paclet:ref/$Post]) allows you to specify arbitrary "postprocessing" to be done on results obtained from the Wolfram System. Thus, for example, to make the Wolfram System get a numerical approximation to every result it generates, all you need do is to set (RefLink[$Post, paclet:ref/$Post])=(RefLink[N, paclet:ref/N]).

This tells the Wolfram System to apply (RefLink[N, paclet:ref/N]) to every result it generates:

$Post=N

Now the Wolfram System gets a numerical approximation to anything you type in:

Sqrt[7]

This removes the postprocessing function you specified:

$Post=.

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

This tells the Wolfram System to shorten all output to two lines:

$PrePrint=Short[#,2]&;

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

Expand[(x+y)^40]

This removes the value you assigned to (RefLink[$PrePrint, paclet:ref/$PrePrint]):

$PrePrint=.

There are various kinds of output generated in a typical Wolfram System session. In general, each kind of output is sent to a definite output channel, as discussed in "Streams and Low-Level Input and Output". Associated with each output channel there is a global variable that gives a list of the output streams to be included in that output channel.

Output channels in a standard Wolfram System session.

By modifying the list of streams in a given output channel, you can redirect or copy particular kinds of Wolfram System output. Thus, for example, by opening an output stream to a file, and including that stream in the (RefLink[$Echo, paclet:ref/$Echo]) list, you can get each piece of input you give to the Wolfram System saved in a file.

Open streams in a Wolfram System session.

The function (RefLink[Streams, paclet:ref/Streams]) shows you all the input, output, and other streams that are open at a particular point in a Wolfram System session. The variable (RefLink[$Input, paclet:ref/$Input]) gives the name of the current stream from which the Wolfram System input is being taken at a particular point. (RefLink[$Input, paclet:ref/$Input]) is reset, for example, during the execution of a (RefLink[Get, paclet:ref/Get]) command. (RefLink[Get, paclet:ref/Get]) also sets (RefLink[$InputFileName, paclet:ref/$InputFileName]) to the full absolute path of the file from which input is read.

Parameters for messages.

There are various global parameters that determine the form of messages generated by the Wolfram System.

As discussed in "Messages", typical messages include a sequence of expressions that are combined with the text of the message through (RefLink[StringForm, paclet:ref/StringForm]). (RefLink[$MessagePrePrint, paclet:ref/$MessagePrePrint]) gives a function to be applied to the expressions before they are printed. The default for (RefLink[$MessagePrePrint, paclet:ref/$MessagePrePrint]) uses (RefLink[Short, paclet:ref/Short]) for text formatting and a combination of (RefLink[Short, paclet:ref/Short]) and (RefLink[Shallow, paclet:ref/Shallow]) for typesetting.

As discussed in "International Messages", the Wolfram System allows you to specify the language in which you want messages to be produced. In a particular Wolfram System session, you can assign a list of language names as the value of (RefLink[$Language, paclet:ref/$Language]).

Terminating Wolfram System sessions.

The Wolfram System will continue in its main loop until you explicitly tell it to exit. Most Wolfram System interfaces provide special ways to do this. Nevertheless, you can always do it by explicitly calling (RefLink[Exit, paclet:ref/Exit]) or (RefLink[Quit, paclet:ref/Quit]).

The Wolfram System allows you to give a value to the global variable (RefLink[$Epilog, paclet:ref/$Epilog]) to specify operations to perform just before the Wolfram System actually exits. In this way, you can, for example, make the Wolfram System always save certain objects before exiting.

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

As discussed in "Special Characters: Strings and Characters", the Wolfram System usually does not treat special characters in a special way. There is one potential exception, however. With the default setting (RefLink[$IgnoreEOF, paclet:ref/$IgnoreEOF])=(RefLink[False, paclet:ref/False]), the Wolfram System recognizes end-of-file characters. If the Wolfram System receives an end-of-file character as the only thing on a particular input line in a standard interactive Wolfram System 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 Ctrl+D.

Note that if you use the Wolfram System 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 (RefLink[$IgnoreEOF, paclet:ref/$IgnoreEOF]).