Objective CAML
OCaml | |
---|---|
Basic data | |
Publishing year: | 1996 |
Designer: | Xavier Leroy, Damien Doligez |
Developer: | INRIA |
Current version | 4.10.0 (February 21, 2020) |
Influenced by: | Caml Light, Cool Standard ML |
Affected: | ATS , Elm , F # , F * , Haxe , Grandpa , Rust , Scala |
Operating system : | Platform independent |
License : | Q Public License (Compiler), LGPL (Library) |
ocaml.org |
OCaml is a programming language based on the ML language family . It is being developed at INRIA under the direction of Xavier Leroy . Caml originally stood for Categorical Abstract Machine Language , but has not been based on this abstract concept for a long time. In addition to the functional and imperative features of ML, OCaml supports object-oriented concepts and differs in details from ML.
OCaml offers a compiler for generating both bytecode and machine code . The generated machine code is very efficient (comparable to C ++ code). The most powerful features of this language include static typing (in conjunction with type inference ), parametric polymorphism , pattern matching , an exception handling mechanism, and automatic garbage collection . OCaml distributions contain some general libraries and are available for a variety of platforms including Unix and Windows .
A well-known program that was written in OCaml is MLDonkey , a P2P - client , who on various operating systems running and to various P2P networks can access.
Functional programming example
let rec wiederhole f = function
| 1 -> f
| n -> (fun x -> (wiederhole f (n - 1)) (f x));;
(wiederhole (fun x -> x + 2) 3) 1;;
This example shows the power of functional programming: The function “repeat” can apply any function to itself several times and is therefore a function of a higher order. At the same time, “repeat” is polymorphic - the data type of the function assigned to it is not fixed in advance. Typical for OCaml is pattern matching , the |
distinction between cases indicated by the symbol . In the example, the function “add two” (
) is used three times on the number 1. The interpreter outputs 7.
fun x -> x + 2
Universal polymorphism
The term polymorphism often encompasses the completely different concepts of ad hoc polymorphism and universal polymorphism. Ad hoc polymorphism, i.e. implicit type conversion and the overloading of functions, are incompatible with the strict static type concept of OCaml and type inference.
As an example, it should be mentioned here that z. B. a distinction is made between the addition of whole numbers (integers) and the addition of floating point numbers by different operators.
Example: The expression
1 + 2
is just as well typed in OCaml as 1.5 +. 2.3
. The expression, 1 + 1.5
however, is not well-typed because the operator +
expects two integer values as inputs. An implicit type conversion does not take place.
Rather, OCaml implements the more powerful concept of universal polymorphism, in both varieties, parametric polymorphism and inclusion polymorphism .
Modular system
A great strength of the OCaml programming language is its modular system. Like the Java "packages", it enables the programmer to structure the source code. Related definitions should be grouped into modules. This means that there can be no name conflicts between different program parts and libraries. Each module is defined using the struct… end expression and is given its signature with sig… end (optional).
module Str : sig
type t
val compare : t -> t -> int
end = struct
type t = string
let compare s1 s2 = String.compare s1 s2
end
The example defines a module with the name "Str". This module has a type "t" and a function "compare". Compare expects two values of type t and returns an integer as the result. This example also shows how information can be hidden in OCaml with the help of module signatures. The type t is only visible in abstract form outside of the Str module. The static type system of OCaml ensures that programmers who work with values of type t in other places can only use the appropriate functions (e.g. the compare function of the Str module). The use of the compare method of the String module , on the other hand, is not possible with values of type t, even if the internal representation shows that this would work (because every value of type t is a string ).
The advantage of these abstract data types is that when the internal representation changes from t to z. B. a Unicode character string only needs to be adjusted in the Str module. All other places in the program code are not affected by the change.
In contrast to the Java package system, OCaml allows modules to be nested.
Functors
The module system from OCaml allows the programming of parameterized modules. This will get statically at compile time another module as a parameter. This allows data structures and algorithms to be designed in a very abstract way, without them depending on their specific use. The module set is mentioned here as an example. It essentially contains a Make functor, which makes it possible to generate quantities for data if there are corresponding interfaces for the data (compare and t must be implemented).
module SetStr = Set.Make(Str)
See also
- Standard ML
- F # , an OCaml-based programming language for Microsoft .NET
- EML , another object-oriented extension of ML
Web links
- Official OCaml site (English)
- Test OCaml in the browser
- Materials for OCaml (English)
- OCaml tutorial
- OCaml video tutorial on YouTube
- Camelia - an IDE for OCaml
- OCaml Package Manager (OPAM) - software package management system for OCaml
- (X) Emacs OCaml mode