## Basic types

Is it possible to do computations with arbritrary precision arithmetics?

OCaml and Caml Light both provide a library that handles exact arithmetic
computation for rational numbers. The library is called

Operations on big numbers gets the suffix

`Num`

in OCaml and `camlnum`

in Caml Light.
Operations on big numbers gets the suffix

`/`

:
addition is thus `+/`

. You build big numbers using
conversion from (small) integers or character strings.
For printing in the toplevel, a custom printer can be used.
An example under OCaml is given below.
$ ocaml nums.cmaopenNum;;openFormat;;letprint_num ff n = fprintf ff "%s" (string_of_num n);; print_num : Format.formatter -> num -> unit = <fun>; #install_printer print_num;; num_of_string "2/3";; - : Num.num = 2/3letn = num_of_string "1/3" +/ num_of_string "2/3";; n : Num.num = 1letrecfact n =ifn <= 0then(num_of_int 1)elsenum_of_int n */ (fact (n - 1));; fact : int -> Num.num = <fun>; fact 100;; - : Num.num = 9332621544394415268169923885626670049071596826438162146859296389521759999322991 5608941463976156518286253697920827223758251185210916864000000000000000000000000

## Data structures

My array is modified, and I don't know why

This is due to the physical sharing of two arrays that you
missed. In Caml there are no implicit array copying. If you
give two names to the same array, every modification on one
array will be visible to the other:

The physical sharing effect also applies to elements stored in vectors: if these elements are also vectors, the sharing of these vectors implies that modifying one of these elements modifies the others (see also the entry below).letv = Array.make 3 0;;valv : int array = [|0; 0; 0|]letw = v;;valw : int array = [|0; 0; 0|] w.(0) <- 4;; - : unit = () v;; - : int array = [|4; 0; 0|]

How to define multidimensional arrays?

The only way is to define an array whose elements are arrays
themselves (Caml arrays are unidimensional, they modelize
mathematical vectors).
The naive way to define multidimensional arrays is bogus: the
result is not right because there is some unexpected physical
sharing between the lines of the new array (see also previous
entry):

The solution is to use the

The allocation of a new array has two phases. First, the initial value is computed; then this value is written in each element of the new array. That's why the line which is allocated byletm = Array.make 2 (Array.make 3 0);; m : int array array = [|[|0; 0; 0|]; [|0; 0; 0|]|] m.(0).(0) <- 1;; - : unit = () m;; - : int array array = [|[|1; 0; 0|]; [|1; 0; 0|]|]

`Array.make 3 0`

is unique and physically shared by
all the lines of the array `m`

.
The solution is to use the

`make_matrix`

primitive
that builds the matrix with all elements equal to the initial
value provided. Alternatively, write the program that allocates
a new line for each line of your matrix. For instance:
In the same vein, theletmatrix n m init =letresult = Array.make n (Array.make m init)infori = 1ton - 1doresult.(i) <- Array.make m init done; result;; matrix : int -> int -> 'a -> 'a array array = <fun>

`copy_vect`

primitive gives
strange results, when applied to matrices: you need to write a
function that explicitly copies each line of the matrix at hand:
letcopy_matrix m =letl = Array.length minifl = 0thenmelseletresult = Array.make l m.(0)infori = 0tol - 1doresult.(i) <- Array.copy m.(i) done; result

## Types definitions

How to define an enumerated type?

An enumerated type is a sum type with only constants. For
instance, a type with 3 constants:

The namestypecolor = Blue | White | Red;;typecolor = Blue | White | Red Blue;; - : color = Blue

`Blue`

, `White`

and
`Red`

are the constructors of the `color`

type. One can define functions on this type by pattern
matching:
letstring_of_color =functionBlue -> "blue" | White -> "white" | Red -> "red";;

How to share a label between two different record types?

When you define two types sharing a label name, the last
defined type hides the labels of the first type. For
instance:

The simplest way to overcome this problem is simply ... to use different names! For instancetypepoint_3d = {x : float; y : float; z : float};;typepoint_2d = {x : float; y : float};; {x = 10.; y = 20.; z = 30.};; The record field label z belongstothetypepoint_3d but is here mixedwithlabelsoftypepoint_2d

