author | paulson |
Mon, 26 May 1997 12:38:29 +0200 | |
changeset 3342 | ec3b55fcb165 |
parent 3308 | da002cef7090 |
child 3534 | c245c88194ff |
permissions | -rw-r--r-- |
923 | 1 |
(* Title: HOL/datatype.ML |
2 |
ID: $Id$ |
|
1668 | 3 |
Author: Max Breitling, Carsten Clasohm, Tobias Nipkow, Norbert Voelker, |
4 |
Konrad Slind |
|
923 | 5 |
Copyright 1995 TU Muenchen |
6 |
*) |
|
7 |
||
3292 | 8 |
(* should go into Pure *) |
9 |
fun ALLNEWSUBGOALS tac tacf i = |
|
10 |
STATE (fn state0 => tac i THEN |
|
11 |
STATE (fn state1 => let val d = nprems_of state1 - nprems_of state0 |
|
12 |
in EVERY(map tacf ((i+d) downto i)) end)); |
|
923 | 13 |
|
14 |
(*used for constructor parameters*) |
|
15 |
datatype dt_type = dtVar of string | |
|
16 |
dtTyp of dt_type list * string | |
|
17 |
dtRek of dt_type list * string; |
|
18 |
||
19 |
structure Datatype = |
|
20 |
struct |
|
21 |
local |
|
22 |
||
23 |
val mysort = sort; |
|
24 |
open ThyParse HOLogic; |
|
25 |
exception Impossible; |
|
26 |
exception RecError of string; |
|
27 |
||
28 |
val is_dtRek = (fn dtRek _ => true | _ => false); |
|
29 |
fun opt_parens s = if s = "" then "" else enclose "(" ")" s; |
|
30 |
||
31 |
(* ----------------------------------------------------------------------- *) |
|
32 |
(* Derivation of the primrec combinator application from the equations *) |
|
33 |
||
34 |
(* substitute fname(ls,xk,rs) by yk(ls,rs) in t for (xk,yk) in pairs *) |
|
35 |
||
36 |
fun subst_apps (_,_) [] t = t |
|
37 |
| subst_apps (fname,rpos) pairs t = |
|
38 |
let |
|
39 |
fun subst (Abs(a,T,t)) = Abs(a,T,subst t) |
|
40 |
| subst (funct $ body) = |
|
1465 | 41 |
let val (f,b) = strip_comb (funct$body) |
42 |
in |
|
43 |
if is_Const f andalso fst(dest_Const f) = fname |
|
44 |
then |
|
45 |
let val (ls,rest) = (take(rpos,b), drop(rpos,b)); |
|
46 |
val (xk,rs) = (hd rest,tl rest) |
|
47 |
handle LIST _ => raise RecError "not enough arguments \ |
|
48 |
\ in recursive application on rhs" |
|
923 | 49 |
in |
1465 | 50 |
(case assoc (pairs,xk) of |
1574
5a63ab90ee8a
modified primrec so it can be used in MiniML/Type.thy
clasohm
parents:
1465
diff
changeset
|
51 |
None => list_comb(f, map subst b) |
5a63ab90ee8a
modified primrec so it can be used in MiniML/Type.thy
clasohm
parents:
1465
diff
changeset
|
52 |
| Some U => list_comb(U, map subst (ls @ rs))) |
1465 | 53 |
end |
54 |
else list_comb(f, map subst b) |
|
55 |
end |
|
923 | 56 |
| subst(t) = t |
57 |
in subst t end; |
|
58 |
||
59 |
(* abstract rhs *) |
|
60 |
||
61 |
fun abst_rec (fname,rpos,tc,ls,cargs,rs,rhs) = |
|
2270 | 62 |
let val rargs = (map #1 o |
1465 | 63 |
(filter (fn (a,T) => is_dtRek T))) (cargs ~~ tc); |
923 | 64 |
val subs = map (fn (s,T) => (s,dummyT)) |
1465 | 65 |
(rev(rename_wrt_term rhs rargs)); |
923 | 66 |
val subst_rhs = subst_apps (fname,rpos) |
1465 | 67 |
(map Free rargs ~~ map Free subs) rhs; |
923 | 68 |
in |
69 |
list_abs_free (cargs @ subs @ ls @ rs, subst_rhs) |
|
70 |
end; |
|
71 |
||
72 |
(* parsing the prim rec equations *) |
|
73 |
||
74 |
fun dest_eq ( Const("Trueprop",_) $ (Const ("op =",_) $ lhs $ rhs)) |
|
75 |
= (lhs, rhs) |
|
76 |
| dest_eq _ = raise RecError "not a proper equation"; |
|
77 |
||
78 |
fun dest_rec eq = |
|
79 |
let val (lhs,rhs) = dest_eq eq; |
|
80 |
val (name,args) = strip_comb lhs; |
|
81 |
val (ls',rest) = take_prefix is_Free args; |
|
82 |
val (middle,rs') = take_suffix is_Free rest; |
|
83 |
val rpos = length ls'; |
|
84 |
val (c,cargs') = strip_comb (hd middle) |
|
85 |
handle LIST "hd" => raise RecError "constructor missing"; |
|
86 |
val (ls,cargs,rs) = (map dest_Free ls', map dest_Free cargs' |
|
1465 | 87 |
, map dest_Free rs') |
923 | 88 |
handle TERM ("dest_Free",_) => |
1465 | 89 |
raise RecError "constructor has illegal argument in pattern"; |
923 | 90 |
in |
91 |
if length middle > 1 then |
|
92 |
raise RecError "more than one non-variable in pattern" |
|
93 |
else if not(null(findrep (map fst (ls @ rs @ cargs)))) then |
|
94 |
raise RecError "repeated variable name in pattern" |
|
1465 | 95 |
else (fst(dest_Const name) handle TERM _ => |
96 |
raise RecError "function is not declared as constant in theory" |
|
97 |
,rpos,ls,fst( dest_Const c),cargs,rs,rhs) |
|
923 | 98 |
end; |
99 |
||
100 |
(* check function specified for all constructors and sort function terms *) |
|
101 |
||
102 |
fun check_and_sort (n,its) = |
|
103 |
if length its = n |
|
104 |
then map snd (mysort (fn ((i : int,_),(j,_)) => i<j) its) |
|
105 |
else raise error "Primrec definition error:\n\ |
|
106 |
\Please give an equation for every constructor"; |
|
107 |
||
108 |
(* translate rec equations into function arguments suitable for rec comb *) |
|
109 |
(* theory parameter needed for printing error messages *) |
|
110 |
||
111 |
fun trans_recs _ _ [] = error("No primrec equations.") |
|
112 |
| trans_recs thy cs' (eq1::eqs) = |
|
113 |
let val (name1,rpos1,ls1,_,_,_,_) = dest_rec eq1 |
|
114 |
handle RecError s => |
|
1465 | 115 |
error("Primrec definition error: " ^ s ^ ":\n" |
116 |
^ " " ^ Sign.string_of_term (sign_of thy) eq1); |
|
923 | 117 |
val tcs = map (fn (_,c,T,_,_) => (c,T)) cs'; |
118 |
val cs = map fst tcs; |
|
119 |
fun trans_recs' _ [] = [] |
|
120 |
| trans_recs' cis (eq::eqs) = |
|
1465 | 121 |
let val (name,rpos,ls,c,cargs,rs,rhs) = dest_rec eq; |
122 |
val tc = assoc(tcs,c); |
|
123 |
val i = (1 + find (c,cs)) handle LIST "find" => 0; |
|
124 |
in |
|
125 |
if name <> name1 then |
|
126 |
raise RecError "function names inconsistent" |
|
127 |
else if rpos <> rpos1 then |
|
128 |
raise RecError "position of rec. argument inconsistent" |
|
129 |
else if i = 0 then |
|
130 |
raise RecError "illegal argument in pattern" |
|
131 |
else if i mem cis then |
|
132 |
raise RecError "constructor already occured as pattern " |
|
133 |
else (i,abst_rec (name,rpos,the tc,ls,cargs,rs,rhs)) |
|
134 |
:: trans_recs' (i::cis) eqs |
|
135 |
end |
|
136 |
handle RecError s => |
|
137 |
error("Primrec definition error\n" ^ s ^ "\n" |
|
138 |
^ " " ^ Sign.string_of_term (sign_of thy) eq); |
|
923 | 139 |
in ( name1, ls1 |
1465 | 140 |
, check_and_sort (length cs, trans_recs' [] (eq1::eqs))) |
923 | 141 |
end ; |
142 |
||
143 |
in |
|
144 |
fun add_datatype (typevars, tname, cons_list') thy = |
|
145 |
let |
|
3308
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
146 |
val dummy = require_thy thy "Arith" "datatype definitions"; |
2880 | 147 |
|
923 | 148 |
fun typid(dtRek(_,id)) = id |
149 |
| typid(dtVar s) = implode (tl (explode s)) |
|
150 |
| typid(dtTyp(_,id)) = id; |
|
151 |
||
152 |
fun index_vnames(vn::vns,tab) = |
|
153 |
(case assoc(tab,vn) of |
|
154 |
None => if vn mem vns |
|
155 |
then (vn^"1") :: index_vnames(vns,(vn,2)::tab) |
|
156 |
else vn :: index_vnames(vns,tab) |
|
157 |
| Some(i) => (vn^(string_of_int i)) :: |
|
158 |
index_vnames(vns,(vn,i+1)::tab)) |
|
159 |
| index_vnames([],tab) = []; |
|
160 |
||
161 |
fun mk_var_names types = index_vnames(map typid types,[]); |
|
162 |
||
163 |
(*search for free type variables and convert recursive *) |
|
164 |
fun analyse_types (cons, types, syn) = |
|
1465 | 165 |
let fun analyse(t as dtVar v) = |
923 | 166 |
if t mem typevars then t |
167 |
else error ("Free type variable " ^ v ^ " on rhs.") |
|
1465 | 168 |
| analyse(dtTyp(typl,s)) = |
169 |
if tname <> s then dtTyp(analyses typl, s) |
|
923 | 170 |
else if typevars = typl then dtRek(typl, s) |
171 |
else error (s ^ " used in different ways") |
|
1465 | 172 |
| analyse(dtRek _) = raise Impossible |
173 |
and analyses ts = map analyse ts; |
|
174 |
in (cons, Syntax.const_name cons syn, analyses types, |
|
923 | 175 |
mk_var_names types, syn) |
176 |
end; |
|
177 |
||
178 |
(*test if all elements are recursive, i.e. if the type is empty*) |
|
179 |
||
180 |
fun non_empty (cs : ('a * 'b * dt_type list * 'c *'d) list) = |
|
1465 | 181 |
not(forall (exists is_dtRek o #3) cs) orelse |
182 |
error("Empty datatype not allowed!"); |
|
923 | 183 |
|
184 |
val cons_list = map analyse_types cons_list'; |
|
185 |
val dummy = non_empty cons_list; |
|
186 |
val num_of_cons = length cons_list; |
|
187 |
||
188 |
(* Auxiliary functions to construct argument and equation lists *) |
|
189 |
||
190 |
(*generate 'var_n, ..., var_m'*) |
|
191 |
fun Args(var, delim, n, m) = |
|
1465 | 192 |
space_implode delim (map (fn n => var^string_of_int(n)) (n upto m)); |
923 | 193 |
|
194 |
fun C_exp name vns = name ^ opt_parens(space_implode ") (" vns); |
|
195 |
||
196 |
(*Arg_eqs([x1,...,xn],[y1,...,yn]) = "x1 = y1 & ... & xn = yn" *) |
|
197 |
fun arg_eqs vns vns' = |
|
198 |
let fun mkeq(x,x') = x ^ "=" ^ x' |
|
2270 | 199 |
in space_implode " & " (ListPair.map mkeq (vns,vns')) end; |
923 | 200 |
|
201 |
(*Pretty printers for type lists; |
|
202 |
pp_typlist1: parentheses, pp_typlist2: brackets*) |
|
1279
f59b4f9f2cdc
All constants introduced by datatype now operate on class term explicitly.
nipkow
parents:
980
diff
changeset
|
203 |
fun pp_typ (dtVar s) = "(" ^ s ^ "::term)" |
923 | 204 |
| pp_typ (dtTyp (typvars, id)) = |
1465 | 205 |
if null typvars then id else (pp_typlist1 typvars) ^ id |
923 | 206 |
| pp_typ (dtRek (typvars, id)) = (pp_typlist1 typvars) ^ id |
207 |
and |
|
1465 | 208 |
pp_typlist' ts = commas (map pp_typ ts) |
923 | 209 |
and |
1465 | 210 |
pp_typlist1 ts = if null ts then "" else parens (pp_typlist' ts); |
923 | 211 |
|
212 |
fun pp_typlist2 ts = if null ts then "" else brackets (pp_typlist' ts); |
|
213 |
||
214 |
(* Generate syntax translation for case rules *) |
|
215 |
fun calc_xrules c_nr y_nr ((_, name, _, vns, _) :: cs) = |
|
1465 | 216 |
let val arity = length vns; |
217 |
val body = "z" ^ string_of_int(c_nr); |
|
218 |
val args1 = if arity=0 then "" |
|
219 |
else " " ^ Args ("y", " ", y_nr, y_nr+arity-1); |
|
220 |
val args2 = if arity=0 then "" |
|
221 |
else "(% " ^ Args ("y", " ", y_nr, y_nr+arity-1) |
|
222 |
^ ". "; |
|
223 |
val (rest1,rest2) = |
|
224 |
if null cs then ("","") |
|
225 |
else let val (h1, h2) = calc_xrules (c_nr+1) (y_nr+arity) cs |
|
226 |
in (" | " ^ h1, " " ^ h2) end; |
|
227 |
in (name ^ args1 ^ " => " ^ body ^ rest1, |
|
964 | 228 |
args2 ^ body ^ (if args2 = "" then "" else ")") ^ rest2) |
923 | 229 |
end |
230 |
| calc_xrules _ _ [] = raise Impossible; |
|
231 |
||
232 |
val xrules = |
|
1465 | 233 |
let val (first_part, scnd_part) = calc_xrules 1 1 cons_list |
1810
0eef167ebe1b
Translation infixes <->, etc., no longer available at top-level
paulson
parents:
1690
diff
changeset
|
234 |
in [Syntax.<-> (("logic", "case x of " ^ first_part), |
2031 | 235 |
("logic", tname ^ "_case " ^ scnd_part ^ " x"))] |
1465 | 236 |
end; |
923 | 237 |
|
238 |
(*type declarations for constructors*) |
|
239 |
fun const_type (id, _, typlist, _, syn) = |
|
1465 | 240 |
(id, |
241 |
(if null typlist then "" else pp_typlist2 typlist ^ " => ") ^ |
|
242 |
pp_typlist1 typevars ^ tname, syn); |
|
923 | 243 |
|
244 |
||
245 |
fun assumpt (dtRek _ :: ts, v :: vs ,found) = |
|
1465 | 246 |
let val h = if found then ";P(" ^ v ^ ")" else "[| P(" ^ v ^ ")" |
247 |
in h ^ (assumpt (ts, vs, true)) end |
|
923 | 248 |
| assumpt (t :: ts, v :: vs, found) = assumpt (ts, vs, found) |
249 |
| assumpt ([], [], found) = if found then "|] ==>" else "" |
|
250 |
| assumpt _ = raise Impossible; |
|
251 |
||
252 |
fun t_inducting ((_, name, types, vns, _) :: cs) = |
|
1465 | 253 |
let |
254 |
val h = if null types then " P(" ^ name ^ ")" |
|
255 |
else " !!" ^ (space_implode " " vns) ^ "." ^ |
|
256 |
(assumpt (types, vns, false)) ^ |
|
923 | 257 |
"P(" ^ C_exp name vns ^ ")"; |
1465 | 258 |
val rest = t_inducting cs; |
259 |
in if rest = "" then h else h ^ "; " ^ rest end |
|
923 | 260 |
| t_inducting [] = ""; |
261 |
||
262 |
fun t_induct cl typ_name = |
|
263 |
"[|" ^ t_inducting cl ^ "|] ==> P(" ^ typ_name ^ ")"; |
|
264 |
||
265 |
fun gen_typlist typevar f ((_, _, ts, _, _) :: cs) = |
|
1465 | 266 |
let val h = if (length ts) > 0 |
267 |
then pp_typlist2(f ts) ^ "=>" |
|
268 |
else "" |
|
269 |
in h ^ typevar ^ "," ^ (gen_typlist typevar f cs) end |
|
923 | 270 |
| gen_typlist _ _ [] = ""; |
271 |
||
272 |
||
273 |
(* -------------------------------------------------------------------- *) |
|
1465 | 274 |
(* The case constant and rules *) |
275 |
||
923 | 276 |
val t_case = tname ^ "_case"; |
277 |
||
278 |
fun case_rule n (id, name, _, vns, _) = |
|
1465 | 279 |
let val args = if vns = [] then "" else " " ^ space_implode " " vns |
280 |
in (t_case ^ "_" ^ id, |
|
281 |
t_case ^ " " ^ Args("f", " ", 1, num_of_cons) |
|
282 |
^ " (" ^ name ^ args ^ ") = f"^string_of_int(n) ^ args) |
|
283 |
end |
|
923 | 284 |
|
285 |
fun case_rules n (c :: cs) = case_rule n c :: case_rules(n+1) cs |
|
286 |
| case_rules _ [] = []; |
|
287 |
||
288 |
val datatype_arity = length typevars; |
|
289 |
||
290 |
val types = [(tname, datatype_arity, NoSyn)]; |
|
291 |
||
292 |
val arities = |
|
293 |
let val term_list = replicate datatype_arity termS; |
|
294 |
in [(tname, term_list, termS)] |
|
1465 | 295 |
end; |
923 | 296 |
|
297 |
val datatype_name = pp_typlist1 typevars ^ tname; |
|
298 |
||
299 |
val new_tvar_name = variant (map (fn dtVar s => s) typevars) "'z"; |
|
300 |
||
301 |
val case_const = |
|
1465 | 302 |
(t_case, |
303 |
"[" ^ gen_typlist new_tvar_name I cons_list |
|
304 |
^ pp_typlist1 typevars ^ tname ^ "] =>" ^ new_tvar_name^"::term", |
|
305 |
NoSyn); |
|
923 | 306 |
|
307 |
val rules_case = case_rules 1 cons_list; |
|
308 |
||
309 |
(* -------------------------------------------------------------------- *) |
|
1465 | 310 |
(* The prim-rec combinator *) |
923 | 311 |
|
312 |
val t_rec = tname ^ "_rec" |
|
313 |
||
314 |
(* adding type variables for dtRek types to end of list of dt_types *) |
|
315 |
||
316 |
fun add_reks ts = |
|
1465 | 317 |
ts @ map (fn _ => dtVar new_tvar_name) (filter is_dtRek ts); |
923 | 318 |
|
319 |
(* positions of the dtRek types in a list of dt_types, starting from 1 *) |
|
2270 | 320 |
fun rek_vars ts vns = map #2 (filter (is_dtRek o fst) (ts ~~ vns)) |
923 | 321 |
|
322 |
fun rec_rule n (id,name,ts,vns,_) = |
|
1465 | 323 |
let val args = opt_parens(space_implode ") (" vns) |
324 |
val fargs = opt_parens(Args("f", ") (", 1, num_of_cons)) |
|
325 |
fun rarg vn = t_rec ^ fargs ^ " (" ^ vn ^ ")" |
|
326 |
val rargs = opt_parens(space_implode ") (" |
|
964 | 327 |
(map rarg (rek_vars ts vns))) |
1465 | 328 |
in |
329 |
(t_rec ^ "_" ^ id, |
|
330 |
t_rec ^ fargs ^ " (" ^ name ^ args ^ ") = f" |
|
331 |
^ string_of_int(n) ^ args ^ rargs) |
|
332 |
end |
|
923 | 333 |
|
334 |
fun rec_rules n (c::cs) = rec_rule n c :: rec_rules (n+1) cs |
|
1465 | 335 |
| rec_rules _ [] = []; |
923 | 336 |
|
337 |
val rec_const = |
|
1465 | 338 |
(t_rec, |
339 |
"[" ^ (gen_typlist new_tvar_name add_reks cons_list) |
|
340 |
^ (pp_typlist1 typevars) ^ tname ^ "] =>" ^ new_tvar_name^"::term", |
|
341 |
NoSyn); |
|
923 | 342 |
|
343 |
val rules_rec = rec_rules 1 cons_list |
|
344 |
||
345 |
(* -------------------------------------------------------------------- *) |
|
3308
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
346 |
(* The size function *) |
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
347 |
|
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
348 |
fun size_eqn(_,name,types,vns,_) = |
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
349 |
let fun sum((T,vn)::args) = |
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
350 |
if is_dtRek T then "size(" ^ vn ^ ") + " ^ sum(args) |
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
351 |
else sum args |
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
352 |
| sum [] = "1" |
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
353 |
val rhs = if exists is_dtRek types then sum(types~~vns) else "0" |
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
354 |
in ("", "size(" ^ C_exp name vns ^ ") = " ^ rhs) end; |
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
355 |
|
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
356 |
val size_eqns = map size_eqn cons_list; |
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
357 |
|
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
358 |
(* -------------------------------------------------------------------- *) |
923 | 359 |
val consts = |
1465 | 360 |
map const_type cons_list |
361 |
@ (if num_of_cons < dtK then [] |
|
362 |
else [(tname ^ "_ord", datatype_name ^ "=>nat", NoSyn)]) |
|
363 |
@ [case_const,rec_const]; |
|
923 | 364 |
|
365 |
||
366 |
fun Ci_ing ((id, name, _, vns, _) :: cs) = |
|
1465 | 367 |
if null vns then Ci_ing cs |
368 |
else let val vns' = variantlist(vns,vns) |
|
923 | 369 |
in ("inject_" ^ id, |
1465 | 370 |
"(" ^ (C_exp name vns) ^ "=" ^ (C_exp name vns') |
371 |
^ ") = (" ^ (arg_eqs vns vns') ^ ")") :: (Ci_ing cs) |
|
923 | 372 |
end |
1465 | 373 |
| Ci_ing [] = []; |
923 | 374 |
|
375 |
fun Ci_negOne (id1,name1,_,vns1,_) (id2,name2,_,vns2,_) = |
|
376 |
let val vns2' = variantlist(vns2,vns1) |
|
377 |
val ax = C_exp name1 vns1 ^ "~=" ^ C_exp name2 vns2' |
|
1465 | 378 |
in (id1 ^ "_not_" ^ id2, ax) end; |
923 | 379 |
|
380 |
fun Ci_neg1 [] = [] |
|
1465 | 381 |
| Ci_neg1 (c1::cs) = (map (Ci_negOne c1) cs) @ Ci_neg1 cs; |
923 | 382 |
|
383 |
fun suc_expr n = |
|
1465 | 384 |
if n=0 then "0" else "Suc(" ^ suc_expr(n-1) ^ ")"; |
923 | 385 |
|
386 |
fun Ci_neg2() = |
|
1465 | 387 |
let val ord_t = tname ^ "_ord"; |
2270 | 388 |
val cis = ListPair.zip (cons_list, 0 upto (num_of_cons - 1)) |
1465 | 389 |
fun Ci_neg2equals ((id, name, _, vns, _), n) = |
390 |
let val ax = ord_t ^ "(" ^ (C_exp name vns) ^ ") = " ^ (suc_expr n) |
|
391 |
in (ord_t ^ "_" ^ id, ax) end |
|
392 |
in (ord_t ^ "_distinct", ord_t^"(x) ~= "^ord_t^"(y) ==> x ~= y") :: |
|
393 |
(map Ci_neg2equals cis) |
|
394 |
end; |
|
923 | 395 |
|
396 |
val rules_distinct = if num_of_cons < dtK then Ci_neg1 cons_list |
|
1465 | 397 |
else Ci_neg2(); |
923 | 398 |
|
399 |
val rules_inject = Ci_ing cons_list; |
|
400 |
||
401 |
val rule_induct = (tname ^ "_induct", t_induct cons_list tname); |
|
402 |
||
403 |
val rules = rule_induct :: |
|
1465 | 404 |
(rules_inject @ rules_distinct @ rules_case @ rules_rec); |
923 | 405 |
|
406 |
fun add_primrec eqns thy = |
|
1465 | 407 |
let val rec_comb = Const(t_rec,dummyT) |
408 |
val teqns = map (fn neq => snd(read_axm (sign_of thy) neq)) eqns |
|
409 |
val (fname,ls,fns) = trans_recs thy cons_list teqns |
|
410 |
val rhs = |
|
411 |
list_abs_free |
|
412 |
(ls @ [(tname,dummyT)] |
|
413 |
,list_comb(rec_comb |
|
414 |
, fns @ map Bound (0 ::(length ls downto 1)))); |
|
923 | 415 |
val sg = sign_of thy; |
1574
5a63ab90ee8a
modified primrec so it can be used in MiniML/Type.thy
clasohm
parents:
1465
diff
changeset
|
416 |
val defpair = (fname ^ "_" ^ tname ^ "_def", |
5a63ab90ee8a
modified primrec so it can be used in MiniML/Type.thy
clasohm
parents:
1465
diff
changeset
|
417 |
Logic.mk_equals (Const(fname,dummyT), rhs)) |
1465 | 418 |
val defpairT as (_, _ $ Const(_,T) $ _ ) = inferT_axm sg defpair; |
419 |
val varT = Type.varifyT T; |
|
923 | 420 |
val ftyp = the (Sign.const_type sg fname); |
1574
5a63ab90ee8a
modified primrec so it can be used in MiniML/Type.thy
clasohm
parents:
1465
diff
changeset
|
421 |
in add_defs_i [defpairT] thy end; |
923 | 422 |
|
1360 | 423 |
in |
424 |
(thy |> add_types types |
|
425 |
|> add_arities arities |
|
426 |
|> add_consts consts |
|
427 |
|> add_trrules xrules |
|
3308
da002cef7090
Added overloaded function `size' for all datatypes.
nipkow
parents:
3292
diff
changeset
|
428 |
|> add_axioms rules, add_primrec, size_eqns) |
923 | 429 |
end |
3040
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
430 |
|
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
431 |
(*Check if the (induction) variable occurs among the premises, which |
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
432 |
usually signals a mistake *) |
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
433 |
fun occs_in_prems a i state = |
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
434 |
let val (_, _, Bi, _) = dest_state(state,i) |
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
435 |
val prems = Logic.strip_assums_hyp Bi |
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
436 |
in a mem map (#1 o dest_Free) (foldr add_term_frees (prems,[])) end; |
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
437 |
|
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
438 |
end; |
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
439 |
|
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
440 |
end; |
923 | 441 |
|
442 |
(* |
|
443 |
Informal description of functions used in datatype.ML for the Isabelle/HOL |
|
444 |
implementation of prim. rec. function definitions. (N. Voelker, Feb. 1995) |
|
445 |
||
446 |
* subst_apps (fname,rpos) pairs t: |
|
447 |
substitute the term |
|
448 |
fname(ls,xk,rs) |
|
449 |
by |
|
450 |
yk(ls,rs) |
|
451 |
in t for (xk,yk) in pairs, where rpos = length ls. |
|
452 |
Applied with : |
|
453 |
fname = function name |
|
454 |
rpos = position of recursive argument |
|
455 |
pairs = list of pairs (xk,yk), where |
|
456 |
xk are the rec. arguments of the constructor in the pattern, |
|
457 |
yk is a variable with name derived from xk |
|
458 |
t = rhs of equation |
|
459 |
||
460 |
* abst_rec (fname,rpos,tc,ls,cargs,rs,rhs) |
|
461 |
- filter recursive arguments from constructor arguments cargs, |
|
462 |
- perform substitutions on rhs, |
|
463 |
- derive list subs of new variable names yk for use in subst_apps, |
|
464 |
- abstract rhs with respect to cargs, subs, ls and rs. |
|
465 |
||
466 |
* dest_eq t |
|
467 |
destruct a term denoting an equation into lhs and rhs. |
|
468 |
||
469 |
* dest_req eq |
|
470 |
destruct an equation of the form |
|
471 |
name (vl1..vlrpos, Ci(vi1..vin), vr1..vrn) = rhs |
|
472 |
into |
|
473 |
- function name (name) |
|
474 |
- position of the first non-variable parameter (rpos) |
|
475 |
- the list of first rpos parameters (ls = [vl1..vlrpos]) |
|
476 |
- the constructor (fst( dest_Const c) = Ci) |
|
477 |
- the arguments of the constructor (cargs = [vi1..vin]) |
|
478 |
- the rest of the variables in the pattern (rs = [vr1..vrn]) |
|
479 |
- the right hand side of the equation (rhs). |
|
480 |
||
481 |
* check_and_sort (n,its) |
|
482 |
check that n = length its holds, and sort elements of its by |
|
483 |
first component. |
|
484 |
||
485 |
* trans_recs thy cs' (eq1::eqs) |
|
486 |
destruct eq1 into name1, rpos1, ls1, etc.. |
|
487 |
get constructor list with and without type (tcs resp. cs) from cs', |
|
488 |
for every equation: |
|
489 |
destruct it into (name,rpos,ls,c,cargs,rs,rhs) |
|
490 |
get typed constructor tc from c and tcs |
|
491 |
determine the index i of the constructor |
|
492 |
check function name and position of rec. argument by comparison |
|
493 |
with first equation |
|
494 |
check for repeated variable names in pattern |
|
495 |
derive function term f_i which is used as argument of the rec. combinator |
|
496 |
sort the terms f_i according to i and return them together |
|
497 |
with the function name and the parameter of the definition (ls). |
|
498 |
||
499 |
* Application: |
|
500 |
||
501 |
The rec. combinator is applied to the function terms resulting from |
|
502 |
trans_rec. This results in a function which takes the recursive arg. |
|
503 |
as first parameter and then the arguments corresponding to ls. The |
|
504 |
order of parameters is corrected by setting the rhs equal to |
|
505 |
||
506 |
list_abs_free |
|
1465 | 507 |
(ls @ [(tname,dummyT)] |
508 |
,list_comb(rec_comb |
|
509 |
, fns @ map Bound (0 ::(length ls downto 1)))); |
|
923 | 510 |
|
511 |
Note the de-Bruijn indices counting the number of lambdas between the |
|
512 |
variable and its binding. |
|
513 |
*) |
|
1668 | 514 |
|
515 |
||
516 |
||
517 |
(* ----------------------------------------------- *) |
|
518 |
(* The following has been written by Konrad Slind. *) |
|
519 |
||
520 |
||
3040
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
521 |
(* type dtype_info is defined in simpdata.ML *) |
1668 | 522 |
|
523 |
signature Dtype_sig = |
|
524 |
sig |
|
525 |
val build_case_cong: Sign.sg -> thm list -> cterm |
|
526 |
val build_nchotomy: Sign.sg -> thm list -> cterm |
|
527 |
||
528 |
val prove_case_cong: thm -> thm list -> cterm -> thm |
|
1690 | 529 |
val prove_nchotomy: (string -> int -> tactic) -> cterm -> thm |
1668 | 530 |
|
531 |
val case_thms : Sign.sg -> thm list -> (string -> int -> tactic) |
|
532 |
-> {nchotomy:thm, case_cong:thm} |
|
533 |
||
534 |
val build_record : (theory * (string * string list) |
|
535 |
* (string -> int -> tactic)) |
|
536 |
-> (string * dtype_info) |
|
537 |
||
538 |
end; |
|
539 |
||
540 |
||
541 |
(*--------------------------------------------------------------------------- |
|
542 |
* This structure is support for the Isabelle datatype package. It provides |
|
543 |
* entrypoints for 1) building and proving the case congruence theorem for |
|
544 |
* a datatype and 2) building and proving the "exhaustion" theorem for |
|
545 |
* a datatype (I have called this theorem "nchotomy" for no good reason). |
|
546 |
* |
|
547 |
* It also brings all these together in the function "build_record", which |
|
548 |
* is probably what will be used. |
|
549 |
* |
|
550 |
* Since these routines are required in order to support TFL, they have |
|
551 |
* been written so they will compile "stand-alone", i.e., in Isabelle-HOL |
|
552 |
* without any TFL code around. |
|
553 |
*---------------------------------------------------------------------------*) |
|
554 |
structure Dtype : Dtype_sig = |
|
555 |
struct |
|
556 |
||
557 |
exception DTYPE_ERR of {func:string, mesg:string}; |
|
558 |
||
559 |
(*--------------------------------------------------------------------------- |
|
560 |
* General support routines |
|
561 |
*---------------------------------------------------------------------------*) |
|
562 |
fun itlist f L base_value = |
|
563 |
let fun it [] = base_value |
|
564 |
| it (a::rst) = f a (it rst) |
|
565 |
in it L |
|
566 |
end; |
|
567 |
||
568 |
fun end_itlist f = |
|
569 |
let fun endit [] = raise DTYPE_ERR{func="end_itlist", mesg="list too short"} |
|
570 |
| endit alist = |
|
571 |
let val (base::ralist) = rev alist |
|
572 |
in itlist f (rev ralist) base end |
|
573 |
in endit |
|
574 |
end; |
|
575 |
||
576 |
fun unzip L = itlist (fn (x,y) => fn (l1,l2) =>((x::l1),(y::l2))) L ([],[]); |
|
577 |
||
578 |
||
579 |
(*--------------------------------------------------------------------------- |
|
580 |
* Miscellaneous Syntax manipulation |
|
581 |
*---------------------------------------------------------------------------*) |
|
582 |
val mk_var = Free; |
|
583 |
val mk_const = Const |
|
584 |
fun mk_comb(Rator,Rand) = Rator $ Rand; |
|
585 |
fun mk_abs(r as (Var((s,_),ty),_)) = Abs(s,ty,abstract_over r) |
|
586 |
| mk_abs(r as (Free(s,ty),_)) = Abs(s,ty,abstract_over r) |
|
587 |
| mk_abs _ = raise DTYPE_ERR{func="mk_abs", mesg="1st not a variable"}; |
|
588 |
||
589 |
fun dest_var(Var((s,i),ty)) = (s,ty) |
|
590 |
| dest_var(Free(s,ty)) = (s,ty) |
|
591 |
| dest_var _ = raise DTYPE_ERR{func="dest_var", mesg="not a variable"}; |
|
592 |
||
593 |
fun dest_const(Const p) = p |
|
594 |
| dest_const _ = raise DTYPE_ERR{func="dest_const", mesg="not a constant"}; |
|
595 |
||
596 |
fun dest_comb(t1 $ t2) = (t1,t2) |
|
597 |
| dest_comb _ = raise DTYPE_ERR{func = "dest_comb", mesg = "not a comb"}; |
|
598 |
val rand = #2 o dest_comb; |
|
599 |
val rator = #1 o dest_comb; |
|
600 |
||
601 |
fun dest_abs(a as Abs(s,ty,M)) = |
|
602 |
let val v = Free(s, ty) |
|
603 |
in (v, betapply (a,v)) end |
|
604 |
| dest_abs _ = raise DTYPE_ERR{func="dest_abs", mesg="not an abstraction"}; |
|
605 |
||
606 |
||
607 |
val bool = Type("bool",[]) |
|
608 |
and prop = Type("prop",[]); |
|
609 |
||
610 |
fun mk_eq(lhs,rhs) = |
|
611 |
let val ty = type_of lhs |
|
612 |
val c = mk_const("op =", ty --> ty --> bool) |
|
613 |
in list_comb(c,[lhs,rhs]) |
|
614 |
end |
|
615 |
||
616 |
fun dest_eq(Const("op =",_) $ M $ N) = (M, N) |
|
617 |
| dest_eq _ = raise DTYPE_ERR{func="dest_eq", mesg="not an equality"}; |
|
618 |
||
619 |
fun mk_disj(disj1,disj2) = |
|
620 |
let val c = Const("op |", bool --> bool --> bool) |
|
621 |
in list_comb(c,[disj1,disj2]) |
|
622 |
end; |
|
623 |
||
624 |
fun mk_forall (r as (Bvar,_)) = |
|
625 |
let val ty = type_of Bvar |
|
626 |
val c = Const("All", (ty --> bool) --> bool) |
|
627 |
in mk_comb(c, mk_abs r) |
|
628 |
end; |
|
629 |
||
630 |
fun mk_exists (r as (Bvar,_)) = |
|
631 |
let val ty = type_of Bvar |
|
632 |
val c = Const("Ex", (ty --> bool) --> bool) |
|
633 |
in mk_comb(c, mk_abs r) |
|
634 |
end; |
|
635 |
||
636 |
fun mk_prop (tm as Const("Trueprop",_) $ _) = tm |
|
637 |
| mk_prop tm = mk_comb(Const("Trueprop", bool --> prop),tm); |
|
638 |
||
639 |
fun drop_prop (Const("Trueprop",_) $ X) = X |
|
640 |
| drop_prop X = X; |
|
641 |
||
642 |
fun mk_all (r as (Bvar,_)) = mk_comb(all (type_of Bvar), mk_abs r); |
|
643 |
fun list_mk_all(V,t) = itlist(fn v => fn b => mk_all(v,b)) V t; |
|
644 |
fun list_mk_exists(V,t) = itlist(fn v => fn b => mk_exists(v,b)) V t; |
|
645 |
val list_mk_disj = end_itlist(fn d1 => fn tm => mk_disj(d1,tm)) |
|
646 |
||
647 |
||
648 |
fun dest_thm thm = |
|
649 |
let val {prop,hyps,...} = rep_thm thm |
|
650 |
in (map drop_prop hyps, drop_prop prop) |
|
651 |
end; |
|
652 |
||
653 |
val concl = #2 o dest_thm; |
|
654 |
||
655 |
||
656 |
(*--------------------------------------------------------------------------- |
|
657 |
* Names of all variables occurring in a term, including bound ones. These |
|
658 |
* are added into the second argument. |
|
3265
8358e19d0d4c
Replaced Konrad's own add_term_names by the predefined one.
nipkow
parents:
3197
diff
changeset
|
659 |
*--------------------------------------------------------------------------- |
1668 | 660 |
fun add_term_names tm = |
661 |
let fun insert (x:string) = |
|
662 |
let fun canfind[] = [x] |
|
663 |
| canfind(alist as (y::rst)) = |
|
664 |
if (x<y) then x::alist |
|
665 |
else if (x=y) then y::rst |
|
666 |
else y::canfind rst |
|
667 |
in canfind end |
|
668 |
fun add (Free(s,_)) V = insert s V |
|
669 |
| add (Var((s,_),_)) V = insert s V |
|
670 |
| add (Abs(s,_,body)) V = add body (insert s V) |
|
671 |
| add (f$t) V = add t (add f V) |
|
672 |
| add _ V = V |
|
673 |
in add tm |
|
674 |
end; |
|
3265
8358e19d0d4c
Replaced Konrad's own add_term_names by the predefined one.
nipkow
parents:
3197
diff
changeset
|
675 |
Why bound ones??? |
8358e19d0d4c
Replaced Konrad's own add_term_names by the predefined one.
nipkow
parents:
3197
diff
changeset
|
676 |
*) |
1668 | 677 |
|
678 |
(*--------------------------------------------------------------------------- |
|
679 |
* We need to make everything free, so that we can put the term into a |
|
680 |
* goalstack, or submit it as an argument to prove_goalw_cterm. |
|
681 |
*---------------------------------------------------------------------------*) |
|
682 |
fun make_free_ty(Type(s,alist)) = Type(s,map make_free_ty alist) |
|
683 |
| make_free_ty(TVar((s,i),srt)) = TFree(s,srt) |
|
684 |
| make_free_ty x = x; |
|
685 |
||
686 |
fun make_free (Var((s,_),ty)) = Free(s,make_free_ty ty) |
|
687 |
| make_free (Abs(s,x,body)) = Abs(s,make_free_ty x, make_free body) |
|
688 |
| make_free (f$t) = (make_free f $ make_free t) |
|
689 |
| make_free (Const(s,ty)) = Const(s, make_free_ty ty) |
|
690 |
| make_free (Free(s,ty)) = Free(s, make_free_ty ty) |
|
691 |
| make_free b = b; |
|
692 |
||
693 |
||
694 |
(*--------------------------------------------------------------------------- |
|
695 |
* Structure of case congruence theorem looks like this: |
|
696 |
* |
|
697 |
* (M = M') |
|
698 |
* ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = f1' x1..xk)) |
|
699 |
* ==> ... |
|
700 |
* ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = fn' x1..xj)) |
|
701 |
* ==> |
|
702 |
* (ty_case f1..fn M = ty_case f1'..fn' m') |
|
703 |
* |
|
704 |
* The input is the list of rules for the case construct for the type, i.e., |
|
705 |
* that found in the "ty.cases" field of a theory where datatype "ty" is |
|
706 |
* defined. |
|
707 |
*---------------------------------------------------------------------------*) |
|
708 |
||
709 |
fun build_case_cong sign case_rewrites = |
|
710 |
let val clauses = map concl case_rewrites |
|
711 |
val clause1 = hd clauses |
|
712 |
val left = (#1 o dest_eq) clause1 |
|
713 |
val ty = type_of ((#2 o dest_comb) left) |
|
3265
8358e19d0d4c
Replaced Konrad's own add_term_names by the predefined one.
nipkow
parents:
3197
diff
changeset
|
714 |
val varnames = foldr add_term_names (clauses, []) |
1668 | 715 |
val M = variant varnames "M" |
716 |
val Mvar = Free(M, ty) |
|
717 |
val M' = variant (M::varnames) M |
|
718 |
val M'var = Free(M', ty) |
|
719 |
fun mk_clause clause = |
|
720 |
let val (lhs,rhs) = dest_eq clause |
|
721 |
val func = (#1 o strip_comb) rhs |
|
722 |
val (constr,xbar) = strip_comb(rand lhs) |
|
723 |
val (Name,Ty) = dest_var func |
|
724 |
val func'name = variant (M::M'::varnames) (Name^"a") |
|
725 |
val func' = mk_var(func'name,Ty) |
|
726 |
in (func', list_mk_all |
|
727 |
(xbar, Logic.mk_implies |
|
728 |
(mk_prop(mk_eq(M'var, list_comb(constr,xbar))), |
|
729 |
mk_prop(mk_eq(list_comb(func, xbar), |
|
730 |
list_comb(func',xbar)))))) end |
|
731 |
val (funcs',clauses') = unzip (map mk_clause clauses) |
|
732 |
val lhsM = mk_comb(rator left, Mvar) |
|
733 |
val c = #1(strip_comb left) |
|
734 |
in |
|
735 |
cterm_of sign |
|
736 |
(make_free |
|
737 |
(Logic.list_implies(mk_prop(mk_eq(Mvar, M'var))::clauses', |
|
738 |
mk_prop(mk_eq(lhsM, list_comb(c,(funcs'@[M'var]))))))) |
|
739 |
end |
|
740 |
handle _ => raise DTYPE_ERR{func="build_case_cong",mesg="failed"}; |
|
741 |
||
742 |
||
743 |
(*--------------------------------------------------------------------------- |
|
744 |
* Proves the result of "build_case_cong". |
|
1897
71e51870cc9a
Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents:
1810
diff
changeset
|
745 |
* This one solves it a disjunct at a time, and builds the ss only once. |
1668 | 746 |
*---------------------------------------------------------------------------*) |
747 |
fun prove_case_cong nchotomy case_rewrites ctm = |
|
748 |
let val {sign,t,...} = rep_cterm ctm |
|
749 |
val (Const("==>",_) $ tm $ _) = t |
|
750 |
val (Const("Trueprop",_) $ (Const("op =",_) $ _ $ Ma)) = tm |
|
751 |
val (Free(str,_)) = Ma |
|
752 |
val thm = prove_goalw_cterm[] ctm |
|
1897
71e51870cc9a
Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents:
1810
diff
changeset
|
753 |
(fn prems => |
71e51870cc9a
Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents:
1810
diff
changeset
|
754 |
let val simplify = asm_simp_tac(HOL_ss addsimps (prems@case_rewrites)) |
71e51870cc9a
Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents:
1810
diff
changeset
|
755 |
in [simp_tac (HOL_ss addsimps [hd prems]) 1, |
71e51870cc9a
Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents:
1810
diff
changeset
|
756 |
cut_inst_tac [("x",str)] (nchotomy RS spec) 1, |
71e51870cc9a
Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents:
1810
diff
changeset
|
757 |
REPEAT (etac disjE 1 THEN REPEAT (etac exE 1) THEN simplify 1), |
71e51870cc9a
Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents:
1810
diff
changeset
|
758 |
REPEAT (etac exE 1) THEN simplify 1 (* Get last disjunct *)] |
71e51870cc9a
Replaced prove_case_cong by Konrad Slinds optimized version.
berghofe
parents:
1810
diff
changeset
|
759 |
end) |
1668 | 760 |
in standard (thm RS eq_reflection) |
761 |
end |
|
762 |
handle _ => raise DTYPE_ERR{func="prove_case_cong",mesg="failed"}; |
|
763 |
||
764 |
||
765 |
(*--------------------------------------------------------------------------- |
|
766 |
* Structure of exhaustion theorem looks like this: |
|
767 |
* |
|
768 |
* !v. (EX y1..yi. v = C1 y1..yi) | ... | (EX y1..yj. v = Cn y1..yj) |
|
769 |
* |
|
770 |
* As for "build_case_cong", the input is the list of rules for the case |
|
771 |
* construct (the case "rewrites"). |
|
772 |
*---------------------------------------------------------------------------*) |
|
773 |
fun build_nchotomy sign case_rewrites = |
|
774 |
let val clauses = map concl case_rewrites |
|
775 |
val C_ybars = map (rand o #1 o dest_eq) clauses |
|
3265
8358e19d0d4c
Replaced Konrad's own add_term_names by the predefined one.
nipkow
parents:
3197
diff
changeset
|
776 |
val varnames = foldr add_term_names (C_ybars, []) |
1668 | 777 |
val vname = variant varnames "v" |
778 |
val ty = type_of (hd C_ybars) |
|
779 |
val v = mk_var(vname,ty) |
|
780 |
fun mk_disj C_ybar = |
|
781 |
let val ybar = #2(strip_comb C_ybar) |
|
782 |
in list_mk_exists(ybar, mk_eq(v,C_ybar)) |
|
783 |
end |
|
784 |
in |
|
785 |
cterm_of sign |
|
786 |
(make_free(mk_prop (mk_forall(v, list_mk_disj (map mk_disj C_ybars))))) |
|
787 |
end |
|
788 |
handle _ => raise DTYPE_ERR{func="build_nchotomy",mesg="failed"}; |
|
789 |
||
790 |
||
791 |
(*--------------------------------------------------------------------------- |
|
792 |
* Takes the induction tactic for the datatype, and the result from |
|
1690 | 793 |
* "build_nchotomy" |
794 |
* |
|
795 |
* !v. (EX y1..yi. v = C1 y1..yi) | ... | (EX y1..yj. v = Cn y1..yj) |
|
796 |
* |
|
797 |
* and proves the theorem. The proof works along a diagonal: the nth |
|
798 |
* disjunct in the nth subgoal is easy to solve. Thus this routine depends |
|
799 |
* on the order of goals arising out of the application of the induction |
|
800 |
* tactic. A more general solution would have to use injectiveness and |
|
801 |
* distinctness rewrite rules. |
|
1668 | 802 |
*---------------------------------------------------------------------------*) |
1690 | 803 |
fun prove_nchotomy induct_tac ctm = |
804 |
let val (Const ("Trueprop",_) $ g) = #t(rep_cterm ctm) |
|
1668 | 805 |
val (Const ("All",_) $ Abs (v,_,_)) = g |
1690 | 806 |
(* For goal i, select the correct disjunct to attack, then prove it *) |
807 |
fun tac i 0 = (rtac disjI1 i ORELSE all_tac) THEN |
|
808 |
REPEAT (rtac exI i) THEN (rtac refl i) |
|
809 |
| tac i n = rtac disjI2 i THEN tac i (n-1) |
|
1668 | 810 |
in |
811 |
prove_goalw_cterm[] ctm |
|
812 |
(fn _ => [rtac allI 1, |
|
813 |
induct_tac v 1, |
|
1690 | 814 |
ALLGOALS (fn i => tac i (i-1))]) |
1668 | 815 |
end |
816 |
handle _ => raise DTYPE_ERR {func="prove_nchotomy", mesg="failed"}; |
|
817 |
||
3282
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
818 |
(*--------------------------------------------------------------------------- |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
819 |
* Turn nchotomy into exhaustion: |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
820 |
* [| !!y1..yi. v = C1 y1..yi ==> P; ...; !!y1..yj. v = Cn y1..yj ==> P |] |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
821 |
* ==> P |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
822 |
*---------------------------------------------------------------------------*) |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
823 |
fun mk_exhaust nchotomy = |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
824 |
let val tac = rtac impI 1 THEN |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
825 |
REPEAT(SOMEGOAL(eresolve_tac [disjE,exE])) |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
826 |
in standard(rule_by_tactic tac (nchotomy RS spec RS rev_mp)) end; |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
827 |
|
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
828 |
(* find name of v in exhaustion: *) |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
829 |
fun exhaust_var thm = |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
830 |
let val _ $ ( _ $ Var((x,_),_) $ _ ) = |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
831 |
hd(Logic.strip_assums_hyp(hd(prems_of thm))) |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
832 |
in x end; |
1668 | 833 |
|
834 |
(*--------------------------------------------------------------------------- |
|
835 |
* Brings the preceeding functions together. |
|
836 |
*---------------------------------------------------------------------------*) |
|
837 |
fun case_thms sign case_rewrites induct_tac = |
|
1690 | 838 |
let val nchotomy = prove_nchotomy induct_tac |
839 |
(build_nchotomy sign case_rewrites) |
|
1668 | 840 |
val cong = prove_case_cong nchotomy case_rewrites |
841 |
(build_case_cong sign case_rewrites) |
|
842 |
in {nchotomy=nchotomy, case_cong=cong} |
|
843 |
end; |
|
844 |
||
1690 | 845 |
|
1668 | 846 |
(*--------------------------------------------------------------------------- |
847 |
* Tests |
|
848 |
* |
|
849 |
* |
|
850 |
Dtype.case_thms (sign_of List.thy) List.list.cases List.list.induct_tac; |
|
851 |
Dtype.case_thms (sign_of Prod.thy) [split] |
|
852 |
(fn s => res_inst_tac [("p",s)] PairE_lemma); |
|
853 |
Dtype.case_thms (sign_of Nat.thy) [nat_case_0, nat_case_Suc] nat_ind_tac; |
|
854 |
||
855 |
* |
|
856 |
*---------------------------------------------------------------------------*) |
|
857 |
||
858 |
||
859 |
(*--------------------------------------------------------------------------- |
|
860 |
* Given a theory and the name (and constructors) of a datatype declared in |
|
861 |
* an ancestor of that theory and an induction tactic for that datatype, |
|
862 |
* return the information that TFL needs. This should only be called once for |
|
863 |
* a datatype, because "build_record" proves various facts, and thus is slow. |
|
864 |
* It fails on the datatype of pairs, which must be included for TFL to work. |
|
865 |
* The test shows how to build the record for pairs. |
|
866 |
*---------------------------------------------------------------------------*) |
|
867 |
||
868 |
local fun mk_rw th = (th RS eq_reflection) handle _ => th |
|
869 |
fun get_fact thy s = (get_axiom thy s handle _ => get_thm thy s) |
|
870 |
in |
|
871 |
fun build_record (thy,(ty,cl),itac) = |
|
872 |
let val sign = sign_of thy |
|
873 |
fun const s = Const(s, the(Sign.const_type sign s)) |
|
874 |
val case_rewrites = map (fn c => get_fact thy (ty^"_case_"^c)) cl |
|
875 |
val {nchotomy,case_cong} = case_thms sign case_rewrites itac |
|
3282
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
876 |
val exhaustion = mk_exhaust nchotomy |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
877 |
val exh_var = exhaust_var exhaustion; |
3292 | 878 |
fun exhaust_tac a = |
879 |
ALLNEWSUBGOALS (res_inst_tac [(exh_var,a)] exhaustion) |
|
880 |
(rotate_tac ~1); |
|
3105 | 881 |
fun induct_tac a i = |
3111
00fb015d27aa
Stupid bug in induct_tac caused warning to always appear.
nipkow
parents:
3105
diff
changeset
|
882 |
STATE(fn st => |
00fb015d27aa
Stupid bug in induct_tac caused warning to always appear.
nipkow
parents:
3105
diff
changeset
|
883 |
(if Datatype.occs_in_prems a i st |
00fb015d27aa
Stupid bug in induct_tac caused warning to always appear.
nipkow
parents:
3105
diff
changeset
|
884 |
then warning "Induction variable occurs also among premises!" |
00fb015d27aa
Stupid bug in induct_tac caused warning to always appear.
nipkow
parents:
3105
diff
changeset
|
885 |
else (); |
00fb015d27aa
Stupid bug in induct_tac caused warning to always appear.
nipkow
parents:
3105
diff
changeset
|
886 |
itac a i)) |
1668 | 887 |
in |
888 |
(ty, {constructors = map(fn s => const s handle _ => const("op "^s)) cl, |
|
889 |
case_const = const (ty^"_case"), |
|
890 |
case_rewrites = map mk_rw case_rewrites, |
|
3040
7d48671753da
Introduced a generic "induct_tac" which picks up the right induction scheme
nipkow
parents:
2880
diff
changeset
|
891 |
induct_tac = induct_tac, |
1668 | 892 |
nchotomy = nchotomy, |
3282
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
893 |
exhaustion = exhaustion, |
c31e6239d4c9
Added exhaustion thm and exhaust_tac for each datatype.
nipkow
parents:
3265
diff
changeset
|
894 |
exhaust_tac = exhaust_tac, |
1668 | 895 |
case_cong = case_cong}) |
896 |
end |
|
897 |
end; |
|
898 |
||
899 |
||
900 |
(*--------------------------------------------------------------------------- |
|
901 |
* Test |
|
902 |
* |
|
903 |
* |
|
904 |
map Dtype.build_record |
|
905 |
[(Nat.thy, ("nat",["0", "Suc"]), nat_ind_tac), |
|
906 |
(List.thy,("list",["[]", "#"]), List.list.induct_tac)] |
|
907 |
@ |
|
908 |
[let val prod_case_thms = Dtype.case_thms (sign_of Prod.thy) [split] |
|
909 |
(fn s => res_inst_tac [("p",s)] PairE_lemma) |
|
910 |
fun const s = Const(s, the(Sign.const_type (sign_of Prod.thy) s)) |
|
911 |
in ("*", |
|
912 |
{constructors = [const "Pair"], |
|
913 |
case_const = const "split", |
|
914 |
case_rewrites = [split RS eq_reflection], |
|
915 |
case_cong = #case_cong prod_case_thms, |
|
916 |
nchotomy = #nchotomy prod_case_thms}) end]; |
|
917 |
||
918 |
* |
|
919 |
*---------------------------------------------------------------------------*) |
|
920 |
||
921 |
end; |