src/HOL/Tools/typedef.ML
author wenzelm
Sun, 04 Aug 2024 17:39:47 +0200
changeset 80636 4041e7c8059d
parent 80633 276b07a1b5f5
child 80694 58a209c8d40a
permissions -rw-r--r--
tuned: more explicit dest_Const_name and dest_Const_type;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30345
diff changeset
     1
(*  Title:      HOL/Tools/typedef.ML
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16126
diff changeset
     2
    Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     3
21352
073c79be780c removed legacy read/cert/string_of;
wenzelm
parents: 20483
diff changeset
     4
Gordon/HOL-style type definitions: create a new syntactic type
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
     5
represented by a non-empty set.
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     6
*)
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     7
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30345
diff changeset
     8
signature TYPEDEF =
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
     9
sig
19705
08de66826677 simplified info/get_info;
wenzelm
parents: 19617
diff changeset
    10
  type info =
36107
2af69e16eced expose foundational typedef axiom name;
wenzelm
parents: 36106
diff changeset
    11
   {rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, axiom_name: string} *
49833
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
    12
   {inhabited: thm, type_definition: thm, Rep: thm, Rep_inverse: thm, Abs_inverse: thm,
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
    13
    Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
29061
c67cc9402ba9 inhabitance goal is now stated in original form and result contracted --
wenzelm
parents: 29059
diff changeset
    14
    Rep_induct: thm, Abs_induct: thm}
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    15
  val transform_info: morphism -> info -> info
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    16
  val get_info: Proof.context -> string -> info list
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    17
  val get_info_global: theory -> string -> info list
58662
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    18
  val interpretation: (string -> local_theory -> local_theory) -> theory -> theory
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
    19
  type bindings = {Rep_name: binding, Abs_name: binding, type_definition_name: binding}
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
    20
  val default_bindings: binding -> bindings
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
    21
  val make_bindings: binding -> bindings option -> bindings
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
    22
  val make_morphisms: binding -> (binding * binding) option -> bindings
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
    23
  val overloaded: bool Config.T
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
    24
  val add_typedef: {overloaded: bool} -> binding * (string * sort) list * mixfix ->
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
    25
    term -> bindings option -> (Proof.context -> tactic) -> local_theory ->
58959
1f195ed99941 proper proof context for typedef;
wenzelm
parents: 58662
diff changeset
    26
    (string * info) * local_theory
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
    27
  val add_typedef_global: {overloaded: bool} -> binding * (string * sort) list * mixfix ->
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
    28
    term -> bindings option -> (Proof.context -> tactic) -> theory ->
58959
1f195ed99941 proper proof context for typedef;
wenzelm
parents: 58662
diff changeset
    29
    (string * info) * theory
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
    30
  val typedef: {overloaded: bool} -> binding * (string * sort) list * mixfix ->
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
    31
    term -> bindings option -> local_theory -> Proof.state
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
    32
  val typedef_cmd: {overloaded: bool} -> binding * (string * string option) list * mixfix ->
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
    33
    string -> bindings option -> local_theory -> Proof.state
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    34
end;
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    35
31723
f5cafe803b55 discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 30345
diff changeset
    36
structure Typedef: TYPEDEF =
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    37
struct
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
    38
17922
0cba8edb269e removed obsolete add_typedef_x;
wenzelm
parents: 17412
diff changeset
    39
(** type definitions **)
0cba8edb269e removed obsolete add_typedef_x;
wenzelm
parents: 17412
diff changeset
    40
0cba8edb269e removed obsolete add_typedef_x;
wenzelm
parents: 17412
diff changeset
    41
(* theory data *)
15259
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
    42
19705
08de66826677 simplified info/get_info;
wenzelm
parents: 19617
diff changeset
    43
type info =
35994
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35840
diff changeset
    44
  (*global part*)
36107
2af69e16eced expose foundational typedef axiom name;
wenzelm
parents: 36106
diff changeset
    45
  {rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, axiom_name: string} *
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    46
  (*local part*)
49833
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
    47
  {inhabited: thm, type_definition: thm, Rep: thm, Rep_inverse: thm, Abs_inverse: thm,
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
    48
    Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
35994
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35840
diff changeset
    49
    Rep_induct: thm, Abs_induct: thm};
