solver.ml 24 KB
Newer Older
1
(*-----------------------------------------------------------------------
2
** Copyright (C) 2001, 2002 - Verimag.
3 4 5 6 7 8 9 10
** This file may only be copied under the terms of the GNU Library General
** Public License 
**-----------------------------------------------------------------------
**
** File: solver.ml
** Main author: jahier@imag.fr
*)

11
open List
12
open Formula
13
open Util
14
open Hashtbl
15
open Gne
16
open Rnumsolver
17

18 19 20
(****************************************************************************)
	  
let (formula_list_to_conj: formula list -> formula) =
21
  fun fl -> 
22 23 24 25 26 27 28 29 30
    (** Transform a (non-empty) list of formula to the conjunction
       made of those formula.  
    *)
    match fl with
	[] -> assert false
      | f::[] -> f
      | f1::f2::tail -> 
          List.fold_left (fun x y -> And(x, y)) (And(f1, f2)) tail

31

32 33 34
let (lookup: env_in -> subst list -> var_name -> var_value option) = 
  fun input pre vn ->  
    try Some(Hashtbl.find input vn)
35
    with Not_found -> 
36 37 38 39 40 41 42 43
      try Some(List.assoc vn pre)
      with Not_found -> None

(****************************************************************************)

type comp = SupZero | SupEqZero | EqZero | NeqZero


44

45 46 47 48 49 50 51 52 53 54 55 56
let rec (formula_to_bdd : env_in -> formula -> Bdd.t * bool) =
  fun input f ->
    (** Replaces input and pre variables by their values in the
      formula [f], and translates it into a bdd.

      Also returns a flag telling whether or not the formula depends
      on input and pre vars. If this flag is true, the formula is
      stored (cached) in a global table ([env_state.bdd_tbl]);
      otherwise, it is stored in a table that is cleared at each new
      step ([env_state.bdd_tbl_global]).

    *)
57
    try (Env_state.bdd f, true)
58
    with Not_found -> 
59
      try (Env_state.bdd_global f, false)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      with Not_found -> 
	let (bdd, dep) =
	  match f with 
	      Not(f1) ->
		let (bdd_not, dep) =  (formula_to_bdd input f1) in
		  (Bdd.dnot bdd_not, dep)

	    | Or(f1, f2) ->
		let (bdd1, dep1) = (formula_to_bdd input f1)
		and (bdd2, dep2) = (formula_to_bdd input f2)
		in
		  (Bdd.dor bdd1 bdd2, dep1 || dep2)

	    | And(f1, f2) -> 
		let (bdd1, dep1) = (formula_to_bdd input f1)
		and (bdd2, dep2) = (formula_to_bdd input f2)
		in
		  (Bdd.dand bdd1 bdd2, dep1 || dep2)

	    | IteB(f1, f2, f3) -> 
		let (bdd1, dep1) = (formula_to_bdd input f1)
		and (bdd2, dep2) = (formula_to_bdd input f2)
		and (bdd3, dep3) = (formula_to_bdd input f3) 
		in
		  ((Bdd.dor (Bdd.dand bdd1 bdd2) 
		      (Bdd.dand (Bdd.dnot bdd1) bdd3)),
		   dep1 || dep2 || dep3 )
		  
88 89
	    | True ->  (Bdd.dtrue  (Env_state.bdd_manager ()), false)
	    | False -> (Bdd.dfalse (Env_state.bdd_manager ()), false)
90
	    | Bvar(vn) ->    
