src/HOL/Nominal/nominal_fresh_fun.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (23 months ago)
changeset 66695 91500c024c7f
parent 60792 722cb21ab680
child 69597 ff784d5a5bfb
permissions -rw-r--r--
tuned;
     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 (apsnd (Thm.ctyp_of ctxt)) tyinst')
    26       (map (apsnd (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 (fn instT => fn inst =>
    35     Thm.instantiate
    36      (map (apfst dest_TVar) instT,
    37       map (apfst dest_Var) inst));
    38 
    39 fun res_inst_tac_term' ctxt =
    40   gen_res_inst_tac_term ctxt
    41     (fn _ => fn inst => infer_instantiate ctxt (map (apfst (#1 o dest_Var)) inst)) [];
    42 
    43 fun cut_inst_tac_term' ctxt tinst th =
    44   res_inst_tac_term' ctxt tinst false (Rule_Insts.make_elim_preserve ctxt th);
    45 
    46 fun get_dyn_thm thy name atom_name =
    47   Global_Theory.get_thm thy name handle ERROR _ =>
    48     error ("The atom type "^atom_name^" is not defined.");
    49 
    50 (* The theorems needed that are known at compile time. *)
    51 val at_exists_fresh' = @{thm "at_exists_fresh'"};
    52 val fresh_fun_app'   = @{thm "fresh_fun_app'"};
    53 val fresh_prod       = @{thm "fresh_prod"};
    54 
    55 (* A tactic to generate a name fresh for  all the free *)
    56 (* variables and parameters of the goal                *)
    57 
    58 fun generate_fresh_tac ctxt atom_name = SUBGOAL (fn (goal, _) =>
    59  let
    60    val thy = Proof_Context.theory_of ctxt;
    61 (* the parsing function returns a qualified name, we get back the base name *)
    62    val atom_basename = Long_Name.base_name atom_name;
    63    val ps = Logic.strip_params goal;
    64    val Ts = rev (map snd ps);
    65    fun is_of_fs_name T = Sign.of_sort thy (T, [Sign.intern_class thy ("fs_"^atom_basename)]);
    66 (* rebuild de bruijn indices *)
    67    val bvs = map_index (Bound o fst) ps;
    68 (* select variables of the right class *)
    69    val vs = filter (fn t => is_of_fs_name (fastype_of1 (Ts, t)))
    70      (Misc_Legacy.term_frees goal @ bvs);
    71 (* build the tuple *)
    72    val s = (Library.foldr1 (fn (v, s) =>
    73        HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s) vs)
    74      handle TERM _ => HOLogic.unit;
    75    val fs_name_thm = get_dyn_thm thy ("fs_"^atom_basename^"1") atom_basename;
    76    val at_name_inst_thm = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
    77    val exists_fresh' = at_name_inst_thm RS at_exists_fresh';
    78 (* find the variable we want to instantiate *)
    79    val x = hd (Misc_Legacy.term_vars (Thm.prop_of exists_fresh'));
    80  in
    81    fn st =>
    82    (cut_inst_tac_term' ctxt [(x,s)] exists_fresh' 1 THEN
    83    resolve_tac ctxt [fs_name_thm] 1 THEN
    84    eresolve_tac ctxt [exE] 1) st
    85   handle List.Empty  => all_tac st (* if we collected no variables then we do nothing *)
    86  end) 1;
    87 
    88 fun get_inner_fresh_fun (Bound j) = NONE
    89   | get_inner_fresh_fun (v as Free _) = NONE
    90   | get_inner_fresh_fun (v as Var _)  = NONE
    91   | get_inner_fresh_fun (Const _) = NONE
    92   | get_inner_fresh_fun (Abs (_, _, t)) = get_inner_fresh_fun t
    93   | get_inner_fresh_fun (Const (@{const_name Nominal.fresh_fun},
    94       Type(@{type_name fun},[Type (@{type_name fun},[Type (T,_),_]),_])) $ u) = SOME T
    95   | get_inner_fresh_fun (t $ u) =
    96      let val a = get_inner_fresh_fun u in
    97      if a = NONE then get_inner_fresh_fun t else a
    98      end;
    99 
   100 (* This tactic generates a fresh name of the atom type *)
   101 (* given by the innermost fresh_fun                    *)
   102 
   103 fun generate_fresh_fun_tac ctxt = SUBGOAL (fn (goal, _) =>
   104   let
   105     val atom_name_opt = get_inner_fresh_fun goal;
   106   in
   107   case atom_name_opt of
   108     NONE => all_tac
   109   | SOME atom_name  => generate_fresh_tac ctxt atom_name
   110   end) 1;
   111 
   112 (* Two substitution tactics which looks for the innermost occurrence in
   113    one assumption or in the conclusion *)
   114 
   115 val search_fun = curry (Seq.flat o uncurry EqSubst.searchf_bt_unify_valid);
   116 val search_fun_asm = EqSubst.skip_first_asm_occs_search EqSubst.searchf_bt_unify_valid;
   117 
   118 fun subst_inner_tac ctxt = EqSubst.eqsubst_tac' ctxt search_fun;
   119 fun subst_inner_asm_tac_aux i ctxt = EqSubst.eqsubst_asm_tac' ctxt search_fun_asm i;
   120 
   121 (* A tactic to substitute in the first assumption
   122    which contains an occurrence. *)
   123 
   124 fun subst_inner_asm_tac ctxt th =
   125   curry (curry (FIRST' (map uncurry (map uncurry (map subst_inner_asm_tac_aux
   126             (1 upto Thm.nprems_of th)))))) ctxt th;
   127 
   128 fun fresh_fun_tac ctxt no_asm = SUBGOAL (fn (goal, i) =>
   129   (* Find the variable we instantiate *)
   130   let
   131     val thy = Proof_Context.theory_of ctxt;
   132     val abs_fresh = Global_Theory.get_thms thy "abs_fresh";
   133     val fresh_perm_app = Global_Theory.get_thms thy "fresh_perm_app";
   134     val simp_ctxt =
   135       ctxt addsimps (fresh_prod :: abs_fresh)
   136       addsimps fresh_perm_app;
   137     val x = hd (tl (Misc_Legacy.term_vars (Thm.prop_of exI)));
   138     val atom_name_opt = get_inner_fresh_fun goal;
   139     val n = length (Logic.strip_params goal);
   140     (* Here we rely on the fact that the variable introduced by generate_fresh_tac *)
   141     (* is the last one in the list, the inner one *)
   142   in
   143   case atom_name_opt of
   144     NONE => all_tac
   145   | SOME atom_name =>
   146   let
   147     val atom_basename = Long_Name.base_name atom_name;
   148     val pt_name_inst = get_dyn_thm thy ("pt_"^atom_basename^"_inst") atom_basename;
   149     val at_name_inst = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
   150     fun inst_fresh vars params i st =
   151    let val vars' = Misc_Legacy.term_vars (Thm.prop_of st);
   152    in case subtract (op =) vars vars' of
   153      [Var v] =>
   154       Seq.single
   155         (Thm.instantiate ([],
   156           [(v, Thm.cterm_of ctxt (fold_rev Term.abs params (Bound 0)))]) st)
   157     | _ => error "fresh_fun_simp: Too many variables, please report."
   158    end
   159   in
   160   ((fn st =>
   161   let
   162     val vars = Misc_Legacy.term_vars (Thm.prop_of st);
   163     val params = Logic.strip_params (nth (Thm.prems_of st) (i-1))
   164     (* The tactics which solve the subgoals generated
   165        by the conditionnal rewrite rule. *)
   166     val post_rewrite_tacs =
   167           [resolve_tac ctxt [pt_name_inst],
   168            resolve_tac ctxt [at_name_inst],
   169            TRY o SOLVED' (NominalPermeq.finite_guess_tac simp_ctxt),
   170            inst_fresh vars params THEN'
   171            (TRY o SOLVED' (NominalPermeq.fresh_guess_tac simp_ctxt)) THEN'
   172            (TRY o SOLVED' (asm_full_simp_tac simp_ctxt))]
   173   in
   174    ((if no_asm then no_tac else
   175     (subst_inner_asm_tac ctxt fresh_fun_app' i THEN (RANGE post_rewrite_tacs i)))
   176     ORELSE
   177     (subst_inner_tac ctxt fresh_fun_app' i THEN (RANGE post_rewrite_tacs i))) st
   178   end))
   179   end
   180   end)
   181