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