src/HOL/Tools/cnf_funcs.ML
changeset 17809 195045659c06
parent 17618 1330157e156a
child 19236 150e8b0fb991
     1.1 --- a/src/HOL/Tools/cnf_funcs.ML	Sat Oct 08 23:43:15 2005 +0200
     1.2 +++ b/src/HOL/Tools/cnf_funcs.ML	Sun Oct 09 17:06:03 2005 +0200
     1.3 @@ -1,660 +1,579 @@
     1.4  (*  Title:      HOL/Tools/cnf_funcs.ML
     1.5      ID:         $Id$
     1.6      Author:     Alwen Tiu, QSL Team, LORIA (http://qsl.loria.fr)
     1.7 +    Author:     Tjark Weber
     1.8      Copyright   2005
     1.9  
    1.10    Description:
    1.11 -  This file contains functions and tactics to transform a formula into 
    1.12 -  Conjunctive Normal Forms (CNF). 
    1.13 +  This file contains functions and tactics to transform a formula into
    1.14 +  Conjunctive Normal Form (CNF).
    1.15    A formula in CNF is of the following form:
    1.16  
    1.17 -      (x11 | x12 | .. x1m) & ... & (xm1 | xm2 | ... | xmn)
    1.18 +      (x11 | x12 | .. x1n) & ... & (xm1 | xm2 | ... | xmk)
    1.19 +      False
    1.20 +      True
    1.21  
    1.22 -  where each xij is a literal (i.e., positive or negative propositional
    1.23 -  variables).
    1.24 -  This kind of formula will simply be referred to as CNF.
    1.25 -  A disjunction of literals is referred to as "clause".
    1.26 +  where each xij is a literal (a positive or negative atomic Boolean term),
    1.27 +  i.e. the formula is a conjunction of disjunctions of literals, or
    1.28 +  "False", or "True".
    1.29 +
    1.30 +  A (non-empty) disjunction of literals is referred to as "clause".
    1.31  
    1.32    For the purpose of SAT proof reconstruction, we also make use of another
    1.33    representation of clauses, which we call the "raw clauses".
    1.34    Raw clauses are of the form
    1.35  
    1.36 -      (x1 ==> x2 ==> .. ==> xn ==> False)
    1.37 +      x1 ==> x2 ==> .. ==> xn ==> False ,
    1.38  
    1.39    where each xi is a literal. Note that the above raw clause corresponds
    1.40    to the clause (x1' | ... | xn'), where each xi' is the negation normal
    1.41    form of ~xi.
    1.42 -
    1.43 -  Notes for current revision:
    1.44 -  - the "definitional CNF transformation" (anything with prefix cnfx_ )
    1.45 -    introduces new literals of the form (lit_i) where i is an integer.
    1.46 -    For these functions to work, it is necessary that no free variables
    1.47 -    which names are of the form lit_i appears in the formula being
    1.48 -    transformed.
    1.49  *)
    1.50  
    1.51 -
    1.52 -(***************************************************************************)
    1.53 -
    1.54  signature CNF =
    1.55  sig
    1.56 -  val cnf_tac : Tactical.tactic
    1.57 -  val cnf_thin_tac : Tactical.tactic
    1.58 -  val cnfx_thin_tac : Tactical.tactic
    1.59 -  val cnf_concl_tac : Tactical.tactic
    1.60 -  val weakening_tac : int -> Tactical.tactic
    1.61 -  val mk_cnf_thm : Sign.sg -> Term.term -> Thm.thm
    1.62 -  val mk_cnfx_thm : Sign.sg -> Term.term ->  Thm.thm
    1.63 -  val is_atm : Term.term -> bool
    1.64 -  val is_lit : Term.term -> bool
    1.65 -  val is_clause : Term.term -> bool
    1.66 -  val is_raw_clause : Term.term -> bool
    1.67 -  val cnf2raw_thm : Thm.thm -> Thm.thm
    1.68 -  val cnf2raw_thms : Thm.thm list -> Thm.thm list
    1.69 -  val cnf2prop : Thm.thm list -> (PropLogic.prop_formula * ((Term.term * int) list))
    1.70 -end
    1.71 +	val is_atom           : Term.term -> bool
    1.72 +	val is_literal        : Term.term -> bool
    1.73 +	val is_clause         : Term.term -> bool
    1.74 +	val clause_is_trivial : Term.term -> bool
    1.75 +
    1.76 +	val is_raw_clause  : Term.term -> bool
    1.77 +	val clause2raw_thm : Thm.thm -> Thm.thm
    1.78  
    1.79 +	val weakening_tac : int -> Tactical.tactic  (* removes the first hypothesis of a subgoal *)
    1.80  
    1.81 -(***************************************************************************)
    1.82 +	val make_cnf_thm  : Theory.theory -> Term.term -> Thm.thm
    1.83 +	val make_cnfx_thm : Theory.theory -> Term.term ->  Thm.thm
    1.84 +	val cnf_rewrite_tac  : int -> Tactical.tactic  (* converts all prems of a subgoal to CNF *)
    1.85 +	val cnfx_rewrite_tac : int -> Tactical.tactic  (* converts all prems of a subgoal to (almost) definitional CNF *)
    1.86 +end;
    1.87  
    1.88  structure cnf : CNF =
    1.89  struct
    1.90  
    1.91 -val cur_thy = the_context();
    1.92 -val mk_disj = HOLogic.mk_disj;
    1.93 -val mk_conj = HOLogic.mk_conj;
    1.94 -val mk_imp  = HOLogic.mk_imp;
    1.95 -val Not = HOLogic.Not;
    1.96 -val false_const = HOLogic.false_const;
    1.97 -val true_const = HOLogic.true_const;
    1.98 -
    1.99 -
   1.100 -(* Index for new literals *)
   1.101 -val lit_id = ref 0;
   1.102 -
   1.103 +(* string -> Thm.thm *)
   1.104  fun thm_by_auto G =
   1.105 -    prove_goal cur_thy G (fn prems => [cut_facts_tac prems 1, Auto_tac]);
   1.106 -
   1.107 -(***************************************************************************)
   1.108 +	prove_goal (the_context ()) G (fn prems => [cut_facts_tac prems 1, Auto_tac]);
   1.109  
   1.110 -
   1.111 -val cnf_eq_id = thm_by_auto "(P :: bool) = P";
   1.112 -
   1.113 -val cnf_eq_sym = thm_by_auto "(P :: bool) = Q ==> Q = P";
   1.114 -
   1.115 -val cnf_not_true_false = thm_by_auto "~True = False";
   1.116 +(* Thm.thm *)
   1.117 +val clause2raw_notE      = thm_by_auto "[| P; ~P |] ==> False";
   1.118 +val clause2raw_not_disj  = thm_by_auto "[| ~P; ~Q |] ==> ~(P | Q)";
   1.119 +val clause2raw_not_not   = thm_by_auto "P ==> ~~P";
   1.120  
   1.121 -val cnf_not_false_true = thm_by_auto "~False = True";
   1.122 -
   1.123 -val cnf_imp2disj = thm_by_auto "(P --> Q) = (~P | Q)";
   1.124 -
   1.125 -val cnf_neg_conj = thm_by_auto "(~(P & Q)) = (~P | ~Q)";
   1.126 -
   1.127 -val cnf_neg_disj = thm_by_auto "(~(P | Q)) = (~P & ~Q)";
   1.128 -
   1.129 -val cnf_neg_imp = thm_by_auto "(~(P --> Q)) = (P & ~Q)";
   1.130 +val iff_refl             = thm_by_auto "(P::bool) = P";
   1.131 +val iff_trans            = thm_by_auto "[| (P::bool) = Q; Q = R |] ==> P = R";
   1.132 +val conj_cong            = thm_by_auto "[| P = P'; Q = Q' |] ==> (P & Q) = (P' & Q')";
   1.133 +val disj_cong            = thm_by_auto "[| P = P'; Q = Q' |] ==> (P | Q) = (P' | Q')";
   1.134  
   1.135 -val cnf_double_neg = thm_by_auto "(~~P) = P";
   1.136 - 
   1.137 -val cnf_disj_conj = thm_by_auto "((P & Q) | R) = ((P | R) & (Q | R))";
   1.138 -
   1.139 -val cnf_disj_imp = thm_by_auto "((P --> Q) | R) = (~P | (Q | R))";
   1.140 -
   1.141 -val cnf_disj_disj = thm_by_auto "((P | Q) | R) = (P | (Q | R))";
   1.142 -
   1.143 -val cnf_disj_false = thm_by_auto "(False | P) = P";
   1.144 -
   1.145 -val cnf_disj_true = thm_by_auto "(True | P) = True";
   1.146 -
   1.147 -val cnf_disj_not_false = thm_by_auto "(~False | P) = True";
   1.148 -
   1.149 -val cnf_disj_not_true = thm_by_auto "(~True | P) = P";
   1.150 -
   1.151 -val cnf_eq_trans = thm_by_auto "[| ( (P::bool) = Q) ; Q = R |] ==> (P = R)";
   1.152 +val make_nnf_imp         = thm_by_auto "[| (~P) = P'; Q = Q' |] ==> (P --> Q) = (P' | Q')";
   1.153 +val make_nnf_iff         = thm_by_auto "[| P = P'; (~P) = NP; Q = Q'; (~Q) = NQ |] ==> (P = Q) = ((P' | NQ) & (NP | Q'))";
   1.154 +val make_nnf_not_false   = thm_by_auto "(~False) = True";
   1.155 +val make_nnf_not_true    = thm_by_auto "(~True) = False";
   1.156 +val make_nnf_not_conj    = thm_by_auto "[| (~P) = P'; (~Q) = Q' |] ==> (~(P & Q)) = (P' | Q')";
   1.157 +val make_nnf_not_disj    = thm_by_auto "[| (~P) = P'; (~Q) = Q' |] ==> (~(P | Q)) = (P' & Q')";
   1.158 +val make_nnf_not_imp     = thm_by_auto "[| P = P'; (~Q) = Q' |] ==> (~(P --> Q)) = (P' & Q')";
   1.159 +val make_nnf_not_iff     = thm_by_auto "[| P = P'; (~P) = NP; Q = Q'; (~Q) = NQ |] ==> (~(P = Q)) = ((P' | Q') & (NP | NQ))";
   1.160 +val make_nnf_not_not     = thm_by_auto "P = P' ==> (~~P) = P'";
   1.161  
   1.162 -val cnf_comb2eq = thm_by_auto "[| P = Q ; R = T |] ==> (P & R) = (Q & T)";
   1.163 -
   1.164 -val cnf_disj_sym = thm_by_auto "(P | Q) = (Q | P)";
   1.165 -
   1.166 -val cnf_cong_disj = thm_by_auto "(P = Q) ==> (P | R) = (Q | R)";
   1.167 -
   1.168 -val icnf_elim_disj1 = thm_by_auto "Q = R  ==> (~P | Q) = (P --> R)";
   1.169 +val simp_TF_conj_True_l  = thm_by_auto "[| P = True; Q = Q' |] ==> (P & Q) = Q'";
   1.170 +val simp_TF_conj_True_r  = thm_by_auto "[| P = P'; Q = True |] ==> (P & Q) = P'";
   1.171 +val simp_TF_conj_False_l = thm_by_auto "P = False ==> (P & Q) = False";
   1.172 +val simp_TF_conj_False_r = thm_by_auto "Q = False ==> (P & Q) = False";
   1.173 +val simp_TF_disj_True_l  = thm_by_auto "P = True ==> (P | Q) = True";
   1.174 +val simp_TF_disj_True_r  = thm_by_auto "Q = True ==> (P | Q) = True";
   1.175 +val simp_TF_disj_False_l = thm_by_auto "[| P = False; Q = Q' |] ==> (P | Q) = Q'";
   1.176 +val simp_TF_disj_False_r = thm_by_auto "[| P = P'; Q = False |] ==> (P | Q) = P'";
   1.177  
   1.178 -val icnf_elim_disj2 = thm_by_auto "Q = R ==> (P | Q) = (~P --> R)";
   1.179 -
   1.180 -val icnf_neg_false1 = thm_by_auto "(~P) = (P --> False)";
   1.181 +val make_cnf_disj_conj_l = thm_by_auto "[| (P | R) = PR; (Q | R) = QR |] ==> ((P & Q) | R) = (PR & QR)";
   1.182 +val make_cnf_disj_conj_r = thm_by_auto "[| (P | Q) = PQ; (P | R) = PR |] ==> (P | (Q & R)) = (PQ & PR)";
   1.183  
   1.184 -val icnf_neg_false2 = thm_by_auto "P = (~P --> False)";
   1.185 +val make_cnfx_disj_ex_l = thm_by_auto "((EX (x::bool). P x) | Q) = (EX x. P x | Q)";
   1.186 +val make_cnfx_disj_ex_r = thm_by_auto "(P | (EX (x::bool). Q x)) = (EX x. P | Q x)";
   1.187 +val make_cnfx_newlit    = thm_by_auto "(P | Q) = (EX x. (P | x) & (Q | ~x))";
   1.188 +val make_cnfx_ex_cong   = thm_by_auto "(ALL (x::bool). P x = Q x) ==> (EX x. P x) = (EX x. Q x)";
   1.189  
   1.190 -val weakening_thm = thm_by_auto "[| P ; Q |] ==> Q";
   1.191 +val weakening_thm        = thm_by_auto "[| P; Q |] ==> Q";
   1.192  
   1.193 -val cnf_newlit = thm_by_auto 
   1.194 -    "((P & Q) | R) = (EX (x :: bool). (~x | P) & (~x | Q) & (x | ~P | ~ Q) & (x | R))";
   1.195 +val cnftac_eq_imp        = thm_by_auto "[| P = Q; P |] ==> Q";
   1.196  
   1.197 -val cnf_all_ex = thm_by_auto 
   1.198 -    "(ALL (x :: bool). (P x = Q x)) ==> (EX x. P x) = (EX x. Q x)";
   1.199 -
   1.200 -(* [| P ; ~P |] ==> False *)
   1.201 -val cnf_notE = read_instantiate [("R", "False")] (rotate_prems 1 notE);
   1.202 -
   1.203 -val cnf_dneg = thm_by_auto "P ==> ~~P";
   1.204 -
   1.205 -val cnf_neg_disjI = thm_by_auto "[| ~P ; ~Q |] ==> ~(P | Q)";
   1.206 -
   1.207 -val cnf_eq_imp = thm_by_auto "[|((P::bool) = Q) ; P |] ==> Q";
   1.208 -
   1.209 -(***************************************************************************)
   1.210 +(* Term.term -> bool *)
   1.211 +fun is_atom (Const ("False", _))                                           = false
   1.212 +  | is_atom (Const ("True", _))                                            = false
   1.213 +  | is_atom (Const ("op &", _) $ _ $ _)                                    = false
   1.214 +  | is_atom (Const ("op |", _) $ _ $ _)                                    = false
   1.215 +  | is_atom (Const ("op -->", _) $ _ $ _)                                  = false
   1.216 +  | is_atom (Const ("op =", Type ("fun", Type ("bool", []) :: _)) $ _ $ _) = false
   1.217 +  | is_atom (Const ("Not", _) $ _)                                         = false
   1.218 +  | is_atom _                                                              = true;
   1.219  
   1.220 -fun is_atm (Const("Trueprop",_) $ x) = is_atm x
   1.221 -  | is_atm (Const("==>",_) $ x $ y) = false
   1.222 -  | is_atm (Const("False",_)) = false
   1.223 -  | is_atm (Const("True", _)) = false
   1.224 -  | is_atm (Const("op &",_) $ x $ y) = false
   1.225 -  | is_atm (Const("op |",_) $ x $ y) = false
   1.226 -  | is_atm (Const("op -->",_) $ x $ y) = false
   1.227 -  | is_atm (Const("Not",_) $ x) = false
   1.228 -  | is_atm t = true
   1.229 +(* Term.term -> bool *)
   1.230 +fun is_literal (Const ("Not", _) $ x) = is_atom x
   1.231 +  | is_literal x                      = is_atom x;
   1.232  
   1.233 -
   1.234 -fun is_lit (Const("Trueprop",_) $ x) = is_lit x
   1.235 -  | is_lit (Const("Not", _) $ x) = is_atm x
   1.236 -  | is_lit t = is_atm t
   1.237 +(* Term.term -> bool *)
   1.238 +fun is_clause (Const ("op |", _) $ x $ y) = is_clause x andalso is_clause y
   1.239 +  | is_clause x                           = is_literal x;
   1.240  
   1.241 -fun is_clause (Const("Trueprop",_) $ x) = is_clause x
   1.242 -  | is_clause (Const("op |", _) $ x $ y) = 
   1.243 -          (is_clause x) andalso (is_clause y)
   1.244 -  | is_clause t = is_lit t
   1.245 +(* ------------------------------------------------------------------------- *)
   1.246 +(* clause_is_trivial: a clause is trivially true if it contains both an atom *)
   1.247 +(*      and the atom's negation                                              *)
   1.248 +(* ------------------------------------------------------------------------- *)
   1.249 +
   1.250 +(* Term.term -> bool *)
   1.251  
   1.252 -fun is_cnf (Const("Trueprop", _) $ x) = is_cnf x
   1.253 -  | is_cnf (Const("op &",_) $ x $ y) = (is_cnf x) andalso (is_cnf y)
   1.254 -  | is_cnf t = is_clause t
   1.255 -
   1.256 -
   1.257 -(* Checking for raw clauses *)
   1.258 -fun is_raw_clause (Const("Trueprop",_) $ x) = is_raw_clause x
   1.259 -  | is_raw_clause (Const("==>",_) $ x $ 
   1.260 -                   (Const("Trueprop",_) $ Const("False",_))) = is_lit x
   1.261 -  | is_raw_clause (Const("==>",_) $ x $ y) = 
   1.262 -        (is_lit x) andalso (is_raw_clause y)
   1.263 -  | is_raw_clause t = false
   1.264 -
   1.265 -
   1.266 +fun clause_is_trivial c =
   1.267 +	let
   1.268 +		(* Term.term -> Term.term list -> Term.term list *)
   1.269 +		fun collect_literals (Const ("op |", _) $ x $ y) ls = collect_literals x (collect_literals y ls)
   1.270 +		  | collect_literals x                           ls = x :: ls
   1.271 +		(* Term.term -> Term.term *)
   1.272 +		fun dual (Const ("Not", _) $ x) = x
   1.273 +		  | dual x                      = HOLogic.Not $ x
   1.274 +		(* Term.term list -> bool *)
   1.275 +		fun has_duals []      = false
   1.276 +		  | has_duals (x::xs) = (dual x) mem xs orelse has_duals xs
   1.277 +	in
   1.278 +		has_duals (collect_literals c [])
   1.279 +	end;
   1.280  
   1.281 -(* Translate a CNF clause into a raw clause *)
   1.282 -fun cnf2raw_thm c =
   1.283 -let val nc = c RS cnf_notE
   1.284 -in
   1.285 -rule_by_tactic (REPEAT_SOME (fn i => 
   1.286 -               rtac cnf_dneg i 
   1.287 -               ORELSE rtac cnf_neg_disjI i)) nc
   1.288 -handle THM _ => nc
   1.289 -end
   1.290 +(* ------------------------------------------------------------------------- *)
   1.291 +(* is_raw_clause: returns true iff the term is of the form                   *)
   1.292 +(*        x1 ==> ... ==> xn ==> False ,                                      *)
   1.293 +(*      with n >= 1, where each xi is a literal                              *)
   1.294 +(* ------------------------------------------------------------------------- *)
   1.295 +
   1.296 +(* Term.term -> bool *)
   1.297  
   1.298 -fun cnf2raw_thms nil = nil
   1.299 -  | cnf2raw_thms (c::l) =
   1.300 -    let val t = term_of (cprop_of c)
   1.301 -    in
   1.302 -       if (is_clause t) then (cnf2raw_thm c) :: cnf2raw_thms l
   1.303 -       else cnf2raw_thms l
   1.304 -    end
   1.305 +fun is_raw_clause (Const ("==>", _) $ x $ y) =
   1.306 +	is_literal x andalso
   1.307 +		(y = HOLogic.mk_Trueprop HOLogic.false_const orelse is_raw_clause y)
   1.308 +  | is_raw_clause _                          =
   1.309 +	false;
   1.310  
   1.311 +(* ------------------------------------------------------------------------- *)
   1.312 +(* clause2raw_thm: translates a clause into a raw clause, i.e.               *)
   1.313 +(*        x1 | ... | xn                                                      *)
   1.314 +(*      (where each xi is a literal) is translated to                        *)
   1.315 +(*        x1' ==> ... ==> xn' ==> False ,                                    *)
   1.316 +(*      where each xi' is the negation normal form of ~xi.                   *)
   1.317 +(* ------------------------------------------------------------------------- *)
   1.318  
   1.319 -(* Translating HOL formula (in CNF) to PropLogic formula. Returns also an
   1.320 -   association list, relating literals to their indices *)
   1.321 +(* Thm.thm -> Thm.thm *)
   1.322  
   1.323 -local
   1.324 -  (* maps atomic formulas to variable numbers *)
   1.325 -  val dictionary : ((Term.term * int) list) ref = ref nil;
   1.326 -  val var_count = ref 0;
   1.327 -  val pAnd = PropLogic.And;
   1.328 -  val pOr = PropLogic.Or;
   1.329 -  val pNot = PropLogic.Not;
   1.330 -  val pFalse = PropLogic.False;
   1.331 -  val pTrue = PropLogic.True;
   1.332 -  val pVar = PropLogic.BoolVar;
   1.333 -
   1.334 -  fun mk_clause (Const("Trueprop",_) $ x) = mk_clause x
   1.335 -    | mk_clause (Const("op |",_) $ x $ y) = pOr(mk_clause x, mk_clause y)
   1.336 -    | mk_clause (Const("Not", _) $ x) = pNot (mk_clause x)
   1.337 -    | mk_clause (Const("True",_)) = pTrue
   1.338 -    | mk_clause (Const("False",_)) = pFalse
   1.339 -    | mk_clause t =
   1.340 -      let
   1.341 -         val idx = AList.lookup op= (!dictionary) t
   1.342 -      in
   1.343 -         case idx of
   1.344 -            (SOME x) => pVar x
   1.345 -           | NONE =>
   1.346 -             let
   1.347 -                val new_var = inc var_count
   1.348 -             in
   1.349 -                dictionary := (t, new_var) :: (!dictionary);
   1.350 -                pVar new_var
   1.351 -             end
   1.352 -      end
   1.353 -
   1.354 -   fun mk_clauses nil = pTrue
   1.355 -     | mk_clauses (x::nil) = mk_clause x
   1.356 -     | mk_clauses (x::l) = pAnd(mk_clause x, mk_clauses l)
   1.357 +fun clause2raw_thm c =
   1.358 +let
   1.359 +	val thm1 = c RS clause2raw_notE  (* ~(x1 | ... | xn) ==> False *)
   1.360 +	(* eliminates negated disjunctions from the i-th premise, possibly *)
   1.361 +	(* adding new premises, then continues with the (i+1)-th premise   *)
   1.362 +	(* Thm.thm -> int -> Thm.thm *)
   1.363 +	fun not_disj_to_prem thm i =
   1.364 +		if i > nprems_of thm then
   1.365 +			thm
   1.366 +		else
   1.367 +			not_disj_to_prem (Seq.hd (REPEAT_DETERM (rtac clause2raw_not_disj i) thm)) (i+1)
   1.368 +	val thm2 = not_disj_to_prem thm1 1  (* ~x1 ==> ... ==> ~xn ==> False *)
   1.369 +	val thm3 = Seq.hd (TRYALL (rtac clause2raw_not_not) thm2)  (* x1' ==> ... ==> xn' ==> False *)
   1.370 +in
   1.371 +	thm3
   1.372 +end;
   1.373  
   1.374 -in
   1.375 -   fun cnf2prop thms =
   1.376 -   (
   1.377 -     var_count := 0;
   1.378 -     dictionary := nil;
   1.379 -     (mk_clauses (map (fn x => term_of (cprop_of x)) thms), !dictionary)
   1.380 -   )
   1.381 -end
   1.382 +(* ------------------------------------------------------------------------- *)
   1.383 +(* inst_thm: instantiates a theorem with a list of terms                     *)
   1.384 +(* ------------------------------------------------------------------------- *)
   1.385  
   1.386 -
   1.387 +(* Theory.theory -> Term.term list -> Thm.thm -> Thm.thm *)
   1.388  
   1.389 -(* Instantiate a theorem with a list of terms *)
   1.390 -fun inst_thm sign l thm = 
   1.391 -  instantiate' [] (map (fn x => SOME (cterm_of sign x)) l) thm
   1.392 -
   1.393 -(* Tactic to remove the first hypothesis of the first subgoal. *) 
   1.394 -fun weakening_tac i = (dtac weakening_thm i) THEN (atac (i+1));
   1.395 +fun inst_thm thy ts thm =
   1.396 +	instantiate' [] (map (SOME o cterm_of thy) ts) thm;
   1.397  
   1.398 -(* Tactic for removing the n first hypotheses of the first subgoal. *)
   1.399 -fun weakenings_tac 0 state = all_tac state
   1.400 -  | weakenings_tac n state = ((weakening_tac  1) THEN (weakenings_tac (n-1))) state
   1.401 -
   1.402 +(* ------------------------------------------------------------------------- *)
   1.403 +(*                         Naive CNF transformation                          *)
   1.404 +(* ------------------------------------------------------------------------- *)
   1.405  
   1.406 -(* 
   1.407 -  Transform a formula into a "head" negation normal form, that is, 
   1.408 -  the top level connective is not a negation, with the exception
   1.409 -  of negative literals. Returns the pair of the head normal term with
   1.410 -  the theorem corresponding to the transformation.
   1.411 -*)
   1.412 -fun head_nnf sign (Const("Not",_)  $ (Const("op &",_) $ x $ y)) =
   1.413 -    let val t = mk_disj(Not $ x, Not $ y)
   1.414 -        val neg_thm = inst_thm sign [x, y] cnf_neg_conj 
   1.415 -    in
   1.416 -        (t, neg_thm)
   1.417 -    end
   1.418 +(* ------------------------------------------------------------------------- *)
   1.419 +(* make_nnf_thm: produces a theorem of the form t = t', where t' is the      *)
   1.420 +(*      negation normal form (i.e. negation only occurs in front of atoms)   *)
   1.421 +(*      of t; implications ("-->") and equivalences ("=" on bool) are        *)
   1.422 +(*      eliminated (possibly causing an exponential blowup)                  *)
   1.423 +(* ------------------------------------------------------------------------- *)
   1.424 +
   1.425 +(* Theory.theory -> Term.term -> Thm.thm *)
   1.426  
   1.427 -  | head_nnf sign (Const("Not", _) $ (Const("op |",_) $ x $ y)) =
   1.428 -    let val t = mk_conj(Not $ x, Not $ y)
   1.429 -        val neg_thm =  inst_thm sign [x, y] cnf_neg_disj; 
   1.430 -    in
   1.431 -        (t, neg_thm)
   1.432 -    end
   1.433 -
   1.434 -  | head_nnf sign (Const("Not", _) $ (Const("op -->",_) $ x $ y)) = 
   1.435 -    let val t = mk_conj(x, Not $ y)
   1.436 -        val neg_thm = inst_thm sign [x, y] cnf_neg_imp
   1.437 -    in
   1.438 -        (t, neg_thm)
   1.439 -    end
   1.440 -
   1.441 -  | head_nnf sign (Const("Not",_) $ (Const("Not",_) $ x)) =
   1.442 -    (x, inst_thm sign [x] cnf_double_neg)
   1.443 -
   1.444 -  | head_nnf sign (Const("Not",_) $ Const("True",_)) = 
   1.445 -      (false_const, cnf_not_true_false)
   1.446 -
   1.447 -  | head_nnf sign (Const("Not",_) $ Const("False",_)) = 
   1.448 -      (true_const, cnf_not_false_true)  
   1.449 -
   1.450 -  | head_nnf sign t = 
   1.451 -    (t, inst_thm sign [t] cnf_eq_id)
   1.452 -
   1.453 -
   1.454 -(***************************************************************************)
   1.455 -(*                  Tactics for simple CNF transformation                  *)
   1.456 +fun make_nnf_thm thy (Const ("op &", _) $ x $ y) =
   1.457 +	let
   1.458 +		val thm1 = make_nnf_thm thy x
   1.459 +		val thm2 = make_nnf_thm thy y
   1.460 +	in
   1.461 +		conj_cong OF [thm1, thm2]
   1.462 +	end
   1.463 +  | make_nnf_thm thy (Const ("op |", _) $ x $ y) =
   1.464 +	let
   1.465 +		val thm1 = make_nnf_thm thy x
   1.466 +		val thm2 = make_nnf_thm thy y
   1.467 +	in
   1.468 +		disj_cong OF [thm1, thm2]
   1.469 +	end
   1.470 +  | make_nnf_thm thy (Const ("op -->", _) $ x $ y) =
   1.471 +	let
   1.472 +		val thm1 = make_nnf_thm thy (HOLogic.Not $ x)
   1.473 +		val thm2 = make_nnf_thm thy y
   1.474 +	in
   1.475 +		make_nnf_imp OF [thm1, thm2]
   1.476 +	end
   1.477 +  | make_nnf_thm thy (Const ("op =", Type ("fun", Type ("bool", []) :: _)) $ x $ y) =
   1.478 +	let
   1.479 +		val thm1 = make_nnf_thm thy x
   1.480 +		val thm2 = make_nnf_thm thy (HOLogic.Not $ x)
   1.481 +		val thm3 = make_nnf_thm thy y
   1.482 +		val thm4 = make_nnf_thm thy (HOLogic.Not $ y)
   1.483 +	in
   1.484 +		make_nnf_iff OF [thm1, thm2, thm3, thm4]
   1.485 +	end
   1.486 +  | make_nnf_thm thy (Const ("Not", _) $ Const ("False", _)) =
   1.487 +	make_nnf_not_false
   1.488 +  | make_nnf_thm thy (Const ("Not", _) $ Const ("True", _)) =
   1.489 +	make_nnf_not_true
   1.490 +  | make_nnf_thm thy (Const ("Not", _) $ (Const ("op &", _) $ x $ y)) =
   1.491 +	let
   1.492 +		val thm1 = make_nnf_thm thy (HOLogic.Not $ x)
   1.493 +		val thm2 = make_nnf_thm thy (HOLogic.Not $ y)
   1.494 +	in
   1.495 +		make_nnf_not_conj OF [thm1, thm2]
   1.496 +	end
   1.497 +  | make_nnf_thm thy (Const ("Not", _) $ (Const ("op |", _) $ x $ y)) =
   1.498 +	let
   1.499 +		val thm1 = make_nnf_thm thy (HOLogic.Not $ x)
   1.500 +		val thm2 = make_nnf_thm thy (HOLogic.Not $ y)
   1.501 +	in
   1.502 +		make_nnf_not_disj OF [thm1, thm2]
   1.503 +	end
   1.504 +  | make_nnf_thm thy (Const ("Not", _) $ (Const ("op -->", _) $ x $ y)) =
   1.505 +	let
   1.506 +		val thm1 = make_nnf_thm thy x
   1.507 +		val thm2 = make_nnf_thm thy (HOLogic.Not $ y)
   1.508 +	in
   1.509 +		make_nnf_not_imp OF [thm1, thm2]
   1.510 +	end
   1.511 +  | make_nnf_thm thy (Const ("Not", _) $ (Const ("op =", Type ("fun", Type ("bool", []) :: _)) $ x $ y)) =
   1.512 +	let
   1.513 +		val thm1 = make_nnf_thm thy x
   1.514 +		val thm2 = make_nnf_thm thy (HOLogic.Not $ x)
   1.515 +		val thm3 = make_nnf_thm thy y
   1.516 +		val thm4 = make_nnf_thm thy (HOLogic.Not $ y)
   1.517 +	in
   1.518 +		make_nnf_not_iff OF [thm1, thm2, thm3, thm4]
   1.519 +	end
   1.520 +  | make_nnf_thm thy (Const ("Not", _) $ (Const ("Not", _) $ x)) =
   1.521 +	let
   1.522 +		val thm1 = make_nnf_thm thy x
   1.523 +	in
   1.524 +		make_nnf_not_not OF [thm1]
   1.525 +	end
   1.526 +  | make_nnf_thm thy t =
   1.527 +	inst_thm thy [t] iff_refl;
   1.528  
   1.529 -(* A naive procedure for CNF transformation:
   1.530 -   Given any t, produce a theorem t = t', where t' is in
   1.531 -   conjunction normal form 
   1.532 -*)
   1.533 -fun mk_cnf_thm sign (Const("Trueprop",_) $ x) = mk_cnf_thm sign x
   1.534 -  | mk_cnf_thm sign (t as (Const(_,_))) = inst_thm sign [t] cnf_eq_id
   1.535 -  | mk_cnf_thm sign (t as (Free(_,_))) =  inst_thm sign [t] cnf_eq_id
   1.536 - 
   1.537 -  | mk_cnf_thm sign (Const("op -->", _) $ x $ y) =
   1.538 -       let val thm1 = inst_thm sign [x, y] cnf_imp2disj;
   1.539 -           val thm2 = mk_cnf_thm sign (mk_disj(Not $ x, y));
   1.540 -       in
   1.541 -           cnf_eq_trans OF [thm1, thm2]
   1.542 -       end
   1.543 -
   1.544 -  | mk_cnf_thm sign (Const("op &", _) $ x $ y) = 
   1.545 -       let val cnf1 = mk_cnf_thm sign x;
   1.546 -           val cnf2 = mk_cnf_thm sign y;
   1.547 -       in
   1.548 -           cnf_comb2eq OF [cnf1, cnf2]
   1.549 -       end
   1.550 -
   1.551 -  | mk_cnf_thm sign (Const("Not",_) $ Const("True",_)) = 
   1.552 -        cnf_not_true_false
   1.553 +(* ------------------------------------------------------------------------- *)
   1.554 +(* simp_True_False_thm: produces a theorem t = t', where t' is equivalent to *)
   1.555 +(*      t, but simplified wrt. the following theorems:                       *)
   1.556 +(*        (True & x) = x                                                     *)
   1.557 +(*        (x & True) = x                                                     *)
   1.558 +(*        (False & x) = False                                                *)
   1.559 +(*        (x & False) = False                                                *)
   1.560 +(*        (True | x) = True                                                  *)
   1.561 +(*        (x | True) = True                                                  *)
   1.562 +(*        (False | x) = x                                                    *)
   1.563 +(*        (x | False) = x                                                    *)
   1.564 +(*      No simplification is performed below connectives other than & and |. *)
   1.565 +(*      Optimization: The right-hand side of a conjunction (disjunction) is  *)
   1.566 +(*      simplified only if the left-hand side does not simplify to False     *)
   1.567 +(*      (True, respectively).                                                *)
   1.568 +(* ------------------------------------------------------------------------- *)
   1.569  
   1.570 -  | mk_cnf_thm sign (Const("Not",_) $ Const("False",_)) = 
   1.571 -        cnf_not_false_true
   1.572 -
   1.573 -  | mk_cnf_thm sign (t as (Const("Not", _) $ x)) =
   1.574 -      ( 
   1.575 -       if (is_atm x) then inst_thm sign [t] cnf_eq_id
   1.576 -       else
   1.577 -         let val (t1, hn_thm) = head_nnf sign t
   1.578 -             val cnf_thm = mk_cnf_thm sign t1
   1.579 -         in
   1.580 -             cnf_eq_trans OF [hn_thm, cnf_thm]
   1.581 -         end
   1.582 -       ) 
   1.583 -
   1.584 -  | mk_cnf_thm sign (Const("op |",_) $ (Const("op &", _) $ p $ q) $ r) =
   1.585 -       let val thm1 = inst_thm sign [p, q, r] cnf_disj_conj;
   1.586 -           val thm2 = mk_cnf_thm sign (mk_conj(mk_disj(p, r), mk_disj(q,r)));
   1.587 -       in
   1.588 -          cnf_eq_trans OF [thm1, thm2]
   1.589 -       end
   1.590 -
   1.591 -  | mk_cnf_thm sign (Const("op |",_) $ (Const("op |", _) $ p $ q) $ r) =
   1.592 -       let val thm1 = inst_thm sign [p,q,r] cnf_disj_disj;
   1.593 -           val thm2 = mk_cnf_thm sign (mk_disj(p, mk_disj(q,r)));
   1.594 -       in
   1.595 -          cnf_eq_trans OF [thm1, thm2]
   1.596 -       end
   1.597 +(* Theory.theory -> Term.term -> Thm.thm *)
   1.598  
   1.599 -  | mk_cnf_thm sign (Const("op |",_) $ (Const("op -->", _) $ p $ q) $ r) =                       
   1.600 -       let val thm1 = inst_thm sign [p,q,r] cnf_disj_imp;
   1.601 -           val thm2 = mk_cnf_thm sign (mk_disj(Not $ p, mk_disj(q, r)));
   1.602 -       in
   1.603 -           cnf_eq_trans OF [thm1, thm2]
   1.604 -       end
   1.605 -
   1.606 -  | mk_cnf_thm sign (Const("op |",_) $ Const("False",_) $ p) =
   1.607 -       let val thm1 = inst_thm sign [p] cnf_disj_false;
   1.608 -           val thm2 = mk_cnf_thm sign p
   1.609 -       in
   1.610 -           cnf_eq_trans OF [thm1, thm2]
   1.611 -       end
   1.612 -
   1.613 -  | mk_cnf_thm sign (Const("op |",_) $ Const("True",_) $ p) =
   1.614 -       inst_thm sign [p] cnf_disj_true
   1.615 -
   1.616 -  | mk_cnf_thm sign (Const("op |",_) $ (Const("Not",_) $ Const("True",_)) $ p) =
   1.617 -       let val thm1 = inst_thm sign [p] cnf_disj_not_true;
   1.618 -           val thm2 = mk_cnf_thm sign p
   1.619 -       in
   1.620 -           cnf_eq_trans OF [thm1, thm2]
   1.621 -       end
   1.622 -
   1.623 -  | mk_cnf_thm sign (Const("op |",_) $ (Const("Not",_) $ Const("False",_)) $ p) =
   1.624 -       inst_thm sign [p] cnf_disj_not_false
   1.625 +fun simp_True_False_thm thy (Const ("op &", _) $ x $ y) =
   1.626 +	let
   1.627 +		val thm1 = simp_True_False_thm thy x
   1.628 +		val x'   = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) thm1
   1.629 +	in
   1.630 +		if x' = HOLogic.false_const then
   1.631 +			simp_TF_conj_False_l OF [thm1]  (* (x & y) = False *)
   1.632 +		else
   1.633 +			let
   1.634 +				val thm2 = simp_True_False_thm thy y
   1.635 +				val y'   = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) thm2
   1.636 +			in
   1.637 +				if x' = HOLogic.true_const then
   1.638 +					simp_TF_conj_True_l OF [thm1, thm2]  (* (x & y) = y' *)
   1.639 +				else if y' = HOLogic.false_const then
   1.640 +					simp_TF_conj_False_r OF [thm2]  (* (x & y) = False *)
   1.641 +				else if y' = HOLogic.true_const then
   1.642 +					simp_TF_conj_True_r OF [thm1, thm2]  (* (x & y) = x' *)
   1.643 +				else
   1.644 +					conj_cong OF [thm1, thm2]  (* (x & y) = (x' & y') *)
   1.645 +			end
   1.646 +	end
   1.647 +  | simp_True_False_thm thy (Const ("op |", _) $ x $ y) =
   1.648 +	let
   1.649 +		val thm1 = simp_True_False_thm thy x
   1.650 +		val x'   = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) thm1
   1.651 +	in
   1.652 +		if x' = HOLogic.true_const then
   1.653 +			simp_TF_disj_True_l OF [thm1]  (* (x | y) = True *)
   1.654 +		else
   1.655 +			let
   1.656 +				val thm2 = simp_True_False_thm thy y
   1.657 +				val y'   = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) thm2
   1.658 +			in
   1.659 +				if x' = HOLogic.false_const then
   1.660 +					simp_TF_disj_False_l OF [thm1, thm2]  (* (x | y) = y' *)
   1.661 +				else if y' = HOLogic.true_const then
   1.662 +					simp_TF_disj_True_r OF [thm2]  (* (x | y) = True *)
   1.663 +				else if y' = HOLogic.false_const then
   1.664 +					simp_TF_disj_False_r OF [thm1, thm2]  (* (x | y) = x' *)
   1.665 +				else
   1.666 +					disj_cong OF [thm1, thm2]  (* (x | y) = (x' | y') *)
   1.667 +			end
   1.668 +	end
   1.669 +  | simp_True_False_thm thy t =
   1.670 +	inst_thm thy [t] iff_refl;  (* t = t *)
   1.671  
   1.672 -  | mk_cnf_thm sign (t as (Const("op |",_) $ p $ q)) = 
   1.673 -       if (is_lit p) then
   1.674 -          (
   1.675 -            if (is_clause t) then inst_thm sign [t] cnf_eq_id
   1.676 -            else 
   1.677 -             let val thm1 = inst_thm sign [p, q] cnf_disj_sym;
   1.678 -                 val thm2 = mk_cnf_thm sign (mk_disj(q, p))
   1.679 -             in
   1.680 -                cnf_eq_trans OF [thm1, thm2]
   1.681 -             end
   1.682 -          )
   1.683 -       else 
   1.684 -            let val (u, thm1) = head_nnf sign p;
   1.685 -                val thm2 = inst_thm sign [p,u,q] cnf_cong_disj;
   1.686 -                val thm3 = mk_cnf_thm sign (mk_disj(u, q))
   1.687 -            in
   1.688 -                cnf_eq_trans OF [(thm1 RS thm2), thm3]
   1.689 -            end
   1.690 -
   1.691 - | mk_cnf_thm sign t = inst_thm sign [t] cnf_eq_id
   1.692 -    (* error ("I don't know how to handle the formula " ^ 
   1.693 -                          (Sign.string_of_term sign t))
   1.694 -    *)
   1.695 +(* ------------------------------------------------------------------------- *)
   1.696 +(* make_cnf_thm: given any HOL term 't', produces a theorem t = t', where t' *)
   1.697 +(*      is in conjunction normal form.  May cause an exponential blowup      *)
   1.698 +(*      in the length of the term.                                           *)
   1.699 +(* ------------------------------------------------------------------------- *)
   1.700  
   1.701 -fun term_of_thm c = term_of (cprop_of c)
   1.702 -
   1.703 -
   1.704 -(* Transform a given list of theorems (thms) into CNF *)
   1.705 -
   1.706 -fun mk_cnf_thms sg nil = nil
   1.707 -  | mk_cnf_thms sg (x::l) = 
   1.708 -    let val t = term_of_thm x
   1.709 -    in
   1.710 -      if (is_clause t) then x :: mk_cnf_thms sg l
   1.711 -      else 
   1.712 -       let val thm1 = mk_cnf_thm sg t
   1.713 -           val thm2 = cnf_eq_imp OF [thm1, x]
   1.714 -       in 
   1.715 -           thm2 :: mk_cnf_thms sg l
   1.716 -       end
   1.717 -    end
   1.718 -
   1.719 -
   1.720 -(* Count the number of hypotheses in a formula *)
   1.721 -fun num_of_hyps (Const("Trueprop", _) $ x) = num_of_hyps x
   1.722 -  | num_of_hyps (Const("==>",_) $ x $ y) = 1 + (num_of_hyps y)
   1.723 -  | num_of_hyps t = 0
   1.724 +(* Theory.theory -> Term.term -> Thm.thm *)
   1.725  
   1.726 -(* Tactic for converting to CNF (in primitive form): 
   1.727 -   it takes the first subgoal of the proof state, transform all its
   1.728 -   hypotheses into CNF (in primivite form) and remove the original 
   1.729 -   hypotheses.
   1.730 -*)
   1.731 -fun cnf_thin_tac state =
   1.732 -let val sg = Thm.sign_of_thm state
   1.733 +fun make_cnf_thm thy t =
   1.734 +let
   1.735 +	(* Term.term -> Thm.thm *)
   1.736 +	fun make_cnf_thm_from_nnf (Const ("op &", _) $ x $ y) =
   1.737 +		let
   1.738 +			val thm1 = make_cnf_thm_from_nnf x
   1.739 +			val thm2 = make_cnf_thm_from_nnf y
   1.740 +		in
   1.741 +			conj_cong OF [thm1, thm2]
   1.742 +		end
   1.743 +	  | make_cnf_thm_from_nnf (Const ("op |", _) $ x $ y) =
   1.744 +		let
   1.745 +			(* produces a theorem "(x' | y') = t'", where x', y', and t' are in CNF *)
   1.746 +			fun make_cnf_disj_thm (Const ("op &", _) $ x1 $ x2) y' =
   1.747 +				let
   1.748 +					val thm1 = make_cnf_disj_thm x1 y'
   1.749 +					val thm2 = make_cnf_disj_thm x2 y'
   1.750 +				in
   1.751 +					make_cnf_disj_conj_l OF [thm1, thm2]  (* ((x1 & x2) | y') = ((x1 | y')' & (x2 | y')') *)
   1.752 +				end
   1.753 +			  | make_cnf_disj_thm x' (Const ("op &", _) $ y1 $ y2) =
   1.754 +				let
   1.755 +					val thm1 = make_cnf_disj_thm x' y1
   1.756 +					val thm2 = make_cnf_disj_thm x' y2
   1.757 +				in
   1.758 +					make_cnf_disj_conj_r OF [thm1, thm2]  (* (x' | (y1 & y2)) = ((x' | y1)' & (x' | y2)') *)
   1.759 +				end
   1.760 +			  | make_cnf_disj_thm x' y' =
   1.761 +				inst_thm thy [HOLogic.mk_disj (x', y')] iff_refl  (* (x' | y') = (x' | y') *)
   1.762 +			val thm1     = make_cnf_thm_from_nnf x
   1.763 +			val thm2     = make_cnf_thm_from_nnf y
   1.764 +			val x'       = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) thm1
   1.765 +			val y'       = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) thm2
   1.766 +			val disj_thm = disj_cong OF [thm1, thm2]  (* (x | y) = (x' | y') *)
   1.767 +		in
   1.768 +			iff_trans OF [disj_thm, make_cnf_disj_thm x' y']
   1.769 +		end
   1.770 +	  | make_cnf_thm_from_nnf t =
   1.771 +		inst_thm thy [t] iff_refl
   1.772 +	(* convert 't' to NNF first *)
   1.773 +	val nnf_thm  = make_nnf_thm thy t
   1.774 +	val nnf      = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) nnf_thm
   1.775 +	(* then simplify wrt. True/False (this should preserve NNF) *)
   1.776 +	val simp_thm = simp_True_False_thm thy nnf
   1.777 +	val simp     = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) simp_thm
   1.778 +	(* finally, convert to CNF (this should preserve the simplification) *)
   1.779 +	val cnf_thm  = make_cnf_thm_from_nnf simp
   1.780  in
   1.781 -case (prems_of state) of 
   1.782 -  [] => Seq.empty
   1.783 -| (subgoal::_) => 
   1.784 -  let 
   1.785 -    val n = num_of_hyps (strip_all_body subgoal);
   1.786 -    val tac1 = METAHYPS (fn l => cut_facts_tac (mk_cnf_thms sg l) 1) 1
   1.787 -  in
   1.788 -    (tac1 THEN weakenings_tac n THEN (REPEAT (etac conjE 1)) ) state
   1.789 -  end
   1.790 -end
   1.791 -
   1.792 -(* Tactic for converting to CNF (in primitive form), keeping 
   1.793 -   the original hypotheses. *)
   1.794 -
   1.795 -fun cnf_tac state =
   1.796 -let val sg = Thm.sign_of_thm state
   1.797 -in
   1.798 -case (prems_of state) of 
   1.799 -  [] => Seq.empty
   1.800 -| (subgoal::_) => 
   1.801 -   METAHYPS (fn l => cut_facts_tac (mk_cnf_thms sg l) 1 
   1.802 -                    THEN (REPEAT (etac conjE 1)) ) 1 state
   1.803 -end
   1.804 -
   1.805 -
   1.806 -(***************************************************************************)
   1.807 -(*            CNF transformation by introducing new literals               *)
   1.808 +	iff_trans OF [iff_trans OF [nnf_thm, simp_thm], cnf_thm]
   1.809 +end;
   1.810  
   1.811 -(*** IMPORTANT: 
   1.812 -  This transformation uses variables of the form "lit_i", where i is a natural
   1.813 -  number. For the transformation to work, these variables must not already
   1.814 -  occur freely in the formula being transformed.
   1.815 -***)
   1.816 -
   1.817 -fun ext_conj x p q r =
   1.818 -   mk_conj(
   1.819 -    mk_disj(Not $ x, p),
   1.820 -    mk_conj(
   1.821 -      mk_disj(Not $ x, q),
   1.822 -      mk_conj(
   1.823 -        mk_disj(x, mk_disj(Not $ p, Not $ q)),
   1.824 -        mk_disj(x, r)
   1.825 -      )
   1.826 -    )
   1.827 -   )
   1.828 -
   1.829 +(* ------------------------------------------------------------------------- *)
   1.830 +(*            CNF transformation by introducing new literals                 *)
   1.831 +(* ------------------------------------------------------------------------- *)
   1.832  
   1.833 -(* Transform to CNF in primitive forms, possibly introduce extra variables *)
   1.834 -fun mk_cnfx_thm sign (Const("Trueprop",_) $ x) = mk_cnfx_thm sign x 
   1.835 -  | mk_cnfx_thm sign (t as (Const(_,_)))  = inst_thm sign [t] cnf_eq_id
   1.836 -  | mk_cnfx_thm sign (t as (Free(_,_)))  = inst_thm sign [t] cnf_eq_id
   1.837 -  | mk_cnfx_thm sign (Const("op -->", _) $ x $ y)  =
   1.838 -       let val thm1 = inst_thm sign [x, y] cnf_imp2disj;
   1.839 -           val thm2 = mk_cnfx_thm sign (mk_disj(Not $ x, y)) 
   1.840 -       in
   1.841 -           cnf_eq_trans OF [thm1, thm2]
   1.842 -       end
   1.843 +(* ------------------------------------------------------------------------- *)
   1.844 +(* make_cnfx_thm: given any HOL term 't', produces a theorem t = t', where   *)
   1.845 +(*      t' is almost in conjunction normal form, except that conjunctions    *)
   1.846 +(*      and existential quantifiers may be nested.  (Use e.g. 'REPEAT_DETERM *)
   1.847 +(*      (etac exE i ORELSE etac conjE i)' afterwards to normalize.)  May     *)
   1.848 +(*      introduce new (existentially bound) literals.  Note: the current     *)
   1.849 +(*      implementation calls 'make_nnf_thm', causing an exponential blowup   *)
   1.850 +(*      in the case of nested equivalences.                                  *)
   1.851 +(* ------------------------------------------------------------------------- *)
   1.852  
   1.853 -  | mk_cnfx_thm sign (Const("op &", _) $ x $ y)  = 
   1.854 -       let val cnf1 = mk_cnfx_thm sign x
   1.855 -           val cnf2 = mk_cnfx_thm sign y
   1.856 -       in
   1.857 -           cnf_comb2eq OF [cnf1, cnf2]
   1.858 -       end
   1.859 -
   1.860 -  | mk_cnfx_thm sign (Const("Not",_) $ Const("True",_)) = 
   1.861 -        cnf_not_true_false
   1.862 +(* Theory.theory -> Term.term -> Thm.thm *)
   1.863  
   1.864 -  | mk_cnfx_thm sign (Const("Not",_) $ Const("False",_))  = 
   1.865 -        cnf_not_false_true
   1.866 -
   1.867 -  | mk_cnfx_thm sign (t as (Const("Not", _) $ x))  =
   1.868 -      ( 
   1.869 -       if (is_atm x) then inst_thm sign [t] cnf_eq_id
   1.870 -       else
   1.871 -         let val (t1, hn_thm) = head_nnf sign t
   1.872 -             val cnf_thm = mk_cnfx_thm sign t1 
   1.873 -         in
   1.874 -             cnf_eq_trans OF [hn_thm, cnf_thm]
   1.875 -         end
   1.876 -       ) 
   1.877 -
   1.878 -  | mk_cnfx_thm sign (Const("op |",_) $ (Const("op &", _) $ p $ q) $ r)  =
   1.879 -      if (is_lit r) then
   1.880 -        let val thm1 = inst_thm sign [p, q, r] cnf_disj_conj
   1.881 -            val thm2 = mk_cnfx_thm sign (mk_conj(mk_disj(p, r), mk_disj(q,r)))
   1.882 -        in
   1.883 -           cnf_eq_trans OF [thm1, thm2]
   1.884 -        end
   1.885 -      else cnfx_newlit sign p q r 
   1.886 -
   1.887 -  | mk_cnfx_thm sign (Const("op |",_) $ (Const("op |", _) $ p $ q) $ r)  =
   1.888 -       let val thm1 = inst_thm sign [p,q,r] cnf_disj_disj
   1.889 -           val thm2 = mk_cnfx_thm sign (mk_disj(p, mk_disj(q,r))) 
   1.890 -       in
   1.891 -          cnf_eq_trans OF [thm1, thm2]
   1.892 -       end
   1.893 -
   1.894 -  | mk_cnfx_thm sign (Const("op |",_) $ (Const("op -->", _) $ p $ q) $ r) =                       
   1.895 -       let val thm1 = inst_thm sign [p,q,r] cnf_disj_imp
   1.896 -           val thm2 = mk_cnfx_thm sign (mk_disj(Not $ p, mk_disj(q, r))) 
   1.897 -       in
   1.898 -           cnf_eq_trans OF [thm1, thm2]
   1.899 -       end
   1.900 -
   1.901 -  | mk_cnfx_thm sign (Const("op |",_) $ Const("False",_) $ p)  =
   1.902 -       let val thm1 = inst_thm sign [p] cnf_disj_false;
   1.903 -           val thm2 = mk_cnfx_thm sign p 
   1.904 -       in
   1.905 -           cnf_eq_trans OF [thm1, thm2]
   1.906 -       end
   1.907 -
   1.908 -  | mk_cnfx_thm sign (Const("op |",_) $ Const("True",_) $ p)  =
   1.909 -       inst_thm sign [p] cnf_disj_true
   1.910 -
   1.911 -  | mk_cnfx_thm sign (Const("op |",_) $ (Const("Not",_) $ Const("True",_)) $ p)  =
   1.912 -       let val thm1 = inst_thm sign [p] cnf_disj_not_true;
   1.913 -           val thm2 = mk_cnfx_thm sign p 
   1.914 -       in
   1.915 -           cnf_eq_trans OF [thm1, thm2]
   1.916 -       end
   1.917 -
   1.918 -  | mk_cnfx_thm sign (Const("op |",_) $ (Const("Not",_) $ Const("False",_)) $ p)  =
   1.919 -       inst_thm sign [p] cnf_disj_not_false
   1.920 -
   1.921 -  | mk_cnfx_thm sign (t as (Const("op |",_) $ p $ q))  = 
   1.922 -       if (is_lit p) then
   1.923 -          (
   1.924 -            if (is_clause t) then inst_thm sign [t] cnf_eq_id
   1.925 -            else 
   1.926 -             let val thm1 = inst_thm sign [p, q] cnf_disj_sym
   1.927 -                 val thm2 = mk_cnfx_thm sign (mk_disj(q, p)) 
   1.928 -             in
   1.929 -                cnf_eq_trans OF [thm1, thm2]
   1.930 -             end
   1.931 -          )
   1.932 -       else 
   1.933 -            let val (u, thm1) = head_nnf sign p
   1.934 -                val thm2 = inst_thm sign [p,u,q] cnf_cong_disj
   1.935 -                val thm3 = mk_cnfx_thm sign (mk_disj(u, q)) 
   1.936 -            in
   1.937 -                cnf_eq_trans OF [(thm1 RS thm2), thm3]
   1.938 -            end
   1.939 +fun make_cnfx_thm thy t =
   1.940 +let
   1.941 +	val var_id = ref 0  (* properly initialized below *)
   1.942 +	(* unit -> Term.term *)
   1.943 +	fun new_free () =
   1.944 +		Free ("cnfx_" ^ string_of_int (inc var_id), HOLogic.boolT)
   1.945 +	(* Term.term -> Thm.thm *)
   1.946 +	fun make_cnfx_thm_from_nnf (Const ("op &", _) $ x $ y) =
   1.947 +		let
   1.948 +			val thm1 = make_cnfx_thm_from_nnf x
   1.949 +			val thm2 = make_cnfx_thm_from_nnf y
   1.950 +		in
   1.951 +			conj_cong OF [thm1, thm2]
   1.952 +		end
   1.953 +	  | make_cnfx_thm_from_nnf (Const ("op |", _) $ x $ y) =
   1.954 +		if is_clause x andalso is_clause y then
   1.955 +			inst_thm thy [HOLogic.mk_disj (x, y)] iff_refl
   1.956 +		else if is_literal y orelse is_literal x then let
   1.957 +			(* produces a theorem "(x' | y') = t'", where x', y', and t' are *)
   1.958 +			(* almost in CNF, and x' or y' is a literal                      *)
   1.959 +			fun make_cnfx_disj_thm (Const ("op &", _) $ x1 $ x2) y' =
   1.960 +				let
   1.961 +					val thm1 = make_cnfx_disj_thm x1 y'
   1.962 +					val thm2 = make_cnfx_disj_thm x2 y'
   1.963 +				in
   1.964 +					make_cnf_disj_conj_l OF [thm1, thm2]  (* ((x1 & x2) | y') = ((x1 | y')' & (x2 | y')') *)
   1.965 +				end
   1.966 +			  | make_cnfx_disj_thm x' (Const ("op &", _) $ y1 $ y2) =
   1.967 +				let
   1.968 +					val thm1 = make_cnfx_disj_thm x' y1
   1.969 +					val thm2 = make_cnfx_disj_thm x' y2
   1.970 +				in
   1.971 +					make_cnf_disj_conj_r OF [thm1, thm2]  (* (x' | (y1 & y2)) = ((x' | y1)' & (x' | y2)') *)
   1.972 +				end
   1.973 +			  | make_cnfx_disj_thm (Const ("Ex", _) $ x') y' =
   1.974 +				let
   1.975 +					val thm1 = inst_thm thy [x', y'] make_cnfx_disj_ex_l   (* ((Ex x') | y') = (Ex (x' | y')) *)
   1.976 +					val var  = new_free ()
   1.977 +					val thm2 = make_cnfx_disj_thm (betapply (x', var)) y'  (* (x' | y') = body' *)
   1.978 +					val thm3 = forall_intr (cterm_of thy var) thm2         (* !!v. (x' | y') = body' *)
   1.979 +					val thm4 = strip_shyps (thm3 COMP allI)                (* ALL v. (x' | y') = body' *)
   1.980 +					val thm5 = strip_shyps (thm4 RS make_cnfx_ex_cong)     (* (EX v. (x' | y')) = (EX v. body') *)
   1.981 +				in
   1.982 +					iff_trans OF [thm1, thm5]  (* ((Ex x') | y') = (Ex v. body') *)
   1.983 +				end
   1.984 +			  | make_cnfx_disj_thm x' (Const ("Ex", _) $ y') =
   1.985 +				let
   1.986 +					val thm1 = inst_thm thy [x', y'] make_cnfx_disj_ex_r   (* (x' | (Ex y')) = (Ex (x' | y')) *)
   1.987 +					val var  = new_free ()
   1.988 +					val thm2 = make_cnfx_disj_thm x' (betapply (y', var))  (* (x' | y') = body' *)
   1.989 +					val thm3 = forall_intr (cterm_of thy var) thm2         (* !!v. (x' | y') = body' *)
   1.990 +					val thm4 = strip_shyps (thm3 COMP allI)                (* ALL v. (x' | y') = body' *)
   1.991 +					val thm5 = strip_shyps (thm4 RS make_cnfx_ex_cong)     (* (EX v. (x' | y')) = (EX v. body') *)
   1.992 +				in
   1.993 +					iff_trans OF [thm1, thm5]  (* (x' | (Ex y')) = (EX v. body') *)
   1.994 +				end
   1.995 +			  | make_cnfx_disj_thm x' y' =
   1.996 +				inst_thm thy [HOLogic.mk_disj (x', y')] iff_refl  (* (x' | y') = (x' | y') *)
   1.997 +			val thm1     = make_cnfx_thm_from_nnf x
   1.998 +			val thm2     = make_cnfx_thm_from_nnf y
   1.999 +			val x'       = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) thm1
  1.1000 +			val y'       = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) thm2
  1.1001 +			val disj_thm = disj_cong OF [thm1, thm2]  (* (x | y) = (x' | y') *)
  1.1002 +		in
  1.1003 +			iff_trans OF [disj_thm, make_cnfx_disj_thm x' y']
  1.1004 +		end else let  (* neither 'x' nor 'y' is a literal: introduce a fresh variable *)
  1.1005 +			val thm1 = inst_thm thy [x, y] make_cnfx_newlit     (* (x | y) = EX v. (x | v) & (y | ~v) *)
  1.1006 +			val var  = new_free ()
  1.1007 +			val body = HOLogic.mk_conj (HOLogic.mk_disj (x, var), HOLogic.mk_disj (y, HOLogic.Not $ var))
  1.1008 +			val thm2 = make_cnfx_thm_from_nnf body              (* (x | v) & (y | ~v) = body' *)
  1.1009 +			val thm3 = forall_intr (cterm_of thy var) thm2      (* !!v. (x | v) & (y | ~v) = body' *)
  1.1010 +			val thm4 = strip_shyps (thm3 COMP allI)             (* ALL v. (x | v) & (y | ~v) = body' *)
  1.1011 +			val thm5 = strip_shyps (thm4 RS make_cnfx_ex_cong)  (* (EX v. (x | v) & (y | ~v)) = (EX v. body') *)
  1.1012 +		in
  1.1013 +			iff_trans OF [thm1, thm5]
  1.1014 +		end
  1.1015 +	  | make_cnfx_thm_from_nnf t =
  1.1016 +		inst_thm thy [t] iff_refl
  1.1017 +	(* convert 't' to NNF first *)
  1.1018 +	val nnf_thm  = make_nnf_thm thy t
  1.1019 +	val nnf      = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) nnf_thm
  1.1020 +	(* then simplify wrt. True/False (this should preserve NNF) *)
  1.1021 +	val simp_thm = simp_True_False_thm thy nnf
  1.1022 +	val simp     = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) simp_thm
  1.1023 +	(* initialize var_id, in case the term already contains variables of the form "cnfx_<int>" *)
  1.1024 +	val _        = (var_id := fold (fn free => fn max =>
  1.1025 +		let
  1.1026 +			val (name, _) = dest_Free free
  1.1027 +			val idx       = if String.isPrefix "cnfx_" name then
  1.1028 +					(Int.fromString o String.extract) (name, String.size "cnfx_", NONE)
  1.1029 +				else
  1.1030 +					NONE
  1.1031 +		in
  1.1032 +			Int.max (max, getOpt (idx, 0))
  1.1033 +		end) (term_frees simp) 0)
  1.1034 +	(* finally, convert to definitional CNF (this should preserve the simplification) *)
  1.1035 +	val cnfx_thm = make_cnfx_thm_from_nnf simp
  1.1036 +in
  1.1037 +	iff_trans OF [iff_trans OF [nnf_thm, simp_thm], cnfx_thm]
  1.1038 +end;
  1.1039  
  1.1040 - | mk_cnfx_thm sign t  = error ("I don't know how to handle the formula " ^ 
  1.1041 -                          (Sign.string_of_term sign t))
  1.1042 +(* ------------------------------------------------------------------------- *)
  1.1043 +(*                                  Tactics                                  *)
  1.1044 +(* ------------------------------------------------------------------------- *)
  1.1045  
  1.1046 -and cnfx_newlit sign p q r  = 
  1.1047 -   let val lit = read ("lit_" ^ (Int.toString (!lit_id)) ^ " :: bool")
  1.1048 -       val _ = (lit_id := !lit_id + 1)
  1.1049 -       val ct_lit = cterm_of sign lit
  1.1050 -       val x_conj = ext_conj lit p q r
  1.1051 -       val thm1 = inst_thm sign [p,q,r] cnf_newlit
  1.1052 -       val thm2 = mk_cnfx_thm sign x_conj 
  1.1053 -       val thm3 = forall_intr ct_lit thm2
  1.1054 -       val thm4 = strip_shyps (thm3 COMP allI)
  1.1055 -       val thm5 = strip_shyps (thm4 RS cnf_all_ex)
  1.1056 -   in
  1.1057 -       cnf_eq_trans OF [thm1, thm5]
  1.1058 -   end
  1.1059 +(* ------------------------------------------------------------------------- *)
  1.1060 +(* weakening_tac: removes the first hypothesis of the 'i'-th subgoal         *)
  1.1061 +(* ------------------------------------------------------------------------- *)
  1.1062  
  1.1063 +(* int -> Tactical.tactic *)
  1.1064  
  1.1065 -(* Theorems for converting formula into CNF (in primitive form), with 
  1.1066 -   new extra variables *)
  1.1067 -
  1.1068 +fun weakening_tac i =
  1.1069 +	dtac weakening_thm i THEN atac (i+1);
  1.1070  
  1.1071 -fun mk_cnfx_thms sg nil = nil
  1.1072 -  | mk_cnfx_thms sg (x::l) = 
  1.1073 -    let val t = term_of_thm x
  1.1074 -    in
  1.1075 -      if (is_clause t) then x :: mk_cnfx_thms sg l
  1.1076 -      else 
  1.1077 -       let val thm1 = mk_cnfx_thm sg t
  1.1078 -           val thm2 = cnf_eq_imp OF [thm1,x]
  1.1079 -       in 
  1.1080 -           thm2 :: mk_cnfx_thms sg l
  1.1081 -       end
  1.1082 -    end
  1.1083 +(* ------------------------------------------------------------------------- *)
  1.1084 +(* cnf_rewrite_tac: converts all premises of the 'i'-th subgoal to CNF       *)
  1.1085 +(*      (possibly causing an exponential blowup in the length of each        *)
  1.1086 +(*      premise)                                                             *)
  1.1087 +(* ------------------------------------------------------------------------- *)
  1.1088  
  1.1089 +(* int -> Tactical.tactic *)
  1.1090  
  1.1091 -(* Tactic for converting hypotheses into CNF, possibly
  1.1092 -   introducing new variables *)
  1.1093 -
  1.1094 -fun cnfx_thin_tac state =
  1.1095 -let val sg = Thm.sign_of_thm state
  1.1096 -in
  1.1097 -case (prems_of state) of 
  1.1098 -  [] => Seq.empty
  1.1099 -| (subgoal::_) => 
  1.1100 -   let val n = num_of_hyps (strip_all_body subgoal);
  1.1101 -       val tac1 =  METAHYPS (fn l => cut_facts_tac (mk_cnfx_thms sg l) 1) 1
  1.1102 -   in
  1.1103 -      EVERY [tac1, weakenings_tac n, 
  1.1104 -             REPEAT (etac conjE 1 ORELSE etac exE 1)] state
  1.1105 -   end
  1.1106 -end
  1.1107 -
  1.1108 -(* Tactic for converting the conclusion of a goal into CNF *)
  1.1109 +fun cnf_rewrite_tac i =
  1.1110 +	(* cut the CNF formulas as new premises *)
  1.1111 +	METAHYPS (fn prems =>
  1.1112 +		let
  1.1113 +			val cnf_thms = map (fn pr => make_cnf_thm (theory_of_thm pr) ((HOLogic.dest_Trueprop o prop_of) pr)) prems
  1.1114 +			val cut_thms = map (fn (th, pr) => cnftac_eq_imp OF [th, pr]) (cnf_thms ~~ prems)
  1.1115 +		in
  1.1116 +			cut_facts_tac cut_thms 1
  1.1117 +		end) i
  1.1118 +	(* remove the original premises *)
  1.1119 +	THEN SELECT_GOAL (fn thm =>
  1.1120 +		let
  1.1121 +			val n = Logic.count_prems ((Term.strip_all_body o fst o Logic.dest_implies o prop_of) thm, 0)
  1.1122 +		in
  1.1123 +			PRIMITIVE (funpow (n div 2) (Seq.hd o weakening_tac 1)) thm
  1.1124 +		end) i;
  1.1125  
  1.1126 -fun get_concl (Const("Trueprop", _) $ x) = get_concl x
  1.1127 -  | get_concl (Const("==>",_) $ x $ y) = get_concl y
  1.1128 -  | get_concl t = t
  1.1129 +(* ------------------------------------------------------------------------- *)
  1.1130 +(* cnfx_rewrite_tac: converts all premises of the 'i'-th subgoal to CNF      *)
  1.1131 +(*      (possibly introducing new literals)                                  *)
  1.1132 +(* ------------------------------------------------------------------------- *)
  1.1133 +
  1.1134 +(* int -> Tactical.tactic *)
  1.1135  
  1.1136 -fun cnf_concl_tac' state =
  1.1137 -case (prems_of state) of 
  1.1138 -  [] => Seq.empty
  1.1139 -| (subgoal::_) =>
  1.1140 -  let val sg = Thm.sign_of_thm state
  1.1141 -      val c = get_concl subgoal
  1.1142 -      val thm1 = (mk_cnf_thm sg c) RS cnf_eq_sym
  1.1143 -      val thm2 = thm1 RS subst
  1.1144 -  in
  1.1145 -    rtac thm2 1 state
  1.1146 -  end
  1.1147 +fun cnfx_rewrite_tac i =
  1.1148 +	(* cut the CNF formulas as new premises *)
  1.1149 +	METAHYPS (fn prems =>
  1.1150 +		let
  1.1151 +			val cnfx_thms = map (fn pr => make_cnfx_thm (theory_of_thm pr) ((HOLogic.dest_Trueprop o prop_of) pr)) prems
  1.1152 +			val cut_thms  = map (fn (th, pr) => cnftac_eq_imp OF [th, pr]) (cnfx_thms ~~ prems)
  1.1153 +		in
  1.1154 +			cut_facts_tac cut_thms 1
  1.1155 +		end) i
  1.1156 +	(* remove the original premises *)
  1.1157 +	THEN SELECT_GOAL (fn thm =>
  1.1158 +		let
  1.1159 +			val n = Logic.count_prems ((Term.strip_all_body o fst o Logic.dest_implies o prop_of) thm, 0)
  1.1160 +		in
  1.1161 +			PRIMITIVE (funpow (n div 2) (Seq.hd o weakening_tac 1)) thm
  1.1162 +		end) i;
  1.1163  
  1.1164 -val cnf_concl_tac  = METAHYPS (fn l => cnf_concl_tac') 1 
  1.1165 -
  1.1166 -
  1.1167 -end (*of structure*)
  1.1168 +end;  (* of structure *)