Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Caml

From Wikipedia, the free encyclopedia
Programming language
This article is about the programming language. For other uses, seeCAML (disambiguation).

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)
DeveloperINRIA,ENS
First appeared1985; 41 years ago (1985)
Stable release
0.75[1] / January 26, 2002; 24 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 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.

Examples

[edit]

In the following,# represents the Caml prompt.

Hello World

[edit]

A"Hello, World!" program is:

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:

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

Numerical derivative (higher-order functions)

[edit]

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 ofx3x1{\displaystyle x^{3}-x-1} atx=3{\displaystyle x=3} with:

#d(funx->x*.x*.x-.x-.1.)3.;;-:float=26.

The correct answer isf(x)=3x21f(3)=271=26{\displaystyle f'(x)=3x^{2}-1\rightarrow f'(3)=27-1=26}.

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.

Discrete wavelet transform (pattern matching)

[edit]

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

History

[edit]

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.

See also

[edit]

References

[edit]
  1. ^"Latest Caml Light release". Retrieved22 February 2020.
  2. ^abc"A History of Caml", inria.fr

Bibliography

[edit]

External links

[edit]
ML programming
Software
Implementations,
dialects
Caml
Standard ML
Dependent ML
Programming tools
Theorem provers,
proof assistants
Community
Designers
  • Lennart Augustsson (Lazy ML)
  • Damien Doligez (OCaml)
  • Gérard Huet (Caml)
  • Xavier Leroy (Caml, OCaml)
  • Robin Milner (ML)
  • Don Sannella (Extended ML)
  • Don Syme (F#)
  • Italics= discontinued
  • ° =Open-source software
    BookCategories:Family:MLFamily:OCamlSoftware:OCaml
  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Caml&oldid=1290279743"
    Categories:
    Hidden categories:

    [8]ページ先頭

    ©2009-2026 Movatter.jp