HOME

TheInfoList



OR:

OCaml ( , formerly Objective Caml) is a general-purpose, high-level,
multi-paradigm Programming languages can be grouped by the number and types of Programming paradigm, paradigms supported. Paradigm summaries A concise reference for the programming paradigms listed in this article. * Concurrent programming language, Concurrent ...
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
which extends the
Caml Caml (originally an acronym for Categorical Abstract Machine Language) is a multi-paradigm, general-purpose, high-level, functional programming language which is a dialect of the ML programming language family. Caml was developed in France ...
dialect of ML with
object-oriented Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impleme ...
features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon,
Damien Doligez Damien Doligez is a French academic and programmer. He is best known for his role as a developer of the OCaml system, especially its garbage collector. He is a research scientist ('' chargé de recherche'') at the French government research inst ...
, Didier Rémy, Ascánder Suárez, and others. The OCaml
toolchain A toolchain is a set of software development tools used to build and otherwise develop software. Often, the tools are executed sequentially and form a pipeline such that the output of one tool is the input for the next. Sometimes the term is us ...
includes an interactive top-level interpreter, a
bytecode Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (normal ...
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
, an optimizing
native code In computer programming, machine code is computer program, computer code consisting of machine language instruction set architecture, instructions, which are used to control a computer's central processing unit (CPU). For conventional binary ...
compiler, a reversible
debugger A debugger is a computer program used to test and debug other programs (the "target" programs). Common features of debuggers include the ability to run or halt the target program using breakpoints, step through code line by line, and display ...
, and a
package manager A package manager or package management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer in a consistent manner. A package manager deals wi ...

OPAM
together with a composable build system for OCaml
Dune
. OCaml was initially developed in the context of
automated theorem proving Automated theorem proving (also known as ATP or automated deduction) is a subfield of automated reasoning and mathematical logic dealing with proving mathematical theorems by computer programs. Automated reasoning over mathematical proof was a majo ...
, and is used in static analysis and
formal methods In computer science, formal methods are mathematics, mathematically rigorous techniques for the formal specification, specification, development, Program analysis, analysis, and formal verification, verification of software and computer hardware, ...
software. Beyond these areas, it has found use in
systems programming Systems programming, or system programming, is the activity of programming computer system software. The primary distinguishing characteristic of systems programming when compared to application programming is that application programming aims t ...
,
web development Web development is the work involved in developing a website for the Internet (World Wide Web) or an intranet (a private network). Web development can range from developing a simple single static page of plain text to complex web applications, ...
, and specific financial utilities, among other application domains. The acronym ''CAML'' originally stood for ''Categorical Abstract Machine Language'', but OCaml omits this
abstract machine In computer science, an abstract machine is a theoretical model that allows for a detailed and precise analysis of how a computer system functions. It is similar to a mathematical function in that it receives inputs and produces outputs based on p ...
. OCaml is a
free and open-source software Free and open-source software (FOSS) is software available under a license that grants users the right to use, modify, and distribute the software modified or not to everyone free of charge. FOSS is an inclusive umbrella term encompassing free ...
project managed and principally maintained by the
French Institute for Research in Computer Science and Automation The National Institute for Research in Digital Science and Technology (Inria) () is a French national research institution focusing on computer science and applied mathematics. It was created under the name French Institute for Research in Comp ...
(Inria). In the early 2000s, elements from OCaml were adopted by many languages, notably F# and Scala.


Philosophy

ML-derived languages are best known for their static
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
s and type-inferring compilers. OCaml unifies functional, imperative, and
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
under an ML-like type system. Thus, programmers need not be highly familiar with the pure functional language
paradigm In science and philosophy, a paradigm ( ) is a distinct set of concepts or thought patterns, including theories, research methods, postulates, and standards for what constitute legitimate contributions to a field. The word ''paradigm'' is Ancient ...
to use OCaml. By requiring the programmer to work within the constraints of its static
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
, OCaml eliminates many of the type-related runtime problems associated with
dynamically typed In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usua ...
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, the
data type In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
s of variables and the
signature A signature (; from , "to sign") is a depiction of someone's name, nickname, or even a simple "X" or other mark that a person writes on documents as a proof of identity and intent. Signatures are often, but not always, Handwriting, handwritt ...
s of functions usually need not be declared explicitly, as they do in languages like
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
and C#, because they can be inferred 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 when array bounds checking is turned off or when some type-unsafe features like
serialization In computing, serialization (or serialisation, also referred to as pickling in Python (programming language), Python) is the process of translating a data structure or object (computer science), object state into a format that can be stored (e. ...
are used. These are rare enough that avoiding them is quite possible in practice. Aside from type-checking overhead,
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
languages are, in general, challenging to compile to efficient machine language code, due to issues such as the funarg problem. Along with standard loop, register, and instruction optimizations, OCaml's
optimizing compiler An optimizing compiler is a compiler designed to generate code that is optimized in aspects such as minimizing program execution time, memory usage, storage size, and power consumption. Optimization is generally implemented as a sequence of op ...
employs
static program analysis In computer science, static program analysis (also known as static analysis or static simulation) is the analysis of computer programs performed without executing them, in contrast with dynamic program analysis, which is performed on programs duri ...
methods to optimize value
boxing Boxing is a combat sport and martial art. Taking place in a boxing ring, it involves two people – usually wearing protective equipment, such as boxing glove, protective gloves, hand wraps, and mouthguards – throwing Punch (combat), punch ...
and closure 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",Linux Weekly News
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 the
immutability In object-oriented (OO) and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created.Goetz et al. ''Java Concurrency in Practice''. Addison Wesley Professional, 2006, Secti ...
of sets to reuse parts of input sets in the output (see persistent data structure).


History


Development of ML (Meta Language)

Between the 1970s and 1980s,
Robin Milner Arthur John Robin Gorell Milner (13 January 1934 – 20 March 2010) was a British computer scientist, and a Turing Award winner.Turing Award The ACM A. M. Turing Award is an annual prize given by the Association for Computing Machinery (ACM) for contributions of lasting and major technical importance to computer science. It is generally recognized as the highest distinction in the fi ...
winner, worked at the
University of Edinburgh The University of Edinburgh (, ; abbreviated as ''Edin.'' in Post-nominal letters, post-nominals) is a Public university, public research university based in Edinburgh, Scotland. Founded by the City of Edinburgh Council, town council under th ...
's Laboratory for Foundations of Computer Science. Milner and others were working on theorem provers, which were historically developed in languages such as
Lisp Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation. Originally specified in the late 1950s, ...
. Milner repeatedly ran into the issue that the theorem provers would attempt to claim a
proof Proof most often refers to: * Proof (truth), argument or sufficient evidence for the truth of a proposition * Alcohol proof, a measure of an alcoholic drink's strength Proof may also refer to: Mathematics and formal logic * Formal proof, a co ...
was valid by putting non-proofs together. As a result, he went on to develop the meta language for his Logic for Computable Functions, a language that would only allow the writer to construct valid proofs with its polymorphic type system. ML was turned into a
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
to simplify using LCF on different machines, and, by the 1980s, was turned into a complete system of its own. ML would eventually serve as a basis for the creation of OCaml. In the early 1980s, there were some developments that prompted
INRIA The National Institute for Research in Digital Science and Technology (Inria) () is a French national research institution focusing on computer science and applied mathematics. It was created under the name French Institute for Research in Comp ...
's Formel team to become interested in the ML language.
Luca Cardelli Luca Andrea Cardelli is an Italian computer scientist who is a research professor at the University of Oxford, UK. Cardelli is well known for his research in type theory and operational semantics. Among other contributions, in programming lang ...
, a research professor at
University of Oxford The University of Oxford is a collegiate university, collegiate research university in Oxford, England. There is evidence of teaching as early as 1096, making it the oldest university in the English-speaking world and the List of oldest un ...
, used his ''functional 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 at
Paris Diderot University Paris Diderot University, also known as Paris 7 (), was a French university located in Paris, France. It was one of the inheritors of the historic University of Paris, which was split into 13 universities in 1970. Paris Diderot merged with Pari ...
, developed a calculus of categorical combinators and linked it to
lambda calculus In mathematical logic, the lambda calculus (also written as ''λ''-calculus) is a formal system for expressing computability, computation based on function Abstraction (computer science), abstraction and function application, application using var ...
, which led to the definition of the '' categorical abstract machine'' (CAM). Guy Cousineau, a researcher at Paris Diderot University, recognized that this could be applied as a compiling method for ML.


First implementation

Caml Caml (originally an acronym for Categorical Abstract Machine Language) is a multi-paradigm, general-purpose, high-level, functional programming language which is a dialect of the ML programming language family. Caml was developed in France ...
was initially designed and developed by INRIA's Formel team headed by Gé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 and Michel Mauny carried on with development after he left in 1988. 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.”


Caml Light

Between 1990 and 1991, Xavier Leroy designed a new implementation of Caml based on a bytecode interpreter written in C. In addition to this,
Damien Doligez Damien Doligez is a French academic and programmer. He is best known for his role as a developer of the OCaml system, especially its garbage collector. He is a research scientist ('' chargé de recherche'') at the French government research inst ...
wrote a memory management system, also known as a sequential garbage collector, for this implementation. This new implementation, known as Caml Light, replaced the old Caml implementation and ran on small desktop machines. 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.


Caml Special Light

In 1995, Xavier Leroy released Caml Special Light, which was an improved version of Caml. An optimizing native-code compiler was added to the bytecode compiler, which greatly increased performance to comparable levels with mainstream languages such as C++. 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.


Objective Caml

Didier Rémy and Jérôme Vouillon designed an expressive
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
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++ or
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
. In 2000, Jacques Garrigue extended Objective Caml with multiple new features such as polymorphic methods, variants, and labeled and optional arguments.


Ongoing development

Language improvements have been incrementally added for the last two decades to support the growing commercial and academic codebases in OCaml. The OCaml 4.0 release in 2012 added Generalized Algebraic Data Types (GADTs) and first-class modules to increase the flexibility of the language. The OCaml 5.0.0 release in 2022 is a complete rewrite of the language runtime, removing the global GC lock and adding effect handlers via delimited continuations. These changes enable support for shared-memory parallelism and color-blind concurrency, respectively. OCaml's development continued within the Cristal team at INRIA until 2005, when it was succeeded by the Gallium team. Subsequently, Gallium was succeeded by the Cambium team in 2019. As of 2023, there are 23 core developers of the compiler distribution from a variety of organizations and 41 developers for the broader OCaml tooling and packaging ecosystem. In 2023, the OCaml compiler was recognised with ACM SIGPLAN's Programming Languages Software Award.


Features

OCaml features a static
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
,
type inference Type inference, sometimes called type reconstruction, refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some bran ...
,
parametric polymorphism In programming languages and type theory, parametric polymorphism allows a single piece of code to be given a "generic" type, using variables in place of actual types, and then instantiated with particular types as needed. Parametrically polymorph ...
, tail recursion,
pattern matching In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually must be exact: "either it will or will not be a ...
, first class lexical closures, functors (parametric modules),
exception handling In computing and computer programming, exception handling is the process of responding to the occurrence of ''exceptions'' – anomalous or exceptional conditions requiring special processing – during the execution of a program. In general, an ...
, effect handling, and incremental generational automatic garbage collection. OCaml is notable for extending ML-style type inference to an object system in a general-purpose language. This permits structural subtyping, where object types are compatible if their method signatures are compatible, regardless of their declared
inheritance Inheritance is the practice of receiving private property, titles, debts, entitlements, privileges, rights, and obligations upon the death of an individual. The rules of inheritance differ among societies and have changed over time. Offi ...
(an unusual feature in statically typed languages). A
foreign function interface A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written or compiled in another one. An FFI is often used in contexts where calls are made into a bin ...
for linking to C primitives is provided, including language support for efficient numerical
arrays An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
in formats compatible with both C and Fortran. OCaml also supports creating libraries of OCaml functions that can be linked to a ''main'' 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 Metaprogramming is a computer programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyse, or transform other programs, and even modi ...
), i.e. built-in support for preprocessing, th
OCaml 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 the
Abstract Syntax Tree An abstract syntax tree (AST) is a data structure used in computer science to represent the structure of a program or code snippet. It is a tree representation of the abstract syntactic structure of text (often source code) written in a formal ...
level. The latter, which is called PPX, acronym for Pre-Processor eXtension, is the recommended one. The OCaml distribution contains: *
Lexical analysis Lexical tokenization is conversion of a text into (semantically or syntactically) meaningful ''lexical tokens'' belonging to categories defined by a "lexer" program. In case of a natural language, those categories include nouns, verbs, adjectives ...
and
parsing Parsing, syntax analysis, or syntactic analysis is a process of analyzing a String (computer science), string of Symbol (formal), symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal gramm ...
tools called ''ocamllex'' and ''ocamlyacc'' *
Debugger A debugger is a computer program used to test and debug other programs (the "target" programs). Common features of debuggers include the ability to run or halt the target program using breakpoints, step through code line by line, and display ...
that supports stepping backwards to investigate errors * Documentation generator * Profiler – to measure performance * Many general-purpose
libraries A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
The native code compiler is available for many platforms, including
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
,
Microsoft Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
, and
Apple An apple is a round, edible fruit produced by an apple tree (''Malus'' spp.). Fruit trees of the orchard or domestic apple (''Malus domestica''), the most widely grown in the genus, are agriculture, cultivated worldwide. The tree originated ...
macOS macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
. Portability is achieved through native code generation support for major architectures: *
X86-64 x86-64 (also known as x64, x86_64, AMD64, and Intel 64) is a 64-bit extension of the x86 instruction set architecture, instruction set. It was announced in 1999 and first available in the AMD Opteron family in 2003. It introduces two new ope ...
(AMD64),
RISC-V RISC-V (pronounced "risk-five") is an open standard instruction set architecture (ISA) based on established reduced instruction set computer (RISC) principles. The project commenced in 2010 at the University of California, Berkeley. It transfer ...
, and
ARM64 AArch64, also known as ARM64, is a 64-bit version of the ARM architecture family, a widely used set of computer processor designs. It was introduced in 2011 with the ARMv8 architecture and later became part of the ARMv9 series. AArch64 allows ...
(in OCaml 5.0.0 and higher) *
IBM Z IBM Z is a family name used by IBM for all of its z/Architecture mainframe computers. In July 2017, with another generation of products, the official family was changed to IBM Z from IBM z Systems; the IBM Z family will soon include the newes ...
(before OCaml 5.0.0, and back in OCaml 5.1.0) * Power (before OCaml 5.0.0, and due to reappear in OCaml 5.2.0) *
IA-32 IA-32 (short for "Intel Architecture, 32-bit", commonly called ''i386'') is the 32-bit version of the x86 instruction set architecture, designed by Intel and first implemented in the i386, 80386 microprocessor in 1985. IA-32 is the first incarn ...
and ARM (before OCaml 5.0.0) * SPARC (before OCaml 4.06.0) *
DEC Alpha Alpha (original name Alpha AXP) is a 64-bit reduced instruction set computer (RISC) instruction set architecture (ISA) developed by Digital Equipment Corporation (DEC). Alpha was designed to replace 32-bit VAX complex instruction set computers ( ...
, HPPA, IA64 and MIPS (before OCaml 4.00.0) The
bytecode Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (normal ...
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
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 a multithreaded style, with preemptive context switching. OCaml threads in the same domain execute by
time sharing In computing, time-sharing is the concurrent sharing of a computing resource among many tasks or users by giving each task or user a small slice of processing time. This quick switch between tasks or users gives the illusion of simultaneous ...
only. However, an OCaml program can contain several domains.


Code examples

Snippets of OCaml code are most easily studied by entering them into the ''top-level REPL''. This is an interactive OCaml session that prints the inferred types of resulting or defined expressions. 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" (a machine-precision
integer An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
) and gives the result "7".


Hello World

The following program "hello.ml": print_endline "Hello World!" can be run directly: $ ocaml hello.ml 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: $ ./hello Hello World! $ The first argument to ocamlc, "hello.ml", specifies the source file to compile and the "-o hello" flag specifies the output file.


Option

The option type constructor in OCaml, similar to the Maybe type in
Haskell Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
, augments a given data type to either return Some value of the given data type, or to return None. This is used to express that a value might or might not be present. # Some 42;; - : int option = Some 42 # None;; - : 'a option = 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 a
string String or strings may refer to: *String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects Arts, entertainment, and media Films * ''Strings'' (1991 film), a Canadian anim ...
, or if not, returns an empty string: let extract o = match o with , Some i -> string_of_int i , None -> "";; # extract (Some 42);; - : string = "42" # extract None;; - : string = ""


Summing a list of integers

Lists are one of the fundamental datatypes in OCaml. The following code example defines a recursive function ''sum'' that accepts one argument, ''integers'', which is supposed to be a list of integers. Note the keyword rec which denotes that the function is recursive. The function recursively iterates over the given list of integers and provides a sum of the elements. The ''match'' statement has similarities to C's
switch In electrical engineering, a switch is an electrical component that can disconnect or connect the conducting path in an electrical circuit, interrupting the electric current or diverting it from one conductor to another. The most common type o ...
element, though it is far more general. let rec sum integers = (* Keyword rec means 'recursive'. *) match integers with , [] -> 0 (* Yield 0 if integers is the empty list []. *) , first :: rest -> first + sum rest;; (* 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 ;2;3;4;5; - : int = 15 Another way is to use standard fold function that works with lists. let sum integers = List.fold_left (fun accumulator x -> accumulator + x) 0 integers;; # sum ;2;3;4;5; - : int = 15 Since the
anonymous function In computer programming, an anonymous function (function literal, expression or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for const ...
is simply the application of the + operator, this can be shortened to: let sum integers = List.fold_left (+) 0 integers Furthermore, one can omit the list argument by making use of a
partial application Partial may refer to: Mathematics *Partial derivative, derivative with respect to one of several variables of a function, with the other variables held constant ** ∂, a symbol that can denote a partial derivative, sometimes pronounced "partial ...
: let sum = List.fold_left (+) 0


Quicksort

OCaml lends itself to concisely expressing recursive algorithms. The following code example implements an algorithm similar to
quicksort Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961. It is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
that sorts a list in increasing order. let rec qsort = function , [] -> [] , pivot :: rest -> let is_less x = x < pivot in let left, right = List.partition is_less rest in qsort left @ [pivot] @ qsort right Or using partial application of the >= operator. let rec qsort = function , [] -> [] , pivot :: rest -> let is_less = (>=) pivot in let left, right = List.partition is_less rest in qsort left @ [pivot] @ qsort right


Birthday problem

The following program calculates the smallest number of people in a room for whom the probability of completely unique birthdays is less than 50% (the
birthday problem In probability theory, the birthday problem asks for the probability that, in a set of randomly chosen people, at least two will share the same birthday. The birthday paradox is the counterintuitive fact that only 23 people are needed for that ...
, 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). let year_size = 365. let rec birthday_paradox prob people = let prob = (year_size -. float people) /. year_size *. prob in if prob < 0.5 then Printf.printf "answer = %d\n" (people+1) else birthday_paradox prob (people+1) ;; birthday_paradox 1.0 1


Church numerals

The following code defines a
Church encoding In mathematics, Church encoding is a means of representing data and operators in the lambda calculus. The Church numerals are a representation of the natural numbers using lambda notation. The method is named for Alonzo Church, who first encoded d ...
of
natural number In mathematics, the natural numbers are the numbers 0, 1, 2, 3, and so on, possibly excluding 0. Some start counting with 0, defining the natural numbers as the non-negative integers , while others start with 1, defining them as the positive in ...
s, with successor (succ) and addition (add). A Church numeral is a
higher-order function In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following: * takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself ...
that accepts a function and a value and applies to exactly times. To convert a Church numeral from a functional value to a string, we pass it a function that prepends the string to its input and the constant string . let zero f x = x let succ n f x = f (n f x) let one = succ zero let two = succ (succ zero) let add n1 n2 f x = n1 f (n2 f x) let to_string n = n (fun k -> "S" ^ k) "0" let _ = to_string (add (succ two) two)


Arbitrary-precision factorial function (libraries)

A variety of libraries are directly accessible from OCaml. For example, OCaml has a built-in library for
arbitrary-precision arithmetic In computer science, arbitrary-precision arithmetic, also called bignum arithmetic, multiple-precision arithmetic, or sometimes infinite-precision arithmetic, indicates that calculations are performed on numbers whose digits of precision are po ...
. 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";; # open Num;; The factorial function may then be written using the arbitrary-precision numeric operators , and : # let rec fact n = if n =/ Int 0 then Int 1 else n */ fact(n -/ Int 1);; val fact : Num.num -> Num.num = This function can compute much larger factorials, such as 120!: # string_of_num (fact (Int 120));; - : string = "6689502913449127057588118054090372586752746333138029810295671352301633 55724496298936687416527198498130815763789321409055253440858940812185989 8481114389650005964960521256960000000000000000000000000000"


Triangle (graphics)

The following program renders a rotating triangle in 2D using
OpenGL OpenGL (Open Graphics Library) is a Language-independent specification, cross-language, cross-platform application programming interface (API) for rendering 2D computer graphics, 2D and 3D computer graphics, 3D vector graphics. The API is typic ...
: let () = ignore (Glut.init Sys.argv); Glut.initDisplayMode ~double_buffer:true (); ignore (Glut.createWindow ~title:"OpenGL Demo"); let angle t = 10. *. t *. t in let render () = GlClear.clear `color GlMat.load_identity (); GlMat.rotate ~angle: (angle (Sys.time ())) ~z:1. (); GlDraw.begins `triangles; List.iter GlDraw.vertex2 1., -1.; 0., 1.; 1., -1. GlDraw.ends (); Glut.swapBuffers () in GlMat.mode `modelview; Glut.displayFunc ~cb:render; Glut.idleFunc ~cb:(Some Glut.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.


Fibonacci sequence

The following code calculates the
Fibonacci sequence In mathematics, the Fibonacci sequence is a Integer sequence, sequence in which each element is the sum of the two elements that precede it. Numbers that are part of the Fibonacci sequence are known as Fibonacci numbers, commonly denoted . Many w ...
of a number ''n'' inputted. It uses tail recursion and pattern matching. let fib n = let rec fib_aux m a b = match m with , 0 -> a , _ -> fib_aux (m - 1) b (a + b) in fib_aux n 0 1


Higher-order functions

Functions may take functions as input and return functions as result. For example, applying ''twice'' to a function ''f'' yields a function that applies ''f'' two times to its argument. let twice (f : 'a -> 'a) = fun (x : 'a) -> f (f x);; let inc (x : int) : int = x + 1;; let add2 = twice inc;; let inc_str (x : string) : string = x ^ " " ^ x;; let add_str = twice(inc_str);; # add2 98;; - : int = 100 # add_str "Test";; - : string = "Test Test Test Test" The function ''twice'' uses a type variable'' 'a'' to indicate that it can be applied to any function ''f'' mapping from a type'' 'a'' to itself, rather than only to ''int->int'' functions. In particular, ''twice'' can even be applied to itself. # let fourtimes f = (twice twice) f;; val fourtimes : ('a -> 'a) -> 'a -> 'a = # let add4 = fourtimes inc;; val add4 : int -> int = # add4 98;; - : int = 102


Derived languages


MetaOCaml

MetaOCaml is a multi-stage programming extension of OCaml enabling incremental compiling of new
machine code In computer programming, machine code is computer code consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). For conventional binary computers, machine code is the binaryOn nonb ...
during runtime. Under some circumstances, significant speedups are possible using multistage 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 some
power function In mathematics, exponentiation, denoted , is an operation involving two numbers: the ''base'', , and the ''exponent'' or ''power'', . When is a positive integer, exponentiation corresponds to repeated multiplication of the base: that is, i ...
is needed often, but the value of is known only at runtime, a two-stage power function can be used in MetaOCaml: let rec power n x = if n = 0 then .<1>. else if even n then sqr (power (n/2) x) else .<.~x *. .~(power (n - 1) x)>. As soon as is known at runtime, a specialized and very fast power function can be created: . .~(power 5 ..)>. The result is: fun x_1 -> (x_1 * let y_3 = let y_2 = (x_1 * 1) in (y_2 * y_2) in (y_3 * y_3)) The new function is automatically compiled.


Other derived languages

* F# is a
.NET The .NET platform (pronounced as "''dot net"'') is a free and open-source, managed code, managed computer software framework for Microsoft Windows, Windows, Linux, and macOS operating systems. The project is mainly developed by Microsoft emplo ...
framework language based on OCaml. *
JoCaml JoCaml is an experimental general-purpose, high-level, multi-paradigm, functional and object-oriented programming language derived from OCaml. It integrates the primitives of the join-calculus to enable flexible, type-checked concurrent and ...
integrates constructions for developing concurrent and distributed programs. *
Reason Reason is the capacity of consciously applying logic by drawing valid conclusions from new or existing information, with the aim of seeking the truth. It is associated with such characteristically human activities as philosophy, religion, scien ...
is an alternative OCaml
syntax In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituenc ...
and
toolchain A toolchain is a set of software development tools used to build and otherwise develop software. Often, the tools are executed sequentially and form a pipeline such that the output of one tool is the input for the next. Sometimes the term is us ...
for OCaml created at
Facebook Facebook is a social media and social networking service owned by the American technology conglomerate Meta Platforms, Meta. Created in 2004 by Mark Zuckerberg with four other Harvard College students and roommates, Eduardo Saverin, Andre ...
, which can compile to both native code and
JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
.


Software written in OCaml

* Ahrefs, an SEO software product * Alt-Ergo, a
SMT solver In computer science and mathematical logic, satisfiability modulo theories (SMT) is the problem of determining whether a mathematical formula is satisfiable. It generalizes the Boolean satisfiability problem (SAT) to more complex formulas involv ...
. * Astrée, a static analyzer * Be Sport, a
social network A social network is a social structure consisting of a set of social actors (such as individuals or organizations), networks of Dyad (sociology), dyadic ties, and other Social relation, social interactions between actors. The social network per ...
* Coccinelle, a utility for transforming the
source code In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer. Since a computer, at base, only ...
of C programs. * Rocq (previously known as ''Coq''), a
formal proof In logic and mathematics, a formal proof or derivation is a finite sequence of sentences (known as well-formed formulas when relating to formal language), each of which is an axiom, an assumption, or follows from the preceding sentences in the s ...
management system A management system is a set of policy, policies, business process, processes and procedures used by an organization to ensure that it can fulfill the tasks required to achieve its objectives. These objectives cover many aspects of the organizati ...
. * Easycrypt, a toolset for writing computer-aided proofs. * F*, a high-level, multi-paradigm, functional and object-oriented programming language intended for
program verification In the context of hardware and software systems, formal verification is the act of proving or disproving the correctness of a system with respect to a certain formal specification or property, using formal methods of mathematics. Formal ver ...
. * FFTW, a
library A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
for computing
discrete Fourier transform In mathematics, the discrete Fourier transform (DFT) converts a finite sequence of equally-spaced Sampling (signal processing), samples of a function (mathematics), function into a same-length sequence of equally-spaced samples of the discre ...
s. Several C routines have been generated by an OCaml program named . * The web version of Facebook Messenger. * Flow, a static analyzer created at
Facebook Facebook is a social media and social networking service owned by the American technology conglomerate Meta Platforms, Meta. Created in 2004 by Mark Zuckerberg with four other Harvard College students and roommates, Eduardo Saverin, Andre ...
that infers and checks static types for
JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
. * Ocsigen, a client server Web and mobile development framework * Owl Scientific Computing, a dedicated system for scientific and engineering computing. *
Frama-C Frama-C is a set of interoperable program analyzers for C programs. The name ''Frama-C'' stands for ''Framework for Modular Analysis of C programs''. Frama-C has been developed by the French Commissariat à l'Énergie Atomique et aux Énergi ...
, a framework for analyzing C programs. * GeneWeb, free and open-source multi-platform genealogy software. * The
Hack Hack may refer to: Arts, entertainment, and media Games * Hack (Unix video game), ''Hack'' (Unix video game), a 1984 roguelike video game * .hack (video game series), ''.hack'' (video game series), a series of video games by the multimedia fran ...
programming language compiler, created at Facebook, extending PHP with static types. * The
Haxe Haxe is a high-level cross-platform programming language and compiler that can produce applications and source code for many different computing platforms from one code-base. It is free and open-source software, released under an MIT License. ...
programming language compiler. *
HOL Light HOL Light is a proof assistant for classical higher-order logic. It is a member of the HOL theorem prover family. Compared with other HOL systems, HOL Light is intended to have relatively simple foundations. HOL Light is authored and maintained ...
, a formal proof assistant. * Infer, a static analyzer created at Facebook for
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, C, C++, and
Objective-C Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was ...
, used to detect bugs in
iOS Ios, Io or Nio (, ; ; locally Nios, Νιός) is a Greek island in the Cyclades group in the Aegean Sea. Ios is a hilly island with cliffs down to the sea on most sides. It is situated halfway between Naxos and Santorini. It is about long an ...
and Android apps. *
Liquidsoap Liquidsoap is a scripting language oriented toward the creation of audio and video streams, the manipulation of multimedia files, automation, serving as webserver back-end and more. The language is Functional programming, functional, statically t ...
, a
scripting language In computing, a script is a relatively short and simple set of instructions that typically automation, automate an otherwise manual process. The act of writing a script is called scripting. A scripting language or script language is a programming ...
for generating multimedia streams. * MirageOS, a unikernel programming framework written in pure OCaml. * MLdonkey, a
peer-to-peer file sharing Peer-to-peer file sharing is the distribution and sharing of digital media using peer-to-peer (P2P) networking technology. P2P file sharing allows users to access media files such as books, music, movies, and games using a P2P software program th ...
application based on the
EDonkey network The eDonkey Network (also known as the eDonkey2000 network or eD2k) is a decentralized, mostly server-based, peer-to-peer file-sharing network created in 2000 by US developers Jed McCaleb and Sam Yagan that is best suited to share big files ...
. * Opa, a free and open-source programming language for web development. * Reason / ReScript, a general-purpose, high-level, multi-paradigm, functional and object-oriented programming language. * The
Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
compiler was initially implemented in OCaml before becoming self-hosting. * Tezos, a self-amending smart contract platform using XTZ as a native currency. *
Unison Unison (stylised as UNISON) is a Great Britain, British trade union. Along with Unite the Union, Unite, Unison is one of the two largest trade unions in the United Kingdom, with over 1.2 million members who work predominantly in public servic ...
, a
file synchronization File synchronization (or syncing) in computing is the process of ensuring that computer files in two or more locations are updated via certain rules. In ''one-way file synchronization'', also called Web mirror, mirroring, updated files are copied ...
program to synchronize files between two directories. * The reference interpreter for
WebAssembly WebAssembly (Wasm) defines a portable binary-code format and a corresponding text format for executable programs as well as software interfaces for facilitating communication between such programs and their host environment. The main goal of ...
, a low-level
bytecode Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (normal ...
intended for execution inside
web browser A web browser, often shortened to browser, is an application for accessing websites. When a user requests a web page from a particular website, the browser retrieves its files from a web server and then displays the page on the user's scr ...
s. * Xen Cloud Platform (XCP), a turnkey
virtualization In computing, virtualization (abbreviated v12n) is a series of technologies that allows dividing of physical computing resources into a series of virtual machines, operating systems, processes or containers. Virtualization began in the 1960s wit ...
solution for the Xen
hypervisor A hypervisor, also known as a virtual machine monitor (VMM) or virtualizer, is a type of computer software, firmware or hardware that creates and runs virtual machines. A computer on which a hypervisor runs one or more virtual machines is called ...
.


Users

At least several dozen companies use OCaml to some degree. Notable examples include: *
Bloomberg L.P. Bloomberg L.P. is an American privately-held financial, software, data, and media company headquartered in Midtown Manhattan, New York City. It was co-founded by Michael Bloomberg in 1981, with Thomas Secunda, Duncan MacMillan, Charles Ze ...
, which create
BuckleScript
an OCaml compiler backend targeting JavaScript. * Citrix Systems, which uses OCaml in XenServer (rebranded as Citrix Hypervisor during 2018). *
Facebook Facebook is a social media and social networking service owned by the American technology conglomerate Meta Platforms, Meta. Created in 2004 by Mark Zuckerberg with four other Harvard College students and roommates, Eduardo Saverin, Andre ...
, which developed Flow, Hack, Infer, Pfff, and ReasonML in OCaml. * Jane Street Capital, a
proprietary trading Proprietary trading (also known as prop trading) occurs when a trader trades stocks, bonds, currencies, commodities, their derivatives, or other financial instruments with the firm's own money (instead of using customer funds) to make a profit ...
firm, which adopted OCaml as its preferred language in its early days and continues to use it as of 2023. * Docker, which uses OCaml in the desktop editions on
macOS macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
and
Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
. 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 foun
ocaml.org


References


External links

*
OCaml manual

OCaml Package Manager

Real World OCaml
{{Authority control High-level programming languages Cross-platform free software Extensible syntax programming languages Free and open source interpreters Functional languages Object-oriented programming languages ML programming language family OCaml programming language family OCaml software Pattern matching programming languages Programming languages created in 1996 Statically typed programming languages Software using the GNU Lesser General Public License Articles with example OCaml code