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