author  wenzelm 
Fri, 10 Nov 2000 19:02:37 +0100  
changeset 10432  3dfbc913d184 
parent 10015  8c16ec5ba62b 
permissions  rwrr 
9866  1 
(* Title: TFL/tfl.sml 
3301
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 

9866  6 
First part of 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 

9866  9 
signature TFL_sig = 
10 
sig 

11 
val trace: bool ref 

12 
type pattern 

13 
val mk_functional: theory > term list > {functional: term, pats: pattern list} 

14 
val wfrec_definition0: theory > string > term > term > theory * thm 

15 
val post_definition: thm list > theory * (thm * pattern list) > 

16 
{theory: theory, 

17 
rules: thm, 

18 
rows: int list, 

19 
TCs: term list list, 

20 
full_pats_TCs: (term * term list) list} 

21 
val wfrec_eqns: theory > xstring > thm list > term list > 

22 
{WFR: term, 

23 
SV: term list, 

24 
proto_def: term, 

25 
extracta: (thm * term list) list, 

26 
pats: pattern list} 

27 
val lazyR_def: theory > xstring > thm list > term list > 

28 
{theory: theory, 

29 
rules: thm, 

30 
R: term, 

31 
SV: term list, 

32 
full_pats_TCs: (term * term list) list, 

33 
patterns : pattern list} 

34 
val mk_induction: theory > 

35 
{fconst: term, R: term, SV: term list, pat_TCs_list: (term * term list) list} > thm 

36 
val postprocess: {wf_tac: tactic, terminator: tactic, simplifier: cterm > thm} > theory > 

37 
{rules: thm, induction: thm, TCs: term list list} > 

38 
{rules: thm, induction: thm, nested_tcs: thm list} 

39 
end; 

40 

41 

42 
structure Prim: TFL_sig = 

2112  43 
struct 
44 

6498  45 
val trace = ref false; 
46 

9866  47 
open BasisLibrary; 
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

48 

2112  49 
structure R = Rules; 
50 
structure S = USyntax; 

9878  51 
structure U = Utils; 
2112  52 

9866  53 

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

7262  55 

2112  56 
val concl = #2 o R.dest_thm; 
57 
val hyp = #1 o R.dest_thm; 

58 

9866  59 
val list_mk_type = U.end_itlist (curry (op >)); 
2112  60 

8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

61 
fun enumerate xs = ListPair.zip(xs, 0 upto (length xs  1)); 
2112  62 

7262  63 
fun front_last [] = raise TFL_ERR {func="front_last", mesg="empty list"} 
64 
 front_last [x] = ([],x) 

65 
 front_last (h::t) = 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

66 
let val (pref,x) = front_last t 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

67 
in 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

68 
(h::pref,x) 
7262  69 
end; 
2112  70 

71 

6498  72 
(* 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

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

76 
* The curried function "gvvariant" returns a function to generate distinct 
3405  77 
* variables that are guaranteed not to be in names. The names of 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

79 
* function contains embedded refs! 
2112  80 
**) 
3405  81 
fun gvvariant names = 
82 
let val slist = ref names 

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

83 
val vname = ref "u" 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

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

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

87 
else (slist := !vname :: !slist; !vname) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

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

90 
end; 
2112  91 

92 

93 
(* 

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

95 
* partitioning up pattern rows by the leading constructor. 

96 
**) 

97 
fun ipartition gv (constructors,rows) = 

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

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

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

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

102 
 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

103 
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

104 
val L = binder_types Ty 
2112  105 
val (in_group, not_in_group) = 
106 
U.itlist (fn (row as (p::rst, rhs)) => 

107 
fn (in_group,not_in_group) => 

108 
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

109 
in if (#1(dest_Const pc) = Name) 
2112  110 
then ((args@rst, rhs)::in_group, not_in_group) 
111 
else (in_group, row::not_in_group) 

112 
end) rows ([],[]) 

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

113 
val col_types = U.take type_of (length L, #1(hd in_group)) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

114 
in 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

115 
part{constrs = crst, rows = not_in_group, 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

116 
A = {constructor = c, 
2112  117 
new_formals = map gv col_types, 
118 
group = in_group}::A} 

119 
end 

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

121 
end; 

122 

123 

124 

125 
(* 

8631  126 
* Each pattern carries with it a tag (i,b) where 
127 
* i is the clause it came from and 

128 
* b=true indicates that clause was given by the user 

129 
* (or is an instantiation of a user supplied pattern) 

130 
* b=false > i = ~1 

2112  131 
**) 
8631  132 

8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

133 
type pattern = term * (int * bool) 
2112  134 

8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

135 
fun pattern_map f (tm,x) = (f tm, x); 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

136 

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

137 
fun pattern_subst theta = pattern_map (subst_free theta); 
8631  138 

8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

139 
val pat_of = fst; 
8642  140 
fun row_of_pat x = fst (snd x); 
141 
fun given x = snd (snd x); 

2112  142 

143 
(* 

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

145 
**) 

146 
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

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

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

149 
and ty = body_type Ty 
2112  150 
val ty_theta = ty_match ty colty 
151 
val c' = S.inst ty_theta c 

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

153 
in (c', gvars) 

154 
end; 

155 

156 

157 
(* 

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

159 
* pattern with constructor = Name. 

160 
**) 

161 
fun mk_group Name rows = 

7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

162 
U.itlist (fn (row as ((prfx, p::rst), rhs)) => 
2112  163 
fn (in_group,not_in_group) => 
164 
let val (pc,args) = S.strip_comb p 

10015  165 
in if ((#1(dest_Const pc) = Name) handle _ => false) (* FIXME do not handle _ !!! *) 
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

166 
then (((prfx,args@rst), rhs)::in_group, not_in_group) 
2112  167 
else (in_group, row::not_in_group) end) 
168 
rows ([],[]); 

169 

170 
(* 

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

172 
**) 

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

174 
 partition gv ty_match 

7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

175 
(constructors, colty, res_ty, rows as (((prfx,_),_)::_)) = 
2112  176 
let val fresh = fresh_constr ty_match colty gv 
177 
fun part {constrs = [], rows, A} = rev A 

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

179 
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

180 
val (Name,Ty) = dest_Const c' 
2112  181 
val (in_group, not_in_group) = mk_group Name rows 
182 
val in_group' = 

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

8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

184 
then [((prfx, #2(fresh c)), (S.ARB res_ty, (~1,false)))] 
2112  185 
else in_group 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

186 
in 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

187 
part{constrs = crst, 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

188 
rows = not_in_group, 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

189 
A = {constructor = c', 
2112  190 
new_formals = gvars, 
191 
group = in_group'}::A} 

192 
end 

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

194 
end; 

195 

196 
(* 

197 
* Misc. routines used in mk_case 

198 
**) 

199 

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

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

201 
let val L = length (binder_types (type_of c)) 
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

202 
fun build (prfx,tag,plist) = 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

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

204 
and plist' = drop(L,plist) 
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

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

206 
in map build l end; 
2112  207 

7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

208 
fun v_to_prfx (prfx, v::pats) = (v::prfx,pats) 
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

209 
 v_to_prfx _ = raise TFL_ERR{func="mk_case", mesg="v_to_prfx"}; 
2112  210 

7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

211 
fun v_to_pats (v::prfx,tag, pats) = (prfx, tag, v::pats) 
2112  212 
 v_to_pats _ = raise TFL_ERR{func="mk_case", mesg="v_to_pats"}; 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

213 

2112  214 

215 
(* 

216 
* Translation of pattern terms into nested case expressions. 

217 
* 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

218 
* This performs the translation and also builds the full set of patterns. 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

219 
* Thus it supports the construction of induction theorems even when an 
2112  220 
* incomplete set of patterns is given. 
221 
**) 

222 

3405  223 
fun mk_case ty_info ty_match usednames range_ty = 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

224 
let 
2112  225 
fun mk_case_fail s = raise TFL_ERR{func = "mk_case", mesg = s} 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

226 
val fresh_var = gvvariant usednames 
2112  227 
val divide = partition fresh_var ty_match 
228 
fun expand constructors ty ((_,[]), _) = mk_case_fail"expand_var_row" 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

229 
 expand constructors ty (row as ((prfx, p::rst), rhs)) = 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

230 
if (is_Free p) 
2112  231 
then let val fresh = fresh_constr ty_match ty fresh_var 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

232 
fun expnd (c,gvs) = 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

233 
let val capp = list_comb(c,gvs) 
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

234 
in ((prfx, capp::rst), pattern_subst[(p,capp)] rhs) 
2112  235 
end 
236 
in map expnd (map fresh constructors) end 

237 
else [row] 

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

8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

239 
 mk{path=[], rows = ((prfx, []), (tm,tag))::_} = (* Done *) 
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

240 
([(prfx,tag,[])], tm) 
2112  241 
 mk{path=[], rows = _::_} = mk_case_fail"blunder" 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

242 
 mk{path as u::rstp, rows as ((prfx, []), rhs)::rst} = 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

243 
mk{path = path, 
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

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

246 
let val (pat_rectangle,rights) = ListPair.unzip rows 
2112  247 
val col0 = map(hd o #2) pat_rectangle 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

248 
in 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

250 
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

251 
(ListPair.zip (col0, rights)) 
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

252 
val pat_rectangle' = map v_to_prfx pat_rectangle 
2112  253 
val (pref_patl,tm) = mk{path = rstp, 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

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

255 
rights')} 
2112  256 
in (map v_to_pats pref_patl, tm) 
257 
end 

258 
else 

3944  259 
let val pty as Type (ty_name,_) = type_of p 
2112  260 
in 
261 
case (ty_info ty_name) 

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

262 
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

263 
 Some{case_const,constructors} => 
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset

264 
let 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

265 
val case_const_name = #1(dest_Const case_const) 
4062  266 
val nrows = List.concat (map (expand constructors pty) rows) 
2112  267 
val subproblems = divide(constructors, pty, range_ty, nrows) 
268 
val groups = map #group subproblems 

269 
and new_formals = map #new_formals subproblems 

270 
and constructors' = map #constructor subproblems 

271 
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

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

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

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

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

277 
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

278 
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

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

281 
(ListPair.map mk_pat (constructors', pat_rect)) 
2112  282 
in (pat_rect1,tree) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

283 
end 
2112  284 
end end 
285 
in mk 

286 
end; 

287 

288 

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

290 
fun FV_multiset tm = 
2112  291 
case (S.dest_term tm) 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

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

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

296 

297 
fun no_repeat_vars thy pat = 

298 
let fun check [] = true 

299 
 check (v::rst) = 

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

300 
if mem_term (v,rst) then 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

301 
raise TFL_ERR{func = "no_repeat_vars", 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

302 
mesg = quote(#1(dest_Free v)) ^ 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

303 
" occurs repeatedly in the pattern " ^ 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

304 
quote (string_of_cterm (Thry.typecheck thy pat))} 
2112  305 
else check rst 
306 
in check (FV_multiset pat) 

307 
end; 

308 

7262  309 
fun dest_atom (Free p) = p 
310 
 dest_atom (Const p) = p 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

311 
 dest_atom _ = raise TFL_ERR {func="dest_atom", 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

312 
mesg="function name not an identifier"}; 
7262  313 

8492
6343c725ba7e
better error messages, especially for multiple types
paulson
parents:
8438
diff
changeset

314 
fun same_name (p,q) = #1(dest_atom p) = #1(dest_atom q); 
7262  315 

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

316 
local fun mk_functional_err s = raise TFL_ERR{func = "mk_functional", mesg=s} 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

317 
fun single [_$_] = 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

318 
mk_functional_err "recdef does not allow currying" 
7262  319 
 single [f] = f 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

320 
 single fs = 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

321 
(*multiple function names?*) 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

322 
if length (gen_distinct same_name fs) < length fs 
8492
6343c725ba7e
better error messages, especially for multiple types
paulson
parents:
8438
diff
changeset

323 
then mk_functional_err 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

324 
"The function being declared appears with multiple types" 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

325 
else mk_functional_err 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

326 
(Int.toString (length fs) ^ 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

329 
fun mk_functional thy clauses = 
6566  330 
let val (L,R) = ListPair.unzip (map HOLogic.dest_eq clauses) 
10015  331 
handle _ => raise TFL_ERR (* FIXME do not handle _ !!! *) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

332 
{func = "mk_functional", 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

333 
mesg = "recursion equations must use the = relation"} 
3391
5e45dd3b64e9
More deHOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset

334 
val (funcs,pats) = ListPair.unzip (map (fn (t$u) =>(t,u)) L) 
7262  335 
val atom = single (gen_distinct (op aconv) funcs) 
336 
val (fname,ftype) = dest_atom atom 

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

337 
val dummy = map (no_repeat_vars thy) pats 
7262  338 
val rows = ListPair.zip (map (fn x => ([]:term list,[x])) pats, 
8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

339 
map (fn (t,i) => (t,(i,true))) (enumerate R)) 
3405  340 
val names = foldr add_term_names (R,[]) 
341 
val atype = type_of(hd pats) 

342 
and aname = variant names "a" 

343 
val a = Free(aname,atype) 

2112  344 
val ty_info = Thry.match_info thy 
345 
val ty_match = Thry.match_type thy 

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

346 
val range_ty = type_of (hd R) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

347 
val (patts, case_tm) = mk_case ty_info ty_match (aname::names) range_ty 
2112  348 
{path=[a], rows=rows} 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

349 
val patts1 = map (fn (_,tag,[pat]) => (pat,tag)) patts 
10015  350 
handle _ => mk_functional_err "error in patternmatch translation" (* FIXME do not handle _ !!! *) 
2112  351 
val patts2 = U.sort(fn p1=>fn p2=> row_of_pat p1 < row_of_pat p2) patts1 
352 
val finals = map row_of_pat patts2 

353 
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

354 
val dummy = case (originals\\finals) 
2112  355 
of [] => () 
8631  356 
 L => mk_functional_err 
357 
("The following clauses are redundant (covered by preceding clauses): " ^ 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

358 
commas (map (fn i => Int.toString (i + 1)) L)) 
3944  359 
in {functional = Abs(Sign.base_name fname, ftype, 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

360 
abstract_over (atom, 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

361 
absfree(aname,atype, case_tm))), 
2112  362 
pats = patts2} 
363 
end end; 

364 

365 

366 
(* 

367 
* 

368 
* PRINCIPLES OF DEFINITION 

369 
* 

370 
**) 

371 

372 

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

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

376 
([Const("==",dummyT) $ Const(Name,Ty) $ rhs], propT) 
6498  377 
> #1; 
378 

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

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

380 
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

381 
 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

382 
 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

383 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

384 
local val f_eq_wfrec_R_M = 
3191  385 
#ant(S.dest_imp(#2(S.strip_forall (concl Thms.WFREC_COROLLARY)))) 
386 
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

387 
val (fname,_) = dest_Free f 
3191  388 
val (wfrec,_) = S.strip_comb rhs 
389 
in 

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

390 
fun wfrec_definition0 thy fid R (functional as Abs(Name, Ty, _)) = 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

391 
let val def_name = if Name<>fid then 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

392 
raise TFL_ERR{func = "wfrec_definition0", 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

393 
mesg = "Expected a definition of " ^ 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

394 
quote fid ^ " but found one of " ^ 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

395 
quote Name} 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

396 
else Name ^ "_def" 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

397 
val wfrec_R_M = map_term_types poly_tvars 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

398 
(wfrec $ map_term_types poly_tvars R) 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

399 
$ functional 
6498  400 
val def_term = mk_const_def (Theory.sign_of thy) (Name, Ty, wfrec_R_M) 
9651  401 
val (thy', [def]) = PureThy.add_defs_i false [Thm.no_attributes (def_name, def_term)] thy 
402 
in (thy', def) end; 

3191  403 
end; 
2112  404 

405 

406 

407 
(* 

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

409 
* from a datatype definition. 

410 
**) 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

411 
fun extraction_thms thy = 
2112  412 
let val {case_rewrites,case_congs} = Thry.extract_info thy 
3459
112cbb8301dc
Removal of structure Context and its replacement by a theorem list of
paulson
parents:
3405
diff
changeset

413 
in (case_rewrites, case_congs) 
2112  414 
end; 
415 

416 

417 
(* 

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

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

420 
* There can be fewer clauses than the full list, if the user omitted some 
2112  421 
* cases. This routine is used to prepare input for mk_induction. 
422 
**) 

423 
fun merge full_pats TCs = 

424 
let fun insert (p,TCs) = 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

425 
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

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

428 
 insrt[] = raise TFL_ERR{func="merge.insert", 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

429 
mesg="pattern not found"} 
2112  430 
in insrt end 
431 
fun pass ([],ptcl_final) = ptcl_final 

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

433 
in 
2112  434 
pass (TCs, map (fn p => (p,[])) full_pats) 
435 
end; 

436 

437 

8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

438 
fun givens pats = map pat_of (filter given pats); 
2112  439 

6498  440 
fun post_definition meta_tflCongs (theory, (def, pats)) = 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

441 
let val tych = Thry.typecheck theory 
3191  442 
val f = #lhs(S.dest_eq(concl def)) 
443 
val corollary = R.MATCH_MP Thms.WFREC_COROLLARY def 

8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

444 
val pats' = filter given pats 
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

445 
val given_pats = map pat_of pats' 
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

446 
val rows = map row_of_pat pats' 
2112  447 
val WFR = #ant(S.dest_imp(concl corollary)) 
3191  448 
val R = #Rand(S.dest_comb WFR) 
2112  449 
val corollary' = R.UNDISCH corollary (* put WF R on assums *) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

450 
val corollaries = map (fn pat => R.SPEC (tych pat) corollary') 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

451 
given_pats 
3191  452 
val (case_rewrites,context_congs) = extraction_thms theory 
3405  453 
val corollaries' = map(rewrite_rule case_rewrites) corollaries 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

454 
val extract = R.CONTEXT_REWRITE_RULE 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

455 
(f, [R], cut_apply, meta_tflCongs@context_congs) 
3405  456 
val (rules, TCs) = ListPair.unzip (map extract corollaries') 
457 
val rules0 = map (rewrite_rule [Thms.CUT_DEF]) rules 

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

2112  459 
val rules1 = R.LIST_CONJ(map mk_cond_rule rules0) 
460 
in 

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

462 
rules = rules1, 

8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

463 
rows = rows, 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

464 
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (given_pats, TCs)), 
8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset

465 
TCs = TCs} 
2112  466 
end; 
467 

6498  468 

2112  469 
(* 
470 
* Perform the extraction without making the definition. Definition and 

6498  471 
* extraction commute for the nonnested case. (Deferred recdefs) 
7262  472 
* 
473 
* The purpose of wfrec_eqns is merely to instantiate the recursion theorem 

474 
* and extract termination conditions: no definition is made. 

6498  475 
**) 
7262  476 

6498  477 
fun wfrec_eqns thy fid tflCongs eqns = 
7262  478 
let val {lhs,rhs} = S.dest_eq (hd eqns) 
479 
val (f,args) = S.strip_comb lhs 

480 
val (fname,fty) = dest_atom f 

481 
val (SV,a) = front_last args (* SV = schematic variables *) 

482 
val g = list_comb(f,SV) 

483 
val h = Free(fname,type_of g) 

484 
val eqns1 = map (subst_free[(g,h)]) eqns 

485 
val {functional as Abs(Name, Ty, _), pats} = mk_functional thy eqns1 

2112  486 
val given_pats = givens pats 
6498  487 
(* val f = Free(Name,Ty) *) 
488 
val Type("fun", [f_dty, f_rty]) = Ty 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

489 
val dummy = if Name<>fid then 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

490 
raise TFL_ERR{func = "wfrec_eqns", 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

491 
mesg = "Expected a definition of " ^ 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

492 
quote fid ^ " but found one of " ^ 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

493 
quote Name} 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

494 
else () 
2112  495 
val (case_rewrites,context_congs) = extraction_thms thy 
496 
val tych = Thry.typecheck thy 

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

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

500 
Rtype) 
7262  501 
val WFREC_THM = R.ISPECL [tych R, tych g] WFREC_THM0 
2112  502 
val ([proto_def, WFR],_) = S.strip_imp(concl WFREC_THM) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

503 
val dummy = 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

504 
if !trace then 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

505 
writeln ("ORIGINAL PROTO_DEF: " ^ 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

506 
Sign.string_of_term (Theory.sign_of thy) proto_def) 
6498  507 
else () 
2112  508 
val R1 = S.rand WFR 
509 
val corollary' = R.UNDISCH(R.UNDISCH WFREC_THM) 

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

512 
fun extract X = R.CONTEXT_REWRITE_RULE 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

513 
(f, R1::SV, cut_apply, tflCongs@context_congs) X 
7262  514 
in {proto_def = proto_def, 
6498  515 
SV=SV, 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

516 
WFR=WFR, 
2112  517 
pats=pats, 
518 
extracta = map extract corollaries'} 

519 
end; 

520 

521 

522 
(* 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

523 
* Define the constant after extracting the termination conditions. The 
2112  524 
* wellfounded relation used in the definition is computed by using the 
525 
* choice operator on the extracted conditions (plus the condition that 

526 
* such a relation must be wellfounded). 

6498  527 
**) 
7262  528 

6498  529 
fun lazyR_def thy fid tflCongs eqns = 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

530 
let val {proto_def,WFR,pats,extracta,SV} = 
9866  531 
wfrec_eqns thy fid tflCongs eqns 
2112  532 
val R1 = S.rand WFR 
7262  533 
val f = #lhs(S.dest_eq proto_def) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

534 
val (extractants,TCl) = ListPair.unzip extracta 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

535 
val dummy = if !trace 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

536 
then (writeln "Extractants = "; 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

537 
prths extractants; 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

538 
()) 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

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

543 
val R'abs = S.rand R' 

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

546 
Sign.string_of_term 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

547 
(Theory.sign_of thy) proto_def') 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

548 
else () 
7262  549 
val {lhs,rhs} = S.dest_eq proto_def' 
550 
val (c,args) = S.strip_comb lhs 

551 
val (Name,Ty) = dest_atom c 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

552 
val defn = mk_const_def (Theory.sign_of thy) 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

553 
(Name, Ty, S.list_mk_abs (args,rhs)) 
8438  554 
val (theory, [def0]) = 
7262  555 
thy 
9329  556 
> PureThy.add_defs_i false 
7262  557 
[Thm.no_attributes (fid ^ "_def", defn)] 
8438  558 
val def = freezeT def0; 
6498  559 
val dummy = if !trace then writeln ("DEF = " ^ string_of_thm def) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

560 
else () 
7262  561 
(* val fconst = #lhs(S.dest_eq(concl def)) *) 
2112  562 
val tych = Thry.typecheck theory 
6498  563 
val full_rqt_prop = map (Dcterm.mk_prop o tych) full_rqt 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

564 
(*lcp: a lot of objectlogic inference to remove*) 
6498  565 
val baz = R.DISCH_ALL 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

566 
(U.itlist R.DISCH full_rqt_prop 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

567 
(R.LIST_CONJ extractants)) 
6498  568 
val dum = if !trace then writeln ("baz = " ^ string_of_thm baz) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

569 
else () 
6498  570 
val f_free = Free (fid, fastype_of f) (*'cos f is a Const*) 
7262  571 
val SV' = map tych SV; 
572 
val SVrefls = map reflexive SV' 

573 
val def0 = (U.rev_itlist (fn x => fn th => R.rbeta(combination th x)) 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

574 
SVrefls def) 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

575 
RS meta_eq_to_obj_eq 
7262  576 
val def' = R.MP (R.SPEC (tych R') (R.GEN (tych R1) baz)) def0 
6498  577 
val body_th = R.LIST_CONJ (map R.ASSUME full_rqt_prop) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

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

582 
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (givens pats, TCl)), 
2112  583 
patterns = pats} 
584 
end; 

585 

586 

587 

588 
(* 

589 
* 

590 
* INDUCTION THEOREM 

591 
* 

592 
**) 

593 

594 

595 
(* Miscellaneous function  

596 
* 

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

598 
*  

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

600 
* ... 
2112  601 
* (x_j,?v_n. M[x_1,...,x_(n1),v_n])] ) 
602 
* 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

603 
* This function is totally ad hoc. Used in the production of the induction 
2112  604 
* theorem. The nchotomy theorem can have clauses that look like 
605 
* 

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

607 
* 

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

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

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

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

612 
**) 

613 

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

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

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

618 
val args = map (fn qv => the (gen_assoc (op aconv) (plist, qv))) qvars 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

620 
"TFL fault [alpha_ex_unroll]: no correspondence" 
3405  621 
fun build ex [] = [] 
622 
 build (_$rex) (v::rst) = 

623 
let val ex1 = betapply(rex, v) 

624 
in ex1 :: build ex1 rst 

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

626 
val (nex::exl) = rev (tm::build tm args) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

628 
(nex, ListPair.zip (args, rev exl)) 
2112  629 
end; 
630 

631 

632 

633 
(* 

634 
* 

635 
* PROVING COMPLETENESS OF PATTERNS 

636 
* 

637 
**) 

638 

3405  639 
fun mk_case ty_info usednames thy = 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

640 
let 
3405  641 
val divide = ipartition (gvvariant usednames) 
2112  642 
val tych = Thry.typecheck thy 
3353
9112a2efb9a3
Removal of module Mask and datatype binding with its constructor >
paulson
parents:
3333
diff
changeset

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

646 
 mk{path=[], rows = [([], (thm, bindings))]} = 
2112  647 
R.IT_EXISTS (map tych_binding bindings) thm 
648 
 mk{path = u::rstp, rows as (p::_, _)::_} = 

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

649 
let val (pat_rectangle,rights) = ListPair.unzip rows 
2112  650 
val col0 = map hd pat_rectangle 
651 
val pat_rectangle' = map tl pat_rectangle 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

653 
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

654 
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

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

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

3944  659 
let val Type (ty_name,_) = type_of p 
2112  660 
in 
661 
case (ty_info ty_name) 

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

662 
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

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

666 
val subproblems = divide(constructors, rows) 

667 
val groups = map #group subproblems 

668 
and new_formals = map #new_formals subproblems 

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

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

670 
(new_formals, disjuncts) 
2112  671 
val constraints = map #1 existentials 
672 
val vexl = map #2 existentials 

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

674 
val news = map (fn (nf,rows,c) => {path = nf@rstp, 
2112  675 
rows = map (expnd c) rows}) 
676 
(U.zip3 new_formals groups constraints) 

677 
val recursive_thms = map mk news 

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

678 
val build_exists = foldr 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

680 
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

681 
val thms' = ListPair.map build_exists (vexl, recursive_thms) 
2112  682 
val same_concls = R.EVEN_ORS thms' 
683 
in R.DISJ_CASESL thm' same_concls 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

684 
end 
2112  685 
end end 
686 
in mk 

687 
end; 

688 

689 

690 
fun complete_cases thy = 

691 
let val tych = Thry.typecheck thy 

692 
val ty_info = Thry.induct_info thy 

693 
in fn pats => 

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

695 
val T = type_of (hd pats) 
3405  696 
val aname = Term.variant names "a" 
697 
val vname = Term.variant (aname::names) "v" 

698 
val a = Free (aname, T) 

699 
val v = Free (vname, T) 

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

700 
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

701 
val ex_th0 = R.EXISTS (tych (S.mk_exists{Bvar=v,Body=a_eq_v}), tych a) 
2112  702 
(R.REFL (tych a)) 
703 
val th0 = R.ASSUME (tych a_eq_v) 

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

705 
in 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

706 
R.GEN (tych a) 
2112  707 
(R.RIGHT_ASSOC 
708 
(R.CHOOSE(tych v, ex_th0) 

3405  709 
(mk_case ty_info (vname::aname::names) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

710 
thy {path=[v], rows=rows}))) 
2112  711 
end end; 
712 

713 

714 
(* 

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

716 
* 

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

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

719 
* allow nested defns to have R variable. 

720 
* 

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

722 
**) 

6498  723 
(* 
3405  724 
local infix 5 ==> 
2112  725 
fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2} 
726 
in 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

727 
fun build_ih f P (pat,TCs) = 
2112  728 
let val globals = S.free_vars_lr pat 
3405  729 
fun nested tm = is_some (S.find_term (curry (op aconv) f) tm) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

730 
fun dest_TC tm = 
2112  731 
let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm)) 
732 
val (R,y,_) = S.dest_relation R_y_pat 

3405  733 
val P_y = if (nested tm) then R_y_pat ==> P$y else P$y 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

734 
in case cntxt 
2112  735 
of [] => (P_y, (tm,[])) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

736 
 _ => let 
2112  737 
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

738 
val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals) 
10015  739 
val locals = #2(U.pluck (curry (op aconv) P) lvs) handle _ => lvs (* FIXME do not handle _ !!! *) 
2112  740 
in (S.list_mk_forall(locals,imp), (tm,locals)) end 
741 
end 

742 
in case TCs 

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

744 
 _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs) 
3405  745 
val ind_clause = S.list_mk_conj ihs ==> P$pat 
2112  746 
in (S.list_mk_forall(globals,ind_clause), TCs_locals) 
747 
end 

748 
end 

749 
end; 

6498  750 
*) 
2112  751 

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

754 
in 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

755 
fun build_ih f (P,SV) (pat,TCs) = 
6498  756 
let val pat_vars = S.free_vars_lr pat 
757 
val globals = pat_vars@SV 

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

759 
fun dest_TC tm = 
6498  760 
let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm)) 
761 
val (R,y,_) = S.dest_relation R_y_pat 

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

763 
in case cntxt 
6498  764 
of [] => (P_y, (tm,[])) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

765 
 _ => let 
6498  766 
val imp = S.list_mk_conj cntxt ==> P_y 
767 
val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals) 

10015  768 
val locals = #2(U.pluck (curry (op aconv) P) lvs) handle _ => lvs (* FIXME do not handle _ !!! *) 
6498  769 
in (S.list_mk_forall(locals,imp), (tm,locals)) end 
770 
end 

771 
in case TCs 

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

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

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

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

776 
end 

777 
end 

778 
end; 

2112  779 

780 
(* 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

781 
* This function makes good on the promise made in "build_ih". 
2112  782 
* 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

783 
* Input is tm = "(!y. R y pat ==> P y) ==> P pat", 
2112  784 
* TCs = TC_1[pat] ... TC_n[pat] 
785 
* thm = ih1 /\ ... /\ ih_n  ih[pat] 

786 
**) 

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

788 
let val tych = Thry.typecheck thy 

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

790 
val thm' = R.SPEC_ALL thm 

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

794 
R.GENL (map tych locals) 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

795 
(if nested 
10015  796 
then R.DISCH (get_cntxt TC) th2 handle _ => th2 (* FIXME do not handle _ !!! *) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

797 
else if S.is_imp(concl TC) 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

798 
then R.IMP_TRANS TC th2 
2112  799 
else R.MP th2 TC) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

800 
in 
2112  801 
R.DISCH antc 
802 
(if S.is_imp(concl thm') (* recursive calls in this clause *) 

803 
then let val th1 = R.ASSUME antc 

804 
val TCs = map #1 TCs_locals 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

805 
val ylist = map (#2 o S.dest_relation o #2 o S.strip_imp o 
2112  806 
#2 o S.strip_forall) TCs 
807 
val TClist = map (fn(TC,lvs) => (R.SPEC_ALL(R.ASSUME(tych TC)),lvs)) 

808 
TCs_locals 

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

810 
val nlist = map nested TCs 

811 
val triples = U.zip3 TClist th2list nlist 

812 
val Pylist = map mk_ih triples 

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

814 
else thm') 

815 
end; 

816 

817 

818 
(* 

819 
* 

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

821 
*  

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

823 
* 

824 
**) 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

825 
fun LEFT_ABS_VSTRUCT tych thm = 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

826 
let fun CHOOSER v (tm,thm) = 
2112  827 
let val ex_tm = S.mk_exists{Bvar=v,Body=tm} 
828 
in (ex_tm, R.CHOOSE(tych v, R.ASSUME (tych ex_tm)) thm) 

829 
end 

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

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

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

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

835 

836 
(* 

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

838 
* 

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

840 
* recursion induction (Rinduct) by proving the antecedent of Sinduct from 
2112  841 
* the antecedent of Rinduct. 
842 
**) 

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

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

848 
val domain = (type_of o hd) pats 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

849 
val Pname = Term.variant (foldr (foldr add_term_names) 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

850 
(pats::TCsl, [])) "P" 
3405  851 
val P = Free(Pname, domain > HOLogic.boolT) 
2112  852 
val Sinduct = R.SPEC (tych P) Sinduction 
853 
val Sinduct_assumf = S.rand ((#ant o S.dest_imp o concl) Sinduct) 

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

855 
val (Rassums,TCl') = ListPair.unzip Rassums_TCl' 
2112  856 
val Rinduct_assum = R.ASSUME (tych (S.list_mk_conj Rassums)) 
3405  857 
val cases = map (fn pat => betapply (Sinduct_assumf, pat)) pats 
2112  858 
val tasks = U.zip3 cases TCl' (R.CONJUNCTS Rinduct_assum) 
6498  859 
val proved_cases = map (prove_case fconst thy) tasks 
3405  860 
val v = Free (variant (foldr add_term_names (map concl proved_cases, [])) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

861 
"v", 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

864 
val substs = map (R.SYM o R.ASSUME o tych o (curry HOLogic.mk_eq v)) pats 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

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

866 
(substs, proved_cases) 
2112  867 
val abs_cases = map (LEFT_ABS_VSTRUCT tych) proved_cases1 
868 
val dant = R.GEN vtyped (R.DISJ_CASESL (R.ISPEC vtyped case_thm) abs_cases) 

869 
val dc = R.MP Sinduct dant 

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

870 
val Parg_ty = type_of(#Bvar(S.dest_forall(concl dc))) 
3405  871 
val vars = map (gvvariant[Pname]) (S.strip_prod_type Parg_ty) 
2112  872 
val dc' = U.itlist (R.GEN o tych) vars 
873 
(R.SPEC (tych(S.mk_vstruct Parg_ty vars)) dc) 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

874 
in 
2112  875 
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

876 
end 
10015  877 
handle _ => raise TFL_ERR{func = "mk_induction", mesg = "failed derivation"}; (* FIXME do not handle _ !!! *) 
2112  878 

879 

880 

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

881 

2112  882 
(* 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

883 
* 
2112  884 
* POST PROCESSING 
885 
* 

886 
**) 

887 

888 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

889 
fun simplify_induction thy hth ind = 
2112  890 
let val tych = Thry.typecheck thy 
891 
val (asl,_) = R.dest_thm ind 

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

893 
val tc = S.lhs tc_eq_tc' 

894 
fun loop [] = ind 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

895 
 loop (asm::rst) = 
2112  896 
if (U.can (Thry.match_term thy asm) tc) 
897 
then R.UNDISCH 

898 
(R.MATCH_MP 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

899 
(R.MATCH_MP Thms.simp_thm (R.DISCH (tych asm) ind)) 
2112  900 
hth) 
901 
else loop rst 

902 
in loop asl 

903 
end; 

904 

905 

906 
(* 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

907 
* The termination condition is an antecedent to the rule, and an 
2112  908 
* assumption to the theorem. 
909 
**) 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

910 
fun elim_tc tcthm (rule,induction) = 
2112  911 
(R.MP rule tcthm, R.PROVE_HYP tcthm induction) 
912 

913 

9866  914 
fun postprocess{wf_tac, terminator, simplifier} theory {rules,induction,TCs} = 
2112  915 
let val tych = Thry.typecheck theory 
916 

917 
(* 

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

919 
**) 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

920 
val (rules1,induction1) = 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

921 
let val thm = R.prove(tych(HOLogic.mk_Trueprop 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

922 
(hd(#1(R.dest_thm rules)))), 
9866  923 
wf_tac) 
2112  924 
in (R.PROVE_HYP thm rules, R.PROVE_HYP thm induction) 
10015  925 
end handle _ => (rules,induction) (* FIXME do not handle _ !!! *) 
2112  926 

927 
(* 

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

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

930 
* 

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

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

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

934 
**) 

6498  935 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

936 
fun print_thms s L = 
7262  937 
if !trace then writeln (cat_lines (s :: map string_of_thm L)) 
938 
else (); 

6498  939 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

940 
fun print_cterms s L = 
7262  941 
if !trace then writeln (cat_lines (s :: map string_of_cterm L)) 
942 
else ();; 

6498  943 

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

947 
val tc_eq = simplifier tc1 

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

949 
in 
2112  950 
elim_tc (R.MATCH_MP Thms.eqT tc_eq) (r,ind) 
10015  951 
handle _ => (* FIXME do not handle _ !!! *) 
2112  952 
(elim_tc (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

953 
(R.prove(tych(HOLogic.mk_Trueprop(S.rhs(concl tc_eq))), 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

954 
terminator))) 
2112  955 
(r,ind) 
10015  956 
handle _ => (* FIXME do not handle _ !!! *) 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

957 
(R.UNDISCH(R.MATCH_MP (R.MATCH_MP Thms.simp_thm r) tc_eq), 
2112  958 
simplify_induction theory tc_eq ind)) 
959 
end 

960 

961 
(* 

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

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

963 
* left embedded in the body of the function (and in induction 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

964 
* theorem hypotheses). Our "solution" is to simplify them, and try to 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

965 
* prove termination, but leave the application of the resulting theorem 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

966 
* to a higher level. So things go much as in "simplify_tc": the 
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

967 
* termination condition (tc) is simplified to  tc = tc' (there might 
2112  968 
* not be a change) and then 2 attempts are made: 
969 
* 

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

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

972 
* 3. return  tc = tc' 

973 
**) 

974 
fun simplify_nested_tc tc = 

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

976 
in 

977 
R.GEN_ALL 

978 
(R.MATCH_MP Thms.eqT tc_eq 

10015  979 
handle _ (* FIXME do not handle _ !!! *) 
2112  980 
=> (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq) 
3405  981 
(R.prove(tych(HOLogic.mk_Trueprop (S.rhs(concl tc_eq))), 
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

982 
terminator)) 
10015  983 
handle _ => tc_eq)) (* FIXME do not handle _ !!! *) 
2112  984 
end 
985 

986 
(* 

8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset

987 
* Attempt to simplify the termination conditions in each rule and 
2112  988 
* in the induction theorem. 
989 
**) 

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

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

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

993 
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

994 
val extra_tcs = gen_rems (op aconv) (ftcs, tcs) 
2112  995 
val extra_tc_thms = map simplify_nested_tc extra_tcs 
996 
val (r1,ind1) = U.rev_itlist simplify_tc tcs (r,ind) 

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

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

999 
end 

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

1000 
val rules_tcs = ListPair.zip (R.CONJUNCTS rules1, TCs) 
2112  1001 
val (rules2,ind2,extras) = loop(rules_tcs,[],[],induction1) 
1002 
in 

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

1004 
end; 

1005 

9866  1006 
end; 