fbpx
Wikipedia

Caml

Caml (originally an acronym for Categorical Abstract Machine Language) is a multi-paradigm, general-purpose programming language which is a dialect of the ML programming language family. Caml was developed in France at INRIA and ENS.

Caml
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)
First appeared1985
Stable release
0.75[1] / January 26, 2002; 22 years ago (2002-01-26)
Typing disciplineInferred, static, strong
OSCross-platform
Websitecaml.inria.fr
Influenced by
ML
Influenced
OCaml

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 layer.

Examples edit

In the following, # represents the Caml prompt.

Hello World edit

print_endline "Hello, world!";; 

Factorial function (recursion and purely functional programming) edit

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:

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 int -> int, meaning that this function maps ints onto ints. For example, 12! is:

 # fact 12;; - : int = 479001600 

Numerical derivative (higher-order functions) edit

Since Caml is a functional programming language, 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 d computes the numerical derivative of a given function f at a given point x:

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

This function requires a small value delta. A good choice for delta is the cube root of the machine epsilon[citation needed].

The type of the function d indicates that it maps a float onto another function with the type (float -> float) -> float -> float. 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 delta to d, to obtain a more specialised function:

# let d = d (sqrt epsilon_float);; val d : (float -> float) -> float -> float = <fun> 

Note that the inferred type indicates that the replacement d is expecting a function with the type float -> float 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 d is called a "higher-order function" because it accepts another function (f) as an argument. We can go further and create the (approximate) derivative of f, by applying d while omitting the x argument:

# let f' = d (fun x -> x *. x *. x -. x -. 1.) ;; val f' : float -> float = <fun> 

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) edit

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 (h1 and h2) off the front and storing their sums and differences on the lists s and d, respectively:

# 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> 

For example:

 # haar [1; 2; 3; 4; -4; -3; -2; -1];; - : int list = [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.).

History edit

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 layer to become Objective Caml, eventually renamed OCaml.

See also edit

References edit

  1. ^ "Latest Caml Light release". Retrieved 22 February 2020.
  2. ^ a b c "A History of Caml", inria.fr

Bibliography edit

  • The Functional Approach to Programming with Caml 2007-12-24 at the Wayback Machine 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 edit

  • Official website – Caml language family

caml, this, article, about, programming, language, other, uses, caml, disambiguation, originally, acronym, categorical, abstract, machine, language, multi, paradigm, general, purpose, programming, language, which, dialect, programming, language, family, develo. This article is about the programming language For other uses see CAML disambiguation Caml originally an acronym for Categorical Abstract Machine Language is a multi paradigm general purpose programming language which is a dialect of the ML programming language family Caml was developed in France at INRIA and ENS CamlParadigmMulti paradigm functional imperativeFamilyMLDesigned byGerard Huet Guy Cousineau Ascander Suarez Pierre Weis Michel Mauny Heavy Caml Xavier Leroy Caml Light First appeared1985Stable release0 75 1 January 26 2002 22 years ago 2002 01 26 Typing disciplineInferred static strongOSCross platformWebsitecaml wbr inria wbr frInfluenced byMLInfluencedOCamlCaml 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 layer Contents 1 Examples 1 1 Hello World 1 2 Factorial function recursion and purely functional programming 1 3 Numerical derivative higher order functions 1 4 Discrete wavelet transform pattern matching 2 History 3 See also 4 References 5 Bibliography 6 External linksExamples editIn the following span class o span represents the Caml prompt Hello World edit print endline Hello world Factorial function recursion and purely functional programming edit 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 let rec fact function 0 gt 1 n gt 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 span class kt int span span class o gt span span class kt int span meaning that this function maps ints onto ints For example 12 is fact 12 int 479001600 Numerical derivative higher order functions edit Since Caml is a functional programming language 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 span class n d span computes the numerical derivative of a given function span class n f span at a given point span class n x span let d delta f x f x delta f x delta 2 delta This function requires a small value span class n delta span A good choice for delta is the cube root of the machine epsilon citation needed The type of the function span class n d span indicates that it maps a span class kt float span onto another function with the type span class o span span class kt float span span class o gt span span class kt float span span class o span span class o gt span span class kt float span span class o gt span span class kt float span 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 span class n delta span to span class n d span to obtain a more specialised function let d d sqrt epsilon float val d float gt float gt float gt float lt fun gt Note that the inferred type indicates that the replacement span class n d span is expecting a function with the type span class kt float span span class o gt span span class kt float span as its first argument We can compute a numerical approximation to the derivative of x 3 x 1 displaystyle x 3 x 1 nbsp at x 3 displaystyle x 3 nbsp with d fun x gt x x x x 1 3 float 26 The correct answer is f x 3 x 2 1 f 3 27 1 26 displaystyle f x 3x 2 1 rightarrow f 3 27 1 26 nbsp The function span class n d span is called a higher order function because it accepts another function span class n f span as an argument We can go further and create the approximate derivative of f by applying span class n d span while omitting the span class n x span argument let f d fun x gt x x x x 1 val f float gt float lt fun gt 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 edit 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 span class n h1 span and span class n h2 span off the front and storing their sums and differences on the lists span class n s span and span class n d span respectively let haar l let rec aux l s d match l s d with s d gt s d s d gt aux s d h1 h2 t s d gt aux t h1 h2 s h1 h2 d gt invalid arg haar in aux l val haar int list gt int list lt fun gt For example haar 1 2 3 4 4 3 2 1 int list 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 History editThe first Caml implementation was written in Lisp by Ascander Suarez 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 layer to become Objective Caml eventually renamed OCaml See also editCategorical abstract machine OCamlReferences edit Latest Caml Light release Retrieved 22 February 2020 a b c A History of Caml inria frBibliography editThe Functional Approach to Programming with Caml Archived 2007 12 24 at the Wayback Machine 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 editOfficial website Caml language family Retrieved from https en wikipedia org w index php title Caml amp oldid 1184569215, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.