Caml (originally an acronym for Categorical Abstract Machine Language) is a
multi-paradigm
Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms.
Some paradigms are concerned mainly with implications for the execution model of the language, suc ...
,
general-purpose programming language
In computer software, a general-purpose programming language (GPL) is a programming language for building software in a wide variety of application domains. Conversely, a domain-specific programming language is used within a specific area. For exam ...
which is a dialect of the
ML programming language
ML (Meta Language) is a General purpose programming language, general-purpose functional programming language. It is known for its use of the polymorphic Hindley–Milner type system, which automatically assigns the data type, types of most Expr ...
family. Caml was developed in France at
INRIA
The National Institute for Research in Digital Science and Technology (Inria) () is a French national research institution focusing on computer science and applied mathematics.
It was created under the name ''Institut de recherche en informatiq ...
and
ENS.
Caml is statically
typed,
strictly evaluated, and uses
automatic memory management
In computer science, garbage collection (GC) is a form of automatic memory management. The ''garbage collector'' attempts to reclaim memory which was allocated by the program, but is no longer referenced; such memory is called ''garbage''. G ...
.
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
, the main descendant of Caml, adds many features to the language, including an
object layer.
Examples
In the following, represents the Caml prompt.
Hello World
print_endline "Hello, world!";;
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:
let rec fact n = if n=0 then 1 else n * fact(n - 1);;
The function can be written equivalently using
pattern matching
In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually has to be exact: "either it will or will not be ...
:
let rec fact = 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 be , meaning that this function maps ints onto ints. For example, 12! is:
# fact 12;;
- : int = 479001600
Numerical derivative (higher-order functions)
Since Caml is a
functional programming language
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that m ...
, it is easy to create and pass around functions in Caml programs. This capability has an enormous number of applications. Calculating the numerical derivative of a function is one such application. The following Caml function computes the numerical derivative of a given function at a given point :
let d delta f x =
(f (x +. delta) -. f (x -. delta)) /. (2. *. delta);;
This function requires a small value . A good choice for delta is the cube root of the
machine epsilon
Machine epsilon or machine precision is an upper bound on the relative approximation error due to rounding in floating point arithmetic. This value characterizes computer arithmetic in the field of numerical analysis, and by extension in the subjec ...
.
The type of the function indicates that it maps a onto another function with the type . This allows us to partially apply arguments. This functional style is known as
currying
In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example, currying a function f that ...
. In this case, it is useful to partially apply the first argument to , to obtain a more specialised function:
# let d = d (sqrt epsilon_float);;
val d : (float -> float) -> float -> float =
Note that the inferred type indicates that the replacement is expecting a function with the type as its first argument. We can compute a numerical approximation to the derivative of
at
with:
# d (fun x -> x *. x *. x -. x -. 1.) 3.;;
- : float = 26.
The correct answer is
.
The function is called a "higher-order function" because it accepts another function () as an argument.
We can go further and create the (approximate) derivative of f, by applying while omitting the argument:
# let f' = d (fun x -> x *. x *. x -. x -. 1.) ;;
val f' : float -> float =
The concepts of curried and higher-order functions are clearly useful in mathematical programs. In fact, 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
In mathematics, the Haar wavelet is a sequence of rescaled "square-shaped" functions which together form a wavelet family or basis. Wavelet analysis is similar to Fourier analysis in that it allows a target function over an interval to be represe ...
transform
Transform may refer to:
Arts and entertainment
* Transform (scratch), a type of scratch used by turntablists
* ''Transform'' (Alva Noto album), 2001
* ''Transform'' (Howard Jones album) or the title song, 2019
* ''Transform'' (Powerman 5000 album ...
of an
integer
An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
-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 ( and ) off the front and storing their sums and differences on the lists and , respectively:
# let haar l =
let rec aux l s d =
match l, s, d with
[], 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 =
For example:
# haar ; 2; 3; 4; -4; -3; -2; -1;
- : int list = ; 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.).
History
The first Caml implementation was written in
Lisp
A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech.
Types
* A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lisping ...
by
Ascánder Suárez in 1987 at the
(INRIA).
["A History of Caml"]
inria.fr
Its successor, ''Caml Light'', was implemented in
C by
Xavier Leroy
Xavier or Xabier may refer to:
Place
* Xavier, Spain
People
* Xavier (surname)
* Xavier (given name)
* Francis Xavier (1506–1552), Catholic saint
** St. Francis Xavier (disambiguation)
* St. Xavier (disambiguation)
* Xavier (footballer, born ...
and
Damien Doligez
Damien Doligez is a French academic and programmer. He is best known for his role as a developer of the OCaml system, especially its garbage collector. He is research scientist (''chargé de recherche'') at the French government research insti ...
,
[ and the original was nicknamed "Heavy Caml" because of its higher memory and CPU requirements.][
''Caml Special Light'' was a further complete rewrite that added a powerful module system to the core language. It was augmented with an object layer to become ''Objective Caml'', eventually renamed ]OCaml
OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
.
See also
*Categorical abstract machine
The categorical abstract machine (CAM) is a model of computation for programs''Cousineau G., Curien P.-L., Mauny M.'' The categorical abstract machine. — LNCS, 201, Functional programming languages computer architecture.-- 1985, pp.~50-64. that ...
*OCaml
OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
References
Bibliography
The Functional Approach to Programming with Caml
by Guy Cousineau and Michel Mauny.
*Cardelli, Luca (1984)
Compiling a functional language
''ACM Symposium on LISP and functional programming'', Association of Computer Machinery.
External links
*{{Official website, caml.inria.fr – Caml language family
ML programming language family
Programming languages
Programming languages created in 1985