OCaml seems to be a (yet another) very interesting programming tool.
Objective Caml (OCaml) is the main implementation of Caml (Categorical Abstract Machine Language), which is based on ML. The Meta-Language (ML) was originally developed at Edinburgh University in the 1970’s as a language designed to efficiently represent other languages. The language was pioneered by Robin Milner for the Logic of Computable Functions (LCF) theorem prover. The original ML, and its derivatives, were designed to stretch theoretical computer science to the limit, yielding remarkably robust and concise programming languages which can also be very efficient.
There is an interpreter which runs OCaml code in a virtual machine (VM) and two compilers, one which compiles OCaml to a machine independent byte-code which can then be executed by a byte-code interpreter and another which compiles OCaml directly to native code. The native-code compiler is already capable of producing code for Alpha, Sparc, x86, MIPS, HPPA, PowerPC, ARM, ia64 and x86-64 CPUs and the associated run-time environment has been ported to the Linux, Windows, MacOS X, BSD, Solaris, HPUX, IRIX and Tru64 operating systems.
Check out its massive features!
- OCaml programs are thoroughly checked at compile-time such that they are proven to be entirely safe to run, e.g. a compiled OCaml program cannot segfault.
- Functions may be nested, passed as arguments to other functions and stored in data structures as values.
- Strongly typed
- The types of all values are checked during compilation to ensure that they are well defined and validly used.
- Statically typed
- Any typing errors in a program are picked up at compile-time by the compiler, instead of at run-time as in many other languages.
- Type inference
- The types of values are automatically inferred during compilation by the context in which they occur. Therefore, the types of variables and functions in OCaml code does not need to be specified explicitly, dramatically reducing source code size.
- In cases where any of several different types may be valid, any such type can be used. This greatly simplifies the writing of generic, reusable code.
- Pattern matching
- Values, particularly the contents of data structures, can be matched against arbitrarily-complicated patterns in order to determine the appropriate action.
- Programs can be structured by grouping their data structures and related functions into modules.
- Data structures and related functions can also be grouped into objects (object-oriented programming).
- Separate compilation
- Source files can be compiled separately into object files which are then linked together to form an executable. When linking, object files are automatically type checked and optimized before the final executable is created.
Time for some sample code, to calculate f(x)=x3−x−1:
# let f x = x *. x *. x -. x -. 1.;; val f : float -> float = <fun>
According to Kevin Murphy, “… benchmarks … suggests the Ocaml compiler generates the second fastest code of any of the currently available compilers (gcc and the Intel C compilers being first). Given that Ocaml is also a beautiful language to program in, this is pretty compelling.”