| Caml | |
|---|---|
| Paradigm | Multi-paradigm:functional,imperative |
| Family | ML |
| Designed by | Gérard Huet, Guy Cousineau, Ascánder Suárez, Pierre Weis, Michel Mauny (Heavy Caml),Xavier Leroy (Caml Light) |
| Developer | INRIA,ENS |
| First appeared | 1985; 41 years ago (1985) |
| Stable release | |
| Typing discipline | inferred,static,strong |
| Memory management | automatic |
| OS | Cross-platform:Unix,Linux,macOS;Windows |
| License | QPL 1,LGPL 2 (Caml Light) |
| Website | caml |
| Influenced by | |
| ML | |
| Influenced | |
| OCaml | |
Caml (originally an acronym forCategorical Abstract Machine Language) is amulti-paradigm,general-purpose,high-level,functionalprogramming language which is adialect of theML programming language family. Caml was developed in France atFrench Institute for Research in Computer Science and Automation (INRIA) andÉcole normale supérieure (Paris) (ENS).
Caml is staticallytyped,strictly evaluated, and usesautomatic memory management.OCaml, the main descendant of Caml, adds many features to the language, including anobject-oriented programming (object) layer.
In the following,# represents the Caml prompt.
print_endline"Hello, world!";;
Many mathematical functions, such as factorial, are most naturally represented in a purely functional form. The following recursive, purely functional Caml function implements factorial:
letrecfactn=ifn=0then1elsen*fact(n-1);;
The function can be written equivalently usingpattern matching:
letrecfact=function|0->1|n->n*fact(n-1);;
This latter form is the mathematical definition of factorial as a recurrence relation.
Note that the compiler inferred the type of this function to beint->int, meaning that this function maps ints onto ints. For example, 12! is:
#fact12;;-:int=479001600
Since Caml is afunctional programming language, it is easy to create and pass around functions in Caml programs. This ability has very many applications. Calculating the numerical derivative of a function is one example. The following Caml functiond computes the numerical derivative of a given functionf at a given pointx:
letddeltafx=(f(x+.delta)-.f(x-.delta))/.(2.*.delta);;
This function requires a small valuedelta. A good choice for delta is the cube root of themachine epsilon.[citation needed].
The type of the functiond indicates that it maps afloat onto another function with the type(float->float)->float->float. This allows us to partially apply arguments. This functional style is known ascurrying. In this case, it is useful to partially apply the first argumentdelta tod, to obtain a more specialised function:
#letd=d(sqrtepsilon_float);;vald:(float->float)->float->float=<fun>
Note that the inferred type indicates that the replacementd is expecting a function with the typefloat->float as its first argument. We can compute a numerical approximation to the derivative of at with:
#d(funx->x*.x*.x-.x-.1.)3.;;-:float=26.
The correct answer is.
The functiond is called a "higher-order function" because it accepts another function (f) as an argument.Going further can create the (approximate) derivative of f, by applyingd while omitting thex argument:
#letf'=d(funx->x*.x*.x-.x-.1.);;valf':float->float=<fun>
The concepts of curried and higher-order functions are clearly useful in mathematical programs. These concepts are equally applicable to most other forms of programming and can be used to factor code much more aggressively, resulting in shorter programs and fewer bugs.
The 1DHaar wavelettransform of aninteger-power-of-two-length list of numbers can be implemented very succinctly in Caml and is an excellent example of the use of pattern matching over lists, taking pairs of elements (h1 andh2) off the front and storing their sums and differences on the listss andd, respectively:
#lethaarl=letrecauxlsd=matchl,s,dwith[s],[],d->s::d|[],s,d->auxs[]d|h1::h2::t,s,d->auxt(h1+h2::s)(h1-h2::d)|_->invalid_arg"haar"inauxl[][];;valhaar:intlist->intlist=<fun>
For example:
#haar[1;2;3;4;-4;-3;-2;-1];;-:intlist=[0;20;4;4;-1;-1;-1;-1]
Pattern matching allows complicated transformations to be represented clearly and succinctly. Moreover, the Caml compiler turns pattern matches into very efficient code, at times resulting in programs that are shorter and faster than equivalent code written with a case statement (Cardelli 1984, p. 210.).
The first Caml implementation was written inLisp by Ascánder Suárez in 1987 at theFrench Institute for Research in Computer Science and Automation (INRIA).[2]
Its successor,Caml Light, was implemented inC byXavier Leroy andDamien Doligez,[2] and the original was nicknamed "Heavy Caml" because of its higher memory and CPU requirements.[2]
Caml Special Light was a further complete rewrite that added a powerful module system to the core language. It was augmented with anobject-oriented programming (object) layer to becomeObjective Caml, eventually renamedOCaml.