module Defs { // Here are two predicates that are handy to capture sortedness of arrays. ghost predicate Ascending(arr: array) reads arr { forall i, j :: 0 <= i <= j < arr.Length ==> arr[i] <= arr[j] } // ghost predicate Descending(arr: array) // reads arr // { // forall i, j :: 0 <= i <= j < arr.Length ==> arr[i] >= arr[j] // } // The following are like the above restricted to particular ranges within // arrays. ghost predicate AscendingInRange(arr: array, i: int, j: int) reads arr requires 0 <= i <= j < arr.Length { forall k, l :: i <= k <= l <= j ==> arr[k] <= arr[l] } // ghost predicate DescendingInRange(arr: array, i: int, j: int) // reads arr // requires 0 <= i <= j < arr.Length // { // forall k, l :: i <= k <= l <= j ==> arr[k] >= arr[l] // } // Is a given position of an array a *peak*, meaning it is higher than its // neighbors? ghost predicate IsPeak(arr: array, i: int) requires 0 <= i < arr.Length reads arr { (i > 0 ==> arr[i] > arr[i-1]) && (i < arr.Length - 1 ==> arr[i] > arr[i+1]) } // Does an array in fact have no peak within a certain range? // predicate hasNoPeak(arr: array, i: int, j: int) // reads arr // requires 0 < arr.Length // requires 0 <= i <= j < arr.Length // { // forall k :: i <= k <= j ==> !IsPeak(arr, k) // } // A recursive definition of what it means for a string to be made up of // balanced parentheses. ghost predicate BalancedParens(s: seq) { |s| == 0 || (|s| >= 2 && s[0] == '(' && s[|s|-1] == ')' && BalancedParens(s[1..|s|-1])) || exists i :: 0 < i < |s| && BalancedParens(s[..i]) && BalancedParens(s[i..]) } }