author | wenzelm |
Fri, 12 Oct 2012 18:58:20 +0200 | |
changeset 49834 | b27bbb021df1 |
parent 49833 | 1d80798e8d8a |
child 49835 | 31f32ec4d766 |
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 |
|
18 |
val interpretation: (string -> theory -> theory) -> theory -> theory |
|
19 |
val setup: theory -> theory |
|
49833 | 20 |
val add_typedef: binding option -> binding * (string * sort) list * mixfix -> |
35741 | 21 |
term -> (binding * binding) option -> tactic -> local_theory -> (string * info) * local_theory |
49833 | 22 |
val add_typedef_global: binding option -> binding * (string * sort) list * mixfix -> |
30345 | 23 |
term -> (binding * binding) option -> tactic -> theory -> (string * info) * theory |
49833 | 24 |
val typedef: binding * (binding * (string * sort) list * mixfix) * term * |
35741 | 25 |
(binding * binding) option -> local_theory -> Proof.state |
49833 | 26 |
val typedef_cmd: binding * (binding * (string * string option) list * mixfix) * string * |
35741 | 27 |
(binding * binding) option -> local_theory -> Proof.state |
4866 | 28 |
end; |
29 |
||
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
30345
diff
changeset
|
30 |
structure Typedef: TYPEDEF = |
4866 | 31 |
struct |
32 |
||
17922 | 33 |
(** type definitions **) |
34 |
||
35 |
(* theory data *) |
|
15259 | 36 |
|
19705 | 37 |
type info = |
35994
9cc3df9a606e
Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents:
35840
diff
changeset
|
38 |
(*global part*) |
36107 | 39 |
{rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, axiom_name: string} * |
35741 | 40 |
(*local part*) |
49833 | 41 |
{inhabited: thm, type_definition: thm, Rep: thm, Rep_inverse: thm, Abs_inverse: thm, |
42 |
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
|
43 |
Rep_induct: thm, Abs_induct: thm}; |
19459 | 44 |
|
35741 | 45 |
fun transform_info phi (info: info) = |
46 |
let |
|
47 |
val thm = Morphism.thm phi; |
|
49833 | 48 |
val (global_info, {inhabited, type_definition, Rep, Rep_inverse, Abs_inverse, |
49 |
Rep_inject, Abs_inject, Rep_cases, Abs_cases, Rep_induct, Abs_induct}) = info; |
|
35741 | 50 |
in |
35994
9cc3df9a606e
Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents:
35840
diff
changeset
|
51 |
(global_info, |
9cc3df9a606e
Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents:
35840
diff
changeset
|
52 |
{inhabited = thm inhabited, type_definition = thm type_definition, |
49833 | 53 |
Rep = thm Rep, Rep_inverse = thm Rep_inverse, Abs_inverse = thm Abs_inverse, |
54 |
Rep_inject = thm Rep_inject, Abs_inject = thm Abs_inject, |
|
55 |
Rep_cases = thm Rep_cases, Abs_cases = thm Abs_cases, |
|
56 |
Rep_induct = thm Rep_induct, Abs_induct = thm Abs_induct}) |
|
35741 | 57 |
end; |
58 |
||
59 |
structure Data = Generic_Data |
|
22846 | 60 |
( |
35741 | 61 |
type T = info list Symtab.table; |
15259 | 62 |
val empty = Symtab.empty; |
16458 | 63 |
val extend = I; |
35741 | 64 |
fun merge data = Symtab.merge_list (K true) data; |
22846 | 65 |
); |
15259 | 66 |
|
35741 | 67 |
val get_info = Symtab.lookup_list o Data.get o Context.Proof; |
68 |
val get_info_global = Symtab.lookup_list o Data.get o Context.Theory; |
|
69 |
||
70 |
fun put_info name info = Data.map (Symtab.cons_list (name, info)); |
|
71 |
||
72 |
||
73 |
(* global interpretation *) |
|
74 |
||
75 |
structure Typedef_Interpretation = Interpretation(type T = string val eq = op =); |
|
76 |
val interpretation = Typedef_Interpretation.interpretation; |
|
77 |
||
78 |
val setup = Typedef_Interpretation.init; |
|
79 |
||
80 |
||
81 |
(* primitive typedef axiomatization -- for fresh typedecl *) |
|
82 |
||
83 |
fun mk_inhabited A = |
|
84 |
let val T = HOLogic.dest_setT (Term.fastype_of A) |
|
85 |
in HOLogic.mk_Trueprop (HOLogic.exists_const T $ Abs ("x", T, HOLogic.mk_mem (Bound 0, A))) end; |
|
86 |
||
87 |
fun mk_typedef newT oldT RepC AbsC A = |
|
88 |
let |
|
89 |
val typedefC = |
|
90 |
Const (@{const_name type_definition}, |
|
91 |
(newT --> oldT) --> (oldT --> newT) --> HOLogic.mk_setT oldT --> HOLogic.boolT); |
|
92 |
in Logic.mk_implies (mk_inhabited A, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ A)) end; |
|
35134 | 93 |
|
42375
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
94 |
fun primitive_typedef typedef_name newT oldT Rep_name Abs_name A lthy = |
35741 | 95 |
let |
96 |
(* errors *) |
|
97 |
||
98 |
fun show_names pairs = commas_quote (map fst pairs); |
|
99 |
||
100 |
val lhs_tfrees = Term.add_tfreesT newT []; |
|
101 |
val rhs_tfrees = Term.add_tfreesT oldT []; |
|
102 |
val _ = |
|
103 |
(case fold (remove (op =)) lhs_tfrees rhs_tfrees of [] => () |
|
104 |
| extras => error ("Extra type variables in representing set: " ^ show_names extras)); |
|
105 |
||
106 |
val _ = |
|
107 |
(case Term.add_frees A [] of [] => [] |
|
108 |
| xs => error ("Illegal variables in representing set: " ^ show_names xs)); |
|
35134 | 109 |
|
35741 | 110 |
|
111 |
(* axiomatization *) |
|
112 |
||
42375
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
113 |
val ((RepC, AbsC), consts_lthy) = lthy |
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
114 |
|> Local_Theory.background_theory_result |
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
115 |
(Sign.declare_const lthy ((Rep_name, newT --> oldT), NoSyn) ##>> |
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
116 |
Sign.declare_const lthy ((Abs_name, oldT --> newT), NoSyn)); |
35741 | 117 |
|
118 |
val typedef_deps = Term.add_consts A []; |
|
119 |
||
42375
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
120 |
val ((axiom_name, axiom), axiom_lthy) = consts_lthy |
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
121 |
|> Local_Theory.background_theory_result |
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
122 |
(Thm.add_axiom consts_lthy (typedef_name, mk_typedef newT oldT RepC AbsC A) ##> |
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
123 |
Theory.add_deps consts_lthy "" (dest_Const RepC) typedef_deps ##> |
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
124 |
Theory.add_deps consts_lthy "" (dest_Const AbsC) typedef_deps); |
35741 | 125 |
|
42375
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42361
diff
changeset
|
126 |
in ((RepC, AbsC, axiom_name, axiom), axiom_lthy) end; |
15259 | 127 |
|
128 |
||
6383 | 129 |
(* prepare_typedef *) |
130 |
||
49833 | 131 |
fun prepare_typedef prep_term name (tname, raw_args, mx) raw_set opt_morphs lthy = |
4866 | 132 |
let |
35741 | 133 |
val full_name = Local_Theory.full_name lthy name; |
30345 | 134 |
val bname = Binding.name_of name; |
4866 | 135 |
|
35741 | 136 |
|
137 |
(* rhs *) |
|
138 |
||
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
|
139 |
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
|
140 |
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
|
141 |
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
|
142 |
|
21352 | 143 |
val setT = Term.fastype_of set; |
35741 | 144 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
145 |
error ("Not a set type: " ^ quote (Syntax.string_of_typ lthy setT)); |
|
146 |
||
147 |
val goal = mk_inhabited set; |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
38757
diff
changeset
|
148 |
val goal_pat = mk_inhabited (Var (the_default (bname, 0) (Lexicon.read_variable bname), setT)); |
35741 | 149 |
|
150 |
||
151 |
(* lhs *) |
|
152 |
||
42361 | 153 |
val args = map (Proof_Context.check_tfree tmp_ctxt') raw_args; |
35741 | 154 |
val (newT, typedecl_lthy) = lthy |
35836
9380fab5f4f7
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints;
wenzelm
parents:
35766
diff
changeset
|
155 |
|> Typedecl.typedecl (tname, args, mx) |
35741 | 156 |
||> Variable.declare_term set; |
157 |
||
158 |
val Type (full_tname, type_args) = newT; |
|
159 |
val lhs_tfrees = map Term.dest_TFree type_args; |
|
160 |
||
161 |
||
162 |
(* axiomatization *) |
|
4866 | 163 |
|
30345 | 164 |
val (Rep_name, Abs_name) = |
165 |
(case opt_morphs of |
|
166 |
NONE => (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name) |
|
167 |
| SOME morphs => morphs); |
|
10280 | 168 |
|
30345 | 169 |
val typedef_name = Binding.prefix_name "type_definition_" name; |
4866 | 170 |
|
49833 | 171 |
val ((RepC, AbsC, axiom_name, typedef), typedef_lthy) = typedecl_lthy |
172 |
|> primitive_typedef typedef_name newT oldT Rep_name Abs_name set; |
|
35741 | 173 |
|
174 |
val alias_lthy = typedef_lthy |
|
175 |
|> Local_Theory.const_alias Rep_name (#1 (Term.dest_Const RepC)) |
|
176 |
|> Local_Theory.const_alias Abs_name (#1 (Term.dest_Const AbsC)); |
|
6383 | 177 |
|
29056 | 178 |
|
35741 | 179 |
(* result *) |
4866 | 180 |
|
35741 | 181 |
fun note_qualify ((b, atts), th) = |
182 |
Local_Theory.note ((Binding.qualify false bname b, map (Attrib.internal o K) atts), [th]) |
|
183 |
#>> (fn (_, [th']) => th'); |
|
4866 | 184 |
|
35741 | 185 |
fun typedef_result inhabited lthy1 = |
186 |
let |
|
42361 | 187 |
val cert = Thm.cterm_of (Proof_Context.theory_of lthy1); |
49833 | 188 |
val typedef' = inhabited RS typedef; |
35741 | 189 |
fun make th = Goal.norm_result (typedef' RS th); |
190 |
val (((((((((((_, [type_definition]), Rep), Rep_inverse), Abs_inverse), Rep_inject), |
|
191 |
Abs_inject), Rep_cases), Abs_cases), Rep_induct), Abs_induct), lthy2) = lthy1 |
|
192 |
|> Local_Theory.note ((typedef_name, []), [typedef']) |
|
193 |
||>> note_qualify ((Rep_name, []), make @{thm type_definition.Rep}) |
|
194 |
||>> note_qualify ((Binding.suffix_name "_inverse" Rep_name, []), |
|
195 |
make @{thm type_definition.Rep_inverse}) |
|
196 |
||>> note_qualify ((Binding.suffix_name "_inverse" Abs_name, []), |
|
197 |
make @{thm type_definition.Abs_inverse}) |
|
198 |
||>> note_qualify ((Binding.suffix_name "_inject" Rep_name, []), |
|
199 |
make @{thm type_definition.Rep_inject}) |
|
200 |
||>> note_qualify ((Binding.suffix_name "_inject" Abs_name, []), |
|
201 |
make @{thm type_definition.Abs_inject}) |
|
202 |
||>> note_qualify ((Binding.suffix_name "_cases" Rep_name, |
|
203 |
[Rule_Cases.case_names [Binding.name_of Rep_name], Induct.cases_pred full_name]), |
|
204 |
make @{thm type_definition.Rep_cases}) |
|
205 |
||>> note_qualify ((Binding.suffix_name "_cases" Abs_name, |
|
206 |
[Rule_Cases.case_names [Binding.name_of Abs_name], Induct.cases_type full_tname]), |
|
207 |
make @{thm type_definition.Abs_cases}) |
|
208 |
||>> note_qualify ((Binding.suffix_name "_induct" Rep_name, |
|
209 |
[Rule_Cases.case_names [Binding.name_of Rep_name], Induct.induct_pred full_name]), |
|
210 |
make @{thm type_definition.Rep_induct}) |
|
211 |
||>> note_qualify ((Binding.suffix_name "_induct" Abs_name, |
|
212 |
[Rule_Cases.case_names [Binding.name_of Abs_name], Induct.induct_type full_tname]), |
|
213 |
make @{thm type_definition.Abs_induct}); |
|
4866 | 214 |
|
35994
9cc3df9a606e
Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents:
35840
diff
changeset
|
215 |
val info = |
36107 | 216 |
({rep_type = oldT, abs_type = newT, Rep_name = #1 (Term.dest_Const RepC), |
217 |
Abs_name = #1 (Term.dest_Const AbsC), axiom_name = axiom_name}, |
|
49833 | 218 |
{inhabited = inhabited, type_definition = type_definition, |
35741 | 219 |
Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse, |
220 |
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
|
221 |
Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct}); |
35741 | 222 |
in |
223 |
lthy2 |
|
45291
57cd50f98fdc
uniform Local_Theory.declaration with explicit params;
wenzelm
parents:
42381
diff
changeset
|
224 |
|> Local_Theory.declaration {syntax = false, pervasive = true} |
57cd50f98fdc
uniform Local_Theory.declaration with explicit params;
wenzelm
parents:
42381
diff
changeset
|
225 |
(fn phi => put_info full_tname (transform_info phi info)) |
38757
2b3e054ae6fc
renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents:
38388
diff
changeset
|
226 |
|> Local_Theory.background_theory (Typedef_Interpretation.data full_tname) |
35741 | 227 |
|> pair (full_tname, info) |
228 |
end; |
|
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
229 |
|
35741 | 230 |
in ((goal, goal_pat, typedef_result), alias_lthy) end |
30345 | 231 |
handle ERROR msg => |
42381
309ec68442c6
added Binding.print convenience, which includes quote already;
wenzelm
parents:
42375
diff
changeset
|
232 |
cat_error msg ("The error(s) above occurred in typedef " ^ Binding.print name); |
4866 | 233 |
|
234 |
||
29056 | 235 |
(* add_typedef: tactic interface *) |
4866 | 236 |
|
49833 | 237 |
fun add_typedef opt_name typ set opt_morphs tac lthy = |
6383 | 238 |
let |
17922 | 239 |
val name = the_default (#1 typ) opt_name; |
35741 | 240 |
val ((goal, _, typedef_result), lthy') = |
49833 | 241 |
prepare_typedef Syntax.check_term name typ set opt_morphs lthy; |
35741 | 242 |
val inhabited = |
243 |
Goal.prove lthy' [] [] goal (K tac) |
|
244 |
|> Goal.norm_result |> Thm.close_derivation; |
|
245 |
in typedef_result inhabited lthy' end; |
|
246 |
||
49833 | 247 |
fun add_typedef_global opt_name typ set opt_morphs tac = |
38388 | 248 |
Named_Target.theory_init |
49833 | 249 |
#> add_typedef opt_name typ set opt_morphs tac |
35741 | 250 |
#> Local_Theory.exit_result_global (apsnd o transform_info); |
4866 | 251 |
|
17339 | 252 |
|
29056 | 253 |
(* typedef: proof interface *) |
6383 | 254 |
|
17339 | 255 |
local |
256 |
||
49833 | 257 |
fun gen_typedef prep_term prep_constraint (name, (b, raw_args, mx), set, opt_morphs) lthy = |
11822 | 258 |
let |
35840
01d7c4ba9050
allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents:
35836
diff
changeset
|
259 |
val args = map (apsnd (prep_constraint lthy)) raw_args; |
35741 | 260 |
val ((goal, goal_pat, typedef_result), lthy') = |
49833 | 261 |
prepare_typedef prep_term name (b, args, mx) set opt_morphs lthy; |
35741 | 262 |
fun after_qed [[th]] = snd o typedef_result th; |
36323
655e2d74de3a
modernized naming conventions of main Isar proof elements;
wenzelm
parents:
36153
diff
changeset
|
263 |
in Proof.theorem NONE after_qed [[(goal, [goal_pat])]] lthy' end; |
17339 | 264 |
|
265 |
in |
|
6383 | 266 |
|
35840
01d7c4ba9050
allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents:
35836
diff
changeset
|
267 |
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
|
268 |
val typedef_cmd = gen_typedef Syntax.read_term Typedecl.read_constraint; |
17339 | 269 |
|
19705 | 270 |
end; |
15259 | 271 |
|
272 |
||
273 |
||
6383 | 274 |
(** outer syntax **) |
275 |
||
24867 | 276 |
val _ = |
46961
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
46949
diff
changeset
|
277 |
Outer_Syntax.local_theory_to_proof @{command_spec "typedef"} |
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
46949
diff
changeset
|
278 |
"HOL type definition (requires non-emptiness proof)" |
49834 | 279 |
(Scan.option (@{keyword "("} |-- Parse.binding --| @{keyword ")"}) -- |
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36610
diff
changeset
|
280 |
(Parse.type_args_constrained -- Parse.binding) -- |
46949 | 281 |
Parse.opt_mixfix -- (@{keyword "="} |-- Parse.term) -- |
282 |
Scan.option (@{keyword "morphisms"} |-- Parse.!!! (Parse.binding -- Parse.binding)) |
|
49834 | 283 |
>> (fn (((((opt_name, (args, t)), mx), A), morphs)) => fn lthy => |
284 |
typedef_cmd (the_default t opt_name, (t, args, mx), A, morphs) lthy)); |
|
6357 | 285 |
|
29056 | 286 |
end; |
287 |