src/HOL/Tools/datatype_package.ML
author berghofe
Wed Oct 21 17:55:18 1998 +0200 (1998-10-21)
changeset 5720 ace664b0c978
parent 5663 aad79a127628
child 5892 e5e44cc54eb2
permissions -rw-r--r--
Changed interface of rep_datatype: Characteristic theorems
of datatypes are now specified by xstrings.
berghofe@5177
     1
(*  Title:      HOL/Tools/datatype_package.ML
berghofe@5177
     2
    ID:         $Id$
berghofe@5177
     3
    Author:     Stefan Berghofer
berghofe@5177
     4
    Copyright   1998  TU Muenchen
berghofe@5177
     5
berghofe@5177
     6
Datatype package for Isabelle/HOL
berghofe@5177
     7
*)
berghofe@5177
     8
berghofe@5177
     9
signature DATATYPE_PACKAGE =
berghofe@5177
    10
sig
berghofe@5661
    11
  val quiet_mode : bool ref
berghofe@5661
    12
  val add_datatype : bool -> string list -> (string list * bstring * mixfix *
berghofe@5661
    13
    (bstring * string list * mixfix) list) list -> theory -> theory *
berghofe@5177
    14
      {distinct : thm list list,
berghofe@5177
    15
       inject : thm list list,
berghofe@5177
    16
       exhaustion : thm list,
berghofe@5177
    17
       rec_thms : thm list,
berghofe@5177
    18
       case_thms : thm list list,
berghofe@5177
    19
       split_thms : (thm * thm) list,
berghofe@5177
    20
       induction : thm,
berghofe@5177
    21
       size : thm list,
berghofe@5177
    22
       simps : thm list}
berghofe@5661
    23
  val add_datatype_i : bool -> string list -> (string list * bstring * mixfix *
berghofe@5661
    24
    (bstring * typ list * mixfix) list) list -> theory -> theory *
berghofe@5177
    25
      {distinct : thm list list,
berghofe@5177
    26
       inject : thm list list,
berghofe@5177
    27
       exhaustion : thm list,
berghofe@5177
    28
       rec_thms : thm list,
berghofe@5177
    29
       case_thms : thm list list,
berghofe@5177
    30
       split_thms : (thm * thm) list,
berghofe@5177
    31
       induction : thm,
berghofe@5177
    32
       size : thm list,
berghofe@5177
    33
       simps : thm list}
berghofe@5720
    34
  val rep_datatype : string list option -> xstring list list ->
berghofe@5720
    35
    xstring list list -> xstring -> theory -> theory *
berghofe@5720
    36
      {distinct : thm list list,
berghofe@5720
    37
       inject : thm list list,
berghofe@5720
    38
       exhaustion : thm list,
berghofe@5720
    39
       rec_thms : thm list,
berghofe@5720
    40
       case_thms : thm list list,
berghofe@5720
    41
       split_thms : (thm * thm) list,
berghofe@5720
    42
       induction : thm,
berghofe@5720
    43
       size : thm list,
berghofe@5720
    44
       simps : thm list}
berghofe@5720
    45
  val rep_datatype_i : string list option -> thm list list ->
berghofe@5177
    46
    thm list list -> thm -> theory -> theory *
berghofe@5177
    47
      {distinct : thm list list,
berghofe@5177
    48
       inject : thm list list,
berghofe@5177
    49
       exhaustion : thm list,
berghofe@5177
    50
       rec_thms : thm list,
berghofe@5177
    51
       case_thms : thm list list,
berghofe@5177
    52
       split_thms : (thm * thm) list,
berghofe@5177
    53
       induction : thm,
berghofe@5177
    54
       size : thm list,
berghofe@5177
    55
       simps : thm list}
berghofe@5177
    56
  val setup: (theory -> theory) list
berghofe@5177
    57
  val get_datatypes : theory -> DatatypeAux.datatype_info Symtab.table
berghofe@5177
    58
  val datatype_info_sg : Sign.sg -> string -> DatatypeAux.datatype_info
berghofe@5177
    59
  val datatype_info : theory -> string -> DatatypeAux.datatype_info
berghofe@5177
    60
  val constrs_of : theory -> string -> term list option
berghofe@5177
    61
  val case_const_of : theory -> string -> term option
berghofe@5177
    62
  val mutual_induct_tac : string list -> int -> tactic
berghofe@5177
    63
  val induct_tac : string -> int -> tactic
berghofe@5177
    64
  val exhaust_tac : string -> int -> tactic
berghofe@5177
    65
end;
berghofe@5177
    66
berghofe@5177
    67
structure DatatypePackage : DATATYPE_PACKAGE =
berghofe@5177
    68
struct
berghofe@5177
    69
berghofe@5177
    70
open DatatypeAux;
berghofe@5177
    71
berghofe@5661
    72
val quiet_mode = quiet_mode;
berghofe@5661
    73
berghofe@5177
    74
(* data kind 'HOL/datatypes' *)
berghofe@5177
    75
berghofe@5177
    76
structure DatatypesArgs =
berghofe@5177
    77
struct
berghofe@5177
    78
  val name = "HOL/datatypes";
berghofe@5177
    79
  type T = datatype_info Symtab.table;
berghofe@5177
    80
berghofe@5177
    81
  val empty = Symtab.empty;
berghofe@5177
    82
  val prep_ext = I;
berghofe@5177
    83
  val merge: T * T -> T = Symtab.merge (K true);
berghofe@5177
    84
berghofe@5177
    85
  fun print sg tab =
berghofe@5177
    86
    Pretty.writeln (Pretty.strs ("datatypes:" ::
berghofe@5177
    87
      map (Sign.cond_extern sg Sign.typeK o fst) (Symtab.dest tab)));
berghofe@5177
    88
end;
berghofe@5177
    89
berghofe@5177
    90
