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

4 5
let (@@) f a = f a

6
let count = ref 0
Pietro Abate's avatar
Pietro Abate committed
7

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

Pietro Abate's avatar
Pietro Abate committed
12
(*
13 14 15 16 17 18 19 20
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
21
type const =
22
  | Integer of Intervals.V.t
23
  | Atom of Atoms.V.t
24
  | Char of Chars.V.t
25 26 27 28
  | Pair of const * const
  | Xml of const * const
  | Record of const label_map
  | String of U.uindex * U.uindex * U.t * const
29

30 31
type service_params =
  | TProd of service_params * service_params
Pietro Abate's avatar
Pietro Abate committed
32
  | TOption of service_params
33 34 35 36 37 38 39
  | 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
40
  | TFloat of string
41 42 43
  | TBool of string
  | TFile of string
      (* | TUserType of string * (string -> 'a) * ('a -> string) *)
Pietro Abate's avatar
Pietro Abate committed
44
  | TCoord of string
45
  | TCoordv of service_params * string
Pietro Abate's avatar
Pietro Abate committed
46
  | TESuffix of string
47 48 49
  | TESuffixs of string
      (*  | TESuffixu of (string * (string -> 'a) * ('a -> string)) *)
  | TSuffix of (bool * service_params)
Pietro Abate's avatar
Pietro Abate committed
50
  | TUnit
51 52 53
  | TAny
  | TConst of string;;

54 55 56
module Const = struct
  type t = const

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

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

68
    | Char x, Char y -> Chars.V.compare x y
69 70 71
    | Char _, _ -> -1
    | _, Char _ -> 1
    | Pair (x1,x2), Pair (y1,y2) ->
72 73
	let c = compare x1 y1 in
	if c <> 0 then c else compare x2 y2
74 75 76
    | Pair (_,_), _ -> -1
    | _, Pair (_,_) -> 1
    | Xml (x1,x2), Xml (y1,y2) ->
77 78
	let c = compare x1 y1 in
	if c <> 0 then c else compare x2 y2
79 80 81
    | Xml (_,_), _ -> -1
    | _, Xml (_,_) -> 1
    | Record x, Record y ->
82
	LabelMap.compare compare x y
83 84 85
    | Record _, _ -> -1
    | _, Record _ -> 1
    | String (i1,j1,s1,r1), String (i2,j2,s2,r2) ->
Pietro Abate's avatar
Pietro Abate committed
86
	let c = Pervasives.compare i1 i2 in if c <> 0 then c
87 88 89
	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 *)
90 91 92 93 94 95 96 97 98 99
	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
100
      (* Note: improve hash for String *)
101

102 103
  let equal c1 c2 = compare c1 c2 = 0
end
104

105
module Abstracts = struct
106
  module T = Custom.String
107 108
  type abs = T.t

109
  module V = struct type t = abs * Obj.t end
110 111

  include SortedList.FiniteCofinite(T)
112
  let full = any
113 114

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

122 123 124 125
  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
126

127 128
end

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

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

Pietro Abate's avatar
Pietro Abate committed
140
module rec Descr :
141
sig
Pietro Abate's avatar
Pietro Abate committed
142
  (* each kind is represented as a union of itersection of types
143 144 145
   * the type is a union of all kinds
   *
   * we add a new field that contains only variables.
Pietro Abate's avatar
Pietro Abate committed
146
   * Inv :
147 148 149 150 151 152 153 154 155 156 157 158
     * 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
   *)
159
  type s = {
160 161 162
    atoms : BoolAtoms.t;
    ints  : BoolIntervals.t;
    chars : BoolChars.t;
163 164 165 166
    times : BoolPair.t;
    xml   : BoolPair.t;
    arrow : BoolPair.t;
    record: BoolRec.t;
167
    abstract: BoolAbstracts.t;
168 169 170
    (* 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 *)
171
    absent: bool;
172
  }
173
  include Custom.T with type t = s
174
  val empty: t
175
  val any : t
Julien Lopez's avatar
Julien Lopez committed
176
  val is_empty : t -> bool
177 178
end =
struct
179
  type s = {
180 181 182
    atoms : BoolAtoms.t;
    ints  : BoolIntervals.t;
    chars : BoolChars.t;
183 184 185 186
    times : BoolPair.t;
    xml   : BoolPair.t;
    arrow : BoolPair.t;
    record: BoolRec.t;
187
    abstract: BoolAbstracts.t;
188
    absent: bool;
189
  }
190
  type t = s
191

192
  let dump ppf d =
