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