*This is a port of the OCaml 4.01.0 manuals core language section.*

*License*

The OCaml system is copyright © 1996–2013 Institut National de Recherche en Informatique et en Automatique (INRIA). INRIA holds all ownership rights to the OCaml system.

The OCaml system is open source and can be freely redistributed. See the file LICENSE in the distribution for licensing information.

The present documentation is copyright © 2013 Institut National de Recherche en Informatique et en Automatique (INRIA). The OCaml documentation and user’s manual may be reproduced and distributed in whole or in part, subject to the following conditions:

- The copyright notice above and this permission notice must be preserved complete on all complete or partial copies.
- Any translation or derivative work of the OCaml documentation and user’s manual must be approved by the authors in writing before distribution.
- If you distribute the OCaml documentation and user’s manual in part, instructions for obtaining the complete version of this manual must be included, and a means for obtaining a complete version provided.
- Small portions may be reproduced as illustrations for reviews or quotes in other works without this permission notice if proper citation is given.

For this overview of OCaml, we use the interactive system, which is started by running ocaml from the Unix shell, or by launching the OCamlwin.exe application under Windows. This tutorial is presented as the transcript of a session with the interactive system: lines starting with `#`

represent user input; the system responses are printed below, without a leading `#`

.

Under the interactive system, the user types OCaml phrases terminated by `;;`

in response to the `#`

prompt, and the system compiles them on the fly, executes them, and prints the outcome of evaluation. Phrases are either simple expressions, or `let`

definitions of identifiers (either values or functions).

In the IOCaml notebook interface lines starting withIn [x]are for user input. Responses are printed just below. Also, phrases do not have to be terminated by`;;`

.

```
In [1]:
```1+2*3;;

```
```

```
In [2]:
```let pi = 4.0 *. atan 1.0;;

```
```

```
In [3]:
```let square x = x *. x;;

```
```

```
In [4]:
```square (sin pi) +. square (cos pi);;

```
```

`+`

and `*`

operate on integers, but `+.`

and `*.`

operate on floats.

```
In [5]:
```1.0 * 2;;

```
```

Recursive functions are defined with the let rec binding:

```
In [6]:
```let rec fib n =
if n < 2 then n else fib (n-1) + fib (n-2);;

```
```

```
In [7]:
```fib 10;;

```
```

```
In [8]:
```(1 < 2) = false;;

```
```

```
In [9]:
```'a';;

```
```

```
In [10]:
```"Hello world";;

```
```

```
In [11]:
```let l = ["is"; "a"; "tale"; "told"; "etc."];;

```
```

```
In [12]:
```"Life" :: l;;

```
```

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.

As with most OCaml data structures, inspecting and destructuring lists is performed by pattern-matching. List patterns have the exact same shape as list expressions, with identifier representing unspecified parts of the list. As an example, here is insertion sort on a list:

```
In [13]:
```let rec sort lst =
match lst with
[] -> []
| head :: tail -> insert head (sort tail)
and insert elt lst =
match lst with
[] -> [elt]
| head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail
;;

```
```

```
In [14]:
```sort l;;

```
```

`'a list -> 'a list`

, means that sort can actually apply to lists of any type, and returns a list of the same type. The type 'a is a *type variable*, and stands for any given type. The reason why sort can apply to lists of any type is that the comparisons (=, <=, etc.) are *polymorphic* in OCaml: they operate between any two values of the same type. This makes sort itself polymorphic over all list types.

```
In [15]:
```sort [6;2;5;3];;

```
```

```
In [16]:
```sort [ 3.14; 2.718 ];;

```
```

*immutable* data structures. Most OCaml data structures are immutable, but a few (most notably arrays) are *mutable*, meaning that they can be modified in-place at any time.

`deriv`

function that takes any float function as argument and returns an approximation of its derivative function:

```
In [17]:
```let deriv f dx = function x -> (f (x +. dx) -. f x) /. dx;;

```
```

