Title: Sets
1Sets
Learning Outcomes
At the end of this lecture you should be able to
- Identify when it is appropriate to use a set for
system modelling - Define a set using enumeration, number ranges and
comprehension - Use the set operators (union, intersection,
difference, subset and cardinality) - Apply the set type to model systems in VDM-SL
2Data Type Constructors
- So far we have looked at specifications involving
- simple scalar types
- enumerated types
(?, ? and so on)
(e.g. ltBROKENgt ltWORKINGgt lt IDLEgt )
Most systems manipulate complex data structures
like lists. To model such data structures,
VDM-SL provides several data type constructors.
The first abstract structured type of VDM-SL
that we will look at is the familiar notion of a
set.
3Sets for system modelling
A set is an unordered collection of objects in
which repetition is not significant.
When to use a set?
Collection of patients registered on the books of
a doctor's surgery?
?
?
The queue of patients waiting for a doctor?
4Declaring sets in VDM-SL
To indicate a variable to be of the set type
-set
variableName ElementType
For example
Day ltMONgt ltTUEgt ltWEDgt ltTHUgt ltFRIgt
ltSATgt ltSUNgt aNumber ? aDay
Day someNumbers ?-set someOtherNumbers ?
-set importantDays Day-set
5Defining sets in VDM-SL
Three ways to initialise the values of a set
- by enumeration
- by number ranges
- by comprehension.
6Defining sets by enumeration
- Involves
- listing the elements individually,
- separated by commas , , ,
- and enclosed in braces
For example someNumbers 2, 4, 28, 19 ,10
importantDays ltFRIgt, ltSATgt, ltSUNgt
7Defining sets by number ranges
Can be used when a set of continuous integers
required
someRange 5, ,15 equal to someRange
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
When the second number in the range is smaller
than the first, the empty set is returned.
7,,6
8Defining a set by comprehension
Allows one set to be defined by means of another
someSet expression (x) x ? someOtherSet ?
test(x)
Examples
someNumbers x x ? 2,,6 ? isEven(x)
2, 3, 4, 5, 6
someOtherNumbers x2 x ? 2,,6
2, 3, 4, 5, 6
4,
9,
16,
25,
36
9Set operations
Set union j ? k
Returns a set that contains all the elements of
the set j and all the elements of the set k.
if j ltMONgt, ltTUEgt, ltWEDgt, ltSUNgt and
k lt MON gt, ltFRIgt, lt TUE gt then j ? k
ltMONgt, ltTUEgt, ltWEDgt, ltSUNgt , ltFRIgt
10Set operations. contd
Set intersection j ? k
Returns a set that contains all the elements that
are common to both j and k.
if j ltMONgt, ltTUEgt, ltWEDgt, ltSUNgt and
k lt MON gt, ltFRIgt, lt TUE gt then j ? k
ltMONgt, ltTUEgt
11Set operations. contd
Set difference j \ k
Returns the set that contains all the elements
that belong to j but do not belong to k.
if j ltMONgt, ltTUEgt, ltWEDgt, ltSUNgt and
k lt MON gt, ltFRIgt, lt TUE gt then j \ k
ltWEDgt, ltSUNgt
12Set operations. contd
Subset j ? k
Returns true if all elements that belong to j
also belong to k.
a, d, e ? a, b, c, d, e, f a, d, e ? d, a,
e
Proper subset j ? k
Returns true if all elements that belong to j
also belong to k but false if sets j and k are
equal.
a, d, e ? a, b, c, d, e, f a, d, e ? d, a,
e
13Set operations. contd
Cardinality card j
Returns the number of elements in a given set.
card 7, 2, 12
3
card 7, 2, 2, 12, 12
3
card 4,,10
7
card
0
14The PatientRegister class
PatientRegister reg Patient addPatient
(Patient) removePatient (Patient) getPatients (
) Patient isRegistered (Patient)
Boolean numberRegistered ( )Integer
15Modelling the PatientRegister class in VDM-SL
types Patient values LIMIT state PatientRegister
of reg init mk-PatientRegister ( ) ?
end
TOKEN
? 200
Patient-set
inv mk-PatientRegister (r) ?
card r ? LIMIT
r
r
16The addPatient operation
addPatient (
) ext pre post
patientIn Patient
reg Patient-set
wr
patientIn ? reg
card reg lt LIMIT
?
patientIn ? reg
17The removePatient operation
removePatient (
) ext pre post
patientIn Patient
wr reg Patient-set
patientIn ? reg
reg \ patientIn
patientIn ? reg
18The getPatients operation
getPatients ( ) ext pre post
output Patient-set
rd reg Patient-set
TRUE
output reg
19The isRegistered operation
isRegistered (
) ext pre post
patientIn Patient
query ?
rd reg Patient-set
TRUE
?
query
patientIn ? reg
20The numberRegistered operation
numberRegistered () ext pre post
total ?
rd reg Patient-set
TRUE
card reg
total
21The Airport Flight Control System
"A system is to be developed that keeps track of
planes that are allowed to land at a particular
airport. Planes must apply for permission to
land at the airport prior to landing. When a
plane arrives to land at the airport it is only
allowed to do so if it has previously been given
permission. When a plane leaves the airport its
permission to land is also removed"
22A UML specification of the Airport class
Airport permission Aircraft landed Aircraft
givePermission(Aircraft) recordLanding(Aircra
ft) recordTakeOff(Aircraft) getPermission( )
Aircraft getLanded( ) Aircraft
numberWaiting() Integer
23Modelling the Airport class in VDM-SL
types state Airport of
init mk-Airport ( ) ? end
Aircraft TOKEN
permission Aircraft-set
landed Aircraft -set
inv mk-Airport(p,l) ?
l ? p
p, l
p
?
l
24The givePermission operation
craftIn Aircraft
givePermission(
) ext pre post
permission Aircraft - set
wr
craftIn ? permission
25The recordLanding operation
recordLanding (
) ext pre post
craftIn Aircraft
landed Aircraft -set
wr
permission Aircraft -set
rd
craftIn ? landed
?
craftIn ? permission
26The recordTakeOff operation
craftIn Aircraft
recordTakeOff (
) ext pre post
permission Aircraft -set
wr
wr
landed Aircraft -set
craftIn ? landed
?
27The getPermission operation
getPermission( ) ext pre post
out Aircraft-set
permission Aircraft -set
rd
TRUE
out permission
28The getLanded operation
out Aircraft -set
getLanded( ) ext pre post
landed Aircraft -set
rd
TRUE
out landed
29The numberWaiting operation
numberWaiting( ) ext pre post
total ?
permission Aircraft -set
rd
landed Aircraft -set
rd
card (permission \ landed)
total