src/HOL/Finite.ML
author nipkow
Mon Mar 04 14:37:33 1996 +0100 (1996-03-04)
changeset 1531 e5eb247ad13c
parent 1465 5d7a7e439cec
child 1548 afe750876848
permissions -rw-r--r--
Added a constant UNIV == {x.True}
Added many new rewrite rules for sets.
Moved LEAST into Nat.
Added cardinality to 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
clasohm@923
     9
open Finite;
clasohm@923
    10
nipkow@1531
    11
(*** 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)";
clasohm@923
    19
by (fast_tac (set_cs 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);
clasohm@923
    52
by (safe_tac (set_cs 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))";
nipkow@1531
    58
by(fast_tac (set_cs 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)";
nipkow@1531
    64
by(stac insert_is_Un 1);
nipkow@1531
    65
by(Simp_tac 1);
nipkow@1531
    66
by(fast_tac (set_cs 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
clasohm@1264
    76
    (!simpset addsimps [image_eqI RS Fin.insertI, image_insert]) 1);
clasohm@923
    77
qed "Fin_imageI";
clasohm@923
    78
clasohm@923
    79
val major::prems = goal Finite.thy 
clasohm@1465
    80
    "[| c: Fin(A);  b: Fin(A);                                  \
clasohm@1465
    81
\       P(b);                                                   \
clasohm@923
    82
\       !!(x::'a) y. [| x:A; y: Fin(A);  x:y;  P(y) |] ==> P(y-{x}) \
clasohm@923
    83
\    |] ==> c<=b --> P(b-c)";
clasohm@923
    84
by (rtac (major RS Fin_induct) 1);
clasohm@923
    85
by (rtac (Diff_insert RS ssubst) 2);
clasohm@923
    86
by (ALLGOALS (asm_simp_tac
clasohm@1264
    87
                (!simpset addsimps (prems@[Diff_subset RS Fin_subset]))));
nipkow@1531
    88
val lemma = result();
clasohm@923
    89
clasohm@923
    90
val prems = goal Finite.thy 
clasohm@1465
    91
    "[| b: Fin(A);                                              \
clasohm@1465
    92
\       P(b);                                                   \
clasohm@923
    93
\       !!x y. [| x:A; y: Fin(A);  x:y;  P(y) |] ==> P(y-{x}) \
clasohm@923
    94
\    |] ==> P({})";
clasohm@923
    95
by (rtac (Diff_cancel RS subst) 1);
nipkow@1531
    96
by (rtac (lemma RS mp) 1);
clasohm@923
    97
by (REPEAT (ares_tac (subset_refl::prems) 1));
clasohm@923
    98
qed "Fin_empty_induct";
nipkow@1531
    99
nipkow@1531
   100
nipkow@1531
   101
(*** finite ***)
nipkow@1531
   102
nipkow@1531
   103
val major::prems = goalw Finite.thy [finite_def]
nipkow@1531
   104
    "[| finite F;  P({}); \
nipkow@1531
   105
\       !!F x. [| finite F;  x~:F;  P(F) |] ==> P(insert x F) \
nipkow@1531
   106
\    |] ==> P(F)";
nipkow@1531
   107
by (rtac (major RS Fin_induct) 1);
nipkow@1531
   108
by (REPEAT (ares_tac prems 1));
nipkow@1531
   109
qed "finite_induct";
nipkow@1531
   110
nipkow@1531
   111
nipkow@1531
   112
goalw Finite.thy [finite_def] "finite {}";
nipkow@1531
   113
by(Simp_tac 1);
nipkow@1531
   114
qed "finite_emptyI";
nipkow@1531
   115
Addsimps [finite_emptyI];
nipkow@1531
   116
nipkow@1531
   117
goalw Finite.thy [finite_def] "!!A. finite A ==> finite(insert a A)";
nipkow@1531
   118
by(Asm_simp_tac 1);
nipkow@1531
   119
qed "finite_insertI";
nipkow@1531
   120
nipkow@1531
   121
(*The union of two finite sets is finite*)
nipkow@1531
   122
goalw Finite.thy [finite_def]
nipkow@1531
   123
    "!!F. [| finite F;  finite G |] ==> finite(F Un G)";
nipkow@1531
   124
by(Asm_simp_tac 1);
nipkow@1531
   125
qed "finite_UnI";
nipkow@1531
   126
nipkow@1531
   127
goalw Finite.thy [finite_def] "!!A. [| A<=B;  finite B |] ==> finite A";
nipkow@1531
   128
be Fin_subset 1;
nipkow@1531
   129
ba 1;
nipkow@1531
   130
qed "finite_subset";
nipkow@1531
   131
nipkow@1531
   132
goalw Finite.thy [finite_def] "finite(F Un G) = (finite F & finite G)";
nipkow@1531
   133
by(Simp_tac 1);
nipkow@1531
   134
qed "subset_finite";
nipkow@1531
   135
Addsimps[subset_finite];
nipkow@1531
   136
nipkow@1531
   137
goalw Finite.thy [finite_def] "finite(insert a A) = finite(A)";
nipkow@1531
   138
by(Simp_tac 1);
nipkow@1531
   139
qed "insert_finite";
nipkow@1531
   140
Addsimps[insert_finite];
nipkow@1531
   141
nipkow@1531
   142
goal Finite.thy "!!A. finite(A) ==> finite(A-B)";
nipkow@1531
   143
be finite_induct 1;
nipkow@1531
   144
by(Simp_tac 1);
nipkow@1531
   145
by(asm_simp_tac (!simpset addsimps [insert_Diff_if]
nipkow@1531
   146
                          setloop split_tac[expand_if]) 1);
nipkow@1531
   147
qed "finite_Diff";
nipkow@1531
   148
Addsimps [finite_Diff];
nipkow@1531
   149
nipkow@1531
   150
(*The image of a finite set is finite*)
nipkow@1531
   151
goal Finite.thy "!!F. finite F ==> finite(h``F)";
nipkow@1531
   152
be finite_induct 1;
nipkow@1531
   153
by(ALLGOALS Asm_simp_tac);
nipkow@1531
   154
qed "finite_imageI";
nipkow@1531
   155
nipkow@1531
   156
val major::prems = goalw Finite.thy [finite_def]
nipkow@1531
   157
    "[| finite A;                                       \
nipkow@1531
   158
\       P(A);                                           \
nipkow@1531
   159
\       !!a A. [| finite A;  a:A;  P(A) |] ==> P(A-{a}) \
nipkow@1531
   160
\    |] ==> P({})";
nipkow@1531
   161
