stdlib.mlw 4.17 KB
Newer Older
1

2
(* References *)
3

4
module Ref
5

6
  type ref 'a = {| mutable contents : 'a |}
7 8 9

  parameter ref : v:'a -> {} ref 'a { result=v }

10
  parameter (!) : r:ref 'a -> {} 'a reads r { result=r }
11

12
  parameter (:=) : r:ref 'a -> v:'a -> {} unit writes r { r=v }
13 14 15

end

16 17 18 19 20
module Refint

  use export int.Int
  use export module Ref

21
  parameter incr : r:ref int -> {} unit writes r { r = old r + 1 }
22

23
  parameter decr : r:ref int -> {} unit writes r { r = old r - 1 }
24 25 26

end

27 28
(* Arrays *)

29 30 31
module Array

  use import int.Int
32
  use import map.Map as M
33

34
  type array 'a model {| length : int; mutable elts : map int 'a |}
35

36
  logic ([]) (a: array 'a) (i :int) : 'a = M.([]) a.elts i
37
  logic unsafe_get (a: array 'a) (i :int) : 'a = M.([]) a.elts i
38

39
  parameter ([]) : a:array 'a -> i:int ->
40
    { 0 <= i < length a } 'a reads a { result = a[i] }
41

42
  parameter set : a:array 'a -> i:int -> v:'a ->
43
    { 0 <= i < length a } unit writes a { a.elts = (old a.elts)[i <- v] }
44 45 46 47 48 49

  parameter length : a:array 'a -> {} int { result = a.length }

  parameter make : n:int -> v:'a -> 
    {} 
    array 'a 
50
    { length result = n and forall i:int. 0 <= i < n -> result[i] = v}
51 52 53 54 55 56 57 58 59 60 61 62 63 64

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

  parameter sub : a:array 'a -> ofs:int -> len:int ->
    { 0 <= ofs and ofs + len <= length a }
    array 'a
    { length result = len and
      forall i:int. 0 <= i < len -> result[i] = a[ofs + i] }

65 66 67 68
  parameter copy : a:array 'a ->
    {}
    array 'a
    { length result = length a and
69 70 71 72 73
      forall i:int. 0 <= i < length result -> result[i] = a[i] }

  parameter fill : a:array 'a -> ofs:int -> len:int -> v:'a ->
   { 0 <= ofs and ofs + len <= length a }
   unit
74 75 76 77
   writes a
   { (forall i:int.
       (0 <= i < ofs or ofs + len <= i < length a) -> a[i] = (old a)[i])
     and
78 79 80
     (forall i:int.
       ofs <= i < ofs + len -> a[i] = v) }

81
  parameter blit :
82 83 84 85
   a1:array 'a -> ofs1:int -> a2:array 'a -> ofs2:int -> len:int ->
   { 0 <= ofs1 and ofs1 + len <= length a1 and
     0 <= ofs2 and ofs2 + len <= length a2 }
   unit
86 87 88 89
   writes a2
   { (forall i:int.
       (0 <= i < ofs2 or ofs2 + len <= i < length a2) -> a2[i] = (old a2)[i])
     and
90 91
     (forall i:int.
       ofs2 <= i < ofs2 + len -> a2[i] = a1[ofs1 + i - ofs2]) }
92

93 94 95
  (* TODO?
     - concat : 'a array list -> 'a array
     - to_list
96
     - of_list
97
  *)
98 99 100

end

101 102 103
module ArraySorted

  use import module Array
104
  clone import map.MapSorted as M with type elt = int
105

106
  logic sorted_sub (a : array int) (l u : int) =
107 108
    M.sorted_sub a.elts l u

109
  logic sorted (a : array int) =
110 111 112 113 114 115 116
    M.sorted_sub a.elts 0 a.length

end

module ArrayEq

  use import module Array
117
  use import map.MapEq as M
118 119 120 121

  logic array_eq_sub (a1 a2: array 'a) (l u: int) =
    map_eq_sub a1.elts a2.elts l u

122
  logic array_eq (a1 a2: array 'a) =
123 124 125 126 127 128 129 130 131 132
    a1.length = a2.length and
    array_eq_sub a1 a2 0 a1.length

end

module ArrayPermut

  use import module Array
  clone import map.MapPermut as M

133 134 135
  logic exchange (a1 a2: array 'a) (i j: int) =
    M.exchange a1.elts a2.elts i j

136
  logic permut_sub (a1 a2: array 'a) (l u: int) =
137 138
    M.permut_sub a1.elts a2.elts l u

139
  logic permut (a1 a2: array 'a) =
140 141 142 143
    a1.length = a2.length and M.permut_sub a1.elts a2.elts 0 a1.length

end

144
(***
145 146 147 148 149
module TestArray

  use import int.Int
  use import module Array

150
  let test_append () =
151 152
    let a1 = make 17 2 in
    assert { a1[3] = 2 };
153
    set a1 3 4;
154 155 156 157 158 159 160 161 162
    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 };
    ()

163
  let test_fill () =
164 165 166
    let a = make 17 True in
    fill a 10 4 False;
    assert { a[10] = False }
167

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

178
end
179
***)
180

181
(*
182
Local Variables:
183
compile-command: "unset LANG; make -C .. modules/stdlib"
184
End:
185
*)