Intro to Standard ML debugging


Interesting URLs

Standard ML of New Jersey: http://cm.bell-labs.com/cm/cs/what/smlnj/index.html

MLworks homepage: http://www.harlequin.com/products/ads/ml/product.shtml

Examples Dr. Fischer used in class: ~cs538-1/public/sml

Using SML Over AFS

To run SML on any Sparc / Solaris machine in the labs, simply type:

     sml

We have also installed MLWorks (for Sparc Solaris) in the public directory at:

     ~cs538-1/public/mlworks/bin/uw-mlworks.sh

This version of MLWorks is nag-ware. They call it the "personal edition" and try to get you to buy the "professional" edition. It delays for 10 seconds every time you start it (showing an advertisement for the pro edition), and limits you to 1 hour sessions. If it asks you to logout, save your work, get out, and get right back in for another hour. There are also limits on the size of a project, but you cannot possibly come close to them on this assignment.

Both SML/NJ and MLWorks are available for Win95 and WinNT. You can download them from the web sites named above.


Should I use SML/NJ? Or MLWorks?

For debugging, MLWorks has tracing, breakpoints, and a class browser for both current and standard basis libraries. You may want to use SML/NJ for normal work and only bring up MLWorks if you have a tough bug.

Your First Statements in SML/NJ

You will see a single dash as the prompt. This is SML telling you it is your turn to type. Type:

     3 + 4

It did NOTHING! That's right. It is waiting for you to type a semi-colon to tell it you are done. The prompt is a "=".

     ;
With a mere 32 Megabytes of RAM and a 200 Megahertz processor, we have successfully added 3 to 4 giving 7. All expressions evaluated at the top-level are bound to a mythical name "it". To bind a name to it, we name it:
     val x = 3 + 4;
Now SML/NJ will remember that x is defined to be 3+4.

You will not want to write all of your programs inside SML/NJ. (Although MLWorks actually integrates with emacs and/or vi quite nicely). You should open a window with your favorite editor and create a file named p2.sml. Start with:

     fun sum_all [] = 0
       | sum_all (h::t) = h + sum_all(t);

Starting MLWorks

Then start up MLWorks by runnging the shell script:
     ~cs538-1/public/mlworks/bin/uw-mlworks.sh

You will see a "Listener" window that lets you type in ML commands and try them interactively. That works for small programs. We'll see in a few minutes how to save even the commands we typed interactively.

Reading in a Program from a Disk File

The prompt will say "MLWorks >". To load your program into MLWorks, type something like (depending on where you put the p2.sml file):

     use "~/private/cs538/p2/p2.sml";

MLWorks now tells you the type of the function you just brought in.

To test out sum_all, let't run it on some test data:

     sum_all ( [1,2,3,4,5] );
Did it give the right answer?

Click on "Previous" to bring it back to the active prompt. You can't edit it or re-use it without bringing it to the active prompt.

You could also have clicked on "History" to see all of the commands you've typed. Or "Tools" / "History" to see the results.

Debugging

To enable tracing, you will have to click on "Listener" / "properties" / "mode" / "debugging". Any functions you compiled before you did this will NOT have debugging enabled.

Now let's compile one you may remember:

     fun subset_sum (v, []) = false
        | subset_sum (v, [h] = v = h
        | subset_sum (v, (h::t)) = subset_sum(v,t)
                            orelse subset_sum(v-h,t);

Test it to see if it works:

     subset_sum (10, [1,2,3,4]);
     subset_sum (999, [1,2,3,4]);

To see a trace, click on "Tools" / "Trace & Breakpoint Manager" / "Add Break or Trace Point" / "Add Trace Point", Type in:

     subset_sum
and click on "Apply". Now, go back to the Listener Window and (with the test of subset_sum still at the prompt) hit Enter.

To analyse the program step by step, bring up a test of the function at the prompt and click on "Step". It will show you a Stack Browser. Click on "val frame argument" and you will see the values of the input parameters to the function currently being executed. That brings up an Inspector. Double click on a component to see it's values graphically displayed.


Saving Your Work to a File

To save your work in MLWorks, click on "File" / "Save Listener Input As . . ."

"Tools" / "Context Browser" will show all of the function you have defined. "Tools" / "System Browser" will show all of the functions in the basis library, along with their types.

For more information, there is plenty of on-line help. Click on "Help".


Comments, complaints, and corrections are appreciated. This page created and maintained by Jim Gast (jgast@cs.wisc.edu)

Updated November 11, 1998