author  wenzelm 
Wed, 15 Feb 2006 21:34:55 +0100  
changeset 19046  bc5c6c9b114e 
parent 18972  2905d1805e1e 
child 19349  36e537f89585 
permissions  rwrr 
10769  1 
(* Title: TFL/tfl.ML 
2 
ID: $Id$ 

3 
Author: Konrad Slind, Cambridge University Computer Laboratory 

4 
Copyright 1997 University of Cambridge 

5 

6 
First part of main module. 

7 
*) 

8 

9 
signature PRIM = 

10 
sig 

11 
val trace: bool ref 

14240  12 
val trace_thms: string > thm list > unit 
13 
val trace_cterms: string > cterm list > unit 

10769  14 
type pattern 
15 
val mk_functional: theory > term list > {functional: term, pats: pattern list} 

16 
val wfrec_definition0: theory > string > term > term > theory * thm 

17 
val post_definition: thm list > theory * (thm * pattern list) > 

18 
{theory: theory, 

19 
rules: thm, 

20 
rows: int list, 

21 
TCs: term list list, 

22 
full_pats_TCs: (term * term list) list} 

23 
val wfrec_eqns: theory > xstring > thm list > term list > 

24 
{WFR: term, 

25 
SV: term list, 

26 
proto_def: term, 

27 
extracta: (thm * term list) list, 

28 
pats: pattern list} 

29 
val lazyR_def: theory > xstring > thm list > term list > 

30 
{theory: theory, 

31 
rules: thm, 

32 
R: term, 

33 
SV: term list, 

34 
full_pats_TCs: (term * term list) list, 

35 
patterns : pattern list} 

36 
val mk_induction: theory > 

37 
{fconst: term, R: term, SV: term list, pat_TCs_list: (term * term list) list} > thm 

11632  38 
val postprocess: bool > {wf_tac: tactic, terminator: tactic, simplifier: cterm > thm} 
39 
> theory > {rules: thm, induction: thm, TCs: term list list} 

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

10769  41 
end; 
42 

43 
structure Prim: PRIM = 

44 
struct 

45 

46 
val trace = ref false; 

47 

48 
structure R = Rules; 

49 
structure S = USyntax; 

50 
structure U = Utils; 

51 

52 

53 
fun TFL_ERR func mesg = U.ERR {module = "Tfl", func = func, mesg = mesg}; 

54 

55 
val concl = #2 o R.dest_thm; 

56 
val hyp = #1 o R.dest_thm; 

57 

58 
val list_mk_type = U.end_itlist (curry (op >)); 

59 

60 
fun enumerate xs = ListPair.zip(xs, 0 upto (length xs  1)); 

61 

62 
fun front_last [] = raise TFL_ERR "front_last" "empty list" 

63 
 front_last [x] = ([],x) 

64 
 front_last (h::t) = 

65 
let val (pref,x) = front_last t 

66 
in 

67 
(h::pref,x) 

68 
end; 

69 

70 

71 
(* 

72 
* The next function is common to patternmatch translation and 

73 
* proof of completeness of cases for the induction theorem. 

74 
* 

75 
* The curried function "gvvariant" returns a function to generate distinct 

76 
* variables that are guaranteed not to be in names. The names of 

77 
* the variables go u, v, ..., z, aa, ..., az, ... The returned 

78 
* function contains embedded refs! 

79 
**) 

80 
fun gvvariant names = 

81 
let val slist = ref names 

82 
val vname = ref "u" 

83 
fun new() = 

84 
if !vname mem_string (!slist) 

12902  85 
then (vname := Symbol.bump_string (!vname); new()) 
10769  86 
else (slist := !vname :: !slist; !vname) 
87 
in 

88 
fn ty => Free(new(), ty) 

89 
end; 

90 

91 

92 
(* 

93 
* Used in induction theorem production. This is the simple case of 

94 
* partitioning up pattern rows by the leading constructor. 

95 
**) 

96 
fun ipartition gv (constructors,rows) = 

97 
let fun pfail s = raise TFL_ERR "partition.part" s 

98 
fun part {constrs = [], rows = [], A} = rev A 

99 
 part {constrs = [], rows = _::_, A} = pfail"extra cases in defn" 

100 
 part {constrs = _::_, rows = [], A} = pfail"cases missing in defn" 

101 
 part {constrs = c::crst, rows, A} = 

16505  102 
let val (c, T) = dest_Const c 
103 
val L = binder_types T 

