Hans
Rischel
Department of Information Technology
Technical University of Denmark
These notes explain how to use SML in the G data bar at DTU. The SML-mode of the Emacs editor yields an integrated environment for editing, compiling and executing SML programs.
Log-in the G data bar.
Create a terminal window on a server (e.g.~Newton) via the menu obtained by pressing the middle mouse
key.
The first time you log-in you have to type the following two
commands in the terminal window (at following log-in's skip to point d):
cp
~v0283/gbar/.emacs .
mkdir 02153
The first command copies the configuration file ``.emacs´´ to your files while the second creates a
directory ``02153´´ to be used for your files in this course.
Type the following two commands in the terminal window:
export
PRINTER=printerName
cd 02153
where printerName
is the name of the printer you want to use
during the data bar session. The command ``cd 02153
´´ changes the current directory to
02100
.
Type the following command to start editing your program file:
emacs
name.sml &
where name
is the name of the file to contain your program,
e.g. you should type:
``emacs gcd.sml
´´ to edit the program named
gcd
. The Emacs editing window will appear and editing
can go on as described in the next section.
If the file name.sml
already exists in the current directory on the disk, it is loaded into Emacs. Otherwise a new empty
file is created. The terminal window will still be active (because of the ampersand
&) so commands can be entered in the terminal
window while Emacs is running. The suffix ``sml´´ is the file type of SML programs. It puts the Emacs window in SML-mode.
When you have finished the editing session you should remove the terminal window and log-out the G data bar.
With Emacs, you can edit your SML program in one window and run an SML session in another window. Emacs provides simple commands for SML program layout, for loading the program into the SML session, and for repeating entries in the ML session. Emacs is a powerful editor once you have learned to use it, but the commands may appear strange at first. The menus, however, make use of Emacs easier. An overview of Emacs commands is found in the GNU Emacs Reference Card. The composed keys
M-x
(``meta-x´´) and C-x (``control-x´´)
for arbitrary key x are made as follows:
M-x | means: | hold down the ``Alt'' or ``Alt function'' key and press the key x |
C-x | means: | hold down the ``Ctrl'' key and press the key x. |
This a brief step-by-step guide to editing programs.
C-x C-s
to save the program on the disk. C-c C-b
in the SML program buffer. Type new-line twice to answer the questions
ML command: mosml
and Any
args:
. This creates an SML session, and loads the program into the SML session. C-c C-s
to see the output from the SML session. The SML session will be in a new window named
mosml, as in the figure above.
C-c C-b
again will load the corrected program into the existing SML session, and SML prints the
types of the functions and values defined (provided the SML compiler finds no more
errors).export PRINTER
´´ command
(cf. point d in the section ``Starting and terminating the SML session´´).
switch to buffer in other window (default *scratch*)
output
(followed by
new-line). An new window named ``output´´ appears. Select this window and place the output here by the command
C-y (``yank''). The output can now be printed via the Tools menu of the editing
window. Afterwards you may make the output window invisible by placing the cursor in the editing window and
type C-x 1 (the output window may be restored using C-x 4 b).
C-c C-d
in the SML session window. C-x C-c
or press the close button in the window. If you have not saved your program file, it will ask
``Save file ...?
´´, to which you should answer
``y´´.
Key kombinations like
C-c C-b
for sending a program to the SML compiler
are specific for the sml mode of Emacs which is activated by the
extension ``.sml´´ of the file name. An overview of these special
functions can be found in the Sml menu (click on Sml in the Emacs
window).
If you get into a mess
with Emacs, try C-g
once
or twice to interrupt it. If this does not help, type C-x C-c
(or press the close button in
the window) to terminate Emacs, and answer any questions it
asks. If this does not help, then you could use ``brute
force'' and select kill in the system menu of the window.
This kills Emacs but the program file may get garbled.
The table below lists a selection of functions of Moscow ML which are not part of the Standard ML Library. Further information can be found in the Moscow ML Owner's Manual which can be downloaded via the Moscow ML home page.
id | type | value | exception |
compile | string -> unit | compile unit (U.sml or U.sig) | Fail |
load | string -> unit | Load unit and any units it needs | Fail |
loadOne | string -> unit | Load unit only | Fail |
printVal | 'a -> -a | Print value on stdOut | |
printDepth | int ref | Limit printed data depth | |
printLength | int ref | Limit printed list and vector lenth | |
quit | unit -> unit | Quit the interactive SML system | |
Process.system | string -> status | Execute operating system command | |
use | string -> unit | Include file in program | |
verbose | bool ref | Permit feedback from compile |
The function printVal may be convenient when debugging programs. It gives the possibility for getting debug output anywhere in the evaluation of an expression as any (sub)expression e can be replaced by (printVal(e)).
The entities printDepth, printLength, and verbose are set by an assignment e.g. the assignment:
printLength := 100;
ensures that the response from SML will print the first 100 elements of a list.
Evaluating the expression:
Process.system str
executes the command str exactly like typing the string str followed by new-line in a terminal window. The evaluation of the expression terminates upon termination of the execution of the command.