91
		( match (lookup input (Env_state.pre ()) vn) with 
92 93
		      Some(B(bool)) -> 
			if bool 
94 95
			then (Bdd.dtrue  (Env_state.bdd_manager ()), true) 
			else (Bdd.dfalse (Env_state.bdd_manager ()), true)
96 97 98 99
		    | Some(_) -> 
			print_string (vn ^ " is not a boolean!\n");
			assert false
		    | None ->
100
			if List.mem vn (Env_state.pre_var_names ())
101 102 103 104 105 106
			then failwith 
			  ("*** " ^ vn ^ " is unknown at this stage.\n "
			   ^ "*** Make sure you have not used "
			   ^ "a pre on a output var at the 1st step, \n "
			   ^ "*** or a pre on a input var at the second step in "
			   ^ "your formula in the environment.\n ")
107
			else (Bdd.ithvar (Env_state.bdd_manager ()) 
108
			 (Env_state.atomic_formula_to_index (Bv(vn))), false)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		)
		
	    | Eq(e1, e2) -> 
		let (gne, dep) = expr_to_gne (Diff(e1, e2)) input in 
		  (gne_to_bdd gne EqZero, dep)
		  
	    | Neq(e1, e2) -> 
		let (gne, dep) = expr_to_gne (Diff(e1, e2)) input in
		  (gne_to_bdd gne NeqZero, dep)
		  
	    | SupEq(e1, e2) ->
		let (gne, dep) = expr_to_gne (Diff(e1, e2)) input in
		  (gne_to_bdd gne SupEqZero, dep)
		  
	    | Sup(e1, e2)   ->
		let (gne, dep) = expr_to_gne (Diff(e1, e2)) input in
		  (gne_to_bdd gne SupZero, dep)
		  
	    | InfEq(e1, e2) ->  
		let (gne, dep) = expr_to_gne (Diff(e2, e1)) input in
		  (gne_to_bdd gne SupEqZero, dep)
		  
	    | Inf(e1, e2)   ->  
		let (gne, dep) =  expr_to_gne (Diff(e2, e1)) input in
		  (gne_to_bdd gne SupZero, dep)
		  
	in
	  if dep
	  then 
138
	    ( Env_state.set_bdd f bdd;	
139 140
	      match f with 
		  Not(nf) -> () (* Already in the tbl thanks to the rec call *)
141
		| _  -> Env_state.set_bdd (Not(f)) (Bdd.dnot bdd) 
142 143 144
	    ) 
	  else 
	    (* [f] does not depend on pre nor input vars *)
145
	    ( Env_state.set_bdd_global f bdd ;	
146 147 148
	      match f with 
		  Not(nf) -> () (* Already in the table thanks to the rec call *)
		| _  -> 
149
		    Env_state.set_bdd_global (Not(f)) (Bdd.dnot bdd)
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	    );

	  (bdd, dep)
and
  (expr_to_gne: expr -> env_in -> Gne.gn_expr * bool) =
  fun e input -> 
    (** Evaluates pre and input vars appearing in [e] and tranlates
      it into a so-called garded normal form. Also returns a flag
      that is true iff [e] depends on pre or input vars. *)
    let (gne, dep) =
      match e with  
	  Sum(e1, e2) ->
	    let (gne1, dep1) = (expr_to_gne e1 input)
	    and (gne2, dep2) = (expr_to_gne e2 input) 
	    in
	      (Gne.add  gne1 gne2, dep1 || dep2)

	| Diff(e1, e2) -> 
	    let (gne1, dep1) = (expr_to_gne e1 input)
	    and (gne2, dep2) = (expr_to_gne e2 input) 
	    in
	      (Gne.diff gne1 gne2, dep1 || dep2)

	| Prod(e1, e2) -> 
	    let (gne1, dep1) = (expr_to_gne e1 input)
	    and (gne2, dep2) = (expr_to_gne e2 input) 
	    in
	      (Gne.mult gne1 gne2, dep1 || dep2)

	| Quot(e1, e2) -> 
	    let (gne1, dep1) = (expr_to_gne e1 input)
	    and (gne2, dep2) = (expr_to_gne e2 input) 
	    in
	      (Gne.quot gne1 gne2, dep1 || dep2)

	| Mod(e1, e2)  -> 
	    let (gne1, dep1) = (expr_to_gne e1 input)
	    and (gne2, dep2) = (expr_to_gne e2 input) 
	    in
	      (Gne.modulo gne1 gne2, dep1 || dep2)

	| Ivar(str) ->
