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