Formal Methods for Software Development | TDA294/DIT271, LP1, HT2019 |

## Lab 2 - Specification and Verification of Programs using KeYEnsure that you are using ## Problem 1: First-Order LogicProve the validity of the following untyped first-order formula using the sequent calculus. $$(\forall x; ((r(x) \implies \lnot g(x)) \land (g(x) \implies \lnot r(x)))) \land (\forall x; \forall y; (i(x,y) \implies r(x) \lor r(y))) \implies (\lnot \exists x; \exists y; (i(x,y) \land g(x) \land g(y))) \land (\exists x; \exists y; i(x,y) \implies \exists z; r(z))$$ One could, for example, imagine that the formula speaks about traffic lights placed at intersections, where the unary predicates r and g speak about a traffic light being red or green respectively, and the binary predicate i can be interpreted as two lights standing at the same intersection.Your task is to first formulate the problem in a KeY problem file, and then load the file in KeY and prove it using the first-order logic rules presented during the lectures, as summarized in this PDF. You are not allowed to use any other rules.
In particular letting KeY auto-prove the formula will not help you much,
as the proof is vastly different and uses many rules which are forbidden.
For the lab report, you should save your KeY proof into a file. In KeY, once you are done with the proof, go to
## Problem 2: JML SpecificationsThe file We will return to this Java file in problem 4 (below), but for now you do not need to implement or prove anything. ## Problem 3: Verification with KeY
The class Sometimes KeY cannot find proofs in a fully automatic fashion. In this part of the lab you will need to interact with KeY in order to provide the correct information so that the prover can find the proofs.
Some proofs can be lengthy and it is easy to get lost in the mare magnum of proof steps. However, constructing proofs is a creative process, in the same way writing software is a creative process (and to some extent, the mechanical aspects of pen-and-paper proofs are supposed to be handled by the KeY system already). Consequently, everyone approaches proofs differently, so it is difficult to give general guidelines on how to close KeY proofs, but we hope the following guidelines might be of some use: -
*Read carefully the contracts you need to prove*. It is important that you understand what is the property you need to prove before you hit the play button. If you get stuck, revisit the property and make sure you really understand what you are trying to prove. -
*Analyse the proof tree*. Specially check the branching points of the proof. They usually provide useful hints on why a proof cannot be close. Here it is also very important that you understand what KeY is trying to prove in a particular branch. Looking at the splitting node (small light-blue folders in the proof tree) gives you information about the goal to prove. When you click on the folder, look at proof obligation and try to understand what needs to be proven in the branch. -
*Consult the KeY book*. We recommend you to have a look at section 15.3 of the KeY book as complementary material to do this lab. This section includes various hints on how to effectively use KeY for verification of large programs.
## Task 3.1: Normal Behaviour
Verify with KeY that the method
## Task 3.2: Exceptional BehaviourYou may have noticed that the method
## Task 3.3: InvariantsThe class`Drawing` that you can find in the file `Drawing.java` contains a two-dimensional array that models a canvas onto which lines can be drawn. When a point is coloured, the corresponding entry in the array is set to true. The class has two invariants. One specifies that the canvas must be square and one specifies that every coloured point on the canvas must be part of either a horizontal or a vertical line.
The proof of every method contract also automatically includes the invariants as assumptions and as proof goals. Thus, showing any method contract also includes proving that the respective method preserves the invariants. We will now take a closer look at proving invariants by proving contracts without postconditions so that the proof obligations are reduced to the preservation of invariants.
In this task you will verify two such contracts. Before you start make sure both "Method Treatment" and "Loop Treatment" under "Proof Search Strategy" are set to "None", and "One Step Simplification" is set to "Enabled". When symbolic execution reaches a call to another method, or a loop, you will have to perform interactive steps in order to continue with the proof.
Prove that the method `drawMultiple()` preserves the invariants. In order to do so, under*"Proof Search Strategy"*set the option*"Class axiom rule"*to*"Delayed"*.First set the *"Proof Search Strategy"*option*"Class axiom rule"*(back) to*"Free"*. The task is to prove that the method`drawVertical(int depth)` preserves the class invariants. For this, you will have to find and correct a small mistake in the loop invariant.One way of doing this is to attempt the proof using the erroneous invariant, as given, and inspect the goal(s) that cannot be closed. For that, set the "Max. Rule Applications" down to 1000. The branch "Initially Valid" can be closed using "apply rules automatically here" (which is offered when the mouse is on the sequent arrow). To discover the error in the loop invariant, apply rules automatically on the branch "Body Preserves Invariant", prune the tree before the CUT nodes that branch over the `length` of canvas. Watch out for formulas on both sides of the sequent which are similar but not equal. Once you have corrected the mistake, restart the proof, with "Max. Rule Applications" set to 20k, such that the proof will only require interaction upon reaching the loop and the remaining proof works automatically.
## Problem 4: Further Verification with KeYAfter the warm-up with KeY in problem 3, we will now return to the Java file
## Reporting- The files required in the submission of this lab are:
- A text report named
**Report_Lab2.txt**describing your solutions to problem 3 and 4 (what you did and why) **All**modified Java files**All**KeY proofs (as instructed above, please make sure you are using the correct KeY version)- Do not submit your files as an archive file (such as zip, tar.bz2, tar.gz or similar)
- Make sure to submit the report in plain text format (.txt). Avoid other formats as this may cause delays in grading your submission.
- Submit via Fire
| |

W. Ahrendt, Oct 16, 2019 |