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