| author | nipkow | 
| Tue, 01 Mar 2005 18:48:52 +0100 | |
| changeset 15554 | 03d4347b071d | 
| parent 15531 | 08c8dad8e399 | 
| child 15579 | 32bee18c675f | 
| permissions | -rw-r--r-- | 
| 15151 | 1 | (* Title: HOL/Reconstruction.thy | 
| 2 | ID: $Id$ | |
| 3 | Author: Lawrence C Paulson and Claire Quigley | |
| 4 | Copyright 2004 University of Cambridge | |
| 5 | *) | |
| 6 | ||
| 7 | (*Attributes for reconstructing external resolution proofs*) | |
| 8 | ||
| 9 | structure Reconstruction = | |
| 10 | let open Attrib | |
| 11 | in | |
| 12 | struct | |
| 13 | ||
| 14 | (**************************************************************) | |
| 15 | (* extra functions necessary for factoring and paramodulation *) | |
| 16 | (**************************************************************) | |
| 17 | ||
| 18 | fun mksubstlist [] sublist = sublist | |
| 19 | | mksubstlist ((a,b)::rest) sublist = | |
| 20 | let val vartype = type_of b | |
| 21 | val avar = Var(a,vartype) | |
| 22 | val newlist = ((avar,b)::sublist) | |
| 23 | in mksubstlist rest newlist end; | |
| 24 | ||
| 25 | ||
| 26 | fun get_unif_comb t eqterm = | |
| 27 | if ((type_of t) = (type_of eqterm)) | |
| 28 | then t | |
| 29 | else | |
| 30 | let val _ $ rand = t | |
| 31 | in get_unif_comb rand eqterm end; | |
| 32 | ||
| 33 | fun get_unif_lit t eqterm = | |
| 34 | if (can HOLogic.dest_eq t) | |
| 35 | then | |
| 36 | let val (lhs,rhs) = HOLogic.dest_eq(HOLogic.dest_Trueprop eqterm) | |
| 37 | in lhs end | |
| 38 | else | |
| 39 | get_unif_comb t eqterm; | |
| 40 | ||
| 41 | ||
| 42 | ||
| 43 | (**** attributes ****) | |
| 44 | ||
| 45 | (** Binary resolution **) | |
| 46 | ||
| 47 | fun binary_rule ((cl1, lit1), (cl2 , lit2)) = | |
| 48 | select_literal (lit1 + 1) cl1 | |
| 49 | RSN ((lit2 + 1), cl2); | |
| 50 | ||
| 51 | fun binary_syntax ((i, B), j) (x, A) = (x, binary_rule ((A,i), (B,j))); | |
| 52 | ||
| 15384 | 53 | fun gen_binary thm = syntax | 
| 15151 | 54 | ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> binary_syntax); | 
| 15384 | 55 | val binary_global = gen_binary global_thm; | 
| 56 | val binary_local = gen_binary local_thm; | |
| 15151 | 57 | |
| 58 | (*I have not done the MRR rule because it seems to be identifical to | |
| 15384 | 59 | binary*) | 
| 15151 | 60 | |
| 61 | ||
| 62 | fun inst_single sign t1 t2 cl = | |
| 63 | let val ct1 = cterm_of sign t1 and ct2 = cterm_of sign t2 | |
| 64 | in hd (Seq.list_of(distinct_subgoals_tac | |
| 65 | (cterm_instantiate [(ct1,ct2)] cl))) | |
| 66 | end; | |
| 67 | ||
| 68 | fun inst_subst sign substs cl = | |
| 69 | if (is_Var (fst(hd(substs)))) | |
| 70 | then inst_single sign (fst (hd substs)) (snd (hd substs)) cl | |
| 71 | else if (is_Var (snd(hd(substs)))) | |
| 72 | then inst_single sign (snd (hd substs)) (fst (hd substs)) cl | |
| 73 |     else raise THM ("inst_subst", 0, [cl]);
 | |
| 74 | ||
| 75 | (*Grabs the environment from the result of Unify.unifiers*) | |
| 76 | fun getnewenv thisseq = fst (hd (Seq.list_of thisseq)); | |
| 77 | ||
| 78 | (** Factoring **) | |
| 79 | ||
| 80 | fun factor_rule (cl, lit1, lit2) = | |
| 81 | let | |
| 82 | val prems = prems_of cl | |
| 83 | val fac1 = List.nth (prems,lit1) | |
| 84 | val fac2 = List.nth (prems,lit2) | |
| 85 | val sign = sign_of_thm cl | |
| 86 | val unif_env = Unify.unifiers (sign, Envir.empty 0, [(fac1, fac2)]) | |
| 87 | val newenv = getnewenv unif_env | |
| 88 | val envlist = Envir.alist_of newenv | |
| 89 | in | |
| 90 | inst_subst sign (mksubstlist envlist []) cl | |
| 91 | end; | |
| 92 | ||
| 93 | fun factor_syntax (i, j) (x, A) = (x, factor_rule (A,i,j)); | |
| 94 | ||
| 15384 | 95 | fun factor x = syntax ((Scan.lift (Args.nat -- Args.nat)) >> factor_syntax) x; | 
| 15151 | 96 | |
| 97 | ||
| 98 | (** Paramodulation **) | |
| 99 | ||
| 15449 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 100 | (*subst with premises exchanged: that way, side literals of the equality will appear | 
| 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 101 | as the second to last premises of the result.*) | 
| 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 102 | val rev_subst = rotate_prems 1 subst; | 
| 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 103 | |
| 15499 | 104 | fun paramod_rule ((cl1, lit1), (cl2, lit2)) = | 
| 15449 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 105 | let val eq_lit_th = select_literal (lit1+1) cl1 | 
| 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 106 | val mod_lit_th = select_literal (lit2+1) cl2 | 
| 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 107 | val eqsubst = eq_lit_th RSN (2,rev_subst) | 
| 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 108 | val newth = Seq.hd (biresolution false [(false, mod_lit_th)] 1 eqsubst) | 
| 15499 | 109 | val newth' = Seq.hd (flexflex_rule newth) | 
| 110 | in negated_asm_of_head newth' end; | |
| 15151 | 111 | |
| 112 | ||
| 113 | fun paramod_syntax ((i, B), j) (x, A) = (x, paramod_rule ((A,i), (B,j))); | |
| 114 | ||
| 15384 | 115 | fun gen_paramod thm = syntax | 
| 15151 | 116 | ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> paramod_syntax); | 
| 15384 | 117 | val paramod_global = gen_paramod global_thm; | 
| 118 | val paramod_local = gen_paramod local_thm; | |
| 15151 | 119 | |
| 120 | ||
| 15449 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 121 | (** Demodulation: rewriting of a single literal (Non-Unit Rewriting, SPASS) **) | 
| 15151 | 122 | |
| 15449 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 123 | fun demod_rule ((cl1, lit1), (cl2 , lit2)) = | 
| 15151 | 124 | let val eq_lit_th = select_literal (lit1+1) cl1 | 
| 15449 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 125 | val mod_lit_th = select_literal (lit2+1) cl2 | 
| 15495 
50fde6f04e4c
new treatment of demodulation in proof reconstruction
 paulson parents: 
