author  paulson 
Thu, 22 May 1997 15:11:56 +0200  
changeset 3301  cdcc4d5602b6 
parent 3245  241838c01caf 
child 3333  0bbf06e86c06 
permissions  rwrr 
3301
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

1 
(* Title: TFL/tfl 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

2 
ID: $Id$ 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

3 
Author: Konrad Slind, Cambridge University Computer Laboratory 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

4 
Copyright 1997 University of Cambridge 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

5 

cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

6 
Main TFL functor 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

7 
*) 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

8 

2112  9 
functor TFL(structure Rules : Rules_sig 
10 
structure Thry : Thry_sig 

11 
structure Thms : Thms_sig 

12 
sharing type Rules.binding = Thry.binding = 

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

13 
Thry.USyntax.binding = Mask.binding) : TFL_sig = 
2112  14 
struct 
15 

16 
(* Declarations *) 

17 
structure Thms = Thms; 

18 
structure Rules = Rules; 

19 
structure Thry = Thry; 

20 
structure USyntax = Thry.USyntax; 

21 

22 

23 
(* Abbreviations *) 

24 
structure R = Rules; 

25 
structure S = USyntax; 

26 
structure U = S.Utils; 

27 

28 
(* Declares 'a binding datatype *) 

29 
open Mask; 

30 

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

31 
nonfix mem > >; 
2112  32 
val > = S.>; 
33 

34 
infixr 3 >; 

35 
infixr 7 >; 

36 

37 
val concl = #2 o R.dest_thm; 

38 
val hyp = #1 o R.dest_thm; 

39 

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

40 
val list_mk_type = U.end_itlist (curry(op >)); 
2112  41 

42 
fun gtake f = 

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

44 
 grab(n, x::rst) = 

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

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

47 
in grab 

48 
end; 

49 

50 
fun enumerate L = 

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

52 

53 
fun stringize [] = "" 

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

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

55 
 stringize (h::t) = (Int.toString h^", "^stringize t); 
2112  56 

57 

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

59 

60 

61 
(* 

62 
* The next function is common to patternmatch translation and 

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

64 
* 

3301
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

65 
* The curried function "gvvariant" returns a function to generate distinct 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

66 
* variables that are guaranteed not to be in vlist. The names of 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

67 
* the variables go u, v, ..., z, aa, ..., az, ... The returned 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

68 
* function contains embedded refs! 
2112  69 
**) 
70 
fun gvvariant vlist = 

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

3301
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

72 
val vname = ref "u" 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

73 
fun new() = 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

74 
if !vname mem_string (!slist) 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

75 
then (vname := bump_string (!vname); new()) 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

76 
else (slist := !vname :: !slist; !vname) 
2112  77 
in 
3301
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

78 
fn ty => Free(new(), ty) 
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

79 
end; 
2112  80 

81 

82 
(* 

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

84 
* partitioning up pattern rows by the leading constructor. 

85 
**) 

86 
fun ipartition gv (constructors,rows) = 

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

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

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

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

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

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

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

94 
val (in_group, not_in_group) = 

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

96 
fn (in_group,not_in_group) => 

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

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

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

100 
else (in_group, row::not_in_group) 

101 
end) rows ([],[]) 

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

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

105 
A = {constructor = c, 

106 
new_formals = map gv col_types, 

107 
group = in_group}::A} 

108 
end 

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

110 
end; 

111 

112 

113 

114 
(* 

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

116 
* clause in a function definition. 

117 
**) 

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

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

119 
 OMITTED of term * int 
2112  120 

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

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

123 

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

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

126 

127 
val pat_of = #2 o dest_pattern; 

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

129 

130 
(* 

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

132 
**) 

133 
fun fresh_constr ty_match colty gv c = 

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

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

136 
val ty_theta = ty_match ty colty 

137 
val c' = S.inst ty_theta c 

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

