faster clause representation (again): full CNF formula as a hypothesis, instead of separate clauses
authorwebertj
Wed Aug 30 16:27:53 2006 +0200 (2006-08-30)
changeset 20440e6fe74eebda3
parent 20439 1bf42b262a38
child 20441 a9034285b96b
faster clause representation (again): full CNF formula as a hypothesis, instead of separate clauses
src/HOL/Tools/cnf_funcs.ML
src/HOL/Tools/sat_funcs.ML
src/HOL/ex/SAT_Examples.thy
     1.1 --- a/src/HOL/Tools/cnf_funcs.ML	Wed Aug 30 15:11:17 2006 +0200
     1.2 +++ b/src/HOL/Tools/cnf_funcs.ML	Wed Aug 30 16:27:53 2006 +0200
     1.3 @@ -23,11 +23,10 @@
     1.4    representation of clauses, which we call the "raw clauses".
     1.5    Raw clauses are of the form
     1.6  
     1.7 -      x1', x2', ..., xn', ~ (x1 | x2 | ... | xn) ==> False |- False ,
     1.8 +      [..., x1', x2', ..., xn'] |- False ,
     1.9  
    1.10    where each xi is a literal, and each xi' is the negation normal form of ~xi.
    1.11  
    1.12 -  Raw clauses may contain further hyps of the form "~ clause ==> False".
    1.13    Literals are successively removed from the hyps of raw clauses by resolution
    1.14    during SAT proof reconstruction.
    1.15  *)
    1.16 @@ -39,8 +38,7 @@
    1.17  	val is_clause         : Term.term -> bool
    1.18  	val clause_is_trivial : Term.term -> bool
    1.19  
    1.20 -	val clause2raw_thm         : Thm.thm -> Thm.thm
    1.21 -	val rawhyps2clausehyps_thm : Thm.thm -> Thm.thm
    1.22 +	val clause2raw_thm : Thm.thm -> Thm.thm
    1.23  
    1.24  	val weakening_tac : int -> Tactical.tactic  (* removes the first hypothesis of a subgoal *)
    1.25  
    1.26 @@ -53,8 +51,7 @@
    1.27  structure cnf : CNF =
    1.28  struct
    1.29  
    1.30 -(* string -> Thm.thm *)
    1.31 -fun thm_by_auto G =
    1.32 +fun thm_by_auto (G : string) : thm =
    1.33  	prove_goal (the_context ()) G (fn prems => [cut_facts_tac prems 1, Auto_tac]);
    1.34  
    1.35  (* Thm.thm *)
    1.36 @@ -140,59 +137,42 @@
    1.37  
    1.38  (* ------------------------------------------------------------------------- *)
    1.39  (* clause2raw_thm: translates a clause into a raw clause, i.e.               *)
    1.40 -(*        ... |- x1 | ... | xn                                               *)
    1.41 +(*        [...] |- x1 | ... | xn                                             *)
    1.42  (*      (where each xi is a literal) is translated to                        *)
    1.43 -(*        ~(x1 | ... | xn) ==> False, x1', ..., xn' |- False ,               *)
    1.44 -(*      where each xi' is the negation normal form of ~xi.                   *)
    1.45 +(*        [..., x1', ..., xn'] |- False ,                                    *)
    1.46 +(*      where each xi' is the negation normal form of ~xi                    *)
    1.47  (* ------------------------------------------------------------------------- *)
    1.48  
    1.49  (* Thm.thm -> Thm.thm *)
    1.50  
    1.51 -fun clause2raw_thm c =
    1.52 +fun clause2raw_thm clause =
    1.53  let
    1.54 -	val disj               = HOLogic.dest_Trueprop (prop_of c)
    1.55 -	val not_disj_imp_false = Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_not disj), HOLogic.mk_Trueprop HOLogic.false_const)
    1.56 -	val thm1 = Thm.assume (cterm_of (theory_of_thm c) not_disj_imp_false)  (* ~(x1 | ... | xn) ==> False |- ~(x1 | ... | xn) ==> False *)
    1.57  	(* eliminates negated disjunctions from the i-th premise, possibly *)
    1.58  	(* adding new premises, then continues with the (i+1)-th premise   *)
    1.59 -	(* Thm.thm -> int -> Thm.thm *)
    1.60 -	fun not_disj_to_prem thm i =
    1.61 +	(* int -> Thm.thm -> Thm.thm *)
    1.62 +	fun not_disj_to_prem i thm =
    1.63  		if i > nprems_of thm then
    1.64  			thm
    1.65  		else
    1.66 -			not_disj_to_prem (Seq.hd (REPEAT_DETERM (rtac clause2raw_not_disj i) thm)) (i+1)
    1.67 -	val thm2 = not_disj_to_prem thm1 1  (* ~(x1 | ... | xn) ==> False |- ~x1 ==> ... ==> ~xn ==> False *)
    1.68 -	val thm3 = Seq.hd (TRYALL (rtac clause2raw_not_not) thm2)  (* ~(x1 | ... | xn) ==> False |- x1' ==> ... ==> xn' ==> False *)
    1.69 -	val thy3 = theory_of_thm thm3
    1.70 -	val thm4 = fold (fn prem => fn thm => Thm.implies_elim thm (Thm.assume (cterm_of thy3 prem))) (prems_of thm3) thm3  (* ~(x1 | ... | xn) ==> False, x1', ..., xn' |- False *)
    1.71 +			not_disj_to_prem (i+1) (Seq.hd (REPEAT_DETERM (rtac clause2raw_not_disj i) thm))
    1.72 +	(* moves all premises to hyps, i.e. "[...] |- A1 ==> ... ==> An ==> B" *)
    1.73 +	(* becomes "[..., A1, ..., An] |- B"                                   *)
    1.74 +	(* Thm.thm -> Thm.thm *)
    1.75 +	fun prems_to_hyps thm =
    1.76 +		fold (fn cprem => fn thm' =>
    1.77 +			Thm.implies_elim thm' (Thm.assume cprem)) (cprems_of thm) thm
    1.78  in
    1.79 -	thm4
    1.80 +	(* [...] |- ~(x1 | ... | xn) ==> False *)
    1.81 +	(clause2raw_notE OF [clause])
    1.82 +	(* [...] |- ~x1 ==> ... ==> ~xn ==> False *)
    1.83 +	|> not_disj_to_prem 1
    1.84 +	(* [...] |- x1' ==> ... ==> xn' ==> False *)
    1.85 +	|> Seq.hd o TRYALL (rtac clause2raw_not_not)
    1.86 +	(* [..., x1', ..., xn'] |- False *)
    1.87 +	|> prems_to_hyps
    1.88  end;
    1.89  
    1.90  (* ------------------------------------------------------------------------- *)
    1.91 -(* rawhyps2clausehyps_thm: translates all hyps of the form "~P ==> False" to *)
    1.92 -(*      hyps of the form "P"                                                 *)
    1.93 -(* ------------------------------------------------------------------------- *)
    1.94 -
    1.95 -(* Thm.thm -> Thm.thm *)
    1.96 -
    1.97 -fun rawhyps2clausehyps_thm c =
    1.98 -	fold (fn hyp => fn thm =>
    1.99 -		case hyp of Const ("==>", _) $ (Const ("Trueprop", _) $ (Const ("Not", _) $ P)) $ (Const ("Trueprop", _) $ Const ("False", _)) =>
   1.100 -			let
   1.101 -				val cterm = cterm_of (theory_of_thm thm)
   1.102 -				val thm1  = Thm.implies_intr (cterm hyp) thm  (* hyps |- (~P ==> False) ==> goal *)
   1.103 -				val varP  = Var (("P", 0), HOLogic.boolT)
   1.104 -				val notE  = Thm.instantiate ([], [(cterm varP, cterm P)]) clause2raw_notE  (* P ==> ~P ==> False *)
   1.105 -				val notE' = Thm.implies_elim notE (Thm.assume (cterm (HOLogic.mk_Trueprop P)))  (* P |- ~P ==> False *)
   1.106 -				val thm2  = Thm.implies_elim thm1 notE'  (* hyps, P |- goal *)
   1.107 -			in
   1.108 -				thm2
   1.109 -			end
   1.110 -		          | _                                                                                                                  =>
   1.111 -			thm) (#hyps (rep_thm c)) c;
   1.112 -
   1.113 -(* ------------------------------------------------------------------------- *)
   1.114  (* inst_thm: instantiates a theorem with a list of terms                     *)
   1.115  (* ------------------------------------------------------------------------- *)
   1.116  
     2.1 --- a/src/HOL/Tools/sat_funcs.ML	Wed Aug 30 15:11:17 2006 +0200
     2.2 +++ b/src/HOL/Tools/sat_funcs.ML	Wed Aug 30 16:27:53 2006 +0200
     2.3 @@ -14,11 +14,9 @@
     2.4      We use a sequent presentation of clauses to speed up resolution
     2.5      proof reconstruction.
     2.6      We call such clauses "raw clauses", which are of the form
     2.7 -          x1; ...; xn; c1; c2; ...; ck |- False
     2.8 +          [x1, ..., xn, P] |- False
     2.9      (note the use of |- instead of ==>, i.e. of Isabelle's (meta-)hyps here),
    2.10 -    where each clause ci is of the form
    2.11 -          ~ (l1 | l2 | ... | lm) ==> False,
    2.12 -    where each xi and each li is a literal (see also comments in cnf_funcs.ML).
    2.13 +    where each xi is a literal (see also comments in cnf_funcs.ML).
    2.14  
    2.15      This does not work for goals containing schematic variables!
    2.16  
    2.17 @@ -29,14 +27,17 @@
    2.18        the empty clause (i.e. "False").  The tactic replays this proof
    2.19        in Isabelle and thus solves the overall goal.
    2.20  
    2.21 -  There are two SAT tactics available.  They differ in the CNF transformation
    2.22 +  There are three SAT tactics available.  They differ in the CNF transformation
    2.23    used. "sat_tac" uses naive CNF transformation to transform the theorem to be
    2.24    proved before giving it to the SAT solver.  The naive transformation in the
    2.25 -  worst case can lead to an exponential blow up in formula size.  The other
    2.26 +  worst case can lead to an exponential blow up in formula size.  Another
    2.27    tactic, "satx_tac", uses "definitional CNF transformation" which attempts to
    2.28    produce a formula of linear size increase compared to the input formula, at
    2.29    the cost of possibly introducing new variables.  See cnf_funcs.ML for more
    2.30 -  comments on the CNF transformation.
    2.31 +  comments on the CNF transformation.  "rawsat_tac" should be used with
    2.32 +  caution: no CNF transformation is performed, and the tactic's behavior is
    2.33 +  undefined if the subgoal is not already given as [| C1; ...; Cn |] ==> False,
    2.34 +  where each Ci is a disjunction.
    2.35  
    2.36    The SAT solver to be used can be set via the "solver" reference.  See
    2.37    sat_solvers.ML for possible values, and etc/settings for required (solver-
    2.38 @@ -50,11 +51,12 @@
    2.39  
    2.40  signature SAT =
    2.41  sig
    2.42 -	val trace_sat : bool ref    (* print trace messages *)
    2.43 -	val solver    : string ref  (* name of SAT solver to be used *)
    2.44 -	val counter   : int ref     (* number of resolution steps during last proof replay *)
    2.45 -	val sat_tac   : int -> Tactical.tactic
    2.46 -	val satx_tac  : int -> Tactical.tactic
    2.47 +	val trace_sat  : bool ref    (* print trace messages *)
    2.48 +	val solver     : string ref  (* name of SAT solver to be used *)
    2.49 +	val counter    : int ref     (* number of resolution steps during last proof replay *)
    2.50 +	val rawsat_tac : int -> Tactical.tactic
    2.51 +	val sat_tac    : int -> Tactical.tactic
    2.52 +	val satx_tac   : int -> Tactical.tactic
    2.53  end
    2.54  
    2.55  functor SATFunc (structure cnf : CNF) : SAT =
    2.56 @@ -97,18 +99,18 @@
    2.57  (* resolve_raw_clauses: given a non-empty list of raw clauses, we fold       *)
    2.58  (*      resolution over the list (starting with its head), i.e. with two raw *)
    2.59  (*      clauses                                                              *)
    2.60 -(*         x1; ... ; a; ...; xn |- False                                     *)
    2.61 +(*        [P, x1, ..., a, ..., xn] |- False                                  *)
    2.62  (*      and                                                                  *)
    2.63 -(*        y1; ... ; a'; ...; ym |- False                                     *)
    2.64 +(*        [Q, y1, ..., a', ..., ym] |- False                                 *)
    2.65  (*      (where a and a' are dual to each other), we convert the first clause *)
    2.66  (*      to                                                                   *)
    2.67 -(*        x1; ...; xn |- a ==> False ,                                       *)
    2.68 +(*        [P, x1, ..., xn] |- a ==> False ,                                  *)
    2.69  (*      the second clause to                                                 *)
    2.70 -(*        y1; ...; ym |- a' ==> False                                        *)
    2.71 +(*        [Q, y1, ..., ym] |- a' ==> False                                   *)
    2.72  (*      and then perform resolution with                                     *)
    2.73  (*        [| ?P ==> False; ~?P ==> False |] ==> False                        *)
    2.74  (*      to produce                                                           *)
    2.75 -(*        x1; ...; xn; y1; ...; ym |- False                                  *)
    2.76 +(*        [P, Q, x1, ..., xn, y1, ..., ym] |- False                          *)
    2.77  (*      Each clause is accompanied with a table mapping integers (positive   *)
    2.78  (*      for positive literals, negative for negative literals, and the same  *)
    2.79  (*      absolute value for dual literals) to the actual literals as cterms.  *)
    2.80 @@ -136,8 +138,8 @@
    2.81  		fun resolution (c1, hyp1_table) (c2, hyp2_table) =
    2.82  		let
    2.83  			val _ = if !trace_sat then
    2.84 -					tracing ("Resolving clause: " ^ string_of_thm c1 ^ " (hyps: " ^ space_implode ", " (map (Sign.string_of_term (theory_of_thm c1)) (#hyps (rep_thm c1)))
    2.85 -						^ ")\nwith clause: " ^ string_of_thm c2 ^ " (hyps: " ^ space_implode ", " (map (Sign.string_of_term (theory_of_thm c2)) (#hyps (rep_thm c2))) ^ ")")
    2.86 +					tracing ("Resolving clause: " ^ string_of_thm c1 ^ " (hyps: " ^ string_of_list (Sign.string_of_term (theory_of_thm c1)) (#hyps (rep_thm c1))
    2.87 +						^ ")\nwith clause: " ^ string_of_thm c2 ^ " (hyps: " ^ string_of_list (Sign.string_of_term (theory_of_thm c2)) (#hyps (rep_thm c2)) ^ ")")
    2.88  				else ()
    2.89  
    2.90  			(* the two literals used for resolution *)
    2.91 @@ -158,15 +160,18 @@
    2.92  					tracing ("Resolution theorem: " ^ string_of_thm res_thm)
    2.93  				else ()
    2.94  
    2.95 -			val c_new = Thm.implies_elim (Thm.implies_elim res_thm (if hyp1_is_neg then c2' else c1')) (if hyp1_is_neg then c1' else c2')  (* Gamma1, Gamma2 |- False *)
    2.96 +			(* Gamma1, Gamma2 |- False *)
    2.97 +			val c_new = Thm.implies_elim
    2.98 +				(Thm.implies_elim res_thm (if hyp1_is_neg then c2' else c1'))
    2.99 +				(if hyp1_is_neg then c1' else c2')
   2.100  
   2.101  			(* since the mapping from integers to literals should be injective *)
   2.102  			(* (over different clauses), 'K true' here should be equivalent to *)
   2.103 -			(* 'op=' (but faster)                                              *)
   2.104 +			(* 'op=' (but slightly faster)                                     *)
   2.105  			val hypnew_table = Inttab.merge (K true) (Inttab.delete i1 hyp1_table, Inttab.delete (~i1) hyp2_table)
   2.106  
   2.107  			val _ = if !trace_sat then
   2.108 -					tracing ("Resulting clause: " ^ string_of_thm c_new ^ " (hyps: " ^ space_implode ", " (map (Sign.string_of_term (theory_of_thm c_new)) (#hyps (rep_thm c_new))) ^ ")")
   2.109 +					tracing ("Resulting clause: " ^ string_of_thm c_new ^ " (hyps: " ^ string_of_list (Sign.string_of_term (theory_of_thm c_new)) (#hyps (rep_thm c_new)) ^ ")")
   2.110  				else ()
   2.111  			val _ = inc counter
   2.112  		in
   2.113 @@ -307,15 +312,38 @@
   2.114  			make_quick_and_dirty_thm ()
   2.115  		else
   2.116  			let
   2.117 +				(* optimization: convert the given clauses from "[c_i] |- c_i" to *)
   2.118 +				(* "[c_1 && ... && c_n] |- c_i"; this avoids accumulation of      *)
   2.119 +				(* hypotheses during resolution                                   *)
   2.120 +				(* Thm.cterm list -> Thm.cterm *)
   2.121 +				fun mk_conjunction_list [x]     = x
   2.122 +				  | mk_conjunction_list (x::xs) = Conjunction.mk_conjunction (x, mk_conjunction_list xs)
   2.123 +				(* [c_1 && ... && c_n] |- c_1 && ... && c_n *)
   2.124 +				val cnf_cterm = mk_conjunction_list (map cprop_of non_triv_clauses)
   2.125 +				val cnf_thm   = Thm.assume cnf_cterm
   2.126 +				(* cf. Conjunction.elim_list *)
   2.127 +				fun right_elim_list th =
   2.128 +					let val (th1, th2) = Conjunction.elim th
   2.129 +					in [th1] @ right_elim_list th2 end handle THM _ => [th]
   2.130 +				(* [[c_1 && ... && c_n] |- c_1, ..., [c_1 && ... && c_n] |- c_n] *)
   2.131 +				val converted_clauses = right_elim_list cnf_thm
   2.132  				(* initialize the clause array with the given clauses *)
   2.133 -				val max_idx     = valOf (Inttab.max_key clause_table)
   2.134 -				val clause_arr  = Array.array (max_idx + 1, NO_CLAUSE)
   2.135 -				val _           = fold (fn thm => fn idx => (Array.update (clause_arr, idx, ORIG_CLAUSE thm); idx+1)) non_triv_clauses 0
   2.136 +				val max_idx    = valOf (Inttab.max_key clause_table)
   2.137 +				val clause_arr = Array.array (max_idx + 1, NO_CLAUSE)
   2.138 +				val _          = fold (fn thm => fn idx => (Array.update (clause_arr, idx, ORIG_CLAUSE thm); idx+1)) converted_clauses 0
   2.139  				(* replay the proof to derive the empty clause *)
   2.140 -				val FalseThm    = replay_proof atom_table clause_arr (clause_table, empty_id)
   2.141 +				(* [c_1 && ... && c_n] |- False *)
   2.142 +				val FalseThm   = replay_proof atom_table clause_arr (clause_table, empty_id)
   2.143  			in
   2.144 -				(* convert the hyps back to the original format *)
   2.145 -				cnf.rawhyps2clausehyps_thm FalseThm
   2.146 +				(* convert the &&-hyp back to the original clauses format *)
   2.147 +				FalseThm
   2.148 +				(* [] |- c_1 && ... && c_n ==> False *)
   2.149 +				|> Thm.implies_intr cnf_cterm
   2.150 +				(* c_1 ==> ... ==> c_n ==> False *)
   2.151 +				|> Conjunction.curry ~1
   2.152 +				(* [c_1, ..., c_n] |- False *)
   2.153 +				|> (fn thm => fold (fn cprem => fn thm' =>
   2.154 +					Thm.implies_elim thm' (Thm.assume cprem)) (cprems_of thm) thm)
   2.155  			end)
   2.156  	| SatSolver.UNSATISFIABLE NONE =>
   2.157  		if !quick_and_dirty then (
     3.1 --- a/src/HOL/ex/SAT_Examples.thy	Wed Aug 30 15:11:17 2006 +0200
     3.2 +++ b/src/HOL/ex/SAT_Examples.thy	Wed Aug 30 16:27:53 2006 +0200
     3.3 @@ -82,6 +82,9 @@
     3.4  ML {* reset sat.trace_sat; *}
     3.5  ML {* reset quick_and_dirty; *}
     3.6  
     3.7 +method_setup rawsat = {* Method.no_args (Method.SIMPLE_METHOD (sat.rawsat_tac 1)) *}
     3.8 +  "SAT solver (no preprocessing)"
     3.9 +
    3.10  ML {* Toplevel.profiling := 1; *}
    3.11  
    3.12  (* Translated from TPTP problem library: PUZ015-2.006.dimacs *)
    3.13 @@ -281,7 +284,10 @@
    3.14  140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    3.15  160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    3.16  180 181 182 183 184
    3.17 +(*
    3.18  by sat
    3.19 +*)
    3.20 +by rawsat  -- {* this is without CNF conversion time *}
    3.21  
    3.22  (* Translated from TPTP problem library: MSC007-1.008.dimacs *)
    3.23  
    3.24 @@ -501,11 +507,23 @@
    3.25  160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    3.26  180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    3.27  200 201 202 203 204
    3.28 +(*
    3.29  by sat
    3.30 +*)
    3.31 +by rawsat  -- {* this is without CNF conversion time *}
    3.32 +
    3.33 +(* Old sequent clause representation ("[c_i, p, ~q, \<dots>] \<turnstile> False"):
    3.34 +   sat, zchaff_with_proofs: 8705 resolution steps in +++ User 1.154  All 1.189 secs
    3.35 +   sat, minisat_with_proofs: 40790 resolution steps in +++ User 3.762  All 3.806 secs
    3.36  
    3.37 -(* zchaff_with_proofs: 8705 resolution steps in +++ User 1.154  All 1.189 secs
    3.38 -   minisat_with_proofs: 40790 resolution steps in +++ User 3.762  All 3.806 secs
    3.39 -   (as of 2006-08-01, on a 2.5 GHz Pentium 4)
    3.40 +   rawsat, zchaff_with_proofs: 8705 resolution steps in +++ User 0.731  All 0.751 secs
    3.41 +   rawsat, minisat_with_proofs: 40790 resolution steps in +++ User 3.514  All 3.550 secs
    3.42 +
    3.43 +   CNF clause representation ("[c_1 && \<dots> && c_n, p, ~q, \<dots>] \<turnstile> False"):
    3.44 +   rawsat, zchaff_with_proofs: 8705 resolution steps in +++ User 0.653  All 0.670 secs
    3.45 +   rawsat, minisat_with_proofs: 40790 resolution steps in +++ User 1.860  All 1.886 secs
    3.46 +
    3.47 +   (as of 2006-08-30, on a 2.5 GHz Pentium 4)
    3.48  *)
    3.49  
    3.50  ML {* Toplevel.profiling := 0; *}