datatype Option = None | Some(value: X) // This BinarySearch method requires the input array to be sorted, but it only // states the requirement on adjacent elements, not on all pairs of indices. // Without changing the method specification or the implementation code, add // annotations so that Dafny proves correctness. For example, you might write // and use a lemma proving that the adjacent-elements-ordered property implies // the "transitive" version. method BinarySearch(a: array, key: int) returns (r: Option) requires forall i :: 0 <= i < a.Length - 1 ==> a[i] <= a[i+1] ensures r.Some? ==> 0 <= r.value < a.Length && a[r.value] == key ensures r == None ==> key !in a[..] { var lo, hi := 0, a.Length; while lo < hi invariant 0 <= lo <= hi <= a.Length invariant key !in a[..lo] && key !in a[hi..] { var m := (lo + hi) / 2; if key < a[m] { hi := m; } else if a[m] < key { lo := m + 1; } else { return Some(m); } } return None; }