src/HOL/Finite_Set.thy
author paulson
Thu Feb 20 11:10:24 2003 +0100 (2003-02-20)
changeset 13825 ef4c41e7956a
parent 13737 e564c3d2d174
child 14208 144f45277d5a
permissions -rw-r--r--
new inverse image lemmas
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
wenzelm@12396
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@12396
     5
*)
wenzelm@12396
     6
wenzelm@12396
     7
header {* Finite sets *}
wenzelm@12396
     8
wenzelm@12396
     9
theory Finite_Set = Divides + Power + Inductive + SetInterval:
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"
nipkow@13737
    28
 "\<lbrakk> ~finite(UNIV::'a set); finite A \<rbrakk> \<Longrightarrow> \<exists>a::'a. a ~: A"
nipkow@13737
    29
by(subgoal_tac "A ~= UNIV", blast, blast)
nipkow@13737
    30
wenzelm@12396
    31
wenzelm@12396
    32
lemma finite_induct [case_names empty insert, induct set: Finites]:
wenzelm@12396
    33
  "finite F ==>
wenzelm@12396
    34
    P {} ==> (!!F x. finite F ==> x \<notin> F ==> P F ==> P (insert x F)) ==> P F"
wenzelm@12396
    35
  -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
wenzelm@12396
    36
proof -
wenzelm@13421
    37
  assume "P {}" and
wenzelm@13421
    38
    insert: "!!F x. finite F ==> x \<notin> F ==> P F ==> P (insert x F)"
wenzelm@12396
    39
  assume "finite F"
wenzelm@12396
    40
  thus "P F"
wenzelm@12396
    41
  proof induct
wenzelm@12396
    42
    show "P {}" .
wenzelm@12396
    43
    fix F x assume F: "finite F" and P: "P F"
wenzelm@12396
    44
    show "P (insert x F)"
wenzelm@12396
    45
    proof cases
wenzelm@12396
    46
      assume "x \<in> F"
wenzelm@12396
    47
      hence "insert x F = F" by (rule insert_absorb)
wenzelm@12396
    48
      with P show ?thesis by (simp only:)
wenzelm@12396
    49
    next
wenzelm@12396
    50
      assume "x \<notin> F"
wenzelm@12396
    51
      from F this P show ?thesis by (rule insert)
wenzelm@12396
    52
    qed
wenzelm@12396
    53
  qed
wenzelm@12396
    54
qed
wenzelm@12396
    55
wenzelm@12396
    56
lemma finite_subset_induct [consumes 2, case_names empty insert]:
wenzelm@12396
    57
  "finite F ==> F \<subseteq> A ==>
wenzelm@12396
    58
    P {} ==> (!!F a. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)) ==>
wenzelm@12396
    59
    P F"
wenzelm@12396
    60
proof -
wenzelm@13421
    61
  assume "P {}" and insert:
wenzelm@13421
    62
    "!!F a. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)"
wenzelm@12396
    63
  assume "finite F"
wenzelm@12396
    64
  thus "F \<subseteq> A ==> P F"
wenzelm@12396
    65
  proof induct
wenzelm@12396
    66
    show "P {}" .
wenzelm@12396
    67
    fix F x assume "finite F" and "x \<notin> F"
wenzelm@12396
    68
      and P: "F \<subseteq> A ==> P F" and i: "insert x F \<subseteq> A"
wenzelm@12396
    69
    show "P (insert x F)"
wenzelm@12396
    70
    proof (rule insert)
wenzelm@12396
    71
      from i show "x \<in> A" by blast
wenzelm@12396
    72
      from i have "F \<subseteq> A" by blast
wenzelm@12396
    73
      with P show "P F" .
wenzelm@12396
    74
    qed
wenzelm@12396
    75
  qed
wenzelm@12396
    76
qed
wenzelm@12396
    77
wenzelm@12396
    78
lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
wenzelm@12396
    79
  -- {* The union of two finite sets is finite. *}
wenzelm@12396
    80
  by (induct set: Finites) simp_all
wenzelm@12396
    81
wenzelm@12396
    82
lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
wenzelm@12396
    83
  -- {* Every subset of a finite set is finite. *}
wenzelm@12396
    84
proof -
wenzelm@12396
    85
  assume "finite B"
wenzelm@12396
    86
  thus "!!A. A \<subseteq> B ==> finite A"
wenzelm@12396
    87
  proof induct
wenzelm@12396
    88
    case empty
wenzelm@12396
    89
    thus ?case by simp
wenzelm@12396
    90
  next
wenzelm@12396
    91
    case (insert F x A)
wenzelm@12396
    92
    have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" .
wenzelm@12396
    93
    show "finite A"
wenzelm@12396
    94
    proof cases
wenzelm@12396
    95
      assume x: "x \<in> A"
wenzelm@12396
    96
      with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
    97
      with r have "finite (A - {x})" .
wenzelm@12396
    98
      hence "finite (insert x (A - {x}))" ..
wenzelm@12396
    99
      also have "insert x (A - {x}) = A" by (rule insert_Diff)
wenzelm@12396
   100
      finally show ?thesis .
wenzelm@12396
   101
    next
wenzelm@12396
   102
      show "A \<subseteq> F ==> ?thesis" .
wenzelm@12396
   103
      assume "x \<notin> A"
wenzelm@12396
   104
      with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   105
    qed
wenzelm@12396
   106
  qed
wenzelm@12396
   107
qed
wenzelm@12396
   108
wenzelm@12396
   109
lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
wenzelm@12396
   110
  by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
wenzelm@12396
   111
wenzelm@12396
   112
lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
wenzelm@12396
   113
  -- {* The converse obviously fails. *}
wenzelm@12396
   114
  by (blast intro: finite_subset)
wenzelm@12396
   115
wenzelm@12396
   116
lemma finite_insert [simp]: "finite (insert a A) = finite A"
wenzelm@12396
   117
  apply (subst insert_is_Un)
wenzelm@12396
   118
  apply (simp only: finite_Un)
wenzelm@12396
   119
  apply blast
wenzelm@12396
   120
  done
wenzelm@12396
   121
wenzelm@12396
   122
lemma finite_empty_induct:
wenzelm@12396
   123
  "finite A ==>
wenzelm@12396
   124
  P A ==> (!!a A. finite A ==> a:A ==> P A ==> P (A - {a})) ==> P {}"
wenzelm@12396
   125
proof -
wenzelm@12396
   126
  assume "finite A"
wenzelm@12396
   127
    and "P A" and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
wenzelm@12396
   128
  have "P (A - A)"
wenzelm@12396
   129
  proof -
wenzelm@12396
   130
    fix c b :: "'a set"
wenzelm@12396
   131
    presume c: "finite c" and b: "finite b"
wenzelm@12396
   132
      and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
wenzelm@12396
   133
    from c show "c \<subseteq> b ==> P (b - c)"
wenzelm@12396
   134
    proof induct
wenzelm@12396
   135
      case empty
wenzelm@12396
   136
      from P1 show ?case by simp
wenzelm@12396
   137
    next
wenzelm@12396
   138
      case (insert F x)
wenzelm@12396
   139
      have "P (b - F - {x})"
wenzelm@12396
   140
      proof (rule P2)
wenzelm@12396
   141
        from _ b show "finite (b - F)" by (rule finite_subset) blast
wenzelm@12396
   142
        from insert show "x \<in> b - F" by simp
wenzelm@12396
   143
        from insert show "P (b - F)" by simp
wenzelm@12396
   144
      qed
wenzelm@12396
   145
      also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
wenzelm@12396
   146
      finally show ?case .
wenzelm@12396
   147
    qed
wenzelm@12396
   148
  next
wenzelm@12396
   149
    show "A \<subseteq> A" ..
wenzelm@12396
   150
  qed
wenzelm@12396
   151
  thus "P {}" by simp
wenzelm@12396
   152
qed
wenzelm@12396
   153
wenzelm@12396
   154
lemma finite_Diff [simp]: "finite B ==> finite (B - Ba)"
wenzelm@12396
   155
  by (rule Diff_subset [THEN finite_subset])
wenzelm@12396
   156
wenzelm@12396
   157
lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
wenzelm@12396
   158
  apply (subst Diff_insert)
wenzelm@12396
   159
  apply (case_tac "a : A - B")
wenzelm@12396
   160
   apply (rule finite_insert [symmetric, THEN trans])
