author | wenzelm |
Wed, 14 Apr 1999 15:58:01 +0200 | |
changeset 6427 | fd36b2e7d80e |
parent 6423 | 2f79b06e54e8 |
child 6441 | 268aa3c4a059 |
permissions | -rw-r--r-- |
5177 | 1 |
(* Title: HOL/Tools/datatype_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Stefan Berghofer |
|
4 |
Copyright 1998 TU Muenchen |
|
5 |
||
6360 | 6 |
Datatype package for Isabelle/HOL. |
5177 | 7 |
*) |
8 |
||
6360 | 9 |
signature BASIC_DATATYPE_PACKAGE = |
10 |
sig |
|
11 |
val mutual_induct_tac : string list -> int -> tactic |
|
12 |
val induct_tac : string -> int -> tactic |
|
13 |
val exhaust_tac : string -> int -> tactic |
|
14 |
end; |
|
15 |
||
5177 | 16 |
signature DATATYPE_PACKAGE = |
17 |
sig |
|
6360 | 18 |
include BASIC_DATATYPE_PACKAGE |
5661 | 19 |
val quiet_mode : bool ref |
20 |
val add_datatype : bool -> string list -> (string list * bstring * mixfix * |
|
21 |
(bstring * string list * mixfix) list) list -> theory -> theory * |
|
5177 | 22 |
{distinct : thm list list, |
23 |
inject : thm list list, |
|
24 |
exhaustion : thm list, |
|
25 |
rec_thms : thm list, |
|
26 |
case_thms : thm list list, |
|
27 |
split_thms : (thm * thm) list, |
|
28 |
induction : thm, |
|
29 |
size : thm list, |
|
30 |
simps : thm list} |
|
5661 | 31 |
val add_datatype_i : bool -> string list -> (string list * bstring * mixfix * |
32 |
(bstring * typ list * mixfix) list) list -> theory -> theory * |
|
5177 | 33 |
{distinct : thm list list, |
34 |
inject : thm list list, |
|
35 |
exhaustion : thm list, |
|
36 |
rec_thms : thm list, |
|
37 |
case_thms : thm list list, |
|
38 |
split_thms : (thm * thm) list, |
|
39 |
induction : thm, |
|
40 |
size : thm list, |
|
41 |
simps : thm list} |
|
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
42 |
val rep_datatype_i : string list option -> (thm * theory attribute list) list list -> |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
43 |
(thm * theory attribute list) list list -> (thm * theory attribute list) -> theory -> theory * |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
44 |
{distinct : thm list list, |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
45 |
inject : thm list list, |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
46 |
exhaustion : thm list, |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
47 |
rec_thms : thm list, |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
48 |
case_thms : thm list list, |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
49 |
split_thms : (thm * thm) list, |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
50 |
induction : thm, |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
51 |
size : thm list, |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
52 |
simps : thm list} |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
53 |
val rep_datatype : string list option -> (xstring * Args.src list) list list -> |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
54 |
(xstring * Args.src list) list list -> xstring * Args.src list -> theory -> theory * |
5177 | 55 |
{distinct : thm list list, |
56 |
inject : thm list list, |
|
57 |
exhaustion : thm list, |
|
58 |
rec_thms : thm list, |
|
59 |
case_thms : thm list list, |
|
60 |
split_thms : (thm * thm) list, |
|
61 |
induction : thm, |
|
62 |
size : thm list, |
|
63 |
simps : thm list} |
|
64 |
val get_datatypes : theory -> DatatypeAux.datatype_info Symtab.table |
|
65 |
val datatype_info_sg : Sign.sg -> string -> DatatypeAux.datatype_info |
|
66 |
val datatype_info : theory -> string -> DatatypeAux.datatype_info |
|
67 |
val constrs_of : theory -> string -> term list option |
|
68 |
val case_const_of : theory -> string -> term option |
|
6360 | 69 |
val setup: (theory -> theory) list |
5177 | 70 |
end; |
71 |
||
72 |
structure DatatypePackage : DATATYPE_PACKAGE = |
|
73 |
struct |
|
74 |
||
75 |
open DatatypeAux; |
|
76 |
||
5661 | 77 |
val quiet_mode = quiet_mode; |
78 |
||
6360 | 79 |
|
5177 | 80 |
(* data kind 'HOL/datatypes' *) |
81 |
||
82 |
structure DatatypesArgs = |
|
83 |
struct |
|
84 |
val name = "HOL/datatypes"; |
|
85 |
type T = datatype_info Symtab.table; |
|
86 |
||
87 |
val empty = Symtab.empty; |
|
88 |
val prep_ext = I; |
|
89 |
val merge: T * T -> T = Symtab.merge (K true); |
|
90 |
||
91 |
fun print sg tab = |
|
92 |
Pretty.writeln (Pretty.strs ("datatypes:" :: |
|
93 |
map (Sign.cond_extern sg Sign.typeK o fst) (Symtab.dest tab))); |
|
94 |
end; |
|
95 |
||
96 |
structure DatatypesData = TheoryDataFun(DatatypesArgs); |
|
97 |
val get_datatypes_sg = DatatypesData.get_sg; |
|
98 |
val get_datatypes = DatatypesData.get; |
|
99 |
val put_datatypes = DatatypesData.put; |
|
100 |
||
101 |
||
102 |
(** theory information about datatypes **) |
|
103 |
||
104 |
fun datatype_info_sg sg name = |
|
105 |
(case Symtab.lookup (get_datatypes_sg sg, name) of |
|
106 |
Some info => info |
|
107 |
| None => error ("Unknown datatype " ^ quote name)); |
|
108 |
||
6394 | 109 |
val datatype_info = datatype_info_sg o Theory.sign_of; |
5177 | 110 |
|
111 |
fun constrs_of thy tname = |
|
112 |
let |
|
113 |
val {index, descr, ...} = datatype_info thy tname; |
|
114 |
val (_, _, constrs) = the (assoc (descr, index)) |
|
115 |
in |
|
116 |
Some (map (fn (cname, _) => |
|
6394 | 117 |
Const (cname, the (Sign.const_type (Theory.sign_of thy) cname))) constrs) |
5177 | 118 |
end handle _ => None; |
119 |
||
120 |
fun case_const_of thy tname = |
|
121 |
let |
|
122 |
val {case_name, ...} = datatype_info thy tname; |
|
123 |
in |
|
6394 | 124 |
Some (Const (case_name, the (Sign.const_type (Theory.sign_of thy) case_name))) |
5177 | 125 |
end handle _ => None; |
126 |
||
127 |
fun find_tname var Bi = |
|
128 |
let val frees = map dest_Free (term_frees Bi) |
|
129 |
val params = Logic.strip_params Bi; |
|
130 |
in case assoc (frees @ params, var) of |
|
131 |
None => error ("No such variable in subgoal: " ^ quote var) |
|
132 |
| Some(Type (tn, _)) => tn |
|
133 |
| _ => error ("Cannot determine type of " ^ quote var) |
|
134 |
end; |
|
135 |
||
136 |
fun infer_tname state sign i aterm = |
|
137 |
let |
|
138 |
val (_, _, Bi, _) = dest_state (state, i) |
|
139 |
val params = Logic.strip_params Bi; (*params of subgoal i*) |
|
140 |
val params = rev (rename_wrt_term Bi params); (*as they are printed*) |
|
141 |
val (types, sorts) = types_sorts state; |
|
142 |
fun types' (a, ~1) = (case assoc (params, a) of None => types(a, ~1) | sm => sm) |
|
143 |
| types' ixn = types ixn; |
|
144 |
val (ct, _) = read_def_cterm (sign, types', sorts) [] false |
|
145 |
(aterm, TVar (("", 0), [])); |
|
146 |
in case #T (rep_cterm ct) of |
|
147 |
Type (tn, _) => tn |
|
148 |
| _ => error ("Cannot determine type of " ^ quote aterm) |
|
149 |
end; |
|
150 |
||
151 |
(*Warn if the (induction) variable occurs Free among the premises, which |
|
152 |
usually signals a mistake. But calls the tactic either way!*) |
|
153 |
fun occs_in_prems tacf vars = |
|
154 |
SUBGOAL (fn (Bi, i) => |
|
155 |
(if exists (fn Free (a, _) => a mem vars) |
|
156 |
(foldr add_term_frees (#2 (strip_context Bi), [])) |
|
157 |
then warning "Induction variable occurs also among premises!" |
|
158 |
else (); |
|
159 |
tacf i)); |
|
160 |
||
161 |
(* generic induction tactic for datatypes *) |
|
162 |
||
163 |
fun mutual_induct_tac vars i state = |
|
164 |
let |
|
165 |
val (_, _, Bi, _) = dest_state (state, i); |
|
166 |
val {sign, ...} = rep_thm state; |
|
167 |
val tn = find_tname (hd vars) Bi; |
|
168 |
val {induction, ...} = datatype_info_sg sign tn; |
|
169 |
val ind_vnames = map (fn (_ $ Var (ixn, _)) => |
|
170 |
implode (tl (explode (Syntax.string_of_vname ixn)))) |
|
171 |
(dest_conj (HOLogic.dest_Trueprop (concl_of induction))); |
|
172 |
val insts = (ind_vnames ~~ vars) handle _ => |
|
173 |
error ("Induction rule for type " ^ tn ^ " has different number of variables") |
|
174 |
in |
|
175 |
occs_in_prems (res_inst_tac insts induction) vars i state |
|
176 |
end; |
|
177 |
||
178 |
fun induct_tac var = mutual_induct_tac [var]; |
|
179 |
||
180 |
(* generic exhaustion tactic for datatypes *) |
|
181 |
||
182 |
fun exhaust_tac aterm i state = |
|
183 |
let |
|
184 |
val {sign, ...} = rep_thm state; |
|
185 |
val tn = infer_tname state sign i aterm; |
|
186 |
val {exhaustion, ...} = datatype_info_sg sign tn; |
|
187 |
val _ $ Var (ixn, _) $ _ = HOLogic.dest_Trueprop |
|
188 |
(hd (Logic.strip_assums_hyp (hd (prems_of exhaustion)))); |
|
189 |
val exh_vname = implode (tl (explode (Syntax.string_of_vname ixn))) |
|
190 |
in |
|
191 |
res_inst_tac [(exh_vname, aterm)] exhaustion i state |
|
192 |
end; |
|
193 |
||
194 |
||
195 |
(* prepare types *) |
|
196 |
||
197 |
fun read_typ sign ((Ts, sorts), str) = |
|
198 |
let |
|
199 |
val T = Type.no_tvars (Sign.read_typ (sign, (curry assoc) |
|
200 |
(map (apfst (rpair ~1)) sorts)) str) handle TYPE (msg, _, _) => error msg |
|
201 |
in (Ts @ [T], add_typ_tfrees (T, sorts)) end; |
|
202 |
||
203 |
fun cert_typ sign ((Ts, sorts), raw_T) = |
|
204 |
let |
|
205 |
val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle |
|
206 |
TYPE (msg, _, _) => error msg; |
|
207 |
val sorts' = add_typ_tfrees (T, sorts) |
|
208 |
in (Ts @ [T], |
|
209 |
case duplicates (map fst sorts') of |
|
210 |
[] => sorts' |
|
211 |
| dups => error ("Inconsistent sort constraints for " ^ commas dups)) |
|
212 |
end; |
|
213 |
||
214 |
||
215 |
(**** make datatype info ****) |
|
216 |
||
217 |
fun make_dt_info descr induct reccomb_names rec_thms |
|
218 |
((((((((i, (_, (tname, _, _))), case_name), case_thms), |
|
219 |
exhaustion_thm), distinct_thm), inject), nchotomy), case_cong) = (tname, |
|
220 |
{index = i, |
|
221 |
descr = descr, |
|
222 |
rec_names = reccomb_names, |
|
223 |
rec_rewrites = rec_thms, |
|
224 |
case_name = case_name, |
|
225 |
case_rewrites = case_thms, |
|
226 |
induction = induct, |
|
227 |
exhaustion = exhaustion_thm, |
|
228 |
distinct = distinct_thm, |
|
229 |
inject = inject, |
|
230 |
nchotomy = nchotomy, |
|
231 |
case_cong = case_cong}); |
|
232 |
||
233 |
fun store_clasimp thy (cla, simp) = |
|
234 |
(claset_ref_of thy := cla; simpset_ref_of thy := simp); |
|
235 |
||
236 |
infix 4 addDistinct; |
|
237 |
||
238 |
fun clasimp addDistinct ([], _) = clasimp |
|
239 |
| clasimp addDistinct (thms::thmss, (_, (_, _, constrs))::descr) = |
|
240 |
if length constrs < DatatypeProp.dtK then |
|
241 |
clasimp addIffs thms addDistinct (thmss, descr) |
|
242 |
else |
|
243 |
clasimp addsimps2 thms addDistinct (thmss, descr); |
|
244 |
||
245 |
||
246 |
(********************* axiomatic introduction of datatypes ********************) |
|
247 |
||
248 |
fun add_and_get_axioms label tnames ts thy = |
|
249 |
foldr (fn ((tname, t), (thy', axs)) => |
|
250 |
let |
|
251 |
val thy'' = thy' |> |
|
5661 | 252 |
Theory.add_path tname |> |
5177 | 253 |
PureThy.add_axioms_i [((label, t), [])]; |
254 |
val ax = get_axiom thy'' label |
|
5661 | 255 |
in (Theory.parent_path thy'', ax::axs) |
5177 | 256 |
end) (tnames ~~ ts, (thy, [])); |
257 |
||
258 |
fun add_and_get_axiomss label tnames tss thy = |
|
259 |
foldr (fn ((tname, ts), (thy', axss)) => |
|
260 |
let |
|
261 |
val thy'' = thy' |> |
|
5661 | 262 |
Theory.add_path tname |> |
5177 | 263 |
PureThy.add_axiomss_i [((label, ts), [])]; |
264 |
val axs = PureThy.get_thms thy'' label |
|
5661 | 265 |
in (Theory.parent_path thy'', axs::axss) |
5177 | 266 |
end) (tnames ~~ tss, (thy, [])); |
267 |
||
5661 | 268 |
fun add_datatype_axm flat_names new_type_names descr sorts types_syntax constr_syntax dt_info thy = |
5177 | 269 |
let |
270 |
val descr' = flat descr; |
|
271 |
val recTs = get_rec_types descr' sorts; |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5279
diff
changeset
|
272 |
val used = foldr add_typ_tfree_names (recTs, []); |
5177 | 273 |
val newTs = take (length (hd descr), recTs); |
274 |
||
6360 | 275 |
val _ = message ("Adding axioms for datatype(s) " ^ commas_quote new_type_names); |
5177 | 276 |
|
277 |
(**** declare new types and constants ****) |
|
278 |
||
279 |
val tyvars = map (fn (_, (_, Ts, _)) => map dest_DtTFree Ts) (hd descr); |
|
280 |
||
281 |
val constr_decls = map (fn (((_, (_, _, constrs)), T), constr_syntax') => |
|
282 |
map (fn ((_, cargs), (cname, mx)) => |
|
283 |
(cname, map (typ_of_dtyp descr' sorts) cargs ---> T, mx)) |
|
284 |
(constrs ~~ constr_syntax')) ((hd descr) ~~ newTs ~~ constr_syntax); |
|
285 |
||
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5279
diff
changeset
|
286 |
val rec_result_Ts = map TFree (variantlist (replicate (length descr') "'t", used) ~~ |
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5279
diff
changeset
|
287 |
replicate (length descr') HOLogic.termS); |
5177 | 288 |
|
289 |
val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) => |
|
290 |
map (fn (_, cargs) => |
|
291 |
let |
|
292 |
val recs = filter is_rec_type cargs; |
|
293 |
val argTs = (map (typ_of_dtyp descr' sorts) cargs) @ |
|
294 |
(map (fn r => nth_elem (dest_DtRec r, rec_result_Ts)) recs) |
|
295 |
in argTs ---> nth_elem (i, rec_result_Ts) |
|
296 |
end) constrs) descr'); |
|
297 |
||
298 |
val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec"; |
|
299 |
val reccomb_names = if length descr' = 1 then [big_reccomb_name] else |
|
300 |
(map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int) |
|
301 |
(1 upto (length descr'))); |
|
302 |
||
303 |
val big_size_name = space_implode "_" new_type_names ^ "_size"; |
|
304 |
val size_names = if length (flat (tl descr)) = 1 then [big_size_name] else |
|
305 |
map (fn i => big_size_name ^ "_" ^ string_of_int i) |
|
306 |
(1 upto length (flat (tl descr))); |
|
307 |
||
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5279
diff
changeset
|
308 |
val freeT = TFree (variant used "'t", HOLogic.termS); |
5177 | 309 |
val case_fn_Ts = map (fn (i, (_, _, constrs)) => |
310 |
map (fn (_, cargs) => |
|
311 |
let val Ts = map (typ_of_dtyp descr' sorts) cargs |
|
312 |
in Ts ---> freeT end) constrs) (hd descr); |
|
313 |
||
314 |
val case_names = map (fn s => (s ^ "_case")) new_type_names; |
|
315 |
||
6305 | 316 |
val thy2' = thy |> |
5177 | 317 |
|
318 |
(** new types **) |
|
319 |
||
320 |
curry (foldr (fn (((name, mx), tvs), thy') => thy' |> |
|
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
321 |
TypedefPackage.add_typedecls [(name, tvs, mx)])) |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
322 |
(types_syntax ~~ tyvars) |> |
5661 | 323 |
add_path flat_names (space_implode "_" new_type_names) |> |
5177 | 324 |
|
325 |
(** primrec combinators **) |
|
326 |
||
327 |
Theory.add_consts_i (map (fn ((name, T), T') => |
|
328 |
(name, reccomb_fn_Ts @ [T] ---> T', NoSyn)) |
|
329 |
(reccomb_names ~~ recTs ~~ rec_result_Ts)) |> |
|
330 |
||
331 |
(** case combinators **) |
|
332 |
||
333 |
Theory.add_consts_i (map (fn ((name, T), Ts) => |
|
334 |
(name, Ts @ [T] ---> freeT, NoSyn)) |
|
335 |
(case_names ~~ newTs ~~ case_fn_Ts)) |> |
|
6305 | 336 |
Theory.add_trrules_i (DatatypeProp.make_case_trrules new_type_names descr); |
337 |
||
6394 | 338 |
val reccomb_names' = map (Sign.intern_const (Theory.sign_of thy2')) reccomb_names; |
339 |
val case_names' = map (Sign.intern_const (Theory.sign_of thy2')) case_names; |
|
6305 | 340 |
|
341 |
val thy2 = thy2' |> |
|
5177 | 342 |
|
343 |
(** t_ord functions **) |
|
344 |
||
345 |
Theory.add_consts_i |
|
346 |
(foldr (fn ((((_, (_, _, constrs)), tname), T), decls) => |
|
347 |
if length constrs < DatatypeProp.dtK then decls |
|
348 |
else (tname ^ "_ord", T --> HOLogic.natT, NoSyn)::decls) |
|
349 |
((hd descr) ~~ new_type_names ~~ newTs, [])) |> |
|
350 |
||
351 |
(** size functions **) |
|
352 |
||
353 |
Theory.add_consts_i (map (fn (s, T) => |
|
354 |
(Sign.base_name s, T --> HOLogic.natT, NoSyn)) |
|
5661 | 355 |
(size_names ~~ drop (length (hd descr), recTs))) |> |
356 |
||
357 |
(** constructors **) |
|
358 |
||
359 |
parent_path flat_names |> |
|
360 |
curry (foldr (fn (((((_, (_, _, constrs)), T), tname), |
|
361 |
constr_syntax'), thy') => thy' |> |
|
362 |
add_path flat_names tname |> |
|
363 |
Theory.add_consts_i (map (fn ((_, cargs), (cname, mx)) => |
|
364 |
(cname, map (typ_of_dtyp descr' sorts) cargs ---> T, mx)) |
|
365 |
(constrs ~~ constr_syntax')) |> |
|
366 |
parent_path flat_names)) |
|
367 |
(hd descr ~~ newTs ~~ new_type_names ~~ constr_syntax); |
|
5177 | 368 |
|
369 |
(**** introduction of axioms ****) |
|
370 |
||
5661 | 371 |
val rec_axs = DatatypeProp.make_primrecs new_type_names descr sorts thy2; |
372 |
val size_axs = DatatypeProp.make_size new_type_names descr sorts thy2; |
|
373 |
||
5177 | 374 |
val (thy3, inject) = thy2 |> |
5661 | 375 |
Theory.add_path (space_implode "_" new_type_names) |> |
5177 | 376 |
PureThy.add_axioms_i [(("induct", DatatypeProp.make_ind descr sorts), [])] |> |
5661 | 377 |
PureThy.add_axiomss_i [(("recs", rec_axs), [])] |> |
378 |
PureThy.add_axiomss_i [(("size", size_axs), [])] |> |
|
379 |
Theory.parent_path |> |
|
5177 | 380 |
add_and_get_axiomss "inject" new_type_names |
381 |
(DatatypeProp.make_injs descr sorts); |
|
5661 | 382 |
val induct = get_axiom thy3 "induct"; |
383 |
val rec_thms = get_thms thy3 "recs"; |
|
384 |
val size_thms = get_thms thy3 "size"; |
|
5177 | 385 |
val (thy4, distinct) = add_and_get_axiomss "distinct" new_type_names |
386 |
(DatatypeProp.make_distincts new_type_names descr sorts thy3) thy3; |
|
387 |
val (thy5, exhaustion) = add_and_get_axioms "exhaust" new_type_names |
|
5661 | 388 |
(DatatypeProp.make_casedists descr sorts) thy4; |
5177 | 389 |
val (thy6, case_thms) = add_and_get_axiomss "cases" new_type_names |
390 |
(DatatypeProp.make_cases new_type_names descr sorts thy5) thy5; |
|
391 |
val (split_ts, split_asm_ts) = ListPair.unzip |
|
392 |
(DatatypeProp.make_splits new_type_names descr sorts thy6); |
|
393 |
val (thy7, split) = add_and_get_axioms "split" new_type_names split_ts thy6; |
|
394 |
val (thy8, split_asm) = add_and_get_axioms "split_asm" new_type_names |
|
395 |
split_asm_ts thy7; |
|
396 |
val (thy9, nchotomys) = add_and_get_axioms "nchotomy" new_type_names |
|
397 |
(DatatypeProp.make_nchotomys descr sorts) thy8; |
|
398 |
val (thy10, case_congs) = add_and_get_axioms "case_cong" new_type_names |
|
399 |
(DatatypeProp.make_case_congs new_type_names descr sorts thy9) thy9; |
|
400 |
||
6305 | 401 |
val dt_infos = map (make_dt_info descr' induct reccomb_names' rec_thms) |
402 |
((0 upto length (hd descr) - 1) ~~ (hd descr) ~~ case_names' ~~ case_thms ~~ |
|
5177 | 403 |
exhaustion ~~ distinct ~~ inject ~~ nchotomys ~~ case_congs); |
404 |
||
405 |
val simps = flat (distinct @ inject @ case_thms) @ size_thms @ rec_thms; |
|
406 |
||
5661 | 407 |
val thy11 = thy10 |> |
408 |
Theory.add_path (space_implode "_" new_type_names) |> |
|
6092 | 409 |
PureThy.add_thmss [(("simps", simps), [])] |> |
5177 | 410 |
put_datatypes (foldr Symtab.update (dt_infos, dt_info)) |> |
411 |
Theory.parent_path; |
|
412 |
||
5661 | 413 |
val _ = store_clasimp thy11 ((claset_of thy11, simpset_of thy11) |
5177 | 414 |
addsimps2 flat case_thms addsimps2 size_thms addsimps2 rec_thms |
415 |
addIffs flat inject addDistinct (distinct, hd descr)); |
|
416 |
||
417 |
in |
|
5661 | 418 |
(thy11, |
5177 | 419 |
{distinct = distinct, |
420 |
inject = inject, |
|
421 |
exhaustion = exhaustion, |
|
422 |
rec_thms = rec_thms, |
|
423 |
case_thms = case_thms, |
|
424 |
split_thms = split ~~ split_asm, |
|
425 |
induction = induct, |
|
426 |
size = size_thms, |
|
427 |
simps = simps}) |
|
428 |
end; |
|
429 |
||
430 |
||
431 |
(******************* definitional introduction of datatypes *******************) |
|
432 |
||
5661 | 433 |
fun add_datatype_def flat_names new_type_names descr sorts types_syntax constr_syntax dt_info thy = |
5177 | 434 |
let |
6360 | 435 |
val _ = message ("Proofs for datatype(s) " ^ commas_quote new_type_names); |
5177 | 436 |
|
437 |
val (thy2, inject, dist_rewrites, induct) = thy |> |
|
5661 | 438 |
DatatypeRepProofs.representation_proofs flat_names dt_info new_type_names descr sorts |
5177 | 439 |
types_syntax constr_syntax; |
440 |
||
441 |
val (thy3, casedist_thms) = |
|
442 |
DatatypeAbsProofs.prove_casedist_thms new_type_names descr sorts induct thy2; |
|
443 |
val (thy4, reccomb_names, rec_thms) = DatatypeAbsProofs.prove_primrec_thms |
|
5661 | 444 |
flat_names new_type_names descr sorts dt_info inject dist_rewrites induct thy3; |
5177 | 445 |
val (thy5, case_names, case_thms) = DatatypeAbsProofs.prove_case_thms |
5661 | 446 |
flat_names new_type_names descr sorts reccomb_names rec_thms thy4; |
5177 | 447 |
val (thy6, distinct) = DatatypeAbsProofs.prove_distinctness_thms |
5661 | 448 |
flat_names new_type_names descr sorts dist_rewrites case_thms thy5; |
5177 | 449 |
val (thy7, split_thms) = DatatypeAbsProofs.prove_split_thms new_type_names |
450 |
descr sorts inject dist_rewrites casedist_thms case_thms thy6; |
|
451 |
val (thy8, nchotomys) = DatatypeAbsProofs.prove_nchotomys new_type_names |
|
452 |
descr sorts casedist_thms thy7; |
|
453 |
val (thy9, case_congs) = DatatypeAbsProofs.prove_case_congs new_type_names |
|
454 |
descr sorts nchotomys case_thms thy8; |
|
5661 | 455 |
val (thy10, size_thms) = DatatypeAbsProofs.prove_size_thms flat_names new_type_names |
5177 | 456 |
descr sorts reccomb_names rec_thms thy9; |
457 |
||
458 |
val dt_infos = map (make_dt_info (flat descr) induct reccomb_names rec_thms) |
|
459 |
((0 upto length (hd descr) - 1) ~~ (hd descr) ~~ case_names ~~ case_thms ~~ |
|
460 |
casedist_thms ~~ distinct ~~ inject ~~ nchotomys ~~ case_congs); |
|
461 |
||
462 |
val simps = flat (distinct @ inject @ case_thms) @ size_thms @ rec_thms; |
|
463 |
||
464 |
val thy11 = thy10 |> |
|
5661 | 465 |
Theory.add_path (space_implode "_" new_type_names) |> |
6092 | 466 |
PureThy.add_thmss [(("simps", simps), [])] |> |
5177 | 467 |
put_datatypes (foldr Symtab.update (dt_infos, dt_info)) |> |
5663
aad79a127628
Fixed bug (improper handling of flag flat_names).
berghofe
parents:
5661
diff
changeset
|
468 |
Theory.parent_path; |
5177 | 469 |
|
470 |
val _ = store_clasimp thy11 ((claset_of thy11, simpset_of thy11) |
|
471 |
addsimps2 flat case_thms addsimps2 size_thms addsimps2 rec_thms |
|
472 |
addIffs flat inject addDistinct (distinct, hd descr)); |
|
473 |
||
474 |
in |
|
475 |
(thy11, |
|
476 |
{distinct = distinct, |
|
477 |
inject = inject, |
|
478 |
exhaustion = casedist_thms, |
|
479 |
rec_thms = rec_thms, |
|
480 |
case_thms = case_thms, |
|
481 |
split_thms = split_thms, |
|
482 |
induction = induct, |
|
483 |
size = size_thms, |
|
484 |
simps = simps}) |
|
485 |
end; |
|
486 |
||
487 |
||
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
488 |
(*********************** declare existing type as datatype *********************) |
5177 | 489 |
|
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
490 |
fun gen_rep_datatype apply_theorems alt_names raw_distinct raw_inject raw_induction thy0 = |
5177 | 491 |
let |
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
492 |
fun app_thmss srcs thy = foldl_map (fn (thy, x) => apply_theorems x thy) (thy, srcs); |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
493 |
fun app_thm src thy = apsnd Library.hd (apply_theorems [src] thy); |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
494 |
|
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
495 |
val (((thy1, induction), inject), distinct) = thy0 |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
496 |
|> app_thmss raw_distinct |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
497 |
|> apfst (app_thmss raw_inject) |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
498 |
|> apfst (apfst (app_thm raw_induction)); |
6394 | 499 |
val sign = Theory.sign_of thy1; |
5177 | 500 |
|
501 |
val induction' = freezeT induction; |
|
502 |
||
503 |
fun err t = error ("Ill-formed predicate in induction rule: " ^ |
|
504 |
Sign.string_of_term sign t); |
|
505 |
||
506 |
fun get_typ (t as _ $ Var (_, Type (tname, Ts))) = |
|
507 |
((tname, map dest_TFree Ts) handle _ => err t) |
|
508 |
| get_typ t = err t; |
|
509 |
||
510 |
val dtnames = map get_typ (dest_conj (HOLogic.dest_Trueprop (concl_of induction'))); |
|
511 |
val new_type_names = if_none alt_names (map fst dtnames); |
|
512 |
||
513 |
fun get_constr t = (case Logic.strip_assums_concl t of |
|
514 |
_ $ (_ $ t') => (case head_of t' of |
|
515 |
Const (cname, cT) => (case strip_type cT of |
|
516 |
(Ts, Type (tname, _)) => (tname, (cname, map (dtyp_of_typ dtnames) Ts)) |
|
517 |
| _ => err t) |
|
518 |
| _ => err t) |
|
519 |
| _ => err t); |
|
520 |
||
521 |
fun make_dt_spec [] _ _ = [] |
|
522 |
| make_dt_spec ((tname, tvs)::dtnames') i constrs = |
|
523 |
let val (constrs', constrs'') = take_prefix (equal tname o fst) constrs |
|
524 |
in (i, (tname, map DtTFree tvs, map snd constrs')):: |
|
525 |
(make_dt_spec dtnames' (i + 1) constrs'') |
|
526 |
end; |
|
527 |
||
528 |
val descr = make_dt_spec dtnames 0 (map get_constr (prems_of induction')); |
|
529 |
val sorts = add_term_tfrees (concl_of induction', []); |
|
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
530 |
val dt_info = get_datatypes thy1; |
5177 | 531 |
|
6427 | 532 |
val _ = message ("Proofs for datatype(s) " ^ commas_quote new_type_names); |
5177 | 533 |
|
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
534 |
val (thy2, casedist_thms) = thy1 |> |
5177 | 535 |
DatatypeAbsProofs.prove_casedist_thms new_type_names [descr] sorts induction; |
536 |
val (thy3, reccomb_names, rec_thms) = DatatypeAbsProofs.prove_primrec_thms |
|
5661 | 537 |
false new_type_names [descr] sorts dt_info inject distinct induction thy2; |
538 |
val (thy4, case_names, case_thms) = DatatypeAbsProofs.prove_case_thms false |
|
5177 | 539 |
new_type_names [descr] sorts reccomb_names rec_thms thy3; |
540 |
val (thy5, split_thms) = DatatypeAbsProofs.prove_split_thms |
|
541 |
new_type_names [descr] sorts inject distinct casedist_thms case_thms thy4; |
|
542 |
val (thy6, nchotomys) = DatatypeAbsProofs.prove_nchotomys new_type_names |
|
543 |
[descr] sorts casedist_thms thy5; |
|
544 |
val (thy7, case_congs) = DatatypeAbsProofs.prove_case_congs new_type_names |
|
545 |
[descr] sorts nchotomys case_thms thy6; |
|
546 |
val (thy8, size_thms) = |
|
6394 | 547 |
if exists (equal "Arith") (Sign.stamp_names_of (Theory.sign_of thy7)) then |
5661 | 548 |
DatatypeAbsProofs.prove_size_thms false new_type_names |
5177 | 549 |
[descr] sorts reccomb_names rec_thms thy7 |
550 |
else (thy7, []); |
|
551 |
||
552 |
val dt_infos = map (make_dt_info descr induction reccomb_names rec_thms) |
|
553 |
((0 upto length descr - 1) ~~ descr ~~ case_names ~~ case_thms ~~ |
|
554 |
casedist_thms ~~ distinct ~~ inject ~~ nchotomys ~~ case_congs); |
|
555 |
||
556 |
val simps = flat (distinct @ inject @ case_thms) @ size_thms @ rec_thms; |
|
557 |
||
558 |
val thy9 = thy8 |> |
|
5661 | 559 |
Theory.add_path (space_implode "_" new_type_names) |> |
6092 | 560 |
PureThy.add_thmss [(("simps", simps), [])] |> |
5177 | 561 |
put_datatypes (foldr Symtab.update (dt_infos, dt_info)) |> |
562 |
Theory.parent_path; |
|
563 |
||
564 |
val _ = store_clasimp thy9 ((claset_of thy9, simpset_of thy9) |
|
565 |
addsimps2 flat case_thms addsimps2 size_thms addsimps2 rec_thms |
|
566 |
addIffs flat inject addDistinct (distinct, descr)); |
|
567 |
||
568 |
in |
|
569 |
(thy9, |
|
570 |
{distinct = distinct, |
|
571 |
inject = inject, |
|
572 |
exhaustion = casedist_thms, |
|
573 |
rec_thms = rec_thms, |
|
574 |
case_thms = case_thms, |
|
575 |
split_thms = split_thms, |
|
576 |
induction = induction, |
|
577 |
size = size_thms, |
|
578 |
simps = simps}) |
|
579 |
end; |
|
580 |
||
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
581 |
val rep_datatype = gen_rep_datatype IsarThy.apply_theorems; |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
582 |
val rep_datatype_i = gen_rep_datatype IsarThy.apply_theorems_i; |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
583 |
|
5177 | 584 |
|
585 |
(******************************** add datatype ********************************) |
|
586 |
||
5661 | 587 |
fun gen_add_datatype prep_typ flat_names new_type_names dts thy = |
5177 | 588 |
let |
589 |
val _ = Theory.requires thy "Datatype" "datatype definitions"; |
|
590 |
||
591 |
(* this theory is used just for parsing *) |
|
592 |
||
593 |
val tmp_thy = thy |> |
|
5892 | 594 |
Theory.copy |> |
5177 | 595 |
Theory.add_types (map (fn (tvs, tname, mx, _) => |
596 |
(tname, length tvs, mx)) dts); |
|
597 |
||
6394 | 598 |
val sign = Theory.sign_of tmp_thy; |
5177 | 599 |
|
5661 | 600 |
val (tyvars, _, _, _)::_ = dts; |
5177 | 601 |
val (new_dts, types_syntax) = ListPair.unzip (map (fn (tvs, tname, mx, _) => |
602 |
let val full_tname = Sign.full_name sign (Syntax.type_name tname mx) |
|
603 |
in (case duplicates tvs of |
|
5661 | 604 |
[] => if eq_set (tyvars, tvs) then ((full_tname, tvs), (tname, mx)) |
605 |
else error ("Mutually recursive datatypes must have same type parameters") |
|
5177 | 606 |
| dups => error ("Duplicate parameter(s) for datatype " ^ full_tname ^ |
607 |
" : " ^ commas dups)) |
|
608 |
end) dts); |
|
609 |
||
610 |
val _ = (case duplicates (map fst new_dts) @ duplicates new_type_names of |
|
611 |
[] => () | dups => error ("Duplicate datatypes: " ^ commas dups)); |
|
612 |
||
613 |
fun prep_dt_spec ((dts', constr_syntax, sorts, i), (tvs, tname, mx, constrs)) = |
|
614 |
let |
|
5661 | 615 |
fun prep_constr ((constrs, constr_syntax', sorts'), (cname, cargs, mx')) = |
5279 | 616 |
let |
617 |
val (cargs', sorts'') = foldl (prep_typ sign) (([], sorts'), cargs); |
|
618 |
val _ = (case foldr add_typ_tfree_names (cargs', []) \\ tvs of |
|
619 |
[] => () |
|
620 |
| vs => error ("Extra type variables on rhs: " ^ commas vs)) |
|
5661 | 621 |
in (constrs @ [((if flat_names then Sign.full_name sign else |
622 |
Sign.full_name_path sign tname) (Syntax.const_name cname mx'), |
|
5177 | 623 |
map (dtyp_of_typ new_dts) cargs')], |
624 |
constr_syntax' @ [(cname, mx')], sorts'') |
|
625 |
end handle ERROR => |
|
626 |
error ("The error above occured in constructor " ^ cname ^ |
|
627 |
" of datatype " ^ tname); |
|
628 |
||
629 |
val (constrs', constr_syntax', sorts') = |
|
630 |
foldl prep_constr (([], [], sorts), constrs) |
|
631 |
||
632 |
in |
|
633 |
case duplicates (map fst constrs') of |
|
634 |
[] => |
|
635 |
(dts' @ [(i, (Sign.full_name sign (Syntax.type_name tname mx), |
|
636 |
map DtTFree tvs, constrs'))], |
|
637 |
constr_syntax @ [constr_syntax'], sorts', i + 1) |
|
638 |
| dups => error ("Duplicate constructors " ^ commas dups ^ |
|
639 |
" in datatype " ^ tname) |
|
640 |
end; |
|
641 |
||
5661 | 642 |
val (dts', constr_syntax, sorts', i) = foldl prep_dt_spec (([], [], [], 0), dts); |
5177 | 643 |
val dt_info = get_datatypes thy; |
644 |
val (descr, _) = unfold_datatypes dt_info dts' i; |
|
645 |
val _ = check_nonempty descr; |
|
5661 | 646 |
val sorts = sorts' @ (map (rpair (Sign.defaultS sign)) (tyvars \\ map fst sorts')); |
5177 | 647 |
|
648 |
in |
|
649 |
(if (!quick_and_dirty) then add_datatype_axm else add_datatype_def) |
|
5661 | 650 |
flat_names new_type_names descr sorts types_syntax constr_syntax dt_info thy |
5177 | 651 |
end; |
652 |
||
653 |
val add_datatype_i = gen_add_datatype cert_typ; |
|
654 |
val add_datatype = gen_add_datatype read_typ; |
|
655 |
||
6360 | 656 |
|
657 |
(** package setup **) |
|
658 |
||
659 |
(* setup theory *) |
|
660 |
||
661 |
val setup = [DatatypesData.init]; |
|
662 |
||
663 |
||
664 |
(* outer syntax *) |
|
665 |
||
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
666 |
local open OuterParse in |
6360 | 667 |
|
668 |
val datatype_decl = |
|
669 |
Scan.option ($$$ "(" |-- name --| $$$ ")") -- type_args -- name -- opt_infix -- |
|
670 |
($$$ "=" |-- enum1 "|" (name -- Scan.repeat typ -- opt_mixfix)); |
|
671 |
||
672 |
fun mk_datatype args = |
|
673 |
let |
|
674 |
val names = map (fn ((((None, _), t), _), _) => t | ((((Some t, _), _), _), _) => t) args; |
|
675 |
val specs = map (fn ((((_, vs), t), mx), cons) => (vs, t, mx, map (fn ((x, y), z) => (x, y, z)) cons)) args; |
|
676 |
in #1 o add_datatype false names specs end; |
|
677 |
||
678 |
val datatypeP = |
|
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
679 |
OuterSyntax.command "datatype" "define inductive datatypes" |
6360 | 680 |
(enum1 "and" datatype_decl >> (Toplevel.theory o mk_datatype)); |
681 |
||
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
682 |
|
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
683 |
val rep_datatype_decl = |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
684 |
Scan.option (Scan.repeat1 name) -- |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
685 |
Scan.optional ($$$ "distinct" |-- !!! (and_list1 xthms1)) [] -- |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
686 |
Scan.optional ($$$ "inject" |-- !!! (and_list1 xthms1)) [] -- |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
687 |
($$$ "induct" |-- !!! xthm); |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
688 |
|
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
689 |
fun mk_rep_datatype (((opt_ts, dss), iss), ind) = #1 o rep_datatype opt_ts dss iss ind; |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
690 |
|
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
691 |
val rep_datatypeP = |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
692 |
OuterSyntax.command "rep_datatype" "represent existing types inductively" |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
693 |
(rep_datatype_decl >> (Toplevel.theory o mk_rep_datatype)); |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
694 |
|
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
695 |
|
6360 | 696 |
val _ = OuterSyntax.add_keywords ["distinct", "inject", "induct"]; |
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
697 |
val _ = OuterSyntax.add_parsers [datatypeP, rep_datatypeP]; |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
698 |
|
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
699 |
end; |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
700 |
|
6360 | 701 |
|
5177 | 702 |
end; |
703 |
||
6360 | 704 |
structure BasicDatatypePackage: BASIC_DATATYPE_PACKAGE = DatatypePackage; |
705 |
open BasicDatatypePackage; |