192
	    ( match (lookup input (Env_state.pre ()) str) with 
193 194 195
		  Some(N(I(i))) ->
		    ( (GneMap.add 
			(NeMap.add "" (I(i)) NeMap.empty) 
196
			(Bdd.dtrue (Env_state.bdd_manager ()))
197 198 199 200 201 202
			GneMap.empty),
		      true
		    )
		| None ->
		    ( (GneMap.add 
			(NeMap.add str (I(1)) NeMap.empty) 
203
			(Bdd.dtrue (Env_state.bdd_manager ())) 
204 205 206 207 208 209 210 211 212 213 214 215 216 217
			GneMap.empty),
		      false
		    )     
		| Some(N(F(f))) -> 
		    print_string ((string_of_float f) 
				  ^ "is a float, but an int is expected.\n");
		    assert false
		| Some(B(f)) -> 
		    print_string ((string_of_bool f) 
				  ^ "is a bool, but an int is expected.\n");
		    assert false
	    )

	| Fvar(str) ->
218
	    ( match (lookup input (Env_state.pre ()) str) with 
219 220 221
		  Some(N(F(f))) ->
		    ( (GneMap.add 
			(NeMap.add "" (F(f)) NeMap.empty) 
222
			(Bdd.dtrue (Env_state.bdd_manager ())) 
223 224 225 226 227 228
			GneMap.empty),
		      true
		    )
		| None ->
		    ( (GneMap.add 
			(NeMap.add str (F(1.)) NeMap.empty) 
229
			(Bdd.dtrue (Env_state.bdd_manager ())) 
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
			GneMap.empty),
		      false
		    )
		| Some(N(I(i))) -> 
		    print_string ((string_of_int i) 
				  ^ "is an int, but a float is expected.\n");
		    assert false
		| Some(B(f)) -> 
		    print_string ((string_of_bool f) 
				  ^ "is a bool, not a float is expected.\n");
		    assert false
	    )

	| Ival(i) ->  
	    ( (GneMap.add 
		(NeMap.add "" (I(i)) NeMap.empty) 
246
		(Bdd.dtrue (Env_state.bdd_manager ())) 
247 248 249 250 251 252 253
		GneMap.empty),
	      false
	    )

	| Fval(f) -> 
	    ( (GneMap.add 
		(NeMap.add "" (F(f)) NeMap.empty) 
254
		(Bdd.dtrue (Env_state.bdd_manager ()))
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 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
		GneMap.empty),
	      false
	    )

	| Ite(f, e1, e2) -> 
	    let (add_formula_to_gne_acc : Bdd.t -> n_expr -> Bdd.t -> 
		   Gne.gn_expr -> Gne.gn_expr) = 
	      fun bdd nexpr c acc -> 
		(* Used (by a GneMap.fold) to add the condition [c] to every
		   condition of a garded expression. *)
		let _ = assert (
		  try 
		    let _ = GneMap.find nexpr acc in
		      false
		  with Not_found -> true
		) 
		in
		let new_bdd = (Bdd.dand bdd c) in
		  if Bdd.is_false new_bdd
		  then acc
		  else GneMap.add nexpr new_bdd acc
	    in
	    let (bdd, dep1) = formula_to_bdd input f in
	    let bdd_not = Bdd.dnot bdd
	    and (gne_t, dep2) = (expr_to_gne e1 input)
	    and (gne_e, dep3) = (expr_to_gne e2 input) in
	    let gne1 = GneMap.fold (add_formula_to_gne_acc bdd) gne_t GneMap.empty in
	    let gne  = GneMap.fold (add_formula_to_gne_acc bdd_not) gne_e gne1 in
	      (gne, dep1 || dep2 || dep3)
    in
      (gne, dep)
	
