Under the interactive system, the user types Caml phrases, terminated by

#1+2*3;; - : int = 7 #let pi = 4.0 *. atan 1.0;; val pi : float = 3.14159265358979312 #let square x = x *. x;; val square : float -> float = <fun> #square(sin pi) +. square(cos pi);; - : float = 1.The Caml 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:

#Recursive functions are defined with the1.0* 2;; This expression has type float but is here used with type int

#let rec fib n = if n < 2 then 1 else fib(n-1) + fib(n-2);; val fib : int -> int = <fun> #fib 10;; - : int = 89

#(1 < 2) = false;; - : bool = false #'a';; - : char = 'a' #"Hello world";; - : string = "Hello world"Predefined data structures include tuples, arrays, and lists. General mechanisms for defining your own data structures are also provided. They will be covered in more details 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

#let l = ["is"; "a"; "tale"; "told"; "etc."];; val l : string list = ["is"; "a"; "tale"; "told"; "etc."] #"Life" :: l;; - : string list = ["Life"; "is"; "a"; "tale"; "told"; "etc."]As with all other Caml data structures, lists do not need to be explicitly allocated and deallocated from memory: all memory management is entirely automatic in Caml. Similarly, there is no explicit handling of pointers: the Caml compiler silently introduces pointers where necessary.

As with most Caml 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:

#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 ;; val sort : 'a list -> 'a list = <fun> val insert : 'a -> 'a list -> 'a list = <fun> #sort l;; - : string list = ["a"; "etc."; "is"; "tale"; "told"]The type inferred for

#sort [6;2;5;3];; - : int list = [2; 3; 5; 6] #sort [3.14; 2.718];; - : float list = [2.718; 3.14]The

#let deriv f dx = function x -> (f(x +. dx) -. f(x)) /. dx;; val deriv : (float -> float) -> float -> float -> float = <fun> #let sin' = deriv sin 1e-6;; val sin' : float -> float = <fun> #sin' pi;; - : float = -1.00000000013961143Even function composition is definable:

#let compose f g = function x -> f(g(x));; val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <fun> #let cos2 = compose square cos;; val cos2 : float -> float = <fun>Functions that take other functions as arguments are called “functionals”, or “higher-order functions”. Functionals are especially useful to provide iterators or similar generic operations over a data structure. For instance, the standard Caml library provides a

#List.map (function n -> n * 2 + 1) [0;1;2;3;4];; - : int list = [1; 3; 5; 7; 9]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.

#let rec map f l = match l with [] -> [] | hd :: tl -> f hd :: map f tl;; val map : ('a -> 'b) -> 'a list -> 'b list = <fun>

#type ratio = {num: int; denum: int};; type ratio = { num : int; denum : int; } #let add_ratio r1 r2 = {num = r1.num * r2.denum + r2.num * r1.denum; denum = r1.denum * r2.denum};; val add_ratio : ratio -> ratio -> ratio = <fun> #add_ratio {num=1; denum=3} {num=2; denum=5};; - : ratio = {num = 11; denum = 15}The declaration of a variant type lists all possible shapes 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):

#type number = Int of int | Float of float | Error;; type number = Int of int | Float of float | ErrorThis declaration expresses that a value of type

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

#type sign = Positive | Negative;; type sign = Positive | Negative #let sign_int n = if n >= 0 then Positive else Negative;; val sign_int : int -> sign = <fun>To define arithmetic operations for the

#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;; val add_num : number -> number -> number = <fun> #add_num (Int 123) (Float 3.14159);; - : number = Float 126.14159The most common usage of variant types is to describe recursive data structures. Consider for example the type of binary trees:

#type 'a btree = Empty | Node of 'a * 'a btree * 'a btree;; type 'a btree = Empty | Node of 'a * 'a btree * 'a btreeThis definition reads as follow: a binary tree containing values of type

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

#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;; val member : 'a -> 'a btree -> bool = <fun> #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);; val insert : 'a -> 'a btree -> 'a btree = <fun>

#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;; val add_vect : float array -> float array -> float array = <fun> #add_vect [| 1.0; 2.0 |] [| 3.0; 4.0 |];; - : float array = [|4.; 6.|]Record fields can also be modified by assignment, provided they are declared

#type mutable_point = { mutable x: float; mutable y: float };; type mutable_point = { mutable x : float; mutable y : float; } #let translate p dx dy = p.x <- p.x +. dx; p.y <- p.y +. dy;; val translate : mutable_point -> float -> float -> unit = <fun> #let mypoint = { x = 0.0; y = 0.0 };; val mypoint : mutable_point = {x = 0.; y = 0.} #translate mypoint 1.0 2.0;; - : unit = () #mypoint;; - : mutable_point = {x = 1.; y = 2.}Caml has no built-in notion of variable – identifiers whose current value can be changed by assignment. (The

#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;; val insertion_sort : 'a array -> unit = <fun>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:

#let current_rand = ref 0;; val current_rand : int ref = {contents = 0} #let random () = current_rand := !current_rand * 25713 + 1345; !current_rand;; val random : unit -> int = <fun>Again, there is nothing magic with references: they are implemented as a one-field mutable record, as follows.

#type 'a ref = { mutable contents: 'a };; type 'a ref = { mutable contents : 'a; } #let (!) r = r.contents;; val ( ! ) : 'a ref -> 'a = <fun> #let (:=) r newval = r.contents <- newval;; val ( := ) : 'a ref -> 'a -> unit = <fun>In some special cases, you may need to store a polymorphic function in a data structure, keeping its polymorphism. Without user-provided type annotations, this is not allowed, as polymorphism is only introduced on a global level. However, you can give explicitly polymorphic types to record fields.

