author  paulson 
Thu, 05 Jun 1997 13:27:28 +0200  
changeset 3405  2cccd0e3e9ea 
parent 3391  5e45dd3b64e9 
child 3459  112cbb8301dc 
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 
3405  46 
* 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

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 
**) 
3405  50 
fun gvvariant names = 
51 
let val slist = ref names 

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 

3405  192 
fun mk_case ty_info ty_match usednames range_ty = 
2112  193 
let 
194 
fun mk_case_fail s = raise TFL_ERR{func = "mk_case", mesg = s} 

3405  195 
val fresh_var = gvvariant usednames 
2112  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) 
3405  288 
val fcon as Const (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

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

290 
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

291 
map GIVEN (enumerate R)) 
3405  292 
val names = foldr add_term_names (R,[]) 
293 
val atype = type_of(hd pats) 

294 
and aname = variant names "a" 

295 
val a = Free(aname,atype) 

2112  296 
val ty_info = Thry.match_info thy 
297 
val ty_match = Thry.match_type thy 

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

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

301 
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

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

305 
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

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

309 
\ are inaccessible: "^stringize L) 

3405  310 
in {functional = Abs(fname, ftype, 
311 
abstract_over (fcon, 

312 
absfree(aname,atype, 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 

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

331 
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

332 
val (fname,_) = dest_Free f 
3191  333 
val (wfrec,_) = S.strip_comb rhs 
334 
in 

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

335 
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

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

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

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

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

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

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

342 
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

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

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

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

346 
Sign.infer_types (sign_of thy) (K None) (K None) [] false 
3405  347 
([Const("==",dummyT) $ Const(Name,Ty) $ wfrec_R_M], 
348 
propT) 

349 
in add_defs_i [(def_name, def_term)] thy end 

3191  350 
end; 
2112  351 

352 

353 

354 
(* 

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

356 
* from a datatype definition. 

357 
**) 

358 
structure Context = 

359 
struct 

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

360 
val non_datatype_context = ref []: thm list ref 
2112  361 
fun read() = !non_datatype_context 
362 
fun write L = (non_datatype_context := L) 

363 
end; 

364 

365 
fun extraction_thms thy = 

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

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

368 
end; 

369 

370 

371 
(* 

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

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

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

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

376 
**) 

377 
fun merge full_pats TCs = 

378 
let fun insert (p,TCs) = 

379 
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

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

382 
 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

383 
mesg="pattern not found"} 
2112  384 
in insrt end 
385 
fun pass ([],ptcl_final) = ptcl_final 

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

387 
in 

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

389 
end; 

390 

391 

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

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

393 
 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

394 
 givens (OMITTED _::pats) = givens pats; 
2112  395 

3405  396 
fun post_definition ss (theory, (def, pats)) = 
3191  397 
let val tych = Thry.typecheck theory 
398 
val f = #lhs(S.dest_eq(concl def)) 

399 
val corollary = R.MATCH_MP Thms.WFREC_COROLLARY def 

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

3191  402 
val R = #Rand(S.dest_comb WFR) 
2112  403 
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

404 
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

405 
given_pats 
3191  406 
val (case_rewrites,context_congs) = extraction_thms theory 
3405  407 
val corollaries' = map(rewrite_rule case_rewrites) corollaries 
408 
val extract = R.CONTEXT_REWRITE_RULE 

409 
(ss, f, R, 

410 
R.ISPECL (map tych [f,R]) Thms.CUT_LEMMA, 

411 
context_congs) 

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

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

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

2112  415 
val rules1 = R.LIST_CONJ(map mk_cond_rule rules0) 
416 
in 

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

418 
rules = rules1, 

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

419 
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

420 
(ListPair.zip (given_pats, TCs)), 
2112  421 
TCs = TCs, 
422 
patterns = pats} 

423 
end; 

424 

425 
(* 

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

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

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

3405  429 
* CURRENTLY UNUSED 
430 
fun wfrec_eqns ss thy eqns = 

2112  431 
let val {functional,pats} = mk_functional thy eqns 
432 
val given_pats = givens pats 

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

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

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

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

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

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

441 
Rtype) 

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

444 
val R1 = S.rand WFR 

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

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

448 
val extract = R.CONTEXT_REWRITE_RULE 

449 
(ss, f, R1, 

450 
R.ISPECL (map tych [f,R1]) Thms.CUT_LEMMA, 

451 
context_congs) 

2112  452 
in {proto_def=proto_def, 
453 
WFR=WFR, 

454 
pats=pats, 

455 
extracta = map extract corollaries'} 

456 
end; 

3405  457 
**) 
2112  458 

459 

460 
(* 

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

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

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

464 
* such a relation must be wellfounded). 

3405  465 
* CURRENTLY UNUSED 
466 
fun lazyR_def ss thy eqns = 

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

2112  468 
val R1 = S.rand WFR 
469 
val f = S.lhs proto_def 

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

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

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

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

475 
val R'abs = S.rand R' 

3405  476 
val theory = add_defs_i [(Name ^ "_def", subst_free[(R1,R')] proto_def)] 
477 
thy 

478 
val def = freezeT((get_axiom theory (Name ^ "_def")) RS meta_eq_to_obj_eq) 

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

481 
val baz = R.DISCH (tych proto_def) 

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

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

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

485 
def 

486 
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

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

491 
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (givens pats, TCl)), 
2112  492 
patterns = pats} 
493 
end; 

3405  494 
**) 
2112  495 

496 

497 

498 
(* 

499 
* 

500 
* INDUCTION THEOREM 

501 
* 

502 
**) 

503 

504 

505 
(* Miscellaneous function  

506 
* 

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

508 
*  

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

510 
* ... 

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

512 
* 

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

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

515 
* 

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

517 
* 

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

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

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

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

522 
**) 

523 

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

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

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

528 
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

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

530 
"TFL fault [alpha_ex_unroll]: no correspondence" 
3405  531 
fun build ex [] = [] 
532 
 build (_$rex) (v::rst) = 

533 
let val ex1 = betapply(rex, v) 

534 
in ex1 :: build ex1 rst 

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

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

538 
(nex, ListPair.zip (args, rev exl)) 
2112  539 
end; 
540 

541 

542 

543 
(* 

544 
* 

545 
* PROVING COMPLETENESS OF PATTERNS 

546 
* 

547 
**) 

548 

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

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

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

557 
R.IT_EXISTS (map tych_binding bindings) thm 

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

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

559 
let val (pat_rectangle,rights) = ListPair.unzip rows 
2112  560 
val col0 = map hd pat_rectangle 
561 
val pat_rectangle' = map tl pat_rectangle 

562 
in 

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

563 
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

564 
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

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

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

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

569 
let val Type (ty_name,_) = type_of p 
2112  570 
in 
571 
case (ty_info ty_name) 

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

572 
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

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

576 
val subproblems = divide(constructors, rows) 

577 
val groups = map #group subproblems 

578 
and new_formals = map #new_formals subproblems 

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

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

580 
(new_formals, disjuncts) 
2112  581 
val constraints = map #1 existentials 
582 
val vexl = map #2 existentials 

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

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

585 
rows = map (expnd c) rows}) 

586 
(U.zip3 new_formals groups constraints) 

587 
val recursive_thms = map mk news 

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

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

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

590 
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

591 
val thms' = ListPair.map build_exists (vexl, recursive_thms) 
2112  592 
val same_concls = R.EVEN_ORS thms' 
593 
in R.DISJ_CASESL thm' same_concls 

594 
end 

595 
end end 

596 
in mk 

597 
end; 

598 

599 

600 
fun complete_cases thy = 

601 
let val tych = Thry.typecheck thy 

602 
val ty_info = Thry.induct_info thy 

603 
in fn pats => 

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

605 
val T = type_of (hd pats) 
3405  606 
val aname = Term.variant names "a" 
607 
val vname = Term.variant (aname::names) "v" 

608 
val a = Free (aname, T) 

609 
val v = Free (vname, T) 

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

610 
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

611 
val ex_th0 = R.EXISTS (tych (S.mk_exists{Bvar=v,Body=a_eq_v}), tych a) 
2112  612 
(R.REFL (tych a)) 
613 
val th0 = R.ASSUME (tych a_eq_v) 

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

615 
in 

616 
R.GEN (tych a) 

617 
(R.RIGHT_ASSOC 

618 
(R.CHOOSE(tych v, ex_th0) 

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

2112  621 
end end; 
622 

623 

624 
(* 

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

626 
* 

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

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

629 
* allow nested defns to have R variable. 

630 
* 

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

632 
**) 

633 

3405  634 
local infix 5 ==> 
2112  635 
fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2} 
636 
in 

637 
fun build_ih f P (pat,TCs) = 

638 
let val globals = S.free_vars_lr pat 

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

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

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

646 
 _ => let 

647 
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

648 
val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals) 
3405  649 
val locals = #2(U.pluck (curry (op aconv) P) lvs) handle _ => lvs 
2112  650 
in (S.list_mk_forall(locals,imp), (tm,locals)) end 
651 
end 

652 
in case TCs 

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

654 
 _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs) 
