src/HOL/Tools/datatype_package.ML
author berghofe
Fri Mar 05 12:11:54 1999 +0100 (1999-03-05)
changeset 6305 4cbdb974220c
parent 6103 36f272ea9413
child 6360 83573ae0f22c
permissions -rw-r--r--
Fixed bug in add_datatype_axm:
Recursion and case combinators were assigned inconsistent names in
quick_and_dirty mode, which caused recdef etc. to crash.
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}
wenzelm@6103
    34
  val 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@6305
   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@6305
   326
      Theory.add_trrules_i (DatatypeProp.make_case_trrules new_type_names descr);
berghofe@6305
   327
berghofe@6305
   328
    val reccomb_names' = map (Sign.intern_const (sign_of thy2')) reccomb_names;
berghofe@6305
   329
    val case_names' = map (Sign.intern_const (sign_of thy2')) case_names;
berghofe@6305
   330
berghofe@6305
   331
    val thy2 = thy2' |>
berghofe@5177
   332
berghofe@5177
   333
      (** t_ord functions **)
berghofe@5177
   334
berghofe@5177
   335
      Theory.add_consts_i
berghofe@5177
   336
        (foldr (fn ((((_, (_, _, constrs)), tname), T), decls) =>
berghofe@5177
   337
          if length constrs < DatatypeProp.dtK then decls
berghofe@5177
   338
          else (tname ^ "_ord", T --> HOLogic.natT, NoSyn)::decls)
berghofe@5177
   339
            ((hd descr) ~~ new_type_names ~~ newTs, [])) |>
berghofe@5177
   340
berghofe@5177
   341
      (** size functions **)
berghofe@5177
   342
berghofe@5177
   343
      Theory.add_consts_i (map (fn (s, T) =>
berghofe@5177
   344
        (Sign.base_name s, T --> HOLogic.natT, NoSyn))
berghofe@5661
   345
          (size_names ~~ drop (length (hd descr), recTs))) |>
berghofe@5661
   346
berghofe@5661
   347
      (** constructors **)
berghofe@5661
   348
berghofe@5661
   349
      parent_path flat_names |>
