author  paulson 
Tue, 03 Jun 1997 11:08:08 +0200  
changeset 3391  5e45dd3b64e9 
parent 3388  dbf61e36f8e9 
child 3405  2cccd0e3e9ea 
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 

12 
(* Abbreviations *) 

13 
structure R = Rules; 

14 
structure S = USyntax; 

15 
structure U = S.Utils; 

16 

17 
val concl = #2 o R.dest_thm; 

18 
val hyp = #1 o R.dest_thm; 

19 

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

20 
val list_mk_type = U.end_itlist (curry(op >)); 
2112  21 

22 
fun gtake f = 

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

24 
 grab(n, x::rst) = 

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

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

27 
in grab 

28 
end; 

29 

30 
fun enumerate L = 

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

32 

33 
fun stringize [] = "" 

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

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

35 
 stringize (h::t) = (Int.toString h^", "^stringize t); 
2112  36 

37 

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

39 

40 

41 
(* 

42 
* The next function is common to patternmatch translation and 

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

44 
* 

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

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

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

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

48 
* function contains embedded refs! 
2112  49 
**) 
50 
fun gvvariant vlist = 

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

51 
let val slist = ref (map (#1 o dest_Free) vlist) 
3301
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset

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

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

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

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

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

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

59 
end; 
2112  60 

61 

62 
(* 

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

64 
* partitioning up pattern rows by the leading constructor. 

65 
**) 

66 
fun ipartition gv (constructors,rows) = 

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

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

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

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

71 
 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

72 
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

73 
val L = binder_types Ty 
2112  74 
val (in_group, not_in_group) = 
75 
U.itlist (fn (row as (p::rst, rhs)) => 

76 
fn (in_group,not_in_group) => 

77 
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

78 
in if (#1(dest_Const pc) = Name) 
2112  79 
then ((args@rst, rhs)::in_group, not_in_group) 
80 
else (in_group, row::not_in_group) 

81 
end) rows ([],[]) 

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

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

85 
A = {constructor = c, 

86 
new_formals = map gv col_types, 

87 
group = in_group}::A} 

88 
end 

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

90 
end; 

91 

92 

93 

94 
(* 

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

96 
* clause in a function definition. 

97 
**) 

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

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

99 
 OMITTED of term * int 
2112  100 

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

101 
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

102 
 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

103 

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

104 
fun pattern_subst theta = pattern_map (subst_free theta); 
2112  105 

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

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

108 

109 
val pat_of = #2 o dest_pattern; 

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

111 

112 
(* 

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

114 
**) 

115 
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

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

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

118 
and ty = body_type Ty 
2112  119 
val ty_theta = ty_match ty colty 
120 
val c' = S.inst ty_theta c 

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

122 
in (c', gvars) 

123 
end; 

124 

125 

126 
(* 

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

128 
* pattern with constructor = Name. 

129 
**) 

130 
fun mk_group Name rows = 

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

132 
fn (in_group,not_in_group) => 

133 
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

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

137 
rows ([],[]); 

138 

139 
(* 

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

141 
**) 

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

143 
 partition gv ty_match 

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

145 
let val fresh = fresh_constr ty_match colty gv 

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

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

148 
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

149 
val (Name,Ty) = dest_Const c' 
2112  150 
val (in_group, not_in_group) = mk_group Name rows 
151 
val in_group' = 

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

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

154 
else in_group 

155 
in 

156 
part{constrs = crst, 

157 
rows = not_in_group, 

158 
A = {constructor = c', 

159 
new_formals = gvars, 

160 
group = in_group'}::A} 

161 
end 

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

163 
end; 

164 

165 
(* 

166 
* Misc. routines used in mk_case 

167 
**) 

168 

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

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

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

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

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

174 
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

175 
in map build l end; 
2112  176 

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

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

179 

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

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

182 

183 

184 
(* 

185 
* Translation of pattern terms into nested case expressions. 

186 
* 

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

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

189 
* incomplete set of patterns is given. 

190 
**) 

191 

192 
fun mk_case ty_info ty_match FV range_ty = 

193 
let 

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

195 
val fresh_var = gvvariant FV 

196 
val divide = partition fresh_var ty_match 

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

198 
 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

199 
if (is_Free p) 
2112  200 
then let val fresh = fresh_constr ty_match ty fresh_var 
201 
fun expnd (c,gvs) = 

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

202 
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

203 
in ((prefix, capp::rst), pattern_subst[(p,capp)] rhs) 
2112  204 
end 
205 
in map expnd (map fresh constructors) end 

206 
else [row] 

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

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

209 
let val (tag,tm) = dest_pattern rhs 

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

211 
end 

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

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

214 
mk{path = path, 

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

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

217 
let val (pat_rectangle,rights) = ListPair.unzip rows 
2112  218 
val col0 = map(hd o #2) pat_rectangle 
219 
in 

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

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

221 
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

222 
(ListPair.zip (col0, rights)) 
2112  223 
val pat_rectangle' = map v_to_prefix pat_rectangle 
224 
val (pref_patl,tm) = mk{path = rstp, 

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

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

226 
rights')} 
2112  227 
in (map v_to_pats pref_patl, tm) 
228 
end 

229 
else 

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

230 
let val pty as Type (ty_name,_) = type_of p 
2112  231 
in 
232 
case (ty_info ty_name) 

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

233 
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

234 
 Some{case_const,constructors} => 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

235 
let val case_const_name = #1(dest_Const case_const) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

236 
val nrows = List_.concat (map (expand constructors pty) rows) 
2112  237 
val subproblems = divide(constructors, pty, range_ty, nrows) 
238 
val groups = map #group subproblems 

239 
and new_formals = map #new_formals subproblems 

240 
and constructors' = map #constructor subproblems 

241 
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

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

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

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

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

247 
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

248 
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

249 
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

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

251 
(ListPair.map mk_pat (constructors', pat_rect)) 
2112  252 
in (pat_rect1,tree) 
253 
end 

254 
end end 

255 
in mk 

256 
end; 

257 

258 

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

260 
fun FV_multiset tm = 

261 
case (S.dest_term tm) 

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

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

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

266 

267 
fun no_repeat_vars thy pat = 

268 
let fun check [] = true 

269 
 check (v::rst) = 

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

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

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

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

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

274 
quote (string_of_cterm (Thry.typecheck thy pat))} 
2112  275 
else check rst 
276 
in check (FV_multiset pat) 

277 
end; 

278 

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

279 
local fun mk_functional_err s = raise TFL_ERR{func = "mk_functional", mesg=s} 
3191  280 
fun single [f] = f 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

281 
 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

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

284 
fun mk_functional thy clauses = 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

285 
let val (L,R) = ListPair.unzip 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

286 
(map (fn (Const("op =",_) $ t $ u) => (t,u)) clauses) 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

287 
val (funcs,pats) = ListPair.unzip (map (fn (t$u) =>(t,u)) L) 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

288 
val f = single (gen_distinct (op aconv) funcs) 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

289 
(**??why change the Const to a Free??????????????**) 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

290 
val fvar = if (is_Free f) then f else Free(dest_Const f) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

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

292 
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

293 
map GIVEN (enumerate R)) 
2112  294 
val fvs = S.free_varsl R 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

295 
val a = S.variant fvs (Free("a",type_of(hd pats))) 
2112  296 
val FV = a::fvs 
297 
val ty_info = Thry.match_info thy 

298 
val ty_match = Thry.match_type thy 

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

299 
val range_ty = type_of (hd R) 
2112  300 
val (patts, case_tm) = mk_case ty_info ty_match FV range_ty 
301 
{path=[a], rows=rows} 

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

302 
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

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

306 
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

307 
val dummy = case (originals\\finals) 
2112  308 
of [] => () 
309 
 L => mk_functional_err("The following rows (counting from zero)\ 

310 
\ are inaccessible: "^stringize L) 

3353
9112a2efb9a3
Removal of module Mask and datatype binding with its constructor >
paulson
parents:
3333
diff
changeset

311 
val case_tm' = subst_free [(f,fvar)] case_tm 
3191  312 
in {functional = S.list_mk_abs ([fvar,a], case_tm'), 
2112  313 
pats = patts2} 
314 
end end; 

315 

316 

317 
(* 

318 
* 

319 
* PRINCIPLES OF DEFINITION 

320 
* 

321 
**) 

322 

323 

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

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

325 
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

326 
 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

327 
 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

328 

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

329 

3191  330 
(* 
331 
* R is already assumed to be typecopacetic with M 

2112  332 
**) 
3191  333 
local val f_eq_wfrec_R_M = 
334 
#ant(S.dest_imp(#2(S.strip_forall (concl Thms.WFREC_COROLLARY)))) 

335 
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

336 
val (fname,_) = dest_Free f 
3191  337 
val (wfrec,_) = S.strip_comb rhs 
338 
in 

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

339 
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

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

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

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

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

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

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

346 
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

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

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

349 
val (_, def_term, _) = 
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

350 
Sign.infer_types (sign_of thy) (K None) (K None) [] false 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

351 
([HOLogic.mk_eq(Const(Name,Ty), wfrec_R_M)], 
3387
6f2eaa0ce04b
poly_tvars allows recdefs to be made without type constraints
paulson
parents:
3379
diff
changeset

352 
HOLogic.boolT) 
3333
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset

353 

3191  354 
in 
355 
Thry.make_definition thy def_name def_term 

356 
end 

357 
end; 

2112  358 

359 

360 

361 
(* 

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

363 
* from a datatype definition. 

364 
**) 

365 
structure Context = 

366 
struct 

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

367 
val non_datatype_context = ref []: thm list ref 
2112  368 
fun read() = !non_datatype_context 
369 
fun write L = (non_datatype_context := L) 

370 
end; 

371 

372 
fun extraction_thms thy = 

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

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

375 
end; 

376 

377 

378 
(* 

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

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

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

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

383 
**) 

384 
fun merge full_pats TCs = 

385 
let fun insert (p,TCs) = 

386 
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

387 
if (p aconv h) then (p,TCs)::rst else x::insrt rst 
2112  388 
 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

389 
 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

390 
mesg="pattern not found"} 
2112  391 
in insrt end 
392 
fun pass ([],ptcl_final) = ptcl_final 

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

394 
in 

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

396 
end; 

397 

398 

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

399 
(*Replace all TFrees by TVars [CURRENTLY UNUSED]*) 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

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

401 
map_term_types (map_type_tfree (fn (a,sort) => TVar ((a, 0), sort))); 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

402 

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

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

404 
 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

405 
 givens (OMITTED _::pats) = givens pats; 
2112  406 

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

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

410 
val corollary = R.MATCH_MP Thms.WFREC_COROLLARY def 

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

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

415 
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

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

3379
7091ffa99c93
Simplified the calling sequence of CONTEXT_REWRITE_RULE
paulson
parents:
3353
diff
changeset

420 
{cut_lemma = R.ISPECL (map tych [f,R]) Thms.CUT_LEMMA, 
7091ffa99c93
Simplified the calling sequence of CONTEXT_REWRITE_RULE
paulson
parents:
3353
diff
changeset

421 
congs = context_congs, 
7091ffa99c93
Simplified the calling sequence of CONTEXT_REWRITE_RULE
paulson
parents:
3353
diff
changeset

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

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

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

427 
in 

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

429 
rules = rules1, 

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

430 
full_pats_TCs = merge (map pat_of pats) 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

431 
(ListPair.zip (given_pats, TCs)), 
2112  432 
TCs = TCs, 
433 
patterns = pats} 

434 
end; 

435 

436 
(* 

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

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

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

440 
**) 

441 
fun wfrec_eqns thy eqns = 

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

443 
val given_pats = givens pats 

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

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

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

446 
val (Name, Type("fun", [f_dty, f_rty])) = dest_Free f 
2112  447 
val (case_rewrites,context_congs) = extraction_thms thy 
448 
val tych = Thry.typecheck thy 

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

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

450 
val R = S.variant(foldr add_term_frees (eqns,[])) 
2112  451 
(#Bvar(S.dest_forall(concl WFREC_THM0))) 
452 
val WFREC_THM = R.ISPECL [tych R, tych f] WFREC_THM0 

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

454 
val R1 = S.rand WFR 

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

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

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

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

3379
7091ffa99c93
Simplified the calling sequence of CONTEXT_REWRITE_RULE
paulson
parents:
3353
diff
changeset

459 
{cut_lemma = R.ISPECL (map tych [f,R1]) Thms.CUT_LEMMA, 
7091ffa99c93
Simplified the calling sequence of CONTEXT_REWRITE_RULE
paulson
parents:
3353
diff
changeset

460 
congs = context_congs, 
7091ffa99c93
Simplified the calling sequence of CONTEXT_REWRITE_RULE
paulson
parents:
3353
diff
changeset

461 
th = th} 
2112  462 
in {proto_def=proto_def, 
463 
WFR=WFR, 

464 
pats=pats, 

465 
extracta = map extract corollaries'} 

466 
end; 

467 

468 

469 
(* 

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

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

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

473 
* such a relation must be wellfounded). 

474 
**) 

475 
fun lazyR_def thy eqns = 

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

477 
val R1 = S.rand WFR 

478 
val f = S.lhs proto_def 

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

479 
val (Name,_) = dest_Free f 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

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

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

484 
val R'abs = S.rand R' 

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

485 
val (def,theory) = Thry.make_definition thy (Name ^ "_def") 
3353
9112a2efb9a3
Removal of module Mask and datatype binding with its constructor >
paulson
parents:
3333
diff
changeset

486 
(subst_free[(R1,R')] proto_def) 
2112  487 
val fconst = #lhs(S.dest_eq(concl def)) 
488 
val tych = Thry.typecheck theory 

489 
val baz = R.DISCH (tych proto_def) 

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

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

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

493 
def 

494 
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

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

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

502 

503 

504 

505 
(* 

506 
* 

507 
* INDUCTION THEOREM 

508 
* 

509 
**) 

510 

511 

512 
(* Miscellaneous function  

513 
* 

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

515 
*  

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

517 
* ... 

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

519 
* 

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

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

522 
* 

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

524 
* 

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

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

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

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

529 
**) 

530 

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

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

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

535 
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

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

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

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

541 
in ex1::build ex1 rst 

542 
end 

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

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

545 
(nex, ListPair.zip (args, rev exl)) 
2112  546 
end; 
547 

548 

549 

550 
(* 

551 
* 

552 
* PROVING COMPLETENESS OF PATTERNS 

553 
* 

554 
**) 

555 

556 
fun mk_case ty_info FV thy = 

557 
let 

558 
val divide = ipartition (gvvariant FV) 

559 
val tych = Thry.typecheck thy 

3353
9112a2efb9a3
Removal of module Mask and datatype binding with its constructor >
paulson
parents:
3333
diff
changeset

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

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

564 
R.IT_EXISTS (map tych_binding bindings) thm 

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

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

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

569 
in 

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

570 
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

571 
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

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

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

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

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

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

579 
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

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

583 
val subproblems = divide(constructors, rows) 

584 
val groups = map #group subproblems 

585 
and new_formals = map #new_formals subproblems 

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

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

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

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

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

592 
rows = map (expnd c) rows}) 

593 
(U.zip3 new_formals groups constraints) 

594 
val recursive_thms = map mk news 

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

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

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

597 
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

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

601 
end 

602 
end end 

603 
in mk 

604 
end; 

605 

606 

607 
fun complete_cases thy = 

608 
let val tych = Thry.typecheck thy 

609 
val ty_info = Thry.induct_info thy 

610 
in fn pats => 

611 
let val FV0 = S.free_varsl pats 

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

612 
val T = type_of (hd pats) 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

613 
val a = S.variant FV0 (Free ("a", T)) 
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

614 
val v = S.variant (a::FV0) (Free ("v", T)) 
2112  615 
val FV = a::v::FV0 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

616 
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

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

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

621 
in 

622 
R.GEN (tych a) 

623 
(R.RIGHT_ASSOC 

624 
(R.CHOOSE(tych v, ex_th0) 

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

626 
end end; 

627 

628 

629 
(* 

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

631 
* 

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

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

634 
* allow nested defns to have R variable. 

635 
* 

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

637 
**) 

638 

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

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

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

642 
in 

643 
fun build_ih f P (pat,TCs) = 

644 
let val globals = S.free_vars_lr pat 

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

646 
fun dest_TC tm = 

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

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

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

650 
in case cntxt 

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

652 
 _ => let 

653 
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

654 
val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals) 
2112  655 
val locals = #2(U.pluck (S.aconv P) lvs) handle _ => lvs 
656 
in (S.list_mk_forall(locals,imp), (tm,locals)) end 

657 
end 

658 
in case TCs 

659 
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

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

663 
end 

664 
end 

665 
end; 

666 

667 

668 

669 
(* 

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

671 
* <something>. 

672 
* 

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

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

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

676 
**) 

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

678 
let val tych = Thry.typecheck thy 

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

680 
val thm' = R.SPEC_ALL thm 

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

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

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

684 
R.GENL (map tych locals) 

685 
(if nested 

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

687 
else if S.is_imp(concl TC) 

688 
then R.IMP_TRANS TC th2 

689 
else R.MP th2 TC) 

690 
in 

691 
R.DISCH antc 

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

693 
then let val th1 = R.ASSUME antc 

694 
val TCs = map #1 TCs_locals 

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

696 
#2 o S.strip_forall) TCs 

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

698 
TCs_locals 

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

700 
val nlist = map nested TCs 

701 
val triples = U.zip3 TClist th2list nlist 

702 
val Pylist = map mk_ih triples 

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

704 
else thm') 

705 
end; 

706 

707 

708 
(* 

709 
* 

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

711 
*  

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

713 
* 

714 
**) 

715 
fun LEFT_ABS_VSTRUCT tych thm = 

716 
let fun CHOOSER v (tm,thm) = 

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

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

719 
end 

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

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

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

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

725 

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

727 

728 

729 
(* 

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

731 
* 

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

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

734 
* the antecedent of Rinduct. 

735 
**) 

736 
fun mk_induction thy f R pat_TCs_list = 

737 
let val tych = Thry.typecheck thy 

738 
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

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

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

742 
val P = S.variant (S.all_varsl (pats @ List_.concat TCsl)) 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

743 
(Free("P",domain > HOLogic.boolT)) 
2112  744 
val Sinduct = R.SPEC (tych P) Sinduction 
745 
val Sinduct_assumf = S.rand ((#ant o S.dest_imp o concl) Sinduct) 

746 
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

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

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

751 
val proved_cases = map (prove_case f thy) tasks 

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

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

753 
(Free("v",domain)) 
2112  754 
val vtyped = tych v 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

755 
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

756 
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

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

760 
val dc = R.MP Sinduct dant 

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

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

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

765 
in 

766 
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

767 
end 
2112  768 
handle _ => raise TFL_ERR{func = "mk_induction", mesg = "failed derivation"}; 
769 

770 

771 

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

772 

2112  773 
(* 
774 
* 

775 
* POST PROCESSING 

776 
* 

777 
**) 

778 

779 

780 
fun simplify_induction thy hth ind = 

781 
let val tych = Thry.typecheck thy 

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

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

784 
val tc = S.lhs tc_eq_tc' 

785 
fun loop [] = ind 

786 
 loop (asm::rst) = 

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

788 
then R.UNDISCH 

789 
(R.MATCH_MP 

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

791 
hth) 

792 
else loop rst 

793 
in loop asl 

794 
end; 

795 

796 

797 
(* 

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

799 
* assumption to the theorem. 

800 
**) 

801 
fun elim_tc tcthm (rule,induction) = 

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

803 

804 

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

806 
let val tych = Thry.typecheck theory 

807 

808 
(* 

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

810 
**) 

811 
val (rules1,induction1) = 

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

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

814 
end handle _ => (rules,induction) 

815 

816 
(* 

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

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

819 
* 

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

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

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

823 
**) 

824 
fun simplify_tc tc (r,ind) = 

825 
let val tc_eq = simplifier (tych tc) 

826 
in 

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

828 
handle _ => 

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

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

831 
(r,ind) 

832 
handle _ => 

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

834 
simplify_induction theory tc_eq ind)) 

835 
end 

836 

837 
(* 

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

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

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

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

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

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

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

845 
* 

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

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

848 
* 3. return  tc = tc' 

849 
**) 

850 
fun simplify_nested_tc tc = 

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

852 
in 

853 
R.GEN_ALL 

854 
(R.MATCH_MP Thms.eqT tc_eq 

855 
handle _ 

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

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

858 
handle _ => tc_eq)) 

859 
end 

860 

861 
(* 

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

863 
* in the induction theorem. 

864 
**) 

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

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

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

868 
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

869 
val extra_tcs = gen_rems (op aconv) (ftcs, tcs) 
2112  870 
val extra_tc_thms = map simplify_nested_tc extra_tcs 
871 
val (r1,ind1) = U.rev_itlist simplify_tc tcs (r,ind) 

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

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

874 
end 

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

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

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

879 
end; 

880 

881 
end; (* TFL *) 