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