| author | wenzelm | 
| Thu, 27 Jun 2024 23:32:24 +0200 | |
| changeset 80433 | 48776d779d94 | 
| parent 78095 | bc42c074e58f | 
| child 80631 | a033b5b6f544 | 
| permissions | -rw-r--r-- | 
| 
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 | 2  | 
Author: Markus Wenzel and Stefan Berghofer, TU Muenchen  | 
| 4866 | 3  | 
|
| 21352 | 4  | 
Gordon/HOL-style type definitions: create a new syntactic type  | 
| 35741 | 5  | 
represented by a non-empty set.  | 
| 4866 | 6  | 
*)  | 
7  | 
||
| 
31723
 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 
haftmann 
parents: 
30345 
diff
changeset
 | 
8  | 
signature TYPEDEF =  | 
| 4866 | 9  | 
sig  | 
| 19705 | 10  | 
type info =  | 
| 36107 | 11  | 
   {rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, axiom_name: string} *
 | 
| 49833 | 12  | 
   {inhabited: thm, type_definition: thm, Rep: thm, Rep_inverse: thm, Abs_inverse: thm,
 | 
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 | 15  | 
val transform_info: morphism -> info -> info  | 
16  | 
val get_info: Proof.context -> string -> info list  | 
|
17  | 
val get_info_global: theory -> string -> info list  | 
|
| 58662 | 18  | 
val interpretation: (string -> local_theory -> local_theory) -> theory -> theory  | 
| 61110 | 19  | 
  type bindings = {Rep_name: binding, Abs_name: binding, type_definition_name: binding}
 | 
20  | 
val default_bindings: binding -> bindings  | 
|
21  | 
val make_bindings: binding -> bindings option -> bindings  | 
|
22  | 
val make_morphisms: binding -> (binding * binding) option -> bindings  | 
|
| 61260 | 23  | 
val overloaded: bool Config.T  | 
24  | 
  val add_typedef: {overloaded: bool} -> binding * (string * sort) list * mixfix ->
 | 
|
| 61110 | 25  | 
term -> bindings option -> (Proof.context -> tactic) -> local_theory ->  | 
| 58959 | 26  | 
(string * info) * local_theory  | 
| 61260 | 27  | 
  val add_typedef_global: {overloaded: bool} -> binding * (string * sort) list * mixfix ->
 | 
| 61110 | 28  | 
term -> bindings option -> (Proof.context -> tactic) -> theory ->  | 
| 58959 | 29  | 
(string * info) * theory  | 
| 61260 | 30  | 
  val typedef: {overloaded: bool} -> binding * (string * sort) list * mixfix ->
 | 
31  | 
term -> bindings option -> local_theory -> Proof.state  | 
|
32  | 
  val typedef_cmd: {overloaded: bool} -> binding * (string * string option) list * mixfix ->
 | 
|
33  | 
string -> bindings option -> local_theory -> Proof.state  | 
|
| 4866 | 34  | 
end;  | 
35  | 
||
| 
31723
 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 
haftmann 
parents: 
30345 
diff
changeset
 | 
36  | 
structure Typedef: TYPEDEF =  | 
| 4866 | 37  | 
struct  | 
38  | 
||
| 17922 | 39  | 
(** type definitions **)  | 
40  | 
||
41  | 
(* theory data *)  | 
|
| 15259 | 42  | 
|
| 19705 | 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 | 45  | 
  {rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, axiom_name: string} *
 | 
| 35741 | 46  | 
(*local part*)  | 
| 49833 | 47  | 
  {inhabited: thm, type_definition: thm, Rep: thm, Rep_inverse: thm, Abs_inverse: thm,
 | 
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 | 50  | 
|
| 35741 | 51  | 
fun transform_info phi (info: info) =  | 
52  | 
let  | 
|
53  | 
val thm = Morphism.thm phi;  | 
|
| 49833 | 54  | 
    val (global_info, {inhabited, type_definition, Rep, Rep_inverse, Abs_inverse,
 | 
55  | 
Rep_inject, Abs_inject, Rep_cases, Abs_cases, Rep_induct, Abs_induct}) = info;  | 
|
| 35741 | 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 | 59  | 
Rep = thm Rep, Rep_inverse = thm Rep_inverse, Abs_inverse = thm Abs_inverse,  | 
60  | 
Rep_inject = thm Rep_inject, Abs_inject = thm Abs_inject,  | 
|
61  | 
Rep_cases = thm Rep_cases, Abs_cases = thm Abs_cases,  | 
|
62  | 
Rep_induct = thm Rep_induct, Abs_induct = thm Abs_induct})  | 
|
| 35741 | 63  | 
end;  | 
64  | 
||
65  | 
structure Data = Generic_Data  | 
|
| 22846 | 66  | 
(  | 
| 35741 | 67  | 
type T = info list Symtab.table;  | 
| 15259 | 68  | 
val empty = Symtab.empty;  | 
| 35741 | 69  | 
fun merge data = Symtab.merge_list (K true) data;  | 
| 22846 | 70  | 
);  | 
| 15259 | 71  | 
|
| 61103 | 72  | 
fun get_info_generic context =  | 
73  | 
Symtab.lookup_list (Data.get context) #>  | 
|
| 67664 | 74  | 
map (transform_info (Morphism.transfer_morphism'' context));  | 
| 35741 | 75  | 
|
| 61103 | 76  | 
val get_info = get_info_generic o Context.Proof;  | 
77  | 
val get_info_global = get_info_generic o Context.Theory;  | 
|
78  | 
||
79  | 
fun put_info name info =  | 
|
80  | 
Data.map (Symtab.cons_list (name, transform_info Morphism.trim_context_morphism info));  | 
|
| 35741 | 81  | 
|
82  | 
||
83  | 
(* global interpretation *)  | 
|
84  | 
||
| 58662 | 85  | 
structure Typedef_Plugin = Plugin(type T = string);  | 
86  | 
||
| 67149 | 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 | 89  | 
fun interpretation f =  | 
90  | 
Typedef_Plugin.interpretation typedef_plugin  | 
|
91  | 
(fn name => fn lthy =>  | 
|
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 | 94  | 
(Name_Space.root_path #> Name_Space.add_path (Long_Name.qualifier name))  | 
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 | 97  | 
|
98  | 
||
99  | 
(* primitive typedef axiomatization -- for fresh typedecl *)  | 
|
100  | 
||
| 67149 | 101  | 
val typedef_overloaded = Attrib.setup_config_bool \<^binding>\<open>typedef_overloaded\<close> (K false);  | 
| 61260 | 102  | 
|
| 35741 | 103  | 
fun mk_inhabited A =  | 
104  | 
let val T = HOLogic.dest_setT (Term.fastype_of A)  | 
|
105  | 
  in HOLogic.mk_Trueprop (HOLogic.exists_const T $ Abs ("x", T, HOLogic.mk_mem (Bound 0, A))) end;
 | 
|
106  | 
||
107  | 
fun mk_typedef newT oldT RepC AbsC A =  | 
|
108  | 
let  | 
|
109  | 
val typedefC =  | 
|
| 67149 | 110  | 
Const (\<^const_name>\<open>type_definition\<close>,  | 
| 35741 | 111  | 
(newT --> oldT) --> (oldT --> newT) --> HOLogic.mk_setT oldT --> HOLogic.boolT);  | 
112  | 
in Logic.mk_implies (mk_inhabited A, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ A)) end;  | 
|
| 35134 | 113  | 
|
| 61260 | 114  | 
fun primitive_typedef {overloaded} type_definition_name newT oldT Rep_name Abs_name A lthy =
 | 
| 35741 | 115  | 
let  | 
116  | 
(* errors *)  | 
|
117  | 
||
118  | 
fun show_names pairs = commas_quote (map fst pairs);  | 
|
119  | 
||
120  | 
val lhs_tfrees = Term.add_tfreesT newT [];  | 
|
121  | 
val rhs_tfrees = Term.add_tfreesT oldT [];  | 
|
122  | 
val _ =  | 
|
| 61260 | 123  | 
(case fold (remove (op =)) lhs_tfrees rhs_tfrees of  | 
124  | 
[] => ()  | 
|
| 35741 | 125  | 
      | extras => error ("Extra type variables in representing set: " ^ show_names extras));
 | 
126  | 
||
127  | 
val _ =  | 
|
| 61260 | 128  | 
(case Term.add_frees A [] of [] =>  | 
129  | 
[]  | 
|
| 35741 | 130  | 
      | xs => error ("Illegal variables in representing set: " ^ show_names xs));
 | 
| 35134 | 131  | 
|
| 35741 | 132  | 
|
133  | 
(* axiomatization *)  | 
|
134  | 
||
| 
42375
 
774df7c59508
report Name_Space.declare/define, relatively to context;
 
wenzelm 
parents: 
42361 
diff
changeset
 | 
135  | 
val ((RepC, AbsC), consts_lthy) = lthy  | 
| 
 
774df7c59508
report Name_Space.declare/define, relatively to context;
 
wenzelm 
parents: 
42361 
diff
changeset
 | 
136  | 
|> Local_Theory.background_theory_result  | 
| 
 
774df7c59508
report Name_Space.declare/define, relatively to context;
 
wenzelm 
parents: 
42361 
diff
changeset
 | 
137  | 
(Sign.declare_const lthy ((Rep_name, newT --> oldT), NoSyn) ##>>  | 
| 
 
774df7c59508
report Name_Space.declare/define, relatively to context;
 
wenzelm 
parents: 
42361 
diff
changeset
 | 
138  | 
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
 | 
139  | 
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
 | 
140  | 
val defs_context = Proof_Context.defs_context consts_lthy;  | 
| 61247 | 141  | 
|
| 
61255
 
15865e0c5598
eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
 
wenzelm 
parents: 
61248 
diff
changeset
 | 
142  | 
val A_consts = fold_aterms (fn Const c => insert (op =) (const_dep c) | _ => I) A [];  | 
| 61248 | 143  | 
val A_types =  | 
| 
61255
 
15865e0c5598
eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
 
wenzelm 
parents: 
61248 
diff
changeset
 | 
144  | 
(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
 | 
145  | 
val typedef_deps = A_consts @ A_types;  | 
| 61247 | 146  | 
|
| 
61255
 
15865e0c5598
eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
 
wenzelm 
parents: 
61248 
diff
changeset
 | 
147  | 
val newT_dep = Theory.type_dep (dest_Type newT);  | 
| 35741 | 148  | 
|
| 
42375
 
774df7c59508
report Name_Space.declare/define, relatively to context;
 
wenzelm 
parents: 
42361 
diff
changeset
 | 
149  | 
val ((axiom_name, axiom), axiom_lthy) = consts_lthy  | 
| 
 
774df7c59508
report Name_Space.declare/define, relatively to context;
 
wenzelm 
parents: 
42361 
diff
changeset
 | 
150  | 
|> Local_Theory.background_theory_result  | 
| 61110 | 151  | 
(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
 | 
152  | 
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
 | 
153  | 
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
 | 
154  | 
Theory.add_deps defs_context "" (const_dep (dest_Const AbsC)) [newT_dep]);  | 
| 35741 | 155  | 
|
| 61260 | 156  | 
val axiom_defs = Theory.defs_of (Proof_Context.theory_of axiom_lthy);  | 
157  | 
val newT_deps = maps #2 (Defs.get_deps axiom_defs (#1 newT_dep));  | 
|
158  | 
val _ =  | 
|
159  | 
if null newT_deps orelse overloaded orelse Config.get lthy typedef_overloaded then ()  | 
|
160  | 
else  | 
|
161  | 
error (Pretty.string_of (Pretty.chunks  | 
|
162  | 
[Pretty.paragraph  | 
|
163  | 
(Pretty.text "Type definition with open dependencies, use" @  | 
|
164  | 
[Pretty.brk 1, Pretty.str "\"", Pretty.keyword1 "typedef", Pretty.brk 1,  | 
|
165  | 
              Pretty.str "(", Pretty.keyword2 "overloaded", Pretty.str ")\"", Pretty.brk 1] @
 | 
|
166  | 
Pretty.text "or enable configuration option \"typedef_overloaded\" in the context."),  | 
|
167  | 
Pretty.block [Pretty.str " Type:", Pretty.brk 2, Syntax.pretty_typ axiom_lthy newT],  | 
|
168  | 
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
 | 
169  | 
Pretty.commas  | 
| 
 
ddb2da7cb2e4
more explicit Defs.context: use proper name spaces as far as possible;
 
wenzelm 
parents: 
61260 
diff
changeset
 | 
170  | 
(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
 | 
171  | 
in ((RepC, AbsC, axiom_name, axiom), axiom_lthy) end;  | 
| 15259 | 172  | 
|
173  | 
||
| 61110 | 174  | 
(* derived bindings *)  | 
175  | 
||
176  | 
type bindings = {Rep_name: binding, Abs_name: binding, type_definition_name: binding};
 | 
|
177  | 
||
| 
62513
 
702085ca8564
take qualification of type name more seriously: derived consts and facts are qualified uniformly;
 
wenzelm 
parents: 
61261 
diff
changeset
 | 
178  | 
fun prefix_binding prfx name =  | 
| 63003 | 179  | 
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
 | 
180  | 
|
| 
 
702085ca8564
take qualification of type name more seriously: derived consts and facts are qualified uniformly;
 
wenzelm 
parents: 
61261 
diff
changeset
 | 
181  | 
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
 | 
182  | 
|
| 61110 | 183  | 
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
 | 
184  | 
 {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
 | 
185  | 
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
 | 
186  | 
type_definition_name = prefix_binding "type_definition_" name};  | 
| 61110 | 187  | 
|
188  | 
fun make_bindings name NONE = default_bindings name  | 
|
189  | 
| make_bindings _ (SOME bindings) = bindings;  | 
|
190  | 
||
191  | 
fun make_morphisms name NONE = default_bindings name  | 
|
192  | 
| 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
 | 
193  | 
     {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
 | 
194  | 
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
 | 
195  | 
type_definition_name = #type_definition_name (default_bindings name)};  | 
| 61110 | 196  | 
|
197  | 
||
| 6383 | 198  | 
(* prepare_typedef *)  | 
199  | 
||
| 61260 | 200  | 
fun prepare_typedef prep_term overloaded (name, raw_args, mx) raw_set opt_bindings lthy =  | 
| 4866 | 201  | 
let  | 
| 35741 | 202  | 
(* rhs *)  | 
203  | 
||
| 
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
 | 
204  | 
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
 | 
205  | 
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
 | 
206  | 
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
 | 
207  | 
|
| 21352 | 208  | 
val setT = Term.fastype_of set;  | 
| 35741 | 209  | 
val oldT = HOLogic.dest_setT setT handle TYPE _ =>  | 
210  | 
      error ("Not a set type: " ^ quote (Syntax.string_of_typ lthy setT));
 | 
|
211  | 
||
| 
62513
 
702085ca8564
take qualification of type name more seriously: derived consts and facts are qualified uniformly;
 
wenzelm 
parents: 
61261 
diff
changeset
 | 
212  | 
val bname = Binding.name_of name;  | 
| 35741 | 213  | 
val goal = mk_inhabited set;  | 
| 
42290
 
b1f544c84040
discontinued special treatment of structure Lexicon;
 
wenzelm 
parents: 
38757 
diff
changeset
 | 
214  | 
val goal_pat = mk_inhabited (Var (the_default (bname, 0) (Lexicon.read_variable bname), setT));  | 
| 35741 | 215  | 
|
216  | 
||
217  | 
(* lhs *)  | 
|
218  | 
||
| 42361 | 219  | 
val args = map (Proof_Context.check_tfree tmp_ctxt') raw_args;  | 
| 35741 | 220  | 
val (newT, typedecl_lthy) = lthy  | 
| 61259 | 221  | 
      |> Typedecl.typedecl {final = false} (name, args, mx)
 | 
| 35741 | 222  | 
||> Variable.declare_term set;  | 
223  | 
||
| 
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
 | 
224  | 
val Type (full_name, _) = newT;  | 
| 35741 | 225  | 
|
226  | 
||
227  | 
(* axiomatization *)  | 
|
| 4866 | 228  | 
|
| 61110 | 229  | 
    val {Rep_name, Abs_name, type_definition_name} = make_bindings name opt_bindings;
 | 
| 4866 | 230  | 
|
| 49833 | 231  | 
val ((RepC, AbsC, axiom_name, typedef), typedef_lthy) = typedecl_lthy  | 
| 61260 | 232  | 
|> primitive_typedef overloaded type_definition_name newT oldT Rep_name Abs_name set;  | 
| 35741 | 233  | 
|
234  | 
val alias_lthy = typedef_lthy  | 
|
235  | 
|> Local_Theory.const_alias Rep_name (#1 (Term.dest_Const RepC))  | 
|
236  | 
|> Local_Theory.const_alias Abs_name (#1 (Term.dest_Const AbsC));  | 
|
| 6383 | 237  | 
|
| 29056 | 238  | 
|
| 35741 | 239  | 
(* result *)  | 
| 4866 | 240  | 
|
| 
62513
 
702085ca8564
take qualification of type name more seriously: derived consts and facts are qualified uniformly;
 
wenzelm 
parents: 
61261 
diff
changeset
 | 
241  | 
fun note ((b, atts), th) =  | 
| 63019 | 242  | 
Local_Theory.note ((b, atts), [th]) #>> (fn (_, [th']) => th');  | 
| 4866 | 243  | 
|
| 35741 | 244  | 
fun typedef_result inhabited lthy1 =  | 
245  | 
let  | 
|
| 70483 | 246  | 
val ((_, [type_definition]), lthy2) = lthy1  | 
247  | 
|> Local_Theory.note ((type_definition_name, []), [inhabited RS typedef]);  | 
|
248  | 
fun make th = Goal.norm_result lthy2 (type_definition RS th);  | 
|
249  | 
val (((((((((Rep, Rep_inverse), Abs_inverse), Rep_inject), Abs_inject), Rep_cases),  | 
|
250  | 
Abs_cases), Rep_induct), Abs_induct), lthy3) = lthy2  | 
|
251  | 
          |> 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
 | 
252  | 
||>> note ((Binding.suffix_name "_inverse" Rep_name, []),  | 
| 35741 | 253  | 
              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
 | 
254  | 
||>> note ((Binding.suffix_name "_inverse" Abs_name, []),  | 
| 35741 | 255  | 
              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
 | 
256  | 
||>> note ((Binding.suffix_name "_inject" Rep_name, []),  | 
| 35741 | 257  | 
              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
 | 
258  | 
||>> note ((Binding.suffix_name "_inject" Abs_name, []),  | 
| 35741 | 259  | 
              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
 | 
260  | 
||>> note ((Binding.suffix_name "_cases" Rep_name,  | 
| 63019 | 261  | 
[Attrib.case_names [Binding.name_of Rep_name],  | 
| 78095 | 262  | 
Attrib.internal \<^here> (K (Induct.cases_pred full_name))]),  | 
| 35741 | 263  | 
              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
 | 
264  | 
||>> note ((Binding.suffix_name "_cases" Abs_name,  | 
| 63019 | 265  | 
[Attrib.case_names [Binding.name_of Abs_name],  | 
| 78095 | 266  | 
Attrib.internal \<^here> (K (Induct.cases_type full_name))]),  | 
| 35741 | 267  | 
              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
 | 
268  | 
||>> note ((Binding.suffix_name "_induct" Rep_name,  | 
| 63019 | 269  | 
[Attrib.case_names [Binding.name_of Rep_name],  | 
| 78095 | 270  | 
Attrib.internal \<^here> (K (Induct.induct_pred full_name))]),  | 
| 35741 | 271  | 
              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
 | 
272  | 
||>> note ((Binding.suffix_name "_induct" Abs_name,  | 
| 63019 | 273  | 
[Attrib.case_names [Binding.name_of Abs_name],  | 
| 78095 | 274  | 
Attrib.internal \<^here> (K (Induct.induct_type full_name))]),  | 
| 35741 | 275  | 
              make @{thm type_definition.Abs_induct});
 | 
| 4866 | 276  | 
|
| 
35994
 
9cc3df9a606e
Typedef.info: separate global and local part, only the latter is transformed by morphisms;
 
wenzelm 
parents: 
35840 
diff
changeset
 | 
277  | 
val info =  | 
| 36107 | 278  | 
          ({rep_type = oldT, abs_type = newT, Rep_name = #1 (Term.dest_Const RepC),
 | 
279  | 
Abs_name = #1 (Term.dest_Const AbsC), axiom_name = axiom_name},  | 
|
| 49833 | 280  | 
           {inhabited = inhabited, type_definition = type_definition,
 | 
| 35741 | 281  | 
Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,  | 
282  | 
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
 | 
283  | 
Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct});  | 
| 35741 | 284  | 
in  | 
| 70483 | 285  | 
lthy3  | 
| 78095 | 286  | 
        |> Local_Theory.declaration {syntax = false, pervasive = true, pos = \<^here>}
 | 
| 49835 | 287  | 
(fn phi => put_info full_name (transform_info phi info))  | 
| 58662 | 288  | 
|> Typedef_Plugin.data Plugin_Name.default_filter full_name  | 
| 49835 | 289  | 
|> pair (full_name, info)  | 
| 35741 | 290  | 
end;  | 
| 
11426
 
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
 
wenzelm 
parents: 
10697 
diff
changeset
 | 
291  | 
|
| 35741 | 292  | 
in ((goal, goal_pat, typedef_result), alias_lthy) end  | 
| 30345 | 293  | 
handle ERROR msg =>  | 
| 
42381
 
309ec68442c6
added Binding.print convenience, which includes quote already;
 
wenzelm 
parents: 
42375 
diff
changeset
 | 
294  | 
    cat_error msg ("The error(s) above occurred in typedef " ^ Binding.print name);
 | 
| 4866 | 295  | 
|
296  | 
||
| 29056 | 297  | 
(* add_typedef: tactic interface *)  | 
| 4866 | 298  | 
|
| 61260 | 299  | 
fun add_typedef overloaded typ set opt_bindings tac lthy =  | 
| 6383 | 300  | 
let  | 
| 35741 | 301  | 
val ((goal, _, typedef_result), lthy') =  | 
| 61260 | 302  | 
prepare_typedef Syntax.check_term overloaded typ set opt_bindings lthy;  | 
| 70483 | 303  | 
val inhabited = Goal.prove lthy' [] [] goal (tac o #context) |> Goal.norm_result lthy';  | 
| 35741 | 304  | 
in typedef_result inhabited lthy' end;  | 
305  | 
||
| 61260 | 306  | 
fun add_typedef_global overloaded typ set opt_bindings tac =  | 
| 69829 | 307  | 
Named_Target.theory_map_result (apsnd o transform_info)  | 
308  | 
(add_typedef overloaded typ set opt_bindings tac)  | 
|
| 4866 | 309  | 
|
| 17339 | 310  | 
|
| 29056 | 311  | 
(* typedef: proof interface *)  | 
| 6383 | 312  | 
|
| 17339 | 313  | 
local  | 
314  | 
||
| 61260 | 315  | 
fun gen_typedef prep_term prep_constraint overloaded (b, raw_args, mx) set opt_bindings lthy =  | 
| 11822 | 316  | 
let  | 
| 
35840
 
01d7c4ba9050
allow sort constraints in HOL/typedef and related HOLCF variants;
 
wenzelm 
parents: 
35836 
diff
changeset
 | 
317  | 
val args = map (apsnd (prep_constraint lthy)) raw_args;  | 
| 35741 | 318  | 
val ((goal, goal_pat, typedef_result), lthy') =  | 
| 61260 | 319  | 
prepare_typedef prep_term overloaded (b, args, mx) set opt_bindings lthy;  | 
| 35741 | 320  | 
fun after_qed [[th]] = snd o typedef_result th;  | 
| 
36323
 
655e2d74de3a
modernized naming conventions of main Isar proof elements;
 
wenzelm 
parents: 
36153 
diff
changeset
 | 
321  | 
in Proof.theorem NONE after_qed [[(goal, [goal_pat])]] lthy' end;  | 
| 17339 | 322  | 
|
323  | 
in  | 
|
| 6383 | 324  | 
|
| 
35840
 
01d7c4ba9050
allow sort constraints in HOL/typedef and related HOLCF variants;
 
wenzelm 
parents: 
35836 
diff
changeset
 | 
325  | 
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
 | 
326  | 
val typedef_cmd = gen_typedef Syntax.read_term Typedecl.read_constraint;  | 
| 17339 | 327  | 
|
| 19705 | 328  | 
end;  | 
| 15259 | 329  | 
|
330  | 
||
331  | 
||
| 6383 | 332  | 
(** outer syntax **)  | 
333  | 
||
| 24867 | 334  | 
val _ =  | 
| 67149 | 335  | 
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
 | 
336  | 
"HOL type definition (requires non-emptiness proof)"  | 
| 61260 | 337  | 
(Parse_Spec.overloaded -- Parse.type_args_constrained -- Parse.binding -- Parse.opt_mixfix --  | 
| 67149 | 338  | 
(\<^keyword>\<open>=\<close> |-- Parse.term) --  | 
339  | 
Scan.option (\<^keyword>\<open>morphisms\<close> |-- Parse.!!! (Parse.binding -- Parse.binding))  | 
|
| 61260 | 340  | 
>> (fn (((((overloaded, vs), t), mx), A), opt_morphs) => fn lthy =>  | 
341  | 
        typedef_cmd {overloaded = overloaded} (t, vs, mx) A
 | 
|
342  | 
(SOME (make_morphisms t opt_morphs)) lthy));  | 
|
343  | 
||
344  | 
||
345  | 
val overloaded = typedef_overloaded;  | 
|
| 6357 | 346  | 
|
| 68264 | 347  | 
|
348  | 
||
349  | 
(** theory export **)  | 
|
350  | 
||
| 
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
 | 
351  | 
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
 | 
352  | 
(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
 | 
353  | 
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
 | 
354  | 
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
 | 
355  | 
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
 | 
356  | 
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
 | 
357  | 
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
 | 
358  | 
|> 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
 | 
359  | 
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
 | 
360  | 
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
 | 
361  | 
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
 | 
362  | 
                |> 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
 | 
363  | 
(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
 | 
364  | 
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
 | 
365  | 
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
 | 
366  | 
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
 | 
367  | 
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
 | 
368  | 
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
 | 
369  | 
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
 | 
370  | 
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
 | 
371  | 
else ());  | 
| 68264 | 372  | 
|
| 29056 | 373  | 
end;  |