Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > fb18813323b88f9a6e869238ab603257 > files > 12

ocaml-doc-4.07.1-2.mga7.noarch.rpm

<!DOCTYPE html>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="generator" content="hevea 2.32">

  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<link rel="stylesheet" type="text/css" href="manual.css">
<title>Chapter&#XA0;1&#XA0;&#XA0;The core language</title>
</head>
<body>
<a href="foreword.html"><img src="previous_motif.svg" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.svg" alt="Up"></a>
<a href="moduleexamples.html"><img src="next_motif.svg" alt="Next"></a>
<hr>
<h1 class="chapter" id="sec7">Chapter&#XA0;1&#XA0;&#XA0;The core language</h1>
<ul>
<li><a href="coreexamples.html#sec8">1.1&#XA0;&#XA0;Basics</a>
</li><li><a href="coreexamples.html#sec9">1.2&#XA0;&#XA0;Data types</a>
</li><li><a href="coreexamples.html#sec10">1.3&#XA0;&#XA0;Functions as values</a>
</li><li><a href="coreexamples.html#sec11">1.4&#XA0;&#XA0;Records and variants</a>
</li><li><a href="coreexamples.html#sec13">1.5&#XA0;&#XA0;Imperative features</a>
</li><li><a href="coreexamples.html#sec14">1.6&#XA0;&#XA0;Exceptions</a>
</li><li><a href="coreexamples.html#sec15">1.7&#XA0;&#XA0;Symbolic processing of expressions</a>
</li><li><a href="coreexamples.html#sec16">1.8&#XA0;&#XA0;Pretty-printing</a>
</li><li><a href="coreexamples.html#sec17">1.9&#XA0;&#XA0;Standalone OCaml programs</a>
</li></ul>
<p> <a id="c:core-xamples"></a>

</p><p>This part of the manual is a tutorial introduction to the
OCaml language. A good familiarity with programming in a conventional
languages (say, C or Java) is assumed, but no prior exposure to
functional languages is required. The present chapter introduces the
core language. Chapter&#XA0;<a href="moduleexamples.html#c%3Amoduleexamples">2</a> deals with the
module system, chapter&#XA0;<a href="objectexamples.html#c%3Aobjectexamples">3</a> with the
object-oriented features, chapter&#XA0;<a href="lablexamples.html#c%3Alabl-examples">4</a> with
extensions to the core language (labeled arguments and polymorphic
variants), and chapter&#XA0;<a href="advexamples.html#c%3Aadvexamples">6</a> gives some advanced examples.</p>
<h2 class="section" id="sec8">1.1&#XA0;&#XA0;Basics</h2>
<p>For this overview of OCaml, we use the interactive system, which
is started by running <span class="c003">ocaml</span> from the Unix shell, or by launching the
<span class="c003">OCamlwin.exe</span> application under Windows. This tutorial is presented
as the transcript of a session with the interactive system:
lines starting with <span class="c003">#</span> represent user input; the system responses are
printed below, without a leading <span class="c003">#</span>.</p><p>Under the interactive system, the user types OCaml phrases terminated
by <span class="c003">;;</span> in response to the <span class="c003">#</span> prompt, and the system compiles them
on the fly, executes them, and prints the outcome of evaluation.
Phrases are either simple expressions, or <span class="c003">let</span> definitions of
identifiers (either values or functions).


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> 1+2*3;;
</div><div class="caml-output ok">- : int = 7
</div></pre>

<pre><div class="caml-input"> let pi = 4.0 *. atan 1.0;;
</div><div class="caml-output ok">val pi : float = 3.14159265358979312
</div></pre>

<pre><div class="caml-input"> let square x = x *. x;;
</div><div class="caml-output ok">val square : float -&gt; float = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> square (sin pi) +. square (cos pi);;
</div><div class="caml-output ok">- : float = 1.
</div></pre>


</div><p>

The OCaml system computes both the value and the type for
each phrase. Even function parameters need no explicit type declaration:
the system infers their types from their usage in the
function. Notice also that integers and floating-point numbers are
distinct types, with distinct operators: <span class="c003">+</span> and <span class="c003">*</span> operate on
integers, but <span class="c003">+.</span> and <span class="c003">*.</span> operate on floats.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> <U>1.0</U> * 2;;
</div><div class="caml-output error">Error: This expression has type float but an expression was expected of type
         int
</div></pre>


</div><p>Recursive functions are defined with the <span class="c003">let rec</span> binding:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let rec fib n =
    if n &lt; 2 then n else fib (n-1) + fib (n-2);;
</div><div class="caml-output ok">val fib : int -&gt; int = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> fib 10;;
</div><div class="caml-output ok">- : int = 55
</div></pre>


</div>
<h2 class="section" id="sec9">1.2&#XA0;&#XA0;Data types</h2>
<p>In addition to integers and floating-point numbers, OCaml offers the
usual basic data types: booleans, characters, and immutable character strings.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> (1 &lt; 2) = false;;
</div><div class="caml-output ok">- : bool = false
</div></pre>

