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

4
let count = ref 0
Pietro Abate's avatar
Pietro Abate committed
5

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

Pietro Abate's avatar
Pietro Abate 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

Pietro Abate's avatar
Pietro Abate committed
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
type service_params =
  | TProd of service_params * service_params
Pietro Abate's avatar
Pietro Abate committed
30
  | TOption of service_params
Jérôme Maloberti's avatar
Jérôme Maloberti committed
31 32 33 34 35 36 37
  | 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
Pietro Abate's avatar
Pietro Abate committed
38
  | TFloat of string
Jérôme Maloberti's avatar
Jérôme Maloberti committed
39 40 41
  | TBool of string
  | TFile of string
      (* | TUserType of string * (string -> 'a) * ('a -> string) *)
Pietro Abate's avatar
Pietro Abate committed
42
  | TCoord of string
Jérôme Maloberti's avatar
Jérôme Maloberti committed
43
  | TCoordv of service_params * string
Pietro Abate's avatar
Pietro Abate committed
44
  | TESuffix of string
Jérôme Maloberti's avatar
Jérôme Maloberti committed
45 46 47
  | TESuffixs of string
      (*  | TESuffixu of (string * (string -> 'a) * ('a -> string)) *)
  | TSuffix of (bool * service_params)
Pietro Abate's avatar
Pietro Abate committed
48
  | TUnit
Jérôme Maloberti's avatar
Jérôme Maloberti committed
49 50 51
  | 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
    | Record _, _ -> -1
    | _, Record _ -> 1
    | String (i1,j1,s1,r1), String (i2,j2,s2,r2) ->
Pietro Abate's avatar
Pietro Abate committed
84
	let c = Pervasives.compare i1 i2 in if c <> 0 then c
85 86 87
	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
    | Cofinite l ->
118 119 120 121
	[ fun ppf ->
	  Format.fprintf ppf "@[Abstract";
	  List.iter (fun x -> Format.fprintf ppf " \\@ !%s" x) l;
	  Format.fprintf ppf "@]" ]
122

123 124 125 126
  let contains_sample s t = match s,t with
    | None, Cofinite _ -> true
    | None, Finite _ -> false
    | Some s, t -> contains s t
Pietro Abate's avatar
Pietro Abate committed
127

128 129
end

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

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

139 140 141

module TLV = struct

Pietro Abate's avatar
Pietro Abate committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  module Set = struct
    include Set.Make(
      struct
        type t = (Var.var * bool)
        let compare (v1,p1) (v2,p2) =
          let c = Var.compare v1 v2 in
          if c == 0 then
            if p1 == p2 then 0
            else if p1 then 1 else -1
          else c
      end)
    let print sep ppf s =
      let aux1 ppf = function
        |(v,true) -> Format.fprintf ppf "%a" Var.print v 
        |(v,false) -> Format.fprintf ppf "~ %a" Var.print v 
      in
      let rec aux ppf = function
        |[] -> ()
        |[h] -> aux1 ppf h
        |h::l -> Format.fprintf ppf "%a %s %a" aux1 h sep aux l
      in 
      aux ppf (elements s)
  end
165

166 167 168
  (* s : top level variables
   * f : all free variables in the subtree
   * b : true if the type contains only variables *)
169
  type t =  { s : Set.t ; f : Var.Set.t ; b : bool }
170

Pietro Abate's avatar
Pietro Abate committed
171
  let empty = { s = Set.empty ; f = Var.Set.empty ; b = false }
172 173 174
  let any = { s = Set.empty ; f = Var.Set.empty ; b = false }

  let singleton (v,p) = { s = Set.singleton (v,p); f = Var.Set.singleton v; b = true }
175 176 177 178 179 180 181

  (* return the max of top level variables *)
  let max x = Set.max_elt x.s

  let pair x y = {
    b = false ; 
    s = Set.empty ; 
182
    f = Var.Set.union x.f y.f 
183 184 185
  }

  (* true if it contains only one variable *)
186
  let is_single x = x.b && (Var.Set.cardinal x.f = 1) && (Set.cardinal x.s = 1)
Pietro Abate's avatar
Pietro Abate committed
187

Pietro Abate's avatar
Pietro Abate committed
188
  let no_variables x = (x.b == false) && (Var.Set.cardinal x.f = 0) && (Set.cardinal x.s = 0)
189

