src/HOL/Finite.ML
author nipkow
Tue Oct 06 14:39:53 1998 +0200 (1998-10-06)
changeset 5616 497eeeace3fc
parent 5537 c2bd39a2c0ee
child 5626 f67c34721486
permissions -rw-r--r--
Merges FoldSet into Finite
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
nipkow@3413
     9
section "finite";
nipkow@1531
    10
clasohm@923
    11
(*Discharging ~ x:y entails extra work*)
paulson@5316
    12
val major::prems = Goal 
nipkow@3413
    13
    "[| finite F;  P({}); \
nipkow@3413
    14
\       !!F x. [| finite F;  x ~: F;  P(F) |] ==> P(insert x F) \
clasohm@923
    15
\    |] ==> P(F)";
nipkow@3413
    16
by (rtac (major RS Finites.induct) 1);
nipkow@3413
    17
by (excluded_middle_tac "a:A" 2);
clasohm@923
    18
by (etac (insert_absorb RS ssubst) 3 THEN assume_tac 3);   (*backtracking!*)
clasohm@923
    19
by (REPEAT (ares_tac prems 1));
nipkow@3413
    20
qed "finite_induct";
nipkow@3413
    21
paulson@5316
    22
val major::subs::prems = Goal 
nipkow@3413
    23
    "[| finite F;  F <= A; \
nipkow@3413
    24
\       P({}); \
nipkow@3413
    25
\       !!F a. [| finite F; a:A; a ~: F;  P(F) |] ==> P(insert a F) \
nipkow@3413
    26
\    |] ==> P(F)";
paulson@4386
    27
by (rtac (subs RS rev_mp) 1);
paulson@4386
    28
by (rtac (major RS finite_induct) 1);
paulson@4386
    29
by (ALLGOALS (blast_tac (claset() addIs prems)));
nipkow@3413
    30
qed "finite_subset_induct";
nipkow@3413
    31
nipkow@3413
    32
Addsimps Finites.intrs;
nipkow@3413
    33
AddSIs Finites.intrs;
clasohm@923
    34
clasohm@923
    35
(*The union of two finite sets is finite*)
paulson@5316
    36
Goal "[| finite F;  finite G |] ==> finite(F Un G)";
paulson@5316
    37
by (etac finite_induct 1);
paulson@5316
    38
by (ALLGOALS Asm_simp_tac);
nipkow@3413
    39
qed "finite_UnI";
clasohm@923
    40
clasohm@923
    41
(*Every subset of a finite set is finite*)
paulson@5143
    42
Goal "finite B ==> ALL A. A<=B --> finite A";
paulson@4304
    43
by (etac finite_induct 1);
paulson@4304
    44
by (Simp_tac 1);
wenzelm@4089
    45
by (safe_tac (claset() addSDs [subset_insert_iff RS iffD1]));
paulson@4304
    46
by (eres_inst_tac [("t","A")] (insert_Diff RS subst) 2);
clasohm@1264
    47
by (ALLGOALS Asm_simp_tac);
paulson@4304
    48
val lemma = result();
paulson@4304
    49
paulson@5143
    50
Goal "[| A<=B;  finite B |] ==> finite A";
wenzelm@4423
    51
by (dtac lemma 1);
paulson@4304
    52
by (Blast_tac 1);
nipkow@3413
    53
qed "finite_subset";
clasohm@923
    54
wenzelm@5069
    55
Goal "finite(F Un G) = (finite F & finite G)";
paulson@4304
    56
by (blast_tac (claset() 
paulson@5413
    57
	         addIs [read_instantiate [("B", "?X Un ?Y")] finite_subset, 
paulson@4304
    58
			finite_UnI]) 1);
nipkow@3413
    59
qed "finite_Un";
nipkow@3413
    60
AddIffs[finite_Un];
nipkow@1531
    61
paulson@5413
    62
Goal "finite F ==> finite(F Int G)";
paulson@5413
    63
by (blast_tac (claset() addIs [finite_subset]) 1);
paulson@5413
    64
qed "finite_Int1";
paulson@5413
    65
paulson@5413
    66
Goal "finite G ==> finite(F Int G)";
paulson@5413
    67
by (blast_tac (claset() addIs [finite_subset]) 1);
paulson@5413
    68
qed "finite_Int2";
paulson@5413
    69
paulson@5413
    70
Addsimps[finite_Int1, finite_Int2];
paulson@5413
    71
AddIs[finite_Int1, finite_Int2];
paulson@5413
    72
paulson@5413
    73
wenzelm@5069
    74
Goal "finite(insert a A) = finite A";
paulson@1553
    75
by (stac insert_is_Un 1);
nipkow@3413
    76
by (simp_tac (HOL_ss addsimps [finite_Un]) 1);
paulson@3427
    77
by (Blast_tac 1);
nipkow@3413
    78
qed "finite_insert";
nipkow@3413
    79
Addsimps[finite_insert];
nipkow@1531
    80
nipkow@3413
    81
(*The image of a finite set is finite *)
paulson@5143
    82
Goal  "finite F ==> finite(h``F)";
nipkow@3413
    83
by (etac finite_induct 1);
clasohm@1264
    84
by (Simp_tac 1);
nipkow@3413
    85
by (Asm_simp_tac 1);
nipkow@3413
    86
qed "finite_imageI";
clasohm@923
    87
paulson@5316
    88
val major::prems = Goal 
nipkow@3413
    89
    "[| finite c;  finite b;                                  \
clasohm@1465
    90
