CSE 428: Standard ML

Online Documentation

Alternative Implementations

Using Standard ML on the Sparcs

Avoiding abbreviations in the output

When the system gives the result of an evaluation, it may abbreviate with the character ``#'' part of the representation of a data structure. For instance:
   - datatype bintree = emp | nod of bintree * int * bintree;
   datatype bintree = emp | nod of bintree * int * bintree
   - nod(nod(nod(emp,2,emp),3,emp),1,nod(nod(emp,4,emp),5,emp));
   val it = nod (nod (nod #,3,emp),1,nod (nod #,5,emp)) : bintree
In order to avoid this, you can use the "print switches" OF SML, which are
   Compiler.Control.Print.printDepth       (for data structures)
   Compiler.Control.Print.printLength      (for lists)
   Compiler.Control.Print.stringDepth      (for strings)
You should set these at the prompt "-", e.g.,
   - Compiler.Control.Print.printDepth := 1000;
   val it = () : unit
   - nod(nod(nod(emp,2,emp),3,emp),1,nod(nod(emp,4,emp),5,emp));
   val it = nod (nod (nod (emp,2,emp),3,emp),1,nod (nod (emp,4,emp),5,emp))
   : bintree

Debugging SML

One simple way to trace a SML program is to use "print" commands.

Unfortunately, the print command of SML works only on strings; for other data, you need to convert them to strings. You can use the SML libraries for the various conversions on predefined types. For user-defined data structures, you need to define your own conversion routines. Here below is an example of "tracing" a program. Note the use of the parenteses around the print command. They are neceesary because otherwise the ";" would be interpreted as the end of the definition.

   - fun append [] L = L
       | append (x::K) L = (print x; print "\n"; x:: append K L);
   val append = fn : string list -> string list -> string list
   - append ["a","b","c"] ["d","e","f"];
   val it = ["a","b","c","d","e","f"] : string list

Matching nonexaustive

Consider the following definition of the function last : 'a list -> 'a, which returns the last element of a list.
   - fun last [x] = x
       | last (x::L) = last L;
   stdIn:154.1-155.25 Warning: match nonexhaustive
             x :: nil => ...
             x :: L => ...
   val last = fn : 'a list -> 'a
The reason of this warning message is that the definition of last does not consider all possible pattern cases for the parameter: the case of empty list is missing. This warning has no consequences on the execution and you can just ignore it. But you should, of course, avoid applying last to the empty list.

Similar warnings are generated each time you don't consider all possible patterns in the definition of a function.

Garbage collection messages

From time to time during your SML session you will get messages like the following:
   GC #   (0 ms)
This only means that the system has done a grbage collection. Just ignore it.

Useful Switches

Standard ML of New Jersey (Version 110) provides various connections to the file system, operating system, etc, through its switches. These are typically variables or functions.