Pietro Abate's avatar
Pietro Abate committed
190
  let no_toplevel x = (Set.cardinal x.s = 0)
Pietro Abate's avatar
Pietro Abate committed
191
  let has_toplevel x = Set.cardinal x.s > 0
Pietro Abate's avatar
Pietro Abate committed
192

Pietro Abate's avatar
Pietro Abate committed
193 194 195 196
  let print ppf x = if x.b then Set.print ";" ppf x.s
  let dump ppf x =
    Format.fprintf ppf "<b = %b ; f = {%a} ; s = {%a}>" 
      x.b Var.Set.dump x.f (Set.print ";") x.s
Pietro Abate's avatar
Pietro Abate committed
197 198
  let mem v x = Set.mem v x.s

199
end
200

Pietro Abate's avatar
Pietro Abate committed
201
module rec Descr :
202
sig
Pietro Abate's avatar
Pietro Abate committed
203
  (* each kind is represented as a union of itersection of types
204 205 206
   * the type is a union of all kinds
   *
   * we add a new field that contains only variables.
Pietro Abate's avatar
Pietro Abate committed
207
   * Inv :
208 209 210 211 212 213 214 215 216 217 218 219
     * 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
   *)
220
  type s = {
221 222 223
    atoms : BoolAtoms.t;
    ints  : BoolIntervals.t;
    chars : BoolChars.t;
224 225 226 227
    times : BoolPair.t;
    xml   : BoolPair.t;
    arrow : BoolPair.t;
    record: BoolRec.t;
228
    abstract: Abstract.t;
229 230 231
    (* 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 *)
232 233 234
    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 *)
235
    toplvars : TLV.t
236
  }
237
  include Custom.T with type t = s
238
  val empty: t
239 240
end =
struct
241
  type s = {
242 243 244
    atoms : BoolAtoms.t;
    ints  : BoolIntervals.t;
    chars : BoolChars.t;
245 246 247 248
    times : BoolPair.t;
    xml   : BoolPair.t;
    arrow : BoolPair.t;
    record: BoolRec.t;
249
    abstract: Abstract.t;
250
    absent: bool;
251
    toplvars : TLV.t
252
  }
253
  type t = s
254

255 256 257 258
  let print_lst ppf =
    List.iter (fun f -> f ppf; Format.fprintf ppf " |")

  let dump ppf d =
Pietro Abate's avatar
Pietro Abate committed
259
    Format.fprintf ppf "<types atoms(%a) ints(%a) chars(%a) times(%a) arrow(%a) record(%a) xml(%a) abstract(%a) absent(%b)>\n%a"
260
      BoolAtoms.dump d.atoms
261 262
      BoolIntervals.dump d.ints
      BoolChars.dump d.chars
263
      BoolPair.dump d.times
264
      BoolPair.dump d.arrow
265 266
      BoolRec.dump d.record
      BoolPair.dump d.xml
267 268
      Abstract.dump d.abstract
      d.absent
Pietro Abate's avatar
Pietro Abate committed
269
      TLV.dump d.toplvars
270

Pietro Abate's avatar
Pietro Abate committed
271 272 273 274
  let empty = {
    times = BoolPair.empty;
    xml   = BoolPair.empty;
    arrow = BoolPair.empty;
275
    record= BoolRec.empty;
276 277 278
    ints  = BoolIntervals.empty;
    atoms = BoolAtoms.empty;
    chars = BoolChars.empty;
279
    abstract = Abstract.empty;
280
    absent= false;
281
    toplvars = TLV.empty
282 283
  }

284 285 286 287 288 289 290 291 292 293 294
  let check a =
    BoolChars.check a.chars;
    BoolIntervals.check a.ints;
    BoolAtoms.check a.atoms;
    BoolPair.check a.times;
    BoolPair.check a.xml;
    BoolPair.check a.arrow;
    BoolRec.check a.record;
    Abstract.check a.abstract;
    ()

295
  let equal a b =
296
    (a == b) || (
297 298 299
      (BoolAtoms.equal a.atoms b.atoms) &&
      (BoolChars.equal a.chars b.chars) &&
      (BoolIntervals.equal a.ints  b.ints) &&
300 301 302 303
      (BoolPair.equal a.times b.times) &&
      (BoolPair.equal a.xml b.xml) &&
      (BoolPair.equal a.arrow b.arrow) &&
      (BoolRec.equal a.record b.record) &&
304
      (Abstract.equal a.abstract b.abstract) &&
305 306
      (a.absent == b.absent)
    )
