World Library  
Flag as Inappropriate
Email this Article
 

ML (programming language)

ML
Paradigm(s) multi-paradigm: imperative, functional
Designed by Robin Milner & others at the University of Edinburgh
Appeared in 1973 (1973)
Typing discipline static, strong, inferred, safe
Dialects Standard ML, OCaml
Influenced by ISWIM
Influenced Miranda, Haskell, Cyclone, C++, F#, Clojure, Felix, Opa, Erlang, Scala

ML is a general-purpose functional programming language developed by Robin Milner and others in the early 1970s at the University of Edinburgh,[1] whose syntax is inspired by ISWIM. Historically, ML stands for metalanguage: it 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). It is known for its use of the Hindley–Milner type inference algorithm, which can automatically infer the types of most expressions without requiring explicit type annotations. Additionally, the use of this algorithm ensures type safety—there is a formal proof that a well-typed ML program does not cause runtime type errors.[2]

Contents

  • Overview 1
  • Examples 2
    • Factorial 2.1
    • List reverse 2.2
  • See also 3
    • Dialects 3.1
    • Other languages and tools 3.2
  • Books 4
  • References 5
  • External links 6

Overview

ML is often referred to as an impure functional language, because it allows side-effects, 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. However, 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.

Today there are several languages in the ML family; the two major dialects are Standard ML (SML) and Caml, but others exist, including F#  — a language that Microsoft supports for their .NET platform. Ideas from ML have influenced numerous other languages, like 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.

ML uses static scoping rules.

Examples

The following examples use the syntax of Standard ML. The other most widely used ML dialect, OCaml, differs in various insubstantial 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). 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 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 Fail "negative argument"
    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 in a tail-recursive style, 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 factorial n = let
  fun fac (0, acc) = acc
    | fac (n, acc) = fac (n - 1, n * acc)
  in
    if (n < 0) then raise Fail "negative argument"
    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 the following more efficient, though less easy-to-read, style:

fun reverse xs = let
  fun rev [] acc = acc
    | rev (hd::tl) acc = rev tl (hd::acc)
in
  rev xs []
end

Notably, 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.

See also

Dialects

  • OCaml, a popular implementation of Caml with support for object-oriented programming
  • Standard ML, a formally specified dialect of ML, and its implementations:
  • Lazy ML, an experimental lazily evaluated ML dialect from the early 1980s that paved the way for compilation of call-by-need languages such as Haskell and Clean
  • Dependent ML, an extension of ML with dependent typing that led to the development of ATS
  • Rpal, an educational language related to ML
  • CakeML,[3] a read-eval-print loop version of ML with formally verified runtime and translation to assembler

Other languages and tools

  • Haskell, another popular functional language with lazy evaluation
  • F#, a functional language for the CLR originating at Microsoft Research, so closely related to ML as to be considered an ML variant
  • ACL2, an automated theorem prover in Common Lisp
  • Cyclone, a safe C variant influenced by ML
  • Mythryl, an ML-like language syntactically close to C with an implementation forked from SML/NJ

Books

  • The Definition of Standard ML, Robin Milner, Mads Tofte, Robert Harper, MIT Press 1990; (Revised edition adds author David MacQueen), MIT Press 1997. ISBN 0-262-63181-4
  • Commentary on Standard ML, Robin Milner, Mads Tofte, MIT Press 1997. ISBN 0-262-63137-7
  • ML for the Working Programmer, Lawrence Paulson, Cambridge University Press 1991, 1996, ISBN 0-521-57050-6
  • Robert Harper: "Programming in Standard ML", Carnegie Mellon University, 2005.
  • Elements of ML Programming, Jeffrey D. Ullman, Prentice-Hall 1994, 1998. ISBN 0-13-790387-1

References

  1. ^  
  2. ^ Robin Milner. A theory of type polymorphism in programming. Journal of Computer and System Sciences, 17(3):348 – 375, 1978.
  3. ^ https://cakeml.org/

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
  • Mythryl, "SML with a Posix face"
  • sML, Successor ML
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
 
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
 
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.
 


Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.