src/HOL/Finite_Set.thy
author nipkow
Mon Aug 16 14:22:27 2004 +0200 (2004-08-16)
changeset 15131 c69542757a4d
parent 15124 1d9b4fcd222d
child 15140 322485b816ac
permissions -rw-r--r--
New theory header syntax.
wenzelm@12396
     1
(*  Title:      HOL/Finite_Set.thy
wenzelm@12396
     2
    ID:         $Id$
wenzelm@12396
     3
    Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
paulson@14430
     4
                Additions by Jeremy Avigad in Feb 2004
wenzelm@12396
     5
*)
wenzelm@12396
     6
wenzelm@12396
     7
header {* Finite sets *}
wenzelm@12396
     8
nipkow@15131
     9
theory Finite_Set
nipkow@15131
    10
import Divides Power Inductive
nipkow@15131
    11
begin
wenzelm@12396
    12
wenzelm@12396
    13
subsection {* Collection of finite sets *}
wenzelm@12396
    14
wenzelm@12396
    15
consts Finites :: "'a set set"
nipkow@13737
    16
syntax
nipkow@13737
    17
  finite :: "'a set => bool"
nipkow@13737
    18
translations
nipkow@13737
    19
  "finite A" == "A : Finites"
wenzelm@12396
    20
wenzelm@12396
    21
inductive Finites
wenzelm@12396
    22
  intros
wenzelm@12396
    23
    emptyI [simp, intro!]: "{} : Finites"
wenzelm@12396
    24
    insertI [simp, intro!]: "A : Finites ==> insert a A : Finites"
wenzelm@12396
    25
wenzelm@12396
    26
axclass finite \<subseteq> type
wenzelm@12396
    27
  finite: "finite UNIV"
wenzelm@12396
    28
nipkow@13737
    29
lemma ex_new_if_finite: -- "does not depend on def of finite at all"
wenzelm@14661
    30
  assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
wenzelm@14661
    31
  shows "\<exists>a::'a. a \<notin> A"
wenzelm@14661
    32
proof -
wenzelm@14661
    33
  from prems have "A \<noteq> UNIV" by blast
wenzelm@14661
    34
  thus ?thesis by blast
wenzelm@14661
    35
qed
wenzelm@12396
    36
wenzelm@12396
    37
lemma finite_induct [case_names empty insert, induct set: Finites]:
wenzelm@12396
    38
  "finite F ==>
wenzelm@12396
    39
    P {} ==> (!!F x. finite F ==> x \<notin> F ==> P F ==> P (insert x F)) ==> P F"
wenzelm@12396
    40
  -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
wenzelm@12396
    41
proof -
wenzelm@13421
    42
  assume "P {}" and
wenzelm@13421
    43
    insert: "!!F x. finite F ==> x \<notin> F ==> P F ==> P (insert x F)"
wenzelm@12396
    44
  assume "finite F"
wenzelm@12396
    45
  thus "P F"
wenzelm@12396
    46
  proof induct
wenzelm@12396
    47
    show "P {}" .
wenzelm@12396
    48
    fix F x assume F: "finite F" and P: "P F"
wenzelm@12396
    49
    show "P (insert x F)"
wenzelm@12396
    50
    proof cases
wenzelm@12396
    51
      assume "x \<in> F"
wenzelm@12396
    52
      hence "insert x F = F" by (rule insert_absorb)
wenzelm@12396
    53
      with P show ?thesis by (simp only:)
wenzelm@12396
    54
    next
wenzelm@12396
    55
      assume "x \<notin> F"
wenzelm@12396
    56
      from F this P show ?thesis by (rule insert)
wenzelm@12396
    57
    qed
wenzelm@12396
    58
  qed
wenzelm@12396
    59
qed
wenzelm@12396
    60
wenzelm@12396
    61
lemma finite_subset_induct [consumes 2, case_names empty insert]:
wenzelm@12396
    62
  "finite F ==> F \<subseteq> A ==>
wenzelm@12396
    63
    P {} ==> (!!F a. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)) ==>
wenzelm@12396
    64
    P F"
wenzelm@12396
    65
proof -
wenzelm@13421
    66
  assume "P {}" and insert:
wenzelm@13421
    67
    "!!F a. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)"
wenzelm@12396
    68
  assume "finite F"
wenzelm@12396
    69
  thus "F \<subseteq> A ==> P F"
wenzelm@12396
    70
  proof induct
wenzelm@12396
    71
    show "P {}" .
wenzelm@12396
    72
    fix F x assume "finite F" and "x \<notin> F"
wenzelm@12396
    73
      and P: "F \<subseteq> A ==> P F" and i: "insert x F \<subseteq> A"
wenzelm@12396
    74
    show "P (insert x F)"
wenzelm@12396
    75
    proof (rule insert)
wenzelm@12396
    76
      from i show "x \<in> A" by blast
wenzelm@12396
    77
      from i have "F \<subseteq> A" by blast
wenzelm@12396
    78
      with P show "P F" .
wenzelm@12396
    79
    qed
wenzelm@12396
    80
  qed
wenzelm@12396
    81
qed
wenzelm@12396
    82
wenzelm@12396
    83
lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
wenzelm@12396
    84
  -- {* The union of two finite sets is finite. *}
wenzelm@12396
    85
  by (induct set: Finites) simp_all
wenzelm@12396
    86
wenzelm@12396
    87
lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
wenzelm@12396
    88
  -- {* Every subset of a finite set is finite. *}
wenzelm@12396
    89
proof -
wenzelm@12396
    90
  assume "finite B"
wenzelm@12396
    91
  thus "!!A. A \<subseteq> B ==> finite A"
wenzelm@12396
    92
  proof induct
wenzelm@12396
    93
    case empty
wenzelm@12396
    94
    thus ?case by simp
wenzelm@12396
    95
  next
wenzelm@12396
    96
    case (insert F x A)
wenzelm@12396
    97
    have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" .
wenzelm@12396
    98
    show "finite A"
wenzelm@12396
    99
    proof cases
wenzelm@12396
   100
      assume x: "x \<in> A"
wenzelm@12396
   101
      with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   102
      with r have "finite (A - {x})" .
wenzelm@12396
   103
      hence "finite (insert x (A - {x}))" ..
wenzelm@12396
   104
      also have "insert x (A - {x}) = A" by (rule insert_Diff)
wenzelm@12396
   105
      finally show ?thesis .
wenzelm@12396
   106
    next
wenzelm@12396
   107
      show "A \<subseteq> F ==> ?thesis" .
wenzelm@12396
   108
      assume "x \<notin> A"
wenzelm@12396
   109
      with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   110
    qed
wenzelm@12396
   111
  qed
wenzelm@12396
   112
qed
wenzelm@12396
   113
wenzelm@12396
   114
lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
wenzelm@12396
   115
  by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
wenzelm@12396
   116
wenzelm@12396
   117
lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
wenzelm@12396
   118
  -- {* The converse obviously fails. *}
wenzelm@12396
   119
  by (blast intro: finite_subset)
wenzelm@12396
   120
wenzelm@12396
   121
lemma finite_insert [simp]: "finite (insert a A) = finite A"
wenzelm@12396
   122
  apply (subst insert_is_Un)
paulson@14208
   123
  apply (simp only: finite_Un, blast)
wenzelm@12396
   124
  done
wenzelm@12396
   125
wenzelm@12396
   126
lemma finite_empty_induct:
wenzelm@12396
   127
  "finite A ==>
wenzelm@12396
   128
  P A ==> (!!a A. finite A ==> a:A ==> P A ==> P (A - {a})) ==> P {}"
wenzelm@12396
   129
proof -
wenzelm@12396
   130
  assume "finite A"
wenzelm@12396
   131
    and "P A" and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
wenzelm@12396
   132
  have "P (A - A)"
wenzelm@12396
   133
  proof -
wenzelm@12396
   134
    fix c b :: "'a set"
wenzelm@12396
   135
    presume c: "finite c" and b: "finite b"
wenzelm@12396
   136
      and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
wenzelm@12396
   137
    from c show "c \<subseteq> b ==> P (b - c)"
wenzelm@12396
   138
    proof induct
wenzelm@12396
   139
      case empty
wenzelm@12396
   140
      from P1 show ?case by simp
wenzelm@12396
   141
    next
wenzelm@12396
   142
      case (insert F x)
wenzelm@12396
   143
      have "P (b - F - {x})"
wenzelm@12396
   144
      proof (rule P2)
wenzelm@12396
   145
        from _ b show "finite (b - F)" by (rule finite_subset) blast
wenzelm@12396
   146
        from insert show "x \<in> b - F" by simp
wenzelm@12396
   147
        from insert show "P (b - F)" by simp
wenzelm@12396
   148
      qed
wenzelm@12396
   149
      also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
wenzelm@12396
   150
      finally show ?case .
wenzelm@12396
   151
    qed
wenzelm@12396
   152
  next
wenzelm@12396
   153
    show "A \<subseteq> A" ..
wenzelm@12396
   154
  qed
wenzelm@12396
   155
  thus "P {}" by simp
wenzelm@12396
   156
qed
wenzelm@12396
   157
wenzelm@12396
   158
lemma finite_Diff [simp]: "finite B ==> finite (B - Ba)"
wenzelm@12396
   159
  by (rule Diff_subset [THEN finite_subset])
wenzelm@12396
   160
wenzelm@12396
   161
lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
wenzelm@12396
   162
  apply (subst Diff_insert)
wenzelm@12396
   163
  apply (case_tac "a : A - B")
wenzelm@12396
   164
   apply (rule finite_insert [symmetric, THEN trans])
paulson@14208
   165
   apply (subst insert_Diff, simp_all)
wenzelm@12396
   166
  done
wenzelm@12396
   167
wenzelm@12396
   168
paulson@13825
   169
subsubsection {* Image and Inverse Image over Finite Sets *}
paulson@13825
   170
paulson@13825
   171
lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
paulson@13825
   172
  -- {* The image of a finite set is finite. *}
paulson@13825
   173
  by (induct set: Finites) simp_all
paulson@13825
   174
paulson@14430
   175
lemma finite_surj: "finite A ==> B <= f ` A ==> finite B"
paulson@14430
   176
  apply (frule finite_imageI)
paulson@14430
   177
  apply (erule finite_subset, assumption)
paulson@14430
   178
  done
paulson@14430
   179
paulson@13825
   180
lemma finite_range_imageI:
paulson@13825
   181
    "finite (range g) ==> finite (range (%x. f (g x)))"
paulson@14208
   182
  apply (drule finite_imageI, simp)
paulson@13825
   183
  done
paulson@13825
   184
wenzelm@12396
   185
lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
wenzelm@12396
   186
proof -
wenzelm@12396
   187
  have aux: "!!A. finite (A - {}) = finite A" by simp
wenzelm@12396
   188
  fix B :: "'a set"
wenzelm@12396
   189
  assume "finite B"
wenzelm@12396
   190
  thus "!!A. f`A = B ==> inj_on f A ==> finite A"
wenzelm@12396
   191
    apply induct
wenzelm@12396
   192
     apply simp
wenzelm@12396
   193
    apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
wenzelm@12396
   194
     apply clarify
wenzelm@12396
   195
     apply (simp (no_asm_use) add: inj_on_def)
paulson@14208
   196
     apply (blast dest!: aux [THEN iffD1], atomize)
wenzelm@12396
   197
    apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
