types.ml 76.7 KB
Newer Older
1
open Ident
2
open Encodings
3

4
let count = ref 0
5
6
7
8
9
		
let () =
  Stats.register Stats.Summary
    (fun ppf -> Format.fprintf ppf "Allocated type nodes:%i@\n" !count)

Jérôme Maloberti's avatar
Jérôme Maloberti committed
10
(* 
11
12
13
14
15
16
17
18
To be sure not to use generic comparison ...
*)
let (=) : int -> int -> bool = (==)
let (<) : int -> int -> bool = (<)
let (<=) : int -> int -> bool = (<=)
let (<>) : int -> int -> bool = (<>)
let compare = 1

19
type const = 
20
  | Integer of Intervals.V.t
21
  | Atom of Atoms.V.t
22
  | Char of Chars.V.t
23
24
25
26
  | Pair of const * const
  | Xml of const * const
  | Record of const label_map
  | String of U.uindex * U.uindex * U.t * const
27

Jérôme Maloberti's avatar
Jérôme Maloberti committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
type service_params =
  | TProd of service_params * service_params
  | TOption of service_params 
  | TList of string * service_params
  | TSet of service_params
  | TSum of service_params * service_params
  | TString of string
  | TInt of string
  | TInt32 of string
  | TInt64 of string
  | TFloat of string 
  | TBool of string
  | TFile of string
      (* | TUserType of string * (string -> 'a) * ('a -> string) *)
  | TCoord of string 
  | TCoordv of service_params * string
  | TESuffix of string 
  | TESuffixs of string
      (*  | TESuffixu of (string * (string -> 'a) * ('a -> string)) *)
  | TSuffix of (bool * service_params)
  | TUnit 
  | TAny
  | TConst of string;;

52
53
54
module Const = struct
  type t = const

55
56
  let check _ = ()
  let dump ppf _ = Format.fprintf ppf "<Types.Const.t>"
57
58

  let rec compare c1 c2 = match (c1,c2) with
59
    | Integer x, Integer y -> Intervals.V.compare x y
60
61
    | Integer _, _ -> -1
    | _, Integer _ -> 1
62
    | Atom x, Atom y -> Atoms.V.compare x y
63
64
    | Atom _, _ -> -1
    | _, Atom _ -> 1
65

66
    | Char x, Char y -> Chars.V.compare x y
67
68
69
    | Char _, _ -> -1
    | _, Char _ -> 1
    | Pair (x1,x2), Pair (y1,y2) ->
70
71
	let c = compare x1 y1 in
	if c <> 0 then c else compare x2 y2
72
73
74
    | Pair (_,_), _ -> -1
    | _, Pair (_,_) -> 1
    | Xml (x1,x2), Xml (y1,y2) ->
75
76
	let c = compare x1 y1 in
	if c <> 0 then c else compare x2 y2
77
78
79
    | Xml (_,_), _ -> -1
    | _, Xml (_,_) -> 1
    | Record x, Record y ->
80
	LabelMap.compare compare x y
81
82
83
84
85
86
87
    | Record _, _ -> -1
    | _, Record _ -> 1
    | String (i1,j1,s1,r1), String (i2,j2,s2,r2) ->
	let c = Pervasives.compare i1 i2 in if c <> 0 then c 
	else let c = Pervasives.compare j1 j2 in if c <> 0 then c
	else let c = U.compare s1 s2 in if c <> 0 then c (* Should compare
							    only the substring *)
88
89
90
91
92
93
94
95
96
97
	else compare r1 r2

  let rec hash = function
    | Integer x -> 1 + 17 * (Intervals.V.hash x)
    | Atom x -> 2 + 17 * (Atoms.V.hash x)
    | Char x -> 3 + 17 * (Chars.V.hash x)
    | Pair (x,y) -> 4 + 17 * (hash x) + 257 * (hash y)
    | Xml (x,y) -> 5 + 17 * (hash x) + 257 * (hash y)
    | Record x -> 6 + 17 * (LabelMap.hash hash x)
    | String (i,j,s,r) -> 7 + 17 * (U.hash s) + 257 * hash r
98
      (* Note: improve hash for String *)
99

100
101
  let equal c1 c2 = compare c1 c2 = 0
end
102

103
104
module Abstract =
struct
105
  module T = Custom.String
106
107
108
109
110
111
112
113
114
115
  type abs = T.t

  module V =
  struct
    type t = abs * Obj.t
  end

  include SortedList.FiniteCofinite(T)

  let print = function
116
    | Finite l -> List.map (fun x ppf -> Format.fprintf ppf "!%s" x) l
117
118
119
120
121
    | Cofinite l ->       
	[ fun ppf ->
	  Format.fprintf ppf "@[Abstract";
	  List.iter (fun x -> Format.fprintf ppf " \\@ !%s" x) l;
	  Format.fprintf ppf "@]" ]
122

123
124
125
126
127
  let contains_sample s t = match s,t with
    | None, Cofinite _ -> true
    | None, Finite _ -> false
    | Some s, t -> contains s t
    
128
129
end

130
131
type pair_kind = [ `Normal | `XML ]

132
module BoolAtoms : BoolVar.S with 
133
  type s = Atoms.t = BoolVar.Make(Atoms)
134
module BoolIntervals : BoolVar.S with 
135
  type s = Intervals.t = BoolVar.Make(Intervals)
