binary_search.mlw 5.06 KB
Newer Older
1 2 3 4
(* Binary search

   A classical example. Searches a sorted array for a given value v. *)

5
module BinarySearch
6

7 8 9 10
  use int.Int
  use int.ComputerDivision
  use ref.Ref
  use array.Array
11

12 13 14 15
  (* the code and its specification *)

  exception Not_found (* raised to signal a search failure *)

16
  let binary_search (a : array int) (v : int) : int
17 18 19
    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 }
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  =
    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
38

39
end
40

41
(* A generalization: the midpoint is computed by some abstract function.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
42
   The only requirement is that it lies between l and u. *)
43 44 45

module BinarySearchAnyMidPoint

46 47 48
  use int.Int
  use ref.Ref
  use array.Array
49 50 51

  exception Not_found (* raised to signal a search failure *)

52 53
  val midpoint (l:int) (u:int) : int
    requires { l <= u } ensures { l <= result <= u }
54

55
  let binary_search (a :array int) (v : int) : int
56 57 58
    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 }
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  =
    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
76 77 78

end

79 80 81 82 83 84
(* The following version of binary search is faster in practice, by being
   friendlier with the branch predictor of most processors. It also happens
   to be stable, since it always return the highest index. *)

module BinarySearchBranchless

85 86 87 88
  use int.Int
  use int.ComputerDivision
  use ref.Ref
  use array.Array
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

  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 }
    ensures  { forall i : int. result < i < length a -> a[i] <> v }
    raises   { Not_found -> forall i:int. 0 <= i < length a -> a[i] <> v }
  =
    let l = ref 0 in
    let s = ref (length a) in
    if !s = 0 then raise Not_found;
    while !s > 1 do
      invariant { 0 <= !l /\ !l + !s <= length a /\ !s >= 1 }
      invariant {
        forall i : int. 0 <= i < length a -> a[i] = v -> a[!l] <= v /\ i < !l + !s }
      variant { !s }
      let h = div !s 2 in
      let m = !l + h in
      l := if a[m] > v then !l else m;
      s := !s - h;
    done;
    if a[!l] = v then !l
    else raise Not_found

end

116 117 118 119
(* binary search using 32-bit integers *)

module BinarySearchInt32

120 121 122 123
  use int.Int
  use mach.int.Int32
  use ref.Ref
  use mach.array.Array32
124

125
  exception Not_found   (* raised to signal a search failure *)
126

127
  let binary_search (a : array int32) (v : int32) : int32
128 129 130
    requires { forall i1 i2 : int. 0 <= i1 <= i2 < a.length ->
               a[i1] <= a[i2] }
    ensures  { 0 <= result < a.length /\ a[result] = v }
131
    raises   { Not_found ->
132
                 forall i:int. 0 <= i < a.length -> a[i] <> v }
133
  =
134 135
    let l = ref 0 in
    let u = ref (length a - 1) in
136
    while !l <= !u do
137 138 139 140 141 142
      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 };
143
      if a[m] < v then
144
        l := m + 1
145
      else if a[m] > v then
146
        u := m - 1
147 148 149 150
      else
        return m
    done;
    raise Not_found
151 152

end
153 154 155 156 157 158

(* 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

159 160 161 162
  use int.Int
  use int.ComputerDivision
  use ref.Ref
  use array.Array
163 164 165 166 167

  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 }
168 169 170 171
    ensures  { 0 <= result < a.length }
    ensures  { a[result] = 1 }
    ensures  { forall i. 0 <= i < result -> a[i] = 0 }
 =
172 173 174 175 176
    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 }
177
      invariant { forall i. 0 <= i < !lo -> a[i] = 0 }
178 179 180 181 182 183 184 185 186 187
      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