\       P(b);                                                   \
nipkow@3413
    91
\       !!x y. [| finite y;  x:y;  P(y) |] ==> P(y-{x}) \
clasohm@923
    92
\    |] ==> c<=b --> P(b-c)";
nipkow@3413
    93
by (rtac (major RS finite_induct) 1);
paulson@2031
    94
by (stac Diff_insert 2);
clasohm@923
    95
by (ALLGOALS (asm_simp_tac
paulson@5537
    96
                (simpset() addsimps prems@[Diff_subset RS finite_subset])));
nipkow@1531
    97
val lemma = result();
clasohm@923
    98
paulson@5316
    99
val prems = Goal 
nipkow@3413
   100
    "[| finite A;                                       \
nipkow@3413
   101
\       P(A);                                           \
nipkow@3413
   102
\       !!a A. [| finite A;  a:A;  P(A) |] ==> P(A-{a}) \
clasohm@923
   103
\    |] ==> P({})";
clasohm@923
   104
by (rtac (Diff_cancel RS subst) 1);
nipkow@1531
   105
by (rtac (lemma RS mp) 1);
clasohm@923
   106
by (REPEAT (ares_tac (subset_refl::prems) 1));
nipkow@3413
   107
qed "finite_empty_induct";
nipkow@1531
   108
nipkow@1531
   109
paulson@1618
   110
(* finite B ==> finite (B - Ba) *)
paulson@1618
   111
bind_thm ("finite_Diff", Diff_subset RS finite_subset);
nipkow@1531
   112
Addsimps [finite_Diff];
nipkow@1531
   113
wenzelm@5069
   114
Goal "finite(A-{a}) = finite(A)";
paulson@3368
   115
by (case_tac "a:A" 1);
paulson@3457
   116
by (rtac (finite_insert RS sym RS trans) 1);
paulson@3368
   117
by (stac insert_Diff 1);
paulson@3368
   118
by (ALLGOALS Asm_simp_tac);
paulson@3368
   119
qed "finite_Diff_singleton";
paulson@3368
   120
AddIffs [finite_Diff_singleton];
paulson@3368
   121
paulson@4059
   122
(*Lemma for proving finite_imageD*)
paulson@5143
   123
Goal "finite B ==> !A. f``A = B --> inj_on f A --> finite A";
paulson@1553
   124
by (etac finite_induct 1);
nipkow@3413
   125
 by (ALLGOALS Asm_simp_tac);
paulson@3708
   126
by (Clarify_tac 1);
nipkow@3413
   127
by (subgoal_tac "EX y:A. f y = x & F = f``(A-{y})" 1);
paulson@3708
   128
 by (Clarify_tac 1);
nipkow@4830
   129
 by (full_simp_tac (simpset() addsimps [inj_on_def]) 1);
nipkow@3413
   130
 by (Blast_tac 1);
paulson@3368
   131
by (thin_tac "ALL A. ?PP(A)" 1);
nipkow@3413
   132
by (forward_tac [[equalityD2, insertI1] MRS subsetD] 1);
paulson@3708
   133
by (Clarify_tac 1);
paulson@3368
   134
by (res_inst_tac [("x","xa")] bexI 1);
paulson@4059
   135
by (ALLGOALS 
nipkow@4830
   136
    (asm_full_simp_tac (simpset() addsimps [inj_on_image_set_diff])));
paulson@3368
   137
val lemma = result();
paulson@3368
   138
paulson@5143
   139
Goal "[| finite(f``A);  inj_on f A |] ==> finite A";
paulson@3457
   140
by (dtac lemma 1);
paulson@3368
   141
by (Blast_tac 1);
paulson@3368
   142
qed "finite_imageD";
paulson@3368
   143
nipkow@4014
   144
(** The finite UNION of finite sets **)
nipkow@4014
   145
paulson@5316
   146
Goal "finite A ==> (!a:A. finite(B a)) --> finite(UN a:A. B a)";
paulson@5316
   147
by (etac finite_induct 1);
paulson@4153
   148
by (ALLGOALS Asm_simp_tac);
nipkow@4014
   149
bind_thm("finite_UnionI", ballI RSN (2, result() RS mp));
nipkow@4014
   150
Addsimps [finite_UnionI];
nipkow@4014
   151
nipkow@4014
   152
(** Sigma of finite sets **)
nipkow@4014
   153
wenzelm@5069
   154
Goalw [Sigma_def]
paulson@5148
   155
 "[| finite A; !a:A. finite(B a) |] ==> finite(SIGMA a:A. B a)";
paulson@4153
   156
by (blast_tac (claset() addSIs [finite_UnionI]) 1);
nipkow@4014
   157
bind_thm("finite_SigmaI", ballI RSN (2,result()));
nipkow@4014
   158
Addsimps [finite_SigmaI];
paulson@3368
   159
paulson@3368
   160
(** The powerset of a finite set **)
paulson@3368
   161
paulson@5143
   162
Goal "finite(Pow A) ==> finite A";
paulson@3368
   163
by (subgoal_tac "finite ((%x.{x})``A)" 1);
paulson@3457
   164
by (rtac finite_subset 2);
paulson@3457
   165
by (assume_tac 3);
paulson@3368
   166
by (ALLGOALS
nipkow@4830
   167
    (fast_tac (claset() addSDs [rewrite_rule [inj_on_def] finite_imageD])));
paulson@3368
   168
val lemma = result();
paulson@3368
   169
wenzelm@5069
   170
