src/HOL/Nominal/nominal_fresh_fun.ML
author wenzelm
Tue Oct 20 23:25:04 2009 +0200 (2009-10-20)
changeset 33034 66ef64a5f122
parent 32149 ef59550a55d3
child 33040 cffdb7b28498
permissions -rw-r--r--
eliminated THENL -- use THEN RANGE;
eliminated TRY' -- use TRY with op o;
observe naming convention ctxt: Proof.context;
tuned whitespace;
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
urbanc@24558
     8
(* First some functions that should be in the library *)
narboux@22729
     9
urbanc@24558
    10
(* A tactic which only succeeds when the argument *)
urbanc@24558
    11
(* tactic solves completely the specified subgoal *)
narboux@23054
    12
fun SOLVEI t = t THEN_ALL_NEW (fn i => no_tac);
narboux@23091
    13
narboux@22729
    14
fun gen_res_inst_tac_term instf tyinst tinst elim th i st =
narboux@22729
    15
  let
narboux@22729
    16
    val thy = theory_of_thm st;
narboux@22729
    17
    val cgoal = nth (cprems_of st) (i - 1);
narboux@22729
    18
    val {maxidx, ...} = rep_cterm cgoal;
narboux@22729
    19
    val j = maxidx + 1;
narboux@22729
    20
    val tyinst' = map (apfst (Logic.incr_tvar j)) tyinst;
narboux@22729
    21
    val ps = Logic.strip_params (term_of cgoal);
narboux@22729
    22
    val Ts = map snd ps;
narboux@22729
    23
    val tinst' = map (fn (t, u) =>
narboux@22729
    24
      (head_of (Logic.incr_indexes (Ts, j) t),
narboux@22729
    25
       list_abs (ps, u))) tinst;
narboux@22729
    26
    val th' = instf
