24
The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Embed Size (px)

Citation preview

Page 1: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

The Z Notation: relations and functions

Compiled ByTariq R. Soomro, Ph.D.

Reference: Text Book

Week-7

Page 2: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

RelationsExample from Text Book:

– Jim plays piano– John plays guitar

• Here, the first element is taken from the set of people and the second from the set of instruments

• A person is related to instrument if he or she plays that instrument– Jim is brother of John– Janny is mother of Jim– Janny is mother of John

• In this case, pairs of people are related so the two sets concerned are in fact the same.

• We borrow the familiar term ‘relation’, used to describe kinship of people, to refer to any interesting connection between the element of sets.

Page 3: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

• It is sometimes helpful when thinking about relations to use pictures to gain an intuition about the idea before making a formal expression of them using mathematical notation.

• This can be done in the form shown below.• We could capture some information about modes of

transport of the Potter family from the following:

Page 4: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations• In this example Kate and Tricycle have no connecting arrows, which

means that Kate does not ride any of the wheeled vehicles and that none of the Potters rides a tricycle.

• The relation itself is completely defined by the connection between the elements of the sets

• Another way of recording the information contained in the picture would be to write down all the pairs of related elements.

• For the rides relation these are:(Alice, Bicycle) (Huw, Bicycle) (Ben, Unicycle) (Ben, Bicycle)

rides == { (Alice, Bicycle), (Huw, Bicylce), (Ben, Unicycle), (Ben, Bicycle)}

Also, form our knowledge of sets we can deduce that the type of rides is: P (Potters x WheeledVehicles) since this is the type of such a set of order pairs.

Page 5: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

• Since the idea of a relation is so useful and occurs so often in Z specifications we introduce some special notation.

• A relation between sets X and Y is a member of: – P (X x Y)

• As a shorthand for this, and to emphasize the relational nature of a set of ordered pairs, we introduced the double-headed arrow notation:– X ↔ Y == P (X x Y)

• The notation can be used when declaring relations, for instance:– Rides: Potters ↔ WheeledVehicles– Plays: People ↔ Instruments

Page 6: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

• A special notation may also be used for the pairs which make up a relation.

• Instead of writing (x, y) for a related pair we sometimes use the maplet notation:– x y

• Which is read ‘x maps to y’• Using maplet notation the rides relation may be written:• {Alice Bicycle, Huw Bicycle, Ben Unicycle, Ben

Bicycle}• And as before we can make statements about maplet:

– Alice Bicycle Є rides and– {Huw Bicycle, Ben Bicycle} C rides

Page 7: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

• Given sets A and B, a relation R from A to B has declarationR : A ↔ B

• and is some subset of A × B, i.e. A ↔ B is an alternative denotation for the set P(A × B).

• As an example, consider the relation factor where A is the set 3 . . 6 and B the set 6 . . 9, and where an element x Є 3 . . 6 is related to an element y Є 6 . . 9 if and only if x is a factor of y.

• The relation is formally defined by

Page 8: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

•This example illustrates the use of an open schema box — a two dimensional form of schema text. •The relation name is introduced and typed in the declaration part (above the horizontal dividing line). •The predicate specifying precisely which ordered pairs are in the relation appears in the predicate part (below the horizontal line). •In effect, the horizontal line serves the role of ‘|’ in non-boxed schema text and is also read as ‘such that’. •The ‘open’ indicates that the quantities defined are accessible within the environment in which the box is placed.

Page 9: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Domain and range• The two sets used in the declaration of a relation are

sometimes referred to as the source and target respectively

• The rides relation is an example of a relation between Potters and WheeledVehicles: in this case, Potters is the source set and WheelVehicles is a target.

• A relation need not relate every member of the source or of the target; in either set, only a subset might be used in the description of the relation

• In the rides example Kate is a member of the source but is not related to anything, and in the target set Tricycle is an element not ridden by anyone

Page 10: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Domain and range• It can be useful to give names to the subsets of the

source and target whose elements really are involved in the description of the relation

• The subset of elements of the source which are related to at least one element of the target is known as the domain of the relation

• The domain of rides is: {Alice, Huw, Ben}• Similarly, the range consists of all elements of the target

set which are related to some elements of the source• The range of rides is: {Bicycle, Unicycle}

Page 11: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Domain and range• Given a relation R : A ↔ B, the domain of R,

denoted dom R, is the set of elements of A related by R to some element in B, i.e.– dom R = {a : A | b : B • a b ε R}.

• Similarly, the range of R, denoted ran R, is the set of elements of B which are related by R to some element in A, i.e.– ran R = {b : B | a : A • a b ε R}.

Page 12: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

