Programming in SML

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

Finding General Documentation

The main SML/NJ web page can be found at the URLs

http://www.smlnj.org
http://smlnj.sourceforge.net
and it is mirrored at
http://smlnj.cs.uchicago.edu

The main documentation page is

http://www.smlnj.org/doc/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).

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, now 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.

Libraries

The Basis Library documentation contains specifications of the standard library functions (for lists, strings, I/O, etc.). It is a good idea to spend time browsing through this documentation, to avoid reinventing old wheels.

The Basis Library covers the core, built-in resources. Another useful library is the SML/NJ Library, which includes a number of standard data structures, as well as support for regular expressions, pretty printing, working with HTML, reactive programming, and using sockets.

The ML-Lex and ML-Yacc tools will play an important role in the course, and their manuals are 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. abyss, gargoyle). The default version of SML/NJ is 110.42. To run the SML/NJ interactive system under Unix or Windows, type

   % sml
(where "%" represents the shell prompt). This will work as long as /usr/local/bin is in your shell PATH.

To exit from the interactive system, type the end-of-file character (typically "^D" in Unix/Linux, or "^Z" in Windows) at the top-level sml prompt ("-"). If for some reason you can't type the end-of-file character, you can exit by evaluating the following expression at the top-level prompt (or even from within a program).

   - OS.Process.exit OS.Process.success;

Two versions of SML/NJ are available on CS Department machines. The default version should be 110.42 (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.42). Version 110.42 runs under Mac OS X, but does not currently support Windows. If you want to install and run SML/NJ under Windows, use the smlnj.exe for version 110.0.7. For the programs used in this course there is little functional difference between the versions 110.0.7 and 110.42, except for CM, the compilation manager, which has a somewhat different interface in 110.42. Either version is also available for download if you want to install SML/NJ on your personal system.

Loading Files, and Using CM, the SML/NJ Compilation Manager

You can type definitions and expressions into the interactive system, but for any serious programming, you'll want to use your favorite editor to create a source file and then load that into the sml system. Source files are typically identified by using the ".sml" extension. The simplest way to load a source file is by calling the function

  use : string -> unit
at top level, giving a file name as argument, as in:
  - use "foo.sml";
The compiler will print out a series of responses indicating the results of evaluating the definitions and expressions in the specified file, as though they had been typed directly at the top-level prompt. The "use" function suffices for quick-and-dirty execution of code, but when multiple files and libraries are involved it is better to use the Compilation Manager, CM. The CM Manual is available in two versions, one for SML/NJ 110.0.7 and a revised version for SML/NJ 110.42.

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:

  Group is

  #if defined (NEW_CM)
    $/basis.cm
    $/smlnj-lib.cm
    $/ml-yacc-lib.cm
    $/pp-lib.cm
  #else
    smlnj-lib.cm
    ml-yacc-lib.cm
    pp-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 "#if defined" section specifies libraries that are to be loaded, in the 110.42 CM format and the 110.0.7 CM format, respectively (NEW_CM is defined in the 110.42 version of CM). Note that the Basis library is loaded by default in the 110.0.7 version of CM, but has to be loaded explicitly in the 110.42 version. The source files can include both SML source files (e.g. "error.sml") and files that are to be processed by various standard tools. In this example "simplebool.grm" and "simplebool.lex" are grammer and lexer specifications that will automatically be processed by the appropriate tools, ml-yacc and ml-lex, resulting in SML source files which will also be compiled.

In sml 110.42 (the default sml), you typically invoke CM with the command:

  CM.make "sources.cm";
assuming that the CM description file in the directory has the usual name "sources.cm". If you want to use a different name for the description file, say "simplebool.cm", then just supply this name as the argument to CM.make:
  CM.make "simplebool.cm";

In sml 110.0.7, CM.make has argument type unit, and by default the file named "sources.cm" is loaded:

  CM.make ();
In this version, if you want to use a different name for the description file then call CM.make' instead of CM.make with the description file name as argument, e.g.:
  CM.make' "simplebool.cm";

Emacs sml-mode

For those who use the emacs (or xemacs) editor, there is a very useful sml-mode for emacs:
sml-mode.tar.gz -- the sml-mode tarball
sml-mode manual
If you have an emacs library directory (e.g. ~/lib/emacs), the simple way to install sml-mode is to unpack sml-mode.tar.gz into that directory and add it to your emacs load-path:
  (add-to-list 'load-path "~/lib/emacs/sml-mode")
See the manual and the documentation and installation files in the sml-mode directory for further information. (Warning: the manual is not fully up-to-date.)

Dave MacQueen
Last modified: Tue Apr 8 15:08:20 CDT 2003