Caml

From English Wikipedia @ Freddythechick

Caml
File:Caml.gif
ParadigmMulti-paradigm: functional, imperative
FamilyML
Designed byGérard Huet, Guy Cousineau, Ascánder Suárez, Pierre Weis, Michel Mauny (Heavy Caml), Xavier Leroy (Caml Light)
DeveloperINRIA, ENS
First appeared1985; 40 years ago (1985)
Stable release
0.75[1] / January 26, 2002; 23 years ago (2002-01-26)
Typing disciplineinferred, static, strong
Memory managementautomatic
OSCross-platform: Unix, Linux, macOS; Windows
LicenseQPL 1, LGPL 2 (Caml Light)
Websitecaml.inria.fr
Influenced by
ML
Influenced
OCaml

Caml (originally an acronym for Categorical Abstract Machine Language) is a multi-paradigm, general-purpose, high-level, functional programming language which is a dialect of the ML programming language family. Caml was developed in France at French Institute for Research in Computer Science and Automation (INRIA) and École normale supérieure (Paris) (ENS).

Caml is statically typed, strictly evaluated, and uses automatic memory management. OCaml, the main descendant of Caml, adds many features to the language, including an object-oriented programming (object) layer.

Examples

In the following, <syntaxhighlight lang="OCaml" inline="1">#</syntaxhighlight> represents the Caml prompt.

Hello World

A "Hello, World!" program is: <syntaxhighlight lang="ocaml"> print_endline "Hello, world!";; </syntaxhighlight>

Factorial function (recursion and purely functional programming)

Many mathematical functions, such as factorial, are most naturally represented in a purely functional form. The following recursive, purely functional Caml function implements factorial: <syntaxhighlight lang=OCaml> let rec fact n = if n=0 then 1 else n * fact(n - 1);; </syntaxhighlight> The function can be written equivalently using pattern matching: <syntaxhighlight lang=OCaml> let rec fact = function

 | 0 -> 1
 | n -> n * fact(n - 1);;

</syntaxhighlight> This latter form is the mathematical definition of factorial as a recurrence relation.

Note that the compiler inferred the type of this function to be <syntaxhighlight lang="OCaml" inline="1">int -> int</syntaxhighlight>, meaning that this function maps ints onto ints. For example, 12! is: <syntaxhighlight lang=OCaml>

# fact 12;;
- : int = 479001600

</syntaxhighlight>

Numerical derivative (higher-order functions)

Since Caml is a functional 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 function <syntaxhighlight lang="OCaml" inline="1">d</syntaxhighlight> computes the numerical derivative of a given function <syntaxhighlight lang="OCaml" inline="1">f</syntaxhighlight> at a given point <syntaxhighlight lang="OCaml" inline="1">x</syntaxhighlight>: <syntaxhighlight lang=OCaml> let d delta f x =

 (f (x +. delta) -. f (x -. delta)) /. (2. *. delta);;

</syntaxhighlight> This function requires a small value <syntaxhighlight lang="OCaml" inline="1">delta</syntaxhighlight>. A good choice for delta is the cube root of the machine epsilon.[citation needed].

The type of the function <syntaxhighlight lang="OCaml" inline="1">d</syntaxhighlight> indicates that it maps a <syntaxhighlight lang="OCaml" inline="1">float</syntaxhighlight> onto another function with the type <syntaxhighlight lang="OCaml" inline="1">(float -> float) -> float -> float</syntaxhighlight>. This allows us to partially apply arguments. This functional style is known as currying. In this case, it is useful to partially apply the first argument <syntaxhighlight lang="OCaml" inline="1">delta</syntaxhighlight> to <syntaxhighlight lang="OCaml" inline="1">d</syntaxhighlight>, to obtain a more specialised function: <syntaxhighlight lang=OCaml>

  1. let d = d (sqrt epsilon_float);;

val d : (float -> float) -> float -> float = <fun> </syntaxhighlight> Note that the inferred type indicates that the replacement <syntaxhighlight lang="OCaml" inline="1">d</syntaxhighlight> is expecting a function with the type <syntaxhighlight lang="OCaml" inline="1">float -> float</syntaxhighlight> as its first argument. We can compute a numerical approximation to the derivative of at with: <syntaxhighlight lang=OCaml>

  1. d (fun x -> x *. x *. x -. x -. 1.) 3.;;

- : float = 26. </syntaxhighlight> The correct answer is .

The function <syntaxhighlight lang="OCaml" inline="1">d</syntaxhighlight> is called a "higher-order function" because it accepts another function (<syntaxhighlight lang="OCaml" inline="1">f</syntaxhighlight>) as an argument. Going further can create the (approximate) derivative of f, by applying <syntaxhighlight lang="OCaml" inline="1">d</syntaxhighlight> while omitting the <syntaxhighlight lang="OCaml" inline="1">x</syntaxhighlight> argument: <syntaxhighlight lang=OCaml>

  1. let f' = d (fun x -> x *. x *. x -. x -. 1.) ;;

val f' : float -> float = <fun> </syntaxhighlight>

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.

Discrete wavelet transform (pattern matching)

The 1D Haar wavelet transform of an integer-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 (<syntaxhighlight lang="OCaml" inline="1">h1</syntaxhighlight> and <syntaxhighlight lang="OCaml" inline="1">h2</syntaxhighlight>) off the front and storing their sums and differences on the lists <syntaxhighlight lang="OCaml" inline="1">s</syntaxhighlight> and <syntaxhighlight lang="OCaml" inline="1">d</syntaxhighlight>, respectively: <syntaxhighlight lang=OCaml>

  1. let haar l =
  let rec aux l s d = 
    match l, s, d with
      [s], [], d -> s :: d
    | [], s, d -> aux s [] d
    | h1 :: h2 :: t, s, d -> aux t (h1 + h2 :: s) (h1 - h2 :: d)
    | _ -> invalid_arg "haar" 
    in aux l [] [];;

val haar : int list -> int list = <fun> </syntaxhighlight> For example: <syntaxhighlight lang=OCaml>

 # haar [1; 2; 3; 4; -4; -3; -2; -1];;
  - : int list = [0; 20; 4; 4; -1; -1; -1; -1]

</syntaxhighlight> 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.).

History

The first Caml implementation was written in Lisp by Ascánder Suárez in 1987 at the French Institute for Research in Computer Science and Automation (INRIA).[2]

Its successor, Caml Light, was implemented in C by Xavier Leroy and Damien 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 an object-oriented programming (object) layer to become Objective Caml, eventually renamed OCaml.

See also

References

  1. ^ "Latest Caml Light release". Retrieved 22 February 2020.
  2. ^ 2.0 2.1 2.2 "A History of Caml", inria.fr

Bibliography

External links