wenzelm@12396
   161
   apply (subst insert_Diff)
wenzelm@12396
   162
    apply simp_all
wenzelm@12396
   163
  done
wenzelm@12396
   164
wenzelm@12396
   165
paulson@13825
   166
subsubsection {* Image and Inverse Image over Finite Sets *}
paulson@13825
   167
paulson@13825
   168
lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
paulson@13825
   169
  -- {* The image of a finite set is finite. *}
paulson@13825
   170
  by (induct set: Finites) simp_all
paulson@13825
   171
paulson@13825
   172
lemma finite_range_imageI:
paulson@13825
   173
    "finite (range g) ==> finite (range (%x. f (g x)))"
paulson@13825
   174
  apply (drule finite_imageI)
paulson@13825
   175
  apply simp
paulson@13825
   176
  done
paulson@13825
   177
wenzelm@12396
   178
lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
wenzelm@12396
   179
proof -
wenzelm@12396
   180
  have aux: "!!A. finite (A - {}) = finite A" by simp
wenzelm@12396
   181
  fix B :: "'a set"
wenzelm@12396
   182
  assume "finite B"
wenzelm@12396
   183
  thus "!!A. f`A = B ==> inj_on f A ==> finite A"
wenzelm@12396
   184
    apply induct
wenzelm@12396
   185
     apply simp
wenzelm@12396
   186
    apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
wenzelm@12396
   187
     apply clarify
wenzelm@12396
   188
     apply (simp (no_asm_use) add: inj_on_def)
wenzelm@12396
   189
     apply (blast dest!: aux [THEN iffD1])
wenzelm@12396
   190
    apply atomize
wenzelm@12396
   191
    apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
wenzelm@12396
   192
    apply (frule subsetD [OF equalityD2 insertI1])
wenzelm@12396
   193
    apply clarify
wenzelm@12396
   194
    apply (rule_tac x = xa in bexI)
wenzelm@12396
   195
     apply (simp_all add: inj_on_image_set_diff)
wenzelm@12396
   196
    done
wenzelm@12396
   197
qed (rule refl)
wenzelm@12396
   198
wenzelm@12396
   199
paulson@13825
   200
lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
paulson@13825
   201
  -- {* The inverse image of a singleton under an injective function
paulson@13825
   202
         is included in a singleton. *}
paulson@13825
   203
  apply (auto simp add: inj_on_def) 
paulson@13825
   204
  apply (blast intro: the_equality [symmetric]) 
paulson@13825
   205
  done
paulson@13825
   206
paulson@13825
   207
lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
paulson@13825
   208
  -- {* The inverse image of a finite set under an injective function
paulson@13825
   209
         is finite. *}
paulson@13825
   210
  apply (induct set: Finites, simp_all) 
paulson@13825
   211
  apply (subst vimage_insert) 
paulson@13825
   212
  apply (simp add: finite_Un finite_subset [OF inj_vimage_singleton]) 
paulson@13825
   213
  done
paulson@13825
   214
paulson@13825
   215
wenzelm@12396
   216
subsubsection {* The finite UNION of finite sets *}
wenzelm@12396
   217
wenzelm@12396
   218
lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)"
wenzelm@12396
   219
  by (induct set: Finites) simp_all
wenzelm@12396
   220
wenzelm@12396
   221
text {*
wenzelm@12396
   222
  Strengthen RHS to
wenzelm@12396
   223
  @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x ~= {}})"}?
wenzelm@12396
   224
wenzelm@12396
   225
  We'd need to prove
wenzelm@12396
   226
  @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x ~= {}}"}
wenzelm@12396
   227
  by induction. *}
wenzelm@12396
   228
wenzelm@12396
   229
lemma finite_UN [simp]: "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
wenzelm@12396
   230
  by (blast intro: finite_UN_I finite_subset)
wenzelm@12396
   231
wenzelm@12396
   232
wenzelm@12396
   233
subsubsection {* Sigma of finite sets *}
wenzelm@12396
   234
wenzelm@12396
   235
lemma finite_SigmaI [simp]:
wenzelm@12396
   236
    "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
wenzelm@12396
   237
  by (unfold Sigma_def) (blast intro!: finite_UN_I)
wenzelm@12396
   238
wenzelm@12396
   239
lemma finite_Prod_UNIV:
wenzelm@12396
   240
    "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
wenzelm@12396
   241
  apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
wenzelm@12396
   242
   apply (erule ssubst)
wenzelm@12396
   243
   apply (erule finite_SigmaI)
wenzelm@12396
   244
   apply auto
wenzelm@12396
   245
  done
wenzelm@12396
   246
wenzelm@12396
   247
instance unit :: finite
wenzelm@12396
   248
proof
wenzelm@12396
   249
  have "finite {()}" by simp
wenzelm@12396
   250
  also have "{()} = UNIV" by auto
wenzelm@12396
   251
  finally show "finite (UNIV :: unit set)" .
wenzelm@12396
   252
qed
wenzelm@12396
   253
wenzelm@12396
   254
instance * :: (finite, finite) finite
wenzelm@12396
   255
proof
wenzelm@12396
   256
  show "finite (UNIV :: ('a \<times> 'b) set)"
wenzelm@12396
   257
  proof (rule finite_Prod_UNIV)
wenzelm@12396
   258
    show "finite (UNIV :: 'a set)" by (rule finite)
wenzelm@12396
   259
    show "finite (UNIV :: 'b set)" by (rule finite)
wenzelm@12396
   260
  qed
wenzelm@12396
   261
qed
wenzelm@12396
   262
wenzelm@12396
   263
wenzelm@12396
   264
subsubsection {* The powerset of a finite set *}
wenzelm@12396
   265
wenzelm@12396
   266
lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
wenzelm@12396
   267
proof
wenzelm@12396
   268
  assume "finite (Pow A)"
wenzelm@12396
   269
  with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
wenzelm@12396
   270
  thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
wenzelm@12396
   271
next
wenzelm@12396
   272
  assume "finite A"
wenzelm@12396
   273
  thus "finite (Pow A)"
wenzelm@12396
   274
    by induct (simp_all add: finite_UnI finite_imageI Pow_insert)
wenzelm@12396
   275
qed
wenzelm@12396
   276
wenzelm@12396
   277
lemma finite_converse [iff]: "finite (r^-1) = finite r"
wenzelm@12396
   278
  apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
wenzelm@12396
   279
   apply simp
wenzelm@12396
   280
   apply (rule iffI)
wenzelm@12396
   281
    apply (erule finite_imageD [unfolded inj_on_def])
wenzelm@12396
   282
    apply (simp split add: split_split)
wenzelm@12396
   283
   apply (erule finite_imageI)
wenzelm@12396
   284
  apply (simp add: converse_def image_def)
wenzelm@12396
   285
  apply auto
wenzelm@12396
   286
  apply (rule bexI)
wenzelm@12396
   287
   prefer 2 apply assumption
wenzelm@12396
   288
  apply simp
wenzelm@12396
   289
  done
wenzelm@12396
   290
wenzelm@12937
   291
lemma finite_lessThan [iff]: fixes k :: nat shows "finite {..k(}"
wenzelm@12396
   292
  by (induct k) (simp_all add: lessThan_Suc)
wenzelm@12396
   293
wenzelm@12937
   294
lemma finite_atMost [iff]: fixes k :: nat shows "finite {..k}"
wenzelm@12396
   295
  by (induct k) (simp_all add: atMost_Suc)
wenzelm@12396
   296
ballarin@13735
   297
lemma finite_greaterThanLessThan [iff]:
ballarin@13735
   298
  fixes l :: nat shows "finite {)l..u(}"
ballarin@13735
   299
by (simp add: greaterThanLessThan_def)
ballarin@13735
   300
ballarin@13735
   301
lemma finite_atLeastLessThan [iff]:
ballarin@13735
   302
  fixes l :: nat shows "finite {l..u(}"
ballarin@13735
   303
by (simp add: atLeastLessThan_def)
ballarin@13735
   304
ballarin@13735
   305
lemma finite_greaterThanAtMost [iff]:
ballarin@13735
   306
  fixes l :: nat shows "finite {)l..u}"
ballarin@13735
   307
by (simp add: greaterThanAtMost_def)
ballarin@13735
   308
ballarin@13735
   309
lemma finite_atLeastAtMost [iff]:
ballarin@13735
   310
  fixes l :: nat shows "finite {l..u}"
