Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

ML (programming language)

From Wikipedia, the free encyclopedia
General purpose functional programming language
For other uses, seeML.
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "ML" programming language – news ·newspapers ·books ·scholar ·JSTOR
(May 2015) (Learn how and when to remove this message)
ML
ParadigmMulti-paradigm:functional,generic,imperative
Designed byRobin Milner, others at theUniversity of Edinburgh
First appeared1973; 52 years ago (1973)
Typing disciplineInferred,static,strong
Dialects
Caml,OCaml,Standard ML,F#
Influenced by
ISWIM
Influenced
Caml,Clojure,Cyclone,C++,Elm,Erlang,F#,F*,Haskell,Idris,Kotlin,Miranda,Nemerle,Opa,Rocq,Rust,Scala,Standard ML

ML (Meta Language) is ageneral-purpose,high-level,functionalprogramming language. It is known for its use of the polymorphicHindley–Milner type system, which automatically assigns thedata types of mostexpressions without requiring explicit type annotations (type inference), and ensures type safety; there is aformal proof that a well-typed ML program does not cause runtime type errors.[1] ML provides pattern matching for function arguments,garbage collection,imperative programming,call-by-value andcurrying. While ageneral-purpose programming language, ML is used heavily inprogramming language research and is one of the few languages to be completely specified and verified usingformal semantics. Its types and pattern matching make it well-suited and commonly used to operate on other formal languages, such as incompiler writing,automated theorem proving, andformal verification.

Overview

[edit]

Features of ML include a call-by-valueevaluation strategy,first-class functions, automatic memory management through garbage collection,parametric polymorphism,static typing,type inference,algebraic data types,pattern matching, andexception handling. ML usesstatic scoping rules.[2]

ML can be referred to as animpure functional language, because although it encourages functional programming, it does allowside-effects[3] (like languages such asLisp, but unlike apurely functional language such asHaskell). Like most programming languages, ML useseager evaluation, meaning that all subexpressions are always evaluated, thoughlazy evaluation can be achieved through the use ofclosures. Thus, infinite streams can be created and used 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 inbioinformatics and financial systems.

ML was developed byRobin Milner and others in the early 1970s at theUniversity of Edinburgh,[4] and its syntax is inspired byISWIM. Historically, ML was conceived to develop proof tactics in theLCF theorem prover (whose language,pplambda, a combination of thefirst-order predicate calculus and the simply typedpolymorphiclambda calculus, had ML as its metalanguage).

Today there are several languages in the ML family; the three most prominent areStandard ML (SML),OCaml andF#. Ideas from ML have influenced numerous other languages, likeHaskell,Cyclone,Nemerle,[5]ATS, andElm.[6]

Examples

[edit]

The following examples use the syntax of Standard ML. Other ML dialects such as OCaml and F# differ in small ways.

Factorial

[edit]

Thefactorial function expressed as pure ML:

funfac(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 thetypes of this function. The notation E : t can be read asexpression E has type t. For instance, the argument n is assigned typeinteger (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 typefunction 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:

funfac0=1|facn=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 therecursion, 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 aninfinite descending chain of recursive calls. A more robust implementation would check for a nonnegative argument before recursing, as follows:

funfactn=letfunfac0=1|facn=n*fac(n-1)inif(n<0)thenraiseDomainelsefacnend

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 atail call, such that thecall 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

funfactn=letfunfac0acc=acc|facnacc=fac(n-1)(n*acc)inif(n<0)thenraiseDomainelsefacn1end

List reverse

[edit]

The following functionreverses the elements in a list. More precisely, it returns a new list whose elements are in reverse order compared to the given list.

funreverse[]=[]|reverse(x::xs)=(reversexs)@[x]

This implementation of reverse, while correct and clear, is inefficient, requiringquadratic time for execution. The function can be rewritten to execute inlinear time:

fun'areversexs:'alist=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

[edit]

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 theAPI to be implemented (like a C header file, orJava interfaces). 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:

signatureARITH=sigtypetvalzero:tvalsucc:t->tvalsum:t*t->tend
structureRational:ARITH=structdatatypet=Ratofint*intvalzero=Rat(0,1)funsucc(Rat(a,b))=Rat(a+b,b)funsum(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

[edit]

References

[edit]
  1. ^Robin Milner. A theory of type polymorphism in programming. Journal of Computer and System Sciences, 17(3):348–375, 1978.
  2. ^Milner, Robin; Tofte, Mads (1991). "4.1 Contexts, environments and scope".Commentary on Standard ML. The MIT Press. pp. 35–36.ISBN 0-262-63137-7.
  3. ^Sebesta, Robert (1999).Concepts of Programming Languages (4th ed.). Addison-Westley. p. 54.ISBN 0-201-38596-1.
  4. ^Gordon, Michael J. C. (1996)."From LCF to HOL: a short history". Retrieved2007-10-11.
  5. ^Programming language for "special forces" of developers, Russian Software Development Network: Nemerle Project Team, retrievedJanuary 24, 2021
  6. ^Tate, Bruce A.; Daoud, Fred; Dees, Ian; Moffitt, Jack (2014). "3. Elm".Seven More Languages in Seven Weeks (Book version: P1.0-November 2014 ed.). The Pragmatic Programmers, LLC. pp. 97, 101.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

[edit]

External links

[edit]
ML programming
Software
Implementations,
dialects
Caml
Standard ML
Dependent ML
Programming tools
Theorem provers,
proof assistants
Community
Designers
  • Lennart Augustsson (Lazy ML)
  • Damien Doligez (OCaml)
  • Gérard Huet (Caml)
  • Xavier Leroy (Caml, OCaml)
  • Robin Milner (ML)
  • Don Sannella (Extended ML)
  • Don Syme (F#)
  • National
    Other
    Retrieved from "https://en.wikipedia.org/w/index.php?title=ML_(programming_language)&oldid=1322111440"
    Categories:
    Hidden categories:

    [8]ページ先頭

    ©2009-2025 Movatter.jp