src/HOL/Finite.ML
author berghofe
Mon Jun 03 17:10:56 1996 +0200 (1996-06-03)
changeset 1786 8a31d85d27b8
parent 1782 ab45b881fa62
child 2031 03a843f0f447
permissions -rw-r--r--
best_tac, deepen_tac and safe_tac now also use default claset.
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)";
berghofe@1760
    19
by (fast_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))";
berghofe@1760
    58
by (fast_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);
berghofe@1760
    66
by (fast_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]
oheimb@1660
    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);
clasohm@923
    86
by (rtac (Diff_insert RS ssubst) 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);
nipkow@1531
   135
qed "subset_finite";
nipkow@1531
   136
Addsimps[subset_finite];
nipkow@1531
   137
nipkow@1531
   138
goalw Finite.thy [finite_def] "finite(insert a A) = finite(A)";
paulson@1553
   139
by (Simp_tac 1);
nipkow@1531
   140
qed "insert_finite";
nipkow@1531
   141
Addsimps[insert_finite];
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
nipkow@1531
   147
(*The image of a finite set is finite*)
nipkow@1531
   148
goal Finite.thy "!!F. finite F ==> finite(h``F)";
paulson@1553
   149
by (etac finite_induct 1);
paulson@1553
   150
by (ALLGOALS Asm_simp_tac);
nipkow@1531
   151
qed "finite_imageI";
nipkow@1531
   152
nipkow@1531
   153
val major::prems = goalw Finite.thy [finite_def]
nipkow@1531
   154
    "[| finite A;                                       \
nipkow@1531
   155
\       P(A);                                           \
nipkow@1531
   156
\       !!a A. [| finite A;  a:A;  P(A) |] ==> P(A-{a}) \
nipkow@1531
   157
\    |] ==> P({})";
nipkow@1531
   158
by (rtac (major RS Fin_empty_induct) 1);
nipkow@1531
   159
by (REPEAT (ares_tac (subset_refl::prems) 1));
nipkow@1531
   160
qed "finite_empty_induct";
nipkow@1531
   161
nipkow@1531
   162
nipkow@1548
   163
section "Finite cardinality -- 'card'";
nipkow@1531
   164
nipkow@1531
   165
goal Set.thy "{f i |i. P i | i=n} = insert (f n) {f i|i. P i}";
berghofe@1760
   166
by (Fast_tac 1);
nipkow@1531
   167
val Collect_conv_insert = result();
nipkow@1531
   168
nipkow@1531
   169
goalw Finite.thy [card_def] "card {} = 0";
paulson@1553
   170
by (rtac Least_equality 1);
paulson@1553
   171
by (ALLGOALS Asm_full_simp_tac);
nipkow@1531
   172
qed "card_empty";
nipkow@1531
   173
Addsimps [card_empty];
nipkow@1531
   174
nipkow@1531
   175
val [major] = goal Finite.thy
nipkow@1531
   176
  "finite A ==> ? (n::nat) f. A = {f i |i. i<n}";
paulson@1553
   177
by (rtac (major RS finite_induct) 1);
paulson@1553
   178
 by (res_inst_tac [("x","0")] exI 1);
paulson@1553
   179
 by (Simp_tac 1);
paulson@1553
   180
by (etac exE 1);
paulson@1553
   181
by (etac exE 1);
paulson@1553
   182
by (hyp_subst_tac 1);
paulson@1553
   183
by (res_inst_tac [("x","Suc n")] exI 1);
paulson@1553
   184
by (res_inst_tac [("x","%i. if i<n then f i else x")] exI 1);
oheimb@1660
   185
by (asm_simp_tac (!simpset addsimps [Collect_conv_insert, less_Suc_eq]
nipkow@1548
   186
                          addcongs [rev_conj_cong]) 1);
nipkow@1531
   187
qed "finite_has_card";
nipkow@1531
   188
nipkow@1531
   189
goal Finite.thy
nipkow@1531
   190
  "!!A.[| x ~: A; insert x A = {f i|i.i<n} |] ==> \
nipkow@1531
   191
\  ? m::nat. m<n & (? g. A = {g i|i.i<m})";
paulson@1553
   192
by (res_inst_tac [("n","n")] natE 1);
paulson@1553
   193
 by (hyp_subst_tac 1);
paulson@1553
   194
 by (Asm_full_simp_tac 1);
paulson@1553
   195
by (rename_tac "m" 1);
paulson@1553
   196
by (hyp_subst_tac 1);
paulson@1553
   197
