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