10769  104 
val (in_group, not_in_group) = 
16853  105 
fold_rev (fn (row as (p::rst, rhs)) => 
10769  106 
fn (in_group,not_in_group) => 
107 
let val (pc,args) = S.strip_comb p 

16505  108 
in if (#1(dest_Const pc) = c) 
10769  109 
then ((args@rst, rhs)::in_group, not_in_group) 
110 
else (in_group, row::not_in_group) 

111 
end) rows ([],[]) 

112 
val col_types = U.take type_of (length L, #1(hd in_group)) 

113 
in 

114 
part{constrs = crst, rows = not_in_group, 

115 
A = {constructor = c, 

116 
new_formals = map gv col_types, 

117 
group = in_group}::A} 

118 
end 

119 
in part{constrs = constructors, rows = rows, A = []} 

120 
end; 

121 

122 

123 

124 
(* 

125 
* Each pattern carries with it a tag (i,b) where 

126 
* i is the clause it came from and 

127 
* b=true indicates that clause was given by the user 

128 
* (or is an instantiation of a user supplied pattern) 

129 
* b=false > i = ~1 

130 
**) 

131 

132 
type pattern = term * (int * bool) 

133 

134 
fun pattern_map f (tm,x) = (f tm, x); 

135 

136 
fun pattern_subst theta = pattern_map (subst_free theta); 

137 

138 
val pat_of = fst; 

139 
fun row_of_pat x = fst (snd x); 

140 
fun given x = snd (snd x); 

141 

142 
(* 

143 
* Produce an instance of a constructor, plus genvars for its arguments. 

144 
**) 

145 
fun fresh_constr ty_match colty gv c = 

146 
let val (_,Ty) = dest_Const c 

147 
val L = binder_types Ty 

148 
and ty = body_type Ty 

149 
val ty_theta = ty_match ty colty 

150 
val c' = S.inst ty_theta c 

151 
val gvars = map (S.inst ty_theta o gv) L 

152 
in (c', gvars) 

153 
end; 

154 

155 

156 
(* 

157 
* Goes through a list of rows and picks out the ones beginning with a 

16505  158 
* pattern with constructor = name. 
10769  159 
**) 
16505  160 
fun mk_group name rows = 
16853  161 
fold_rev (fn (row as ((prfx, p::rst), rhs)) => 
10769  162 
fn (in_group,not_in_group) => 
163 
let val (pc,args) = S.strip_comb p 

16505  164 
in if ((#1 (Term.dest_Const pc) = name) handle TERM _ => false) 
10769  165 
then (((prfx,args@rst), rhs)::in_group, not_in_group) 
166 
else (in_group, row::not_in_group) end) 

167 
rows ([],[]); 

168 

169 
(* 

170 
* Partition the rows. Not efficient: we should use hashing. 

171 
**) 

172 
fun partition _ _ (_,_,_,[]) = raise TFL_ERR "partition" "no rows" 

173 
 partition gv ty_match 

174 
(constructors, colty, res_ty, rows as (((prfx,_),_)::_)) = 

175 
let val fresh = fresh_constr ty_match colty gv 

176 
fun part {constrs = [], rows, A} = rev A 

177 
 part {constrs = c::crst, rows, A} = 

178 
let val (c',gvars) = fresh c 

16505  179 
val (in_group, not_in_group) = mk_group (#1 (dest_Const c')) rows 
10769  180 
val in_group' = 
181 
if (null in_group) (* Constructor not given *) 

182 
then [((prfx, #2(fresh c)), (S.ARB res_ty, (~1,false)))] 

183 
else in_group 

184 
in 

185 
part{constrs = crst, 

186 
rows = not_in_group, 

187 
A = {constructor = c', 

188 
new_formals = gvars, 

189 
group = in_group'}::A} 

190 
end 

191 
in part{constrs=constructors, rows=rows, A=[]} 

192 
end; 

193 

194 
(* 

195 
* Misc. routines used in mk_case 

196 
**) 

197 

198 
fun mk_pat (c,l) = 

199 
let val L = length (binder_types (type_of c)) 

200 
fun build (prfx,tag,plist) = 

15570  201 
let val args = Library.take (L,plist) 
202 
and plist' = Library.drop(L,plist) 

10769  203 
in (prfx,tag,list_comb(c,args)::plist') end 
204 
in map build l end; 

205 

206 
fun v_to_prfx (prfx, v::pats) = (v::prfx,pats) 

207 
 v_to_prfx _ = raise TFL_ERR "mk_case" "v_to_prfx"; 

208 

209 
fun v_to_pats (v::prfx,tag, pats) = (prfx, tag, v::pats) 

210 
 v_to_pats _ = raise TFL_ERR "mk_case" "v_to_pats"; 

211 

212 

213 
(* 

214 
* Translation of pattern terms into nested case expressions. 

215 
* 

216 
* This performs the translation and also builds the full set of patterns. 

217 
* Thus it supports the construction of induction theorems even when an 

218 
* incomplete set of patterns is given. 

219 
**) 

220 

221 
fun mk_case ty_info ty_match usednames range_ty = 

222 
let 

223 
fun mk_case_fail s = raise TFL_ERR "mk_case" s 

224 
val fresh_var = gvvariant usednames 

225 
val divide = partition fresh_var ty_match 

226 
fun expand constructors ty ((_,[]), _) = mk_case_fail"expand_var_row" 

227 
 expand constructors ty (row as ((prfx, p::rst), rhs)) = 

228 
if (is_Free p) 

229 
then let val fresh = fresh_constr ty_match ty fresh_var 

230 
fun expnd (c,gvs) = 

231 
let val capp = list_comb(c,gvs) 

232 
in ((prfx, capp::rst), pattern_subst[(p,capp)] rhs) 

233 
end 

234 
in map expnd (map fresh constructors) end 

235 
else [row] 

236 
fun mk{rows=[],...} = mk_case_fail"no rows" 

237 
 mk{path=[], rows = ((prfx, []), (tm,tag))::_} = (* Done *) 

238 
([(prfx,tag,[])], tm) 

239 
 mk{path=[], rows = _::_} = mk_case_fail"blunder" 

240 
 mk{path as u::rstp, rows as ((prfx, []), rhs)::rst} = 

241 
mk{path = path, 

242 
rows = ((prfx, [fresh_var(type_of u)]), rhs)::rst} 

243 
 mk{path = u::rstp, rows as ((_, p::_), _)::_} = 

244 
let val (pat_rectangle,rights) = ListPair.unzip rows 

245 
val col0 = map(hd o #2) pat_rectangle 

246 
in 

247 
if (forall is_Free col0) 

248 
then let val rights' = map (fn(v,e) => pattern_subst[(v,u)] e) 

249 
(ListPair.zip (col0, rights)) 

250 
val pat_rectangle' = map v_to_prfx pat_rectangle 

251 
val (pref_patl,tm) = mk{path = rstp, 

252 
rows = ListPair.zip (pat_rectangle', 

253 
rights')} 

254 
in (map v_to_pats pref_patl, tm) 

255 
end 

256 
else 

257 
let val pty as Type (ty_name,_) = type_of p 

258 
in 

259 
case (ty_info ty_name) 

15531  260 
of NONE => mk_case_fail("Not a known datatype: "^ty_name) 
261 
 SOME{case_const,constructors} => 

10769  262 
let 
263 
val case_const_name = #1(dest_Const case_const) 

264 
val nrows = List.concat (map (expand constructors pty) rows) 

265 
val subproblems = divide(constructors, pty, range_ty, nrows) 

266 
val groups = map #group subproblems 

267 
and new_formals = map #new_formals subproblems 

268 
and constructors' = map #constructor subproblems 

269 
val news = map (fn (nf,rows) => {path = nf@rstp, rows=rows}) 

270 
(ListPair.zip (new_formals, groups)) 

271 
val rec_calls = map mk news 

272 
val (pat_rect,dtrees) = ListPair.unzip rec_calls 

273 
val case_functions = map S.list_mk_abs 

274 
(ListPair.zip (new_formals, dtrees)) 

275 
val types = map type_of (case_functions@[u]) @ [range_ty] 

276 
val case_const' = Const(case_const_name, list_mk_type types) 

277 
val tree = list_comb(case_const', case_functions@[u]) 

278 
val pat_rect1 = List.concat 

279 
(ListPair.map mk_pat (constructors', pat_rect)) 

280 
in (pat_rect1,tree) 

281 
end 

282 
end end 

283 
in mk 

284 
end; 

285 

286 

287 
(* Repeated variable occurrences in a pattern are not allowed. *) 

288 
fun FV_multiset tm = 

289 
case (S.dest_term tm) 

16505  290 
of S.VAR{Name = c, Ty = T} => [Free(c, T)] 
10769  291 
 S.CONST _ => [] 
292 
 S.COMB{Rator, Rand} => FV_multiset Rator @ FV_multiset Rand 

293 
 S.LAMB _ => raise TFL_ERR "FV_multiset" "lambda"; 

294 

295 
fun no_repeat_vars thy pat = 

296 
let fun check [] = true 

297 
 check (v::rst) = 

298 
if mem_term (v,rst) then 

299 
raise TFL_ERR "no_repeat_vars" 

300 
(quote (#1 (dest_Free v)) ^ 

301 
" occurs repeatedly in the pattern " ^ 

302 
quote (string_of_cterm (Thry.typecheck thy pat))) 

303 
else check rst 

304 
in check (FV_multiset pat) 

305 
end; 

306 

307 
fun dest_atom (Free p) = p 

308 
 dest_atom (Const p) = p 

309 
 dest_atom _ = raise TFL_ERR "dest_atom" "function name not an identifier"; 

310 

311 
fun same_name (p,q) = #1(dest_atom p) = #1(dest_atom q); 

312 

313 
local fun mk_functional_err s = raise TFL_ERR "mk_functional" s 

314 
fun single [_$_] = 

315 
mk_functional_err "recdef does not allow currying" 

316 
 single [f] = f 

317 
 single fs = 

318 
(*multiple function names?*) 

19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18972
diff
changeset

319 
if length (distinct same_name fs) < length fs 
10769  320 
then mk_functional_err 
321 
"The function being declared appears with multiple types" 

322 
else mk_functional_err 

323 
(Int.toString (length fs) ^ 

324 
" distinct function names being declared") 

325 
in 

326 
fun mk_functional thy clauses = 

327 
let val (L,R) = ListPair.unzip (map HOLogic.dest_eq clauses 

328 
handle TERM _ => raise TFL_ERR "mk_functional" 

329 
"recursion equations must use the = relation") 

330 
val (funcs,pats) = ListPair.unzip (map (fn (t$u) =>(t,u)) L) 

19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18972
diff
changeset

331 
val atom = single (distinct (op aconv) funcs) 
10769  332 
val (fname,ftype) = dest_atom atom 
333 
val dummy = map (no_repeat_vars thy) pats 

334 
val rows = ListPair.zip (map (fn x => ([]:term list,[x])) pats, 

335 
map (fn (t,i) => (t,(i,true))) (enumerate R)) 

15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

336 
val names = foldr add_term_names [] R 
10769  337 
val atype = type_of(hd pats) 
338 
and aname = variant names "a" 

339 
val a = Free(aname,atype) 

340 
val ty_info = Thry.match_info thy 

341 
val ty_match = Thry.match_type thy 

342 
val range_ty = type_of (hd R) 

343 
val (patts, case_tm) = mk_case ty_info ty_match (aname::names) range_ty 

344 
{path=[a], rows=rows} 

345 
val patts1 = map (fn (_,tag,[pat]) => (pat,tag)) patts 

346 
handle Match => mk_functional_err "error in patternmatch translation" 

347 
val patts2 = Library.sort (Library.int_ord o Library.pairself row_of_pat) patts1 

348 
val finals = map row_of_pat patts2 

349 
val originals = map (row_of_pat o #2) rows 

350 
val dummy = case (originals\\finals) 

351 
of [] => () 

352 
 L => mk_functional_err 

353 
("The following clauses are redundant (covered by preceding clauses): " ^ 

354 
commas (map (fn i => Int.toString (i + 1)) L)) 

355 
in {functional = Abs(Sign.base_name fname, ftype, 

356 
abstract_over (atom, 

357 
absfree(aname,atype, case_tm))), 

358 
pats = patts2} 

359 
end end; 

360 

361 

362 
(* 

363 
* 

364 
* PRINCIPLES OF DEFINITION 

365 
* 

366 
**) 

367 

368 

369 
(*For Isabelle, the lhs of a definition must be a constant.*) 

16505  370 
fun mk_const_def sign (c, Ty, rhs) = 
18972  371 
Sign.infer_types (Sign.pp sign) sign (Sign.consts_of sign) (K NONE) (K NONE) [] false 
16505  372 
([Const("==",dummyT) $ Const(c,Ty) $ rhs], propT) 
10769  373 
> #1; 
374 

375 
(*Make all TVars available for instantiation by adding a ? to the front*) 

376 
fun poly_tvars (Type(a,Ts)) = Type(a, map (poly_tvars) Ts) 

377 
 poly_tvars (TFree (a,sort)) = TVar (("?" ^ a, 0), sort) 

378 
 poly_tvars (TVar ((a,i),sort)) = TVar (("?" ^ a, i+1), sort); 

379 

380 
local val f_eq_wfrec_R_M = 

381 
#ant(S.dest_imp(#2(S.strip_forall (concl Thms.WFREC_COROLLARY)))) 

382 
val {lhs=f, rhs} = S.dest_eq f_eq_wfrec_R_M 

383 
val (fname,_) = dest_Free f 

384 
val (wfrec,_) = S.strip_comb rhs 

385 
in 

16505  386 
fun wfrec_definition0 thy fid R (functional as Abs(x, Ty, _)) = 
387 
let val def_name = if x<>fid then 

10769  388 
raise TFL_ERR "wfrec_definition0" 
389 
("Expected a definition of " ^ 

390 
quote fid ^ " but found one of " ^ 

16505  391 
quote x) 
392 
else x ^ "_def" 

10769  393 
val wfrec_R_M = map_term_types poly_tvars 
394 
(wfrec $ map_term_types poly_tvars R) 

395 
$ functional 

16505  396 
val def_term = mk_const_def (Theory.sign_of thy) (x, Ty, wfrec_R_M) 
18358  397 
val ([def], thy') = PureThy.add_defs_i false [Thm.no_attributes (def_name, def_term)] thy 
10769  398 
in (thy', def) end; 
399 
end; 

400 

401 

402 

403 
(* 

404 
* This structure keeps track of congruence rules that aren't derived 

405 
* from a datatype definition. 

406 
**) 

407 
fun extraction_thms thy = 

408 
let val {case_rewrites,case_congs} = Thry.extract_info thy 

409 
in (case_rewrites, case_congs) 

410 
end; 

411 

412 

413 
(* 

414 
* Pair patterns with termination conditions. The full list of patterns for 

415 
* a definition is merged with the TCs arising from the usergiven clauses. 

416 
* There can be fewer clauses than the full list, if the user omitted some 

417 
* cases. This routine is used to prepare input for mk_induction. 

418 
**) 

419 
fun merge full_pats TCs = 

420 
let fun insert (p,TCs) = 

421 
let fun insrt ((x as (h,[]))::rst) = 

422 
if (p aconv h) then (p,TCs)::rst else x::insrt rst 

423 
 insrt (x::rst) = x::insrt rst 

424 
 insrt[] = raise TFL_ERR "merge.insert" "pattern not found" 

425 
in insrt end 

426 
fun pass ([],ptcl_final) = ptcl_final 

427 
 pass (ptcs::tcl, ptcl) = pass(tcl, insert ptcs ptcl) 

428 
in 

429 
pass (TCs, map (fn p => (p,[])) full_pats) 

430 
end; 

431 

432 

15570  433 
fun givens pats = map pat_of (List.filter given pats); 
10769  434 

435 
fun post_definition meta_tflCongs (theory, (def, pats)) = 

436 
let val tych = Thry.typecheck theory 

437 
val f = #lhs(S.dest_eq(concl def)) 

438 
val corollary = R.MATCH_MP Thms.WFREC_COROLLARY def 

15570  439 
val pats' = List.filter given pats 
10769  440 
val given_pats = map pat_of pats' 
441 
val rows = map row_of_pat pats' 

442 
val WFR = #ant(S.dest_imp(concl corollary)) 

443 
val R = #Rand(S.dest_comb WFR) 

444 
val corollary' = R.UNDISCH corollary (* put WF R on assums *) 

445 
val corollaries = map (fn pat => R.SPEC (tych pat) corollary') 

446 
given_pats 

447 
val (case_rewrites,context_congs) = extraction_thms theory 

14219  448 
(*case_ss causes minimal simplification: bodies of case expressions are 
16505  449 
not simplified. Otherwise large examples (RedBlack trees) are too 
14219  450 
slow.*) 
14217
9f5679e97eac
Fixed inefficiency in post_definition by adding weak case congruence
berghofe
parents:
12902
diff
changeset

451 
val case_ss = HOL_basic_ss addcongs 
9f5679e97eac
Fixed inefficiency in post_definition by adding weak case congruence
berghofe
parents:
12902
diff
changeset

452 
DatatypePackage.weak_case_congs_of theory addsimps case_rewrites 
9f5679e97eac
Fixed inefficiency in post_definition by adding weak case congruence
berghofe
parents:
12902
diff
changeset

453 
val corollaries' = map (Simplifier.simplify case_ss) corollaries 
10769  454 
val extract = R.CONTEXT_REWRITE_RULE 
455 
(f, [R], cut_apply, meta_tflCongs@context_congs) 

456 
val (rules, TCs) = ListPair.unzip (map extract corollaries') 

457 
val rules0 = map (rewrite_rule [Thms.CUT_DEF]) rules 

458 
val mk_cond_rule = R.FILTER_DISCH_ALL(not o curry (op aconv) WFR) 

459 
val rules1 = R.LIST_CONJ(map mk_cond_rule rules0) 

460 
in 

461 
{theory = theory, 

462 
rules = rules1, 

463 
rows = rows, 

464 
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (given_pats, TCs)), 

465 
TCs = TCs} 

466 
end; 

467 

468 

469 
(* 

470 
* Perform the extraction without making the definition. Definition and 

471 
* extraction commute for the nonnested case. (Deferred recdefs) 

472 
* 

473 
* The purpose of wfrec_eqns is merely to instantiate the recursion theorem 

474 
* and extract termination conditions: no definition is made. 

475 
**) 

476 

477 
fun wfrec_eqns thy fid tflCongs eqns = 

478 
let val {lhs,rhs} = S.dest_eq (hd eqns) 

479 
val (f,args) = S.strip_comb lhs 

480 
val (fname,fty) = dest_atom f 

481 
val (SV,a) = front_last args (* SV = schematic variables *) 

482 
val g = list_comb(f,SV) 

483 
val h = Free(fname,type_of g) 

484 
val eqns1 = map (subst_free[(g,h)]) eqns 

16505  485 
val {functional as Abs(x, Ty, _), pats} = mk_functional thy eqns1 
10769  486 
val given_pats = givens pats 
16505  487 
(* val f = Free(x,Ty) *) 
10769  488 
val Type("fun", [f_dty, f_rty]) = Ty 
16505  489 
val dummy = if x<>fid then 
10769  490 
raise TFL_ERR "wfrec_eqns" 
491 
("Expected a definition of " ^ 

492 
quote fid ^ " but found one of " ^ 

16505  493 
quote x) 
10769  494 
else () 
495 
val (case_rewrites,context_congs) = extraction_thms thy 

496 
val tych = Thry.typecheck thy 

497 
val WFREC_THM0 = R.ISPEC (tych functional) Thms.WFREC_COROLLARY 

498 
val Const("All",_) $ Abs(Rname,Rtype,_) = concl WFREC_THM0 

15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

499 
val R = Free (variant (foldr add_term_names [] eqns) Rname, 
10769  500 
Rtype) 
501 
val WFREC_THM = R.ISPECL [tych R, tych g] WFREC_THM0 

502 
val ([proto_def, WFR],_) = S.strip_imp(concl WFREC_THM) 

503 
val dummy = 

504 
if !trace then 

505 
writeln ("ORIGINAL PROTO_DEF: " ^ 

506 
Sign.string_of_term (Theory.sign_of thy) proto_def) 

507 
else () 

508 
val R1 = S.rand WFR 

509 
val corollary' = R.UNDISCH(R.UNDISCH WFREC_THM) 

510 
val corollaries = map (fn pat => R.SPEC (tych pat) corollary') given_pats 

511 
val corollaries' = map (rewrite_rule case_rewrites) corollaries 

512 
fun extract X = R.CONTEXT_REWRITE_RULE 

513 
(f, R1::SV, cut_apply, tflCongs@context_congs) X 

514 
in {proto_def = proto_def, 

515 
SV=SV, 

516 
WFR=WFR, 

517 
pats=pats, 

518 
extracta = map extract corollaries'} 

519 
end; 

520 

521 

522 
(* 

523 
* Define the constant after extracting the termination conditions. The 

524 
* wellfounded relation used in the definition is computed by using the 

525 
* choice operator on the extracted conditions (plus the condition that 

526 
* such a relation must be wellfounded). 

527 
**) 

528 

529 
fun lazyR_def thy fid tflCongs eqns = 

530 
let val {proto_def,WFR,pats,extracta,SV} = 

531 
wfrec_eqns thy fid tflCongs eqns 

532 
val R1 = S.rand WFR 

533 
val f = #lhs(S.dest_eq proto_def) 

534 
val (extractants,TCl) = ListPair.unzip extracta 

535 
val dummy = if !trace 

536 
then (writeln "Extractants = "; 

537 
prths extractants; 

538 
()) 

539 
else () 

15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

540 
val TCs = foldr (gen_union (op aconv)) [] TCl 
10769  541 
val full_rqt = WFR::TCs 
542 
val R' = S.mk_select{Bvar=R1, Body=S.list_mk_conj full_rqt} 

543 
val R'abs = S.rand R' 

544 
val proto_def' = subst_free[(R1,R')] proto_def 

545 
val dummy = if !trace then writeln ("proto_def' = " ^ 

546 
Sign.string_of_term 

547 
(Theory.sign_of thy) proto_def') 

548 
else () 

549 
val {lhs,rhs} = S.dest_eq proto_def' 

550 
val (c,args) = S.strip_comb lhs 

16505  551 
val (name,Ty) = dest_atom c 
10769  552 
val defn = mk_const_def (Theory.sign_of thy) 
16505  553 
(name, Ty, S.list_mk_abs (args,rhs)) 
18358  554 
val ([def0], theory) = 
10769  555 
thy 
556 
> PureThy.add_defs_i false 

557 
[Thm.no_attributes (fid ^ "_def", defn)] 

558 
val def = freezeT def0; 

559 
val dummy = if !trace then writeln ("DEF = " ^ string_of_thm def) 

560 
else () 

561 
(* val fconst = #lhs(S.dest_eq(concl def)) *) 

562 
val tych = Thry.typecheck theory 

563 
val full_rqt_prop = map (Dcterm.mk_prop o tych) full_rqt 

564 
(*lcp: a lot of objectlogic inference to remove*) 

565 
val baz = R.DISCH_ALL 

16853  566 
(fold_rev R.DISCH full_rqt_prop 
10769  567 
(R.LIST_CONJ extractants)) 
568 
val dum = if !trace then writeln ("baz = " ^ string_of_thm baz) 

569 
else () 

570 
val f_free = Free (fid, fastype_of f) (*'cos f is a Const*) 

571 
val SV' = map tych SV; 

572 
val SVrefls = map reflexive SV' 

16853  573 
val def0 = (fold (fn x => fn th => R.rbeta(combination th x)) 
10769  574 
SVrefls def) 
575 
RS meta_eq_to_obj_eq 

576 
val def' = R.MP (R.SPEC (tych R') (R.GEN (tych R1) baz)) def0 

577 
val body_th = R.LIST_CONJ (map R.ASSUME full_rqt_prop) 

11455
e07927b980ec
defer_recdef (lazyR_def) now looks for theorem Hilbert_Choice.tfl_some
paulson
parents:
10769
diff
changeset

578 
val SELECT_AX = (*in this way we hope to avoid a STATIC dependence upon 
e07927b980ec
defer_recdef (lazyR_def) now looks for theorem Hilbert_Choice.tfl_some
paulson
parents:
10769
diff
changeset

579 
theory Hilbert_Choice*) 
16505  580 
thm "Hilbert_Choice.tfl_some" 
18678  581 
handle ERROR msg => cat_error msg 
11455
e07927b980ec
defer_recdef (lazyR_def) now looks for theorem Hilbert_Choice.tfl_some
paulson
parents:
10769
diff
changeset

582 
"defer_recdef requires theory Main or at least Hilbert_Choice as parent" 
e07927b980ec
defer_recdef (lazyR_def) now looks for theorem Hilbert_Choice.tfl_some
paulson
parents:
10769
diff
changeset

583 
val bar = R.MP (R.ISPECL[tych R'abs, tych R1] SELECT_AX) body_th 
10769  584 
in {theory = theory, R=R1, SV=SV, 
16853  585 
rules = fold (U.C R.MP) (R.CONJUNCTS bar) def', 
10769  586 
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (givens pats, TCl)), 
587 
patterns = pats} 

588 
end; 

589 

590 

591 

592 
(* 

593 
* 

594 
* INDUCTION THEOREM 

595 
* 

596 
**) 

597 

598 

599 
(* Miscellaneous function  

600 
* 

601 
* [x_1,...,x_n] ?v_1...v_n. M[v_1,...,v_n] 

602 
*  

603 
* ( M[x_1,...,x_n], [(x_i,?v_1...v_n. M[v_1,...,v_n]), 

604 
* ... 

605 
* (x_j,?v_n. M[x_1,...,x_(n1),v_n])] ) 

606 
* 

607 
* This function is totally ad hoc. Used in the production of the induction 

608 
* theorem. The nchotomy theorem can have clauses that look like 

609 
* 

610 
* ?v1..vn. z = C vn..v1 

611 
* 

612 
* in which the order of quantification is not the order of occurrence of the 

613 
* quantified variables as arguments to C. Since we have no control over this 

614 
* aspect of the nchotomy theorem, we make the correspondence explicit by 

615 
* pairing the incoming new variable with the term it gets betareduced into. 

616 
**) 

617 

618 
fun alpha_ex_unroll (xlist, tm) = 

619 
let val (qvars,body) = S.strip_exists tm 

620 
val vlist = #2(S.strip_comb (S.rhs body)) 

621 
val plist = ListPair.zip (vlist, xlist) 

17314  622 
val args = map (the o AList.lookup (op aconv) plist) qvars 
15531  623 
handle Option => sys_error 
10769  624 
"TFL fault [alpha_ex_unroll]: no correspondence" 
625 
fun build ex [] = [] 

626 
 build (_$rex) (v::rst) = 

18176  627 
let val ex1 = Term.betapply(rex, v) 
10769  628 
in ex1 :: build ex1 rst 
629 
end 

630 
val (nex::exl) = rev (tm::build tm args) 

631 
in 

632 
(nex, ListPair.zip (args, rev exl)) 

633 
end; 

634 

635 

636 

637 
(* 

638 
* 

639 
* PROVING COMPLETENESS OF PATTERNS 

640 
* 

641 
**) 

642 

643 
fun mk_case ty_info usednames thy = 

644 
let 

645 
val divide = ipartition (gvvariant usednames) 

646 
val tych = Thry.typecheck thy 

647 
fun tych_binding(x,y) = (tych x, tych y) 

648 
fun fail s = raise TFL_ERR "mk_case" s 

649 
fun mk{rows=[],...} = fail"no rows" 

650 
 mk{path=[], rows = [([], (thm, bindings))]} = 

651 
R.IT_EXISTS (map tych_binding bindings) thm 

652 
 mk{path = u::rstp, rows as (p::_, _)::_} = 

653 
let val (pat_rectangle,rights) = ListPair.unzip rows 

654 
val col0 = map hd pat_rectangle 

655 
val pat_rectangle' = map tl pat_rectangle 

656 
in 

657 
if (forall is_Free col0) (* column 0 is all variables *) 

658 
then let val rights' = map (fn ((thm,theta),v) => (thm,theta@[(u,v)])) 

659 
(ListPair.zip (rights, col0)) 

660 
in mk{path = rstp, rows = ListPair.zip (pat_rectangle', rights')} 

661 
end 

662 
else (* column 0 is all constructors *) 

663 
let val Type (ty_name,_) = type_of p 

664 
in 

665 
case (ty_info ty_name) 

15531  666 
of NONE => fail("Not a known datatype: "^ty_name) 
667 
 SOME{constructors,nchotomy} => 

10769  668 
let val thm' = R.ISPEC (tych u) nchotomy 
669 
val disjuncts = S.strip_disj (concl thm') 

670 
val subproblems = divide(constructors, rows) 

671 
val groups = map #group subproblems 

672 
and new_formals = map #new_formals subproblems 

673 
val existentials = ListPair.map alpha_ex_unroll 

674 
(new_formals, disjuncts) 

675 
val constraints = map #1 existentials 

676 
val vexl = map #2 existentials 

677 
fun expnd tm (pats,(th,b)) = (pats,(R.SUBS[R.ASSUME(tych tm)]th,b)) 

678 
val news = map (fn (nf,rows,c) => {path = nf@rstp, 

679 
rows = map (expnd c) rows}) 

680 
(U.zip3 new_formals groups constraints) 

681 
val recursive_thms = map mk news 

15570  682 
val build_exists = Library.foldr 
10769  683 
(fn((x,t), th) => 
684 
R.CHOOSE (tych x, R.ASSUME (tych t)) th) 

685 
val thms' = ListPair.map build_exists (vexl, recursive_thms) 

686 
val same_concls = R.EVEN_ORS thms' 

687 
in R.DISJ_CASESL thm' same_concls 

688 
end 

689 
end end 

690 
in mk 

691 
end; 

692 

693 

694 
fun complete_cases thy = 

695 
let val tych = Thry.typecheck thy 

696 
val ty_info = Thry.induct_info thy 

697 
in fn pats => 

15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

698 
let val names = foldr add_term_names [] pats 
10769  699 
val T = type_of (hd pats) 
700 
val aname = Term.variant names "a" 

701 
val vname = Term.variant (aname::names) "v" 

702 
val a = Free (aname, T) 

703 
val v = Free (vname, T) 

704 
val a_eq_v = HOLogic.mk_eq(a,v) 

705 
val ex_th0 = R.EXISTS (tych (S.mk_exists{Bvar=v,Body=a_eq_v}), tych a) 

706 
(R.REFL (tych a)) 

707 
val th0 = R.ASSUME (tych a_eq_v) 

708 
val rows = map (fn x => ([x], (th0,[]))) pats 

709 
in 

710 
R.GEN (tych a) 

711 
(R.RIGHT_ASSOC 

712 
(R.CHOOSE(tych v, ex_th0) 

713 
(mk_case ty_info (vname::aname::names) 

714 
thy {path=[v], rows=rows}))) 

715 
end end; 

716 

717 

718 
(* 

719 
* Constructing induction hypotheses: one for each recursive call. 

720 
* 

721 
* Note. R will never occur as a variable in the ind_clause, because 

722 
* to do so, it would have to be from a nested definition, and we don't 

723 
* allow nested defns to have R variable. 

724 
* 

725 
* Note. When the context is empty, there can be no local variables. 

726 
**) 

727 
(* 

728 
local infix 5 ==> 

729 
fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2} 

730 
in 

731 
fun build_ih f P (pat,TCs) = 

732 
let val globals = S.free_vars_lr pat 

15570  733 
fun nested tm = isSome (S.find_term (curry (op aconv) f) tm) 
10769  734 
fun dest_TC tm = 
735 
let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm)) 

736 
val (R,y,_) = S.dest_relation R_y_pat 

737 
val P_y = if (nested tm) then R_y_pat ==> P$y else P$y 

738 
in case cntxt 

739 
of [] => (P_y, (tm,[])) 

740 
 _ => let 

741 
val imp = S.list_mk_conj cntxt ==> P_y 

742 
val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals) 

743 
val locals = #2(U.pluck (curry (op aconv) P) lvs) handle U.ERR _ => lvs 

744 
in (S.list_mk_forall(locals,imp), (tm,locals)) end 

745 
end 

746 
in case TCs 

747 
of [] => (S.list_mk_forall(globals, P$pat), []) 

748 
 _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs) 

749 
val ind_clause = S.list_mk_conj ihs ==> P$pat 

750 
in (S.list_mk_forall(globals,ind_clause), TCs_locals) 

751 
end 

752 
end 

753 
end; 

754 
*) 

755 

756 
local infix 5 ==> 

757 
fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2} 

758 
in 

759 
fun build_ih f (P,SV) (pat,TCs) = 

760 
let val pat_vars = S.free_vars_lr pat 

761 
val globals = pat_vars@SV 

15570  762 
fun nested tm = isSome (S.find_term (curry (op aconv) f) tm) 
10769  763 
fun dest_TC tm = 
764 
let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm)) 

765 
val (R,y,_) = S.dest_relation R_y_pat 

766 
val P_y = if (nested tm) then R_y_pat ==> P$y else P$y 

767 
in case cntxt 

768 
of [] => (P_y, (tm,[])) 

769 
 _ => let 

770 
val imp = S.list_mk_conj cntxt ==> P_y 

771 
val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals) 

772 
val locals = #2(U.pluck (curry (op aconv) P) lvs) handle U.ERR _ => lvs 

773 
in (S.list_mk_forall(locals,imp), (tm,locals)) end 

774 
end 

775 
in case TCs 

776 
of [] => (S.list_mk_forall(pat_vars, P$pat), []) 

777 
 _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs) 

778 
val ind_clause = S.list_mk_conj ihs ==> P$pat 

779 
in (S.list_mk_forall(pat_vars,ind_clause), TCs_locals) 

780 
end 

781 
end 

782 
end; 

783 

784 
(* 

785 
* This function makes good on the promise made in "build_ih". 

786 
* 

787 
* Input is tm = "(!y. R y pat ==> P y) ==> P pat", 

788 
* TCs = TC_1[pat] ... TC_n[pat] 

789 
* thm = ih1 /\ ... /\ ih_n  ih[pat] 

790 
**) 

791 
fun prove_case f thy (tm,TCs_locals,thm) = 

792 
let val tych = Thry.typecheck thy 

793 
val antc = tych(#ant(S.dest_imp tm)) 

794 
val thm' = R.SPEC_ALL thm 

15570  795 
fun nested tm = isSome (S.find_term (curry (op aconv) f) tm) 
10769  796 
fun get_cntxt TC = tych(#ant(S.dest_imp(#2(S.strip_forall(concl TC))))) 
797 
fun mk_ih ((TC,locals),th2,nested) = 

798 
R.GENL (map tych locals) 

799 
(if nested then R.DISCH (get_cntxt TC) th2 handle U.ERR _ => th2 

800 
else if S.is_imp (concl TC) then R.IMP_TRANS TC th2 

801 
else R.MP th2 TC) 

802 
in 

803 
R.DISCH antc 

804 
(if S.is_imp(concl thm') (* recursive calls in this clause *) 

805 
then let val th1 = R.ASSUME antc 

806 
val TCs = map #1 TCs_locals 

807 
val ylist = map (#2 o S.dest_relation o #2 o S.strip_imp o 

808 
#2 o S.strip_forall) TCs 

809 
val TClist = map (fn(TC,lvs) => (R.SPEC_ALL(R.ASSUME(tych TC)),lvs)) 

810 
TCs_locals 

811 
val th2list = map (U.C R.SPEC th1 o tych) ylist 

812 
val nlist = map nested TCs 

813 
val triples = U.zip3 TClist th2list nlist 

814 
val Pylist = map mk_ih triples 

815 
in R.MP thm' (R.LIST_CONJ Pylist) end 

816 
else thm') 

817 
end; 

818 

819 

820 
(* 

821 
* 

822 
* x = (v1,...,vn)  M[x] 

823 
*  

824 
* ?v1 ... vn. x = (v1,...,vn)  M[x] 

825 
* 

826 
**) 

827 
fun LEFT_ABS_VSTRUCT tych thm = 

828 
let fun CHOOSER v (tm,thm) = 

829 
let val ex_tm = S.mk_exists{Bvar=v,Body=tm} 

830 
in (ex_tm, R.CHOOSE(tych v, R.ASSUME (tych ex_tm)) thm) 

831 
end 

15570  832 
val [veq] = List.filter (can S.dest_eq) (#1 (R.dest_thm thm)) 
10769  833 
val {lhs,rhs} = S.dest_eq veq 
834 
val L = S.free_vars_lr rhs 

16853  835 
in #2 (fold_rev CHOOSER L (veq,thm)) end; 
10769  836 

837 

838 
(* 

839 
* Input : f, R, and [(pat1,TCs1),..., (patn,TCsn)] 

840 
* 

841 
* Instantiates WF_INDUCTION_THM, getting Sinduct and then tries to prove 

842 
* recursion induction (Rinduct) by proving the antecedent of Sinduct from 

843 
* the antecedent of Rinduct. 

844 
**) 

845 
fun mk_induction thy {fconst, R, SV, pat_TCs_list} = 

846 
let val tych = Thry.typecheck thy 

847 
val Sinduction = R.UNDISCH (R.ISPEC (tych R) Thms.WF_INDUCTION_THM) 

848 
val (pats,TCsl) = ListPair.unzip pat_TCs_list 

849 
val case_thm = complete_cases thy pats 

850 
val domain = (type_of o hd) pats 

15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

851 
val Pname = Term.variant (foldr (Library.foldr add_term_names) 
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

852 
[] (pats::TCsl)) "P" 
10769  853 
val P = Free(Pname, domain > HOLogic.boolT) 
854 
val Sinduct = R.SPEC (tych P) Sinduction 

855 
val Sinduct_assumf = S.rand ((#ant o S.dest_imp o concl) Sinduct) 

856 
val Rassums_TCl' = map (build_ih fconst (P,SV)) pat_TCs_list 

857 
val (Rassums,TCl') = ListPair.unzip Rassums_TCl' 

858 
val Rinduct_assum = R.ASSUME (tych (S.list_mk_conj Rassums)) 

18176  859 
val cases = map (fn pat => Term.betapply (Sinduct_assumf, pat)) pats 
10769  860 
val tasks = U.zip3 cases TCl' (R.CONJUNCTS Rinduct_assum) 
861 
val proved_cases = map (prove_case fconst thy) tasks 

15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset

862 
val v = Free (variant (foldr add_term_names [] (map concl proved_cases)) 
10769  863 
"v", 
864 
domain) 

865 
val vtyped = tych v 

866 
val substs = map (R.SYM o R.ASSUME o tych o (curry HOLogic.mk_eq v)) pats 

867 
val proved_cases1 = ListPair.map (fn (th,th') => R.SUBS[th]th') 

868 
(substs, proved_cases) 

869 
val abs_cases = map (LEFT_ABS_VSTRUCT tych) proved_cases1 

870 
val dant = R.GEN vtyped (R.DISJ_CASESL (R.ISPEC vtyped case_thm) abs_cases) 

871 
val dc = R.MP Sinduct dant 

872 
val Parg_ty = type_of(#Bvar(S.dest_forall(concl dc))) 

873 
val vars = map (gvvariant[Pname]) (S.strip_prod_type Parg_ty) 

16853  874 
val dc' = fold_rev (R.GEN o tych) vars 
10769  875 
(R.SPEC (tych(S.mk_vstruct Parg_ty vars)) dc) 
876 
in 

877 
R.GEN (tych P) (R.DISCH (tych(concl Rinduct_assum)) dc') 

878 
end 

879 
handle U.ERR _ => raise TFL_ERR "mk_induction" "failed derivation"; 

880 

881 

882 

883 

884 
(* 

885 
* 

886 
* POST PROCESSING 

887 
* 

888 
**) 

889 

890 

891 
fun simplify_induction thy hth ind = 

892 
let val tych = Thry.typecheck thy 

893 
val (asl,_) = R.dest_thm ind 

894 
val (_,tc_eq_tc') = R.dest_thm hth 

895 
val tc = S.lhs tc_eq_tc' 

896 
fun loop [] = ind 

897 
 loop (asm::rst) = 

898 
if (can (Thry.match_term thy asm) tc) 

899 
then R.UNDISCH 

900 
(R.MATCH_MP 

901 
(R.MATCH_MP Thms.simp_thm (R.DISCH (tych asm) ind)) 

902 
hth) 

903 
else loop rst 

904 
in loop asl 

905 
end; 

906 

907 

908 
(* 

909 
* The termination condition is an antecedent to the rule, and an 

910 
* assumption to the theorem. 

911 
**) 

912 
fun elim_tc tcthm (rule,induction) = 

913 
(R.MP rule tcthm, R.PROVE_HYP tcthm induction) 

914 

915 

14240  916 
fun trace_thms s L = 
917 
if !trace then writeln (cat_lines (s :: map string_of_thm L)) 

918 
else (); 

919 

920 
fun trace_cterms s L = 

921 
if !trace then writeln (cat_lines (s :: map string_of_cterm L)) 

922 
else ();; 

923 

924 

11632  925 
fun postprocess strict {wf_tac, terminator, simplifier} theory {rules,induction,TCs} = 
10769  926 
let val tych = Thry.typecheck theory 
11632  927 
val prove = R.prove strict; 
10769  928 

929 
(* 

930 
* Attempt to eliminate WF condition. It's the only assumption of rules 

931 
**) 

932 
val (rules1,induction1) = 

11632  933 
let val thm = prove(tych(HOLogic.mk_Trueprop 
10769  934 
(hd(#1(R.dest_thm rules)))), 
935 
wf_tac) 

936 
in (R.PROVE_HYP thm rules, R.PROVE_HYP thm induction) 

937 
end handle U.ERR _ => (rules,induction); 

938 

939 
(* 

940 
* The termination condition (tc) is simplified to  tc = tc' (there 

941 
* might not be a change!) and then 3 attempts are made: 

942 
* 

943 
* 1. if  tc = T, then eliminate it with eqT; otherwise, 

944 
* 2. apply the terminator to tc'. If  tc' = T then eliminate; else 

945 
* 3. replace tc by tc' in both the rules and the induction theorem. 

946 
**) 

947 

948 
fun simplify_tc tc (r,ind) = 

949 
let val tc1 = tych tc 

14240  950 
val _ = trace_cterms "TC before simplification: " [tc1] 
10769  951 
val tc_eq = simplifier tc1 
14240  952 
val _ = trace_thms "result: " [tc_eq] 
10769  953 
in 
954 
elim_tc (R.MATCH_MP Thms.eqT tc_eq) (r,ind) 

955 
handle U.ERR _ => 

956 
(elim_tc (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq) 

11632  957 
(prove(tych(HOLogic.mk_Trueprop(S.rhs(concl tc_eq))), 
10769  958 
terminator))) 
959 
(r,ind) 

960 
handle U.ERR _ => 

961 
(R.UNDISCH(R.MATCH_MP (R.MATCH_MP Thms.simp_thm r) tc_eq), 

962 
simplify_induction theory tc_eq ind)) 

963 
end 

964 

965 
(* 

966 
* Nested termination conditions are harder to get at, since they are 

967 
* left embedded in the body of the function (and in induction 

968 
* theorem hypotheses). Our "solution" is to simplify them, and try to 

969 
* prove termination, but leave the application of the resulting theorem 

970 
* to a higher level. So things go much as in "simplify_tc": the 

971 
* termination condition (tc) is simplified to  tc = tc' (there might 

972 
* not be a change) and then 2 attempts are made: 

973 
* 

974 
* 1. if  tc = T, then return  tc; otherwise, 

975 
* 2. apply the terminator to tc'. If  tc' = T then return  tc; else 

976 
* 3. return  tc = tc' 

977 
**) 

978 
fun simplify_nested_tc tc = 

979 
let val tc_eq = simplifier (tych (#2 (S.strip_forall tc))) 

980 
in 

981 
R.GEN_ALL 

982 
(R.MATCH_MP Thms.eqT tc_eq 

983 
handle U.ERR _ => 

984 
(R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq) 

11632  985 
(prove(tych(HOLogic.mk_Trueprop (S.rhs(concl tc_eq))), 
10769  986 
terminator)) 
987 
handle U.ERR _ => tc_eq)) 

988 
end 

989 

990 
(* 

991 
* Attempt to simplify the termination conditions in each rule and 

992 
* in the induction theorem. 

993 
**) 

994 
fun strip_imp tm = if S.is_neg tm then ([],tm) else S.strip_imp tm 

995 
fun loop ([],extras,R,ind) = (rev R, ind, extras) 

996 
 loop ((r,ftcs)::rst, nthms, R, ind) = 

997 
let val tcs = #1(strip_imp (concl r)) 

998 
val extra_tcs = gen_rems (op aconv) (ftcs, tcs) 

999 
val extra_tc_thms = map simplify_nested_tc extra_tcs 

16853  1000 
val (r1,ind1) = fold simplify_tc tcs (r,ind) 
10769  1001 
val r2 = R.FILTER_DISCH_ALL(not o S.is_WFR) r1 
1002 
in loop(rst, nthms@extra_tc_thms, r2::R, ind1) 

1003 
end 

1004 
val rules_tcs = ListPair.zip (R.CONJUNCTS rules1, TCs) 

1005 
val (rules2,ind2,extras) = loop(rules_tcs,[],[],induction1) 

1006 
in 

1007 
{induction = ind2, rules = R.LIST_CONJ rules2, nested_tcs = extras} 

1008 
end; 

1009 

1010 

1011 
end; 