Top Banner
1 Functions Chapter 10 Formal Specification using Z
46

Functions

Jan 21, 2016

Download

Documents

Derick Derick

Functions. Chapter 10 Formal Specification using Z. A function is a relation. In a programming language a function is a way of specifying some processing which produces a value as a result. In Z a function is a data structure. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Functions

1

Functions

Chapter 10

Formal Specification using Z

Page 2: Functions

2

A function is a relation

• In a programming language a function is a way of specifying some processing which produces a value as a result.

• In Z a function is a data structure.• The two views are not incompatable; the

programming language view is just a restrictive form of the Z view and in both cases a function provides a result value given an input value or values.

Page 3: Functions

3

A function in Cint addTwo(int arg) Example of usage

{ y = addTwo(7);

return (arg+2);

}

arg1

arg2

Argument

return1

return2

Return value

addTwo()

Page 4: Functions

4

A function is a relation• A function is a special case of a relation in which there is at

most one value in the range for each value in the domain. A function with a finite domain is also known as a mapping. Note in diagram no diverging lines from left to right.

x1

x2

x4

x6

X

y1

y2

f

dom f ran f

Y

x5

x3

Page 5: Functions

5

A function is a relation• In Z a function f from type X to type Y is declared as • f: X Y ‘the function f, from X to Y’

• Is equilanent to relation f: X Y with the restriction that for each x in the domain of f, f relates x to at most one y;

x1

x2

x4

x6

X

y1

y2

f

dom f ran f

Y

x5

x3

Page 6: Functions

6

Is equilanent to relation f: X Y with the restriction that for each x in the domain of f, f relates x to at most one y;

• Which can be stated formally as:

• x dom f 1y:Y xfy

x1

x2

x4

x6

X

y1

y2

f

dom f ran f

Y

x5

x3

Source Target

Page 7: Functions

7

Examples of functions

• The relation between persons and their identity numbers • identityNo: PERSON • is a function if there is a rule that a person may only have

one identity number . In that case it could be declared as:• identityNo: PERSON • There would probably also be a rule that only one identity

number may be associated with any one person, but that is not indicated here.

Page 8: Functions

8

Examples of functions

• A functions source and target can be the same:

• hasMother: PERSON PERSON

• any person can have only one mother and several people could have the same mother.

Page 9: Functions

9

Function Application• All the concepts which pertain to relations apply to

functions.

• In addition a function may be applied.

• Since there will be at most only one value in the range for a given x it is possible to designate that value directly.

• The value of f applied to x is the value in the range of function of the function f corresponding to the value of x in its domain.

Page 10: Functions

10

Function Application• The application is undefined if the value of x is not

in the domain of f.

• It is important to check that the value of x is in the domain of the function f before attempting to apply f to x.

• The application of the function to the value x (the argument) is written: fx or f(x)

Page 11: Functions

11

Function Application

• We can check the applicability of f by writing: • x domf • fx = y• These predicates must be true if f(x) = y is a function.

Page 12: Functions

12

Partial and Total Functions• The identyNo function is a partial function there may be

values in the sources that are not in the domain (some people may have no identity number).

• A total function is one where there is a is a value for every possible value of x, so fx is always defined. It is written

• f: X Y • Because: • domf = X (or source)• function application can always be used with a total

function. Note no ‘bar’ on total function arrow.

Page 13: Functions

13

Examples of Total Functions

• The function age, from PERSON to natural number, is total since every person has an age:

• age: PERSON • The function hasMother, is total since every

person has one mother (including a mother!): • hasMother : PERSON PERSON

Page 14: Functions

14

Formal Partial and Total functions

• The set of Partial Functions from X to Y:

X Y =={f:X Y | ( x:X | x dom f (1 y:Y xfy)) f}

• The set of Total Functions from X to Y:

