<s>
Standard	B-Language
ML	I-Language
(	O
SML	B-Language
)	O
is	O
a	O
general-purpose	B-Language
,	O
modular	B-Architecture
,	O
functional	B-Language
programming	I-Language
language	I-Language
with	O
compile-time	O
type	O
checking	O
and	O
type	O
inference	O
.	O
</s>
<s>
It	O
is	O
popular	O
among	O
compiler	B-Language
writers	O
and	O
programming	O
language	O
researchers	O
,	O
as	O
well	O
as	O
in	O
the	O
development	O
of	O
theorem	B-Application
provers	I-Application
.	O
</s>
<s>
Standard	B-Language
ML	I-Language
is	O
a	O
modern	O
dialect	O
of	O
ML	B-Algorithm
,	O
the	O
language	O
used	O
in	O
the	O
Logic	O
for	O
Computable	O
Functions	O
(	O
LCF	O
)	O
theorem-proving	O
project	O
.	O
</s>
<s>
It	O
is	O
distinctive	O
among	O
widely	O
used	O
languages	O
in	O
that	O
it	O
has	O
a	O
formal	O
specification	O
,	O
given	O
as	O
typing	O
rules	O
and	O
operational	O
semantics	O
in	O
The	O
Definition	O
of	O
Standard	B-Language
ML	I-Language
.	O
</s>
<s>
Standard	B-Language
ML	I-Language
is	O
a	O
functional	B-Language
programming	I-Language
language	I-Language
with	O
some	O
impure	O
features	O
.	O
</s>
<s>
Programs	O
written	O
in	O
Standard	B-Language
ML	I-Language
consist	O
of	O
expressions	O
as	O
opposed	O
to	O
statements	O
or	O
commands	O
,	O
although	O
some	O
expressions	O
of	O
type	O
unit	O
are	O
only	O
evaluated	O
for	O
their	O
side-effects	O
.	O
</s>
<s>
Like	O
all	O
functional	B-Language
languages	I-Language
,	O
a	O
key	O
feature	O
of	O
Standard	B-Language
ML	I-Language
is	O
the	O
function	O
,	O
which	O
is	O
used	O
for	O
abstraction	B-Application
.	O
</s>
<s>
An	O
SML	B-Language
compiler	B-Language
must	O
infer	O
the	O
static	O
type	O
without	O
user-supplied	O
type	O
annotations	O
.	O
</s>
<s>
or	O
as	O
a	O
lambda	B-General_Concept
function	I-General_Concept
:	O
</s>
<s>
Here	O
,	O
the	O
keyword	O
introduces	O
a	O
binding	O
of	O
an	O
identifier	O
to	O
a	O
value	O
,	O
introduces	O
an	O
anonymous	B-General_Concept
function	I-General_Concept
,	O
and	O
allows	O
the	O
definition	O
to	O
be	O
self-referential	O
.	O
</s>
<s>
The	O
encapsulation	O
of	O
an	O
invariant-preserving	O
tail-recursive	B-Language
tight	O
loop	O
with	O
one	O
or	O
more	O
accumulator	O
parameters	O
within	O
an	O
invariant-free	O
outer	O
function	O
,	O
as	O
seen	O
here	O
,	O
is	O
a	O
common	O
idiom	O
in	O
Standard	B-Language
ML	I-Language
.	O
</s>
<s>
Using	O
a	O
local	O
function	O
,	O
it	O
can	O
be	O
rewritten	O
in	O
a	O
more	O
efficient	O
tail-recursive	B-Language
style	O
:	O
</s>
<s>
Standard	B-Language
ML	I-Language
provides	O
strong	O
support	O
for	O
algebraic	O
datatypes	O
(	O
ADT	O
)	O
.	O
</s>
<s>
They	O
are	O
easy	O
to	O
define	O
and	O
easy	O
to	O
use	O
,	O
largely	O
because	O
of	O
pattern	B-Language
matching	I-Language
as	O
well	O
as	O
most	O
Standard	B-Language
ML	I-Language
implementations	O
 '	O