193
    Format.fprintf ppf "<types atoms(%a) ints(%a) chars(%a) times(%a) arrow(%a) record(%a) xml(%a) abstract(%a) absent(%b)>\n"
194
      BoolAtoms.dump d.atoms
195 196
      BoolIntervals.dump d.ints
      BoolChars.dump d.chars
197
      BoolPair.dump d.times
198
      BoolPair.dump d.arrow
199 200
      BoolRec.dump d.record
      BoolPair.dump d.xml
201
      BoolAbstracts.dump d.abstract
202
      d.absent
203

Pietro Abate's avatar
Pietro Abate committed
204 205 206 207
  let empty = {
    times = BoolPair.empty;
    xml   = BoolPair.empty;
    arrow = BoolPair.empty;
208
    record= BoolRec.empty;
209 210 211
    ints  = BoolIntervals.empty;
    atoms = BoolAtoms.empty;
    chars = BoolChars.empty;
212
    abstract = BoolAbstracts.empty;
213
    absent = false;
214 215
  }

216 217 218 219 220 221 222 223
  (*
   * Two representations possible. Either all fields (except vars) are full, OR
   * the field vars is full.
   *)
  let any =  {
    times = BoolPair.full;
    xml   = BoolPair.full;
    arrow = BoolPair.full;
224
    record = BoolRec.full;
225 226 227
    ints  = BoolIntervals.full;
    atoms = BoolAtoms.full;
    chars = BoolChars.full;
228
    abstract = BoolAbstracts.full;
229
    absent = false;
230 231
  }

232 233 234 235 236 237 238 239
  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;
240
    BoolAbstracts.check a.abstract;
241 242
    ()

243
  let equal a b =
244
    (a == b) || (
245 246 247
      (BoolAtoms.equal a.atoms b.atoms) &&
      (BoolChars.equal a.chars b.chars) &&
      (BoolIntervals.equal a.ints  b.ints) &&
248 249 250 251
      (BoolPair.equal a.times b.times) &&
      (BoolPair.equal a.xml b.xml) &&
      (BoolPair.equal a.arrow b.arrow) &&
      (BoolRec.equal a.record b.record) &&
252
      (BoolAbstracts.equal a.abstract b.abstract) &&
253 254
      (a.absent == b.absent)
    )
255

Julien Lopez's avatar
Julien Lopez committed
256 257 258 259 260 261 262 263
  let is_empty a =
    (BoolAtoms.is_empty a.atoms) &&
      (BoolChars.is_empty a.chars) &&
      (BoolIntervals.is_empty a.ints) &&
      (BoolPair.is_empty a.times) &&
      (BoolPair.is_empty a.xml) &&
      (BoolPair.is_empty a.arrow) &&
      (BoolRec.is_empty a.record) &&
264
      (BoolAbstracts.is_empty a.abstract)
Julien Lopez's avatar
Julien Lopez committed
265

266
  let compare a b =
Pietro Abate's avatar
Pietro Abate committed
267
    if a == b then 0
268 269 270
    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
271 272 273 274
    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
275
    else let c = BoolAbstracts.compare a.abstract b.abstract in if c <> 0 then c
276 277 278
    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
279

280
  let hash a =
281 282 283
    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
284 285 286 287
    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
288
    let accu = 17 * accu + BoolAbstracts.hash a.abstract in
289 290
    let accu = if a.absent then accu+5 else accu in
    accu
291

292 293 294
end
and Node :
sig
295
  type t = { id : int; cu: Compunit.t; mutable descr : Descr.t }
296 297 298 299 300
  val dump: Format.formatter -> t -> unit
  val check: t -> unit
  val equal: t -> t -> bool
  val hash: t -> int
  val compare:t -> t -> int
301
  val mk: int -> Descr.t -> t
302
end =
303

304
struct
305
  type t = { id : int; cu: Compunit.t; mutable descr : Descr.t }
306
  let check n = ()
307
  let dump ppf n = Format.fprintf ppf "X%i" n.id
308
  let hash x = x.id + Compunit.hash x.cu
Pietro Abate's avatar
Pietro Abate committed
309
  let compare x y =
310 311 312
    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 }
313 314
end

315 316
and Pair : Bool.S with type elem = (Node.t * Node.t) =
  Bool.Make(Custom.Pair(Node)(Node))
Kim Nguyễn's avatar
Kim Nguyễn committed
317
and BoolPair : BoolVar.S with type s = Pair.t =
318
  BoolVar.Make(Pair)
319

320 321 322 323 324
(* 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)))
325 326
and BoolRec : BoolVar.S with
  type s = Rec.t = BoolVar.Make(Rec)
327

