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