136
module BoolChars : BoolVar.S with 
137
  type s = Chars.t = BoolVar.Make(Chars)
138

139
140
type tlvs =  { s : Var.Set.t ; b : bool }

141
142
module rec Descr : 
sig
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  (* each kind is represented as a union of itersection of types 
   * the type is a union of all kinds
   *
   * we add a new field that contains only variables.
   * Inv : 
     * if the bdd of ANY kind is composed only of variables,
       the we move it in vars:
     * From a bdd we move all variables to vars: that belong to
     * to a path in the bdd that contains only variables and end in
     * true
     * A bdd never contains a path that ends in 1 and contains only variables
     *
     * (t1 v a ) ^ ( t2 v b )
     * we need to distribute variables for the intersection
     * (t1 ^ t2) v (t1 ^ b) v (t2 ^ a) v (a ^ b)
     * before we were doing only t1 ^ t2
   *)
160
  type s = {
161
162
163
    atoms : BoolAtoms.t;
    ints  : BoolIntervals.t;
    chars : BoolChars.t;
164
165
166
167
    times : BoolPair.t;
    xml   : BoolPair.t;
    arrow : BoolPair.t;
    record: BoolRec.t;
168
    abstract: Abstract.t;
169
170
171
    (* this is used in record to flag the fact that the type of a label is
     * absent . It is used for optional arguments in functions as ?Int
     * is the union of Int ^ undef where undef is a type with absent : true *)
172
173
174
175
    absent: bool;
    (* maintains the list of all toplevel type variables in s
     * and a flag that is true if s contains only variables, false otherwise *)
    toplvars : tlvs
176
  }
177
  include Custom.T with type t = s
178
  val empty: t
179
180
end =
struct
181
  type s = {
182
183
184
    atoms : BoolAtoms.t;
    ints  : BoolIntervals.t;
    chars : BoolChars.t;
185
186
187
188
    times : BoolPair.t;
    xml   : BoolPair.t;
    arrow : BoolPair.t;
    record: BoolRec.t;
189
    abstract: Abstract.t;
190
191
    absent: bool;
    toplvars : tlvs
192
  }
193
  type t = s
194

195
196
197
198
  let print_lst ppf =
    List.iter (fun f -> f ppf; Format.fprintf ppf " |")

  let dump ppf d =
199
    Format.fprintf ppf "<types atoms(%a) ints(%a) chars(%a) times(%a) record(%a) xml(%a)>"
200
      BoolAtoms.dump d.atoms
201
202
      BoolIntervals.dump d.ints
      BoolChars.dump d.chars
203
204
205
      BoolPair.dump d.times
      BoolRec.dump d.record
      BoolPair.dump d.xml
206

207
208
209
210
211
  let empty = { 
    times = BoolPair.empty; 
    xml   = BoolPair.empty; 
    arrow = BoolPair.empty; 
    record= BoolRec.empty;
212
213
214
    ints  = BoolIntervals.empty;
    atoms = BoolAtoms.empty;
    chars = BoolChars.empty;
215
    abstract = Abstract.empty;
216
    absent= false;
217
    toplvars = { s = Var.Set.empty ; b = true }
218
219
  }

220
  let equal a b =
221
    (a == b) || (
222
223
224
      (BoolAtoms.equal a.atoms b.atoms) &&
      (BoolChars.equal a.chars b.chars) &&
      (BoolIntervals.equal a.ints  b.ints) &&
225
226
227
228
      (BoolPair.equal a.times b.times) &&
      (BoolPair.equal a.xml b.xml) &&
      (BoolPair.equal a.arrow b.arrow) &&
      (BoolRec.equal a.record b.record) &&
229
      (Abstract.equal a.abstract b.abstract) &&
230
231
      (a.absent == b.absent)
    )
232
233
234

  let compare a b =
    if a == b then 0 
235
236
237
    else let c = BoolAtoms.compare a.atoms b.atoms in if c <> 0 then c
    else let c = BoolChars.compare a.chars b.chars in if c <> 0 then c
    else let c = BoolIntervals.compare a.ints b.ints in if c <> 0 then c
238
239
240
241
    else let c = BoolPair.compare a.times b.times in if c <> 0 then c
    else let c = BoolPair.compare a.xml b.xml in if c <> 0 then c
    else let c = BoolPair.compare a.arrow b.arrow in if c <> 0 then c
    else let c = BoolRec.compare a.record b.record in if c <> 0 then c
242
    else let c = Abstract.compare a.abstract b.abstract in if c <> 0 then c
243
244
245
    else if a.absent && not b.absent then -1
    else if b.absent && not a.absent then 1
    else 0
246
      
247
  let hash a =
248
249
250
    let accu = BoolChars.hash a.chars in
    let accu = 17 * accu + BoolIntervals.hash a.ints in
    let accu = 17 * accu + BoolAtoms.hash a.atoms in
251
252
253
254
255
256
257
    let accu = 17 * accu + BoolPair.hash a.times in
    let accu = 17 * accu + BoolPair.hash a.xml in
    let accu = 17 * accu + BoolPair.hash a.arrow in
    let accu = 17 * accu + BoolRec.hash a.record in
    let accu = 17 * accu + Abstract.hash a.abstract in
    let accu = if a.absent then accu+5 else accu in
    accu
258

259
  let check a =
260
261
262
    BoolChars.check a.chars;
    BoolIntervals.check a.ints;
    BoolAtoms.check a.atoms;