<pre><div class="caml-input"> 'a';;
</div><div class="caml-output ok">- : char = 'a'
</div></pre>

<pre><div class="caml-input"> "Hello world";;
</div><div class="caml-output ok">- : string = "Hello world"
</div></pre>


</div><p>Predefined data structures include tuples, arrays, and lists. There are also
general mechanisms for defining your own data structures, such as records and
variants, which will be covered in more detail later; for now, we concentrate
on lists. Lists are either given in extension as a bracketed list of
semicolon-separated elements, or built from the empty list <span class="c003">[]</span>
(pronounce &#X201C;nil&#X201D;) by adding elements in front using the <span class="c003">::</span>
(&#X201C;cons&#X201D;) operator.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let l = ["is"; "a"; "tale"; "told"; "etc."];;
</div><div class="caml-output ok">val l : string list = ["is"; "a"; "tale"; "told"; "etc."]
</div></pre>

<pre><div class="caml-input"> "Life" :: l;;
</div><div class="caml-output ok">- : string list = ["Life"; "is"; "a"; "tale"; "told"; "etc."]
</div></pre>


</div><p>

As with all other OCaml data structures, lists do not need to be
explicitly allocated and deallocated from memory: all memory
management is entirely automatic in OCaml. Similarly, there is no
explicit handling of pointers: the OCaml compiler silently introduces
pointers where necessary.</p><p>As with most OCaml data structures, inspecting and destructuring lists
is performed by pattern-matching. List patterns have exactly the same
form as list expressions, with identifiers representing unspecified
parts of the list. As an example, here is insertion sort on a list:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let rec sort lst =
    match lst with
      [] -&gt; []
    | head :: tail -&gt; insert head (sort tail)
  and insert elt lst =
    match lst with
      [] -&gt; [elt]
    | head :: tail -&gt; if elt &lt;= head then elt :: lst else head :: insert elt tail
  ;;
</div><div class="caml-output ok">val sort : 'a list -&gt; 'a list = &lt;fun&gt;
val insert : 'a -&gt; 'a list -&gt; 'a list = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> sort l;;
</div><div class="caml-output ok">- : string list = ["a"; "etc."; "is"; "tale"; "told"]
</div></pre>


</div><p>The type inferred for <span class="c003">sort</span>, <span class="c003">'a list -&gt; 'a list</span>, means that <span class="c003">sort</span>
can actually apply to lists of any type, and returns a list of the
same type. The type <span class="c003">'a</span> is a <em>type variable</em>, and stands for any
given type. The reason why <span class="c003">sort</span> can apply to lists of any type is
that the comparisons (<span class="c003">=</span>, <span class="c003">&lt;=</span>, etc.) are <em>polymorphic</em> in OCaml:
they operate between any two values of the same type. This makes
<span class="c003">sort</span> itself polymorphic over all list types.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> sort [6;2;5;3];;
</div><div class="caml-output ok">- : int list = [2; 3; 5; 6]
</div></pre>

<pre><div class="caml-input"> sort [3.14; 2.718];;
</div><div class="caml-output ok">- : float list = [2.718; 3.14]
</div></pre>


</div><p>The <span class="c003">sort</span> function above does not modify its input list: it builds
and returns a new list containing the same elements as the input list,
in ascending order. There is actually no way in OCaml to modify
a list in-place once it is built: we say that lists are <em>immutable</em>
data structures. Most OCaml data structures are immutable, but a few
(most notably arrays) are <em>mutable</em>, meaning that they can be
modified in-place at any time.</p><p>The OCaml notation for the type of a function with multiple arguments is <br>
<span class="c003">arg1_type -&gt; arg2_type -&gt; ... -&gt; return_type</span>. For example,
the type inferred for <span class="c003">insert</span>, <span class="c003">'a -&gt; 'a list -&gt; 'a list</span>, means that <span class="c003">insert</span>
takes two arguments, an element of any type <span class="c003">'a</span> and a list with elements of
the same type <span class="c003">'a</span> and returns a list of the same type.
</p>
<h2 class="section" id="sec10">1.3&#XA0;&#XA0;Functions as values</h2>
<p>OCaml is a functional language: functions in the full mathematical
sense are supported and can be passed around freely just as any other
piece of data. For instance, here is a <span class="c003">deriv</span> function that takes any
float function as argument and returns an approximation of its
derivative function:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let deriv f dx = function x -&gt; (f (x +. dx) -. f x) /. dx;;
</div><div class="caml-output ok">val deriv : (float -&gt; float) -&gt; float -&gt; float -&gt; float = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> let sin' = deriv sin 1e-6;;
</div><div class="caml-output ok">val sin' : float -&gt; float = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> sin' pi;;
</div><div class="caml-output ok">- : float = -1.00000000013961143
</div></pre>


</div><p>

Even function composition is definable:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let compose f g = function x -&gt; f (g x);;
</div><div class="caml-output ok">val compose : ('a -&gt; 'b) -&gt; ('c -&gt; 'a) -&gt; 'c -&gt; 'b = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> let cos2 = compose square cos;;
</div><div class="caml-output ok">val cos2 : float -&gt; float = &lt;fun&gt;
</div></pre>


</div><p>Functions that take other functions as arguments are called
&#X201C;functionals&#X201D;, or &#X201C;higher-order functions&#X201D;. Functionals are
especially useful to provide iterators or similar generic operations
over a data structure. For instance, the standard OCaml library
provides a <span class="c003">List.map</span> functional that applies a given function to each
element of a list, and returns the list of the results:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> List.map (function n -&gt; n * 2 + 1) [0;1;2;3;4];;
</div><div class="caml-output ok">- : int list = [1; 3; 5; 7; 9]
</div></pre>


</div><p>

This functional, along with a number of other list and array
functionals, is predefined because it is often useful, but there is
nothing magic with it: it can easily be defined as follows.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let rec map f l =
    match l with
      [] -&gt; []
    | hd :: tl -&gt; f hd :: map f tl;;
</div><div class="caml-output ok">val map : ('a -&gt; 'b) -&gt; 'a list -&gt; 'b list = &lt;fun&gt;
</div></pre>


</div>
<h2 class="section" id="sec11">1.4&#XA0;&#XA0;Records and variants</h2>
<p>

<a id="s:tut-recvariants"></a></p><p>User-defined data structures include records and variants. Both are
defined with the <span class="c003">type</span> declaration. Here, we declare a record type to
represent rational numbers.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> type ratio = {num: int; denom: int};;
</div><div class="caml-output ok">type ratio = { num : int; denom : int; }
</div></pre>

<pre><div class="caml-input"> let add_ratio r1 r2 =
    {num = r1.num * r2.denom + r2.num * r1.denom;
     denom = r1.denom * r2.denom};;
</div><div class="caml-output ok">val add_ratio : ratio -&gt; ratio -&gt; ratio = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> add_ratio {num=1; denom=3} {num=2; denom=5};;
</div><div class="caml-output ok">- : ratio = {num = 11; denom = 15}
</div></pre>


</div><p>

Record fields can also be accessed through pattern-matching:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let integer_part r =
    match r with
      {num=num; denom=denom} -&gt; num / denom;;
</div><div class="caml-output ok">val integer_part : ratio -&gt; int = &lt;fun&gt;
</div></pre>


</div><p>

Since there is only one case in this pattern matching, it
is safe to expand directly the argument <span class="c003">r</span> in a record pattern:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let integer_part {num=num; denom=denom} = num / denom;;
</div><div class="caml-output ok">val integer_part : ratio -&gt; int = &lt;fun&gt;
</div></pre>


</div><p>

Unneeded fields can be omitted:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let get_denom {denom=denom} = denom;;
</div><div class="caml-output ok">val get_denom : ratio -&gt; int = &lt;fun&gt;
</div></pre>


</div><p>

Optionally, missing fields can be made explicit by ending the list of
fields with a trailing wildcard <span class="c003">_</span>::


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let get_num {num=num; _ } = num;;
</div><div class="caml-output ok">val get_num : ratio -&gt; int = &lt;fun&gt;
</div></pre>


</div><p>

When both sides of the <span class="c003">=</span> sign are the same, it is possible to avoid
repeating the field name by eliding the <span class="c003">=field</span> part:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let integer_part {num; denom} = num / denom;;
</div><div class="caml-output ok">val integer_part : ratio -&gt; int = &lt;fun&gt;
</div></pre>


</div><p>

This short notation for fields also works when constructing records:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let ratio num denom = {num; denom};;
</div><div class="caml-output ok">val ratio : int -&gt; int -&gt; ratio = &lt;fun&gt;
</div></pre>


</div><p>

At last, it is possible to update few fields of a record at once:


</p><div class="caml-example toplevel">

<pre><div class="caml-input">   let integer_product integer ratio = { ratio with num = integer * ratio.num };;
</div><div class="caml-output ok">val integer_product : int -&gt; ratio -&gt; ratio = &lt;fun&gt;
</div></pre>


</div><p>

With this functional update notation, the record on the left-hand side
of <span class="c003">with</span> is copied except for the fields on the right-hand side which
are updated.</p><p>The declaration of a variant type lists all possible forms for values
of that type. Each case is identified by a name, called a constructor,
which serves both for constructing values of the variant type and
inspecting them by pattern-matching. Constructor names are capitalized
to distinguish them from variable names (which must start with a
lowercase letter). For instance, here is a variant
type for doing mixed arithmetic (integers and floats):


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> type number = Int of int | Float of float | Error;;
</div><div class="caml-output ok">type number = Int of int | Float of float | Error
</div></pre>


</div><p>

This declaration expresses that a value of type <span class="c003">number</span> is either an
integer, a floating-point number, or the constant <span class="c003">Error</span> representing
the result of an invalid operation (e.g. a division by zero).</p><p>Enumerated types are a special case of variant types, where all
alternatives are constants:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> type sign = Positive | Negative;;
</div><div class="caml-output ok">type sign = Positive | Negative
</div></pre>

<pre><div class="caml-input"> let sign_int n = if n &gt;= 0 then Positive else Negative;;
</div><div class="caml-output ok">val sign_int : int -&gt; sign = &lt;fun&gt;
</div></pre>


</div><p>To define arithmetic operations for the <span class="c003">number</span> type, we use
pattern-matching on the two numbers involved:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let add_num n1 n2 =
    match (n1, n2) with
      (Int i1, Int i2) -&gt;
        (* Check for overflow of integer addition *)
        if sign_int i1 = sign_int i2 &amp;&amp; sign_int (i1 + i2) &lt;&gt; sign_int i1
        then Float(float i1 +. float i2)
        else Int(i1 + i2)
    | (Int i1, Float f2) -&gt; Float(float i1 +. f2)
    | (Float f1, Int i2) -&gt; Float(f1 +. float i2)
    | (Float f1, Float f2) -&gt; Float(f1 +. f2)
    | (Error, _) -&gt; Error
    | (_, Error) -&gt; Error;;
</div><div class="caml-output ok">val add_num : number -&gt; number -&gt; number = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> add_num (Int 123) (Float 3.14159);;
</div><div class="caml-output ok">- : number = Float 126.14159
</div></pre>


</div><p>Another interesting example of variant type is the built-in
<span class="c003">'a option</span> type which represents either a value of type <span class="c003">'a</span> or an
absence of value:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> type 'a option = Some of 'a | None;;
</div><div class="caml-output ok">type 'a option = Some of 'a | None
</div></pre>


</div><p>

This type is particularly useful when defining function that can
fail in common situations, for instance


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let safe_square_root x = if x &gt; 0. then Some(sqrt x) else None;;
</div><div class="caml-output ok">val safe_square_root : float -&gt; float option = &lt;fun&gt;
</div></pre>


</div><p>The most common usage of variant types is to describe recursive data
structures. Consider for example the type of binary trees:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> type 'a btree = Empty | Node of 'a * 'a btree * 'a btree;;
</div><div class="caml-output ok">type 'a btree = Empty | Node of 'a * 'a btree * 'a btree
</div></pre>


</div><p>

This definition reads as follows: a binary tree containing values of
type <span class="c003">'a</span> (an arbitrary type) is either empty, or is a node containing
one value of type <span class="c003">'a</span> and two subtrees also containing values of type
<span class="c003">'a</span>, that is, two <span class="c003">'a btree</span>.</p><p>Operations on binary trees are naturally expressed as recursive functions
following the same structure as the type definition itself. For
instance, here are functions performing lookup and insertion in
ordered binary trees (elements increase from left to right):


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let rec member x btree =
    match btree with
      Empty -&gt; false
    | Node(y, left, right) -&gt;
        if x = y then true else
        if x &lt; y then member x left else member x right;;
</div><div class="caml-output ok">val member : 'a -&gt; 'a btree -&gt; bool = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> let rec insert x btree =
    match btree with
      Empty -&gt; Node(x, Empty, Empty)
    | Node(y, left, right) -&gt;
        if x &lt;= y then Node(y, insert x left, right)
                  else Node(y, left, insert x right);;
</div><div class="caml-output ok">val insert : 'a -&gt; 'a btree -&gt; 'a btree = &lt;fun&gt;
</div></pre>


</div>
<h3 class="subsection" id="sec12">1.4.1&#XA0;&#XA0;Record and variant disambiguation</h3>
<p>
( This subsection can be skipped on the first reading )</p><p>Astute readers may have wondered what happens when two or more record
fields or constructors share the same name</p><div class="caml-example toplevel">

<pre><div class="caml-input"> type first_record  = { x:int; y:int; z:int }
  type middle_record = { x:int; z:int }
  type last_record   = { x:int };;
</div>
</pre>

<pre><div class="caml-input"> type first_variant = A | B | C
  type last_variant  = A;;
</div>
</pre>


</div><p>The answer is that when confronted with multiple options, OCaml tries to
use locally available information to disambiguate between the various fields
and constructors. First, if the type of the record or variant is known,
OCaml can pick unambiguously the corresponding field or constructor.
For instance:</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let look_at_x_then_z (r:first_record) =
    let x = r.x in
    x + r.z;;
</div><div class="caml-output ok">val look_at_x_then_z : first_record -&gt; int = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> let permute (x:first_variant) = match x with
    | A -&gt; (B:first_variant)
    | B -&gt; A
    | C -&gt; C;;
</div><div class="caml-output ok">val permute : first_variant -&gt; first_variant = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> type wrapped = First of first_record
  let f (First r) = r, r.x;;
</div><div class="caml-output ok">type wrapped = First of first_record
val f : wrapped -&gt; first_record * int = &lt;fun&gt;
</div></pre>


</div><p>In the first example, <span class="c003">(r:first_record)</span> is an explicit annotation
telling OCaml that the type of <span class="c003">r</span> is <span class="c003">first_record</span>. With this
annotation, Ocaml knows that <span class="c003">r.x</span> refers to the <span class="c003">x</span> field of the first
record type. Similarly, the type annotation in the second example makes
it clear to OCaml that the constructors <span class="c003">A</span>, <span class="c003">B</span> and <span class="c003">C</span> come from the
first variant type. Contrarily, in the last example, OCaml has inferred
by itself that the type of <span class="c003">r</span> can only be <span class="c003">first_record</span> and there are
no needs for explicit type annotations.</p><p>Those explicit type annotations can in fact be used anywhere.
Most of the time they are unnecessary, but they are useful to guide
disambiguation, to debug unexpected type errors, or combined with some
of the more advanced features of OCaml described in later chapters.</p><p>Secondly, for records, OCaml can also deduce the right record type by
looking at the whole set of fields used in a expression or pattern:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let project_and_rotate {x;y; _ } = { x= - y; y = x ; z = 0} ;;
</div><div class="caml-output ok">val project_and_rotate : first_record -&gt; first_record = &lt;fun&gt;
</div></pre>


</div><p>

Since the fields <span class="c003">x</span> and <span class="c003">y</span> can only appear simultaneously in the first
record type, OCaml infers that the type of <span class="c003">project_and_rotate</span> is
<span class="c003">first_record -&gt; first_record</span>.</p><p>In last resort, if there is not enough information to disambiguate between
different fields or constructors, Ocaml picks the last defined type
amongst all locally valid choices:</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let look_at_xz {x;z} = x;;
</div><div class="caml-output ok">val look_at_xz : middle_record -&gt; int = &lt;fun&gt;
</div></pre>


</div><p>Here, OCaml has inferred that the possible choices for the type of
<span class="c003">{x;z}</span> are <span class="c003">first_record</span> and <span class="c003">middle_record</span>, since the type
<span class="c003">last_record</span> has no field <span class="c003">z</span>. Ocaml then picks the type <span class="c003">middle_record</span>
as the last defined type between the two possibilities.</p><p>Beware that this last resort disambiguation is local: once Ocaml has
chosen a disambiguation, it sticks to this choice, even if it leads to
an ulterior type error:</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let look_at_x_then_y r =
    let x = r.x in (* Ocaml deduces [r: last_record] *)
    x + r.<U>y</U>;;
</div><div class="caml-output error">Error: This expression has type last_record
       The field y does not belong to type last_record
</div></pre>

<pre><div class="caml-input"> let is_a_or_b x = match x with
    | A -&gt; true (* OCaml infers [x: last_variant] *)
    | <U>B</U> -&gt; true;;
</div><div class="caml-output error">Error: This variant pattern is expected to have type last_variant
       The constructor B does not belong to type last_variant
</div></pre>


</div><p>Moreover, being the last defined type is a quite unstable position that
may change surreptitiously after adding or moving around a type
definition, or after opening a module (see chapter <a href="moduleexamples.html#c%3Amoduleexamples">2</a>).
Consequently, adding explicit type annotations to guide disambiguation is
more robust than relying on the last defined type disambiguation.</p>
<h2 class="section" id="sec13">1.5&#XA0;&#XA0;Imperative features</h2>
<p>Though all examples so far were written in purely applicative style,
OCaml is also equipped with full imperative features. This includes the
usual <span class="c003">while</span> and <span class="c003">for</span> loops, as well as mutable data structures such
as arrays. Arrays are either created by listing semicolon-separated element
values between <span class="c003">[|</span> and <span class="c003">|]</span> brackets, or allocated and initialized with the
<span class="c003">Array.make</span> function, then filled up later by assignments. For instance, the
function below sums two vectors (represented as float arrays) componentwise.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let add_vect v1 v2 =
    let len = min (Array.length v1) (Array.length v2) in
    let res = Array.make len 0.0 in
    for i = 0 to len - 1 do
      res.(i) &lt;- v1.(i) +. v2.(i)
    done;
    res;;
</div><div class="caml-output ok">val add_vect : float array -&gt; float array -&gt; float array = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> add_vect [| 1.0; 2.0 |] [| 3.0; 4.0 |];;
</div><div class="caml-output ok">- : float array = [|4.; 6.|]
</div></pre>


</div><p>Record fields can also be modified by assignment, provided they are
declared <span class="c003">mutable</span> in the definition of the record type:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> type mutable_point = { mutable x: float; mutable y: float };;
</div><div class="caml-output ok">type mutable_point = { mutable x : float; mutable y : float; }
</div></pre>

<pre><div class="caml-input"> let translate p dx dy =
    p.x &lt;- p.x +. dx; p.y &lt;- p.y +. dy;;
</div><div class="caml-output ok">val translate : mutable_point -&gt; float -&gt; float -&gt; unit = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> let mypoint = { x = 0.0; y = 0.0 };;
</div><div class="caml-output ok">val mypoint : mutable_point = {x = 0.; y = 0.}
</div></pre>

<pre><div class="caml-input"> translate mypoint 1.0 2.0;;
</div><div class="caml-output ok">- : unit = ()
</div></pre>

<pre><div class="caml-input"> mypoint;;
</div><div class="caml-output ok">- : mutable_point = {x = 1.; y = 2.}
</div></pre>


</div><p>OCaml has no built-in notion of variable &#X2013; identifiers whose current
value can be changed by assignment. (The <span class="c003">let</span> binding is not an
assignment, it introduces a new identifier with a new scope.)
However, the standard library provides references, which are mutable
indirection cells, with operators <span class="c003">!</span> to fetch
the current contents of the reference and <span class="c003">:=</span> to assign the contents.
Variables can then be emulated by <span class="c003">let</span>-binding a reference. For
instance, here is an in-place insertion sort over arrays:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let insertion_sort a =
    for i = 1 to Array.length a - 1 do
      let val_i = a.(i) in
      let j = ref i in
      while !j &gt; 0 &amp;&amp; val_i &lt; a.(!j - 1) do
        a.(!j) &lt;- a.(!j - 1);
        j := !j - 1
      done;
      a.(!j) &lt;- val_i
    done;;
</div><div class="caml-output ok">val insertion_sort : 'a array -&gt; unit = &lt;fun&gt;
</div></pre>


</div><p>References are also useful to write functions that maintain a current
state between two calls to the function. For instance, the following
pseudo-random number generator keeps the last returned number in a
reference:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let current_rand = ref 0;;
</div><div class="caml-output ok">val current_rand : int ref = {contents = 0}
</div></pre>

<pre><div class="caml-input"> let random () =
    current_rand := !current_rand * 25713 + 1345;
    !current_rand;;
</div><div class="caml-output ok">val random : unit -&gt; int = &lt;fun&gt;
</div></pre>


</div><p>Again, there is nothing magical with references: they are implemented as
a single-field mutable record, as follows.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> type 'a ref = { mutable contents: 'a };;
</div><div class="caml-output ok">type 'a ref = { mutable contents : 'a; }
</div></pre>