3405  655 
val ind_clause = S.list_mk_conj ihs ==> P$pat 
2112  656 
in (S.list_mk_forall(globals,ind_clause), TCs_locals) 
657 
end 

658 
end 

659 
end; 

660 

661 

662 

663 
(* 

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

665 
* <something>. 

666 
* 

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

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

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

670 
**) 

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

672 
let val tych = Thry.typecheck thy 

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

674 
val thm' = R.SPEC_ALL thm 

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

678 
R.GENL (map tych locals) 

679 
(if nested 

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

681 
else if S.is_imp(concl TC) 

682 
then R.IMP_TRANS TC th2 

683 
else R.MP th2 TC) 

684 
in 

685 
R.DISCH antc 

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

687 
then let val th1 = R.ASSUME antc 

688 
val TCs = map #1 TCs_locals 

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

690 
#2 o S.strip_forall) TCs 

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

692 
TCs_locals 

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

694 
val nlist = map nested TCs 

695 
val triples = U.zip3 TClist th2list nlist 

696 
val Pylist = map mk_ih triples 

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

698 
else thm') 

699 
end; 

700 

701 

702 
(* 

703 
* 

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

705 
*  

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

707 
* 

708 
**) 

709 
fun LEFT_ABS_VSTRUCT tych thm = 

