author  Andreas Lochbihler 
Fri, 01 Jun 2012 20:40:34 +0200  
changeset 48062  9014e78ccde2 
parent 48060  1f4d00a7f59f 
child 48067  9f458b3efb5c 
permissions  rwrr 
37653  1 
(* Title: HOL/Library/Cardinality.thy 
48051  2 
Author: Brian Huffman, Andreas Lochbihler 
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

3 
*) 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

4 

37653  5 
header {* Cardinality of types *} 
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

6 

37653  7 
theory Cardinality 
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
44142
diff
changeset

8 
imports "~~/src/HOL/Main" 
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

9 
begin 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

10 

e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

11 
subsection {* Preliminary lemmas *} 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

12 
(* These should be moved elsewhere *) 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

13 

e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

14 
lemma (in type_definition) univ: 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

15 
"UNIV = Abs ` A" 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

16 
proof 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

17 
show "Abs ` A \<subseteq> UNIV" by (rule subset_UNIV) 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

18 
show "UNIV \<subseteq> Abs ` A" 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

19 
proof 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

20 
fix x :: 'b 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

21 
have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric]) 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

22 
moreover have "Rep x \<in> A" by (rule Rep) 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

23 
ultimately show "x \<in> Abs ` A" by (rule image_eqI) 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

24 
qed 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

25 
qed 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

26 

e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

27 
lemma (in type_definition) card: "card (UNIV :: 'b set) = card A" 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

28 
by (simp add: univ card_image inj_on_def Abs_inject) 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

29 

48060  30 
lemma finite_range_Some: "finite (range (Some :: 'a \<Rightarrow> 'a option)) = finite (UNIV :: 'a set)" 
31 
by(auto dest: finite_imageD intro: inj_Some) 

32 

24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

33 

e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

34 
subsection {* Cardinalities of types *} 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

35 

e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

36 
syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))") 
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

37 

35431  38 
translations "CARD('t)" => "CONST card (CONST UNIV \<Colon> 't set)" 
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

39 

42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset

40 
typed_print_translation (advanced) {* 
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset

41 
let 
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset

42 
fun card_univ_tr' ctxt _ [Const (@{const_syntax UNIV}, Type (_, [T, _]))] = 
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset

43 
Syntax.const @{syntax_const "_type_card"} $ Syntax_Phases.term_of_typ ctxt T; 
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset

44 
in [(@{const_syntax card}, card_univ_tr')] end 
24407  45 
*} 
46 

30001  47 
lemma card_unit [simp]: "CARD(unit) = 1" 
26153  48 
unfolding UNIV_unit by simp 
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

49 

48058  50 
lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a) * CARD('b)" 
26153  51 
unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product) 
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

52 

48060  53 
lemma card_UNIV_sum: "CARD('a + 'b) = (if CARD('a) \<noteq> 0 \<and> CARD('b) \<noteq> 0 then CARD('a) + CARD('b) else 0)" 
54 
unfolding UNIV_Plus_UNIV[symmetric] 

55 
by(auto simp add: card_eq_0_iff card_Plus simp del: UNIV_Plus_UNIV) 

56 

30001  57 
lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)" 
48060  58 
by(simp add: card_UNIV_sum) 
59 

60 
lemma card_UNIV_option: "CARD('a option) = (if CARD('a) = 0 then 0 else CARD('a) + 1)" 

61 
proof  

62 
have "(None :: 'a option) \<notin> range Some" by clarsimp 

63 
thus ?thesis 

64 
by(simp add: UNIV_option_conv card_eq_0_iff finite_range_Some card_insert_disjoint card_image) 

65 
qed 

24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

66 

30001  67 
lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)" 
48060  68 
by(simp add: card_UNIV_option) 
69 

70 
lemma card_UNIV_set: "CARD('a set) = (if CARD('a) = 0 then 0 else 2 ^ CARD('a))" 

71 
by(simp add: Pow_UNIV[symmetric] card_eq_0_iff card_Pow del: Pow_UNIV) 

24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

72 

30001  73 
lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)" 
48060  74 
by(simp add: card_UNIV_set) 
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset

75 

30001  76 
lemma card_nat [simp]: "CARD(nat) = 0" 
44142  77 
by (simp add: card_eq_0_iff) 
30001  78 

48060  79 
lemma card_fun: "CARD('a \<Rightarrow> 'b) = (if CARD('a) \<noteq> 0 \<and> CARD('b) \<noteq> 0 \<or> CARD('b) = 1 then CARD('b) ^ CARD('a) else 0)" 
80 
proof  

81 
{ assume "0 < CARD('a)" and "0 < CARD('b)" 

82 
hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)" 

83 
by(simp_all only: card_ge_0_finite) 

84 
from finite_distinct_list[OF finb] obtain bs 

85 
where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast 

86 
from finite_distinct_list[OF fina] obtain as 

87 
where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast 

88 
have cb: "CARD('b) = length bs" 

89 
unfolding bs[symmetric] distinct_card[OF distb] .. 

90 
have ca: "CARD('a) = length as" 

91 
unfolding as[symmetric] distinct_card[OF dista] .. 

92 
let ?xs = "map (\<lambda>ys. the o map_of (zip as ys)) (Enum.n_lists (length as) bs)" 

93 
have "UNIV = set ?xs" 

94 
proof(rule UNIV_eq_I) 

95 
fix f :: "'a \<Rightarrow> 'b" 

96 
from as have "f = the \<circ> map_of (zip as (map f as))" 

97 
by(auto simp add: map_of_zip_map) 

98 
thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists) 

99 
qed 

100 
moreover have "distinct ?xs" unfolding distinct_map 

101 
proof(intro conjI distinct_n_lists distb inj_onI) 

102 
fix xs ys :: "'b list" 

103 
assume xs: "xs \<in> set (Enum.n_lists (length as) bs)" 

104 
and ys: "ys \<in> set (Enum.n_lists (length as) bs)" 

105 
and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)" 

106 
from xs ys have [simp]: "length xs = length as" "length ys = length as" 

107 
by(simp_all add: length_n_lists_elem) 

108 
have "map_of (zip as xs) = map_of (zip as ys)" 

109 
proof 

110 
fix x 

111 
from as bs have "\<exists>y. map_of (zip as xs) x = Some y" "\<exists>y. map_of (zip as ys) x = Some y" 

112 
by(simp_all add: map_of_zip_is_Some[symmetric]) 

113 
with eq show "map_of (zip as xs) x = map_of (zip as ys) x" 

114 
by(auto dest: fun_cong[where x=x]) 

115 
qed 

116 
with dista show "xs = ys" by(simp add: map_of_zip_inject) 

117 
qed 

118 
hence "card (set ?xs) = length ?xs" by(simp only: distinct_card) 

119 
moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists) 

120 
ultimately have "CARD('a \<Rightarrow> 'b) = CARD('b) ^ CARD('a)" using cb ca by simp } 

121 
moreover { 

122 
assume cb: "CARD('b) = 1" 

123 
then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq) 

124 
have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}" 

125 
proof(rule UNIV_eq_I) 

126 
fix x :: "'a \<Rightarrow> 'b" 

127 
{ fix y 

128 
have "x y \<in> UNIV" .. 

129 
hence "x y = b" unfolding b by simp } 

130 
thus "x \<in> {\<lambda>x. b}" by(auto) 

131 
qed 

132 
have "CARD('a \<Rightarrow> 'b) = 1" unfolding eq by simp } 

133 
ultimately show ?thesis 

134 
by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1) 

135 
qed 

136 

137 
lemma card_nibble: "CARD(nibble) = 16" 

138 
unfolding UNIV_nibble by simp 

139 

140 
lemma card_UNIV_char: "CARD(char) = 256" 

141 
proof  

142 
have "inj (\<lambda>(x, y). Char x y)" by(auto intro: injI) 

143 
thus ?thesis unfolding UNIV_char by(simp add: card_image card_nibble) 

144 
qed 

145 

146 
lemma card_literal: "CARD(String.literal) = 0" 

147 
proof  

