| author | haftmann | 
| Mon, 04 Oct 2010 12:22:58 +0200 | |
| changeset 39915 | ecf97cf3d248 | 
| parent 38864 | 4abe644fcea5 | 
| child 41164 | 6854e9a40edc | 
| permissions | -rw-r--r-- | 
| 23150 | 1  | 
(* Title: HOL/Tools/TFL/post.ML  | 
2  | 
Author: Konrad Slind, Cambridge University Computer Laboratory  | 
|
3  | 
Copyright 1997 University of Cambridge  | 
|
4  | 
||
5  | 
Second part of main module (postprocessing of TFL definitions).  | 
|
6  | 
*)  | 
|
7  | 
||
8  | 
signature TFL =  | 
|
9  | 
sig  | 
|
10  | 
val define_i: bool -> theory -> claset -> simpset -> thm list -> thm list -> xstring ->  | 
|
| 
35756
 
cfde251d03a5
refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
 
bulwahn 
parents: 
35625 
diff
changeset
 | 
11  | 
    term -> term list -> theory * {lhs: term, rules: (thm * int) list, induct: thm, tcs: term list}
 | 
| 23150 | 12  | 
val define: bool -> theory -> claset -> simpset -> thm list -> thm list -> xstring ->  | 
| 
35756
 
cfde251d03a5
refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
 
bulwahn 
parents: 
35625 
diff
changeset
 | 
13  | 
    string -> string list -> theory * {lhs: term, rules: (thm * int) list, induct: thm, tcs: term list}
 | 
| 23150 | 14  | 
val defer_i: theory -> thm list -> xstring -> term list -> theory * thm  | 
15  | 
val defer: theory -> thm list -> xstring -> string list -> theory * thm  | 
|
16  | 
end;  | 
|
17  | 
||
18  | 
structure Tfl: TFL =  | 
|
19  | 
struct  | 
|
20  | 
||
21  | 
structure S = USyntax  | 
|
22  | 
||
23  | 
(* misc *)  | 
|
24  | 
||
25  | 
(*---------------------------------------------------------------------------  | 
|
26  | 
* Extract termination goals so that they can be put it into a goalstack, or  | 
|
27  | 
* have a tactic directly applied to them.  | 
|
28  | 
*--------------------------------------------------------------------------*)  | 
|
29  | 
fun termination_goals rules =  | 
|
| 33832 | 30  | 
map (Type.legacy_freeze o HOLogic.dest_Trueprop)  | 
| 33339 | 31  | 
(fold_rev (union (op aconv) o prems_of) rules []);  | 
| 23150 | 32  | 
|
33  | 
(*---------------------------------------------------------------------------  | 
|
34  | 
* Three postprocessors are applied to the definition. It  | 
|
35  | 
* attempts to prove wellfoundedness of the given relation, simplifies the  | 
|
36  | 
* non-proved termination conditions, and finally attempts to prove the  | 
|
37  | 
* simplified termination conditions.  | 
|
38  | 
*--------------------------------------------------------------------------*)  | 
|
39  | 
fun std_postprocessor strict cs ss wfs =  | 
|
40  | 
Prim.postprocess strict  | 
|
41  | 
   {wf_tac     = REPEAT (ares_tac wfs 1),
 | 
|
42  | 
terminator = asm_simp_tac ss 1  | 
|
| 
30686
 
47a32dd1b86e
moved generic arith_tac (formerly silent_arith_tac), verbose_arith_tac (formerly arith_tac) to Arith_Data; simple_arith-tac now named linear_arith_tac
 
haftmann 
parents: 
30364 
diff
changeset
 | 
43  | 
THEN TRY (Arith_Data.arith_tac (Simplifier.the_context ss) 1 ORELSE  | 
| 23880 | 44  | 
                           fast_tac (cs addSDs [@{thm not0_implies_Suc}] addss ss) 1),
 | 
| 23150 | 45  | 
simplifier = Rules.simpl_conv ss []};  | 
46  | 
||
47  | 
||
48  | 
||
49  | 
val concl = #2 o Rules.dest_thm;  | 
|
50  | 
||
51  | 
(*---------------------------------------------------------------------------  | 
|
52  | 
* Postprocess a definition made by "define". This is a separate stage of  | 
|
53  | 
* processing from the definition stage.  | 
|
54  | 
*---------------------------------------------------------------------------*)  | 
|
55  | 
local  | 
|
56  | 
structure R = Rules  | 
|
57  | 
structure U = Utils  | 
|
58  | 
||
59  | 
(* The rest of these local definitions are for the tricky nested case *)  | 
|
60  | 
val solved = not o can S.dest_eq o #2 o S.strip_forall o concl  | 
|
61  | 
||
62  | 
fun id_thm th =  | 
|
63  | 
   let val {lhs,rhs} = S.dest_eq (#2 (S.strip_forall (#2 (R.dest_thm th))));
 | 
|
64  | 
in lhs aconv rhs end  | 
|
65  | 
handle U.ERR _ => false;  | 
|
66  | 
||
| 30737 | 67  | 
val P_imp_P_eq_True = @{thm eqTrueI} RS eq_reflection;
 | 
| 23150 | 68  | 
fun mk_meta_eq r = case concl_of r of  | 
69  | 
     Const("==",_)$_$_ => r
 | 
|
| 
38864
 
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
 
haftmann 
parents: 
38549 
diff
changeset
 | 
70  | 
  |   _ $(Const(@{const_name HOL.eq},_)$_$_) => r RS eq_reflection
 | 
| 23150 | 71  | 
| _ => r RS P_imp_P_eq_True  | 
72  | 
||
73  | 
(*Is this the best way to invoke the simplifier??*)  | 
|
| 33317 | 74  | 
fun rewrite L = rewrite_rule (map mk_meta_eq (filter_out id_thm L))  | 
| 23150 | 75  | 
|
76  | 
fun join_assums th =  | 
|
| 
26626
 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 
wenzelm 
parents: 
26478 
diff
changeset
 | 
77  | 
let val thy = Thm.theory_of_thm th  | 
| 23150 | 78  | 
val tych = cterm_of thy  | 
79  | 
      val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th)))
 | 
