School of Computer Science & Engineering UNSW - - PowerPoint PPT Presentation

school of computer science engineering unsw
SMART_READER_LITE
LIVE PREVIEW

School of Computer Science & Engineering UNSW - - PowerPoint PPT Presentation

School of Computer Science & Engineering UNSW http://www.cse.unsw.edu.au/ An Introduction to the B Method Preconditions and Guards Revised 27th March 2001 Ken Robinson mailto::k.robinson@unsw.edu.au k.robinson@unsw.edu.au Objectives


slide-1
SLIDE 1

School of Computer Science & Engineering — UNSW

http://www.cse.unsw.edu.au/

An Introduction to the B Method Preconditions and Guards

Revised 27th March 2001

Ken Robinson

mailto::k.robinson@unsw.edu.auk.robinson@unsw.edu.au

slide-2
SLIDE 2

Objectives of this lecture

The two concepts of preconditions and guards are frequently confused, but in this lecture we will attempt to show that they are very different.

slide-3
SLIDE 3

Objectives of this lecture

The two concepts of preconditions and guards are frequently confused, but in this lecture we will attempt to show that they are very different. In part the confusion arises because, in general, only the concept of guard is implemented in commonly used programming languages. Curiously, in a strategy known as defensive programming guards are used where preconditions are needed, and this is most inappropriate.

slide-4
SLIDE 4

Objectives of this lecture

The two concepts of preconditions and guards are frequently confused, but in this lecture we will attempt to show that they are very different. In part the confusion arises because, in general, only the concept of guard is implemented in commonly used programming languages. Curiously, in a strategy known as defensive programming guards are used where preconditions are needed, and this is most inappropriate. In this lecture we will explore and contrast the concepts of preconditions and guards, in order to understand their intended purpose, and the implications of their use.

slide-5
SLIDE 5

Preconditions and Guards

In the Simple Library case study we have instances of preconditions and guards.

slide-6
SLIDE 6

Preconditions and Guards

In the Simple Library case study we have instances of preconditions and guards. Preconditions appeared in preconditioned substitutions. Guards appeared in if-then-else constructs.

slide-7
SLIDE 7

Preconditions and Guards

In the Simple Library case study we have instances of preconditions and guards. Preconditions appeared in preconditioned substitutions. Guards appeared in if-then-else constructs. Preconditions and guards appear to be similar, but in fact they diametrically

  • pposed concepts.
slide-8
SLIDE 8

Experiment 1

Animate the CoffeeClub0 machine developed in the B Method Overview lectures.

slide-9
SLIDE 9

Experiment 1

Animate the CoffeeClub0 machine developed in the B Method Overview lectures. Select the FeedBank operation a few times, and observe what happens to the state.

slide-10
SLIDE 10

Experiment 1

Animate the CoffeeClub0 machine developed in the B Method Overview lectures. Select the FeedBank operation a few times, and observe what happens to the state. Select the RobBank operation a few times.

slide-11
SLIDE 11

Experiment 1

Animate the CoffeeClub0 machine developed in the B Method Overview lectures. Select the FeedBank operation a few times, and observe what happens to the state. Select the RobBank operation a few times. What happens when RobBank is selected when piggybank is 0?

slide-12
SLIDE 12

Experiment 1

Animate the CoffeeClub0 machine developed in the B Method Overview lectures. Select the FeedBank operation a few times, and observe what happens to the state. Select the RobBank operation a few times. What happens when RobBank is selected when piggybank is 0? What happens for the following sequence: select RobBank when piggybank is 0; then select FeedBank?

slide-13
SLIDE 13

Experiment 1

Animate the CoffeeClub0 machine developed in the B Method Overview lectures. Select the FeedBank operation a few times, and observe what happens to the state. Select the RobBank operation a few times. What happens when RobBank is selected when piggybank is 0? What happens for the following sequence: select RobBank when piggybank is 0; then select FeedBank? What conclusion do you draw from this?

slide-14
SLIDE 14

Experiment 2

Modify the FeedBank operation by adding a precondition 42 ≤ piggybank, and remake the machine.

slide-15
SLIDE 15

Experiment 2

Modify the FeedBank operation by adding a precondition 42 ≤ piggybank, and remake the machine. Run the animator, and select FeedBank with piggybank equal to 0.

slide-16
SLIDE 16

Experiment 2

Modify the FeedBank operation by adding a precondition 42 ≤ piggybank, and remake the machine. Run the animator, and select FeedBank with piggybank equal to 0. What happens?

slide-17
SLIDE 17

Experiment 2

Modify the FeedBank operation by adding a precondition 42 ≤ piggybank, and remake the machine. Run the animator, and select FeedBank with piggybank equal to 0. What happens? What conclusions do you draw from this?

slide-18
SLIDE 18

What are precondition?

Preconditions are assumptions.

slide-19
SLIDE 19

What are precondition?

Preconditions are assumptions. They are not conditions that are going to be checked.

