src/HOL/Nominal/nominal_inductive.ML
author wenzelm
Fri Nov 13 21:11:15 2009 +0100 (2009-11-13)
changeset 33671 4b0f2599ed48
parent 33670 02b7738aef6a
child 33772 b6a1feca2ac2
permissions -rw-r--r--
modernized structure Local_Theory;
berghofe@22313
     1
(*  Title:      HOL/Nominal/nominal_inductive.ML
berghofe@22313
     2
    Author:     Stefan Berghofer, TU Muenchen
berghofe@22313
     3
berghofe@22530
     4
Infrastructure for proving equivariance and strong induction theorems
berghofe@22530
     5
for inductive predicates involving nominal datatypes.
berghofe@22313
     6
*)
berghofe@22313
     7
berghofe@22313
     8
signature NOMINAL_INDUCTIVE =
berghofe@22313
     9
sig
berghofe@30087
    10
  val prove_strong_ind: string -> (string * string list) list -> local_theory -> Proof.state
berghofe@30087
    11
  val prove_eqvt: string -> string list -> local_theory -> local_theory
berghofe@22313
    12
end
berghofe@22313
    13
berghofe@22313
    14
structure NominalInductive : NOMINAL_INDUCTIVE =
berghofe@22313
    15
struct
berghofe@22313
    16
berghofe@24570
    17
val inductive_forall_name = "HOL.induct_forall";
berghofe@24570
    18
val inductive_forall_def = thm "induct_forall_def";
berghofe@24570
    19
val inductive_atomize = thms "induct_atomize";
berghofe@24570
    20
val inductive_rulify = thms "induct_rulify";
berghofe@24570
    21
berghofe@24570
    22
fun rulify_term thy = MetaSimplifier.rewrite_term thy inductive_rulify [];
berghofe@24570
    23
berghofe@24570
    24
val atomize_conv =
berghofe@24570
    25
  MetaSimplifier.rewrite_cterm (true, false, false) (K (K NONE))
berghofe@24570
    26
    (HOL_basic_ss addsimps inductive_atomize);
berghofe@24570
    27
val atomize_intr = Conv.fconv_rule (Conv.prems_conv ~1 atomize_conv);
wenzelm@24832
    28
fun atomize_induct ctxt = Conv.fconv_rule (Conv.prems_conv ~1
wenzelm@26568
    29
  (Conv.params_conv ~1 (K (Conv.prems_conv ~1 atomize_conv)) ctxt));
berghofe@24570
    30
haftmann@33049
    31
fun preds_of ps t = inter (op = o apfst dest_Free) (Term.add_frees t []) ps;
berghofe@30087
    32
berghofe@22530
    33
val fresh_prod = thm "fresh_prod";
berghofe@22530
    34
berghofe@24570
    35
val perm_bool = mk_meta_eq (thm "perm_bool");
berghofe@22313
    36
val perm_boolI = thm "perm_boolI";
berghofe@22313
    37
val (_, [perm_boolI_pi, _]) = Drule.strip_comb (snd (Thm.dest_comb
berghofe@22313
    38
  (Drule.strip_imp_concl (cprop_of perm_boolI))));
berghofe@22313
    39
berghofe@25824
    40
fun mk_perm_bool pi th = th RS Drule.cterm_instantiate
berghofe@25824
    41
  [(perm_boolI_pi, pi)] perm_boolI;
berghofe@25824
    42
berghofe@24570
    43
