Setting up SML/NJ and Emacs for CSE 341

Hal Perkins, Spring 2011

This is meant to be a simple set of instructions to get up and running with SML for CSE341 projects. There are certainly many ways to install and customize the software, and you should feel free to do as much of that as you want. The goal here is to get a working SML command-line prompt and SML Emacs mode running with a minimum of fuss.

These instructions are geared mostly towards people running Windows 7 or OS X 10.6 on their personal machines and have been verified as of March 2011. Everything should also work on Windows XP and probably Vista, but may need some adjusting. The OS X instructions should also work on OS X 10.5 but have not been rechecked there. There are also a few notes about setting up under Linux, but this should be quite straightforward.

SMLNJ and Emacs are available on all of the CSE undergrad lab machines. You probably will need to do some work to get SML Mode for emacs installed - see the instructions at the end.

SML/NJ

Current versions are available from http://www.smlnj.org/. Click on the most recent "working version" link, which is 110.72 as of March 2011.

Windows 7

Download the Windows msi installer and run it. After the installer finishes you should have a Start Menu entry for SML of New Jersey. When you run this, a window should appear with a Standard ML of New Jersey banner and a prompt. Verify that it is working by entering 1+1; or some other expresson. SML/NJ should evaluate the expression and print the result and its type. Type c-z (control-z) to exit.

OS X 10.6

OS X is a Unix system underneath, so you can always compile the SML/NJ distribution from scratch if you have the developer tools installed (see Linux, below). But it's easier to download and run the installer in the smlnj-x86-110.72.dmg package (use the ppc one if you're using a pre-Intel mac). That will install everything in /usr/local/smlnj-110.72/. After running the installer you need to add the smlnj bin directory to your search path. If you know about bash shell variables, you can edit your .profile or .bash_profile file (whichever one you use) to add /usr/local/smlnj-110.72/bin to your $PATH. That should make the sml command available the next time you open a terminal application window.

There is another little-documented trick for adding directories to the default system path in OS X that allows the sml command to be found by other programs besides the bash (terminal) shell. It's also a little easier to set up, particularly if you don't know about $PATH and other shell variables. In OS X there is a directory /etc/paths.d/ whose contents are read to construct the system default search path. To use this for SML/NJ do the following:

  1. Create a plain text file somewhere that contains a single line with the name of the SML/NJ bin directory. For the current verison of SML/NJ the file contents should be the following single line:

    /usr/local/smlnj-110.72/bin

    A good name for the file is smlnj-110.72. Create this file anywhere you want initially.

  2. In a terminal window, copy the file from step 1 to /etc/paths.d. Since this is a protected directory you must do this with the sudo command, which will ask you to enter your password and then allow you to copy the file using appropriate permissions. Assuming that the file is named smlnj-110.72 and is in the current directory, the following terminal command will do the job:

    sudo cp smlnj-110.72 /etc/paths.d

Now open a new terminal window and enter the command sml. You should get a SML/NJ prompt. Try entering 1+1; or some other expression and SML/NJ should display the value and its type. Enter c-d (control-d) to exit.

Linux

If you're running Linux and have the usual set of developer tools installed (gcc, make, etc.), follow the instructions to download the starter file from the SML/NJ web site and run make to build it. The default configure file options are fine. Your Linux distribution might also include smlnj as an available package, saving the trouble of building it from scratch.

Emacs

There are two parts to getting emacs working with SML. Install the emacs editor itself, then install SML mode to provide syntax highlighting and indenting for sml files. This part is about getting emacs installed if it is not already present.

Windows 7

Point your browser to http://ftp.gnu.org/gnu/emacs/windows/. Download the full bin archive for the most recent version of emacs, which is emacs-23.3-bin-i386.zip currently. In principle all that is needed is to unpack this file and you should have a working emacs. Unfortunately the built-in Windows 7 support for zip archive files doesn't seem to unpack the emacs archive correctly. So, ...

