author | paulson |
Wed, 18 May 2005 10:23:47 +0200 | |
changeset 15997 | c71031d7988c |
parent 15956 | 0da64b5a9a00 |
child 16009 | a6d480e6c5f0 |
permissions | -rw-r--r-- |
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 |
||
15997 | 10 |
signature RES_AXIOMS = |
11 |
sig |
|
12 |
exception ELIMR2FOL of string |
|
13 |
val elimRule_tac : thm -> Tactical.tactic |
|
14 |
val elimR2Fol : thm -> Term.term |
|
15 |
val transform_elim : thm -> thm |
|
16 |
||
17 |
val clausify_axiom : thm -> ResClause.clause list |
|
18 |
val cnf_axiom : (string * thm) -> thm list |
|
19 |
val meta_cnf_axiom : thm -> thm list |
|
20 |
val cnf_rule : thm -> thm list |
|
21 |
val cnf_classical_rules_thy : theory -> thm list list * thm list |
|
22 |
val clausify_classical_rules_thy : theory -> ResClause.clause list list * thm list |
|
23 |
val cnf_simpset_rules_thy : theory -> thm list list * thm list |
|
24 |
val clausify_simpset_rules_thy : theory -> ResClause.clause list list * thm list |
|
25 |
val rm_Eps |
|
26 |
: (Term.term * Term.term) list -> thm list -> Term.term list |
|
27 |
val claset_rules_of_thy : theory -> (string * thm) list |
|
28 |
val simpset_rules_of_thy : theory -> (string * thm) list |
|
29 |
val clausify_rules : thm list -> thm list -> ResClause.clause list list * thm list |
|
30 |
end; |
|
15347 | 31 |
|
15997 | 32 |
structure ResAxioms : RES_AXIOMS = |
33 |
||
34 |
struct |
|
15347 | 35 |
|
15997 | 36 |
(**** Transformation of Elimination Rules into First-Order Formulas****) |
15347 | 37 |
|
15390 | 38 |
(* a tactic used to prove an elim-rule. *) |
15347 | 39 |
fun elimRule_tac thm = |
40 |
((rtac impI 1) ORELSE (rtac notI 1)) THEN (etac thm 1) THEN |
|
15371 | 41 |
REPEAT(Fast_tac 1); |
15347 | 42 |
|
43 |
||
44 |
(* This following version fails sometimes, need to investigate, do not use it now. *) |
|
45 |
fun elimRule_tac' thm = |
|
46 |
((rtac impI 1) ORELSE (rtac notI 1)) THEN (etac thm 1) THEN |
|
47 |
REPEAT(SOLVE((etac exI 1) ORELSE (rtac conjI 1) ORELSE (rtac disjI1 1) ORELSE (rtac disjI2 1))); |
|
48 |
||
49 |
||
50 |
exception ELIMR2FOL of string; |
|
51 |
||
15390 | 52 |
(* functions used to construct a formula *) |
53 |
||
15347 | 54 |
fun make_disjs [x] = x |
15956 | 55 |
| make_disjs (x :: xs) = HOLogic.mk_disj(x, make_disjs xs) |
15347 | 56 |
|
57 |
fun make_conjs [x] = x |
|
15956 | 58 |
| make_conjs (x :: xs) = HOLogic.mk_conj(x, make_conjs xs) |
59 |
||
60 |
fun add_EX tm [] = tm |
|
61 |
| add_EX tm ((x,xtp)::xs) = add_EX (HOLogic.exists_const xtp $ Abs(x,xtp,tm)) xs; |
|
15347 | 62 |
|
63 |
||
64 |
||
15956 | 65 |
fun is_neg (Const("Trueprop",_) $ (Const("Not",_) $ Free(p,_))) (Const("Trueprop",_) $ Free(q,_)) = (p = q) |
15371 | 66 |
| is_neg _ _ = false; |
67 |
||
15347 | 68 |
|
69 |
exception STRIP_CONCL; |
|
70 |
||
71 |
||
15371 | 72 |
fun strip_concl' prems bvs (Const ("==>",_) $ P $ Q) = |
15956 | 73 |
let val P' = HOLogic.dest_Trueprop P |
74 |
val prems' = P'::prems |
|
75 |
in |
|
15371 | 76 |
strip_concl' prems' bvs Q |
15956 | 77 |
end |
15371 | 78 |
| strip_concl' prems bvs P = |
15956 | 79 |
let val P' = HOLogic.Not $ (HOLogic.dest_Trueprop P) |
80 |
in |
|
15371 | 81 |
add_EX (make_conjs (P'::prems)) bvs |
15956 | 82 |
end; |
15371 | 83 |
|
84 |
||
85 |
fun strip_concl prems bvs concl (Const ("all", _) $ Abs (x,xtp,body)) = strip_concl prems ((x,xtp)::bvs) concl body |
|
86 |
| strip_concl prems bvs concl (Const ("==>",_) $ P $ Q) = |
|
87 |
if (is_neg P concl) then (strip_concl' prems bvs Q) |
|
88 |
else |
|
15956 | 89 |
(let val P' = HOLogic.dest_Trueprop P |
15371 | 90 |
val prems' = P'::prems |
91 |
in |
|
92 |
strip_concl prems' bvs concl Q |
|
93 |
end) |
|
94 |
| strip_concl prems bvs concl _ = add_EX (make_conjs prems) bvs; |
|
15347 | 95 |
|
96 |
||
97 |
||
15371 | 98 |
fun trans_elim (main,others,concl) = |
99 |
let val others' = map (strip_concl [] [] concl) others |
|
15347 | 100 |
val disjs = make_disjs others' |
101 |
in |
|
15956 | 102 |
HOLogic.mk_imp (HOLogic.dest_Trueprop main, disjs) |
15347 | 103 |
end; |
104 |
||
105 |
||
15390 | 106 |
(* aux function of elim2Fol, take away predicate variable. *) |
15371 | 107 |
fun elimR2Fol_aux prems concl = |
15347 | 108 |
let val nprems = length prems |
109 |
val main = hd prems |
|
110 |
in |
|
15956 | 111 |
if (nprems = 1) then HOLogic.Not $ (HOLogic.dest_Trueprop main) |
15371 | 112 |
else trans_elim (main, tl prems, concl) |
15347 | 113 |
end; |
114 |
||
15956 | 115 |
|
15390 | 116 |
(* convert an elim rule into an equivalent formula, of type Term.term. *) |
15347 | 117 |
fun elimR2Fol elimR = |
118 |
let val elimR' = Drule.freeze_all elimR |
|
119 |
val (prems,concl) = (prems_of elimR', concl_of elimR') |
|
120 |
in |
|
121 |
case concl of Const("Trueprop",_) $ Free(_,Type("bool",[])) |
|
15956 | 122 |
=> HOLogic.mk_Trueprop (elimR2Fol_aux prems concl) |
123 |
| Free(x,Type("prop",[])) => HOLogic.mk_Trueprop(elimR2Fol_aux prems concl) |
|
15347 | 124 |
| _ => raise ELIMR2FOL("Not an elimination rule!") |
125 |
end; |
|
126 |
||
127 |
||
15390 | 128 |
(* check if a rule is an elim rule *) |
15347 | 129 |
fun is_elimR thm = |
130 |
case (concl_of thm) of (Const ("Trueprop", _) $ Var (idx,_)) => true |
|
131 |
| Var(indx,Type("prop",[])) => true |
|
132 |
| _ => false; |
|
133 |
||
15997 | 134 |
(* convert an elim-rule into an equivalent theorem that does not have the |
135 |
predicate variable. Leave other theorems unchanged.*) |
|
136 |
fun transform_elim thm = |
|
137 |
if is_elimR thm then |
|
138 |
let val tm = elimR2Fol thm |
|
139 |
val ctm = cterm_of (sign_of_thm thm) tm |
|
140 |
in |
|
141 |
prove_goalw_cterm [] ctm (fn prems => [elimRule_tac thm]) |
|
142 |
end |
|
143 |
else thm; |
|
144 |
||
145 |
||
146 |
(**** Transformation of Clasets and Simpsets into First-Order Axioms ****) |
|
147 |
||
148 |
(* to be fixed: cnf_intro, cnf_rule, is_introR *) |
|
15347 | 149 |
|
15390 | 150 |
(* repeated resolution *) |
15347 | 151 |
fun repeat_RS thm1 thm2 = |
152 |
let val thm1' = thm1 RS thm2 handle THM _ => thm1 |
|
153 |
in |
|
154 |
if eq_thm(thm1,thm1') then thm1' else (repeat_RS thm1' thm2) |
|
155 |
end; |
|
156 |
||
157 |
||
15390 | 158 |
(* convert a theorem into NNF and also skolemize it. *) |
15347 | 159 |
fun skolem_axiom thm = |
15872 | 160 |
if Term.is_first_order (prop_of thm) then |
161 |
let val thm' = (skolemize o make_nnf o ObjectLogic.atomize_thm o Drule.freeze_all) thm |
|
15347 | 162 |
in |
163 |
repeat_RS thm' someI_ex |
|
15872 | 164 |
end |
165 |
else raise THM ("skolem_axiom: not first-order", 0, [thm]); |
|
15347 | 166 |
|
167 |
||
15997 | 168 |
fun cnf_rule thm = make_clauses [skolem_axiom (transform_elim thm)]; |
15347 | 169 |
|
15370 | 170 |
(*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
|
171 |
inside that theory -- because it's needed for Skolemization *) |
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
paulson
parents:
15347
diff
changeset
|
172 |
|
15370 | 173 |
val recon_thy = ThyInfo.get_theory"Reconstruction"; |
15359
8bad1f42fec0
new CLAUSIFY attribute for proof reconstruction with lemmas
paulson
parents:
15347
diff
changeset
|
174 |
|
15370 | 175 |
fun transfer_to_Reconstruction thm = |
176 |
transfer recon_thy thm handle THM _ => thm; |
|
15347 | 177 |
|
15955
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
178 |
fun is_taut th = |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
179 |
case (prop_of th) of |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
180 |
(Const ("Trueprop", _) $ Const ("True", _)) => true |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
181 |
| _ => false; |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
182 |
|
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
183 |
(* remove tautologous clauses *) |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
184 |
val rm_redundant_cls = List.filter (not o is_taut); |
15347 | 185 |
|
186 |
(* transform an Isabelle thm into CNF *) |
|
15955
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
187 |
fun cnf_axiom_aux thm = |
15997 | 188 |
map (zero_var_indexes o Thm.varifyT) |
189 |
(rm_redundant_cls (cnf_rule (transfer_to_Reconstruction thm))); |
|
190 |
||
191 |
||
15955
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
192 |
(*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
|
193 |
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
|
194 |
|
15956 | 195 |
fun cnf_axiom (name,th) = |
196 |
case name of |
|
15955
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
197 |
"" => 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
|
198 |
| 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
|
199 |
NONE => |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
200 |
let val cls = cnf_axiom_aux th |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
201 |
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
|
202 |
end |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
203 |
| SOME(th',cls) => |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
204 |
if eq_thm(th,th') then cls |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
205 |
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
|
206 |
let val cls = cnf_axiom_aux th |
87cf2ce8ede8
memoization of ResAxioms.cnf_axiom rather than of Reconstruction.clausify_rule
paulson
parents:
15872
diff
changeset
|
207 |
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
|
208 |
end; |
15347 | 209 |
|
15956 | 210 |
fun pairname th = (Thm.name_of_thm th, th); |
211 |
||
212 |
fun meta_cnf_axiom th = |
|
213 |
map Meson.make_meta_clause (cnf_axiom (pairname th)); |
|
15499 | 214 |
|
15347 | 215 |
|
216 |
(* changed: with one extra case added *) |
|
15956 | 217 |
fun univ_vars_of_aux (Const ("Hilbert_Choice.Eps",_) $ Abs(_,_,body)) vars = |
218 |
univ_vars_of_aux body vars |
|
219 |
| univ_vars_of_aux (Const ("Ex",_) $ Abs(_,_,body)) vars = |
|
220 |
univ_vars_of_aux body vars (* EX x. body *) |
|
15347 | 221 |
| univ_vars_of_aux (P $ Q) vars = |
15956 | 222 |
univ_vars_of_aux Q (univ_vars_of_aux P vars) |
15347 | 223 |
| univ_vars_of_aux (t as Var(_,_)) vars = |
15956 | 224 |
if (t mem vars) then vars else (t::vars) |
15347 | 225 |
| univ_vars_of_aux _ vars = vars; |
226 |
||
227 |
fun univ_vars_of t = univ_vars_of_aux t []; |
|
228 |
||
229 |
||
230 |
fun get_new_skolem epss (t as (Const ("Hilbert_Choice.Eps",_) $ Abs(_,tp,_))) = |
|
231 |
let val all_vars = univ_vars_of t |
|
232 |
val sk_term = ResSkolemFunction.gen_skolem all_vars tp |
|
233 |
in |
|
234 |
(sk_term,(t,sk_term)::epss) |
|
235 |
end; |
|
236 |
||
237 |
||
15531 | 238 |
fun sk_lookup [] t = NONE |
239 |
| sk_lookup ((tm,sk_tm)::tms) t = if (t = tm) then SOME (sk_tm) else (sk_lookup tms t); |
|
15347 | 240 |
|
241 |
||
15390 | 242 |
|
243 |
(* get the proper skolem term to replace epsilon term *) |
|
15347 | 244 |
fun get_skolem epss t = |
15956 | 245 |
case (sk_lookup epss t) of NONE => get_new_skolem epss t |
246 |
| SOME sk => (sk,epss); |
|
15347 | 247 |
|
248 |
||
249 |
fun rm_Eps_cls_aux epss (t as (Const ("Hilbert_Choice.Eps",_) $ Abs(_,_,_))) = get_skolem epss t |
|
250 |
| rm_Eps_cls_aux epss (P $ Q) = |
|
251 |
let val (P',epss') = rm_Eps_cls_aux epss P |
|
252 |
val (Q',epss'') = rm_Eps_cls_aux epss' Q |
|
253 |
in |
|
254 |
(P' $ Q',epss'') |
|
255 |
end |
|
256 |
| rm_Eps_cls_aux epss t = (t,epss); |
|
257 |
||
258 |
||
15956 | 259 |
fun rm_Eps_cls epss thm = rm_Eps_cls_aux epss (prop_of thm); |
15347 | 260 |
|
261 |
||
15390 | 262 |
(* remove the epsilon terms in a formula, by skolem terms. *) |
15347 | 263 |
fun rm_Eps _ [] = [] |
264 |
| rm_Eps epss (thm::thms) = |
|
15956 | 265 |
let val (thm',epss') = rm_Eps_cls epss thm |
266 |
in |
|
15347 | 267 |
thm' :: (rm_Eps epss' thms) |
15956 | 268 |
end; |
15347 | 269 |
|
270 |
||
15390 | 271 |
(* convert a theorem into CNF and then into Clause.clause format. *) |
15347 | 272 |
fun clausify_axiom thm = |
15956 | 273 |
let val name = Thm.name_of_thm thm |
274 |
val isa_clauses = cnf_axiom (name, thm) |
|
15997 | 275 |
(*"isa_clauses" are already in "standard" form. *) |
15347 | 276 |
val isa_clauses' = rm_Eps [] isa_clauses |
15956 | 277 |
val clauses_n = length isa_clauses |
15347 | 278 |
fun make_axiom_clauses _ [] = [] |
15997 | 279 |
| make_axiom_clauses i (cls::clss) = |
280 |
(ResClause.make_axiom_clause cls (name,i)) :: make_axiom_clauses (i+1) clss |
|
15347 | 281 |
in |
15872 | 282 |
make_axiom_clauses 0 isa_clauses' |
15347 | 283 |
end; |
284 |
||
285 |
||
15872 | 286 |
(**** Extract and Clausify theorems from a theory's claset and simpset ****) |
15347 | 287 |
|
288 |
fun claset_rules_of_thy thy = |
|
289 |
let val clsset = rep_cs (claset_of thy) |
|
290 |
val safeEs = #safeEs clsset |
|
291 |
val safeIs = #safeIs clsset |
|
292 |
val hazEs = #hazEs clsset |
|
293 |
val hazIs = #hazIs clsset |
|
294 |
in |
|
15956 | 295 |
map pairname (safeEs @ safeIs @ hazEs @ hazIs) |
15347 | 296 |
end; |
297 |
||
298 |
fun simpset_rules_of_thy thy = |
|
15872 | 299 |
let val rules = #rules(fst (rep_ss (simpset_of thy))) |
15347 | 300 |
in |
15872 | 301 |
map (fn (_,r) => (#name r, #thm r)) (Net.dest rules) |
15347 | 302 |
end; |
303 |
||
304 |
||
15872 | 305 |
(**** Translate a set of classical/simplifier rules into CNF (still as type "thm") ****) |
15347 | 306 |
|
307 |
(* classical rules *) |
|
15872 | 308 |
fun cnf_rules [] err_list = ([],err_list) |
15956 | 309 |
| cnf_rules ((name,thm) :: thms) err_list = |
15872 | 310 |
let val (ts,es) = cnf_rules thms err_list |
15956 | 311 |
in (cnf_axiom (name,thm) :: ts,es) handle _ => (ts, (thm::es)) end; |
15347 | 312 |
|
313 |
(* CNF all rules from a given theory's classical reasoner *) |
|
314 |
fun cnf_classical_rules_thy thy = |
|
15872 | 315 |
cnf_rules (claset_rules_of_thy thy) []; |
15347 | 316 |
|
317 |
(* CNF all simplifier rules from a given theory's simpset *) |
|
318 |
fun cnf_simpset_rules_thy thy = |
|
15956 | 319 |
cnf_rules (simpset_rules_of_thy thy) []; |
15347 | 320 |
|
321 |
||
15872 | 322 |
(**** Convert all theorems of a claset/simpset into clauses (ResClause.clause) ****) |
15347 | 323 |
|
324 |
(* classical rules *) |
|
15872 | 325 |
fun clausify_rules [] err_list = ([],err_list) |
326 |
| clausify_rules (thm::thms) err_list = |
|
327 |
let val (ts,es) = clausify_rules thms err_list |
|
15347 | 328 |
in |
329 |
((clausify_axiom thm)::ts,es) handle _ => (ts,(thm::es)) |
|
330 |
end; |
|
331 |
||
15390 | 332 |
|
15736 | 333 |
(* convert all classical rules from a given theory into Clause.clause format. *) |
15347 | 334 |
fun clausify_classical_rules_thy thy = |
15956 | 335 |
clausify_rules (map #2 (claset_rules_of_thy thy)) []; |
15347 | 336 |
|
15736 | 337 |
(* convert all simplifier rules from a given theory into Clause.clause format. *) |
15347 | 338 |
fun clausify_simpset_rules_thy thy = |
15872 | 339 |
clausify_rules (map #2 (simpset_rules_of_thy thy)) []; |
15347 | 340 |
|
341 |
||
342 |
end; |