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