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