RelationsOperations on relationsRelational inverse• Sometimes it may be useful to look at relations the other way round• For example, having defined the rides relation it may be found

desirable to know what is ridden by whom rather than who rides what

• We might call this the is isriddenby relation• To obtain the new relation we would have to reverse the order of

each pair in the original• For instance, since:

– Ben rides Unicycle• Is part of rides relation, we would expect to see

– Unicycle isriddenby Ben• Reversing all the pairs define the new relation:

– Isriddenby == {(Unicycle, Ben), (Bicycle, Ben), (Bicycle, Alice), (Bicycle, Huw)}

Page 13: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Operations on relations

Relational inverse• This process of reversing a relation gives a new

relation which is known as the inverse• We denote the inverse operation by the

suerscript, ~• So for example:

– isriddenby = rides~

– rides = isriddenby~

Page 14: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

RelationsOperations on relationsRestriction and Subtraction:• Suppose that, given the rides relation, the modes of transport of

Alice and Kate are found to be special interest, perhaps as part of a survey to see which cycles women ride

• We might want to make a smaller relation by considering only the pairs which relate Alice and Kate to some wheeled vehicle

• Restricting rides in this way would produce the relation:– wrides = {Alice Bicycle}

• Creating a smaller relation like this, by considering only part of the original domain, is known as domain restriction

• The following notation is used:– wrides == {Alice, Kate} ◄ rides

• The domain restriction operator, ◄, appears between the set that we wish to restrict the domain to and the name of the relation being restricted

Page 15: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

RelationsOperations on relationsRestriction and Subtraction:• Notice that the restricting set appears on the left with one point of

the triangle facing towards it• There is a complementary range restriction operation which creates

a smaller relation by considering only a part of the original range• For instance, suppose we are interested in a relation which tells us

who rides a bicycle• This can be obtained by restricting the range of rides to the

singleton set {Bicycle}• For this we would write

– rides ►{Bicycle}• The range restriction the restricting set appears to the right of the

range restriction symbol ►

Page 16: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Operations on relationsRestriction and Subtraction:• Supposed that instead of restricting the

domain (or range) to a certain set we have a particular set of elements which we would like to remove from the domain (or range)

• This can be done in similar way by using domain (or range) subtraction

Page 17: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Operations on relations

Restriction and Subtraction:

Page 18: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Operations on relationsComposition of relation:• Given two relation such that the range type of the first is

the same as the domain type of the second, it is useful to be able to put the relation ‘end to end’ and form a single relation containing all pairs joined by some common element

• To see how this works, let us define a new relation hasweels which relates wheeled vehicles to the number of wheels they possess:– haswheels == {Unicycle 1, Bicycle 2, Tricycle 3}

• We now have two relations:– rides: Potters ↔ WheeledVehicles– haswheels: WheeledVehicles ↔ N1

Page 19: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Operations on relationsComposition of relation:• The range type of rides and the domain type of

haswheels are the same so we can compose rides with haswheels to produce a new relation ridesonwhools of type Potters ↔ N1 which records how many wheels each of the Potters can ride on

• A picture shows how this composition is achieved

Page 20: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Operations on relations

Composition of relation:

Page 21: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Operations on relationsComposition of relation:• To find which Potters are related to which numbers we must include

all possible paths through this diagram from Potters to N1 • This shows that:

– ridesonwheels == {Ben 1, Ben 2, Alice 2, Huw 2}

• The composition operation is donoted by ; and we write:– ridesonwheels == rides ; haswheels

• As another example, supposed we have two relations, R1, R2 : N ↔ N with:– R1 = {0 1, 2 0, 2 1}– R2 = {1 0, 1 2, 2 1}

• As before we draw a diagram to represent their composition:

Page 22: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Operations on relations

Composition of relation:

Page 23: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Operations on relationsComposition of relation:• To obtain R1 ; R2 we must find all pairs of numbers where the first is

in dom R1 the second is in ran R2 and they are connected to some common intermediate number by both R1 and R2

• This time there are more branching paths so we must be careful to include all the possibilities

• We see that R1 connects 0 to 1 and that R2 connects 1 to both 0 and 2

• So the composed relation must contain both (0,0) and (0,2).• R1 does not connect 1 to anything, so there can be no pairs with first

element 1 in the composition• Finally, 2 is related to both 0 and 1 by R1

Page 24: The Z Notation: relations and functions Compiled By Tariq R. Soomro, Ph.D. Reference: Text Book Week-7

Relations

Operations on relations

Composition of relation:• R2 does not connect 0 to anything so that path comes to an end

• But again, 1 is connected to both 0 and 2 so the pairs (2,0) and (2,2) are both present in the composition

• Putting this together gives:– R1 ; R2 {0 0, 0 2, 2 0, 2 2}