Standard ML of New Jersey
We are using Standard ML of New Jersey in our course work. It is one of
the best known and most widely used Standard ML systems.
Standard ML of NJ is installed in the Cmps 112 course locker on CATS.
The full path for the system executable is
/afs/cats.ucsc.edu/courses/cmps112-hb/usr/sml/bin/sml
SML Reference Material
An extensive online archive of Standard ML tutorials and reference documents
is available on the Standard ML of New Jersey homepage.. A link to this
page and a selected set of SML documents are available via the Cmps 112
webpage.
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. When you fire up the interpreter 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 = "brown"; (* define a string value*)
val my_name = "brown" : 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 = #"b" : 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
-
Debugging in SML
There is no debugger currently installed with SML/NJ. 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). These libraries are already
installed in the course locker. There is a description of the various library
components in the SML book, Notes on Programming Standard ML of New
Jersey, and a link to the Basis Library Manual is on the course website.
Note some of the more basic library components are automatically included
in SML of NJ. You may want to add the Math components, i.e., do open
Math; so as to have functions such as sin and cos to experiment with.
Running SML at Home
If you want to run SML on a home computer, you have at least two options:
-
A self installing version of SML of New Jersey for Windows can be downloaded
from the course webpage.
-
Since the SML of New Jersey system comes up in a terminal window, you can
use it remotely without having to fight remote X-Windows. You do need to
have an SSH client on your home machine to access CATS.