540 likes | 688 Vues
Sequences for system modelling. At the end of this lecture you should be able to:. provide a definition of a VDM sequence ; identify situations in which a sequence is an appropriate data type ; utilize and interpret sequence notation ; make appropriate use of the VDM sequence operators ;
E N D
At the end of this lecture you should be able to: • provide a definition of a VDM sequence; • identify situations in which a sequence is an appropriate data type; • utilize and interpret sequence notation; • make appropriate use of the VDM sequence operators; • define a sequence by comprehension; • write VDM specifications using the sequence type.
A sequence is an ordered collection of objects in which repetitions are significant. A queue of jobs waiting for a printer
A sequence is an ordered collection of objects in which repetitions are significant. A group of planes circling an airport
To declare a variable to be of type sequence we place an asterisk after the name of the type contained within the sequence. * seq: * convoy : SpaceCraft
Sequence Notation s = [ a, d, f, a, d, d, c ] queue = [ michael, varinder, elizabeth, winston, judith ] [ a, d, f ] [ a, f, d ] [ ]
Retrieving items from the sequence s = [ a, d, f, a, d, d, c ] queue = [ michael, varinder, elizabeth, winston, judith ] s(3) = f queue(4) = winston s(10) = undefined
Sequence operators s = [ a, d, f, a, d, d, c ] queue = [ michael, varinder, elizabeth, winston, judith ] len operator: Returns the length of a sequence lens = 7 len queue = 5
Sequence operators s = [ a, d, f, a, d, d, c ] queue = [ michael, varinder, elizabeth, winston, judith ] elems operator: Returns a set that contains all the members of the sequence elemss = { a, d, f, c } elemsqueue = {michael, varinder, elizabeth, winston, judith}
Sequence operators s = [ a, d, f, a, d, d, c ] queue = [ michael, varinder, elizabeth, winston, judith ] inds operator : Returns a set of all the indices of the sequence indss = {1, 2, 3, 4, 5, 6, 7 } inds queue = {1, 2, 3, 4, 5} inds []= { }
Sequence operators s = [ a, d, f, a, d, d, c ] queue = [ michael, varinder, elizabeth, winston, judith ] head (hd) operator : Returns the first element in the sequence hds = a hd queue = michael hd []= undefined
Sequence operators s = [ a, d, f, a, d, d, c ] queue = [ michael, varinder, elizabeth, winston, judith ] tail (tl) operator : Returns a sequence containing all but the first element tls = [d, f, a, d, d, c ] tl queue = [varinder, elizabeth, winston, judith ] tl[]= undefined tl [a]= [ ]
Sequence operators first = [ w, e, r, w ] second = [ t, w, q ] concatenation operator (^ ) operator: operates on two sequences, and returns a sequence that consists of the two sequences joined together first^second = [ w, e, r, w, t, w, q ] second^first = [t, w, q, w, e, r, w ] first^[ ] = [ w, e, r, w ]
Sequence operators the override operator (†) Takes a sequence and gives us a new sequence with a particular element of the old sequence overridden by a new element [a, c, d, e] † {1 z}= [z, c, d, e] [a, c, d, e] † {2 x, 4 y}= [a, x, d, y] [a, c, d, e] † {7 g}= undefined
Sequence operators subsequence operator allow us to extract a part of a sequence between two indices s = [ a, d, f, a, d, d, c ] s(2, ... , 5) = [d, f, a, d] s(2, ... , 2) = [d] s(2, ... , 13) = undefined s(1, ... ,0) = [ ] s(8, ... , 7) = [ ]
Sequence comprehension [ expression(a) | aSomeSet test (a) ]
Sequence comprehension [ expression(a) | aSomeSettest (a) ]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] [ a | a{1,…,10}is-odd(a)]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] [ a | a{1,…,10}is-odd(a)] [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] [ a | a {1,…,10} is-odd(a)] [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] [ a | a {1,…,10} is-odd(a)] [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] Let’s filter this sequence so that we only have values greater than 10
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] [39, 11, 45, 39]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] s2 = [ s1(i) | iindss1s1(i) > 10 ]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] s2 = [ | ielemss1] [ 2, 3, 4, 6, 7, 8, 9, 11, 39, 45]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] s2 = [ | iindss1] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] s2 = [ | i inds s1s1(i) > 10 ] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] s2 = [ s1(i) | i inds s1s1(i)> 10 ] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] s2 = [ s1(i) | i inds s1s1(i)> 10 ] [1, 2, 3, 4, 5, 39, 7, 8, 9, 10, 11, 12, 14]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] s2 = [ s1(i) | i inds s1s1(i)> 10 ] [1, 2, 3, 4, 5, 39, 7, 8, 9, 11, 11, 12, 14]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] s2 = [ s1(i) | i inds s1s1(i)> 10 ] [1, 2, 3, 4, 5, 39, 7, 8, 9, 11, 45, 12, 14]
Sequence comprehension [ expression(a) | aSomeSettest (a) ] s1 = [2, 3, 4, 7, 9, 39, 6, 7, 8, 11, 45, 39, 3] s2 = [ s1(i) | i inds s1s1(i)> 10 ] [1, 2, 3, 4, 5, 39, 7, 8, 9, 11, 45, 39, 14]
Stack stack : Element [*] push(Element) pop() : Element isEmpty(): Boolean
Stack stack : Element [*] push(Element) pop() : Element isEmpty(): Boolean
types Element = TOKEN state Stack of stack : init mk-Stack(s) end Element * s = [ ]
Stack stack : Element [*] push(Element) pop() : Element isEmpty(): Boolean
stack = [itemIn] ^ stack push( ) ext pre post itemIn : Element wr stack : Element* TRUE
Stack stack : Element [*] push(Element) pop() : Element isEmpty(): Boolean
stack = tlstack itemRemoved = hdstack pop( ) ext pre post itemRemoved : Element wr stack : Element* stack [ ]
Stack stack : Element [*] push(Element) pop() : Element isEmpty(): Boolean
isEmpty( ) ext pre post query: rd stack : Element* TRUE query stack = [ ]
Airport2 permission: Aircraft [*] landed: Aircraft [*] circling: Aircraft [*] givePermission(Aircraft) recordLanding( ) recordTakeOff(Aircraft) getPermission( ): Aircraft [*] getLanded( ): Aircraft [*] numberWaiting(): Integer getCircling( ): Aircraft [*] allowToCircle (Aircraft)
types state Airport2 of init mk-Airport2 ( ) end Aircraft = TOKEN permission: Aircraft-set landed: Aircraft-set circling: Aircraft* inv mk-Airport2(p,l,c) ? p, l, c p = { } l = { } c = [ ]
The new invariant • Landed planes must have permission • Circling planes must have permission • Circling planes can not be landed • All circling planes are unique inv mk-Airport2(p,l,c) l p elems c p elemscl = { } isUnique(c) isUnique(seqIn : Aircraft*) query : pre true postquerylen seqIn= card elems seqIn
The new invariant • Landed planes must have permission • Circling planes must have permission • Circling planes can not be landed • All circling planes are unique inv mk-Airport2(p,l,c) l p elems c p elemscl = { } isUnique(c) isUnique(seqIn : Aircraft*) query : pre true postquery i1 ,i2indsseqIn i1 i2 seqIn(i1) seqIn(i2)
1 2 3 4 5 i1 ,i2indsseqIn i1 i2 seqIn(i1) seqIn(i2)
Airport2 permission: Aircraft [*] landed: Aircraft [*] circling: Aircraft [*] givePermission(Aircraft) recordLanding( ) recordTakeOff(Aircraft) getPermission( ): Aircraft [*] getLanded( ): Aircraft [*] numberWaiting(): Integer getCircling( ): Aircraft [*] allowToCircle (Aircraft)