328 329
module DescrHash = Hashtbl.Make(Descr)
module DescrMap = Map.Make(Descr)
330 331
module DescrSet = Set.Make(Descr)
module DescrSList = SortedList.Make(Descr)
332

333 334 335
type descr = Descr.t
type node = Node.t
include Descr
336

337 338
let dummy_print = (fun _ _ -> assert false)
let forward_print = ref dummy_print
339

Pietro Abate's avatar
Pietro Abate committed
340 341
let make () =
  incr count;
342 343
  Node.mk !count empty

Pietro Abate's avatar
Pietro Abate committed
344
let define n d =
345 346
  n.Node.descr <- d

Pietro Abate's avatar
Pietro Abate committed
347 348
let cons d =
  incr count;
349 350
  Node.mk !count d

351 352 353 354
let descr n = n.Node.descr
let internalize n = n
let id n = n.Node.id

355
let non_constructed =
Pietro Abate's avatar
Pietro Abate committed
356
  { any with
357
      times = empty.times; xml = empty.xml; record = empty.record }
Pietro Abate's avatar
Pietro Abate committed
358 359

let non_constructed_or_absent =
360
  { non_constructed with absent = true }
361

Pietro Abate's avatar
Pietro Abate committed
362
(* Descr.t type constructors *)
363 364 365
let times x y = { empty with times = BoolPair.atom (`Atm (Pair.atom (x,y))) }
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))) }
366

Pietro Abate's avatar
Pietro Abate committed
367
let record label t =
368
  { empty with record = BoolRec.atom (`Atm (Rec.atom (true,LabelMap.singleton label t))) }
369

370
let record_fields x =
371
  { empty with record = BoolRec.atom (`Atm (Rec.atom x)) }
372

373
let atom a = { empty with atoms = BoolAtoms.atom (`Atm a) }
374 375

(* Atm = Any ^ a *)
376 377
let var a =  {
  times = BoolPair.vars a;
Pietro Abate's avatar
Pietro Abate committed
378 379 380
  xml   = BoolPair.vars a;
  arrow = BoolPair.vars a;
  record= BoolRec.vars a;
381 382 383
  ints  = BoolIntervals.vars a;
  atoms = BoolAtoms.vars a;
  chars = BoolChars.vars a;
384
  abstract = BoolAbstracts.vars a;
385
  absent = false;
386 387
}