263
264
265
266
    BoolPair.check a.times;
    BoolPair.check a.xml;
    BoolPair.check a.arrow;
    BoolRec.check a.record;
267
    Abstract.check a.abstract;
268
269
    ()

270
271
272
end
and Node :
sig
273
  type t = { id : int; cu: Compunit.t; mutable descr : Descr.t }
274
275
276
277
278
  val dump: Format.formatter -> t -> unit
  val check: t -> unit
  val equal: t -> t -> bool
  val hash: t -> int
  val compare:t -> t -> int
279
  val mk: int -> Descr.t -> t
280
end =
281

282
struct
283
  type t = { id : int; cu: Compunit.t; mutable descr : Descr.t }
284
  let check n = ()
285
  let dump ppf n = Format.fprintf ppf "X%i" n.id
286
  let hash x = x.id + Compunit.hash x.cu
287
  let compare x y = 
288
289
290
    let c = x.id - y.id in if c = 0 then Compunit.compare x.cu y.cu else c
  let equal x y = x==y || (x.id == y.id && (Compunit.equal x.cu y.cu))
  let mk id d = { id = id; cu = Compunit.current (); descr = d }
291
292
end

293
294
and Pair : Bool.S with type elem = (Node.t * Node.t) =
  Bool.Make(Custom.Pair(Node)(Node))
295
296
and BoolPair : BoolVar.S with 
  type s = Pair.t = BoolVar.Make(Pair)
297

298
299
300
301
302
(* bool = true means that the record is open that is, that
 * the labels that are not in the domain of the map are
 * equal to "any" *)
and Rec : Bool.S with type elem = bool * Node.t Ident.label_map =
  Bool.Make(Custom.Pair(Custom.Bool)(LabelSet.MakeMap(Node)))
303
304
and BoolRec : BoolVar.S with
  type s = Rec.t = BoolVar.Make(Rec)
305

306
307
module DescrHash = Hashtbl.Make(Descr)
module DescrMap = Map.Make(Descr)
308
309
module DescrSet = Set.Make(Descr)
module DescrSList = SortedList.Make(Descr)
310

311
312
313
type descr = Descr.t
type node = Node.t
include Descr
314

315
316
let forward_print = ref (fun _ _ -> assert false)

317
318
319
320
let make () = 
  incr count; 
  Node.mk !count empty

321
322
323
324
325
326
327
let define n d = 
  n.Node.descr <- d

let cons d = 
  incr count; 
  Node.mk !count d

328
329
330
(* two representation possible. either all fields (except vars) are full, OR
 * the field vars is full.
 *)
331
let any =  {
332
333
334
  times = BoolPair.full; 
  xml   = BoolPair.full; 
  arrow = BoolPair.full; 
335
  record= BoolRec.full; 
336
337
338
  ints  = BoolIntervals.full;
  atoms = BoolAtoms.full;
  chars = BoolChars.full;
339
  abstract = Abstract.any;
340
  absent= false;
341
  toplvars = { s = Var.Set.empty ; b = true }
342
}
343
344

let non_constructed =
345
346
  { any with  
      times = empty.times; xml = empty.xml; record = empty.record }
347
     
348
let non_constructed_or_absent = 
349
  { non_constructed with absent = true }
350
351

(* Descr.t type constructors *)	     
352
let times x y = { empty with times = BoolPair.atom (`Atm (Pair.atom (x,y))); toplvars = {empty.toplvars with b = false } }
353
354
let xml x y = { empty with xml = BoolPair.atom (`Atm (Pair.atom (x,y))) }
let arrow x y = { empty with arrow = BoolPair.atom (`Atm (Pair.atom (x,y))) }
355
let record label t = 
356
  { empty with 
357
      record = BoolRec.atom (`Atm (Rec.atom (true,LabelMap.singleton label t))) }
358
let record_fields x =
359
  { empty with record = BoolRec.atom (`Atm (Rec.atom x)) }
360
let atom a = { empty with atoms = BoolAtoms.atom (`Atm a) }
361
let var a =  {
362
  (* Atm = Any ^ a *)
363
364
365
366
367
368
369
  times = BoolPair.vars a;
  xml   = BoolPair.vars a; 
  arrow = BoolPair.vars a; 
  record= BoolRec.vars a; 
  ints  = BoolIntervals.vars a;
  atoms = BoolAtoms.vars a;
  chars = BoolChars.vars a;
370
371
  abstract = Abstract.any;
  absent= false;
372
  toplvars = { s = Var.Set.singleton a; b = true }
373
374
}

375
376
let char c = { empty with chars = BoolChars.atom (`Atm c) }
let interval i = { empty with ints = BoolIntervals.atom (`Atm i) }
377
let abstract a = { empty with abstract = a }
378
379

let get_abstract t = t.abstract
380
381
382
383

(* union is component wise + a pass to extract variables
 * Var = ( t1.Var v t2.Var ) v ( extracted variables )
 *)
384
385
let cup x y = 
  if x == y then x else {
386
387
388
    times = BoolPair.cup x.times y.times;
    xml   = BoolPair.cup x.xml y.xml;
    arrow = BoolPair.cup x.arrow y.arrow;
389
    record= BoolRec.cup x.record y.record;
390
391
392
    ints  = BoolIntervals.cup x.ints  y.ints;
    atoms = BoolAtoms.cup x.atoms y.atoms;
    chars = BoolChars.cup x.chars y.chars;
393
    abstract = Abstract.cup x.abstract y.abstract;
394
    absent= x.absent || y.absent;
395
    toplvars = { s = Var.Set.union x.toplvars.s y.toplvars.s; b = x.toplvars.b && y.toplvars.b }
396
  }
