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