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