('''OCaml''') is the main implementation of the
Caml Programming Language , created by
Xavier Leroy ,
Jérôme Vouillon ,
Damien Doligez ,
Didier Rémy and others in 1996. OCaml is an
Open Source project managed and principally maintained by
INRIA .
OCaml extends the core Caml language with
Object-oriented constructs.
OCaml's toolset includes an interactive toplevel
Interpreter , a
Bytecode Compiler , and an optimizing
Native Code compiler. It has a large standard library that makes it useful for many of the same applications as
Python or
Perl , 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 -derived languages are best known for their static
Type System s and
Type-inferring compilers. OCaml unifies functional, imperative, and object-oriented programming 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 of variables and the signature of functions usually do not need to be expressly declared, as they do in
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.
Aside from type-checking overhead,
Functional Programming languages are, in general, challenging to compile to efficient machine language code, due to issues such as the
Funarg Problem . In addition to standard loop, register, and instruction
Optimizations , OCaml's optimizing compiler employs
Static Program Analysis techniques to optimize value
Boxing 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 cautiously stated that "OCaml delivers at least 50% of the performance of a decent C compiler"[http://lwn.net/Articles/19378/ Linux Weekly News]., and
Benchmarks have shown that this is generally the case[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game].. 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).
OCaml features: a
Static
Type System ,
Type Inference ,
Parametric Polymorphism ,
Tail Recursion ,
Pattern Matching ,
First Class Lexical Closures ,
Functors (parametric Modules) ,
Exception Handling , and
incremental generational
Automatic Garbage Collection .
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 for
Linking to
C primitives is provided, including language support for efficient numerical
Arrays in formats compatible with both C and
FORTRAN . Unlike Lisp implementations, 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:
The native code compiler is available for many platforms, including ,
IA-64 ,
AMD64 ,
HP/PA ;
PowerPC ,
SPARC ,
Alpha ,
MIPS , and
StrongARM .
OCaml bytecode and native code programs can be written in a
Multithreaded style, with preemptive context switching. However, because the garbage collector is not designed for concurrency,
Symmetric Multiprocessing is not supported
Xavier Leroy's "standard lecture" on threads . OCaml threads in the same process execute by time sharing only.
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
#
OCaml infers the type of the expression to be "int" (a machine-precision integer) and gives the result "7".
The following program "hello.ml":
print_endline "Hello world!";;
can be compiled to bytecode:
$ ocamlc hello.ml -o hello
and executed:
$ ./hello
Hello world!
$
Ocaml lends itself to the concise expression of recursive algorithms. The following code example implements the
Quicksort algorithm to sort a list into increasing order.
let rec quicksort list = match list with