// In this assignment, we provide method implementations but not enough // annotations to convince Dafny that the methods are correct. In each case, // your job is to add annotations *without* changing lines of code that // determine the step-by-step executable behavior of a method. Sometimes there // exists a simpler but less-efficient implementation of a method; it doesn't // count to switch to that version so there is less proving work! include "pset10_definitions.dfy" // See pset10_hints.dfy for some spoilers. module Pset10 { import opened Defs // Add annotations to this method so that it passes verification. // In English, its purpose is to find two elements of a sorted array that sum to // target number k. method TwoSum(arr: array, k: int) returns (r1: int, r2: int) requires exists n, m :: 0 <= n < arr.Length && 0 <= m < arr.Length && arr[n] + arr[m] == k requires Ascending(arr) ensures 0 <= r1 < arr.Length ensures 0 <= r2 < arr.Length ensures arr[r1] + arr[r2] == k { var i := 0; var j := arr.Length - 1; while true { if arr[i] + arr[j] < k { i := i + 1; } else if arr[i] + arr[j] > k { j := j - 1; } else { return i, j; } } } // Given that three sorted arrays share some common element, find the indices // where it appears. method FindCommonElement(f: array, g: array, h: array) returns (x: nat, y: nat, z: nat) requires Ascending(f) && Ascending(g) && Ascending(h) requires exists i, j, k :: 0 <= i < f.Length && 0 <= j < g.Length && 0 <= k < h.Length && f[i] == g[j] == h[k] ensures 0 <= x < f.Length ensures 0 <= y < g.Length ensures 0 <= z < h.Length ensures f[x] == g[y] == h[z] { x, y, z := 0, 0, 0; while true { if f[x] < g[y] { x := x + 1; } else if g[y] < h[z] { y := y + 1; } else if h[z] < f[x] { z := z + 1; } else { return; } } } // The following method finds the longest ascending subsequence within an // array and returns the start and end indices of that subsequence. // Verify that the subsequence is both ascending and the longest (i.e. // every other ascending subsequence is not longer than the returned one). method LongestAscending(arr: array) returns (i: nat, j: nat) requires arr.Length > 0 ensures 0 <= i <= j < arr.Length ensures AscendingInRange(arr, i, j) ensures forall k, l :: 0 <= k <= l < arr.Length && AscendingInRange(arr, k, l) ==> l - k <= j - i { i, j := 0, 0; var i', j' := 0, 0; while j' < arr.Length - 1 { if arr[j'] <= arr[j' + 1] { j' := j' + 1; } else { if j' - i' > j - i { j := j'; i := i'; } j' := j' + 1; i' := j'; } } if j' - i' > j - i { j := j'; i := i'; } } // The naive implementation of FindPeak is to iterate though all elements and // check if each element is a peak, but it takes linear time. The following // algorithm only takes log time, exploiting properties of peaks in arrays. // Prove that this method finds a peak in any nonempty array where no adjacent // elements are equal. method FindPeak(arr: array) returns (i: int) requires 0 < arr.Length requires forall k :: 0 <= k < arr.Length - 1 ==> arr[k] != arr[k+1] ensures 0 <= i < arr.Length ensures IsPeak(arr, i) { var l, r := 0, arr.Length; while l < r { var m := (l + r) / 2; if m > 0 && arr[m] < arr[m-1] { r := m; } else if m < arr.Length - 1 && arr[m] < arr[m+1] { l := m + 1; } else { return m; } } } // Prove that this method computes BalancedParens using a simple loop with a // counter. method CheckBalancedParens(str: array) returns (b: bool) requires forall c :: c in str[..] ==> c == '(' || c == ')' ensures b == BalancedParens(str[..]) { var i, cnt := 0, 0; while i < str.Length { if str[i] == '(' { cnt := cnt + 1; } else if str[i] == ')' { if cnt <= 0 { return false; } cnt := cnt - 1; } i := i + 1; } return cnt == 0; } }