author | wenzelm |
Wed, 19 Oct 2005 21:52:49 +0200 | |
changeset 17936 | 308b19d78764 |
parent 17412 | e26cb20ef0cc |
child 17959 | 8db36a108213 |
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 -> |
|
15703 | 36 |
(thmref * Attrib.src list) list * (thmref * Attrib.src list) list * |
37 |
(thmref * 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 |
|
12183 | 49 |
fun add_datatype_i (dom_sum, rec_tms) con_ty_lists (monos, type_intrs, type_elims) thy = |
6052 | 50 |
let |
51 |
val dummy = (*has essential ancestors?*) |
|
52 |
Theory.requires thy "Datatype" "(co)datatype definitions"; |
|
53 |
||
13150
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
54 |
val rec_hds = map head_of rec_tms; |
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
55 |
|
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
56 |
val dummy = assert_all is_Const rec_hds |
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
57 |
(fn t => "Datatype set not previously declared as constant: " ^ |
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
58 |
Sign.string_of_term (sign_of thy) t); |
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
59 |
|
0c50d13d449a
better error messages for datatypes not declared Const
paulson
parents:
12876
diff
changeset
|
60 |
val rec_names = map (#1 o dest_Const) rec_hds |
6052 | 61 |
val rec_base_names = map Sign.base_name rec_names |
62 |
val big_rec_base_name = space_implode "_" rec_base_names |
|
63 |
||
64 |
val thy_path = thy |> Theory.add_path big_rec_base_name |
|
65 |
val sign = sign_of thy_path |
|
66 |
||
67 |
val big_rec_name = Sign.intern_const sign big_rec_base_name; |
|
68 |
||
12131 | 69 |
val intr_tms = Ind_Syntax.mk_all_intr_tms sign (rec_tms, con_ty_lists); |
6052 | 70 |
|
12131 | 71 |
val dummy = |
12243 | 72 |
writeln ((if coind then "Codatatype" else "Datatype") ^ " definition " ^ quote big_rec_name); |
6052 | 73 |
|
74 |
val case_varname = "f"; (*name for case variables*) |
|
75 |
||
76 |
(** Define the constructors **) |
|
77 |
||
78 |
(*The empty tuple is 0*) |
|
79 |
fun mk_tuple [] = Const("0",iT) |
|
7696 | 80 |
| mk_tuple args = foldr1 (fn (t1, t2) => Pr.pair $ t1 $ t2) args; |
6052 | 81 |
|
7696 | 82 |
fun mk_inject n k u = access_bal (fn t => Su.inl $ t, fn t => Su.inr $ t, u) n k; |
6052 | 83 |
|
84 |
val npart = length rec_names; (*number of mutually recursive parts*) |
|
85 |
||
86 |
||
87 |
val full_name = Sign.full_name sign; |
|
88 |
||
12131 | 89 |
(*Make constructor definition; |
6052 | 90 |
kpart is the number of this mutually recursive part*) |
12131 | 91 |
fun mk_con_defs (kpart, con_ty_list) = |
6052 | 92 |
let val ncon = length con_ty_list (*number of constructors*) |
12131 | 93 |
fun mk_def (((id,T,syn), name, args, prems), kcon) = |
94 |
(*kcon is index of constructor*) |
|
95 |
Logic.mk_defpair (list_comb (Const (full_name name, T), args), |
|
96 |
mk_inject npart kpart |
|
97 |
(mk_inject ncon kcon (mk_tuple args))) |
|
6052 | 98 |
in ListPair.map mk_def (con_ty_list, 1 upto ncon) end; |
99 |
||
100 |
||
101 |
(*** Define the case operator ***) |
|
102 |
||
103 |
(*Combine split terms using case; yields the case operator for one part*) |
|
12131 | 104 |
fun call_case case_list = |
6052 | 105 |
let fun call_f (free,[]) = Abs("null", iT, free) |
12131 | 106 |
| call_f (free,args) = |
107 |
CP.ap_split (foldr1 CP.mk_prod (map (#2 o dest_Free) args)) |
|
108 |
Ind_Syntax.iT |
|
109 |
free |
|
7696 | 110 |
in fold_bal (fn (t1, t2) => Su.elim $ t1 $ t2) (map call_f case_list) end; |
6052 | 111 |
|
112 |
(** Generating function variables for the case definition |
|
113 |
Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **) |
|
114 |
||
115 |
(*The function variable for a single constructor*) |
|
116 |
fun add_case (((_, T, _), name, args, _), (opno, cases)) = |
|
117 |
if Syntax.is_identifier name then |
|
118 |
(opno, (Free (case_varname ^ "_" ^ name, T), args) :: cases) |
|
119 |
else |
|
12131 | 120 |
(opno + 1, (Free (case_varname ^ "_op_" ^ string_of_int opno, T), args) |
6052 | 121 |
:: cases); |
122 |
||
123 |
(*Treatment of a list of constructors, for one part |
|
124 |
Result adds a list of terms, each a function variable with arguments*) |
|
125 |
fun add_case_list (con_ty_list, (opno, case_lists)) = |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
126 |
let val (opno', case_list) = foldr add_case (opno, []) con_ty_list |
6052 | 127 |
in (opno', case_list :: case_lists) end; |
128 |
||
129 |
(*Treatment of all parts*) |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
130 |
val (_, case_lists) = foldr add_case_list (1,[]) con_ty_lists; |
6052 | 131 |
|
132 |
(*extract the types of all the variables*) |
|
15570 | 133 |
val case_typ = List.concat (map (map (#2 o #1)) con_ty_lists) ---> (iT-->iT); |
6052 | 134 |
|
135 |
val case_base_name = big_rec_base_name ^ "_case"; |
|
136 |
val case_name = full_name case_base_name; |
|
137 |
||
138 |
(*The list of all the function variables*) |
|
15570 | 139 |
val case_args = List.concat (map (map #1) case_lists); |
6052 | 140 |
|
12131 | 141 |
val case_const = Const (case_name, case_typ); |
6052 | 142 |
val case_tm = list_comb (case_const, case_args); |
143 |
||
144 |
val case_def = Logic.mk_defpair |
|
7696 | 145 |
(case_tm, fold_bal (fn (t1, t2) => Su.elim $ t1 $ t2) (map call_case case_lists)); |
6052 | 146 |
|
147 |
||
148 |
(** Generating function variables for the recursor definition |
|
149 |
Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **) |
|
150 |
||
151 |
(*a recursive call for x is the application rec`x *) |
|
152 |
val rec_call = Ind_Syntax.apply_const $ Free ("rec", iT); |
|
153 |
||
12131 | 154 |
(*look back down the "case args" (which have been reversed) to |
6052 | 155 |
determine the de Bruijn index*) |
156 |
fun make_rec_call ([], _) arg = error |
|
12131 | 157 |
"Internal error in datatype (variable name mismatch)" |
158 |
| make_rec_call (a::args, i) arg = |
|
159 |
if a = arg then rec_call $ Bound i |
|
160 |
else make_rec_call (args, i+1) arg; |
|
6052 | 161 |
|
162 |
(*creates one case of the "X_case" definition of the recursor*) |
|
12131 | 163 |
fun call_recursor ((case_var, case_args), (recursor_var, recursor_args)) = |
6052 | 164 |
let fun add_abs (Free(a,T), u) = Abs(a,T,u) |
12131 | 165 |
val ncase_args = length case_args |
166 |
val bound_args = map Bound ((ncase_args - 1) downto 0) |
|
167 |
val rec_args = map (make_rec_call (rev case_args,0)) |
|
168 |
(List.drop(recursor_args, ncase_args)) |
|
6052 | 169 |
in |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
170 |
foldr add_abs |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
171 |
(list_comb (recursor_var, |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
172 |
bound_args @ rec_args)) case_args |
6052 | 173 |
end |
174 |
||
175 |
(*Find each recursive argument and add a recursive call for it*) |
|
176 |
fun rec_args [] = [] |
|
177 |
| rec_args ((Const("op :",_)$arg$X)::prems) = |
|
178 |
(case head_of X of |
|
12131 | 179 |
Const(a,_) => (*recursive occurrence?*) |
180 |
if a mem_string rec_names |
|
181 |
then arg :: rec_args prems |
|
182 |
else rec_args prems |
|
183 |
| _ => rec_args prems) |
|
184 |
| rec_args (_::prems) = rec_args prems; |
|
6052 | 185 |
|
186 |
(*Add an argument position for each occurrence of a recursive set. |
|
187 |
Strictly speaking, the recursive arguments are the LAST of the function |
|
188 |
variable, but they all have type "i" anyway*) |
|
189 |
fun add_rec_args args' T = (map (fn _ => iT) args') ---> T |
|
190 |
||
191 |
(*Plug in the function variable type needed for the recursor |
|
192 |
as well as the new arguments (recursive calls)*) |
|
193 |
fun rec_ty_elem ((id, T, syn), name, args, prems) = |
|
12131 | 194 |
let val args' = rec_args prems |
195 |
in ((id, add_rec_args args' T, syn), |
|
196 |
name, args @ args', prems) |
|
6052 | 197 |
end; |
198 |
||
12131 | 199 |
val rec_ty_lists = (map (map rec_ty_elem) con_ty_lists); |
6052 | 200 |
|
201 |
(*Treatment of all parts*) |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
202 |
val (_, recursor_lists) = foldr add_case_list (1,[]) rec_ty_lists; |
6052 | 203 |
|
204 |
(*extract the types of all the variables*) |
|
15570 | 205 |
val recursor_typ = List.concat (map (map (#2 o #1)) rec_ty_lists) |
12131 | 206 |
---> (iT-->iT); |
6052 | 207 |
|
208 |
val recursor_base_name = big_rec_base_name ^ "_rec"; |
|
209 |
val recursor_name = full_name recursor_base_name; |
|
210 |
||
211 |
(*The list of all the function variables*) |
|
15570 | 212 |
val recursor_args = List.concat (map (map #1) recursor_lists); |
6052 | 213 |
|
214 |
val recursor_tm = |
|
12131 | 215 |
list_comb (Const (recursor_name, recursor_typ), recursor_args); |
6052 | 216 |
|
12131 | 217 |
val recursor_cases = map call_recursor |
15570 | 218 |
(List.concat case_lists ~~ List.concat recursor_lists) |
6052 | 219 |
|
12131 | 220 |
val recursor_def = |
6052 | 221 |
Logic.mk_defpair |
12131 | 222 |
(recursor_tm, |
223 |
Ind_Syntax.Vrecursor_const $ |
|
224 |
absfree ("rec", iT, list_comb (case_const, recursor_cases))); |
|
6052 | 225 |
|
226 |
(* Build the new theory *) |
|
227 |
||
12183 | 228 |
val need_recursor = (not coind andalso recursor_typ <> case_typ); |
6052 | 229 |
|
12131 | 230 |
fun add_recursor thy = |
6052 | 231 |
if need_recursor then |
12131 | 232 |
thy |> Theory.add_consts_i |
233 |
[(recursor_base_name, recursor_typ, NoSyn)] |
|
234 |
|> (#1 o PureThy.add_defs_i false [Thm.no_attributes recursor_def]) |
|
6052 | 235 |
else thy; |
236 |
||
8438 | 237 |
val (thy0, con_defs) = thy_path |
12131 | 238 |
|> Theory.add_consts_i |
239 |
((case_base_name, case_typ, NoSyn) :: |
|
15570 | 240 |
map #1 (List.concat con_ty_lists)) |
12131 | 241 |
|> PureThy.add_defs_i false |
242 |
(map Thm.no_attributes |
|
243 |
(case_def :: |
|
15570 | 244 |
List.concat (ListPair.map mk_con_defs |
12131 | 245 |
(1 upto npart, con_ty_lists)))) |
246 |
|>> add_recursor |
|
247 |
|>> Theory.parent_path |
|
6052 | 248 |
|
15570 | 249 |
val intr_names = map #2 (List.concat con_ty_lists); |
12131 | 250 |
val (thy1, ind_result) = |
12187 | 251 |
thy0 |> Ind_Package.add_inductive_i |
252 |
false (rec_tms, dom_sum) (map Thm.no_attributes (intr_names ~~ intr_tms)) |
|
253 |
(monos, con_defs, type_intrs @ Datatype_Arg.intrs, type_elims @ Datatype_Arg.elims); |
|
6052 | 254 |
|
255 |
(**** Now prove the datatype theorems in this theory ****) |
|
256 |
||
257 |
||
258 |
(*** Prove the case theorems ***) |
|
259 |
||
12131 | 260 |
(*Each equation has the form |
6052 | 261 |
case(f_con1,...,f_conn)(coni(args)) = f_coni(args) *) |
12131 | 262 |
fun mk_case_eqn (((_,T,_), name, args, _), case_free) = |
6052 | 263 |
FOLogic.mk_Trueprop |
264 |
(FOLogic.mk_eq |
|
265 |
(case_tm $ |
|
12131 | 266 |
(list_comb (Const (Sign.intern_const (sign_of thy1) name,T), |
267 |
args)), |
|
268 |
list_comb (case_free, args))); |
|
6052 | 269 |
|
270 |
val case_trans = hd con_defs RS Ind_Syntax.def_trans |
|
271 |
and split_trans = Pr.split_eq RS meta_eq_to_obj_eq RS trans; |
|
272 |
||
273 |
(*Proves a single case equation. Could use simp_tac, but it's slower!*) |
|
12131 | 274 |
fun case_tacsf con_def _ = |
6052 | 275 |
[rewtac con_def, |
276 |
rtac case_trans 1, |
|
12131 | 277 |
REPEAT (resolve_tac [refl, split_trans, |
278 |
Su.case_inl RS trans, |
|
279 |
Su.case_inr RS trans] 1)]; |
|
6052 | 280 |
|
281 |
fun prove_case_eqn (arg,con_def) = |
|
12131 | 282 |
prove_goalw_cterm [] |
283 |
(Ind_Syntax.traceIt "next case equation = " |
|
284 |
(cterm_of (sign_of thy1) (mk_case_eqn arg))) |
|
285 |
(case_tacsf con_def); |
|
6052 | 286 |
|
12187 | 287 |
val free_iffs = map standard (con_defs RL [Ind_Syntax.def_swap_iff]); |
6052 | 288 |
|
12131 | 289 |
val case_eqns = |
290 |
map prove_case_eqn |
|
15570 | 291 |
(List.concat con_ty_lists ~~ case_args ~~ tl con_defs); |
6052 | 292 |
|
293 |
(*** Prove the recursor theorems ***) |
|
294 |
||
295 |
val recursor_eqns = case try (get_def thy1) recursor_base_name of |
|
15531 | 296 |
NONE => (writeln " [ No recursion operator ]"; |
12131 | 297 |
[]) |
15531 | 298 |
| SOME recursor_def => |
6052 | 299 |
let |
12131 | 300 |
(*Replace subterms rec`x (where rec is a Free var) by recursor_tm(x) *) |
301 |
fun subst_rec (Const("op `",_) $ Free _ $ arg) = recursor_tm $ arg |
|
302 |
| subst_rec tm = |
|
303 |
let val (head, args) = strip_comb tm |
|
304 |
in list_comb (head, map subst_rec args) end; |
|
6052 | 305 |
|
12131 | 306 |
(*Each equation has the form |
307 |
REC(coni(args)) = f_coni(args, REC(rec_arg), ...) |
|
308 |
where REC = recursor(f_con1,...,f_conn) and rec_arg is a recursive |
|
309 |
constructor argument.*) |
|
310 |
fun mk_recursor_eqn (((_,T,_), name, args, _), recursor_case) = |
|
311 |
FOLogic.mk_Trueprop |
|
312 |
(FOLogic.mk_eq |
|
313 |
(recursor_tm $ |
|
314 |
(list_comb (Const (Sign.intern_const (sign_of thy1) name,T), |
|
315 |
args)), |
|
15570 | 316 |
subst_rec (Library.foldl betapply (recursor_case, args)))); |
6052 | 317 |
|
12131 | 318 |
val recursor_trans = recursor_def RS def_Vrecursor RS trans; |
6052 | 319 |
|
12131 | 320 |
(*Proves a single recursor equation.*) |
321 |
fun recursor_tacsf _ = |
|
322 |
[rtac recursor_trans 1, |
|
323 |
simp_tac (rank_ss addsimps case_eqns) 1, |
|
324 |
IF_UNSOLVED (simp_tac (rank_ss addsimps tl con_defs) 1)]; |
|
6052 | 325 |
|
12131 | 326 |
fun prove_recursor_eqn arg = |
327 |
prove_goalw_cterm [] |
|
328 |
(Ind_Syntax.traceIt "next recursor equation = " |
|
329 |
(cterm_of (sign_of thy1) (mk_recursor_eqn arg))) |
|
330 |
recursor_tacsf |
|
6052 | 331 |
in |
15570 | 332 |
map prove_recursor_eqn (List.concat con_ty_lists ~~ recursor_cases) |
6052 | 333 |
end |
334 |
||
335 |
val constructors = |
|
336 |
map (head_of o #1 o Logic.dest_equals o #prop o rep_thm) (tl con_defs); |
|
337 |
||
12187 | 338 |
val free_SEs = map standard (Ind_Syntax.mk_free_SEs free_iffs); |
6052 | 339 |
|
6154
6a00a5baef2b
automatic insertion of datatype intr rules into claset
paulson
parents:
6141
diff
changeset
|
340 |
val {intrs, elim, induct, mutual_induct, ...} = ind_result |
6052 | 341 |
|
342 |
(*Typical theorems have the form ~con1=con2, con1=con2==>False, |
|
343 |
con1(x)=con1(y) ==> x=y, con1(x)=con1(y) <-> x=y, etc. *) |
|
344 |
fun mk_free s = |
|
345 |
prove_goalw (theory_of_thm elim) (*Don't use thy1: it will be stale*) |
|
346 |
con_defs s |
|
12131 | 347 |
(fn prems => [cut_facts_tac prems 1, |
348 |
fast_tac (ZF_cs addSEs free_SEs @ Su.free_SEs) 1]); |
|
6052 | 349 |
|
350 |
val simps = case_eqns @ recursor_eqns; |
|
351 |
||
352 |
val dt_info = |
|
12131 | 353 |
{inductive = true, |
354 |
constructors = constructors, |
|
355 |
rec_rewrites = recursor_eqns, |
|
356 |
case_rewrites = case_eqns, |
|
357 |
induct = induct, |
|
358 |
mutual_induct = mutual_induct, |
|
359 |
exhaustion = elim}; |
|
6052 | 360 |
|
361 |
val con_info = |
|
362 |
{big_rec_name = big_rec_name, |
|
12131 | 363 |
constructors = constructors, |
6052 | 364 |
(*let primrec handle definition by cases*) |
12131 | 365 |
free_iffs = free_iffs, |
366 |
rec_rewrites = (case recursor_eqns of |
|
367 |
[] => case_eqns | _ => recursor_eqns)}; |
|
6052 | 368 |
|
369 |
(*associate with each constructor the datatype name and rewrites*) |
|
370 |
val con_pairs = map (fn c => (#1 (dest_Const c), con_info)) constructors |
|
371 |
||
372 |
in |
|
373 |
(*Updating theory components: simprules and datatype info*) |
|
374 |
(thy1 |> Theory.add_path big_rec_base_name |
|
12183 | 375 |
|> (#1 o PureThy.add_thmss |
376 |
[(("simps", simps), [Simplifier.simp_add_global]), |
|
12187 | 377 |
(("", intrs), [Classical.safe_intro_global]), |
378 |
(("con_defs", con_defs), []), |
|
379 |
(("case_eqns", case_eqns), []), |
|
380 |
(("recursor_eqns", recursor_eqns), []), |
|
381 |
(("free_iffs", free_iffs), []), |
|
382 |
(("free_elims", free_SEs), [])]) |
|
17412 | 383 |
|> DatatypesData.map (Symtab.update (big_rec_name, dt_info)) |
384 |
|> ConstructorsData.map (fold Symtab.update con_pairs) |
|
12131 | 385 |
|> Theory.parent_path, |
6052 | 386 |
ind_result, |
387 |
{con_defs = con_defs, |
|
388 |
case_eqns = case_eqns, |
|
389 |
recursor_eqns = recursor_eqns, |
|
390 |
free_iffs = free_iffs, |
|
391 |
free_SEs = free_SEs, |
|
392 |
mk_free = mk_free}) |
|
393 |
end; |
|
394 |
||
17936 | 395 |
fun add_datatype (sdom, srec_tms) scon_ty_lists (raw_monos, raw_type_intrs, raw_type_elims) thy = |
12183 | 396 |
let |
17936 | 397 |
val read_i = Sign.simple_read_term thy Ind_Syntax.iT; |
12183 | 398 |
val rec_tms = map read_i srec_tms; |
17936 | 399 |
val con_ty_lists = Ind_Syntax.read_constructs thy scon_ty_lists; |
12183 | 400 |
val dom_sum = |
401 |
if sdom = "" then Ind_Syntax.data_domain coind (rec_tms, con_ty_lists) |
|
402 |
else read_i sdom; |
|
403 |
||
404 |
val (thy', ((monos, type_intrs), type_elims)) = thy |
|
405 |
|> IsarThy.apply_theorems raw_monos |
|
406 |
|>>> IsarThy.apply_theorems raw_type_intrs |
|
407 |
|>>> IsarThy.apply_theorems raw_type_elims; |
|
17936 | 408 |
in add_datatype_i (dom_sum, rec_tms) con_ty_lists (monos, type_intrs, type_elims) thy' end; |
12183 | 409 |
|
410 |
||
411 |
(* outer syntax *) |
|
412 |
||
17057 | 413 |
local structure P = OuterParse and K = OuterKeyword in |
12183 | 414 |
|
415 |
fun mk_datatype ((((dom, dts), monos), type_intrs), type_elims) = |
|
416 |
#1 o add_datatype (dom, map fst dts) (map snd dts) (monos, type_intrs, type_elims); |
|
417 |
||
418 |
val con_decl = |
|
419 |
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
|
420 |
>> P.triple1; |
12183 | 421 |
|
422 |
val datatype_decl = |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12243
diff
changeset
|
423 |
(Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<=") |-- P.!!! P.term) "") -- |
12183 | 424 |
P.and_list1 (P.term -- (P.$$$ "=" |-- P.enum1 "|" con_decl)) -- |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12243
diff
changeset
|
425 |
Scan.optional (P.$$$ "monos" |-- P.!!! P.xthms1) [] -- |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12243
diff
changeset
|
426 |
Scan.optional (P.$$$ "type_intros" |-- P.!!! P.xthms1) [] -- |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12243
diff
changeset
|
427 |
Scan.optional (P.$$$ "type_elims" |-- P.!!! P.xthms1) [] |
12183 | 428 |
>> (Toplevel.theory o mk_datatype); |
429 |
||
430 |
val coind_prefix = if coind then "co" else ""; |
|
431 |
||
432 |
val inductiveP = OuterSyntax.command (coind_prefix ^ "datatype") |
|
433 |
("define " ^ coind_prefix ^ "datatype") K.thy_decl datatype_decl; |
|
434 |
||
435 |
val _ = OuterSyntax.add_parsers [inductiveP]; |
|
6052 | 436 |
|
437 |
end; |
|
12183 | 438 |
|
439 |
end; |
|
15705 | 440 |