148 
have "inj STR" by(auto intro: injI) 

149 
thus ?thesis by(simp add: type_definition.univ[OF type_definition_literal] card_image infinite_UNIV_listI) 

150 
qed 

30001  151 

152 
subsection {* Classes with at least 1 and 2 *} 

153 

154 
text {* Class finite already captures "at least 1" *} 

155 

156 
lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)" 

29997  157 
unfolding neq0_conv [symmetric] by simp 
158 

30001  159 
lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)" 
160 
by (simp add: less_Suc_eq_le [symmetric]) 

161 

162 
text {* Class for cardinality "at least 2" *} 

163 

164 
class card2 = finite + 

165 
assumes two_le_card: "2 \<le> CARD('a)" 

166 

167 
lemma one_less_card: "Suc 0 < CARD('a::card2)" 

168 
using two_le_card [where 'a='a] by simp 

169 

170 
lemma one_less_int_card: "1 < int CARD('a::card2)" 

171 
using one_less_card [where 'a='a] by simp 

172 

48051  173 
subsection {* A type class for computing the cardinality of types *} 
174 

48059  175 
definition is_list_UNIV :: "'a list \<Rightarrow> bool" 
176 
where "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)" 

177 

178 
lemmas [code_unfold] = is_list_UNIV_def[abs_def] 

179 

180 
lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV" 

181 
by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric] 

182 
dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV) 

183 

48051  184 
class card_UNIV = 
185 
fixes card_UNIV :: "'a itself \<Rightarrow> nat" 

48059  186 
assumes card_UNIV: "card_UNIV x = CARD('a)" 
48051  187 

48059  188 
lemma card_UNIV_code [code_unfold]: "CARD('a :: card_UNIV) = card_UNIV TYPE('a)" 
189 
by(simp add: card_UNIV) 

48051  190 

48060  191 
lemma finite_UNIV_conv_card_UNIV [code_unfold]: 
192 
"finite (UNIV :: 'a :: card_UNIV set) \<longleftrightarrow> card_UNIV TYPE('a) > 0" 

193 
by(simp add: card_UNIV card_gt_0_iff) 

48051  194 

48060  195 
subsection {* Instantiations for @{text "card_UNIV"} *} 
48051  196 

197 
instantiation nat :: card_UNIV begin 

198 
definition "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)" 

48053  199 
instance by intro_classes (simp add: card_UNIV_nat_def) 
48051  200 
end 
201 

202 
instantiation int :: card_UNIV begin 

48052  203 
definition "card_UNIV = (\<lambda>a :: int itself. 0)" 
48053  204 
instance by intro_classes (simp add: card_UNIV_int_def infinite_UNIV_int) 
48051  205 
end 
206 

207 
instantiation list :: (type) card_UNIV begin 

48052  208 
definition "card_UNIV = (\<lambda>a :: 'a list itself. 0)" 
48053  209 
instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI) 
48051  210 
end 
211 

212 
instantiation unit :: card_UNIV begin 

48052  213 
definition "card_UNIV = (\<lambda>a :: unit itself. 1)" 
48053  214 
instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit) 
48051  215 
end 
216 

217 
instantiation bool :: card_UNIV begin 

48052  218 
definition "card_UNIV = (\<lambda>a :: bool itself. 2)" 
48053  219 
instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool) 
48051  220 
end 
221 

222 
instantiation char :: card_UNIV begin 

48052  223 
definition "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)" 
48053  224 
instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char) 
48051  225 
end 
226 

227 
instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin 

48052  228 
definition "card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))" 
48060  229 
instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV) 
48051  230 
end 
231 

232 
instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin 

48060  233 
definition "card_UNIV = (\<lambda>a :: ('a + 'b) itself. 
48052  234 
let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b)) 
235 
in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)" 

48060  236 
instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum) 
48051  237 
end 
238 

239 
instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin 

48060  240 
definition "card_UNIV = (\<lambda>a :: ('a \<Rightarrow> 'b) itself. 
241 
let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b)) 

242 
in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)" 

243 
instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun) 

