src/HOL/Tools/sat_funcs.ML
author paulson
Fri Oct 20 11:04:15 2006 +0200 (2006-10-20 ago)
changeset 21070 0a898140fea2
parent 20486 02ca20e33030
child 21267 5294ecae6708
permissions -rw-r--r--
Added more debugging info
webertj@17618
     1
(*  Title:      HOL/Tools/sat_funcs.ML
webertj@17618
     2
    ID:         $Id$
webertj@17618
     3
    Author:     Stephan Merz and Alwen Tiu, QSL Team, LORIA (http://qsl.loria.fr)
webertj@17622
     4
    Author:     Tjark Weber
webertj@19236
     5
    Copyright   2005-2006
webertj@17618
     6
webertj@17618
     7
webertj@17618
     8
Proof reconstruction from SAT solvers.
webertj@17618
     9
webertj@17618
    10
  Description:
webertj@17618
    11
    This file defines several tactics to invoke a proof-producing
webertj@17618
    12
    SAT solver on a propositional goal in clausal form.
webertj@17618
    13
webertj@17618
    14
    We use a sequent presentation of clauses to speed up resolution
webertj@17695
    15
    proof reconstruction.
webertj@17695
    16
    We call such clauses "raw clauses", which are of the form
webertj@20440
    17
          [x1, ..., xn, P] |- False
webertj@19236
    18
    (note the use of |- instead of ==>, i.e. of Isabelle's (meta-)hyps here),
webertj@20440
    19
    where each xi is a literal (see also comments in cnf_funcs.ML).
webertj@17618
    20
webertj@19236
    21
    This does not work for goals containing schematic variables!
webertj@19236
    22
webertj@20039
    23
      The tactic produces a clause representation of the given goal
webertj@20039
    24
      in DIMACS format and invokes a SAT solver, which should return
webertj@20039
    25
      a proof consisting of a sequence of resolution steps, indicating
webertj@20039
    26
      the two input clauses, and resulting in new clauses, leading to
webertj@20039
    27
      the empty clause (i.e. "False").  The tactic replays this proof
webertj@20039
    28
      in Isabelle and thus solves the overall goal.
webertj@17618
    29
webertj@20440
    30
  There are three SAT tactics available.  They differ in the CNF transformation
webertj@20039
    31
  used. "sat_tac" uses naive CNF transformation to transform the theorem to be
webertj@20039
    32
  proved before giving it to the SAT solver.  The naive transformation in the
webertj@20440
    33
  worst case can lead to an exponential blow up in formula size.  Another
webertj@20039
    34
  tactic, "satx_tac", uses "definitional CNF transformation" which attempts to
webertj@20039
    35
  produce a formula of linear size increase compared to the input formula, at
webertj@20039
    36
  the cost of possibly introducing new variables.  See cnf_funcs.ML for more
webertj@20440
    37
  comments on the CNF transformation.  "rawsat_tac" should be used with
webertj@20440
    38
  caution: no CNF transformation is performed, and the tactic's behavior is
webertj@20440
    39
  undefined if the subgoal is not already given as [| C1; ...; Cn |] ==> False,
webertj@20440
    40
  where each Ci is a disjunction.
webertj@17618
    41
webertj@20039
    42
  The SAT solver to be used can be set via the "solver" reference.  See
webertj@20039
    43
  sat_solvers.ML for possible values, and etc/settings for required (solver-
webertj@20039
    44
  dependent) configuration settings.  To replay SAT proofs in Isabelle, you
webertj@20039
    45
  must of course use a proof-producing SAT solver in the first place.
webertj@20039
    46
webertj@20039
    47
  Proofs are replayed only if "!quick_and_dirty" is false.  If
webertj@20039
    48
  "!quick_and_dirty" is true, the theorem (in case the SAT solver claims its
webertj@20039
    49
  negation to be unsatisfiable) is proved via an oracle.
webertj@17618
    50
*)
webertj@17618
    51
webertj@17618
    52
signature SAT =
webertj@17618
    53
sig
webertj@20440
    54
	val trace_sat  : bool ref    (* print trace messages *)
webertj@20440
    55
	val solver     : string ref  (* name of SAT solver to be used *)
webertj@20440
    56
	val counter    : int ref     (* number of resolution steps during last proof replay *)
webertj@20440
    57
	val rawsat_tac : int -> Tactical.tactic
webertj@20440
    58
	val sat_tac    : int -> Tactical.tactic
webertj@20440
    59
	val satx_tac   : int -> Tactical.tactic
webertj@17618
    60
end
webertj@17618
    61
webertj@17622
    62
functor SATFunc (structure cnf : CNF) : SAT =
webertj@17622
    63
struct
webertj@17618
    64
webertj@17622
    65
val trace_sat = ref false;
webertj@17622
    66
webertj@20170
    67
val solver = ref "zchaff_with_proofs";  (* see HOL/Tools/sat_solver.ML for possible values *)
webertj@20039
    68
webertj@17622
    69
val counter = ref 0;
webertj@17622
    70
webertj@19236
    71
(* Thm.thm *)
webertj@19236
    72
val resolution_thm =  (* "[| P ==> False; ~P ==> False |] ==> False" *)
webertj@19236
    73
	let
webertj@19236
    74
		val cterm = cterm_of (the_context ())
webertj@19236
    75
		val Q     = Var (("Q", 0), HOLogic.boolT)
webertj@19236
    76
		val False = HOLogic.false_const
webertj@19236
    77
	in
webertj@19236
    78
		Thm.instantiate ([], [(cterm Q, cterm False)]) case_split_thm
webertj@19236
    79
	end;
webertj@17622
    80
webertj@20278
    81
(* Thm.cterm *)
webertj@20278
    82
val cP = cterm_of (theory_of_thm resolution_thm) (Var (("P", 0), HOLogic.boolT));
webertj@20278
    83
webertj@20278
    84
(* ------------------------------------------------------------------------- *)
webertj@20278
    85
(* CLAUSE: during proof reconstruction, three kinds of clauses are           *)
webertj@20278
    86
(*      distinguished:                                                       *)
webertj@20278
    87
(*      1. NO_CLAUSE: clause not proved (yet)                                *)
webertj@20278
    88
(*      2. ORIG_CLAUSE: a clause as it occurs in the original problem        *)
webertj@20278
    89
(*      3. RAW_CLAUSE: a raw clause, with additional precomputed information *)
webertj@20278
    90
(*         (a mapping from int's to its literals) for faster proof           *)
webertj@20278
    91
(*         reconstruction                                                    *)
webertj@20278
    92
(* ------------------------------------------------------------------------- *)
webertj@20278
    93
webertj@20278
    94
datatype CLAUSE = NO_CLAUSE
webertj@20278
    95
                | ORIG_CLAUSE of Thm.thm
webertj@20278
    96
                | RAW_CLAUSE of Thm.thm * Thm.cterm Inttab.table;
webertj@20278
    97
webertj@17622
    98
(* ------------------------------------------------------------------------- *)
webertj@17809
    99
(* resolve_raw_clauses: given a non-empty list of raw clauses, we fold       *)
webertj@17809
   100
(*      resolution over the list (starting with its head), i.e. with two raw *)
webertj@17809
   101
(*      clauses                                                              *)
webertj@20440
   102
(*        [P, x1, ..., a, ..., xn] |- False                                  *)
webertj@17622
   103
(*      and                                                                  *)
webertj@20440
   104
(*        [Q, y1, ..., a', ..., ym] |- False                                 *)
webertj@19976
   105
(*      (where a and a' are dual to each other), we convert the first clause *)
webertj@19976
   106
(*      to                                                                   *)
webertj@20440
   107
(*        [P, x1, ..., xn] |- a ==> False ,                                  *)
webertj@19976
   108
(*      the second clause to                                                 *)
webertj@20440
   109
(*        [Q, y1, ..., ym] |- a' ==> False                                   *)
webertj@19976
   110
(*      and then perform resolution with                                     *)
webertj@19976
   111
(*        [| ?P ==> False; ~?P ==> False |] ==> False                        *)
webertj@19976
   112
(*      to produce                                                           *)
webertj@20440
   113
(*        [P, Q, x1, ..., xn, y1, ..., ym] |- False                          *)
webertj@20278
   114
(*      Each clause is accompanied with a table mapping integers (positive   *)
webertj@20278
   115
(*      for positive literals, negative for negative literals, and the same  *)
webertj@20278
   116
(*      absolute value for dual literals) to the actual literals as cterms.  *)
webertj@17622
   117
(* ------------------------------------------------------------------------- *)
webertj@17618
   118
webertj@20278
   119
(* (Thm.thm * Thm.cterm Inttab.table) list -> Thm.thm * Thm.cterm Inttab.table *)
webertj@17622
   120
webertj@17809
   121
fun resolve_raw_clauses [] =
webertj@17809
   122
	raise THM ("Proof reconstruction failed (empty list of resolvents)!", 0, [])
webertj@17809
   123
  | resolve_raw_clauses (c::cs) =
webertj@17809
   124
	let
webertj@20278
   125
		(* find out which two hyps are used in the resolution *)
webertj@20278
   126
		local exception RESULT of int * Thm.cterm * Thm.cterm in
webertj@20278
   127
			(* Thm.cterm Inttab.table -> Thm.cterm Inttab.table -> int * Thm.cterm * Thm.cterm *)
webertj@20278
   128
			fun find_res_hyps hyp1_table hyp2_table = (
webertj@20278
   129
				Inttab.fold (fn (i, hyp1) => fn () =>
webertj@20278
   130
					case Inttab.lookup hyp2_table (~i) of
webertj@20278
   131
					  SOME hyp2 => raise RESULT (i, hyp1, hyp2)
webertj@20278
   132
					| NONE      => ()) hyp1_table ();
webertj@20278
   133
				raise THM ("Proof reconstruction failed (no literal for resolution)!", 0, [])
webertj@20278
   134
			) handle RESULT x => x
webertj@20278
   135
		end
webertj@19976
   136
webertj@20278
   137
		(* Thm.thm * Thm.cterm Inttab.table -> Thm.thm * Thm.cterm Inttab.table -> Thm.thm * Thm.cterm Inttab.table *)
webertj@20278
   138
		fun resolution (c1, hyp1_table) (c2, hyp2_table) =
webertj@17622
   139
		let
webertj@17622
   140
			val _ = if !trace_sat then
webertj@20440
   141
					tracing ("Resolving clause: " ^ string_of_thm c1 ^ " (hyps: " ^ string_of_list (Sign.string_of_term (theory_of_thm c1)) (#hyps (rep_thm c1))
webertj@20440
   142
						^ ")\nwith clause: " ^ string_of_thm c2 ^ " (hyps: " ^ string_of_list (Sign.string_of_term (theory_of_thm c2)) (#hyps (rep_thm c2)) ^ ")")
webertj@17622
   143
				else ()
webertj@17809
   144
webertj@20278
   145
			(* the two literals used for resolution *)
webertj@20278
   146
			val (i1, hyp1, hyp2) = find_res_hyps hyp1_table hyp2_table
webertj@20278
   147
			val hyp1_is_neg      = i1 < 0
webertj@19236
   148
webertj@20278
   149
			val c1' = Thm.implies_intr hyp1 c1  (* Gamma1 |- hyp1 ==> False *)
webertj@20278
   150
			val c2' = Thm.implies_intr hyp2 c2  (* Gamma2 |- hyp2 ==> False *)
webertj@17618
   151
webertj@20278
   152
			val res_thm =  (* |- (lit ==> False) ==> (~lit ==> False) ==> False *)
webertj@19236
   153
				let
webertj@20278
   154
					val cLit = snd (Thm.dest_comb (if hyp1_is_neg then hyp2 else hyp1))  (* strip Trueprop *)
webertj@19236
   155
				in
webertj@19976
   156
					Thm.instantiate ([], [(cP, cLit)]) resolution_thm
webertj@19236
   157
				end
webertj@19236
   158
webertj@19236
   159
			val _ = if !trace_sat then
webertj@19236
   160
					tracing ("Resolution theorem: " ^ string_of_thm res_thm)
webertj@19236
   161
				else ()
webertj@19236
   162
webertj@20440
   163
			(* Gamma1, Gamma2 |- False *)
webertj@20440
   164
			val c_new = Thm.implies_elim
webertj@20440
   165
				(Thm.implies_elim res_thm (if hyp1_is_neg then c2' else c1'))
webertj@20440
   166
				(if hyp1_is_neg then c1' else c2')
webertj@20278
   167
webertj@20278
   168
			(* since the mapping from integers to literals should be injective *)
webertj@20278
   169
			(* (over different clauses), 'K true' here should be equivalent to *)
webertj@20440
   170
			(* 'op=' (but slightly faster)                                     *)
webertj@20278
   171
			val hypnew_table = Inttab.merge (K true) (Inttab.delete i1 hyp1_table, Inttab.delete (~i1) hyp2_table)
webertj@19236
   172
webertj@19236
   173
			val _ = if !trace_sat then
webertj@20440
   174
					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)) ^ ")")
webertj@19236
   175
				else ()
webertj@17809
   176
			val _ = inc counter
webertj@17809
   177
		in
webertj@20278
   178
			(c_new, hypnew_table)
webertj@17809
   179
		end
webertj@17809
   180
	in
webertj@17809
   181
		fold resolution cs c
webertj@17809
   182
	end;
webertj@17618
   183
webertj@17622
   184
(* ------------------------------------------------------------------------- *)
webertj@17809
   185
(* replay_proof: replays the resolution proof returned by the SAT solver;    *)
webertj@17809
   186
(*      cf. SatSolver.proof for details of the proof format.  Updates the    *)
webertj@17809
   187
(*      'clauses' array with derived clauses, and returns the derived clause *)
webertj@17809
   188
(*      at index 'empty_id' (which should just be "False" if proof           *)
webertj@17809
   189
(*      reconstruction was successful, with the used clauses as hyps).       *)
webertj@20278
   190
(*      'atom_table' must contain an injective mapping from all atoms that   *)
webertj@20278
   191
(*      occur (as part of a literal) in 'clauses' to positive integers.      *)
webertj@17622
   192
(* ------------------------------------------------------------------------- *)
webertj@17622
   193
webertj@20278
   194
(* int Termtab.table -> CLAUSE Array.array -> SatSolver.proof -> Thm.thm *)
webertj@17618
   195
webertj@20278
   196
fun replay_proof atom_table clauses (clause_table, empty_id) =
webertj@17622
   197
let
webertj@20278
   198
	(* Thm.cterm -> int option *)
webertj@20278
   199
	fun index_of_literal chyp = (
webertj@20278
   200
		case (HOLogic.dest_Trueprop o term_of) chyp of
webertj@20278
   201
		  (Const ("Not", _) $ atom) =>
webertj@20278
   202
			SOME (~(valOf (Termtab.lookup atom_table atom)))
webertj@20278
   203
		| atom =>
webertj@20278
   204
			SOME (valOf (Termtab.lookup atom_table atom))
webertj@20278
   205
	) handle TERM _ => NONE;  (* 'chyp' is not a literal *)
webertj@20278
   206
webertj@20278
   207
	(* int -> Thm.thm * Thm.cterm Inttab.table *)
webertj@17623
   208
	fun prove_clause id =
webertj@17623
   209
		case Array.sub (clauses, id) of
webertj@20278
   210
		  RAW_CLAUSE clause =>
webertj@20278
   211
			clause
webertj@20278
   212
		| ORIG_CLAUSE thm =>
webertj@20278
   213
			(* convert the original clause *)
webertj@17623
   214
			let
webertj@20278
   215
				val _         = if !trace_sat then tracing ("Using original clause #" ^ string_of_int id) else ()
webertj@20278
   216
				val raw       = cnf.clause2raw_thm thm
webertj@20278
   217
				val lit_table = fold (fn chyp => fn lit_table => (case index_of_literal chyp of
webertj@20278
   218
					  SOME i => Inttab.update_new (i, chyp) lit_table
webertj@20278
   219
					| NONE   => lit_table)) (#hyps (Thm.crep_thm raw)) Inttab.empty
webertj@20278
   220
				val clause    = (raw, lit_table)
webertj@20278
   221
				val _         = Array.update (clauses, id, RAW_CLAUSE clause)
webertj@17623
   222
			in
webertj@20278
   223
				clause
webertj@20278
   224
			end
webertj@20278
   225
		| NO_CLAUSE =>
webertj@20278
   226
			(* prove the clause, using information from 'clause_table' *)
webertj@20278
   227
			let
webertj@20278
   228
				val _      = if !trace_sat then tracing ("Proving clause #" ^ string_of_int id ^ " ...") else ()
webertj@20278
   229
				val ids    = valOf (Inttab.lookup clause_table id)
webertj@20278
   230
				val clause = resolve_raw_clauses (map prove_clause ids)
webertj@20278
   231
				val _      = Array.update (clauses, id, RAW_CLAUSE clause)
webertj@20278
   232
				val _      = if !trace_sat then tracing ("Replay chain successful; clause stored at #" ^ string_of_int id) else ()
webertj@20278
   233
			in
webertj@20278
   234
				clause
webertj@17623
   235
			end
webertj@17618
   236
webertj@17809
   237
	val _            = counter := 0
webertj@20278
   238
	val empty_clause = fst (prove_clause empty_id)
webertj@17809
   239
	val _            = if !trace_sat then tracing ("Proof reconstruction successful; " ^ string_of_int (!counter) ^ " resolution step(s) total.") else ()
webertj@17622
   240
in
webertj@17809
   241
	empty_clause
webertj@17622
   242
end;
webertj@17622
   243
webertj@20278
   244
(* ------------------------------------------------------------------------- *)
webertj@20278
   245
(* string_of_prop_formula: return a human-readable string representation of  *)
webertj@20278
   246
(*      a 'prop_formula' (just for tracing)                                  *)
webertj@20278
   247
(* ------------------------------------------------------------------------- *)
webertj@20278
   248
webertj@17809
   249
(* PropLogic.prop_formula -> string *)
webertj@20278
   250
webertj@17809
   251
fun string_of_prop_formula PropLogic.True             = "True"
webertj@17809
   252
  | string_of_prop_formula PropLogic.False            = "False"
webertj@17809
   253
  | string_of_prop_formula (PropLogic.BoolVar i)      = "x" ^ string_of_int i
webertj@17809
   254
  | string_of_prop_formula (PropLogic.Not fm)         = "~" ^ string_of_prop_formula fm
webertj@17809
   255
  | string_of_prop_formula (PropLogic.Or (fm1, fm2))  = "(" ^ string_of_prop_formula fm1 ^ " v " ^ string_of_prop_formula fm2 ^ ")"
webertj@17809
   256
  | string_of_prop_formula (PropLogic.And (fm1, fm2)) = "(" ^ string_of_prop_formula fm1 ^ " & " ^ string_of_prop_formula fm2 ^ ")";
webertj@17622
   257
webertj@17622
   258
(* ------------------------------------------------------------------------- *)
webertj@17622
   259
(* rawsat_thm: run external SAT solver with the given clauses.  Reconstructs *)
webertj@17809
   260
(*      a proof from the resulting proof trace of the SAT solver.  Each      *)
webertj@17809
   261
(*      premise in 'prems' that is not a clause is ignored, and the theorem  *)
webertj@17809
   262
(*      returned is just "False" (with some clauses as hyps).                *)
webertj@17622
   263
(* ------------------------------------------------------------------------- *)
webertj@17618
   264
webertj@17809
   265
(* Thm.thm list -> Thm.thm *)
webertj@17809
   266
webertj@17809
   267
fun rawsat_thm prems =
webertj@17622
   268
let
webertj@17809
   269
	(* remove premises that equal "True" *)
webertj@17809
   270
	val non_triv_prems    = filter (fn thm =>
webertj@17809
   271
		(not_equal HOLogic.true_const o HOLogic.dest_Trueprop o prop_of) thm
webertj@17809
   272
			handle TERM ("dest_Trueprop", _) => true) prems
webertj@17809
   273
	(* remove non-clausal premises -- of course this shouldn't actually   *)
webertj@17809
   274
	(* remove anything as long as 'rawsat_thm' is only called after the   *)
webertj@17809
   275
	(* premises have been converted to clauses                            *)
webertj@17809
   276
	val clauses           = filter (fn thm =>
webertj@17809
   277
		((cnf.is_clause o HOLogic.dest_Trueprop o prop_of) thm handle TERM ("dest_Trueprop", _) => false)
webertj@17809
   278
		orelse (warning ("Ignoring non-clausal premise " ^ (string_of_cterm o cprop_of) thm); false)) non_triv_prems
webertj@17809
   279
	(* remove trivial clauses -- this is necessary because zChaff removes *)
webertj@17809
   280
	(* trivial clauses during preprocessing, and otherwise our clause     *)
webertj@17809
   281
	(* numbering would be off                                             *)
webertj@17809
   282
	val non_triv_clauses  = filter (not o cnf.clause_is_trivial o HOLogic.dest_Trueprop o prop_of) clauses
webertj@19534
   283
	val _                 = if !trace_sat then
webertj@19534
   284
			tracing ("Non-trivial clauses:\n" ^ space_implode "\n" (map (string_of_cterm o cprop_of) non_triv_clauses))
webertj@19534
   285
		else ()
webertj@17809
   286
	(* translate clauses from HOL terms to PropLogic.prop_formula *)
webertj@17809
   287
	val (fms, atom_table) = fold_map (PropLogic.prop_formula_of_term o HOLogic.dest_Trueprop o prop_of) non_triv_clauses Termtab.empty
webertj@17809
   288
	val _                 = if !trace_sat then
webertj@17809
   289
			tracing ("Invoking SAT solver on clauses:\n" ^ space_implode "\n" (map string_of_prop_formula fms))
webertj@17809
   290
		else ()
webertj@17809
   291
	val fm                = PropLogic.all fms
webertj@17842
   292
	(* unit -> Thm.thm *)
webertj@17842
   293
	fun make_quick_and_dirty_thm () = (
webertj@17842
   294
		if !trace_sat then
webertj@17842
   295
			tracing "'quick_and_dirty' is set: proof reconstruction skipped, using oracle instead."
webertj@17842
   296
		else ();
webertj@17842
   297
		(* of course just returning "False" is unsound; what we should return *)
webertj@17842
   298
		(* instead is "False" with all 'non_triv_clauses' as hyps -- but this *)
webertj@17842
   299
		(* might be rather slow, and it makes no real difference as long as   *)
webertj@17842
   300
		(* 'rawsat_thm' is only called from 'rawsat_tac'                      *)
webertj@17842
   301
		SkipProof.make_thm (the_context ()) (HOLogic.Trueprop $ HOLogic.false_const)
webertj@17842
   302
	)
webertj@17618
   303
in
webertj@20039
   304
	case SatSolver.invoke_solver (!solver) fm of
webertj@17842
   305
	  SatSolver.UNSATISFIABLE (SOME (clause_table, empty_id)) => (
webertj@17842
   306
		if !trace_sat then
webertj@17842
   307
			tracing ("Proof trace from SAT solver:\n" ^
webertj@20371
   308
				"clauses: " ^ string_of_list (string_of_pair string_of_int (string_of_list string_of_int)) (Inttab.dest clause_table) ^ "\n" ^
webertj@17842
   309
				"empty clause: " ^ string_of_int empty_id)
webertj@17842
   310
		else ();
webertj@17842
   311
		if !quick_and_dirty then
webertj@17842
   312
			make_quick_and_dirty_thm ()
webertj@17842
   313
		else
webertj@17842
   314
			let
webertj@20440
   315
				(* optimization: convert the given clauses from "[c_i] |- c_i" to *)
webertj@20440
   316
				(* "[c_1 && ... && c_n] |- c_i"; this avoids accumulation of      *)
webertj@20440
   317
				(* hypotheses during resolution                                   *)
webertj@20440
   318
				(* [c_1 && ... && c_n] |- c_1 && ... && c_n *)
webertj@20486
   319
				val cnf_cterm = Conjunction.mk_conjunction_list (map cprop_of non_triv_clauses)
webertj@20440
   320
				val cnf_thm   = Thm.assume cnf_cterm
webertj@20440
   321
				(* cf. Conjunction.elim_list *)
webertj@20440
   322
				fun right_elim_list th =
webertj@20440
   323
					let val (th1, th2) = Conjunction.elim th
webertj@20464
   324
					in th1 :: right_elim_list th2 end handle THM _ => [th]
webertj@20440
   325
				(* [[c_1 && ... && c_n] |- c_1, ..., [c_1 && ... && c_n] |- c_n] *)
webertj@20440
   326
				val converted_clauses = right_elim_list cnf_thm
webertj@20278
   327
				(* initialize the clause array with the given clauses *)
webertj@20440
   328
				val max_idx    = valOf (Inttab.max_key clause_table)
webertj@20440
   329
				val clause_arr = Array.array (max_idx + 1, NO_CLAUSE)
webertj@20440
   330
				val _          = fold (fn thm => fn idx => (Array.update (clause_arr, idx, ORIG_CLAUSE thm); idx+1)) converted_clauses 0
webertj@19236
   331
				(* replay the proof to derive the empty clause *)
webertj@20440
   332
				(* [c_1 && ... && c_n] |- False *)
webertj@20440
   333
				val FalseThm   = replay_proof atom_table clause_arr (clause_table, empty_id)
webertj@17842
   334
			in
webertj@20440
   335
				(* convert the &&-hyp back to the original clauses format *)
webertj@20440
   336
				FalseThm
webertj@20440
   337
				(* [] |- c_1 && ... && c_n ==> False *)
webertj@20440
   338
				|> Thm.implies_intr cnf_cterm
webertj@20440
   339
				(* c_1 ==> ... ==> c_n ==> False *)
webertj@20440
   340
				|> Conjunction.curry ~1
webertj@20440
   341
				(* [c_1, ..., c_n] |- False *)
webertj@20440
   342
				|> (fn thm => fold (fn cprem => fn thm' =>
webertj@20440
   343
					Thm.implies_elim thm' (Thm.assume cprem)) (cprems_of thm) thm)
webertj@17842
   344
			end)
webertj@17622
   345
	| SatSolver.UNSATISFIABLE NONE =>
webertj@17842
   346
		if !quick_and_dirty then (
webertj@17842
   347
			warning "SAT solver claims the formula to be unsatisfiable, but did not provide a proof";
webertj@17842
   348
			make_quick_and_dirty_thm ()
webertj@17842
   349
		) else
webertj@17842
   350
			raise THM ("SAT solver claims the formula to be unsatisfiable, but did not provide a proof", 0, [])
webertj@17622
   351
	| SatSolver.SATISFIABLE assignment =>
webertj@17622
   352
		let
webertj@17622
   353
			val msg = "SAT solver found a countermodel:\n"
webertj@17809
   354
				^ (commas
webertj@17809
   355
					o map (fn (term, idx) =>
webertj@17809
   356
						Sign.string_of_term (the_context ()) term ^ ": "
webertj@17809
   357
							^ (case assignment idx of NONE => "arbitrary" | SOME true => "true" | SOME false => "false")))
webertj@17809
   358
					(Termtab.dest atom_table)
webertj@17622
   359
		in
webertj@17622
   360
			raise THM (msg, 0, [])
webertj@17622
   361
		end
webertj@17622
   362
	| SatSolver.UNKNOWN =>
webertj@17622
   363
		raise THM ("SAT solver failed to decide the formula", 0, [])
webertj@17622
   364
end;
webertj@17618
   365
webertj@17622
   366
(* ------------------------------------------------------------------------- *)
webertj@17622
   367
(* Tactics                                                                   *)
webertj@17622
   368
(* ------------------------------------------------------------------------- *)
webertj@17618
   369
webertj@17809
   370
(* ------------------------------------------------------------------------- *)
webertj@17809
   371
(* rawsat_tac: solves the i-th subgoal of the proof state; this subgoal      *)
webertj@17809
   372
(*      should be of the form                                                *)
webertj@17809
   373
(*        [| c1; c2; ...; ck |] ==> False                                    *)
webertj@17809
   374
(*      where each cj is a non-empty clause (i.e. a disjunction of literals) *)
webertj@17809
   375
(*      or "True"                                                            *)
webertj@17809
   376
(* ------------------------------------------------------------------------- *)
webertj@17809
   377
webertj@17809
   378
(* int -> Tactical.tactic *)
webertj@17809
   379
webertj@17809
   380
fun rawsat_tac i = METAHYPS (fn prems => rtac (rawsat_thm prems) 1) i;
webertj@17618
   381
webertj@17809
   382
(* ------------------------------------------------------------------------- *)
webertj@17809
   383
(* pre_cnf_tac: converts the i-th subgoal                                    *)
webertj@17809
   384
(*        [| A1 ; ... ; An |] ==> B                                          *)
webertj@17809
   385
(*      to                                                                   *)
webertj@17809
   386
(*        [| A1; ... ; An ; ~B |] ==> False                                  *)
webertj@17809
   387
(*      (handling meta-logical connectives in B properly before negating),   *)
webertj@17809
   388
(*      then replaces meta-logical connectives in the premises (i.e. "==>",  *)
webertj@17809
   389
(*      "!!" and "==") by connectives of the HOL object-logic (i.e. by       *)
webertj@19553
   390
(*      "-->", "!", and "="), then performs beta-eta-normalization on the    *)
webertj@19553
   391
(*      subgoal                                                              *)
webertj@17809
   392
(* ------------------------------------------------------------------------- *)
webertj@17809
   393
webertj@17809
   394
(* int -> Tactical.tactic *)
webertj@17809
   395
webertj@20278
   396
fun pre_cnf_tac i =
webertj@20278
   397
	rtac ccontr i THEN ObjectLogic.atomize_tac i THEN
webertj@20278
   398
		PRIMITIVE (Drule.fconv_rule (Drule.goals_conv (equal i) (Drule.beta_eta_conversion)));
webertj@17809
   399
webertj@17809
   400
(* ------------------------------------------------------------------------- *)
webertj@17809
   401
(* cnfsat_tac: checks if the empty clause "False" occurs among the premises; *)
webertj@17809
   402
(*      if not, eliminates conjunctions (i.e. each clause of the CNF formula *)
webertj@17809
   403
(*      becomes a separate premise), then applies 'rawsat_tac' to solve the  *)
webertj@17809
   404
(*      subgoal                                                              *)
webertj@17809
   405
(* ------------------------------------------------------------------------- *)
webertj@17697
   406
webertj@17809
   407
(* int -> Tactical.tactic *)
webertj@17809
   408
webertj@20278
   409
fun cnfsat_tac i =
webertj@20278
   410
	(etac FalseE i) ORELSE (REPEAT_DETERM (etac conjE i) THEN rawsat_tac i);
webertj@17618
   411
webertj@17809
   412
(* ------------------------------------------------------------------------- *)
webertj@17809
   413
(* cnfxsat_tac: checks if the empty clause "False" occurs among the          *)
webertj@17809
   414
(*      premises; if not, eliminates conjunctions (i.e. each clause of the   *)
webertj@17809
   415
(*      CNF formula becomes a separate premise) and existential quantifiers, *)
webertj@17809
   416
(*      then applies 'rawsat_tac' to solve the subgoal                       *)
webertj@17809
   417
(* ------------------------------------------------------------------------- *)
webertj@17809
   418
webertj@17809
   419
(* int -> Tactical.tactic *)
webertj@17809
   420
webertj@20278
   421
fun cnfxsat_tac i =
webertj@20278
   422
	(etac FalseE i) ORELSE
webertj@20278
   423
		(REPEAT_DETERM (etac conjE i ORELSE etac exE i) THEN rawsat_tac i);
webertj@17618
   424
webertj@17809
   425
(* ------------------------------------------------------------------------- *)
webertj@17809
   426
(* sat_tac: tactic for calling an external SAT solver, taking as input an    *)
webertj@17809
   427
(*      arbitrary formula.  The input is translated to CNF, possibly causing *)
webertj@17809
   428
(*      an exponential blowup.                                               *)
webertj@17809
   429
(* ------------------------------------------------------------------------- *)
webertj@17809
   430
webertj@17809
   431
(* int -> Tactical.tactic *)
webertj@17809
   432
webertj@20278
   433
fun sat_tac i =
webertj@20278
   434
	pre_cnf_tac i THEN cnf.cnf_rewrite_tac i THEN cnfsat_tac i;
webertj@17809
   435
webertj@17809
   436
(* ------------------------------------------------------------------------- *)
webertj@17809
   437
(* satx_tac: tactic for calling an external SAT solver, taking as input an   *)
webertj@17809
   438
(*      arbitrary formula.  The input is translated to CNF, possibly         *)
webertj@17809
   439
(*      introducing new literals.                                            *)
webertj@17809
   440
(* ------------------------------------------------------------------------- *)
webertj@17809
   441
webertj@17809
   442
(* int -> Tactical.tactic *)
webertj@17809
   443
webertj@20278
   444
fun satx_tac i =
webertj@20278
   445
	pre_cnf_tac i THEN cnf.cnfx_rewrite_tac i THEN cnfxsat_tac i;
webertj@17618
   446
webertj@20039
   447
end;  (* of structure SATFunc *)