author | wenzelm |
Thu, 09 Oct 2008 20:53:12 +0200 | |
changeset 28545 | 2fb2d48de366 |
parent 28084 | a05ca48ef263 |
child 28965 | 1de908189869 |
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 |
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
43 |
val obtain: string -> (Name.binding * string option * mixfix) list -> |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28080
diff
changeset
|
44 |
(Attrib.binding * (string * string list) list) list -> |
20308 | 45 |
bool -> Proof.state -> Proof.state |
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
46 |
val obtain_i: string -> (Name.binding * typ option * mixfix) list -> |
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
47 |
((Name.binding * attribute list) * (term * term list) list) list -> |
20308 | 48 |
bool -> Proof.state -> Proof.state |
49 |
val result: (Proof.context -> tactic) -> thm list -> Proof.context -> |
|
50 |
(cterm list * thm list) * Proof.context |
|
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
51 |
val guess: (Name.binding * string option * mixfix) list -> bool -> Proof.state -> Proof.state |
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
52 |
val guess_i: (Name.binding * 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 |
*) |
21686 | 67 |
fun eliminate_term ctxt xs tm = |
68 |
let |
|
69 |
val vs = map (dest_Free o Thm.term_of) xs; |
|
70 |
val bads = Term.fold_aterms (fn t as Free v => |
|
71 |
if member (op =) vs v then insert (op aconv) t else I | _ => I) tm []; |
|
72 |
val _ = null bads orelse |
|
73 |
error ("Result contains obtained parameters: " ^ |
|
24920 | 74 |
space_implode " " (map (Syntax.string_of_term ctxt) bads)); |
21686 | 75 |
in tm end; |
76 |
||
77 |
fun eliminate fix_ctxt rule xs As thm = |
|
9468 | 78 |
let |
20308 | 79 |
val thy = ProofContext.theory_of fix_ctxt; |
9468 | 80 |
|
21686 | 81 |
val _ = eliminate_term fix_ctxt xs (Thm.full_prop_of thm); |
20308 | 82 |
val _ = ObjectLogic.is_judgment thy (Thm.concl_of thm) orelse |
83 |
error "Conclusion in obtained context must be object-logic judgment"; |
|
84 |
||
22568
ed7aa5a350ef
renamed Variable.import to import_thms (avoid clash with Alice keywords);
wenzelm
parents:
21686
diff
changeset
|
85 |
val ((_, [thm']), ctxt') = Variable.import_thms true [thm] fix_ctxt; |
20308 | 86 |
val prems = Drule.strip_imp_prems (#prop (Thm.crep_thm thm')); |
9468 | 87 |
in |
20308 | 88 |
((Drule.implies_elim_list thm' (map Thm.assume prems) |
89 |
|> Drule.implies_intr_list (map Drule.norm_hhf_cterm As) |
|
90 |
|> Drule.forall_intr_list xs) |
|
91 |
COMP rule) |
|
92 |
|> Drule.implies_intr_list prems |
|
93 |
|> singleton (Variable.export ctxt' fix_ctxt) |
|
9468 | 94 |
end; |
95 |
||
21686 | 96 |
fun obtain_export ctxt rule xs _ As = |
97 |
(eliminate ctxt rule xs As, eliminate_term ctxt xs); |
|
98 |
||
9468 | 99 |
|
100 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
101 |
(** obtain **) |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
102 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
103 |
fun bind_judgment ctxt name = |
18670 | 104 |
let |
20308 | 105 |
val (bind, ctxt') = ProofContext.bind_fixes [name] ctxt; |
18670 | 106 |
val (t as _ $ Free v) = bind (ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) name); |
20308 | 107 |
in ((v, t), ctxt') end; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
108 |
|
18897 | 109 |
val thatN = "that"; |
110 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
111 |
local |
8094 | 112 |
|
18897 | 113 |
fun gen_obtain prep_att prep_vars prep_propp |
114 |
name raw_vars raw_asms int state = |
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
115 |
let |
9468 | 116 |
val _ = Proof.assert_forward_or_chain state; |
20308 | 117 |
val thy = Proof.theory_of state; |
118 |
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
|
119 |
val ctxt = Proof.context_of state; |
17357 | 120 |
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
|
121 |
|
8543 | 122 |
(*obtain vars*) |
19844 | 123 |
val (vars, vars_ctxt) = prep_vars raw_vars ctxt; |
18670 | 124 |
val (_, fix_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars; |
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
125 |
val xs = map (Name.name_of o #1) vars; |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
126 |
|
8543 | 127 |
(*obtain asms*) |
11890 | 128 |
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
|
129 |
val asm_props = maps (map fst) proppss; |
18678 | 130 |
val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss; |
10464 | 131 |
|
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19844
diff
changeset
|
132 |
val _ = Variable.warn_extra_tfrees fix_ctxt asms_ctxt; |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
133 |
|
12970 | 134 |
(*obtain statements*) |
20085
c5d60752587f
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
20004
diff
changeset
|
135 |
val thesisN = Name.variant xs AutoBind.thesisN; |
20308 | 136 |
val (thesis_var, thesis) = #1 (bind_judgment fix_ctxt thesisN); |
9468 | 137 |
|
20308 | 138 |
val asm_frees = fold Term.add_frees asm_props []; |
139 |
val parms = xs |> map (fn x => |
|
140 |
let val x' = ProofContext.get_skolem fix_ctxt x |
|
141 |
in (x', the_default propT (AList.lookup (op =) asm_frees x')) end); |
|
10582 | 142 |
|
18897 | 143 |
val that_name = if name = "" then thatN else name; |
10582 | 144 |
val that_prop = |
20308 | 145 |
Term.list_all_free (parms, Logic.list_implies (asm_props, thesis)) |
146 |
|> Library.curry Logic.list_rename_params xs; |
|
12970 | 147 |
val obtain_prop = |
148 |
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
|
149 |
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
|
150 |
|
18124 | 151 |
fun after_qed _ = |
17357 | 152 |
Proof.local_qed (NONE, false) |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
153 |
#> Seq.map (`Proof.the_fact #-> (fn rule => |
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
154 |
Proof.fix_i vars |
20308 | 155 |
#> 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
|
156 |
in |
8094 | 157 |
state |
9468 | 158 |
|> Proof.enter_forward |
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
159 |
|> Proof.have_i NONE (K Seq.single) [((Name.no_binding, []), [(obtain_prop, [])])] int |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
160 |
|> Proof.proof (SOME Method.succeed_text) |> Seq.hd |
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
161 |
|> Proof.fix_i [(Name.binding thesisN, NONE, NoSyn)] |
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
162 |
|> Proof.assume_i |
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
163 |
[((Name.binding that_name, [ContextRules.intro_query NONE]), [(that_prop, [])])] |
16842 | 164 |
|> `Proof.the_facts |
17357 | 165 |
||> Proof.chain_facts chain_facts |
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
166 |
||> Proof.show_i NONE after_qed [((Name.no_binding, []), [(thesis, [])])] false |
18907 | 167 |
|-> Proof.refine_insert |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
168 |
end; |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
169 |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
170 |
in |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
171 |
|
18728 | 172 |
val obtain = gen_obtain Attrib.attribute ProofContext.read_vars ProofContext.read_propp; |
17111 | 173 |
val obtain_i = gen_obtain (K I) ProofContext.cert_vars ProofContext.cert_propp; |
8094 | 174 |
|
175 |
end; |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
176 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
177 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
178 |
|
20308 | 179 |
(** tactical result **) |
180 |
||
181 |
fun check_result ctxt thesis th = |
|
182 |
(case Thm.prems_of th of |
|
183 |
[prem] => |
|
184 |
if Thm.concl_of th aconv thesis andalso |
|
185 |
Logic.strip_assums_concl prem aconv thesis then th |
|
186 |
else error ("Guessed a different clause:\n" ^ ProofContext.string_of_thm ctxt th) |
|
187 |
| [] => error "Goal solved -- nothing guessed." |
|
188 |
| _ => error ("Guess split into several cases:\n" ^ ProofContext.string_of_thm ctxt th)); |
|
189 |
||
190 |
fun result tac facts ctxt = |
|
191 |
let |
|
192 |
val thy = ProofContext.theory_of ctxt; |
|
193 |
val cert = Thm.cterm_of thy; |
|
194 |
||
195 |
val ((thesis_var, thesis), thesis_ctxt) = bind_judgment ctxt AutoBind.thesisN; |
|
196 |
val rule = |
|
197 |
(case SINGLE (Method.insert_tac facts 1 THEN tac thesis_ctxt) (Goal.init (cert thesis)) of |
|
198 |
NONE => raise THM ("Obtain.result: tactic failed", 0, facts) |
|
21605 | 199 |
| SOME th => check_result ctxt thesis (MetaSimplifier.norm_hhf (Goal.conclude th))); |
20308 | 200 |
|
201 |
val closed_rule = Thm.forall_intr (cert (Free thesis_var)) rule; |
|
22568
ed7aa5a350ef
renamed Variable.import to import_thms (avoid clash with Alice keywords);
wenzelm
parents:
21686
diff
changeset
|
202 |
val ((_, [rule']), ctxt') = Variable.import_thms false [closed_rule] ctxt; |
20308 | 203 |
val obtain_rule = Thm.forall_elim (cert (Logic.varify (Free thesis_var))) rule'; |
204 |
val ((params, stmt), fix_ctxt) = Variable.focus (Thm.cprem_of obtain_rule 1) ctxt'; |
|
205 |
val (prems, ctxt'') = |
|
206 |
Assumption.add_assms (obtain_export fix_ctxt obtain_rule params) |
|
207 |
(Drule.strip_imp_prems stmt) fix_ctxt; |
|
208 |
in ((params, prems), ctxt'') end; |
|
209 |
||
210 |
||
211 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
212 |
(** guess **) |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
213 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
214 |
local |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
215 |
|
20308 | 216 |
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
|
217 |
let |
18678 | 218 |
val thy = ProofContext.theory_of ctxt; |
19978 | 219 |
val certT = Thm.ctyp_of thy; |
220 |
val cert = Thm.cterm_of thy; |
|
24920 | 221 |
val string_of_typ = Syntax.string_of_typ ctxt; |
222 |
val string_of_term = setmp show_types true (Syntax.string_of_term ctxt); |
|
17891 | 223 |
|
18678 | 224 |
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
|
225 |
|
19978 | 226 |
val maxidx = fold (Term.maxidx_typ o snd o fst) vars ~1; |
19779 | 227 |
val rule = Thm.incr_indexes (maxidx + 1) raw_rule; |
228 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
229 |
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
|
230 |
val m = length vars; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
231 |
val n = length params; |
19779 | 232 |
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
|
233 |
|
19779 | 234 |
fun unify ((x, T), (y, U)) (tyenv, max) = Sign.typ_unify thy (T, U) (tyenv, max) |
235 |
handle Type.TUNIFY => |
|
236 |
err ("Failed to unify variable " ^ |
|
237 |
string_of_term (Free (x, Envir.norm_type tyenv T)) ^ " against parameter " ^ |
|
238 |
string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule; |
|
19978 | 239 |
val (tyenv, _) = fold unify (map #1 vars ~~ Library.take (m, params)) |
19779 | 240 |
(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
|
241 |
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
|
242 |
|
19978 | 243 |
val xs = map (apsnd norm_type o fst) vars; |
19779 | 244 |
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
|
245 |
val ys' = map Name.internal (Name.variant_list (map fst xs) (map fst ys)) ~~ map #2 ys; |
19978 | 246 |
val terms = map (Drule.mk_term o cert o Free) (xs @ ys'); |
19779 | 247 |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
248 |
val instT = |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
249 |
fold (Term.add_tvarsT o #2) params [] |
19978 | 250 |
|> map (TVar #> (fn T => (certT T, certT (norm_type T)))); |
20308 | 251 |
val closed_rule = rule |
252 |
|> Thm.forall_intr (cert (Free thesis_var)) |
|
253 |
|> Thm.instantiate (instT, []); |
|
17891 | 254 |
|
22568
ed7aa5a350ef
renamed Variable.import to import_thms (avoid clash with Alice keywords);
wenzelm
parents:
21686
diff
changeset
|
255 |
val ((_, rule' :: terms'), ctxt') = Variable.import_thms false (closed_rule :: terms) ctxt; |
19978 | 256 |
val vars' = |
257 |
map (dest_Free o Thm.term_of o Drule.dest_term) terms' ~~ |
|
258 |
(map snd vars @ replicate (length ys) NoSyn); |
|
20308 | 259 |
val rule'' = Thm.forall_elim (cert (Logic.varify (Free thesis_var))) rule'; |
19978 | 260 |
in ((vars', rule''), ctxt') end; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
261 |
|
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
262 |
fun inferred_type (binding, _, mx) ctxt = |
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
263 |
let |
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
264 |
val x = Name.name_of binding; |
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
265 |
val (T, ctxt') = ProofContext.inferred_param x ctxt |
19779 | 266 |
in ((x, T, mx), ctxt') end; |
267 |
||
20004
e6d3f2b031e6
guess: proper context for polymorphic parameters;
wenzelm
parents:
19978
diff
changeset
|
268 |
fun polymorphic ctxt vars = |
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19844
diff
changeset
|
269 |
let val Ts = map Logic.dest_type (Variable.polymorphic ctxt (map (Logic.mk_type o #2) vars)) |
19779 | 270 |
in map2 (fn (x, _, mx) => fn T => ((x, T), mx)) vars Ts end; |
18693 | 271 |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
272 |
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
|
273 |
let |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
274 |
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
|
275 |
val thy = Proof.theory_of state; |
20308 | 276 |
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
|
277 |
val ctxt = Proof.context_of state; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
278 |
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
|
279 |
|
20308 | 280 |
val (thesis_var, thesis) = #1 (bind_judgment ctxt AutoBind.thesisN); |
20004
e6d3f2b031e6
guess: proper context for polymorphic parameters;
wenzelm
parents:
19978
diff
changeset
|
281 |
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
|
282 |
|
19978 | 283 |
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
|
284 |
let |
19978 | 285 |
val ((parms, rule), ctxt') = |
20308 | 286 |
unify_params vars thesis_var raw_rule (Proof.context_of state'); |
19978 | 287 |
val (bind, _) = ProofContext.bind_fixes (map (#1 o #1) parms) ctxt'; |
288 |
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
|
289 |
val ps = map dest_Free ts; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
290 |
val asms = |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
291 |
Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule)) |
19585 | 292 |
|> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), [])); |
19779 | 293 |
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
|
294 |
in |
19978 | 295 |
state' |
296 |
|> Proof.map_context (K ctxt') |
|
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
297 |
|> Proof.fix_i (map (fn ((x, T), mx) => (Name.binding x, SOME T, mx)) parms) |
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
298 |
|> `Proof.context_of |-> (fn fix_ctxt => Proof.assm_i |
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
299 |
(obtain_export fix_ctxt rule (map cert ts)) [((Name.no_binding, []), asms)]) |
19978 | 300 |
|> 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
|
301 |
end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
302 |
|
19779 | 303 |
val goal = Var (("guess", 0), propT); |
304 |
fun print_result ctxt' (k, [(s, [_, th])]) = |
|
305 |
ProofDisplay.print_results int ctxt' (k, [(s, [th])]); |
|
21605 | 306 |
val before_qed = SOME (Method.primitive_text (Goal.conclude #> MetaSimplifier.norm_hhf #> |
20308 | 307 |
(fn th => Goal.protect (Conjunction.intr (Drule.mk_term (Thm.cprop_of th)) th)))); |
19779 | 308 |
fun after_qed [[_, res]] = |
20308 | 309 |
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
|
310 |
in |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
311 |
state |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
312 |
|> Proof.enter_forward |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
313 |
|> Proof.begin_block |
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
314 |
|> Proof.fix_i [(Name.binding AutoBind.thesisN, NONE, NoSyn)] |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
315 |
|> Proof.chain_facts chain_facts |
19779 | 316 |
|> Proof.local_goal print_result (K I) (apsnd (rpair I)) |
28080
4723eb2456ce
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
24920
diff
changeset
|
317 |
"guess" before_qed after_qed [((Name.no_binding, []), [Logic.mk_term goal, goal])] |
20308 | 318 |
|> 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
|
319 |
end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
320 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
321 |
in |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
322 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
323 |
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
|
324 |
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
|
325 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
326 |
end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
327 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
328 |
end; |