
-----------------------------------
wtd
Tue May 17, 2005 4:47 pm

O'Caml syntactic oddities explained
-----------------------------------
Tuple types

Tuples are critical to O'Caml, and immensely useful, but their type representation can be a bit confusing.

Let's say I enter the following at the O'Caml toplevel:

# let a = (42, 56.7);;

Now it shows me:

val a : int * float = (42, 56.7)

But what the heck is "int * float"?

Well, in this case the asterisk indicates a tuple of an int and a float.

Where is this really an issue?

Fortunately O'Caml infers types very well so we almost never have to worry about this.  There are at least a few places where we do have to worry about it, though.

"Algebraic" types are one example.  Let's look at a simple binary tree definition.

type 'a btree = Empty 
   | Leaf of 'a 
   | Branch of 'a btree * 'a * 'a btree

Here the 'a is the generic type that can stand in for any type.  The three possible forms of a binary tree are empty, a single leaf with one value, and a branch with one value and two subtrees.  Each "constructor" only takes one argument which is a tuple.

Seeing this is action:

let tree = Branch (Branch (Leaf 32, 56, Empty), 12, Leaf 3);;

The other places we see this syntax is with exceptions.  Let's say we have a bank_account class.

class bank_account overdraft init_balance=
   object (self)
      val overdraft = overdraft
      val mutable balance = init_balance
      method current_balance = balance
      method deposit amount = 
         balance 