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