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