Title: Relations
1Relations
- Chapter 9
- Formal Specification using Z
2Formal definition of Relation
- The Z examples so far have been limited to one
basic type. We need of relating one set to
another. - A relation is a set which is based in the idea of
the Cartesian product.
3Formal definition of Relation
- A Cartesian product is the pairing of values of
two or more sets. The Cartesian product is
written - X x Y x Z
- For example
- red, green xhardtop,softtop
- (red, hardtop), (red, softtop), (green,
hardtop), (green,softtop) - This set consists of ordered paired called a
tuple
4Formal definition of Relation
- A binary relation R from the set X to the set Y
is a subset of the Cartisian Product X Y. If
(x,y) R, we write xRy and say x is related to
y. In the case XY, we call R a binary relation
on X. - The domain of R is the set
- x X (x,y) R from some y Y
- The range of R is the set
- y Y (x,y) R for some x X
5Relation as a table
- A relation can be thought of as a table that
lists the relationship of elements to other
elements - Student Course
- Bill Computer Science
- Mary Maths
- Bill Art
- Beth History
- Beth Computer Science
- Dave Math
- The table is a set of ordered pairs
6Relation as a set of ordered Pairs
- If we let
- X Bill, Mary, Beth, Dave
- Y ComputerScience, Math, Art, History
- then the relation from the prevuous table is
- R (Bill,ComputerScience), (Mary, Math),
(Bill,Art), (Beth,ComputerScience),
(Beth,History), (Dave, Math) - We say Mary R Math. The domain is the domain
(first column) of R is the set X the range
(second column) of R is the set Y. So far the
relation has been defined by specifying the
ordered pairs involved.
7Relation defined by rule
- It is also possible to define a relation by
giving a rule for membership of the relation. - Let X 2,3,4 and Y 3,4,5,6,7
- If we define R from X to Y by
- (x,y) R if x divides y (with zero remainder)
- We get
- R (2,4), (2,6), (3,3), (3,6), (4,4)
8Relation written as table
- This can be written as a table .
- X Y
- 2 4
- 2 6
- 3 3
- 3 6
- 4 4
- The domain of R is the set 2,3,4 and the range
is 3,4,5
9Let R be the relation on X 1,2,3,4 defined
by (x,y) R if x y, x,y X. is shown
The digraph for R
1
2
3
4
R (1,1), (1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3
,3),(3,4),(4,4) The domain and the range are
both equal to x. R is reflexive, transitive and
antisymmetric.
10Let R be the relation on X 1,2,3,4 defined
by (x,y) R if x lt y, x,y X. is shown
The digraph for R
1
2
3
4
R (1,2),(1,3),(1,4),(2,3),(2,4),(3,4) The
domain and the range are both equal to x. R is
reflexive, transitive and non-symmetric.
11Types of relation
- A relation R on a set X is reflexive if
(x,x) R for every x X. - A relation R on a set X is symmetric if for all
x,y X, if (x,y) R, then (y,x) R. - A relation R on a set X is antisymmetric if for
all x,y X, if (x,y) R, (y,x) and x y
, then (y,x) R.
12Types of relation
- A relation R on a set X is transitive if for all
x,y,z X, if (x,y) R, and (y,z) R
then (x,z) R.
13Types of relation
- Let ? be a relation on the set A, then
- ? is reflexive if x? x for all x? A
- ? is symmetric if x? y implies y ? x
- ? is transitive if x? y and y? z ? imply x? z
- ? is antisymmetric if x? y and y? x imply that xy
14Relations
- A special case of Cartesian product is a pair. A
binary relation is set of pairs, of related
values. - COUNTRY the set of all countries
- LANGUAGE set of all languages
15A relation on COUNTRY and LANGUAGE called SPEAKS
could have values (France,French),(Canada,French
),(Canada,English),(GB,English),(USA,English)
SPEAKS relation
France Canada GB USA
French English
LANGUAGE
COUNTRY
16RELATIONS
- As can be seen from the SPEAKS relation, there
are no restrictions requiring values of the two
sets to be paired only one-to-one. - We are free to choose the direction or order of
the relation - SPOKEN ? (LANGUAGE ?COUNTRY)
17Declaring Relations in Z
- R X ? Y ( equivalent to ?(X ?Y) )
- SPEAKS COUNTRY ? LANGUAGE
- ( equivalent to ?(COUNTRY ? LANGUAGE) )
- In English we say
- R relates X to Y.
- SPEAKS relates COUNTRY to LANGUAGE
18MAPLETS
- A maplet relates x to y and is written
- x ?y (x,y)
- x is related to y or x maps to y
- GB ? English ? SPEAKS
- is equivalent to
- (GB, English) ? SPEAKS
19Membership
- To discover if a certain pair of values are
related it is sufficient to see if the pair or
maplet is an element of the relation - GB ? English ? SPEAKS
- OR
- (GB, English) ? SPEAKS
20Infix Notation
- If we declare a relation using low-line
characters as place holders to show the fact that
is infix - _SPEAKS_ COUNTRY ? LANGUAGE
- GB speaks English
- In general
- xRy x ? y ? R(x,y) ? R
21 The DOMAIN and RANGE of the relation SPEAKSdom
SPEAKS is set of countries where at least one
language is spoken.ran SPEAKS is set of
languages which are spoke in at least one
country.
SPEAKS relation
French English
France Canada GB USA
LANGUAGE (TARGET)
COUNTRY (SOURCE)
dom SPEAKS ran
SPEAKS
22Domain and Range
- A relation relates values of a set called source
or from-set to a set called target or to-set. - Below the source is X and the target Y.
- ?R X? Y
- ?______________________
- ? R (x1,y2), (x2,y2), (x4,y3), (x6, y3) ),
(x4,y2) - ?________________________
23Domain
- In most cases only a subset of of the source is
involved in the relation. This subset is called
the domain - dom R x1,x2,x4,x6
24Range
- In most cases only a subset of of the target is
involved in the relation. This subset is called
the range - ran R y2,y3
25 The DOMAIN and RANGE of the relation R dom R
x1,x2,x4,x6 ran R y2,y3
R relation
y1
x5
x1 x2 x4 x6
Y2 y3
y4
Y (TARGET)
X (SOURCE)
x3
dom R ran R
26Relational Image
- To discover the set of values from the range of a
relation related to a set of values from the
domain of the relation one can use the relational
image - R?S?
- pronounced the relational image of S in R. For
the languages spoken in France and Switzerland
would be - SPEAKS ?France, Switzerland?
- French,German, Italian, Romansch
27Constant value for a Relation
- Some relations have constant values. If the value
of a relation is known, it can be given by an
axiomatic definition. For example the infix
relation greater-than-or-equal - ?_ ? _ ???
- ?______________
- ? ? i,j? ? i ? j ??n? ? ij n
- ?________________
28Domain Restriction
- The domain restriction operator restricts a
relation to that part where the domain is
contained in a particular set. The relation R
domain restricted to S, is written as - S ? R
- The point of the operator can be thought of as
pointing left to the domain
29Range Restriction
- The range restriction operator restricts a
relation to that part where the range is
contained in a particular set. The relation R
range restricted to S, is written as - R ? S
- The point of the operator can be thought of as
pointing right to the range
30Domain Subtraction
- The Domain Subtraction operator restricts a
relation to that part where the domain is not
contained in a particular set. The relation R
domain subtracted to S, is written as - S ? R
- The point of the operator can be thought of as
pointing left to the domain
31Range Subtraction
- The Range Subtraction operator restricts a
relation to that part where the Range is not
contained in a particular set. The relation R
range subtracted to S, is written as - R ? S
32Example of Domain Restriction
- ?Hols_____
- ?holidays COUNTRY ?DATE
- ?__________
- EU ?COUNTRY
- The relation mapping only EU countries to their
public holidays is - EU ? holidays
- The relation mapping only non-EU countries to
their public holidays is - EU ? holidays
33Composition
- Relations can be joined together in an operation
called composition. Given - R X ? Y and
- Q Y ? Z
- We can have forward composition or backward
composition
34Forward Composition
- Given
- R X ? Y and
- Q Y ? Z
- forward composition is defined as
- RQ X ? Z
35Forward Composition
RQ
R
Q
X
Z
Y
36For any pair (x,y) related by R forward composed
with Q x RQ z there is a y where R relates x to
y and Q relates y to z ?yY ? xRy ? yQz
RQ
R
Q
X
Z
Y
37Backward Composition
- Given
- R X ? Y and
- Q Y ? Z
- backward composition is defined as
- Q?R RQ
38Repeated Composition
- A homogeneous relation is one which relates
values from a type to values of the same type.
Such a relation can be composed with iteslf - R X ? X
- R R X ? X
39Composition Example
- Countries are related by the borders relation if
the share a border - borders COUNTRY ? COUNTRY
- e.g. France borders Switzerland
- Switzerland borders Austria
- Countries are related by borders composed with
borders if they each share a border with a third
country.
40Composition Example
- The two facts
- 1. France borders Germany
- 2. Germany borders Austria
- can be represented by
- France bordersborders Austria
- We can write
- Spain borders3 Denmark
- for
- Spain borders borders borders Denmark
41Transitive Closure
- In general the transitive closure R as used in
- x R y
- means that there is a repeated composition of R
which relates x to y. For example - France borders India.
- Means France is on the same land-mass as India
42Identity Relation
- The identity relation
- id X
- is the relation which maps all xs on to
themselves - id X xX ? x ? x
43Reflexive Transitive Closure
- The repeated composition
- R x R ? id X
- includes the identity relation. The Reflexive
Transitive Closure is similar to the Transitive
Closure except that it includes the identity
relation. For example - x R x is true, even if x R x is false, (id x)
- France borders France is true
44Relation Example
- Public holidays around the world can be described
as follows - COUNTRY the set of all countries
- DATE the dates of a given year
- ?Hols_____
- ?holidays COUNTRY ?DATE
- ?__________
45Relation Example
- An operation to discover whether a date d? is a
public holiday in country c? is - REPLY yes no
- ?Enquire_____
- ??Hols
- ?c? COUNTRY
- ?d? DATE
- ?rep! REPLY
- ?________ ?
- ?(c? ? d? ? holidays ? rep! yes) ?
- ?(c? ? d? ? holidays ? rep! no)
- ?__________
46Relation Example
- An operation to decree whether a public holiday
in country c? on date d? is - ?Decree_____
- ??Hols
- ?c? COUNTRY
- ?d? DATE
- ?________ ?
- ?holidays holidays ? c? ? d?
- ?__________
47Relation Example
- An operation to abolish a public holiday in
country c? on date d? is - ?Abolish_____
- ??Hols
- ?c? COUNTRY
- ?d? DATE
- ?________ ?
- ?holidays holidays \ c? ? d?
- ?__________
48Relation Example
- An operation to find the dates ds! of all public
holidays in country c? is - ?Dates_____
- ??Hols
- ?c? COUNTRY
- ?ds? ?DATE
- ?________ ?
- ?ds! holidays ?c??
- ?__________
49Inverse of a relation
- The inverse of a relation R from X to Y
- R X ? Y is written
- R
- and it relate Y to X (mirror image). So if
- x R y
- then
- y Rx
50Family Example
- PERSON the set of all persons
- father,mother PERSON ? PERSON
- x father y meaning x has father y
- v mother w meaning v has mother w
51Family Example parent relation
- parent PERSON ? PERSON
- can be defined as
- parent father ? mother
52Family Example sibling
- sibling PERSON ? PERSON
- can be defined as
- sibling (parent parent ) \ id PERSON
- This is defined as the relation parent composed
with its own inverse, in other words the set of
persons with the same parent. Note the inverse of
parent that relates parent to child. A person is
not usually counted as their own sibling, hence
the use of the identity relation.
53sibling PERSON ? PERSON can be defined
as sibling (parent parent ) \ id
PERSON children parents ?PERSON children
Pat, Bernard,Marion parents Liam, Mary
parentparent
R
Q
Pat Bernard Marion
Liam Mary
Pat Bernard Marion
children
Z
Y
54 (parent parent ) (Pat, Pat),
(Pat,Bernard), (Pat,Marion), (Bernard, Bernard),
(Bernard,Pat),
(Bernard,Marion), (Marion,Marion),
(Marion,Pat),
(Marion,Bernard) (parent parent ) \ id
PERSON (Pat,Bernard), (Pat,Marion),(Bernard,P
at),
(Bernard,Marion), (Marion,Pat),
(Marion,Bernard)
parentparent
parent
parent
Pat Bernard Marion
Liam Mary
Pat Bernard Marion
children
Z
Y
55Family Example ANCESTOR
- ancestor PERSON ? PERSON
- can be defined as
- ancestor parent
- The relation ancestor can be defined as the
repeated composition of parent.
56Chapter 9 Question 1
- Express the fact that the language Latin is not
the official language of any country
57Chapter 9 Question 1
- Express the fact that the language Latin is not
the official language of any country - LatinLANGUAGE
- Latin ? ran speaks
58Chapter 9 Question 2
- Express the fact that Switzerland has four
languages
59Chapter 9 Question 2
- Express the fact that Switzerland has four
languages - speaks ? Switzerland ? 4
60Chapter 9 Question 3
- Give a value to the relation speaksInEU which
relates countries which are in the set EU to
their languages.
61Chapter 9 Question 3
- Give a value to the relation speaksInEU which
relates countries which are in the set EU to
their languages. - EU ? COUNTRY
- speaksInEU COUNTRY ? LANGUAGE
- speaksInEU EU ? speaks
62Chapter 9 Question 4
- Give a value to the relation grandParent
63Chapter 9 Question 4
- Give a value to the relation grandParent
- grandParent PERSON ? PERSON
- grandParent parent parent
64Chapter 9 Question 5
- A persons first cousin is defined as a child of
the persons aunt or uncle. Give a value for the
relation firstCousin.
65Chapter 9 Question 5
- A persons first cousin is defined as a child of
the persons aunt or uncle. Give a value for the
relation firstCousin. - firstCousin PERSON ? PERSON
- firstCousin (grandParent grandParent ) \
sibling
66Chapter 9 Question 6
- Given
- PERSON set of uniquely identifiable persons
- MODULE set module numbers at a university
- students,teachers,EU,inter ? PERSON
- offered ? MODULE
- studies PERSON ? MODULE
- teaches PERSON ? MODULE
67Chapter 9 Question 6
- PERSON set of uniquely identifiable persons
- MODULE set module numbers at a university
- students,teachers,EU,inter ? PERSON
- offered ? MODULE
- studies PERSON ? MODULE
- teaches PERSON ? MODULE
- Explain the predicates
- EU ? inter ?
- EU ? inter students
- dom studies ? students
- dom teaches ? teachers
- ran studies ? offered
- ran teaches ran studies
68Chapter 9 Question 6
- Explain the effect of each predicates
- EU ? inter ?
- EU ? inter students
- dom studies ? students
- dom teaches ? teachers
- ran studies ? offered
- ran teaches ran studies
- Students are either from the EU or overseas, but
not from both. - Students study and teachers teach.
- Only offered modules can be studied.
- Those modules that are taught are studied.
69Chapter 9 Question 7
- Give an expression for the set of modules studied
by a person p.
70Chapter 9 Question 7
- Give an expression for the set of modules studied
by a person p. - studies ? p ?
71Chapter 9 Question 8
- Give an expression for the number of modules
taught by a person q.
72Chapter 9 Question 8
- Give an expression for the number of modules
taught by a person q. - (teaches ? q ? )
73Chapter 9 Question 9
- Explain what is meant by the inverse of the
relation studies - studies
74Chapter 9 Question 9
- Explain what is meant by the inverse of the
relation studies - studies
- The inverse of the relation studies relates
modules to persons studying them.
75Chapter 9 Question 10
- Explain what is meant by the composition of the
relations - studies teaches
76Chapter 9 Question 10
- Explain what is meant by the composition of the
relations - studies teaches
- The composition of relates students to the
teachers who teach modules the students study.
77Chapter 9 Question 11
- Give an expression for the set of persons who
teach person p.
78Chapter 9 Question 11
- Give an expression for the set of persons who
teach person p. - (studies teaches)? p ?
79Chapter 9 Question 12
- Give an expression for the number of persons who
teach both person p and person q.
80Chapter 9 Question 12
- Give an expression for the number of persons who
teach both person p and person q. - ((studies teaches)? p ? ?(studies
teaches)? q ?)
81Chapter 9 Question 13
- Give an expression for that part of the relation
studies that pertains to international
students(inter) -
82Chapter 9 Question 13
- Give an expression for that part of the relation
studies that pertains to international
students(inter) -
- inter ? studies
83Chapter 9 Question 14
- Give an expression for that states that p and q
teach some of the same international students.
84Chapter 9 Question 14
- Give an expression for that states that p and q
teach some of the same international students. - ((teaches studies )? p ? ? (teaches studies
) ? q ?) ? inter ? ?
85Chapter 9 Question 15
- The following declarations are part of the
description of an international conference. - PERSON set of uniquely identifiable persons
- LANGUAGE the set of world languages
- official ? LANGUAGE
86Chapter 9 Question 15
- The following declarations are part of the
description of an international conference. - PERSON set of uniquely identifiable persons
- LANGUAGE the set of world languages
- official ? LANGUAGE
87Chapter 9 Question 15
- PERSON set of uniquely identifiable persons
- LANGUAGE the set of world languages
- official ? LANGUAGE
- ?CONFERENCE_____
- ? ? PERSON
- ? ? LANGUAGE
- ? _speaks_ PERSON ? LANGUAGE
- ?__________
88Chapter 9 Question 15A
- Write expression that states every delegate
speaks at least one language - delagates ? dom speaks
89Chapter 9 Question 15B
- Write expression that states every delegate
speaks at least one official language of the
conference - delagates ? ran speaks ? ?
90Chapter 9 Question 15C
- Write expression that states every delegate
speaks at least one official language of the
conference - ?langLANGUAGE ?
- (?delPERSON del? delagates ? del speaks lang)
91Chapter 9 Question 15E
- Write an operation schema to register a new
delegate and the set of languages he or she
speaks. - ?Register_____________
- ? del?PERSON
- ? lang? ?LANGUAGE
- ?? CONFERENCE
- ?________________
- ?del? ? delegates
- ?delegates delegates ? del?
- ?speaks
- ?speaks ? lanLANGUAGE lan? lang? ? del? ?lan
- ?official official
- ?_________________
92Labs
- Using pen and paper do Chapter 2 Exercises 5.
- Using pen and paper do exam example Edward,
Fleur, and Gareth. - Type both of the above into Word using symbols or
Z fonts.
93Labs
- Calculate the following unions.
- S1 1,2, S2 2, 3, 4, 5, S3 1, 2, 2, 3,
4, 5, union(S1,S2,W). - S1 1,2, S2 2, 3, 4, 5, S3 1, 2, 2, 3,
4, 5, union(S1,S3,W). - S1 1,2, S2 2, 3, 4, 5, S3 1, 2, 2, 3,
4, 5, union(S3,S2,W).
94Labs
- Start Prolog
- Copy sets.pro from X\distrib\wmt2\maths\prolog
- Consult sets.pro
- Use the sets programs to solve the previous union
of S1,S2 and S3.
95Labs
- Using prolog do exam example Edward, Fleur, and
Gareth.
96Labs
- 1)Using pen and paper do Chapter 2 Exercises 5.
- 2)Using pen and paper do exam example Edward,
Fleur, and Gareth. - 3)Calculate the following unions.
- S1 1,2, S2 2, 3, 4, 5, S3 1, 2, 2, 3,
4, 5, union(S1,S2,W). - S1 1,2, S2 2, 3, 4, 5, S3 1, 2, 2, 3,
4, 5, union(S1,S3,W). - S1 1,2, S2 2, 3, 4, 5, S3 1, 2, 2, 3,
4, 5, union(S3,S2,W). - 4)Type both 1 and 2 above into Word using symbols
or Z fonts. - 5)Start Prolog
- 6)Copy sets.pro from X\distrib\wmt2\maths\prolog
- 7)Consult sets.pro
- 8)Use the sets programs to solve the previous
union of S1,S2,S3 - 9)Using prolog do exam example Edward, Fleur, and
Gareth.