33 exists (fn prefix => String.isPrefix prefix s) |
33 exists (fn prefix => String.isPrefix prefix s) |
34 [new_skolem_var_prefix, new_nonskolem_var_prefix] |
34 [new_skolem_var_prefix, new_nonskolem_var_prefix] |
35 |
35 |
36 (**** Transformation of Elimination Rules into First-Order Formulas****) |
36 (**** Transformation of Elimination Rules into First-Order Formulas****) |
37 |
37 |
38 val cfalse = Thm.global_cterm_of @{theory HOL} @{term False}; |
38 val cfalse = Thm.cterm_of @{theory_context HOL} @{term False}; |
39 val ctp_false = Thm.global_cterm_of @{theory HOL} (HOLogic.mk_Trueprop @{term False}); |
39 val ctp_false = Thm.cterm_of @{theory_context HOL} (HOLogic.mk_Trueprop @{term False}); |
40 |
40 |
41 (* Converts an elim-rule into an equivalent theorem that does not have the |
41 (* Converts an elim-rule into an equivalent theorem that does not have the |
42 predicate variable. Leaves other theorems unchanged. We simply instantiate |
42 predicate variable. Leaves other theorems unchanged. We simply instantiate |
43 the conclusion variable to False. (Cf. "transform_elim_prop" in |
43 the conclusion variable to False. (Cf. "transform_elim_prop" in |
44 "Sledgehammer_Util".) *) |
44 "Sledgehammer_Util".) *) |
45 fun transform_elim_theorem th = |
45 fun transform_elim_theorem th = |
46 (case Thm.concl_of th of (*conclusion variable*) |
46 (case Thm.concl_of th of (*conclusion variable*) |
47 @{const Trueprop} $ (v as Var (_, @{typ bool})) => |
47 @{const Trueprop} $ (v as Var (_, @{typ bool})) => |
48 Thm.instantiate ([], [(Thm.global_cterm_of @{theory HOL} v, cfalse)]) th |
48 Thm.instantiate ([], [(Thm.cterm_of @{theory_context HOL} v, cfalse)]) th |
49 | v as Var(_, @{typ prop}) => |
49 | v as Var(_, @{typ prop}) => |
50 Thm.instantiate ([], [(Thm.global_cterm_of @{theory HOL} v, ctp_false)]) th |
50 Thm.instantiate ([], [(Thm.cterm_of @{theory_context HOL} v, ctp_false)]) th |
51 | _ => th) |
51 | _ => th) |
52 |
52 |
53 |
53 |
54 (**** SKOLEMIZATION BY INFERENCE (lcp) ****) |
54 (**** SKOLEMIZATION BY INFERENCE (lcp) ****) |
55 |
55 |
92 | is_quasi_lambda_free (t1 $ t2) = |
92 | is_quasi_lambda_free (t1 $ t2) = |
93 is_quasi_lambda_free t1 andalso is_quasi_lambda_free t2 |
93 is_quasi_lambda_free t1 andalso is_quasi_lambda_free t2 |
94 | is_quasi_lambda_free (Abs _) = false |
94 | is_quasi_lambda_free (Abs _) = false |
95 | is_quasi_lambda_free _ = true |
95 | is_quasi_lambda_free _ = true |
96 |
96 |
97 val [f_B,g_B] = map (Thm.global_cterm_of @{theory}) (Misc_Legacy.term_vars (Thm.prop_of @{thm abs_B})); |
97 val [f_B,g_B] = map (Thm.cterm_of @{context}) (Misc_Legacy.term_vars (Thm.prop_of @{thm abs_B})); |
98 val [g_C,f_C] = map (Thm.global_cterm_of @{theory}) (Misc_Legacy.term_vars (Thm.prop_of @{thm abs_C})); |
98 val [g_C,f_C] = map (Thm.cterm_of @{context}) (Misc_Legacy.term_vars (Thm.prop_of @{thm abs_C})); |
99 val [f_S,g_S] = map (Thm.global_cterm_of @{theory}) (Misc_Legacy.term_vars (Thm.prop_of @{thm abs_S})); |
99 val [f_S,g_S] = map (Thm.cterm_of @{context}) (Misc_Legacy.term_vars (Thm.prop_of @{thm abs_S})); |
100 |
100 |
101 (* FIXME: Requires more use of cterm constructors. *) |
101 (* FIXME: Requires more use of cterm constructors. *) |
102 fun abstract ct = |
102 fun abstract ct = |
103 let |
103 let |
104 val thy = Thm.theory_of_cterm ct |
104 val thy = Thm.theory_of_cterm ct |
177 "\nException message: " ^ msg); |
177 "\nException message: " ^ msg); |
178 (* A type variable of sort "{}" will make "abstraction" fail. *) |
178 (* A type variable of sort "{}" will make "abstraction" fail. *) |
179 TrueI) |
179 TrueI) |
180 |
180 |
181 (*cterms are used throughout for efficiency*) |
181 (*cterms are used throughout for efficiency*) |
182 val cTrueprop = Thm.global_cterm_of @{theory HOL} HOLogic.Trueprop; |
182 val cTrueprop = Thm.cterm_of @{theory_context HOL} HOLogic.Trueprop; |
183 |
183 |
184 (*Given an abstraction over n variables, replace the bound variables by free |
184 (*Given an abstraction over n variables, replace the bound variables by free |
185 ones. Return the body, along with the list of free variables.*) |
185 ones. Return the body, along with the list of free variables.*) |
186 fun c_variant_abs_multi (ct0, vars) = |
186 fun c_variant_abs_multi (ct0, vars) = |
187 let val (cv,ct) = Thm.dest_abs NONE ct0 |
187 let val (cv,ct) = Thm.dest_abs NONE ct0 |
191 (* Given the definition of a Skolem function, return a theorem to replace |
191 (* Given the definition of a Skolem function, return a theorem to replace |
192 an existential formula by a use of that function. |
192 an existential formula by a use of that function. |
193 Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B" [.] *) |
193 Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B" [.] *) |
194 fun old_skolem_theorem_of_def ctxt rhs0 = |
194 fun old_skolem_theorem_of_def ctxt rhs0 = |
195 let |
195 let |
196 val thy = Proof_Context.theory_of ctxt |
196 val rhs = rhs0 |> Type.legacy_freeze_thaw |> #1 |> Thm.cterm_of ctxt |
197 val rhs = rhs0 |> Type.legacy_freeze_thaw |> #1 |> Thm.global_cterm_of thy |
|
198 val rhs' = rhs |> Thm.dest_comb |> snd |
197 val rhs' = rhs |> Thm.dest_comb |> snd |
199 val (ch, frees) = c_variant_abs_multi (rhs', []) |
198 val (ch, frees) = c_variant_abs_multi (rhs', []) |
200 val (hilbert, cabs) = ch |> Thm.dest_comb |>> Thm.term_of |
199 val (hilbert, cabs) = ch |> Thm.dest_comb |>> Thm.term_of |
201 val T = |
200 val T = |
202 case hilbert of |
201 case hilbert of |
203 Const (_, Type (@{type_name fun}, [_, T])) => T |
202 Const (_, Type (@{type_name fun}, [_, T])) => T |
204 | _ => raise TERM ("old_skolem_theorem_of_def: expected \"Eps\"", [hilbert]) |
203 | _ => raise TERM ("old_skolem_theorem_of_def: expected \"Eps\"", [hilbert]) |
205 val cex = Thm.global_cterm_of thy (HOLogic.exists_const T) |
204 val cex = Thm.cterm_of ctxt (HOLogic.exists_const T) |
206 val ex_tm = Thm.apply cTrueprop (Thm.apply cex cabs) |
205 val ex_tm = Thm.apply cTrueprop (Thm.apply cex cabs) |
207 val conc = |
206 val conc = |
208 Drule.list_comb (rhs, frees) |
207 Drule.list_comb (rhs, frees) |
209 |> Drule.beta_conv cabs |> Thm.apply cTrueprop |
208 |> Drule.beta_conv cabs |> Thm.apply cTrueprop |
210 fun tacf [prem] = |
209 fun tacf [prem] = |
211 rewrite_goals_tac ctxt @{thms skolem_def [abs_def]} |
210 rewrite_goals_tac ctxt @{thms skolem_def [abs_def]} |
212 THEN resolve_tac ctxt [(prem |> rewrite_rule ctxt @{thms skolem_def [abs_def]}) |
211 THEN resolve_tac ctxt |
213 RS Global_Theory.get_thm thy "Hilbert_Choice.someI_ex"] 1 |
212 [(prem |> rewrite_rule ctxt @{thms skolem_def [abs_def]}) |
|
213 RS Global_Theory.get_thm (Proof_Context.theory_of ctxt) "Hilbert_Choice.someI_ex"] 1 |
214 in |
214 in |
215 Goal.prove_internal ctxt [ex_tm] conc tacf |
215 Goal.prove_internal ctxt [ex_tm] conc tacf |
216 |> forall_intr_list frees |
216 |> forall_intr_list frees |
217 |> Thm.forall_elim_vars 0 (*Introduce Vars, but don't discharge defs.*) |
217 |> Thm.forall_elim_vars 0 (*Introduce Vars, but don't discharge defs.*) |
218 |> Thm.varifyT_global |
218 |> Thm.varifyT_global |
307 th |> transform_elim_theorem |
307 th |> transform_elim_theorem |
308 |> zero_var_indexes |
308 |> zero_var_indexes |
309 |> new_skolem ? forall_intr_vars |
309 |> new_skolem ? forall_intr_vars |
310 val (th, ctxt) = Variable.import true [th] ctxt |>> snd |>> the_single |
310 val (th, ctxt) = Variable.import true [th] ctxt |>> snd |>> the_single |
311 val th = th |> Conv.fconv_rule (Object_Logic.atomize ctxt) |
311 val th = th |> Conv.fconv_rule (Object_Logic.atomize ctxt) |
312 |> cong_extensionalize_thm thy |
312 |> cong_extensionalize_thm ctxt |
313 |> abs_extensionalize_thm ctxt |
313 |> abs_extensionalize_thm ctxt |
314 |> make_nnf ctxt |
314 |> make_nnf ctxt |
315 in |
315 in |
316 if new_skolem then |
316 if new_skolem then |
317 let |
317 let |
332 val zapped_th = |
332 val zapped_th = |
333 discharger_th |> Thm.prop_of |> rename_bound_vars_to_be_zapped ax_no |
333 discharger_th |> Thm.prop_of |> rename_bound_vars_to_be_zapped ax_no |
334 |> (if no_choice then |
334 |> (if no_choice then |
335 Skip_Proof.make_thm thy #> skolemize [cheat_choice] #> Thm.cprop_of |
335 Skip_Proof.make_thm thy #> skolemize [cheat_choice] #> Thm.cprop_of |
336 else |
336 else |
337 Thm.global_cterm_of thy) |
337 Thm.cterm_of ctxt) |
338 |> zap true |
338 |> zap true |
339 val fixes = |
339 val fixes = |
340 [] |> Term.add_free_names (Thm.prop_of zapped_th) |
340 [] |> Term.add_free_names (Thm.prop_of zapped_th) |
341 |> filter is_zapped_var_name |
341 |> filter is_zapped_var_name |
342 val ctxt' = ctxt |> Variable.add_fixes_direct fixes |
342 val ctxt' = ctxt |> Variable.add_fixes_direct fixes |
348 String.isPrefix new_skolem_var_prefix s |
348 String.isPrefix new_skolem_var_prefix s |
349 | _ => false) fully_skolemized_t then |
349 | _ => false) fully_skolemized_t then |
350 let |
350 let |
351 val (fully_skolemized_ct, ctxt) = |
351 val (fully_skolemized_ct, ctxt) = |
352 Variable.import_terms true [fully_skolemized_t] ctxt |
352 Variable.import_terms true [fully_skolemized_t] ctxt |
353 |>> the_single |>> Thm.global_cterm_of thy |
353 |>> the_single |>> Thm.cterm_of ctxt |
354 in |
354 in |
355 (SOME (discharger_th, fully_skolemized_ct), |
355 (SOME (discharger_th, fully_skolemized_ct), |
356 (Thm.assume fully_skolemized_ct, ctxt)) |
356 (Thm.assume fully_skolemized_ct, ctxt)) |
357 end |
357 end |
358 else |
358 else |
375 (if new_skolem orelse null choice_ths then [] |
375 (if new_skolem orelse null choice_ths then [] |
376 else map (old_skolem_theorem_of_def ctxt) (old_skolem_defs th)) |
376 else map (old_skolem_theorem_of_def ctxt) (old_skolem_defs th)) |
377 th ctxt |
377 th ctxt |
378 val (cnf_ths, ctxt) = clausify nnf_th |
378 val (cnf_ths, ctxt) = clausify nnf_th |
379 fun intr_imp ct th = |
379 fun intr_imp ct th = |
380 Thm.instantiate ([], map (apply2 (Thm.global_cterm_of thy)) |
380 Thm.instantiate ([], map (apply2 (Thm.cterm_of ctxt)) |
381 [(Var (("i", 0), @{typ nat}), |
381 [(Var (("i", 0), @{typ nat}), |
382 HOLogic.mk_nat ax_no)]) |
382 HOLogic.mk_nat ax_no)]) |
383 (zero_var_indexes @{thm skolem_COMBK_D}) |
383 (zero_var_indexes @{thm skolem_COMBK_D}) |
384 RS Thm.implies_intr ct th |
384 RS Thm.implies_intr ct th |
385 in |
385 in |