139 
in (c', gvars) 

140 
end; 

141 

142 

143 
(* 

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

145 
* pattern with constructor = Name. 

146 
**) 

147 
fun mk_group Name rows = 

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

149 
fn (in_group,not_in_group) => 

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

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

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

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

154 
rows ([],[]); 

155 

156 
(* 

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

158 
**) 

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

160 
 partition gv ty_match 

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

162 
let val fresh = fresh_constr ty_match colty gv 

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

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

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

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

167 
val (in_group, not_in_group) = mk_group Name rows 

168 
val in_group' = 

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

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

171 
else in_group 

172 
in 

173 
part{constrs = crst, 

174 
rows = not_in_group, 

175 
A = {constructor = c', 

176 
new_formals = gvars, 

177 
group = in_group'}::A} 

178 
end 

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

180 
end; 

181 

182 
(* 

183 
* Misc. routines used in mk_case 

184 
**) 

185 

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

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

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

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

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

191 
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

192 
in map build l end; 
2112  193 

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

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

196 

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

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

199 

200 

201 
(* 

202 
* Translation of pattern terms into nested case expressions. 

203 
* 

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

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

206 
* incomplete set of patterns is given. 

207 
**) 

208 

209 
fun mk_case ty_info ty_match FV range_ty = 

210 
let 

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

212 
val fresh_var = gvvariant FV 

213 
val divide = partition fresh_var ty_match 

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

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

216 
if (S.is_var p) 

217 
then let val fresh = fresh_constr ty_match ty fresh_var 

218 
fun expnd (c,gvs) = 

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

219 
let val capp = list_comb(c,gvs) 
2112  220 
in ((prefix, capp::rst), psubst[p > capp] rhs) 
221 
end 

222 
in map expnd (map fresh constructors) end 

223 
else [row] 

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

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

226 
let val (tag,tm) = dest_pattern rhs 

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

228 
end 

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

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

231 
mk{path = path, 

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

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

234 
let val (pat_rectangle,rights) = ListPair.unzip rows 
2112  235 
val col0 = map(hd o #2) pat_rectangle 
236 
in 

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

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

238 
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

239 
(ListPair.zip (col0, rights)) 
2112  240 
val pat_rectangle' = map v_to_prefix pat_rectangle 
241 
val (pref_patl,tm) = mk{path = rstp, 

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

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

243 
rights')} 
2112  244 
in (map v_to_pats pref_patl, tm) 
245 
end 

246 
else 

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

247 
let val pty as Type (ty_name,_) = type_of p 
2112  248 
in 
249 
case (ty_info ty_name) 

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

250 
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

251 
 Some{case_const,constructors} => 
2112  252 
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

253 
val nrows = List_.concat (map (expand constructors pty) rows) 
2112  254 
val subproblems = divide(constructors, pty, range_ty, nrows) 
255 
val groups = map #group subproblems 

256 
and new_formals = map #new_formals subproblems 

257 
and constructors' = map #constructor subproblems 

258 
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

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

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

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

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

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

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

267 
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

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

269 
(ListPair.map mk_pat (constructors', pat_rect)) 
2112  270 
in (pat_rect1,tree) 
271 
end 

272 
end end 

273 
in mk 

274 
end; 

275 

276 

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

278 
fun FV_multiset tm = 

279 
case (S.dest_term tm) 

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

281 
 S.CONST _ => [] 

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

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

284 

285 
fun no_repeat_vars thy pat = 

286 
let fun check [] = true 

287 
 check (v::rst) = 

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

289 
then raise TFL_ERR{func = "no_repeat_vars", 

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

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

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

293 
else check rst 

294 
in check (FV_multiset pat) 

295 
end; 

296 

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

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

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

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

301 
 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

302 
" distinct function names!") 
2112  303 
in 
304 
fun mk_functional thy eqs = 

305 
let val clauses = S.strip_conj eqs 

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

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

307 
(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

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

309 
val (funcs,pats) = ListPair.unzip(map (paired2 o S.dest_comb) L) 
3191  310 
val f = single (U.mk_set (S.aconv) funcs) 
311 
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

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

313 
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

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

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

319 
val ty_match = Thry.match_type thy 

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

320 
val range_ty = type_of (hd R) 
2112  321 
val (patts, case_tm) = mk_case ty_info ty_match FV range_ty 
322 
{path=[a], rows=rows} 

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

324 
=> mk_functional_err "error in patternmatch translation" 

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

326 
val finals = map row_of_pat patts2 

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

328 
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

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

332 
\ are inaccessible: "^stringize L) 

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

2112  335 
pats = patts2} 
336 
end end; 

337 

338 

339 
(* 

340 
* 

341 
* PRINCIPLES OF DEFINITION 

342 
* 

343 
**) 

344 

345 

3191  346 
(* 
347 
* R is already assumed to be typecopacetic with M 

2112  348 
**) 
3191  349 
local val f_eq_wfrec_R_M = 
350 
#ant(S.dest_imp(#2(S.strip_forall (concl Thms.WFREC_COROLLARY)))) 

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

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

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

354 
in 

355 
fun wfrec_definition0 thy R functional = 

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

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

358 
val def_name = U.concat Name "_def" 

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

360 
val wfrec' = S.inst ty_theta wfrec 

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

361 
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

362 
val def_term = HOLogic.mk_eq(Bvar, wfrec_R_M') 
3191  363 
in 
364 
Thry.make_definition thy def_name def_term 

365 
end 

366 
end; 

2112  367 

368 

369 

370 
(* 

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

372 
* from a datatype definition. 

373 
**) 

374 
structure Context = 

375 
struct 

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

376 
val non_datatype_context = ref []: thm list ref 
2112  377 
fun read() = !non_datatype_context 
378 
fun write L = (non_datatype_context := L) 

379 
end; 

380 

381 
fun extraction_thms thy = 

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

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

384 
end; 

385 

386 

387 
(* 

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

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

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

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

392 
**) 

393 
fun merge full_pats TCs = 

394 
let fun insert (p,TCs) = 

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

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

397 
 insrt (x::rst) = x::insrt rst 

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

399 
in insrt end 

400 
fun pass ([],ptcl_final) = ptcl_final 

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

402 
in 

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

404 
end; 

405 

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

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

408 
val givens = U.mapfilter not_omitted; 

409 

410 

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

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

414 
val corollary = R.MATCH_MP Thms.WFREC_COROLLARY def 

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

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

3191  420 
val (case_rewrites,context_congs) = extraction_thms theory 
2112  421 
val corollaries' = map(R.simplify case_rewrites) corollaries 
422 
fun xtract th = R.CONTEXT_REWRITE_RULE(f,R) 

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

424 
congs = context_congs, 

425 
th = th} 

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

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

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

430 
in 

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

432 
rules = rules1, 

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

433 
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (given_pats, TCs)), 
2112  434 
TCs = TCs, 
435 
patterns = pats} 

436 
end; 

437 

438 
(* 

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

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

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

442 
**) 

443 
fun wfrec_eqns thy eqns = 

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

445 
val given_pats = givens pats 

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

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

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

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

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

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

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

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

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

456 
val R1 = S.rand WFR 

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

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

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

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

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

462 
congs = context_congs, 

463 
th = th} 

464 
in {proto_def=proto_def, 

465 
WFR=WFR, 

466 
pats=pats, 

467 
extracta = map extract corollaries'} 

468 
end; 

469 

470 

471 
(* 

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

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

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

475 
* such a relation must be wellfounded). 

476 
**) 

477 
fun lazyR_def thy eqns = 

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

479 
val R1 = S.rand WFR 

480 
val f = S.lhs proto_def 

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

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

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

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

486 
val R'abs = S.rand R' 

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

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

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

490 
val tych = Thry.typecheck theory 

491 
val baz = R.DISCH (tych proto_def) 

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

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

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

495 
def 

496 
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

497 
val bar = R.MP (R.ISPECL[tych R'abs, tych R1] Thms.SELECT_AX) 
3191  498 
body_th 
2112  499 
in {theory = theory, R=R1, 
500 
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

501 
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (givens pats, TCl)), 
2112  502 
patterns = pats} 
503 
end; 

504 

505 

506 

507 
(* 

508 
* 

509 
* INDUCTION THEOREM 

510 
* 

511 
**) 

512 

513 

514 
(* Miscellaneous function  

515 
* 

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

517 
*  

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

519 
* ... 

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

521 
* 

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

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

524 
* 

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

526 
* 

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

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

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

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

531 
**) 

532 

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

533 
fun alpha_ex_unroll (xlist, tm) = 
2112  534 
let val (qvars,body) = S.strip_exists tm 
535 
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

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

537 
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

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

539 
"TFL fault [alpha_ex_unroll]: no correspondence" 
2112  540 
fun build ex [] = [] 
541 
 build ex (v::rst) = 

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

543 
in ex1::build ex1 rst 

544 
end 

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

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

547 
(nex, ListPair.zip (args, rev exl)) 
2112  548 
end; 
549 

550 

551 

552 
(* 

553 
* 

554 
* PROVING COMPLETENESS OF PATTERNS 

555 
* 

556 
**) 

557 

558 
fun mk_case ty_info FV thy = 

559 
let 

560 
val divide = ipartition (gvvariant FV) 

561 
val tych = Thry.typecheck thy 

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

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

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

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

566 
R.IT_EXISTS (map tych_binding bindings) thm 

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

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

568 
let val (pat_rectangle,rights) = ListPair.unzip rows 
2112  569 
val col0 = map hd pat_rectangle 
570 
val pat_rectangle' = map tl pat_rectangle 

571 
in 

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

572 
if (forall S.is_var col0) (* column 0 is all variables *) 
2112  573 
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

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

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

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

578 
let val Type (ty_name,_) = type_of p 
2112  579 
in 
580 
case (ty_info ty_name) 

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

581 
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

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

585 
val subproblems = divide(constructors, rows) 

586 
val groups = map #group subproblems 

587 
and new_formals = map #new_formals subproblems 

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

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

589 
(new_formals, disjuncts) 
2112  590 
val constraints = map #1 existentials 
591 
val vexl = map #2 existentials 

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

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

594 
rows = map (expnd c) rows}) 

595 
(U.zip3 new_formals groups constraints) 

596 
val recursive_thms = map mk news 

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

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

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

599 
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

600 
val thms' = ListPair.map build_exists (vexl, recursive_thms) 
2112  601 
val same_concls = R.EVEN_ORS thms' 
602 
in R.DISJ_CASESL thm' same_concls 

603 
end 

604 
end end 

605 
in mk 

606 
end; 

607 

608 

609 
fun complete_cases thy = 

610 
let val tych = Thry.typecheck thy 

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

612 
val ty_info = Thry.induct_info thy 

613 
in fn pats => 

614 
let val FV0 = S.free_varsl pats 

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

615 
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

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

618 
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

619 
val ex_th0 = R.EXISTS (tych (S.mk_exists{Bvar=v,Body=a_eq_v}), tych a) 
2112  620 
(R.REFL (tych a)) 
621 
val th0 = R.ASSUME (tych a_eq_v) 

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

623 
in 

624 
R.GEN (tych a) 

625 
(R.RIGHT_ASSOC 

626 
(R.CHOOSE(tych v, ex_th0) 

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

628 
end end; 

629 

630 

631 
(* 

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

633 
* 

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

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

636 
* allow nested defns to have R variable. 

637 
* 

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

639 
**) 

640 

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

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

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

644 
in 

645 
fun build_ih f P (pat,TCs) = 

646 
let val globals = S.free_vars_lr pat 

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

648 
fun dest_TC tm = 

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

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

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

652 
in case cntxt 

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

654 
 _ => let 

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

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

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

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

659 
end 

660 
in case TCs 

661 
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

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

665 
end 

666 
end 

667 
end; 

668 

669 

670 

671 
(* 

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

673 
* <something>. 

674 
* 

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

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

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

678 
**) 

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

680 
let val tych = Thry.typecheck thy 

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

682 
val thm' = R.SPEC_ALL thm 

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

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

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

686 
R.GENL (map tych locals) 

687 
(if nested 

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

689 
else if S.is_imp(concl TC) 

690 
then R.IMP_TRANS TC th2 

691 
else R.MP th2 TC) 

692 
in 

693 
R.DISCH antc 

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

695 
then let val th1 = R.ASSUME antc 

696 
val TCs = map #1 TCs_locals 

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

698 
#2 o S.strip_forall) TCs 

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

700 
TCs_locals 

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

702 
val nlist = map nested TCs 

703 
val triples = U.zip3 TClist th2list nlist 

704 
val Pylist = map mk_ih triples 

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

706 
else thm') 

707 
end; 

708 

709 

710 
(* 

711 
* 

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

713 
*  

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

715 
* 

716 
**) 

717 
fun LEFT_ABS_VSTRUCT tych thm = 

718 
let fun CHOOSER v (tm,thm) = 

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

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

721 
end 

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

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

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

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

727 

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

729 

730 

731 
(* 

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

733 
* 

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

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

736 
* the antecedent of Rinduct. 

737 
**) 

738 
fun mk_induction thy f R pat_TCs_list = 

739 
let val tych = Thry.typecheck thy 

740 
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

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

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

744 
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

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

748 
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

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

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

753 
val proved_cases = map (prove_case f thy) tasks 

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

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

756 
val vtyped = tych v 

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

757 
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

758 
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

759 
(substs, proved_cases) 
2112  760 
val abs_cases = map (LEFT_ABS_VSTRUCT tych) proved_cases1 
761 
val dant = R.GEN vtyped (R.DISJ_CASESL (R.ISPEC vtyped case_thm) abs_cases) 

762 
val dc = R.MP Sinduct dant 

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

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

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

767 
in 

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

769 
end 

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

771 

772 

773 

774 
(* 

775 
* 

776 
* POST PROCESSING 

777 
* 

778 
**) 

779 

780 

781 
fun simplify_induction thy hth ind = 

782 
let val tych = Thry.typecheck thy 

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

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

785 
val tc = S.lhs tc_eq_tc' 

786 
fun loop [] = ind 

787 
 loop (asm::rst) = 

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

789 
then R.UNDISCH 

790 
(R.MATCH_MP 

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

792 
hth) 

793 
else loop rst 

794 
in loop asl 

795 
end; 

796 

797 

798 
(* 

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

800 
* assumption to the theorem. 

801 
**) 

802 
fun elim_tc tcthm (rule,induction) = 

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

804 

805 

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

807 
let val tych = Thry.typecheck theory 

808 

809 
(* 

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

811 
**) 

812 
val (rules1,induction1) = 

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

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

815 
end handle _ => (rules,induction) 

816 

817 
(* 

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

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

820 
* 

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

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

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

824 
**) 

825 
fun simplify_tc tc (r,ind) = 

826 
let val tc_eq = simplifier (tych tc) 

827 
in 

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

829 
handle _ => 

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

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

832 
(r,ind) 

833 
handle _ => 

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

835 
simplify_induction theory tc_eq ind)) 

836 
end 

837 

838 
(* 

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

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

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

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

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

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

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

846 
* 

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

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

849 
* 3. return  tc = tc' 

850 
**) 

851 
fun simplify_nested_tc tc = 

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

853 
in 

854 
R.GEN_ALL 

855 
(R.MATCH_MP Thms.eqT tc_eq 

856 
handle _ 

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

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

859 
handle _ => tc_eq)) 

860 
end 

861 

862 
(* 

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

864 
* in the induction theorem. 

865 
**) 

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

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

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

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

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

871 
val extra_tc_thms = map simplify_nested_tc extra_tcs 

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

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

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

875 
end 

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

876 
val rules_tcs = ListPair.zip (R.CONJUNCTS rules1, TCs) 
2112  877 
val (rules2,ind2,extras) = loop(rules_tcs,[],[],induction1) 
878 
in 

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

880 
end; 

881 

882 
end; (* TFL *) 