boolVar.ml 15.1 KB
Newer Older
1 2 3 4
let (<) : int -> int -> bool = (<)
let (>) : int -> int -> bool = (>)
let (=) : int -> int -> bool = (=)

5
(* this is the the of the Constructor container *)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
module type E =
sig
  type elem
  include Custom.T

  val empty : t
  val full  : t
  val cup   : t -> t -> t
  val cap   : t -> t -> t
  val diff  : t -> t -> t
  val atom  : elem -> t

end

module type S =
sig
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  type s
  type elem = s Custom.pairvar
  type 'a bdd =
    [ `True
    | `False
    | `Split of int * 'a * ('a bdd) * ('a bdd) * ('a bdd) ]

  (*
  include Custom.T 
  *)
  type t = elem bdd
  val dump : Format.formatter -> t -> unit
  val check : t -> unit
  val equal : t -> t -> bool
  val hash : t -> int
  val compare : t -> t -> int
38

39
  (* returns the union of all leaves in the BDD *)
40
  val get: t -> s
41 42

  (* val get': t -> (elem list * (elem list) list) list *)
43 44 45

  val empty : t
  val full  : t
Pietro Abate's avatar
Pietro Abate committed
46 47 48
  (* same as full, but we keep it for the moment to avoid chaging 
   * the code everywhere *)
  val any  : t
49 50 51 52
  val cup   : t -> t -> t
  val cap   : t -> t -> t
  val diff  : t -> t -> t
  val atom  : elem -> t
53
  val vars  : Custom.var -> t
54 55 56 57 58 59 60 61 62 63 64 65

  val iter: (elem-> unit) -> t -> unit

  val compute: empty:'b -> full:'b -> cup:('b -> 'b -> 'b) 
    -> cap:('b -> 'b -> 'b) -> diff:('b -> 'b -> 'b) ->
    atom:(elem -> 'b) -> t -> 'b

  val is_empty : t -> bool

  val print: string -> t -> (Format.formatter -> unit) list

  val trivially_disjoint: t -> t -> bool
66

67 68
  val extractvars : t -> [> `Var of Custom.String.t ] bdd * t 

69 70
end

71 72 73 74
(*
module type MAKE = functor (T : E) -> S with type elem = T.t Custom.pairvar 
*)

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
(* ternary BDD
 * where the nodes are Atm of X.t | Var of String.t
 * Variables are always before Values
 * All the leaves are then base types 
 *
 * we add a third case when two leaves of the bdd are of the same
 * kind, that's it Val of t1 , Val of t2
 *
 * This representation can be used for all kinds.
 * Intervals, Atoms and Chars can be always merged (for union and intersection)
 * Products can be merged for intersections
 * Arrows can be never merged
 *
 * extract_var : copy the orginal tree and on one copy put to zero all 
 * leaves that have an Atm on the other all leaves that have a Var
 *
 * *)

93
module Make(T : E) : S with type s = T.t =
94 95 96
struct
  (* ternary decision trees . cf section 11.3.3 Frish PhD *)
  (* plus variables *)
97 98
  (* `Atm are containers (Atoms, Chars, Intervals, Pairs ... )
   * `Var are String
99
   *)
100
  type s = T.t
101
  module X = Custom.Var(T)
