Standard ML (SML) is a generalpurpose, modular, functional programming language with compiletime type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of theorem provers.
SML is a modern descendant of the ML programming language used in the LCF theoremproving project. It is distinctive among widely used languages in that it has a formal specification, given as typing rules and operational semantics in The Definition of Standard ML (1990, revised and simplified as The Definition of Standard ML (Revised) in 1997).
Like all functional programming languages, a key feature of Standard ML is the function, which is used for abstraction. For instance, the factorial function can be expressed as:
fun factorial x =
if x = 0 then 1 else x * factorial (x1)
A Standard ML compiler is required to infer the static type int > int of this function without usersupplied type annotations. I.e., it has to deduce that x is only used with integer expressions, and must therefore itself be an integer, and that all valueproducing expressions within the function return integers.
The same function can be expressed with clausal function definitions where the ifthenelse conditional is replaced by a sequence of templates of the factorial function evaluated for specific values, separated by '', which are tried one by one in the order written until a match is found:
fun factorial 0 = 1
 factorial n = n * factorial (n  1)
Using a local function, this function can be rewritten to use tail recursion:
fun factorial x =
let
fun tail_fact p 0 = p
 tail_fact p n = tail_fact (p * n) (n  1)
in
tail_fact 1 x
end
The value of a letexpression is that of the expression between in and end.
Standard ML has an advanced module system, allowing programs to be decomposed into hierarchically organized structures of logically related type and value declarations. SML modules provide not only namespace control but also abstraction, in the sense that they allow programmers to define abstract data types.
Three main syntactic constructs comprise the SML module system: signatures, structures and functors. A structure is a module; it consists of a collection of types, exceptions, values and structures (called substructures) packaged together into a logical unit. A signature is an interface, usually thought of as a type for a structure: it specifies the names of all the entities provided by the structure as well as the arities of type components, the types of value components, and signatures for substructures. The definitions of type components may or may not be exported; type components whose definitions are hidden are abstract types. Finally, a functor is a function from structures to structures; that is, a functor accepts one or more arguments, which are usually structures of a given signature, and produces a structure as its result. Functors are used to implement generic data structures and algorithms.
For example, the signature for a queue data structure might be:
signature QUEUE =
sig
type 'a queue
exception Queue
val empty : 'a queue
val insert : 'a * 'a queue > 'a queue
val isEmpty : 'a queue > bool
val peek : 'a queue > 'a
val remove : 'a queue > 'a * 'a queue
end
This signature describes a module that provides a parameterized type queue
of queues, an exception called Queue
, and five values (four of which are functions) providing the basic operations on queues. One can now implement the queue data structure by writing a structure with this signature:
structure TwoListQueue :> QUEUE =
struct
type 'a queue = 'a list * 'a list
exception Queue
val empty = ([],[])
fun insert (a,(ins,outs)) = (a::ins,outs)
fun isEmpty ([],[]) = true
 isEmpty _ = false
fun peek ([],[]) = raise Queue
 peek (ins,[]) = hd (rev ins)
 peek (ins,a::outs) = a
fun remove ([],[]) = raise Queue
 remove (ins,[]) =
let val newouts = rev ins
in (hd newouts,([],tl newouts))
end
 remove (ins,a::outs) = (a,(ins,outs))
end
This definition declares that TwoListQueue
is an implementation of the QUEUE
signature. Furthermore, the opaque ascription (denoted by :>
) states that any type components whose definitions are not provided in the signature (i.e., queue
) should be treated as abstract, meaning that the definition of a queue as a pair of lists is not visible outside the module. The body of the structure provides bindings for all of the components listed in the signature.
To use a structure, one can access its type and value members using "dot notation". For instance, a queue of strings would have type string TwoListQueue.queue
, the empty queue is TwoListQueue.empty
, and to remove the first element from a queue called q
one would write TwoListQueue.remove q
.
Snippets of SML code are most easily studied by entering them into a "toplevel", also known as a readevalprint loop. This is an interactive session that prints the inferred types of resulting or defined expressions. Many SML implementations provide an interactive toplevel, including SML/NJ:
$ sml
Standard ML of New Jersey v110.52 [built: Fri Jan 21 16:42:10 2005]

Code can then be entered at the "" prompt. For example, to calculate 1+2*3:
 1 + 2 * 3;
val it = 7 : int
The toplevel infers the type of the expression to be "int" and gives the result "7".
The following program "hello.sml":
print "Hello world!n";
can be compiled with MLton:
$ mlton hello.sml
and executed:
$ ./hello
Hello world!
$
Main article: Merge sort
Here, Merge Sort is implemented in three functions: split, merge and MergeSort.
The split function is implemented with a local function named split_iter, which has an additional parameter. One uses such a function because it is tail recursive. This function makes use of SML's pattern matching syntax, being defined for both the nonempty ('x::xs') and empty ('[]') list cases.
(* Given a list of elements, split it into two elements of
* about the same size.
* O(n)
*)
local
fun split_iter (x::xs, left, right) = split_iter(xs, right, x::left)
 split_iter ([], left, right) = (left, right)
in
fun split(x) = split_iter(x,[],[])
end;
The localinend syntax could be replaced with a letinend syntax, yielding the equivalent definition:
fun split(x) =
let
fun split_iter (x::xs, left, right) = split_iter(xs, right, x::left)
 split_iter ([], left, right) = (left, right)
