src/HOL/Nominal/nominal_fresh_fun.ML
author wenzelm
Tue, 03 Mar 2009 14:07:43 +0100
changeset 30211 556d1810cdad
parent 29265 5b4247055bd7
child 30280 eb98b49ef835
permissions -rw-r--r--
Thm.binding;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
     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
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
     3
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
     4
Provides a tactic to generate fresh names and
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
     5
a tactic to analyse instances of the fresh_fun.
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
     6
*)
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
     7
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
     8
(* First some functions that should be in the library *)
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
     9
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    10
(* A tactical which applies a list of int -> tactic to the          *) 
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    11
(* corresponding subgoals present after the application of          *) 
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    12
(* another tactic.                                                  *)
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    13
(*                                                                  *)
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    14
(*  T THENL [A,B,C] is equivalent to T THEN (C 3 THEN B 2 THEN A 1) *) 
22753
49d7818e6161 modify fresh_fun_simp to ease debugging
narboux
parents: 22729
diff changeset
    15
49d7818e6161 modify fresh_fun_simp to ease debugging
narboux
parents: 22729
diff changeset
    16
infix 1 THENL
22792
2443ae6dac7d fix sml compilation
narboux
parents: 22787
diff changeset
    17
fun tac THENL tacs =
22753
49d7818e6161 modify fresh_fun_simp to ease debugging
narboux
parents: 22729
diff changeset
    18
 tac THEN
49d7818e6161 modify fresh_fun_simp to ease debugging
narboux
parents: 22729
diff changeset
    19
  (EVERY (map  (fn (tac,i) => tac i) (rev tacs ~~ (length tacs downto 1))))
49d7818e6161 modify fresh_fun_simp to ease debugging
narboux
parents: 22729
diff changeset
    20
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    21
(* A tactic which only succeeds when the argument *)
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    22
(* tactic solves completely the specified subgoal *)
23054
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
    23
fun SOLVEI t = t THEN_ALL_NEW (fn i => no_tac);
23091
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
    24
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
    25
(* A version of TRY for int -> tactic *)
23054
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
    26
