src/HOL/Tools/typedef.ML
author wenzelm
Wed, 04 Aug 2021 19:41:59 +0200
changeset 74114 700e5bd59c7d
parent 70483 59250a328113
child 74561 8e6c973003c8
permissions -rw-r--r--
clarified export of formal entities: name space info is always present, but content depends on option "export_theory";
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;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16126
diff changeset
    69
  val extend = I;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    70
  fun merge data = Symtab.merge_list (K true) data;
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 21565
diff changeset
    71
);
15259
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
    72
61103
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    73
fun get_info_generic context =
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    74
  Symtab.lookup_list (Data.get context) #>
67664
ad2b3e330c27 tuned signature;
wenzelm
parents: 67149
diff changeset
    75
  map (transform_info (Morphism.transfer_morphism'' context));
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    76
61103
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    77
val get_info = get_info_generic o Context.Proof;
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    78
val get_info_global = get_info_generic o Context.Theory;
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    79
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    80
fun put_info name info =
8ed21464e260 trim context for persistent storage;
wenzelm
parents: 59936
diff changeset
    81
  Data.map (Symtab.cons_list (name, transform_info Morphism.trim_context_morphism info));
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    82
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    83
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    84
(* global interpretation *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    85
58662
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    86
structure Typedef_Plugin = Plugin(type T = string);
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    87
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
    88
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
    89
58662
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    90
fun interpretation f =
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    91
  Typedef_Plugin.interpretation typedef_plugin
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    92
    (fn name => fn lthy =>
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    93
      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
    94
      |> Local_Theory.map_background_naming
58662
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    95
          (Name_Space.root_path #> Name_Space.add_path (Long_Name.qualifier name))
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
    96
      |> 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
    97
      |> Local_Theory.restore_background_naming lthy);
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    98
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
    99
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   100
(* primitive typedef axiomatization -- for fresh typedecl *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   101
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
   102
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
   103
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   104
fun mk_inhabited A =
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   105
  let val T = HOLogic.dest_setT (Term.fastype_of A)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   106
  in HOLogic.mk_Trueprop (HOLogic.exists_const T $ Abs ("x", T, HOLogic.mk_mem (Bound 0, A))) end;
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   107
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   108
fun mk_typedef newT oldT RepC AbsC A =
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   109
  let
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   110
    val typedefC =
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
   111
      Const (\<^const_name>\<open>type_definition\<close>,
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   112
        (newT --> oldT) --> (oldT --> newT) --> HOLogic.mk_setT oldT --> HOLogic.boolT);
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   113
  in Logic.mk_implies (mk_inhabited A, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ A)) end;
35134
d8d6c2f55c0c Typedef.the_info;
wenzelm
parents: 35129
diff changeset
   114
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   115
fun primitive_typedef {overloaded} type_definition_name newT oldT Rep_name Abs_name A lthy =
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   116
  let
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   117
    (* errors *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   118
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   119
    fun show_names pairs = commas_quote (map fst pairs);
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   120
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   121
    val lhs_tfrees = Term.add_tfreesT newT [];
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   122
    val rhs_tfrees = Term.add_tfreesT oldT [];
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   123
    val _ =
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   124
      (case fold (remove (op =)) lhs_tfrees rhs_tfrees of
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   125
        [] => ()
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   126
      | extras => error ("Extra type variables in representing set: " ^ show_names extras));
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   127
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   128
    val _ =
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   129
      (case Term.add_frees A [] of [] =>
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   130
        []
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   131
      | xs => error ("Illegal variables in representing set: " ^ show_names xs));
35134
d8d6c2f55c0c Typedef.the_info;
wenzelm
parents: 35129
diff changeset
   132
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   133
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   134
    (* axiomatization *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   135
42375
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   136
    val ((RepC, AbsC), consts_lthy) = lthy
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   137
      |> Local_Theory.background_theory_result
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   138
        (Sign.declare_const lthy ((Rep_name, newT --> oldT), NoSyn) ##>>
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   139
          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
   140
    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
   141
    val defs_context = Proof_Context.defs_context consts_lthy;
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 A_consts = fold_aterms (fn Const c => insert (op =) (const_dep c) | _ => I) A [];
61248
066792098895 tuned signature;
wenzelm
parents: 61247
diff changeset
   144
    val A_types =
61255
15865e0c5598 eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
wenzelm
parents: 61248
diff changeset
   145
      (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
   146
    val typedef_deps = A_consts @ A_types;
61247
76148d288b2e tuned whitespace;
wenzelm
parents: 61246
diff changeset
   147
61255
15865e0c5598 eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
wenzelm
parents: 61248
diff changeset
   148
    val newT_dep = Theory.type_dep (dest_Type newT);
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   149
42375
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   150
    val ((axiom_name, axiom), axiom_lthy) = consts_lthy
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42361
diff changeset
   151
      |> Local_Theory.background_theory_result
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   152
        (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
   153
          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
   154
          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
   155
          Theory.add_deps defs_context "" (const_dep (dest_Const AbsC)) [newT_dep]);
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   156
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   157
    val axiom_defs = Theory.defs_of (Proof_Context.theory_of axiom_lthy);
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   158
    val newT_deps = maps #2 (Defs.get_deps axiom_defs (#1 newT_dep));
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   159
    val _ =
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   160
      if null newT_deps orelse overloaded orelse Config.get lthy typedef_overloaded then ()
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   161
      else
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   162
        error (Pretty.string_of (Pretty.chunks
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   163
          [Pretty.paragraph
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   164
            (Pretty.text "Type definition with open dependencies, use" @
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   165
             [Pretty.brk 1, Pretty.str "\"", Pretty.keyword1 "typedef", Pretty.brk 1,
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   166
              Pretty.str "(", Pretty.keyword2 "overloaded", Pretty.str ")\"", Pretty.brk 1] @
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   167
             Pretty.text "or enable configuration option \"typedef_overloaded\" in the context."),
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   168
           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
   169
           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
   170
             Pretty.commas
ddb2da7cb2e4 more explicit Defs.context: use proper name spaces as far as possible;
wenzelm
parents: 61260
diff changeset
   171
              (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
   172
  in ((RepC, AbsC, axiom_name, axiom), axiom_lthy) end;
15259
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
   173
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
   174
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   175
(* derived bindings *)
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   176
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   177
type bindings = {Rep_name: binding, Abs_name: binding, type_definition_name: binding};
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   178
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   179
fun prefix_binding prfx name =
63003
bf5fcc65586b clarified signature;
wenzelm
parents: 62515
diff changeset
   180
  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
   181
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   182
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
   183
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   184
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
   185
 {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
   186
  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
   187
  type_definition_name = prefix_binding "type_definition_" name};
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   188
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   189
fun make_bindings name NONE = default_bindings name
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   190
  | make_bindings _ (SOME bindings) = bindings;
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   191
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   192
fun make_morphisms name NONE = default_bindings name
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   193
  | 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
   194
     {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
   195
      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
   196
      type_definition_name = #type_definition_name (default_bindings name)};
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   197
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   198
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   199
(* prepare_typedef *)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   200
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   201
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
   202
  let
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   203
    (* rhs *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   204
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
   205
    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
   206
    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
   207
    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
   208
21352
073c79be780c removed legacy read/cert/string_of;
wenzelm
parents: 20483
diff changeset
   209
    val setT = Term.fastype_of set;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   210
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   211
      error ("Not a set type: " ^ quote (Syntax.string_of_typ lthy setT));
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   212
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   213
    val bname = Binding.name_of name;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   214
    val goal = mk_inhabited set;
42290
b1f544c84040 discontinued special treatment of structure Lexicon;
wenzelm
parents: 38757
diff changeset
   215
    val goal_pat = mk_inhabited (Var (the_default (bname, 0) (Lexicon.read_variable bname), setT));
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   216
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   217
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   218
    (* lhs *)
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   219
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42290
diff changeset
   220
    val args = map (Proof_Context.check_tfree tmp_ctxt') raw_args;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   221
    val (newT, typedecl_lthy) = lthy
61259
6dc3d5d50e57 tuned signature;
wenzelm
parents: 61255
diff changeset
   222
      |> Typedecl.typedecl {final = false} (name, args, mx)
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   223
      ||> Variable.declare_term set;
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   224
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
   225
    val Type (full_name, _) = newT;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   226
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   227
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   228
    (* axiomatization *)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   229
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 61103
diff changeset
   230
    val {Rep_name, Abs_name, type_definition_name} = make_bindings name opt_bindings;
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   231
49833
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
   232
    val ((RepC, AbsC, axiom_name, typedef), typedef_lthy) = typedecl_lthy
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   233
      |> primitive_typedef overloaded type_definition_name newT oldT Rep_name Abs_name set;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   234
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   235
    val alias_lthy = typedef_lthy
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   236
      |> Local_Theory.const_alias Rep_name (#1 (Term.dest_Const RepC))
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   237
      |> Local_Theory.const_alias Abs_name (#1 (Term.dest_Const AbsC));
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   238
29056
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 29054
diff changeset
   239
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   240
    (* result *)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   241
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 61261
diff changeset
   242
    fun note ((b, atts), th) =
63019
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   243
      Local_Theory.note ((b, atts), [th]) #>> (fn (_, [th']) => th');
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   244
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   245
    fun typedef_result inhabited lthy1 =
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   246
      let
70483
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   247
        val ((_, [type_definition]), lthy2) = lthy1
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   248
          |> Local_Theory.note ((type_definition_name, []), [inhabited RS typedef]);
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   249
        fun make th = Goal.norm_result lthy2 (type_definition RS th);
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   250
        val (((((((((Rep, Rep_inverse), Abs_inverse), Rep_inject), Abs_inject), Rep_cases),
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   251
            Abs_cases), Rep_induct), Abs_induct), lthy3) = lthy2
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   252
          |> 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
   253
          ||>> note ((Binding.suffix_name "_inverse" Rep_name, []),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   254
              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
   255
          ||>> note ((Binding.suffix_name "_inverse" Abs_name, []),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   256
              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
   257
          ||>> note ((Binding.suffix_name "_inject" Rep_name, []),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   258
              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
   259
          ||>> note ((Binding.suffix_name "_inject" Abs_name, []),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   260
              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
   261
          ||>> note ((Binding.suffix_name "_cases" Rep_name,
63019
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   262
                [Attrib.case_names [Binding.name_of Rep_name],
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   263
                 Attrib.internal (K (Induct.cases_pred full_name))]),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   264
              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
   265
          ||>> note ((Binding.suffix_name "_cases" Abs_name,
63019
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   266
                [Attrib.case_names [Binding.name_of Abs_name],
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   267
                 Attrib.internal (K (Induct.cases_type full_name))]),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   268
              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
   269
          ||>> note ((Binding.suffix_name "_induct" Rep_name,
63019
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   270
                [Attrib.case_names [Binding.name_of Rep_name],
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   271
                 Attrib.internal (K (Induct.induct_pred full_name))]),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   272
              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
   273
          ||>> note ((Binding.suffix_name "_induct" Abs_name,
63019
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   274
                [Attrib.case_names [Binding.name_of Abs_name],
80ef19b51493 prefer internal attribute source;
wenzelm
parents: 63003
diff changeset
   275
                 Attrib.internal (K (Induct.induct_type full_name))]),
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   276
              make @{thm type_definition.Abs_induct});
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   277
35994
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35840
diff changeset
   278
        val info =
36107
2af69e16eced expose foundational typedef axiom name;
wenzelm
parents: 36106
diff changeset
   279
          ({rep_type = oldT, abs_type = newT, Rep_name = #1 (Term.dest_Const RepC),
2af69e16eced expose foundational typedef axiom name;
wenzelm
parents: 36106
diff changeset
   280
            Abs_name = #1 (Term.dest_Const AbsC), axiom_name = axiom_name},
49833
1d80798e8d8a discontinued typedef with implicit set_def;
wenzelm
parents: 47702
diff changeset
   281
           {inhabited = inhabited, type_definition = type_definition,
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   282
            Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   283
            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
   284
          Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct});
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   285
      in
70483
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   286
        lthy3
45291
57cd50f98fdc uniform Local_Theory.declaration with explicit params;
wenzelm
parents: 42381
diff changeset
   287
        |> Local_Theory.declaration {syntax = false, pervasive = true}
49835
31f32ec4d766 discontinued typedef with alternative name;
wenzelm
parents: 49834
diff changeset
   288
          (fn phi => put_info full_name (transform_info phi info))
58662
5963cdbad926 module Interpretation is superseded by Plugin;
wenzelm
parents: 58239
diff changeset
   289
        |> Typedef_Plugin.data Plugin_Name.default_filter full_name
49835
31f32ec4d766 discontinued typedef with alternative name;
wenzelm
parents: 49834
diff changeset
   290
        |> pair (full_name, info)
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   291
      end;
11426
f280d4b29a2c abtract non-emptiness statements (no longer use Eps);
wenzelm
parents: 10697
diff changeset
   292
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   293
  in ((goal, goal_pat, typedef_result), alias_lthy) end
30345
76fd85bbf139 more uniform handling of binding in packages;
wenzelm
parents: 29579
diff changeset
   294
  handle ERROR msg =>
42381
309ec68442c6 added Binding.print convenience, which includes quote already;
wenzelm
parents: 42375
diff changeset
   295
    cat_error msg ("The error(s) above occurred in typedef " ^ Binding.print name);
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   296
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   297
29056
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 29054
diff changeset
   298
(* add_typedef: tactic interface *)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   299
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   300
fun add_typedef overloaded typ set opt_bindings tac lthy =
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   301
  let
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   302
    val ((goal, _, typedef_result), lthy') =
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   303
      prepare_typedef Syntax.check_term overloaded typ set opt_bindings lthy;
70483
59250a328113 clarified proofterms;
wenzelm
parents: 69829
diff changeset
   304
    val inhabited = Goal.prove lthy' [] [] goal (tac o #context) |> Goal.norm_result lthy';
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   305
  in typedef_result inhabited lthy' end;
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   306
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   307
fun add_typedef_global overloaded typ set opt_bindings tac =
69829
3bfa28b3a5b2 streamlined specification interfaces
haftmann
parents: 68264
diff changeset
   308
  Named_Target.theory_map_result (apsnd o transform_info)
3bfa28b3a5b2 streamlined specification interfaces
haftmann
parents: 68264
diff changeset
   309
    (add_typedef overloaded typ set opt_bindings tac)
4866
72a46bd00c8d renamed from typedef.ML;
wenzelm
parents:
diff changeset
   310
17339
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   311
29056
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 29054
diff changeset
   312
(* typedef: proof interface *)
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   313
17339
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   314
local
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   315
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   316
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
   317
  let
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35836
diff changeset
   318
    val args = map (apsnd (prep_constraint lthy)) raw_args;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   319
    val ((goal, goal_pat, typedef_result), lthy') =
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   320
      prepare_typedef prep_term overloaded (b, args, mx) set opt_bindings lthy;
35741
4f3660a3e5af localized typedef;
wenzelm
parents: 35682
diff changeset
   321
    fun after_qed [[th]] = snd o typedef_result th;
36323
655e2d74de3a modernized naming conventions of main Isar proof elements;
wenzelm
parents: 36153
diff changeset
   322
  in Proof.theorem NONE after_qed [[(goal, [goal_pat])]] lthy' end;
17339
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   323
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   324
in
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   325
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35836
diff changeset
   326
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
   327
val typedef_cmd = gen_typedef Syntax.read_term Typedecl.read_constraint;
17339
ab97ccef124a tuned Isar interfaces;
wenzelm
parents: 17280
diff changeset
   328
19705
08de66826677 simplified info/get_info;
wenzelm
parents: 19617
diff changeset
   329
end;
15259
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
   330
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
   331
6aa593317905 Added simple code generator.
berghofe
parents: 14981
diff changeset
   332
6383
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   333
(** outer syntax **)
45bb139e6ceb actually check non-emptiness theorem;
wenzelm
parents: 6357
diff changeset
   334
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24861
diff changeset
   335
val _ =
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
   336
  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
   337
    "HOL type definition (requires non-emptiness proof)"
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   338
    (Parse_Spec.overloaded -- Parse.type_args_constrained -- Parse.binding -- Parse.opt_mixfix --
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
   339
      (\<^keyword>\<open>=\<close> |-- Parse.term) --
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 63019
diff changeset
   340
      Scan.option (\<^keyword>\<open>morphisms\<close> |-- Parse.!!! (Parse.binding -- Parse.binding))
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   341
    >> (fn (((((overloaded, vs), t), mx), A), opt_morphs) => fn lthy =>
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   342
        typedef_cmd {overloaded = overloaded} (t, vs, mx) A
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   343
          (SOME (make_morphisms t opt_morphs)) lthy));
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   344
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   345
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61259
diff changeset
   346
val overloaded = typedef_overloaded;
6357
12448b8f92fb named witnesses: PureThy.get_thmss;
wenzelm
parents: 6092
diff changeset
   347
68264
bb9a3be6952a more exports;
wenzelm
parents: 67664
diff changeset
   348
bb9a3be6952a more exports;
wenzelm
parents: 67664
diff changeset
   349
bb9a3be6952a more exports;
wenzelm
parents: 67664
diff changeset
   350
(** theory export **)
bb9a3be6952a more exports;
wenzelm
parents: 67664
diff changeset
   351
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
   352
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
   353
  (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
   354
    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
   355
      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
   356
        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
   357
        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
   358
          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
   359
          |> 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
   360
              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
   361
              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
   362
                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
   363
                |> 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
   364
                  (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
   365
        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
   366
          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
   367
          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
   368
      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
   369
        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
   370
        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
   371
      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
   372
    else ());
68264
bb9a3be6952a more exports;
wenzelm
parents: 67664
diff changeset
   373
29056
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 29054
diff changeset
   374
end;