src/HOL/Tools/datatype_package/datatype_package.ML
author haftmann
Tue Jun 16 16:37:07 2009 +0200 (2009-06-16)
changeset 31668 a616e56a5ec8
parent 31604 eb2f9d709296
child 31689 84a14d2dc868
permissions -rw-r--r--
datatype packages: record datatype_config for configuration flags; less verbose signatures
berghofe@5177
     1
(*  Title:      HOL/Tools/datatype_package.ML
wenzelm@11539
     2
    Author:     Stefan Berghofer, TU Muenchen
berghofe@5177
     3
wenzelm@6360
     4
Datatype package for Isabelle/HOL.
berghofe@5177
     5
*)
berghofe@5177
     6
wenzelm@27097
     7
signature DATATYPE_PACKAGE =
wenzelm@6360
     8
sig
haftmann@31668
     9
  type datatype_config = DatatypeAux.datatype_config
haftmann@31604
    10
  type datatype_info = DatatypeAux.datatype_info
haftmann@31604
    11
  type descr = DatatypeAux.descr
haftmann@31604
    12
  val get_datatypes : theory -> datatype_info Symtab.table
haftmann@31604
    13
  val get_datatype : theory -> string -> datatype_info option
haftmann@31604
    14
  val the_datatype : theory -> string -> datatype_info
haftmann@31604
    15
  val datatype_of_constr : theory -> string -> datatype_info option
haftmann@31604
    16
  val datatype_of_case : theory -> string -> datatype_info option
wenzelm@27097
    17
  val the_datatype_spec : theory -> string -> (string * sort) list * (string * typ list) list
haftmann@31604
    18
  val the_datatype_descr : theory -> string list
haftmann@31604
    19
    -> descr * (string * sort) list * string list
haftmann@31604
    20
      * (string list * string list) * (typ list * typ list)
wenzelm@27097
    21
  val get_datatype_constrs : theory -> string -> (string * typ) list option
berghofe@7015
    22
  val distinct_simproc : simproc
wenzelm@27097
    23
  val make_case :  Proof.context -> bool -> string list -> term ->
wenzelm@27097
    24
    (term * term) list -> term * (term * (int * bool)) list
wenzelm@27097
    25
  val strip_case : Proof.context -> bool -> term -> (term * (term * term) list) option
wenzelm@27277
    26
  val read_typ: theory ->
wenzelm@27277
    27
    (typ list * (string * sort) list) * string -> typ list * (string * sort) list
haftmann@31668
    28
  val interpretation : (datatype_config -> string list -> theory -> theory) -> theory -> theory
haftmann@31668
    29
  type rules = {distinct : thm list list,
haftmann@31668
    30
    inject : thm list list,
haftmann@31668
    31
    exhaustion : thm list,
haftmann@31668
    32
    rec_thms : thm list,
haftmann@31668
    33
    case_thms : thm list list,
haftmann@31668
    34
    split_thms : (thm * thm) list,
haftmann@31668
    35
    induction : thm,
haftmann@31668
    36
    simps : thm list}
haftmann@31668
    37
  val rep_datatype : datatype_config -> (rules -> Proof.context -> Proof.context)
haftmann@31668
    38
    -> string list option -> term list -> theory -> Proof.state;
haftmann@31668
    39
  val rep_datatype_cmd : string list option -> string list -> theory -> Proof.state
haftmann@31668
    40
  val add_datatype : datatype_config -> string list -> (string list * binding * mixfix *
haftmann@31668
    41
    (binding * typ list * mixfix) list) list -> theory -> rules * theory
haftmann@31668
    42
  val add_datatype_cmd : string list -> (string list * binding * mixfix *
haftmann@31668
    43
    (binding * string list * mixfix) list) list -> theory -> rules * theory
wenzelm@18708
    44
  val setup: theory -> theory
haftmann@31604
    45
  val print_datatypes : theory -> unit
berghofe@5177
    46
end;
berghofe@5177
    47
berghofe@5177
    48
structure DatatypePackage : DATATYPE_PACKAGE =
berghofe@5177
    49
struct
berghofe@5177
    50
berghofe@5177
    51
open DatatypeAux;
berghofe@5177
    52
wenzelm@6360
    53
wenzelm@22846
    54
(* theory data *)
berghofe@5177
    55
wenzelm@16430
    56
structure DatatypesData = TheoryDataFun
wenzelm@22846
    57
(
berghofe@22777
    58
  type T =
berghofe@22777
    59
    {types: datatype_info Symtab.table,
berghofe@22777
    60
     constrs: datatype_info Symtab.table,
berghofe@22777
    61
     cases: datatype_info Symtab.table};
berghofe@5177
    62
berghofe@22777
    63
  val empty =
berghofe@22777
    64
    {types = Symtab.empty, constrs = Symtab.empty, cases = Symtab.empty};
wenzelm@6556
    65
  val copy = I;
wenzelm@16430
    66
  val extend = I;
berghofe@22777
    67
  fun merge _
berghofe@22777
    68
    ({types = types1, constrs = constrs1, cases = cases1},
berghofe@22777
    69
     {types = types2, constrs = constrs2, cases = cases2}) =
berghofe@22777
    70
    {types = Symtab.merge (K true) (types1, types2),
berghofe@22777
    71
     constrs = Symtab.merge (K true) (constrs1, constrs2),
berghofe@22777
    72
     cases = Symtab.merge (K true) (cases1, cases2)};
wenzelm@22846
    73
);
berghofe@5177
    74
berghofe@22777
    75
val get_datatypes = #types o DatatypesData.get;
berghofe@22777
    76
val map_datatypes = DatatypesData.map;
wenzelm@22846
    77
wenzelm@22846
    78
