author  paulson 
Tue, 20 May 1997 11:49:57 +0200  
changeset 3245  241838c01caf 
parent 3191  14bd6e5985f1 
child 3301  cdcc4d5602b6 
permissions  rwrr 
2112  1 
functor TFL(structure Rules : Rules_sig 
2 
structure Thry : Thry_sig 

3 
structure Thms : Thms_sig 

4 
sharing type Rules.binding = Thry.binding = 

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

5 
Thry.USyntax.binding = Mask.binding) : TFL_sig = 
2112  6 
struct 
7 

8 
(* Declarations *) 

9 
structure Thms = Thms; 

10 
structure Rules = Rules; 

11 
structure Thry = Thry; 

12 
structure USyntax = Thry.USyntax; 

13 

14 

15 
(* Abbreviations *) 

16 
structure R = Rules; 

17 
structure S = USyntax; 

18 
structure U = S.Utils; 

19 

20 
(* Declares 'a binding datatype *) 

21 
open Mask; 

22 

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

23 
nonfix mem > >; 
2112  24 
val > = S.>; 
25 

26 
infixr 3 >; 

27 
infixr 7 >; 

28 

29 
val concl = #2 o R.dest_thm; 

30 
val hyp = #1 o R.dest_thm; 

31 

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

32 
val list_mk_type = U.end_itlist (curry(op >)); 
2112  33 

34 
fun gtake f = 

35 
let fun grab(0,rst) = ([],rst) 

36 
 grab(n, x::rst) = 

37 
let val (taken,left) = grab(n1,rst) 

38 
in (f x::taken, left) end 

39 
in grab 

40 
end; 

41 

42 
fun enumerate L = 

43 
rev(#1(U.rev_itlist (fn x => fn (alist,i) => ((x,i)::alist, i+1)) L ([],0))); 

44 

45 
fun stringize [] = "" 

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

46 
 stringize [i] = Int.toString i 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

47 
 stringize (h::t) = (Int.toString h^", "^stringize t); 
2112  48 

49 

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

51 

52 

53 
(* 

54 
* The next function is common to patternmatch translation and 

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

56 
* 

57 
* "gvvariant" make variables that are guaranteed not to be in vlist and 

58 
* furthermore, are guaranteed not to be equal to each other. The names of 

59 
* the variables will start with "v" and end in a number. 

60 
**) 

61 
local val counter = ref 0 

62 
in 

63 
fun gvvariant vlist = 

64 
let val slist = ref (map (#Name o S.dest_var) vlist) 

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

65 
val mem = U.mem (curry (op=)) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

66 
val dummy = counter := 0 
2112  67 
fun pass str = 
68 
if (mem str (!slist)) 

69 
then ( counter := !counter + 1; 

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

70 
pass (U.concat"v" (Int.toString(!counter)))) 
2112  71 
else (slist := str :: !slist; str) 
72 
in 

73 
fn ty => S.mk_var{Name=pass "v", Ty=ty} 

74 
end 

75 
end; 

76 

77 

78 
(* 

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

80 
* partitioning up pattern rows by the leading constructor. 

81 
**) 

82 
fun ipartition gv (constructors,rows) = 

83 
let fun pfail s = raise TFL_ERR{func = "partition.part", mesg = s} 

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

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

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

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

88 
let val {Name,Ty} = S.dest_const c 

89 
val (L,_) = S.strip_type Ty 

90 
val (in_group, not_in_group) = 

91 
U.itlist (fn (row as (p::rst, rhs)) => 

92 
fn (in_group,not_in_group) => 

93 
let val (pc,args) = S.strip_comb p 

94 
in if (#Name(S.dest_const pc) = Name) 

95 
then ((args@rst, rhs)::in_group, not_in_group) 

96 
else (in_group, row::not_in_group) 

97 
end) rows ([],[]) 

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

98 
val col_types = U.take type_of (length L, #1(hd in_group)) 
2112  99 
in 
100 
part{constrs = crst, rows = not_in_group, 

101 
A = {constructor = c, 

102 
new_formals = map gv col_types, 

103 
group = in_group}::A} 

104 
end 

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

106 
end; 

107 

108 

109 

110 
(* 

111 
* This datatype carries some information about the origin of a 

112 
* clause in a function definition. 

113 
**) 

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

114 
datatype pattern = GIVEN of term * int 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

115 
 OMITTED of term * int 
2112  116 

117 
fun psubst theta (GIVEN (tm,i)) = GIVEN(S.subst theta tm, i) 

118 
 psubst theta (OMITTED (tm,i)) = OMITTED(S.subst theta tm, i); 

119 

120 
fun dest_pattern (GIVEN (tm,i)) = ((GIVEN,i),tm) 

121 
 dest_pattern (OMITTED (tm,i)) = ((OMITTED,i),tm); 

122 

123 
val pat_of = #2 o dest_pattern; 

124 
val row_of_pat = #2 o #1 o dest_pattern; 

125 

126 
(* 

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

128 
**) 

129 
fun fresh_constr ty_match colty gv c = 

130 
let val {Ty,...} = S.dest_const c 

131 
val (L,ty) = S.strip_type Ty 

132 
val ty_theta = ty_match ty colty 

133 
val c' = S.inst ty_theta c 

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

135 
in (c', gvars) 

136 
end; 

137 

138 

139 
(* 

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

141 
* pattern with constructor = Name. 

142 
**) 

143 
fun mk_group Name rows = 

144 
U.itlist (fn (row as ((prefix, p::rst), rhs)) => 

145 
fn (in_group,not_in_group) => 

146 
let val (pc,args) = S.strip_comb p 

147 
in if ((#Name(S.dest_const pc) = Name) handle _ => false) 

148 
then (((prefix,args@rst), rhs)::in_group, not_in_group) 

149 
else (in_group, row::not_in_group) end) 

150 
rows ([],[]); 

151 

152 
(* 

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

154 
**) 

155 
fun partition _ _ (_,_,_,[]) = raise TFL_ERR{func="partition", mesg="no rows"} 

156 
 partition gv ty_match 

157 
(constructors, colty, res_ty, rows as (((prefix,_),_)::_)) = 

158 
let val fresh = fresh_constr ty_match colty gv 

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

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

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

162 
val {Name,Ty} = S.dest_const c' 

163 
val (in_group, not_in_group) = mk_group Name rows 

164 
val in_group' = 

165 
if (null in_group) (* Constructor not given *) 

166 
then [((prefix, #2(fresh c)), OMITTED (S.ARB res_ty, ~1))] 

167 
else in_group 

168 
in 

169 
part{constrs = crst, 

170 
rows = not_in_group, 

171 
A = {constructor = c', 

172 
new_formals = gvars, 

173 
group = in_group'}::A} 

174 
end 

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

176 
end; 

177 

178 
(* 

179 
* Misc. routines used in mk_case 

180 
**) 

181 

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

182 
fun mk_pat (c,l) = 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

183 
let val L = length(#1(S.strip_type(type_of c))) 
2112  184 
fun build (prefix,tag,plist) = 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

185 
let val args = take (L,plist) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

186 
and plist' = drop(L,plist) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

187 
in (prefix,tag,list_comb(c,args)::plist') end 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

188 
in map build l end; 
2112  189 

190 
fun v_to_prefix (prefix, v::pats) = (v::prefix,pats) 

191 
 v_to_prefix _ = raise TFL_ERR{func="mk_case", mesg="v_to_prefix"}; 

192 

193 
fun v_to_pats (v::prefix,tag, pats) = (prefix, tag, v::pats) 

194 
 v_to_pats _ = raise TFL_ERR{func="mk_case", mesg="v_to_pats"}; 

195 

196 

197 
(* 

198 
* Translation of pattern terms into nested case expressions. 

199 
* 

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

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

202 
* incomplete set of patterns is given. 

203 
**) 

204 

205 
fun mk_case ty_info ty_match FV range_ty = 

206 
let 

207 
fun mk_case_fail s = raise TFL_ERR{func = "mk_case", mesg = s} 

208 
val fresh_var = gvvariant FV 

209 
val divide = partition fresh_var ty_match 

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

211 
 expand constructors ty (row as ((prefix, p::rst), rhs)) = 

212 
if (S.is_var p) 

213 
then let val fresh = fresh_constr ty_match ty fresh_var 

214 
fun expnd (c,gvs) = 

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

215 
let val capp = list_comb(c,gvs) 
2112  216 
in ((prefix, capp::rst), psubst[p > capp] rhs) 
217 
end 

218 
in map expnd (map fresh constructors) end 

219 
else [row] 

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

221 
 mk{path=[], rows = ((prefix, []), rhs)::_} = (* Done *) 

222 
let val (tag,tm) = dest_pattern rhs 

223 
in ([(prefix,tag,[])], tm) 

224 
end 

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

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

227 
mk{path = path, 

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

228 
rows = ((prefix, [fresh_var(type_of u)]), rhs)::rst} 
2112  229 
 mk{path = u::rstp, rows as ((_, p::_), _)::_} = 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

230 
let val (pat_rectangle,rights) = ListPair.unzip rows 
2112  231 
val col0 = map(hd o #2) pat_rectangle 
232 
in 

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

233 
if (forall S.is_var col0) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

234 
then let val rights' = map (fn(v,e) => psubst[v>u] e) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

235 
(ListPair.zip (col0, rights)) 
2112  236 
val pat_rectangle' = map v_to_prefix pat_rectangle 
237 
val (pref_patl,tm) = mk{path = rstp, 

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

238 
rows = ListPair.zip (pat_rectangle', 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

239 
rights')} 
2112  240 
in (map v_to_pats pref_patl, tm) 
241 
end 

242 
else 

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

243 
let val pty as Type (ty_name,_) = type_of p 
2112  244 
in 
245 
case (ty_info ty_name) 

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

246 
of None => mk_case_fail("Not a known datatype: "^ty_name) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

247 
 Some{case_const,constructors} => 
2112  248 
let val case_const_name = #Name(S.dest_const case_const) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

249 
val nrows = List_.concat (map (expand constructors pty) rows) 
2112  250 
val subproblems = divide(constructors, pty, range_ty, nrows) 
251 
val groups = map #group subproblems 

252 
and new_formals = map #new_formals subproblems 

253 
and constructors' = map #constructor subproblems 

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

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

255 
(ListPair.zip (new_formals, groups)) 
2112  256 
val rec_calls = map mk news 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

257 
val (pat_rect,dtrees) = ListPair.unzip rec_calls 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

258 
val case_functions = map S.list_mk_abs 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

259 
(ListPair.zip (new_formals, dtrees)) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

260 
val types = map type_of (case_functions@[u]) @ [range_ty] 
2112  261 
val case_const' = S.mk_const{Name = case_const_name, 
262 
Ty = list_mk_type types} 

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

263 
val tree = list_comb(case_const', case_functions@[u]) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

264 
val pat_rect1 = List_.concat 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

265 
(ListPair.map mk_pat (constructors', pat_rect)) 
2112  266 
in (pat_rect1,tree) 
267 
end 

268 
end end 

269 
in mk 

270 
end; 

271 

272 

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

274 
fun FV_multiset tm = 

275 
case (S.dest_term tm) 

276 
of S.VAR v => [S.mk_var v] 

277 
 S.CONST _ => [] 

278 
 S.COMB{Rator, Rand} => FV_multiset Rator @ FV_multiset Rand 

279 
 S.LAMB _ => raise TFL_ERR{func = "FV_multiset", mesg = "lambda"}; 

280 

281 
fun no_repeat_vars thy pat = 

282 
let fun check [] = true 

283 
 check (v::rst) = 

284 
if (U.mem S.aconv v rst) 

285 
then raise TFL_ERR{func = "no_repeat_vars", 

286 
mesg = U.concat(U.quote(#Name(S.dest_var v))) 

287 
(U.concat" occurs repeatedly in the pattern " 

288 
(U.quote(S.Term_to_string (Thry.typecheck thy pat))))} 

289 
else check rst 

290 
in check (FV_multiset pat) 

291 
end; 

292 

293 
local fun paired1{lhs,rhs} = (lhs,rhs) 

294 
and paired2{Rator,Rand} = (Rator,Rand) 

295 
fun mk_functional_err s = raise TFL_ERR{func = "mk_functional", mesg=s} 

3191  296 
fun single [f] = f 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

297 
 single fs = mk_functional_err (Int.toString (length fs) ^ 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

298 
" distinct function names!") 
2112  299 
in 
300 
fun mk_functional thy eqs = 

301 
let val clauses = S.strip_conj eqs 

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

302 
val (L,R) = ListPair.unzip 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

303 
(map (paired1 o S.dest_eq o #2 o S.strip_forall) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

304 
clauses) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

305 
val (funcs,pats) = ListPair.unzip(map (paired2 o S.dest_comb) L) 
3191  306 
val f = single (U.mk_set (S.aconv) funcs) 
307 
val fvar = if (S.is_var f) then f else S.mk_var(S.dest_const f) 

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

308 
val dummy = map (no_repeat_vars thy) pats 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

309 
val rows = ListPair.zip (map (fn x => ([],[x])) pats, 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

310 
map GIVEN (enumerate R)) 
2112  311 
val fvs = S.free_varsl R 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

312 
val a = S.variant fvs (S.mk_var{Name="a", Ty = type_of(hd pats)}) 
2112  313 
val FV = a::fvs 
314 
val ty_info = Thry.match_info thy 

315 
val ty_match = Thry.match_type thy 

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

316 
val range_ty = type_of (hd R) 
2112  317 
val (patts, case_tm) = mk_case ty_info ty_match FV range_ty 
318 
{path=[a], rows=rows} 

319 
val patts1 = map (fn (_,(tag,i),[pat]) => tag (pat,i)) patts handle _ 

320 
=> mk_functional_err "error in patternmatch translation" 

321 
val patts2 = U.sort(fn p1=>fn p2=> row_of_pat p1 < row_of_pat p2) patts1 

322 
val finals = map row_of_pat patts2 

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

324 
fun int_eq i1 (i2:int) = (i1=i2) 

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

325 
val dummy = case (U.set_diff int_eq originals finals) 
2112  326 
of [] => () 
327 
 L => mk_functional_err("The following rows (counting from zero)\ 

328 
\ are inaccessible: "^stringize L) 

3191  329 
val case_tm' = S.subst [f > fvar] case_tm 
330 
in {functional = S.list_mk_abs ([fvar,a], case_tm'), 

2112  331 
pats = patts2} 
332 
end end; 

333 

334 

335 
(* 

336 
* 

337 
* PRINCIPLES OF DEFINITION 

338 
* 

339 
**) 

340 

341 

3191  342 
(* 
343 
* R is already assumed to be typecopacetic with M 

2112  344 
**) 
3191  345 
local val f_eq_wfrec_R_M = 
346 
#ant(S.dest_imp(#2(S.strip_forall (concl Thms.WFREC_COROLLARY)))) 

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

348 
val fname = #Name(S.dest_var f) 

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

350 
in 

351 
fun wfrec_definition0 thy R functional = 

352 
let val {Bvar,...} = S.dest_abs functional 

353 
val {Name, Ty} = S.dest_var Bvar 

354 
val def_name = U.concat Name "_def" 

355 
val (_,ty_theta) = Thry.match_term thy f (S.mk_var{Name=fname,Ty=Ty}) 

356 
val wfrec' = S.inst ty_theta wfrec 

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

357 
val wfrec_R_M' = list_comb(wfrec',[R,functional]) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

358 
val def_term = HOLogic.mk_eq(Bvar, wfrec_R_M') 
3191  359 
in 
360 
Thry.make_definition thy def_name def_term 

361 
end 

362 
end; 

2112  363 

364 

365 

366 
(* 

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

368 
* from a datatype definition. 

369 
**) 

370 
structure Context = 

371 
struct 

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

372 
val non_datatype_context = ref []: thm list ref 
2112  373 
fun read() = !non_datatype_context 
374 
fun write L = (non_datatype_context := L) 

375 
end; 

376 

377 
fun extraction_thms thy = 

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

379 
in (case_rewrites, case_congs@Context.read()) 

380 
end; 

381 

382 

383 
(* 

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

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

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

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

388 
**) 

389 
fun merge full_pats TCs = 

390 
let fun insert (p,TCs) = 

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

392 
if (S.aconv p h) then (p,TCs)::rst else x::insrt rst 

393 
 insrt (x::rst) = x::insrt rst 

394 
 insrt[] = raise TFL_ERR{func="merge.insert",mesg="pat not found"} 

395 
in insrt end 

396 
fun pass ([],ptcl_final) = ptcl_final 

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

398 
in 

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

400 
end; 

401 

402 
fun not_omitted (GIVEN(tm,_)) = tm 

403 
 not_omitted (OMITTED _) = raise TFL_ERR{func="not_omitted",mesg=""} 

404 
val givens = U.mapfilter not_omitted; 

405 

406 

3191  407 
fun post_definition (theory, (def, pats)) = 
408 
let val tych = Thry.typecheck theory 

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

410 
val corollary = R.MATCH_MP Thms.WFREC_COROLLARY def 

2112  411 
val given_pats = givens pats 
412 
val WFR = #ant(S.dest_imp(concl corollary)) 

3191  413 
val R = #Rand(S.dest_comb WFR) 
2112  414 
val corollary' = R.UNDISCH corollary (* put WF R on assums *) 
415 
val corollaries = map (U.C R.SPEC corollary' o tych) given_pats 

3191  416 
val (case_rewrites,context_congs) = extraction_thms theory 
2112  417 
val corollaries' = map(R.simplify case_rewrites) corollaries 
418 
fun xtract th = R.CONTEXT_REWRITE_RULE(f,R) 

419 
{thms = [(R.ISPECL o map tych)[f,R] Thms.CUT_LEMMA], 

420 
congs = context_congs, 

421 
th = th} 

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

422 
val (rules, TCs) = ListPair.unzip (map xtract corollaries') 
2112  423 
val rules0 = map (R.simplify [Thms.CUT_DEF]) rules 
424 
val mk_cond_rule = R.FILTER_DISCH_ALL(not o S.aconv WFR) 

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

426 
in 

427 
{theory = theory, (* holds def, if it's needed *) 

428 
rules = rules1, 

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

429 
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (given_pats, TCs)), 
2112  430 
TCs = TCs, 
431 
patterns = pats} 

432 
end; 

433 

434 
(* 

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

436 
* extraction commute for the nonnested case. For hol90 users, this 

437 
* function can be invoked without being in draft mode. 

438 
**) 

439 
fun wfrec_eqns thy eqns = 

440 
let val {functional,pats} = mk_functional thy eqns 

441 
val given_pats = givens pats 

442 
val {Bvar = f, Body} = S.dest_abs functional 

443 
val {Bvar = x, ...} = S.dest_abs Body 

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

444 
val {Name, Ty = Type("fun", [f_dty, f_rty])} = S.dest_var f 
2112  445 
val (case_rewrites,context_congs) = extraction_thms thy 
446 
val tych = Thry.typecheck thy 

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

448 
val R = S.variant(S.free_vars eqns) 

449 
(#Bvar(S.dest_forall(concl WFREC_THM0))) 

450 
val WFREC_THM = R.ISPECL [tych R, tych f] WFREC_THM0 

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

452 
val R1 = S.rand WFR 

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

454 
val corollaries = map (U.C R.SPEC corollary' o tych) given_pats 

455 
val corollaries' = map (R.simplify case_rewrites) corollaries 

456 
fun extract th = R.CONTEXT_REWRITE_RULE(f,R1) 

457 
{thms = [(R.ISPECL o map tych)[f,R1] Thms.CUT_LEMMA], 

458 
congs = context_congs, 

459 
th = th} 

460 
in {proto_def=proto_def, 

461 
WFR=WFR, 

462 
pats=pats, 

463 
extracta = map extract corollaries'} 

464 
end; 

465 

466 

467 
(* 

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

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

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

471 
* such a relation must be wellfounded). 

472 
**) 

473 
fun lazyR_def thy eqns = 

474 
let val {proto_def,WFR,pats,extracta} = wfrec_eqns thy eqns 

475 
val R1 = S.rand WFR 

476 
val f = S.lhs proto_def 

477 
val {Name,...} = S.dest_var f 

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

478 
val (extractants,TCl) = ListPair.unzip extracta 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

479 
val TCs = foldr (gen_union (op aconv)) (TCl, []) 
2112  480 
val full_rqt = WFR::TCs 
481 
val R' = S.mk_select{Bvar=R1, Body=S.list_mk_conj full_rqt} 

482 
val R'abs = S.rand R' 

483 
val (def,theory) = Thry.make_definition thy (U.concat Name "_def") 

484 
(S.subst[R1 > R'] proto_def) 

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

486 
val tych = Thry.typecheck theory 

487 
val baz = R.DISCH (tych proto_def) 

488 
(U.itlist (R.DISCH o tych) full_rqt (R.LIST_CONJ extractants)) 

489 
val def' = R.MP (R.SPEC (tych fconst) 

490 
(R.SPEC (tych R') (R.GENL[tych R1, tych f] baz))) 

491 
def 

492 
val body_th = R.LIST_CONJ (map (R.ASSUME o tych) full_rqt) 

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

493 
val bar = R.MP (R.ISPECL[tych R'abs, tych R1] Thms.SELECT_AX) 
3191  494 
body_th 
2112  495 
in {theory = theory, R=R1, 
496 
rules = U.rev_itlist (U.C R.MP) (R.CONJUNCTS bar) def', 

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

497 
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (givens pats, TCl)), 
2112  498 
patterns = pats} 
499 
end; 

500 

501 

502 

503 
(* 

504 
* 

505 
* INDUCTION THEOREM 

506 
* 

507 
**) 

508 

509 

510 
(* Miscellaneous function  

511 
* 

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

513 
*  

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

515 
* ... 

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

517 
* 

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

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

520 
* 

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

522 
* 

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

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

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

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

527 
**) 

528 

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

529 
fun alpha_ex_unroll (xlist, tm) = 
2112  530 
let val (qvars,body) = S.strip_exists tm 
531 
val vlist = #2(S.strip_comb (S.rhs body)) 

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

532 
val plist = ListPair.zip (vlist, xlist) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

533 
val args = map (fn qv => the (gen_assoc (op aconv) (plist, qv))) qvars 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

534 
handle OPTION _ => error 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

535 
"TFL fault [alpha_ex_unroll]: no correspondence" 
2112  536 
fun build ex [] = [] 
537 
 build ex (v::rst) = 

538 
let val ex1 = S.beta_conv(S.mk_comb{Rator=S.rand ex, Rand=v}) 

539 
in ex1::build ex1 rst 

540 
end 

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

541 
val (nex::exl) = rev (tm::build tm args) 
2112  542 
in 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

543 
(nex, ListPair.zip (args, rev exl)) 
2112  544 
end; 
545 

546 

547 

548 
(* 

549 
* 

550 
* PROVING COMPLETENESS OF PATTERNS 

551 
* 

552 
**) 

553 

554 
fun mk_case ty_info FV thy = 

555 
let 

556 
val divide = ipartition (gvvariant FV) 

557 
val tych = Thry.typecheck thy 

558 
fun tych_binding(x>y) = (tych x > tych y) 

559 
fun fail s = raise TFL_ERR{func = "mk_case", mesg = s} 

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

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

562 
R.IT_EXISTS (map tych_binding bindings) thm 

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

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

564 
let val (pat_rectangle,rights) = ListPair.unzip rows 
2112  565 
val col0 = map hd pat_rectangle 
566 
val pat_rectangle' = map tl pat_rectangle 

567 
in 

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

568 
if (forall S.is_var col0) (* column 0 is all variables *) 
2112  569 
then let val rights' = map (fn ((thm,theta),v) => (thm,theta@[u>v])) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

570 
(ListPair.zip (rights, col0)) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

571 
in mk{path = rstp, rows = ListPair.zip (pat_rectangle', rights')} 
2112  572 
end 
573 
else (* column 0 is all constructors *) 

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

574 
let val Type (ty_name,_) = type_of p 
2112  575 
in 
576 
case (ty_info ty_name) 

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

577 
of None => fail("Not a known datatype: "^ty_name) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

578 
 Some{constructors,nchotomy} => 
2112  579 
let val thm' = R.ISPEC (tych u) nchotomy 
580 
val disjuncts = S.strip_disj (concl thm') 

581 
val subproblems = divide(constructors, rows) 

582 
val groups = map #group subproblems 

583 
and new_formals = map #new_formals subproblems 

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

584 
val existentials = ListPair.map alpha_ex_unroll 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

585 
(new_formals, disjuncts) 
2112  586 
val constraints = map #1 existentials 
587 
val vexl = map #2 existentials 

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

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

590 
rows = map (expnd c) rows}) 

591 
(U.zip3 new_formals groups constraints) 

592 
val recursive_thms = map mk news 

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

593 
val build_exists = foldr 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

594 
(fn((x,t), th) => 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

595 
R.CHOOSE (tych x, R.ASSUME (tych t)) th) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

596 
val thms' = ListPair.map build_exists (vexl, recursive_thms) 
2112  597 
val same_concls = R.EVEN_ORS thms' 
598 
in R.DISJ_CASESL thm' same_concls 

599 
end 

600 
end end 

601 
in mk 

602 
end; 

603 

604 

605 
fun complete_cases thy = 

606 
let val tych = Thry.typecheck thy 

607 
fun pmk_var n ty = S.mk_var{Name = n,Ty = ty} 

608 
val ty_info = Thry.induct_info thy 

609 
in fn pats => 

610 
let val FV0 = S.free_varsl pats 

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

611 
val a = S.variant FV0 (pmk_var "a" (type_of(hd pats))) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

612 
val v = S.variant (a::FV0) (pmk_var "v" (type_of a)) 
2112  613 
val FV = a::v::FV0 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

614 
val a_eq_v = HOLogic.mk_eq(a,v) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

615 
val ex_th0 = R.EXISTS (tych (S.mk_exists{Bvar=v,Body=a_eq_v}), tych a) 
2112  616 
(R.REFL (tych a)) 
617 
val th0 = R.ASSUME (tych a_eq_v) 

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

619 
in 

620 
R.GEN (tych a) 

621 
(R.RIGHT_ASSOC 

622 
(R.CHOOSE(tych v, ex_th0) 

623 
(mk_case ty_info FV thy {path=[v], rows=rows}))) 

624 
end end; 

625 

626 

627 
(* 

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

629 
* 

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

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

632 
* allow nested defns to have R variable. 

633 
* 

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

635 
**) 

636 

637 
local nonfix ^ ; infix 9 ^ ; infix 5 ==> 

638 
fun (tm1 ^ tm2) = S.mk_comb{Rator = tm1, Rand = tm2} 

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

640 
in 

641 
fun build_ih f P (pat,TCs) = 

642 
let val globals = S.free_vars_lr pat 

643 
fun nested tm = U.can(S.find_term (S.aconv f)) tm handle _ => false 

644 
fun dest_TC tm = 

645 
let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm)) 

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

647 
val P_y = if (nested tm) then R_y_pat ==> P^y else P^y 

648 
in case cntxt 

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

650 
 _ => let 

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

652 
val lvs = U.set_diff S.aconv (S.free_vars_lr imp) globals 

653 
val locals = #2(U.pluck (S.aconv P) lvs) handle _ => lvs 

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

655 
end 

656 
in case TCs 

657 
of [] => (S.list_mk_forall(globals, P^pat), []) 

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

658 
 _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs) 
2112  659 
val ind_clause = S.list_mk_conj ihs ==> P^pat 
660 
in (S.list_mk_forall(globals,ind_clause), TCs_locals) 

661 
end 

662 
end 

663 
end; 

664 

665 

666 

667 
(* 

668 
* This function makes good on the promise made in "build_ih: we prove 

669 
* <something>. 

670 
* 

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

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

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

674 
**) 

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

676 
let val tych = Thry.typecheck thy 

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

678 
val thm' = R.SPEC_ALL thm 

679 
fun nested tm = U.can(S.find_term (S.aconv f)) tm handle _ => false 

680 
fun get_cntxt TC = tych(#ant(S.dest_imp(#2(S.strip_forall(concl TC))))) 

681 
fun mk_ih ((TC,locals),th2,nested) = 

682 
R.GENL (map tych locals) 

683 
(if nested 

684 
then R.DISCH (get_cntxt TC) th2 handle _ => th2 

685 
else if S.is_imp(concl TC) 

686 
then R.IMP_TRANS TC th2 

687 
else R.MP th2 TC) 

688 
in 

689 
R.DISCH antc 

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

691 
then let val th1 = R.ASSUME antc 

692 
val TCs = map #1 TCs_locals 

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

694 
#2 o S.strip_forall) TCs 

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

696 
TCs_locals 

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

698 
val nlist = map nested TCs 

699 
val triples = U.zip3 TClist th2list nlist 

700 
val Pylist = map mk_ih triples 

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

702 
else thm') 

703 
end; 

704 

705 

706 
(* 

707 
* 

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

709 
*  

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

711 
* 

712 
**) 

713 
fun LEFT_ABS_VSTRUCT tych thm = 

714 
let fun CHOOSER v (tm,thm) = 

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

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

717 
end 

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

718 
val [veq] = filter (U.can S.dest_eq) (#1 (R.dest_thm thm)) 
2112  719 
val {lhs,rhs} = S.dest_eq veq 
720 
val L = S.free_vars_lr rhs 

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

721 
in #2 (U.itlist CHOOSER L (veq,thm)) end; 
2112  722 

723 

724 
fun combize M N = S.mk_comb{Rator=M,Rand=N}; 

725 

726 

727 
(* 

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

729 
* 

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

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

732 
* the antecedent of Rinduct. 

733 
**) 

734 
fun mk_induction thy f R pat_TCs_list = 

735 
let val tych = Thry.typecheck thy 

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

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

737 
val (pats,TCsl) = ListPair.unzip pat_TCs_list 
2112  738 
val case_thm = complete_cases thy pats 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

739 
val domain = (type_of o hd) pats 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

740 
val P = S.variant (S.all_varsl (pats @ List_.concat TCsl)) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

741 
(S.mk_var{Name="P", Ty=domain > HOLogic.boolT}) 
2112  742 
val Sinduct = R.SPEC (tych P) Sinduction 
743 
val Sinduct_assumf = S.rand ((#ant o S.dest_imp o concl) Sinduct) 

744 
val Rassums_TCl' = map (build_ih f P) pat_TCs_list 

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

745 
val (Rassums,TCl') = ListPair.unzip Rassums_TCl' 
2112  746 
val Rinduct_assum = R.ASSUME (tych (S.list_mk_conj Rassums)) 
747 
val cases = map (S.beta_conv o combize Sinduct_assumf) pats 

748 
val tasks = U.zip3 cases TCl' (R.CONJUNCTS Rinduct_assum) 

749 
val proved_cases = map (prove_case f thy) tasks 

750 
val v = S.variant (S.free_varsl (map concl proved_cases)) 

751 
(S.mk_var{Name="v", Ty=domain}) 

752 
val vtyped = tych v 

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

753 
val substs = map (R.SYM o R.ASSUME o tych o (curry HOLogic.mk_eq v)) pats 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

754 
val proved_cases1 = ListPair.map (fn (th,th') => R.SUBS[th]th') 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

755 
(substs, proved_cases) 
2112  756 
val abs_cases = map (LEFT_ABS_VSTRUCT tych) proved_cases1 
757 
val dant = R.GEN vtyped (R.DISJ_CASESL (R.ISPEC vtyped case_thm) abs_cases) 

758 
val dc = R.MP Sinduct dant 

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

759 
val Parg_ty = type_of(#Bvar(S.dest_forall(concl dc))) 
2112  760 
val vars = map (gvvariant[P]) (S.strip_prod_type Parg_ty) 
761 
val dc' = U.itlist (R.GEN o tych) vars 

762 
(R.SPEC (tych(S.mk_vstruct Parg_ty vars)) dc) 

763 
in 

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

765 
end 

766 
handle _ => raise TFL_ERR{func = "mk_induction", mesg = "failed derivation"}; 

767 

768 

769 

770 
(* 

771 
* 

772 
* POST PROCESSING 

773 
* 

774 
**) 

775 

776 

777 
fun simplify_induction thy hth ind = 

778 
let val tych = Thry.typecheck thy 

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

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

781 
val tc = S.lhs tc_eq_tc' 

782 
fun loop [] = ind 

783 
 loop (asm::rst) = 

784 
if (U.can (Thry.match_term thy asm) tc) 

785 
then R.UNDISCH 

786 
(R.MATCH_MP 

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

788 
hth) 

789 
else loop rst 

790 
in loop asl 

791 
end; 

792 

793 

794 
(* 

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

796 
* assumption to the theorem. 

797 
**) 

798 
fun elim_tc tcthm (rule,induction) = 

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

800 

801 

802 
fun postprocess{WFtac, terminator, simplifier} theory {rules,induction,TCs} = 

803 
let val tych = Thry.typecheck theory 

804 

805 
(* 

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

807 
**) 

808 
val (rules1,induction1) = 

809 
let val thm = R.prove(tych(hd(#1(R.dest_thm rules))),WFtac) 

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

811 
end handle _ => (rules,induction) 

812 

813 
(* 

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

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

816 
* 

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

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

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

820 
**) 

821 
fun simplify_tc tc (r,ind) = 

822 
let val tc_eq = simplifier (tych tc) 

823 
in 

824 
elim_tc (R.MATCH_MP Thms.eqT tc_eq) (r,ind) 

825 
handle _ => 

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

827 
(R.prove(tych(S.rhs(concl tc_eq)),terminator))) 

828 
(r,ind) 

829 
handle _ => 

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

831 
simplify_induction theory tc_eq ind)) 

832 
end 

833 

834 
(* 

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

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

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

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

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

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

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

842 
* 

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

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

845 
* 3. return  tc = tc' 

846 
**) 

847 
fun simplify_nested_tc tc = 

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

849 
in 

850 
R.GEN_ALL 

851 
(R.MATCH_MP Thms.eqT tc_eq 

852 
handle _ 

853 
=> (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq) 

854 
(R.prove(tych(S.rhs(concl tc_eq)),terminator)) 

855 
handle _ => tc_eq)) 

856 
end 

857 

858 
(* 

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

860 
* in the induction theorem. 

861 
**) 

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

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

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

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

866 
val extra_tcs = U.set_diff S.aconv ftcs tcs 

867 
val extra_tc_thms = map simplify_nested_tc extra_tcs 

868 
val (r1,ind1) = U.rev_itlist simplify_tc tcs (r,ind) 

869 
val r2 = R.FILTER_DISCH_ALL(not o S.is_WFR) r1 

870 
in loop(rst, nthms@extra_tc_thms, r2::R, ind1) 

871 
end 

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

872 
val rules_tcs = ListPair.zip (R.CONJUNCTS rules1, TCs) 
2112  873 
val (rules2,ind2,extras) = loop(rules_tcs,[],[],induction1) 
874 
in 

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

876 
end; 

877 

878 
end; (* TFL *) 