types.ml 104 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

Jérôme Maloberti's avatar
Jérôme Maloberti committed
30 31
type service_params =
  | TProd of service_params * service_params
Pietro Abate's avatar
Pietro Abate committed
32
  | TOption of service_params
Jérôme Maloberti's avatar
Jérôme Maloberti committed
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
Jérôme Maloberti's avatar
Jérôme Maloberti committed
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
Jérôme Maloberti's avatar
Jérôme Maloberti committed
45
  | TCoordv of service_params * string
Pietro Abate's avatar
Pietro Abate committed
46
  | TESuffix of string
Jérôme Maloberti's avatar
Jérôme Maloberti committed
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
Jérôme Maloberti's avatar
Jérôme Maloberti committed
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

140 141
module TLV = struct

Pietro Abate's avatar
Pietro Abate committed
142 143 144 145 146 147 148 149 150 151 152
  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)
153 154 155 156
    let pp_aux ppf pp_elem s = 
      let f ppf = function
        |(v,true) -> Format.fprintf ppf "%a" pp_elem v
        |(v,false) -> Format.fprintf ppf "~ %a" pp_elem v
Pietro Abate's avatar
Pietro Abate committed
157
      in
158 159 160 161 162
      Utils.pp_list ~sep:";" ~delim:("{","}") f ppf (elements s)
    let dump ppf s = pp_aux ppf Var.dump s
    let pp ppf s = pp_aux ppf Var.pp s

    let printf = pp Format.std_formatter
Pietro Abate's avatar
Pietro Abate committed
163
  end
164

Pietro Abate's avatar
Pietro Abate committed
165
  (* tlv : top level variables
Julien Lopez's avatar
Julien Lopez committed
166 167
   * fv : all free variables in the subtree *)
  type t =  { tlv : Set.t ; fv : Var.Set.t; isvar : bool }
168

Julien Lopez's avatar
Julien Lopez committed
169 170
  let empty = { tlv = Set.empty ; fv = Var.Set.empty; isvar = false }
  let any = { tlv = Set.empty ; fv = Var.Set.empty; isvar = false }
171

Julien Lopez's avatar
Julien Lopez committed
172
  let singleton (v,p) = { tlv = Set.singleton (v,p); fv = Var.Set.singleton v; isvar = true }
173 174

  (* return the max of top level variables *)
Pietro Abate's avatar
Pietro Abate committed
175
  let max x = Set.max_elt x.tlv
176 177

  let pair x y = {
Pietro Abate's avatar
Pietro Abate committed
178
    tlv = Set.empty ;
Julien Lopez's avatar
Julien Lopez committed
179 180 181 182 183 184 185 186
    fv = Var.Set.union x.fv y.fv ;
    isvar = false
  }

  let record x = {
    tlv = Set.empty ;
    fv = x.fv ;
    isvar = false
187 188 189
  }

  (* true if it contains only one variable *)
Pietro Abate's avatar
Pietro Abate committed
190
  let is_single t = t.isvar && (Var.Set.cardinal t.fv = 1) && (Set.cardinal t.tlv = 1)
Pietro Abate's avatar
Pietro Abate committed
191

Pietro Abate's avatar
Pietro Abate committed
192
  let no_variables t = (Var.Set.cardinal t.fv = 0) && (Set.cardinal t.tlv = 0)
193

Pietro Abate's avatar
Pietro Abate committed
194
  let has_toplevel t = Set.cardinal t.tlv > 0
Pietro Abate's avatar
Pietro Abate committed
195

196 197 198 199
  let pp ppf x = Set.pp ppf x.tlv
  let dump ppf x = Format.fprintf ppf "<fv = %a ; tlv = %a>" Var.Set.dump x.fv Set.dump x.tlv
  let printf = pp Format.std_formatter

Pietro Abate's avatar
Pietro Abate committed
200
  let mem v x = Set.mem v x.tlv
Pietro Abate's avatar
Pietro Abate committed
201

202
end
203

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

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

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