ballarin@13735
   311
by (simp add: atLeastAtMost_def)
ballarin@13735
   312
wenzelm@12396
   313
lemma bounded_nat_set_is_finite:
wenzelm@12396
   314
    "(ALL i:N. i < (n::nat)) ==> finite N"
wenzelm@12396
   315
  -- {* A bounded set of natural numbers is finite. *}
wenzelm@12396
   316
  apply (rule finite_subset)
wenzelm@12396
   317
   apply (rule_tac [2] finite_lessThan)
wenzelm@12396
   318
  apply auto
wenzelm@12396
   319
  done
wenzelm@12396
   320
wenzelm@12396
   321
wenzelm@12396
   322
subsubsection {* Finiteness of transitive closure *}
wenzelm@12396
   323
wenzelm@12396
   324
text {* (Thanks to Sidi Ehmety) *}
wenzelm@12396
   325
wenzelm@12396
   326
lemma finite_Field: "finite r ==> finite (Field r)"
wenzelm@12396
   327
  -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
wenzelm@12396
   328
  apply (induct set: Finites)
wenzelm@12396
   329
   apply (auto simp add: Field_def Domain_insert Range_insert)
wenzelm@12396
   330
  done
wenzelm@12396
   331
wenzelm@12396
   332
lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
wenzelm@12396
   333
  apply clarify
wenzelm@12396
   334
  apply (erule trancl_induct)
wenzelm@12396
   335
   apply (auto simp add: Field_def)
wenzelm@12396
   336
  done
wenzelm@12396
   337
wenzelm@12396
   338
lemma finite_trancl: "finite (r^+) = finite r"
wenzelm@12396
   339
  apply auto
wenzelm@12396
   340
   prefer 2
wenzelm@12396
   341
   apply (rule trancl_subset_Field2 [THEN finite_subset])
wenzelm@12396
   342
   apply (rule finite_SigmaI)
wenzelm@12396
   343
    prefer 3
berghofe@13704
   344
    apply (blast intro: r_into_trancl' finite_subset)
wenzelm@12396
   345
   apply (auto simp add: finite_Field)
wenzelm@12396
   346
  done
wenzelm@12396
   347
wenzelm@12396
   348
wenzelm@12396
   349
subsection {* Finite cardinality *}
wenzelm@12396
   350
wenzelm@12396
   351
text {*
wenzelm@12396
   352
  This definition, although traditional, is ugly to work with: @{text
wenzelm@12396
   353
  "card A == LEAST n. EX f. A = {f i | i. i < n}"}.  Therefore we have
wenzelm@12396
   354
  switched to an inductive one:
wenzelm@12396
   355
*}
wenzelm@12396
   356
wenzelm@12396
   357
consts cardR :: "('a set \<times> nat) set"
wenzelm@12396
   358
wenzelm@12396
   359
inductive cardR
wenzelm@12396
   360
  intros
wenzelm@12396
   361
    EmptyI: "({}, 0) : cardR"
wenzelm@12396
   362
    InsertI: "(A, n) : cardR ==> a \<notin> A ==> (insert a A, Suc n) : cardR"
wenzelm@12396
   363
wenzelm@12396
   364
constdefs
wenzelm@12396
   365
  card :: "'a set => nat"
wenzelm@12396
   366
  "card A == THE n. (A, n) : cardR"
wenzelm@12396
   367
wenzelm@12396
   368
inductive_cases cardR_emptyE: "({}, n) : cardR"
wenzelm@12396
   369
inductive_cases cardR_insertE: "(insert a A,n) : cardR"
wenzelm@12396
   370
wenzelm@12396
   371
lemma cardR_SucD: "(A, n) : cardR ==> a : A --> (EX m. n = Suc m)"
wenzelm@12396
   372
  by (induct set: cardR) simp_all
wenzelm@12396
   373
wenzelm@12396
   374
lemma cardR_determ_aux1:
wenzelm@12396
   375
    "(A, m): cardR ==> (!!n a. m = Suc n ==> a:A ==> (A - {a}, n) : cardR)"
wenzelm@12396
   376
  apply (induct set: cardR)
wenzelm@12396
   377
   apply auto
wenzelm@12396
   378
  apply (simp add: insert_Diff_if)
wenzelm@12396
   379
  apply auto
wenzelm@12396
   380
  apply (drule cardR_SucD)
wenzelm@12396
   381
  apply (blast intro!: cardR.intros)
wenzelm@12396
   382
  done
wenzelm@12396
   383
wenzelm@12396
   384
lemma cardR_determ_aux2: "(insert a A, Suc m) : cardR ==> a \<notin> A ==> (A, m) : cardR"
wenzelm@12396
   385
  by (drule cardR_determ_aux1) auto
wenzelm@12396
   386
wenzelm@12396
   387
lemma cardR_determ: "(A, m): cardR ==> (!!n. (A, n) : cardR ==> n = m)"
wenzelm@12396
   388
  apply (induct set: cardR)
wenzelm@12396
   389
   apply (safe elim!: cardR_emptyE cardR_insertE)
wenzelm@12396
   390
  apply (rename_tac B b m)
wenzelm@12396
   391
  apply (case_tac "a = b")
wenzelm@12396
   392
   apply (subgoal_tac "A = B")
wenzelm@12396
   393
    prefer 2 apply (blast elim: equalityE)
wenzelm@12396
   394
   apply blast
wenzelm@12396
   395
  apply (subgoal_tac "EX C. A = insert b C & B = insert a C")
wenzelm@12396
   396
   prefer 2
wenzelm@12396
   397
   apply (rule_tac x = "A Int B" in exI)
wenzelm@12396
   398
   apply (blast elim: equalityE)
wenzelm@12396
   399
  apply (frule_tac A = B in cardR_SucD)
wenzelm@12396
   400
  apply (blast intro!: cardR.intros dest!: cardR_determ_aux2)
wenzelm@12396
   401
  done
wenzelm@12396
   402
wenzelm@12396
   403
lemma cardR_imp_finite: "(A, n) : cardR ==> finite A"
wenzelm@12396
   404
  by (induct set: cardR) simp_all
wenzelm@12396
   405
wenzelm@12396
   406
lemma finite_imp_cardR: "finite A ==> EX n. (A, n) : cardR"
wenzelm@12396
   407
  by (induct set: Finites) (auto intro!: cardR.intros)
wenzelm@12396
   408
wenzelm@12396
   409
lemma card_equality: "(A,n) : cardR ==> card A = n"
wenzelm@12396
   410
  by (unfold card_def) (blast intro: cardR_determ)
wenzelm@12396
   411
wenzelm@12396
   412
lemma card_empty [simp]: "card {} = 0"
wenzelm@12396
   413
  by (unfold card_def) (blast intro!: cardR.intros elim!: cardR_emptyE)
wenzelm@12396
   414
wenzelm@12396
   415
lemma card_insert_disjoint [simp]:
wenzelm@12396
   416
  "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
wenzelm@12396
   417
proof -
wenzelm@12396
   418
  assume x: "x \<notin> A"
wenzelm@12396
   419
  hence aux: "!!n. ((insert x A, n) : cardR) = (EX m. (A, m) : cardR & n = Suc m)"
wenzelm@12396
   420
    apply (auto intro!: cardR.intros)
wenzelm@12396
   421
    apply (rule_tac A1 = A in finite_imp_cardR [THEN exE])
wenzelm@12396
   422
     apply (force dest: cardR_imp_finite)
wenzelm@12396
   423
    apply (blast intro!: cardR.intros intro: cardR_determ)
wenzelm@12396
   424
    done
wenzelm@12396
   425
  assume "finite A"
wenzelm@12396
   426
  thus ?thesis
wenzelm@12396
   427
    apply (simp add: card_def aux)
wenzelm@12396
   428
    apply (rule the_equality)
wenzelm@12396
   429
     apply (auto intro: finite_imp_cardR
wenzelm@12396
   430
       cong: conj_cong simp: card_def [symmetric] card_equality)
wenzelm@12396
   431
    done
wenzelm@12396
   432
qed
wenzelm@12396
   433
wenzelm@12396
   434
lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})"
wenzelm@12396
   435
  apply auto
wenzelm@12396
   436
  apply (drule_tac a = x in mk_disjoint_insert)
wenzelm@12396
   437
  apply clarify
