author  paulson 
Thu, 12 May 2005 18:24:42 +0200  
changeset 15956  0da64b5a9a00 
parent 15955  87cf2ce8ede8 
child 15997  c71031d7988c 
permissions  rwrr 
15347  1 
(* Author: Jia Meng, Cambridge University Computer Laboratory 
2 
ID: $Id$ 

3 
Copyright 2004 University of Cambridge 

4 

5 
Transformation of axiom rules (elim/intro/etc) into CNF forms. 

6 
*) 

7 

8 

9 

10 
signature RES_ELIM_RULE = 

11 
sig 

12 

13 
exception ELIMR2FOL of string 

15956  14 
val elimRule_tac : thm > Tactical.tactic 
15 
val elimR2Fol : thm > Term.term 

16 
val transform_elim : thm > thm 

15347  17 

18 
end; 

19 

20 
structure ResElimRule: RES_ELIM_RULE = 

21 

22 
struct 

23 

15390  24 
(* a tactic used to prove an elimrule. *) 
15347  25 
fun elimRule_tac thm = 
26 
((rtac impI 1) ORELSE (rtac notI 1)) THEN (etac thm 1) THEN 

15371  27 
REPEAT(Fast_tac 1); 
15347  28 

29 

30 
(* This following version fails sometimes, need to investigate, do not use it now. *) 

31 
fun elimRule_tac' thm = 

32 
((rtac impI 1) ORELSE (rtac notI 1)) THEN (etac thm 1) THEN 

33 
REPEAT(SOLVE((etac exI 1) ORELSE (rtac conjI 1) ORELSE (rtac disjI1 1) ORELSE (rtac disjI2 1))); 

34 

35 

36 
exception ELIMR2FOL of string; 

37 

15390  38 
(* functions used to construct a formula *) 
39 

15347  40 
fun make_disjs [x] = x 
15956  41 
 make_disjs (x :: xs) = HOLogic.mk_disj(x, make_disjs xs) 
15347  42 

43 
fun make_conjs [x] = x 

15956  44 
 make_conjs (x :: xs) = HOLogic.mk_conj(x, make_conjs xs) 
45 

46 
fun add_EX tm [] = tm 

47 
 add_EX tm ((x,xtp)::xs) = add_EX (HOLogic.exists_const xtp $ Abs(x,xtp,tm)) xs; 

15347  48 

49 

50 

15956  51 
fun is_neg (Const("Trueprop",_) $ (Const("Not",_) $ Free(p,_))) (Const("Trueprop",_) $ Free(q,_)) = (p = q) 
15371  52 
 is_neg _ _ = false; 
53 

15347  54 

55 
exception STRIP_CONCL; 

56 

57 

15371  58 
fun strip_concl' prems bvs (Const ("==>",_) $ P $ Q) = 
15956  59 
let val P' = HOLogic.dest_Trueprop P 
60 
val prems' = P'::prems 

61 
in 

15371  62 
strip_concl' prems' bvs Q 
15956  63 
end 
15371  64 
 strip_concl' prems bvs P = 
15956  65 
let val P' = HOLogic.Not $ (HOLogic.dest_Trueprop P) 
66 
in 