Julien Lopez's avatar
Julien Lopez committed
286 287 288 289 290 291 292 293 294 295 296 297
  (*
   * 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;
    record= BoolRec.full;
    ints  = BoolIntervals.full;
    atoms = BoolAtoms.full;
    chars = BoolChars.full;
298
    abstract = BoolAbstracts.any;
Julien Lopez's avatar
Julien Lopez committed
299 300 301 302
    absent= false;
    toplvars = TLV.any
  }

303 304 305 306 307 308 309 310
  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;
311
    BoolAbstracts.check a.abstract;
312 313
    ()

314
  let equal a b =
315
    (a == b) || (
316 317 318
      (BoolAtoms.equal a.atoms b.atoms) &&
      (BoolChars.equal a.chars b.chars) &&
      (BoolIntervals.equal a.ints  b.ints) &&
319 320 321 322
      (BoolPair.equal a.times b.times) &&
      (BoolPair.equal a.xml b.xml) &&
      (BoolPair.equal a.arrow b.arrow) &&
      (BoolRec.equal a.record b.record) &&
323
      (BoolAbstracts.equal a.abstract b.abstract) &&
324 325
      (a.absent == b.absent)
    )
326

Julien Lopez's avatar
Julien Lopez committed
327 328 329 330 331 332 333 334
  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) &&
335
      (BoolAbstracts.is_empty a.abstract)
Julien Lopez's avatar
Julien Lopez committed
336

337
  let compare a b =
Pietro Abate's avatar
Pietro Abate committed
338
    if a == b then 0
339 340 341
    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
342 343 344 345
    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
346
    else let c = BoolAbstracts.compare a.abstract b.abstract in if c <> 0 then c
347 348 349
    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
350

351
  let hash a =
352 353 354
    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
355 356 357 358
    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
359
    let accu = 17 * accu + BoolAbstracts.hash a.abstract in
360 361
    let accu = if a.absent then accu+5 else accu in
    accu
362

363 364 365
end
and Node :
sig
366
  type t = { id : int; cu: Compunit.t; mutable descr : Descr.t }
367 368 369 370 371
  val dump: Format.formatter -> t -> unit
  val check: t -> unit
  val equal: t -> t -> bool
  val hash: t -> int
  val compare:t -> t -> int
372
  val mk: int -> Descr.t -> t
373
end =
374

375
struct
376
  type t = { id : int; cu: Compunit.t; mutable descr : Descr.t }
377
  let check n = ()
378
  let dump ppf n = Format.fprintf ppf "X%i" n.id
379
  let hash x = x.id + Compunit.hash x.cu
Pietro Abate's avatar
Pietro Abate committed
380
  let compare x y =
381 382 383
    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 }
384 385
end

386 387
and Pair : Bool.S with type elem = (Node.t * Node.t) =
  Bool.Make(Custom.Pair(Node)(Node))
388 389
and BoolPair : BoolVar.S with type s = Pair.t = 
  BoolVar.Make(Pair)
390

391 392 393 394 395
(* 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)))
396 397
and BoolRec : BoolVar.S with
  type s = Rec.t = BoolVar.Make(Rec)
398

399 400
module DescrHash = Hashtbl.Make(Descr)
module DescrMap = Map.Make(Descr)
401 402
module DescrSet = Set.Make(Descr)
module DescrSList = SortedList.Make(Descr)
403

404 405 406
type descr = Descr.t
type node = Node.t
include Descr
407

408 409
let forward_print = ref (fun _ _ -> assert false)

Pietro Abate's avatar
Pietro Abate committed
410 411
let make () =
  incr count;
412 413
  Node.mk !count empty

Pietro Abate's avatar
Pietro Abate committed
414
let define n d =
415 416
  n.Node.descr <- d

Pietro Abate's avatar
Pietro Abate committed
417 418
let cons d =
  incr count;
419 420
  Node.mk !count d

421 422 423 424
let descr n = n.Node.descr
let internalize n = n
let id n = n.Node.id

425
let non_constructed =
Pietro Abate's avatar
Pietro Abate committed
426
  { any with
427
      times = empty.times; xml = empty.xml; record = empty.record }
Pietro Abate's avatar
Pietro Abate committed
428 429

let non_constructed_or_absent =
430
  { non_constructed with absent = true }
431

