CS334: ML Hints

Table Of Contents

Quitting The Interpreter

Type Ctrl-D to exit the ML interpreter if you run it interactively.

Source Code Comments

Comments in ML have the form (* ... *).

Output Messages Containing “…” or “#”

By default, ML abbreviates large lists, data structures, and strings when printing them. For example, it may print "[1,2,3,4,....]" instead of the complete contents of a list, or "Node(Node(Node(#,#),#),Leaf(3))" where the #’s represent unprinted parts of a datatype value. Include the following line at the top of the file to print larger datatype values before resorting to #.

Control.Print.printDepth := 100;

Similarly, include the following lines to print long lists and strings:

Control.Print.printLength := 500;
Control.Print.stringDepth := 500;

Warning: calling polyEqual

The version of ML installed on the UNIX machines reports a warning whenever you use = to compare two values with polymorphic type. For example,

fun eq(x,y) = (x = y);

will cause this warning to be generated, because x and y will have polymorhic type ''a. This is perfectly fine and you may ignore the warning. It is not reporting any kind of semantic error or type error in your code.

The compiler reports the warning because there can be a slight ineffeciency in how ML tests whether two values of a polymorphic type are equal. In particular, to perform the equality test, the run-time system must first determine what types of values you are currently using and then determine whether the values are equal. The first part (checking the run-time types) can make the = test slightly slower than if the types are known ahead of time (such as when we test 3 = 4 and know that the = test is being applied to integers). However, that is not something most users of ML ever need to worry about, and certainly not us. (I'm actually surprised the compiler writers decided to have the compiler generate this warning- previous releases never did.)

Error: duplicate variable in pattern(s)

You may only use a name once in any pattern. Thus,

fun eq(x,x) = true
  | eq(x,y) = false;

will give you an error. You would need to rewrite the above as we wrote it in the last section:

fun eq(x,y) = (x = y);

Warning: match nonexhaustive

You are performing pattern matching, but not handling all cases. In other words, some value exists that does not match any of the patterns you provide. If pattern matching is applied to such a value, the program will fail at run time with an exception.

If you are pattern matching on lists, be sure to handle the nil and non-empty cases. For datatypes, be sure to handle each different constrctor form with its own case.

Other Common Errors

If you are getting syntax errors that you cannot decipher, double check the following: