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