author | wenzelm |
Sun, 07 Oct 2007 21:19:31 +0200 | |
changeset 24893 | b8ef7afe3a6b |
parent 24867 | e5b55d7be9bb |
child 25985 | 8d69087f6a4b |
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: " ^ |
22578 | 58 |
Sign.string_of_term thy t); |
13150
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 |
||
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
64 |
val thy_path = thy |> Sign.add_path big_rec_base_name |
6052 | 65 |
|
22578 | 66 |
val big_rec_name = Sign.intern_const thy_path big_rec_base_name; |
6052 | 67 |
|
22578 | 68 |
val intr_tms = Ind_Syntax.mk_all_intr_tms thy_path (rec_tms, con_ty_lists); |
6052 | 69 |
|
12131 | 70 |
val dummy = |
12243 | 71 |
writeln ((if coind then "Codatatype" else "Datatype") ^ " definition " ^ quote big_rec_name); |
6052 | 72 |
|
73 |
val case_varname = "f"; (*name for case variables*) |
|
74 |
||
75 |
(** Define the constructors **) |
|
76 |
||
77 |
(*The empty tuple is 0*) |
|
78 |
fun mk_tuple [] = Const("0",iT) |
|
7696 | 79 |
| mk_tuple args = foldr1 (fn (t1, t2) => Pr.pair $ t1 $ t2) args; |
6052 | 80 |
|
23419 | 81 |
fun mk_inject n k u = BalancedTree.access |
82 |
{left = fn t => Su.inl $ t, right = fn t => Su.inr $ t, init = u} n k; |
|
6052 | 83 |
|
84 |
val npart = length rec_names; (*number of mutually recursive parts*) |
|
85 |
||
86 |
||
22578 | 87 |
val full_name = Sign.full_name thy_path; |
6052 | 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*) |
|
24255 | 95 |
PrimitiveDefs.mk_defpair (list_comb (Const (full_name name, T), args), |
12131 | 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 |
|
23419 | 110 |
in BalancedTree.make (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 |
||
24255 | 144 |
val case_def = PrimitiveDefs.mk_defpair |
23419 | 145 |
(case_tm, BalancedTree.make (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 [] = [] |
|
24826 | 177 |
| rec_args ((Const(@{const_name mem},_)$arg$X)::prems) = |
6052 | 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 = |
24255 | 221 |
PrimitiveDefs.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 |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
232 |
thy |> Sign.add_consts_i |
12131 | 233 |
[(recursor_base_name, recursor_typ, NoSyn)] |
18358 | 234 |
|> (snd o PureThy.add_defs_i false [Thm.no_attributes recursor_def]) |
6052 | 235 |
else thy; |
236 |
||
18358 | 237 |
val (con_defs, thy0) = thy_path |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
238 |
|> Sign.add_consts_i |
12131 | 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)))) |
18358 | 246 |
||> add_recursor |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
247 |
||> Sign.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 $ |
|
22578 | 266 |
(list_comb (Const (Sign.intern_const thy1 name,T), |
12131 | 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 |
||
17985 | 273 |
fun prove_case_eqn (arg, con_def) = |
20046 | 274 |
Goal.prove_global thy1 [] [] |
17985 | 275 |
(Ind_Syntax.traceIt "next case equation = " thy1 (mk_case_eqn arg)) |
276 |
(*Proves a single case equation. Could use simp_tac, but it's slower!*) |
|
277 |
(fn _ => EVERY |
|
278 |
[rewtac con_def, |
|
279 |
rtac case_trans 1, |
|
20046 | 280 |
REPEAT (resolve_tac [refl, split_trans, Su.case_inl RS trans, Su.case_inr RS trans] 1)]); |
6052 | 281 |
|
12187 | 282 |
val free_iffs = map standard (con_defs RL [Ind_Syntax.def_swap_iff]); |
6052 | 283 |
|
12131 | 284 |
val case_eqns = |
285 |
map prove_case_eqn |
|
15570 | 286 |
(List.concat con_ty_lists ~~ case_args ~~ tl con_defs); |
6052 | 287 |
|
288 |
(*** Prove the recursor theorems ***) |
|
289 |
||
290 |
val recursor_eqns = case try (get_def thy1) recursor_base_name of |
|
15531 | 291 |
NONE => (writeln " [ No recursion operator ]"; |
12131 | 292 |
[]) |
15531 | 293 |
| SOME recursor_def => |
6052 | 294 |
let |
12131 | 295 |
(*Replace subterms rec`x (where rec is a Free var) by recursor_tm(x) *) |
24826 | 296 |
fun subst_rec (Const(@{const_name apply},_) $ Free _ $ arg) = recursor_tm $ arg |
12131 | 297 |
| subst_rec tm = |
298 |
let val (head, args) = strip_comb tm |
|
299 |
in list_comb (head, map subst_rec args) end; |
|
6052 | 300 |
|
12131 | 301 |
(*Each equation has the form |
302 |
REC(coni(args)) = f_coni(args, REC(rec_arg), ...) |
|
303 |
where REC = recursor(f_con1,...,f_conn) and rec_arg is a recursive |
|
304 |
constructor argument.*) |
|
305 |
fun mk_recursor_eqn (((_,T,_), name, args, _), recursor_case) = |
|
306 |
FOLogic.mk_Trueprop |
|
307 |
(FOLogic.mk_eq |
|
308 |
(recursor_tm $ |
|
22578 | 309 |
(list_comb (Const (Sign.intern_const thy1 name,T), |
12131 | 310 |
args)), |
18185 | 311 |
subst_rec (Term.betapplys (recursor_case, args)))); |
6052 | 312 |
|
24893 | 313 |
val recursor_trans = recursor_def RS @{thm def_Vrecursor} RS trans; |
6052 | 314 |
|
12131 | 315 |
fun prove_recursor_eqn arg = |
20046 | 316 |
Goal.prove_global thy1 [] [] |
17985 | 317 |
(Ind_Syntax.traceIt "next recursor equation = " thy1 (mk_recursor_eqn arg)) |
318 |
(*Proves a single recursor equation.*) |
|
319 |
(fn _ => EVERY |
|
320 |
[rtac recursor_trans 1, |
|
321 |
simp_tac (rank_ss addsimps case_eqns) 1, |
|
20046 | 322 |
IF_UNSOLVED (simp_tac (rank_ss addsimps tl con_defs) 1)]); |
6052 | 323 |
in |
15570 | 324 |
map prove_recursor_eqn (List.concat con_ty_lists ~~ recursor_cases) |
6052 | 325 |
end |
326 |
||
327 |
val constructors = |
|
328 |
map (head_of o #1 o Logic.dest_equals o #prop o rep_thm) (tl con_defs); |
|
329 |
||
12187 | 330 |
val free_SEs = map standard (Ind_Syntax.mk_free_SEs free_iffs); |
6052 | 331 |
|
6154
6a00a5baef2b
automatic insertion of datatype intr rules into claset
paulson
parents:
6141
diff
changeset
|
332 |
val {intrs, elim, induct, mutual_induct, ...} = ind_result |
6052 | 333 |
|
334 |
(*Typical theorems have the form ~con1=con2, con1=con2==>False, |
|
335 |
con1(x)=con1(y) ==> x=y, con1(x)=con1(y) <-> x=y, etc. *) |
|
336 |
fun mk_free s = |
|
17985 | 337 |
let val thy = theory_of_thm elim in (*Don't use thy1: it will be stale*) |
24707 | 338 |
Goal.prove_global thy [] [] (Syntax.read_prop_global thy s) |
17985 | 339 |
(fn _ => EVERY |
340 |
[rewrite_goals_tac con_defs, |
|
20046 | 341 |
fast_tac (ZF_cs addSEs free_SEs @ Su.free_SEs) 1]) |
17985 | 342 |
end; |
6052 | 343 |
|
344 |
val simps = case_eqns @ recursor_eqns; |
|
345 |
||
346 |
val dt_info = |
|
12131 | 347 |
{inductive = true, |
348 |
constructors = constructors, |
|
349 |
rec_rewrites = recursor_eqns, |
|
350 |
case_rewrites = case_eqns, |
|
351 |
induct = induct, |
|
352 |
mutual_induct = mutual_induct, |
|
353 |
exhaustion = elim}; |
|
6052 | 354 |
|
355 |
val con_info = |
|
356 |
{big_rec_name = big_rec_name, |
|
12131 | 357 |
constructors = constructors, |
6052 | 358 |
(*let primrec handle definition by cases*) |
12131 | 359 |
free_iffs = free_iffs, |
360 |
rec_rewrites = (case recursor_eqns of |
|
361 |
[] => case_eqns | _ => recursor_eqns)}; |
|
6052 | 362 |
|
363 |
(*associate with each constructor the datatype name and rewrites*) |
|
364 |
val con_pairs = map (fn c => (#1 (dest_Const c), con_info)) constructors |
|
365 |
||
366 |
in |
|
367 |
(*Updating theory components: simprules and datatype info*) |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
368 |
(thy1 |> Sign.add_path big_rec_base_name |
18377 | 369 |
|> PureThy.add_thmss |
18728 | 370 |
[(("simps", simps), [Simplifier.simp_add]), |
371 |
(("", intrs), [Classical.safe_intro NONE]), |
|
12187 | 372 |
(("con_defs", con_defs), []), |
373 |
(("case_eqns", case_eqns), []), |
|
374 |
(("recursor_eqns", recursor_eqns), []), |
|
375 |
(("free_iffs", free_iffs), []), |
|
18377 | 376 |
(("free_elims", free_SEs), [])] |> snd |
17412 | 377 |
|> DatatypesData.map (Symtab.update (big_rec_name, dt_info)) |
378 |
|> ConstructorsData.map (fold Symtab.update con_pairs) |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24707
diff
changeset
|
379 |
|> Sign.parent_path, |
6052 | 380 |
ind_result, |
381 |
{con_defs = con_defs, |
|
382 |
case_eqns = case_eqns, |
|
383 |
recursor_eqns = recursor_eqns, |
|
384 |
free_iffs = free_iffs, |
|
385 |
free_SEs = free_SEs, |
|
386 |
mk_free = mk_free}) |
|
387 |
end; |
|
388 |
||
17936 | 389 |
fun add_datatype (sdom, srec_tms) scon_ty_lists (raw_monos, raw_type_intrs, raw_type_elims) thy = |
12183 | 390 |
let |
24725 | 391 |
val ctxt = ProofContext.init thy; |
17936 | 392 |
val read_i = Sign.simple_read_term thy Ind_Syntax.iT; |
12183 | 393 |
val rec_tms = map read_i srec_tms; |
17936 | 394 |
val con_ty_lists = Ind_Syntax.read_constructs thy scon_ty_lists; |
12183 | 395 |
val dom_sum = |
396 |
if sdom = "" then Ind_Syntax.data_domain coind (rec_tms, con_ty_lists) |
|
397 |
else read_i sdom; |
|
24725 | 398 |
val monos = Attrib.eval_thms ctxt raw_monos; |
399 |
val type_intrs = Attrib.eval_thms ctxt raw_type_intrs; |
|
400 |
val type_elims = Attrib.eval_thms ctxt raw_type_elims; |
|
401 |
in add_datatype_i (dom_sum, rec_tms) con_ty_lists (monos, type_intrs, type_elims) thy end; |
|
402 |
||
12183 | 403 |
|
404 |
(* outer syntax *) |
|
405 |
||
17057 | 406 |
local structure P = OuterParse and K = OuterKeyword in |
12183 | 407 |
|
408 |
fun mk_datatype ((((dom, dts), monos), type_intrs), type_elims) = |
|
409 |
#1 o add_datatype (dom, map fst dts) (map snd dts) (monos, type_intrs, type_elims); |
|
410 |
||
411 |
val con_decl = |
|
412 |
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
|
413 |
>> P.triple1; |
12183 | 414 |
|
415 |
val datatype_decl = |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12243
diff
changeset
|
416 |
(Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<=") |-- P.!!! P.term) "") -- |
12183 | 417 |
P.and_list1 (P.term -- (P.$$$ "=" |-- P.enum1 "|" con_decl)) -- |
22101 | 418 |
Scan.optional (P.$$$ "monos" |-- P.!!! SpecParse.xthms1) [] -- |
419 |
Scan.optional (P.$$$ "type_intros" |-- P.!!! SpecParse.xthms1) [] -- |
|
420 |
Scan.optional (P.$$$ "type_elims" |-- P.!!! SpecParse.xthms1) [] |
|
12183 | 421 |
>> (Toplevel.theory o mk_datatype); |
422 |
||
423 |
val coind_prefix = if coind then "co" else ""; |
|
424 |
||
24867 | 425 |
val _ = OuterSyntax.command (coind_prefix ^ "datatype") |
12183 | 426 |
("define " ^ coind_prefix ^ "datatype") K.thy_decl datatype_decl; |
427 |
||
6052 | 428 |
end; |
12183 | 429 |
|
430 |
end; |
|
15705 | 431 |