Download and install the free archive manager 7-zip (http://www.7-zip.org/). The 32-bit installer should work with any Windows 7 system. Once this is installed, right-click on the emacs-23.3-bin-i386.zip archive you downloaded in the previous step and select 7-Zip > Extract Here from the popup menu. You can leave the resulting emacs-23.3 folder where it is or drag it anywhere convenient on your disk.

This next step is optional but very useful. Once you've moved the emacs-23.3 folder to where you want it, look inside to find bin\addpm.exe (the .exe extension might not be visible in the folder window depending on how you have Windows configured). Double click on this file to run it one time and it should add a Gnu-Emacs folder to your Start menu and do some other setup operations. Open the Start menu and select Gnu-Emacs > Emacs to launch emacs.

OS X

OS X comes with a version of emacs that runs inside a terminal window. Open a terminal window and enter an emacs command. If everthing is installed normally you should see a fairly bare-bones emacs display in the window.

There are also versions of Emacs that run as regular OS X applications. These definitely look nicer and they run independently of the terminal program.

A generic emacs version compiled for OS X is available at http://emacsformacosx.com/. This one is created by simply compiling the standard GNU Emacs sources, which contain support for OS X (and, if you're feeling adventerous, you could do this yourself). Download it and put it in the Applications folder.

Aquamacs (http://aquamacs.org/) is a version of Emacs customized for OS X. In particular, it supports the standard OS X keyboard shortcuts for common operations like copy and paste in addition to the more arcane (but much older) generic Emacs commands. It also has the advantage that if you want to run SML from inside an Emacs window, that will work in Aquamacs and in the standard OS X terminal-window emacs, but not in the generic OS X emacs application described in the previous paragraph. Aquamacs also installs as a regular program in the Applications folder.

Linux

Emacs is available for all Linux distributions. Use your package manager to install it if it is not already there.

SML Mode for Emacs

This you probably want (need?) to do even if you're using sml and emacs in the CSE undergrad labs.

The main reason for using Emacs for CSE341 is that there is a good Emacs editing mode that understands syntax coloring and automatic indenting for SML code. Go to http://www.iro.umontreal.ca/~monnier/elisp/ and download the latest version. Unpack it and you should get a folder named sml-mode-4.1 (the current version). You can place this folder anywhere you like, although if you are running in the CSE labs you should put it somewhere in your user file directories (Z drive on Windows); not on the local disk of a particular machine.

To get sml mode to work with emacs, you need to create a .emacs file that contains the following two lines:

(setq load-path (cons "/path/to/where/you/stored/sml-mode-4.1" load-path))
(load "sml-mode-startup")

(replace "path/to/where/you/stored" with the appropriate path). On Windows machines, the path can look like "Z:\\look\\here", but each backslash (\) needs to be doubled (\\) because backslash by itself is a special meta-character in emacs settings files and a pair \\ is needed to represent each backslash in the path. The .emacs file itself needs to be in the "home" directory that emacs searches when it starts up. The easiest way to get this right is to use emacs itself to create the .emacs file. Run emacs and enter the command c-x c-f ~/.emacs (c-x c-f are control-x control-f in emacs speak). Emacs expands the ~ to wherever it actually does look for its configuration file, and this works correctly on Windows, OS X, and Linux.

To verify that sml mode is properly installed, exit emacs then restart it so it will read the new .emacs file. Then edit an existing or new sml file (try c-x c-f test.sml to create a new file if nothing else is handy). You should see the mode display at the bottom of the emacs window change from Fundamental (or whatever it was) to SML. If you enter a line of code like val n = 1; you should see colors highlighting the keywords and variable names. When you're editing code, whenever you hit the TAB key, emacs will try to reindent the current line appropriately.

(There are much more elaborate installation instructions in the documentation included in the sml-mode folder, but these are aimed at system administrators who might want to install sml mode for an entire site instead of a single user. Please ignore these - what's given here is all you need.)

 

Last updated 3/31/2011 to add information about \\ in .emacs files. HP