ML (Meta Language) is a
general-purpose functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declar ...
language. It is known for its use of the polymorphic
Hindley–Milner type system, which automatically assigns the
types of most
expressions without requiring explicit type annotations, and ensures type safetythere is a formal proof that a well-typed ML program does not cause runtime type errors. ML provides pattern matching for function arguments,
garbage collection
Waste collection is a part of the process of waste management. It is the transfer of solid waste from the point of use and disposal to the point of treatment or landfill. Waste collection also includes the curbside collection of recyclable m ...
,
imperative programming
In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program c ...
,
call-by-value and
currying
In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example, currying a function f that ...
. It is used heavily in programming language research and is one of the few languages to be completely specified and verified using
formal semantics. Its types and pattern matching make it well-suited and commonly used to operate on other formal languages, such as in
compiler writing,
automated theorem proving
Automated theorem proving (also known as ATP or automated deduction) is a subfield of automated reasoning and mathematical logic dealing with proving mathematical theorems by computer programs. Automated reasoning over mathematical proof was a maj ...
, and
formal verification
In the context of hardware and software systems, formal verification is the act of proving or disproving the correctness of intended algorithms underlying a system with respect to a certain formal specification or property, using formal metho ...
.
Overview
Features of ML include a call-by-value
evaluation strategy,
first-class functions, automatic memory management through garbage collection,
parametric polymorphism,
static typing,
type inference
Type inference refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistics ...
,
algebraic data types
In computer programming, especially functional programming and type theory, an algebraic data type (ADT) is a kind of composite type, i.e., a type formed by combining other types.
Two common classes of algebraic types are product types (i.e., t ...
,
pattern matching, and
exception handling. ML uses
static scoping
In computer programming, the scope of a name binding (an association of a name to an entity, such as a variable) is the part of a program where the name binding is valid; that is, where the name can be used to refer to the entity. In other parts o ...
rules.
ML can be referred to as an ''impure'' functional language, because although it encourages functional programming, it does allow
side-effects
In medicine, a side effect is an effect, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequence ...
(like languages such as
Lisp
A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech.
Types
* A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lisping ...
, but unlike a
purely functional language
In computer science, purely functional programming usually designates a programming paradigm—a style of building the structure and elements of computer programs—that treats all computation as the evaluation of mathematical functions.
Program ...
such as
Haskell). Like most programming languages, ML uses
eager evaluation, meaning that all subexpressions are always evaluated, though
lazy evaluation can be achieved through the use of
closures. Thus one can create and use infinite streams as in Haskell, but their expression is indirect.
ML's strengths are mostly applied in language design and manipulation (compilers, analyzers, theorem provers), but it is a general-purpose language also used in
bioinformatics
Bioinformatics () is an interdisciplinary field that develops methods and software tools for understanding biological data, in particular when the data sets are large and complex. As an interdisciplinary field of science, bioinformatics combi ...
and financial systems.
ML was developed by
Robin Milner and others in the early 1970s at the
University of Edinburgh
The University of Edinburgh ( sco, University o Edinburgh, gd, Oilthigh Dhùn Èideann; abbreviated as ''Edin.'' in post-nominals) is a public research university based in Edinburgh, Scotland. Granted a royal charter by King James VI in 15 ...
,
and its syntax is inspired by
ISWIM. Historically, ML was conceived to develop proof tactics in the
LCF theorem prover (whose language, ''pplambda'', a combination of the
first-order predicate calculus and the simply-typed
polymorphic lambda calculus
Lambda calculus (also written as ''λ''-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation ...
, had ML as its metalanguage).
Today there are several languages in the ML family; the three most prominent are
Standard ML
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 researchers, as well as in the development of the ...
(SML),
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
and
F#. Ideas from ML have influenced numerous other languages, like
Haskell,
Cyclone
In meteorology, a cyclone () is a large air mass that rotates around a strong center of low atmospheric pressure, counterclockwise in the Northern Hemisphere and clockwise in the Southern Hemisphere as viewed from above (opposite to an anti ...
,
Nemerle
Nemerle is a general-purpose, high-level, statically typed programming language designed for platforms using the Common Language Infrastructure ( .NET/Mono). It offers functional, object-oriented, aspect-oriented, reflective and imperative featu ...
,
ATS
ATS or Ats may refer to:
Businesses
* ATS Wheels, or ''Auto Technisches Spezialzubehör'', a German wheel manufacturer and sponsor of a Formula One racing team
* ATS Automation Tooling Systems, an Ontario, Canada-based factory automation company
...
, and
Elm
Elms are deciduous and semi-deciduous trees comprising the flowering plant genus ''Ulmus'' in the plant family Ulmaceae. They are distributed over most of the Northern Hemisphere, inhabiting the temperate and tropical-montane regions of North ...
.
Examples
The following examples use the syntax of Standard ML. Other ML dialects such as OCaml and F# differ in small ways.
Factorial
The
factorial
In mathematics, the factorial of a non-negative denoted is the product of all positive integers less than or equal The factorial also equals the product of n with the next smaller factorial:
\begin
n! &= n \times (n-1) \times (n-2) \t ...
function expressed as pure ML:
fun fac (0 : int) : int = 1
, fac (n : int) : int = n * fac (n - 1)
This describes the factorial as a recursive function, with a single terminating base case. It is similar to the descriptions of factorials found in mathematics textbooks. Much of ML code is similar to mathematics in facility and syntax.
Part of the definition shown is optional, and describes the ''types'' of this function. The notation E : t can be read as ''expression E has type t''. For instance, the argument n is assigned type ''integer'' (int), and fac (n : int), the result of applying fac to the integer n, also has type integer. The function fac as a whole then has type ''function from integer to integer'' (int -> int), that is, fac accepts an integer as an argument and returns an integer result. Thanks to type inference, the type annotations can be omitted and will be derived by the compiler. Rewritten without the type annotations, the example looks like:
fun fac 0 = 1
, fac n = n * fac (n - 1)
The function also relies on pattern matching, an important part of ML programming. Note that parameters of a function are not necessarily in parentheses but separated by spaces. When the function's argument is 0 (zero) it will return the integer 1 (one). For all other cases the second line is tried. This is the
recursion
Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
, and executes the function again until the base case is reached.
This implementation of the factorial function is not guaranteed to terminate, since a negative argument causes an
infinite descending chain
In mathematics, a total or linear order is a partial order in which any two elements are comparable. That is, a total order is a binary relation \leq on some Set (mathematics), set X, which satisfies the following for all a, b and c in X:
# a ...
of recursive calls. A more robust implementation would check for a nonnegative argument before recursing, as follows:
fun fact n = let
fun fac 0 = 1
, fac n = n * fac (n - 1)
in
if (n < 0) then raise Domain else fac n
end
The problematic case (when n is negative) demonstrates a use of ML's exception system.
The function can be improved further by writing its inner loop as a
tail call, such that the
call stack
In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or ma ...
need not grow in proportion to the number of function calls. This is achieved by adding an extra, ''accumulator'', parameter to the inner function. At last, we arrive at
fun fact n = let
fun fac 0 acc = acc
, fac n acc = fac (n - 1) (n * acc)
in
if (n < 0) then raise Domain else fac n 1
end
List reverse
The following function ''reverses'' the elements in a list. More precisely, it returns a new list whose elements are in reverse order compared to the given list.
fun reverse [] = []
, reverse (x :: xs) = (reverse xs) @ [x]
This implementation of reverse, while correct and clear, is inefficient, requiring quadratic time for execution. The function can be rewritten to execute in
linear time
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
:
fun 'a reverse xs : 'a list = List.foldl (op ::) [] xs
This function is an example of parametric polymorphism. That is, it can consume lists whose elements have any type, and return lists of the same type.
Modules
Modules are ML's system for structuring large projects and libraries. A module consists of a signature file and one or more structure files. The signature file specifies the
API
An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software Interface (computing), interface, offering a service to other pieces of software. A document or standa ...
to be implemented (like a C header file, or
Java interface
An interface in the Java programming language is an abstract type that is used to describe a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword, and may only contain method sign ...
file). The structure implements the signature (like a C source file or Java class file). For example, the following define an Arithmetic signature and an implementation of it using Rational numbers:
signature ARITH =
sig
type t
val zero : t
val succ : t -> t
val sum : t * t -> t
end
structure Rational : ARITH =
struct
datatype t = Rat of int * int
val zero = Rat (0, 1)
fun succ (Rat (a, b)) = Rat (a + b, b)
fun sum (Rat (a, b), Rat (c, d)) = Rat (a * d + c * b , b * d)
end
These are imported into the interpreter by the 'use' command. Interaction with the implementation is only allowed via the signature functions, for example it is not possible to create a 'Rat' data object directly via this code. The 'structure' block hides all the implementation detail from outside.
ML's standard libraries are implemented as modules in this way.
See also
*
Standard ML
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 researchers, as well as in the development of the ...
and
*
Dependent ML
Dependent ML is an experimental functional programming language proposed by Hongwei Xi and Frank Pfenning. Dependent ML extends ML by a restricted notion of dependent types: types may be dependent on static indices of type Nat (natural numbers). ...
: a dependently typed extension of ML
**
ATS
ATS or Ats may refer to:
Businesses
* ATS Wheels, or ''Auto Technisches Spezialzubehör'', a German wheel manufacturer and sponsor of a Formula One racing team
* ATS Automation Tooling Systems, an Ontario, Canada-based factory automation company
...
: a further development of dependent ML
*
Lazy ML
Lazy is the adjective for laziness, a lack of desire to expend effort.
It may also refer to:
Music Groups and musicians
* Lazy (band), a Japanese rock band
* Lazy Lester, American blues harmonica player Leslie Johnson (1933–2018)
* Lazy Bill ...
: an experimental lazily evaluated ML dialect from the early 1980s
*
PAL (programming language) PAL, the Pedagogic Algorithmic Language, is a programming language developed at the Massachusetts Institute of Technology in around 1967 to help teach programming language semantics and design.John M. Wozencraft and Arthur Evans, Jr. ''Notes on Prog ...
: an educational language related to ML
*
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
: an ML dialect used to implement
Coq
*
F#: an open-source cross-platform functional-first language for the
.NET Framework
The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
*
CoffeeScript and
TypeScript: metalanguages for
ECMAScript
ECMAScript (; ES) is a JavaScript standard intended to ensure the interoperability of web pages across different browsers. It is standardized by Ecma International in the documenECMA-262
ECMAScript is commonly used for client-side scripting o ...
References
Further reading
* ''The Definition of Standard ML'', Robin Milner,
Mads Tofte,
Robert Harper, MIT Press 1990; (revised edition adds author David MacQueen), MIT Press 1997,
The Definition of Standard ML (Revised)
* ''Commentary on Standard ML'',
Robin Milner,
Mads Tofte, MIT Press 1997, .
* ''ML for the Working Programmer'',
Lawrence Paulson
Lawrence Charles Paulson (born 1955) is an American computer scientist. He is a Professor of Computational Logic at the University of Cambridge Computer Laboratory and a Fellow of Clare College, Cambridge.
Education
Paulson graduated from the ...
, Cambridge University Press 1991, 1996, .
*
* ''Elements of ML Programming'',
Jeffrey D. Ullman
Jeffrey David Ullman (born November 22, 1942) is an American computer scientist and the Stanford W. Ascherman Professor of Engineering, Emeritus, at Stanford University. His textbooks on compilers (various editions are popularly known as the ...
, Prentice-Hall 1994, 1998, .
External links
Standard ML of New Jersey, another popular implementationF#, an ML implementation using the Microsoft .NET framework
MLton, a whole-program optimizing Standard ML compilerSuccessor MLor sML
CakeML, a read-eval-print loop version of ML with formally verified runtime and translation to assembler
{{DEFAULTSORT:ML (Programming Language)
Academic programming languages
Functional languages
Procedural programming languages
ML programming language family
Pattern matching programming languages
Statically typed programming languages
Programming languages created in 1973