1 (* Title: HOL/Typedef.thy
3 Author: Markus Wenzel, TU Munich
6 header {* HOL type definitions *}
11 ("Tools/typedef_package.ML")
12 ("Tools/typecopy_package.ML")
13 ("Tools/typedef_codegen.ML")
17 structure HOL = struct val thy = theory "HOL" end;
18 *} -- "belongs to theory HOL"
20 locale type_definition =
21 fixes Rep and Abs and A
22 assumes Rep: "Rep x \<in> A"
23 and Rep_inverse: "Abs (Rep x) = x"
24 and Abs_inverse: "y \<in> A ==> Rep (Abs y) = y"
25 -- {* This will be axiomatized for each typedef! *}
29 "(Rep x = Rep y) = (x = y)"
31 assume "Rep x = Rep y"
32 then have "Abs (Rep x) = Abs (Rep y)" by (simp only:)
33 moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
34 moreover have "Abs (Rep y) = y" by (rule Rep_inverse)
35 ultimately show "x = y" by simp
38 thus "Rep x = Rep y" by (simp only:)
42 assumes x: "x \<in> A" and y: "y \<in> A"
43 shows "(Abs x = Abs y) = (x = y)"
45 assume "Abs x = Abs y"
46 then have "Rep (Abs x) = Rep (Abs y)" by (simp only:)
47 moreover from x have "Rep (Abs x) = x" by (rule Abs_inverse)
48 moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
49 ultimately show "x = y" by simp
52 thus "Abs x = Abs y" by (simp only:)
55 lemma Rep_cases [cases set]:
56 assumes y: "y \<in> A"
57 and hyp: "!!x. y = Rep x ==> P"
60 from y have "Rep (Abs y) = y" by (rule Abs_inverse)
61 thus "y = Rep (Abs y)" ..
64 lemma Abs_cases [cases type]:
65 assumes r: "!!y. x = Abs y ==> y \<in> A ==> P"
68 have "Abs (Rep x) = x" by (rule Rep_inverse)
69 thus "x = Abs (Rep x)" ..
70 show "Rep x \<in> A" by (rule Rep)
73 lemma Rep_induct [induct set]:
74 assumes y: "y \<in> A"
75 and hyp: "!!x. P (Rep x)"
78 have "P (Rep (Abs y))" by (rule hyp)
79 moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
80 ultimately show "P y" by simp
83 lemma Abs_induct [induct type]:
84 assumes r: "!!y. y \<in> A ==> P (Abs y)"
87 have "Rep x \<in> A" by (rule Rep)
88 then have "P (Abs (Rep x))" by (rule r)
89 moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
90 ultimately show "P x" by simp
93 lemma Rep_range: "range Rep = A"
95 show "range Rep <= A" using Rep by (auto simp add: image_def)
99 hence "x = Rep (Abs x)" by (rule Abs_inverse [symmetric])
100 thus "x : range Rep" by (rule range_eqI)
104 lemma Abs_image: "Abs ` A = UNIV"
106 show "Abs ` A <= UNIV" by (rule subset_UNIV)
108 show "UNIV <= Abs ` A"
111 have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
112 moreover have "Rep x : A" by (rule Rep)
113 ultimately show "x : Abs ` A" by (rule image_eqI)
119 use "Tools/typedef_package.ML"
120 use "Tools/typecopy_package.ML"
121 use "Tools/typedef_codegen.ML"
125 #> TypecopyPackage.setup
126 #> TypedefCodegen.setup
129 text {* This class is just a workaround for classes without parameters;
130 it shall disappear as soon as possible. *}
132 class itself = type +
133 fixes itself :: "'a itself"
136 let fun add_itself tyco thy =
138 val vs = Name.names Name.context "'a"
139 (replicate (Sign.arity_number thy tyco) @{sort type});
140 val ty = Type (tyco, map TFree vs);
141 val lhs = Const (@{const_name itself}, Term.itselfT ty);
142 val rhs = Logic.mk_type ty;
143 val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
146 |> TheoryTarget.instantiation ([tyco], vs, @{sort itself})
147 |> `(fn lthy => Syntax.check_term lthy eq)
148 |-> (fn eq => Specification.definition (NONE, ((Name.no_binding, []), eq)))
150 |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
152 |> ProofContext.theory_of
154 in TypedefPackage.interpretation add_itself end
157 instantiation bool :: itself
160 definition "itself = TYPE(bool)"
166 instantiation "fun" :: ("type", "type") itself
169 definition "itself = TYPE('a \<Rightarrow> 'b)"
175 hide (open) const itself