Une MAJ de sécurité est nécessaire sur notre version actuelle. Elle sera effectuée lundi 02/08 entre 12h30 et 13h. L'interruption de service devrait durer quelques minutes (probablement moins de 5 minutes).

array.mlw 6.88 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
    { a with elts = M.set a.elts i v }
21

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

26 27 28
  val ([]) (a: array 'a) (i: int) : 'a reads {a}
    requires { 0 <= i < length a }
    ensures  { result = a[i] }
29

30 31 32
  val ([]<-) (a: array 'a) (i: int) (v: 'a) : unit writes {a}
    requires { 0 <= i < length a }
    ensures  { a.elts = M.set (old a.elts) i v }
33

34
  val length (a: array 'a) : int ensures { result = a.length }
35

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

39 40 41 42
  let defensive_get (a: array 'a) (i: int)
    ensures { 0 <= i < length a /\ result = a[i] }
    raises { OutOfBounds -> i < 0 \/ i >= length a }
  = if i < 0 || i >= length a then raise OutOfBounds;
43
    a[i]
44

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

51
  function make (n: int) (v: 'a) : array 'a =
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
    { length = n; elts = M.const v }

  val make (n: int) (v: 'a) : array 'a
    requires { n >= 0 } ensures { result = make n v }

  val append (a1: array 'a) (a2: array 'a) : array 'a
    ensures { length result = length a1 + length a2 }
    ensures { forall i:int. 0 <= i < length a1 -> result[i] = a1[i] }
    ensures {
      forall i:int. 0 <= i < length a2 -> result[length a1 + i] = a2[i] }

  val sub (a: array 'a) (ofs: int) (len: int) : array 'a
    requires { 0 <= ofs /\ ofs + len <= length a }
    ensures  { length result = len }
    ensures  { forall i:int. 0 <= i < len -> result[i] = a[ofs + i] }

  val copy (a: array 'a) : array 'a
    ensures  { length result = length a }
    ensures  { forall i:int. 0 <= i < length result -> result[i] = a[i] }

  let fill (a: array 'a) (ofs: int) (len: int) (v: 'a)
    requires { 0 <= ofs /\ ofs + len <= length a }
    ensures  { forall i:int.
      (0 <= i < ofs \/ ofs + len <= i < length a) -> a[i] = (old a)[i] }
    ensures  { forall i:int. ofs <= i < ofs + len -> a[i] = v }
  = 'Init:
78
    for k = 0 to len - 1 do
79 80 81
      invariant { forall i:int.
        (0 <= i < ofs \/ ofs + len <= i < length a) -> a[i] = (at a 'Init)[i] }
      invariant { forall i:int. ofs <= i < ofs + k -> a[i] = v }
82 83
      a[ofs + k] <- v
    done
84

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

MARCHE Claude's avatar
MARCHE Claude committed
94
  (*** TODO?
95 96
     - concat : 'a array list -> 'a array
     - to_list
97
     - of_list
98
  *)
99 100 101

end

MARCHE Claude's avatar
MARCHE Claude committed
102 103
(** {2 Sorted Arrays} *)

104 105
module ArraySorted

106
  use import int.Int
107
  use import Array
Andrei Paskevich's avatar
Andrei Paskevich committed
108
  clone import map.MapSorted as M with type elt = int, predicate le = (<=)
109

Andrei Paskevich's avatar
Andrei Paskevich committed
110
  predicate sorted_sub (a : array int) (l u : int) =
111 112
    M.sorted_sub a.elts l u

Andrei Paskevich's avatar
Andrei Paskevich committed
113
  predicate sorted (a : array int) =
114 115 116 117
    M.sorted_sub a.elts 0 a.length

end

MARCHE Claude's avatar
MARCHE Claude committed
118 119
(** {2 Arrays Equality} *)

120 121
module ArrayEq

122
  use import Array
123
  use import map.MapEq as M
124

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

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

end

MARCHE Claude's avatar
MARCHE Claude committed
134 135
(** {2 Permutation} *)

136 137
module ArrayPermut

138
  use import int.Int
139
  use import Array
140 141
  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
  lemma exchange_permut:
152
    forall a1 a2: array 'a, i j: int [exchange a1 a2 i j].
153 154 155 156
    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
  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

165
  use import ArrayEq
166 167 168 169 170 171 172 173 174

  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

175 176
end

MARCHE Claude's avatar
MARCHE Claude committed
177 178
(** {2 Sum of elements} *)

179 180
module ArraySum

181
  use import Array
182 183
  clone import map.MapSum as M

MARCHE Claude's avatar
MARCHE Claude committed
184
  (** [sum a l h] is the sum of [a[i]] for [l <= i < h] *)
Andrei Paskevich's avatar
Andrei Paskevich committed
185
  function sum (a: array int) (l h: int) : int = M.sum a.elts l h
186 187 188

end

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
(** {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_

208
  (* the number of a[i] such that [l <= i < u] and [pr p i a[i]] *)
209 210 211
  function numof (a: array elt) (p: param) (l u: int) : int = num_of (a, p) l u
end

212 213 214 215 216 217 218 219 220 221 222 223 224 225
module NumOf
  use import Array

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

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

  (* the number of a[i] such that [l <= i < u] and [pr i a[i]] *)
  function numof (a: array elt) (l u: int) : int = num_of a l u
end

226 227 228
module TestArray

  use import int.Int
229
  use import Array
230

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
end