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