array.mlw 5.41 KB
Newer Older
1

2 3
(* Arrays *)

4 5 6
module Array

  use import int.Int
7
  use import map.Map as M
8

9
  type array 'a model {| length : int; mutable elts : map int 'a |}
10

Andrei Paskevich's avatar
Andrei Paskevich committed
11
  function get (a: array 'a) (i: int) : 'a = M.get a.elts i
12

Andrei Paskevich's avatar
Andrei Paskevich committed
13
  function set (a: array 'a) (i: int) (v: 'a) : array 'a =
14 15 16
    {| a with elts = M.set a.elts i v |}

  (* syntactic sugar *)
Andrei Paskevich's avatar
Andrei Paskevich committed
17 18
  function ([]) (a: array 'a) (i: int) : 'a = get a i
  function ([<-]) (a: array 'a) (i: int) (v: 'a) : array 'a = set a i v
19

Andrei Paskevich's avatar
Andrei Paskevich committed
20
  val ([]) (a: array 'a) (i: int) :
21
    { 0 <= i < length a } 'a reads a { result = a[i] }
22

Andrei Paskevich's avatar
Andrei Paskevich committed
23
  val ([]<-) (a: array 'a) (i: int) (v: 'a) :
24
    { 0 <= i < length a } unit writes a { a.elts = M.set (old a.elts) i v }
25

Andrei Paskevich's avatar
Andrei Paskevich committed
26
  val length (a: array 'a) : {} int { result = a.length }
27

28
  (* unsafe get/set operations with no precondition *)
29 30
  exception OutOfBounds

31
  let defensive_get (a: array 'a) (i: int) =
32
    { }
33 34
    if i < 0 || i >= length a then raise OutOfBounds;
    a[i]
Andrei Paskevich's avatar
Andrei Paskevich committed
35 36
    { 0 <= i < length a /\ result = a[i] }
    | OutOfBounds -> { i < 0 \/ i >= length a }
37

38
  let defensive_set (a: array 'a) (i: int) (v: 'a) =
39
    { }
40 41
    if i < 0 || i >= length a then raise OutOfBounds;
    a[i] <- v
Andrei Paskevich's avatar
Andrei Paskevich committed
42 43
    { 0 <= i < length a /\ a = (old a)[i <- v] }
    | OutOfBounds -> { i < 0 \/ i >= length a }
44

Andrei Paskevich's avatar
Andrei Paskevich committed
45
  val make (n: int) (v: 'a) :
46 47
    {}
    array 'a
48
    { result = {| length = n; elts = M.const v |} }
Andrei Paskevich's avatar
Andrei Paskevich committed
49
    (* { length result = n /\ forall i:int. 0 <= i < n -> result[i] = v} *)
50

Andrei Paskevich's avatar
Andrei Paskevich committed
51
  val append (a1: array 'a) (a2: array 'a) :
52 53
    {}
    array 'a
Andrei Paskevich's avatar
Andrei Paskevich committed
54 55
    { length result = length a1 + length a2 /\
      (forall i:int. 0 <= i < length a1 -> result[i] = a1[i]) /\
56 57
      (forall i:int. 0 <= i < length a2 -> result[length a1 + i] = a2[i]) }

Andrei Paskevich's avatar
Andrei Paskevich committed
58
  val sub (a: array 'a) (ofs: int) (len: int) :
Andrei Paskevich's avatar
Andrei Paskevich committed
59
    { 0 <= ofs /\ ofs + len <= length a }
60
    array 'a
Andrei Paskevich's avatar
Andrei Paskevich committed
61
    { length result = len /\
62 63
      forall i:int. 0 <= i < len -> result[i] = a[ofs + i] }

Andrei Paskevich's avatar
Andrei Paskevich committed
64
  val copy (a: array 'a) :
65 66
    {}
    array 'a
Andrei Paskevich's avatar
Andrei Paskevich committed
67
    { length result = length a /\
68 69
      forall i:int. 0 <= i < length result -> result[i] = a[i] }

70
  let fill (a: array 'a) (ofs: int) (len: int) (v: 'a) =
Andrei Paskevich's avatar
Andrei Paskevich committed
71
    { 0 <= ofs /\ ofs + len <= length a }
72
'Init:
73 74 75
    for k = 0 to len - 1 do
      invariant {
        (forall i:int.
76
        (0 <= i < ofs \/ ofs + len <= i < length a) -> a[i] = (at a 'Init)[i])
Andrei Paskevich's avatar
Andrei Paskevich committed
77
        /\
78 79 80 81 82
        (forall i:int. ofs <= i < ofs + k -> a[i] = v)
      }
      a[ofs + k] <- v
    done
    { (forall i:int.
Andrei Paskevich's avatar
Andrei Paskevich committed
83 84
        (0 <= i < ofs \/ ofs + len <= i < length a) -> a[i] = (old a)[i])
      /\
85 86
      (forall i:int.
        ofs <= i < ofs + len -> a[i] = v) }
87

Andrei Paskevich's avatar
Andrei Paskevich committed
88
  val blit (a1: array 'a) (ofs1: int)
89
                 (a2: array 'a) (ofs2: int) (len: int) :
Andrei Paskevich's avatar
Andrei Paskevich committed
90 91
    { 0 <= ofs1 /\ ofs1 + len <= length a1 /\
      0 <= ofs2 /\ ofs2 + len <= length a2 }
92 93 94
    unit
    writes a2
    { (forall i:int.
Andrei Paskevich's avatar
Andrei Paskevich committed
95 96
        (0 <= i < ofs2 \/ ofs2 + len <= i < length a2) -> a2[i] = (old a2)[i])
      /\
97 98
      (forall i:int.
        ofs2 <= i < ofs2 + len -> a2[i] = a1[ofs1 + i - ofs2]) }
99

100 101 102
  (* TODO?
     - concat : 'a array list -> 'a array
     - to_list
103
     - of_list
104
  *)
105 106 107

end

108 109
module ArraySorted

110
  use import int.Int
111
  use import module Array
Andrei Paskevich's avatar
Andrei Paskevich committed
112
  clone import map.MapSorted as M with type elt = int, predicate le = (<=)
113

Andrei Paskevich's avatar
Andrei Paskevich committed
114
  predicate sorted_sub (a : array int) (l u : int) =
115 116
    M.sorted_sub a.elts l u

Andrei Paskevich's avatar
Andrei Paskevich committed
117
  predicate sorted (a : array int) =
118 119 120 121 122 123 124
    M.sorted_sub a.elts 0 a.length

end

module ArrayEq

  use import module Array
125
  use import map.MapEq as M
126

Andrei Paskevich's avatar
Andrei Paskevich committed
127
  predicate array_eq_sub (a1 a2: array 'a) (l u: int) =
128 129
    map_eq_sub a1.elts a2.elts l u

Andrei Paskevich's avatar
Andrei Paskevich committed
130 131
  predicate array_eq (a1 a2: array 'a) =
    a1.length = a2.length /\
132 133 134 135 136 137
    array_eq_sub a1 a2 0 a1.length

end

module ArrayPermut

138
  use import int.Int
139 140 141
  use import module Array
  clone import map.MapPermut as M

Andrei Paskevich's avatar
Andrei Paskevich committed
142
  predicate exchange (a1 a2: array 'a) (i j: int) =
143 144
    M.exchange a1.elts a2.elts i j

Andrei Paskevich's avatar
Andrei Paskevich committed
145
  predicate permut_sub (a1 a2: array 'a) (l u: int) =
146 147
    M.permut_sub a1.elts a2.elts l u

Andrei Paskevich's avatar
Andrei Paskevich committed
148 149
  predicate permut (a1 a2: array 'a) =
    a1.length = a2.length /\ M.permut_sub a1.elts a2.elts 0 a1.length
150

151 152 153 154 155 156
  lemma exchange_permut:
    forall a1 a2: array 'a, i j: int.
    exchange a1 a2 i j -> a1.length = a2.length ->
    0 <= i < a1.length -> 0 <= j < a1.length ->
    permut a1 a2

157 158 159 160 161 162 163 164 165 166
  use import module ArrayEq

  lemma array_eq_sub_permut:
    forall a1 a2: array 'a, l u: int.
    array_eq_sub a1 a2 l u -> permut_sub a1 a2 l u

  lemma array_eq_permut:
    forall a1 a2: array 'a.
    array_eq a1 a2 -> permut a1 a2

167 168
end

169 170 171 172 173 174 175
module ArraySum

  use import module Array
  clone import map.MapSum as M

  (* [sum a l h] is the sum of a[i] for l <= i < h *)

Andrei Paskevich's avatar
Andrei Paskevich committed
176
  function sum (a: array int) (l h: int) : int = M.sum a.elts l h
177 178 179

end

180 181 182 183 184
module TestArray

  use import int.Int
  use import module Array

185
  let test_append () =
186 187
    let a1 = make 17 2 in
    assert { a1[3] = 2 };
188
    a1[3] <- 4;
189 190 191 192 193 194 195 196 197
    assert { a1[3] = 4 };
    let a2 = make 25 3 in
    assert { a2[0] = 3 }; (* needed to prove a[17]=3 below *)
    let a  = append a1 a2 in
    assert { length a = 42 };
    assert { a[3] = 4 };
    assert { a[17] = 3 };
    ()

198
  let test_fill () =
199 200
    let a = make 17 True in
    fill a 10 4 False;
201 202 203
    assert { a[9] = True };
    assert { a[10] = False };
    assert { a[14] = True }
204

205 206 207 208 209 210 211 212 213 214
  let test_blit () =
    let a1 = make 17 True in
    let a2 = make 25 False in
    blit a1 10 a2 17 7;
    assert { a1[10] = True  };
    assert { a2[16] = False };
    assert { a2[17] = True  };
    assert { a2[23] = True  };
    assert { a2[24] = False }

215 216
end

217
(*
218
Local Variables:
219
compile-command: "unset LANG; make -C .. modules/array"
220
End:
221
*)