structure DatatypesData = TheoryDataFun(DatatypesArgs);
berghofe@5177
    91
val get_datatypes_sg = DatatypesData.get_sg;
berghofe@5177
    92
val get_datatypes = DatatypesData.get;
berghofe@5177
    93
val put_datatypes = DatatypesData.put;
berghofe@5177
    94
berghofe@5177
    95
(* setup *)
berghofe@5177
    96
berghofe@5177
    97
val setup = [DatatypesData.init];
berghofe@5177
    98
berghofe@5177
    99
(** theory information about datatypes **)
berghofe@5177
   100
berghofe@5177
   101
fun datatype_info_sg sg name =
berghofe@5177
   102
  (case Symtab.lookup (get_datatypes_sg sg, name) of
berghofe@5177
   103
    Some info => info
berghofe@5177
   104
  | None => error ("Unknown datatype " ^ quote name));
berghofe@5177
   105
berghofe@5177
   106
val datatype_info = datatype_info_sg o sign_of;
berghofe@5177
   107
berghofe@5177
   108
fun constrs_of thy tname =
berghofe@5177
   109
  let
berghofe@5177
   110
    val {index, descr, ...} = datatype_info thy tname;
berghofe@5177
   111
    val (_, _, constrs) = the (assoc (descr, index))
berghofe@5177
   112
  in
berghofe@5177
   113
    Some (map (fn (cname, _) =>
berghofe@5177
   114
      Const (cname, the (Sign.const_type (sign_of thy) cname))) constrs)
berghofe@5177
   115
  end handle _ => None;
berghofe@5177
   116
berghofe@5177
   117
fun case_const_of thy tname =
berghofe@5177
   118
  let
berghofe@5177
   119
    val {case_name, ...} = datatype_info thy tname;
berghofe@5177
   120
  in
berghofe@5177
   121
    Some (Const (case_name, the (Sign.const_type (sign_of thy) case_name)))
berghofe@5177
   122
  end handle _ => None;
berghofe@5177
   123
berghofe@5177
   124
fun find_tname var Bi =
berghofe@5177
   125
  let val frees = map dest_Free (term_frees Bi)
berghofe@5177
   126
      val params = Logic.strip_params Bi;
berghofe@5177
   127
  in case assoc (frees @ params, var) of
berghofe@5177
   128
       None => error ("No such variable in subgoal: " ^ quote var)
berghofe@5177
   129
     | Some(Type (tn, _)) => tn
berghofe@5177
   130
     | _ => error ("Cannot determine type of " ^ quote var)
berghofe@5177
   131
  end;
berghofe@5177
   132
berghofe@5177
   133
fun infer_tname state sign i aterm =
berghofe@5177
   134
  let
berghofe@5177
   135
    val (_, _, Bi, _) = dest_state (state, i)
berghofe@5177
   136
    val params = Logic.strip_params Bi;   (*params of subgoal i*)
berghofe@5177
   137
    val params = rev (rename_wrt_term Bi params);   (*as they are printed*)
berghofe@5177
   138
    val (types, sorts) = types_sorts state;
berghofe@5177
   139
    fun types' (a, ~1) = (case assoc (params, a) of None => types(a, ~1) | sm => sm)
berghofe@5177
   140
      | types' ixn = types ixn;
