('''OCaml''') is a general-purpose
Programming Language descended from the
ML family, 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 shares the
Functional and
Imperative programming features of ML, but adds
Object-oriented constructs and has minor
Syntax differences. Like all descendants of ML, OCaml is
Compiled , statically
Typed ,
Strictly Evaluated , and uses
Automatic Memory Management .
OCaml's toolset includes an interactive toplevel, 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 a successor to
Caml Light . The acronym CAML originally stood for ''
Categorical Abstract Machine Language '', although OCaml abandons this abstract machine.
ML-derived languages are most well 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. The type-inferring compiler greatly reduces the need for manual type annotation (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 the need for runtime type and safety checks that burden the performance of dynamically typed languages, while still guaranteeing runtime safety.
Aside from typechecking 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"
and Benchmarks have shown that this is generally the case [http://shootout.alioth.debian.org/ .
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 with
C primitives is provided, including language support for efficient numerical
Arrays in formats compatible with both C and
FORTRAN .
The OCaml distribution contains:
The native code compiler is available for many platforms, including ,
AMD64 ,
PowerPC ,
Sparc ,
IA64 ,
Alpha ,
HP/PA ,
MIPS , and
StrongARM .
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 may be used as a
Scripting Language , as the following script calculates the number of people in a room before the probability of two
sharing the same birthday becomes larger than 50% (the so-called
Birthday Paradox ).
On a
Unix-like machine, save it to a file,
Chmod to
Executable (
chmod 0755 birthday.ml) and run it
from the
Command Line (
./birthday.ml).
#!/usr/bin/ocamlrun ocaml
let size = 365. ;;
let rec loop p i =
- . p /. size in
if p' < 0.5 then
Printf.printf "answer = %d
" i
else
loop p' (i+1) ;;
loop 1.0 2
Many mathematical functions, such as factorial, are most naturally represented in a purely functional form. The following recursive, purely-functional OCaml function implements factorial:
- fact(n-1);;
val fact : int -> int =
The function can be written equivalently using
Pattern Matching :
# let rec fact = function
|   |
"]," class="copylinks" target="_blank">s, d -> aux s [ d
|