397
398
399

(* intersection 
 *)
400
401
let cap x y = 
  if x == y then x else {
402
    ints  = BoolIntervals.cap x.ints y.ints;
403
404
    times = BoolPair.cap x.times y.times;
    xml   = BoolPair.cap x.xml y.xml;
405
    record= BoolRec.cap x.record y.record;
406
    arrow = BoolPair.cap x.arrow y.arrow;
407
408
    atoms = BoolAtoms.cap x.atoms y.atoms;
    chars = BoolChars.cap x.chars y.chars;
409
    abstract = Abstract.cap x.abstract y.abstract;
410
    absent= x.absent && y.absent;
411
    toplvars = { s = Var.Set.inter x.toplvars.s y.toplvars.s; b = x.toplvars.b && y.toplvars.b }
412
  }
413
414
415
416
417
418
419
420
421

(* 
 *
 * t1 // t2 = ( t1.Atm ^ - t2.Atm ^ t2.Var ) v
 *            ( t1.Var ^ t2.Var )
 *
 * Atm t1.Atm // ( t2.Atm v t2.Var )
 * Var ( t1.Var // t2.Var ) v ( extracted variables )
 *)    
422
423
let diff x y = 
  if x == y then empty else {
424
425
426
    times = BoolPair.diff x.times y.times;
    xml   = BoolPair.diff x.xml y.xml;
    arrow = BoolPair.diff x.arrow y.arrow;
427
    record= BoolRec.diff x.record y.record;
428
    ints  = BoolIntervals.diff x.ints y.ints;
429
430
    atoms = BoolAtoms.diff x.atoms y.atoms;
    chars = BoolChars.diff x.chars y.chars;
431
    abstract = Abstract.diff x.abstract y.abstract;
432
    absent= x.absent && not y.absent;
433
    toplvars = { s = Var.Set.diff x.toplvars.s y.toplvars.s; b = x.toplvars.b && y.toplvars.b }
434
435
  }
    
436
437
(* TODO: optimize disjoint check for boolean combinations *)
let trivially_disjoint a b =
438
439
440
  (BoolChars.trivially_disjoint a.chars b.chars) &&
  (BoolIntervals.trivially_disjoint a.ints b.ints) &&
  (BoolAtoms.trivially_disjoint a.atoms b.atoms) &&
441
442
443
  (BoolPair.trivially_disjoint a.times b.times) &&
  (BoolPair.trivially_disjoint a.xml b.xml) &&
  (BoolPair.trivially_disjoint a.arrow b.arrow) &&
444
  (BoolRec.trivially_disjoint a.record b.record) &&
445
  (Abstract.disjoint a.abstract b.abstract) &&
446
  (not (a.absent && b.absent))
447

448
let descr n = n.Node.descr
449
let internalize n = n
450
let id n = n.Node.id
451

452
let rec constant = function
453
454
455
  | Integer i -> interval (Intervals.atom i)
  | Atom a -> atom (Atoms.atom a)
  | Char c -> char (Chars.atom c)
456
  | Pair (x,y) -> times (const_node x) (const_node y)
457
  | Xml (x,y) -> xml (const_node x) (const_node y)
458
  | Record x -> record_fields (false ,LabelMap.map const_node x)
459
460
461
462
463
464
  | String (i,j,s,c) ->
      if U.equal_index i j then constant c
      else 
	let (ch,i') = U.next s i in
	constant (Pair (Char (Chars.V.mk_int ch), String (i',j,s,c)))
and const_node c = cons (constant c)
465

466
467
let neg x = diff any x

468
let any_node = cons any
469
let empty_node = cons empty
470

471
module LabelS = Set.Make(Label)
472

473
474
let any_or_absent = { any with absent = true } 
let only_absent = { empty with absent = true }
475

476
477
let get_record r =
  let labs accu (_,r) = 
478
479
    List.fold_left 
      (fun accu (l,_) -> LabelS.add l accu) accu (LabelMap.get r) in
480
  let extend descrs labs (o,r) =
481
482
483
484
485
    let rec aux i labs r =
      match labs with
	| [] -> ()
	| l1::labs ->
	    match r with
486
	      | (l2,x)::r when l1 == l2 -> 
487
488
489
		  descrs.(i) <- cap descrs.(i) (descr x);
		  aux (i+1) labs r
	      | r ->
490
491
		  if not o then 
		    descrs.(i) <- cap descrs.(i) only_absent; (* TODO:OPT *)
492
493
		  aux (i+1) labs r
    in
494
    aux 0 labs (LabelMap.get r);
495
496
497
498
    o
  in
  let line (p,n) =
    let labels = 
499
500
      List.fold_left labs (List.fold_left labs LabelS.empty p) n in
    let labels = LabelS.elements labels in
501
    let nlab = List.length labels in
502
    let mk () = Array.create nlab any_or_absent in
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517

    let pos = mk () in
    let opos = List.fold_left 
		 (fun accu x -> 
		    (extend pos labels x) && accu)
		 true p in
    let p = (opos, pos) in

    let n = List.map (fun x ->
			let neg = mk () in
			let o = extend neg labels x in
			(o,neg)
		     ) n in
    (labels,p,n)
  in
518
  List.map line (Rec.get r)
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566

(* substitute all occurrences of v in t by s *)
let rec subst v (t,s) =
  let module C ( X : BoolVar.S ) = 
    struct
      let atom_aux ?f v (s,ss) = 
        let open X in function
          |`Var z when (Pervasives.compare (`Var z) v) = 0 -> ss
          |`Var z -> vars (`Var z)
          |`Atm constr ->
              begin match f with
              |None -> atom (`Atm constr)
              |Some f -> f constr v (s,ss)
              end
          |_ -> assert false

      let subst ?f v s bdd = 
        let open X in
        let atom z = atom_aux ?f v s z in
        compute ~empty ~full ~cup ~cap ~diff ~atom bdd
    end
  in
  let subtimes t v (s,_) =
    List.fold_left (fun acc (left,rigth) ->
      let deep_subst l = 
        List.fold_left (fun acc (t1,t2) -> 
          let d1 = cons (subst v (descr t1,s)) in 
          let d2 = cons (subst v (descr t2,s)) in
          BoolPair.cap acc (BoolPair.atom (`Atm (Pair.atom (d1,d2))))
        ) BoolPair.full l
      in
      let acc1 = BoolPair.diff (deep_subst left) (deep_subst rigth) in
      BoolPair.cup acc acc1
    ) BoolPair.empty (Pair.get t)
  in
  {
    atoms = (let module M = C(BoolAtoms) in M.subst) v (s,s.atoms) t.atoms;
    ints  = (let module M = C(BoolIntervals) in M.subst) v (s,s.ints) t.ints;
    chars = (let module M = C(BoolChars) in M.subst) v (s,s.chars) t.chars;
    times = (let module M = C(BoolPair) in M.subst) ~f:subtimes v (s,s.times) t.times;
    xml   = (let module M = C(BoolPair) in M.subst) ~f:subtimes v (s,s.xml) t.xml;
    record= (let module M = C(BoolRec) in M.subst) v (s,s.record) t.record;
    arrow = (let module M = C(BoolPair) in M.subst) ~f:subtimes v (s,s.arrow) t.arrow;
    abstract = t.abstract;
    absent= t.absent;
    toplvars = { t.toplvars with s = Var.Set.remove v t.toplvars.s }
  }

567
568
569
570
571
(* Subtyping algorithm *)

let diff_t d t = diff d (descr t)
let cap_t d t = cap d (descr t)
let cup_t d t = cup d (descr t)
572
let cap_product any_left any_right l =
573
574
  List.fold_left 
    (fun (d1,d2) (t1,t2) -> (cap_t d1 t1, cap_t d2 t2))
575
    (any_left,any_right)
576
    l
577
let any_pair = { empty with times = any.times }
578

579
580
581
let rec exists max f =
  (max > 0) && (f (max - 1) || exists (max - 1) f)

582
exception NotEmpty
583

584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
module Witness = struct

  module NodeSet = Set.Make(Node)

  type witness =
    | WInt of Intervals.V.t
    | WAtom of Atoms.sample
    | WChar of Chars.V.t
    | WAbsent
    | WAbstract of Abstract.elem option

    | WPair of witness * witness * witness_slot
    | WXml of witness * witness * witness_slot
    | WRecord of witness label_map * bool * witness_slot
	(* Invariant: WAbsent cannot actually appear *)

    | WFun of (witness * witness option) list * witness_slot
  and witness_slot = 
      { mutable wnodes_in: NodeSet.t;
	mutable wnodes_out: NodeSet.t;
	mutable wuid: int }

  module WHash = Hashtbl.Make(
    struct
      type t = witness
      let hash_small = function
	| WInt i -> 17 * Intervals.V.hash i
	| WChar c -> 1 + 17 * Chars.V.hash c
	| WAtom None -> 2
	| WAtom (Some (ns,None)) -> 3 + 17 * Ns.Uri.hash ns
	| WAtom (Some (_,Some t)) -> 4 + 17 * Ns.Label.hash t
	| WAbsent -> 5
	| WAbstract None -> 6
	| WAbstract (Some t) -> 7 + 17 * Abstract.T.hash t
	| WPair (_,_,s) 
	| WXml (_,_,s)
	| WRecord (_,_,s)
	| WFun (_,s) -> 8 + 17 * s.wuid
      let hash = function
	| WPair (p1,p2,_) -> 257 * hash_small p1 + 65537 * hash_small p2
	| WXml (p1,p2,_) -> 1 + 257 * hash_small p1 + 65537 * hash_small p2
	| WRecord (r,o,_) -> 
	    (if o then 2 else 3) + 257 * LabelMap.hash hash_small r
	| WFun (f,_) ->
	    4 + 257 *
	      (Hashtbl.hash 
		 (List.map 
		    (function (x,None) -> 17 * hash_small x
		       | (x,Some y) -> 
			   1 + 17 * hash_small x + 257 * hash_small y)
		    f)
	      )
	| _ -> assert false

      let equal_small w1 w2 = match w1,w2 with
	| WInt i1, WInt i2 -> Intervals.V.equal i1 i2
	| WChar c1, WChar c2 -> Chars.V.equal c1 c2
	| WAtom None, WAtom None -> true
	| WAtom (Some (ns1,None)), WAtom (Some (ns2,None)) ->
	    Ns.Uri.equal ns1 ns2
	| WAtom (Some (_,Some t1)), WAtom (Some (_,Some t2)) ->
	    Ns.Label.equal t1 t2
	| WAbsent, WAbsent -> true
	| WAbstract None, WAbstract None -> false
	| WAbstract (Some t1), WAbstract (Some t2) -> Abstract.T.equal t1 t2
	| _ -> w1 == w2

      let equal w1 w2 = match w1,w2 with
	| WPair (p1,q1,_), WPair (p2,q2,_) 
653
	| WXml (p1,q1,_), WXml (p2,q2,_) -> 
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
	    equal_small p1 p2 && equal_small q1 q2
	| WRecord (r1,o1,_), WRecord (r2,o2,_) ->
	    o1 == o2 && (LabelMap.equal equal_small r1 r2)
	| WFun (f1,_), WFun (f2,_) ->
	    List.length f1 = List.length f2 &&
		List.for_all2
		(fun (x1,y1) (x2,y2) ->
		   equal_small x1 x2 && (match y1,y2 with
					   | Some y1, Some y2 -> 
					       equal_small y1 y2
					   | None, None -> true
					   | _ -> false)
		) f1 f2
	| _ -> false
    end)

  let wmemo = WHash.create 1024
  let wuid = ref 0
  let wslot () = { wuid = !wuid; wnodes_in = NodeSet.empty; 
		   wnodes_out = NodeSet.empty }

675
676
677
678
  let () =
    Stats.register Stats.Summary
      (fun ppf -> Format.fprintf ppf "Allocated witnesses:%i@\n" !wuid)

679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
  let rec print_witness ppf = function
    | WInt i ->
	Format.fprintf ppf "%a" Intervals.V.print i
    | WChar c ->
	Format.fprintf ppf "%a" Chars.V.print c
    | WAtom None ->
	Format.fprintf ppf "`#:#"
    | WAtom (Some (ns,None)) ->
	Format.fprintf ppf "`%a" Ns.InternalPrinter.print_any_ns ns
    | WAtom (Some (_,Some t)) ->
	Format.fprintf ppf "`%a" Ns.Label.print_attr t
    | WPair (w1,w2,_) -> 
	Format.fprintf ppf "(%a,%a)" print_witness w1 print_witness w2
    | WXml (w1,w2,_) -> 
	Format.fprintf ppf "XML(%a,%a)" print_witness w1 print_witness w2
    | WRecord (ws,o,_) ->
	Format.fprintf ppf "{";
	LabelMap.iteri
	  (fun l w -> Format.fprintf ppf " %a=%a" 
	     Label.print_attr l print_witness w)
	  ws;
	if o then Format.fprintf ppf " ..";
	Format.fprintf ppf " }"
    | WFun (f,_) ->
	Format.fprintf ppf "FUN{";
	List.iter (fun (x,y) ->
		     Format.fprintf ppf " %a->" print_witness x;
		     match y with
		       | None -> Format.fprintf ppf "#"
		       | Some y -> print_witness ppf y) f;
	Format.fprintf ppf " }"
    | WAbstract None ->
	Format.fprintf ppf "Abstract(..)"
    | WAbstract (Some s) ->
	Format.fprintf ppf "Abstract(%s)" s
    | WAbsent ->
	Format.fprintf ppf "Absent"
	  
717
718
719
720
721
722
723
724
725
726
727
728
729
730
  let wmk w =  (* incr wuid; w *)  (* hash-consing disabled *)
    try WHash.find wmemo w
    with Not_found -> 
      incr wuid; 
      WHash.add wmemo w w;
(*      Format.fprintf Format.std_formatter "W:%a@." 
	print_witness w; *)
      w

  let wpair p1 p2 = wmk (WPair (p1,p2, wslot()))
  let wxml p1 p2 = wmk (WXml (p1,p2, wslot()))
  let wrecord r o = wmk (WRecord (r,o, wslot()))
  let wfun f = wmk (WFun (f, wslot()))

731
  let bool_pair f =
732
    Pair.compute 
733
734
735
736
737
      ~empty:false ~full:true 
      ~cup:(||) ~cap:(&&) ~diff:(fun x y -> x && not y) 
      ~atom:f

  let bool_rec f =
738
    Rec.compute 
739
740
741
742
743
744
745
746
747
748
749
750
751
752
      ~empty:false ~full:true 
      ~cup:(||) ~cap:(&&) ~diff:(fun x y -> x && not y) 
      ~atom:f

  let rec node_has n = function
    | WXml (_,_,s) | WPair (_,_,s) | WFun (_,s) | WRecord (_,_,s) as w ->
	if NodeSet.mem n s.wnodes_in then true
	else if NodeSet.mem n s.wnodes_out then false
	else (let r = type_has (descr n) w in
	      if r then s.wnodes_in <- NodeSet.add n s.wnodes_in
	      else s.wnodes_out <- NodeSet.add n s.wnodes_out;
	      r)
    | w -> type_has (descr n) w

753
754
  (* type_has checks if a witness is contained in the union of
   * the leafs of a bdd, ignoring all variables. *)
755
  and type_has t = function
756
757
758
    | WInt i -> Intervals.contains i (BoolIntervals.leafconj t.ints)
    | WChar c -> Chars.contains c (BoolChars.leafconj t.chars)
    | WAtom a -> Atoms.contains_sample a (BoolAtoms.leafconj t.atoms)
759
760
761
    | WPair (w1,w2,_) -> 
	bool_pair 
	  (fun (n1,n2) -> node_has n1 w1 && node_has n2 w2) 
762
	  (BoolPair.leafconj t.times)
763
764
765
    | WXml (w1,w2,_) ->
	bool_pair 
	  (fun (n1,n2) -> node_has n1 w1 && node_has n2 w2)
766
	  (BoolPair.leafconj t.xml)
767
768
769
770
771
772
773
774
775
    | WFun (f,_) ->
	bool_pair 
	  (fun (n1,n2) ->
	     List.for_all
	       (fun (x,y) ->
		  not (node_has n1 x) ||
		    (match y with None -> false
		       | Some y -> node_has n2 y))
	       f) 
776
	  (BoolPair.leafconj t.arrow)
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
    | WRecord (f,o,_) ->
	bool_rec 
	  (fun (o',f') ->
	     ((not o) || o') && (
	       let checked = ref 0 in
	       try 
		 LabelMap.iteri 
		   (fun l n ->
		      let w = 
			try let w = LabelMap.assoc l f in incr checked; w
			with Not_found -> WAbsent in
		      if not (node_has n w) then raise Exit
		   ) f'; 
		 o' || (LabelMap.length f == !checked)
		   (* All the remaining fields cannot be WAbsent
		      because of an invariant. Otherwise, we must
		      check that all are WAbsent here. *)
	       with Exit -> false))
795
	  (BoolRec.leafconj t.record)
796
797
798
799
    | WAbsent -> t.absent
    | WAbstract a -> Abstract.contains_sample a t.abstract
end

800
801
802
type slot = { mutable status : status; 
	       mutable notify : notify;
	       mutable active : bool }
803
804
and status = Empty | NEmpty of Witness.witness | Maybe
and notify = Nothing | Do of slot * (Witness.witness -> unit) * notify
805
806

let slot_empty = { status = Empty; active = false; notify = Nothing }
807
808
let slot_nempty w = { status = NEmpty w;
		     active = false; notify = Nothing }
809

810
let rec notify w = function
811
812
  | Nothing -> ()
  | Do (n,f,rem) -> 
813
814
      if n.status == Maybe then (try f w with NotEmpty -> ());
      notify w rem
815
816
817
818
819

let rec iter_s s f = function
  | [] -> ()
  | arg::rem -> f arg s; iter_s s f rem

820
821
822
let set s w =
  s.status <- NEmpty w;
  notify w s.notify;
823
  s.notify <- Nothing; 
824
825
  raise NotEmpty

826
let rec big_conj f l n w =
827
  match l with
828
829
    | [] -> set n w
    | [arg] -> f w arg n
830
    | arg::rem ->
831
832
	let s = 
	  { status = Maybe; active = false; 
833
	    notify = Do (n,(big_conj f rem n), Nothing) } in
834
	try 
835
	  f w arg s;
836
	  if s.active then n.active <- true
837
	with NotEmpty when n.status == Empty || n.status == Maybe -> ()
838

839
let memo = DescrHash.create 8191
840
841
let marks = ref [] 

842
843
let count_subtype = Stats.Counter.create "Subtyping internal loop" 

844
845
let complex = ref 0

846
let rec slot d =
847
  incr complex;
848
  Stats.Counter.incr count_subtype; 
849
  if d.absent then slot_nempty Witness.WAbsent
850
851
852
853
854
855
  else if not (Intervals.is_empty (BoolIntervals.leafconj d.ints)) 
  then slot_nempty (Witness.WInt (Intervals.sample (BoolIntervals.leafconj d.ints)))
  else if not (Atoms.is_empty (BoolAtoms.leafconj d.atoms)) 
  then slot_nempty (Witness.WAtom (Atoms.sample (BoolAtoms.leafconj d.atoms)))
  else if not (Chars.is_empty (BoolChars.leafconj d.chars)) 
  then slot_nempty (Witness.WChar (Chars.sample (BoolChars.leafconj d.chars)))
856
  else if not (Abstract.is_empty d.abstract) 
857
  then slot_nempty (Witness.WAbstract (Abstract.sample d.abstract))
858
859
860
861
862
  else try DescrHash.find memo d
  with Not_found ->
    let s = { status = Maybe; active = false; notify = Nothing } in
    DescrHash.add memo d s;
    (try
863
864
865
866
       iter_s s check_times (Pair.get (BoolPair.leafconj d.times));
       iter_s s check_xml (Pair.get (BoolPair.leafconj d.xml)); 
       iter_s s check_arrow (Pair.get (BoolPair.leafconj d.arrow));
       iter_s s check_record (get_record (BoolRec.leafconj d.record));
867
       if s.active then marks := s :: !marks else s.status <- Empty;
868
     with NotEmpty -> ());
869
870
    s

871
872
873
874
875
and guard n t f = match (slot t) with
  | { status = Empty } -> ()
  | { status = Maybe } as s -> n.active <- true; s.notify <- Do (n,f,s.notify)
  | { status = NEmpty v } -> f v

876
and check_times (left,right) s =
877
878
879
880
881
882
883
884
  let rec aux w1 w2 accu1 accu2 seen = function
    (* Find a product in right which contains (w1,w2) *)
    | [] -> (* no such product: the current witness is in the difference. *)
	set s (Witness.wpair w1 w2)
    | (n1,n2) :: rest 
	when (Witness.node_has n1 w1) && (Witness.node_has n2 w2) ->
	let right = seen @ rest in
	let accu2' = diff accu2 (descr n2) in 
885
886
887
	guard s accu2' (fun w2 -> aux w1 w2 accu1 accu2' [] right);
	let accu1' = diff accu1 (descr n1) in
	guard s accu1' (fun w1 -> aux w1 w2 accu1' accu2 [] right)
888
    | k :: rest -> aux w1 w2 accu1 accu2 (k::seen) rest
889
  in
890
891
  let (t1,t2) = cap_product any any left in
  guard s t1 (fun w1 -> guard s t2 (fun w2 -> aux w1 w2 t1 t2 [] right))
892
893

and check_xml (left,right) s =
894
895
896
897
898
899
900
901
  let rec aux w1 w2 accu1 accu2 seen = function
    (* Find a product in right which contains (w1,w2) *)
    | [] -> (* no such product: the current witness is in the difference. *)
	set s (Witness.wxml w1 w2)
    | (n1,n2) :: rest 
	when (Witness.node_has n1 w1) && (Witness.node_has n2 w2) ->
	let right = seen @ rest in
	let accu2' = diff accu2 (descr n2) in 
902
903
904
	guard s accu2' (fun w2 -> aux w1 w2 accu1 accu2' [] right);
	let accu1' = diff accu1 (descr n1) in
	guard s accu1' (fun w1 -> aux w1 w2 accu1' accu2 [] right)
905
    | k :: rest -> aux w1 w2 accu1 accu2 (k::seen) rest
906
  in
907
908
  let (t1,t2) = cap_product any any_pair left in
  guard s t1 (fun w1 -> guard s t2 (fun w2 -> aux w1 w2 t1 t2 [] right))
909

910
and check_arrow (left,right) s =
911
912
  let single_right f (s1,s2) s =
    let rec aux w1 w2 accu1 accu2 left = match left with
913
      | (t1,t2)::left ->
914
          let accu1' = diff_t accu1 t1 in 
915
	  guard s accu1' (fun w1 -> aux w1 w2 accu1' accu2 left);
