src/HOL/Nominal/nominal_fresh_fun.ML
author narboux
Thu Apr 19 16:27:53 2007 +0200 (2007-04-19)
changeset 22729 69ef734825c5
child 22753 49d7818e6161
permissions -rw-r--r--
add a tactic to generate fresh names
narboux@22729
     1
(*  Title:      HOL/Nominal/nominal_fresh_fun.ML
narboux@22729
     2
    ID:         $Id$
narboux@22729
     3
    Authors:    Stefan Berghofer, Julien Narboux, TU Muenchen
narboux@22729
     4
narboux@22729
     5
A tactic to generate fresh names.
narboux@22729
     6
A tactic to get rid of the fresh_fun.
narboux@22729
     7
*)
narboux@22729
     8
narboux@22729
     9
(* First some functions that could be
narboux@22729
    10
 in the library *)
narboux@22729
    11
narboux@22729
    12
fun gen_res_inst_tac_term instf tyinst tinst elim th i st =
narboux@22729
    13
  let
narboux@22729
    14
    val thy = theory_of_thm st;
narboux@22729
    15
    val cgoal = nth (cprems_of st) (i - 1);
narboux@22729
    16
    val {maxidx, ...} = rep_cterm cgoal;
narboux@22729
    17
    val j = maxidx + 1;
narboux@22729
    18
    val tyinst' = map (apfst (Logic.incr_tvar j)) tyinst;
narboux@22729
    19
    val ps = Logic.strip_params (term_of cgoal);
narboux@22729
    20
    val Ts = map snd ps;
narboux@22729
    21
    val tinst' = map (fn (t, u) =>
narboux@22729
    22
      (head_of (Logic.incr_indexes (Ts, j) t),
narboux@22729
    23
       list_abs (ps, u))) tinst;
narboux@22729
    24
    val th' = instf