fun print_datatypes thy =
wenzelm@22846
    79
  Pretty.writeln (Pretty.strs ("datatypes:" ::
wenzelm@22846
    80
    map #1 (NameSpace.extern_table (Sign.type_space thy, get_datatypes thy))));
berghofe@5177
    81
berghofe@5177
    82
berghofe@5177
    83
(** theory information about datatypes **)
berghofe@5177
    84
berghofe@22777
    85
fun put_dt_infos (dt_infos : (string * datatype_info) list) =
berghofe@22777
    86
  map_datatypes (fn {types, constrs, cases} =>
berghofe@22777
    87
    {types = fold Symtab.update dt_infos types,
haftmann@31208
    88
     constrs = fold Symtab.default (*conservative wrt. overloaded constructors*)
berghofe@22777
    89
       (maps (fn (_, info as {descr, index, ...}) => map (rpair info o fst)
berghofe@22777
    90
          (#3 (the (AList.lookup op = descr index)))) dt_infos) constrs,
berghofe@22777
    91
     cases = fold Symtab.update
berghofe@22777
    92
       (map (fn (_, info as {case_name, ...}) => (case_name, info)) dt_infos)
berghofe@22777
    93
       cases});
berghofe@22777
    94
haftmann@19346
    95
val get_datatype = Symtab.lookup o get_datatypes;
berghofe@5177
    96
haftmann@19346
    97
fun the_datatype thy name = (case get_datatype thy name of
skalberg@15531
    98
      SOME info => info
skalberg@15531
    99
    | NONE => error ("Unknown datatype " ^ quote name));
berghofe@5177
   100
berghofe@22777
   101
val datatype_of_constr = Symtab.lookup o #constrs o DatatypesData.get;
berghofe@22777
   102
val datatype_of_case = Symtab.lookup o #cases o DatatypesData.get;
berghofe@22777
   103
haftmann@19346
   104
fun get_datatype_descr thy dtco =
haftmann@19346
   105
  get_datatype thy dtco
wenzelm@24711
   106
  |> Option.map (fn info as { descr, index, ... } =>
haftmann@19346
   107
       (info, (((fn SOME (_, dtys, cos) => (dtys, cos)) o AList.lookup (op =) descr) index)));
berghofe@7015
   108
haftmann@25537
   109
fun the_datatype_spec thy dtco =
haftmann@18518
   110
  let
haftmann@26093
   111
    val info as { descr, index, sorts = raw_sorts, ... } = the_datatype thy dtco;
haftmann@26093
   112
    val SOME (_, dtys, raw_cos) = AList.lookup (op =) descr index;
haftmann@26093
   113
    val sorts = map ((fn v => (v, (the o AList.lookup (op =) raw_sorts) v))
haftmann@26093
   114
      o DatatypeAux.dest_DtTFree) dtys;
haftmann@26093
   115
    val cos = map
haftmann@26093
   116
      (fn (co, tys) => (co, map (DatatypeAux.typ_of_dtyp descr sorts) tys)) raw_cos;
haftmann@26093
   117
  in (sorts, cos) end;
haftmann@18518
   118
haftmann@31604
   119
fun the_datatype_descr thy (raw_tycos as raw_tyco :: _) =
haftmann@31604
   120
  let
haftmann@31604
   121
    val info = the_datatype thy raw_tyco;
haftmann@31604
   122
    val descr = #descr info;
haftmann@31604
   123
haftmann@31604
   124
    val SOME (_, dtys, raw_cos) = AList.lookup (op =) descr (#index info);
haftmann@31604
   125
    val vs = map ((fn v => (v, (the o AList.lookup (op =) (#sorts info)) v))
haftmann@31604
   126
      o dest_DtTFree) dtys;
haftmann@31604
   127
haftmann@31604
   128
    fun is_DtTFree (DtTFree _) = true
haftmann@31604
   129
      | is_DtTFree _ = false
haftmann@31604
   130
    val k = find_index (fn (_, (_, dTs, _)) => not (forall is_DtTFree dTs)) descr;
haftmann@31604
   131
    val protoTs as (dataTs, _) = chop k descr
haftmann@31604
   132
      |> (pairself o map) (fn (_, (tyco, dTs, _)) => (tyco, map (typ_of_dtyp descr vs) dTs));
haftmann@31604
   133
    
haftmann@31604
   134
    val tycos = map fst dataTs;
haftmann@31604
   135
    val _ = if gen_eq_set (op =) (tycos, raw_tycos) then ()
haftmann@31604
   136
      else error ("Type constructors " ^ commas (map quote raw_tycos)
haftmann@31604
   137
        ^ "do not belong exhaustively to one mutual recursive datatype");
haftmann@31604
   138
haftmann@31604
   139
    val (Ts, Us) = (pairself o map) Type protoTs;
haftmann@31604
   140
haftmann@31604
   141
    val names = map Long_Name.base_name (the_default tycos (#alt_names info));
haftmann@31604
   142
    val (auxnames, _) = Name.make_context names
haftmann@31604
   143
      |> fold_map (yield_singleton Name.variants o name_of_typ) Us
haftmann@31604
   144
haftmann@31604
   145
  in (descr, vs, tycos, (names, auxnames), (Ts, Us)) end;
haftmann@31604
   146
haftmann@19346
   147
fun get_datatype_constrs thy dtco =
haftmann@25537
   148
  case try (the_datatype_spec thy) dtco
haftmann@19346
   149
   of SOME (sorts, cos) =>
haftmann@19346
   150
        let
haftmann@19346
   151
          fun subst (v, sort) = TVar ((v, 0), sort);
haftmann@19346
   152
          fun subst_ty (TFree v) = subst v
haftmann@19346
   153
            | subst_ty ty = ty;
haftmann@19346
   154
          val dty = Type (dtco, map subst sorts);
haftmann@19346
   155
          fun mk_co (co, tys) = (co, map (Term.map_atyps subst_ty) tys ---> dty);
haftmann@19346
   156
        in SOME (map mk_co cos) end
haftmann@19346
   157
    | NONE => NONE;
haftmann@18518
   158
berghofe@5177
   159
wenzelm@8437
   160
(** induct method setup **)
wenzelm@8437
   161
wenzelm@8437
   162
(* case names *)
wenzelm@8437
   163
wenzelm@8437
   164
local
wenzelm@8437
   165
wenzelm@8437
   166
fun dt_recs (DtTFree _) = []
haftmann@21045
   167
  | dt_recs (DtType (_, dts)) = maps dt_recs dts
wenzelm@8437
   168
  | dt_recs (DtRec i) = [i];
wenzelm@8437
   169
wenzelm@8437
   170
fun dt_cases (descr: descr) (_, args, constrs) =
wenzelm@8437
   171
  let
wenzelm@30364
   172
    fun the_bname i = Long_Name.base_name (#1 (the (AList.lookup (op =) descr i)));
haftmann@21045
   173
    val bnames = map the_bname (distinct (op =) (maps dt_recs args));
wenzelm@30364
   174
  in map (fn (c, _) => space_implode "_" (Long_Name.base_name c :: bnames)) constrs end;
wenzelm@8437
   175
wenzelm@8437
   176
wenzelm@8437
   177
fun induct_cases descr =
haftmann@21045
   178
  DatatypeProp.indexify_names (maps (dt_cases descr) (map #2 descr));
wenzelm@8437
   179
haftmann@21045
   180
fun exhaust_cases descr i = dt_cases descr (the (AList.lookup (op =) descr i));
wenzelm@8437
   181
wenzelm@8437
   182
in
wenzelm@8437
   183
wenzelm@8437
   184
fun mk_case_names_induct descr = RuleCases.case_names (induct_cases descr);
wenzelm@8437
   185
wenzelm@8437
   186
fun mk_case_names_exhausts descr new =
wenzelm@8437
   187
  map (RuleCases.case_names o exhaust_cases descr o #1)
haftmann@21045
   188
    (filter (fn ((_, (name, _, _))) => member (op =) new name) descr);
wenzelm@8437
   189
wenzelm@8437
   190
end;
wenzelm@8437
   191
haftmann@24699
   192
fun add_rules simps case_thms rec_thms inject distinct
oheimb@11345
   193
                  weak_case_congs cong_att =
haftmann@29579
   194
  PureThy.add_thmss [((Binding.name "simps", simps), []),
haftmann@29579
   195
    ((Binding.empty, flat case_thms @
haftmann@21045
   196
          flat distinct @ rec_thms), [Simplifier.simp_add]),
haftmann@29579
   197
    ((Binding.empty, rec_thms), [Code.add_default_eqn_attribute]),
haftmann@29579
   198
    ((Binding.empty, flat inject), [iff_add]),
haftmann@29579
   199
    ((Binding.empty, map (fn th => th RS notE) (flat distinct)), [Classical.safe_elim NONE]),
haftmann@29579
   200
    ((Binding.empty, weak_case_congs), [cong_att])]
haftmann@21127
   201
  #> snd;
oheimb@11345
   202
wenzelm@8437
   203
wenzelm@8437
   204
(* add_cases_induct *)
wenzelm@8306
   205
wenzelm@19874
   206
fun add_cases_induct infos induction thy =
wenzelm@8306
   207
  let
wenzelm@19874
   208
    val inducts = ProjectRule.projections (ProofContext.init thy) induction;
wenzelm@8405
   209
wenzelm@11805
   210
    fun named_rules (name, {index, exhaustion, ...}: datatype_info) =
haftmann@29579
   211
      [((Binding.empty, nth inducts index), [Induct.induct_type name]),
haftmann@29579
   212
       ((Binding.empty, exhaustion), [Induct.cases_type name])];
wenzelm@11805
   213
    fun unnamed_rule i =
haftmann@29579
   214
      ((Binding.empty, nth inducts i), [Thm.kind_internal, Induct.induct_type ""]);
wenzelm@18462
   215
  in
wenzelm@19874
   216
    thy |> PureThy.add_thms
haftmann@21045
   217
      (maps named_rules infos @
wenzelm@19874
   218
        map unnamed_rule (length infos upto length inducts - 1)) |> snd
haftmann@29579
   219
    |> PureThy.add_thmss [((Binding.name "inducts", inducts), [])] |> snd
wenzelm@18462
   220
  end;
wenzelm@8306
   221
berghofe@5177
   222
wenzelm@8405
   223
berghofe@7015
   224
(**** simplification procedure for showing distinctness of constructors ****)
berghofe@7015
   225
berghofe@7060
   226
fun stripT (i, Type ("fun", [_, T])) = stripT (i + 1, T)
berghofe@7060
   227
  | stripT p = p;
berghofe@7060
   228
berghofe@7060
   229
fun stripC (i, f $ x) = stripC (i + 1, f)
berghofe@7060
   230
  | stripC p = p;
berghofe@7060
   231
berghofe@7015
   232
val distinctN = "constr_distinct";
berghofe@7015
   233
haftmann@25537
   234
fun distinct_rule thy ss tname eq_t = case #distinct (the_datatype thy tname) of
berghofe@26533
   235
    FewConstrs thms => Goal.prove (Simplifier.the_context ss) [] [] eq_t (K
haftmann@25537
   236
      (EVERY [rtac eq_reflection 1, rtac iffI 1, rtac notE 1,
haftmann@25537
   237
        atac 2, resolve_tac thms 1, etac FalseE 1]))
haftmann@25537
   238
  | ManyConstrs (thm, simpset) =>
haftmann@25537
   239
      let
haftmann@25537
   240
        val [In0_inject, In1_inject, In0_not_In1, In1_not_In0] =
wenzelm@26343
   241
          map (PureThy.get_thm (ThyInfo.the_theory "Datatype" thy))
haftmann@25537
   242
            ["In0_inject", "In1_inject", "In0_not_In1", "In1_not_In0"];
haftmann@25537
   243
      in
haftmann@25537
   244
        Goal.prove (Simplifier.the_context ss) [] [] eq_t (K
haftmann@25537
   245
        (EVERY [rtac eq_reflection 1, rtac iffI 1, dtac thm 1,
haftmann@25537
   246
          full_simp_tac (Simplifier.inherit_context ss simpset) 1,
haftmann@25537
   247
          REPEAT (dresolve_tac [In0_inject, In1_inject] 1),
haftmann@25537
   248
          eresolve_tac [In0_not_In1 RS notE, In1_not_In0 RS notE] 1,
haftmann@25537
   249
          etac FalseE 1]))
haftmann@25537
   250
      end;
haftmann@25537
   251
wenzelm@19539
   252
fun distinct_proc thy ss (t as Const ("op =", _) $ t1 $ t2) =
berghofe@7060
   253
  (case (stripC (0, t1), stripC (0, t2)) of
berghofe@7060
   254
     ((i, Const (cname1, T1)), (j, Const (cname2, T2))) =>
berghofe@7060
   255
         (case (stripT (0, T1), stripT (0, T2)) of
berghofe@7060
   256
            ((i', Type (tname1, _)), (j', Type (tname2, _))) =>
berghofe@7060
   257
                if tname1 = tname2 andalso not (cname1 = cname2) andalso i = i' andalso j = j' then
wenzelm@19539
   258
                   (case (get_datatype_descr thy) tname1 of
haftmann@19346
   259
                      SOME (_, (_, constrs)) => let val cnames = map fst constrs
berghofe@7015
   260
                        in if cname1 mem cnames andalso cname2 mem cnames then
haftmann@25537
   261
                             SOME (distinct_rule thy ss tname1
haftmann@25537
   262
                               (Logic.mk_equals (t, Const ("False", HOLogic.boolT))))
skalberg@15531
   263
                           else NONE
berghofe@7015
   264
                        end
skalberg@15531
   265
                    | NONE => NONE)
skalberg@15531
   266
                else NONE
skalberg@15531
   267
          | _ => NONE)
skalberg@15531
   268
   | _ => NONE)
wenzelm@20054
   269
  | distinct_proc _ _ _ = NONE;
berghofe@7015
   270
wenzelm@13462
   271
val distinct_simproc =
wenzelm@29064
   272
  Simplifier.simproc @{theory HOL} distinctN ["s = t"] distinct_proc;
berghofe@7015
   273
berghofe@7015
   274
val dist_ss = HOL_ss addsimprocs [distinct_simproc];
berghofe@7015
   275
berghofe@7015
   276
val simproc_setup =
wenzelm@26496
   277
  Simplifier.map_simpset (fn ss => ss addsimprocs [distinct_simproc]);
berghofe@7015
   278
berghofe@7015
   279
berghofe@14799
   280
(**** translation rules for case ****)
berghofe@14799
   281
berghofe@22777
   282
fun make_case ctxt = DatatypeCase.make_case
berghofe@22777
   283
  (datatype_of_constr (ProofContext.theory_of ctxt)) ctxt;
berghofe@22777
   284
berghofe@22777
   285
fun strip_case ctxt = DatatypeCase.strip_case
berghofe@22777
   286
  (datatype_of_case (ProofContext.theory_of ctxt));
berghofe@14799
   287
berghofe@22777
   288
fun add_case_tr' case_names thy =
wenzelm@24712
   289
  Sign.add_advanced_trfuns ([], [],
wenzelm@24711
   290
    map (fn case_name =>
berghofe@22777
   291
      let val case_name' = Sign.const_syntax_name thy case_name
berghofe@22777
   292
      in (case_name', DatatypeCase.case_tr' datatype_of_case case_name')
berghofe@22777
   293
      end) case_names, []) thy;
berghofe@14799
   294
berghofe@14799
   295
val trfun_setup =
wenzelm@24712
   296
  Sign.add_advanced_trfuns ([],
nipkow@24349
   297
    [("_case_syntax", DatatypeCase.case_tr true datatype_of_constr)],
berghofe@22777
   298
    [], []);
berghofe@14799
   299
berghofe@14799
   300
berghofe@5177
   301
(* prepare types *)
berghofe@5177
   302
wenzelm@27277
   303
fun read_typ thy ((Ts, sorts), str) =
berghofe@5177
   304
  let
wenzelm@27277
   305
    val ctxt = ProofContext.init thy
wenzelm@27277
   306
      |> fold (Variable.declare_typ o TFree) sorts;
wenzelm@27277
   307
    val T = Syntax.read_typ ctxt str;
wenzelm@27277
   308
  in (Ts @ [T], Term.add_tfreesT T sorts) end;
berghofe@5177
   309
berghofe@5177
   310
fun cert_typ sign ((Ts, sorts), raw_T) =
berghofe@5177
   311
  let
berghofe@5177
   312
    val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle
berghofe@5177
   313
      TYPE (msg, _, _) => error msg;
wenzelm@27277
   314
    val sorts' = Term.add_tfreesT T sorts;
berghofe@5177
   315
  in (Ts @ [T],
wenzelm@18964
   316
      case duplicates (op =) (map fst sorts') of
berghofe@5177
   317
         [] => sorts'
berghofe@5177
   318
       | dups => error ("Inconsistent sort constraints for " ^ commas dups))
berghofe@5177
   319
  end;
berghofe@5177
   320
berghofe@5177
   321
berghofe@5177
   322
(**** make datatype info ****)
berghofe@5177
   323
berghofe@25677
   324
fun make_dt_info alt_names descr sorts induct reccomb_names rec_thms
wenzelm@10121
   325
    (((((((((i, (_, (tname, _, _))), case_name), case_thms),
wenzelm@10121
   326
      exhaustion_thm), distinct_thm), inject), nchotomy), case_cong), weak_case_cong) =
wenzelm@10121
   327
  (tname,
wenzelm@10121
   328
   {index = i,
berghofe@25677
   329
    alt_names = alt_names,
wenzelm@10121
   330
    descr = descr,
berghofe@18319
   331
    sorts = sorts,
wenzelm@10121
   332
    rec_names = reccomb_names,
wenzelm@10121
   333
    rec_rewrites = rec_thms,
wenzelm@10121
   334
    case_name = case_name,
wenzelm@10121
   335
    case_rewrites = case_thms,
wenzelm@10121
   336
    induction = induct,
wenzelm@10121
   337
    exhaustion = exhaustion_thm,
wenzelm@10121
   338
    distinct = distinct_thm,
wenzelm@10121
   339
    inject = inject,
wenzelm@10121
   340
    nchotomy = nchotomy,
wenzelm@10121
   341
    case_cong = case_cong,
wenzelm@10121
   342
    weak_case_cong = weak_case_cong});
berghofe@5177
   343
haftmann@31668
   344
type rules = {distinct : thm list list,
haftmann@31668
   345
  inject : thm list list,
haftmann@31668
   346
  exhaustion : thm list,
haftmann@31668
   347
  rec_thms : thm list,
haftmann@31668
   348
  case_thms : thm list list,
haftmann@31668
   349
  split_thms : (thm * thm) list,
haftmann@31668
   350
  induction : thm,
haftmann@31668
   351
  simps : thm list}
haftmann@31668
   352
haftmann@31668
   353
structure DatatypeInterpretation = InterpretationFun
haftmann@31668
   354
  (type T = datatype_config * string list val eq = eq_snd op =);
haftmann@31668
   355
fun interpretation f = DatatypeInterpretation.interpretation (uncurry f);
haftmann@24626
   356
berghofe@5177
   357
berghofe@5177
   358
(******************* definitional introduction of datatypes *******************)
berghofe@5177
   359
haftmann@31668
   360
fun add_datatype_def (config : datatype_config) new_type_names descr sorts types_syntax constr_syntax dt_info
wenzelm@8437
   361
    case_names_induct case_names_exhausts thy =
berghofe@5177
   362
  let
haftmann@31668
   363
    val _ = message config ("Proofs for datatype(s) " ^ commas_quote new_type_names);
berghofe@5177
   364
haftmann@18314
   365
    val ((inject, distinct, dist_rewrites, simproc_dists, induct), thy2) = thy |>
haftmann@31668
   366
      DatatypeRepProofs.representation_proofs config dt_info new_type_names descr sorts
wenzelm@8437
   367
        types_syntax constr_syntax case_names_induct;
berghofe@5177
   368
haftmann@31668
   369
    val (casedist_thms, thy3) = DatatypeAbsProofs.prove_casedist_thms config new_type_names descr
wenzelm@8437
   370
      sorts induct case_names_exhausts thy2;
haftmann@18314
   371
    val ((reccomb_names, rec_thms), thy4) = DatatypeAbsProofs.prove_primrec_thms
haftmann@31668
   372
      config new_type_names descr sorts dt_info inject dist_rewrites
wenzelm@20054
   373
      (Simplifier.theory_context thy3 dist_ss) induct thy3;
haftmann@18314
   374
    val ((case_thms, case_names), thy6) = DatatypeAbsProofs.prove_case_thms
haftmann@31668
   375
      config new_type_names descr sorts reccomb_names rec_thms thy4;
haftmann@31668
   376
    val (split_thms, thy7) = DatatypeAbsProofs.prove_split_thms config new_type_names
berghofe@5177
   377
      descr sorts inject dist_rewrites casedist_thms case_thms thy6;
haftmann@31668
   378
    val (nchotomys, thy8) = DatatypeAbsProofs.prove_nchotomys config new_type_names
berghofe@5177
   379
      descr sorts casedist_thms thy7;
haftmann@18314
   380
    val (case_congs, thy9) = DatatypeAbsProofs.prove_case_congs new_type_names
berghofe@5177
   381
      descr sorts nchotomys case_thms thy8;
haftmann@18314
   382
    val (weak_case_congs, thy10) = DatatypeAbsProofs.prove_weak_case_congs new_type_names
nipkow@8601
   383
      descr sorts thy9;
berghofe@5177
   384
berghofe@31361
   385
    val dt_infos = map
berghofe@31361
   386
      (make_dt_info (SOME new_type_names) (flat descr) sorts induct reccomb_names rec_thms)
berghofe@5177
   387
      ((0 upto length (hd descr) - 1) ~~ (hd descr) ~~ case_names ~~ case_thms ~~
wenzelm@10121
   388
        casedist_thms ~~ simproc_dists ~~ inject ~~ nchotomys ~~ case_congs ~~ weak_case_congs);
berghofe@5177
   389
haftmann@24699
   390
    val simps = flat (distinct @ inject @ case_thms) @ rec_thms;
berghofe@5177
   391
haftmann@18518
   392
    val thy12 =
haftmann@24699
   393
      thy10
berghofe@22777
   394
      |> add_case_tr' case_names
wenzelm@24712
   395
      |> Sign.add_path (space_implode "_" new_type_names)
haftmann@24699
   396
      |> add_rules simps case_thms rec_thms inject distinct
wenzelm@18728
   397
          weak_case_congs (Simplifier.attrib (op addcongs))
berghofe@22777
   398
      |> put_dt_infos dt_infos
haftmann@18518
   399
      |> add_cases_induct dt_infos induct
wenzelm@24712
   400
      |> Sign.parent_path
haftmann@18518
   401
      |> store_thmss "splits" new_type_names (map (fn (x, y) => [x, y]) split_thms) |> snd
haftmann@31668
   402
      |> DatatypeInterpretation.data (config, map fst dt_infos);
berghofe@5177
   403
  in
haftmann@18008
   404
    ({distinct = distinct,
berghofe@5177
   405
      inject = inject,
berghofe@5177
   406
      exhaustion = casedist_thms,
berghofe@5177
   407
      rec_thms = rec_thms,
berghofe@5177
   408
      case_thms = case_thms,
berghofe@5177
   409
      split_thms = split_thms,
berghofe@5177
   410
      induction = induct,
haftmann@18008
   411
      simps = simps}, thy12)
berghofe@5177
   412
  end;
berghofe@5177
   413
berghofe@5177
   414
wenzelm@6385
   415
(*********************** declare existing type as datatype *********************)
berghofe@5177
   416
haftmann@31668
   417
fun prove_rep_datatype (config : datatype_config) alt_names new_type_names descr sorts induct inject half_distinct thy =
berghofe@5177
   418
  let
haftmann@27104
   419
    val ((_, [induct']), _) =
haftmann@27104
   420
      Variable.importT_thms [induct] (Variable.thm_context induct);
berghofe@5177
   421
berghofe@5177
   422
    fun err t = error ("Ill-formed predicate in induction rule: " ^
haftmann@27104
   423
      Syntax.string_of_term_global thy t);
berghofe@5177
   424
berghofe@5177
   425
    fun get_typ (t as _ $ Var (_, Type (tname, Ts))) =
berghofe@25888
   426
          ((tname, map (fst o dest_TFree) Ts) handle TERM _ => err t)
berghofe@5177
   427
      | get_typ t = err t;
haftmann@27104
   428
    val dtnames = map get_typ (HOLogic.dest_conj (HOLogic.dest_Trueprop (Thm.concl_of induct')));
berghofe@5177
   429
haftmann@27104
   430
    val dt_info = get_datatypes thy;
berghofe@5177
   431
haftmann@27300
   432
    val distinct = (map o maps) (fn thm => [thm, thm RS not_sym]) half_distinct;
haftmann@21419
   433
    val (case_names_induct, case_names_exhausts) =
haftmann@21419
   434
      (mk_case_names_induct descr, mk_case_names_exhausts descr (map #1 dtnames));
wenzelm@8437
   435
haftmann@31668
   436
    val _ = message config ("Proofs for datatype(s) " ^ commas_quote new_type_names);
berghofe@5177
   437
haftmann@27104
   438
    val (casedist_thms, thy2) = thy |>
haftmann@31668
   439
      DatatypeAbsProofs.prove_casedist_thms config new_type_names [descr] sorts induct
wenzelm@8437
   440
        case_names_exhausts;
haftmann@18314
   441
    val ((reccomb_names, rec_thms), thy3) = DatatypeAbsProofs.prove_primrec_thms
haftmann@31668
   442
      config new_type_names [descr] sorts dt_info inject distinct
haftmann@27104
   443
      (Simplifier.theory_context thy2 dist_ss) induct thy2;
haftmann@31668
   444
    val ((case_thms, case_names), thy4) = DatatypeAbsProofs.prove_case_thms
haftmann@31668
   445
      config new_type_names [descr] sorts reccomb_names rec_thms thy3;
haftmann@18314
   446
    val (split_thms, thy5) = DatatypeAbsProofs.prove_split_thms
haftmann@31668
   447
      config new_type_names [descr] sorts inject distinct casedist_thms case_thms thy4;
haftmann@31668
   448
    val (nchotomys, thy6) = DatatypeAbsProofs.prove_nchotomys config new_type_names
berghofe@5177
   449
      [descr] sorts casedist_thms thy5;
haftmann@18314
   450
    val (case_congs, thy7) = DatatypeAbsProofs.prove_case_congs new_type_names
berghofe@5177
   451
      [descr] sorts nchotomys case_thms thy6;
haftmann@18314
   452
    val (weak_case_congs, thy8) = DatatypeAbsProofs.prove_weak_case_congs new_type_names
nipkow@8601
   453
      [descr] sorts thy7;
berghofe@5177
   454
haftmann@27104
   455
    val ((_, [induct']), thy10) =
haftmann@24699
   456
      thy8
haftmann@18377
   457
      |> store_thmss "inject" new_type_names inject
haftmann@18377
   458
      ||>> store_thmss "distinct" new_type_names distinct
wenzelm@24712
   459
      ||> Sign.add_path (space_implode "_" new_type_names)
haftmann@29579
   460
      ||>> PureThy.add_thms [((Binding.name "induct", induct), [case_names_induct])];
wenzelm@9149
   461
haftmann@27104
   462
    val dt_infos = map (make_dt_info alt_names descr sorts induct' reccomb_names rec_thms)
wenzelm@10121
   463
      ((0 upto length descr - 1) ~~ descr ~~ case_names ~~ case_thms ~~ casedist_thms ~~
wenzelm@10121
   464
        map FewConstrs distinct ~~ inject ~~ nchotomys ~~ case_congs ~~ weak_case_congs);
berghofe@5177
   465
haftmann@24699
   466
    val simps = flat (distinct @ inject @ case_thms) @ rec_thms;
berghofe@5177
   467
haftmann@19599
   468
    val thy11 =
haftmann@19599
   469
      thy10
berghofe@22777
   470
      |> add_case_tr' case_names
haftmann@24699
   471
      |> add_rules simps case_thms rec_thms inject distinct
haftmann@19599
   472
           weak_case_congs (Simplifier.attrib (op addcongs))
berghofe@22777
   473
      |> put_dt_infos dt_infos
haftmann@27104
   474
      |> add_cases_induct dt_infos induct'
wenzelm@24712
   475
      |> Sign.parent_path
haftmann@19599
   476
      |> store_thmss "splits" new_type_names (map (fn (x, y) => [x, y]) split_thms)
haftmann@19599
   477
      |> snd
haftmann@31668
   478
      |> DatatypeInterpretation.data (config, map fst dt_infos);
berghofe@5177
   479
  in
haftmann@18008
   480
    ({distinct = distinct,
berghofe@5177
   481
      inject = inject,
berghofe@5177
   482
      exhaustion = casedist_thms,
berghofe@5177
   483
      rec_thms = rec_thms,
berghofe@5177
   484
      case_thms = case_thms,
berghofe@5177
   485
      split_thms = split_thms,
haftmann@27104
   486
      induction = induct',
haftmann@18008
   487
      simps = simps}, thy11)
berghofe@5177
   488
  end;
berghofe@5177
   489
haftmann@31668
   490
fun gen_rep_datatype prep_term (config : datatype_config) after_qed alt_names raw_ts thy =
haftmann@27104
   491
  let
haftmann@27104
   492
    fun constr_of_term (Const (c, T)) = (c, T)
haftmann@27104
   493
      | constr_of_term t =
haftmann@27104
   494
          error ("Not a constant: " ^ Syntax.string_of_term_global thy t);
haftmann@27104
   495
    fun no_constr (c, T) = error ("Bad constructor: "
haftmann@27104
   496
      ^ Sign.extern_const thy c ^ "::"
haftmann@27104
   497
      ^ Syntax.string_of_typ_global thy T);
haftmann@27104
   498
    fun type_of_constr (cT as (_, T)) =
haftmann@27104
   499
      let
wenzelm@29270
   500
        val frees = OldTerm.typ_tfrees T;
haftmann@27104
   501
        val (tyco, vs) = ((apsnd o map) (dest_TFree) o dest_Type o snd o strip_type) T
haftmann@27104
   502
          handle TYPE _ => no_constr cT
haftmann@27104
   503
        val _ = if has_duplicates (eq_fst (op =)) vs then no_constr cT else ();
haftmann@27104
   504
        val _ = if length frees <> length vs then no_constr cT else ();
haftmann@27104
   505
      in (tyco, (vs, cT)) end;
haftmann@27104
   506
haftmann@27104
   507
    val raw_cs = AList.group (op =) (map (type_of_constr o constr_of_term o prep_term thy) raw_ts);
haftmann@27104
   508
    val _ = case map_filter (fn (tyco, _) =>
haftmann@27104
   509
        if Symtab.defined (get_datatypes thy) tyco then SOME tyco else NONE) raw_cs
haftmann@27104
   510
     of [] => ()
haftmann@27104
   511
      | tycos => error ("Type(s) " ^ commas (map quote tycos)
haftmann@27104
   512
          ^ " already represented inductivly");
haftmann@27104
   513
    val raw_vss = maps (map (map snd o fst) o snd) raw_cs;
haftmann@27104
   514
    val ms = case distinct (op =) (map length raw_vss)
haftmann@27104
   515
     of [n] => 0 upto n - 1
haftmann@27104
   516
      | _ => error ("Different types in given constructors");
haftmann@27104
   517
    fun inter_sort m = map (fn xs => nth xs m) raw_vss
haftmann@27104
   518
      |> Library.foldr1 (Sorts.inter_sort (Sign.classes_of thy))
haftmann@27104
   519
    val sorts = map inter_sort ms;
haftmann@27104
   520
    val vs = Name.names Name.context Name.aT sorts;
haftmann@27104
   521
haftmann@27104
   522
    fun norm_constr (raw_vs, (c, T)) = (c, map_atyps
haftmann@27104
   523
      (TFree o (the o AList.lookup (op =) (map fst raw_vs ~~ vs)) o fst o dest_TFree) T);
haftmann@27104
   524
haftmann@27104
   525
    val cs = map (apsnd (map norm_constr)) raw_cs;
haftmann@27104
   526
    val dtyps_of_typ = map (dtyp_of_typ (map (rpair (map fst vs) o fst) cs))
haftmann@27104
   527
      o fst o strip_type;
wenzelm@30364
   528
    val new_type_names = map Long_Name.base_name (the_default (map fst cs) alt_names);
haftmann@27104
   529
haftmann@27104
   530
    fun mk_spec (i, (tyco, constr)) = (i, (tyco,
haftmann@27104
   531
      map (DtTFree o fst) vs,
haftmann@27104
   532
      (map o apsnd) dtyps_of_typ constr))
haftmann@27104
   533
    val descr = map_index mk_spec cs;
haftmann@27104
   534
    val injs = DatatypeProp.make_injs [descr] vs;
haftmann@27300
   535
    val half_distincts = map snd (DatatypeProp.make_distincts [descr] vs);
haftmann@27104
   536
    val ind = DatatypeProp.make_ind [descr] vs;
haftmann@27300
   537
    val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts];
haftmann@27104
   538
haftmann@27104
   539
    fun after_qed' raw_thms =
haftmann@27104
   540
      let
haftmann@27300
   541
        val [[[induct]], injs, half_distincts] =
haftmann@27104
   542
          unflat rules (map Drule.zero_var_indexes_list raw_thms);
haftmann@27104
   543
            (*FIXME somehow dubious*)
haftmann@27104
   544
      in
haftmann@27104
   545
        ProofContext.theory_result
haftmann@31668
   546
          (prove_rep_datatype config alt_names new_type_names descr vs induct injs half_distincts)
haftmann@27104
   547
        #-> after_qed
haftmann@27104
   548
      end;
haftmann@27104
   549
  in
haftmann@27104
   550
    thy
haftmann@27104
   551
    |> ProofContext.init
haftmann@27104
   552
    |> Proof.theorem_i NONE after_qed' ((map o map) (rpair []) (flat rules))
haftmann@27104
   553
  end;
haftmann@27104
   554
haftmann@27104
   555
val rep_datatype = gen_rep_datatype Sign.cert_term;
haftmann@31668
   556
val rep_datatype_cmd = gen_rep_datatype Syntax.read_term_global default_datatype_config (K I);
wenzelm@6385
   557
berghofe@5177
   558
wenzelm@11958
   559
berghofe@5177
   560
(******************************** add datatype ********************************)
berghofe@5177
   561
haftmann@31668
   562
fun gen_add_datatype prep_typ (config : datatype_config) new_type_names dts thy =
berghofe@5177
   563
  let
wenzelm@20820
   564
    val _ = Theory.requires thy "Datatype" "datatype definitions";
berghofe@5177
   565
berghofe@5177
   566
    (* this theory is used just for parsing *)
berghofe@5177
   567
berghofe@5177
   568
    val tmp_thy = thy |>
wenzelm@5892
   569
      Theory.copy |>
wenzelm@24712
   570
      Sign.add_types (map (fn (tvs, tname, mx, _) =>
berghofe@5177
   571
        (tname, length tvs, mx)) dts);
berghofe@5177
   572
berghofe@5661
   573
    val (tyvars, _, _, _)::_ = dts;
berghofe@5177
   574
    val (new_dts, types_syntax) = ListPair.unzip (map (fn (tvs, tname, mx, _) =>
wenzelm@30345
   575
      let val full_tname = Sign.full_name tmp_thy (Binding.map_name (Syntax.type_name mx) tname)
wenzelm@18964
   576
      in (case duplicates (op =) tvs of
berghofe@5661
   577
            [] => if eq_set (tyvars, tvs) then ((full_tname, tvs), (tname, mx))
berghofe@5661
   578
                  else error ("Mutually recursive datatypes must have same type parameters")
wenzelm@30345
   579
          | dups => error ("Duplicate parameter(s) for datatype " ^ quote (Binding.str_of tname) ^
berghofe@5177
   580
              " : " ^ commas dups))
berghofe@5177
   581
      end) dts);
berghofe@5177
   582
wenzelm@18964
   583
    val _ = (case duplicates (op =) (map fst new_dts) @ duplicates (op =) new_type_names of
berghofe@5177
   584
      [] => () | dups => error ("Duplicate datatypes: " ^ commas dups));
berghofe@5177
   585
berghofe@31361
   586
    fun prep_dt_spec ((tvs, tname, mx, constrs), tname') (dts', constr_syntax, sorts, i) =
berghofe@5177
   587
      let
haftmann@21045
   588
        fun prep_constr (cname, cargs, mx') (constrs, constr_syntax', sorts') =
berghofe@5279
   589
          let
haftmann@20597
   590
            val (cargs', sorts'') = Library.foldl (prep_typ tmp_thy) (([], sorts'), cargs);
wenzelm@29270
   591
            val _ = (case fold (curry OldTerm.add_typ_tfree_names) cargs' [] \\ tvs of
berghofe@5279
   592
                [] => ()
berghofe@5279
   593
              | vs => error ("Extra type variables on rhs: " ^ commas vs))
haftmann@31668
   594
          in (constrs @ [((if #flat_names config then Sign.full_name tmp_thy else
berghofe@31361
   595
                Sign.full_name_path tmp_thy tname')
wenzelm@30345
   596
                  (Binding.map_name (Syntax.const_name mx') cname),
berghofe@5177
   597
                   map (dtyp_of_typ new_dts) cargs')],
berghofe@5177
   598
              constr_syntax' @ [(cname, mx')], sorts'')
wenzelm@30345
   599
          end handle ERROR msg => cat_error msg
wenzelm@30345
   600
           ("The error above occured in constructor " ^ quote (Binding.str_of cname) ^
wenzelm@30345
   601
            " of datatype " ^ quote (Binding.str_of tname));
berghofe@5177
   602
berghofe@5177
   603
        val (constrs', constr_syntax', sorts') =
haftmann@21045
   604
          fold prep_constr constrs ([], [], sorts)
berghofe@5177
   605
wenzelm@8405
   606
      in
wenzelm@18964
   607
        case duplicates (op =) (map fst constrs') of
berghofe@5177
   608
           [] =>
wenzelm@30345
   609
             (dts' @ [(i, (Sign.full_name tmp_thy (Binding.map_name (Syntax.type_name mx) tname),
berghofe@5177
   610
                map DtTFree tvs, constrs'))],
berghofe@5177
   611
              constr_syntax @ [constr_syntax'], sorts', i + 1)
berghofe@5177
   612
         | dups => error ("Duplicate constructors " ^ commas dups ^
wenzelm@30345
   613
             " in datatype " ^ quote (Binding.str_of tname))
berghofe@5177
   614
      end;
berghofe@5177
   615
berghofe@31361
   616
    val (dts', constr_syntax, sorts', i) =
berghofe@31361
   617
      fold prep_dt_spec (dts ~~ new_type_names) ([], [], [], 0);
haftmann@20597
   618
    val sorts = sorts' @ (map (rpair (Sign.defaultS tmp_thy)) (tyvars \\ map fst sorts'));
berghofe@5177
   619
    val dt_info = get_datatypes thy;
haftmann@20597
   620
    val (descr, _) = unfold_datatypes tmp_thy dts' sorts dt_info dts' i;
berghofe@14887
   621
    val _ = check_nonempty descr handle (exn as Datatype_Empty s) =>
haftmann@31668
   622
      if #strict config then error ("Nonemptiness check failed for datatype " ^ s)
berghofe@14887
   623
      else raise exn;
berghofe@5177
   624
haftmann@21045
   625
    val descr' = flat descr;
wenzelm@8437
   626
    val case_names_induct = mk_case_names_induct descr';
wenzelm@8437
   627
    val case_names_exhausts = mk_case_names_exhausts descr' (map #1 new_dts);
berghofe@5177
   628
  in
berghofe@26533
   629
    add_datatype_def
haftmann@31668
   630
      (config : datatype_config) new_type_names descr sorts types_syntax constr_syntax dt_info
wenzelm@8437
   631
      case_names_induct case_names_exhausts thy
berghofe@5177
   632
  end;
berghofe@5177
   633
haftmann@27104
   634
val add_datatype = gen_add_datatype cert_typ;
haftmann@31668
   635
val add_datatype_cmd = gen_add_datatype read_typ default_datatype_config;
berghofe@5177
   636
wenzelm@6360
   637
haftmann@18451
   638
wenzelm@6360
   639
(** package setup **)
wenzelm@6360
   640
wenzelm@6360
   641
(* setup theory *)
wenzelm@6360
   642
wenzelm@18708
   643
val setup =
haftmann@27002
   644
  DatatypeRepProofs.distinctness_limit_setup #>
wenzelm@24098
   645
  simproc_setup #>
haftmann@24626
   646
  trfun_setup #>
wenzelm@24711
   647
  DatatypeInterpretation.init;
wenzelm@6360
   648
wenzelm@6360
   649
wenzelm@6360
   650
(* outer syntax *)
wenzelm@6360
   651
wenzelm@17057
   652
local structure P = OuterParse and K = OuterKeyword in
wenzelm@6360
   653
wenzelm@6360
   654
val datatype_decl =
wenzelm@30345
   655
  Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- P.type_args -- P.binding -- P.opt_infix --
wenzelm@30345
   656
    (P.$$$ "=" |-- P.enum1 "|" (P.binding -- Scan.repeat P.typ -- P.opt_mixfix));
wenzelm@6360
   657
wenzelm@6360
   658
fun mk_datatype args =
wenzelm@6360
   659
  let
wenzelm@30345
   660
    val names = map
wenzelm@30345
   661
      (fn ((((NONE, _), t), _), _) => Binding.name_of t | ((((SOME t, _), _), _), _) => t) args;
wenzelm@12876
   662
    val specs = map (fn ((((_, vs), t), mx), cons) =>
wenzelm@12876
   663
      (vs, t, mx, map (fn ((x, y), z) => (x, y, z)) cons)) args;
haftmann@31668
   664
  in snd o add_datatype_cmd names specs end;
wenzelm@6360
   665
wenzelm@24867
   666
val _ =
wenzelm@6723
   667
  OuterSyntax.command "datatype" "define inductive datatypes" K.thy_decl
wenzelm@6723
   668
    (P.and_list1 datatype_decl >> (Toplevel.theory o mk_datatype));
wenzelm@6360
   669
wenzelm@24867
   670
val _ =
haftmann@27104
   671
  OuterSyntax.command "rep_datatype" "represent existing types inductively" K.thy_goal
haftmann@27104
   672
    (Scan.option (P.$$$ "(" |-- Scan.repeat1 P.name --| P.$$$ ")") -- Scan.repeat1 P.term
haftmann@27104
   673
      >> (fn (alt_names, ts) => Toplevel.print
haftmann@27104
   674
           o Toplevel.theory_to_proof (rep_datatype_cmd alt_names ts)));
wenzelm@6385
   675
wenzelm@30391
   676
end;
wenzelm@30391
   677
wenzelm@30391
   678
wenzelm@30391
   679
(* document antiquotation *)
wenzelm@30391
   680
wenzelm@30391
   681
val _ = ThyOutput.antiquotation "datatype" Args.tyname
wenzelm@30391
   682
  (fn {source = src, context = ctxt, ...} => fn dtco =>
wenzelm@30391
   683
    let
wenzelm@30391
   684
      val thy = ProofContext.theory_of ctxt;
wenzelm@30391
   685
      val (vs, cos) = the_datatype_spec thy dtco;
wenzelm@30391
   686
      val ty = Type (dtco, map TFree vs);
haftmann@30431
   687
      fun pretty_typ_bracket (ty as Type (_, _ :: _)) =
haftmann@30431
   688
            Pretty.enclose "(" ")" [Syntax.pretty_typ ctxt ty]
haftmann@30431
   689
        | pretty_typ_bracket ty =
haftmann@30431
   690
            Syntax.pretty_typ ctxt ty;
wenzelm@30391
   691
      fun pretty_constr (co, tys) =
wenzelm@30391
   692
        (Pretty.block o Pretty.breaks)
wenzelm@30391
   693
          (Syntax.pretty_term ctxt (Const (co, tys ---> ty)) ::
haftmann@30431
   694
            map pretty_typ_bracket tys);
wenzelm@30391
   695
      val pretty_datatype =
wenzelm@30391
   696
        Pretty.block
wenzelm@30391
   697
          (Pretty.command "datatype" :: Pretty.brk 1 ::
wenzelm@30391
   698
           Syntax.pretty_typ ctxt ty ::
wenzelm@30391
   699
           Pretty.str " =" :: Pretty.brk 1 ::
wenzelm@30391
   700
           flat (separate [Pretty.brk 1, Pretty.str "| "]
wenzelm@30391
   701
             (map (single o pretty_constr) cos)));
wenzelm@30391
   702
    in ThyOutput.output (ThyOutput.maybe_pretty_source (K pretty_datatype) src [()]) end);
haftmann@26111
   703
wenzelm@6385
   704
end;
wenzelm@6385
   705