and
  (gne_to_bdd : Gne.gn_expr -> comp -> Bdd.t) =
  fun gne cmp -> 
    (** Use [cmp] to compare [gne] with 0 and returns the
       corresponding formula.  E.g., if [gne] is bounded to
       [e1 -> c1; e2 -> c2], then [gne_to_bdd gne SupZero] returns
       (the bdd corresponding to) the formula [(c1 and (e1 > 0)) or
       (c2 and (e2 > 0))] *)
    match cmp with
	SupZero ->
	  ( GneMap.fold 
	      (fun nexpr c acc -> 
		 let bdd = 
		   if is_n_expr_a_constant nexpr
		   then 
		     let cst = NeMap.find "" nexpr in
		       match cst with
			   I(i) -> 
			     if i > 0 
306 307
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
308 309
			 | F(f) -> 
			     if f > 0. 
310 311
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
312 313 314

		   else 
		     Bdd.ithvar 
315
		       (Env_state.bdd_manager ()) 
316 317 318 319 320
		       (Env_state.atomic_formula_to_index (GZ(nexpr))) 
		 in
		   Bdd.dor (Bdd.dand c bdd) acc
	      )
	      gne 
321
	      (Bdd.dfalse (Env_state.bdd_manager ()))
322 323 324 325 326 327 328 329 330 331 332
	  )
      | SupEqZero ->
	  ( GneMap.fold 
	      (fun nexpr c acc -> 
		 let bdd = 
		   if is_n_expr_a_constant nexpr
		   then 
		     let cst = NeMap.find "" nexpr in
		       match cst with
			   I(i) -> 
			     if i >= 0 
333 334
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
335 336
			 | F(f) -> 
			     if f >= 0. 
337 338
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
339 340 341

		   else 
		     Bdd.ithvar 
342
		       (Env_state.bdd_manager ()) 
343 344 345 346 347
		       (Env_state.atomic_formula_to_index (GeqZ(nexpr))) 
		 in
		   Bdd.dor (Bdd.dand c bdd) acc
	      )
	      gne 
348
	      (Bdd.dfalse (Env_state.bdd_manager ()))
349 350 351 352 353 354 355 356 357 358 359
	  )
      | EqZero -> 
	  ( GneMap.fold 
	      (fun nexpr c acc -> 
		 let bdd1 = 
		   if is_n_expr_a_constant nexpr
		   then 
		     let cst = NeMap.find "" nexpr in
		       match cst with
			   I(i) -> 
			     if i >= 0 
360 361
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
362 363
			 | F(f) -> 
			     if f >= 0. 
364 365
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
366 367 368

		   else 
		     Bdd.ithvar 
369
		       (Env_state.bdd_manager ()) 
370 371 372 373 374 375 376 377 378
		       (Env_state.atomic_formula_to_index (GeqZ(nexpr))) 
		 in
		 let bdd2 = 
		   if is_n_expr_a_constant nexpr
		   then 
		     let cst = NeMap.find "" nexpr in
		       match cst with
			   I(i) -> 
			     if i <= 0 
379 380
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
381 382
			 | F(f) -> 
			     if f <= 0. 
383 384
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
385 386 387

		   else 
		     Bdd.ithvar 
388
		       (Env_state.bdd_manager ()) 
389 390 391 392 393 394 395 396
		       (Env_state.atomic_formula_to_index (SeqZ(nexpr))) 
		 in
		 let bdd = Bdd.dand bdd1 bdd2 in 
		   (* We transform [e1 = e2] into [e1 <= e2 ^ e1 >= e2] as the 
		      numeric solver can not handle equalities *)
		   Bdd.dor (Bdd.dand c bdd) acc
	      )
	      gne 
397
	      (Bdd.dfalse (Env_state.bdd_manager ()))
398 399 400 401 402 403 404 405 406 407 408
	  )
      | NeqZero -> 
	  ( GneMap.fold 
	      (fun nexpr c acc -> 
		 let bdd1 = 
		   if is_n_expr_a_constant nexpr
		   then 
		     let cst = NeMap.find "" nexpr in
		       match cst with
			   I(i) -> 
			     if i > 0 
409 410
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
411 412
			 | F(f) -> 
			     if f > 0. 
413 414
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
415 416 417

		   else 
		     Bdd.ithvar 
418
		       (Env_state.bdd_manager ()) 
419 420 421 422 423 424 425 426 427
		       (Env_state.atomic_formula_to_index (GZ(nexpr))) 
		 in
		 let bdd2 = 
		   if is_n_expr_a_constant nexpr
		   then 
		     let cst = NeMap.find "" nexpr in
		       match cst with
			   I(i) -> 
			     if i < 0 
428 429
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
430 431
			 | F(f) -> 
			     if f < 0. 
432 433
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
434 435 436

		   else 
		     Bdd.ithvar 
437
		       (Env_state.bdd_manager ()) 
438 439 440 441 442 443 444 445
		       (Env_state.atomic_formula_to_index (SZ(nexpr))) 
		 in
		 let bdd = Bdd.dor bdd1 bdd2 in 
		   (* We transform [e1 <> e2] into [e1 < e2 or e1 > e2] as the 
		      numeric solver can not handle disequalities *)
		   Bdd.dor (Bdd.dand c bdd) acc
	      )
	      gne 
446
	      (Bdd.dfalse (Env_state.bdd_manager ()))
447
	  )
