Software
Formal
Specificati
Eng/TA Maiada Alaa
Sets
is an unordered collection of objects in without repetition.
● Collection of patients registered on the books of a doctor's surgery
● The queue of patients waiting for a doctor?
● The set of courses a student is enrolled in.
● The members of a group
● A telephone help-line
● The set of employees in a company
Ways to initialize
value of sets
• Enumeration
• Number ranges
• Comprehension
Enumeration
Listing the elements individually, separated by commas, and
enclosed in brackets.
Ex: someNumbers = {2, 4, 28, 19, 10}
importantDays = {<FRI>,<SAT> ,<SUN> }
Number
Ranges
A set of continuous integers is required
Ex: someRange = {5, … ,15}
is equivalent 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} is returned as an empty set, { }
Comprehensi
on The symbol ‘|’ is read
as “such that”
Defining one set by means of another.
General format :
someSet = { expression (x) | x ∈ someOtherSet ● test(x) }
EX: someNumbers = { x | x ∈ {2,…,6} isEven(x)} is the set { 2, 4, 6 }
while, someOtherNumbers = { | x ∈ {2,…,6}} is the set { 4, 9, 16,
25, 36 }
Set
Membershipoperations
Element set
• Returns true if the element is a member of the
set.
• a {a, b, c, d, e, f} is true
• a {f, g, h} is false
But:
• a {a, b, c, d, e, f} is false
Set
set union operations
j∪k
• Returns a set that contains all the elements of the set j and all
the elements of the set k.
Ex: j = { <MON>,<TUE> ,<WED> ,<SUN> } and
k = { < MON >,<FRI> ,<TUE> } then
j ∪ k = {<MON>,<TUE> ,<WED> , <SUN>,
<FRI>}
Set
operations
set intersection
j∩k
Returns a set that contains all the elements that are common to
both j and k. .
Ex: j = { <MON>,<TUE> ,<WED> ,<SUN> } and
k = { < MON >,<FRI> ,<TUE> } then
j ∩ k = {<MON>,<TUE>}
Set
operations
Set difference
j\k
Returns the set that contains all the elements that belong to j but
do not belong to k.
Ex: j = { <MON>,<TUE> ,<WED> ,<SUN> } and
k = { < MON >,<FRI> ,<TUE> } then
j \ k = {<WED>,<SUN>}
Set
set Subset operations
j⊆k
• Returns true if all elements that belong to j also belong to k.
Ex: {a, d, e} ⊆ {a, b, c, d, e, f} is true
{a, d, e} ⊆ {d, a, e} is true
Set
set proper operations
j⊂k
• Returns true if all elements that belong to j also belong to k
but false if sets j and k are equal.
Ex: {a, d, e} ⊂ {a, b, c, d, e, f} is true
{a, d, e} ⊂ {a, d, e} is false
{a, d, e} ⊄ {a, d, e} is true
Set
operations
set cardinality
Cardinality: card
• Returns the number of elements in a given set.
card { 7, 2, 12 } = 3
card { 7, 2, 2, 12, 12 } = card {7, 2, 12} =
3
card { 4,…,10 } = 7
card {}=0
Patient Register Example
(using VDMSL)
Patient Register Case Study
● Consider a system that registers patients at a doctor’s surgery.
● Assume that the surgery can deal with a maximum of 200 patients
on its register.
● It will be necessary to add and remove patients from the register.
● The register must be able to be interrogated so that the list of
patients and the number of patients registered can be returned.
● Also, a check can be made to see if a given patient is registered .
Patient Register UML class
VDM-SL
types
Patient = TOKEN
values
LIMIT : N = 200
state PatientRegister of
reg: Patient-set • The register is a set of Patients
inv mk-PatientRegister (r) ∆ card r ≤ LIMIT • TOKEN means it's a unique identifier.
init mk-PatientRegister (r) ∆ r = { } as each patient has a unique ID.
End • Note the use of the cardinality
Operations operation to ensure that the number
addPatient ( patientIn: Patient ) of patients is no more than LIMIT,
ext wr reg: Patient-set which is set as a constant
Pre patientIn ∉ reg ∧ card reg < LIMIT • Note that the patient register is
post reg = reg ∪ { patientIn } initialized to the empty set, { }
removePatient ( patientIn: Patient )
ext wr reg: Patient-set
pre patientIn ∈ reg
Post reg = reg \ { patientIn }
VDM-SL
getPatients ( ) output : Patient-set
ext rd reg: Patient-set
pre TRUE
post output = reg
isRegistered ( patientIn : Patient ) query : B
ext rd reg: Patient-set
pre TRUE
post query ⇔ patientIn ∈ reg
numberRegistered ( ) total : N
ext rd reg: Patient-set
pre TRUE
post total = card reg
Thank
s