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