448

449 450 451 452 453
(****************************************************************************)
(****************************************************************************)


(* Exported *)
454 455
let rec (is_satisfiable: env_in -> formula list -> bool) = 
  fun input fl -> 
456
    let f = formula_list_to_conj fl in
457
    let (bdd, _) = formula_to_bdd input f in
458 459 460
      not (Bdd.is_false bdd) &&
      ( 
	try 
461
	  let (n, m) = Env_state.sol_number bdd in 
462 463 464 465 466 467 468
	    not ((zero_sol, zero_sol) = (n, m))
	with Not_found -> true
      )
      


(****************************************************************************)
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
(****************************************************************************)

type var = int

(** In the following, we call a comb the bdd of a conjunction of
 litterals (var). They provide the ordering in which litterals
 appear in the bdds we manipulate.
*)



let rec (build_sol_nb_table: Bdd.t -> Bdd.t -> sol_nb * sol_nb) =
  fun bdd comb -> 
    (** Returns the relative (to which bbd points to it) number of
      solutions of [bdd] and the one of its negation. Also udpates
484 485 486 487 488 489
      the solution number table for [bdd] and its negation, and
      recursively for all its sub-bdds.

      [comb] is a positive cube that ougth to contain the indexes of
      boolean vars that are still to be generated, and the numerical
      indexes that appears in [bdd].  
490
    *)
491 492 493
    let _ = assert (not (Bdd.is_cst bdd) 
		    && (Bdd.topvar comb) = (Bdd.topvar bdd)) 
    in
494 495 496
    let bdd_not = (Bdd.dnot bdd) in
    let (sol_nb, sol_nb_not) =
      try
497 498
	let (nt, ne) = Env_state.sol_number bdd 
	and (not_nt, not_ne) = Env_state.sol_number bdd_not in
499 500
	  (* solutions numbers in the table are absolute *)
	  ((add_sol_nb nt ne), (add_sol_nb not_nt not_ne))
501
      with Not_found ->
502 503
	let (nt, not_nt) = compute_absolute_sol_nb (Bdd.dthen bdd) comb in
	let (ne, not_ne) = compute_absolute_sol_nb (Bdd.delse bdd) comb in
504 505
	  Env_state.set_sol_number bdd (nt, ne) ;
	  Env_state.set_sol_number bdd_not (not_nt, not_ne) ;
506 507 508 509 510 511
	  ((add_sol_nb nt ne), (add_sol_nb not_nt not_ne))
    in
      (sol_nb, sol_nb_not)
and 
  (compute_absolute_sol_nb: Bdd.t -> Bdd.t -> sol_nb * sol_nb) =
  fun sub_bdd comb -> 