X Y =={f:X Y | ( dom f =X f}

Page 15: Functions

15

Other Classes of Functions: injective functions (one-to-one)

• An injection or (injective function) is a function which maps different values of the source on to different values of the target, e.g. f: X Y

• The inverse relation of an injective function f

from X to Y , f~ , is itself an injective function from, from Y to X.

• f~ Y X

• An injective function may be partial f: X Y or total f: X Y . The functions identityNo and MonogamousMarriage are injective.

Page 16: Functions

16

An Injective function.

The function f: X Y is injective (or one-to-one) because each element in Y has one arrow pointing to it. A injective can be partial, f: X Y, or total f: X Y ;

x1

x2

x3

X

y1

y2

y3

f

dom f ran f

Y

Source Target

Page 17: Functions

17

An Injective function can be:partial, f: X Y =={f:X Y | f~ Y X f} (has inverse)

or total f: X Y =={f:X Y | dom f = X f}

x1

x2

x3

X

y1

y2

y3

f

dom f ran f

Y

Source Target

Page 18: Functions

18

A Surjective function.

The function f: X Y is surjective (or onto) because range of is all of Y. The range is the whole of the target. A surjective can be partial, f: X Y, or total f: X Y ;

x1

x2

x3

x4

x4

X

y1

y2

y3

f

dom f ran f

Y

Source Target

Page 19: Functions

19

A Surjective function can be:partial f: X Y == {f:X Y | ran f = Y f}

or total f: X Y == {f:X Y | dom f = X }

x1

x2

x3

x4

X

y1

y2

y3

f

dom f ran f

Y

Source Target

Page 20: Functions

20

A Bijective function.

The function f: X Y is a total function that is both injective (one-to-one) and surjective (or onto) is called a bijective function, ;

x1

x2

x3

X

y1

y2

y3

f

dom f ran f

Y

Source Target

Page 21: Functions

21

A Bijective function.The function f: X Y is a total function that is both injective

(one-to-one) and surjective (or onto) is called a bijective function, f: X Y == (X Y) (X Y);

x1

x2

x3

X

y1

y2

y3

f

dom f ran f

Y

Source Target

Page 22: Functions

22

A function is called a bijection , if it is onto and one-to-one. A bijective function has an inverse

Function Application

Some examples, range on the left domain on the right

A function f from X to Y is a relation from X to Y having the properties:1. The domain of f is in X.2. If (x,y), (x,y’) f, then y=y’ (no fan out from domain).A total function from X to Y is denoted f: X Y.

Page 23: Functions

23

Example Function

Given

SIDE ::= left | right

the function

driveON COUNTRY SIDE side of the road vehicles drive on

is a surjection, because the range includes all the values of the type SIDE. It would usually be considered total because driveOn should be defined in all countries.

driveON COUNTRY SIDE

Page 24: Functions

24

What is a Hash Function?• A hash function takes a data item to be stored or

retrieved (say to/from disk) and computes the first choice for a storage location for the item. For example assume StudentNumber is the key field of a student record, and one record is stored on one disk block. To store or retrieve the record for student number n in a choice of 11 disk locations numbered 0 to 10 we might use the hash function.

• h(n) = n mod 11

Page 25: Functions

25

0 1 2 3 4 5 6 7 8 9 10

Describe the steps needed to store a record for student number 257

using in locations disk 0-10 using h(n) = n mod 11

132 515102 558 32

Steps to store 257 using h(n)=n mod 11 into locations 0-10

1. Calculate h(257) giving 4.

2. Note location is already occupied, a collision has occurred.

3. Search for next free space , with 0 assumed to follow 10.

4. If no free space then array is full, otherwise store 257 in next free space

0 1 2 3 4 5 6 7 8 9 10

132 515102 558 32257

Page 26: Functions

26

Hash Function Collision, Search Criterion

• A collision occurs if h(x) = h(y), for x y

• A collision resolution policy is required. One simply approach is to find the next unoccupied cell.

• The search criterion must be an equality condition on a single field. Not suited to range searches (e.g. Find StudentNumber < 123)

• Range: StudentNumber 1…. 99,999,999.

Page 27: Functions

27

Hash Function Collision

• h(x) does not guarantee that distinct key values will hash to distinct disk address .

• The domain may be much larger than the range If that is the case some keys will map onto the same disk address.

Page 28: Functions

28

Hash Function Collision

• The hash function maps the hash field space to the disk address space. It is usually a partial surjective function

• HashFieldSpace DiskAddressSpace.

• What are the implications of the hash function being a total function?

Page 29: Functions

29

Hash Function Collision• Problem if the the hash function is total i.e. defined for

all elements in the domain. Many collisions• h(n) = n mod 3

x1

x2

x3

x4

x6

N

y0

y1

y2

A

Key Space = 6 Disk Block space =3

Page 30: Functions

30

Hash Function Collision

• Also important is the selection of keys from the key space.

• Will they be random or clustered?

Page 31: Functions

31

What properties should the function possess?

• If the domain is greater than the range it is should a partial surjective function.

• surjective : The range is the whole of the target, all the disk addresses could be used.

• partial there may be values in the sources that are not in the domain, not all keys will be used .

HashFieldSpace DiskAddressSpace

Page 32: Functions

32

Is hash efficient?

• If collisions occur infrequently and if when one does occur it is resolved quickly, then hashing provides a very fast method for storing and retrieving data. As an example, personnel data are frequently stored and retrieved by hashing on employee identification number.

• The hash function h should distribute the records uniformly over the storage locations; otherwise, search time will be increased because many collisions occur.

Page 33: Functions

33

Functions and Hash Functions• When a hash function is used, the following conditions could hold:

#source > #target and #domain < #source and #range=#target .

• Where source is all possible record keys and target is actual disk blocks available. The domain is actual records keys and the range is the actual disk blocks available.

• What are the implications of a hash function being a total function?

• What are the implications of a hash function being an invective function?

• What are the implications of a hash function being a bijective function?

• In general what are the desirable properties that a hash function should possess?

Page 34: Functions

34

Functions and Hash Functions• When a hash function is used, the following conditions could hold:

#source > #target and #domain < #source and #range=#target .

• Where source is all possible record keys and target is actual disk blocks available. The domain is actual records keys and the range is the actual disk blocks available.

• What are the implications of the hash function being a total function?

• It is not stated wheather the #domain>#range if it is the problem is many collisions occur when the hash function is total i.e. defined for all elements in the domain.

Page 35: Functions

35

Functions and Hash Functions• When a hash function is used, the following conditions could hold:

#source > #target and #domain < #source and #range=#target .

• Where source is all possible record keys and target is actual disk blocks available. The domain is actual records keys and the range is the actual disk blocks available.

• What are the implications of a hash function being a invective function?

• One to one, an element in the domain will have a corresponding unique element in the range:- no collisions. But some source elements may not be mapped into the the range.

Page 36: Functions

36

Functions and Hash Functions• When a hash function is used, the following conditions could hold:

#source > #target and #domain < #source and #range=#target .

• Where source is all possible record keys and target is actual disk blocks available. The domain is actual records keys and the range is the actual disk blocks available.

• What are the implications of a hash function being a bijective function?

• The property of having an inverse is not normally of interest as we are usually working from domain (HashFieldSpace) to the

range(DiskAddressSpace).

Page 37: Functions

37

Exercise 1

• A system records the booking of hotel rooms on one night. Given base types:

• [ROOM] set of all rooms in hotel

• [PERSON] set of all possible persons

• the state of the hotel’s bookings can be represented by the following schema:

Page 38: Functions

38

Exercise 1

HotelbookedTo: ROOM PERSON

(a) Explain why bookedTo is a function.(b) Explain why bookedTo is partial.

Page 39: Functions

39

Exercise 1 Answer

HotelbookedTo: ROOM PERSON

(a) Explain why bookedTo is a function.• bookedTo is a function since it maps rooms

to persons and for any given room(domain) at most one person (range) can book it. A person can book any number of rooms.

Page 40: Functions

40

Exercise 1 Answer

HotelbookedTo: ROOM PERSON

(b) Explain why bookedTo is partial.

The function is partial since not all rooms have been booked.

Page 41: Functions

41

Exercise 2

Given:

InitHotel’

bookedTo =

Page 42: Functions

42

Exercise 2

Given, first version of accept booking operation:

AcceptBooking0Hotel

p?:PERSON

r?: ROOM

r? dom bookedTo

bookedTo’ = bookedTo {r? p?}

Explain the meaning of each line

Page 43: Functions

43

Exercise 2 Answer

AcceptBooking0 (schema name)

Hotel (incorporates Hotel and its variables)

p?:PERSON ( person input variable)

r?: ROOM ( room input variable)

r? dom bookedTo ( room not booked)

bookedTo’ = bookedTo {r? p?}( the maplet relating the room to the person is

included in the new value of the function bookedTo)

Page 44: Functions

44

Exercise 3

Write a schema cancelBooking0 which cancels a booking for a given person and a given room. It should deal with error conditions in a similar manner as AcceptBooking0

Page 45: Functions

45

Exercise 3 Answer (a)

CancelBooking0Hotel

p?:PERSON

r?: ROOM

{r? p?} dom bookedTo

bookedTo’ = bookedTo \ {r? p?}

Explain the meaning of each line

Page 46: Functions

46

Exercise 3 Answer (b)

CancelBooking0(schema name)

Hotel (incorporates Hotel and its variables)

p?:PERSON ( person input variable)

r?: ROOM ( room input variable)

{r? p?} dom bookedTo ( room is booked)

bookedTo’ = bookedTo \ {r? p?}

( the maplet relating the room to the person is removed from

the new value of the function bookedTo)