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