A non empty interval of integer is defined by two integers $$[lowerb, upperb]$$ where:

  • $$lowerb$$ is the lower bound, $$upperb$$ the upper bound, and $$lowerb \leq upperb$$;
  • empty interval $$[]$$ is defined as having no lower bound nor upper bound.

We define $$\texttt{Interval}$$ of integers ADT as follows:

Type: \(\texttt{Interval}\)
Use: \(\texttt{integer, Boolean}\)

Functions:
\(\begin{array}{p{1cm} l l l l} & \textbf{init} & & \rightarrow \texttt{interval} \text{(empty interval creation)}\\ & \textbf{Create} & \texttt{integer} \times \texttt{integer} & \rightarrow \texttt{interval} \text{(non empty interval creation)}\\ & \textbf{Belong} & \texttt{integer} \times \texttt{interval} & \rightarrow \texttt{Boolean}\\ & \textbf{isEmpty} & \texttt{interval} & \rightarrow \texttt{Boolean}\\ & \textbf{l_bound} & \texttt{interval} & \rightarrow \texttt{integer}\\ & \textbf{u_bound} & \texttt{interval} & \rightarrow \texttt{integer}\\ & \textbf{Intersect} &\texttt{interval} \times \texttt{interval} & \rightarrow \texttt{interval}\\ & \textbf{Union} &\texttt{interval} \times \texttt{interval} & \rightarrow \texttt{interval}\\ \end{array}\)

Constructors: ...
Preconditions:  

Let $$lb$$ and $$ub$$ be two integers, and $$I_1$$ and $$I_2$$ two intervals,

  • \(\texttt{Create(lb,ub)}\) defined iff $$lb \leq ub$$
  • \(\texttt{l$\_$bound($I_1$)}\), \(\texttt{u$\_$bound($I_1$)}\) defined iff \(\texttt{not isEmpty($I_1$)}\)
  • \(\texttt{Union($I_1$,$I_2$)}\) defined iff \(\texttt{ not isEmpty(Intersect($I_1$,$I_2$))}\)


Axioms: ...

Choose constructors functions and complete axioms.

Hint: \(\texttt{min}\) and \(\texttt{max}\) functions calculating respectively the minimum and the maximum of 2 integers could be useful.


Difficulty level
This exercise is mostly suitable for students

Constructors: init, Create

Let a, b, c, d, e be integers, In an interval
Axioms:
    * Belong(e,init())=False
    * Belong(e,Create(a,b))=False if e<a or e>b, True otherwise
    * isEmpty(init())=True
    * isEmpty(Create(a,b))=False
    * l_bound(Create(a,b))=a
    * u_bound(Create(a,b))=b
    * Intersect(init(),I)=init()

    * Intersect(Create(a,b), Create(c,d))=init() If c>b or a>d, else Create(max(a,c),min(b,d))

(If the intervals do not overlap, their union will not be an interval.)
    * Union(Create(a,b),Create(c,d))=Create(min(a,c),max(b,d))

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Kruskal Algorithm - Minimal Spanning Tree