Functions and Sequences
Dr. Rong Qu
http://www.cs.nott.ac.uk/ rxq/#g53fsp ∼
G53FSP Formal Specification 1 Functions
A function as used in Z
Is a discrete function
Acts on a discrete element from a particular set (its domain) as its argument
Delivers a corresponding element from the same or another set as its result
For this purpose it is defined to be a relation with its left-hand elements unique
G53FSP Formal Specification 2 Functions The condition for a relation to be a function is that
#R = #dom R
Mapping We talk of the function mapping elements from its domain into elements of its range
G53FSP Formal Specification 3 Examples
The following relations will be functions
Mapping bank customers to sets of account numbers (but not mapping customers to single account numbers, since one customer may have several accounts)
Mapping file-names to areas on disc (if each file occupies a unique and distinct area)
Mapping car number-plates to model names (all number plates should be distinct)
G53FSP Formal Specification 4 Partial Function
A partial function is one where the domain is a proper subset of the possible left-hand elements of the relation symbol: + instead of → ↔
G53FSP Formal Specification 5 Total Function
A total function is one in which the domain is the complete set If a relation acted on bank account numbers and delivered the amount in that account, it would be a partial function from the set of all possible account numbers (perhaps a 10 digit numbers). So we would declare it in a Z schema as balance : account number+ cash amount →
G53FSP Formal Specification 6 Injective Function
An injective function is one whose inverse is also a function Concepts of partial and total functions still apply.
Partial injective functions: >+ → Total injective functions: > → An injective function is a one-one relation
G53FSP Formal Specification 7 Surjective Function
A function is surjective if its range is the total set of possible objects Similarly it can be both partial and total The adjectives partial and total still refer to the domain A function is bijective if it is both injective and surjective
G53FSP Formal Specification 8 Note
Functions are a particular type of relation All the operations described in the previous section also apply to functions When writing explicit entries in a function, we shall normally use the symbol instead of a comma 7→
G53FSP Formal Specification 9 Sequences
The concept of an ordered collection of elements is very important in computing A queue is a collection of objects in which the ordering is important, and will occur in many real life situations
G53FSP Formal Specification 10 Sequences In declarations we use the type seq, as
waiting : seq people
file : seq blocks people is the type of object in waiting blocks is the type of objects occupied by a file
G53FSP Formal Specification 11 Example If people is the set
F red, John, Sue { } We may write it explicitly as
queue = (1, F red), (2, John), (3, Sue) { } Or for more clarity as
queue = 1 F red, 2 John, 3 Sue { 7→ 7→ 7→ }
G53FSP Formal Specification 12 Example To specify a particular sequence directly, the notation can be simplified as < F red, John, Sue >
Empty sequence as <>
G53FSP Formal Specification 13 Concatenation
the concatenation operator ˆ
Acts between sequences
Gives a sequence as a result
With the left-hand operand as the first set of elements in the sequence
Followed by the elements from the right-hand operand
G53FSP Formal Specification 14 Squash squash: monadic operator Takes as argument any function whose domain is a subset of N Results a sequence, in which
The range elements of the original function have been ordered by the numeric values in the domain
Domain elements have been replaced by consecutive integer values starting at 1
G53FSP Formal Specification 15 Squash Thus
squash 9 e, 5 f, 7 r, 11 d =< f, r, e, d > { 7→ 7→ 7→ 7→ } It is often used as in
new seq = squash( 5 ¡old seq)ˆanother seq { }−
G53FSP Formal Specification 16 Head and Tail head, tail: monadic operators
Operate on a sequence
head delivers the first object (a single element)
tail delivers the sequence with the head removed (a sequence may be empty)
Total injective functions:
G53FSP Formal Specification 17 Summary Functions
Partial and total (+ , ) → → Partial and total injective (>+ , > ) → → Surjective and bijective
Sequences
Concatenation ˆ
Squash (order), Head and tail (remove)
G53FSP Formal Specification 18