Matt's Blog

Ocaml modules

Sat Nov 4 19:59:37 EST 2006

The ability to construct abstract data types is vital for producing large programs since it allows implementation details to be hidden from other parts of the program. For example in mathematical programming languages the underlying representation of complex numbers is not relevant to the scientist who must construct formulae using the complex numbers.

Ocaml has a module system that allows the programmer to define an interface specification separately to the implementation of that interface. Take for an example a module that describes intervals. An interval is defined by the end points of the interval (including the end points - this is a design decision here not a fundametal property of intervals). The module signature (interface) and structure definition (implementation) for intervals consisting of integers and floats is given below:

 module type IntervalSig = sig
  type elt
  type t
  (* constructor function from two end points *)
  val make_endpoints: elt -> elt ->  t
  (* constructor from centre poelt and width *)
  val make_centre_width: elt -> elt ->  t
  (* selector of lower limit *)
  val lower:  t -> elt
  (* selector of upper limit *)
  val upper:  t -> elt
  (* test whether an element is a 
     member of the elterval *)
  val test:  t -> elt -> bool
  (* width of the elterval *)
  val width:  t -> elt

 module IntInterval : IntervalSig 
  with type elt=int
 = struct
  type elt = int
  type t = (int * int)
  let make_endpoints l u = 
    if l < u then (l, u) else (u,l)
  let make_centre_width c w = (c-w, c+w)
  let lower (l, _) = l
  let upper (_, u) = u
  let test (l, u) x = (x >= l) & (x <= u)
  let width (l, u) = (u-l)/2

module FloatInterval : IntervalSig 
 with type elt=float   
= struct
  type elt = float
  type t = (float * float)
  let make_endpoints l u = 
    if l < u then (l, u) else (u,l)
  let make_centre_width c w = (c -. w, c +. w)
  let lower (l, _) = l
  let upper (_, u) = u
  let test (l, u) x = (x >= l) & (x <= u)
  let width (l, u) = (u -. l)/. 2.

To use this definition place the above code into a single file. This can be compiled with ocamlc or brought into the toplevel interpreter with

#use "";;

It would be useful to be able to create intervals of different element types, for example a character interval or rational number interval. This involves using Functors, which is a topic I will leave until the next entry. The above example illustrates the use of type sharing in the signature definition so that the "elt" types in the signature and structure agree with each other. Working with functors and abstract types will shorten the definitions of the structures greatly - something to look forward to!

Details of the module system can be found in

  • "Objective CAML for Scientists" by Jon Harrop
  • "Introduction to the Objective Caml Programming Language" by Jason Hickey (very good details about working with functors)
  • "Developing Applications with Objective Caml" by Emmanuel Chailloux, Pascal Manoury and Bruno Pagano (plus translators)
  • The Ocaml reference manual.



code (24)

erlang (5)
ideas (19)
lisp (1)
me (11)
notes (4)
ocaml (1)
physics (45)
qo (7)
unix (6)
vim (3)