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