|
80  | 
val cntxtl = (#1 o S.strip_imp) lhs (* cntxtl should = cntxtr *)  | 
|
81  | 
val cntxtr = (#1 o S.strip_imp) rhs (* but union is solider *)  | 
|
| 33042 | 82  | 
val cntxt = union (op aconv) cntxtl cntxtr  | 
| 23150 | 83  | 
in  | 
84  | 
R.GEN_ALL  | 
|
85  | 
(R.DISCH_ALL  | 
|
86  | 
(rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th)))  | 
|
87  | 
end  | 
|
88  | 
val gen_all = S.gen_all  | 
|
89  | 
in  | 
|
90  | 
fun proof_stage strict cs ss wfs theory {f, R, rules, full_pats_TCs, TCs} =
 | 
|
91  | 
let  | 
|
| 26478 | 92  | 
val _ = writeln "Proving induction theorem ..."  | 
| 23150 | 93  | 
    val ind = Prim.mk_induction theory {fconst=f, R=R, SV=[], pat_TCs_list=full_pats_TCs}
 | 
| 26478 | 94  | 
val _ = writeln "Postprocessing ...";  | 
| 23150 | 95  | 
    val {rules, induction, nested_tcs} =
 | 
96  | 
      std_postprocessor strict cs ss wfs theory {rules=rules, induction=ind, TCs=TCs}
 | 
|
97  | 
in  | 
|
98  | 
case nested_tcs  | 
|
99  | 
  of [] => {induction=induction, rules=rules,tcs=[]}
 | 
|
| 26478 | 100  | 
| L => let val dummy = writeln "Simplifying nested TCs ..."  | 
| 23150 | 101  | 
val (solved,simplified,stubborn) =  | 
102  | 
fold_rev (fn th => fn (So,Si,St) =>  | 
|
103  | 
if (id_thm th) then (So, Si, th::St) else  | 
|
104  | 
if (solved th) then (th::So, Si, St)  | 
|
105  | 
else (So, th::Si, St)) nested_tcs ([],[],[])  | 
|
106  | 
val simplified' = map join_assums simplified  | 
|
107  | 
val dummy = (Prim.trace_thms "solved =" solved;  | 
|
108  | 
Prim.trace_thms "simplified' =" simplified')  | 
|
109  | 
val rewr = full_simplify (ss addsimps (solved @ simplified'));  | 
|
110  | 
val dummy = Prim.trace_thms "Simplifying the induction rule..."  | 
|
111  | 
[induction]  | 
|
112  | 
val induction' = rewr induction  | 
|
113  | 
val dummy = Prim.trace_thms "Simplifying the recursion rules..."  | 
|
114  | 
[rules]  | 
|
115  | 
val rules' = rewr rules  | 
|
| 26478 | 116  | 
val _ = writeln "... Postprocessing finished";  | 
| 23150 | 117  | 
in  | 
118  | 
          {induction = induction',
 | 
|
119  | 
rules = rules',  | 
|
120  | 
tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl)  | 
|
121  | 
(simplified@stubborn)}  | 
|
122  | 
end  | 
|
123  | 
end;  | 
|
124  | 
||
125  | 
||
126  | 
(*lcp: curry the predicate of the induction rule*)  | 
|
127  | 
fun curry_rule rl =  | 
|
| 35365 | 128  | 
Split_Rule.split_rule_var (Term.head_of (HOLogic.dest_Trueprop (concl_of rl))) rl;  | 
| 23150 | 129  | 
|
130  | 
(*lcp: put a theorem into Isabelle form, using meta-level connectives*)  | 
|
| 36546 | 131  | 
fun meta_outer ctxt =  | 
| 
35021
 
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
 
wenzelm 
parents: 
33832 
diff
changeset
 | 
132  | 
curry_rule o Drule.export_without_context o  | 
| 36546 | 133  | 
rule_by_tactic ctxt (REPEAT (FIRSTGOAL (resolve_tac [allI, impI, conjI] ORELSE' etac conjE)));  | 
| 23150 | 134  | 
|
135  | 
(*Strip off the outer !P*)  | 
|
| 27239 | 136  | 
val spec'= read_instantiate @{context} [(("x", 0), "P::?'b=>bool")] spec;
 | 
| 23150 | 137  | 
|
138  | 
fun tracing true _ = ()  | 
|
139  | 
| tracing false msg = writeln msg;  | 
|
140  | 
||
141  | 
fun simplify_defn strict thy cs ss congs wfs id pats def0 =  | 
|
| 36546 | 142  | 
let  | 
| 
36610
 
bafd82950e24
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
 
wenzelm 
parents: 
36546 
diff
changeset
 | 
143  | 
val ctxt = ProofContext.init_global thy  | 
| 
36616
 
712724c32ac8
replaced Thm.legacy_freezeT by Thm.unvarify_global -- these facts stem from closed definitions, i.e. there are no term Vars;
 
wenzelm 
parents: 
36615 
diff
changeset
 | 
144  | 
val def = Thm.unvarify_global def0 RS meta_eq_to_obj_eq  | 
| 23150 | 145  | 
       val {rules,rows,TCs,full_pats_TCs} =
 | 
146  | 
Prim.post_definition congs (thy, (def,pats))  | 
|
147  | 
       val {lhs=f,rhs} = S.dest_eq (concl def)
 | 
|
148  | 
val (_,[R,_]) = S.strip_comb rhs  | 
|
149  | 
val dummy = Prim.trace_thms "congs =" congs  | 
|
150  | 
(*the next step has caused simplifier looping in some cases*)  | 
|
151  | 
       val {induction, rules, tcs} =
 | 
|
152  | 
proof_stage strict cs ss wfs thy  | 
|
153  | 
               {f = f, R = R, rules = rules,
 | 
|
154  | 
full_pats_TCs = full_pats_TCs,  | 
|
155  | 
TCs = TCs}  | 
|
| 35625 | 156  | 
val rules' = map (Drule.export_without_context o Object_Logic.rulify_no_asm)  | 
| 23150 | 157  | 
(R.CONJUNCTS rules)  | 
| 36546 | 158  | 
         in  {induct = meta_outer ctxt (Object_Logic.rulify_no_asm (induction RS spec')),
 | 
| 23150 | 159  | 
rules = ListPair.zip(rules', rows),  | 
160  | 
tcs = (termination_goals rules') @ tcs}  | 
|
161  | 
end  | 
|
162  | 
  handle U.ERR {mesg,func,module} =>
 | 
|
163  | 
error (mesg ^  | 
|
164  | 
"\n (In TFL function " ^ module ^ "." ^ func ^ ")");  | 
|
165  | 
||
166  | 
||
167  | 
(* Derive the initial equations from the case-split rules to meet the  | 
|
168  | 
users specification of the recursive function.  | 
|
169  | 
Note: We don't do this if the wf conditions fail to be solved, as each  | 
|
170  | 
case may have a different wf condition. We could group the conditions  | 
|
171  | 
together and say that they must be true to solve the general case,  | 
|
172  | 
but that would hide from the user which sub-case they were related  | 
|
173  | 
to. Probably this is not important, and it would work fine, but, for now, I  | 
|
174  | 
prefer leaving more fine-grain control to the user.  | 
|
175  | 
-- Lucas Dixon, Aug 2004 *)  | 
|
176  | 
local  | 
|
177  | 
fun get_related_thms i =  | 
|
| 32952 | 178  | 
map_filter ((fn (r,x) => if x = i then SOME r else NONE));  | 
| 23150 | 179  | 
|
180  | 
fun solve_eq (th, [], i) =  | 
|
181  | 
error "derive_init_eqs: missing rules"  | 
|
182  | 
| solve_eq (th, [a], i) = [(a, i)]  | 
|
183  | 
| solve_eq (th, splitths as (_ :: _), i) =  | 
|
184  | 
(writeln "Proving unsplit equation...";  | 
|
| 35625 | 185  | 
[((Drule.export_without_context o Object_Logic.rulify_no_asm)  | 
| 23150 | 186  | 
(CaseSplit.splitto splitths th), i)])  | 
187  | 
(* if there's an error, pretend nothing happened with this definition  | 
|
188  | 
We should probably print something out so that the user knows...? *)  | 
|
189  | 
handle ERROR s =>  | 
|
190  | 
             (warning ("recdef (solve_eq): " ^ s); map (fn x => (x,i)) splitths);
 | 
|
191  | 
in  | 
|
192  | 
fun derive_init_eqs sgn rules eqs =  | 
|
193  | 
let  | 
|
| 33245 | 194  | 
val eqths = map (Thm.trivial o (Thm.cterm_of sgn) o HOLogic.mk_Trueprop) eqs  | 
195  | 
fun countlist l =  | 
|
196  | 
rev (snd (fold (fn e => fn (i, L) => (i + 1, (e, i) :: L)) l (0, [])))  | 
|
| 23150 | 197  | 
in  | 
| 33245 | 198  | 
maps (fn (e, i) => solve_eq (e, (get_related_thms i rules), i)) (countlist eqths)  | 
| 23150 | 199  | 
end;  | 
200  | 
end;  | 
|
201  | 
||
202  | 
||
203  | 
(*---------------------------------------------------------------------------  | 
|
204  | 
* Defining a function with an associated termination relation.  | 
|
205  | 
*---------------------------------------------------------------------------*)  | 
|
206  | 
fun define_i strict thy cs ss congs wfs fid R eqs =  | 
|
207  | 
  let val {functional,pats} = Prim.mk_functional thy eqs
 | 
|
| 
35799
 
7adb03f27b28
preserve full const name more carefully, and avoid slightly odd Sign.intern_term;
 
wenzelm 
parents: 
35756 
diff
changeset
 | 
208  | 
val (thy, def) = Prim.wfrec_definition0 thy fid R functional  | 
| 
35756
 
cfde251d03a5
refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
 
bulwahn 
parents: 
35625 
diff
changeset
 | 
209  | 
val (lhs, _) = Logic.dest_equals (prop_of def)  | 
| 23150 | 210  | 
      val {induct, rules, tcs} = 
 | 
211  | 
simplify_defn strict thy cs ss congs wfs fid pats def  | 
|
212  | 
val rules' =  | 
|
213  | 
if strict then derive_init_eqs thy rules eqs  | 
|
214  | 
else rules  | 
|
| 
35756
 
cfde251d03a5
refining and adding Spec_Rules to definitional packages old_primrec, primrec, recdef, size and function
 
bulwahn 
parents: 
35625 
diff
changeset
 | 
215  | 
  in (thy, {lhs = lhs, rules = rules', induct = induct, tcs = tcs}) end;
 | 
| 23150 | 216  | 
|
217  | 
fun define strict thy cs ss congs wfs fid R seqs =  | 
|
| 24707 | 218  | 
define_i strict thy cs ss congs wfs fid  | 
219  | 
(Syntax.read_term_global thy R) (map (Syntax.read_term_global thy) seqs)  | 
|
| 23150 | 220  | 
    handle U.ERR {mesg,...} => error mesg;
 | 
221  | 
||
222  | 
||
223  | 
(*---------------------------------------------------------------------------  | 
|
224  | 
*  | 
|
225  | 
* Definitions with synthesized termination relation  | 
|
226  | 
*  | 
|
227  | 
*---------------------------------------------------------------------------*)  | 
|
228  | 
||
229  | 
fun func_of_cond_eqn tm =  | 
|
230  | 
#1 (S.strip_comb (#lhs (S.dest_eq (#2 (S.strip_forall (#2 (S.strip_imp tm)))))));  | 
|
231  | 
||
232  | 
fun defer_i thy congs fid eqs =  | 
|
| 
35799
 
7adb03f27b28
preserve full const name more carefully, and avoid slightly odd Sign.intern_term;
 
wenzelm 
parents: 
35756 
diff
changeset
 | 
233  | 
 let val {rules,R,theory,full_pats_TCs,SV,...} = Prim.lazyR_def thy fid congs eqs
 | 
| 23150 | 234  | 
val f = func_of_cond_eqn (concl (R.CONJUNCT1 rules handle U.ERR _ => rules));  | 
| 26478 | 235  | 
val dummy = writeln "Proving induction theorem ...";  | 
| 23150 | 236  | 
val induction = Prim.mk_induction theory  | 
237  | 
                        {fconst=f, R=R, SV=SV, pat_TCs_list=full_pats_TCs}
 | 
|
238  | 
in (theory,  | 
|
239  | 
(*return the conjoined induction rule and recursion equations,  | 
|
240  | 
with assumptions remaining to discharge*)  | 
|
| 
35021
 
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
 
wenzelm 
parents: 
33832 
diff
changeset
 | 
241  | 
Drule.export_without_context (induction RS (rules RS conjI)))  | 
| 23150 | 242  | 
end  | 
243  | 
||
244  | 
fun defer thy congs fid seqs =  | 
|
| 24707 | 245  | 
defer_i thy congs fid (map (Syntax.read_term_global thy) seqs)  | 
| 23150 | 246  | 
    handle U.ERR {mesg,...} => error mesg;
 | 
247  | 
end;  | 
|
248  | 
||
249  | 
end;  |