Standard ML

We shall be using Standard ML of New Jersey in our course work. It is one of the best-known and most widely-used Standard ML systems.

SML Tutorials and Reference Material

An extensive online archive of Standard ML tutotials and reference documents is available. Feel free to consult it if you have questions not covered in the text or in class lectures. A good reference is Introduction to Standard ML. Class notes prepared by Professor Larus are also available.

Starting and Exiting SML/NJ

SML/NJ is an SML interpreter. You can type in code and interactively see its output as it is run. To fire up the interpreter, type sml (you must be on a SPARC/Solaris). You should see something like this come up :

% sml
Standard ML of New Jersey, Version 110.0.6, October 31, 1999
-


The '-' is the SML prompt. Now you can type in ML code and the interpreter immediately evaluates it and produces an output. (The italics indicate user input; the rest is produced by SML. Comments are anything within (* and *)). You need a terminating semicolon at the end of each definition you type. Control-C interrupts the interpreter and returns you to the top-level prompt. 

- val my_name = "fischer"; (* define a string value*)
val my_name = "fischer" : string
- fun get_initial (name) = hd(explode(name)); (* define a function to get my initial*)
val get_initial = fn : string -> char
- get_initial (my_name);
val it = #"f" : char
- get_initial("zorro");
val it = #"z" : char
- get_initial(["curly","larry","moe"]);
std_in:6.1-6.13 Error: operator and operand don't agree (tycon mismatch)
  operator domain: string
  operand: string list
  in expression:
    get_initial ("curly" :: "larry" :: "moe" :: nil)
-


To exit, either type Control-D at the prompt.

Reading an SML program from a File

The interpretive mode is great for testing out functions one by one, but it's not effective for typing in lots of functions or making changes to them. SML provides you with a function for reading in an SML program from a file. The function is use (its type is string -> unit). Suppose that you had typed in the following SML code into a file called hello.sml :

fun hello () = print ("Hello, world\n" );

Now, we fire up SML and read in this file :

% sml
Standard ML of New Jersey, Version 110.0.3, January 30, 1998
- use "hello.sml";
[opening hello.sml]
val hello = fn : unit -> unit
val it = () : unit
- hello;
val it = fn : unit -> unit
- hello ();
Hello, world
val it = () : unit
-


Saving the Current Environment

During an interactive session with SML, you may have definitions in the current environment that you want to save for future sessions. SML provides you with a function to save the current environment : SMLofNJ.exportML (whose type is string -> bool). Entering

SMLofNJ.exportML("saved_env");

will create a file called saved_env which is basically a dump of the entire memory contents (including the code for the interpreter and the environment). Later you can restart SML, naming the file on the shell command line to resume execution in the same environment in which the exportML was done. That is, you'd enter
% sml saved_env
to restart SML. Beware -- the saved file can be HUGE (at least several megabytes).

Debugging in SML

There is no debugger currently installed with SML/NJ. (A special version of the SML interpreter that can then interact with GNU Emacs exists). SML is a strongly typed language and its type inference system is powerful enough to capture a wide variety of errors and ambiguities. Furthermore, there is run-time checking so there's no way you can crash the system (except for an infinite loop). SML does provide you with references which are pointers (and hence dangerous), but their use is deprecated as not in keeping with the functional style.

The Edit, Compile, Debug Cycle in SML

A convenient way to develop ML programs is to open two windows, one for an editor and one to run sml. You edit ML functions in the edit window, using your favorite text editor. To test ML functions, go to the sml window. Enter the command use "filename.sml"; to load and compile the file you've just edited. You can interactively test each function as you add it. When changes, corrections, or extensions are needed, you return to the edit window.

A good approach is to write and test functions one by one. That way you can focus on a limited amount of code. If necessary, print statements can be added to view selected values during execution.

The Standard Basis Library

Standard ML provides a rich library of types and operations in the Standard Basis Library. Definitions are grouped into "structures" which may be accessed using the ML statement open structure_name (for example open List; will make available a wide variety of list operations). Individual definitions may also be accessed in structure_name.identifier form (for example List.partition).

Running SML at Home

If you want to run SML on a home computer, you have at least two options: