Chapter 1 The core language

This part of the manual is a tutorial introduction to theOCaml language. A good familiarity with programming in a conventionallanguages (say, C or Java) is assumed, but no prior exposure tofunctional languages is required. The present chapter introduces thecore language. Chapter 2 deals with themodule system, chapter 3 with theobject-oriented features, chapter 4 withextensions to the core language (labeled arguments and polymorphicvariants), and chapter 6 gives some advanced examples.

1.1 Basics

For this overview of OCaml, we use the interactive system, whichis started by running ocaml from the Unix shell, or by launching theOCamlwin.exe application under Windows. This tutorial is presentedas the transcript of a session with the interactive system:lines starting with # represent user input; the system responses areprinted below, without a leading #.

Under the interactive system, the user types OCaml phrases terminatedby ;; in response to the # prompt, and the system compiles themon the fly, executes them, and prints the outcome of evaluation.Phrases are either simple expressions, or let definitions ofidentifiers (either values or functions).

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

The OCaml system computes both the value and the type foreach phrase. Even function parameters need no explicit type declaration:the system infers their types from their usage in thefunction. Notice also that integers and floating-point numbers aredistinct types, with distinct operators: + and operate onintegers, but +. and . operate on floats.

  1. 1.0 * 2;;
  2. Error: This expression has type float but an expression was expected of type
  3. int

Recursive functions are defined with the let rec binding:

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

1.2 Data types

In addition to integers and floating-point numbers, OCaml offers theusual basic data types:

  • booleans
  1. (1 < 2) = false;;
  2. - : bool = false
  1. let one = if true then 1 else 2;;
  2. val one : int = 1
  • characters
  1. 'a';;
  2. - : char = 'a'
  1. int_of_char '\n';;
  2. - : int = 10
  • immutable character strings
  1. "Hello" ^ " " ^ "world";;
  2. - : string = "Hello world"
  1. {|This is a quoted string, here, neither \ nor " are special characters|};;
  2. - : string =
  3. "This is a quoted string, here, neither \\ nor \" are special characters"
  1. {|"\\"|}="\"\\\\\"";;
  2. - : bool = true
  1. {delimiter|the end of this|}quoted string is here|delimiter}
  2. = "the end of this|}quoted string is here";;
  3. - : bool = true

Predefined data structures include tuples, arrays, and lists. There are alsogeneral mechanisms for defining your own data structures, such as records andvariants, which will be covered in more detail later; for now, we concentrateon lists. Lists are either given in extension as a bracketed list ofsemicolon-separated elements, or built from the empty list by adding elements in front using the ::(“cons”) operator.

  1. let l = ["is"; "a"; "tale"; "told"; "etc."];;
  2. val l : string list = ["is"; "a"; "tale"; "told"; "etc."]
  1. "Life" :: l;;
  2. - : string list = ["Life"; "is"; "a"; "tale"; "told"; "etc."]

As with all other OCaml data structures, lists do not need to beexplicitly allocated and deallocated from memory: all memorymanagement is entirely automatic in OCaml. Similarly, there is noexplicit handling of pointers: the OCaml compiler silently introducespointers where necessary.

As with most OCaml data structures, inspecting and destructuring listsis performed by pattern-matching. List patterns have exactly the sameform as list expressions, with identifiers representing unspecifiedparts of the list. As an example, here is insertion sort on a list:

  1. let rec sort lst =
  2. match lst with
  3. [] -> []
  4. | head :: tail -> insert head (sort tail)
  5. and insert elt lst =
  6. match lst with
  7. [] -> [elt]
  8. | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail
  9. ;;
  10. val sort : 'a list -> 'a list = <fun>
  11. val insert : 'a -> 'a list -> 'a list = <fun>
  1. sort l;;
  2. - : string list = ["a"; "etc."; "is"; "tale"; "told"]

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

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

The sort function above does not modify its input list: it buildsand returns a new list containing the same elements as the input list,in ascending order. There is actually no way in OCaml to modifya list in-place once it is built: we say that lists are immutable_data structures. Most OCaml data structures are immutable, but a few(most notably arrays) are _mutable, meaning that they can bemodified in-place at any time.

The OCaml notation for the type of a function with multiple arguments is arg1_type -> arg2_type -> … -> return_type. For example,the type inferred for insert, 'a -> 'a list -> 'a list, means that inserttakes two arguments, an element of any type 'a and a list with elements ofthe same type 'a and returns a list of the same type.

1.3 Functions as values

OCaml is a functional language: functions in the full mathematicalsense are supported and can be passed around freely just as any otherpiece of data. For instance, here is a deriv function that takes anyfloat function as argument and returns an approximation of itsderivative function:

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

Even function composition is definable:

  1. let compose f g = function x -> f (g x);;
  2. val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <fun>
  1. let cos2 = compose square cos;;
  2. val cos2 : float -> float = <fun>

Functions that take other functions as arguments are called“functionals”, or “higher-order functions”. Functionals areespecially useful to provide iterators or similar generic operationsover a data structure. For instance, the standard OCaml libraryprovides a List.map functional that applies a given function to eachelement of a list, and returns the list of the results:

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

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

  1. let rec map f l =
  2. match l with
  3. [] -> []
  4. | hd :: tl -> f hd :: map f tl;;
  5. val map : ('a -> 'b) -> 'a list -> 'b list = <fun>

1.4 Records and variants

User-defined data structures include records and variants. Both aredefined with the type declaration. Here, we declare a record type torepresent rational numbers.

  1. type ratio = {num: int; denom: int};;
  2. type ratio = { num : int; denom : int; }
  1. let add_ratio r1 r2 =
  2. {num = r1.num * r2.denom + r2.num * r1.denom;
  3. denom = r1.denom * r2.denom};;
  4. val add_ratio : ratio -> ratio -> ratio = <fun>
  1. add_ratio {num=1; denom=3} {num=2; denom=5};;
  2. - : ratio = {num = 11; denom = 15}

Record fields can also be accessed through pattern-matching:

  1. let integer_part r =
  2. match r with
  3. {num=num; denom=denom} -> num / denom;;
  4. val integer_part : ratio -> int = <fun>

Since there is only one case in this pattern matching, itis safe to expand directly the argument r in a record pattern:

  1. let integer_part {num=num; denom=denom} = num / denom;;
  2. val integer_part : ratio -> int = <fun>

Unneeded fields can be omitted:

  1. let get_denom {denom=denom} = denom;;
  2. val get_denom : ratio -> int = <fun>

Optionally, missing fields can be made explicit by ending the list offields with a trailing wildcard _::

  1. let get_num {num=num; _ } = num;;
  2. val get_num : ratio -> int = <fun>

When both sides of the = sign are the same, it is possible to avoidrepeating the field name by eliding the =field part:

  1. let integer_part {num; denom} = num / denom;;
  2. val integer_part : ratio -> int = <fun>

This short notation for fields also works when constructing records:

  1. let ratio num denom = {num; denom};;
  2. val ratio : int -> int -> ratio = <fun>

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

  1. let integer_product integer ratio = { ratio with num = integer * ratio.num };;
  2. val integer_product : int -> ratio -> ratio = <fun>

With this functional update notation, the record on the left-hand sideof with is copied except for the fields on the right-hand side whichare updated.

The declaration of a variant type lists all possible forms for valuesof that type. Each case is identified by a name, called a constructor,which serves both for constructing values of the variant type andinspecting them by pattern-matching. Constructor names are capitalizedto distinguish them from variable names (which must start with alowercase letter). For instance, here is a varianttype for doing mixed arithmetic (integers and floats):

  1. type number = Int of int | Float of float | Error;;
  2. type number = Int of int | Float of float | Error

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

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

  1. type sign = Positive | Negative;;
  2. type sign = Positive | Negative
  1. let sign_int n = if n >= 0 then Positive else Negative;;
  2. val sign_int : int -> sign = <fun>

To define arithmetic operations for the number type, we usepattern-matching on the two numbers involved:

  1. let add_num n1 n2 =
  2. match (n1, n2) with
  3. (Int i1, Int i2) ->
  4. (* Check for overflow of integer addition *)
  5. if sign_int i1 = sign_int i2 && sign_int (i1 + i2) <> sign_int i1
  6. then Float(float i1 +. float i2)
  7. else Int(i1 + i2)
  8. | (Int i1, Float f2) -> Float(float i1 +. f2)
  9. | (Float f1, Int i2) -> Float(f1 +. float i2)
  10. | (Float f1, Float f2) -> Float(f1 +. f2)
  11. | (Error, _) -> Error
  12. | (_, Error) -> Error;;
  13. val add_num : number -> number -> number = <fun>
  1. add_num (Int 123) (Float 3.14159);;
  2. - : number = Float 126.14159

Another interesting example of variant type is the built-in'a option type which represents either a value of type 'a or anabsence of value:

  1. type 'a option = Some of 'a | None;;
  2. type 'a option = Some of 'a | None

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

  1. let safe_square_root x = if x > 0. then Some(sqrt x) else None;;
  2. val safe_square_root : float -> float option = <fun>

The most common usage of variant types is to describe recursive datastructures. Consider for example the type of binary trees:

  1. type 'a btree = Empty | Node of 'a * 'a btree * 'a btree;;
  2. type 'a btree = Empty | Node of 'a * 'a btree * 'a btree

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

Operations on binary trees are naturally expressed as recursive functionsfollowing the same structure as the type definition itself. Forinstance, here are functions performing lookup and insertion inordered binary trees (elements increase from left to right):

  1. let rec member x btree =
  2. match btree with
  3. Empty -> false
  4. | Node(y, left, right) ->
  5. if x = y then true else
  6. if x < y then member x left else member x right;;
  7. val member : 'a -> 'a btree -> bool = <fun>
  1. let rec insert x btree =
  2. match btree with
  3. Empty -> Node(x, Empty, Empty)
  4. | Node(y, left, right) ->
  5. if x <= y then Node(y, insert x left, right)
  6. else Node(y, left, insert x right);;
  7. val insert : 'a -> 'a btree -> 'a btree = <fun>

1.4.1 Record and variant disambiguation

( This subsection can be skipped on the first reading )

Astute readers may have wondered what happens when two or more recordfields or constructors share the same name

  1. type first_record = { x:int; y:int; z:int }
  2. type middle_record = { x:int; z:int }
  3. type last_record = { x:int };;
  4.  
  1. type first_variant = A | B | C
  2. type last_variant = A;;
  3.  

The answer is that when confronted with multiple options, OCaml tries touse locally available information to disambiguate between the various fieldsand constructors. First, if the type of the record or variant is known,OCaml can pick unambiguously the corresponding field or constructor.For instance:

  1. let look_at_x_then_z (r:first_record) =
  2. let x = r.x in
  3. x + r.z;;
  4. val look_at_x_then_z : first_record -> int = <fun>
  1. let permute (x:first_variant) = match x with
  2. | A -> (B:first_variant)
  3. | B -> A
  4. | C -> C;;
  5. val permute : first_variant -> first_variant = <fun>
  1. type wrapped = First of first_record
  2. let f (First r) = r, r.x;;
  3. type wrapped = First of first_record
  4. val f : wrapped -> first_record * int = <fun>

In the first example, (r:first_record) is an explicit annotationtelling OCaml that the type of r is first_record. With thisannotation, Ocaml knows that r.x refers to the x field of the firstrecord type. Similarly, the type annotation in the second example makesit clear to OCaml that the constructors A, B and C come from thefirst variant type. Contrarily, in the last example, OCaml has inferredby itself that the type of r can only be first_record and there areno needs for explicit type annotations.

Those explicit type annotations can in fact be used anywhere.Most of the time they are unnecessary, but they are useful to guidedisambiguation, to debug unexpected type errors, or combined with someof the more advanced features of OCaml described in later chapters.

Secondly, for records, OCaml can also deduce the right record type bylooking at the whole set of fields used in a expression or pattern:

  1. let project_and_rotate {x;y; _ } = { x= - y; y = x ; z = 0} ;;
  2. val project_and_rotate : first_record -> first_record = <fun>

Since the fields x and y can only appear simultaneously in the firstrecord type, OCaml infers that the type of project_and_rotate isfirst_record -> first_record.

In last resort, if there is not enough information to disambiguate betweendifferent fields or constructors, Ocaml picks the last defined typeamongst all locally valid choices:

  1. let look_at_xz {x;z} = x;;
  2. val look_at_xz : middle_record -> int = <fun>

Here, OCaml has inferred that the possible choices for the type of{x;z} are first_record and middle_record, since the typelast_record has no field z. Ocaml then picks the type middle_recordas the last defined type between the two possibilities.

Beware that this last resort disambiguation is local: once Ocaml haschosen a disambiguation, it sticks to this choice, even if it leads toan ulterior type error:

  1. let look_at_x_then_y r =
  2. let x = r.x in (* Ocaml deduces [r: last_record] *)
  3. x + r.y;;
  4. Error: This expression has type last_record
  5. The field y does not belong to type last_record
  1. let is_a_or_b x = match x with
  2. | A -> true (* OCaml infers [x: last_variant] *)
  3. | B -> true;;
  4. Error: This variant pattern is expected to have type last_variant
  5. The constructor B does not belong to type last_variant

Moreover, being the last defined type is a quite unstable position thatmay change surreptitiously after adding or moving around a typedefinition, or after opening a module (see chapter 2).Consequently, adding explicit type annotations to guide disambiguation ismore robust than relying on the last defined type disambiguation.

1.5 Imperative features

Though all examples so far were written in purely applicative style,OCaml is also equipped with full imperative features. This includes theusual while and for loops, as well as mutable data structures suchas arrays. Arrays are either created by listing semicolon-separated elementvalues between [| and |] brackets, or allocated and initialized with theArray.make function, then filled up later by assignments. For instance, thefunction below sums two vectors (represented as float arrays) componentwise.

  1. let add_vect v1 v2 =
  2. let len = min (Array.length v1) (Array.length v2) in
  3. let res = Array.make len 0.0 in
  4. for i = 0 to len - 1 do
  5. res.(i) <- v1.(i) +. v2.(i)
  6. done;
  7. res;;
  8. val add_vect : float array -> float array -> float array = <fun>
  1. add_vect [| 1.0; 2.0 |] [| 3.0; 4.0 |];;
  2. - : float array = [|4.; 6.|]

Record fields can also be modified by assignment, provided they aredeclared mutable in the definition of the record type:

  1. type mutable_point = { mutable x: float; mutable y: float };;
  2. type mutable_point = { mutable x : float; mutable y : float; }
  1. let translate p dx dy =
  2. p.x <- p.x +. dx; p.y <- p.y +. dy;;
  3. val translate : mutable_point -> float -> float -> unit = <fun>
  1. let mypoint = { x = 0.0; y = 0.0 };;
  2. val mypoint : mutable_point = {x = 0.; y = 0.}
  1. translate mypoint 1.0 2.0;;
  2. - : unit = ()
  1. mypoint;;
  2. - : mutable_point = {x = 1.; y = 2.}

OCaml has no built-in notion of variable – identifiers whose currentvalue can be changed by assignment. (The let binding is not anassignment, it introduces a new identifier with a new scope.)However, the standard library provides references, which are mutableindirection cells, with operators ! to fetchthe current contents of the reference and := to assign the contents.Variables can then be emulated by let-binding a reference. Forinstance, here is an in-place insertion sort over arrays:

  1. let insertion_sort a =
  2. for i = 1 to Array.length a - 1 do
  3. let val_i = a.(i) in
  4. let j = ref i in
  5. while !j > 0 && val_i < a.(!j - 1) do
  6. a.(!j) <- a.(!j - 1);
  7. j := !j - 1
  8. done;
  9. a.(!j) <- val_i
  10. done;;
  11. val insertion_sort : 'a array -> unit = <fun>

References are also useful to write functions that maintain a currentstate between two calls to the function. For instance, the followingpseudo-random number generator keeps the last returned number in areference:

  1. let current_rand = ref 0;;
  2. val current_rand : int ref = {contents = 0}
  1. let random () =
  2. current_rand := !current_rand * 25713 + 1345;
  3. !current_rand;;
  4. val random : unit -> int = <fun>

Again, there is nothing magical with references: they are implemented asa single-field mutable record, as follows.

  1. type 'a ref = { mutable contents: 'a };;
  2. type 'a ref = { mutable contents : 'a; }
  1. let ( ! ) r = r.contents;;
  2. val ( ! ) : 'a ref -> 'a = <fun>
  1. let ( := ) r newval = r.contents <- newval;;
  2. val ( := ) : 'a ref -> 'a -> unit = <fun>

In some special cases, you may need to store a polymorphic function ina data structure, keeping its polymorphism. Doing this requiresuser-provided type annotations, since polymorphism is only introducedautomatically for global definitions. However, you can explicitly givepolymorphic types to record fields.

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

1.6 Exceptions

OCaml provides exceptions for signalling and handling exceptionalconditions. Exceptions can also be used as a general-purpose non-localcontrol structure, although this should not be overused since it canmake the code harder to understand. Exceptions are declared with theexception construct, and signalled with the raise operator. For instance,the function below for taking the head of a list uses an exception tosignal the case where an empty list is given.

  1. exception Empty_list;;
  2. exception Empty_list
  1. let head l =
  2. match l with
  3. [] -> raise Empty_list
  4. | hd :: tl -> hd;;
  5. val head : 'a list -> 'a = <fun>
  1. head [1;2];;
  2. - : int = 1
  1. head [];;
  2. Exception: Empty_list.

Exceptions are used throughout the standard library to signal caseswhere the library functions cannot complete normally. For instance,the List.assoc function, which returns the data associated with agiven key in a list of (key, data) pairs, raises the predefinedexception Not_found when the key does not appear in the list:

  1. List.assoc 1 [(0, "zero"); (1, "one")];;
  2. - : string = "one"
  1. List.assoc 2 [(0, "zero"); (1, "one")];;
  2. Exception: Not_found.

Exceptions can be trapped with the try…with construct:

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

The with part does pattern matching on theexception value with the same syntax and behavior as match. Thus,several exceptions can be caught by onetry…with construct:

  1. let rec first_named_value values names =
  2. try
  3. List.assoc (head values) names
  4. with
  5. | Empty_list -> "no named value"
  6. | Not_found -> first_named_value (List.tl values) names;;
  7. val first_named_value : 'a list -> ('a * string) list -> string = <fun>
  1. first_named_value [ 0; 10 ] [ 1, "one"; 10, "ten"];;
  2. - : string = "ten"

Also, finalization can be performed bytrapping all exceptions, performing the finalization, then re-raisingthe exception:

  1. let temporarily_set_reference ref newval funct =
  2. let oldval = !ref in
  3. try
  4. ref := newval;
  5. let res = funct () in
  6. ref := oldval;
  7. res
  8. with x ->
  9. ref := oldval;
  10. raise x;;
  11. val temporarily_set_reference : 'a ref -> 'a -> (unit -> 'b) -> 'b = <fun>

An alternative to try…with is to catch the exception whilepattern matching:

  1. let assoc_may_map f x l =
  2. match List.assoc x l with
  3. | exception Not_found -> None
  4. | y -> f y;;
  5. val assoc_may_map : ('a -> 'b option) -> 'c -> ('c * 'a) list -> 'b option =
  6. <fun>

Note that this construction is only useful if the exception is raisedbetween match…with. Exception patterns can be combinedwith ordinary patterns at the toplevel,

  1. let flat_assoc_opt x l =
  2. match List.assoc x l with
  3. | None | exception Not_found -> None
  4. | Some _ as v -> v;;
  5. val flat_assoc_opt : 'a -> ('a * 'b option) list -> 'b option = <fun>

but they cannot be nested inside other patterns. For instance,the pattern Some (exception A) is invalid.

When exceptions are used as a control structure, it can be useful to makethem as local as possible by using a locally defined exception.For instance, with

  1. let fixpoint f x =
  2. let exception Done in
  3. let x = ref x in
  4. try while true do
  5. let y = f !x in
  6. if !x = y then raise Done else x := y
  7. done; assert false
  8. with Done -> !x;;
  9. val fixpoint : ('a -> 'a) -> 'a -> 'a = <fun>

the function f cannot raise a Done exception, which removes anentire class of misbehaving functions.

1.7 Symbolic processing of expressions

We finish this introduction with a more complete examplerepresentative of the use of OCaml for symbolic processing: formalmanipulations of arithmetic expressions containing variables. Thefollowing variant type describes the expressions we shall manipulate:

  1. type expression =
  2. Const of float
  3. | Var of string
  4. | Sum of expression * expression (* e1 + e2 *)
  5. | Diff of expression * expression (* e1 - e2 *)
  6. | Prod of expression * expression (* e1 * e2 *)
  7. | Quot of expression * expression (* e1 / e2 *)
  8. ;;
  9. type expression =
  10. Const of float
  11. | Var of string
  12. | Sum of expression * expression
  13. | Diff of expression * expression
  14. | Prod of expression * expression
  15. | Quot of expression * expression

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

  1. exception Unbound_variable of string;;
  2. exception Unbound_variable of string
  1. let rec eval env exp =
  2. match exp with
  3. Const c -> c
  4. | Var v ->
  5. (try List.assoc v env with Not_found -> raise (Unbound_variable v))
  6. | Sum(f, g) -> eval env f +. eval env g
  7. | Diff(f, g) -> eval env f -. eval env g
  8. | Prod(f, g) -> eval env f *. eval env g
  9. | Quot(f, g) -> eval env f /. eval env g;;
  10. val eval : (string * float) list -> expression -> float = <fun>
  1. eval [("x", 1.0); ("y", 3.14)] (Prod(Sum(Var "x", Const 2.0), Var "y"));;
  2. - : float = 9.42

Now for a real symbolic processing, we define the derivative of anexpression with respect to a variable dv:

  1. let rec deriv exp dv =
  2. match exp with
  3. Const c -> Const 0.0
  4. | Var v -> if v = dv then Const 1.0 else Const 0.0
  5. | Sum(f, g) -> Sum(deriv f dv, deriv g dv)
  6. | Diff(f, g) -> Diff(deriv f dv, deriv g dv)
  7. | Prod(f, g) -> Sum(Prod(f, deriv g dv), Prod(deriv f dv, g))
  8. | Quot(f, g) -> Quot(Diff(Prod(deriv f dv, g), Prod(f, deriv g dv)),
  9. Prod(g, g))
  10. ;;
  11. val deriv : expression -> string -> expression = <fun>
  1. deriv (Quot(Const 1.0, Var "x")) "x";;
  2. - : expression =
  3. Quot (Diff (Prod (Const 0., Var "x"), Prod (Const 1., Const 1.)),
  4. Prod (Var "x", Var "x"))

1.8 Pretty-printing

As shown in the examples above, the internal representation (alsocalled abstract syntax) of expressions quickly becomes hard toread and write as the expressions get larger. We need a printer and aparser to go back and forth between the abstract syntax and the concrete syntax, which in the case of expressions is the familiaralgebraic notation (e.g. 2*x+1).

For the printing function, we take into account the usual precedencerules (i.e. * binds tighter than +) to avoid printing unnecessaryparentheses. To this end, we maintain the current operator precedenceand print parentheses around an operator only if its precedence isless than the current precedence.

  1. let print_expr exp =
  2. (* Local function definitions *)
  3. let open_paren prec op_prec =
  4. if prec > op_prec then print_string "(" in
  5. let close_paren prec op_prec =
  6. if prec > op_prec then print_string ")" in
  7. let rec print prec exp = (* prec is the current precedence *)
  8. match exp with
  9. Const c -> print_float c
  10. | Var v -> print_string v
  11. | Sum(f, g) ->
  12. open_paren prec 0;
  13. print 0 f; print_string " + "; print 0 g;
  14. close_paren prec 0
  15. | Diff(f, g) ->
  16. open_paren prec 0;
  17. print 0 f; print_string " - "; print 1 g;
  18. close_paren prec 0
  19. | Prod(f, g) ->
  20. open_paren prec 2;
  21. print 2 f; print_string " * "; print 2 g;
  22. close_paren prec 2
  23. | Quot(f, g) ->
  24. open_paren prec 2;
  25. print 2 f; print_string " / "; print 3 g;
  26. close_paren prec 2
  27. in print 0 exp;;
  28. val print_expr : expression -> unit = <fun>
  1. let e = Sum(Prod(Const 2.0, Var "x"), Const 1.0);;
  2. val e : expression = Sum (Prod (Const 2., Var "x"), Const 1.)
  1. print_expr e; print_newline ();;
  2. 2. * x + 1.
  3. - : unit = ()
  1. print_expr (deriv e "x"); print_newline ();;
  2. 2. * 1. + 0. * x + 0.
  3. - : unit = ()

1.9 Standalone OCaml programs

All examples given so far were executed under the interactive system.OCaml code can also be compiled separately and executednon-interactively using the batch compilers ocamlc and ocamlopt.The source code must be put in a file with extension .ml. Itconsists of a sequence of phrases, which will be evaluated at runtimein their order of appearance in the source file. Unlike in interactivemode, types and values are not printed automatically; the program mustcall printing functions explicitly to produce some output. The ;; usedin the interactive examples is not required insource files created for use with OCaml compilers, but can be helpfulto mark the end of a top-level expression unambiguously even whenthere are syntax errors.Here is asample standalone program to print Fibonacci numbers:

  1. (* File fib.ml *)
  2. let rec fib n =
  3. if n < 2 then 1 else fib (n-1) + fib (n-2);;
  4. let main () =
  5. let arg = int_of_string Sys.argv.(1) in
  6. print_int (fib arg);
  7. print_newline ();
  8. exit 0;;
  9. main ();;

Sys.argv is an array of strings containing the command-lineparameters. Sys.argv.(1) is thus the first command-line parameter.The program above is compiled and executed with the following shellcommands:

  1. $ ocamlc -o fib fib.ml
  2. $ ./fib 10
  3. 89
  4. $ ./fib 20
  5. 10946

More complex standalone OCaml programs are typically composed ofmultiple source files, and can link with precompiled libraries.Chapters 9 and 12 explain how to use thebatch compilers ocamlc and ocamlopt. Recompilation ofmulti-file OCaml projects can be automated using third-partybuild systems, such as theocamlbuildcompilation manager.