102 103 104 105 106 107 108 109
  type elem = s Custom.pairvar
  type 'a bdd =
    [ `True
    | `False
    | `Split of int * 'a * ('a bdd) * ('a bdd) * ('a bdd) ]
  type t = elem bdd

  let rec equal_aux eq a b =
110 111
    (a == b) ||
    match (a,b) with
112
      | `Split (h1,x1,p1,i1,n1), `Split (h2,x2,p2,i2,n2) ->
113
	  (h1 == h2) &&
114 115
	  (equal_aux eq p1 p2) && (equal_aux eq i1 i2) &&
	  (equal_aux eq n1 n2) && (eq x1 x2)
116 117
      | _ -> false

118 119
  let equal = equal_aux X.equal

120 121 122 123 124 125
(* Idea: add a mutable "unique" identifier and set it to
   the minimum of the two when egality ... *)

  let rec compare a b =
    if (a == b) then 0 
    else match (a,b) with
126
      | `Split (h1,x1, p1,i1,n1), `Split (h2,x2, p2,i2,n2) ->
127 128 129 130 131
	  if h1 < h2 then -1 else if h1 > h2 then 1 
	  else let c = X.compare x1 x2 in if c <> 0 then c
	  else let c = compare p1 p2 in if c <> 0 then c
	  else let c = compare i1 i2 in if c <> 0 then c 
	  else compare n1 n2
132 133 134 135
      | `True,_  -> -1
      | _, `True -> 1
      | `False,_ -> -1
      | _,`False -> 1
136 137

  let rec hash = function
138 139 140
    | `True -> 1
    | `False -> 0
    | `Split(h, _,_,_,_) -> h
141 142

  let compute_hash x p i n = 
143
	(Hashtbl.hash x) + 17 * (hash p) + 257 * (hash i) + 16637 * (hash n)
144 145

  let rec check = function
146 147 148
    | `True -> assert false;
    | `False -> ()
    | `Split (h,x,p,i,n) ->
149
	assert (h = compute_hash x p i n);
150 151 152
	(match p with `Split (_,y,_,_,_) -> assert (X.compare x y < 0) | _ -> ());
	(match i with `Split (_,y,_,_,_) -> assert (X.compare x y < 0) | _ -> ());
	(match n with `Split (_,y,_,_,_) -> assert (X.compare x y < 0) | _ -> ());
153 154 155 156
	X.check x; check p; check i; check n

  let atom x =
    let h = X.hash x + 17 in (* partial evaluation of compute_hash... *)
157
    `Split (h, x,`True,`False,`False)
158 159 160
 
  let neg_atom x =
    let h = X.hash x + 16637 in (* partial evaluation of compute_hash... *)
161 162 163 164 165 166
    `Split (h, x,`False,`False,`True)

  let vars v =
    let a = atom (`Atm T.full) in 
    let h = compute_hash v a `False `False in 
    ( `Split (h,v,a,`False,`False) :> t )
167 168

  let rec iter f = function
169
    | `Split (_, x, p,i,n) -> f x; iter f p; iter f i; iter f n
170 171 172
    | _ -> ()

  let rec dump ppf = function
173 174 175 176 177
    | `True -> Format.fprintf ppf "+"
    | `False -> Format.fprintf ppf "-"
    | `Split (_,x, p,i,n) -> 
	Format.fprintf ppf "%a(@[%a,%a,%a@])" 
	X.dump x (*X.hash x*) dump p dump i dump n
178 179

  let rec print f ppf = function
180 181 182
    | `True -> Format.fprintf ppf "Any"
    | `False -> Format.fprintf ppf "Empty"
    | `Split (_, x, p,i, n) ->
183 184 185
	let flag = ref false in
	let b () = if !flag then Format.fprintf ppf " | " else flag := true in
	(match p with 
186 187
	   | `True -> b(); Format.fprintf ppf "%a" f x
	   | `False -> ()
188 189
	   | _ -> b (); Format.fprintf ppf "%a & @[(%a)@]" f x (print f) p );
	(match i with 
190 191
	   | `True -> assert false;
	   | `False -> ()
192 193
	   | _ -> b(); print f ppf i);
	(match n with 
194 195
	   | `True -> b (); Format.fprintf ppf "@[~%a@]" f x
	   | `False -> ()
196 197 198
	   | _ -> b (); Format.fprintf ppf "@[~%a@] & @[(%a)@]" f x (print f) n)
	
  let print a = function
199 200
    | `True -> [ fun ppf -> Format.fprintf ppf "%s" a ]
    | `False -> []
201 202
    | c -> [ fun ppf -> print X.dump ppf c ]

203
  (*
204
  let rec get accu pos neg = function
205 206 207 208
    | `True -> (pos,neg) :: accu
    | `False -> accu
    | `Split (_,x, p,i,n) ->
	(*OPT: can avoid creating this list cell when pos or neg =`False *)
209 210 211 212 213 214
	let accu = get accu (x::pos) neg p in
	let accu = get accu pos (x::neg) n in
	let accu = get accu pos neg i in
	accu
	  
  let get x = get [] [] [] x
215 216 217
  *)

  let rec get accu = function
218 219 220 221 222
    | `True -> accu
    | `False -> accu
    | `Split (_,`Atm x, `True,`False,`False) -> x :: accu
    | `Split (_,`Atm x, _,_,_) -> assert false
    | `Split (_,`Var x, p,i,n) ->