244 
end 

48051  245 

48060  246 
instantiation option :: (card_UNIV) card_UNIV begin 
247 
definition "card_UNIV = (\<lambda>a :: 'a option itself. 

248 
let c = card_UNIV (TYPE('a)) in if c \<noteq> 0 then Suc c else 0)" 

249 
instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option) 

250 
end 

48051  251 

48060  252 
instantiation String.literal :: card_UNIV begin 
253 
definition "card_UNIV = (\<lambda>a :: String.literal itself. 0)" 

254 
instance by intro_classes (simp add: card_UNIV_literal_def card_literal) 

255 
end 

256 

257 
instantiation set :: (card_UNIV) card_UNIV begin 

258 
definition "card_UNIV = (\<lambda>a :: 'a set itself. 

259 
let c = card_UNIV (TYPE('a)) in if c = 0 then 0 else 2 ^ c)" 

260 
instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV) 

48051  261 
end 
262 

48060  263 

264 
lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]" 

265 
by(auto intro: finite_1.exhaust) 

266 

267 
lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]" 

268 
by(auto intro: finite_2.exhaust) 

48051  269 

48060  270 
lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^isub>1, finite_3.a\<^isub>2, finite_3.a\<^isub>3]" 
271 
by(auto intro: finite_3.exhaust) 

48051  272 

48060  273 
lemma UNIV_finite_4: "UNIV = set [finite_4.a\<^isub>1, finite_4.a\<^isub>2, finite_4.a\<^isub>3, finite_4.a\<^isub>4]" 
274 
by(auto intro: finite_4.exhaust) 

275 

276 
lemma UNIV_finite_5: 

277 
"UNIV = set [finite_5.a\<^isub>1, finite_5.a\<^isub>2, finite_5.a\<^isub>3, finite_5.a\<^isub>4, finite_5.a\<^isub>5]" 

278 
by(auto intro: finite_5.exhaust) 

48051  279 

48060  280 
instantiation Enum.finite_1 :: card_UNIV begin 
281 
definition "card_UNIV = (\<lambda>a :: Enum.finite_1 itself. 1)" 

282 
instance by intro_classes (simp add: UNIV_finite_1 card_UNIV_finite_1_def) 

283 
end 

284 

285 
instantiation Enum.finite_2 :: card_UNIV begin 

286 
definition "card_UNIV = (\<lambda>a :: Enum.finite_2 itself. 2)" 

287 
instance by intro_classes (simp add: UNIV_finite_2 card_UNIV_finite_2_def) 

288 
end 

48051  289 

48060  290 
instantiation Enum.finite_3 :: card_UNIV begin 
291 
definition "card_UNIV = (\<lambda>a :: Enum.finite_3 itself. 3)" 

292 
instance by intro_classes (simp add: UNIV_finite_3 card_UNIV_finite_3_def) 

293 
end 

294 

295 
instantiation Enum.finite_4 :: card_UNIV begin 

296 
definition "card_UNIV = (\<lambda>a :: Enum.finite_4 itself. 4)" 

297 
instance by intro_classes (simp add: UNIV_finite_4 card_UNIV_finite_4_def) 

298 
end 

299 

300 
instantiation Enum.finite_5 :: card_UNIV begin 

301 
definition "card_UNIV = (\<lambda>a :: Enum.finite_5 itself. 5)" 

302 
instance by intro_classes (simp add: UNIV_finite_5 card_UNIV_finite_5_def) 

48051  303 
end 
304 

48062
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

305 
subsection {* Code setup for sets *} 
48051  306 

307 
lemma card_Compl: 

308 
"finite A \<Longrightarrow> card ( A) = card (UNIV :: 'a set)  card (A :: 'a set)" 

309 
by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest) 

310 

48062
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

311 
context fixes xs :: "'a :: card_UNIV list" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

312 
begin 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

313 

9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

314 
definition card' :: "'a set \<Rightarrow> nat" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

315 
where [simp, code del, code_abbrev]: "card' = card" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

316 

9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

317 
lemma card'_code [code]: 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

318 
"card' (set xs) = length (remdups xs)" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

