src/HOL/Nominal/nominal_inductive2.ML
author wenzelm
Mon Apr 06 17:06:48 2015 +0200 (2015-04-06)
changeset 59936 b8ffc3dc9e24
parent 59621 291934bac95e
child 59940 087d81f5213e
permissions -rw-r--r--
@{command_spec} is superseded by @{command_keyword};
     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 option -> (string * string list) list ->
    12     local_theory -> Proof.state
    13 end
    14 
    15 structure NominalInductive2 : NOMINAL_INDUCTIVE2 =
    16 struct
    17 
    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 = Raw_Simplifier.rewrite_term thy inductive_rulify [];
    23 
    24 fun atomize_conv ctxt =
    25   Raw_Simplifier.rewrite_cterm (true, false, false) (K (K NONE))
    26     (put_simpset HOL_basic_ss ctxt addsimps inductive_atomize);
    27 fun atomize_intr ctxt = Conv.fconv_rule (Conv.prems_conv ~1 (atomize_conv ctxt));
    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))) ctxt));
    30 
    31 fun fresh_postprocess ctxt =
    32   Simplifier.full_simplify (put_simpset HOL_basic_ss ctxt 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 = inter (op = o apsnd dest_Free) ps (Term.add_frees t []);
    37 
    38 val perm_bool = mk_meta_eq @{thm perm_bool_def};
    39 val perm_boolI = @{thm perm_boolI};
    40 val (_, [perm_boolI_pi, _]) = Drule.strip_comb (snd (Thm.dest_comb
    41   (Drule.strip_imp_concl (Thm.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_global_i
    47   (Thm.theory_of_thm perm_bool) "perm_bool" [@{term "perm pi x"}] (fn ctxt =>
    48     fn Const (@{const_name Nominal.perm}, _) $ _ $ t =>
    49          if member (op =) names (the_default "" (try (head_of #> dest_Const #> fst) t))
    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 NominalDatatype.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 (@{const_name HOL.conj}, _) $ p $ q) _ = (case head_of p of
    79       Const (name, _) =>
    80         if member (op =) names name 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 (@{const_name 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 (@{const_name HOL.conj}, _) $ p $ q) _ =
    97       (case head_of p of
    98          Const (name, _) =>
    99            if member (op =) names name then SOME (HOLogic.mk_conj (p,
   100              Const (@{const_name 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 (@{const_name 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 ctxt k = EVERY
   112   [TRY (EVERY [etac conjE 1, rtac conjI 1, atac 1]),
   113    REPEAT_DETERM_N k (etac allE 1),
   114    simp_tac (put_simpset HOL_basic_ss ctxt 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 = Thm.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 ctxt 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 (Logic.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, Thm.prop_of th)
   148     (Vartab.empty, Vartab.empty)
   149   in Thm.instantiate ([],
   150     map (Envir.subst_term env #> Thm.global_cterm_of thy) vs ~~ cts) th
   151   end;
   152 
   153 fun prove_strong_ind s alt_name avoids ctxt =
   154   let
   155     val thy = Proof_Context.theory_of ctxt;
   156     val ({names, ...}, {raw_induct, intrs, elims, ...}) =
   157       Inductive.the_inductive ctxt (Sign.intern_const thy s);
   158     val ind_params = Inductive.params_of raw_induct;
   159     val raw_induct = atomize_induct ctxt raw_induct;
   160     val elims = map (atomize_induct ctxt) elims;
   161     val monos = Inductive.get_monos ctxt;
   162     val eqvt_thms = NominalThmDecls.get_eqvt_thms ctxt;
   163     val _ = (case subtract (op =) (fold (Term.add_const_names o Thm.prop_of) eqvt_thms []) names of
   164         [] => ()
   165       | xs => error ("Missing equivariance theorem for predicate(s): " ^
   166           commas_quote xs));
   167     val induct_cases = map (fst o fst) (fst (Rule_Cases.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 [Thm.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 apply2 fst) avoids of
   177         [] => ()
   178       | xs => error ("Duplicate case names: " ^ commas_quote (map fst xs)));
   179     val _ = (case subtract (op =) induct_cases (map fst avoids) 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') = Proof_Context.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' = fold_rev absfree 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 sup}, 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 @{sort type}
   226       else Sign.minimize_sort thy (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.variant "'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 (Thm.global_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 member (op =) ps p then HOLogic.mk_induct_forall fsT $
   243               Abs ("z", fsT, lift_pred p (map (incr_boundvars 1) ts))
   244             else lift_prem f $ lift_prem u
   245           end
   246       | lift_prem (Abs (s, T, t)) = Abs (s, T, lift_prem t)
   247       | lift_prem t = t;
   248 
   249     fun mk_fresh (x, T) = HOLogic.mk_Trueprop
   250       (NominalDatatype.fresh_star_const T fsT $ x $ Bound 0);
   251 
   252     val (prems', prems'') = split_list (map (fn (params, sets, prems, (p, ts)) =>
   253       let
   254         val params' = params @ [("y", fsT)];
   255         val prem = Logic.list_implies
   256           (map mk_fresh sets @
   257            map (fn prem =>
   258              if null (preds_of ps prem) then prem
   259              else lift_prem prem) prems,
   260            HOLogic.mk_Trueprop (lift_pred p ts));
   261       in abs_params params' prem end) prems);
   262 
   263     val ind_vars =
   264       (Old_Datatype_Prop.indexify_names (replicate (length atomTs) "pi") ~~
   265        map NominalAtoms.mk_permT atomTs) @ [("z", fsT)];
   266     val ind_Ts = rev (map snd ind_vars);
   267 
   268     val concl = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
   269       (map (fn (prem, (p, ts)) => HOLogic.mk_imp (prem,
   270         HOLogic.list_all (ind_vars, lift_pred p
   271           (map (fold_rev (NominalDatatype.mk_perm ind_Ts)
   272             (map Bound (length atomTs downto 1))) ts)))) concls));
   273 
   274     val concl' = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
   275       (map (fn (prem, (p, ts)) => HOLogic.mk_imp (prem,
   276         lift_pred' (Free (fs_ctxt_name, fsT)) p ts)) concls));
   277 
   278     val (vc_compat, vc_compat') = map (fn (params, sets, prems, (p, ts)) =>
   279       map (fn q => abs_params params (incr_boundvars ~1 (Logic.list_implies
   280           (map_filter (fn prem =>
   281              if null (preds_of ps prem) then SOME prem
   282              else map_term (split_conj (K o I) names) prem prem) prems, q))))
   283         (maps (fn (t, T) => map (fn (u, U) => HOLogic.mk_Trueprop
   284            (NominalDatatype.fresh_star_const U T $ u $ t)) sets)
   285              (ts ~~ binder_types (fastype_of p)) @
   286          map (fn (u, U) => HOLogic.mk_Trueprop (Const (@{const_name finite},
   287            HOLogic.mk_setT U --> HOLogic.boolT) $ u)) sets) |>
   288       split_list) prems |> split_list;
   289 
   290     val perm_pi_simp = Global_Theory.get_thms thy "perm_pi_simp";
   291     val pt2_atoms = map (fn a => Global_Theory.get_thm thy
   292       ("pt_" ^ Long_Name.base_name a ^ "2")) atoms;
   293     val eqvt_ss = simpset_of (put_simpset HOL_basic_ss (Proof_Context.init_global thy)
   294       addsimps (eqvt_thms @ perm_pi_simp @ pt2_atoms)
   295       addsimprocs [mk_perm_bool_simproc [@{const_name Fun.id}],
   296         NominalPermeq.perm_simproc_app, NominalPermeq.perm_simproc_fun]);
   297     val fresh_star_bij = Global_Theory.get_thms thy "fresh_star_bij";
   298     val pt_insts = map (NominalAtoms.pt_inst_of thy) atoms;
   299     val at_insts = map (NominalAtoms.at_inst_of thy) atoms;
   300     val dj_thms = maps (fn a =>
   301       map (NominalAtoms.dj_thm_of thy a) (remove (op =) a atoms)) atoms;
   302     val finite_ineq = map2 (fn th => fn th' => th' RS (th RS
   303       @{thm pt_set_finite_ineq})) pt_insts at_insts;
   304     val perm_set_forget =
   305       map (fn th => th RS @{thm dj_perm_set_forget}) dj_thms;
   306     val perm_freshs_freshs = atomTs ~~ map2 (fn th => fn th' => th' RS (th RS
   307       @{thm pt_freshs_freshs})) pt_insts at_insts;
   308 
   309     fun obtain_fresh_name ts sets (T, fin) (freshs, ths1, ths2, ths3, ctxt) =
   310       let
   311         val thy = Proof_Context.theory_of ctxt;
   312         (** protect terms to avoid that fresh_star_prod_set interferes with  **)
   313         (** pairs used in introduction rules of inductive predicate          **)
   314         fun protect t =
   315           let val T = fastype_of t in Const (@{const_name Fun.id}, T --> T) $ t end;
   316         val p = foldr1 HOLogic.mk_prod (map protect ts);
   317         val atom = fst (dest_Type T);
   318         val {at_inst, ...} = NominalAtoms.the_atom_info thy atom;
   319         val fs_atom = Global_Theory.get_thm thy
   320           ("fs_" ^ Long_Name.base_name atom ^ "1");
   321         val avoid_th = Drule.instantiate'
   322           [SOME (Thm.global_ctyp_of thy (fastype_of p))] [SOME (Thm.global_cterm_of thy p)]
   323           ([at_inst, fin, fs_atom] MRS @{thm at_set_avoiding});
   324         val (([(_, cx)], th1 :: th2 :: ths), ctxt') = Obtain.result
   325           (fn ctxt' => EVERY
   326             [rtac avoid_th 1,
   327              full_simp_tac (put_simpset HOL_ss ctxt' addsimps [@{thm fresh_star_prod_set}]) 1,
   328              full_simp_tac (put_simpset HOL_basic_ss ctxt' addsimps [@{thm id_apply}]) 1,
   329              rotate_tac 1 1,
   330              REPEAT (etac conjE 1)])
   331           [] ctxt;
   332         val (Ts1, _ :: Ts2) = take_prefix (not o equal T) (map snd sets);
   333         val pTs = map NominalAtoms.mk_permT (Ts1 @ Ts2);
   334         val (pis1, pis2) = chop (length Ts1)
   335           (map Bound (length pTs - 1 downto 0));
   336         val _ $ (f $ (_ $ pi $ l) $ r) = Thm.prop_of th2
   337         val th2' =
   338           Goal.prove ctxt' [] []
   339             (Logic.list_all (map (pair "pi") pTs, HOLogic.mk_Trueprop
   340                (f $ fold_rev (NominalDatatype.mk_perm (rev pTs))
   341                   (pis1 @ pi :: pis2) l $ r)))
   342             (fn _ => cut_facts_tac [th2] 1 THEN
   343                full_simp_tac (put_simpset HOL_basic_ss ctxt' addsimps perm_set_forget) 1) |>
   344           Simplifier.simplify (put_simpset eqvt_ss ctxt')
   345       in
   346         (freshs @ [Thm.term_of cx],
   347          ths1 @ ths, ths2 @ [th1], ths3 @ [th2'], ctxt')
   348       end;
   349 
   350     fun mk_ind_proof ctxt' thss =
   351       Goal.prove ctxt' [] prems' concl' (fn {prems = ihyps, context = ctxt} =>
   352         let val th = Goal.prove ctxt [] [] concl (fn {context, ...} =>
   353           rtac raw_induct 1 THEN
   354           EVERY (maps (fn (((((_, sets, oprems, _),
   355               vc_compat_ths), vc_compat_vs), ihyp), vs_ihypt) =>
   356             [REPEAT (rtac allI 1), simp_tac (put_simpset eqvt_ss context) 1,
   357              SUBPROOF (fn {prems = gprems, params, concl, context = ctxt', ...} =>
   358                let
   359                  val (cparams', (pis, z)) =
   360                    chop (length params - length atomTs - 1) (map #2 params) ||>
   361                    (map Thm.term_of #> split_last);
   362                  val params' = map Thm.term_of cparams'
   363                  val sets' = map (apfst (curry subst_bounds (rev params'))) sets;
   364                  val pi_sets = map (fn (t, _) =>
   365                    fold_rev (NominalDatatype.mk_perm []) pis t) sets';
   366                  val (P, ts) = strip_comb (HOLogic.dest_Trueprop (Thm.term_of concl));
   367                  val gprems1 = map_filter (fn (th, t) =>
   368                    if null (preds_of ps t) then SOME th
   369                    else
   370                      map_thm ctxt' (split_conj (K o I) names)
   371                        (etac conjunct1 1) monos NONE th)
   372                    (gprems ~~ oprems);
   373                  val vc_compat_ths' = map2 (fn th => fn p =>
   374                    let
   375                      val th' = gprems1 MRS inst_params thy p th cparams';
   376                      val (h, ts) =
   377                        strip_comb (HOLogic.dest_Trueprop (Thm.concl_of th'))
   378                    in
   379                      Goal.prove ctxt' [] []
   380                        (HOLogic.mk_Trueprop (list_comb (h,
   381                           map (fold_rev (NominalDatatype.mk_perm []) pis) ts)))
   382                        (fn _ => simp_tac (put_simpset HOL_basic_ss ctxt' addsimps
   383                           (fresh_star_bij @ finite_ineq)) 1 THEN rtac th' 1)
   384                    end) vc_compat_ths vc_compat_vs;
   385                  val (vc_compat_ths1, vc_compat_ths2) =
   386                    chop (length vc_compat_ths - length sets) vc_compat_ths';
   387                  val vc_compat_ths1' = map
   388                    (Conv.fconv_rule (Conv.arg_conv (Conv.arg_conv
   389                       (Simplifier.rewrite (put_simpset eqvt_ss ctxt'))))) vc_compat_ths1;
   390                  val (pis', fresh_ths1, fresh_ths2, fresh_ths3, ctxt'') = fold
   391                    (obtain_fresh_name ts sets)
   392                    (map snd sets' ~~ vc_compat_ths2) ([], [], [], [], ctxt');
   393                  fun concat_perm pi1 pi2 =
   394                    let val T = fastype_of pi1
   395                    in if T = fastype_of pi2 then
   396                        Const (@{const_name append}, T --> T --> T) $ pi1 $ pi2
   397                      else pi2
   398                    end;
   399                  val pis'' = fold_rev (concat_perm #> map) pis' pis;
   400                  val ihyp' = inst_params thy vs_ihypt ihyp
   401                    (map (fold_rev (NominalDatatype.mk_perm [])
   402                       (pis' @ pis) #> Thm.global_cterm_of thy) params' @ [Thm.global_cterm_of thy z]);
   403                  fun mk_pi th =
   404                    Simplifier.simplify (put_simpset HOL_basic_ss ctxt' addsimps [@{thm id_apply}]
   405                        addsimprocs [NominalDatatype.perm_simproc])
   406                      (Simplifier.simplify (put_simpset eqvt_ss ctxt')
   407                        (fold_rev (mk_perm_bool o Thm.global_cterm_of thy)
   408                          (pis' @ pis) th));
   409                  val gprems2 = map (fn (th, t) =>
   410                    if null (preds_of ps t) then mk_pi th
   411                    else
   412                      mk_pi (the (map_thm ctxt' (inst_conj_all names ps (rev pis''))
   413                        (inst_conj_all_tac ctxt' (length pis'')) monos (SOME t) th)))
   414                    (gprems ~~ oprems);
   415                  val perm_freshs_freshs' = map (fn (th, (_, T)) =>
   416                    th RS the (AList.lookup op = perm_freshs_freshs T))
   417                      (fresh_ths2 ~~ sets);
   418                  val th = Goal.prove ctxt'' [] []
   419                    (HOLogic.mk_Trueprop (list_comb (P $ hd ts,
   420                      map (fold_rev (NominalDatatype.mk_perm []) pis') (tl ts))))
   421                    (fn _ => EVERY ([simp_tac (put_simpset eqvt_ss ctxt'') 1, rtac ihyp' 1] @
   422                      map (fn th => rtac th 1) fresh_ths3 @
   423                      [REPEAT_DETERM_N (length gprems)
   424                        (simp_tac (put_simpset HOL_basic_ss ctxt''
   425                           addsimps [inductive_forall_def']
   426                           addsimprocs [NominalDatatype.perm_simproc]) 1 THEN
   427                         resolve_tac ctxt'' gprems2 1)]));
   428                  val final = Goal.prove ctxt'' [] [] (Thm.term_of concl)
   429                    (fn _ => cut_facts_tac [th] 1 THEN full_simp_tac (put_simpset HOL_ss ctxt''
   430                      addsimps vc_compat_ths1' @ fresh_ths1 @
   431                        perm_freshs_freshs') 1);
   432                  val final' = Proof_Context.export ctxt'' ctxt' [final];
   433                in resolve_tac ctxt' final' 1 end) context 1])
   434                  (prems ~~ thss ~~ vc_compat' ~~ ihyps ~~ prems'')))
   435         in
   436           cut_facts_tac [th] 1 THEN REPEAT (etac conjE 1) THEN
   437           REPEAT (REPEAT (resolve_tac ctxt' [conjI, impI] 1) THEN
   438             etac impE 1 THEN atac 1 THEN REPEAT (etac @{thm allE_Nil} 1) THEN
   439             asm_full_simp_tac ctxt 1)
   440         end) |>
   441         fresh_postprocess ctxt' |>
   442         singleton (Proof_Context.export ctxt' ctxt);
   443 
   444   in
   445     ctxt'' |>
   446     Proof.theorem NONE (fn thss => fn ctxt =>  (* FIXME ctxt/ctxt' should be called lthy/lthy' *)
   447       let
   448         val rec_name = space_implode "_" (map Long_Name.base_name names);
   449         val rec_qualified = Binding.qualify false rec_name;
   450         val ind_case_names = Rule_Cases.case_names induct_cases;
   451         val induct_cases' = Inductive.partition_rules' raw_induct
   452           (intrs ~~ induct_cases); 
   453         val thss' = map (map (atomize_intr ctxt)) thss;
   454         val thsss = Inductive.partition_rules' raw_induct (intrs ~~ thss');
   455         val strong_raw_induct =
   456           mk_ind_proof ctxt thss' |> Inductive.rulify ctxt;
   457         val strong_induct_atts =
   458           map (Attrib.internal o K)
   459             [ind_case_names, Rule_Cases.consumes (~ (Thm.nprems_of strong_raw_induct))];
   460         val strong_induct =
   461           if length names > 1 then strong_raw_induct
   462           else strong_raw_induct RSN (2, rev_mp);
   463         val (induct_name, inducts_name) =
   464           case alt_name of
   465             NONE => (rec_qualified (Binding.name "strong_induct"),
   466                      rec_qualified (Binding.name "strong_inducts"))
   467           | SOME s => (Binding.name s, Binding.name (s ^ "s"));
   468         val ((_, [strong_induct']), ctxt') = ctxt |> Local_Theory.note
   469           ((induct_name, strong_induct_atts), [strong_induct]);
   470         val strong_inducts =
   471           Project_Rule.projects ctxt' (1 upto length names) strong_induct'
   472       in
   473         ctxt' |>
   474         Local_Theory.notes [((inducts_name, []),
   475           strong_inducts |> map (fn th => ([th],
   476             [Attrib.internal (K ind_case_names),
   477              Attrib.internal (K (Rule_Cases.consumes (1 - Thm.nprems_of th)))])))] |> snd
   478       end)
   479       (map (map (rulify_term thy #> rpair [])) vc_compat)
   480   end;
   481 
   482 
   483 (* outer syntax *)
   484 
   485 val _ =
   486   Outer_Syntax.local_theory_to_proof @{command_keyword nominal_inductive2}
   487     "prove strong induction theorem for inductive predicate involving nominal datatypes"
   488     (Parse.xname -- 
   489      Scan.option (@{keyword "("} |-- Parse.!!! (Parse.name --| @{keyword ")"})) --
   490      (Scan.optional (@{keyword "avoids"} |-- Parse.enum1 "|" (Parse.name --
   491       (@{keyword ":"} |-- Parse.and_list1 Parse.term))) []) >> (fn ((name, rule_name), avoids) =>
   492         prove_strong_ind name rule_name avoids));
   493 
   494 end