paulson@14208
   198
    apply (frule subsetD [OF equalityD2 insertI1], clarify)
wenzelm@12396
   199
    apply (rule_tac x = xa in bexI)
wenzelm@12396
   200
     apply (simp_all add: inj_on_image_set_diff)
wenzelm@12396
   201
    done
wenzelm@12396
   202
qed (rule refl)
wenzelm@12396
   203
wenzelm@12396
   204
paulson@13825
   205
lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
paulson@13825
   206
  -- {* The inverse image of a singleton under an injective function
paulson@13825
   207
         is included in a singleton. *}
paulson@14430
   208
  apply (auto simp add: inj_on_def)
paulson@14430
   209
  apply (blast intro: the_equality [symmetric])
paulson@13825
   210
  done
paulson@13825
   211
paulson@13825
   212
lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
paulson@13825
   213
  -- {* The inverse image of a finite set under an injective function
paulson@13825
   214
         is finite. *}
paulson@14430
   215
  apply (induct set: Finites, simp_all)
paulson@14430
   216
  apply (subst vimage_insert)
paulson@14430
   217
  apply (simp add: finite_Un finite_subset [OF inj_vimage_singleton])
paulson@13825
   218
  done
paulson@13825
   219
paulson@13825
   220
wenzelm@12396
   221
subsubsection {* The finite UNION of finite sets *}
wenzelm@12396
   222
wenzelm@12396
   223
lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)"
wenzelm@12396
   224
  by (induct set: Finites) simp_all
wenzelm@12396
   225
wenzelm@12396
   226
text {*
wenzelm@12396
   227
  Strengthen RHS to
paulson@14430
   228
  @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x \<noteq> {}})"}?
wenzelm@12396
   229
wenzelm@12396
   230
  We'd need to prove
paulson@14430
   231
  @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x \<noteq> {}}"}
wenzelm@12396
   232
  by induction. *}
wenzelm@12396
   233
wenzelm@12396
   234
lemma finite_UN [simp]: "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
wenzelm@12396
   235
  by (blast intro: finite_UN_I finite_subset)
wenzelm@12396
   236
wenzelm@12396
   237
wenzelm@12396
   238
subsubsection {* Sigma of finite sets *}
wenzelm@12396
   239
wenzelm@12396
   240
lemma finite_SigmaI [simp]:
wenzelm@12396
   241
    "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
wenzelm@12396
   242
  by (unfold Sigma_def) (blast intro!: finite_UN_I)
wenzelm@12396
   243
wenzelm@12396
   244
lemma finite_Prod_UNIV:
wenzelm@12396
   245
    "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
wenzelm@12396
   246
  apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
wenzelm@12396
   247
   apply (erule ssubst)
paulson@14208
   248
   apply (erule finite_SigmaI, auto)
wenzelm@12396
   249
  done
wenzelm@12396
   250
wenzelm@12396
   251
instance unit :: finite
wenzelm@12396
   252
proof
wenzelm@12396
   253
  have "finite {()}" by simp
wenzelm@12396
   254
  also have "{()} = UNIV" by auto
wenzelm@12396
   255
  finally show "finite (UNIV :: unit set)" .
wenzelm@12396
   256
qed
wenzelm@12396
   257
wenzelm@12396
   258
instance * :: (finite, finite) finite
wenzelm@12396
   259
proof
wenzelm@12396
   260
  show "finite (UNIV :: ('a \<times> 'b) set)"
wenzelm@12396
   261
  proof (rule finite_Prod_UNIV)
wenzelm@12396
   262
    show "finite (UNIV :: 'a set)" by (rule finite)
wenzelm@12396
   263
    show "finite (UNIV :: 'b set)" by (rule finite)
wenzelm@12396
   264
  qed
wenzelm@12396
   265
qed
wenzelm@12396
   266
wenzelm@12396
   267
wenzelm@12396
   268
subsubsection {* The powerset of a finite set *}
wenzelm@12396
   269
wenzelm@12396
   270
lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
wenzelm@12396
   271
proof
wenzelm@12396
   272
  assume "finite (Pow A)"
wenzelm@12396
   273
  with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
wenzelm@12396
   274
  thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
wenzelm@12396
   275
next
wenzelm@12396
   276
  assume "finite A"
wenzelm@12396
   277
  thus "finite (Pow A)"
wenzelm@12396
   278
    by induct (simp_all add: finite_UnI finite_imageI Pow_insert)
wenzelm@12396
   279
qed
wenzelm@12396
   280
wenzelm@12396
   281
lemma finite_converse [iff]: "finite (r^-1) = finite r"
wenzelm@12396
   282
  apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
wenzelm@12396
   283
   apply simp
wenzelm@12396
   284
   apply (rule iffI)
wenzelm@12396
   285
    apply (erule finite_imageD [unfolded inj_on_def])
wenzelm@12396
   286
    apply (simp split add: split_split)
wenzelm@12396
   287
   apply (erule finite_imageI)
paulson@14208
   288
  apply (simp add: converse_def image_def, auto)
wenzelm@12396
   289
  apply (rule bexI)
wenzelm@12396
   290
   prefer 2 apply assumption
wenzelm@12396
   291
  apply simp
wenzelm@12396
   292
  done
wenzelm@12396
   293
paulson@14430
   294
wenzelm@12396
   295
subsubsection {* Finiteness of transitive closure *}
wenzelm@12396
   296
wenzelm@12396
   297
text {* (Thanks to Sidi Ehmety) *}
wenzelm@12396
   298
wenzelm@12396
   299
lemma finite_Field: "finite r ==> finite (Field r)"
wenzelm@12396
   300
  -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
wenzelm@12396
   301
  apply (induct set: Finites)
wenzelm@12396
   302
   apply (auto simp add: Field_def Domain_insert Range_insert)
wenzelm@12396
   303
  done
wenzelm@12396
   304
wenzelm@12396
   305
lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
wenzelm@12396
   306
  apply clarify
wenzelm@12396
   307
  apply (erule trancl_induct)
wenzelm@12396
   308
   apply (auto simp add: Field_def)
wenzelm@12396
   309
  done
wenzelm@12396
   310
wenzelm@12396
   311
lemma finite_trancl: "finite (r^+) = finite r"
wenzelm@12396
   312
  apply auto
wenzelm@12396
   313
   prefer 2
wenzelm@12396
   314
   apply (rule trancl_subset_Field2 [THEN finite_subset])
wenzelm@12396
   315
   apply (rule finite_SigmaI)
wenzelm@12396
   316
    prefer 3
berghofe@13704
   317
    apply (blast intro: r_into_trancl' finite_subset)
wenzelm@12396
   318
   apply (auto simp add: finite_Field)
wenzelm@12396
   319
  done
wenzelm@12396
   320
paulson@14430
   321
lemma finite_cartesian_product: "[| finite A; finite B |] ==>
paulson@14430
   322
    finite (A <*> B)"
paulson@14430
   323
  by (rule finite_SigmaI)
paulson@14430
   324
wenzelm@12396
   325
wenzelm@12396
   326
subsection {* Finite cardinality *}
wenzelm@12396
   327
wenzelm@12396
   328
text {*
wenzelm@12396
   329
  This definition, although traditional, is ugly to work with: @{text
wenzelm@12396
   330
  "card A == LEAST n. EX f. A = {f i | i. i < n}"}.  Therefore we have
wenzelm@12396
   331
  switched to an inductive one:
wenzelm@12396
   332
*}
wenzelm@12396
   333
wenzelm@12396
   334
consts cardR :: "('a set \<times> nat) set"
wenzelm@12396
   335
wenzelm@12396
   336
inductive cardR
wenzelm@12396
   337
  intros
wenzelm@12396
   338
    EmptyI: "({}, 0) : cardR"
wenzelm@12396
   339
    InsertI: "(A, n) : cardR ==> a \<notin> A ==> (insert a A, Suc n) : cardR"
wenzelm@12396
   340
wenzelm@12396
   341
constdefs
wenzelm@12396
   342
  card :: "'a set => nat"
wenzelm@12396
   343
  "card A == THE n. (A, n) : cardR"
wenzelm@12396
   344
wenzelm@12396
   345
inductive_cases cardR_emptyE: "({}, n) : cardR"
wenzelm@12396
   346
inductive_cases cardR_insertE: "(insert a A,n) : cardR"
wenzelm@12396
   347
wenzelm@12396
   348
lemma cardR_SucD: "(A, n) : cardR ==> a : A --> (EX m. n = Suc m)"
wenzelm@12396
   349
  by (induct set: cardR) simp_all
wenzelm@12396
   350
wenzelm@12396
   351
lemma cardR_determ_aux1:
wenzelm@12396
   352
    "(A, m): cardR ==> (!!n a. m = Suc n ==> a:A ==> (A - {a}, n) : cardR)"
paulson@14208
   353
  apply (induct set: cardR, auto)
paulson@14208
   354
  apply (simp add: insert_Diff_if, auto)
wenzelm@12396
   355
  apply (drule cardR_SucD)
wenzelm@12396
   356
  apply (blast intro!: cardR.intros)
wenzelm@12396
   357
  done
wenzelm@12396
   358
wenzelm@12396
   359
lemma cardR_determ_aux2: "(insert a A, Suc m) : cardR ==> a \<notin> A ==> (A, m) : cardR"
wenzelm@12396
   360
  by (drule cardR_determ_aux1) auto
wenzelm@12396
   361
wenzelm@12396
   362
lemma cardR_determ: "(A, m): cardR ==> (!!n. (A, n) : cardR ==> n = m)"
wenzelm@12396
   363
  apply (induct set: cardR)
wenzelm@12396
   364
   apply (safe elim!: cardR_emptyE cardR_insertE)
wenzelm@12396
   365
  apply (rename_tac B b m)
wenzelm@12396
   366
  apply (case_tac "a = b")
wenzelm@12396
   367
   apply (subgoal_tac "A = B")
paulson@14208
   368
    prefer 2 apply (blast elim: equalityE, blast)
wenzelm@12396
   369
  apply (subgoal_tac "EX C. A = insert b C & B = insert a C")
wenzelm@12396
   370
   prefer 2
wenzelm@12396
   371
   apply (rule_tac x = "A Int B" in exI)
wenzelm@12396
   372
   apply (blast elim: equalityE)
wenzelm@12396
   373
  apply (frule_tac A = B in cardR_SucD)
wenzelm@12396
   374
  apply (blast intro!: cardR.intros dest!: cardR_determ_aux2)
wenzelm@12396
   375
  done
wenzelm@12396
   376
wenzelm@12396
   377
lemma cardR_imp_finite: "(A, n) : cardR ==> finite A"
wenzelm@12396
   378
  by (induct set: cardR) simp_all
wenzelm@12396
   379
wenzelm@12396
   380
lemma finite_imp_cardR: "finite A ==> EX n. (A, n) : cardR"
wenzelm@12396
   381
  by (induct set: Finites) (auto intro!: cardR.intros)
wenzelm@12396
   382
wenzelm@12396
   383
lemma card_equality: "(A,n) : cardR ==> card A = n"
wenzelm@12396
   384
  by (unfold card_def) (blast intro: cardR_determ)
wenzelm@12396
   385
wenzelm@12396
   386
lemma card_empty [simp]: "card {} = 0"
wenzelm@12396
   387
  by (unfold card_def) (blast intro!: cardR.intros elim!: cardR_emptyE)
wenzelm@12396
   388
wenzelm@12396
   389
lemma card_insert_disjoint [simp]:
wenzelm@12396
   390
  "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
wenzelm@12396
   391
proof -
wenzelm@12396
   392
  assume x: "x \<notin> A"
wenzelm@12396
   393
  hence aux: "!!n. ((insert x A, n) : cardR) = (EX m. (A, m) : cardR & n = Suc m)"
wenzelm@12396
   394
    apply (auto intro!: cardR.intros)
wenzelm@12396
   395
    apply (rule_tac A1 = A in finite_imp_cardR [THEN exE])
wenzelm@12396
   396
     apply (force dest: cardR_imp_finite)
wenzelm@12396
   397
    apply (blast intro!: cardR.intros intro: cardR_determ)
wenzelm@12396
   398
    done
wenzelm@12396
   399
  assume "finite A"
wenzelm@12396
   400
  thus ?thesis
wenzelm@12396
   401
    apply (simp add: card_def aux)
wenzelm@12396
   402
    apply (rule the_equality)
wenzelm@12396
   403
     apply (auto intro: finite_imp_cardR
wenzelm@12396
   404
       cong: conj_cong simp: card_def [symmetric] card_equality)
wenzelm@12396
   405
    done
wenzelm@12396
   406
qed
wenzelm@12396
   407
wenzelm@12396
   408
lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})"
wenzelm@12396
   409
  apply auto
