OCaml

OCaml is a based on the ML programming language family. It is developed at INRIA, headed by Xavier Leroy. Caml originally stood for categorically Abstract Machine Language, but is no longer based for a long time on this abstract concept. In addition to the functional and imperative features of ML supports OCaml object-oriented concepts and differs in details of ML.

OCaml provides a compiler for the generation of both bytecode and machine code. The machine code generated is very efficient (similar to C code ). One of the most useful features of this language is static typing (in conjunction with type inference ), parametric polymorphism, pattern matching, a mechanism for exception handling and garbage collection. OCaml distributions include some public libraries and are available for many platforms, including Unix and Windows.

A well-known program, written in OCaml, MLDonkey, a P2P client that runs on various operating systems and has access to various P2P networks is.

Example of Functional Programming

Let rec repeat f = function     | 1 -> f     | N -> (fun x -> (repeat f ( n - 1)) (f x ) );     (repeat (fun x -> x 2) 3) 1;; This example shows the power of the functional programming: The " repeat " can be any function apply several times on itself and is therefore a function of higher order. The data type of its assigned function is not fixed in advance - the same time " repeat " polymorphic. Typical of the OCaml pattern matching is that by the | character case distinction marked " ". In the example, the function " add two " (fun x -> x 2 ) are applied three times to the number 1. The interpreter prints 7.

Universal polymorphism

The term polymorphism completely different concepts of ad hoc polymorphism and universal polymorphism is often aggregated. Ad- hoc polymorphism, ie implicit type conversion and function overloading are not compatible with the strict static type concept of OCaml and type inference.

As an example may be mentioned here that is distinguished by different operators, for example, between the addition of whole numbers (integers ) and the addition of floating point numbers.

Example, the expression 1 2 is just as wohltypisiert as 1.5 in OCaml . 2.3. The expression 1 1.5, however, is not wohltypisiert because the operator expects two integer values ​​as inputs. An implicit type conversion does not take place.

Rather OCaml implements the powerful concept of universal polymorphism, in both game types, parametric polymorphism, and Inklusionspolymorphie.

Module system

A major strength of the OCaml programming language is its modular system. It allows the programmer as the packages of Java structuring of the source code. Related definitions are to be grouped into modules. This can lead to no name conflicts between different parts of the program and libraries. Each module is defined using the struct ... end expression and receives with his sig ... end signature ( optional).

Modules Str: sig    type t    val compare: t -> t -> int end = struct    type t = string    let compare s1 s2 = s1 s2 string.Compare end The example defines a module called "Str". This module has a type "t" and a function " compare". Compare takes two values ​​of type t and returns an int as a result. From this example you can also see what can be hidden from module information using signatures in OCaml. The type t is abstract only visible outside the module Str. The static type system of OCaml ensures that programmers working in other places with values ​​of type t, can only use the appropriate functions (eg, the compare function of the module Str). The use of the method compare the module string is, however, with values ​​of type t is not possible, even if the internal representation shows that this would work ( because every value of type t is a string ).

The advantage of this abstract data types, however, is that when you change the internal representation of t in, for example, a Unicode string only in the module Str adjustments must be made. All other points in the program code are not affected by the change.

In contrast to the package system of Java allows OCaml to nest modules.

Functors

The module system of OCaml allows programming of parameterized modules. These get statically at compile time another module as a parameter. Using this system, can be very abstract way data structures and algorithms design, without these depend on your specific use. As an example here is called the set module. It mainly includes a functor make, which makes it possible to produce quantities of data, if there is corresponding to the data interface ( compare and t need to be implemented).

Modules SetStr = Set.Make (Str ) see also

  • Standard ML
  • F #, a programming language based on OCaml for Microsoft. NET
  • EML, another object-oriented extension of ML
612208
de