307 308

  let compare a b =
Pietro Abate's avatar
Pietro Abate committed
309
    if a == b then 0
310 311 312
    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
313 314 315 316
    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
317
    else let c = Abstract.compare a.abstract b.abstract in if c <> 0 then c
318 319 320
    else if a.absent && not b.absent then -1
    else if b.absent && not a.absent then 1
    else 0
Pietro Abate's avatar
Pietro Abate committed
321

322
  let hash a =
323 324 325
    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
326 327 328 329 330 331 332
    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
333

334 335 336
end
and Node :
sig
337
  type t = { id : int; cu: Compunit.t; mutable descr : Descr.t }
338 339 340 341 342
  val dump: Format.formatter -> t -> unit
  val check: t -> unit
  val equal: t -> t -> bool
  val hash: t -> int
  val compare:t -> t -> int
343
  val mk: int -> Descr.t -> t
344
end =
345

346
struct
347
  type t = { id : int; cu: Compunit.t; mutable descr : Descr.t }
348
  let check n = ()
349
  let dump ppf n = Format.fprintf ppf "X%i" n.id
350
  let hash x = x.id + Compunit.hash x.cu
Pietro Abate's avatar
Pietro Abate committed
351
  let compare x y =
352 353 354
    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 }
355 356
end

357 358
and Pair : Bool.S with type elem = (Node.t * Node.t) =
  Bool.Make(Custom.Pair(Node)(Node))
Pietro Abate's avatar
Pietro Abate committed
359
and BoolPair : BoolVar.S with
360
  type s = Pair.t = BoolVar.Make(Pair)
361

362 363 364 365 366
(* 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)))
367 368
and BoolRec : BoolVar.S with
  type s = Rec.t = BoolVar.Make(Rec)
369

370 371
module DescrHash = Hashtbl.Make(Descr)
module DescrMap = Map.Make(Descr)
372 373
module DescrSet = Set.Make(Descr)
module DescrSList = SortedList.Make(Descr)
374

375 376 377
type descr = Descr.t
type node = Node.t
include Descr
378

379 380
let forward_print = ref (fun _ _ -> assert false)

Pietro Abate's avatar
Pietro Abate committed
381 382
let make () =
  incr count;
383 384
  Node.mk !count empty

Pietro Abate's avatar
Pietro Abate committed
385
let define n d =
386 387
  n.Node.descr <- d

Pietro Abate's avatar
Pietro Abate committed
388 389
let cons d =
  incr count;
390 391
  Node.mk !count d

392 393 394 395
let descr n = n.Node.descr
let internalize n = n
let id n = n.Node.id

396 397 398
(* two representation possible. either all fields (except vars) are full, OR
 * the field vars is full.
 *)
399
let any =  {
Pietro Abate's avatar
Pietro Abate committed
400 401 402 403
  times = BoolPair.full;
  xml   = BoolPair.full;
  arrow = BoolPair.full;
  record= BoolRec.full;
404 405 406
  ints  = BoolIntervals.full;
  atoms = BoolAtoms.full;
  chars = BoolChars.full;
407
  abstract = Abstract.any;
408
  absent= false;
409
  toplvars = TLV.any
410
}
411 412

let non_constructed =
Pietro Abate's avatar
Pietro Abate committed
413
  { any with
414
      times = empty.times; xml = empty.xml; record = empty.record }
Pietro Abate's avatar
Pietro Abate committed
415 416

let non_constructed_or_absent =
417
  { non_constructed with absent = true }
418

Pietro Abate's avatar
Pietro Abate committed
419
(* Descr.t type constructors *)
Pietro Abate's avatar
Pietro Abate committed
420 421 422 423 424 425 426 427 428
let times x y = { empty with 
                    times = BoolPair.atom (`Atm (Pair.atom (x,y))); 
                    toplvars = TLV.pair (descr x).toplvars (descr y).toplvars }
let xml x y = { empty with 
                    xml = BoolPair.atom (`Atm (Pair.atom (x,y)));
                    toplvars = TLV.pair (descr x).toplvars (descr y).toplvars }