paulson@14208
   410
  apply (drule_tac a = x in mk_disjoint_insert, clarify)
paulson@14208
   411
  apply (rotate_tac -1, auto)
wenzelm@12396
   412
  done
wenzelm@12396
   413
wenzelm@12396
   414
lemma card_insert_if:
wenzelm@12396
   415
    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
wenzelm@12396
   416
  by (simp add: insert_absorb)
wenzelm@12396
   417
wenzelm@12396
   418
lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
nipkow@14302
   419
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
nipkow@14302
   420
apply(simp del:insert_Diff_single)
nipkow@14302
   421
done
wenzelm@12396
   422
wenzelm@12396
   423
lemma card_Diff_singleton:
wenzelm@12396
   424
    "finite A ==> x: A ==> card (A - {x}) = card A - 1"
wenzelm@12396
   425
  by (simp add: card_Suc_Diff1 [symmetric])
wenzelm@12396
   426
wenzelm@12396
   427
lemma card_Diff_singleton_if:
wenzelm@12396
   428
    "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)"
wenzelm@12396
   429
  by (simp add: card_Diff_singleton)
wenzelm@12396
   430
wenzelm@12396
   431
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
wenzelm@12396
   432
  by (simp add: card_insert_if card_Suc_Diff1)
wenzelm@12396
   433
wenzelm@12396
   434
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
wenzelm@12396
   435
  by (simp add: card_insert_if)
wenzelm@12396
   436
wenzelm@12396
   437
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
paulson@14208
   438
  apply (induct set: Finites, simp, clarify)
wenzelm@12396
   439
  apply (subgoal_tac "finite A & A - {x} <= F")
paulson@14208
   440
   prefer 2 apply (blast intro: finite_subset, atomize)
wenzelm@12396
   441
  apply (drule_tac x = "A - {x}" in spec)
wenzelm@12396
   442
  apply (simp add: card_Diff_singleton_if split add: split_if_asm)
paulson@14208
   443
  apply (case_tac "card A", auto)
wenzelm@12396
   444
  done
wenzelm@12396
   445
wenzelm@12396
   446
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
wenzelm@12396
   447
  apply (simp add: psubset_def linorder_not_le [symmetric])
wenzelm@12396
   448
  apply (blast dest: card_seteq)
wenzelm@12396
   449
  done
wenzelm@12396
   450
wenzelm@12396
   451
lemma card_mono: "finite B ==> A <= B ==> card A <= card B"
paulson@14208
   452
  apply (case_tac "A = B", simp)
wenzelm@12396
   453
  apply (simp add: linorder_not_less [symmetric])
wenzelm@12396
   454
  apply (blast dest: card_seteq intro: order_less_imp_le)
wenzelm@12396
   455
  done
wenzelm@12396
   456
wenzelm@12396
   457
lemma card_Un_Int: "finite A ==> finite B
wenzelm@12396
   458
    ==> card A + card B = card (A Un B) + card (A Int B)"
paulson@14208
   459
  apply (induct set: Finites, simp)
wenzelm@12396
   460
  apply (simp add: insert_absorb Int_insert_left)
wenzelm@12396
   461
  done
wenzelm@12396
   462
wenzelm@12396
   463
lemma card_Un_disjoint: "finite A ==> finite B
wenzelm@12396
   464
    ==> A Int B = {} ==> card (A Un B) = card A + card B"
wenzelm@12396
   465
  by (simp add: card_Un_Int)
wenzelm@12396
   466
wenzelm@12396
   467
lemma card_Diff_subset:
wenzelm@12396
   468
    "finite A ==> B <= A ==> card A - card B = card (A - B)"
wenzelm@12396
   469
  apply (subgoal_tac "(A - B) Un B = A")
wenzelm@12396
   470
   prefer 2 apply blast
paulson@14331
   471
  apply (rule nat_add_right_cancel [THEN iffD1])
wenzelm@12396
   472
  apply (rule card_Un_disjoint [THEN subst])
wenzelm@12396
   473
     apply (erule_tac [4] ssubst)
wenzelm@12396
   474
     prefer 3 apply blast
wenzelm@12396
   475
    apply (simp_all add: add_commute not_less_iff_le
wenzelm@12396
   476
      add_diff_inverse card_mono finite_subset)
wenzelm@12396
   477
  done
wenzelm@12396
   478
wenzelm@12396
   479
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
wenzelm@12396
   480
  apply (rule Suc_less_SucD)
wenzelm@12396
   481
  apply (simp add: card_Suc_Diff1)
wenzelm@12396
   482
  done
wenzelm@12396
   483
wenzelm@12396
   484
lemma card_Diff2_less:
wenzelm@12396
   485
    "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
wenzelm@12396
   486
  apply (case_tac "x = y")
wenzelm@12396
   487
   apply (simp add: card_Diff1_less)
wenzelm@12396
   488
  apply (rule less_trans)
wenzelm@12396
   489
   prefer 2 apply (auto intro!: card_Diff1_less)
wenzelm@12396
   490
  done
wenzelm@12396
   491
wenzelm@12396
   492
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
wenzelm@12396
   493
  apply (case_tac "x : A")
wenzelm@12396
   494
   apply (simp_all add: card_Diff1_less less_imp_le)
wenzelm@12396
   495
  done
wenzelm@12396
   496
wenzelm@12396
   497
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
paulson@14208
   498
by (erule psubsetI, blast)
wenzelm@12396
   499
paulson@14889
   500
lemma insert_partition:
paulson@14889
   501
     "[| x \<notin> F; \<forall>c1\<in>insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 --> c1 \<inter> c2 = {}|] 
paulson@14889
   502
      ==> x \<inter> \<Union> F = {}"
paulson@14889
   503
by auto
paulson@14889
   504
paulson@14889
   505
(* main cardinality theorem *)
paulson@14889
   506
lemma card_partition [rule_format]:
paulson@14889
   507
     "finite C ==>  
paulson@14889
   508
        finite (\<Union> C) -->  
paulson@14889
   509
        (\<forall>c\<in>C. card c = k) -->   
