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