With OCaml, one can propose two others solutions. First, it is possible to use modules to define the two types in different name spaces:typepoint3d = {x3d : float; y3d : float; z3d : float};;typepoint2d = {x2d : float; y2d : float};;

This way labels can be fully qualified asmoduleD3 =structtypepoint = {x : float; y : float; z : float} end;;moduleD2 =structtypepoint = {x : float; y : float} end;;

`D3.x`

`D2.x`

:
{D3.x = 10.; D3.y = 20.; D3.z = 30.};; - : D3.point = {D3.x = 10.000000; D3.y = 20.000000; D3.z = 30.000000} {D2.x = 10.; D2.y = 20.};; - : D2.point = {D2.x = 10.000000; D2.y = 20.000000}You can also use objects that provide overloading on method names:

Note that objects provide you more than overloading: you can define truly polymorphic functions, working on bothclasspoint_3d ~x ~y ~z =objectmethodx : float = xmethody : float = ymethodz : float = z end;;classpoint_2d ~x ~y =objectmethodx : float = xmethody : float = y end;;

`point_3d`

and `point_2d`

, and you can even
coerce a `point_3d`

to a `point_2d`

.
How to define two sum types that share constructor names?

Generally speaking you cannot. As for all other names, you
must use distinct name constructors. However, you can define the
two types in two different name spaces, i.e. into two different
modules. As for labels discussed above, you obtain constructors
that can be qualified by their module names.
With OCaml you can alternatively use polymorphic
variants, i.e. constructors that are, in some sense,

