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


6
module type S = sig
Pietro Abate's avatar
Pietro Abate committed
7

8
  module Atom : Bool.S
9

10
  include Bool.S with type elem = Atom.t Var.var_or_atom
11

12
  val var : Var.t -> t
13

14 15
  (** returns the union of all leaves in the BDD *)
  val leafconj: t -> Atom.t
16 17 18

  val is_empty : t -> bool

19
  val pp_print : Format.formatter -> t -> unit
20

21
  val print : ?f:(Format.formatter -> elem -> unit) -> t -> (Format.formatter -> unit) list
22

23 24 25 26 27
end

(* ternary BDD
 * where the nodes are Atm of X.t | Var of String.t
 * Variables are always before Values
28
 * All the leaves are then base types
29 30 31 32 33 34 35 36 37
 *
 * 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
 *
38
 * extract_var : copy the orginal tree and on one copy put to zero all
39 40 41 42
 * leaves that have an Atm on the other all leaves that have a Var
 *
 * *)

43
module Make (T : Bool.S) : S with module Atom = T and type elem = T.t Var.var_or_atom = struct
44 45
  (* ternary decision trees . cf section 11.3.3 Frish PhD *)
  (* plus variables *)
46 47
  (* `Atm are containers (Atoms, Chars, Intervals, Pairs ... )
   * `Var are String
48
   *)
Pietro Abate's avatar
Pietro Abate committed
49

50 51
  module Atom = T
  type elem = T.t Var.var_or_atom
52
  module X : Custom.T with type t = elem = Var.Make(T)
53 54 55 56
  type 'a bdd = False
               | True
               | Split of int * 'a * ('a bdd) * ('a bdd) * ('a bdd)

57 58 59
  type t = elem bdd

  let rec equal_aux eq a b =
60 61
    (a == b) ||
    match (a,b) with
62
      | Split (h1,x1,p1,i1,n1), Split (h2,x2,p2,i2,n2) ->
63
	  (h1 == h2) &&
64 65
	  (equal_aux eq p1 p2) && (equal_aux eq i1 i2) &&
	  (equal_aux eq n1 n2) && (eq x1 x2)
66 67
      | _ -> false

68 69
  let equal = equal_aux X.equal

70 71 72 73
(* Idea: add a mutable "unique" identifier and set it to
   the minimum of the two when egality ... *)

  let rec compare a b =
74
    if (a == b) then 0
75
    else match (a,b) with
76
      | Split (h1,x1, p1,i1,n1), Split (h2,x2, p2,i2,n2) ->
77
	  if h1 < h2 then -1 else if h1 > h2 then 1
78 79
	  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
80
	  else let c = compare i1 i2 in if c <> 0 then c
81
	  else compare n1 n2
82 83 84 85
      | True,_  -> -1
      | _, True -> 1
      | False,_ -> -1
      | _,False -> 1
86 87

  let rec hash = function
88 89 90
    | True -> 1
    | False -> 0
    | Split(h,_,_,_,_) -> h
91

92
  let compute_hash x p i n =
93
	(X.hash x) + 17 * (hash p) + 257 * (hash i) + 16637 * (hash n)
94 95

  let rec check = function
96 97 98
    | True -> ()
    | False -> ()
    | Split (h,x,p,i,n) ->
99
	assert (h = compute_hash x p i n);
100 101 102
	(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) | _ -> ());
103 104 105 106
	X.check x; check p; check i; check n

  let atom x =
    let h = X.hash x + 17 in (* partial evaluation of compute_hash... *)
107
    Split (h, x,True,False,False)
Pietro Abate's avatar
Pietro Abate committed
108

109
  let var v =
110
    let compute_hash x p i n =
111 112
        (Var.hash x) + 17 * (hash p) + 257 * (hash i) + 16637 * (hash n)
    in
113
    let a = atom (`Atm T.full) in
114 115
    let h = compute_hash v a False False in
    ( Split (h,`Var v,a,False,False) :> t )
116 117

  let rec iter f = function
118
    | Split (_, x, p,i,n) -> f x; iter f p; iter f i; iter f n
119 120 121
    | _ -> ()

  let rec dump ppf = function
122 123 124
    | True -> Format.fprintf ppf "⫧"
    | False -> Format.fprintf ppf "⫨"
    | Split (_,x, p,i,n) ->
