Relations Relations By the end of this part of the course the - - PDF document
Relations Relations By the end of this part of the course the - - PDF document
Relations Relations By the end of this part of the course the student should understand and be able to use the concepts of relations and functions in a Z specification. The concepts introduced are: Power set Cartesian product
Relations
By the end of this part of the course the
student should understand and be able to use the concepts of relations and functions in a Z specification.
The concepts introduced are:
Power set Cartesian product Relation Function
A “Registry” Specification
Declarations registered is a set of STUDENTs
- ptions is a set of MODULES
is_enrolled_on is a relation between STUDENTs and MODULEs Invariant More later, but essentially this is saying that Only STUDENTs who are registered may be enrolled_on MODULEs that are options.
Power Set
The power set of a set is the set of all
subsets of that set.
The power set of a set A is written e.g. STUDENT = { Bill, John, Sue}
Three Elements
The power set of STUDENT
John Bill Sue John Bill Sue John Bill Sue John Bill Bill Sue Sue John
STUDENT= {Bill, John,Sue}
If A has n elements, how many does its powerset have?
0% 0% 0% 0% 0%
1. 2. 3. 4. 5.
Which of the following are in ?
0% 0% 0% 0% 0%
1. 2. 3. 4. 5.
Cartesian Product of two sets
The cartesian product of two sets A and B is the
set of all ordered pairs (x,y), where
x is an element of A and y is an element of B
The cartesian product of A and B is written A x B Example:
How many elements does A x B have?
0% 0% 0% 0%
1. 2. 3. 4.
A Relation, ρ , from a set A to a set B, is a subset of AXB. It is a set of pairs.
is_on = {(john,CS189), (sue,CS189), (sue,CS154)} knows_about={(john,CS189), (john,CS154), (bill,CS189), (bill,CS154), (sue,CS189), (sue,CS154) } likes={ } attends={(john,CS189), (john,CS154), (sue,CS189), (sue,CS154)}
- Relations
Relations in UML diagrams
The existence of a relationship between
- bjects of type A and objects of type B
is denoted:
A B
(mark is_enrolled_on CS153) (mary is_enrolled_on CS266) (sue is_enrolled_on CS153) (william is_enrolled_on CS266) (mark is_enrolled_on CS189) (john is_enrolled_on CS153) (john is_enrolled_on CS189) (mark, CS153) (mary, CS266) (sue, CS153) (william, CS266) (mark, CS189) (john,CS153) (john, CS189)
Relations: sets of ordered pairs
is_enrolled_on as a picture
John Mary Sue Mark William Bill CS189 CS153 CS266 CS183 CS288
is_enrolled_on as a database table
John CS153 John CS189 Mary CS266 Sue CS153 Mark CS153 Mark CS189 William CS266
Infix notation
If then we can write “
’’
This is called infix notation e.g. (Mary,CS266) is_enrolled_on
written as Mary is_enrolled_on CS266
e.g. 2 < 3
Domain
The domain of a relation is
the set of all elements in A that are related by to something in B
This is written Thus if then It is always true that
dom(is_enrolled_on)
John Mary Sue Mark William Bill CS189 CS153 CS266 CS183 CS288 The set of students enrolled on some course
What is dom(R)?
0% 0% 0% 0%
a b c 1 2 3 4
1.
{ a,b,c}
2.
{ (a,1),(a,2),(b,4)}
3.
{ a,b}
4.
{ 1,2,4}
R
Range
The range of a relation is
the set of all elements in B that are related by to something in A
This is written Thus if then It is always true that
ran(is_enrolled_on)
John Mary Sue Mark William Bill CS189 CS153 CS266 CS183 CS288 The set of courses with some students enrolled
What is ran(R)?
0% 0% 0% 0%
a b c 1 2 3 4 R
1.
{ 1,2,3,4}
2.
{ 1,2}
3.
{ a,b}
4.
None of the above
Library example again
is_on_loan_to is a
relation from BOOKS to PEOPLE
is_on_loan_to : BOOKS PEOPLE We will use relational notation to specify the
library system
Library a set stock a set members A rule is_on_loan_to
The Library state in Z
We now have an INVARIANT on the
Library state: that only elements of stock can be on loan, and only to members.
Borrowing Books
Borrow(book b, person p) b is not already on loan to anyone Add b is on loan to p
` Delta’ means change of state Declare input parameters Preconditions Postcondition
}
}
Exercise: returning books
Return(book b, person p) b is currently on loan to p Remove b is on loan to p
Summary
Power set Cartesian Product Relations
Infix notation 2< 3
Domain of a relation/function Range of a relation/function Z schemas Input and output parameters in Z event
schemas
[Currie chapters 6-7 , Haggarty chapters 4-5 ]
More relational notation
Running example: registered
Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran
Domain restriction
registered without domain restriction
Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran
registered with domain restriction
Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran
Domain anti-restriction
Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran
Example: Registered with domain anti-restriction
Range restriction
Example: registered with a range restriction
Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran
Range anti-restriction
Example: registered with a range anti-restriction
Abigail Barney Charlotte Dora Emily CS189 CS153 CS154 CS173 CS183 Fran
What is denoted by ?
0% 0% 0%
1.
The course CS154
2.
The people registered for CS154
3.
Something else
What is denoted by ?
0% 0% 0%
1.
Those of ian, james, kate who have registered for some courses
2.
The courses ian, james, and kate have registered for
3.
The course registrations that ian, james, and kate are involved in
What is denoted by ?
0% 0% 0%
The people not re... The people regist... The registrations...
1.
The people not registered for CS154
2.
The people registered for courses other than CS154
3.