src/HOL/Nominal/nominal_inductive.ML
author berghofe
Thu Apr 19 16:38:59 2007 +0200 (2007-04-19)
changeset 22730 8bcc8809ed3b
parent 22544 549615dcd4f2
child 22755 e268f608669a
permissions -rw-r--r--
nominal_inductive no longer proves equivariance.
berghofe@22313
     1
(*  Title:      HOL/Nominal/nominal_inductive.ML
berghofe@22313
     2
    ID:         $Id$
berghofe@22313
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@22313
     4
berghofe@22530
     5
Infrastructure for proving equivariance and strong induction theorems
berghofe@22530
     6
for inductive predicates involving nominal datatypes.
berghofe@22313
     7
*)
berghofe@22313
     8
berghofe@22313
     9
signature NOMINAL_INDUCTIVE =
berghofe@22313
    10
sig
berghofe@22730
    11
  val prove_strong_ind: string -> (string * string list) list -> theory -> Proof.state
berghofe@22730
    12
  val prove_eqvt: string -> string list -> theory -> theory
berghofe@22313
    13
end
berghofe@22313
    14
berghofe@22313
    15
structure NominalInductive : NOMINAL_INDUCTIVE =
berghofe@22313
    16
struct
berghofe@22313
    17
berghofe@22530
    18
val finite_Un = thm "finite_Un";
berghofe@22530
    19
val supp_prod = thm "supp_prod";
berghofe@22530
    20
val fresh_prod = thm "fresh_prod";
berghofe@22530
    21
berghofe@22313
    22
val perm_boolI = thm "perm_boolI";
berghofe@22313
    23
val (_, [perm_boolI_pi, _]) = Drule.strip_comb (snd (Thm.dest_comb
berghofe@22313
    24
  (Drule.strip_imp_concl (cprop_of perm_boolI))));
berghofe@22313
    25
berghofe@22530
    26
val allE_Nil = read_instantiate_sg (the_context()) [("x", "[]")] allE;
berghofe@22530
    27
berghofe@22313
    28
fun transp ([] :: _) = []
berghofe@22313
    29
  | transp xs = map hd xs :: transp (map tl xs);
berghofe@22313
    30
berghofe@22530
    31
