HOME

TheInfoList



OR:

Caml (originally an acronym for Categorical Abstract Machine Language) is a
multi-paradigm Programming languages can be grouped by the number and types of Programming paradigm, paradigms supported. Paradigm summaries A concise reference for the programming paradigms listed in this article. * Concurrent programming language, Concurrent ...
, general-purpose, high-level, functional
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
which is a
dialect A dialect is a Variety (linguistics), variety of language spoken by a particular group of people. This may include dominant and standard language, standardized varieties as well as Vernacular language, vernacular, unwritten, or non-standardize ...
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) The – PSL (; also known as ENS, , Ulm or ENS Paris) is a ''grande école'' in Paris, France. It is one of the constituent members of Paris Sciences et Lettres University (PSL). Due to its selectivity, historical role, and influence within F ...
(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 that was allocated by the program, but is no longer referenced; such memory is called ''garbage''. Gar ...
.
OCaml OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
, the main descendant of Caml, adds many features to the language, including an
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
(object) layer.


Examples

In the following, represents the Caml prompt.


Hello World

A
"Hello, World!" program A "Hello, World!" program is usually a simple computer program that emits (or displays) to the screen (often the Console application, console) a message similar to "Hello, World!". A small piece of code in most general-purpose programming languag ...
is: 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 must be exact: "either it will or will not be a ...
: 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 In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
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 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.. 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 a function that takes multiple arguments into a sequence of families of functions, each taking a single argument. In the prototypical example, one begins with a functi ...
. 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 x^3 - x - 1 at x=3 with: # d (fun x -> x *. x *. x -. x -. 1.) 3.;; - : float = 26. The correct answer is f'(x) = 3x^2 - 1 \rightarrow f'(3) = 27 - 1 = 26. The function is called a "
higher-order function In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following: * takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself ...
" because it accepts another function () as an argument. Going further can 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. 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 repr ...
transform of an
integer An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
-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 Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation. Originally specified in the late 1950s, ...
by Ascánder Suárez in 1987 at the French Institute for Research in Computer Science and Automation (INRIA)."A History of Caml"
inria.fr
Its successor, ''Caml Light'', was implemented in C by Xavier Leroy and Damien Doligez, 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-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
(object) layer to become ''Objective Caml'', eventually renamed
OCaml OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
.


See also

* Categorical abstract machine *
OCaml OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...


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

*, INRIA {{Programming languages Programming languages High-level programming languages Functional languages ML programming language family Programming languages created in 1985 French inventions