author | wenzelm |
Wed, 18 Jun 2008 18:55:10 +0200 | |
changeset 27261 | 5b3101338f42 |
parent 26939 | 1035c89b4c02 |
child 27691 | ce171cbd4b93 |
permissions | -rw-r--r-- |
6065 | 1 |
(* Title: ZF/Tools/datatype_package.ML |
6052 | 2 |
ID: $Id$ |
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1994 University of Cambridge |
|
5 |
||
6065 | 6 |
Datatype/Codatatype Definitions |
6052 | 7 |
|
8 |
The functor will be instantiated for normal sums/products (datatype defs) |
|
9 |
and non-standard sums/products (codatatype defs) |
|
10 |
||
11 |
Sums are used only for mutual recursion; |
|
12 |
Products are used only to derive "streamlined" induction rules for relations |
|
13 |
*) |
|
14 |
||
15 |
type datatype_result = |
|
16 |
{con_defs : thm list, (*definitions made in thy*) |
|
17 |
case_eqns : thm list, (*equations for case operator*) |
|
18 |
recursor_eqns : thm list, (*equations for the recursor*) |
|
19 |
free_iffs : thm list, (*freeness rewrite rules*) |
|
20 |
free_SEs : thm list, (*freeness destruct rules*) |
|
6112 | 21 |
mk_free : string -> thm}; (*function to make freeness theorems*) |
6052 | 22 |
|
23 |
signature DATATYPE_ARG = |
|
12183 | 24 |
sig |
6052 | 25 |
val intrs : thm list |
26 |
val elims : thm list |
|
12183 | 27 |
end; |
6052 | 28 |
|
29 |
signature DATATYPE_PACKAGE = |
|
12131 | 30 |
sig |
6052 | 31 |
(*Insert definitions for the recursive sets, which |
32 |
must *already* be declared as constants in parent theory!*) |
|
12183 | 33 |
val add_datatype_i: term * term list -> Ind_Syntax.constructor_spec list list -> |
34 |
thm list * thm list * thm list -> theory -> theory * inductive_result * datatype_result |
|
35 |
val add_datatype: string * string list -> (string * string list * mixfix) list list -> |
|
26336
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents:
26190
diff
changeset
|
36 |
(Facts.ref * Attrib.src list) list * (Facts.ref * Attrib.src list) list * |
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents:
26190
diff
changeset
|
37 |
(Facts.ref * Attrib.src list) list -> theory -> theory * inductive_result * datatype_result |
12131 | 38 |
end; |
6052 | 39 |
|
12131 | 40 |
functor Add_datatype_def_Fun |
12183 | 41 |
(structure Fp: FP and Pr : PR and CP: CARTPROD and Su : SU |
42 |
and Ind_Package : INDUCTIVE_PACKAGE |
|
43 |
and Datatype_Arg : DATATYPE_ARG |
|
44 |
val coind : bool): DATATYPE_PACKAGE = |
|
6052 | 45 |
struct |
46 |
||
12183 | 47 |
(*con_ty_lists specifies the constructors in the form (name, prems, mixfix) *) |
6052 | 48 |
|
26189 | 49 |
(*univ or quniv constitutes the sum domain for mutual recursion; |
50 |
it is applied to the datatype parameters and to Consts occurring in the |
|
51 |
definition other than Nat.nat and the datatype sets themselves. |
|
52 |
FIXME: could insert all constant set expressions, e.g. nat->nat.*) |
|
53 |
fun data_domain co (rec_tms, con_ty_lists) = |
|
54 |
let val rec_hds = map head_of rec_tms |
|
55 |
val dummy = assert_all is_Const rec_hds |
|
56 |
(fn t => "Datatype set not previously declared as constant: " ^ |
|
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26336
diff
changeset
|
57 |
Syntax.string_of_term_global @{theory IFOL} t); |
26189 | 58 |
val rec_names = (*nat doesn't have to be added*) |
59 |
@{const_name nat} :: map (#1 o dest_Const) rec_hds |
|
60 |
val u = if co then @{const QUniv.quniv} else @{const Univ.univ} |
|
61 |
val cs = (fold o fold) (fn (_, _, _, prems) => prems |> (fold o fold_aterms) |
|
62 |
(fn t as Const (a, _) => if a mem_string rec_names then I else insert (op =) t |
|
63 |
| _ => I)) con_ty_lists []; |
|
64 |
in u $ Ind_Syntax.union_params (hd rec_tms, cs) end; |
|
65 |
||
12183 | 66 |
fun add_datatype_i (dom_sum, rec_tms) con_ty_lists (monos, type_intrs, type_elims) thy = |
6052 | 67 |
let |
68 |
val dummy = (*has essential ancestors?*) |
|
26056
6a0801279f4c
Made theory names in ZF disjoint from HOL theory names to allow loading both developments
krauss
parents:
25985
diff
changeset
|
69 |
Theory.requires thy "Datatype_ZF" "(co)datatype definitions"; |
6052 | 70 |
|
13150
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
71 |
val rec_hds = map head_of rec_tms; |
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
72 |
|
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
73 |
val dummy = assert_all is_Const rec_hds |
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
74 |
(fn t => "Datatype set not previously declared as constant: " ^ |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26336
diff
changeset
|
75 |
Syntax.string_of_term_global thy t); |
13150
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
76 |
|
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
77 |
val rec_names = map (#1 o dest_Const) rec_hds |
6052 | 78 |
val rec_base_names = map Sign.base_name rec_names |
79 |
val big_rec_base_name = space_implode "_" rec_base_names |
|
80 |
||
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
81 |
val thy_path = thy |> Sign.add_path big_rec_base_name |
6052 | 82 |
|
22578 | 83 |
val big_rec_name = Sign.intern_const thy_path big_rec_base_name; |
6052 | 84 |
|
22578 | 85 |
val intr_tms = Ind_Syntax.mk_all_intr_tms thy_path (rec_tms, con_ty_lists); |
6052 | 86 |
|
12131 | 87 |
val dummy = |
12243 | 88 |
writeln ((if coind then "Codatatype" else "Datatype") ^ " definition " ^ quote big_rec_name); |
6052 | 89 |
|
90 |
val case_varname = "f"; (*name for case variables*) |
|
91 |
||
92 |
(** Define the constructors **) |
|
93 |
||
94 |
(*The empty tuple is 0*) |
|
26190 | 95 |
fun mk_tuple [] = @{const "0"} |
7696 | 96 |
| mk_tuple args = foldr1 (fn (t1, t2) => Pr.pair $ t1 $ t2) args; |
6052 | 97 |
|
23419 | 98 |
fun mk_inject n k u = BalancedTree.access |
99 |
{left = fn t => Su.inl $ t, right = fn t => Su.inr $ t, init = u} n k; |
|
6052 | 100 |
|
101 |
val npart = length rec_names; (*number of mutually recursive parts*) |
|
102 |
||
103 |
||
22578 | 104 |
val full_name = Sign.full_name thy_path; |
6052 | 105 |
|
12131 | 106 |
(*Make constructor definition; |
6052 | 107 |
kpart is the number of this mutually recursive part*) |
12131 | 108 |
fun mk_con_defs (kpart, con_ty_list) = |
6052 | 109 |
let val ncon = length con_ty_list (*number of constructors*) |
12131 | 110 |
fun mk_def (((id,T,syn), name, args, prems), kcon) = |
111 |
(*kcon is index of constructor*) |
|
24255 | 112 |
PrimitiveDefs.mk_defpair (list_comb (Const (full_name name, T), args), |
12131 | 113 |
mk_inject npart kpart |
114 |
(mk_inject ncon kcon (mk_tuple args))) |
|
6052 | 115 |
in ListPair.map mk_def (con_ty_list, 1 upto ncon) end; |
116 |
||
117 |
||
118 |
(*** Define the case operator ***) |
|
119 |
||
120 |
(*Combine split terms using case; yields the case operator for one part*) |
|
12131 | 121 |
fun call_case case_list = |
26190 | 122 |
let fun call_f (free,[]) = Abs("null", @{typ i}, free) |
12131 | 123 |
| call_f (free,args) = |
124 |
CP.ap_split (foldr1 CP.mk_prod (map (#2 o dest_Free) args)) |
|
26190 | 125 |
@{typ i} |
12131 | 126 |
free |
23419 | 127 |
in BalancedTree.make (fn (t1, t2) => Su.elim $ t1 $ t2) (map call_f case_list) end; |
6052 | 128 |
|
129 |
(** Generating function variables for the case definition |
|
130 |
Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **) |
|
131 |
||
132 |
(*The function variable for a single constructor*) |
|
133 |
fun add_case (((_, T, _), name, args, _), (opno, cases)) = |
|
134 |
if Syntax.is_identifier name then |
|
135 |
(opno, (Free (case_varname ^ "_" ^ name, T), args) :: cases) |
|
136 |
else |
|
12131 | 137 |
(opno + 1, (Free (case_varname ^ "_op_" ^ string_of_int opno, T), args) |
6052 | 138 |
:: cases); |
139 |
||
140 |
(*Treatment of a list of constructors, for one part |
|
141 |
Result adds a list of terms, each a function variable with arguments*) |
|
142 |
fun add_case_list (con_ty_list, (opno, case_lists)) = |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
143 |
let val (opno', case_list) = foldr add_case (opno, []) con_ty_list |
6052 | 144 |
in (opno', case_list :: case_lists) end; |
145 |
||
146 |
(*Treatment of all parts*) |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
147 |
val (_, case_lists) = foldr add_case_list (1,[]) con_ty_lists; |
6052 | 148 |
|
149 |
(*extract the types of all the variables*) |
|
26190 | 150 |
val case_typ = List.concat (map (map (#2 o #1)) con_ty_lists) ---> @{typ "i => i"}; |
6052 | 151 |
|
152 |
val case_base_name = big_rec_base_name ^ "_case"; |
|
153 |
val case_name = full_name case_base_name; |
|
154 |
||
155 |
(*The list of all the function variables*) |
|
15570 | 156 |
val case_args = List.concat (map (map #1) case_lists); |
6052 | 157 |
|
12131 | 158 |
val case_const = Const (case_name, case_typ); |
6052 | 159 |
val case_tm = list_comb (case_const, case_args); |
160 |
||
24255 | 161 |
val case_def = PrimitiveDefs.mk_defpair |
23419 | 162 |
(case_tm, BalancedTree.make (fn (t1, t2) => Su.elim $ t1 $ t2) (map call_case case_lists)); |
6052 | 163 |
|
164 |
||
165 |
(** Generating function variables for the recursor definition |
|
166 |
Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **) |
|
167 |
||
168 |
(*a recursive call for x is the application rec`x *) |
|
26190 | 169 |
val rec_call = @{const apply} $ Free ("rec", @{typ i}); |
6052 | 170 |
|
12131 | 171 |
(*look back down the "case args" (which have been reversed) to |
6052 | 172 |
determine the de Bruijn index*) |
173 |
fun make_rec_call ([], _) arg = error |
|
12131 | 174 |
"Internal error in datatype (variable name mismatch)" |
175 |
| make_rec_call (a::args, i) arg = |
|
176 |
if a = arg then rec_call $ Bound i |
|
177 |
else make_rec_call (args, i+1) arg; |
|
6052 | 178 |
|
179 |
(*creates one case of the "X_case" definition of the recursor*) |
|
12131 | 180 |
fun call_recursor ((case_var, case_args), (recursor_var, recursor_args)) = |
6052 | 181 |
let fun add_abs (Free(a,T), u) = Abs(a,T,u) |
12131 | 182 |
val ncase_args = length case_args |
183 |
val bound_args = map Bound ((ncase_args - 1) downto 0) |
|
184 |
val rec_args = map (make_rec_call (rev case_args,0)) |
|
185 |
(List.drop(recursor_args, ncase_args)) |
|
6052 | 186 |
in |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
187 |
foldr add_abs |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
188 |
(list_comb (recursor_var, |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
189 |
bound_args @ rec_args)) case_args |
6052 | 190 |
end |
191 |
||
192 |
(*Find each recursive argument and add a recursive call for it*) |
|
193 |
fun rec_args [] = [] |
|
24826 | 194 |
| rec_args ((Const(@{const_name mem},_)$arg$X)::prems) = |
6052 | 195 |
(case head_of X of |
12131 | 196 |
Const(a,_) => (*recursive occurrence?*) |
197 |
if a mem_string rec_names |
|
198 |
then arg :: rec_args prems |
|
199 |
else rec_args prems |
|
200 |
| _ => rec_args prems) |
|
201 |
| rec_args (_::prems) = rec_args prems; |
|
6052 | 202 |
|
203 |
(*Add an argument position for each occurrence of a recursive set. |
|
204 |
Strictly speaking, the recursive arguments are the LAST of the function |
|
205 |
variable, but they all have type "i" anyway*) |
|
26190 | 206 |
fun add_rec_args args' T = (map (fn _ => @{typ i}) args') ---> T |
6052 | 207 |
|
208 |
(*Plug in the function variable type needed for the recursor |
|
209 |
as well as the new arguments (recursive calls)*) |
|
210 |
fun rec_ty_elem ((id, T, syn), name, args, prems) = |
|
12131 | 211 |
let val args' = rec_args prems |
212 |
in ((id, add_rec_args args' T, syn), |
|
213 |
name, args @ args', prems) |
|
6052 | 214 |
end; |
215 |
||
12131 | 216 |
val rec_ty_lists = (map (map rec_ty_elem) con_ty_lists); |
6052 | 217 |
|
218 |
(*Treatment of all parts*) |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
219 |
val (_, recursor_lists) = foldr add_case_list (1,[]) rec_ty_lists; |
6052 | 220 |
|
221 |
(*extract the types of all the variables*) |
|
26190 | 222 |
val recursor_typ = List.concat (map (map (#2 o #1)) rec_ty_lists) ---> @{typ "i => i"}; |
6052 | 223 |
|
224 |
val recursor_base_name = big_rec_base_name ^ "_rec"; |
|
225 |
val recursor_name = full_name recursor_base_name; |
|
226 |
||
227 |
(*The list of all the function variables*) |
|
15570 | 228 |
val recursor_args = List.concat (map (map #1) recursor_lists); |
6052 | 229 |
|
230 |
val recursor_tm = |
|
12131 | 231 |
list_comb (Const (recursor_name, recursor_typ), recursor_args); |
6052 | 232 |
|
12131 | 233 |
val recursor_cases = map call_recursor |
15570 | 234 |
(List.concat case_lists ~~ List.concat recursor_lists) |
6052 | 235 |
|
12131 | 236 |
val recursor_def = |
24255 | 237 |
PrimitiveDefs.mk_defpair |
12131 | 238 |
(recursor_tm, |
26189 | 239 |
@{const Univ.Vrecursor} $ |
26190 | 240 |
absfree ("rec", @{typ i}, list_comb (case_const, recursor_cases))); |
6052 | 241 |
|
242 |
(* Build the new theory *) |
|
243 |
||
12183 | 244 |
val need_recursor = (not coind andalso recursor_typ <> case_typ); |
6052 | 245 |
|
12131 | 246 |
fun add_recursor thy = |
6052 | 247 |
if need_recursor then |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
248 |
thy |> Sign.add_consts_i |
12131 | 249 |
[(recursor_base_name, recursor_typ, NoSyn)] |
18358 | 250 |
|> (snd o PureThy.add_defs_i false [Thm.no_attributes recursor_def]) |
6052 | 251 |
else thy; |
252 |
||
18358 | 253 |
val (con_defs, thy0) = thy_path |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
254 |
|> Sign.add_consts_i |
12131 | 255 |
((case_base_name, case_typ, NoSyn) :: |
15570 | 256 |
map #1 (List.concat con_ty_lists)) |
12131 | 257 |
|> PureThy.add_defs_i false |
258 |
(map Thm.no_attributes |
|
259 |
(case_def :: |
|
15570 | 260 |
List.concat (ListPair.map mk_con_defs |
12131 | 261 |
(1 upto npart, con_ty_lists)))) |
18358 | 262 |
||> add_recursor |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
263 |
||> Sign.parent_path |
6052 | 264 |
|
15570 | 265 |
val intr_names = map #2 (List.concat con_ty_lists); |
12131 | 266 |
val (thy1, ind_result) = |
12187 | 267 |
thy0 |> Ind_Package.add_inductive_i |
268 |
false (rec_tms, dom_sum) (map Thm.no_attributes (intr_names ~~ intr_tms)) |
|
269 |
(monos, con_defs, type_intrs @ Datatype_Arg.intrs, type_elims @ Datatype_Arg.elims); |
|
6052 | 270 |
|
271 |
(**** Now prove the datatype theorems in this theory ****) |
|
272 |
||
273 |
||
274 |
(*** Prove the case theorems ***) |
|
275 |
||
12131 | 276 |
(*Each equation has the form |
6052 | 277 |
case(f_con1,...,f_conn)(coni(args)) = f_coni(args) *) |
12131 | 278 |
fun mk_case_eqn (((_,T,_), name, args, _), case_free) = |
6052 | 279 |
FOLogic.mk_Trueprop |
280 |
(FOLogic.mk_eq |
|
281 |
(case_tm $ |
|
22578 | 282 |
(list_comb (Const (Sign.intern_const thy1 name,T), |
12131 | 283 |
args)), |
284 |
list_comb (case_free, args))); |
|
6052 | 285 |
|
26189 | 286 |
val case_trans = hd con_defs RS @{thm def_trans} |
287 |
and split_trans = Pr.split_eq RS meta_eq_to_obj_eq RS @{thm trans}; |
|
6052 | 288 |
|
17985 | 289 |
fun prove_case_eqn (arg, con_def) = |
20046 | 290 |
Goal.prove_global thy1 [] [] |
17985 | 291 |
(Ind_Syntax.traceIt "next case equation = " thy1 (mk_case_eqn arg)) |
292 |
(*Proves a single case equation. Could use simp_tac, but it's slower!*) |
|
293 |
(fn _ => EVERY |
|
294 |
[rewtac con_def, |
|
295 |
rtac case_trans 1, |
|
20046 | 296 |
REPEAT (resolve_tac [refl, split_trans, Su.case_inl RS trans, Su.case_inr RS trans] 1)]); |
6052 | 297 |
|
26189 | 298 |
val free_iffs = map standard (con_defs RL [@{thm def_swap_iff}]); |
6052 | 299 |
|
12131 | 300 |
val case_eqns = |
301 |
map prove_case_eqn |
|
15570 | 302 |
(List.concat con_ty_lists ~~ case_args ~~ tl con_defs); |
6052 | 303 |
|
304 |
(*** Prove the recursor theorems ***) |
|
305 |
||
306 |
val recursor_eqns = case try (get_def thy1) recursor_base_name of |
|
15531 | 307 |
NONE => (writeln " [ No recursion operator ]"; |
12131 | 308 |
[]) |
15531 | 309 |
| SOME recursor_def => |
6052 | 310 |
let |
12131 | 311 |
(*Replace subterms rec`x (where rec is a Free var) by recursor_tm(x) *) |
24826 | 312 |
fun subst_rec (Const(@{const_name apply},_) $ Free _ $ arg) = recursor_tm $ arg |
12131 | 313 |
| subst_rec tm = |
314 |
let val (head, args) = strip_comb tm |
|
315 |
in list_comb (head, map subst_rec args) end; |
|
6052 | 316 |
|
12131 | 317 |
(*Each equation has the form |
318 |
REC(coni(args)) = f_coni(args, REC(rec_arg), ...) |
|
319 |
where REC = recursor(f_con1,...,f_conn) and rec_arg is a recursive |
|
320 |
constructor argument.*) |
|
321 |
fun mk_recursor_eqn (((_,T,_), name, args, _), recursor_case) = |
|
322 |
FOLogic.mk_Trueprop |
|
323 |
(FOLogic.mk_eq |
|
324 |
(recursor_tm $ |
|
22578 | 325 |
(list_comb (Const (Sign.intern_const thy1 name,T), |
12131 | 326 |
args)), |
18185 | 327 |
subst_rec (Term.betapplys (recursor_case, args)))); |
6052 | 328 |
|
24893 | 329 |
val recursor_trans = recursor_def RS @{thm def_Vrecursor} RS trans; |
6052 | 330 |
|
12131 | 331 |
fun prove_recursor_eqn arg = |
20046 | 332 |
Goal.prove_global thy1 [] [] |
17985 | 333 |
(Ind_Syntax.traceIt "next recursor equation = " thy1 (mk_recursor_eqn arg)) |
334 |
(*Proves a single recursor equation.*) |
|
335 |
(fn _ => EVERY |
|
336 |
[rtac recursor_trans 1, |
|
337 |
simp_tac (rank_ss addsimps case_eqns) 1, |
|
20046 | 338 |
IF_UNSOLVED (simp_tac (rank_ss addsimps tl con_defs) 1)]); |
6052 | 339 |
in |
15570 | 340 |
map prove_recursor_eqn (List.concat con_ty_lists ~~ recursor_cases) |
6052 | 341 |
end |
342 |
||
343 |
val constructors = |
|
344 |
map (head_of o #1 o Logic.dest_equals o #prop o rep_thm) (tl con_defs); |
|
345 |
||
12187 | 346 |
val free_SEs = map standard (Ind_Syntax.mk_free_SEs free_iffs); |
6052 | 347 |
|
6154
6a00a5baef2b
automatic insertion of datatype intr rules into claset
paulson
parents:
6141
diff
changeset
|
348 |
val {intrs, elim, induct, mutual_induct, ...} = ind_result |
6052 | 349 |
|
350 |
(*Typical theorems have the form ~con1=con2, con1=con2==>False, |
|
351 |
con1(x)=con1(y) ==> x=y, con1(x)=con1(y) <-> x=y, etc. *) |
|
352 |
fun mk_free s = |
|
17985 | 353 |
let val thy = theory_of_thm elim in (*Don't use thy1: it will be stale*) |
24707 | 354 |
Goal.prove_global thy [] [] (Syntax.read_prop_global thy s) |
17985 | 355 |
(fn _ => EVERY |
356 |
[rewrite_goals_tac con_defs, |
|
20046 | 357 |
fast_tac (ZF_cs addSEs free_SEs @ Su.free_SEs) 1]) |
17985 | 358 |
end; |
6052 | 359 |
|
360 |
val simps = case_eqns @ recursor_eqns; |
|
361 |
||
362 |
val dt_info = |
|
12131 | 363 |
{inductive = true, |
364 |
constructors = constructors, |
|
365 |
rec_rewrites = recursor_eqns, |
|
366 |
case_rewrites = case_eqns, |
|
367 |
induct = induct, |
|
368 |
mutual_induct = mutual_induct, |
|
369 |
exhaustion = elim}; |
|
6052 | 370 |
|
371 |
val con_info = |
|
372 |
{big_rec_name = big_rec_name, |
|
12131 | 373 |
constructors = constructors, |
6052 | 374 |
(*let primrec handle definition by cases*) |
12131 | 375 |
free_iffs = free_iffs, |
376 |
rec_rewrites = (case recursor_eqns of |
|
377 |
[] => case_eqns | _ => recursor_eqns)}; |
|
6052 | 378 |
|
379 |
(*associate with each constructor the datatype name and rewrites*) |
|
380 |
val con_pairs = map (fn c => (#1 (dest_Const c), con_info)) constructors |
|
381 |
||
382 |
in |
|
383 |
(*Updating theory components: simprules and datatype info*) |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
384 |
(thy1 |> Sign.add_path big_rec_base_name |
18377 | 385 |
|> PureThy.add_thmss |
18728 | 386 |
[(("simps", simps), [Simplifier.simp_add]), |
387 |
(("", intrs), [Classical.safe_intro NONE]), |
|
12187 | 388 |
(("con_defs", con_defs), []), |
389 |
(("case_eqns", case_eqns), []), |
|
390 |
(("recursor_eqns", recursor_eqns), []), |
|
391 |
(("free_iffs", free_iffs), []), |
|
18377 | 392 |
(("free_elims", free_SEs), [])] |> snd |
17412 | 393 |
|> DatatypesData.map (Symtab.update (big_rec_name, dt_info)) |
394 |
|> ConstructorsData.map (fold Symtab.update con_pairs) |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
395 |
|> Sign.parent_path, |
6052 | 396 |
ind_result, |
397 |
{con_defs = con_defs, |
|
398 |
case_eqns = case_eqns, |
|
399 |
recursor_eqns = recursor_eqns, |
|
400 |
free_iffs = free_iffs, |
|
401 |
free_SEs = free_SEs, |
|
402 |
mk_free = mk_free}) |
|
403 |
end; |
|
404 |
||
17936 | 405 |
fun add_datatype (sdom, srec_tms) scon_ty_lists (raw_monos, raw_type_intrs, raw_type_elims) thy = |
12183 | 406 |
let |
24725 | 407 |
val ctxt = ProofContext.init thy; |
27261
5b3101338f42
eliminated old Sign.read_term/Thm.read_cterm etc.;
wenzelm
parents:
26939
diff
changeset
|
408 |
fun read_is strs = |
5b3101338f42
eliminated old Sign.read_term/Thm.read_cterm etc.;
wenzelm
parents:
26939
diff
changeset
|
409 |
map (Syntax.parse_term ctxt #> TypeInfer.constrain @{typ i}) strs |
5b3101338f42
eliminated old Sign.read_term/Thm.read_cterm etc.;
wenzelm
parents:
26939
diff
changeset
|
410 |
|> Syntax.check_terms ctxt; |
5b3101338f42
eliminated old Sign.read_term/Thm.read_cterm etc.;
wenzelm
parents:
26939
diff
changeset
|
411 |
|
5b3101338f42
eliminated old Sign.read_term/Thm.read_cterm etc.;
wenzelm
parents:
26939
diff
changeset
|
412 |
val rec_tms = read_is srec_tms; |
5b3101338f42
eliminated old Sign.read_term/Thm.read_cterm etc.;
wenzelm
parents:
26939
diff
changeset
|
413 |
val con_ty_lists = Ind_Syntax.read_constructs ctxt scon_ty_lists; |
12183 | 414 |
val dom_sum = |
26189 | 415 |
if sdom = "" then data_domain coind (rec_tms, con_ty_lists) |
27261
5b3101338f42
eliminated old Sign.read_term/Thm.read_cterm etc.;
wenzelm
parents:
26939
diff
changeset
|
416 |
else singleton read_is sdom; |
24725 | 417 |
val monos = Attrib.eval_thms ctxt raw_monos; |
418 |
val type_intrs = Attrib.eval_thms ctxt raw_type_intrs; |
|
419 |
val type_elims = Attrib.eval_thms ctxt raw_type_elims; |
|
420 |
in add_datatype_i (dom_sum, rec_tms) con_ty_lists (monos, type_intrs, type_elims) thy end; |
|
421 |
||
12183 | 422 |
|
423 |
(* outer syntax *) |
|
424 |
||
17057 | 425 |
local structure P = OuterParse and K = OuterKeyword in |
12183 | 426 |
|
427 |
fun mk_datatype ((((dom, dts), monos), type_intrs), type_elims) = |
|
428 |
#1 o add_datatype (dom, map fst dts) (map snd dts) (monos, type_intrs, type_elims); |
|
429 |
||
430 |
val con_decl = |
|
431 |
P.name -- Scan.optional (P.$$$ "(" |-- P.list1 P.term --| P.$$$ ")") [] -- P.opt_mixfix |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12243
diff
changeset
|
432 |
>> P.triple1; |
12183 | 433 |
|
434 |
val datatype_decl = |
|
25985 | 435 |
(Scan.optional ((P.$$$ "\<subseteq>" || P.$$$ "<=") |-- P.!!! P.term) "") -- |
12183 | 436 |
P.and_list1 (P.term -- (P.$$$ "=" |-- P.enum1 "|" con_decl)) -- |
22101 | 437 |
Scan.optional (P.$$$ "monos" |-- P.!!! SpecParse.xthms1) [] -- |
438 |
Scan.optional (P.$$$ "type_intros" |-- P.!!! SpecParse.xthms1) [] -- |
|
439 |
Scan.optional (P.$$$ "type_elims" |-- P.!!! SpecParse.xthms1) [] |
|
12183 | 440 |
>> (Toplevel.theory o mk_datatype); |
441 |
||
442 |
val coind_prefix = if coind then "co" else ""; |
|
443 |
||
24867 | 444 |
val _ = OuterSyntax.command (coind_prefix ^ "datatype") |
12183 | 445 |
("define " ^ coind_prefix ^ "datatype") K.thy_decl datatype_decl; |
446 |
||
6052 | 447 |
end; |
12183 | 448 |
|
449 |
end; |
|
15705 | 450 |