author | wenzelm |
Tue, 16 Oct 2001 17:55:53 +0200 | |
changeset 11807 | 50a36627e6d6 |
parent 11744 | 3a4625eaead0 |
child 11822 | 122834177ec1 |
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 |
4866 | 13 |
val add_typedef: string -> bstring * string list * mixfix -> |
11807 | 14 |
string -> string list -> thm list -> tactic option -> theory -> theory * thm |
15 |
val add_typedef_no_result: string -> bstring * string list * mixfix -> |
|
4866 | 16 |
string -> string list -> thm list -> tactic option -> theory -> theory |
17 |
val add_typedef_i: string -> bstring * string list * mixfix -> |
|
11807 | 18 |
term -> string list -> thm list -> tactic option -> theory -> theory * thm |
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
19 |
val add_typedef_i_no_def: string -> bstring * string list * mixfix -> |
11807 | 20 |
term -> string list -> thm list -> tactic option -> theory -> theory * thm |
11744 | 21 |
val typedef_proof: |
22 |
(string * (bstring * string list * mixfix) * string * (string * string) option) * Comment.text |
|
6690 | 23 |
-> bool -> theory -> ProofHistory.T |
11744 | 24 |
val typedef_proof_i: |
25 |
(string * (bstring * string list * mixfix) * term * (string * string) option) * Comment.text |
|
6690 | 26 |
-> bool -> theory -> ProofHistory.T |
4866 | 27 |
end; |
28 |
||
29 |
structure TypedefPackage: TYPEDEF_PACKAGE = |
|
30 |
struct |
|
31 |
||
32 |
||
10280 | 33 |
(** theory context references **) |
34 |
||
11608 | 35 |
val type_definitionN = "Typedef.type_definition"; |
10280 | 36 |
val type_definition_def = thm "type_definition_def"; |
37 |
||
38 |
val Rep = thm "Rep"; |
|
39 |
val Rep_inverse = thm "Rep_inverse"; |
|
40 |
val Abs_inverse = thm "Abs_inverse"; |
|
41 |
val Rep_inject = thm "Rep_inject"; |
|
42 |
val Abs_inject = thm "Abs_inject"; |
|
43 |
val Rep_cases = thm "Rep_cases"; |
|
44 |
val Abs_cases = thm "Abs_cases"; |
|
45 |
val Rep_induct = thm "Rep_induct"; |
|
46 |
val Abs_induct = thm "Abs_induct"; |
|
47 |
||
48 |
||
49 |
||
5104 | 50 |
(** type declarations **) |
51 |
||
52 |
fun add_typedecls decls thy = |
|
53 |
let |
|
54 |
val full = Sign.full_name (Theory.sign_of thy); |
|
55 |
||
56 |
fun arity_of (raw_name, args, mx) = |
|
57 |
(full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.termS, HOLogic.termS); |
|
58 |
in |
|
8141 | 59 |
if can (Theory.assert_super HOL.thy) thy then |
60 |
thy |
|
61 |
|> PureThy.add_typedecls decls |
|
62 |
|> Theory.add_arities_i (map arity_of decls) |
|
63 |
else thy |> PureThy.add_typedecls decls |
|
5104 | 64 |
end; |
65 |
||
66 |
||
67 |
||
68 |
(** type definitions **) |
|
69 |
||
5697 | 70 |
(* messages *) |
71 |
||
72 |
val quiet_mode = ref false; |
|
73 |
fun message s = if ! quiet_mode then () else writeln s; |
|
74 |
||
75 |
||
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
76 |
(* prove_nonempty -- tactical version *) (*exception ERROR*) |
4866 | 77 |
|
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
78 |
fun prove_nonempty thy cset goal (witn_names, witn_thms, witn_tac) = |
6383 | 79 |
let |
80 |
val is_def = Logic.is_equals o #prop o Thm.rep_thm; |
|
81 |
val thms = PureThy.get_thmss thy witn_names @ witn_thms; |
|
4866 | 82 |
val tac = |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
83 |
rtac exI 1 THEN |
4866 | 84 |
TRY (rewrite_goals_tac (filter is_def thms)) THEN |
85 |
TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN |
|
6383 | 86 |
if_none witn_tac (TRY (ALLGOALS (CLASET' blast_tac))); |
4866 | 87 |
in |
6383 | 88 |
message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ..."); |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
89 |
prove_goalw_cterm [] (cterm_of (sign_of thy) goal) (K [tac]) |
6383 | 90 |
end handle ERROR => error ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset)); |
4866 | 91 |
|
92 |
||
6383 | 93 |
(* prepare_typedef *) |
94 |
||
95 |
fun read_term sg used s = |
|
8100 | 96 |
#1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.termT)); |
4866 | 97 |
|
6383 | 98 |
fun cert_term sg _ t = Thm.cterm_of sg t handle TERM (msg, _) => error msg; |
99 |
||
100 |
fun err_in_typedef name = |
|
101 |
error ("The error(s) above occurred in typedef " ^ quote name); |
|
102 |
||
11744 | 103 |
fun prepare_typedef prep_term no_def name (t, vs, mx) raw_set opt_morphs thy = |
4866 | 104 |
let |
11608 | 105 |
val _ = Theory.requires thy "Typedef" "typedefs"; |
6383 | 106 |
val sign = Theory.sign_of thy; |
10280 | 107 |
val full = Sign.full_name sign; |
4866 | 108 |
|
109 |
(*rhs*) |
|
10280 | 110 |
val full_name = full name; |
6383 | 111 |
val cset = prep_term sign vs raw_set; |
112 |
val {T = setT, t = set, ...} = Thm.rep_cterm cset; |
|
4866 | 113 |
val rhs_tfrees = term_tfrees set; |
114 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
|
115 |
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
|
116 |
fun mk_nonempty A = |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
117 |
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
|
118 |
val goal = mk_nonempty set; |
11744 | 119 |
val vname = take_suffix Symbol.is_digit (Symbol.explode name) |
120 |
|> apfst implode |> apsnd (#1 o Term.read_int); |
|
121 |
val goal_pat = mk_nonempty (Var (vname, setT)); |
|
4866 | 122 |
|
123 |
(*lhs*) |
|
6383 | 124 |
val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs; |
4866 | 125 |
val tname = Syntax.type_name t mx; |
10280 | 126 |
val full_tname = full tname; |
127 |
val newT = Type (full_tname, map TFree lhs_tfrees); |
|
4866 | 128 |
|
11744 | 129 |
val (Rep_name, Abs_name) = if_none opt_morphs ("Rep_" ^ name, "Abs_" ^ name); |
10280 | 130 |
val setC = Const (full_name, setT); |
131 |
val RepC = Const (full Rep_name, newT --> oldT); |
|
132 |
val AbsC = Const (full Abs_name, oldT --> newT); |
|
4866 | 133 |
val x_new = Free ("x", newT); |
134 |
val y_old = Free ("y", oldT); |
|
10280 | 135 |
|
10615 | 136 |
val set' = if no_def then set else setC; |
4866 | 137 |
|
10280 | 138 |
val typedef_name = "type_definition_" ^ name; |
139 |
val typedefC = |
|
140 |
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
|
141 |
val typedef_prop = |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
142 |
Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set')); |
4866 | 143 |
|
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
144 |
fun typedef_att (theory, nonempty) = |
6383 | 145 |
theory |
146 |
|> add_typedecls [(t, vs, mx)] |
|
147 |
|> Theory.add_consts_i |
|
148 |
((if no_def then [] else [(name, setT, NoSyn)]) @ |
|
149 |
[(Rep_name, newT --> oldT, NoSyn), |
|
150 |
(Abs_name, oldT --> newT, NoSyn)]) |
|
9315 | 151 |
|> (if no_def then I else (#1 oo (PureThy.add_defs_i false o map Thm.no_attributes)) |
6383 | 152 |
[Logic.mk_defpair (setC, set)]) |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
153 |
|> PureThy.add_axioms_i [((typedef_name, typedef_prop), |
11727
a27150cc8fa5
test: use SkipProof.make_thm instead of Thm.assume;
wenzelm
parents:
11608
diff
changeset
|
154 |
[apsnd (fn cond_axm => Drule.standard (nonempty RS cond_axm))])] |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
155 |
|> (fn (theory', axm) => |
11727
a27150cc8fa5
test: use SkipProof.make_thm instead of Thm.assume;
wenzelm
parents:
11608
diff
changeset
|
156 |
let fun make th = Drule.standard (th OF axm) in |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
157 |
rpair (hd axm) (theory' |
10280 | 158 |
|> (#1 oo PureThy.add_thms) |
159 |
([((Rep_name, make Rep), []), |
|
160 |
((Rep_name ^ "_inverse", make Rep_inverse), []), |
|
10615 | 161 |
((Abs_name ^ "_inverse", make Abs_inverse), []), |
162 |
((Rep_name ^ "_inject", make Rep_inject), []), |
|
10280 | 163 |
((Abs_name ^ "_inject", make Abs_inject), []), |
164 |
((Rep_name ^ "_cases", make Rep_cases), |
|
165 |
[RuleCases.case_names [Rep_name], InductAttrib.cases_set_global full_name]), |
|
166 |
((Abs_name ^ "_cases", make Abs_cases), |
|
167 |
[RuleCases.case_names [Abs_name], InductAttrib.cases_type_global full_tname]), |
|
168 |
((Rep_name ^ "_induct", make Rep_induct), |
|
169 |
[RuleCases.case_names [Rep_name], InductAttrib.induct_set_global full_name]), |
|
170 |
((Abs_name ^ "_induct", make Abs_induct), |
|
10675
0b40c19f09f3
'typedef': present result theorem "type_definition Rep Abs A";
wenzelm
parents:
10615
diff
changeset
|
171 |
[RuleCases.case_names [Abs_name], InductAttrib.induct_type_global full_tname])])) |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
172 |
end); |
6383 | 173 |
|
4866 | 174 |
|
175 |
(* errors *) |
|
176 |
||
177 |
fun show_names pairs = commas_quote (map fst pairs); |
|
178 |
||
179 |
val illegal_vars = |
|
180 |
if null (term_vars set) andalso null (term_tvars set) then [] |
|
181 |
else ["Illegal schematic variable(s) on rhs"]; |
|
182 |
||
183 |
val dup_lhs_tfrees = |
|
184 |
(case duplicates lhs_tfrees of [] => [] |
|
185 |
| dups => ["Duplicate type variables on lhs: " ^ show_names dups]); |
|
186 |
||
187 |
val extra_rhs_tfrees = |
|
188 |
(case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => [] |
|
189 |
| extras => ["Extra type variables on rhs: " ^ show_names extras]); |
|
190 |
||
191 |
val illegal_frees = |
|
192 |
(case term_frees set of [] => [] |
|
193 |
| xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]); |
|
194 |
||
195 |
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
|
196 |
val _ = if null errs then () else error (cat_lines errs); |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
197 |
|
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
198 |
(*test theory errors now!*) |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
199 |
val test_thy = Theory.copy thy; |
11727
a27150cc8fa5
test: use SkipProof.make_thm instead of Thm.assume;
wenzelm
parents:
11608
diff
changeset
|
200 |
val _ = (test_thy, |
a27150cc8fa5
test: use SkipProof.make_thm instead of Thm.assume;
wenzelm
parents:
11608
diff
changeset
|
201 |
setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal) |> typedef_att; |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
202 |
|
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
203 |
in (cset, goal, goal_pat, typedef_att) end |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
204 |
handle ERROR => err_in_typedef name; |
4866 | 205 |
|
206 |
||
6383 | 207 |
(* add_typedef interfaces *) |
4866 | 208 |
|
6383 | 209 |
fun gen_add_typedef prep_term no_def name typ set names thms tac thy = |
210 |
let |
|
11744 | 211 |
val (cset, goal, _, typedef_att) = prepare_typedef prep_term no_def name typ set None thy; |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
212 |
val result = prove_nonempty thy cset goal (names, thms, tac); |
11807 | 213 |
in (thy, result) |> typedef_att end; |
4866 | 214 |
|
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
215 |
val add_typedef = gen_add_typedef read_term false; |
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
216 |
val add_typedef_i = gen_add_typedef cert_term false; |
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
217 |
val add_typedef_i_no_def = gen_add_typedef cert_term true; |
11807 | 218 |
fun add_typedef_no_result x y z = #1 oooo add_typedef x y z; |
4866 | 219 |
|
220 |
||
6383 | 221 |
(* typedef_proof interface *) |
222 |
||
11744 | 223 |
fun gen_typedef_proof prep_term ((name, typ, set, opt_morphs), comment) int thy = |
224 |
let val (_, goal, goal_pat, att) = prepare_typedef prep_term false name typ set opt_morphs thy in |
|
225 |
thy |> |
|
226 |
IsarThy.theorem_i Drule.internalK ((("", [att]), (goal, ([goal_pat], []))), comment) int |
|
11740 | 227 |
end; |
6383 | 228 |
|
229 |
val typedef_proof = gen_typedef_proof read_term; |
|
230 |
val typedef_proof_i = gen_typedef_proof cert_term; |
|
6357 | 231 |
|
232 |
||
6383 | 233 |
|
234 |
(** outer syntax **) |
|
235 |
||
6723 | 236 |
local structure P = OuterParse and K = OuterSyntax.Keyword in |
6383 | 237 |
|
6357 | 238 |
val typedeclP = |
6723 | 239 |
OuterSyntax.command "typedecl" "HOL type declaration" K.thy_decl |
7152 | 240 |
(P.type_args -- P.name -- P.opt_infix --| P.marg_comment >> (fn ((vs, t), mx) => |
6357 | 241 |
Toplevel.theory (add_typedecls [(t, vs, mx)]))); |
242 |
||
6723 | 243 |
|
6383 | 244 |
val typedef_proof_decl = |
6723 | 245 |
Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- |
11744 | 246 |
(P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) -- |
247 |
Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name)) -- |
|
248 |
P.marg_comment; |
|
6357 | 249 |
|
11744 | 250 |
fun mk_typedef_proof (((((opt_name, (vs, t)), mx), A), morphs), comment) = |
251 |
typedef_proof ((if_none opt_name (Syntax.type_name t mx), (t, vs, mx), A, morphs), comment); |
|
6357 | 252 |
|
253 |
val typedefP = |
|
6723 | 254 |
OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal |
6383 | 255 |
(typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof))); |
6357 | 256 |
|
6723 | 257 |
|
11744 | 258 |
val _ = OuterSyntax.add_keywords ["morphisms"]; |
6357 | 259 |
val _ = OuterSyntax.add_parsers [typedeclP, typedefP]; |
260 |
||
4866 | 261 |
end; |
6383 | 262 |
|
263 |
||
264 |
end; |