ExercisesExercise 3 - Local definitionsExercise 4 - Closed intervals whose bounds are integersDocumentation and user's manualTable of contentsOCaml programs

Exercise 4 - Closed intervals whose bounds are integers

The non-built-in type  closed_interval  will be used. It is provided by the  Closed_interval  module.

Specification
of the
type  closed_interval 
This type gives each closed interval whose bounds are integers a unique representation. It is equipped with the five following functions:
  • The constructor  cons_closed  constructs the representation of the interval [a,b] from a pair (a,b) of integers where a is less than or equal to b.
  • The accessors  lower_bound  et  upper_bound  access the bounds of an interval.
  • The conversion function  string_of_closed_interval  gives a string representation of an interval.
  • The printing function  print_closed_interval  prints such a string representation.
The interface definition of the module (i.e. the content of the  Closed_interval.mli  file) shows the types of these five functions:

type closed_interval
val cons_closed : int * int -> closed_interval
val lower_bound : closed_interval -> int
val upper_bound : closed_interval -> int
val string_of_closed_interval : closed_interval -> string
val print_closed_interval : closed_interval -> unit

It also indicates that the type  closed_interval  is abstract. The type definition phrase  type closed_interval  only contains the keyword  type  followed by the name  closed_interval , indeed. After this there is nothing. Therefore the type representation is hidden.

Write definitions of functional values according to the following specifications:

 1 
  Name:  width . Type:  closed_interval -> int . Computes the width of a closed interval.

 2 
  Name:  is_member . Type:  closed_interval -> bool . Checks whether z belongs to i given a pair (z,i) where z is an integer and i is an interval.

 3 
  Name:  cons_closed_1 . Type:  int * int -> closed_interval . Computes the interval whose lower bound is z and width is n given a pair (z,n) where z is an integer and n is a non-negative integer.

 4 
  Name:  are_disjoint . Type:  closed_interval * closed_interval -> bool . Checks whether two intervals are disjoint.

 5 
  Name:  intersection . Type:  closed_interval * closed_interval -> closed_interval . Computes the intersection of two non-disjoint intervals.

 6 
  Name:  is_subset . Type:  closed_interval * closed_interval -> bool . Checks whether i is a proper subset of or is equal to j given a pair (i,j) of intervals (is a subset means is a proper subset of or is equal to).

 7 
  Name:  envelope . Type:  closed_interval * closed_interval -> closed_interval . Computes the envelope of two intervals, that is, the smallest interval that is a superset of both.

Test all these definitions:

 8 
  First create a directory. Create in this directory a file containing all these definitions and whose name has extension  ml . For instance  Use_closed_interval.ml . Write also in this directory the content of the archive  Closed_interval , that is, the files  Closed_interval.cmi  and  Closed_interval.cmo  (in other words: the module  Closed_interval ). Enter this directory. Build a Caml toplevel that contains the compiled implementation of the module  Closed_interval  preloaded at start-up. To do this, execute the Unix command

ocamlmktop -o mytoplevel Closed_interval.cmo

To run this new toplevel execute the Unix command

mytoplevel

In this toplevel first evaluate the Caml phrase

open Closed_interval

This opens the module  Closed_interval , which allows to use short names instead of long ones in dotted notation (that is, for instance  lower_bound  instead of  Closed_interval.lower_bound ). Then evaluate the Caml phrase

#use "Use_closed_interval.ml"

This reads, compiles and executes all the source phrases from the given file. Now test each of the definitions, that is, apply each of the function values several times.


Latest update : October 5, 2006
This document was translated from LaTeX by Hyperlatex 2.5, which is not the latest version of Hyperlatex.

ExercisesExercise 3 - Local definitionsExercise 4 - Closed intervals whose bounds are integersDocumentation and user's manualTable of contentsOCaml programs