Goal "finite(Pow A) = finite A";
paulson@3457
   171
by (rtac iffI 1);
paulson@3457
   172
by (etac lemma 1);
paulson@3368
   173
(*Opposite inclusion: finite A ==> finite (Pow A) *)
paulson@3340
   174
by (etac finite_induct 1);
paulson@3340
   175
by (ALLGOALS 
paulson@3340
   176
    (asm_simp_tac
wenzelm@4089
   177
     (simpset() addsimps [finite_UnI, finite_imageI, Pow_insert])));
paulson@3368
   178
qed "finite_Pow_iff";
paulson@3368
   179
AddIffs [finite_Pow_iff];
paulson@3340
   180
wenzelm@5069
   181
Goal "finite(r^-1) = finite r";
paulson@3457
   182
by (subgoal_tac "r^-1 = (%(x,y).(y,x))``r" 1);
paulson@3457
   183
 by (Asm_simp_tac 1);
paulson@3457
   184
 by (rtac iffI 1);
nipkow@4830
   185
  by (etac (rewrite_rule [inj_on_def] finite_imageD) 1);
nipkow@4830
   186
  by (simp_tac (simpset() addsplits [split_split]) 1);
paulson@3457
   187
 by (etac finite_imageI 1);
paulson@4746
   188
by (simp_tac (simpset() addsimps [converse_def,image_def]) 1);
paulson@4477
   189
by Auto_tac;
oheimb@5516
   190
by (rtac bexI 1);
oheimb@5516
   191
by  (assume_tac 2);
oheimb@4763
   192
by (Simp_tac 1);
paulson@4746
   193
qed "finite_converse";
paulson@4746
   194
AddIffs [finite_converse];
nipkow@1531
   195
nipkow@1548
   196
section "Finite cardinality -- 'card'";
nipkow@1531
   197
paulson@5316
   198
Goal "{f i |i. (P i | i=n)} = insert (f n) {f i|i. P i}";
paulson@2922
   199
by (Blast_tac 1);
nipkow@1531
   200
val Collect_conv_insert = result();
nipkow@1531
   201
wenzelm@5069
   202
Goalw [card_def] "card {} = 0";
paulson@1553
   203
by (rtac Least_equality 1);
paulson@1553
   204
by (ALLGOALS Asm_full_simp_tac);
nipkow@1531
   205
qed "card_empty";
nipkow@1531
   206
Addsimps [card_empty];
nipkow@1531
   207
paulson@5316
   208
Goal "finite A ==> ? (n::nat) f. A = {f i |i. i<n}";
paulson@5316
   209
by (etac finite_induct 1);
paulson@1553
   210
 by (res_inst_tac [("x","0")] exI 1);
paulson@1553
   211
 by (Simp_tac 1);
paulson@1553
   212
by (etac exE 1);
paulson@1553
   213
by (etac exE 1);
paulson@1553
   214
by (hyp_subst_tac 1);
paulson@1553
   215
by (res_inst_tac [("x","Suc n")] exI 1);
paulson@1553
   216
by (res_inst_tac [("x","%i. if i<n then f i else x")] exI 1);
wenzelm@4089
   217
by (asm_simp_tac (simpset() addsimps [Collect_conv_insert, less_Suc_eq]
nipkow@1548
   218
                          addcongs [rev_conj_cong]) 1);
nipkow@1531
   219
qed "finite_has_card";
nipkow@1531
   220
paulson@5278
   221
Goal "[| x ~: A; insert x A = {f i|i. i<n} |]  \
paulson@5278
   222
\     ==> ? m::nat. m<n & (? g. A = {g i|i. i<m})";
berghofe@5183
   223
by (exhaust_tac "n" 1);
paulson@1553
   224
 by (hyp_subst_tac 1);
paulson@1553
   225
 by (Asm_full_simp_tac 1);
paulson@1553
   226
by (rename_tac "m" 1);
paulson@1553
   227
by (hyp_subst_tac 1);
paulson@1553
   228
by (case_tac "? a. a:A" 1);
paulson@1553
   229
 by (res_inst_tac [("x","0")] exI 2);
paulson@1553
   230
 by (Simp_tac 2);
paulson@2922
   231
 by (Blast_tac 2);
paulson@1553
   232
by (etac exE 1);
wenzelm@4089
   233
by (simp_tac (simpset() addsimps [less_Suc_eq]) 1);
paulson@1553
   234
by (rtac exI 1);
paulson@1782
   235
by (rtac (refl RS disjI2 RS conjI) 1);
paulson@1553
   236
by (etac equalityE 1);
paulson@1553
   237
by (asm_full_simp_tac
wenzelm@4089
   238
     (simpset() addsimps [subset_insert,Collect_conv_insert, less_Suc_eq]) 1);
paulson@4153
   239
by Safe_tac;
paulson@1553
   240
  by (Asm_full_simp_tac 1);
paulson@1553
   241
  by (res_inst_tac [("x","%i. if f i = f m then a else f i")] exI 1);
paulson@4153
   242
  by (SELECT_GOAL Safe_tac 1);
paulson@1553
   243
   by (subgoal_tac "x ~= f m" 1);
paulson@2922
   244
    by (Blast_tac 2);
paulson@1553
   245
   by (subgoal_tac "? k. f k = x & k<m" 1);
paulson@2922
   246
    by (Blast_tac 2);
paulson@4153
   247
   by (SELECT_GOAL Safe_tac 1);
paulson@1553
   248
   by (res_inst_tac [("x","k")] exI 1);
