binary_search.mlw 3.47 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
  use import int.Int
8
  use import int.ComputerDivision
9 10
  use import module ref.Ref
  use import module array.Array
11

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

  exception Break int (* raised to exit the loop *)
  exception Not_found (* raised to signal a search failure *)

  let binary_search (a :array int) (v : int) =
18
    { forall i1 i2 : int. 0 <= i1 <= i2 < length a -> a[i1] <= a[i2] }
19 20 21 22 23
    try
      let l = ref 0 in
      let u = ref (length a - 1) in
      while !l <= !u do
        invariant {
24
          0 <= !l /\ !u < length a /\
25 26
          forall i : int. 0 <= i < length a -> a[i] = v -> !l <= i <= !u }
        variant { !u - !l }
27
        let m = !l + div (!u - !l) 2 in
28
        assert { !l <= m <= !u };
29
        if a[m] < v then
30
          l := m + 1
31
        else if a[m] > v then
32
          u := m - 1
33
        else
34 35 36 37 38 39
          raise (Break m)
      done;
      raise Not_found
    with Break i ->
      i
    end
Andrei Paskevich's avatar
Andrei Paskevich committed
40
    { 0 <= result < length a /\ a[result] = v }
41
    | Not_found -> { forall i:int. 0 <= i < length a -> a[i] <> v }
42

43
end
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
(* A generalization: the midpoint is computed by some abstract function.
   The only requirement is that is lies between l and u. *)

module BinarySearchAnyMidPoint

  use import int.Int
  use import module ref.Ref
  use import module array.Array

  exception Break int (* raised to exit the loop *)
  exception Not_found (* raised to signal a search failure *)

  val midpoint: l:int -> u:int -> { l <= u } int { l <= result <= u }

  let binary_search (a :array int) (v : int) =
    { forall i1 i2 : int. 0 <= i1 <= i2 < length a -> a[i1] <= a[i2] }
    try
      let l = ref 0 in
      let u = ref (length a - 1) in
      while !l <= !u do
        invariant {
          0 <= !l /\ !u < length a /\
          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
          raise (Break m)
      done;
      raise Not_found
    with Break i ->
      i
    end
    { 0 <= result < length a /\ a[result] = v }
    | Not_found -> { forall i:int. 0 <= i < length a -> a[i] <> v }

end

86 87 88 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 116 117 118 119 120 121 122 123 124 125 126 127
(* binary search using 32-bit integers *)

module BinarySearchInt32

  use import module arith.Int32
  use import module ref.Ref
  use import module array.Array

  (* the code and its specification *)

  exception Break int (* raised to exit the loop *)
  exception Not_found (* raised to signal a search failure *)

  let binary_search (a :array int) (v : int) =
    { 0 <= length a <= max_int /\
      forall i1 i2 : int. 0 <= i1 <= i2 < length a -> a[i1] <= a[i2] }
    try
      let l = ref 0 in
      let u = ref (length a - 1) in
      while !l <= !u do
        invariant {
          0 <= !l /\ !u < length a /\
          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
          raise (Break m)
      done;
      raise Not_found
    with Break i ->
      i
    end
    { 0 <= result < length a /\ a[result] = v }
    | Not_found -> { forall i:int. 0 <= i < length a -> a[i] <> v }

end

128
(*
129
Local Variables:
130
compile-command: "unset LANG; make -C ../.. examples/programs/binary_search.gui"
131
End:
132 133
*)