512
    (* Returns the absolute number of solutions of [sub_bdd] (and its
513
       negation) w.r.t. [comb], where [comb] is the comb of the
514 515 516 517 518 519 520 521 522 523
       father of [sub_bdd].

       The [comb] is used to know which output boolean variables are
       unconstraint along a path in the bdd. Indeed, the comb is made
       of all the boolean output var indexes plus the num contraints
       indexes that appears in the bdd; hence, if the topvar of the
       bdd is different from the topvar of the comb, it means that
       the topvar of the comb is unsconstraint and we need to
       multiply the number of solution of the branch by 2.
    *)
524
    if Bdd.is_cst sub_bdd 
525
    then
526
      let sol_nb = 
527
	if Bdd.is_true comb
528
	then one_sol
529
	else (two_power_of (List.length (Bdd.list_of_support (Bdd.dthen comb)))) 
530
      in
531 532 533 534 535
	if Bdd.is_true sub_bdd
	then (sol_nb, zero_sol) 
	else (zero_sol, sol_nb)
    else 
      let topvar = Bdd.topvar sub_bdd in
536 537 538 539 540 541 542 543 544 545 546 547
      let rec
	(count_missing_vars: Bdd.t -> var -> int -> Bdd.t * int) =
	fun comb var cpt -> 
	  (* Returns [cpt] + the number of variables occurring in [comb]
	     before reaching [var] ([var] excluded). Also returns the comb
	     whch topvar is [var]. *)
	  let _ = assert (not (Bdd.is_cst comb)) in
	  let combvar = Bdd.topvar comb in
	    if var = combvar
	    then (comb, cpt)
	    else count_missing_vars (Bdd.dthen comb) var (cpt+1)
      in
548
      let (sub_comb, missing_vars_nb) = 
549
	count_missing_vars (Bdd.dthen comb) topvar 0
550 551 552 553 554
      in
      let (n0, not_n0) = build_sol_nb_table sub_bdd sub_comb in
      let factor = (two_power_of missing_vars_nb) in
	(mult_sol_nb n0 factor, mult_sol_nb not_n0 factor)
	
555 556 557 558

(****************************************************************************)
(****************************************************************************)

559

560
let (toss_up_one_var: var -> subst option) =
561
  fun var -> 
562 563 564 565 566 567 568 569 570
    (* if [var] is a index that corresponds to a boolean variable,
       this fonction performs a toss and returns a substitution for
       the corresponding boolean variable. It returns [None]
       otherwise.

       Indeed, if it happens that a numerical constraint does not
       appear along a path, we simply ignore it and hence it will not
       be added to the store.
    *)
571 572
    let af = Env_state.index_to_atomic_formula var in
      match af with 
573 574 575 576 577 578 579
          Bv(vn) -> 
	    let ran = Random.float 1. in
	      if (ran < 0.5) 
	      then Some(vn, Formula.B(true)) 
	      else Some(vn, Formula.B(false))
	| _  -> None
     
580

581 582 583 584 585 586 587 588
let (is_a_numeric_constraint : atomic_formula -> bool) =
  fun af -> 
    match af with
	Bv(_) -> false
      | GZ(_)   -> true 
      | GeqZ(_) -> true
      | SZ(_)   -> true
      | SeqZ(_) -> true
589 590 591 592 593


(* exported *)
exception No_numeric_solution

594 595
let rec (draw_in_bdd: subst list * store -> Bdd.t -> Bdd.t -> 
	   subst list * store) = 
596 597 598 599 600 601
  fun (sl, store) bdd comb ->
    (** Returns [sl] appended to a draw of all the boolean variables
      bigger than the topvar of [bdd] according to the ordering
      induced by the comb [comb]. Also returns the (non empty) store
      obtained by adding to [store] all the numeric constraints that
      were encountered during this draw.
602

603 604 605
      Raises the [No_numeric_solution] exception whenever no valid
      path in [bdd] leads to a satisfiable set of numeric
      constraints.  
606 607
    *)
    let _ = assert (not (Bdd.is_cst bdd)) in
608
    let _ = assert (Env_state.sol_number_exists bdd) in
609 610 611
    let bddvar  = Bdd.topvar bdd in
    let af = (Env_state.index_to_atomic_formula bddvar) in 
    let top_var_is_numeric = is_a_numeric_constraint af in
