src/HOL/Nominal/nominal_fresh_fun.ML
author wenzelm
Wed Jan 13 00:08:56 2010 +0100 (2010-01-13)
changeset 34885 6587c24ef6d8
parent 33040 cffdb7b28498
child 35360 df2b2168e43a
permissions -rw-r--r--
added SOLVED' -- a more direct version of THEN_ALL_NEW (K no_tac) -- strictly speaking it does not even depend on subgoal addressing, but it would be too confusing without it;
     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 fun gen_res_inst_tac_term instf tyinst tinst elim th i st =
    11   let
    12     val thy = theory_of_thm st;
    13     val cgoal = nth (cprems_of st) (i - 1);
    14     val {maxidx, ...} = rep_cterm cgoal;
    15     val j = maxidx + 1;
    16     val tyinst' = map (apfst (Logic.incr_tvar j)) tyinst;
    17     val ps = Logic.strip_params (term_of cgoal);
    18     val Ts = map snd ps;
    19     val tinst' = map (fn (t, u) =>
    20       (head_of (Logic.incr_indexes (Ts, j) t),
    21        list_abs (ps, u))) tinst;
    22     val th' = instf
    23       (map (pairself (ctyp_of thy)) tyinst')
    24       (map (pairself (cterm_of thy)) tinst')
    25       (Thm.lift_rule cgoal th)
    26   in
    27     compose_tac (elim, th', nprems_of th) i st
    28   end handle Subscript => Seq.empty;
    29 
    30 val res_inst_tac_term =
    31   gen_res_inst_tac_term (curry Thm.instantiate);
    32 
    33 val res_inst_tac_term' =
    34   gen_res_inst_tac_term (K Drule.cterm_instantiate) [];
    35 
    36 fun cut_inst_tac_term' tinst th =
    37   res_inst_tac_term' tinst false (RuleInsts.make_elim_preserve th);
    38 
    39 fun get_dyn_thm thy name atom_name =
    40   PureThy.get_thm thy name handle ERROR _ =>
    41     error ("The atom type "^atom_name^" is not defined.");
    42 
    43 (* End of function waiting to be in the library :o) *)
    44 
    45 (* The theorems needed that are known at compile time. *)
    46 val at_exists_fresh' = @{thm "at_exists_fresh'"};
    47 val fresh_fun_app'   = @{thm "fresh_fun_app'"};
    48 val fresh_prod       = @{thm "fresh_prod"};
    49 
    50 (* A tactic to generate a name fresh for  all the free *)
    51 (* variables and parameters of the goal                *)
    52 
    53 fun generate_fresh_tac atom_name i thm =
    54  let
    55    val thy = theory_of_thm thm;
    56 (* the parsing function returns a qualified name, we get back the base name *)
    57    val atom_basename = Long_Name.base_name atom_name;
    58    val goal = List.nth(prems_of thm, i-1);
    59    val ps = Logic.strip_params goal;
    60    val Ts = rev (map snd ps);
    61    fun is_of_fs_name T = Sign.of_sort thy (T, Sign.intern_sort thy ["fs_"^atom_basename]);
    62 (* rebuild de bruijn indices *)
    63    val bvs = map_index (Bound o fst) ps;
    64 (* select variables of the right class *)
    65    val vs = filter (fn t => is_of_fs_name (fastype_of1 (Ts, t)))
    66      (OldTerm.term_frees goal @ bvs);
    67 (* build the tuple *)
    68    val s = (Library.foldr1 (fn (v, s) =>
    69      HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s) vs) handle _ => HOLogic.unit ;  (* FIXME avoid handle _ *)
    70    val fs_name_thm = get_dyn_thm thy ("fs_"^atom_basename^"1") atom_basename;
    71    val at_name_inst_thm = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
    72    val exists_fresh' = at_name_inst_thm RS at_exists_fresh';
    73 (* find the variable we want to instantiate *)
    74    val x = hd (OldTerm.term_vars (prop_of exists_fresh'));
    75  in
    76    (cut_inst_tac_term' [(x,s)] exists_fresh' 1 THEN
    77    rtac fs_name_thm 1 THEN
    78    etac exE 1) thm
    79   handle Empty  => all_tac thm (* if we collected no variables then we do nothing *)
    80   end;
    81 
    82 fun get_inner_fresh_fun (Bound j) = NONE
    83   | get_inner_fresh_fun (v as Free _) = NONE
    84   | get_inner_fresh_fun (v as Var _)  = NONE
    85   | get_inner_fresh_fun (Const _) = NONE
    86   | get_inner_fresh_fun (Abs (_, _, t)) = get_inner_fresh_fun t
    87   | get_inner_fresh_fun (Const ("Nominal.fresh_fun",Type("fun",[Type ("fun",[Type (T,_),_]),_])) $ u)
    88                            = SOME T
    89   | get_inner_fresh_fun (t $ u) =
    90      let val a = get_inner_fresh_fun u in
    91      if a = NONE then get_inner_fresh_fun t else a
    92      end;
    93 
    94 (* This tactic generates a fresh name of the atom type *)
    95 (* given by the innermost fresh_fun                    *)
    96 
    97 fun generate_fresh_fun_tac i thm =
    98   let
    99     val goal = List.nth(prems_of thm, i-1);
   100     val atom_name_opt = get_inner_fresh_fun goal;
   101   in
   102   case atom_name_opt of
   103     NONE => all_tac thm
   104   | SOME atom_name  => generate_fresh_tac atom_name i thm
   105   end
   106 
   107 (* Two substitution tactics which looks for the innermost occurence in
   108    one assumption or in the conclusion *)
   109 
   110 val search_fun = curry (Seq.flat o uncurry EqSubst.searchf_bt_unify_valid);
   111 val search_fun_asm = EqSubst.skip_first_asm_occs_search EqSubst.searchf_bt_unify_valid;
   112 
   113 fun subst_inner_tac ctxt = EqSubst.eqsubst_tac' ctxt search_fun;
   114 fun subst_inner_asm_tac_aux i ctxt = EqSubst.eqsubst_asm_tac' ctxt search_fun_asm i;
   115 
   116 (* A tactic to substitute in the first assumption
   117    which contains an occurence. *)
   118 
   119 fun subst_inner_asm_tac ctxt th =
   120   curry (curry (FIRST' (map uncurry (map uncurry (map subst_inner_asm_tac_aux
   121             (1 upto Thm.nprems_of th)))))) ctxt th;
   122 
   123 fun fresh_fun_tac no_asm i thm =
   124   (* Find the variable we instantiate *)
   125   let
   126     val thy = theory_of_thm thm;
   127     val ctxt = ProofContext.init thy;
   128     val ss = global_simpset_of thy;
   129     val abs_fresh = PureThy.get_thms thy "abs_fresh";
   130     val fresh_perm_app = PureThy.get_thms thy "fresh_perm_app";
   131     val ss' = ss addsimps fresh_prod::abs_fresh;
   132     val ss'' = ss' addsimps fresh_perm_app;
   133     val x = hd (tl (OldTerm.term_vars (prop_of exI)));
   134     val goal = nth (prems_of thm) (i-1);
   135     val atom_name_opt = get_inner_fresh_fun goal;
   136     val n = length (Logic.strip_params goal);
   137     (* Here we rely on the fact that the variable introduced by generate_fresh_tac *)
   138     (* is the last one in the list, the inner one *)
   139   in
   140   case atom_name_opt of
   141     NONE => all_tac thm
   142   | SOME atom_name =>
   143   let
   144     val atom_basename = Long_Name.base_name atom_name;
   145     val pt_name_inst = get_dyn_thm thy ("pt_"^atom_basename^"_inst") atom_basename;
   146     val at_name_inst = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
   147     fun inst_fresh vars params i st =
   148    let val vars' = OldTerm.term_vars (prop_of st);
   149        val thy = theory_of_thm st;
   150    in case subtract (op =) vars vars' of
   151      [x] => Seq.single (Thm.instantiate ([],[(cterm_of thy x,cterm_of thy (list_abs (params,Bound 0)))]) st)
   152     | _ => error "fresh_fun_simp: Too many variables, please report."
   153   end
   154   in
   155   ((fn st =>
   156   let
   157     val vars = OldTerm.term_vars (prop_of st);
   158     val params = Logic.strip_params (nth (prems_of st) (i-1))
   159     (* The tactics which solve the subgoals generated
   160        by the conditionnal rewrite rule. *)
   161     val post_rewrite_tacs =
   162           [rtac pt_name_inst,
   163            rtac at_name_inst,
   164            TRY o SOLVED' (NominalPermeq.finite_guess_tac ss''),
   165            inst_fresh vars params THEN'
   166            (TRY o SOLVED' (NominalPermeq.fresh_guess_tac ss'')) THEN'
   167            (TRY o SOLVED' (asm_full_simp_tac ss''))]
   168   in
   169    ((if no_asm then no_tac else
   170     (subst_inner_asm_tac ctxt fresh_fun_app' i THEN (RANGE post_rewrite_tacs i)))
   171     ORELSE
   172     (subst_inner_tac ctxt fresh_fun_app' i THEN (RANGE post_rewrite_tacs i))) st
   173   end)) thm
   174 
   175   end
   176   end
   177 
   178 (* syntax for options, given "(no_asm)" will give back true, without
   179    gives back false *)
   180 val options_syntax =
   181     (Args.parens (Args.$$$ "no_asm") >> (K true)) ||
   182      (Scan.succeed false);
   183 
   184 fun setup_generate_fresh x =
   185   (Args.goal_spec -- Args.tyname >>
   186     (fn (quant, s) => K (SIMPLE_METHOD'' quant (generate_fresh_tac s)))) x;
   187 
   188 fun setup_fresh_fun_simp x =
   189   (Scan.lift options_syntax >> (fn b => K (SIMPLE_METHOD' (fresh_fun_tac b)))) x;
   190