author | wenzelm |
Sun, 30 Apr 2006 22:50:03 +0200 | |
changeset 19510 | 29fc4e5a638c |
parent 19473 | d87a8838afa4 |
child 19585 | 70a1ce3b23ae |
permissions | -rw-r--r-- |
4866 | 1 |
(* Title: HOL/Tools/typedef_package.ML |
2 |
ID: $Id$ |
|
16458 | 3 |
Author: Markus Wenzel and Stefan Berghofer, TU Muenchen |
4866 | 4 |
|
6357 | 5 |
Gordon/HOL-style type definitions. |
4866 | 6 |
*) |
7 |
||
8 |
signature TYPEDEF_PACKAGE = |
|
9 |
sig |
|
5697 | 10 |
val quiet_mode: bool ref |
5104 | 11 |
val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory |
11827 | 12 |
val add_typedef: bool -> string option -> bstring * string list * mixfix -> |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
13 |
string -> (bstring * bstring) option -> tactic -> theory -> |
11822 | 14 |
{type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm, |
15 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm, |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
16 |
Rep_induct: thm, Abs_induct: thm} * theory |
11827 | 17 |
val add_typedef_i: bool -> string option -> bstring * string list * mixfix -> |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
18 |
term -> (bstring * bstring) option -> tactic -> theory -> |
11822 | 19 |
{type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm, |
20 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm, |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
21 |
Rep_induct: thm, Abs_induct: thm} * theory |
17339 | 22 |
val typedef: (bool * string) * (bstring * string list * mixfix) * string |
23 |
* (string * string) option -> theory -> Proof.state |
|
24 |
val typedef_i: (bool * string) * (bstring * string list * mixfix) * term |
|
25 |
* (string * string) option -> theory -> Proof.state |
|
19459 | 26 |
type typedef_info = (typ * typ * string * string) * (thm option * thm * thm) |
27 |
val get_typedef_info: theory -> string -> typedef_info option |
|
18708 | 28 |
val setup: theory -> theory |
4866 | 29 |
end; |
30 |
||
31 |
structure TypedefPackage: TYPEDEF_PACKAGE = |
|
32 |
struct |
|
33 |
||
34 |
||
10280 | 35 |
(** theory context references **) |
36 |
||
11608 | 37 |
val type_definitionN = "Typedef.type_definition"; |
10280 | 38 |
|
13413 | 39 |
val Rep = thm "type_definition.Rep"; |
40 |
val Rep_inverse = thm "type_definition.Rep_inverse"; |
|
41 |
val Abs_inverse = thm "type_definition.Abs_inverse"; |
|
42 |
val Rep_inject = thm "type_definition.Rep_inject"; |
|
43 |
val Abs_inject = thm "type_definition.Abs_inject"; |
|
44 |
val Rep_cases = thm "type_definition.Rep_cases"; |
|
45 |
val Abs_cases = thm "type_definition.Abs_cases"; |
|
46 |
val Rep_induct = thm "type_definition.Rep_induct"; |
|
47 |
val Abs_induct = thm "type_definition.Abs_induct"; |
|
10280 | 48 |
|
49 |
||
50 |
||
17922 | 51 |
(** type declarations **) |
52 |
||
19510 | 53 |
fun HOL_arity (raw_name, args, mx) thy = |
54 |
thy |> AxClass.axiomatize_arity_i |
|
55 |
(Sign.full_name thy (Syntax.type_name raw_name mx), |
|
56 |
replicate (length args) HOLogic.typeS, HOLogic.typeS); |
|
57 |
||
17922 | 58 |
fun add_typedecls decls thy = |
19510 | 59 |
thy |
60 |
|> Theory.add_typedecls decls |
|
61 |
|> can (Theory.assert_super HOL.thy) ? fold HOL_arity decls; |
|
17922 | 62 |
|
63 |
||
64 |
||
65 |
(** type definitions **) |
|
66 |
||
67 |
(* messages *) |
|
68 |
||
69 |
val quiet_mode = ref false; |
|
70 |
fun message s = if ! quiet_mode then () else writeln s; |
|
71 |
||
72 |
||
73 |
(* theory data *) |
|
15259 | 74 |
|
19459 | 75 |
type typedef_info = (typ * typ * string * string) * (thm option * thm * thm); |
76 |
||
16458 | 77 |
structure TypedefData = TheoryDataFun |
78 |
(struct |
|
15259 | 79 |
val name = "HOL/typedef"; |
19459 | 80 |
type T = typedef_info Symtab.table; |
15259 | 81 |
val empty = Symtab.empty; |
82 |
val copy = I; |
|
16458 | 83 |
val extend = I; |
84 |
fun merge _ (tabs: T * T) = Symtab.merge (op =) tabs; |
|
85 |
fun print _ _ = (); |
|
86 |
end); |
|
15259 | 87 |
|
19459 | 88 |
val get_typedef_info = Symtab.lookup o TypedefData.get; |
89 |
||
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
90 |
fun put_typedef newT oldT Abs_name Rep_name def inject inverse = |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
91 |
TypedefData.map (Symtab.update_new (fst (dest_Type newT), |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
92 |
((newT, oldT, Abs_name, Rep_name), (def, inject, inverse)))); |
15259 | 93 |
|
94 |
||
6383 | 95 |
(* prepare_typedef *) |
96 |
||
16458 | 97 |
fun read_term thy used s = |
98 |
#1 (Thm.read_def_cterm (thy, K NONE, K NONE) used true (s, HOLogic.typeT)); |
|
4866 | 99 |
|
16458 | 100 |
fun cert_term thy _ t = Thm.cterm_of thy t handle TERM (msg, _) => error msg; |
6383 | 101 |
|
18678 | 102 |
fun err_in_typedef msg name = |
103 |
cat_error msg ("The error(s) above occurred in typedef " ^ quote name); |
|
6383 | 104 |
|
11822 | 105 |
fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy = |
4866 | 106 |
let |
11608 | 107 |
val _ = Theory.requires thy "Typedef" "typedefs"; |
16458 | 108 |
val full = Sign.full_name thy; |
4866 | 109 |
|
110 |
(*rhs*) |
|
10280 | 111 |
val full_name = full name; |
16458 | 112 |
val cset = prep_term thy vs raw_set; |
6383 | 113 |
val {T = setT, t = set, ...} = Thm.rep_cterm cset; |
17280 | 114 |
val rhs_tfrees = Term.add_tfrees set []; |
115 |
val rhs_tfreesT = Term.add_tfreesT setT []; |
|
4866 | 116 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
16458 | 117 |
error ("Not a set type: " ^ quote (Sign.string_of_typ thy setT)); |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
118 |
fun mk_nonempty A = |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
119 |
HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A))); |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
120 |
val goal = mk_nonempty set; |
19473 | 121 |
val goal_pat = mk_nonempty (Var (the_default (name, 0) (Syntax.read_variable name), setT)); |
4866 | 122 |
|
123 |
(*lhs*) |
|
17280 | 124 |
val defS = Sign.defaultS thy; |
19473 | 125 |
val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs; |
17280 | 126 |
val args_setT = lhs_tfrees |
127 |
|> filter (member (op =) rhs_tfrees andf (not o member (op =) rhs_tfreesT)) |
|
128 |
|> map TFree; |
|
129 |
||
4866 | 130 |
val tname = Syntax.type_name t mx; |
10280 | 131 |
val full_tname = full tname; |
132 |
val newT = Type (full_tname, map TFree lhs_tfrees); |
|
4866 | 133 |
|
19473 | 134 |
val (Rep_name, Abs_name) = the_default ("Rep_" ^ name, "Abs_" ^ name) opt_morphs; |
19391 | 135 |
val setT' = map Term.itselfT args_setT ---> setT; |
17280 | 136 |
val setC = Term.list_comb (Const (full_name, setT'), map Logic.mk_type args_setT); |
10280 | 137 |
val RepC = Const (full Rep_name, newT --> oldT); |
138 |
val AbsC = Const (full Abs_name, oldT --> newT); |
|
4866 | 139 |
val x_new = Free ("x", newT); |
140 |
val y_old = Free ("y", oldT); |
|
10280 | 141 |
|
11822 | 142 |
val set' = if def then setC else set; |
4866 | 143 |
|
10280 | 144 |
val typedef_name = "type_definition_" ^ name; |
145 |
val typedefC = |
|
146 |
Const (type_definitionN, (newT --> oldT) --> (oldT --> newT) --> setT --> HOLogic.boolT); |
|
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
147 |
val typedef_prop = |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
148 |
Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set')); |
4866 | 149 |
|
18358 | 150 |
fun add_def def def' thy = |
151 |
if def |
|
152 |
then |
|
153 |
thy |
|
154 |
|> PureThy.add_defs_i false [Thm.no_attributes def'] |
|
155 |
|-> (fn [def'] => pair (SOME def')) |
|
156 |
else |
|
157 |
(NONE, thy); |
|
158 |
||
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
159 |
fun typedef_result nonempty context = |
18728 | 160 |
Context.the_theory context |
6383 | 161 |
|> add_typedecls [(t, vs, mx)] |
162 |
|> Theory.add_consts_i |
|
17280 | 163 |
((if def then [(name, setT', NoSyn)] else []) @ |
6383 | 164 |
[(Rep_name, newT --> oldT, NoSyn), |
165 |
(Abs_name, oldT --> newT, NoSyn)]) |
|
18358 | 166 |
|> add_def def (Logic.mk_defpair (setC, set)) |
18377 | 167 |
||>> PureThy.add_axioms_i [((typedef_name, typedef_prop), |
168 |
[apsnd (fn cond_axm => Drule.standard (nonempty RS cond_axm))])] |
|
18358 | 169 |
||> Theory.add_finals_i false [RepC, AbsC] |
170 |
|-> (fn (set_def, [type_definition]) => fn theory' => |
|
11822 | 171 |
let |
172 |
fun make th = Drule.standard (th OF [type_definition]); |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
173 |
val abs_inject = make Abs_inject; |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
174 |
val abs_inverse = make Abs_inverse; |
18377 | 175 |
val ([Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject, |
176 |
Rep_cases, Abs_cases, Rep_induct, Abs_induct], theory'') = |
|
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
177 |
theory' |
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
178 |
|> Theory.add_path name |
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
179 |
|> PureThy.add_thms |
11822 | 180 |
([((Rep_name, make Rep), []), |
181 |
((Rep_name ^ "_inverse", make Rep_inverse), []), |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
182 |
((Abs_name ^ "_inverse", abs_inverse), []), |
11822 | 183 |
((Rep_name ^ "_inject", make Rep_inject), []), |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
184 |
((Abs_name ^ "_inject", abs_inject), []), |
11822 | 185 |
((Rep_name ^ "_cases", make Rep_cases), |
18728 | 186 |
[RuleCases.case_names [Rep_name], InductAttrib.cases_set full_name]), |
11822 | 187 |
((Abs_name ^ "_cases", make Abs_cases), |
18728 | 188 |
[RuleCases.case_names [Abs_name], InductAttrib.cases_type full_tname]), |
11822 | 189 |
((Rep_name ^ "_induct", make Rep_induct), |
18728 | 190 |
[RuleCases.case_names [Rep_name], InductAttrib.induct_set full_name]), |
11822 | 191 |
((Abs_name ^ "_induct", make Abs_induct), |
18728 | 192 |
[RuleCases.case_names [Abs_name], InductAttrib.induct_type full_tname])]) |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
193 |
||> Theory.parent_path |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
194 |
||> put_typedef newT oldT (full Abs_name) (full Rep_name) |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
195 |
set_def abs_inject abs_inverse |
11822 | 196 |
val result = {type_definition = type_definition, set_def = set_def, |
197 |
Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse, |
|
198 |
Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases, |
|
199 |
Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct}; |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
200 |
in ((type_definition, result), Context.Theory theory'') end); |
6383 | 201 |
|
4866 | 202 |
|
203 |
(* errors *) |
|
204 |
||
205 |
fun show_names pairs = commas_quote (map fst pairs); |
|
206 |
||
207 |
val illegal_vars = |
|
208 |
if null (term_vars set) andalso null (term_tvars set) then [] |
|
209 |
else ["Illegal schematic variable(s) on rhs"]; |
|
210 |
||
211 |
val dup_lhs_tfrees = |
|
18964 | 212 |
(case duplicates (op =) lhs_tfrees of [] => [] |
4866 | 213 |
| dups => ["Duplicate type variables on lhs: " ^ show_names dups]); |
214 |
||
215 |
val extra_rhs_tfrees = |
|
17280 | 216 |
(case fold (remove (op =)) lhs_tfrees rhs_tfrees of [] => [] |
4866 | 217 |
| extras => ["Extra type variables on rhs: " ^ show_names extras]); |
218 |
||
219 |
val illegal_frees = |
|
220 |
(case term_frees set of [] => [] |
|
221 |
| xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]); |
|
222 |
||
223 |
val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees; |
|
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
224 |
val _ = if null errs then () else error (cat_lines errs); |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
225 |
|
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
226 |
(*test theory errors now!*) |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
227 |
val test_thy = Theory.copy thy; |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
228 |
val _ = |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
229 |
Context.Theory test_thy |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
230 |
|> typedef_result (setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal); |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
231 |
|
11822 | 232 |
in (cset, goal, goal_pat, typedef_result) end |
18678 | 233 |
handle ERROR msg => err_in_typedef msg name; |
4866 | 234 |
|
235 |
||
6383 | 236 |
(* add_typedef interfaces *) |
4866 | 237 |
|
17339 | 238 |
local |
239 |
||
17922 | 240 |
fun gen_typedef prep_term def opt_name typ set opt_morphs tac thy = |
6383 | 241 |
let |
17922 | 242 |
val name = the_default (#1 typ) opt_name; |
11822 | 243 |
val (cset, goal, _, typedef_result) = |
244 |
prepare_typedef prep_term def name typ set opt_morphs thy; |
|
17922 | 245 |
val _ = message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ..."); |
18678 | 246 |
val non_empty = Goal.prove thy [] [] goal (K tac) handle ERROR msg => |
247 |
cat_error msg ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset)); |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
248 |
in |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
249 |
Context.Theory thy |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
250 |
|> typedef_result non_empty |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
251 |
||> Context.the_theory |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
252 |
|-> (fn (_, result) => pair result) |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
253 |
end; |
4866 | 254 |
|
17339 | 255 |
in |
256 |
||
17922 | 257 |
val add_typedef = gen_typedef read_term; |
258 |
val add_typedef_i = gen_typedef cert_term; |
|
4866 | 259 |
|
17339 | 260 |
end; |
4866 | 261 |
|
17339 | 262 |
|
263 |
(* Isar typedef interface *) |
|
6383 | 264 |
|
17339 | 265 |
local |
266 |
||
267 |
fun gen_typedef prep_term ((def, name), typ, set, opt_morphs) thy = |
|
11822 | 268 |
let |
269 |
val (_, goal, goal_pat, att_result) = |
|
13443 | 270 |
prepare_typedef prep_term def name typ set opt_morphs thy; |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
271 |
fun att (thy, th) = |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
272 |
let val ((th', _), thy') = att_result th thy |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
273 |
in (thy', th') end; |
18799 | 274 |
in IsarThy.theorem_i PureThy.internalK ("", [att]) (goal, ([goal_pat], [])) thy end; |
17339 | 275 |
|
276 |
in |
|
6383 | 277 |
|
17339 | 278 |
val typedef = gen_typedef read_term; |
279 |
val typedef_i = gen_typedef cert_term; |
|
280 |
||
19459 | 281 |
end; (*local*) |
15259 | 282 |
|
19459 | 283 |
val setup = TypedefData.init; |
15259 | 284 |
|
285 |
||
286 |
||
6383 | 287 |
(** outer syntax **) |
288 |
||
17057 | 289 |
local structure P = OuterParse and K = OuterKeyword in |
6383 | 290 |
|
6357 | 291 |
val typedeclP = |
12624 | 292 |
OuterSyntax.command "typedecl" "type declaration (HOL)" K.thy_decl |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12694
diff
changeset
|
293 |
(P.type_args -- P.name -- P.opt_infix >> (fn ((vs, t), mx) => |
6357 | 294 |
Toplevel.theory (add_typedecls [(t, vs, mx)]))); |
295 |
||
6723 | 296 |
|
17339 | 297 |
val typedef_decl = |
16126
3ba9eb7ea366
fixed outer syntax: allow type_args with parentheses;
wenzelm
parents:
15570
diff
changeset
|
298 |
Scan.optional (P.$$$ "(" |-- |
3ba9eb7ea366
fixed outer syntax: allow type_args with parentheses;
wenzelm
parents:
15570
diff
changeset
|
299 |
((P.$$$ "open" >> K false) -- Scan.option P.name || P.name >> (fn s => (true, SOME s))) |
3ba9eb7ea366
fixed outer syntax: allow type_args with parentheses;
wenzelm
parents:
15570
diff
changeset
|
300 |
--| P.$$$ ")") (true, NONE) -- |
11744 | 301 |
(P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) -- |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12694
diff
changeset
|
302 |
Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name)); |
6357 | 303 |
|
17339 | 304 |
fun mk_typedef ((((((def, opt_name), (vs, t)), mx), A), morphs)) = |
19473 | 305 |
typedef ((def, the_default (Syntax.type_name t mx) opt_name), (t, vs, mx), A, morphs); |
6357 | 306 |
|
307 |
val typedefP = |
|
6723 | 308 |
OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal |
17339 | 309 |
(typedef_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef))); |
6357 | 310 |
|
6723 | 311 |
|
11744 | 312 |
val _ = OuterSyntax.add_keywords ["morphisms"]; |
6357 | 313 |
val _ = OuterSyntax.add_parsers [typedeclP, typedefP]; |
314 |
||
4866 | 315 |
end; |
6383 | 316 |
|
317 |
end; |