Pré-visualização parcial do texto
Baixe B - Method e outras Manuais, Projetos, Pesquisas em PDF para Informática, somente na Docsity!
cornerstones of computing series editors: richard bird & tony hoare the b-method an introduction steve schneider te rst | 2928949920 dm, 3.6 skip 3.7 Conditional 3.8 Case statement 3.9 BEGIN and END 3.10 Exercises Towards machine consistency 41 Consistency of INVARIANT 4.2 Proof obligation for initialisation 4.3 Proof obligation for operations 4.4 Exercises Parameters, sets, and constants 5.1 MACHINE parameters 5.2 CONSTRAINTS 5.3 SETS 5.4 CONSTANTS 5.5 PROPERTIES 5.6 Example: club behaviour 5.7 Full machine consistency 5.8 Summary 5.9 Exercises Relations 61 Relations between sets 6.2 Domain restriction 6.3 Range restriction 6.4 Relational image 6.5 Relational inverse 6.6 Relational composition 6.7 Relations on a single set 6.8 Relational over-riding 6.9 Example: heirs to the throne Contents 37 37 39 41 41 45 45 46 47 53 55 56 57 58 58 59 60 62 67 67 69 69 72 72 74 75 75 76 79 81 e Contents v 6.10 Relations in machines 85 6.11 Example: access 86 6.12 Exercises 88 7 Functions and sequences 91 74 Partial functions 91 7.2 Total functions 92 73 Injective functions 93 7.4 Surjective functions 94 7.5 Bijective functions 94 7.6 Lambda notation for functions 96 7.7 | Example: tracking reading books 97 7.8 'Sequences 98 7.9 Exercises 105 8 Arrays 107 81 Arrays as functions 107 8.2 Weakest preconditions for arrays 109 8.3 Exercises nz 9 Nondeterminism n9 9.1 Nondeterminism in specifications 119 9.2 The ANY statement 120 9.3 Nondeterministic assignment 126 9.4 The CHOICE statement 127 9.5 The SELECT statement 130 9.6 The PRE statement 133 9.7 Example: jukebox 135 9.8 Exercises 137 10 Structuring with INCLUDES 139 10.1 Inclusion 140 10.2 Included operations 143 Contents 15 16 14.4 14.5 14.6 Loops 15.1 15.2 15.3 15.4 15.5 15.6 Operations with outputs Proof obligations Exercises Loop execution Loop invariant Guaranteeing termination Loop semantics Loop development Exercises Implementation machines 16.1 16.2 16.3 16.4 16.5 16.6 IMPLEMENTATION Example: a robust queue Data refinement revisited Loops in implementations Implementing sets and constants Exercises 17 Case study: heapsort 18 1721 17.2 17.3 17.4 Priority queues and heaps The loops Sorting Exercises Library machines 181 18.2 18.3 18.4 18.5 18.6 Completing developments A library machine: Boo! TYPE Other TYPE machines A library machine: Nvar A library machine: Varr Other simple state-maintaining machines vii 227 228 229 231 231 233 236 238 242 250 255 255 264 268 272 278 283 285 285 290 293 297 299 299 301 303 304 308 312 vili Contents 18.7 Collections 314 18.8 Example: supermarket checkouts 320 18.9 Other library machines 325 18.10 BASE machines 326 18.11 Exercises 336 Answers to self tests 339 A Generalised Substitution Language 357 B Machine readable AMN 361 Index 365 Index of machines 369 2 1 Introducing abstract machines MACHINE ... VARIABLES ... INITIALISATION ... OPERATIONS ... END Figure 1.1: Example headings within an abstract machine Figure 1.2: An abstract machine combination of these. The collection of operations are the interface by which the machine interacts with its environment. These are all listed, along with a precise description of what they do, under the heading OPERATIONS. A machine might also be required to maintain or process information. Mm this case it would be required to keep some local state. The state variables are listed under the VARIABLES heading. Their types, and any other information which concerns them, are listed under the INVARIANT heading. This contains all the information which must always be true of the state, whichever sequence of operations is selected. For example, the type of a variable should not change during the course of an execution, so it is given as part of the invariant. The initial state should also be specified. This part of the specification appears under the INITIALISATION heading. Finally, a machine must have a name in order to allow other parts of a large specification to refer to it. The name is given under the heading of MACHINE, which declares that the object being described is an abstract machine (rather than a refinement or an implementation, which will be covered in later chap- ters). A machine is not unlike a description of an object in the object-oriented sense. It has a name, some internal state, and a set of operations, as do objects. It may be thought of as a black box with a set of buttons on the side (which can take inputs and provide outputs) corresponding to the operations, and a set of state variables inside. Interaction with the component must be through these escoa aqpesurcacã epa o i 1.2 MACHINE 3 Now Serving 2/1 26) Figure 1.3: A ticketing system operations, which comprise its interface. This view is illustrated in Figure 1.2. The stateis maintained within the machine, and the buttons around the outside of the component represent the operations—the interface through which auser may interact with the machine. To introduce the structure of a machine description in more detail, we willuse an example of a ticket dispenser which is used in a shop to order the queue. On entry to the shop, a customer takes a numbered ticket from the dispenser. When a sales assistant is ready to serve, a display indicates the number of the customer who is to be served next. Customers wait until it is their turn to be served. The system is pictured in Figure 1.3. The behaviour of this small system can be specified as an abstract machine. This will give details of the operations offered by the system, the internal state reguired to support those operations, and the additional information about that state. 1.2 MACHINE The MACHINE clause is used to provide the name of the machine. All machines in a development must have differént names. In the case of the ticket dis- penser, we will call the machine Ticket. This part of the description is written as follows: MACHINE Ticket 1.5 OPERATIONS 5 given out next. If the machine is able to reach a state in which serve > next then something has gone wrong. The requirement that serve « next must always be true simply reflects our understanding of the role that these two variables are playing in the specification, and so it should appear in the invariant. One major advantage of B is that it allows predicates such as serve < next to be written in a machine processable way, something that ordinary type systems cannot do. The INVARIANT clause is an assertion, or a claim about the reachable states of the machine. The INVARIANT clause characterises the sensible states that are permitted for the machine, The machine should never arrive at a state in which some part of the INVARIANT clause is false. The complete invariant for the Ticket machine is therefore the conjunction of the type information and the other constraint: [ INVARIANT serve e NA nexteNA serve < next | 1.5 OPERATIONS The OPERATIONS clause of the machine description contains a list of operation definitions. mn software engineering, specifications of operations generally provide the fol- lowing information: * The name of the operation; * Input parameters; * Output parameters; * What the operation requires (restrictions on parameters and the states from which the operation may be called); * What the operation modifies (variables that may be modified); * The effects or behaviour of the operation (what the operation does). AB description of an operation provides all of this information, in a form which is suited to structuring specifications. The name, and input and output parameters of an operation are given by an operation header: outputs — name(inputs) where name is the name of the operation, outputs is a list of output variables, and inputs is a list of input variables. The variables are all formal parameters, so they must all be different. 6 1 Introducing abstract machines The name of the operation must be given, but both the input list and the output list are optional, since some operations do not take inputs, and some do not provide outputs. The Ticket machine will offer two operations, one to serve the next customer (and update the indicator board), and one to provide a customer with the next ticket. These are declared as follows”: ss — serve next tt — take. ticket Neither of them takes any input, and they both provide a single output: ss in the case of serve next, and tt in the case of take ticket. The operation specification itself consists of a precondition part, and a body. The precondition must contain type information for all of the input variables, as well as any further assumptions on the inputs or the state of the machine. These together describe what the operation requires in order to be sure to behave correctly. The precondition therefore corresponds to a requirement on the user to ensure that the requirements are met whenever the operation is called. For example, the serve next operation is likely to be called when the sales assistant has finished serving a customer. Mn this case, serve (the ticket number of the person who has just finished being served) must be strictly less than next, the number on the next ticket to be dispensed. If the shop is empty, then the value of serve will be the same as next. Im this situation, the serve. next operation should not be called before a ticket is taken. Thus the precondition for this operation will be serve < next. The body of an operation describes what the operation achieves. It must assign some value to each of the output variables, and it can also update the machine state, The effect of an operation is therefore specified by giving the relation- ship berween the state before it is called together with the input values, and the state after the operation has completed together with the output values. This is accomplished in B by giving an abstract assignment statement which determines how the state is to be updated and what the output values should be in terms of the initial state and input values. m the case of serve. next, the state variable serve should be incremented, and the output (to the display board) should also be incremented to mirror the value of the state serve. In AMN, an assignment is written as x:= E (pronounced 'x becomes E") where x is the variable to be assigned, and E is the expression whose value is to be assigned to x. The variable serve is thus incremented by the assignment serve := serve + 1. 2The B-Toolkit requires any variable name to be at least two characters long. Thus double- letter variable names such as ss and tt often appear in B descriptions 8 1 Introducing abstract machines be called), then the operation might no longer be consistent with its invariant. Consider the situation where the operation can always be called, whatever state the machine is in. This corresponds to a precondition of true, with a resulting operation ss — serve next E PRE true THEN ss, serve := serve + 1, serve+ 1 END In this case, the operation can be invoked even when serve = next, and will reach a state where the invariant is no longer true. This weaker version of the operation is no longer consistent with the invariant, since it allows the user to fulfil his or her own obligations but does not meet its own. The original version of serve next can violate the invariant only if the user fails to meet the requirements on executing the operation. Strengthening the precondition of serve next' to serve < next imposes more constraints on the user, and in this case results in the operation becoming consistent. There is a natural tension between usability (which needs the pre- conditions to be as weak as possible) and correctness (which needs the pre- conditions to be restrictive enough to prevent operations being called when they should not be). In the extreme case, an operation can have a precondi- tion which can never be fulfilled. m this case, it is correct in that it can never bring the machine to an invalid state, but at the expense of being completely unusable. The operation take ticket can never result in an incorrect state, since it incre- ments next (outputting the current value) and leaves serve unchanged. The complete operation is specified as follows: > tt — take ticket PRE true THEN tt, next := next, next + 1 END It has the weakest possible precondition—true—indicating that there are no constraints on the user regarding its invocation. When a precondition is true, then it may be dropped from the specification of its operation. A shorter way of specifying take. ticket is as follows: tt — take ticket 5 tt, next := next, next + 1 If no precondition is given, then it is assumed to be true. If the operation is consistent with this precondition, then it must stillbe correct with any stronger one, 1.6 INITIALISATION 9 Self Test 1.2 Suppose that the invariant has a further clause next < serve+ 20 indicating that there must never be more than 20 outstanding tickets. Why is the operation take ticket not consistent with this invariant? How should the precondition of take ticket be strengthened to preserve its consistency with the invariant? D Self Test 1.3 Is the operation tt — replace ticket = next := next — 1 consistent with the invariant of the Ticket machine? õ 1.6 INITIALISATION The INITIALISATION clause is used to describe the possible initial state of the machine. It consists of an AMN statement which is used to set the state in which the machine starts. All variables listed in the VARIABLES clause must be assigned some value. The Ticket machine should start with O on the display board, and O as the number of the first ticket to be taken. Hence both variables should be set to O, so a multiple assignment will be used to set them both simultaneously. The INITIALISATION clause is thus written as follows: INITIALISATION serve, next := 0,0 For the INITIALISATION clause to be a consistent part of the specification, pos- sible initial states must be correct with respect to the invariant. This means that the program in the INITIALISATION clause must be guaranteed to establish the invariant. The complete specification of the Ticket machine is given in Figure 1.4. 1.7 Exercises Exercise 1.1 Add a reset operation to the Ticket machine, which returns it to its initial state. o Exercise 1.2 Amend the description of the Ticket machine so that only tickets numbered up to 500 can be dispensed. (This will involve a change to the in- variant of the machine, and to the operation take. ticket.) [a] Review of set theory and logic Specifications in the form of abstract machines specify the information that is maintained by the machine, consistency requirements on the information, and the ways in which it can be updated, manipulated, and accessed. The B-Method makes use of the language of set theory and logic within AMN in order to express these requirements in a formal and precise way. This chapter provides a brief review of set theoretic and logical notation, motivated by the way in which it is used by the B-Method. 2.1 Settheory A set is a collection of entities of some sort. For example, the set N is the collection of all of the natural numbers, and no other numbers (or anything else) are in the set N. The items that are in a set are said to be its elements, and a set is completely defined by its elements. In other words, if two sets have exactly the same elements, then they are actually the same set. And if there is some entity which appears in one set but not the other, then the sets are not the same, The notation for set membership is e e $, pronounced 'e is a member of S' for a set S and element e. Conversely, e É S states that “e is not in the set S”. If a set has only a finite number of elements, then it can be written explicitly, by listing all of its elements within set brackets '[' and '". For example, the set SMALLSQUARE consisting of all square numbers less than 20 can be written as SMALLSQUARE = 10,1,4,9,16) The convention within the B-Method is for names of given sets to be in upper case. Since the set is defined entirely by the set of elements that are in it, the order in which the elements are listed in an explicitly given set is irrelevant. The same 12 2 Review of set theory and logic set would also have been defined by SMALLSQUARE = (4,16,0,1,9) The empty set contains no elements at all, and is therefore the smallest possible set. Itis written as f), a special case of an explicitly given set. Sets can also be defined by means of a set comprehension. Instead of listing the elements of the set explicitly (which in any case is impossible for sets of infinite size), a set can be defined as the collection of all elements of some type which meet a particular property. mm the case of SMALLSQUARE, its elements are precisely those numbers which are less than 20 and which are a perfect square of some number. In general, a set comprehension will be of the form [x |x e T A PJ, wherexisa variable ranging over elements of the set T, and P is a condition which is true or false for individual members of T. The set defined by this set comprehension is the set of elements of T for which the condition P is true. For example, SMALLSQUARE could be defined by a set comprehension as fol- lows: SMALISQUARE = fx|xeNandxisa perfect square and x < 20; A segment of the natural numbers can be expressed using the notation m..n, which defines the set of numbers between m and n inclusive. This is a short- hand for a particular set comprehension: mn = IxitxeNamsxax