in
split_iter(x,[],[])
end;
As with split, merge also uses a local function merge_iter for efficiency. Merge_iter is defined in terms of cases: when two nonempty lists are passed, when one nonempty list is passed, and when two empty lists are passed. Note the use of '_' as a wildcard pattern.
This function merges two 'ascending lists into one descending because of how lists are constructed in SML. Because SML lists are implemented as imbalanced binary trees, it is efficient to prepend an element to a list, but very inefficient to append an element to a list.
(* Given two lists in ascending order, merge them into
* a single list in descending order.
* The function lt(a,b) iff a < b
* O(n)
*)
local
fun merge_iter (out, left as (x::xs), right as (y::ys), lt) =
if lt(x, y)
then merge_iter(x::out, xs, right, lt)
else merge_iter(y::out, left, ys, lt)
 merge_iter (out, x::xs, [], lt) = merge_iter(x::out, xs, [], lt)
 merge_iter (out, [], y::ys, lt) = merge_iter(y::out, [], ys, lt)
 merge_iter (out, [], [], _) = out
in
fun merge(x,y,lt) = merge_iter([],x,y,lt)
end;Finally, the MergeSort function.
(* Sort a list in ascending order according to lt(a,b) <==> a < b
* O(n log n)
*)
fun MergeSort(empty as [], _) = empty MergeSort(single as _::[], _) = single  MergeSort(x, lt) =
let
val (left, right) = split(x)
val sl = MergeSort(left, lt)
val sr = MergeSort(right, lt)
val s = merge(sl,sr,lt)
in
rev s
end;
Also note that the code makes no mention of variable types, with the exception of the :: and [] syntax which signify lists. This code will sort lists of any type, so long as a consistent ordering function lt can be defined. Using HindleyMilner type inference, the compiler is capable of inferring the types of all variables, even complicated types such as that of the lt function.
In SML, the IntInf module provides arbitraryprecision integer arithmetic. Moreover, integer literals may be used as arbitraryprecision integers without the programmer having to do anything.
The following program "fact.sml" implements an arbitraryprecision factorial function and prints the factorial of 120:
fun fact n : IntInf.int =
if n=0 then 1 else n * fact(n  1)
val =
print (IntInf.toString (fact 120)^"n")
and can be compiled and run with:
$ mlton fact.sml
$ ./fact
66895029134491270575881180540903725867527463331380298102956713523016335
57244962989366874165271984981308157637893214090552534408589408121859898
481114389650005964960521256960000000000000000000000000000
Since SML is a functional programming language, it is easy to create and pass around functions in SML programs. This capability has an enormous number of applications. Calculating the numerical derivative of a function is one such application. The following SML function "d" computes the numerical derivative of a given function "f" at a given point "x":
 fun d delta f x =
(f (x + delta)  f (x  delta)) / (2.0 * delta);
val d = fn : real > (real > real) > real > real
This function requires a small value "delta". A good choice for delta when using this algorithm is the cube root of the machine epsilon.
The type of the function "d" indicates that it maps a "float" onto another function with the type "(real > real) > real > real". This allows us to partially apply arguments. This functional style is known as currying. In this case, it is useful to partially apply the first argument "delta" to "d", to obtain a more specialised function:
 val d = d 1E~8;
val d = fn : (real > real) > real > real
Note that the inferred type indicates that the replacement "d" is expecting a function with the type "real > real" as its first argument. We can compute a numerical approximation to the derivative of x^3x1 at x=3 with:
 d (fn x => x * x * x  x  1.0) 3.0;
val it = 25.9999996644 : real
The correct answer is f'(x) = 3x^21 => f'(3) = 271 = 26.
The function "d" is called a "higherorder function" because it accepts another function ("f") as an argument.
Curried and higherorder functions can be used to eliminate redundant code. For example, a library may require functions of type a > b
, but it is more convenient to write functions of type a * c > b
where there is a fixed relationship between the objects of type a
and c
. A higher order function of type (a * c > b) > (a > b) can factor out this commonality. This is an example of the adapter pattern.
The 1D Haar wavelet transform of an integerpoweroftwolength list of numbers can be implemented very succinctly in SML and is an excellent example of the use of pattern matching over lists, taking pairs of elements ("h1" and "h2") off the front and storing their sums and differences on the lists "s" and "d", respectively:
 fun haar l =
let fun aux [s] [] d = s :: d
 aux [] s d = aux s [] d
 aux (h1::h2::t) s d =
aux t (h1 + h2 :: s) (h1  h2 :: d)
 aux _ _ _ = raise Empty
in aux l [] []
end;
val haar = fn : int list > int list
For example:
 haar [1, 2, 3, 4, ~4, ~3, ~2, ~1];
val it = [0,20,4,4,~1,~1,~1,~1] : int list
Pattern matching is a useful construct that allows complicated transformations to be represented clearly and succinctly. Moreover, SML compilers turn pattern matches into efficient code, resulting in programs that are not only shorter but also faster.
Many SML implementations exist, including:
All of these implementations are opensource and freely available. Most are implemented themselves in SML. There are no longer any commercial SML implementations. Harlequin once produced a commercial IDE and compiler for SML called MLWorks. The company is now defunct. MLWorks is believed to have been passed on to Xanalys.