pattern-exhaustiveness	B-Algorithm
checking	O
and	O
pattern	O
redundancy	O
checking	O
.	O
</s>
<s>
In	O
object-oriented	O
programming	O
languages	O
,	O
a	O
disjoint	O
union	O
can	O
be	O
expressed	O
as	O
class	B-Application
hierarchies	I-Application
.	O
</s>
<s>
However	O
,	O
as	O
opposed	O
to	O
class	B-Application
hierarchies	I-Application
,	O
ADTs	O
are	O
closed	B-Application
.	O
</s>
<s>
Thus	O
the	O
extensibility	O
of	O
ADTs	O
is	O
orthogonal	O
to	O
the	O
extensibility	O
of	O
class	B-Application
hierarchies	I-Application
.	O
</s>
<s>
Class	B-Application
hierarchies	I-Application
can	O
be	O
extended	O
with	O
new	O
subclasses	O
which	O
implement	O
the	O
same	O
interface	B-Application
,	O
while	O
the	O
functionality	O
of	O
ADTs	O
can	O
be	O
extended	O
for	O
the	O
fixed	O
set	O
of	O
constructors	O
.	O
</s>
<s>
C	O
programmers	O
can	O
use	O
tagged	B-Language
unions	I-Language
,	O
dispatching	O
on	O
tag	O
values	O
,	O
to	O
accomplish	O
what	O
ML	B-Algorithm
accomplishes	O
with	O
datatypes	O
and	O
pattern	B-Language
matching	I-Language
.	O
</s>
<s>
Nevertheless	O
,	O
while	O
a	O
C	O
program	O
decorated	O
with	O
appropriate	O
checks	O
will	O
,	O
in	O
a	O
sense	O
,	O
be	O
as	O
robust	O
as	O
the	O
corresponding	O
ML	B-Algorithm
program	O
,	O
those	O
checks	O
will	O
of	O
necessity	O
be	O
dynamic	O
;	O
ML	B-Algorithm
's	O
static	O
checks	O
provide	O
strong	O
guarantees	O
about	O
the	O
correctness	O
of	O
the	O
program	O
at	O
compile	B-Language
time	O
.	O
</s>
<s>
Pattern-exhaustiveness	B-Algorithm
checking	O
will	O
make	O
sure	O
that	O
each	O
constructor	O
of	O
the	O
datatype	O
is	O
matched	O
by	O
at	O
least	O
one	O
pattern	O
.	O
</s>
<s>
The	O
compiler	B-Language
will	O
issue	O
a	O
warning	O
that	O
the	O
case	O
expression	O
is	O
not	O
exhaustive	O
,	O
and	O
if	O
a	O
is	O
passed	O
to	O
this	O
function	O
at	O
runtime	O
,	O
will	O
be	O
raised	O
.	O
</s>
<s>
Therefore	O
,	O
this	O
definition	O
as	O
a	O
whole	O
exhibits	O
redundancy	O
,	O
and	O
causes	O
a	O
compile-time	O
warning	O
.	O
</s>
<s>
The	O
function	O
from	O
the	O
basis	O
library	O
is	O
one	O
of	O
the	O
most	O
commonly	O
used	O
higher-order	O
functions	O
in	O
Standard	B-Language
ML	I-Language
:	O
</s>
<s>
A	O
more	O
efficient	O
implementation	O
with	O
tail-recursive	B-Language
:	O
</s>
<s>
Exceptions	O
are	O
raised	O
with	O
the	O
keyword	O
and	O
handled	O
with	O
the	O
pattern	B-Language
matching	I-Language
construct	O
.	O
</s>
<s>
The	O
same	O
optimization	O
can	O
be	O
obtained	O
with	O
a	O
tail	B-Language
call	I-Language
.	O
</s>
<s>
Standard	B-Language
ML	I-Language
's	O
advanced	O
module	B-Architecture
system	I-Architecture
allows	O
programs	O
to	O
be	O
decomposed	O
into	O
hierarchically	O
organized	O
structures	O
of	O
logically	O
related	O
type	O
and	O
value	O
definitions	O
.	O
</s>
<s>
Modules	B-Architecture
provide	O
not	O
only	O
namespace	O
control	O
but	O
also	O
abstraction	B-Application
,	O
in	O
the	O
sense	O
that	O
they	O
allow	O
the	O
definition	O
of	O
abstract	O
data	O
types	O
.	O
</s>
<s>
Three	O
main	O
syntactic	O
constructs	O
comprise	O
the	O
module	B-Architecture
system	I-Architecture
:	O
signatures	O
,	O
structures	O
and	O
functors	O
.	O
</s>
<s>
A	O
signature	O
is	O
an	O
interface	B-Application
,	O
usually	O
thought	O
of	O
as	O
a	O
type	O
for	O
a	O
structure	O
;	O
it	O
specifies	O
the	O
names	O
of	O
all	O
entities	O
provided	O
by	O
the	O
structure	O
as	O
well	O
as	O
the	O
arity	O
of	O
each	O
type	O
component	O
,	O
the	O
type	O
of	O
each	O
value	O
component	O
,	O
and	O
the	O
signature	O
of	O
each	O
substructure	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
signature	O
for	O
a	O
queue	B-Application
may	O
be	O
:	O
</s>
<s>
This	O
signature	O
describes	O
a	O
module	B-Architecture
that	O
provides	O
a	O
polymorphic	O
type	O
,	O
,	O
and	O
values	O
that	O
define	O
basic	O
operations	O
on	O
queues	B-Application
.	O
</s>
<s>
A	O
structure	O
is	O
a	O
module	B-Architecture
;	O
it	O
consists	O
of	O
a	O
collection	O
of	O
types	O
,	O
exceptions	O
,	O
values	O
and	O
structures	O
(	O
called	O
substructures	O
)	O
packaged	O
together	O
into	O
a	O
logical	O
unit	O
.	O
</s>
<s>
A	O
queue	B-Application
structure	O
can	O
be	O
implemented	O
as	O
follows	O
:	O
</s>
<s>
should	O
be	O
abstract	O
,	O
meaning	O
that	O
the	O
definition	O
of	O
a	O
queue	B-Application
as	O
a	O
pair	O
of	O
lists	O
is	O
not	O
visible	O
outside	O
the	O
module	B-Architecture
.	O
</s>
<s>
Functors	O
are	O
used	O
to	O
implement	O
generic	B-Language
data	O
structures	O
and	O
algorithms	O
.	O
</s>
<s>
One	O
popular	O
algorithm	O
for	O
breadth-first	B-Algorithm
search	I-Algorithm
of	O
trees	O
makes	O
use	O
of	O
queues	B-Application
.	O
</s>
<s>
Here	O
we	O
present	O
a	O
version	O
of	O
that	O
algorithm	O
parameterized	O
over	O
an	O
abstract	O
queue	B-Application
structure	O
:	O
</s>
<s>
Within	O
,	O
the	O
representation	O
of	O
the	O
queue	B-Application
is	O
not	O
visible	O
.	O
</s>
<s>
More	O
concretely	O
,	O
there	O
is	O
no	O
way	O
to	O
select	O
the	O
first	O
list	O
in	O
the	O
two-list	O
queue	B-Application
,	O
if	O
that	O
is	O
indeed	O
the	O
representation	O
being	O
used	O
.	O
</s>
<s>
This	O
data	B-Application
abstraction	I-Application
mechanism	O
makes	O
the	O
breadth-first	B-Algorithm
search	I-Algorithm
truly	O
agnostic	O
to	O
the	O
queue	B-Application
's	O
implementation	O
.	O
</s>
<s>
This	O
is	O
in	O
general	O
desirable	O
;	O
in	O
this	O
case	O
,	O
the	O
queue	B-Application
structure	O
can	O
safely	O
maintain	O
any	O
logical	O
invariants	O
on	O
which	O
its	O
correctness	O
depends	O
behind	O
the	O
bulletproof	O
wall	O
of	O
abstraction	B-Application
.	O
</s>
<s>
Snippets	O
of	O
SML	B-Language
code	O
are	O
most	O
easily	O
studied	O
by	O
entering	O
them	O
into	O
an	O
interactive	B-Application
top-level	I-Application
.	O
</s>
<s>
Using	O
Hindley	B-Algorithm
–	I-Algorithm
Milner	I-Algorithm
type	I-Algorithm
inference	I-Algorithm
,	O
the	O
types	O
of	O
all	O
variables	O
can	O
be	O
inferred	O
,	O
even	O
complicated	O
types	O
such	O
as	O
that	O
of	O
the	O
function	O
.	O
</s>
<s>
is	O
implemented	O
with	O
a	O
stateful	B-Application
closure	B-Language
which	O
alternates	O
between	O
and	O
,	O
ignoring	O
the	O
input	O
:	O
</s>
<s>
is	O
a	O
closure	B-Language
that	O
consumes	O
an	O
order	O
operator	O
.	O
</s>
<s>
The	O
module	B-Architecture
provides	O
arbitrary-precision	O
integer	O
arithmetic	O
.	O
</s>
<s>
Curried	B-Application
functions	I-Application
have	O
many	O
applications	O
,	O
such	O
as	O
eliminating	O
redundant	O
code	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
module	B-Architecture
may	O
require	O
functions	O
of	O
type	O
,	O
but	O
it	O
is	O
more	O
convenient	O
to	O
write	O
functions	O
of	O
type	O
where	O
there	O
is	O
a	O
fixed	O
relationship	O
between	O
the	O
objects	O
of	O
type	O
and	O
.	O
</s>
<s>
This	O
allows	O
us	O
to	O
partially	O
apply	O
arguments	O
,	O
known	O
as	O
currying	B-Application
.	O
</s>
<s>
A	O
good	O
choice	O
for	O
when	O
using	O
this	O
algorithm	O
is	O
the	O
cube	O
root	O
of	O
the	O
machine	B-Algorithm
epsilon	I-Algorithm
.	O
</s>
<s>
It	O
provides	O
modules	B-Architecture
for	O
trees	O
,	O
arrays	O
and	O
other	O
data	O
structures	O
as	O
well	O
as	O
input/output	O
and	O
system	O
interfaces	B-Application
.	O
</s>
<s>
For	O
numerical	O
computing	O
,	O
a	O
Matrix	O
module	B-Architecture
exists	O
(	O
but	O
is	O
currently	O
broken	O
)	O
,	O
.	O
</s>
<s>
For	O
graphics	O
,	O
cairo-sml	O
is	O
an	O
open	O
source	O
interface	B-Application
to	O
the	O
Cairo	B-Language
graphics	I-Language
library	I-Language
.	O
</s>
<s>
Implementations	O
of	O
Standard	B-Language
ML	I-Language
include	O
the	O
following	O
:	O
</s>
<s>
:	O
a	O
full	O
implementation	O
of	O
Standard	B-Language
ML	I-Language
that	O
produces	O
fast	O
code	O
and	O
supports	O
multicore	O
hardware	O
(	O
via	O
Posix	O
threads	O
)	O
;	O
its	O
runtime	O
system	O
performs	O
parallel	O
garbage	O
collection	O
and	O
online	O
sharing	O
of	O
immutable	O
substructures	O
.	O
</s>
<s>
:	O
an	O
extension	O
of	O
SML	B-Language
providing	O
record	O
polymorphism	O
and	O
C	O
language	O
interoperability	O
.	O
</s>
<s>
is	O
a	O
REPL	B-Application
version	O
of	O
ML	B-Algorithm
with	O
formally	O
verified	O
runtime	O
and	O
translation	O
to	O
assembler	O
.	O
</s>
<s>
Isabelle	B-Application
(	O
)	O
integrates	O
parallel	O
Poly/ML	O
into	O
an	O
interactive	O
theorem	O
prover	O
,	O
with	O
a	O
sophisticated	O
IDE	O
(	O
based	O
on	O
jEdit	B-Language
)	O
for	O
official	O
Standard	B-Language
ML	I-Language
(	O
SML'97	O
)	O
,	O
the	O
Isabelle/ML	O
dialect	O
,	O
and	O
the	O
proof	O
language	O
.	O
</s>
<s>
Starting	O
with	O
Isabelle2016	O
,	O
there	O
is	O
also	O
a	O
source-level	O
debugger	O
for	O
ML	B-Algorithm
.	O
</s>
<s>
Poplog	B-Language
implements	O
a	O
version	O
of	O
Standard	B-Language
ML	I-Language
,	O
along	O
with	O
Common	B-Language
Lisp	I-Language
and	O
Prolog	B-Language
,	O
allowing	O
mixed	O
language	O
programming	O
;	O
all	O
are	O
implemented	O
in	O
POP-11	B-Language
,	O
which	O
is	O
compiled	B-Application
incrementally	I-Application
.	O
</s>
<s>
is	O
a	O
full	O
certifying	O
compiler	B-Language
for	O
Standard	B-Language
ML	I-Language
which	O
uses	O
typed	O
intermediate	B-Application
languages	I-Application
to	O
optimize	O
code	O
and	O
ensure	O
correctness	O
,	O
and	O
can	O
compile	B-Language
to	O
typed	B-Language
assembly	I-Language
language	I-Language
.	O
</s>
<s>
Most	O
are	O
implemented	O
themselves	O
in	O
Standard	B-Language
ML	I-Language
.	O
</s>
<s>
There	O
are	O
no	O
longer	O
any	O
commercial	O
implementations	O
;	O
Harlequin	O
,	O
now	O
defunct	O
,	O
once	O
produced	O
a	O
commercial	O
IDE	O
and	O
compiler	B-Language
called	O
MLWorks	O
which	O
passed	O
on	O
to	O
Xanalys	O
and	O
was	O
later	O
open-sourced	O
after	O
it	O
was	O
acquired	O
by	O
Ravenbrook	O
Limited	O
on	O
April	O
26	O
,	O
2013	O
.	O
</s>
<s>
The	O
IT	O
University	O
of	O
Copenhagen	O
's	O
entire	O
enterprise	O
architecture	O
is	O
implemented	O
in	O
around	O
100,000	O
lines	O
of	O
SML	B-Language
,	O
including	O
staff	O
records	O
,	O
payroll	O
,	O
course	O
administration	O
and	O
feedback	O
,	O
student	O
project	O
management	O
,	O
and	O
web-based	O
self-service	O
interfaces	B-Application
.	O
</s>
<s>
The	O
proof	O
assistants	O
HOL4	O
,	O
Isabelle	B-Application
,	O
LEGO	B-Language
,	O
and	O
Twelf	B-Application
are	O
written	O
in	O
Standard	B-Language
ML	I-Language
.	O
</s>
<s>
It	O
is	O
also	O
used	O
by	O
compiler	B-Language
writers	O
and	O
integrated	O
circuit	O
designers	O
such	O
as	O
ARM	B-Architecture
.	O
</s>
