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