388 389
let char c = { empty with chars = BoolChars.atom (`Atm c) }
let interval i = { empty with ints = BoolIntervals.atom (`Atm i) }
390
let abstract a = { empty with abstract = BoolAbstracts.atom (`Atm a) }
391

Pietro Abate's avatar
Pietro Abate committed
392
let cup x y =
393
  if x == y then x else
394
    {
395 396 397 398 399 400 401
      times = BoolPair.cup x.times y.times;
      xml   = BoolPair.cup x.xml y.xml;
      arrow = BoolPair.cup x.arrow y.arrow;
      record= BoolRec.cup x.record y.record;
      ints  = BoolIntervals.cup x.ints  y.ints;
      atoms = BoolAtoms.cup x.atoms y.atoms;
      chars = BoolChars.cup x.chars y.chars;
402
      abstract = BoolAbstracts.cup x.abstract y.abstract;
403 404
      absent = x.absent || y.absent;
    }
405

Pietro Abate's avatar
Pietro Abate committed
406
let cap x y =
407
  if x == y then x else
408
    {
409 410 411
      ints  = BoolIntervals.cap x.ints y.ints;
      times = BoolPair.cap x.times y.times;
      xml   = BoolPair.cap x.xml y.xml;
412
      record = BoolRec.cap x.record y.record;
413 414 415
      arrow = BoolPair.cap x.arrow y.arrow;
      atoms = BoolAtoms.cap x.atoms y.atoms;
      chars = BoolChars.cap x.chars y.chars;
416
      abstract = BoolAbstracts.cap x.abstract y.abstract;
417
      absent= x.absent && y.absent;
418
    }
419

Pietro Abate's avatar
Pietro Abate committed
420
let diff x y =
421
  if x == y then empty else
422
    {
423 424 425 426 427 428 429
      times = BoolPair.diff x.times y.times;
      xml   = BoolPair.diff x.xml y.xml;
      arrow = BoolPair.diff x.arrow y.arrow;
      record= BoolRec.diff x.record y.record;
      ints  = BoolIntervals.diff x.ints y.ints;
      atoms = BoolAtoms.diff x.atoms y.atoms;
      chars = BoolChars.diff x.chars y.chars;
430
      abstract = BoolAbstracts.diff x.abstract y.abstract;
431
      absent= x.absent && not y.absent;
432
    }
Pietro Abate's avatar
Pietro Abate committed
433

434 435
(* TODO: optimize disjoint check for boolean combinations *)
let trivially_disjoint a b =
436 437 438
  (BoolChars.trivially_disjoint a.chars b.chars) &&
  (BoolIntervals.trivially_disjoint a.ints b.ints) &&
  (BoolAtoms.trivially_disjoint a.atoms b.atoms) &&
439 440 441
  (BoolPair.trivially_disjoint a.times b.times) &&
  (BoolPair.trivially_disjoint a.xml b.xml) &&
  (BoolPair.trivially_disjoint a.arrow b.arrow) &&
442
  (BoolRec.trivially_disjoint a.record b.record) &&
443
  (BoolAbstracts.trivially_disjoint a.abstract b.abstract) &&
444
  (not (a.absent && b.absent))
445

446
let rec constant = function
447 448 449
  | Integer i -> interval (Intervals.atom i)
  | Atom a -> atom (Atoms.atom a)
  | Char c -> char (Chars.atom c)
450
  | Pair (x,y) -> times (const_node x) (const_node y)
451
  | Xml (x,y) -> xml (const_node x) (const_node y)
452
  | Record x -> record_fields (false ,LabelMap.map const_node x)
453 454
  | String (i,j,s,c) ->
      if U.equal_index i j then constant c
Pietro Abate's avatar
Pietro Abate committed
455
      else
456 457 458
	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)
459

460 461
let neg x = diff any x

462
let any_node = cons any
463
let empty_node = cons empty
464

465
module LabelS = Set.Make(Label)
466

Pietro Abate's avatar
Pietro Abate committed
467
let any_or_absent = { any with absent = true }
468
let only_absent = { empty with absent = true }
469

470
let get_record r =
Pietro Abate's avatar
Pietro Abate committed
471 472
  let labs accu (_,r) =
    List.fold_left
473
      (fun accu (l,_) -> LabelS.add l accu) accu (LabelMap.get r) in
474
  let extend descrs labs (o,r) =
475 476 477 478 479
    let rec aux i labs r =
      match labs with
	| [] -> ()
	| l1::labs ->
	    match r with
Pietro Abate's avatar
Pietro Abate committed
480
	      | (l2,x)::r when l1 == l2 ->
481 482 483
		  descrs.(i) <- cap descrs.(i) (descr x);
		  aux (i+1) labs r
	      | r ->
Pietro Abate's avatar
Pietro Abate committed
484
		  if not o then
485
		    descrs.(i) <- cap descrs.(i) only_absent; (* TODO:OPT *)
486 487
		  aux (i+1) labs r
    in
488
    aux 0 labs (LabelMap.get r);
489 490 491
    o
  in
  let line (p,n) =
Pietro Abate's avatar
Pietro Abate committed
492
    let labels =
493 494
      List.fold_left labs (List.fold_left labs LabelS.empty p) n in
    let labels = LabelS.elements labels in
495
    let nlab = List.length labels in
496
    let mk () = Array.create nlab any_or_absent in
497 498

    let pos = mk () in
Pietro Abate's avatar
Pietro Abate committed
499 500
    let opos = List.fold_left
		 (fun accu x ->
501 502 503 504 505 506 507 508 509 510 511
		    (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
512
  List.map line (Rec.get r)
513

514 515 516 517 518
(* 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)
519
let cap_product any_left any_right l =
Pietro Abate's avatar
Pietro Abate committed
520
  List.fold_left
521
    (fun (d1,d2) (t1,t2) -> (cap_t d1 t1, cap_t d2 t2))
522
    (any_left,any_right)
523
    l
524
let any_pair = { empty with times = any.times }
525

526 527 528
let rec exists max f =
  (max > 0) && (f (max - 1) || exists (max - 1) f)

529
exception NotEmpty
530

531 532 533 534 535 536 537 538 539
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
540
    | WAbstract of Abstracts.elem option
541 542 543 544 545 546 547

    | 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
548
  and witness_slot =
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
      { 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
564
	| WAbstract (Some t) -> 7 + 17 * Abstracts.T.hash t
Pietro Abate's avatar
Pietro Abate committed
565
	| WPair (_,_,s)
566 567 568 569 570 571
	| 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
572
	| WRecord (r,o,_) ->
573 574 575
	    (if o then 2 else 3) + 257 * LabelMap.hash hash_small r
	| WFun (f,_) ->
	    4 + 257 *
Pietro Abate's avatar
Pietro Abate committed
576 577
	      (Hashtbl.hash
		 (List.map
578
		    (function (x,None) -> 17 * hash_small x
Pietro Abate's avatar
Pietro Abate committed
579
		       | (x,Some y) ->
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
			   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
595
	| WAbstract (Some t1), WAbstract (Some t2) -> Abstracts.T.equal t1 t2
596 597 598
	| _ -> w1 == w2

      let equal w1 w2 = match w1,w2 with
Pietro Abate's avatar
Pietro Abate committed
599 600
	| WPair (p1,q1,_), WPair (p2,q2,_)
	| WXml (p1,q1,_), WXml (p2,q2,_) ->
601 602 603 604 605 606 607 608
	    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
609
					   | Some y1, Some y2 ->
610 611 612 613 614 615 616 617 618
					       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
619
  let wslot () = { wuid = !wuid; wnodes_in = NodeSet.empty;
620 621
		   wnodes_out = NodeSet.empty }

622 623 624 625
  let () =
    Stats.register Stats.Summary
      (fun ppf -> Format.fprintf ppf "Allocated witnesses:%i@\n" !wuid)

626
  let rec pp ppf = function
627 628 629 630 631 632 633 634 635 636
    | 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
637
    | WPair (w1,w2,_) ->
638
	Format.fprintf ppf "(%a,%a)" pp w1 pp w2
Pietro Abate's avatar
Pietro Abate committed
639
    | WXml (w1,w2,_) ->
640
	Format.fprintf ppf "XML(%a,%a)" pp w1 pp w2
641 642 643
    | WRecord (ws,o,_) ->
	Format.fprintf ppf "{";
	LabelMap.iteri
Pietro Abate's avatar
Pietro Abate committed
644
	  (fun l w -> Format.fprintf ppf " %a=%a"
645
	     Label.print_attr l pp w)
646 647 648 649 650 651
	  ws;
	if o then Format.fprintf ppf " ..";
	Format.fprintf ppf " }"
    | WFun (f,_) ->
	Format.fprintf ppf "FUN{";
	List.iter (fun (x,y) ->
652
		     Format.fprintf ppf " %a->" pp x;
653 654
		     match y with
		       | None -> Format.fprintf ppf "#"
655
		       | Some y -> pp ppf y) f;
656 657 658 659 660 661 662
	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
663

664 665
  let printf = pp Format.std_formatter

666 667
  let wmk w =  (* incr wuid; w *)  (* hash-consing disabled *)
    try WHash.find wmemo w
Pietro Abate's avatar
Pietro Abate committed
668 669
    with Not_found ->
      incr wuid;
670
      WHash.add wmemo w w;
Pietro Abate's avatar
Pietro Abate committed
671
(*      Format.fprintf Format.std_formatter "W:%a@."
672
	pp w; *)
673 674 675 676 677 678 679
      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()))

680
  let bool_pair f =
Pietro Abate's avatar
Pietro Abate committed
681 682 683
    Pair.compute
      ~empty:false ~full:true
      ~cup:(||) ~cap:(&&) ~diff:(fun x y -> x && not y)
684 685 686
      ~atom:f

  let bool_rec f =
Pietro Abate's avatar
Pietro Abate committed
687 688 689
    Rec.compute
      ~empty:false ~full:true
      ~cup:(||) ~cap:(&&) ~diff:(fun x y -> x && not y)
690 691
      ~atom:f

692
  let rec node_has n = function
693 694 695 696 697 698 699 700 701
    | 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

702 703
  (* type_has checks if a witness is contained in the union of
   * the leafs of a bdd, ignoring all variables. *)
704
  and type_has t = function
705 706 707
    | 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)
Pietro Abate's avatar
Pietro Abate committed
708 709 710
    | WPair (w1,w2,_) ->
	bool_pair
	  (fun (n1,n2) -> node_has n1 w1 && node_has n2 w2)
711
	  (BoolPair.leafconj t.times)
712
    | WXml (w1,w2,_) ->
Pietro Abate's avatar
Pietro Abate committed
713
	bool_pair
714
	  (fun (n1,n2) -> node_has n1 w1 && node_has n2 w2)
715
	  (BoolPair.leafconj t.xml)
716
    | WFun (f,_) ->
Pietro Abate's avatar
Pietro Abate committed
717
	bool_pair
718 719 720 721 722 723
	  (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))
Pietro Abate's avatar
Pietro Abate committed
724
	       f)
725
	  (BoolPair.leafconj t.arrow)
726
    | WRecord (f,o,_) ->
Pietro Abate's avatar
Pietro Abate committed
727
	bool_rec
728 729 730
	  (fun (o',f') ->
	     ((not o) || o') && (
	       let checked = ref 0 in
Pietro Abate's avatar
Pietro Abate committed
731 732
	       try
		 LabelMap.iteri
733
		   (fun l n ->
Pietro Abate's avatar
Pietro Abate committed
734
		      let w =
735 736 737
			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
Pietro Abate's avatar
Pietro Abate committed
738
		   ) f';
739 740 741 742 743
		 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))
744
	  (BoolRec.leafconj t.record)
745
    | WAbsent -> t.absent
746
    | WAbstract a -> Abstracts.contains_sample a (BoolAbstracts.leafconj t.abstract)
747 748
end

Pietro Abate's avatar
Pietro Abate committed
749
type slot = { mutable status : status;
750 751
	       mutable notify : notify;
	       mutable active : bool }
752 753
and status = Empty | NEmpty of Witness.witness | Maybe
and notify = Nothing | Do of slot * (Witness.witness -> unit) * notify
754 755

let slot_empty = { status = Empty; active = false; notify = Nothing }
756 757
let slot_nempty w = { status = NEmpty w;
		     active = false; notify = Nothing }
758

759
let rec notify w = function
760
  | Nothing -> ()
Pietro Abate's avatar
Pietro Abate committed
761
  | Do (n,f,rem) ->
762 763
      if n.status == Maybe then (try f w with NotEmpty -> ());
      notify w rem
764 765 766 767 768

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

769 770 771
let set s w =
  s.status <- NEmpty w;
  notify w s.notify;
Pietro Abate's avatar
Pietro Abate committed
772
  s.notify <- Nothing;
773 774
  raise NotEmpty

775
let rec big_conj f l n w =
776
  match l with
777 778
    | [] -> set n w
    | [arg] -> f w arg n
779
    | arg::rem ->
Pietro Abate's avatar
Pietro Abate committed
780 781
	let s =
	  { status = Maybe; active = false;
782
	    notify = Do (n,(big_conj f rem n), Nothing) } in
Pietro Abate's avatar
Pietro Abate committed
783
	try
784
	  f w arg s;
785
	  if s.active then n.active <- true
786
	with NotEmpty when n.status == Empty || n.status == Maybe -> ()
787

788
let memo = DescrHash.create 8191
Pietro Abate's avatar
Pietro Abate committed
789
let marks = ref []
790

Pietro Abate's avatar
Pietro Abate committed
791
let count_subtype = Stats.Counter.create "Subtyping internal loop"
792

793 794
let complex = ref 0

795
let rec slot d =
796
  incr complex;
Pietro Abate's avatar
Pietro Abate committed
797
  Stats.Counter.incr count_subtype;
798
  (* XXX here I call leafconj a zilliontime. REWRITE !!! *)
799
  if d.absent then slot_nempty Witness.WAbsent
Pietro Abate's avatar
Pietro Abate committed
800
  else if not (Intervals.is_empty (BoolIntervals.leafconj d.ints))
801
  then slot_nempty (Witness.WInt (Intervals.sample (BoolIntervals.leafconj d.ints)))
Pietro Abate's avatar
Pietro Abate committed
802
  else if not (Atoms.is_empty (BoolAtoms.leafconj d.atoms))
803
  then slot_nempty (Witness.WAtom (Atoms.sample (BoolAtoms.leafconj d.atoms)))
Pietro Abate's avatar
Pietro Abate committed
804
  else if not (Chars.is_empty (BoolChars.leafconj d.chars))
805
  then slot_nempty (Witness.WChar (Chars.sample (BoolChars.leafconj d.chars)))
806 807
  else if not (Abstracts.is_empty (BoolAbstracts.leafconj d.abstract))
  then slot_nempty (Witness.WAbstract (Abstracts.sample (BoolAbstracts.leafconj d.abstract)))
808 809
  else try
         DescrHash.find memo d
810 811 812 813
  with Not_found ->
    let s = { status = Maybe; active = false; notify = Nothing } in
    DescrHash.add memo d s;
    (try
814
       iter_s s check_times (Pair.get (BoolPair.leafconj d.times));
Pietro Abate's avatar
Pietro Abate committed
815
       iter_s s check_xml (Pair.get (BoolPair.leafconj d.xml));
816 817
       iter_s s check_arrow (Pair.get (BoolPair.leafconj d.arrow));
       iter_s s check_record (get_record (BoolRec.leafconj d.record));
818
       if s.active then marks := s :: !marks else s.status <- Empty;
819
     with NotEmpty -> ());
820 821
    s

822 823 824 825 826
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

827
and check_times (left,right) s =
828 829 830 831
  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)
Pietro Abate's avatar
Pietro Abate committed
832
    | (n1,n2) :: rest
833 834
	when (Witness.node_has n1 w1) && (Witness.node_has n2 w2) ->
	let right = seen @ rest in
Pietro Abate's avatar
Pietro Abate committed
835
	let accu2' = diff accu2 (descr n2) in
836 837 838
	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)
839
    | k :: rest -> aux w1 w2 accu1 accu2 (k::seen) rest
840
  in
841 842
  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))
843 844

and check_xml (left,right) s =
845 846 847 848
  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)
Pietro Abate's avatar
Pietro Abate committed
849
    | (n1,n2) :: rest
850 851
	when (Witness.node_has n1 w1) && (Witness.node_has n2 w2) ->
	let right = seen @ rest in
Pietro Abate's avatar
Pietro Abate committed
852
	let accu2' = diff accu2 (descr n2) in
853 854 855
	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)
856
    | k :: rest -> aux w1 w2 accu1 accu2 (k::seen) rest
857
  in
858 859
  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))
860

861
and check_arrow (left,right) s =
862 863
  let single_right f (s1,s2) s =
    let rec aux w1 w2 accu1 accu2 left = match left with
864
      | (t1,t2)::left ->
Pietro Abate's avatar
Pietro Abate committed
865
          let accu1' = diff_t accu1 t1 in
866
          guard s accu1' (fun w1 -> aux w1 w2 accu1' accu2 left);
867

Pietro Abate's avatar
Pietro Abate committed
868
          let accu2' = cap_t  accu2 t2 in
869
          guard s accu2' (fun w2 -> aux w1 (Some w2) accu1 accu2' left)
Pietro Abate's avatar
Pietro Abate committed
870
      | [] ->
871 872
	  let f = match f with Witness.WFun (f,_) -> f | _ -> assert false in
	  set s (Witness.wfun ((w1,w2)::f))
873 874
    in
    let accu1 = descr s1 in
875
    guard s accu1 (fun w1 -> aux w1 None accu1 (neg (descr s2)) left)
876
  in
877
  big_conj single_right right s (Witness.wfun [])
878

879
and check_record (labels,(oleft,left),rights) s =
880 881
  let rec aux ws accus seen = function
    | [] ->
882 883
	let rec aux w i = function
	  | [] -> assert (i == Array.length ws); w
Pietro Abate's avatar
Pietro Abate committed
884 885
	  | l::labs ->
	      let w = match ws.(i) with
886
              | Witness.WAbsent -> w
Pietro Abate's avatar
Pietro Abate committed
887
              | wl -> LabelMap.add l wl w
888 889 890
            in
	      aux w (succ i) labs
      in
891 892
	set s (Witness.wrecord (aux LabelMap.empty 0 labels) oleft)
    | (false,_) :: rest when oleft -> aux ws accus seen rest
Pietro Abate's avatar
Pietro Abate committed
893
    | (_,f) :: rest
894 895 896 897 898 899
	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
Pietro Abate's avatar
Pietro Abate committed
900
	  guard s di (fun wi ->
901 902 903 904 905
			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
906
  in
907
  let rec start wl i =
908
    if (i < 0) then aux (Array.of_list wl) left [] rights
909
    else guard s left.(i) (fun w -> start (w::wl) (i - 1))
910
  in
911
  start [] (Array.length left - 1)
912

913
let timer_subtype = Stats.Timer.create "Types.is_empty"
914

915
let is_empty d =
916
  Stats.Timer.start timer_subtype;
917
  let s = slot d in
Pietro Abate's avatar
Pietro Abate committed
918 919 920
  List.iter
    (fun s' ->
       if s'.status == Maybe then s'.status <- Empty; s'.notify <- Nothing)
921 922
    !marks;
  marks := [];
923
  Stats.Timer.stop timer_subtype
924
    (s.status == Empty)
925

926 927 928 929
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
930

931
let non_empty d = not (is_empty d)
932

933 934 935
let disjoint d1 d2 = is_empty (cap d1 d2)

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

937 938
let equiv d1 d2 = (subtype d1 d2) && (subtype d2 d1)

939 940

(* functions on variables need the subtyping relation *)
941 942 943 944 945 946 947 948 949

module type BVS =
sig
    type elem
    type t
    val get : t -> (elem list* elem list) list
end

let get_variables main_memo temp_memo t =
950
  let get_vars_conj get_atom pos acc l =
951
    List.fold_left (fun acc e -> get_atom pos acc e) acc l
952 953 954 955 956 957 958 959 960 961 962
  in
  let get_vars_bdd (type s) (type e)
      (module BV : BVS with type elem = e and type t = s)
      get_atom _pos acc bdd
      =
    List.fold_left (fun ((tlvp, tlvn, vars) as acc) (p, n) ->
      let acc' = get_vars_conj get_atom true acc p in
      get_vars_conj  get_atom false acc' n
    ) acc (BV.get bdd)
  in
  let get_vars_boolvar get_atom pos (tlvp, tlvn, vars) = function
963 964
    | `Var v when pos -> Var.Set.add v tlvp, tlvn, Var.Set.add v vars
    | `Var v -> tlvp, Var.Set.add v tlvn,  Var.Set.add v vars
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
    | `Atm x ->
      let _, _, vars' = get_atom pos (Var.Set.empty, Var.Set.empty, vars) x
      in
      tlvp, tlvn, vars'
  in
  let get_nothing _ acc _ = acc in
  let rec get_pair pos acc (t1, t2) =
    let acc1 = get_variables pos acc (descr t1) in
    get_variables pos acc1 (descr t2)
  and get_record pos acc (_, lmap) =
    LabelMap.fold (fun _ t acc -> get_variables pos acc (descr t)) lmap acc
  and get_bdd_pair pos = get_vars_bdd (module Pair) (get_pair) pos
  and get_bdd_record pos = get_vars_bdd (module Rec) (get_record) pos
  and get_bv_pairs pos = get_vars_bdd (module BoolPair)
    (get_vars_boolvar get_bdd_pair) pos
  and get_bv_recs pos = get_vars_bdd (module BoolRec)
    (get_vars_boolvar get_bdd_record) pos
  and get_variables pos (tvpos,tvneg,vars) t =
    let tpos, tneg, tvars =
      try
985
        DescrHash.find main_memo t
986
      with
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
        Not_found -> begin
          try DescrHash.find temp_memo t with
            Not_found ->
              let acc = Var.Set.(empty,empty,empty) in
              DescrHash.add temp_memo t acc;
              let acc = get_vars_bdd (module BoolIntervals)
                (get_vars_boolvar get_nothing) pos acc t.ints in
              let acc = get_vars_bdd (module BoolChars)
                (get_vars_boolvar get_nothing) pos acc t.chars in
              let acc = get_vars_bdd (module BoolAtoms)
                (get_vars_boolvar get_nothing) pos acc t.atoms in
              let acc = get_vars_bdd (module BoolAbstracts)
                (get_vars_boolvar get_nothing) pos acc t.abstract in
              let acc = get_bv_pairs pos acc t.times in
              let acc = get_bv_pairs pos acc t.xml in
              let acc = get_bv_pairs pos acc t.arrow in
              let acc = get_bv_recs pos acc t.record in
              DescrHash.replace temp_memo t acc;
              acc
        end
1007 1008 1009 1010 1011
    in
    (Var.Set.union tvpos tpos,
     Var.Set.union tvneg tneg,
     Var.Set.union tvars vars)
  in
1012 1013 1014 1015 1016 1017 1018 1019 1020
  get_variables true Var.Set.(empty,empty,empty) t

let get_variables =
  let main_memo = DescrHash.create 17 in
  fun t ->
    try DescrHash.find main_memo t
    with Not_found ->
      let res = get_variables main_memo (DescrHash.create 17) t in
      DescrHash.add main_memo t res;
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
      res

let check_var =
  let aux t =
    let tvpos, tvneg, tvars = get_variables t in
    match Var.Set.(cardinal tvpos, cardinal tvneg, cardinal tvars) with
      1, 0, 1 -> let v = Var.Set.choose tvpos in
                if equiv (var v) t then `Pos v else `NotVar
    | 0, 1, 1 -> let v = Var.Set.choose tvneg in
                if equiv (diff any (var v)) t then `Neg v else `NotVar
    | _ -> `NotVar
  in
  let memo_descr = DescrHash.create 17 in
  fun t -> try DescrHash.find memo_descr t with
    Not_found ->
      let res = aux t in
      DescrHash.add memo_descr t res;
      res

let is_var t = match check_var t with `NotVar -> false | _ -> true
let no_var t =
  let _, _, s = get_variables t in Var.Set.is_empty s

let has_tlv t =
  let p, n, _ = get_variables t in
  not Var.Set.(is_empty p && is_empty n)

let all_vars t =
  let _, _, s = get_variables t in s

let all_tlv t =
  let p , n, _ = get_variables t in Var.Set.union p n

let is_closed delta t =
  Var.Set.(is_empty (diff (all_vars t) delta))

let extract_variable t =
  match check_var t with
    `Pos v -> v, true
  | `Neg v -> v, false
  | _ -> assert false