src/HOL/Tools/cnf_funcs.ML
author webertj
Fri, 23 Sep 2005 22:58:50 +0200
changeset 17618 1330157e156a
child 17809 195045659c06
permissions -rw-r--r--
new sat tactic imports resolution proofs from zChaff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17618
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
     1
(*  Title:      HOL/Tools/cnf_funcs.ML
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
     2
    ID:         $Id$
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
     3
    Author:     Alwen Tiu, QSL Team, LORIA (http://qsl.loria.fr)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
     4
    Copyright   2005
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
     5
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
     6
  Description:
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
     7
  This file contains functions and tactics to transform a formula into 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
     8
  Conjunctive Normal Forms (CNF). 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
     9
  A formula in CNF is of the following form:
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    10
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    11
      (x11 | x12 | .. x1m) & ... & (xm1 | xm2 | ... | xmn)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    12
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    13
  where each xij is a literal (i.e., positive or negative propositional
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    14
  variables).
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    15
  This kind of formula will simply be referred to as CNF.
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    16
  A disjunction of literals is referred to as "clause".
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    17
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    18
  For the purpose of SAT proof reconstruction, we also make use of another
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    19
  representation of clauses, which we call the "raw clauses".
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    20
  Raw clauses are of the form
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    21
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    22
      (x1 ==> x2 ==> .. ==> xn ==> False)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    23
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    24
  where each xi is a literal. Note that the above raw clause corresponds
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    25
  to the clause (x1' | ... | xn'), where each xi' is the negation normal
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    26
  form of ~xi.
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    27
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    28
  Notes for current revision:
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    29
  - the "definitional CNF transformation" (anything with prefix cnfx_ )
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    30
    introduces new literals of the form (lit_i) where i is an integer.
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    31
    For these functions to work, it is necessary that no free variables
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    32
    which names are of the form lit_i appears in the formula being
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    33
    transformed.
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    34
*)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    35
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    36
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    37
(***************************************************************************)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    38
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    39
signature CNF =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    40
sig
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    41
  val cnf_tac : Tactical.tactic
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    42
  val cnf_thin_tac : Tactical.tactic
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    43
  val cnfx_thin_tac : Tactical.tactic
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    44
  val cnf_concl_tac : Tactical.tactic
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    45
  val weakening_tac : int -> Tactical.tactic
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    46
  val mk_cnf_thm : Sign.sg -> Term.term -> Thm.thm
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    47
  val mk_cnfx_thm : Sign.sg -> Term.term ->  Thm.thm
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    48
  val is_atm : Term.term -> bool
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    49
  val is_lit : Term.term -> bool
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    50
  val is_clause : Term.term -> bool
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    51
  val is_raw_clause : Term.term -> bool
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    52
  val cnf2raw_thm : Thm.thm -> Thm.thm
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    53
  val cnf2raw_thms : Thm.thm list -> Thm.thm list
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    54
  val cnf2prop : Thm.thm list -> (PropLogic.prop_formula * ((Term.term * int) list))
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    55
end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    56
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    57
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    58
(***************************************************************************)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    59
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    60
structure cnf : CNF =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    61
struct
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    62
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    63
val cur_thy = the_context();
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    64
val mk_disj = HOLogic.mk_disj;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    65
val mk_conj = HOLogic.mk_conj;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    66
val mk_imp  = HOLogic.mk_imp;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    67
val Not = HOLogic.Not;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    68
val false_const = HOLogic.false_const;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    69
val true_const = HOLogic.true_const;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    70
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    71
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    72
(* Index for new literals *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    73
val lit_id = ref 0;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    74
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    75
fun thm_by_auto G =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    76
    prove_goal cur_thy G (fn prems => [cut_facts_tac prems 1, Auto_tac]);
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    77
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    78
(***************************************************************************)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    79
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    80
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    81
val cnf_eq_id = thm_by_auto "(P :: bool) = P";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    82
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    83
val cnf_eq_sym = thm_by_auto "(P :: bool) = Q ==> Q = P";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    84
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    85
val cnf_not_true_false = thm_by_auto "~True = False";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    86
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    87
val cnf_not_false_true = thm_by_auto "~False = True";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    88
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    89
val cnf_imp2disj = thm_by_auto "(P --> Q) = (~P | Q)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    90
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    91
val cnf_neg_conj = thm_by_auto "(~(P & Q)) = (~P | ~Q)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    92
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    93
val cnf_neg_disj = thm_by_auto "(~(P | Q)) = (~P & ~Q)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    94
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    95
val cnf_neg_imp = thm_by_auto "(~(P --> Q)) = (P & ~Q)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    96
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    97
val cnf_double_neg = thm_by_auto "(~~P) = P";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    98
 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
    99
val cnf_disj_conj = thm_by_auto "((P & Q) | R) = ((P | R) & (Q | R))";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   100
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   101
val cnf_disj_imp = thm_by_auto "((P --> Q) | R) = (~P | (Q | R))";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   102
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   103
val cnf_disj_disj = thm_by_auto "((P | Q) | R) = (P | (Q | R))";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   104
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   105
val cnf_disj_false = thm_by_auto "(False | P) = P";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   106
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   107
val cnf_disj_true = thm_by_auto "(True | P) = True";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   108
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   109
val cnf_disj_not_false = thm_by_auto "(~False | P) = True";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   110
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   111
val cnf_disj_not_true = thm_by_auto "(~True | P) = P";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   112
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   113
val cnf_eq_trans = thm_by_auto "[| ( (P::bool) = Q) ; Q = R |] ==> (P = R)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   114
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   115
val cnf_comb2eq = thm_by_auto "[| P = Q ; R = T |] ==> (P & R) = (Q & T)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   116
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   117
val cnf_disj_sym = thm_by_auto "(P | Q) = (Q | P)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   118
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   119
val cnf_cong_disj = thm_by_auto "(P = Q) ==> (P | R) = (Q | R)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   120
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   121
val icnf_elim_disj1 = thm_by_auto "Q = R  ==> (~P | Q) = (P --> R)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   122
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   123
val icnf_elim_disj2 = thm_by_auto "Q = R ==> (P | Q) = (~P --> R)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   124
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   125
val icnf_neg_false1 = thm_by_auto "(~P) = (P --> False)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   126
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   127
val icnf_neg_false2 = thm_by_auto "P = (~P --> False)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   128
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   129
val weakening_thm = thm_by_auto "[| P ; Q |] ==> Q";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   130
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   131
val cnf_newlit = thm_by_auto 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   132
    "((P & Q) | R) = (EX (x :: bool). (~x | P) & (~x | Q) & (x | ~P | ~ Q) & (x | R))";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   133
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   134
val cnf_all_ex = thm_by_auto 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   135
    "(ALL (x :: bool). (P x = Q x)) ==> (EX x. P x) = (EX x. Q x)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   136
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   137
(* [| P ; ~P |] ==> False *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   138
val cnf_notE = read_instantiate [("R", "False")] (rotate_prems 1 notE);
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   139
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   140
val cnf_dneg = thm_by_auto "P ==> ~~P";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   141
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   142
val cnf_neg_disjI = thm_by_auto "[| ~P ; ~Q |] ==> ~(P | Q)";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   143
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   144
val cnf_eq_imp = thm_by_auto "[|((P::bool) = Q) ; P |] ==> Q";
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   145
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   146
(***************************************************************************)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   147
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   148
fun is_atm (Const("Trueprop",_) $ x) = is_atm x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   149
  | is_atm (Const("==>",_) $ x $ y) = false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   150
  | is_atm (Const("False",_)) = false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   151
  | is_atm (Const("True", _)) = false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   152
  | is_atm (Const("op &",_) $ x $ y) = false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   153
  | is_atm (Const("op |",_) $ x $ y) = false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   154
  | is_atm (Const("op -->",_) $ x $ y) = false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   155
  | is_atm (Const("Not",_) $ x) = false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   156
  | is_atm t = true
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   157
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   158
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   159
fun is_lit (Const("Trueprop",_) $ x) = is_lit x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   160
  | is_lit (Const("Not", _) $ x) = is_atm x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   161
  | is_lit t = is_atm t
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   162
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   163
fun is_clause (Const("Trueprop",_) $ x) = is_clause x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   164
  | is_clause (Const("op |", _) $ x $ y) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   165
          (is_clause x) andalso (is_clause y)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   166
  | is_clause t = is_lit t
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   167
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   168
fun is_cnf (Const("Trueprop", _) $ x) = is_cnf x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   169
  | is_cnf (Const("op &",_) $ x $ y) = (is_cnf x) andalso (is_cnf y)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   170
  | is_cnf t = is_clause t
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   171
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   172
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   173
(* Checking for raw clauses *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   174
fun is_raw_clause (Const("Trueprop",_) $ x) = is_raw_clause x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   175
  | is_raw_clause (Const("==>",_) $ x $ 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   176
                   (Const("Trueprop",_) $ Const("False",_))) = is_lit x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   177
  | is_raw_clause (Const("==>",_) $ x $ y) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   178
        (is_lit x) andalso (is_raw_clause y)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   179
  | is_raw_clause t = false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   180
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   181
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   182
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   183
(* Translate a CNF clause into a raw clause *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   184
fun cnf2raw_thm c =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   185
let val nc = c RS cnf_notE
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   186
in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   187
rule_by_tactic (REPEAT_SOME (fn i => 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   188
               rtac cnf_dneg i 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   189
               ORELSE rtac cnf_neg_disjI i)) nc
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   190
handle THM _ => nc
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   191
end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   192
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   193
fun cnf2raw_thms nil = nil
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   194
  | cnf2raw_thms (c::l) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   195
    let val t = term_of (cprop_of c)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   196
    in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   197
       if (is_clause t) then (cnf2raw_thm c) :: cnf2raw_thms l
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   198
       else cnf2raw_thms l
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   199
    end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   200
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   201
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   202
(* Translating HOL formula (in CNF) to PropLogic formula. Returns also an
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   203
   association list, relating literals to their indices *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   204
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   205
local
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   206
  (* maps atomic formulas to variable numbers *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   207
  val dictionary : ((Term.term * int) list) ref = ref nil;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   208
  val var_count = ref 0;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   209
  val pAnd = PropLogic.And;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   210
  val pOr = PropLogic.Or;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   211
  val pNot = PropLogic.Not;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   212
  val pFalse = PropLogic.False;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   213
  val pTrue = PropLogic.True;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   214
  val pVar = PropLogic.BoolVar;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   215
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   216
  fun mk_clause (Const("Trueprop",_) $ x) = mk_clause x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   217
    | mk_clause (Const("op |",_) $ x $ y) = pOr(mk_clause x, mk_clause y)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   218
    | mk_clause (Const("Not", _) $ x) = pNot (mk_clause x)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   219
    | mk_clause (Const("True",_)) = pTrue
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   220
    | mk_clause (Const("False",_)) = pFalse
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   221
    | mk_clause t =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   222
      let
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   223
         val idx = AList.lookup op= (!dictionary) t
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   224
      in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   225
         case idx of
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   226
            (SOME x) => pVar x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   227
           | NONE =>
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   228
             let
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   229
                val new_var = inc var_count
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   230
             in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   231
                dictionary := (t, new_var) :: (!dictionary);
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   232
                pVar new_var
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   233
             end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   234
      end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   235
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   236
   fun mk_clauses nil = pTrue
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   237
     | mk_clauses (x::nil) = mk_clause x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   238
     | mk_clauses (x::l) = pAnd(mk_clause x, mk_clauses l)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   239
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   240
in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   241
   fun cnf2prop thms =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   242
   (
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   243
     var_count := 0;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   244
     dictionary := nil;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   245
     (mk_clauses (map (fn x => term_of (cprop_of x)) thms), !dictionary)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   246
   )
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   247
end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   248
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   249
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   250
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   251
(* Instantiate a theorem with a list of terms *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   252
fun inst_thm sign l thm = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   253
  instantiate' [] (map (fn x => SOME (cterm_of sign x)) l) thm
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   254
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   255
(* Tactic to remove the first hypothesis of the first subgoal. *) 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   256
fun weakening_tac i = (dtac weakening_thm i) THEN (atac (i+1));
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   257
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   258
(* Tactic for removing the n first hypotheses of the first subgoal. *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   259
fun weakenings_tac 0 state = all_tac state
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   260
  | weakenings_tac n state = ((weakening_tac  1) THEN (weakenings_tac (n-1))) state
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   261
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   262
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   263
(* 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   264
  Transform a formula into a "head" negation normal form, that is, 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   265
  the top level connective is not a negation, with the exception
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   266
  of negative literals. Returns the pair of the head normal term with
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   267
  the theorem corresponding to the transformation.
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   268
*)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   269
fun head_nnf sign (Const("Not",_)  $ (Const("op &",_) $ x $ y)) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   270
    let val t = mk_disj(Not $ x, Not $ y)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   271
        val neg_thm = inst_thm sign [x, y] cnf_neg_conj 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   272
    in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   273
        (t, neg_thm)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   274
    end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   275
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   276
  | head_nnf sign (Const("Not", _) $ (Const("op |",_) $ x $ y)) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   277
    let val t = mk_conj(Not $ x, Not $ y)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   278
        val neg_thm =  inst_thm sign [x, y] cnf_neg_disj; 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   279
    in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   280
        (t, neg_thm)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   281
    end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   282
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   283
  | head_nnf sign (Const("Not", _) $ (Const("op -->",_) $ x $ y)) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   284
    let val t = mk_conj(x, Not $ y)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   285
        val neg_thm = inst_thm sign [x, y] cnf_neg_imp
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   286
    in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   287
        (t, neg_thm)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   288
    end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   289
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   290
  | head_nnf sign (Const("Not",_) $ (Const("Not",_) $ x)) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   291
    (x, inst_thm sign [x] cnf_double_neg)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   292
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   293
  | head_nnf sign (Const("Not",_) $ Const("True",_)) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   294
      (false_const, cnf_not_true_false)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   295
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   296
  | head_nnf sign (Const("Not",_) $ Const("False",_)) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   297
      (true_const, cnf_not_false_true)  
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   298
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   299
  | head_nnf sign t = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   300
    (t, inst_thm sign [t] cnf_eq_id)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   301
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   302
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   303
(***************************************************************************)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   304
(*                  Tactics for simple CNF transformation                  *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   305
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   306
(* A naive procedure for CNF transformation:
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   307
   Given any t, produce a theorem t = t', where t' is in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   308
   conjunction normal form 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   309
*)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   310
fun mk_cnf_thm sign (Const("Trueprop",_) $ x) = mk_cnf_thm sign x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   311
  | mk_cnf_thm sign (t as (Const(_,_))) = inst_thm sign [t] cnf_eq_id
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   312
  | mk_cnf_thm sign (t as (Free(_,_))) =  inst_thm sign [t] cnf_eq_id
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   313
 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   314
  | mk_cnf_thm sign (Const("op -->", _) $ x $ y) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   315
       let val thm1 = inst_thm sign [x, y] cnf_imp2disj;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   316
           val thm2 = mk_cnf_thm sign (mk_disj(Not $ x, y));
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   317
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   318
           cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   319
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   320
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   321
  | mk_cnf_thm sign (Const("op &", _) $ x $ y) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   322
       let val cnf1 = mk_cnf_thm sign x;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   323
           val cnf2 = mk_cnf_thm sign y;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   324
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   325
           cnf_comb2eq OF [cnf1, cnf2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   326
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   327
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   328
  | mk_cnf_thm sign (Const("Not",_) $ Const("True",_)) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   329
        cnf_not_true_false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   330
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   331
  | mk_cnf_thm sign (Const("Not",_) $ Const("False",_)) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   332
        cnf_not_false_true
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   333
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   334
  | mk_cnf_thm sign (t as (Const("Not", _) $ x)) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   335
      ( 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   336
       if (is_atm x) then inst_thm sign [t] cnf_eq_id
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   337
       else
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   338
         let val (t1, hn_thm) = head_nnf sign t
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   339
             val cnf_thm = mk_cnf_thm sign t1
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   340
         in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   341
             cnf_eq_trans OF [hn_thm, cnf_thm]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   342
         end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   343
       ) 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   344
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   345
  | mk_cnf_thm sign (Const("op |",_) $ (Const("op &", _) $ p $ q) $ r) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   346
       let val thm1 = inst_thm sign [p, q, r] cnf_disj_conj;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   347
           val thm2 = mk_cnf_thm sign (mk_conj(mk_disj(p, r), mk_disj(q,r)));
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   348
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   349
          cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   350
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   351
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   352
  | mk_cnf_thm sign (Const("op |",_) $ (Const("op |", _) $ p $ q) $ r) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   353
       let val thm1 = inst_thm sign [p,q,r] cnf_disj_disj;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   354
           val thm2 = mk_cnf_thm sign (mk_disj(p, mk_disj(q,r)));
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   355
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   356
          cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   357
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   358
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   359
  | mk_cnf_thm sign (Const("op |",_) $ (Const("op -->", _) $ p $ q) $ r) =                       
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   360
       let val thm1 = inst_thm sign [p,q,r] cnf_disj_imp;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   361
           val thm2 = mk_cnf_thm sign (mk_disj(Not $ p, mk_disj(q, r)));
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   362
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   363
           cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   364
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   365
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   366
  | mk_cnf_thm sign (Const("op |",_) $ Const("False",_) $ p) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   367
       let val thm1 = inst_thm sign [p] cnf_disj_false;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   368
           val thm2 = mk_cnf_thm sign p
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   369
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   370
           cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   371
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   372
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   373
  | mk_cnf_thm sign (Const("op |",_) $ Const("True",_) $ p) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   374
       inst_thm sign [p] cnf_disj_true
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   375
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   376
  | mk_cnf_thm sign (Const("op |",_) $ (Const("Not",_) $ Const("True",_)) $ p) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   377
       let val thm1 = inst_thm sign [p] cnf_disj_not_true;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   378
           val thm2 = mk_cnf_thm sign p
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   379
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   380
           cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   381
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   382
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   383
  | mk_cnf_thm sign (Const("op |",_) $ (Const("Not",_) $ Const("False",_)) $ p) =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   384
       inst_thm sign [p] cnf_disj_not_false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   385
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   386
  | mk_cnf_thm sign (t as (Const("op |",_) $ p $ q)) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   387
       if (is_lit p) then
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   388
          (
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   389
            if (is_clause t) then inst_thm sign [t] cnf_eq_id
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   390
            else 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   391
             let val thm1 = inst_thm sign [p, q] cnf_disj_sym;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   392
                 val thm2 = mk_cnf_thm sign (mk_disj(q, p))
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   393
             in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   394
                cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   395
             end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   396
          )
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   397
       else 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   398
            let val (u, thm1) = head_nnf sign p;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   399
                val thm2 = inst_thm sign [p,u,q] cnf_cong_disj;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   400
                val thm3 = mk_cnf_thm sign (mk_disj(u, q))
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   401
            in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   402
                cnf_eq_trans OF [(thm1 RS thm2), thm3]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   403
            end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   404
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   405
 | mk_cnf_thm sign t = inst_thm sign [t] cnf_eq_id
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   406
    (* error ("I don't know how to handle the formula " ^ 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   407
                          (Sign.string_of_term sign t))
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   408
    *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   409
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   410
fun term_of_thm c = term_of (cprop_of c)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   411
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   412
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   413
(* Transform a given list of theorems (thms) into CNF *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   414
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   415
fun mk_cnf_thms sg nil = nil
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   416
  | mk_cnf_thms sg (x::l) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   417
    let val t = term_of_thm x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   418
    in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   419
      if (is_clause t) then x :: mk_cnf_thms sg l
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   420
      else 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   421
       let val thm1 = mk_cnf_thm sg t
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   422
           val thm2 = cnf_eq_imp OF [thm1, x]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   423
       in 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   424
           thm2 :: mk_cnf_thms sg l
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   425
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   426
    end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   427
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   428
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   429
(* Count the number of hypotheses in a formula *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   430
fun num_of_hyps (Const("Trueprop", _) $ x) = num_of_hyps x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   431
  | num_of_hyps (Const("==>",_) $ x $ y) = 1 + (num_of_hyps y)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   432
  | num_of_hyps t = 0
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   433
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   434
(* Tactic for converting to CNF (in primitive form): 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   435
   it takes the first subgoal of the proof state, transform all its
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   436
   hypotheses into CNF (in primivite form) and remove the original 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   437
   hypotheses.
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   438
*)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   439
fun cnf_thin_tac state =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   440
let val sg = Thm.sign_of_thm state
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   441
in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   442
case (prems_of state) of 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   443
  [] => Seq.empty
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   444
| (subgoal::_) => 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   445
  let 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   446
    val n = num_of_hyps (strip_all_body subgoal);
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   447
    val tac1 = METAHYPS (fn l => cut_facts_tac (mk_cnf_thms sg l) 1) 1
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   448
  in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   449
    (tac1 THEN weakenings_tac n THEN (REPEAT (etac conjE 1)) ) state
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   450
  end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   451
end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   452
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   453
(* Tactic for converting to CNF (in primitive form), keeping 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   454
   the original hypotheses. *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   455
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   456
fun cnf_tac state =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   457
let val sg = Thm.sign_of_thm state
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   458
in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   459
case (prems_of state) of 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   460
  [] => Seq.empty
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   461
| (subgoal::_) => 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   462
   METAHYPS (fn l => cut_facts_tac (mk_cnf_thms sg l) 1 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   463
                    THEN (REPEAT (etac conjE 1)) ) 1 state
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   464
end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   465
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   466
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   467
(***************************************************************************)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   468
(*            CNF transformation by introducing new literals               *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   469
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   470
(*** IMPORTANT: 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   471
  This transformation uses variables of the form "lit_i", where i is a natural
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   472
  number. For the transformation to work, these variables must not already
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   473
  occur freely in the formula being transformed.
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   474
***)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   475
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   476
fun ext_conj x p q r =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   477
   mk_conj(
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   478
    mk_disj(Not $ x, p),
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   479
    mk_conj(
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   480
      mk_disj(Not $ x, q),
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   481
      mk_conj(
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   482
        mk_disj(x, mk_disj(Not $ p, Not $ q)),
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   483
        mk_disj(x, r)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   484
      )
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   485
    )
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   486
   )
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   487
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   488
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   489
(* Transform to CNF in primitive forms, possibly introduce extra variables *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   490
fun mk_cnfx_thm sign (Const("Trueprop",_) $ x) = mk_cnfx_thm sign x 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   491
  | mk_cnfx_thm sign (t as (Const(_,_)))  = inst_thm sign [t] cnf_eq_id
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   492
  | mk_cnfx_thm sign (t as (Free(_,_)))  = inst_thm sign [t] cnf_eq_id
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   493
  | mk_cnfx_thm sign (Const("op -->", _) $ x $ y)  =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   494
       let val thm1 = inst_thm sign [x, y] cnf_imp2disj;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   495
           val thm2 = mk_cnfx_thm sign (mk_disj(Not $ x, y)) 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   496
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   497
           cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   498
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   499
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   500
  | mk_cnfx_thm sign (Const("op &", _) $ x $ y)  = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   501
       let val cnf1 = mk_cnfx_thm sign x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   502
           val cnf2 = mk_cnfx_thm sign y
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   503
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   504
           cnf_comb2eq OF [cnf1, cnf2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   505
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   506
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   507
  | mk_cnfx_thm sign (Const("Not",_) $ Const("True",_)) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   508
        cnf_not_true_false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   509
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   510
  | mk_cnfx_thm sign (Const("Not",_) $ Const("False",_))  = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   511
        cnf_not_false_true
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   512
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   513
  | mk_cnfx_thm sign (t as (Const("Not", _) $ x))  =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   514
      ( 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   515
       if (is_atm x) then inst_thm sign [t] cnf_eq_id
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   516
       else
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   517
         let val (t1, hn_thm) = head_nnf sign t
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   518
             val cnf_thm = mk_cnfx_thm sign t1 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   519
         in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   520
             cnf_eq_trans OF [hn_thm, cnf_thm]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   521
         end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   522
       ) 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   523
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   524
  | mk_cnfx_thm sign (Const("op |",_) $ (Const("op &", _) $ p $ q) $ r)  =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   525
      if (is_lit r) then
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   526
        let val thm1 = inst_thm sign [p, q, r] cnf_disj_conj
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   527
            val thm2 = mk_cnfx_thm sign (mk_conj(mk_disj(p, r), mk_disj(q,r)))
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   528
        in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   529
           cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   530
        end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   531
      else cnfx_newlit sign p q r 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   532
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   533
  | mk_cnfx_thm sign (Const("op |",_) $ (Const("op |", _) $ p $ q) $ r)  =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   534
       let val thm1 = inst_thm sign [p,q,r] cnf_disj_disj
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   535
           val thm2 = mk_cnfx_thm sign (mk_disj(p, mk_disj(q,r))) 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   536
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   537
          cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   538
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   539
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   540
  | mk_cnfx_thm sign (Const("op |",_) $ (Const("op -->", _) $ p $ q) $ r) =                       
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   541
       let val thm1 = inst_thm sign [p,q,r] cnf_disj_imp
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   542
           val thm2 = mk_cnfx_thm sign (mk_disj(Not $ p, mk_disj(q, r))) 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   543
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   544
           cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   545
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   546
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   547
  | mk_cnfx_thm sign (Const("op |",_) $ Const("False",_) $ p)  =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   548
       let val thm1 = inst_thm sign [p] cnf_disj_false;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   549
           val thm2 = mk_cnfx_thm sign p 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   550
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   551
           cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   552
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   553
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   554
  | mk_cnfx_thm sign (Const("op |",_) $ Const("True",_) $ p)  =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   555
       inst_thm sign [p] cnf_disj_true
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   556
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   557
  | mk_cnfx_thm sign (Const("op |",_) $ (Const("Not",_) $ Const("True",_)) $ p)  =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   558
       let val thm1 = inst_thm sign [p] cnf_disj_not_true;
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   559
           val thm2 = mk_cnfx_thm sign p 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   560
       in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   561
           cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   562
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   563
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   564
  | mk_cnfx_thm sign (Const("op |",_) $ (Const("Not",_) $ Const("False",_)) $ p)  =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   565
       inst_thm sign [p] cnf_disj_not_false
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   566
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   567
  | mk_cnfx_thm sign (t as (Const("op |",_) $ p $ q))  = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   568
       if (is_lit p) then
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   569
          (
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   570
            if (is_clause t) then inst_thm sign [t] cnf_eq_id
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   571
            else 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   572
             let val thm1 = inst_thm sign [p, q] cnf_disj_sym
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   573
                 val thm2 = mk_cnfx_thm sign (mk_disj(q, p)) 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   574
             in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   575
                cnf_eq_trans OF [thm1, thm2]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   576
             end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   577
          )
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   578
       else 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   579
            let val (u, thm1) = head_nnf sign p
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   580
                val thm2 = inst_thm sign [p,u,q] cnf_cong_disj
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   581
                val thm3 = mk_cnfx_thm sign (mk_disj(u, q)) 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   582
            in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   583
                cnf_eq_trans OF [(thm1 RS thm2), thm3]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   584
            end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   585
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   586
 | mk_cnfx_thm sign t  = error ("I don't know how to handle the formula " ^ 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   587
                          (Sign.string_of_term sign t))
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   588
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   589
and cnfx_newlit sign p q r  = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   590
   let val lit = read ("lit_" ^ (Int.toString (!lit_id)) ^ " :: bool")
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   591
       val _ = (lit_id := !lit_id + 1)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   592
       val ct_lit = cterm_of sign lit
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   593
       val x_conj = ext_conj lit p q r
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   594
       val thm1 = inst_thm sign [p,q,r] cnf_newlit
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   595
       val thm2 = mk_cnfx_thm sign x_conj 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   596
       val thm3 = forall_intr ct_lit thm2
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   597
       val thm4 = strip_shyps (thm3 COMP allI)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   598
       val thm5 = strip_shyps (thm4 RS cnf_all_ex)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   599
   in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   600
       cnf_eq_trans OF [thm1, thm5]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   601
   end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   602
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   603
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   604
(* Theorems for converting formula into CNF (in primitive form), with 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   605
   new extra variables *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   606
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   607
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   608
fun mk_cnfx_thms sg nil = nil
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   609
  | mk_cnfx_thms sg (x::l) = 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   610
    let val t = term_of_thm x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   611
    in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   612
      if (is_clause t) then x :: mk_cnfx_thms sg l
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   613
      else 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   614
       let val thm1 = mk_cnfx_thm sg t
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   615
           val thm2 = cnf_eq_imp OF [thm1,x]
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   616
       in 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   617
           thm2 :: mk_cnfx_thms sg l
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   618
       end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   619
    end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   620
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   621
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   622
(* Tactic for converting hypotheses into CNF, possibly
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   623
   introducing new variables *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   624
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   625
fun cnfx_thin_tac state =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   626
let val sg = Thm.sign_of_thm state
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   627
in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   628
case (prems_of state) of 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   629
  [] => Seq.empty
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   630
| (subgoal::_) => 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   631
   let val n = num_of_hyps (strip_all_body subgoal);
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   632
       val tac1 =  METAHYPS (fn l => cut_facts_tac (mk_cnfx_thms sg l) 1) 1
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   633
   in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   634
      EVERY [tac1, weakenings_tac n, 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   635
             REPEAT (etac conjE 1 ORELSE etac exE 1)] state
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   636
   end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   637
end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   638
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   639
(* Tactic for converting the conclusion of a goal into CNF *)
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   640
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   641
fun get_concl (Const("Trueprop", _) $ x) = get_concl x
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   642
  | get_concl (Const("==>",_) $ x $ y) = get_concl y
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   643
  | get_concl t = t
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   644
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   645
fun cnf_concl_tac' state =
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   646
case (prems_of state) of 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   647
  [] => Seq.empty
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   648
| (subgoal::_) =>
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   649
  let val sg = Thm.sign_of_thm state
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   650
      val c = get_concl subgoal
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   651
      val thm1 = (mk_cnf_thm sg c) RS cnf_eq_sym
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   652
      val thm2 = thm1 RS subst
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   653
  in
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   654
    rtac thm2 1 state
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   655
  end
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   656
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   657
val cnf_concl_tac  = METAHYPS (fn l => cnf_concl_tac') 1 
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   658
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   659
1330157e156a new sat tactic imports resolution proofs from zChaff
webertj
parents:
diff changeset
   660
end (*of structure*)