1 (* Title: HOL/BNF/Tools/bnf_util.ML |
|
2 Author: Dmitriy Traytel, TU Muenchen |
|
3 Copyright 2012 |
|
4 |
|
5 Library for bounded natural functors. |
|
6 *) |
|
7 |
|
8 signature BNF_UTIL = |
|
9 sig |
|
10 include CTR_SUGAR_UTIL |
|
11 |
|
12 val map6: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g) -> |
|
13 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list |
|
14 val map7: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h) -> |
|
15 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list |
|
16 val map8: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i) -> |
|
17 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> 'i list |
|
18 val map9: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j) -> |
|
19 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> |
|
20 'i list -> 'j list |
|
21 val map10: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k) -> |
|
22 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> |
|
23 'i list -> 'j list -> 'k list |
|
24 val map11: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k -> 'l) -> |
|
25 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> |
|
26 'i list -> 'j list -> 'k list -> 'l list |
|
27 val map12: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k -> 'l -> 'm) -> |
|
28 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> |
|
29 'i list -> 'j list -> 'k list -> 'l list -> 'm list |
|
30 val map13: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k -> 'l -> 'm -> 'n) -> |
|
31 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> |
|
32 'i list -> 'j list -> 'k list -> 'l list -> 'm list -> 'n list |
|
33 val map14: |
|
34 ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k -> 'l -> 'm -> 'n -> 'o) -> |
|
35 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> |
|
36 'i list -> 'j list -> 'k list -> 'l list -> 'm list -> 'n list -> 'o list |
|
37 val fold_map4: ('a -> 'b -> 'c -> 'd -> 'e -> 'f * 'e) -> |
|
38 'a list -> 'b list -> 'c list -> 'd list -> 'e -> 'f list * 'e |
|
39 val fold_map5: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g * 'f) -> |
|
40 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f -> 'g list * 'f |
|
41 val fold_map6: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h * 'g) -> |
|
42 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g -> 'h list * 'g |
|
43 val fold_map7: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i * 'h) -> |
|
44 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h -> 'i list * 'h |
|
45 val fold_map8: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j * 'i) -> |
|
46 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> 'i -> |
|
47 'j list * 'i |
|
48 val fold_map9: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i -> 'j -> 'k * 'j) -> |
|
49 'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list -> 'g list -> 'h list -> |
|
50 'i list -> 'j -> 'k list * 'j |
|
51 val split_list4: ('a * 'b * 'c * 'd) list -> 'a list * 'b list * 'c list * 'd list |
|
52 val split_list5: ('a * 'b * 'c * 'd * 'e) list -> 'a list * 'b list * 'c list * 'd list * 'e list |
|
53 val find_indices: ('b * 'a -> bool) -> 'a list -> 'b list -> int list |
|
54 |
|
55 val mk_TFreess: int list -> Proof.context -> typ list list * Proof.context |
|
56 val mk_Freesss: string -> typ list list list -> Proof.context -> |
|
57 term list list list * Proof.context |
|
58 val mk_Freessss: string -> typ list list list list -> Proof.context -> |
|
59 term list list list list * Proof.context |
|
60 val nonzero_string_of_int: int -> string |
|
61 val retype_free: typ -> term -> term |
|
62 |
|
63 val binder_fun_types: typ -> typ list |
|
64 val body_fun_type: typ -> typ |
|
65 val num_binder_types: typ -> int |
|
66 val strip_fun_type: typ -> typ list * typ |
|
67 val strip_typeN: int -> typ -> typ list * typ |
|
68 |
|
69 val mk_pred2T: typ -> typ -> typ |
|
70 val mk_relT: typ * typ -> typ |
|
71 val dest_relT: typ -> typ * typ |
|
72 val dest_pred2T: typ -> typ * typ |
|
73 val mk_sumT: typ * typ -> typ |
|
74 |
|
75 val ctwo: term |
|
76 val fst_const: typ -> term |
|
77 val snd_const: typ -> term |
|
78 val Id_const: typ -> term |
|
79 |
|
80 val mk_Ball: term -> term -> term |
|
81 val mk_Bex: term -> term -> term |
|
82 val mk_Card_order: term -> term |
|
83 val mk_Field: term -> term |
|
84 val mk_Gr: term -> term -> term |
|
85 val mk_Grp: term -> term -> term |
|
86 val mk_UNION: term -> term -> term |
|
87 val mk_Union: typ -> term |
|
88 val mk_card_binop: string -> (typ * typ -> typ) -> term -> term -> term |
|
89 val mk_card_of: term -> term |
|
90 val mk_card_order: term -> term |
|
91 val mk_cexp: term -> term -> term |
|
92 val mk_cinfinite: term -> term |
|
93 val mk_collect: term list -> typ -> term |
|
94 val mk_converse: term -> term |
|
95 val mk_conversep: term -> term |
|
96 val mk_cprod: term -> term -> term |
|
97 val mk_csum: term -> term -> term |
|
98 val mk_dir_image: term -> term -> term |
|
99 val mk_fun_rel: term -> term -> term |
|
100 val mk_image: term -> term |
|
101 val mk_in: term list -> term list -> typ -> term |
|
102 val mk_leq: term -> term -> term |
|
103 val mk_ordLeq: term -> term -> term |
|
104 val mk_rel_comp: term * term -> term |
|
105 val mk_rel_compp: term * term -> term |
|
106 |
|
107 (*parameterized terms*) |
|
108 val mk_nthN: int -> term -> int -> term |
|
109 |
|
110 (*parameterized thms*) |
|
111 val mk_Un_upper: int -> int -> thm |
|
112 val mk_conjIN: int -> thm |
|
113 val mk_conjunctN: int -> int -> thm |
|
114 val conj_dests: int -> thm -> thm list |
|
115 val mk_nthI: int -> int -> thm |
|
116 val mk_nth_conv: int -> int -> thm |
|
117 val mk_ordLeq_csum: int -> int -> thm -> thm |
|
118 val mk_UnIN: int -> int -> thm |
|
119 |
|
120 val Pair_eqD: thm |
|
121 val Pair_eqI: thm |
|
122 val ctrans: thm |
|
123 val id_apply: thm |
|
124 val meta_mp: thm |
|
125 val meta_spec: thm |
|
126 val o_apply: thm |
|
127 val set_mp: thm |
|
128 val set_rev_mp: thm |
|
129 val subset_UNIV: thm |
|
130 val mk_sym: thm -> thm |
|
131 val mk_trans: thm -> thm -> thm |
|
132 |
|
133 val is_refl: thm -> bool |
|
134 val is_concl_refl: thm -> bool |
|
135 val no_refl: thm list -> thm list |
|
136 val no_reflexive: thm list -> thm list |
|
137 |
|
138 val fold_thms: Proof.context -> thm list -> thm -> thm |
|
139 |
|
140 val parse_binding_colon: binding parser |
|
141 val parse_opt_binding_colon: binding parser |
|
142 val parse_type_args_named_constrained: (binding option * (string * string option)) list parser |
|
143 val parse_map_rel_bindings: (binding * binding) parser |
|
144 |
|
145 val typedef: binding * (string * sort) list * mixfix -> term -> |
|
146 (binding * binding) option -> tactic -> local_theory -> (string * Typedef.info) * local_theory |
|
147 end; |
|
148 |
|
149 structure BNF_Util : BNF_UTIL = |
|
150 struct |
|
151 |
|
152 open Ctr_Sugar_Util |
|
153 |
|
154 (* Library proper *) |
|
155 |
|
156 fun map6 _ [] [] [] [] [] [] = [] |
|
157 | map6 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) = |
|
158 f x1 x2 x3 x4 x5 x6 :: map6 f x1s x2s x3s x4s x5s x6s |
|
159 | map6 _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
160 |
|
161 fun map7 _ [] [] [] [] [] [] [] = [] |
|
162 | map7 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) = |
|
163 f x1 x2 x3 x4 x5 x6 x7 :: map7 f x1s x2s x3s x4s x5s x6s x7s |
|
164 | map7 _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
165 |
|
166 fun map8 _ [] [] [] [] [] [] [] [] = [] |
|
167 | map8 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) = |
|
168 f x1 x2 x3 x4 x5 x6 x7 x8 :: map8 f x1s x2s x3s x4s x5s x6s x7s x8s |
|
169 | map8 _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
170 |
|
171 fun map9 _ [] [] [] [] [] [] [] [] [] = [] |
|
172 | map9 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) |
|
173 (x9::x9s) = |
|
174 f x1 x2 x3 x4 x5 x6 x7 x8 x9 :: map9 f x1s x2s x3s x4s x5s x6s x7s x8s x9s |
|
175 | map9 _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
176 |
|
177 fun map10 _ [] [] [] [] [] [] [] [] [] [] = [] |
|
178 | map10 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) |
|
179 (x9::x9s) (x10::x10s) = |
|
180 f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 :: map10 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s |
|
181 | map10 _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
182 |
|
183 fun map11 _ [] [] [] [] [] [] [] [] [] [] [] = [] |
|
184 | map11 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) |
|
185 (x9::x9s) (x10::x10s) (x11::x11s) = |
|
186 f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 :: map11 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s x11s |
|
187 | map11 _ _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
188 |
|
189 fun map12 _ [] [] [] [] [] [] [] [] [] [] [] [] = [] |
|
190 | map12 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) |
|
191 (x9::x9s) (x10::x10s) (x11::x11s) (x12::x12s) = |
|
192 f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 :: |
|
193 map12 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s x11s x12s |
|
194 | map12 _ _ _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
195 |
|
196 fun map13 _ [] [] [] [] [] [] [] [] [] [] [] [] [] = [] |
|
197 | map13 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) |
|
198 (x9::x9s) (x10::x10s) (x11::x11s) (x12::x12s) (x13::x13s) = |
|
199 f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 :: |
|
200 map13 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s x11s x12s x13s |
|
201 | map13 _ _ _ _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
202 |
|
203 fun map14 _ [] [] [] [] [] [] [] [] [] [] [] [] [] [] = [] |
|
204 | map14 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) |
|
205 (x9::x9s) (x10::x10s) (x11::x11s) (x12::x12s) (x13::x13s) (x14::x14s) = |
|
206 f x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 :: |
|
207 map14 f x1s x2s x3s x4s x5s x6s x7s x8s x9s x10s x11s x12s x13s x14s |
|
208 | map14 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
209 |
|
210 fun fold_map4 _ [] [] [] [] acc = ([], acc) |
|
211 | fold_map4 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) acc = |
|
212 let |
|
213 val (x, acc') = f x1 x2 x3 x4 acc; |
|
214 val (xs, acc'') = fold_map4 f x1s x2s x3s x4s acc'; |
|
215 in (x :: xs, acc'') end |
|
216 | fold_map4 _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
217 |
|
218 fun fold_map5 _ [] [] [] [] [] acc = ([], acc) |
|
219 | fold_map5 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) acc = |
|
220 let |
|
221 val (x, acc') = f x1 x2 x3 x4 x5 acc; |
|
222 val (xs, acc'') = fold_map5 f x1s x2s x3s x4s x5s acc'; |
|
223 in (x :: xs, acc'') end |
|
224 | fold_map5 _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
225 |
|
226 fun fold_map6 _ [] [] [] [] [] [] acc = ([], acc) |
|
227 | fold_map6 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) acc = |
|
228 let |
|
229 val (x, acc') = f x1 x2 x3 x4 x5 x6 acc; |
|
230 val (xs, acc'') = fold_map6 f x1s x2s x3s x4s x5s x6s acc'; |
|
231 in (x :: xs, acc'') end |
|
232 | fold_map6 _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
233 |
|
234 fun fold_map7 _ [] [] [] [] [] [] [] acc = ([], acc) |
|
235 | fold_map7 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) acc = |
|
236 let |
|
237 val (x, acc') = f x1 x2 x3 x4 x5 x6 x7 acc; |
|
238 val (xs, acc'') = fold_map7 f x1s x2s x3s x4s x5s x6s x7s acc'; |
|
239 in (x :: xs, acc'') end |
|
240 | fold_map7 _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
241 |
|
242 fun fold_map8 _ [] [] [] [] [] [] [] [] acc = ([], acc) |
|
243 | fold_map8 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) |
|
244 acc = |
|
245 let |
|
246 val (x, acc') = f x1 x2 x3 x4 x5 x6 x7 x8 acc; |
|
247 val (xs, acc'') = fold_map8 f x1s x2s x3s x4s x5s x6s x7s x8s acc'; |
|
248 in (x :: xs, acc'') end |
|
249 | fold_map8 _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
250 |
|
251 fun fold_map9 _ [] [] [] [] [] [] [] [] [] acc = ([], acc) |
|
252 | fold_map9 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) (x6::x6s) (x7::x7s) (x8::x8s) |
|
253 (x9::x9s) acc = |
|
254 let |
|
255 val (x, acc') = f x1 x2 x3 x4 x5 x6 x7 x8 x9 acc; |
|
256 val (xs, acc'') = fold_map9 f x1s x2s x3s x4s x5s x6s x7s x8s x9s acc'; |
|
257 in (x :: xs, acc'') end |
|
258 | fold_map9 _ _ _ _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
|
259 |
|
260 fun split_list4 [] = ([], [], [], []) |
|
261 | split_list4 ((x1, x2, x3, x4) :: xs) = |
|
262 let val (xs1, xs2, xs3, xs4) = split_list4 xs; |
|
263 in (x1 :: xs1, x2 :: xs2, x3 :: xs3, x4 :: xs4) end; |
|
264 |
|
265 fun split_list5 [] = ([], [], [], [], []) |
|
266 | split_list5 ((x1, x2, x3, x4, x5) :: xs) = |
|
267 let val (xs1, xs2, xs3, xs4, xs5) = split_list5 xs; |
|
268 in (x1 :: xs1, x2 :: xs2, x3 :: xs3, x4 :: xs4, x5 :: xs5) end; |
|
269 |
|
270 val parse_binding_colon = parse_binding --| @{keyword ":"}; |
|
271 val parse_opt_binding_colon = Scan.optional parse_binding_colon Binding.empty; |
|
272 |
|
273 val parse_type_arg_constrained = |
|
274 Parse.type_ident -- Scan.option (@{keyword "::"} |-- Parse.!!! Parse.sort); |
|
275 |
|
276 val parse_type_arg_named_constrained = |
|
277 (Parse.minus --| @{keyword ":"} >> K NONE || parse_opt_binding_colon >> SOME) -- |
|
278 parse_type_arg_constrained; |
|
279 |
|
280 val parse_type_args_named_constrained = |
|
281 parse_type_arg_constrained >> (single o pair (SOME Binding.empty)) || |
|
282 @{keyword "("} |-- Parse.!!! (Parse.list1 parse_type_arg_named_constrained --| @{keyword ")"}) || |
|
283 Scan.succeed []; |
|
284 |
|
285 val parse_map_rel_binding = Parse.short_ident --| @{keyword ":"} -- parse_binding; |
|
286 |
|
287 val no_map_rel = (Binding.empty, Binding.empty); |
|
288 |
|
289 fun extract_map_rel ("map", b) = apfst (K b) |
|
290 | extract_map_rel ("rel", b) = apsnd (K b) |
|
291 | extract_map_rel (s, _) = error ("Unknown label " ^ quote s ^ " (expected \"map\" or \"rel\")"); |
|
292 |
|
293 val parse_map_rel_bindings = |
|
294 @{keyword "("} |-- Scan.repeat parse_map_rel_binding --| @{keyword ")"} |
|
295 >> (fn ps => fold extract_map_rel ps no_map_rel) || |
|
296 Scan.succeed no_map_rel; |
|
297 |
|
298 |
|
299 (*TODO: is this really different from Typedef.add_typedef_global?*) |
|
300 fun typedef (b, Ts, mx) set opt_morphs tac lthy = |
|
301 let |
|
302 (*Work around loss of qualification in "typedef" axioms by replicating it in the name*) |
|
303 val b' = fold_rev Binding.prefix_name (map (suffix "_" o fst) (#2 (Binding.dest b))) b; |
|
304 val ((name, info), (lthy, lthy_old)) = |
|
305 lthy |
|
306 |> Typedef.add_typedef (b', Ts, mx) set opt_morphs tac |
|
307 ||> `Local_Theory.restore; |
|
308 val phi = Proof_Context.export_morphism lthy_old lthy; |
|
309 in |
|
310 ((name, Typedef.transform_info phi info), lthy) |
|
311 end; |
|
312 |
|
313 |
|
314 |
|
315 (* Term construction *) |
|
316 |
|
317 (** Fresh variables **) |
|
318 |
|
319 fun nonzero_string_of_int 0 = "" |
|
320 | nonzero_string_of_int n = string_of_int n; |
|
321 |
|
322 val mk_TFreess = fold_map mk_TFrees; |
|
323 |
|
324 fun mk_Freesss x Tsss = fold_map2 mk_Freess (mk_names (length Tsss) x) Tsss; |
|
325 fun mk_Freessss x Tssss = fold_map2 mk_Freesss (mk_names (length Tssss) x) Tssss; |
|
326 |
|
327 fun retype_free T (Free (s, _)) = Free (s, T) |
|
328 | retype_free _ t = raise TERM ("retype_free", [t]); |
|
329 |
|
330 |
|
331 (** Types **) |
|
332 |
|
333 (*stolen from ~~/src/HOL/Tools/Nitpick/nitpick_hol.ML*) |
|
334 fun num_binder_types (Type (@{type_name fun}, [_, T2])) = |
|
335 1 + num_binder_types T2 |
|
336 | num_binder_types _ = 0 |
|
337 |
|
338 (*maps [T1,...,Tn]--->T to ([T1,T2,...,Tn], T)*) |
|
339 fun strip_typeN 0 T = ([], T) |
|
340 | strip_typeN n (Type (@{type_name fun}, [T, T'])) = strip_typeN (n - 1) T' |>> cons T |
|
341 | strip_typeN _ T = raise TYPE ("strip_typeN", [T], []); |
|
342 |
|
343 (*maps [T1,...,Tn]--->T-->U to ([T1,T2,...,Tn], T-->U), where U is not a function type*) |
|
344 fun strip_fun_type T = strip_typeN (num_binder_types T - 1) T; |
|
345 |
|
346 val binder_fun_types = fst o strip_fun_type; |
|
347 val body_fun_type = snd o strip_fun_type; |
|
348 |
|
349 fun mk_pred2T T U = mk_predT [T, U]; |
|
350 val mk_relT = HOLogic.mk_setT o HOLogic.mk_prodT; |
|
351 val dest_relT = HOLogic.dest_prodT o HOLogic.dest_setT; |
|
352 val dest_pred2T = apsnd Term.domain_type o Term.dest_funT; |
|
353 fun mk_sumT (LT, RT) = Type (@{type_name Sum_Type.sum}, [LT, RT]); |
|
354 |
|
355 |
|
356 (** Constants **) |
|
357 |
|
358 fun fst_const T = Const (@{const_name fst}, T --> fst (HOLogic.dest_prodT T)); |
|
359 fun snd_const T = Const (@{const_name snd}, T --> snd (HOLogic.dest_prodT T)); |
|
360 fun Id_const T = Const (@{const_name Id}, mk_relT (T, T)); |
|
361 |
|
362 |
|
363 (** Operators **) |
|
364 |
|
365 fun mk_converse R = |
|
366 let |
|
367 val RT = dest_relT (fastype_of R); |
|
368 val RST = mk_relT (snd RT, fst RT); |
|
369 in Const (@{const_name converse}, fastype_of R --> RST) $ R end; |
|
370 |
|
371 fun mk_rel_comp (R, S) = |
|
372 let |
|
373 val RT = fastype_of R; |
|
374 val ST = fastype_of S; |
|
375 val RST = mk_relT (fst (dest_relT RT), snd (dest_relT ST)); |
|
376 in Const (@{const_name relcomp}, RT --> ST --> RST) $ R $ S end; |
|
377 |
|
378 fun mk_Gr A f = |
|
379 let val ((AT, BT), FT) = `dest_funT (fastype_of f); |
|
380 in Const (@{const_name Gr}, HOLogic.mk_setT AT --> FT --> mk_relT (AT, BT)) $ A $ f end; |
|
381 |
|
382 fun mk_conversep R = |
|
383 let |
|
384 val RT = dest_pred2T (fastype_of R); |
|
385 val RST = mk_pred2T (snd RT) (fst RT); |
|
386 in Const (@{const_name conversep}, fastype_of R --> RST) $ R end; |
|
387 |
|
388 fun mk_rel_compp (R, S) = |
|
389 let |
|
390 val RT = fastype_of R; |
|
391 val ST = fastype_of S; |
|
392 val RST = mk_pred2T (fst (dest_pred2T RT)) (snd (dest_pred2T ST)); |
|
393 in Const (@{const_name relcompp}, RT --> ST --> RST) $ R $ S end; |
|
394 |
|
395 fun mk_Grp A f = |
|
396 let val ((AT, BT), FT) = `dest_funT (fastype_of f); |
|
397 in Const (@{const_name Grp}, HOLogic.mk_setT AT --> FT --> mk_pred2T AT BT) $ A $ f end; |
|
398 |
|
399 fun mk_image f = |
|
400 let val (T, U) = dest_funT (fastype_of f); |
|
401 in Const (@{const_name image}, |
|
402 (T --> U) --> (HOLogic.mk_setT T) --> (HOLogic.mk_setT U)) $ f end; |
|
403 |
|
404 fun mk_Ball X f = |
|
405 Const (@{const_name Ball}, fastype_of X --> fastype_of f --> HOLogic.boolT) $ X $ f; |
|
406 |
|
407 fun mk_Bex X f = |
|
408 Const (@{const_name Bex}, fastype_of X --> fastype_of f --> HOLogic.boolT) $ X $ f; |
|
409 |
|
410 fun mk_UNION X f = |
|
411 let val (T, U) = dest_funT (fastype_of f); |
|
412 in Const (@{const_name SUPR}, fastype_of X --> (T --> U) --> U) $ X $ f end; |
|
413 |
|
414 fun mk_Union T = |
|
415 Const (@{const_name Sup}, HOLogic.mk_setT (HOLogic.mk_setT T) --> HOLogic.mk_setT T); |
|
416 |
|
417 fun mk_Field r = |
|
418 let val T = fst (dest_relT (fastype_of r)); |
|
419 in Const (@{const_name Field}, mk_relT (T, T) --> HOLogic.mk_setT T) $ r end; |
|
420 |
|
421 fun mk_card_order bd = |
|
422 let |
|
423 val T = fastype_of bd; |
|
424 val AT = fst (dest_relT T); |
|
425 in |
|
426 Const (@{const_name card_order_on}, HOLogic.mk_setT AT --> T --> HOLogic.boolT) $ |
|
427 (HOLogic.mk_UNIV AT) $ bd |
|
428 end; |
|
429 |
|
430 fun mk_Card_order bd = |
|
431 let |
|
432 val T = fastype_of bd; |
|
433 val AT = fst (dest_relT T); |
|
434 in |
|
435 Const (@{const_name card_order_on}, HOLogic.mk_setT AT --> T --> HOLogic.boolT) $ |
|
436 mk_Field bd $ bd |
|
437 end; |
|
438 |
|
439 fun mk_cinfinite bd = |
|
440 Const (@{const_name cinfinite}, fastype_of bd --> HOLogic.boolT) $ bd; |
|
441 |
|
442 fun mk_ordLeq t1 t2 = |
|
443 HOLogic.mk_mem (HOLogic.mk_prod (t1, t2), |
|
444 Const (@{const_name ordLeq}, mk_relT (fastype_of t1, fastype_of t2))); |
|
445 |
|
446 fun mk_card_of A = |
|
447 let |
|
448 val AT = fastype_of A; |
|
449 val T = HOLogic.dest_setT AT; |
|
450 in |
|
451 Const (@{const_name card_of}, AT --> mk_relT (T, T)) $ A |
|
452 end; |
|
453 |
|
454 fun mk_dir_image r f = |
|
455 let val (T, U) = dest_funT (fastype_of f); |
|
456 in Const (@{const_name dir_image}, mk_relT (T, T) --> (T --> U) --> mk_relT (U, U)) $ r $ f end; |
|
457 |
|
458 fun mk_fun_rel R S = |
|
459 let |
|
460 val ((RA, RB), RT) = `dest_pred2T (fastype_of R); |
|
461 val ((SA, SB), ST) = `dest_pred2T (fastype_of S); |
|
462 in Const (@{const_name fun_rel}, RT --> ST --> mk_pred2T (RA --> SA) (RB --> SB)) $ R $ S end; |
|
463 |
|
464 (*FIXME: "x"?*) |
|
465 (*(nth sets i) must be of type "T --> 'ai set"*) |
|
466 fun mk_in As sets T = |
|
467 let |
|
468 fun in_single set A = |
|
469 let val AT = fastype_of A; |
|
470 in Const (@{const_name less_eq}, |
|
471 AT --> AT --> HOLogic.boolT) $ (set $ Free ("x", T)) $ A end; |
|
472 in |
|
473 if length sets > 0 |
|
474 then HOLogic.mk_Collect ("x", T, foldr1 (HOLogic.mk_conj) (map2 in_single sets As)) |
|
475 else HOLogic.mk_UNIV T |
|
476 end; |
|
477 |
|
478 fun mk_leq t1 t2 = |
|
479 Const (@{const_name less_eq}, (fastype_of t1) --> (fastype_of t2) --> HOLogic.boolT) $ t1 $ t2; |
|
480 |
|
481 fun mk_card_binop binop typop t1 t2 = |
|
482 let |
|
483 val (T1, relT1) = `(fst o dest_relT) (fastype_of t1); |
|
484 val (T2, relT2) = `(fst o dest_relT) (fastype_of t2); |
|
485 in |
|
486 Const (binop, relT1 --> relT2 --> mk_relT (typop (T1, T2), typop (T1, T2))) $ t1 $ t2 |
|
487 end; |
|
488 |
|
489 val mk_csum = mk_card_binop @{const_name csum} mk_sumT; |
|
490 val mk_cprod = mk_card_binop @{const_name cprod} HOLogic.mk_prodT; |
|
491 val mk_cexp = mk_card_binop @{const_name cexp} (op --> o swap); |
|
492 val ctwo = @{term ctwo}; |
|
493 |
|
494 fun mk_collect xs defT = |
|
495 let val T = (case xs of [] => defT | (x::_) => fastype_of x); |
|
496 in Const (@{const_name collect}, HOLogic.mk_setT T --> T) $ (HOLogic.mk_set T xs) end; |
|
497 |
|
498 fun find_indices eq xs ys = map_filter I |
|
499 (map_index (fn (i, y) => if member eq xs y then SOME i else NONE) ys); |
|
500 |
|
501 fun mk_trans thm1 thm2 = trans OF [thm1, thm2]; |
|
502 fun mk_sym thm = thm RS sym; |
|
503 |
|
504 (*TODO: antiquote heavily used theorems once*) |
|
505 val Pair_eqD = @{thm iffD1[OF Pair_eq]}; |
|
506 val Pair_eqI = @{thm iffD2[OF Pair_eq]}; |
|
507 val ctrans = @{thm ordLeq_transitive}; |
|
508 val id_apply = @{thm id_apply}; |
|
509 val meta_mp = @{thm meta_mp}; |
|
510 val meta_spec = @{thm meta_spec}; |
|
511 val o_apply = @{thm o_apply}; |
|
512 val set_mp = @{thm set_mp}; |
|
513 val set_rev_mp = @{thm set_rev_mp}; |
|
514 val subset_UNIV = @{thm subset_UNIV}; |
|
515 |
|
516 fun mk_nthN 1 t 1 = t |
|
517 | mk_nthN _ t 1 = HOLogic.mk_fst t |
|
518 | mk_nthN 2 t 2 = HOLogic.mk_snd t |
|
519 | mk_nthN n t m = mk_nthN (n - 1) (HOLogic.mk_snd t) (m - 1); |
|
520 |
|
521 fun mk_nth_conv n m = |
|
522 let |
|
523 fun thm b = if b then @{thm fstI} else @{thm sndI} |
|
524 fun mk_nth_conv _ 1 1 = refl |
|
525 | mk_nth_conv _ _ 1 = @{thm fst_conv} |
|
526 | mk_nth_conv _ 2 2 = @{thm snd_conv} |
|
527 | mk_nth_conv b _ 2 = @{thm snd_conv} RS thm b |
|
528 | mk_nth_conv b n m = mk_nth_conv false (n - 1) (m - 1) RS thm b; |
|
529 in mk_nth_conv (not (m = n)) n m end; |
|
530 |
|
531 fun mk_nthI 1 1 = @{thm TrueE[OF TrueI]} |
|
532 | mk_nthI n m = fold (curry op RS) (replicate (m - 1) @{thm sndI}) |
|
533 (if m = n then @{thm TrueE[OF TrueI]} else @{thm fstI}); |
|
534 |
|
535 fun mk_conjunctN 1 1 = @{thm TrueE[OF TrueI]} |
|
536 | mk_conjunctN _ 1 = conjunct1 |
|
537 | mk_conjunctN 2 2 = conjunct2 |
|
538 | mk_conjunctN n m = conjunct2 RS (mk_conjunctN (n - 1) (m - 1)); |
|
539 |
|
540 fun conj_dests n thm = map (fn k => thm RS mk_conjunctN n k) (1 upto n); |
|
541 |
|
542 fun mk_conjIN 1 = @{thm TrueE[OF TrueI]} |
|
543 | mk_conjIN n = mk_conjIN (n - 1) RSN (2, conjI); |
|
544 |
|
545 fun mk_ordLeq_csum 1 1 thm = thm |
|
546 | mk_ordLeq_csum _ 1 thm = @{thm ordLeq_transitive} OF [thm, @{thm ordLeq_csum1}] |
|
547 | mk_ordLeq_csum 2 2 thm = @{thm ordLeq_transitive} OF [thm, @{thm ordLeq_csum2}] |
|
548 | mk_ordLeq_csum n m thm = @{thm ordLeq_transitive} OF |
|
549 [mk_ordLeq_csum (n - 1) (m - 1) thm, @{thm ordLeq_csum2[OF Card_order_csum]}]; |
|
550 |
|
551 local |
|
552 fun mk_Un_upper' 0 = subset_refl |
|
553 | mk_Un_upper' 1 = @{thm Un_upper1} |
|
554 | mk_Un_upper' k = Library.foldr (op RS o swap) |
|
555 (replicate (k - 1) @{thm subset_trans[OF Un_upper1]}, @{thm Un_upper1}); |
|
556 in |
|
557 fun mk_Un_upper 1 1 = subset_refl |
|
558 | mk_Un_upper n 1 = mk_Un_upper' (n - 2) RS @{thm subset_trans[OF Un_upper1]} |
|
559 | mk_Un_upper n m = mk_Un_upper' (n - m) RS @{thm subset_trans[OF Un_upper2]}; |
|
560 end; |
|
561 |
|
562 local |
|
563 fun mk_UnIN' 0 = @{thm UnI2} |
|
564 | mk_UnIN' m = mk_UnIN' (m - 1) RS @{thm UnI1}; |
|
565 in |
|
566 fun mk_UnIN 1 1 = @{thm TrueE[OF TrueI]} |
|
567 | mk_UnIN n 1 = Library.foldr1 (op RS o swap) (replicate (n - 1) @{thm UnI1}) |
|
568 | mk_UnIN n m = mk_UnIN' (n - m) |
|
569 end; |
|
570 |
|
571 fun is_refl_prop t = |
|
572 op aconv (HOLogic.dest_eq (HOLogic.dest_Trueprop t)) |
|
573 handle TERM _ => false; |
|
574 |
|
575 val is_refl = is_refl_prop o Thm.prop_of; |
|
576 val is_concl_refl = is_refl_prop o Logic.strip_imp_concl o Thm.prop_of; |
|
577 |
|
578 val no_refl = filter_out is_refl; |
|
579 val no_reflexive = filter_out Thm.is_reflexive; |
|
580 |
|
581 fun fold_thms ctxt thms = Local_Defs.fold ctxt (distinct Thm.eq_thm_prop thms); |
|
582 |
|
583 end; |
|