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