slide-20
SLIDE 20

What are precondition?

Preconditions are assumptions. They are not conditions that are going to be checked. They do not prevent access to the substitutions to which they are attached.

slide-21
SLIDE 21

What are precondition?

Preconditions are assumptions. They are not conditions that are going to be checked. They do not prevent access to the substitutions to which they are attached. On the contrary, a preconditioned substitution assumes the precondition is true. If it is not then the consequence is not defined.

slide-22
SLIDE 22

What are precondition?

Preconditions are assumptions. They are not conditions that are going to be checked. They do not prevent access to the substitutions to which they are attached. On the contrary, a preconditioned substitution assumes the precondition is true. If it is not then the consequence is not defined. Preconditions are not necessarily correct or incorrect; they may be appropriate or inappropriate.

slide-23
SLIDE 23

Experiment 3

Remove the preconditions from the FeedBank and RobBank operations.

slide-24
SLIDE 24

Experiment 3

Remove the preconditions from the FeedBank and RobBank operations. Remake the machine.

slide-25
SLIDE 25

Experiment 3

Remove the preconditions from the FeedBank and RobBank operations. Remake the machine. Enter the Provers environment and run the AutoProver.

slide-26
SLIDE 26

Experiment 3

Remove the preconditions from the FeedBank and RobBank operations. Remake the machine. Enter the Provers environment and run the AutoProver. Look at the undischarged proof obligations, using the BToolProver.

slide-27
SLIDE 27

Experiment 3

Remove the preconditions from the FeedBank and RobBank operations. Remake the machine. Enter the Provers environment and run the AutoProver. Look at the undischarged proof obligations, using the BToolProver. What conclusions do you draw?

slide-28
SLIDE 28

Experiment 4

Modify the RobBank operation to have a guard.

slide-29
SLIDE 29

Experiment 4

Modify the RobBank operation to have a guard.

RobBank = IF amount <= piggybank THEN piggybank := piggybank - amount END

Remake.

slide-30
SLIDE 30

Experiment 4

Modify the RobBank operation to have a guard.

RobBank = IF amount <= piggybank THEN piggybank := piggybank - amount END

Remake. Examine any undischarged proof obligations.

slide-31
SLIDE 31

Experiment 4

Modify the RobBank operation to have a guard.

RobBank = IF amount <= piggybank THEN piggybank := piggybank - amount END

Remake. Examine any undischarged proof obligations. What conclusions do you draw?

slide-32
SLIDE 32

Conclusions from experiments

We could conclude from the preceding experiments that it doesn’t matter for the RobBank operation whether we use a guard or a precondition. Both lead to proof

  • bligations that are discharged.
slide-33
SLIDE 33

Conclusions from experiments

We could conclude from the preceding experiments that it doesn’t matter for the RobBank operation whether we use a guard or a precondition. Both lead to proof

  • bligations that are discharged.

But that might lead us into a false sense of security.

slide-34
SLIDE 34

Conclusions from experiments

We could conclude from the preceding experiments that it doesn’t matter for the RobBank operation whether we use a guard or a precondition. Both lead to proof

  • bligations that are discharged.

But that might lead us into a false sense of security. For example, we might want to explore when RobBank(1) followed by FeedBank(1) is equivalent to doing nothing.

slide-35
SLIDE 35

Conclusions from experiments

We could conclude from the preceding experiments that it doesn’t matter for the RobBank operation whether we use a guard or a precondition. Both lead to proof

  • bligations that are discharged.

But that might lead us into a false sense of security. For example, we might want to explore when RobBank(1) followed by FeedBank(1) is equivalent to doing nothing. This is true for all values of the initial value of piggybank, except 0.

slide-36
SLIDE 36

Conclusions from experiments

We could conclude from the preceding experiments that it doesn’t matter for the RobBank operation whether we use a guard or a precondition. Both lead to proof

  • bligations that are discharged.

But that might lead us into a false sense of security. For example, we might want to explore when RobBank(1) followed by FeedBank(1) is equivalent to doing nothing. This is true for all values of the initial value of piggybank, except 0. The exception is due to the behaviour of RobBank(amount), and hence this

  • peration should have a precondition amount ≤ piggybank.
slide-37
SLIDE 37

Conclusions from experiments

We could conclude from the preceding experiments that it doesn’t matter for the RobBank operation whether we use a guard or a precondition. Both lead to proof

  • bligations that are discharged.

But that might lead us into a false sense of security. For example, we might want to explore when RobBank(1) followed by FeedBank(1) is equivalent to doing nothing. This is true for all values of the initial value of piggybank, except 0. The exception is due to the behaviour of RobBank(amount), and hence this

  • peration should have a precondition amount ≤ piggybank.

It is pointless having a precondition and a guard with the same condition.

slide-38
SLIDE 38

Preconditions in real life

Consider the following sign

slide-39
SLIDE 39

Preconditions in real life

Consider the following sign M15 Tollway Toll for cars: $2.50 Toll payable at toll booths preceding first exit

