Tips on Using SML on Mac OS X

(Thanks to Craig Maccomber and Chloe Houvener for writing these instructions!

The easiest way to install SMLNJ and various other Unix/Linux software on your Mac is using the Mac "Ports" system. If you have Ports installed, you can type the following into a Terminal window to install SMLNJ:

sudo port install Smlnj

If you don't use Ports, you can do a manual install of SMLNJ instead. To do a manual install, go to smlnj.org which gives you the download. I suggest getting the latest "working" versions. That provides a standard mac installer, which you will need to run. As stated in the MACOSXINSTALL text file, downloading/installing from there will not add sml to your path. You can add it to your path by editing your .bash_profile file (it's invisible in your user folder). You prabably want to add a line like this:

PATH=$PATH:/usr/local/smlnj-110.72/bin

If you need further info about screwing with path stuff, you may want to look up some stuff. I found this at http://www.troubleshooters.com/linux/prepostpath.htm

There are several decent text editors for the Mac, such as Fraise (free), TextWrangler (free), and TextMate (not free).

If you use TextWrangler, you can get a language module that provides context coloring for ML here: http://www.cl.cam.ac.uk/teaching/0809/FoundsCS/usingml.html (see the bottom).

TextWrangler also is a handy way to open and edit invisible (and/or) permission restricted text files. You can use File->Open Hidden... and show invisibles to open your .bash_profile file.

To run your file, save it, and in the terminal you can enter sml yourFilePath. You should know you can just drag the file onto the terminal to get the file path. Issue a control d to terminate, and an up arrow and enter to rerun. You can also just enter sml to enter the interpreter. You can do all this without editing your path, but you will need to use the full path to sml instead of just 'sml'. The full path should be /usr/local/smlnj-110.72/bin/sml

It should be possible to make one of the menu items run it, then bind a keyboard shortcut to it, but I haven't gotten that far. I'll edit later and add a solution when I have one.

And now you should be able to easily edit ML code with context coloring, and run it on your Mac.

Edit: Here is a handy applyScript written by Craig Maccomber for TextWrangler which you can map to a keyboard shortcut to run the current file in ml. You an edit in the full path to sml to avoid having to add /usr/local/smlnj-110.72/bin/sml to the path:

tell application "TextWrangler"
    set loc to file of window 1 as string -- the mac path
    set ploc to POSIX path of loc -- the unix path
end tell
-- http://www.alecjacobson.com/weblog/?p=52
on last_offset(the_text, char)
    try
		set i to 1
		set last_occurrence to 0
		repeat count of the_text times
			if item i of the_text as string = char then
				set last_occurrence to i
			end if
			set i to i + 1
		end repeat
    on error
		return 0
    end try
    return last_occurrence
end last_offset
on basedir(the_path)
    set last_occurrence to last_offset(the_path, "/")
    return items 1 thru (last_occurrence) of the_path as string
end basedir
set base to basedir(ploc)
tell application "Terminal"
    activate
    do script with command "cd " & base & "
    sml " & ploc
end tell
tell application "System Events"
    -- kill the interperter, remove if you want to leave it running
    keystroke "d" using control down
end tell