15466diff
changeset | 126 | val (fmod_th,thaw) = Drule.freeze_thaw_robust mod_lit_th | 
| 15449 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 127 | val eqsubst = eq_lit_th RSN (2,rev_subst) | 
| 15495 
50fde6f04e4c
new treatment of demodulation in proof reconstruction
 paulson parents: 
15466diff
changeset | 128 | val newth = Seq.hd(biresolution false [(false, fmod_th)] 1 eqsubst) | 
| 15499 | 129 | val offset = #maxidx(rep_thm newth) + 1 | 
| 130 | (*ensures "renaming apart" even when Vars are frozen*) | |
| 131 | in negated_asm_of_head (thaw offset newth) end; | |
| 15151 | 132 | |
| 15449 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 133 | fun demod_syntax ((i, B), j) (x, A) = (x, demod_rule ((A,i), (B,j))); | 
| 15151 | 134 | |
| 15449 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 135 | fun gen_demod thm = syntax | 
| 
a27c81bd838d
fixed the treatment of demodulation and paramodulation
 paulson parents: 
15384diff
changeset | 136 | ((Scan.lift Args.nat -- thm -- Scan.lift Args.nat) >> demod_syntax); | 
| 15384 | 137 | val demod_global = gen_demod global_thm; | 
| 138 | val demod_local = gen_demod local_thm; | |
| 15151 | 139 | |
| 140 | ||
| 15359 
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
 paulson parents: 
15151diff
changeset | 141 | (** Conversion of a theorem into clauses **) | 
| 
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
 paulson parents: 
15151diff
changeset | 142 | |
| 15466 | 143 | local | 
| 144 | ||
| 145 | (*Cache for clauses: could be a hash table if we provided them.*) | |
| 146 | val cc = ref (Symtab.empty : (thm * thm list) Symtab.table) | |
| 147 | ||
| 148 | fun memo_cnf th = | |
| 149 | case Thm.name_of_thm th of | |
| 15499 | 150 | "" => ResAxioms.meta_cnf_axiom th (*no name, so can't cache*) | 
| 15466 | 151 | | s => case Symtab.lookup (!cc,s) of | 
| 15531 | 152 | NONE => | 
| 15499 | 153 | let val cls = ResAxioms.meta_cnf_axiom th | 
| 15466 | 154 | in cc := Symtab.update ((s, (th,cls)), !cc); cls | 
| 155 | end | |
| 15531 | 156 | | SOME(th',cls) => | 
| 15466 | 157 | if eq_thm(th,th') then cls | 
| 158 | else (*New theorem stored under the same name? Possible??*) | |
| 15499 | 159 | let val cls = ResAxioms.meta_cnf_axiom th | 
| 15466 | 160 | in cc := Symtab.update ((s, (th,cls)), !cc); cls | 
| 161 | end; | |
| 162 | ||
| 163 | in | |
| 15499 | 164 | fun clausify_rule (A,i) = List.nth (memo_cnf A,i) | 
| 15466 | 165 | end; | 
| 15359 
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
 paulson parents: 
15151diff
changeset | 166 | |
| 
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
 paulson parents: 
15151diff
changeset | 167 | fun clausify_syntax i (x, A) = (x, clausify_rule (A,i)); | 
| 
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
 paulson parents: 
15151diff
changeset | 168 | |
| 15384 | 169 | fun clausify x = syntax ((Scan.lift Args.nat) >> clausify_syntax) x; | 
| 15359 
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
 paulson parents: 
15151diff
changeset | 170 | |
| 
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
 paulson parents: 
15151diff
changeset | 171 | |
| 15151 | 172 | (** theory setup **) | 
| 173 | ||
| 174 | val setup = | |
| 175 | [Attrib.add_attributes | |
| 15384 | 176 |      [("binary", (binary_global, binary_local), "binary resolution"),
 | 
| 177 |       ("paramod", (paramod_global, paramod_local), "paramodulation"),
 | |
| 178 |       ("demod", (demod_global, demod_local), "demodulation"),
 | |
| 179 |       ("factor", (factor, factor), "factoring"),
 | |
| 180 |       ("clausify", (clausify, clausify), "conversion to clauses")]];
 | |
| 15151 | 181 | |
| 182 | end | |
| 183 | end | |
| 184 | ||
| 185 |