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