src/HOL/Nominal/nominal_datatype.ML
author wenzelm
Tue Oct 10 19:23:03 2017 +0200 (2017-10-10)
changeset 66831 29ea2b900a05
parent 63352 4eaf35781b23
child 67315 9301e197a47b
permissions -rw-r--r--
tuned: each session has at most one defining entry;
haftmann@31936
     1
(*  Title:      HOL/Nominal/nominal_datatype.ML
berghofe@19494
     2
    Author:     Stefan Berghofer and Christian Urban, TU Muenchen
berghofe@19494
     3
berghofe@19494
     4
Nominal datatype package for Isabelle/HOL.
berghofe@19494
     5
*)
berghofe@17870
     6
haftmann@31936
     7
signature NOMINAL_DATATYPE =
berghofe@17870
     8
sig
blanchet@58112
     9
  val nominal_datatype : Old_Datatype_Aux.config -> Old_Datatype.spec list -> theory -> theory
blanchet@58112
    10
  val nominal_datatype_cmd : Old_Datatype_Aux.config -> Old_Datatype.spec_cmd list -> theory ->
blanchet@58112
    11
    theory
berghofe@22433
    12
  type descr
berghofe@21540
    13
  type nominal_datatype_info
berghofe@21540
    14
  val get_nominal_datatypes : theory -> nominal_datatype_info Symtab.table
berghofe@21540
    15
  val get_nominal_datatype : theory -> string -> nominal_datatype_info option
berghofe@22311
    16
  val mk_perm: typ list -> term -> term -> term
berghofe@22529
    17
  val perm_of_pair: term * term -> term
berghofe@22529
    18
  val mk_not_sym: thm list -> thm list
berghofe@22529
    19
  val perm_simproc: simproc
berghofe@25823
    20
  val fresh_const: typ -> typ -> term
berghofe@28373
    21
  val fresh_star_const: typ -> typ -> term
berghofe@17870
    22
end
berghofe@17870
    23
haftmann@31936
    24
structure NominalDatatype : NOMINAL_DATATYPE =
berghofe@17870
    25
struct
berghofe@17870
    26
wenzelm@39159
    27
val finite_emptyI = @{thm finite.emptyI};
wenzelm@39159
    28
val finite_Diff = @{thm finite_Diff};
wenzelm@39159
    29
val finite_Un = @{thm finite_Un};
wenzelm@39159
    30
val Un_iff = @{thm Un_iff};
wenzelm@39159
    31
val In0_eq = @{thm In0_eq};
wenzelm@39159
    32
val In1_eq = @{thm In1_eq};
wenzelm@39159
    33
val In0_not_In1 = @{thm In0_not_In1};
wenzelm@39159
    34
val In1_not_In0 = @{thm In1_not_In0};
wenzelm@39159
    35
val Un_assoc = @{thm Un_assoc};
wenzelm@39159
    36
val Collect_disj_eq = @{thm Collect_disj_eq};
haftmann@32129
    37
val Collect_False_empty = @{thm empty_def [THEN sym, THEN eq_reflection]};
wenzelm@39159
    38
val empty_iff = @{thm empty_iff};
wenzelm@21669
    39
berghofe@18068
    40
open NominalAtoms;
berghofe@17870
    41
berghofe@18016
    42
wenzelm@22846
    43
(* theory data *)
berghofe@21540
    44
wenzelm@45838
    45
type descr =
blanchet@58112
    46
  (int * (string * Old_Datatype_Aux.dtyp list *
blanchet@58112
    47
      (string * (Old_Datatype_Aux.dtyp list * Old_Datatype_Aux.dtyp) list) list)) list;
berghofe@22433
    48
berghofe@21540
    49
type nominal_datatype_info =
berghofe@21540
    50
  {index : int,
berghofe@21540
    51
   descr : descr,
berghofe@21540
    52
   rec_names : string list,
berghofe@21540
    53
   rec_rewrites : thm list,
berghofe@21540
    54
   induction : thm,
berghofe@21540
    55
   distinct : thm list,
berghofe@21540
    56
   inject : thm list};
berghofe@21540
    57
wenzelm@33522
    58
structure NominalDatatypesData = Theory_Data
wenzelm@22846
    59
(
berghofe@21540
    60
  type T = nominal_datatype_info Symtab.table;
berghofe@21540
    61
  val empty = Symtab.empty;
berghofe@21540
    62
  val extend = I;
wenzelm@33522
    63
  fun merge data = Symtab.merge (K true) data;
wenzelm@22846
    64
);
berghofe@21540
    65
berghofe@21540
    66
val get_nominal_datatypes = NominalDatatypesData.get;
berghofe@21540
    67
val put_nominal_datatypes = NominalDatatypesData.put;
berghofe@21540
    68
val map_nominal_datatypes = NominalDatatypesData.map;
berghofe@21540
    69
val get_nominal_datatype = Symtab.lookup o get_nominal_datatypes;
wenzelm@22846
    70
berghofe@21540
    71
berghofe@21540
    72
(**** make datatype info ****)
berghofe@21540
    73
wenzelm@45822
    74
fun make_dt_info descr induct reccomb_names rec_thms
haftmann@33063
    75
    (i, (((_, (tname, _, _)), distinct), inject)) =
berghofe@21540
    76
  (tname,
berghofe@21540
    77
   {index = i,
berghofe@21540
    78
    descr = descr,
berghofe@21540
    79
    rec_names = reccomb_names,
berghofe@21540
    80
    rec_rewrites = rec_thms,
berghofe@21540
    81
    induction = induct,
berghofe@21540
    82
    distinct = distinct,
berghofe@21540
    83
    inject = inject});
berghofe@21540
    84
berghofe@18016
    85
(*******************************)
berghofe@18016
    86
wenzelm@59582
    87
val (_ $ (_ $ (_ $ (distinct_f $ _) $ _))) = hd (Thm.prems_of Old_Datatype.distinct_lemma);
berghofe@17870
    88
berghofe@17870
    89
berghofe@18658
    90
(** simplification procedure for sorting permutations **)
berghofe@18658
    91
wenzelm@39159
    92
val dj_cp = @{thm dj_cp};
berghofe@18658
    93
wenzelm@56253
    94
fun dest_permT (Type (@{type_name fun},
wenzelm@56253
    95
    [Type (@{type_name list}, [Type (@{type_name Product_Type.prod}, [T, _])]),
wenzelm@56253
    96
      Type (@{type_name fun}, [_, U])])) = (T, U);
berghofe@18658
    97
wenzelm@56253
    98
fun permTs_of (Const (@{const_name Nominal.perm}, T) $ t $ u) = fst (dest_permT T) :: permTs_of u
berghofe@18658
    99
  | permTs_of _ = [];
berghofe@18658
   100
wenzelm@61144
   101