*predefined*, since they are not defined by a type definition. For instance:typeids = [ `Name | `Val ];;typeperson = [ `Nameofstring ];;let(f : person -> string) =function`Name s -> s;;valf : person -> string = <fun>let(is_name : ids -> bool) =function`Name ->true| _ -> false;;valis_name : ids -> bool = <fun>

## Functions and procedures

How to define a function?

In Caml, the syntax to define functions is close to the
mathematical usage: the definition is introduced by the keyword

**let**

, followed by the name of the function and its
arguments; then the formula that computes the image of the
argument is written after an `=`

sign.
In fact, parens surrounding the argument may be omitted, so we generally write:letsuccessor (n) = n + 1;; successor : int -> int = <fun>

letsuccessor n = n + 1;; successor : int -> int = <fun>

How to define a recursive function?

You need to explicitly tell that you want to define a
recursive function: use “let rec” instead of
“let”. For instance:

Functions may be mutually recursive:letrecfact n =ifn = 0then1elsen * fact (n - 1);; fact : int -> int = <fun>letrecfib n =ifn <= 1thennelsefib (n - 1) + fib (n - 2);; fib : int -> int = <fun>

letrecodd n =ifn = 0thentrueelseifn = 1thenfalseelseeven (n - 1)andeven n =ifn = 0thenfalseelseifn = 1thentrueelseodd (n - 1);; odd : int -> bool = <fun> even : int -> bool = <fun>

How to apply a function?

Functions are applied as in mathematics: write the function's
name, followed by its argument enclosed in parens: f (x). In
practice, parens are omitted in case of constants or identifiers:
we write

When the argument of a function is more complex than just an identifier, you must enclose this argument between parentheses. In particular you need parens when the argument is a negative constant number: to apply

`fib 2`

instead of `fib (2)`

, and
`fact x`

instead of `fact (x)`

.
When the argument of a function is more complex than just an identifier, you must enclose this argument between parentheses. In particular you need parens when the argument is a negative constant number: to apply

`f`

to `-1`

you must write
`f (-1)`

and **not**`f -1`

that is
syntactically similar to `f - 1`

(hence it is a
subtraction, not an application).
How to define a procedure?

Recall that

In Caml, there is no special treatment of procedures: they are just considered as special cases of functions that return the special “meaningless” value

Procedures that do not need any meaningful argument, get

Procedures with argument are defined exactly as ordinary functions. For instance:

*procedures*are commands that produce an*effect*(for instance printing something on the terminal or writing some memory location), but have no mathematically meaningful result.In Caml, there is no special treatment of procedures: they are just considered as special cases of functions that return the special “meaningless” value

`()`

. For
instance, the `print_string`

primitive that prints a
character string on the terminal, just returns `()`

as
a way of indicating that its job has been properly completed.
Procedures that do not need any meaningful argument, get

`()`

as dummy argument. For instance, the
`print_newline`

procedure, that outputs a newline on
the terminal, gets no meaningful argument: it has type `unit -> unit`

.Procedures with argument are defined exactly as ordinary functions. For instance:

letmessage s = print_string s; print_newline();; message : string -> unit = <fun> message "Hello world!";; Hello world! - : unit = ()

How to define a procedure/function that takes no argument?

Note that it is impossible to define a procedure without any
argument at all: its definition would imply to execute it, and
there would be no way to call it afterwards. In the following
fragment

`double_newline`

is bound to `()`

,
and its further evaluation never produces carriage returns as may
be erroneously expected by the user.
The correct definition and usage of this procedure is:letdouble_newline = print_newline(); print_newline();; @ @ double_newline : unit = () double_newline;; - : unit = ()

letdouble_newline () = print_newline(); print_newline();; double_newline : unit -> unit = <fun> double_newline;; - : unit -> unit = <fun> double_newline ();; @ @ - : unit = ()

How to define a function with more than one argument?

Just write the list of successive arguments when defining the
function. For instance:

then gives the actual arguments in the same order when applying the function:letsum x y = x + y;; sum : int -> int -> int = <fun>

```
sum 1 2;;
- : int = 3
```

These functions are named “curried” functions, as opposed to
functions with tuples as argument:
letsum' (x, y) = x + y;; sum' : int * int -> int = <fun> sum' (1, 2);; - : int = 3

How to define a function that has several results?

You can define a function that return a pair or a tuple:

letdiv_mod x y = (x quo y, xmody);; div_mod : int -> int -> int * int = <fun> div_mod 15 7;; - : int * int = 2, 1

What is an “anonymous function”?

You may use functions that have no names: we call them
functional values or anonymous functions. A functional value is
introduced by the keyword

**function**

, followed by its
argument, then an arrow `->`

and the function body.
For instance:
functionx -> x + 1;; - : int -> int =fun(functionx -> x + 1) 2;; - : int = 3

What is the difference between

**fun**

and
**function**

?
Functions are usually introduced by the keyword

The keyword

**function**

. Each parameter is introduced by its own
**function**

construct. For instance, the construct:
defines a function with two parametersfunctionx ->functiony -> ...

`x`

and
`y`

. Functions that use pattern-matching are also
introduced by the keyword **function**

.
The keyword

**fun**

introduces curried functions
(with several successive arguments). For instance:
introduces a function with two argumentsfunx y -> ...

`x`

and
`y`

as to the previous one.
My function is never applied

This is probably due to a missing argument: since Caml is a
functional programming language, there is no error when you
evaluate a function with missing arguments: in this case, a
functional value is returned, but the function is evidently not
applied. Example: if you evaluate

`print_newline`

without argument, there is no error, but nothing happens. The
compiler issues a warning in case of a blatant misuse.
#print_newline;; - : unit -> unit #print_newline ();; @ - : unit = ()

## Pattern matching

How to do nested pattern matching?

You imperatively need to enclose between parens a pattern
matching which is written inside another pattern matching. In
effect, the internal pattern matching “catches” all the pattern
matching clauses that are written after it. For instance:

is parsed asletf =function| 0 ->match...with| a -> ... | b -> ... | 1 -> ... | 2 -> ...;;

This error may occur for every syntactic construct that involves pattern matching:letf =function| 0 ->match...with| a -> ... | b -> ... | 1 -> ... | 2 -> ...;;

**function**

, **match** .. **with**

and
**try** ... **with**

. The usual trick is to enclose inner pattern
matchings with **begin**

and **end**

. One
write:
letf =function| 0 ->beginmatch...with| a -> ... | b -> ...end| 1 -> ... | 2 -> ...;;

## Exceptions

## Typing

Error message: a type is not compatible with itself

You may obtain the message: This expression has type
“some type” but is used with type “the same some
type”. This may occur very often when using the
interactive system.

The reason is that two types with the same name have been defined the compiler does not confuse the two types, but the types are evidently written the same. Consider for instance:

The reason is that two types with the same name have been defined the compiler does not confuse the two types, but the types are evidently written the same. Consider for instance:

This phenomenon appears when you load many times the same file into the interactive system, since each reloading redefines the types. The solution is to quit your interactive system and reload your files in a new session.typet = Tofinttypet = Tofintletx = T 1;;valx : t = T 1typet = Tofint;;typet = Tofintletincr =functionT x -> T (x+1);;valincr : t -> t = <fun> incr x;; This expression hastypet but is here usedwithtypet

A function obtained through partial application is not polymorphic enough

The more common case to get a ``not polymorphic enough''
definition is when defining a function via partial
application of a general polymorphic function. In Caml
polymorphism is introduced only through the “let”
construct, and results from application are weakly polymorph;
hence the function resulting from the application is not
polymorph.
In this case, you recover a fully polymorphic definition by
clearly exhibiting the functionality to the type-checker :
define the function with an explicit functional abstraction,
that is, add a

**function**

construct or an extra
parameter (this rewriting is known as eta-expansion):
The two definitions are semantically equivalent, and the new one can be assigned a polymorphic type scheme, since it is no more a function application.letmap_id = List.map (functionx -> x) (* Result is weakly polymorphic *)valmap_id : '_a list -> '_a list = <fun> map_id [1;2] - : int list = [1;2] map_id (* No longer polymorphic *) - : int list -> int list = <fun>letmap_id' l = List.map (functionx -> x) lvalmap_id' : 'a list -> 'a list = <fun> map_id' [1;2] - : int list = [1;2] map_id' (* Still fully polymorphic *) - : 'a list -> 'a list = <fun>

The type of this expression contains type variables that
cannot be generalized

This message appears when the Caml compiler tries to
compile a function or a value which is monorphic, but for
which some types have not been completely inferred. Some
types variables are left in the type, which are are called
“weak” (and are displayed by an underscore:

`'_a`

); they will disappear thanks to type
inference as soon as enough informations will be given.
Since the expression mentionned in the error message cannot be compiled as is, two cases must be envisioned:letr = ref []valr : '_a list ref = {contents = []}letf = List.map (funx -> x)valf : '_a list -> '_a list = <fun>

