Objective Caml, or OCaml is the main implementation of the Caml
CAML
CAML may mean:* Caml, a dialect of the ML programming language* Collaborative Application Markup Language, an XML-based markup language used with the Microsoft SharePoint collaborative portal application... programming language
Programming language
A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer.... , created by Xavier Leroy
Xavier Leroy
Xavier Leroy is a France computer scientist and programmer. He is best known for his role as a primary developer of the Objective Caml system.... , Jérôme Vouillon, Damien Doligez
Damien Doligez
Damien Doligez is a France academic and programmer. He is best known for his role as a developer of the Objective Caml system, especially its Garbage collection .... , Didier Rémy and others in 1996. OCaml is an open source
Open source
Open source is an approach to design, development, and distribution offering practical accessibility to a product's source . Some consider open source as one of various possible design approaches, while others consider it a critical Strategy element of their business operations.... project managed and principally maintained by INRIA
Institut national de recherche en informatique et en automatique
The Institut national de recherche en informatique et en automatique is a France national research institution focusing on computer science, control theory and applied mathematics.... .
Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs.... constructs.
OCaml's toolset includes an interactive toplevel interpreter
Interpreter (computing)
In computer science, an interpreter normally means a computer program that execution , i.e. performs, instructions written in a programming language.... , a bytecode
Bytecode
Bytecode is a term which has been used to denote various forms of instruction sets designed for efficient execution by a software Interpreter as well as being suitable for further compilation into machine language.... compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program.... , and an optimizing native code compiler. It has a large standard library that makes it useful for many of the same applications as Python
Python (programming language)
Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive.... or Perl
Perl
In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language.... , as well as robust modular and object-oriented programming constructs that make it applicable for large-scale software engineering.
OCaml is the successor to Caml Light.
Discussion
Ask a question about 'Objective Caml'
Start a new discussion about 'Objective Caml'
Answer questions from other users
Full Discussion Forum
Encyclopedia
Objective Caml, or OCaml is the main implementation of the Caml
CAML
CAML may mean:* Caml, a dialect of the ML programming language* Collaborative Application Markup Language, an XML-based markup language used with the Microsoft SharePoint collaborative portal application... programming language
Programming language
A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer.... , created by Xavier Leroy
Xavier Leroy
Xavier Leroy is a France computer scientist and programmer. He is best known for his role as a primary developer of the Objective Caml system.... , Jérôme Vouillon, Damien Doligez
Damien Doligez
Damien Doligez is a France academic and programmer. He is best known for his role as a developer of the Objective Caml system, especially its Garbage collection .... , Didier Rémy and others in 1996. OCaml is an open source
Open source
Open source is an approach to design, development, and distribution offering practical accessibility to a product's source . Some consider open source as one of various possible design approaches, while others consider it a critical Strategy element of their business operations.... project managed and principally maintained by INRIA
Institut national de recherche en informatique et en automatique
The Institut national de recherche en informatique et en automatique is a France national research institution focusing on computer science, control theory and applied mathematics.... .
Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs.... constructs.
OCaml's toolset includes an interactive toplevel interpreter
Interpreter (computing)
In computer science, an interpreter normally means a computer program that execution , i.e. performs, instructions written in a programming language.... , a bytecode
Bytecode
Bytecode is a term which has been used to denote various forms of instruction sets designed for efficient execution by a software Interpreter as well as being suitable for further compilation into machine language.... compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program.... , and an optimizing native code compiler. It has a large standard library that makes it useful for many of the same applications as Python
Python (programming language)
Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive.... or Perl
Perl
In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language.... , as well as robust modular and object-oriented programming constructs that make it applicable for large-scale software engineering.
OCaml is the successor to Caml Light. The acronym CAML originally stood for Categorical Abstract Machine Language, although OCaml abandons this abstract machine.
ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM.... -derived languages are best known for their static type system
Type system
In computer science, a type system may be defined as "a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.".... s and type-inferring
Type inference
Type inference, or implicit typing, refers to the ability to deduce automatically the type of a value in a programming language. It is a feature present in some strongly-typed programming language static typing#Static and dynamic typing languages.... compilers. OCaml unifies functional
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data.... , imperative
Imperative programming
In computer science, imperative programming is a programming paradigm that describes computation in terms of statement s that change a program state .... , and object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs.... under an ML-like type system.
OCaml's static type system eliminates a large class of programmer errors that may cause problems at runtime. However, it also forces the programmer to conform to the constraints of the type system, which can require careful thought and close attention. A type-inferring compiler greatly reduces the need for manual type annotations (for example, the data type
Data type
A data type in programming languages is an attribute of a data which tells the computer something about the kind of data it is. This involves setting constraints on the datum, such as what values it can take and what operations may be performed upon it.... of variables and the signature of functions usually do not need to be explicitly declared, as they do in Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java .... ). Nonetheless, effective use of OCaml's type system can require some sophistication on the part of the programmer.
OCaml is perhaps most distinguished from other languages with origins in academia by its emphasis on performance. Firstly, its static type system renders runtime type mismatches impossible, 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 certain type-unsafe features like serialization are used; these are rare enough that avoiding them is practically possible).
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data.... languages are, in general, challenging to compile to efficient machine language code, due to issues such as the funarg problem
Funarg problem
Funarg is an abbreviation for "functional argument"; in computer science, the funarg problem relates to a difficulty in implementing function s as first-class objects in stack-oriented programming language implementations.... . In addition to standard loop, register, and instruction optimizations
Compiler optimization
Compiler optimization is the process of tuning the output of a compiler to minimize or maximize some attribute of an executable computer program.... , OCaml's optimizing compiler employs static program analysis techniques to optimize value boxing and closure
Closure (computer science)
In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables.... allocation, helping to maximize the performance of the resulting code even if it makes extensive use of functional programming constructs.
Xavier Leroy is a France computer scientist and programmer. He is best known for his role as a primary developer of the Objective Caml system.... has cautiously stated that "OCaml delivers at least 50% of the performance of a decent C compiler", and benchmarks
Benchmark (computing)
In computing, a benchmark is the act of running a computer program, a set of programs, or other operations, in order to assess the relative performance of an object, normally by running a number of standard tests and trials against it.... have shown that this is generally the case. 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 is asymptotically faster than the equivalent function in the standard libraries of imperative languages (e.g. C++, Java) because the OCaml implementation exploits the immutability of sets in order to reuse parts of input sets in the output (persistence).
In computer science, a type system may be defined as "a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.".... , type inference
Type inference
Type inference, or implicit typing, refers to the ability to deduce automatically the type of a value in a programming language. It is a feature present in some strongly-typed programming language static typing#Static and dynamic typing languages.... ,
parametric polymorphism, tail recursion
Tail recursion
In computer science, tail recursion is a special case of Recursion_ in which the last operation of the function, the tail call, is a recursive call.... ,
pattern matching
Pattern matching
In computer science, pattern matching is the act of checking for the presence of the constituents of a given pattern. In contrast to pattern recognition, the pattern is rigidly specified.... ,
first class lexical closures
Closure (computer science)
In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables.... ,
functors (parametric modules)
Function object
A function object, also called a functor, functional or functionoid, is a computer programming construct allowing an object to be invoked or called as if it were an ordinary function , usually with the same syntax.... , exception handling
Exception handling
Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions - special conditions that change the normal flow of execution.... , and
incremental generational automatic garbage collection
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage , or memory used by Object that will never be accessed or mutated again by the Application software.... .
OCaml is particularly 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; an unusual feature in statically-typed languages.
A foreign function interface is a mechanism by which a program written in one programming language can call routines or make use of services written in another.... for linking
Linker
In computer science, a linker or link editor is a computer program that takes one ormore object file generated by a compiler and combines them into a single executable program.... to C
C (programming language)
C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system.... primitives is provided, including language support for efficient numerical arrays in formats compatible with both C and FORTRAN
Fortran
Fortran is a general-purpose programming language, procedural programming language, imperative programming language programming language that is especially suited to numerical analysis and scientific computing.... . OCaml also supports the creation of libraries of OCaml functions that can be linked to a "main" program in C, so that one could distribute an OCaml library to C programmers who have no knowledge nor installation of OCaml.
The OCaml distribution contains:
An extensible parser and macro language named Camlp4
Camlp4
Camlp4 is a software system for writing extensible parsers for programming languages. It provides a set of Objective Caml libraries that are used to define grammars as well as loadable syntax extensions of such grammars.... , which permits the syntax of OCaml to be extended or even replaced
Lexer and parser tools called ocamllex and ocamlyacc
A debugger is a computer program that is used to test and debug other programs. The code to be examined might alternatively be running on an Instruction Set Simulator, a technique that allows great power in its ability to halt when specific conditions are encountered but which will typically be much slower than executing the code directly on... which supports stepping backwards to investigate errors
A documentation generator is a programming tool that generates documentation intended for programmers or end users , or both, from a set of specially comment ed source code files, and in some cases, binary files....
In computer science, a library is a collection of subroutines or Class used to develop software. Libraries contain code and data that provide services to independent programs....
The native code compiler is available for many platforms, including Unix
Unix
Unix is a computer operating system originally developed in 1969 by a group of American Telephone & Telegraph employees at Bell Labs, including Ken Thompson , Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.... , Microsoft Windows
Microsoft Windows
Microsoft Windows is a series of software operating systems and graphical user interfaces produced by Microsoft. Microsoft first introduced an operating environment named Windows in November 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces .... , and Apple
Apple Computer
Apple Inc., formerly Apple Computer Inc., is an United States multinational corporation which designs and manufactures consumer electronics and software products.... Mac OS X
Mac OS X
Mac OS X is a line of computer operating systems developed, marketed, and sold by Apple Inc., and since 2002 has been included with all new Macintosh computer systems.... . Excellent portability is ensured through native code generation support for major architectures: IA-32
IA-32
IA-32 , often generically called x86 or x86-32, is the instruction set architecture of Intel's most commercially successful microprocessors.... , IA-64
Itanium
Itanium is the brand name for 64-bit Intel microprocessors that implement the Intel Itanium architecture . Intel has released two processor families using the brand: the original Itanium and the Itanium 2.... , AMD64
X86-64
x86-64 is a superset of the x86. x86-64 Central processing units can run existing 32-bit or 16-bit x86 programs at full speed, but also support new programs written with a 64-bit address space and other additional capabilities.... , HP/PA
PA-RISC family
PA-RISC is an instruction set architecture developed by Hewlett-Packard's Systems & VLSI Technology Operation. As the name implies, it is a RISC architecture, where the PA stands for Precision Architecture.... ; PowerPC
PowerPC
PowerPC is a RISC instruction set architecture created by the 1991 Apple Inc.?IBM?Motorola alliance, known as AIM alliance. Originally intended for personal computers, PowerPC CPUs have since become popular embedded system and high-performance processors.... , SPARC
SPARC
SPARC is a Reduced Instruction Set Computer microprocessor instruction set Computer architecture originally designed in 1985 by Sun Microsystems.... , Alpha
DEC Alpha
Alpha, originally known as Alpha AXP, was a 64-bit reduced instruction set computer instruction set architecture developed by Digital Equipment Corporation , designed to replace the 32-bit VAX complex instruction set computer ISA and its implementations.... , MIPS
MIPS architecture
MIPS is a RISC instruction set architecture developed by MIPS Technologies . In the mid to late 1990s, it was estimated that one in three RISC microprocessors produced were MIPS implementations.... , and StrongARM
StrongARM
The StrongARM is a family of microprocessors that implemented the ARM architecture instruction set architecture . It was developed by Digital Equipment Corporation , and later sold to Intel who continued to manufacture it, before replacing it with the XScale.... .
OCaml bytecode and native code programs can be written in a multithreaded
Thread (computer science)
In computer science, a thread of execution is a Fork of a computer program into two or more Concurrency running task s. The implementation of threads and process es differs from one operating system to another, but in most cases, a thread is contained inside a process.... style, with preemptive context switching. However, because the garbage collector is not designed for concurrency, symmetric multiprocessing
Symmetric multiprocessing
In computing, symmetric multiprocessing or SMP involves a multiprocessor computer-architecture where two or more identical processors can connect to a single shared main memory.... is not supported. OCaml threads in the same process execute by time sharing only.
Code examples
Snippets of OCaml code are most easily studied by entering them into the "top-level". 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) and gives the result "7".
Hello World
The following program "bonjour.ml":
print_endline "Bonjour Mondé!";;
can be compiled to bytecode:
$ ocamlc bonjour.ml -o bonjour
and executed:
$ ./bonjour
Bonjour Mondé!
$
Summing a list of integers
Lists are one of the most fundamental datatypes in OCaml. The following code example sums a list of integers.
let rec sum xs =
match xs with
| [] -> 0
| x xs' -> x + sum xs'
# sum [1;2;3;4;5];;
- : int = 15
Quicksort
Ocaml lends itself to the concise expression of recursive algorithms. The following code example implements the quicksort
Quicksort
Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, average performance, makes comparisons to sort n items. However, in the Best, worst and average case, it makes comparisons.... algorithm to sort a list into increasing order.
let rec quicksort = function
| [] -> []
| pivot rest ->
let is_less x = x < pivot in
let left, right = List.partition is_less rest in
quicksort left @ [pivot] @ quicksort right
Birthday paradox
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 so-called birthday paradox
Birthday paradox
In probability theory, the birthday problem, or birthday paradox pertains to the probability that in a set of randomly chosen people some pair of them will have the same birthday.... , where for 1 person the probability is obviously 100%, for 2 it is 364/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) ;;
In mathematics, Church encoding is a means of embedding data and operators into the lambda calculus, the most familiar form being the Church numerals, a representation of the natural numbers using lambda notation.... of natural numbers, with successor (succ) and addition (add). A Church numeral n is a higher-order function that accepts a function f and a value x and applies f to x exactly n times. To convert a Church numeral from a functional value to a string, we pass it a function which prepends the string "S" to its input and the constant string "0".
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 _ = print (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. 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 provides arbitrary-precision arithmetic and can be loaded into a running top-level using:
# #load "nums.cma";;
# 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!:
Far more sophisticated, high-performance 2D and 3D graphical programs are easily developed in OCaml. Thanks to the use of OpenGL, the resulting programs are not only succinct and efficient but also cross-platform, compiling without any changes on all major platforms.
Derived languages
MetaOCaml
MetaOCaml is a multi-stage programming extension of OCaml enabling incremental compiling of new machine code during runtime. Under certain circumstances, significant speedups are possible using multi-stage 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 a certain power function
Exponentiation
Exponentiation is a mathematics operation , written 'an', involving two numbers, the base a and the exponentn.... x -> x^n is needed very frequently, but the value of n is known only at runtime, you can use a two-stage power function 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 you know n at runtime, you can create a specialized and very fast power function:
. .~(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
provides a synchronization primitive for atomic (transactional) execution of code.
F# is a Microsoft .NET language based on OCaml.
facilitates the manipulation of names and binders.
adds extensional polymorphism to OCaml, thus allowing overloading and type-safe marshalling.
JoCaml is an experimental functional programming language derived from OCaml. It integrates the primitives of the join-calculus to enable flexible, type safety concurrent computing and distributed computing programming.... integrates constructions for developing concurrent and distributed programs.
extends OCaml with features such as XML expressions and regular-expression types.
is a parallel programming system based on OCaml and the P3L language
CAML may mean:* Caml, a dialect of the ML programming language* Collaborative Application Markup Language, an XML-based markup language used with the Microsoft SharePoint collaborative portal application... and Caml Light, languages from which OCaml evolved
Standard ML is a general-purpose, Module , functional programming language with compile-time type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of automated theorem proving.... , another popular dialect of ML
Extensible ML is an ML programming language-like programming language that adds support for object-oriented idioms in a functional setting. EML extends ML-style datatypes and function s with a class construct designed to be extended into hierarchy, thus allowing the programmer to seamlessly integrate the object-oriented programming paradigm... , another object-oriented dialect of ML
O'Haskell is an object-oriented, concurrent programming extension of the functional programming language Haskell . It was developed at OGI School of Science and Engineering and Chalmers University of Technology.... , an object-oriented extension to the functional language Haskell
Haskell (programming language)
Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
The Microsoft .NET Framework is a software framework that is available with several Microsoft Windows operating systems. It includes a large Library of coded solutions to prevent common programming problems and a virtual machine that manages the execution of programs written specifically for the Software framework....
OpenGL is a standard specification defining a cross-language cross-platform Application programming interface for writing applications that produce 2D computer graphics and 3D computer graphics.... + bindings (LablGL) and GTK+
GTK+
GTK+, or The GIMP Toolkit, is a cross-platform widget toolkit for creating graphical user interfaces. It is one of the most popular toolkits for the X Window System, along with Qt .... bindings (LablGTK)