author  paulson 
Wed, 21 Jul 1999 15:16:32 +0200  
changeset 7052  4c201f27c74e 
parent 6566  7ed743d18af7 
child 7262  a05dc63ca29b 
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 

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

6 
Main module 
3301
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 

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

9 
structure Prim : TFL_sig = 
2112  10 
struct 
11 

6498  12 
val trace = ref false; 
13 

2112  14 
(* Abbreviations *) 
15 
structure R = Rules; 

16 
structure S = USyntax; 

17 
structure U = S.Utils; 

18 

19 
val concl = #2 o R.dest_thm; 

20 
val hyp = #1 o R.dest_thm; 

21 

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

22 
val list_mk_type = U.end_itlist (curry(op >)); 
2112  23 

3712
242546f35f8e
Deleted the unused gtake and recoded enumerate to use foldl
paulson
parents:
3459
diff
changeset

24 
fun enumerate l = 
242546f35f8e
Deleted the unused gtake and recoded enumerate to use foldl
paulson
parents:
3459
diff
changeset

25 
rev(#1(foldl (fn ((alist,i), x) => ((x,i)::alist, i+1)) (([],0), l))); 
2112  26 

27 
fun stringize [] = "" 

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

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

29 
 stringize (h::t) = (Int.toString h^", "^stringize t); 
2112  30 

31 

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

33 

34 

35 
(* 

6498  36 
handling of usersupplied congruence rules: lcp*) 
37 

38 
(*Convert conclusion from = to ==*) 

39 
val eq_reflect_list = map (fn th => (th RS eq_reflection) handle _ => th); 

40 

41 
(*default congruence rules include those for LET and IF*) 

42 
val default_congs = eq_reflect_list [Thms.LET_CONG, if_cong]; 

43 

44 
fun congs ths = default_congs @ eq_reflect_list ths; 

45 

46 
val default_simps = 

47 
[less_Suc_eq RS iffD2, lex_prod_def, measure_def, inv_image_def]; 

48 

49 

50 

51 

52 
(* 

2112  53 
* The next function is common to patternmatch translation and 
54 
* proof of completeness of cases for the induction theorem. 

55 
* 

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

56 
* The curried function "gvvariant" returns a function to generate distinct 
3405  57 
* variables that are guaranteed not to be in names. The names of 
3301
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

58 
* 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

59 
* function contains embedded refs! 
2112  60 
**) 
3405  61 
fun gvvariant names = 
62 
let val slist = ref names 

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

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

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

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

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

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

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

70 
end; 
2112  71 

72 

73 
(* 

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

75 
* partitioning up pattern rows by the leading constructor. 

76 
**) 

77 
fun ipartition gv (constructors,rows) = 

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

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

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

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

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

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

83 
let val (Name,Ty) = dest_Const c 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

84 
val L = binder_types Ty 
2112  85 
val (in_group, not_in_group) = 
86 
U.itlist (fn (row as (p::rst, rhs)) => 

87 
fn (in_group,not_in_group) => 

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

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

89 
in if (#1(dest_Const pc) = Name) 
2112  90 
then ((args@rst, rhs)::in_group, not_in_group) 
91 
else (in_group, row::not_in_group) 

92 
end) rows ([],[]) 

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

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

96 
A = {constructor = c, 

97 
new_formals = map gv col_types, 

98 
group = in_group}::A} 

99 
end 

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

101 
end; 

102 

103 

104 

105 
(* 

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

107 
* clause in a function definition. 

108 
**) 

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

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

110 
 OMITTED of term * int 
2112  111 

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

112 
fun pattern_map f (GIVEN (tm,i)) = GIVEN(f tm, i) 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

113 
 pattern_map f (OMITTED (tm,i)) = OMITTED(f tm, i); 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

114 

5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

115 
fun pattern_subst theta = pattern_map (subst_free theta); 
2112  116 

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

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

119 

120 
val pat_of = #2 o dest_pattern; 

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

122 

123 
(* 

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

125 
**) 

126 
fun fresh_constr ty_match colty gv c = 

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

127 
let val (_,Ty) = dest_Const c 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

128 
val L = binder_types Ty 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

129 
and ty = body_type Ty 
2112  130 
val ty_theta = ty_match ty colty 
131 
val c' = S.inst ty_theta c 

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

133 
in (c', gvars) 

134 
end; 

135 

136 

137 
(* 

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

139 
* pattern with constructor = Name. 

140 
**) 

141 
fun mk_group Name rows = 

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

143 
fn (in_group,not_in_group) => 

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

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

145 
in if ((#1(dest_Const pc) = Name) handle _ => false) 
2112  146 
then (((prefix,args@rst), rhs)::in_group, not_in_group) 
147 
else (in_group, row::not_in_group) end) 

148 
rows ([],[]); 

149 

150 
(* 

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

152 
**) 

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

154 
 partition gv ty_match 

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

156 
let val fresh = fresh_constr ty_match colty gv 

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

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

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

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

160 
val (Name,Ty) = dest_Const c' 
2112  161 
val (in_group, not_in_group) = mk_group Name rows 
162 
val in_group' = 

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

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

165 
else in_group 

166 
in 

167 
part{constrs = crst, 

168 
rows = not_in_group, 

169 
A = {constructor = c', 

170 
new_formals = gvars, 

171 
group = in_group'}::A} 

172 
end 

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

174 
end; 

175 

176 
(* 

177 
* Misc. routines used in mk_case 

178 
**) 

179 

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

180 
fun mk_pat (c,l) = 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

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

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

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

185 
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

186 
in map build l end; 
2112  187 

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

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

190 

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

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

193 

194 

195 
(* 

196 
* Translation of pattern terms into nested case expressions. 

197 
* 

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

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

200 
* incomplete set of patterns is given. 

201 
**) 

202 

3405  203 
fun mk_case ty_info ty_match usednames range_ty = 
2112  204 
let 
205 
fun mk_case_fail s = raise TFL_ERR{func = "mk_case", mesg = s} 

3405  206 
val fresh_var = gvvariant usednames 
2112  207 
val divide = partition fresh_var ty_match 
208 
fun expand constructors ty ((_,[]), _) = mk_case_fail"expand_var_row" 

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

3333
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

210 
if (is_Free p) 
2112  211 
then let val fresh = fresh_constr ty_match ty fresh_var 
212 
fun expnd (c,gvs) = 

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

213 
let val capp = list_comb(c,gvs) 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

214 
in ((prefix, capp::rst), pattern_subst[(p,capp)] rhs) 
2112  215 
end 
216 
in map expnd (map fresh constructors) end 

217 
else [row] 

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

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

220 
let val (tag,tm) = dest_pattern rhs 

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

222 
end 

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

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

225 
mk{path = path, 

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

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

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

3333
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

231 
if (forall is_Free col0) 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

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

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

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

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

237 
rights')} 
2112  238 
in (map v_to_pats pref_patl, tm) 
239 
end 

240 
else 

3944  241 
let val pty as Type (ty_name,_) = type_of p 
2112  242 
in 
243 
case (ty_info ty_name) 

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

244 
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

245 
 Some{case_const,constructors} => 
4221  246 
let open BasisLibrary (*restore original List*) 
4062  247 
val case_const_name = #1(dest_Const case_const) 
248 
val nrows = List.concat (map (expand constructors pty) rows) 

2112  249 
val subproblems = divide(constructors, pty, range_ty, nrows) 
250 
val groups = map #group subproblems 

251 
and new_formals = map #new_formals subproblems 

252 
and constructors' = map #constructor subproblems 

253 
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

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

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

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

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

259 
val types = map type_of (case_functions@[u]) @ [range_ty] 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

260 
val case_const' = Const(case_const_name, list_mk_type types) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

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

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

266 
end end 

267 
in mk 

268 
end; 

269 

270 

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

272 
fun FV_multiset tm = 

273 
case (S.dest_term tm) 

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

274 
of S.VAR{Name,Ty} => [Free(Name,Ty)] 
2112  275 
 S.CONST _ => [] 
276 
 S.COMB{Rator, Rand} => FV_multiset Rator @ FV_multiset Rand 

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

278 

279 
fun no_repeat_vars thy pat = 

280 
let fun check [] = true 

281 
 check (v::rst) = 

3333
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

282 
if mem_term (v,rst) then 
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

283 
raise TFL_ERR{func = "no_repeat_vars", 
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

284 
mesg = quote(#1(dest_Free v)) ^ 
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

285 
" occurs repeatedly in the pattern " ^ 
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

286 
quote (string_of_cterm (Thry.typecheck thy pat))} 
2112  287 
else check rst 
288 
in check (FV_multiset pat) 

289 
end; 

290 

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

291 
local fun mk_functional_err s = raise TFL_ERR{func = "mk_functional", mesg=s} 
6566  292 
fun single [Free(a,_)] = 
293 
mk_functional_err (a ^ " has not been declared as a constant") 

7052  294 
 single [_$_] = 
295 
mk_functional_err "recdef does not allow currying" 

296 
 single [Const arg] = arg 

297 
 single [_] = mk_functional_err "recdef: bad function name" 

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

298 
 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

299 
" distinct function names!") 
2112  300 
in 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

301 
fun mk_functional thy clauses = 
6566  302 
let val (L,R) = ListPair.unzip (map HOLogic.dest_eq clauses) 
303 
handle _ => raise TFL_ERR 

304 
{func = "mk_functional", 

305 
mesg = "recursion equations must use the = relation"} 

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

306 
val (funcs,pats) = ListPair.unzip (map (fn (t$u) =>(t,u)) L) 
7052  307 
val (fname, ftype) = single (gen_distinct (op aconv) funcs) 
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)) 
3405  311 
val names = foldr add_term_names (R,[]) 
312 
val atype = type_of(hd pats) 

313 
and aname = variant names "a" 

314 
val a = Free(aname,atype) 

2112  315 
val ty_info = Thry.match_info thy 
316 
val ty_match = Thry.match_type thy 

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

317 
val range_ty = type_of (hd R) 
3405  318 
val (patts, case_tm) = mk_case ty_info ty_match (aname::names) range_ty 
2112  319 
{path=[a], rows=rows} 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

320 
val patts1 = map (fn (_,(tag,i),[pat]) => tag (pat,i)) patts 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

321 
handle _ => mk_functional_err "error in patternmatch translation" 
2112  322 
val patts2 = U.sort(fn p1=>fn p2=> row_of_pat p1 < row_of_pat p2) patts1 
323 
val finals = map row_of_pat patts2 

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

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

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

328 
\ are inaccessible: "^stringize L) 

3944  329 
in {functional = Abs(Sign.base_name fname, ftype, 
7052  330 
abstract_over (Const(fname,ftype), 
331 
absfree(aname, atype, case_tm))), 

2112  332 
pats = patts2} 
333 
end end; 

334 

335 

336 
(* 

337 
* 

338 
* PRINCIPLES OF DEFINITION 

339 
* 

340 
**) 

341 

342 

6498  343 
(*For Isabelle, the lhs of a definition must be a constant.*) 
344 
fun mk_const_def sign (Name, Ty, rhs) = 

345 
Sign.infer_types sign (K None) (K None) [] false 

346 
([Const("==",dummyT) $ Const(Name,Ty) $ rhs], propT) 

347 
> #1; 

348 

3387
6f2eaa0ce04b
poly_tvars allows recdefs to be made without type constraints
paulson
parents:
3379
diff
changeset

349 
(*Make all TVars available for instantiation by adding a ? to the front*) 
6f2eaa0ce04b
poly_tvars allows recdefs to be made without type constraints
paulson
parents:
3379
diff
changeset

350 
fun poly_tvars (Type(a,Ts)) = Type(a, map (poly_tvars) Ts) 
6f2eaa0ce04b
poly_tvars allows recdefs to be made without type constraints
paulson
parents:
3379
diff
changeset

351 
 poly_tvars (TFree (a,sort)) = TVar (("?" ^ a, 0), sort) 
6f2eaa0ce04b
poly_tvars allows recdefs to be made without type constraints
paulson
parents:
3379
diff
changeset

352 
 poly_tvars (TVar ((a,i),sort)) = TVar (("?" ^ a, i+1), sort); 
6f2eaa0ce04b
poly_tvars allows recdefs to be made without type constraints
paulson
parents:
3379
diff
changeset

353 

3191  354 
local val f_eq_wfrec_R_M = 
355 
#ant(S.dest_imp(#2(S.strip_forall (concl Thms.WFREC_COROLLARY)))) 

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

3333
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

357 
val (fname,_) = dest_Free f 
3191  358 
val (wfrec,_) = S.strip_comb rhs 
359 
in 

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

360 
fun wfrec_definition0 thy fid R (functional as Abs(Name, Ty, _)) = 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

361 
let val def_name = if Name<>fid then 
3333
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

362 
raise TFL_ERR{func = "wfrec_definition0", 
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

363 
mesg = "Expected a definition of " ^ 
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

364 
quote fid ^ " but found one of " ^ 
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

365 
quote Name} 
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

366 
else Name ^ "_def" 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

367 
val wfrec_R_M = map_term_types poly_tvars 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

368 
(wfrec $ map_term_types poly_tvars R) 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

369 
$ functional 
6498  370 
val def_term = mk_const_def (Theory.sign_of thy) (Name, Ty, wfrec_R_M) 
6092  371 
in PureThy.add_defs_i [Thm.no_attributes (def_name, def_term)] thy end 
3191  372 
end; 
2112  373 

374 

375 

376 
(* 

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

378 
* from a datatype definition. 

379 
**) 

380 
fun extraction_thms thy = 

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

3459
112cbb8301dc
Removal of structure Context and its replacement by a theorem list of
paulson
parents:
3405
diff
changeset

382 
in (case_rewrites, case_congs) 
2112  383 
end; 
384 

385 

386 
(* 

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

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

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

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

391 
**) 

392 
fun merge full_pats TCs = 

393 
let fun insert (p,TCs) = 

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

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

395 
if (p aconv h) then (p,TCs)::rst else x::insrt rst 
2112  396 
 insrt (x::rst) = x::insrt rst 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

397 
 insrt[] = raise TFL_ERR{func="merge.insert", 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

398 
mesg="pattern not found"} 
2112  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 

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

407 
fun givens [] = [] 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

408 
 givens (GIVEN(tm,_)::pats) = tm :: givens pats 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

409 
 givens (OMITTED _::pats) = givens pats; 
2112  410 

6498  411 
(*called only by Tfl.simplify_defn*) 
412 
fun post_definition meta_tflCongs (theory, (def, pats)) = 

3191  413 
let val tych = Thry.typecheck theory 
414 
val f = #lhs(S.dest_eq(concl def)) 

415 
val corollary = R.MATCH_MP Thms.WFREC_COROLLARY def 

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

3191  418 
val R = #Rand(S.dest_comb WFR) 
2112  419 
val corollary' = R.UNDISCH corollary (* put WF R on assums *) 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

420 
val corollaries = map (fn pat => R.SPEC (tych pat) corollary') 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

421 
given_pats 
3191  422 
val (case_rewrites,context_congs) = extraction_thms theory 
3405  423 
val corollaries' = map(rewrite_rule case_rewrites) corollaries 
424 
val extract = R.CONTEXT_REWRITE_RULE 

6498  425 
(f, [R], cut_apply, meta_tflCongs@context_congs) 
3405  426 
val (rules, TCs) = ListPair.unzip (map extract corollaries') 
427 
val rules0 = map (rewrite_rule [Thms.CUT_DEF]) rules 

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

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

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

436 
end; 

437 

6498  438 

2112  439 
(* 
440 
* Perform the extraction without making the definition. Definition and 

6498  441 
* extraction commute for the nonnested case. (Deferred recdefs) 
442 
**) 

443 
fun wfrec_eqns thy fid tflCongs eqns = 

444 
let val {functional as Abs(Name, Ty, _), pats} = mk_functional thy eqns 

2112  445 
val given_pats = givens pats 
6498  446 
val f = #1 (S.strip_comb(#lhs(S.dest_eq (hd eqns)))) 
447 
(* val f = Free(Name,Ty) *) 

448 
val Type("fun", [f_dty, f_rty]) = Ty 

449 
val dummy = if Name<>fid then 

450 
raise TFL_ERR{func = "lazyR_def", 

451 
mesg = "Expected a definition of " ^ 

452 
quote fid ^ " but found one of " ^ 

453 
quote Name} 

454 
else () 

455 
val SV = S.free_vars_lr functional (* schema variables *) 

2112  456 
val (case_rewrites,context_congs) = extraction_thms thy 
457 
val tych = Thry.typecheck thy 

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

3405  459 
val Const("All",_) $ Abs(Rname,Rtype,_) = concl WFREC_THM0 
460 
val R = Free (variant (foldr add_term_names (eqns,[])) Rname, 

461 
Rtype) 

2112  462 
val WFREC_THM = R.ISPECL [tych R, tych f] WFREC_THM0 
463 
val ([proto_def, WFR],_) = S.strip_imp(concl WFREC_THM) 

6498  464 
val dummy = 
465 
if !trace then 

466 
writeln ("ORIGINAL PROTO_DEF: " ^ 

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

468 
else () 

2112  469 
val R1 = S.rand WFR 
470 
val corollary' = R.UNDISCH(R.UNDISCH WFREC_THM) 

3405  471 
val corollaries = map (fn pat => R.SPEC (tych pat) corollary') given_pats 
472 
val corollaries' = map (rewrite_rule case_rewrites) corollaries 

6498  473 
fun extract X = R.CONTEXT_REWRITE_RULE 
474 
(f, R1::SV, cut_apply, tflCongs@context_congs) X 

475 
in {proto_def = (*Use == rather than = for definitions*) 

476 
mk_const_def (Theory.sign_of thy) 

477 
(Name, Ty, S.rhs proto_def), 

478 
SV=SV, 

2112  479 
WFR=WFR, 
480 
pats=pats, 

481 
extracta = map extract corollaries'} 

482 
end; 

483 

484 

485 
(* 

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

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

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

489 
* such a relation must be wellfounded). 

6498  490 
**) 
491 
fun lazyR_def thy fid tflCongs eqns = 

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

493 
wfrec_eqns thy fid (congs tflCongs) eqns 

2112  494 
val R1 = S.rand WFR 
6498  495 
val f = #1 (Logic.dest_equals proto_def) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

496 
val (extractants,TCl) = ListPair.unzip extracta 
6498  497 
val dummy = if !trace 
498 
then (writeln "Extractants = "; 

499 
prths extractants; 

500 
()) 

501 
else () 

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

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

505 
val R'abs = S.rand R' 

6498  506 
val proto_def' = subst_free[(R1,R')] proto_def 
507 
val dummy = if !trace then writeln ("proto_def' = " ^ 

508 
Sign.string_of_term 

509 
(Theory.sign_of thy) proto_def') 

510 
else () 

4857  511 
val theory = 
512 
thy 

6498  513 
> PureThy.add_defs_i 
514 
[Thm.no_attributes (fid ^ "_def", proto_def')]; 

515 
val def = get_axiom theory (fid ^ "_def") RS meta_eq_to_obj_eq 

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

517 
else () 

2112  518 
val fconst = #lhs(S.dest_eq(concl def)) 
519 
val tych = Thry.typecheck theory 

6498  520 
val full_rqt_prop = map (Dcterm.mk_prop o tych) full_rqt 
521 
(*lcp: a lot of objectlogic inference to remove*) 

522 
val baz = R.DISCH_ALL 

523 
(U.itlist R.DISCH full_rqt_prop 

524 
(R.LIST_CONJ extractants)) 

525 
val dum = if !trace then writeln ("baz = " ^ string_of_thm baz) 

526 
else () 

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

2112  528 
val def' = R.MP (R.SPEC (tych fconst) 
6498  529 
(R.SPEC (tych R') 
530 
(R.GENL[tych R1, tych f_free] baz))) 

2112  531 
def 
6498  532 
val body_th = R.LIST_CONJ (map R.ASSUME full_rqt_prop) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

533 
val bar = R.MP (R.ISPECL[tych R'abs, tych R1] Thms.SELECT_AX) 
3191  534 
body_th 
6498  535 
in {theory = theory, R=R1, SV=SV, 
2112  536 
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

537 
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (givens pats, TCl)), 
2112  538 
patterns = pats} 
539 
end; 

540 

541 

542 

543 
(* 

544 
* 

545 
* INDUCTION THEOREM 

546 
* 

547 
**) 

548 

549 

550 
(* Miscellaneous function  

551 
* 

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

553 
*  

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

555 
* ... 

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

557 
* 

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

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

560 
* 

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

562 
* 

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

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

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

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

567 
**) 

568 

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

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

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

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

575 
"TFL fault [alpha_ex_unroll]: no correspondence" 
3405  576 
fun build ex [] = [] 
577 
 build (_$rex) (v::rst) = 

578 
let val ex1 = betapply(rex, v) 

579 
in ex1 :: build ex1 rst 

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

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

583 
(nex, ListPair.zip (args, rev exl)) 
2112  584 
end; 
585 

586 

587 

588 
(* 

589 
* 

590 
* PROVING COMPLETENESS OF PATTERNS 

591 
* 

592 
**) 

593 

3405  594 
fun mk_case ty_info usednames thy = 
2112  595 
let 
3405  596 
val divide = ipartition (gvvariant usednames) 
2112  597 
val tych = Thry.typecheck thy 
3353
9112a2efb9a3
Removal of module Mask and datatype binding with its constructor >
paulson
parents:
3333
diff
changeset

598 
fun tych_binding(x,y) = (tych x, tych y) 
2112  599 
fun fail s = raise TFL_ERR{func = "mk_case", mesg = s} 
600 
fun mk{rows=[],...} = fail"no rows" 

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

602 
R.IT_EXISTS (map tych_binding bindings) thm 

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

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

604 
let val (pat_rectangle,rights) = ListPair.unzip rows 
2112  605 
val col0 = map hd pat_rectangle 
606 
val pat_rectangle' = map tl pat_rectangle 

607 
in 

3333
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

608 
if (forall is_Free col0) (* column 0 is all variables *) 
3353
9112a2efb9a3
Removal of module Mask and datatype binding with its constructor >
paulson
parents:
3333
diff
changeset

609 
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

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

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

3944  614 
let val Type (ty_name,_) = type_of p 
2112  615 
in 
616 
case (ty_info ty_name) 

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

617 
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

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

621 
val subproblems = divide(constructors, rows) 

622 
val groups = map #group subproblems 

623 
and new_formals = map #new_formals subproblems 

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

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

625 
(new_formals, disjuncts) 
2112  626 
val constraints = map #1 existentials 
627 
val vexl = map #2 existentials 

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

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

630 
rows = map (expnd c) rows}) 

631 
(U.zip3 new_formals groups constraints) 

632 
val recursive_thms = map mk news 

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

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

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

635 
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

636 
val thms' = ListPair.map build_exists (vexl, recursive_thms) 
2112  637 
val same_concls = R.EVEN_ORS thms' 
638 
in R.DISJ_CASESL thm' same_concls 

639 
end 

640 
end end 

641 
in mk 

642 
end; 

643 

644 

645 
fun complete_cases thy = 

646 
let val tych = Thry.typecheck thy 

647 
val ty_info = Thry.induct_info thy 

648 
in fn pats => 

3405  649 
let val names = foldr add_term_names (pats,[]) 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

650 
val T = type_of (hd pats) 
3405  651 
val aname = Term.variant names "a" 
652 
val vname = Term.variant (aname::names) "v" 

653 
val a = Free (aname, T) 

654 
val v = Free (vname, T) 

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

655 
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

656 
val ex_th0 = R.EXISTS (tych (S.mk_exists{Bvar=v,Body=a_eq_v}), tych a) 
2112  657 
(R.REFL (tych a)) 
658 
val th0 = R.ASSUME (tych a_eq_v) 

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

660 
in 

661 
R.GEN (tych a) 

662 
(R.RIGHT_ASSOC 

663 
(R.CHOOSE(tych v, ex_th0) 

3405  664 
(mk_case ty_info (vname::aname::names) 
665 
thy {path=[v], rows=rows}))) 

2112  666 
end end; 
667 

668 

669 
(* 

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

671 
* 

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

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

674 
* allow nested defns to have R variable. 

675 
* 

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

677 
**) 

6498  678 
(* 
3405  679 
local infix 5 ==> 
2112  680 
fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2} 
681 
in 

682 
fun build_ih f P (pat,TCs) = 

683 
let val globals = S.free_vars_lr pat 

3405  684 
fun nested tm = is_some (S.find_term (curry (op aconv) f) tm) 
2112  685 
fun dest_TC tm = 
686 
let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm)) 

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

3405  688 
val P_y = if (nested tm) then R_y_pat ==> P$y else P$y 
2112  689 
in case cntxt 
690 
of [] => (P_y, (tm,[])) 

691 
 _ => let 

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

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

693 
val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals) 
3405  694 
val locals = #2(U.pluck (curry (op aconv) P) lvs) handle _ => lvs 
2112  695 
in (S.list_mk_forall(locals,imp), (tm,locals)) end 
696 
end 

697 
in case TCs 

3405  698 
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

699 
 _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs) 
3405  700 
val ind_clause = S.list_mk_conj ihs ==> P$pat 
2112  701 
in (S.list_mk_forall(globals,ind_clause), TCs_locals) 
702 
end 

703 
end 

704 
end; 

6498  705 
*) 
2112  706 

6498  707 
local infix 5 ==> 
708 
fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2} 

709 
in 

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

711 
let val pat_vars = S.free_vars_lr pat 

712 
val globals = pat_vars@SV 

713 
fun nested tm = is_some (S.find_term (curry (op aconv) f) tm) 

714 
fun dest_TC tm = 

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

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

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

718 
in case cntxt 

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

720 
 _ => let 

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

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

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

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

725 
end 

726 
in case TCs 

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

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

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

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

731 
end 

732 
end 

733 
end; 

2112  734 

735 
(* 

6498  736 
* This function makes good on the promise made in "build_ih". 
2112  737 
* 
738 
* Input is tm = "(!y. R y pat ==> P y) ==> P pat", 

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

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

741 
**) 

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

743 
let val tych = Thry.typecheck thy 

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

745 
val thm' = R.SPEC_ALL thm 

3405  746 
fun nested tm = is_some (S.find_term (curry (op aconv) f) tm) 
2112  747 
fun get_cntxt TC = tych(#ant(S.dest_imp(#2(S.strip_forall(concl TC))))) 
748 
fun mk_ih ((TC,locals),th2,nested) = 

749 
R.GENL (map tych locals) 

750 
(if nested 

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

752 
else if S.is_imp(concl TC) 

753 
then R.IMP_TRANS TC th2 

754 
else R.MP th2 TC) 

755 
in 

756 
R.DISCH antc 

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

758 
then let val th1 = R.ASSUME antc 

759 
val TCs = map #1 TCs_locals 

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

761 
#2 o S.strip_forall) TCs 

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

763 
TCs_locals 

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

765 
val nlist = map nested TCs 

766 
val triples = U.zip3 TClist th2list nlist 

767 
val Pylist = map mk_ih triples 

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

769 
else thm') 

770 
end; 

771 

772 

773 
(* 

774 
* 

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

776 
*  

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

778 
* 

779 
**) 

780 
fun LEFT_ABS_VSTRUCT tych thm = 

781 
let fun CHOOSER v (tm,thm) = 

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

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

784 
end 

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

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

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

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

790 

791 
(* 

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

793 
* 

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

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

796 
* the antecedent of Rinduct. 

797 
**) 

6498  798 
fun mk_induction thy {fconst, R, SV, pat_TCs_list} = 
2112  799 
let val tych = Thry.typecheck thy 
800 
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

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

803 
val domain = (type_of o hd) pats 
3405  804 
val Pname = Term.variant (foldr (foldr add_term_names) 
805 
(pats::TCsl, [])) "P" 

806 
val P = Free(Pname, domain > HOLogic.boolT) 

2112  807 
val Sinduct = R.SPEC (tych P) Sinduction 
808 
val Sinduct_assumf = S.rand ((#ant o S.dest_imp o concl) Sinduct) 

6498  809 
val Rassums_TCl' = map (build_ih fconst (P,SV)) pat_TCs_list 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

810 
val (Rassums,TCl') = ListPair.unzip Rassums_TCl' 
2112  811 
val Rinduct_assum = R.ASSUME (tych (S.list_mk_conj Rassums)) 
3405  812 
val cases = map (fn pat => betapply (Sinduct_assumf, pat)) pats 
2112  813 
val tasks = U.zip3 cases TCl' (R.CONJUNCTS Rinduct_assum) 
6498  814 
val proved_cases = map (prove_case fconst thy) tasks 
3405  815 
val v = Free (variant (foldr add_term_names (map concl proved_cases, [])) 
816 
"v", 

817 
domain) 

2112  818 
val vtyped = tych v 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

819 
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

820 
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

821 
(substs, proved_cases) 
2112  822 
val abs_cases = map (LEFT_ABS_VSTRUCT tych) proved_cases1 
823 
val dant = R.GEN vtyped (R.DISJ_CASESL (R.ISPEC vtyped case_thm) abs_cases) 

824 
val dc = R.MP Sinduct dant 

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

825 
val Parg_ty = type_of(#Bvar(S.dest_forall(concl dc))) 
3405  826 
val vars = map (gvvariant[Pname]) (S.strip_prod_type Parg_ty) 
2112  827 
val dc' = U.itlist (R.GEN o tych) vars 
828 
(R.SPEC (tych(S.mk_vstruct Parg_ty vars)) dc) 

829 
in 

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

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

831 
end 
2112  832 
handle _ => raise TFL_ERR{func = "mk_induction", mesg = "failed derivation"}; 
833 

834 

835 

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

836 

2112  837 
(* 
838 
* 

839 
* POST PROCESSING 

840 
* 

841 
**) 

842 

843 

844 
fun simplify_induction thy hth ind = 

845 
let val tych = Thry.typecheck thy 

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

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

848 
val tc = S.lhs tc_eq_tc' 

849 
fun loop [] = ind 

850 
 loop (asm::rst) = 

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

852 
then R.UNDISCH 

853 
(R.MATCH_MP 

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

855 
hth) 

856 
else loop rst 

857 
in loop asl 

858 
end; 

859 

860 

861 
(* 

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

863 
* assumption to the theorem. 

864 
**) 

865 
fun elim_tc tcthm (rule,induction) = 

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

867 

868 

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

870 
let val tych = Thry.typecheck theory 

871 

872 
(* 

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

874 
**) 

875 
val (rules1,induction1) = 

3405  876 
let val thm = R.prove(tych(HOLogic.mk_Trueprop 
877 
(hd(#1(R.dest_thm rules)))), 

878 
WFtac) 

2112  879 
in (R.PROVE_HYP thm rules, R.PROVE_HYP thm induction) 
880 
end handle _ => (rules,induction) 

881 

882 
(* 

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

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

885 
* 

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

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

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

889 
**) 

6498  890 

891 
fun print_thms s L = 

892 
if !trace then writeln (cat_lines (s :: map string_of_thm L)) 

893 
else (); 

894 

895 
fun print_cterms s L = 

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

897 
else ();; 

898 

2112  899 
fun simplify_tc tc (r,ind) = 
6498  900 
let val tc1 = tych tc 
901 
val _ = print_cterms "TC before simplification: " [tc1] 

902 
val tc_eq = simplifier tc1 

903 
val _ = print_thms "result: " [tc_eq] 

2112  904 
in 
905 
elim_tc (R.MATCH_MP Thms.eqT tc_eq) (r,ind) 

906 
handle _ => 

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

3405  908 
(R.prove(tych(HOLogic.mk_Trueprop(S.rhs(concl tc_eq))), 
909 
terminator))) 

2112  910 
(r,ind) 
911 
handle _ => 

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

913 
simplify_induction theory tc_eq ind)) 

914 
end 

915 

916 
(* 

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

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

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

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

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

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

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

924 
* 

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

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

927 
* 3. return  tc = tc' 

928 
**) 

929 
fun simplify_nested_tc tc = 

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

931 
in 

932 
R.GEN_ALL 

933 
(R.MATCH_MP Thms.eqT tc_eq 

934 
handle _ 

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

3405  936 
(R.prove(tych(HOLogic.mk_Trueprop (S.rhs(concl tc_eq))), 
937 
terminator)) 

2112  938 
handle _ => tc_eq)) 
939 
end 

940 

941 
(* 

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

943 
* in the induction theorem. 

944 
**) 

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

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

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

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

3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

949 
val extra_tcs = gen_rems (op aconv) (ftcs, tcs) 
2112  950 
val extra_tc_thms = map simplify_nested_tc extra_tcs 
951 
val (r1,ind1) = U.rev_itlist simplify_tc tcs (r,ind) 

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

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

954 
end 

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

955 
val rules_tcs = ListPair.zip (R.CONJUNCTS rules1, TCs) 
2112  956 
val (rules2,ind2,extras) = loop(rules_tcs,[],[],induction1) 
957 
in 

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

959 
end; 

960 

961 
end; (* TFL *) 