VDM-10 Language Manual
VDM-10 Language Manual
VDM-10 Language Manual
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 7. Patterns<br />
In the following examples we use match values:<br />
✞<br />
✡✝<br />
let a = <br />
in cases GroupA(1).team:<br />
-> "Brazil are winners",<br />
(a) -> "France are winners",<br />
others -> "Neither France nor Brazil are winners"<br />
end;<br />
✆<br />
Match values can only match against their own values, so here if the team at the head of<br />
GroupA is then the first clause is matched; if the team at the head of GroupA<br />
is then the second clause is matched. Otherwise the others clause is matched.<br />
Note here that the use of brackets around a forces a to be considered as a match value.<br />
Set enumerations match patterns to elements of a set. For instance in<br />
✞<br />
✡✝<br />
let {sc1, sc2, sc3, sc4} = elems GroupA<br />
in<br />
sc1.points + sc2.points + sc3.points + sc4.points;<br />
the identifiers sc1, sc2, sc3 and sc4 are bound to the four elements of GroupA. Note that<br />
the choice of binding is loose - for instance sc1 may be bound to [any] element of elems<br />
GroupA. In this case if elems GroupA does not contain precisely four elements, then the<br />
expression is not well-formed.<br />
A set union pattern can be used to decompose a set for recursive function calls. An example<br />
of this is the function set2seq which converts a set into a sequence (with arbitrary order):<br />
✞<br />
set2seq[@elem] : set of @elem -> seq of @elem<br />
set2seq(s) ==<br />
cases s:<br />
{} -> [],<br />
{x}<br />
-> [x],<br />
s1 union s2 -> (set2seq[@elem](s1))ˆ(set2seq[@elem](s2))<br />
end<br />
✡✝<br />
In the third cases alternative we see the use of a set union pattern. This binds s1 and s2 to<br />
arbitrary subsets of s such that they partition s. The Toolbox interpreter always ensures a<br />
disjoint partition.<br />
Sequence enumeration patterns can be used to extract specific elements from a sequence. An<br />
example of this is the function promoted which extracts the first two elements of a seqnce<br />
75<br />
✆<br />
✆