relations relations
play

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


  1. Relations

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

  3. A “Registry” Specification Declarations registered is a set of STUDENTs options 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.

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

  5. The power set of STUDENT Sue Bill Bill John Sue Bill Sue John John Sue John Bill STUDENT= {Bill, John,Sue} Sue John Bill Three Elements

  6. If A has n elements, how many does its powerset have? 1. 2. 3. 4. 5. 0% 0% 0% 0% 0%

  7. Which of the following are in ? 1. 2. 3. 4. 5. 0% 0% 0% 0% 0%

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

  9. How many elements does A x B have? 1. 2. 3. 4. 0% 0% 0% 0%

  10. Relations A Relation, ρ , from a set A to a set B , � is a subset of A X B . 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) } �

  11. Relations in UML diagrams � The existence of a relationship between objects of type A and objects of type B is denoted: B A

  12. Relations: sets of ordered pairs (mary is_enrolled_on CS266) (john is_enrolled_on CS189) (mary, CS266) (john is_enrolled_on CS153) (john, CS189) (mark is_enrolled_on CS189) (mark, CS189) (john,CS153) (william is_enrolled_on CS266) (william, CS266) (sue is_enrolled_on CS153) (sue, CS153) (mark is_enrolled_on CS153) (mark, CS153)

  13. is_enrolled_on as a picture John CS153 CS189 Mary Sue CS266 Bill CS183 Mark CS288 William

  14. is_enrolled_on as a database table John CS153 John CS189 Mary CS266 Sue CS153 Mark CS153 Mark CS189 William CS266

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

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

  17. dom(is_enrolled_on) CS153 John CS189 Mary Sue CS266 Bill CS183 Mark William CS288 The set of students enrolled on some course

  18. What is dom(R)? { a,b,c} 0% 1. { (a,1),(a,2),(b,4)} 0% 2. 0% { a,b} 3. 0% { 1,2,4} 4. 1 a 2 b 3 c 4 R

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

  20. ran(is_enrolled_on) John CS153 CS189 Mary Sue CS266 Bill CS183 Mark CS288 William The set of courses with some students enrolled

  21. What is ran(R)? { 1,2,3,4} 0% 1. { 1,2} 0% 2. 0% { a,b} 3. 0% None of the above 4. 1 a 2 b 3 c 4 R

  22. Library example again � is_on_loan_to is a Library a set stock relation from a set members BOOKS to PEOPLE A rule is_on_loan_to � is_on_loan_to : BOOKS PEOPLE � We will use relational notation to specify the library system

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

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

  25. Exercise: returning books Return( book b, person p) b is currently on loan to p Remove b is on loan to p

  26. 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 ]

  27. More relational notation

  28. Running example: registered Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran

  29. Domain restriction

  30. registered without domain restriction Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran

  31. registered with domain restriction Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran

  32. Domain anti-restriction

  33. Example: Registered with domain anti-restriction Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran

  34. Range restriction

  35. Example: registered with a range restriction Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran

  36. Range anti-restriction

  37. Example: registered with a range anti-restriction Abigail CS189 CS153 Barney CS154 Charlotte CS173 Dora Emily CS183 Fran

  38. What is denoted by ? 0% The course CS154 1. 0% The people registered for CS154 2. Something else 0% 3.

  39. What is denoted by ? Those of ian, james, kate who have 1. 0% registered for some courses The courses ian, james, and kate have 0% 2. registered for 0% The course registrations that ian, 3. james, and kate are involved in

  40. What is denoted by ? The people not 1. registered for CS154 The people 2. registered for 0% 0% 0% courses other than CS154 The registrations for 3. courses other than CS154 The people not re... The people regist... The registrations...

  41. Relational inverse

  42. Example: inverse of registered CS189 Abigail CS153 Barney Charlotte CS154 Dora CS173 Emily CS183 Fran

  43. Relational image

  44. Example: Registered CS189 Abigail Barney CS153 Charlotte CS154 Dora CS173 Emily CS183 Fran

  45. Example: extracting information from a relation

  46. Exercise: extracting information

  47. What denotes the courses that Ian is registered for? 1. 2. 3. 4. 0% 0% 0% 0%

  48. Summary

  49. Please ensure you return your handset before leaving. The handset is useless outside of this class and non-returns will decease the likelihood of future voting system use on this course.

Recommend


More recommend