author | wenzelm |
Thu, 30 Nov 2006 14:17:29 +0100 | |
changeset 21605 | 4e7307e229b3 |
parent 21229 | 9c96c1ec235f |
child 21686 | 4f5f6c685ab4 |
permissions | -rw-r--r-- |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/Isar/obtain.ML |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
3 |
Author: Markus Wenzel, TU Muenchen |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
4 |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
5 |
The 'obtain' and 'guess' language elements -- generalized existence at |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
6 |
the level of proof texts: 'obtain' involves a proof that certain |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
7 |
fixes/assumes may be introduced into the present context; 'guess' is |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
8 |
similar, but derives these elements from the course of reasoning! |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
9 |
|
9468 | 10 |
<chain_facts> |
18870 | 11 |
obtain x where "A x" <proof> == |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
12 |
|
18870 | 13 |
have "!!thesis. (!!x. A x ==> thesis) ==> thesis" |
12970 | 14 |
proof succeed |
9468 | 15 |
fix thesis |
18870 | 16 |
assume that [intro?]: "!!x. A x ==> thesis" |
17 |
<chain_facts> |
|
18 |
show thesis |
|
19 |
apply (insert that) |
|
20 |
<proof> |
|
12970 | 21 |
qed |
18870 | 22 |
fix x assm <<obtain_export>> "A x" |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
23 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
24 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
25 |
<chain_facts> |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
26 |
guess x <proof body> <proof end> == |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
27 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
28 |
{ |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
29 |
fix thesis |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
30 |
<chain_facts> have "PROP ?guess" |
18870 | 31 |
apply magic -- {* turns goal into "thesis ==> #thesis" *} |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
32 |
<proof body> |
18870 | 33 |
apply_end magic -- {* turns final "(!!x. P x ==> thesis) ==> #thesis" into |
34 |
"#((!!x. A x ==> thesis) ==> thesis)" which is a finished goal state *} |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
35 |
<proof end> |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
36 |
} |
18870 | 37 |
fix x assm <<obtain_export>> "A x" |
8094 | 38 |
*) |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
39 |
|
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
40 |
signature OBTAIN = |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
41 |
sig |
21229 | 42 |
val thatN: string |
19844 | 43 |
val obtain: string -> (string * string option * mixfix) list -> |
20308 | 44 |
((string * Attrib.src list) * (string * string list) list) list -> |
45 |
bool -> Proof.state -> Proof.state |
|
19844 | 46 |
val obtain_i: string -> (string * typ option * mixfix) list -> |
20308 | 47 |
((string * attribute list) * (term * term list) list) list -> |
48 |
bool -> Proof.state -> Proof.state |
|
49 |
val result: (Proof.context -> tactic) -> thm list -> Proof.context -> |
|
50 |
(cterm list * thm list) * Proof.context |
|
19844 | 51 |
val guess: (string * string option * mixfix) list -> bool -> Proof.state -> Proof.state |
52 |
val guess_i: (string * typ option * mixfix) list -> bool -> Proof.state -> Proof.state |
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
53 |
end; |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
54 |
|
10379
93630e0c5ae9
improved handling of "that": insert into goal, only declare as Pure "intro";
wenzelm
parents:
9481
diff
changeset
|
55 |
structure Obtain: OBTAIN = |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
56 |
struct |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
57 |
|
18670 | 58 |
(** obtain_export **) |
59 |
||
18870 | 60 |
(* |
18897 | 61 |
[x, A x] |
62 |
: |
|
63 |
B |
|
64 |
-------- |
|
65 |
B |
|
18870 | 66 |
*) |
20308 | 67 |
fun obtain_export fix_ctxt rule xs _ As thm = |
9468 | 68 |
let |
20308 | 69 |
val thy = ProofContext.theory_of fix_ctxt; |
9468 | 70 |
|
20308 | 71 |
val vs = map (dest_Free o Thm.term_of) xs; |
72 |
val bads = Term.fold_aterms (fn t as Free v => |
|
73 |
if member (op =) vs v then insert (op aconv) t else I | _ => I) (Thm.prop_of thm) []; |
|
74 |
val _ = null bads orelse |
|
75 |
error ("Result contains obtained parameters: " ^ |
|
76 |
space_implode " " (map (ProofContext.string_of_term fix_ctxt) bads)); |
|
77 |
val _ = ObjectLogic.is_judgment thy (Thm.concl_of thm) orelse |
|
78 |
error "Conclusion in obtained context must be object-logic judgment"; |
|
79 |
||
80 |
val ((_, [thm']), ctxt') = Variable.import true [thm] fix_ctxt; |
|
81 |
val prems = Drule.strip_imp_prems (#prop (Thm.crep_thm thm')); |
|
9468 | 82 |
in |
20308 | 83 |
((Drule.implies_elim_list thm' (map Thm.assume prems) |
84 |
|> Drule.implies_intr_list (map Drule.norm_hhf_cterm As) |
|
85 |
|> Drule.forall_intr_list xs) |
|
86 |
COMP rule) |
|
87 |
|> Drule.implies_intr_list prems |
|
88 |
|> singleton (Variable.export ctxt' fix_ctxt) |
|
9468 | 89 |
end; |
90 |
||
91 |
||
92 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
93 |
(** obtain **) |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
94 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
95 |
fun bind_judgment ctxt name = |
18670 | 96 |
let |
20308 | 97 |
val (bind, ctxt') = ProofContext.bind_fixes [name] ctxt; |
18670 | 98 |
val (t as _ $ Free v) = bind (ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) name); |
20308 | 99 |
in ((v, t), ctxt') end; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
100 |
|
18897 | 101 |
val thatN = "that"; |
102 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
103 |
local |
8094 | 104 |
|
18897 | 105 |
fun gen_obtain prep_att prep_vars prep_propp |
106 |
name raw_vars raw_asms int state = |
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
107 |
let |
9468 | 108 |
val _ = Proof.assert_forward_or_chain state; |
20308 | 109 |
val thy = Proof.theory_of state; |
110 |
val cert = Thm.cterm_of thy; |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
111 |
val ctxt = Proof.context_of state; |
17357 | 112 |
val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else []; |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
113 |
|
8543 | 114 |
(*obtain vars*) |
19844 | 115 |
val (vars, vars_ctxt) = prep_vars raw_vars ctxt; |
18670 | 116 |
val (_, fix_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars; |
117 |
val xs = map #1 vars; |
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
118 |
|
8543 | 119 |
(*obtain asms*) |
11890 | 120 |
val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map snd raw_asms); |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19300
diff
changeset
|
121 |
val asm_props = maps (map fst) proppss; |
18678 | 122 |
val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss; |
10464 | 123 |
|
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19844
diff
changeset
|
124 |
val _ = Variable.warn_extra_tfrees fix_ctxt asms_ctxt; |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
125 |
|
12970 | 126 |
(*obtain statements*) |
20085
c5d60752587f
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
20004
diff
changeset
|
127 |
val thesisN = Name.variant xs AutoBind.thesisN; |
20308 | 128 |
val (thesis_var, thesis) = #1 (bind_judgment fix_ctxt thesisN); |
9468 | 129 |
|
20308 | 130 |
val asm_frees = fold Term.add_frees asm_props []; |
131 |
val parms = xs |> map (fn x => |
|
132 |
let val x' = ProofContext.get_skolem fix_ctxt x |
|
133 |
in (x', the_default propT (AList.lookup (op =) asm_frees x')) end); |
|
10582 | 134 |
|
18897 | 135 |
val that_name = if name = "" then thatN else name; |
10582 | 136 |
val that_prop = |
20308 | 137 |
Term.list_all_free (parms, Logic.list_implies (asm_props, thesis)) |
138 |
|> Library.curry Logic.list_rename_params xs; |
|
12970 | 139 |
val obtain_prop = |
140 |
Logic.list_rename_params ([AutoBind.thesisN], |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
141 |
Term.list_all_free ([thesis_var], Logic.mk_implies (that_prop, thesis))); |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
142 |
|
18124 | 143 |
fun after_qed _ = |
17357 | 144 |
Proof.local_qed (NONE, false) |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
145 |
#> Seq.map (`Proof.the_fact #-> (fn rule => |
19844 | 146 |
Proof.fix_i (map2 (fn x => fn (_, T, mx) => (x, T, mx)) xs vars) |
20308 | 147 |
#> Proof.assm_i (obtain_export fix_ctxt rule (map (cert o Free) parms)) asms)); |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
148 |
in |
8094 | 149 |
state |
9468 | 150 |
|> Proof.enter_forward |
19585 | 151 |
|> Proof.have_i NONE (K Seq.single) [(("", []), [(obtain_prop, [])])] int |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
152 |
|> Proof.proof (SOME Method.succeed_text) |> Seq.hd |
19844 | 153 |
|> Proof.fix_i [(thesisN, NONE, NoSyn)] |
19585 | 154 |
|> Proof.assume_i [((that_name, [ContextRules.intro_query NONE]), [(that_prop, [])])] |
16842 | 155 |
|> `Proof.the_facts |
17357 | 156 |
||> Proof.chain_facts chain_facts |
19585 | 157 |
||> Proof.show_i NONE after_qed [(("", []), [(thesis, [])])] false |
18907 | 158 |
|-> Proof.refine_insert |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
159 |
end; |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
160 |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
161 |
in |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
162 |
|
18728 | 163 |
val obtain = gen_obtain Attrib.attribute ProofContext.read_vars ProofContext.read_propp; |
17111 | 164 |
val obtain_i = gen_obtain (K I) ProofContext.cert_vars ProofContext.cert_propp; |
8094 | 165 |
|
166 |
end; |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
167 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
168 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
169 |
|
20308 | 170 |
(** tactical result **) |
171 |
||
172 |
fun check_result ctxt thesis th = |
|
173 |
(case Thm.prems_of th of |
|
174 |
[prem] => |
|
175 |
if Thm.concl_of th aconv thesis andalso |
|
176 |
Logic.strip_assums_concl prem aconv thesis then th |
|
177 |
else error ("Guessed a different clause:\n" ^ ProofContext.string_of_thm ctxt th) |
|
178 |
| [] => error "Goal solved -- nothing guessed." |
|
179 |
| _ => error ("Guess split into several cases:\n" ^ ProofContext.string_of_thm ctxt th)); |
|
180 |
||
181 |
fun result tac facts ctxt = |
|
182 |
let |
|
183 |
val thy = ProofContext.theory_of ctxt; |
|
184 |
val cert = Thm.cterm_of thy; |
|
185 |
||
186 |
val ((thesis_var, thesis), thesis_ctxt) = bind_judgment ctxt AutoBind.thesisN; |
|
187 |
val rule = |
|
188 |
(case SINGLE (Method.insert_tac facts 1 THEN tac thesis_ctxt) (Goal.init (cert thesis)) of |
|
189 |
NONE => raise THM ("Obtain.result: tactic failed", 0, facts) |
|
21605 | 190 |
| SOME th => check_result ctxt thesis (MetaSimplifier.norm_hhf (Goal.conclude th))); |
20308 | 191 |
|
192 |
val closed_rule = Thm.forall_intr (cert (Free thesis_var)) rule; |
|
193 |
val ((_, [rule']), ctxt') = Variable.import false [closed_rule] ctxt; |
|
194 |
val obtain_rule = Thm.forall_elim (cert (Logic.varify (Free thesis_var))) rule'; |
|
195 |
val ((params, stmt), fix_ctxt) = Variable.focus (Thm.cprem_of obtain_rule 1) ctxt'; |
|
196 |
val (prems, ctxt'') = |
|
197 |
Assumption.add_assms (obtain_export fix_ctxt obtain_rule params) |
|
198 |
(Drule.strip_imp_prems stmt) fix_ctxt; |
|
199 |
in ((params, prems), ctxt'') end; |
|
200 |
||
201 |
||
202 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
203 |
(** guess **) |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
204 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
205 |
local |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
206 |
|
20308 | 207 |
fun unify_params vars thesis_var raw_rule ctxt = |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
208 |
let |
18678 | 209 |
val thy = ProofContext.theory_of ctxt; |
19978 | 210 |
val certT = Thm.ctyp_of thy; |
211 |
val cert = Thm.cterm_of thy; |
|
17891 | 212 |
val string_of_typ = ProofContext.string_of_typ ctxt; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
213 |
val string_of_term = setmp show_types true (ProofContext.string_of_term ctxt); |
17891 | 214 |
|
18678 | 215 |
fun err msg th = error (msg ^ ":\n" ^ ProofContext.string_of_thm ctxt th); |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
216 |
|
19978 | 217 |
val maxidx = fold (Term.maxidx_typ o snd o fst) vars ~1; |
19779 | 218 |
val rule = Thm.incr_indexes (maxidx + 1) raw_rule; |
219 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
220 |
val params = RuleCases.strip_params (Logic.nth_prem (1, Thm.prop_of rule)); |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
221 |
val m = length vars; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
222 |
val n = length params; |
19779 | 223 |
val _ = m <= n orelse err "More variables than parameters in obtained rule" rule; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
224 |
|
19779 | 225 |
fun unify ((x, T), (y, U)) (tyenv, max) = Sign.typ_unify thy (T, U) (tyenv, max) |
226 |
handle Type.TUNIFY => |
|
227 |
err ("Failed to unify variable " ^ |
|
228 |
string_of_term (Free (x, Envir.norm_type tyenv T)) ^ " against parameter " ^ |
|
229 |
string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule; |
|
19978 | 230 |
val (tyenv, _) = fold unify (map #1 vars ~~ Library.take (m, params)) |
19779 | 231 |
(Vartab.empty, Int.max (maxidx, Thm.maxidx_of rule)); |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
232 |
val norm_type = Envir.norm_type tyenv; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
233 |
|
19978 | 234 |
val xs = map (apsnd norm_type o fst) vars; |
19779 | 235 |
val ys = map (apsnd norm_type) (Library.drop (m, params)); |
20085
c5d60752587f
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
20004
diff
changeset
|
236 |
val ys' = map Name.internal (Name.variant_list (map fst xs) (map fst ys)) ~~ map #2 ys; |
19978 | 237 |
val terms = map (Drule.mk_term o cert o Free) (xs @ ys'); |
19779 | 238 |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
239 |
val instT = |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
240 |
fold (Term.add_tvarsT o #2) params [] |
19978 | 241 |
|> map (TVar #> (fn T => (certT T, certT (norm_type T)))); |
20308 | 242 |
val closed_rule = rule |
243 |
|> Thm.forall_intr (cert (Free thesis_var)) |
|
244 |
|> Thm.instantiate (instT, []); |
|
17891 | 245 |
|
20308 | 246 |
val ((_, rule' :: terms'), ctxt') = Variable.import false (closed_rule :: terms) ctxt; |
19978 | 247 |
val vars' = |
248 |
map (dest_Free o Thm.term_of o Drule.dest_term) terms' ~~ |
|
249 |
(map snd vars @ replicate (length ys) NoSyn); |
|
20308 | 250 |
val rule'' = Thm.forall_elim (cert (Logic.varify (Free thesis_var))) rule'; |
19978 | 251 |
in ((vars', rule''), ctxt') end; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
252 |
|
18693 | 253 |
fun inferred_type (x, _, mx) ctxt = |
18769 | 254 |
let val ((_, T), ctxt') = ProofContext.inferred_param x ctxt |
19779 | 255 |
in ((x, T, mx), ctxt') end; |
256 |
||
20004
e6d3f2b031e6
guess: proper context for polymorphic parameters;
wenzelm
parents:
19978
diff
changeset
|
257 |
fun polymorphic ctxt vars = |
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19844
diff
changeset
|
258 |
let val Ts = map Logic.dest_type (Variable.polymorphic ctxt (map (Logic.mk_type o #2) vars)) |
19779 | 259 |
in map2 (fn (x, _, mx) => fn T => ((x, T), mx)) vars Ts end; |
18693 | 260 |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
261 |
fun gen_guess prep_vars raw_vars int state = |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
262 |
let |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
263 |
val _ = Proof.assert_forward_or_chain state; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
264 |
val thy = Proof.theory_of state; |
20308 | 265 |
val cert = Thm.cterm_of thy; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
266 |
val ctxt = Proof.context_of state; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
267 |
val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else []; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
268 |
|
20308 | 269 |
val (thesis_var, thesis) = #1 (bind_judgment ctxt AutoBind.thesisN); |
20004
e6d3f2b031e6
guess: proper context for polymorphic parameters;
wenzelm
parents:
19978
diff
changeset
|
270 |
val vars = ctxt |> prep_vars raw_vars |-> fold_map inferred_type |> fst |> polymorphic ctxt; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
271 |
|
19978 | 272 |
fun guess_context raw_rule state' = |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
273 |
let |
19978 | 274 |
val ((parms, rule), ctxt') = |
20308 | 275 |
unify_params vars thesis_var raw_rule (Proof.context_of state'); |
19978 | 276 |
val (bind, _) = ProofContext.bind_fixes (map (#1 o #1) parms) ctxt'; |
277 |
val ts = map (bind o Free o #1) parms; |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
278 |
val ps = map dest_Free ts; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
279 |
val asms = |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
280 |
Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule)) |
19585 | 281 |
|> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), [])); |
19779 | 282 |
val _ = not (null asms) orelse error "Trivial result -- nothing guessed"; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
283 |
in |
19978 | 284 |
state' |
285 |
|> Proof.map_context (K ctxt') |
|
286 |
|> Proof.fix_i (map (fn ((x, T), mx) => (x, SOME T, mx)) parms) |
|
20308 | 287 |
|> `Proof.context_of |-> (fn fix_ctxt => |
288 |
Proof.assm_i (obtain_export fix_ctxt rule (map cert ts)) [(("", []), asms)]) |
|
19978 | 289 |
|> Proof.add_binds_i AutoBind.no_facts |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
290 |
end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
291 |
|
19779 | 292 |
val goal = Var (("guess", 0), propT); |
293 |
fun print_result ctxt' (k, [(s, [_, th])]) = |
|
294 |
ProofDisplay.print_results int ctxt' (k, [(s, [th])]); |
|
21605 | 295 |
val before_qed = SOME (Method.primitive_text (Goal.conclude #> MetaSimplifier.norm_hhf #> |
20308 | 296 |
(fn th => Goal.protect (Conjunction.intr (Drule.mk_term (Thm.cprop_of th)) th)))); |
19779 | 297 |
fun after_qed [[_, res]] = |
20308 | 298 |
Proof.end_block #> guess_context (check_result ctxt thesis res) #> Seq.single; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
299 |
in |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
300 |
state |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
301 |
|> Proof.enter_forward |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
302 |
|> Proof.begin_block |
19844 | 303 |
|> Proof.fix_i [(AutoBind.thesisN, NONE, NoSyn)] |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
304 |
|> Proof.chain_facts chain_facts |
19779 | 305 |
|> Proof.local_goal print_result (K I) (apsnd (rpair I)) |
306 |
"guess" before_qed after_qed [(("", []), [Logic.mk_term goal, goal])] |
|
20308 | 307 |
|> Proof.refine (Method.primitive_text (K (Goal.init (cert thesis)))) |> Seq.hd |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
308 |
end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
309 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
310 |
in |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
311 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
312 |
val guess = gen_guess ProofContext.read_vars; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
313 |
val guess_i = gen_guess ProofContext.cert_vars; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
314 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
315 |
end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
316 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
317 |
end; |