author | wenzelm |
Mon, 13 Mar 2000 13:16:57 +0100 | |
changeset 8428 | be4c8a57cf7e |
parent 8141 | d6d896e81cd7 |
child 9230 | 17ae63f82ad8 |
permissions | -rw-r--r-- |
4866 | 1 |
(* Title: HOL/Tools/typedef_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
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 |
4866 | 12 |
val add_typedef: string -> bstring * string list * mixfix -> |
13 |
string -> string list -> thm list -> tactic option -> theory -> theory |
|
14 |
val add_typedef_i: string -> bstring * string list * mixfix -> |
|
15 |
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
|
16 |
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
|
17 |
term -> string list -> thm list -> tactic option -> theory -> theory |
6729 | 18 |
val typedef_proof: (string * (bstring * string list * mixfix) * string) * Comment.text |
6690 | 19 |
-> bool -> theory -> ProofHistory.T |
6729 | 20 |
val typedef_proof_i: (string * (bstring * string list * mixfix) * term) * Comment.text |
6690 | 21 |
-> bool -> theory -> ProofHistory.T |
4866 | 22 |
end; |
23 |
||
24 |
structure TypedefPackage: TYPEDEF_PACKAGE = |
|
25 |
struct |
|
26 |
||
27 |
||
5104 | 28 |
(** type declarations **) |
29 |
||
30 |
fun add_typedecls decls thy = |
|
31 |
let |
|
32 |
val full = Sign.full_name (Theory.sign_of thy); |
|
33 |
||
34 |
fun arity_of (raw_name, args, mx) = |
|
35 |
(full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.termS, HOLogic.termS); |
|
36 |
in |
|
8141 | 37 |
if can (Theory.assert_super HOL.thy) thy then |
38 |
thy |
|
39 |
|> PureThy.add_typedecls decls |
|
40 |
|> Theory.add_arities_i (map arity_of decls) |
|
41 |
else thy |> PureThy.add_typedecls decls |
|
5104 | 42 |
end; |
43 |
||
44 |
||
45 |
||
46 |
(** type definitions **) |
|
47 |
||
5697 | 48 |
(* messages *) |
49 |
||
50 |
val quiet_mode = ref false; |
|
51 |
fun message s = if ! quiet_mode then () else writeln s; |
|
52 |
||
53 |
||
6383 | 54 |
(* non-emptiness of set *) (*exception ERROR*) |
4866 | 55 |
|
6383 | 56 |
fun check_nonempty cset thm = |
4866 | 57 |
let |
6383 | 58 |
val {t, sign, maxidx, ...} = Thm.rep_cterm cset; |
59 |
val {prop, ...} = Thm.rep_thm (Thm.transfer_sg sign (Drule.standard thm)); |
|
60 |
val matches = Pattern.matches (Sign.tsig_of sign); |
|
61 |
in |
|
62 |
(case try (HOLogic.dest_mem o HOLogic.dest_Trueprop) prop of |
|
63 |
None => raise ERROR |
|
64 |
| Some (_, A) => if matches (Logic.incr_indexes ([], maxidx) A, t) then () else raise ERROR) |
|
65 |
end handle ERROR => error ("Bad non-emptiness theorem " ^ Display.string_of_thm thm ^ |
|
66 |
"\nfor set " ^ quote (Display.string_of_cterm cset)); |
|
67 |
||
7481 | 68 |
fun goal_nonempty ex cset = |
6383 | 69 |
let |
70 |
val {T = setT, t = A, maxidx, sign} = Thm.rep_cterm cset; |
|
4866 | 71 |
val T = HOLogic.dest_setT setT; |
7481 | 72 |
val tm = |
73 |
if ex then HOLogic.mk_exists ("x", T, HOLogic.mk_mem (Free ("x", T), A)) |
|
74 |
else HOLogic.mk_mem (Var (("x", maxidx + 1), T), A); (*old-style version*) |
|
75 |
in Thm.cterm_of sign (HOLogic.mk_Trueprop tm) end; |
|
6383 | 76 |
|
77 |
fun prove_nonempty thy cset (witn_names, witn_thms, witn_tac) = |
|
78 |
let |
|
79 |
val is_def = Logic.is_equals o #prop o Thm.rep_thm; |
|
80 |
val thms = PureThy.get_thmss thy witn_names @ witn_thms; |
|
4866 | 81 |
val tac = |
82 |
TRY (rewrite_goals_tac (filter is_def thms)) THEN |
|
83 |
TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN |
|
6383 | 84 |
if_none witn_tac (TRY (ALLGOALS (CLASET' blast_tac))); |
4866 | 85 |
in |
6383 | 86 |
message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ..."); |
7481 | 87 |
prove_goalw_cterm [] (goal_nonempty false cset) (K [tac]) |
6383 | 88 |
end handle ERROR => error ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset)); |
4866 | 89 |
|
90 |
||
6383 | 91 |
(* prepare_typedef *) |
92 |
||
93 |
fun read_term sg used s = |
|
8100 | 94 |
#1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.termT)); |
4866 | 95 |
|
6383 | 96 |
fun cert_term sg _ t = Thm.cterm_of sg t handle TERM (msg, _) => error msg; |
97 |
||
98 |
fun err_in_typedef name = |
|
99 |
error ("The error(s) above occurred in typedef " ^ quote name); |
|
100 |
||
101 |
fun prepare_typedef prep_term no_def name (t, vs, mx) raw_set thy = |
|
4866 | 102 |
let |
4970 | 103 |
val _ = Theory.requires thy "Set" "typedefs"; |
6383 | 104 |
val sign = Theory.sign_of thy; |
4866 | 105 |
val full_name = Sign.full_name sign; |
106 |
||
107 |
(*rhs*) |
|
6383 | 108 |
val cset = prep_term sign vs raw_set; |
109 |
val {T = setT, t = set, ...} = Thm.rep_cterm cset; |
|
4866 | 110 |
val rhs_tfrees = term_tfrees set; |
111 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
|
112 |
error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT)); |
|
113 |
||
114 |
(*lhs*) |
|
6383 | 115 |
val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs; |
4866 | 116 |
val tname = Syntax.type_name t mx; |
117 |
val newT = Type (full_name tname, map TFree lhs_tfrees); |
|
118 |
||
119 |
val Rep_name = "Rep_" ^ name; |
|
120 |
val Abs_name = "Abs_" ^ name; |
|
121 |
val setC = Const (full_name name, setT); |
|
122 |
val RepC = Const (full_name Rep_name, newT --> oldT); |
|
123 |
val AbsC = Const (full_name Abs_name, oldT --> newT); |
|
124 |
val x_new = Free ("x", newT); |
|
125 |
val y_old = Free ("y", oldT); |
|
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
126 |
val set' = if no_def then set else setC; |
4866 | 127 |
|
128 |
(*axioms*) |
|
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
129 |
val rep_type = HOLogic.mk_Trueprop (HOLogic.mk_mem (RepC $ x_new, set')); |
4866 | 130 |
val rep_type_inv = HOLogic.mk_Trueprop (HOLogic.mk_eq (AbsC $ (RepC $ x_new), x_new)); |
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
131 |
val abs_type_inv = Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (y_old, set')), |
4866 | 132 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (RepC $ (AbsC $ y_old), y_old))); |
133 |
||
6383 | 134 |
(*theory extender*) |
135 |
fun do_typedef theory = |
|
136 |
theory |
|
137 |
|> Theory.assert_super thy |
|
138 |
|> add_typedecls [(t, vs, mx)] |
|
139 |
|> Theory.add_consts_i |
|
140 |
((if no_def then [] else [(name, setT, NoSyn)]) @ |
|
141 |
[(Rep_name, newT --> oldT, NoSyn), |
|
142 |
(Abs_name, oldT --> newT, NoSyn)]) |
|
8428 | 143 |
|> (if no_def then I else (#1 oo (PureThy.add_defs_i o map Thm.no_attributes)) |
6383 | 144 |
[Logic.mk_defpair (setC, set)]) |
8428 | 145 |
|> (#1 oo (PureThy.add_axioms_i o map Thm.no_attributes)) |
6383 | 146 |
[(Rep_name, rep_type), |
147 |
(Rep_name ^ "_inverse", rep_type_inv), |
|
148 |
(Abs_name ^ "_inverse", abs_type_inv)] |
|
149 |
handle ERROR => err_in_typedef name; |
|
150 |
||
4866 | 151 |
|
152 |
(* errors *) |
|
153 |
||
154 |
fun show_names pairs = commas_quote (map fst pairs); |
|
155 |
||
156 |
val illegal_vars = |
|
157 |
if null (term_vars set) andalso null (term_tvars set) then [] |
|
158 |
else ["Illegal schematic variable(s) on rhs"]; |
|
159 |
||
160 |
val dup_lhs_tfrees = |
|
161 |
(case duplicates lhs_tfrees of [] => [] |
|
162 |
| dups => ["Duplicate type variables on lhs: " ^ show_names dups]); |
|
163 |
||
164 |
val extra_rhs_tfrees = |
|
165 |
(case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => [] |
|
166 |
| extras => ["Extra type variables on rhs: " ^ show_names extras]); |
|
167 |
||
168 |
val illegal_frees = |
|
169 |
(case term_frees set of [] => [] |
|
170 |
| xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]); |
|
171 |
||
172 |
val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees; |
|
173 |
in |
|
6383 | 174 |
if null errs then () else error (cat_lines errs); |
175 |
(cset, do_typedef) |
|
176 |
end handle ERROR => err_in_typedef name; |
|
4866 | 177 |
|
178 |
||
6383 | 179 |
(* add_typedef interfaces *) |
4866 | 180 |
|
6383 | 181 |
fun gen_add_typedef prep_term no_def name typ set names thms tac thy = |
182 |
let |
|
183 |
val (cset, do_typedef) = prepare_typedef prep_term no_def name typ set thy; |
|
184 |
val result = prove_nonempty thy cset (names, thms, tac); |
|
185 |
in check_nonempty cset result; thy |> do_typedef end; |
|
4866 | 186 |
|
5180
d82a70766af0
Added new function add_typedef_i_no_def which doesn't add
berghofe
parents:
5104
diff
changeset
|
187 |
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
|
188 |
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
|
189 |
val add_typedef_i_no_def = gen_add_typedef cert_term true; |
4866 | 190 |
|
191 |
||
6383 | 192 |
(* typedef_proof interface *) |
193 |
||
194 |
fun typedef_attribute cset do_typedef (thy, thm) = |
|
7481 | 195 |
(check_nonempty cset (thm RS (select_eq_Ex RS iffD2)); (thy |> do_typedef, thm)); |
6357 | 196 |
|
6729 | 197 |
fun gen_typedef_proof prep_term ((name, typ, set), comment) int thy = |
6383 | 198 |
let |
199 |
val (cset, do_typedef) = prepare_typedef prep_term false name typ set thy; |
|
7481 | 200 |
val goal = Thm.term_of (goal_nonempty true cset); |
6383 | 201 |
in |
202 |
thy |
|
6935 | 203 |
|> IsarThy.theorem_i (("", [typedef_attribute cset do_typedef], (goal, ([], []))), comment) int |
6383 | 204 |
end; |
205 |
||
206 |
val typedef_proof = gen_typedef_proof read_term; |
|
207 |
val typedef_proof_i = gen_typedef_proof cert_term; |
|
6357 | 208 |
|
209 |
||
6383 | 210 |
|
211 |
(** outer syntax **) |
|
212 |
||
6723 | 213 |
local structure P = OuterParse and K = OuterSyntax.Keyword in |
6383 | 214 |
|
6357 | 215 |
val typedeclP = |
6723 | 216 |
OuterSyntax.command "typedecl" "HOL type declaration" K.thy_decl |
7152 | 217 |
(P.type_args -- P.name -- P.opt_infix --| P.marg_comment >> (fn ((vs, t), mx) => |
6357 | 218 |
Toplevel.theory (add_typedecls [(t, vs, mx)]))); |
219 |
||
6723 | 220 |
|
6383 | 221 |
val typedef_proof_decl = |
6723 | 222 |
Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- |
6729 | 223 |
(P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) -- P.marg_comment; |
6357 | 224 |
|
6729 | 225 |
fun mk_typedef_proof ((((opt_name, (vs, t)), mx), A), comment) = |
226 |
typedef_proof ((if_none opt_name (Syntax.type_name t mx), (t, vs, mx), A), comment); |
|
6357 | 227 |
|
228 |
val typedefP = |
|
6723 | 229 |
OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal |
6383 | 230 |
(typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof))); |
6357 | 231 |
|
6723 | 232 |
|
6357 | 233 |
val _ = OuterSyntax.add_parsers [typedeclP, typedefP]; |
234 |
||
4866 | 235 |
end; |
6383 | 236 |
|
237 |
||
238 |
end; |