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

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

The length is a non-mutable field, so that we get for free that modification of an array does not modify its length

*)
8

9 10 11
module Array

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

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

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

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

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

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

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

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

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

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

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

Andrei Paskevich's avatar
Andrei Paskevich committed
50
  val make (n: int) (v: 'a) :
51
    { n >= 0 }
52
    array 'a
53
    { result = {| length = n; elts = M.const v |} }
MARCHE Claude's avatar
MARCHE Claude committed
54
     (*** { length result = n /\ forall i:int. 0 <= i < n -> result[i] = v} *)
55

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

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

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

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

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

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

end

MARCHE Claude's avatar
MARCHE Claude committed
113 114 115

(** {2 Sorted Arrays} *)

116 117
module ArraySorted

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

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

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

end

MARCHE Claude's avatar
MARCHE Claude committed
130 131
(** {2 Arrays Equality} *)

132 133 134
module ArrayEq

  use import module Array
135
  use import map.MapEq as M
136

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

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

end

MARCHE Claude's avatar
MARCHE Claude committed
146 147
(** {2 Permutation} *)

148 149
module ArrayPermut

150
  use import int.Int
151 152 153
  use import module Array
  clone import map.MapPermut as M

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

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

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

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

169 170 171 172 173 174 175 176
  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

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

187 188
end

MARCHE Claude's avatar
MARCHE Claude committed
189 190
(** {2 Sum of elements} *)

191 192 193 194 195
module ArraySum

  use import module Array
  clone import map.MapSum as M

MARCHE Claude's avatar
MARCHE Claude committed
196
  (** [sum a l h] is the sum of [a[i]] for [l <= i < h] *)
197

Andrei Paskevich's avatar
Andrei Paskevich committed
198
  function sum (a: array int) (l h: int) : int = M.sum a.elts l h
199 200 201

end

202 203 204 205 206
module TestArray

  use import int.Int
  use import module Array

207
  let test_append () =
208 209
    let a1 = make 17 2 in
    assert { a1[3] = 2 };
210
    a1[3] <- 4;
211 212 213 214 215 216 217 218 219
    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 };
    ()

220
  let test_fill () =
221 222
    let a = make 17 True in
    fill a 10 4 False;
223 224 225
    assert { a[9] = True };
    assert { a[10] = False };
    assert { a[14] = True }
226

227 228 229 230 231 232 233 234 235 236
  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 }

237 238
end

MARCHE Claude's avatar
MARCHE Claude committed
239
(***
240
Local Variables:
241
compile-command: "unset LANG; make -C .. modules/array"
242
End:
243
*)