berghofe@5661
   350
      curry (foldr (fn (((((_, (_, _, constrs)), T), tname),
berghofe@5661
   351
        constr_syntax'), thy') => thy' |>
berghofe@5661
   352
          add_path flat_names tname |>
berghofe@5661
   353
            Theory.add_consts_i (map (fn ((_, cargs), (cname, mx)) =>
berghofe@5661
   354
              (cname, map (typ_of_dtyp descr' sorts) cargs ---> T, mx))
berghofe@5661
   355
                (constrs ~~ constr_syntax')) |>
berghofe@5661
   356
          parent_path flat_names))
berghofe@5661
   357
            (hd descr ~~ newTs ~~ new_type_names ~~ constr_syntax);
berghofe@5177
   358
berghofe@5177
   359
    (**** introduction of axioms ****)
berghofe@5177
   360
berghofe@5661
   361
    val rec_axs = DatatypeProp.make_primrecs new_type_names descr sorts thy2;
berghofe@5661
   362
    val size_axs = DatatypeProp.make_size new_type_names descr sorts thy2;
berghofe@5661
   363
berghofe@5177
   364
    val (thy3, inject) = thy2 |>
berghofe@5661
   365
      Theory.add_path (space_implode "_" new_type_names) |>
berghofe@5177
   366
      PureThy.add_axioms_i [(("induct", DatatypeProp.make_ind descr sorts), [])] |>
berghofe@5661
   367
      PureThy.add_axiomss_i [(("recs", rec_axs), [])] |>
berghofe@5661
   368
      PureThy.add_axiomss_i [(("size", size_axs), [])] |>
berghofe@5661
   369
      Theory.parent_path |>
berghofe@5177
   370
      add_and_get_axiomss "inject" new_type_names
berghofe@5177
   371
        (DatatypeProp.make_injs descr sorts);
berghofe@5661
   372
    val induct = get_axiom thy3 "induct";
berghofe@5661
   373
    val rec_thms = get_thms thy3 "recs";
berghofe@5661
   374
    val size_thms = get_thms thy3 "size";
berghofe@5177
   375
    val (thy4, distinct) = add_and_get_axiomss "distinct" new_type_names
berghofe@5177
   376
      (DatatypeProp.make_distincts new_type_names descr sorts thy3) thy3;
berghofe@5177
   377
    val (thy5, exhaustion) = add_and_get_axioms "exhaust" new_type_names
berghofe@5661
   378
      (DatatypeProp.make_casedists descr sorts) thy4;
berghofe@5177
   379
    val (thy6, case_thms) = add_and_get_axiomss "cases" new_type_names
berghofe@5177
   380
      (DatatypeProp.make_cases new_type_names descr sorts thy5) thy5;
berghofe@5177
   381
    val (split_ts, split_asm_ts) = ListPair.unzip
berghofe@5177
   382
      (DatatypeProp.make_splits new_type_names descr sorts thy6);
berghofe@5177
   383
    val (thy7, split) = add_and_get_axioms "split" new_type_names split_ts thy6;
berghofe@5177
   384
    val (thy8, split_asm) = add_and_get_axioms "split_asm" new_type_names
berghofe@5177
   385
      split_asm_ts thy7;
berghofe@5177
   386
    val (thy9, nchotomys) = add_and_get_axioms "nchotomy" new_type_names
berghofe@5177
   387
      (DatatypeProp.make_nchotomys descr sorts) thy8;
berghofe@5177
   388
    val (thy10, case_congs) = add_and_get_axioms "case_cong" new_type_names
berghofe@5177
   389
      (DatatypeProp.make_case_congs new_type_names descr sorts thy9) thy9;
berghofe@5177
   390
    
berghofe@6305
   391
    val dt_infos = map (make_dt_info descr' induct reccomb_names' rec_thms)
berghofe@6305
   392
      ((0 upto length (hd descr) - 1) ~~ (hd descr) ~~ case_names' ~~ case_thms ~~
berghofe@5177
   393
        exhaustion ~~ distinct ~~ inject ~~ nchotomys ~~ case_congs);
berghofe@5177
   394
berghofe@5177
   395
    val simps = flat (distinct @ inject @ case_thms) @ size_thms @ rec_thms;
berghofe@5177
   396
berghofe@5661
   397
    val thy11 = thy10 |>
berghofe@5661
   398
      Theory.add_path (space_implode "_" new_type_names) |>
wenzelm@6092
   399
      PureThy.add_thmss [(("simps", simps), [])] |>
berghofe@5177
   400
      put_datatypes (foldr Symtab.update (dt_infos, dt_info)) |>
berghofe@5177
   401
      Theory.parent_path;
berghofe@5177
   402
berghofe@5661
   403
    val _ = store_clasimp thy11 ((claset_of thy11, simpset_of thy11)
berghofe@5177
   404
      addsimps2 flat case_thms addsimps2 size_thms addsimps2 rec_thms
berghofe@5177
   405
      addIffs flat inject addDistinct (distinct, hd descr));
berghofe@5177
   406
berghofe@5177
   407
  in
berghofe@5661
   408
    (thy11,
berghofe@5177
   409
     {distinct = distinct,
berghofe@5177
   410
      inject = inject,
berghofe@5177
   411
      exhaustion = exhaustion,
berghofe@5177
   412
      rec_thms = rec_thms,
berghofe@5177
   413
      case_thms = case_thms,
berghofe@5177
   414
      split_thms = split ~~ split_asm,
berghofe@5177
   415
      induction = induct,
berghofe@5177
   416
      size = size_thms,
berghofe@5177
   417
      simps = simps})
berghofe@5177
   418
  end;
berghofe@5177
   419
berghofe@5177
   420
berghofe@5177
   421
(******************* definitional introduction of datatypes *******************)
berghofe@5177
   422
berghofe@5661
   423
fun add_datatype_def flat_names new_type_names descr sorts types_syntax constr_syntax dt_info thy =
berghofe@5177
   424
  let
berghofe@5661
   425
    val _ = message ("Proofs for datatype(s) " ^ commas new_type_names);
berghofe@5177
   426
berghofe@5177
   427
    val (thy2, inject, dist_rewrites, induct) = thy |>
berghofe@5661
   428
      DatatypeRepProofs.representation_proofs flat_names dt_info new_type_names descr sorts
berghofe@5177
   429
        types_syntax constr_syntax;
berghofe@5177
   430
berghofe@5177
   431
    val (thy3, casedist_thms) =
berghofe@5177
   432
      DatatypeAbsProofs.prove_casedist_thms new_type_names descr sorts induct thy2;
berghofe@5177
   433
    val (thy4, reccomb_names, rec_thms) = DatatypeAbsProofs.prove_primrec_thms
berghofe@5661
   434
      flat_names new_type_names descr sorts dt_info inject dist_rewrites induct thy3;
berghofe@5177
   435
    val (thy5, case_names, case_thms) = DatatypeAbsProofs.prove_case_thms
berghofe@5661
   436
      flat_names new_type_names descr sorts reccomb_names rec_thms thy4;
berghofe@5177
   437
    val (thy6, distinct) = DatatypeAbsProofs.prove_distinctness_thms
berghofe@5661
   438
      flat_names new_type_names descr sorts dist_rewrites case_thms thy5;
berghofe@5177
   439
    val (thy7, split_thms) = DatatypeAbsProofs.prove_split_thms new_type_names
berghofe@5177
   440
      descr sorts inject dist_rewrites casedist_thms case_thms thy6;
berghofe@5177
   441
    val (thy8, nchotomys) = DatatypeAbsProofs.prove_nchotomys new_type_names
berghofe@5177
   442
      descr sorts casedist_thms thy7;
berghofe@5177
   443
    val (thy9, case_congs) = DatatypeAbsProofs.prove_case_congs new_type_names
berghofe@5177
   444
      descr sorts nchotomys case_thms thy8;
berghofe@5661
   445
    val (thy10, size_thms) = DatatypeAbsProofs.prove_size_thms flat_names new_type_names
berghofe@5177
   446
      descr sorts reccomb_names rec_thms thy9;
berghofe@5177
   447
berghofe@5177
   448
    val dt_infos = map (make_dt_info (flat descr) induct reccomb_names rec_thms)
berghofe@5177
   449
      ((0 upto length (hd descr) - 1) ~~ (hd descr) ~~ case_names ~~ case_thms ~~
berghofe@5177
   450
        casedist_thms ~~ distinct ~~ inject ~~ nchotomys ~~ case_congs);
berghofe@5177
   451
berghofe@5177
   452
    val simps = flat (distinct @ inject @ case_thms) @ size_thms @ rec_thms;
berghofe@5177
   453
berghofe@5177
   454
    val thy11 = thy10 |>
berghofe@5661
   455
      Theory.add_path (space_implode "_" new_type_names) |>
wenzelm@6092
   456
      PureThy.add_thmss [(("simps", simps), [])] |>
berghofe@5177
   457
      put_datatypes (foldr Symtab.update (dt_infos, dt_info)) |>
berghofe@5663
   458
      Theory.parent_path;
berghofe@5177
   459
berghofe@5177
   460
    val _ = store_clasimp thy11 ((claset_of thy11, simpset_of thy11)
berghofe@5177
   461
      addsimps2 flat case_thms addsimps2 size_thms addsimps2 rec_thms
berghofe@5177
   462
      addIffs flat inject addDistinct (distinct, hd descr));
berghofe@5177
   463
berghofe@5177
   464
  in
berghofe@5177
   465
    (thy11,
berghofe@5177
   466
     {distinct = distinct,
berghofe@5177
   467
      inject = inject,
berghofe@5177
   468
      exhaustion = casedist_thms,
berghofe@5177
   469
      rec_thms = rec_thms,
berghofe@5177
   470
      case_thms = case_thms,
berghofe@5177
   471
      split_thms = split_thms,
berghofe@5177
   472
      induction = induct,
berghofe@5177
   473
      size = size_thms,
berghofe@5177
   474
      simps = simps})
berghofe@5177
   475
  end;
berghofe@5177
   476
berghofe@5177
   477
berghofe@5177
   478
(*********************** declare non-datatype as datatype *********************)
berghofe@5177
   479
wenzelm@6103
   480
fun rep_datatype alt_names distinct inject induction thy =
berghofe@5177
   481
  let
berghofe@5177
   482
    val sign = sign_of thy;
berghofe@5177
   483
berghofe@5177
   484
    val induction' = freezeT induction;
berghofe@5177
   485
berghofe@5177
   486
    fun err t = error ("Ill-formed predicate in induction rule: " ^
berghofe@5177
   487
      Sign.string_of_term sign t);
berghofe@5177
   488
berghofe@5177
   489
    fun get_typ (t as _ $ Var (_, Type (tname, Ts))) =
berghofe@5177
   490
          ((tname, map dest_TFree Ts) handle _ => err t)
berghofe@5177
   491
      | get_typ t = err t;
berghofe@5177
   492
berghofe@5177
   493
    val dtnames = map get_typ (dest_conj (HOLogic.dest_Trueprop (concl_of induction')));
berghofe@5177
   494
    val new_type_names = if_none alt_names (map fst dtnames);
berghofe@5177
   495
berghofe@5177
   496
    fun get_constr t = (case Logic.strip_assums_concl t of
berghofe@5177
   497
        _ $ (_ $ t') => (case head_of t' of
berghofe@5177
   498
            Const (cname, cT) => (case strip_type cT of
berghofe@5177
   499
                (Ts, Type (tname, _)) => (tname, (cname, map (dtyp_of_typ dtnames) Ts))
berghofe@5177
   500
              | _ => err t)
berghofe@5177
   501
          | _ => err t)
berghofe@5177
   502
      | _ => err t);
berghofe@5177
   503
berghofe@5177
   504
    fun make_dt_spec [] _ _ = []
berghofe@5177
   505
      | make_dt_spec ((tname, tvs)::dtnames') i constrs =
berghofe@5177
   506
          let val (constrs', constrs'') = take_prefix (equal tname o fst) constrs
berghofe@5177
   507
          in (i, (tname, map DtTFree tvs, map snd constrs'))::
berghofe@5177
   508
            (make_dt_spec dtnames' (i + 1) constrs'')
berghofe@5177
   509
          end;
berghofe@5177
   510
berghofe@5177
   511
    val descr = make_dt_spec dtnames 0 (map get_constr (prems_of induction'));
berghofe@5177
   512
    val sorts = add_term_tfrees (concl_of induction', []);
berghofe@5177
   513
    val dt_info = get_datatypes thy;
berghofe@5177
   514
berghofe@5177
   515
    val _ = writeln ("Proofs for datatype(s) " ^ commas new_type_names);
berghofe@5177
   516
berghofe@5177
   517
    val (thy2, casedist_thms) = thy |>
berghofe@5177
   518
      DatatypeAbsProofs.prove_casedist_thms new_type_names [descr] sorts induction;
berghofe@5177
   519
    val (thy3, reccomb_names, rec_thms) = DatatypeAbsProofs.prove_primrec_thms
berghofe@5661
   520
      false new_type_names [descr] sorts dt_info inject distinct induction thy2;
berghofe@5661
   521
    val (thy4, case_names, case_thms) = DatatypeAbsProofs.prove_case_thms false
berghofe@5177
   522
      new_type_names [descr] sorts reccomb_names rec_thms thy3;
berghofe@5177
   523
    val (thy5, split_thms) = DatatypeAbsProofs.prove_split_thms
berghofe@5177
   524
      new_type_names [descr] sorts inject distinct casedist_thms case_thms thy4;
berghofe@5177
   525
    val (thy6, nchotomys) = DatatypeAbsProofs.prove_nchotomys new_type_names
berghofe@5177
   526
      [descr] sorts casedist_thms thy5;
berghofe@5177
   527
    val (thy7, case_congs) = DatatypeAbsProofs.prove_case_congs new_type_names
berghofe@5177
   528
      [descr] sorts nchotomys case_thms thy6;
berghofe@5177
   529
    val (thy8, size_thms) =
berghofe@5177
   530
      if exists (equal "Arith") (Sign.stamp_names_of (sign_of thy7)) then
berghofe@5661
   531
        DatatypeAbsProofs.prove_size_thms false new_type_names
berghofe@5177
   532
          [descr] sorts reccomb_names rec_thms thy7
berghofe@5177
   533
      else (thy7, []);
berghofe@5177
   534
berghofe@5177
   535
    val dt_infos = map (make_dt_info descr induction reccomb_names rec_thms)
berghofe@5177
   536
      ((0 upto length descr - 1) ~~ descr ~~ case_names ~~ case_thms ~~
berghofe@5177
   537
        casedist_thms ~~ distinct ~~ inject ~~ nchotomys ~~ case_congs);
berghofe@5177
   538
berghofe@5177
   539
    val simps = flat (distinct @ inject @ case_thms) @ size_thms @ rec_thms;
berghofe@5177
   540
berghofe@5177
   541
    val thy9 = thy8 |>
berghofe@5661
   542
      Theory.add_path (space_implode "_" new_type_names) |>
wenzelm@6092
   543
      PureThy.add_thmss [(("simps", simps), [])] |>
berghofe@5177
   544
      put_datatypes (foldr Symtab.update (dt_infos, dt_info)) |>
berghofe@5177
   545
      Theory.parent_path;
berghofe@5177
   546
berghofe@5177
   547
    val _ = store_clasimp thy9 ((claset_of thy9, simpset_of thy9)
berghofe@5177
   548
      addsimps2 flat case_thms addsimps2 size_thms addsimps2 rec_thms
berghofe@5177
   549
      addIffs flat inject addDistinct (distinct, descr));
berghofe@5177
   550
berghofe@5177
   551
  in
berghofe@5177
   552
    (thy9,
berghofe@5177
   553
     {distinct = distinct,
berghofe@5177
   554
      inject = inject,
berghofe@5177
   555
      exhaustion = casedist_thms,
berghofe@5177
   556
      rec_thms = rec_thms,
berghofe@5177
   557
      case_thms = case_thms,
berghofe@5177
   558
      split_thms = split_thms,
berghofe@5177
   559
      induction = induction,
berghofe@5177
   560
      size = size_thms,
berghofe@5177
   561
      simps = simps})
berghofe@5177
   562
  end;
berghofe@5177
   563
berghofe@5177
   564
berghofe@5177
   565
(******************************** add datatype ********************************)
berghofe@5177
   566
berghofe@5661
   567
fun gen_add_datatype prep_typ flat_names new_type_names dts thy =
berghofe@5177
   568
  let
berghofe@5177
   569
    val _ = Theory.requires thy "Datatype" "datatype definitions";
berghofe@5177
   570
berghofe@5177
   571
    (* this theory is used just for parsing *)
berghofe@5177
   572
berghofe@5177
   573
    val tmp_thy = thy |>
wenzelm@5892
   574
      Theory.copy |>
berghofe@5177
   575
      Theory.add_types (map (fn (tvs, tname, mx, _) =>
berghofe@5177
   576
        (tname, length tvs, mx)) dts);
berghofe@5177
   577
berghofe@5177
   578
    val sign = sign_of tmp_thy;
berghofe@5177
   579
berghofe@5661
   580
    val (tyvars, _, _, _)::_ = dts;
berghofe@5177
   581
    val (new_dts, types_syntax) = ListPair.unzip (map (fn (tvs, tname, mx, _) =>
berghofe@5177
   582
      let val full_tname = Sign.full_name sign (Syntax.type_name tname mx)
berghofe@5177
   583
      in (case duplicates tvs of
berghofe@5661
   584
            [] => if eq_set (tyvars, tvs) then ((full_tname, tvs), (tname, mx))
berghofe@5661
   585
                  else error ("Mutually recursive datatypes must have same type parameters")
berghofe@5177
   586
          | dups => error ("Duplicate parameter(s) for datatype " ^ full_tname ^
berghofe@5177
   587
              " : " ^ commas dups))
berghofe@5177
   588
      end) dts);
berghofe@5177
   589
berghofe@5177
   590
    val _ = (case duplicates (map fst new_dts) @ duplicates new_type_names of
berghofe@5177
   591
      [] => () | dups => error ("Duplicate datatypes: " ^ commas dups));
berghofe@5177
   592
berghofe@5177
   593
    fun prep_dt_spec ((dts', constr_syntax, sorts, i), (tvs, tname, mx, constrs)) =
berghofe@5177
   594
      let
berghofe@5661
   595
        fun prep_constr ((constrs, constr_syntax', sorts'), (cname, cargs, mx')) =
berghofe@5279
   596
          let
berghofe@5279
   597
            val (cargs', sorts'') = foldl (prep_typ sign) (([], sorts'), cargs);
berghofe@5279
   598
            val _ = (case foldr add_typ_tfree_names (cargs', []) \\ tvs of
berghofe@5279
   599
                [] => ()
berghofe@5279
   600
              | vs => error ("Extra type variables on rhs: " ^ commas vs))
berghofe@5661
   601
          in (constrs @ [((if flat_names then Sign.full_name sign else
berghofe@5661
   602
                Sign.full_name_path sign tname) (Syntax.const_name cname mx'),
berghofe@5177
   603
                   map (dtyp_of_typ new_dts) cargs')],
berghofe@5177
   604
              constr_syntax' @ [(cname, mx')], sorts'')
berghofe@5177
   605
          end handle ERROR =>
berghofe@5177
   606
            error ("The error above occured in constructor " ^ cname ^
berghofe@5177
   607
              " of datatype " ^ tname);
berghofe@5177
   608
berghofe@5177
   609
        val (constrs', constr_syntax', sorts') =
berghofe@5177
   610
          foldl prep_constr (([], [], sorts), constrs)
berghofe@5177
   611
berghofe@5177
   612
      in 
berghofe@5177
   613
        case duplicates (map fst constrs') of
berghofe@5177
   614
           [] =>
berghofe@5177
   615
             (dts' @ [(i, (Sign.full_name sign (Syntax.type_name tname mx),
berghofe@5177
   616
                map DtTFree tvs, constrs'))],
berghofe@5177
   617
              constr_syntax @ [constr_syntax'], sorts', i + 1)
berghofe@5177
   618
         | dups => error ("Duplicate constructors " ^ commas dups ^
berghofe@5177
   619
             " in datatype " ^ tname)
berghofe@5177
   620
      end;
berghofe@5177
   621
berghofe@5661
   622
    val (dts', constr_syntax, sorts', i) = foldl prep_dt_spec (([], [], [], 0), dts);
berghofe@5177
   623
    val dt_info = get_datatypes thy;
berghofe@5177
   624
    val (descr, _) = unfold_datatypes dt_info dts' i;
berghofe@5177
   625
    val _ = check_nonempty descr;
berghofe@5661
   626
    val sorts = sorts' @ (map (rpair (Sign.defaultS sign)) (tyvars \\ map fst sorts'));
berghofe@5177
   627
berghofe@5177
   628
  in
berghofe@5177
   629
    (if (!quick_and_dirty) then add_datatype_axm else add_datatype_def)
berghofe@5661
   630
      flat_names new_type_names descr sorts types_syntax constr_syntax dt_info thy
berghofe@5177
   631
  end;
berghofe@5177
   632
berghofe@5177
   633
val add_datatype_i = gen_add_datatype cert_typ;
berghofe@5177
   634
val add_datatype = gen_add_datatype read_typ;
berghofe@5177
   635
berghofe@5177
   636
end;
berghofe@5177
   637
berghofe@5177
   638
val induct_tac = DatatypePackage.induct_tac;
berghofe@5177
   639
val mutual_induct_tac = DatatypePackage.mutual_induct_tac;
berghofe@5177
   640
val exhaust_tac = DatatypePackage.exhaust_tac;