#type idref = { mutable id: 'a. 'a -> 'a };; type idref = { mutable id : 'a. 'a -> 'a; } #let r = {id = fun x -> x};; val r : idref = {id = <fun>} #let g s = (s.id 1, s.id true);; val g : idref -> int * bool = <fun> #r.id <- (fun x -> print_string "called id\n"; x);; - : unit = () #g r;; called id called id - : int * bool = (1, true)

#exception Empty_list;; exception Empty_list #let head l = match l with [] -> raise Empty_list | hd :: tl -> hd;; val head : 'a list -> 'a = <fun> #head [1;2];; - : int = 1 #head [];; Exception: Empty_list.Exceptions are used throughout the standard library to signal cases where the library functions cannot complete normally. For instance, the

#List.assoc 1 [(0, "zero"); (1, "one")];; - : string = "one" #List.assoc 2 [(0, "zero"); (1, "one")];; Exception: Not_found.Exceptions can be trapped with the

#let name_of_binary_digit digit = try List.assoc digit [0, "zero"; 1, "one"] with Not_found -> "not a binary digit";; val name_of_binary_digit : int -> string = <fun> #name_of_binary_digit 0;; - : string = "zero" #name_of_binary_digit (-1);; - : string = "not a binary digit"The

#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;; val temporarily_set_reference : 'a ref -> 'a -> (unit -> 'b) -> 'b = <fun>

#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 *) ;; type expression = Const of float | Var of string | Sum of expression * expression | Diff of expression * expression | Prod of expression * expression | Quot of expression * expressionWe 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.

#exception Unbound_variable of string;; exception Unbound_variable of string #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;; val eval : (string * float) list -> expression -> float = <fun> #eval [("x", 1.0); ("y", 3.14)] (Prod(Sum(Var "x", Const 2.0), Var "y"));; - : float = 9.42Now for a real symbolic processing, we define the derivative of an expression with respect to a variable

#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)) ;; val deriv : expression -> string -> expression = <fun> #deriv (Quot(Const 1.0, Var "x")) "x";; - : expression = Quot (Diff (Prod (Const 0., Var "x"), Prod (Const 1., Const 1.)), Prod (Var "x", Var "x"))

For the printing function, we take into account the usual precedence rules (i.e.

#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;; val print_expr : expression -> unit = <fun> #let e = Sum(Prod(Const 2.0, Var "x"), Const 1.0);; val e : expression = Sum (Prod (Const 2., Var "x"), Const 1.) #print_expr e; print_newline();; 2. * x + 1. - : unit = () #print_expr (deriv e "x"); print_newline();; 2. * 1. + 0. * x + 0. - : unit = ()Parsing (transforming concrete syntax into abstract syntax) is usually more delicate. Caml offers several tools to help write parsers: on the one hand, Caml versions of the lexer generator Lex and the parser generator Yacc (see chapter 12), which handle LALR(1) languages using push-down automata; on the other hand, a predefined type of streams (of characters or tokens) and pattern-matching over streams, which facilitate the writing of recursive-descent parsers for LL(1) languages. An example using

##load "camlp4o.cma";; Camlp4 Parsing version 3.09.0 #open Genlex;; let lexer = make_lexer ["("; ")"; "+"; "-"; "*"; "/"];; val lexer : char Stream.t -> Genlex.token Stream.t = <fun>For the lexical analysis phase (transformation of the input text into a stream of tokens), we use a “generic” lexer provided in the standard library module

#let token_stream = lexer(Stream.of_string "1.0 +x");; val token_stream : Genlex.token Stream.t = <abstr> #Stream.next token_stream;; - : Genlex.token = Float 1. #Stream.next token_stream;; - : Genlex.token = Kwd "+" #Stream.next token_stream;; - : Genlex.token = Ident "x"The parser itself operates by pattern-matching on the stream of tokens. As usual with recursive descent parsers, we use several intermediate parsing functions to reflect the precedence and associativity of operators. Pattern-matching over streams is more powerful than on regular data structures, as it allows recursive calls to parsing functions inside the patterns, for matching sub-components of the input stream. See the Camlp4 documentation for more details.

#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;; val parse_expr : Genlex.token Stream.t -> expression = <fun> val parse_more_adds : expression -> Genlex.token Stream.t -> expression = <fun> val parse_mult : Genlex.token Stream.t -> expression = <fun> val parse_more_mults : expression -> Genlex.token Stream.t -> expression = <fun> val parse_simple : Genlex.token Stream.t -> expression = <fun> #let parse_expression = parser [< e = parse_expr; _ = Stream.empty >] -> e;; val parse_expression : Genlex.token Stream.t -> expression = <fun>Composing the lexer and parser, we finally obtain a function to read an expression from a character string:

#let read_expression s = parse_expression(lexer(Stream.of_string s));; val read_expression : string -> expression = <fun> #read_expression "2*(x+y)";; - : expression = Prod (Const 2., Sum (Var "x", Var "y"))A small puzzle: why do we get different results in the following two examples?

#read_expression "x - 1";; - : expression = Diff (Var "x", Const 1.) #read_expression "x-1";; Exception: Stream.Error "".Answer: the generic lexer provided by

(* 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