src/HOL/Nominal/nominal_fresh_fun.ML
author wenzelm
Fri Mar 06 23:52:57 2015 +0100 (2015-03-06)
changeset 59635 025f70f35daf
parent 59621 291934bac95e
child 59787 6e2a20486897
permissions -rw-r--r--
clarified context;
     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 ctxt instf tyinst tinst elim th i st =
    14   let
    15     val cgoal = nth (cprems_of st) (i - 1);
    16     val maxidx = Thm.maxidx_of_cterm cgoal;
    17     val j = maxidx + 1;
    18     val tyinst' = map (apfst (Logic.incr_tvar j)) tyinst;
    19     val ps = Logic.strip_params (Thm.term_of cgoal);
    20     val Ts = map snd ps;
    21     val tinst' = map (fn (t, u) =>
    22       (head_of (Logic.incr_indexes (Ts, j) t),
    23        fold_rev Term.abs ps u)) tinst;
    24     val th' = instf
    25       (map (apply2 (Thm.ctyp_of ctxt)) tyinst')
    26       (map (apply2 (Thm.cterm_of ctxt)) tinst')
    27       (Thm.lift_rule cgoal th)
    28   in
    29     compose_tac ctxt (elim, th', Thm.nprems_of th) i st
    30   end handle General.Subscript => Seq.empty;
    31 (* FIXME proper SUBGOAL/CSUBGOAL instead of cprems_of etc. *)
    32 
    33 fun res_inst_tac_term ctxt =
    34   gen_res_inst_tac_term ctxt (curry Thm.instantiate);
    35 
    36 fun res_inst_tac_term' ctxt =
    37   gen_res_inst_tac_term ctxt (K Drule.cterm_instantiate) [];
    38 
    39 fun cut_inst_tac_term' ctxt tinst th =
    40   res_inst_tac_term' ctxt tinst false (Rule_Insts.make_elim_preserve ctxt th);
    41 
    42 fun get_dyn_thm thy name atom_name =
    43   Global_Theory.get_thm thy name handle ERROR _ =>
    44     error ("The atom type "^atom_name^" is not defined.");
    45 
    46 (* The theorems needed that are known at compile time. *)
    47 val at_exists_fresh' = @{thm "at_exists_fresh'"};
    48 val fresh_fun_app'   = @{thm "fresh_fun_app'"};
    49 val fresh_prod       = @{thm "fresh_prod"};
    50 
    51 (* A tactic to generate a name fresh for  all the free *)
    52 (* variables and parameters of the goal                *)
    53 
    54 fun generate_fresh_tac ctxt atom_name = SUBGOAL (fn (goal, _) =>
    55  let
    56    val thy = Proof_Context.theory_of ctxt;
    57 (* the parsing function returns a qualified name, we get back the base name *)
    58    val atom_basename = Long_Name.base_name atom_name;
    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_class 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      (Misc_Legacy.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)
    70      handle TERM _ => HOLogic.unit;
    71    val fs_name_thm = get_dyn_thm thy ("fs_"^atom_basename^"1") atom_basename;
    72    val at_name_inst_thm = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
    73    val exists_fresh' = at_name_inst_thm RS at_exists_fresh';
    74 (* find the variable we want to instantiate *)
    75    val x = hd (Misc_Legacy.term_vars (Thm.prop_of exists_fresh'));
    76  in
    77    fn st =>
    78    (cut_inst_tac_term' ctxt [(x,s)] exists_fresh' 1 THEN
    79    rtac fs_name_thm 1 THEN
    80    etac exE 1) st
    81   handle List.Empty  => all_tac st (* if we collected no variables then we do nothing *)
    82  end) 1;
    83 
    84 fun get_inner_fresh_fun (Bound j) = NONE
    85   | get_inner_fresh_fun (v as Free _) = NONE
    86   | get_inner_fresh_fun (v as Var _)  = NONE
    87   | get_inner_fresh_fun (Const _) = NONE
    88   | get_inner_fresh_fun (Abs (_, _, t)) = get_inner_fresh_fun t
    89   | get_inner_fresh_fun (Const (@{const_name Nominal.fresh_fun},
    90       Type(@{type_name fun},[Type (@{type_name fun},[Type (T,_),_]),_])) $ u) = SOME T
    91   | get_inner_fresh_fun (t $ u) =
    92      let val a = get_inner_fresh_fun u in
    93      if a = NONE then get_inner_fresh_fun t else a
    94      end;
    95 
    96 (* This tactic generates a fresh name of the atom type *)
    97 (* given by the innermost fresh_fun                    *)
    98 
    99 fun generate_fresh_fun_tac ctxt = SUBGOAL (fn (goal, _) =>
   100   let
   101     val atom_name_opt = get_inner_fresh_fun goal;
   102   in
   103   case atom_name_opt of
   104     NONE => all_tac
   105   | SOME atom_name  => generate_fresh_tac ctxt atom_name
   106   end) 1;
   107 
   108 (* Two substitution tactics which looks for the innermost occurrence in
   109    one assumption or in the conclusion *)
   110 
   111 val search_fun = curry (Seq.flat o uncurry EqSubst.searchf_bt_unify_valid);
   112 val search_fun_asm = EqSubst.skip_first_asm_occs_search EqSubst.searchf_bt_unify_valid;
   113 
   114 fun subst_inner_tac ctxt = EqSubst.eqsubst_tac' ctxt search_fun;
   115 fun subst_inner_asm_tac_aux i ctxt = EqSubst.eqsubst_asm_tac' ctxt search_fun_asm i;
   116 
   117 (* A tactic to substitute in the first assumption
   118    which contains an occurrence. *)
   119 
   120 fun subst_inner_asm_tac ctxt th =
   121   curry (curry (FIRST' (map uncurry (map uncurry (map subst_inner_asm_tac_aux
   122             (1 upto Thm.nprems_of th)))))) ctxt th;
   123 
   124 fun fresh_fun_tac ctxt no_asm = SUBGOAL (fn (goal, i) =>
   125   (* Find the variable we instantiate *)
   126   let
   127     val thy = Proof_Context.theory_of ctxt;
   128     val abs_fresh = Global_Theory.get_thms thy "abs_fresh";
   129     val fresh_perm_app = Global_Theory.get_thms thy "fresh_perm_app";
   130     val simp_ctxt =
   131       ctxt addsimps (fresh_prod :: abs_fresh)
   132       addsimps fresh_perm_app;
   133     val x = hd (tl (Misc_Legacy.term_vars (Thm.prop_of exI)));
   134     val atom_name_opt = get_inner_fresh_fun goal;
   135     val n = length (Logic.strip_params goal);
   136     (* Here we rely on the fact that the variable introduced by generate_fresh_tac *)
   137     (* is the last one in the list, the inner one *)
   138   in
   139   case atom_name_opt of
   140     NONE => all_tac
   141   | SOME atom_name =>
   142   let
   143     val atom_basename = Long_Name.base_name atom_name;
   144     val pt_name_inst = get_dyn_thm thy ("pt_"^atom_basename^"_inst") atom_basename;
   145     val at_name_inst = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
   146     fun inst_fresh vars params i st =
   147    let val vars' = Misc_Legacy.term_vars (Thm.prop_of st);
   148        val thy = Thm.theory_of_thm st;
   149    in case subtract (op =) vars vars' of
   150      [x] =>
   151       Seq.single (Thm.instantiate ([], [(Thm.global_cterm_of thy x, Thm.global_cterm_of thy (fold_rev Term.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 = Misc_Legacy.term_vars (Thm.prop_of st);
   158     val params = Logic.strip_params (nth (Thm.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 simp_ctxt),
   165            inst_fresh vars params THEN'
   166            (TRY o SOLVED' (NominalPermeq.fresh_guess_tac simp_ctxt)) THEN'
   167            (TRY o SOLVED' (asm_full_simp_tac simp_ctxt))]
   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))
   174   end
   175   end)
   176