60918
|
1 |
(* Title: HOL/Tools/BNF/bnf_lift.ML
|
|
2 |
Author: Julian Biendarra, TU Muenchen
|
|
3 |
Author: Dmitriy Traytel, ETH Zurich
|
|
4 |
Copyright 2015
|
|
5 |
|
|
6 |
Lifting of BNFs through typedefs.
|
|
7 |
*)
|
|
8 |
|
|
9 |
signature BNF_LIFT = sig
|
|
10 |
datatype lift_bnf_option = Plugins_Option of Proof.context -> Plugin_Name.filter | No_Warn_Wits
|
|
11 |
|
|
12 |
val copy_bnf:
|
|
13 |
(((lift_bnf_option list * (binding option * (string * sort option)) list) *
|
|
14 |
string) * thm option) * (binding * binding) ->
|
|
15 |
local_theory -> local_theory
|
|
16 |
val copy_bnf_cmd:
|
|
17 |
(((lift_bnf_option list * (binding option * (string * string option)) list) *
|
|
18 |
string) * (Facts.ref * Token.src list) option) * (binding * binding) ->
|
|
19 |
local_theory -> local_theory
|
|
20 |
val lift_bnf:
|
|
21 |
(((lift_bnf_option list * (binding option * (string * sort option)) list) *
|
|
22 |
string) * thm option) * (binding * binding) ->
|
|
23 |
({context: Proof.context, prems: thm list} -> tactic) list ->
|
|
24 |
local_theory -> local_theory
|
|
25 |
val lift_bnf_cmd:
|
|
26 |
((((lift_bnf_option list * (binding option * (string * string option)) list) *
|
|
27 |
string) * string list) * (Facts.ref * Token.src list) option) * (binding * binding) ->
|
|
28 |
local_theory -> Proof.state
|
|
29 |
end
|
|
30 |
|
|
31 |
structure BNF_Lift : BNF_LIFT = struct
|
|
32 |
|
|
33 |
open Ctr_Sugar_Tactics
|
|
34 |
open BNF_Util
|
|
35 |
open BNF_Comp
|
|
36 |
open BNF_Def
|
|
37 |
|
|
38 |
datatype lift_bnf_option = Plugins_Option of Proof.context -> Plugin_Name.filter | No_Warn_Wits
|
|
39 |
|
|
40 |
fun typedef_bnf thm wits specs map_b rel_b opts lthy =
|
|
41 |
let
|
|
42 |
val plugins = get_first (fn Plugins_Option f => SOME (f lthy) | _ => NONE) (rev opts)
|
|
43 |
|> the_default Plugin_Name.default_filter;
|
|
44 |
val no_warn_wits = exists (can (fn Sequential_Option => ())) opts;
|
|
45 |
|
|
46 |
(* extract Rep Abs F RepT AbsT *)
|
|
47 |
val (_, [Rep_G, Abs_G, F]) = Thm.prop_of thm
|
|
48 |
|> HOLogic.dest_Trueprop
|
|
49 |
|> Term.strip_comb;
|
|
50 |
val typ_Abs_G = fastype_of Abs_G |> dest_funT;
|
|
51 |
val RepT = fst typ_Abs_G; (* F *)
|
|
52 |
val AbsT = snd typ_Abs_G; (* G *)
|
|
53 |
val AbsT_name = fst (dest_Type AbsT);
|
|
54 |
val tvs = AbsT |> dest_Type |> snd |> map (fst o dest_TVar);
|
|
55 |
val alpha0s = map (TFree o snd) specs;
|
|
56 |
|
|
57 |
(* instantiate the new type variables newtvs to oldtvs *)
|
|
58 |
val subst = subst_TVars (tvs ~~ alpha0s);
|
|
59 |
val typ_subst = typ_subst_TVars (tvs ~~ alpha0s);
|
|
60 |
|
|
61 |
val Rep_G = subst Rep_G;
|
|
62 |
val Abs_G = subst Abs_G;
|
|
63 |
val F = subst F;
|
|
64 |
val RepT = typ_subst RepT;
|
|
65 |
val AbsT = typ_subst AbsT;
|
|
66 |
|
|
67 |
fun flatten_tyargs Ass = map dest_TFree alpha0s |>
|
|
68 |
filter (fn T => exists (fn Ts => member (op =) Ts T) Ass);
|
|
69 |
|
|
70 |
val Ds0 = filter (is_none o fst) specs |> map snd;
|
|
71 |
|
|
72 |
(* get the bnf for RepT *)
|
|
73 |
val ((bnf, (deads, alphas)),((_, unfolds), lthy)) =
|
|
74 |
bnf_of_typ Dont_Inline (Binding.qualify true AbsT_name) flatten_tyargs []
|
|
75 |
Ds0 RepT ((empty_comp_cache, empty_unfolds), lthy);
|
|
76 |
|
|
77 |
val set_bs = map (fn T => find_index (fn U => T = U) alpha0s) alphas
|
|
78 |
|> map (the_default Binding.empty o fst o nth specs);
|
|
79 |
|
|
80 |
val _ = case alphas of [] => error "No live variables." | alphas => alphas;
|
|
81 |
|
|
82 |
val defs = #map_unfolds unfolds @ flat (#set_unfoldss unfolds) @ #rel_unfolds unfolds;
|
|
83 |
|
|
84 |
(* number of live variables *)
|
|
85 |
val lives = length alphas;
|
|
86 |
|
|
87 |
(* state the three required properties *)
|
|
88 |
val sorts = map Type.sort_of_atyp alphas;
|
|
89 |
val names_lthy = fold Variable.declare_typ (alphas @ deads) lthy;
|
|
90 |
val (alphas', names_lthy) = mk_TFrees' sorts names_lthy;
|
|
91 |
val (betas, names_lthy) = mk_TFrees' sorts names_lthy;
|
|
92 |
|
|
93 |
val map_F = mk_map_of_bnf deads alphas betas bnf;
|
|
94 |
|
|
95 |
val (typ_fs, typ_aF) = fastype_of map_F |> strip_typeN lives ||> domain_type;
|
|
96 |
val typ_pairs = map HOLogic.mk_prodT (alphas ~~ alphas');
|
|
97 |
val typ_subst_pair = typ_subst_atomic (alphas ~~ typ_pairs);
|
|
98 |
val typ_pair = typ_subst_pair RepT;
|
|
99 |
|
|
100 |
val subst_b = subst_atomic_types (alphas ~~ betas);
|
|
101 |
val subst_a' = subst_atomic_types (alphas ~~ alphas');
|
|
102 |
val subst_pair = subst_atomic_types (alphas ~~ typ_pairs);
|
|
103 |
val aF_set = F;
|
|
104 |
val bF_set = subst_b F;
|
|
105 |
val aF_set' = subst_a' F;
|
|
106 |
val pairF_set = subst_pair F;
|
|
107 |
val map_F_fst = mk_map_of_bnf deads typ_pairs alphas bnf;
|
|
108 |
val map_F_snd = mk_map_of_bnf deads typ_pairs alphas' bnf;
|
|
109 |
val wits_F = mk_wits_of_bnf
|
|
110 |
(replicate (nwits_of_bnf bnf) deads)
|
|
111 |
(replicate (nwits_of_bnf bnf) alphas) bnf;
|
|
112 |
|
|
113 |
(* val map_closed_F = @{term "\<And>f x. x \<in> F \<Longrightarrow> map_F f x \<in> F"}; *)
|
|
114 |
val (var_fs, names_lthy) = mk_Frees "f" typ_fs names_lthy;
|
|
115 |
val (var_x, names_lthy) = mk_Frees "x" [typ_aF] names_lthy |>> the_single;
|
|
116 |
val mem_x = HOLogic.mk_mem (var_x, aF_set) |> HOLogic.mk_Trueprop;
|
|
117 |
val map_f = list_comb (map_F, var_fs);
|
|
118 |
val mem_map = HOLogic.mk_mem (map_f $ var_x, bF_set) |> HOLogic.mk_Trueprop;
|
|
119 |
val imp_map = Logic.mk_implies (mem_x, mem_map);
|
|
120 |
val map_closed_F = Library.foldr (Library.uncurry Logic.all) (var_fs, Logic.all var_x imp_map);
|
|
121 |
|
|
122 |
(* val zip_closed_F = @{term "\<And>z. map_F fst z \<in> F \<Longrightarrow> map_F snd z \<in> F \<Longrightarrow> z \<in> F"}; *)
|
|
123 |
val (var_zs, names_lthy) = mk_Frees "z" [typ_pair] names_lthy;
|
|
124 |
val (pairs, names_lthy) = mk_Frees "tmp" typ_pairs names_lthy;
|
|
125 |
val var_z = hd var_zs;
|
|
126 |
val fsts = map (fst o Term.strip_comb o HOLogic.mk_fst) pairs;
|
|
127 |
val snds = map (fst o Term.strip_comb o HOLogic.mk_snd) pairs;
|
|
128 |
val map_fst = list_comb (list_comb (map_F_fst, fsts), var_zs);
|
|
129 |
val mem_map_fst = HOLogic.mk_mem (map_fst, aF_set) |> HOLogic.mk_Trueprop;
|
|
130 |
val map_snd = list_comb (list_comb (map_F_snd, snds), var_zs);
|
|
131 |
val mem_map_snd = HOLogic.mk_mem (map_snd, aF_set') |> HOLogic.mk_Trueprop;
|
|
132 |
val mem_z = HOLogic.mk_mem (var_z, pairF_set) |> HOLogic.mk_Trueprop;
|
|
133 |
val imp_zip = Logic.mk_implies (mem_map_fst, Logic.mk_implies (mem_map_snd, mem_z));
|
|
134 |
val zip_closed_F = Logic.all var_z imp_zip;
|
|
135 |
|
|
136 |
(* val wit_closed_F = @{term "wit_F a \<in> F"}; *)
|
|
137 |
val (var_as, names_lthy) = mk_Frees "a" alphas names_lthy;
|
|
138 |
val (var_bs, _) = mk_Frees "a" alphas names_lthy;
|
|
139 |
val Iwits = the_default wits_F (Option.map (map (`(map (fn T =>
|
|
140 |
find_index (fn U => T = U) alphas) o fst o strip_type o fastype_of))) wits);
|
|
141 |
val wit_closed_Fs =
|
|
142 |
map (fn (I, wit_F) =>
|
|
143 |
let
|
|
144 |
val vars = map (nth var_as) I;
|
|
145 |
val wit_a = list_comb (wit_F, vars);
|
|
146 |
in
|
|
147 |
Library.foldr (Library.uncurry Logic.all) (vars,
|
|
148 |
HOLogic.mk_mem (wit_a, aF_set) |> HOLogic.mk_Trueprop)
|
|
149 |
end)
|
|
150 |
Iwits;
|
|
151 |
|
|
152 |
val mk_wit_goals = mk_wit_goals var_as var_bs
|
|
153 |
(mk_sets_of_bnf (replicate lives deads) (replicate lives alphas) bnf);
|
|
154 |
|
|
155 |
val goals = [map_closed_F, zip_closed_F] @ wit_closed_Fs @
|
|
156 |
(case wits of NONE => [] | _ => maps mk_wit_goals Iwits);
|
|
157 |
|
|
158 |
val lost_wits = filter_out (fn (J, _) => exists (fn (I, _) => I = J) Iwits) wits_F;
|
|
159 |
val _ = if null lost_wits orelse no_warn_wits then () else
|
|
160 |
lost_wits
|
|
161 |
|> map (Syntax.pretty_typ lthy o fastype_of o snd)
|
|
162 |
|> Pretty.big_list
|
|
163 |
"The following types of nonemptiness witnesses of the raw type's BNF were lost:"
|
|
164 |
|> (fn pt => Pretty.chunks [pt,
|
|
165 |
Pretty.para "You can specify a liftable witness (e.g., a term of one of the above types\
|
|
166 |
\ that satisfies the typedef's invariant)\
|
|
167 |
\ using the annotation [wits: <term>]."])
|
|
168 |
|> Pretty.string_of
|
|
169 |
|> warning;
|
|
170 |
|
|
171 |
fun after_qed ([map_closed_thm] :: [zip_closed_thm] :: wit_thmss) lthy =
|
|
172 |
let
|
|
173 |
val (wit_closed_thms, wit_thms) =
|
|
174 |
(case wits of
|
|
175 |
NONE => (map the_single wit_thmss, wit_thms_of_bnf bnf)
|
|
176 |
| _ => chop (length wit_closed_Fs) (map the_single wit_thmss))
|
|
177 |
|
|
178 |
(* construct map set bd rel wit *)
|
|
179 |
(* val map_G = @{term "\<lambda>f. Abs_G o map_F f o Rep_G"}; *)
|
|
180 |
val Abs_Gb = subst_b Abs_G;
|
|
181 |
val map_G = Library.foldr (uncurry HOLogic.tupled_lambda)
|
|
182 |
(var_fs, HOLogic.mk_comp (HOLogic.mk_comp (Abs_Gb, map_f),
|
|
183 |
Rep_G));
|
|
184 |
|
|
185 |
(* val sets_G = [@{term "set_F o Rep_G"}]; *)
|
|
186 |
val sets_F = mk_sets_of_bnf (replicate lives deads) (replicate lives alphas) bnf;
|
|
187 |
val sets_G = map (fn set_F => HOLogic.mk_comp (set_F, Rep_G)) sets_F;
|
|
188 |
|
|
189 |
(* val bd_G = @{term "bd_F"}; *)
|
|
190 |
val bd_F = mk_bd_of_bnf deads alphas bnf;
|
|
191 |
val bd_G = bd_F;
|
|
192 |
|
|
193 |
(* val rel_G = @{term "\<lambda>R. BNF_Def.vimage2p Rep_G Rep_G (rel_F R)"}; *)
|
|
194 |
val rel_F = mk_rel_of_bnf deads alphas betas bnf;
|
|
195 |
val (typ_Rs, _) = fastype_of rel_F |> strip_typeN lives;
|
|
196 |
|
|
197 |
val (var_Rs, names_lthy) = mk_Frees "R" typ_Rs lthy;
|
|
198 |
val Rep_Gb = subst_b Rep_G;
|
|
199 |
val rel_G = fold_rev absfree (map dest_Free var_Rs)
|
|
200 |
(mk_vimage2p Rep_G Rep_Gb $ list_comb (rel_F, var_Rs));
|
|
201 |
|
|
202 |
(* val wits_G = [@{term "Abs_G o wit_F"}]; *)
|
|
203 |
val (var_as, _) = mk_Frees "a" alphas names_lthy;
|
|
204 |
val wits_G =
|
|
205 |
map (fn (I, wit_F) =>
|
|
206 |
let
|
|
207 |
val vs = map (nth var_as) I;
|
|
208 |
in fold_rev absfree (map dest_Free vs) (Abs_G $ (list_comb (wit_F, vs))) end)
|
|
209 |
Iwits;
|
|
210 |
|
|
211 |
(* tactics *)
|
|
212 |
val Rep_thm = thm RS @{thm type_definition.Rep};
|
|
213 |
val Abs_inverse_thm = thm RS @{thm type_definition.Abs_inverse};
|
|
214 |
val Abs_inject_thm = thm RS @{thm type_definition.Abs_inject};
|
|
215 |
val Rep_cases_thm = thm RS @{thm type_definition.Rep_cases};
|
|
216 |
val Rep_inverse_thm = thm RS @{thm type_definition.Rep_inverse};
|
|
217 |
|
|
218 |
fun map_id0_tac ctxt =
|
|
219 |
HEADGOAL (EVERY' [rtac ctxt ext,
|
|
220 |
SELECT_GOAL (unfold_thms_tac ctxt [map_id0_of_bnf bnf, id_apply, o_apply,
|
|
221 |
Rep_inverse_thm]),
|
|
222 |
rtac ctxt refl]);
|
|
223 |
|
|
224 |
fun map_comp0_tac ctxt =
|
|
225 |
HEADGOAL (EVERY' [rtac ctxt ext,
|
|
226 |
SELECT_GOAL (unfold_thms_tac ctxt [map_comp0_of_bnf bnf, o_apply,
|
|
227 |
Rep_thm RS (map_closed_thm RS Abs_inverse_thm)]),
|
|
228 |
rtac ctxt refl]);
|
|
229 |
|
|
230 |
fun map_cong0_tac ctxt =
|
|
231 |
HEADGOAL (EVERY' ([SELECT_GOAL (unfold_thms_tac ctxt [o_apply]),
|
|
232 |
rtac ctxt (([Rep_thm RS map_closed_thm, Rep_thm RS map_closed_thm] MRS
|
|
233 |
Abs_inject_thm) RS iffD2),
|
|
234 |
rtac ctxt (map_cong0_of_bnf bnf)] @ replicate lives (Goal.assume_rule_tac ctxt)));
|
|
235 |
|
|
236 |
val set_map0s_tac =
|
|
237 |
map (fn set_map => fn ctxt =>
|
|
238 |
HEADGOAL (EVERY' [rtac ctxt ext,
|
|
239 |
SELECT_GOAL (unfold_thms_tac ctxt [set_map, o_apply,
|
|
240 |
Rep_thm RS (map_closed_thm RS Abs_inverse_thm)]),
|
|
241 |
rtac ctxt refl]))
|
|
242 |
(set_map_of_bnf bnf);
|
|
243 |
|
|
244 |
fun card_order_bd_tac ctxt = HEADGOAL (rtac ctxt (bd_card_order_of_bnf bnf));
|
|
245 |
|
|
246 |
fun cinfinite_bd_tac ctxt = HEADGOAL (rtac ctxt (bd_cinfinite_of_bnf bnf));
|
|
247 |
|
|
248 |
val set_bds_tac =
|
|
249 |
map (fn set_bd => fn ctxt =>
|
|
250 |
HEADGOAL (EVERY' [SELECT_GOAL (unfold_thms_tac ctxt [o_apply]), rtac ctxt set_bd]))
|
|
251 |
(set_bd_of_bnf bnf);
|
|
252 |
|
|
253 |
fun le_rel_OO_tac ctxt =
|
|
254 |
HEADGOAL (EVERY' [rtac ctxt @{thm vimage2p_relcompp_mono},
|
|
255 |
rtac ctxt ((rel_OO_of_bnf bnf RS sym) RS @{thm ord_eq_le_trans}),
|
|
256 |
rtac ctxt @{thm order_refl}]);
|
|
257 |
|
|
258 |
fun rel_OO_Grp_tac ctxt =
|
|
259 |
HEADGOAL (EVERY' ([SELECT_GOAL (REPEAT_DETERM (HEADGOAL (rtac ctxt ext))),
|
|
260 |
SELECT_GOAL (unfold_thms_tac ctxt [@{thm OO_Grp_alt}, mem_Collect_eq,
|
|
261 |
o_apply, @{thm vimage2p_def}, in_rel_of_bnf bnf, Bex_def, mem_Collect_eq]),
|
|
262 |
rtac ctxt iffI,
|
|
263 |
SELECT_GOAL (REPEAT_DETERM (HEADGOAL (eresolve0_tac [exE,conjE]))),
|
|
264 |
rtac ctxt (zip_closed_thm OF (replicate 2 (Rep_thm RSN (2, @{thm ssubst_mem}))) RS
|
|
265 |
Rep_cases_thm),
|
|
266 |
assume_tac ctxt,
|
|
267 |
assume_tac ctxt,
|
|
268 |
hyp_subst_tac ctxt,
|
|
269 |
SELECT_GOAL (REPEAT_DETERM (HEADGOAL (rtac ctxt exI))),
|
|
270 |
rtac ctxt conjI] @
|
|
271 |
replicate (lives - 1) (rtac ctxt conjI THEN' assume_tac ctxt) @
|
|
272 |
[assume_tac ctxt,
|
|
273 |
SELECT_GOAL (REPEAT_DETERM (HEADGOAL (rtac ctxt conjI))),
|
|
274 |
REPEAT_DETERM_N 2 o
|
|
275 |
etac ctxt (trans OF [iffD2 OF [Abs_inject_thm OF
|
|
276 |
[map_closed_thm OF [Rep_thm], Rep_thm]], Rep_inverse_thm]),
|
|
277 |
SELECT_GOAL (REPEAT_DETERM (HEADGOAL (eresolve0_tac [exE,conjE]))),
|
|
278 |
rtac ctxt exI,
|
|
279 |
rtac ctxt conjI] @
|
|
280 |
replicate (lives - 1) (rtac ctxt conjI THEN' assume_tac ctxt) @
|
|
281 |
[assume_tac ctxt,
|
|
282 |
rtac ctxt conjI,
|
|
283 |
REPEAT_DETERM_N 2 o EVERY'
|
|
284 |
[rtac ctxt (iffD1 OF [Abs_inject_thm OF [map_closed_thm OF [Rep_thm], Rep_thm]]),
|
|
285 |
etac ctxt (Rep_inverse_thm RS sym RSN (2, trans))]]));
|
|
286 |
|
|
287 |
fun wit_tac ctxt =
|
|
288 |
HEADGOAL (EVERY'
|
|
289 |
(map (fn thm => (EVERY'
|
|
290 |
[SELECT_GOAL (unfold_thms_tac ctxt (o_apply ::
|
|
291 |
(wit_closed_thms RL [Abs_inverse_thm]))),
|
|
292 |
dtac ctxt thm, assume_tac ctxt]))
|
|
293 |
wit_thms));
|
|
294 |
|
|
295 |
val tactics = [map_id0_tac, map_comp0_tac, map_cong0_tac] @ set_map0s_tac @
|
|
296 |
[card_order_bd_tac, cinfinite_bd_tac] @ set_bds_tac @ [le_rel_OO_tac, rel_OO_Grp_tac];
|
|
297 |
|
|
298 |
val (bnf, lthy) = bnf_def Dont_Inline (user_policy Note_Some) false I
|
|
299 |
tactics wit_tac NONE map_b rel_b set_bs
|
|
300 |
((((((Binding.empty, AbsT), map_G), sets_G), bd_G), wits_G), SOME rel_G)
|
|
301 |
lthy;
|
|
302 |
in
|
|
303 |
lthy |> BNF_Def.register_bnf plugins AbsT_name bnf
|
|
304 |
end
|
|
305 |
| after_qed _ _ = error "should not happen";
|
|
306 |
in
|
|
307 |
(goals, after_qed, defs, lthy)
|
|
308 |
end;
|
|
309 |
|
|
310 |
fun prepare_common prepare_name prepare_sort prepare_term prepare_thm
|
|
311 |
(((((plugins, raw_specs), raw_Tname), raw_wits), xthm_opt), (map_b, rel_b)) lthy =
|
|
312 |
let
|
|
313 |
val Tname = prepare_name lthy raw_Tname;
|
|
314 |
val input_thm =
|
|
315 |
(case xthm_opt of
|
|
316 |
SOME xthm => prepare_thm lthy xthm
|
|
317 |
| NONE => Typedef.get_info lthy Tname |> hd |> snd |> #type_definition);
|
|
318 |
val wits = Option.map (map (prepare_term lthy)) raw_wits;
|
|
319 |
val specs = map (apsnd (apsnd
|
|
320 |
(the_default @{sort type} o Option.map (prepare_sort lthy)))) raw_specs;
|
|
321 |
|
|
322 |
(* analyze theorem here*)
|
|
323 |
fun is_typedef (t as (Const ("Typedef.type_definition", _) $ _ $ _ $ _)) = t
|
|
324 |
| is_typedef t = raise TERM("not a typedef",[t]);
|
|
325 |
|
|
326 |
val _ = (HOLogic.dest_Trueprop o Thm.prop_of) input_thm |> is_typedef
|
|
327 |
handle TERM _ => error "Unsupported type of a theorem. Only type_definition is supported.";
|
|
328 |
in
|
|
329 |
typedef_bnf input_thm wits specs map_b rel_b plugins lthy
|
|
330 |
end;
|
|
331 |
|
|
332 |
fun prepare_lift_bnf prepare_name prepare_sort prepare_term prepare_thm =
|
|
333 |
(fn (goals, after_qed, definitions, lthy) =>
|
|
334 |
lthy
|
|
335 |
|> Proof.theorem NONE after_qed (map (single o rpair []) goals)
|
|
336 |
|> Proof.refine (Method.Basic (fn ctxt => SIMPLE_METHOD (unfold_thms_tac ctxt definitions)))
|
|
337 |
|> Seq.hd
|
|
338 |
|> Proof.refine (Method.primitive_text (K I))
|
|
339 |
|> Seq.hd) oo
|
|
340 |
prepare_common prepare_name prepare_sort prepare_term prepare_thm o apfst (apfst (apsnd SOME));
|
|
341 |
|
|
342 |
val lift_bnf_cmd = prepare_lift_bnf
|
|
343 |
(fst o dest_Type oo Proof_Context.read_type_name {proper = true, strict = false})
|
|
344 |
Syntax.read_sort Syntax.read_term (singleton o Attrib.eval_thms);
|
|
345 |
|
|
346 |
fun prepare_solve prepare_name prepare_typ prepare_sort prepare_thm tacs =
|
|
347 |
(fn (goals, after_qed, _, lthy) =>
|
|
348 |
lthy
|
|
349 |
|> after_qed (map2 (single oo Goal.prove lthy [] []) goals (tacs (length goals)))) oo
|
|
350 |
prepare_common prepare_name prepare_typ prepare_sort prepare_thm o apfst (apfst (rpair NONE));
|
|
351 |
|
|
352 |
fun lift_bnf args tacs = prepare_solve (K I) (K I) (K I) (K I) (K tacs) args;
|
|
353 |
val copy_bnf = prepare_solve (K I) (K I) (K I) (K I)
|
|
354 |
(fn n => replicate n (fn {context = ctxt, prems = _} => rtac ctxt UNIV_I 1));
|
|
355 |
val copy_bnf_cmd = prepare_solve
|
|
356 |
(fst o dest_Type oo Proof_Context.read_type_name {proper = true, strict = false})
|
|
357 |
Syntax.read_sort Syntax.read_term (singleton o Attrib.eval_thms)
|
|
358 |
(fn n => replicate n (fn {context = ctxt, prems = _} => rtac ctxt UNIV_I 1));
|
|
359 |
|
|
360 |
val parse_wits =
|
|
361 |
@{keyword "["} |-- (Parse.name --| @{keyword ":"} -- Scan.repeat Parse.term >>
|
|
362 |
(fn ("wits", Ts) => Ts
|
|
363 |
| (s, _) => error ("Unknown label " ^ quote s ^ " (expected \"wits\")"))) --|
|
|
364 |
@{keyword "]"} || Scan.succeed [];
|
|
365 |
|
|
366 |
val parse_options =
|
|
367 |
Scan.optional (@{keyword "("} |--
|
|
368 |
Parse.list1 (Parse.group (K "option")
|
|
369 |
(Plugin_Name.parse_filter >> Plugins_Option
|
|
370 |
|| Parse.reserved "no_warn_wits" >> K No_Warn_Wits))
|
|
371 |
--| @{keyword ")"}) [];
|
|
372 |
|
|
373 |
val parse_plugins =
|
|
374 |
Scan.optional (@{keyword "("} |-- Plugin_Name.parse_filter --| @{keyword ")"})
|
|
375 |
(K Plugin_Name.default_filter) >> Plugins_Option >> single;
|
|
376 |
|
|
377 |
val parse_typedef_thm = Scan.option (Parse.reserved "via" |-- Parse.xthm);
|
|
378 |
|
|
379 |
val _ =
|
|
380 |
Outer_Syntax.local_theory_to_proof @{command_keyword lift_bnf}
|
|
381 |
"register a subtype of a bounded natural functor (BNF) as a BNF"
|
|
382 |
((parse_options -- parse_type_args_named_constrained -- Parse.type_const -- parse_wits --
|
|
383 |
parse_typedef_thm -- parse_map_rel_bindings) >> lift_bnf_cmd);
|
|
384 |
|
|
385 |
val _ =
|
|
386 |
Outer_Syntax.local_theory @{command_keyword copy_bnf}
|
|
387 |
"register a type copy of a bounded natural functor (BNF) as a BNF"
|
|
388 |
((parse_plugins -- parse_type_args_named_constrained -- Parse.type_const --
|
|
389 |
parse_typedef_thm -- parse_map_rel_bindings) >> copy_bnf_cmd);
|
|
390 |
|
|
391 |
end |