author | berghofe |
Mon, 12 Jun 2006 18:17:21 +0200 | |
changeset 19857 | a0c36e0fc897 |
parent 19494 | 2e909d5309f4 |
child 19858 | d319e39a2e0e |
permissions | -rw-r--r-- |
19494 | 1 |
(* Title: HOL/Nominal/nominal_permeq.ML |
2 |
ID: $Id$ |
|
3 |
Author: Christian Urban, TU Muenchen |
|
17870 | 4 |
|
19494 | 5 |
Methods for simplifying permutations and |
6 |
for analysing equations involving permutations. |
|
7 |
*) |
|
17870 | 8 |
|
9 |
local |
|
10 |
||
18012 | 11 |
(* pulls out dynamically a thm via the simpset *) |
19477 | 12 |
fun dynamic_thms ss name = ProofContext.get_thms (Simplifier.the_context ss) (Name name); |
13 |
fun dynamic_thm ss name = ProofContext.get_thm (Simplifier.the_context ss) (Name name); |
|
18012 | 14 |
|
19477 | 15 |
(* a tactic simplyfying permutations *) |
19494 | 16 |
val perm_fun_def = thm "Nominal.perm_fun_def" |
17 |
val perm_eq_app = thm "Nominal.pt_fun_app_eq" |
|
19477 | 18 |
|
19139 | 19 |
fun perm_eval_tac ss i = |
18012 | 20 |
let |
19139 | 21 |
fun perm_eval_simproc sg ss redex = |
19169
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
22 |
let |
19477 | 23 |
(* the "application" case below is only applicable when the head *) |
19169
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
24 |
(* of f is not a constant or when it is a permuattion with two or *) |
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
25 |
(* more arguments *) |
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
26 |
fun applicable t = |
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
27 |
(case (strip_comb t) of |
19494 | 28 |
(Const ("Nominal.perm",_),ts) => (length ts) >= 2 |
19169
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
29 |
| (Const _,_) => false |
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
30 |
| _ => true) |
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
31 |
|
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
32 |
in |
19139 | 33 |
(case redex of |
19169
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
34 |
(* case pi o (f x) == (pi o f) (pi o x) *) |
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
35 |
(* special treatment according to the head of f *) |
19494 | 36 |
(Const("Nominal.perm", |
19169
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
37 |
Type("fun",[Type("List.list",[Type("*",[Type(n,_),_])]),_])) $ pi $ (f $ x)) => |
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
38 |
(case (applicable f) of |
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
39 |
false => NONE |
19163 | 40 |
| _ => |
41 |
let |
|
42 |
val name = Sign.base_name n |
|
43 |
val at_inst = dynamic_thm ss ("at_"^name^"_inst") |
|
44 |
val pt_inst = dynamic_thm ss ("pt_"^name^"_inst") |
|
45 |
in SOME ((at_inst RS (pt_inst RS perm_eq_app)) RS eq_reflection) end) |
|
19139 | 46 |
|
47 |
(* case pi o (%x. f x) == (%x. pi o (f ((rev pi)o x))) *) |
|
19494 | 48 |
| (Const("Nominal.perm",_) $ pi $ (Abs _)) => SOME (perm_fun_def) |
19139 | 49 |
|
50 |
(* no redex otherwise *) |
|
19169
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
51 |
| _ => NONE) end |
19139 | 52 |
|
53 |
val perm_eval = |
|
54 |
Simplifier.simproc (Theory.sign_of (the_context ())) "perm_eval" |
|
19494 | 55 |
["Nominal.perm pi x"] perm_eval_simproc; |
19139 | 56 |
|
19477 | 57 |
(* these lemmas are created dynamically according to the atom types *) |
58 |
val perm_swap = dynamic_thms ss "perm_swap" |
|
59 |
val perm_fresh = dynamic_thms ss "perm_fresh_fresh" |
|
60 |
val perm_bij = dynamic_thms ss "perm_bij" |
|
61 |
val perm_pi_simp = dynamic_thms ss "perm_pi_simp" |
|
62 |
val ss' = ss addsimps (perm_swap@perm_fresh@perm_bij@perm_pi_simp) |
|
63 |
in |
|
64 |
("general simplification step", asm_full_simp_tac (ss' addsimprocs [perm_eval]) i) |
|
65 |
end; |
|
66 |
||
67 |
(* applies the perm_compose rule such that *) |
|
68 |
(* *) |
|
69 |
(* pi o (pi' o lhs) = rhs *) |
|
70 |
(* *) |
|
71 |
(* is transformed to *) |
|
72 |
(* *) |
|
73 |
(* (pi o pi') o (pi' o lhs) = rhs *) |
|
74 |
(* *) |
|
75 |
(* this rule would loop in the simplifier, so some trick is used with *) |
|
76 |
(* generating perm_aux'es for the outermost permutation and then un- *) |
|
77 |
(* folding the definition *) |
|
78 |
val pt_perm_compose_aux = thm "pt_perm_compose_aux"; |
|
79 |
val cp1_aux = thm "cp1_aux"; |
|
80 |
val perm_aux_fold = thm "perm_aux_fold"; |
|
81 |
||
82 |
fun perm_compose_tac ss i = |
|
83 |
let |
|
84 |
fun perm_compose_simproc sg ss redex = |
|
85 |
(case redex of |
|
19494 | 86 |
(Const ("Nominal.perm", Type ("fun", [Type ("List.list", |
87 |
[Type ("*", [T as Type (tname,_),_])]),_])) $ pi1 $ (Const ("Nominal.perm", |
|
19477 | 88 |
Type ("fun", [Type ("List.list", [Type ("*", [U as Type (uname,_),_])]),_])) $ |
89 |
pi2 $ t)) => |
|
19350 | 90 |
let |
91 |
val tname' = Sign.base_name tname |
|
19477 | 92 |
val uname' = Sign.base_name uname |
19350 | 93 |
in |
19477 | 94 |
if pi1 <> pi2 then (* only apply the composition rule in this case *) |
95 |
if T = U then |
|
19350 | 96 |
SOME (Drule.instantiate' |
97 |
[SOME (ctyp_of sg (fastype_of t))] |
|
98 |
[SOME (cterm_of sg pi1), SOME (cterm_of sg pi2), SOME (cterm_of sg t)] |
|
99 |
(mk_meta_eq ([PureThy.get_thm sg (Name ("pt_"^tname'^"_inst")), |
|
19477 | 100 |
PureThy.get_thm sg (Name ("at_"^tname'^"_inst"))] MRS pt_perm_compose_aux))) |
101 |
else |
|
102 |
SOME (Drule.instantiate' |
|
103 |
[SOME (ctyp_of sg (fastype_of t))] |
|
104 |
[SOME (cterm_of sg pi1), SOME (cterm_of sg pi2), SOME (cterm_of sg t)] |
|
105 |
(mk_meta_eq (PureThy.get_thm sg (Name ("cp_"^tname'^"_"^uname'^"_inst")) RS |
|
106 |
cp1_aux))) |
|
19350 | 107 |
else NONE |
108 |
end |
|
109 |
| _ => NONE); |
|
19477 | 110 |
|
111 |
val perm_compose = |
|
19350 | 112 |
Simplifier.simproc (the_context()) "perm_compose" |
19494 | 113 |
["Nominal.perm pi1 (Nominal.perm pi2 t)"] perm_compose_simproc; |
19477 | 114 |
|
115 |
val ss' = Simplifier.theory_context (the_context ()) empty_ss |
|
116 |
||
18012 | 117 |
in |
19477 | 118 |
("analysing permutation compositions on the lhs", |
119 |
EVERY [rtac trans i, |
|
120 |
asm_full_simp_tac (ss' addsimprocs [perm_compose]) i, |
|
121 |
asm_full_simp_tac (HOL_basic_ss addsimps [perm_aux_fold]) i]) |
|
18012 | 122 |
end |
123 |
||
124 |
(* applying Stefan's smart congruence tac *) |
|
125 |
fun apply_cong_tac i = |
|
126 |
("application of congruence", |
|
19477 | 127 |
(fn st => DatatypeAux.cong_tac i st handle Subscript => no_tac st)); |
17870 | 128 |
|
19477 | 129 |
(* unfolds the definition of permutations *) |
130 |
(* applied to functions such that *) |
|
131 |
(* *) |
|
132 |
(* pi o f = rhs *) |
|
133 |
(* *) |
|
134 |
(* is transformed to *) |
|
135 |
(* *) |
|
136 |
(* %x. pi o (f ((rev pi) o x)) = rhs *) |
|
18012 | 137 |
fun unfold_perm_fun_def_tac i = |
138 |
let |
|
19494 | 139 |
val perm_fun_def = thm "Nominal.perm_fun_def" |
18012 | 140 |
in |
141 |
("unfolding of permutations on functions", |
|
19477 | 142 |
rtac (perm_fun_def RS meta_eq_to_obj_eq RS trans) i) |
17870 | 143 |
end |
144 |
||
19477 | 145 |
(* applies the ext-rule such that *) |
146 |
(* *) |
|
147 |
(* f = g goes to /\x. f x = g x *) |
|
148 |
fun ext_fun_tac i = ("extensionality expansion of functions", rtac ext i); |
|
17870 | 149 |
|
19477 | 150 |
(* FIXME FIXME FIXME *) |
151 |
(* should be able to analyse pi o fresh_fun () = fresh_fun instances *) |
|
152 |
fun fresh_fun_eqvt_tac i = |
|
153 |
let |
|
19494 | 154 |
val fresh_fun_equiv = thm "Nominal.fresh_fun_equiv_ineq" |
19477 | 155 |
in |
156 |
("fresh_fun equivariance", rtac (fresh_fun_equiv RS trans) i) |
|
157 |
end |
|
158 |
||
17870 | 159 |
(* debugging *) |
160 |
fun DEBUG_tac (msg,tac) = |
|
19169
20a73345dd6e
fixed a problem where a permutation is not analysed
urbanc
parents:
19163
diff
changeset
|
161 |
CHANGED (EVERY [tac, print_tac ("after "^msg)]); |
17870 | 162 |
fun NO_DEBUG_tac (_,tac) = CHANGED tac; |
163 |
||
19477 | 164 |
(* Main Tactics *) |
18012 | 165 |
fun perm_simp_tac tactical ss i = |
19139 | 166 |
DETERM (tactical (perm_eval_tac ss i)); |
167 |
||
19477 | 168 |
(* perm_full_simp_tac is perm_simp_tac plus additional tactics *) |
169 |
(* to decide equation that come from support problems *) |
|
170 |
(* since it contains looping rules the "recursion" - depth is set *) |
|
171 |
(* to 10 - this seems to be sufficient in most cases *) |
|
172 |
fun perm_full_simp_tac tactical ss = |
|
173 |
let fun perm_full_simp_tac_aux tactical ss n = |
|
174 |
if n=0 then K all_tac |
|
175 |
else DETERM o |
|
176 |
(FIRST'[fn i => tactical ("splitting conjunctions on the rhs", rtac conjI i), |
|
177 |
fn i => tactical (perm_eval_tac ss i), |
|
178 |
fn i => tactical (perm_compose_tac ss i), |
|
179 |
fn i => tactical (apply_cong_tac i), |
|
180 |
fn i => tactical (unfold_perm_fun_def_tac i), |
|
181 |
fn i => tactical (ext_fun_tac i), |
|
182 |
fn i => tactical (fresh_fun_eqvt_tac i)] |
|
183 |
THEN_ALL_NEW (TRY o (perm_full_simp_tac_aux tactical ss (n-1)))) |
|
184 |
in perm_full_simp_tac_aux tactical ss 10 end; |
|
19151 | 185 |
|
19477 | 186 |
(* tactic that first unfolds the support definition *) |
187 |
(* and strips off the intros, then applies perm_full_simp_tac *) |
|
18012 | 188 |
fun supports_tac tactical ss i = |
189 |
let |
|
19494 | 190 |
val supports_def = thm "Nominal.op supports_def"; |
191 |
val fresh_def = thm "Nominal.fresh_def"; |
|
192 |
val fresh_prod = thm "Nominal.fresh_prod"; |
|
18012 | 193 |
val simps = [supports_def,symmetric fresh_def,fresh_prod] |
17870 | 194 |
in |
19477 | 195 |
EVERY [tactical ("unfolding of supports ", simp_tac (HOL_basic_ss addsimps simps) i), |
196 |
tactical ("stripping of foralls ", REPEAT_DETERM (rtac allI i)), |
|
197 |
tactical ("geting rid of the imps ", rtac impI i), |
|
198 |
tactical ("eliminating conjuncts ", REPEAT_DETERM (etac conjE i)), |
|
199 |
tactical ("applying perm_full_simp ", perm_full_simp_tac tactical ss i |
|
200 |
(*perm_simp_tac tactical ss i*))] |
|
17870 | 201 |
end; |
202 |
||
19151 | 203 |
|
19477 | 204 |
(* tactic that guesses the finite-support of a goal *) |
205 |
(* it collects all free variables and tries to show *) |
|
206 |
(* that the support of these free variables (op supports) *) |
|
207 |
(* the goal *) |
|
19151 | 208 |
fun collect_vars i (Bound j) vs = if j < i then vs else Bound (j - i) ins vs |
209 |
| collect_vars i (v as Free _) vs = v ins vs |
|
210 |
| collect_vars i (v as Var _) vs = v ins vs |
|
211 |
| collect_vars i (Const _) vs = vs |
|
212 |
| collect_vars i (Abs (_, _, t)) vs = collect_vars (i+1) t vs |
|
213 |
| collect_vars i (t $ u) vs = collect_vars i u (collect_vars i t vs); |
|
214 |
||
215 |
val supports_rule = thm "supports_finite"; |
|
216 |
||
217 |
fun finite_guess_tac tactical ss i st = |
|
218 |
let val goal = List.nth(cprems_of st, i-1) |
|
219 |
in |
|
220 |
case Logic.strip_assums_concl (term_of goal) of |
|
19494 | 221 |
_ $ (Const ("op :", _) $ (Const ("Nominal.supp", T) $ x) $ |
19151 | 222 |
Const ("Finite_Set.Finites", _)) => |
223 |
let |
|
224 |
val cert = Thm.cterm_of (sign_of_thm st); |
|
225 |
val ps = Logic.strip_params (term_of goal); |
|
226 |
val Ts = rev (map snd ps); |
|
227 |
val vs = collect_vars 0 x []; |
|
228 |
val s = foldr (fn (v, s) => |
|
229 |
HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s) |
|
230 |
HOLogic.unit vs; |
|
231 |
val s' = list_abs (ps, |
|
19494 | 232 |
Const ("Nominal.supp", fastype_of1 (Ts, s) --> body_type T) $ s); |
19151 | 233 |
val supports_rule' = Thm.lift_rule goal supports_rule; |
234 |
val _ $ (_ $ S $ _) = |
|
235 |
Logic.strip_assums_concl (hd (prems_of supports_rule')); |
|
236 |
val supports_rule'' = Drule.cterm_instantiate |
|
237 |
[(cert (head_of S), cert s')] supports_rule' |
|
238 |
in |
|
239 |
(tactical ("guessing of the right supports-set", |
|
240 |
EVERY [compose_tac (false, supports_rule'', 2) i, |
|
241 |
asm_full_simp_tac ss (i+1), |
|
242 |
supports_tac tactical ss i])) st |
|
243 |
end |
|
244 |
| _ => Seq.empty |
|
245 |
end |
|
246 |
handle Subscript => Seq.empty |
|
247 |
||
19857 | 248 |
val supports_fresh_rule = thm "supports_fresh"; |
249 |
||
250 |
fun fresh_guess_tac tactical ss i st = |
|
251 |
let |
|
252 |
val goal = List.nth(cprems_of st, i-1) |
|
253 |
in |
|
254 |
case Logic.strip_assums_concl (term_of goal) of |
|
255 |
_ $ (Const ("Nominal.fresh", Type ("fun", [T, _])) $ _ $ t) => |
|
256 |
let |
|
257 |
val cert = Thm.cterm_of (sign_of_thm st); |
|
258 |
val ps = Logic.strip_params (term_of goal); |
|
259 |
val Ts = rev (map snd ps); |
|
260 |
val vs = collect_vars 0 t []; |
|
261 |
val s = foldr (fn (v, s) => |
|
262 |
HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s) |
|
263 |
HOLogic.unit vs; |
|
264 |
val s' = list_abs (ps, |
|
265 |
Const ("Nominal.supp", fastype_of1 (Ts, s) --> (HOLogic.mk_setT T)) $ s); |
|
266 |
val supports_fresh_rule' = Thm.lift_rule goal supports_fresh_rule; |
|
267 |
val _ $ (_ $ S $ _) = |
|
268 |
Logic.strip_assums_concl (hd (prems_of supports_fresh_rule')); |
|
269 |
val supports_fresh_rule'' = Drule.cterm_instantiate |
|
270 |
[(cert (head_of S), cert s')] supports_fresh_rule' |
|
271 |
in |
|
272 |
(tactical ("guessing of the right set that supports the goal", |
|
273 |
EVERY [compose_tac (false, supports_fresh_rule'', 3) i(*, |
|
274 |
asm_full_simp_tac ss (i+1), |
|
275 |
supports_tac tactical ss i*)])) st |
|
276 |
end |
|
277 |
| _ => Seq.empty |
|
278 |
end |
|
279 |
handle Subscript => Seq.empty |
|
280 |
||
17870 | 281 |
in |
282 |
||
18012 | 283 |
fun simp_meth_setup tac = |
18046
b7389981170b
Changed Simplifier.simp_modifiers to Simplifier.simp_modifiers'.
urbanc
parents:
18012
diff
changeset
|
284 |
Method.only_sectioned_args (Simplifier.simp_modifiers' @ Splitter.split_modifiers) |
18012 | 285 |
(Method.SIMPLE_METHOD' HEADGOAL o tac o local_simpset_of); |
17870 | 286 |
|
19477 | 287 |
val perm_eq_meth = simp_meth_setup (perm_simp_tac NO_DEBUG_tac); |
288 |
val perm_eq_meth_debug = simp_meth_setup (perm_simp_tac DEBUG_tac); |
|
289 |
val perm_full_eq_meth = simp_meth_setup (perm_full_simp_tac NO_DEBUG_tac); |
|
290 |
val perm_full_eq_meth_debug = simp_meth_setup (perm_full_simp_tac DEBUG_tac); |
|
291 |
val supports_meth = simp_meth_setup (supports_tac NO_DEBUG_tac); |
|
292 |
val supports_meth_debug = simp_meth_setup (supports_tac DEBUG_tac); |
|
293 |
val finite_gs_meth = simp_meth_setup (finite_guess_tac NO_DEBUG_tac); |
|
294 |
val finite_gs_meth_debug = simp_meth_setup (finite_guess_tac DEBUG_tac); |
|
19857 | 295 |
val fresh_gs_meth = simp_meth_setup (fresh_guess_tac NO_DEBUG_tac); |
296 |
val fresh_gs_meth_debug = simp_meth_setup (fresh_guess_tac DEBUG_tac); |
|
17870 | 297 |
|
298 |
end |
|
299 |
||
300 |
||
301 |