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.

<strong>VDM</strong>-<strong>10</strong> <strong>Language</strong> <strong>Manual</strong><br />

of scores and returns the corresponding pair of teams:<br />

✞<br />

promoted : seq of Score -> Team * Team<br />

promoted([sc1,sc2]ˆ-) == mk_(sc1.team,sc2.team);<br />

✡✝<br />

✆<br />

Here sc1 is bound to the head of the argument sequence, and sc2 is bound to the second<br />

element of the sequence. If promoted is called with a sequence with fewer than two<br />

elements then a runtime error occurs. Note that as we are not interested in the remaining<br />

elements of the list we use a don’t care pattern for the remainder.<br />

The preceding example also demonstrated the use of sequence concatenation patterns. Another<br />

example of this is the function quicksort which implements a standard quicksort<br />

algorithm:<br />

✞<br />

quicksort : seq of nat -> seq of nat<br />

quicksort (l) ==<br />

cases l:<br />

[] -> [],<br />

[x] -> [x],<br />

[x,y] -> if x < y then [x,y] else [y,x],<br />

-ˆ[x]ˆ- -> quicksort ([y | y in set elems l & y < x]) ˆ<br />

[x] ˆ<br />

quicksort ([y | y in set elems l & y > x])<br />

end<br />

✡✝<br />

Here, in the second cases clause a sequence concatenation pattern is used to decompose l<br />

into an arbitrary pivot element and two subsequences. The pivot is used to partition the list<br />

into those values less than the pivot and those values greater, and these two partitions are<br />

recursively sorted.<br />

Tuple patterns can be used to bind tuple components to identifiers. For instance since the<br />

function promoted defined above returns a pair, the following value definition binds the<br />

winning team of GroupA to the identifier Awinner:<br />

✞<br />

values<br />

✆<br />

✡✝<br />

mk_(Awinner,-) = promoted(GroupA);<br />

✆<br />

Record patterns are useful when several fields of a record are used in the same expression.<br />

For instance the following expression constructs a map from team names to points score:<br />

✞<br />

{ t |-> w * 3 + l | mk_Score(t,w,l,-,-) in set elems GroupA}<br />

76

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

Saved successfully!

Ooh no, something went wrong!