let arrow x y = { empty with 
                    arrow = BoolPair.atom (`Atm (Pair.atom (x,y))); 
                    toplvars = TLV.pair (descr x).toplvars (descr y).toplvars }
429 430

(* XXX toplevel variables are not properly set for records *)
Pietro Abate's avatar
Pietro Abate committed
431 432
let record label t =
  { empty with
433
      record = BoolRec.atom (`Atm (Rec.atom (true,LabelMap.singleton label t))) }
434
let record_fields x =
435
  { empty with record = BoolRec.atom (`Atm (Rec.atom x)) }
436

437
let atom a = { empty with atoms = BoolAtoms.atom (`Atm a) }
438 439

(* Atm = Any ^ a *)
440 441
let var a =  {
  times = BoolPair.vars a;
Pietro Abate's avatar
Pietro Abate committed
442 443 444
  xml   = BoolPair.vars a;
  arrow = BoolPair.vars a;
  record= BoolRec.vars a;
445 446 447
  ints  = BoolIntervals.vars a;
  atoms = BoolAtoms.vars a;
  chars = BoolChars.vars a;
448
  abstract = Abstract.empty;
449
  absent= false;
450
  toplvars = TLV.singleton (a,true)
451 452
}

453
let is_var t = TLV.is_single t.toplvars
454
let no_var t = TLV.no_variables t.toplvars
455
let all_vars t = t.toplvars.TLV.f
456

Pietro Abate's avatar
Pietro Abate committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
(* XXX this function could be potentially costly. There should be 
 * better way to take trace of top level variables in a type *) 
let update_tlv x y t =
  let open TLV in
  let tlv t =
    let aux get bdd : Set.t =
      let l = 
        List.fold_left (fun acc (p,n) ->
          let acc1 = 
            List.fold_left (fun acc -> function
              |(`Var v) as x -> Set.union acc (Set.singleton (x,true))
              |_ -> acc
            ) Set.empty p
          in
          let acc2 = 
            List.fold_left (fun acc -> function
              |(`Var v) as x -> Set.union acc (Set.singleton (x,false))
              |_ -> assert false
            ) acc1 n
          in acc2::acc
        ) [] (get bdd)
      in
      match l with
      |[] -> Set.empty
      |[h] -> h
      |h::l -> List.fold_left Set.inter h l
    in
    List.fold_left Set.inter
    (aux BoolChars.get t.chars) 
    [(aux BoolIntervals.get t.ints);
    (aux BoolAtoms.get t.atoms);
    (aux BoolPair.get t.arrow);
    (aux BoolPair.get t.xml);
    (aux BoolRec.get t.record)]
  in
  let s = tlv t in
  { t with toplvars = { s = s ; f = Var.Set.union x.toplvars.f y.toplvars.f ; b = x.toplvars.b && x.toplvars.b } }
;;

496 497
let char c = { empty with chars = BoolChars.atom (`Atm c) }
let interval i = { empty with ints = BoolIntervals.atom (`Atm i) }
498
let abstract a = { empty with abstract = a }
499 500

let get_abstract t = t.abstract
501

Pietro Abate's avatar
Pietro Abate committed
502
let cup x y =
Pietro Abate's avatar
Pietro Abate committed
503
  if x == y then x else update_tlv x y {
504 505 506
    times = BoolPair.cup x.times y.times;
    xml   = BoolPair.cup x.xml y.xml;
    arrow = BoolPair.cup x.arrow y.arrow;
507
    record= BoolRec.cup x.record y.record;
508 509 510
    ints  = BoolIntervals.cup x.ints  y.ints;
    atoms = BoolAtoms.cup x.atoms y.atoms;
    chars = BoolChars.cup x.chars y.chars;
511
    abstract = Abstract.cup x.abstract y.abstract;
512
    absent= x.absent || y.absent;
Pietro Abate's avatar
Pietro Abate committed
513
    toplvars = TLV.empty
514
  }
515

Pietro Abate's avatar
Pietro Abate committed
516
let cap x y =
Pietro Abate's avatar
Pietro Abate committed
517
  if x == y then x else update_tlv x y {
518
    ints  = BoolIntervals.cap x.ints y.ints;
519 520
    times = BoolPair.cap x.times y.times;
    xml   = BoolPair.cap x.xml y.xml;
521
    record= BoolRec.cap x.record y.record;
522
    arrow = BoolPair.cap x.arrow y.arrow;
523 524
    atoms = BoolAtoms.cap x.atoms y.atoms;
    chars = BoolChars.cap x.chars y.chars;
525
    abstract = Abstract.cap x.abstract y.abstract;
526
    absent= x.absent && y.absent;
Pietro Abate's avatar
Pietro Abate committed
527
    toplvars = TLV.empty
528
  }
