Assignment 5: Verification with Dafny


Due Sunday, December 16 at 11:59 PM

Goals for This Assignment

By the time you have completed this work, you should be able to:

This assignment may be completed in groups.

Step-by-Step Instructions

Step 1: Prove Factorial Correct

Download the template code here. This code contains a recursive factorial function method, which has been proven correct. This code also contains a factorial_iterative method, which computes factorial via a loop.

factorial_iterative has a postcondition which expresses that it behaves the same as factorial. While the implementation is correct, Dafny is unable to prove that the implementation satisfies the postcondition fully automatically. For this part of the assignment, you'll need to add loop invariants nad possibly assertions to prove the code correct. You should not change the preconditions, postconditions, or signature of factorial_iterative or factorial. If you wish, you can change the implementation.

Hints

Step 2: Prove Array Splicing Correct

Download the template code here. This code contains a splice_in method, which is used to stitch one array into another, returning the result. Like the previous step, the implementation you've been provided is correct, though Dafny cannot automatically prove it. You'll need to add in invariant and assert statements to get this code working. You may change the implementation if you desire, but you may not change the signature of splice_in, or the preconditions and postconditions of splice_in.

Recommended Approach

Many of the hints in the prior step are applicable here. The provided implementation can be divided into three parts:

  1. Copy elements from a until index into the result array.
  2. Copy the contents of add_these into the result array.
  3. Copy the remaining elements in a into the result array.

Each one of these parts has its own corresponding postcondition on splice_in. With this in mind, there are two major things you need to do for each of the three parts:

  1. Show that the given part satisfies its given postcondition. You'll need to show that the part's loop incrementally gets closer to the part's postcondition. It's recommended to put an assert statement immediately after the part which simply asserts the corresponding postcondition, which will give you a quick indication of whether or not you've actually satisfied the postcondition (this assertion will be true if you satisfied the postcondition, and it will be false if you didn't).
  2. Show that the given part doesn't jeopardize the postconditions from any previous parts. Related to the previous point, you should start with assert statements which merely repeat the other postconditions. If these fail, you'll need to provide Dafny further information that you haven't broken another postcondition. You may need to add prior proven postconditions as loop invariants later (i.e., it's invariant that this loop doesn't break a previously-proven postcondition.

Step 3: Submit Everything

Submit your factorial.dfy and splice.dfy files to the corresponding box on Canvas. In the comments for the submission, list everyone you worked with, if applicable.