[go: nahoru, domu]

Jump to content

Standard ML: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
refactor headings
Citation bot (talk | contribs)
Removed parameters. | Use this bot. Report bugs. | Suggested by Headbomb | Category:CS1 maint: DOI inactive as of June 2024 | #UCB_Category 268/305
 
(47 intermediate revisions by 21 users not shown)
Line 1: Line 1:
{{Short description|Programming language}}
{{Short description|General-purpose functional programming language}}
{{Infobox programming language
{{Infobox programming language
| name = Standard ML
| name = Standard ML
| logo =
| logo =
| paradigm = [[Programming paradigm|Multi-paradigm]]: [[functional programming|functional]], [[imperative programming|imperative]], [[modular programming|modular]]<ref name="intro" />
| paradigm = [[Programming paradigm|Multi-paradigm]]: [[Functional programming|functional]], [[Imperative programming|imperative]], [[Modular programming|modular]]<ref name="intro"/>
| family = [[ML (programming language)|ML]]
| family = [[ML (programming language)|ML]]
| year = {{Start date and age|1983|df=yes}}
| designer =
| designer =
| latest release version = Standard ML '97<ref name="smlnj" />
| released = {{Start date and age|1983|df=yes}}<ref name="smlnj"/>
| latest release version = Standard ML '97<ref name="smlnj"/>
| latest release date = {{Start date and age|1997|df=yes}}
| latest release date = {{Start date and age|1997|df=yes}}
| typing = [[Type inference|Inferred]], [[type system#static type checking|static]], [[strong typing|strong]]
| typing = [[Type inference|Inferred]], [[Type system#static type checking|static]], [[Strong and weak typing|strong]]
| implementations = [[Standard ML of New Jersey|SML/NJ]], [[MLton]]
| implementations = [[Standard ML of New Jersey|SML/NJ]], [[MLton]], [[Poly/ML]]
| dialects = [[Alice (programming language)|Alice]], [[Concurrent ML]], [[Dependent ML]]
| dialects = [[Alice (programming language)|Alice]], [[Concurrent ML]], [[Dependent ML]]
| influenced_by = [[ML (programming language)|ML]], [[Hope (programming language)|Hope]], [[Pascal (programming language)|Pascal]]
| influenced by = [[ML (programming language)|ML]], [[Hope (programming language)|Hope]], [[Pascal (programming language)|Pascal]]
| influenced = [[Elm (programming language)|Elm]], [[F Sharp (programming language)|F#]], [[F* (programming language)|F*]], [[Haskell (programming language)|Haskell]], [[OCaml]], [[Python (programming language)|Python]],<ref name="itertools" /> [[Rust (programming language)|Rust]], [[Scala (programming language)|Scala]]
| influenced = [[Elm (programming language)|Elm]], [[F Sharp (programming language)|F#]], [[F* (programming language)|F*]], [[Haskell]], [[OCaml]], [[Python (programming language)|Python]],<ref name="itertools"/> [[Rust (programming language)|Rust]],<ref>{{Cite web |url=https://doc.rust-lang.org/reference/influences.html |title=Influences - The Rust Reference |website=The Rust Reference |access-date=2023-12-31}}</ref> [[Scala (programming language)|Scala]]
| file_ext = .sml
| file ext = .sml
| website =
| website =
}}
}}


'''Standard ML''' ('''SML''') is a general-purpose modular functional programming language with compile-time type checking and type inference. It is popular among [[compiler]] writers and [[programming language research]]ers, as well as in the development of [[automated theorem proving|theorem provers]].
'''Standard ML''' ('''SML''') is a [[General-purpose programming language|general-purpose]], [[High-level programming language|high-level]], [[Modular programming|modular]], [[Functional programming|functional]] [[programming language]] with compile-time [[type checking]] and [[type inference]]. It is popular for writing [[compiler]]s, for [[programming language research]], and for developing [[automated theorem proving|theorem provers]].


Standard ML is a modern dialect of [[ML (programming language)|ML]], the language used in the [[Logic for Computable Functions]] (LCF) theorem-proving project. It is distinctive among widely used languages in that it has a formal specification, given as [[type rules|typing rules]] and [[operational semantics]] in ''The Definition of Standard ML''.<ref name="revision" />
Standard ML is a modern dialect of [[ML (programming language)|ML]], the language used in the [[Logic for Computable Functions]] (LCF) theorem-proving project. It is distinctive among widely used languages in that it has a [[formal specification]], given as [[typing rule]]s and [[operational semantics]] in ''The Definition of Standard ML''.<ref name="revision"/>


==Language==
==Language==
{{multiple issues|section=y|
Standard ML is a functional programming language with some impure features. Programs written in Standard ML consist of [[expression (programming)|expressions]] as opposed to statements or commands, although some expressions return a trivial "unit" and are only evaluated for their side-effects.
{{How-to|section|date=November 2021}}
{{Unreferenced section|date=November 2021}}
}}

Standard ML is a functional [[programming language]] with some impure features. Programs written in Standard ML consist of [[Expression (computer science)|expressions]] in contrast to statements or commands, although some expressions of type [[Unit type|unit]] are only evaluated for their [[Side effect (computer science)|side-effects]].


===Functions===
===Functions===
Line 98: Line 103:


===Algebraic datatypes===
===Algebraic datatypes===
Standard ML provides strong support for [[algebraic datatype]]s (ADT). A datatype can be thought of as a [[disjoint union]] of tuples (or a "sum of products"). They are easy to define and easy to use, largely because of [[pattern matching]] as well as most Standard ML implementations' [[Partial function|pattern-exhaustiveness]] checking and pattern redundancy checking.
Standard ML provides strong support for [[algebraic datatype]]s (ADT). A [[data type]] can be thought of as a [[disjoint union]] of tuples (or a "sum of products"). They are easy to define and easy to use, largely because of [[pattern matching]], and most Standard ML implementations' [[Partial function|pattern-exhaustiveness]] checking and pattern redundancy checking.


In object-oriented programming languages, a disjoint union can be expressed as [[class hierarchies]]. However, as opposed to class hierarchies, ADTs are [[Closed world assumption|closed]]. Thus the extensibility of ADTs is orthogonal to the extensibility of class hierarchies. Class hierarchies can be extended with new subclasses which implement the same interface, while the functionality of ADTs can be extended for the fixed set of constructors. See [[expression problem]].
In [[object-oriented programming]] languages, a disjoint union can be expressed as [[Class (computer programming)|class]] hierarchies. However, in contrast to [[Class hierarchy|class hierarchies]], ADTs are [[Closed-world assumption|closed]]. Thus, the extensibility of ADTs is orthogonal to the extensibility of class hierarchies. Class hierarchies can be extended with new subclasses which implement the same interface, while the functions of ADTs can be extended for the fixed set of constructors. See [[expression problem]].


A datatype is defined with the keyword {{code|lang=sml|datatype}}, as in:
A datatype is defined with the keyword {{code|lang=sml|datatype}}, as in:
Line 114: Line 119:


===Pattern matching===
===Pattern matching===
Patterns are matched in the order in which they are defined. C programmers can use [[tagged union]]s, dispatching on tag values, to accomplish what ML accomplishes with datatypes and pattern matching. Nevertheless, while a C program decorated with appropriate checks will, in a sense, be as robust as the corresponding ML program, those checks will of necessity be dynamic; ML's [[Static program analysis|static checks]] provide strong guarantees about the correctness of the program at compile time.
Patterns are matched in the order in which they are defined. [[C (programming language)|C]] programmers can use [[tagged union]]s, dispatching on tag values, to do what ML does with datatypes and pattern matching. Nevertheless, while a C program decorated with appropriate checks will, in a sense, be as robust as the corresponding ML program, those checks will of necessity be dynamic; ML's [[Static program analysis|static checks]] provide strong guarantees about the correctness of the program at compile time.


Function arguments can be defined as patterns as follows:
Function arguments can be defined as patterns as follows:
Line 174: Line 179:
<syntaxhighlight lang="sml">fun compose (f, g) = (fn x => f (g x))</syntaxhighlight>
<syntaxhighlight lang="sml">fun compose (f, g) = (fn x => f (g x))</syntaxhighlight>


The function {{code|List.map}} from the basis library is one of the most commonly used higher-order functions in Standard ML:
The function {{code|List.map}} from the basis [[Library (computing)|library]] is one of the most commonly used higher-order functions in Standard ML:
<syntaxhighlight lang="sml">
<syntaxhighlight lang="sml">
fun map _ [] = []
fun map _ [] = []
Line 189: Line 194:


<syntaxhighlight lang="sml">
<syntaxhighlight lang="sml">
local
exception Zero;
exception Zero;

val p = fn (0, _) => raise Zero | (a, b) => a * b
fun prod xs = let
in
fun p (0 :: _) = raise Zero
| p (h :: t) = h * p t
fun prod xs = List.foldl p 1 xs handle Zero => 0
end
| p [] = 1
in
p xs handle Zero => 0
end
</syntaxhighlight>
</syntaxhighlight>


When {{code|lang=sml|exception Zero}} is raised, control leaves the function {{code|p}} altogether. Consider the alternative: the value 0 would be returned to the most recent awaiting frame, it would be multiplied by the local value of {{code|h}}, the resulting value (inevitably 0) would be returned to the next awaiting frame, and so on. The raising of the exception allows control to leapfrog over the entire chain of frames and avoid the associated computation. Note the use of the underscore ({{code|_}}) as a wildcard pattern.
When {{code|lang=sml|exception Zero}} is raised, control leaves the function {{code|lang=sml|List.foldl}} altogether. Consider the alternative: the value 0 would be returned, it would be multiplied by the next integer in the list, the resulting value (inevitably 0) would be returned, and so on. The raising of the exception allows control to skip over the entire chain of frames and avoid the associated computation. Note the use of the underscore ({{code|_}}) as a wildcard pattern.


It has to be noted that the same optimization can be obtained with a [[tail call]].
The same optimization can be obtained with a [[tail call]].


<syntaxhighlight lang="sml">
<syntaxhighlight lang="sml">
Line 218: Line 220:


====Signatures====
====Signatures====
A ''signature'' is an [[Interface (computer science)|interface]], usually thought of as a type for a structure; it specifies the names of all entities provided by the structure as well as the [[arity]] of each type component, the type of each value component, and the signature of each substructure. The definitions of type components are optional; type components whose definitions are hidden are ''abstract types''.
A ''signature'' is an [[Interface (computing)|interface]], usually thought of as a type for a structure; it specifies the names of all entities provided by the structure, the [[arity]] of each type component, the type of each value component, and the signature of each substructure. The definitions of type components are optional; type components whose definitions are hidden are ''abstract types''.


For example, the signature for a [[Queue (data structure)|queue]] may be:
For example, the signature for a [[Queue (data structure)|queue]] may be:
Line 229: Line 231:
val isEmpty : 'a queue -> bool
val isEmpty : 'a queue -> bool
val singleton : 'a -> 'a queue
val singleton : 'a -> 'a queue
val fromList : 'a list -> 'a queue
val insert : 'a * 'a queue -> 'a queue
val insert : 'a * 'a queue -> 'a queue
val peek : 'a queue -> 'a
val peek : 'a queue -> 'a
Line 235: Line 238:
</syntaxhighlight>
</syntaxhighlight>


This signature describes a module that provides a polymorphic type {{code|lang=sml|'a queue}}, {{code|lang=sml|exception QueueError}}, and values that define the basic operations on queues.
This signature describes a module that provides a polymorphic type {{code|lang=sml|'a queue}}, {{code|lang=sml|exception QueueError}}, and values that define basic operations on queues.


====Structures====
====Structures====
Line 250: Line 253:
val empty = ([], [])
val empty = ([], [])


fun isEmpty q = (q = empty)
fun isEmpty ([], []) = true
| isEmpty _ = false


fun singleton a = ([], [a])
fun singleton a = ([], [a])

fun fromList a = ([], a)


fun insert (a, ([], [])) = singleton a
fun insert (a, ([], [])) = singleton a
Line 258: Line 264:


fun peek (_, []) = raise QueueError
fun peek (_, []) = raise QueueError
| peek (ins, a :: outs) = a
| peek (ins, outs) = List.hd outs


fun remove (_, []) = raise QueueError
fun remove (_, []) = raise QueueError
Line 276: Line 282:


====Functors====
====Functors====
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 programming|generic]] data structures and algorithms.
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 programming|generic]] data structures and algorithms.


One popular algorithm<ref name="bfs" /> for [[breadth-first search]] of trees makes use of queues. Here we present a version of that algorithm parameterized over an abstract queue structure:
One popular algorithm<ref name="bfs"/> for [[breadth-first search]] of trees makes use of queues. Here is a version of that algorithm parameterized over an abstract queue structure:


<syntaxhighlight lang="sml">
<syntaxhighlight lang="sml">
Line 298: Line 304:
</syntaxhighlight>
</syntaxhighlight>


Note that within {{code|lang=sml|functor BFS}}, the representation of the queue is not visible. More concretely, there is no way to select the first list in the two-list queue, if that is indeed the representation being used. This [[data abstraction]] mechanism makes the breadth-first search truly agnostic to the queue's implementation. This is in general desirable; in this case, the queue structure can safely maintain any logical invariants on which its correctness depends behind the bulletproof wall of abstraction.
Within {{code|lang=sml|functor BFS}}, the representation of the queue is not visible. More concretely, there is no way to select the first list in the two-list queue, if that is indeed the representation being used. This [[data abstraction]] mechanism makes the breadth-first search truly agnostic to the queue's implementation. This is in general desirable; in this case, the queue structure can safely maintain any logical invariants on which its correctness depends behind the bulletproof wall of abstraction.


==Code examples==
==Code examples==
Line 305: Line 311:
Snippets of SML code are most easily studied by entering them into an [[Read–eval–print loop|interactive top-level]].
Snippets of SML code are most easily studied by entering them into an [[Read–eval–print loop|interactive top-level]].


===Hello world===
===Hello, world!===
The following is a [[Hello, world!]] program:
The following is a [["Hello, World!" program]]:


{| class="wikitable"
{| class="wikitable"
Line 314: Line 320:
|
|
<syntaxhighlight lang="sml" line>
<syntaxhighlight lang="sml" line>
print "Hello, world!\n"
print "Hello, world!\n";
</syntaxhighlight>
</syntaxhighlight>
|-
|-
Line 343: Line 349:


'''Split'''
'''Split'''

{{code|lang=sml|fun split}} is implemented with a [[State (computer science)|stateful]] closure which alternates between {{code|true}} and {{code|false}}, ignoring the input:
{{code|lang=sml|fun split}} is implemented with a [[State (computer science)|stateful]] closure which alternates between {{code|true}} and {{code|false}}, ignoring the input:


Line 357: Line 364:


'''Merge'''
'''Merge'''

Merge uses a local function loop for efficiency. The inner {{code|loop}} is defined in terms of cases: when both lists are non-empty ({{code|lang=sml|x :: xs}}) and when one list is empty ({{code|lang=sml|[]}}).
Merge uses a local function loop for efficiency. The inner {{code|loop}} is defined in terms of cases: when both lists are non-empty ({{code|lang=sml|x :: xs}}) and when one list is empty ({{code|lang=sml|[]}}).


This function merges two sorted lists into one sorted list. Note how the accumulator {{code|acc}} is built backwards, then reversed before being returned. This is a common technique, since {{code|lang=sml|'a list}} is represented as a [[Linked list#Linked lists vs. dynamic_arrays|linked list]]; while this technique requires more clock time, the [[Asymptotic analysis#Applications|asymptotics]] are not worse, unlike [[Bogosort#Standard ML|bogosort]].
This function merges two sorted lists into one sorted list. Note how the accumulator {{code|acc}} is built backwards, then reversed before being returned. This is a common technique, since {{code|lang=sml|'a list}} is represented as a [[Linked list#Linked lists vs. dynamic_arrays|linked list]]; this technique requires more clock time, but the [[Asymptotic analysis#Applications|asymptotics]] are not worse.


<syntaxhighlight lang="sml">
<syntaxhighlight lang="sml">
Line 379: Line 387:


'''Mergesort'''
'''Mergesort'''

The main function:
The main function:


Line 416: Line 425:
exception TyErr;
exception TyErr;


datatype ty
datatype ty = IntTy | BoolTy
= IntTy
| BoolTy


fun unify (IntTy, IntTy) = IntTy
fun unify (IntTy, IntTy) = IntTy
Line 432: Line 439:
| If of exp * exp * exp
| If of exp * exp * exp


fun typeOf True = BoolTy
fun infer True = BoolTy
| typeOf False = BoolTy
| infer False = BoolTy
| typeOf (Int _) = IntTy
| infer (Int _) = IntTy
| typeOf (Not e) = (assert e BoolTy; BoolTy)
| infer (Not e) = (assert e BoolTy; BoolTy)
| typeOf (Add (a, b)) = (assert a IntTy; assert b IntTy; IntTy)
| infer (Add (a, b)) = (assert a IntTy; assert b IntTy; IntTy)
| typeOf (If (e, t, f)) = (assert e BoolTy; unify (typeOf t, typeOf f))
| infer (If (e, t, f)) = (assert e BoolTy; unify (infer t, infer f))
and assert e t = unify (typeOf e, t)
and assert e t = unify (infer e, t)


fun eval True = True
fun eval True = True
Line 447: Line 454:
| eval (If (e, t, f)) = eval (if eval e = True then t else f)
| eval (If (e, t, f)) = eval (if eval e = True then t else f)


fun run e = (typeOf e; SOME (eval e)) handle TyErr => NONE
fun run e = (infer e; SOME (eval e)) handle TyErr => NONE
</syntaxhighlight>
</syntaxhighlight>


Line 469: Line 476:
fun fact n : IntInf.int = if n = 0 then 1 else n * fact (n - 1);
fun fact n : IntInf.int = if n = 0 then 1 else n * fact (n - 1);


fun printLine str = let in
fun printLine str = TextIO.output (TextIO.stdOut, str ^ "\n");
TextIO.output (TextIO.stdOut, str);
TextIO.output (TextIO.stdOut, "\n")
end;


val () = printLine (IntInf.toString (fact 120));
val () = printLine (IntInf.toString (fact 120));
Line 490: Line 494:


===Partial application===
===Partial application===
Curried functions have a great many applications, such as eliminating redundant code. For example, a module may require functions of type {{code|lang=sml|a -> b}}, but it is more convenient to write functions of type {{code|lang=sml|a * c -> b}} where there is a fixed relationship between the objects of type {{code|a}} and {{code|c}}. A function of type {{code|lang=sml|c -> (a * c -> b) -> a -> b}} can factor out this commonality. This is an example of the [[adapter pattern]].{{Citation needed|date=May 2015}}
Curried functions have many applications, such as eliminating redundant code. For example, a module may require functions of type {{code|lang=sml|a -> b}}, but it is more convenient to write functions of type {{code|lang=sml|a * c -> b}} where there is a fixed relationship between the objects of type {{code|a}} and {{code|c}}. A function of type {{code|lang=sml|c -> (a * c -> b) -> a -> b}} can factor out this commonality. This is an example of the [[adapter pattern]].{{Citation needed|date=May 2015}}


In this example, {{code|lang=sml|fun d}} computes the numerical derivative of a given function {{code|f}} at point {{code|x}}:
In this example, {{code|lang=sml|fun d}} computes the numerical derivative of a given function {{code|f}} at point {{code|x}}:
Line 506: Line 510:
</syntaxhighlight>
</syntaxhighlight>


Note that the inferred type indicates that {{code|d'}} expects a function with the type {{code|lang=sml|real -> real}} as its first argument. We can compute an approximation to the derivative of <math>f(x) = x^3-x-1</math> at <math>x=3</math>. The correct answer is <math>f'(3) = 27-1 = 26</math>.
The inferred type indicates that {{code|d'}} expects a function with the type {{code|lang=sml|real -> real}} as its first argument. We can compute an approximation to the derivative of <math>f(x) = x^3-x-1</math> at <math>x=3</math>. The correct answer is <math>f'(3) = 27-1 = 26</math>.


<syntaxhighlight lang="sml" highlight="1">
<syntaxhighlight lang="sml" highlight="1">
Line 515: Line 519:
==Libraries==
==Libraries==
===Standard===
===Standard===
The basis library has been standardized and ships with most implementations. It provides modules for trees, arrays and other data structures as well as input/output and system interfaces.
The Basis Library<ref>{{Cite web|title=Standard ML Basis Library|url=https://smlfamily.github.io/Basis/index.html|access-date=2022-01-10|website=smlfamily.github.io}}</ref> has been standardized and ships with most implementations. It provides modules for trees, arrays, and other data structures, and [[input/output]] and system interfaces.


===Third party===
===Third party===
For numerical computing, a Matrix module exists (but is currently broken), https://www.cs.cmu.edu/afs/cs/project/pscico/pscico/src/matrix/README.html.
For [[Numerical analysis|numerical computing]], a Matrix module exists (but is currently broken), https://www.cs.cmu.edu/afs/cs/project/pscico/pscico/src/matrix/README.html.


For graphics, cairo-sml is an open source interface to the [[Cairo (graphics)|Cairo]] graphics library. For machine learning, a library for graphical models exists.
For graphics, cairo-sml is an open source interface to the [[Cairo (graphics)|Cairo]] graphics library. For machine learning, a library for graphical models exists.
Line 528: Line 532:
* [http://www.mpi-sws.org/~rossberg/hamlet/ HaMLet]: a Standard ML interpreter that aims to be an accurate and accessible reference implementation of the standard
* [http://www.mpi-sws.org/~rossberg/hamlet/ HaMLet]: a Standard ML interpreter that aims to be an accurate and accessible reference implementation of the standard
* [[MLton]] ([http://www.mlton.org mlton.org]): a [[Interprocedural optimization|whole-program optimizing]] compiler which strictly conforms to the Definition and produces very fast code compared to other ML implementations, including [[Compiler#Back end|backends]] for [[LLVM]] and C
* [[MLton]] ([http://www.mlton.org mlton.org]): a [[Interprocedural optimization|whole-program optimizing]] compiler which strictly conforms to the Definition and produces very fast code compared to other ML implementations, including [[Compiler#Back end|backends]] for [[LLVM]] and C
* [http://www.itu.dk/people/sestoft/mosml.html Moscow ML]: a light-weight implementation, based on the [[Caml|CAML Light]] runtime engine which implements the full Standard ML language, including modules and much of the basis library
* [http://www.itu.dk/people/sestoft/mosml.html Moscow ML]: a light-weight implementation, based on the [[Caml]] Light runtime engine which implements the full Standard ML language, including modules and much of the basis library
* [http://www.polyml.org/ Poly/ML]: a full implementation of Standard ML that produces fast code and supports multicore hardware (via Posix threads); its runtime system performs parallel garbage collection and online sharing of immutable substructures.
* [http://www.polyml.org/ Poly/ML]: a full implementation of Standard ML that produces fast code and supports multicore hardware (via Portable Operating System Interface ([[POSIX]]) threads); its runtime system performs parallel [[Garbage collection (computer science)|garbage collection]] and online sharing of immutable substructures.
* [[Standard ML of New Jersey]] ([http://www.smlnj.org/ smlnj.org]): a full compiler, with associated libraries, tools, an interactive shell, and documentation with support for [[Concurrent ML]]
* [[Standard ML of New Jersey]] ([http://www.smlnj.org/ smlnj.org]): a full compiler, with associated libraries, tools, an interactive shell, and documentation with support for [[Concurrent ML]]
* [http://www.cl.cam.ac.uk/Research/TSG/SMLNET/ SML.NET]: a Standard ML compiler for the [[common language runtime]] with extensions for linking with other [[.NET Framework|.NET]] code
* [http://www.cl.cam.ac.uk/Research/TSG/SMLNET/ SML.NET]: a Standard ML compiler for the [[Common Language Runtime]] with extensions for linking with other [[.NET]] framework code
* [http://www.elsman.com/mlkit/ ML Kit]: an implementation based very closely on the Definition, integrating a garbage collector (which can be disabled) and [[region-based memory management]] with automatic inference of regions, aiming to support real-time applications
* [http://www.elsman.com/mlkit/ ML Kit] {{Webarchive|url=https://web.archive.org/web/20160107005413/http://www.elsman.com/mlkit/ |date=2016-01-07}}: an implementation based very closely on the Definition, integrating a garbage collector (which can be disabled) and [[region-based memory management]] with automatic inference of regions, aiming to support real-time applications


'''Derivative'''
'''Derivative'''
Line 541: Line 545:
'''Research'''
'''Research'''
* [https://cakeml.org/ CakeML] is a REPL version of ML with formally verified runtime and translation to assembler.
* [https://cakeml.org/ CakeML] is a REPL version of ML with formally verified runtime and translation to assembler.
* [http://isabelle.in.tum.de Isabelle/ML] integrates parallel Poly/ML into an interactive theorem prover, with a sophisticated IDE (based on [[jEdit]]) for official Standard ML (SML'97), the Isabelle/ML dialect, and the proof language. Starting with Isabelle2016, there is also a source-level debugger for ML.
* [[Isabelle (proof assistant)|Isabelle]] ([http://isabelle.in.tum.de Isabelle/ML] {{Webarchive|url=https://web.archive.org/web/20200830080049/http://isabelle.in.tum.de/ |date=2020-08-30 }}) integrates parallel Poly/ML into an interactive theorem prover, with a sophisticated IDE (based on [[jEdit]]) for official Standard ML (SML'97), the Isabelle/ML dialect, and the proof language. Starting with Isabelle2016, there is also a source-level debugger for ML.
* [[Poplog]] implements a version of Standard ML, along with [[Common Lisp]] and [[Prolog]], allowing mixed language programming; all are implemented in [[POP-11]], which is compiled incrementally.
* [[Poplog]] implements a version of Standard ML, along with [[Common Lisp]] and [[Prolog]], allowing mixed language programming; all are implemented in [[POP-11]], which is [[Incremental compiler|compiled incrementally]].
* [http://www.cs.cornell.edu/home/jgm/tilt.html TILT] is a full certifying compiler for Standard ML which uses typed intermediate languages to optimize code and ensure correctness, and can compile to [[typed assembly language]].
* [http://www.cs.cornell.edu/home/jgm/tilt.html TILT] is a full certifying compiler for Standard ML which uses typed [[Intermediate representation|intermediate languages]] to [[Program optimization|optimize]] code and ensure correctness, and can compile to [[typed assembly language]].


All of these implementations are [[Open-source license|open-source]] and freely available. Most are implemented themselves in Standard ML. There are no longer any commercial implementations; [[Harlequin (software company)|Harlequin]] once produced a commercial IDE and compiler called [[MLWorks]]. The company is now defunct. MLWorks passed on to [[Xanalys]] and was later acquired by [[Ravenbrook Limited]] on April 26, 2013 and open sourced.
All of these implementations are [[Open-source license|open-source]] and freely available. Most are implemented themselves in Standard ML. There are no longer any commercial implementations; [[Harlequin (software company)|Harlequin]], now defunct, once produced a commercial IDE and compiler called MLWorks which passed on to [[Xanalys]] and was later open-sourced after it was acquired by Ravenbrook Limited on April 26, 2013.


==Major projects using SML==
==Major projects using SML==
The [[IT University of Copenhagen]]'s entire [[enterprise architecture]] is implemented in around 100,000 lines of SML, including staff records, payroll, course administration and feedback, student project management, and web-based self-service interfaces.<ref name="sml" />
The [[IT University of Copenhagen]]'s entire [[enterprise architecture]] is implemented in around 100,000 lines of SML, including staff records, payroll, course administration and feedback, student project management, and web-based self-service interfaces.<ref name="sml"/>


The proof assistants [[HOL (proof assistant)|HOL4]], [[Isabelle (proof assistant)|Isabelle]], [[LEGO (proof assistant)|LEGO]], and [[Twelf]] are written in Standard ML, and it is also widely used by [[Compiler#Compiler construction|compiler writers]] and [[integrated circuit design]]ers such as [[ARM architecture|ARM]].{{Citation needed|date=December 2017}}
The [[proof assistant]]s [[HOL (proof assistant)|HOL4]], [[Isabelle (proof assistant)|Isabelle]], [[LEGO (proof assistant)|LEGO]], and [[Twelf]] are written in Standard ML. It is also used by [[Compiler#Compiler construction|compiler writers]] and [[integrated circuit design]]ers such as [[ARM architecture|ARM]].<ref name="machine code verification"/>


==See also==
==See also==
Line 556: Line 560:


==References==
==References==
{{refs|refs=
{{refs|
<ref name="intro">{{cite web
<ref name="intro">{{cite web
| title = Programming in Standard ML: Hierarchies and Parameterization
|title=Programming in Standard ML: Hierarchies and Parameterization
| url = https://www.cs.cmu.edu/~rwh/introsml/modules/subfun.htm
|url=https://www.cs.cmu.edu/~rwh/introsml/modules/subfun.htm
| access-date = 2020-02-22
|access-date=2020-02-22
}}</ref>
}}</ref>


<ref name="smlnj">{{cite web
<ref name="sml">{{cite journal
|last=Tofte |first=Mads
| url = http://www.smlnj.org/sml97.html
|year=2009
| title = SML '97
|title=Standard ML language
| website = www.smlnj.org
|journal=Scholarpedia
|volume=4
|issue=2
|page=7515
|doi=10.4249/scholarpedia.7515
|bibcode=2009SchpJ...4.7515T
|doi-access=free
}}</ref>
}}</ref>


<ref name="itertools">{{cite web
<ref name="smlnj">{{cite web
|title=SML '97
| url = https://docs.python.org/3/library/itertools.html
|url=http://www.smlnj.org/sml97.html
| title = itertools — Functions creating iterators for efficient looping — Python 3.7.1rc1 documentation
| website = docs.python.org
|website=www.smlnj.org
}}</ref>
}}</ref>


<ref name="revision">{{cite book
<ref name="revision">{{cite book
| last1 = Milner
|last1=Milner |first1=Robin |author1-link=Robin Milner
|last2=Tofte |first2=Mads
| first1 = Robin
|last3=Harper |first3=Robert
| last2 = Tofte
|last4=MacQueen |first4=David
| first2 = Mads
|year=1997
| last3 = Harper
|title=The Definition of Standard ML (Revised)
| first3 = Robert
|publisher=MIT Press
| last4 = MacQueen
|isbn=0-262-63181-4
| first4 = David
}}</ref>
| title = The Definition of Standard ML (Revised)

| publisher = MIT Press
<ref name="itertools">{{cite web
| year = 1997
|title=itertools — Functions creating iterators for efficient looping — Python 3.7.1rc1 documentation
| isbn = 0-262-63181-4
|url=https://docs.python.org/3/library/itertools.html
|website=docs.python.org
}}</ref>
}}</ref>


<ref name="bfs">{{cite conference
<ref name="bfs">{{cite conference
| last = Okasaki
|last=Okasaki |first=Chris
|year=2000
| first = Chris
| title = Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design
|title=Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design
| book-title = International Conference on Functional Programming 2000
|book-title=International Conference on Functional Programming 2000
| publisher = ACM
|publisher=ACM
| year = 2000
}}</ref>
}}</ref>


<ref name="sml">{{cite web
<ref name="machine code verification">{{cite conference
|last1=Alglave |first1=Jade |author1-link=Jade Alglave
| last1 = Tofte
|last2=Fox |first2=Anthony C. J.
| first1 = Mads
|last3=Ishtiaq |first3=Samin
| title = Standard ML language
|last4=Myreen |first4=Magnus O.
| url = http://www.scholarpedia.org/article/Standard_ML_language
|last5=Sarkar |first5=Susmit
| website = Scholarpedia
|last6=Sewell |first6=Peter
| access-date = 2020-01-08
|last7=Nardelli |first7=Francesco Zappa
|date=2009
|title=The Semantics of Power and ARM Multiprocessor Machine Code
|conference=DAMP 2009
|pages=13–24
|url=http://www0.cs.ucl.ac.uk/staff/j.alglave/papers/damp09.pdf |archive-url=https://web.archive.org/web/20170814015026/http://www0.cs.ucl.ac.uk/staff/j.alglave/papers/damp09.pdf |archive-date=2017-08-14 |url-status=live
}}</ref>
}}</ref>
}}
}}
Line 611: Line 629:
==External links==
==External links==
'''About Standard ML'''
'''About Standard ML'''
* [https://smlfamily.github.io/sml97-defn.pdf Revised definition]
* [http://sml-family.org Standard ML Family GitHub Project]
* [http://sml-family.org Standard ML Family GitHub Project] {{Webarchive|url=https://web.archive.org/web/20200220023435/http://sml-family.org/ |date=2020-02-20}}
* [http://www.smlnj.org/sml.html What is SML?]
* [http://www.smlnj.org/sml.html What is SML?]
* [http://www.smlnj.org/sml97.html What is SML '97?]
* [http://www.smlnj.org/sml97.html What is SML '97?]

* [http://www.successor-ml.org successor ML (sML)]: evolution of ML using Standard ML as a starting point
'''About successor ML'''
* [https://github.com/SMLFamily/Successor-ML successor ML (sML)]: evolution of ML using Standard ML as a starting point
* [https://github.com/rossberg/hamlet/tree/succ HaMLet on GitHub]: reference implementation for successor ML


'''Practical'''
'''Practical'''
Line 621: Line 643:


'''Academic'''
'''Academic'''
* [http://scholarpedia.org/article/Standard_ML_language Standard ML] Mads Tofte, [[Scholarpedia]], 4(2):7515. [[doi:10.4249/scholarpedia.7515]]
* [https://www.cs.cmu.edu/~rwh/isml/book.pdf Programming in Standard ML]
* [https://www.cs.cmu.edu/~rwh/isml/book.pdf Programming in Standard ML]
* [http://www.dcs.ed.ac.uk/home/stg/NOTES/notes.html Programming in Standard ML '97: An Online Tutorial]
* [http://www.dcs.ed.ac.uk/home/stg/NOTES/notes.html Programming in Standard ML '97: An Online Tutorial]
* [http://www.classes.cs.uchicago.edu/archive/2007/winter/22610-1/docs/sml-tutorial.pdf Univ. of Chicago - SML tutorial (slides)]
* [http://courses.cs.washington.edu/courses/cse341/13sp/ CSE341: Programming Languages], Dan Grossman, University of Washington. Also on [https://www.coursera.org/learn/programming-languages Coursera] and [https://www.youtube.com/playlist?list=PL-eVNDa9MNJczU4ZjhJDT8rIcCa12DyAx YouTube]


{{ML programming}}
{{Programming languages}}
{{Programming languages}}
{{Authority control}}
{{Authority control}}


[[Category:ML programming language family]]
[[Category:High-level programming languages]]
[[Category:Functional languages]]
[[Category:Functional languages]]
[[Category:Procedural programming languages]]
[[Category:Procedural programming languages]]
[[Category:Programming languages created in 1990]]
[[Category:ML programming language family]]
[[Category:Programming languages created in 1983]]

Latest revision as of 02:01, 13 June 2024

Standard ML
ParadigmMulti-paradigm: functional, imperative, modular[1]
FamilyML
First appeared1983; 41 years ago (1983)[2]
Stable release
Standard ML '97[2] / 1997; 27 years ago (1997)
Typing disciplineInferred, static, strong
Filename extensions.sml
Websitesmlfamily.github.io
Major implementations
SML/NJ, MLton, Poly/ML
Dialects
Alice, Concurrent ML, Dependent ML
Influenced by
ML, Hope, Pascal
Influenced
Elm, F#, F*, Haskell, OCaml, Python,[3] Rust,[4] Scala

Standard ML (SML) is a general-purpose, high-level, modular, functional programming language with compile-time type checking and type inference. It is popular for writing compilers, for programming language research, and for developing theorem provers.

Standard ML is a modern dialect of ML, the language used in the Logic for Computable Functions (LCF) theorem-proving 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.[5]

Language

[edit]

Standard ML is a functional programming language with some impure features. Programs written in Standard ML consist of expressions in contrast to statements or commands, although some expressions of type unit are only evaluated for their side-effects.

Functions

[edit]

Like all functional languages, a key feature of Standard ML is the function, which is used for abstraction. The factorial function can be expressed as follows:

fun factorial n = 
    if n = 0 then 1 else n * factorial (n - 1)

Type inference

[edit]

An SML compiler must infer the static type val factorial : int -> int without user-supplied type annotations. It has to deduce that n is only used with integer expressions, and must therefore itself be an integer, and that all terminal expressions are integer expressions.

Declarative definitions

[edit]

The same function can be expressed with clausal function definitions where the if-then-else conditional is replaced with templates of the factorial function evaluated for specific values:

fun factorial 0 = 1
  | factorial n = n * factorial (n - 1)

Imperative definitions

[edit]

or iteratively:

fun factorial n = let val i = ref n and acc = ref 1 in
    while !i > 0 do (acc := !acc * !i; i := !i - 1); !acc
end

Lambda functions

[edit]

or as a lambda function:

val rec factorial = fn 0 => 1 | n => n * factorial (n - 1)

Here, the keyword val introduces a binding of an identifier to a value, fn introduces an anonymous function, and rec allows the definition to be self-referential.

Local definitions

[edit]

The encapsulation of an invariant-preserving tail-recursive tight loop with one or more accumulator parameters within an invariant-free outer function, as seen here, is a common idiom in Standard ML.

Using a local function, it can be rewritten in a more efficient tail-recursive style:

local
    fun loop (0, acc) = acc
      | loop (m, acc) = loop (m - 1, m * acc)
in
    fun factorial n = loop (n, 1)
end

Type synonyms

[edit]

A type synonym is defined with the keyword type. Here is a type synonym for points on a plane, and functions computing the distances between two points, and the area of a triangle with the given corners as per Heron's formula. (These definitions will be used in subsequent examples).

type loc = real * real

fun square (x : real) = x * x

fun dist (x, y) (x', y') =
    Math.sqrt (square (x' - x) + square (y' - y))

fun heron (a, b, c) = let
    val x = dist a b
    val y = dist b c
    val z = dist a c
    val s = (x + y + z) / 2.0
    in
        Math.sqrt (s * (s - x) * (s - y) * (s - z))
    end

Algebraic datatypes

[edit]

Standard ML provides strong support for algebraic datatypes (ADT). A data type can be thought of as a disjoint union of tuples (or a "sum of products"). They are easy to define and easy to use, largely because of pattern matching, and most Standard ML implementations' pattern-exhaustiveness checking and pattern redundancy checking.

In object-oriented programming languages, a disjoint union can be expressed as class hierarchies. However, in contrast to class hierarchies, ADTs are closed. Thus, the extensibility of ADTs is orthogonal to the extensibility of class hierarchies. Class hierarchies can be extended with new subclasses which implement the same interface, while the functions of ADTs can be extended for the fixed set of constructors. See expression problem.

A datatype is defined with the keyword datatype, as in:

datatype shape
    = Circle   of loc * real      (* center and radius *)
    | Square   of loc * real      (* upper-left corner and side length; axis-aligned *)
    | Triangle of loc * loc * loc (* corners *)

Note that a type synonym cannot be recursive; datatypes are necessary to define recursive constructors. (This is not at issue in this example.)

Pattern matching

[edit]

Patterns are matched in the order in which they are defined. C programmers can use tagged unions, dispatching on tag values, to do what ML does with datatypes and pattern matching. Nevertheless, while a C program decorated with appropriate checks will, in a sense, be as robust as the corresponding ML program, those checks will of necessity be dynamic; ML's static checks provide strong guarantees about the correctness of the program at compile time.

Function arguments can be defined as patterns as follows:

fun area (Circle (_, r)) = Math.pi * square r
  | area (Square (_, s)) = square s
  | area (Triangle p) = heron p (* see above *)

The so-called "clausal form" of function definition, where arguments are defined as patterns, is merely syntactic sugar for a case expression:

fun area shape = case shape of
    Circle (_, r) => Math.pi * square r
  | Square (_, s) => square s
  | Triangle p => heron p

Exhaustiveness checking

[edit]

Pattern-exhaustiveness checking will make sure that each constructor of the datatype is matched by at least one pattern.

The following pattern is not exhaustive:

fun center (Circle (c, _)) = c
  | center (Square ((x, y), s)) = (x + s / 2.0, y + s / 2.0)

There is no pattern for the Triangle case in the center function. The compiler will issue a warning that the case expression is not exhaustive, and if a Triangle is passed to this function at runtime, exception Match will be raised.

Redundancy checking

[edit]

The pattern in the second clause of the following (meaningless) function is redundant:

fun f (Circle ((x, y), r)) = x + y
  | f (Circle _) = 1.0
  | f _ = 0.0

Any value that would match the pattern in the second clause would also match the pattern in the first clause, so the second clause is unreachable. Therefore, this definition as a whole exhibits redundancy, and causes a compile-time warning.

The following function definition is exhaustive and not redundant:

val hasCorners = fn (Circle _) => false | _ => true

If control gets past the first pattern (Circle), we know the shape must be either a Square or a Triangle. In either of those cases, we know the shape has corners, so we can return true without discerning the actual shape.

Higher-order functions

[edit]

Functions can consume functions as arguments:

fun map f (x, y) = (f x, f y)

Functions can produce functions as return values:

fun constant k = (fn _ => k)

Functions can also both consume and produce functions:

fun compose (f, g) = (fn x => f (g x))

The function List.map from the basis library is one of the most commonly used higher-order functions in Standard ML:

fun map _ [] = []
  | map f (x :: xs) = f x :: map f xs

A more efficient implementation with tail-recursive List.foldl:

fun map f = List.rev o List.foldl (fn (x, acc) => f x :: acc) []

Exceptions

[edit]

Exceptions are raised with the keyword raise and handled with the pattern matching handle construct. The exception system can implement non-local exit; this optimization technique is suitable for functions like the following.

local
    exception Zero;
    val p = fn (0, _) => raise Zero | (a, b) => a * b
in
    fun prod xs = List.foldl p 1 xs handle Zero => 0
end

When exception Zero is raised, control leaves the function List.foldl altogether. Consider the alternative: the value 0 would be returned, it would be multiplied by the next integer in the list, the resulting value (inevitably 0) would be returned, and so on. The raising of the exception allows control to skip over the entire chain of frames and avoid the associated computation. Note the use of the underscore (_) as a wildcard pattern.

The same optimization can be obtained with a tail call.

local
    fun p a (0 :: _) = 0
      | p a (x :: xs) = p (a * x) xs
      | p a [] = a
in
    val prod = p 1
end

Module system

[edit]

Standard ML's advanced module system allows programs to be decomposed into hierarchically organized structures of logically related type and value definitions. Modules provide not only namespace control but also abstraction, in the sense that they allow the definition of abstract data types. Three main syntactic constructs comprise the module system: signatures, structures and functors.

Signatures

[edit]

A signature is an interface, usually thought of as a type for a structure; it specifies the names of all entities provided by the structure, the arity of each type component, the type of each value component, and the signature of each substructure. The definitions of type components are optional; type components whose definitions are hidden are abstract types.

For example, the signature for a queue may be:

signature QUEUE = sig
    type 'a queue
    exception QueueError;
    val empty     : 'a queue
    val isEmpty   : 'a queue -> bool
    val singleton : 'a -> 'a queue
    val fromList  : 'a list -> 'a queue
    val insert    : 'a * 'a queue -> 'a queue
    val peek      : 'a queue -> 'a
    val remove    : 'a queue -> 'a * 'a queue
end

This signature describes a module that provides a polymorphic type 'a queue, exception QueueError, and values that define basic operations on queues.

Structures

[edit]

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 queue structure can be implemented as follows:

structure TwoListQueue :> QUEUE = struct
    type 'a queue = 'a list * 'a list

    exception QueueError;

    val empty = ([], [])

    fun isEmpty ([], []) = true
      | isEmpty _ = false

    fun singleton a = ([], [a])

    fun fromList a = ([], a)

    fun insert (a, ([], [])) = singleton a
      | insert (a, (ins, outs)) = (a :: ins, outs)

    fun peek (_, []) = raise QueueError
      | peek (ins, outs) = List.hd outs

    fun remove (_, []) = raise QueueError
      | remove (ins, [a]) = (a, ([], List.rev ins))
      | remove (ins, a :: outs) = (a, (ins, outs))
end

This definition declares that structure TwoListQueue implements signature QUEUE. Furthermore, the opaque ascription denoted by :> states that any types which are not defined in the signature (i.e. type 'a queue) should be abstract, meaning that the definition of a queue as a pair of lists is not visible outside the module. The structure implements all of the definitions in the signature.

The types and values in a structure can be accessed with "dot notation":

val q : string TwoListQueue.queue = TwoListQueue.empty
val q' = TwoListQueue.insert (Real.toString Math.pi, q)

Functors

[edit]

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.

One popular algorithm[6] for breadth-first search of trees makes use of queues. Here is a version of that algorithm parameterized over an abstract queue structure:

(* after Okasaki, ICFP, 2000 *)
functor BFS (Q: QUEUE) = struct
  datatype 'a tree = E | T of 'a * 'a tree * 'a tree

  local
    fun bfsQ q = if Q.isEmpty q then [] else search (Q.remove q)
    and search (E, q) = bfsQ q
      | search (T (x, l, r), q) = x :: bfsQ (insert (insert q l) r)
    and insert q a = Q.insert (a, q)
  in
    fun bfs t = bfsQ (Q.singleton t)
  end
end

structure QueueBFS = BFS (TwoListQueue)

Within functor BFS, the representation of the queue is not visible. More concretely, there is no way to select the first list in the two-list queue, if that is indeed the representation being used. This data abstraction mechanism makes the breadth-first search truly agnostic to the queue's implementation. This is in general desirable; in this case, the queue structure can safely maintain any logical invariants on which its correctness depends behind the bulletproof wall of abstraction.

Code examples

[edit]

Snippets of SML code are most easily studied by entering them into an interactive top-level.

Hello, world!

[edit]

The following is a "Hello, World!" program:

hello.sml
print "Hello, world!\n";
bash
$ mlton hello.sml
$ ./hello
Hello, world!

Algorithms

[edit]

Insertion sort

[edit]

Insertion sort for int list (ascending) can be expressed concisely as follows:

fun insert (x, []) = [x] | insert (x, h :: t) = sort x (h, t)
and sort x (h, t) = if x < h then [x, h] @ t else h :: insert (x, t)
val insertionsort = List.foldl insert []

Mergesort

[edit]

Here, the classic mergesort algorithm is implemented in three functions: split, merge and mergesort. Also note the absence of types, with the exception of the syntax op :: and [] which signify lists. This code will sort lists of any type, so long as a consistent ordering function cmp is defined. Using Hindley–Milner type inference, the types of all variables can be inferred, even complicated types such as that of the function cmp.

Split

fun split is implemented with a stateful closure which alternates between true and false, ignoring the input:

fun alternator {} = let val state = ref true
    in fn a => !state before state := not (!state) end

(* Split a list into near-halves which will either be the same length,
 * or the first will have one more element than the other.
 * Runs in O(n) time, where n = |xs|.
 *)
fun split xs = List.partition (alternator {}) xs

Merge

Merge uses a local function loop for efficiency. The inner loop is defined in terms of cases: when both lists are non-empty (x :: xs) and when one list is empty ([]).

This function merges two sorted lists into one sorted list. Note how the accumulator acc is built backwards, then reversed before being returned. This is a common technique, since 'a list is represented as a linked list; this technique requires more clock time, but the asymptotics are not worse.

(* Merge two ordered lists using the order cmp.
 * Pre: each list must already be ordered per cmp.
 * Runs in O(n) time, where n = |xs| + |ys|.
 *)
fun merge cmp (xs, []) = xs
  | merge cmp (xs, y :: ys) = let
    fun loop (a, acc) (xs, []) = List.revAppend (a :: acc, xs)
      | loop (a, acc) (xs, y :: ys) =
        if cmp (a, y)
        then loop (y, a :: acc) (ys, xs)
        else loop (a, y :: acc) (xs, ys)
    in
        loop (y, []) (ys, xs)
    end

Mergesort

The main function:

fun ap f (x, y) = (f x, f y)

(* Sort a list in according to the given ordering operation cmp.
 * Runs in O(n log n) time, where n = |xs|.
 *)
fun mergesort cmp [] = []
  | mergesort cmp [x] = [x]
  | mergesort cmp xs = (merge cmp o ap (mergesort cmp) o split) xs

Quicksort

[edit]

Quicksort can be expressed as follows. fun part is a closure that consumes an order operator op <<.

infix <<

fun quicksort (op <<) = let
    fun part p = List.partition (fn x => x << p)
    fun sort [] = []
      | sort (p :: xs) = join p (part p xs)
    and join p (l, r) = sort l @ p :: sort r
    in
        sort
    end

Expression interpreter

[edit]

Note the relative ease with which a small expression language can be defined and processed:

exception TyErr;

datatype ty = IntTy | BoolTy

fun unify (IntTy, IntTy) = IntTy
  | unify (BoolTy, BoolTy) = BoolTy
  | unify (_, _) = raise TyErr

datatype exp
    = True
    | False
    | Int of int
    | Not of exp
    | Add of exp * exp
    | If  of exp * exp * exp

fun infer True = BoolTy
  | infer False = BoolTy
  | infer (Int _) = IntTy
  | infer (Not e) = (assert e BoolTy; BoolTy)
  | infer (Add (a, b)) = (assert a IntTy; assert b IntTy; IntTy)
  | infer (If (e, t, f)) = (assert e BoolTy; unify (infer t, infer f))
and assert e t = unify (infer e, t)

fun eval True = True
  | eval False = False
  | eval (Int n) = Int n
  | eval (Not e) = if eval e = True then False else True
  | eval (Add (a, b)) = (case (eval a, eval b) of (Int x, Int y) => Int (x + y))
  | eval (If (e, t, f)) = eval (if eval e = True then t else f)

fun run e = (infer e; SOME (eval e)) handle TyErr => NONE

Example usage on well-typed and ill-typed expressions:

val SOME (Int 3) = run (Add (Int 1, Int 2)) (* well-typed *)
val NONE = run (If (Not (Int 1), True, False)) (* ill-typed *)

Arbitrary-precision integers

[edit]

The IntInf module provides arbitrary-precision integer arithmetic. Moreover, integer literals may be used as arbitrary-precision integers without the programmer having to do anything.

The following program implements an arbitrary-precision factorial function:

fact.sml
fun fact n : IntInf.int = if n = 0 then 1 else n * fact (n - 1);

fun printLine str = TextIO.output (TextIO.stdOut, str ^ "\n");

val () = printLine (IntInf.toString (fact 120));
bash
$ mlton fact.sml
$ ./fact
6689502913449127057588118054090372586752746333138029810295671352301
6335572449629893668741652719849813081576378932140905525344085894081
21859898481114389650005964960521256960000000000000000000000000000

Partial application

[edit]

Curried functions have many applications, such as eliminating redundant code. For example, a module 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 function of type c -> (a * c -> b) -> a -> b can factor out this commonality. This is an example of the adapter pattern.[citation needed]

In this example, fun d computes the numerical derivative of a given function f at point x:

- fun d delta f x = (f (x + delta) - f (x - delta)) / (2.0 * delta)
val d = fn : real -> (real -> real) -> real -> real

The type of fun d indicates that it maps a "float" onto a function with the type (real -> real) -> real -> real. This allows us to partially apply arguments, known as currying. In this case, function d can be specialised by partially applying it with the argument delta. A good choice for delta when using this algorithm is the cube root of the machine epsilon.[citation needed]

- val d' = d 1E~8;
val d' = fn : (real -> real) -> real -> real

The inferred type indicates that d' expects a function with the type real -> real as its first argument. We can compute an approximation to the derivative of at . The correct answer is .

- d' (fn x => x * x * x - x - 1.0) 3.0;
val it = 25.9999996644 : real

Libraries

[edit]

Standard

[edit]

The Basis Library[7] has been standardized and ships with most implementations. It provides modules for trees, arrays, and other data structures, and input/output and system interfaces.

Third party

[edit]

For numerical computing, a Matrix module exists (but is currently broken), https://www.cs.cmu.edu/afs/cs/project/pscico/pscico/src/matrix/README.html.

For graphics, cairo-sml is an open source interface to the Cairo graphics library. For machine learning, a library for graphical models exists.

Implementations

[edit]

Implementations of Standard ML include the following:

Standard

  • HaMLet: a Standard ML interpreter that aims to be an accurate and accessible reference implementation of the standard
  • MLton (mlton.org): a whole-program optimizing compiler which strictly conforms to the Definition and produces very fast code compared to other ML implementations, including backends for LLVM and C
  • Moscow ML: a light-weight implementation, based on the Caml Light runtime engine which implements the full Standard ML language, including modules and much of the basis library
  • Poly/ML: a full implementation of Standard ML that produces fast code and supports multicore hardware (via Portable Operating System Interface (POSIX) threads); its runtime system performs parallel garbage collection and online sharing of immutable substructures.
  • Standard ML of New Jersey (smlnj.org): a full compiler, with associated libraries, tools, an interactive shell, and documentation with support for Concurrent ML
  • SML.NET: a Standard ML compiler for the Common Language Runtime with extensions for linking with other .NET framework code
  • ML Kit Archived 2016-01-07 at the Wayback Machine: an implementation based very closely on the Definition, integrating a garbage collector (which can be disabled) and region-based memory management with automatic inference of regions, aiming to support real-time applications

Derivative

Research

All of these implementations are open-source and freely available. Most are implemented themselves in Standard ML. There are no longer any commercial implementations; Harlequin, now defunct, once produced a commercial IDE and compiler called MLWorks which passed on to Xanalys and was later open-sourced after it was acquired by Ravenbrook Limited on April 26, 2013.

Major projects using SML

[edit]

The IT University of Copenhagen's entire enterprise architecture is implemented in around 100,000 lines of SML, including staff records, payroll, course administration and feedback, student project management, and web-based self-service interfaces.[8]

The proof assistants HOL4, Isabelle, LEGO, and Twelf are written in Standard ML. It is also used by compiler writers and integrated circuit designers such as ARM.[9]

See also

[edit]

References

[edit]
  1. ^ a b "Programming in Standard ML: Hierarchies and Parameterization". Retrieved 2020-02-22.
  2. ^ a b c "SML '97". www.smlnj.org.
  3. ^ a b "itertools — Functions creating iterators for efficient looping — Python 3.7.1rc1 documentation". docs.python.org.
  4. ^ "Influences - The Rust Reference". The Rust Reference. Retrieved 2023-12-31.
  5. ^ a b Milner, Robin; Tofte, Mads; Harper, Robert; MacQueen, David (1997). The Definition of Standard ML (Revised). MIT Press. ISBN 0-262-63181-4.
  6. ^ a b Okasaki, Chris (2000). "Breadth-First Numbering: Lessons from a Small Exercise in Algorithm Design". International Conference on Functional Programming 2000. ACM.
  7. ^ "Standard ML Basis Library". smlfamily.github.io. Retrieved 2022-01-10.
  8. ^ a b Tofte, Mads (2009). "Standard ML language". Scholarpedia. 4 (2): 7515. Bibcode:2009SchpJ...4.7515T. doi:10.4249/scholarpedia.7515.
  9. ^ a b Alglave, Jade; Fox, Anthony C. J.; Ishtiaq, Samin; Myreen, Magnus O.; Sarkar, Susmit; Sewell, Peter; Nardelli, Francesco Zappa (2009). The Semantics of Power and ARM Multiprocessor Machine Code (PDF). DAMP 2009. pp. 13–24. Archived (PDF) from the original on 2017-08-14.
[edit]

About Standard ML

About successor ML

Practical

Academic