Caml

From Wikipedia, the free encyclopedia

Jump to: navigation, search
Caml
Image:Caml.128x58.gif
Paradigm multi-paradigm: functional, imperative; object-oriented in OCaml
Appeared in 1985
Designed by Gérard Huet, Guy Cousineau, Ascánder Suárez, Pierre Weis, Michel Mauny (Heavy Caml), Xavier Leroy (Caml Light, Objective Caml)
Typing discipline strong, static
Major implementations OCaml, Caml Light
Influenced by ML
Influenced F#
Website caml.inria.fr

Caml (originally an acronym for Categorical Abstract Machine Language) is a dialect of the ML programming language family, developed at INRIA and formerly at ENS.

Like all descendants of ML, Caml is statically typed, strictly evaluated, and uses automatic memory management.

The first Caml implementation in Lisp was nicknamed "Heavy CAML" because of its memory and CPU requirements relative to its successor Caml Light which was implemented in C by Xavier Leroy and Damien Doligez. In addition to a complete rewriting, CAML Special Light added a powerful (applicative) module system to the core language.

Currently, the main implementation of Caml is Objective Caml, which adds many new features to the language including an object layer.

Contents

In the following, # represents the OCaml prompt.

print_endline "Hello world!";;

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

As a functional programming language, it is easy to create and pass around functions in OCaml 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 square root of the machine epsilon.

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 = 

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 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 => f'(3) = 27-1 = 26.

The function "d" is called a "higher-order function" because it accepts another function ("f") as an argument.

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.

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 = 

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 OCaml 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[citation needed].

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.