paulson@1553
   249
   by (Asm_simp_tac 1);
nipkow@4686
   250
  by (Simp_tac 1);
paulson@2922
   251
  by (Blast_tac 1);
paulson@3457
   252
 by (dtac sym 1);
paulson@1553
   253
 by (rotate_tac ~1 1);
paulson@1553
   254
 by (Asm_full_simp_tac 1);
paulson@1553
   255
 by (res_inst_tac [("x","%i. if f i = f m then a else f i")] exI 1);
paulson@4153
   256
 by (SELECT_GOAL Safe_tac 1);
paulson@1553
   257
  by (subgoal_tac "x ~= f m" 1);
paulson@2922
   258
   by (Blast_tac 2);
paulson@1553
   259
  by (subgoal_tac "? k. f k = x & k<m" 1);
paulson@2922
   260
   by (Blast_tac 2);
paulson@4153
   261
  by (SELECT_GOAL Safe_tac 1);
paulson@1553
   262
  by (res_inst_tac [("x","k")] exI 1);
paulson@1553
   263
  by (Asm_simp_tac 1);
nipkow@4686
   264
 by (Simp_tac 1);
paulson@2922
   265
 by (Blast_tac 1);
paulson@1553
   266
by (res_inst_tac [("x","%j. if f j = f i then f m else f j")] exI 1);
paulson@4153
   267
by (SELECT_GOAL Safe_tac 1);
paulson@1553
   268
 by (subgoal_tac "x ~= f i" 1);
paulson@2922
   269
  by (Blast_tac 2);
paulson@1553
   270
 by (case_tac "x = f m" 1);
paulson@1553
   271
  by (res_inst_tac [("x","i")] exI 1);
paulson@1553
   272
  by (Asm_simp_tac 1);
paulson@1553
   273
 by (subgoal_tac "? k. f k = x & k<m" 1);
paulson@2922
   274
  by (Blast_tac 2);
paulson@4153
   275
 by (SELECT_GOAL Safe_tac 1);
paulson@1553
   276
 by (res_inst_tac [("x","k")] exI 1);
paulson@1553
   277
 by (Asm_simp_tac 1);
nipkow@4686
   278
by (Simp_tac 1);
paulson@2922
   279
by (Blast_tac 1);
nipkow@1531
   280
val lemma = result();
nipkow@1531
   281
paulson@5143
   282
Goal "[| finite A; x ~: A |] ==> \
wenzelm@3842
   283
\ (LEAST n. ? f. insert x A = {f i|i. i<n}) = Suc(LEAST n. ? f. A={f i|i. i<n})";
paulson@1553
   284
by (rtac Least_equality 1);
paulson@3457
   285
 by (dtac finite_has_card 1);
paulson@3457
   286
 by (etac exE 1);
wenzelm@3842
   287
 by (dres_inst_tac [("P","%n.? f. A={f i|i. i<n}")] LeastI 1);
paulson@3457
   288
 by (etac exE 1);
paulson@1553
   289
 by (res_inst_tac
nipkow@1531
   290
   [("x","%i. if i<(LEAST n. ? f. A={f i |i. i < n}) then f i else x")] exI 1);
paulson@1553
   291
 by (simp_tac
wenzelm@4089
   292
    (simpset() addsimps [Collect_conv_insert, less_Suc_eq] 
paulson@2031
   293
              addcongs [rev_conj_cong]) 1);
paulson@3457
   294
 by (etac subst 1);
paulson@3457
   295
 by (rtac refl 1);
paulson@1553
   296
by (rtac notI 1);
paulson@1553
   297
by (etac exE 1);
paulson@1553
   298
by (dtac lemma 1);
paulson@3457
   299
 by (assume_tac 1);
paulson@1553
   300
by (etac exE 1);
paulson@1553
   301
by (etac conjE 1);
paulson@1553
   302
by (dres_inst_tac [("P","%x. ? g. A = {g i |i. i < x}")] Least_le 1);
paulson@1553
   303
by (dtac le_less_trans 1 THEN atac 1);
wenzelm@4089
   304