by (case_tac "? a. a:A" 1);
paulson@1553
   198
 by (res_inst_tac [("x","0")] exI 2);
paulson@1553
   199
 by (Simp_tac 2);
berghofe@1760
   200
 by (Fast_tac 2);
paulson@1553
   201
by (etac exE 1);
oheimb@1660
   202
by (simp_tac (!simpset addsimps [less_Suc_eq]) 1);
paulson@1553
   203
by (rtac exI 1);
paulson@1782
   204
by (rtac (refl RS disjI2 RS conjI) 1);
paulson@1553
   205
by (etac equalityE 1);
paulson@1553
   206
by (asm_full_simp_tac
oheimb@1660
   207
     (!simpset addsimps [subset_insert,Collect_conv_insert, less_Suc_eq]) 1);
berghofe@1786
   208
by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   209
  by (Asm_full_simp_tac 1);
paulson@1553
   210
  by (res_inst_tac [("x","%i. if f i = f m then a else f i")] exI 1);
berghofe@1786
   211
  by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   212
   by (subgoal_tac "x ~= f m" 1);
berghofe@1760
   213
    by (Fast_tac 2);
paulson@1553
   214
   by (subgoal_tac "? k. f k = x & k<m" 1);
berghofe@1786
   215
    by (best_tac (!claset) 2);
berghofe@1786
   216
   by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   217
   by (res_inst_tac [("x","k")] exI 1);
paulson@1553
   218
   by (Asm_simp_tac 1);
paulson@1553
   219
  by (simp_tac (!simpset setloop (split_tac [expand_if])) 1);
berghofe@1786
   220
  by (best_tac (!claset) 1);
nipkow@1531
   221
 bd sym 1;
paulson@1553
   222
 by (rotate_tac ~1 1);
paulson@1553
   223
 by (Asm_full_simp_tac 1);
paulson@1553
   224
 by (res_inst_tac [("x","%i. if f i = f m then a else f i")] exI 1);
berghofe@1786
   225
 by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   226
  by (subgoal_tac "x ~= f m" 1);
berghofe@1760
   227
   by (Fast_tac 2);
paulson@1553
   228
  by (subgoal_tac "? k. f k = x & k<m" 1);
berghofe@1786
   229
   by (best_tac (!claset) 2);
berghofe@1786
   230
  by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   231
  by (res_inst_tac [("x","k")] exI 1);
paulson@1553
   232
  by (Asm_simp_tac 1);
paulson@1553
   233
 by (simp_tac (!simpset setloop (split_tac [expand_if])) 1);
berghofe@1786
   234
 by (best_tac (!claset) 1);
paulson@1553
   235
by (res_inst_tac [("x","%j. if f j = f i then f m else f j")] exI 1);
berghofe@1786
   236
by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   237
 by (subgoal_tac "x ~= f i" 1);
berghofe@1760
   238
  by (Fast_tac 2);
paulson@1553
   239
 by (case_tac "x = f m" 1);
paulson@1553
   240
  by (res_inst_tac [("x","i")] exI 1);
paulson@1553
   241
  by (Asm_simp_tac 1);
paulson@1553
   242
 by (subgoal_tac "? k. f k = x & k<m" 1);
berghofe@1786
   243
  by (best_tac (!claset) 2);
berghofe@1786
   244
 by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   245
 by (res_inst_tac [("x","k")] exI 1);
paulson@1553
   246
 by (Asm_simp_tac 1);
paulson@1553
   247
by (simp_tac (!simpset setloop (split_tac [expand_if])) 1);
berghofe@1786
   248
by (best_tac (!claset) 1);
nipkow@1531
   249
val lemma = result();
nipkow@1531
   250
nipkow@1531
   251
goal Finite.thy "!!A. [| finite A; x ~: A |] ==> \
nipkow@1531
   252
\ (LEAST n. ? f. insert x A = {f i|i.i<n}) = Suc(LEAST n. ? f. A={f i|i.i<n})";
paulson@1553
   253
by (rtac Least_equality 1);
nipkow@1531
   254
 bd finite_has_card 1;
nipkow@1531
   255
 be exE 1;
paulson@1553
   256
 by (dres_inst_tac [("P","%n.? f. A={f i|i.i<n}")] LeastI 1);
nipkow@1531
   257
 be exE 1;
paulson@1553
   258
 by (res_inst_tac
nipkow@1531
   259
   [("x","%i. if i<(LEAST n. ? f. A={f i |i. i < n}) then f i else x")] exI 1);