fun mk_perm_bool_simproc names = Simplifier.simproc_i
berghofe@24570
    44
  (theory_of_thm perm_bool) "perm_bool" [@{term "perm pi x"}] (fn thy => fn ss =>
berghofe@24570
    45
    fn Const ("Nominal.perm", _) $ _ $ t =>
berghofe@24570
    46
         if the_default "" (try (head_of #> dest_Const #> fst) t) mem names
berghofe@24570
    47
         then SOME perm_bool else NONE
berghofe@24570
    48
     | _ => NONE);
berghofe@24570
    49
berghofe@22313
    50
fun transp ([] :: _) = []
berghofe@22313
    51
  | transp xs = map hd xs :: transp (map tl xs);
berghofe@22313
    52
berghofe@22530
    53
fun add_binders thy i (t as (_ $ _)) bs = (case strip_comb t of
berghofe@22530
    54
      (Const (s, T), ts) => (case strip_type T of
berghofe@22530
    55
        (Ts, Type (tname, _)) =>
haftmann@31938
    56
          (case NominalDatatype.get_nominal_datatype thy tname of
berghofe@22530
    57
             NONE => fold (add_binders thy i) ts bs
berghofe@22530
    58
           | SOME {descr, index, ...} => (case AList.lookup op =
berghofe@22530
    59
                 (#3 (the (AList.lookup op = descr index))) s of
berghofe@22530
    60
               NONE => fold (add_binders thy i) ts bs
berghofe@22530
    61
             | SOME cargs => fst (fold (fn (xs, x) => fn (bs', cargs') =>
berghofe@22530
    62
                 let val (cargs1, (u, _) :: cargs2) = chop (length xs) cargs'
berghofe@22530
    63
                 in (add_binders thy i u
berghofe@22530
    64
                   (fold (fn (u, T) =>
berghofe@22530
    65
                      if exists (fn j => j < i) (loose_bnos u) then I
berghofe@22530
    66
                      else insert (op aconv o pairself fst)
berghofe@22530
    67
                        (incr_boundvars (~i) u, T)) cargs1 bs'), cargs2)
berghofe@22530
    68
                 end) cargs (bs, ts ~~ Ts))))
berghofe@22530
    69
      | _ => fold (add_binders thy i) ts bs)
berghofe@22530
    70
    | (u, ts) => add_binders thy i u (fold (add_binders thy i) ts bs))
berghofe@22530
    71
  | add_binders thy i (Abs (_, _, t)) bs = add_binders thy (i + 1) t bs
berghofe@22530
    72
  | add_binders thy i _ bs = bs;
berghofe@22530
    73
berghofe@24570
    74
fun split_conj f names (Const ("op &", _) $ p $ q) _ = (case head_of p of
berghofe@24570
    75
      Const (name, _) =>
berghofe@24570
    76
        if name mem names then SOME (f p q) else NONE
berghofe@24570
    77
    | _ => NONE)
berghofe@24570
    78
  | split_conj _ _ _ _ = NONE;
berghofe@24570
    79
berghofe@24570
    80
fun strip_all [] t = t
berghofe@24570
    81
  | strip_all (_ :: xs) (Const ("All", _) $ Abs (s, T, t)) = strip_all xs t;
berghofe@24570
    82
berghofe@24570
    83
(*********************************************************************)
berghofe@24570
    84
(* maps  R ... & (ALL pi_1 ... pi_n z. P z (pi_1 o ... o pi_n o t))  *)
berghofe@27722
    85
(* or    ALL pi_1 ... pi_n z. P z (pi_1 o ... o pi_n o t)            *)
berghofe@27722
    86
(* to    R ... & id (ALL z. P z (pi_1 o ... o pi_n o t))             *)
berghofe@27722
    87
(* or    id (ALL z. P z (pi_1 o ... o pi_n o t))                     *)
berghofe@24570
    88
(*                                                                   *)
berghofe@24570
    89
(* where "id" protects the subformula from simplification            *)
berghofe@24570
    90
(*********************************************************************)
berghofe@24570
    91
berghofe@24570
    92
fun inst_conj_all names ps pis (Const ("op &", _) $ p $ q) _ =
berghofe@24570
    93
      (case head_of p of
berghofe@24570
    94
         Const (name, _) =>
berghofe@24570
    95
           if name mem names then SOME (HOLogic.mk_conj (p,
berghofe@24570
    96
             Const ("Fun.id", HOLogic.boolT --> HOLogic.boolT) $
berghofe@24570
    97
               (subst_bounds (pis, strip_all pis q))))
berghofe@24570
    98
           else NONE
berghofe@24570
    99
       | _ => NONE)
berghofe@24570
   100
  | inst_conj_all names ps pis t u =
berghofe@24570
   101
      if member (op aconv) ps (head_of u) then
berghofe@24570
   102
        SOME (Const ("Fun.id", HOLogic.boolT --> HOLogic.boolT) $
berghofe@24570
   103
          (subst_bounds (pis, strip_all pis t)))
berghofe@24570
   104
      else NONE
berghofe@24570
   105
  | inst_conj_all _ _ _ _ _ = NONE;
berghofe@24570
   106
berghofe@24570
   107
fun inst_conj_all_tac k = EVERY
berghofe@24570
   108
  [TRY (EVERY [etac conjE 1, rtac conjI 1, atac 1]),
berghofe@24570
   109
   REPEAT_DETERM_N k (etac allE 1),
haftmann@26359
   110
   simp_tac (HOL_basic_ss addsimps [@{thm id_apply}]) 1];
berghofe@24570
   111
berghofe@24570
   112
fun map_term f t u = (case f t u of
berghofe@24570
   113
      NONE => map_term' f t u | x => x)
berghofe@24570
   114
and map_term' f (t $ u) (t' $ u') = (case (map_term f t t', map_term f u u') of
berghofe@24570
   115
      (NONE, NONE) => NONE
berghofe@24570
   116
    | (SOME t'', NONE) => SOME (t'' $ u)
berghofe@24570
   117
    | (NONE, SOME u'') => SOME (t $ u'')
berghofe@24570
   118
    | (SOME t'', SOME u'') => SOME (t'' $ u''))
berghofe@24570
   119
  | map_term' f (Abs (s, T, t)) (Abs (s', T', t')) = (case map_term f t t' of
berghofe@24570
   120
      NONE => NONE
berghofe@24570
   121
    | SOME t'' => SOME (Abs (s, T, t'')))
berghofe@24570
   122
  | map_term' _ _ _ = NONE;
berghofe@24570
   123
berghofe@24570
   124
(*********************************************************************)
berghofe@24570
   125
(*         Prove  F[f t]  from  F[t],  where F is monotone           *)
berghofe@24570
   126
(*********************************************************************)
berghofe@24570
   127
berghofe@24570
   128
fun map_thm ctxt f tac monos opt th =
berghofe@24570
   129
  let
berghofe@24570
   130
    val prop = prop_of th;
berghofe@24570
   131
    fun prove t =
berghofe@24570
   132
      Goal.prove ctxt [] [] t (fn _ =>
berghofe@24570
   133
        EVERY [cut_facts_tac [th] 1, etac rev_mp 1,
berghofe@24570
   134
          REPEAT_DETERM (FIRSTGOAL (resolve_tac monos)),
berghofe@24570
   135
          REPEAT_DETERM (rtac impI 1 THEN (atac 1 ORELSE tac))])
berghofe@24570
   136
  in Option.map prove (map_term f prop (the_default prop opt)) end;
berghofe@24570
   137
berghofe@27352
   138
val eta_contract_cterm = Thm.dest_arg o Thm.cprop_of o Thm.eta_conversion;
berghofe@27352
   139
berghofe@25824
   140
fun first_order_matchs pats objs = Thm.first_order_match
berghofe@27352
   141
  (eta_contract_cterm (Conjunction.mk_conjunction_balanced pats),
berghofe@27352
   142
   eta_contract_cterm (Conjunction.mk_conjunction_balanced objs));
berghofe@25824
   143
berghofe@25824
   144
fun first_order_mrs ths th = ths MRS
berghofe@25824
   145
  Thm.instantiate (first_order_matchs (cprems_of th) (map cprop_of ths)) th;
berghofe@25824
   146
berghofe@30087
   147
fun prove_strong_ind s avoids ctxt =
berghofe@22313
   148
  let
berghofe@30087
   149
    val thy = ProofContext.theory_of ctxt;
berghofe@25824
   150
    val ({names, ...}, {raw_induct, intrs, elims, ...}) =
haftmann@31723
   151
      Inductive.the_inductive ctxt (Sign.intern_const thy s);
haftmann@31723
   152
    val ind_params = Inductive.params_of raw_induct;
wenzelm@24832
   153
    val raw_induct = atomize_induct ctxt raw_induct;
berghofe@25824
   154
    val elims = map (atomize_induct ctxt) elims;
haftmann@31723
   155
    val monos = Inductive.get_monos ctxt;
urbanc@24571
   156
    val eqvt_thms = NominalThmDecls.get_eqvt_thms ctxt;
haftmann@33040
   157
    val _ = (case subtract (op =) (fold (Term.add_const_names o Thm.prop_of) eqvt_thms []) names of
berghofe@22788
   158
        [] => ()
berghofe@22788
   159
      | xs => error ("Missing equivariance theorem for predicate(s): " ^
berghofe@22788
   160
          commas_quote xs));
wenzelm@33368
   161
    val induct_cases = map fst (fst (Rule_Cases.get (the
wenzelm@24861
   162
      (Induct.lookup_inductP ctxt (hd names)))));
berghofe@30087
   163
    val ([raw_induct'], ctxt') = Variable.import_terms false [prop_of raw_induct] ctxt;
berghofe@22530
   164
    val concls = raw_induct' |> Logic.strip_imp_concl |> HOLogic.dest_Trueprop |>
berghofe@22530
   165
      HOLogic.dest_conj |> map (HOLogic.dest_imp ##> strip_comb);
berghofe@22530
   166
    val ps = map (fst o snd) concls;
berghofe@22530
   167
berghofe@22530
   168
    val _ = (case duplicates (op = o pairself fst) avoids of
berghofe@22530
   169
        [] => ()
berghofe@22530
   170
      | xs => error ("Duplicate case names: " ^ commas_quote (map fst xs)));
berghofe@22530
   171
    val _ = assert_all (null o duplicates op = o snd) avoids
berghofe@22530
   172
      (fn (a, _) => error ("Duplicate variable names for case " ^ quote a));
haftmann@33040
   173
    val _ = (case subtract (op =) induct_cases (map fst avoids) of
berghofe@22530
   174
        [] => ()
berghofe@22530
   175
      | xs => error ("No such case(s) in inductive definition: " ^ commas_quote xs));
berghofe@25824
   176
    val avoids' = if null induct_cases then replicate (length intrs) ("", [])
berghofe@25824
   177
      else map (fn name =>
berghofe@25824
   178
        (name, the_default [] (AList.lookup op = avoids name))) induct_cases;
berghofe@22530
   179
    fun mk_avoids params (name, ps) =
berghofe@22530
   180
      let val k = length params - 1
berghofe@22530
   181
      in map (fn x => case find_index (equal x o fst) params of
berghofe@22530
   182
          ~1 => error ("No such variable in case " ^ quote name ^
berghofe@22530
   183
            " of inductive definition: " ^ quote x)
berghofe@22530
   184
        | i => (Bound (k - i), snd (nth params i))) ps
berghofe@22530
   185
      end;
berghofe@22530
   186
berghofe@22530
   187
    val prems = map (fn (prem, avoid) =>
berghofe@22530
   188
      let
berghofe@22530
   189
        val prems = map (incr_boundvars 1) (Logic.strip_assums_hyp prem);
berghofe@22530
   190
        val concl = incr_boundvars 1 (Logic.strip_assums_concl prem);
berghofe@22530
   191
        val params = Logic.strip_params prem
berghofe@22530
   192
      in
berghofe@22530
   193
        (params,
berghofe@22530
   194
         fold (add_binders thy 0) (prems @ [concl]) [] @
berghofe@22530
   195
           map (apfst (incr_boundvars 1)) (mk_avoids params avoid),
berghofe@22530
   196
         prems, strip_comb (HOLogic.dest_Trueprop concl))
berghofe@22530
   197
      end) (Logic.strip_imp_prems raw_induct' ~~ avoids');
berghofe@22530
   198
berghofe@22530
   199
    val atomTs = distinct op = (maps (map snd o #2) prems);
berghofe@22530
   200
    val ind_sort = if null atomTs then HOLogic.typeS
berghofe@22530
   201
      else Sign.certify_sort thy (map (fn T => Sign.intern_class thy
wenzelm@30364
   202
        ("fs_" ^ Long_Name.base_name (fst (dest_Type T)))) atomTs);
berghofe@30087
   203
    val ([fs_ctxt_tyname], _) = Name.variants ["'n"] (Variable.names_of ctxt');
berghofe@30087
   204
    val ([fs_ctxt_name], ctxt'') = Variable.variant_fixes ["z"] ctxt';
berghofe@22530
   205
    val fsT = TFree (fs_ctxt_tyname, ind_sort);
berghofe@22530
   206
berghofe@24570
   207
    val inductive_forall_def' = Drule.instantiate'
berghofe@24570
   208
      [SOME (ctyp_of thy fsT)] [] inductive_forall_def;
berghofe@24570
   209
berghofe@22530
   210
    fun lift_pred' t (Free (s, T)) ts =
berghofe@22530
   211
      list_comb (Free (s, fsT --> T), t :: ts);
berghofe@22530
   212
    val lift_pred = lift_pred' (Bound 0);
berghofe@22530
   213
berghofe@24570
   214
    fun lift_prem (t as (f $ u)) =
berghofe@22530
   215
          let val (p, ts) = strip_comb t
berghofe@22530
   216
          in
berghofe@22530
   217
            if p mem ps then
berghofe@24570
   218
              Const (inductive_forall_name,
berghofe@24570
   219
                (fsT --> HOLogic.boolT) --> HOLogic.boolT) $
berghofe@24570
   220
                  Abs ("z", fsT, lift_pred p (map (incr_boundvars 1) ts))
berghofe@22530
   221
            else lift_prem f $ lift_prem u
berghofe@22530
   222
          end
berghofe@22530
   223
      | lift_prem (Abs (s, T, t)) = Abs (s, T, lift_prem t)
berghofe@22530
   224
      | lift_prem t = t;
berghofe@22530
   225
berghofe@22530
   226
    fun mk_distinct [] = []
wenzelm@32952
   227
      | mk_distinct ((x, T) :: xs) = map_filter (fn (y, U) =>
berghofe@22530
   228
          if T = U then SOME (HOLogic.mk_Trueprop
berghofe@22530
   229
            (HOLogic.mk_not (HOLogic.eq_const T $ x $ y)))
berghofe@22530
   230
          else NONE) xs @ mk_distinct xs;
berghofe@22530
   231
berghofe@22530
   232
    fun mk_fresh (x, T) = HOLogic.mk_Trueprop
haftmann@31938
   233
      (NominalDatatype.fresh_const T fsT $ x $ Bound 0);
berghofe@22530
   234
berghofe@22530
   235
    val (prems', prems'') = split_list (map (fn (params, bvars, prems, (p, ts)) =>
berghofe@22530
   236
      let
berghofe@22530
   237
        val params' = params @ [("y", fsT)];
berghofe@22530
   238
        val prem = Logic.list_implies
berghofe@22530
   239
          (map mk_fresh bvars @ mk_distinct bvars @
berghofe@22530
   240
           map (fn prem =>
berghofe@30087
   241
             if null (preds_of ps prem) then prem
berghofe@22530
   242
             else lift_prem prem) prems,
berghofe@22530
   243
           HOLogic.mk_Trueprop (lift_pred p ts));
wenzelm@29276
   244
        val vs = map (Var o apfst (rpair 0)) (Term.rename_wrt_term prem params')
berghofe@22530
   245
      in
berghofe@22530
   246
        (list_all (params', prem), (rev vs, subst_bounds (vs, prem)))
berghofe@22530
   247
      end) prems);
berghofe@22530
   248
berghofe@22530
   249
    val ind_vars =
berghofe@22530
   250
      (DatatypeProp.indexify_names (replicate (length atomTs) "pi") ~~
berghofe@22530
   251
       map NominalAtoms.mk_permT atomTs) @ [("z", fsT)];
berghofe@22530
   252
    val ind_Ts = rev (map snd ind_vars);
berghofe@22530
   253
berghofe@22530
   254
    val concl = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@22530
   255
      (map (fn (prem, (p, ts)) => HOLogic.mk_imp (prem,
berghofe@22530
   256
        HOLogic.list_all (ind_vars, lift_pred p
haftmann@31938
   257
          (map (fold_rev (NominalDatatype.mk_perm ind_Ts)
berghofe@22530
   258
            (map Bound (length atomTs downto 1))) ts)))) concls));
berghofe@22530
   259
berghofe@22530
   260
    val concl' = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@22530
   261
      (map (fn (prem, (p, ts)) => HOLogic.mk_imp (prem,
berghofe@22530
   262
        lift_pred' (Free (fs_ctxt_name, fsT)) p ts)) concls));
berghofe@22530
   263
berghofe@22530
   264
    val vc_compat = map (fn (params, bvars, prems, (p, ts)) =>
berghofe@22530
   265
      map (fn q => list_all (params, incr_boundvars ~1 (Logic.list_implies
wenzelm@32952
   266
          (map_filter (fn prem =>
berghofe@30087
   267
             if null (preds_of ps prem) then SOME prem
berghofe@24570
   268
             else map_term (split_conj (K o I) names) prem prem) prems, q))))
berghofe@22530
   269
        (mk_distinct bvars @
berghofe@22530
   270
         maps (fn (t, T) => map (fn (u, U) => HOLogic.mk_Trueprop
haftmann@31938
   271
           (NominalDatatype.fresh_const U T $ u $ t)) bvars)
berghofe@22530
   272
             (ts ~~ binder_types (fastype_of p)))) prems;
berghofe@22530
   273
wenzelm@26343
   274
    val perm_pi_simp = PureThy.get_thms thy "perm_pi_simp";
wenzelm@26343
   275
    val pt2_atoms = map (fn aT => PureThy.get_thm thy
wenzelm@30364
   276
      ("pt_" ^ Long_Name.base_name (fst (dest_Type aT)) ^ "2")) atomTs;
berghofe@27352
   277
    val eqvt_ss = Simplifier.theory_context thy HOL_basic_ss
berghofe@27352
   278
      addsimps (eqvt_thms @ perm_pi_simp @ pt2_atoms)
berghofe@27352
   279
      addsimprocs [mk_perm_bool_simproc ["Fun.id"],
berghofe@27352
   280
        NominalPermeq.perm_simproc_app, NominalPermeq.perm_simproc_fun];
wenzelm@26343
   281
    val fresh_bij = PureThy.get_thms thy "fresh_bij";
wenzelm@26343
   282
    val perm_bij = PureThy.get_thms thy "perm_bij";
wenzelm@26343
   283
    val fs_atoms = map (fn aT => PureThy.get_thm thy
wenzelm@30364
   284
      ("fs_" ^ Long_Name.base_name (fst (dest_Type aT)) ^ "1")) atomTs;
wenzelm@26343
   285
    val exists_fresh' = PureThy.get_thms thy "exists_fresh'";
wenzelm@26343
   286
    val fresh_atm = PureThy.get_thms thy "fresh_atm";
berghofe@27449
   287
    val swap_simps = PureThy.get_thms thy "swap_simps";
wenzelm@26343
   288
    val perm_fresh_fresh = PureThy.get_thms thy "perm_fresh_fresh";
berghofe@22530
   289
berghofe@22530
   290
    fun obtain_fresh_name ts T (freshs1, freshs2, ctxt) =
berghofe@22530
   291
      let
berghofe@27722
   292
        (** protect terms to avoid that fresh_prod interferes with  **)
berghofe@22530
   293
        (** pairs used in introduction rules of inductive predicate **)
berghofe@22530
   294
        fun protect t =
berghofe@22530
   295
          let val T = fastype_of t in Const ("Fun.id", T --> T) $ t end;
berghofe@22530
   296
        val p = foldr1 HOLogic.mk_prod (map protect ts @ freshs1);
berghofe@22530
   297
        val ex = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop
berghofe@22530
   298
            (HOLogic.exists_const T $ Abs ("x", T,
haftmann@31938
   299
              NominalDatatype.fresh_const T (fastype_of p) $
berghofe@22530
   300
                Bound 0 $ p)))
berghofe@22530
   301
          (fn _ => EVERY
berghofe@22530
   302
            [resolve_tac exists_fresh' 1,
berghofe@27722
   303
             resolve_tac fs_atoms 1]);
wenzelm@32202
   304
        val (([(_, cx)], ths), ctxt') = Obtain.result
berghofe@22530
   305
          (fn _ => EVERY
berghofe@22530
   306
            [etac exE 1,
berghofe@22530
   307
             full_simp_tac (HOL_ss addsimps (fresh_prod :: fresh_atm)) 1,
haftmann@26359
   308
             full_simp_tac (HOL_basic_ss addsimps [@{thm id_apply}]) 1,
berghofe@22530
   309
             REPEAT (etac conjE 1)])
berghofe@22530
   310
          [ex] ctxt
berghofe@22530
   311
      in (freshs1 @ [term_of cx], freshs2 @ ths, ctxt') end;
berghofe@22530
   312
berghofe@30087
   313
    fun mk_ind_proof ctxt' thss =
berghofe@30087
   314
      Goal.prove ctxt' [] prems' concl' (fn {prems = ihyps, context = ctxt} =>
berghofe@22530
   315
        let val th = Goal.prove ctxt [] [] concl (fn {context, ...} =>
berghofe@22530
   316
          rtac raw_induct 1 THEN
berghofe@22530
   317
          EVERY (maps (fn ((((_, bvars, oprems, _), vc_compat_ths), ihyp), (vs, ihypt)) =>
berghofe@22530
   318
            [REPEAT (rtac allI 1), simp_tac eqvt_ss 1,
berghofe@22530
   319
             SUBPROOF (fn {prems = gprems, params, concl, context = ctxt', ...} =>
berghofe@22530
   320
               let
berghofe@22530
   321
                 val (params', (pis, z)) =
wenzelm@32202
   322
                   chop (length params - length atomTs - 1) (map (term_of o #2) params) ||>
berghofe@22530
   323
                   split_last;
berghofe@22530
   324
                 val bvars' = map
berghofe@22530
   325
                   (fn (Bound i, T) => (nth params' (length params' - i), T)
berghofe@22530
   326
                     | (t, T) => (t, T)) bvars;
berghofe@22530
   327
                 val pi_bvars = map (fn (t, _) =>
haftmann@31938
   328
                   fold_rev (NominalDatatype.mk_perm []) pis t) bvars';
berghofe@22530
   329
                 val (P, ts) = strip_comb (HOLogic.dest_Trueprop (term_of concl));
berghofe@22530
   330
                 val (freshs1, freshs2, ctxt'') = fold
berghofe@22530
   331
                   (obtain_fresh_name (ts @ pi_bvars))
berghofe@22530
   332
                   (map snd bvars') ([], [], ctxt');
haftmann@31938
   333
                 val freshs2' = NominalDatatype.mk_not_sym freshs2;
haftmann@31938
   334
                 val pis' = map NominalDatatype.perm_of_pair (pi_bvars ~~ freshs1);
berghofe@24570
   335
                 fun concat_perm pi1 pi2 =
berghofe@24570
   336
                   let val T = fastype_of pi1
berghofe@24570
   337
                   in if T = fastype_of pi2 then
berghofe@24570
   338
                       Const ("List.append", T --> T --> T) $ pi1 $ pi2
berghofe@24570
   339
                     else pi2
berghofe@24570
   340
                   end;
berghofe@24570
   341
                 val pis'' = fold (concat_perm #> map) pis' pis;
berghofe@22530
   342
                 val env = Pattern.first_order_match thy (ihypt, prop_of ihyp)
berghofe@22530
   343
                   (Vartab.empty, Vartab.empty);
berghofe@22530
   344
                 val ihyp' = Thm.instantiate ([], map (pairself (cterm_of thy))
wenzelm@32035
   345
                   (map (Envir.subst_term env) vs ~~
haftmann@31938
   346
                    map (fold_rev (NominalDatatype.mk_perm [])
berghofe@22530
   347
                      (rev pis' @ pis)) params' @ [z])) ihyp;
berghofe@24570
   348
                 fun mk_pi th =
haftmann@26359
   349
                   Simplifier.simplify (HOL_basic_ss addsimps [@{thm id_apply}]
haftmann@31938
   350
                       addsimprocs [NominalDatatype.perm_simproc])
berghofe@24570
   351
                     (Simplifier.simplify eqvt_ss
berghofe@25824
   352
                       (fold_rev (mk_perm_bool o cterm_of thy)
berghofe@25824
   353
                         (rev pis' @ pis) th));
berghofe@24570
   354
                 val (gprems1, gprems2) = split_list
berghofe@24570
   355
                   (map (fn (th, t) =>
berghofe@30087
   356
                      if null (preds_of ps t) then (SOME th, mk_pi th)
berghofe@24570
   357
                      else
berghofe@24570
   358
                        (map_thm ctxt (split_conj (K o I) names)
berghofe@24570
   359
                           (etac conjunct1 1) monos NONE th,
berghofe@24570
   360
                         mk_pi (the (map_thm ctxt (inst_conj_all names ps (rev pis''))
berghofe@24570
   361
                           (inst_conj_all_tac (length pis'')) monos (SOME t) th))))
wenzelm@32952
   362
                      (gprems ~~ oprems)) |>> map_filter I;
berghofe@22530
   363
                 val vc_compat_ths' = map (fn th =>
berghofe@22530
   364
                   let
berghofe@25824
   365
                     val th' = first_order_mrs gprems1 th;
berghofe@22530
   366
                     val (bop, lhs, rhs) = (case concl_of th' of
berghofe@22530
   367
                         _ $ (fresh $ lhs $ rhs) =>
berghofe@22530
   368
                           (fn t => fn u => fresh $ t $ u, lhs, rhs)
berghofe@22530
   369
                       | _ $ (_ $ (_ $ lhs $ rhs)) =>
berghofe@22530
   370
                           (curry (HOLogic.mk_not o HOLogic.mk_eq), lhs, rhs));
berghofe@22530
   371
                     val th'' = Goal.prove ctxt'' [] [] (HOLogic.mk_Trueprop
haftmann@31938
   372
                         (bop (fold_rev (NominalDatatype.mk_perm []) pis lhs)
haftmann@31938
   373
                            (fold_rev (NominalDatatype.mk_perm []) pis rhs)))
berghofe@22530
   374
                       (fn _ => simp_tac (HOL_basic_ss addsimps
berghofe@22530
   375
                          (fresh_bij @ perm_bij)) 1 THEN rtac th' 1)
berghofe@22530
   376
                   in Simplifier.simplify (eqvt_ss addsimps fresh_atm) th'' end)
berghofe@22530
   377
                     vc_compat_ths;
haftmann@31938
   378
                 val vc_compat_ths'' = NominalDatatype.mk_not_sym vc_compat_ths';
berghofe@27449
   379
                 (** Since swap_simps simplifies (pi :: 'a prm) o (x :: 'b) to x **)
berghofe@27449
   380
                 (** we have to pre-simplify the rewrite rules                   **)
berghofe@27449
   381
                 val swap_simps_ss = HOL_ss addsimps swap_simps @
berghofe@27449
   382
                    map (Simplifier.simplify (HOL_ss addsimps swap_simps))
berghofe@22530
   383
                      (vc_compat_ths'' @ freshs2');
berghofe@22530
   384
                 val th = Goal.prove ctxt'' [] []
berghofe@22530
   385
                   (HOLogic.mk_Trueprop (list_comb (P $ hd ts,
haftmann@31938
   386
                     map (fold (NominalDatatype.mk_perm []) pis') (tl ts))))
berghofe@22530
   387
                   (fn _ => EVERY ([simp_tac eqvt_ss 1, rtac ihyp' 1,
berghofe@22530
   388
                     REPEAT_DETERM_N (nprems_of ihyp - length gprems)
berghofe@27449
   389
                       (simp_tac swap_simps_ss 1),
berghofe@22530
   390
                     REPEAT_DETERM_N (length gprems)
berghofe@27847
   391
                       (simp_tac (HOL_basic_ss
berghofe@27847
   392
                          addsimps [inductive_forall_def']
haftmann@31938
   393
                          addsimprocs [NominalDatatype.perm_simproc]) 1 THEN
berghofe@27847
   394
                        resolve_tac gprems2 1)]));
berghofe@22530
   395
                 val final = Goal.prove ctxt'' [] [] (term_of concl)
berghofe@22530
   396
                   (fn _ => cut_facts_tac [th] 1 THEN full_simp_tac (HOL_ss
berghofe@22530
   397
                     addsimps vc_compat_ths'' @ freshs2' @
berghofe@22530
   398
                       perm_fresh_fresh @ fresh_atm) 1);
berghofe@22530
   399
                 val final' = ProofContext.export ctxt'' ctxt' [final];
berghofe@22530
   400
               in resolve_tac final' 1 end) context 1])
berghofe@22530
   401
                 (prems ~~ thss ~~ ihyps ~~ prems'')))
berghofe@22530
   402
        in
berghofe@22530
   403
          cut_facts_tac [th] 1 THEN REPEAT (etac conjE 1) THEN
berghofe@22530
   404
          REPEAT (REPEAT (resolve_tac [conjI, impI] 1) THEN
wenzelm@27228
   405
            etac impE 1 THEN atac 1 THEN REPEAT (etac @{thm allE_Nil} 1) THEN
wenzelm@32149
   406
            asm_full_simp_tac (simpset_of ctxt) 1)
berghofe@30087
   407
        end) |> singleton (ProofContext.export ctxt' ctxt);
berghofe@22530
   408
berghofe@25824
   409
    (** strong case analysis rule **)
berghofe@25824
   410
berghofe@25824
   411
    val cases_prems = map (fn ((name, avoids), rule) =>
berghofe@25824
   412
      let
berghofe@30087
   413
        val ([rule'], ctxt') = Variable.import_terms false [prop_of rule] ctxt;
berghofe@30087
   414
        val prem :: prems = Logic.strip_imp_prems rule';
berghofe@30087
   415
        val concl = Logic.strip_imp_concl rule'
berghofe@25824
   416
      in
berghofe@25824
   417
        (prem,
berghofe@25824
   418
         List.drop (snd (strip_comb (HOLogic.dest_Trueprop prem)), length ind_params),
berghofe@25824
   419
         concl,
berghofe@30087
   420
         fold_map (fn (prem, (_, avoid)) => fn ctxt =>
berghofe@25824
   421
           let
berghofe@25824
   422
             val prems = Logic.strip_assums_hyp prem;
berghofe@25824
   423
             val params = Logic.strip_params prem;
berghofe@25824
   424
             val bnds = fold (add_binders thy 0) prems [] @ mk_avoids params avoid;
berghofe@30087
   425
             fun mk_subst (p as (s, T)) (i, j, ctxt, ps, qs, is, ts) =
berghofe@25824
   426
               if member (op = o apsnd fst) bnds (Bound i) then
berghofe@25824
   427
                 let
berghofe@30087
   428
                   val ([s'], ctxt') = Variable.variant_fixes [s] ctxt;
berghofe@25824
   429
                   val t = Free (s', T)
berghofe@30087
   430
                 in (i + 1, j, ctxt', ps, (t, T) :: qs, i :: is, t :: ts) end
berghofe@30087
   431
               else (i + 1, j + 1, ctxt, p :: ps, qs, is, Bound j :: ts);
berghofe@30087
   432
             val (_, _, ctxt', ps, qs, is, ts) = fold_rev mk_subst params
berghofe@30087
   433
               (0, 0, ctxt, [], [], [], [])
berghofe@25824
   434
           in
berghofe@30087
   435
             ((ps, qs, is, map (curry subst_bounds (rev ts)) prems), ctxt')
berghofe@30087
   436
           end) (prems ~~ avoids) ctxt')
berghofe@25824
   437
      end)
haftmann@31723
   438
        (Inductive.partition_rules' raw_induct (intrs ~~ avoids') ~~
berghofe@30087
   439
         elims);
berghofe@25824
   440
berghofe@25824
   441
    val cases_prems' =
berghofe@30087
   442
      map (fn (prem, args, concl, (prems, _)) =>
berghofe@25824
   443
        let
berghofe@25824
   444
          fun mk_prem (ps, [], _, prems) =
berghofe@25824
   445
                list_all (ps, Logic.list_implies (prems, concl))
berghofe@25824
   446
            | mk_prem (ps, qs, _, prems) =
berghofe@25824
   447
                list_all (ps, Logic.mk_implies
berghofe@25824
   448
                  (Logic.list_implies
berghofe@25824
   449
                    (mk_distinct qs @
berghofe@25824
   450
                     maps (fn (t, T) => map (fn u => HOLogic.mk_Trueprop
haftmann@31938
   451
                      (NominalDatatype.fresh_const T (fastype_of u) $ t $ u))
berghofe@25824
   452
                        args) qs,
berghofe@25824
   453
                     HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@25824
   454
                       (map HOLogic.dest_Trueprop prems))),
berghofe@25824
   455
                   concl))
berghofe@25824
   456
          in map mk_prem prems end) cases_prems;
berghofe@25824
   457
berghofe@27352
   458
    val cases_eqvt_ss = Simplifier.theory_context thy HOL_ss
berghofe@27449
   459
      addsimps eqvt_thms @ swap_simps @ perm_pi_simp
berghofe@27352
   460
      addsimprocs [NominalPermeq.perm_simproc_app,
berghofe@27352
   461
        NominalPermeq.perm_simproc_fun];
berghofe@27352
   462
berghofe@27352
   463
    val simp_fresh_atm = map
berghofe@27352
   464
      (Simplifier.simplify (HOL_basic_ss addsimps fresh_atm));
berghofe@25824
   465
berghofe@30087
   466
    fun mk_cases_proof ((((name, thss), elim), (prem, args, concl, (prems, ctxt'))),
berghofe@25824
   467
        prems') =
berghofe@30087
   468
      (name, Goal.prove ctxt' [] (prem :: prems') concl
wenzelm@26711
   469
        (fn {prems = hyp :: hyps, context = ctxt1} =>
berghofe@25824
   470
        EVERY (rtac (hyp RS elim) 1 ::
berghofe@25824
   471
          map (fn (((_, vc_compat_ths), case_hyp), (_, qs, is, _)) =>
berghofe@25824
   472
            SUBPROOF (fn {prems = case_hyps, params, context = ctxt2, concl, ...} =>
berghofe@25824
   473
              if null qs then
berghofe@25824
   474
                rtac (first_order_mrs case_hyps case_hyp) 1
berghofe@25824
   475
              else
berghofe@25824
   476
                let
wenzelm@32202
   477
                  val params' = map (term_of o #2 o nth (rev params)) is;
berghofe@25824
   478
                  val tab = params' ~~ map fst qs;
berghofe@25824
   479
                  val (hyps1, hyps2) = chop (length args) case_hyps;
berghofe@25824
   480
                  (* turns a = t and [x1 # t, ..., xn # t] *)
berghofe@25824
   481
                  (* into [x1 # a, ..., xn # a]            *)
berghofe@25824
   482
                  fun inst_fresh th' ths =
berghofe@25824
   483
                    let val (ths1, ths2) = chop (length qs) ths
berghofe@25824
   484
                    in
berghofe@25824
   485
                      (map (fn th =>
berghofe@25824
   486
                         let
berghofe@25824
   487
                           val (cf, ct) =
berghofe@25824
   488
                             Thm.dest_comb (Thm.dest_arg (cprop_of th));
berghofe@25824
   489
                           val arg_cong' = Drule.instantiate'
berghofe@25824
   490
                             [SOME (ctyp_of_term ct)]
berghofe@25824
   491
                             [NONE, SOME ct, SOME cf] (arg_cong RS iffD2);
berghofe@25824
   492
                           val inst = Thm.first_order_match (ct,
berghofe@25824
   493
                             Thm.dest_arg (Thm.dest_arg (cprop_of th')))
berghofe@25824
   494
                         in [th', th] MRS Thm.instantiate inst arg_cong'
berghofe@25824
   495
                         end) ths1,
berghofe@25824
   496
                       ths2)
berghofe@25824
   497
                    end;
berghofe@25824
   498
                  val (vc_compat_ths1, vc_compat_ths2) =
berghofe@25824
   499
                    chop (length vc_compat_ths - length args * length qs)
berghofe@25824
   500
                      (map (first_order_mrs hyps2) vc_compat_ths);
berghofe@25824
   501
                  val vc_compat_ths' =
haftmann@31938
   502
                    NominalDatatype.mk_not_sym vc_compat_ths1 @
berghofe@25824
   503
                    flat (fst (fold_map inst_fresh hyps1 vc_compat_ths2));
berghofe@25824
   504
                  val (freshs1, freshs2, ctxt3) = fold
berghofe@25824
   505
                    (obtain_fresh_name (args @ map fst qs @ params'))
berghofe@25824
   506
                    (map snd qs) ([], [], ctxt2);
haftmann@31938
   507
                  val freshs2' = NominalDatatype.mk_not_sym freshs2;
haftmann@31938
   508
                  val pis = map (NominalDatatype.perm_of_pair)
berghofe@25824
   509
                    ((freshs1 ~~ map fst qs) @ (params' ~~ freshs1));
berghofe@25824
   510
                  val mk_pis = fold_rev mk_perm_bool (map (cterm_of thy) pis);
berghofe@25824
   511
                  val obj = cterm_of thy (foldr1 HOLogic.mk_conj (map (map_aterms
berghofe@25824
   512
                     (fn x as Free _ =>
berghofe@25824
   513
                           if x mem args then x
berghofe@25824
   514
                           else (case AList.lookup op = tab x of
berghofe@25824
   515
                             SOME y => y
haftmann@31938
   516
                           | NONE => fold_rev (NominalDatatype.mk_perm []) pis x)
berghofe@25824
   517
                       | x => x) o HOLogic.dest_Trueprop o prop_of) case_hyps));
berghofe@25824
   518
                  val inst = Thm.first_order_match (Thm.dest_arg
berghofe@25824
   519
                    (Drule.strip_imp_concl (hd (cprems_of case_hyp))), obj);
berghofe@25824
   520
                  val th = Goal.prove ctxt3 [] [] (term_of concl)
berghofe@25824
   521
                    (fn {context = ctxt4, ...} =>
berghofe@25824
   522
                       rtac (Thm.instantiate inst case_hyp) 1 THEN
berghofe@25824
   523
                       SUBPROOF (fn {prems = fresh_hyps, ...} =>
berghofe@25824
   524
                         let
haftmann@31938
   525
                           val fresh_hyps' = NominalDatatype.mk_not_sym fresh_hyps;
berghofe@27352
   526
                           val case_ss = cases_eqvt_ss addsimps freshs2' @
berghofe@27352
   527
                             simp_fresh_atm (vc_compat_ths' @ fresh_hyps');
berghofe@25824
   528
                           val fresh_fresh_ss = case_ss addsimps perm_fresh_fresh;
berghofe@25824
   529
                           val hyps1' = map
berghofe@25824
   530
                             (mk_pis #> Simplifier.simplify fresh_fresh_ss) hyps1;
berghofe@25824
   531
                           val hyps2' = map
berghofe@25824
   532
                             (mk_pis #> Simplifier.simplify case_ss) hyps2;
berghofe@27449
   533
                           val case_hyps' = hyps1' @ hyps2'
berghofe@25824
   534
                         in
berghofe@27449
   535
                           simp_tac case_ss 1 THEN
berghofe@25824
   536
                           REPEAT_DETERM (TRY (rtac conjI 1) THEN
berghofe@25824
   537
                             resolve_tac case_hyps' 1)
berghofe@25824
   538
                         end) ctxt4 1)
berghofe@25824
   539
                  val final = ProofContext.export ctxt3 ctxt2 [th]
berghofe@25824
   540
                in resolve_tac final 1 end) ctxt1 1)
berghofe@30087
   541
                  (thss ~~ hyps ~~ prems))) |>
berghofe@30087
   542
                  singleton (ProofContext.export ctxt' ctxt))
berghofe@25824
   543
berghofe@22530
   544
  in
berghofe@30087
   545
    ctxt'' |>
berghofe@30087
   546
    Proof.theorem_i NONE (fn thss => fn ctxt =>
berghofe@22530
   547
      let
wenzelm@30364
   548
        val rec_name = space_implode "_" (map Long_Name.base_name names);
wenzelm@30223
   549
        val rec_qualified = Binding.qualify false rec_name;
wenzelm@33368
   550
        val ind_case_names = Rule_Cases.case_names induct_cases;
haftmann@31723
   551
        val induct_cases' = Inductive.partition_rules' raw_induct
berghofe@25824
   552
          (intrs ~~ induct_cases); 
berghofe@25824
   553
        val thss' = map (map atomize_intr) thss;
haftmann@31723
   554
        val thsss = Inductive.partition_rules' raw_induct (intrs ~~ thss');
berghofe@24570
   555
        val strong_raw_induct =
haftmann@31723
   556
          mk_ind_proof ctxt thss' |> Inductive.rulify;
haftmann@31723
   557
        val strong_cases = map (mk_cases_proof ##> Inductive.rulify)
berghofe@25824
   558
          (thsss ~~ elims ~~ cases_prems ~~ cases_prems');
berghofe@22530
   559
        val strong_induct =
berghofe@22530
   560
          if length names > 1 then
wenzelm@33368
   561
            (strong_raw_induct, [ind_case_names, Rule_Cases.consumes 0])
berghofe@22530
   562
          else (strong_raw_induct RSN (2, rev_mp),
wenzelm@33368
   563
            [ind_case_names, Rule_Cases.consumes 1]);
wenzelm@33671
   564
        val ((_, [strong_induct']), ctxt') = ctxt |> Local_Theory.note
berghofe@30087
   565
          ((rec_qualified (Binding.name "strong_induct"),
wenzelm@33670
   566
            map (Attrib.internal o K) (#2 strong_induct)), [#1 strong_induct]);
berghofe@22530
   567
        val strong_inducts =
wenzelm@33670
   568
          Project_Rule.projects ctxt (1 upto length names) strong_induct';
berghofe@22530
   569
      in
berghofe@30087
   570
        ctxt' |>
wenzelm@33671
   571
        Local_Theory.note
berghofe@30087
   572
          ((rec_qualified (Binding.name "strong_inducts"),
berghofe@30087
   573
            [Attrib.internal (K ind_case_names),
wenzelm@33368
   574
             Attrib.internal (K (Rule_Cases.consumes 1))]),
berghofe@30087
   575
           strong_inducts) |> snd |>
wenzelm@33671
   576
        Local_Theory.notes (map (fn ((name, elim), (_, cases)) =>
wenzelm@30435
   577
            ((Binding.qualified_name (Long_Name.qualify (Long_Name.base_name name) "strong_cases"),
wenzelm@33368
   578
              [Attrib.internal (K (Rule_Cases.case_names (map snd cases))),
wenzelm@33368
   579
               Attrib.internal (K (Rule_Cases.consumes 1))]), [([elim], [])]))
berghofe@30087
   580
          (strong_cases ~~ induct_cases')) |> snd
berghofe@30087
   581
      end)
berghofe@24570
   582
      (map (map (rulify_term thy #> rpair [])) vc_compat)
berghofe@22530
   583
  end;
berghofe@22530
   584
berghofe@30087
   585
fun prove_eqvt s xatoms ctxt =
berghofe@22530
   586
  let
berghofe@30087
   587
    val thy = ProofContext.theory_of ctxt;
berghofe@22788
   588
    val ({names, ...}, {raw_induct, intrs, elims, ...}) =
haftmann@31723
   589
      Inductive.the_inductive ctxt (Sign.intern_const thy s);
wenzelm@24832
   590
    val raw_induct = atomize_induct ctxt raw_induct;
wenzelm@24832
   591
    val elims = map (atomize_induct ctxt) elims;
berghofe@24570
   592
    val intrs = map atomize_intr intrs;
haftmann@31723
   593
    val monos = Inductive.get_monos ctxt;
haftmann@31723
   594
    val intrs' = Inductive.unpartition_rules intrs
berghofe@22788
   595
      (map (fn (((s, ths), (_, k)), th) =>
haftmann@31723
   596
           (s, ths ~~ Inductive.infer_intro_vars th k ths))
haftmann@31723
   597
         (Inductive.partition_rules raw_induct intrs ~~
haftmann@31723
   598
          Inductive.arities_of raw_induct ~~ elims));
haftmann@31723
   599
    val k = length (Inductive.params_of raw_induct);
berghofe@22730
   600
    val atoms' = NominalAtoms.atoms_of thy;
berghofe@22730
   601
    val atoms =
berghofe@22730
   602
      if null xatoms then atoms' else
berghofe@22730
   603
      let val atoms = map (Sign.intern_type thy) xatoms
berghofe@22730
   604
      in
berghofe@22730
   605
        (case duplicates op = atoms of
berghofe@22730
   606
             [] => ()
berghofe@22730
   607
           | xs => error ("Duplicate atoms: " ^ commas xs);
haftmann@33040
   608
         case subtract (op =) atoms' atoms of
berghofe@22730
   609
             [] => ()
berghofe@22730
   610
           | xs => error ("No such atoms: " ^ commas xs);
berghofe@22730
   611
         atoms)
berghofe@22730
   612
      end;
wenzelm@26343
   613
    val perm_pi_simp = PureThy.get_thms thy "perm_pi_simp";
berghofe@26364
   614
    val eqvt_ss = Simplifier.theory_context thy HOL_basic_ss addsimps
urbanc@24571
   615
      (NominalThmDecls.get_eqvt_thms ctxt @ perm_pi_simp) addsimprocs
berghofe@26364
   616
      [mk_perm_bool_simproc names,
berghofe@26364
   617
       NominalPermeq.perm_simproc_app, NominalPermeq.perm_simproc_fun];
berghofe@30087
   618
    val (([t], [pi]), ctxt') = ctxt |>
berghofe@30087
   619
      Variable.import_terms false [concl_of raw_induct] ||>>
berghofe@30087
   620
      Variable.variant_fixes ["pi"];
berghofe@22313
   621
    val ps = map (fst o HOLogic.dest_imp)
berghofe@22313
   622
      (HOLogic.dest_conj (HOLogic.dest_Trueprop t));
berghofe@30087
   623
    fun eqvt_tac ctxt'' pi (intr, vs) st =
berghofe@22544
   624
      let
berghofe@30087
   625
        fun eqvt_err s =
berghofe@30087
   626
          let val ([t], ctxt''') = Variable.import_terms true [prop_of intr] ctxt
berghofe@30087
   627
          in error ("Could not prove equivariance for introduction rule\n" ^
berghofe@30087
   628
            Syntax.string_of_term ctxt''' t ^ "\n" ^ s)
berghofe@30087
   629
          end;
berghofe@22788
   630
        val res = SUBPROOF (fn {prems, params, ...} =>
berghofe@22788
   631
          let
berghofe@30087
   632
            val prems' = map (fn th => the_default th (map_thm ctxt'
berghofe@24570
   633
              (split_conj (K I) names) (etac conjunct2 1) monos NONE th)) prems;
berghofe@25824
   634
            val prems'' = map (fn th => Simplifier.simplify eqvt_ss
berghofe@25824
   635
              (mk_perm_bool (cterm_of thy pi) th)) prems';
berghofe@22788
   636
            val intr' = Drule.cterm_instantiate (map (cterm_of thy) vs ~~
wenzelm@32202
   637
               map (cterm_of thy o NominalDatatype.mk_perm [] pi o term_of o #2) params)
berghofe@22788
   638
               intr
berghofe@24570
   639
          in (rtac intr' THEN_ALL_NEW (TRY o resolve_tac prems'')) 1
berghofe@30087
   640
          end) ctxt' 1 st
berghofe@22544
   641
      in
berghofe@22544
   642
        case (Seq.pull res handle THM (s, _, _) => eqvt_err s) of
berghofe@22544
   643
          NONE => eqvt_err ("Rule does not match goal\n" ^
berghofe@30087
   644
            Syntax.string_of_term ctxt'' (hd (prems_of st)))
berghofe@22544
   645
        | SOME (th, _) => Seq.single th
berghofe@22544
   646
      end;
berghofe@22313
   647
    val thss = map (fn atom =>
berghofe@25824
   648
      let val pi' = Free (pi, NominalAtoms.mk_permT (Type (atom, [])))
berghofe@22530
   649
      in map (fn th => zero_var_indexes (th RS mp))
berghofe@30087
   650
        (DatatypeAux.split_conj_thm (Goal.prove ctxt' [] []
berghofe@22313
   651
          (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj (map (fn p =>
berghofe@30087
   652
            let
berghofe@30087
   653
              val (h, ts) = strip_comb p;
berghofe@30087
   654
              val (ts1, ts2) = chop k ts
berghofe@30087
   655
            in
berghofe@30087
   656
              HOLogic.mk_imp (p, list_comb (h, ts1 @
haftmann@31938
   657
                map (NominalDatatype.mk_perm [] pi') ts2))
berghofe@30087
   658
            end) ps)))
berghofe@30087
   659
          (fn {context, ...} => EVERY (rtac raw_induct 1 :: map (fn intr_vs =>
berghofe@22788
   660
              full_simp_tac eqvt_ss 1 THEN
berghofe@30087
   661
              eqvt_tac context pi' intr_vs) intrs')) |>
berghofe@30087
   662
          singleton (ProofContext.export ctxt' ctxt)))
berghofe@22544
   663
      end) atoms
berghofe@22544
   664
  in
berghofe@30087
   665
    ctxt |>
wenzelm@33671
   666
    Local_Theory.notes (map (fn (name, ths) =>
wenzelm@30435
   667
        ((Binding.qualified_name (Long_Name.qualify (Long_Name.base_name name) "eqvt"),
berghofe@30087
   668
          [Attrib.internal (K NominalThmDecls.eqvt_add)]), [(ths, [])]))
berghofe@30087
   669
      (names ~~ transp thss)) |> snd
berghofe@22544
   670
  end;
berghofe@22313
   671
berghofe@22313
   672
berghofe@22313
   673
(* outer syntax *)
berghofe@22313
   674
berghofe@22313
   675
local structure P = OuterParse and K = OuterKeyword in
berghofe@22313
   676
wenzelm@27353
   677
val _ = OuterKeyword.keyword "avoids";
wenzelm@24867
   678
wenzelm@24867
   679
val _ =
berghofe@30087
   680
  OuterSyntax.local_theory_to_proof "nominal_inductive"
berghofe@22530
   681
    "prove equivariance and strong induction theorem for inductive predicate involving nominal datatypes" K.thy_goal
berghofe@30087
   682
    (P.xname -- Scan.optional (P.$$$ "avoids" |-- P.and_list1 (P.name --
berghofe@22530
   683
      (P.$$$ ":" |-- Scan.repeat1 P.name))) [] >> (fn (name, avoids) =>
berghofe@30087
   684
        prove_strong_ind name avoids));
berghofe@22313
   685
wenzelm@24867
   686
val _ =
berghofe@30087
   687
  OuterSyntax.local_theory "equivariance"
berghofe@22530
   688
    "prove equivariance for inductive predicate involving nominal datatypes" K.thy_decl
berghofe@30087
   689
    (P.xname -- Scan.optional (P.$$$ "[" |-- P.list1 P.name --| P.$$$ "]") [] >>
berghofe@30087
   690
      (fn (name, atoms) => prove_eqvt name atoms));
berghofe@22530
   691
berghofe@22313
   692
end;
berghofe@22313
   693
berghofe@22313
   694
end