25.07.2014 Views

VDM-10 Language Manual

VDM-10 Language Manual

VDM-10 Language Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14. Top-level Specification (<strong>VDM</strong>++ and <strong>VDM</strong>-RT)<br />

min_index : seq1 of nat -> nat<br />

min_index(l) ==<br />

if len l = 1<br />

then 1<br />

else let mi = min_index(tl l)<br />

in<br />

if l(mi+1) < hd l<br />

then mi+1<br />

else 1<br />

operations<br />

sort_ascending : () ==> ()<br />

sort_ascending () == selectSort(1);<br />

selectSort : nat ==> ()<br />

selectSort (i) ==<br />

if i < len data<br />

then (dcl temp: nat;<br />

dcl mi: nat := min_index(data(i,...,len data)) +<br />

i - 1;<br />

temp := data(mi);<br />

data(mi) := data(i);<br />

data(i) := temp;<br />

selectSort(i+1)<br />

)<br />

end SelectionSort<br />

✡✝<br />

✆<br />

Here the class Sort defines an abstract interface to be implemented by different sorting<br />

algorithms. One implementation is provided by the SelectionSort class.<br />

The next example clarifies how name space clashes are resolved.<br />

✞<br />

class A<br />

instance variables}<br />

i: int := 1;<br />

j: int := 2;<br />

end A<br />

class B is subclass of A<br />

131

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!