ML (programming language)

From Wikipedia, the free encyclopedia

ML
Paradigm: multi-paradigm: imperative, functional
Appeared in: 1973
Designed by: Robin Milner & others at the University of Edinburgh
Typing discipline: static, strong
Dialects: Standard ML, OCaml, F#
Influenced by: ISWIM
Influenced: Haskell, Cyclone, Nemerle

ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM. Historically, ML stands for metalanguage as it was conceived to develop proof tactics in the LCF theorem prover (the language of which ML was the metalanguage is pplambda, a combination of the first-order predicate calculus and the simply-typed polymorphic lambda-calculus). It is known for its use of the Hindley-Milner type inference algorithm, which can infer the types of most values without requiring the extensive annotation often criticised in languages such as Java.

Contents

[edit] Overview

ML is often referred to as an impure functional language, because it permits side-effects, and therefore imperative programming, unlike purely functional programming languages such as Haskell.

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, algebraic data types, pattern matching, and exception handling.

Unlike Haskell, ML uses eager evaluation, which means that all subexpressions are always evaluated. One result of this is that you cannot use infinite lists per se. However, lazy evaluation and hence infinite lists can be simulated, through use of anonymous functions.

Today there are several languages in the ML family; the two major dialects are Standard ML and Caml, but others exist, including F# - an open research project that targets the Microsoft .NET platform. Ideas from ML have influenced numerous other languages, such as Haskell, Cyclone, and Nemerle.

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, financial systems, and applications including a genealogical database, a peer-to-peer client/server program, etc.

[edit] Examples of ML

[edit] Anatomy of an ML function

The "Hello World" of functional languages is the factorial function. Expressed as pure ML:

fun fac : (int -> int) 0 = 1
  | fac n = 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 first line of the factorial function shown is optional, and describes the types of this function. It can be read as the function fac (fun fac) has type (:) function from integer to integer (fn: int -> int). That is, it takes an integer as an argument, and returns another integer. Rewritten without the unnecessary type annotation, it 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 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.

[edit] See also

[edit] External links


Preceding: LCF theorem prover, ISWIM
Subsequent: Standard ML, OCaml, F#, Haskell, Cyclone, Nemerle