narboux@22729
    27
      (map (pairself (ctyp_of thy)) tyinst')
narboux@22729
    28
      (map (pairself (cterm_of thy)) tinst')
narboux@22729
    29
      (Thm.lift_rule cgoal th)
narboux@22729
    30
  in
narboux@22729
    31
    compose_tac (elim, th', nprems_of th) i st
narboux@22729
    32
  end handle Subscript => Seq.empty;
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@27239
    41
  res_inst_tac_term' tinst false (RuleInsts.make_elim_preserve th);
narboux@22729
    42
wenzelm@26337
    43
fun get_dyn_thm thy name atom_name =
wenzelm@26343
    44
  PureThy.get_thm thy name handle ERROR _ =>
wenzelm@27187
    45
    error ("The atom type "^atom_name^" is not defined.");
narboux@22729
    46
urbanc@24558
    47
(* End of function waiting to be in the library :o) *)
narboux@22729
    48
urbanc@24558
    49
(* The theorems needed that are known at compile time. *)
urbanc@24558
    50
val at_exists_fresh' = @{thm "at_exists_fresh'"};
urbanc@24558
    51
val fresh_fun_app'   = @{thm "fresh_fun_app'"};
urbanc@24558
    52
val fresh_prod       = @{thm "fresh_prod"};
narboux@22729
    53
wenzelm@33034
    54
(* A tactic to generate a name fresh for  all the free *)
urbanc@24558
    55
(* variables and parameters of the goal                *)
narboux@22729
    56
narboux@22729
    57
fun generate_fresh_tac atom_name i thm =
wenzelm@33034
    58
 let
narboux@22729
    59
   val thy = theory_of_thm thm;
narboux@22729
    60
(* the parsing function returns a qualified name, we get back the base name *)
wenzelm@30364
    61
   val atom_basename = Long_Name.base_name atom_name;
narboux@22729
    62
   val goal = List.nth(prems_of thm, i-1);
narboux@22729
    63
   val ps = Logic.strip_params goal;
narboux@22729
    64
   val Ts = rev (map snd ps);
wenzelm@33034
    65
   fun is_of_fs_name T = Sign.of_sort thy (T, Sign.intern_sort thy ["fs_"^atom_basename]);
narboux@22729
    66
(* rebuild de bruijn indices *)
narboux@22729
    67
   val bvs = map_index (Bound o fst) ps;
narboux@22729
    68
(* select variables of the right class *)
narboux@22729
    69
   val vs = filter (fn t => is_of_fs_name (fastype_of1 (Ts, t)))
wenzelm@29265
    70
     (OldTerm.term_frees goal @ bvs);
narboux@22729
    71
(* build the tuple *)
narboux@23368
    72
   val s = (Library.foldr1 (fn (v, s) =>
wenzelm@28397
    73
     HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s) vs) handle _ => HOLogic.unit ;  (* FIXME avoid handle _ *)
narboux@22729
    74
   val fs_name_thm = get_dyn_thm thy ("fs_"^atom_basename^"1") atom_basename;
narboux@22729
    75
   val at_name_inst_thm = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
narboux@22729
    76
   val exists_fresh' = at_name_inst_thm RS at_exists_fresh';
narboux@22729
    77
(* find the variable we want to instantiate *)
wenzelm@29265
    78
   val x = hd (OldTerm.term_vars (prop_of exists_fresh'));
wenzelm@33034
    79
 in
narboux@22729
    80
   (cut_inst_tac_term' [(x,s)] exists_fresh' 1 THEN
narboux@22729
    81
   rtac fs_name_thm 1 THEN
narboux@22729
    82
   etac exE 1) thm
narboux@22729
    83
  handle Empty  => all_tac thm (* if we collected no variables then we do nothing *)
narboux@22729
    84
  end;
narboux@22729
    85
narboux@22729
    86
fun get_inner_fresh_fun (Bound j) = NONE
wenzelm@33034
    87
  | get_inner_fresh_fun (v as Free _) = NONE
narboux@22729
    88
  | get_inner_fresh_fun (v as Var _)  = NONE
narboux@22729
    89
  | get_inner_fresh_fun (Const _) = NONE
wenzelm@33034
    90
  | get_inner_fresh_fun (Abs (_, _, t)) = get_inner_fresh_fun t
wenzelm@33034
    91
  | get_inner_fresh_fun (Const ("Nominal.fresh_fun",Type("fun",[Type ("fun",[Type (T,_),_]),_])) $ u)
wenzelm@33034
    92
                           = SOME T
wenzelm@33034
    93
  | get_inner_fresh_fun (t $ u) =
narboux@22729
    94
     let val a = get_inner_fresh_fun u in
wenzelm@33034
    95
     if a = NONE then get_inner_fresh_fun t else a
narboux@22729
    96
     end;
narboux@22729
    97
wenzelm@33034
    98
(* This tactic generates a fresh name of the atom type *)
urbanc@24558
    99
(* given by the innermost fresh_fun                    *)
narboux@22729
   100
narboux@22729
   101
fun generate_fresh_fun_tac i thm =
narboux@22729
   102
  let
narboux@22729
   103
    val goal = List.nth(prems_of thm, i-1);
narboux@22729
   104
    val atom_name_opt = get_inner_fresh_fun goal;
narboux@22729
   105
  in
wenzelm@33034
   106
  case atom_name_opt of
narboux@22729
   107
    NONE => all_tac thm
wenzelm@33034
   108
  | SOME atom_name  => generate_fresh_tac atom_name i thm
narboux@22729
   109
  end
narboux@22729
   110
wenzelm@33034
   111
(* Two substitution tactics which looks for the innermost occurence in
narboux@23091
   112
   one assumption or in the conclusion *)
narboux@22729
   113
wenzelm@33034
   114
val search_fun = curry (Seq.flat o uncurry EqSubst.searchf_bt_unify_valid);
narboux@23065
   115
val search_fun_asm = EqSubst.skip_first_asm_occs_search EqSubst.searchf_bt_unify_valid;
narboux@23054
   116
wenzelm@33034
   117
fun subst_inner_tac ctxt = EqSubst.eqsubst_tac' ctxt search_fun;
wenzelm@33034
   118
fun subst_inner_asm_tac_aux i ctxt = EqSubst.eqsubst_asm_tac' ctxt search_fun_asm i;
narboux@23054
   119
wenzelm@33034
   120
(* A tactic to substitute in the first assumption
narboux@23091
   121
   which contains an occurence. *)
narboux@22729
   122
wenzelm@33034
   123
fun subst_inner_asm_tac ctxt th =
wenzelm@33034
   124
  curry (curry (FIRST' (map uncurry (map uncurry (map subst_inner_asm_tac_aux
wenzelm@33034
   125
            (1 upto Thm.nprems_of th)))))) ctxt th;
narboux@23091
   126
wenzelm@33034
   127
fun fresh_fun_tac no_asm i thm =
narboux@22729
   128
  (* Find the variable we instantiate *)
narboux@22729
   129
  let
narboux@22729
   130
    val thy = theory_of_thm thm;
wenzelm@33034
   131
    val ctxt = ProofContext.init thy;
wenzelm@32149
   132
    val ss = global_simpset_of thy;
wenzelm@26343
   133
    val abs_fresh = PureThy.get_thms thy "abs_fresh";
wenzelm@26343
   134
    val fresh_perm_app = PureThy.get_thms thy "fresh_perm_app";
narboux@22729
   135
    val ss' = ss addsimps fresh_prod::abs_fresh;
narboux@23054
   136
    val ss'' = ss' addsimps fresh_perm_app;
wenzelm@29265
   137
    val x = hd (tl (OldTerm.term_vars (prop_of exI)));
narboux@22787
   138
    val goal = nth (prems_of thm) (i-1);
narboux@22729
   139
    val atom_name_opt = get_inner_fresh_fun goal;
wenzelm@33034
   140
    val n = length (Logic.strip_params goal);
urbanc@24558
   141
    (* Here we rely on the fact that the variable introduced by generate_fresh_tac *)
urbanc@24558
   142
    (* is the last one in the list, the inner one *)
narboux@22729
   143
  in
wenzelm@33034
   144
  case atom_name_opt of
narboux@22729
   145
    NONE => all_tac thm
wenzelm@33034
   146
  | SOME atom_name =>
wenzelm@33034
   147
  let
wenzelm@30364
   148
    val atom_basename = Long_Name.base_name atom_name;
narboux@22729
   149
    val pt_name_inst = get_dyn_thm thy ("pt_"^atom_basename^"_inst") atom_basename;
narboux@22729
   150
    val at_name_inst = get_dyn_thm thy ("at_"^atom_basename^"_inst") atom_basename;
narboux@22787
   151
    fun inst_fresh vars params i st =
wenzelm@29265
   152
   let val vars' = OldTerm.term_vars (prop_of st);
narboux@22787
   153
       val thy = theory_of_thm st;
wenzelm@33034
   154
   in case vars' \\ vars of
narboux@22787
   155
     [x] => Seq.single (Thm.instantiate ([],[(cterm_of thy x,cterm_of thy (list_abs (params,Bound 0)))]) st)
narboux@22787
   156
    | _ => error "fresh_fun_simp: Too many variables, please report."
narboux@22787
   157
  end
narboux@22729
   158
  in
narboux@23091
   159
  ((fn st =>
wenzelm@33034
   160
  let
wenzelm@29265
   161
    val vars = OldTerm.term_vars (prop_of st);
narboux@22787
   162
    val params = Logic.strip_params (nth (prems_of st) (i-1))
wenzelm@33034
   163
    (* The tactics which solve the subgoals generated
narboux@23091
   164
       by the conditionnal rewrite rule. *)
wenzelm@33034
   165
    val post_rewrite_tacs =
narboux@23054
   166
          [rtac pt_name_inst,
narboux@23054
   167
           rtac at_name_inst,
wenzelm@33034
   168
           TRY o SOLVEI (NominalPermeq.finite_guess_tac ss''),
narboux@23054
   169
           inst_fresh vars params THEN'
wenzelm@33034
   170
           (TRY o SOLVEI (NominalPermeq.fresh_guess_tac ss'')) THEN'
wenzelm@33034
   171
           (TRY o SOLVEI (asm_full_simp_tac ss''))]
wenzelm@33034
   172
  in
narboux@23094
   173
   ((if no_asm then no_tac else
wenzelm@33034
   174
    (subst_inner_asm_tac ctxt fresh_fun_app' i THEN (RANGE post_rewrite_tacs i)))
narboux@23091
   175
    ORELSE
wenzelm@33034
   176
    (subst_inner_tac ctxt fresh_fun_app' i THEN (RANGE post_rewrite_tacs i))) st
narboux@22787
   177
  end)) thm
wenzelm@33034
   178
narboux@22729
   179
  end
narboux@22729
   180
  end
narboux@22729
   181
narboux@23091
   182
(* syntax for options, given "(no_asm)" will give back true, without
narboux@23091
   183
   gives back false *)
narboux@23091
   184
val options_syntax =
narboux@23091
   185
    (Args.parens (Args.$$$ "no_asm") >> (K true)) ||
narboux@23091
   186
     (Scan.succeed false);
narboux@23091
   187
wenzelm@30549
   188
fun setup_generate_fresh x =
wenzelm@30549
   189
  (Args.goal_spec -- Args.tyname >>
wenzelm@30549
   190
    (fn (quant, s) => K (SIMPLE_METHOD'' quant (generate_fresh_tac s)))) x;
narboux@22729
   191
wenzelm@30549
   192
fun setup_fresh_fun_simp x =
wenzelm@30549
   193
  (Scan.lift options_syntax >> (fn b => K (SIMPLE_METHOD' (fresh_fun_tac b)))) x;
wenzelm@30549
   194