ML (programming language)
This article needs additional citations for verification. (May 2015) |
strong | |
---|---|
Dialects | |
OCaml, Standard ML, F# | |
Influenced by | |
ISWIM | |
Influenced | |
Clojure, Coq, Cyclone, C++, Elm, F#, F*, Haskell, Idris, Kotlin, Miranda, Nemerle, OCaml, Opa, Erlang, Rust, Scala, Standard ML |
ML (Meta Language) is a
Overview
Features of ML include a call-by-value
ML can be referred to as an impure functional language, because although it encourages functional programming, it does allow
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 and financial systems.
ML was developed by Robin Milner and others in the early 1970s at the University of Edinburgh,[4] 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, had ML as its metalanguage).
Today there are several languages in the ML family; the three most prominent are Standard ML (SML), OCaml and F#. Ideas from ML have influenced numerous other languages, like Haskell, Cyclone, Nemerle,[5] ATS, and Elm.[6]
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 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, 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
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 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
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 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 and Standard ML § Implementations
- 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): an educational language related to ML
- OCaml: an ML dialect used to implement Coq and various softwares
- F#: an open-source cross-platform functional-first language for the .NET Framework
References
- ^ Robin Milner. A theory of type polymorphism in programming. Journal of Computer and System Sciences, 17(3):348–375, 1978.
- ISBN 0-262-63137-7.
- ISBN 0-201-38596-1.
- ^ Gordon, Michael J. C. (1996). "From LCF to HOL: a short history". Retrieved 2007-10-11.
- ^ Programming language for "special forces" of developers, Russian Software Development Network: Nemerle Project Team, retrieved January 24, 2021
- ISBN 978-1-941222-15-7.
On page 101, Elm creator Evan Czaplicki says: 'I tend to say "Elm is an ML-family language" to get at the shared heritage of all these languages.' ["these languages" is referring to Haskell, OCaml, SML, and F#.]
Further reading
- The Definition of Standard ML, Robin Milner, .
- Commentary on Standard ML, ISBN 0-262-63137-7.
- ML for the Working Programmer, ISBN 0-521-57050-6.
- Harper, Robert (2011). Programming in Standard ML (PDF). Carnegie Mellon University.
- Elements of ML Programming, ISBN 0-13-790387-1.
External links
- Standard ML of New Jersey, another popular implementation
- F#, an ML implementation using the Microsoft .NET framework Archived 2010-02-18 at the Wayback Machine
- MLton, a whole-program optimizing Standard ML compiler
- CakeML, a read-eval-print loop version of ML with formally verified runtime and translation to assembler