223 224 225 226 227 228
	let accu = get accu p in
	let accu = get accu n in
	let accu = get accu i in
	accu

  let get x = List.fold_left T.cup T.empty (get [] x)
229

230
(*      
231
  let rec get' accu pos neg = function
232 233 234
    | `True -> (pos,neg) :: accu
    | `False -> accu
    | `Split (_,x,p,i,n) ->
235 236
	let accu = get' accu (x::pos) neg p in
	let rec aux l = function
237
	  | `Split (_,x,`False,i,n') when equal n n' ->
238 239 240 241 242 243 244 245
	      aux (x :: l) i
	  | i ->
	      let accu = get' accu pos (l :: neg) n in
	      get' accu pos neg i
	in
	aux [x] i

  let get' x = get' [] [] [] x
246
  *)
247 248 249

  let compute ~empty ~full ~cup ~cap ~diff ~atom b =
    let rec aux = function
250 251 252
      | `True -> full
      | `False -> empty
      | `Split(_,x, p,i,n) ->
253 254 255 256 257 258 259 260 261 262
	  let p = cap (atom x) (aux p)
	  and i = aux i
	  and n = diff (aux n) (atom x) in
	  cup (cup p i) n
    in
    aux b
      
(* Invariant: correct hash value *)

  let split0 x pos ign neg =
263
    `Split (compute_hash x pos ign neg, x, pos, ign, neg)
264

265 266 267
  let empty = `False
  let full = split0 (`Atm T.full) `True `False `False
  let any = full
268 269 270 271

  let is_empty t = (t == empty)

(* Invariants:
272
     `Split (x, pos,ign,neg) ==>  (ign <> `True), (pos <> neg)
273 274 275 276
*)

  let rec has_true = function
    | [] -> false
277
    | `True :: _ -> true
278 279
    | _ :: l -> has_true l

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  (* split removes redundant subtrees from the positive and negative
   * branch if they are present in the lazy union branch *)
  let gensplit compare normalize normunion is_empty =
    let equal = equal_aux (fun a b -> compare a b = 0) in
    let rec has_same a = function
      | [] -> false
      | b :: l -> (equal a b) || (has_same a l)
    in
    let rec split x p i n =
      if is_empty x  then `False
      (* 0?p:i:n -> 0 *)
      else if i == `True then `True 
      (* x?p:1:n -> 1 *)
      else if equal p n then p ++ i
      else let p = simplify p [i] and n = simplify n [i] in
      (* x?p:i:n when p = n -> bdd of (p ++ i) *)
      if equal p n then p ++ i
      else split0 x p i n

    (* simplify t l -> bdd of ( t // l ) *)
    and simplify a l =
      match normalize a with
        | `False -> `False
        | `True -> if has_true l then `False else `True
        | `Split (_,x,p,i,n) ->
          if (has_true l) || (has_same a l) then `False
          else s_aux2 a x p i n [] [] [] l
    and s_aux2 a x p i n ap ai an = function
      | [] -> 
        let p = simplify p ap 
        and n = simplify n an
        and i = simplify i ai in
        if equal p n then p ++ i else split0 x p i n
      | b :: l -> s_aux3 a x p i n ap ai an l b 
    and s_aux3 a x p i n ap ai an l = function
      | `False -> s_aux2 a x p i n ap ai an l
      | `True -> assert false
      | `Split (_,x2,p2,i2,n2) as b ->
        if equal a b then `False 
        else let c = compare x2 x in
        if c < 0 then s_aux3 a x p i n ap ai an l i2
        else if c > 0 then s_aux2 a x p i n (b :: ap) (b :: ai) (b :: an) l
        else s_aux2 a x p i n (p2 :: i2 :: ap) (i2 :: ai) (n2 :: i2 :: an) l

    (* Inv : all leafs are of type Atm and they are always merged *)
    (* union *)
    and ( ++ ) a b = if a == b then a
    else match normunion (a,b) with
      | `True, _ | _, `True -> `True
      | `False, a | a, `False -> a
      
      | `Split (_,x1, p1,i1,n1), `Split (_,x2, p2,i2,n2) ->
        let c = compare x1 x2 in
        if c = 0 then split x1 (p1 ++ p2) (i1 ++ i2) (n1 ++ n2)
        else if c < 0 then split x1 p1 (i1 ++ b) n1
        else split x2 p2 (i2 ++ a) n2

    in split,(++)

    (*
  let splitvar,_ = gensplit Pervasives.compare (fun x -> x) (fun x -> x) (fun _ -> false)
*)


  let split,(++) = 
    let norm = function 
      | `Split (_,`Atm x, `False,`False,`True) -> split0 (`Atm(T.diff T.full x)) `True `False `False 
      | x -> x
    in
    let normunion = function
        | `Split (_,`Atm x1, `True,`False,`False), `Split (_,`Atm x2, `True,`False,`False) ->
            split0 (`Atm (T.cup x1 x2)) `True `False `False,`False

        | `Split (_,`Atm x1, `False,`False,`True), `Split (_,`Atm x2, `False,`False,`True) ->
            split0 (`Atm (T.cup (T.diff T.full x1) (T.diff T.full x2))) `True `False `False,`False

        | `Split (_,`Atm x1, `True,`False,`False), `Split (_,`Atm x2, `False,`False,`True) ->
            split0 (`Atm (T.cup x1 (T.diff T.full x2))) `True `False `False,`False

        | `Split (_,`Atm x1, `False,`False,`True), `Split (_,`Atm x2, `True,`False,`False) ->
            split0 (`Atm (T.cup (T.diff T.full x1) x2)) `True `False `False,`False

        |a,b -> a,b
    in
      gensplit X.compare norm normunion (fun x -> X.equal (`Atm T.empty) x)
365 366 367 368 369 370

(* seems better not to make ++ and this split mutually recursive;
   is the invariant still inforced ? *)

  (* intersection *)
  let rec ( ** ) a b = if a == b then a else match (a,b) with
371 372
    | `True, a | a, `True -> a
    | `False, _ | _, `False -> `False
373

374 375
    | `Split (_,`Atm x1, `True,`False,`False), `Split (_,`Atm x2, `True,`False,`False) ->
        split (`Atm(T.cap x1 x2)) `True `False `False
376

377 378
    | `Split (_,`Atm x1, `False,`False,`True), `Split (_,`Atm x2, `False,`False,`True) ->
        split (`Atm(T.cap (T.diff T.full x1) (T.diff T.full x2))) `True `False `False
379

380 381
    | `Split (_,`Atm x1, `True,`False,`False), `Split (_,`Atm x2, `False,`False,`True) ->
        split (`Atm(T.cap x1 (T.diff T.full x2))) `True `False `False
382

383 384
    | `Split (_,`Atm x1, `False,`False,`True), `Split (_,`Atm x2, `True,`False,`False) ->
        split (`Atm(T.cap (T.diff T.full x1) x2)) `True `False `False
385

386
    | `Split (_,x1, p1,i1,n1), `Split (_,x2, p2,i2,n2) ->
387 388 389 390 391 392 393 394 395 396
	let c = X.compare x1 x2 in
	if c = 0 then
	  split x1 
	    (p1 ** (p2 ++ i2) ++ (p2 ** i1))
	    (i1 ** i2)
	    (n1 ** (n2 ++ i2) ++ (n2 ** i1))  
	else if c < 0 then split x1 (p1 ** b) (i1 ** b) (n1 ** b)
	else split x2 (p2 ** a) (i2 ** a) (n2 ** a)

  let rec trivially_disjoint a b =
397
    if a == b then a == `False
