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