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