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