125 126 127 128 129 130 131 132 133 134
      let fmt = format_of_string (
        match x with
          `Var _ ->
            "@[{@[%a@]}{@[<hov>%a,@ %a,@ %a@]}@]"
        | `Atm _ ->
          "@[ {@[%a@]}@\n {@[<hov>%a,@ %a,@ %a@]}@]"
      )
      in
      Format.fprintf ppf fmt
        X.dump x dump p dump i dump n
135 136

  let rec print f ppf = function
137 138 139
    | True -> Format.fprintf ppf "Any"
    | False -> Format.fprintf ppf "Empty"
    | Split (_, x, p,i, n) ->
140 141
	let flag = ref false in
	let b () = if !flag then Format.fprintf ppf " | " else flag := true in
142
	(match p with
143 144
	   | True -> b(); Format.fprintf ppf "%a" f x
	   | False -> ()
145
	   | _ -> b (); Format.fprintf ppf "%a & @[(%a)@]" f x (print f) p );
146
	(match i with
147 148
	   | True -> assert false;
	   | False -> ()
149
	   | _ -> b(); print f ppf i);
150
	(match n with
151 152
	   | True -> b (); Format.fprintf ppf "@[~%a@]" f x
	   | False -> ()
153
	   | _ -> b (); Format.fprintf ppf "@[~%a@] & @[(%a)@]" f x (print f) n)
154

Pietro Abate's avatar
Pietro Abate committed
155
  let pp_print = print X.dump
156

157
  let print ?(f=X.dump) = function
158 159
    | True -> assert false (* [] a bdd cannot be of this type *)
    | False -> [ fun ppf -> Format.fprintf ppf "Empty" ]
160
    | c -> [ fun ppf -> print f ppf c ]
161

162 163 164 165
  (* return a list of pairs, where each pair holds the list
   * of positive and negative elements on a branch *)
  let get x =
    let rec aux accu pos neg = function
166 167 168
      | True -> (List.rev pos, List.rev neg) :: accu
      | False -> accu
      | Split (_,x, p,i,n) ->
169 170 171 172 173 174
        let accu = aux accu (x::pos) neg p in
        let accu = aux accu pos (x::neg) n in
        let accu = aux accu pos neg i in
        accu
    in aux [] [] [] x

175

176
  let leafconj x =
177
    let rec aux accu = function
178 179 180 181 182
      | True -> accu
      | False -> accu
      | Split (_,`Atm x, True,False,False) -> x :: accu
      | Split (_,`Atm x, _,_,_) -> assert false
      | Split (_,`Var x, p,i,n) ->
183 184 185 186 187 188
        let accu = aux accu p in
        let accu = aux accu n in
        let accu = aux accu i in
        accu
    in
    List.fold_left T.cup T.empty (aux [] x)
189 190 191

  let compute ~empty ~full ~cup ~cap ~diff ~atom b =
    let rec aux = function
192 193 194 195 196
      | True  -> full
      | False -> empty
      | Split (_,`Atm x,True,_,_) when T.equal x T.empty -> empty
      | Split (_,`Atm x,True,_,_) when T.equal x T.full -> full
      | Split(_,x, p,i,n) ->
197 198 199 200 201
        let x1 = atom x in
	let p = cap x1 (aux p) in
        let i = aux i in
        let n = diff (aux n) x1 in
	cup (cup p i) n
202 203
    in
    aux b
204

205 206 207
(* Invariant: correct hash value *)

  let split0 x pos ign neg =
208
    Split (compute_hash x pos ign neg, x, pos, ign, neg)
209

210 211
  let empty = False
  let full = split0 (`Atm T.full) True False False
212 213 214 215

  let is_empty t = (t == empty)

(* Invariants:
216
     Split (x, pos,ign,neg) ==>  (ign <> True), (pos <> neg)
217 218 219 220
*)

  let rec has_true = function
    | [] -> false
221
    | True :: _ -> true
222 223
    | _ :: l -> has_true l

224 225 226 227
  let rec has_same a = function
    | [] -> false
    | b :: l -> (equal a b) || (has_same a l)

228 229
  (* split removes redundant subtrees from the positive and negative
   * branch if they are present in the lazy union branch *)
230
  let rec split x p i n =