wenzelm@12396
   438
  apply (rotate_tac -1)
wenzelm@12396
   439
  apply auto
wenzelm@12396
   440
  done
wenzelm@12396
   441
wenzelm@12396
   442
lemma card_insert_if:
wenzelm@12396
   443
    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
wenzelm@12396
   444
  by (simp add: insert_absorb)
wenzelm@12396
   445
wenzelm@12396
   446
lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
wenzelm@12396
   447
  apply (rule_tac t = A in insert_Diff [THEN subst])
wenzelm@12396
   448
   apply assumption
wenzelm@12396
   449
  apply simp
wenzelm@12396
   450
  done
wenzelm@12396
   451
wenzelm@12396
   452
lemma card_Diff_singleton:
wenzelm@12396
   453
    "finite A ==> x: A ==> card (A - {x}) = card A - 1"
wenzelm@12396
   454
  by (simp add: card_Suc_Diff1 [symmetric])
wenzelm@12396
   455
wenzelm@12396
   456
lemma card_Diff_singleton_if:
wenzelm@12396
   457
    "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)"
wenzelm@12396
   458
  by (simp add: card_Diff_singleton)
wenzelm@12396
   459
wenzelm@12396
   460
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
wenzelm@12396
   461
  by (simp add: card_insert_if card_Suc_Diff1)
wenzelm@12396
   462
wenzelm@12396
   463
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
wenzelm@12396
   464
  by (simp add: card_insert_if)
wenzelm@12396
   465
wenzelm@12396
   466
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
wenzelm@12396
   467
  apply (induct set: Finites)
wenzelm@12396
   468
   apply simp
wenzelm@12396
   469
  apply clarify
wenzelm@12396
   470
  apply (subgoal_tac "finite A & A - {x} <= F")
wenzelm@12396
   471
   prefer 2 apply (blast intro: finite_subset)
wenzelm@12396
   472
  apply atomize
wenzelm@12396
   473
  apply (drule_tac x = "A - {x}" in spec)
wenzelm@12396
   474
  apply (simp add: card_Diff_singleton_if split add: split_if_asm)
wenzelm@12396
   475
  apply (case_tac "card A")
wenzelm@12396
   476
   apply auto
wenzelm@12396
   477
  done
wenzelm@12396
   478
wenzelm@12396
   479
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
wenzelm@12396
   480
  apply (simp add: psubset_def linorder_not_le [symmetric])
wenzelm@12396
   481
  apply (blast dest: card_seteq)
wenzelm@12396
   482
  done
wenzelm@12396
   483
wenzelm@12396
   484
lemma card_mono: "finite B ==> A <= B ==> card A <= card B"
wenzelm@12396
   485
  apply (case_tac "A = B")
wenzelm@12396
   486
   apply simp
wenzelm@12396
   487
  apply (simp add: linorder_not_less [symmetric])
wenzelm@12396
   488
  apply (blast dest: card_seteq intro: order_less_imp_le)
wenzelm@12396
   489
  done
wenzelm@12396
   490
wenzelm@12396
   491
lemma card_Un_Int: "finite A ==> finite B
wenzelm@12396
   492
    ==> card A + card B = card (A Un B) + card (A Int B)"
wenzelm@12396
   493
  apply (induct set: Finites)
wenzelm@12396
   494
   apply simp
wenzelm@12396
   495
  apply (simp add: insert_absorb Int_insert_left)
wenzelm@12396
   496
  done
wenzelm@12396
   497
wenzelm@12396
   498
lemma card_Un_disjoint: "finite A ==> finite B
wenzelm@12396
   499
    ==> A Int B = {} ==> card (A Un B) = card A + card B"
wenzelm@12396
   500
  by (simp add: card_Un_Int)
wenzelm@12396
   501
wenzelm@12396
   502
lemma card_Diff_subset:
wenzelm@12396
   503
    "finite A ==> B <= A ==> card A - card B = card (A - B)"
wenzelm@12396
   504
  apply (subgoal_tac "(A - B) Un B = A")
wenzelm@12396
   505
   prefer 2 apply blast
wenzelm@12396
   506
  apply (rule add_right_cancel [THEN iffD1])
wenzelm@12396
   507
  apply (rule card_Un_disjoint [THEN subst])
wenzelm@12396
   508
     apply (erule_tac [4] ssubst)
wenzelm@12396
   509
     prefer 3 apply blast
wenzelm@12396
   510
    apply (simp_all add: add_commute not_less_iff_le
wenzelm@12396
   511
      add_diff_inverse card_mono finite_subset)
wenzelm@12396
   512
  done
wenzelm@12396
   513
wenzelm@12396
   514
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
wenzelm@12396
   515
  apply (rule Suc_less_SucD)
wenzelm@12396
   516
  apply (simp add: card_Suc_Diff1)
wenzelm@12396
   517
  done
wenzelm@12396
   518
wenzelm@12396
   519
lemma card_Diff2_less:
wenzelm@12396
   520
    "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
wenzelm@12396
   521
  apply (case_tac "x = y")
wenzelm@12396
   522
   apply (simp add: card_Diff1_less)
wenzelm@12396
   523
  apply (rule less_trans)
wenzelm@12396
   524
   prefer 2 apply (auto intro!: card_Diff1_less)
wenzelm@12396
   525
  done
wenzelm@12396
   526
wenzelm@12396
   527
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
wenzelm@12396
   528
  apply (case_tac "x : A")
wenzelm@12396
   529
   apply (simp_all add: card_Diff1_less less_imp_le)
wenzelm@12396
   530
  done
wenzelm@12396
   531
wenzelm@12396
   532
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
wenzelm@12396
   533
  apply (erule psubsetI)
wenzelm@12396
   534
  apply blast
wenzelm@12396
   535
  done
wenzelm@12396
   536
wenzelm@12396
   537
wenzelm@12396
   538
subsubsection {* Cardinality of image *}
wenzelm@12396
   539
wenzelm@12396
   540
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
wenzelm@12396
   541
  apply (induct set: Finites)
wenzelm@12396
   542
   apply simp
wenzelm@12396
   543
  apply (simp add: le_SucI finite_imageI card_insert_if)
wenzelm@12396
   544
  done
wenzelm@12396
   545
wenzelm@12396
   546
lemma card_image: "finite A ==> inj_on f A ==> card (f ` A) = card A"
wenzelm@12396
   547
  apply (induct set: Finites)
wenzelm@12396
   548
   apply simp_all
wenzelm@12396
   549
  apply atomize
wenzelm@12396
   550
  apply safe
wenzelm@12396
   551
   apply (unfold inj_on_def)
wenzelm@12396
   552
   apply blast
wenzelm@12396
   553
  apply (subst card_insert_disjoint)
wenzelm@12396
   554
    apply (erule finite_imageI)
wenzelm@12396
   555
   apply blast
wenzelm@12396
   556
  apply blast
wenzelm@12396
   557
  done
wenzelm@12396
   558
wenzelm@12396
   559
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
wenzelm@12396
   560
  by (simp add: card_seteq card_image)
wenzelm@12396
   561
wenzelm@12396
   562
wenzelm@12396
   563
subsubsection {* Cardinality of the Powerset *}
wenzelm@12396
   564
wenzelm@12396
   565
lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
wenzelm@12396
   566
  apply (induct set: Finites)
wenzelm@12396
   567
   apply (simp_all add: Pow_insert)
wenzelm@12396
   568
  apply (subst card_Un_disjoint)
wenzelm@12396
   569
     apply blast
wenzelm@12396
   570
    apply (blast intro: finite_imageI)
wenzelm@12396
   571
   apply blast
wenzelm@12396
   572
  apply (subgoal_tac "inj_on (insert x) (Pow F)")
wenzelm@12396
   573
   apply (simp add: card_image Pow_insert)
wenzelm@12396
   574
  apply (unfold inj_on_def)
wenzelm@12396
   575
  apply (blast elim!: equalityE)
wenzelm@12396
   576
  done
wenzelm@12396
   577
wenzelm@12396
   578
text {*
wenzelm@12396
   579
  \medskip Relates to equivalence classes.  Based on a theorem of
wenzelm@12396
   580
  F. Kammüller's.  The @{prop "finite C"} premise is redundant.
wenzelm@12396
   581
*}
wenzelm@12396
   582
wenzelm@12396
   583
