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