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