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