231
    if X.equal (`Atm T.empty) x then False
232
    (* 0?p:i:n -> 0 *)
233
    else if i == True then True
234 235 236 237
    (* 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) *)
238
    if equal p n then p ++ i
239 240 241 242 243
    else split0 x p i n

  (* simplify t l -> bdd of ( t // l ) *)
  and simplify a l =
    match a with
244 245 246 247 248 249
      | False -> False
      | True -> if has_true l then False else True
      | Split (_,`Atm x, False,False,True) ->
          split (`Atm(T.diff T.full x)) True False False
      | Split (_,x,p,i,n) ->
        if (has_true l) || (has_same a l) then False
250 251
        else s_aux2 a x p i n [] [] [] l
  and s_aux2 a x p i n ap ai an = function
252 253
    | [] ->
      let p = simplify p ap
254 255 256
      and n = simplify n an
      and i = simplify i ai in
      if equal p n then p ++ i else split0 x p i n
257
    | b :: l -> s_aux3 a x p i n ap ai an l b
258
  and s_aux3 a x p i n ap ai an l = function
259 260 261 262
    | 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
263 264 265 266 267 268 269 270 271
      else let c = X.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 (a,b) with
272 273
    | True, _ | _, True -> True
    | False, a | a, False -> a
274

275 276
    | Split (_,`Atm x1, True,False,False), Split (_,`Atm x2, True,False,False) ->
        split (`Atm (T.cup x1 x2)) True False False
277

278 279 280
    | Split (_,`Atm x1, False,False,True), Split (_,`Atm x2, False,False,True)
    | Split (_,`Atm x1, True,False,False), Split (_,`Atm x2, False,False,True)
    | Split (_,`Atm x1, False,False,True), Split (_,`Atm x2, True,False,False) ->
281
        assert false
282

283
    | Split (_,x1, p1,i1,n1), Split (_,x2, p2,i2,n2) ->
284 285 286 287
      let c = X.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
288 289 290 291 292 293

(* 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
294 295
    | True, a | a, True -> a
    | False, _ | _, False -> False
296

297 298
    | Split (_,`Atm x1, True,False,False), Split (_,`Atm x2, True,False,False) ->
        split (`Atm(T.cap x1 x2)) True False False
299

300 301
    | 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
302

303 304
    | 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
305

306 307
    | 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
308

309
    | Split (_,x1, p1,i1,n1), Split (_,x2, p2,i2,n2) ->
310 311
	let c = X.compare x1 x2 in
	if c = 0 then
312
	  split x1
313 314
	    (p1 ** (p2 ++ i2) ++ (p2 ** i1))
	    (i1 ** i2)
315
	    (n1 ** (n2 ++ i2) ++ (n2 ** i1))
316 317 318 319
	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 =
320
    if a == b then a == False
321
    else match (a,b) with
322 323 324
      | True, a | a, True -> a == False
      | False, _ | _, False -> true
      | Split (_,x1, p1,i1,n1), Split (_,x2, p2,i2,n2) ->
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	  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
340 341 342 343 344 345 346 347
    | 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))
348

349 350
  let rec ( // ) a b =
    let negatm = T.diff T.full in
351
    if a == b then False
352
    else match (a,b) with
353 354 355
      | False,_ | _, True -> False
      | a, False -> a
      | True, b -> neg b
356

357 358
      | Split (_,`Atm x1, True,False,False), Split (_,`Atm x2, True,False,False) ->
          split (`Atm(T.diff x1 x2)) True False False
359

360 361
      | Split (_,`Atm x1, False,False,True), Split (_,`Atm x2, False,False,True) ->
          split (`Atm(T.diff (negatm x1) (negatm x2))) True False False
362

363 364
      | Split (_,`Atm x1, True,False,False), Split (_,`Atm x2, False,False,True) ->
          split (`Atm(T.diff x1 (negatm x2))) True False False
365

366 367
      | Split (_,`Atm x1, False,False,True), Split (_,`Atm x2, True,False,False) ->
          split (`Atm(T.diff (negatm x1) x2)) True False False
368

369
      | Split (_,x1, p1,i1,n1), Split (_,x2, p2,i2,n2) ->
370 371
	  let c = X.compare x1 x2 in
	  if c = 0 then
372
	    if (i2 == False) && (n2 == False)
373
	    then split x1 (p1 // p2) (i1 // p2) (n1 ++ i1)
374
	    else
375
	      split x1 ((p1++i1) // (p2 ++ i2)) False ((n1++i1) // (n2 ++ i2))
376
	  else if c < 0 then
377
	    split x1 (p1 // b) (i1 // b) (n1 // b)
378
	  else
379
	    split x2 (a // (i2 ++ p2)) False (a // (i2 ++ n2))
380

381 382 383 384 385
  let cup = ( ++ )
  let cap = ( ** )
  let diff = ( // )

end