- 84 Views
- Uploaded on
- Presentation posted in: General

Functions

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Functions

Chapter 10

Formal Specification using Z

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

int addTwo(int arg) Example of usage

{ y = addTwo(7);

return (arg+2);

}

addTwo()

arg1

arg2

return1

return2

Return value

Argument

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.

f

x5

x1

x2

x4

x6

y1

y2

X

x3

Y

dom f ran f

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 Ywith the restriction that for each x in the domain of f, f relates x to at most one y;

f

x5

x1

x2

x4

x6

y1

y2

X

x3

Y

dom f ran f

Is equilanent to relation f: X Ywith 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

Target

Source

x5

f

x1

x2

x4

x6

y1

y2

X

x3

Y

dom f ran f

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

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

- 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 fapplied to x is the value in the range of function of the function f corresponding to the value of x in its domain.

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

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

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

- 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

- The set of Partial Functions from X to Y:
X Y

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

- The set of Total Functions from X to Y:
X Y

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

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

An Injective function.

The function f: X Yis 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 ;

Target

Source

f

x1

x2

x3

y1

y2

y3

X

Y

dom f ran f

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}

Target

Source

f

x1

x2

x3

y1

y2

y3

X

Y

dom f ran f

A Surjective function.

The function f: X Yis 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 ;

Target

Source

f

x1

x2

x3

x4

x4

y1

y2

y3

X

Y

dom f ran f

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 }

Target

Source

f

x1

x2

x3

x4

y1

y2

y3

X

Y

dom f ran f

A Bijective function.

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

Target

Source

f

x1

x2

x3

y1

y2

y3

X

Y

dom f ran f

A Bijective function.

The function f: X Yis 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);

Target

Source

f

x1

x2

x3

y1

y2

y3

X

Y

dom f ran f

Some examples, range on the left domain on the right

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

Function Application

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.

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

- 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

Describe the steps needed to store a record for student number 257 using in locations disk 0-10 using h(n) = n mod 11

558

32

132

102

15

5

0 1 2 3 4 5 6 7 8 9 10

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

558

32

132

102

15

5

257

0 1 2 3 4 5 6 7 8 9 10

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

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

- 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?

- Problem if the the hash function is total i.e. defined for all elements in the domain. Many collisions
- h(n) = n mod 3

Disk Block space =3

Key Space = 6

x1

x2

x3

x4

x6

y0

y1

y2

N

A

- Also important is the selection of keys from the key space.
- Will they be random or clustered?

- 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

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

- 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?

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

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

- When a hash function is used, the following conditions could hold:
#source > #target and #domain < #source and #range=#target .

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

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

Hotel___

bookedTo: ROOM PERSON

_______

(a) Explain why bookedTo is a function.

(b) Explain why bookedTo is partial.

Hotel___

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

Hotel___

bookedTo: ROOM PERSON

_______

(b) Explain why bookedTo is partial.

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

Given:

Init___

Hotel’

______

bookedTo =

_______

Given, first version of accept booking operation:

AcceptBooking0___

Hotel

p?:PERSON

r?: ROOM

_____________

r? dom bookedTo

bookedTo’ = bookedTo {r? p?}

_________________

Explain the meaning of each line

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)

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

CancelBooking0_

Hotel

p?:PERSON

r?: ROOM

_____________

{r? p?} dom bookedTo

bookedTo’ = bookedTo \ {r? p?}

_________________

Explain the meaning of each line

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)