berghofe@5177
   141
    val (ct, _) = read_def_cterm (sign, types', sorts) [] false
berghofe@5177
   142
                                  (aterm, TVar (("", 0), []));
berghofe@5177
   143
  in case #T (rep_cterm ct) of
berghofe@5177
   144
       Type (tn, _) => tn
berghofe@5177
   145
     | _ => error ("Cannot determine type of " ^ quote aterm)
berghofe@5177
   146
  end;
berghofe@5177
   147
berghofe@5177
   148
(*Warn if the (induction) variable occurs Free among the premises, which
berghofe@5177
   149
  usually signals a mistake.  But calls the tactic either way!*)
berghofe@5177
   150
fun occs_in_prems tacf vars = 
berghofe@5177
   151
  SUBGOAL (fn (Bi, i) =>
berghofe@5177
   152
	   (if  exists (fn Free (a, _) => a mem vars)
berghofe@5177
   153
	              (foldr add_term_frees (#2 (strip_context Bi), []))
berghofe@5177
   154
	     then warning "Induction variable occurs also among premises!"
berghofe@5177
   155
	     else ();
berghofe@5177
   156
	    tacf i));
berghofe@5177
   157
berghofe@5177
   158
(* generic induction tactic for datatypes *)
berghofe@5177
   159
berghofe@5177
   160
fun mutual_induct_tac vars i state =
berghofe@5177
   161
  let
berghofe@5177
   162
    val (_, _, Bi, _) = dest_state (state, i);
berghofe@5177
   163
    val {sign, ...} = rep_thm state;
berghofe@5177
   164
    val tn = find_tname (hd vars) Bi;
berghofe@5177
   165
    val {induction, ...} = datatype_info_sg sign tn;
berghofe@5177
   166
    val ind_vnames = map (fn (_ $ Var (ixn, _)) =>
berghofe@5177
   167
      implode (tl (explode (Syntax.string_of_vname ixn))))
berghofe@5177
   168
        (dest_conj (HOLogic.dest_Trueprop (concl_of induction)));
berghofe@5177
   169
    val insts = (ind_vnames ~~ vars) handle _ =>
berghofe@5177
   170
      error ("Induction rule for type " ^ tn ^ " has different number of variables")
berghofe@5177
   171
  in
berghofe@5177
   172
    occs_in_prems (res_inst_tac insts induction) vars i state
berghofe@5177
   173
  end;
berghofe@5177
   174
berghofe@5177
   175
fun induct_tac var = mutual_induct_tac [var];
berghofe@5177
   176
berghofe@5177
   177
(* generic exhaustion tactic for datatypes *)
berghofe@5177
   178
berghofe@5177
   179
fun exhaust_tac aterm i state =
berghofe@5177
   180
  let
berghofe@5177
   181
    val {sign, ...} = rep_thm state;
berghofe@5177
   182
    val tn = infer_tname state sign i aterm;
berghofe@5177
   183
    val {exhaustion, ...} = datatype_info_sg sign tn;
berghofe@5177
   184
    val _ $ Var (ixn, _) $ _ = HOLogic.dest_Trueprop
berghofe@5177
   185
      (hd (Logic.strip_assums_hyp (hd (prems_of exhaustion))));
berghofe@5177
   186
    val exh_vname = implode (tl (explode (Syntax.string_of_vname ixn)))
berghofe@5177
   187
  in
berghofe@5177
   188
    res_inst_tac [(exh_vname, aterm)] exhaustion i state
berghofe@5177
   189
  end;
berghofe@5177
   190
berghofe@5177
   191
berghofe@5177
   192
(* prepare types *)
berghofe@5177
   193
berghofe@5177
   194
fun read_typ sign ((Ts, sorts), str) =
berghofe@5177
   195
  let
berghofe@5177
   196
    val T = Type.no_tvars (Sign.read_typ (sign, (curry assoc)
berghofe@5177
   197
      (map (apfst (rpair ~1)) sorts)) str) handle TYPE (msg, _, _) => error msg
berghofe@5177
   198
  in (Ts @ [T], add_typ_tfrees (T, sorts)) end;
berghofe@5177
   199
berghofe@5177
   200
fun cert_typ sign ((Ts, sorts), raw_T) =
berghofe@5177
   201
  let
berghofe@5177
   202
    val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle
berghofe@5177
   203
      TYPE (msg, _, _) => error msg;
berghofe@5177
   204
    val sorts' = add_typ_tfrees (T, sorts)
berghofe@5177
   205
  in (Ts @ [T],
berghofe@5177
   206
      case duplicates (map fst sorts') of
berghofe@5177
   207
         [] => sorts'
berghofe@5177
   208
       | dups => error ("Inconsistent sort constraints for " ^ commas dups))
berghofe@5177
   209
  end;
berghofe@5177
   210
berghofe@5177
   211
berghofe@5177
   212
(**** make datatype info ****)
berghofe@5177
   213
berghofe@5177
   214
fun make_dt_info descr induct reccomb_names rec_thms
berghofe@5177
   215
  ((((((((i, (_, (tname, _, _))), case_name), case_thms),
berghofe@5177
   216
    exhaustion_thm), distinct_thm), inject), nchotomy), case_cong) = (tname,
berghofe@5177
   217
      {index = i,
berghofe@5177
   218
       descr = descr,
berghofe@5177
   219
       rec_names = reccomb_names,
berghofe@5177
   220
       rec_rewrites = rec_thms,
berghofe@5177
   221
       case_name = case_name,
berghofe@5177
   222
       case_rewrites = case_thms,
berghofe@5177
   223
       induction = induct,
berghofe@5177
   224
       exhaustion = exhaustion_thm,
berghofe@5177
   225
       distinct = distinct_thm,
berghofe@5177
   226
       inject = inject,
berghofe@5177
   227
       nchotomy = nchotomy,
berghofe@5177
   228
       case_cong = case_cong});
berghofe@5177
   229
berghofe@5177
   230
fun store_clasimp thy (cla, simp) =
berghofe@5177
   231
  (claset_ref_of thy := cla; simpset_ref_of thy := simp);
berghofe@5177
   232
berghofe@5177
   233
infix 4 addDistinct;
berghofe@5177
   234
berghofe@5177
   235
fun clasimp addDistinct ([], _) = clasimp
berghofe@5177
   236
  | clasimp addDistinct (thms::thmss, (_, (_, _, constrs))::descr) =
berghofe@5177
   237
      if length constrs < DatatypeProp.dtK then
berghofe@5177
   238
        clasimp addIffs thms addDistinct (thmss, descr)
berghofe@5177
   239
      else
berghofe@5177
   240
        clasimp addsimps2 thms addDistinct (thmss, descr);
berghofe@5177
   241
berghofe@5177
   242
berghofe@5177
   243
(********************* axiomatic introduction of datatypes ********************)
berghofe@5177
   244
berghofe@5177
   245
fun add_and_get_axioms label tnames ts thy =
berghofe@5177
   246
  foldr (fn ((tname, t), (thy', axs)) =>
berghofe@5177
   247
    let
berghofe@5177
   248
      val thy'' = thy' |>
berghofe@5661
   249
        Theory.add_path tname |>
berghofe@5177
   250
        PureThy.add_axioms_i [((label, t), [])];
berghofe@5177
   251
      val ax = get_axiom thy'' label
berghofe@5661
   252
    in (Theory.parent_path thy'', ax::axs)
berghofe@5177
   253
    end) (tnames ~~ ts, (thy, []));
berghofe@5177
   254
berghofe@5177
   255
fun add_and_get_axiomss label tnames tss thy =
berghofe@5177
   256
  foldr (fn ((tname, ts), (thy', axss)) =>
berghofe@5177
   257
    let
berghofe@5177
   258
      val thy'' = thy' |>
berghofe@5661
   259
        Theory.add_path tname |>
berghofe@5177
   260
        PureThy.add_axiomss_i [((label, ts), [])];
berghofe@5177
   261
      val axs = PureThy.get_thms thy'' label
berghofe@5661
   262
    in (Theory.parent_path thy'', axs::axss)
berghofe@5177
   263
    end) (tnames ~~ tss, (thy, []));
berghofe@5177
   264
berghofe@5661
   265
fun add_datatype_axm flat_names new_type_names descr sorts types_syntax constr_syntax dt_info thy =
berghofe@5177
   266
  let
berghofe@5177
   267
    val descr' = flat descr;
berghofe@5177
   268
    val recTs = get_rec_types descr' sorts;
berghofe@5578
   269
    val used = foldr add_typ_tfree_names (recTs, []);
berghofe@5177
   270
    val newTs = take (length (hd descr), recTs);
berghofe@5177
   271
berghofe@5661
   272
    val _ = message ("Adding axioms for datatype(s) " ^ commas new_type_names);
berghofe@5177
   273
berghofe@5177
   274
    (**** declare new types and constants ****)
berghofe@5177
   275
berghofe@5177
   276
    val tyvars = map (fn (_, (_, Ts, _)) => map dest_DtTFree Ts) (hd descr);
berghofe@5177
   277
berghofe@5177
   278
    val constr_decls = map (fn (((_, (_, _, constrs)), T), constr_syntax') =>
berghofe@5177
   279
      map (fn ((_, cargs), (cname, mx)) =>
berghofe@5177
   280
        (cname, map (typ_of_dtyp descr' sorts) cargs ---> T, mx))
berghofe@5177
   281
          (constrs ~~ constr_syntax')) ((hd descr) ~~ newTs ~~ constr_syntax);
berghofe@5177
   282
berghofe@5578
   283
    val rec_result_Ts = map TFree (variantlist (replicate (length descr') "'t", used) ~~
berghofe@5578
   284
      replicate (length descr') HOLogic.termS);
berghofe@5177
   285
berghofe@5177
   286
    val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) =>
berghofe@5177
   287
      map (fn (_, cargs) =>
berghofe@5177
   288
        let
berghofe@5177
   289
          val recs = filter is_rec_type cargs;
berghofe@5177
   290
          val argTs = (map (typ_of_dtyp descr' sorts) cargs) @
berghofe@5177
   291
            (map (fn r => nth_elem (dest_DtRec r, rec_result_Ts)) recs)
berghofe@5177
   292
        in argTs ---> nth_elem (i, rec_result_Ts)
berghofe@5177
   293
        end) constrs) descr');
berghofe@5177
   294
berghofe@5177
   295
    val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
berghofe@5177
   296
    val reccomb_names = if length descr' = 1 then [big_reccomb_name] else
berghofe@5177
   297
      (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
berghofe@5177
   298
        (1 upto (length descr')));
berghofe@5177
   299
berghofe@5177
   300
    val big_size_name = space_implode "_" new_type_names ^ "_size";
berghofe@5177
   301
    val size_names = if length (flat (tl descr)) = 1 then [big_size_name] else
berghofe@5177
   302
      map (fn i => big_size_name ^ "_" ^ string_of_int i)
berghofe@5177
   303
        (1 upto length (flat (tl descr)));
berghofe@5177
   304
berghofe@5578
   305
    val freeT = TFree (variant used "'t", HOLogic.termS);
berghofe@5177
   306
    val case_fn_Ts = map (fn (i, (_, _, constrs)) =>
berghofe@5177
   307
      map (fn (_, cargs) =>
berghofe@5177
   308
        let val Ts = map (typ_of_dtyp descr' sorts) cargs
berghofe@5177
   309
        in Ts ---> freeT end) constrs) (hd descr);
berghofe@5177
   310
berghofe@5177
   311
    val case_names = map (fn s => (s ^ "_case")) new_type_names;
berghofe@5177
   312
berghofe@5177
   313
    val thy2 = thy |>
berghofe@5177
   314
berghofe@5177
   315
      (** new types **)
berghofe@5177
   316
berghofe@5177
   317
      curry (foldr (fn (((name, mx), tvs), thy') => thy' |>
berghofe@5177
   318
        PureThy.add_typedecls [(name, tvs, mx)] |>
berghofe@5177
   319
        Theory.add_arities_i
berghofe@5177
   320
          [(Sign.full_name (sign_of thy') (Syntax.type_name name mx),
berghofe@5177
   321
            replicate (length tvs) HOLogic.termS, HOLogic.termS)]))
berghofe@5177
   322
              (types_syntax ~~ tyvars) |>
berghofe@5177
   323
berghofe@5661
   324
      add_path flat_names (space_implode "_" new_type_names) |>
berghofe@5177
   325
berghofe@5177
   326
      (** primrec combinators **)
berghofe@5177
   327
berghofe@5177
   328
      Theory.add_consts_i (map (fn ((name, T), T') =>
berghofe@5177
   329
        (name, reccomb_fn_Ts @ [T] ---> T', NoSyn))
berghofe@5177
   330
          (reccomb_names ~~ recTs ~~ rec_result_Ts)) |>
berghofe@5177
   331
berghofe@5177
   332
      (** case combinators **)
berghofe@5177
   333
berghofe@5177
   334
      Theory.add_consts_i (map (fn ((name, T), Ts) =>
berghofe@5177
   335
        (name, Ts @ [T] ---> freeT, NoSyn))
berghofe@5177
   336
          (case_names ~~ newTs ~~ case_fn_Ts)) |>
berghofe@5177
   337
      Theory.add_trrules_i (DatatypeProp.make_case_trrules new_type_names descr) |>
berghofe@5177
   338
berghofe@5177
   339
      (** t_ord functions **)
berghofe@5177
   340
berghofe@5177
   341
      Theory.add_consts_i
berghofe@5177
   342
        (foldr (fn ((((_, (_, _, constrs)), tname), T), decls) =>
berghofe@5177
   343
          if length constrs < DatatypeProp.dtK then decls
berghofe@5177
   344
          else (tname ^ "_ord", T --> HOLogic.natT, NoSyn)::decls)
berghofe@5177
   345
            ((hd descr) ~~ new_type_names ~~ newTs, [])) |>
berghofe@5177
   346
berghofe@5177
   347
      (** size functions **)
berghofe@5177
   348
berghofe@5177
   349
      Theory.add_consts_i (map (fn (s, T) =>
berghofe@5177
   350
        (Sign.base_name s, T --> HOLogic.natT, NoSyn))
berghofe@5661
   351
          (size_names ~~ drop (length (hd descr), recTs))) |>
berghofe@5661
   352
berghofe@5661
   353
      (** constructors **)
berghofe@5661
   354
berghofe@5661
   355
      parent_path flat_names |>
berghofe@5661
   356
      curry (foldr (fn (((((_, (_, _, constrs)), T), tname),
berghofe@5661
   357
        constr_syntax'), thy') => thy' |>
berghofe@5661
   358
          add_path flat_names tname |>
berghofe@5661
   359
            Theory.add_consts_i (map (fn ((_, cargs), (cname, mx)) =>
berghofe@5661
   360
              (cname, map (typ_of_dtyp descr' sorts) cargs ---> T, mx))
berghofe@5661
   361
                (constrs ~~ constr_syntax')) |>
berghofe@5661
   362
          parent_path flat_names))
berghofe@5661
   363
            (hd descr ~~ newTs ~~ new_type_names ~~ constr_syntax);
berghofe@5177
   364
berghofe@5177
   365
    (**** introduction of axioms ****)
berghofe@5177
   366
berghofe@5661
   367
    val rec_axs = DatatypeProp.make_primrecs new_type_names descr sorts thy2;
berghofe@5661
   368
    val size_axs = DatatypeProp.make_size new_type_names descr sorts thy2;
berghofe@5661
   369
berghofe@5177
   370
    val (thy3, inject) = thy2 |>
berghofe@5661
   371
      Theory.add_path (space_implode "_" new_type_names) |>
berghofe@5177
   372
      PureThy.add_axioms_i [(("induct", DatatypeProp.make_ind descr sorts), [])] |>
berghofe@5661
   373
      PureThy.add_axiomss_i [(("recs", rec_axs), [])] |>
berghofe@5661
   374
      PureThy.add_axiomss_i [(("size", size_axs), [])] |>
berghofe@5661
   375
      Theory.parent_path |>
berghofe@5177
   376
      add_and_get_axiomss "inject" new_type_names
berghofe@5177
   377
        (DatatypeProp.make_injs descr sorts);
berghofe@5661
   378
    val induct = get_axiom thy3 "induct";
berghofe@5661
   379
    val rec_thms = get_thms thy3 "recs";
berghofe@5661
   380
    val size_thms = get_thms thy3 "size";
berghofe@5177
   381
    val (thy4, distinct) = add_and_get_axiomss "distinct" new_type_names
berghofe@5177
   382
      (DatatypeProp.make_distincts new_type_names descr sorts thy3) thy3;
berghofe@5177
   383
    val (thy5, exhaustion) = add_and_get_axioms "exhaust" new_type_names
berghofe@5661
   384
      (DatatypeProp.make_casedists descr sorts) thy4;
berghofe@5177
   385
    val (thy6, case_thms) = add_and_get_axiomss "cases" new_type_names
berghofe@5177
   386
      (DatatypeProp.make_cases new_type_names descr sorts thy5) thy5;
berghofe@5177
   387
    val (split_ts, split_asm_ts) = ListPair.unzip
berghofe@5177
   388
      (DatatypeProp.make_splits new_type_names descr sorts thy6);
berghofe@5177
   389
    val (thy7, split) = add_and_get_axioms "split" new_type_names split_ts thy6;
berghofe@5177
   390
    val (thy8, split_asm) = add_and_get_axioms "split_asm" new_type_names
berghofe@5177
   391
      split_asm_ts thy7;
berghofe@5177
   392
    val (thy9, nchotomys) = add_and_get_axioms "nchotomy" new_type_names
berghofe@5177
   393
      (DatatypeProp.make_nchotomys descr sorts) thy8;
berghofe@5177
   394
    val (thy10, case_congs) = add_and_get_axioms "case_cong" new_type_names
berghofe@5177
   395
      (DatatypeProp.make_case_congs new_type_names descr sorts thy9) thy9;
berghofe@5177
   396
    
berghofe@5177
   397
    val dt_infos = map (make_dt_info descr' induct reccomb_names rec_thms)
berghofe@5177
   398
      ((0 upto length (hd descr) - 1) ~~ (hd descr) ~~ case_names ~~ case_thms ~~
berghofe@5177
   399
        exhaustion ~~ distinct ~~ inject ~~ nchotomys ~~ case_congs);
berghofe@5177
   400
berghofe@5177
   401
    val simps = flat (distinct @ inject @ case_thms) @ size_thms @ rec_thms;
berghofe@5177
   402
berghofe@5661
   403
    val thy11 = thy10 |>
berghofe@5661
   404
      Theory.add_path (space_implode "_" new_type_names) |>
berghofe@5177
   405
      PureThy.add_tthmss [(("simps", map Attribute.tthm_of simps), [])] |>
berghofe@5177
   406
      put_datatypes (foldr Symtab.update (dt_infos, dt_info)) |>
berghofe@5177
   407
      Theory.parent_path;
berghofe@5177
   408
berghofe@5661
   409
    val _ = store_clasimp thy11 ((claset_of thy11, simpset_of thy11)
berghofe@5177
   410
      addsimps2 flat case_thms addsimps2 size_thms addsimps2 rec_thms
berghofe@5177
   411
      addIffs flat inject addDistinct (distinct, hd descr));
berghofe@5177
   412
berghofe@5177
   413
  in
berghofe@5661
   414
    (thy11,
berghofe@5177
   415
     {distinct = distinct,
berghofe@5177
   416
      inject = inject,
berghofe@5177
   417
      exhaustion = exhaustion,
berghofe@5177
   418
      rec_thms = rec_thms,
berghofe@5177
   419
      case_thms = case_thms,
berghofe@5177
   420
      split_thms = split ~~ split_asm,
berghofe@5177
   421
      induction = induct,
berghofe@5177
   422
      size = size_thms,
berghofe@5177
   423
      simps = simps})
berghofe@5177
   424
  end;
berghofe@5177
   425
berghofe@5177
   426
berghofe@5177
   427
(******************* definitional introduction of datatypes *******************)
berghofe@5177
   428
berghofe@5661
   429
fun add_datatype_def flat_names new_type_names descr sorts types_syntax constr_syntax dt_info thy =
berghofe@5177
   430
  let
berghofe@5661
   431
    val _ = message ("Proofs for datatype(s) " ^ commas new_type_names);
berghofe@5177
   432
berghofe@5177
   433
    val (thy2, inject, dist_rewrites, induct) = thy |>
berghofe@5661
   434
      DatatypeRepProofs.representation_proofs flat_names dt_info new_type_names descr sorts
berghofe@5177
   435
        types_syntax constr_syntax;
berghofe@5177
   436
berghofe@5177
   437
    val (thy3, casedist_thms) =
berghofe@5177
   438
      DatatypeAbsProofs.prove_casedist_thms new_type_names descr sorts induct thy2;
berghofe@5177
   439
    val (thy4, reccomb_names, rec_thms) = DatatypeAbsProofs.prove_primrec_thms
berghofe@5661
   440
      flat_names new_type_names descr sorts dt_info inject dist_rewrites induct thy3;
berghofe@5177
   441
    val (thy5, case_names, case_thms) = DatatypeAbsProofs.prove_case_thms
berghofe@5661
   442
      flat_names new_type_names descr sorts reccomb_names rec_thms thy4;
berghofe@5177
   443
    val (thy6, distinct) = DatatypeAbsProofs.prove_distinctness_thms
berghofe@5661
   444
      flat_names new_type_names descr sorts dist_rewrites case_thms thy5;
berghofe@5177
   445
    val (thy7, split_thms) = DatatypeAbsProofs.prove_split_thms new_type_names
berghofe@5177
   446
      descr sorts inject dist_rewrites casedist_thms case_thms thy6;
berghofe@5177
   447
    val (thy8, nchotomys) = DatatypeAbsProofs.prove_nchotomys new_type_names
berghofe@5177
   448
      descr sorts casedist_thms thy7;
berghofe@5177
   449
    val (thy9, case_congs) = DatatypeAbsProofs.prove_case_congs new_type_names
berghofe@5177
   450
      descr sorts nchotomys case_thms thy8;
berghofe@5661
   451
    val (thy10, size_thms) = DatatypeAbsProofs.prove_size_thms flat_names new_type_names
berghofe@5177
   452
      descr sorts reccomb_names rec_thms thy9;
berghofe@5177
   453
berghofe@5177
   454
    val dt_infos = map (make_dt_info (flat descr) induct reccomb_names rec_thms)
berghofe@5177
   455
      ((0 upto length (hd descr) - 1) ~~ (hd descr) ~~ case_names ~~ case_thms ~~
berghofe@5177
   456
        casedist_thms ~~ distinct ~~ inject ~~ nchotomys ~~ case_congs);
berghofe@5177
   457
berghofe@5177
   458
    val simps = flat (distinct @ inject @ case_thms) @ size_thms @ rec_thms;
berghofe@5177
   459
berghofe@5177
   460
    val thy11 = thy10 |>
berghofe@5661
   461
      Theory.add_path (space_implode "_" new_type_names) |>
berghofe@5177
   462
      PureThy.add_tthmss [(("simps", map Attribute.tthm_of simps), [])] |>
berghofe@5177
   463
      put_datatypes (foldr Symtab.update (dt_infos, dt_info)) |>
berghofe@5663
   464
      Theory.parent_path;
berghofe@5177
   465
berghofe@5177
   466
    val _ = store_clasimp thy11 ((claset_of thy11, simpset_of thy11)
berghofe@5177
   467
      addsimps2 flat case_thms addsimps2 size_thms addsimps2 rec_thms
berghofe@5177
   468
      addIffs flat inject addDistinct (distinct, hd descr));
berghofe@5177
   469
berghofe@5177
   470
  in
berghofe@5177
   471
    (thy11,
berghofe@5177
   472
     {distinct = distinct,
berghofe@5177
   473
      inject = inject,
berghofe@5177
   474
      exhaustion = casedist_thms,
berghofe@5177
   475
      rec_thms = rec_thms,
berghofe@5177
   476
      case_thms = case_thms,
berghofe@5177
   477
      split_thms = split_thms,
berghofe@5177
   478
      induction = induct,
berghofe@5177
   479
      size = size_thms,
berghofe@5177
   480
      simps = simps})
berghofe@5177
   481
  end;
berghofe@5177
   482
berghofe@5177
   483
berghofe@5177
   484
(*********************** declare non-datatype as datatype *********************)
berghofe@5177
   485
berghofe@5720
   486
fun gen_rep_datatype get get' alt_names distinct_names inject_names induction_name thy =
berghofe@5177
   487
  let
berghofe@5177
   488
    val sign = sign_of thy;
berghofe@5177
   489
berghofe@5720
   490
    val distinct = map (get thy) distinct_names;
berghofe@5720
   491
    val inject = map (get thy) inject_names;
berghofe@5720
   492
    val induction = get' thy induction_name;
berghofe@5720
   493
berghofe@5177
   494
    val induction' = freezeT induction;
berghofe@5177
   495
berghofe@5177
   496
    fun err t = error ("Ill-formed predicate in induction rule: " ^
berghofe@5177
   497
      Sign.string_of_term sign t);
berghofe@5177
   498
berghofe@5177
   499
    fun get_typ (t as _ $ Var (_, Type (tname, Ts))) =
berghofe@5177
   500
          ((tname, map dest_TFree Ts) handle _ => err t)
berghofe@5177
   501
      | get_typ t = err t;
berghofe@5177
   502
berghofe@5177
   503
    val dtnames = map get_typ (dest_conj (HOLogic.dest_Trueprop (concl_of induction')));
berghofe@5177
   504
    val new_type_names = if_none alt_names (map fst dtnames);
berghofe@5177
   505
berghofe@5177
   506
    fun get_constr t = (case Logic.strip_assums_concl t of
berghofe@5177
   507
        _ $ (_ $ t') => (case head_of t' of
berghofe@5177
   508
            Const (cname, cT) => (case strip_type cT of
berghofe@5177
   509
                (Ts, Type (tname, _)) => (tname, (cname, map (dtyp_of_typ dtnames) Ts))
berghofe@5177
   510
              | _ => err t)
berghofe@5177
   511
          | _ => err t)
berghofe@5177
   512
      | _ => err t);
berghofe@5177
   513
berghofe@5177
   514
    fun make_dt_spec [] _ _ = []
berghofe@5177
   515
      | make_dt_spec ((tname, tvs)::dtnames') i constrs =
berghofe@5177
   516
          let val (constrs', constrs'') = take_prefix (equal tname o fst) constrs
berghofe@5177
   517
          in (i, (tname, map DtTFree tvs, map snd constrs'))::
berghofe@5177
   518
            (make_dt_spec dtnames' (i + 1) constrs'')
berghofe@5177
   519
          end;
berghofe@5177
   520
berghofe@5177
   521
    val descr = make_dt_spec dtnames 0 (map get_constr (prems_of induction'));
berghofe@5177
   522
    val sorts = add_term_tfrees (concl_of induction', []);
berghofe@5177
   523
    val dt_info = get_datatypes thy;
berghofe@5177
   524
berghofe@5177
   525
    val _ = writeln ("Proofs for datatype(s) " ^ commas new_type_names);
berghofe@5177
   526
berghofe@5177
   527
    val (thy2, casedist_thms) = thy |>
berghofe@5177
   528
      DatatypeAbsProofs.prove_casedist_thms new_type_names [descr] sorts induction;
berghofe@5177
   529
    val (thy3, reccomb_names, rec_thms) = DatatypeAbsProofs.prove_primrec_thms
berghofe@5661
   530
      false new_type_names [descr] sorts dt_info inject distinct induction thy2;
berghofe@5661
   531
    val (thy4, case_names, case_thms) = DatatypeAbsProofs.prove_case_thms false
berghofe@5177
   532
      new_type_names [descr] sorts reccomb_names rec_thms thy3;
berghofe@5177
   533
    val (thy5, split_thms) = DatatypeAbsProofs.prove_split_thms
berghofe@5177
   534
      new_type_names [descr] sorts inject distinct casedist_thms case_thms thy4;
berghofe@5177
   535
    val (thy6, nchotomys) = DatatypeAbsProofs.prove_nchotomys new_type_names
berghofe@5177
   536
      [descr] sorts casedist_thms thy5;
berghofe@5177
   537
    val (thy7, case_congs) = DatatypeAbsProofs.prove_case_congs new_type_names
berghofe@5177
   538
      [descr] sorts nchotomys case_thms thy6;
berghofe@5177
   539
    val (thy8, size_thms) =
berghofe@5177
   540
      if exists (equal "Arith") (Sign.stamp_names_of (sign_of thy7)) then
berghofe@5661
   541
        DatatypeAbsProofs.prove_size_thms false new_type_names
berghofe@5177
   542
          [descr] sorts reccomb_names rec_thms thy7
berghofe@5177
   543
      else (thy7, []);
berghofe@5177
   544
berghofe@5177
   545
    val dt_infos = map (make_dt_info descr induction reccomb_names rec_thms)
berghofe@5177
   546
      ((0 upto length descr - 1) ~~ descr ~~ case_names ~~ case_thms ~~
berghofe@5177
   547
        casedist_thms ~~ distinct ~~ inject ~~ nchotomys ~~ case_congs);
berghofe@5177
   548
berghofe@5177
   549
    val simps = flat (distinct @ inject @ case_thms) @ size_thms @ rec_thms;
berghofe@5177
   550
berghofe@5177
   551
    val thy9 = thy8 |>
berghofe@5661
   552
      Theory.add_path (space_implode "_" new_type_names) |>
berghofe@5177
   553
      PureThy.add_tthmss [(("simps", map Attribute.tthm_of simps), [])] |>
berghofe@5177
   554
      put_datatypes (foldr Symtab.update (dt_infos, dt_info)) |>
berghofe@5177
   555
      Theory.parent_path;
berghofe@5177
   556
berghofe@5177
   557
    val _ = store_clasimp thy9 ((claset_of thy9, simpset_of thy9)
berghofe@5177
   558
      addsimps2 flat case_thms addsimps2 size_thms addsimps2 rec_thms
berghofe@5177
   559
      addIffs flat inject addDistinct (distinct, descr));
berghofe@5177
   560
berghofe@5177
   561
  in
berghofe@5177
   562
    (thy9,
berghofe@5177
   563
     {distinct = distinct,
berghofe@5177
   564
      inject = inject,
berghofe@5177
   565
      exhaustion = casedist_thms,
berghofe@5177
   566
      rec_thms = rec_thms,
berghofe@5177
   567
      case_thms = case_thms,
berghofe@5177
   568
      split_thms = split_thms,
berghofe@5177
   569
      induction = induction,
berghofe@5177
   570
      size = size_thms,
berghofe@5177
   571
      simps = simps})
berghofe@5177
   572
  end;
berghofe@5177
   573
berghofe@5720
   574
val rep_datatype = gen_rep_datatype
berghofe@5720
   575
  (fn thy => map Attribute.thm_of o PureThy.get_tthmss thy) get_thm;
berghofe@5720
   576
val rep_datatype_i = gen_rep_datatype (K I) (K I);
berghofe@5177
   577
berghofe@5177
   578
(******************************** add datatype ********************************)
berghofe@5177
   579
berghofe@5661
   580
fun gen_add_datatype prep_typ flat_names new_type_names dts thy =
berghofe@5177
   581
  let
berghofe@5177
   582
    val _ = Theory.requires thy "Datatype" "datatype definitions";
berghofe@5177
   583
berghofe@5177
   584
    (* this theory is used just for parsing *)
berghofe@5177
   585
berghofe@5177
   586
    val tmp_thy = thy |>
berghofe@5177
   587
      Theory.prep_ext |>
berghofe@5177
   588
      Theory.add_types (map (fn (tvs, tname, mx, _) =>
berghofe@5177
   589
        (tname, length tvs, mx)) dts);
berghofe@5177
   590
berghofe@5177
   591
    val sign = sign_of tmp_thy;
berghofe@5177
   592
berghofe@5661
   593
    val (tyvars, _, _, _)::_ = dts;
berghofe@5177
   594
    val (new_dts, types_syntax) = ListPair.unzip (map (fn (tvs, tname, mx, _) =>
berghofe@5177
   595
      let val full_tname = Sign.full_name sign (Syntax.type_name tname mx)
berghofe@5177
   596
      in (case duplicates tvs of
berghofe@5661
   597
            [] => if eq_set (tyvars, tvs) then ((full_tname, tvs), (tname, mx))
berghofe@5661
   598
                  else error ("Mutually recursive datatypes must have same type parameters")
berghofe@5177
   599
          | dups => error ("Duplicate parameter(s) for datatype " ^ full_tname ^
berghofe@5177
   600
              " : " ^ commas dups))
berghofe@5177
   601
      end) dts);
berghofe@5177
   602
berghofe@5177
   603
    val _ = (case duplicates (map fst new_dts) @ duplicates new_type_names of
berghofe@5177
   604
      [] => () | dups => error ("Duplicate datatypes: " ^ commas dups));
berghofe@5177
   605
berghofe@5177
   606
    fun prep_dt_spec ((dts', constr_syntax, sorts, i), (tvs, tname, mx, constrs)) =
berghofe@5177
   607
      let
berghofe@5661
   608
        fun prep_constr ((constrs, constr_syntax', sorts'), (cname, cargs, mx')) =
berghofe@5279
   609
          let
berghofe@5279
   610
            val (cargs', sorts'') = foldl (prep_typ sign) (([], sorts'), cargs);
berghofe@5279
   611
            val _ = (case foldr add_typ_tfree_names (cargs', []) \\ tvs of
berghofe@5279
   612
                [] => ()
berghofe@5279
   613
              | vs => error ("Extra type variables on rhs: " ^ commas vs))
berghofe@5661
   614
          in (constrs @ [((if flat_names then Sign.full_name sign else
berghofe@5661
   615
                Sign.full_name_path sign tname) (Syntax.const_name cname mx'),
berghofe@5177
   616
                   map (dtyp_of_typ new_dts) cargs')],
berghofe@5177
   617
              constr_syntax' @ [(cname, mx')], sorts'')
berghofe@5177
   618
          end handle ERROR =>
berghofe@5177
   619
            error ("The error above occured in constructor " ^ cname ^
berghofe@5177
   620
              " of datatype " ^ tname);
berghofe@5177
   621
berghofe@5177
   622
        val (constrs', constr_syntax', sorts') =
berghofe@5177
   623
          foldl prep_constr (([], [], sorts), constrs)
berghofe@5177
   624
berghofe@5177
   625
      in 
berghofe@5177
   626
        case duplicates (map fst constrs') of
berghofe@5177
   627
           [] =>
berghofe@5177
   628
             (dts' @ [(i, (Sign.full_name sign (Syntax.type_name tname mx),
berghofe@5177
   629
                map DtTFree tvs, constrs'))],
berghofe@5177
   630
              constr_syntax @ [constr_syntax'], sorts', i + 1)
berghofe@5177
   631
         | dups => error ("Duplicate constructors " ^ commas dups ^
berghofe@5177
   632
             " in datatype " ^ tname)
berghofe@5177
   633
      end;
berghofe@5177
   634
berghofe@5661
   635
    val (dts', constr_syntax, sorts', i) = foldl prep_dt_spec (([], [], [], 0), dts);
berghofe@5177
   636
    val dt_info = get_datatypes thy;
berghofe@5177
   637
    val (descr, _) = unfold_datatypes dt_info dts' i;
berghofe@5177
   638
    val _ = check_nonempty descr;
berghofe@5661
   639
    val sorts = sorts' @ (map (rpair (Sign.defaultS sign)) (tyvars \\ map fst sorts'));
berghofe@5177
   640
berghofe@5177
   641
  in
berghofe@5177
   642
    (if (!quick_and_dirty) then add_datatype_axm else add_datatype_def)
berghofe@5661
   643
      flat_names new_type_names descr sorts types_syntax constr_syntax dt_info thy
berghofe@5177
   644
  end;
berghofe@5177
   645
berghofe@5177
   646
val add_datatype_i = gen_add_datatype cert_typ;
berghofe@5177
   647
val add_datatype = gen_add_datatype read_typ;
berghofe@5177
   648
berghofe@5177
   649
end;
berghofe@5177
   650
berghofe@5177
   651
val induct_tac = DatatypePackage.induct_tac;
berghofe@5177
   652
val mutual_induct_tac = DatatypePackage.mutual_induct_tac;
berghofe@5177
   653
val exhaust_tac = DatatypePackage.exhaust_tac;