author  paulson 
Wed, 21 May 1997 10:57:38 +0200  
changeset 3271  b873969b05d3 
parent 3245  241838c01caf 
child 3302  404fe31fd8d2 
permissions  rwrr 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

1 
(* 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

2 
there are 3 postprocessors that get applied to the definition: 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

3 

241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

4 
 a wellfoundedness prover (WF_TAC) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

5 
 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

6 
 a termination prover 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

7 
**) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

8 

3191  9 
signature TFL = 
10 
sig 

2112  11 
structure Prim : TFL_sig 
12 

3191  13 
val tgoalw : theory > thm list > thm list > thm list 
14 
val tgoal: theory > thm list > thm list 

2112  15 

16 
val WF_TAC : thm list > tactic 

17 

18 
val simplifier : thm > thm 

19 
val std_postprocessor : theory 

20 
> {induction:thm, rules:thm, TCs:term list list} 

21 
> {induction:thm, rules:thm, nested_tcs:thm list} 

22 

3191  23 
val define_i : theory > term > term > theory * (thm * Prim.pattern list) 
24 
val define : theory > string > string list > theory * Prim.pattern list 

2112  25 

3191  26 
val simplify_defn : theory * (string * Prim.pattern list) 
27 
> {rules:thm list, induct:thm, tcs:term list} 

2112  28 

3191  29 
(* 
30 
val function : theory > term > {theory:theory, eq_ind : thm} 

31 
val lazyR_def: theory > term > {theory:theory, eqns : thm} 

32 
**) 

2112  33 

34 
val tflcongs : theory > thm list 

35 

3191  36 
end; 
37 

38 

39 
structure Tfl: TFL = 

2112  40 
struct 
41 
structure Prim = Prim 

3191  42 
structure S = Prim.USyntax 
2112  43 

3191  44 
(* 
45 
* Extract termination goals so that they can be put it into a goalstack, or 

46 
* have a tactic directly applied to them. 

47 
**) 

48 
fun termination_goals rules = 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

49 
map (Logic.freeze_vars o HOLogic.dest_Trueprop) 
3191  50 
(foldr (fn (th,A) => union_term (prems_of th, A)) (rules, [])); 
51 

52 
(* 

53 
* Finds the termination conditions in (highly massaged) definition and 

54 
* puts them into a goalstack. 

55 
**) 

56 
fun tgoalw thy defs rules = 

57 
let val L = termination_goals rules 

2112  58 
open USyntax 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

59 
val g = cterm_of (sign_of thy) (HOLogic.mk_Trueprop(list_mk_conj L)) 
2112  60 
in goalw_cterm defs g 
61 
end; 

62 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

63 
fun tgoal thy = tgoalw thy []; 
2112  64 

3191  65 
(* 
66 
* Simple wellfoundedness prover. 

67 
**) 

2112  68 
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

69 
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

70 
wf_pred_list, wf_trancl]; 
2112  71 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

72 
val terminator = simp_tac(!simpset addsimps [less_Suc_eq, pred_list_def]) 1 
3208  73 
THEN TRY(best_tac 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

74 
(!claset addSDs [not0_implies_Suc] 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

75 
addss (!simpset)) 1); 
3191  76 

2112  77 
val simpls = [less_eq RS eq_reflection, 
3208  78 
lex_prod_def, rprod_def, measure_def, inv_image_def]; 
2112  79 

3191  80 
(* 
81 
* Does some standard things with the termination conditions of a definition: 

82 
* attempts to prove wellfoundedness of the given relation; simplifies the 

83 
* nonproven termination conditions; and finally attempts to prove the 

84 
* simplified termination conditions. 

85 
**) 

2112  86 
val std_postprocessor = Prim.postprocess{WFtac = WFtac, 
87 
terminator = terminator, 

88 
simplifier = Prim.Rules.simpl_conv simpls}; 

89 

3208  90 
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

91 
[pred_list_def]); 
3191  92 

