This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Global Aspects of Mathematica Sessions /

2.14.2 Dialogs

Within a standard interactive session, you can create "subsessions" or dialogs using the Mathematica command Dialog. Dialogs are often useful if you want to interact with Mathematica while it is in the middle of doing a calculation. As mentioned in Section 2.6.11, TraceDialog for example automatically calls Dialog at specified points in the evaluation of a particular expression. In addition, if you interrupt Mathematica during a computation, you can typically "inspect" its state using a dialog.

Initiating and returning from dialogs.

This initiates a dialog.

In[1]:= Dialog[ ]

You can do computations in a dialog just as you would in any Mathematica session.

In[2]:= 2^41


You can use Return to exit from a dialog.

In[3]:= Return[ ]


When you exit a dialog, you can return a value for the dialog using Return[expr]. If you do not want to return a value, and you have set $IgnoreEOF = False, then you can also exit a dialog simply by giving an end-of-file character, at least on systems with text-based interfaces.

To evaluate this expression, Mathematica initiates a dialog.

In[2]:= 1 + Dialog[ ]^2

The value a + b returned from the dialog is now inserted in the original expression.

In[3]:= Return[a + b]


In starting a dialog, you will often find it useful to have some "initial expression". If you use Dialog[expr], then Mathematica will start a dialog, using expr as the initial expression, accessible for example as the value of %.

This first starts a dialog with initial expression a^2.

In[3]:= Map[Dialog, {a^2, b + c}]


% is the initial expression in the dialog.

In[5]:= %^2 + 1


This returns a value from the first dialog, and starts the second dialog, with initial expression b + c.

In[6]:= Return[%]


This returns a value from the second dialog. The final result is the original expression, with values from the two dialogs inserted.

In[5]:= Return[444]


Dialog effectively works by running a subsidiary version of the standard Mathematica main loop. Each dialog you start effectively "inherits" various values from the overall main loop. Some of the values are, however, local to the dialog, so their original values are restored when you exit the dialog.

Thus, for example, dialogs inherit the current line number $Line when they start. This means that the lines in a dialog have numbers that follow the sequence used in the main loop. Nevertheless, the value of $Line is local to the dialog. As a result, when you exit the dialog, the value of $Line reverts to what it was in the main loop.

If you start a dialog on line 10 of your Mathematica session, then the first line of the dialog will be labeled In[11]. Successive lines of the dialog will be labeled In[12], In[13] and so on. Then, when you exit the dialog, the next line in your main loop will be labeled In[11]. At this point, you can still refer to results generated within the dialog as Out[11], Out[12] and so on. These results will be overwritten, however, when you reach lines In[12], In[13], and so on in the main loop.

In a standard Mathematica session, you can tell whether you are in a dialog by seeing whether your input and output lines are indented. If you call a dialog from within a dialog, you will get two levels of indentation. In general, the indentation you get inside d nested dialogs is determined by the output form of the object DialogIndent[d]. By defining the format for this object, you can specify how dialogs should be indicated in your Mathematica session.

Options for Dialog.

Whatever setting you give for DialogSymbols, Dialog will always treat the values of $Line, $Epilog and $MessageList as local. Note that if you give a value for $Epilog, it will automatically be evaluated when you exit the dialog.

When you call Dialog, its first step is to localize the values of variables. Then it evaluates any expression you have set for the option DialogProlog. If you have given an explicit argument to the Dialog function, this is then evaluated next. Finally, the actual dialog is started.

When you exit the dialog, you can explicitly specify the return value using Return[expr]. If you do not do this, the return value will be taken to be the last value generated in the dialog.