19459
2041d472fc17 seperated typedef codegen from main code
haftmann
parents: 19391
diff changeset
    50
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    51
fun transform_info phi (info: info) =
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    52
  let
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    53
    val thm = Morphism.thm phi;
49833
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
    54
    val (global_info, {inhabited, type_definition, Rep, Rep_inverse, Abs_inverse,
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
    55
      Rep_inject, Abs_inject, Rep_cases, Abs_cases, Rep_induct, Abs_induct}) = info;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    56
  in
35994
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35840
diff changeset
    57
    (global_info,
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35840
diff changeset
    58
     {inhabited = thm inhabited, type_definition = thm type_definition,
49833
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
    59
      Rep = thm Rep, Rep_inverse = thm Rep_inverse, Abs_inverse = thm Abs_inverse,
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
    60
      Rep_inject = thm Rep_inject, Abs_inject = thm Abs_inject,
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
    61
      Rep_cases = thm Rep_cases, Abs_cases = thm Abs_cases,
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
    62
      Rep_induct = thm Rep_induct, Abs_induct = thm Abs_induct})
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    63
  end;
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    64
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    65
structure Data = Generic_Data
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 21565
diff changeset
    66
(
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    67
  type T = info list Symtab.table;
15259
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
    68
  val empty = Symtab.empty;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    69
  fun merge data = Symtab.merge_list (K true) data;
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 21565
diff changeset
    70
);
15259
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
    71
61103
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    72
fun get_info_generic context =
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    73
  Symtab.lookup_list (Data.get context) #>
