src/HOL/Tools/Datatype/datatype_data.ML
author wenzelm
Thu, 15 Dec 2011 18:08:40 +0100
changeset 45891 d73605c829cc
parent 45890 5f70aaecae26
child 54400 418a183fbe21
permissions -rw-r--r--
clarified module dependencies: Datatype_Data, Datatype_Case, Rep_Datatype;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33970
haftmann
parents: 33969
diff changeset
     1
(*  Title:      HOL/Tools/Datatype/datatype_data.ML
11539
0f17da240450 tuned headers;
wenzelm
parents: 11345
diff changeset
     2
    Author:     Stefan Berghofer, TU Muenchen
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     3
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
     4
Datatype package bookkeeping.
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     5
*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     6
33970
haftmann
parents: 33969
diff changeset
     7
signature DATATYPE_DATA =
6360
83573ae0f22c outer syntax for 'datatype';
wenzelm
parents: 6305
diff changeset
     8
sig
33970
haftmann
parents: 33969
diff changeset
     9
  include DATATYPE_COMMON
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    10
  val get_all : theory -> info Symtab.table
33970
haftmann
parents: 33969
diff changeset
    11
  val get_info : theory -> string -> info option
haftmann
parents: 33969
diff changeset
    12
  val the_info : theory -> string -> info
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    13
  val info_of_constr : theory -> string * typ -> info option
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    14
  val info_of_constr_permissive : theory -> string * typ -> info option
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    15
  val info_of_case : theory -> string -> info option
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    16
  val register: (string * info) list -> theory -> theory
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    17
  val the_spec : theory -> string -> (string * sort) list * (string * typ list) list
45701
615da8b8d758 discontinued obsolete datatype "alt_names";
wenzelm
parents: 45700
diff changeset
    18
  val the_descr : theory -> string list ->
615da8b8d758 discontinued obsolete datatype "alt_names";
wenzelm
parents: 45700
diff changeset
    19
    descr * (string * sort) list * string list * string *
615da8b8d758 discontinued obsolete datatype "alt_names";
wenzelm
parents: 45700
diff changeset
    20
    (string list * string list) * (typ list * typ list)
33970
haftmann
parents: 33969
diff changeset
    21
  val all_distincts : theory -> typ list -> thm list list
haftmann
parents: 33969
diff changeset
    22
  val get_constrs : theory -> string -> (string * typ) list option
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    23
  val mk_case_names_induct: descr -> attribute
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    24
  val mk_case_names_exhausts: descr -> string list -> attribute list
33970
haftmann
parents: 33969
diff changeset
    25
  val interpretation : (config -> string list -> theory -> theory) -> theory -> theory
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    26
  val interpretation_data : config * string list -> theory -> theory
33970
haftmann
parents: 33969
diff changeset
    27
  val setup: theory -> theory
32717
0e787c721fa3 re-established reasonable inner outline for module
haftmann
parents: 32712
diff changeset
    28
end;
0e787c721fa3 re-established reasonable inner outline for module
haftmann
parents: 32712
diff changeset
    29
33970
haftmann
parents: 33969
diff changeset
    30
structure Datatype_Data: DATATYPE_DATA =
33963
977b94b64905 renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33959
diff changeset
    31
struct
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    32
33970
haftmann
parents: 33969
diff changeset
    33
(** theory data **)
haftmann
parents: 33969
diff changeset
    34
haftmann
parents: 33969
diff changeset
    35
(* data management *)
haftmann
parents: 33969
diff changeset
    36
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    37
structure Data = Theory_Data
33970
haftmann
parents: 33969
diff changeset
    38
(
haftmann
parents: 33969
diff changeset
    39
  type T =
41423
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
    40
    {types: Datatype_Aux.info Symtab.table,
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
    41
     constrs: (string * Datatype_Aux.info) list Symtab.table,
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
    42
     cases: Datatype_Aux.info Symtab.table};
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
    43
33970
haftmann
parents: 33969
diff changeset
    44
  val empty =
haftmann
parents: 33969
diff changeset
    45
    {types = Symtab.empty, constrs = Symtab.empty, cases = Symtab.empty};
haftmann
parents: 33969
diff changeset
    46
  val extend = I;
haftmann
parents: 33969
diff changeset
    47
  fun merge
haftmann
parents: 33969
diff changeset
    48
    ({types = types1, constrs = constrs1, cases = cases1},
haftmann
parents: 33969
diff changeset
    49
     {types = types2, constrs = constrs2, cases = cases2}) : T =
haftmann
parents: 33969
diff changeset
    50
    {types = Symtab.merge (K true) (types1, types2),
haftmann
parents: 33969
diff changeset
    51
     constrs = Symtab.join (K (AList.merge (op =) (K true))) (constrs1, constrs2),
haftmann
parents: 33969
diff changeset
    52
     cases = Symtab.merge (K true) (cases1, cases2)};
haftmann
parents: 33969
diff changeset
    53
);
haftmann
parents: 33969
diff changeset
    54
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    55
val get_all = #types o Data.get;
33970
haftmann
parents: 33969
diff changeset
    56
val get_info = Symtab.lookup o get_all;
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
    57
33970
haftmann
parents: 33969
diff changeset
    58
fun the_info thy name =
haftmann
parents: 33969
diff changeset
    59
  (case get_info thy name of
haftmann
parents: 33969
diff changeset
    60
    SOME info => info
haftmann
parents: 33969
diff changeset
    61
  | NONE => error ("Unknown datatype " ^ quote name));
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
    62
33970
haftmann
parents: 33969
diff changeset
    63
fun info_of_constr thy (c, T) =
haftmann
parents: 33969
diff changeset
    64
  let
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    65
    val tab = Symtab.lookup_list (#constrs (Data.get thy)) c;
43580
023a1d1f97bd new Datatype.info_of_constr with strict behaviour wrt. to overloaded constructors -- side effect: function package correctly identifies 0::int as a non-constructor;
krauss
parents: 43329
diff changeset
    66
  in
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    67
    (case body_type T of
43580
023a1d1f97bd new Datatype.info_of_constr with strict behaviour wrt. to overloaded constructors -- side effect: function package correctly identifies 0::int as a non-constructor;
krauss
parents: 43329
diff changeset
    68
      Type (tyco, _) => AList.lookup (op =) tab tyco
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    69
    | _ => NONE)
43580
023a1d1f97bd new Datatype.info_of_constr with strict behaviour wrt. to overloaded constructors -- side effect: function package correctly identifies 0::int as a non-constructor;
krauss
parents: 43329
diff changeset
    70
  end;
023a1d1f97bd new Datatype.info_of_constr with strict behaviour wrt. to overloaded constructors -- side effect: function package correctly identifies 0::int as a non-constructor;
krauss
parents: 43329
diff changeset
    71
023a1d1f97bd new Datatype.info_of_constr with strict behaviour wrt. to overloaded constructors -- side effect: function package correctly identifies 0::int as a non-constructor;
krauss
parents: 43329
diff changeset
    72
fun info_of_constr_permissive thy (c, T) =
023a1d1f97bd new Datatype.info_of_constr with strict behaviour wrt. to overloaded constructors -- side effect: function package correctly identifies 0::int as a non-constructor;
krauss
parents: 43329
diff changeset
    73
  let
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    74
    val tab = Symtab.lookup_list (#constrs (Data.get thy)) c;
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    75
    val hint = (case body_type T of Type (tyco, _) => SOME tyco | _ => NONE);
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    76
    val default = if null tab then NONE else SOME (snd (List.last tab));
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    77
    (*conservative wrt. overloaded constructors*)
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    78
  in
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    79
    (case hint of
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    80
      NONE => default
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    81
    | SOME tyco =>
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    82
        (case AList.lookup (op =) tab tyco of
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    83
          NONE => default (*permissive*)
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
    84
        | SOME info => SOME info))
33970
haftmann
parents: 33969
diff changeset
    85
  end;
haftmann
parents: 33969
diff changeset
    86
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    87
val info_of_case = Symtab.lookup o #cases o Data.get;
33970
haftmann
parents: 33969
diff changeset
    88
41423
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
    89
fun register (dt_infos : (string * Datatype_Aux.info) list) =
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
    90
  Data.map (fn {types, constrs, cases} =>
33970
haftmann
parents: 33969
diff changeset
    91
    {types = types |> fold Symtab.update dt_infos,
haftmann
parents: 33969
diff changeset
    92
     constrs = constrs |> fold (fn (constr, dtname_info) =>
haftmann
parents: 33969
diff changeset
    93
         Symtab.map_default (constr, []) (cons dtname_info))
haftmann
parents: 33969
diff changeset
    94
       (maps (fn (dtname, info as {descr, index, ...}) =>
haftmann
parents: 33969
diff changeset
    95
          map (rpair (dtname, info) o fst)
haftmann
parents: 33969
diff changeset
    96
            (#3 (the (AList.lookup op = descr index)))) dt_infos),
haftmann
parents: 33969
diff changeset
    97
     cases = cases |> fold Symtab.update
haftmann
parents: 33969
diff changeset
    98
       (map (fn (_, info as {case_name, ...}) => (case_name, info)) dt_infos)});
haftmann
parents: 33969
diff changeset
    99
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   100
33970
haftmann
parents: 33969
diff changeset
   101
(* complex queries *)
haftmann
parents: 33969
diff changeset
   102
haftmann
parents: 33969
diff changeset
   103
fun the_spec thy dtco =
haftmann
parents: 33969
diff changeset
   104
  let
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   105
    val {descr, index, ...} = the_info thy dtco;
45821
wenzelm
parents: 45738
diff changeset
   106
    val (_, dtys, raw_cos) = the (AList.lookup (op =) descr index);
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   107
    val args = map Datatype_Aux.dest_DtTFree dtys;
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   108
    val cos = map (fn (co, tys) => (co, map (Datatype_Aux.typ_of_dtyp descr) tys)) raw_cos;
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   109
  in (args, cos) end;
33970
haftmann
parents: 33969
diff changeset
   110
haftmann
parents: 33969
diff changeset
   111
fun the_descr thy (raw_tycos as raw_tyco :: _) =
haftmann
parents: 33969
diff changeset
   112
  let
haftmann
parents: 33969
diff changeset
   113
    val info = the_info thy raw_tyco;
haftmann
parents: 33969
diff changeset
   114
    val descr = #descr info;
haftmann
parents: 33969
diff changeset
   115
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   116
    val (_, dtys, _) = the (AList.lookup (op =) descr (#index info));
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   117
    val vs = map Datatype_Aux.dest_DtTFree dtys;
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   118
41423
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
   119
    fun is_DtTFree (Datatype_Aux.DtTFree _) = true
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   120
      | is_DtTFree _ = false;
33970
haftmann
parents: 33969
diff changeset
   121
    val k = find_index (fn (_, (_, dTs, _)) => not (forall is_DtTFree dTs)) descr;
41423
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
   122
    val protoTs as (dataTs, _) =
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
   123
      chop k descr
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
   124
      |> (pairself o map)
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   125
        (fn (_, (tyco, dTs, _)) => (tyco, map (Datatype_Aux.typ_of_dtyp descr) dTs));
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   126
33970
haftmann
parents: 33969
diff changeset
   127
    val tycos = map fst dataTs;
45430
b8eb7a791dac misc tuning;
wenzelm
parents: 44121
diff changeset
   128
    val _ =
b8eb7a791dac misc tuning;
wenzelm
parents: 44121
diff changeset
   129
      if eq_set (op =) (tycos, raw_tycos) then ()
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   130
      else
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   131
        error ("Type constructors " ^ commas_quote raw_tycos ^
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   132
          " do not belong exhaustively to one mutual recursive datatype");
33970
haftmann
parents: 33969
diff changeset
   133
haftmann
parents: 33969
diff changeset
   134
    val (Ts, Us) = (pairself o map) Type protoTs;
haftmann
parents: 33969
diff changeset
   135
45701
615da8b8d758 discontinued obsolete datatype "alt_names";
wenzelm
parents: 45700
diff changeset
   136
    val names = map Long_Name.base_name tycos;
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   137
    val (auxnames, _) =
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   138
      Name.make_context names
43326
47cf4bc789aa simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents: 43253
diff changeset
   139
      |> fold_map (Name.variant o Datatype_Aux.name_of_typ) Us;
33970
haftmann
parents: 33969
diff changeset
   140
    val prefix = space_implode "_" names;
haftmann
parents: 33969
diff changeset
   141
haftmann
parents: 33969
diff changeset
   142
  in (descr, vs, tycos, prefix, (names, auxnames), (Ts, Us)) end;
haftmann
parents: 33969
diff changeset
   143
haftmann
parents: 33969
diff changeset
   144
fun all_distincts thy Ts =
haftmann
parents: 33969
diff changeset
   145
  let
haftmann
parents: 33969
diff changeset
   146
    fun add_tycos (Type (tyco, Ts)) = insert (op =) tyco #> fold add_tycos Ts
haftmann
parents: 33969
diff changeset
   147
      | add_tycos _ = I;
haftmann
parents: 33969
diff changeset
   148
    val tycos = fold add_tycos Ts [];
haftmann
parents: 33969
diff changeset
   149
  in map_filter (Option.map #distinct o get_info thy) tycos end;
haftmann
parents: 33969
diff changeset
   150
haftmann
parents: 33969
diff changeset
   151
fun get_constrs thy dtco =
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   152
  (case try (the_spec thy) dtco of
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   153
    SOME (args, cos) =>
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   154
      let
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   155
        fun subst (v, sort) = TVar ((v, 0), sort);
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   156
        fun subst_ty (TFree v) = subst v
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   157
          | subst_ty ty = ty;
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   158
        val dty = Type (dtco, map subst args);
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   159
        fun mk_co (co, tys) = (co, map (Term.map_atyps subst_ty) tys ---> dty);
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   160
      in SOME (map mk_co cos) end
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   161
  | NONE => NONE);
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   162
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   163
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   164
33970
haftmann
parents: 33969
diff changeset
   165
(** various auxiliary **)
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   166
33970
haftmann
parents: 33969
diff changeset
   167
(* case names *)
haftmann
parents: 33969
diff changeset
   168
haftmann
parents: 33969
diff changeset
   169
local
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   170
41423
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
   171
fun dt_recs (Datatype_Aux.DtTFree _) = []
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
   172
  | dt_recs (Datatype_Aux.DtType (_, dts)) = maps dt_recs dts
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
   173
  | dt_recs (Datatype_Aux.DtRec i) = [i];
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   174
41423
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
   175
fun dt_cases (descr: Datatype_Aux.descr) (_, args, constrs) =
33970
haftmann
parents: 33969
diff changeset
   176
  let
haftmann
parents: 33969
diff changeset
   177
    fun the_bname i = Long_Name.base_name (#1 (the (AList.lookup (op =) descr i)));
haftmann
parents: 33969
diff changeset
   178
    val bnames = map the_bname (distinct (op =) (maps dt_recs args));
haftmann
parents: 33969
diff changeset
   179
  in map (fn (c, _) => space_implode "_" (Long_Name.base_name c :: bnames)) constrs end;
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   180
33970
haftmann
parents: 33969
diff changeset
   181
fun induct_cases descr =
haftmann
parents: 33969
diff changeset
   182
  Datatype_Prop.indexify_names (maps (dt_cases descr) (map #2 descr));
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   183
33970
haftmann
parents: 33969
diff changeset
   184
fun exhaust_cases descr i = dt_cases descr (the (AList.lookup (op =) descr i));
haftmann
parents: 33969
diff changeset
   185
haftmann
parents: 33969
diff changeset
   186
in
haftmann
parents: 33969
diff changeset
   187
haftmann
parents: 33969
diff changeset
   188
fun mk_case_names_induct descr = Rule_Cases.case_names (induct_cases descr);
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   189
33970
haftmann
parents: 33969
diff changeset
   190
fun mk_case_names_exhausts descr new =
haftmann
parents: 33969
diff changeset
   191
  map (Rule_Cases.case_names o exhaust_cases descr o #1)
haftmann
parents: 33969
diff changeset
   192
    (filter (fn ((_, (name, _, _))) => member (op =) new name) descr);
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   193
33970
haftmann
parents: 33969
diff changeset
   194
end;
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   195
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   196
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   197
33970
haftmann
parents: 33969
diff changeset
   198
(** document antiquotation **)
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   199
43564
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   200
val antiq_setup =
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   201
  Thy_Output.antiquotation @{binding datatype} (Args.type_name true)
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   202
    (fn {source = src, context = ctxt, ...} => fn dtco =>
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   203
      let
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   204
        val thy = Proof_Context.theory_of ctxt;
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   205
        val (vs, cos) = the_spec thy dtco;
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   206
        val ty = Type (dtco, map TFree vs);
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   207
        val pretty_typ_bracket = Syntax.pretty_typ (Config.put pretty_priority 1001 ctxt);
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   208
        fun pretty_constr (co, tys) =
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   209
          Pretty.block (Pretty.breaks
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   210
            (Syntax.pretty_term ctxt (Const (co, tys ---> ty)) ::
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   211
              map pretty_typ_bracket tys));
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   212
        val pretty_datatype =
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   213
          Pretty.block
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   214
           (Pretty.command "datatype" :: Pretty.brk 1 ::
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   215
            Syntax.pretty_typ ctxt ty ::
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   216
            Pretty.str " =" :: Pretty.brk 1 ::
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   217
            flat (separate [Pretty.brk 1, Pretty.str "| "] (map (single o pretty_constr) cos)));
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   218
      in
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   219
        Thy_Output.output ctxt
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   220
          (Thy_Output.maybe_pretty_source (K (K pretty_datatype)) ctxt src [()])
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   221
      end);
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   222
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   223
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   224
33970
haftmann
parents: 33969
diff changeset
   225
(** abstract theory extensions relative to a datatype characterisation **)
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   226
33970
haftmann
parents: 33969
diff changeset
   227
structure Datatype_Interpretation = Interpretation
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   228
(
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   229
  type T = Datatype_Aux.config * string list;
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   230
  val eq: T * T -> bool = eq_snd (op =);
9dcbf6a1829c misc tuning;
wenzelm
parents: 45620
diff changeset
   231
);
33970
haftmann
parents: 33969
diff changeset
   232
fun interpretation f = Datatype_Interpretation.interpretation (uncurry f);
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
   233
val interpretation_data = Datatype_Interpretation.data;
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   234
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   235
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33963
diff changeset
   236
45890
5f70aaecae26 separate rep_datatype.ML;
wenzelm
parents: 45889
diff changeset
   237
(** setup theory **)
33970
haftmann
parents: 33969
diff changeset
   238
haftmann
parents: 33969
diff changeset
   239
val setup =
43564
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43329
diff changeset
   240
  antiq_setup #>
33970
haftmann
parents: 33969
diff changeset
   241
  Datatype_Interpretation.init;
haftmann
parents: 33969
diff changeset
   242
41423
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
   243
open Datatype_Aux;
25df154b8ffc do not open auxiliary ML structures;
wenzelm
parents: 40957
diff changeset
   244
30391
d930432adb13 adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents: 30364
diff changeset
   245
end;