src/HOL/Finite.ML
author nipkow
Mon Apr 27 16:45:11 1998 +0200 (1998-04-27)
changeset 4830 bd73675adbed
parent 4775 66b1a7c42d94
child 5069 3ea049f7979d
permissions -rw-r--r--
Added a few lemmas.
Renamed expand_const -> split_const.
     1 (*  Title:      HOL/Finite.thy
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson & Tobias Nipkow
     4     Copyright   1995  University of Cambridge & TU Muenchen
     5 
     6 Finite sets and their cardinality
     7 *)
     8 
     9 open Finite;
    10 
    11 section "finite";
    12 
    13 (*Discharging ~ x:y entails extra work*)
    14 val major::prems = goal Finite.thy 
    15     "[| finite F;  P({}); \
    16 \       !!F x. [| finite F;  x ~: F;  P(F) |] ==> P(insert x F) \
    17 \    |] ==> P(F)";
    18 by (rtac (major RS Finites.induct) 1);
    19 by (excluded_middle_tac "a:A" 2);
    20 by (etac (insert_absorb RS ssubst) 3 THEN assume_tac 3);   (*backtracking!*)
    21 by (REPEAT (ares_tac prems 1));
    22 qed "finite_induct";
    23 
    24 val major::subs::prems = goal Finite.thy 
    25     "[| finite F;  F <= A; \
    26 \       P({}); \
    27 \       !!F a. [| finite F; a:A; a ~: F;  P(F) |] ==> P(insert a F) \
    28 \    |] ==> P(F)";
    29 by (rtac (subs RS rev_mp) 1);
    30 by (rtac (major RS finite_induct) 1);
    31 by (ALLGOALS (blast_tac (claset() addIs prems)));
    32 qed "finite_subset_induct";
    33 
    34 Addsimps Finites.intrs;
    35 AddSIs Finites.intrs;
    36 
    37 (*The union of two finite sets is finite*)
    38 val major::prems = goal Finite.thy
    39     "[| finite F;  finite G |] ==> finite(F Un G)";
    40 by (rtac (major RS finite_induct) 1);
    41 by (ALLGOALS (asm_simp_tac (simpset() addsimps prems)));
    42 qed "finite_UnI";
    43 
    44 (*Every subset of a finite set is finite*)
    45 goal Finite.thy "!!B. finite B ==> ALL A. A<=B --> finite A";
    46 by (etac finite_induct 1);
    47 by (Simp_tac 1);
    48 by (safe_tac (claset() addSDs [subset_insert_iff RS iffD1]));
    49 by (eres_inst_tac [("t","A")] (insert_Diff RS subst) 2);
    50 by (ALLGOALS Asm_simp_tac);
    51 val lemma = result();
    52 
    53 goal Finite.thy "!!A. [| A<=B;  finite B |] ==> finite A";
    54 by (dtac lemma 1);
    55 by (Blast_tac 1);
    56 qed "finite_subset";
    57 
    58 goal Finite.thy "finite(F Un G) = (finite F & finite G)";
    59 by (blast_tac (claset() 
    60 	         addIs [read_instantiate [("B", "?AA Un ?BB")] finite_subset, 
    61 			finite_UnI]) 1);
    62 qed "finite_Un";
    63 AddIffs[finite_Un];
    64 
    65 goal Finite.thy "finite(insert a A) = finite A";
    66 by (stac insert_is_Un 1);
    67 by (simp_tac (HOL_ss addsimps [finite_Un]) 1);
    68 by (Blast_tac 1);
    69 qed "finite_insert";
    70 Addsimps[finite_insert];
    71 
    72 (*The image of a finite set is finite *)
    73 goal Finite.thy  "!!F. finite F ==> finite(h``F)";
    74 by (etac finite_induct 1);
    75 by (Simp_tac 1);
    76 by (Asm_simp_tac 1);
    77 qed "finite_imageI";
    78 
    79 val major::prems = goal Finite.thy 
    80     "[| finite c;  finite b;                                  \
    81 \       P(b);                                                   \
    82 \       !!x y. [| finite y;  x:y;  P(y) |] ==> P(y-{x}) \
    83 \    |] ==> c<=b --> P(b-c)";
    84 by (rtac (major RS finite_induct) 1);
    85 by (stac Diff_insert 2);
    86 by (ALLGOALS (asm_simp_tac
    87                 (simpset() addsimps (prems@[Diff_subset RS finite_subset]))));
    88 val lemma = result();
    89 
    90 val prems = goal Finite.thy 
    91     "[| finite A;                                       \
    92 \       P(A);                                           \
    93 \       !!a A. [| finite A;  a:A;  P(A) |] ==> P(A-{a}) \
    94 \    |] ==> P({})";
    95 by (rtac (Diff_cancel RS subst) 1);
    96 by (rtac (lemma RS mp) 1);
    97 by (REPEAT (ares_tac (subset_refl::prems) 1));
    98 qed "finite_empty_induct";
    99 
   100 
   101 (* finite B ==> finite (B - Ba) *)
   102 bind_thm ("finite_Diff", Diff_subset RS finite_subset);
   103 Addsimps [finite_Diff];
   104 
   105 goal Finite.thy "finite(A-{a}) = finite(A)";
   106 by (case_tac "a:A" 1);
   107 by (rtac (finite_insert RS sym RS trans) 1);
   108 by (stac insert_Diff 1);
   109 by (ALLGOALS Asm_simp_tac);
   110 qed "finite_Diff_singleton";
   111 AddIffs [finite_Diff_singleton];
   112 
   113 (*Lemma for proving finite_imageD*)
   114 goal Finite.thy "!!A. finite B ==> !A. f``A = B --> inj_on f A --> finite A";
   115 by (etac finite_induct 1);
   116  by (ALLGOALS Asm_simp_tac);
   117 by (Clarify_tac 1);
   118 by (subgoal_tac "EX y:A. f y = x & F = f``(A-{y})" 1);
   119  by (Clarify_tac 1);
   120  by (full_simp_tac (simpset() addsimps [inj_on_def]) 1);
   121  by (Blast_tac 1);
   122 by (thin_tac "ALL A. ?PP(A)" 1);
   123 by (forward_tac [[equalityD2, insertI1] MRS subsetD] 1);
   124 by (Clarify_tac 1);
   125 by (res_inst_tac [("x","xa")] bexI 1);
   126 by (ALLGOALS 
   127     (asm_full_simp_tac (simpset() addsimps [inj_on_image_set_diff])));
   128 val lemma = result();
   129 
   130 goal Finite.thy "!!A. [| finite(f``A);  inj_on f A |] ==> finite A";
   131 by (dtac lemma 1);
   132 by (Blast_tac 1);
   133 qed "finite_imageD";
   134 
   135 (** The finite UNION of finite sets **)
   136 
   137 val [prem] = goal Finite.thy
   138  "finite A ==> (!a:A. finite(B a)) --> finite(UN a:A. B a)";
   139 by (rtac (prem RS finite_induct) 1);
   140 by (ALLGOALS Asm_simp_tac);
   141 bind_thm("finite_UnionI", ballI RSN (2, result() RS mp));
   142 Addsimps [finite_UnionI];
   143 
   144 (** Sigma of finite sets **)
   145 
   146 goalw Finite.thy [Sigma_def]
   147  "!!A. [| finite A; !a:A. finite(B a) |] ==> finite(SIGMA a:A. B a)";
   148 by (blast_tac (claset() addSIs [finite_UnionI]) 1);
   149 bind_thm("finite_SigmaI", ballI RSN (2,result()));
   150 Addsimps [finite_SigmaI];
   151 
   152 (** The powerset of a finite set **)
   153 
   154 goal Finite.thy "!!A. finite(Pow A) ==> finite A";
   155 by (subgoal_tac "finite ((%x.{x})``A)" 1);
   156 by (rtac finite_subset 2);
   157 by (assume_tac 3);
   158 by (ALLGOALS
   159     (fast_tac (claset() addSDs [rewrite_rule [inj_on_def] finite_imageD])));
   160 val lemma = result();
   161 
   162 goal Finite.thy "finite(Pow A) = finite A";
   163 by (rtac iffI 1);
   164 by (etac lemma 1);
   165 (*Opposite inclusion: finite A ==> finite (Pow A) *)
   166 by (etac finite_induct 1);
   167 by (ALLGOALS 
   168     (asm_simp_tac
   169      (simpset() addsimps [finite_UnI, finite_imageI, Pow_insert])));
   170 qed "finite_Pow_iff";
   171 AddIffs [finite_Pow_iff];
   172 
   173 goal Finite.thy "finite(r^-1) = finite r";
   174 by (subgoal_tac "r^-1 = (%(x,y).(y,x))``r" 1);
   175  by (Asm_simp_tac 1);
   176  by (rtac iffI 1);
   177   by (etac (rewrite_rule [inj_on_def] finite_imageD) 1);
   178   by (simp_tac (simpset() addsplits [split_split]) 1);
   179  by (etac finite_imageI 1);
   180 by (simp_tac (simpset() addsimps [converse_def,image_def]) 1);
   181 by Auto_tac;
   182  by (rtac bexI 1);
   183  by (assume_tac 2);
   184 by (Simp_tac 1);
   185 qed "finite_converse";
   186 AddIffs [finite_converse];
   187 
   188 section "Finite cardinality -- 'card'";
   189 
   190 goal Set.thy "{f i |i. (P i | i=n)} = insert (f n) {f i|i. P i}";
   191 by (Blast_tac 1);
   192 val Collect_conv_insert = result();
   193 
   194 goalw Finite.thy [card_def] "card {} = 0";
   195 by (rtac Least_equality 1);
   196 by (ALLGOALS Asm_full_simp_tac);
   197 qed "card_empty";
   198 Addsimps [card_empty];
   199 
   200 val [major] = goal Finite.thy
   201   "finite A ==> ? (n::nat) f. A = {f i |i. i<n}";
   202 by (rtac (major RS finite_induct) 1);
   203  by (res_inst_tac [("x","0")] exI 1);
   204  by (Simp_tac 1);
   205 by (etac exE 1);
   206 by (etac exE 1);
   207 by (hyp_subst_tac 1);
   208 by (res_inst_tac [("x","Suc n")] exI 1);
   209 by (res_inst_tac [("x","%i. if i<n then f i else x")] exI 1);
   210 by (asm_simp_tac (simpset() addsimps [Collect_conv_insert, less_Suc_eq]
   211                           addcongs [rev_conj_cong]) 1);
   212 qed "finite_has_card";
   213 
   214 goal Finite.thy
   215   "!!A.[| x ~: A; insert x A = {f i|i. i<n} |] ==> \
   216 \  ? m::nat. m<n & (? g. A = {g i|i. i<m})";
   217 by (res_inst_tac [("n","n")] natE 1);
   218  by (hyp_subst_tac 1);
   219  by (Asm_full_simp_tac 1);
   220 by (rename_tac "m" 1);
   221 by (hyp_subst_tac 1);
   222 by (case_tac "? a. a:A" 1);
   223  by (res_inst_tac [("x","0")] exI 2);
   224  by (Simp_tac 2);
   225  by (Blast_tac 2);
   226 by (etac exE 1);
   227 by (simp_tac (simpset() addsimps [less_Suc_eq]) 1);
   228 by (rtac exI 1);
   229 by (rtac (refl RS disjI2 RS conjI) 1);
   230 by (etac equalityE 1);
   231 by (asm_full_simp_tac
   232      (simpset() addsimps [subset_insert,Collect_conv_insert, less_Suc_eq]) 1);
   233 by Safe_tac;
   234   by (Asm_full_simp_tac 1);
   235   by (res_inst_tac [("x","%i. if f i = f m then a else f i")] exI 1);
   236   by (SELECT_GOAL Safe_tac 1);
   237    by (subgoal_tac "x ~= f m" 1);
   238     by (Blast_tac 2);
   239    by (subgoal_tac "? k. f k = x & k<m" 1);
   240     by (Blast_tac 2);
   241    by (SELECT_GOAL Safe_tac 1);
   242    by (res_inst_tac [("x","k")] exI 1);
   243    by (Asm_simp_tac 1);
   244   by (Simp_tac 1);
   245   by (Blast_tac 1);
   246  by (dtac sym 1);
   247  by (rotate_tac ~1 1);
   248  by (Asm_full_simp_tac 1);
   249  by (res_inst_tac [("x","%i. if f i = f m then a else f i")] exI 1);
   250  by (SELECT_GOAL Safe_tac 1);
   251   by (subgoal_tac "x ~= f m" 1);
   252    by (Blast_tac 2);
   253   by (subgoal_tac "? k. f k = x & k<m" 1);
   254    by (Blast_tac 2);
   255   by (SELECT_GOAL Safe_tac 1);
   256   by (res_inst_tac [("x","k")] exI 1);
   257   by (Asm_simp_tac 1);
   258  by (Simp_tac 1);
   259  by (Blast_tac 1);
   260 by (res_inst_tac [("x","%j. if f j = f i then f m else f j")] exI 1);
   261 by (SELECT_GOAL Safe_tac 1);
   262  by (subgoal_tac "x ~= f i" 1);
   263   by (Blast_tac 2);
   264  by (case_tac "x = f m" 1);
   265   by (res_inst_tac [("x","i")] exI 1);
   266   by (Asm_simp_tac 1);
   267  by (subgoal_tac "? k. f k = x & k<m" 1);
   268   by (Blast_tac 2);
   269  by (SELECT_GOAL Safe_tac 1);
   270  by (res_inst_tac [("x","k")] exI 1);
   271  by (Asm_simp_tac 1);
   272 by (Simp_tac 1);
   273 by (Blast_tac 1);
   274 val lemma = result();
   275 
   276 goal Finite.thy "!!A. [| finite A; x ~: A |] ==> \
   277 \ (LEAST n. ? f. insert x A = {f i|i. i<n}) = Suc(LEAST n. ? f. A={f i|i. i<n})";
   278 by (rtac Least_equality 1);
   279  by (dtac finite_has_card 1);
   280  by (etac exE 1);
   281  by (dres_inst_tac [("P","%n.? f. A={f i|i. i<n}")] LeastI 1);
   282  by (etac exE 1);
   283  by (res_inst_tac
   284    [("x","%i. if i<(LEAST n. ? f. A={f i |i. i < n}) then f i else x")] exI 1);
   285  by (simp_tac
   286     (simpset() addsimps [Collect_conv_insert, less_Suc_eq] 
   287               addcongs [rev_conj_cong]) 1);
   288  by (etac subst 1);
   289  by (rtac refl 1);
   290 by (rtac notI 1);
   291 by (etac exE 1);
   292 by (dtac lemma 1);
   293  by (assume_tac 1);
   294 by (etac exE 1);
   295 by (etac conjE 1);
   296 by (dres_inst_tac [("P","%x. ? g. A = {g i |i. i < x}")] Least_le 1);
   297 by (dtac le_less_trans 1 THEN atac 1);
   298 by (asm_full_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
   299 by (etac disjE 1);
   300 by (etac less_asym 1 THEN atac 1);
   301 by (hyp_subst_tac 1);
   302 by (Asm_full_simp_tac 1);
   303 val lemma = result();
   304 
   305 goalw Finite.thy [card_def]
   306   "!!A. [| finite A; x ~: A |] ==> card(insert x A) = Suc(card A)";
   307 by (etac lemma 1);
   308 by (assume_tac 1);
   309 qed "card_insert_disjoint";
   310 Addsimps [card_insert_disjoint];
   311 
   312 goal Finite.thy "!!A. finite A ==> card A <= card (insert x A)";
   313 by (case_tac "x: A" 1);
   314 by (ALLGOALS (asm_simp_tac (simpset() addsimps [insert_absorb])));
   315 qed "card_insert_le";
   316 
   317 goal Finite.thy  "!!A. finite A ==> !B. B <= A --> card(B) <= card(A)";
   318 by (etac finite_induct 1);
   319 by (Simp_tac 1);
   320 by (Clarify_tac 1);
   321 by (case_tac "x:B" 1);
   322  by (dres_inst_tac [("A","B")] mk_disjoint_insert 1);
   323 by (asm_full_simp_tac (simpset() addsimps [subset_insert_iff]) 2);
   324 by (fast_tac (claset() addss
   325 	      (simpset() addsimps [subset_insert_iff, finite_subset])) 1);
   326 qed_spec_mp "card_mono";
   327 
   328 goal Finite.thy "!!A B. [| finite A; finite B |]\
   329 \                       ==> A Int B = {} --> card(A Un B) = card A + card B";
   330 by (etac finite_induct 1);
   331 by (ALLGOALS (asm_simp_tac (simpset() addsimps [Int_insert_left])));
   332 qed_spec_mp "card_Un_disjoint";
   333 
   334 goal Finite.thy "!!A. [| finite A; B<=A |] ==> card A - card B = card (A - B)";
   335 by (subgoal_tac "(A-B) Un B = A" 1);
   336 by (Blast_tac 2);
   337 by (rtac (add_right_cancel RS iffD1) 1);
   338 by (rtac (card_Un_disjoint RS subst) 1);
   339 by (etac ssubst 4);
   340 by (Blast_tac 3);
   341 by (ALLGOALS 
   342     (asm_simp_tac
   343      (simpset() addsimps [add_commute, not_less_iff_le, 
   344 			 add_diff_inverse, card_mono, finite_subset])));
   345 qed "card_Diff_subset";
   346 
   347 goal Finite.thy "!!A. [| finite A; x: A |] ==> Suc(card(A-{x})) = card A";
   348 by (res_inst_tac [("t", "A")] (insert_Diff RS subst) 1);
   349 by (assume_tac 1);
   350 by (Asm_simp_tac 1);
   351 qed "card_Suc_Diff";
   352 
   353 goal Finite.thy "!!A. [| finite A; x: A |] ==> card(A-{x}) < card A";
   354 by (rtac Suc_less_SucD 1);
   355 by (asm_simp_tac (simpset() addsimps [card_Suc_Diff]) 1);
   356 qed "card_Diff";
   357 
   358 goal Finite.thy "!!A. finite A ==> card(A-{x}) <= card A";
   359 by (case_tac "x: A" 1);
   360 by (ALLGOALS (asm_simp_tac (simpset() addsimps [card_Diff, less_imp_le])));
   361 qed "card_Diff_le";
   362 
   363 
   364 (*** Cardinality of the Powerset ***)
   365 
   366 goal Finite.thy "!!A. finite A ==> card(insert x A) = Suc(card(A-{x}))";
   367 by (case_tac "x:A" 1);
   368 by (ALLGOALS 
   369     (asm_simp_tac (simpset() addsimps [card_Suc_Diff, insert_absorb])));
   370 qed "card_insert";
   371 Addsimps [card_insert];
   372 
   373 goal Finite.thy "!!A. finite(A) ==> inj_on f A --> card (f `` A) = card A";
   374 by (etac finite_induct 1);
   375 by (ALLGOALS Asm_simp_tac);
   376 by Safe_tac;
   377 by (rewtac inj_on_def);
   378 by (Blast_tac 1);
   379 by (stac card_insert_disjoint 1);
   380 by (etac finite_imageI 1);
   381 by (Blast_tac 1);
   382 by (Blast_tac 1);
   383 qed_spec_mp "card_image";
   384 
   385 goal thy "!!A. finite A ==> card (Pow A) = 2 ^ card A";
   386 by (etac finite_induct 1);
   387 by (ALLGOALS (asm_simp_tac (simpset() addsimps [Pow_insert])));
   388 by (stac card_Un_disjoint 1);
   389 by (EVERY (map (blast_tac (claset() addIs [finite_imageI])) [3,2,1]));
   390 by (subgoal_tac "inj_on (insert x) (Pow F)" 1);
   391 by (asm_simp_tac (simpset() addsimps [card_image, Pow_insert]) 1);
   392 by (rewtac inj_on_def);
   393 by (blast_tac (claset() addSEs [equalityE]) 1);
   394 qed "card_Pow";
   395 Addsimps [card_Pow];
   396 
   397 
   398 (*Proper subsets*)
   399 goalw Finite.thy [psubset_def]
   400     "!!B. finite B ==> !A. A < B --> card(A) < card(B)";
   401 by (etac finite_induct 1);
   402 by (Simp_tac 1);
   403 by (Clarify_tac 1);
   404 by (case_tac "x:A" 1);
   405 (*1*)
   406 by (dres_inst_tac [("A","A")]mk_disjoint_insert 1);
   407 by (Clarify_tac 1);
   408 by (rotate_tac ~3 1);
   409 by (asm_full_simp_tac (simpset() addsimps [finite_subset]) 1);
   410 by (Blast_tac 1);
   411 (*2*)
   412 by (eres_inst_tac [("P","?a<?b")] notE 1);
   413 by (asm_full_simp_tac (simpset() addsimps [subset_insert_iff]) 1);
   414 by (case_tac "A=F" 1);
   415 by (ALLGOALS Asm_simp_tac);
   416 qed_spec_mp "psubset_card" ;
   417 
   418 
   419 (*Relates to equivalence classes.   Based on a theorem of F. Kammueller's.
   420   The "finite C" premise is redundant*)
   421 goal thy "!!C. finite C ==> finite (Union C) --> \
   422 \          (! c : C. k dvd card c) -->  \
   423 \          (! c1: C. ! c2: C. c1 ~= c2 --> c1 Int c2 = {}) \
   424 \          --> k dvd card(Union C)";
   425 by (etac finite_induct 1);
   426 by (ALLGOALS Asm_simp_tac);
   427 by (Clarify_tac 1);
   428 by (stac card_Un_disjoint 1);
   429 by (ALLGOALS
   430     (asm_full_simp_tac (simpset()
   431 			 addsimps [dvd_add, disjoint_eq_subset_Compl])));
   432 by (thin_tac "!c:F. ?PP(c)" 1);
   433 by (thin_tac "!c:F. ?PP(c) & ?QQ(c)" 1);
   434 by (Clarify_tac 1);
   435 by (ball_tac 1);
   436 by (Blast_tac 1);
   437 qed_spec_mp "dvd_partition";
   438