paulson@14889
   510
        (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->  
paulson@14889
   511
        k * card(C) = card (\<Union> C)"
paulson@14889
   512
apply (erule finite_induct, simp)
paulson@14889
   513
apply (simp add: card_insert_disjoint card_Un_disjoint insert_partition 
paulson@14889
   514
       finite_subset [of _ "\<Union> (insert x F)"])
paulson@14889
   515
done
paulson@14889
   516
wenzelm@12396
   517
wenzelm@12396
   518
subsubsection {* Cardinality of image *}
wenzelm@12396
   519
wenzelm@12396
   520
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
paulson@14208
   521
  apply (induct set: Finites, simp)
wenzelm@12396
   522
  apply (simp add: le_SucI finite_imageI card_insert_if)
wenzelm@12396
   523
  done
wenzelm@12396
   524
wenzelm@12396
   525
lemma card_image: "finite A ==> inj_on f A ==> card (f ` A) = card A"
nipkow@15111
   526
by (induct set: Finites, simp_all)
wenzelm@12396
   527
wenzelm@12396
   528
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
wenzelm@12396
   529
  by (simp add: card_seteq card_image)
wenzelm@12396
   530
nipkow@15111
   531
lemma eq_card_imp_inj_on:
nipkow@15111
   532
  "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
nipkow@15111
   533
apply(induct rule:finite_induct)
nipkow@15111
   534
 apply simp
nipkow@15111
   535
apply(frule card_image_le[where f = f])
nipkow@15111
   536
apply(simp add:card_insert_if split:if_splits)
nipkow@15111
   537
done
nipkow@15111
   538
nipkow@15111
   539
lemma inj_on_iff_eq_card:
nipkow@15111
   540
  "finite A ==> inj_on f A = (card(f ` A) = card A)"
nipkow@15111
   541
by(blast intro: card_image eq_card_imp_inj_on)
nipkow@15111
   542
wenzelm@12396
   543
wenzelm@12396
   544
subsubsection {* Cardinality of the Powerset *}
wenzelm@12396
   545
wenzelm@12396
   546
lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
wenzelm@12396
   547
  apply (induct set: Finites)
wenzelm@12396
   548
   apply (simp_all add: Pow_insert)
paulson@14208
   549
  apply (subst card_Un_disjoint, blast)
paulson@14208
   550
    apply (blast intro: finite_imageI, blast)
wenzelm@12396
   551
  apply (subgoal_tac "inj_on (insert x) (Pow F)")
wenzelm@12396
   552
   apply (simp add: card_image Pow_insert)
wenzelm@12396
   553
  apply (unfold inj_on_def)
wenzelm@12396
   554
  apply (blast elim!: equalityE)
wenzelm@12396
   555
  done
wenzelm@12396
   556
wenzelm@12396
   557
text {*
wenzelm@12396
   558
  \medskip Relates to equivalence classes.  Based on a theorem of
wenzelm@12396
   559
  F. Kammüller's.  The @{prop "finite C"} premise is redundant.
wenzelm@12396
   560
*}
wenzelm@12396
   561
wenzelm@12396
   562
lemma dvd_partition:
wenzelm@12396
   563
  "finite C ==> finite (Union C) ==>
wenzelm@12396
   564
    ALL c : C. k dvd card c ==>
paulson@14430
   565
    (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
wenzelm@12396
   566
  k dvd card (Union C)"
paulson@14208
   567
  apply (induct set: Finites, simp_all, clarify)
wenzelm@12396
   568
  apply (subst card_Un_disjoint)
wenzelm@12396
   569
  apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
wenzelm@12396
   570
  done
wenzelm@12396
   571
wenzelm@12396
   572
wenzelm@12396
   573
subsection {* A fold functional for finite sets *}
wenzelm@12396
   574
wenzelm@12396
   575
text {*
wenzelm@12396
   576
  For @{text n} non-negative we have @{text "fold f e {x1, ..., xn} =
wenzelm@12396
   577
  f x1 (... (f xn e))"} where @{text f} is at least left-commutative.
wenzelm@12396
   578
*}
wenzelm@12396
   579
wenzelm@12396
   580
consts
wenzelm@12396
   581
  foldSet :: "('b => 'a => 'a) => 'a => ('b set \<times> 'a) set"
wenzelm@12396
   582
wenzelm@12396
   583
inductive "foldSet f e"
wenzelm@12396
   584
  intros
wenzelm@12396
   585
    emptyI [intro]: "({}, e) : foldSet f e"
wenzelm@12396
   586
    insertI [intro]: "x \<notin> A ==> (A, y) : foldSet f e ==> (insert x A, f x y) : foldSet f e"
wenzelm@12396
   587
wenzelm@12396
   588
inductive_cases empty_foldSetE [elim!]: "({}, x) : foldSet f e"
wenzelm@12396
   589
wenzelm@12396
   590
constdefs
wenzelm@12396
   591
  fold :: "('b => 'a => 'a) => 'a => 'b set => 'a"
wenzelm@12396
   592
  "fold f e A == THE x. (A, x) : foldSet f e"
wenzelm@12396
   593
wenzelm@12396
   594
lemma Diff1_foldSet: "(A - {x}, y) : foldSet f e ==> x: A ==> (A, f x y) : foldSet f e"
paulson@14208
   595
by (erule insert_Diff [THEN subst], rule foldSet.intros, auto)
wenzelm@12396
   596
wenzelm@12396
   597
lemma foldSet_imp_finite [simp]: "(A, x) : foldSet f e ==> finite A"
wenzelm@12396
   598
  by (induct set: foldSet) auto
wenzelm@12396
   599
wenzelm@12396
   600
lemma finite_imp_foldSet: "finite A ==> EX x. (A, x) : foldSet f e"
wenzelm@12396
   601
  by (induct set: Finites) auto
wenzelm@12396
   602
wenzelm@12396
   603
wenzelm@12396
   604
subsubsection {* Left-commutative operations *}
wenzelm@12396
   605
wenzelm@12396
   606
locale LC =
wenzelm@12396
   607
  fixes f :: "'b => 'a => 'a"    (infixl "\<cdot>" 70)
wenzelm@12396
   608
  assumes left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
wenzelm@12396
   609
wenzelm@12396
   610
lemma (in LC) foldSet_determ_aux:
wenzelm@12396
   611
  "ALL A x. card A < n --> (A, x) : foldSet f e -->
wenzelm@12396
   612
    (ALL y. (A, y) : foldSet f e --> y = x)"
wenzelm@12396
   613
  apply (induct n)
wenzelm@12396
   614
   apply (auto simp add: less_Suc_eq)
paulson@14208
   615
  apply (erule foldSet.cases, blast)
paulson@14208
   616
  apply (erule foldSet.cases, blast, clarify)
wenzelm@12396
   617
  txt {* force simplification of @{text "card A < card (insert ...)"}. *}
wenzelm@12396
   618
  apply (erule rev_mp)
wenzelm@12396
   619
  apply (simp add: less_Suc_eq_le)
wenzelm@12396
   620
  apply (rule impI)
wenzelm@12396
   621
  apply (rename_tac Aa xa ya Ab xb yb, case_tac "xa = xb")
wenzelm@12396
   622
   apply (subgoal_tac "Aa = Ab")
paulson@14208
   623
    prefer 2 apply (blast elim!: equalityE, blast)
wenzelm@12396
   624
  txt {* case @{prop "xa \<notin> xb"}. *}
wenzelm@12396
   625
  apply (subgoal_tac "Aa - {xb} = Ab - {xa} & xb : Aa & xa : Ab")
paulson@14208
   626
   prefer 2 apply (blast elim!: equalityE, clarify)
wenzelm@12396
   627
  apply (subgoal_tac "Aa = insert xb Ab - {xa}")
wenzelm@12396
   628
   prefer 2 apply blast
wenzelm@12396
   629
  apply (subgoal_tac "card Aa <= card Ab")
wenzelm@12396
   630
   prefer 2
wenzelm@12396
   631
   apply (rule Suc_le_mono [THEN subst])
wenzelm@12396
   632
   apply (simp add: card_Suc_Diff1)
wenzelm@12396
   633
  apply (rule_tac A1 = "Aa - {xb}" and f1 = f in finite_imp_foldSet [THEN exE])
wenzelm@12396
   634
  apply (blast intro: foldSet_imp_finite finite_Diff)
wenzelm@12396
   635
  apply (frule (1) Diff1_foldSet)
wenzelm@12396
   636
  apply (subgoal_tac "ya = f xb x")
wenzelm@12396
   637
   prefer 2 apply (blast del: equalityCE)
wenzelm@12396
   638
  apply (subgoal_tac "(Ab - {xa}, x) : foldSet f e")
wenzelm@12396
   639
   prefer 2 apply simp
wenzelm@12396
   640
  apply (subgoal_tac "yb = f xa x")
wenzelm@12396
   641
   prefer 2 apply (blast del: equalityCE dest: Diff1_foldSet)
wenzelm@12396
   642
  apply (simp (no_asm_simp) add: left_commute)
wenzelm@12396
   643
  done
wenzelm@12396
   644
wenzelm@12396
   645
lemma (in LC) foldSet_determ: "(A, x) : foldSet f e ==> (A, y) : foldSet f e ==> y = x"
wenzelm@12396
   646
  by (blast intro: foldSet_determ_aux [rule_format])
wenzelm@12396
   647
wenzelm@12396
   648
lemma (in LC) fold_equality: "(A, y) : foldSet f e ==> fold f e A = y"
wenzelm@12396
   649
  by (unfold fold_def) (blast intro: foldSet_determ)
wenzelm@12396
   650
wenzelm@12396
   651
lemma fold_empty [simp]: "fold f e {} = e"
wenzelm@12396
   652
  by (unfold fold_def) blast
wenzelm@12396
   653
wenzelm@12396
   654
lemma (in LC) fold_insert_aux: "x \<notin> A ==>
wenzelm@12396
   655
    ((insert x A, v) : foldSet f e) =
wenzelm@12396
   656
    (EX y. (A, y) : foldSet f e & v = f x y)"
wenzelm@12396
   657
  apply auto
wenzelm@12396
   658
  apply (rule_tac A1 = A and f1 = f in finite_imp_foldSet [THEN exE])
wenzelm@12396
   659
   apply (fastsimp dest: foldSet_imp_finite)
wenzelm@12396
   660
  apply (blast intro: foldSet_determ)
wenzelm@12396
   661
  done
wenzelm@12396
   662
wenzelm@12396
   663
lemma (in LC) fold_insert:
wenzelm@12396
   664
    "finite A ==> x \<notin> A ==> fold f e (insert x A) = f x (fold f e A)"
wenzelm@12396
   665
  apply (unfold fold_def)
wenzelm@12396
   666
  apply (simp add: fold_insert_aux)
wenzelm@12396
   667
  apply (rule the_equality)
wenzelm@12396
   668
  apply (auto intro: finite_imp_foldSet
wenzelm@12396
   669
    cong add: conj_cong simp add: fold_def [symmetric] fold_equality)
wenzelm@12396
   670
  done
wenzelm@12396
   671
wenzelm@12396
   672
lemma (in LC) fold_commute: "finite A ==> (!!e. f x (fold f e A) = fold f (f x e) A)"
paulson@14208
   673
  apply (induct set: Finites, simp)
wenzelm@12396
   674
  apply (simp add: left_commute fold_insert)
wenzelm@12396
   675
  done
wenzelm@12396
   676
wenzelm@12396
   677
lemma (in LC) fold_nest_Un_Int:
wenzelm@12396
   678
  "finite A ==> finite B
wenzelm@12396
   679
    ==> fold f (fold f e B) A = fold f (fold f e (A Int B)) (A Un B)"
paulson@14208
   680
  apply (induct set: Finites, simp)
wenzelm@12396
   681
  apply (simp add: fold_insert fold_commute Int_insert_left insert_absorb)
wenzelm@12396
   682
  done
wenzelm@12396
   683
wenzelm@12396
   684
lemma (in LC) fold_nest_Un_disjoint:
wenzelm@12396
   685
  "finite A ==> finite B ==> A Int B = {}
wenzelm@12396
   686
    ==> fold f e (A Un B) = fold f (fold f e B) A"
wenzelm@12396
   687
  by (simp add: fold_nest_Un_Int)
wenzelm@12396
   688
wenzelm@12396
   689
declare foldSet_imp_finite [simp del]
wenzelm@12396
   690
    empty_foldSetE [rule del]  foldSet.intros [rule del]
wenzelm@12396
   691
  -- {* Delete rules to do with @{text foldSet} relation. *}
wenzelm@12396
   692
wenzelm@12396
   693
wenzelm@12396
   694
wenzelm@12396
   695
subsubsection {* Commutative monoids *}
wenzelm@12396
   696
wenzelm@12396
   697
text {*
wenzelm@12396
   698
  We enter a more restrictive context, with @{text "f :: 'a => 'a => 'a"}
wenzelm@12396
   699
  instead of @{text "'b => 'a => 'a"}.
wenzelm@12396
   700
*}
wenzelm@12396
   701
wenzelm@12396
   702
locale ACe =
wenzelm@12396
   703
  fixes f :: "'a => 'a => 'a"    (infixl "\<cdot>" 70)
wenzelm@12396
   704
    and e :: 'a
wenzelm@12396
   705
  assumes ident [simp]: "x \<cdot> e = x"
wenzelm@12396
   706
    and commute: "x \<cdot> y = y \<cdot> x"
wenzelm@12396
   707
    and assoc: "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)"
wenzelm@12396
   708
wenzelm@12396
   709
lemma (in ACe) left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
wenzelm@12396
   710
proof -
wenzelm@12396
   711
  have "x \<cdot> (y \<cdot> z) = (y \<cdot> z) \<cdot> x" by (simp only: commute)
wenzelm@12396
   712
  also have "... = y \<cdot> (z \<cdot> x)" by (simp only: assoc)
wenzelm@12396
   713
  also have "z \<cdot> x = x \<cdot> z" by (simp only: commute)
wenzelm@12396
   714
  finally show ?thesis .
wenzelm@12396
   715
qed
wenzelm@12396
   716
wenzelm@12718
   717
lemmas (in ACe) AC = assoc commute left_commute
wenzelm@12396
   718
wenzelm@12693
   719
lemma (in ACe) left_ident [simp]: "e \<cdot> x = x"
wenzelm@12396
   720
proof -
wenzelm@12396
   721
  have "x \<cdot> e = x" by (rule ident)
wenzelm@12396
   722
  thus ?thesis by (subst commute)
wenzelm@12396
   723
qed
wenzelm@12396
   724
wenzelm@12396
   725
lemma (in ACe) fold_Un_Int:
wenzelm@12396
   726
  "finite A ==> finite B ==>
wenzelm@12396
   727
    fold f e A \<cdot> fold f e B = fold f e (A Un B) \<cdot> fold f e (A Int B)"
paulson@14208
   728
  apply (induct set: Finites, simp)
wenzelm@13400
   729
  apply (simp add: AC insert_absorb Int_insert_left
wenzelm@13421
   730
    LC.fold_insert [OF LC.intro])
wenzelm@12396
   731
  done
wenzelm@12396
   732
wenzelm@12396
   733
lemma (in ACe) fold_Un_disjoint:
wenzelm@12396
   734
  "finite A ==> finite B ==> A Int B = {} ==>
wenzelm@12396
   735
    fold f e (A Un B) = fold f e A \<cdot> fold f e B"
wenzelm@12396
   736
  by (simp add: fold_Un_Int)
wenzelm@12396
   737
wenzelm@12396
   738
lemma (in ACe) fold_Un_disjoint2:
wenzelm@12396
   739
  "finite A ==> finite B ==> A Int B = {} ==>
wenzelm@12396
   740
    fold (f o g) e (A Un B) = fold (f o g) e A \<cdot> fold (f o g) e B"
wenzelm@12396
   741
proof -
wenzelm@12396
   742
  assume b: "finite B"
wenzelm@12396
   743
  assume "finite A"
wenzelm@12396
   744
  thus "A Int B = {} ==>
wenzelm@12396
   745
    fold (f o g) e (A Un B) = fold (f o g) e A \<cdot> fold (f o g) e B"
wenzelm@12396
   746
  proof induct
wenzelm@12396
   747
    case empty
wenzelm@12396
   748
    thus ?case by simp
wenzelm@12396
   749
  next
wenzelm@12396
   750
    case (insert F x)
paulson@13571
   751
    have "fold (f o g) e (insert x F \<union> B) = fold (f o g) e (insert x (F \<union> B))"
wenzelm@12396
   752
      by simp
paulson@13571
   753
    also have "... = (f o g) x (fold (f o g) e (F \<union> B))"
wenzelm@13400
   754
      by (rule LC.fold_insert [OF LC.intro])
wenzelm@13421
   755
        (insert b insert, auto simp add: left_commute)
paulson@13571
   756
    also from insert have "fold (f o g) e (F \<union> B) =
paulson@13571
   757
      fold (f o g) e F \<cdot> fold (f o g) e B" by blast
paulson@13571
   758
    also have "(f o g) x ... = (f o g) x (fold (f o g) e F) \<cdot> fold (f o g) e B"
wenzelm@12396
   759
      by (simp add: AC)
paulson@13571
   760
    also have "(f o g) x (fold (f o g) e F) = fold (f o g) e (insert x F)"
wenzelm@13400
   761
      by (rule LC.fold_insert [OF LC.intro, symmetric]) (insert b insert,
wenzelm@14661
   762
        auto simp add: left_commute)
wenzelm@12396
   763
    finally show ?case .
wenzelm@12396
   764
  qed
wenzelm@12396
   765
qed
wenzelm@12396
   766
wenzelm@12396
   767
wenzelm@12396
   768
subsection {* Generalized summation over a set *}
wenzelm@12396
   769
wenzelm@12396
   770
constdefs
obua@14738
   771
  setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
wenzelm@12396
   772
  "setsum f A == if finite A then fold (op + o f) 0 A else 0"
wenzelm@12396
   773
nipkow@15042
   774
text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
nipkow@15042
   775
written @{text"\<Sum>x\<in>A. e"}. *}
nipkow@15042
   776
wenzelm@12396
   777
syntax
nipkow@15074
   778
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
wenzelm@12396
   779
syntax (xsymbols)
obua@14738
   780
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
kleing@14565
   781
syntax (HTML output)
obua@14738
   782
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15074
   783
nipkow@15074
   784
translations -- {* Beware of argument permutation! *}
nipkow@15074
   785
  "SUM i:A. b" == "setsum (%i. b) A"
nipkow@15074
   786
  "\<Sum>i\<in>A. b" == "setsum (%i. b) A"
wenzelm@12396
   787
nipkow@15042
   788
text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
nipkow@15042
   789
 @{text"\<Sum>x|P. e"}. *}
nipkow@15042
   790
nipkow@15042
   791
syntax
nipkow@15074
   792
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
nipkow@15042
   793
syntax (xsymbols)
nipkow@15042
   794
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15042
   795
syntax (HTML output)
nipkow@15042
   796
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15042
   797
nipkow@15074
   798
translations
nipkow@15074
   799
  "SUM x|P. t" => "setsum (%x. t) {x. P}"
nipkow@15074
   800
  "\<Sum>x|P. t" => "setsum (%x. t) {x. P}"
nipkow@15042
   801
nipkow@15042
   802
print_translation {*
nipkow@15042
   803
let
nipkow@15042
   804
  fun setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) $ Abs(y,Ty,P)] = 
