Download Sequences-Formal Specification Methods in Software Development-Lecture Slides and more Slides Software Development Methodologies in PDF only on Docsity!
Chapter No. 9
Sequences
Introduction
- It is sometimes necessary to record the order in which objects are arranged, e.g., - Data may be indexed by an ordered collection of keys - Messages may be stored in order of arrival - Tasks may be performed in order of importance.
- Now, we introduce a notion of a sequence which is an ordered collection of objects
Introduction
- Sequence: A sequence is an ordered collection of objects.
- Empty Sequence : If there are no objects in the collection, the sequence is the empty sequence, and is written .
- Otherwise, the sequence is written as a list of objects between angle brackets, e.g. a, b, c denotes the sequence containing objects a, b, and c, in that order.
Sequence Notation
- A useful way of composing sequences is concatenation
- In concatenation two sequences are combined in such a way that the elements of one follow elements of the other, order is maintained.
- If s and t are sequences, we write s t to denote the concatenation of s and t, e.g., a, b, c d, e = a, b, c, d, e
Example: Destination Board at Railway Station
Time From To 10:15 Oxford London Paddington 10:38 London Paddington Edinburgh 10:40 Great Malvern London Paddington 11:15 Manchester Poole 11:20 Oxford Reading 11:40 London Paddington Manchester
Modeling using Sequence
Model of Above Destination Board
The destination board displays a list of trains, arranged in order of departure. Model it as a sequence of pairs, each recording a time and a destination
trains = (10 :15, london), (10:38, edinburgh), (10:40, london), (11:15, birmingham), (11:20, reading), (11:40, london)
Head and Tail Operators
head a, b, c, d, e = a tail a, b, c, d, e = b, c, d, e
it is to be noted that
s = head s tail s
Neither of these operators is defined upon the empty sequence.
Example 9.3: Head and Tail Operators
Sally wants to take the first train to London. From the list of trains on the destination board, she knows that this is the 10:15, head (trains { t : Time (t, london)}) = (10 :15, london) If we assume that she will not reach in time, then the list of available trains is given by tail trains = (10:38, edinburgh), (10:40, london), (11:15, birmingham), (11:20, reading), (11:40, london)
A Model of Sequences
- If X is a set, then the set of all finite sequences of objects from X is defined by the following abbreviation: seq X == {s : X | n : dom s = 1..n}
- This definition makes explicit an assumption about sequences, that every element of a given sequence must share the same type.
- The expression 1, (1, 2) does not make any sense to us
Formalizing Concatenation Operator
- If a function takes more than one argument, then it may be defined as an infix symbol,
- We use underscores to indicate its position
[ X ] _ _ : seq X seq X seq X
s, t : seq X
( s t) = # s + # t
i :1.. # s (s t) i = s i j : 1.. # t (s t ) (# s + j )= t j
Formalizing Tail Operator
On the other hand, tail operator requires a translation. Tail operator is also partial
[ X ]
_ tail _ : seq X seq X
s : seq X | s
tail s = # s -
i: 1.. #s -1 (tail s) i = s (i+ 1)
Non-empty and Injective Sequences
- we give a name to the set of all non-empty sequences over X as well, seq1 X == {s : seq X | s }
- “head” and „tail‟ are total when defined upon this
- Another special set of sequences is set of all injective sequences i.e. sequences in which no element appears more than once. We write iseq X to denote set of all injective sequences over set X, iseq X == {s : seq X | s X}
- Such sequences are used to represent ordered collections of distinct objects.
Bags
- A sequence stores information about the multiplicity and ordering of its elements.
- For example, in sequence a, b, c, a, b, c we can see that there are exactly two occurrences of a, and that these occupy the first and fourth positions in the sequence.
- Sometimes this is more information than we need, and hence we use bag
Bags
- Suppose that only the number of occurrences of a is important.
- If this is the case, then the sequence above contains more detail than is necessary
- It is not a fully abstract representation.
- The set {a, b, c}, on the other hand, is not an adequate representation
- It records that a is present, but does not record how many times it occurs.