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