18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and...

23
18 April 2009 Instructor: Tasneem Darwi sh 1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal Methods Sequences

Transcript of 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and...

Page 1: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 1

University of PalestineFaculty of Applied Engineering and Urban Planning

Software Engineering Department

Formal Methods

Sequences

Page 2: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 2

Outlines

Sequence notation

A model for sequences

Functions on sequences

Bags

Page 3: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 3

Sequence notationA sequence is an ordered collection of objects

If there are no objects in the collection, the sequence is the empty sequence, and it is written as

The expression denotes the sequence containing objects a, b, and c, in that order

Concatenation is when two sequences are combined in such a way that the elements of one follow the elements of the other, and order is maintainedExample:If s and t are sequences, we write s % t to denote the concatenation of s and t

Page 4: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 4

Sequence notationExample 9.1 The ticket office in a railway station has a choice of two counters at which tickets may be purchased. There are two queues of people, one at each counter; these may be modelled as sequences

To restrict elements of a sequence we can use the filter operator

if s is a sequence, then is the largest subsequence of s containing only those objects that are elements of the set A

Note: The order and multiplicity of elements must be preserved

Page 5: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 5

Sequence notationExample 9.2 In the station, there is a destination board displaying a list of trains, arranged in order of departure. This may be modelled as a sequence of pairs, each recording a time and a destination

Sally is interested only in those trains that are going to London;

Page 6: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 6

Sequence notationTo refer to the first element of a sequence, or to the part of the

sequence that follows the first element; these are called the head and tail, respectively

Notice that the head of a sequence is an element, while the tail is another sequence.

If s is any non-empty sequence

Page 7: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 7

Sequence notationExample 9.3 Sally wants to take the first train to London. From the list of trains on the destination board

Tim is still waiting to buy a ticket, and the first train is about to leave, then the list of available trains is given by `tail trains', the sequence

Page 8: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 8

Sequence notationif s is a sequence, then we write `#s' to denote the length of s. For

example

Example 9.4 The total number of trains on the destination board is given by

and the total number of trains to London is given by

flattening is to map a sequence of sequences to a single sequence; For example,

Page 9: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 9

A model for sequencesA sequence may be regarded as a function from the natural

numbers to a collection of objects.

If X is a set, then the set of all finite sequences of objects from X is defined by the following abbreviation:

is used to denote the set of all finite functions from N to X a finite function is a function with a finite set as a domain

If s is a sequence of at least n objects, then the expression `s n' denotes the nth object in s

Page 10: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 10

A model for sequencesExample 9.6 The second person in the queue at Counter b is Wilson:Queue_b 2 = wilson

•If s and t are sequences and i is a number in the range 1 … #s, Then:

The ith element of s % t is the ith element of s:

if j is a number in the range 1 …. #t, then the (j + #s)th element of s % t is the jth element of t :

Page 11: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 11

A model for sequences

Page 12: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 12

A model for sequencesThe function squash takes a finite function defined upon the

natural numbers and returns a sequence.

Page 13: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 13

A model for sequencesThe head operator definition is:

The tail operator is defined as:

Page 14: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 14

A model for sequencesThe set of all non-empty sequences over X

Another special set of sequences is the set of all injective sequences: (sequences in which no element appears more than once.)

Page 15: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 15

Functions on sequencesWe can introduce an operation f by describing its effect upon the

empty sequence, and also its effect upon a sequence starting with an arbitrary element

Example 9.7 The function `reverse' returns a sequence in which the elements appear in reverse order.

Page 16: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 16

Functions on sequencesFor the restriction operator

A common property of functions on sequences is distributivity. We say that a function f is distributive if

Example 9.9 The function add one is defined on sequences of numbers by the following pair of equations:

The effect of applying add one is to increase each number in the sequence by precisely one: for example,This function is distributive

Page 17: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 17

BagsA sequence stores information about the multiplicity and ordering

of its elements

If we wish to record multiplicities, but not ordering, then we may represent a collection of objects as a bag

write to denote the bag containing two copies of a, two copies of b, and two copies of cExample 9.12 Four friends Alice, Bill, Claire, and David are sitting in a café, waiting for their drinks to arrive. Alice and Claire have asked for espresso, Bill has asked for fruit tea, and David has asked for grape juice. Their requests can be represented by a bag:The order in which these drinks are to be delivered is unspecified

Page 18: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 18

BagsIf B is a bag of elements from set X , then B may be regarded as a

partial function from X to N

For example, the bag contains the same information as the function which associates each element with the number 2.

the set of all bags of elements from X may be defined by the following generic abbreviation:

If we wish to know how many instances of an object there are in a given bag, we can use the function count which is defined as:

Page 19: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 19

Bags is the number of occurrences of x in B:

The number of occurrences of a in the bag could be written as or

Example 9.13 If drinks denotes the collection of drinks requested by the four friends, thencount drinks espresso = 2count drinks decaffeinated coffee = 0

Page 20: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 20

BagsWe define bag membership and sub-bag relations, similar to the

set membership and subset relations introduced in Chapter 5:

An element x is a member of bag B if it appears in the domain of B, considered as a function.

A bag B is a sub-bag of another bag C of the same type if eachelement occurs no more often in B than it does in C.

Page 21: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 21

BagsExample 9.14 At least one grape juice has been requested,

and two of the group have asked for espresso,

If B and C are bags of the same type, then their union contains as many copies of each element as B and C put together:

If there are m copies of some element in bag B, and n copies of the same element in bag C, then the bag difference contains m − n copies, provided that m ≥ n.

If there are more copies in C than in B, then the count of this element is zero in the difference.

Page 22: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 22

Bags

Example 9.15 No sooner have the drinks been asked for than two more friends arrive Edward and Fleur, and take their places at the table. Edward asks for a cappuccino; Fleur asks for a mineral water. If we define then:

A few minutes later, a tray of drinks is brought over. It holds a mineral water, a grape juice, a decaffeinated coffee, and an espresso

Page 23: 18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.

18 April 2009 Instructor: Tasneem Darwish 23

BagsIf s is a sequence, then we may extract the multiplicity

information from s using the function items, which turns sequences into bags

Example 9.16 Another tray is brought over with the remaining drinks; these are placed on the table in front of our friends. The order in which the drinks are placed upon the table is recorded in the following sequence,

but only the count of each drink is important. The group at the table will be content, as items arrive = drinks