author | wenzelm |
Wed, 15 Nov 2006 20:50:21 +0100 | |
changeset 21390 | b3a9d8a83dea |
parent 21359 | 072e83a0b5bb |
child 21434 | 944f80576be0 |
permissions | -rw-r--r-- |
4866 | 1 |
(* Title: HOL/Tools/typedef_package.ML |
2 |
ID: $Id$ |
|
16458 | 3 |
Author: Markus Wenzel and Stefan Berghofer, TU Muenchen |
4866 | 4 |
|
21352 | 5 |
Gordon/HOL-style type definitions: create a new syntactic type |
6 |
represented by a non-empty subset. |
|
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 |
19705 | 13 |
type info = |
14 |
{rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, |
|
15 |
type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm, |
|
16 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, |
|
17 |
Abs_cases: thm, Rep_induct: thm, Abs_induct: thm}; |
|
18 |
val get_info: theory -> string -> info option |
|
11827 | 19 |
val add_typedef: bool -> string option -> bstring * string list * mixfix -> |
20483
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20462
diff
changeset
|
20 |
string -> (bstring * bstring) option -> tactic -> theory -> (string * info) * theory |
11827 | 21 |
val add_typedef_i: bool -> string option -> bstring * string list * mixfix -> |
20483
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20462
diff
changeset
|
22 |
term -> (bstring * bstring) option -> tactic -> theory -> (string * info) * theory |
17339 | 23 |
val typedef: (bool * string) * (bstring * string list * mixfix) * string |
24 |
* (string * string) option -> theory -> Proof.state |
|
25 |
val typedef_i: (bool * string) * (bstring * string list * mixfix) * term |
|
26 |
* (string * string) option -> theory -> Proof.state |
|
18708 | 27 |
val setup: theory -> theory |
4866 | 28 |
end; |
29 |
||
30 |
structure TypedefPackage: TYPEDEF_PACKAGE = |
|
31 |
struct |
|
32 |
||
10280 | 33 |
(** theory context references **) |
34 |
||
11608 | 35 |
val type_definitionN = "Typedef.type_definition"; |
10280 | 36 |
|
13413 | 37 |
val Rep = thm "type_definition.Rep"; |
38 |
val Rep_inverse = thm "type_definition.Rep_inverse"; |
|
39 |
val Abs_inverse = thm "type_definition.Abs_inverse"; |
|
40 |
val Rep_inject = thm "type_definition.Rep_inject"; |
|
41 |
val Abs_inject = thm "type_definition.Abs_inject"; |
|
42 |
val Rep_cases = thm "type_definition.Rep_cases"; |
|
43 |
val Abs_cases = thm "type_definition.Abs_cases"; |
|
44 |
val Rep_induct = thm "type_definition.Rep_induct"; |
|
45 |
val Abs_induct = thm "type_definition.Abs_induct"; |
|
10280 | 46 |
|
47 |
||
48 |
||
17922 | 49 |
(** type declarations **) |
50 |
||
19510 | 51 |
fun HOL_arity (raw_name, args, mx) thy = |
52 |
thy |> AxClass.axiomatize_arity_i |
|
53 |
(Sign.full_name thy (Syntax.type_name raw_name mx), |
|
54 |
replicate (length args) HOLogic.typeS, HOLogic.typeS); |
|
55 |
||
17922 | 56 |
fun add_typedecls decls thy = |
19510 | 57 |
thy |
58 |
|> Theory.add_typedecls decls |
|
59 |
|> can (Theory.assert_super HOL.thy) ? fold HOL_arity decls; |
|
17922 | 60 |
|
61 |
||
62 |
||
63 |
(** type definitions **) |
|
64 |
||
65 |
(* messages *) |
|
66 |
||
67 |
val quiet_mode = ref false; |
|
68 |
fun message s = if ! quiet_mode then () else writeln s; |
|
69 |
||
70 |
||
71 |
(* theory data *) |
|
15259 | 72 |
|
19705 | 73 |
type info = |
74 |
{rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, |
|
75 |
type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm, |
|
76 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, |
|
77 |
Abs_cases: thm, Rep_induct: thm, Abs_induct: thm}; |
|
19459 | 78 |
|
16458 | 79 |
structure TypedefData = TheoryDataFun |
80 |
(struct |
|
15259 | 81 |
val name = "HOL/typedef"; |
19705 | 82 |
type T = info Symtab.table; |
15259 | 83 |
val empty = Symtab.empty; |
84 |
val copy = I; |
|
16458 | 85 |
val extend = I; |
19617 | 86 |
fun merge _ tabs : T = Symtab.merge (K true) tabs; |
16458 | 87 |
fun print _ _ = (); |
88 |
end); |
|
15259 | 89 |
|
19705 | 90 |
val get_info = Symtab.lookup o TypedefData.get; |
91 |
fun put_info name info = TypedefData.map (Symtab.update (name, info)); |
|
15259 | 92 |
|
93 |
||
6383 | 94 |
(* prepare_typedef *) |
95 |
||
18678 | 96 |
fun err_in_typedef msg name = |
97 |
cat_error msg ("The error(s) above occurred in typedef " ^ quote name); |
|
6383 | 98 |
|
21352 | 99 |
fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS))); |
100 |
||
11822 | 101 |
fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy = |
4866 | 102 |
let |
11608 | 103 |
val _ = Theory.requires thy "Typedef" "typedefs"; |
21352 | 104 |
val ctxt = ProofContext.init thy; |
16458 | 105 |
val full = Sign.full_name thy; |
4866 | 106 |
|
107 |
(*rhs*) |
|
10280 | 108 |
val full_name = full name; |
21352 | 109 |
val set = prep_term (ctxt |> fold declare_type_name vs) raw_set; |
110 |
val setT = Term.fastype_of set; |
|
17280 | 111 |
val rhs_tfrees = Term.add_tfrees set []; |
112 |
val rhs_tfreesT = Term.add_tfreesT setT []; |
|
4866 | 113 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
21352 | 114 |
error ("Not a set type: " ^ quote (ProofContext.string_of_typ ctxt setT)); |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
115 |
fun mk_nonempty A = |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
116 |
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
|
117 |
val goal = mk_nonempty set; |
19473 | 118 |
val goal_pat = mk_nonempty (Var (the_default (name, 0) (Syntax.read_variable name), setT)); |
4866 | 119 |
|
120 |
(*lhs*) |
|
17280 | 121 |
val defS = Sign.defaultS thy; |
19473 | 122 |
val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs; |
17280 | 123 |
val args_setT = lhs_tfrees |
124 |
|> filter (member (op =) rhs_tfrees andf (not o member (op =) rhs_tfreesT)) |
|
125 |
|> map TFree; |
|
126 |
||
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 |
|
19473 | 131 |
val (Rep_name, Abs_name) = the_default ("Rep_" ^ name, "Abs_" ^ name) opt_morphs; |
19391 | 132 |
val setT' = map Term.itselfT args_setT ---> setT; |
17280 | 133 |
val setC = Term.list_comb (Const (full_name, setT'), map Logic.mk_type args_setT); |
10280 | 134 |
val RepC = Const (full Rep_name, newT --> oldT); |
135 |
val AbsC = Const (full Abs_name, oldT --> newT); |
|
4866 | 136 |
val x_new = Free ("x", newT); |
137 |
val y_old = Free ("y", oldT); |
|
10280 | 138 |
|
11822 | 139 |
val set' = if def then setC else set; |
4866 | 140 |
|
10280 | 141 |
val typedef_name = "type_definition_" ^ name; |
142 |
val typedefC = |
|
143 |
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
|
144 |
val typedef_prop = |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
145 |
Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set')); |
19705 | 146 |
val typedef_deps = Term.fold_aterms (fn Const c => insert (op =) c | _ => I) set' []; |
4866 | 147 |
|
19705 | 148 |
fun add_def eq thy = |
149 |
if def then |
|
18358 | 150 |
thy |
19705 | 151 |
|> PureThy.add_defs_i false [Thm.no_attributes eq] |
152 |
|-> (fn [th] => pair (SOME th)) |
|
153 |
else (NONE, thy); |
|
18358 | 154 |
|
21352 | 155 |
fun typedef_result nonempty = |
156 |
add_typedecls [(t, vs, mx)] |
|
157 |
#> Theory.add_consts_i |
|
17280 | 158 |
((if def then [(name, setT', NoSyn)] else []) @ |
6383 | 159 |
[(Rep_name, newT --> oldT, NoSyn), |
160 |
(Abs_name, oldT --> newT, NoSyn)]) |
|
21352 | 161 |
#> add_def (Logic.mk_defpair (setC, set)) |
162 |
##>> PureThy.add_axioms_i [((typedef_name, typedef_prop), |
|
20046 | 163 |
[apsnd (fn cond_axm => nonempty RS cond_axm)])] |
21352 | 164 |
##> Theory.add_deps "" (dest_Const RepC) typedef_deps |
165 |
##> Theory.add_deps "" (dest_Const AbsC) typedef_deps |
|
166 |
#-> (fn (set_def, [type_definition]) => fn thy1 => |
|
11822 | 167 |
let |
168 |
fun make th = Drule.standard (th OF [type_definition]); |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
169 |
val abs_inject = make Abs_inject; |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
170 |
val abs_inverse = make Abs_inverse; |
18377 | 171 |
val ([Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject, |
19705 | 172 |
Rep_cases, Abs_cases, Rep_induct, Abs_induct], thy2) = |
173 |
thy1 |
|
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
174 |
|> Theory.add_path name |
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
175 |
|> PureThy.add_thms |
11822 | 176 |
([((Rep_name, make Rep), []), |
177 |
((Rep_name ^ "_inverse", make Rep_inverse), []), |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
178 |
((Abs_name ^ "_inverse", abs_inverse), []), |
11822 | 179 |
((Rep_name ^ "_inject", make Rep_inject), []), |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
180 |
((Abs_name ^ "_inject", abs_inject), []), |
11822 | 181 |
((Rep_name ^ "_cases", make Rep_cases), |
18728 | 182 |
[RuleCases.case_names [Rep_name], InductAttrib.cases_set full_name]), |
11822 | 183 |
((Abs_name ^ "_cases", make Abs_cases), |
18728 | 184 |
[RuleCases.case_names [Abs_name], InductAttrib.cases_type full_tname]), |
11822 | 185 |
((Rep_name ^ "_induct", make Rep_induct), |
18728 | 186 |
[RuleCases.case_names [Rep_name], InductAttrib.induct_set full_name]), |
11822 | 187 |
((Abs_name ^ "_induct", make Abs_induct), |
18728 | 188 |
[RuleCases.case_names [Abs_name], InductAttrib.induct_type full_tname])]) |
19705 | 189 |
||> Theory.parent_path; |
190 |
val info = {rep_type = oldT, abs_type = newT, |
|
191 |
Rep_name = full Rep_name, Abs_name = full Abs_name, |
|
192 |
type_definition = type_definition, set_def = set_def, |
|
193 |
Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse, |
|
194 |
Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases, |
|
11822 | 195 |
Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct}; |
19705 | 196 |
val thy3 = thy2 |> put_info full_tname info; |
21352 | 197 |
in ((full_tname, info), thy3) end); |
6383 | 198 |
|
4866 | 199 |
|
200 |
(* errors *) |
|
201 |
||
202 |
fun show_names pairs = commas_quote (map fst pairs); |
|
203 |
||
204 |
val illegal_vars = |
|
205 |
if null (term_vars set) andalso null (term_tvars set) then [] |
|
206 |
else ["Illegal schematic variable(s) on rhs"]; |
|
207 |
||
208 |
val dup_lhs_tfrees = |
|
18964 | 209 |
(case duplicates (op =) lhs_tfrees of [] => [] |
4866 | 210 |
| dups => ["Duplicate type variables on lhs: " ^ show_names dups]); |
211 |
||
212 |
val extra_rhs_tfrees = |
|
17280 | 213 |
(case fold (remove (op =)) lhs_tfrees rhs_tfrees of [] => [] |
4866 | 214 |
| extras => ["Extra type variables on rhs: " ^ show_names extras]); |
215 |
||
216 |
val illegal_frees = |
|
217 |
(case term_frees set of [] => [] |
|
218 |
| xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]); |
|
219 |
||
220 |
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
|
221 |
val _ = if null errs then () else error (cat_lines errs); |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
222 |
|
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
223 |
(*test theory errors now!*) |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
224 |
val test_thy = Theory.copy thy; |
21352 | 225 |
val _ = test_thy |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
226 |
|> typedef_result (setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal); |
11426
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
227 |
|
21352 | 228 |
in (set, goal, goal_pat, typedef_result) end |
18678 | 229 |
handle ERROR msg => err_in_typedef msg name; |
4866 | 230 |
|
231 |
||
6383 | 232 |
(* add_typedef interfaces *) |
4866 | 233 |
|
17339 | 234 |
local |
235 |
||
17922 | 236 |
fun gen_typedef prep_term def opt_name typ set opt_morphs tac thy = |
6383 | 237 |
let |
21352 | 238 |
val string_of_term = ProofContext.string_of_term (ProofContext.init thy); |
17922 | 239 |
val name = the_default (#1 typ) opt_name; |
21352 | 240 |
val (set, goal, _, typedef_result) = |
11822 | 241 |
prepare_typedef prep_term def name typ set opt_morphs thy; |
21352 | 242 |
val _ = message ("Proving non-emptiness of set " ^ quote (string_of_term set) ^ " ..."); |
20046 | 243 |
val non_empty = Goal.prove_global thy [] [] goal (K tac) handle ERROR msg => |
21352 | 244 |
cat_error msg ("Failed to prove non-emptiness of " ^ quote (string_of_term set)); |
245 |
in typedef_result non_empty thy end; |
|
4866 | 246 |
|
17339 | 247 |
in |
248 |
||
21352 | 249 |
val add_typedef = gen_typedef ProofContext.read_term; |
250 |
val add_typedef_i = gen_typedef ProofContext.cert_term; |
|
4866 | 251 |
|
17339 | 252 |
end; |
4866 | 253 |
|
17339 | 254 |
|
255 |
(* Isar typedef interface *) |
|
6383 | 256 |
|
17339 | 257 |
local |
258 |
||
259 |
fun gen_typedef prep_term ((def, name), typ, set, opt_morphs) thy = |
|
11822 | 260 |
let |
19705 | 261 |
val (_, goal, goal_pat, typedef_result) = |
13443 | 262 |
prepare_typedef prep_term def name typ set opt_morphs thy; |
21352 | 263 |
fun after_qed [[th]] = ProofContext.theory (snd o typedef_result th); |
20483
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20462
diff
changeset
|
264 |
in |
21352 | 265 |
ProofContext.init thy |
21359 | 266 |
|> Proof.theorem_i PureThy.internalK NONE after_qed [[(goal, [goal_pat])]] |
20483
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20462
diff
changeset
|
267 |
end; |
17339 | 268 |
|
269 |
in |
|
6383 | 270 |
|
21352 | 271 |
val typedef = gen_typedef ProofContext.read_term; |
272 |
val typedef_i = gen_typedef ProofContext.cert_term; |
|
17339 | 273 |
|
19705 | 274 |
end; |
15259 | 275 |
|
19459 | 276 |
val setup = TypedefData.init; |
15259 | 277 |
|
278 |
||
279 |
||
6383 | 280 |
(** outer syntax **) |
281 |
||
17057 | 282 |
local structure P = OuterParse and K = OuterKeyword in |
6383 | 283 |
|
6357 | 284 |
val typedeclP = |
12624 | 285 |
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
|
286 |
(P.type_args -- P.name -- P.opt_infix >> (fn ((vs, t), mx) => |
6357 | 287 |
Toplevel.theory (add_typedecls [(t, vs, mx)]))); |
288 |
||
6723 | 289 |
|
17339 | 290 |
val typedef_decl = |
16126
3ba9eb7ea366
fixed outer syntax: allow type_args with parentheses;
wenzelm
parents:
15570
diff
changeset
|
291 |
Scan.optional (P.$$$ "(" |-- |
3ba9eb7ea366
fixed outer syntax: allow type_args with parentheses;
wenzelm
parents:
15570
diff
changeset
|
292 |
((P.$$$ "open" >> K false) -- Scan.option P.name || P.name >> (fn s => (true, SOME s))) |
3ba9eb7ea366
fixed outer syntax: allow type_args with parentheses;
wenzelm
parents:
15570
diff
changeset
|
293 |
--| P.$$$ ")") (true, NONE) -- |
11744 | 294 |
(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
|
295 |
Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name)); |
6357 | 296 |
|
17339 | 297 |
fun mk_typedef ((((((def, opt_name), (vs, t)), mx), A), morphs)) = |
19473 | 298 |
typedef ((def, the_default (Syntax.type_name t mx) opt_name), (t, vs, mx), A, morphs); |
6357 | 299 |
|
300 |
val typedefP = |
|
6723 | 301 |
OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal |
17339 | 302 |
(typedef_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef))); |
6357 | 303 |
|
6723 | 304 |
|
11744 | 305 |
val _ = OuterSyntax.add_keywords ["morphisms"]; |
6357 | 306 |
val _ = OuterSyntax.add_parsers [typedeclP, typedefP]; |
307 |
||
4866 | 308 |
end; |
6383 | 309 |
|
310 |
end; |