src/ZF/Tools/datatype_package.ML
author wenzelm
Wed Apr 13 18:45:25 2005 +0200 (2005-04-13)
changeset 15705 b5edb9dcec9a
parent 15703 727ef1b8b3ee
child 17057 0934ac31985f
permissions -rw-r--r--
*** MESSAGE REFERS TO PREVIOUS VERSION ***
Attrib.src;
paulson@6065
     1
(*  Title:      ZF/Tools/datatype_package.ML
paulson@6052
     2
    ID:         $Id$
paulson@6052
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@6052
     4
    Copyright   1994  University of Cambridge
paulson@6052
     5
paulson@6065
     6
Datatype/Codatatype Definitions
paulson@6052
     7
paulson@6052
     8
The functor will be instantiated for normal sums/products (datatype defs)
paulson@6052
     9
                         and non-standard sums/products (codatatype defs)
paulson@6052
    10
paulson@6052
    11
Sums are used only for mutual recursion;
paulson@6052
    12
Products are used only to derive "streamlined" induction rules for relations
paulson@6052
    13
*)
paulson@6052
    14
paulson@6052
    15
type datatype_result =
paulson@6052
    16
   {con_defs   : thm list,             (*definitions made in thy*)
paulson@6052
    17
    case_eqns  : thm list,             (*equations for case operator*)
paulson@6052
    18
    recursor_eqns : thm list,          (*equations for the recursor*)
paulson@6052
    19
    free_iffs  : thm list,             (*freeness rewrite rules*)
paulson@6052
    20
    free_SEs   : thm list,             (*freeness destruct rules*)
paulson@6112
    21
    mk_free    : string -> thm};       (*function to make freeness theorems*)
paulson@6052
    22
paulson@6052
    23
signature DATATYPE_ARG =
wenzelm@12183
    24
sig
paulson@6052
    25
  val intrs : thm list
paulson@6052
    26
  val elims : thm list
wenzelm@12183
    27
end;
paulson@6052
    28
paulson@6052
    29