fun TRY' tac i =  TRY (tac i);
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
    27
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    28
fun gen_res_inst_tac_term instf tyinst tinst elim th i st =
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    29
  let
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    30
    val thy = theory_of_thm st;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    31
    val cgoal = nth (cprems_of st) (i - 1);
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    32
    val {maxidx, ...} = rep_cterm cgoal;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    33
    val j = maxidx + 1;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    34
    val tyinst' = map (apfst (Logic.incr_tvar j)) tyinst;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    35
    val ps = Logic.strip_params (term_of cgoal);
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    36
    val Ts = map snd ps;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    37
    val tinst' = map (fn (t, u) =>
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    38
      (head_of (Logic.incr_indexes (Ts, j) t),
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    39
       list_abs (ps, u))) tinst;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    40
    val th' = instf
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    41
      (map (pairself (ctyp_of thy)) tyinst')
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    42
      (map (pairself (cterm_of thy)) tinst')
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    43
      (Thm.lift_rule cgoal th)
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    44
  in
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    45
    compose_tac (elim, th', nprems_of th) i st
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    46
  end handle Subscript => Seq.empty;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    47
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    48
val res_inst_tac_term = 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    49
  gen_res_inst_tac_term (curry Thm.instantiate);
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    50
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    51
val res_inst_tac_term' = 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    52
  gen_res_inst_tac_term (K Drule.cterm_instantiate) [];
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    53
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    54
fun cut_inst_tac_term' tinst th =
27239
f2f42f9fa09d pervasive RuleInsts;
wenzelm
parents: 27187
diff changeset
    55
  res_inst_tac_term' tinst false (RuleInsts.make_elim_preserve th);
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    56
26337
44473c957672 auxiliary dynamic_thm(s) for fact lookup;
wenzelm
parents: 24558
diff changeset
    57
fun get_dyn_thm thy name atom_name =
26343
0dd2eab7b296 simplified get_thm(s): back to plain name argument;
wenzelm
parents: 26337
diff changeset
    58
  PureThy.get_thm thy name handle ERROR _ =>
27187
17b63e145986 use regular error function;
wenzelm
parents: 26343
diff changeset
    59
    error ("The atom type "^atom_name^" is not defined.");
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    60
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    61
(* End of function waiting to be in the library :o) *)
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    62
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    63
(* The theorems needed that are known at compile time. *)
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    64
val at_exists_fresh' = @{thm "at_exists_fresh'"};
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    65
val fresh_fun_app'   = @{thm "fresh_fun_app'"};
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    66
val fresh_prod       = @{thm "fresh_prod"};
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    67
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    68
(* A tactic to generate a name fresh for  all the free *) 
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
    69
(* variables and parameters of the goal                *)
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    70
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    71
fun generate_fresh_tac atom_name i thm =
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    72
 let 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    73
   val thy = theory_of_thm thm;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    74
(* the parsing function returns a qualified name, we get back the base name *)
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    75
   val atom_basename = Sign.base_name atom_name;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    76
   val goal = List.nth(prems_of thm, i-1);
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    77
   val ps = Logic.strip_params goal;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    78
   val Ts = rev (map snd ps);
24271
499608101177 avoid low-level tsig;
wenzelm
parents: 23368
diff changeset
    79
   fun is_of_fs_name T = Sign.of_sort thy (T, Sign.intern_sort thy ["fs_"^atom_basename]); 
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    80
(* rebuild de bruijn indices *)
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    81
   val bvs = map_index (Bound o fst) ps;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    82
(* select variables of the right class *)
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    83
   val vs = filter (fn t => is_of_fs_name (fastype_of1 (Ts, t)))
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 28397
diff changeset
    84
     (OldTerm.term_frees goal @ bvs);
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    85
(* build the tuple *)
23368
ad690b9bca1c generate_fresh works even if there is no free variable in the goal
narboux
parents: 23094
diff changeset
    86
   val s = (Library.foldr1 (fn (v, s) =>
28397
389c5e494605 handle _ should be avoided (spurious Interrupt will spoil the game);
wenzelm
parents: 27239
diff changeset
    87
     HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s) vs) handle _ => HOLogic.unit ;  (* FIXME avoid handle _ *)
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    88
   val fs_name_thm = get_dyn_thm thy ("fs_"^atom_basename^"1") atom_basename;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    89
   val at_name_inst_thm = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    90
   val exists_fresh' = at_name_inst_thm RS at_exists_fresh';
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    91
(* find the variable we want to instantiate *)
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 28397
diff changeset
    92
   val x = hd (OldTerm.term_vars (prop_of exists_fresh'));
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    93
 in 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    94
   (cut_inst_tac_term' [(x,s)] exists_fresh' 1 THEN
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    95
   rtac fs_name_thm 1 THEN
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    96
   etac exE 1) thm
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    97
  handle Empty  => all_tac thm (* if we collected no variables then we do nothing *)
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    98
  end;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
    99
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   100
fun get_inner_fresh_fun (Bound j) = NONE
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   101
  | get_inner_fresh_fun (v as Free _) = NONE 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   102
  | get_inner_fresh_fun (v as Var _)  = NONE
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   103
  | get_inner_fresh_fun (Const _) = NONE
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   104
  | get_inner_fresh_fun (Abs (_, _, t)) = get_inner_fresh_fun t 
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
   105
  | get_inner_fresh_fun (Const ("Nominal.fresh_fun",Type("fun",[Type ("fun",[Type (T,_),_]),_])) $ u) 
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
   106
                           = SOME T 
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   107
  | get_inner_fresh_fun (t $ u) = 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   108
     let val a = get_inner_fresh_fun u in
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   109
     if a = NONE then get_inner_fresh_fun t else a 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   110
     end;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   111
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
   112
(* This tactic generates a fresh name of the atom type *) 
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
   113
(* given by the innermost fresh_fun                    *)
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   114
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   115
fun generate_fresh_fun_tac i thm =
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   116
  let
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   117
    val goal = List.nth(prems_of thm, i-1);
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   118
    val atom_name_opt = get_inner_fresh_fun goal;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   119
  in
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   120
  case atom_name_opt of 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   121
    NONE => all_tac thm
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   122
  | SOME atom_name  => generate_fresh_tac atom_name i thm               
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   123
  end
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   124
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
   125
(* Two substitution tactics which looks for the innermost occurence in 
23091
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   126
   one assumption or in the conclusion *)
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   127
23065
ab28e8398670 search bottom up to get the inner fresh fun
narboux
parents: 23054
diff changeset
   128
val search_fun     = curry (Seq.flat o (uncurry EqSubst.searchf_bt_unify_valid));
ab28e8398670 search bottom up to get the inner fresh fun
narboux
parents: 23054
diff changeset
   129
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
   130
23091
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   131
fun subst_inner_tac           ctx = EqSubst.eqsubst_tac' ctx search_fun;
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   132
fun subst_inner_asm_tac_aux i ctx = EqSubst.eqsubst_asm_tac' ctx 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
   133
23091
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   134
(* A tactic to substitute in the first assumption 
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   135
   which contains an occurence. *)
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   136
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
   137
fun subst_inner_asm_tac ctx th =  
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
   138
   curry (curry (FIRST' (map uncurry (map uncurry (map subst_inner_asm_tac_aux 
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
   139
                                                             (1 upto Thm.nprems_of th)))))) ctx th;
23091
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   140
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   141
fun fresh_fun_tac no_asm i thm = 
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   142
  (* Find the variable we instantiate *)
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   143
  let
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   144
    val thy = theory_of_thm thm;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   145
    val ctx = Context.init_proof thy;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   146
    val ss = simpset_of thy;
26343
0dd2eab7b296 simplified get_thm(s): back to plain name argument;
wenzelm
parents: 26337
diff changeset
   147
    val abs_fresh = PureThy.get_thms thy "abs_fresh";
0dd2eab7b296 simplified get_thm(s): back to plain name argument;
wenzelm
parents: 26337
diff changeset
   148
    val fresh_perm_app = PureThy.get_thms thy "fresh_perm_app";
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   149
    val ss' = ss addsimps fresh_prod::abs_fresh;
23054
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
   150
    val ss'' = ss' addsimps fresh_perm_app;
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 28397
diff changeset
   151
    val x = hd (tl (OldTerm.term_vars (prop_of exI)));
22787
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   152
    val goal = nth (prems_of thm) (i-1);
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   153
    val atom_name_opt = get_inner_fresh_fun goal;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   154
    val n = List.length (Logic.strip_params goal);
24558
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
   155
    (* Here we rely on the fact that the variable introduced by generate_fresh_tac *)
419f7cde7f59 some cleaning up
urbanc
parents: 24271
diff changeset
   156
    (* is the last one in the list, the inner one *)
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   157
  in
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   158
  case atom_name_opt of 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   159
    NONE => all_tac thm
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   160
  | SOME atom_name  =>    
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   161
  let 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   162
    val atom_basename = Sign.base_name atom_name;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   163
    val pt_name_inst = get_dyn_thm thy ("pt_"^atom_basename^"_inst") atom_basename;
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   164
    val at_name_inst = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
22787
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   165
    fun inst_fresh vars params i st =
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 28397
diff changeset
   166
   let val vars' = OldTerm.term_vars (prop_of st);
22787
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   167
       val thy = theory_of_thm st;
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   168
   in case vars' \\ vars of 
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   169
     [x] => Seq.single (Thm.instantiate ([],[(cterm_of thy x,cterm_of thy (list_abs (params,Bound 0)))]) st)
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   170
    | _ => error "fresh_fun_simp: Too many variables, please report."
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   171
  end
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   172
  in
23091
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   173
  ((fn st =>
22787
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   174
  let 
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 28397
diff changeset
   175
    val vars = OldTerm.term_vars (prop_of st);
22787
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   176
    val params = Logic.strip_params (nth (prems_of st) (i-1))
23091
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   177
    (* The tactics which solve the subgoals generated 
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   178
       by the conditionnal rewrite rule. *)
23054
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
   179
    val post_rewrite_tacs =  
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
   180
          [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
   181
           rtac at_name_inst,
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
   182
           TRY' (SOLVEI (NominalPermeq.finite_guess_tac ss'')),
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
   183
           inst_fresh vars params THEN'
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
   184
           (TRY' (SOLVEI (NominalPermeq.fresh_guess_tac ss''))) THEN'
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
   185
           (TRY' (SOLVEI (asm_full_simp_tac ss'')))] 
d1bbe5afa279 change fresh_fun_simp to treat occurences in assumptions and try to solve the generated subgoals
narboux
parents: 22792
diff changeset
   186
  in 
23094
f1df8d2da98a fix a bug : the semantics of no_asm was the opposite
narboux
parents: 23091
diff changeset
   187
   ((if no_asm then no_tac else
f1df8d2da98a fix a bug : the semantics of no_asm was the opposite
narboux
parents: 23091
diff changeset
   188
    (subst_inner_asm_tac ctx fresh_fun_app' i THENL post_rewrite_tacs)) 
23091
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   189
    ORELSE
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   190
    (subst_inner_tac     ctx fresh_fun_app' i THENL post_rewrite_tacs)) st
22787
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   191
  end)) thm
85e7e32e6004 update fresh_fun_simp for debugging purposes
narboux
parents: 22774
diff changeset
   192
  
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   193
  end
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   194
  end
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   195
23091
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   196
(* syntax for options, given "(no_asm)" will give back true, without
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   197
   gives back false *)
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   198
val options_syntax =
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   199
    (Args.parens (Args.$$$ "no_asm") >> (K true)) ||
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   200
     (Scan.succeed false);
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   201
22729
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   202
val setup_generate_fresh =
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   203
  Method.goal_args_ctxt' Args.tyname (fn ctxt => generate_fresh_tac) 
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   204
69ef734825c5 add a tactic to generate fresh names
narboux
parents:
diff changeset
   205
val setup_fresh_fun_simp =
23091
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   206
  Method.simple_args options_syntax 
c91530f18d9c add an option in fresh_fun_simp to prevent rewriting in assumptions
narboux
parents: 23065
diff changeset
   207
  (fn b => fn _ => Method.SIMPLE_METHOD (fresh_fun_tac b 1))