author | wenzelm |
Thu, 02 Feb 2006 16:31:35 +0100 | |
changeset 18907 | f984f22f1cb4 |
parent 18897 | b31293969d4f |
child 19300 | 7689f81f8996 |
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 |
18897 | 42 |
val obtain: string -> (string * string option) list -> |
17111 | 43 |
((string * Attrib.src list) * (string * (string list * string list)) list) list |
17357 | 44 |
-> bool -> Proof.state -> Proof.state |
18897 | 45 |
val obtain_i: string -> (string * typ option) list -> |
18728 | 46 |
((string * attribute list) * (term * (term list * term list)) list) list |
17357 | 47 |
-> bool -> Proof.state -> Proof.state |
18670 | 48 |
val guess: (string * string option) list -> bool -> Proof.state -> Proof.state |
49 |
val guess_i: (string * typ option) list -> bool -> Proof.state -> Proof.state |
|
18897 | 50 |
val statement: (string * ((string * 'typ option) list * 'term list)) list -> |
51 |
(('typ, 'term, 'fact) Element.ctxt list * |
|
18907 | 52 |
((string * Attrib.src list) * ('term * ('term list * 'term list)) list) list) * |
53 |
(((string * Attrib.src list) * (term * (term list * term list)) list) list -> Proof.context -> |
|
54 |
(((string * Attrib.src list) * (term * (term list * term list)) list) list * thm list) * |
|
18897 | 55 |
Proof.context) |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
56 |
end; |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
57 |
|
10379
93630e0c5ae9
improved handling of "that": insert into goal, only declare as Pure "intro";
wenzelm
parents:
9481
diff
changeset
|
58 |
structure Obtain: OBTAIN = |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
59 |
struct |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
60 |
|
8094 | 61 |
|
18670 | 62 |
(** obtain_export **) |
63 |
||
18870 | 64 |
(* |
18897 | 65 |
[x, A x] |
66 |
: |
|
67 |
B |
|
68 |
-------- |
|
69 |
B |
|
18870 | 70 |
*) |
18678 | 71 |
fun obtain_export ctxt parms rule cprops thm = |
9468 | 72 |
let |
17111 | 73 |
val {thy, prop, maxidx, ...} = Thm.rep_thm thm; |
74 |
val cparms = map (Thm.cterm_of thy) parms; |
|
9468 | 75 |
|
76 |
val thm' = thm |
|
18040
c67505cdecad
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17974
diff
changeset
|
77 |
|> Drule.implies_intr_protected cprops |
9468 | 78 |
|> Drule.forall_intr_list cparms |
79 |
|> Drule.forall_elim_vars (maxidx + 1); |
|
18040
c67505cdecad
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17974
diff
changeset
|
80 |
val elim_tacs = replicate (length cprops) (Tactic.etac Drule.protectI); |
9468 | 81 |
|
82 |
val concl = Logic.strip_assums_concl prop; |
|
83 |
val bads = parms inter (Term.term_frees concl); |
|
84 |
in |
|
85 |
if not (null bads) then |
|
18678 | 86 |
error ("Conclusion contains obtained parameters: " ^ |
87 |
space_implode " " (map (ProofContext.string_of_term ctxt) bads)) |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
88 |
else if not (ObjectLogic.is_judgment thy concl) then |
18678 | 89 |
error "Conclusion in obtained context must be object-logic judgments" |
9468 | 90 |
else (Tactic.rtac thm' THEN' RANGE elim_tacs) 1 rule |
91 |
end; |
|
92 |
||
93 |
||
94 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
95 |
(** obtain **) |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
96 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
97 |
fun bind_judgment ctxt name = |
18670 | 98 |
let |
99 |
val (bind, _) = ProofContext.bind_fixes [name] ctxt; |
|
100 |
val (t as _ $ Free v) = bind (ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) name); |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
101 |
in (v, t) end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
102 |
|
18897 | 103 |
val thatN = "that"; |
104 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
105 |
local |
8094 | 106 |
|
18897 | 107 |
fun gen_obtain prep_att prep_vars prep_propp |
108 |
name raw_vars raw_asms int state = |
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
109 |
let |
9468 | 110 |
val _ = Proof.assert_forward_or_chain state; |
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; |
18678 | 112 |
val thy = Proof.theory_of state; |
17357 | 113 |
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
|
114 |
|
8543 | 115 |
(*obtain vars*) |
18670 | 116 |
val (vars, vars_ctxt) = prep_vars (map Syntax.no_syn raw_vars) ctxt; |
117 |
val (_, fix_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars; |
|
118 |
val xs = map #1 vars; |
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
119 |
|
8543 | 120 |
(*obtain asms*) |
11890 | 121 |
val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map snd raw_asms); |
15570 | 122 |
val asm_props = List.concat (map (map fst) proppss); |
18678 | 123 |
val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss; |
10464 | 124 |
|
10582 | 125 |
val _ = ProofContext.warn_extra_tfrees fix_ctxt asms_ctxt; |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
126 |
|
12970 | 127 |
(*obtain statements*) |
16606 | 128 |
val thesisN = Term.variant xs AutoBind.thesisN; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
129 |
val (thesis_var, thesis) = bind_judgment fix_ctxt thesisN; |
9468 | 130 |
|
10582 | 131 |
fun occs_var x = Library.get_first (fn t => |
18151 | 132 |
Term.find_free t (ProofContext.get_skolem fix_ctxt x)) asm_props; |
10582 | 133 |
val raw_parms = map occs_var xs; |
15570 | 134 |
val parms = List.mapPartial I raw_parms; |
10582 | 135 |
val parm_names = |
15570 | 136 |
List.mapPartial (fn (SOME (Free a), x) => SOME (a, x) | _ => NONE) (raw_parms ~~ xs); |
10582 | 137 |
|
18897 | 138 |
val that_name = if name = "" then thatN else name; |
10582 | 139 |
val that_prop = |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
140 |
Term.list_all_free (map #1 parm_names, Logic.list_implies (asm_props, thesis)) |
10582 | 141 |
|> Library.curry Logic.list_rename_params (map #2 parm_names); |
12970 | 142 |
val obtain_prop = |
143 |
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
|
144 |
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
|
145 |
|
18124 | 146 |
fun after_qed _ = |
17357 | 147 |
Proof.local_qed (NONE, false) |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
148 |
#> Seq.map (`Proof.the_fact #-> (fn rule => |
18693 | 149 |
Proof.fix_i (xs ~~ map #2 vars) |
18678 | 150 |
#> Proof.assm_i (K (obtain_export ctxt parms rule)) asms)); |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
151 |
in |
8094 | 152 |
state |
9468 | 153 |
|> Proof.enter_forward |
18124 | 154 |
|> 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
|
155 |
|> Proof.proof (SOME Method.succeed_text) |> Seq.hd |
18670 | 156 |
|> Proof.fix_i [(thesisN, NONE)] |
18897 | 157 |
|> Proof.assume_i [((that_name, [ContextRules.intro_query NONE]), [(that_prop, ([], []))])] |
16842 | 158 |
|> `Proof.the_facts |
17357 | 159 |
||> Proof.chain_facts chain_facts |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
160 |
||> Proof.show_i NONE after_qed [(("", []), [(thesis, ([], []))])] false |
18907 | 161 |
|-> Proof.refine_insert |
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
162 |
end; |
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
163 |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
164 |
in |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
165 |
|
18728 | 166 |
val obtain = gen_obtain Attrib.attribute ProofContext.read_vars ProofContext.read_propp; |
17111 | 167 |
val obtain_i = gen_obtain (K I) ProofContext.cert_vars ProofContext.cert_propp; |
8094 | 168 |
|
169 |
end; |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
170 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
171 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
172 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
173 |
(** guess **) |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
174 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
175 |
local |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
176 |
|
18678 | 177 |
fun match_params ctxt vars rule = |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
178 |
let |
18678 | 179 |
val thy = ProofContext.theory_of ctxt; |
17891 | 180 |
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
|
181 |
val string_of_term = setmp show_types true (ProofContext.string_of_term ctxt); |
17891 | 182 |
|
18678 | 183 |
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
|
184 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
185 |
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
|
186 |
val m = length vars; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
187 |
val n = length params; |
17891 | 188 |
val _ = conditional (m > n) |
189 |
(fn () => 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
|
190 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
191 |
fun match ((x, SOME T), (y, U)) tyenv = |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
192 |
((x, T), Sign.typ_match thy (U, T) tyenv handle Type.TYPE_MATCH => |
17891 | 193 |
err ("Failed to match variable " ^ |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
194 |
string_of_term (Free (x, T)) ^ " against parameter " ^ |
17891 | 195 |
string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule) |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
196 |
| match ((x, NONE), (_, U)) tyenv = ((x, U), tyenv); |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
197 |
val (xs, tyenv) = fold_map match (vars ~~ Library.take (m, params)) Vartab.empty; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
198 |
val ys = Library.drop (m, params); |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
199 |
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
|
200 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
201 |
val xs' = xs |> map (apsnd norm_type); |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
202 |
val ys' = |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
203 |
map Syntax.internal (Term.variantlist (map fst ys, map fst xs)) ~~ |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
204 |
map (norm_type o snd) ys; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
205 |
val instT = |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
206 |
fold (Term.add_tvarsT o #2) params [] |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
207 |
|> map (TVar #> (fn T => (Thm.ctyp_of thy T, Thm.ctyp_of thy (norm_type T)))); |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
208 |
val rule' = rule |> Thm.instantiate (instT, []); |
17891 | 209 |
|
210 |
val tvars = Drule.tvars_of rule'; |
|
211 |
val vars = fold (remove op =) (Term.add_vars (Thm.concl_of rule') []) (Drule.vars_of rule'); |
|
212 |
val _ = |
|
213 |
if null tvars andalso null vars then () |
|
214 |
else err ("Illegal schematic variable(s) " ^ |
|
215 |
commas (map (string_of_typ o TVar) tvars @ map (string_of_term o Var) vars) ^ " in") rule'; |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
216 |
in (xs' @ ys', rule') end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
217 |
|
18693 | 218 |
fun inferred_type (x, _, mx) ctxt = |
18769 | 219 |
let val ((_, T), ctxt') = ProofContext.inferred_param x ctxt |
18693 | 220 |
in ((x, SOME T, mx), ctxt') end; |
221 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
222 |
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
|
223 |
let |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
224 |
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
|
225 |
val thy = Proof.theory_of state; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
226 |
val ctxt = Proof.context_of state; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
227 |
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
|
228 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
229 |
val (thesis_var, thesis) = bind_judgment ctxt AutoBind.thesisN; |
18693 | 230 |
val (vars, _) = ctxt |> prep_vars (map Syntax.no_syn raw_vars) |-> fold_map inferred_type; |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
231 |
|
17974 | 232 |
fun check_result th = |
233 |
(case Thm.prems_of th of |
|
234 |
[prem] => |
|
235 |
if Thm.concl_of th aconv thesis andalso |
|
236 |
Logic.strip_assums_concl prem aconv thesis then () |
|
18678 | 237 |
else error ("Guessed a different clause:\n" ^ ProofContext.string_of_thm ctxt th) |
238 |
| [] => error "Goal solved -- nothing guessed." |
|
239 |
| _ => error ("Guess split into several cases:\n" ^ ProofContext.string_of_thm ctxt th)); |
|
17891 | 240 |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
241 |
fun guess_context raw_rule = |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
242 |
let |
18678 | 243 |
val (parms, rule) = match_params ctxt (map (fn (x, T, _) => (x, T)) vars) raw_rule; |
18670 | 244 |
val (bind, _) = ProofContext.bind_fixes (map #1 parms) ctxt; |
245 |
val ts = map (bind o Free) parms; |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
246 |
val ps = map dest_Free ts; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
247 |
val asms = |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
248 |
Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule)) |
18185 | 249 |
|> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), ([], []))); |
18678 | 250 |
val _ = conditional (null asms) (fn () => error "Trivial result -- nothing guessed"); |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
251 |
in |
18670 | 252 |
Proof.fix_i (map (apsnd SOME) parms) |
18678 | 253 |
#> Proof.assm_i (K (obtain_export ctxt ts rule)) [(("", []), asms)] |
17974 | 254 |
#> 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
|
255 |
end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
256 |
|
18040
c67505cdecad
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17974
diff
changeset
|
257 |
val before_qed = SOME (Method.primitive_text (Goal.conclude #> Goal.protect)); |
18124 | 258 |
fun after_qed [[res]] = |
17974 | 259 |
(check_result res; Proof.end_block #> Seq.map (`Proof.the_fact #-> guess_context)); |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
260 |
in |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
261 |
state |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
262 |
|> Proof.enter_forward |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
263 |
|> Proof.begin_block |
18670 | 264 |
|> Proof.fix_i [(AutoBind.thesisN, NONE)] |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
265 |
|> Proof.chain_facts chain_facts |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
266 |
|> Proof.local_goal (ProofDisplay.print_results int) (K I) (apsnd (rpair I)) |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
267 |
"guess" before_qed after_qed [(("", []), [Var (("guess", 0), propT)])] |
18151 | 268 |
|> Proof.refine (Method.primitive_text (K (Goal.init (Thm.cterm_of thy thesis)))) |> Seq.hd |
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
269 |
end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
270 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
271 |
in |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
272 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
273 |
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
|
274 |
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
|
275 |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
276 |
end; |
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
277 |
|
18897 | 278 |
|
279 |
||
280 |
(** statements with several cases **) |
|
281 |
||
282 |
fun statement cases = |
|
283 |
let |
|
18907 | 284 |
val names = |
285 |
cases |> map_index (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name); |
|
18897 | 286 |
val elems = cases |> map (fn (_, (vars, _)) => |
287 |
Element.Constrains (vars |> List.mapPartial (fn (x, SOME T) => SOME (x, T) | _ => NONE))); |
|
288 |
val concl = cases |> map (fn (_, (_, props)) => (("", []), map (rpair ([], [])) props)); |
|
289 |
||
290 |
fun mk_stmt stmt ctxt = |
|
291 |
let |
|
292 |
val thesis = |
|
293 |
ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN; |
|
18907 | 294 |
val atts = map Attrib.internal |
295 |
[RuleCases.consumes (~ (length cases)), RuleCases.case_names names]; |
|
296 |
||
18897 | 297 |
fun assume_case ((name, (vars, _)), (_, propp)) ctxt' = |
298 |
let |
|
299 |
val xs = map fst vars; |
|
300 |
val props = map fst propp; |
|
301 |
val (parms, ctxt'') = |
|
302 |
ctxt' |
|
303 |
|> fold ProofContext.declare_term props |
|
304 |
|> fold_map ProofContext.inferred_param xs; |
|
305 |
val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis)); |
|
306 |
in |
|
307 |
ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs)); |
|
308 |
ctxt' |> ProofContext.add_assms_i ProofContext.assume_export |
|
309 |
[((name, [ContextRules.intro_query NONE]), [(asm, ([], []))])] |
|
310 |
|>> (fn [(_, [th])] => th) |
|
311 |
end; |
|
312 |
val (ths, ctxt') = ctxt |
|
313 |
|> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)]) |
|
314 |
|> fold_map assume_case (cases ~~ stmt) |
|
315 |
|-> (fn ths => ProofContext.note_thmss_i [((thatN, []), [(ths, [])])] #> #2 #> pair ths); |
|
18907 | 316 |
in (([(("", atts), [(thesis, ([], []))])], ths), ctxt') end; |
18897 | 317 |
in ((elems, concl), mk_stmt) end; |
318 |
||
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
319 |
end; |