398
    else match (a,b) with
399 400 401
      | `True, a | a, `True -> a == `False
      | `False, _ | _, `False -> true
      | `Split (_,x1, p1,i1,n1), `Split (_,x2, p2,i2,n2) ->
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
	  let c = X.compare x1 x2 in
	  if c = 0 then
(* try expanding -> p1 p2; p1 i2; i1 p2; i1 i2 ... *)
	    trivially_disjoint (p1 ++ i1) (p2 ++ i2) &&
	    trivially_disjoint (n1 ++ i1) (n2 ++ i2)
	  else if c < 0 then
	    trivially_disjoint p1 b &&
	    trivially_disjoint i1 b &&
	    trivially_disjoint n1 b
	  else
	    trivially_disjoint p2 a &&
	    trivially_disjoint i2 a &&
	    trivially_disjoint n2 a

  let rec neg = function
417 418 419 420 421 422 423 424
    | `True -> `False
    | `False -> `True
    | `Split (_,`Atm x, `True,`False,`False) -> split0 (`Atm(T.diff T.full x)) `True `False `False
    | `Split (_,`Atm x, `False,`False,`True) -> split0 (`Atm(T.diff T.full x)) `True `False `False
    | `Split (_,x, p,i,`False) -> split x `False (neg (i ++ p)) (neg i)
    | `Split (_,x, `False,i,n) -> split x (neg i) (neg (i ++ n)) `False 
    | `Split (_,x, p,`False,n) -> split x (neg p) (neg (p ++ n)) (neg n)  
    | `Split (_,x, p,i,n) -> split x (neg (i ++ p)) `False (neg (i ++ n))
425 426 427
	      
  let rec ( // ) a b =
    let negatm = T.diff T.full in
428
    if a == b then `False 
429
    else match (a,b) with
430 431 432
      | `False,_ | _, `True -> `False
      | a, `False -> a
      | `True, b -> neg b
433

434 435
      | `Split (_,`Atm x1, `True,`False,`False), `Split (_,`Atm x2, `True,`False,`False) ->
          split (`Atm(T.diff x1 x2)) `True `False `False
436

437 438
      | `Split (_,`Atm x1, `False,`False,`True), `Split (_,`Atm x2, `False,`False,`True) ->
          split (`Atm(T.diff (negatm x1) (negatm x2))) `True `False `False
439

440 441
      | `Split (_,`Atm x1, `True,`False,`False), `Split (_,`Atm x2, `False,`False,`True) ->
          split (`Atm(T.diff x1 (negatm x2))) `True `False `False
442

443 444
      | `Split (_,`Atm x1, `False,`False,`True), `Split (_,`Atm x2, `True,`False,`False) ->
          split (`Atm(T.diff (negatm x1) x2)) `True `False `False
445

446
      | `Split (_,x1, p1,i1,n1), `Split (_,x2, p2,i2,n2) ->
447 448
	  let c = X.compare x1 x2 in
	  if c = 0 then
449
	    if (i2 == `False) && (n2 == `False) 
450 451
	    then split x1 (p1 // p2) (i1 // p2) (n1 ++ i1)
	    else 
452
	      split x1 ((p1++i1) // (p2 ++ i2)) `False ((n1++i1) // (n2 ++ i2))
453 454 455
	  else if c < 0 then
	    split x1 (p1 // b) (i1 // b) (n1 // b) 
	  else
456
	    split x2 (a // (i2 ++ p2)) `False (a // (i2 ++ n2))
457 458 459 460 461 462 463 464 465
	      
  let cup = ( ++ )
  let cap = ( ** )
  let diff = ( // )

  (* return a couple of trees (v,a), the second where all variables
   * v = only variables as leaves
   * a = only atoms as leaves
   *)
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
  let rec extractvars = function
    (* `True or `False can only be under a variable *)
    | `True -> `True,`False
    | `False -> `False,`False
    | `Split (_,`Atm _, `True,`False,`False) as x -> `False, x
    | `Split (_,`Atm _, _,_,_) -> assert false
    | `Split (_,((`Var y) as x),p,i,n) ->
        let p1,p2 = extractvars p in
        let i1,i2 = extractvars i in
        let n1,n2 = extractvars n in
        (* let v = `Split (compute_hash x p1 i1 n1,x,p1,i1,n1) in   *)
        let v = (fst(gensplit Pervasives.compare (fun x -> x) (fun x -> x) (fun _ -> false)) x p1 i1 n1) in
        let t = split x p2 i2 n2 in
        assert(v <> `True);
        (v,t)
Pietro Abate's avatar
Pietro Abate committed
481

482
end
483 484 485 486 487 488 489

module Vars = struct
  module V = struct include Custom.String end
  include Bool.Make(V)
end

module BoolVars = Make(Vars)