Source file Indexing.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
(******************************************************************************)
(*                                                                            *)
(*                                    Fix                                     *)
(*                                                                            *)
(*                       François Pottier, Inria Paris                        *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the GNU Library General Public License version 2, with a         *)
(*  special exception on linking, as described in the file LICENSE.           *)
(*                                                                            *)
(******************************************************************************)

open Printf

(* A suspension is used to represent a cardinal-that-may-still-be-unknown. *)

(* Earlier versions used the definition [type 'n cardinal = int Lazy.t]. We
   now use the stronger definition that follows. This new definition forces
   the equality ['n = unit]. This equality remains invisible outside of this
   module. Inside this module, it lets the type-checker regard all phantom
   indices as equal. This allows [equal] and [assert_equal] to be well-typed;
   with the weaker definition, they would be ill-typed. *)

type 'n cardinal =
  Cardinal : int Lazy.t -> unit cardinal [@@ocaml.unboxed]

(* The function [cardinal] forces the cardinal to become fixed. Up to the
   type annotations, it is equivalent to [Lazy.force]. *)

let cardinal (type n) (Cardinal (lazy n) : n cardinal) =
  n

let is_fixed (type n) (Cardinal c : n cardinal) =
  Lazy.is_val c

type (_, _) eq = Refl : ('a, 'a) eq

let equal (type n m) (n : n cardinal) (m : m cardinal) : (n, m) eq option =
  let Cardinal (lazy n) = n in
  let Cardinal (lazy m) = m in
  if n = m then
    Some Refl
  else
    None

let assert_equal (type n m) (n : n cardinal) (m : m cardinal) : (n, m) eq =
  let Cardinal (lazy n) = n in
  let Cardinal (lazy m) = m in
  if n = m then
    Refl
  else
    sprintf
      "Fix.Indexing.assert_equal: cardinals are not equal (%d <> %d).\n%s\n"
      n m __LOC__
    |> invalid_arg

type 'n index =
  int

module type CARDINAL = sig type n val n : n cardinal end

(* [Empty] and [Const] produce sets whose cardinal is known. *)

module Empty = struct
  type n = unit
  let n = Cardinal (lazy 0)
end

module Const (X : sig val cardinal : int end) : CARDINAL = struct
  type n = unit
  let () = assert (X.cardinal >= 0)
  let n = Cardinal (lazy X.cardinal)
end

let const c : (module CARDINAL) =
  assert (c >= 0);
  (module struct type n = unit let n = Cardinal (lazy c) end)

(* [Gensym] produces a set whose cardinal is a priori unknown. A new reference
   stores the current cardinal, which grows when [fresh()] is invoked. [fresh]
   fails if the suspension [n] has been forced. *)

module Gensym () = struct

  type n = unit
  let counter = ref 0
  let n = Cardinal (lazy !counter)

  let fresh () =
    assert (not (is_fixed n));
    let result = !counter in
    incr counter;
    result

end

type ('l, 'r) either =
  | L of 'l
  | R of 'r

module type SUM = sig
  type l and r
  include CARDINAL
  val inj_l : l index -> n index
  val inj_r : r index -> n index
  val prj : n index -> (l index, r index) either
end

module Sum (L : CARDINAL)(R : CARDINAL) = struct

  type n = unit

  type l = L.n
  type r = R.n

  (* The cardinal [l] of the left-hand set becomes fixed now (if it
     wasn't already). We need it to be fixed for our injections and
     projections to make sense. *)
  let l : int = cardinal L.n
  (* The right-hand set can remain open-ended. *)
  let r : r cardinal = R.n

  let n : n cardinal =
    (* We optimize the case where [r] is fixed already, but the code
       in the [else] branch would work always. *)
    if is_fixed r then
      let n = l + cardinal r in
      Cardinal (lazy n)
    else
      Cardinal (lazy (l + cardinal r))

  (* Injections. The two sets are numbered side by side. *)
  let inj_l x = x
  let inj_r y = l + y

  (* Projection. *)
  let prj x = if x < l then L x else R (x - l)

end

let sum (type l r) (l : l cardinal) (r : r cardinal) =
  let module L = struct type n = l let n = l end in
  let module R = struct type n = r let n = r end in
  (module Sum(L)(R) : SUM with type l = l and type r = r)

module Index = struct

  type 'n t = 'n index

  let of_int (n : 'n cardinal) i : 'n index =
    let n = cardinal n in
    if 0 <= i && i < n then
      i
    else
      sprintf
        "Fix.Indexing.Index.of_int: \
         the index %d is not in the range [0, %d).\n%s\n"
        i n __LOC__
      |> invalid_arg

  let[@inline] to_int i = i

  let[@inline] iter (n : 'n cardinal) (yield : 'n index -> unit) =
    let n = cardinal n in
    for i = 0 to n - 1 do
      yield i
    done

  let[@inline] rev_iter (n : 'n cardinal) (yield : 'n index -> unit) =
    let n = cardinal n in
    for i = n - 1 downto 0 do
      yield i
    done

  exception End_of_set

  let enumerate (n : 'n cardinal) : unit -> 'n index =
    let n = cardinal n in
    let next = ref 0 in
    fun () ->
      let i = !next in
      if n <= i then raise End_of_set;
      incr next;
      i

end

type ('n, 'a) vector =
  Vector : 'a array -> (unit, 'a) vector [@@ocaml.unboxed]

module Vector = struct

  type ('n, 'a) t = ('n, 'a) vector

  let[@inline] as_array (type n) (Vector a : (n, _) t) = a

  let[@inline] get (type n) (Vector a : (n, _) t) i =
    Array.unsafe_get a i

  let[@inline] set (type n) (Vector a : (n, _) t) i x =
    Array.unsafe_set a i x

  let[@inline] set_cons t i x =
    set t i (x :: get t i)

  let length (type n) (Vector a : (n, _) t) : n cardinal =
    let n = Array.length a in
    Cardinal (lazy n)

  let empty : (Empty.n, _) t = Vector [||]

  let make (type n) (Cardinal n : n cardinal) x : (n, _) t =
    Vector (Array.make (Lazy.force n) x)

  let make' (type n) (Cardinal n : n cardinal) f : (n, _) t=
    match Lazy.force n with
    | 0 -> empty
    | n -> Vector (Array.make n (f()))

  let init (type n) (Cardinal n : n cardinal) f : (n, _) t=
    Vector (Array.init (Lazy.force n) f)

  let map (type n) f (Vector a : (n, _) t) : (n, _) t =
    Vector (Array.map f a)

  let mapi (type n) f (Vector a : (n, _) t) : (n, _) t =
    Vector (Array.mapi f a)

  let copy (type n) (Vector a : (n, _) t) : (n, _) t =
    Vector (Array.copy a)

  let iter (type n) f (Vector a : (n, _) t) =
    Array.iter f a

  let iteri (type n) f (Vector a : (n, _) t) =
    Array.iteri f a

  let iter2 (type n) f (Vector a : (n, _) t) (Vector b : (n, _) t) =
    Array.iter2 f a b

  let fold_left (type n) f acc (Vector a : (n, _) t) =
    Array.fold_left f acc a

  let fold_right (type n) f (Vector a : (n, _) t) acc =
    Array.fold_right f a acc

  let fold_left2 (type n) f acc (Vector a : (n, _) t) (Vector b : (n, _) t) =
    let acc = ref acc in
    for i = 0 to Array.length a - 1 do
      acc := f !acc (Array.unsafe_get a i) (Array.unsafe_get b i)
    done;
    !acc

  let fold_right2 (type n) f (Vector a : (n, _) t) (Vector b : (n, _) t) acc =
    let acc = ref acc in
    for i = Array.length a - 1 downto 0 do
      acc := f (Array.unsafe_get a i) (Array.unsafe_get b i) !acc
    done;
    !acc

  let to_list (type n) (Vector a : (n, _) t) =
    Array.to_list a

  let sort (type n) cmp (Vector a : (n, _) t) =
    Array.sort cmp a

  let of_array (type n) (Cardinal n : n cardinal) a : (n, _) t =
    let n = Lazy.force n
    and m = Array.length a in
    if n = m then
      Vector a
    else
      sprintf
        "Fix.Indexing.Vector.of_array: \
         cardinal and array length do not match (%d <> %d).\n%s\n"
        n m __LOC__
      |> invalid_arg

  let invert (type m) (type n) (n : n cardinal) (v : (m, n index) t) : (n, m index option) t =
    (* We assume that [v] represents an injection of [m] into [n].
       We want to tabulate the inverse function [w], a partial function. *)
    let w = make n None in
    v |> iteri @@ begin fun i j ->
      assert (get w j = None);
      set w j (Some i);
    end;
    w

  module type V = sig
    type n
    type a
    val vector : (n, a) t
  end

  module Of_array (A : sig type a val array : a array end) = struct
    type n = unit
    type a = A.a
    let vector = Vector A.array
  end

end