fun add_binders thy i (t as (_ $ _)) bs = (case strip_comb t of
berghofe@22530
    32
      (Const (s, T), ts) => (case strip_type T of
berghofe@22530
    33
        (Ts, Type (tname, _)) =>
berghofe@22530
    34
          (case NominalPackage.get_nominal_datatype thy tname of
berghofe@22530
    35
             NONE => fold (add_binders thy i) ts bs
berghofe@22530
    36
           | SOME {descr, index, ...} => (case AList.lookup op =
berghofe@22530
    37
                 (#3 (the (AList.lookup op = descr index))) s of
berghofe@22530
    38
               NONE => fold (add_binders thy i) ts bs
berghofe@22530
    39
             | SOME cargs => fst (fold (fn (xs, x) => fn (bs', cargs') =>
berghofe@22530
    40
                 let val (cargs1, (u, _) :: cargs2) = chop (length xs) cargs'
berghofe@22530
    41
                 in (add_binders thy i u
berghofe@22530
    42
                   (fold (fn (u, T) =>
berghofe@22530
    43
                      if exists (fn j => j < i) (loose_bnos u) then I
berghofe@22530
    44
                      else insert (op aconv o pairself fst)
berghofe@22530
    45
                        (incr_boundvars (~i) u, T)) cargs1 bs'), cargs2)
berghofe@22530
    46
                 end) cargs (bs, ts ~~ Ts))))
berghofe@22530
    47
      | _ => fold (add_binders thy i) ts bs)
berghofe@22530
    48
    | (u, ts) => add_binders thy i u (fold (add_binders thy i) ts bs))
berghofe@22530
    49
  | add_binders thy i (Abs (_, _, t)) bs = add_binders thy (i + 1) t bs
berghofe@22530
    50
  | add_binders thy i _ bs = bs;
berghofe@22530
    51
berghofe@22730
    52
fun prove_strong_ind s avoids thy =
berghofe@22313
    53
  let
berghofe@22313
    54
    val ctxt = ProofContext.init thy;
berghofe@22730
    55
    val ({names, ...}, {raw_induct, ...}) =
berghofe@22730
    56
      InductivePackage.the_inductive ctxt (Sign.intern_const thy s);
berghofe@22530
    57
    val induct_cases = map fst (fst (RuleCases.get (the
berghofe@22530
    58
      (InductAttrib.lookup_inductS ctxt (hd names)))));
berghofe@22530
    59
    val raw_induct' = Logic.unvarify (prop_of raw_induct);
berghofe@22530
    60
    val concls = raw_induct' |> Logic.strip_imp_concl |> HOLogic.dest_Trueprop |>
berghofe@22530
    61
      HOLogic.dest_conj |> map (HOLogic.dest_imp ##> strip_comb);
berghofe@22530
    62
    val ps = map (fst o snd) concls;
berghofe@22530
    63
berghofe@22530
    64
    val _ = (case duplicates (op = o pairself fst) avoids of
berghofe@22530
    65
        [] => ()
berghofe@22530
    66
      | xs => error ("Duplicate case names: " ^ commas_quote (map fst xs)));
berghofe@22530
    67
    val _ = assert_all (null o duplicates op = o snd) avoids
berghofe@22530
    68
      (fn (a, _) => error ("Duplicate variable names for case " ^ quote a));
berghofe@22530
    69
    val _ = (case map fst avoids \\ induct_cases of
berghofe@22530
    70
        [] => ()
berghofe@22530
    71
      | xs => error ("No such case(s) in inductive definition: " ^ commas_quote xs));
berghofe@22530
    72
    val avoids' = map (fn name =>
berghofe@22530
    73
      (name, the_default [] (AList.lookup op = avoids name))) induct_cases;
berghofe@22530
    74
    fun mk_avoids params (name, ps) =
berghofe@22530
    75
      let val k = length params - 1
berghofe@22530
    76
      in map (fn x => case find_index (equal x o fst) params of
berghofe@22530
    77
          ~1 => error ("No such variable in case " ^ quote name ^
berghofe@22530
    78
            " of inductive definition: " ^ quote x)
berghofe@22530
    79
        | i => (Bound (k - i), snd (nth params i))) ps
berghofe@22530
    80
      end;
berghofe@22530
    81
berghofe@22530
    82
    val prems = map (fn (prem, avoid) =>
berghofe@22530
    83
      let
berghofe@22530
    84
        val prems = map (incr_boundvars 1) (Logic.strip_assums_hyp prem);
berghofe@22530
    85
        val concl = incr_boundvars 1 (Logic.strip_assums_concl prem);
berghofe@22530
    86
        val params = Logic.strip_params prem
berghofe@22530
    87
      in
berghofe@22530
    88
        (params,
berghofe@22530
    89
         fold (add_binders thy 0) (prems @ [concl]) [] @
berghofe@22530
    90
           map (apfst (incr_boundvars 1)) (mk_avoids params avoid),
berghofe@22530
    91
         prems, strip_comb (HOLogic.dest_Trueprop concl))
berghofe@22530
    92
      end) (Logic.strip_imp_prems raw_induct' ~~ avoids');
berghofe@22530
    93
berghofe@22530
    94
    val atomTs = distinct op = (maps (map snd o #2) prems);
berghofe@22530
    95
    val ind_sort = if null atomTs then HOLogic.typeS
berghofe@22530
    96
      else Sign.certify_sort thy (map (fn T => Sign.intern_class thy
berghofe@22530
    97
        ("fs_" ^ Sign.base_name (fst (dest_Type T)))) atomTs);
berghofe@22530
    98
    val fs_ctxt_tyname = Name.variant (map fst (term_tfrees raw_induct')) "'n";
berghofe@22530
    99
    val fs_ctxt_name = Name.variant (add_term_names (raw_induct', [])) "z";
berghofe@22530
   100
    val fsT = TFree (fs_ctxt_tyname, ind_sort);
berghofe@22530
   101
berghofe@22530
   102
    fun lift_pred' t (Free (s, T)) ts =
berghofe@22530
   103
      list_comb (Free (s, fsT --> T), t :: ts);
berghofe@22530
   104
    val lift_pred = lift_pred' (Bound 0);
berghofe@22530
   105
berghofe@22530
   106
    fun lift_prem (Const ("Trueprop", _) $ t) =
berghofe@22530
   107
          let val (u, ts) = strip_comb t
berghofe@22530
   108
          in
berghofe@22530
   109
            if u mem ps then
berghofe@22530
   110
              all fsT $ Abs ("z", fsT, HOLogic.mk_Trueprop
berghofe@22530
   111
                (lift_pred u (map (incr_boundvars 1) ts)))
berghofe@22530
   112
            else HOLogic.mk_Trueprop (lift_prem t)
berghofe@22530
   113
          end
berghofe@22530
   114
      | lift_prem (t as (f $ u)) =
berghofe@22530
   115
          let val (p, ts) = strip_comb t
berghofe@22530
   116
          in
berghofe@22530
   117
            if p mem ps then
berghofe@22530
   118
              HOLogic.all_const fsT $ Abs ("z", fsT,
berghofe@22530
   119
                lift_pred p (map (incr_boundvars 1) ts))
berghofe@22530
   120
            else lift_prem f $ lift_prem u
berghofe@22530
   121
          end
berghofe@22530
   122
      | lift_prem (Abs (s, T, t)) = Abs (s, T, lift_prem t)
berghofe@22530
   123
      | lift_prem t = t;
berghofe@22530
   124
berghofe@22530
   125
    fun mk_distinct [] = []
berghofe@22530
   126
      | mk_distinct ((x, T) :: xs) = List.mapPartial (fn (y, U) =>
berghofe@22530
   127
          if T = U then SOME (HOLogic.mk_Trueprop
berghofe@22530
   128
            (HOLogic.mk_not (HOLogic.eq_const T $ x $ y)))
berghofe@22530
   129
          else NONE) xs @ mk_distinct xs;
berghofe@22530
   130
berghofe@22530
   131
    fun mk_fresh (x, T) = HOLogic.mk_Trueprop
berghofe@22530
   132
      (Const ("Nominal.fresh", T --> fsT --> HOLogic.boolT) $ x $ Bound 0);
berghofe@22530
   133
berghofe@22530
   134
    val (prems', prems'') = split_list (map (fn (params, bvars, prems, (p, ts)) =>
berghofe@22530
   135
      let
berghofe@22530
   136
        val params' = params @ [("y", fsT)];
berghofe@22530
   137
        val prem = Logic.list_implies
berghofe@22530
   138
          (map mk_fresh bvars @ mk_distinct bvars @
berghofe@22530
   139
           map (fn prem =>
berghofe@22530
   140
             if null (term_frees prem inter ps) then prem
berghofe@22530
   141
             else lift_prem prem) prems,
berghofe@22530
   142
           HOLogic.mk_Trueprop (lift_pred p ts));
berghofe@22530
   143
        val vs = map (Var o apfst (rpair 0)) (rename_wrt_term prem params')
berghofe@22530
   144
      in
berghofe@22530
   145
        (list_all (params', prem), (rev vs, subst_bounds (vs, prem)))
berghofe@22530
   146
      end) prems);
berghofe@22530
   147
berghofe@22530
   148
    val ind_vars =
berghofe@22530
   149
      (DatatypeProp.indexify_names (replicate (length atomTs) "pi") ~~
berghofe@22530
   150
       map NominalAtoms.mk_permT atomTs) @ [("z", fsT)];
berghofe@22530
   151
    val ind_Ts = rev (map snd ind_vars);
berghofe@22530
   152
berghofe@22530
   153
    val concl = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@22530
   154
      (map (fn (prem, (p, ts)) => HOLogic.mk_imp (prem,
berghofe@22530
   155
        HOLogic.list_all (ind_vars, lift_pred p
berghofe@22530
   156
          (map (fold_rev (NominalPackage.mk_perm ind_Ts)
berghofe@22530
   157
            (map Bound (length atomTs downto 1))) ts)))) concls));
berghofe@22530
   158
berghofe@22530
   159
    val concl' = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@22530
   160
      (map (fn (prem, (p, ts)) => HOLogic.mk_imp (prem,
berghofe@22530
   161
        lift_pred' (Free (fs_ctxt_name, fsT)) p ts)) concls));
berghofe@22530
   162
berghofe@22530
   163
    val vc_compat = map (fn (params, bvars, prems, (p, ts)) =>
berghofe@22530
   164
      map (fn q => list_all (params, incr_boundvars ~1 (Logic.list_implies
berghofe@22530
   165
          (filter (fn prem => null (ps inter term_frees prem)) prems, q))))
berghofe@22530
   166
        (mk_distinct bvars @
berghofe@22530
   167
         maps (fn (t, T) => map (fn (u, U) => HOLogic.mk_Trueprop
berghofe@22530
   168
           (Const ("Nominal.fresh", U --> T --> HOLogic.boolT) $ u $ t)) bvars)
berghofe@22530
   169
             (ts ~~ binder_types (fastype_of p)))) prems;
berghofe@22530
   170
berghofe@22530
   171
    val eqvt_ss = HOL_basic_ss addsimps NominalThmDecls.get_eqvt_thms thy;
berghofe@22530
   172
    val fresh_bij = PureThy.get_thms thy (Name "fresh_bij");
berghofe@22530
   173
    val perm_bij = PureThy.get_thms thy (Name "perm_bij");
berghofe@22530
   174
    val fs_atoms = map (fn aT => PureThy.get_thm thy
berghofe@22530
   175
      (Name ("fs_" ^ Sign.base_name (fst (dest_Type aT)) ^ "1"))) atomTs;
berghofe@22530
   176
    val exists_fresh' = PureThy.get_thms thy (Name "exists_fresh'");
berghofe@22530
   177
    val fresh_atm = PureThy.get_thms thy (Name "fresh_atm");
berghofe@22530
   178
    val calc_atm = PureThy.get_thms thy (Name "calc_atm");
berghofe@22530
   179
    val perm_fresh_fresh = PureThy.get_thms thy (Name "perm_fresh_fresh");
berghofe@22530
   180
    val pt2_atoms = map (fn aT => PureThy.get_thm thy
berghofe@22530
   181
      (Name ("pt_" ^ Sign.base_name (fst (dest_Type aT)) ^ "2")) RS sym) atomTs;
berghofe@22530
   182
berghofe@22530
   183
    fun obtain_fresh_name ts T (freshs1, freshs2, ctxt) =
berghofe@22530
   184
      let
berghofe@22530
   185
        (** protect terms to avoid that supp_prod interferes with   **)
berghofe@22530
   186
        (** pairs used in introduction rules of inductive predicate **)
berghofe@22530
   187
        fun protect t =
berghofe@22530
   188
          let val T = fastype_of t in Const ("Fun.id", T --> T) $ t end;
berghofe@22530
   189
        val p = foldr1 HOLogic.mk_prod (map protect ts @ freshs1);
berghofe@22530
   190
        val ex = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop
berghofe@22530
   191
            (HOLogic.exists_const T $ Abs ("x", T,
berghofe@22530
   192
              Const ("Nominal.fresh", T --> fastype_of p --> HOLogic.boolT) $
berghofe@22530
   193
                Bound 0 $ p)))
berghofe@22530
   194
          (fn _ => EVERY
berghofe@22530
   195
            [resolve_tac exists_fresh' 1,
berghofe@22530
   196
             simp_tac (HOL_ss addsimps (supp_prod :: finite_Un :: fs_atoms)) 1]);
berghofe@22530
   197
        val (([cx], ths), ctxt') = Obtain.result
berghofe@22530
   198
          (fn _ => EVERY
berghofe@22530
   199
            [etac exE 1,
berghofe@22530
   200
             full_simp_tac (HOL_ss addsimps (fresh_prod :: fresh_atm)) 1,
berghofe@22530
   201
             full_simp_tac (HOL_basic_ss addsimps [id_apply]) 1,
berghofe@22530
   202
             REPEAT (etac conjE 1)])
berghofe@22530
   203
          [ex] ctxt
berghofe@22530
   204
      in (freshs1 @ [term_of cx], freshs2 @ ths, ctxt') end;
berghofe@22530
   205
berghofe@22530
   206
    fun mk_proof thy thss =
berghofe@22530
   207
      let val ctxt = ProofContext.init thy
berghofe@22530
   208
      in Goal.prove_global thy [] prems' concl' (fn ihyps =>
berghofe@22530
   209
        let val th = Goal.prove ctxt [] [] concl (fn {context, ...} =>
berghofe@22530
   210
          rtac raw_induct 1 THEN
berghofe@22530
   211
          EVERY (maps (fn ((((_, bvars, oprems, _), vc_compat_ths), ihyp), (vs, ihypt)) =>
berghofe@22530
   212
            [REPEAT (rtac allI 1), simp_tac eqvt_ss 1,
berghofe@22530
   213
             SUBPROOF (fn {prems = gprems, params, concl, context = ctxt', ...} =>
berghofe@22530
   214
               let
berghofe@22530
   215
                 val (params', (pis, z)) =
berghofe@22530
   216
                   chop (length params - length atomTs - 1) (map term_of params) ||>
berghofe@22530
   217
                   split_last;
berghofe@22530
   218
                 val bvars' = map
berghofe@22530
   219
                   (fn (Bound i, T) => (nth params' (length params' - i), T)
berghofe@22530
   220
                     | (t, T) => (t, T)) bvars;
berghofe@22530
   221
                 val pi_bvars = map (fn (t, _) =>
berghofe@22530
   222
                   fold_rev (NominalPackage.mk_perm []) pis t) bvars';
berghofe@22530
   223
                 val (P, ts) = strip_comb (HOLogic.dest_Trueprop (term_of concl));
berghofe@22530
   224
                 val (freshs1, freshs2, ctxt'') = fold
berghofe@22530
   225
                   (obtain_fresh_name (ts @ pi_bvars))
berghofe@22530
   226
                   (map snd bvars') ([], [], ctxt');
berghofe@22530
   227
                 val freshs2' = NominalPackage.mk_not_sym freshs2;
berghofe@22530
   228
                 val pis' = map NominalPackage.perm_of_pair (pi_bvars ~~ freshs1);
berghofe@22530
   229
                 val env = Pattern.first_order_match thy (ihypt, prop_of ihyp)
berghofe@22530
   230
                   (Vartab.empty, Vartab.empty);
berghofe@22530
   231
                 val ihyp' = Thm.instantiate ([], map (pairself (cterm_of thy))
berghofe@22530
   232
                   (map (Envir.subst_vars env) vs ~~
berghofe@22530
   233
                    map (fold_rev (NominalPackage.mk_perm [])
berghofe@22530
   234
                      (rev pis' @ pis)) params' @ [z])) ihyp;
berghofe@22530
   235
                 val (gprems1, gprems2) = pairself (map fst) (List.partition
berghofe@22530
   236
                   (fn (th, t) => null (term_frees t inter ps)) (gprems ~~ oprems));
berghofe@22530
   237
                 val vc_compat_ths' = map (fn th =>
berghofe@22530
   238
                   let
berghofe@22530
   239
                     val th' = gprems1 MRS
berghofe@22530
   240
                       Thm.instantiate (Thm.cterm_first_order_match
berghofe@22530
   241
                         (Conjunction.mk_conjunction_list (cprems_of th),
berghofe@22530
   242
                          Conjunction.mk_conjunction_list (map cprop_of gprems1))) th;
berghofe@22530
   243
                     val (bop, lhs, rhs) = (case concl_of th' of
berghofe@22530
   244
                         _ $ (fresh $ lhs $ rhs) =>
berghofe@22530
   245
                           (fn t => fn u => fresh $ t $ u, lhs, rhs)
berghofe@22530
   246
                       | _ $ (_ $ (_ $ lhs $ rhs)) =>
berghofe@22530
   247
                           (curry (HOLogic.mk_not o HOLogic.mk_eq), lhs, rhs));
berghofe@22530
   248
                     val th'' = Goal.prove ctxt'' [] [] (HOLogic.mk_Trueprop
berghofe@22530
   249
                         (bop (fold_rev (NominalPackage.mk_perm []) pis lhs)
berghofe@22530
   250
                            (fold_rev (NominalPackage.mk_perm []) pis rhs)))
berghofe@22530
   251
                       (fn _ => simp_tac (HOL_basic_ss addsimps
berghofe@22530
   252
                          (fresh_bij @ perm_bij)) 1 THEN rtac th' 1)
berghofe@22530
   253
                   in Simplifier.simplify (eqvt_ss addsimps fresh_atm) th'' end)
berghofe@22530
   254
                     vc_compat_ths;
berghofe@22530
   255
                 val vc_compat_ths'' = NominalPackage.mk_not_sym vc_compat_ths';
berghofe@22530
   256
                 val gprems1' = map (fn th => fold_rev (fn pi => fn th' =>
berghofe@22530
   257
                   Simplifier.simplify eqvt_ss (th' RS Drule.cterm_instantiate
berghofe@22530
   258
                     [(perm_boolI_pi, cterm_of thy pi)] perm_boolI))
berghofe@22530
   259
                       (rev pis' @ pis) th) gprems1;
berghofe@22530
   260
                 val gprems2' = map (Simplifier.simplify eqvt_ss) gprems2;
berghofe@22530
   261
                 (** Since calc_atm simplifies (pi :: 'a prm) o (x :: 'b) to x **)
berghofe@22530
   262
                 (** we have to pre-simplify the rewrite rules                 **)
berghofe@22530
   263
                 val calc_atm_ss = HOL_ss addsimps calc_atm @
berghofe@22530
   264
                    map (Simplifier.simplify (HOL_ss addsimps calc_atm))
berghofe@22530
   265
                      (vc_compat_ths'' @ freshs2');
berghofe@22530
   266
                 val th = Goal.prove ctxt'' [] []
berghofe@22530
   267
                   (HOLogic.mk_Trueprop (list_comb (P $ hd ts,
berghofe@22530
   268
                     map (fold (NominalPackage.mk_perm []) pis') (tl ts))))
berghofe@22530
   269
                   (fn _ => EVERY ([simp_tac eqvt_ss 1, rtac ihyp' 1,
berghofe@22530
   270
                     REPEAT_DETERM_N (nprems_of ihyp - length gprems)
berghofe@22530
   271
                       (simp_tac calc_atm_ss 1),
berghofe@22530
   272
                     REPEAT_DETERM_N (length gprems)
berghofe@22530
   273
                       (resolve_tac gprems1' 1 ORELSE
berghofe@22530
   274
                        simp_tac (HOL_basic_ss addsimps pt2_atoms @ gprems2'
berghofe@22530
   275
                          addsimprocs [NominalPackage.perm_simproc]) 1)]));
berghofe@22530
   276
                 val final = Goal.prove ctxt'' [] [] (term_of concl)
berghofe@22530
   277
                   (fn _ => cut_facts_tac [th] 1 THEN full_simp_tac (HOL_ss
berghofe@22530
   278
                     addsimps vc_compat_ths'' @ freshs2' @
berghofe@22530
   279
                       perm_fresh_fresh @ fresh_atm) 1);
berghofe@22530
   280
                 val final' = ProofContext.export ctxt'' ctxt' [final];
berghofe@22530
   281
               in resolve_tac final' 1 end) context 1])
berghofe@22530
   282
                 (prems ~~ thss ~~ ihyps ~~ prems'')))
berghofe@22530
   283
        in
berghofe@22530
   284
          cut_facts_tac [th] 1 THEN REPEAT (etac conjE 1) THEN
berghofe@22530
   285
          REPEAT (REPEAT (resolve_tac [conjI, impI] 1) THEN
berghofe@22530
   286
            etac impE 1 THEN atac 1 THEN REPEAT (etac allE_Nil 1) THEN
berghofe@22530
   287
            asm_full_simp_tac (simpset_of thy) 1)
berghofe@22530
   288
        end)
berghofe@22530
   289
      end;
berghofe@22530
   290
berghofe@22530
   291
  in
berghofe@22530
   292
    thy |>
berghofe@22530
   293
    ProofContext.init |>
berghofe@22530
   294
    Proof.theorem_i NONE (fn thss => ProofContext.theory (fn thy =>
berghofe@22530
   295
      let
berghofe@22530
   296
        val ctxt = ProofContext.init thy;
berghofe@22530
   297
        val rec_name = space_implode "_" (map Sign.base_name names);
berghofe@22530
   298
        val ind_case_names = RuleCases.case_names induct_cases;
berghofe@22530
   299
        val strong_raw_induct = mk_proof thy thss;
berghofe@22530
   300
        val strong_induct =
berghofe@22530
   301
          if length names > 1 then
berghofe@22530
   302
            (strong_raw_induct, [ind_case_names, RuleCases.consumes 0])
berghofe@22530
   303
          else (strong_raw_induct RSN (2, rev_mp),
berghofe@22530
   304
            [ind_case_names, RuleCases.consumes 1]);
berghofe@22530
   305
        val ([strong_induct'], thy') = thy |>
berghofe@22530
   306
          Theory.add_path rec_name |>
berghofe@22530
   307
          PureThy.add_thms [(("strong_induct", #1 strong_induct), #2 strong_induct)];
berghofe@22530
   308
        val strong_inducts =
berghofe@22530
   309
          ProjectRule.projects ctxt (1 upto length names) strong_induct'
berghofe@22530
   310
      in
berghofe@22530
   311
        thy' |>
berghofe@22530
   312
        PureThy.add_thmss [(("strong_inducts", strong_inducts),
berghofe@22530
   313
          [ind_case_names, RuleCases.consumes 1])] |> snd |>
berghofe@22530
   314
        Theory.parent_path
berghofe@22530
   315
      end))
berghofe@22530
   316
      (map (map (rpair [])) vc_compat)
berghofe@22530
   317
  end;
berghofe@22530
   318
berghofe@22730
   319
fun prove_eqvt s xatoms thy =
berghofe@22530
   320
  let
berghofe@22530
   321
    val ctxt = ProofContext.init thy;
berghofe@22730
   322
    val ({names, ...}, {raw_induct, intrs, ...}) =
berghofe@22730
   323
      InductivePackage.the_inductive ctxt (Sign.intern_const thy s);
berghofe@22730
   324
    val atoms' = NominalAtoms.atoms_of thy;
berghofe@22730
   325
    val atoms =
berghofe@22730
   326
      if null xatoms then atoms' else
berghofe@22730
   327
      let val atoms = map (Sign.intern_type thy) xatoms
berghofe@22730
   328
      in
berghofe@22730
   329
        (case duplicates op = atoms of
berghofe@22730
   330
             [] => ()
berghofe@22730
   331
           | xs => error ("Duplicate atoms: " ^ commas xs);
berghofe@22730
   332
         case atoms \\ atoms' of
berghofe@22730
   333
             [] => ()
berghofe@22730
   334
           | xs => error ("No such atoms: " ^ commas xs);
berghofe@22730
   335
         atoms)
berghofe@22730
   336
      end;
berghofe@22313
   337
    val eqvt_ss = HOL_basic_ss addsimps NominalThmDecls.get_eqvt_thms thy;
berghofe@22313
   338
    val t = Logic.unvarify (concl_of raw_induct);
berghofe@22313
   339
    val pi = Name.variant (add_term_names (t, [])) "pi";
berghofe@22313
   340
    val ps = map (fst o HOLogic.dest_imp)
berghofe@22313
   341
      (HOLogic.dest_conj (HOLogic.dest_Trueprop t));
berghofe@22544
   342
    fun eqvt_tac th intr st =
berghofe@22544
   343
      let
berghofe@22544
   344
        fun eqvt_err s = error
berghofe@22544
   345
          ("Could not prove equivariance for introduction rule\n" ^
berghofe@22544
   346
           Sign.string_of_term (theory_of_thm intr)
berghofe@22544
   347
             (Logic.unvarify (prop_of intr)) ^ "\n" ^ s);
berghofe@22544
   348
        val res = SUBPROOF (fn {prems, ...} =>
berghofe@22544
   349
          let val prems' = map (fn th' =>
berghofe@22544
   350
            if null (names inter term_consts (prop_of th')) then th' RS th
berghofe@22544
   351
            else th') prems
berghofe@22544
   352
          in (rtac intr THEN_ALL_NEW
berghofe@22544
   353
            (resolve_tac prems ORELSE'
berghofe@22544
   354
              (cut_facts_tac prems' THEN' full_simp_tac eqvt_ss))) 1
berghofe@22544
   355
          end) ctxt 1 st
berghofe@22544
   356
      in
berghofe@22544
   357
        case (Seq.pull res handle THM (s, _, _) => eqvt_err s) of
berghofe@22544
   358
          NONE => eqvt_err ("Rule does not match goal\n" ^
berghofe@22544
   359
            Sign.string_of_term (theory_of_thm st) (hd (prems_of st)))
berghofe@22544
   360
        | SOME (th, _) => Seq.single th
berghofe@22544
   361
      end;
berghofe@22313
   362
    val thss = map (fn atom =>
berghofe@22313
   363
      let
berghofe@22313
   364
        val pi' = Free (pi, NominalAtoms.mk_permT (Type (atom, [])));
berghofe@22313
   365
        val perm_boolI' = Drule.cterm_instantiate
berghofe@22313
   366
          [(perm_boolI_pi, cterm_of thy pi')] perm_boolI
berghofe@22530
   367
      in map (fn th => zero_var_indexes (th RS mp))
berghofe@22313
   368
        (DatatypeAux.split_conj_thm (Goal.prove_global thy [] []
berghofe@22313
   369
          (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj (map (fn p =>
berghofe@22313
   370
            HOLogic.mk_imp (p, list_comb
berghofe@22313
   371
             (apsnd (map (NominalPackage.mk_perm [] pi')) (strip_comb p)))) ps)))
berghofe@22544
   372
          (fn _ => EVERY (rtac raw_induct 1 :: map (fn intr =>
berghofe@22544
   373
              full_simp_tac eqvt_ss 1 THEN eqvt_tac perm_boolI' intr) intrs))))
berghofe@22544
   374
      end) atoms
berghofe@22544
   375
  in
berghofe@22544
   376
    fold (fn (name, ths) =>
berghofe@22544
   377
      Theory.add_path (Sign.base_name name) #>
berghofe@22544
   378
      PureThy.add_thmss [(("eqvt", ths), [NominalThmDecls.eqvt_add])] #> snd #>
berghofe@22544
   379
      Theory.parent_path) (names ~~ transp thss) thy
berghofe@22544
   380
  end;
berghofe@22313
   381
berghofe@22313
   382
berghofe@22313
   383
(* outer syntax *)
berghofe@22313
   384
berghofe@22313
   385
local structure P = OuterParse and K = OuterKeyword in
berghofe@22313
   386
berghofe@22313
   387
val nominal_inductiveP =
berghofe@22313
   388
  OuterSyntax.command "nominal_inductive"
berghofe@22530
   389
    "prove equivariance and strong induction theorem for inductive predicate involving nominal datatypes" K.thy_goal
berghofe@22530
   390
    (P.name -- Scan.optional (P.$$$ "avoids" |-- P.and_list1 (P.name --
berghofe@22530
   391
      (P.$$$ ":" |-- Scan.repeat1 P.name))) [] >> (fn (name, avoids) =>
berghofe@22730
   392
        Toplevel.print o Toplevel.theory_to_proof (prove_strong_ind name avoids)));
berghofe@22313
   393
berghofe@22530
   394
val equivarianceP =
berghofe@22530
   395
  OuterSyntax.command "equivariance"
berghofe@22530
   396
    "prove equivariance for inductive predicate involving nominal datatypes" K.thy_decl
berghofe@22730
   397
    (P.name -- Scan.optional (P.$$$ "[" |-- P.list1 P.name --| P.$$$ "]") [] >>
berghofe@22730
   398
      (fn (name, atoms) => Toplevel.theory (prove_eqvt name atoms)));
berghofe@22530
   399
berghofe@22530
   400
val _ = OuterSyntax.add_keywords ["avoids"];
berghofe@22530
   401
val _ = OuterSyntax.add_parsers [nominal_inductiveP, equivarianceP];
berghofe@22313
   402
berghofe@22313
   403
end;
berghofe@22313
   404
berghofe@22313
   405
end