- The expression can really not be turned into a
polymorphic expression, as in
`r`

above. You must use an explicit type annotation, in order to turn it into something completely monomorphic. - The expression can be transformed into something
polymorphic through rewriting some part of the code
(for example using eta-expansion) as in
the case of
`f`

.

How to write a function with polymorphic arguments?

In ML, an argument of a function cannot be polymorphic
inside the body of the function; hence the following
typing:

Nevertheless, in OCaml it is possible to use first-order polymorphism. For this, you can use either records or objects; in the case of records, you need to declare the type before using it in the function.

The function is not as polymorphic as we could have hoped.letf (g : 'a -> 'a) x y = g x, g yvalf : ('a -> 'a) -> 'a -> 'a -> 'a * 'a = <fun>

Nevertheless, in OCaml it is possible to use first-order polymorphism. For this, you can use either records or objects; in the case of records, you need to declare the type before using it in the function.

letf (o : <g : 'a. 'a -> 'a>) x y = o#g x, o#g yvalf : < g : 'a. 'a -> 'a > -> 'b -> 'c -> 'b * 'c = <fun>typeid = { g : 'a. 'a -> 'a; }typeid = { g : 'a. 'a -> 'a; }letf r x y = r.g x, r.g yvalf : id -> 'a -> 'b -> 'a * 'b = <fun>

## Intput/output

Why some printing material is mixed up and does not appear
in the right order?

If you use printing functions of the

`format`

module, you might not mix printing commands from
`format`

with printing commands from the basic I/O
system. In effect, the material printed by functions from
the `format`

module is delayed (stored into the
pretty-printing queue) in order to find out the proper line
breaking to perform with the material at hand. By contrast
low level output is performed with no more buffering than
usual I/O buffering.
```
print_string "before";
Format.print_string "MIDDLE";
print_string "after";;
beforeafterMIDDLE- : unit = ()
```

To avoid this kind of problems you should not mix printing
orders from `format`

and basic printing commands;
that's the reason why when using functions from the
`format`

module, it is considered good programming
habit to open `format`

globally in order to
completely mask low level printing functions by the high
level printing functions provided by `format`

.