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