Using SML in the G data bar

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.

Starting and terminating the SML session

  1. Log-in the G data bar.

  2. Create a terminal window on a server (e.g.~Newton) via the menu obtained by pressing the middle mouse key. 

  3. 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.

  4. 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.

  5. 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. 

  6. When you have finished the editing session you should remove the terminal window and log-out the G data bar. 

Editing SML programs

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. 

  1. Start the editing of a program file as described in (e) above.
  2. Type and edit your program in the SML program
    buffer. Editing commands are described in the reference card.
  3. Type C-x C-s to save the program on the disk. 
  4. To compile and load the program into an SML session, type 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.
  5. Type 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.
  6. Resize and move the windows to separate the two windows on the screen.
  7. If the program has syntax errors or type errors, the SML session prints messages about them. Edit the program to correct the errors. When you have finished correcting the errors, go back to step 3 above. Typing 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).
  8. Select the SML session window by moving the cursor to the window and click the left mouse button. Enter some expressions to test  your program. To go back to the SML program window, move the cursor to the window and click the left mouse button. 
  9. To print out the program select Postscript print buffer in the Tools menu (move the cursor
    to Tools at the top of the window, press down the left mouse button, keep the button pressed down, move the cursor to Postscript print buffer, and release the button). You may instead use an a2ps command in the terminal window to print the program. The output will be printed on the printer selected by the ``export PRINTER´´ command  (cf. point d in the section ``Starting and terminating the SML session´´).

    To print output from the SML session window you should highlight the region to print (put the cursor in the upper left corner of the region, press down the left mouse button, keep the button pressed down and move the cursor to the lower right corner of the region, and release the button). Next you select the Emacs editing window where you create a new Emacs buffer by the command C-x 4 b. The following question will appear in the command line:

            switch to buffer in other window (default *scratch*)

    to which you answer 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).

  10. When you have finished editing and testing, terminate the SML session by typing C-c C-d in the SML session window.
  11. To terminate Emacs, type 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´´.

  12. You may now log-out from the G data bar, or you may start a new SML session by entering another ``emacs name.sml &´´ command in the terminal window.

 

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.

Moscow ML system functions

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.