916
917

          let accu2' = cap_t  accu2 t2 in 
918
	  guard s accu2' (fun w2 -> aux w1 (Some w2) accu1 accu2' left)
919
920
921
      | [] -> 
	  let f = match f with Witness.WFun (f,_) -> f | _ -> assert false in
	  set s (Witness.wfun ((w1,w2)::f))
922
923
    in
    let accu1 = descr s1 in
924
    guard s accu1 (fun w1 -> aux w1 None accu1 (neg (descr s2)) left)
925
  in
926
  big_conj single_right right s (Witness.wfun [])
927

928
and check_record (labels,(oleft,left),rights) s =
929
930
  let rec aux ws accus seen = function
    | [] ->
931
932
	let rec aux w i = function
	  | [] -> assert (i == Array.length ws); w
933
934
	  | l::labs -> 
	      let w = match ws.(i) with 
935
936
937
938
939
              | Witness.WAbsent -> w
              | wl -> LabelMap.add l wl w 
            in
	      aux w (succ i) labs
      in
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
	set s (Witness.wrecord (aux LabelMap.empty 0 labels) oleft)
    | (false,_) :: rest when oleft -> aux ws accus seen rest
    | (_,f) :: rest 
	when not (exists (Array.length left)
		    (fun i -> not (Witness.type_has f.(i) ws.(i)))) ->
	(* TODO: a version f get_record which keeps nodes in neg records. *)
	let right = seen @ rest in
	for i = 0 to Array.length left - 1 do
	  let di = diff accus.(i) f.(i) in
	  guard s di (fun wi -> 
			let accus' = Array.copy accus in accus'.(i) <- di;
			let ws' = Array.copy ws in ws'.(i) <- wi;
			aux ws' accus' [] right);
	done
    | k :: rest -> aux ws accus (k::seen) rest
955
  in
956
  let rec start wl i =
957
    if (i < 0) then aux (Array.of_list wl) left [] rights
958
    else guard s left.(i) (fun w -> start (w::wl) (i - 1))
959
  in
960
  start [] (Array.length left - 1)
961

962
let timer_subtype = Stats.Timer.create "Types.is_empty"
963

964
let is_empty d =
965
  Stats.Timer.start timer_subtype;
966
967
  let s = slot d in
  List.iter 
968
969
    (fun s' -> 
       if s'.status == Maybe then s'.status <- Empty; s'.notify <- Nothing) 
970
971
    !marks;
  marks := [];
972
  Stats.Timer.stop timer_subtype
973
    (s.status == Empty)
974

975
976
977
978
let getwit t = match (slot t).status with NEmpty w -> w | _ -> assert false
  (* Assumes that is_empty has been called on t before. *)

let witness t = if is_empty t then raise Not_found else getwit t
979

980
981
982
let non_empty d = 
  not (is_empty d)

983
984
985
let disjoint d1 d2 = is_empty (cap d1 d2)

let subtype d1 d2 = is_empty (diff d1 d2)
986

987
988
let equiv d1 d2 = (subtype d1 d2) && (subtype d2 d1)

989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016