Pietro Abate's avatar
Pietro Abate committed
432
(* Descr.t type constructors *)
433 434
let times x y = { empty with
                    times = BoolPair.atom (`Atm (Pair.atom (x,y)));
Pietro Abate's avatar
Pietro Abate committed
435
                    toplvars = TLV.pair (descr x).toplvars (descr y).toplvars }
436
let xml x y = { empty with
Pietro Abate's avatar
Pietro Abate committed
437 438
                    xml = BoolPair.atom (`Atm (Pair.atom (x,y)));
                    toplvars = TLV.pair (descr x).toplvars (descr y).toplvars }
439 440
let arrow x y = { empty with
                    arrow = BoolPair.atom (`Atm (Pair.atom (x,y)));
Pietro Abate's avatar
Pietro Abate committed
441
                    toplvars = TLV.pair (descr x).toplvars (descr y).toplvars }
442

Pietro Abate's avatar
Pietro Abate committed
443 444
let record label t =
  { empty with
445
    record = BoolRec.atom (`Atm (Rec.atom (true,LabelMap.singleton label t)));
Julien Lopez's avatar
Julien Lopez committed
446
    toplvars = TLV.record (descr t).toplvars }
447 448 449

let tlv_from_rec (_, l) =
  let rec aux acc = function
Julien Lopez's avatar
Julien Lopez committed
450
    | (_, n) :: rest -> aux (TLV.pair acc (descr n).toplvars) rest
451 452 453
    | [] -> acc in
  aux TLV.empty (Ident.LabelMap.get l)

454
let record_fields x =
455 456
  { empty with record = BoolRec.atom (`Atm (Rec.atom x));
    toplvars = tlv_from_rec x }
457

458
let atom a = { empty with atoms = BoolAtoms.atom (`Atm a) }
459 460

(* Atm = Any ^ a *)
461 462
let var a =  {
  times = BoolPair.vars a;
Pietro Abate's avatar
Pietro Abate committed
463 464 465
  xml   = BoolPair.vars a;
  arrow = BoolPair.vars a;
  record= BoolRec.vars a;
466 467 468
  ints  = BoolIntervals.vars a;
  atoms = BoolAtoms.vars a;
  chars = BoolChars.vars a;
469
  abstract = BoolAbstracts.empty;
470
  absent= false;
471
  toplvars = TLV.singleton (a,true)
472 473
}

474 475
(* XXX this function could be potentially costly. There should be
 * better way to take trace of top level variables in a type *)
Pietro Abate's avatar
Pietro Abate committed
476 477 478 479
let update_tlv x y t =
  let open TLV in
  let tlv t =
    let aux get bdd : Set.t =
480
      let l =
