author | paulson |
Wed, 28 Jun 2000 10:56:34 +0200 | |
changeset 9173 | 422968aeed49 |
parent 8818 | 253dad743f00 |
child 9329 | d2655dc8a4b4 |
permissions | -rw-r--r-- |
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 de-HOLification: 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 de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
9 |
structure Prim : TFL_sig = |
2112 | 10 |
struct |
11 |
||
6498 | 12 |
val trace = ref false; |
13 |
||
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
14 |
open BasisLibrary; (*restore original structures*) |
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
15 |
|
2112 | 16 |
(* Abbreviations *) |
17 |
structure R = Rules; |
|
18 |
structure S = USyntax; |
|
19 |
structure U = S.Utils; |
|
20 |
||
7262 | 21 |
fun TFL_ERR{func,mesg} = U.ERR{module = "Tfl", func = func, mesg = mesg}; |
22 |
||
2112 | 23 |
val concl = #2 o R.dest_thm; |
24 |
val hyp = #1 o R.dest_thm; |
|
25 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
26 |
val list_mk_type = U.end_itlist (curry(op -->)); |
2112 | 27 |
|
8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset
|
28 |
fun enumerate xs = ListPair.zip(xs, 0 upto (length xs - 1)); |
2112 | 29 |
|
7262 | 30 |
fun front_last [] = raise TFL_ERR {func="front_last", mesg="empty list"} |
31 |
| front_last [x] = ([],x) |
|
32 |
| front_last (h::t) = |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
33 |
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
|
34 |
in |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
35 |
(h::pref,x) |
7262 | 36 |
end; |
2112 | 37 |
|
38 |
||
39 |
||
40 |
(*--------------------------------------------------------------------------- |
|
6498 | 41 |
handling of user-supplied congruence rules: lcp*) |
42 |
||
43 |
(*Convert conclusion from = to ==*) |
|
44 |
val eq_reflect_list = map (fn th => (th RS eq_reflection) handle _ => th); |
|
45 |
||
46 |
(*default congruence rules include those for LET and IF*) |
|
47 |
val default_congs = eq_reflect_list [Thms.LET_CONG, if_cong]; |
|
48 |
||
49 |
fun congs ths = default_congs @ eq_reflect_list ths; |
|
50 |
||
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
51 |
val default_simps = |
6498 | 52 |
[less_Suc_eq RS iffD2, lex_prod_def, measure_def, inv_image_def]; |
53 |
||
54 |
||
55 |
||
56 |
(*--------------------------------------------------------------------------- |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
57 |
* The next function is common to pattern-match translation and |
2112 | 58 |
* proof of completeness of cases for the induction theorem. |
59 |
* |
|
3301
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset
|
60 |
* The curried function "gvvariant" returns a function to generate distinct |
3405 | 61 |
* 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
|
62 |
* 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
|
63 |
* function contains embedded refs! |
2112 | 64 |
*---------------------------------------------------------------------------*) |
3405 | 65 |
fun gvvariant names = |
66 |
let val slist = ref names |
|
3301
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset
|
67 |
val vname = ref "u" |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
68 |
fun new() = |
3301
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset
|
69 |
if !vname mem_string (!slist) |
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset
|
70 |
then (vname := bump_string (!vname); new()) |
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset
|
71 |
else (slist := !vname :: !slist; !vname) |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
72 |
in |
3301
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset
|
73 |
fn ty => Free(new(), ty) |
cdcc4d5602b6
Now the recdef induction rule variables are named u, v, ...
paulson
parents:
3245
diff
changeset
|
74 |
end; |
2112 | 75 |
|
76 |
||
77 |
(*--------------------------------------------------------------------------- |
|
78 |
* Used in induction theorem production. This is the simple case of |
|
79 |
* partitioning up pattern rows by the leading constructor. |
|
80 |
*---------------------------------------------------------------------------*) |
|
81 |
fun ipartition gv (constructors,rows) = |
|
82 |
let fun pfail s = raise TFL_ERR{func = "partition.part", mesg = s} |
|
83 |
fun part {constrs = [], rows = [], A} = rev A |
|
84 |
| part {constrs = [], rows = _::_, A} = pfail"extra cases in defn" |
|
85 |
| part {constrs = _::_, rows = [], A} = pfail"cases missing in defn" |
|
86 |
| part {constrs = c::crst, rows, A} = |
|
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
87 |
let val (Name,Ty) = dest_Const c |
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
88 |
val L = binder_types Ty |
2112 | 89 |
val (in_group, not_in_group) = |
90 |
U.itlist (fn (row as (p::rst, rhs)) => |
|
91 |
fn (in_group,not_in_group) => |
|
92 |
let val (pc,args) = S.strip_comb p |
|
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
93 |
in if (#1(dest_Const pc) = Name) |
2112 | 94 |
then ((args@rst, rhs)::in_group, not_in_group) |
95 |
else (in_group, row::not_in_group) |
|
96 |
end) rows ([],[]) |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
97 |
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
|
98 |
in |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
99 |
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
|
100 |
A = {constructor = c, |
2112 | 101 |
new_formals = map gv col_types, |
102 |
group = in_group}::A} |
|
103 |
end |
|
104 |
in part{constrs = constructors, rows = rows, A = []} |
|
105 |
end; |
|
106 |
||
107 |
||
108 |
||
109 |
(*--------------------------------------------------------------------------- |
|
8631 | 110 |
* Each pattern carries with it a tag (i,b) where |
111 |
* i is the clause it came from and |
|
112 |
* b=true indicates that clause was given by the user |
|
113 |
* (or is an instantiation of a user supplied pattern) |
|
114 |
* b=false --> i = ~1 |
|
2112 | 115 |
*---------------------------------------------------------------------------*) |
8631 | 116 |
|
8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset
|
117 |
type pattern = term * (int * bool) |
2112 | 118 |
|
8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset
|
119 |
fun pattern_map f (tm,x) = (f tm, x); |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
120 |
|
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
121 |
fun pattern_subst theta = pattern_map (subst_free theta); |
8631 | 122 |
|
8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset
|
123 |
val pat_of = fst; |
8642 | 124 |
fun row_of_pat x = fst (snd x); |
125 |
fun given x = snd (snd x); |
|
2112 | 126 |
|
127 |
(*--------------------------------------------------------------------------- |
|
128 |
* Produce an instance of a constructor, plus genvars for its arguments. |
|
129 |
*---------------------------------------------------------------------------*) |
|
130 |
fun fresh_constr ty_match colty gv c = |
|
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
131 |
let val (_,Ty) = dest_Const c |
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
132 |
val L = binder_types Ty |
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
133 |
and ty = body_type Ty |
2112 | 134 |
val ty_theta = ty_match ty colty |
135 |
val c' = S.inst ty_theta c |
|
136 |
val gvars = map (S.inst ty_theta o gv) L |
|
137 |
in (c', gvars) |
|
138 |
end; |
|
139 |
||
140 |
||
141 |
(*--------------------------------------------------------------------------- |
|
142 |
* Goes through a list of rows and picks out the ones beginning with a |
|
143 |
* pattern with constructor = Name. |
|
144 |
*---------------------------------------------------------------------------*) |
|
145 |
fun mk_group Name rows = |
|
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
146 |
U.itlist (fn (row as ((prfx, p::rst), rhs)) => |
2112 | 147 |
fn (in_group,not_in_group) => |
148 |
let val (pc,args) = S.strip_comb p |
|
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
149 |
in if ((#1(dest_Const pc) = Name) handle _ => false) |
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
150 |
then (((prfx,args@rst), rhs)::in_group, not_in_group) |
2112 | 151 |
else (in_group, row::not_in_group) end) |
152 |
rows ([],[]); |
|
153 |
||
154 |
(*--------------------------------------------------------------------------- |
|
155 |
* Partition the rows. Not efficient: we should use hashing. |
|
156 |
*---------------------------------------------------------------------------*) |
|
157 |
fun partition _ _ (_,_,_,[]) = raise TFL_ERR{func="partition", mesg="no rows"} |
|
158 |
| partition gv ty_match |
|
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
159 |
(constructors, colty, res_ty, rows as (((prfx,_),_)::_)) = |
2112 | 160 |
let val fresh = fresh_constr ty_match colty gv |
161 |
fun part {constrs = [], rows, A} = rev A |
|
162 |
| part {constrs = c::crst, rows, A} = |
|
163 |
let val (c',gvars) = fresh c |
|
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
164 |
val (Name,Ty) = dest_Const c' |
2112 | 165 |
val (in_group, not_in_group) = mk_group Name rows |
166 |
val in_group' = |
|
167 |
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
|
168 |
then [((prfx, #2(fresh c)), (S.ARB res_ty, (~1,false)))] |
2112 | 169 |
else in_group |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
170 |
in |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
171 |
part{constrs = crst, |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
172 |
rows = not_in_group, |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
173 |
A = {constructor = c', |
2112 | 174 |
new_formals = gvars, |
175 |
group = in_group'}::A} |
|
176 |
end |
|
177 |
in part{constrs=constructors, rows=rows, A=[]} |
|
178 |
end; |
|
179 |
||
180 |
(*--------------------------------------------------------------------------- |
|
181 |
* Misc. routines used in mk_case |
|
182 |
*---------------------------------------------------------------------------*) |
|
183 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
184 |
fun mk_pat (c,l) = |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
185 |
let val L = length (binder_types (type_of c)) |
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
186 |
fun build (prfx,tag,plist) = |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
187 |
let val args = take (L,plist) |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
188 |
and plist' = drop(L,plist) |
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
189 |
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
|
190 |
in map build l end; |
2112 | 191 |
|
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
192 |
fun v_to_prfx (prfx, v::pats) = (v::prfx,pats) |
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
193 |
| v_to_prfx _ = raise TFL_ERR{func="mk_case", mesg="v_to_prfx"}; |
2112 | 194 |
|
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
195 |
fun v_to_pats (v::prfx,tag, pats) = (prfx, tag, v::pats) |
2112 | 196 |
| 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
|
197 |
|
2112 | 198 |
|
199 |
(*---------------------------------------------------------------------------- |
|
200 |
* Translation of pattern terms into nested case expressions. |
|
201 |
* |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
202 |
* 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
|
203 |
* Thus it supports the construction of induction theorems even when an |
2112 | 204 |
* incomplete set of patterns is given. |
205 |
*---------------------------------------------------------------------------*) |
|
206 |
||
3405 | 207 |
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
|
208 |
let |
2112 | 209 |
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
|
210 |
val fresh_var = gvvariant usednames |
2112 | 211 |
val divide = partition fresh_var ty_match |
212 |
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
|
213 |
| 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
|
214 |
if (is_Free p) |
2112 | 215 |
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
|
216 |
fun expnd (c,gvs) = |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
217 |
let val capp = list_comb(c,gvs) |
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
218 |
in ((prfx, capp::rst), pattern_subst[(p,capp)] rhs) |
2112 | 219 |
end |
220 |
in map expnd (map fresh constructors) end |
|
221 |
else [row] |
|
222 |
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
|
223 |
| 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
|
224 |
([(prfx,tag,[])], tm) |
2112 | 225 |
| 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
|
226 |
| 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
|
227 |
mk{path = path, |
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
228 |
rows = ((prfx, [fresh_var(type_of u)]), rhs)::rst} |
2112 | 229 |
| mk{path = u::rstp, rows as ((_, p::_), _)::_} = |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
230 |
let val (pat_rectangle,rights) = ListPair.unzip rows |
2112 | 231 |
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
|
232 |
in |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
233 |
if (forall is_Free col0) |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
234 |
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
|
235 |
(ListPair.zip (col0, rights)) |
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
236 |
val pat_rectangle' = map v_to_prfx pat_rectangle |
2112 | 237 |
val (pref_patl,tm) = mk{path = rstp, |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
238 |
rows = ListPair.zip (pat_rectangle', |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
239 |
rights')} |
2112 | 240 |
in (map v_to_pats pref_patl, tm) |
241 |
end |
|
242 |
else |
|
3944 | 243 |
let val pty as Type (ty_name,_) = type_of p |
2112 | 244 |
in |
245 |
case (ty_info ty_name) |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
246 |
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
|
247 |
| Some{case_const,constructors} => |
7697
cdaf7f18856d
renamed 'prefix' to 'prfx' (avoids clash with infix);
wenzelm
parents:
7286
diff
changeset
|
248 |
let |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
249 |
val case_const_name = #1(dest_Const case_const) |
4062 | 250 |
val nrows = List.concat (map (expand constructors pty) rows) |
2112 | 251 |
val subproblems = divide(constructors, pty, range_ty, nrows) |
252 |
val groups = map #group subproblems |
|
253 |
and new_formals = map #new_formals subproblems |
|
254 |
and constructors' = map #constructor subproblems |
|
255 |
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
|
256 |
(ListPair.zip (new_formals, groups)) |
2112 | 257 |
val rec_calls = map mk news |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
258 |
val (pat_rect,dtrees) = ListPair.unzip rec_calls |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
259 |
val case_functions = map S.list_mk_abs |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
260 |
(ListPair.zip (new_formals, dtrees)) |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
261 |
val types = map type_of (case_functions@[u]) @ [range_ty] |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
262 |
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
|
263 |
val tree = list_comb(case_const', case_functions@[u]) |
4062 | 264 |
val pat_rect1 = List.concat |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
265 |
(ListPair.map mk_pat (constructors', pat_rect)) |
2112 | 266 |
in (pat_rect1,tree) |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
267 |
end |
2112 | 268 |
end end |
269 |
in mk |
|
270 |
end; |
|
271 |
||
272 |
||
273 |
(* 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
|
274 |
fun FV_multiset tm = |
2112 | 275 |
case (S.dest_term tm) |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
276 |
of S.VAR{Name,Ty} => [Free(Name,Ty)] |
2112 | 277 |
| S.CONST _ => [] |
278 |
| S.COMB{Rator, Rand} => FV_multiset Rator @ FV_multiset Rand |
|
279 |
| S.LAMB _ => raise TFL_ERR{func = "FV_multiset", mesg = "lambda"}; |
|
280 |
||
281 |
fun no_repeat_vars thy pat = |
|
282 |
let fun check [] = true |
|
283 |
| check (v::rst) = |
|
3333
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset
|
284 |
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
|
285 |
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
|
286 |
mesg = quote(#1(dest_Free v)) ^ |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
287 |
" occurs repeatedly in the pattern " ^ |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
288 |
quote (string_of_cterm (Thry.typecheck thy pat))} |
2112 | 289 |
else check rst |
290 |
in check (FV_multiset pat) |
|
291 |
end; |
|
292 |
||
7262 | 293 |
fun dest_atom (Free p) = p |
294 |
| dest_atom (Const p) = p |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
295 |
| 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
|
296 |
mesg="function name not an identifier"}; |
7262 | 297 |
|
8492
6343c725ba7e
better error messages, especially for multiple types
paulson
parents:
8438
diff
changeset
|
298 |
fun same_name (p,q) = #1(dest_atom p) = #1(dest_atom q); |
7262 | 299 |
|
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
300 |
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
|
301 |
fun single [_$_] = |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
302 |
mk_functional_err "recdef does not allow currying" |
7262 | 303 |
| single [f] = f |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
304 |
| single fs = |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
305 |
(*multiple function names?*) |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
306 |
if length (gen_distinct same_name fs) < length fs |
8492
6343c725ba7e
better error messages, especially for multiple types
paulson
parents:
8438
diff
changeset
|
307 |
then mk_functional_err |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
308 |
"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
|
309 |
else mk_functional_err |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
310 |
(Int.toString (length fs) ^ |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
311 |
" distinct function names being declared") |
2112 | 312 |
in |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
313 |
fun mk_functional thy clauses = |
6566 | 314 |
let val (L,R) = ListPair.unzip (map HOLogic.dest_eq clauses) |
315 |
handle _ => raise TFL_ERR |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
316 |
{func = "mk_functional", |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
317 |
mesg = "recursion equations must use the = relation"} |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
318 |
val (funcs,pats) = ListPair.unzip (map (fn (t$u) =>(t,u)) L) |
7262 | 319 |
val atom = single (gen_distinct (op aconv) funcs) |
320 |
val (fname,ftype) = dest_atom atom |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
321 |
val dummy = map (no_repeat_vars thy) pats |
7262 | 322 |
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
|
323 |
map (fn (t,i) => (t,(i,true))) (enumerate R)) |
3405 | 324 |
val names = foldr add_term_names (R,[]) |
325 |
val atype = type_of(hd pats) |
|
326 |
and aname = variant names "a" |
|
327 |
val a = Free(aname,atype) |
|
2112 | 328 |
val ty_info = Thry.match_info thy |
329 |
val ty_match = Thry.match_type thy |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
330 |
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
|
331 |
val (patts, case_tm) = mk_case ty_info ty_match (aname::names) range_ty |
2112 | 332 |
{path=[a], rows=rows} |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
333 |
val patts1 = map (fn (_,tag,[pat]) => (pat,tag)) patts |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
334 |
handle _ => mk_functional_err "error in pattern-match translation" |
2112 | 335 |
val patts2 = U.sort(fn p1=>fn p2=> row_of_pat p1 < row_of_pat p2) patts1 |
336 |
val finals = map row_of_pat patts2 |
|
337 |
val originals = map (row_of_pat o #2) rows |
|
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
338 |
val dummy = case (originals\\finals) |
2112 | 339 |
of [] => () |
8631 | 340 |
| L => mk_functional_err |
341 |
("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
|
342 |
commas (map (fn i => Int.toString (i + 1)) L)) |
3944 | 343 |
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
|
344 |
abstract_over (atom, |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
345 |
absfree(aname,atype, case_tm))), |
2112 | 346 |
pats = patts2} |
347 |
end end; |
|
348 |
||
349 |
||
350 |
(*---------------------------------------------------------------------------- |
|
351 |
* |
|
352 |
* PRINCIPLES OF DEFINITION |
|
353 |
* |
|
354 |
*---------------------------------------------------------------------------*) |
|
355 |
||
356 |
||
6498 | 357 |
(*For Isabelle, the lhs of a definition must be a constant.*) |
358 |
fun mk_const_def sign (Name, Ty, rhs) = |
|
359 |
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
|
360 |
([Const("==",dummyT) $ Const(Name,Ty) $ rhs], propT) |
6498 | 361 |
|> #1; |
362 |
||
3387
6f2eaa0ce04b
poly_tvars allows recdefs to be made without type constraints
paulson
parents:
3379
diff
changeset
|
363 |
(*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
|
364 |
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
|
365 |
| 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
|
366 |
| 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
|
367 |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
368 |
local val f_eq_wfrec_R_M = |
3191 | 369 |
#ant(S.dest_imp(#2(S.strip_forall (concl Thms.WFREC_COROLLARY)))) |
370 |
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
|
371 |
val (fname,_) = dest_Free f |
3191 | 372 |
val (wfrec,_) = S.strip_comb rhs |
373 |
in |
|
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
374 |
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
|
375 |
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
|
376 |
raise TFL_ERR{func = "wfrec_definition0", |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
377 |
mesg = "Expected a definition of " ^ |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
378 |
quote fid ^ " but found one of " ^ |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
379 |
quote Name} |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
380 |
else Name ^ "_def" |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
381 |
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
|
382 |
(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
|
383 |
$ functional |
6498 | 384 |
val def_term = mk_const_def (Theory.sign_of thy) (Name, Ty, wfrec_R_M) |
8438 | 385 |
in #1 (PureThy.add_defs_i [Thm.no_attributes (def_name, def_term)] thy) end |
3191 | 386 |
end; |
2112 | 387 |
|
388 |
||
389 |
||
390 |
(*--------------------------------------------------------------------------- |
|
391 |
* This structure keeps track of congruence rules that aren't derived |
|
392 |
* from a datatype definition. |
|
393 |
*---------------------------------------------------------------------------*) |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
394 |
fun extraction_thms thy = |
2112 | 395 |
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
|
396 |
in (case_rewrites, case_congs) |
2112 | 397 |
end; |
398 |
||
399 |
||
400 |
(*--------------------------------------------------------------------------- |
|
401 |
* Pair patterns with termination conditions. The full list of patterns for |
|
402 |
* a definition is merged with the TCs arising from the user-given clauses. |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
403 |
* There can be fewer clauses than the full list, if the user omitted some |
2112 | 404 |
* cases. This routine is used to prepare input for mk_induction. |
405 |
*---------------------------------------------------------------------------*) |
|
406 |
fun merge full_pats TCs = |
|
407 |
let fun insert (p,TCs) = |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
408 |
let fun insrt ((x as (h,[]))::rst) = |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
409 |
if (p aconv h) then (p,TCs)::rst else x::insrt rst |
2112 | 410 |
| insrt (x::rst) = x::insrt rst |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
411 |
| 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
|
412 |
mesg="pattern not found"} |
2112 | 413 |
in insrt end |
414 |
fun pass ([],ptcl_final) = ptcl_final |
|
415 |
| 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
|
416 |
in |
2112 | 417 |
pass (TCs, map (fn p => (p,[])) full_pats) |
418 |
end; |
|
419 |
||
420 |
||
8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset
|
421 |
fun givens pats = map pat_of (filter given pats); |
2112 | 422 |
|
6498 | 423 |
(*called only by Tfl.simplify_defn*) |
424 |
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
|
425 |
let val tych = Thry.typecheck theory |
3191 | 426 |
val f = #lhs(S.dest_eq(concl def)) |
427 |
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
|
428 |
val pats' = filter given pats |
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset
|
429 |
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
|
430 |
val rows = map row_of_pat pats' |
2112 | 431 |
val WFR = #ant(S.dest_imp(concl corollary)) |
3191 | 432 |
val R = #Rand(S.dest_comb WFR) |
2112 | 433 |
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
|
434 |
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
|
435 |
given_pats |
3191 | 436 |
val (case_rewrites,context_congs) = extraction_thms theory |
3405 | 437 |
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
|
438 |
val extract = R.CONTEXT_REWRITE_RULE |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
439 |
(f, [R], cut_apply, meta_tflCongs@context_congs) |
3405 | 440 |
val (rules, TCs) = ListPair.unzip (map extract corollaries') |
441 |
val rules0 = map (rewrite_rule [Thms.CUT_DEF]) rules |
|
442 |
val mk_cond_rule = R.FILTER_DISCH_ALL(not o curry (op aconv) WFR) |
|
2112 | 443 |
val rules1 = R.LIST_CONJ(map mk_cond_rule rules0) |
444 |
in |
|
445 |
{theory = theory, (* holds def, if it's needed *) |
|
446 |
rules = rules1, |
|
8622
870a58dd0ddd
the simplification rules returned from TFL are now paired with the row they
nipkow
parents:
8492
diff
changeset
|
447 |
rows = rows, |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
448 |
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
|
449 |
TCs = TCs} |
2112 | 450 |
end; |
451 |
||
6498 | 452 |
|
2112 | 453 |
(*--------------------------------------------------------------------------- |
454 |
* Perform the extraction without making the definition. Definition and |
|
6498 | 455 |
* extraction commute for the non-nested case. (Deferred recdefs) |
7262 | 456 |
* |
457 |
* The purpose of wfrec_eqns is merely to instantiate the recursion theorem |
|
458 |
* and extract termination conditions: no definition is made. |
|
6498 | 459 |
*---------------------------------------------------------------------------*) |
7262 | 460 |
|
6498 | 461 |
fun wfrec_eqns thy fid tflCongs eqns = |
7262 | 462 |
let val {lhs,rhs} = S.dest_eq (hd eqns) |
463 |
val (f,args) = S.strip_comb lhs |
|
464 |
val (fname,fty) = dest_atom f |
|
465 |
val (SV,a) = front_last args (* SV = schematic variables *) |
|
466 |
val g = list_comb(f,SV) |
|
467 |
val h = Free(fname,type_of g) |
|
468 |
val eqns1 = map (subst_free[(g,h)]) eqns |
|
469 |
val {functional as Abs(Name, Ty, _), pats} = mk_functional thy eqns1 |
|
2112 | 470 |
val given_pats = givens pats |
6498 | 471 |
(* val f = Free(Name,Ty) *) |
472 |
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
|
473 |
val dummy = if Name<>fid then |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
474 |
raise TFL_ERR{func = "wfrec_eqns", |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
475 |
mesg = "Expected a definition of " ^ |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
476 |
quote fid ^ " but found one of " ^ |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
477 |
quote Name} |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
478 |
else () |
2112 | 479 |
val (case_rewrites,context_congs) = extraction_thms thy |
480 |
val tych = Thry.typecheck thy |
|
481 |
val WFREC_THM0 = R.ISPEC (tych functional) Thms.WFREC_COROLLARY |
|
3405 | 482 |
val Const("All",_) $ Abs(Rname,Rtype,_) = concl WFREC_THM0 |
483 |
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
|
484 |
Rtype) |
7262 | 485 |
val WFREC_THM = R.ISPECL [tych R, tych g] WFREC_THM0 |
2112 | 486 |
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
|
487 |
val dummy = |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
488 |
if !trace then |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
489 |
writeln ("ORIGINAL PROTO_DEF: " ^ |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
490 |
Sign.string_of_term (Theory.sign_of thy) proto_def) |
6498 | 491 |
else () |
2112 | 492 |
val R1 = S.rand WFR |
493 |
val corollary' = R.UNDISCH(R.UNDISCH WFREC_THM) |
|
3405 | 494 |
val corollaries = map (fn pat => R.SPEC (tych pat) corollary') given_pats |
495 |
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
|
496 |
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
|
497 |
(f, R1::SV, cut_apply, tflCongs@context_congs) X |
7262 | 498 |
in {proto_def = proto_def, |
6498 | 499 |
SV=SV, |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
500 |
WFR=WFR, |
2112 | 501 |
pats=pats, |
502 |
extracta = map extract corollaries'} |
|
503 |
end; |
|
504 |
||
505 |
||
506 |
(*--------------------------------------------------------------------------- |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
507 |
* Define the constant after extracting the termination conditions. The |
2112 | 508 |
* wellfounded relation used in the definition is computed by using the |
509 |
* choice operator on the extracted conditions (plus the condition that |
|
510 |
* such a relation must be wellfounded). |
|
6498 | 511 |
*---------------------------------------------------------------------------*) |
7262 | 512 |
|
6498 | 513 |
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
|
514 |
let val {proto_def,WFR,pats,extracta,SV} = |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
515 |
wfrec_eqns thy fid (congs tflCongs) eqns |
2112 | 516 |
val R1 = S.rand WFR |
7262 | 517 |
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
|
518 |
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
|
519 |
val dummy = if !trace |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
520 |
then (writeln "Extractants = "; |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
521 |
prths extractants; |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
522 |
()) |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
523 |
else () |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
524 |
val TCs = foldr (gen_union (op aconv)) (TCl, []) |
2112 | 525 |
val full_rqt = WFR::TCs |
526 |
val R' = S.mk_select{Bvar=R1, Body=S.list_mk_conj full_rqt} |
|
527 |
val R'abs = S.rand R' |
|
6498 | 528 |
val proto_def' = subst_free[(R1,R')] proto_def |
529 |
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
|
530 |
Sign.string_of_term |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
531 |
(Theory.sign_of thy) proto_def') |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
532 |
else () |
7262 | 533 |
val {lhs,rhs} = S.dest_eq proto_def' |
534 |
val (c,args) = S.strip_comb lhs |
|
535 |
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
|
536 |
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
|
537 |
(Name, Ty, S.list_mk_abs (args,rhs)) |
8438 | 538 |
val (theory, [def0]) = |
7262 | 539 |
thy |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
540 |
|> PureThy.add_defs_i |
7262 | 541 |
[Thm.no_attributes (fid ^ "_def", defn)] |
8438 | 542 |
val def = freezeT def0; |
6498 | 543 |
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
|
544 |
else () |
7262 | 545 |
(* val fconst = #lhs(S.dest_eq(concl def)) *) |
2112 | 546 |
val tych = Thry.typecheck theory |
6498 | 547 |
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
|
548 |
(*lcp: a lot of object-logic inference to remove*) |
6498 | 549 |
val baz = R.DISCH_ALL |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
550 |
(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
|
551 |
(R.LIST_CONJ extractants)) |
6498 | 552 |
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
|
553 |
else () |
6498 | 554 |
val f_free = Free (fid, fastype_of f) (*'cos f is a Const*) |
7262 | 555 |
val SV' = map tych SV; |
556 |
val SVrefls = map reflexive SV' |
|
557 |
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
|
558 |
SVrefls def) |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
559 |
RS meta_eq_to_obj_eq |
7262 | 560 |
val def' = R.MP (R.SPEC (tych R') (R.GEN (tych R1) baz)) def0 |
6498 | 561 |
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
|
562 |
val bar = R.MP (R.ISPECL[tych R'abs, tych R1] Thms.SELECT_AX) |
3191 | 563 |
body_th |
6498 | 564 |
in {theory = theory, R=R1, SV=SV, |
2112 | 565 |
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
|
566 |
full_pats_TCs = merge (map pat_of pats) (ListPair.zip (givens pats, TCl)), |
2112 | 567 |
patterns = pats} |
568 |
end; |
|
569 |
||
570 |
||
571 |
||
572 |
(*---------------------------------------------------------------------------- |
|
573 |
* |
|
574 |
* INDUCTION THEOREM |
|
575 |
* |
|
576 |
*---------------------------------------------------------------------------*) |
|
577 |
||
578 |
||
579 |
(*------------------------ Miscellaneous function -------------------------- |
|
580 |
* |
|
581 |
* [x_1,...,x_n] ?v_1...v_n. M[v_1,...,v_n] |
|
582 |
* ----------------------------------------------------------- |
|
583 |
* ( 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
|
584 |
* ... |
2112 | 585 |
* (x_j,?v_n. M[x_1,...,x_(n-1),v_n])] ) |
586 |
* |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
587 |
* This function is totally ad hoc. Used in the production of the induction |
2112 | 588 |
* theorem. The nchotomy theorem can have clauses that look like |
589 |
* |
|
590 |
* ?v1..vn. z = C vn..v1 |
|
591 |
* |
|
592 |
* in which the order of quantification is not the order of occurrence of the |
|
593 |
* quantified variables as arguments to C. Since we have no control over this |
|
594 |
* aspect of the nchotomy theorem, we make the correspondence explicit by |
|
595 |
* pairing the incoming new variable with the term it gets beta-reduced into. |
|
596 |
*---------------------------------------------------------------------------*) |
|
597 |
||
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
598 |
fun alpha_ex_unroll (xlist, tm) = |
2112 | 599 |
let val (qvars,body) = S.strip_exists tm |
600 |
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
|
601 |
val plist = ListPair.zip (vlist, xlist) |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
602 |
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
|
603 |
handle OPTION => error |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
604 |
"TFL fault [alpha_ex_unroll]: no correspondence" |
3405 | 605 |
fun build ex [] = [] |
606 |
| build (_$rex) (v::rst) = |
|
607 |
let val ex1 = betapply(rex, v) |
|
608 |
in ex1 :: build ex1 rst |
|
2112 | 609 |
end |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
610 |
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
|
611 |
in |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
612 |
(nex, ListPair.zip (args, rev exl)) |
2112 | 613 |
end; |
614 |
||
615 |
||
616 |
||
617 |
(*---------------------------------------------------------------------------- |
|
618 |
* |
|
619 |
* PROVING COMPLETENESS OF PATTERNS |
|
620 |
* |
|
621 |
*---------------------------------------------------------------------------*) |
|
622 |
||
3405 | 623 |
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
|
624 |
let |
3405 | 625 |
val divide = ipartition (gvvariant usednames) |
2112 | 626 |
val tych = Thry.typecheck thy |
3353
9112a2efb9a3
Removal of module Mask and datatype binding with its constructor |->
paulson
parents:
3333
diff
changeset
|
627 |
fun tych_binding(x,y) = (tych x, tych y) |
2112 | 628 |
fun fail s = raise TFL_ERR{func = "mk_case", mesg = s} |
629 |
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
|
630 |
| mk{path=[], rows = [([], (thm, bindings))]} = |
2112 | 631 |
R.IT_EXISTS (map tych_binding bindings) thm |
632 |
| mk{path = u::rstp, rows as (p::_, _)::_} = |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
633 |
let val (pat_rectangle,rights) = ListPair.unzip rows |
2112 | 634 |
val col0 = map hd pat_rectangle |
635 |
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
|
636 |
in |
3333
0bbf06e86c06
Now checks the name of the function being defined;
paulson
parents:
3301
diff
changeset
|
637 |
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
|
638 |
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
|
639 |
(ListPair.zip (rights, col0)) |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
640 |
in mk{path = rstp, rows = ListPair.zip (pat_rectangle', rights')} |
2112 | 641 |
end |
642 |
else (* column 0 is all constructors *) |
|
3944 | 643 |
let val Type (ty_name,_) = type_of p |
2112 | 644 |
in |
645 |
case (ty_info ty_name) |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
646 |
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
|
647 |
| Some{constructors,nchotomy} => |
2112 | 648 |
let val thm' = R.ISPEC (tych u) nchotomy |
649 |
val disjuncts = S.strip_disj (concl thm') |
|
650 |
val subproblems = divide(constructors, rows) |
|
651 |
val groups = map #group subproblems |
|
652 |
and new_formals = map #new_formals subproblems |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
653 |
val existentials = ListPair.map alpha_ex_unroll |
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
654 |
(new_formals, disjuncts) |
2112 | 655 |
val constraints = map #1 existentials |
656 |
val vexl = map #2 existentials |
|
657 |
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
|
658 |
val news = map (fn (nf,rows,c) => {path = nf@rstp, |
2112 | 659 |
rows = map (expnd c) rows}) |
660 |
(U.zip3 new_formals groups constraints) |
|
661 |
val recursive_thms = map mk news |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
662 |
val build_exists = foldr |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
663 |
(fn((x,t), th) => |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
664 |
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
|
665 |
val thms' = ListPair.map build_exists (vexl, recursive_thms) |
2112 | 666 |
val same_concls = R.EVEN_ORS thms' |
667 |
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
|
668 |
end |
2112 | 669 |
end end |
670 |
in mk |
|
671 |
end; |
|
672 |
||
673 |
||
674 |
fun complete_cases thy = |
|
675 |
let val tych = Thry.typecheck thy |
|
676 |
val ty_info = Thry.induct_info thy |
|
677 |
in fn pats => |
|
3405 | 678 |
let val names = foldr add_term_names (pats,[]) |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
679 |
val T = type_of (hd pats) |
3405 | 680 |
val aname = Term.variant names "a" |
681 |
val vname = Term.variant (aname::names) "v" |
|
682 |
val a = Free (aname, T) |
|
683 |
val v = Free (vname, T) |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
684 |
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
|
685 |
val ex_th0 = R.EXISTS (tych (S.mk_exists{Bvar=v,Body=a_eq_v}), tych a) |
2112 | 686 |
(R.REFL (tych a)) |
687 |
val th0 = R.ASSUME (tych a_eq_v) |
|
688 |
val rows = map (fn x => ([x], (th0,[]))) pats |
|
689 |
in |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
690 |
R.GEN (tych a) |
2112 | 691 |
(R.RIGHT_ASSOC |
692 |
(R.CHOOSE(tych v, ex_th0) |
|
3405 | 693 |
(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
|
694 |
thy {path=[v], rows=rows}))) |
2112 | 695 |
end end; |
696 |
||
697 |
||
698 |
(*--------------------------------------------------------------------------- |
|
699 |
* Constructing induction hypotheses: one for each recursive call. |
|
700 |
* |
|
701 |
* Note. R will never occur as a variable in the ind_clause, because |
|
702 |
* to do so, it would have to be from a nested definition, and we don't |
|
703 |
* allow nested defns to have R variable. |
|
704 |
* |
|
705 |
* Note. When the context is empty, there can be no local variables. |
|
706 |
*---------------------------------------------------------------------------*) |
|
6498 | 707 |
(* |
3405 | 708 |
local infix 5 ==> |
2112 | 709 |
fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2} |
710 |
in |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
711 |
fun build_ih f P (pat,TCs) = |
2112 | 712 |
let val globals = S.free_vars_lr pat |
3405 | 713 |
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
|
714 |
fun dest_TC tm = |
2112 | 715 |
let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm)) |
716 |
val (R,y,_) = S.dest_relation R_y_pat |
|
3405 | 717 |
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
|
718 |
in case cntxt |
2112 | 719 |
of [] => (P_y, (tm,[])) |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
720 |
| _ => let |
2112 | 721 |
val imp = S.list_mk_conj cntxt ==> P_y |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
722 |
val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals) |
3405 | 723 |
val locals = #2(U.pluck (curry (op aconv) P) lvs) handle _ => lvs |
2112 | 724 |
in (S.list_mk_forall(locals,imp), (tm,locals)) end |
725 |
end |
|
726 |
in case TCs |
|
3405 | 727 |
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
|
728 |
| _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs) |
3405 | 729 |
val ind_clause = S.list_mk_conj ihs ==> P$pat |
2112 | 730 |
in (S.list_mk_forall(globals,ind_clause), TCs_locals) |
731 |
end |
|
732 |
end |
|
733 |
end; |
|
6498 | 734 |
*) |
2112 | 735 |
|
6498 | 736 |
local infix 5 ==> |
737 |
fun (tm1 ==> tm2) = S.mk_imp{ant = tm1, conseq = tm2} |
|
738 |
in |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
739 |
fun build_ih f (P,SV) (pat,TCs) = |
6498 | 740 |
let val pat_vars = S.free_vars_lr pat |
741 |
val globals = pat_vars@SV |
|
742 |
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
|
743 |
fun dest_TC tm = |
6498 | 744 |
let val (cntxt,R_y_pat) = S.strip_imp(#2(S.strip_forall tm)) |
745 |
val (R,y,_) = S.dest_relation R_y_pat |
|
746 |
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
|
747 |
in case cntxt |
6498 | 748 |
of [] => (P_y, (tm,[])) |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
749 |
| _ => let |
6498 | 750 |
val imp = S.list_mk_conj cntxt ==> P_y |
751 |
val lvs = gen_rems (op aconv) (S.free_vars_lr imp, globals) |
|
752 |
val locals = #2(U.pluck (curry (op aconv) P) lvs) handle _ => lvs |
|
753 |
in (S.list_mk_forall(locals,imp), (tm,locals)) end |
|
754 |
end |
|
755 |
in case TCs |
|
756 |
of [] => (S.list_mk_forall(pat_vars, P$pat), []) |
|
757 |
| _ => let val (ihs, TCs_locals) = ListPair.unzip(map dest_TC TCs) |
|
758 |
val ind_clause = S.list_mk_conj ihs ==> P$pat |
|
759 |
in (S.list_mk_forall(pat_vars,ind_clause), TCs_locals) |
|
760 |
end |
|
761 |
end |
|
762 |
end; |
|
2112 | 763 |
|
764 |
(*--------------------------------------------------------------------------- |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
765 |
* This function makes good on the promise made in "build_ih". |
2112 | 766 |
* |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
767 |
* Input is tm = "(!y. R y pat ==> P y) ==> P pat", |
2112 | 768 |
* TCs = TC_1[pat] ... TC_n[pat] |
769 |
* thm = ih1 /\ ... /\ ih_n |- ih[pat] |
|
770 |
*---------------------------------------------------------------------------*) |
|
771 |
fun prove_case f thy (tm,TCs_locals,thm) = |
|
772 |
let val tych = Thry.typecheck thy |
|
773 |
val antc = tych(#ant(S.dest_imp tm)) |
|
774 |
val thm' = R.SPEC_ALL thm |
|
3405 | 775 |
fun nested tm = is_some (S.find_term (curry (op aconv) f) tm) |
2112 | 776 |
fun get_cntxt TC = tych(#ant(S.dest_imp(#2(S.strip_forall(concl TC))))) |
777 |
fun mk_ih ((TC,locals),th2,nested) = |
|
778 |
R.GENL (map tych locals) |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
779 |
(if nested |
2112 | 780 |
then R.DISCH (get_cntxt TC) th2 handle _ => th2 |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
781 |
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
|
782 |
then R.IMP_TRANS TC th2 |
2112 | 783 |
else R.MP th2 TC) |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
784 |
in |
2112 | 785 |
R.DISCH antc |
786 |
(if S.is_imp(concl thm') (* recursive calls in this clause *) |
|
787 |
then let val th1 = R.ASSUME antc |
|
788 |
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
|
789 |
val ylist = map (#2 o S.dest_relation o #2 o S.strip_imp o |
2112 | 790 |
#2 o S.strip_forall) TCs |
791 |
val TClist = map (fn(TC,lvs) => (R.SPEC_ALL(R.ASSUME(tych TC)),lvs)) |
|
792 |
TCs_locals |
|
793 |
val th2list = map (U.C R.SPEC th1 o tych) ylist |
|
794 |
val nlist = map nested TCs |
|
795 |
val triples = U.zip3 TClist th2list nlist |
|
796 |
val Pylist = map mk_ih triples |
|
797 |
in R.MP thm' (R.LIST_CONJ Pylist) end |
|
798 |
else thm') |
|
799 |
end; |
|
800 |
||
801 |
||
802 |
(*--------------------------------------------------------------------------- |
|
803 |
* |
|
804 |
* x = (v1,...,vn) |- M[x] |
|
805 |
* --------------------------------------------- |
|
806 |
* ?v1 ... vn. x = (v1,...,vn) |- M[x] |
|
807 |
* |
|
808 |
*---------------------------------------------------------------------------*) |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
809 |
fun LEFT_ABS_VSTRUCT tych thm = |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
810 |
let fun CHOOSER v (tm,thm) = |
2112 | 811 |
let val ex_tm = S.mk_exists{Bvar=v,Body=tm} |
812 |
in (ex_tm, R.CHOOSE(tych v, R.ASSUME (tych ex_tm)) thm) |
|
813 |
end |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
814 |
val [veq] = filter (U.can S.dest_eq) (#1 (R.dest_thm thm)) |
2112 | 815 |
val {lhs,rhs} = S.dest_eq veq |
816 |
val L = S.free_vars_lr rhs |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
817 |
in #2 (U.itlist CHOOSER L (veq,thm)) end; |
2112 | 818 |
|
819 |
||
820 |
(*---------------------------------------------------------------------------- |
|
821 |
* Input : f, R, and [(pat1,TCs1),..., (patn,TCsn)] |
|
822 |
* |
|
823 |
* 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
|
824 |
* recursion induction (Rinduct) by proving the antecedent of Sinduct from |
2112 | 825 |
* the antecedent of Rinduct. |
826 |
*---------------------------------------------------------------------------*) |
|
6498 | 827 |
fun mk_induction thy {fconst, R, SV, pat_TCs_list} = |
2112 | 828 |
let val tych = Thry.typecheck thy |
829 |
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
|
830 |
val (pats,TCsl) = ListPair.unzip pat_TCs_list |
2112 | 831 |
val case_thm = complete_cases thy pats |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
832 |
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
|
833 |
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
|
834 |
(pats::TCsl, [])) "P" |
3405 | 835 |
val P = Free(Pname, domain --> HOLogic.boolT) |
2112 | 836 |
val Sinduct = R.SPEC (tych P) Sinduction |
837 |
val Sinduct_assumf = S.rand ((#ant o S.dest_imp o concl) Sinduct) |
|
6498 | 838 |
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
|
839 |
val (Rassums,TCl') = ListPair.unzip Rassums_TCl' |
2112 | 840 |
val Rinduct_assum = R.ASSUME (tych (S.list_mk_conj Rassums)) |
3405 | 841 |
val cases = map (fn pat => betapply (Sinduct_assumf, pat)) pats |
2112 | 842 |
val tasks = U.zip3 cases TCl' (R.CONJUNCTS Rinduct_assum) |
6498 | 843 |
val proved_cases = map (prove_case fconst thy) tasks |
3405 | 844 |
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
|
845 |
"v", |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
846 |
domain) |
2112 | 847 |
val vtyped = tych v |
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
848 |
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
|
849 |
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
|
850 |
(substs, proved_cases) |
2112 | 851 |
val abs_cases = map (LEFT_ABS_VSTRUCT tych) proved_cases1 |
852 |
val dant = R.GEN vtyped (R.DISJ_CASESL (R.ISPEC vtyped case_thm) abs_cases) |
|
853 |
val dc = R.MP Sinduct dant |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
854 |
val Parg_ty = type_of(#Bvar(S.dest_forall(concl dc))) |
3405 | 855 |
val vars = map (gvvariant[Pname]) (S.strip_prod_type Parg_ty) |
2112 | 856 |
val dc' = U.itlist (R.GEN o tych) vars |
857 |
(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
|
858 |
in |
2112 | 859 |
R.GEN (tych P) (R.DISCH (tych(concl Rinduct_assum)) dc') |
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
860 |
end |
2112 | 861 |
handle _ => raise TFL_ERR{func = "mk_induction", mesg = "failed derivation"}; |
862 |
||
863 |
||
864 |
||
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
865 |
|
2112 | 866 |
(*--------------------------------------------------------------------------- |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
867 |
* |
2112 | 868 |
* POST PROCESSING |
869 |
* |
|
870 |
*---------------------------------------------------------------------------*) |
|
871 |
||
872 |
||
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
873 |
fun simplify_induction thy hth ind = |
2112 | 874 |
let val tych = Thry.typecheck thy |
875 |
val (asl,_) = R.dest_thm ind |
|
876 |
val (_,tc_eq_tc') = R.dest_thm hth |
|
877 |
val tc = S.lhs tc_eq_tc' |
|
878 |
fun loop [] = ind |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
879 |
| loop (asm::rst) = |
2112 | 880 |
if (U.can (Thry.match_term thy asm) tc) |
881 |
then R.UNDISCH |
|
882 |
(R.MATCH_MP |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
883 |
(R.MATCH_MP Thms.simp_thm (R.DISCH (tych asm) ind)) |
2112 | 884 |
hth) |
885 |
else loop rst |
|
886 |
in loop asl |
|
887 |
end; |
|
888 |
||
889 |
||
890 |
(*--------------------------------------------------------------------------- |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
891 |
* The termination condition is an antecedent to the rule, and an |
2112 | 892 |
* assumption to the theorem. |
893 |
*---------------------------------------------------------------------------*) |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
894 |
fun elim_tc tcthm (rule,induction) = |
2112 | 895 |
(R.MP rule tcthm, R.PROVE_HYP tcthm induction) |
896 |
||
897 |
||
898 |
fun postprocess{WFtac, terminator, simplifier} theory {rules,induction,TCs} = |
|
899 |
let val tych = Thry.typecheck theory |
|
900 |
||
901 |
(*--------------------------------------------------------------------- |
|
902 |
* Attempt to eliminate WF condition. It's the only assumption of rules |
|
903 |
*---------------------------------------------------------------------*) |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
904 |
val (rules1,induction1) = |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
905 |
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
|
906 |
(hd(#1(R.dest_thm rules)))), |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
907 |
WFtac) |
2112 | 908 |
in (R.PROVE_HYP thm rules, R.PROVE_HYP thm induction) |
909 |
end handle _ => (rules,induction) |
|
910 |
||
911 |
(*---------------------------------------------------------------------- |
|
912 |
* The termination condition (tc) is simplified to |- tc = tc' (there |
|
913 |
* might not be a change!) and then 3 attempts are made: |
|
914 |
* |
|
915 |
* 1. if |- tc = T, then eliminate it with eqT; otherwise, |
|
916 |
* 2. apply the terminator to tc'. If |- tc' = T then eliminate; else |
|
917 |
* 3. replace tc by tc' in both the rules and the induction theorem. |
|
918 |
*---------------------------------------------------------------------*) |
|
6498 | 919 |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
920 |
fun print_thms s L = |
7262 | 921 |
if !trace then writeln (cat_lines (s :: map string_of_thm L)) |
922 |
else (); |
|
6498 | 923 |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
924 |
fun print_cterms s L = |
7262 | 925 |
if !trace then writeln (cat_lines (s :: map string_of_cterm L)) |
926 |
else ();; |
|
6498 | 927 |
|
2112 | 928 |
fun simplify_tc tc (r,ind) = |
6498 | 929 |
let val tc1 = tych tc |
930 |
val _ = print_cterms "TC before simplification: " [tc1] |
|
931 |
val tc_eq = simplifier tc1 |
|
932 |
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
|
933 |
in |
2112 | 934 |
elim_tc (R.MATCH_MP Thms.eqT tc_eq) (r,ind) |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
935 |
handle _ => |
2112 | 936 |
(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
|
937 |
(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
|
938 |
terminator))) |
2112 | 939 |
(r,ind) |
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
940 |
handle _ => |
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
941 |
(R.UNDISCH(R.MATCH_MP (R.MATCH_MP Thms.simp_thm r) tc_eq), |
2112 | 942 |
simplify_induction theory tc_eq ind)) |
943 |
end |
|
944 |
||
945 |
(*---------------------------------------------------------------------- |
|
946 |
* 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
|
947 |
* 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
|
948 |
* 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
|
949 |
* 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
|
950 |
* 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
|
951 |
* termination condition (tc) is simplified to |- tc = tc' (there might |
2112 | 952 |
* not be a change) and then 2 attempts are made: |
953 |
* |
|
954 |
* 1. if |- tc = T, then return |- tc; otherwise, |
|
955 |
* 2. apply the terminator to tc'. If |- tc' = T then return |- tc; else |
|
956 |
* 3. return |- tc = tc' |
|
957 |
*---------------------------------------------------------------------*) |
|
958 |
fun simplify_nested_tc tc = |
|
959 |
let val tc_eq = simplifier (tych (#2 (S.strip_forall tc))) |
|
960 |
in |
|
961 |
R.GEN_ALL |
|
962 |
(R.MATCH_MP Thms.eqT tc_eq |
|
963 |
handle _ |
|
964 |
=> (R.MATCH_MP(R.MATCH_MP Thms.rev_eq_mp tc_eq) |
|
3405 | 965 |
(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
|
966 |
terminator)) |
2112 | 967 |
handle _ => tc_eq)) |
968 |
end |
|
969 |
||
970 |
(*------------------------------------------------------------------- |
|
8818
253dad743f00
error msg: counting from one (again), in order to be consistent with
wenzelm
parents:
8719
diff
changeset
|
971 |
* Attempt to simplify the termination conditions in each rule and |
2112 | 972 |
* in the induction theorem. |
973 |
*-------------------------------------------------------------------*) |
|
974 |
fun strip_imp tm = if S.is_neg tm then ([],tm) else S.strip_imp tm |
|
975 |
fun loop ([],extras,R,ind) = (rev R, ind, extras) |
|
976 |
| loop ((r,ftcs)::rst, nthms, R, ind) = |
|
977 |
let val tcs = #1(strip_imp (concl r)) |
|
3391
5e45dd3b64e9
More de-HOLification: using Free, Const, etc. instead of mk_var, mk_const
paulson
parents:
3388
diff
changeset
|
978 |
val extra_tcs = gen_rems (op aconv) (ftcs, tcs) |
2112 | 979 |
val extra_tc_thms = map simplify_nested_tc extra_tcs |
980 |
val (r1,ind1) = U.rev_itlist simplify_tc tcs (r,ind) |
|
981 |
val r2 = R.FILTER_DISCH_ALL(not o S.is_WFR) r1 |
|
982 |
in loop(rst, nthms@extra_tc_thms, r2::R, ind1) |
|
983 |
end |
|
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset
|
984 |
val rules_tcs = ListPair.zip (R.CONJUNCTS rules1, TCs) |
2112 | 985 |
val (rules2,ind2,extras) = loop(rules_tcs,[],[],induction1) |
986 |
in |
|
987 |
{induction = ind2, rules = R.LIST_CONJ rules2, nested_tcs = extras} |
|
988 |
end; |
|
989 |
||
990 |
end; (* TFL *) |