```
In [18]:
```let sin' = deriv sin 1e-6;;

```
```

```
In [19]:
```sin' pi;;

```
```

Even function composition is definable:

```
In [20]:
```let compose f g = function x -> f (g x);;

```
```

```
In [21]:
```let cos2 = compose square cos;;

```
```

`List.map`

functional that applies a given function to each element of a list, and returns the list of the results:

```
In [22]:
```List.map (function n -> n * 2 + 1) [0;1;2;3;4];;

```
```

```
In [23]:
```let rec map f l =
match l with
[] -> []
| hd :: tl -> f hd :: map f tl;;

```
```

```
In [24]:
```type ratio = {num: int; denom: int};;

```
```

```
In [25]:
```let add_ratio r1 r2 =
{num = r1.num * r2.denom + r2.num * r1.denom;
denom = r1.denom * r2.denom};;

```
```

```
In [26]:
```add_ratio {num=1; denom=3} {num=2; denom=5};;

```
```

```
In [27]:
```type number = Int of int | Float of float | Error;;

```
```

This declaration expresses that a value of type number is either an integer, a floating-point number, or the constant Error representing the result of an invalid operation (e.g. a division by zero).

Enumerated types are a special case of variant types, where all alternatives are constants:

```
In [28]:
```type sign = Positive | Negative;;

```
```

```
In [29]:
```let sign_int n = if n >= 0 then Positive else Negative;;

```
```

```
In [30]:
```let add_num n1 n2 =
match (n1, n2) with
(Int i1, Int i2) ->
(* Check for overflow of integer addition *)
if sign_int i1 = sign_int i2 && sign_int (i1 + i2) <> sign_int i1
then Float(float i1 +. float i2)
else Int(i1 + i2)
| (Int i1, Float f2) -> Float(float i1 +. f2)
| (Float f1, Int i2) -> Float(f1 +. float i2)
| (Float f1, Float f2) -> Float(f1 +. f2)
| (Error, _) -> Error
| (_, Error) -> Error;;

```
```

```
In [31]:
```add_num (Int 123) (Float 3.14159);;

```
```

```
In [32]:
```type 'a btree = Empty | Node of 'a * 'a btree * 'a btree;;

```
```

This definition reads as follow: a binary tree containing values of type 'a (an arbitrary type) is either empty, or is a node containing one value of type 'a and two subtrees containing also values of type 'a, that is, two 'a btree.

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):

```
In [33]:
```let rec member x btree =
match btree with
Empty -> false
| Node(y, left, right) ->
if x = y then true else
if x < y then member x left else member x right;;

```
```

```
In [34]:
```let rec insert x btree =
match btree with
Empty -> Node(x, Empty, Empty)
| Node(y, left, right) ->
if x <= y then Node(y, insert x left, right)
else Node(y, left, insert x right);;

```
```

```
In [35]:
```let add_vect v1 v2 =
let len = min (Array.length v1) (Array.length v2) in
let res = Array.create len 0.0 in
for i = 0 to len - 1 do
res.(i) <- v1.(i) +. v2.(i)
done;
res;;

```
```

```
In [36]:
```add_vect [| 1.0; 2.0 |] [| 3.0; 4.0 |];;

```
```

```
In [37]:
```type mutable_point = { mutable x: float; mutable y: float };;

```
```

```
In [38]:
```let translate p dx dy =
p.x <- p.x +. dx; p.y <- p.y +. dy;;

```
```

```
In [39]:
```let mypoint = { x = 0.0; y = 0.0 };;

```
```

```
In [40]:
```translate mypoint 1.0 2.0;;

```
```

```
In [41]:
```mypoint;;

```
```

`let`

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 (or one-element arrays), with operators ! to fetch the current contents of the reference and := to assign the contents. Variables can then be emulated by `let`

-binding a reference. For instance, here is an in-place insertion sort over arrays:

