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