Objective Caml, or OCaml (pronounced /oʊˈkæməl/ oh-KAM-əl) is the main implementation of the Caml Caml is a dialect of the ML programming language family, developed at INRIA and formerly at ENS programming language A programming language is an artificial language designed to express computations that can be performed by a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine, to express algorithms precisely, or as a mode of human communication, created by Xavier Leroy Xavier Leroy is a French computer scientist and programmer. He is best known for his role as a primary developer of the Objective Caml system. He is senior scientist (directeur de recherche) at the French government research institution INRIA, Jérôme Vouillon, Damien Doligez Damien Doligez is a French academic and programmer. He is best known for his role as a developer of the Objective Caml system, especially its garbage collector. He is research scientist at the French government research institution INRIA, Didier Rémy and others in 1996. OCaml extends the core Caml language with object-oriented Object-oriented programming is a programming paradigm that uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and constructs.
OCaml's toolset includes an interactive toplevel interpreter In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in a programming language. An interpreter may be a program that either, a bytecode Bytecode is a term which has been used to denote various forms of instruction sets designed for efficient execution by a software interpreter as well as being suitable for further compilation into machine code. Since instructions are processed by software, they may be arbitrarily complex, but are nonetheless often akin to traditional hardware compiler A compiler is a computer program that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source code is to create an executable program, and an optimizing native code Machine code or machine language is a system of instructions and data executed directly by a computer's central processing unit. Machine code may be regarded as a primitive programming language or as the lowest-level representation of a compiled and/or assembled computer program. Programs in interpreted languages are not represented by machine compiler. It has a large standard library that makes it useful for many of the same applications as Python Python is a general-purpose high-level programming language whose design philosophy emphasizes code readability. Python aims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive. Its use of indentation for block delimiters is unusual among popular programming languages or Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to oversee, as well as robust modular and object-oriented programming constructs that make it applicable for large-scale software engineering. OCaml is the successor to Caml Light Caml is a dialect of the ML programming language family, developed at INRIA and formerly at ENS. The acronym CAML originally stood for Categorical Abstract Machine Language, although OCaml abandons this abstract machine.
OCaml is a free Free software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with minimal restrictions only to ensure that further recipients can also do these things and that manufacturers of consumer- open source Open-source software is computer software that is available in source code form for which the source code and certain other rights normally reserved for copyright holders are provided under a software license that permits users to study, change, and improve the software. Open source licenses often meet the requirements of the Open Source project managed and principally maintained by INRIA The Institut national de recherche en informatique et en automatique (English: National Institute for Research in Computer and Control Sciences) is a French national research institution focusing on computer science, control theory and applied mathematics. It was created in 1967 at Rocquencourt near Paris. Its first site was the historical. In recent years, many new languages have drawn elements from OCaml, most notably F# F# is a multi-paradigm programming language, targeting the .NET Framework, that encompasses functional programming as well as imperative object-oriented programming disciplines. It is a variant of ML and is largely compatible with the OCaml implementation. F# was initially developed by Don Syme at Microsoft Research but is now being developed at and Scala Scala is a multi-paradigm programming language designed to integrate features of object-oriented programming and functional programming. The name Scala stands for "scalable language", signifying that it is designed to grow with the demands of its users.
Contents |
Philosophy
ML ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM. Historically, ML stands for metalanguage: it was conceived to develop proof tactics in the LCF theorem prover . It is known for its use of the Hindley–Milner type-derived languages are best known for their static type systems In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question and type-inferring Type inference, or implicit typing, refers to the ability to deduce automatically the type of a value in a programming language. It is a feature present in some strongly statically typed languages. It is often characteristic of — but not limited to — functional programming languages in general. Some languages that include type inference are: compilers. OCaml unifies functional In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in the, imperative In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way that imperative mood in natural languages expresses commands to take action, imperative programs define sequences of commands for the computer to perform, and object-oriented programming Object-oriented programming is a programming paradigm that uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and under an ML-like type system. This means the program author is not required to be overly familiar with pure functional language paradigm in order to use OCaml.
OCaml's static type system can help eliminate problems at runtime. However, it also forces the programmer to conform to the constraints of the type system, which can require careful thought and close attention. A type-inferring compiler greatly reduces the need for manual type annotations (for example, the data type A data type In programming, a classification identifying one of various types of data, as floating-point, integer, or Boolean, stating the possible values for that type, the operations that can be done on that type, and the way the values of that type are stored of variables and the signature of functions usually do not need to be explicitly declared, as they do in Java Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file)). Nonetheless, effective use of OCaml's type system can require some sophistication on the part of the programmer.
OCaml is perhaps most distinguished from other languages with origins in academia by its emphasis on performance. Firstly, its static type system renders runtime type mismatches impossible, and thus obviates runtime type and safety checks that burden the performance of dynamically typed languages, while still guaranteeing runtime safety (except when array bounds checking is turned off, or when certain type-unsafe features like serialization are used; these are rare enough that avoiding them is quite possible in practice).
Aside from type-checking overhead, functional programming In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in the languages are, in general, challenging to compile to efficient machine language code, due to issues such as the funarg problem Funarg is an abbreviation for "functional argument"; in computer science, the funarg problem relates to a difficulty in implementing functions as first-class objects in stack-based programming language implementations. In addition to standard loop, register, and instruction optimizations Compiler optimization is the process of tuning the output of a compiler to minimize or maximize some attribute of an executable computer program. The most common requirement is to minimize the time taken to execute a program; a less common one is to minimize the amount of memory occupied. The growth of portable computers has created a market for, OCaml's optimizing compiler employs static program analysis techniques to optimize value boxing and closure In computer science, a closure is a first-class function with free variables that are bound by the lexical environment. Such a function is said to be "closed over" its free variables. A closure is defined within the scope of its free variables, and the extent of those variables is at least as long as the lifetime of the closure itself allocation, helping to maximize the performance of the resulting code even if it makes extensive use of functional programming constructs.
Xavier Leroy Xavier Leroy is a French computer scientist and programmer. He is best known for his role as a primary developer of the Objective Caml system. He is senior scientist (directeur de recherche) at the French government research institution INRIA has stated that "OCaml delivers at least 50% of the performance of a decent C compiler"[1], but a direct comparison is impossible. Some functions in the OCaml standard library are implemented with faster algorithms than equivalent functions in the standard libraries of other languages. For example, the implementation of set union in the OCaml standard library is asymptotically faster than the equivalent function in the standard libraries of imperative languages (e.g. C++, Java) because the OCaml implementation exploits the immutability of sets in order to reuse parts of input sets in the output (persistence).
Features
OCaml features: a static A compiler is a computer program that transforms source code written in a computer language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source code is to create an executable program type system In computer science, a type system may be defined as a tractable syntactic framework for classifying phrases according to the kinds of values they compute. A type system associates types with each computed value. By examining the flow of these values, a type system attempts to prove that no type errors can occur. The type system in question, type inference Type inference, or implicit typing, refers to the ability to deduce automatically the type of a value in a programming language. It is a feature present in some strongly statically typed languages. It is often characteristic of — but not limited to — functional programming languages in general. Some languages that include type inference are:, parametric polymorphism In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. The concept of parametric polymorphism applies to both data types and functions. A function that can evaluate to or be applied to values of different types is known as a polymorphic function. A, tail recursion In computer science, tail recursion is a special case of recursion in which the last operation of the function, the tail call, is a recursive call. Such recursions can be easily transformed to iterations. Replacing recursion with iteration, manually or automatically, can drastically decrease the amount of stack space used and improve efficiency, pattern matching In computer science, pattern matching is the act of checking for the presence of the constituents of a given pattern. In contrast to pattern recognition, the pattern is rigidly specified. Such a pattern concerns conventionally either sequences or tree structures. Pattern matching is used to test whether things have a desired structure, to find, first class lexical closures In computer science, a closure is a first-class function with free variables that are bound by the lexical environment. Such a function is said to be "closed over" its free variables. A closure is defined within the scope of its free variables, and the extent of those variables is at least as long as the lifetime of the closure itself, functors (parametric modules) A function object, also called a functor, functional, or functionoid, is a computer programming construct allowing an object to be invoked or called like it was an ordinary function, usually with the same syntax, exception handling Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution, and incremental generational automatic garbage collection In computer science, garbage collection is a form of automatic memory management. It is a special case of resource management, in which the limited resource being managed is memory. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. Garbage collection was.
OCaml is particularly notable for extending ML-style type inference to an object system in a general purpose language. This permits structural subtyping A structural type system is a major class of type system, in which type compatibility and equivalence are determined by the type's structure, and not through explicit declarations. Structural systems are used to determine if types are equivalent, as well as if a type is a subtype of another. It contrasts with nominative systems, where comparisons, where object types are compatible if their method signatures are compatible, regardless of their declared inheritance; an unusual feature in statically-typed languages.
A foreign function interface A foreign function interface is a mechanism by which a program written in one programming language can call routines or make use of services written in another. The term comes from the specification for Common Lisp, which explicitly refers to the language features for inter-language calls as such; the term is also used officially by the Haskell for linking In computer science, a linker or link editor is a program that takes one or more objects generated by a compiler and combines them into a single executable program to C C is a general-purpose computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system primitives is provided, including language support for efficient numerical arrays In computer science, an array data structure or simply array is a data structure consisting of a collection of elements , each identified by one or more integer indices, stored so that the address of each element can be computed from its index tuple by a simple mathematical formula. For example, an array of 10 integer variables, with indices 0 in formats compatible with both C and FORTRAN Fortran is a general-purpose,[note 2] procedural,[note 3] imperative programming language that is especially suited to numeric computation and scientific computing. Originally developed by IBM at their campus in south San Jose, California in the 1950s for scientific and engineering applications, Fortran came to dominate this area of programming. OCaml also supports the creation of libraries of OCaml functions that can be linked to a "main" program in C, so that one could distribute an OCaml library to C programmers who have no knowledge nor installation of OCaml.
The OCaml distribution contains:
- An extensible parser and macro language A macro in computer science is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to an output sequence (also often a sequence of characters) according to a defined procedure. The mapping process that instantiates (transforms) a macro into a specific output sequence is known as macro named Camlp4, which permits the syntax of OCaml to be extended or even replaced
- Lexer and parser tools called ocamllex and ocamlyacc
- Debugger A debugger or debugging tool is a computer program that is used to test and debug other programs . The code to be examined might alternatively be running on an instruction set simulator (ISS), a technique that allows great power in its ability to halt when specific conditions are encountered but which will typically be somewhat slower than which supports stepping backwards to investigate errors
- Documentation generator
- Profiler In software engineering, program profiling, software profiling or simply profiling, a form of dynamic program analysis , is the investigation of a program's behavior using information gathered as the program executes. The usual purpose of this analysis is to determine which sections of a program to optimize - to increase its overall speed, — for measuring performance
- Numerous general purpose libraries In computer science, a library is a collection of subroutines or classes used to develop software
The native code compiler is available for many platforms, including Unix, Microsoft Windows, and Apple Mac OS X. Excellent portability is ensured through native code generation support for major architectures: IA-32, IA-64, AMD64, HP/PA; PowerPC, SPARC, Alpha, MIPS, and StrongARM.
OCaml bytecode and native code programs can be written in a multithreaded style, with preemptive context switching. However, because the garbage collector is not designed for concurrency, symmetric multiprocessing is not supported[2]. OCaml threads in the same process execute by time sharing only.
Code examples
Snippets of OCaml code are most easily studied by entering them into the "top-level". This is an interactive OCaml session that prints the inferred types of resulting or defined expressions. The OCaml top-level is started by simply executing the "ocaml" program:
$ ocaml Objective Caml version 3.09.0 #
Code can then be entered at the "#" prompt. For example, to calculate 1+2*3:
# 1 + 2 * 3;; - : int = 7
OCaml infers the type of the expression to be "int" (a machine-precision integer) and gives the result "7".
Hello World
The following program "hello.ml":
print_endline "Hello world!"
can be compiled into a bytecode executable:
$ ocamlc hello.ml -o hello
or compiled into an optimized native-code executable:
$ ocamlopt hello.ml -o hello
and executed:
$ ./hello Hello world! $
Summing a list of integers
Lists are one of the most fundamental datatypes in OCaml. The following code example defines a recursive function sum that accepts one argument xs. Function recursively iterates over a given list and provides a sum of integer elements. Match statement has similarities with C++ or Java languages' switch element.
let rec sum xs = match xs with | [] -> 0 | x :: xs' -> x + sum xs'
# sum [1;2;3;4;5];; - : int = 15
Another way is to use standard fold function which works with lists.
let sum xs = List.fold_left (+) 0 xs
# sum [1;2;3;4;5];; - : int = 15
Quicksort
OCaml lends itself to the concise expression of recursive algorithms. The following code example implements an algorithm similar to quicksort which sorts a list in increasing order.
let rec qsort = function | [] -> [] | pivot :: rest -> let is_less x = x < pivot in let left, right = List.partition is_less rest in qsort left @ [pivot] @ qsort right
Birthday paradox
The following program calculates the smallest number of people in a room for whom the probability of completely unique birthdays is less than 50% (the so-called birthday paradox, where for 1 person the probability is obviously 100%, for 2 it is 364/365, etc.) (answer = 23).
let year_size = 365.;; let rec birthday_paradox prob people = let prob' = (year_size -. float people) /. year_size *. prob in if prob' < 0.5 then Printf.printf "answer = %d\n" (people+1) else birthday_paradox prob' (people+1) ;; birthday_paradox 1.0 1
Church numerals
The following code defines a Church encoding of natural numbers, with successor (succ) and addition (add). A Church numeral n is a higher-order function that accepts a function f and a value x and applies f to x exactly n times. To convert a Church numeral from a functional value to a string, we pass it a function which prepends the string "S" to its input and the constant string "0".
let zero f x = x let succ n f x = f (n f x) let one = succ zero let two = succ (succ zero) let add n1 n2 f x = n1 f (n2 f x) let to_string n = n (fun k -> "S" ^ k) "0" let _ = to_string (add (succ two) two)
Arbitrary-precision factorial function (libraries)
A variety of libraries are directly accessible from OCaml. For example, OCaml has a built-in library for arbitrary precision arithmetic. As the factorial function grows very rapidly, it quickly overflows machine-precision numbers (typically 32- or 64-bits). Thus, factorial is a suitable candidate for arbitrary-precision arithmetic.
In OCaml, the Num module provides arbitrary-precision arithmetic and can be loaded into a running top-level using:
# #load "nums.cma";; # open Num;;
The factorial function may then be written using the arbitrary-precision numeric operators =/, */ and -/ :
# let rec fact n = if n =/ Int 0 then Int 1 else n */ fact(n -/ Int 1);; val fact : Num.num -> Num.num = <fun>
This function can compute much larger factorials, such as 120!:
# string_of_num (fact (Int 120));; - : string = "6689502913449127057588118054090372586752746333138029810295671352301633 55724496298936687416527198498130815763789321409055253440858940812185989 8481114389650005964960521256960000000000000000000000000000"
Triangle (graphics)
The following program "simple.ml" renders a rotating triangle in 2D using OpenGL:
let () = ignore( Glut.init Sys.argv ); Glut.initDisplayMode ~double_buffer:true (); ignore (Glut.createWindow ~title:"OpenGL Demo"); let angle t = 10. *. t *. t in let render () = GlClear.clear [ `color ]; GlMat.load_identity (); GlMat.rotate ~angle: (angle (Sys.time ())) ~z:1. (); GlDraw.begins `triangles; List.iter GlDraw.vertex2 [-1., -1.; 0., 1.; 1., -1.]; GlDraw.ends (); Glut.swapBuffers () in GlMat.mode `modelview; Glut.displayFunc ~cb:render; Glut.idleFunc ~cb:(Some Glut.postRedisplay); Glut.mainLoop ()
The LablGL bindings to OpenGL are required. The program may then be compiled to bytecode with:
$ ocamlc -I +lablgl lablglut.cma lablgl.cma simple.ml -o simple
or to nativecode with:
$ ocamlopt -I +lablgl lablglut.cmxa lablgl.cmxa simple.ml -o simple
and run:
$ ./simple
Far more sophisticated, high-performance 2D and 3D graphical programs are easily developed in OCaml. Thanks to the use of OpenGL, the resulting programs are not only succinct and efficient but also cross-platform, compiling without any changes on all major platforms.
Fibonacci Sequence
The following code calculates the Fibonacci sequence of a number n inputed. It uses tail recursion and pattern matching.
let rec fib_aux (n, a, b) = match (n, a, b) with | (0, a, b) -> a | _ -> fib_aux (n - 1, a + b, a) let fib n = fib_aux (n, 0, 1)
It is more likely written as:
let fib n = let rec fib_aux (n, a, b) = match (n, a, b) with | (0, a, b) -> a | _ -> fib_aux (n - 1, a + b, a) in fib_aux (n, 0, 1)
Derived languages
MetaOCaml
MetaOCaml[3] is a multi-stage programming extension of OCaml enabling incremental compiling of new machine code during runtime. Under certain circumstances, significant speedups are possible using multi-stage programming, because more detailed information about the data to process is available at runtime than at the regular compile time, so the incremental compiler can optimize away many cases of condition checking etc.
As an example: if at compile time it is known that a certain power function x -> x^n is needed very frequently, but the value of n is known only at runtime, you can use a two-stage power function in MetaOCaml:
let rec power n x = if n = 0 then .<1>. else if even n then sqr (power (n/2) x) else .<.~x *. ~(power (n-1) x)>.
As soon as you know n at runtime, you can create a specialized and very fast power function:
.<fun x -> .~(power 5 .<x>.)>.
The result is:
fun x_1 -> (x_1 * let y_3 = let y_2 = (x_1 * 1) in (y_2 * y_2) in (y_3 * y_3))
The new function is automatically compiled.
Other derived languages
- AtomCaml provides a synchronization primitive for atomic (transactional) execution of code.
- "Emily is a subset of OCaml that uses a design rule verifier to enforce object-capability [security] principles."
- F# is a Microsoft .NET language based on OCaml.
- Fresh OCaml facilitates the manipulation of names and binders.
- GCaml adds extensional polymorphism to OCaml, thus allowing overloading and type-safe marshalling.
- JoCaml integrates constructions for developing concurrent and distributed programs.
- OCamlDuce extends OCaml with features such as XML expressions and regular-expression types.
- OCamlP3l is a parallel programming system based on OCaml and the P3L language
Software written in OCaml
- Orpie, is a command-line RPN calculator
- FFTW – a software library for computing discrete Fourier transforms. Several C routines have been generated by an OCaml program named
genfft. - Unison – a file synchronization program to synchronize files between two directories.
- Mldonkey – a peer to peer client based on the EDonkey network.
- GeneWeb – free open source multi-platform genealogy software.
- The haXe compiler – a free open source compiler for the haXe programming language.
- Frama-c – a framework for C programs analysis.
- Liquidsoap – Liquidsoap is the audio stream generator of the Savonet project, notably used for generating the stream of netradios. [1]
- Coccinelle – Coccinelle is a program matching and transformation engine which provides the SmPL language (Semantic Patch Language) for specifying desired matches and transformations in C code. [2]
- CSIsat – a Tool for LA+EUF Interpolation.
See also
- Caml and Caml Light, languages from which OCaml evolved
- Standard ML, another popular dialect of ML
- Extensible ML, another object-oriented dialect of ML
- O'Haskell, an object-oriented extension to the functional language Haskell
References
External links
- Caml language family official website
- OCaml tutorial for C, C++, Java and Perl programmers
- A basic OCaml tutorial
- A Tutorial with a practical approach.
- OCaml Batteries Included, a community-built standard library for OCaml
- OCaml-Java, OCaml for Java
- OCamIL, an OCaml compiler for Microsoft .NET
- Comparison of the speed of various languages including Ocaml
- LablGL and LablGTK OpenGL+ bindings (LablGL) and GTK+ bindings (LablGTK)
- Newest Ocaml Projects on Sourceforge
- MetaOCaml home page
- the GODI package manager for OCaml
- OCamlcore Planet
- OCamlForge
- Ocamlwizard
Categories: ML programming language family | Functional languages | Object-oriented programming languages | Extensible syntax programming languages | Cross-platform software | OCaml software | Programming languages created in 1996
Personal tools
- New features
- Log in / create account
Namespaces
- Article
- Discussion
Variants
Views
- Read
- Edit
- View history
Actions
Navigation
- Main page
- Contents
- Featured content
- Current events
- Random article
Interaction
- About Wikipedia
- Community portal
- Recent changes
- Contact Wikipedia
- Donate to Wikipedia
- Help
Toolbox
- What links here
- Related changes
- Upload file
- Special pages
- Permanent link
- Cite this page
Print/export
- Create a book
- Download as PDF
- Printable version
Languages
- Català
- Česky
- Deutsch
- Ελληνικά
- Español
- Français
- Galego
- 한국어
- Italiano
- ქართული
- Latina
- Nederlands
- 日本語
- Norsk (bokmål)
- Norsk (nynorsk)
- Polski
- Português
- Русский
- Simple English
- Slovenščina
- Suomi
- Тоҷикӣ
- Türkçe
- Українська
- Tiếng Việt
- 中文