nipkow@15042
   805
    (if x<>y then raise Match
nipkow@15042
   806
     else let val x' = Syntax.mark_bound x
nipkow@15042
   807
              val t' = subst_bound(x',t)
nipkow@15042
   808
              val P' = subst_bound(x',P)
nipkow@15042
   809
          in Syntax.const "_qsetsum" $ Syntax.mark_bound x $ P' $ t' end)
nipkow@15042
   810
in
nipkow@15042
   811
[("setsum", setsum_tr')]
nipkow@15042
   812
end
nipkow@15042
   813
*}
nipkow@15042
   814
paulson@15047
   815
text{* As Jeremy Avigad notes, setprod needs the same treatment \dots *}
wenzelm@12396
   816
wenzelm@12396
   817
lemma setsum_empty [simp]: "setsum f {} = 0"
wenzelm@12396
   818
  by (simp add: setsum_def)
wenzelm@12396
   819
wenzelm@12396
   820
lemma setsum_insert [simp]:
wenzelm@12396
   821
    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
paulson@15047
   822
  by (simp add: setsum_def LC.fold_insert [OF LC.intro] add_left_commute)
wenzelm@12396
   823
paulson@14944
   824
lemma setsum_reindex [rule_format]:
paulson@14944
   825
     "finite B ==> inj_on f B --> setsum h (f ` B) = setsum (h \<circ> f) B"
nipkow@15111
   826
by (rule finite_induct, auto)
wenzelm@12396
   827
paulson@14944
   828
lemma setsum_reindex_id:
paulson@14944
   829
     "finite B ==> inj_on f B ==> setsum f B = setsum id (f ` B)"
paulson@14485
   830
by (auto simp add: setsum_reindex id_o)
wenzelm@12396
   831
wenzelm@12396
   832
lemma setsum_cong:
wenzelm@12396
   833
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
wenzelm@12396
   834
  apply (case_tac "finite B")
paulson@14208
   835
   prefer 2 apply (simp add: setsum_def, simp)
wenzelm@12396
   836
  apply (subgoal_tac "ALL C. C <= B --> (ALL x:C. f x = g x) --> setsum f C = setsum g C")
wenzelm@12396
   837
   apply simp
paulson@14208
   838
  apply (erule finite_induct, simp)
paulson@14208
   839
  apply (simp add: subset_insert_iff, clarify)
wenzelm@12396
   840
  apply (subgoal_tac "finite C")
wenzelm@12396
   841
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
wenzelm@12396
   842
  apply (subgoal_tac "C = insert x (C - {x})")
wenzelm@12396
   843
   prefer 2 apply blast
wenzelm@12396
   844
  apply (erule ssubst)
wenzelm@12396
   845
  apply (drule spec)
wenzelm@12396
   846
  apply (erule (1) notE impE)
nipkow@14302
   847
  apply (simp add: Ball_def del:insert_Diff_single)
wenzelm@12396
   848
  done
wenzelm@12396
   849
paulson@14944
   850
lemma setsum_reindex_cong:
paulson@14944
   851
     "[|finite A; inj_on f A; B = f ` A; !!a. g a = h (f a)|] 
paulson@14944
   852
      ==> setsum h B = setsum g A"
paulson@14944
   853
  by (simp add: setsum_reindex cong: setsum_cong) 
paulson@14944
   854
paulson@14485
   855
lemma setsum_0: "setsum (%i. 0) A = 0"
paulson@14485
   856
  apply (case_tac "finite A")
paulson@14485
   857
   prefer 2 apply (simp add: setsum_def)
paulson@14485
   858
  apply (erule finite_induct, auto)
paulson@14430
   859
  done
paulson@14430
   860
paulson@14430
   861
lemma setsum_0': "ALL a:F. f a = 0 ==> setsum f F = 0"
paulson@14430
   862
  apply (subgoal_tac "setsum f F = setsum (%x. 0) F")
paulson@14430
   863
  apply (erule ssubst, rule setsum_0)
paulson@14430
   864
  apply (rule setsum_cong, auto)
paulson@14430
   865
  done
paulson@14430
   866
paulson@14485
   867
lemma card_eq_setsum: "finite A ==> card A = setsum (%x. 1) A"
paulson@14485
   868
  -- {* Could allow many @{text "card"} proofs to be simplified. *}
paulson@14485
   869
  by (induct set: Finites) auto
paulson@14430
   870
paulson@14485
   871
lemma setsum_Un_Int: "finite A ==> finite B
paulson@14485
   872
    ==> setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
paulson@14485
   873
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
paulson@14485
   874
  apply (induct set: Finites, simp)
obua@14738
   875
  apply (simp add: add_ac Int_insert_left insert_absorb)
paulson@14485
   876
  done
paulson@14485
   877
paulson@14485
   878
lemma setsum_Un_disjoint: "finite A ==> finite B
paulson@14485
   879
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
paulson@14485
   880
  apply (subst setsum_Un_Int [symmetric], auto)
paulson@14485
   881
  done
paulson@14430
   882
paulson@14485
   883
lemma setsum_UN_disjoint:
paulson@14485
   884
    "finite I ==> (ALL i:I. finite (A i)) ==>
paulson@14485
   885
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
paulson@14485
   886
      setsum f (UNION I A) = setsum (%i. setsum f (A i)) I"
paulson@14485
   887
  apply (induct set: Finites, simp, atomize)
paulson@14485
   888
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
paulson@14485
   889
   prefer 2 apply blast
paulson@14485
   890
  apply (subgoal_tac "A x Int UNION F A = {}")
paulson@14485
   891
   prefer 2 apply blast
paulson@14485
   892
  apply (simp add: setsum_Un_disjoint)
paulson@14485
   893
  done
paulson@14485
   894
paulson@14485
   895
lemma setsum_Union_disjoint:
paulson@14485
   896
  "finite C ==> (ALL A:C. finite A) ==>
paulson@14485
   897
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
paulson@14485
   898
      setsum f (Union C) = setsum (setsum f) C"
paulson@14485
   899
  apply (frule setsum_UN_disjoint [of C id f])
paulson@14485
   900
  apply (unfold Union_def id_def, assumption+)
paulson@14430
   901
  done
paulson@14430
   902
wenzelm@14661
   903
lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@15074
   904
    (\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) =
nipkow@15074
   905
    (\<Sum>z\<in>(SIGMA x:A. B x). f (fst z) (snd z))"
paulson@14485
   906
  apply (subst Sigma_def)
paulson@14485
   907
  apply (subst setsum_UN_disjoint)
paulson@14485
   908
  apply assumption
paulson@14485
   909
  apply (rule ballI)
paulson@14485
   910
  apply (drule_tac x = i in bspec, assumption)
wenzelm@14661
   911
  apply (subgoal_tac "(UN y:(B i). {(i, y)}) <= (%y. (i, y)) ` (B i)")
paulson@14485
   912
  apply (rule finite_surj)
paulson@14485
   913
  apply auto
paulson@14485
   914
  apply (rule setsum_cong, rule refl)
paulson@14485
   915
  apply (subst setsum_UN_disjoint)
paulson@14485
   916
  apply (erule bspec, assumption)
paulson@14485
   917
  apply auto
paulson@14485
   918
  done
paulson@14430
   919
paulson@14485
   920
lemma setsum_cartesian_product: "finite A ==> finite B ==>
nipkow@15074
   921
    (\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) =
nipkow@15074
   922
    (\<Sum>z\<in>A <*> B. f (fst z) (snd z))"
paulson@14485
   923
  by (erule setsum_Sigma, auto);
paulson@14485
   924
paulson@14485
   925
lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
paulson@14485
   926
  apply (case_tac "finite A")
paulson@14485
   927
   prefer 2 apply (simp add: setsum_def)
paulson@14485
   928
  apply (erule finite_induct, auto)
obua@14738
   929
  apply (simp add: add_ac)
paulson@14485
   930
  done
paulson@14430
   931
paulson@14430
   932
subsubsection {* Properties in more restricted classes of structures *}
paulson@14430
   933
paulson@14485
   934
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
paulson@14485
   935
  apply (case_tac "finite A")
paulson@14485
   936
   prefer 2 apply (simp add: setsum_def)
paulson@14485
   937
  apply (erule rev_mp)
paulson@14485
   938
  apply (erule finite_induct, auto)
paulson@14485
   939
  done
paulson@14485
   940
paulson@14430
   941
lemma setsum_eq_0_iff [simp]:
paulson@14430
   942
    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
paulson@14430
   943
  by (induct set: Finites) auto
paulson@14430
   944
paulson@15047
   945
lemma setsum_constant_nat:
nipkow@15074
   946
    "finite A ==> (\<Sum>x\<in>A. y) = (card A) * y"
paulson@15047
   947
  -- {* Generalized to any @{text comm_semiring_1_cancel} in
paulson@15047
   948
        @{text IntDef} as @{text setsum_constant}. *}
paulson@14430
   949
  by (erule finite_induct, auto)
paulson@14430
   950
paulson@14430
   951
lemma setsum_Un: "finite A ==> finite B ==>
paulson@14430
   952
    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
paulson@14430
   953
  -- {* For the natural numbers, we have subtraction. *}
obua@14738
   954
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
paulson@14430
   955
paulson@14430
   956
lemma setsum_Un_ring: "finite A ==> finite B ==>
obua@14738
   957
    (setsum f (A Un B) :: 'a :: comm_ring_1) =
paulson@14430
   958
      setsum f A + setsum f B - setsum f (A Int B)"
obua@14738
   959
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
paulson@14430
   960
paulson@14430
   961
lemma setsum_diff1: "(setsum f (A - {a}) :: nat) =
paulson@14430
   962
    (if a:A then setsum f A - f a else setsum f A)"
paulson@14430
   963
  apply (case_tac "finite A")
paulson@14430
   964
   prefer 2 apply (simp add: setsum_def)
paulson@14430
   965
  apply (erule finite_induct)
paulson@14430
   966
   apply (auto simp add: insert_Diff_if)
paulson@14430
   967
  apply (drule_tac a = a in mk_disjoint_insert, auto)
paulson@14430
   968
  done
paulson@14430
   969
nipkow@15124
   970
(* By Jeremy Siek: *)
nipkow@15124
   971
nipkow@15124
   972
lemma setsum_diff: 
nipkow@15124
   973
  assumes finB: "finite B"
nipkow@15124
   974
  shows "B \<subseteq> A \<Longrightarrow> (setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
nipkow@15124
   975
using finB
nipkow@15124
   976
proof (induct)
nipkow@15124
   977
  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
nipkow@15124
   978
next
nipkow@15124
   979
  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
nipkow@15124
   980
    and xFinA: "insert x F \<subseteq> A"
nipkow@15124
   981
    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
nipkow@15124
   982
  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
nipkow@15124
   983
  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
nipkow@15124
   984
    by (simp add: setsum_diff1)
nipkow@15124
   985
  from xFinA have "F \<subseteq> A" by simp
nipkow@15124
   986
  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
nipkow@15124
   987
  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
nipkow@15124
   988
    by simp
nipkow@15124
   989
  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
nipkow@15124
   990
  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
nipkow@15124
   991
    by simp
nipkow@15124
   992
  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
nipkow@15124
   993
  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
nipkow@15124
   994
    by simp
nipkow@15124
   995
  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
nipkow@15124
   996
qed
nipkow@15124
   997
obua@14738
   998
lemma setsum_negf: "finite A ==> setsum (%x. - (f x)::'a::comm_ring_1) A =
paulson@14430
   999
  - setsum f A"
paulson@14430
  1000
  by (induct set: Finites, auto)
paulson@14430
  1001
obua@14738
  1002
lemma setsum_subtractf: "finite A ==> setsum (%x. ((f x)::'a::comm_ring_1) - g x) A =
paulson@14430
  1003
  setsum f A - setsum g A"
paulson@14430
  1004
  by (simp add: diff_minus setsum_addf setsum_negf)
paulson@14430
  1005
paulson@14430
  1006
lemma setsum_nonneg: "[| finite A;
obua@14738
  1007
    \<forall>x \<in> A. (0::'a::ordered_semidom) \<le> f x |] ==>
paulson@14430
  1008
    0 \<le>  setsum f A";
paulson@14430
  1009
  apply (induct set: Finites, auto)
paulson@14430
  1010
  apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
paulson@14430
  1011
  apply (blast intro: add_mono)
paulson@14430
  1012
  done
paulson@14430
  1013
paulson@15047
  1014
lemma setsum_mult: 
paulson@15047
  1015
  fixes f :: "'a => ('b::semiring_0_cancel)"
paulson@15047
  1016
  assumes fin: "finite A" 
paulson@15047
  1017
  shows "r * setsum f A = setsum (%n. r * f n) A"
paulson@15047
  1018
using fin 
paulson@15047
  1019
proof (induct) 
paulson@15047
  1020
  case empty thus ?case by simp
paulson@15047
  1021
next
paulson@15047
  1022
  case (insert A x)
paulson@15047
  1023
  thus ?case by (simp add: right_distrib) 
paulson@15047
  1024
qed
paulson@15047
  1025
paulson@15047
  1026
lemma setsum_abs: 
paulson@15047
  1027
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
paulson@15047
  1028
  assumes fin: "finite A" 
paulson@15047
  1029
  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
paulson@15047
  1030
using fin 
paulson@15047
  1031
proof (induct) 
paulson@15047
  1032
  case empty thus ?case by simp
paulson@15047
  1033
next
paulson@15047
  1034
  case (insert A x)
paulson@15047
  1035
  thus ?case by (auto intro: abs_triangle_ineq order_trans)
paulson@15047
  1036
qed
paulson@15047
  1037
paulson@15047
  1038
lemma setsum_abs_ge_zero: 
paulson@15047
  1039
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
paulson@15047
  1040
  assumes fin: "finite A" 
paulson@15047
  1041
  shows "0 \<le> setsum (%i. abs(f i)) A"
paulson@15047
  1042
using fin 
paulson@15047
  1043
proof (induct) 
paulson@15047
  1044
  case empty thus ?case by simp
paulson@15047
  1045
next
paulson@15047
  1046
  case (insert A x) thus ?case by (auto intro: order_trans)
paulson@15047
  1047
qed
paulson@15047
  1048
paulson@14485
  1049
subsubsection {* Cardinality of unions and Sigma sets *}
paulson@14485
  1050
paulson@14485
  1051
lemma card_UN_disjoint:
paulson@14485
  1052
    "finite I ==> (ALL i:I. finite (A i)) ==>
paulson@14485
  1053
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
paulson@14485
  1054
      card (UNION I A) = setsum (%i. card (A i)) I"
paulson@14485
  1055
  apply (subst card_eq_setsum)
paulson@14485
  1056
  apply (subst finite_UN, assumption+)
paulson@15047
  1057
  apply (subgoal_tac
paulson@15047
  1058
           "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
paulson@15047
  1059
  apply (simp add: setsum_UN_disjoint) 
paulson@15047
  1060
  apply (simp add: setsum_constant_nat cong: setsum_cong) 
paulson@14485
  1061
  done
paulson@14485
  1062
paulson@14485
  1063
lemma card_Union_disjoint:
paulson@14485
  1064
  "finite C ==> (ALL A:C. finite A) ==>
paulson@14485
  1065
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
paulson@14485
  1066
      card (Union C) = setsum card C"
paulson@14485
  1067
  apply (frule card_UN_disjoint [of C id])
paulson@14485
  1068
  apply (unfold Union_def id_def, assumption+)
paulson@14485
  1069
  done
paulson@14430
  1070
paulson@14430
  1071
lemma SigmaI_insert: "y \<notin> A ==>
paulson@14430
  1072
  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
paulson@14430
  1073
  by auto
paulson@14430
  1074
paulson@14485
  1075
lemma card_cartesian_product_singleton: "finite A ==>
paulson@14430
  1076
    card({x} <*> A) = card(A)"
paulson@14430
  1077
  apply (subgoal_tac "inj_on (%y .(x,y)) A")
paulson@14430
  1078
  apply (frule card_image, assumption)
paulson@14430
  1079
  apply (subgoal_tac "(Pair x ` A) = {x} <*> A")
paulson@14430
  1080
  apply (auto simp add: inj_on_def)
paulson@14430
  1081
  done
paulson@14430
  1082
paulson@14430
  1083
lemma card_SigmaI [rule_format,simp]: "finite A ==>
paulson@14430
  1084
  (ALL a:A. finite (B a)) -->
nipkow@15074
  1085
  card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
paulson@14430
  1086
  apply (erule finite_induct, auto)
paulson@14430
  1087
  apply (subst SigmaI_insert, assumption)
paulson@14430
  1088
  apply (subst card_Un_disjoint)
paulson@14485
  1089
  apply (auto intro: finite_SigmaI simp add: card_cartesian_product_singleton)
paulson@14430
  1090
  done
paulson@14430
  1091
paulson@15047
  1092
lemma card_cartesian_product:
paulson@15047
  1093
     "[| finite A; finite B |] ==> card (A <*> B) = card(A) * card(B)"
paulson@15047
  1094
  by (simp add: setsum_constant_nat)
paulson@15047
  1095
paulson@14430
  1096
paulson@14430
  1097
paulson@14430
  1098
subsection {* Generalized product over a set *}
paulson@14430
  1099
paulson@14430
  1100
constdefs
obua@14738
  1101
  setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
paulson@14430
  1102
  "setprod f A == if finite A then fold (op * o f) 1 A else 1"
paulson@14430
  1103
paulson@14430
  1104
syntax
obua@14738
  1105
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_:_. _)" [0, 51, 10] 10)
paulson@14430
  1106
paulson@14430
  1107
syntax (xsymbols)
obua@14738
  1108
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
kleing@14565
  1109
syntax (HTML output)
obua@14738
  1110
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
paulson@14430
  1111
translations
paulson@14430
  1112
  "\<Prod>i:A. b" == "setprod (%i. b) A"  -- {* Beware of argument permutation! *}
paulson@14430
  1113
paulson@14430
  1114
lemma setprod_empty [simp]: "setprod f {} = 1"
paulson@14430
  1115
  by (auto simp add: setprod_def)
paulson@14430
  1116
paulson@14430
  1117
lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
paulson@14430
  1118
    setprod f (insert a A) = f a * setprod f A"
paulson@14430
  1119
  by (auto simp add: setprod_def LC_def LC.fold_insert
obua@14738
  1120
      mult_left_commute)
paulson@14430
  1121
paulson@14748
  1122
lemma setprod_reindex [rule_format]:
paulson@14748
  1123
     "finite B ==> inj_on f B --> setprod h (f ` B) = setprod (h \<circ> f) B"
nipkow@15111
  1124
by (rule finite_induct, auto)
paulson@14430
  1125
paulson@14485
  1126
lemma setprod_reindex_id: "finite B ==> inj_on f B ==>
paulson@14485
  1127
    setprod f B = setprod id (f ` B)"
paulson@14485
  1128
by (auto simp add: setprod_reindex id_o)
paulson@14430
  1129
wenzelm@14661
  1130
lemma setprod_reindex_cong: "finite A ==> inj_on f A ==>
paulson@14485
  1131
    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
paulson@14485
  1132
  by (frule setprod_reindex, assumption, simp)
paulson@14430
  1133
paulson@14430
  1134
lemma setprod_cong:
paulson@14430
  1135
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
paulson@14430
  1136
  apply (case_tac "finite B")
paulson@14430
  1137
   prefer 2 apply (simp add: setprod_def, simp)
paulson@14430
  1138
  apply (subgoal_tac "ALL C. C <= B --> (ALL x:C. f x = g x) --> setprod f C = setprod g C")
paulson@14430
  1139
   apply simp
paulson@14430
  1140
  apply (erule finite_induct, simp)
paulson@14430
  1141
  apply (simp add: subset_insert_iff, clarify)
paulson@14430
  1142
  apply (subgoal_tac "finite C")
paulson@14430
  1143
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
paulson@14430
  1144
  apply (subgoal_tac "C = insert x (C - {x})")
paulson@14430
  1145
   prefer 2 apply blast
paulson@14430
  1146
  apply (erule ssubst)
paulson@14430
  1147
  apply (drule spec)
paulson@14430
  1148
  apply (erule (1) notE impE)
paulson@14430
  1149
  apply (simp add: Ball_def del:insert_Diff_single)
paulson@14430
  1150
  done
paulson@14430
  1151
paulson@14485
  1152
lemma setprod_1: "setprod (%i. 1) A = 1"
paulson@14485
  1153
  apply (case_tac "finite A")
obua@14738
  1154
  apply (erule finite_induct, auto simp add: mult_ac)
paulson@14485
  1155
  apply (simp add: setprod_def)
paulson@14485
  1156
  done
paulson@14485
  1157
paulson@14430
  1158
lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
paulson@14430
  1159
  apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
paulson@14430
  1160
  apply (erule ssubst, rule setprod_1)
paulson@14430
  1161
  apply (rule setprod_cong, auto)
paulson@14430
  1162
  done
paulson@14430
  1163
paulson@14485
  1164
lemma setprod_Un_Int: "finite A ==> finite B
paulson@14485
  1165
    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
paulson@14485
  1166
  apply (induct set: Finites, simp)
obua@14738
  1167
  apply (simp add: mult_ac insert_absorb)
obua@14738
  1168
  apply (simp add: mult_ac Int_insert_left insert_absorb)
paulson@14485
  1169
  done
paulson@14430
  1170
paulson@14485
  1171
lemma setprod_Un_disjoint: "finite A ==> finite B
paulson@14485
  1172
  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
obua@14738
  1173
  apply (subst setprod_Un_Int [symmetric], auto simp add: mult_ac)
paulson@14485
  1174
  done
paulson@14485
  1175
paulson@14485
  1176
lemma setprod_UN_disjoint:
paulson@14485
  1177
    "finite I ==> (ALL i:I. finite (A i)) ==>
paulson@14485
  1178
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
paulson@14485
  1179
      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
paulson@14485
  1180
  apply (induct set: Finites, simp, atomize)
paulson@14485
  1181
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
paulson@14485
  1182
   prefer 2 apply blast
paulson@14485
  1183
  apply (subgoal_tac "A x Int UNION F A = {}")
paulson@14485
  1184
   prefer 2 apply blast
paulson@14485
  1185
  apply (simp add: setprod_Un_disjoint)
paulson@14430
  1186
  done
paulson@14430
  1187
paulson@14485
  1188
lemma setprod_Union_disjoint:
paulson@14485
  1189
  "finite C ==> (ALL A:C. finite A) ==>
paulson@14485
  1190
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
paulson@14485
  1191
      setprod f (Union C) = setprod (setprod f) C"
paulson@14485
  1192
  apply (frule setprod_UN_disjoint [of C id f])
paulson@14485
  1193
  apply (unfold Union_def id_def, assumption+)
paulson@14485
  1194
  done
paulson@14430
  1195
wenzelm@14661
  1196
lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
wenzelm@14661
  1197
    (\<Prod>x:A. (\<Prod>y: B x. f x y)) =
wenzelm@14661
  1198
    (\<Prod>z:(SIGMA x:A. B x). f (fst z) (snd z))"
paulson@14485
  1199
  apply (subst Sigma_def)
paulson@14485
  1200
  apply (subst setprod_UN_disjoint)
paulson@14485
  1201
  apply assumption
paulson@14485
  1202
  apply (rule ballI)
paulson@14485
  1203
  apply (drule_tac x = i in bspec, assumption)
wenzelm@14661
  1204
  apply (subgoal_tac "(UN y:(B i). {(i, y)}) <= (%y. (i, y)) ` (B i)")
paulson@14485
  1205
  apply (rule finite_surj)
paulson@14485
  1206
  apply auto
paulson@14485
  1207
  apply (rule setprod_cong, rule refl)
paulson@14485
  1208
  apply (subst setprod_UN_disjoint)
paulson@14485
  1209
  apply (erule bspec, assumption)
paulson@14485
  1210
  apply auto
paulson@14485
  1211
  done
paulson@14485
  1212
wenzelm@14661
  1213
lemma setprod_cartesian_product: "finite A ==> finite B ==>
wenzelm@14661
  1214
    (\<Prod>x:A. (\<Prod>y: B. f x y)) =
wenzelm@14661
  1215
    (\<Prod>z:(A <*> B). f (fst z) (snd z))"
paulson@14485
  1216
  by (erule setprod_Sigma, auto)
paulson@14485
  1217
paulson@14485
  1218
lemma setprod_timesf: "setprod (%x. f x * g x) A =
paulson@14485
  1219
    (setprod f A * setprod g A)"
paulson@14485
  1220
  apply (case_tac "finite A")
obua@14738
  1221
   prefer 2 apply (simp add: setprod_def mult_ac)
paulson@14485
  1222
  apply (erule finite_induct, auto)
obua@14738
  1223
  apply (simp add: mult_ac)
paulson@14485
  1224
  done
paulson@14430
  1225
paulson@14430
  1226
subsubsection {* Properties in more restricted classes of structures *}
paulson@14430
  1227
paulson@14430
  1228
lemma setprod_eq_1_iff [simp]:
paulson@14430
  1229
    "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
paulson@14430
  1230
  by (induct set: Finites) auto
paulson@14430
  1231
paulson@15004
  1232
lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::recpower)) = y^(card A)"
paulson@14430
  1233
  apply (erule finite_induct)
paulson@14430
  1234
  apply (auto simp add: power_Suc)
paulson@14430
  1235
  done
paulson@14430
  1236
paulson@15004
  1237
lemma setprod_zero:
paulson@15004
  1238
     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1_cancel) ==> setprod f A = 0"
paulson@14430
  1239
  apply (induct set: Finites, force, clarsimp)
paulson@14430
  1240
  apply (erule disjE, auto)
paulson@14430
  1241
  done
paulson@14430
  1242
paulson@15004
  1243
lemma setprod_nonneg [rule_format]:
paulson@15004
  1244
     "(ALL x: A. (0::'a::ordered_idom) \<le> f x) --> 0 \<le> setprod f A"
paulson@14430
  1245
  apply (case_tac "finite A")
paulson@14430
  1246
  apply (induct set: Finites, force, clarsimp)
paulson@14430
  1247
  apply (subgoal_tac "0 * 0 \<le> f x * setprod f F", force)
paulson@14430
  1248
  apply (rule mult_mono, assumption+)
paulson@14430
  1249
  apply (auto simp add: setprod_def)
paulson@14430
  1250
  done
paulson@14430
  1251
obua@14738
  1252
lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x)
paulson@14430
  1253
     --> 0 < setprod f A"