Pietro Abate's avatar
Pietro Abate committed
481
        List.fold_left (fun acc (p,n) ->
482
          let acc1 =
Pietro Abate's avatar
Pietro Abate committed
483 484 485 486 487
            List.fold_left (fun acc -> function
              |(`Var v) as x -> Set.union acc (Set.singleton (x,true))
              |_ -> acc
            ) Set.empty p
          in
488
          let acc2 =
Pietro Abate's avatar
Pietro Abate committed
489 490 491 492 493 494 495 496 497 498
            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
Julien Lopez's avatar
Julien Lopez committed
499
      |h::l -> List.fold_left Set.union h l
Pietro Abate's avatar
Pietro Abate committed
500
    in
Julien Lopez's avatar
Julien Lopez committed
501
    List.fold_left Set.union
502
    (aux BoolChars.get t.chars)
503 504 505 506 507 508 509
    [
      (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)
    ]
Pietro Abate's avatar
Pietro Abate committed
510
  in
511
  let fv t =
Julien Lopez's avatar
Julien Lopez committed
512
    if Descr.is_empty t || equal t Descr.any then Var.Set.empty
513 514 515 516
    else Var.Set.union x.toplvars.fv y.toplvars.fv
  in
  let toplvars = { tlv = tlv t ; fv = fv t ; isvar = t.toplvars.isvar } in
  { t with toplvars = toplvars }
Pietro Abate's avatar
Pietro Abate committed
517 518
;;

519 520
let char c = { empty with chars = BoolChars.atom (`Atm c) }
let interval i = { empty with ints = BoolIntervals.atom (`Atm i) }
521
let abstract a = { empty with abstract = BoolAbstracts.atom (`Atm a) }
522

523 524 525 526 527 528 529
let is_var t = TLV.is_single t.toplvars
let no_var t = TLV.no_variables t.toplvars
let has_tlv t = TLV.has_toplevel t.toplvars

let all_vars t = t.toplvars.TLV.fv
let all_tlv t = t.toplvars.TLV.tlv

Pietro Abate's avatar
Pietro Abate committed
530 531
let is_closed delta t = (no_var t) || (Var.Set.is_empty (Var.Set.diff (all_vars t) delta))

Pietro Abate's avatar
Pietro Abate committed
532
let cup x y =
533 534 535 536 537 538 539 540 541
  if x == y then x else
    let t = {
      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;
542
      abstract = BoolAbstracts.cup x.abstract y.abstract;
543 544 545
      absent= x.absent || y.absent;
      toplvars = TLV.empty
    } in
546
    let isvar = (is_var x && equal x t) || (is_var y && equal y t) in
547
    update_tlv x y { t with toplvars = { t.toplvars with TLV.isvar = isvar }}
548

Pietro Abate's avatar
Pietro Abate committed
549
let cap x y =
550 551 552 553 554 555 556 557 558
  if x == y then x else
    let t = {
      ints  = BoolIntervals.cap x.ints y.ints;
      times = BoolPair.cap x.times y.times;
      xml   = BoolPair.cap x.xml y.xml;
      record= BoolRec.cap x.record y.record;
      arrow = BoolPair.cap x.arrow y.arrow;
      atoms = BoolAtoms.cap x.atoms y.atoms;
      chars = BoolChars.cap x.chars y.chars;
559
      abstract = BoolAbstracts.cap x.abstract y.abstract;
560 561 562
      absent= x.absent && y.absent;
      toplvars = TLV.empty
    } in
Julien Lopez's avatar
Julien Lopez committed
563
    let isvar = (is_var x && equal x t) || (is_var y && equal y t) in
564
    update_tlv x y { t with toplvars = { t.toplvars with TLV.isvar = isvar }}
565

Pietro Abate's avatar
Pietro Abate committed
566
let diff x y =
567 568 569 570 571 572 573 574 575
  if x == y then empty else
    let t = {
      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;
576
      abstract = BoolAbstracts.diff x.abstract y.abstract;
577 578 579
      absent= x.absent && not y.absent;
      toplvars = TLV.empty
    } in
580
    let isvar = (equal x any && is_var y) || (is_var x && equal y empty) in
581
    update_tlv x y { t with toplvars = { t.toplvars with TLV.isvar = isvar }}
Pietro Abate's avatar
Pietro Abate committed
582

583 584
(* TODO: optimize disjoint check for boolean combinations *)
let trivially_disjoint a b =
585 586 587
  (BoolChars.trivially_disjoint a.chars b.chars) &&
  (BoolIntervals.trivially_disjoint a.ints b.ints) &&
  (BoolAtoms.trivially_disjoint a.atoms b.atoms) &&
588 589 590
  (BoolPair.trivially_disjoint a.times b.times) &&
  (BoolPair.trivially_disjoint a.xml b.xml) &&
  (BoolPair.trivially_disjoint a.arrow b.arrow) &&
591
  (BoolRec.trivially_disjoint a.record b.record) &&
592
  (BoolAbstracts.trivially_disjoint a.abstract b.abstract) &&
593
  (not (a.absent && b.absent))
594

595
let rec constant = function
596 597 598
  | Integer i -> interval (Intervals.atom i)
  | Atom a -> atom (Atoms.atom a)
  | Char c -> char (Chars.atom c)
599
  | Pair (x,y) -> times (const_node x) (const_node y)
600
  | Xml (x,y) -> xml (const_node x) (const_node y)
601
  | Record x -> record_fields (false ,LabelMap.map const_node x)
602 603
  | String (i,j,s,c) ->
      if U.equal_index i j then constant c
Pietro Abate's avatar
Pietro Abate committed
604
      else
605 606 607
	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)
608

