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