529

Pietro Abate's avatar
Pietro Abate committed
530
let diff x y =
Pietro Abate's avatar
Pietro Abate committed
531
  if x == y then empty else update_tlv x y {
532 533 534
    times = BoolPair.diff x.times y.times;
    xml   = BoolPair.diff x.xml y.xml;
    arrow = BoolPair.diff x.arrow y.arrow;
535
    record= BoolRec.diff x.record y.record;
536
    ints  = BoolIntervals.diff x.ints y.ints;
537 538
    atoms = BoolAtoms.diff x.atoms y.atoms;
    chars = BoolChars.diff x.chars y.chars;
539
    abstract = Abstract.diff x.abstract y.abstract;
540
    absent= x.absent && not y.absent;
Pietro Abate's avatar
Pietro Abate committed
541
    toplvars = TLV.empty
542
  }
Pietro Abate's avatar
Pietro Abate committed
543

544 545
(* TODO: optimize disjoint check for boolean combinations *)
let trivially_disjoint a b =
546 547 548
  (BoolChars.trivially_disjoint a.chars b.chars) &&
  (BoolIntervals.trivially_disjoint a.ints b.ints) &&
  (BoolAtoms.trivially_disjoint a.atoms b.atoms) &&
549 550 551
  (BoolPair.trivially_disjoint a.times b.times) &&
  (BoolPair.trivially_disjoint a.xml b.xml) &&
  (BoolPair.trivially_disjoint a.arrow b.arrow) &&
552
  (BoolRec.trivially_disjoint a.record b.record) &&
553
  (Abstract.disjoint a.abstract b.abstract) &&
554
  (not (a.absent && b.absent))
555

556
let rec constant = function
557 558 559
  | Integer i -> interval (Intervals.atom i)
  | Atom a -> atom (Atoms.atom a)
  | Char c -> char (Chars.atom c)
560
  | Pair (x,y) -> times (const_node x) (const_node y)
561
  | Xml (x,y) -> xml (const_node x) (const_node y)
562
  | Record x -> record_fields (false ,LabelMap.map const_node x)
563 564
  | String (i,j,s,c) ->
      if U.equal_index i j then constant c
Pietro Abate's avatar
Pietro Abate committed
565
      else
566 567 568
	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)
569

570 571
let neg x = diff any x

572
let any_node = cons any
573
let empty_node = cons empty
574

575
module LabelS = Set.Make(Label)
576

Pietro Abate's avatar
Pietro Abate committed
577
let any_or_absent = { any with absent = true }
578
let only_absent = { empty with absent = true }
579

580
let get_record r =
Pietro Abate's avatar
Pietro Abate committed
581 582
  let labs accu (_,r) =
    List.fold_left
583
      (fun accu (l,_) -> LabelS.add l accu) accu (LabelMap.get r) in
584
  let extend descrs labs (o,r) =
585 586 587 588 589
    let rec aux i labs r =
      match labs with
	| [] -> ()
	| l1::labs ->
	    match r with
Pietro Abate's avatar
Pietro Abate committed
590
	      | (l2,x)::r when l1 == l2 ->
591 592 593
		  descrs.(i) <- cap descrs.(i) (descr x);
		  aux (i+1) labs r
	      | r ->
Pietro Abate's avatar
Pietro Abate committed
594
		  if not o then
595
		    descrs.(i) <- cap descrs.(i) only_absent; (* TODO:OPT *)
596 597
		  aux (i+1) labs r
    in
598
    aux 0 labs (LabelMap.get r);
599 600 601
    o
  in
  let line (p,n) =
Pietro Abate's avatar
Pietro Abate committed
602
    let labels =
603 604
      List.fold_left labs (List.fold_left labs LabelS.empty p) n in
    let labels = LabelS.elements labels in
605
    let nlab = List.length labels in
606
    let mk () = Array.create nlab any_or_absent in
607 608

    let pos = mk () in