paulson@14430
  1254
  apply (case_tac "finite A")
paulson@14430
  1255
  apply (induct set: Finites, force, clarsimp)
paulson@14430
  1256
  apply (subgoal_tac "0 * 0 < f x * setprod f F", force)
paulson@14430
  1257
  apply (rule mult_strict_mono, assumption+)
paulson@14430
  1258
  apply (auto simp add: setprod_def)
paulson@14430
  1259
  done
paulson@14430
  1260
paulson@14430
  1261
lemma setprod_nonzero [rule_format]:
obua@14738
  1262
    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
paulson@14430
  1263
      finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
paulson@14430
  1264
  apply (erule finite_induct, auto)
paulson@14430
  1265
  done
paulson@14430
  1266
paulson@14430
  1267
lemma setprod_zero_eq:
obua@14738
  1268
    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
paulson@14430
  1269
     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
paulson@14430
  1270
  apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
paulson@14430
  1271
  done
paulson@14430
  1272
paulson@14430
  1273
lemma setprod_nonzero_field:
paulson@14430
  1274
    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::field)) ==> setprod f A \<noteq> 0"
paulson@14430
  1275
  apply (rule setprod_nonzero, auto)
paulson@14430
  1276
  done
paulson@14430
  1277
paulson@14430
  1278
