author | paulson |
Tue, 27 May 1997 13:22:30 +0200 | |
changeset 3353 | 9112a2efb9a3 |
parent 3331 | c81c7f8ad333 |
child 3391 | 5e45dd3b64e9 |
permissions | -rw-r--r-- |
3302 | 1 |
(* Title: TFL/post |
2 |
ID: $Id$ |
|
3 |
Author: Konrad Slind, Cambridge University Computer Laboratory |
|
4 |
Copyright 1997 University of Cambridge |
|
5 |
||
6 |
Postprocessing of TFL definitions |
|
7 |
*) |
|
8 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
9 |
(*------------------------------------------------------------------------- |
3302 | 10 |
Three postprocessors are applied to the definition: |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
11 |
- a wellfoundedness prover (WF_TAC) |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
12 |
- a simplifier (tries to eliminate the language of termination expressions) |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
13 |
- a termination prover |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
14 |
*-------------------------------------------------------------------------*) |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
15 |
|
3191 | 16 |
signature TFL = |
17 |
sig |
|
2112 | 18 |
structure Prim : TFL_sig |
19 |
||
3191 | 20 |
val tgoalw : theory -> thm list -> thm list -> thm list |
21 |
val tgoal: theory -> thm list -> thm list |
|
2112 | 22 |
|
23 |
val WF_TAC : thm list -> tactic |
|
24 |
||
25 |
val simplifier : thm -> thm |
|
26 |
val std_postprocessor : theory |
|
27 |
-> {induction:thm, rules:thm, TCs:term list list} |
|
28 |
-> {induction:thm, rules:thm, nested_tcs:thm list} |
|
29 |
||
3331 | 30 |
val define_i : theory -> string -> term -> term |
31 |
-> theory * (thm * Prim.pattern list) |
|
32 |
||
33 |
val define : theory -> string -> string -> string list |
|
34 |
-> theory * Prim.pattern list |
|
2112 | 35 |
|
3191 | 36 |
val simplify_defn : theory * (string * Prim.pattern list) |
37 |
-> {rules:thm list, induct:thm, tcs:term list} |
|
2112 | 38 |
|
3191 | 39 |
(*------------------------------------------------------------------------- |
40 |
val function : theory -> term -> {theory:theory, eq_ind : thm} |
|
41 |
val lazyR_def: theory -> term -> {theory:theory, eqns : thm} |
|
42 |
*-------------------------------------------------------------------------*) |
|
2112 | 43 |
|
44 |
val tflcongs : theory -> thm list |
|
45 |
||
3191 | 46 |
end; |
47 |
||
48 |
||
49 |
structure Tfl: TFL = |
|
2112 | 50 |
struct |
51 |
structure Prim = Prim |
|
3191 | 52 |
structure S = Prim.USyntax |
2112 | 53 |
|
3191 | 54 |
(*--------------------------------------------------------------------------- |
55 |
* Extract termination goals so that they can be put it into a goalstack, or |
|
56 |
* have a tactic directly applied to them. |
|
57 |
*--------------------------------------------------------------------------*) |
|
58 |
fun termination_goals rules = |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
59 |
map (Logic.freeze_vars o HOLogic.dest_Trueprop) |
3191 | 60 |
(foldr (fn (th,A) => union_term (prems_of th, A)) (rules, [])); |
61 |
||
62 |
(*--------------------------------------------------------------------------- |
|
63 |
* Finds the termination conditions in (highly massaged) definition and |
|
64 |
* puts them into a goalstack. |
|
65 |
*--------------------------------------------------------------------------*) |
|
66 |
fun tgoalw thy defs rules = |
|
67 |
let val L = termination_goals rules |
|
2112 | 68 |
open USyntax |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
69 |
val g = cterm_of (sign_of thy) (HOLogic.mk_Trueprop(list_mk_conj L)) |
2112 | 70 |
in goalw_cterm defs g |
71 |
end; |
|
72 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
73 |
fun tgoal thy = tgoalw thy []; |
2112 | 74 |
|
3191 | 75 |
(*--------------------------------------------------------------------------- |
76 |
* Simple wellfoundedness prover. |
|
77 |
*--------------------------------------------------------------------------*) |
|
2112 | 78 |
fun WF_TAC thms = REPEAT(FIRST1(map rtac thms)) |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
79 |
val WFtac = WF_TAC[wf_measure, wf_inv_image, wf_lex_prod, wf_less_than, |
3331 | 80 |
wf_trancl]; |
2112 | 81 |
|
3331 | 82 |
val terminator = simp_tac(!simpset addsimps [less_Suc_eq]) 1 |
3208 | 83 |
THEN TRY(best_tac |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
84 |
(!claset addSDs [not0_implies_Suc] |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
85 |
addss (!simpset)) 1); |
3191 | 86 |
|
2112 | 87 |
val simpls = [less_eq RS eq_reflection, |
3302 | 88 |
lex_prod_def, measure_def, inv_image_def]; |
2112 | 89 |
|
3191 | 90 |
(*--------------------------------------------------------------------------- |
91 |
* Does some standard things with the termination conditions of a definition: |
|
92 |
* attempts to prove wellfoundedness of the given relation; simplifies the |
|
93 |
* non-proven termination conditions; and finally attempts to prove the |
|
94 |
* simplified termination conditions. |
|
95 |
*--------------------------------------------------------------------------*) |
|
2112 | 96 |
val std_postprocessor = Prim.postprocess{WFtac = WFtac, |
97 |
terminator = terminator, |
|
98 |
simplifier = Prim.Rules.simpl_conv simpls}; |
|
99 |
||
3208 | 100 |
val simplifier = rewrite_rule (simpls @ #simps(rep_ss (!simpset)) @ |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
101 |
[pred_list_def]); |
3191 | 102 |
|
2112 | 103 |
fun tflcongs thy = Prim.Context.read() @ (#case_congs(Thry.extract_info thy)); |
104 |
||
105 |
||
106 |
val concl = #2 o Prim.Rules.dest_thm; |
|
107 |
||
108 |
(*--------------------------------------------------------------------------- |
|
3191 | 109 |
* Defining a function with an associated termination relation. |
110 |
*---------------------------------------------------------------------------*) |
|
3331 | 111 |
fun define_i thy fid R eqs = |
112 |
let val dummy = require_thy thy "WF_Rel" "recursive function definitions" |
|
3191 | 113 |
val {functional,pats} = Prim.mk_functional thy eqs |
3331 | 114 |
val (thm,thry) = Prim.wfrec_definition0 thy fid R functional |
3191 | 115 |
in (thry,(thm,pats)) |
116 |
end; |
|
117 |
||
118 |
(*lcp's version: takes strings; doesn't return "thm" |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
119 |
(whose signature is a draft and therefore useless) *) |
3331 | 120 |
fun define thy fid R eqs = |
3191 | 121 |
let fun read thy = readtm (sign_of thy) (TVar(("DUMMY",0),[])) |
122 |
val (thy',(_,pats)) = |
|
3331 | 123 |
define_i thy fid (read thy R) |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
124 |
(fold_bal (app Ind_Syntax.conj) (map (read thy) eqs)) |
3191 | 125 |
in (thy',pats) end |
126 |
handle Utils.ERR {mesg,...} => error mesg; |
|
127 |
||
128 |
(*--------------------------------------------------------------------------- |
|
129 |
* Postprocess a definition made by "define". This is a separate stage of |
|
130 |
* processing from the definition stage. |
|
2112 | 131 |
*---------------------------------------------------------------------------*) |
132 |
local |
|
133 |
structure R = Prim.Rules |
|
134 |
structure U = Utils |
|
135 |
||
3191 | 136 |
(* The rest of these local definitions are for the tricky nested case *) |
2112 | 137 |
val solved = not o U.can S.dest_eq o #2 o S.strip_forall o concl |
138 |
||
139 |
fun id_thm th = |
|
140 |
let val {lhs,rhs} = S.dest_eq(#2(S.strip_forall(#2 (R.dest_thm th)))) |
|
141 |
in S.aconv lhs rhs |
|
142 |
end handle _ => false |
|
143 |
||
144 |
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]); |
|
145 |
val P_imp_P_iff_True = prover "P --> (P= True)" RS mp; |
|
146 |
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection; |
|
147 |
fun mk_meta_eq r = case concl_of r of |
|
148 |
Const("==",_)$_$_ => r |
|
149 |
| _$(Const("op =",_)$_$_) => r RS eq_reflection |
|
150 |
| _ => r RS P_imp_P_eq_True |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
151 |
fun rewrite L = rewrite_rule (map mk_meta_eq (filter(not o id_thm) L)) |
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
152 |
fun reducer thl = rewrite (map standard thl @ #simps(rep_ss (!simpset))) |
2112 | 153 |
|
154 |
fun join_assums th = |
|
155 |
let val {sign,...} = rep_thm th |
|
156 |
val tych = cterm_of sign |
|
157 |
val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th))) |
|
158 |
val cntxtl = (#1 o S.strip_imp) lhs (* cntxtl should = cntxtr *) |
|
159 |
val cntxtr = (#1 o S.strip_imp) rhs (* but union is solider *) |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
160 |
val cntxt = gen_union (op aconv) (cntxtl, cntxtr) |
2112 | 161 |
in |
3191 | 162 |
R.GEN_ALL |
163 |
(R.DISCH_ALL |
|
164 |
(rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th))) |
|
2112 | 165 |
end |
166 |
val gen_all = S.gen_all |
|
167 |
in |
|
3191 | 168 |
(*--------------------------------------------------------------------------- |
169 |
* The "reducer" argument is |
|
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
170 |
* (fn thl => rewrite (map standard thl @ #simps(rep_ss (!simpset)))); |
3191 | 171 |
*---------------------------------------------------------------------------*) |
172 |
fun proof_stage theory reducer {f, R, rules, full_pats_TCs, TCs} = |
|
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
173 |
let val dummy = prs "Proving induction theorem.. " |
3191 | 174 |
val ind = Prim.mk_induction theory f R full_pats_TCs |
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
175 |
val dummy = writeln "Proved induction theorem." |
3191 | 176 |
val pp = std_postprocessor theory |
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
177 |
val dummy = prs "Postprocessing.. " |
3191 | 178 |
val {rules,induction,nested_tcs} = pp{rules=rules,induction=ind,TCs=TCs} |
179 |
in |
|
180 |
case nested_tcs |
|
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
181 |
of [] => (writeln "Postprocessing done."; |
3191 | 182 |
{induction=induction, rules=rules,tcs=[]}) |
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
183 |
| L => let val dummy = prs "Simplifying nested TCs.. " |
2112 | 184 |
val (solved,simplified,stubborn) = |
185 |
U.itlist (fn th => fn (So,Si,St) => |
|
186 |
if (id_thm th) then (So, Si, th::St) else |
|
187 |
if (solved th) then (th::So, Si, St) |
|
188 |
else (So, th::Si, St)) nested_tcs ([],[],[]) |
|
189 |
val simplified' = map join_assums simplified |
|
190 |
val induction' = reducer (solved@simplified') induction |
|
191 |
val rules' = reducer (solved@simplified') rules |
|
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
192 |
val dummy = writeln "Postprocessing done." |
2112 | 193 |
in |
194 |
{induction = induction', |
|
195 |
rules = rules', |
|
3191 | 196 |
tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl) |
197 |
(simplified@stubborn)} |
|
2112 | 198 |
end |
3191 | 199 |
end handle (e as Utils.ERR _) => Utils.Raise e |
200 |
| e => print_exn e; |
|
201 |
||
202 |
||
3302 | 203 |
(*lcp: curry the predicate of the induction rule*) |
204 |
fun curry_rule rl = Prod_Syntax.split_rule_var |
|
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
205 |
(head_of (HOLogic.dest_Trueprop (concl_of rl)), |
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
206 |
rl); |
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
207 |
|
3191 | 208 |
(*lcp: put a theorem into Isabelle form, using meta-level connectives*) |
209 |
val meta_outer = |
|
3302 | 210 |
curry_rule o standard o |
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
211 |
rule_by_tactic (REPEAT_FIRST (resolve_tac [allI, impI, conjI] |
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset
|
212 |
ORELSE' etac conjE)); |
3191 | 213 |
|
214 |
(*Strip off the outer !P*) |
|
215 |
val spec'= read_instantiate [("x","P::?'b=>bool")] spec; |
|
2112 | 216 |
|
217 |
||
3191 | 218 |
fun simplify_defn (thy,(id,pats)) = |
3208 | 219 |
let val dummy = deny (id mem map ! (stamps_of_thy thy)) |
220 |
("Recursive definition " ^ id ^ |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
221 |
" would clash with the theory of the same name!") |
3208 | 222 |
val def = freezeT(get_def thy id RS meta_eq_to_obj_eq) |
3191 | 223 |
val {theory,rules,TCs,full_pats_TCs,patterns} = |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
224 |
Prim.post_definition (thy,(def,pats)) |
3191 | 225 |
val {lhs=f,rhs} = S.dest_eq(concl def) |
226 |
val (_,[R,_]) = S.strip_comb rhs |
|
227 |
val {induction, rules, tcs} = |
|
228 |
proof_stage theory reducer |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
229 |
{f = f, R = R, rules = rules, |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
230 |
full_pats_TCs = full_pats_TCs, |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
231 |
TCs = TCs} |
3191 | 232 |
val rules' = map (standard o normalize_thm [RSmp]) (R.CONJUNCTS rules) |
233 |
in {induct = meta_outer |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
234 |
(normalize_thm [RSspec,RSmp] (induction RS spec')), |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
235 |
rules = rules', |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset
|
236 |
tcs = (termination_goals rules') @ tcs} |
3191 | 237 |
end |
238 |
handle Utils.ERR {mesg,...} => error mesg |
|
2112 | 239 |
end; |
240 |
||
3191 | 241 |
(*--------------------------------------------------------------------------- |
242 |
* |
|
243 |
* Definitions with synthesized termination relation temporarily |
|
244 |
* deleted -- it's not clear how to integrate this facility with |
|
245 |
* the Isabelle theory file scheme, which restricts |
|
246 |
* inference at theory-construction time. |
|
247 |
* |
|
2112 | 248 |
|
3208 | 249 |
local structure R = Prim.Rules |
2112 | 250 |
in |
251 |
fun function theory eqs = |
|
3208 | 252 |
let val dummy = prs "Making definition.. " |
2112 | 253 |
val {rules,R,theory,full_pats_TCs,...} = Prim.lazyR_def theory eqs |
254 |
val f = func_of_cond_eqn (concl(R.CONJUNCT1 rules handle _ => rules)) |
|
3208 | 255 |
val dummy = prs "Definition made.\n" |
256 |
val dummy = prs "Proving induction theorem.. " |
|
2112 | 257 |
val induction = Prim.mk_induction theory f R full_pats_TCs |
3208 | 258 |
val dummy = prs "Induction theorem proved.\n" |
2112 | 259 |
in {theory = theory, |
260 |
eq_ind = standard (induction RS (rules RS conjI))} |
|
261 |
end |
|
262 |
handle (e as Utils.ERR _) => Utils.Raise e |
|
263 |
| e => print_exn e |
|
264 |
end; |
|
265 |
||
266 |
||
267 |
fun lazyR_def theory eqs = |
|
268 |
let val {rules,theory, ...} = Prim.lazyR_def theory eqs |
|
269 |
in {eqns=rules, theory=theory} |
|
270 |
end |
|
271 |
handle (e as Utils.ERR _) => Utils.Raise e |
|
272 |
| e => print_exn e; |
|
3191 | 273 |
* |
274 |
* |
|
275 |
*---------------------------------------------------------------------------*) |
|
276 |
||
277 |
||
2112 | 278 |
|
279 |
||
3191 | 280 |
(*--------------------------------------------------------------------------- |
281 |
* Install the basic context notions. Others (for nat and list and prod) |
|
282 |
* have already been added in thry.sml |
|
283 |
*---------------------------------------------------------------------------*) |
|
284 |
val () = Prim.Context.write[Thms.LET_CONG, Thms.COND_CONG]; |
|
2112 | 285 |
|
286 |
end; |