319 
"card' (List.coset xs) = card_UNIV TYPE('a)  length (remdups xs)" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

320 
by(simp_all add: List.card_set card_Compl card_UNIV) 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

321 

9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

322 
lemma card'_UNIV [code_unfold]: "card' (UNIV :: 'a :: card_UNIV set) = card_UNIV TYPE('a)" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

323 
by(simp add: card_UNIV) 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

324 

9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

325 
definition finite' :: "'a set \<Rightarrow> bool" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

326 
where [simp, code del, code_abbrev]: "finite' = finite" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

327 

9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

328 
lemma finite'_code [code]: 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

329 
"finite' (set xs) \<longleftrightarrow> True" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

330 
"finite' (List.coset xs) \<longleftrightarrow> CARD('a) > 0" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

331 
by(simp_all add: card_gt_0_iff) 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

332 

9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

333 
definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

334 
where [simp, code del, code_abbrev]: "subset' = op \<subseteq>" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

335 

9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

336 
lemma subset'_code [code]: 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

337 
"subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

338 
"subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

339 
"subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

340 
by(auto simp add: Let_def card_gt_0_iff dest: card_eq_UNIV_imp_eq_UNIV intro: arg_cong[where f=card]) 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

341 
(metis finite_compl finite_set rev_finite_subset) 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

342 

9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

343 
definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

344 
where [simp, code del, code_abbrev]: "eq_set = op =" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

345 

48051  346 
lemma eq_set_code [code]: 
48062
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

347 
fixes ys 
48051  348 
defines "rhs \<equiv> 
48059  349 
let n = CARD('a) 
48051  350 
in if n = 0 then False else 
351 
let xs' = remdups xs; ys' = remdups ys 

352 
in length xs' + length ys' = n \<and> (\<forall>x \<in> set xs'. x \<notin> set ys') \<and> (\<forall>y \<in> set ys'. y \<notin> set xs')" 

353 
shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1) 

354 
and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2) 

355 
and "eq_set (set xs) (set ys) \<longleftrightarrow> (\<forall>x \<in> set xs. x \<in> set ys) \<and> (\<forall>y \<in> set ys. y \<in> set xs)" (is ?thesis3) 

356 
and "eq_set (List.coset xs) (List.coset ys) \<longleftrightarrow> (\<forall>x \<in> set xs. x \<in> set ys) \<and> (\<forall>y \<in> set ys. y \<in> set xs)" (is ?thesis4) 

357 
proof  

358 
show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs") 

359 
proof 

360 
assume ?lhs thus ?rhs 

361 
by(auto simp add: rhs_def Let_def List.card_set[symmetric] card_Un_Int[where A="set xs" and B=" set xs"] card_UNIV Compl_partition card_gt_0_iff dest: sym)(metis finite_compl finite_set) 

362 
next 

363 
assume ?rhs 

364 
moreover have "\<lbrakk> \<forall>y\<in>set xs. y \<notin> set ys; \<forall>x\<in>set ys. x \<notin> set xs \<rbrakk> \<Longrightarrow> set xs \<inter> set ys = {}" by blast 

365 
ultimately show ?lhs 

366 
by(auto simp add: rhs_def Let_def List.card_set[symmetric] card_UNIV card_gt_0_iff card_Un_Int[where A="set xs" and B="set ys"] dest: card_eq_UNIV_imp_eq_UNIV split: split_if_asm) 

367 
qed 

368 
thus ?thesis2 unfolding eq_set_def by blast 

369 
show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+ 

370 
qed 

371 

48062
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

372 
end 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

373 

9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

374 
notepad begin (* test code setup *) 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

375 
have "List.coset [True] = set [False] \<and> List.coset [] \<subseteq> List.set [True, False] \<and> finite (List.coset [True])" 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

376 
by eval 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

377 
end 
9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

378 

9014e78ccde2
improved code setup for card, finite, subset
Andreas Lochbihler
parents:
48060
diff
changeset

379 
hide_const (open) card' finite' subset' eq_set 
48051  380 

381 
end 