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