author | wenzelm |
Tue, 09 Oct 2007 17:10:34 +0200 | |
changeset 24926 | bcb6b098df11 |
parent 24920 | 2a45e400fdad |
child 25458 | ba8f5e4fa336 |
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 |
|
4866 | 27 |
end; |
28 |
||
29 |
structure TypedefPackage: TYPEDEF_PACKAGE = |
|
30 |
struct |
|
31 |
||
10280 | 32 |
(** theory context references **) |
33 |
||
11608 | 34 |
val type_definitionN = "Typedef.type_definition"; |
10280 | 35 |
|
13413 | 36 |
val Rep = thm "type_definition.Rep"; |
37 |
val Rep_inverse = thm "type_definition.Rep_inverse"; |
|
38 |
val Abs_inverse = thm "type_definition.Abs_inverse"; |
|
39 |
val Rep_inject = thm "type_definition.Rep_inject"; |
|
40 |
val Abs_inject = thm "type_definition.Abs_inject"; |
|
41 |
val Rep_cases = thm "type_definition.Rep_cases"; |
|
42 |
val Abs_cases = thm "type_definition.Abs_cases"; |
|
43 |
val Rep_induct = thm "type_definition.Rep_induct"; |
|
44 |
val Abs_induct = thm "type_definition.Abs_induct"; |
|
10280 | 45 |
|
46 |
||
47 |
||
17922 | 48 |
(** type declarations **) |
49 |
||
19510 | 50 |
fun HOL_arity (raw_name, args, mx) thy = |
24926
bcb6b098df11
AxClass.axiomatize: renamed XXX_i to XXX, and XXX to XXX_cmd;
wenzelm
parents:
24920
diff
changeset
|
51 |
thy |> AxClass.axiomatize_arity |
19510 | 52 |
(Sign.full_name thy (Syntax.type_name raw_name mx), |
53 |
replicate (length args) HOLogic.typeS, HOLogic.typeS); |
|
54 |
||
17922 | 55 |
fun add_typedecls decls thy = |
21565 | 56 |
if can (Theory.assert_super HOL.thy) thy then |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24509
diff
changeset
|
57 |
thy |> Sign.add_typedecls decls |> fold HOL_arity decls |
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24509
diff
changeset
|
58 |
else thy |> Sign.add_typedecls decls; |
17922 | 59 |
|
60 |
||
61 |
||
62 |
(** type definitions **) |
|
63 |
||
64 |
(* messages *) |
|
65 |
||
66 |
val quiet_mode = ref false; |
|
67 |
fun message s = if ! quiet_mode then () else writeln s; |
|
68 |
||
69 |
||
70 |
(* theory data *) |
|
15259 | 71 |
|
19705 | 72 |
type info = |
73 |
{rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, |
|
74 |
type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm, |
|
75 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, |
|
76 |
Abs_cases: thm, Rep_induct: thm, Abs_induct: thm}; |
|
19459 | 77 |
|
16458 | 78 |
structure TypedefData = TheoryDataFun |
22846 | 79 |
( |
19705 | 80 |
type T = info Symtab.table; |
15259 | 81 |
val empty = Symtab.empty; |
82 |
val copy = I; |
|
16458 | 83 |
val extend = I; |
19617 | 84 |
fun merge _ tabs : T = Symtab.merge (K true) tabs; |
22846 | 85 |
); |
15259 | 86 |
|
19705 | 87 |
val get_info = Symtab.lookup o TypedefData.get; |
88 |
fun put_info name info = TypedefData.map (Symtab.update (name, info)); |
|
15259 | 89 |
|
90 |
||
6383 | 91 |
(* prepare_typedef *) |
92 |
||
18678 | 93 |
fun err_in_typedef msg name = |
94 |
cat_error msg ("The error(s) above occurred in typedef " ^ quote name); |
|
6383 | 95 |
|
21352 | 96 |
fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS))); |
97 |
||
11822 | 98 |
fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy = |
4866 | 99 |
let |
11608 | 100 |
val _ = Theory.requires thy "Typedef" "typedefs"; |
21352 | 101 |
val ctxt = ProofContext.init thy; |
16458 | 102 |
val full = Sign.full_name thy; |
4866 | 103 |
|
104 |
(*rhs*) |
|
10280 | 105 |
val full_name = full name; |
21352 | 106 |
val set = prep_term (ctxt |> fold declare_type_name vs) raw_set; |
107 |
val setT = Term.fastype_of set; |
|
17280 | 108 |
val rhs_tfrees = Term.add_tfrees set []; |
109 |
val rhs_tfreesT = Term.add_tfreesT setT []; |
|
4866 | 110 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
24920 | 111 |
error ("Not a set type: " ^ quote (Syntax.string_of_typ ctxt 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; |
19473 | 115 |
val goal_pat = mk_nonempty (Var (the_default (name, 0) (Syntax.read_variable name), setT)); |
4866 | 116 |
|
117 |
(*lhs*) |
|
17280 | 118 |
val defS = Sign.defaultS thy; |
19473 | 119 |
val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs; |
17280 | 120 |
val args_setT = lhs_tfrees |
121 |
|> filter (member (op =) rhs_tfrees andf (not o member (op =) rhs_tfreesT)) |
|
122 |
|> map TFree; |
|
123 |
||
4866 | 124 |
val tname = Syntax.type_name t mx; |
10280 | 125 |
val full_tname = full tname; |
126 |
val newT = Type (full_tname, map TFree lhs_tfrees); |
|
4866 | 127 |
|
19473 | 128 |
val (Rep_name, Abs_name) = the_default ("Rep_" ^ name, "Abs_" ^ name) opt_morphs; |
19391 | 129 |
val setT' = map Term.itselfT args_setT ---> setT; |
17280 | 130 |
val setC = Term.list_comb (Const (full_name, setT'), map Logic.mk_type args_setT); |
10280 | 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 |
|
11822 | 136 |
val set' = if def then setC else set; |
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')); |
19705 | 143 |
val typedef_deps = Term.fold_aterms (fn Const c => insert (op =) c | _ => I) set' []; |
4866 | 144 |
|
19705 | 145 |
fun add_def eq thy = |
146 |
if def then |
|
18358 | 147 |
thy |
19705 | 148 |
|> PureThy.add_defs_i false [Thm.no_attributes eq] |
149 |
|-> (fn [th] => pair (SOME th)) |
|
150 |
else (NONE, thy); |
|
18358 | 151 |
|
21352 | 152 |
fun typedef_result nonempty = |
153 |
add_typedecls [(t, vs, mx)] |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24509
diff
changeset
|
154 |
#> Sign.add_consts_i |
17280 | 155 |
((if def then [(name, setT', NoSyn)] else []) @ |
6383 | 156 |
[(Rep_name, newT --> oldT, NoSyn), |
157 |
(Abs_name, oldT --> newT, NoSyn)]) |
|
24255 | 158 |
#> add_def (PrimitiveDefs.mk_defpair (setC, set)) |
21352 | 159 |
##>> PureThy.add_axioms_i [((typedef_name, typedef_prop), |
20046 | 160 |
[apsnd (fn cond_axm => nonempty RS cond_axm)])] |
21352 | 161 |
##> Theory.add_deps "" (dest_Const RepC) typedef_deps |
162 |
##> Theory.add_deps "" (dest_Const AbsC) typedef_deps |
|
163 |
#-> (fn (set_def, [type_definition]) => fn thy1 => |
|
11822 | 164 |
let |
165 |
fun make th = Drule.standard (th OF [type_definition]); |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
166 |
val abs_inject = make Abs_inject; |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
167 |
val abs_inverse = make Abs_inverse; |
18377 | 168 |
val ([Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject, |
19705 | 169 |
Rep_cases, Abs_cases, Rep_induct, Abs_induct], thy2) = |
170 |
thy1 |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24509
diff
changeset
|
171 |
|> Sign.add_path name |
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
172 |
|> PureThy.add_thms |
11822 | 173 |
([((Rep_name, make Rep), []), |
174 |
((Rep_name ^ "_inverse", make Rep_inverse), []), |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
175 |
((Abs_name ^ "_inverse", abs_inverse), []), |
11822 | 176 |
((Rep_name ^ "_inject", make Rep_inject), []), |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
177 |
((Abs_name ^ "_inject", abs_inject), []), |
11822 | 178 |
((Rep_name ^ "_cases", make Rep_cases), |
24861
cc669ca5f382
tuned Induct interface: prefer pred'' over set'';
wenzelm
parents:
24830
diff
changeset
|
179 |
[RuleCases.case_names [Rep_name], Induct.cases_pred full_name]), |
11822 | 180 |
((Abs_name ^ "_cases", make Abs_cases), |
24830
a7b3ab44d993
moved Pure/Isar/induct_attrib.ML and Provers/induct_method.ML to Tools/induct.ML;
wenzelm
parents:
24712
diff
changeset
|
181 |
[RuleCases.case_names [Abs_name], Induct.cases_type full_tname]), |
11822 | 182 |
((Rep_name ^ "_induct", make Rep_induct), |
24861
cc669ca5f382
tuned Induct interface: prefer pred'' over set'';
wenzelm
parents:
24830
diff
changeset
|
183 |
[RuleCases.case_names [Rep_name], Induct.induct_pred full_name]), |
11822 | 184 |
((Abs_name ^ "_induct", make Abs_induct), |
24830
a7b3ab44d993
moved Pure/Isar/induct_attrib.ML and Provers/induct_method.ML to Tools/induct.ML;
wenzelm
parents:
24712
diff
changeset
|
185 |
[RuleCases.case_names [Abs_name], Induct.induct_type full_tname])]) |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24509
diff
changeset
|
186 |
||> Sign.parent_path; |
19705 | 187 |
val info = {rep_type = oldT, abs_type = newT, |
188 |
Rep_name = full Rep_name, Abs_name = full Abs_name, |
|
189 |
type_definition = type_definition, set_def = set_def, |
|
190 |
Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse, |
|
191 |
Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases, |
|
11822 | 192 |
Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct}; |
19705 | 193 |
val thy3 = thy2 |> put_info full_tname info; |
21352 | 194 |
in ((full_tname, info), thy3) end); |
6383 | 195 |
|
4866 | 196 |
|
197 |
(* errors *) |
|
198 |
||
199 |
fun show_names pairs = commas_quote (map fst pairs); |
|
200 |
||
201 |
val illegal_vars = |
|
202 |
if null (term_vars set) andalso null (term_tvars set) then [] |
|
203 |
else ["Illegal schematic variable(s) on rhs"]; |
|
204 |
||
205 |
val dup_lhs_tfrees = |
|
18964 | 206 |
(case duplicates (op =) lhs_tfrees of [] => [] |
4866 | 207 |
| dups => ["Duplicate type variables on lhs: " ^ show_names dups]); |
208 |
||
209 |
val extra_rhs_tfrees = |
|
17280 | 210 |
(case fold (remove (op =)) lhs_tfrees rhs_tfrees of [] => [] |
4866 | 211 |
| extras => ["Extra type variables on rhs: " ^ show_names extras]); |
212 |
||
213 |
val illegal_frees = |
|
214 |
(case term_frees set of [] => [] |
|
215 |
| xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]); |
|
216 |
||
217 |
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
|
218 |
val _ = if null errs then () else error (cat_lines errs); |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
219 |
|
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
220 |
(*test theory errors now!*) |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
221 |
val test_thy = Theory.copy thy; |
21352 | 222 |
val _ = test_thy |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
223 |
|> 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
|
224 |
|
21352 | 225 |
in (set, goal, goal_pat, typedef_result) end |
18678 | 226 |
handle ERROR msg => err_in_typedef msg name; |
4866 | 227 |
|
228 |
||
6383 | 229 |
(* add_typedef interfaces *) |
4866 | 230 |
|
17339 | 231 |
local |
232 |
||
17922 | 233 |
fun gen_typedef prep_term def opt_name typ set opt_morphs tac thy = |
6383 | 234 |
let |
24920 | 235 |
val string_of_term = Sign.string_of_term thy; |
17922 | 236 |
val name = the_default (#1 typ) opt_name; |
21352 | 237 |
val (set, goal, _, typedef_result) = |
11822 | 238 |
prepare_typedef prep_term def name typ set opt_morphs thy; |
21352 | 239 |
val _ = message ("Proving non-emptiness of set " ^ quote (string_of_term set) ^ " ..."); |
20046 | 240 |
val non_empty = Goal.prove_global thy [] [] goal (K tac) handle ERROR msg => |
21352 | 241 |
cat_error msg ("Failed to prove non-emptiness of " ^ quote (string_of_term set)); |
242 |
in typedef_result non_empty thy end; |
|
4866 | 243 |
|
17339 | 244 |
in |
245 |
||
24509
23ee6b7788c2
replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents:
24255
diff
changeset
|
246 |
val add_typedef = gen_typedef Syntax.read_term; |
23ee6b7788c2
replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents:
24255
diff
changeset
|
247 |
val add_typedef_i = gen_typedef Syntax.check_term; |
4866 | 248 |
|
17339 | 249 |
end; |
4866 | 250 |
|
17339 | 251 |
|
252 |
(* Isar typedef interface *) |
|
6383 | 253 |
|
17339 | 254 |
local |
255 |
||
256 |
fun gen_typedef prep_term ((def, name), typ, set, opt_morphs) thy = |
|
11822 | 257 |
let |
19705 | 258 |
val (_, goal, goal_pat, typedef_result) = |
13443 | 259 |
prepare_typedef prep_term def name typ set opt_morphs thy; |
21352 | 260 |
fun after_qed [[th]] = ProofContext.theory (snd o typedef_result th); |
21434 | 261 |
in Proof.theorem_i NONE after_qed [[(goal, [goal_pat])]] (ProofContext.init thy) end; |
17339 | 262 |
|
263 |
in |
|
6383 | 264 |
|
24509
23ee6b7788c2
replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents:
24255
diff
changeset
|
265 |
val typedef = gen_typedef Syntax.read_term; |
23ee6b7788c2
replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents:
24255
diff
changeset
|
266 |
val typedef_i = gen_typedef Syntax.check_term; |
17339 | 267 |
|
19705 | 268 |
end; |
15259 | 269 |
|
270 |
||
271 |
||
6383 | 272 |
(** outer syntax **) |
273 |
||
17057 | 274 |
local structure P = OuterParse and K = OuterKeyword in |
6383 | 275 |
|
24867 | 276 |
val _ = OuterSyntax.keywords ["morphisms"]; |
277 |
||
278 |
val _ = |
|
12624 | 279 |
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
|
280 |
(P.type_args -- P.name -- P.opt_infix >> (fn ((vs, t), mx) => |
6357 | 281 |
Toplevel.theory (add_typedecls [(t, vs, mx)]))); |
282 |
||
6723 | 283 |
|
17339 | 284 |
val typedef_decl = |
16126
3ba9eb7ea366
fixed outer syntax: allow type_args with parentheses;
wenzelm
parents:
15570
diff
changeset
|
285 |
Scan.optional (P.$$$ "(" |-- |
3ba9eb7ea366
fixed outer syntax: allow type_args with parentheses;
wenzelm
parents:
15570
diff
changeset
|
286 |
((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
|
287 |
--| P.$$$ ")") (true, NONE) -- |
11744 | 288 |
(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
|
289 |
Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name)); |
6357 | 290 |
|
17339 | 291 |
fun mk_typedef ((((((def, opt_name), (vs, t)), mx), A), morphs)) = |
19473 | 292 |
typedef ((def, the_default (Syntax.type_name t mx) opt_name), (t, vs, mx), A, morphs); |
6357 | 293 |
|
24867 | 294 |
val _ = |
6723 | 295 |
OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal |
17339 | 296 |
(typedef_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef))); |
6357 | 297 |
|
4866 | 298 |
end; |
6383 | 299 |
|
300 |
end; |