author | wenzelm |
Wed, 26 Nov 2014 20:05:34 +0100 | |
changeset 59058 | a78612c67ec0 |
parent 58956 | a816aa3ff391 |
child 59582 | 0fbed69ff081 |
permissions | -rw-r--r-- |
22729 | 1 |
(* Title: HOL/Nominal/nominal_fresh_fun.ML |
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
28397
diff
changeset
|
2 |
Authors: Stefan Berghofer and Julien Narboux, TU Muenchen |
22729 | 3 |
|
24558 | 4 |
Provides a tactic to generate fresh names and |
5 |
a tactic to analyse instances of the fresh_fun. |
|
22729 | 6 |
*) |
7 |
||
56230 | 8 |
(* FIXME proper ML structure! *) |
22729 | 9 |
|
43278 | 10 |
(* FIXME res_inst_tac mostly obsolete, cf. Subgoal.FOCUS *) |
11 |
||
12 |
(* FIXME proper SUBGOAL/CSUBGOAL instead of cprems_of etc. *) |
|
58956
a816aa3ff391
proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
wenzelm
parents:
58950
diff
changeset
|
13 |
fun gen_res_inst_tac_term ctxt instf tyinst tinst elim th i st = |
22729 | 14 |
let |
15 |
val thy = theory_of_thm st; |
|
16 |
val cgoal = nth (cprems_of st) (i - 1); |
|
17 |
val {maxidx, ...} = rep_cterm cgoal; |
|
18 |
val j = maxidx + 1; |
|
19 |
val tyinst' = map (apfst (Logic.incr_tvar j)) tyinst; |
|
20 |
val ps = Logic.strip_params (term_of cgoal); |
|
21 |
val Ts = map snd ps; |
|
22 |
val tinst' = map (fn (t, u) => |
|
23 |
(head_of (Logic.incr_indexes (Ts, j) t), |
|
46219
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents:
44121
diff
changeset
|
24 |
fold_rev Term.abs ps u)) tinst; |
22729 | 25 |
val th' = instf |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58956
diff
changeset
|
26 |
(map (apply2 (ctyp_of thy)) tyinst') |
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58956
diff
changeset
|
27 |
(map (apply2 (cterm_of thy)) tinst') |
22729 | 28 |
(Thm.lift_rule cgoal th) |
29 |
in |
|
58956
a816aa3ff391
proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
wenzelm
parents:
58950
diff
changeset
|
30 |
compose_tac ctxt (elim, th', nprems_of th) i st |
43278 | 31 |
end handle General.Subscript => Seq.empty; |
32 |
(* FIXME proper SUBGOAL/CSUBGOAL instead of cprems_of etc. *) |
|
22729 | 33 |
|
58956
a816aa3ff391
proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
wenzelm
parents:
58950
diff
changeset
|
34 |
fun res_inst_tac_term ctxt = |
a816aa3ff391
proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
wenzelm
parents:
58950
diff
changeset
|
35 |
gen_res_inst_tac_term ctxt (curry Thm.instantiate); |
22729 | 36 |
|
58956
a816aa3ff391
proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
wenzelm
parents:
58950
diff
changeset
|
37 |
fun res_inst_tac_term' ctxt = |
a816aa3ff391
proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
wenzelm
parents:
58950
diff
changeset
|
38 |
gen_res_inst_tac_term ctxt (K Drule.cterm_instantiate) []; |
22729 | 39 |
|
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58318
diff
changeset
|
40 |
fun cut_inst_tac_term' ctxt tinst th = |
58956
a816aa3ff391
proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
wenzelm
parents:
58950
diff
changeset
|
41 |
res_inst_tac_term' ctxt tinst false (Rule_Insts.make_elim_preserve ctxt th); |
22729 | 42 |
|
26337 | 43 |
fun get_dyn_thm thy name atom_name = |
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
36610
diff
changeset
|
44 |
Global_Theory.get_thm thy name handle ERROR _ => |
27187 | 45 |
error ("The atom type "^atom_name^" is not defined."); |
22729 | 46 |
|
24558 | 47 |
(* The theorems needed that are known at compile time. *) |
48 |
val at_exists_fresh' = @{thm "at_exists_fresh'"}; |
|
49 |
val fresh_fun_app' = @{thm "fresh_fun_app'"}; |
|
50 |
val fresh_prod = @{thm "fresh_prod"}; |
|
22729 | 51 |
|
33034 | 52 |
(* A tactic to generate a name fresh for all the free *) |
24558 | 53 |
(* variables and parameters of the goal *) |
22729 | 54 |
|
56230 | 55 |
fun generate_fresh_tac ctxt atom_name = SUBGOAL (fn (goal, _) => |
33034 | 56 |
let |
56230 | 57 |
val thy = Proof_Context.theory_of ctxt; |
22729 | 58 |
(* the parsing function returns a qualified name, we get back the base name *) |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
59 |
val atom_basename = Long_Name.base_name atom_name; |
22729 | 60 |
val ps = Logic.strip_params goal; |
61 |
val Ts = rev (map snd ps); |
|
35667 | 62 |
fun is_of_fs_name T = Sign.of_sort thy (T, [Sign.intern_class thy ("fs_"^atom_basename)]); |
22729 | 63 |
(* rebuild de bruijn indices *) |
64 |
val bvs = map_index (Bound o fst) ps; |
|
65 |
(* select variables of the right class *) |
|
66 |
val vs = filter (fn t => is_of_fs_name (fastype_of1 (Ts, t))) |
|
44121 | 67 |
(Misc_Legacy.term_frees goal @ bvs); |
22729 | 68 |
(* build the tuple *) |
23368
ad690b9bca1c
generate_fresh works even if there is no free variable in the goal
narboux
parents:
23094
diff
changeset
|
69 |
val s = (Library.foldr1 (fn (v, s) => |
41519
0940fff556a6
avoid catch-all exception handler, presumably TERM was meant here;
wenzelm
parents:
39557
diff
changeset
|
70 |
HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s) vs) |
0940fff556a6
avoid catch-all exception handler, presumably TERM was meant here;
wenzelm
parents:
39557
diff
changeset
|
71 |
handle TERM _ => HOLogic.unit; |
22729 | 72 |
val fs_name_thm = get_dyn_thm thy ("fs_"^atom_basename^"1") atom_basename; |
73 |
val at_name_inst_thm = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename; |
|
74 |
val exists_fresh' = at_name_inst_thm RS at_exists_fresh'; |
|
75 |
(* find the variable we want to instantiate *) |
|
44121 | 76 |
val x = hd (Misc_Legacy.term_vars (prop_of exists_fresh')); |
33034 | 77 |
in |
56230 | 78 |
fn st => |
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58318
diff
changeset
|
79 |
(cut_inst_tac_term' ctxt [(x,s)] exists_fresh' 1 THEN |
22729 | 80 |
rtac fs_name_thm 1 THEN |
56230 | 81 |
etac exE 1) st |
82 |
handle List.Empty => all_tac st (* if we collected no variables then we do nothing *) |
|
83 |
end) 1; |
|
22729 | 84 |
|
85 |
fun get_inner_fresh_fun (Bound j) = NONE |
|
33034 | 86 |
| get_inner_fresh_fun (v as Free _) = NONE |
22729 | 87 |
| get_inner_fresh_fun (v as Var _) = NONE |
88 |
| get_inner_fresh_fun (Const _) = NONE |
|
33034 | 89 |
| get_inner_fresh_fun (Abs (_, _, t)) = get_inner_fresh_fun t |
56253 | 90 |
| get_inner_fresh_fun (Const (@{const_name Nominal.fresh_fun}, |
91 |
Type(@{type_name fun},[Type (@{type_name fun},[Type (T,_),_]),_])) $ u) = SOME T |
|
33034 | 92 |
| get_inner_fresh_fun (t $ u) = |
22729 | 93 |
let val a = get_inner_fresh_fun u in |
33034 | 94 |
if a = NONE then get_inner_fresh_fun t else a |
22729 | 95 |
end; |
96 |
||
33034 | 97 |
(* This tactic generates a fresh name of the atom type *) |
24558 | 98 |
(* given by the innermost fresh_fun *) |
22729 | 99 |
|
56230 | 100 |
fun generate_fresh_fun_tac ctxt = SUBGOAL (fn (goal, _) => |
22729 | 101 |
let |
102 |
val atom_name_opt = get_inner_fresh_fun goal; |
|
103 |
in |
|
33034 | 104 |
case atom_name_opt of |
56230 | 105 |
NONE => all_tac |
106 |
| SOME atom_name => generate_fresh_tac ctxt atom_name |
|
107 |
end) 1; |
|
22729 | 108 |
|
58318 | 109 |
(* Two substitution tactics which looks for the innermost occurrence in |
23091
c91530f18d9c
add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents:
23065
diff
changeset
|
110 |
one assumption or in the conclusion *) |
22729 | 111 |
|
33034 | 112 |
val search_fun = curry (Seq.flat o uncurry EqSubst.searchf_bt_unify_valid); |
23065 | 113 |
val search_fun_asm = EqSubst.skip_first_asm_occs_search EqSubst.searchf_bt_unify_valid; |
23054
d1bbe5afa279
change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents:
22792
diff
changeset
|
114 |
|
33034 | 115 |
fun subst_inner_tac ctxt = EqSubst.eqsubst_tac' ctxt search_fun; |
116 |
fun subst_inner_asm_tac_aux i ctxt = EqSubst.eqsubst_asm_tac' ctxt search_fun_asm i; |
|
23054
d1bbe5afa279
change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents:
22792
diff
changeset
|
117 |
|
33034 | 118 |
(* A tactic to substitute in the first assumption |
58318 | 119 |
which contains an occurrence. *) |
22729 | 120 |
|
33034 | 121 |
fun subst_inner_asm_tac ctxt th = |
122 |
curry (curry (FIRST' (map uncurry (map uncurry (map subst_inner_asm_tac_aux |
|
123 |
(1 upto Thm.nprems_of th)))))) ctxt th; |
|
23091
c91530f18d9c
add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents:
23065
diff
changeset
|
124 |
|
56230 | 125 |
fun fresh_fun_tac ctxt no_asm = SUBGOAL (fn (goal, i) => |
22729 | 126 |
(* Find the variable we instantiate *) |
127 |
let |
|
56230 | 128 |
val thy = Proof_Context.theory_of ctxt; |
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
36610
diff
changeset
|
129 |
val abs_fresh = Global_Theory.get_thms thy "abs_fresh"; |
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
36610
diff
changeset
|
130 |
val fresh_perm_app = Global_Theory.get_thms thy "fresh_perm_app"; |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51669
diff
changeset
|
131 |
val simp_ctxt = |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51669
diff
changeset
|
132 |
ctxt addsimps (fresh_prod :: abs_fresh) |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51669
diff
changeset
|
133 |
addsimps fresh_perm_app; |
44121 | 134 |
val x = hd (tl (Misc_Legacy.term_vars (prop_of exI))); |
22729 | 135 |
val atom_name_opt = get_inner_fresh_fun goal; |
33034 | 136 |
val n = length (Logic.strip_params goal); |
24558 | 137 |
(* Here we rely on the fact that the variable introduced by generate_fresh_tac *) |
138 |
(* is the last one in the list, the inner one *) |
|
22729 | 139 |
in |
33034 | 140 |
case atom_name_opt of |
56230 | 141 |
NONE => all_tac |
33034 | 142 |
| SOME atom_name => |
143 |
let |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
144 |
val atom_basename = Long_Name.base_name atom_name; |
22729 | 145 |
val pt_name_inst = get_dyn_thm thy ("pt_"^atom_basename^"_inst") atom_basename; |
146 |
val at_name_inst = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename; |
|
22787 | 147 |
fun inst_fresh vars params i st = |
44121 | 148 |
let val vars' = Misc_Legacy.term_vars (prop_of st); |
22787 | 149 |
val thy = theory_of_thm st; |
33040 | 150 |
in case subtract (op =) vars vars' of |
46219
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents:
44121
diff
changeset
|
151 |
[x] => |
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents:
44121
diff
changeset
|
152 |
Seq.single (Thm.instantiate ([],[(cterm_of thy x,cterm_of thy (fold_rev Term.abs params (Bound 0)))]) st) |
22787 | 153 |
| _ => error "fresh_fun_simp: Too many variables, please report." |
46219
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents:
44121
diff
changeset
|
154 |
end |
22729 | 155 |
in |
23091
c91530f18d9c
add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents:
23065
diff
changeset
|
156 |
((fn st => |
33034 | 157 |
let |
44121 | 158 |
val vars = Misc_Legacy.term_vars (prop_of st); |
22787 | 159 |
val params = Logic.strip_params (nth (prems_of st) (i-1)) |
33034 | 160 |
(* The tactics which solve the subgoals generated |
23091
c91530f18d9c
add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents:
23065
diff
changeset
|
161 |
by the conditionnal rewrite rule. *) |
33034 | 162 |
val post_rewrite_tacs = |
23054
d1bbe5afa279
change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents:
22792
diff
changeset
|
163 |
[rtac pt_name_inst, |
d1bbe5afa279
change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents:
22792
diff
changeset
|
164 |
rtac at_name_inst, |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51669
diff
changeset
|
165 |
TRY o SOLVED' (NominalPermeq.finite_guess_tac simp_ctxt), |
23054
d1bbe5afa279
change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents:
22792
diff
changeset
|
166 |
inst_fresh vars params THEN' |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51669
diff
changeset
|
167 |
(TRY o SOLVED' (NominalPermeq.fresh_guess_tac simp_ctxt)) THEN' |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51669
diff
changeset
|
168 |
(TRY o SOLVED' (asm_full_simp_tac simp_ctxt))] |
33034 | 169 |
in |
23094
f1df8d2da98a
fix a bug : the semantics of no_asm was the opposite
narboux
parents:
23091
diff
changeset
|
170 |
((if no_asm then no_tac else |
33034 | 171 |
(subst_inner_asm_tac ctxt fresh_fun_app' i THEN (RANGE post_rewrite_tacs i))) |
23091
c91530f18d9c
add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents:
23065
diff
changeset
|
172 |
ORELSE |
33034 | 173 |
(subst_inner_tac ctxt fresh_fun_app' i THEN (RANGE post_rewrite_tacs i))) st |
56230 | 174 |
end)) |
22729 | 175 |
end |
56230 | 176 |
end) |
22729 | 177 |