<pre><div class="caml-input"> let ( ! ) r = r.contents;;
</div><div class="caml-output ok">val ( ! ) : 'a ref -&gt; 'a = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> let ( := ) r newval = r.contents &lt;- newval;;
</div><div class="caml-output ok">val ( := ) : 'a ref -&gt; 'a -&gt; unit = &lt;fun&gt;
</div></pre>


</div><p>In some special cases, you may need to store a polymorphic function in
a data structure, keeping its polymorphism. Doing this requires
user-provided type annotations, since polymorphism is only introduced
automatically for global definitions. However, you can explicitly give
polymorphic types to record fields.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> type idref = { mutable id: 'a. 'a -&gt; 'a };;
</div><div class="caml-output ok">type idref = { mutable id : 'a. 'a -&gt; 'a; }
</div></pre>

<pre><div class="caml-input"> let r = {id = fun x -&gt; x};;
</div><div class="caml-output ok">val r : idref = {id = &lt;fun&gt;}
</div></pre>

<pre><div class="caml-input"> let g s = (s.id 1, s.id true);;
</div><div class="caml-output ok">val g : idref -&gt; int * bool = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> r.id &lt;- (fun x -&gt; print_string "called id\n"; x);;
</div><div class="caml-output ok">- : unit = ()
</div></pre>

<pre><div class="caml-input"> g r;;
</div><div class="caml-output ok">called id
called id
- : int * bool = (1, true)
</div></pre>


</div>
<h2 class="section" id="sec14">1.6&#XA0;&#XA0;Exceptions</h2>
<p>OCaml provides exceptions for signalling and handling exceptional
conditions. Exceptions can also be used as a general-purpose non-local
control structure, although this should not be overused since it can
make the code harder to understand. Exceptions are declared with the
<span class="c003">exception</span> construct, and signalled with the <span class="c003">raise</span> operator. For instance,
the function below for taking the head of a list uses an exception to
signal the case where an empty list is given.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> exception Empty_list;;
</div><div class="caml-output ok">exception Empty_list
</div></pre>

<pre><div class="caml-input"> let head l =
    match l with
      [] -&gt; raise Empty_list
    | hd :: tl -&gt; hd;;
</div><div class="caml-output ok">val head : 'a list -&gt; 'a = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> head [1;2];;
</div><div class="caml-output ok">- : int = 1
</div></pre>

<pre><div class="caml-input"> head [];;
</div><div class="caml-output ok">Exception: Empty_list.
</div></pre>


</div><p>Exceptions are used throughout the standard library to signal cases
where the library functions cannot complete normally. For instance,
the <span class="c003">List.assoc</span> function, which returns the data associated with a
given key in a list of (key, data) pairs, raises the predefined
exception <span class="c003">Not_found</span> when the key does not appear in the list:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> List.assoc 1 [(0, "zero"); (1, "one")];;
</div><div class="caml-output ok">- : string = "one"
</div></pre>

<pre><div class="caml-input"> List.assoc 2 [(0, "zero"); (1, "one")];;
</div><div class="caml-output ok">Exception: Not_found.
</div></pre>


</div><p>Exceptions can be trapped with the <span class="c003">try</span>&#X2026;<span class="c003">with</span> construct:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let name_of_binary_digit digit =
    try
      List.assoc digit [0, "zero"; 1, "one"]
    with Not_found -&gt;
      "not a binary digit";;
</div><div class="caml-output ok">val name_of_binary_digit : int -&gt; string = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> name_of_binary_digit 0;;
</div><div class="caml-output ok">- : string = "zero"
</div></pre>

<pre><div class="caml-input"> name_of_binary_digit (-1);;
</div><div class="caml-output ok">- : string = "not a binary digit"
</div></pre>


</div><p>The <span class="c003">with</span> part does pattern matching on the
exception value with the same syntax and behavior as <span class="c003">match</span>. Thus,
several exceptions can be caught by one
<span class="c003">try</span>&#X2026;<span class="c003">with</span> construct. Also, finalization can be performed by
trapping all exceptions, performing the finalization, then re-raising
the exception:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let temporarily_set_reference ref newval funct =
    let oldval = !ref in
    try
      ref := newval;
      let res = funct () in
      ref := oldval;
      res
    with x -&gt;
      ref := oldval;
      raise x;;
</div><div class="caml-output ok">val temporarily_set_reference : 'a ref -&gt; 'a -&gt; (unit -&gt; 'b) -&gt; 'b = &lt;fun&gt;
</div></pre>


</div>
<h2 class="section" id="sec15">1.7&#XA0;&#XA0;Symbolic processing of expressions</h2>
<p>We finish this introduction with a more complete example
representative of the use of OCaml for symbolic processing: formal
manipulations of arithmetic expressions containing variables. The
following variant type describes the expressions we shall manipulate:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> type expression =
      Const of float
    | Var of string
    | Sum of expression * expression    (* e1 + e2 *)
    | Diff of expression * expression   (* e1 - e2 *)
    | Prod of expression * expression   (* e1 * e2 *)
    | Quot of expression * expression   (* e1 / e2 *)
  ;;
</div><div class="caml-output ok">type expression =
    Const of float
  | Var of string
  | Sum of expression * expression
  | Diff of expression * expression
  | Prod of expression * expression
  | Quot of expression * expression
</div></pre>


</div><p>We first define a function to evaluate an expression given an
environment that maps variable names to their values. For simplicity,
the environment is represented as an association list.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> exception Unbound_variable of string;;
</div><div class="caml-output ok">exception Unbound_variable of string
</div></pre>

<pre><div class="caml-input"> let rec eval env exp =
    match exp with
      Const c -&gt; c
    | Var v -&gt;
        (try List.assoc v env with Not_found -&gt; raise (Unbound_variable v))
    | Sum(f, g) -&gt; eval env f +. eval env g
    | Diff(f, g) -&gt; eval env f -. eval env g
    | Prod(f, g) -&gt; eval env f *. eval env g
    | Quot(f, g) -&gt; eval env f /. eval env g;;
</div><div class="caml-output ok">val eval : (string * float) list -&gt; expression -&gt; float = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> eval [("x", 1.0); ("y", 3.14)] (Prod(Sum(Var "x", Const 2.0), Var "y"));;
</div><div class="caml-output ok">- : float = 9.42
</div></pre>


</div><p>Now for a real symbolic processing, we define the derivative of an
expression with respect to a variable <span class="c003">dv</span>:


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let rec deriv exp dv =
    match exp with
      Const c -&gt; Const 0.0
    | Var v -&gt; if v = dv then Const 1.0 else Const 0.0
    | Sum(f, g) -&gt; Sum(deriv f dv, deriv g dv)
    | Diff(f, g) -&gt; Diff(deriv f dv, deriv g dv)
    | Prod(f, g) -&gt; Sum(Prod(f, deriv g dv), Prod(deriv f dv, g))
    | Quot(f, g) -&gt; Quot(Diff(Prod(deriv f dv, g), Prod(f, deriv g dv)),
                         Prod(g, g))
  ;;
</div><div class="caml-output ok">val deriv : expression -&gt; string -&gt; expression = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> deriv (Quot(Const 1.0, Var "x")) "x";;
</div><div class="caml-output ok">- : expression =
Quot (Diff (Prod (Const 0., Var "x"), Prod (Const 1., Const 1.)),
 Prod (Var "x", Var "x"))
</div></pre>


</div>
<h2 class="section" id="sec16">1.8&#XA0;&#XA0;Pretty-printing</h2>
<p>As shown in the examples above, the internal representation (also
called <em>abstract syntax</em>) of expressions quickly becomes hard to
read and write as the expressions get larger. We need a printer and a
parser to go back and forth between the abstract syntax and the <em>concrete syntax</em>, which in the case of expressions is the familiar
algebraic notation (e.g. <span class="c003">2*x+1</span>).</p><p>For the printing function, we take into account the usual precedence
rules (i.e. <span class="c003">*</span> binds tighter than <span class="c003">+</span>) to avoid printing unnecessary
parentheses. To this end, we maintain the current operator precedence
and print parentheses around an operator only if its precedence is
less than the current precedence.


</p><div class="caml-example toplevel">

<pre><div class="caml-input"> let print_expr exp =
    (* Local function definitions *)
    let open_paren prec op_prec =
      if prec &gt; op_prec then print_string "(" in
    let close_paren prec op_prec =
      if prec &gt; op_prec then print_string ")" in
    let rec print prec exp =     (* prec is the current precedence *)
      match exp with
        Const c -&gt; print_float c
      | Var v -&gt; print_string v
      | Sum(f, g) -&gt;
          open_paren prec 0;
          print 0 f; print_string " + "; print 0 g;
          close_paren prec 0
      | Diff(f, g) -&gt;
          open_paren prec 0;
          print 0 f; print_string " - "; print 1 g;
          close_paren prec 0
      | Prod(f, g) -&gt;
          open_paren prec 2;
          print 2 f; print_string " * "; print 2 g;
          close_paren prec 2
      | Quot(f, g) -&gt;
          open_paren prec 2;
          print 2 f; print_string " / "; print 3 g;
          close_paren prec 2
    in print 0 exp;;
</div><div class="caml-output ok">val print_expr : expression -&gt; unit = &lt;fun&gt;
</div></pre>

<pre><div class="caml-input"> let e = Sum(Prod(Const 2.0, Var "x"), Const 1.0);;
</div><div class="caml-output ok">val e : expression = Sum (Prod (Const 2., Var "x"), Const 1.)
</div></pre>

<pre><div class="caml-input"> print_expr e; print_newline ();;
</div><div class="caml-output ok">2. * x + 1.
- : unit = ()
</div></pre>

<pre><div class="caml-input"> print_expr (deriv e "x"); print_newline ();;
</div><div class="caml-output ok">2. * 1. + 0. * x + 0.
- : unit = ()
</div></pre>


</div>
<h2 class="section" id="sec17">1.9&#XA0;&#XA0;Standalone OCaml programs</h2>
<p>All examples given so far were executed under the interactive system.
OCaml code can also be compiled separately and executed
non-interactively using the batch compilers <span class="c003">ocamlc</span> and <span class="c003">ocamlopt</span>.
The source code must be put in a file with extension <span class="c003">.ml</span>. It
consists of a sequence of phrases, which will be evaluated at runtime
in their order of appearance in the source file. Unlike in interactive
mode, types and values are not printed automatically; the program must
call printing functions explicitly to produce some output. The <span class="c003">;;</span> used
in the interactive examples is not required in
source files created for use with OCaml compilers, but can be helpful
to mark the end of a top-level expression unambiguously even when
there are syntax errors.
Here is a
sample standalone program to print Fibonacci numbers:
</p><pre>(* File fib.ml *)
let rec fib n =
  if n &lt; 2 then 1 else fib (n-1) + fib (n-2);;
let main () =
  let arg = int_of_string Sys.argv.(1) in
  print_int (fib arg);
  print_newline ();
  exit 0;;
main ();;
</pre><p><span class="c003">Sys.argv</span> is an array of strings containing the command-line
parameters. <span class="c003">Sys.argv.(1)</span> is thus the first command-line parameter.
The program above is compiled and executed with the following shell
commands:
</p><pre>$ ocamlc -o fib fib.ml
$ ./fib 10
89
$ ./fib 20
10946
</pre><p>
More complex standalone OCaml programs are typically composed of
multiple source files, and can link with precompiled libraries.
Chapters&#XA0;<a href="comp.html#c%3Acamlc">9</a> and&#XA0;<a href="native.html#c%3Anativecomp">12</a> explain how to use the
batch compilers <span class="c003">ocamlc</span> and <span class="c003">ocamlopt</span>. Recompilation of
multi-file OCaml projects can be automated using third-party
build systems, such as the
<a href="https://github.com/ocaml/ocamlbuild/">ocamlbuild</a>
compilation manager.

</p>
<hr>
<a href="foreword.html"><img src="previous_motif.svg" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.svg" alt="Up"></a>
<a href="moduleexamples.html"><img src="next_motif.svg" alt="Next"></a>
</body>
</html>