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