18 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal Methods Sequences
18 April 2009Instructor: Tasneem Darwish2 Outlines Sequence notation A model for sequences Functions on sequences Bags
18 April 2009Instructor: Tasneem Darwish3 Sequence notation A 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 maintained Example: If s and t are sequences, we write s ̑ t to denote the concatenation of s and t
18 April 2009Instructor: Tasneem Darwish4 Sequence notation Example 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
18 April 2009Instructor: Tasneem Darwish5 Sequence notation Example 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;
18 April 2009Instructor: Tasneem Darwish6 Sequence notation To 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
18 April 2009Instructor: Tasneem Darwish7 Sequence notation Example 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
18 April 2009Instructor: Tasneem Darwish8 Sequence notation if 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,
18 April 2009Instructor: Tasneem Darwish9 A model for sequences A 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 n th object in s
18 April 2009Instructor: Tasneem Darwish10 A model for sequences Example 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 i th element of s ̑ t is the i th element of s: if j is a number in the range 1 …. #t, then the (j + #s) th element of s ̑ t is the j th element of t :
18 April 2009Instructor: Tasneem Darwish11 A model for sequences
18 April 2009Instructor: Tasneem Darwish12 A model for sequences The function squash takes a finite function defined upon the natural numbers and returns a sequence.
18 April 2009Instructor: Tasneem Darwish13 A model for sequences The head operator definition is: The tail operator is defined as:
18 April 2009Instructor: Tasneem Darwish14 A model for sequences The 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.)
18 April 2009Instructor: Tasneem Darwish15 Functions on sequences We 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.
18 April 2009Instructor: Tasneem Darwish16 Functions on sequences For 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
18 April 2009Instructor: Tasneem Darwish17 Bags A 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 c Example 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
18 April 2009Instructor: Tasneem Darwish18 Bags If 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:
18 April 2009Instructor: Tasneem Darwish19 Bags is the number of occurrences of x in B: The number of occurrences of a in the bag could be written asor Example 9.13 If drinks denotes the collection of drinks requested by the four friends, then count drinks espresso = 2 count drinks decaffeinated coffee = 0
18 April 2009Instructor: Tasneem Darwish20 Bags We 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 each element occurs no more often in B than it does in C.
18 April 2009Instructor: Tasneem Darwish21 Bags Example 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.
18 April 2009Instructor: Tasneem Darwish22 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 definethen: 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
18 April 2009Instructor: Tasneem Darwish23 Bags If 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