binary_search_vc_sp.mlw 3.98 KB
 MARCHE Claude committed Oct 13, 2017 1 2 3 4 5 6 ``````(* Binary search A classical example. Searches a sorted array for a given value v. *) module BinarySearch `````` Andrei Paskevich committed Jun 15, 2018 7 8 9 10 `````` use int.Int use int.ComputerDivision use ref.Ref use array.Array `````` MARCHE Claude committed Oct 13, 2017 11 12 13 14 15 16 17 18 19 `````` (* the code and its specification *) exception Not_found (* raised to signal a search failure *) let binary_search (a : array int) (v : int) : int requires { forall i1 i2 : int. 0 <= i1 <= i2 < length a -> a[i1] <= a[i2] } ensures { 0 <= result < length a /\ a[result] = v } raises { Not_found -> forall i:int. 0 <= i < length a -> a[i] <> v } `````` Guillaume Melquiond committed Jan 12, 2018 20 `````` = [@vc:sp] `````` MARCHE Claude committed Oct 13, 2017 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 `````` let l = ref 0 in let u = ref (length a - 1) in while !l <= !u do invariant { 0 <= !l /\ !u < length a } invariant { forall i : int. 0 <= i < length a -> a[i] = v -> !l <= i <= !u } variant { !u - !l } let m = !l + div (!u - !l) 2 in assert { !l <= m <= !u }; if a[m] < v then l := m + 1 else if a[m] > v then u := m - 1 else return m done; raise Not_found end (* A generalization: the midpoint is computed by some abstract function. The only requirement is that it lies between l and u. *) module BinarySearchAnyMidPoint `````` Andrei Paskevich committed Jun 15, 2018 46 47 48 `````` use int.Int use ref.Ref use array.Array `````` MARCHE Claude committed Oct 13, 2017 49 50 51 52 53 54 55 56 57 58 `````` exception Not_found (* raised to signal a search failure *) val midpoint (l:int) (u:int) : int requires { l <= u } ensures { l <= result <= u } let binary_search (a :array int) (v : int) : int requires { forall i1 i2 : int. 0 <= i1 <= i2 < length a -> a[i1] <= a[i2] } ensures { 0 <= result < length a /\ a[result] = v } raises { Not_found -> forall i:int. 0 <= i < length a -> a[i] <> v } `````` Guillaume Melquiond committed Jan 12, 2018 59 `````` = [@vc:sp] `````` MARCHE Claude committed Oct 13, 2017 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 `````` let l = ref 0 in let u = ref (length a - 1) in while !l <= !u do invariant { 0 <= !l /\ !u < length a } invariant { forall i : int. 0 <= i < length a -> a[i] = v -> !l <= i <= !u } variant { !u - !l } let m = midpoint !l !u in if a[m] < v then l := m + 1 else if a[m] > v then u := m - 1 else return m done; raise Not_found end (* binary search using 32-bit integers *) module BinarySearchInt32 `````` Andrei Paskevich committed Jun 15, 2018 83 84 85 86 `````` use int.Int use mach.int.Int32 use ref.Ref use mach.array.Array32 `````` MARCHE Claude committed Oct 13, 2017 87 88 89 90 91 92 `````` (* the code and its specification *) exception Not_found (* raised to signal a search failure *) let binary_search (a : array int32) (v : int32) : int32 `````` 93 94 95 `````` requires { forall i1 i2 : int. 0 <= i1 <= i2 < a.length -> a[i1] <= a[i2] } ensures { 0 <= result < a.length /\ a[result] = v } `````` MARCHE Claude committed Oct 13, 2017 96 `````` raises { Not_found -> `````` 97 `````` forall i:int. 0 <= i < a.length -> a[i] <> v } `````` Guillaume Melquiond committed Jan 12, 2018 98 `````` = [@vc:sp] `````` 99 100 `````` let l = ref 0 in let u = ref (length a - 1) in `````` MARCHE Claude committed Oct 13, 2017 101 `````` while !l <= !u do `````` 102 103 104 105 106 107 `````` invariant { 0 <= !l /\ !u < a.length } invariant { forall i : int. 0 <= i < a.length -> a[i] = v -> !l <= i <= !u } variant { !u - !l } let m = !l + (!u - !l) / 2 in assert { !l <= m <= !u }; `````` MARCHE Claude committed Oct 13, 2017 108 `````` if a[m] < v then `````` 109 `````` l := m + 1 `````` MARCHE Claude committed Oct 13, 2017 110 `````` else if a[m] > v then `````` 111 `````` u := m - 1 `````` MARCHE Claude committed Oct 13, 2017 112 113 114 115 116 117 118 119 120 121 122 123 `````` else return m done; raise Not_found end (* A particular case with Boolean values (0 or 1) and a sentinel 1 at the end. We look for the first position containing a 1. *) module BinarySearchBoolean `````` Andrei Paskevich committed Jun 15, 2018 124 125 126 127 `````` use int.Int use int.ComputerDivision use ref.Ref use array.Array `````` MARCHE Claude committed Oct 13, 2017 128 129 130 131 132 133 134 135 `````` let binary_search (a: array int) : int requires { 0 < a.length } requires { forall i j. 0 <= i <= j < a.length -> 0 <= a[i] <= a[j] <= 1 } requires { a[a.length - 1] = 1 } ensures { 0 <= result < a.length } ensures { a[result] = 1 } ensures { forall i. 0 <= i < result -> a[i] = 0 } `````` Guillaume Melquiond committed Jan 12, 2018 136 `````` = [@vc:sp] `````` MARCHE Claude committed Oct 13, 2017 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 `````` let lo = ref 0 in let hi = ref (length a - 1) in while !lo < !hi do invariant { 0 <= !lo <= !hi < a.length } invariant { a[!hi] = 1 } invariant { forall i. 0 <= i < !lo -> a[i] = 0 } variant { !hi - !lo } let mid = !lo + div (!hi - !lo) 2 in if a[mid] = 1 then hi := mid else lo := mid + 1 done; !lo end``````