by (rtac (major RS Fin_empty_induct) 1);
nipkow@1531
   162
by (REPEAT (ares_tac (subset_refl::prems) 1));
nipkow@1531
   163
qed "finite_empty_induct";
nipkow@1531
   164
nipkow@1531
   165
nipkow@1531
   166
(*** Cardinality ***)
nipkow@1531
   167
nipkow@1531
   168
goal Set.thy "{f i |i. P i | i=n} = insert (f n) {f i|i. P i}";
nipkow@1531
   169
by(fast_tac eq_cs 1);
nipkow@1531
   170
val Collect_conv_insert = result();
nipkow@1531
   171
nipkow@1531
   172
goalw Finite.thy [card_def] "card {} = 0";
nipkow@1531
   173
br Least_equality 1;
nipkow@1531
   174
by(ALLGOALS Asm_full_simp_tac);
nipkow@1531
   175
qed "card_empty";
nipkow@1531
   176
Addsimps [card_empty];
nipkow@1531
   177
nipkow@1531
   178
(*Addsimps [Collect_conv_insert];*)
nipkow@1531
   179
nipkow@1531
   180
val [major] = goal Finite.thy
nipkow@1531
   181
  "finite A ==> ? (n::nat) f. A = {f i |i. i<n}";
nipkow@1531
   182
br (major RS finite_induct) 1;
nipkow@1531
   183
 by(res_inst_tac [("x","0")] exI 1);
nipkow@1531
   184
 by(Simp_tac 1);
nipkow@1531
   185
be exE 1;
nipkow@1531
   186
be exE 1;
nipkow@1531
   187
by(hyp_subst_tac 1);
nipkow@1531
   188
by(res_inst_tac [("x","Suc n")] exI 1);
nipkow@1531
   189
by(res_inst_tac [("x","%i. if i<n then f i else x")] exI 1);
nipkow@1531
   190
by(asm_simp_tac (!simpset addsimps [Collect_conv_insert]
nipkow@1531
   191
                          addcongs [Collect_cong1]) 1);
nipkow@1531
   192
qed "finite_has_card";
nipkow@1531
   193
nipkow@1531
   194
goal Finite.thy
nipkow@1531
   195
  "!!A.[| x ~: A; insert x A = {f i|i.i<n} |] ==> \
nipkow@1531
   196
\  ? m::nat. m<n & (? g. A = {g i|i.i<m})";
nipkow@1531
   197