15371  67 
add_EX (make_conjs (P'::prems)) bvs 
15956  68 
end; 
15371  69 

70 

71 
fun strip_concl prems bvs concl (Const ("all", _) $ Abs (x,xtp,body)) = strip_concl prems ((x,xtp)::bvs) concl body 

72 
 strip_concl prems bvs concl (Const ("==>",_) $ P $ Q) = 

73 
if (is_neg P concl) then (strip_concl' prems bvs Q) 

74 
else 

15956  75 
(let val P' = HOLogic.dest_Trueprop P 
15371  76 
val prems' = P'::prems 
77 
in 

78 
strip_concl prems' bvs concl Q 

79 
end) 

80 
 strip_concl prems bvs concl _ = add_EX (make_conjs prems) bvs; 

15347  81 

82 

83 

15371  84 
fun trans_elim (main,others,concl) = 
85 
let val others' = map (strip_concl [] [] concl) others 

15347  86 
val disjs = make_disjs others' 
87 
in 

15956  88 
HOLogic.mk_imp (HOLogic.dest_Trueprop main, disjs) 
15347  89 
end; 
90 

91 

15390  92 
(* aux function of elim2Fol, take away predicate variable. *) 
15371  93 
fun elimR2Fol_aux prems concl = 
15347  94 
let val nprems = length prems 
95 
val main = hd prems 

96 
in 

15956  97 
if (nprems = 1) then HOLogic.Not $ (HOLogic.dest_Trueprop main) 
15371  98 
else trans_elim (main, tl prems, concl) 
15347  99 
end; 
100 

15956  101 

15390  102 
(* convert an elim rule into an equivalent formula, of type Term.term. *) 
15347  103 
fun elimR2Fol elimR = 
104 
let val elimR' = Drule.freeze_all elimR 

105 
val (prems,concl) = (prems_of elimR', concl_of elimR') 

106 
in 

107 
case concl of Const("Trueprop",_) $ Free(_,Type("bool",[])) 

15956  108 
=> HOLogic.mk_Trueprop (elimR2Fol_aux prems concl) 
109 
 Free(x,Type("prop",[])) => HOLogic.mk_Trueprop(elimR2Fol_aux prems concl) 

15347  110 
 _ => raise ELIMR2FOL("Not an elimination rule!") 
111 
end; 

112 

113 

114 

115 
(**** use prove_goalw_cterm to prove ****) 

116 

15390  117 
(* convert an elimrule into an equivalent theorem that does not have the predicate variable. *) 
15347  118 
fun transform_elim thm = 
119 
let val tm = elimR2Fol thm 

120 
val ctm = cterm_of (sign_of_thm thm) tm 

121 
in 

122 
prove_goalw_cterm [] ctm (fn prems => [elimRule_tac thm]) 

123 
end; 

124 

125 

126 
end; 

127 

128 

129 

130 
signature RES_AXIOMS = 

131 
sig 

132 

15956  133 
val clausify_axiom : thm > ResClause.clause list 
134 
val cnf_axiom : (string * thm) > thm list 

135 
val meta_cnf_axiom : thm > thm list 

136 
val cnf_elim : thm > thm list 

137 
val cnf_rule : thm > thm list 

138 
val cnf_classical_rules_thy : theory > thm list list * thm list 

139 
val clausify_classical_rules_thy : theory > ResClause.clause list list * thm list 

140 
val cnf_simpset_rules_thy : theory > thm list list * thm list 

141 
val clausify_simpset_rules_thy : theory > ResClause.clause list list * thm list 

15347  142 
val rm_Eps 
15956  143 
: (Term.term * Term.term) list > thm list > Term.term list 
144 
val claset_rules_of_thy : theory > (string * thm) list 

145 
val simpset_rules_of_thy : theory > (string * thm) list 

146 
val clausify_rules : thm list > thm list > ResClause.clause list list * thm list 

15684
5ec4d21889d6
Reconstruction code, now packaged to avoid name clashes
paulson
parents:
15644
diff
changeset

147 

15347  148 
end; 
149 

150 
structure ResAxioms : RES_AXIOMS = 

151 

152 
struct 

153 

154 
open ResElimRule; 

155 

156 
(* to be fixed: cnf_intro, cnf_rule, is_introR *) 

157 

15390  158 
(* check if a rule is an elim rule *) 
15347  159 
fun is_elimR thm = 
160 
case (concl_of thm) of (Const ("Trueprop", _) $ Var (idx,_)) => true 

161 
 Var(indx,Type("prop",[])) => true 

162 
 _ => false; 

163 

164 

15390  165 
(* repeated resolution *) 
15347  166 
fun repeat_RS thm1 thm2 = 
167 
let val thm1' = thm1 RS thm2 handle THM _ => thm1 

168 
in 

169 
if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2) 

170 
end; 

171 

172 

15390  173 
(* convert a theorem into NNF and also skolemize it. *) 
15347  174 
fun skolem_axiom thm = 
15872  175 
if Term.is_first_order (prop_of thm) then 
176 
let val thm' = (skolemize o make_nnf o ObjectLogic.atomize_thm o Drule.freeze_all) thm 

15347  177 
in 
178 
repeat_RS thm' someI_ex 

15872  179 
end 
180 
else raise THM ("skolem_axiom: not firstorder", 0, [thm]); 

15347  181 

182 

15872  183 
fun cnf_rule thm = make_clauses [skolem_axiom thm] 
15347  184 

15872  185 
fun cnf_elim thm = cnf_rule (transform_elim thm); 
15347  186 

187 

15370  188 
(*Transfer a theorem in to theory Reconstruction.thy if it is not already 
15359
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
paulson
parents:
15347
diff
changeset

189 
inside that theory  because it's needed for Skolemization *) 
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
paulson
parents:
15347
diff
changeset

190 

15370  191 
val recon_thy = ThyInfo.get_theory"Reconstruction"; 
15359
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
paulson
parents:
15347
diff
changeset

192 

15370  193 
fun transfer_to_Reconstruction thm = 
194 
transfer recon_thy thm handle THM _ => thm; 

15347  195 

15955
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

196 
fun is_taut th = 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

197 
case (prop_of th) of 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

198 
(Const ("Trueprop", _) $ Const ("True", _)) => true 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

199 
 _ => false; 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

200 

87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

201 
(* remove tautologous clauses *) 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

202 
val rm_redundant_cls = List.filter (not o is_taut); 
15347  203 

204 
(* transform an Isabelle thm into CNF *) 

15955
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

205 
fun cnf_axiom_aux thm = 
15370  206 
let val thm' = transfer_to_Reconstruction thm 
15499  207 
val thm'' = if (is_elimR thm') then (cnf_elim thm') else cnf_rule thm' 
15347  208 
in 
15955
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

209 
map (zero_var_indexes o Thm.varifyT) (rm_redundant_cls thm'') 
15347  210 
end; 
15955
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

211 

87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

212 
(*Cache for clauses: could be a hash table if we provided them.*) 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

213 
val clause_cache = ref (Symtab.empty : (thm * thm list) Symtab.table) 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

214 

15956  215 
fun cnf_axiom (name,th) = 
216 
case name of 

15955
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

217 
"" => cnf_axiom_aux th (*no name, so can't cache*) 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

218 
 s => case Symtab.lookup (!clause_cache,s) of 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

219 
NONE => 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

220 
let val cls = cnf_axiom_aux th 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

221 
in clause_cache := Symtab.update ((s, (th,cls)), !clause_cache); cls 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

222 
end 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

223 
 SOME(th',cls) => 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

224 
if eq_thm(th,th') then cls 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

225 
else (*New theorem stored under the same name? Possible??*) 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

226 
let val cls = cnf_axiom_aux th 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

227 
in clause_cache := Symtab.update ((s, (th,cls)), !clause_cache); cls 
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset

228 
end; 
15347  229 

15956  230 
fun pairname th = (Thm.name_of_thm th, th); 
231 

232 
fun meta_cnf_axiom th = 

233 
map Meson.make_meta_clause (cnf_axiom (pairname th)); 

15499  234 

15347  235 

236 
(* changed: with one extra case added *) 

15956  237 
fun univ_vars_of_aux (Const ("Hilbert_Choice.Eps",_) $ Abs(_,_,body)) vars = 
238 
univ_vars_of_aux body vars 

239 
 univ_vars_of_aux (Const ("Ex",_) $ Abs(_,_,body)) vars = 

240 
univ_vars_of_aux body vars (* EX x. body *) 

15347  241 
 univ_vars_of_aux (P $ Q) vars = 
15956  242 
univ_vars_of_aux Q (univ_vars_of_aux P vars) 
15347  243 
 univ_vars_of_aux (t as Var(_,_)) vars = 
15956  244 
if (t mem vars) then vars else (t::vars) 
15347  245 
 univ_vars_of_aux _ vars = vars; 
246 

247 
fun univ_vars_of t = univ_vars_of_aux t []; 

248 

249 

250 
fun get_new_skolem epss (t as (Const ("Hilbert_Choice.Eps",_) $ Abs(_,tp,_))) = 

251 
let val all_vars = univ_vars_of t 

252 
val sk_term = ResSkolemFunction.gen_skolem all_vars tp 

253 
in 

254 
(sk_term,(t,sk_term)::epss) 

255 
end; 

256 

257 

15531  258 
fun sk_lookup [] t = NONE 
259 
 sk_lookup ((tm,sk_tm)::tms) t = if (t = tm) then SOME (sk_tm) else (sk_lookup tms t); 

15347  260 

261 

15390  262 

263 
(* get the proper skolem term to replace epsilon term *) 

15347  264 
fun get_skolem epss t = 
15956  265 
case (sk_lookup epss t) of NONE => get_new_skolem epss t 
266 
 SOME sk => (sk,epss); 

15347  267 

268 

269 
fun rm_Eps_cls_aux epss (t as (Const ("Hilbert_Choice.Eps",_) $ Abs(_,_,_))) = get_skolem epss t 

270 
 rm_Eps_cls_aux epss (P $ Q) = 

271 
let val (P',epss') = rm_Eps_cls_aux epss P 

272 
val (Q',epss'') = rm_Eps_cls_aux epss' Q 

273 
in 

274 
(P' $ Q',epss'') 

275 
end 

276 
 rm_Eps_cls_aux epss t = (t,epss); 

277 

278 

15956  279 
fun rm_Eps_cls epss thm = rm_Eps_cls_aux epss (prop_of thm); 
15347  280 

281 

15390  282 
(* remove the epsilon terms in a formula, by skolem terms. *) 
15347  283 
fun rm_Eps _ [] = [] 
284 
 rm_Eps epss (thm::thms) = 

15956  285 
let val (thm',epss') = rm_Eps_cls epss thm 
286 
in 

15347  287 
thm' :: (rm_Eps epss' thms) 
15956  288 
end; 
15347  289 

290 

15390  291 
(* convert a theorem into CNF and then into Clause.clause format. *) 
15347  292 
fun clausify_axiom thm = 
15956  293 
let val name = Thm.name_of_thm thm 
294 
val isa_clauses = cnf_axiom (name, thm) 

295 
(*"isa_clauses" are already "standard"ed. *) 

15347  296 
val isa_clauses' = rm_Eps [] isa_clauses 
15956  297 
val clauses_n = length isa_clauses 
15347  298 
fun make_axiom_clauses _ [] = [] 
15956  299 
 make_axiom_clauses i (cls::clss) = (ResClause.make_axiom_clause cls (name,i)) :: make_axiom_clauses (i+1) clss 
15347  300 
in 
15872  301 
make_axiom_clauses 0 isa_clauses' 
15347  302 
end; 
303 

304 

15872  305 
(**** Extract and Clausify theorems from a theory's claset and simpset ****) 
15347  306 

307 
fun claset_rules_of_thy thy = 

308 
let val clsset = rep_cs (claset_of thy) 

309 
val safeEs = #safeEs clsset 

310 
val safeIs = #safeIs clsset 

311 
val hazEs = #hazEs clsset 

312 
val hazIs = #hazIs clsset 

313 
in 

15956  314 
map pairname (safeEs @ safeIs @ hazEs @ hazIs) 
15347  315 
end; 
316 

317 
fun simpset_rules_of_thy thy = 

15872  318 
let val rules = #rules(fst (rep_ss (simpset_of thy))) 
15347  319 
in 
15872  320 
map (fn (_,r) => (#name r, #thm r)) (Net.dest rules) 
15347  321 
end; 
322 

323 

15872  324 
(**** Translate a set of classical/simplifier rules into CNF (still as type "thm") ****) 
15347  325 

326 
(* classical rules *) 

15872  327 
fun cnf_rules [] err_list = ([],err_list) 
15956  328 
 cnf_rules ((name,thm) :: thms) err_list = 
15872  329 
let val (ts,es) = cnf_rules thms err_list 
15956  330 
in (cnf_axiom (name,thm) :: ts,es) handle _ => (ts, (thm::es)) end; 
15347  331 

332 
(* CNF all rules from a given theory's classical reasoner *) 

333 
fun cnf_classical_rules_thy thy = 

15872  334 
cnf_rules (claset_rules_of_thy thy) []; 
15347  335 

336 
(* CNF all simplifier rules from a given theory's simpset *) 

337 
fun cnf_simpset_rules_thy thy = 

15956  338 
cnf_rules (simpset_rules_of_thy thy) []; 
15347  339 

340 

15872  341 
(**** Convert all theorems of a claset/simpset into clauses (ResClause.clause) ****) 
15347  342 

343 
(* classical rules *) 

15872  344 
fun clausify_rules [] err_list = ([],err_list) 
345 
 clausify_rules (thm::thms) err_list = 

346 
let val (ts,es) = clausify_rules thms err_list 

15347  347 
in 
348 
((clausify_axiom thm)::ts,es) handle _ => (ts,(thm::es)) 

349 
end; 

350 

15390  351 

15736  352 
(* convert all classical rules from a given theory into Clause.clause format. *) 
15347  353 
fun clausify_classical_rules_thy thy = 
15956  354 
clausify_rules (map #2 (claset_rules_of_thy thy)) []; 
15347  355 

15736  356 
(* convert all simplifier rules from a given theory into Clause.clause format. *) 
15347  357 
fun clausify_simpset_rules_thy thy = 
15872  358 
clausify_rules (map #2 (simpset_rules_of_thy thy)) []; 
15347  359 

360 

361 
end; 