by (asm_full_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
paulson@1553
   305
by (etac disjE 1);
paulson@1553
   306
by (etac less_asym 1 THEN atac 1);
paulson@1553
   307
by (hyp_subst_tac 1);
paulson@1553
   308
by (Asm_full_simp_tac 1);
nipkow@1531
   309
val lemma = result();
nipkow@1531
   310
paulson@5416
   311
Goalw [card_def] "[| finite A; x ~: A |] ==> card(insert x A) = Suc(card A)";
paulson@1553
   312
by (etac lemma 1);
paulson@1553
   313
by (assume_tac 1);
nipkow@1531
   314
qed "card_insert_disjoint";
paulson@3352
   315
Addsimps [card_insert_disjoint];
paulson@3352
   316
paulson@5143
   317
Goal "finite A ==> card A <= card (insert x A)";
paulson@4768
   318
by (case_tac "x: A" 1);
paulson@4768
   319
by (ALLGOALS (asm_simp_tac (simpset() addsimps [insert_absorb])));
paulson@4768
   320
qed "card_insert_le";
paulson@4768
   321
paulson@5143
   322
Goal  "finite A ==> !B. B <= A --> card(B) <= card(A)";
paulson@3352
   323
by (etac finite_induct 1);
paulson@3352
   324
by (Simp_tac 1);
paulson@3708
   325
by (Clarify_tac 1);
paulson@3352
   326
by (case_tac "x:B" 1);
nipkow@3413
   327
 by (dres_inst_tac [("A","B")] mk_disjoint_insert 1);
oheimb@5476
   328
 by (asm_full_simp_tac (simpset() addsimps [subset_insert_iff]) 2);
paulson@4775
   329
by (fast_tac (claset() addss
oheimb@5477
   330
	      (simpset() addsimps [subset_insert_iff, finite_subset]
oheimb@5477
   331
			 delsimps [insert_subset])) 1);
paulson@3352
   332
qed_spec_mp "card_mono";
paulson@3352
   333
paulson@5416
   334
paulson@5416
   335
Goal "[| finite A; finite B |] \
paulson@5416
   336
\     ==> card A + card B = card (A Un B) + card (A Int B)";
paulson@3352
   337
by (etac finite_induct 1);
paulson@5416
   338
by (Simp_tac 1);
paulson@5416
   339
by (asm_simp_tac (simpset() addsimps [insert_absorb, Int_insert_left]) 1);
paulson@5416
   340
qed "card_Un_Int";
paulson@5416
   341
paulson@5416
   342
Goal "[| finite A; finite B; A Int B = {} |] \
paulson@5416
   343
\     ==> card (A Un B) = card A + card B";
paulson@5416
   344
by (asm_simp_tac (simpset() addsimps [card_Un_Int]) 1);
paulson@5416
   345
qed "card_Un_disjoint";
paulson@3352
   346
paulson@5143
   347
Goal "[| finite A; B<=A |] ==> card A - card B = card (A - B)";
paulson@3352
   348
by (subgoal_tac "(A-B) Un B = A" 1);
paulson@3352
   349
by (Blast_tac 2);
paulson@3457
   350
by (rtac (add_right_cancel RS iffD1) 1);
paulson@3457
   351
by (rtac (card_Un_disjoint RS subst) 1);
paulson@3457
   352
by (etac ssubst 4);
paulson@3352
   353
by (Blast_tac 3);
paulson@3352
   354
by (ALLGOALS 
paulson@3352
   355
    (asm_simp_tac
wenzelm@4089
   356
     (simpset() addsimps [add_commute, not_less_iff_le, 
paulson@5416
   357
			  add_diff_inverse, card_mono, finite_subset])));
paulson@3352
   358
qed "card_Diff_subset";
nipkow@1531
   359
paulson@5143
   360
Goal "[| finite A; x: A |] ==> Suc(card(A-{x})) = card A";
paulson@1618
   361
by (res_inst_tac [("t", "A")] (insert_Diff RS subst) 1);
paulson@1618
   362
by (assume_tac 1);
paulson@3352
   363
by (Asm_simp_tac 1);
paulson@1618
   364
qed "card_Suc_Diff";
paulson@1618
   365
paulson@5143
   366
Goal "[| finite A; x: A |] ==> card(A-{x}) < card A";
paulson@2031
   367
by (rtac Suc_less_SucD 1);
wenzelm@4089
   368
by (asm_simp_tac (simpset() addsimps [card_Suc_Diff]) 1);
paulson@1618
   369
qed "card_Diff";
paulson@1618
   370
paulson@5143
   371
Goal "finite A ==> card(A-{x}) <= card A";
paulson@4768
   372
by (case_tac "x: A" 1);
paulson@4768
   373
by (ALLGOALS (asm_simp_tac (simpset() addsimps [card_Diff, less_imp_le])));
paulson@4768
   374
qed "card_Diff_le";
paulson@4768
   375
paulson@3389
   376
paulson@3389
   377
(*** Cardinality of the Powerset ***)
paulson@3389
   378
paulson@5143
   379
Goal "finite A ==> card(insert x A) = Suc(card(A-{x}))";
paulson@1553
   380
by (case_tac "x:A" 1);
paulson@4768
   381
by (ALLGOALS 
paulson@4768
   382
    (asm_simp_tac (simpset() addsimps [card_Suc_Diff, insert_absorb])));
nipkow@1531
   383
qed "card_insert";
nipkow@1531
   384
paulson@5143
   385
Goal "finite(A) ==> inj_on f A --> card (f `` A) = card A";
paulson@3340
   386
by (etac finite_induct 1);
paulson@3340
   387
by (ALLGOALS Asm_simp_tac);
paulson@3724
   388
by Safe_tac;
nipkow@4830
   389
by (rewtac inj_on_def);
paulson@3340
   390
by (Blast_tac 1);
paulson@3340
   391
by (stac card_insert_disjoint 1);
paulson@3340
   392
by (etac finite_imageI 1);
paulson@3340
   393
by (Blast_tac 1);
paulson@3340
   394
by (Blast_tac 1);
paulson@3340
   395
qed_spec_mp "card_image";
paulson@3340
   396
paulson@5143
   397
Goal "finite A ==> card (Pow A) = 2 ^ card A";
paulson@3389
   398
by (etac finite_induct 1);
wenzelm@4089
   399
by (ALLGOALS (asm_simp_tac (simpset() addsimps [Pow_insert])));
paulson@3389
   400
by (stac card_Un_disjoint 1);
wenzelm@4089
   401
by (EVERY (map (blast_tac (claset() addIs [finite_imageI])) [3,2,1]));
nipkow@4830
   402
by (subgoal_tac "inj_on (insert x) (Pow F)" 1);
wenzelm@4089
   403
by (asm_simp_tac (simpset() addsimps [card_image, Pow_insert]) 1);
nipkow@4830
   404
by (rewtac inj_on_def);
wenzelm@4089
   405
by (blast_tac (claset() addSEs [equalityE]) 1);
paulson@3389
   406
qed "card_Pow";
paulson@3389
   407
Addsimps [card_Pow];
paulson@3340
   408
paulson@3389
   409
paulson@3389
   410
(*Proper subsets*)
paulson@5148
   411
Goalw [psubset_def] "finite B ==> !A. A < B --> card(A) < card(B)";
nipkow@3222
   412
by (etac finite_induct 1);
nipkow@3222
   413
by (Simp_tac 1);
paulson@3708
   414
by (Clarify_tac 1);
nipkow@3222
   415
by (case_tac "x:A" 1);
nipkow@3222
   416
(*1*)
nipkow@3413
   417
by (dres_inst_tac [("A","A")]mk_disjoint_insert 1);
paulson@4775
   418
by (Clarify_tac 1);
paulson@4775
   419
by (rotate_tac ~3 1);
paulson@4775
   420
by (asm_full_simp_tac (simpset() addsimps [finite_subset]) 1);
paulson@3708
   421
by (Blast_tac 1);
nipkow@3222
   422
(*2*)
paulson@3708
   423
by (eres_inst_tac [("P","?a<?b")] notE 1);
paulson@4775
   424
by (asm_full_simp_tac (simpset() addsimps [subset_insert_iff]) 1);
nipkow@3222
   425
by (case_tac "A=F" 1);
paulson@3708
   426
by (ALLGOALS Asm_simp_tac);
nipkow@3222
   427
qed_spec_mp "psubset_card" ;
paulson@3368
   428
paulson@3368
   429
wenzelm@3430
   430
(*Relates to equivalence classes.   Based on a theorem of F. Kammueller's.
paulson@3368
   431
  The "finite C" premise is redundant*)
paulson@5143
   432
Goal "finite C ==> finite (Union C) --> \
paulson@3368
   433
\          (! c : C. k dvd card c) -->  \
paulson@3368
   434
\          (! c1: C. ! c2: C. c1 ~= c2 --> c1 Int c2 = {}) \
paulson@3368
   435
\          --> k dvd card(Union C)";
paulson@3368
   436
by (etac finite_induct 1);
paulson@3368
   437
by (ALLGOALS Asm_simp_tac);
paulson@3708
   438
by (Clarify_tac 1);
paulson@3368
   439
by (stac card_Un_disjoint 1);
paulson@3368
   440
by (ALLGOALS
wenzelm@4089
   441
    (asm_full_simp_tac (simpset()
paulson@3368
   442
			 addsimps [dvd_add, disjoint_eq_subset_Compl])));
paulson@3368
   443
by (thin_tac "!c:F. ?PP(c)" 1);
paulson@3368
   444
by (thin_tac "!c:F. ?PP(c) & ?QQ(c)" 1);
paulson@3708
   445
by (Clarify_tac 1);
paulson@3368
   446
by (ball_tac 1);
paulson@3368
   447
by (Blast_tac 1);
paulson@3368
   448
qed_spec_mp "dvd_partition";
paulson@3368
   449
nipkow@5616
   450
nipkow@5616
   451
(*** foldSet ***)
nipkow@5616
   452
nipkow@5616
   453
val empty_foldSetE = foldSet.mk_cases [] "({}, x) : foldSet f e";
nipkow@5616
   454
nipkow@5616
   455
AddSEs [empty_foldSetE];
nipkow@5616
   456
AddIs foldSet.intrs;
nipkow@5616
   457
nipkow@5616
   458
Goal "[| (A-{x},y) : foldSet f e;  x: A |] ==> (A, f x y) : foldSet f e";
nipkow@5616
   459
by (etac (insert_Diff RS subst) 1 THEN resolve_tac foldSet.intrs 1);
nipkow@5616
   460
by Auto_tac;
nipkow@5616
   461
qed "Diff_foldSet";
nipkow@5616
   462
nipkow@5616
   463
Goal "(A, x) : foldSet f e ==> finite(A)";
nipkow@5616
   464
by (eresolve_tac [foldSet.induct] 1);
nipkow@5616
   465
by Auto_tac;
nipkow@5616
   466
qed "foldSet_imp_finite";
nipkow@5616
   467
nipkow@5616
   468
Addsimps [foldSet_imp_finite];
nipkow@5616
   469
nipkow@5616
   470
nipkow@5616
   471
Goal "finite(A) ==> EX x. (A, x) : foldSet f e";
nipkow@5616
   472
by (etac finite_induct 1);
nipkow@5616
   473
by Auto_tac;
nipkow@5616
   474
qed "finite_imp_foldSet";
nipkow@5616
   475
nipkow@5616
   476
nipkow@5616
   477
Open_locale "LC"; 
nipkow@5616
   478
nipkow@5616
   479
(*Strip meta-quantifiers: perhaps the locale should do this?*)
nipkow@5616
   480
val f_lcomm = forall_elim_vars 0 (thm "lcomm");
nipkow@5616
   481
nipkow@5616
   482
nipkow@5616
   483
Goal "ALL A x. card(A) < n --> (A, x) : foldSet f e --> \
nipkow@5616
   484
\            (ALL y. (A, y) : foldSet f e --> y=x)";
nipkow@5616
   485
by (induct_tac "n" 1);
nipkow@5616
   486
by (auto_tac (claset(), simpset() addsimps [less_Suc_eq]));
nipkow@5616
   487
by (etac foldSet.elim 1);
nipkow@5616
   488
by (Blast_tac 1);
nipkow@5616
   489
by (etac foldSet.elim 1);
nipkow@5616
   490
by (Blast_tac 1);
nipkow@5616
   491
by (Clarify_tac 1);
nipkow@5616
   492
(*force simplification of "card A < card (insert ...)"*)
nipkow@5616
   493
by (etac rev_mp 1);
nipkow@5616
   494
by (asm_simp_tac (simpset() addsimps [less_Suc_eq_le]) 1);
nipkow@5616
   495
by (rtac impI 1);
nipkow@5616
   496
(** LEVEL 10 **)
nipkow@5616
   497
by (rename_tac "Aa xa ya Ab xb yb" 1);
nipkow@5616
   498
 by (case_tac "xa=xb" 1);
nipkow@5616
   499
 by (subgoal_tac "Aa = Ab" 1);
nipkow@5616
   500
 by (blast_tac (claset() addEs [equalityE]) 2);
nipkow@5616
   501
 by (Blast_tac 1);
nipkow@5616
   502
(*case xa ~= xb*)
nipkow@5616
   503
by (subgoal_tac "Aa-{xb} = Ab-{xa} & xb : Aa & xa : Ab" 1);
nipkow@5616
   504
 by (blast_tac (claset() addEs [equalityE]) 2);
nipkow@5616
   505
by (Clarify_tac 1);
nipkow@5616
   506
by (subgoal_tac "Aa = insert xb Ab - {xa}" 1);
nipkow@5616
   507
 by (blast_tac (claset() addEs [equalityE]) 2);
nipkow@5616
   508
(** LEVEL 20 **)
nipkow@5616
   509
by (subgoal_tac "card Aa <= card Ab" 1);
nipkow@5616
   510
 by (rtac (Suc_le_mono RS subst) 2);
nipkow@5616
   511
 by (asm_simp_tac (simpset() addsimps [card_Suc_Diff]) 2);
nipkow@5616
   512
by (res_inst_tac [("A1", "Aa-{xb}"), ("f1","f")] 
nipkow@5616
   513
    (finite_imp_foldSet RS exE) 1);
nipkow@5616
   514
by (blast_tac (claset() addIs [foldSet_imp_finite, finite_Diff]) 1);
nipkow@5616
   515
by (forward_tac [Diff_foldSet] 1 THEN assume_tac 1);
nipkow@5616
   516
by (subgoal_tac "ya = f xb x" 1);
nipkow@5616
   517
 by (Blast_tac 2);
nipkow@5616
   518
by (subgoal_tac "(Ab - {xa}, x) : foldSet f e" 1);
nipkow@5616
   519
 by (Asm_full_simp_tac 2);
nipkow@5616
   520
by (subgoal_tac "yb = f xa x" 1);
nipkow@5616
   521
 by (blast_tac (claset() addDs [Diff_foldSet]) 2);
nipkow@5616
   522
by (asm_simp_tac (simpset() addsimps [f_lcomm]) 1);
nipkow@5616
   523
val lemma = result();
nipkow@5616
   524
nipkow@5616
   525
nipkow@5616
   526
Goal "[| (A, x) : foldSet f e;  (A, y) : foldSet f e |] ==> y=x";
nipkow@5616
   527
by (blast_tac (claset() addIs [normalize_thm [RSspec, RSmp] lemma]) 1);
nipkow@5616
   528
qed "foldSet_determ";
nipkow@5616
   529
nipkow@5616
   530
Goalw [fold_def] "(A,y) : foldSet f e ==> fold f e A = y";
nipkow@5616
   531
by (blast_tac (claset() addIs [foldSet_determ]) 1);
nipkow@5616
   532
qed "fold_equality";
nipkow@5616
   533
nipkow@5616
   534
Goalw [fold_def] "fold f e {} = e";
nipkow@5616
   535
by (Blast_tac 1);
nipkow@5616
   536
qed "fold_empty";
nipkow@5616
   537
Addsimps [fold_empty];
nipkow@5616
   538
nipkow@5616
   539
Goal "x ~: A ==> \
nipkow@5616
   540
\     ((insert x A, v) : foldSet f e) =  \
nipkow@5616
   541
\     (EX y. (A, y) : foldSet f e & v = f x y)";
nipkow@5616
   542
by Auto_tac;
nipkow@5616
   543
by (res_inst_tac [("A1", "A"), ("f1","f")] (finite_imp_foldSet RS exE) 1);
nipkow@5616
   544
by (force_tac (claset() addDs [foldSet_imp_finite], simpset()) 1);
nipkow@5616
   545
by (blast_tac (claset() addIs [foldSet_determ]) 1);
nipkow@5616
   546
val lemma = result();
nipkow@5616
   547
nipkow@5616
   548
nipkow@5616
   549
Goalw [fold_def]
nipkow@5616
   550
     "[| finite A; x ~: A |] ==> fold f e (insert x A) = f x (fold f e A)";
nipkow@5616
   551
by (asm_simp_tac (simpset() addsimps [lemma]) 1);
nipkow@5616
   552
by (rtac select_equality 1);
nipkow@5616
   553
by (auto_tac (claset() addIs [finite_imp_foldSet],
nipkow@5616
   554
	      simpset() addcongs [conj_cong]
nipkow@5616
   555
		        addsimps [symmetric fold_def,
nipkow@5616
   556
				  fold_equality]));
nipkow@5616
   557
qed "fold_insert";
nipkow@5616
   558
nipkow@5616
   559
Close_locale();
nipkow@5616
   560
nipkow@5616
   561
Open_locale "ACe"; 
nipkow@5616
   562
nipkow@5616
   563
(*Strip meta-quantifiers: perhaps the locale should do this?*)
nipkow@5616
   564
val f_ident   = forall_elim_vars 0 (thm "ident");
nipkow@5616
   565
val f_commute = forall_elim_vars 0 (thm "commute");
nipkow@5616
   566
val f_assoc   = forall_elim_vars 0 (thm "assoc");
nipkow@5616
   567
nipkow@5616
   568
nipkow@5616
   569
Goal "f x (f y z) = f y (f x z)";
nipkow@5616
   570
by (rtac (f_commute RS trans) 1);
nipkow@5616
   571
by (rtac (f_assoc RS trans) 1);
nipkow@5616
   572
by (rtac (f_commute RS arg_cong) 1);
nipkow@5616
   573
qed "f_left_commute";
nipkow@5616
   574
nipkow@5616
   575
val f_ac = [f_assoc, f_commute, f_left_commute];
nipkow@5616
   576
nipkow@5616
   577
Goal "f e x = x";
nipkow@5616
   578
by (stac f_commute 1);
nipkow@5616
   579
by (rtac f_ident 1);
nipkow@5616
   580
qed "f_left_ident";
nipkow@5616
   581
nipkow@5616
   582
val f_idents = [f_left_ident, f_ident];
nipkow@5616
   583
nipkow@5616
   584
Goal "[| finite A; finite B |] \
nipkow@5616
   585
\     ==> f (fold f e A) (fold f e B) =  \
nipkow@5616
   586
\         f (fold f e (A Un B)) (fold f e (A Int B))";
nipkow@5616
   587
by (etac finite_induct 1);
nipkow@5616
   588
by (simp_tac (simpset() addsimps f_idents) 1);
nipkow@5616
   589
by (asm_simp_tac (simpset() addsimps f_ac @ f_idents @
nipkow@5616
   590
           [export fold_insert,insert_absorb, Int_insert_left]) 1);
nipkow@5616
   591
qed "fold_Un_Int";
nipkow@5616
   592
nipkow@5616
   593
Goal "[| finite A; finite B; A Int B = {} |] \
nipkow@5616
   594
\     ==> fold f e (A Un B) = f (fold f e A) (fold f e B)";
nipkow@5616
   595
by (asm_simp_tac (simpset() addsimps fold_Un_Int::f_idents) 1);
nipkow@5616
   596
qed "fold_Un_disjoint";
nipkow@5616
   597
nipkow@5616
   598
Goal
nipkow@5616
   599
 "[| finite A; finite B |] ==> A Int B = {} --> \
nipkow@5616
   600
\ fold (f o g) e (A Un B) = f (fold (f o g) e A) (fold (f o g) e B)";
nipkow@5616
   601
by (etac finite_induct 1);
nipkow@5616
   602
by (simp_tac (simpset() addsimps f_idents) 1);
nipkow@5616
   603
by (asm_full_simp_tac (simpset() addsimps f_ac @ f_idents @
nipkow@5616
   604
           [export fold_insert,insert_absorb, Int_insert_left]) 1);
nipkow@5616
   605
qed "fold_Un_disjoint2";
nipkow@5616
   606
nipkow@5616
   607
Close_locale();
nipkow@5616
   608
nipkow@5616
   609
Delrules ([empty_foldSetE] @ foldSet.intrs);
nipkow@5616
   610
Delsimps [foldSet_imp_finite];
nipkow@5616
   611
nipkow@5616
   612
(*** setsum ***)
nipkow@5616
   613
nipkow@5616
   614
Goalw [setsum_def] "setsum f {} = 0";
nipkow@5616
   615
by(Simp_tac 1);
nipkow@5616
   616
qed "setsum_empty";
nipkow@5616
   617
Addsimps [setsum_empty];
nipkow@5616
   618
nipkow@5616
   619
Goalw [setsum_def]
nipkow@5616
   620
 "[| finite F; a ~: F |] ==> setsum f (insert a F) = f(a) + setsum f F";
nipkow@5616
   621
by(asm_simp_tac (simpset() addsimps [export fold_insert]) 1);
nipkow@5616
   622
qed "setsum_insert";
nipkow@5616
   623
Addsimps [setsum_insert];
nipkow@5616
   624
nipkow@5616
   625
Goalw [setsum_def]
nipkow@5616
   626
 "[| finite A; finite B; A Int B = {} |] ==> \
nipkow@5616
   627
\ setsum f (A Un B) = setsum f A + setsum f B";
nipkow@5616
   628
by(asm_simp_tac (simpset() addsimps [export fold_Un_disjoint2]) 1);
nipkow@5616
   629
qed_spec_mp "setsum_disj_Un";
nipkow@5616
   630
nipkow@5616
   631
Goal "[| finite F |] ==> \
nipkow@5616
   632
\     setsum f (F-{a}) = (if a:F then setsum f F - f a else setsum f F)";
nipkow@5616
   633
be finite_induct 1;
nipkow@5616
   634
by(auto_tac (claset(), simpset() addsimps [insert_Diff_if]));
nipkow@5616
   635
by(dres_inst_tac [("a","a")] mk_disjoint_insert 1);
nipkow@5616
   636
by(Auto_tac);
nipkow@5616
   637
qed_spec_mp "setsum_diff1";