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