paulson@1553
   260
 by (simp_tac
oheimb@1660
   261
    (!simpset addsimps [Collect_conv_insert, less_Suc_eq] 
oheimb@1660
   262
	      addcongs [rev_conj_cong]) 1);
nipkow@1531
   263
 be subst 1;
nipkow@1531
   264
 br refl 1;
paulson@1553
   265
by (rtac notI 1);
paulson@1553
   266
by (etac exE 1);
paulson@1553
   267
by (dtac lemma 1);
nipkow@1531
   268
 ba 1;
paulson@1553
   269
by (etac exE 1);
paulson@1553
   270
by (etac conjE 1);
paulson@1553
   271
by (dres_inst_tac [("P","%x. ? g. A = {g i |i. i < x}")] Least_le 1);
paulson@1553
   272
by (dtac le_less_trans 1 THEN atac 1);
oheimb@1660
   273
by (asm_full_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
paulson@1553
   274
by (etac disjE 1);
paulson@1553
   275
by (etac less_asym 1 THEN atac 1);
paulson@1553
   276
by (hyp_subst_tac 1);
paulson@1553
   277
by (Asm_full_simp_tac 1);
nipkow@1531
   278
val lemma = result();
nipkow@1531
   279
nipkow@1531
   280
goalw Finite.thy [card_def]
nipkow@1531
   281
  "!!A. [| finite A; x ~: A |] ==> card(insert x A) = Suc(card A)";
paulson@1553
   282
by (etac lemma 1);
paulson@1553
   283
by (assume_tac 1);
nipkow@1531
   284
qed "card_insert_disjoint";
nipkow@1531
   285
paulson@1618
   286
goal Finite.thy "!!A. [| finite A; x: A |] ==> Suc(card(A-{x})) = card A";
paulson@1618
   287
by (res_inst_tac [("t", "A")] (insert_Diff RS subst) 1);
paulson@1618
   288
by (assume_tac 1);
paulson@1618
   289
by (asm_simp_tac (!simpset addsimps [card_insert_disjoint]) 1);
paulson@1618
   290
qed "card_Suc_Diff";
paulson@1618
   291
paulson@1618
   292
goal Finite.thy "!!A. [| finite A; x: A |] ==> card(A-{x}) < card A";
paulson@1618
   293
by (resolve_tac [Suc_less_SucD] 1);
paulson@1618
   294
by (asm_simp_tac (!simpset addsimps [card_Suc_Diff]) 1);
paulson@1618
   295
qed "card_Diff";
paulson@1618
   296
nipkow@1531
   297
val [major] = goal Finite.thy
nipkow@1531
   298
  "finite A ==> card(insert x A) = Suc(card(A-{x}))";
paulson@1553
   299
by (case_tac "x:A" 1);
paulson@1553
   300
by (asm_simp_tac (!simpset addsimps [insert_absorb]) 1);
paulson@1553
   301
by (dtac mk_disjoint_insert 1);
paulson@1553
   302
by (etac exE 1);
paulson@1553
   303
by (Asm_simp_tac 1);
paulson@1553
   304
by (rtac card_insert_disjoint 1);
paulson@1553
   305
by (rtac (major RSN (2,finite_subset)) 1);
berghofe@1760
   306
by (Fast_tac 1);
berghofe@1760
   307
by (Fast_tac 1);
paulson@1553
   308
by (asm_simp_tac (!simpset addsimps [major RS card_insert_disjoint]) 1);
nipkow@1531
   309
qed "card_insert";
nipkow@1531
   310
Addsimps [card_insert];
nipkow@1531
   311
nipkow@1531
   312
nipkow@1531
   313
goal Finite.thy  "!!A. finite A ==> !B. B <= A --> card(B) <= card(A)";
paulson@1553
   314
by (etac finite_induct 1);
paulson@1553
   315
by (Simp_tac 1);
paulson@1553
   316
by (strip_tac 1);
paulson@1553
   317
by (case_tac "x:B" 1);
paulson@1553
   318
 by (dtac mk_disjoint_insert 1);
berghofe@1786
   319
 by (SELECT_GOAL(safe_tac (!claset))1);
paulson@1553
   320
 by (rotate_tac ~1 1);
paulson@1553
   321
 by (asm_full_simp_tac (!simpset addsimps [subset_insert_iff,finite_subset]) 1);
paulson@1553
   322
by (rotate_tac ~1 1);
paulson@1553
   323
by (asm_full_simp_tac (!simpset addsimps [subset_insert_iff,finite_subset]) 1);
nipkow@1531
   324
qed_spec_mp "card_mono";