by(res_inst_tac [("n","n")] natE 1);
nipkow@1531
   198
 by(hyp_subst_tac 1);
nipkow@1531
   199
 by(Asm_full_simp_tac 1);
nipkow@1531
   200
by(rename_tac "m" 1);
nipkow@1531
   201
by(hyp_subst_tac 1);
nipkow@1531
   202
by(case_tac "? a. a:A" 1);
nipkow@1531
   203
 by(res_inst_tac [("x","0")] exI 2);
nipkow@1531
   204
 by(Simp_tac 2);
nipkow@1531
   205
 by(fast_tac eq_cs 2);
nipkow@1531
   206
be exE 1;
nipkow@1531
   207
by(Simp_tac 1);
nipkow@1531
   208
br exI 1;
nipkow@1531
   209
br conjI 1;
nipkow@1531
   210
 br disjI2 1;
nipkow@1531
   211
 br refl 1;
nipkow@1531
   212
be equalityE 1;
nipkow@1531
   213
by(asm_full_simp_tac
nipkow@1531
   214
     (!simpset addsimps [subset_insert,Collect_conv_insert]) 1);
nipkow@1531
   215
by(SELECT_GOAL(safe_tac eq_cs)1);
nipkow@1531
   216
  by(Asm_full_simp_tac 1);
nipkow@1531
   217
  by(res_inst_tac [("x","%i. if f i = f m then a else f i")] exI 1);
nipkow@1531
   218
  by(SELECT_GOAL(safe_tac eq_cs)1);
nipkow@1531
   219
   by(subgoal_tac "x ~= f m" 1);
nipkow@1531
   220
    by(fast_tac set_cs 2);
nipkow@1531
   221
   by(subgoal_tac "? k. f k = x & k<m" 1);
nipkow@1531
   222
    by(best_tac set_cs 2);
nipkow@1531
   223
   by(SELECT_GOAL(safe_tac HOL_cs)1);
nipkow@1531
   224
   by(res_inst_tac [("x","k")] exI 1);
nipkow@1531
   225
   by(Asm_simp_tac 1);
nipkow@1531
   226
  by(simp_tac (!simpset setloop (split_tac [expand_if])) 1);
nipkow@1531
   227
  by(best_tac set_cs 1);
nipkow@1531
   228
 bd sym 1;
nipkow@1531
   229
 by(rotate_tac ~1 1);
nipkow@1531
   230
 by(Asm_full_simp_tac 1);
nipkow@1531
   231
 by(res_inst_tac [("x","%i. if f i = f m then a else f i")] exI 1);
nipkow@1531
   232
 by(SELECT_GOAL(safe_tac eq_cs)1);
nipkow@1531
   233
  by(subgoal_tac "x ~= f m" 1);
nipkow@1531
   234
   by(fast_tac set_cs 2);
nipkow@1531
   235
  by(subgoal_tac "? k. f k = x & k<m" 1);
nipkow@1531
   236
   by(best_tac set_cs 2);
nipkow@1531
   237
  by(SELECT_GOAL(safe_tac HOL_cs)1);
nipkow@1531
   238
  by(res_inst_tac [("x","k")] exI 1);
nipkow@1531
   239
  by(Asm_simp_tac 1);
nipkow@1531
   240
 by(simp_tac (!simpset setloop (split_tac [expand_if])) 1);
nipkow@1531
   241
 by(best_tac set_cs 1);
nipkow@1531
   242
by(res_inst_tac [("x","%j. if f j = f i then f m else f j")] exI 1);
nipkow@1531
   243
by(SELECT_GOAL(safe_tac eq_cs)1);
nipkow@1531
   244
 by(subgoal_tac "x ~= f i" 1);
nipkow@1531
   245
  by(fast_tac set_cs 2);
nipkow@1531
   246
 by(case_tac "x = f m" 1);
nipkow@1531
   247
  by(res_inst_tac [("x","i")] exI 1);
nipkow@1531
   248
  by(Asm_simp_tac 1);
nipkow@1531
   249
 by(subgoal_tac "? k. f k = x & k<m" 1);
nipkow@1531
   250
  by(best_tac set_cs 2);
nipkow@1531
   251
 by(SELECT_GOAL(safe_tac HOL_cs)1);
nipkow@1531
   252
 by(res_inst_tac [("x","k")] exI 1);
nipkow@1531
   253
 by(Asm_simp_tac 1);
nipkow@1531
   254
by(simp_tac (!simpset setloop (split_tac [expand_if])) 1);
nipkow@1531
   255
by(best_tac set_cs 1);
nipkow@1531
   256