```
In [42]:
```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 > 0 && val_i < a.(!j - 1) do
a.(!j) <- a.(!j - 1);
j := !j - 1
done;
a.(!j) <- val_i
done;;

```
```

```
In [43]:
```let current_rand = ref 0;;

```
```

```
In [44]:
```let random () =
current_rand := !current_rand * 25713 + 1345;
!current_rand;;

```
```

```
In [45]:
```type 'a ref = { mutable contents: 'a };;

```
```

```
In [46]:
```let ( ! ) r = r.contents;;

```
```

```
In [47]:
```let ( := ) r newval = r.contents <- newval;;

```
```

```
In [48]:
```type idref = { mutable id: 'a. 'a -> 'a };;

```
```

```
In [49]:
```let r = {id = fun x -> x};;

```
```

```
In [50]:
```let g s = (s.id 1, s.id true);;

```
```

```
In [51]:
```r.id <- (fun x -> print_string "called id\n"; x);;

```
```

```
In [52]:
```g r;;

```
```

`exception`

construct, and signalled with the `raise`

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.

```
In [53]:
```exception Empty_list;;

```
```

```
In [54]:
```let head l =
match l with
[] -> raise Empty_list
| hd :: tl -> hd;;

```
```

```
In [55]:
```head [1;2];;

```
```

```
In [56]:
```head [];;

```
```

Exceptions can be trapped with the `try…with`

construct:

```
In [57]:
```let name_of_binary_digit digit =
try
List.assoc digit [0, "zero"; 1, "one"]
with Not_found ->
"not a binary digit";;

```
```

```
In [58]:
```name_of_binary_digit 0;;

```
```

```
In [59]:
```name_of_binary_digit (-1);;

```
```

`try…with`

construct. Also, finalization can be performed by trapping all exceptions, performing the finalization, then raising again the exception:

```
In [60]:
```let temporarily_set_reference ref newval funct =
let oldval = !ref in
try
ref := newval;
let res = funct () in
ref := oldval;
res
with x ->
ref := oldval;
raise x;;

```
```

```
In [61]:
```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 *)
;;

```
```

```
In [62]:
```exception Unbound_variable of string;;

```
```

```
In [63]:
```let rec eval env exp =
match exp with
Const c -> c
| Var v ->
(try List.assoc v env with Not_found -> raise (Unbound_variable v))
| Sum(f, g) -> eval env f +. eval env g
| Diff(f, g) -> eval env f -. eval env g
| Prod(f, g) -> eval env f *. eval env g
| Quot(f, g) -> eval env f /. eval env g;;

```
```

```
In [64]:
```eval [("x", 1.0); ("y", 3.14)] (Prod(Sum(Var "x", Const 2.0), Var "y"));;

```
```

```
In [65]:
```let rec deriv exp dv =
match exp with
Const c -> Const 0.0
| Var v -> if v = dv then Const 1.0 else Const 0.0
| Sum(f, g) -> Sum(deriv f dv, deriv g dv)
| Diff(f, g) -> Diff(deriv f dv, deriv g dv)
| Prod(f, g) -> Sum(Prod(f, deriv g dv), Prod(deriv f dv, g))
| Quot(f, g) -> Quot(Diff(Prod(deriv f dv, g), Prod(f, deriv g dv)),
Prod(g, g))
;;

```
```

```
In [66]:
```deriv (Quot(Const 1.0, Var "x")) "x";;

```
```

As shown in the examples above, the internal representation (also called *abstract syntax*) 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 *concrete syntax*, which in the case of expressions is the familiar algebraic notation (e.g. 2*x+1).

For the printing function, we take into account the usual precedence rules (i.e. * binds tighter than +) 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.