lemma dvd_partition:
wenzelm@12396
   584
  "finite C ==> finite (Union C) ==>
wenzelm@12396
   585
    ALL c : C. k dvd card c ==>
wenzelm@12396
   586
    (ALL c1: C. ALL c2: C. c1 ~= c2 --> c1 Int c2 = {}) ==>
wenzelm@12396
   587
  k dvd card (Union C)"
wenzelm@12396
   588
  apply (induct set: Finites)
wenzelm@12396
   589
   apply simp_all
wenzelm@12396
   590
  apply clarify
wenzelm@12396
   591
  apply (subst card_Un_disjoint)
wenzelm@12396
   592
  apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
wenzelm@12396
   593
  done
wenzelm@12396
   594
wenzelm@12396
   595
wenzelm@12396
   596
subsection {* A fold functional for finite sets *}
wenzelm@12396
   597
wenzelm@12396
   598
text {*
wenzelm@12396
   599
  For @{text n} non-negative we have @{text "fold f e {x1, ..., xn} =
wenzelm@12396
   600
  f x1 (... (f xn e))"} where @{text f} is at least left-commutative.
wenzelm@12396
   601
*}
wenzelm@12396
   602
wenzelm@12396
   603
consts
wenzelm@12396
   604
  foldSet :: "('b => 'a => 'a) => 'a => ('b set \<times> 'a) set"
wenzelm@12396
   605
wenzelm@12396
   606
inductive "foldSet f e"
wenzelm@12396
   607
  intros
wenzelm@12396
   608
    emptyI [intro]: "({}, e) : foldSet f e"
wenzelm@12396
   609
    insertI [intro]: "x \<notin> A ==> (A, y) : foldSet f e ==> (insert x A, f x y) : foldSet f e"
wenzelm@12396
   610
wenzelm@12396
   611
inductive_cases empty_foldSetE [elim!]: "({}, x) : foldSet f e"
wenzelm@12396
   612
wenzelm@12396
   613
constdefs
wenzelm@12396
   614
  fold :: "('b => 'a => 'a) => 'a => 'b set => 'a"
wenzelm@12396
   615
  "fold f e A == THE x. (A, x) : foldSet f e"
wenzelm@12396
   616
wenzelm@12396
   617
lemma Diff1_foldSet: "(A - {x}, y) : foldSet f e ==> x: A ==> (A, f x y) : foldSet f e"
wenzelm@12396
   618
  apply (erule insert_Diff [THEN subst], rule foldSet.intros)
wenzelm@12396
   619
   apply auto
wenzelm@12396
   620
  done
wenzelm@12396
   621
wenzelm@12396
   622
lemma foldSet_imp_finite [simp]: "(A, x) : foldSet f e ==> finite A"
wenzelm@12396
   623
  by (induct set: foldSet) auto
wenzelm@12396
   624
wenzelm@12396
   625
lemma finite_imp_foldSet: "finite A ==> EX x. (A, x) : foldSet f e"
wenzelm@12396
   626
  by (induct set: Finites) auto
wenzelm@12396
   627
wenzelm@12396
   628
wenzelm@12396
   629
subsubsection {* Left-commutative operations *}
wenzelm@12396
   630
wenzelm@12396
   631
locale LC =
wenzelm@12396
   632
  fixes f :: "'b => 'a => 'a"    (infixl "\<cdot>" 70)
wenzelm@12396
   633
  assumes left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
wenzelm@12396
   634
wenzelm@12396
   635
lemma (in LC) foldSet_determ_aux:
wenzelm@12396
   636
  "ALL A x. card A < n --> (A, x) : foldSet f e -->
wenzelm@12396
   637
    (ALL y. (A, y) : foldSet f e --> y = x)"
wenzelm@12396
   638
  apply (induct n)
wenzelm@12396
   639
   apply (auto simp add: less_Suc_eq)
wenzelm@12396
   640
  apply (erule foldSet.cases)
wenzelm@12396
   641
   apply blast
wenzelm@12396
   642
  apply (erule foldSet.cases)
wenzelm@12396
   643
   apply blast
wenzelm@12396
   644
  apply clarify
wenzelm@12396
   645
  txt {* force simplification of @{text "card A < card (insert ...)"}. *}
wenzelm@12396
   646
  apply (erule rev_mp)
wenzelm@12396
   647
  apply (simp add: less_Suc_eq_le)
wenzelm@12396
   648
  apply (rule impI)
wenzelm@12396
   649
  apply (rename_tac Aa xa ya Ab xb yb, case_tac "xa = xb")
wenzelm@12396
   650
   apply (subgoal_tac "Aa = Ab")
wenzelm@12396
   651
    prefer 2 apply (blast elim!: equalityE)
wenzelm@12396
   652
   apply blast
wenzelm@12396
   653
  txt {* case @{prop "xa \<notin> xb"}. *}
wenzelm@12396
   654
  apply (subgoal_tac "Aa - {xb} = Ab - {xa} & xb : Aa & xa : Ab")
wenzelm@12396
   655
   prefer 2 apply (blast elim!: equalityE)
wenzelm@12396
   656
  apply clarify
wenzelm@12396
   657
  apply (subgoal_tac "Aa = insert xb Ab - {xa}")
wenzelm@12396
   658
   prefer 2 apply blast
wenzelm@12396
   659
  apply (subgoal_tac "card Aa <= card Ab")
wenzelm@12396
   660
   prefer 2
wenzelm@12396
   661
   apply (rule Suc_le_mono [THEN subst])
wenzelm@12396
   662
   apply (simp add: card_Suc_Diff1)
wenzelm@12396
   663
  apply (rule_tac A1 = "Aa - {xb}" and f1 = f in finite_imp_foldSet [THEN exE])
wenzelm@12396
   664
  apply (blast intro: foldSet_imp_finite finite_Diff)
wenzelm@12396
   665
  apply (frule (1) Diff1_foldSet)
wenzelm@12396
   666
  apply (subgoal_tac "ya = f xb x")
wenzelm@12396
   667
   prefer 2 apply (blast del: equalityCE)
wenzelm@12396
   668
  apply (subgoal_tac "(Ab - {xa}, x) : foldSet f e")
wenzelm@12396
   669
   prefer 2 apply simp
wenzelm@12396
   670
  apply (subgoal_tac "yb = f xa x")
wenzelm@12396
   671
   prefer 2 apply (blast del: equalityCE dest: Diff1_foldSet)
wenzelm@12396
   672
  apply (simp (no_asm_simp) add: left_commute)
wenzelm@12396
   673
  done
wenzelm@12396
   674
wenzelm@12396
   675
lemma (in LC) foldSet_determ: "(A, x) : foldSet f e ==> (A, y) : foldSet f e ==> y = x"
wenzelm@12396
   676
  by (blast intro: foldSet_determ_aux [rule_format])
wenzelm@12396
   677
wenzelm@12396
   678
lemma (in LC) fold_equality: "(A, y) : foldSet f e ==> fold f e A = y"
wenzelm@12396
   679
  by (unfold fold_def) (blast intro: foldSet_determ)
wenzelm@12396
   680
wenzelm@12396
   681
lemma fold_empty [simp]: "fold f e {} = e"
wenzelm@12396
   682
  by (unfold fold_def) blast
wenzelm@12396
   683
wenzelm@12396
   684
lemma (in LC) fold_insert_aux: "x \<notin> A ==>
wenzelm@12396
   685
    ((insert x A, v) : foldSet f e) =
wenzelm@12396
   686
    (EX y. (A, y) : foldSet f e & v = f x y)"
wenzelm@12396
   687
  apply auto
wenzelm@12396
   688
  apply (rule_tac A1 = A and f1 = f in finite_imp_foldSet [THEN exE])
wenzelm@12396
   689
   apply (fastsimp dest: foldSet_imp_finite)
wenzelm@12396
   690
  apply (blast intro: foldSet_determ)
wenzelm@12396
   691
  done
wenzelm@12396
   692
wenzelm@12396
   693
lemma (in LC) fold_insert:
wenzelm@12396
   694
    "finite A ==> x \<notin> A ==> fold f e (insert x A) = f x (fold f e A)"
wenzelm@12396
   695
  apply (unfold fold_def)
wenzelm@12396
   696
  apply (simp add: fold_insert_aux)
wenzelm@12396
   697
  apply (rule the_equality)
wenzelm@12396
   698
  apply (auto intro: finite_imp_foldSet
wenzelm@12396
   699
    cong add: conj_cong simp add: fold_def [symmetric] fold_equality)
wenzelm@12396
   700
  done
wenzelm@12396
   701
wenzelm@12396
   702
lemma (in LC) fold_commute: "finite A ==> (!!e. f x (fold f e A) = fold f (f x e) A)"
wenzelm@12396
   703
  apply (induct set: Finites)
wenzelm@12396
   704
   apply simp
wenzelm@12396
   705
  apply (simp add: left_commute fold_insert)
wenzelm@12396
   706
  done
wenzelm@12396
   707
wenzelm@12396
   708
lemma (in LC) fold_nest_Un_Int:
wenzelm@12396
   709
  "finite A ==> finite B
wenzelm@12396
   710
    ==> fold f (fold f e B) A = fold f (fold f e (A Int B)) (A Un B)"
wenzelm@12396
   711
  apply (induct set: Finites)
wenzelm@12396
   712
   apply simp
wenzelm@12396
   713
  apply (simp add: fold_insert fold_commute Int_insert_left insert_absorb)
wenzelm@12396
   714
  done
wenzelm@12396
   715
wenzelm@12396
   716
lemma (in LC) fold_nest_Un_disjoint:
wenzelm@12396
   717
  "finite A ==> finite B ==> A Int B = {}
wenzelm@12396
   718
    ==> fold f e (A Un B) = fold f (fold f e B) A"
wenzelm@12396
   719
  by (simp add: fold_nest_Un_Int)
wenzelm@12396
   720
wenzelm@12396
   721
declare foldSet_imp_finite [simp del]
wenzelm@12396
   722
    empty_foldSetE [rule del]  foldSet.intros [rule del]
wenzelm@12396
   723
  -- {* Delete rules to do with @{text foldSet} relation. *}
wenzelm@12396
   724
wenzelm@12396
   725
wenzelm@12396
   726
wenzelm@12396
   727
subsubsection {* Commutative monoids *}
wenzelm@12396
   728
wenzelm@12396
   729
text {*
wenzelm@12396
   730
  We enter a more restrictive context, with @{text "f :: 'a => 'a => 'a"}
wenzelm@12396
   731
  instead of @{text "'b => 'a => 'a"}.
wenzelm@12396
   732
*}
wenzelm@12396
   733
wenzelm@12396
   734
locale ACe =
wenzelm@12396
   735
  fixes f :: "'a => 'a => 'a"    (infixl "\<cdot>" 70)
wenzelm@12396
   736
    and e :: 'a
wenzelm@12396
   737
  assumes ident [simp]: "x \<cdot> e = x"
wenzelm@12396
   738
    and commute: "x \<cdot> y = y \<cdot> x"
wenzelm@12396
   739
    and assoc: "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)"
wenzelm@12396
   740
wenzelm@12396
   741
lemma (in ACe) left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
wenzelm@12396
   742
proof -
wenzelm@12396
   743
  have "x \<cdot> (y \<cdot> z) = (y \<cdot> z) \<cdot> x" by (simp only: commute)
wenzelm@12396
   744
  also have "... = y \<cdot> (z \<cdot> x)" by (simp only: assoc)
wenzelm@12396
   745
  also have "z \<cdot> x = x \<cdot> z" by (simp only: commute)
wenzelm@12396
   746
  finally show ?thesis .
wenzelm@12396
   747
qed
wenzelm@12396
   748
wenzelm@12718
   749
lemmas (in ACe) AC = assoc commute left_commute
wenzelm@12396
   750
wenzelm@12693
   751
lemma (in ACe) left_ident [simp]: "e \<cdot> x = x"
wenzelm@12396
   752
proof -
wenzelm@12396
   753
  have "x \<cdot> e = x" by (rule ident)
wenzelm@12396
   754
  thus ?thesis by (subst commute)
wenzelm@12396
   755
qed
wenzelm@12396
   756
wenzelm@12396
   757
lemma (in ACe) fold_Un_Int:
wenzelm@12396
   758
  "finite A ==> finite B ==>
wenzelm@12396
   759
    fold f e A \<cdot> fold f e B = fold f e (A Un B) \<cdot> fold f e (A Int B)"
wenzelm@12396
   760
  apply (induct set: Finites)
wenzelm@12396
   761
   apply simp
wenzelm@13400
   762
  apply (simp add: AC insert_absorb Int_insert_left
wenzelm@13421
   763
    LC.fold_insert [OF LC.intro])
wenzelm@12396
   764
  done
wenzelm@12396
   765
wenzelm@12396
   766
lemma (in ACe) fold_Un_disjoint:
wenzelm@12396
   767
  "finite A ==> finite B ==> A Int B = {} ==>
wenzelm@12396
   768
    fold f e (A Un B) = fold f e A \<cdot> fold f e B"
wenzelm@12396
   769
  by (simp add: fold_Un_Int)
wenzelm@12396
   770
wenzelm@12396
   771
lemma (in ACe) fold_Un_disjoint2:
wenzelm@12396
   772
  "finite A ==> finite B ==> A Int B = {} ==>
wenzelm@12396
   773
    fold (f o g) e (A Un B) = fold (f o g) e A \<cdot> fold (f o g) e B"
wenzelm@12396
   774
proof -
wenzelm@12396
   775
  assume b: "finite B"
wenzelm@12396
   776
  assume "finite A"
wenzelm@12396
   777
  thus "A Int B = {} ==>
wenzelm@12396
   778
    fold (f o g) e (A Un B) = fold (f o g) e A \<cdot> fold (f o g) e B"
wenzelm@12396
   779
  proof induct
wenzelm@12396
   780
    case empty
wenzelm@12396
   781
    thus ?case by simp
wenzelm@12396
   782
  next
wenzelm@12396
   783
    case (insert F x)
paulson@13571
   784
    have "fold (f o g) e (insert x F \<union> B) = fold (f o g) e (insert x (F \<union> B))"
wenzelm@12396
   785
      by simp
paulson@13571
   786
    also have "... = (f o g) x (fold (f o g) e (F \<union> B))"
wenzelm@13400
   787
      by (rule LC.fold_insert [OF LC.intro])
wenzelm@13421
   788
        (insert b insert, auto simp add: left_commute)
paulson@13571
   789
    also from insert have "fold (f o g) e (F \<union> B) =
paulson@13571
   790
      fold (f o g) e F \<cdot> fold (f o g) e B" by blast
paulson@13571
   791
    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
   792
      by (simp add: AC)
paulson@13571
   793
    also have "(f o g) x (fold (f o g) e F) = fold (f o g) e (insert x F)"
wenzelm@13400
   794
      by (rule LC.fold_insert [OF LC.intro, symmetric]) (insert b insert,
wenzelm@13421
   795
	auto simp add: left_commute)
wenzelm@12396
   796
    finally show ?case .
wenzelm@12396
   797
  qed
wenzelm@12396
   798
qed
wenzelm@12396
   799
wenzelm@12396
   800
wenzelm@12396
   801
subsection {* Generalized summation over a set *}
wenzelm@12396
   802
wenzelm@12396
   803
constdefs
wenzelm@12396
   804
  setsum :: "('a => 'b) => 'a set => 'b::plus_ac0"
wenzelm@12396
   805
  "setsum f A == if finite A then fold (op + o f) 0 A else 0"
wenzelm@12396
   806
wenzelm@12396
   807
syntax
wenzelm@12396
   808
  "_setsum" :: "idt => 'a set => 'b => 'b::plus_ac0"    ("\<Sum>_:_. _" [0, 51, 10] 10)
wenzelm@12396
   809
syntax (xsymbols)
wenzelm@12396
   810
  "_setsum" :: "idt => 'a set => 'b => 'b::plus_ac0"    ("\<Sum>_\<in>_. _" [0, 51, 10] 10)
wenzelm@12396
   811
translations
wenzelm@12396
   812
  "\<Sum>i:A. b" == "setsum (%i. b) A"  -- {* Beware of argument permutation! *}
wenzelm@12396
   813
wenzelm@12396
   814
wenzelm@12396
   815
lemma setsum_empty [simp]: "setsum f {} = 0"
wenzelm@12396
   816
  by (simp add: setsum_def)
wenzelm@12396
   817
wenzelm@12396
   818
lemma setsum_insert [simp]:
wenzelm@12396
   819
    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
wenzelm@13365
   820
  by (simp add: setsum_def
wenzelm@13421
   821
    LC.fold_insert [OF LC.intro] plus_ac0_left_commute)
wenzelm@12396
   822
wenzelm@12396
   823
lemma setsum_0: "setsum (\<lambda>i. 0) A = 0"
wenzelm@12396
   824
  apply (case_tac "finite A")
wenzelm@12396
   825
   prefer 2 apply (simp add: setsum_def)
wenzelm@12396
   826
  apply (erule finite_induct)
wenzelm@12396
   827
   apply auto
wenzelm@12396
   828
  done
wenzelm@12396
   829
wenzelm@12396
   830
lemma setsum_eq_0_iff [simp]:
wenzelm@12396
   831
    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
wenzelm@12396
   832
  by (induct set: Finites) auto
wenzelm@12396
   833
wenzelm@12396
   834
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
wenzelm@12396
   835
  apply (case_tac "finite A")
wenzelm@12396
   836
   prefer 2 apply (simp add: setsum_def)
wenzelm@12396
   837
  apply (erule rev_mp)
wenzelm@12396
   838
  apply (erule finite_induct)
wenzelm@12396
   839
   apply auto
wenzelm@12396
   840
  done
wenzelm@12396
   841
wenzelm@12396
   842
lemma card_eq_setsum: "finite A ==> card A = setsum (\<lambda>x. 1) A"
wenzelm@12396
   843
  -- {* Could allow many @{text "card"} proofs to be simplified. *}
wenzelm@12396
   844
  by (induct set: Finites) auto
wenzelm@12396
   845
wenzelm@12396
   846
lemma setsum_Un_Int: "finite A ==> finite B
wenzelm@12396
   847
    ==> setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
wenzelm@12396
   848
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
wenzelm@12396
   849
  apply (induct set: Finites)
wenzelm@12396
   850
   apply simp
wenzelm@12396
   851
  apply (simp add: plus_ac0 Int_insert_left insert_absorb)
wenzelm@12396
   852
  done
wenzelm@12396
   853
wenzelm@12396
   854
lemma setsum_Un_disjoint: "finite A ==> finite B
wenzelm@12396
   855
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
wenzelm@12396
   856
  apply (subst setsum_Un_Int [symmetric])
wenzelm@12396
   857
    apply auto
wenzelm@12396
   858
  done
wenzelm@12396
   859
wenzelm@12937
   860
lemma setsum_UN_disjoint:
wenzelm@12937
   861
  fixes f :: "'a => 'b::plus_ac0"
wenzelm@12937
   862
  shows
wenzelm@12937
   863
    "finite I ==> (ALL i:I. finite (A i)) ==>
wenzelm@12937
   864
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
wenzelm@12937
   865
      setsum f (UNION I A) = setsum (\<lambda>i. setsum f (A i)) I"
wenzelm@12396
   866
  apply (induct set: Finites)
wenzelm@12396
   867
   apply simp
wenzelm@12396
   868
  apply atomize
wenzelm@12396
   869
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
wenzelm@12396
   870
   prefer 2 apply blast
wenzelm@12396
   871
  apply (subgoal_tac "A x Int UNION F A = {}")
wenzelm@12396
   872
   prefer 2 apply blast
wenzelm@12396
   873
  apply (simp add: setsum_Un_disjoint)
wenzelm@12396
   874
  done
wenzelm@12396
   875
wenzelm@12396
   876
lemma setsum_addf: "setsum (\<lambda>x. f x + g x) A = (setsum f A + setsum g A)"
wenzelm@12396
   877
  apply (case_tac "finite A")
wenzelm@12396
   878
   prefer 2 apply (simp add: setsum_def)
wenzelm@12396
   879
  apply (erule finite_induct)
wenzelm@12396
   880
   apply auto
wenzelm@12396
   881
  apply (simp add: plus_ac0)
wenzelm@12396
   882
  done
wenzelm@12396
   883
wenzelm@12396
   884
lemma setsum_Un: "finite A ==> finite B ==>
wenzelm@12396
   885
    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
wenzelm@12396
   886
  -- {* For the natural numbers, we have subtraction. *}
wenzelm@12396
   887
  apply (subst setsum_Un_Int [symmetric])
wenzelm@12396
   888
    apply auto
wenzelm@12396
   889
  done
wenzelm@12396
   890
wenzelm@12396
   891
lemma setsum_diff1: "(setsum f (A - {a}) :: nat) =
wenzelm@12396
   892
    (if a:A then setsum f A - f a else setsum f A)"
wenzelm@12396
   893
  apply (case_tac "finite A")
wenzelm@12396
   894
   prefer 2 apply (simp add: setsum_def)
wenzelm@12396
   895
  apply (erule finite_induct)
wenzelm@12396
   896
   apply (auto simp add: insert_Diff_if)
wenzelm@12396
   897
  apply (drule_tac a = a in mk_disjoint_insert)
wenzelm@12396
   898
  apply auto
wenzelm@12396
   899
  done
wenzelm@12396
   900
wenzelm@12396
   901
lemma setsum_cong:
wenzelm@12396
   902
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
wenzelm@12396
   903
  apply (case_tac "finite B")
wenzelm@12396
   904
   prefer 2 apply (simp add: setsum_def)
wenzelm@12396
   905
  apply simp
wenzelm@12396
   906
  apply (subgoal_tac "ALL C. C <= B --> (ALL x:C. f x = g x) --> setsum f C = setsum g C")
wenzelm@12396
   907
   apply simp
wenzelm@12396
   908
  apply (erule finite_induct)
wenzelm@12396
   909
  apply simp
wenzelm@12396
   910
  apply (simp add: subset_insert_iff)
wenzelm@12396
   911
  apply clarify
wenzelm@12396
   912
  apply (subgoal_tac "finite C")
wenzelm@12396
   913
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
wenzelm@12396
   914
  apply (subgoal_tac "C = insert x (C - {x})")
wenzelm@12396
   915
   prefer 2 apply blast
wenzelm@12396
   916
  apply (erule ssubst)
wenzelm@12396
   917
  apply (drule spec)
wenzelm@12396
   918
  apply (erule (1) notE impE)
wenzelm@12396
   919
  apply (simp add: Ball_def)
wenzelm@12396
   920
  done
wenzelm@12396
   921
nipkow@13490
   922
subsubsection{* Min and Max of finite linearly ordered sets *}
nipkow@13490
   923
nipkow@13490
   924
text{* Seemed easier to define directly than via fold. *}
nipkow@13490
   925
nipkow@13490
   926
lemma ex_Max: fixes S :: "('a::linorder)set"
nipkow@13490
   927
  assumes fin: "finite S" shows "S \<noteq> {} \<Longrightarrow> \<exists>m\<in>S. \<forall>s \<in> S. s \<le> m"
nipkow@13490
   928
using fin
nipkow@13490
   929
proof (induct)
nipkow@13490
   930
  case empty thus ?case by simp
nipkow@13490
   931
next
nipkow@13490
   932
  case (insert S x)
nipkow@13490
   933
  show ?case
nipkow@13490
   934
  proof (cases)
nipkow@13490
   935
    assume "S = {}" thus ?thesis by simp
nipkow@13490
   936
  next
nipkow@13490
   937
    assume nonempty: "S \<noteq> {}"
nipkow@13490
   938
    then obtain m where m: "m\<in>S" "\<forall>s\<in>S. s \<le> m" using insert by blast
nipkow@13490
   939
    show ?thesis
nipkow@13490
   940
    proof (cases)
nipkow@13490
   941
      assume "x \<le> m" thus ?thesis using m by blast
nipkow@13490
   942
    next
nipkow@13490
   943
      assume "\<not> x \<le> m" thus ?thesis using m
nipkow@13490
   944
	by(simp add:linorder_not_le order_less_le)(blast intro: order_trans)
nipkow@13490
   945
    qed
nipkow@13490
   946
  qed
nipkow@13490
   947
qed
nipkow@13490
   948
nipkow@13490
   949
lemma ex_Min: fixes S :: "('a::linorder)set"
nipkow@13490
   950
  assumes fin: "finite S" shows "S \<noteq> {} \<Longrightarrow> \<exists>m\<in>S. \<forall>s \<in> S. m \<le> s"
nipkow@13490
   951
using fin
nipkow@13490
   952
proof (induct)
nipkow@13490
   953
  case empty thus ?case by simp
nipkow@13490
   954
next
nipkow@13490
   955
  case (insert S x)
nipkow@13490
   956
  show ?case
nipkow@13490
   957
  proof (cases)
nipkow@13490
   958
    assume "S = {}" thus ?thesis by simp
nipkow@13490
   959
  next
nipkow@13490
   960
    assume nonempty: "S \<noteq> {}"
nipkow@13490
   961
    then obtain m where m: "m\<in>S" "\<forall>s\<in>S. m \<le> s" using insert by blast
nipkow@13490
   962
    show ?thesis
nipkow@13490
   963
    proof (cases)
nipkow@13490
   964
      assume "m \<le> x" thus ?thesis using m by blast
nipkow@13490
   965
    next
nipkow@13490
   966
      assume "\<not> m \<le> x" thus ?thesis using m
nipkow@13490
   967
	by(simp add:linorder_not_le order_less_le)(blast intro: order_trans)
nipkow@13490
   968
    qed
nipkow@13490
   969
  qed
nipkow@13490
   970
qed
nipkow@13490
   971
nipkow@13490
   972
constdefs
nipkow@13490
   973
 Min :: "('a::linorder)set \<Rightarrow> 'a"
nipkow@13490
   974
"Min S  \<equiv>  THE m. m \<in> S \<and> (\<forall>s \<in> S. m \<le> s)"
nipkow@13490
   975
nipkow@13490
   976
 Max :: "('a::linorder)set \<Rightarrow> 'a"
nipkow@13490
   977
"Max S  \<equiv>  THE m. m \<in> S \<and> (\<forall>s \<in> S. s \<le> m)"
nipkow@13490
   978
nipkow@13490
   979
lemma Min[simp]: assumes a: "finite S" "S \<noteq> {}"
nipkow@13490
   980
                 shows "Min S \<in> S \<and> (\<forall>s \<in> S. Min S \<le> s)" (is "?P(Min S)")
nipkow@13490
   981
proof (unfold Min_def, rule theI')
nipkow@13490
   982
  show "\<exists>!m. ?P m"
nipkow@13490
   983
  proof (rule ex_ex1I)
nipkow@13490
   984
    show "\<exists>m. ?P m" using ex_Min[OF a] by blast
nipkow@13490
   985
  next
nipkow@13490
   986
    fix m1 m2 assume "?P m1" "?P m2"
nipkow@13490
   987
    thus "m1 = m2" by (blast dest:order_antisym)
nipkow@13490
   988
  qed
nipkow@13490
   989
qed
nipkow@13490
   990
nipkow@13490
   991
lemma Max[simp]: assumes a: "finite S" "S \<noteq> {}"
nipkow@13490
   992
                 shows "Max S \<in> S \<and> (\<forall>s \<in> S. s \<le> Max S)" (is "?P(Max S)")
nipkow@13490
   993
proof (unfold Max_def, rule theI')
nipkow@13490
   994
  show "\<exists>!m. ?P m"
nipkow@13490
   995
  proof (rule ex_ex1I)
nipkow@13490
   996
    show "\<exists>m. ?P m" using ex_Max[OF a] by blast
nipkow@13490
   997
  next
nipkow@13490
   998
    fix m1 m2 assume "?P m1" "?P m2"
nipkow@13490
   999
    thus "m1 = m2" by (blast dest:order_antisym)
nipkow@13490
  1000
  qed
nipkow@13490
  1001
qed
nipkow@13490
  1002
wenzelm@12396
  1003
wenzelm@12396
  1004
text {*
wenzelm@12396
  1005
  \medskip Basic theorem about @{text "choose"}.  By Florian
wenzelm@12396
  1006
  Kammüller, tidied by LCP.
wenzelm@12396
  1007
*}
wenzelm@12396
  1008
wenzelm@12396
  1009
lemma card_s_0_eq_empty:
wenzelm@12396
  1010
    "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
wenzelm@12396
  1011
  apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
wenzelm@12396
  1012
  apply (simp cong add: rev_conj_cong)
wenzelm@12396
  1013
  done
wenzelm@12396
  1014
wenzelm@12396
  1015
lemma choose_deconstruct: "finite M ==> x \<notin> M
wenzelm@12396
  1016
  ==> {s. s <= insert x M & card(s) = Suc k}
wenzelm@12396
  1017
       = {s. s <= M & card(s) = Suc k} Un
wenzelm@12396
  1018
         {s. EX t. t <= M & card(t) = k & s = insert x t}"
wenzelm@12396
  1019
  apply safe
wenzelm@12396
  1020
   apply (auto intro: finite_subset [THEN card_insert_disjoint])
wenzelm@12396
  1021
  apply (drule_tac x = "xa - {x}" in spec)
wenzelm@12396
  1022
  apply (subgoal_tac "x ~: xa")
wenzelm@12396
  1023
   apply auto
wenzelm@12396
  1024
  apply (erule rev_mp, subst card_Diff_singleton)
wenzelm@12396
  1025
  apply (auto intro: finite_subset)
wenzelm@12396
  1026
  done
wenzelm@12396
  1027
wenzelm@12396
  1028
lemma card_inj_on_le:
paulson@13595
  1029
    "[|inj_on f A; f ` A \<subseteq> B; finite A; finite B |] ==> card A <= card B"
wenzelm@12396
  1030
  by (auto intro: card_mono simp add: card_image [symmetric])
wenzelm@12396
  1031
paulson@13595
  1032
lemma card_bij_eq: 
paulson@13595
  1033
    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A; 
paulson@13595
  1034
       finite A; finite B |] ==> card A = card B"
wenzelm@12396
  1035
  by (auto intro: le_anti_sym card_inj_on_le)
wenzelm@12396
  1036
paulson@13595
  1037
text{*There are as many subsets of @{term A} having cardinality @{term k}
paulson@13595
  1038
 as there are sets obtained from the former by inserting a fixed element
paulson@13595
  1039
 @{term x} into each.*}
paulson@13595
  1040
lemma constr_bij:
paulson@13595
  1041
   "[|finite A; x \<notin> A|] ==>
paulson@13595
  1042
    card {B. EX C. C <= A & card(C) = k & B = insert x C} =
wenzelm@12396
  1043
    card {B. B <= A & card(B) = k}"
wenzelm@12396
  1044
  apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
paulson@13595
  1045
       apply (auto elim!: equalityE simp add: inj_on_def)
paulson@13595
  1046
    apply (subst Diff_insert0, auto)
paulson@13595
  1047
   txt {* finiteness of the two sets *}
paulson@13595
  1048
   apply (rule_tac [2] B = "Pow (A)" in finite_subset)
paulson@13595
  1049
   apply (rule_tac B = "Pow (insert x A)" in finite_subset)
paulson@13595
  1050
   apply fast+
wenzelm@12396
  1051
  done
wenzelm@12396
  1052
wenzelm@12396
  1053
text {*
wenzelm@12396
  1054
  Main theorem: combinatorial statement about number of subsets of a set.
wenzelm@12396
  1055
*}
wenzelm@12396
  1056
wenzelm@12396
  1057
lemma n_sub_lemma:
wenzelm@12396
  1058
  "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
wenzelm@12396
  1059
  apply (induct k)
wenzelm@12396
  1060
   apply (simp add: card_s_0_eq_empty)
wenzelm@12396
  1061
  apply atomize
wenzelm@12396
  1062
  apply (rotate_tac -1, erule finite_induct)
wenzelm@13421
  1063
   apply (simp_all (no_asm_simp) cong add: conj_cong
wenzelm@13421
  1064
     add: card_s_0_eq_empty choose_deconstruct)
wenzelm@12396
  1065
  apply (subst card_Un_disjoint)
wenzelm@12396
  1066
     prefer 4 apply (force simp add: constr_bij)
wenzelm@12396
  1067
    prefer 3 apply force
wenzelm@12396
  1068
   prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
wenzelm@12396
  1069
     finite_subset [of _ "Pow (insert x F)", standard])
wenzelm@12396
  1070
  apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
wenzelm@12396
  1071
  done
wenzelm@12396
  1072
wenzelm@13421
  1073
theorem n_subsets:
wenzelm@13421
  1074
    "finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
wenzelm@12396
  1075
  by (simp add: n_sub_lemma)
wenzelm@12396
  1076
wenzelm@12396
  1077
end