author | krauss |
Mon, 08 Dec 2008 08:56:30 +0100 | |
changeset 29054 | 6f61794f1ff7 |
parent 29053 | 077fb9b16119 |
child 29056 | dc08e3990c77 |
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 |
|
19705 | 11 |
type info = |
12 |
{rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, |
|
28848 | 13 |
inhabited: thm, type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm, |
19705 | 14 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, |
15 |
Abs_cases: thm, Rep_induct: thm, Abs_induct: thm}; |
|
16 |
val get_info: theory -> string -> info option |
|
11827 | 17 |
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
|
18 |
term -> (bstring * bstring) option -> tactic -> theory -> (string * info) * theory |
28662 | 19 |
val typedef: (bool * string) * (bstring * string list * mixfix) * term |
17339 | 20 |
* (string * string) option -> theory -> Proof.state |
28662 | 21 |
val typedef_cmd: (bool * string) * (bstring * string list * mixfix) * string |
17339 | 22 |
* (string * string) option -> theory -> Proof.state |
25513 | 23 |
val interpretation: (string -> theory -> theory) -> theory -> theory |
25535 | 24 |
val setup: theory -> theory |
4866 | 25 |
end; |
26 |
||
27 |
structure TypedefPackage: TYPEDEF_PACKAGE = |
|
28 |
struct |
|
29 |
||
10280 | 30 |
(** theory context references **) |
31 |
||
11608 | 32 |
val type_definitionN = "Typedef.type_definition"; |
10280 | 33 |
|
28662 | 34 |
val Rep = @{thm "type_definition.Rep"}; |
35 |
val Rep_inverse = @{thm "type_definition.Rep_inverse"}; |
|
36 |
val Abs_inverse = @{thm "type_definition.Abs_inverse"}; |
|
37 |
val Rep_inject = @{thm "type_definition.Rep_inject"}; |
|
38 |
val Abs_inject = @{thm "type_definition.Abs_inject"}; |
|
39 |
val Rep_cases = @{thm "type_definition.Rep_cases"}; |
|
40 |
val Abs_cases = @{thm "type_definition.Abs_cases"}; |
|
41 |
val Rep_induct = @{thm "type_definition.Rep_induct"}; |
|
42 |
val Abs_induct = @{thm "type_definition.Abs_induct"}; |
|
10280 | 43 |
|
44 |
||
45 |
||
17922 | 46 |
(** type definitions **) |
47 |
||
48 |
(* theory data *) |
|
15259 | 49 |
|
19705 | 50 |
type info = |
51 |
{rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, |
|
28848 | 52 |
inhabited: thm, type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm, |
19705 | 53 |
Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, |
54 |
Abs_cases: thm, Rep_induct: thm, Abs_induct: thm}; |
|
19459 | 55 |
|
16458 | 56 |
structure TypedefData = TheoryDataFun |
22846 | 57 |
( |
19705 | 58 |
type T = info Symtab.table; |
15259 | 59 |
val empty = Symtab.empty; |
60 |
val copy = I; |
|
16458 | 61 |
val extend = I; |
19617 | 62 |
fun merge _ tabs : T = Symtab.merge (K true) tabs; |
22846 | 63 |
); |
15259 | 64 |
|
19705 | 65 |
val get_info = Symtab.lookup o TypedefData.get; |
66 |
fun put_info name info = TypedefData.map (Symtab.update (name, info)); |
|
15259 | 67 |
|
68 |
||
6383 | 69 |
(* prepare_typedef *) |
70 |
||
18678 | 71 |
fun err_in_typedef msg name = |
72 |
cat_error msg ("The error(s) above occurred in typedef " ^ quote name); |
|
6383 | 73 |
|
21352 | 74 |
fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS))); |
75 |
||
25535 | 76 |
structure TypedefInterpretation = InterpretationFun(type T = string val eq = op =); |
77 |
val interpretation = TypedefInterpretation.interpretation; |
|
78 |
||
11822 | 79 |
fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy = |
4866 | 80 |
let |
11608 | 81 |
val _ = Theory.requires thy "Typedef" "typedefs"; |
21352 | 82 |
val ctxt = ProofContext.init thy; |
28965 | 83 |
val full = Sign.full_bname thy; |
4866 | 84 |
|
85 |
(*rhs*) |
|
10280 | 86 |
val full_name = full name; |
21352 | 87 |
val set = prep_term (ctxt |> fold declare_type_name vs) raw_set; |
88 |
val setT = Term.fastype_of set; |
|
17280 | 89 |
val rhs_tfrees = Term.add_tfrees set []; |
90 |
val rhs_tfreesT = Term.add_tfreesT setT []; |
|
4866 | 91 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
24920 | 92 |
error ("Not a set type: " ^ quote (Syntax.string_of_typ ctxt setT)); |
4866 | 93 |
|
94 |
(*lhs*) |
|
17280 | 95 |
val defS = Sign.defaultS thy; |
19473 | 96 |
val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs; |
17280 | 97 |
val args_setT = lhs_tfrees |
98 |
|> filter (member (op =) rhs_tfrees andf (not o member (op =) rhs_tfreesT)) |
|
99 |
|> map TFree; |
|
100 |
||
4866 | 101 |
val tname = Syntax.type_name t mx; |
10280 | 102 |
val full_tname = full tname; |
103 |
val newT = Type (full_tname, map TFree lhs_tfrees); |
|
4866 | 104 |
|
19473 | 105 |
val (Rep_name, Abs_name) = the_default ("Rep_" ^ name, "Abs_" ^ name) opt_morphs; |
19391 | 106 |
val setT' = map Term.itselfT args_setT ---> setT; |
17280 | 107 |
val setC = Term.list_comb (Const (full_name, setT'), map Logic.mk_type args_setT); |
10280 | 108 |
val RepC = Const (full Rep_name, newT --> oldT); |
109 |
val AbsC = Const (full Abs_name, oldT --> newT); |
|
110 |
||
11822 | 111 |
val set' = if def then setC else set; |
29054
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
112 |
fun mk_inhabited A = |
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
113 |
HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A))); |
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
114 |
val goal = mk_inhabited set'; |
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
115 |
val term_binding = (the_default (name, 0) (Syntax.read_variable name), SOME set) |
4866 | 116 |
|
10280 | 117 |
val typedef_name = "type_definition_" ^ name; |
118 |
val typedefC = |
|
119 |
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
|
120 |
val typedef_prop = |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
121 |
Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set')); |
19705 | 122 |
val typedef_deps = Term.fold_aterms (fn Const c => insert (op =) c | _ => I) set' []; |
4866 | 123 |
|
29053 | 124 |
val (set_def, thy') = if def then |
18358 | 125 |
thy |
29053 | 126 |
|> Sign.add_consts_i [(name, setT', NoSyn)] |
127 |
|> PureThy.add_defs false [Thm.no_attributes (PrimitiveDefs.mk_defpair (setC, set))] |
|
19705 | 128 |
|-> (fn [th] => pair (SOME th)) |
129 |
else (NONE, thy); |
|
18358 | 130 |
|
29053 | 131 |
fun typedef_result inhabited = |
25495 | 132 |
ObjectLogic.typedecl (t, vs, mx) |
25458
ba8f5e4fa336
separated typedecl module, providing typedecl command with interpretation
haftmann
parents:
24926
diff
changeset
|
133 |
#> snd |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24509
diff
changeset
|
134 |
#> Sign.add_consts_i |
6383 | 135 |
[(Rep_name, newT --> oldT, NoSyn), |
29053 | 136 |
(Abs_name, oldT --> newT, NoSyn)] |
137 |
#> PureThy.add_axioms [((typedef_name, typedef_prop), |
|
28848 | 138 |
[apsnd (fn cond_axm => inhabited RS cond_axm)])] |
21352 | 139 |
##> Theory.add_deps "" (dest_Const RepC) typedef_deps |
140 |
##> Theory.add_deps "" (dest_Const AbsC) typedef_deps |
|
29053 | 141 |
#-> (fn [type_definition] => fn thy1 => |
11822 | 142 |
let |
143 |
fun make th = Drule.standard (th OF [type_definition]); |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
144 |
val abs_inject = make Abs_inject; |
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
145 |
val abs_inverse = make Abs_inverse; |
18377 | 146 |
val ([Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject, |
19705 | 147 |
Rep_cases, Abs_cases, Rep_induct, Abs_induct], thy2) = |
148 |
thy1 |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24509
diff
changeset
|
149 |
|> Sign.add_path name |
12338
de0f4a63baa5
renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents:
12043
diff
changeset
|
150 |
|> PureThy.add_thms |
11822 | 151 |
([((Rep_name, make Rep), []), |
152 |
((Rep_name ^ "_inverse", make Rep_inverse), []), |
|
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
153 |
((Abs_name ^ "_inverse", abs_inverse), []), |
11822 | 154 |
((Rep_name ^ "_inject", make Rep_inject), []), |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
155 |
((Abs_name ^ "_inject", abs_inject), []), |
11822 | 156 |
((Rep_name ^ "_cases", make Rep_cases), |
24861
cc669ca5f382
tuned Induct interface: prefer pred'' over set'';
wenzelm
parents:
24830
diff
changeset
|
157 |
[RuleCases.case_names [Rep_name], Induct.cases_pred full_name]), |
11822 | 158 |
((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
|
159 |
[RuleCases.case_names [Abs_name], Induct.cases_type full_tname]), |
11822 | 160 |
((Rep_name ^ "_induct", make Rep_induct), |
24861
cc669ca5f382
tuned Induct interface: prefer pred'' over set'';
wenzelm
parents:
24830
diff
changeset
|
161 |
[RuleCases.case_names [Rep_name], Induct.induct_pred full_name]), |
11822 | 162 |
((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
|
163 |
[RuleCases.case_names [Abs_name], Induct.induct_type full_tname])]) |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24509
diff
changeset
|
164 |
||> Sign.parent_path; |
19705 | 165 |
val info = {rep_type = oldT, abs_type = newT, |
166 |
Rep_name = full Rep_name, Abs_name = full Abs_name, |
|
28848 | 167 |
inhabited = inhabited, type_definition = type_definition, set_def = set_def, |
19705 | 168 |
Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse, |
169 |
Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases, |
|
11822 | 170 |
Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct}; |
25535 | 171 |
in |
172 |
thy2 |
|
173 |
|> put_info full_tname info |
|
174 |
|> TypedefInterpretation.data full_tname |
|
175 |
|> pair (full_tname, info) |
|
176 |
end); |
|
6383 | 177 |
|
4866 | 178 |
(* errors *) |
179 |
||
180 |
fun show_names pairs = commas_quote (map fst pairs); |
|
181 |
||
182 |
val illegal_vars = |
|
183 |
if null (term_vars set) andalso null (term_tvars set) then [] |
|
184 |
else ["Illegal schematic variable(s) on rhs"]; |
|
185 |
||
186 |
val dup_lhs_tfrees = |
|
18964 | 187 |
(case duplicates (op =) lhs_tfrees of [] => [] |
4866 | 188 |
| dups => ["Duplicate type variables on lhs: " ^ show_names dups]); |
189 |
||
190 |
val extra_rhs_tfrees = |
|
17280 | 191 |
(case fold (remove (op =)) lhs_tfrees rhs_tfrees of [] => [] |
4866 | 192 |
| extras => ["Extra type variables on rhs: " ^ show_names extras]); |
193 |
||
194 |
val illegal_frees = |
|
195 |
(case term_frees set of [] => [] |
|
196 |
| xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]); |
|
197 |
||
198 |
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
|
199 |
val _ = if null errs then () else error (cat_lines errs); |
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
200 |
|
f280d4b29a2c
abtract non-emptiness statements (no longer use Eps);
wenzelm
parents:
10697
diff
changeset
|
201 |
(*test theory errors now!*) |
29053 | 202 |
val test_thy = Theory.copy thy'; |
21352 | 203 |
val _ = test_thy |
19342
094a1c071c8e
added functions for definitional code generation
haftmann
parents:
18964
diff
changeset
|
204 |
|> 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
|
205 |
|
29054
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
206 |
in ((set, goal, term_binding, set_def, typedef_result), thy') end |
18678 | 207 |
handle ERROR msg => err_in_typedef msg name; |
4866 | 208 |
|
209 |
||
28662 | 210 |
(* add_typedef interface *) |
4866 | 211 |
|
28662 | 212 |
fun add_typedef def opt_name typ set opt_morphs tac thy = |
6383 | 213 |
let |
17922 | 214 |
val name = the_default (#1 typ) opt_name; |
29054
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
215 |
val ((set, goal, _, set_def, typedef_result), thy') = |
28662 | 216 |
prepare_typedef Syntax.check_term def name typ set opt_morphs thy; |
29053 | 217 |
val non_empty = Goal.prove_global thy' [] [] goal (K tac) |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26478
diff
changeset
|
218 |
handle ERROR msg => cat_error msg |
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26478
diff
changeset
|
219 |
("Failed to prove non-emptiness of " ^ quote (Syntax.string_of_term_global thy set)); |
29053 | 220 |
in typedef_result non_empty thy' end; |
4866 | 221 |
|
17339 | 222 |
|
223 |
(* Isar typedef interface *) |
|
6383 | 224 |
|
17339 | 225 |
local |
226 |
||
227 |
fun gen_typedef prep_term ((def, name), typ, set, opt_morphs) thy = |
|
11822 | 228 |
let |
29054
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
229 |
val ((_, goal, term_binding, set_def, typedef_result), thy') = |
13443 | 230 |
prepare_typedef prep_term def name typ set opt_morphs thy; |
21352 | 231 |
fun after_qed [[th]] = ProofContext.theory (snd o typedef_result th); |
29054
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
232 |
in |
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
233 |
Proof.theorem_i NONE after_qed [[(goal, [])]] (ProofContext.init thy') |
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
234 |
|> Proof.add_binds_i [term_binding] |
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
235 |
|> (if def |
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
236 |
then Seq.hd o Proof.refine (Method.Basic (Method.unfold [the set_def], Position.none)) |
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
237 |
else I) |
6f61794f1ff7
logically separate typedef axiomatization from constant definition
krauss
parents:
29053
diff
changeset
|
238 |
end; |
17339 | 239 |
|
240 |
in |
|
6383 | 241 |
|
28662 | 242 |
val typedef = gen_typedef Syntax.check_term; |
243 |
val typedef_cmd = gen_typedef Syntax.read_term; |
|
17339 | 244 |
|
19705 | 245 |
end; |
15259 | 246 |
|
247 |
||
248 |
||
6383 | 249 |
(** outer syntax **) |
250 |
||
17057 | 251 |
local structure P = OuterParse and K = OuterKeyword in |
6383 | 252 |
|
27353
71c4dd53d4cb
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents:
26939
diff
changeset
|
253 |
val _ = OuterKeyword.keyword "morphisms"; |
24867 | 254 |
|
17339 | 255 |
val typedef_decl = |
16126
3ba9eb7ea366
fixed outer syntax: allow type_args with parentheses;
wenzelm
parents:
15570
diff
changeset
|
256 |
Scan.optional (P.$$$ "(" |-- |
3ba9eb7ea366
fixed outer syntax: allow type_args with parentheses;
wenzelm
parents:
15570
diff
changeset
|
257 |
((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
|
258 |
--| P.$$$ ")") (true, NONE) -- |
11744 | 259 |
(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
|
260 |
Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name)); |
6357 | 261 |
|
17339 | 262 |
fun mk_typedef ((((((def, opt_name), (vs, t)), mx), A), morphs)) = |
28662 | 263 |
typedef_cmd ((def, the_default (Syntax.type_name t mx) opt_name), (t, vs, mx), A, morphs); |
6357 | 264 |
|
24867 | 265 |
val _ = |
6723 | 266 |
OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal |
17339 | 267 |
(typedef_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef))); |
6357 | 268 |
|
25535 | 269 |
val setup = TypedefInterpretation.init; |
270 |
||
4866 | 271 |
end; |
6383 | 272 |
|
273 |
end; |