```
In [67]:
```let print_expr exp =
(* Local function definitions *)
let open_paren prec op_prec =
if prec > op_prec then print_string "(" in
let close_paren prec op_prec =
if prec > op_prec then print_string ")" in
let rec print prec exp = (* prec is the current precedence *)
match exp with
Const c -> print_float c
| Var v -> print_string v
| Sum(f, g) ->
open_paren prec 0;
print 0 f; print_string " + "; print 0 g;
close_paren prec 0
| Diff(f, g) ->
open_paren prec 0;
print 0 f; print_string " - "; print 1 g;
close_paren prec 0
| Prod(f, g) ->
open_paren prec 2;
print 2 f; print_string " * "; print 2 g;
close_paren prec 2
| Quot(f, g) ->
open_paren prec 2;
print 2 f; print_string " / "; print 3 g;
close_paren prec 2
in print 0 exp;;

```
```

```
In [68]:
```let e = Sum(Prod(Const 2.0, Var "x"), Const 1.0);;

```
```

```
In [69]:
```print_expr e; print_newline ();;

```
```

```
In [70]:
```print_expr (deriv e "x"); print_newline ();;

```
```

`ocamllex`

and `ocamlyacc`

is given in chapter 12. Here, we will use stream parsers. The syntactic support for stream parsers is provided by the Camlp4 preprocessor, which can be loaded into the interactive toplevel via the #load directives below.

```
In [71]:
```#load "dynlink.cma";;
#load "camlp4o.cma";;

```
```

```
In [72]:
```open Genlex;;

```
```

```
In [73]:
```let lexer = make_lexer ["("; ")"; "+"; "-"; "*"; "/"];;

```
```

```
In [74]:
```let token_stream = lexer (Stream.of_string "1.0 +x");;

```
```

```
In [75]:
```Stream.next token_stream;;

```
```

```
In [76]:
```Stream.next token_stream;;

```
```

```
In [77]:
```Stream.next token_stream;;

```
```

```
In [78]:
```let rec parse_expr = parser
[< e1 = parse_mult; e = parse_more_adds e1 >] -> e
and parse_more_adds e1 = parser
[< 'Kwd "+"; e2 = parse_mult; e = parse_more_adds (Sum(e1, e2)) >] -> e
| [< 'Kwd "-"; e2 = parse_mult; e = parse_more_adds (Diff(e1, e2)) >] -> e
| [< >] -> e1
and parse_mult = parser
[< e1 = parse_simple; e = parse_more_mults e1 >] -> e
and parse_more_mults e1 = parser
[< 'Kwd "*"; e2 = parse_simple; e = parse_more_mults (Prod(e1, e2)) >] -> e
| [< 'Kwd "/"; e2 = parse_simple; e = parse_more_mults (Quot(e1, e2)) >] -> e
| [< >] -> e1
and parse_simple = parser
[< 'Ident s >] -> Var s
| [< 'Int i >] -> Const(float i)
| [< 'Float f >] -> Const f
| [< 'Kwd "("; e = parse_expr; 'Kwd ")" >] -> e;;

```
```

```
In [79]:
```let parse_expression = parser [< e = parse_expr; _ = Stream.empty >] -> e;;

```
```

```
In [80]:
```let read_expression s = parse_expression (lexer (Stream.of_string s));;

```
```

```
In [81]:
```read_expression "2*(x+y)";;

```
```

A small puzzle: why do we get different results in the following two examples?

```
In [82]:
```read_expression "x - 1";;

```
```

```
In [83]:
```read_expression "x-1";;

```
```

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 `ocamlc`

and `ocamlopt`

. The source code must be put in a file with extension .ml. 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. Here is a sample standalone program to print Fibonacci numbers:

```
(* File fib.ml *)
let rec fib n =
if n < 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 ();;
```

```
$ ocamlc -o fib fib.ml
$ ./fib 10
89
$ ./fib 20
10946
```

More complex standalone OCaml programs are typically composed of multiple source files, and can link with precompiled libraries. Chapters 8 and 11 explain how to use the batch compilers `ocamlc`

and `ocamlopt`

. Recompilation of multi-file OCaml projects can be automated using the `ocamlbuild`

compilation manager, documented in chapter 18.