author | wenzelm |
Mon, 16 Nov 1998 11:14:02 +0100 | |
changeset 5891 | 92e0f5e6fd17 |
parent 5661 | 6ecb6ea25f19 |
child 6092 | d9db67970c73 |
permissions | -rw-r--r-- |
5177 | 1 |
(* Title: HOL/Tools/datatype_abs_proofs.ML |
2 |
ID: $Id$ |
|
3 |
Author: Stefan Berghofer |
|
4 |
Copyright 1998 TU Muenchen |
|
5 |
||
6 |
Proofs and defintions independent of concrete representation |
|
7 |
of datatypes (i.e. requiring only abstract properties such as |
|
8 |
injectivity / distinctness of constructors and induction) |
|
9 |
||
10 |
- case distinction (exhaustion) theorems |
|
11 |
- characteristic equations for primrec combinators |
|
12 |
- characteristic equations for case combinators |
|
13 |
- distinctness of constructors (external version) |
|
14 |
- equations for splitting "P (case ...)" expressions |
|
15 |
- datatype size function |
|
16 |
- "nchotomy" and "case_cong" theorems for TFL |
|
17 |
||
18 |
*) |
|
19 |
||
20 |
signature DATATYPE_ABS_PROOFS = |
|
21 |
sig |
|
22 |
val prove_casedist_thms : string list -> (int * (string * DatatypeAux.dtyp list * |
|
23 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
24 |
thm -> theory -> theory * thm list |
|
5661 | 25 |
val prove_primrec_thms : bool -> string list -> (int * (string * DatatypeAux.dtyp list * |
5177 | 26 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
27 |
DatatypeAux.datatype_info Symtab.table -> thm list list -> thm list list -> |
|
28 |
thm -> theory -> theory * string list * thm list |
|
5661 | 29 |
val prove_case_thms : bool -> string list -> (int * (string * DatatypeAux.dtyp list * |
5177 | 30 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
31 |
string list -> thm list -> theory -> theory * string list * thm list list |
|
5661 | 32 |
val prove_distinctness_thms : bool -> string list -> (int * (string * DatatypeAux.dtyp list * |
5177 | 33 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
34 |
thm list list -> thm list list -> theory -> theory * thm list list |
|
35 |
val prove_split_thms : string list -> (int * (string * DatatypeAux.dtyp list * |
|
36 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
37 |
thm list list -> thm list list -> thm list -> thm list list -> theory -> |
|
38 |
theory * (thm * thm) list |
|
5661 | 39 |
val prove_size_thms : bool -> string list -> (int * (string * DatatypeAux.dtyp list * |
5177 | 40 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
41 |
string list -> thm list -> theory -> theory * thm list |
|
42 |
val prove_nchotomys : string list -> (int * (string * DatatypeAux.dtyp list * |
|
43 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
44 |
thm list -> theory -> theory * thm list |
|
45 |
val prove_case_congs : string list -> (int * (string * DatatypeAux.dtyp list * |
|
46 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
47 |
thm list -> thm list list -> theory -> theory * thm list |
|
48 |
end; |
|
49 |
||
50 |
structure DatatypeAbsProofs : DATATYPE_ABS_PROOFS = |
|
51 |
struct |
|
52 |
||
53 |
open DatatypeAux; |
|
54 |
||
55 |
val thin = read_instantiate_sg (sign_of Set.thy) [("V", "?X : ?Y")] thin_rl; |
|
56 |
||
57 |
val (_ $ (_ $ (_ $ (distinct_f $ _) $ _))) = hd (prems_of distinct_lemma); |
|
58 |
||
59 |
(************************ case distinction theorems ***************************) |
|
60 |
||
61 |
fun prove_casedist_thms new_type_names descr sorts induct thy = |
|
62 |
let |
|
5661 | 63 |
val _ = message "Proving case distinction theorems..."; |
5177 | 64 |
|
65 |
val descr' = flat descr; |
|
66 |
val recTs = get_rec_types descr' sorts; |
|
67 |
val newTs = take (length (hd descr), recTs); |
|
68 |
||
69 |
val induct_Ps = map head_of (dest_conj (HOLogic.dest_Trueprop (concl_of induct))); |
|
70 |
||
71 |
fun prove_casedist_thm ((i, t), T) = |
|
72 |
let |
|
73 |
val dummyPs = map (fn (Var (_, Type (_, [T', T'']))) => |
|
74 |
Abs ("z", T', Const ("True", T''))) induct_Ps; |
|
75 |
val P = Abs ("z", T, HOLogic.imp $ HOLogic.mk_eq (Var (("a", 0), T), Bound 0) $ |
|
76 |
Var (("P", 0), HOLogic.boolT)) |
|
77 |
val insts = take (i, dummyPs) @ (P::(drop (i + 1, dummyPs))); |
|
78 |
val cert = cterm_of (sign_of thy); |
|
79 |
val insts' = (map cert induct_Ps) ~~ (map cert insts); |
|
80 |
val induct' = refl RS ((nth_elem (i, |
|
81 |
split_conj_thm (cterm_instantiate insts' induct))) RSN (2, rev_mp)) |
|
82 |
||
83 |
in prove_goalw_cterm [] (cert t) (fn prems => |
|
84 |
[rtac induct' 1, |
|
85 |
REPEAT (rtac TrueI 1), |
|
86 |
REPEAT ((rtac impI 1) THEN (eresolve_tac prems 1)), |
|
87 |
REPEAT (rtac TrueI 1)]) |
|
88 |
end; |
|
89 |
||
90 |
val casedist_thms = map prove_casedist_thm ((0 upto (length newTs - 1)) ~~ |
|
91 |
(DatatypeProp.make_casedists descr sorts) ~~ newTs) |
|
92 |
||
93 |
in |
|
94 |
(store_thms "exhaust" new_type_names casedist_thms thy, casedist_thms) |
|
95 |
end; |
|
96 |
||
97 |
(*************************** primrec combinators ******************************) |
|
98 |
||
5661 | 99 |
fun prove_primrec_thms flat_names new_type_names descr sorts |
5177 | 100 |
(dt_info : datatype_info Symtab.table) constr_inject dist_rewrites induct thy = |
101 |
let |
|
5661 | 102 |
val _ = message "Constructing primrec combinators..."; |
103 |
||
104 |
val big_name = space_implode "_" new_type_names; |
|
105 |
val thy0 = add_path flat_names big_name thy; |
|
5177 | 106 |
|
107 |
val descr' = flat descr; |
|
108 |
val recTs = get_rec_types descr' sorts; |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5553
diff
changeset
|
109 |
val used = foldr add_typ_tfree_names (recTs, []); |
5177 | 110 |
val newTs = take (length (hd descr), recTs); |
111 |
||
112 |
val induct_Ps = map head_of (dest_conj (HOLogic.dest_Trueprop (concl_of induct))); |
|
113 |
||
5661 | 114 |
val big_rec_name' = big_name ^ "_rec_set"; |
115 |
val rec_set_names = map (Sign.full_name (sign_of thy0)) |
|
5177 | 116 |
(if length descr' = 1 then [big_rec_name'] else |
117 |
(map ((curry (op ^) (big_rec_name' ^ "_")) o string_of_int) |
|
118 |
(1 upto (length descr')))); |
|
119 |
||
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5553
diff
changeset
|
120 |
val rec_result_Ts = map TFree (variantlist (replicate (length descr') "'t", used) ~~ |
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5553
diff
changeset
|
121 |
replicate (length descr') HOLogic.termS); |
5177 | 122 |
|
123 |
val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) => |
|
124 |
map (fn (_, cargs) => |
|
125 |
let |
|
126 |
val recs = filter is_rec_type cargs; |
|
127 |
val argTs = (map (typ_of_dtyp descr' sorts) cargs) @ |
|
128 |
(map (fn r => nth_elem (dest_DtRec r, rec_result_Ts)) recs) |
|
129 |
in argTs ---> nth_elem (i, rec_result_Ts) |
|
130 |
end) constrs) descr'); |
|
131 |
||
132 |
val rec_set_Ts = map (fn (T1, T2) => reccomb_fn_Ts ---> HOLogic.mk_setT |
|
133 |
(HOLogic.mk_prodT (T1, T2))) (recTs ~~ rec_result_Ts); |
|
134 |
||
135 |
val rec_fns = map (uncurry (mk_Free "f")) |
|
136 |
(reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts))); |
|
137 |
val rec_sets = map (fn c => list_comb (Const c, rec_fns)) |
|
138 |
(rec_set_names ~~ rec_set_Ts); |
|
139 |
||
140 |
(* introduction rules for graph of primrec function *) |
|
141 |
||
142 |
fun make_rec_intr T set_name ((rec_intr_ts, l), (cname, cargs)) = |
|
143 |
let |
|
144 |
fun mk_prem (dt, (j, k, prems, t1s, t2s)) = |
|
145 |
let |
|
146 |
val T = typ_of_dtyp descr' sorts dt; |
|
147 |
val free1 = mk_Free "x" T j |
|
148 |
in (case dt of |
|
149 |
DtRec m => |
|
150 |
let val free2 = mk_Free "y" (nth_elem (m, rec_result_Ts)) k |
|
151 |
in (j + 1, k + 1, (HOLogic.mk_Trueprop (HOLogic.mk_mem |
|
152 |
(HOLogic.mk_prod (free1, free2), nth_elem (m, rec_sets))))::prems, |
|
153 |
free1::t1s, free2::t2s) |
|
154 |
end |
|
155 |
| _ => (j + 1, k, prems, free1::t1s, t2s)) |
|
156 |
end; |
|
157 |
||
158 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
159 |
val (_, _, prems, t1s, t2s) = foldr mk_prem (cargs, (1, 1, [], [], [])) |
|
160 |
||
161 |
in (rec_intr_ts @ [Logic.list_implies (prems, HOLogic.mk_Trueprop (HOLogic.mk_mem |
|
162 |
(HOLogic.mk_prod (list_comb (Const (cname, Ts ---> T), t1s), |
|
163 |
list_comb (nth_elem (l, rec_fns), t1s @ t2s)), set_name)))], l + 1) |
|
164 |
end; |
|
165 |
||
166 |
val (rec_intr_ts, _) = foldl (fn (x, ((d, T), set_name)) => |
|
167 |
foldl (make_rec_intr T set_name) (x, #3 (snd d))) |
|
168 |
(([], 0), descr' ~~ recTs ~~ rec_sets); |
|
169 |
||
170 |
val (thy1, {intrs = rec_intrs, elims = rec_elims, ...}) = |
|
5661 | 171 |
setmp InductivePackage.quiet_mode (!quiet_mode) |
172 |
(InductivePackage.add_inductive_i false true big_rec_name' false false true |
|
173 |
rec_sets rec_intr_ts [] []) thy0; |
|
5177 | 174 |
|
175 |
(* prove uniqueness and termination of primrec combinators *) |
|
176 |
||
5661 | 177 |
val _ = message "Proving termination and uniqueness of primrec functions..."; |
5177 | 178 |
|
179 |
fun mk_unique_tac ((tac, intrs), ((((i, (tname, _, constrs)), elim), T), T')) = |
|
180 |
let |
|
181 |
val distinct_tac = (etac Pair_inject 1) THEN |
|
182 |
(if i < length newTs then |
|
183 |
full_simp_tac (HOL_ss addsimps (nth_elem (i, dist_rewrites))) 1 |
|
184 |
else full_simp_tac (HOL_ss addsimps |
|
185 |
((#distinct (the (Symtab.lookup (dt_info, tname)))) @ |
|
186 |
[Suc_Suc_eq, Suc_not_Zero, Zero_not_Suc])) 1); |
|
187 |
||
188 |
val inject = map (fn r => r RS iffD1) |
|
189 |
(if i < length newTs then nth_elem (i, constr_inject) |
|
190 |
else #inject (the (Symtab.lookup (dt_info, tname)))); |
|
191 |
||
192 |
fun mk_unique_constr_tac n ((tac, intr::intrs, j), (cname, cargs)) = |
|
193 |
let |
|
194 |
val k = length (filter is_rec_type cargs) |
|
195 |
||
196 |
in (EVERY [DETERM tac, |
|
197 |
REPEAT (etac ex1E 1), rtac ex1I 1, |
|
198 |
DEPTH_SOLVE_1 (ares_tac [intr] 1), |
|
199 |
REPEAT_DETERM_N k (etac thin 1), |
|
200 |
etac elim 1, |
|
201 |
REPEAT_DETERM_N j distinct_tac, |
|
202 |
etac Pair_inject 1, TRY (dresolve_tac inject 1), |
|
203 |
REPEAT (etac conjE 1), hyp_subst_tac 1, |
|
204 |
REPEAT (etac allE 1), |
|
205 |
REPEAT (dtac mp 1 THEN atac 1), |
|
206 |
TRY (hyp_subst_tac 1), |
|
207 |
rtac refl 1, |
|
208 |
REPEAT_DETERM_N (n - j - 1) distinct_tac], |
|
209 |
intrs, j + 1) |
|
210 |
end; |
|
211 |
||
212 |
val (tac', intrs', _) = foldl (mk_unique_constr_tac (length constrs)) |
|
213 |
((tac, intrs, 0), constrs); |
|
214 |
||
215 |
in (tac', intrs') end; |
|
216 |
||
217 |
val rec_unique_thms = |
|
218 |
let |
|
219 |
val rec_unique_ts = map (fn (((set_t, T1), T2), i) => |
|
220 |
Const ("Ex1", (T2 --> HOLogic.boolT) --> HOLogic.boolT) $ |
|
221 |
absfree ("y", T2, HOLogic.mk_mem (HOLogic.mk_prod |
|
222 |
(mk_Free "x" T1 i, Free ("y", T2)), set_t))) |
|
223 |
(rec_sets ~~ recTs ~~ rec_result_Ts ~~ (1 upto length recTs)); |
|
224 |
val cert = cterm_of (sign_of thy1) |
|
225 |
val insts = map (fn ((i, T), t) => absfree ("x" ^ (string_of_int i), T, t)) |
|
226 |
((1 upto length recTs) ~~ recTs ~~ rec_unique_ts); |
|
227 |
val induct' = cterm_instantiate ((map cert induct_Ps) ~~ |
|
228 |
(map cert insts)) induct; |
|
229 |
val (tac, _) = foldl mk_unique_tac |
|
230 |
((rtac induct' 1, rec_intrs), descr' ~~ rec_elims ~~ recTs ~~ rec_result_Ts) |
|
231 |
||
232 |
in split_conj_thm (prove_goalw_cterm [] |
|
233 |
(cert (HOLogic.mk_Trueprop (mk_conj rec_unique_ts))) (K [tac])) |
|
234 |
end; |
|
235 |
||
236 |
val rec_total_thms = map (fn r => |
|
237 |
r RS ex1_implies_ex RS (select_eq_Ex RS iffD2)) rec_unique_thms; |
|
238 |
||
239 |
(* define primrec combinators *) |
|
240 |
||
241 |
val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec"; |
|
242 |
val reccomb_names = map (Sign.full_name (sign_of thy1)) |
|
243 |
(if length descr' = 1 then [big_reccomb_name] else |
|
244 |
(map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int) |
|
245 |
(1 upto (length descr')))); |
|
246 |
val reccombs = map (fn ((name, T), T') => list_comb |
|
247 |
(Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns)) |
|
248 |
(reccomb_names ~~ recTs ~~ rec_result_Ts); |
|
249 |
||
250 |
val thy2 = thy1 |> |
|
251 |
Theory.add_consts_i (map (fn ((name, T), T') => |
|
252 |
(Sign.base_name name, reccomb_fn_Ts @ [T] ---> T', NoSyn)) |
|
253 |
(reccomb_names ~~ recTs ~~ rec_result_Ts)) |> |
|
254 |
Theory.add_defs_i (map (fn ((((name, comb), set), T), T') => |
|
255 |
((Sign.base_name name) ^ "_def", Logic.mk_equals |
|
256 |
(comb $ Free ("x", T), |
|
257 |
Const ("Eps", (T' --> HOLogic.boolT) --> T') $ absfree ("y", T', |
|
258 |
HOLogic.mk_mem (HOLogic.mk_prod (Free ("x", T), Free ("y", T')), set))))) |
|
5661 | 259 |
(reccomb_names ~~ reccombs ~~ rec_sets ~~ recTs ~~ rec_result_Ts)) |> |
260 |
parent_path flat_names; |
|
5177 | 261 |
|
262 |
val reccomb_defs = map ((get_def thy2) o Sign.base_name) reccomb_names; |
|
263 |
||
264 |
(* prove characteristic equations for primrec combinators *) |
|
265 |
||
5661 | 266 |
val _ = message "Proving characteristic theorems for primrec combinators..." |
5177 | 267 |
|
268 |
val rec_thms = map (fn t => prove_goalw_cterm reccomb_defs |
|
269 |
(cterm_of (sign_of thy2) t) (fn _ => |
|
270 |
[rtac select1_equality 1, |
|
271 |
resolve_tac rec_unique_thms 1, |
|
272 |
resolve_tac rec_intrs 1, |
|
273 |
REPEAT (resolve_tac rec_total_thms 1)])) |
|
274 |
(DatatypeProp.make_primrecs new_type_names descr sorts thy2) |
|
275 |
||
276 |
in |
|
5661 | 277 |
(thy2 |> Theory.add_path (space_implode "_" new_type_names) |> |
5891 | 278 |
PureThy.add_tthmss [(("recs", Attribute.tthms_of rec_thms), [])] |> |
5661 | 279 |
Theory.parent_path, |
5177 | 280 |
reccomb_names, rec_thms) |
281 |
end; |
|
282 |
||
283 |
(***************************** case combinators *******************************) |
|
284 |
||
5661 | 285 |
fun prove_case_thms flat_names new_type_names descr sorts reccomb_names primrec_thms thy = |
5177 | 286 |
let |
5661 | 287 |
val _ = message "Proving characteristic theorems for case combinators..."; |
288 |
||
289 |
val thy1 = add_path flat_names (space_implode "_" new_type_names) thy; |
|
5177 | 290 |
|
291 |
val descr' = flat descr; |
|
292 |
val recTs = get_rec_types descr' sorts; |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5553
diff
changeset
|
293 |
val used = foldr add_typ_tfree_names (recTs, []); |
5177 | 294 |
val newTs = take (length (hd descr), recTs); |
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5553
diff
changeset
|
295 |
val T' = TFree (variant used "'t", HOLogic.termS); |
5177 | 296 |
|
297 |
val case_dummy_fns = map (fn (_, (_, _, constrs)) => map (fn (_, cargs) => |
|
298 |
let |
|
299 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5553
diff
changeset
|
300 |
val Ts' = replicate (length (filter is_rec_type cargs)) T' |
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5553
diff
changeset
|
301 |
in Const ("arbitrary", Ts @ Ts' ---> T') |
5177 | 302 |
end) constrs) descr'; |
303 |
||
304 |
val case_names = map (fn s => |
|
5661 | 305 |
Sign.full_name (sign_of thy1) (s ^ "_case")) new_type_names; |
5177 | 306 |
|
307 |
(* define case combinators via primrec combinators *) |
|
308 |
||
309 |
val (case_defs, thy2) = foldl (fn ((defs, thy), |
|
310 |
((((i, (_, _, constrs)), T), name), recname)) => |
|
311 |
let |
|
312 |
val (fns1, fns2) = ListPair.unzip (map (fn ((_, cargs), j) => |
|
313 |
let |
|
314 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
315 |
val Ts' = Ts @ (replicate (length (filter is_rec_type cargs)) T'); |
|
316 |
val frees' = map (uncurry (mk_Free "x")) (Ts' ~~ (1 upto length Ts')); |
|
317 |
val frees = take (length cargs, frees'); |
|
318 |
val free = mk_Free "f" (Ts ---> T') j |
|
319 |
in |
|
320 |
(free, list_abs_free (map dest_Free frees', |
|
321 |
list_comb (free, frees))) |
|
322 |
end) (constrs ~~ (1 upto length constrs))); |
|
323 |
||
324 |
val caseT = (map (snd o dest_Free) fns1) @ [T] ---> T'; |
|
325 |
val fns = (flat (take (i, case_dummy_fns))) @ |
|
326 |
fns2 @ (flat (drop (i + 1, case_dummy_fns))); |
|
327 |
val reccomb = Const (recname, (map fastype_of fns) @ [T] ---> T'); |
|
328 |
val decl = (Sign.base_name name, caseT, NoSyn); |
|
329 |
val def = ((Sign.base_name name) ^ "_def", |
|
330 |
Logic.mk_equals (list_comb (Const (name, caseT), fns1), |
|
331 |
list_comb (reccomb, (flat (take (i, case_dummy_fns))) @ |
|
332 |
fns2 @ (flat (drop (i + 1, case_dummy_fns))) ))); |
|
333 |
val thy' = thy |> |
|
334 |
Theory.add_consts_i [decl] |> Theory.add_defs_i [def]; |
|
335 |
||
336 |
in (defs @ [get_def thy' (Sign.base_name name)], thy') |
|
5661 | 337 |
end) (([], thy1), (hd descr) ~~ newTs ~~ case_names ~~ |
5177 | 338 |
(take (length newTs, reccomb_names))); |
339 |
||
340 |
val case_thms = map (map (fn t => prove_goalw_cterm (case_defs @ |
|
5553 | 341 |
(map mk_meta_eq primrec_thms)) (cterm_of (sign_of thy2) t) |
5177 | 342 |
(fn _ => [rtac refl 1]))) |
343 |
(DatatypeProp.make_cases new_type_names descr sorts thy2); |
|
344 |
||
5661 | 345 |
val thy3 = thy2 |> Theory.add_trrules_i |
346 |
(DatatypeProp.make_case_trrules new_type_names descr) |> |
|
347 |
parent_path flat_names; |
|
5177 | 348 |
|
5661 | 349 |
in |
350 |
(store_thmss "cases" new_type_names case_thms thy3, case_names, case_thms) |
|
5177 | 351 |
end; |
352 |
||
353 |
(************************ distinctness of constructors ************************) |
|
354 |
||
5661 | 355 |
fun prove_distinctness_thms flat_names new_type_names descr sorts dist_rewrites case_thms thy = |
5177 | 356 |
let |
5661 | 357 |
val thy1 = add_path flat_names (space_implode "_" new_type_names) thy; |
358 |
||
5177 | 359 |
val descr' = flat descr; |
360 |
val recTs = get_rec_types descr' sorts; |
|
361 |
val newTs = take (length (hd descr), recTs); |
|
362 |
||
363 |
(*--------------------------------------------------------------------*) |
|
364 |
(* define t_ord - functions for proving distinctness of constructors: *) |
|
365 |
(* t_ord C_i ... = i *) |
|
366 |
(*--------------------------------------------------------------------*) |
|
367 |
||
368 |
fun define_ord ((thy, ord_defs), (((_, (_, _, constrs)), T), tname)) = |
|
369 |
if length constrs < DatatypeProp.dtK then (thy, ord_defs) |
|
370 |
else |
|
371 |
let |
|
372 |
val Tss = map ((map (typ_of_dtyp descr' sorts)) o snd) constrs; |
|
373 |
val ts = map HOLogic.mk_nat (0 upto length constrs - 1); |
|
374 |
val mk_abs = foldr (fn (T, t') => Abs ("x", T, t')); |
|
375 |
val fs = map mk_abs (Tss ~~ ts); |
|
376 |
val fTs = map (fn Ts => Ts ---> HOLogic.natT) Tss; |
|
377 |
val ord_name = Sign.full_name (sign_of thy) (tname ^ "_ord"); |
|
378 |
val case_name = Sign.intern_const (sign_of thy) (tname ^ "_case"); |
|
379 |
val ordT = T --> HOLogic.natT; |
|
380 |
val caseT = fTs ---> ordT; |
|
381 |
val defpair = (tname ^ "_ord_def", Logic.mk_equals |
|
382 |
(Const (ord_name, ordT), list_comb (Const (case_name, caseT), fs))); |
|
383 |
val thy' = thy |> |
|
384 |
Theory.add_consts_i [(tname ^ "_ord", ordT, NoSyn)] |> |
|
385 |
Theory.add_defs_i [defpair]; |
|
386 |
val def = get_def thy' (tname ^ "_ord") |
|
387 |
||
388 |
in (thy', ord_defs @ [def]) end; |
|
389 |
||
390 |
val (thy2, ord_defs) = |
|
5661 | 391 |
foldl define_ord ((thy1, []), (hd descr) ~~ newTs ~~ new_type_names); |
5177 | 392 |
|
393 |
(**** number of constructors < dtK ****) |
|
394 |
||
395 |
fun prove_distinct_thms _ [] = [] |
|
396 |
| prove_distinct_thms dist_rewrites' (t::_::ts) = |
|
397 |
let |
|
398 |
val dist_thm = prove_goalw_cterm [] (cterm_of (sign_of thy2) t) (fn _ => |
|
399 |
[simp_tac (HOL_ss addsimps dist_rewrites') 1]) |
|
400 |
in dist_thm::(standard (dist_thm RS not_sym)):: |
|
401 |
(prove_distinct_thms dist_rewrites' ts) |
|
402 |
end; |
|
403 |
||
404 |
val distinct_thms = map (fn ((((_, (_, _, constrs)), ts), |
|
405 |
dist_rewrites'), case_thms) => |
|
406 |
if length constrs < DatatypeProp.dtK then |
|
407 |
prove_distinct_thms dist_rewrites' ts |
|
408 |
else |
|
409 |
let |
|
410 |
val t::ts' = rev ts; |
|
411 |
val (_ $ (_ $ (_ $ (f $ _) $ _))) = hd (Logic.strip_imp_prems t); |
|
412 |
val cert = cterm_of (sign_of thy2); |
|
413 |
val distinct_lemma' = cterm_instantiate |
|
414 |
[(cert distinct_f, cert f)] distinct_lemma; |
|
5553 | 415 |
val rewrites = ord_defs @ (map mk_meta_eq case_thms) |
5177 | 416 |
in |
417 |
(map (fn t => prove_goalw_cterm rewrites (cert t) |
|
418 |
(fn _ => [rtac refl 1])) (rev ts')) @ [standard distinct_lemma'] |
|
419 |
end) ((hd descr) ~~ (DatatypeProp.make_distincts new_type_names |
|
420 |
descr sorts thy2) ~~ dist_rewrites ~~ case_thms) |
|
421 |
||
5661 | 422 |
in |
423 |
(thy2 |> parent_path flat_names |> |
|
424 |
store_thmss "distinct" new_type_names distinct_thms, |
|
425 |
distinct_thms) |
|
5177 | 426 |
end; |
427 |
||
428 |
(******************************* case splitting *******************************) |
|
429 |
||
430 |
fun prove_split_thms new_type_names descr sorts constr_inject dist_rewrites |
|
431 |
casedist_thms case_thms thy = |
|
432 |
let |
|
5661 | 433 |
val _ = message "Proving equations for case splitting..."; |
5177 | 434 |
|
435 |
val descr' = flat descr; |
|
436 |
val recTs = get_rec_types descr' sorts; |
|
437 |
val newTs = take (length (hd descr), recTs); |
|
438 |
||
439 |
fun prove_split_thms ((((((t1, t2), inject), dist_rewrites'), |
|
440 |
exhaustion), case_thms'), T) = |
|
441 |
let |
|
442 |
val cert = cterm_of (sign_of thy); |
|
443 |
val _ $ (_ $ lhs $ _) = hd (Logic.strip_assums_hyp (hd (prems_of exhaustion))); |
|
444 |
val exhaustion' = cterm_instantiate |
|
445 |
[(cert lhs, cert (Free ("x", T)))] exhaustion; |
|
446 |
val tacsf = K [rtac exhaustion' 1, ALLGOALS (asm_simp_tac |
|
447 |
(HOL_ss addsimps (dist_rewrites' @ inject @ case_thms')))] |
|
448 |
in |
|
449 |
(prove_goalw_cterm [] (cert t1) tacsf, |
|
450 |
prove_goalw_cterm [] (cert t2) tacsf) |
|
451 |
end; |
|
452 |
||
453 |
val split_thm_pairs = map prove_split_thms |
|
454 |
((DatatypeProp.make_splits new_type_names descr sorts thy) ~~ constr_inject ~~ |
|
455 |
dist_rewrites ~~ casedist_thms ~~ case_thms ~~ newTs); |
|
456 |
||
457 |
val (split_thms, split_asm_thms) = ListPair.unzip split_thm_pairs |
|
458 |
||
459 |
in |
|
460 |
(thy |> store_thms "split" new_type_names split_thms |> |
|
461 |
store_thms "split_asm" new_type_names split_asm_thms, |
|
462 |
split_thm_pairs) |
|
463 |
end; |
|
464 |
||
465 |
(******************************* size functions *******************************) |
|
466 |
||
5661 | 467 |
fun prove_size_thms flat_names new_type_names descr sorts reccomb_names primrec_thms thy = |
5177 | 468 |
let |
5661 | 469 |
val _ = message "Proving equations for size function..."; |
470 |
||
471 |
val big_name = space_implode "_" new_type_names; |
|
472 |
val thy1 = add_path flat_names big_name thy; |
|
5177 | 473 |
|
474 |
val descr' = flat descr; |
|
475 |
val recTs = get_rec_types descr' sorts; |
|
476 |
||
477 |
val big_size_name = space_implode "_" new_type_names ^ "_size"; |
|
5661 | 478 |
val size_name = Sign.intern_const (sign_of (the (get_thy "Arith" thy1))) "size"; |
5177 | 479 |
val size_names = replicate (length (hd descr)) size_name @ |
5661 | 480 |
map (Sign.full_name (sign_of thy1)) |
5177 | 481 |
(if length (flat (tl descr)) = 1 then [big_size_name] else |
482 |
map (fn i => big_size_name ^ "_" ^ string_of_int i) |
|
483 |
(1 upto length (flat (tl descr)))); |
|
484 |
val def_names = map (fn i => big_size_name ^ "_def_" ^ string_of_int i) |
|
485 |
(1 upto length recTs); |
|
486 |
||
487 |
val plus_t = Const ("op +", [HOLogic.natT, HOLogic.natT] ---> HOLogic.natT); |
|
488 |
||
489 |
fun make_sizefun (_, cargs) = |
|
490 |
let |
|
491 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
492 |
val k = length (filter is_rec_type cargs); |
|
493 |
val t = if k = 0 then HOLogic.zero else |
|
494 |
foldl1 (app plus_t) (map Bound (k - 1 downto 0) @ [HOLogic.mk_nat 1]) |
|
495 |
in |
|
496 |
foldr (fn (T, t') => Abs ("x", T, t')) (Ts @ replicate k HOLogic.natT, t) |
|
497 |
end; |
|
498 |
||
499 |
val fs = flat (map (fn (_, (_, _, constrs)) => map make_sizefun constrs) descr'); |
|
500 |
val fTs = map fastype_of fs; |
|
501 |
||
5661 | 502 |
val thy' = thy1 |> |
5177 | 503 |
Theory.add_consts_i (map (fn (s, T) => |
504 |
(Sign.base_name s, T --> HOLogic.natT, NoSyn)) |
|
505 |
(drop (length (hd descr), size_names ~~ recTs))) |> |
|
506 |
Theory.add_defs_i (map (fn (((s, T), def_name), rec_name) => |
|
507 |
(def_name, Logic.mk_equals (Const (s, T --> HOLogic.natT), |
|
508 |
list_comb (Const (rec_name, fTs @ [T] ---> HOLogic.natT), fs)))) |
|
5661 | 509 |
(size_names ~~ recTs ~~ def_names ~~ reccomb_names)) |> |
510 |
parent_path flat_names; |
|
5177 | 511 |
|
512 |
val size_def_thms = map (get_axiom thy') def_names; |
|
5553 | 513 |
val rewrites = size_def_thms @ map mk_meta_eq primrec_thms; |
5177 | 514 |
|
515 |
val size_thms = map (fn t => prove_goalw_cterm rewrites |
|
516 |
(cterm_of (sign_of thy') t) (fn _ => [rtac refl 1])) |
|
517 |
(DatatypeProp.make_size new_type_names descr sorts thy') |
|
518 |
||
519 |
in |
|
5661 | 520 |
(thy' |> Theory.add_path big_name |> |
5891 | 521 |
PureThy.add_tthmss [(("size", Attribute.tthms_of size_thms), [])] |> |
5661 | 522 |
Theory.parent_path, |
5177 | 523 |
size_thms) |
524 |
end; |
|
525 |
||
526 |
(************************* additional theorems for TFL ************************) |
|
527 |
||
528 |
fun prove_nchotomys new_type_names descr sorts casedist_thms thy = |
|
529 |
let |
|
5661 | 530 |
val _ = message "Proving additional theorems for TFL..."; |
5177 | 531 |
|
532 |
fun prove_nchotomy (t, exhaustion) = |
|
533 |
let |
|
534 |
(* For goal i, select the correct disjunct to attack, then prove it *) |
|
535 |
fun tac i 0 = EVERY [TRY (rtac disjI1 i), |
|
536 |
hyp_subst_tac i, REPEAT (rtac exI i), rtac refl i] |
|
537 |
| tac i n = rtac disjI2 i THEN tac i (n - 1) |
|
538 |
in |
|
539 |
prove_goalw_cterm [] (cterm_of (sign_of thy) t) (fn _ => |
|
540 |
[rtac allI 1, |
|
541 |
exh_tac (K exhaustion) 1, |
|
542 |
ALLGOALS (fn i => tac i (i-1))]) |
|
543 |
end; |
|
544 |
||
545 |
val nchotomys = |
|
546 |
map prove_nchotomy (DatatypeProp.make_nchotomys descr sorts ~~ casedist_thms) |
|
547 |
||
548 |
in |
|
549 |
(store_thms "nchotomy" new_type_names nchotomys thy, nchotomys) |
|
550 |
end; |
|
551 |
||
552 |
fun prove_case_congs new_type_names descr sorts nchotomys case_thms thy = |
|
553 |
let |
|
554 |
fun prove_case_cong ((t, nchotomy), case_rewrites) = |
|
555 |
let |
|
556 |
val (Const ("==>", _) $ tm $ _) = t; |
|
557 |
val (Const ("Trueprop", _) $ (Const ("op =", _) $ _ $ Ma)) = tm; |
|
558 |
val cert = cterm_of (sign_of thy); |
|
559 |
val nchotomy' = nchotomy RS spec; |
|
560 |
val nchotomy'' = cterm_instantiate |
|
561 |
[(cert (hd (add_term_vars (concl_of nchotomy', []))), cert Ma)] nchotomy' |
|
562 |
in |
|
563 |
prove_goalw_cterm [] (cert t) (fn prems => |
|
564 |
let val simplify = asm_simp_tac (HOL_ss addsimps (prems @ case_rewrites)) |
|
565 |
in [simp_tac (HOL_ss addsimps [hd prems]) 1, |
|
566 |
cut_facts_tac [nchotomy''] 1, |
|
567 |
REPEAT (etac disjE 1 THEN REPEAT (etac exE 1) THEN simplify 1), |
|
568 |
REPEAT (etac exE 1) THEN simplify 1 (* Get last disjunct *)] |
|
569 |
end) |
|
570 |
end; |
|
571 |
||
572 |
val case_congs = map prove_case_cong (DatatypeProp.make_case_congs |
|
573 |
new_type_names descr sorts thy ~~ nchotomys ~~ case_thms) |
|
574 |
||
575 |
in |
|
576 |
(store_thms "case_cong" new_type_names case_congs thy, case_congs) |
|
577 |
end; |
|
578 |
||
579 |
end; |