fun perm_simproc' ctxt ct =
wenzelm@61144
   102
  (case Thm.term_of ct of
wenzelm@61144
   103
    Const (@{const_name Nominal.perm}, T) $ t $ (u as Const (@{const_name Nominal.perm}, U) $ r $ s) =>
berghofe@18658
   104
      let
wenzelm@51717
   105
        val thy = Proof_Context.theory_of ctxt;
berghofe@18658
   106
        val (aT as Type (a, []), S) = dest_permT T;
wenzelm@51717
   107
        val (bT as Type (b, []), _) = dest_permT U;
wenzelm@61144
   108
      in
wenzelm@61144
   109
        if member (op =) (permTs_of u) aT andalso aT <> bT then
berghofe@18658
   110
          let
berghofe@28731
   111
            val cp = cp_inst_of thy a b;
berghofe@28731
   112
            val dj = dj_thm_of thy b a;
berghofe@18658
   113
            val dj_cp' = [cp, dj] MRS dj_cp;
wenzelm@59635
   114
            val cert = SOME o Thm.cterm_of ctxt
berghofe@18658
   115
          in
wenzelm@60801
   116
            SOME (mk_meta_eq (Thm.instantiate' [SOME (Thm.ctyp_of ctxt S)]
berghofe@18658
   117
              [cert t, cert r, cert s] dj_cp'))
berghofe@18658
   118
          end
berghofe@18658
   119
        else NONE
berghofe@18658
   120
      end
wenzelm@61144
   121
  | _ => NONE);
berghofe@18658
   122
berghofe@18658
   123
val perm_simproc =
wenzelm@61144
   124
  Simplifier.make_simproc @{context} "perm_simp"
wenzelm@62913
   125
   {lhss = [@{term "pi1 \<bullet> (pi2 \<bullet> x)"}], proc = K perm_simproc'};
berghofe@18658
   126
wenzelm@60359
   127
fun projections ctxt rule =
wenzelm@60359
   128
  Project_Rule.projections ctxt rule
wenzelm@35021
   129
  |> map (Drule.export_without_context #> Rule_Cases.save rule);
wenzelm@18582
   130
wenzelm@39159
   131
val supp_prod = @{thm supp_prod};
wenzelm@39159
   132
val fresh_prod = @{thm fresh_prod};
wenzelm@39159
   133
val supports_fresh = @{thm supports_fresh};
haftmann@44685
   134
val supports_def = Simpdata.mk_eq @{thm Nominal.supports_def};
haftmann@44685
   135
val fresh_def = Simpdata.mk_eq @{thm fresh_def};
haftmann@44685
   136
val supp_def = Simpdata.mk_eq @{thm supp_def};
wenzelm@39159
   137
val rev_simps = @{thms rev.simps};
wenzelm@39159
   138
val app_simps = @{thms append.simps};
wenzelm@39159
   139
val at_fin_set_supp = @{thm at_fin_set_supp};
wenzelm@39159
   140
val at_fin_set_fresh = @{thm at_fin_set_fresh};
wenzelm@39159
   141
val abs_fun_eq1 = @{thm abs_fun_eq1};
berghofe@20267
   142
wenzelm@54742
   143
fun collect_simp ctxt = rewrite_rule ctxt [mk_meta_eq mem_Collect_eq];
berghofe@21021
   144
berghofe@22311
   145
fun mk_perm Ts t u =
berghofe@22311
   146
  let
berghofe@22311
   147
    val T = fastype_of1 (Ts, t);
berghofe@22311
   148
    val U = fastype_of1 (Ts, u)
wenzelm@56253
   149
  in Const (@{const_name Nominal.perm}, T --> U --> U) $ t $ u end;
berghofe@22311
   150
berghofe@22529
   151
fun perm_of_pair (x, y) =
berghofe@22529
   152
  let
berghofe@22529
   153
    val T = fastype_of x;
berghofe@22529
   154
    val pT = mk_permT T
wenzelm@56253
   155
  in Const (@{const_name Cons}, HOLogic.mk_prodT (T, T) --> pT --> pT) $
wenzelm@56253
   156
    HOLogic.mk_prod (x, y) $ Const (@{const_name Nil}, pT)
berghofe@22529
   157
  end;
berghofe@22529
   158
wenzelm@59582
   159
fun mk_not_sym ths = maps (fn th =>
wenzelm@59582
   160
  (case Thm.prop_of th of
wenzelm@59582
   161
    _ $ (Const (@{const_name Not}, _) $ (Const (@{const_name HOL.eq}, _) $ _ $ _)) =>
wenzelm@59582
   162
    [th, th RS not_sym]
wenzelm@59582
   163
  | _ => [th])) ths;
berghofe@22529
   164
wenzelm@56253
   165
fun fresh_const T U = Const (@{const_name Nominal.fresh}, T --> U --> HOLogic.boolT);
berghofe@28373
   166
fun fresh_star_const T U =
wenzelm@56253
   167
  Const (@{const_name Nominal.fresh_star}, HOLogic.mk_setT T --> U --> HOLogic.boolT);
berghofe@25823
   168
wenzelm@59992
   169
fun gen_nominal_datatype prep_specs (config: Old_Datatype_Aux.config) dts thy =
berghofe@17870
   170
  let
wenzelm@45839
   171
    val new_type_names = map (fn ((tname, _, _), _) => Binding.name_of tname) dts;
wenzelm@45701
   172
wenzelm@45839
   173
    val (dts', _) = prep_specs dts thy;
berghofe@17870
   174
berghofe@17870
   175
    val atoms = atoms_of thy;
berghofe@17870
   176
wenzelm@45839
   177
    val tyvars = map (fn ((_, tvs, _), _) => tvs) dts';
wenzelm@45839
   178
    val sorts = flat tyvars;
berghofe@17870
   179
wenzelm@42388
   180
    fun inter_sort thy S S' = Sign.inter_sort thy (S, S');
berghofe@28731
   181
    fun augment_sort_typ thy S =
wenzelm@36428
   182
      let val S = Sign.minimize_sort thy (Sign.certify_sort thy S)
berghofe@28731
   183
      in map_type_tfree (fn (s, S') => TFree (s,
berghofe@28731
   184
        if member (op = o apsnd fst) sorts s then inter_sort thy S S' else S'))
berghofe@28731
   185
      end;
berghofe@28731
   186
    fun augment_sort thy S = map_types (augment_sort_typ thy S);
berghofe@28731
   187
wenzelm@45839
   188
    val types_syntax = map (fn ((tname, tvs, mx), constrs) => (tname, mx)) dts';
wenzelm@45839
   189
    val constr_syntax = map (fn (_, constrs) =>
berghofe@17870
   190
      map (fn (cname, cargs, mx) => (cname, mx)) constrs) dts';
berghofe@17870
   191
wenzelm@45839
   192
    val ps = map (fn ((n, _, _), _) =>
wenzelm@45839
   193
      (Sign.full_name thy n, Sign.full_name thy (Binding.suffix_name "_Rep" n))) dts;
berghofe@17870
   194
    val rps = map Library.swap ps;
berghofe@17870
   195
wenzelm@56253
   196
    fun replace_types (Type (@{type_name ABS}, [T, U])) =
wenzelm@56253
   197
          Type (@{type_name fun}, [T, Type (@{type_name noption}, [replace_types U])])
berghofe@17870
   198
      | replace_types (Type (s, Ts)) =
wenzelm@33035
   199
          Type (the_default s (AList.lookup op = ps s), map replace_types Ts)
berghofe@17870
   200
      | replace_types T = T;
berghofe@17870
   201
wenzelm@45839
   202
    val dts'' = map (fn ((tname, tvs, mx), constrs) =>
wenzelm@45839
   203
      ((Binding.suffix_name "_Rep" tname, tvs, NoSyn),
wenzelm@45701
   204
        map (fn (cname, cargs, mx) => (Binding.suffix_name "_Rep" cname,
wenzelm@45701
   205
          map replace_types cargs, NoSyn)) constrs)) dts';
berghofe@17870
   206
berghofe@17870
   207
    val new_type_names' = map (fn n => n ^ "_Rep") new_type_names;
berghofe@17870
   208
blanchet@58354
   209
    val (full_new_type_names',thy1) = BNF_LFP_Compat.add_datatype [BNF_LFP_Compat.Kill_Type_Args] dts'' thy;
berghofe@17870
   210
blanchet@58354
   211
    val {descr, induct, ...} = BNF_LFP_Compat.the_info thy1 [] (hd full_new_type_names');
blanchet@58112
   212
    fun nth_dtyp i = Old_Datatype_Aux.typ_of_dtyp descr (Old_Datatype_Aux.DtRec i);
berghofe@18107
   213
wenzelm@26651
   214
    val big_name = space_implode "_" new_type_names;
wenzelm@26651
   215
wenzelm@26651
   216
berghofe@17870
   217
    (**** define permutation functions ****)
berghofe@17870
   218
wenzelm@56253
   219
    val permT = mk_permT (TFree ("'x", @{sort type}));
berghofe@17870
   220
    val pi = Free ("pi", permT);
berghofe@17870
   221
    val perm_types = map (fn (i, _) =>
berghofe@17870
   222
      let val T = nth_dtyp i
berghofe@17870
   223
      in permT --> T --> T end) descr;
blanchet@58112
   224
    val perm_names' = Old_Datatype_Prop.indexify_names (map (fn (i, _) =>
blanchet@58112
   225
      "perm_" ^ Old_Datatype_Aux.name_of_typ (nth_dtyp i)) descr);
wenzelm@56253
   226
    val perm_names = replicate (length new_type_names) @{const_name Nominal.perm} @
haftmann@28965
   227
      map (Sign.full_bname thy1) (List.drop (perm_names', length new_type_names));
berghofe@17870
   228
    val perm_names_types = perm_names ~~ perm_types;
berghofe@26680
   229
    val perm_names_types' = perm_names' ~~ perm_types;
berghofe@17870
   230
berghofe@26680
   231
    val perm_eqs = maps (fn (i, (_, _, constrs)) =>
berghofe@17870
   232
      let val T = nth_dtyp i
wenzelm@21365
   233
      in map (fn (cname, dts) =>
berghofe@17870
   234
        let
blanchet@58112
   235
          val Ts = map (Old_Datatype_Aux.typ_of_dtyp descr) dts;
blanchet@58112
   236
          val names = Name.variant_list ["pi"] (Old_Datatype_Prop.make_tnames Ts);
berghofe@17870
   237
          val args = map Free (names ~~ Ts);
berghofe@17870
   238
          val c = Const (cname, Ts ---> T);
berghofe@17870
   239
          fun perm_arg (dt, x) =
berghofe@17870
   240
            let val T = type_of x
blanchet@58112
   241
            in if Old_Datatype_Aux.is_rec_type dt then
wenzelm@40844
   242
                let val Us = binder_types T
wenzelm@46219
   243
                in
wenzelm@46219
   244
                  fold_rev (Term.abs o pair "x") Us
blanchet@58112
   245
                    (Free (nth perm_names_types' (Old_Datatype_Aux.body_index dt)) $ pi $
wenzelm@46219
   246
                      list_comb (x, map (fn (i, U) =>
wenzelm@56253
   247
                        Const (@{const_name Nominal.perm}, permT --> U --> U) $
wenzelm@56253
   248
                          (Const (@{const_name rev}, permT --> permT) $ pi) $
wenzelm@46219
   249
                          Bound i) ((length Us - 1 downto 0) ~~ Us)))
berghofe@17870
   250
                end
wenzelm@56253
   251
              else Const (@{const_name Nominal.perm}, permT --> T --> T) $ pi $ x
wenzelm@21365
   252
            end;
berghofe@17870
   253
        in
wenzelm@63352
   254
          ((Binding.empty_atts, HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@26680
   255
            (Free (nth perm_names_types' i) $
wenzelm@56253
   256
               Free ("pi", mk_permT (TFree ("'x", @{sort type}))) $
berghofe@17870
   257
               list_comb (c, args),
wenzelm@63182
   258
             list_comb (c, map perm_arg (dts ~~ args))))), [], [])
berghofe@17870
   259
        end) constrs
berghofe@26680
   260
      end) descr;
berghofe@17870
   261
berghofe@26689
   262
    val (perm_simps, thy2) =
blanchet@60003
   263
      BNF_LFP_Compat.primrec_overloaded
berghofe@26689
   264
        (map (fn (s, sT) => (s, sT, false))
berghofe@26689
   265
           (List.take (perm_names' ~~ perm_names_types, length new_type_names)))
wenzelm@52788
   266
        (map (fn s => (Binding.name s, NONE, NoSyn)) perm_names') perm_eqs thy1;
berghofe@17870
   267
berghofe@17870
   268
    (**** prove that permutation functions introduced by unfolding are ****)
berghofe@17870
   269
    (**** equivalent to already existing permutation functions         ****)
berghofe@17870
   270
berghofe@17870
   271
    val _ = warning ("length descr: " ^ string_of_int (length descr));
berghofe@17870
   272
    val _ = warning ("length new_type_names: " ^ string_of_int (length new_type_names));
berghofe@17870
   273
blanchet@58112
   274
    val perm_indnames = Old_Datatype_Prop.make_tnames (map body_type perm_types);
haftmann@44685
   275
    val perm_fun_def = Simpdata.mk_eq @{thm perm_fun_def};
berghofe@17870
   276
berghofe@17870
   277
    val unfolded_perm_eq_thms =
berghofe@17870
   278
      if length descr = length new_type_names then []
blanchet@58112
   279
      else map Drule.export_without_context (List.drop (Old_Datatype_Aux.split_conj_thm
wenzelm@51122
   280
        (Goal.prove_global_future thy2 [] []
berghofe@17870
   281
          (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@17870
   282
            (map (fn (c as (s, T), x) =>
berghofe@17870
   283
               let val [T1, T2] = binder_types T
berghofe@17870
   284
               in HOLogic.mk_eq (Const c $ pi $ Free (x, T2),
wenzelm@56253
   285
                 Const (@{const_name Nominal.perm}, T) $ pi $ Free (x, T2))
berghofe@17870
   286
               end)
berghofe@18010
   287
             (perm_names_types ~~ perm_indnames))))
wenzelm@60328
   288
          (fn {context = ctxt, ...} =>
wenzelm@60328
   289
            EVERY [Old_Datatype_Aux.ind_tac ctxt induct perm_indnames 1,
wenzelm@60328
   290
              ALLGOALS (asm_full_simp_tac (ctxt addsimps [perm_fun_def]))])),
berghofe@17870
   291
        length new_type_names));
berghofe@17870
   292
berghofe@17870
   293
    (**** prove [] \<bullet> t = t ****)
berghofe@17870
   294
berghofe@17870
   295
    val _ = warning "perm_empty_thms";
berghofe@17870
   296
wenzelm@32952
   297
    val perm_empty_thms = maps (fn a =>
berghofe@17870
   298
      let val permT = mk_permT (Type (a, []))
blanchet@58112
   299
      in map Drule.export_without_context (List.take (Old_Datatype_Aux.split_conj_thm
wenzelm@51122
   300
        (Goal.prove_global_future thy2 [] []
berghofe@28731
   301
          (augment_sort thy2 [pt_class_of thy2 a]
berghofe@28731
   302
            (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@28731
   303
              (map (fn ((s, T), x) => HOLogic.mk_eq
berghofe@28731
   304
                  (Const (s, permT --> T --> T) $
wenzelm@56253
   305
                     Const (@{const_name Nil}, permT) $ Free (x, T),
berghofe@28731
   306
                   Free (x, T)))
berghofe@28731
   307
               (perm_names ~~
berghofe@28731
   308
                map body_type perm_types ~~ perm_indnames)))))
wenzelm@60328
   309
          (fn {context = ctxt, ...} =>
wenzelm@60328
   310
            EVERY [Old_Datatype_Aux.ind_tac ctxt induct perm_indnames 1,
wenzelm@60328
   311
              ALLGOALS (asm_full_simp_tac ctxt)])),
berghofe@17870
   312
        length new_type_names))
berghofe@17870
   313
      end)
wenzelm@32952
   314
      atoms;
berghofe@17870
   315
berghofe@17870
   316
    (**** prove (pi1 @ pi2) \<bullet> t = pi1 \<bullet> (pi2 \<bullet> t) ****)
berghofe@17870
   317
berghofe@17870
   318
    val _ = warning "perm_append_thms";
berghofe@17870
   319
berghofe@17870
   320
    (*FIXME: these should be looked up statically*)
wenzelm@39557
   321
    val at_pt_inst = Global_Theory.get_thm thy2 "at_pt_inst";
wenzelm@39557
   322
    val pt2 = Global_Theory.get_thm thy2 "pt2";
berghofe@17870
   323
wenzelm@32952
   324
    val perm_append_thms = maps (fn a =>
berghofe@17870
   325
      let
berghofe@17870
   326
        val permT = mk_permT (Type (a, []));
berghofe@17870
   327
        val pi1 = Free ("pi1", permT);
berghofe@17870
   328
        val pi2 = Free ("pi2", permT);
berghofe@28731
   329
        val pt_inst = pt_inst_of thy2 a;
berghofe@17870
   330
        val pt2' = pt_inst RS pt2;
wenzelm@39557
   331
        val pt2_ax = Global_Theory.get_thm thy2 (Long_Name.map_base_name (fn s => "pt_" ^ s ^ "2") a);
blanchet@58112
   332
      in List.take (map Drule.export_without_context (Old_Datatype_Aux.split_conj_thm
wenzelm@51122
   333
        (Goal.prove_global_future thy2 [] []
berghofe@28731
   334
           (augment_sort thy2 [pt_class_of thy2 a]
berghofe@17870
   335
             (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@17870
   336
                (map (fn ((s, T), x) =>
berghofe@17870
   337
                    let val perm = Const (s, permT --> T --> T)
berghofe@17870
   338
                    in HOLogic.mk_eq
wenzelm@56253
   339
                      (perm $ (Const (@{const_name append}, permT --> permT --> permT) $
berghofe@17870
   340
                         pi1 $ pi2) $ Free (x, T),
berghofe@17870
   341
                       perm $ pi1 $ (perm $ pi2 $ Free (x, T)))
berghofe@17870
   342
                    end)
berghofe@17870
   343
                  (perm_names ~~
berghofe@28731
   344
                   map body_type perm_types ~~ perm_indnames)))))
wenzelm@60328
   345
           (fn {context = ctxt, ...} =>
wenzelm@60328
   346
             EVERY [Old_Datatype_Aux.ind_tac ctxt induct perm_indnames 1,
wenzelm@60328
   347
               ALLGOALS (asm_full_simp_tac (ctxt addsimps [pt2', pt2_ax]))]))),
berghofe@17870
   348
         length new_type_names)
wenzelm@32952
   349
      end) atoms;
berghofe@17870
   350
berghofe@17870
   351
    (**** prove pi1 ~ pi2 ==> pi1 \<bullet> t = pi2 \<bullet> t ****)
berghofe@17870
   352
berghofe@17870
   353
    val _ = warning "perm_eq_thms";
berghofe@17870
   354
wenzelm@39557
   355
    val pt3 = Global_Theory.get_thm thy2 "pt3";
wenzelm@39557
   356
    val pt3_rev = Global_Theory.get_thm thy2 "pt3_rev";
berghofe@17870
   357
wenzelm@32952
   358
    val perm_eq_thms = maps (fn a =>
berghofe@17870
   359
      let
berghofe@17870
   360
        val permT = mk_permT (Type (a, []));
berghofe@17870
   361
        val pi1 = Free ("pi1", permT);
berghofe@17870
   362
        val pi2 = Free ("pi2", permT);
berghofe@28731
   363
        val at_inst = at_inst_of thy2 a;
berghofe@28731
   364
        val pt_inst = pt_inst_of thy2 a;
berghofe@17870
   365
        val pt3' = pt_inst RS pt3;
berghofe@17870
   366
        val pt3_rev' = at_inst RS (pt_inst RS pt3_rev);
wenzelm@39557
   367
        val pt3_ax = Global_Theory.get_thm thy2 (Long_Name.map_base_name (fn s => "pt_" ^ s ^ "3") a);
blanchet@58112
   368
      in List.take (map Drule.export_without_context (Old_Datatype_Aux.split_conj_thm
wenzelm@51122
   369
        (Goal.prove_global_future thy2 [] []
berghofe@28731
   370
          (augment_sort thy2 [pt_class_of thy2 a] (Logic.mk_implies
wenzelm@56253
   371
             (HOLogic.mk_Trueprop (Const (@{const_name Nominal.prm_eq},
berghofe@17870
   372
                permT --> permT --> HOLogic.boolT) $ pi1 $ pi2),
berghofe@17870
   373
              HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@17870
   374
                (map (fn ((s, T), x) =>
berghofe@17870
   375
                    let val perm = Const (s, permT --> T --> T)
berghofe@17870
   376
                    in HOLogic.mk_eq
berghofe@17870
   377
                      (perm $ pi1 $ Free (x, T),
berghofe@17870
   378
                       perm $ pi2 $ Free (x, T))
berghofe@17870
   379
                    end)
berghofe@17870
   380
                  (perm_names ~~
berghofe@28731
   381
                   map body_type perm_types ~~ perm_indnames))))))
wenzelm@60328
   382
           (fn {context = ctxt, ...} =>
wenzelm@60328
   383
             EVERY [Old_Datatype_Aux.ind_tac ctxt induct perm_indnames 1,
wenzelm@60328
   384
               ALLGOALS (asm_full_simp_tac (ctxt addsimps [pt3', pt3_rev', pt3_ax]))]))),
berghofe@17870
   385
         length new_type_names)
wenzelm@32952
   386
      end) atoms;
berghofe@17870
   387
berghofe@17870
   388
    (**** prove pi1 \<bullet> (pi2 \<bullet> t) = (pi1 \<bullet> pi2) \<bullet> (pi1 \<bullet> t) ****)
berghofe@17870
   389
wenzelm@39557
   390
    val cp1 = Global_Theory.get_thm thy2 "cp1";
wenzelm@39557
   391
    val dj_cp = Global_Theory.get_thm thy2 "dj_cp";
wenzelm@39557
   392
    val pt_perm_compose = Global_Theory.get_thm thy2 "pt_perm_compose";
wenzelm@39557
   393
    val pt_perm_compose_rev = Global_Theory.get_thm thy2 "pt_perm_compose_rev";
wenzelm@39557
   394
    val dj_perm_perm_forget = Global_Theory.get_thm thy2 "dj_perm_perm_forget";
berghofe@17870
   395
berghofe@17870
   396
    fun composition_instance name1 name2 thy =
berghofe@17870
   397
      let
berghofe@28731
   398
        val cp_class = cp_class_of thy name1 name2;
berghofe@28731
   399
        val pt_class =
berghofe@28731
   400
          if name1 = name2 then [pt_class_of thy name1]
berghofe@28731
   401
          else [];
berghofe@17870
   402
        val permT1 = mk_permT (Type (name1, []));
berghofe@17870
   403
        val permT2 = mk_permT (Type (name2, []));
berghofe@28731
   404
        val Ts = map body_type perm_types;
berghofe@28731
   405
        val cp_inst = cp_inst_of thy name1 name2;
wenzelm@51717
   406
        fun simps ctxt = ctxt addsimps (perm_fun_def ::
berghofe@17870
   407
          (if name1 <> name2 then
berghofe@28731
   408
             let val dj = dj_thm_of thy name2 name1
berghofe@17870
   409
             in [dj RS (cp_inst RS dj_cp), dj RS dj_perm_perm_forget] end
berghofe@17870
   410
           else
berghofe@17870
   411
             let
berghofe@28731
   412
               val at_inst = at_inst_of thy name1;
berghofe@28731
   413
               val pt_inst = pt_inst_of thy name1;
berghofe@17870
   414
             in
berghofe@17870
   415
               [cp_inst RS cp1 RS sym,
berghofe@17870
   416
                at_inst RS (pt_inst RS pt_perm_compose) RS sym,
berghofe@17870
   417
                at_inst RS (pt_inst RS pt_perm_compose_rev) RS sym]
berghofe@17870
   418
            end))
wenzelm@36428
   419
        val sort = Sign.minimize_sort thy (Sign.certify_sort thy (cp_class :: pt_class));
blanchet@58112
   420
        val thms = Old_Datatype_Aux.split_conj_thm (Goal.prove_global_future thy [] []
berghofe@28736
   421
          (augment_sort thy sort
berghofe@17870
   422
            (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@17870
   423
              (map (fn ((s, T), x) =>
berghofe@17870
   424
                  let
berghofe@17870
   425
                    val pi1 = Free ("pi1", permT1);
berghofe@17870
   426
                    val pi2 = Free ("pi2", permT2);
berghofe@17870
   427
                    val perm1 = Const (s, permT1 --> T --> T);
berghofe@17870
   428
                    val perm2 = Const (s, permT2 --> T --> T);
wenzelm@56253
   429
                    val perm3 = Const (@{const_name Nominal.perm}, permT1 --> permT2 --> permT2)
berghofe@17870
   430
                  in HOLogic.mk_eq
berghofe@17870
   431
                    (perm1 $ pi1 $ (perm2 $ pi2 $ Free (x, T)),
berghofe@17870
   432
                     perm2 $ (perm3 $ pi1 $ pi2) $ (perm1 $ pi1 $ Free (x, T)))
berghofe@17870
   433
                  end)
berghofe@28731
   434
                (perm_names ~~ Ts ~~ perm_indnames)))))
wenzelm@60328
   435
          (fn {context = ctxt, ...} =>
wenzelm@60328
   436
            EVERY [Old_Datatype_Aux.ind_tac ctxt induct perm_indnames 1,
wenzelm@60328
   437
              ALLGOALS (asm_full_simp_tac (simps ctxt))]))
berghofe@17870
   438
      in
wenzelm@51685
   439
        fold (fn (s, tvs) => fn thy => Axclass.prove_arity
berghofe@28736
   440
            (s, map (inter_sort thy sort o snd) tvs, [cp_class])
wenzelm@59498
   441
            (fn ctxt' =>
wenzelm@59498
   442
              Class.intro_classes_tac ctxt' [] THEN ALLGOALS (resolve_tac ctxt' thms)) thy)
berghofe@28736
   443
          (full_new_type_names' ~~ tyvars) thy
berghofe@17870
   444
      end;
berghofe@17870
   445
urbanc@18381
   446
    val (perm_thmss,thy3) = thy2 |>
berghofe@17870
   447
      fold (fn name1 => fold (composition_instance name1) atoms) atoms |>
berghofe@28731
   448
      fold (fn atom => fn thy =>
berghofe@28731
   449
        let val pt_name = pt_class_of thy atom
berghofe@28731
   450
        in
wenzelm@51685
   451
          fold (fn (s, tvs) => fn thy => Axclass.prove_arity
berghofe@28736
   452
              (s, map (inter_sort thy [pt_name] o snd) tvs, [pt_name])
wenzelm@58963
   453
              (fn ctxt => EVERY
wenzelm@59498
   454
                [Class.intro_classes_tac ctxt [],
wenzelm@59498
   455
                 resolve_tac ctxt perm_empty_thms 1,
wenzelm@59498
   456
                 resolve_tac ctxt perm_append_thms 1,
wenzelm@59498
   457
                 resolve_tac ctxt perm_eq_thms 1, assume_tac ctxt 1]) thy)
berghofe@28736
   458
            (full_new_type_names' ~~ tyvars) thy
berghofe@28731
   459
        end) atoms |>
wenzelm@39557
   460
      Global_Theory.add_thmss
haftmann@29585
   461
        [((Binding.name (space_implode "_" new_type_names ^ "_unfolded_perm_eq"),
krauss@18759
   462
          unfolded_perm_eq_thms), [Simplifier.simp_add]),
haftmann@29585
   463
         ((Binding.name (space_implode "_" new_type_names ^ "_perm_empty"),
krauss@18759
   464
          perm_empty_thms), [Simplifier.simp_add]),
haftmann@29585
   465
         ((Binding.name (space_implode "_" new_type_names ^ "_perm_append"),
krauss@18759
   466
          perm_append_thms), [Simplifier.simp_add]),
haftmann@29585
   467
         ((Binding.name (space_implode "_" new_type_names ^ "_perm_eq"),
wenzelm@52788
   468
          perm_eq_thms), [Simplifier.simp_add])];
wenzelm@21365
   469
berghofe@17870
   470
    (**** Define representing sets ****)
berghofe@17870
   471
berghofe@17870
   472
    val _ = warning "representing sets";
berghofe@17870
   473
wenzelm@45838
   474
    val rep_set_names =
blanchet@58112
   475
      Old_Datatype_Prop.indexify_names
blanchet@58112
   476
        (map (fn (i, _) => Old_Datatype_Aux.name_of_typ (nth_dtyp i) ^ "_set") descr);
berghofe@17870
   477
    val big_rep_name =
blanchet@58112
   478
      space_implode "_" (Old_Datatype_Prop.indexify_names (map_filter
wenzelm@56253
   479
        (fn (i, (@{type_name noption}, _, _)) => NONE
blanchet@58112
   480
          | (i, _) => SOME (Old_Datatype_Aux.name_of_typ (nth_dtyp i))) descr)) ^ "_set";
berghofe@17870
   481
    val _ = warning ("big_rep_name: " ^ big_rep_name);
berghofe@17870
   482
blanchet@58112
   483
    fun strip_option (dtf as Old_Datatype_Aux.DtType ("fun", [dt, Old_Datatype_Aux.DtRec i])) =
berghofe@17870
   484
          (case AList.lookup op = descr i of
wenzelm@56253
   485
             SOME (@{type_name noption}, _, [(_, [dt']), _]) =>
berghofe@17870
   486
               apfst (cons dt) (strip_option dt')
berghofe@17870
   487
           | _ => ([], dtf))
blanchet@58112
   488
      | strip_option (Old_Datatype_Aux.DtType ("fun",
blanchet@58112
   489
            [dt, Old_Datatype_Aux.DtType (@{type_name noption}, [dt'])])) =
berghofe@18261
   490
          apfst (cons dt) (strip_option dt')
berghofe@17870
   491
      | strip_option dt = ([], dt);
berghofe@17870
   492
blanchet@58112
   493
    val dt_atomTs = distinct op = (map (Old_Datatype_Aux.typ_of_dtyp descr)
wenzelm@32952
   494
      (maps (fn (_, (_, _, cs)) => maps (maps (fst o strip_option) o snd) cs) descr));
berghofe@28731
   495
    val dt_atoms = map (fst o dest_Type) dt_atomTs;
berghofe@18280
   496
berghofe@17870
   497
    fun make_intr s T (cname, cargs) =
berghofe@17870
   498
      let
wenzelm@33338
   499
        fun mk_prem dt (j, j', prems, ts) =
berghofe@17870
   500
          let
berghofe@17870
   501
            val (dts, dt') = strip_option dt;
blanchet@58112
   502
            val (dts', dt'') = Old_Datatype_Aux.strip_dtyp dt';
blanchet@58112
   503
            val Ts = map (Old_Datatype_Aux.typ_of_dtyp descr) dts;
blanchet@58112
   504
            val Us = map (Old_Datatype_Aux.typ_of_dtyp descr) dts';
blanchet@58112
   505
            val T = Old_Datatype_Aux.typ_of_dtyp descr dt'';
blanchet@58112
   506
            val free = Old_Datatype_Aux.mk_Free "x" (Us ---> T) j;
blanchet@58112
   507
            val free' = Old_Datatype_Aux.app_bnds free (length Us);
wenzelm@33338
   508
            fun mk_abs_fun T (i, t) =
berghofe@17870
   509
              let val U = fastype_of t
wenzelm@56253
   510
              in (i + 1, Const (@{const_name Nominal.abs_fun}, [T, U, T] --->
blanchet@58112
   511
                Type (@{type_name noption}, [U])) $ Old_Datatype_Aux.mk_Free "y" T i $ t)
berghofe@17870
   512
              end
berghofe@17870
   513
          in (j + 1, j' + length Ts,
berghofe@17870
   514
            case dt'' of
blanchet@58112
   515
                Old_Datatype_Aux.DtRec k => Logic.list_all (map (pair "x") Us,
wenzelm@42364
   516
                  HOLogic.mk_Trueprop (Free (nth rep_set_names k,
berghofe@21021
   517
                    T --> HOLogic.boolT) $ free')) :: prems
berghofe@17870
   518
              | _ => prems,
wenzelm@33338
   519
            snd (fold_rev mk_abs_fun Ts (j', free)) :: ts)
berghofe@17870
   520
          end;
berghofe@17870
   521
wenzelm@33338
   522
        val (_, _, prems, ts) = fold_rev mk_prem cargs (1, 1, [], []);
berghofe@21021
   523
        val concl = HOLogic.mk_Trueprop (Free (s, T --> HOLogic.boolT) $
berghofe@21021
   524
          list_comb (Const (cname, map fastype_of ts ---> T), ts))
berghofe@17870
   525
      in Logic.list_implies (prems, concl)
berghofe@17870
   526
      end;
berghofe@17870
   527
berghofe@21021
   528
    val (intr_ts, (rep_set_names', recTs')) =
wenzelm@32952
   529
      apfst flat (apsnd ListPair.unzip (ListPair.unzip (map_filter
wenzelm@56253
   530
        (fn ((_, (@{type_name noption}, _, _)), _) => NONE
berghofe@17870
   531
          | ((i, (_, _, constrs)), rep_set_name) =>
berghofe@17870
   532
              let val T = nth_dtyp i
berghofe@17870
   533
              in SOME (map (make_intr rep_set_name T) constrs,
berghofe@21021
   534
                (rep_set_name, T))
berghofe@17870
   535
              end)
berghofe@21021
   536
                (descr ~~ rep_set_names))));
haftmann@28965
   537
    val rep_set_names'' = map (Sign.full_bname thy3) rep_set_names';
berghofe@17870
   538
wenzelm@21365
   539
    val ({raw_induct = rep_induct, intrs = rep_intrs, ...}, thy4) =
wenzelm@33278
   540
      thy3
wenzelm@59880
   541
      |> Sign.concealed
wenzelm@33726
   542
      |> Inductive.add_inductive_global
wenzelm@33669
   543
          {quiet_mode = false, verbose = false, alt_name = Binding.name big_rep_name,
wenzelm@49170
   544
           coind = false, no_elim = true, no_ind = false, skip_mono = true}
haftmann@28965
   545
          (map (fn (s, T) => ((Binding.name s, T --> HOLogic.boolT), NoSyn))
wenzelm@26128
   546
             (rep_set_names' ~~ recTs'))
wenzelm@63352
   547
          [] (map (fn x => (Binding.empty_atts, x)) intr_ts) []
wenzelm@52788
   548
      ||> Sign.restore_naming thy3;
berghofe@17870
   549
berghofe@17870
   550
    (**** Prove that representing set is closed under permutation ****)
berghofe@17870
   551
berghofe@17870
   552
    val _ = warning "proving closure under permutation...";
berghofe@17870
   553
wenzelm@39557
   554
    val abs_perm = Global_Theory.get_thms thy4 "abs_perm";
berghofe@25951
   555
wenzelm@32952
   556
    val perm_indnames' = map_filter
wenzelm@56253
   557
      (fn (x, (_, (@{type_name noption}, _, _))) => NONE | (x, _) => SOME x)
berghofe@17870
   558
      (perm_indnames ~~ descr);
berghofe@17870
   559
wenzelm@35021
   560
    fun mk_perm_closed name = map (fn th => Drule.export_without_context (th RS mp))
blanchet@58112
   561
      (List.take (Old_Datatype_Aux.split_conj_thm (Goal.prove_global_future thy4 [] []
berghofe@28731
   562
        (augment_sort thy4
haftmann@33040
   563
          (pt_class_of thy4 name :: map (cp_class_of thy4 name) (remove (op =) name dt_atoms))
berghofe@28731
   564
          (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj (map
berghofe@28731
   565
            (fn ((s, T), x) =>
berghofe@28731
   566
               let
berghofe@28731
   567
                 val S = Const (s, T --> HOLogic.boolT);
berghofe@28731
   568
                 val permT = mk_permT (Type (name, []))
berghofe@28731
   569
               in HOLogic.mk_imp (S $ Free (x, T),
wenzelm@56253
   570
                 S $ (Const (@{const_name Nominal.perm}, permT --> T --> T) $
berghofe@28731
   571
                   Free ("pi", permT) $ Free (x, T)))
berghofe@28731
   572
               end) (rep_set_names'' ~~ recTs' ~~ perm_indnames')))))
wenzelm@51671
   573
        (fn {context = ctxt, ...} => EVERY
wenzelm@60328
   574
           [Old_Datatype_Aux.ind_tac ctxt rep_induct [] 1,
wenzelm@51717
   575
            ALLGOALS (simp_tac (ctxt addsimps
wenzelm@36945
   576
              (Thm.symmetric perm_fun_def :: abs_perm))),
wenzelm@59498
   577
            ALLGOALS (resolve_tac ctxt rep_intrs THEN_ALL_NEW assume_tac ctxt)])),
berghofe@17870
   578
        length new_type_names));
berghofe@17870
   579
berghofe@17870
   580
    val perm_closed_thmss = map mk_perm_closed atoms;
berghofe@17870
   581
berghofe@17870
   582
    (**** typedef ****)
berghofe@17870
   583
berghofe@17870
   584
    val _ = warning "defining type...";
berghofe@17870
   585
berghofe@18366
   586
    val (typedefs, thy6) =
wenzelm@26651
   587
      thy4
wenzelm@45701
   588
      |> fold_map (fn (((name, mx), tvs), (cname, U)) => fn thy =>
wenzelm@61260
   589
          Typedef.add_typedef_global {overloaded = false}
wenzelm@45701
   590
            (name, map (fn (v, _) => (v, dummyS)) tvs, mx)  (* FIXME keep constraints!? *)
haftmann@37677
   591
            (Const (@{const_name Collect}, (U --> HOLogic.boolT) --> HOLogic.mk_setT U) $
berghofe@21021
   592
               Const (cname, U --> HOLogic.boolT)) NONE
wenzelm@60754
   593
            (fn ctxt =>
wenzelm@60754
   594
              resolve_tac ctxt [exI] 1 THEN
wenzelm@60754
   595
              resolve_tac ctxt [CollectI] 1 THEN
berghofe@17870
   596
              QUIET_BREADTH_FIRST (has_fewer_prems 1)
wenzelm@59498
   597
              (resolve_tac ctxt rep_intrs 1)) thy |> (fn ((_, r), thy) =>
berghofe@17870
   598
        let
berghofe@28736
   599
          val permT = mk_permT
wenzelm@56253
   600
            (TFree (singleton (Name.variant_list (map fst tvs)) "'a", @{sort type}));
berghofe@17870
   601
          val pi = Free ("pi", permT);
wenzelm@45701
   602
          val T = Type (Sign.full_name thy name, map TFree tvs);
berghofe@18366
   603
        in apfst (pair r o hd)
wenzelm@45701
   604
          (Global_Theory.add_defs_unchecked true
wenzelm@45701
   605
            [((Binding.map_name (fn n => "prm_" ^ n ^ "_def") name, Logic.mk_equals
wenzelm@56253
   606
              (Const (@{const_name Nominal.perm}, permT --> T --> T) $ pi $ Free ("x", T),
wenzelm@45701
   607
               Const (Sign.intern_const thy ("Abs_" ^ Binding.name_of name), U --> T) $
wenzelm@56253
   608
                 (Const (@{const_name Nominal.perm}, permT --> U --> U) $ pi $
wenzelm@45701
   609
                   (Const (Sign.intern_const thy ("Rep_" ^ Binding.name_of name), T --> U) $
wenzelm@45701
   610
                     Free ("x", T))))), [])] thy)
berghofe@17870
   611
        end))
wenzelm@52788
   612
        (types_syntax ~~ tyvars ~~ List.take (rep_set_names'' ~~ recTs', length new_type_names));
berghofe@17870
   613
wenzelm@54742
   614
    val ctxt6 = Proof_Context.init_global thy6;
wenzelm@54742
   615
berghofe@17870
   616
    val perm_defs = map snd typedefs;
wenzelm@54742
   617
    val Abs_inverse_thms = map (collect_simp ctxt6 o #Abs_inverse o snd o fst) typedefs;
wenzelm@35994
   618
    val Rep_inverse_thms = map (#Rep_inverse o snd o fst) typedefs;
wenzelm@54742
   619
    val Rep_thms = map (collect_simp ctxt6 o #Rep o snd o fst) typedefs;
berghofe@17870
   620
berghofe@18016
   621
berghofe@17870
   622
    (** prove that new types are in class pt_<name> **)
berghofe@17870
   623
berghofe@17870
   624
    val _ = warning "prove that new types are in class pt_<name> ...";
berghofe@17870
   625
berghofe@28731
   626
    fun pt_instance (atom, perm_closed_thms) =
berghofe@21021
   627
      fold (fn ((((((Abs_inverse, Rep_inverse), Rep),
berghofe@17870
   628
        perm_def), name), tvs), perm_closed) => fn thy =>
berghofe@28731
   629
          let
berghofe@28731
   630
            val pt_class = pt_class_of thy atom;
wenzelm@36428
   631
            val sort = Sign.minimize_sort thy (Sign.certify_sort thy
wenzelm@36428
   632
              (pt_class :: map (cp_class_of thy atom) (remove (op =) atom dt_atoms)))
wenzelm@51685
   633
          in Axclass.prove_arity
berghofe@17870
   634
            (Sign.intern_type thy name,
berghofe@28736
   635
              map (inter_sort thy sort o snd) tvs, [pt_class])
wenzelm@59498
   636
            (fn ctxt => EVERY [Class.intro_classes_tac ctxt [],
wenzelm@54742
   637
              rewrite_goals_tac ctxt [perm_def],
wenzelm@51717
   638
              asm_full_simp_tac (ctxt addsimps [Rep_inverse]) 1,
wenzelm@51717
   639
              asm_full_simp_tac (ctxt addsimps
berghofe@17870
   640
                [Rep RS perm_closed RS Abs_inverse]) 1,
wenzelm@51717
   641
              asm_full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps [Global_Theory.get_thm thy
wenzelm@30364
   642
                ("pt_" ^ Long_Name.base_name atom ^ "3")]) 1]) thy
berghofe@28731
   643
          end)
berghofe@21021
   644
        (Abs_inverse_thms ~~ Rep_inverse_thms ~~ Rep_thms ~~ perm_defs ~~
berghofe@21021
   645
           new_type_names ~~ tyvars ~~ perm_closed_thms);
berghofe@17870
   646
berghofe@17870
   647
berghofe@17870
   648
    (** prove that new types are in class cp_<name1>_<name2> **)
berghofe@17870
   649
berghofe@17870
   650
    val _ = warning "prove that new types are in class cp_<name1>_<name2> ...";
berghofe@17870
   651
berghofe@17870
   652
    fun cp_instance (atom1, perm_closed_thms1) (atom2, perm_closed_thms2) thy =
berghofe@17870
   653
      let
berghofe@28731
   654
        val cp_class = cp_class_of thy atom1 atom2;
wenzelm@36428
   655
        val sort = Sign.minimize_sort thy (Sign.certify_sort thy
haftmann@33040
   656
          (pt_class_of thy atom1 :: map (cp_class_of thy atom1) (remove (op =) atom1 dt_atoms) @
berghofe@28736
   657
           (if atom1 = atom2 then [cp_class_of thy atom1 atom1] else
wenzelm@36428
   658
            pt_class_of thy atom2 :: map (cp_class_of thy atom2) (remove (op =) atom2 dt_atoms))));
berghofe@28731
   659
        val cp1' = cp_inst_of thy atom1 atom2 RS cp1
berghofe@21021
   660
      in fold (fn ((((((Abs_inverse, Rep),
berghofe@17870
   661
        perm_def), name), tvs), perm_closed1), perm_closed2) => fn thy =>
wenzelm@51685
   662
          Axclass.prove_arity
berghofe@17870
   663
            (Sign.intern_type thy name,
berghofe@28736
   664
              map (inter_sort thy sort o snd) tvs, [cp_class])
wenzelm@59498
   665
            (fn ctxt => EVERY [Class.intro_classes_tac ctxt [],
wenzelm@54742
   666
              rewrite_goals_tac ctxt [perm_def],
wenzelm@51717
   667
              asm_full_simp_tac (ctxt addsimps
berghofe@17870
   668
                ((Rep RS perm_closed1 RS Abs_inverse) ::
berghofe@17870
   669
                 (if atom1 = atom2 then []
berghofe@17870
   670
                  else [Rep RS perm_closed2 RS Abs_inverse]))) 1,
wenzelm@58956
   671
              cong_tac ctxt 1,
wenzelm@60754
   672
              resolve_tac ctxt [refl] 1,
wenzelm@60754
   673
              resolve_tac ctxt [cp1'] 1]) thy)
berghofe@21021
   674
        (Abs_inverse_thms ~~ Rep_thms ~~ perm_defs ~~ new_type_names ~~
berghofe@21021
   675
           tyvars ~~ perm_closed_thms1 ~~ perm_closed_thms2) thy
berghofe@17870
   676
      end;
berghofe@17870
   677
berghofe@17870
   678
    val thy7 = fold (fn x => fn thy => thy |>
berghofe@17870
   679
      pt_instance x |>
berghofe@28731
   680
      fold (cp_instance x) (atoms ~~ perm_closed_thmss))
wenzelm@52788
   681
        (atoms ~~ perm_closed_thmss) thy6;
berghofe@17870
   682
berghofe@17870
   683
    (**** constructors ****)
berghofe@17870
   684
wenzelm@33338
   685
    fun mk_abs_fun x t =
berghofe@17870
   686
      let
berghofe@17870
   687
        val T = fastype_of x;
berghofe@17870
   688
        val U = fastype_of t
berghofe@17870
   689
      in
wenzelm@56253
   690
        Const (@{const_name Nominal.abs_fun}, T --> U --> T -->
wenzelm@56253
   691
          Type (@{type_name noption}, [U])) $ x $ t
berghofe@17870
   692
      end;
berghofe@17870
   693
wenzelm@30190
   694
    val (ty_idxs, _) = List.foldl
wenzelm@56253
   695
      (fn ((i, (@{type_name noption}, _, _)), p) => p
berghofe@18016
   696
        | ((i, _), (ty_idxs, j)) => (ty_idxs @ [(i, j)], j + 1)) ([], 0) descr;
berghofe@18016
   697
blanchet@58112
   698
    fun reindex (Old_Datatype_Aux.DtType (s, dts)) = Old_Datatype_Aux.DtType (s, map reindex dts)
blanchet@58112
   699
      | reindex (Old_Datatype_Aux.DtRec i) =
blanchet@58112
   700
        Old_Datatype_Aux.DtRec (the (AList.lookup op = ty_idxs i))
berghofe@18016
   701
      | reindex dt = dt;
berghofe@18016
   702
wenzelm@40627
   703
    fun strip_suffix i s = implode (List.take (raw_explode s, size s - i));  (* FIXME Symbol.explode (?) *)
berghofe@18016
   704
berghofe@18016
   705
    (** strips the "_Rep" in type names *)
wenzelm@21365
   706
    fun strip_nth_name i s =
wenzelm@30364
   707
      let val xs = Long_Name.explode s;
wenzelm@30364
   708
      in Long_Name.implode (Library.nth_map (length xs - i) (strip_suffix 4) xs) end;
berghofe@18016
   709
haftmann@31458
   710
    val (descr'', ndescr) = ListPair.unzip (map_filter
wenzelm@56253
   711
      (fn (i, (@{type_name noption}, _, _)) => NONE
berghofe@18107
   712
        | (i, (s, dts, constrs)) =>
berghofe@18107
   713
             let
berghofe@18107
   714
               val SOME index = AList.lookup op = ty_idxs i;
haftmann@31458
   715
               val (constrs2, constrs1) =
haftmann@31458
   716
                 map_split (fn (cname, cargs) =>
haftmann@31458
   717
                   apsnd (pair (strip_nth_name 2 (strip_nth_name 1 cname)))
haftmann@31458
   718
                   (fold_map (fn dt => fn dts =>
berghofe@18107
   719
                     let val (dts', dt') = strip_option dt
haftmann@31458
   720
                     in ((length dts, length dts'), dts @ dts' @ [reindex dt']) end)
haftmann@31458
   721
                       cargs [])) constrs
berghofe@18107
   722
             in SOME ((index, (strip_nth_name 1 s,  map reindex dts, constrs1)),
berghofe@18107
   723
               (index, constrs2))
berghofe@18107
   724
             end) descr);
urbanc@18045
   725
berghofe@19489
   726
    val (descr1, descr2) = chop (length new_type_names) descr'';
berghofe@18016
   727
    val descr' = [descr1, descr2];
berghofe@18016
   728
berghofe@19710
   729
    fun partition_cargs idxs xs = map (fn (i, j) =>
wenzelm@42364
   730
      (List.take (List.drop (xs, i), j), nth xs (i + j))) idxs;
berghofe@19710
   731
berghofe@19833
   732
    val pdescr = map (fn ((i, (s, dts, constrs)), (_, idxss)) => (i, (s, dts,
berghofe@19833
   733
      map (fn ((cname, cargs), idxs) => (cname, partition_cargs idxs cargs))
berghofe@19833
   734
        (constrs ~~ idxss)))) (descr'' ~~ ndescr);
berghofe@19833
   735
blanchet@58112
   736
    fun nth_dtyp' i = Old_Datatype_Aux.typ_of_dtyp descr'' (Old_Datatype_Aux.DtRec i);
berghofe@17870
   737
berghofe@17870
   738
    val rep_names = map (fn s =>
berghofe@17870
   739
      Sign.intern_const thy7 ("Rep_" ^ s)) new_type_names;
berghofe@17870
   740
    val abs_names = map (fn s =>
berghofe@17870
   741
      Sign.intern_const thy7 ("Abs_" ^ s)) new_type_names;
berghofe@17870
   742
blanchet@58112
   743
    val recTs = Old_Datatype_Aux.get_rec_types descr'';
haftmann@33957
   744
    val newTs' = take (length new_type_names) recTs';
haftmann@33957
   745
    val newTs = take (length new_type_names) recTs;
berghofe@17870
   746
haftmann@28965
   747
    val full_new_type_names = map (Sign.full_bname thy) new_type_names;
berghofe@17870
   748
wenzelm@33244
   749
    fun make_constr_def tname T T' (((cname_rep, _), (cname, cargs)), (cname', mx))
wenzelm@33244
   750
        (thy, defs, eqns) =
berghofe@17870
   751
      let
wenzelm@33338
   752
        fun constr_arg (dts, dt) (j, l_args, r_args) =
berghofe@17870
   753
          let
wenzelm@45838
   754
            val xs =
blanchet@58112
   755
              map (fn (dt, i) =>
blanchet@58112
   756
                  Old_Datatype_Aux.mk_Free "x" (Old_Datatype_Aux.typ_of_dtyp descr'' dt) i)
wenzelm@45838
   757
                (dts ~~ (j upto j + length dts - 1))
blanchet@58112
   758
            val x =
blanchet@58112
   759
              Old_Datatype_Aux.mk_Free "x" (Old_Datatype_Aux.typ_of_dtyp descr'' dt)
blanchet@58112
   760
                (j + length dts)
berghofe@18261
   761
          in
berghofe@18261
   762
            (j + length dts + 1,
berghofe@18261
   763
             xs @ x :: l_args,
wenzelm@33338
   764
             fold_rev mk_abs_fun xs
berghofe@19833
   765
               (case dt of
blanchet@58112
   766
                  Old_Datatype_Aux.DtRec k => if k < length new_type_names then
blanchet@58112
   767
                      Const (nth rep_names k, Old_Datatype_Aux.typ_of_dtyp descr'' dt -->
blanchet@58112
   768
                        Old_Datatype_Aux.typ_of_dtyp descr dt) $ x
blanchet@58372
   769
                    else error "nested recursion not supported"
wenzelm@33338
   770
                | _ => x) :: r_args)
berghofe@17870
   771
          end
berghofe@17870
   772
wenzelm@33338
   773
        val (_, l_args, r_args) = fold_rev constr_arg cargs (1, [], []);
wenzelm@22578
   774
        val abs_name = Sign.intern_const thy ("Abs_" ^ tname);
wenzelm@22578
   775
        val rep_name = Sign.intern_const thy ("Rep_" ^ tname);
berghofe@17870
   776
        val constrT = map fastype_of l_args ---> T;
berghofe@19833
   777
        val lhs = list_comb (Const (cname, constrT), l_args);
berghofe@19833
   778
        val rhs = list_comb (Const (cname_rep, map fastype_of r_args ---> T'), r_args);
berghofe@17870
   779
        val def = Logic.mk_equals (lhs, Const (abs_name, T' --> T) $ rhs);
berghofe@17870
   780
        val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17870
   781
          (Const (rep_name, T --> T') $ lhs, rhs));
wenzelm@30364
   782
        val def_name = (Long_Name.base_name cname) ^ "_def";
berghofe@18366
   783
        val ([def_thm], thy') = thy |>
wenzelm@56239
   784
          Sign.add_consts [(cname', constrT, mx)] |>
wenzelm@52788
   785
          (Global_Theory.add_defs false o map Thm.no_attributes) [(Binding.name def_name, def)];
berghofe@17870
   786
      in (thy', defs @ [def_thm], eqns @ [eqn]) end;
berghofe@17870
   787
wenzelm@33244
   788
    fun dt_constr_defs ((((((_, (_, _, constrs)),
wenzelm@33244
   789
        (_, (_, _, constrs'))), tname), T), T'), constr_syntax) (thy, defs, eqns, dist_lemmas) =
berghofe@17870
   790
      let
wenzelm@59621
   791
        val rep_const = Thm.global_cterm_of thy
berghofe@17870
   792
          (Const (Sign.intern_const thy ("Rep_" ^ tname), T --> T'));
wenzelm@33244
   793
        val dist =
wenzelm@35021
   794
          Drule.export_without_context
wenzelm@60781
   795
            (infer_instantiate (Proof_Context.init_global thy)
wenzelm@60781
   796
              [(#1 (dest_Var distinct_f), rep_const)] Old_Datatype.distinct_lemma);
wenzelm@33244
   797
        val (thy', defs', eqns') = fold (make_constr_def tname T T')
wenzelm@33244
   798
          (constrs ~~ constrs' ~~ constr_syntax) (Sign.add_path tname thy, defs, [])
berghofe@17870
   799
      in
wenzelm@52788
   800
        (Sign.parent_path thy', defs', eqns @ [eqns'], dist_lemmas @ [dist])
berghofe@17870
   801
      end;
berghofe@17870
   802
wenzelm@33244
   803
    val (thy8, constr_defs, constr_rep_eqns, dist_lemmas) = fold dt_constr_defs
wenzelm@33244
   804
      (List.take (descr, length new_type_names) ~~
berghofe@19833
   805
        List.take (pdescr, length new_type_names) ~~
wenzelm@33244
   806
        new_type_names ~~ newTs ~~ newTs' ~~ constr_syntax)
wenzelm@33244
   807
      (thy7, [], [], []);
berghofe@17870
   808
wenzelm@54742
   809
    val abs_inject_thms = map (collect_simp ctxt6 o #Abs_inject o snd o fst) typedefs
wenzelm@35994
   810
    val rep_inject_thms = map (#Rep_inject o snd o fst) typedefs
berghofe@17870
   811
berghofe@17870
   812
    (* prove theorem  Rep_i (Constr_j ...) = Constr'_j ...  *)
wenzelm@21365
   813
berghofe@17870
   814
    fun prove_constr_rep_thm eqn =
berghofe@17870
   815
      let
berghofe@17870
   816
        val inj_thms = map (fn r => r RS iffD1) abs_inject_thms;
berghofe@21021
   817
        val rewrites = constr_defs @ map mk_meta_eq Rep_inverse_thms
wenzelm@54742
   818
      in Goal.prove_global_future thy8 [] [] eqn (fn {context = ctxt, ...} => EVERY
wenzelm@59498
   819
        [resolve_tac ctxt inj_thms 1,
wenzelm@54742
   820
         rewrite_goals_tac ctxt rewrites,
wenzelm@60754
   821
         resolve_tac ctxt [refl] 3,
wenzelm@59498
   822
         resolve_tac ctxt rep_intrs 2,
wenzelm@59498
   823
         REPEAT (resolve_tac ctxt Rep_thms 1)])
berghofe@17870
   824
      end;
berghofe@17870
   825
berghofe@17870
   826
    val constr_rep_thmss = map (map prove_constr_rep_thm) constr_rep_eqns;
berghofe@17870
   827
berghofe@17870
   828
    (* prove theorem  pi \<bullet> Rep_i x = Rep_i (pi \<bullet> x) *)
berghofe@17870
   829
berghofe@17870
   830
    fun prove_perm_rep_perm (atom, perm_closed_thms) = map (fn th =>
berghofe@17870
   831
      let
wenzelm@59582
   832
        val _ $ (_ $ (Rep $ x)) = Logic.unvarify_global (Thm.prop_of th);
berghofe@17870
   833
        val Type ("fun", [T, U]) = fastype_of Rep;
berghofe@17870
   834
        val permT = mk_permT (Type (atom, []));
berghofe@17870
   835
        val pi = Free ("pi", permT);
berghofe@17870
   836
      in
wenzelm@51122
   837
        Goal.prove_global_future thy8 [] []
berghofe@28731
   838
          (augment_sort thy8
haftmann@33040
   839
            (pt_class_of thy8 atom :: map (cp_class_of thy8 atom) (remove (op =) atom dt_atoms))
berghofe@28731
   840
            (HOLogic.mk_Trueprop (HOLogic.mk_eq
wenzelm@56253
   841
              (Const (@{const_name Nominal.perm}, permT --> U --> U) $ pi $ (Rep $ x),
wenzelm@56253
   842
               Rep $ (Const (@{const_name Nominal.perm}, permT --> T --> T) $ pi $ x)))))
wenzelm@51717
   843
          (fn {context = ctxt, ...} =>
wenzelm@51717
   844
            simp_tac (put_simpset HOL_basic_ss ctxt addsimps (perm_defs @ Abs_inverse_thms @
wenzelm@20046
   845
            perm_closed_thms @ Rep_thms)) 1)
berghofe@17870
   846
      end) Rep_thms;
berghofe@17870
   847
wenzelm@32952
   848
    val perm_rep_perm_thms = maps prove_perm_rep_perm (atoms ~~ perm_closed_thmss);
berghofe@17870
   849
berghofe@17870
   850
    (* prove distinctness theorems *)
berghofe@17870
   851
blanchet@58112
   852
    val distinct_props = Old_Datatype_Prop.make_distincts descr';
haftmann@27300
   853
    val dist_rewrites = map2 (fn rep_thms => fn dist_lemma =>
haftmann@27300
   854
      dist_lemma :: rep_thms @ [In0_eq, In1_eq, In0_not_In1, In1_not_In0])
haftmann@27300
   855
        constr_rep_thmss dist_lemmas;
berghofe@17870
   856
wenzelm@45889
   857
    fun prove_distinct_thms _ [] = []
wenzelm@45889
   858
      | prove_distinct_thms (p as (rep_thms, dist_lemma)) (t :: ts) =
berghofe@17870
   859
          let
wenzelm@51671
   860
            val dist_thm = Goal.prove_global_future thy8 [] [] t (fn {context = ctxt, ...} =>
wenzelm@51717
   861
              simp_tac (ctxt addsimps (dist_lemma :: rep_thms)) 1)
wenzelm@35021
   862
          in
wenzelm@35021
   863
            dist_thm :: Drule.export_without_context (dist_thm RS not_sym) ::
wenzelm@45889
   864
              prove_distinct_thms p ts
berghofe@17870
   865
          end;
berghofe@17870
   866
haftmann@26969
   867
    val distinct_thms = map2 prove_distinct_thms
haftmann@26969
   868
      (constr_rep_thmss ~~ dist_lemmas) distinct_props;
berghofe@17870
   869
berghofe@17870
   870
    (** prove equations for permutation functions **)
berghofe@17870
   871
berghofe@17870
   872
    val perm_simps' = map (fn (((i, (_, _, constrs)), tname), constr_rep_thms) =>
berghofe@19833
   873
      let val T = nth_dtyp' i
wenzelm@32952
   874
      in maps (fn (atom, perm_closed_thms) =>
wenzelm@21365
   875
          map (fn ((cname, dts), constr_rep_thm) =>
berghofe@17870
   876
        let
berghofe@17870
   877
          val cname = Sign.intern_const thy8
wenzelm@30364
   878
            (Long_Name.append tname (Long_Name.base_name cname));
berghofe@17870
   879
          val permT = mk_permT (Type (atom, []));
berghofe@17870
   880
          val pi = Free ("pi", permT);
berghofe@17870
   881
berghofe@17870
   882
          fun perm t =
berghofe@17870
   883
            let val T = fastype_of t
wenzelm@56253
   884
            in Const (@{const_name Nominal.perm}, permT --> T --> T) $ pi $ t end;
berghofe@17870
   885
wenzelm@33338
   886
          fun constr_arg (dts, dt) (j, l_args, r_args) =
berghofe@17870
   887
            let
blanchet@58112
   888
              val Ts = map (Old_Datatype_Aux.typ_of_dtyp descr'') dts;
wenzelm@45838
   889
              val xs =
blanchet@58112
   890
                map (fn (T, i) => Old_Datatype_Aux.mk_Free "x" T i)
wenzelm@45838
   891
                  (Ts ~~ (j upto j + length dts - 1));
wenzelm@45838
   892
              val x =
blanchet@58112
   893
                Old_Datatype_Aux.mk_Free "x" (Old_Datatype_Aux.typ_of_dtyp descr'' dt)
blanchet@58112
   894
                  (j + length dts);
berghofe@18261
   895
            in
berghofe@18261
   896
              (j + length dts + 1,
berghofe@18261
   897
               xs @ x :: l_args,
berghofe@18261
   898
               map perm (xs @ [x]) @ r_args)
berghofe@17870
   899
            end
berghofe@17870
   900
wenzelm@33338
   901
          val (_, l_args, r_args) = fold_rev constr_arg dts (1, [], []);
berghofe@17870
   902
          val c = Const (cname, map fastype_of l_args ---> T)
berghofe@17870
   903
        in
wenzelm@51122
   904
          Goal.prove_global_future thy8 [] []
berghofe@28731
   905
            (augment_sort thy8
haftmann@33040
   906
              (pt_class_of thy8 atom :: map (cp_class_of thy8 atom) (remove (op =) atom dt_atoms))
berghofe@28731
   907
              (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@28731
   908
                (perm (list_comb (c, l_args)), list_comb (c, r_args)))))
wenzelm@51671
   909
            (fn {context = ctxt, ...} => EVERY
wenzelm@51717
   910
              [simp_tac (ctxt addsimps (constr_rep_thm :: perm_defs)) 1,
wenzelm@51717
   911
               simp_tac (put_simpset HOL_basic_ss ctxt addsimps (Rep_thms @ Abs_inverse_thms @
berghofe@17870
   912
                 constr_defs @ perm_closed_thms)) 1,
wenzelm@51717
   913
               TRY (simp_tac (put_simpset HOL_basic_ss ctxt addsimps
wenzelm@36945
   914
                 (Thm.symmetric perm_fun_def :: abs_perm)) 1),
wenzelm@51717
   915
               TRY (simp_tac (put_simpset HOL_basic_ss ctxt addsimps
berghofe@17870
   916
                 (perm_fun_def :: perm_defs @ Rep_thms @ Abs_inverse_thms @
wenzelm@20046
   917
                    perm_closed_thms)) 1)])
wenzelm@32952
   918
        end) (constrs ~~ constr_rep_thms)) (atoms ~~ perm_closed_thmss)
berghofe@19833
   919
      end) (List.take (pdescr, length new_type_names) ~~ new_type_names ~~ constr_rep_thmss);
berghofe@17870
   920
berghofe@17870
   921
    (** prove injectivity of constructors **)
berghofe@17870
   922
berghofe@17870
   923
    val rep_inject_thms' = map (fn th => th RS sym) rep_inject_thms;
wenzelm@39557
   924
    val alpha = Global_Theory.get_thms thy8 "alpha";
wenzelm@39557
   925
    val abs_fresh = Global_Theory.get_thms thy8 "abs_fresh";
berghofe@17870
   926
berghofe@28731
   927
    val pt_cp_sort =
berghofe@28731
   928
      map (pt_class_of thy8) dt_atoms @
haftmann@33040
   929
      maps (fn s => map (cp_class_of thy8 s) (remove (op =) s dt_atoms)) dt_atoms;
berghofe@28731
   930
berghofe@17870
   931
    val inject_thms = map (fn (((i, (_, _, constrs)), tname), constr_rep_thms) =>
berghofe@19833
   932
      let val T = nth_dtyp' i
wenzelm@32952
   933
      in map_filter (fn ((cname, dts), constr_rep_thm) =>
berghofe@17870
   934
        if null dts then NONE else SOME
berghofe@17870
   935
        let
berghofe@17870
   936
          val cname = Sign.intern_const thy8
wenzelm@30364
   937
            (Long_Name.append tname (Long_Name.base_name cname));
berghofe@17870
   938
wenzelm@33338
   939
          fun make_inj (dts, dt) (j, args1, args2, eqs) =
berghofe@17870
   940
            let
wenzelm@45838
   941
              val Ts_idx =
blanchet@58112
   942
                map (Old_Datatype_Aux.typ_of_dtyp descr'') dts ~~ (j upto j + length dts - 1);
blanchet@58112
   943
              val xs = map (fn (T, i) => Old_Datatype_Aux.mk_Free "x" T i) Ts_idx;
blanchet@58112
   944
              val ys = map (fn (T, i) => Old_Datatype_Aux.mk_Free "y" T i) Ts_idx;
wenzelm@45838
   945
              val x =
blanchet@58112
   946
                Old_Datatype_Aux.mk_Free "x" (Old_Datatype_Aux.typ_of_dtyp descr'' dt)
blanchet@58112
   947
                  (j + length dts);
wenzelm@45838
   948
              val y =
blanchet@58112
   949
                Old_Datatype_Aux.mk_Free "y" (Old_Datatype_Aux.typ_of_dtyp descr'' dt)
blanchet@58112
   950
                  (j + length dts);
berghofe@18261
   951
            in
berghofe@18261
   952
              (j + length dts + 1,
berghofe@18261
   953
               xs @ (x :: args1), ys @ (y :: args2),
berghofe@18261
   954
               HOLogic.mk_eq
wenzelm@33338
   955
                 (fold_rev mk_abs_fun xs x, fold_rev mk_abs_fun ys y) :: eqs)
berghofe@17870
   956
            end;
berghofe@17870
   957
wenzelm@33338
   958
          val (_, args1, args2, eqs) = fold_rev make_inj dts (1, [], [], []);
berghofe@17870
   959
          val Ts = map fastype_of args1;
berghofe@17870
   960
          val c = Const (cname, Ts ---> T)
berghofe@17870
   961
        in
wenzelm@51122
   962
          Goal.prove_global_future thy8 [] []
berghofe@28731
   963
            (augment_sort thy8 pt_cp_sort
berghofe@28731
   964
              (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@28731
   965
                (HOLogic.mk_eq (list_comb (c, args1), list_comb (c, args2)),
berghofe@28731
   966
                 foldr1 HOLogic.mk_conj eqs))))
wenzelm@51671
   967
            (fn {context = ctxt, ...} => EVERY
wenzelm@51717
   968
               [asm_full_simp_tac (ctxt addsimps (constr_rep_thm ::
berghofe@17870
   969
                  rep_inject_thms')) 1,
wenzelm@51717
   970
                TRY (asm_full_simp_tac (put_simpset HOL_basic_ss ctxt
wenzelm@51717
   971
                  addsimps (fresh_def :: supp_def ::
berghofe@17870
   972
                  alpha @ abs_perm @ abs_fresh @ rep_inject_thms @
berghofe@28731
   973
                  perm_rep_perm_thms)) 1)])
berghofe@17870
   974
        end) (constrs ~~ constr_rep_thms)
berghofe@19833
   975
      end) (List.take (pdescr, length new_type_names) ~~ new_type_names ~~ constr_rep_thmss);
berghofe@17870
   976
berghofe@17872
   977
    (** equations for support and freshness **)
berghofe@17872
   978
berghofe@17872
   979
    val (supp_thms, fresh_thms) = ListPair.unzip (map ListPair.unzip
berghofe@17872
   980
      (map (fn ((((i, (_, _, constrs)), tname), inject_thms'), perm_thms') =>
berghofe@19833
   981
      let val T = nth_dtyp' i
wenzelm@32952
   982
      in maps (fn (cname, dts) => map (fn atom =>
berghofe@17872
   983
        let
berghofe@17872
   984
          val cname = Sign.intern_const thy8
wenzelm@30364
   985
            (Long_Name.append tname (Long_Name.base_name cname));
berghofe@17872
   986
          val atomT = Type (atom, []);
berghofe@17872
   987
wenzelm@33338
   988
          fun process_constr (dts, dt) (j, args1, args2) =
berghofe@17872
   989
            let
wenzelm@45838
   990
              val Ts_idx =
blanchet@58112
   991
                map (Old_Datatype_Aux.typ_of_dtyp descr'') dts ~~ (j upto j + length dts - 1);
blanchet@58112
   992
              val xs = map (fn (T, i) => Old_Datatype_Aux.mk_Free "x" T i) Ts_idx;
wenzelm@45838
   993
              val x =
blanchet@58112
   994
                Old_Datatype_Aux.mk_Free "x" (Old_Datatype_Aux.typ_of_dtyp descr'' dt)
blanchet@58112
   995
                  (j + length dts);
berghofe@18261
   996
            in
berghofe@18261
   997
              (j + length dts + 1,
wenzelm@33338
   998
               xs @ (x :: args1), fold_rev mk_abs_fun xs x :: args2)
berghofe@17872
   999
            end;
berghofe@17872
  1000
wenzelm@33338
  1001
          val (_, args1, args2) = fold_rev process_constr dts (1, [], []);
berghofe@17872
  1002
          val Ts = map fastype_of args1;
berghofe@17872
  1003
          val c = list_comb (Const (cname, Ts ---> T), args1);
berghofe@17872
  1004
          fun supp t =
wenzelm@56253
  1005
            Const (@{const_name Nominal.supp}, fastype_of t --> HOLogic.mk_setT atomT) $ t;
berghofe@25823
  1006
          fun fresh t = fresh_const atomT (fastype_of t) $ Free ("a", atomT) $ t;
wenzelm@51122
  1007
          val supp_thm = Goal.prove_global_future thy8 [] []
berghofe@28731
  1008
            (augment_sort thy8 pt_cp_sort
berghofe@17872
  1009
              (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@17872
  1010
                (supp c,
haftmann@30450
  1011
                 if null dts then HOLogic.mk_set atomT []
wenzelm@35402
  1012
                 else foldr1 (HOLogic.mk_binop @{const_abbrev union}) (map supp args2)))))
wenzelm@51717
  1013
            (fn {context = ctxt, ...} =>
wenzelm@51717
  1014
              simp_tac (put_simpset HOL_basic_ss ctxt addsimps (supp_def ::
wenzelm@46161
  1015
                 Un_assoc :: @{thm de_Morgan_conj} :: Collect_disj_eq :: finite_Un ::
wenzelm@45654
  1016
                 Collect_False_empty :: finite_emptyI :: @{thms simp_thms} @
wenzelm@20046
  1017
                 abs_perm @ abs_fresh @ inject_thms' @ perm_thms')) 1)
berghofe@17872
  1018
        in
berghofe@17872
  1019
          (supp_thm,
wenzelm@51122
  1020
           Goal.prove_global_future thy8 [] [] (augment_sort thy8 pt_cp_sort
berghofe@28731
  1021
             (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@28731
  1022
               (fresh c,
wenzelm@45740
  1023
                if null dts then @{term True}
berghofe@28731
  1024
                else foldr1 HOLogic.mk_conj (map fresh args2)))))
wenzelm@51717
  1025
             (fn {context = ctxt, ...} =>
wenzelm@51717
  1026
               simp_tac (put_simpset HOL_ss ctxt addsimps [Un_iff, empty_iff, fresh_def, supp_thm]) 1))
wenzelm@32952
  1027
        end) atoms) constrs
berghofe@19833
  1028
      end) (List.take (pdescr, length new_type_names) ~~ new_type_names ~~ inject_thms ~~ perm_simps')));
berghofe@17872
  1029
berghofe@18107
  1030
    (**** weak induction theorem ****)
berghofe@18016
  1031
wenzelm@33244
  1032
    fun mk_indrule_lemma (((i, _), T), U) (prems, concls) =
berghofe@18016
  1033
      let
blanchet@58112
  1034
        val Rep_t = Const (nth rep_names i, T --> U) $ Old_Datatype_Aux.mk_Free "x" T i;
berghofe@18016
  1035
wenzelm@42364
  1036
        val Abs_t =  Const (nth abs_names i, U --> T);
berghofe@18016
  1037
wenzelm@45838
  1038
      in
wenzelm@45838
  1039
        (prems @ [HOLogic.imp $
wenzelm@42364
  1040
            (Const (nth rep_set_names'' i, U --> HOLogic.boolT) $ Rep_t) $
blanchet@58112
  1041
              (Old_Datatype_Aux.mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ (Abs_t $ Rep_t))],
wenzelm@45838
  1042
         concls @
blanchet@58112
  1043
           [Old_Datatype_Aux.mk_Free "P" (T --> HOLogic.boolT) (i + 1) $
blanchet@58112
  1044
              Old_Datatype_Aux.mk_Free "x" T i])
berghofe@18016
  1045
      end;
berghofe@18016
  1046
berghofe@18016
  1047
    val (indrule_lemma_prems, indrule_lemma_concls) =
wenzelm@33244
  1048
      fold mk_indrule_lemma (descr'' ~~ recTs ~~ recTs') ([], []);
berghofe@18016
  1049
wenzelm@51122
  1050
    val indrule_lemma = Goal.prove_global_future thy8 [] []
berghofe@18016
  1051
      (Logic.mk_implies
blanchet@58112
  1052
        (HOLogic.mk_Trueprop (Old_Datatype_Aux.mk_conj indrule_lemma_prems),
blanchet@58112
  1053
         HOLogic.mk_Trueprop (Old_Datatype_Aux.mk_conj indrule_lemma_concls)))
wenzelm@51717
  1054
         (fn {context = ctxt, ...} => EVERY
wenzelm@60754
  1055
           [REPEAT (eresolve_tac ctxt [conjE] 1),
berghofe@18016
  1056
            REPEAT (EVERY
wenzelm@60754
  1057
              [TRY (resolve_tac ctxt [conjI] 1),
wenzelm@51717
  1058
               full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps Rep_inverse_thms) 1,
wenzelm@60754
  1059
               eresolve_tac ctxt [mp] 1,
wenzelm@60754
  1060
               resolve_tac ctxt Rep_thms 1])]);
berghofe@18016
  1061
wenzelm@59582
  1062
    val Ps = map head_of (HOLogic.dest_conj (HOLogic.dest_Trueprop (Thm.concl_of indrule_lemma)));
berghofe@18016
  1063
    val frees = if length Ps = 1 then [Free ("P", snd (dest_Var (hd Ps)))] else
berghofe@18016
  1064
      map (Free o apfst fst o dest_Var) Ps;
berghofe@18016
  1065
berghofe@19833
  1066
    val Abs_inverse_thms' = map (fn r => r RS subst) Abs_inverse_thms;
berghofe@18016
  1067
blanchet@58112
  1068
    val dt_induct_prop = Old_Datatype_Prop.make_ind descr';
wenzelm@51122
  1069
    val dt_induct = Goal.prove_global_future thy8 []
berghofe@18016
  1070
      (Logic.strip_imp_prems dt_induct_prop) (Logic.strip_imp_concl dt_induct_prop)
wenzelm@60781
  1071
      (fn {prems, context = ctxt} =>
wenzelm@60781
  1072
        let
wenzelm@60781
  1073
          val indrule_lemma' = infer_instantiate ctxt
wenzelm@60781
  1074
            (map (#1 o dest_Var) Ps ~~ map (Thm.cterm_of ctxt) frees) indrule_lemma;
wenzelm@60781
  1075
        in
wenzelm@60781
  1076
          EVERY [resolve_tac ctxt [indrule_lemma'] 1,
wenzelm@60781
  1077
           (Old_Datatype_Aux.ind_tac ctxt rep_induct [] THEN_ALL_NEW
wenzelm@60781
  1078
             Object_Logic.atomize_prems_tac ctxt) 1,
wenzelm@60781
  1079
           EVERY (map (fn (prem, r) => (EVERY
wenzelm@60781
  1080
             [REPEAT (eresolve_tac ctxt Abs_inverse_thms' 1),
wenzelm@60781
  1081
              simp_tac (put_simpset HOL_basic_ss ctxt addsimps [Thm.symmetric r]) 1,
wenzelm@60781
  1082
              DEPTH_SOLVE_1
wenzelm@60781
  1083
                (assume_tac ctxt 1 ORELSE resolve_tac ctxt [prem] 1 ORELSE eresolve_tac ctxt [allE] 1)]))
wenzelm@60781
  1084
                  (prems ~~ constr_defs))]
wenzelm@60781
  1085
        end);
berghofe@18016
  1086
blanchet@58112
  1087
    val case_names_induct = Old_Datatype_Data.mk_case_names_induct descr'';
berghofe@18016
  1088
berghofe@18066
  1089
    (**** prove that new datatypes have finite support ****)
berghofe@18066
  1090
urbanc@18246
  1091
    val _ = warning "proving finite support for the new datatype";
urbanc@18246
  1092
blanchet@58112
  1093
    val indnames = Old_Datatype_Prop.make_tnames recTs;
berghofe@18066
  1094
wenzelm@39557
  1095
    val abs_supp = Global_Theory.get_thms thy8 "abs_supp";
wenzelm@39557
  1096
    val supp_atm = Global_Theory.get_thms thy8 "supp_atm";
berghofe@18066
  1097
berghofe@18066
  1098
    val finite_supp_thms = map (fn atom =>
berghofe@18066
  1099
      let val atomT = Type (atom, [])
wenzelm@35021
  1100
      in map Drule.export_without_context (List.take
blanchet@58112
  1101
        (Old_Datatype_Aux.split_conj_thm (Goal.prove_global_future thy8 [] []
berghofe@28731
  1102
           (augment_sort thy8 (fs_class_of thy8 atom :: pt_cp_sort)
berghofe@28731
  1103
             (HOLogic.mk_Trueprop
berghofe@28731
  1104
               (foldr1 HOLogic.mk_conj (map (fn (s, T) =>
wenzelm@56253
  1105
                 Const (@{const_name finite}, HOLogic.mk_setT atomT --> HOLogic.boolT) $
wenzelm@56253
  1106
                   (Const (@{const_name Nominal.supp}, T --> HOLogic.mk_setT atomT) $ Free (s, T)))
berghofe@28731
  1107
                   (indnames ~~ recTs)))))
wenzelm@60328
  1108
           (fn {context = ctxt, ...} =>
wenzelm@60328
  1109
             Old_Datatype_Aux.ind_tac ctxt dt_induct indnames 1 THEN
wenzelm@60328
  1110
             ALLGOALS (asm_full_simp_tac (ctxt addsimps
urbanc@18067
  1111
              (abs_supp @ supp_atm @
wenzelm@39557
  1112
               Global_Theory.get_thms thy8 ("fs_" ^ Long_Name.base_name atom ^ "1") @
wenzelm@32952
  1113
               flat supp_thms))))),
berghofe@18066
  1114
         length new_type_names))
berghofe@18066
  1115
      end) atoms;
berghofe@18066
  1116
krauss@18759
  1117
    val simp_atts = replicate (length new_type_names) [Simplifier.simp_add];
berghofe@18658
  1118
wenzelm@32960
  1119
        (* Function to add both the simp and eqvt attributes *)
urbanc@22245
  1120
        (* These two attributes are duplicated on all the types in the mutual nominal datatypes *)
urbanc@22245
  1121
urbanc@22245
  1122
    val simp_eqvt_atts = replicate (length new_type_names) [Simplifier.simp_add, NominalThmDecls.eqvt_add];
urbanc@22245
  1123
 
berghofe@18658
  1124
    val (_, thy9) = thy8 |>
wenzelm@24712
  1125
      Sign.add_path big_name |>
wenzelm@39557
  1126
      Global_Theory.add_thms [((Binding.name "induct", dt_induct), [case_names_induct])] ||>>
wenzelm@60359
  1127
      Global_Theory.add_thmss
wenzelm@60359
  1128
        [((Binding.name "inducts", projections (Proof_Context.init_global thy8) dt_induct),
wenzelm@60359
  1129
            [case_names_induct])] ||>
wenzelm@24712
  1130
      Sign.parent_path ||>>
blanchet@58112
  1131
      Old_Datatype_Aux.store_thmss_atts "distinct" new_type_names simp_atts distinct_thms ||>>
blanchet@58112
  1132
      Old_Datatype_Aux.store_thmss "constr_rep" new_type_names constr_rep_thmss ||>>
blanchet@58112
  1133
      Old_Datatype_Aux.store_thmss_atts "perm" new_type_names simp_eqvt_atts perm_simps' ||>>
blanchet@58112
  1134
      Old_Datatype_Aux.store_thmss "inject" new_type_names inject_thms ||>>
blanchet@58112
  1135
      Old_Datatype_Aux.store_thmss "supp" new_type_names supp_thms ||>>
blanchet@58112
  1136
      Old_Datatype_Aux.store_thmss_atts "fresh" new_type_names simp_atts fresh_thms ||>
berghofe@18658
  1137
      fold (fn (atom, ths) => fn thy =>
berghofe@28736
  1138
        let
berghofe@28736
  1139
          val class = fs_class_of thy atom;
wenzelm@36428
  1140
          val sort = Sign.minimize_sort thy (Sign.certify_sort thy (class :: pt_cp_sort));
wenzelm@51685
  1141
        in fold (fn Type (s, Ts) => Axclass.prove_arity
berghofe@28736
  1142
          (s, map (inter_sort thy sort o snd o dest_TFree) Ts, [class])
wenzelm@59498
  1143
          (fn ctxt => Class.intro_classes_tac ctxt [] THEN resolve_tac ctxt ths 1)) newTs thy
wenzelm@52788
  1144
        end) (atoms ~~ finite_supp_thms);
berghofe@18658
  1145
berghofe@18107
  1146
    (**** strong induction theorem ****)
berghofe@18107
  1147
berghofe@18107
  1148
    val pnames = if length descr'' = 1 then ["P"]
berghofe@18107
  1149
      else map (fn i => "P" ^ string_of_int i) (1 upto length descr'');
wenzelm@56253
  1150
    val ind_sort = if null dt_atomTs then @{sort type}
wenzelm@36428
  1151
      else Sign.minimize_sort thy9 (Sign.certify_sort thy9 (map (fs_class_of thy9) dt_atoms));
berghofe@18107
  1152
    val fsT = TFree ("'n", ind_sort);
wenzelm@56253
  1153
    val fsT' = TFree ("'n", @{sort type});
berghofe@18107
  1154
berghofe@18658
  1155
    val fresh_fs = map (fn (s, T) => (T, Free (s, fsT' --> HOLogic.mk_setT T)))
blanchet@58112
  1156
      (Old_Datatype_Prop.indexify_names (replicate (length dt_atomTs) "f") ~~ dt_atomTs);
berghofe@18658
  1157
wenzelm@42364
  1158
    fun make_pred fsT i T = Free (nth pnames i, fsT --> T --> HOLogic.boolT);
berghofe@18107
  1159
berghofe@19851
  1160
    fun mk_fresh1 xs [] = []
berghofe@19851
  1161
      | mk_fresh1 xs ((y as (_, T)) :: ys) = map (fn x => HOLogic.mk_Trueprop
berghofe@19851
  1162
            (HOLogic.mk_not (HOLogic.mk_eq (Free y, Free x))))
berghofe@19851
  1163
              (filter (fn (_, U) => T = U) (rev xs)) @
berghofe@19851
  1164
          mk_fresh1 (y :: xs) ys;
berghofe@19851
  1165
berghofe@19851
  1166
    fun mk_fresh2 xss [] = []
wenzelm@32952
  1167
      | mk_fresh2 xss ((p as (ys, _)) :: yss) = maps (fn y as (_, T) =>
berghofe@19851
  1168
            map (fn (_, x as (_, U)) => HOLogic.mk_Trueprop
wenzelm@32952
  1169
              (fresh_const T U $ Free y $ Free x)) (rev xss @ yss)) ys @
berghofe@19851
  1170
          mk_fresh2 (p :: xss) yss;
berghofe@19851
  1171
berghofe@18658
  1172
    fun make_ind_prem fsT f k T ((cname, cargs), idxs) =
berghofe@18107
  1173
      let
blanchet@58112
  1174
        val recs = filter Old_Datatype_Aux.is_rec_type cargs;
blanchet@58112
  1175
        val Ts = map (Old_Datatype_Aux.typ_of_dtyp descr'') cargs;
blanchet@58112
  1176
        val recTs' = map (Old_Datatype_Aux.typ_of_dtyp descr'') recs;
blanchet@58112
  1177
        val tnames = Name.variant_list pnames (Old_Datatype_Prop.make_tnames Ts);
blanchet@58112
  1178
        val rec_tnames = map fst (filter (Old_Datatype_Aux.is_rec_type o snd) (tnames ~~ cargs));
berghofe@18107
  1179
        val frees = tnames ~~ Ts;
berghofe@19710
  1180
        val frees' = partition_cargs idxs frees;
wenzelm@43324
  1181
        val z = (singleton (Name.variant_list tnames) "z", fsT);
berghofe@18107
  1182
berghofe@18107
  1183
        fun mk_prem ((dt, s), T) =
berghofe@18107
  1184
          let
berghofe@18107
  1185
            val (Us, U) = strip_type T;
wenzelm@45838
  1186
            val l = length Us;
wenzelm@45838
  1187
          in
wenzelm@46218
  1188
            Logic.list_all (z :: map (pair "x") Us,
wenzelm@45838
  1189
              HOLogic.mk_Trueprop
blanchet@58112
  1190
                (make_pred fsT (Old_Datatype_Aux.body_index dt) U $ Bound l $
blanchet@58112
  1191
                  Old_Datatype_Aux.app_bnds (Free (s, T)) l))
berghofe@18107
  1192
          end;
berghofe@18107
  1193
berghofe@18107
  1194
        val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs');
berghofe@18107
  1195
        val prems' = map (fn p as (_, T) => HOLogic.mk_Trueprop
wenzelm@32952
  1196
            (f T (Free p) (Free z))) (maps fst frees') @
wenzelm@32952
  1197
          mk_fresh1 [] (maps fst frees') @
berghofe@19710
  1198
          mk_fresh2 [] frees'
berghofe@18107
  1199
wenzelm@46215
  1200
      in
wenzelm@46215
  1201
        fold_rev (Logic.all o Free) (frees @ [z])
wenzelm@46215
  1202
          (Logic.list_implies (prems' @ prems,
wenzelm@46215
  1203
            HOLogic.mk_Trueprop (make_pred fsT k T $ Free z $
wenzelm@46215
  1204
              list_comb (Const (cname, Ts ---> T), map Free frees))))
berghofe@18107
  1205
      end;
berghofe@18107
  1206
wenzelm@32952
  1207
    val ind_prems = maps (fn (((i, (_, _, constrs)), (_, idxss)), T) =>
berghofe@18658
  1208
      map (make_ind_prem fsT (fn T => fn t => fn u =>
berghofe@25823
  1209
        fresh_const T fsT $ t $ u) i T)
wenzelm@32952
  1210
          (constrs ~~ idxss)) (descr'' ~~ ndescr ~~ recTs);
blanchet@58112
  1211
    val tnames = Old_Datatype_Prop.make_tnames recTs;
wenzelm@20071
  1212
    val zs = Name.variant_list tnames (replicate (length descr'') "z");
haftmann@38795
  1213
    val ind_concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop @{const_name HOL.conj})
berghofe@18658
  1214
      (map (fn ((((i, _), T), tname), z) =>
berghofe@18658
  1215
        make_pred fsT i T $ Free (z, fsT) $ Free (tname, T))
berghofe@18658
  1216
        (descr'' ~~ recTs ~~ tnames ~~ zs)));
berghofe@18107
  1217
    val induct = Logic.list_implies (ind_prems, ind_concl);
berghofe@18107
  1218
berghofe@18658
  1219
    val ind_prems' =
wenzelm@46215
  1220
      map (fn (_, f as Free (_, T)) => Logic.all (Free ("x", fsT'))
wenzelm@56253
  1221
        (HOLogic.mk_Trueprop (Const (@{const_name finite},
huffman@44692
  1222
          Term.range_type T -->
berghofe@26806
  1223
            HOLogic.boolT) $ (f $ Free ("x", fsT'))))) fresh_fs @
wenzelm@32952
  1224
      maps (fn (((i, (_, _, constrs)), (_, idxss)), T) =>
berghofe@18658
  1225
        map (make_ind_prem fsT' (fn T => fn t => fn u => HOLogic.Not $
berghofe@18658
  1226
          HOLogic.mk_mem (t, the (AList.lookup op = fresh_fs T) $ u)) i T)
wenzelm@32952
  1227
            (constrs ~~ idxss)) (descr'' ~~ ndescr ~~ recTs);
haftmann@38795
  1228
    val ind_concl' = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop @{const_name HOL.conj})
berghofe@18658
  1229
      (map (fn ((((i, _), T), tname), z) =>
berghofe@18658
  1230
        make_pred fsT' i T $ Free (z, fsT') $ Free (tname, T))
berghofe@18658
  1231
        (descr'' ~~ recTs ~~ tnames ~~ zs)));
berghofe@18658
  1232
    val induct' = Logic.list_implies (ind_prems', ind_concl');
berghofe@18658
  1233
berghofe@18658
  1234
    val aux_ind_vars =
blanchet@58112
  1235
      (Old_Datatype_Prop.indexify_names (replicate (length dt_atomTs) "pi") ~~
berghofe@18658
  1236
       map mk_permT dt_atomTs) @ [("z", fsT')];
berghofe@18658
  1237
    val aux_ind_Ts = rev (map snd aux_ind_vars);
haftmann@38795
  1238
    val aux_ind_concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop @{const_name HOL.conj})
berghofe@18658
  1239
      (map (fn (((i, _), T), tname) =>
berghofe@18658
  1240
        HOLogic.list_all (aux_ind_vars, make_pred fsT' i T $ Bound 0 $
berghofe@22311
  1241
          fold_rev (mk_perm aux_ind_Ts) (map Bound (length dt_atomTs downto 1))
berghofe@22311
  1242
            (Free (tname, T))))
berghofe@18658
  1243
        (descr'' ~~ recTs ~~ tnames)));
berghofe@18658
  1244
berghofe@28731
  1245
    val fin_set_supp = map (fn s =>
berghofe@28731
  1246
      at_inst_of thy9 s RS at_fin_set_supp) dt_atoms;
berghofe@28731
  1247
    val fin_set_fresh = map (fn s =>
berghofe@28731
  1248
      at_inst_of thy9 s RS at_fin_set_fresh) dt_atoms;
berghofe@25951
  1249
    val pt1_atoms = map (fn Type (s, _) =>
wenzelm@39557
  1250
      Global_Theory.get_thm thy9 ("pt_" ^ Long_Name.base_name s ^ "1")) dt_atomTs;
berghofe@25951
  1251
    val pt2_atoms = map (fn Type (s, _) =>
wenzelm@39557
  1252
      Global_Theory.get_thm thy9 ("pt_" ^ Long_Name.base_name s ^ "2") RS sym) dt_atomTs;
wenzelm@39557
  1253
    val exists_fresh' = Global_Theory.get_thms thy9 "exists_fresh'";
wenzelm@39557
  1254
    val fs_atoms = Global_Theory.get_thms thy9 "fin_supp";
wenzelm@39557
  1255
    val abs_supp = Global_Theory.get_thms thy9 "abs_supp";
wenzelm@39557
  1256
    val perm_fresh_fresh = Global_Theory.get_thms thy9 "perm_fresh_fresh";
wenzelm@39557
  1257
    val calc_atm = Global_Theory.get_thms thy9 "calc_atm";
wenzelm@39557
  1258
    val fresh_atm = Global_Theory.get_thms thy9 "fresh_atm";
wenzelm@39557
  1259
    val fresh_left = Global_Theory.get_thms thy9 "fresh_left";
wenzelm@39557
  1260
    val perm_swap = Global_Theory.get_thms thy9 "perm_swap";
berghofe@18658
  1261
berghofe@25951
  1262
    fun obtain_fresh_name' ths ts T (freshs1, freshs2, ctxt) =
berghofe@18658
  1263
      let
berghofe@25951
  1264
        val p = foldr1 HOLogic.mk_prod (ts @ freshs1);
berghofe@25951
  1265
        val ex = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop
berghofe@25951
  1266
            (HOLogic.exists_const T $ Abs ("x", T,
berghofe@25951
  1267
              fresh_const T (fastype_of p) $
berghofe@25951
  1268
                Bound 0 $ p)))
berghofe@25951
  1269
          (fn _ => EVERY
wenzelm@59498
  1270
            [resolve_tac ctxt exists_fresh' 1,
wenzelm@51717
  1271
             simp_tac (put_simpset HOL_ss ctxt addsimps (supp_prod :: finite_Un :: fs_atoms @
berghofe@25951
  1272
               fin_set_supp @ ths)) 1]);
wenzelm@32202
  1273
        val (([(_, cx)], ths), ctxt') = Obtain.result
wenzelm@51717
  1274
          (fn ctxt' => EVERY
wenzelm@60754
  1275
            [eresolve_tac ctxt' [exE] 1,
wenzelm@51717
  1276
             full_simp_tac (put_simpset HOL_ss ctxt' addsimps (fresh_prod :: fresh_atm)) 1,
wenzelm@60754
  1277
             REPEAT (eresolve_tac ctxt' [conjE] 1)])
berghofe@25951
  1278
          [ex] ctxt
wenzelm@59582
  1279
      in (freshs1 @ [Thm.term_of cx], freshs2 @ ths, ctxt') end;
berghofe@25951
  1280
berghofe@25951
  1281
    fun fresh_fresh_inst thy a b =
berghofe@25951
  1282
      let
berghofe@25951
  1283
        val T = fastype_of a;
wenzelm@59582
  1284
        val SOME th = find_first (fn th =>
wenzelm@59582
  1285
          (case Thm.prop_of th of
berghofe@25951
  1286
            _ $ (_ $ (Const (_, Type (_, [U, _])) $ _ $ _)) $ _ => U = T
wenzelm@59582
  1287
          | _ => false)) perm_fresh_fresh
berghofe@18658
  1288
      in
wenzelm@60801
  1289
        Thm.instantiate' []
wenzelm@59621
  1290
          [SOME (Thm.global_cterm_of thy a), NONE, SOME (Thm.global_cterm_of thy b)] th
berghofe@18658
  1291
      end;
urbanc@18104
  1292
berghofe@28731
  1293
    val fs_cp_sort =
berghofe@28731
  1294
      map (fs_class_of thy9) dt_atoms @
haftmann@33040
  1295
      maps (fn s => map (cp_class_of thy9 s) (remove (op =) s dt_atoms)) dt_atoms;
berghofe@28731
  1296
berghofe@19710
  1297
    (**********************************************************************
berghofe@19710
  1298
      The subgoals occurring in the proof of induct_aux have the
berghofe@19710
  1299
      following parameters:
berghofe@19710
  1300
berghofe@25951
  1301
        x_1 ... x_k p_1 ... p_m z
berghofe@19710
  1302
berghofe@19710
  1303
      where
berghofe@19710
  1304
berghofe@19710
  1305
        x_i : constructor arguments (introduced by weak induction rule)
berghofe@19710
  1306
        p_i : permutations (one for each atom type in the data type)
berghofe@19710
  1307
        z   : freshness context
berghofe@19710
  1308
    ***********************************************************************)
berghofe@19710
  1309
berghofe@19710
  1310
    val _ = warning "proving strong induction theorem ...";
berghofe@19710
  1311
wenzelm@51122
  1312
    val induct_aux = Goal.prove_global_future thy9 []
berghofe@28731
  1313
        (map (augment_sort thy9 fs_cp_sort) ind_prems')
berghofe@28731
  1314
        (augment_sort thy9 fs_cp_sort ind_concl') (fn {prems, context} =>
berghofe@25951
  1315
      let
berghofe@25951
  1316
        val (prems1, prems2) = chop (length dt_atomTs) prems;
wenzelm@51717
  1317
        val ind_ss2 = put_simpset HOL_ss context addsimps
berghofe@25951
  1318
          finite_Diff :: abs_fresh @ abs_supp @ fs_atoms;
berghofe@25951
  1319
        val ind_ss1 = ind_ss2 addsimps fresh_left @ calc_atm @
berghofe@25951
  1320
          fresh_atm @ rev_simps @ app_simps;
wenzelm@51717
  1321
        val ind_ss3 = put_simpset HOL_ss context addsimps abs_fun_eq1 ::
berghofe@25951
  1322
          abs_perm @ calc_atm @ perm_swap;
wenzelm@51717
  1323
        val ind_ss4 = put_simpset HOL_basic_ss context addsimps fresh_left @ prems1 @
berghofe@25951
  1324
          fin_set_fresh @ calc_atm;
wenzelm@51717
  1325
        val ind_ss5 = put_simpset HOL_basic_ss context addsimps pt1_atoms;
wenzelm@51717
  1326
        val ind_ss6 = put_simpset HOL_basic_ss context addsimps flat perm_simps';
berghofe@28731
  1327
        val th = Goal.prove context [] []
berghofe@28731
  1328
          (augment_sort thy9 fs_cp_sort aux_ind_concl)
berghofe@25951
  1329
          (fn {context = context1, ...} =>
wenzelm@60328
  1330
             EVERY (Old_Datatype_Aux.ind_tac context1 dt_induct tnames 1 ::
berghofe@25951
  1331
               maps (fn ((_, (_, _, constrs)), (_, constrs')) =>
berghofe@25951
  1332
                 map (fn ((cname, cargs), is) =>
wenzelm@60754
  1333
                   REPEAT (resolve_tac context1 [allI] 1) THEN
berghofe@25951
  1334
                   SUBPROOF (fn {prems = iprems, params, concl,
berghofe@25951
  1335
                       context = context2, ...} =>
berghofe@18658
  1336
                     let
wenzelm@59582
  1337
                       val concl' = Thm.term_of concl;
berghofe@25951
  1338
                       val _ $ (_ $ _ $ u) = concl';
berghofe@25951
  1339
                       val U = fastype_of u;
berghofe@25951
  1340
                       val (xs, params') =
wenzelm@59582
  1341
                         chop (length cargs) (map (Thm.term_of o #2) params);
berghofe@25951
  1342
                       val Ts = map fastype_of xs;
berghofe@25951
  1343
                       val cnstr = Const (cname, Ts ---> U);
berghofe@25951
  1344
                       val (pis, z) = split_last params';
berghofe@25951
  1345
                       val mk_pi = fold_rev (mk_perm []) pis;
berghofe@25951
  1346
                       val xs' = partition_cargs is xs;
berghofe@25951
  1347
                       val xs'' = map (fn (ts, u) => (map mk_pi ts, mk_pi u)) xs';
berghofe@25951
  1348
                       val ts = maps (fn (ts, u) => ts @ [u]) xs'';
berghofe@25951
  1349
                       val (freshs1, freshs2, context3) = fold (fn t =>
berghofe@25951
  1350
                         let val T = fastype_of t
berghofe@25951
  1351
                         in obtain_fresh_name' prems1
berghofe@25951
  1352
                           (the (AList.lookup op = fresh_fs T) $ z :: ts) T
berghofe@25951
  1353
                         end) (maps fst xs') ([], [], context2);
berghofe@25951
  1354
                       val freshs1' = unflat (map fst xs') freshs1;
berghofe@25951
  1355
                       val freshs2' = map (Simplifier.simplify ind_ss4)
berghofe@25951
  1356
                         (mk_not_sym freshs2);
berghofe@25951
  1357
                       val ind_ss1' = ind_ss1 addsimps freshs2';
berghofe@25951
  1358
                       val ind_ss3' = ind_ss3 addsimps freshs2';
berghofe@25951
  1359
                       val rename_eq =
berghofe@25951
  1360
                         if forall (null o fst) xs' then []
berghofe@25951
  1361
                         else [Goal.prove context3 [] []
berghofe@25951
  1362
                           (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@25951
  1363
                             (list_comb (cnstr, ts),
berghofe@25951
  1364
                              list_comb (cnstr, maps (fn ((bs, t), cs) =>
berghofe@25951
  1365
                                cs @ [fold_rev (mk_perm []) (map perm_of_pair
berghofe@25951
  1366
                                  (bs ~~ cs)) t]) (xs'' ~~ freshs1')))))
wenzelm@60754
  1367
                           (fn {context = ctxt, ...} => EVERY
wenzelm@51717
  1368
                              (simp_tac (put_simpset HOL_ss context3 addsimps flat inject_thms) 1 ::
wenzelm@60754
  1369
                               REPEAT (FIRSTGOAL (resolve_tac ctxt [conjI])) ::
berghofe@25951
  1370
                               maps (fn ((bs, t), cs) =>
berghofe@25951
  1371
                                 if null bs then []
wenzelm@60754
  1372
                                 else resolve_tac ctxt [sym] 1 :: maps (fn (b, c) =>
wenzelm@60754
  1373
                                   [resolve_tac ctxt [trans] 1,
wenzelm@60754
  1374
                                    resolve_tac ctxt [sym] 1,
wenzelm@60754
  1375
                                    resolve_tac ctxt [fresh_fresh_inst thy9 b c] 1,
berghofe@25951
  1376
                                    simp_tac ind_ss1' 1,
berghofe@25951
  1377
                                    simp_tac ind_ss2 1,
berghofe@25951
  1378
                                    simp_tac ind_ss3' 1]) (bs ~~ cs))
berghofe@25951
  1379
                                 (xs'' ~~ freshs1')))];
berghofe@25951
  1380
                       val th = Goal.prove context3 [] [] concl' (fn _ => EVERY
berghofe@25951
  1381
                         [simp_tac (ind_ss6 addsimps rename_eq) 1,
berghofe@25951
  1382
                          cut_facts_tac iprems 1,
wenzelm@59498
  1383
                          (resolve_tac context2 prems THEN_ALL_NEW
berghofe@25951
  1384
                            SUBGOAL (fn (t, i) => case Logic.strip_assums_concl t of
wenzelm@56253
  1385
                                _ $ (Const (@{const_name Nominal.fresh}, _) $ _ $ _) =>
berghofe@25951
  1386
                                  simp_tac ind_ss1' i
haftmann@38558
  1387
                              | _ $ (Const (@{const_name Not}, _) $ _) =>
wenzelm@59498
  1388
                                  resolve_tac context2 freshs2' i
wenzelm@51717
  1389
                              | _ => asm_simp_tac (put_simpset HOL_basic_ss context3 addsimps
berghofe@25951
  1390
                                  pt2_atoms addsimprocs [perm_simproc]) i)) 1])
wenzelm@42361
  1391
                       val final = Proof_Context.export context3 context2 [th]
berghofe@18658
  1392
                     in
wenzelm@59498
  1393
                       resolve_tac context2 final 1
berghofe@25951
  1394
                     end) context1 1) (constrs ~~ constrs')) (descr'' ~~ ndescr)))
berghofe@25951
  1395
      in
berghofe@25951
  1396
        EVERY
berghofe@25951
  1397
          [cut_facts_tac [th] 1,
wenzelm@59498
  1398
           REPEAT (eresolve_tac context [conjE, @{thm allE_Nil}] 1),
wenzelm@60754
  1399
           REPEAT (eresolve_tac context [allE] 1),
wenzelm@60754
  1400
           REPEAT (TRY (resolve_tac context [conjI] 1) THEN asm_full_simp_tac ind_ss5 1)]
berghofe@25951
  1401
      end);
berghofe@18658
  1402
berghofe@18658
  1403
    val induct_aux' = Thm.instantiate ([],
wenzelm@60642
  1404
      map (fn (s, Var (v as (_, T))) =>
wenzelm@60642
  1405
        (v, Thm.global_cterm_of thy9 (Free (s, T))))
berghofe@28731
  1406
          (pnames ~~ map head_of (HOLogic.dest_conj
wenzelm@59582
  1407
             (HOLogic.dest_Trueprop (Thm.concl_of induct_aux)))) @
berghofe@18658
  1408
      map (fn (_, f) =>
wenzelm@35845
  1409
        let val f' = Logic.varify_global f
wenzelm@60642
  1410
        in (dest_Var f',
wenzelm@59621
  1411
          Thm.global_cterm_of thy9 (Const (@{const_name Nominal.supp}, fastype_of f')))
berghofe@18658
  1412
        end) fresh_fs) induct_aux;
berghofe@18658
  1413
wenzelm@51122
  1414
    val induct = Goal.prove_global_future thy9 []
berghofe@28731
  1415
      (map (augment_sort thy9 fs_cp_sort) ind_prems)
berghofe@28731
  1416
      (augment_sort thy9 fs_cp_sort ind_concl)
wenzelm@51717
  1417
      (fn {prems, context = ctxt} => EVERY
wenzelm@60754
  1418
         [resolve_tac ctxt [induct_aux'] 1,
wenzelm@59498
  1419
          REPEAT (resolve_tac ctxt fs_atoms 1),
wenzelm@59498
  1420
          REPEAT ((resolve_tac ctxt prems THEN_ALL_NEW
wenzelm@60754
  1421
            (eresolve_tac ctxt @{thms meta_spec} ORELSE'
wenzelm@51717
  1422
              full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps [fresh_def]))) 1)])
berghofe@18658
  1423
berghofe@18658
  1424
    val (_, thy10) = thy9 |>
wenzelm@24712
  1425
      Sign.add_path big_name |>
wenzelm@39557
  1426
      Global_Theory.add_thms [((Binding.name "strong_induct'", induct_aux), [])] ||>>
wenzelm@39557
  1427
      Global_Theory.add_thms [((Binding.name "strong_induct", induct), [case_names_induct])] ||>>
wenzelm@60359
  1428
      Global_Theory.add_thmss
wenzelm@60359
  1429
        [((Binding.name "strong_inducts", projections (Proof_Context.init_global thy9) induct),
wenzelm@60359
  1430
            [case_names_induct])];
berghofe@18658
  1431
berghofe@19322
  1432
    (**** recursion combinator ****)
berghofe@19251
  1433
berghofe@19322
  1434
    val _ = warning "defining recursion combinator ...";
berghofe@19251
  1435
wenzelm@45741
  1436
    val used = fold Term.add_tfree_namesT recTs [];
berghofe@19251
  1437
blanchet@58112
  1438
    val (rec_result_Ts', rec_fn_Ts') = Old_Datatype_Prop.make_primrec_Ts descr' used;
berghofe@19985
  1439
wenzelm@56253
  1440
    val rec_sort = if null dt_atomTs then @{sort type} else
wenzelm@36428
  1441
      Sign.minimize_sort thy10 (Sign.certify_sort thy10 pt_cp_sort);
berghofe@19985
  1442
berghofe@19985
  1443
    val rec_result_Ts = map (fn TFree (s, _) => TFree (s, rec_sort)) rec_result_Ts';
berghofe@19985
  1444
    val rec_fn_Ts = map (typ_subst_atomic (rec_result_Ts' ~~ rec_result_Ts)) rec_fn_Ts';
berghofe@19251
  1445
berghofe@21021
  1446
    val rec_set_Ts = map (fn (T1, T2) =>
berghofe@21021
  1447
      rec_fn_Ts @ [T1, T2] ---> HOLogic.boolT) (recTs ~~ rec_result_Ts);
berghofe@19251
  1448
berghofe@19322
  1449
    val big_rec_name = big_name ^ "_rec_set";
berghofe@21021
  1450
    val rec_set_names' =
berghofe@21021
  1451
      if length descr'' = 1 then [big_rec_name] else
berghofe@21021
  1452
        map ((curry (op ^) (big_rec_name ^ "_")) o string_of_int)
berghofe@21021
  1453
          (1 upto (length descr''));
haftmann@28965
  1454
    val rec_set_names =  map (Sign.full_bname thy10) rec_set_names';
berghofe@19251
  1455
blanchet@58112
  1456
    val rec_fns = map (uncurry (Old_Datatype_Aux.mk_Free "f"))
berghofe@19322
  1457
      (rec_fn_Ts ~~ (1 upto (length rec_fn_Ts)));
berghofe@21021
  1458
    val rec_sets' = map (fn c => list_comb (Free c, rec_fns))
berghofe@21021
  1459
      (rec_set_names' ~~ rec_set_Ts);
berghofe@19322
  1460
    val rec_sets = map (fn c => list_comb (Const c, rec_fns))
berghofe@19322
  1461
      (rec_set_names ~~ rec_set_Ts);
berghofe@19251
  1462
berghofe@19322
  1463
    (* introduction rules for graph of recursion function *)
berghofe@19251
  1464
berghofe@20145
  1465
    val rec_preds = map (fn (a, T) =>
berghofe@20145
  1466
      Free (a, T --> HOLogic.boolT)) (pnames ~~ rec_result_Ts);
berghofe@20145
  1467
berghofe@20267
  1468
    fun mk_fresh3 rs [] = []
wenzelm@32952
  1469
      | mk_fresh3 rs ((p as (ys, z)) :: yss) = maps (fn y as (_, T) =>
wenzelm@32952
  1470
            map_filter (fn ((_, (_, x)), r as (_, U)) => if z = x then NONE
berghofe@20267
  1471
              else SOME (HOLogic.mk_Trueprop
wenzelm@32952
  1472
                (fresh_const T U $ Free y $ Free r))) rs) ys @
berghofe@20267
  1473
          mk_fresh3 rs yss;
berghofe@20267
  1474
berghofe@21088
  1475
    (* FIXME: avoid collisions with other variable names? *)
berghofe@21088
  1476
    val rec_ctxt = Free ("z", fsT');
berghofe@21088
  1477
wenzelm@33244
  1478
    fun make_rec_intr T p rec_set ((cname, cargs), idxs)
wenzelm@33244
  1479
        (rec_intr_ts, rec_prems, rec_prems', rec_eq_prems, l) =
berghofe@19251
  1480
      let
blanchet@58112
  1481
        val Ts = map (Old_Datatype_Aux.typ_of_dtyp descr'') cargs;
berghofe@19851
  1482
        val frees = map (fn i => "x" ^ string_of_int i) (1 upto length Ts) ~~ Ts;
berghofe@19851
  1483
        val frees' = partition_cargs idxs frees;
wenzelm@32952
  1484
        val binders = maps fst frees';
berghofe@20411
  1485
        val atomTs = distinct op = (maps (map snd o fst) frees');
wenzelm@32952
  1486
        val recs = map_filter
blanchet@58112
  1487
          (fn ((_, Old_Datatype_Aux.DtRec i), p) => SOME (i, p) | _ => NONE)
berghofe@19851
  1488
          (partition_cargs idxs cargs ~~ frees');
berghofe@19851
  1489
        val frees'' = map (fn i => "y" ^ string_of_int i) (1 upto length recs) ~~
wenzelm@42364
  1490
          map (fn (i, _) => nth rec_result_Ts i) recs;
berghofe@20145
  1491
        val prems1 = map (fn ((i, (_, x)), y) => HOLogic.mk_Trueprop
wenzelm@42364
  1492
          (nth rec_sets' i $ Free x $ Free y)) (recs ~~ frees'');
berghofe@20145
  1493
        val prems2 =
berghofe@20145
  1494
          map (fn f => map (fn p as (_, T) => HOLogic.mk_Trueprop
berghofe@25823
  1495
            (fresh_const T (fastype_of f) $ Free p $ f)) binders) rec_fns;
berghofe@21088
  1496
        val prems3 = mk_fresh1 [] binders @ mk_fresh2 [] frees';
berghofe@20145
  1497
        val prems4 = map (fn ((i, _), y) =>
wenzelm@42364
  1498
          HOLogic.mk_Trueprop (nth rec_preds i $ Free y)) (recs ~~ frees'');
berghofe@20267
  1499
        val prems5 = mk_fresh3 (recs ~~ frees'') frees';
berghofe@20411
  1500
        val prems6 = maps (fn aT => map (fn y as (_, T) => HOLogic.mk_Trueprop
wenzelm@56253
  1501
          (Const (@{const_name finite}, HOLogic.mk_setT aT --> HOLogic.boolT) $
wenzelm@56253
  1502
             (Const (@{const_name Nominal.supp}, T --> HOLogic.mk_setT aT) $ Free y)))
berghofe@20411
  1503
               frees'') atomTs;
berghofe@21088
  1504
        val prems7 = map (fn x as (_, T) => HOLogic.mk_Trueprop
berghofe@25823
  1505
          (fresh_const T fsT' $ Free x $ rec_ctxt)) binders;
wenzelm@42364
  1506
        val result = list_comb (nth rec_fns l, map Free (frees @ frees''));
berghofe@20376
  1507
        val result_freshs = map (fn p as (_, T) =>
berghofe@25823
  1508
          fresh_const T (fastype_of result) $ Free p $ result) binders;
berghofe@20145
  1509
        val P = HOLogic.mk_Trueprop (p $ result)
berghofe@20145
  1510
      in
wenzelm@32952
  1511
        (rec_intr_ts @ [Logic.list_implies (flat prems2 @ prems3 @ prems1,
berghofe@21021
  1512
           HOLogic.mk_Trueprop (rec_set $
berghofe@21021
  1513
             list_comb (Const (cname, Ts ---> T), map Free frees) $ result))],
wenzelm@46215
  1514
         rec_prems @ [fold_rev (Logic.all o Free) (frees @ frees'') (Logic.list_implies (prems4, P))],
wenzelm@46215
  1515
         rec_prems' @ map (fn fr => fold_rev (Logic.all o Free) (frees @ frees'')
wenzelm@46215
  1516
           (Logic.list_implies (nth prems2 l @ prems3 @ prems5 @ prems7 @ prems6 @ [P],
berghofe@21054
  1517
             HOLogic.mk_Trueprop fr))) result_freshs,
wenzelm@32952
  1518
         rec_eq_prems @ [flat prems2 @ prems3],
berghofe@20145
  1519
         l + 1)
berghofe@19251
  1520
      end;
berghofe@19251
  1521
berghofe@20397
  1522
    val (rec_intr_ts, rec_prems, rec_prems', rec_eq_prems, _) =
wenzelm@33244
  1523
      fold (fn ((((d, d'), T), p), rec_set) =>
wenzelm@33244
  1524
        fold (make_rec_intr T p rec_set) (#3 (snd d) ~~ snd d'))
wenzelm@33244
  1525
          (descr'' ~~ ndescr ~~ recTs ~~ rec_preds ~~ rec_sets')
wenzelm@33244
  1526
          ([], [], [], [], 0);
berghofe@19251
  1527
wenzelm@21365
  1528
    val ({intrs = rec_intrs, elims = rec_elims, raw_induct = rec_induct, ...}, thy11) =
wenzelm@33278
  1529
      thy10
wenzelm@59880
  1530
      |> Sign.concealed
wenzelm@33726
  1531
      |> Inductive.add_inductive_global
wenzelm@33669
  1532
          {quiet_mode = #quiet config, verbose = false, alt_name = Binding.name big_rec_name,
wenzelm@49170
  1533
           coind = false, no_elim = false, no_ind = false, skip_mono = true}
haftmann@28965
  1534
          (map (fn (s, T) => ((Binding.name s, T), NoSyn)) (rec_set_names' ~~ rec_set_Ts))
wenzelm@26128
  1535
          (map dest_Free rec_fns)
wenzelm@63352
  1536
          (map (fn x => (Binding.empty_atts, x)) rec_intr_ts) []
wenzelm@39557
  1537
      ||> Global_Theory.hide_fact true (Long_Name.append (Sign.full_bname thy10 big_rec_name) "induct")
wenzelm@52788
  1538
      ||> Sign.restore_naming thy10;
berghofe@19251
  1539
berghofe@19985
  1540
    (** equivariance **)
berghofe@19985
  1541
wenzelm@39557
  1542
    val fresh_bij = Global_Theory.get_thms thy11 "fresh_bij";
wenzelm@39557
  1543
    val perm_bij = Global_Theory.get_thms thy11 "perm_bij";
berghofe@19985
  1544
berghofe@19985
  1545
    val (rec_equiv_thms, rec_equiv_thms') = ListPair.unzip (map (fn aT =>
berghofe@19985
  1546
      let
berghofe@19985
  1547
        val permT = mk_permT aT;
berghofe@19985
  1548
        val pi = Free ("pi", permT);
blanchet@58112
  1549
        val rec_fns_pi = map (mk_perm [] pi o uncurry (Old_Datatype_Aux.mk_Free "f"))
berghofe@19985
  1550
          (rec_fn_Ts ~~ (1 upto (length rec_fn_Ts)));
berghofe@19985
  1551
        val rec_sets_pi = map (fn c => list_comb (Const c, rec_fns_pi))
berghofe@19985
  1552
          (rec_set_names ~~ rec_set_Ts);
berghofe@19985
  1553
        val ps = map (fn ((((T, U), R), R'), i) =>
berghofe@19985
  1554
          let
berghofe@19985
  1555
            val x = Free ("x" ^ string_of_int i, T);
berghofe@19985
  1556
            val y = Free ("y" ^ string_of_int i, U)
berghofe@19985
  1557
          in
berghofe@22311
  1558
            (R $ x $ y, R' $ mk_perm [] pi x $ mk_perm [] pi y)
berghofe@19985
  1559
          end) (recTs ~~ rec_result_Ts ~~ rec_sets ~~ rec_sets_pi ~~ (1 upto length recTs));
blanchet@58112
  1560
        val ths = map (fn th => Drule.export_without_context (th RS mp))
blanchet@58112
  1561
          (Old_Datatype_Aux.split_conj_thm
blanchet@58112
  1562
            (Goal.prove_global_future thy11 [] []
blanchet@58112
  1563
              (augment_sort thy1 pt_cp_sort
blanchet@58112
  1564
                (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj (map HOLogic.mk_imp ps))))
wenzelm@60754
  1565
              (fn {context = ctxt, ...} =>
wenzelm@60754
  1566
                 resolve_tac ctxt [rec_induct] 1 THEN REPEAT
blanchet@58112
  1567
                 (simp_tac (put_simpset HOL_basic_ss ctxt
blanchet@58112
  1568
                    addsimps flat perm_simps'
blanchet@58112
  1569
                    addsimprocs [NominalPermeq.perm_simproc_app]) 1 THEN
wenzelm@59498
  1570
                  (resolve_tac ctxt rec_intrs THEN_ALL_NEW
blanchet@58112
  1571
                   asm_simp_tac (put_simpset HOL_ss ctxt addsimps (fresh_bij @ perm_bij))) 1))))
wenzelm@20046
  1572
        val ths' = map (fn ((P, Q), th) =>
wenzelm@51122
  1573
          Goal.prove_global_future thy11 [] []
berghofe@28731
  1574
            (augment_sort thy1 pt_cp_sort
berghofe@28731
  1575
              (Logic.mk_implies (HOLogic.mk_Trueprop Q, HOLogic.mk_Trueprop P)))
wenzelm@60754
  1576
            (fn {context = ctxt, ...} =>
wenzelm@60754
  1577
              dresolve_tac ctxt [Thm.instantiate ([],
wenzelm@60642
  1578
                 [((("pi", 0), permT),
wenzelm@60754
  1579
                   Thm.global_cterm_of thy11 (Const (@{const_name rev}, permT --> permT) $ pi))]) th] 1 THEN
wenzelm@51717
  1580
               NominalPermeq.perm_simp_tac (put_simpset HOL_ss ctxt) 1)) (ps ~~ ths)
berghofe@19985
  1581
      in (ths, ths') end) dt_atomTs);
berghofe@19985
  1582
berghofe@19985
  1583
    (** finite support **)
berghofe@19985
  1584
berghofe@19985
  1585
    val rec_fin_supp_thms = map (fn aT =>
berghofe@19985
  1586
      let
wenzelm@30364
  1587
        val name = Long_Name.base_name (fst (dest_Type aT));
wenzelm@39557
  1588
        val fs_name = Global_Theory.get_thm thy11 ("fs_" ^ name ^ "1");
berghofe@19985
  1589
        val aset = HOLogic.mk_setT aT;
wenzelm@56253
  1590
        val finite = Const (@{const_name finite}, aset --> HOLogic.boolT);
berghofe@22274
  1591
        val fins = map (fn (f, T) => HOLogic.mk_Trueprop
wenzelm@56253
  1592
          (finite $ (Const (@{const_name Nominal.supp}, T --> aset) $ f)))
berghofe@19985
  1593
            (rec_fns ~~ rec_fn_Ts)
berghofe@19985
  1594
      in
blanchet@58112
  1595
        map (fn th => Drule.export_without_context (th RS mp)) (Old_Datatype_Aux.split_conj_thm
wenzelm@51122
  1596
          (Goal.prove_global_future thy11 []
berghofe@28731
  1597
            (map (augment_sort thy11 fs_cp_sort) fins)
berghofe@28731
  1598
            (augment_sort thy11 fs_cp_sort
berghofe@28731
  1599
              (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@28731
  1600
                (map (fn (((T, U), R), i) =>
berghofe@28731
  1601
                   let
berghofe@28731
  1602
                     val x = Free ("x" ^ string_of_int i, T);
berghofe@28731
  1603
                     val y = Free ("y" ^ string_of_int i, U)
berghofe@28731
  1604
                   in
berghofe@28731
  1605
                     HOLogic.mk_imp (R $ x $ y,
wenzelm@56253
  1606
                       finite $ (Const (@{const_name Nominal.supp}, U --> aset) $ y))
berghofe@28731
  1607
                   end) (recTs ~~ rec_result_Ts ~~ rec_sets ~~
berghofe@28731
  1608
                     (1 upto length recTs))))))
wenzelm@51717
  1609
            (fn {prems = fins, context = ctxt} =>
wenzelm@60754
  1610
              (resolve_tac ctxt [rec_induct] THEN_ALL_NEW cut_facts_tac fins) 1 THEN REPEAT
wenzelm@51717
  1611
               (NominalPermeq.finite_guess_tac (put_simpset HOL_ss ctxt addsimps [fs_name]) 1))))
berghofe@19985
  1612
      end) dt_atomTs;
berghofe@19985
  1613
berghofe@20376
  1614
    (** freshness **)
berghofe@20376
  1615
berghofe@20376
  1616
    val finite_premss = map (fn aT =>
berghofe@22274
  1617
      map (fn (f, T) => HOLogic.mk_Trueprop
wenzelm@56253
  1618
        (Const (@{const_name finite}, HOLogic.mk_setT aT --> HOLogic.boolT) $
wenzelm@56253
  1619
           (Const (@{const_name Nominal.supp}, T --> HOLogic.mk_setT aT) $ f)))
berghofe@20376
  1620
           (rec_fns ~~ rec_fn_Ts)) dt_atomTs;
berghofe@20376
  1621
berghofe@28731
  1622
    val rec_fns' = map (augment_sort thy11 fs_cp_sort) rec_fns;
berghofe@28731
  1623
berghofe@20376
  1624
    val rec_fresh_thms = map (fn ((aT, eqvt_ths), finite_prems) =>
berghofe@20376
  1625
      let
wenzelm@30364
  1626
        val name = Long_Name.base_name (fst (dest_Type aT));
wenzelm@39557
  1627
        val fs_name = Global_Theory.get_thm thy11 ("fs_" ^ name ^ "1");
berghofe@20376
  1628
        val a = Free ("a", aT);
berghofe@20376
  1629
        val freshs = map (fn (f, fT) => HOLogic.mk_Trueprop
berghofe@25823
  1630
          (fresh_const aT fT $ a $ f)) (rec_fns ~~ rec_fn_Ts)
berghofe@20376
  1631
      in
berghofe@20376
  1632
        map (fn (((T, U), R), eqvt_th) =>
berghofe@20376
  1633
          let
berghofe@28731
  1634
            val x = Free ("x", augment_sort_typ thy11 fs_cp_sort T);
berghofe@20376
  1635
            val y = Free ("y", U);
berghofe@20376
  1636
            val y' = Free ("y'", U)
berghofe@20376
  1637
          in
wenzelm@42361
  1638
            Drule.export_without_context (Goal.prove (Proof_Context.init_global thy11) []
berghofe@28731
  1639
              (map (augment_sort thy11 fs_cp_sort)
berghofe@28731
  1640
                (finite_prems @
berghofe@28731
  1641
                   [HOLogic.mk_Trueprop (R $ x $ y),
berghofe@28731
  1642
                    HOLogic.mk_Trueprop (HOLogic.mk_all ("y'", U,
berghofe@28731
  1643
                      HOLogic.mk_imp (R $ x $ y', HOLogic.mk_eq (y', y)))),
berghofe@28731
  1644
                    HOLogic.mk_Trueprop (fresh_const aT T $ a $ x)] @
berghofe@28731
  1645
                 freshs))
berghofe@25823
  1646
              (HOLogic.mk_Trueprop (fresh_const aT U $ a $ y))
wenzelm@60787
  1647
              (fn {prems, context = ctxt} =>
berghofe@20376
  1648
                 let
berghofe@20376
  1649
                   val (finite_prems, rec_prem :: unique_prem ::
berghofe@20376
  1650
                     fresh_prems) = chop (length finite_prems) prems;
berghofe@20376
  1651
                   val unique_prem' = unique_prem RS spec RS mp;
berghofe@20376
  1652
                   val unique = [unique_prem', unique_prem' RS sym] MRS trans;
wenzelm@59582
  1653
                   val _ $ (_ $ (_ $ S $ _)) $ _ = Thm.prop_of supports_fresh;
berghofe@28731
  1654
                   val tuple = foldr1 HOLogic.mk_prod (x :: rec_fns')
berghofe@20376
  1655
                 in EVERY
wenzelm@60787
  1656
                   [resolve_tac ctxt [infer_instantiate ctxt
wenzelm@60787
  1657
                      [(#1 (dest_Var S),
wenzelm@60787
  1658
                        Thm.cterm_of ctxt (Const (@{const_name Nominal.supp},
berghofe@20376
  1659
                          fastype_of tuple --> HOLogic.mk_setT aT) $ tuple))]
wenzelm@60754
  1660
                      supports_fresh] 1,
wenzelm@60787
  1661
                    simp_tac (put_simpset HOL_basic_ss ctxt addsimps
wenzelm@36945
  1662
                      [supports_def, Thm.symmetric fresh_def, fresh_prod]) 1,
wenzelm@60787
  1663
                    REPEAT_DETERM (resolve_tac ctxt [allI, impI] 1),
wenzelm@60787
  1664
                    REPEAT_DETERM (eresolve_tac ctxt [conjE] 1),
wenzelm@60787
  1665
                    resolve_tac ctxt [unique] 1,
wenzelm@60787
  1666
                    SUBPROOF (fn {context = ctxt', prems = prems', params = [(_, a), (_, b)], ...} =>
wenzelm@60754
  1667
                      EVERY [cut_facts_tac [rec_prem] 1,
wenzelm@60787
  1668
                       resolve_tac ctxt' [Thm.instantiate ([],
wenzelm@60642
  1669
                         [((("pi", 0), mk_permT aT),
wenzelm@60787
  1670
                           Thm.cterm_of ctxt'
wenzelm@60754
  1671
                            (perm_of_pair (Thm.term_of a, Thm.term_of b)))]) eqvt_th] 1,
wenzelm@60787
  1672
                       asm_simp_tac (put_simpset HOL_ss ctxt' addsimps
wenzelm@60787
  1673
                         (prems' @ perm_swap @ perm_fresh_fresh)) 1]) ctxt 1,
wenzelm@60787
  1674
                    resolve_tac ctxt [rec_prem] 1,
wenzelm@60787
  1675
                    simp_tac (put_simpset HOL_ss ctxt addsimps (fs_name ::
berghofe@20376
  1676
                      supp_prod :: finite_Un :: finite_prems)) 1,
wenzelm@60787
  1677
                    simp_tac (put_simpset HOL_ss ctxt addsimps (Thm.symmetric fresh_def ::
berghofe@20376
  1678
                      fresh_prod :: fresh_prems)) 1]
berghofe@20376
  1679
                 end))
berghofe@20376
  1680
          end) (recTs ~~ rec_result_Ts ~~ rec_sets ~~ eqvt_ths)
berghofe@20376
  1681
      end) (dt_atomTs ~~ rec_equiv_thms' ~~ finite_premss);
berghofe@20376
  1682
berghofe@20376
  1683
    (** uniqueness **)
berghofe@20376
  1684
berghofe@21088
  1685
    val fun_tuple = foldr1 HOLogic.mk_prod (rec_ctxt :: rec_fns);
berghofe@20145
  1686
    val fun_tupleT = fastype_of fun_tuple;
berghofe@20145
  1687
    val rec_unique_frees =
blanchet@58112
  1688
      Old_Datatype_Prop.indexify_names (replicate (length recTs) "x") ~~ recTs;
berghofe@20397
  1689
    val rec_unique_frees'' = map (fn (s, T) => (s ^ "'", T)) rec_unique_frees;
berghofe@20267
  1690
    val rec_unique_frees' =
blanchet@58112
  1691
      Old_Datatype_Prop.indexify_names (replicate (length recTs) "y") ~~ rec_result_Ts;
berghofe@21021
  1692
    val rec_unique_concls = map (fn ((x, U), R) =>
haftmann@38558
  1693
        Const (@{const_name Ex1}, (U --> HOLogic.boolT) --> HOLogic.boolT) $
berghofe@21021
  1694
          Abs ("y", U, R $ Free x $ Bound 0))
berghofe@20145
  1695
      (rec_unique_frees ~~ rec_result_Ts ~~ rec_sets);
berghofe@20376
  1696
wenzelm@60787
  1697
    val induct_aux_rec =
wenzelm@60787
  1698
      infer_instantiate (Proof_Context.init_global thy11)
wenzelm@60787
  1699
      (map (apsnd (Thm.global_cterm_of thy11 o augment_sort thy11 fs_cp_sort))
wenzelm@60787
  1700
         (map (fn (aT, f) => (#1 (dest_Var (Logic.varify_global f)), Abs ("z", HOLogic.unitT,
wenzelm@56253
  1701
            Const (@{const_name Nominal.supp}, fun_tupleT --> HOLogic.mk_setT aT) $ fun_tuple)))
berghofe@20145
  1702
              fresh_fs @
berghofe@20145
  1703
          map (fn (((P, T), (x, U)), Q) =>
wenzelm@60787
  1704
           ((P, 0),
wenzelm@44241
  1705
            Abs ("z", HOLogic.unitT, absfree (x, U) Q)))
berghofe@20145
  1706
              (pnames ~~ recTs ~~ rec_unique_frees ~~ rec_unique_concls) @
wenzelm@60787
  1707
          map (fn (s, T) => ((s, 0), Free (s, T)))
berghofe@20145
  1708
            rec_unique_frees)) induct_aux;
berghofe@20145
  1709
berghofe@20376
  1710
    fun obtain_fresh_name vs ths rec_fin_supp T (freshs1, freshs2, ctxt) =
berghofe@20376
  1711
      let
berghofe@20376
  1712
        val p = foldr1 HOLogic.mk_prod (vs @ freshs1);
berghofe@20376
  1713
        val ex = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop
berghofe@20376
  1714
            (HOLogic.exists_const T $ Abs ("x", T,
berghofe@25823
  1715
              fresh_const T (fastype_of p) $ Bound 0 $ p)))
berghofe@20376
  1716
          (fn _ => EVERY
berghofe@20376
  1717
            [cut_facts_tac ths 1,
wenzelm@59498
  1718
             REPEAT_DETERM (dresolve_tac ctxt (the (AList.lookup op = rec_fin_supp T)) 1),
wenzelm@59498
  1719
             resolve_tac ctxt exists_fresh' 1,
wenzelm@51717
  1720
             asm_simp_tac (put_simpset HOL_ss ctxt addsimps (supp_prod :: finite_Un :: fs_atoms)) 1]);
wenzelm@32202
  1721
        val (([(_, cx)], ths), ctxt') = Obtain.result
wenzelm@60754
  1722
          (fn ctxt' => EVERY
wenzelm@60754
  1723
            [eresolve_tac ctxt' [exE] 1,
wenzelm@60754
  1724
             full_simp_tac (put_simpset HOL_ss ctxt' addsimps (fresh_prod :: fresh_atm)) 1,
wenzelm@60754
  1725
             REPEAT (eresolve_tac ctxt [conjE] 1)])
berghofe@20376
  1726
          [ex] ctxt
wenzelm@59582
  1727
      in (freshs1 @ [Thm.term_of cx], freshs2 @ ths, ctxt') end;
berghofe@20376
  1728
berghofe@21088
  1729
    val finite_ctxt_prems = map (fn aT =>
berghofe@22274
  1730
      HOLogic.mk_Trueprop
wenzelm@56253
  1731
        (Const (@{const_name finite}, HOLogic.mk_setT aT --> HOLogic.boolT) $
wenzelm@56253
  1732
           (Const (@{const_name Nominal.supp}, fsT' --> HOLogic.mk_setT aT) $ rec_ctxt))) dt_atomTs;
berghofe@21088
  1733
blanchet@58112
  1734
    val rec_unique_thms = Old_Datatype_Aux.split_conj_thm (Goal.prove
wenzelm@42361
  1735
      (Proof_Context.init_global thy11) (map fst rec_unique_frees)
berghofe@28731
  1736
      (map (augment_sort thy11 fs_cp_sort)
wenzelm@32952
  1737
        (flat finite_premss @ finite_ctxt_prems @ rec_prems @ rec_prems'))
berghofe@28731
  1738
      (augment_sort thy11 fs_cp_sort
berghofe@28731
  1739
        (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj rec_unique_concls)))
berghofe@20376
  1740
      (fn {prems, context} =>
berghofe@20267
  1741
         let
berghofe@20267
  1742
           val k = length rec_fns;
berghofe@20376
  1743
           val (finite_thss, ths1) = fold_map (fn T => fn xs =>
berghofe@20376
  1744
             apfst (pair T) (chop k xs)) dt_atomTs prems;
berghofe@21088
  1745
           val (finite_ctxt_ths, ths2) = chop (length dt_atomTs) ths1;
berghofe@21088
  1746
           val (P_ind_ths, fcbs) = chop k ths2;
blanchet@58112
  1747
           val P_ths = map (fn th => th RS mp) (Old_Datatype_Aux.split_conj_thm
berghofe@20376
  1748
             (Goal.prove context
berghofe@20397
  1749
               (map fst (rec_unique_frees'' @ rec_unique_frees')) []
berghofe@28731
  1750
               (augment_sort thy11 fs_cp_sort
berghofe@28731
  1751
                 (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
berghofe@28731
  1752
                    (map (fn (((x, y), S), P) => HOLogic.mk_imp
berghofe@28731
  1753
                      (S $ Free x $ Free y, P $ (Free y)))
berghofe@28731
  1754
                        (rec_unique_frees'' ~~ rec_unique_frees' ~~
berghofe@28731
  1755
                           rec_sets ~~ rec_preds)))))
wenzelm@58963
  1756
               (fn {context = ctxt, ...} =>
wenzelm@60754
  1757
                  resolve_tac ctxt [rec_induct] 1 THEN
wenzelm@59498
  1758
                  REPEAT ((resolve_tac ctxt P_ind_ths THEN_ALL_NEW assume_tac ctxt) 1))));
berghofe@20376
  1759
           val rec_fin_supp_thms' = map
berghofe@20376
  1760
             (fn (ths, (T, fin_ths)) => (T, map (curry op MRS fin_ths) ths))
berghofe@20376
  1761
             (rec_fin_supp_thms ~~ finite_thss);
berghofe@20267
  1762
         in EVERY
wenzelm@60754
  1763
           ([resolve_tac context [induct_aux_rec] 1] @
berghofe@21088
  1764
            maps (fn ((_, finite_ths), finite_th) =>
berghofe@21088
  1765
              [cut_facts_tac (finite_th :: finite_ths) 1,
wenzelm@51717
  1766
               asm_simp_tac (put_simpset HOL_ss context addsimps [supp_prod, finite_Un]) 1])
berghofe@21088
  1767
                (finite_thss ~~ finite_ctxt_ths) @
berghofe@20397
  1768
            maps (fn ((_, idxss), elim) => maps (fn idxs =>
wenzelm@51717
  1769
              [full_simp_tac (put_simpset HOL_ss context addsimps [Thm.symmetric fresh_def, supp_prod, Un_iff]) 1,
wenzelm@59498
  1770
               REPEAT_DETERM (eresolve_tac context @{thms conjE ex1E} 1),
wenzelm@60754
  1771
               resolve_tac context @{thms ex1I} 1,
wenzelm@60754
  1772
               (resolve_tac context rec_intrs THEN_ALL_NEW assume_tac context) 1,
berghofe@20397
  1773
               rotate_tac ~1 1,
wenzelm@60754
  1774
               ((DETERM o eresolve_tac context [elim]) THEN_ALL_NEW full_simp_tac
wenzelm@51717
  1775
                  (put_simpset HOL_ss context addsimps flat distinct_thms)) 1] @
wenzelm@51798
  1776
               (if null idxs then [] else [hyp_subst_tac context 1,
berghofe@20397
  1777
                SUBPROOF (fn {asms, concl, prems = prems', params, context = context', ...} =>
berghofe@20376
  1778
                  let
berghofe@21021
  1779
                    val SOME prem = find_first (can (HOLogic.dest_eq o
wenzelm@59582
  1780
                      HOLogic.dest_Trueprop o Thm.prop_of)) prems';
wenzelm@59582
  1781
                    val _ $ (_ $ lhs $ rhs) = Thm.prop_of prem;
wenzelm@59582
  1782
                    val _ $ (_ $ lhs' $ rhs') = Thm.term_of concl;
berghofe@20376
  1783
                    val rT = fastype_of lhs';
berghofe@20376
  1784
                    val (c, cargsl) = strip_comb lhs;
berghofe@20376
  1785
                    val cargsl' = partition_cargs idxs cargsl;
wenzelm@32952
  1786
                    val boundsl = maps fst cargsl';
berghofe@20376
  1787
                    val (_, cargsr) = strip_comb rhs;
berghofe@20376
  1788
                    val cargsr' = partition_cargs idxs cargsr;
wenzelm@32952
  1789
                    val boundsr = maps fst cargsr';
berghofe@20376
  1790
                    val (params1, _ :: params2) =
wenzelm@59582
  1791
                      chop (length params div 2) (map (Thm.term_of o #2) params);
berghofe@20376
  1792
                    val params' = params1 @ params2;
wenzelm@59582
  1793
                    val rec_prems = filter (fn th =>
wenzelm@59582
  1794
                        (case Thm.prop_of th of
wenzelm@59582
  1795
                          _ $ p =>
wenzelm@59582
  1796
                            (case head_of p of
wenzelm@59582
  1797
                              Const (s, _) => member (op =) rec_set_names s
wenzelm@59582
  1798
                            | _ => false)
wenzelm@59582
  1799
                        | _ => false)) prems';
wenzelm@59582
  1800
                    val fresh_prems = filter (fn th =>
wenzelm@59582
  1801
                      (case Thm.prop_of th of
wenzelm@56253
  1802
                        _ $ (Const (@{const_name Nominal.fresh}, _) $ _ $ _) => true
haftmann@38558
  1803
                      | _ $ (Const (@{const_name Not}, _) $ _) => true
wenzelm@59582
  1804
                      | _ => false)) prems';
berghofe@20376
  1805
                    val Ts = map fastype_of boundsl;
berghofe@20376
  1806
berghofe@20376
  1807
                    val _ = warning "step 1: obtaining fresh names";
berghofe@20376
  1808
                    val (freshs1, freshs2, context'') = fold
berghofe@28731
  1809
                      (obtain_fresh_name (rec_ctxt :: rec_fns' @ params')
wenzelm@32952
  1810
                         (maps snd finite_thss @ finite_ctxt_ths @ rec_prems)
berghofe@20376
  1811
                         rec_fin_supp_thms')
berghofe@20376
  1812
                      Ts ([], [], context');
berghofe@20376
  1813
                    val pi1 = map perm_of_pair (boundsl ~~ freshs1);
berghofe@20376
  1814
                    val rpi1 = rev pi1;
berghofe@20376
  1815
                    val pi2 = map perm_of_pair (boundsr ~~ freshs1);
berghofe@21073
  1816
                    val rpi2 = rev pi2;
berghofe@20376
  1817
berghofe@20376
  1818
                    val fresh_prems' = mk_not_sym fresh_prems;
berghofe@20376
  1819
                    val freshs2' = mk_not_sym freshs2;
berghofe@20376
  1820
berghofe@20376
  1821
                    (** as, bs, cs # K as ts, K bs us **)
berghofe@20376
  1822
                    val _ = warning "step 2: as, bs, cs # K as ts, K bs us";
wenzelm@51717
  1823
                    val prove_fresh_simpset = put_simpset HOL_ss context'' addsimps
wenzelm@32952
  1824
                      (finite_Diff :: flat fresh_thms @
berghofe@20376
  1825
                       fs_atoms @ abs_fresh @ abs_supp @ fresh_atm);
berghofe@20376
  1826
                    (* FIXME: avoid asm_full_simp_tac ? *)
berghofe@20376
  1827
                    fun prove_fresh ths y x = Goal.prove context'' [] []
berghofe@25823
  1828
                      (HOLogic.mk_Trueprop (fresh_const
berghofe@25823
  1829
                         (fastype_of x) (fastype_of y) $ x $ y))
wenzelm@51717
  1830
                      (fn _ => cut_facts_tac ths 1 THEN asm_full_simp_tac prove_fresh_simpset 1);
berghofe@20376
  1831
                    val constr_fresh_thms =
berghofe@20376
  1832
                      map (prove_fresh fresh_prems lhs) boundsl @
berghofe@20376
  1833
                      map (prove_fresh fresh_prems rhs) boundsr @
berghofe@20376
  1834
                      map (prove_fresh freshs2 lhs) freshs1 @
berghofe@20376
  1835
                      map (prove_fresh freshs2 rhs) freshs1;
berghofe@20376
  1836
berghofe@20376
  1837
                    (** pi1 o (K as ts) = pi2 o (K bs us) **)
berghofe@20376
  1838
                    val _ = warning "step 3: pi1 o (K as ts) = pi2 o (K bs us)";
berghofe@20376
  1839
                    val pi1_pi2_eq = Goal.prove context'' [] []
berghofe@20376
  1840
                      (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@22311
  1841
                        (fold_rev (mk_perm []) pi1 lhs, fold_rev (mk_perm []) pi2 rhs)))
wenzelm@60754
  1842
                      (fn {context = ctxt, ...} => EVERY
berghofe@20376
  1843
                         [cut_facts_tac constr_fresh_thms 1,
wenzelm@51717
  1844
                          asm_simp_tac (put_simpset HOL_basic_ss context'' addsimps perm_fresh_fresh) 1,
wenzelm@60754
  1845
                          resolve_tac ctxt [prem] 1]);
berghofe@20376
  1846
berghofe@20376
  1847
                    (** pi1 o ts = pi2 o us **)
berghofe@20376
  1848
                    val _ = warning "step 4: pi1 o ts = pi2 o us";
berghofe@20376
  1849
                    val pi1_pi2_eqs = map (fn (t, u) =>
berghofe@20376
  1850
                      Goal.prove context'' [] []
berghofe@20376
  1851
                        (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@22311
  1852
                          (fold_rev (mk_perm []) pi1 t, fold_rev (mk_perm []) pi2 u)))
berghofe@20376
  1853
                        (fn _ => EVERY
berghofe@20376
  1854
                           [cut_facts_tac [pi1_pi2_eq] 1,
wenzelm@51717
  1855
                            asm_full_simp_tac (put_simpset HOL_ss context'' addsimps
wenzelm@32952
  1856
                              (calc_atm @ flat perm_simps' @
berghofe@20376
  1857
                               fresh_prems' @ freshs2' @ abs_perm @
wenzelm@32952
  1858
                               alpha @ flat inject_thms)) 1]))
berghofe@20376
  1859
                        (map snd cargsl' ~~ map snd cargsr');
berghofe@20376
  1860
berghofe@20376
  1861
                    (** pi1^-1 o pi2 o us = ts **)
berghofe@20376
  1862
                    val _ = warning "step 5: pi1^-1 o pi2 o us = ts";
berghofe@20376
  1863
                    val rpi1_pi2_eqs = map (fn ((t, u), eq) =>
berghofe@20376
  1864
                      Goal.prove context'' [] []
berghofe@20376
  1865
                        (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@22311
  1866
                          (fold_rev (mk_perm []) (rpi1 @ pi2) u, t)))
wenzelm@51717
  1867
                        (fn _ => simp_tac (put_simpset HOL_ss context'' addsimps
berghofe@20376
  1868
                           ((eq RS sym) :: perm_swap)) 1))
berghofe@20376
  1869
                        (map snd cargsl' ~~ map snd cargsr' ~~ pi1_pi2_eqs);
berghofe@20376
  1870
berghofe@20376
  1871
                    val (rec_prems1, rec_prems2) =
berghofe@20376
  1872
                      chop (length rec_prems div 2) rec_prems;
berghofe@20376
  1873
berghofe@20376
  1874
                    (** (ts, pi1^-1 o pi2 o vs) in rec_set **)
berghofe@20376
  1875
                    val _ = warning "step 6: (ts, pi1^-1 o pi2 o vs) in rec_set";
berghofe@20376
  1876
                    val rec_prems' = map (fn th =>
berghofe@20376
  1877
                      let
wenzelm@59582
  1878
                        val _ $ (S $ x $ y) = Thm.prop_of th;
berghofe@28731
  1879
                        val Const (s, _) = head_of S;
berghofe@28731
  1880
                        val k = find_index (equal s) rec_set_names;
berghofe@20376
  1881
                        val pi = rpi1 @ pi2;
berghofe@22311
  1882
                        fun mk_pi z = fold_rev (mk_perm []) pi z;
wenzelm@60754
  1883
                        fun eqvt_tac ctxt p =
berghofe@20376
  1884
                          let
berghofe@20376
  1885
                            val U as Type (_, [Type (_, [T, _])]) = fastype_of p;
berghofe@20376
  1886
                            val l = find_index (equal T) dt_atomTs;
wenzelm@42364
  1887
                            val th = nth (nth rec_equiv_thms' l) k;
berghofe@20376
  1888
                            val th' = Thm.instantiate ([],
wenzelm@60642
  1889
                              [((("pi", 0), U), Thm.global_cterm_of thy11 p)]) th;
wenzelm@60754
  1890
                          in resolve_tac ctxt [th'] 1 end;
berghofe@20376
  1891
                        val th' = Goal.prove context'' [] []
berghofe@21021
  1892
                          (HOLogic.mk_Trueprop (S $ mk_pi x $ mk_pi y))
wenzelm@60754
  1893
                          (fn {context = ctxt, ...} => EVERY
wenzelm@60754
  1894
                             (map (eqvt_tac ctxt) pi @
wenzelm@51717
  1895
                              [simp_tac (put_simpset HOL_ss context'' addsimps (fresh_prems' @ freshs2' @
berghofe@20376
  1896
                                 perm_swap @ perm_fresh_fresh)) 1,
wenzelm@60754
  1897
                               resolve_tac ctxt [th] 1]))
berghofe@20376
  1898
                      in
berghofe@20376
  1899
                        Simplifier.simplify
wenzelm@51717
  1900
                          (put_simpset HOL_basic_ss context'' addsimps rpi1_pi2_eqs) th'
berghofe@20376
  1901
                      end) rec_prems2;
berghofe@20376
  1902
wenzelm@59582
  1903
                    val ihs = filter (fn th =>
wenzelm@59582
  1904
                      (case Thm.prop_of th of
wenzelm@59582
  1905
                        _ $ (Const (@{const_name All}, _) $ _) => true
wenzelm@59582
  1906
                      | _ => false)) prems';
berghofe@20376
  1907
berghofe@21073
  1908
                    (** pi1 o rs = pi2 o vs , rs = pi1^-1 o pi2 o vs **)
berghofe@21073
  1909
                    val _ = warning "step 7: pi1 o rs = pi2 o vs , rs = pi1^-1 o pi2 o vs";
berghofe@21073
  1910
                    val rec_eqns = map (fn (th, ih) =>
berghofe@20376
  1911
                      let
berghofe@20376
  1912
                        val th' = th RS (ih RS spec RS mp) RS sym;
wenzelm@59582
  1913
                        val _ $ (_ $ lhs $ rhs) = Thm.prop_of th';
berghofe@20376
  1914
                        fun strip_perm (_ $ _ $ t) = strip_perm t
berghofe@20376
  1915
                          | strip_perm t = t;
berghofe@20376
  1916
                      in
berghofe@21073
  1917
                        Goal.prove context'' [] []
berghofe@20376
  1918
                           (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@22311
  1919
                              (fold_rev (mk_perm []) pi1 lhs,
berghofe@22311
  1920
                               fold_rev (mk_perm []) pi2 (strip_perm rhs))))
wenzelm@51717
  1921
                           (fn _ => simp_tac (put_simpset HOL_basic_ss context'' addsimps
berghofe@21073
  1922
                              (th' :: perm_swap)) 1)
berghofe@21073
  1923
                      end) (rec_prems' ~~ ihs);
berghofe@20376
  1924
berghofe@21073
  1925
                    (** as # rs **)
berghofe@21073
  1926
                    val _ = warning "step 8: as # rs";
wenzelm@32952
  1927
                    val rec_freshs =
wenzelm@32952
  1928
                      maps (fn (rec_prem, ih) =>
berghofe@20376
  1929
                        let
berghofe@21021
  1930
                          val _ $ (S $ x $ (y as Free (_, T))) =
wenzelm@59582
  1931
                            Thm.prop_of rec_prem;
berghofe@20376
  1932
                          val k = find_index (equal S) rec_sets;
wenzelm@32952
  1933
                          val atoms = flat (map_filter (fn (bs, z) =>
berghofe@21073
  1934
                            if z = x then NONE else SOME bs) cargsl')
berghofe@20376
  1935
                        in
berghofe@21073
  1936
                          map (fn a as Free (_, aT) =>
berghofe@21073
  1937
                            let val l = find_index (equal aT) dt_atomTs;
berghofe@21073
  1938
                            in
berghofe@21073
  1939
                              Goal.prove context'' [] []
berghofe@25823
  1940
                                (HOLogic.mk_Trueprop (fresh_const aT T $ a $ y))
wenzelm@60754
  1941
                                (fn {context = ctxt, ...} => EVERY
wenzelm@60754
  1942
                                   (resolve_tac ctxt [nth (nth rec_fresh_thms l) k] 1 ::
wenzelm@60754
  1943
                                    map (fn th => resolve_tac ctxt [th] 1)
wenzelm@42364
  1944
                                      (snd (nth finite_thss l)) @
wenzelm@60754
  1945
                                    [resolve_tac ctxt [rec_prem] 1,
wenzelm@60754
  1946
                                     resolve_tac ctxt [ih] 1,
wenzelm@60754
  1947
                                     REPEAT_DETERM (resolve_tac ctxt fresh_prems 1)]))
berghofe@21073
  1948
                            end) atoms
wenzelm@32952
  1949
                        end) (rec_prems1 ~~ ihs);
berghofe@20376
  1950
berghofe@20376
  1951
                    (** as # fK as ts rs , bs # fK bs us vs **)
berghofe@20376
  1952
                    val _ = warning "step 9: as # fK as ts rs , bs # fK bs us vs";
berghofe@21073
  1953
                    fun prove_fresh_result (a as Free (_, aT)) =
berghofe@20376
  1954
                      Goal.prove context'' [] []
berghofe@25823
  1955
                        (HOLogic.mk_Trueprop (fresh_const aT rT $ a $ rhs'))
berghofe@20376
  1956
                        (fn _ => EVERY
wenzelm@59498
  1957
                           [resolve_tac context'' fcbs 1,
wenzelm@59498
  1958
                            REPEAT_DETERM (resolve_tac context''
berghofe@21073
  1959
                              (fresh_prems @ rec_freshs) 1),
wenzelm@59498
  1960
                            REPEAT_DETERM (resolve_tac context'' (maps snd rec_fin_supp_thms') 1
wenzelm@59498
  1961
                              THEN resolve_tac context'' rec_prems 1),
wenzelm@59498
  1962
                            resolve_tac context'' P_ind_ths 1,
wenzelm@59498
  1963
                            REPEAT_DETERM (resolve_tac context'' (P_ths @ rec_prems) 1)]);
wenzelm@21365
  1964
berghofe@21073
  1965
                    val fresh_results'' = map prove_fresh_result boundsl;
berghofe@21073
  1966
berghofe@21073
  1967
                    fun prove_fresh_result'' ((a as Free (_, aT), b), th) =
berghofe@21073
  1968
                      let val th' = Goal.prove context'' [] []
berghofe@25823
  1969
                        (HOLogic.mk_Trueprop (fresh_const aT rT $
berghofe@22311
  1970
                            fold_rev (mk_perm []) (rpi2 @ pi1) a $
berghofe@22311
  1971
                            fold_rev (mk_perm []) (rpi2 @ pi1) rhs'))
wenzelm@60754
  1972
                        (fn {context = ctxt, ...} =>
wenzelm@60754
  1973
                          simp_tac (put_simpset HOL_ss context'' addsimps fresh_bij) 1 THEN
wenzelm@60754
  1974
                          resolve_tac ctxt [th] 1)
berghofe@21073
  1975
                      in
berghofe@21073
  1976
                        Goal.prove context'' [] []
berghofe@25823
  1977
                          (HOLogic.mk_Trueprop (fresh_const aT rT $ b $ lhs'))
wenzelm@54895
  1978
                          (fn {context = ctxt, ...} => EVERY
berghofe@21073
  1979
                             [cut_facts_tac [th'] 1,
wenzelm@54895
  1980
                              full_simp_tac (put_simpset HOL_ss ctxt
berghofe@25998
  1981
                                addsimps rec_eqns @ pi1_pi2_eqs @ perm_swap
berghofe@25998
  1982
                                addsimprocs [NominalPermeq.perm_simproc_app]) 1,
wenzelm@51717
  1983
                              full_simp_tac (put_simpset HOL_ss context'' addsimps (calc_atm @
berghofe@21073
  1984
                                fresh_prems' @ freshs2' @ perm_fresh_fresh)) 1])
berghofe@21073
  1985
                      end;
berghofe@21073
  1986
berghofe@21073
  1987
                    val fresh_results = fresh_results'' @ map prove_fresh_result''
berghofe@21073
  1988
                      (boundsl ~~ boundsr ~~ fresh_results'');
berghofe@20376
  1989
berghofe@20376
  1990
                    (** cs # fK as ts rs , cs # fK bs us vs **)
berghofe@20376
  1991
                    val _ = warning "step 10: cs # fK as ts rs , cs # fK bs us vs";
berghofe@20376
  1992
                    fun prove_fresh_result' recs t (a as Free (_, aT)) =
berghofe@20376
  1993
                      Goal.prove context'' [] []
berghofe@25823
  1994
                        (HOLogic.mk_Trueprop (fresh_const aT rT $ a $ t))
berghofe@20376
  1995
                        (fn _ => EVERY
berghofe@20376
  1996
                          [cut_facts_tac recs 1,
wenzelm@59498
  1997
                           REPEAT_DETERM (dresolve_tac context''
berghofe@20376
  1998
                             (the (AList.lookup op = rec_fin_supp_thms' aT)) 1),
berghofe@20376
  1999
                           NominalPermeq.fresh_guess_tac
wenzelm@51717
  2000
                             (put_simpset HOL_ss context'' addsimps (freshs2 @
berghofe@20376
  2001
                                fs_atoms @ fresh_atm @
wenzelm@32952
  2002
                                maps snd finite_thss)) 1]);
berghofe@20376
  2003
berghofe@20376
  2004
                    val fresh_results' =
berghofe@20376
  2005
                      map (prove_fresh_result' rec_prems1 rhs') freshs1 @
berghofe@20376
  2006
                      map (prove_fresh_result' rec_prems2 lhs') freshs1;
berghofe@20376
  2007
berghofe@20376
  2008
                    (** pi1 o (fK as ts rs) = pi2 o (fK bs us vs) **)
berghofe@20376
  2009
                    val _ = warning "step 11: pi1 o (fK as ts rs) = pi2 o (fK bs us vs)";
berghofe@20376
  2010
                    val pi1_pi2_result = Goal.prove context'' [] []
berghofe@20376
  2011
                      (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@22311
  2012
                        (fold_rev (mk_perm []) pi1 rhs', fold_rev (mk_perm []) pi2 lhs')))
wenzelm@51717
  2013
                      (fn _ => simp_tac (put_simpset HOL_ss context''
berghofe@27450
  2014
                           addsimps pi1_pi2_eqs @ rec_eqns
berghofe@27450
  2015
                           addsimprocs [NominalPermeq.perm_simproc_app]) 1 THEN
wenzelm@51717
  2016
                         TRY (simp_tac (put_simpset HOL_ss context'' addsimps
berghofe@20376
  2017
                           (fresh_prems' @ freshs2' @ calc_atm @ perm_fresh_fresh)) 1));
berghofe@20376
  2018
berghofe@20376
  2019
                    val _ = warning "final result";
wenzelm@59582
  2020
                    val final = Goal.prove context'' [] [] (Thm.term_of concl)
berghofe@20376
  2021
                      (fn _ => cut_facts_tac [pi1_pi2_result RS sym] 1 THEN
wenzelm@51717
  2022
                        full_simp_tac (put_simpset HOL_basic_ss context'' addsimps perm_fresh_fresh @
berghofe@20376
  2023
                          fresh_results @ fresh_results') 1);
wenzelm@42361
  2024
                    val final' = Proof_Context.export context'' context' [final];
berghofe@20376
  2025
                    val _ = warning "finished!"
berghofe@20376
  2026
                  in
wenzelm@59498
  2027
                    resolve_tac context' final' 1
berghofe@20397
  2028
                  end) context 1])) idxss) (ndescr ~~ rec_elims))
berghofe@20397
  2029
         end));
berghofe@20397
  2030
wenzelm@46161
  2031
    val rec_total_thms = map (fn r => r RS @{thm theI'}) rec_unique_thms;
berghofe@20397
  2032
berghofe@20397
  2033
    (* define primrec combinators *)
berghofe@20397
  2034
wenzelm@45701
  2035
    val big_reccomb_name = space_implode "_" new_type_names ^ "_rec";
haftmann@28965
  2036
    val reccomb_names = map (Sign.full_bname thy11)
berghofe@20397
  2037
      (if length descr'' = 1 then [big_reccomb_name] else
berghofe@20397
  2038
        (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
berghofe@20397
  2039
          (1 upto (length descr''))));
berghofe@20397
  2040
    val reccombs = map (fn ((name, T), T') => list_comb
berghofe@20397
  2041
      (Const (name, rec_fn_Ts @ [T] ---> T'), rec_fns))
berghofe@20397
  2042
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
berghofe@20397
  2043
berghofe@20397
  2044
    val (reccomb_defs, thy12) =
berghofe@20397
  2045
      thy11
wenzelm@56239
  2046
      |> Sign.add_consts (map (fn ((name, T), T') =>
wenzelm@30364
  2047
          (Binding.name (Long_Name.base_name name), rec_fn_Ts @ [T] ---> T', NoSyn))
berghofe@20397
  2048
          (reccomb_names ~~ recTs ~~ rec_result_Ts))
wenzelm@39557
  2049
      |> (Global_Theory.add_defs false o map Thm.no_attributes) (map (fn ((((name, comb), set), T), T') =>
wenzelm@44241
  2050
          (Binding.name (Long_Name.base_name name ^ "_def"), Logic.mk_equals (comb, absfree ("x", T)
wenzelm@44241
  2051
           (Const (@{const_name The}, (T' --> HOLogic.boolT) --> T') $ absfree ("y", T')
wenzelm@44241
  2052
             (set $ Free ("x", T) $ Free ("y", T'))))))
wenzelm@52788
  2053
               (reccomb_names ~~ reccombs ~~ rec_sets ~~ recTs ~~ rec_result_Ts));
berghofe@20397
  2054
berghofe@20397
  2055
    (* prove characteristic equations for primrec combinators *)
berghofe@20397
  2056
berghofe@20397
  2057
    val rec_thms = map (fn (prems, concl) =>
berghofe@20397
  2058
      let
berghofe@20397
  2059
        val _ $ (_ $ (_ $ x) $ _) = concl;
berghofe@20397
  2060
        val (_, cargs) = strip_comb x;
berghofe@20397
  2061
        val ps = map (fn (x as Free (_, T), i) =>
berghofe@20397
  2062
          (Free ("x" ^ string_of_int i, T), x)) (cargs ~~ (1 upto length cargs));
berghofe@20397
  2063
        val concl' = subst_atomic_types (rec_result_Ts' ~~ rec_result_Ts) concl;
wenzelm@32952
  2064
        val prems' = flat finite_premss @ finite_ctxt_prems @
berghofe@21088
  2065
          rec_prems @ rec_prems' @ map (subst_atomic ps) prems;
wenzelm@59498
  2066
        fun solve ctxt rules prems = resolve_tac ctxt rules THEN_ALL_NEW
wenzelm@60754
  2067
          (resolve_tac ctxt prems THEN_ALL_NEW assume_tac ctxt)
berghofe@20397
  2068
      in
wenzelm@51122
  2069
        Goal.prove_global_future thy12 []
berghofe@28731
  2070
          (map (augment_sort thy12 fs_cp_sort) prems')
berghofe@28731
  2071
          (augment_sort thy12 fs_cp_sort concl')
wenzelm@54742
  2072
          (fn {context = ctxt, prems} => EVERY
wenzelm@54742
  2073
            [rewrite_goals_tac ctxt reccomb_defs,
wenzelm@60754
  2074
             resolve_tac ctxt @{thms the1_equality} 1,
wenzelm@59498
  2075
             solve ctxt rec_unique_thms prems 1,
wenzelm@59498
  2076
             resolve_tac ctxt rec_intrs 1,
wenzelm@59498
  2077
             REPEAT (solve ctxt (prems @ rec_total_thms) prems 1)])
berghofe@20397
  2078
      end) (rec_eq_prems ~~
blanchet@58112
  2079
        Old_Datatype_Prop.make_primrecs reccomb_names descr' thy12);
wenzelm@21365
  2080
wenzelm@45822
  2081
    val dt_infos = map_index (make_dt_info pdescr induct reccomb_names rec_thms)
haftmann@33063
  2082
      (descr1 ~~ distinct_thms ~~ inject_thms);
berghofe@21540
  2083
berghofe@19985
  2084
    (* FIXME: theorems are stored in database for testing only *)
berghofe@20397
  2085
    val (_, thy13) = thy12 |>
wenzelm@39557
  2086
      Global_Theory.add_thmss
wenzelm@32952
  2087
        [((Binding.name "rec_equiv", flat rec_equiv_thms), []),
wenzelm@32952
  2088
         ((Binding.name "rec_equiv'", flat rec_equiv_thms'), []),
wenzelm@32952
  2089
         ((Binding.name "rec_fin_supp", flat rec_fin_supp_thms), []),
wenzelm@32952
  2090
         ((Binding.name "rec_fresh", flat rec_fresh_thms), []),
wenzelm@35021
  2091
         ((Binding.name "rec_unique", map Drule.export_without_context rec_unique_thms), []),
haftmann@29585
  2092
         ((Binding.name "recs", rec_thms), [])] ||>
wenzelm@24712
  2093
      Sign.parent_path ||>
wenzelm@52788
  2094
      map_nominal_datatypes (fold Symtab.update dt_infos);
berghofe@17870
  2095
  in
berghofe@20397
  2096
    thy13
berghofe@17870
  2097
  end;
berghofe@17870
  2098
blanchet@58112
  2099
val nominal_datatype = gen_nominal_datatype Old_Datatype.check_specs;
blanchet@58112
  2100
val nominal_datatype_cmd = gen_nominal_datatype Old_Datatype.read_specs;
berghofe@17870
  2101
wenzelm@24867
  2102
val _ =
wenzelm@59936
  2103
  Outer_Syntax.command @{command_keyword nominal_datatype} "define nominal datatypes"
blanchet@58112
  2104
    (Parse.and_list1 Old_Datatype.spec_cmd >>
blanchet@58112
  2105
      (Toplevel.theory o nominal_datatype_cmd Old_Datatype_Aux.default_config));
berghofe@17870
  2106
berghofe@18261
  2107
end