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