26582
|
1 |
(* Title: Tools/atomize_elim.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Alexander Krauss, TU Muenchen
|
|
4 |
|
|
5 |
Turn elimination rules into atomic expressions in the object logic.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature ATOMIZE_ELIM =
|
|
9 |
sig
|
|
10 |
val declare_atomize_elim : attribute
|
|
11 |
|
|
12 |
val atomize_elim : Proof.context -> conv
|
|
13 |
val full_atomize_elim : Proof.context -> conv
|
|
14 |
|
|
15 |
val atomize_elim_tac : Proof.context -> int -> tactic
|
|
16 |
|
|
17 |
val setup : theory -> theory
|
|
18 |
end
|
|
19 |
|
|
20 |
structure AtomizeElim : ATOMIZE_ELIM =
|
|
21 |
struct
|
|
22 |
|
|
23 |
(* theory data *)
|
|
24 |
structure AtomizeElimData = TheoryDataFun
|
|
25 |
(
|
|
26 |
type T = thm list;
|
|
27 |
val empty = [];
|
|
28 |
val copy = I;
|
|
29 |
val extend = I;
|
|
30 |
fun merge _ = Thm.merge_thms
|
|
31 |
);
|
|
32 |
|
|
33 |
val add_elim = AtomizeElimData.map o Thm.add_thm
|
|
34 |
val declare_atomize_elim = Thm.declaration_attribute (fn th => Context.mapping (add_elim th) I);
|
|
35 |
|
|
36 |
|
|
37 |
(* More conversions: *)
|
|
38 |
local open Conv in
|
|
39 |
|
|
40 |
(* Compute inverse permutation *)
|
|
41 |
fun invert_perm pi =
|
|
42 |
(pi @ ((0 upto (fold (curry Int.max) pi 0)) \\ pi))
|
|
43 |
|> map_index I
|
|
44 |
|> sort (int_ord o pairself snd)
|
|
45 |
|> map fst
|
|
46 |
|
|
47 |
(* rearrange_prems as a conversion *)
|
|
48 |
fun rearrange_prems_conv pi ct =
|
|
49 |
let
|
|
50 |
val pi' = 0 :: map (curry op + 1) pi
|
|
51 |
val fwd = trivial ct
|
|
52 |
|> Drule.rearrange_prems pi'
|
|
53 |
val back = trivial (snd (Thm.dest_implies (cprop_of fwd)))
|
|
54 |
|> Drule.rearrange_prems (invert_perm pi')
|
|
55 |
in equal_intr fwd back end
|
|
56 |
|
|
57 |
|
|
58 |
(* convert !!thesis. (!!x y z. ... => thesis) ==> ...
|
|
59 |
==> (!!a b c. ... => thesis)
|
|
60 |
==> thesis
|
|
61 |
|
|
62 |
to
|
|
63 |
|
|
64 |
(EX x y z. ...) | ... | (EX a b c. ...)
|
|
65 |
*)
|
|
66 |
fun atomize_elim ctxt ct =
|
|
67 |
(forall_conv (K (prems_conv ~1 ObjectLogic.atomize_prems)) ctxt
|
|
68 |
then_conv MetaSimplifier.rewrite true (AtomizeElimData.get (ProofContext.theory_of ctxt))
|
|
69 |
then_conv (fn ct' => if can ObjectLogic.dest_judgment ct'
|
|
70 |
then all_conv ct'
|
|
71 |
else raise CTERM ("atomize_elim", [ct', ct])))
|
|
72 |
ct
|
|
73 |
|
|
74 |
|
|
75 |
(* Move the thesis-quantifier inside over other quantifiers and unrelated prems *)
|
|
76 |
fun thesis_miniscope_conv inner_cv ctxt =
|
|
77 |
let
|
|
78 |
(* check if the outermost quantifier binds the conclusion *)
|
|
79 |
fun is_forall_thesis (Const ("all", _) $ Abs (_, T, b)) =
|
|
80 |
exists_subterm (fn Free (n, _) => n = "" | _ => false)
|
|
81 |
(Logic.strip_assums_concl (subst_bound (Free ("", dummyT), b)))
|
|
82 |
| is_forall_thesis _ = false
|
|
83 |
|
|
84 |
(* move unrelated assumptions out of the quantification *)
|
|
85 |
fun movea_conv ctxt ct =
|
|
86 |
let
|
|
87 |
val _ $ Abs (_, _, b) = term_of ct
|
|
88 |
val idxs = fold_index (fn (i, t) => not (loose_bvar1 (t, 0)) ? cons i)
|
|
89 |
(Logic.strip_imp_prems b) []
|
|
90 |
|> rev
|
|
91 |
|
|
92 |
fun skip_over_assm cv = rewr_conv (Thm.symmetric Drule.norm_hhf_eq)
|
|
93 |
then_conv (implies_concl_conv cv)
|
|
94 |
in
|
|
95 |
(forall_conv (K (rearrange_prems_conv idxs)) ctxt
|
|
96 |
then_conv (funpow (length idxs) skip_over_assm (inner_cv ctxt)))
|
|
97 |
ct
|
|
98 |
end
|
|
99 |
|
|
100 |
(* move current quantifier to the right *)
|
|
101 |
fun moveq_conv ctxt =
|
|
102 |
(rewr_conv @{thm swap_params} then_conv (forall_conv (uncurry (K moveq_conv)) ctxt))
|
|
103 |
else_conv (movea_conv ctxt)
|
|
104 |
|
|
105 |
fun ms_conv ctxt ct =
|
|
106 |
if is_forall_thesis (term_of ct)
|
|
107 |
then moveq_conv ctxt ct
|
|
108 |
else (implies_concl_conv (ms_conv ctxt)
|
|
109 |
else_conv
|
|
110 |
(forall_conv (uncurry (K ms_conv)) ctxt))
|
|
111 |
ct
|
|
112 |
in
|
|
113 |
ms_conv ctxt
|
|
114 |
end
|
|
115 |
|
|
116 |
val full_atomize_elim = thesis_miniscope_conv atomize_elim
|
|
117 |
|
|
118 |
end
|
|
119 |
|
|
120 |
|
|
121 |
fun atomize_elim_tac ctxt = CSUBGOAL (fn (csubg, i) =>
|
|
122 |
let
|
|
123 |
val _ $ thesis = Logic.strip_assums_concl (term_of csubg)
|
|
124 |
|
|
125 |
(* Introduce a quantifier first if the thesis is not bound *)
|
|
126 |
val quantify_thesis =
|
|
127 |
if is_Bound thesis then all_tac
|
|
128 |
else let
|
|
129 |
val cthesis = cterm_of (theory_of_cterm csubg) thesis
|
|
130 |
val rule = instantiate' [SOME (ctyp_of_term cthesis)] [NONE, SOME cthesis]
|
|
131 |
@{thm meta_spec}
|
|
132 |
in
|
|
133 |
Thm.compose_no_flatten false (rule, 1) i
|
|
134 |
end
|
|
135 |
in
|
|
136 |
quantify_thesis
|
|
137 |
THEN (CONVERSION (full_atomize_elim ctxt) i)
|
|
138 |
end)
|
|
139 |
|
|
140 |
val setup = Method.add_methods
|
|
141 |
[("atomize_elim", Method.ctxt_args (Method.SIMPLE_METHOD' o atomize_elim_tac),
|
|
142 |
"convert obtains statement to atomic object logic goal")]
|
|
143 |
#> Attrib.add_attributes
|
|
144 |
[("atomize_elim", Attrib.no_args declare_atomize_elim, "declaration of atomize_elim rewrite rule")]
|
|
145 |
|
|
146 |
end
|