val lemma = result();
nipkow@1531
   257
nipkow@1531
   258
goal Finite.thy "!!A. [| finite A; x ~: A |] ==> \
nipkow@1531
   259
\ (LEAST n. ? f. insert x A = {f i|i.i<n}) = Suc(LEAST n. ? f. A={f i|i.i<n})";
nipkow@1531
   260
br Least_equality 1;
nipkow@1531
   261
 bd finite_has_card 1;
nipkow@1531
   262
 be exE 1;
nipkow@1531
   263
 by(dres_inst_tac [("P","%n.? f. A={f i|i.i<n}")] LeastI 1);
nipkow@1531
   264
 be exE 1;
nipkow@1531
   265
 by(res_inst_tac
nipkow@1531
   266
   [("x","%i. if i<(LEAST n. ? f. A={f i |i. i < n}) then f i else x")] exI 1);
nipkow@1531
   267
 by(simp_tac
nipkow@1531
   268
    (!simpset addsimps [Collect_conv_insert] addcongs [Collect_cong1]) 1);
nipkow@1531
   269
 be subst 1;
nipkow@1531
   270
 br refl 1;
nipkow@1531
   271
br notI 1;
nipkow@1531
   272
be exE 1;
nipkow@1531
   273
bd lemma 1;
nipkow@1531
   274
 ba 1;
nipkow@1531
   275
be exE 1;
nipkow@1531
   276
be conjE 1;
nipkow@1531
   277
by(dres_inst_tac [("P","%x. ? g. A = {g i |i. i < x}")] Least_le 1);
nipkow@1531
   278
by(dtac le_less_trans 1 THEN atac 1);
nipkow@1531
   279
by(Asm_full_simp_tac 1);
nipkow@1531
   280
be disjE 1;
nipkow@1531
   281
by(etac less_asym 1 THEN atac 1);
nipkow@1531
   282
by(hyp_subst_tac 1);
nipkow@1531
   283
by(Asm_full_simp_tac 1);
nipkow@1531
   284
val lemma = result();
nipkow@1531
   285
nipkow@1531
   286
goalw Finite.thy [card_def]
nipkow@1531
   287
  "!!A. [| finite A; x ~: A |] ==> card(insert x A) = Suc(card A)";
nipkow@1531
   288
be lemma 1;
nipkow@1531
   289
ba 1;
nipkow@1531
   290
qed "card_insert_disjoint";
nipkow@1531
   291
nipkow@1531
   292
val [major] = goal Finite.thy
nipkow@1531
   293
  "finite A ==> card(insert x A) = Suc(card(A-{x}))";
nipkow@1531
   294
by(case_tac "x:A" 1);
nipkow@1531
   295
by(asm_simp_tac (!simpset addsimps [insert_absorb]) 1);
nipkow@1531
   296
bd mk_disjoint_insert 1;
nipkow@1531
   297
be exE 1;
nipkow@1531
   298
by(Asm_simp_tac 1);
nipkow@1531
   299
br card_insert_disjoint 1;
nipkow@1531
   300
br (major RSN (2,finite_subset)) 1;
nipkow@1531
   301
by(fast_tac set_cs 1);
nipkow@1531
   302
by(fast_tac HOL_cs 1);
nipkow@1531
   303
by(asm_simp_tac (!simpset addsimps [major RS card_insert_disjoint]) 1);
nipkow@1531
   304
qed "card_insert";
nipkow@1531
   305
Addsimps [card_insert];
nipkow@1531
   306
nipkow@1531
   307
nipkow@1531
   308
goal Finite.thy  "!!A. finite A ==> !B. B <= A --> card(B) <= card(A)";
nipkow@1531
   309
be finite_induct 1;
nipkow@1531
   310
by(Simp_tac 1);
nipkow@1531
   311
by(strip_tac 1);
nipkow@1531
   312
by(case_tac "x:B" 1);
nipkow@1531
   313
 bd mk_disjoint_insert 1;
nipkow@1531
   314
 by(SELECT_GOAL(safe_tac HOL_cs)1);
nipkow@1531
   315
 by(rotate_tac ~1 1);
nipkow@1531
   316
 by(asm_full_simp_tac (!simpset addsimps [subset_insert_iff,finite_subset]) 1);
nipkow@1531
   317
by(rotate_tac ~1 1);
nipkow@1531
   318
by(asm_full_simp_tac (!simpset addsimps [subset_insert_iff,finite_subset]) 1);
nipkow@1531
   319
qed_spec_mp "card_mono";