slide-40
SLIDE 40

Preconditions in real life

Consider the following sign M15 Tollway Toll for cars: $2.50 Toll payable at toll booths preceding first exit If you enter the tollway you have been warned that you will need to have $2.50 to pay the toll.

slide-41
SLIDE 41

Preconditions in real life

Consider the following sign M15 Tollway Toll for cars: $2.50 Toll payable at toll booths preceding first exit If you enter the tollway you have been warned that you will need to have $2.50 to pay the toll. Being able to pay the toll is a precondition to entry.

slide-42
SLIDE 42

Preconditions in real life

Consider the following sign M15 Tollway Toll for cars: $2.50 Toll payable at toll booths preceding first exit If you enter the tollway you have been warned that you will need to have $2.50 to pay the toll. Being able to pay the toll is a precondition to entry. What is the consequence of entering the tollway and not having the means to pay the toll?

slide-43
SLIDE 43

Another example

Near the entrance to a supermarket there may be a sign

slide-44
SLIDE 44

Another example

Near the entrance to a supermarket there may be a sign Condition of entry to this store All bags may be searched before exit from this store

slide-45
SLIDE 45

Another example

Near the entrance to a supermarket there may be a sign Condition of entry to this store All bags may be searched before exit from this store On entry to the store you have to be prepared to have any bags searched.

slide-46
SLIDE 46

Another example

Near the entrance to a supermarket there may be a sign Condition of entry to this store All bags may be searched before exit from this store On entry to the store you have to be prepared to have any bags searched. This is a precondition.

slide-47
SLIDE 47

Another example

Near the entrance to a supermarket there may be a sign Condition of entry to this store All bags may be searched before exit from this store On entry to the store you have to be prepared to have any bags searched. This is a precondition. What happens if you enter the store and are not prepared to have your bag searched?

slide-48
SLIDE 48

Guards in real life

On entrance to a cinema there may be the following sign

slide-49
SLIDE 49

Guards in real life

On entrance to a cinema there may be the following sign Cinema admission prices Adults $13 Children $7 Please pay at box office.

slide-50
SLIDE 50

Guards in real life

On entrance to a cinema there may be the following sign Cinema admission prices Adults $13 Children $7 Please pay at box office. You have to buy a ticket before entering the particular theatre.

slide-51
SLIDE 51

Guards in real life

On entrance to a cinema there may be the following sign Cinema admission prices Adults $13 Children $7 Please pay at box office. You have to buy a ticket before entering the particular theatre. There will be a person (guard) at each theatre to collect your ticket.

slide-52
SLIDE 52

Guards in real life

On entrance to a cinema there may be the following sign Cinema admission prices Adults $13 Children $7 Please pay at box office. You have to buy a ticket before entering the particular theatre. There will be a person (guard) at each theatre to collect your ticket. This is a guard and you cannot enter the theatre without purchasing a ticket.

slide-53
SLIDE 53

A Club

Club B Patrons of this club must be 18 years of age or older.

slide-54
SLIDE 54

A Club

Club B Patrons of this club must be 18 years of age or older. If there is a guard (bouncer) at the door, then entrance is guarded; otherwise it is preconditioned entrance.

slide-55
SLIDE 55

Conclusions on guards and preconditions

In general, preconditions are assumptions that are necessary for the successful completion of behaviour. They are the part of the contract, between the user of an operation and the implementor of an operation, that must be met by the user

  • f the operation.
slide-56
SLIDE 56

Conclusions on guards and preconditions

In general, preconditions are assumptions that are necessary for the successful completion of behaviour. They are the part of the contract, between the user of an operation and the implementor of an operation, that must be met by the user

  • f the operation.

Clearly, there is a weakest precondition —necessary and sufficient— but it might be difficult in some cases to determine that weakest precondition. In such cases a sufficient precondition will be used.

slide-57
SLIDE 57

Conclusions on guards and preconditions

In general, preconditions are assumptions that are necessary for the successful completion of behaviour. They are the part of the contract, between the user of an operation and the implementor of an operation, that must be met by the user

  • f the operation.

Clearly, there is a weakest precondition —necessary and sufficient— but it might be difficult in some cases to determine that weakest precondition. In such cases a sufficient precondition will be used. It is desirable that a precondition is not strengthened capriciously as was done earlier for the FeedBank operation.

slide-58
SLIDE 58

Conclusions on guards and preconditions

In general, preconditions are assumptions that are necessary for the successful completion of behaviour. They are the part of the contract, between the user of an operation and the implementor of an operation, that must be met by the user

  • f the operation.

Clearly, there is a weakest precondition —necessary and sufficient— but it might be difficult in some cases to determine that weakest precondition. In such cases a sufficient precondition will be used. It is desirable that a precondition is not strengthened capriciously as was done earlier for the FeedBank operation. Guards are used to select between different states and inputs. A guard ensures that some condition is satisfied; there is no assumption.