612
      if
613
	bddvar <> (Bdd.topvar comb) &&
614
	not top_var_is_numeric
615
      then
616 617 618 619 620
	let new_sl =
	  match toss_up_one_var (Bdd.topvar comb) with
	      Some(s) -> s::sl
	    | None -> sl
	in
621 622
	  draw_in_bdd (new_sl, store) bdd (Bdd.dthen comb)
      else 
623
	(* bddvar = combvar xor top_var_is_numeric *) 
624 625 626 627 628
	(* nb: I handle those two cases alltogether to avoid code
	   duplication (i.e., retrieving sol numbers, performing the
	   toss, the recursive call, handling the base case where a
	   dtrue bdd is reached, etc).  It makes the code a little
	   bit more obscur, but ...  *)
629
	let (n, m) = Env_state.sol_number bdd in
630 631 632
	let _ =
	  if ((eq_sol_nb n zero_sol) && (eq_sol_nb m zero_sol))
	  then raise No_numeric_solution ;
633
	in
634 635 636 637
	let (store_plus_af, store_plus_not_af) = 
	  (* A first trick to avoid code dup (cf nb above) *)
	  if top_var_is_numeric
	  then 
638
	    split_store store af
639 640 641 642 643 644 645 646 647 648
	  else 
	    (store, store)
	in
	let (swap, store1, bdd1, bool1, sol_nb1, store2, bdd2, bool2, sol_nb2) =
	  (* Depending on the result of a toss (based on the number
	     of solution in each branch), we try the [else] or the
	     [then] branch first. [swap] indicates whether or not the
	     [else] part is put before the [then] one. *)
	  let ran = Random.float 1. in
	    if ran < ((float_of_sol_nb n) /. (float_of_sol_nb (add_sol_nb n m)))
649
	    then
650 651 652 653 654
	      (false, store_plus_af, (Bdd.dthen bdd), true, n,
	       store_plus_not_af, (Bdd.delse bdd), false, m )
	    else 
	      (true, store_plus_not_af, (Bdd.delse bdd), false, m,
	       store_plus_af, (Bdd.dthen bdd), true, n )
655
	in
656 657 658
	let (sl1, sl2, new_comb) = (
	  (* A second trick to avoid code dup (cf nb above) *)
	  match af with 
659 660 661
	    Bv(vn) -> 
	      (((vn, Formula.B(bool1))::sl), 
	       ((vn, Formula.B(bool2))::sl), 
662
	       (if Bdd.is_true comb then comb else Bdd.dthen comb) )
663 664 665 666
	  | _ -> 
	      (* top_var_is_numeric *)
	      (sl, sl, comb)
	  )
667
	in
668 669 670 671 672
	let res_opt =
	  (* A solution will be found in this branch iff there exists
	     at least one path in the bdd that leads to a satisfiable
	     set of numeric constraints. If it is not the case,
	     [res_opt] is bound to [None]. *)
673
	  if not (is_empty store1)
674 675 676 677 678 679 680 681
	  then 
	    try 
	      let tail_draw1 = 
		if Bdd.is_true bdd1
		then
		  (* Toss the remaining bool vars. *)
		  ( (List.append 
		       sl1 
682
		       (Util.list_map_option toss_up_one_var (Bdd.list_of_support new_comb))),
683 684 685 686 687 688 689 690
		    store1
		  )
		else
		  draw_in_bdd (sl1, store1) bdd1 new_comb
	      in
		Some(tail_draw1)
	    with No_numeric_solution -> 
	      None
691
	  else
692
	    None
693 694 695 696 697 698 699 700 701
	in
	  match res_opt with 
	      Some(res) -> res
	    | None -> 
		(* The second branch is now tried because no path in
		   the first bdd leaded to a satisfiable set of
		   numeric constraints. *)
		if not (eq_sol_nb sol_nb2 zero_sol)
		then
702
		  if not (is_empty store2)
