src/HOL/Nominal/nominal_inductive2.ML
author bulwahn
Sat May 16 20:17:59 2009 +0200 (2009-05-16)
changeset 31174 f1f1e9b53c81
parent 30763 6976521b4263
child 31177 c39994cb152a
permissions -rw-r--r--
added new kind generated_theorem for theorems which are generated by packages to distinguish between theorems from users and packages
     1 (*  Title:      HOL/Nominal/nominal_inductive2.ML
     2     Author:     Stefan Berghofer, TU Muenchen
     3 
     4 Infrastructure for proving equivariance and strong induction theorems
     5 for inductive predicates involving nominal datatypes.
     6 Experimental version that allows to avoid lists of atoms.
     7 *)
     8 
     9 signature NOMINAL_INDUCTIVE2 =
    10 sig
    11   val prove_strong_ind: string -> (string * string list) list -> local_theory -> Proof.state
    12 end
    13 
    14 structure NominalInductive2 : NOMINAL_INDUCTIVE2 =
    15 struct
    16 
    17 val inductive_forall_name = "HOL.induct_forall";
    18 val inductive_forall_def = thm "induct_forall_def";
    19 val inductive_atomize = thms "induct_atomize";
    20 val inductive_rulify = thms "induct_rulify";
    21 
    22 fun rulify_term thy = MetaSimplifier.rewrite_term thy inductive_rulify [];
    23 
    24 val atomize_conv =
    25   MetaSimplifier.rewrite_cterm (true, false, false) (K (K NONE))
    26     (HOL_basic_ss addsimps inductive_atomize);
    27 val atomize_intr = Conv.fconv_rule (Conv.prems_conv ~1 atomize_conv);
    28 fun atomize_induct ctxt = Conv.fconv_rule (Conv.prems_conv ~1
    29   (Conv.params_conv ~1 (K (Conv.prems_conv ~1 atomize_conv)) ctxt));
    30 
    31 val fresh_postprocess =
    32   Simplifier.full_simplify (HOL_basic_ss addsimps
    33     [@{thm fresh_star_set_eq}, @{thm fresh_star_Un_elim},
    34      @{thm fresh_star_insert_elim}, @{thm fresh_star_empty_elim}]);
    35 
    36 fun preds_of ps t = gen_inter (op = o apfst dest_Free) (ps, Term.add_frees t []);
    37 
    38 val perm_bool = mk_meta_eq (thm "perm_bool");
    39 val perm_boolI = thm "perm_boolI";
    40 val (_, [perm_boolI_pi, _]) = Drule.strip_comb (snd (Thm.dest_comb
    41   (Drule.strip_imp_concl (cprop_of perm_boolI))));
    42 
    43 fun mk_perm_bool pi th = th RS Drule.cterm_instantiate
    44   [(perm_boolI_pi, pi)] perm_boolI;
    45 
    46 fun mk_perm_bool_simproc names = Simplifier.simproc_i
    47   (theory_of_thm perm_bool) "perm_bool" [@{term "perm pi x"}] (fn thy => fn ss =>
    48     fn Const ("Nominal.perm", _) $ _ $ t =>
    49          if the_default "" (try (head_of #> dest_Const #> fst) t) mem names
    50          then SOME perm_bool else NONE
    51      | _ => NONE);
    52 
    53 fun transp ([] :: _) = []
    54   | transp xs = map hd xs :: transp (map tl xs);
    55 
    56 fun add_binders thy i (t as (_ $ _)) bs = (case strip_comb t of
    57       (Const (s, T), ts) => (case strip_type T of
    58         (Ts, Type (tname, _)) =>
    59           (case NominalPackage.get_nominal_datatype thy tname of
    60              NONE => fold (add_binders thy i) ts bs
    61            | SOME {descr, index, ...} => (case AList.lookup op =
    62                  (#3 (the (AList.lookup op = descr index))) s of
    63                NONE => fold (add_binders thy i) ts bs
    64              | SOME cargs => fst (fold (fn (xs, x) => fn (bs', cargs') =>
    65                  let val (cargs1, (u, _) :: cargs2) = chop (length xs) cargs'
    66                  in (add_binders thy i u
    67                    (fold (fn (u, T) =>
    68                       if exists (fn j => j < i) (loose_bnos u) then I
    69                       else AList.map_default op = (T, [])
    70                         (insert op aconv (incr_boundvars (~i) u)))
    71                           cargs1 bs'), cargs2)
    72                  end) cargs (bs, ts ~~ Ts))))
    73       | _ => fold (add_binders thy i) ts bs)
    74     | (u, ts) => add_binders thy i u (fold (add_binders thy i) ts bs))
    75   | add_binders thy i (Abs (_, _, t)) bs = add_binders thy (i + 1) t bs
    76   | add_binders thy i _ bs = bs;
    77 
    78 fun split_conj f names (Const ("op &", _) $ p $ q) _ = (case head_of p of
    79       Const (name, _) =>
    80         if name mem names then SOME (f p q) else NONE
    81     | _ => NONE)
    82   | split_conj _ _ _ _ = NONE;
    83 
    84 fun strip_all [] t = t
    85   | strip_all (_ :: xs) (Const ("All", _) $ Abs (s, T, t)) = strip_all xs t;
    86 
    87 (*********************************************************************)
    88 (* maps  R ... & (ALL pi_1 ... pi_n z. P z (pi_1 o ... o pi_n o t))  *)
    89 (* or    ALL pi_1 ... pi_n z. P z (pi_1 o ... o pi_n o t)            *)
    90 (* to    R ... & id (ALL z. P z (pi_1 o ... o pi_n o t))             *)
    91 (* or    id (ALL z. P z (pi_1 o ... o pi_n o t))                     *)
    92 (*                                                                   *)
    93 (* where "id" protects the subformula from simplification            *)
    94 (*********************************************************************)
    95 
    96 fun inst_conj_all names ps pis (Const ("op &", _) $ p $ q) _ =
    97       (case head_of p of
    98          Const (name, _) =>
    99            if name mem names then SOME (HOLogic.mk_conj (p,
   100              Const ("Fun.id", HOLogic.boolT --> HOLogic.boolT) $
   101                (subst_bounds (pis, strip_all pis q))))
   102            else NONE
   103        | _ => NONE)
   104   | inst_conj_all names ps pis t u =
   105       if member (op aconv) ps (head_of u) then
   106         SOME (Const ("Fun.id", HOLogic.boolT --> HOLogic.boolT) $
   107           (subst_bounds (pis, strip_all pis t)))
   108       else NONE
   109   | inst_conj_all _ _ _ _ _ = NONE;
   110 
   111 fun inst_conj_all_tac k = EVERY
   112   [TRY (EVERY [etac conjE 1, rtac conjI 1, atac 1]),
   113    REPEAT_DETERM_N k (etac allE 1),
   114    simp_tac (HOL_basic_ss addsimps [@{thm id_apply}]) 1];
   115 
   116 fun map_term f t u = (case f t u of
   117       NONE => map_term' f t u | x => x)
   118 and map_term' f (t $ u) (t' $ u') = (case (map_term f t t', map_term f u u') of
   119       (NONE, NONE) => NONE
   120     | (SOME t'', NONE) => SOME (t'' $ u)
   121     | (NONE, SOME u'') => SOME (t $ u'')
   122     | (SOME t'', SOME u'') => SOME (t'' $ u''))
   123   | map_term' f (Abs (s, T, t)) (Abs (s', T', t')) = (case map_term f t t' of
   124       NONE => NONE
   125     | SOME t'' => SOME (Abs (s, T, t'')))
   126   | map_term' _ _ _ = NONE;
   127 
   128 (*********************************************************************)
   129 (*         Prove  F[f t]  from  F[t],  where F is monotone           *)
   130 (*********************************************************************)
   131 
   132 fun map_thm ctxt f tac monos opt th =
   133   let
   134     val prop = prop_of th;
   135     fun prove t =
   136       Goal.prove ctxt [] [] t (fn _ =>
   137         EVERY [cut_facts_tac [th] 1, etac rev_mp 1,
   138           REPEAT_DETERM (FIRSTGOAL (resolve_tac monos)),
   139           REPEAT_DETERM (rtac impI 1 THEN (atac 1 ORELSE tac))])
   140   in Option.map prove (map_term f prop (the_default prop opt)) end;
   141 
   142 fun abs_params params t =
   143   let val vs =  map (Var o apfst (rpair 0)) (Term.rename_wrt_term t params)
   144   in (list_all (params, t), (rev vs, subst_bounds (vs, t))) end;
   145 
   146 fun inst_params thy (vs, p) th cts =
   147   let val env = Pattern.first_order_match thy (p, prop_of th)
   148     (Vartab.empty, Vartab.empty)
   149   in Thm.instantiate ([],
   150     map (Envir.subst_vars env #> cterm_of thy) vs ~~ cts) th
   151   end;
   152 
   153 fun prove_strong_ind s avoids ctxt =
   154   let
   155     val thy = ProofContext.theory_of ctxt;
   156     val ({names, ...}, {raw_induct, intrs, elims, ...}) =
   157       InductivePackage.the_inductive ctxt (Sign.intern_const thy s);
   158     val ind_params = InductivePackage.params_of raw_induct;
   159     val raw_induct = atomize_induct ctxt raw_induct;
   160     val elims = map (atomize_induct ctxt) elims;
   161     val monos = InductivePackage.get_monos ctxt;
   162     val eqvt_thms = NominalThmDecls.get_eqvt_thms ctxt;
   163     val _ = (case names \\ fold (Term.add_const_names o Thm.prop_of) eqvt_thms [] of
   164         [] => ()
   165       | xs => error ("Missing equivariance theorem for predicate(s): " ^
   166           commas_quote xs));
   167     val induct_cases = map fst (fst (RuleCases.get (the
   168       (Induct.lookup_inductP ctxt (hd names)))));
   169     val induct_cases' = if null induct_cases then replicate (length intrs) ""
   170       else induct_cases;
   171     val ([raw_induct'], ctxt') = Variable.import_terms false [prop_of raw_induct] ctxt;
   172     val concls = raw_induct' |> Logic.strip_imp_concl |> HOLogic.dest_Trueprop |>
   173       HOLogic.dest_conj |> map (HOLogic.dest_imp ##> strip_comb);
   174     val ps = map (fst o snd) concls;
   175 
   176     val _ = (case duplicates (op = o pairself fst) avoids of
   177         [] => ()
   178       | xs => error ("Duplicate case names: " ^ commas_quote (map fst xs)));
   179     val _ = (case map fst avoids \\ induct_cases of
   180         [] => ()
   181       | xs => error ("No such case(s) in inductive definition: " ^ commas_quote xs));
   182     fun mk_avoids params name sets =
   183       let
   184         val (_, ctxt') = ProofContext.add_fixes
   185           (map (fn (s, T) => (Binding.name s, SOME T, NoSyn)) params) ctxt;
   186         fun mk s =
   187           let
   188             val t = Syntax.read_term ctxt' s;
   189             val t' = list_abs_free (params, t) |>
   190               funpow (length params) (fn Abs (_, _, t) => t)
   191           in (t', HOLogic.dest_setT (fastype_of t)) end
   192           handle TERM _ =>
   193             error ("Expression " ^ quote s ^ " to be avoided in case " ^
   194               quote name ^ " is not a set type");
   195         fun add_set p [] = [p]
   196           | add_set (t, T) ((u, U) :: ps) =
   197               if T = U then
   198                 let val S = HOLogic.mk_setT T
   199                 in (Const (@{const_name "Un"}, S --> S --> S) $ u $ t, T) :: ps
   200                 end
   201               else (u, U) :: add_set (t, T) ps
   202       in
   203         fold (mk #> add_set) sets []
   204       end;
   205 
   206     val prems = map (fn (prem, name) =>
   207       let
   208         val prems = map (incr_boundvars 1) (Logic.strip_assums_hyp prem);
   209         val concl = incr_boundvars 1 (Logic.strip_assums_concl prem);
   210         val params = Logic.strip_params prem
   211       in
   212         (params,
   213          if null avoids then
   214            map (fn (T, ts) => (HOLogic.mk_set T ts, T))
   215              (fold (add_binders thy 0) (prems @ [concl]) [])
   216          else case AList.lookup op = avoids name of
   217            NONE => []
   218          | SOME sets =>
   219              map (apfst (incr_boundvars 1)) (mk_avoids params name sets),
   220          prems, strip_comb (HOLogic.dest_Trueprop concl))
   221       end) (Logic.strip_imp_prems raw_induct' ~~ induct_cases');
   222 
   223     val atomTs = distinct op = (maps (map snd o #2) prems);
   224     val atoms = map (fst o dest_Type) atomTs;
   225     val ind_sort = if null atomTs then HOLogic.typeS
   226       else Sign.certify_sort thy (map (fn a => Sign.intern_class thy
   227         ("fs_" ^ Long_Name.base_name a)) atoms);
   228     val ([fs_ctxt_tyname], _) = Name.variants ["'n"] (Variable.names_of ctxt');
   229     val ([fs_ctxt_name], ctxt'') = Variable.variant_fixes ["z"] ctxt';
   230     val fsT = TFree (fs_ctxt_tyname, ind_sort);
   231 
   232     val inductive_forall_def' = Drule.instantiate'
   233       [SOME (ctyp_of thy fsT)] [] inductive_forall_def;
   234 
   235     fun lift_pred' t (Free (s, T)) ts =
   236       list_comb (Free (s, fsT --> T), t :: ts);
   237     val lift_pred = lift_pred' (Bound 0);
   238 
   239     fun lift_prem (t as (f $ u)) =
   240           let val (p, ts) = strip_comb t
   241           in
   242             if p mem ps then
   243               Const (inductive_forall_name,
   244                 (fsT --> HOLogic.boolT) --> HOLogic.boolT) $
   245                   Abs ("z", fsT, lift_pred p (map (incr_boundvars 1) ts))
   246             else lift_prem f $ lift_prem u
   247           end
   248       | lift_prem (Abs (s, T, t)) = Abs (s, T, lift_prem t)
   249       | lift_prem t = t;
   250 
   251     fun mk_fresh (x, T) = HOLogic.mk_Trueprop
   252       (NominalPackage.fresh_star_const T fsT $ x $ Bound 0);
   253 
   254     val (prems', prems'') = split_list (map (fn (params, sets, prems, (p, ts)) =>
   255       let
   256         val params' = params @ [("y", fsT)];
   257         val prem = Logic.list_implies
   258           (map mk_fresh sets @
   259            map (fn prem =>
   260              if null (preds_of ps prem) then prem
   261              else lift_prem prem) prems,
   262            HOLogic.mk_Trueprop (lift_pred p ts));
   263       in abs_params params' prem end) prems);
   264 
   265     val ind_vars =
   266       (DatatypeProp.indexify_names (replicate (length atomTs) "pi") ~~
   267        map NominalAtoms.mk_permT atomTs) @ [("z", fsT)];
   268     val ind_Ts = rev (map snd ind_vars);
   269 
   270     val concl = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
   271       (map (fn (prem, (p, ts)) => HOLogic.mk_imp (prem,
   272         HOLogic.list_all (ind_vars, lift_pred p
   273           (map (fold_rev (NominalPackage.mk_perm ind_Ts)
   274             (map Bound (length atomTs downto 1))) ts)))) concls));
   275 
   276     val concl' = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
   277       (map (fn (prem, (p, ts)) => HOLogic.mk_imp (prem,
   278         lift_pred' (Free (fs_ctxt_name, fsT)) p ts)) concls));
   279 
   280     val (vc_compat, vc_compat') = map (fn (params, sets, prems, (p, ts)) =>
   281       map (fn q => abs_params params (incr_boundvars ~1 (Logic.list_implies
   282           (List.mapPartial (fn prem =>
   283              if null (preds_of ps prem) then SOME prem
   284              else map_term (split_conj (K o I) names) prem prem) prems, q))))
   285         (maps (fn (t, T) => map (fn (u, U) => HOLogic.mk_Trueprop
   286            (NominalPackage.fresh_star_const U T $ u $ t)) sets)
   287              (ts ~~ binder_types (fastype_of p)) @
   288          map (fn (u, U) => HOLogic.mk_Trueprop (Const (@{const_name finite},
   289            HOLogic.mk_setT U --> HOLogic.boolT) $ u)) sets) |>
   290       split_list) prems |> split_list;
   291 
   292     val perm_pi_simp = PureThy.get_thms thy "perm_pi_simp";
   293     val pt2_atoms = map (fn a => PureThy.get_thm thy
   294       ("pt_" ^ Long_Name.base_name a ^ "2")) atoms;
   295     val eqvt_ss = Simplifier.theory_context thy HOL_basic_ss
   296       addsimps (eqvt_thms @ perm_pi_simp @ pt2_atoms)
   297       addsimprocs [mk_perm_bool_simproc ["Fun.id"],
   298         NominalPermeq.perm_simproc_app, NominalPermeq.perm_simproc_fun];
   299     val fresh_star_bij = PureThy.get_thms thy "fresh_star_bij";
   300     val pt_insts = map (NominalAtoms.pt_inst_of thy) atoms;
   301     val at_insts = map (NominalAtoms.at_inst_of thy) atoms;
   302     val dj_thms = maps (fn a =>
   303       map (NominalAtoms.dj_thm_of thy a) (atoms \ a)) atoms;
   304     val finite_ineq = map2 (fn th => fn th' => th' RS (th RS
   305       @{thm pt_set_finite_ineq})) pt_insts at_insts;
   306     val perm_set_forget =
   307       map (fn th => th RS @{thm dj_perm_set_forget}) dj_thms;
   308     val perm_freshs_freshs = atomTs ~~ map2 (fn th => fn th' => th' RS (th RS
   309       @{thm pt_freshs_freshs})) pt_insts at_insts;
   310 
   311     fun obtain_fresh_name ts sets (T, fin) (freshs, ths1, ths2, ths3, ctxt) =
   312       let
   313         val thy = ProofContext.theory_of ctxt;
   314         (** protect terms to avoid that fresh_star_prod_set interferes with  **)
   315         (** pairs used in introduction rules of inductive predicate          **)
   316         fun protect t =
   317           let val T = fastype_of t in Const ("Fun.id", T --> T) $ t end;
   318         val p = foldr1 HOLogic.mk_prod (map protect ts);
   319         val atom = fst (dest_Type T);
   320         val {at_inst, ...} = NominalAtoms.the_atom_info thy atom;
   321         val fs_atom = PureThy.get_thm thy
   322           ("fs_" ^ Long_Name.base_name atom ^ "1");
   323         val avoid_th = Drule.instantiate'
   324           [SOME (ctyp_of thy (fastype_of p))] [SOME (cterm_of thy p)]
   325           ([at_inst, fin, fs_atom] MRS @{thm at_set_avoiding});
   326         val (([cx], th1 :: th2 :: ths), ctxt') = Obtain.result
   327           (fn _ => EVERY
   328             [rtac avoid_th 1,
   329              full_simp_tac (HOL_ss addsimps [@{thm fresh_star_prod_set}]) 1,
   330              full_simp_tac (HOL_basic_ss addsimps [@{thm id_apply}]) 1,
   331              rotate_tac 1 1,
   332              REPEAT (etac conjE 1)])
   333           [] ctxt;
   334         val (Ts1, _ :: Ts2) = take_prefix (not o equal T) (map snd sets);
   335         val pTs = map NominalAtoms.mk_permT (Ts1 @ Ts2);
   336         val (pis1, pis2) = chop (length Ts1)
   337           (map Bound (length pTs - 1 downto 0));
   338         val _ $ (f $ (_ $ pi $ l) $ r) = prop_of th2
   339         val th2' =
   340           Goal.prove ctxt [] []
   341             (list_all (map (pair "pi") pTs, HOLogic.mk_Trueprop
   342                (f $ fold_rev (NominalPackage.mk_perm (rev pTs))
   343                   (pis1 @ pi :: pis2) l $ r)))
   344             (fn _ => cut_facts_tac [th2] 1 THEN
   345                full_simp_tac (HOL_basic_ss addsimps perm_set_forget) 1) |>
   346           Simplifier.simplify eqvt_ss
   347       in
   348         (freshs @ [term_of cx],
   349          ths1 @ ths, ths2 @ [th1], ths3 @ [th2'], ctxt')
   350       end;
   351 
   352     fun mk_ind_proof ctxt' thss =
   353       Goal.prove ctxt' [] prems' concl' (fn {prems = ihyps, context = ctxt} =>
   354         let val th = Goal.prove ctxt [] [] concl (fn {context, ...} =>
   355           rtac raw_induct 1 THEN
   356           EVERY (maps (fn (((((_, sets, oprems, _),
   357               vc_compat_ths), vc_compat_vs), ihyp), vs_ihypt) =>
   358             [REPEAT (rtac allI 1), simp_tac eqvt_ss 1,
   359              SUBPROOF (fn {prems = gprems, params, concl, context = ctxt', ...} =>
   360                let
   361                  val (cparams', (pis, z)) =
   362                    chop (length params - length atomTs - 1) params ||>
   363                    (map term_of #> split_last);
   364                  val params' = map term_of cparams'
   365                  val sets' = map (apfst (curry subst_bounds (rev params'))) sets;
   366                  val pi_sets = map (fn (t, _) =>
   367                    fold_rev (NominalPackage.mk_perm []) pis t) sets';
   368                  val (P, ts) = strip_comb (HOLogic.dest_Trueprop (term_of concl));
   369                  val gprems1 = List.mapPartial (fn (th, t) =>
   370                    if null (preds_of ps t) then SOME th
   371                    else
   372                      map_thm ctxt' (split_conj (K o I) names)
   373                        (etac conjunct1 1) monos NONE th)
   374                    (gprems ~~ oprems);
   375                  val vc_compat_ths' = map2 (fn th => fn p =>
   376                    let
   377                      val th' = gprems1 MRS inst_params thy p th cparams';
   378                      val (h, ts) =
   379                        strip_comb (HOLogic.dest_Trueprop (concl_of th'))
   380                    in
   381                      Goal.prove ctxt' [] []
   382                        (HOLogic.mk_Trueprop (list_comb (h,
   383                           map (fold_rev (NominalPackage.mk_perm []) pis) ts)))
   384                        (fn _ => simp_tac (HOL_basic_ss addsimps
   385                           (fresh_star_bij @ finite_ineq)) 1 THEN rtac th' 1)
   386                    end) vc_compat_ths vc_compat_vs;
   387                  val (vc_compat_ths1, vc_compat_ths2) =
   388                    chop (length vc_compat_ths - length sets) vc_compat_ths';
   389                  val vc_compat_ths1' = map
   390                    (Conv.fconv_rule (Conv.arg_conv (Conv.arg_conv
   391                       (Simplifier.rewrite eqvt_ss)))) vc_compat_ths1;
   392                  val (pis', fresh_ths1, fresh_ths2, fresh_ths3, ctxt'') = fold
   393                    (obtain_fresh_name ts sets)
   394                    (map snd sets' ~~ vc_compat_ths2) ([], [], [], [], ctxt');
   395                  fun concat_perm pi1 pi2 =
   396                    let val T = fastype_of pi1
   397                    in if T = fastype_of pi2 then
   398                        Const ("List.append", T --> T --> T) $ pi1 $ pi2
   399                      else pi2
   400                    end;
   401                  val pis'' = fold_rev (concat_perm #> map) pis' pis;
   402                  val ihyp' = inst_params thy vs_ihypt ihyp
   403                    (map (fold_rev (NominalPackage.mk_perm [])
   404                       (pis' @ pis) #> cterm_of thy) params' @ [cterm_of thy z]);
   405                  fun mk_pi th =
   406                    Simplifier.simplify (HOL_basic_ss addsimps [@{thm id_apply}]
   407                        addsimprocs [NominalPackage.perm_simproc])
   408                      (Simplifier.simplify eqvt_ss
   409                        (fold_rev (mk_perm_bool o cterm_of thy)
   410                          (pis' @ pis) th));
   411                  val gprems2 = map (fn (th, t) =>
   412                    if null (preds_of ps t) then mk_pi th
   413                    else
   414                      mk_pi (the (map_thm ctxt (inst_conj_all names ps (rev pis''))
   415                        (inst_conj_all_tac (length pis'')) monos (SOME t) th)))
   416                    (gprems ~~ oprems);
   417                  val perm_freshs_freshs' = map (fn (th, (_, T)) =>
   418                    th RS the (AList.lookup op = perm_freshs_freshs T))
   419                      (fresh_ths2 ~~ sets);
   420                  val th = Goal.prove ctxt'' [] []
   421                    (HOLogic.mk_Trueprop (list_comb (P $ hd ts,
   422                      map (fold_rev (NominalPackage.mk_perm []) pis') (tl ts))))
   423                    (fn _ => EVERY ([simp_tac eqvt_ss 1, rtac ihyp' 1] @
   424                      map (fn th => rtac th 1) fresh_ths3 @
   425                      [REPEAT_DETERM_N (length gprems)
   426                        (simp_tac (HOL_basic_ss
   427                           addsimps [inductive_forall_def']
   428                           addsimprocs [NominalPackage.perm_simproc]) 1 THEN
   429                         resolve_tac gprems2 1)]));
   430                  val final = Goal.prove ctxt'' [] [] (term_of concl)
   431                    (fn _ => cut_facts_tac [th] 1 THEN full_simp_tac (HOL_ss
   432                      addsimps vc_compat_ths1' @ fresh_ths1 @
   433                        perm_freshs_freshs') 1);
   434                  val final' = ProofContext.export ctxt'' ctxt' [final];
   435                in resolve_tac final' 1 end) context 1])
   436                  (prems ~~ thss ~~ vc_compat' ~~ ihyps ~~ prems'')))
   437         in
   438           cut_facts_tac [th] 1 THEN REPEAT (etac conjE 1) THEN
   439           REPEAT (REPEAT (resolve_tac [conjI, impI] 1) THEN
   440             etac impE 1 THEN atac 1 THEN REPEAT (etac @{thm allE_Nil} 1) THEN
   441             asm_full_simp_tac (simpset_of thy) 1)
   442         end) |>
   443         fresh_postprocess |>
   444         singleton (ProofContext.export ctxt' ctxt);
   445 
   446   in
   447     ctxt'' |>
   448     Proof.theorem_i NONE (fn thss => fn ctxt =>
   449       let
   450         val rec_name = space_implode "_" (map Long_Name.base_name names);
   451         val rec_qualified = Binding.qualify false rec_name;
   452         val ind_case_names = RuleCases.case_names induct_cases;
   453         val induct_cases' = InductivePackage.partition_rules' raw_induct
   454           (intrs ~~ induct_cases); 
   455         val thss' = map (map atomize_intr) thss;
   456         val thsss = InductivePackage.partition_rules' raw_induct (intrs ~~ thss');
   457         val strong_raw_induct =
   458           mk_ind_proof ctxt thss' |> InductivePackage.rulify;
   459         val strong_induct =
   460           if length names > 1 then
   461             (strong_raw_induct, [ind_case_names, RuleCases.consumes 0])
   462           else (strong_raw_induct RSN (2, rev_mp),
   463             [ind_case_names, RuleCases.consumes 1]);
   464         val ((_, [strong_induct']), ctxt') = LocalTheory.note Thm.generated_theoremK
   465           ((rec_qualified (Binding.name "strong_induct"),
   466             map (Attrib.internal o K) (#2 strong_induct)), [#1 strong_induct])
   467           ctxt;
   468         val strong_inducts =
   469           ProjectRule.projects ctxt' (1 upto length names) strong_induct'
   470       in
   471         ctxt' |>
   472         LocalTheory.note Thm.generated_theoremK
   473           ((rec_qualified (Binding.name "strong_inducts"),
   474             [Attrib.internal (K ind_case_names),
   475              Attrib.internal (K (RuleCases.consumes 1))]),
   476            strong_inducts) |> snd
   477       end)
   478       (map (map (rulify_term thy #> rpair [])) vc_compat)
   479   end;
   480 
   481 
   482 (* outer syntax *)
   483 
   484 local structure P = OuterParse and K = OuterKeyword in
   485 
   486 val _ =
   487   OuterSyntax.local_theory_to_proof "nominal_inductive2"
   488     "prove strong induction theorem for inductive predicate involving nominal datatypes" K.thy_goal
   489     (P.xname -- Scan.optional (P.$$$ "avoids" |-- P.enum1 "|" (P.name --
   490       (P.$$$ ":" |-- P.and_list1 P.term))) [] >> (fn (name, avoids) =>
   491         prove_strong_ind name avoids));
   492 
   493 end;
   494 
   495 end