author | wenzelm |
Thu, 21 Jun 2007 17:28:53 +0200 | |
changeset 23463 | 9953ff53cc64 |
parent 23189 | 4574ab8f3b21 |
child 23766 | 77e796fe89eb |
permissions | -rw-r--r-- |
22166 | 1 |
(* Title: HOL/Tools/function_package/fundef_core.ML |
2 |
ID: $Id$ |
|
3 |
Author: Alexander Krauss, TU Muenchen |
|
4 |
||
5 |
A package for general recursive function definitions. |
|
6 |
Main functionality |
|
7 |
*) |
|
8 |
||
9 |
signature FUNDEF_CORE = |
|
10 |
sig |
|
11 |
val prepare_fundef : FundefCommon.fundef_config |
|
12 |
-> string (* defname *) |
|
23189 | 13 |
-> ((string * typ) * mixfix) list (* defined symbol *) |
22166 | 14 |
-> ((string * typ) list * term list * term * term) list (* specification *) |
15 |
-> local_theory |
|
16 |
||
17 |
-> (term (* f *) |
|
18 |
* thm (* goalstate *) |
|
19 |
* (thm -> FundefCommon.fundef_result) (* continuation *) |
|
20 |
) * local_theory |
|
21 |
||
22 |
end |
|
23 |
||
24 |
structure FundefCore : FUNDEF_CORE = |
|
25 |
struct |
|
26 |
||
27 |
||
28 |
open FundefLib |
|
29 |
open FundefCommon |
|
30 |
open FundefAbbrev |
|
31 |
||
32 |
datatype globals = |
|
33 |
Globals of { |
|
34 |
fvar: term, |
|
35 |
domT: typ, |
|
36 |
ranT: typ, |
|
37 |
h: term, |
|
38 |
y: term, |
|
39 |
x: term, |
|
40 |
z: term, |
|
41 |
a: term, |
|
42 |
P: term, |
|
43 |
D: term, |
|
44 |
Pbool:term |
|
45 |
} |
|
46 |
||
47 |
||
48 |
datatype rec_call_info = |
|
49 |
RCInfo of |
|
50 |
{ |
|
51 |
RIvs: (string * typ) list, (* Call context: fixes and assumes *) |
|
52 |
CCas: thm list, |
|
53 |
rcarg: term, (* The recursive argument *) |
|
54 |
||
55 |
llRI: thm, |
|
56 |
h_assum: term |
|
57 |
} |
|
58 |
||
59 |
||
60 |
datatype clause_context = |
|
61 |
ClauseContext of |
|
62 |
{ |
|
63 |
ctxt : Proof.context, |
|
64 |
||
65 |
qs : term list, |
|
66 |
gs : term list, |
|
67 |
lhs: term, |
|
68 |
rhs: term, |
|
69 |
||
70 |
cqs: cterm list, |
|
71 |
ags: thm list, |
|
72 |
case_hyp : thm |
|
73 |
} |
|
74 |
||
75 |
||
76 |
fun transfer_clause_ctx thy (ClauseContext { ctxt, qs, gs, lhs, rhs, cqs, ags, case_hyp }) = |
|
77 |
ClauseContext { ctxt = ProofContext.transfer thy ctxt, |
|
78 |
qs = qs, gs = gs, lhs = lhs, rhs = rhs, cqs = cqs, ags = ags, case_hyp = case_hyp } |
|
79 |
||
80 |
||
81 |
datatype clause_info = |
|
82 |
ClauseInfo of |
|
83 |
{ |
|
84 |
no: int, |
|
85 |
qglr : ((string * typ) list * term list * term * term), |
|
86 |
cdata : clause_context, |
|
87 |
||
88 |
tree: ctx_tree, |
|
89 |
lGI: thm, |
|
90 |
RCs: rec_call_info list |
|
91 |
} |
|
92 |
||
93 |
||
94 |
(* Theory dependencies. *) |
|
95 |
val Pair_inject = thm "Product_Type.Pair_inject"; |
|
96 |
||
22279
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
97 |
val acc_induct_rule = thm "Accessible_Part.acc_induct_rule" |
22166 | 98 |
|
99 |
val ex1_implies_ex = thm "FunDef.fundef_ex1_existence" |
|
100 |
val ex1_implies_un = thm "FunDef.fundef_ex1_uniqueness" |
|
101 |
val ex1_implies_iff = thm "FunDef.fundef_ex1_iff" |
|
102 |
||
22279
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
103 |
val acc_downward = thm "Accessible_Part.acc_downward" |
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
104 |
val accI = thm "Accessible_Part.accI" |
22166 | 105 |
val case_split = thm "HOL.case_split_thm" |
106 |
val fundef_default_value = thm "FunDef.fundef_default_value" |
|
22279
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
107 |
val not_acc_down = thm "Accessible_Part.not_acc_down" |
22166 | 108 |
|
109 |
||
110 |
||
111 |
fun find_calls tree = |
|
112 |
let |
|
113 |
fun add_Ri (fixes,assumes) (_ $ arg) _ (_, xs) = ([], (fixes, assumes, arg) :: xs) |
|
114 |
| add_Ri _ _ _ _ = raise Match |
|
115 |
in |
|
116 |
rev (FundefCtxTree.traverse_tree add_Ri tree []) |
|
117 |
end |
|
118 |
||
119 |
||
120 |
(** building proof obligations *) |
|
121 |
||
122 |
fun mk_compat_proof_obligations domT ranT fvar f glrs = |
|
123 |
let |
|
124 |
fun mk_impl ((qs, gs, lhs, rhs),(qs', gs', lhs', rhs')) = |
|
125 |
let |
|
126 |
val shift = incr_boundvars (length qs') |
|
127 |
in |
|
128 |
(implies $ Trueprop (eq_const domT $ shift lhs $ lhs') |
|
129 |
$ Trueprop (eq_const ranT $ shift rhs $ rhs')) |
|
130 |
|> fold_rev (curry Logic.mk_implies) (map shift gs @ gs') |
|
131 |
|> fold_rev (fn (n,T) => fn b => all T $ Abs(n,T,b)) (qs @ qs') |
|
132 |
|> curry abstract_over fvar |
|
133 |
|> curry subst_bound f |
|
134 |
end |
|
135 |
in |
|
136 |
map mk_impl (unordered_pairs glrs) |
|
137 |
end |
|
138 |
||
139 |
||
140 |
fun mk_completeness (Globals {x, Pbool, ...}) clauses qglrs = |
|
141 |
let |
|
142 |
fun mk_case (ClauseContext {qs, gs, lhs, ...}, (oqs, _, _, _)) = |
|
143 |
Trueprop Pbool |
|
144 |
|> curry Logic.mk_implies (Trueprop (mk_eq (x, lhs))) |
|
145 |
|> fold_rev (curry Logic.mk_implies) gs |
|
146 |
|> fold_rev mk_forall_rename (map fst oqs ~~ qs) |
|
147 |
in |
|
148 |
Trueprop Pbool |
|
149 |
|> fold_rev (curry Logic.mk_implies o mk_case) (clauses ~~ qglrs) |
|
150 |
|> mk_forall_rename ("x", x) |
|
151 |
|> mk_forall_rename ("P", Pbool) |
|
152 |
end |
|
153 |
||
154 |
(** making a context with it's own local bindings **) |
|
155 |
||
156 |
fun mk_clause_context x ctxt (pre_qs,pre_gs,pre_lhs,pre_rhs) = |
|
157 |
let |
|
158 |
val (qs, ctxt') = Variable.variant_fixes (map fst pre_qs) ctxt |
|
159 |
|>> map2 (fn (_, T) => fn n => Free (n, T)) pre_qs |
|
160 |
||
161 |
val thy = ProofContext.theory_of ctxt' |
|
162 |
||
163 |
fun inst t = subst_bounds (rev qs, t) |
|
164 |
val gs = map inst pre_gs |
|
165 |
val lhs = inst pre_lhs |
|
166 |
val rhs = inst pre_rhs |
|
167 |
||
168 |
val cqs = map (cterm_of thy) qs |
|
169 |
val ags = map (assume o cterm_of thy) gs |
|
170 |
||
171 |
val case_hyp = assume (cterm_of thy (Trueprop (mk_eq (x, lhs)))) |
|
172 |
in |
|
173 |
ClauseContext { ctxt = ctxt', qs = qs, gs = gs, lhs = lhs, rhs = rhs, |
|
174 |
cqs = cqs, ags = ags, case_hyp = case_hyp } |
|
175 |
end |
|
176 |
||
177 |
||
178 |
(* lowlevel term function *) |
|
179 |
fun abstract_over_list vs body = |
|
180 |
let |
|
181 |
exception SAME; |
|
182 |
fun abs lev v tm = |
|
183 |
if v aconv tm then Bound lev |
|
184 |
else |
|
185 |
(case tm of |
|
186 |
Abs (a, T, t) => Abs (a, T, abs (lev + 1) v t) |
|
187 |
| t $ u => (abs lev v t $ (abs lev v u handle SAME => u) handle SAME => t $ abs lev v u) |
|
188 |
| _ => raise SAME); |
|
189 |
in |
|
190 |
fold_index (fn (i,v) => fn t => abs i v t handle SAME => t) vs body |
|
191 |
end |
|
192 |
||
193 |
||
194 |
||
195 |
fun mk_clause_info globals G f no cdata qglr tree RCs GIntro_thm RIntro_thms = |
|
196 |
let |
|
197 |
val Globals {h, fvar, x, ...} = globals |
|
198 |
||
199 |
val ClauseContext { ctxt, qs, cqs, ags, ... } = cdata |
|
200 |
val cert = Thm.cterm_of (ProofContext.theory_of ctxt) |
|
201 |
||
202 |
(* Instantiate the GIntro thm with "f" and import into the clause context. *) |
|
203 |
val lGI = GIntro_thm |
|
204 |
|> forall_elim (cert f) |
|
205 |
|> fold forall_elim cqs |
|
206 |
|> fold implies_elim_swp ags |
|
207 |
||
208 |
fun mk_call_info (rcfix, rcassm, rcarg) RI = |
|
209 |
let |
|
210 |
val llRI = RI |
|
211 |
|> fold forall_elim cqs |
|
212 |
|> fold (forall_elim o cert o Free) rcfix |
|
213 |
|> fold implies_elim_swp ags |
|
214 |
|> fold implies_elim_swp rcassm |
|
215 |
||
216 |
val h_assum = |
|
217 |
Trueprop (G $ rcarg $ (h $ rcarg)) |
|
218 |
|> fold_rev (curry Logic.mk_implies o prop_of) rcassm |
|
219 |
|> fold_rev (mk_forall o Free) rcfix |
|
220 |
|> Pattern.rewrite_term (ProofContext.theory_of ctxt) [(f, h)] [] |
|
221 |
|> abstract_over_list (rev qs) |
|
222 |
in |
|
223 |
RCInfo {RIvs=rcfix, rcarg=rcarg, CCas=rcassm, llRI=llRI, h_assum=h_assum} |
|
224 |
end |
|
225 |
||
226 |
val RC_infos = map2 mk_call_info RCs RIntro_thms |
|
227 |
in |
|
228 |
ClauseInfo |
|
229 |
{ |
|
230 |
no=no, |
|
231 |
cdata=cdata, |
|
232 |
qglr=qglr, |
|
233 |
||
234 |
lGI=lGI, |
|
235 |
RCs=RC_infos, |
|
236 |
tree=tree |
|
237 |
} |
|
238 |
end |
|
239 |
||
240 |
||
241 |
||
242 |
||
243 |
||
244 |
||
245 |
||
246 |
(* replace this by a table later*) |
|
247 |
fun store_compat_thms 0 thms = [] |
|
248 |
| store_compat_thms n thms = |
|
249 |
let |
|
250 |
val (thms1, thms2) = chop n thms |
|
251 |
in |
|
252 |
(thms1 :: store_compat_thms (n - 1) thms2) |
|
253 |
end |
|
254 |
||
255 |
(* expects i <= j *) |
|
256 |
fun lookup_compat_thm i j cts = |
|
257 |
nth (nth cts (i - 1)) (j - i) |
|
258 |
||
259 |
(* Returns "Gsi, Gsj, lhs_i = lhs_j |-- rhs_j_f = rhs_i_f" *) |
|
260 |
(* if j < i, then turn around *) |
|
261 |
fun get_compat_thm thy cts i j ctxi ctxj = |
|
262 |
let |
|
263 |
val ClauseContext {cqs=cqsi,ags=agsi,lhs=lhsi,...} = ctxi |
|
264 |
val ClauseContext {cqs=cqsj,ags=agsj,lhs=lhsj,...} = ctxj |
|
265 |
||
266 |
val lhsi_eq_lhsj = cterm_of thy (Trueprop (mk_eq (lhsi, lhsj))) |
|
267 |
in if j < i then |
|
268 |
let |
|
269 |
val compat = lookup_compat_thm j i cts |
|
270 |
in |
|
271 |
compat (* "!!qj qi. Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *) |
|
272 |
|> fold forall_elim (cqsj @ cqsi) (* "Gsj => Gsi => lhsj = lhsi ==> rhsj = rhsi" *) |
|
273 |
|> fold implies_elim_swp agsj |
|
274 |
|> fold implies_elim_swp agsi |
|
275 |
|> implies_elim_swp ((assume lhsi_eq_lhsj) RS sym) (* "Gsj, Gsi, lhsi = lhsj |-- rhsj = rhsi" *) |
|
276 |
end |
|
277 |
else |
|
278 |
let |
|
279 |
val compat = lookup_compat_thm i j cts |
|
280 |
in |
|
281 |
compat (* "!!qi qj. Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *) |
|
282 |
|> fold forall_elim (cqsi @ cqsj) (* "Gsi => Gsj => lhsi = lhsj ==> rhsi = rhsj" *) |
|
283 |
|> fold implies_elim_swp agsi |
|
284 |
|> fold implies_elim_swp agsj |
|
285 |
|> implies_elim_swp (assume lhsi_eq_lhsj) |
|
286 |
|> (fn thm => thm RS sym) (* "Gsi, Gsj, lhsi = lhsj |-- rhsj = rhsi" *) |
|
287 |
end |
|
288 |
end |
|
289 |
||
290 |
||
291 |
||
292 |
||
293 |
(* Generates the replacement lemma in fully quantified form. *) |
|
294 |
fun mk_replacement_lemma thy h ih_elim clause = |
|
295 |
let |
|
296 |
val ClauseInfo {cdata=ClauseContext {qs, lhs, rhs, cqs, ags, case_hyp, ...}, RCs, tree, ...} = clause |
|
297 |
||
298 |
val ih_elim_case = full_simplify (HOL_basic_ss addsimps [case_hyp]) ih_elim |
|
299 |
||
300 |
val Ris = map (fn RCInfo {llRI, ...} => llRI) RCs |
|
301 |
val h_assums = map (fn RCInfo {h_assum, ...} => assume (cterm_of thy (subst_bounds (rev qs, h_assum)))) RCs |
|
302 |
||
303 |
val ih_elim_case_inst = instantiate' [] [NONE, SOME (cterm_of thy h)] ih_elim_case (* Should be done globally *) |
|
304 |
||
305 |
val (eql, _) = FundefCtxTree.rewrite_by_tree thy h ih_elim_case_inst (Ris ~~ h_assums) tree |
|
306 |
||
307 |
val replace_lemma = (eql RS meta_eq_to_obj_eq) |
|
308 |
|> implies_intr (cprop_of case_hyp) |
|
309 |
|> fold_rev (implies_intr o cprop_of) h_assums |
|
310 |
|> fold_rev (implies_intr o cprop_of) ags |
|
311 |
|> fold_rev forall_intr cqs |
|
312 |
|> Goal.close_result |
|
313 |
in |
|
314 |
replace_lemma |
|
315 |
end |
|
316 |
||
317 |
||
318 |
fun mk_uniqueness_clause thy globals f compat_store clausei clausej RLj = |
|
319 |
let |
|
320 |
val Globals {h, y, x, fvar, ...} = globals |
|
321 |
val ClauseInfo {no=i, cdata=cctxi as ClauseContext {ctxt=ctxti, lhs=lhsi, case_hyp, ...}, ...} = clausei |
|
322 |
val ClauseInfo {no=j, qglr=cdescj, RCs=RCsj, ...} = clausej |
|
323 |
||
324 |
val cctxj as ClauseContext {ags = agsj', lhs = lhsj', rhs = rhsj', qs = qsj', cqs = cqsj', ...} |
|
325 |
= mk_clause_context x ctxti cdescj |
|
326 |
||
327 |
val rhsj'h = Pattern.rewrite_term thy [(fvar,h)] [] rhsj' |
|
328 |
val compat = get_compat_thm thy compat_store i j cctxi cctxj |
|
329 |
val Ghsj' = map (fn RCInfo {h_assum, ...} => assume (cterm_of thy (subst_bounds (rev qsj', h_assum)))) RCsj |
|
330 |
||
331 |
val RLj_import = |
|
332 |
RLj |> fold forall_elim cqsj' |
|
333 |
|> fold implies_elim_swp agsj' |
|
334 |
|> fold implies_elim_swp Ghsj' |
|
335 |
||
336 |
val y_eq_rhsj'h = assume (cterm_of thy (Trueprop (mk_eq (y, rhsj'h)))) |
|
337 |
val lhsi_eq_lhsj' = assume (cterm_of thy (Trueprop (mk_eq (lhsi, lhsj')))) (* lhs_i = lhs_j' |-- lhs_i = lhs_j' *) |
|
338 |
in |
|
339 |
(trans OF [case_hyp, lhsi_eq_lhsj']) (* lhs_i = lhs_j' |-- x = lhs_j' *) |
|
340 |
|> implies_elim RLj_import (* Rj1' ... Rjk', lhs_i = lhs_j' |-- rhs_j'_h = rhs_j'_f *) |
|
341 |
|> (fn it => trans OF [it, compat]) (* lhs_i = lhs_j', Gj', Rj1' ... Rjk' |-- rhs_j'_h = rhs_i_f *) |
|
342 |
|> (fn it => trans OF [y_eq_rhsj'h, it]) (* lhs_i = lhs_j', Gj', Rj1' ... Rjk', y = rhs_j_h' |-- y = rhs_i_f *) |
|
343 |
|> fold_rev (implies_intr o cprop_of) Ghsj' |
|
344 |
|> fold_rev (implies_intr o cprop_of) agsj' (* lhs_i = lhs_j' , y = rhs_j_h' |-- Gj', Rj1'...Rjk' ==> y = rhs_i_f *) |
|
345 |
|> implies_intr (cprop_of y_eq_rhsj'h) |
|
346 |
|> implies_intr (cprop_of lhsi_eq_lhsj') |
|
347 |
|> fold_rev forall_intr (cterm_of thy h :: cqsj') |
|
348 |
end |
|
349 |
||
350 |
||
351 |
||
22617 | 352 |
fun mk_uniqueness_case ctxt thy globals G f ihyp ih_intro G_cases compat_store clauses rep_lemmas clausei = |
22166 | 353 |
let |
354 |
val Globals {x, y, ranT, fvar, ...} = globals |
|
355 |
val ClauseInfo {cdata = ClauseContext {lhs, rhs, qs, cqs, ags, case_hyp, ...}, lGI, RCs, ...} = clausei |
|
356 |
val rhsC = Pattern.rewrite_term thy [(fvar, f)] [] rhs |
|
357 |
||
358 |
val ih_intro_case = full_simplify (HOL_basic_ss addsimps [case_hyp]) ih_intro |
|
359 |
||
360 |
fun prep_RC (RCInfo {llRI, RIvs, CCas, ...}) = (llRI RS ih_intro_case) |
|
361 |
|> fold_rev (implies_intr o cprop_of) CCas |
|
362 |
|> fold_rev (forall_intr o cterm_of thy o Free) RIvs |
|
363 |
||
364 |
val existence = fold (curry op COMP o prep_RC) RCs lGI |
|
365 |
||
366 |
val P = cterm_of thy (mk_eq (y, rhsC)) |
|
367 |
val G_lhs_y = assume (cterm_of thy (Trueprop (G $ lhs $ y))) |
|
368 |
||
369 |
val unique_clauses = map2 (mk_uniqueness_clause thy globals f compat_store clausei) clauses rep_lemmas |
|
370 |
||
371 |
val uniqueness = G_cases |
|
372 |
|> forall_elim (cterm_of thy lhs) |
|
373 |
|> forall_elim (cterm_of thy y) |
|
374 |
|> forall_elim P |
|
375 |
|> implies_elim_swp G_lhs_y |
|
376 |
|> fold implies_elim_swp unique_clauses |
|
377 |
|> implies_intr (cprop_of G_lhs_y) |
|
378 |
|> forall_intr (cterm_of thy y) |
|
379 |
||
380 |
val P2 = cterm_of thy (lambda y (G $ lhs $ y)) (* P2 y := (lhs, y): G *) |
|
381 |
||
382 |
val exactly_one = |
|
383 |
ex1I |> instantiate' [SOME (ctyp_of thy ranT)] [SOME P2, SOME (cterm_of thy rhsC)] |
|
384 |
|> curry (op COMP) existence |
|
385 |
|> curry (op COMP) uniqueness |
|
386 |
|> simplify (HOL_basic_ss addsimps [case_hyp RS sym]) |
|
387 |
|> implies_intr (cprop_of case_hyp) |
|
388 |
|> fold_rev (implies_intr o cprop_of) ags |
|
389 |
|> fold_rev forall_intr cqs |
|
390 |
||
391 |
val function_value = |
|
392 |
existence |
|
393 |
|> implies_intr ihyp |
|
394 |
|> implies_intr (cprop_of case_hyp) |
|
395 |
|> forall_intr (cterm_of thy x) |
|
396 |
|> forall_elim (cterm_of thy lhs) |
|
397 |
|> curry (op RS) refl |
|
398 |
in |
|
399 |
(exactly_one, function_value) |
|
400 |
end |
|
401 |
||
402 |
||
403 |
||
404 |
||
22617 | 405 |
fun prove_stuff ctxt congs globals G f R clauses complete compat compat_store G_elim f_def = |
22166 | 406 |
let |
407 |
val Globals {h, domT, ranT, x, ...} = globals |
|
22617 | 408 |
val thy = ProofContext.theory_of ctxt |
22166 | 409 |
|
410 |
(* Inductive Hypothesis: !!z. (z,x):R ==> EX!y. (z,y):G *) |
|
411 |
val ihyp = all domT $ Abs ("z", domT, |
|
412 |
implies $ Trueprop (R $ Bound 0 $ x) |
|
413 |
$ Trueprop (Const ("Ex1", (ranT --> boolT) --> boolT) $ |
|
414 |
Abs ("y", ranT, G $ Bound 1 $ Bound 0))) |
|
415 |
|> cterm_of thy |
|
416 |
||
417 |
val ihyp_thm = assume ihyp |> forall_elim_vars 0 |
|
418 |
val ih_intro = ihyp_thm RS (f_def RS ex1_implies_ex) |
|
419 |
val ih_elim = ihyp_thm RS (f_def RS ex1_implies_un) |
|
420 |
||
421 |
val _ = Output.debug (K "Proving Replacement lemmas...") |
|
422 |
val repLemmas = map (mk_replacement_lemma thy h ih_elim) clauses |
|
423 |
||
424 |
val _ = Output.debug (K "Proving cases for unique existence...") |
|
425 |
val (ex1s, values) = |
|
22617 | 426 |
split_list (map (mk_uniqueness_case ctxt thy globals G f ihyp ih_intro G_elim compat_store clauses repLemmas) clauses) |
22166 | 427 |
|
22617 | 428 |
val _ = Output.debug (K "Proving: Graph is a function") |
22166 | 429 |
val graph_is_function = complete |
430 |
|> forall_elim_vars 0 |
|
431 |
|> fold (curry op COMP) ex1s |
|
432 |
|> implies_intr (ihyp) |
|
433 |
|> implies_intr (cterm_of thy (Trueprop (mk_acc domT R $ x))) |
|
434 |
|> forall_intr (cterm_of thy x) |
|
435 |
|> (fn it => Drule.compose_single (it, 2, acc_induct_rule)) (* "EX! y. (?x,y):G" *) |
|
436 |
|> (fn it => fold (forall_intr o cterm_of thy) (term_vars (prop_of it)) it) |
|
437 |
||
438 |
val goalstate = Conjunction.intr graph_is_function complete |
|
439 |
|> Goal.close_result |
|
440 |
|> Goal.protect |
|
441 |
|> fold_rev (implies_intr o cprop_of) compat |
|
442 |
|> implies_intr (cprop_of complete) |
|
443 |
in |
|
444 |
(goalstate, values) |
|
445 |
end |
|
446 |
||
447 |
||
448 |
fun define_graph Gname fvar domT ranT clauses RCss lthy = |
|
449 |
let |
|
450 |
val GT = domT --> ranT --> boolT |
|
451 |
val Gvar = Free (the_single (Variable.variant_frees lthy [] [(Gname, GT)])) |
|
452 |
||
453 |
fun mk_GIntro (ClauseContext {qs, gs, lhs, rhs, ...}) RCs = |
|
454 |
let |
|
455 |
fun mk_h_assm (rcfix, rcassm, rcarg) = |
|
456 |
Trueprop (Gvar $ rcarg $ (fvar $ rcarg)) |
|
457 |
|> fold_rev (curry Logic.mk_implies o prop_of) rcassm |
|
458 |
|> fold_rev (mk_forall o Free) rcfix |
|
459 |
in |
|
460 |
Trueprop (Gvar $ lhs $ rhs) |
|
461 |
|> fold_rev (curry Logic.mk_implies o mk_h_assm) RCs |
|
462 |
|> fold_rev (curry Logic.mk_implies) gs |
|
463 |
|> fold_rev mk_forall (fvar :: qs) |
|
464 |
end |
|
465 |
||
466 |
val G_intros = map2 mk_GIntro clauses RCss |
|
467 |
||
468 |
val (GIntro_thms, (G, G_elim, G_induct, lthy)) = |
|
469 |
FundefInductiveWrap.inductive_def G_intros ((dest_Free Gvar, NoSyn), lthy) |
|
470 |
in |
|
471 |
((G, GIntro_thms, G_elim, G_induct), lthy) |
|
472 |
end |
|
473 |
||
474 |
||
475 |
||
476 |
fun define_function fdefname (fname, mixfix) domT ranT G default lthy = |
|
477 |
let |
|
478 |
val f_def = |
|
479 |
Abs ("x", domT, Const ("FunDef.THE_default", ranT --> (ranT --> boolT) --> ranT) $ (default $ Bound 0) $ |
|
480 |
Abs ("y", ranT, G $ Bound 1 $ Bound 0)) |
|
22617 | 481 |
|> ProofContext.cert_term lthy |
22496 | 482 |
|
22166 | 483 |
val ((f, (_, f_defthm)), lthy) = |
22496 | 484 |
LocalTheory.def Thm.internalK ((function_name fname, mixfix), ((fdefname, []), f_def)) lthy |
22166 | 485 |
in |
486 |
((f, f_defthm), lthy) |
|
487 |
end |
|
488 |
||
489 |
||
490 |
fun define_recursion_relation Rname domT ranT fvar f qglrs clauses RCss lthy = |
|
491 |
let |
|
492 |
||
493 |
val RT = domT --> domT --> boolT |
|
494 |
val Rvar = Free (the_single (Variable.variant_frees lthy [] [(Rname, RT)])) |
|
495 |
||
496 |
fun mk_RIntro (ClauseContext {qs, gs, lhs, ...}, (oqs, _, _, _)) (rcfix, rcassm, rcarg) = |
|
497 |
Trueprop (Rvar $ rcarg $ lhs) |
|
498 |
|> fold_rev (curry Logic.mk_implies o prop_of) rcassm |
|
499 |
|> fold_rev (curry Logic.mk_implies) gs |
|
500 |
|> fold_rev (mk_forall o Free) rcfix |
|
501 |
|> fold_rev mk_forall_rename (map fst oqs ~~ qs) |
|
502 |
(* "!!qs xs. CS ==> G => (r, lhs) : R" *) |
|
503 |
||
504 |
val R_intross = map2 (map o mk_RIntro) (clauses ~~ qglrs) RCss |
|
505 |
||
506 |
val (RIntro_thmss, (R, R_elim, _, lthy)) = |
|
507 |
fold_burrow FundefInductiveWrap.inductive_def R_intross ((dest_Free Rvar, NoSyn), lthy) |
|
508 |
in |
|
509 |
((R, RIntro_thmss, R_elim), lthy) |
|
510 |
end |
|
511 |
||
512 |
||
513 |
fun fix_globals domT ranT fvar ctxt = |
|
514 |
let |
|
515 |
val ([h, y, x, z, a, D, P, Pbool],ctxt') = |
|
516 |
Variable.variant_fixes ["h_fd", "y_fd", "x_fd", "z_fd", "a_fd", "D_fd", "P_fd", "Pb_fd"] ctxt |
|
517 |
in |
|
518 |
(Globals {h = Free (h, domT --> ranT), |
|
519 |
y = Free (y, ranT), |
|
520 |
x = Free (x, domT), |
|
521 |
z = Free (z, domT), |
|
522 |
a = Free (a, domT), |
|
523 |
D = Free (D, domT --> boolT), |
|
524 |
P = Free (P, domT --> boolT), |
|
525 |
Pbool = Free (Pbool, boolT), |
|
526 |
fvar = fvar, |
|
527 |
domT = domT, |
|
528 |
ranT = ranT |
|
529 |
}, |
|
530 |
ctxt') |
|
531 |
end |
|
532 |
||
533 |
||
534 |
||
535 |
fun inst_RC thy fvar f (rcfix, rcassm, rcarg) = |
|
536 |
let |
|
537 |
fun inst_term t = subst_bound(f, abstract_over (fvar, t)) |
|
538 |
in |
|
539 |
(rcfix, map (assume o cterm_of thy o inst_term o prop_of) rcassm, inst_term rcarg) |
|
540 |
end |
|
541 |
||
542 |
||
543 |
||
544 |
(********************************************************** |
|
545 |
* PROVING THE RULES |
|
546 |
**********************************************************) |
|
547 |
||
548 |
fun mk_psimps thy globals R clauses valthms f_iff graph_is_function = |
|
549 |
let |
|
550 |
val Globals {domT, z, ...} = globals |
|
551 |
||
552 |
fun mk_psimp (ClauseInfo {qglr = (oqs, _, _, _), cdata = ClauseContext {cqs, lhs, ags, ...}, ...}) valthm = |
|
553 |
let |
|
554 |
val lhs_acc = cterm_of thy (Trueprop (mk_acc domT R $ lhs)) (* "acc R lhs" *) |
|
555 |
val z_smaller = cterm_of thy (Trueprop (R $ z $ lhs)) (* "R z lhs" *) |
|
556 |
in |
|
557 |
((assume z_smaller) RS ((assume lhs_acc) RS acc_downward)) |
|
558 |
|> (fn it => it COMP graph_is_function) |
|
559 |
|> implies_intr z_smaller |
|
560 |
|> forall_intr (cterm_of thy z) |
|
561 |
|> (fn it => it COMP valthm) |
|
562 |
|> implies_intr lhs_acc |
|
563 |
|> asm_simplify (HOL_basic_ss addsimps [f_iff]) |
|
564 |
|> fold_rev (implies_intr o cprop_of) ags |
|
565 |
|> fold_rev forall_intr_rename (map fst oqs ~~ cqs) |
|
566 |
end |
|
567 |
in |
|
568 |
map2 mk_psimp clauses valthms |
|
569 |
end |
|
570 |
||
571 |
||
572 |
(** Induction rule **) |
|
573 |
||
574 |
||
22461 | 575 |
val acc_subset_induct = thm "Fun.predicate1I" RS |
22279
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
576 |
thm "Accessible_Part.acc_subset_induct" |
22166 | 577 |
|
578 |
fun mk_partial_induct_rule thy globals R complete_thm clauses = |
|
579 |
let |
|
580 |
val Globals {domT, x, z, a, P, D, ...} = globals |
|
581 |
val acc_R = mk_acc domT R |
|
582 |
||
583 |
val x_D = assume (cterm_of thy (Trueprop (D $ x))) |
|
584 |
val a_D = cterm_of thy (Trueprop (D $ a)) |
|
585 |
||
586 |
val D_subset = cterm_of thy (mk_forall x (implies $ Trueprop (D $ x) $ Trueprop (acc_R $ x))) |
|
587 |
||
588 |
val D_dcl = (* "!!x z. [| x: D; (z,x):R |] ==> z:D" *) |
|
589 |
mk_forall x |
|
590 |
(mk_forall z (Logic.mk_implies (Trueprop (D $ x), |
|
591 |
Logic.mk_implies (Trueprop (R $ z $ x), |
|
592 |
Trueprop (D $ z))))) |
|
593 |
|> cterm_of thy |
|
594 |
||
595 |
||
596 |
(* Inductive Hypothesis: !!z. (z,x):R ==> P z *) |
|
597 |
val ihyp = all domT $ Abs ("z", domT, |
|
598 |
implies $ Trueprop (R $ Bound 0 $ x) |
|
599 |
$ Trueprop (P $ Bound 0)) |
|
600 |
|> cterm_of thy |
|
601 |
||
602 |
val aihyp = assume ihyp |
|
603 |
||
604 |
fun prove_case clause = |
|
605 |
let |
|
606 |
val ClauseInfo {cdata = ClauseContext {qs, cqs, ags, gs, lhs, case_hyp, ...}, RCs, |
|
607 |
qglr = (oqs, _, _, _), ...} = clause |
|
608 |
||
609 |
val replace_x_ss = HOL_basic_ss addsimps [case_hyp] |
|
610 |
val lhs_D = simplify replace_x_ss x_D (* lhs : D *) |
|
611 |
val sih = full_simplify replace_x_ss aihyp |
|
612 |
||
613 |
fun mk_Prec (RCInfo {llRI, RIvs, CCas, rcarg, ...}) = |
|
614 |
sih |> forall_elim (cterm_of thy rcarg) |
|
615 |
|> implies_elim_swp llRI |
|
616 |
|> fold_rev (implies_intr o cprop_of) CCas |
|
617 |
|> fold_rev (forall_intr o cterm_of thy o Free) RIvs |
|
618 |
||
619 |
val P_recs = map mk_Prec RCs (* [P rec1, P rec2, ... ] *) |
|
620 |
||
621 |
val step = Trueprop (P $ lhs) |
|
622 |
|> fold_rev (curry Logic.mk_implies o prop_of) P_recs |
|
623 |
|> fold_rev (curry Logic.mk_implies) gs |
|
624 |
|> curry Logic.mk_implies (Trueprop (D $ lhs)) |
|
625 |
|> fold_rev mk_forall_rename (map fst oqs ~~ qs) |
|
626 |
|> cterm_of thy |
|
627 |
||
628 |
val P_lhs = assume step |
|
629 |
|> fold forall_elim cqs |
|
630 |
|> implies_elim_swp lhs_D |
|
22419
17441293ebc6
fixed function package bug in the handling of multiple guards
krauss
parents:
22325
diff
changeset
|
631 |
|> fold implies_elim_swp ags |
22166 | 632 |
|> fold implies_elim_swp P_recs |
633 |
||
634 |
val res = cterm_of thy (Trueprop (P $ x)) |
|
635 |
|> Simplifier.rewrite replace_x_ss |
|
636 |
|> symmetric (* P lhs == P x *) |
|
637 |
|> (fn eql => equal_elim eql P_lhs) (* "P x" *) |
|
638 |
|> implies_intr (cprop_of case_hyp) |
|
639 |
|> fold_rev (implies_intr o cprop_of) ags |
|
640 |
|> fold_rev forall_intr cqs |
|
641 |
in |
|
642 |
(res, step) |
|
643 |
end |
|
644 |
||
645 |
val (cases, steps) = split_list (map prove_case clauses) |
|
646 |
||
647 |
val istep = complete_thm |
|
648 |
|> forall_elim_vars 0 |
|
649 |
|> fold (curry op COMP) cases (* P x *) |
|
650 |
|> implies_intr ihyp |
|
651 |
|> implies_intr (cprop_of x_D) |
|
652 |
|> forall_intr (cterm_of thy x) |
|
653 |
||
654 |
val subset_induct_rule = |
|
655 |
acc_subset_induct |
|
656 |
|> (curry op COMP) (assume D_subset) |
|
657 |
|> (curry op COMP) (assume D_dcl) |
|
658 |
|> (curry op COMP) (assume a_D) |
|
659 |
|> (curry op COMP) istep |
|
660 |
|> fold_rev implies_intr steps |
|
661 |
|> implies_intr a_D |
|
662 |
|> implies_intr D_dcl |
|
663 |
|> implies_intr D_subset |
|
664 |
||
665 |
val subset_induct_all = fold_rev (forall_intr o cterm_of thy) [P, a, D] subset_induct_rule |
|
666 |
||
667 |
val simple_induct_rule = |
|
668 |
subset_induct_rule |
|
669 |
|> forall_intr (cterm_of thy D) |
|
670 |
|> forall_elim (cterm_of thy acc_R) |
|
671 |
|> assume_tac 1 |> Seq.hd |
|
672 |
|> (curry op COMP) (acc_downward |
|
673 |
|> (instantiate' [SOME (ctyp_of thy domT)] |
|
674 |
(map (SOME o cterm_of thy) [R, x, z])) |
|
675 |
|> forall_intr (cterm_of thy z) |
|
676 |
|> forall_intr (cterm_of thy x)) |
|
677 |
|> forall_intr (cterm_of thy a) |
|
678 |
|> forall_intr (cterm_of thy P) |
|
679 |
in |
|
680 |
(subset_induct_all, simple_induct_rule) |
|
681 |
end |
|
682 |
||
683 |
||
684 |
||
685 |
(* FIXME: This should probably use fixed goals, to be more reliable and faster *) |
|
686 |
fun mk_domain_intro thy (Globals {domT, ...}) R R_cases clause = |
|
687 |
let |
|
688 |
val ClauseInfo {cdata = ClauseContext {qs, gs, lhs, rhs, cqs, ...}, |
|
689 |
qglr = (oqs, _, _, _), ...} = clause |
|
690 |
val goal = Trueprop (mk_acc domT R $ lhs) |
|
691 |
|> fold_rev (curry Logic.mk_implies) gs |
|
692 |
|> cterm_of thy |
|
693 |
in |
|
694 |
Goal.init goal |
|
695 |
|> (SINGLE (resolve_tac [accI] 1)) |> the |
|
696 |
|> (SINGLE (eresolve_tac [forall_elim_vars 0 R_cases] 1)) |> the |
|
697 |
|> (SINGLE (CLASIMPSET auto_tac)) |> the |
|
698 |
|> Goal.conclude |
|
699 |
|> fold_rev forall_intr_rename (map fst oqs ~~ cqs) |
|
700 |
end |
|
701 |
||
702 |
||
703 |
||
704 |
(** Termination rule **) |
|
705 |
||
22279
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
706 |
val wf_induct_rule = thm "Wellfounded_Recursion.wfP_induct_rule"; |
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
707 |
val wf_in_rel = thm "Wellfounded_Recursion.wf_implies_wfP"; |
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
708 |
val in_rel_def = thm "Predicate.member2_eq"; |
22166 | 709 |
|
710 |
fun mk_nest_term_case thy globals R' ihyp clause = |
|
711 |
let |
|
712 |
val Globals {x, z, ...} = globals |
|
713 |
val ClauseInfo {cdata = ClauseContext {qs,cqs,ags,lhs,rhs,case_hyp,...},tree, |
|
714 |
qglr=(oqs, _, _, _), ...} = clause |
|
715 |
||
716 |
val ih_case = full_simplify (HOL_basic_ss addsimps [case_hyp]) ihyp |
|
717 |
||
718 |
fun step (fixes, assumes) (_ $ arg) u (sub,(hyps,thms)) = |
|
719 |
let |
|
720 |
val used = map (fn ((f,a),thm) => FundefCtxTree.export_thm thy (f, map prop_of a) thm) (u @ sub) |
|
721 |
||
722 |
val hyp = Trueprop (R' $ arg $ lhs) |
|
723 |
|> fold_rev (curry Logic.mk_implies o prop_of) used |
|
724 |
|> FundefCtxTree.export_term (fixes, map prop_of assumes) |
|
725 |
|> fold_rev (curry Logic.mk_implies o prop_of) ags |
|
726 |
|> fold_rev mk_forall_rename (map fst oqs ~~ qs) |
|
727 |
|> cterm_of thy |
|
728 |
||
729 |
val thm = assume hyp |
|
730 |
|> fold forall_elim cqs |
|
731 |
|> fold implies_elim_swp ags |
|
732 |
|> FundefCtxTree.import_thm thy (fixes, assumes) (* "(arg, lhs) : R'" *) |
|
733 |
|> fold implies_elim_swp used |
|
734 |
||
735 |
val acc = thm COMP ih_case |
|
736 |
||
737 |
val z_eq_arg = cterm_of thy (Trueprop (HOLogic.mk_eq (z, arg))) |
|
738 |
||
739 |
val arg_eq_z = (assume z_eq_arg) RS sym |
|
740 |
||
741 |
val z_acc = simplify (HOL_basic_ss addsimps [arg_eq_z]) acc (* fragile, slow... *) |
|
742 |
|> implies_intr (cprop_of case_hyp) |
|
743 |
|> implies_intr z_eq_arg |
|
744 |
||
745 |
val z_eq_arg = assume (cterm_of thy (Trueprop (mk_eq (z, arg)))) |
|
746 |
val x_eq_lhs = assume (cterm_of thy (Trueprop (mk_eq (x, lhs)))) |
|
747 |
||
748 |
val ethm = (z_acc OF [z_eq_arg, x_eq_lhs]) |
|
749 |
|> FundefCtxTree.export_thm thy (fixes, |
|
750 |
prop_of z_eq_arg :: prop_of x_eq_lhs :: map prop_of (ags @ assumes)) |
|
751 |
|> fold_rev forall_intr_rename (map fst oqs ~~ cqs) |
|
752 |
||
753 |
val sub' = sub @ [(([],[]), acc)] |
|
754 |
in |
|
755 |
(sub', (hyp :: hyps, ethm :: thms)) |
|
756 |
end |
|
757 |
| step _ _ _ _ = raise Match |
|
758 |
in |
|
759 |
FundefCtxTree.traverse_tree step tree |
|
760 |
end |
|
22325 | 761 |
|
762 |
||
22166 | 763 |
fun mk_nest_term_rule thy globals R R_cases clauses = |
764 |
let |
|
765 |
val Globals { domT, x, z, ... } = globals |
|
766 |
val acc_R = mk_acc domT R |
|
767 |
||
768 |
val R' = Free ("R", fastype_of R) |
|
769 |
||
770 |
val Rrel = Free ("R", mk_relT (domT, domT)) |
|
22279
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
771 |
val inrel_R = Const ("Predicate.member2", mk_relT (domT, domT) --> fastype_of R) $ Rrel |
22166 | 772 |
|
22279
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
773 |
val wfR' = cterm_of thy (Trueprop (Const ("Wellfounded_Recursion.wfP", (domT --> domT --> boolT) --> boolT) $ R')) (* "wf R'" *) |
22166 | 774 |
|
775 |
(* Inductive Hypothesis: !!z. (z,x):R' ==> z : acc R *) |
|
776 |
val ihyp = all domT $ Abs ("z", domT, |
|
777 |
implies $ Trueprop (R' $ Bound 0 $ x) |
|
778 |
$ Trueprop (acc_R $ Bound 0)) |
|
779 |
|> cterm_of thy |
|
780 |
||
781 |
val ihyp_a = assume ihyp |> forall_elim_vars 0 |
|
782 |
||
783 |
val R_z_x = cterm_of thy (Trueprop (R $ z $ x)) |
|
784 |
||
785 |
val (hyps,cases) = fold (mk_nest_term_case thy globals R' ihyp_a) clauses ([],[]) |
|
786 |
in |
|
787 |
R_cases |
|
788 |
|> forall_elim (cterm_of thy z) |
|
789 |
|> forall_elim (cterm_of thy x) |
|
790 |
|> forall_elim (cterm_of thy (acc_R $ z)) |
|
791 |
|> curry op COMP (assume R_z_x) |
|
792 |
|> fold_rev (curry op COMP) cases |
|
793 |
|> implies_intr R_z_x |
|
794 |
|> forall_intr (cterm_of thy z) |
|
795 |
|> (fn it => it COMP accI) |
|
796 |
|> implies_intr ihyp |
|
797 |
|> forall_intr (cterm_of thy x) |
|
798 |
|> (fn it => Drule.compose_single(it,2,wf_induct_rule)) |
|
799 |
|> curry op RS (assume wfR') |
|
22325 | 800 |
|> forall_intr_vars |
801 |
|> (fn it => it COMP allI) |
|
22166 | 802 |
|> fold implies_intr hyps |
803 |
|> implies_intr wfR' |
|
804 |
|> forall_intr (cterm_of thy R') |
|
805 |
|> forall_elim (cterm_of thy (inrel_R)) |
|
806 |
|> curry op RS wf_in_rel |
|
807 |
|> full_simplify (HOL_basic_ss addsimps [in_rel_def]) |
|
808 |
|> forall_intr (cterm_of thy Rrel) |
|
809 |
end |
|
810 |
||
811 |
||
812 |
||
813 |
(* Tail recursion (probably very fragile) |
|
814 |
* |
|
815 |
* FIXME: |
|
816 |
* - Need to do forall_elim_vars on psimps: Unneccesary, if psimps would be taken from the same context. |
|
817 |
* - Must we really replace the fvar by f here? |
|
818 |
* - Splitting is not configured automatically: Problems with case? |
|
819 |
*) |
|
820 |
fun mk_trsimps octxt globals f G R f_def R_cases G_induct clauses psimps = |
|
821 |
let |
|
822 |
val Globals {domT, ranT, fvar, ...} = globals |
|
823 |
||
824 |
val R_cases = forall_elim_vars 0 R_cases (* FIXME: Should be already in standard form. *) |
|
825 |
||
826 |
val graph_implies_dom = (* "G ?x ?y ==> dom ?x" *) |
|
827 |
Goal.prove octxt ["x", "y"] [HOLogic.mk_Trueprop (G $ Free ("x", domT) $ Free ("y", ranT))] |
|
828 |
(HOLogic.mk_Trueprop (mk_acc domT R $ Free ("x", domT))) |
|
829 |
(fn {prems=[a], ...} => |
|
830 |
((rtac (G_induct OF [a])) |
|
831 |
THEN_ALL_NEW (rtac accI) |
|
832 |
THEN_ALL_NEW (etac R_cases) |
|
833 |
THEN_ALL_NEW (SIMPSET' asm_full_simp_tac)) 1) |
|
834 |
||
835 |
val default_thm = (forall_intr_vars graph_implies_dom) COMP (f_def COMP fundef_default_value) |
|
836 |
||
837 |
fun mk_trsimp clause psimp = |
|
838 |
let |
|
839 |
val ClauseInfo {qglr = (oqs, _, _, _), cdata = ClauseContext {ctxt, cqs, qs, gs, lhs, rhs, ...}, ...} = clause |
|
840 |
val thy = ProofContext.theory_of ctxt |
|
841 |
val rhs_f = Pattern.rewrite_term thy [(fvar, f)] [] rhs |
|
842 |
||
843 |
val trsimp = Logic.list_implies(gs, HOLogic.mk_Trueprop (HOLogic.mk_eq(f $ lhs, rhs_f))) (* "f lhs = rhs" *) |
|
844 |
val lhs_acc = (mk_acc domT R $ lhs) (* "acc R lhs" *) |
|
845 |
in |
|
846 |
Goal.prove ctxt [] [] trsimp |
|
847 |
(fn _ => |
|
848 |
rtac (instantiate' [] [SOME (cterm_of thy lhs_acc)] case_split) 1 |
|
849 |
THEN (rtac (forall_elim_vars 0 psimp) THEN_ALL_NEW assume_tac) 1 |
|
850 |
THEN (SIMPSET (fn ss => asm_full_simp_tac (ss addsimps [default_thm]) 1)) |
|
22279
b0d482a9443f
Adapted to changes in Accessible_Part and Wellfounded_Recursion theories.
berghofe
parents:
22166
diff
changeset
|
851 |
THEN (etac not_acc_down 1) |
22166 | 852 |
THEN ((etac R_cases) THEN_ALL_NEW (SIMPSET' (fn ss => asm_full_simp_tac (ss addsimps [default_thm])))) 1) |
853 |
|> fold_rev forall_intr_rename (map fst oqs ~~ cqs) |
|
854 |
end |
|
855 |
in |
|
856 |
map2 mk_trsimp clauses psimps |
|
857 |
end |
|
858 |
||
859 |
||
23189 | 860 |
fun prepare_fundef config defname [((fname, fT), mixfix)] abstract_qglrs lthy = |
22166 | 861 |
let |
23189 | 862 |
val FundefConfig {domintros, tailrec, default=default_str, ...} = config |
22166 | 863 |
|
864 |
val fvar = Free (fname, fT) |
|
865 |
val domT = domain_type fT |
|
866 |
val ranT = range_type fT |
|
867 |
||
22769 | 868 |
val default = singleton (ProofContext.read_termTs lthy) (default_str, fT) |
22166 | 869 |
|
870 |
val congs = get_fundef_congs (Context.Proof lthy) |
|
871 |
val (globals, ctxt') = fix_globals domT ranT fvar lthy |
|
872 |
||
873 |
val Globals { x, h, ... } = globals |
|
874 |
||
875 |
val clauses = map (mk_clause_context x ctxt') abstract_qglrs |
|
876 |
||
877 |
val n = length abstract_qglrs |
|
878 |
||
879 |
val congs_deps = map (fn c => (c, FundefCtxTree.cong_deps c)) (congs @ FundefCtxTree.add_congs) (* FIXME: Save in theory *) |
|
880 |
||
881 |
fun build_tree (ClauseContext { ctxt, rhs, ...}) = |
|
882 |
FundefCtxTree.mk_tree congs_deps (fname, fT) h ctxt rhs |
|
883 |
||
884 |
val trees = map build_tree clauses |
|
885 |
val RCss = map find_calls trees |
|
886 |
||
887 |
val ((G, GIntro_thms, G_elim, G_induct), lthy) = |
|
888 |
PROFILE "def_graph" (define_graph (graph_name defname) fvar domT ranT clauses RCss) lthy |
|
889 |
||
890 |
val ((f, f_defthm), lthy) = |
|
891 |
PROFILE "def_fun" (define_function (defname ^ "_sum_def") (fname, mixfix) domT ranT G default) lthy |
|
892 |
||
893 |
val RCss = map (map (inst_RC (ProofContext.theory_of lthy) fvar f)) RCss |
|
894 |
val trees = map (FundefCtxTree.inst_tree (ProofContext.theory_of lthy) fvar f) trees |
|
895 |
||
896 |
val ((R, RIntro_thmss, R_elim), lthy) = |
|
897 |
PROFILE "def_rel" (define_recursion_relation (rel_name defname) domT ranT fvar f abstract_qglrs clauses RCss) lthy |
|
898 |
||
899 |
val (_, lthy) = |
|
22496 | 900 |
LocalTheory.abbrev Syntax.default_mode ((dom_name defname, NoSyn), mk_acc domT R) lthy |
22166 | 901 |
|
902 |
val newthy = ProofContext.theory_of lthy |
|
903 |
val clauses = map (transfer_clause_ctx newthy) clauses |
|
904 |
||
905 |
val cert = cterm_of (ProofContext.theory_of lthy) |
|
906 |
||
907 |
val xclauses = PROFILE "xclauses" (map7 (mk_clause_info globals G f) (1 upto n) clauses abstract_qglrs trees RCss GIntro_thms) RIntro_thmss |
|
908 |
||
909 |
val complete = mk_completeness globals clauses abstract_qglrs |> cert |> assume |
|
910 |
val compat = mk_compat_proof_obligations domT ranT fvar f abstract_qglrs |> map (cert #> assume) |
|
911 |
||
912 |
val compat_store = store_compat_thms n compat |
|
913 |
||
22617 | 914 |
val (goalstate, values) = PROFILE "prove_stuff" (prove_stuff lthy congs globals G f R xclauses complete compat compat_store G_elim) f_defthm |
22166 | 915 |
|
916 |
val mk_trsimps = mk_trsimps lthy globals f G R f_defthm R_elim G_induct xclauses |
|
917 |
||
918 |
fun mk_partial_rules provedgoal = |
|
919 |
let |
|
920 |
val newthy = theory_of_thm provedgoal (*FIXME*) |
|
921 |
||
922 |
val (graph_is_function, complete_thm) = |
|
923 |
provedgoal |
|
924 |
|> Conjunction.elim |
|
925 |
|> apfst (forall_elim_vars 0) |
|
926 |
||
927 |
val f_iff = graph_is_function RS (f_defthm RS ex1_implies_iff) |
|
928 |
||
929 |
val psimps = PROFILE "Proving simplification rules" (mk_psimps newthy globals R xclauses values f_iff) graph_is_function |
|
930 |
||
931 |
val (subset_pinduct, simple_pinduct) = PROFILE "Proving partial induction rule" |
|
932 |
(mk_partial_induct_rule newthy globals R complete_thm) xclauses |
|
933 |
||
934 |
||
935 |
val total_intro = PROFILE "Proving nested termination rule" (mk_nest_term_rule newthy globals R R_elim) xclauses |
|
936 |
||
937 |
val dom_intros = if domintros |
|
938 |
then SOME (PROFILE "Proving domain introduction rules" (map (mk_domain_intro newthy globals R R_elim)) xclauses) |
|
939 |
else NONE |
|
940 |
val trsimps = if tailrec then SOME (mk_trsimps psimps) else NONE |
|
941 |
||
942 |
in |
|
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22617
diff
changeset
|
943 |
FundefResult {fs=[f], G=G, R=R, cases=complete_thm, |
22166 | 944 |
psimps=psimps, subset_pinducts=[subset_pinduct], simple_pinducts=[simple_pinduct], |
945 |
termination=total_intro, trsimps=trsimps, |
|
946 |
domintros=dom_intros} |
|
947 |
end |
|
948 |
in |
|
949 |
((f, goalstate, mk_partial_rules), lthy) |
|
950 |
end |
|
951 |
||
952 |
||
953 |
||
954 |
||
955 |
end |