| author | wenzelm |
| Wed, 26 Jul 2006 19:37:41 +0200 | |
| changeset 20218 | be3bfb0699ba |
| parent 20201 | 24b49cbd438b |
| child 20224 | 9c40a144ee0e |
| 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 |
| 19844 | 42 |
val obtain: string -> (string * string option * mixfix) list -> |
| 19585 | 43 |
((string * Attrib.src list) * (string * string list) list) list |
| 17357 | 44 |
-> bool -> Proof.state -> Proof.state |
| 19844 | 45 |
val obtain_i: string -> (string * typ option * mixfix) list -> |
| 19585 | 46 |
((string * attribute list) * (term * term list) list) list |
| 17357 | 47 |
-> bool -> Proof.state -> Proof.state |
| 19844 | 48 |
val guess: (string * string option * mixfix) list -> bool -> Proof.state -> Proof.state |
49 |
val guess_i: (string * typ option * mixfix) 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 *
|
|
| 19585 | 52 |
((string * Attrib.src list) * ('term * 'term list) list) list) *
|
53 |
(((string * Attrib.src list) * (term * term list) list) list -> Proof.context -> |
|
54 |
(((string * Attrib.src list) * (term * term list) list) list * thm list) * Proof.context) |
|
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
55 |
end; |
|
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
56 |
|
|
10379
93630e0c5ae9
improved handling of "that": insert into goal, only declare as Pure "intro";
wenzelm
parents:
9481
diff
changeset
|
57 |
structure Obtain: OBTAIN = |
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
58 |
struct |
|
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
59 |
|
| 8094 | 60 |
|
| 18670 | 61 |
(** obtain_export **) |
62 |
||
| 18870 | 63 |
(* |
| 18897 | 64 |
[x, A x] |
65 |
: |
|
66 |
B |
|
67 |
-------- |
|
68 |
B |
|
| 18870 | 69 |
*) |
| 18678 | 70 |
fun obtain_export ctxt parms rule cprops thm = |
| 9468 | 71 |
let |
| 19978 | 72 |
val {thy, prop, ...} = Thm.rep_thm thm;
|
73 |
val concl = Logic.strip_assums_concl prop; |
|
74 |
val bads = Term.fold_aterms (fn v as Free (x, _) => |
|
75 |
if member (op =) parms x then insert (op aconv) v else I | _ => I) concl []; |
|
| 9468 | 76 |
|
|
20004
e6d3f2b031e6
guess: proper context for polymorphic parameters;
wenzelm
parents:
19978
diff
changeset
|
77 |
val thm' = thm |> Drule.implies_intr_protected cprops |> Drule.generalize ([], parms); |
|
18040
c67505cdecad
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17974
diff
changeset
|
78 |
val elim_tacs = replicate (length cprops) (Tactic.etac Drule.protectI); |
| 9468 | 79 |
in |
80 |
if not (null bads) then |
|
| 18678 | 81 |
error ("Conclusion contains obtained parameters: " ^
|
82 |
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
|
83 |
else if not (ObjectLogic.is_judgment thy concl) then |
| 19978 | 84 |
error "Conclusion in obtained context must be object-logic judgment" |
|
20004
e6d3f2b031e6
guess: proper context for polymorphic parameters;
wenzelm
parents:
19978
diff
changeset
|
85 |
else (Tactic.rtac thm' THEN' RANGE elim_tacs) 1 rule |
| 9468 | 86 |
end; |
87 |
||
88 |
||
89 |
||
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
90 |
(** obtain **) |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
91 |
|
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
92 |
fun bind_judgment ctxt name = |
| 18670 | 93 |
let |
94 |
val (bind, _) = ProofContext.bind_fixes [name] ctxt; |
|
95 |
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
|
96 |
in (v, t) end; |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
97 |
|
| 18897 | 98 |
val thatN = "that"; |
99 |
||
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
100 |
local |
| 8094 | 101 |
|
| 20201 | 102 |
fun find_free t x = |
103 |
let |
|
104 |
exception Found of term; |
|
105 |
fun find (t as Free (x', _)) = if x = x' then raise Found t else I |
|
106 |
| find _ = I; |
|
107 |
in (fold_aterms find t (); NONE) handle Found v => SOME v end; |
|
108 |
||
| 18897 | 109 |
fun gen_obtain prep_att prep_vars prep_propp |
110 |
name raw_vars raw_asms int state = |
|
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
111 |
let |
| 9468 | 112 |
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
|
113 |
val ctxt = Proof.context_of state; |
| 18678 | 114 |
val thy = Proof.theory_of state; |
| 17357 | 115 |
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
|
116 |
|
| 8543 | 117 |
(*obtain vars*) |
| 19844 | 118 |
val (vars, vars_ctxt) = prep_vars raw_vars ctxt; |
| 18670 | 119 |
val (_, fix_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars; |
120 |
val xs = map #1 vars; |
|
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
121 |
|
| 8543 | 122 |
(*obtain asms*) |
| 11890 | 123 |
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
|
124 |
val asm_props = maps (map fst) proppss; |
| 18678 | 125 |
val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss; |
| 10464 | 126 |
|
|
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19844
diff
changeset
|
127 |
val _ = Variable.warn_extra_tfrees fix_ctxt asms_ctxt; |
|
7674
99305245f6bd
The 'obtain' language element -- achieves (eliminated) existential
wenzelm
parents:
diff
changeset
|
128 |
|
| 12970 | 129 |
(*obtain statements*) |
|
20085
c5d60752587f
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
20004
diff
changeset
|
130 |
val thesisN = Name.variant xs AutoBind.thesisN; |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
131 |
val (thesis_var, thesis) = bind_judgment fix_ctxt thesisN; |
| 9468 | 132 |
|
| 10582 | 133 |
fun occs_var x = Library.get_first (fn t => |
| 20201 | 134 |
find_free t (ProofContext.get_skolem fix_ctxt x)) asm_props; |
| 19978 | 135 |
val parms = |
136 |
map_filter (fn (SOME (Free a), x) => SOME (a, x) | _ => NONE) (map occs_var xs ~~ xs); |
|
| 10582 | 137 |
|
| 18897 | 138 |
val that_name = if name = "" then thatN else name; |
| 10582 | 139 |
val that_prop = |
| 19978 | 140 |
Term.list_all_free (map #1 parms, Logic.list_implies (asm_props, thesis)) |
141 |
|> Library.curry Logic.list_rename_params (map #2 parms); |
|
| 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 => |
| 19844 | 149 |
Proof.fix_i (map2 (fn x => fn (_, T, mx) => (x, T, mx)) xs vars) |
| 19978 | 150 |
#> Proof.assm_i (K (obtain_export ctxt (map (#1 o #1) 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 |
| 19585 | 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 |
| 19844 | 156 |
|> Proof.fix_i [(thesisN, NONE, NoSyn)] |
| 19585 | 157 |
|> Proof.assume_i [((that_name, [ContextRules.intro_query NONE]), [(that_prop, [])])] |
| 16842 | 158 |
|> `Proof.the_facts |
| 17357 | 159 |
||> Proof.chain_facts chain_facts |
| 19585 | 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 |
|
| 19978 | 177 |
fun unify_params vars thesis_name raw_rule ctxt = |
|
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; |
| 19978 | 180 |
val certT = Thm.ctyp_of thy; |
181 |
val cert = Thm.cterm_of thy; |
|
| 17891 | 182 |
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
|
183 |
val string_of_term = setmp show_types true (ProofContext.string_of_term ctxt); |
| 17891 | 184 |
|
| 18678 | 185 |
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
|
186 |
|
| 19978 | 187 |
val maxidx = fold (Term.maxidx_typ o snd o fst) vars ~1; |
| 19779 | 188 |
val rule = Thm.incr_indexes (maxidx + 1) raw_rule; |
189 |
||
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
190 |
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
|
191 |
val m = length vars; |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
192 |
val n = length params; |
| 19779 | 193 |
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
|
194 |
|
| 19779 | 195 |
fun unify ((x, T), (y, U)) (tyenv, max) = Sign.typ_unify thy (T, U) (tyenv, max) |
196 |
handle Type.TUNIFY => |
|
197 |
err ("Failed to unify variable " ^
|
|
198 |
string_of_term (Free (x, Envir.norm_type tyenv T)) ^ " against parameter " ^ |
|
199 |
string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule; |
|
| 19978 | 200 |
val (tyenv, _) = fold unify (map #1 vars ~~ Library.take (m, params)) |
| 19779 | 201 |
(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
|
202 |
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
|
203 |
|
| 19978 | 204 |
val xs = map (apsnd norm_type o fst) vars; |
| 19779 | 205 |
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
|
206 |
val ys' = map Name.internal (Name.variant_list (map fst xs) (map fst ys)) ~~ map #2 ys; |
| 19978 | 207 |
val terms = map (Drule.mk_term o cert o Free) (xs @ ys'); |
| 19779 | 208 |
|
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
209 |
val instT = |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
210 |
fold (Term.add_tvarsT o #2) params [] |
| 19978 | 211 |
|> map (TVar #> (fn T => (certT T, certT (norm_type T)))); |
|
20218
be3bfb0699ba
Variable.import(T): result includes fixed types/terms;
wenzelm
parents:
20201
diff
changeset
|
212 |
val ((_, rule' :: terms'), ctxt') = |
| 19978 | 213 |
Variable.import false (Thm.instantiate (instT, []) rule :: terms) ctxt; |
| 17891 | 214 |
|
| 19978 | 215 |
val vars' = |
216 |
map (dest_Free o Thm.term_of o Drule.dest_term) terms' ~~ |
|
217 |
(map snd vars @ replicate (length ys) NoSyn); |
|
|
20004
e6d3f2b031e6
guess: proper context for polymorphic parameters;
wenzelm
parents:
19978
diff
changeset
|
218 |
val rule'' = Drule.generalize ([], [thesis_name]) rule'; |
| 19978 | 219 |
in ((vars', rule''), ctxt') end; |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
220 |
|
| 18693 | 221 |
fun inferred_type (x, _, mx) ctxt = |
| 18769 | 222 |
let val ((_, T), ctxt') = ProofContext.inferred_param x ctxt |
| 19779 | 223 |
in ((x, T, mx), ctxt') end; |
224 |
||
|
20004
e6d3f2b031e6
guess: proper context for polymorphic parameters;
wenzelm
parents:
19978
diff
changeset
|
225 |
fun polymorphic ctxt vars = |
|
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19844
diff
changeset
|
226 |
let val Ts = map Logic.dest_type (Variable.polymorphic ctxt (map (Logic.mk_type o #2) vars)) |
| 19779 | 227 |
in map2 (fn (x, _, mx) => fn T => ((x, T), mx)) vars Ts end; |
| 18693 | 228 |
|
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
229 |
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
|
230 |
let |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
231 |
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
|
232 |
val thy = Proof.theory_of state; |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
233 |
val ctxt = Proof.context_of state; |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
234 |
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
|
235 |
|
| 19978 | 236 |
val ((thesis_name, _), thesis) = bind_judgment ctxt AutoBind.thesisN; |
|
20004
e6d3f2b031e6
guess: proper context for polymorphic parameters;
wenzelm
parents:
19978
diff
changeset
|
237 |
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
|
238 |
|
| 17974 | 239 |
fun check_result th = |
240 |
(case Thm.prems_of th of |
|
241 |
[prem] => |
|
242 |
if Thm.concl_of th aconv thesis andalso |
|
243 |
Logic.strip_assums_concl prem aconv thesis then () |
|
| 18678 | 244 |
else error ("Guessed a different clause:\n" ^ ProofContext.string_of_thm ctxt th)
|
245 |
| [] => error "Goal solved -- nothing guessed." |
|
246 |
| _ => error ("Guess split into several cases:\n" ^ ProofContext.string_of_thm ctxt th));
|
|
| 17891 | 247 |
|
| 19978 | 248 |
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
|
249 |
let |
| 19978 | 250 |
val ((parms, rule), ctxt') = |
251 |
unify_params vars thesis_name raw_rule (Proof.context_of state'); |
|
252 |
val (bind, _) = ProofContext.bind_fixes (map (#1 o #1) parms) ctxt'; |
|
253 |
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
|
254 |
val ps = map dest_Free ts; |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
255 |
val asms = |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
256 |
Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule)) |
| 19585 | 257 |
|> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), [])); |
| 19779 | 258 |
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
|
259 |
in |
| 19978 | 260 |
state' |
261 |
|> Proof.map_context (K ctxt') |
|
262 |
|> Proof.fix_i (map (fn ((x, T), mx) => (x, SOME T, mx)) parms) |
|
263 |
|> Proof.assm_i (K (obtain_export ctxt' (map #1 ps) rule)) [(("", []), asms)]
|
|
264 |
|> 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
|
265 |
end; |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
266 |
|
| 19779 | 267 |
val goal = Var (("guess", 0), propT);
|
268 |
fun print_result ctxt' (k, [(s, [_, th])]) = |
|
269 |
ProofDisplay.print_results int ctxt' (k, [(s, [th])]); |
|
270 |
val before_qed = SOME (Method.primitive_text (Goal.conclude #> (fn th => |
|
271 |
Goal.protect (Conjunction.intr (Drule.mk_term (Thm.cprop_of th)) th)))); |
|
272 |
fun after_qed [[_, res]] = |
|
| 19978 | 273 |
(check_result res; Proof.end_block #> Seq.map (guess_context res)); |
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
274 |
in |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
275 |
state |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
276 |
|> Proof.enter_forward |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
277 |
|> Proof.begin_block |
| 19844 | 278 |
|> 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
|
279 |
|> Proof.chain_facts chain_facts |
| 19779 | 280 |
|> Proof.local_goal print_result (K I) (apsnd (rpair I)) |
281 |
"guess" before_qed after_qed [(("", []), [Logic.mk_term goal, goal])]
|
|
| 18151 | 282 |
|> 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
|
283 |
end; |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
284 |
|
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
285 |
in |
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
286 |
|
|
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
287 |
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
|
288 |
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
|
289 |
|
|
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 |
|
| 18897 | 292 |
|
293 |
||
294 |
(** statements with several cases **) |
|
295 |
||
296 |
fun statement cases = |
|
297 |
let |
|
| 18907 | 298 |
val names = |
299 |
cases |> map_index (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
|
|
| 18897 | 300 |
val elems = cases |> map (fn (_, (vars, _)) => |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19300
diff
changeset
|
301 |
Element.Constrains (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))); |
| 19585 | 302 |
val concl = cases |> map (fn (_, (_, props)) => (("", []), map (rpair []) props));
|
| 18897 | 303 |
|
304 |
fun mk_stmt stmt ctxt = |
|
305 |
let |
|
306 |
val thesis = |
|
307 |
ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN; |
|
| 18907 | 308 |
val atts = map Attrib.internal |
309 |
[RuleCases.consumes (~ (length cases)), RuleCases.case_names names]; |
|
310 |
||
| 18897 | 311 |
fun assume_case ((name, (vars, _)), (_, propp)) ctxt' = |
312 |
let |
|
313 |
val xs = map fst vars; |
|
314 |
val props = map fst propp; |
|
315 |
val (parms, ctxt'') = |
|
316 |
ctxt' |
|
|
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19844
diff
changeset
|
317 |
|> fold Variable.declare_term props |
| 18897 | 318 |
|> fold_map ProofContext.inferred_param xs; |
319 |
val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis)); |
|
320 |
in |
|
321 |
ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs)); |
|
322 |
ctxt' |> ProofContext.add_assms_i ProofContext.assume_export |
|
| 19585 | 323 |
[((name, [ContextRules.intro_query NONE]), [(asm, [])])] |
| 18897 | 324 |
|>> (fn [(_, [th])] => th) |
325 |
end; |
|
326 |
val (ths, ctxt') = ctxt |
|
327 |
|> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)]) |
|
328 |
|> fold_map assume_case (cases ~~ stmt) |
|
329 |
|-> (fn ths => ProofContext.note_thmss_i [((thatN, []), [(ths, [])])] #> #2 #> pair ths); |
|
| 19585 | 330 |
in (([(("", atts), [(thesis, [])])], ths), ctxt') end;
|
| 18897 | 331 |
in ((elems, concl), mk_stmt) end; |
332 |
||
|
17858
bc4db8cfd92f
added 'guess', which derives the obtained context from the course of reasoning;
wenzelm
parents:
17357
diff
changeset
|
333 |
end; |