703 704 705 706 707
		  then    
		    if Bdd.is_true bdd2
		    then 
		      ( (List.append 
			   sl2 
708
			   (Util.list_map_option toss_up_one_var 
709
			      (Bdd.list_of_support new_comb))),
710 711 712 713 714
			store2
		      )
		    else
		      draw_in_bdd (sl2, store2) bdd2 new_comb 
		  else
715
		    raise No_numeric_solution
716 717
		else
		  raise No_numeric_solution
718

719

720 721 722 723
let (draw : vn list -> vnt list -> Bdd.t -> Bdd.t -> subst list * subst list) =
  fun bool_vars_to_gen num_vnt_to_gen comb bdd ->
    (** Draw the output and local vars to be generated by the environnent. *)
    let (bool_subst_l, store) = 
724
      draw_in_bdd ([], (new_store num_vnt_to_gen)) bdd comb 
725
    in
726
    let num_subst_l = draw_inside store in
727
    let subst_l = append bool_subst_l num_subst_l in
728
    let (out_vars, _) = List.split (Env_state.output_var_names ()) 
729 730 731
    in
      assert ( 
	(*  Checks that we generated all variables. *)
732 733
	let (gen_vars, _) = List.split subst_l in
	let (num_vars_to_gen, _) = List.split num_vnt_to_gen in
734 735 736 737 738 739 740
	let vars_to_gen = append bool_vars_to_gen num_vars_to_gen in
          (sort (compare) gen_vars) = (sort (compare) vars_to_gen) 
      );
      (* Splits output and local vars. *)
      List.partition 
	(fun (vn, _) -> List.mem vn out_vars) 
	subst_l
741

742

743
  
744
(* Exported *)
745
let (solve_formula: env_in -> int -> formula list -> vn list -> vnt list ->
746
       (subst list * subst list) list) =
747
  fun input p fl bool_vars_to_gen num_vars_to_gen ->
748
    let f = formula_list_to_conj fl in
749
    let bdd = 
750
      (* The bdd of f has necessarily been computed (by is_satisfiable) *)
751 752
      try Env_state.bdd f
      with Not_found -> Env_state.bdd_global f
753
    in
754
    let bool_vars_to_gen_f = 
755 756 757
      List.fold_left
	(fun acc vn -> (And(Bvar(vn), acc)))
	True
758
	bool_vars_to_gen
759
    in
760 761 762 763 764 765 766 767 768 769 770 771 772 773
    let (comb0, _) = formula_to_bdd input bool_vars_to_gen_f in
    let comb = 
      (* All boolean vars should appear in the comb so that when we
	 find that such a var is missing along a bdd path, we can
	 perform a (fair) toss for it. On the contrary, if a
	 numerical contraint disappear from a bdd (eg, consider [(f
	 && false) || true]), it is not important; fairly tossing a
	 (boolean) value for a num constaint [nc] and performing a
	 fair toss in the resulting domain is equivalent to directly
	 perform the toss in the (unconstraint wrt [nc]) initial
	 domain.  
      *)
      Bdd.dand (Bdd.support bdd) comb0 
    in
774
    let _ =
775
      if not (Env_state.sol_number_exists bdd)
776
      then
777 778 779 780 781
	let rec skip_unconstraint_bool_var_at_top comb v =
	  (* [build_sol_nb_table] supposes that the bdd and its comb 
	     have the same top var. 
	  *)
	  if Bdd.is_true comb then comb
782 783
	  else 
	    let topvar = (Bdd.topvar comb) in
784 785
	      if v = topvar then comb 
	      else skip_unconstraint_bool_var_at_top (Bdd.dthen comb) v
786
	in
787
	let comb2 = skip_unconstraint_bool_var_at_top comb (Bdd.topvar bdd) in 
788 789
	let _ = build_sol_nb_table bdd comb2 in
	  ()
790
    in
791 792
      try 	
	Util.unfold (draw bool_vars_to_gen num_vars_to_gen comb) bdd p
793
      with No_numeric_solution -> 
794
	Env_state.set_sol_number bdd (zero_sol, zero_sol);
795
	[]
796