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