summary |
shortlog |
changelog |
graph |
tags |
branches |
files |
changeset |
file |
revisions |
annotate |
diff |
raw

src/HOL/Typedef.thy

author | wenzelm |

Fri Mar 28 19:43:54 2008 +0100 (2008-03-28) | |

changeset 26462 | dac4e2bce00d |

parent 26151 | 4a9b8f15ce7f |

child 26802 | 9eede540a5e8 |

permissions | -rw-r--r-- |

avoid rebinding of existing facts;

1 (* Title: HOL/Typedef.thy

2 ID: $Id$

3 Author: Markus Wenzel, TU Munich

4 *)

6 header {* HOL type definitions *}

8 theory Typedef

9 imports Set

10 uses

11 ("Tools/typedef_package.ML")

12 ("Tools/typecopy_package.ML")

13 ("Tools/typedef_codegen.ML")

14 begin

16 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! *}

26 begin

28 lemma Rep_inject:

29 "(Rep x = Rep y) = (x = y)"

30 proof

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

36 next

37 assume "x = y"

38 thus "Rep x = Rep y" by (simp only:)

39 qed

41 lemma Abs_inject:

42 assumes x: "x \<in> A" and y: "y \<in> A"

43 shows "(Abs x = Abs y) = (x = y)"

44 proof

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

50 next

51 assume "x = y"

52 thus "Abs x = Abs y" by (simp only:)

53 qed

55 lemma Rep_cases [cases set]:

56 assumes y: "y \<in> A"

57 and hyp: "!!x. y = Rep x ==> P"

58 shows P

59 proof (rule hyp)

60 from y have "Rep (Abs y) = y" by (rule Abs_inverse)

61 thus "y = Rep (Abs y)" ..

62 qed

64 lemma Abs_cases [cases type]:

65 assumes r: "!!y. x = Abs y ==> y \<in> A ==> P"

66 shows P

67 proof (rule r)

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)

71 qed

73 lemma Rep_induct [induct set]:

74 assumes y: "y \<in> A"

75 and hyp: "!!x. P (Rep x)"

76 shows "P y"

77 proof -

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

81 qed

83 lemma Abs_induct [induct type]:

84 assumes r: "!!y. y \<in> A ==> P (Abs y)"

85 shows "P x"

86 proof -

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

91 qed

93 lemma Rep_range:

94 shows "range Rep = A"

95 proof

96 show "range Rep <= A" using Rep by (auto simp add: image_def)

97 show "A <= range Rep"

98 proof

99 fix x assume "x : A"

100 hence "x = Rep (Abs x)" by (rule Abs_inverse [symmetric])

101 thus "x : range Rep" by (rule range_eqI)

102 qed

103 qed

105 end

107 use "Tools/typedef_package.ML"

108 use "Tools/typecopy_package.ML"

109 use "Tools/typedef_codegen.ML"

111 setup {*

112 TypedefPackage.setup

113 #> TypecopyPackage.setup

114 #> TypedefCodegen.setup

115 *}

117 text {* This class is just a workaround for classes without parameters;

118 it shall disappear as soon as possible. *}

120 class itself = type +

121 fixes itself :: "'a itself"

123 setup {*

124 let fun add_itself tyco thy =

125 let

126 val vs = Name.names Name.context "'a"

127 (replicate (Sign.arity_number thy tyco) @{sort type});

128 val ty = Type (tyco, map TFree vs);

129 val lhs = Const (@{const_name itself}, Term.itselfT ty);

130 val rhs = Logic.mk_type ty;

131 val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));

132 in

133 thy

134 |> TheoryTarget.instantiation ([tyco], vs, @{sort itself})

135 |> `(fn lthy => Syntax.check_term lthy eq)

136 |-> (fn eq => Specification.definition (NONE, (("", []), eq)))

137 |> snd

138 |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))

139 |> LocalTheory.exit

140 |> ProofContext.theory_of

141 end

142 in TypedefPackage.interpretation add_itself end

143 *}

145 instantiation bool :: itself

146 begin

148 definition "itself = TYPE(bool)"

150 instance ..

152 end

154 instantiation "fun" :: ("type", "type") itself

155 begin

157 definition "itself = TYPE('a \<Rightarrow> 'b)"

159 instance ..

161 end

163 instantiation "set" :: ("type") itself

164 begin

166 definition "itself = TYPE('a set)"

168 instance ..

170 end

172 hide (open) const itself

174 end