Ocaml
|
Objective Caml (OCaml) is an advanced programming language which is a member of the ML family. It is developed and distributed as open-source by INRIA. OCaml was created in 1996 as a Caml Light successor by Xavier Leroy, Jerome Vouillon and Damien Doligez and others.
CAML originally stood for Categorical Abstract Machine Language. OCaml abandoned this abstract machine a long time ago.
OCaml shares the functional and imperative features of ML, but adds object-oriented concepts and has minor syntax differences.
Features
Performance distinguishes OCaml from other ML languages. The runtime system was designed to be fast, efficient, and frugal in memory usage. OCaml provides a bytecode compiler, a script interpreter or toplevel evaluation loop, and an optimizing native code compiler. Code generated by the native code compiler is typically comparable to C/C++ in efficiency. See The Computer Language Shootout Benchmarks (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. Its syntax is quite concise. The object system supports multiple inheritance, direct object construction (by specifying methods for a unique object) or from classes, and structural subtyping, where (objects types are compatible if their methods are compatible, regardless of inheritance).
OCaml changes are pragmatically balanced between expressivity, new features, ease of interfacing with existing code and
efficiency. Familiar functions such as printf
. A foreign function interface for
easy linking with C primitives is provided, including language support for efficient
numerical arrays in formats compatible with both C and
FORTRAN.
OCaml distribution components:
- Preprocessor named Camlp4 which permits syntactical extensions
- Debugger which supports stepping backwards to investigate errors
- Documentation generator
- Profiler - for measuring performance
- Numerous general purpose libraries
The compiler is available for many platforms, including Unix, Windows, and Macintosh. Excellent portability is ensured through native code generation support for major architectures.
Uses
Computer science
- Theorem proving (i.e. HOL Light (http://www.cl.cam.ac.uk/users/jrh/hol-light/))
- Computer program analysis
- Compiler writing
Natural science
OCaml is also widely used in physics, chemistry, biology and, more recently, bioinformatics:
Scientific computing OCaml examples (http://www.ffconsultancy.com/products/ocaml_for_scientists/complete/).
Education
Ocaml is used as an introductory language in many universities, including:
- École Normale Supérieure
- Institut d'Informatique d'Entreprise
- EPITA
- Caltech
- Brown University
- University of Pisa
OCaml is also used to teach Computer Science (mainly algorithms and complexity theories) in the French Classes Préparatoires (Preparation Courses), for students studying Computer Science (almost replacing Pascal).
Code examples
Hello World
print_endline "Hello world!";;
Birthday paradox
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 = let p' = (size -. (float (i-1))) *. p /. size in if p' < 0.5 then Printf.printf "answer = %d\n" i else loop p' (i+1) ;; loop 1.0 2
Programs written in OCaml
Commonly used:
- MLDonkey - a popular multi-network P2P program
- Unison (http://www.cis.upenn.edu/~bcpierce/unison/) file synchronizer
Fun:
- Several ICFP winners
- A 2D maze generator (http://www.ffconsultancy.com/free/maze/)
- Gravity simulator (http://handhelds.freshmeat.net/projects/planets/)
- Drgeocaml (http://home.gna.org/geocaml/), a dynamic geometry software
See also
- Standard ML
- F Sharp, an OCaml-like compiler for Microsoft .NET
- EML, a different kind of object-oriented extension to ML
- O'Haskell an object-oriented extension to Haskell, a different functional language
External links
- Caml language family official website (http://caml.inria.fr/)
- OCaml libraries (http://caml.inria.fr/humps/caml_latest.html)
- OCaml tutorial for C, C++, Java and Perl programmers (http://www.merjis.com/developers/ocaml_tutorial/)
- A book on OCaml for scientific programming (http://www.ffconsultancy.com/products/ocaml_for_scientists/)
- Comparison of the speed of various languages (http://shootout.alioth.debian.org/) (with favorable results for Ocaml)
- Unison File Synchronizer (http://www.cis.upenn.edu/~bcpierce/unison/)
- Developing applications with Objective CAML (http://caml.inria.fr/oreilly-book/)
- LablGL (http://wwwfun.kurims.kyoto-u.ac.jp/soft/olabl/lablgl.html) (OpenGL+ interface)
- LablGTK (http://wwwfun.kurims.kyoto-u.ac.jp/soft/olabl/lablgtk.html) (GTK+ interface)
- FFTW (http://fftw.org/) (a widely-used FFT library)de:Objective CAML
es:Ocaml fr:Objective Caml it:Objective Caml ja:OCaml pl:Ocaml pt:OCaml ru:OCaml tr:Ocaml