author | oheimb |
Wed, 03 Apr 2002 10:21:13 +0200 | |
changeset 13076 | 70704dd48bd5 |
parent 12876 | a70df1e5bf10 |
child 13413 | 0b60b9e18a26 |
permissions | -rw-r--r-- |
4866 | 1 |
(* Title: HOL/Tools/typedef_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
9230 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
4866 | 5 |
|
6357 | 6 |
Gordon/HOL-style type definitions. |
4866 | 7 |
*) |
8 |
||
9 |
signature TYPEDEF_PACKAGE = |
|
10 |
sig |
|
5697 | 11 |
val quiet_mode: bool ref |
5104 | 12 |
val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory |
11822 | 13 |
val add_typedef_x: string -> bstring * string list * mixfix -> |
4866 | 14 |
string -> string list -> thm list -> tactic option -> theory -> theory |
11827 | 15 |
val add_typedef: bool -> string option -> bstring * string list * mixfix -> |
11822 | 16 |
string -> (bstring * bstring) option -> tactic -> theory -> theory * |
17 |
{type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm, |
|
18 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm, |
|
19 |
Rep_induct: thm, Abs_induct: thm} |
|
11827 | 20 |
val add_typedef_i: bool -> string option -> bstring * string list * mixfix -> |
11822 | 21 |
term -> (bstring * bstring) option -> tactic -> theory -> theory * |
22 |
{type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm, |
|
23 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm, |
|
24 |
Rep_induct: thm, Abs_induct: thm} |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12694
diff
changeset
|
25 |
val typedef_proof: string * (bstring * string list * mixfix) * string * (string * string) option |
6690 | 26 |
-> bool -> theory -> ProofHistory.T |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12694
diff
changeset
|
27 |
val typedef_proof_i: string * (bstring * string list * mixfix) * term * (string * string) option |
6690 | 28 |
-> bool -> theory -> ProofHistory.T |
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 |
val type_definition_def = thm "type_definition_def"; |
39 |
||
40 |
val Rep = thm "Rep"; |
|
41 |
val Rep_inverse = thm "Rep_inverse"; |
|
42 |
val Abs_inverse = thm "Abs_inverse"; |
|
43 |
val Rep_inject = thm "Rep_inject"; |
|
44 |
val Abs_inject = thm "Abs_inject"; |
|
45 |
val Rep_cases = thm "Rep_cases"; |
|
46 |
val Abs_cases = thm "Abs_cases"; |
|
47 |
val Rep_induct = thm "Rep_induct"; |
|
48 |
val Abs_induct = thm "Abs_induct"; |
|
49 |
||
50 |
||
51 |
||
5104 | 52 |
(** type declarations **) |
53 |
||
54 |
fun add_typedecls decls thy = |
|
55 |
let |
|
56 |
val full = Sign.full_name (Theory.sign_of thy); |
|
57 |
||
58 |
fun arity_of (raw_name, args, mx) = |
|
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
59 |
(full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.typeS, HOLogic.typeS); |
5104 | 60 |
in |
8141 | 61 |
if can (Theory.assert_super HOL.thy) thy then |
62 |
thy |
|
63 |
|> PureThy.add_typedecls decls |
|
64 |
|> Theory.add_arities_i (map arity_of decls) |
|
65 |
else thy |> PureThy.add_typedecls decls |
|
5104 | 66 |
end; |
67 |
||
68 |
||
69 |
||
70 |
(** type definitions **) |
|
71 |
||
5697 | 72 |
(* messages *) |
73 |
||
74 |
val quiet_mode = ref false; |
|
75 |
fun message s = if ! quiet_mode then () else writeln s; |
|
76 |
||
77 |
||
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
78 |
(* prove_nonempty -- tactical version *) (*exception ERROR*) |
4866 | 79 |
|
11827 | 80 |
fun prove_nonempty thy cset goal (witn1_tac, witn_names, witn_thms, witn2_tac) = |
6383 | 81 |
let |
82 |
val is_def = Logic.is_equals o #prop o Thm.rep_thm; |
|
83 |
val thms = PureThy.get_thmss thy witn_names @ witn_thms; |
|
4866 | 84 |
val tac = |
11827 | 85 |
witn1_tac THEN |
4866 | 86 |
TRY (rewrite_goals_tac (filter is_def thms)) THEN |
87 |
TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN |
|
11827 | 88 |
if_none witn2_tac (TRY (ALLGOALS (CLASET' blast_tac))); |
4866 | 89 |
in |
6383 | 90 |
message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ..."); |
11968 | 91 |
Tactic.prove (Theory.sign_of thy) [] [] goal (K tac) |
6383 | 92 |
end handle ERROR => error ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset)); |
4866 | 93 |
|
94 |
||
6383 | 95 |
(* prepare_typedef *) |
96 |
||
97 |
fun read_term sg used s = |
|
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
98 |
#1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.typeT)); |
4866 | 99 |
|
6383 | 100 |
fun cert_term sg _ t = Thm.cterm_of sg t handle TERM (msg, _) => error msg; |
101 |
||
102 |
fun err_in_typedef name = |
|
103 |
error ("The error(s) above occurred in typedef " ^ quote name); |
|
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"; |
6383 | 108 |
val sign = Theory.sign_of thy; |
10280 | 109 |
val full = Sign.full_name sign; |
4866 | 110 |
|
111 |
(*rhs*) |
|
10280 | 112 |
val full_name = full name; |
6383 | 113 |
val cset = prep_term sign vs raw_set; |
114 |
val {T = setT, t = set, ...} = Thm.rep_cterm cset; |
|
4866 | 115 |
val rhs_tfrees = term_tfrees set; |
116 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
|
117 |
error ("Not a set type: " ^ quote (Sign.string_of_typ sign 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; |
11744 | 121 |
val vname = take_suffix Symbol.is_digit (Symbol.explode name) |
122 |
|> apfst implode |> apsnd (#1 o Term.read_int); |
|
123 |
val goal_pat = mk_nonempty (Var (vname, setT)); |
|
4866 | 124 |
|
125 |
(*lhs*) |
|
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
126 |
val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.typeS)) vs; |
4866 | 127 |
val tname = Syntax.type_name t mx; |
10280 | 128 |
val full_tname = full tname; |
129 |
val newT = Type (full_tname, map TFree lhs_tfrees); |
|
4866 | 130 |
|
11744 | 131 |
val (Rep_name, Abs_name) = if_none opt_morphs ("Rep_" ^ name, "Abs_" ^ name); |
10280 | 132 |
val setC = Const (full_name, setT); |
133 |
val RepC = Const (full Rep_name, newT --> oldT); |
|
134 |
val AbsC = Const (full Abs_name, oldT --> newT); |
|
4866 | 135 |
val x_new = Free ("x", newT); |
136 |
val y_old = Free ("y", oldT); |
|
10280 | 137 |
|
11822 | 138 |
val set' = if def then setC else set; |
4866 | 139 |
|
10280 | 140 |
val typedef_name = "type_definition_" ^ name; |
141 |
val typedefC = |
|
142 |
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
|
143 |
val typedef_prop = |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
144 |
Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set')); |
4866 | 145 |
|
11822 | 146 |
fun typedef_result (theory, nonempty) = |
6383 | 147 |
theory |
148 |
|> add_typedecls [(t, vs, mx)] |
|
149 |
|> Theory.add_consts_i |
|
11822 | 150 |
((if def then [(name, setT, NoSyn)] else []) @ |
6383 | 151 |
[(Rep_name, newT --> oldT, NoSyn), |
152 |
(Abs_name, oldT --> newT, NoSyn)]) |
|
11822 | 153 |
|> (if def then (apsnd (Some o hd) oo (PureThy.add_defs_i false o map Thm.no_attributes)) |
154 |
[Logic.mk_defpair (setC, set)] |
|
155 |
else rpair None) |
|
156 |
|>> PureThy.add_axioms_i [((typedef_name, typedef_prop), |
|
11727
a27150cc8fa5
test: use SkipProof.make_thm instead of Thm.assume;
wenzelm
parents:
11608
diff
changeset
|
157 |
[apsnd (fn cond_axm => Drule.standard (nonempty RS cond_axm))])] |
11822 | 158 |
|> (fn ((theory', [type_definition]), set_def) => |
159 |
let |
|
160 |
fun make th = Drule.standard (th OF [type_definition]); |
|
161 |
val (theory'', [Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject, |
|
162 |
Rep_cases, Abs_cases, Rep_induct, Abs_induct]) = |
|
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
163 |
theory' |
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
164 |
|> Theory.add_path name |
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
165 |
|> PureThy.add_thms |
11822 | 166 |
([((Rep_name, make Rep), []), |
167 |
((Rep_name ^ "_inverse", make Rep_inverse), []), |
|
168 |
((Abs_name ^ "_inverse", make Abs_inverse), []), |
|
169 |
((Rep_name ^ "_inject", make Rep_inject), []), |
|
170 |
((Abs_name ^ "_inject", make Abs_inject), []), |
|
171 |
((Rep_name ^ "_cases", make Rep_cases), |
|
172 |
[RuleCases.case_names [Rep_name], InductAttrib.cases_set_global full_name]), |
|
173 |
((Abs_name ^ "_cases", make Abs_cases), |
|
174 |
[RuleCases.case_names [Abs_name], InductAttrib.cases_type_global full_tname]), |
|
175 |
((Rep_name ^ "_induct", make Rep_induct), |
|
176 |
[RuleCases.case_names [Rep_name], InductAttrib.induct_set_global full_name]), |
|
177 |
((Abs_name ^ "_induct", make Abs_induct), |
|
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
178 |
[RuleCases.case_names [Abs_name], InductAttrib.induct_type_global full_tname])]) |
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
179 |
|>> Theory.parent_path; |
11822 | 180 |
val result = {type_definition = type_definition, set_def = set_def, |
181 |
Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse, |
|
182 |
Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases, |
|
183 |
Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct}; |
|
184 |
in ((theory'', type_definition), result) end); |
|
6383 | 185 |
|
4866 | 186 |
|
187 |
(* errors *) |
|
188 |
||
189 |
fun show_names pairs = commas_quote (map fst pairs); |
|
190 |
||
191 |
val illegal_vars = |
|
192 |
if null (term_vars set) andalso null (term_tvars set) then [] |
|
193 |
else ["Illegal schematic variable(s) on rhs"]; |
|
194 |
||
195 |
val dup_lhs_tfrees = |
|
196 |
(case duplicates lhs_tfrees of [] => [] |
|
197 |
| dups => ["Duplicate type variables on lhs: " ^ show_names dups]); |
|
198 |
||
199 |
val extra_rhs_tfrees = |
|
200 |
(case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => [] |
|
201 |
| extras => ["Extra type variables on rhs: " ^ show_names extras]); |
|
202 |
||
203 |
val illegal_frees = |
|
204 |
(case term_frees set of [] => [] |
|
205 |
| xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]); |
|
206 |
||
207 |
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
|
208 |
val _ = if null errs then () else error (cat_lines errs); |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
209 |
|
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
210 |
(*test theory errors now!*) |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
211 |
val test_thy = Theory.copy thy; |
11727
a27150cc8fa5
test: use SkipProof.make_thm instead of Thm.assume;
wenzelm
parents:
11608
diff
changeset
|
212 |
val _ = (test_thy, |
11822 | 213 |
setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal) |> typedef_result; |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
214 |
|
11822 | 215 |
in (cset, goal, goal_pat, typedef_result) end |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
216 |
handle ERROR => err_in_typedef name; |
4866 | 217 |
|
218 |
||
6383 | 219 |
(* add_typedef interfaces *) |
4866 | 220 |
|
11827 | 221 |
fun gen_typedef prep_term def name typ set opt_morphs tac1 names thms tac2 thy = |
6383 | 222 |
let |
11822 | 223 |
val (cset, goal, _, typedef_result) = |
224 |
prepare_typedef prep_term def name typ set opt_morphs thy; |
|
11827 | 225 |
val non_empty = prove_nonempty thy cset goal (tac1, names, thms, tac2); |
11822 | 226 |
val ((thy', _), result) = (thy, non_empty) |> typedef_result; |
227 |
in (thy', result) end; |
|
4866 | 228 |
|
11827 | 229 |
fun sane_typedef prep_term def opt_name typ set opt_morphs tac = |
230 |
gen_typedef prep_term def |
|
231 |
(if_none opt_name (#1 typ)) typ set opt_morphs all_tac [] [] (Some tac); |
|
11822 | 232 |
|
233 |
fun add_typedef_x name typ set names thms tac = |
|
11827 | 234 |
#1 o gen_typedef read_term true name typ set None (Tactic.rtac exI 1) names thms tac; |
11822 | 235 |
|
236 |
val add_typedef = sane_typedef read_term; |
|
237 |
val add_typedef_i = sane_typedef cert_term; |
|
4866 | 238 |
|
239 |
||
6383 | 240 |
(* typedef_proof interface *) |
241 |
||
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12694
diff
changeset
|
242 |
fun gen_typedef_proof prep_term (name, typ, set, opt_morphs) int thy = |
11822 | 243 |
let |
244 |
val (_, goal, goal_pat, att_result) = |
|
245 |
prepare_typedef prep_term true name typ set opt_morphs thy; |
|
246 |
val att = #1 o att_result; |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12694
diff
changeset
|
247 |
in thy |> IsarThy.theorem_i Drule.internalK (("", [att]), (goal, ([goal_pat], []))) int end; |
6383 | 248 |
|
249 |
val typedef_proof = gen_typedef_proof read_term; |
|
250 |
val typedef_proof_i = gen_typedef_proof cert_term; |
|
6357 | 251 |
|
252 |
||
6383 | 253 |
|
254 |
(** outer syntax **) |
|
255 |
||
6723 | 256 |
local structure P = OuterParse and K = OuterSyntax.Keyword in |
6383 | 257 |
|
6357 | 258 |
val typedeclP = |
12624 | 259 |
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
|
260 |
(P.type_args -- P.name -- P.opt_infix >> (fn ((vs, t), mx) => |
6357 | 261 |
Toplevel.theory (add_typedecls [(t, vs, mx)]))); |
262 |
||
6723 | 263 |
|
6383 | 264 |
val typedef_proof_decl = |
6723 | 265 |
Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- |
11744 | 266 |
(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
|
267 |
Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name)); |
6357 | 268 |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12694
diff
changeset
|
269 |
fun mk_typedef_proof ((((opt_name, (vs, t)), mx), A), morphs) = |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12694
diff
changeset
|
270 |
typedef_proof (if_none opt_name (Syntax.type_name t mx), (t, vs, mx), A, morphs); |
6357 | 271 |
|
272 |
val typedefP = |
|
6723 | 273 |
OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal |
6383 | 274 |
(typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof))); |
6357 | 275 |
|
6723 | 276 |
|
11744 | 277 |
val _ = OuterSyntax.add_keywords ["morphisms"]; |
6357 | 278 |
val _ = OuterSyntax.add_parsers [typedeclP, typedefP]; |
279 |
||
4866 | 280 |
end; |
6383 | 281 |
|
282 |
end; |