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