Pietro Abate's avatar
Pietro Abate committed
609 610
    let opos = List.fold_left
		 (fun accu x ->
611 612 613 614 615 616 617 618 619 620 621
		    (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
622
  List.map line (Rec.get r)
623

624 625 626 627 628
(* 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)
629
let cap_product any_left any_right l =
Pietro Abate's avatar
Pietro Abate committed
630
  List.fold_left
631
    (fun (d1,d2) (t1,t2) -> (cap_t d1 t1, cap_t d2 t2))
632
    (any_left,any_right)
633
    l
634
let any_pair = { empty with times = any.times }
635

636 637 638
let rec exists max f =
  (max > 0) && (f (max - 1) || exists (max - 1) f)

639
exception NotEmpty
640

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
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
Pietro Abate's avatar
Pietro Abate committed
658
  and witness_slot =
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
      { 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
Pietro Abate's avatar
Pietro Abate committed
675
	| WPair (_,_,s)
676 677 678 679 680 681
	| 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
Pietro Abate's avatar
Pietro Abate committed
682
	| WRecord (r,o,_) ->
683 684 685
	    (if o then 2 else 3) + 257 * LabelMap.hash hash_small r
	| WFun (f,_) ->
	    4 + 257 *
Pietro Abate's avatar
Pietro Abate committed
686 687
	      (Hashtbl.hash
		 (List.map
688
		    (function (x,None) -> 17 * hash_small x
Pietro Abate's avatar
Pietro Abate committed
689
		       | (x,Some y) ->
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
			   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
Pietro Abate's avatar
Pietro Abate committed
709 710
	| WPair (p1,q1,_), WPair (p2,q2,_)
	| WXml (p1,q1,_), WXml (p2,q2,_) ->
711 712 713 714 715 716 717 718
	    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
Pietro Abate's avatar
Pietro Abate committed
719
					   | Some y1, Some y2 ->
720 721 722 723 724 725 726 727 728
					       equal_small y1 y2
					   | None, None -> true
					   | _ -> false)
		) f1 f2
	| _ -> false
    end)

  let wmemo = WHash.create 1024
  let wuid = ref 0
Pietro Abate's avatar
Pietro Abate committed
729
  let wslot () = { wuid = !wuid; wnodes_in = NodeSet.empty;
730 731
		   wnodes_out = NodeSet.empty }

732 733 734 735
  let () =
    Stats.register Stats.Summary
      (fun ppf -> Format.fprintf ppf "Allocated witnesses:%i@\n" !wuid)

736 737 738 739 740 741 742 743 744 745 746
  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
Pietro Abate's avatar
Pietro Abate committed
747
    | WPair (w1,w2,_) ->
748
	Format.fprintf ppf "(%a,%a)" print_witness w1 print_witness w2
Pietro Abate's avatar
Pietro Abate committed
749
    | WXml (w1,w2,_) ->
750 751 752 753
	Format.fprintf ppf "XML(%a,%a)" print_witness w1 print_witness w2
    | WRecord (ws,o,_) ->
	Format.fprintf ppf "{";
	LabelMap.iteri
Pietro Abate's avatar
Pietro Abate committed
754
	  (fun l w -> Format.fprintf ppf " %a=%a"
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
	     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"
Pietro Abate's avatar
Pietro Abate committed
773

774 775
  let wmk w =  (* incr wuid; w *)  (* hash-consing disabled *)
    try WHash.find wmemo w
Pietro Abate's avatar
Pietro Abate committed
776 777
    with Not_found ->
      incr wuid;
778
      WHash.add wmemo w w;
Pietro Abate's avatar
Pietro Abate committed
779
(*      Format.fprintf Format.std_formatter "W:%a@."
780 781 782 783 784 785 786 787
	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()))

788
  let bool_pair f =
Pietro Abate's avatar
Pietro Abate committed
789 790 791
    Pair.compute
      ~empty:false ~full:true
      ~cup:(||) ~cap:(&&) ~diff:(fun x y -> x && not y)
792 793 794
      ~atom:f

  let bool_rec f =
Pietro Abate's avatar
Pietro Abate committed
795 796 797
    Rec.compute
      ~empty:false ~full:true
      ~cup:(||) ~cap:(&&) ~diff:(fun x y -> x && not y)
798 799 800 801 802 803 804 805 806 807 808 809
      ~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

810 811
  (* type_has checks if a witness is contained in the union of
   * the leafs of a bdd, ignoring all variables. *)
812
  and type_has t = function
813 814 815
    | WInt i -> Intervals.contains i (BoolIntervals.leafconj t.ints)
    | WChar c