(*Functor's result signature*)
paulson@6052
    30
signature DATATYPE_PACKAGE =
wenzelm@12131
    31
sig
paulson@6052
    32
  (*Insert definitions for the recursive sets, which
paulson@6052
    33
     must *already* be declared as constants in parent theory!*)
wenzelm@12183
    34
  val add_datatype_i: term * term list -> Ind_Syntax.constructor_spec list list ->
wenzelm@12183
    35
    thm list * thm list * thm list -> theory -> theory * inductive_result * datatype_result
wenzelm@12183
    36
  val add_datatype_x: string * string list -> (string * string list * mixfix) list list ->
wenzelm@12183
    37
    thm list * thm list * thm list -> theory -> theory * inductive_result * datatype_result
wenzelm@12183
    38
  val add_datatype: string * string list -> (string * string list * mixfix) list list ->
wenzelm@15703
    39
    (thmref * Attrib.src list) list * (thmref * Attrib.src list) list *
wenzelm@15703
    40
    (thmref * Attrib.src list) list -> theory -> theory * inductive_result * datatype_result
wenzelm@12131
    41
end;
paulson@6052
    42
wenzelm@12131
    43
functor Add_datatype_def_Fun
wenzelm@12183
    44
 (structure Fp: FP and Pr : PR and CP: CARTPROD and Su : SU
wenzelm@12183
    45
  and Ind_Package : INDUCTIVE_PACKAGE
wenzelm@12183
    46
  and Datatype_Arg : DATATYPE_ARG
wenzelm@12183
    47
  val coind : bool): DATATYPE_PACKAGE =
paulson@6052
    48
struct
paulson@6052
    49
wenzelm@12183
    50
(*con_ty_lists specifies the constructors in the form (name, prems, mixfix) *)
paulson@6052
    51
wenzelm@12183
    52
fun add_datatype_i (dom_sum, rec_tms) con_ty_lists (monos, type_intrs, type_elims) thy =
paulson@6052
    53
 let
paulson@6052
    54
  val dummy = (*has essential ancestors?*)
paulson@6052
    55
    Theory.requires thy "Datatype" "(co)datatype definitions";
paulson@6052
    56
paulson@13150
    57
  val rec_hds = map head_of rec_tms;
paulson@13150
    58
paulson@13150
    59
  val dummy = assert_all is_Const rec_hds
paulson@13150
    60
          (fn t => "Datatype set not previously declared as constant: " ^
paulson@13150
    61
                   Sign.string_of_term (sign_of thy) t);
paulson@13150
    62
paulson@13150
    63
  val rec_names = map (#1 o dest_Const) rec_hds
paulson@6052
    64
  val rec_base_names = map Sign.base_name rec_names
paulson@6052
    65
  val big_rec_base_name = space_implode "_" rec_base_names
paulson@6052
    66
paulson@6052
    67
  val thy_path = thy |> Theory.add_path big_rec_base_name
paulson@6052
    68
  val sign = sign_of thy_path
paulson@6052
    69
paulson@6052
    70
  val big_rec_name = Sign.intern_const sign big_rec_base_name;
paulson@6052
    71
wenzelm@12131
    72
  val intr_tms = Ind_Syntax.mk_all_intr_tms sign (rec_tms, con_ty_lists);
paulson@6052
    73
wenzelm@12131
    74
  val dummy =
wenzelm@12243
    75
    writeln ((if coind then "Codatatype" else "Datatype") ^ " definition " ^ quote big_rec_name);
paulson@6052
    76
paulson@6052
    77
  val case_varname = "f";                (*name for case variables*)
paulson@6052
    78
paulson@6052
    79
  (** Define the constructors **)
paulson@6052
    80
paulson@6052
    81
  (*The empty tuple is 0*)
paulson@6052
    82
  fun mk_tuple [] = Const("0",iT)
wenzelm@7696
    83
    | mk_tuple args = foldr1 (fn (t1, t2) => Pr.pair $ t1 $ t2) args;
paulson@6052
    84
wenzelm@7696
    85
  fun mk_inject n k u = access_bal (fn t => Su.inl $ t, fn t => Su.inr $ t, u) n k;
paulson@6052
    86
paulson@6052
    87
  val npart = length rec_names;  (*number of mutually recursive parts*)
paulson@6052
    88
paulson@6052
    89
paulson@6052
    90
  val full_name = Sign.full_name sign;
paulson@6052
    91
wenzelm@12131
    92
  (*Make constructor definition;
paulson@6052
    93
    kpart is the number of this mutually recursive part*)
wenzelm@12131
    94
  fun mk_con_defs (kpart, con_ty_list) =
paulson@6052
    95
    let val ncon = length con_ty_list    (*number of constructors*)
wenzelm@12131
    96
        fun mk_def (((id,T,syn), name, args, prems), kcon) =
wenzelm@12131
    97
              (*kcon is index of constructor*)
wenzelm@12131
    98
            Logic.mk_defpair (list_comb (Const (full_name name, T), args),
wenzelm@12131
    99
                        mk_inject npart kpart
wenzelm@12131
   100
                        (mk_inject ncon kcon (mk_tuple args)))
paulson@6052
   101
    in  ListPair.map mk_def (con_ty_list, 1 upto ncon)  end;
paulson@6052
   102
paulson@6052
   103
paulson@6052
   104
  (*** Define the case operator ***)
paulson@6052
   105
paulson@6052
   106
  (*Combine split terms using case; yields the case operator for one part*)
wenzelm@12131
   107
  fun call_case case_list =
paulson@6052
   108
    let fun call_f (free,[]) = Abs("null", iT, free)
wenzelm@12131
   109
          | call_f (free,args) =
wenzelm@12131
   110
                CP.ap_split (foldr1 CP.mk_prod (map (#2 o dest_Free) args))
wenzelm@12131
   111
                            Ind_Syntax.iT
wenzelm@12131
   112
                            free
wenzelm@7696
   113
    in  fold_bal (fn (t1, t2) => Su.elim $ t1 $ t2) (map call_f case_list)  end;
paulson@6052
   114
paulson@6052
   115
  (** Generating function variables for the case definition
paulson@6052
   116
      Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
paulson@6052
   117
paulson@6052
   118
  (*The function variable for a single constructor*)
paulson@6052
   119
  fun add_case (((_, T, _), name, args, _), (opno, cases)) =
paulson@6052
   120
    if Syntax.is_identifier name then
paulson@6052
   121
      (opno, (Free (case_varname ^ "_" ^ name, T), args) :: cases)
paulson@6052
   122
    else
wenzelm@12131
   123
      (opno + 1, (Free (case_varname ^ "_op_" ^ string_of_int opno, T), args)
paulson@6052
   124
       :: cases);
paulson@6052
   125
paulson@6052
   126
  (*Treatment of a list of constructors, for one part
paulson@6052
   127
    Result adds a list of terms, each a function variable with arguments*)
paulson@6052
   128
  fun add_case_list (con_ty_list, (opno, case_lists)) =
skalberg@15574
   129
    let val (opno', case_list) = foldr add_case (opno, []) con_ty_list
paulson@6052
   130
    in (opno', case_list :: case_lists) end;
paulson@6052
   131
paulson@6052
   132
  (*Treatment of all parts*)
skalberg@15574
   133
  val (_, case_lists) = foldr add_case_list (1,[]) con_ty_lists;
paulson@6052
   134
paulson@6052
   135
  (*extract the types of all the variables*)
skalberg@15570
   136
  val case_typ = List.concat (map (map (#2 o #1)) con_ty_lists) ---> (iT-->iT);
paulson@6052
   137
paulson@6052
   138
  val case_base_name = big_rec_base_name ^ "_case";
paulson@6052
   139
  val case_name = full_name case_base_name;
paulson@6052
   140
paulson@6052
   141
  (*The list of all the function variables*)
skalberg@15570
   142
  val case_args = List.concat (map (map #1) case_lists);
paulson@6052
   143
wenzelm@12131
   144
  val case_const = Const (case_name, case_typ);
paulson@6052
   145
  val case_tm = list_comb (case_const, case_args);
paulson@6052
   146
paulson@6052
   147
  val case_def = Logic.mk_defpair
wenzelm@7696
   148
           (case_tm, fold_bal (fn (t1, t2) => Su.elim $ t1 $ t2) (map call_case case_lists));
paulson@6052
   149
paulson@6052
   150
paulson@6052
   151
  (** Generating function variables for the recursor definition
paulson@6052
   152
      Non-identifiers (e.g. infixes) get a name of the form f_op_nnn. **)
paulson@6052
   153
paulson@6052
   154
  (*a recursive call for x is the application rec`x  *)
paulson@6052
   155
  val rec_call = Ind_Syntax.apply_const $ Free ("rec", iT);
paulson@6052
   156
wenzelm@12131
   157
  (*look back down the "case args" (which have been reversed) to
paulson@6052
   158
    determine the de Bruijn index*)
paulson@6052
   159
  fun make_rec_call ([], _) arg = error
wenzelm@12131
   160
          "Internal error in datatype (variable name mismatch)"
wenzelm@12131
   161
    | make_rec_call (a::args, i) arg =
wenzelm@12131
   162
           if a = arg then rec_call $ Bound i
wenzelm@12131
   163
           else make_rec_call (args, i+1) arg;
paulson@6052
   164
paulson@6052
   165
  (*creates one case of the "X_case" definition of the recursor*)
wenzelm@12131
   166
  fun call_recursor ((case_var, case_args), (recursor_var, recursor_args)) =
paulson@6052
   167
      let fun add_abs (Free(a,T), u) = Abs(a,T,u)
wenzelm@12131
   168
          val ncase_args = length case_args
wenzelm@12131
   169
          val bound_args = map Bound ((ncase_args - 1) downto 0)
wenzelm@12131
   170
          val rec_args = map (make_rec_call (rev case_args,0))
wenzelm@12131
   171
                         (List.drop(recursor_args, ncase_args))
paulson@6052
   172
      in
skalberg@15574
   173
          foldr add_abs
skalberg@15574
   174
            (list_comb (recursor_var,
skalberg@15574
   175
                        bound_args @ rec_args)) case_args
paulson@6052
   176
      end
paulson@6052
   177
paulson@6052
   178
  (*Find each recursive argument and add a recursive call for it*)
paulson@6052
   179
  fun rec_args [] = []
paulson@6052
   180
    | rec_args ((Const("op :",_)$arg$X)::prems) =
paulson@6052
   181
       (case head_of X of
wenzelm@12131
   182
            Const(a,_) => (*recursive occurrence?*)
wenzelm@12131
   183
                          if a mem_string rec_names
wenzelm@12131
   184
                              then arg :: rec_args prems
wenzelm@12131
   185
                          else rec_args prems
wenzelm@12131
   186
          | _ => rec_args prems)
wenzelm@12131
   187
    | rec_args (_::prems) = rec_args prems;
paulson@6052
   188
paulson@6052
   189
  (*Add an argument position for each occurrence of a recursive set.
paulson@6052
   190
    Strictly speaking, the recursive arguments are the LAST of the function
paulson@6052
   191
    variable, but they all have type "i" anyway*)
paulson@6052
   192
  fun add_rec_args args' T = (map (fn _ => iT) args') ---> T
paulson@6052
   193
paulson@6052
   194
  (*Plug in the function variable type needed for the recursor
paulson@6052
   195
    as well as the new arguments (recursive calls)*)
paulson@6052
   196
  fun rec_ty_elem ((id, T, syn), name, args, prems) =
wenzelm@12131
   197
      let val args' = rec_args prems
wenzelm@12131
   198
      in ((id, add_rec_args args' T, syn),
wenzelm@12131
   199
          name, args @ args', prems)
paulson@6052
   200
      end;
paulson@6052
   201
wenzelm@12131
   202
  val rec_ty_lists = (map (map rec_ty_elem) con_ty_lists);
paulson@6052
   203
paulson@6052
   204
  (*Treatment of all parts*)
skalberg@15574
   205
  val (_, recursor_lists) = foldr add_case_list (1,[]) rec_ty_lists;
paulson@6052
   206
paulson@6052
   207
  (*extract the types of all the variables*)
skalberg@15570
   208
  val recursor_typ = List.concat (map (map (#2 o #1)) rec_ty_lists)
wenzelm@12131
   209
                         ---> (iT-->iT);
paulson@6052
   210
paulson@6052
   211
  val recursor_base_name = big_rec_base_name ^ "_rec";
paulson@6052
   212
  val recursor_name = full_name recursor_base_name;
paulson@6052
   213
paulson@6052
   214
  (*The list of all the function variables*)
skalberg@15570
   215
  val recursor_args = List.concat (map (map #1) recursor_lists);
paulson@6052
   216
paulson@6052
   217
  val recursor_tm =
wenzelm@12131
   218
    list_comb (Const (recursor_name, recursor_typ), recursor_args);
paulson@6052
   219
wenzelm@12131
   220
  val recursor_cases = map call_recursor
skalberg@15570
   221
                         (List.concat case_lists ~~ List.concat recursor_lists)
paulson@6052
   222
wenzelm@12131
   223
  val recursor_def =
paulson@6052
   224
      Logic.mk_defpair
wenzelm@12131
   225
        (recursor_tm,
wenzelm@12131
   226
         Ind_Syntax.Vrecursor_const $
wenzelm@12131
   227
           absfree ("rec", iT, list_comb (case_const, recursor_cases)));
paulson@6052
   228
paulson@6052
   229
  (* Build the new theory *)
paulson@6052
   230
wenzelm@12183
   231
  val need_recursor = (not coind andalso recursor_typ <> case_typ);
paulson@6052
   232
wenzelm@12131
   233
  fun add_recursor thy =
paulson@6052
   234
      if need_recursor then
wenzelm@12131
   235
           thy |> Theory.add_consts_i
wenzelm@12131
   236
                    [(recursor_base_name, recursor_typ, NoSyn)]
wenzelm@12131
   237
               |> (#1 o PureThy.add_defs_i false [Thm.no_attributes recursor_def])
paulson@6052
   238
      else thy;
paulson@6052
   239
wenzelm@8438
   240
  val (thy0, con_defs) = thy_path
wenzelm@12131
   241
             |> Theory.add_consts_i
wenzelm@12131
   242
                 ((case_base_name, case_typ, NoSyn) ::
skalberg@15570
   243
                  map #1 (List.concat con_ty_lists))
wenzelm@12131
   244
             |> PureThy.add_defs_i false
wenzelm@12131
   245
                 (map Thm.no_attributes
wenzelm@12131
   246
                  (case_def ::
skalberg@15570
   247
                   List.concat (ListPair.map mk_con_defs
wenzelm@12131
   248
                         (1 upto npart, con_ty_lists))))
wenzelm@12131
   249
             |>> add_recursor
wenzelm@12131
   250
             |>> Theory.parent_path
paulson@6052
   251
skalberg@15570
   252
  val intr_names = map #2 (List.concat con_ty_lists);
wenzelm@12131
   253
  val (thy1, ind_result) =
wenzelm@12187
   254
    thy0 |> Ind_Package.add_inductive_i
wenzelm@12187
   255
      false (rec_tms, dom_sum) (map Thm.no_attributes (intr_names ~~ intr_tms))
wenzelm@12187
   256
      (monos, con_defs, type_intrs @ Datatype_Arg.intrs, type_elims @ Datatype_Arg.elims);
paulson@6052
   257
paulson@6052
   258
  (**** Now prove the datatype theorems in this theory ****)
paulson@6052
   259
paulson@6052
   260
paulson@6052
   261
  (*** Prove the case theorems ***)
paulson@6052
   262
wenzelm@12131
   263
  (*Each equation has the form
paulson@6052
   264
    case(f_con1,...,f_conn)(coni(args)) = f_coni(args) *)
wenzelm@12131
   265
  fun mk_case_eqn (((_,T,_), name, args, _), case_free) =
paulson@6052
   266
    FOLogic.mk_Trueprop
paulson@6052
   267
      (FOLogic.mk_eq
paulson@6052
   268
       (case_tm $
wenzelm@12131
   269
         (list_comb (Const (Sign.intern_const (sign_of thy1) name,T),
wenzelm@12131
   270
                     args)),
wenzelm@12131
   271
        list_comb (case_free, args)));
paulson@6052
   272
paulson@6052
   273
  val case_trans = hd con_defs RS Ind_Syntax.def_trans
paulson@6052
   274
  and split_trans = Pr.split_eq RS meta_eq_to_obj_eq RS trans;
paulson@6052
   275
paulson@6052
   276
  (*Proves a single case equation.  Could use simp_tac, but it's slower!*)
wenzelm@12131
   277
  fun case_tacsf con_def _ =
paulson@6052
   278
    [rewtac con_def,
paulson@6052
   279
     rtac case_trans 1,
wenzelm@12131
   280
     REPEAT (resolve_tac [refl, split_trans,
wenzelm@12131
   281
                          Su.case_inl RS trans,
wenzelm@12131
   282
                          Su.case_inr RS trans] 1)];
paulson@6052
   283
paulson@6052
   284
  fun prove_case_eqn (arg,con_def) =
wenzelm@12131
   285
      prove_goalw_cterm []
wenzelm@12131
   286
        (Ind_Syntax.traceIt "next case equation = "
wenzelm@12131
   287
           (cterm_of (sign_of thy1) (mk_case_eqn arg)))
wenzelm@12131
   288
        (case_tacsf con_def);
paulson@6052
   289
wenzelm@12187
   290
  val free_iffs = map standard (con_defs RL [Ind_Syntax.def_swap_iff]);
paulson@6052
   291
wenzelm@12131
   292
  val case_eqns =
wenzelm@12131
   293
      map prove_case_eqn
skalberg@15570
   294
         (List.concat con_ty_lists ~~ case_args ~~ tl con_defs);
paulson@6052
   295
paulson@6052
   296
  (*** Prove the recursor theorems ***)
paulson@6052
   297
paulson@6052
   298
  val recursor_eqns = case try (get_def thy1) recursor_base_name of
skalberg@15531
   299
     NONE => (writeln "  [ No recursion operator ]";
wenzelm@12131
   300
              [])
skalberg@15531
   301
   | SOME recursor_def =>
paulson@6052
   302
      let
wenzelm@12131
   303
        (*Replace subterms rec`x (where rec is a Free var) by recursor_tm(x) *)
wenzelm@12131
   304
        fun subst_rec (Const("op `",_) $ Free _ $ arg) = recursor_tm $ arg
wenzelm@12131
   305
          | subst_rec tm =
wenzelm@12131
   306
              let val (head, args) = strip_comb tm
wenzelm@12131
   307
              in  list_comb (head, map subst_rec args)  end;
paulson@6052
   308
wenzelm@12131
   309
        (*Each equation has the form
wenzelm@12131
   310
          REC(coni(args)) = f_coni(args, REC(rec_arg), ...)
wenzelm@12131
   311
          where REC = recursor(f_con1,...,f_conn) and rec_arg is a recursive
wenzelm@12131
   312
          constructor argument.*)
wenzelm@12131
   313
        fun mk_recursor_eqn (((_,T,_), name, args, _), recursor_case) =
wenzelm@12131
   314
          FOLogic.mk_Trueprop
wenzelm@12131
   315
           (FOLogic.mk_eq
wenzelm@12131
   316
            (recursor_tm $
wenzelm@12131
   317
             (list_comb (Const (Sign.intern_const (sign_of thy1) name,T),
wenzelm@12131
   318
                         args)),
skalberg@15570
   319
             subst_rec (Library.foldl betapply (recursor_case, args))));
paulson@6052
   320
wenzelm@12131
   321
        val recursor_trans = recursor_def RS def_Vrecursor RS trans;
paulson@6052
   322
wenzelm@12131
   323
        (*Proves a single recursor equation.*)
wenzelm@12131
   324
        fun recursor_tacsf _ =
wenzelm@12131
   325
          [rtac recursor_trans 1,
wenzelm@12131
   326
           simp_tac (rank_ss addsimps case_eqns) 1,
wenzelm@12131
   327
           IF_UNSOLVED (simp_tac (rank_ss addsimps tl con_defs) 1)];
paulson@6052
   328
wenzelm@12131
   329
        fun prove_recursor_eqn arg =
wenzelm@12131
   330
            prove_goalw_cterm []
wenzelm@12131
   331
              (Ind_Syntax.traceIt "next recursor equation = "
wenzelm@12131
   332
                (cterm_of (sign_of thy1) (mk_recursor_eqn arg)))
wenzelm@12131
   333
              recursor_tacsf
paulson@6052
   334
      in
skalberg@15570
   335
         map prove_recursor_eqn (List.concat con_ty_lists ~~ recursor_cases)
paulson@6052
   336
      end
paulson@6052
   337
paulson@6052
   338
  val constructors =
paulson@6052
   339
      map (head_of o #1 o Logic.dest_equals o #prop o rep_thm) (tl con_defs);
paulson@6052
   340
wenzelm@12187
   341
  val free_SEs = map standard (Ind_Syntax.mk_free_SEs free_iffs);
paulson@6052
   342
paulson@6154
   343
  val {intrs, elim, induct, mutual_induct, ...} = ind_result
paulson@6052
   344
paulson@6052
   345
  (*Typical theorems have the form ~con1=con2, con1=con2==>False,
paulson@6052
   346
    con1(x)=con1(y) ==> x=y, con1(x)=con1(y) <-> x=y, etc.  *)
paulson@6052
   347
  fun mk_free s =
paulson@6052
   348
      prove_goalw (theory_of_thm elim)   (*Don't use thy1: it will be stale*)
paulson@6052
   349
                  con_defs s
wenzelm@12131
   350
        (fn prems => [cut_facts_tac prems 1,
wenzelm@12131
   351
                      fast_tac (ZF_cs addSEs free_SEs @ Su.free_SEs) 1]);
paulson@6052
   352
paulson@6052
   353
  val simps = case_eqns @ recursor_eqns;
paulson@6052
   354
paulson@6052
   355
  val dt_info =
wenzelm@12131
   356
        {inductive = true,
wenzelm@12131
   357
         constructors = constructors,
wenzelm@12131
   358
         rec_rewrites = recursor_eqns,
wenzelm@12131
   359
         case_rewrites = case_eqns,
wenzelm@12131
   360
         induct = induct,
wenzelm@12131
   361
         mutual_induct = mutual_induct,
wenzelm@12131
   362
         exhaustion = elim};
paulson@6052
   363
paulson@6052
   364
  val con_info =
paulson@6052
   365
        {big_rec_name = big_rec_name,
wenzelm@12131
   366
         constructors = constructors,
paulson@6052
   367
            (*let primrec handle definition by cases*)
wenzelm@12131
   368
         free_iffs = free_iffs,
wenzelm@12131
   369
         rec_rewrites = (case recursor_eqns of
wenzelm@12131
   370
                             [] => case_eqns | _ => recursor_eqns)};
paulson@6052
   371
paulson@6052
   372
  (*associate with each constructor the datatype name and rewrites*)
paulson@6052
   373
  val con_pairs = map (fn c => (#1 (dest_Const c), con_info)) constructors
paulson@6052
   374
paulson@6052
   375
 in
paulson@6052
   376
  (*Updating theory components: simprules and datatype info*)
paulson@6052
   377
  (thy1 |> Theory.add_path big_rec_base_name
wenzelm@12183
   378
        |> (#1 o PureThy.add_thmss
wenzelm@12183
   379
         [(("simps", simps), [Simplifier.simp_add_global]),
wenzelm@12187
   380
          (("", intrs), [Classical.safe_intro_global]),
wenzelm@12187
   381
          (("con_defs", con_defs), []),
wenzelm@12187
   382
          (("case_eqns", case_eqns), []),
wenzelm@12187
   383
          (("recursor_eqns", recursor_eqns), []),
wenzelm@12187
   384
          (("free_iffs", free_iffs), []),
wenzelm@12187
   385
          (("free_elims", free_SEs), [])])
wenzelm@12183
   386
        |> DatatypesData.map (fn tab => Symtab.update ((big_rec_name, dt_info), tab))
skalberg@15574
   387
        |> ConstructorsData.map (fn tab => foldr Symtab.update tab con_pairs)
wenzelm@12131
   388
        |> Theory.parent_path,
paulson@6052
   389
   ind_result,
paulson@6052
   390
   {con_defs = con_defs,
paulson@6052
   391
    case_eqns = case_eqns,
paulson@6052
   392
    recursor_eqns = recursor_eqns,
paulson@6052
   393
    free_iffs = free_iffs,
paulson@6052
   394
    free_SEs = free_SEs,
paulson@6052
   395
    mk_free = mk_free})
paulson@6052
   396
  end;
paulson@6052
   397
paulson@6052
   398
wenzelm@12183
   399
fun add_datatype_x (sdom, srec_tms) scon_ty_lists (monos, type_intrs, type_elims) thy =
wenzelm@12183
   400
  let
wenzelm@12183
   401
    val sign = sign_of thy;
wenzelm@12183
   402
    val read_i = Sign.simple_read_term sign Ind_Syntax.iT;
wenzelm@12183
   403
    val rec_tms = map read_i srec_tms;
wenzelm@12183
   404
    val con_ty_lists = Ind_Syntax.read_constructs sign scon_ty_lists
wenzelm@12183
   405
    val dom_sum =
wenzelm@12183
   406
      if sdom = "" then Ind_Syntax.data_domain coind (rec_tms, con_ty_lists)
wenzelm@12183
   407
      else read_i sdom;
wenzelm@12183
   408
  in add_datatype_i (dom_sum, rec_tms) con_ty_lists (monos, type_intrs, type_elims) thy end;
wenzelm@12183
   409
wenzelm@12183
   410
fun add_datatype (sdom, srec_tms) scon_ty_lists (raw_monos, raw_type_intrs, raw_type_elims) thy =
wenzelm@12183
   411
  let
wenzelm@12183
   412
    val (thy', ((monos, type_intrs), type_elims)) = thy
wenzelm@12183
   413
      |> IsarThy.apply_theorems raw_monos
wenzelm@12183
   414
      |>>> IsarThy.apply_theorems raw_type_intrs
wenzelm@12183
   415
      |>>> IsarThy.apply_theorems raw_type_elims;
wenzelm@12183
   416
  in add_datatype_x (sdom, srec_tms) scon_ty_lists (monos, type_intrs, type_elims) thy' end;
wenzelm@12183
   417
wenzelm@12183
   418
wenzelm@12183
   419
(* outer syntax *)
wenzelm@12183
   420
wenzelm@12183
   421
local structure P = OuterParse and K = OuterSyntax.Keyword in
wenzelm@12183
   422
wenzelm@12183
   423
fun mk_datatype ((((dom, dts), monos), type_intrs), type_elims) =
wenzelm@12183
   424
  #1 o add_datatype (dom, map fst dts) (map snd dts) (monos, type_intrs, type_elims);
wenzelm@12183
   425
wenzelm@12183
   426
val con_decl =
wenzelm@12183
   427
  P.name -- Scan.optional (P.$$$ "(" |-- P.list1 P.term --| P.$$$ ")") [] -- P.opt_mixfix
wenzelm@12876
   428
  >> P.triple1;
wenzelm@12183
   429
wenzelm@12183
   430
val datatype_decl =
wenzelm@12876
   431
  (Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<=") |-- P.!!! P.term) "") --
wenzelm@12183
   432
  P.and_list1 (P.term -- (P.$$$ "=" |-- P.enum1 "|" con_decl)) --
wenzelm@12876
   433
  Scan.optional (P.$$$ "monos" |-- P.!!! P.xthms1) [] --
wenzelm@12876
   434
  Scan.optional (P.$$$ "type_intros" |-- P.!!! P.xthms1) [] --
wenzelm@12876
   435
  Scan.optional (P.$$$ "type_elims" |-- P.!!! P.xthms1) []
wenzelm@12183
   436
  >> (Toplevel.theory o mk_datatype);
wenzelm@12183
   437
wenzelm@12183
   438
val coind_prefix = if coind then "co" else "";
wenzelm@12183
   439
wenzelm@12183
   440
val inductiveP = OuterSyntax.command (coind_prefix ^ "datatype")
wenzelm@12183
   441
  ("define " ^ coind_prefix ^ "datatype") K.thy_decl datatype_decl;
wenzelm@12183
   442
wenzelm@12183
   443
val _ = OuterSyntax.add_parsers [inductiveP];
paulson@6052
   444
paulson@6052
   445
end;
wenzelm@12183
   446
wenzelm@12183
   447
end;
wenzelm@15705
   448