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