ML (programming language)
   HOME

TheInfoList



OR:

ML (Meta Language) is a general-purpose
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions tha ...
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 recyclabl ...
,
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 ...
, 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 tha ...
. 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 ...
, 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 met ...
.


Overview

Features of ML include a call-by-value evaluation strategy,
first-class function In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from ...
s, 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 linguistic ...
, algebraic data types, pattern matching, and
exception handling In computing and computer programming, exception handling is the process of responding to the occurrence of ''exceptions'' – anomalous or exceptional conditions requiring special processing – during the execution of a program. In general, an ...
. ML uses static scoping rules. ML can be referred to as an ''impure'' functional language, because although it encourages functional programming, it does allow side-effects (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 lispin ...
, but unlike a purely functional language 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 1 ...
, 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 th ...
, 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 ...
(SML),
OCaml OCaml ( , formerly Objective Caml) is a general-purpose, multi-paradigm programming language Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms. ...
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 an ...
, Nemerle, ATS, and Elm.


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) \ ...
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 mathematic ...
, 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 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 mac ...
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 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 sig ...
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 ...
and * Dependent ML: a dependently typed extension of ML ** ATS: a further development of dependent ML * Lazy ML: 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 Pr ...
: an educational language related to ML *
OCaml OCaml ( , formerly Objective Caml) is a general-purpose, multi-paradigm programming language Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms. ...
: 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 CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehe ...
and
TypeScript TypeScript is a free and open source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. It is designed for the development of large app ...
: 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 scripti ...


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, Prentice-Hall 1994, 1998, .


External links


Standard ML of New Jersey, another popular implementation

F#, an ML implementation using the Microsoft .NET framework

MLton, a whole-program optimizing Standard ML compiler

Successor ML
or 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