1 / 96

Relations

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.

Download Presentation

Relations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Relations Chapter 9 Formal Specification using Z

  2. 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.

  3. 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

  4. 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}

  5. 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

  6. 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.

  7. 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) }

  8. 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}

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. 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

  14. 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

  15. 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

  16. 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)

  17. Declaring 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 toLANGUAGE

  18. 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

  19. 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

  20. 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

  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

  22. 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)} ________________________

  23. 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}

  24. 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}

  25. 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

  26. 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: • 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}

  27. 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  jn:  i=j+ n ________________

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. Composition • 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

  34. Forward Composition • Given: • R: XY and • Q: YZ • forward composition is defined as: • R;Q: XZ

  35. Forward Composition R;Q R Q X Z Y

  36. 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

  37. Backward Composition • Given: • R: XY and • Q: YZ • backward composition is defined as: • QR: R;Q

  38. 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: XX • R ; R: XX

  39. 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.

  40. 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

  41. 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

  42. Identity Relation • The identity relation • id X • is the relation which maps all x’s on to themselves: • id X == {x:X x  x }

  43. 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

  44. 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 __________

  45. 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) __________

  46. 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?} __________

  47. 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?} __________

  48. 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? __________

  49. Inverse 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 R~x

  50. 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

More Related