narboux@22729
    25
      (map (pairself (ctyp_of thy)) tyinst')
narboux@22729
    26
      (map (pairself (cterm_of thy)) tinst')
narboux@22729
    27
      (Thm.lift_rule cgoal th)
narboux@22729
    28
  in
narboux@22729
    29
    compose_tac (elim, th', nprems_of th) i st
narboux@22729
    30
  end handle Subscript => Seq.empty;
narboux@22729
    31
narboux@22729
    32
val res_inst_tac_term = 
narboux@22729
    33
  gen_res_inst_tac_term (curry Thm.instantiate);
narboux@22729
    34
narboux@22729
    35
val res_inst_tac_term' = 
narboux@22729
    36
  gen_res_inst_tac_term (K Drule.cterm_instantiate) [];
narboux@22729
    37
narboux@22729
    38
fun cut_inst_tac_term' tinst th =
narboux@22729
    39
  res_inst_tac_term' tinst false (Tactic.make_elim_preserve th);
narboux@22729
    40
narboux@22729
    41
fun get_dyn_thm thy name atom_name = (PureThy.get_thm thy (Name name)) handle _ => raise ERROR ("The atom type "^atom_name^" is not defined."); 
narboux@22729
    42
narboux@22729
    43
(* End of function waiting to be
narboux@22729
    44
 in the library *)
narboux@22729
    45
narboux@22729
    46
(* The theorems needed that are known at
narboux@22729
    47
compile time. *)
narboux@22729
    48
narboux@22729
    49
val at_exists_fresh' = thm "at_exists_fresh'";
narboux@22729
    50
val fresh_fun_app = thm "fresh_fun_app";
narboux@22729
    51
val fresh_prod = thm "fresh_prod";
narboux@22729
    52
narboux@22729
    53
(* A tactic to generate a name fresh for 
narboux@22729
    54
all the free variables and parameters of the goal *)
narboux@22729
    55
narboux@22729
    56
fun generate_fresh_tac atom_name i thm =
narboux@22729
    57
 let 
narboux@22729
    58
   val thy = theory_of_thm thm;
narboux@22729
    59
(* the parsing function returns a qualified name, we get back the base name *)
narboux@22729
    60
   val atom_basename = Sign.base_name atom_name;
narboux@22729
    61
   val goal = List.nth(prems_of thm, i-1);
narboux@22729
    62
   val ps = Logic.strip_params goal;
narboux@22729
    63
   val Ts = rev (map snd ps);
narboux@22729
    64
   fun is_of_fs_name T = Type.of_sort (Sign.tsig_of thy)
narboux@22729
    65
     (T, Sign.intern_sort thy ["fs_"^atom_basename]); 
narboux@22729
    66
(* rebuild de bruijn indices *)
narboux@22729
    67
   val bvs = map_index (Bound o fst) ps;
narboux@22729
    68
(* select variables of the right class *)
narboux@22729
    69
   val vs = filter (fn t => is_of_fs_name (fastype_of1 (Ts, t)))
narboux@22729
    70
     (term_frees goal @ bvs);
narboux@22729
    71
(* build the tuple *)
narboux@22729
    72
   val s = Library.foldr1 (fn (v, s) =>
narboux@22729
    73
     HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s)
narboux@22729
    74
       vs;
narboux@22729
    75
   val fs_name_thm = get_dyn_thm thy ("fs_"^atom_basename^"1") atom_basename;
narboux@22729
    76
   val at_name_inst_thm = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
narboux@22729
    77
   val exists_fresh' = at_name_inst_thm RS at_exists_fresh';
narboux@22729
    78
(* find the variable we want to instantiate *)
narboux@22729
    79
   val x = hd (term_vars (prop_of exists_fresh'));
narboux@22729
    80
 in 
narboux@22729
    81
   (cut_inst_tac_term' [(x,s)] exists_fresh' 1 THEN
narboux@22729
    82
   rtac fs_name_thm 1 THEN
narboux@22729
    83
   etac exE 1) thm
narboux@22729
    84
  handle Empty  => all_tac thm (* if we collected no variables then we do nothing *)
narboux@22729
    85
  end;
narboux@22729
    86
narboux@22729
    87
fun get_inner_fresh_fun (Bound j) = NONE
narboux@22729
    88
  | get_inner_fresh_fun (v as Free _) = NONE 
narboux@22729
    89
  | get_inner_fresh_fun (v as Var _)  = NONE
narboux@22729
    90
  | get_inner_fresh_fun (Const _) = NONE
narboux@22729
    91
  | get_inner_fresh_fun (Abs (_, _, t)) = get_inner_fresh_fun t 
narboux@22729
    92
  | get_inner_fresh_fun (Const ("Nominal.fresh_fun",Type("fun",[Type ("fun",[Type (T,_),_]),_])) $ u) = SOME T 
narboux@22729
    93
  | get_inner_fresh_fun (t $ u) = 
narboux@22729
    94
     let val a = get_inner_fresh_fun u in
narboux@22729
    95
     if a = NONE then get_inner_fresh_fun t else a 
narboux@22729
    96
     end;
narboux@22729
    97
narboux@22729
    98
(* This tactic generates a fresh name 
narboux@22729
    99
of the atom type given by the inner most fresh_fun *)
narboux@22729
   100
narboux@22729
   101
fun generate_fresh_fun_tac i thm =
narboux@22729
   102
  let
narboux@22729
   103
    val goal = List.nth(prems_of thm, i-1);
narboux@22729
   104
    val atom_name_opt = get_inner_fresh_fun goal;
narboux@22729
   105
  in
narboux@22729
   106
  case atom_name_opt of 
narboux@22729
   107
    NONE => all_tac thm
narboux@22729
   108
  | SOME atom_name  => generate_fresh_tac atom_name i thm               
narboux@22729
   109
  end
narboux@22729
   110
narboux@22729
   111
(* A substitution tactic, FIXME : change the search function to get the inner occurence of fresh_fun (bottom -> up search ) *)
narboux@22729
   112
narboux@22729
   113
fun subst_outer_tac ctx = EqSubst.eqsubst_tac' ctx (curry (Seq.flat o (uncurry EqSubst.searchf_lr_unify_valid)));
narboux@22729
   114
narboux@22729
   115
fun fresh_fun_tac i thm = 
narboux@22729
   116
  (* Find the variable we instantiate *)
narboux@22729
   117
  let
narboux@22729
   118
    val thy = theory_of_thm thm;
narboux@22729
   119
    val ctx = Context.init_proof thy;
narboux@22729
   120
    val ss = simpset_of thy;
narboux@22729
   121
    val abs_fresh = PureThy.get_thms thy (Name "abs_fresh");
narboux@22729
   122
    val ss' = ss addsimps fresh_prod::abs_fresh;
narboux@22729
   123
    val x = hd (tl (term_vars (prop_of exI)));
narboux@22729
   124
    val goal = List.nth(prems_of thm, i-1);
narboux@22729
   125
    val atom_name_opt = get_inner_fresh_fun goal;
narboux@22729
   126
    val n = List.length (Logic.strip_params goal);
narboux@22729
   127
    (* Here we rely on the fact that the variable introduced by generate_fresh_tac is the last one in the list, the inner one *)
narboux@22729
   128
  in
narboux@22729
   129
  case atom_name_opt of 
narboux@22729
   130
    NONE => all_tac thm
narboux@22729
   131
  | SOME atom_name  =>    
narboux@22729
   132
  let 
narboux@22729
   133
    val atom_basename = Sign.base_name atom_name;
narboux@22729
   134
    val pt_name_inst = get_dyn_thm thy ("pt_"^atom_basename^"_inst") atom_basename;
narboux@22729
   135
    val at_name_inst = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
narboux@22729
   136
  in
narboux@22729
   137
  (generate_fresh_tac atom_name i               THEN
narboux@22729
   138
   subst_outer_tac ctx fresh_fun_app i          THEN
narboux@22729
   139
   rtac pt_name_inst i                          THEN
narboux@22729
   140
   rtac at_name_inst i                          THEN
narboux@22729
   141
   NominalPermeq.finite_guess_tac HOL_ss i      THEN
narboux@22729
   142
   auto_tac (HOL_cs , HOL_ss)                   THEN
narboux@22729
   143
   NominalPermeq.fresh_guess_tac HOL_ss (i+1)   THEN
narboux@22729
   144
   res_inst_tac_term' [(x,Bound n)] false exI i THEN
narboux@22729
   145
   asm_full_simp_tac ss' i                      THEN
narboux@22729
   146
   NominalPermeq.fresh_guess_tac HOL_ss i) thm 
narboux@22729
   147
  end
narboux@22729
   148
  end
narboux@22729
   149
narboux@22729
   150
val setup_generate_fresh =
narboux@22729
   151
  Method.goal_args_ctxt' Args.tyname (fn ctxt => generate_fresh_tac) 
narboux@22729
   152
narboux@22729
   153
val setup_fresh_fun_simp =
narboux@22729
   154
  Method.no_args (Method.SIMPLE_METHOD (fresh_fun_tac 1))