609 610
let neg x = diff any x

611
let any_node = cons any
612
let empty_node = cons empty
613

614
module LabelS = Set.Make(Label)
615

Pietro Abate's avatar
Pietro Abate committed
616
let any_or_absent = { any with absent = true }
617
let only_absent = { empty with absent = true }
618

619
let get_record r =
Pietro Abate's avatar
Pietro Abate committed
620 621
  let labs accu (_,r) =
    List.fold_left
622
      (fun accu (l,_) -> LabelS.add l accu) accu (LabelMap.get r) in
623
  let extend descrs labs (o,r) =
624 625 626 627 628
    let rec aux i labs r =
      match labs with
	| [] -> ()
	| l1::labs ->
	    match r with
Pietro Abate's avatar
Pietro Abate committed
629
	      | (l2,x)::r when l1 == l2 ->
630 631 632
		  descrs.(i) <- cap descrs.(i) (descr x);
		  aux (i+1) labs r
	      | r ->
Pietro Abate's avatar
Pietro Abate committed
633
		  if not o then
634
		    descrs.(i) <- cap descrs.(i) only_absent; (* TODO:OPT *)
635 636
		  aux (i+1) labs r
    in
637
    aux 0 labs (LabelMap.get r);
638 639 640
    o
  in
  let line (p,n) =
Pietro Abate's avatar
Pietro Abate committed
641
    let labels =
642 643
      List.fold_left labs (List.fold_left labs LabelS.empty p) n in
    let labels = LabelS.elements labels in
644
    let nlab = List.length labels in
645
    let mk () = Array.create nlab any_or_absent in
646 647

    let pos = mk () in
Pietro Abate's avatar
Pietro Abate committed
648 649
    let opos = List.fold_left
		 (fun accu x ->
650 651 652 653 654 655 656 657 658 659 660
		    (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
661
  List.map line (Rec.get r)
662

663 664 665 666 667
(* 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)
668
let cap_product any_left any_right l =
Pietro Abate's avatar
Pietro Abate committed
669
  List.fold_left
670
    (fun (d1,d2) (t1,t2) -> (cap_t d1 t1, cap_t d2 t2))
671
    (any_left,any_right)
672
    l
673
let any_pair = { empty with times = any.times }
674

675 676 677
let rec exists max f =
  (max > 0) && (f (max - 1) || exists (max - 1) f)

678
exception NotEmpty
679

680 681 682 683 684 685 686 687 688
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
689
    | WAbstract of Abstracts.elem option
690 691 692 693 694 695 696

    | 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
697
  and witness_slot =
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
      { 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
713
	| WAbstract (Some t) -> 7 + 17 * Abstracts.T.hash t
Pietro Abate's avatar
Pietro Abate committed
714
	| WPair (_,_,s)
715 716 717 718 719 720
	| 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
721
	| WRecord (r,o,_) ->
722 723 724
	    (if o then 2 else 3) + 257 * LabelMap.hash hash_small r
	| WFun (f,_) ->
	    4 + 257 *
Pietro Abate's avatar
Pietro Abate committed
725 726
	      (Hashtbl.hash
		 (List.map
727
		    (function (x,None) -> 17 * hash_small x
Pietro Abate's avatar
Pietro Abate committed
728
		       | (x,Some y) ->
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
			   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
744
	| WAbstract (Some t1), WAbstract (Some t2) -> Abstracts.T.equal t1 t2
745 746 747
	| _ -> w1 == w2

      let equal w1 w2 = match w1,w2 with
Pietro Abate's avatar
Pietro Abate committed
748 749
	| WPair (p1,q1,_), WPair (p2,q2,_)
	| WXml (p1,q1,_), WXml (p2,q2,_) ->
750 751 752 753 754 755 756 757
	    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
758
					   | Some y1, Some y2 ->
759 760 761 762 763 764 765 766 767
					       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
768
  let wslot () = { wuid = !wuid; wnodes_in = NodeSet.empty;
769 770
		   wnodes_out = NodeSet.empty }

771 772 773 774
  let () =
    Stats.register Stats.Summary
      (fun ppf -> Format.fprintf ppf "Allocated witnesses:%i@\n" !wuid)