970 likes | 1.22k Views
Relations. Chapter 9 Formal Specification using Z. Formal 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. Formal definition of Relation.
E N D
Relations Chapter 9 Formal Specification using Z
Formal 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.
Formal 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} x{hardtop,softtop} = • {(red, hardtop), (red, softtop), (green, hardtop), (green,softtop)} • This set consists of ordered paired called a tuple
Formal 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 X=Y, 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}
Relation 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
Relation 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.
Relation 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) }
Relation 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}
Let 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.
Let 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,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.
Types 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.
Types 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.
Types 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 x=y
Relations • 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
A 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
RELATIONS • 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)
Declaring Relations in Z • R: XY ( equivalent to (X Y) ) • SPEAKS: COUNTRY LANGUAGE • ( equivalent to (COUNTRY LANGUAGE) ) • In English we say: • R relates X to Y. • SPEAKS relates COUNTRY toLANGUAGE
MAPLETS • 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
Membership • 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
Infix 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
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
Domain 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)} ________________________
Domain 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}
Range 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}
The DOMAIN and RANGE of the relation Rdom 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
Relational 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: • RS • 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}
Constant 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 jn: i=j+ n ________________
Domain 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
Range 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
Domain 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
Range 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
Example 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
Composition • Relations can be joined together in an operation called composition. Given: • R: XY and • Q: YZ • We can have forward composition or backward composition
Forward Composition • Given: • R: XY and • Q: YZ • forward composition is defined as: • R;Q: XZ
Forward Composition R;Q R Q X Z Y
For any pair (x,y) related by R forward composed with Q x R;Q z there is a y where R relates x to y and Q relates y to z; y:Y xRy yQz R;Q R Q X Z Y
Backward Composition • Given: • R: XY and • Q: YZ • backward composition is defined as: • QR: R;Q
Repeated 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: XX • R ; R: XX
Composition 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.
Composition Example • The two facts: • 1. France borders Germany • 2. Germany borders Austria • can be represented by: • France borders;borders Austria • We can write: • Spain borders3 Denmark • for • Spain borders; borders; borders Denmark
Transitive 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
Identity Relation • The identity relation • id X • is the relation which maps all x’s on to themselves: • id X == {x:X x x }
Reflexive 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* xis true, even if x Rx is false, (id x) • France borders* France is true
Relation 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 __________
Relation 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) __________
Relation 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?} __________
Relation 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?} __________
Relation 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? __________
Inverse of a relation • The inverse of a relation R from X to Y • R: XY is written • R~ • and it relate Y to X (mirror image). So if • x R y • then • y R~x
Family 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