// In this file, you can find hints for... // - FindCommonElement // - LongestAscending // - FindPeak // - CheckBalancedParens // ╭───────────────────╮ // │ FindCommonElement │ // ╰───────────────────╯ // By the existence requirement of a common element, it's possible to declare // THE indices of the common element as ghost variables and asserts the real // indices x,y,z never go beyond the ghost indices. This way, it is possible to // have a simple decrease clause and nearly no manual proof is needed. // ╭──────────────────╮ // │ LongestAscending │ // ╰──────────────────╯ // At any point in the iteration, what is the length of the longest ascending // subsequence up to i'? What is the length of the longest ascending subsequence // up to j'? And for all k, l such that 0 <= k <= l <= j' && AscendingInRange(arr, k, l), // what are some possible ranges of k and l, and why is that? // ╭──────────╮ // │ FindPeak │ // ╰──────────╯ // 1) Think about how and why the log time implementation could find a peak. // 2) Think about what the array would look like if there is no peak between // some indices i and j. For example, if there is no peak between the beginning // of the array and some j (i.e. first few elements), what does the array // look like? How about when there is no peak in the last few elements? How // about in general? How about when there is no peak in the whole array (from // the beginning all the way to the end)? // 3) It would be helpful to define a predicate hasNoPeak(arr, i, j) to describe // there is no peak in range i and j. AscendingInRange is provided for you. // Would DescendingInRange be useful? // ╭─────────────────────╮ // │ CheckBalancedParens │ // ╰─────────────────────╯ // Our approach was to define a function that sums up the open and close parens // in a sequence, adding 1 for open and -1 for close: ghost function ParenCount(s: seq): int { if |s| == 0 then 0 else if s[0] == '(' then ParenCount(s[1..]) + 1 else ParenCount(s[1..]) - 1 } // (For this and some of the other auxiliary functions we defined, it was easier // to omit the contains-only-parentheses precondition and avoid having to convince // Dafny that it would be satisfied.) // We proved that a string has BalancedParens if and only if its parentheses sum // is zero *and* every prefix of the string has a nonnegative parentheses sum.