710 
let fun CHOOSER v (tm,thm) = 

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

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

713 
end 

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

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

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

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

719 

720 
(* 

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

722 
* 

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

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

725 
* the antecedent of Rinduct. 

726 
**) 

727 
fun mk_induction thy f R pat_TCs_list = 

728 
let val tych = Thry.typecheck thy 

729 
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

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

732 
val domain = (type_of o hd) pats 
3405  733 
val Pname = Term.variant (foldr (foldr add_term_names) 
734 
(pats::TCsl, [])) "P" 

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

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

738 
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

739 
val (Rassums,TCl') = ListPair.unzip Rassums_TCl' 
2112  740 
val Rinduct_assum = R.ASSUME (tych (S.list_mk_conj Rassums)) 
3405  741 
val cases = map (fn pat => betapply (Sinduct_assumf, pat)) pats 
2112  742 
val tasks = U.zip3 cases TCl' (R.CONJUNCTS Rinduct_assum) 
743 
val proved_cases = map (prove_case f thy) tasks 

3405  744 
val v = Free (variant (foldr add_term_names (map concl proved_cases, [])) 
745 
"v", 

746 
domain) 

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

748 
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

749 
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

750 
(substs, proved_cases) 
2112  751 
val abs_cases = map (LEFT_ABS_VSTRUCT tych) proved_cases1 
752 
val dant = R.GEN vtyped (R.DISJ_CASESL (R.ISPEC vtyped case_thm) abs_cases) 

753 
val dc = R.MP Sinduct dant 

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

754 
val Parg_ty = type_of(#Bvar(S.dest_forall(concl dc))) 
3405  755 
val vars = map (gvvariant[Pname]) (S.strip_prod_type Parg_ty) 
2112  756 
val dc' = U.itlist (R.GEN o tych) vars 
757 
(R.SPEC (tych(S.mk_vstruct Parg_ty vars)) dc) 

758 
in 

759 
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

760 
end 
2112  761 
handle _ => raise TFL_ERR{func = "mk_induction", mesg = "failed derivation"}; 
762 

763 

764 

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

765 

2112  766 
(* 
767 
* 

768 
* POST PROCESSING 

769 
* 

770 
**) 

771 

772 

773 
fun simplify_induction thy hth ind = 

774 
let val tych = Thry.typecheck thy 

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

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

777 
val tc = S.lhs tc_eq_tc' 

778 
fun loop [] = ind 

779 
 loop (asm::rst) = 

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

781 
then R.UNDISCH 

782 
(R.MATCH_MP 

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

784 
hth) 

785 
else loop rst 

786 
in loop asl 

787 
end; 

788 

789 

790 
(* 

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

792 
* assumption to the theorem. 

793 
**) 

794 
fun elim_tc tcthm (rule,induction) = 

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

796 

797 

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

799 
let val tych = Thry.typecheck theory 

800 

801 
(* 

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

803 
**) 

804 
val (rules1,induction1) = 

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

807 
WFtac) 

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

810 

811 
(* 

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

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

814 
* 

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

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

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

818 
**) 

819 
fun simplify_tc tc (r,ind) = 

820 
let val tc_eq = simplifier (tych tc) 

821 
in 

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

823 
handle _ => 

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

3405  825 
(R.prove(tych(HOLogic.mk_Trueprop(S.rhs(concl tc_eq))), 
826 
terminator))) 

2112  827 
(r,ind) 
828 
handle _ => 

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

830 
simplify_induction theory tc_eq ind)) 

831 
end 

832 

833 
(* 

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

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

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

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

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

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

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

841 
* 

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

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

844 
* 3. return  tc = tc' 

845 
**) 

846 
fun simplify_nested_tc tc = 

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

848 
in 

849 
R.GEN_ALL 

850 
(R.MATCH_MP Thms.eqT tc_eq 

851 
handle _ 

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

3405  853 
(R.prove(tych(HOLogic.mk_Trueprop (S.rhs(concl tc_eq))), 
854 
terminator)) 

2112  855 
handle _ => tc_eq)) 
856 
end 

857 

858 
(* 

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

860 
* in the induction theorem. 

861 
**) 

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

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

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

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

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

866 
val extra_tcs = gen_rems (op aconv) (ftcs, tcs) 
2112  867 
val extra_tc_thms = map simplify_nested_tc extra_tcs 
868 
val (r1,ind1) = U.rev_itlist simplify_tc tcs (r,ind) 

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

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

871 
end 

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

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

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

876 
end; 

877 

878 
end; (* TFL *) 