Programming in SML

This page provides a few useful hints on running the SML/NJ compiler and programming in Standard ML (SML).

Finding Documentation

The main SML/NJ web page is found at the URL

http://www.smlnj.org
and it is mirrored at
http://cm.bell-labs.com/cm/cs/what/smlnj

The main documentation page is

http://www.smlnj.org/literature.html
and this lists a number of online resources, such as downloadable books and web-based tutorials. I particularly recommend Riccardo Pucella's Notes on Programming SML/NJ and Bob Harper's book "Programming in Standard ML" ( online pdf version or offline pdf version for printing). The Basis Library documentation contains specifications of the standard library functions (for lists, strings, I/O, etc.).

Note: There was a major revision of Standard ML in 1997. Some of the online tutorials are out-of-date and describe the old version, called SML 90, while others have been updated to SML 97. If you use an old tutorial, some of the library functions will be different and some language features will have changed. If you still want to use an SML 90 tutorial, you can refer to this top-level environment comparison that relates old and new versions of basic functions in the top-level environment.

Here are some useful manuals available for download:


Accessing or installing SML/NJ

The following reiterates some basic information that is available in the SML/NJ FAQ or in Pucella's Notes. SML/NJ is installed on the department Linux systems and Solaris servers (e.g. gargoyle). To run the SML/NJ interactive system in a Unix or, type

   % sml
(where "%" represents the shell prompt). To exit, type the end-of-file character (typically ^D in Unix/Linux, or ^Z in Windows).

Two versions of SML/NJ are available on department machines, or for installation on your personal machine. The default version is 110.0.7 (found at /usr/local/bin/sml, which is a link to /opt/smlnj/default/bin/sml, while /opt/smlnj/default is a link to /opt/smlnj/smlnj-110.0.7). The more recent development version is 110.37, which is required if you want to run sml under Mac OS X. For the programs used in this course there is little functional difference between the two versions, except for CM, the compilation manager, which has a somewhat different interface in 110.37.

Using CM, the SML/NJ Compilation Manager

The CM Manual is available in two versions, one for SML/NJ 110.0.7 and a revised version for SML/NJ 110.37.

We use CM in a fairly basic way in this course. A program consists of a set of source files, called a group, and may import some standard libraries. A group is specified in a CM description file, typically named "sources.cm". Here is an example (simplebool/sources.cm):

  Group is

  #if defined (NEW_CM)
    $/basis.cm
    $/ml-yacc-lib.cm
    $/pp-lib.cm
  #else
    pp-lib.cm
    ml-yacc-lib.cm
  #endif

    simplebool.grm
    simplebool.lex
    error.sml
    syntax.sml
    pputil.sml  
    core.sml
    main.sml

The main body of the file lists the source files of the project. The ifdef section specifies libraries that are to be loaded, in the 110.37 CM format and the 110.0.7 CM format, respectively (NEW_CM is defined in the 110.37 version of CM). The Basis library is loaded by default in the 110.0.7 version of CM, but has to be loaded explicitly in the 110.37 version.

For the lexer and grammar specification files simplebool.lex and simplebool.grm, CM will automatically invoke ml-lex and ml-yacc to generate the source code for the lexer and parser, and will then compile and load that source code.

In sml 110.0.7 (the default sml), you can invoke CM with the command:

  CM.make();
assuming that the CM description file in the directory has the default name "sources.cm". If you want to use a different name for the description file, say "simplebool.cm", then it can be passed as a parameter to the function CM.make'.
  CM.make' "simplebool.cm";

In sml 110.37, the name of the description file always has to be passed explicitly:

  CM.make "sources.cm";

Dave MacQueen
Last modified: Tue Jan 29 17:57:13 CST 2002