| author | wenzelm | 
| Fri, 22 Dec 2017 22:39:31 +0100 | |
| changeset 67267 | c5994f1fa0fa | 
| parent 67149 | e61557884799 | 
| child 67710 | cc2db3239932 | 
| permissions | -rw-r--r-- | 
| 55239 | 1  | 
(* Title: HOL/Tools/cnf.ML  | 
| 17618 | 2  | 
Author: Alwen Tiu, QSL Team, LORIA (http://qsl.loria.fr)  | 
| 
29265
 
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
 
wenzelm 
parents: 
26341 
diff
changeset
 | 
3  | 
Author: Tjark Weber, TU Muenchen  | 
| 17618 | 4  | 
|
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
5  | 
FIXME: major overlaps with the code in meson.ML  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
6  | 
|
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
7  | 
Functions and tactics to transform a formula into Conjunctive Normal  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
8  | 
Form (CNF).  | 
| 24958 | 9  | 
|
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
10  | 
A formula in CNF is of the following form:  | 
| 17618 | 11  | 
|
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
12  | 
(x11 | x12 | ... | x1n) & ... & (xm1 | xm2 | ... | xmk)  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
13  | 
False  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
14  | 
True  | 
| 17618 | 15  | 
|
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
16  | 
where each xij is a literal (a positive or negative atomic Boolean  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
17  | 
term), i.e. the formula is a conjunction of disjunctions of literals,  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
18  | 
or "False", or "True".  | 
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
19  | 
|
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
20  | 
A (non-empty) disjunction of literals is referred to as "clause".  | 
| 17618 | 21  | 
|
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
22  | 
For the purpose of SAT proof reconstruction, we also make use of  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
23  | 
another representation of clauses, which we call the "raw clauses".  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
24  | 
Raw clauses are of the form  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
25  | 
|
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
26  | 
[..., x1', x2', ..., xn'] |- False ,  | 
| 17618 | 27  | 
|
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
28  | 
where each xi is a literal, and each xi' is the negation normal form  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
29  | 
of ~xi.  | 
| 
19236
 
150e8b0fb991
clauses now use (meta-)hyps instead of (meta-)implications; significant speedup
 
webertj 
parents: 
17809 
diff
changeset
 | 
30  | 
|
| 
32960
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
31  | 
Literals are successively removed from the hyps of raw clauses by  | 
| 
 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 
wenzelm 
parents: 
32740 
diff
changeset
 | 
32  | 
resolution during SAT proof reconstruction.  | 
| 17618 | 33  | 
*)  | 
34  | 
||
35  | 
signature CNF =  | 
|
36  | 
sig  | 
|
| 41447 | 37  | 
val is_atom: term -> bool  | 
38  | 
val is_literal: term -> bool  | 
|
39  | 
val is_clause: term -> bool  | 
|
40  | 
val clause_is_trivial: term -> bool  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
41  | 
|
| 
59498
 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 
wenzelm 
parents: 
58963 
diff
changeset
 | 
42  | 
val clause2raw_thm: Proof.context -> thm -> thm  | 
| 42335 | 43  | 
val make_nnf_thm: theory -> term -> thm  | 
| 17618 | 44  | 
|
| 
58963
 
26bf09b95dda
proper context for assume_tac (atac remains as fall-back without context);
 
wenzelm 
parents: 
58839 
diff
changeset
 | 
45  | 
val weakening_tac: Proof.context -> int -> tactic (* removes the first hypothesis of a subgoal *)  | 
| 17618 | 46  | 
|
| 42335 | 47  | 
val make_cnf_thm: Proof.context -> term -> thm  | 
48  | 
val make_cnfx_thm: Proof.context -> term -> thm  | 
|
| 41447 | 49  | 
val cnf_rewrite_tac: Proof.context -> int -> tactic (* converts all prems of a subgoal to CNF *)  | 
50  | 
val cnfx_rewrite_tac: Proof.context -> int -> tactic  | 
|
51  | 
(* converts all prems of a subgoal to (almost) definitional CNF *)  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
52  | 
end;  | 
| 17618 | 53  | 
|
| 55239 | 54  | 
structure CNF : CNF =  | 
| 17618 | 55  | 
struct  | 
56  | 
||
| 60759 | 57  | 
val clause2raw_notE      = @{lemma "\<lbrakk>P; \<not>P\<rbrakk> \<Longrightarrow> False" by auto};
 | 
58  | 
val clause2raw_not_disj  = @{lemma "\<lbrakk>\<not> P; \<not> Q\<rbrakk> \<Longrightarrow> \<not> (P \<or> Q)" by auto};
 | 
|
59  | 
val clause2raw_not_not   = @{lemma "P \<Longrightarrow> \<not>\<not> P" by auto};
 | 
|
| 17618 | 60  | 
|
| 
30607
 
c3d1590debd8
eliminated global SIMPSET, CLASET etc. -- refer to explicit context;
 
wenzelm 
parents: 
29265 
diff
changeset
 | 
61  | 
val iff_refl             = @{lemma "(P::bool) = P" by auto};
 | 
| 
 
c3d1590debd8
eliminated global SIMPSET, CLASET etc. -- refer to explicit context;
 
wenzelm 
parents: 
29265 
diff
changeset
 | 
62  | 
val iff_trans            = @{lemma "[| (P::bool) = Q; Q = R |] ==> P = R" by auto};
 | 
| 60759 | 63  | 
val conj_cong            = @{lemma "[| P = P'; Q = Q' |] ==> (P \<and> Q) = (P' \<and> Q')" by auto};
 | 
64  | 
val disj_cong            = @{lemma "[| P = P'; Q = Q' |] ==> (P \<or> Q) = (P' \<or> Q')" by auto};
 | 
|
| 17618 | 65  | 
|
| 67091 | 66  | 
val make_nnf_imp         = @{lemma "[| (\<not>P) = P'; Q = Q' |] ==> (P \<longrightarrow> Q) = (P' \<or> Q')" by auto};
 | 
67  | 
val make_nnf_iff         = @{lemma "[| P = P'; (\<not>P) = NP; Q = Q'; (\<not>Q) = NQ |] ==> (P = Q) = ((P' \<or> NQ) \<and> (NP \<or> Q'))" by auto};
 | 
|
68  | 
val make_nnf_not_false   = @{lemma "(\<not>False) = True" by auto};
 | 
|
69  | 
val make_nnf_not_true    = @{lemma "(\<not>True) = False" by auto};
 | 
|
70  | 
val make_nnf_not_conj    = @{lemma "[| (\<not>P) = P'; (\<not>Q) = Q' |] ==> (\<not>(P \<and> Q)) = (P' \<or> Q')" by auto};
 | 
|
71  | 
val make_nnf_not_disj    = @{lemma "[| (\<not>P) = P'; (\<not>Q) = Q' |] ==> (\<not>(P \<or> Q)) = (P' \<and> Q')" by auto};
 | 
|
72  | 
val make_nnf_not_imp     = @{lemma "[| P = P'; (\<not>Q) = Q' |] ==> (\<not>(P \<longrightarrow> Q)) = (P' \<and> Q')" by auto};
 | 
|
73  | 
val make_nnf_not_iff     = @{lemma "[| P = P'; (\<not>P) = NP; Q = Q'; (\<not>Q) = NQ |] ==> (\<not>(P = Q)) = ((P' \<or> Q') \<and> (NP \<or> NQ))" by auto};
 | 
|
74  | 
val make_nnf_not_not     = @{lemma "P = P' ==> (\<not>\<not>P) = P'" by auto};
 | 
|
| 17618 | 75  | 
|
| 60759 | 76  | 
val simp_TF_conj_True_l  = @{lemma "[| P = True; Q = Q' |] ==> (P \<and> Q) = Q'" by auto};
 | 
77  | 
val simp_TF_conj_True_r  = @{lemma "[| P = P'; Q = True |] ==> (P \<and> Q) = P'" by auto};
 | 
|
78  | 
val simp_TF_conj_False_l = @{lemma "P = False ==> (P \<and> Q) = False" by auto};
 | 
|
79  | 
val simp_TF_conj_False_r = @{lemma "Q = False ==> (P \<and> Q) = False" by auto};
 | 
|
80  | 
val simp_TF_disj_True_l  = @{lemma "P = True ==> (P \<or> Q) = True" by auto};
 | 
|
81  | 
val simp_TF_disj_True_r  = @{lemma "Q = True ==> (P \<or> Q) = True" by auto};
 | 
|
82  | 
val simp_TF_disj_False_l = @{lemma "[| P = False; Q = Q' |] ==> (P \<or> Q) = Q'" by auto};
 | 
|
83  | 
val simp_TF_disj_False_r = @{lemma "[| P = P'; Q = False |] ==> (P \<or> Q) = P'" by auto};
 | 
|
| 17618 | 84  | 
|
| 60759 | 85  | 
val make_cnf_disj_conj_l = @{lemma "[| (P \<or> R) = PR; (Q \<or> R) = QR |] ==> ((P \<and> Q) \<or> R) = (PR \<and> QR)" by auto};
 | 
86  | 
val make_cnf_disj_conj_r = @{lemma "[| (P \<or> Q) = PQ; (P \<or> R) = PR |] ==> (P \<or> (Q \<and> R)) = (PQ \<and> PR)" by auto};
 | 
|
| 17618 | 87  | 
|
| 67091 | 88  | 
val make_cnfx_disj_ex_l  = @{lemma "((\<exists>(x::bool). P x) \<or> Q) = (\<exists>x. P x \<or> Q)" by auto};
 | 
89  | 
val make_cnfx_disj_ex_r  = @{lemma "(P \<or> (\<exists>(x::bool). Q x)) = (\<exists>x. P \<or> Q x)" by auto};
 | 
|
90  | 
val make_cnfx_newlit     = @{lemma "(P \<or> Q) = (\<exists>x. (P \<or> x) \<and> (Q \<or> \<not>x))" by auto};
 | 
|
91  | 
val make_cnfx_ex_cong    = @{lemma "(\<forall>(x::bool). P x = Q x) \<Longrightarrow> (\<exists>x. P x) = (\<exists>x. Q x)" by auto};
 | 
|
| 17618 | 92  | 
|
| 
30607
 
c3d1590debd8
eliminated global SIMPSET, CLASET etc. -- refer to explicit context;
 
wenzelm 
parents: 
29265 
diff
changeset
 | 
93  | 
val weakening_thm        = @{lemma "[| P; Q |] ==> Q" by auto};
 | 
| 17618 | 94  | 
|
| 
30607
 
c3d1590debd8
eliminated global SIMPSET, CLASET etc. -- refer to explicit context;
 
wenzelm 
parents: 
29265 
diff
changeset
 | 
95  | 
val cnftac_eq_imp        = @{lemma "[| P = Q; P |] ==> Q" by auto};
 | 
| 17618 | 96  | 
|
| 67149 | 97  | 
fun is_atom (Const (\<^const_name>\<open>False\<close>, _)) = false  | 
98  | 
| is_atom (Const (\<^const_name>\<open>True\<close>, _)) = false  | 
|
99  | 
| is_atom (Const (\<^const_name>\<open>HOL.conj\<close>, _) $ _ $ _) = false  | 
|
100  | 
| is_atom (Const (\<^const_name>\<open>HOL.disj\<close>, _) $ _ $ _) = false  | 
|
101  | 
| is_atom (Const (\<^const_name>\<open>HOL.implies\<close>, _) $ _ $ _) = false  | 
|
102  | 
  | is_atom (Const (\<^const_name>\<open>HOL.eq\<close>, Type ("fun", \<^typ>\<open>bool\<close> :: _)) $ _ $ _) = false
 | 
|
103  | 
| is_atom (Const (\<^const_name>\<open>Not\<close>, _) $ _) = false  | 
|
| 41447 | 104  | 
| is_atom _ = true;  | 
| 17618 | 105  | 
|
| 67149 | 106  | 
fun is_literal (Const (\<^const_name>\<open>Not\<close>, _) $ x) = is_atom x  | 
| 41447 | 107  | 
| is_literal x = is_atom x;  | 
| 17618 | 108  | 
|
| 67149 | 109  | 
fun is_clause (Const (\<^const_name>\<open>HOL.disj\<close>, _) $ x $ y) = is_clause x andalso is_clause y  | 
| 41447 | 110  | 
| is_clause x = is_literal x;  | 
| 17618 | 111  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
112  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
113  | 
(* clause_is_trivial: a clause is trivially true if it contains both an atom *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
114  | 
(* and the atom's negation *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
115  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
116  | 
|
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
117  | 
fun clause_is_trivial c =  | 
| 41447 | 118  | 
let  | 
| 67149 | 119  | 
fun dual (Const (\<^const_name>\<open>Not\<close>, _) $ x) = x  | 
| 41447 | 120  | 
| dual x = HOLogic.Not $ x  | 
121  | 
fun has_duals [] = false  | 
|
122  | 
| has_duals (x::xs) = member (op =) xs (dual x) orelse has_duals xs  | 
|
123  | 
in  | 
|
124  | 
has_duals (HOLogic.disjuncts c)  | 
|
125  | 
end;  | 
|
| 17618 | 126  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
127  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
128  | 
(* clause2raw_thm: translates a clause into a raw clause, i.e. *)  | 
| 
20440
 
e6fe74eebda3
faster clause representation (again): full CNF formula as a hypothesis, instead of separate clauses
 
webertj 
parents: 
19236 
diff
changeset
 | 
129  | 
(* [...] |- x1 | ... | xn *)  | 
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
130  | 
(* (where each xi is a literal) is translated to *)  | 
| 
20440
 
e6fe74eebda3
faster clause representation (again): full CNF formula as a hypothesis, instead of separate clauses
 
webertj 
parents: 
19236 
diff
changeset
 | 
131  | 
(* [..., x1', ..., xn'] |- False , *)  | 
| 
 
e6fe74eebda3
faster clause representation (again): full CNF formula as a hypothesis, instead of separate clauses
 
webertj 
parents: 
19236 
diff
changeset
 | 
132  | 
(* where each xi' is the negation normal form of ~xi *)  | 
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
133  | 
(* ------------------------------------------------------------------------- *)  | 
| 17618 | 134  | 
|
| 
59498
 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 
wenzelm 
parents: 
58963 
diff
changeset
 | 
135  | 
fun clause2raw_thm ctxt clause =  | 
| 41447 | 136  | 
let  | 
137  | 
(* eliminates negated disjunctions from the i-th premise, possibly *)  | 
|
138  | 
(* adding new premises, then continues with the (i+1)-th premise *)  | 
|
139  | 
(* int -> Thm.thm -> Thm.thm *)  | 
|
140  | 
fun not_disj_to_prem i thm =  | 
|
| 59582 | 141  | 
if i > Thm.nprems_of thm then  | 
| 41447 | 142  | 
thm  | 
143  | 
else  | 
|
| 
59498
 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 
wenzelm 
parents: 
58963 
diff
changeset
 | 
144  | 
not_disj_to_prem (i+1)  | 
| 
 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 
wenzelm 
parents: 
58963 
diff
changeset
 | 
145  | 
(Seq.hd (REPEAT_DETERM (resolve_tac ctxt [clause2raw_not_disj] i) thm))  | 
| 41447 | 146  | 
(* moves all premises to hyps, i.e. "[...] |- A1 ==> ... ==> An ==> B" *)  | 
147  | 
(* becomes "[..., A1, ..., An] |- B" *)  | 
|
148  | 
(* Thm.thm -> Thm.thm *)  | 
|
149  | 
fun prems_to_hyps thm =  | 
|
150  | 
fold (fn cprem => fn thm' =>  | 
|
151  | 
Thm.implies_elim thm' (Thm.assume cprem)) (cprems_of thm) thm  | 
|
152  | 
in  | 
|
153  | 
(* [...] |- ~(x1 | ... | xn) ==> False *)  | 
|
154  | 
(clause2raw_notE OF [clause])  | 
|
155  | 
(* [...] |- ~x1 ==> ... ==> ~xn ==> False *)  | 
|
156  | 
|> not_disj_to_prem 1  | 
|
157  | 
(* [...] |- x1' ==> ... ==> xn' ==> False *)  | 
|
| 
59498
 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 
wenzelm 
parents: 
58963 
diff
changeset
 | 
158  | 
|> Seq.hd o TRYALL (resolve_tac ctxt [clause2raw_not_not])  | 
| 41447 | 159  | 
(* [..., x1', ..., xn'] |- False *)  | 
160  | 
|> prems_to_hyps  | 
|
161  | 
end;  | 
|
| 17618 | 162  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
163  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
164  | 
(* inst_thm: instantiates a theorem with a list of terms *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
165  | 
(* ------------------------------------------------------------------------- *)  | 
| 17618 | 166  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
167  | 
fun inst_thm thy ts thm =  | 
| 60801 | 168  | 
Thm.instantiate' [] (map (SOME o Thm.global_cterm_of thy) ts) thm;  | 
| 17618 | 169  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
170  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
171  | 
(* Naive CNF transformation *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
172  | 
(* ------------------------------------------------------------------------- *)  | 
| 17618 | 173  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
174  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
175  | 
(* make_nnf_thm: produces a theorem of the form t = t', where t' is the *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
176  | 
(* negation normal form (i.e. negation only occurs in front of atoms) *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
177  | 
(*      of t; implications ("-->") and equivalences ("=" on bool) are        *)
 | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
178  | 
(* eliminated (possibly causing an exponential blowup) *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
179  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
180  | 
|
| 67149 | 181  | 
fun make_nnf_thm thy (Const (\<^const_name>\<open>HOL.conj\<close>, _) $ x $ y) =  | 
| 41447 | 182  | 
let  | 
183  | 
val thm1 = make_nnf_thm thy x  | 
|
184  | 
val thm2 = make_nnf_thm thy y  | 
|
185  | 
in  | 
|
186  | 
conj_cong OF [thm1, thm2]  | 
|
187  | 
end  | 
|
| 67149 | 188  | 
| make_nnf_thm thy (Const (\<^const_name>\<open>HOL.disj\<close>, _) $ x $ y) =  | 
| 41447 | 189  | 
let  | 
190  | 
val thm1 = make_nnf_thm thy x  | 
|
191  | 
val thm2 = make_nnf_thm thy y  | 
|
192  | 
in  | 
|
193  | 
disj_cong OF [thm1, thm2]  | 
|
194  | 
end  | 
|
| 67149 | 195  | 
| make_nnf_thm thy (Const (\<^const_name>\<open>HOL.implies\<close>, _) $ x $ y) =  | 
| 41447 | 196  | 
let  | 
197  | 
val thm1 = make_nnf_thm thy (HOLogic.Not $ x)  | 
|
198  | 
val thm2 = make_nnf_thm thy y  | 
|
199  | 
in  | 
|
200  | 
make_nnf_imp OF [thm1, thm2]  | 
|
201  | 
end  | 
|
| 67149 | 202  | 
  | make_nnf_thm thy (Const (\<^const_name>\<open>HOL.eq\<close>, Type ("fun", \<^typ>\<open>bool\<close> :: _)) $ x $ y) =
 | 
| 41447 | 203  | 
let  | 
204  | 
val thm1 = make_nnf_thm thy x  | 
|
205  | 
val thm2 = make_nnf_thm thy (HOLogic.Not $ x)  | 
|
206  | 
val thm3 = make_nnf_thm thy y  | 
|
207  | 
val thm4 = make_nnf_thm thy (HOLogic.Not $ y)  | 
|
208  | 
in  | 
|
209  | 
make_nnf_iff OF [thm1, thm2, thm3, thm4]  | 
|
210  | 
end  | 
|
| 67149 | 211  | 
| make_nnf_thm _ (Const (\<^const_name>\<open>Not\<close>, _) $ Const (\<^const_name>\<open>False\<close>, _)) =  | 
| 41447 | 212  | 
make_nnf_not_false  | 
| 67149 | 213  | 
| make_nnf_thm _ (Const (\<^const_name>\<open>Not\<close>, _) $ Const (\<^const_name>\<open>True\<close>, _)) =  | 
| 41447 | 214  | 
make_nnf_not_true  | 
| 67149 | 215  | 
| make_nnf_thm thy (Const (\<^const_name>\<open>Not\<close>, _) $ (Const (\<^const_name>\<open>HOL.conj\<close>, _) $ x $ y)) =  | 
| 41447 | 216  | 
let  | 
217  | 
val thm1 = make_nnf_thm thy (HOLogic.Not $ x)  | 
|
218  | 
val thm2 = make_nnf_thm thy (HOLogic.Not $ y)  | 
|
219  | 
in  | 
|
220  | 
make_nnf_not_conj OF [thm1, thm2]  | 
|
221  | 
end  | 
|
| 67149 | 222  | 
| make_nnf_thm thy (Const (\<^const_name>\<open>Not\<close>, _) $ (Const (\<^const_name>\<open>HOL.disj\<close>, _) $ x $ y)) =  | 
| 41447 | 223  | 
let  | 
224  | 
val thm1 = make_nnf_thm thy (HOLogic.Not $ x)  | 
|
225  | 
val thm2 = make_nnf_thm thy (HOLogic.Not $ y)  | 
|
226  | 
in  | 
|
227  | 
make_nnf_not_disj OF [thm1, thm2]  | 
|
228  | 
end  | 
|
229  | 
| make_nnf_thm thy  | 
|
| 67149 | 230  | 
(Const (\<^const_name>\<open>Not\<close>, _) $ (Const (\<^const_name>\<open>HOL.implies\<close>, _) $ x $ y)) =  | 
| 41447 | 231  | 
let  | 
232  | 
val thm1 = make_nnf_thm thy x  | 
|
233  | 
val thm2 = make_nnf_thm thy (HOLogic.Not $ y)  | 
|
234  | 
in  | 
|
235  | 
make_nnf_not_imp OF [thm1, thm2]  | 
|
236  | 
end  | 
|
237  | 
| make_nnf_thm thy  | 
|
| 67149 | 238  | 
(Const (\<^const_name>\<open>Not\<close>, _) $  | 
239  | 
        (Const (\<^const_name>\<open>HOL.eq\<close>, Type ("fun", \<^typ>\<open>bool\<close> :: _)) $ x $ y)) =
 | 
|
| 41447 | 240  | 
let  | 
241  | 
val thm1 = make_nnf_thm thy x  | 
|
242  | 
val thm2 = make_nnf_thm thy (HOLogic.Not $ x)  | 
|
243  | 
val thm3 = make_nnf_thm thy y  | 
|
244  | 
val thm4 = make_nnf_thm thy (HOLogic.Not $ y)  | 
|
245  | 
in  | 
|
246  | 
make_nnf_not_iff OF [thm1, thm2, thm3, thm4]  | 
|
247  | 
end  | 
|
| 67149 | 248  | 
| make_nnf_thm thy (Const (\<^const_name>\<open>Not\<close>, _) $ (Const (\<^const_name>\<open>Not\<close>, _) $ x)) =  | 
| 41447 | 249  | 
let  | 
250  | 
val thm1 = make_nnf_thm thy x  | 
|
251  | 
in  | 
|
252  | 
make_nnf_not_not OF [thm1]  | 
|
253  | 
end  | 
|
254  | 
| make_nnf_thm thy t = inst_thm thy [t] iff_refl;  | 
|
| 17618 | 255  | 
|
| 42335 | 256  | 
val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
 | 
257  | 
val eq_reflection = @{thm eq_reflection}
 | 
|
258  | 
||
259  | 
fun make_under_quantifiers ctxt make t =  | 
|
260  | 
let  | 
|
261  | 
fun conv ctxt ct =  | 
|
| 59582 | 262  | 
(case Thm.term_of ct of  | 
| 
45511
 
9b0f8ca4388e
continued implementation of lambda-lifting in Metis
 
blanchet 
parents: 
44121 
diff
changeset
 | 
263  | 
Const _ $ Abs _ => Conv.comb_conv (conv ctxt) ct  | 
| 42335 | 264  | 
| Abs _ => Conv.abs_conv (conv o snd) ctxt ct  | 
265  | 
| Const _ => Conv.all_conv ct  | 
|
| 59582 | 266  | 
| t => make t RS eq_reflection)  | 
| 59642 | 267  | 
in conv ctxt (Thm.cterm_of ctxt t) RS meta_eq_to_obj_eq end  | 
| 42335 | 268  | 
|
269  | 
fun make_nnf_thm_under_quantifiers ctxt =  | 
|
| 42361 | 270  | 
make_under_quantifiers ctxt (make_nnf_thm (Proof_Context.theory_of ctxt))  | 
| 42335 | 271  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
272  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
273  | 
(* simp_True_False_thm: produces a theorem t = t', where t' is equivalent to *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
274  | 
(* t, but simplified wrt. the following theorems: *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
275  | 
(* (True & x) = x *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
276  | 
(* (x & True) = x *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
277  | 
(* (False & x) = False *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
278  | 
(* (x & False) = False *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
279  | 
(* (True | x) = True *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
280  | 
(* (x | True) = True *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
281  | 
(* (False | x) = x *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
282  | 
(* (x | False) = x *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
283  | 
(* No simplification is performed below connectives other than & and |. *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
284  | 
(* Optimization: The right-hand side of a conjunction (disjunction) is *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
285  | 
(* simplified only if the left-hand side does not simplify to False *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
286  | 
(* (True, respectively). *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
287  | 
(* ------------------------------------------------------------------------- *)  | 
| 17618 | 288  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
289  | 
(* Theory.theory -> Term.term -> Thm.thm *)  | 
| 17618 | 290  | 
|
| 67149 | 291  | 
fun simp_True_False_thm thy (Const (\<^const_name>\<open>HOL.conj\<close>, _) $ x $ y) =  | 
| 41447 | 292  | 
let  | 
293  | 
val thm1 = simp_True_False_thm thy x  | 
|
| 59582 | 294  | 
val x'= (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm1  | 
| 41447 | 295  | 
in  | 
| 67149 | 296  | 
if x' = \<^term>\<open>False\<close> then  | 
| 41447 | 297  | 
simp_TF_conj_False_l OF [thm1] (* (x & y) = False *)  | 
298  | 
else  | 
|
299  | 
let  | 
|
300  | 
val thm2 = simp_True_False_thm thy y  | 
|
| 59582 | 301  | 
val y' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm2  | 
| 41447 | 302  | 
in  | 
| 67149 | 303  | 
if x' = \<^term>\<open>True\<close> then  | 
| 41447 | 304  | 
simp_TF_conj_True_l OF [thm1, thm2] (* (x & y) = y' *)  | 
| 67149 | 305  | 
else if y' = \<^term>\<open>False\<close> then  | 
| 41447 | 306  | 
simp_TF_conj_False_r OF [thm2] (* (x & y) = False *)  | 
| 67149 | 307  | 
else if y' = \<^term>\<open>True\<close> then  | 
| 41447 | 308  | 
simp_TF_conj_True_r OF [thm1, thm2] (* (x & y) = x' *)  | 
309  | 
else  | 
|
310  | 
conj_cong OF [thm1, thm2] (* (x & y) = (x' & y') *)  | 
|
311  | 
end  | 
|
312  | 
end  | 
|
| 67149 | 313  | 
| simp_True_False_thm thy (Const (\<^const_name>\<open>HOL.disj\<close>, _) $ x $ y) =  | 
| 41447 | 314  | 
let  | 
315  | 
val thm1 = simp_True_False_thm thy x  | 
|
| 59582 | 316  | 
val x' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm1  | 
| 41447 | 317  | 
in  | 
| 67149 | 318  | 
if x' = \<^term>\<open>True\<close> then  | 
| 41447 | 319  | 
simp_TF_disj_True_l OF [thm1] (* (x | y) = True *)  | 
320  | 
else  | 
|
321  | 
let  | 
|
322  | 
val thm2 = simp_True_False_thm thy y  | 
|
| 59582 | 323  | 
val y' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm2  | 
| 41447 | 324  | 
in  | 
| 67149 | 325  | 
if x' = \<^term>\<open>False\<close> then  | 
| 41447 | 326  | 
simp_TF_disj_False_l OF [thm1, thm2] (* (x | y) = y' *)  | 
| 67149 | 327  | 
else if y' = \<^term>\<open>True\<close> then  | 
| 41447 | 328  | 
simp_TF_disj_True_r OF [thm2] (* (x | y) = True *)  | 
| 67149 | 329  | 
else if y' = \<^term>\<open>False\<close> then  | 
| 41447 | 330  | 
simp_TF_disj_False_r OF [thm1, thm2] (* (x | y) = x' *)  | 
331  | 
else  | 
|
332  | 
disj_cong OF [thm1, thm2] (* (x | y) = (x' | y') *)  | 
|
333  | 
end  | 
|
334  | 
end  | 
|
335  | 
| simp_True_False_thm thy t = inst_thm thy [t] iff_refl; (* t = t *)  | 
|
| 17618 | 336  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
337  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
338  | 
(* make_cnf_thm: given any HOL term 't', produces a theorem t = t', where t' *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
339  | 
(* is in conjunction normal form. May cause an exponential blowup *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
340  | 
(* in the length of the term. *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
341  | 
(* ------------------------------------------------------------------------- *)  | 
| 17618 | 342  | 
|
| 42335 | 343  | 
fun make_cnf_thm ctxt t =  | 
| 41447 | 344  | 
let  | 
| 42361 | 345  | 
val thy = Proof_Context.theory_of ctxt  | 
| 67149 | 346  | 
fun make_cnf_thm_from_nnf (Const (\<^const_name>\<open>HOL.conj\<close>, _) $ x $ y) =  | 
| 41447 | 347  | 
let  | 
348  | 
val thm1 = make_cnf_thm_from_nnf x  | 
|
349  | 
val thm2 = make_cnf_thm_from_nnf y  | 
|
350  | 
in  | 
|
351  | 
conj_cong OF [thm1, thm2]  | 
|
352  | 
end  | 
|
| 67149 | 353  | 
| make_cnf_thm_from_nnf (Const (\<^const_name>\<open>HOL.disj\<close>, _) $ x $ y) =  | 
| 41447 | 354  | 
let  | 
355  | 
(* produces a theorem "(x' | y') = t'", where x', y', and t' are in CNF *)  | 
|
| 67149 | 356  | 
fun make_cnf_disj_thm (Const (\<^const_name>\<open>HOL.conj\<close>, _) $ x1 $ x2) y' =  | 
| 41447 | 357  | 
let  | 
358  | 
val thm1 = make_cnf_disj_thm x1 y'  | 
|
359  | 
val thm2 = make_cnf_disj_thm x2 y'  | 
|
360  | 
in  | 
|
361  | 
make_cnf_disj_conj_l OF [thm1, thm2] (* ((x1 & x2) | y') = ((x1 | y')' & (x2 | y')') *)  | 
|
362  | 
end  | 
|
| 67149 | 363  | 
| make_cnf_disj_thm x' (Const (\<^const_name>\<open>HOL.conj\<close>, _) $ y1 $ y2) =  | 
| 41447 | 364  | 
let  | 
365  | 
val thm1 = make_cnf_disj_thm x' y1  | 
|
366  | 
val thm2 = make_cnf_disj_thm x' y2  | 
|
367  | 
in  | 
|
368  | 
make_cnf_disj_conj_r OF [thm1, thm2] (* (x' | (y1 & y2)) = ((x' | y1)' & (x' | y2)') *)  | 
|
369  | 
end  | 
|
370  | 
| make_cnf_disj_thm x' y' =  | 
|
371  | 
inst_thm thy [HOLogic.mk_disj (x', y')] iff_refl (* (x' | y') = (x' | y') *)  | 
|
372  | 
val thm1 = make_cnf_thm_from_nnf x  | 
|
373  | 
val thm2 = make_cnf_thm_from_nnf y  | 
|
| 59582 | 374  | 
val x' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm1  | 
375  | 
val y' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm2  | 
|
| 41447 | 376  | 
val disj_thm = disj_cong OF [thm1, thm2] (* (x | y) = (x' | y') *)  | 
377  | 
in  | 
|
378  | 
iff_trans OF [disj_thm, make_cnf_disj_thm x' y']  | 
|
379  | 
end  | 
|
380  | 
| make_cnf_thm_from_nnf t = inst_thm thy [t] iff_refl  | 
|
381  | 
(* convert 't' to NNF first *)  | 
|
| 42335 | 382  | 
val nnf_thm = make_nnf_thm_under_quantifiers ctxt t  | 
383  | 
(*###  | 
|
| 41447 | 384  | 
val nnf_thm = make_nnf_thm thy t  | 
| 42335 | 385  | 
*)  | 
| 59582 | 386  | 
val nnf = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) nnf_thm  | 
| 41447 | 387  | 
(* then simplify wrt. True/False (this should preserve NNF) *)  | 
388  | 
val simp_thm = simp_True_False_thm thy nnf  | 
|
| 59582 | 389  | 
val simp = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) simp_thm  | 
| 41447 | 390  | 
(* finally, convert to CNF (this should preserve the simplification) *)  | 
| 42335 | 391  | 
val cnf_thm = make_under_quantifiers ctxt make_cnf_thm_from_nnf simp  | 
392  | 
(* ###  | 
|
| 41447 | 393  | 
val cnf_thm = make_cnf_thm_from_nnf simp  | 
| 42335 | 394  | 
*)  | 
| 41447 | 395  | 
in  | 
396  | 
iff_trans OF [iff_trans OF [nnf_thm, simp_thm], cnf_thm]  | 
|
397  | 
end;  | 
|
| 17618 | 398  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
399  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
400  | 
(* CNF transformation by introducing new literals *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
401  | 
(* ------------------------------------------------------------------------- *)  | 
| 17618 | 402  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
403  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
404  | 
(* make_cnfx_thm: given any HOL term 't', produces a theorem t = t', where *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
405  | 
(* t' is almost in conjunction normal form, except that conjunctions *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
406  | 
(* and existential quantifiers may be nested. (Use e.g. 'REPEAT_DETERM *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
407  | 
(* (etac exE i ORELSE etac conjE i)' afterwards to normalize.) May *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
408  | 
(* introduce new (existentially bound) literals. Note: the current *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
409  | 
(* implementation calls 'make_nnf_thm', causing an exponential blowup *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
410  | 
(* in the case of nested equivalences. *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
411  | 
(* ------------------------------------------------------------------------- *)  | 
| 17618 | 412  | 
|
| 42335 | 413  | 
fun make_cnfx_thm ctxt t =  | 
| 41447 | 414  | 
let  | 
| 42361 | 415  | 
val thy = Proof_Context.theory_of ctxt  | 
| 41447 | 416  | 
val var_id = Unsynchronized.ref 0 (* properly initialized below *)  | 
417  | 
fun new_free () =  | 
|
418  | 
      Free ("cnfx_" ^ string_of_int (Unsynchronized.inc var_id), HOLogic.boolT)
 | 
|
| 67149 | 419  | 
fun make_cnfx_thm_from_nnf (Const (\<^const_name>\<open>HOL.conj\<close>, _) $ x $ y) : thm =  | 
| 41447 | 420  | 
let  | 
421  | 
val thm1 = make_cnfx_thm_from_nnf x  | 
|
422  | 
val thm2 = make_cnfx_thm_from_nnf y  | 
|
423  | 
in  | 
|
424  | 
conj_cong OF [thm1, thm2]  | 
|
425  | 
end  | 
|
| 67149 | 426  | 
| make_cnfx_thm_from_nnf (Const (\<^const_name>\<open>HOL.disj\<close>, _) $ x $ y) =  | 
| 41447 | 427  | 
if is_clause x andalso is_clause y then  | 
428  | 
inst_thm thy [HOLogic.mk_disj (x, y)] iff_refl  | 
|
429  | 
else if is_literal y orelse is_literal x then  | 
|
430  | 
let  | 
|
431  | 
(* produces a theorem "(x' | y') = t'", where x', y', and t' are *)  | 
|
432  | 
(* almost in CNF, and x' or y' is a literal *)  | 
|
| 67149 | 433  | 
fun make_cnfx_disj_thm (Const (\<^const_name>\<open>HOL.conj\<close>, _) $ x1 $ x2) y' =  | 
| 41447 | 434  | 
let  | 
435  | 
val thm1 = make_cnfx_disj_thm x1 y'  | 
|
436  | 
val thm2 = make_cnfx_disj_thm x2 y'  | 
|
437  | 
in  | 
|
438  | 
make_cnf_disj_conj_l OF [thm1, thm2] (* ((x1 & x2) | y') = ((x1 | y')' & (x2 | y')') *)  | 
|
439  | 
end  | 
|
| 67149 | 440  | 
| make_cnfx_disj_thm x' (Const (\<^const_name>\<open>HOL.conj\<close>, _) $ y1 $ y2) =  | 
| 41447 | 441  | 
let  | 
442  | 
val thm1 = make_cnfx_disj_thm x' y1  | 
|
443  | 
val thm2 = make_cnfx_disj_thm x' y2  | 
|
444  | 
in  | 
|
445  | 
make_cnf_disj_conj_r OF [thm1, thm2] (* (x' | (y1 & y2)) = ((x' | y1)' & (x' | y2)') *)  | 
|
446  | 
end  | 
|
| 67149 | 447  | 
| make_cnfx_disj_thm (\<^term>\<open>Ex :: (bool \<Rightarrow> bool) \<Rightarrow> bool\<close> $ x') y' =  | 
| 41447 | 448  | 
let  | 
449  | 
val thm1 = inst_thm thy [x', y'] make_cnfx_disj_ex_l (* ((Ex x') | y') = (Ex (x' | y')) *)  | 
|
450  | 
val var = new_free ()  | 
|
451  | 
val thm2 = make_cnfx_disj_thm (betapply (x', var)) y' (* (x' | y') = body' *)  | 
|
| 
59621
 
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
 
wenzelm 
parents: 
59582 
diff
changeset
 | 
452  | 
val thm3 = Thm.forall_intr (Thm.global_cterm_of thy var) thm2 (* !!v. (x' | y') = body' *)  | 
| 41447 | 453  | 
val thm4 = Thm.strip_shyps (thm3 COMP allI) (* ALL v. (x' | y') = body' *)  | 
454  | 
val thm5 = Thm.strip_shyps (thm4 RS make_cnfx_ex_cong) (* (EX v. (x' | y')) = (EX v. body') *)  | 
|
455  | 
in  | 
|
456  | 
iff_trans OF [thm1, thm5] (* ((Ex x') | y') = (Ex v. body') *)  | 
|
457  | 
end  | 
|
| 67149 | 458  | 
| make_cnfx_disj_thm x' (\<^term>\<open>Ex :: (bool \<Rightarrow> bool) \<Rightarrow> bool\<close> $ y') =  | 
| 41447 | 459  | 
let  | 
460  | 
val thm1 = inst_thm thy [x', y'] make_cnfx_disj_ex_r (* (x' | (Ex y')) = (Ex (x' | y')) *)  | 
|
461  | 
val var = new_free ()  | 
|
462  | 
val thm2 = make_cnfx_disj_thm x' (betapply (y', var)) (* (x' | y') = body' *)  | 
|
| 
59621
 
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
 
wenzelm 
parents: 
59582 
diff
changeset
 | 
463  | 
val thm3 = Thm.forall_intr (Thm.global_cterm_of thy var) thm2 (* !!v. (x' | y') = body' *)  | 
| 41447 | 464  | 
val thm4 = Thm.strip_shyps (thm3 COMP allI) (* ALL v. (x' | y') = body' *)  | 
465  | 
val thm5 = Thm.strip_shyps (thm4 RS make_cnfx_ex_cong) (* (EX v. (x' | y')) = (EX v. body') *)  | 
|
466  | 
in  | 
|
467  | 
iff_trans OF [thm1, thm5] (* (x' | (Ex y')) = (EX v. body') *)  | 
|
468  | 
end  | 
|
469  | 
| make_cnfx_disj_thm x' y' =  | 
|
470  | 
inst_thm thy [HOLogic.mk_disj (x', y')] iff_refl (* (x' | y') = (x' | y') *)  | 
|
471  | 
val thm1 = make_cnfx_thm_from_nnf x  | 
|
472  | 
val thm2 = make_cnfx_thm_from_nnf y  | 
|
| 59582 | 473  | 
val x' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm1  | 
474  | 
val y' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm2  | 
|
| 41447 | 475  | 
val disj_thm = disj_cong OF [thm1, thm2] (* (x | y) = (x' | y') *)  | 
476  | 
in  | 
|
477  | 
iff_trans OF [disj_thm, make_cnfx_disj_thm x' y']  | 
|
478  | 
end  | 
|
479  | 
else  | 
|
480  | 
let (* neither 'x' nor 'y' is a literal: introduce a fresh variable *)  | 
|
481  | 
val thm1 = inst_thm thy [x, y] make_cnfx_newlit (* (x | y) = EX v. (x | v) & (y | ~v) *)  | 
|
482  | 
val var = new_free ()  | 
|
483  | 
val body = HOLogic.mk_conj (HOLogic.mk_disj (x, var), HOLogic.mk_disj (y, HOLogic.Not $ var))  | 
|
484  | 
val thm2 = make_cnfx_thm_from_nnf body (* (x | v) & (y | ~v) = body' *)  | 
|
| 
59621
 
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
 
wenzelm 
parents: 
59582 
diff
changeset
 | 
485  | 
val thm3 = Thm.forall_intr (Thm.global_cterm_of thy var) thm2 (* !!v. (x | v) & (y | ~v) = body' *)  | 
| 41447 | 486  | 
val thm4 = Thm.strip_shyps (thm3 COMP allI) (* ALL v. (x | v) & (y | ~v) = body' *)  | 
487  | 
val thm5 = Thm.strip_shyps (thm4 RS make_cnfx_ex_cong) (* (EX v. (x | v) & (y | ~v)) = (EX v. body') *)  | 
|
488  | 
in  | 
|
489  | 
iff_trans OF [thm1, thm5]  | 
|
490  | 
end  | 
|
491  | 
| make_cnfx_thm_from_nnf t = inst_thm thy [t] iff_refl  | 
|
492  | 
(* convert 't' to NNF first *)  | 
|
| 42335 | 493  | 
val nnf_thm = make_nnf_thm_under_quantifiers ctxt t  | 
494  | 
(* ###  | 
|
| 41447 | 495  | 
val nnf_thm = make_nnf_thm thy t  | 
| 42335 | 496  | 
*)  | 
| 59582 | 497  | 
val nnf = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) nnf_thm  | 
| 41447 | 498  | 
(* then simplify wrt. True/False (this should preserve NNF) *)  | 
499  | 
val simp_thm = simp_True_False_thm thy nnf  | 
|
| 59582 | 500  | 
val simp = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) simp_thm  | 
| 41447 | 501  | 
(* initialize var_id, in case the term already contains variables of the form "cnfx_<int>" *)  | 
502  | 
val _ = (var_id := fold (fn free => fn max =>  | 
|
503  | 
let  | 
|
504  | 
val (name, _) = dest_Free free  | 
|
505  | 
val idx =  | 
|
506  | 
if String.isPrefix "cnfx_" name then  | 
|
507  | 
(Int.fromString o String.extract) (name, String.size "cnfx_", NONE)  | 
|
508  | 
else  | 
|
509  | 
NONE  | 
|
510  | 
in  | 
|
511  | 
Int.max (max, the_default 0 idx)  | 
|
| 44121 | 512  | 
end) (Misc_Legacy.term_frees simp) 0)  | 
| 41447 | 513  | 
(* finally, convert to definitional CNF (this should preserve the simplification) *)  | 
| 42335 | 514  | 
val cnfx_thm = make_under_quantifiers ctxt make_cnfx_thm_from_nnf simp  | 
515  | 
(*###  | 
|
| 41447 | 516  | 
val cnfx_thm = make_cnfx_thm_from_nnf simp  | 
| 42335 | 517  | 
*)  | 
| 41447 | 518  | 
in  | 
519  | 
iff_trans OF [iff_trans OF [nnf_thm, simp_thm], cnfx_thm]  | 
|
520  | 
end;  | 
|
| 17618 | 521  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
522  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
523  | 
(* Tactics *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
524  | 
(* ------------------------------------------------------------------------- *)  | 
| 17618 | 525  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
526  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
527  | 
(* weakening_tac: removes the first hypothesis of the 'i'-th subgoal *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
528  | 
(* ------------------------------------------------------------------------- *)  | 
| 17618 | 529  | 
|
| 
58963
 
26bf09b95dda
proper context for assume_tac (atac remains as fall-back without context);
 
wenzelm 
parents: 
58839 
diff
changeset
 | 
530  | 
fun weakening_tac ctxt i =  | 
| 
59498
 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 
wenzelm 
parents: 
58963 
diff
changeset
 | 
531  | 
dresolve_tac ctxt [weakening_thm] i THEN assume_tac ctxt (i+1);  | 
| 17618 | 532  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
533  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
534  | 
(* cnf_rewrite_tac: converts all premises of the 'i'-th subgoal to CNF *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
535  | 
(* (possibly causing an exponential blowup in the length of each *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
536  | 
(* premise) *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
537  | 
(* ------------------------------------------------------------------------- *)  | 
| 17618 | 538  | 
|
| 32232 | 539  | 
fun cnf_rewrite_tac ctxt i =  | 
| 41447 | 540  | 
(* cut the CNF formulas as new premises *)  | 
| 60696 | 541  | 
  Subgoal.FOCUS (fn {prems, context = ctxt', ...} =>
 | 
| 41447 | 542  | 
let  | 
| 60696 | 543  | 
val cnf_thms = map (make_cnf_thm ctxt' o HOLogic.dest_Trueprop o Thm.prop_of) prems  | 
| 41447 | 544  | 
val cut_thms = map (fn (th, pr) => cnftac_eq_imp OF [th, pr]) (cnf_thms ~~ prems)  | 
545  | 
in  | 
|
546  | 
cut_facts_tac cut_thms 1  | 
|
547  | 
end) ctxt i  | 
|
548  | 
(* remove the original premises *)  | 
|
549  | 
THEN SELECT_GOAL (fn thm =>  | 
|
550  | 
let  | 
|
| 59582 | 551  | 
val n = Logic.count_prems ((Term.strip_all_body o fst o Logic.dest_implies o Thm.prop_of) thm)  | 
| 41447 | 552  | 
in  | 
| 
58963
 
26bf09b95dda
proper context for assume_tac (atac remains as fall-back without context);
 
wenzelm 
parents: 
58839 
diff
changeset
 | 
553  | 
PRIMITIVE (funpow (n div 2) (Seq.hd o weakening_tac ctxt 1)) thm  | 
| 41447 | 554  | 
end) i;  | 
| 17618 | 555  | 
|
| 
17809
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
556  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
557  | 
(* cnfx_rewrite_tac: converts all premises of the 'i'-th subgoal to CNF *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
558  | 
(* (possibly introducing new literals) *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
559  | 
(* ------------------------------------------------------------------------- *)  | 
| 
 
195045659c06
Tactics sat and satx reimplemented, several improvements
 
webertj 
parents: 
17618 
diff
changeset
 | 
560  | 
|
| 32232 | 561  | 
fun cnfx_rewrite_tac ctxt i =  | 
| 41447 | 562  | 
(* cut the CNF formulas as new premises *)  | 
| 60696 | 563  | 
  Subgoal.FOCUS (fn {prems, context = ctxt', ...} =>
 | 
| 41447 | 564  | 
let  | 
| 60696 | 565  | 
val cnfx_thms = map (make_cnfx_thm ctxt' o HOLogic.dest_Trueprop o Thm.prop_of) prems  | 
| 41447 | 566  | 
val cut_thms = map (fn (th, pr) => cnftac_eq_imp OF [th, pr]) (cnfx_thms ~~ prems)  | 
567  | 
in  | 
|
568  | 
cut_facts_tac cut_thms 1  | 
|
569  | 
end) ctxt i  | 
|
570  | 
(* remove the original premises *)  | 
|
571  | 
THEN SELECT_GOAL (fn thm =>  | 
|
572  | 
let  | 
|
| 59582 | 573  | 
val n = Logic.count_prems ((Term.strip_all_body o fst o Logic.dest_implies o Thm.prop_of) thm)  | 
| 41447 | 574  | 
in  | 
| 
58963
 
26bf09b95dda
proper context for assume_tac (atac remains as fall-back without context);
 
wenzelm 
parents: 
58839 
diff
changeset
 | 
575  | 
PRIMITIVE (funpow (n div 2) (Seq.hd o weakening_tac ctxt 1)) thm  | 
| 41447 | 576  | 
end) i;  | 
| 17618 | 577  | 
|
| 41447 | 578  | 
end;  |