lemma setprod_zero_eq_field:
paulson@14430
  1279
    "finite A ==> (setprod f A = (0::'a::field)) = (EX x: A. f x = 0)"
paulson@14430
  1280
  apply (rule setprod_zero_eq, auto)
paulson@14430
  1281
  done
paulson@14430
  1282
paulson@14430
  1283
lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
paulson@14430
  1284
    (setprod f (A Un B) :: 'a ::{field})
paulson@14430
  1285
      = setprod f A * setprod f B / setprod f (A Int B)"
paulson@14430
  1286
  apply (subst setprod_Un_Int [symmetric], auto)
paulson@14430
  1287
  apply (subgoal_tac "finite (A Int B)")
paulson@14430
  1288
  apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
paulson@14430
  1289
  apply (subst times_divide_eq_right [THEN sym], auto)
paulson@14430
  1290
  done
paulson@14430
  1291
paulson@14430
  1292
lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
paulson@14430
  1293
    (setprod f (A - {a}) :: 'a :: {field}) =
paulson@14430
  1294
      (if a:A then setprod f A / f a else setprod f A)"
paulson@14430
  1295
  apply (erule finite_induct)
paulson@14430
  1296
   apply (auto simp add: insert_Diff_if)
paulson@14430
  1297
  apply (subgoal_tac "f a * setprod f F / f a = setprod f F * f a / f a")
paulson@14430
  1298
  apply (erule ssubst)
paulson@14430
  1299
  apply (subst times_divide_eq_right [THEN sym])
paulson@14430
  1300
  apply (auto simp add: mult_ac)
paulson@14430
  1301
  done
paulson@14430
  1302
paulson@14430
  1303
lemma setprod_inversef: "finite A ==>
paulson@14430
  1304
    ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
paulson@14430
  1305
      setprod (inverse \<circ> f) A = inverse (setprod f A)"
paulson@14430
  1306
  apply (erule finite_induct)
paulson@14430
  1307
  apply (simp, simp)
paulson@14430
  1308
  done
paulson@14430
  1309
paulson@14430
  1310
lemma setprod_dividef:
paulson@14430
  1311
     "[|finite A;
paulson@14430
  1312
        \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
paulson@14430
  1313
      ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
paulson@14430
  1314
  apply (subgoal_tac
paulson@14430
  1315
         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
paulson@14430
  1316
  apply (erule ssubst)
paulson@14430
  1317
  apply (subst divide_inverse)
paulson@14430
  1318
  apply (subst setprod_timesf)
paulson@14430
  1319
  apply (subst setprod_inversef, assumption+, rule refl)
paulson@14430
  1320
  apply (rule setprod_cong, rule refl)
paulson@14430
  1321
  apply (subst divide_inverse, auto)
paulson@14430
  1322
  done
paulson@14430
  1323
paulson@14430
  1324
paulson@14430
  1325
subsection{* Min and Max of finite linearly ordered sets *}
nipkow@13490
  1326
nipkow@13490
  1327
text{* Seemed easier to define directly than via fold. *}
nipkow@13490
  1328
nipkow@13490
  1329
lemma ex_Max: fixes S :: "('a::linorder)set"
paulson@14430
  1330
  assumes fin: "finite S" shows "S \<noteq> {} ==> \<exists>m\<in>S. \<forall>s \<in> S. s \<le> m"
nipkow@13490
  1331
using fin
nipkow@13490
  1332
proof (induct)
nipkow@13490
  1333
  case empty thus ?case by simp
nipkow@13490
  1334
next
nipkow@13490
  1335
  case (insert S x)
nipkow@13490
  1336
  show ?case
nipkow@13490
  1337
  proof (cases)
nipkow@13490
  1338
    assume "S = {}" thus ?thesis by simp
nipkow@13490
  1339
  next
nipkow@13490
  1340
    assume nonempty: "S \<noteq> {}"
nipkow@13490
  1341
    then obtain m where m: "m\<in>S" "\<forall>s\<in>S. s \<le> m" using insert by blast
nipkow@13490
  1342
    show ?thesis
nipkow@13490
  1343
    proof (cases)
nipkow@13490
  1344
      assume "x \<le> m" thus ?thesis using m by blast
nipkow@13490
  1345
    next
paulson@14430
  1346
      assume "~ x \<le> m" thus ?thesis using m
wenzelm@14661
  1347
        by(simp add:linorder_not_le order_less_le)(blast intro: order_trans)
nipkow@13490
  1348
    qed
nipkow@13490
  1349
  qed
nipkow@13490
  1350
qed
nipkow@13490
  1351
nipkow@13490
  1352
lemma ex_Min: fixes S :: "('a::linorder)set"
paulson@14430
  1353
  assumes fin: "finite S" shows "S \<noteq> {} ==> \<exists>m\<in>S. \<forall>s \<in> S. m \<le> s"
nipkow@13490
  1354
using fin
nipkow@13490
  1355
proof (induct)
nipkow@13490
  1356
  case empty thus ?case by simp
nipkow@13490
  1357
next
nipkow@13490
  1358
  case (insert S x)
nipkow@13490
  1359
  show ?case
nipkow@13490
  1360
  proof (cases)
nipkow@13490
  1361
    assume "S = {}" thus ?thesis by simp
nipkow@13490
  1362
  next
nipkow@13490
  1363
    assume nonempty: "S \<noteq> {}"
nipkow@13490
  1364
    then obtain m where m: "m\<in>S" "\<forall>s\<in>S. m \<le> s" using insert by blast
nipkow@13490
  1365
    show ?thesis
nipkow@13490
  1366
    proof (cases)
nipkow@13490
  1367
      assume "m \<le> x" thus ?thesis using m by blast
nipkow@13490
  1368
    next
paulson@14430
  1369
      assume "~ m \<le> x" thus ?thesis using m
wenzelm@14661
  1370
        by(simp add:linorder_not_le order_less_le)(blast intro: order_trans)
nipkow@13490
  1371
    qed
nipkow@13490
  1372
  qed
nipkow@13490
  1373
qed
nipkow@13490
  1374
nipkow@13490
  1375
constdefs
wenzelm@14661
  1376
  Min :: "('a::linorder)set => 'a"
wenzelm@14661
  1377
  "Min S  ==  THE m. m \<in> S \<and> (\<forall>s \<in> S. m \<le> s)"
nipkow@13490
  1378
wenzelm@14661
  1379
  Max :: "('a::linorder)set => 'a"
wenzelm@14661
  1380
  "Max S  ==  THE m. m \<in> S \<and> (\<forall>s \<in> S. s \<le> m)"
nipkow@13490
  1381
wenzelm@14661
  1382
lemma Min [simp]:
wenzelm@14661
  1383
  assumes a: "finite S"  "S \<noteq> {}"
wenzelm@14661
  1384
  shows "Min S \<in> S \<and> (\<forall>s \<in> S. Min S \<le> s)" (is "?P(Min S)")
nipkow@13490
  1385
proof (unfold Min_def, rule theI')
nipkow@13490
  1386
  show "\<exists>!m. ?P m"
nipkow@13490
  1387
  proof (rule ex_ex1I)
nipkow@13490
  1388
    show "\<exists>m. ?P m" using ex_Min[OF a] by blast
nipkow@13490
  1389
  next
wenzelm@14661
  1390
    fix m1 m2 assume "?P m1" and "?P m2"
wenzelm@14661
  1391
    thus "m1 = m2" by (blast dest: order_antisym)
nipkow@13490
  1392
  qed
nipkow@13490
  1393
qed
nipkow@13490
  1394
wenzelm@14661
  1395
lemma Max [simp]:
wenzelm@14661
  1396
  assumes a: "finite S"  "S \<noteq> {}"
wenzelm@14661
  1397
  shows "Max S \<in> S \<and> (\<forall>s \<in> S. s \<le> Max S)" (is "?P(Max S)")
nipkow@13490
  1398
proof (unfold Max_def, rule theI')
nipkow@13490
  1399
  show "\<exists>!m. ?P m"
nipkow@13490
  1400
  proof (rule ex_ex1I)
nipkow@13490
  1401
    show "\<exists>m. ?P m" using ex_Max[OF a] by blast
nipkow@13490
  1402
  next
nipkow@13490
  1403
    fix m1 m2 assume "?P m1" "?P m2"
wenzelm@14661
  1404
    thus "m1 = m2" by (blast dest: order_antisym)
nipkow@13490
  1405
  qed
nipkow@13490
  1406
qed
nipkow@13490
  1407
wenzelm@14661
  1408
paulson@14430
  1409
subsection {* Theorems about @{text "choose"} *}
wenzelm@12396
  1410
wenzelm@12396
  1411
text {*
wenzelm@12396
  1412
  \medskip Basic theorem about @{text "choose"}.  By Florian
wenzelm@14661
  1413
  Kamm\"uller, tidied by LCP.
wenzelm@12396
  1414
*}
wenzelm@12396
  1415
wenzelm@12396
  1416
lemma card_s_0_eq_empty:
wenzelm@12396
  1417
    "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
wenzelm@12396
  1418
  apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
wenzelm@12396
  1419
  apply (simp cong add: rev_conj_cong)
wenzelm@12396
  1420
  done
wenzelm@12396
  1421
wenzelm@12396
  1422
lemma choose_deconstruct: "finite M ==> x \<notin> M
wenzelm@12396
  1423
  ==> {s. s <= insert x M & card(s) = Suc k}
wenzelm@12396
  1424
       = {s. s <= M & card(s) = Suc k} Un
wenzelm@12396
  1425
         {s. EX t. t <= M & card(t) = k & s = insert x t}"
wenzelm@12396
  1426
  apply safe
wenzelm@12396
  1427
   apply (auto intro: finite_subset [THEN card_insert_disjoint])
wenzelm@12396
  1428
  apply (drule_tac x = "xa - {x}" in spec)
paulson@14430
  1429
  apply (subgoal_tac "x \<notin> xa", auto)
wenzelm@12396
  1430
  apply (erule rev_mp, subst card_Diff_singleton)
wenzelm@12396
  1431
  apply (auto intro: finite_subset)
wenzelm@12396
  1432
  done
wenzelm@12396
  1433
wenzelm@12396
  1434
lemma card_inj_on_le:
paulson@14748
  1435
    "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
paulson@14748
  1436
apply (subgoal_tac "finite A") 
paulson@14748
  1437
 apply (force intro: card_mono simp add: card_image [symmetric])
paulson@14944
  1438
apply (blast intro: finite_imageD dest: finite_subset) 
paulson@14748
  1439
done
wenzelm@12396
  1440
paulson@14430
  1441
lemma card_bij_eq:
paulson@14430
  1442
    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
paulson@13595
  1443
       finite A; finite B |] ==> card A = card B"
wenzelm@12396
  1444
  by (auto intro: le_anti_sym card_inj_on_le)
wenzelm@12396
  1445
paulson@13595
  1446
text{*There are as many subsets of @{term A} having cardinality @{term k}
paulson@13595
  1447
 as there are sets obtained from the former by inserting a fixed element
paulson@13595
  1448
 @{term x} into each.*}
paulson@13595
  1449
lemma constr_bij:
paulson@13595
  1450
   "[|finite A; x \<notin> A|] ==>
paulson@13595
  1451
    card {B. EX C. C <= A & card(C) = k & B = insert x C} =
wenzelm@12396
  1452
    card {B. B <= A & card(B) = k}"
wenzelm@12396
  1453
  apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
paulson@13595
  1454
       apply (auto elim!: equalityE simp add: inj_on_def)
paulson@13595
  1455
    apply (subst Diff_insert0, auto)
paulson@13595
  1456
   txt {* finiteness of the two sets *}
paulson@13595
  1457
   apply (rule_tac [2] B = "Pow (A)" in finite_subset)
paulson@13595
  1458
   apply (rule_tac B = "Pow (insert x A)" in finite_subset)
paulson@13595
  1459
   apply fast+
wenzelm@12396
  1460
  done
wenzelm@12396
  1461
wenzelm@12396
  1462
text {*
wenzelm@12396
  1463
  Main theorem: combinatorial statement about number of subsets of a set.
wenzelm@12396
  1464
*}
wenzelm@12396
  1465
wenzelm@12396
  1466
lemma n_sub_lemma:
wenzelm@12396
  1467
  "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
wenzelm@12396
  1468
  apply (induct k)
paulson@14208
  1469
   apply (simp add: card_s_0_eq_empty, atomize)
wenzelm@12396
  1470
  apply (rotate_tac -1, erule finite_induct)
wenzelm@13421
  1471
   apply (simp_all (no_asm_simp) cong add: conj_cong
wenzelm@13421
  1472
     add: card_s_0_eq_empty choose_deconstruct)
wenzelm@12396
  1473
  apply (subst card_Un_disjoint)
wenzelm@12396
  1474
     prefer 4 apply (force simp add: constr_bij)
wenzelm@12396
  1475
    prefer 3 apply force
wenzelm@12396
  1476
   prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
wenzelm@12396
  1477
     finite_subset [of _ "Pow (insert x F)", standard])
wenzelm@12396
  1478
  apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
wenzelm@12396
  1479
  done
wenzelm@12396
  1480
wenzelm@13421
  1481
theorem n_subsets:
wenzelm@13421
  1482
    "finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
wenzelm@12396
  1483
  by (simp add: n_sub_lemma)
wenzelm@12396
  1484
wenzelm@12396
  1485
end