![]() | |
Paradigms | Multi-paradigm:functional,imperative,modular,[1]object-oriented |
---|---|
Family | ML:Caml |
Designed by | Xavier Leroy, Jérôme Vouillon,Damien Doligez, Didier Rémy, Ascánder Suárez |
Developer | Inria |
First appeared | 1996; 29 years ago (1996)[2] |
Stable release | |
Typing discipline | Inferred,static,strong,structural |
Implementation language | OCaml,C |
Platform | IA-32,x86-64,Power,SPARC,ARM 32-64,RISC-V |
OS | Cross-platform:Linux,Unix,macOS,Windows |
License | LGPLv2.1 |
Filename extensions | .ml, .mli |
Website | ocaml |
Influenced by | |
C,Caml,Modula-3,Pascal,Standard ML | |
Influenced | |
ATS,Coq,Elm,F#,F*,Haxe,Opa,Rust,[4]Scala | |
|
OCaml (/oʊˈkæməl/oh-KAM-əl, formerlyObjective Caml) is ageneral-purpose,high-level,multi-paradigmprogramming language which extends theCaml dialect ofML withobject-oriented features. OCaml was created in 1996 byXavier Leroy, Jérôme Vouillon,[5]Damien Doligez, Didier Rémy,[6] Ascánder Suárez, and others.
The OCamltoolchain includes an interactive top-levelinterpreter, abytecodecompiler, an optimizingnative code compiler, a reversibledebugger, and apackage manager (OPAM) together with a composable build system for OCaml (Dune). OCaml was initially developed in the context ofautomated theorem proving, and is used instatic analysis andformal methods software. Beyond these areas, it has found use insystems programming,web development, and specific financial utilities, among other application domains.
The acronymCAML originally stood forCategorical Abstract Machine Language, but OCaml omits thisabstract machine.[7] OCaml is afree and open-source software project managed and principally maintained by theFrench Institute for Research in Computer Science and Automation (Inria). In the early 2000s, elements from OCaml were adopted by many languages, notablyF# andScala.
ML-derived languages are best known for their statictype systems andtype-inferring compilers. OCaml unifiesfunctional,imperative, andobject-oriented programming under an ML-like type system. Thus, programmers need not be highly familiar with the pure functional languageparadigm to use OCaml.
By requiring the programmer to work within the constraints of its statictype system, OCaml eliminates many of the type-relatedruntime problems associated withdynamically typed languages. Also, OCaml's type-inferring compiler greatly reduces the need for the manual type annotations that are required in most statically typed languages. For example, thedata types of variables and thesignatures of functions usually need not be declared explicitly, as they do in languages likeJava andC#, because they can beinferred from the operators and other functions that are applied to the variables and other values in the code. Effective use of OCaml's type system can require some sophistication on the part of a programmer, but this discipline is rewarded with reliable, high-performance software.
OCaml is perhaps most distinguished from other languages with origins in academia by its emphasis on performance. Its static type system prevents runtime type mismatches and thus obviates runtime type and safety checks that burden the performance of dynamically typed languages, while still guaranteeing runtime safety, except whenarray bounds checking is turned off or when some type-unsafe features likeserialization are used. These are rare enough that avoiding them is quite possible in practice.
Aside from type-checking overhead,functional programming languages are, in general, challenging to compile to efficient machine language code, due to issues such as thefunarg problem. Along with standard loop, register, and instruction optimizations, OCaml'soptimizing compiler employsstatic program analysis methods to optimize valueboxing andclosure allocation, helping to maximize the performance of the resulting code even if it makes extensive use of functional programming constructs.
Xavier Leroy has stated that "OCaml delivers at least 50% of the performance of a decent C compiler",[8] although a direct comparison is impossible. Some functions in the OCaml standard library are implemented with faster algorithms than equivalent functions in the standard libraries of other languages. For example, the implementation of set union in the OCaml standard library in theory is asymptotically faster than the equivalent function in the standard libraries of imperative languages (e.g., C++, Java) because the OCaml implementation can exploit theimmutability of sets to reuse parts of input sets in the output (seepersistent data structure).
Between the 1970s and 1980s,Robin Milner, a British computer scientist andTuring Award winner, worked at theUniversity of Edinburgh'sLaboratory for Foundations of Computer Science.[9][10] Milner and others were working ontheorem provers, which were historically developed in languages such asLisp. Milner repeatedly ran into the issue that the theorem provers would attempt to claim aproof was valid by putting non-proofs together.[10] As a result, he went on to develop themeta language for hisLogic for Computable Functions, a language that would only allow the writer to construct valid proofs with its polymorphic type system.[11] ML was turned into acompiler to simplify using LCF on different machines, and, by the 1980s, was turned into a complete system of its own.[11] ML would eventually serve as a basis for the creation of OCaml.
In the early 1980s, there were some developments that promptedINRIA's Formel team to become interested in the ML language.Luca Cardelli, a research professor atUniversity of Oxford, used hisfunctional abstract machine to develop a faster implementation of ML, and Robin Milner proposed a new definition of ML to avoid divergence between various implementations. Simultaneously, Pierre-Louis Curien, a senior researcher atParis Diderot University, developed a calculus of categorical combinators and linked it tolambda calculus, which led to the definition of thecategorical abstract machine (CAM). Guy Cousineau, a researcher at Paris Diderot University, recognized that this could be applied as a compiling method for ML.[12]
Caml was initially designed and developed by INRIA's Formel team headed byGérard Huet. The first implementation of Caml was created in 1987 and was further developed until 1992. Though it was spearheaded by Ascánder Suárez,Pierre Weis andMichel Mauny carried on with development after he left in 1988.[12]
Guy Cousineau is quoted recalling that his experience with programming language implementation was initially very limited, and that there were multiple inadequacies for which he is responsible. Despite this, he believes that "Ascander, Pierre and Michel did quite a nice piece of work.”[12]
Between 1990 and 1991,Xavier Leroy designed a new implementation of Caml based on abytecode interpreter written inC. In addition to this,Damien Doligez wrote a memory management system, also known as a sequentialgarbage collector, for this implementation.[11] This new implementation, known asCaml Light, replaced the old Caml implementation and ran on small desktop machines.[12] In the following years, libraries such as Michel Mauny's syntax manipulation tools appeared and helped promote the use of Caml in educational and research teams.[11]
In 1995, Xavier Leroy released Caml Special Light, which was an improved version of Caml.[12] An optimizingnative-code compiler was added to the bytecode compiler, which greatly increased performance to comparable levels with mainstream languages such asC++.[11][12] Also, Leroy designed a high-level module system inspired by the module system of Standard ML which provided powerful facilities for abstraction and parameterization and made larger-scale programs easier to build.[11]
Didier Rémy and Jérôme Vouillon designed an expressivetype system for objects and classes, which was integrated within Caml Special Light. This led to the emergence of the Objective Caml language, first released in 1996 and subsequently renamed to OCaml in 2011. This object system notably supported many prevalent object-oriented idioms in a statically type-safe way, while those same idioms caused unsoundness or required runtime checks in languages such as C++ orJava. In 2000, Jacques Garrigue extended Objective Caml with multiple new features such as polymorphic methods, variants, and labeled and optional arguments.[11][12]
Language improvements have been incrementally added for the last two decades to support the growing commercial and academic codebases in OCaml.[11] The OCaml 4.0 release in 2012 added Generalized Algebraic Data Types (GADTs) and first-class modules to increase the flexibility of the language.[11] The OCaml 5.0.0 release in 2022[13] is a complete rewrite of the language runtime, removing theglobal GC lock and addingeffect handlers viadelimited continuations. These changes enable support forshared-memory parallelism andcolor-blind concurrency, respectively.
OCaml's development continued within the Cristal team at INRIA until 2005, when it was succeeded by the Gallium team.[14] Subsequently, Gallium was succeeded by the Cambium team in 2019.[15][16] As of 2023, there are 23 core developers of the compiler distribution from a variety of organizations[17] and 41 developers for the broader OCaml tooling and packaging ecosystem.[18] In 2023, the OCaml compiler was recognised withACM SIGPLAN's Programming Languages Software Award.
OCaml features astatictype system,type inference,parametric polymorphism,tail recursion,pattern matching, first class lexicalclosures,functors (parametric modules),exception handling,effect handling, and incremental generationalautomatic garbage collection.
OCaml is notable for extending ML-style type inference to an object system in a general-purpose language. This permitsstructural subtyping, where object types are compatible if their method signatures are compatible, regardless of their declaredinheritance (an unusual feature in statically typed languages).
Aforeign function interface forlinking toC primitives is provided, including language support for efficient numericalarrays in formats compatible with both C andFortran. OCaml also supports creating libraries of OCaml functions that can be linked to amain program in C, so that an OCaml library can be distributed to C programmers who have no knowledge or installation of OCaml.
Although OCaml does not have a macro system as an indivisible part of the language (metaprogramming), i.e. built-in support forpreprocessing, theOCaml platform does officially support a library for writing such preprocessors. These can be of two types: one that works at the source code level (as in C), and one that works on theAbstract Syntax Tree level. The latter, which is called PPX, acronym for Pre-Processor eXtension, is the recommended one.
The OCaml distribution contains:
The native code compiler is available for many platforms, includingUnix,Microsoft Windows, andApplemacOS. Portability is achieved through nativecode generation support for major architectures:
Thebytecodecompiler supports operation on any 32- or 64-bit architecture when native code generation is not available, requiring only a C compiler.
OCaml bytecode and native code programs can be written in amultithreaded style, withpreemptive context switching. OCamlthreads in the same domain[20] execute bytime sharing only. However, an OCaml program can contain several domains.
This articlerelies largely or entirely on asingle source. Relevant discussion may be found on thetalk page. Please helpimprove this article byintroducing citations to additional sources. Find sources: "OCaml" – news ·newspapers ·books ·scholar ·JSTOR(January 2024) |
Snippets of OCaml code are most easily studied by entering them into thetop-levelREPL. This is an interactive OCaml session that prints the inferred types of resulting or defined expressions.[21] The OCaml top-level is started by simply executing the OCaml program:
$ocaml Objective Caml version 3.09.0#
Code can then be entered at the "#" prompt. For example, to calculate 1+2*3:
#1+2*3;;- : int = 7
OCaml infers the type of the expression to be "int" (amachine-precisioninteger) and gives the result "7".
The following program "hello.ml":
print_endline"Hello World!"
can be compiled into a bytecode executable:
$ ocamlc hello.ml -o hello
or compiled into an optimized native-code executable:
$ ocamlopt hello.ml -o hello
and executed:
$./helloHello World!$
The first argument to ocamlc, "hello.ml", specifies the source file to compile and the "-o hello" flag specifies the output file.[22]
Theoption
type constructor in OCaml, similar to theMaybe
type inHaskell, augments a given data type to either returnSome
value of the given data type, or to returnNone
.[23] This is used to express that a value might or might not be present.
#Some42;;-:intoption=Some42#None;;-:'aoption=None
This is an example of a function that either extracts an int from an option, if there is one inside, and converts it into astring, or if not, returns an empty string:
letextracto=matchowith|Somei->string_of_inti|None->"";;
#extract(Some42);;-:string="42"#extractNone;;-:string=""
Lists are one of the fundamental datatypes in OCaml. The following code example defines arecursive functionsum that accepts one argument,integers, which is supposed to be a list of integers. Note the keywordrec
which denotes that the function is recursive. The function recursively iterates over the given list of integers and provides a sum of the elements. Thematch statement has similarities toC'sswitch element, though it is far more general.
letrecsumintegers=(* Keyword rec means 'recursive'. *)matchintegerswith|[]->0(* Yield 0 if integers is the empty list []. *)|first::rest->first+sumrest;;(* Recursive call if integers is a non- empty list; first is the first element of the list, and rest is a list of the rest of the elements, possibly []. *)
#sum[1;2;3;4;5];;-:int=15
Another way is to use standardfold function that works with lists.
letsumintegers=List.fold_left(funaccumulatorx->accumulator+x)0integers;;
#sum[1;2;3;4;5];;-:int=15
Since theanonymous function is simply the application of the + operator, this can be shortened to:
letsumintegers=List.fold_left(+)0integers
Furthermore, one can omit the list argument by making use of apartial application:
letsum=List.fold_left(+)0
OCaml lends itself to concisely expressing recursive algorithms. The following code example implements an algorithm similar toquicksort that sorts a list in increasing order.
letrecqsort=function|[]->[]|pivot::rest->letis_lessx=x<pivotinletleft,right=List.partitionis_lessrestinqsortleft@[pivot]@qsortright
Or using partial application of the >= operator.
letrecqsort=function|[]->[]|pivot::rest->letis_less=(>=)pivotinletleft,right=List.partitionis_lessrestinqsortleft@[pivot]@qsortright
The following program calculates the smallest number of people in a room for whom the probability of completely unique birthdays is less than 50% (thebirthday problem, where for 1 person the probability is 365/365 (or 100%), for 2 it is 364/365, for 3 it is 364/365 × 363/365, etc.) (answer = 23).
letyear_size=365.letrecbirthday_paradoxprobpeople=letprob=(year_size-.floatpeople)/.year_size*.probinifprob<0.5thenPrintf.printf"answer = %d\n"(people+1)elsebirthday_paradoxprob(people+1);;birthday_paradox1.01
The following code defines aChurch encoding ofnatural numbers, with successor (succ) and addition (add). A Church numeraln
is ahigher-order function that accepts a functionf
and a valuex
and appliesf
tox
exactlyn
times. To convert a Church numeral from a functional value to a string, we pass it a function that prepends the string"S"
to its input and the constant string"0"
.
letzerofx=xletsuccnfx=f(nfx)letone=succzerolettwo=succ(succzero)letaddn1n2fx=n1f(n2fx)letto_stringn=n(funk->"S"^k)"0"let_=to_string(add(succtwo)two)
A variety of libraries are directly accessible from OCaml. For example, OCaml has a built-in library forarbitrary-precision arithmetic. As the factorial function grows very rapidly, it quickly overflows machine-precision numbers (typically 32- or 64-bits). Thus, factorial is a suitable candidate for arbitrary-precision arithmetic.
In OCaml, the Num module (now superseded by the ZArith module) provides arbitrary-precision arithmetic and can be loaded into a running top-level using:
##use"topfind";;##require"num";;#openNum;;
The factorial function may then be written using the arbitrary-precision numeric operators=/,*/ and-/ :
#letrecfactn=ifn=/Int0thenInt1elsen*/fact(n-/Int1);;valfact:Num.num->Num.num=<fun>
This function can compute much larger factorials, such as 120!:
#string_of_num(fact(Int120));;-:string="6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000"
The following program renders a rotating triangle in 2D usingOpenGL:
let()=ignore(Glut.initSys.argv);Glut.initDisplayMode~double_buffer:true();ignore(Glut.createWindow~title:"OpenGL Demo");letanglet=10.*.t*.tinletrender()=GlClear.clear[`color];GlMat.load_identity();GlMat.rotate~angle:(angle(Sys.time()))~z:1.();GlDraw.begins`triangles;List.iterGlDraw.vertex2[-1.,-1.;0.,1.;1.,-1.];GlDraw.ends();Glut.swapBuffers()inGlMat.mode`modelview;Glut.displayFunc~cb:render;Glut.idleFunc~cb:(SomeGlut.postRedisplay);Glut.mainLoop()
The LablGL bindings to OpenGL are required. The program may then be compiled to bytecode with:
$ ocamlc -I +lablGL lablglut.cma lablgl.cma simple.ml -o simple
or to nativecode with:
$ ocamlopt -I +lablGL lablglut.cmxa lablgl.cmxa simple.ml -o simple
or, more simply, using the ocamlfind build command
$ ocamlfind opt simple.ml -package lablgl.glut -linkpkg -o simple
and run:
$ ./simple
Far more sophisticated, high-performance 2D and 3D graphical programs can be developed in OCaml. Thanks to the use of OpenGL and OCaml, the resulting programs can be cross-platform, compiling without any changes on many major platforms.
The following code calculates theFibonacci sequence of a numbern inputted. It usestail recursion and pattern matching.
letfibn=letrecfib_auxmab=matchmwith|0->a|_->fib_aux(m-1)b(a+b)infib_auxn01
Functions may take functions as input and return functions as result. For example, applyingtwice to a functionf yields a function that appliesf two times to its argument.
lettwice(f:'a->'a)=fun(x:'a)->f(fx);;letinc(x:int):int=x+1;;letadd2=twiceinc;;letinc_str(x:string):string=x^" "^x;;letadd_str=twice(inc_str);;
#add298;;-:int=100#add_str"Test";;-:string="Test Test Test Test"
The functiontwice uses a type variable 'a to indicate that it can be applied to any functionf mapping from a type 'a to itself, rather than only toint->int functions. In particular,twice can even be applied to itself.
#letfourtimesf=(twicetwice)f;;valfourtimes:('a->'a)->'a->'a=<fun>#letadd4=fourtimesinc;;valadd4:int->int=<fun>#add498;;-:int=102
MetaOCaml[24] is amulti-stage programming extension of OCaml enabling incremental compiling of newmachine code during runtime. Under some circumstances, significantspeedups are possible usingmultistage programming, because more detailed information about the data to process is available at runtime than at the regular compile time, so the incremental compiler can optimize away many cases of condition checking, etc.
As an example: if at compile time it is known that somepower functionx->x^n
is needed often, but the value ofn
is known only atruntime, a two-stage power function can be used in MetaOCaml:
letrecpowernx=ifn=0then.<1>.elseifevennthensqr(power(n/2)x)else.<.~x*..~(power(n-1)x)>.
As soon asn
is known at runtime, a specialized and very fast power function can be created:
.<funx->.~(power5.<x>.)>.
The result is:
funx_1->(x_1*lety_3=lety_2=(x_1*1)in(y_2*y_2)in(y_3*y_3))
The new function is automatically compiled.
genfft
.At least several dozen companies use OCaml to some degree.[30] Notable examples include:
In the context of Academic teaching and research, OCaml has a remarkable presence in computer science teaching programmes, both in universities and colleges. A list of educational resources and these teaching programmes can be foundocaml.org.
{{cite web}}
:Missing or empty|title=
(help)