2112  93 
fun tflcongs thy = Prim.Context.read() @ (#case_congs(Thry.extract_info thy)); 
94 

95 

96 
val concl = #2 o Prim.Rules.dest_thm; 

97 

98 
(* 

3191  99 
* Defining a function with an associated termination relation. 
100 
**) 

101 
fun define_i thy R eqs = 

102 
let val dummy = require_thy thy "WF_Rel" "recursive function definitions"; 

103 

104 
val {functional,pats} = Prim.mk_functional thy eqs 

105 
val (thm,thry) = Prim.wfrec_definition0 thy R functional 

106 
in (thry,(thm,pats)) 

107 
end; 

108 

109 
(*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

110 
(whose signature is a draft and therefore useless) *) 
3191  111 
fun define thy R eqs = 
112 
let fun read thy = readtm (sign_of thy) (TVar(("DUMMY",0),[])) 

113 
val (thy',(_,pats)) = 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

114 
define_i thy (read thy R) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

115 
(fold_bal (app Ind_Syntax.conj) (map (read thy) eqs)) 
3191  116 
in (thy',pats) end 
117 
handle Utils.ERR {mesg,...} => error mesg; 

118 

119 
(* 

120 
* Postprocess a definition made by "define". This is a separate stage of 

121 
* processing from the definition stage. 

2112  122 
**) 
123 
local 

124 
structure R = Prim.Rules 

125 
structure U = Utils 

126 

3191  127 
(* The rest of these local definitions are for the tricky nested case *) 
2112  128 
val solved = not o U.can S.dest_eq o #2 o S.strip_forall o concl 
129 

130 
fun id_thm th = 

131 
let val {lhs,rhs} = S.dest_eq(#2(S.strip_forall(#2 (R.dest_thm th)))) 

132 
in S.aconv lhs rhs 

133 
end handle _ => false 

134 

135 
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]); 

136 
val P_imp_P_iff_True = prover "P > (P= True)" RS mp; 

137 
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection; 

138 
fun mk_meta_eq r = case concl_of r of 

139 
Const("==",_)$_$_ => r 

140 
 _$(Const("op =",_)$_$_) => r RS eq_reflection 

141 
 _ => r RS P_imp_P_eq_True 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

142 
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

143 
fun reducer thl = rewrite (map standard thl @ #simps(rep_ss (!simpset))) 
2112  144 

145 
fun join_assums th = 

146 
let val {sign,...} = rep_thm th 

147 
val tych = cterm_of sign 

148 
val {lhs,rhs} = S.dest_eq(#2 (S.strip_forall (concl th))) 

149 
val cntxtl = (#1 o S.strip_imp) lhs (* cntxtl should = cntxtr *) 

150 
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

151 
val cntxt = gen_union (op aconv) (cntxtl, cntxtr) 
2112  152 
in 
3191  153 
R.GEN_ALL 
154 
(R.DISCH_ALL 

155 
(rewrite (map (R.ASSUME o tych) cntxt) (R.SPEC_ALL th))) 

2112  156 
end 
157 
val gen_all = S.gen_all 

158 
in 

3191  159 
(* 
160 
* The "reducer" argument is 

3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

161 
* (fn thl => rewrite (map standard thl @ #simps(rep_ss (!simpset)))); 
3191  162 
**) 
163 
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

164 
let val dummy = prs "Proving induction theorem.. " 
3191  165 
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

166 
val dummy = writeln "Proved induction theorem." 
3191  167 
val pp = std_postprocessor theory 
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

168 
val dummy = prs "Postprocessing.. " 
3191  169 
val {rules,induction,nested_tcs} = pp{rules=rules,induction=ind,TCs=TCs} 
170 
in 

171 
case nested_tcs 

3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

172 
of [] => (writeln "Postprocessing done."; 
3191  173 
{induction=induction, rules=rules,tcs=[]}) 
3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

174 
 L => let val dummy = prs "Simplifying nested TCs.. " 
2112  175 
val (solved,simplified,stubborn) = 
176 
U.itlist (fn th => fn (So,Si,St) => 

177 
if (id_thm th) then (So, Si, th::St) else 

178 
if (solved th) then (th::So, Si, St) 

179 
else (So, th::Si, St)) nested_tcs ([],[],[]) 

180 
val simplified' = map join_assums simplified 

181 
val induction' = reducer (solved@simplified') induction 

182 
val rules' = reducer (solved@simplified') rules 

3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

183 
val dummy = writeln "Postprocessing done." 
2112  184 
in 
185 
{induction = induction', 

186 
rules = rules', 

3191  187 
tcs = map (gen_all o S.rhs o #2 o S.strip_forall o concl) 
188 
(simplified@stubborn)} 

2112  189 
end 
3191  190 
end handle (e as Utils.ERR _) => Utils.Raise e 
191 
 e => print_exn e; 

192 

193 

3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

194 
(*lcp: uncurry the predicate of the induction rule*) 
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

195 
fun uncurry_rule rl = Prod_Syntax.split_rule_var 
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

196 
(head_of (HOLogic.dest_Trueprop (concl_of rl)), 
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

197 
rl); 
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

198 

3191  199 
(*lcp: put a theorem into Isabelle form, using metalevel connectives*) 
200 
val meta_outer = 

3271
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

201 
uncurry_rule o standard o 
b873969b05d3
Basis library input/output primitives; currying the induction rule;
paulson
parents:
3245
diff
changeset

202 
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

203 
ORELSE' etac conjE)); 
3191  204 

205 
(*Strip off the outer !P*) 

206 
val spec'= read_instantiate [("x","P::?'b=>bool")] spec; 

2112  207 

208 

3191  209 
fun simplify_defn (thy,(id,pats)) = 
3208  210 
let val dummy = deny (id mem map ! (stamps_of_thy thy)) 
211 
("Recursive definition " ^ id ^ 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

212 
" would clash with the theory of the same name!") 
3208  213 
val def = freezeT(get_def thy id RS meta_eq_to_obj_eq) 
3191  214 
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

215 
Prim.post_definition (thy,(def,pats)) 
3191  216 
val {lhs=f,rhs} = S.dest_eq(concl def) 
217 
val (_,[R,_]) = S.strip_comb rhs 

218 
val {induction, rules, tcs} = 

219 
proof_stage theory reducer 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

220 
{f = f, R = R, rules = rules, 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

221 
full_pats_TCs = full_pats_TCs, 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

222 
TCs = TCs} 
3191  223 
val rules' = map (standard o normalize_thm [RSmp]) (R.CONJUNCTS rules) 
224 
in {induct = meta_outer 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

225 
(normalize_thm [RSspec,RSmp] (induction RS spec')), 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

226 
rules = rules', 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3208
diff
changeset

227 
tcs = (termination_goals rules') @ tcs} 
3191  228 
end 
229 
handle Utils.ERR {mesg,...} => error mesg 

2112  230 
end; 
231 

3191  232 
(* 
233 
* 

234 
* Definitions with synthesized termination relation temporarily 

235 
* deleted  it's not clear how to integrate this facility with 

236 
* the Isabelle theory file scheme, which restricts 

237 
* inference at theoryconstruction time. 

238 
* 

2112  239 

3208  240 
local structure R = Prim.Rules 
2112  241 
in 
242 
fun function theory eqs = 

3208  243 
let val dummy = prs "Making definition.. " 
2112  244 
val {rules,R,theory,full_pats_TCs,...} = Prim.lazyR_def theory eqs 
245 
val f = func_of_cond_eqn (concl(R.CONJUNCT1 rules handle _ => rules)) 

3208  246 
val dummy = prs "Definition made.\n" 
247 
val dummy = prs "Proving induction theorem.. " 

2112  248 
val induction = Prim.mk_induction theory f R full_pats_TCs 
3208  249 
val dummy = prs "Induction theorem proved.\n" 
2112  250 
in {theory = theory, 
251 
eq_ind = standard (induction RS (rules RS conjI))} 

252 
end 

253 
handle (e as Utils.ERR _) => Utils.Raise e 

254 
 e => print_exn e 

255 
end; 

256 

257 

258 
fun lazyR_def theory eqs = 

259 
let val {rules,theory, ...} = Prim.lazyR_def theory eqs 

260 
in {eqns=rules, theory=theory} 

261 
end 

262 
handle (e as Utils.ERR _) => Utils.Raise e 

263 
 e => print_exn e; 

3191  264 
* 
265 
* 

266 
**) 

267 

268 

2112  269 

270 

3191  271 
(* 
272 
* Install the basic context notions. Others (for nat and list and prod) 

273 
* have already been added in thry.sml 

274 
**) 

275 
val () = Prim.Context.write[Thms.LET_CONG, Thms.COND_CONG]; 

2112  276 

277 
end; 