author | blanchet |
Sat, 26 Apr 2014 06:43:06 +0200 | |
changeset 56737 | e4f363e16bdc |
parent 56684 | d8f32f55e463 |
child 56966 | 01637dd1260c |
permissions | -rw-r--r-- |
56638 | 1 |
(* Title: HOL/Tools/BNF/bnf_lfp_size.ML |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
3 |
Copyright 2014 |
|
4 |
||
5 |
Generation of size functions for new-style datatypes. |
|
6 |
*) |
|
7 |
||
56642
15cd15f9b40c
allow registration of custom size functions for BNF-based datatypes
blanchet
parents:
56640
diff
changeset
|
8 |
signature BNF_LFP_SIZE = |
15cd15f9b40c
allow registration of custom size functions for BNF-based datatypes
blanchet
parents:
56640
diff
changeset
|
9 |
sig |
56651 | 10 |
val register_size: string -> string -> thm list -> thm list -> local_theory -> local_theory |
11 |
val register_size_global: string -> string -> thm list -> thm list -> theory -> theory |
|
12 |
val lookup_size: Proof.context -> string -> (string * (thm list * thm list)) option |
|
13 |
val lookup_size_global: theory -> string -> (string * (thm list * thm list)) option |
|
14 |
val generate_lfp_size: BNF_FP_Util.fp_sugar list -> local_theory -> local_theory |
|
56642
15cd15f9b40c
allow registration of custom size functions for BNF-based datatypes
blanchet
parents:
56640
diff
changeset
|
15 |
end; |
15cd15f9b40c
allow registration of custom size functions for BNF-based datatypes
blanchet
parents:
56640
diff
changeset
|
16 |
|
15cd15f9b40c
allow registration of custom size functions for BNF-based datatypes
blanchet
parents:
56640
diff
changeset
|
17 |
structure BNF_LFP_Size : BNF_LFP_SIZE = |
56638 | 18 |
struct |
19 |
||
20 |
open BNF_Util |
|
56640
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
21 |
open BNF_Tactics |
56638 | 22 |
open BNF_Def |
56650
1f9ab71d43a5
no need to make 'size' generation an interpretation -- overkill
blanchet
parents:
56647
diff
changeset
|
23 |
open BNF_FP_Util |
56638 | 24 |
|
25 |
val size_N = "size_" |
|
26 |
||
56640
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
27 |
val rec_o_mapN = "rec_o_map" |
56638 | 28 |
val sizeN = "size" |
56640
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
29 |
val size_o_mapN = "size_o_map" |
56638 | 30 |
|
56651 | 31 |
val nitpicksimp_attrs = @{attributes [nitpick_simp]}; |
32 |
val simp_attrs = @{attributes [simp]}; |
|
33 |
val code_nitpicksimp_simp_attrs = Code.add_default_eqn_attrib :: nitpicksimp_attrs @ simp_attrs; |
|
34 |
||
35 |
structure Data = Generic_Data |
|
56638 | 36 |
( |
37 |
type T = (string * (thm list * thm list)) Symtab.table; |
|
38 |
val empty = Symtab.empty; |
|
39 |
val extend = I |
|
40 |
fun merge data = Symtab.merge (K true) data; |
|
41 |
); |
|
42 |
||
56642
15cd15f9b40c
allow registration of custom size functions for BNF-based datatypes
blanchet
parents:
56640
diff
changeset
|
43 |
fun register_size T_name size_name size_simps size_o_maps = |
56651 | 44 |
Context.proof_map (Data.map (Symtab.update (T_name, (size_name, (size_simps, size_o_maps))))); |
56643
41d3596d8a64
move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents:
56642
diff
changeset
|
45 |
|
56651 | 46 |
fun register_size_global T_name size_name size_simps size_o_maps = |
47 |
Context.theory_map (Data.map (Symtab.update (T_name, (size_name, (size_simps, size_o_maps))))); |
|
48 |
||
49 |
val lookup_size = Symtab.lookup o Data.get o Context.Proof; |
|
50 |
val lookup_size_global = Symtab.lookup o Data.get o Context.Theory; |
|
56642
15cd15f9b40c
allow registration of custom size functions for BNF-based datatypes
blanchet
parents:
56640
diff
changeset
|
51 |
|
56638 | 52 |
val zero_nat = @{const zero_class.zero (nat)}; |
53 |
||
54 |
fun mk_plus_nat (t1, t2) = Const (@{const_name Groups.plus}, |
|
55 |
HOLogic.natT --> HOLogic.natT --> HOLogic.natT) $ t1 $ t2; |
|
56 |
||
57 |
fun mk_to_natT T = T --> HOLogic.natT; |
|
58 |
||
59 |
fun mk_abs_zero_nat T = Term.absdummy T zero_nat; |
|
60 |
||
56640
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
61 |
fun pointfill ctxt th = unfold_thms ctxt [o_apply] (th RS fun_cong); |
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
62 |
|
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
63 |
fun mk_unabs_def_unused_0 n = |
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
64 |
funpow n (fn thm => thm RS @{thm fun_cong_unused_0} handle THM _ => thm RS fun_cong); |
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
65 |
|
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
66 |
val rec_o_map_simp_thms = |
56651 | 67 |
@{thms o_def id_def case_prod_app case_sum_map_sum case_prod_map_prod BNF_Comp.id_bnf_comp_def}; |
56638 | 68 |
|
56640
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
69 |
fun mk_rec_o_map_tac ctxt rec_def pre_map_defs abs_inverses ctor_rec_o_map = |
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
70 |
unfold_thms_tac ctxt [rec_def] THEN |
56651 | 71 |
HEADGOAL (rtac (ctor_rec_o_map RS trans)) THEN |
72 |
PRIMITIVE (Conv.fconv_rule Thm.eta_long_conversion) THEN |
|
73 |
HEADGOAL (asm_simp_tac (ss_only (pre_map_defs @ distinct Thm.eq_thm_prop abs_inverses @ |
|
74 |
rec_o_map_simp_thms) ctxt)); |
|
56638 | 75 |
|
56651 | 76 |
val size_o_map_simp_thms = @{thms prod_inj_map inj_on_id snd_comp_apfst[unfolded apfst_def]}; |
56640
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
77 |
|
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
78 |
fun mk_size_o_map_tac ctxt size_def rec_o_map inj_maps size_maps = |
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
79 |
unfold_thms_tac ctxt [size_def] THEN |
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
80 |
HEADGOAL (rtac (rec_o_map RS trans) THEN' |
56642
15cd15f9b40c
allow registration of custom size functions for BNF-based datatypes
blanchet
parents:
56640
diff
changeset
|
81 |
asm_simp_tac (ss_only (inj_maps @ size_maps @ size_o_map_simp_thms) ctxt)) THEN |
56645
a16d294f7e3f
prevent tactic failures by detecting and ignoring BNFs with no 'size' functions early
blanchet
parents:
56643
diff
changeset
|
82 |
IF_UNSOLVED (unfold_thms_tac ctxt @{thms o_def} THEN HEADGOAL (rtac refl)); |
56638 | 83 |
|
56651 | 84 |
fun generate_lfp_size (fp_sugars as ({T = Type (_, As), BT = Type (_, Bs), |
85 |
fp_res = {bnfs = fp_bnfs, xtor_co_rec_o_map_thms = ctor_rec_o_maps, ...}, nested_bnfs, |
|
86 |
nesting_bnfs, ...} : fp_sugar) :: _) lthy0 = |
|
87 |
let |
|
88 |
val data = Data.get (Context.Proof lthy0); |
|
89 |
||
90 |
val Ts = map #T fp_sugars |
|
91 |
val T_names = map (fst o dest_Type) Ts; |
|
92 |
val nn = length Ts; |
|
93 |
||
94 |
val B_ify = Term.typ_subst_atomic (As ~~ Bs); |
|
95 |
||
96 |
val recs = map #co_rec fp_sugars; |
|
97 |
val rec_thmss = map #co_rec_thms fp_sugars; |
|
98 |
val rec_Ts as rec_T1 :: _ = map fastype_of recs; |
|
99 |
val rec_arg_Ts = binder_fun_types rec_T1; |
|
100 |
val Cs = map body_type rec_Ts; |
|
101 |
val Cs_rho = map (rpair HOLogic.natT) Cs; |
|
102 |
val substCnatT = Term.subst_atomic_types Cs_rho; |
|
103 |
||
104 |
val f_Ts = map mk_to_natT As; |
|
105 |
val f_TsB = map mk_to_natT Bs; |
|
106 |
val num_As = length As; |
|
107 |
||
108 |
fun variant_names n pre = fst (Variable.variant_fixes (replicate n pre) lthy0); |
|
56640
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
109 |
|
56651 | 110 |
val f_names = variant_names num_As "f"; |
111 |
val fs = map2 (curry Free) f_names f_Ts; |
|
112 |
val fsB = map2 (curry Free) f_names f_TsB; |
|
113 |
val As_fs = As ~~ fs; |
|
114 |
||
56654 | 115 |
val size_bs = |
116 |
map ((fn base => Binding.qualify false base (Binding.name (prefix size_N base))) o |
|
117 |
Long_Name.base_name) T_names; |
|
56651 | 118 |
|
119 |
fun is_pair_C @{type_name prod} [_, T'] = member (op =) Cs T' |
|
120 |
| is_pair_C _ _ = false; |
|
56640
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
121 |
|
56651 | 122 |
fun mk_size_of_typ (T as TFree _) = |
123 |
pair (case AList.lookup (op =) As_fs T of |
|
124 |
SOME f => f |
|
125 |
| NONE => if member (op =) Cs T then Term.absdummy T (Bound 0) else mk_abs_zero_nat T) |
|
126 |
| mk_size_of_typ (T as Type (s, Ts)) = |
|
127 |
if is_pair_C s Ts then |
|
128 |
pair (snd_const T) |
|
56737 | 129 |
else if exists (exists_subtype_in (As @ Cs)) Ts then |
56651 | 130 |
(case Symtab.lookup data s of |
131 |
SOME (size_name, (_, size_o_maps as _ :: _)) => |
|
132 |
let |
|
133 |
val (args, size_o_mapss') = split_list (map (fn T => mk_size_of_typ T []) Ts); |
|
134 |
val size_const = Const (size_name, map fastype_of args ---> mk_to_natT T); |
|
135 |
in |
|
136 |
fold (union Thm.eq_thm) (size_o_maps :: size_o_mapss') |
|
137 |
#> pair (Term.list_comb (size_const, args)) |
|
138 |
end |
|
139 |
| _ => pair (mk_abs_zero_nat T)) |
|
140 |
else |
|
141 |
pair (mk_abs_zero_nat T); |
|
56638 | 142 |
|
56651 | 143 |
fun mk_size_of_arg t = |
144 |
mk_size_of_typ (fastype_of t) #>> (fn s => substCnatT (betapply (s, t))); |
|
145 |
||
146 |
fun mk_size_arg rec_arg_T size_o_maps = |
|
147 |
let |
|
148 |
val x_Ts = binder_types rec_arg_T; |
|
149 |
val m = length x_Ts; |
|
150 |
val x_names = variant_names m "x"; |
|
151 |
val xs = map2 (curry Free) x_names x_Ts; |
|
152 |
val (summands, size_o_maps') = |
|
153 |
fold_map mk_size_of_arg xs size_o_maps |
|
154 |
|>> remove (op =) zero_nat; |
|
155 |
val sum = |
|
156 |
if null summands then HOLogic.zero |
|
157 |
else foldl1 mk_plus_nat (summands @ [HOLogic.Suc_zero]); |
|
158 |
in |
|
159 |
(fold_rev Term.lambda (map substCnatT xs) sum, size_o_maps') |
|
160 |
end; |
|
161 |
||
162 |
fun mk_size_rhs recx size_o_maps = |
|
163 |
let val (args, size_o_maps') = fold_map mk_size_arg rec_arg_Ts size_o_maps in |
|
164 |
(fold_rev Term.lambda fs (Term.list_comb (substCnatT recx, args)), size_o_maps') |
|
165 |
end; |
|
166 |
||
167 |
val maybe_conceal_def_binding = Thm.def_binding |
|
168 |
#> Config.get lthy0 bnf_note_all = false ? Binding.conceal; |
|
169 |
||
170 |
val (size_rhss, nested_size_o_maps) = fold_map mk_size_rhs recs []; |
|
171 |
val size_Ts = map fastype_of size_rhss; |
|
56638 | 172 |
|
56651 | 173 |
val ((raw_size_consts, raw_size_defs), (lthy1', lthy1)) = lthy0 |
174 |
|> apfst split_list o fold_map2 (fn b => fn rhs => |
|
175 |
Local_Theory.define ((b, NoSyn), ((maybe_conceal_def_binding b, []), rhs)) #>> apsnd snd) |
|
176 |
size_bs size_rhss |
|
177 |
||> `Local_Theory.restore; |
|
178 |
||
179 |
val phi = Proof_Context.export_morphism lthy1 lthy1'; |
|
180 |
||
181 |
val size_defs = map (Morphism.thm phi) raw_size_defs; |
|
182 |
||
183 |
val size_consts0 = map (Morphism.term phi) raw_size_consts; |
|
184 |
val size_consts = map2 retype_const_or_free size_Ts size_consts0; |
|
185 |
val size_constsB = map (Term.map_types B_ify) size_consts; |
|
186 |
||
187 |
val zeros = map mk_abs_zero_nat As; |
|
56638 | 188 |
|
56651 | 189 |
val overloaded_size_rhss = map (fn c => Term.list_comb (c, zeros)) size_consts; |
190 |
val overloaded_size_Ts = map fastype_of overloaded_size_rhss; |
|
191 |
val overloaded_size_consts = map (curry Const @{const_name size}) overloaded_size_Ts; |
|
192 |
val overloaded_size_def_bs = |
|
193 |
map (maybe_conceal_def_binding o Binding.suffix_name "_overloaded") size_bs; |
|
56638 | 194 |
|
56651 | 195 |
fun define_overloaded_size def_b lhs0 rhs lthy = |
196 |
let |
|
197 |
val Free (c, _) = Syntax.check_term lthy lhs0; |
|
198 |
val (thm, lthy') = lthy |
|
199 |
|> Local_Theory.define ((Binding.name c, NoSyn), ((def_b, []), rhs)) |
|
200 |
|-> (fn (t, (_, thm)) => Spec_Rules.add Spec_Rules.Equational ([t], [thm]) #> pair thm); |
|
201 |
val ctxt_thy = Proof_Context.init_global (Proof_Context.theory_of lthy'); |
|
202 |
val thm' = singleton (Proof_Context.export lthy' ctxt_thy) thm; |
|
203 |
in (thm', lthy') end; |
|
56638 | 204 |
|
56651 | 205 |
val (overloaded_size_defs, lthy2) = lthy1 |
206 |
|> Local_Theory.background_theory_result |
|
207 |
(Class.instantiation (T_names, map dest_TFree As, [HOLogic.class_size]) |
|
208 |
#> fold_map3 define_overloaded_size overloaded_size_def_bs overloaded_size_consts |
|
209 |
overloaded_size_rhss |
|
210 |
##> Class.prove_instantiation_instance (K (Class.intro_classes_tac [])) |
|
211 |
##> Local_Theory.exit_global); |
|
212 |
||
213 |
val size_defs' = |
|
214 |
map (mk_unabs_def (num_As + 1) o (fn thm => thm RS meta_eq_to_obj_eq)) size_defs; |
|
215 |
val size_defs_unused_0 = |
|
216 |
map (mk_unabs_def_unused_0 (num_As + 1) o (fn thm => thm RS meta_eq_to_obj_eq)) size_defs; |
|
217 |
val overloaded_size_defs' = |
|
218 |
map (mk_unabs_def 1 o (fn thm => thm RS meta_eq_to_obj_eq)) overloaded_size_defs; |
|
219 |
||
56684
d8f32f55e463
more unfolding and more folding in size equations, to look more natural in the nested case
blanchet
parents:
56682
diff
changeset
|
220 |
val all_overloaded_size_defs = overloaded_size_defs @ |
d8f32f55e463
more unfolding and more folding in size equations, to look more natural in the nested case
blanchet
parents:
56682
diff
changeset
|
221 |
(Spec_Rules.retrieve lthy0 @{const size ('a)} |
d8f32f55e463
more unfolding and more folding in size equations, to look more natural in the nested case
blanchet
parents:
56682
diff
changeset
|
222 |
|> map_filter (try (fn (Equational, (_, [thm])) => thm))); |
d8f32f55e463
more unfolding and more folding in size equations, to look more natural in the nested case
blanchet
parents:
56682
diff
changeset
|
223 |
|
56651 | 224 |
val nested_size_maps = map (pointfill lthy2) nested_size_o_maps @ nested_size_o_maps; |
225 |
val all_inj_maps = map inj_map_of_bnf (fp_bnfs @ nested_bnfs @ nesting_bnfs); |
|
226 |
||
227 |
fun derive_size_simp size_def' simp0 = |
|
228 |
(trans OF [size_def', simp0]) |
|
56684
d8f32f55e463
more unfolding and more folding in size equations, to look more natural in the nested case
blanchet
parents:
56682
diff
changeset
|
229 |
|> Simplifier.asm_full_simplify (ss_only (@{thms inj_on_convol_ident id_def o_def snd_conv} @ |
56651 | 230 |
all_inj_maps @ nested_size_maps) lthy2) |
231 |
|> fold_thms lthy2 size_defs_unused_0; |
|
56684
d8f32f55e463
more unfolding and more folding in size equations, to look more natural in the nested case
blanchet
parents:
56682
diff
changeset
|
232 |
|
d8f32f55e463
more unfolding and more folding in size equations, to look more natural in the nested case
blanchet
parents:
56682
diff
changeset
|
233 |
fun derive_overloaded_size_simp overloaded_size_def' simp0 = |
d8f32f55e463
more unfolding and more folding in size equations, to look more natural in the nested case
blanchet
parents:
56682
diff
changeset
|
234 |
(trans OF [overloaded_size_def', simp0]) |
56651 | 235 |
|> unfold_thms lthy2 @{thms add_0_left add_0_right} |
56684
d8f32f55e463
more unfolding and more folding in size equations, to look more natural in the nested case
blanchet
parents:
56682
diff
changeset
|
236 |
|> fold_thms lthy2 all_overloaded_size_defs; |
56651 | 237 |
|
238 |
val size_simpss = map2 (map o derive_size_simp) size_defs' rec_thmss; |
|
239 |
val size_simps = flat size_simpss; |
|
240 |
val overloaded_size_simpss = |
|
241 |
map2 (map o derive_overloaded_size_simp) overloaded_size_defs' size_simpss; |
|
242 |
val size_thmss = map2 append size_simpss overloaded_size_simpss; |
|
243 |
||
244 |
val ABs = As ~~ Bs; |
|
245 |
val g_names = variant_names num_As "g"; |
|
246 |
val gs = map2 (curry Free) g_names (map (op -->) ABs); |
|
247 |
||
248 |
val liveness = map (op <>) ABs; |
|
249 |
val live_gs = AList.find (op =) (gs ~~ liveness) true; |
|
250 |
val live = length live_gs; |
|
251 |
||
252 |
val maps0 = map map_of_bnf fp_bnfs; |
|
253 |
||
254 |
val (rec_o_map_thmss, size_o_map_thmss) = |
|
255 |
if live = 0 then |
|
256 |
`I (replicate nn []) |
|
257 |
else |
|
258 |
let |
|
259 |
val pre_bnfs = map #pre_bnf fp_sugars; |
|
260 |
val pre_map_defs = map map_def_of_bnf pre_bnfs; |
|
261 |
val abs_inverses = map (#abs_inverse o #absT_info) fp_sugars; |
|
262 |
val rec_defs = map #co_rec_def fp_sugars; |
|
263 |
||
264 |
val gmaps = map (fn map0 => Term.list_comb (mk_map live As Bs map0, live_gs)) maps0; |
|
265 |
||
266 |
val num_rec_args = length rec_arg_Ts; |
|
267 |
val h_Ts = map B_ify rec_arg_Ts; |
|
268 |
val h_names = variant_names num_rec_args "h"; |
|
269 |
val hs = map2 (curry Free) h_names h_Ts; |
|
270 |
val hrecs = map (fn recx => Term.list_comb (Term.map_types B_ify recx, hs)) recs; |
|
56638 | 271 |
|
56651 | 272 |
val rec_o_map_lhss = map2 (curry HOLogic.mk_comp) hrecs gmaps; |
273 |
||
274 |
val ABgs = ABs ~~ gs; |
|
275 |
||
276 |
fun mk_rec_arg_arg (x as Free (_, T)) = |
|
277 |
let val U = B_ify T in |
|
278 |
if T = U then x else build_map lthy2 (the o AList.lookup (op =) ABgs) (T, U) $ x |
|
279 |
end; |
|
280 |
||
281 |
fun mk_rec_o_map_arg rec_arg_T h = |
|
282 |
let |
|
283 |
val x_Ts = binder_types rec_arg_T; |
|
284 |
val m = length x_Ts; |
|
285 |
val x_names = variant_names m "x"; |
|
286 |
val xs = map2 (curry Free) x_names x_Ts; |
|
287 |
val xs' = map mk_rec_arg_arg xs; |
|
288 |
in |
|
289 |
fold_rev Term.lambda xs (Term.list_comb (h, xs')) |
|
290 |
end; |
|
291 |
||
292 |
fun mk_rec_o_map_rhs recx = |
|
293 |
let val args = map2 mk_rec_o_map_arg rec_arg_Ts hs in |
|
294 |
Term.list_comb (recx, args) |
|
295 |
end; |
|
296 |
||
297 |
val rec_o_map_rhss = map mk_rec_o_map_rhs recs; |
|
56638 | 298 |
|
56651 | 299 |
val rec_o_map_goals = |
300 |
map2 (fold_rev (fold_rev Logic.all) [gs, hs] o HOLogic.mk_Trueprop oo |
|
301 |
curry HOLogic.mk_eq) rec_o_map_lhss rec_o_map_rhss; |
|
302 |
val rec_o_map_thms = |
|
303 |
map3 (fn goal => fn rec_def => fn ctor_rec_o_map => |
|
304 |
Goal.prove lthy2 [] [] goal (fn {context = ctxt, ...} => |
|
305 |
mk_rec_o_map_tac ctxt rec_def pre_map_defs abs_inverses ctor_rec_o_map) |
|
306 |
|> Thm.close_derivation) |
|
307 |
rec_o_map_goals rec_defs ctor_rec_o_maps; |
|
308 |
||
309 |
val size_o_map_conds = |
|
310 |
if exists (can Logic.dest_implies o Thm.prop_of) nested_size_o_maps then |
|
311 |
map (HOLogic.mk_Trueprop o mk_inj) live_gs |
|
312 |
else |
|
313 |
[]; |
|
314 |
||
315 |
val fsizes = map (fn size_constB => Term.list_comb (size_constB, fsB)) size_constsB; |
|
316 |
val size_o_map_lhss = map2 (curry HOLogic.mk_comp) fsizes gmaps; |
|
317 |
||
318 |
val fgs = map2 (fn fB => fn g as Free (_, Type (_, [A, B])) => |
|
319 |
if A = B then fB else HOLogic.mk_comp (fB, g)) fsB gs; |
|
320 |
val size_o_map_rhss = map (fn c => Term.list_comb (c, fgs)) size_consts; |
|
321 |
||
322 |
val size_o_map_goals = |
|
323 |
map2 (fold_rev (fold_rev Logic.all) [fsB, gs] o |
|
324 |
curry Logic.list_implies size_o_map_conds o HOLogic.mk_Trueprop oo |
|
325 |
curry HOLogic.mk_eq) size_o_map_lhss size_o_map_rhss; |
|
326 |
val size_o_map_thms = |
|
327 |
map3 (fn goal => fn size_def => fn rec_o_map => |
|
328 |
Goal.prove lthy2 [] [] goal (fn {context = ctxt, ...} => |
|
329 |
mk_size_o_map_tac ctxt size_def rec_o_map all_inj_maps nested_size_maps) |
|
330 |
|> Thm.close_derivation) |
|
331 |
size_o_map_goals size_defs rec_o_map_thms; |
|
56640
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
332 |
in |
56651 | 333 |
pairself (map single) (rec_o_map_thms, size_o_map_thms) |
56640
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
334 |
end; |
0a35354137a5
generate 'rec_o_map' and 'size_o_map' in size extension
blanchet
parents:
56639
diff
changeset
|
335 |
|
56651 | 336 |
val massage_multi_notes = |
337 |
maps (fn (thmN, thmss, attrs) => |
|
338 |
map2 (fn T_name => fn thms => |
|
339 |
((Binding.qualify true (Long_Name.base_name T_name) (Binding.name thmN), attrs), |
|
340 |
[(thms, [])])) |
|
341 |
T_names thmss) |
|
342 |
#> filter_out (null o fst o hd o snd); |
|
56638 | 343 |
|
56651 | 344 |
val notes = |
345 |
[(rec_o_mapN, rec_o_map_thmss, []), |
|
346 |
(sizeN, size_thmss, code_nitpicksimp_simp_attrs), |
|
347 |
(size_o_mapN, size_o_map_thmss, [])] |
|
348 |
|> massage_multi_notes; |
|
349 |
in |
|
350 |
lthy2 |
|
351 |
|> Local_Theory.notes notes |> snd |
|
352 |
|> Spec_Rules.add Spec_Rules.Equational (size_consts, size_simps) |
|
353 |
|> Local_Theory.declaration {syntax = false, pervasive = true} |
|
354 |
(fn phi => Data.map (fold2 (fn T_name => fn Const (size_name, _) => |
|
355 |
Symtab.update (T_name, (size_name, |
|
356 |
pairself (map (Morphism.thm phi)) (size_simps, flat size_o_map_thmss)))) |
|
357 |
T_names size_consts)) |
|
358 |
end; |
|
56638 | 359 |
|
360 |
end; |