VDM-10 Language Manual
VDM-10 Language Manual
VDM-10 Language Manual
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 6. Expressions<br />
✡✝<br />
mk_Score(-,w2,-,-,p2) = sc2<br />
in (p1 > p2) or (p1 = p2 and w1 > w2)<br />
✆<br />
In this particular example we extract the second and fifth components of the two scores.<br />
Note that don’t care patterns (page 73) are used to indicate that the remaining components<br />
are irrelevant for the processing done in the body of this expression.<br />
Let-be-such-that expressions are useful for abstracting away the non-essential choice of an<br />
element from a set, in particular in formulating recursive definitions over sets. An example<br />
of this is a version of the sequence filter function (page 40) over sets:<br />
✞<br />
✡✝<br />
set_filter[@elem] : (@elem -> bool) -> (set of @elem) -><br />
(set of @elem)<br />
set_filter(p)(s) ==<br />
if s = {}<br />
then {}<br />
else let x in set s<br />
in (if p(x) then {x} else {}) union<br />
set_filter[@elem](p)(s \ {x});<br />
We could alternatively have defined this function using a set comprehension (described in<br />
section 6.7):<br />
✞<br />
✡✝<br />
set_filter[@elem] : (@elem -> bool) -> (set of @elem) -><br />
(set of @elem)<br />
set_filter(p)(s) ==<br />
{ x | x in set s & p(x)};<br />
The last example shows how the optional “be such that” part (be st) can be used. This<br />
part is especially useful when it is known that an element with some property exists but an<br />
explicit expression for such an element is not known or difficult to write. For instance we<br />
can exploit this expression to write a selection sort algorithm:<br />
✞<br />
remove : nat * seq of nat -> seq of nat<br />
remove (x,l) ==<br />
let i in set inds l be st l(i) = x<br />
in<br />
l(1,...,i-1) ˆ l(i+1,...,len l)<br />
pre x in set elems l;<br />
selection_sort : seq of nat -> seq of nat<br />
43<br />
✆<br />
✆