67664
ad2b3e330c27 tuned signature;
wenzelm
parents: 67149
diff changeset
    74
  map (transform_info (Morphism.transfer_morphism'' context));
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    75
61103
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    76
val get_info = get_info_generic o Context.Proof;
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    77
val get_info_global = get_info_generic o Context.Theory;
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    78
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    79
fun put_info name info =
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    80
  Data.map (Symtab.cons_list (name, transform_info Morphism.trim_context_morphism info));
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    81
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    82
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    83
(* global interpretation *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    84
58662
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    85
structure Typedef_Plugin = Plugin(type T = string);
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    86
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
    87
val typedef_plugin = Plugin_Name.declare_setup \<^binding>\<open>typedef\<close>;
56375
32e0da92c786 use same idiom as used for datatype 'size' function to name constants and theorems emerging from various type interpretations -- reduces the chances of name clashes on theory merges
blanchet
parents: 54883
diff changeset
    88
58662
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    89
fun interpretation f =
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    90
  Typedef_Plugin.interpretation typedef_plugin
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    91
    (fn name => fn lthy =>
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    92
      lthy
59880
30687c3f2b10 clarified role of naming for background theory: transform_binding (e.g. for "concealed" flag) uses naming of hypothetical context;
wenzelm
parents: 59859
diff changeset
    93
      |> Local_Theory.map_background_naming
58662
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    94
          (Name_Space.root_path #> Name_Space.add_path (Long_Name.qualifier name))
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    95
      |> f name
59880
30687c3f2b10 clarified role of naming for background theory: transform_binding (e.g. for "concealed" flag) uses naming of hypothetical context;
wenzelm
parents: 59859
diff changeset
    96
      |> Local_Theory.restore_background_naming lthy);
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    97
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    98
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    99
(* primitive typedef axiomatization -- for fresh typedecl *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   100
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
   101
val typedef_overloaded = Attrib.setup_config_bool \<^binding>\<open>typedef_overloaded\<close> (K false);
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   102
80631
a033b5b6f544 tuned: more antiquotations;
wenzelm
parents: 78095
diff changeset
   103
fun mk_inhabited T A =
a033b5b6f544 tuned: more antiquotations;
wenzelm
parents: 78095
diff changeset
   104
  HOLogic.mk_Trueprop (\<^Const>\<open>Ex T for \<open>Abs ("x", T, \<^Const>\<open>Set.member T for \<open>Bound 0\<close> A\<close>)\<close>\<close>);
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   105
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   106
fun mk_typedef newT oldT RepC AbsC A =
80631
a033b5b6f544 tuned: more antiquotations;
wenzelm
parents: 78095
diff changeset
   107
  let val type_definition = \<^Const>\<open>type_definition newT oldT for RepC AbsC A\<close>
a033b5b6f544 tuned: more antiquotations;
wenzelm
parents: 78095
diff changeset
   108
  in Logic.mk_implies (mk_inhabited oldT A, HOLogic.mk_Trueprop type_definition) end;
35134
d8d6c2f55c0c Typedef.the_info;
wenzelm
parents: 35129
diff changeset
   109
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   110
fun primitive_typedef {overloaded} type_definition_name newT oldT Rep_name Abs_name A lthy =
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   111
  let
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   112
    (* errors *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   113
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   114
    fun show_names pairs = commas_quote (map fst pairs);
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   115
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   116
    val lhs_tfrees = Term.add_tfreesT newT [];
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   117
    val rhs_tfrees = Term.add_tfreesT oldT [];
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   118
    val _ =
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   119
      (case fold (remove (op =)) lhs_tfrees rhs_tfrees of
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   120
        [] => ()
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   121
      | extras => error ("Extra type variables in representing set: " ^ show_names extras));
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   122
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   123
    val _ =
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   124
      (case Term.add_frees A [] of [] =>
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   125
        []
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   126
      | xs => error ("Illegal variables in representing set: " ^ show_names xs));
35134
d8d6c2f55c0c Typedef.the_info;
wenzelm
parents: 35129
diff changeset
   127
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   128
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   129
    (* axiomatization *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   130
42375
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   131
    val ((RepC, AbsC), consts_lthy) = lthy
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   132
      |> Local_Theory.background_theory_result
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   133
        (Sign.declare_const lthy ((Rep_name, newT --> oldT), NoSyn) ##>>
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   134
          Sign.declare_const lthy ((Abs_name, oldT --> newT), NoSyn));
61255
15865e0c5598 eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
wenzelm
parents: 61248
diff changeset
   135
    val const_dep = Theory.const_dep (Proof_Context.theory_of consts_lthy);
61261
ddb2da7cb2e4 more explicit Defs.context: use proper name spaces as far as possible;
wenzelm
parents: 61260
diff changeset
   136
    val defs_context = Proof_Context.defs_context consts_lthy;
61247
76148d288b2e tuned whitespace;
wenzelm
parents: 61246
diff changeset
   137
61255
15865e0c5598 eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
wenzelm
parents: 61248
diff changeset
   138
    val A_consts = fold_aterms (fn Const c => insert (op =) (const_dep c) | _ => I) A [];
61248
066792098895 tuned signature;
wenzelm
parents: 61247
diff changeset
   139
    val A_types =
61255
15865e0c5598 eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
wenzelm
parents: 61248
diff changeset
   140
      (fold_types o fold_subtypes) (fn Type t => insert (op =) (Theory.type_dep t) | _ => I) A [];
61246
077b88f9ec16 HOL typedef with explicit dependency checks according to Ondrey Kuncar, 07-Jul-2015, 16-Jul-2015, 30-Jul-2015;
wenzelm
parents: 61110
diff changeset
   141
    val typedef_deps = A_consts @ A_types;
61247
76148d288b2e tuned whitespace;
wenzelm
parents: 61246
diff changeset
   142
61255
15865e0c5598 eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
wenzelm
parents: 61248
diff changeset
   143
    val newT_dep = Theory.type_dep (dest_Type newT);
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   144
42375
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   145
    val ((axiom_name, axiom), axiom_lthy) = consts_lthy
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   146
      |> Local_Theory.background_theory_result
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   147
        (Thm.add_axiom consts_lthy (type_definition_name, mk_typedef newT oldT RepC AbsC A) ##>
61261
ddb2da7cb2e4 more explicit Defs.context: use proper name spaces as far as possible;
wenzelm
parents: 61260
diff changeset
   148
          Theory.add_deps defs_context "" newT_dep typedef_deps ##>
ddb2da7cb2e4 more explicit Defs.context: use proper name spaces as far as possible;
wenzelm
parents: 61260
diff changeset
   149
          Theory.add_deps defs_context "" (const_dep (dest_Const RepC)) [newT_dep] ##>
ddb2da7cb2e4 more explicit Defs.context: use proper name spaces as far as possible;
wenzelm
parents: 61260
diff changeset
   150
          Theory.add_deps defs_context "" (const_dep (dest_Const AbsC)) [newT_dep]);
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   151
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   152
    val axiom_defs = Theory.defs_of (Proof_Context.theory_of axiom_lthy);
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   153
    val newT_deps = maps #2 (Defs.get_deps axiom_defs (#1 newT_dep));
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   154
    val _ =
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   155
      if null newT_deps orelse overloaded orelse Config.get lthy typedef_overloaded then ()
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   156
      else
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   157
        error (Pretty.string_of (Pretty.chunks
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   158
          [Pretty.paragraph
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   159
            (Pretty.text "Type definition with open dependencies, use" @
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   160
             [Pretty.brk 1, Pretty.str "\"", Pretty.keyword1 "typedef", Pretty.brk 1,
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   161
              Pretty.str "(", Pretty.keyword2 "overloaded", Pretty.str ")\"", Pretty.brk 1] @
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   162
             Pretty.text "or enable configuration option \"typedef_overloaded\" in the context."),
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   163
           Pretty.block [Pretty.str "  Type:", Pretty.brk 2, Syntax.pretty_typ axiom_lthy newT],
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   164
           Pretty.block (Pretty.str "  Deps:" :: Pretty.brk 2 ::
61261
ddb2da7cb2e4 more explicit Defs.context: use proper name spaces as far as possible;
wenzelm
parents: 61260
diff changeset
   165
             Pretty.commas
ddb2da7cb2e4 more explicit Defs.context: use proper name spaces as far as possible;
wenzelm
parents: 61260
diff changeset
   166
              (map (Defs.pretty_entry (Proof_Context.defs_context axiom_lthy)) newT_deps))]))
42375
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   167
  in ((RepC, AbsC, axiom_name, axiom), axiom_lthy) end;
15259
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
   168
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
   169
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   170
(* derived bindings *)
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   171
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   172
type bindings = {Rep_name: binding, Abs_name: binding, type_definition_name: binding};
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   173
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   174
fun prefix_binding prfx name =
63003
bf5fcc65586b clarified signature;
wenzelm
parents: 62515
diff changeset
   175
  Binding.reset_pos (Binding.qualify_name false name (prfx ^ Binding.name_of name));
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   176
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   177
fun qualify_binding name = Binding.qualify false (Binding.name_of name);
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   178
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   179
fun default_bindings name =
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   180
 {Rep_name = prefix_binding "Rep_" name,
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   181
  Abs_name = prefix_binding "Abs_" name,
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   182
  type_definition_name = prefix_binding "type_definition_" name};
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   183
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   184
fun make_bindings name NONE = default_bindings name
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   185
  | make_bindings _ (SOME bindings) = bindings;
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   186
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   187
fun make_morphisms name NONE = default_bindings name
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   188
  | make_morphisms name (SOME (Rep_name, Abs_name)) =
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   189
     {Rep_name = qualify_binding name Rep_name,
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   190
      Abs_name = qualify_binding name Abs_name,
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   191
      type_definition_name = #type_definition_name (default_bindings name)};
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   192
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   193
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   194
(* prepare_typedef *)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   195
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   196
fun prepare_typedef prep_term overloaded (name, raw_args, mx) raw_set opt_bindings lthy =
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   197
  let
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   198
    (* rhs *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   199
36153
1ac501e16a6a replaced slightly odd Typedecl.predeclare_constraints by plain declaration of type arguments -- also avoid "recursive" declaration of type constructor, which can cause problems with sequential definitions B.foo = A.foo;
wenzelm
parents: 36107
diff changeset
   200
    val tmp_ctxt = lthy |> fold (Variable.declare_typ o TFree) raw_args;
1ac501e16a6a replaced slightly odd Typedecl.predeclare_constraints by plain declaration of type arguments -- also avoid "recursive" declaration of type constructor, which can cause problems with sequential definitions B.foo = A.foo;
wenzelm
parents: 36107
diff changeset
   201
    val set = prep_term tmp_ctxt raw_set;
1ac501e16a6a replaced slightly odd Typedecl.predeclare_constraints by plain declaration of type arguments -- also avoid "recursive" declaration of type constructor, which can cause problems with sequential definitions B.foo = A.foo;
wenzelm
parents: 36107
diff changeset
   202
    val tmp_ctxt' = tmp_ctxt |> Variable.declare_term set;
35836
9380fab5f4f7 eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints;
wenzelm
parents: 35766
diff changeset
   203
21352
073c79be780c removed legacy read/cert/string_of;
wenzelm
parents: 20483
diff changeset
   204
    val setT = Term.fastype_of set;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   205
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   206
      error ("Not a set type: " ^ quote (Syntax.string_of_typ lthy setT));
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   207
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   208
    val bname = Binding.name_of name;
80631
a033b5b6f544 tuned: more antiquotations;
wenzelm
parents: 78095
diff changeset
   209
    val goal = mk_inhabited oldT set;
a033b5b6f544 tuned: more antiquotations;
wenzelm
parents: 78095
diff changeset
   210
    val goal_pat =
a033b5b6f544 tuned: more antiquotations;
wenzelm
parents: 78095
diff changeset
   211
      mk_inhabited oldT (Var (the_default (bname, 0) (Lexicon.read_variable bname), setT));
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   212
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   213
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   214
    (* lhs *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   215
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42290
diff changeset
   216
    val args = map (Proof_Context.check_tfree tmp_ctxt') raw_args;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   217
    val (newT, typedecl_lthy) = lthy
61259
6dc3d5d50e57 tuned signature;
wenzelm
parents: 61255
diff changeset
   218
      |> Typedecl.typedecl {final = false} (name, args, mx)
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   219
      ||> Variable.declare_term set;
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   220
80633
wenzelm
parents: 80631
diff changeset
   221
    val full_name = dest_Type_name newT;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   222
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   223
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   224
    (* axiomatization *)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   225
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   226
    val {Rep_name, Abs_name, type_definition_name} = make_bindings name opt_bindings;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   227
49833
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
   228
    val ((RepC, AbsC, axiom_name, typedef), typedef_lthy) = typedecl_lthy
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   229
      |> primitive_typedef overloaded type_definition_name newT oldT Rep_name Abs_name set;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   230
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   231
    val alias_lthy = typedef_lthy
80636
4041e7c8059d tuned: more explicit dest_Const_name and dest_Const_type;
wenzelm
parents: 80633
diff changeset
   232
      |> Local_Theory.const_alias Rep_name (Term.dest_Const_name RepC)
4041e7c8059d tuned: more explicit dest_Const_name and dest_Const_type;
wenzelm
parents: 80633
diff changeset
   233
      |> Local_Theory.const_alias Abs_name (Term.dest_Const_name AbsC);
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   234
29056
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 29054
diff changeset
   235
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   236
    (* result *)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   237
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   238
    fun note ((b, atts), th) =
63019
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   239
      Local_Theory.note ((b, atts), [th]) #>> (fn (_, [th']) => th');
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   240
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   241
    fun typedef_result inhabited lthy1 =
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   242
      let
70483
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   243
        val ((_, [type_definition]), lthy2) = lthy1
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   244
          |> Local_Theory.note ((type_definition_name, []), [inhabited RS typedef]);
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   245
        fun make th = Goal.norm_result lthy2 (type_definition RS th);
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   246
        val (((((((((Rep, Rep_inverse), Abs_inverse), Rep_inject), Abs_inject), Rep_cases),
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   247
            Abs_cases), Rep_induct), Abs_induct), lthy3) = lthy2
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   248
          |> note ((Rep_name, []), make @{thm type_definition.Rep})
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   249
          ||>> note ((Binding.suffix_name "_inverse" Rep_name, []),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   250
              make @{thm type_definition.Rep_inverse})
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   251
          ||>> note ((Binding.suffix_name "_inverse" Abs_name, []),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   252
              make @{thm type_definition.Abs_inverse})
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   253
          ||>> note ((Binding.suffix_name "_inject" Rep_name, []),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   254
              make @{thm type_definition.Rep_inject})
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   255
          ||>> note ((Binding.suffix_name "_inject" Abs_name, []),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   256
              make @{thm type_definition.Abs_inject})
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   257
          ||>> note ((Binding.suffix_name "_cases" Rep_name,
63019
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   258
                [Attrib.case_names [Binding.name_of Rep_name],
78095
bc42c074e58f tuned signature: more position information;
wenzelm
parents: 74561
diff changeset
   259
                 Attrib.internal \<^here> (K (Induct.cases_pred full_name))]),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   260
              make @{thm type_definition.Rep_cases})
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   261
          ||>> note ((Binding.suffix_name "_cases" Abs_name,
63019
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   262
                [Attrib.case_names [Binding.name_of Abs_name],
78095
bc42c074e58f tuned signature: more position information;
wenzelm
parents: 74561
diff changeset
   263
                 Attrib.internal \<^here> (K (Induct.cases_type full_name))]),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   264
              make @{thm type_definition.Abs_cases})
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   265
          ||>> note ((Binding.suffix_name "_induct" Rep_name,
63019
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   266
                [Attrib.case_names [Binding.name_of Rep_name],
78095
bc42c074e58f tuned signature: more position information;
wenzelm
parents: 74561
diff changeset
   267
                 Attrib.internal \<^here> (K (Induct.induct_pred full_name))]),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   268
              make @{thm type_definition.Rep_induct})
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   269
          ||>> note ((Binding.suffix_name "_induct" Abs_name,
63019
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   270
                [Attrib.case_names [Binding.name_of Abs_name],
78095
bc42c074e58f tuned signature: more position information;
wenzelm
parents: 74561
diff changeset
   271
                 Attrib.internal \<^here> (K (Induct.induct_type full_name))]),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   272
              make @{thm type_definition.Abs_induct});
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   273
35994
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35840
diff changeset
   274
        val info =
80636
4041e7c8059d tuned: more explicit dest_Const_name and dest_Const_type;
wenzelm
parents: 80633
diff changeset
   275
          ({rep_type = oldT, abs_type = newT, Rep_name = dest_Const_name RepC,
4041e7c8059d tuned: more explicit dest_Const_name and dest_Const_type;
wenzelm
parents: 80633
diff changeset
   276
            Abs_name = dest_Const_name AbsC, axiom_name = axiom_name},
49833
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
   277
           {inhabited = inhabited, type_definition = type_definition,
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   278
            Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   279
            Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
35994
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35840
diff changeset
   280
          Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct});
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   281
      in
70483
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   282
        lthy3
78095
bc42c074e58f tuned signature: more position information;
wenzelm
parents: 74561
diff changeset
   283
        |> Local_Theory.declaration {syntax = false, pervasive = true, pos = \<^here>}
49835
31f32ec4d766 discontinued typedef with alternative name;
wenzelm
parents: 49834
diff changeset
   284
          (fn phi => put_info full_name (transform_info phi info))
58662
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
   285
        |> Typedef_Plugin.data Plugin_Name.default_filter full_name
49835
31f32ec4d766 discontinued typedef with alternative name;
wenzelm
parents: 49834
diff changeset
   286
        |> pair (full_name, info)
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   287
      end;
11426
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   288
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   289
  in ((goal, goal_pat, typedef_result), alias_lthy) end
30345
76fd85bbf139 more uniform handling of binding in packages;
wenzelm
parents: 29579
diff changeset
   290
  handle ERROR msg =>
42381
309ec68442c6 added Binding.print convenience, which includes quote already;
wenzelm
parents: 42375
diff changeset
   291
    cat_error msg ("The error(s) above occurred in typedef " ^ Binding.print name);
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   292
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   293
29056
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 29054
diff changeset
   294
(* add_typedef: tactic interface *)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   295
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   296
fun add_typedef overloaded typ set opt_bindings tac lthy =
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   297
  let
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   298
    val ((goal, _, typedef_result), lthy') =
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   299
      prepare_typedef Syntax.check_term overloaded typ set opt_bindings lthy;
70483
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   300
    val inhabited = Goal.prove lthy' [] [] goal (tac o #context) |> Goal.norm_result lthy';
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   301
  in typedef_result inhabited lthy' end;
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   302
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   303
fun add_typedef_global overloaded typ set opt_bindings tac =
69829
3bfa28b3a5b2 streamlined specification interfaces
haftmann
parents: 68264
diff changeset
   304
  Named_Target.theory_map_result (apsnd o transform_info)
3bfa28b3a5b2 streamlined specification interfaces
haftmann
parents: 68264
diff changeset
   305
    (add_typedef overloaded typ set opt_bindings tac)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   306
17339
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   307
29056
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 29054
diff changeset
   308
(* typedef: proof interface *)
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   309
17339
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   310
local
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   311
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   312
fun gen_typedef prep_term prep_constraint overloaded (b, raw_args, mx) set opt_bindings lthy =
11822
122834177ec1 improved internal interface of typedef;
wenzelm
parents: 11807
diff changeset
   313
  let
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35836
diff changeset
   314
    val args = map (apsnd (prep_constraint lthy)) raw_args;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   315
    val ((goal, goal_pat, typedef_result), lthy') =
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   316
      prepare_typedef prep_term overloaded (b, args, mx) set opt_bindings lthy;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   317
    fun after_qed [[th]] = snd o typedef_result th;
36323
655e2d74de3a modernized naming conventions of main Isar proof elements;
wenzelm
parents: 36153
diff changeset
   318
  in Proof.theorem NONE after_qed [[(goal, [goal_pat])]] lthy' end;
17339
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   319
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   320
in
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   321
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35836
diff changeset
   322
val typedef = gen_typedef Syntax.check_term (K I);
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35836
diff changeset
   323
val typedef_cmd = gen_typedef Syntax.read_term Typedecl.read_constraint;
17339
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   324
19705
08de66826677 simplified info/get_info;
wenzelm
parents: 19617
diff changeset
   325
end;
15259
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
   326
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
   327
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
   328
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   329
(** outer syntax **)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   330
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24861
diff changeset
   331
val _ =
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
   332
  Outer_Syntax.local_theory_to_proof \<^command_keyword>\<open>typedef\<close>
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46949
diff changeset
   333
    "HOL type definition (requires non-emptiness proof)"
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   334
    (Parse_Spec.overloaded -- Parse.type_args_constrained -- Parse.binding -- Parse.opt_mixfix --
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
   335
      (\<^keyword>\<open>=\<close> |-- Parse.term) --
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
   336
      Scan.option (\<^keyword>\<open>morphisms\<close> |-- Parse.!!! (Parse.binding -- Parse.binding))
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   337
    >> (fn (((((overloaded, vs), t), mx), A), opt_morphs) => fn lthy =>
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   338
        typedef_cmd {overloaded = overloaded} (t, vs, mx) A
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   339
          (SOME (make_morphisms t opt_morphs)) lthy));
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   340
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   341
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   342
val overloaded = typedef_overloaded;
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   343
68264
bb9a3be6952a more exports;
wenzelm
parents: 67664
diff changeset
   344
bb9a3be6952a more exports;
wenzelm
parents: 67664
diff changeset
   345
bb9a3be6952a more exports;
wenzelm
parents: 67664
diff changeset
   346
(** theory export **)
bb9a3be6952a more exports;
wenzelm
parents: 67664
diff changeset
   347
74114
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   348
val _ =
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   349
  (Theory.setup o Thy_Info.add_presentation) (fn context => fn thy =>
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   350
    if Export_Theory.export_enabled context then
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   351
      let
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   352
        val parent_spaces = map Sign.type_space (Theory.parents_of thy);
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   353
        val typedefs =
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   354
          Name_Space.dest_table (#types (Type.rep_tsig (Sign.tsig_of thy)))
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   355
          |> maps (fn (name, _) =>
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   356
              if exists (fn space => Name_Space.declared space name) parent_spaces then []
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   357
              else
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   358
                get_info_global thy name
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   359
                |> map (fn ({rep_type, abs_type, Rep_name, Abs_name, axiom_name}, _) =>
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   360
                  (name, (rep_type, (abs_type, (Rep_name, (Abs_name, axiom_name)))))));
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   361
        val encode =
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   362
          let open XML.Encode Term_XML.Encode
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   363
          in list (pair string (pair typ (pair typ (pair string (pair string string))))) end;
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   364
      in
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   365
        if null typedefs then ()
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   366
        else Export_Theory.export_body thy "typedefs" (encode typedefs)
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   367
      end
700e5bd59c7d clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
wenzelm
parents: 70483
diff changeset
   368
    else ());
68264
bb9a3be6952a more exports;
wenzelm
parents: 67664
diff changeset
   369
29056
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 29054
diff changeset
   370
end;