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