array.mlw 6.55 KB
Newer Older
MARCHE Claude's avatar
MARCHE Claude committed
1
(** {1 Arrays} *)
2

MARCHE Claude's avatar
MARCHE Claude committed
3 4
(** {2 Generic Arrays}

5 6
The length is a non-mutable field, so that we get for free that
modification of an array does not modify its length.
MARCHE Claude's avatar
MARCHE Claude committed
7 8

*)
9

10 11 12
module Array

  use import int.Int
13
  use import map.Map as M
14

15
  type array 'a model {| length : int; mutable elts : map int 'a |}
16

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

Andrei Paskevich's avatar
Andrei Paskevich committed
19
  function set (a: array 'a) (i: int) (v: 'a) : array 'a =
20 21
    {| a with elts = M.set a.elts i v |}

MARCHE Claude's avatar
MARCHE Claude committed
22
  (** syntactic sugar *)
Andrei Paskevich's avatar
Andrei Paskevich committed
23 24
  function ([]) (a: array 'a) (i: int) : 'a = get a i
  function ([<-]) (a: array 'a) (i: int) (v: 'a) : array 'a = set a i v
25

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

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

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

MARCHE Claude's avatar
MARCHE Claude committed
34
  (** unsafe get/set operations with no precondition *)
35 36
  exception OutOfBounds

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

44
  let defensive_set (a: array 'a) (i: int) (v: 'a) =
45
    { }
46 47
    if i < 0 || i >= length a then raise OutOfBounds;
    a[i] <- v
Andrei Paskevich's avatar
Andrei Paskevich committed
48 49
    { 0 <= i < length a /\ a = (old a)[i <- v] }
    | OutOfBounds -> { i < 0 \/ i >= length a }
50

51 52 53
  function make (n: int) (v: 'a) : array 'a =
    {| length = n; elts = M.const v |}

Andrei Paskevich's avatar
Andrei Paskevich committed
54
  val make (n: int) (v: 'a) :
55
    { n >= 0 }
56
    array 'a
57
    { result = make n v }
58

Andrei Paskevich's avatar
Andrei Paskevich committed
59
  val append (a1: array 'a) (a2: array 'a) :
60 61
    {}
    array 'a
Andrei Paskevich's avatar
Andrei Paskevich committed
62 63
    { length result = length a1 + length a2 /\
      (forall i:int. 0 <= i < length a1 -> result[i] = a1[i]) /\
64 65
      (forall i:int. 0 <= i < length a2 -> result[length a1 + i] = a2[i]) }

Andrei Paskevich's avatar
Andrei Paskevich committed
66
  val sub (a: array 'a) (ofs: int) (len: int) :
Andrei Paskevich's avatar
Andrei Paskevich committed
67
    { 0 <= ofs /\ ofs + len <= length a }
68
    array 'a
Andrei Paskevich's avatar
Andrei Paskevich committed
69
    { length result = len /\
70 71
      forall i:int. 0 <= i < len -> result[i] = a[ofs + i] }

Andrei Paskevich's avatar
Andrei Paskevich committed
72
  val copy (a: array 'a) :
73 74
    {}
    array 'a
Andrei Paskevich's avatar
Andrei Paskevich committed
75
    { length result = length a /\
76 77
      forall i:int. 0 <= i < length result -> result[i] = a[i] }

78
  let fill (a: array 'a) (ofs: int) (len: int) (v: 'a) =
Andrei Paskevich's avatar
Andrei Paskevich committed
79
    { 0 <= ofs /\ ofs + len <= length a }
80
'Init:
81 82 83
    for k = 0 to len - 1 do
      invariant {
        (forall i:int.
84
        (0 <= i < ofs \/ ofs + len <= i < length a) -> a[i] = (at a 'Init)[i])
Andrei Paskevich's avatar
Andrei Paskevich committed
85
        /\
86 87 88 89 90
        (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
91 92
        (0 <= i < ofs \/ ofs + len <= i < length a) -> a[i] = (old a)[i])
      /\
93 94
      (forall i:int.
        ofs <= i < ofs + len -> a[i] = v) }
95

Andrei Paskevich's avatar
Andrei Paskevich committed
96
  val blit (a1: array 'a) (ofs1: int)
97
                 (a2: array 'a) (ofs2: int) (len: int) :
Andrei Paskevich's avatar
Andrei Paskevich committed
98 99
    { 0 <= ofs1 /\ ofs1 + len <= length a1 /\
      0 <= ofs2 /\ ofs2 + len <= length a2 }
100 101 102
    unit
    writes a2
    { (forall i:int.
Andrei Paskevich's avatar
Andrei Paskevich committed
103 104
        (0 <= i < ofs2 \/ ofs2 + len <= i < length a2) -> a2[i] = (old a2)[i])
      /\
105 106
      (forall i:int.
        ofs2 <= i < ofs2 + len -> a2[i] = a1[ofs1 + i - ofs2]) }
107

MARCHE Claude's avatar
MARCHE Claude committed
108
  (*** TODO?
109 110
     - concat : 'a array list -> 'a array
     - to_list
111
     - of_list
112
  *)
113 114 115

end

MARCHE Claude's avatar
MARCHE Claude committed
116 117
(** {2 Sorted Arrays} *)

118 119
module ArraySorted

120
  use import int.Int
121
  use import module Array
Andrei Paskevich's avatar
Andrei Paskevich committed
122
  clone import map.MapSorted as M with type elt = int, predicate le = (<=)
123

Andrei Paskevich's avatar
Andrei Paskevich committed
124
  predicate sorted_sub (a : array int) (l u : int) =
125 126
    M.sorted_sub a.elts l u

Andrei Paskevich's avatar
Andrei Paskevich committed
127
  predicate sorted (a : array int) =
128 129 130 131
    M.sorted_sub a.elts 0 a.length

end

MARCHE Claude's avatar
MARCHE Claude committed
132 133
(** {2 Arrays Equality} *)

134 135 136
module ArrayEq

  use import module Array
137
  use import map.MapEq as M
138

Andrei Paskevich's avatar
Andrei Paskevich committed
139
  predicate array_eq_sub (a1 a2: array 'a) (l u: int) =
140 141
    map_eq_sub a1.elts a2.elts l u

Andrei Paskevich's avatar
Andrei Paskevich committed
142 143
  predicate array_eq (a1 a2: array 'a) =
    a1.length = a2.length /\
144 145 146 147
    array_eq_sub a1 a2 0 a1.length

end

MARCHE Claude's avatar
MARCHE Claude committed
148 149
(** {2 Permutation} *)

150 151
module ArrayPermut

152
  use import int.Int
153 154 155
  use import module Array
  clone import map.MapPermut as M

Andrei Paskevich's avatar
Andrei Paskevich committed
156
  predicate exchange (a1 a2: array 'a) (i j: int) =
157 158
    M.exchange a1.elts a2.elts i j

Andrei Paskevich's avatar
Andrei Paskevich committed
159
  predicate permut_sub (a1 a2: array 'a) (l u: int) =
160 161
    M.permut_sub a1.elts a2.elts l u

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

165
  lemma exchange_permut:
166
    forall a1 a2: array 'a, i j: int [exchange a1 a2 i j].
167 168 169 170
    exchange a1 a2 i j -> a1.length = a2.length ->
    0 <= i < a1.length -> 0 <= j < a1.length ->
    permut a1 a2

171 172 173 174 175 176 177 178
  lemma permut_sym:
    forall a1 a2: array 'a.
    permut a1 a2 -> permut a2 a1

  lemma permut_trans:
    forall a1 a2 a3: array 'a.
    permut a1 a2 -> permut a2 a3 -> permut a1 a3

179 180 181 182 183 184 185 186 187 188
  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

189 190
end

MARCHE Claude's avatar
MARCHE Claude committed
191 192
(** {2 Sum of elements} *)

193 194 195 196 197
module ArraySum

  use import module Array
  clone import map.MapSum as M

MARCHE Claude's avatar
MARCHE Claude committed
198
  (** [sum a l h] is the sum of [a[i]] for [l <= i < h] *)
Andrei Paskevich's avatar
Andrei Paskevich committed
199
  function sum (a: array int) (l h: int) : int = M.sum a.elts l h
200 201 202

end

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
(** {2 Number of array elements satisfying a given predicate}

This module is parametrized by a predicate [pr]. It is supposed to be
cloned with needed instances for [pr]. It is also parametrized by a
type [param] to be used as an additional parameter to [pr].

*)

module NumOfParam
  use import Array

  type elt
  type param
  predicate pr (p: param) (i: int) (e: elt)

  type param_ = (array elt, param)
  predicate pr_ (p: param_) (i: int) = let (a, p) = p in pr p i a[i]
  clone import int.NumOfParam with type param = param_, predicate pr = pr_

222
  (* the number of a[i] such that [l <= i < u] and [pr p i a[i]] *)
223 224 225
  function numof (a: array elt) (p: param) (l u: int) : int = num_of (a, p) l u
end

226 227 228 229 230
module TestArray

  use import int.Int
  use import module Array

231
  let test_append () =
232 233
    let a1 = make 17 2 in
    assert { a1[3] = 2 };
234
    a1[3] <- 4;
235 236 237 238 239 240 241 242 243
    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 };
    ()

244
  let test_fill () =
245 246
    let a = make 17 True in
    fill a 10 4 False;
247 248 249
    assert { a[9] = True };
    assert { a[10] = False };
    assert { a[14] = True }
250

251 252 253 254 255 256 257 258 259 260
  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 }

261 262
end

MARCHE Claude's avatar
MARCHE Claude committed
263
(***
264
Local Variables:
265
compile-command: "unset LANG; make -C .. modules/array"
266
End:
267
*)