src/HOL/Finite_Set.thy
author nipkow
Tue Oct 23 23:27:23 2007 +0200 (2007-10-23)
changeset 25162 ad4d5365d9d8
parent 25062 af5ef0d4d655
child 25205 b408ceba4627
permissions -rw-r--r--
went back to >0
wenzelm@12396
     1
(*  Title:      HOL/Finite_Set.thy
wenzelm@12396
     2
    ID:         $Id$
wenzelm@12396
     3
    Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
avigad@16775
     4
                with contributions by Jeremy Avigad
wenzelm@12396
     5
*)
wenzelm@12396
     6
wenzelm@12396
     7
header {* Finite sets *}
wenzelm@12396
     8
nipkow@15131
     9
theory Finite_Set
haftmann@24728
    10
imports Divides
nipkow@15131
    11
begin
wenzelm@12396
    12
nipkow@15392
    13
subsection {* Definition and basic properties *}
wenzelm@12396
    14
berghofe@23736
    15
inductive finite :: "'a set => bool"
berghofe@22262
    16
  where
berghofe@22262
    17
    emptyI [simp, intro!]: "finite {}"
berghofe@22262
    18
  | insertI [simp, intro!]: "finite A ==> finite (insert a A)"
wenzelm@12396
    19
nipkow@13737
    20
lemma ex_new_if_finite: -- "does not depend on def of finite at all"
wenzelm@14661
    21
  assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
wenzelm@14661
    22
  shows "\<exists>a::'a. a \<notin> A"
wenzelm@14661
    23
proof -
wenzelm@14661
    24
  from prems have "A \<noteq> UNIV" by blast
wenzelm@14661
    25
  thus ?thesis by blast
wenzelm@14661
    26
qed
wenzelm@12396
    27
berghofe@22262
    28
lemma finite_induct [case_names empty insert, induct set: finite]:
wenzelm@12396
    29
  "finite F ==>
nipkow@15327
    30
    P {} ==> (!!x F. finite F ==> x \<notin> F ==> P F ==> P (insert x F)) ==> P F"
wenzelm@12396
    31
  -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
wenzelm@12396
    32
proof -
wenzelm@13421
    33
  assume "P {}" and
nipkow@15327
    34
    insert: "!!x F. finite F ==> x \<notin> F ==> P F ==> P (insert x F)"
wenzelm@12396
    35
  assume "finite F"
wenzelm@12396
    36
  thus "P F"
wenzelm@12396
    37
  proof induct
wenzelm@23389
    38
    show "P {}" by fact
nipkow@15327
    39
    fix x F assume F: "finite F" and P: "P F"
wenzelm@12396
    40
    show "P (insert x F)"
wenzelm@12396
    41
    proof cases
wenzelm@12396
    42
      assume "x \<in> F"
wenzelm@12396
    43
      hence "insert x F = F" by (rule insert_absorb)
wenzelm@12396
    44
      with P show ?thesis by (simp only:)
wenzelm@12396
    45
    next
wenzelm@12396
    46
      assume "x \<notin> F"
wenzelm@12396
    47
      from F this P show ?thesis by (rule insert)
wenzelm@12396
    48
    qed
wenzelm@12396
    49
  qed
wenzelm@12396
    50
qed
wenzelm@12396
    51
nipkow@15484
    52
lemma finite_ne_induct[case_names singleton insert, consumes 2]:
nipkow@15484
    53
assumes fin: "finite F" shows "F \<noteq> {} \<Longrightarrow>
nipkow@15484
    54
 \<lbrakk> \<And>x. P{x};
nipkow@15484
    55
   \<And>x F. \<lbrakk> finite F; F \<noteq> {}; x \<notin> F; P F \<rbrakk> \<Longrightarrow> P (insert x F) \<rbrakk>
nipkow@15484
    56
 \<Longrightarrow> P F"
nipkow@15484
    57
using fin
nipkow@15484
    58
proof induct
nipkow@15484
    59
  case empty thus ?case by simp
nipkow@15484
    60
next
nipkow@15484
    61
  case (insert x F)
nipkow@15484
    62
  show ?case
nipkow@15484
    63
  proof cases
wenzelm@23389
    64
    assume "F = {}"
wenzelm@23389
    65
    thus ?thesis using `P {x}` by simp
nipkow@15484
    66
  next
wenzelm@23389
    67
    assume "F \<noteq> {}"
wenzelm@23389
    68
    thus ?thesis using insert by blast
nipkow@15484
    69
  qed
nipkow@15484
    70
qed
nipkow@15484
    71
wenzelm@12396
    72
lemma finite_subset_induct [consumes 2, case_names empty insert]:
wenzelm@23389
    73
  assumes "finite F" and "F \<subseteq> A"
wenzelm@23389
    74
    and empty: "P {}"
wenzelm@23389
    75
    and insert: "!!a F. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)"
wenzelm@23389
    76
  shows "P F"
wenzelm@12396
    77
proof -
wenzelm@23389
    78
  from `finite F` and `F \<subseteq> A`
wenzelm@23389
    79
  show ?thesis
wenzelm@12396
    80
  proof induct
wenzelm@23389
    81
    show "P {}" by fact
wenzelm@23389
    82
  next
wenzelm@23389
    83
    fix x F
wenzelm@23389
    84
    assume "finite F" and "x \<notin> F" and
wenzelm@23389
    85
      P: "F \<subseteq> A ==> P F" and i: "insert x F \<subseteq> A"
wenzelm@12396
    86
    show "P (insert x F)"
wenzelm@12396
    87
    proof (rule insert)
wenzelm@12396
    88
      from i show "x \<in> A" by blast
wenzelm@12396
    89
      from i have "F \<subseteq> A" by blast
wenzelm@12396
    90
      with P show "P F" .
wenzelm@23389
    91
      show "finite F" by fact
wenzelm@23389
    92
      show "x \<notin> F" by fact
wenzelm@12396
    93
    qed
wenzelm@12396
    94
  qed
wenzelm@12396
    95
qed
wenzelm@12396
    96
haftmann@23878
    97
nipkow@15392
    98
text{* Finite sets are the images of initial segments of natural numbers: *}
nipkow@15392
    99
paulson@15510
   100
lemma finite_imp_nat_seg_image_inj_on:
paulson@15510
   101
  assumes fin: "finite A" 
paulson@15510
   102
  shows "\<exists> (n::nat) f. A = f ` {i. i<n} & inj_on f {i. i<n}"
nipkow@15392
   103
using fin
nipkow@15392
   104
proof induct
nipkow@15392
   105
  case empty
paulson@15510
   106
  show ?case  
paulson@15510
   107
  proof show "\<exists>f. {} = f ` {i::nat. i < 0} & inj_on f {i. i<0}" by simp 
paulson@15510
   108
  qed
nipkow@15392
   109
next
nipkow@15392
   110
  case (insert a A)
wenzelm@23389
   111
  have notinA: "a \<notin> A" by fact
paulson@15510
   112
  from insert.hyps obtain n f
paulson@15510
   113
    where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}" by blast
paulson@15510
   114
  hence "insert a A = f(n:=a) ` {i. i < Suc n}"
paulson@15510
   115
        "inj_on (f(n:=a)) {i. i < Suc n}" using notinA
paulson@15510
   116
    by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq)
nipkow@15392
   117
  thus ?case by blast
nipkow@15392
   118
qed
nipkow@15392
   119
nipkow@15392
   120
lemma nat_seg_image_imp_finite:
nipkow@15392
   121
  "!!f A. A = f ` {i::nat. i<n} \<Longrightarrow> finite A"
nipkow@15392
   122
proof (induct n)
nipkow@15392
   123
  case 0 thus ?case by simp
nipkow@15392
   124
next
nipkow@15392
   125
  case (Suc n)
nipkow@15392
   126
  let ?B = "f ` {i. i < n}"
nipkow@15392
   127
  have finB: "finite ?B" by(rule Suc.hyps[OF refl])
nipkow@15392
   128
  show ?case
nipkow@15392
   129
  proof cases
nipkow@15392
   130
    assume "\<exists>k<n. f n = f k"
nipkow@15392
   131
    hence "A = ?B" using Suc.prems by(auto simp:less_Suc_eq)
nipkow@15392
   132
    thus ?thesis using finB by simp
nipkow@15392
   133
  next
nipkow@15392
   134
    assume "\<not>(\<exists> k<n. f n = f k)"
nipkow@15392
   135
    hence "A = insert (f n) ?B" using Suc.prems by(auto simp:less_Suc_eq)
nipkow@15392
   136
    thus ?thesis using finB by simp
nipkow@15392
   137
  qed
nipkow@15392
   138
qed
nipkow@15392
   139
nipkow@15392
   140
lemma finite_conv_nat_seg_image:
nipkow@15392
   141
  "finite A = (\<exists> (n::nat) f. A = f ` {i::nat. i<n})"
paulson@15510
   142
by(blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on)
nipkow@15392
   143
nipkow@15392
   144
subsubsection{* Finiteness and set theoretic constructions *}
nipkow@15392
   145
wenzelm@12396
   146
lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
wenzelm@12396
   147
  -- {* The union of two finite sets is finite. *}
berghofe@22262
   148
  by (induct set: finite) simp_all
wenzelm@12396
   149
wenzelm@12396
   150
lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
wenzelm@12396
   151
  -- {* Every subset of a finite set is finite. *}
wenzelm@12396
   152
proof -
wenzelm@12396
   153
  assume "finite B"
wenzelm@12396
   154
  thus "!!A. A \<subseteq> B ==> finite A"
wenzelm@12396
   155
  proof induct
wenzelm@12396
   156
    case empty
wenzelm@12396
   157
    thus ?case by simp
wenzelm@12396
   158
  next
nipkow@15327
   159
    case (insert x F A)
wenzelm@23389
   160
    have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" by fact+
wenzelm@12396
   161
    show "finite A"
wenzelm@12396
   162
    proof cases
wenzelm@12396
   163
      assume x: "x \<in> A"
wenzelm@12396
   164
      with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   165
      with r have "finite (A - {x})" .
wenzelm@12396
   166
      hence "finite (insert x (A - {x}))" ..
wenzelm@23389
   167
      also have "insert x (A - {x}) = A" using x by (rule insert_Diff)
wenzelm@12396
   168
      finally show ?thesis .
wenzelm@12396
   169
    next
wenzelm@23389
   170
      show "A \<subseteq> F ==> ?thesis" by fact
wenzelm@12396
   171
      assume "x \<notin> A"
wenzelm@12396
   172
      with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   173
    qed
wenzelm@12396
   174
  qed
wenzelm@12396
   175
qed
wenzelm@12396
   176
nipkow@18423
   177
lemma finite_Collect_subset[simp]: "finite A \<Longrightarrow> finite{x \<in> A. P x}"
nipkow@17761
   178
using finite_subset[of "{x \<in> A. P x}" "A"] by blast
nipkow@17761
   179
wenzelm@12396
   180
lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
wenzelm@12396
   181
  by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
wenzelm@12396
   182
wenzelm@12396
   183
lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
wenzelm@12396
   184
  -- {* The converse obviously fails. *}
wenzelm@12396
   185
  by (blast intro: finite_subset)
wenzelm@12396
   186
wenzelm@12396
   187
lemma finite_insert [simp]: "finite (insert a A) = finite A"
wenzelm@12396
   188
  apply (subst insert_is_Un)
paulson@14208
   189
  apply (simp only: finite_Un, blast)
wenzelm@12396
   190
  done
wenzelm@12396
   191
nipkow@15281
   192
lemma finite_Union[simp, intro]:
nipkow@15281
   193
 "\<lbrakk> finite A; !!M. M \<in> A \<Longrightarrow> finite M \<rbrakk> \<Longrightarrow> finite(\<Union>A)"
nipkow@15281
   194
by (induct rule:finite_induct) simp_all
nipkow@15281
   195
wenzelm@12396
   196
lemma finite_empty_induct:
wenzelm@23389
   197
  assumes "finite A"
wenzelm@23389
   198
    and "P A"
wenzelm@23389
   199
    and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
wenzelm@23389
   200
  shows "P {}"
wenzelm@12396
   201
proof -
wenzelm@12396
   202
  have "P (A - A)"
wenzelm@12396
   203
  proof -
wenzelm@23389
   204
    {
wenzelm@23389
   205
      fix c b :: "'a set"
wenzelm@23389
   206
      assume c: "finite c" and b: "finite b"
wenzelm@23389
   207
	and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
wenzelm@23389
   208
      have "c \<subseteq> b ==> P (b - c)"
wenzelm@23389
   209
	using c
wenzelm@23389
   210
      proof induct
wenzelm@23389
   211
	case empty
wenzelm@23389
   212
	from P1 show ?case by simp
wenzelm@23389
   213
      next
wenzelm@23389
   214
	case (insert x F)
wenzelm@23389
   215
	have "P (b - F - {x})"
wenzelm@23389
   216
	proof (rule P2)
wenzelm@23389
   217
          from _ b show "finite (b - F)" by (rule finite_subset) blast
wenzelm@23389
   218
          from insert show "x \<in> b - F" by simp
wenzelm@23389
   219
          from insert show "P (b - F)" by simp
wenzelm@23389
   220
	qed
wenzelm@23389
   221
	also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
wenzelm@23389
   222
	finally show ?case .
wenzelm@12396
   223
      qed
wenzelm@23389
   224
    }
wenzelm@23389
   225
    then show ?thesis by this (simp_all add: assms)
wenzelm@12396
   226
  qed
wenzelm@23389
   227
  then show ?thesis by simp
wenzelm@12396
   228
qed
wenzelm@12396
   229
wenzelm@12396
   230
lemma finite_Diff [simp]: "finite B ==> finite (B - Ba)"
wenzelm@12396
   231
  by (rule Diff_subset [THEN finite_subset])
wenzelm@12396
   232
wenzelm@12396
   233
lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
wenzelm@12396
   234
  apply (subst Diff_insert)
wenzelm@12396
   235
  apply (case_tac "a : A - B")
wenzelm@12396
   236
   apply (rule finite_insert [symmetric, THEN trans])
paulson@14208
   237
   apply (subst insert_Diff, simp_all)
wenzelm@12396
   238
  done
wenzelm@12396
   239
paulson@19870
   240
lemma finite_Diff_singleton [simp]: "finite (A - {a}) = finite A"
paulson@19870
   241
  by simp
paulson@19870
   242
wenzelm@12396
   243
nipkow@15392
   244
text {* Image and Inverse Image over Finite Sets *}
paulson@13825
   245
paulson@13825
   246
lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
paulson@13825
   247
  -- {* The image of a finite set is finite. *}
berghofe@22262
   248
  by (induct set: finite) simp_all
paulson@13825
   249
paulson@14430
   250
lemma finite_surj: "finite A ==> B <= f ` A ==> finite B"
paulson@14430
   251
  apply (frule finite_imageI)
paulson@14430
   252
  apply (erule finite_subset, assumption)
paulson@14430
   253
  done
paulson@14430
   254
paulson@13825
   255
lemma finite_range_imageI:
paulson@13825
   256
    "finite (range g) ==> finite (range (%x. f (g x)))"
paulson@14208
   257
  apply (drule finite_imageI, simp)
paulson@13825
   258
  done
paulson@13825
   259
wenzelm@12396
   260
lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
wenzelm@12396
   261
proof -
wenzelm@12396
   262
  have aux: "!!A. finite (A - {}) = finite A" by simp
wenzelm@12396
   263
  fix B :: "'a set"
wenzelm@12396
   264
  assume "finite B"
wenzelm@12396
   265
  thus "!!A. f`A = B ==> inj_on f A ==> finite A"
wenzelm@12396
   266
    apply induct
wenzelm@12396
   267
     apply simp
wenzelm@12396
   268
    apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
wenzelm@12396
   269
     apply clarify
wenzelm@12396
   270
     apply (simp (no_asm_use) add: inj_on_def)
paulson@14208
   271
     apply (blast dest!: aux [THEN iffD1], atomize)
wenzelm@12396
   272
    apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
paulson@14208
   273
    apply (frule subsetD [OF equalityD2 insertI1], clarify)
wenzelm@12396
   274
    apply (rule_tac x = xa in bexI)
wenzelm@12396
   275
     apply (simp_all add: inj_on_image_set_diff)
wenzelm@12396
   276
    done
wenzelm@12396
   277
qed (rule refl)
wenzelm@12396
   278
wenzelm@12396
   279
paulson@13825
   280
lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
paulson@13825
   281
  -- {* The inverse image of a singleton under an injective function
paulson@13825
   282
         is included in a singleton. *}
paulson@14430
   283
  apply (auto simp add: inj_on_def)
paulson@14430
   284
  apply (blast intro: the_equality [symmetric])
paulson@13825
   285
  done
paulson@13825
   286
paulson@13825
   287
lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
paulson@13825
   288
  -- {* The inverse image of a finite set under an injective function
paulson@13825
   289
         is finite. *}
berghofe@22262
   290
  apply (induct set: finite)
wenzelm@21575
   291
   apply simp_all
paulson@14430
   292
  apply (subst vimage_insert)
paulson@14430
   293
  apply (simp add: finite_Un finite_subset [OF inj_vimage_singleton])
paulson@13825
   294
  done
paulson@13825
   295
paulson@13825
   296
nipkow@15392
   297
text {* The finite UNION of finite sets *}
wenzelm@12396
   298
wenzelm@12396
   299
lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)"
berghofe@22262
   300
  by (induct set: finite) simp_all
wenzelm@12396
   301
wenzelm@12396
   302
text {*
wenzelm@12396
   303
  Strengthen RHS to
paulson@14430
   304
  @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x \<noteq> {}})"}?
wenzelm@12396
   305
wenzelm@12396
   306
  We'd need to prove
paulson@14430
   307
  @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x \<noteq> {}}"}
wenzelm@12396
   308
  by induction. *}
wenzelm@12396
   309
wenzelm@12396
   310
lemma finite_UN [simp]: "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
wenzelm@12396
   311
  by (blast intro: finite_UN_I finite_subset)
wenzelm@12396
   312
wenzelm@12396
   313
nipkow@17022
   314
lemma finite_Plus: "[| finite A; finite B |] ==> finite (A <+> B)"
nipkow@17022
   315
by (simp add: Plus_def)
nipkow@17022
   316
nipkow@15392
   317
text {* Sigma of finite sets *}
wenzelm@12396
   318
wenzelm@12396
   319
lemma finite_SigmaI [simp]:
wenzelm@12396
   320
    "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
wenzelm@12396
   321
  by (unfold Sigma_def) (blast intro!: finite_UN_I)
wenzelm@12396
   322
nipkow@15402
   323
lemma finite_cartesian_product: "[| finite A; finite B |] ==>
nipkow@15402
   324
    finite (A <*> B)"
nipkow@15402
   325
  by (rule finite_SigmaI)
nipkow@15402
   326
wenzelm@12396
   327
lemma finite_Prod_UNIV:
wenzelm@12396
   328
    "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
wenzelm@12396
   329
  apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
wenzelm@12396
   330
   apply (erule ssubst)
paulson@14208
   331
   apply (erule finite_SigmaI, auto)
wenzelm@12396
   332
  done
wenzelm@12396
   333
paulson@15409
   334
lemma finite_cartesian_productD1:
paulson@15409
   335
     "[| finite (A <*> B); B \<noteq> {} |] ==> finite A"
paulson@15409
   336
apply (auto simp add: finite_conv_nat_seg_image) 
paulson@15409
   337
apply (drule_tac x=n in spec) 
paulson@15409
   338
apply (drule_tac x="fst o f" in spec) 
paulson@15409
   339
apply (auto simp add: o_def) 
paulson@15409
   340
 prefer 2 apply (force dest!: equalityD2) 
paulson@15409
   341
apply (drule equalityD1) 
paulson@15409
   342
apply (rename_tac y x)
paulson@15409
   343
apply (subgoal_tac "\<exists>k. k<n & f k = (x,y)") 
paulson@15409
   344
 prefer 2 apply force
paulson@15409
   345
apply clarify
paulson@15409
   346
apply (rule_tac x=k in image_eqI, auto)
paulson@15409
   347
done
paulson@15409
   348
paulson@15409
   349
lemma finite_cartesian_productD2:
paulson@15409
   350
     "[| finite (A <*> B); A \<noteq> {} |] ==> finite B"
paulson@15409
   351
apply (auto simp add: finite_conv_nat_seg_image) 
paulson@15409
   352
apply (drule_tac x=n in spec) 
paulson@15409
   353
apply (drule_tac x="snd o f" in spec) 
paulson@15409
   354
apply (auto simp add: o_def) 
paulson@15409
   355
 prefer 2 apply (force dest!: equalityD2) 
paulson@15409
   356
apply (drule equalityD1)
paulson@15409
   357
apply (rename_tac x y)
paulson@15409
   358
apply (subgoal_tac "\<exists>k. k<n & f k = (x,y)") 
paulson@15409
   359
 prefer 2 apply force
paulson@15409
   360
apply clarify
paulson@15409
   361
apply (rule_tac x=k in image_eqI, auto)
paulson@15409
   362
done
paulson@15409
   363
paulson@15409
   364
nipkow@15392
   365
text {* The powerset of a finite set *}
wenzelm@12396
   366
wenzelm@12396
   367
lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
wenzelm@12396
   368
proof
wenzelm@12396
   369
  assume "finite (Pow A)"
wenzelm@12396
   370
  with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
wenzelm@12396
   371
  thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
wenzelm@12396
   372
next
wenzelm@12396
   373
  assume "finite A"
wenzelm@12396
   374
  thus "finite (Pow A)"
wenzelm@12396
   375
    by induct (simp_all add: finite_UnI finite_imageI Pow_insert)
wenzelm@12396
   376
qed
wenzelm@12396
   377
nipkow@15392
   378
nipkow@15392
   379
lemma finite_UnionD: "finite(\<Union>A) \<Longrightarrow> finite A"
nipkow@15392
   380
by(blast intro: finite_subset[OF subset_Pow_Union])
nipkow@15392
   381
nipkow@15392
   382
wenzelm@12396
   383
lemma finite_converse [iff]: "finite (r^-1) = finite r"
wenzelm@12396
   384
  apply (subgoal_tac "r^-1 = (%(x,y). (y,x))`r")
wenzelm@12396
   385
   apply simp
wenzelm@12396
   386
   apply (rule iffI)
wenzelm@12396
   387
    apply (erule finite_imageD [unfolded inj_on_def])
wenzelm@12396
   388
    apply (simp split add: split_split)
wenzelm@12396
   389
   apply (erule finite_imageI)
paulson@14208
   390
  apply (simp add: converse_def image_def, auto)
wenzelm@12396
   391
  apply (rule bexI)
wenzelm@12396
   392
   prefer 2 apply assumption
wenzelm@12396
   393
  apply simp
wenzelm@12396
   394
  done
wenzelm@12396
   395
paulson@14430
   396
nipkow@15392
   397
text {* \paragraph{Finiteness of transitive closure} (Thanks to Sidi
nipkow@15392
   398
Ehmety) *}
wenzelm@12396
   399
wenzelm@12396
   400
lemma finite_Field: "finite r ==> finite (Field r)"
wenzelm@12396
   401
  -- {* A finite relation has a finite field (@{text "= domain \<union> range"}. *}
berghofe@22262
   402
  apply (induct set: finite)
wenzelm@12396
   403
   apply (auto simp add: Field_def Domain_insert Range_insert)
wenzelm@12396
   404
  done
wenzelm@12396
   405
wenzelm@12396
   406
lemma trancl_subset_Field2: "r^+ <= Field r \<times> Field r"
wenzelm@12396
   407
  apply clarify
wenzelm@12396
   408
  apply (erule trancl_induct)
wenzelm@12396
   409
   apply (auto simp add: Field_def)
wenzelm@12396
   410
  done
wenzelm@12396
   411
wenzelm@12396
   412
lemma finite_trancl: "finite (r^+) = finite r"
wenzelm@12396
   413
  apply auto
wenzelm@12396
   414
   prefer 2
wenzelm@12396
   415
   apply (rule trancl_subset_Field2 [THEN finite_subset])
wenzelm@12396
   416
   apply (rule finite_SigmaI)
wenzelm@12396
   417
    prefer 3
berghofe@13704
   418
    apply (blast intro: r_into_trancl' finite_subset)
wenzelm@12396
   419
   apply (auto simp add: finite_Field)
wenzelm@12396
   420
  done
wenzelm@12396
   421
wenzelm@12396
   422
nipkow@15392
   423
subsection {* A fold functional for finite sets *}
nipkow@15392
   424
nipkow@15392
   425
text {* The intended behaviour is
nipkow@15480
   426
@{text "fold f g z {x\<^isub>1, ..., x\<^isub>n} = f (g x\<^isub>1) (\<dots> (f (g x\<^isub>n) z)\<dots>)"}
nipkow@15392
   427
if @{text f} is associative-commutative. For an application of @{text fold}
nipkow@15392
   428
se the definitions of sums and products over finite sets.
nipkow@15392
   429
*}
nipkow@15392
   430
berghofe@23736
   431
inductive
berghofe@22262
   432
  foldSet :: "('a => 'a => 'a) => ('b => 'a) => 'a => 'b set => 'a => bool"
berghofe@22262
   433
  for f ::  "'a => 'a => 'a"
berghofe@22262
   434
  and g :: "'b => 'a"
berghofe@22262
   435
  and z :: 'a
berghofe@22262
   436
where
berghofe@22262
   437
  emptyI [intro]: "foldSet f g z {} z"
berghofe@22262
   438
| insertI [intro]:
berghofe@22262
   439
     "\<lbrakk> x \<notin> A; foldSet f g z A y \<rbrakk>
berghofe@22262
   440
      \<Longrightarrow> foldSet f g z (insert x A) (f (g x) y)"
berghofe@22262
   441
berghofe@23736
   442
inductive_cases empty_foldSetE [elim!]: "foldSet f g z {} x"
nipkow@15392
   443
nipkow@15392
   444
constdefs
nipkow@21733
   445
  fold :: "('a => 'a => 'a) => ('b => 'a) => 'a => 'b set => 'a"
berghofe@22262
   446
  "fold f g z A == THE x. foldSet f g z A x"
nipkow@15392
   447
paulson@15498
   448
text{*A tempting alternative for the definiens is
berghofe@22262
   449
@{term "if finite A then THE x. foldSet f g e A x else e"}.
paulson@15498
   450
It allows the removal of finiteness assumptions from the theorems
paulson@15498
   451
@{text fold_commute}, @{text fold_reindex} and @{text fold_distrib}.
paulson@15498
   452
The proofs become ugly, with @{text rule_format}. It is not worth the effort.*}
paulson@15498
   453
paulson@15498
   454
nipkow@15392
   455
lemma Diff1_foldSet:
berghofe@22262
   456
  "foldSet f g z (A - {x}) y ==> x: A ==> foldSet f g z A (f (g x) y)"
nipkow@15392
   457
by (erule insert_Diff [THEN subst], rule foldSet.intros, auto)
nipkow@15392
   458
berghofe@22262
   459
lemma foldSet_imp_finite: "foldSet f g z A x==> finite A"
nipkow@15392
   460
  by (induct set: foldSet) auto
nipkow@15392
   461
berghofe@22262
   462
lemma finite_imp_foldSet: "finite A ==> EX x. foldSet f g z A x"
berghofe@22262
   463
  by (induct set: finite) auto
nipkow@15392
   464
nipkow@15392
   465
nipkow@15392
   466
subsubsection {* Commutative monoids *}
nipkow@15480
   467
haftmann@22917
   468
(*FIXME integrate with Orderings.thy/OrderedGroup.thy*)
nipkow@15392
   469
locale ACf =
nipkow@15392
   470
  fixes f :: "'a => 'a => 'a"    (infixl "\<cdot>" 70)
nipkow@15392
   471
  assumes commute: "x \<cdot> y = y \<cdot> x"
nipkow@15392
   472
    and assoc: "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)"
haftmann@22917
   473
begin
haftmann@22917
   474
haftmann@22917
   475
lemma left_commute: "x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
nipkow@15392
   476
proof -
nipkow@15392
   477
  have "x \<cdot> (y \<cdot> z) = (y \<cdot> z) \<cdot> x" by (simp only: commute)
nipkow@15392
   478
  also have "... = y \<cdot> (z \<cdot> x)" by (simp only: assoc)
nipkow@15392
   479
  also have "z \<cdot> x = x \<cdot> z" by (simp only: commute)
nipkow@15392
   480
  finally show ?thesis .
nipkow@15392
   481
qed
nipkow@15392
   482
haftmann@22917
   483
lemmas AC = assoc commute left_commute
haftmann@22917
   484
haftmann@22917
   485
end
haftmann@22917
   486
haftmann@22917
   487
locale ACe = ACf +
haftmann@22917
   488
  fixes e :: 'a
haftmann@22917
   489
  assumes ident [simp]: "x \<cdot> e = x"
haftmann@22917
   490
begin
haftmann@22917
   491
haftmann@22917
   492
lemma left_ident [simp]: "e \<cdot> x = x"
nipkow@15392
   493
proof -
nipkow@15392
   494
  have "x \<cdot> e = x" by (rule ident)
nipkow@15392
   495
  thus ?thesis by (subst commute)
nipkow@15392
   496
qed
nipkow@15392
   497
haftmann@22917
   498
end
haftmann@22917
   499
haftmann@22917
   500
locale ACIf = ACf +
haftmann@22917
   501
  assumes idem: "x \<cdot> x = x"
haftmann@22917
   502
begin
haftmann@22917
   503
haftmann@22917
   504
lemma idem2: "x \<cdot> (x \<cdot> y) = x \<cdot> y"
nipkow@15497
   505
proof -
nipkow@15497
   506
  have "x \<cdot> (x \<cdot> y) = (x \<cdot> x) \<cdot> y" by(simp add:assoc)
nipkow@15497
   507
  also have "\<dots> = x \<cdot> y" by(simp add:idem)
nipkow@15497
   508
  finally show ?thesis .
nipkow@15497
   509
qed
nipkow@15497
   510
haftmann@22917
   511
lemmas ACI = AC idem idem2
haftmann@22917
   512
haftmann@22917
   513
end
haftmann@22917
   514
nipkow@15402
   515
nipkow@15392
   516
subsubsection{*From @{term foldSet} to @{term fold}*}
nipkow@15392
   517
paulson@15510
   518
lemma image_less_Suc: "h ` {i. i < Suc m} = insert (h m) (h ` {i. i < m})"
wenzelm@19868
   519
  by (auto simp add: less_Suc_eq) 
paulson@15510
   520
paulson@15510
   521
lemma insert_image_inj_on_eq:
paulson@15510
   522
     "[|insert (h m) A = h ` {i. i < Suc m}; h m \<notin> A; 
paulson@15510
   523
        inj_on h {i. i < Suc m}|] 
paulson@15510
   524
      ==> A = h ` {i. i < m}"
paulson@15510
   525
apply (auto simp add: image_less_Suc inj_on_def)
paulson@15510
   526
apply (blast intro: less_trans) 
paulson@15510
   527
done
paulson@15510
   528
paulson@15510
   529
lemma insert_inj_onE:
paulson@15510
   530
  assumes aA: "insert a A = h`{i::nat. i<n}" and anot: "a \<notin> A" 
paulson@15510
   531
      and inj_on: "inj_on h {i::nat. i<n}"
paulson@15510
   532
  shows "\<exists>hm m. inj_on hm {i::nat. i<m} & A = hm ` {i. i<m} & m < n"
paulson@15510
   533
proof (cases n)
paulson@15510
   534
  case 0 thus ?thesis using aA by auto
paulson@15510
   535
next
paulson@15510
   536
  case (Suc m)
wenzelm@23389
   537
  have nSuc: "n = Suc m" by fact
paulson@15510
   538
  have mlessn: "m<n" by (simp add: nSuc)
paulson@15532
   539
  from aA obtain k where hkeq: "h k = a" and klessn: "k<n" by (blast elim!: equalityE)
paulson@15520
   540
  let ?hm = "swap k m h"
paulson@15520
   541
  have inj_hm: "inj_on ?hm {i. i < n}" using klessn mlessn 
paulson@15520
   542
    by (simp add: inj_on_swap_iff inj_on)
paulson@15510
   543
  show ?thesis
paulson@15520
   544
  proof (intro exI conjI)
paulson@15520
   545
    show "inj_on ?hm {i. i < m}" using inj_hm
paulson@15510
   546
      by (auto simp add: nSuc less_Suc_eq intro: subset_inj_on)
paulson@15520
   547
    show "m<n" by (rule mlessn)
paulson@15520
   548
    show "A = ?hm ` {i. i < m}" 
paulson@15520
   549
    proof (rule insert_image_inj_on_eq)
paulson@15520
   550
      show "inj_on (swap k m h) {i. i < Suc m}" using inj_hm nSuc by simp
paulson@15520
   551
      show "?hm m \<notin> A" by (simp add: swap_def hkeq anot) 
paulson@15520
   552
      show "insert (?hm m) A = ?hm ` {i. i < Suc m}"
paulson@15520
   553
	using aA hkeq nSuc klessn
paulson@15520
   554
	by (auto simp add: swap_def image_less_Suc fun_upd_image 
paulson@15520
   555
			   less_Suc_eq inj_on_image_set_diff [OF inj_on])
nipkow@15479
   556
    qed
nipkow@15479
   557
  qed
nipkow@15479
   558
qed
nipkow@15479
   559
nipkow@15392
   560
lemma (in ACf) foldSet_determ_aux:
paulson@15510
   561
  "!!A x x' h. \<lbrakk> A = h`{i::nat. i<n}; inj_on h {i. i<n}; 
berghofe@22262
   562
                foldSet f g z A x; foldSet f g z A x' \<rbrakk>
nipkow@15392
   563
   \<Longrightarrow> x' = x"
paulson@15510
   564
proof (induct n rule: less_induct)
paulson@15510
   565
  case (less n)
paulson@15510
   566
    have IH: "!!m h A x x'. 
paulson@15510
   567
               \<lbrakk>m<n; A = h ` {i. i<m}; inj_on h {i. i<m}; 
wenzelm@23389
   568
                foldSet f g z A x; foldSet f g z A x'\<rbrakk> \<Longrightarrow> x' = x" by fact
berghofe@22262
   569
    have Afoldx: "foldSet f g z A x" and Afoldx': "foldSet f g z A x'"
wenzelm@23389
   570
     and A: "A = h`{i. i<n}" and injh: "inj_on h {i. i<n}" by fact+
paulson@15510
   571
    show ?case
paulson@15510
   572
    proof (rule foldSet.cases [OF Afoldx])
berghofe@22262
   573
      assume "A = {}" and "x = z"
paulson@15510
   574
      with Afoldx' show "x' = x" by blast
nipkow@15392
   575
    next
paulson@15510
   576
      fix B b u
berghofe@22262
   577
      assume AbB: "A = insert b B" and x: "x = g b \<cdot> u"
berghofe@22262
   578
         and notinB: "b \<notin> B" and Bu: "foldSet f g z B u"
paulson@15510
   579
      show "x'=x" 
paulson@15510
   580
      proof (rule foldSet.cases [OF Afoldx'])
berghofe@22262
   581
        assume "A = {}" and "x' = z"
paulson@15510
   582
        with AbB show "x' = x" by blast
nipkow@15392
   583
      next
paulson@15510
   584
	fix C c v
berghofe@22262
   585
	assume AcC: "A = insert c C" and x': "x' = g c \<cdot> v"
berghofe@22262
   586
           and notinC: "c \<notin> C" and Cv: "foldSet f g z C v"
paulson@15510
   587
	from A AbB have Beq: "insert b B = h`{i. i<n}" by simp
paulson@15510
   588
        from insert_inj_onE [OF Beq notinB injh]
paulson@15510
   589
        obtain hB mB where inj_onB: "inj_on hB {i. i < mB}" 
paulson@15510
   590
                     and Beq: "B = hB ` {i. i < mB}"
paulson@15510
   591
                     and lessB: "mB < n" by auto 
paulson@15510
   592
	from A AcC have Ceq: "insert c C = h`{i. i<n}" by simp
paulson@15510
   593
        from insert_inj_onE [OF Ceq notinC injh]
paulson@15510
   594
        obtain hC mC where inj_onC: "inj_on hC {i. i < mC}"
paulson@15510
   595
                       and Ceq: "C = hC ` {i. i < mC}"
paulson@15510
   596
                       and lessC: "mC < n" by auto 
paulson@15510
   597
	show "x'=x"
nipkow@15392
   598
	proof cases
paulson@15510
   599
          assume "b=c"
paulson@15510
   600
	  then moreover have "B = C" using AbB AcC notinB notinC by auto
paulson@15510
   601
	  ultimately show ?thesis  using Bu Cv x x' IH[OF lessC Ceq inj_onC]
paulson@15510
   602
            by auto
nipkow@15392
   603
	next
nipkow@15392
   604
	  assume diff: "b \<noteq> c"
nipkow@15392
   605
	  let ?D = "B - {c}"
nipkow@15392
   606
	  have B: "B = insert c ?D" and C: "C = insert b ?D"
paulson@15510
   607
	    using AbB AcC notinB notinC diff by(blast elim!:equalityE)+
nipkow@15402
   608
	  have "finite A" by(rule foldSet_imp_finite[OF Afoldx])
paulson@15510
   609
	  with AbB have "finite ?D" by simp
berghofe@22262
   610
	  then obtain d where Dfoldd: "foldSet f g z ?D d"
nipkow@17589
   611
	    using finite_imp_foldSet by iprover
paulson@15506
   612
	  moreover have cinB: "c \<in> B" using B by auto
berghofe@22262
   613
	  ultimately have "foldSet f g z B (g c \<cdot> d)"
nipkow@15392
   614
	    by(rule Diff1_foldSet)
paulson@15510
   615
	  hence "g c \<cdot> d = u" by (rule IH [OF lessB Beq inj_onB Bu]) 
paulson@15510
   616
          moreover have "g b \<cdot> d = v"
paulson@15510
   617
	  proof (rule IH[OF lessC Ceq inj_onC Cv])
berghofe@22262
   618
	    show "foldSet f g z C (g b \<cdot> d)" using C notinB Dfoldd
nipkow@15392
   619
	      by fastsimp
nipkow@15392
   620
	  qed
paulson@15510
   621
	  ultimately show ?thesis using x x' by (auto simp: AC)
nipkow@15392
   622
	qed
nipkow@15392
   623
      qed
nipkow@15392
   624
    qed
nipkow@15392
   625
  qed
nipkow@15392
   626
nipkow@15392
   627
nipkow@15392
   628
lemma (in ACf) foldSet_determ:
berghofe@22262
   629
  "foldSet f g z A x ==> foldSet f g z A y ==> y = x"
paulson@15510
   630
apply (frule foldSet_imp_finite [THEN finite_imp_nat_seg_image_inj_on]) 
paulson@15510
   631
apply (blast intro: foldSet_determ_aux [rule_format])
nipkow@15392
   632
done
nipkow@15392
   633
berghofe@22262
   634
lemma (in ACf) fold_equality: "foldSet f g z A y ==> fold f g z A = y"
nipkow@15392
   635
  by (unfold fold_def) (blast intro: foldSet_determ)
nipkow@15392
   636
nipkow@15392
   637
text{* The base case for @{text fold}: *}
nipkow@15392
   638
nipkow@15480
   639
lemma fold_empty [simp]: "fold f g z {} = z"
nipkow@15392
   640
  by (unfold fold_def) blast
nipkow@15392
   641
nipkow@15392
   642
lemma (in ACf) fold_insert_aux: "x \<notin> A ==>
berghofe@22262
   643
    (foldSet f g z (insert x A) v) =
berghofe@22262
   644
    (EX y. foldSet f g z A y & v = f (g x) y)"
nipkow@15392
   645
  apply auto
nipkow@15392
   646
  apply (rule_tac A1 = A and f1 = f in finite_imp_foldSet [THEN exE])
nipkow@15392
   647
   apply (fastsimp dest: foldSet_imp_finite)
nipkow@15392
   648
  apply (blast intro: foldSet_determ)
nipkow@15392
   649
  done
nipkow@15392
   650
nipkow@15392
   651
text{* The recursion equation for @{text fold}: *}
nipkow@15392
   652
nipkow@15392
   653
lemma (in ACf) fold_insert[simp]:
nipkow@15480
   654
    "finite A ==> x \<notin> A ==> fold f g z (insert x A) = f (g x) (fold f g z A)"
nipkow@15392
   655
  apply (unfold fold_def)
nipkow@15392
   656
  apply (simp add: fold_insert_aux)
nipkow@15392
   657
  apply (rule the_equality)
nipkow@15392
   658
  apply (auto intro: finite_imp_foldSet
nipkow@15392
   659
    cong add: conj_cong simp add: fold_def [symmetric] fold_equality)
nipkow@15392
   660
  done
nipkow@15392
   661
nipkow@15535
   662
lemma (in ACf) fold_rec:
nipkow@15535
   663
assumes fin: "finite A" and a: "a:A"
nipkow@15535
   664
shows "fold f g z A = f (g a) (fold f g z (A - {a}))"
nipkow@15535
   665
proof-
nipkow@15535
   666
  have A: "A = insert a (A - {a})" using a by blast
nipkow@15535
   667
  hence "fold f g z A = fold f g z (insert a (A - {a}))" by simp
nipkow@15535
   668
  also have "\<dots> = f (g a) (fold f g z (A - {a}))"
nipkow@15535
   669
    by(rule fold_insert) (simp add:fin)+
nipkow@15535
   670
  finally show ?thesis .
nipkow@15535
   671
qed
nipkow@15535
   672
nipkow@15392
   673
nipkow@15480
   674
text{* A simplified version for idempotent functions: *}
nipkow@15480
   675
paulson@15509
   676
lemma (in ACIf) fold_insert_idem:
nipkow@15480
   677
assumes finA: "finite A"
paulson@15508
   678
shows "fold f g z (insert a A) = g a \<cdot> fold f g z A"
nipkow@15480
   679
proof cases
nipkow@15480
   680
  assume "a \<in> A"
nipkow@15480
   681
  then obtain B where A: "A = insert a B" and disj: "a \<notin> B"
nipkow@15480
   682
    by(blast dest: mk_disjoint_insert)
nipkow@15480
   683
  show ?thesis
nipkow@15480
   684
  proof -
nipkow@15480
   685
    from finA A have finB: "finite B" by(blast intro: finite_subset)
nipkow@15480
   686
    have "fold f g z (insert a A) = fold f g z (insert a B)" using A by simp
nipkow@15480
   687
    also have "\<dots> = (g a) \<cdot> (fold f g z B)"
paulson@15506
   688
      using finB disj by simp
nipkow@15480
   689
    also have "\<dots> = g a \<cdot> fold f g z A"
nipkow@15480
   690
      using A finB disj by(simp add:idem assoc[symmetric])
nipkow@15480
   691
    finally show ?thesis .
nipkow@15480
   692
  qed
nipkow@15480
   693
next
nipkow@15480
   694
  assume "a \<notin> A"
nipkow@15480
   695
  with finA show ?thesis by simp
nipkow@15480
   696
qed
nipkow@15480
   697
nipkow@15484
   698
lemma (in ACIf) foldI_conv_id:
nipkow@15484
   699
  "finite A \<Longrightarrow> fold f g z A = fold f id z (g ` A)"
paulson@15509
   700
by(erule finite_induct)(simp_all add: fold_insert_idem del: fold_insert)
nipkow@15484
   701
nipkow@15392
   702
subsubsection{*Lemmas about @{text fold}*}
nipkow@15392
   703
nipkow@15392
   704
lemma (in ACf) fold_commute:
paulson@15487
   705
  "finite A ==> (!!z. f x (fold f g z A) = fold f g (f x z) A)"
berghofe@22262
   706
  apply (induct set: finite)
wenzelm@21575
   707
   apply simp
paulson@15487
   708
  apply (simp add: left_commute [of x])
nipkow@15392
   709
  done
nipkow@15392
   710
nipkow@15392
   711
lemma (in ACf) fold_nest_Un_Int:
nipkow@15392
   712
  "finite A ==> finite B
nipkow@15480
   713
    ==> fold f g (fold f g z B) A = fold f g (fold f g z (A Int B)) (A Un B)"
berghofe@22262
   714
  apply (induct set: finite)
wenzelm@21575
   715
   apply simp
nipkow@15392
   716
  apply (simp add: fold_commute Int_insert_left insert_absorb)
nipkow@15392
   717
  done
nipkow@15392
   718
nipkow@15392
   719
lemma (in ACf) fold_nest_Un_disjoint:
nipkow@15392
   720
  "finite A ==> finite B ==> A Int B = {}
nipkow@15480
   721
    ==> fold f g z (A Un B) = fold f g (fold f g z B) A"
nipkow@15392
   722
  by (simp add: fold_nest_Un_Int)
nipkow@15392
   723
nipkow@15392
   724
lemma (in ACf) fold_reindex:
paulson@15487
   725
assumes fin: "finite A"
paulson@15487
   726
shows "inj_on h A \<Longrightarrow> fold f g z (h ` A) = fold f (g \<circ> h) z A"
paulson@15506
   727
using fin apply induct
nipkow@15392
   728
 apply simp
nipkow@15392
   729
apply simp
nipkow@15392
   730
done
nipkow@15392
   731
nipkow@15392
   732
lemma (in ACe) fold_Un_Int:
nipkow@15392
   733
  "finite A ==> finite B ==>
nipkow@15392
   734
    fold f g e A \<cdot> fold f g e B =
nipkow@15392
   735
    fold f g e (A Un B) \<cdot> fold f g e (A Int B)"
berghofe@22262
   736
  apply (induct set: finite, simp)
nipkow@15392
   737
  apply (simp add: AC insert_absorb Int_insert_left)
nipkow@15392
   738
  done
nipkow@15392
   739
nipkow@15392
   740
corollary (in ACe) fold_Un_disjoint:
nipkow@15392
   741
  "finite A ==> finite B ==> A Int B = {} ==>
nipkow@15392
   742
    fold f g e (A Un B) = fold f g e A \<cdot> fold f g e B"
nipkow@15392
   743
  by (simp add: fold_Un_Int)
nipkow@15392
   744
nipkow@15392
   745
lemma (in ACe) fold_UN_disjoint:
nipkow@15392
   746
  "\<lbrakk> finite I; ALL i:I. finite (A i);
nipkow@15392
   747
     ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {} \<rbrakk>
nipkow@15392
   748
   \<Longrightarrow> fold f g e (UNION I A) =
nipkow@15392
   749
       fold f (%i. fold f g e (A i)) e I"
berghofe@22262
   750
  apply (induct set: finite, simp, atomize)
nipkow@15392
   751
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
nipkow@15392
   752
   prefer 2 apply blast
nipkow@15392
   753
  apply (subgoal_tac "A x Int UNION F A = {}")
nipkow@15392
   754
   prefer 2 apply blast
nipkow@15392
   755
  apply (simp add: fold_Un_disjoint)
nipkow@15392
   756
  done
nipkow@15392
   757
paulson@15506
   758
text{*Fusion theorem, as described in
paulson@15506
   759
Graham Hutton's paper,
paulson@15506
   760
A Tutorial on the Universality and Expressiveness of Fold,
paulson@15506
   761
JFP 9:4 (355-372), 1999.*}
paulson@15506
   762
lemma (in ACf) fold_fusion:
paulson@15506
   763
      includes ACf g
paulson@15506
   764
      shows
paulson@15506
   765
	"finite A ==> 
paulson@15506
   766
	 (!!x y. h (g x y) = f x (h y)) ==>
paulson@15506
   767
         h (fold g j w A) = fold f j (h w) A"
berghofe@22262
   768
  by (induct set: finite) simp_all
paulson@15506
   769
nipkow@15392
   770
lemma (in ACf) fold_cong:
nipkow@15480
   771
  "finite A \<Longrightarrow> (!!x. x:A ==> g x = h x) ==> fold f g z A = fold f h z A"
nipkow@15480
   772
  apply (subgoal_tac "ALL C. C <= A --> (ALL x:C. g x = h x) --> fold f g z C = fold f h z C")
nipkow@15392
   773
   apply simp
nipkow@15392
   774
  apply (erule finite_induct, simp)
nipkow@15392
   775
  apply (simp add: subset_insert_iff, clarify)
nipkow@15392
   776
  apply (subgoal_tac "finite C")
nipkow@15392
   777
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
nipkow@15392
   778
  apply (subgoal_tac "C = insert x (C - {x})")
nipkow@15392
   779
   prefer 2 apply blast
nipkow@15392
   780
  apply (erule ssubst)
nipkow@15392
   781
  apply (drule spec)
nipkow@15392
   782
  apply (erule (1) notE impE)
nipkow@15392
   783
  apply (simp add: Ball_def del: insert_Diff_single)
nipkow@15392
   784
  done
nipkow@15392
   785
nipkow@15392
   786
lemma (in ACe) fold_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@15392
   787
  fold f (%x. fold f (g x) e (B x)) e A =
nipkow@15392
   788
  fold f (split g) e (SIGMA x:A. B x)"
nipkow@15392
   789
apply (subst Sigma_def)
paulson@15506
   790
apply (subst fold_UN_disjoint, assumption, simp)
nipkow@15392
   791
 apply blast
nipkow@15392
   792
apply (erule fold_cong)
paulson@15506
   793
apply (subst fold_UN_disjoint, simp, simp)
nipkow@15392
   794
 apply blast
paulson@15506
   795
apply simp
nipkow@15392
   796
done
nipkow@15392
   797
nipkow@15392
   798
lemma (in ACe) fold_distrib: "finite A \<Longrightarrow>
nipkow@15392
   799
   fold f (%x. f (g x) (h x)) e A = f (fold f g e A) (fold f h e A)"
paulson@15506
   800
apply (erule finite_induct, simp)
nipkow@15392
   801
apply (simp add:AC)
nipkow@15392
   802
done
nipkow@15392
   803
nipkow@15392
   804
haftmann@22917
   805
text{* Interpretation of locales -- see OrderedGroup.thy *}
haftmann@22917
   806
haftmann@22917
   807
interpretation AC_add: ACe ["op +" "0::'a::comm_monoid_add"]
haftmann@22917
   808
  by unfold_locales (auto intro: add_assoc add_commute)
haftmann@22917
   809
haftmann@22917
   810
interpretation AC_mult: ACe ["op *" "1::'a::comm_monoid_mult"]
haftmann@22917
   811
  by unfold_locales (auto intro: mult_assoc mult_commute)
haftmann@22917
   812
haftmann@22917
   813
nipkow@15402
   814
subsection {* Generalized summation over a set *}
nipkow@15402
   815
nipkow@15402
   816
constdefs
nipkow@15402
   817
  setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
nipkow@15402
   818
  "setsum f A == if finite A then fold (op +) f 0 A else 0"
nipkow@15402
   819
wenzelm@19535
   820
abbreviation
wenzelm@21404
   821
  Setsum  ("\<Sum>_" [1000] 999) where
wenzelm@19535
   822
  "\<Sum>A == setsum (%x. x) A"
wenzelm@19535
   823
nipkow@15402
   824
text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
nipkow@15402
   825
written @{text"\<Sum>x\<in>A. e"}. *}
nipkow@15402
   826
nipkow@15402
   827
syntax
paulson@17189
   828
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
nipkow@15402
   829
syntax (xsymbols)
paulson@17189
   830
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   831
syntax (HTML output)
paulson@17189
   832
  "_setsum" :: "pttrn => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   833
nipkow@15402
   834
translations -- {* Beware of argument permutation! *}
nipkow@15402
   835
  "SUM i:A. b" == "setsum (%i. b) A"
nipkow@15402
   836
  "\<Sum>i\<in>A. b" == "setsum (%i. b) A"
nipkow@15402
   837
nipkow@15402
   838
text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
nipkow@15402
   839
 @{text"\<Sum>x|P. e"}. *}
nipkow@15402
   840
nipkow@15402
   841
syntax
paulson@17189
   842
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
nipkow@15402
   843
syntax (xsymbols)
paulson@17189
   844
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   845
syntax (HTML output)
paulson@17189
   846
  "_qsetsum" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   847
nipkow@15402
   848
translations
nipkow@15402
   849
  "SUM x|P. t" => "setsum (%x. t) {x. P}"
nipkow@15402
   850
  "\<Sum>x|P. t" => "setsum (%x. t) {x. P}"
nipkow@15402
   851
nipkow@15402
   852
print_translation {*
nipkow@15402
   853
let
wenzelm@19535
   854
  fun setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) $ Abs(y,Ty,P)] = 
wenzelm@19535
   855
    if x<>y then raise Match
wenzelm@19535
   856
    else let val x' = Syntax.mark_bound x
wenzelm@19535
   857
             val t' = subst_bound(x',t)
wenzelm@19535
   858
             val P' = subst_bound(x',P)
wenzelm@19535
   859
         in Syntax.const "_qsetsum" $ Syntax.mark_bound x $ P' $ t' end
wenzelm@19535
   860
in [("setsum", setsum_tr')] end
nipkow@15402
   861
*}
nipkow@15402
   862
wenzelm@19535
   863
nipkow@15402
   864
lemma setsum_empty [simp]: "setsum f {} = 0"
nipkow@15402
   865
  by (simp add: setsum_def)
nipkow@15402
   866
nipkow@15402
   867
lemma setsum_insert [simp]:
nipkow@15402
   868
    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
ballarin@15765
   869
  by (simp add: setsum_def)
nipkow@15402
   870
paulson@15409
   871
lemma setsum_infinite [simp]: "~ finite A ==> setsum f A = 0"
paulson@15409
   872
  by (simp add: setsum_def)
paulson@15409
   873
nipkow@15402
   874
lemma setsum_reindex:
nipkow@15402
   875
     "inj_on f B ==> setsum h (f ` B) = setsum (h \<circ> f) B"
ballarin@15765
   876
by(auto simp add: setsum_def AC_add.fold_reindex dest!:finite_imageD)
nipkow@15402
   877
nipkow@15402
   878
lemma setsum_reindex_id:
nipkow@15402
   879
     "inj_on f B ==> setsum f B = setsum id (f ` B)"
nipkow@15402
   880
by (auto simp add: setsum_reindex)
nipkow@15402
   881
nipkow@15402
   882
lemma setsum_cong:
nipkow@15402
   883
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
ballarin@15765
   884
by(fastsimp simp: setsum_def intro: AC_add.fold_cong)
nipkow@15402
   885
nipkow@16733
   886
lemma strong_setsum_cong[cong]:
nipkow@16733
   887
  "A = B ==> (!!x. x:B =simp=> f x = g x)
nipkow@16733
   888
   ==> setsum (%x. f x) A = setsum (%x. g x) B"
berghofe@16632
   889
by(fastsimp simp: simp_implies_def setsum_def intro: AC_add.fold_cong)
berghofe@16632
   890
nipkow@15554
   891
lemma setsum_cong2: "\<lbrakk>\<And>x. x \<in> A \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> setsum f A = setsum g A";
nipkow@15554
   892
  by (rule setsum_cong[OF refl], auto);
nipkow@15554
   893
nipkow@15402
   894
lemma setsum_reindex_cong:
nipkow@15554
   895
     "[|inj_on f A; B = f ` A; !!a. a:A \<Longrightarrow> g a = h (f a)|] 
nipkow@15402
   896
      ==> setsum h B = setsum g A"
nipkow@15402
   897
  by (simp add: setsum_reindex cong: setsum_cong)
nipkow@15402
   898
nipkow@15542
   899
lemma setsum_0[simp]: "setsum (%i. 0) A = 0"
nipkow@15402
   900
apply (clarsimp simp: setsum_def)
ballarin@15765
   901
apply (erule finite_induct, auto)
nipkow@15402
   902
done
nipkow@15402
   903
nipkow@15543
   904
lemma setsum_0': "ALL a:A. f a = 0 ==> setsum f A = 0"
nipkow@15543
   905
by(simp add:setsum_cong)
nipkow@15402
   906
nipkow@15402
   907
lemma setsum_Un_Int: "finite A ==> finite B ==>
nipkow@15402
   908
  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
nipkow@15402
   909
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
ballarin@15765
   910
by(simp add: setsum_def AC_add.fold_Un_Int [symmetric])
nipkow@15402
   911
nipkow@15402
   912
lemma setsum_Un_disjoint: "finite A ==> finite B
nipkow@15402
   913
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
nipkow@15402
   914
by (subst setsum_Un_Int [symmetric], auto)
nipkow@15402
   915
paulson@15409
   916
(*But we can't get rid of finite I. If infinite, although the rhs is 0, 
paulson@15409
   917
  the lhs need not be, since UNION I A could still be finite.*)
nipkow@15402
   918
lemma setsum_UN_disjoint:
nipkow@15402
   919
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
   920
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
   921
      setsum f (UNION I A) = (\<Sum>i\<in>I. setsum f (A i))"
ballarin@15765
   922
by(simp add: setsum_def AC_add.fold_UN_disjoint cong: setsum_cong)
nipkow@15402
   923
paulson@15409
   924
text{*No need to assume that @{term C} is finite.  If infinite, the rhs is
paulson@15409
   925
directly 0, and @{term "Union C"} is also infinite, hence the lhs is also 0.*}
nipkow@15402
   926
lemma setsum_Union_disjoint:
paulson@15409
   927
  "[| (ALL A:C. finite A);
paulson@15409
   928
      (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) |]
paulson@15409
   929
   ==> setsum f (Union C) = setsum (setsum f) C"
paulson@15409
   930
apply (cases "finite C") 
paulson@15409
   931
 prefer 2 apply (force dest: finite_UnionD simp add: setsum_def)
nipkow@15402
   932
  apply (frule setsum_UN_disjoint [of C id f])
paulson@15409
   933
 apply (unfold Union_def id_def, assumption+)
paulson@15409
   934
done
nipkow@15402
   935
paulson@15409
   936
(*But we can't get rid of finite A. If infinite, although the lhs is 0, 
paulson@15409
   937
  the rhs need not be, since SIGMA A B could still be finite.*)
nipkow@15402
   938
lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
paulson@17189
   939
    (\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) = (\<Sum>(x,y)\<in>(SIGMA x:A. B x). f x y)"
ballarin@15765
   940
by(simp add:setsum_def AC_add.fold_Sigma split_def cong:setsum_cong)
nipkow@15402
   941
paulson@15409
   942
text{*Here we can eliminate the finiteness assumptions, by cases.*}
paulson@15409
   943
lemma setsum_cartesian_product: 
paulson@17189
   944
   "(\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) = (\<Sum>(x,y) \<in> A <*> B. f x y)"
paulson@15409
   945
apply (cases "finite A") 
paulson@15409
   946
 apply (cases "finite B") 
paulson@15409
   947
  apply (simp add: setsum_Sigma)
paulson@15409
   948
 apply (cases "A={}", simp)
nipkow@15543
   949
 apply (simp) 
paulson@15409
   950
apply (auto simp add: setsum_def
paulson@15409
   951
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
   952
done
nipkow@15402
   953
nipkow@15402
   954
lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
ballarin@15765
   955
by(simp add:setsum_def AC_add.fold_distrib)
nipkow@15402
   956
nipkow@15402
   957
nipkow@15402
   958
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
   959
nipkow@15402
   960
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
nipkow@15402
   961
  apply (case_tac "finite A")
nipkow@15402
   962
   prefer 2 apply (simp add: setsum_def)
nipkow@15402
   963
  apply (erule rev_mp)
nipkow@15402
   964
  apply (erule finite_induct, auto)
nipkow@15402
   965
  done
nipkow@15402
   966
nipkow@15402
   967
lemma setsum_eq_0_iff [simp]:
nipkow@15402
   968
    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
berghofe@22262
   969
  by (induct set: finite) auto
nipkow@15402
   970
nipkow@15402
   971
lemma setsum_Un_nat: "finite A ==> finite B ==>
nipkow@15402
   972
    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
nipkow@15402
   973
  -- {* For the natural numbers, we have subtraction. *}
nipkow@23477
   974
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_simps)
nipkow@15402
   975
nipkow@15402
   976
lemma setsum_Un: "finite A ==> finite B ==>
nipkow@15402
   977
    (setsum f (A Un B) :: 'a :: ab_group_add) =
nipkow@15402
   978
      setsum f A + setsum f B - setsum f (A Int B)"
nipkow@23477
   979
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_simps)
nipkow@15402
   980
nipkow@15402
   981
lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
nipkow@15402
   982
    (if a:A then setsum f A - f a else setsum f A)"
nipkow@15402
   983
  apply (case_tac "finite A")
nipkow@15402
   984
   prefer 2 apply (simp add: setsum_def)
nipkow@15402
   985
  apply (erule finite_induct)
nipkow@15402
   986
   apply (auto simp add: insert_Diff_if)
nipkow@15402
   987
  apply (drule_tac a = a in mk_disjoint_insert, auto)
nipkow@15402
   988
  done
nipkow@15402
   989
nipkow@15402
   990
lemma setsum_diff1: "finite A \<Longrightarrow>
nipkow@15402
   991
  (setsum f (A - {a}) :: ('a::ab_group_add)) =
nipkow@15402
   992
  (if a:A then setsum f A - f a else setsum f A)"
nipkow@15402
   993
  by (erule finite_induct) (auto simp add: insert_Diff_if)
nipkow@15402
   994
obua@15552
   995
lemma setsum_diff1'[rule_format]: "finite A \<Longrightarrow> a \<in> A \<longrightarrow> (\<Sum> x \<in> A. f x) = f a + (\<Sum> x \<in> (A - {a}). f x)"
obua@15552
   996
  apply (erule finite_induct[where F=A and P="% A. (a \<in> A \<longrightarrow> (\<Sum> x \<in> A. f x) = f a + (\<Sum> x \<in> (A - {a}). f x))"])
obua@15552
   997
  apply (auto simp add: insert_Diff_if add_ac)
obua@15552
   998
  done
obua@15552
   999
nipkow@15402
  1000
(* By Jeremy Siek: *)
nipkow@15402
  1001
nipkow@15402
  1002
lemma setsum_diff_nat: 
wenzelm@19535
  1003
  assumes "finite B"
wenzelm@19535
  1004
    and "B \<subseteq> A"
wenzelm@19535
  1005
  shows "(setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
wenzelm@19535
  1006
  using prems
wenzelm@19535
  1007
proof induct
nipkow@15402
  1008
  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
nipkow@15402
  1009
next
nipkow@15402
  1010
  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
nipkow@15402
  1011
    and xFinA: "insert x F \<subseteq> A"
nipkow@15402
  1012
    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
nipkow@15402
  1013
  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
nipkow@15402
  1014
  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
nipkow@15402
  1015
    by (simp add: setsum_diff1_nat)
nipkow@15402
  1016
  from xFinA have "F \<subseteq> A" by simp
nipkow@15402
  1017
  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
nipkow@15402
  1018
  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
nipkow@15402
  1019
    by simp
nipkow@15402
  1020
  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
nipkow@15402
  1021
  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
nipkow@15402
  1022
    by simp
nipkow@15402
  1023
  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
nipkow@15402
  1024
  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
nipkow@15402
  1025
    by simp
nipkow@15402
  1026
  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
nipkow@15402
  1027
qed
nipkow@15402
  1028
nipkow@15402
  1029
lemma setsum_diff:
nipkow@15402
  1030
  assumes le: "finite A" "B \<subseteq> A"
nipkow@15402
  1031
  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
nipkow@15402
  1032
proof -
nipkow@15402
  1033
  from le have finiteB: "finite B" using finite_subset by auto
nipkow@15402
  1034
  show ?thesis using finiteB le
wenzelm@21575
  1035
  proof induct
wenzelm@19535
  1036
    case empty
wenzelm@19535
  1037
    thus ?case by auto
wenzelm@19535
  1038
  next
wenzelm@19535
  1039
    case (insert x F)
wenzelm@19535
  1040
    thus ?case using le finiteB 
wenzelm@19535
  1041
      by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
nipkow@15402
  1042
  qed
wenzelm@19535
  1043
qed
nipkow@15402
  1044
nipkow@15402
  1045
lemma setsum_mono:
nipkow@15402
  1046
  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, pordered_ab_semigroup_add}))"
nipkow@15402
  1047
  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
nipkow@15402
  1048
proof (cases "finite K")
nipkow@15402
  1049
  case True
nipkow@15402
  1050
  thus ?thesis using le
wenzelm@19535
  1051
  proof induct
nipkow@15402
  1052
    case empty
nipkow@15402
  1053
    thus ?case by simp
nipkow@15402
  1054
  next
nipkow@15402
  1055
    case insert
wenzelm@19535
  1056
    thus ?case using add_mono by fastsimp
nipkow@15402
  1057
  qed
nipkow@15402
  1058
next
nipkow@15402
  1059
  case False
nipkow@15402
  1060
  thus ?thesis
nipkow@15402
  1061
    by (simp add: setsum_def)
nipkow@15402
  1062
qed
nipkow@15402
  1063
nipkow@15554
  1064
lemma setsum_strict_mono:
wenzelm@19535
  1065
  fixes f :: "'a \<Rightarrow> 'b::{pordered_cancel_ab_semigroup_add,comm_monoid_add}"
wenzelm@19535
  1066
  assumes "finite A"  "A \<noteq> {}"
wenzelm@19535
  1067
    and "!!x. x:A \<Longrightarrow> f x < g x"
wenzelm@19535
  1068
  shows "setsum f A < setsum g A"
wenzelm@19535
  1069
  using prems
nipkow@15554
  1070
proof (induct rule: finite_ne_induct)
nipkow@15554
  1071
  case singleton thus ?case by simp
nipkow@15554
  1072
next
nipkow@15554
  1073
  case insert thus ?case by (auto simp: add_strict_mono)
nipkow@15554
  1074
qed
nipkow@15554
  1075
nipkow@15535
  1076
lemma setsum_negf:
wenzelm@19535
  1077
  "setsum (%x. - (f x)::'a::ab_group_add) A = - setsum f A"
nipkow@15535
  1078
proof (cases "finite A")
berghofe@22262
  1079
  case True thus ?thesis by (induct set: finite) auto
nipkow@15535
  1080
next
nipkow@15535
  1081
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
  1082
qed
nipkow@15402
  1083
nipkow@15535
  1084
lemma setsum_subtractf:
wenzelm@19535
  1085
  "setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
wenzelm@19535
  1086
    setsum f A - setsum g A"
nipkow@15535
  1087
proof (cases "finite A")
nipkow@15535
  1088
  case True thus ?thesis by (simp add: diff_minus setsum_addf setsum_negf)
nipkow@15535
  1089
next
nipkow@15535
  1090
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
  1091
qed
nipkow@15402
  1092
nipkow@15535
  1093
lemma setsum_nonneg:
wenzelm@19535
  1094
  assumes nn: "\<forall>x\<in>A. (0::'a::{pordered_ab_semigroup_add,comm_monoid_add}) \<le> f x"
wenzelm@19535
  1095
  shows "0 \<le> setsum f A"
nipkow@15535
  1096
proof (cases "finite A")
nipkow@15535
  1097
  case True thus ?thesis using nn
wenzelm@21575
  1098
  proof induct
wenzelm@19535
  1099
    case empty then show ?case by simp
wenzelm@19535
  1100
  next
wenzelm@19535
  1101
    case (insert x F)
wenzelm@19535
  1102
    then have "0 + 0 \<le> f x + setsum f F" by (blast intro: add_mono)
wenzelm@19535
  1103
    with insert show ?case by simp
wenzelm@19535
  1104
  qed
nipkow@15535
  1105
next
nipkow@15535
  1106
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
  1107
qed
nipkow@15402
  1108
nipkow@15535
  1109
lemma setsum_nonpos:
wenzelm@19535
  1110
  assumes np: "\<forall>x\<in>A. f x \<le> (0::'a::{pordered_ab_semigroup_add,comm_monoid_add})"
wenzelm@19535
  1111
  shows "setsum f A \<le> 0"
nipkow@15535
  1112
proof (cases "finite A")
nipkow@15535
  1113
  case True thus ?thesis using np
wenzelm@21575
  1114
  proof induct
wenzelm@19535
  1115
    case empty then show ?case by simp
wenzelm@19535
  1116
  next
wenzelm@19535
  1117
    case (insert x F)
wenzelm@19535
  1118
    then have "f x + setsum f F \<le> 0 + 0" by (blast intro: add_mono)
wenzelm@19535
  1119
    with insert show ?case by simp
wenzelm@19535
  1120
  qed
nipkow@15535
  1121
next
nipkow@15535
  1122
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15535
  1123
qed
nipkow@15402
  1124
nipkow@15539
  1125
lemma setsum_mono2:
nipkow@15539
  1126
fixes f :: "'a \<Rightarrow> 'b :: {pordered_ab_semigroup_add_imp_le,comm_monoid_add}"
nipkow@15539
  1127
assumes fin: "finite B" and sub: "A \<subseteq> B" and nn: "\<And>b. b \<in> B-A \<Longrightarrow> 0 \<le> f b"
nipkow@15539
  1128
shows "setsum f A \<le> setsum f B"
nipkow@15539
  1129
proof -
nipkow@15539
  1130
  have "setsum f A \<le> setsum f A + setsum f (B-A)"
nipkow@15539
  1131
    by(simp add: add_increasing2[OF setsum_nonneg] nn Ball_def)
nipkow@15539
  1132
  also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
nipkow@15539
  1133
    by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
nipkow@15539
  1134
  also have "A \<union> (B-A) = B" using sub by blast
nipkow@15539
  1135
  finally show ?thesis .
nipkow@15539
  1136
qed
nipkow@15542
  1137
avigad@16775
  1138
lemma setsum_mono3: "finite B ==> A <= B ==> 
avigad@16775
  1139
    ALL x: B - A. 
avigad@16775
  1140
      0 <= ((f x)::'a::{comm_monoid_add,pordered_ab_semigroup_add}) ==>
avigad@16775
  1141
        setsum f A <= setsum f B"
avigad@16775
  1142
  apply (subgoal_tac "setsum f B = setsum f A + setsum f (B - A)")
avigad@16775
  1143
  apply (erule ssubst)
avigad@16775
  1144
  apply (subgoal_tac "setsum f A + 0 <= setsum f A + setsum f (B - A)")
avigad@16775
  1145
  apply simp
avigad@16775
  1146
  apply (rule add_left_mono)
avigad@16775
  1147
  apply (erule setsum_nonneg)
avigad@16775
  1148
  apply (subst setsum_Un_disjoint [THEN sym])
avigad@16775
  1149
  apply (erule finite_subset, assumption)
avigad@16775
  1150
  apply (rule finite_subset)
avigad@16775
  1151
  prefer 2
avigad@16775
  1152
  apply assumption
avigad@16775
  1153
  apply auto
avigad@16775
  1154
  apply (rule setsum_cong)
avigad@16775
  1155
  apply auto
avigad@16775
  1156
done
avigad@16775
  1157
ballarin@19279
  1158
lemma setsum_right_distrib: 
huffman@22934
  1159
  fixes f :: "'a => ('b::semiring_0)"
nipkow@15402
  1160
  shows "r * setsum f A = setsum (%n. r * f n) A"
nipkow@15402
  1161
proof (cases "finite A")
nipkow@15402
  1162
  case True
nipkow@15402
  1163
  thus ?thesis
wenzelm@21575
  1164
  proof induct
nipkow@15402
  1165
    case empty thus ?case by simp
nipkow@15402
  1166
  next
nipkow@15402
  1167
    case (insert x A) thus ?case by (simp add: right_distrib)
nipkow@15402
  1168
  qed
nipkow@15402
  1169
next
nipkow@15402
  1170
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
  1171
qed
nipkow@15402
  1172
ballarin@17149
  1173
lemma setsum_left_distrib:
huffman@22934
  1174
  "setsum f A * (r::'a::semiring_0) = (\<Sum>n\<in>A. f n * r)"
ballarin@17149
  1175
proof (cases "finite A")
ballarin@17149
  1176
  case True
ballarin@17149
  1177
  then show ?thesis
ballarin@17149
  1178
  proof induct
ballarin@17149
  1179
    case empty thus ?case by simp
ballarin@17149
  1180
  next
ballarin@17149
  1181
    case (insert x A) thus ?case by (simp add: left_distrib)
ballarin@17149
  1182
  qed
ballarin@17149
  1183
next
ballarin@17149
  1184
  case False thus ?thesis by (simp add: setsum_def)
ballarin@17149
  1185
qed
ballarin@17149
  1186
ballarin@17149
  1187
lemma setsum_divide_distrib:
ballarin@17149
  1188
  "setsum f A / (r::'a::field) = (\<Sum>n\<in>A. f n / r)"
ballarin@17149
  1189
proof (cases "finite A")
ballarin@17149
  1190
  case True
ballarin@17149
  1191
  then show ?thesis
ballarin@17149
  1192
  proof induct
ballarin@17149
  1193
    case empty thus ?case by simp
ballarin@17149
  1194
  next
ballarin@17149
  1195
    case (insert x A) thus ?case by (simp add: add_divide_distrib)
ballarin@17149
  1196
  qed
ballarin@17149
  1197
next
ballarin@17149
  1198
  case False thus ?thesis by (simp add: setsum_def)
ballarin@17149
  1199
qed
ballarin@17149
  1200
nipkow@15535
  1201
lemma setsum_abs[iff]: 
nipkow@15402
  1202
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
nipkow@15402
  1203
  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
nipkow@15535
  1204
proof (cases "finite A")
nipkow@15535
  1205
  case True
nipkow@15535
  1206
  thus ?thesis
wenzelm@21575
  1207
  proof induct
nipkow@15535
  1208
    case empty thus ?case by simp
nipkow@15535
  1209
  next
nipkow@15535
  1210
    case (insert x A)
nipkow@15535
  1211
    thus ?case by (auto intro: abs_triangle_ineq order_trans)
nipkow@15535
  1212
  qed
nipkow@15402
  1213
next
nipkow@15535
  1214
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
  1215
qed
nipkow@15402
  1216
nipkow@15535
  1217
lemma setsum_abs_ge_zero[iff]: 
nipkow@15402
  1218
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
nipkow@15402
  1219
  shows "0 \<le> setsum (%i. abs(f i)) A"
nipkow@15535
  1220
proof (cases "finite A")
nipkow@15535
  1221
  case True
nipkow@15535
  1222
  thus ?thesis
wenzelm@21575
  1223
  proof induct
nipkow@15535
  1224
    case empty thus ?case by simp
nipkow@15535
  1225
  next
nipkow@21733
  1226
    case (insert x A) thus ?case by (auto simp: add_nonneg_nonneg)
nipkow@15535
  1227
  qed
nipkow@15402
  1228
next
nipkow@15535
  1229
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
  1230
qed
nipkow@15402
  1231
nipkow@15539
  1232
lemma abs_setsum_abs[simp]: 
nipkow@15539
  1233
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
nipkow@15539
  1234
  shows "abs (\<Sum>a\<in>A. abs(f a)) = (\<Sum>a\<in>A. abs(f a))"
nipkow@15539
  1235
proof (cases "finite A")
nipkow@15539
  1236
  case True
nipkow@15539
  1237
  thus ?thesis
wenzelm@21575
  1238
  proof induct
nipkow@15539
  1239
    case empty thus ?case by simp
nipkow@15539
  1240
  next
nipkow@15539
  1241
    case (insert a A)
nipkow@15539
  1242
    hence "\<bar>\<Sum>a\<in>insert a A. \<bar>f a\<bar>\<bar> = \<bar>\<bar>f a\<bar> + (\<Sum>a\<in>A. \<bar>f a\<bar>)\<bar>" by simp
nipkow@15539
  1243
    also have "\<dots> = \<bar>\<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>\<bar>"  using insert by simp
avigad@16775
  1244
    also have "\<dots> = \<bar>f a\<bar> + \<bar>\<Sum>a\<in>A. \<bar>f a\<bar>\<bar>"
avigad@16775
  1245
      by (simp del: abs_of_nonneg)
nipkow@15539
  1246
    also have "\<dots> = (\<Sum>a\<in>insert a A. \<bar>f a\<bar>)" using insert by simp
nipkow@15539
  1247
    finally show ?case .
nipkow@15539
  1248
  qed
nipkow@15539
  1249
next
nipkow@15539
  1250
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15539
  1251
qed
nipkow@15539
  1252
nipkow@15402
  1253
ballarin@17149
  1254
text {* Commuting outer and inner summation *}
ballarin@17149
  1255
ballarin@17149
  1256
lemma swap_inj_on:
ballarin@17149
  1257
  "inj_on (%(i, j). (j, i)) (A \<times> B)"
ballarin@17149
  1258
  by (unfold inj_on_def) fast
ballarin@17149
  1259
ballarin@17149
  1260
lemma swap_product:
ballarin@17149
  1261
  "(%(i, j). (j, i)) ` (A \<times> B) = B \<times> A"
ballarin@17149
  1262
  by (simp add: split_def image_def) blast
ballarin@17149
  1263
ballarin@17149
  1264
lemma setsum_commute:
ballarin@17149
  1265
  "(\<Sum>i\<in>A. \<Sum>j\<in>B. f i j) = (\<Sum>j\<in>B. \<Sum>i\<in>A. f i j)"
ballarin@17149
  1266
proof (simp add: setsum_cartesian_product)
paulson@17189
  1267
  have "(\<Sum>(x,y) \<in> A <*> B. f x y) =
paulson@17189
  1268
    (\<Sum>(y,x) \<in> (%(i, j). (j, i)) ` (A \<times> B). f x y)"
ballarin@17149
  1269
    (is "?s = _")
ballarin@17149
  1270
    apply (simp add: setsum_reindex [where f = "%(i, j). (j, i)"] swap_inj_on)
ballarin@17149
  1271
    apply (simp add: split_def)
ballarin@17149
  1272
    done
paulson@17189
  1273
  also have "... = (\<Sum>(y,x)\<in>B \<times> A. f x y)"
ballarin@17149
  1274
    (is "_ = ?t")
ballarin@17149
  1275
    apply (simp add: swap_product)
ballarin@17149
  1276
    done
ballarin@17149
  1277
  finally show "?s = ?t" .
ballarin@17149
  1278
qed
ballarin@17149
  1279
ballarin@19279
  1280
lemma setsum_product:
huffman@22934
  1281
  fixes f :: "'a => ('b::semiring_0)"
ballarin@19279
  1282
  shows "setsum f A * setsum g B = (\<Sum>i\<in>A. \<Sum>j\<in>B. f i * g j)"
ballarin@19279
  1283
  by (simp add: setsum_right_distrib setsum_left_distrib) (rule setsum_commute)
ballarin@19279
  1284
ballarin@17149
  1285
nipkow@15402
  1286
subsection {* Generalized product over a set *}
nipkow@15402
  1287
nipkow@15402
  1288
constdefs
nipkow@15402
  1289
  setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
nipkow@15402
  1290
  "setprod f A == if finite A then fold (op *) f 1 A else 1"
nipkow@15402
  1291
wenzelm@19535
  1292
abbreviation
wenzelm@21404
  1293
  Setprod  ("\<Prod>_" [1000] 999) where
wenzelm@19535
  1294
  "\<Prod>A == setprod (%x. x) A"
wenzelm@19535
  1295
nipkow@15402
  1296
syntax
paulson@17189
  1297
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3PROD _:_. _)" [0, 51, 10] 10)
nipkow@15402
  1298
syntax (xsymbols)
paulson@17189
  1299
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
  1300
syntax (HTML output)
paulson@17189
  1301
  "_setprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@16550
  1302
nipkow@16550
  1303
translations -- {* Beware of argument permutation! *}
nipkow@16550
  1304
  "PROD i:A. b" == "setprod (%i. b) A" 
nipkow@16550
  1305
  "\<Prod>i\<in>A. b" == "setprod (%i. b) A" 
nipkow@16550
  1306
nipkow@16550
  1307
text{* Instead of @{term"\<Prod>x\<in>{x. P}. e"} we introduce the shorter
nipkow@16550
  1308
 @{text"\<Prod>x|P. e"}. *}
nipkow@16550
  1309
nipkow@16550
  1310
syntax
paulson@17189
  1311
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3PROD _ |/ _./ _)" [0,0,10] 10)
nipkow@16550
  1312
syntax (xsymbols)
paulson@17189
  1313
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Prod>_ | (_)./ _)" [0,0,10] 10)
nipkow@16550
  1314
syntax (HTML output)
paulson@17189
  1315
  "_qsetprod" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Prod>_ | (_)./ _)" [0,0,10] 10)
nipkow@16550
  1316
nipkow@15402
  1317
translations
nipkow@16550
  1318
  "PROD x|P. t" => "setprod (%x. t) {x. P}"
nipkow@16550
  1319
  "\<Prod>x|P. t" => "setprod (%x. t) {x. P}"
nipkow@16550
  1320
nipkow@15402
  1321
nipkow@15402
  1322
lemma setprod_empty [simp]: "setprod f {} = 1"
nipkow@15402
  1323
  by (auto simp add: setprod_def)
nipkow@15402
  1324
nipkow@15402
  1325
lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
nipkow@15402
  1326
    setprod f (insert a A) = f a * setprod f A"
ballarin@19931
  1327
  by (simp add: setprod_def)
nipkow@15402
  1328
paulson@15409
  1329
lemma setprod_infinite [simp]: "~ finite A ==> setprod f A = 1"
paulson@15409
  1330
  by (simp add: setprod_def)
paulson@15409
  1331
nipkow@15402
  1332
lemma setprod_reindex:
nipkow@15402
  1333
     "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
ballarin@15765
  1334
by(auto simp: setprod_def AC_mult.fold_reindex dest!:finite_imageD)
nipkow@15402
  1335
nipkow@15402
  1336
lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"
nipkow@15402
  1337
by (auto simp add: setprod_reindex)
nipkow@15402
  1338
nipkow@15402
  1339
lemma setprod_cong:
nipkow@15402
  1340
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
ballarin@15765
  1341
by(fastsimp simp: setprod_def intro: AC_mult.fold_cong)
nipkow@15402
  1342
berghofe@16632
  1343
lemma strong_setprod_cong:
berghofe@16632
  1344
  "A = B ==> (!!x. x:B =simp=> f x = g x) ==> setprod f A = setprod g B"
berghofe@16632
  1345
by(fastsimp simp: simp_implies_def setprod_def intro: AC_mult.fold_cong)
berghofe@16632
  1346
nipkow@15402
  1347
lemma setprod_reindex_cong: "inj_on f A ==>
nipkow@15402
  1348
    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
nipkow@15402
  1349
  by (frule setprod_reindex, simp)
nipkow@15402
  1350
nipkow@15402
  1351
nipkow@15402
  1352
lemma setprod_1: "setprod (%i. 1) A = 1"
nipkow@15402
  1353
  apply (case_tac "finite A")
nipkow@15402
  1354
  apply (erule finite_induct, auto simp add: mult_ac)
nipkow@15402
  1355
  done
nipkow@15402
  1356
nipkow@15402
  1357
lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
nipkow@15402
  1358
  apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
nipkow@15402
  1359
  apply (erule ssubst, rule setprod_1)
nipkow@15402
  1360
  apply (rule setprod_cong, auto)
nipkow@15402
  1361
  done
nipkow@15402
  1362
nipkow@15402
  1363
lemma setprod_Un_Int: "finite A ==> finite B
nipkow@15402
  1364
    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
ballarin@15765
  1365
by(simp add: setprod_def AC_mult.fold_Un_Int[symmetric])
nipkow@15402
  1366
nipkow@15402
  1367
lemma setprod_Un_disjoint: "finite A ==> finite B
nipkow@15402
  1368
  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
nipkow@15402
  1369
by (subst setprod_Un_Int [symmetric], auto)
nipkow@15402
  1370
nipkow@15402
  1371
lemma setprod_UN_disjoint:
nipkow@15402
  1372
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
  1373
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
  1374
      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
ballarin@15765
  1375
by(simp add: setprod_def AC_mult.fold_UN_disjoint cong: setprod_cong)
nipkow@15402
  1376
nipkow@15402
  1377
lemma setprod_Union_disjoint:
paulson@15409
  1378
  "[| (ALL A:C. finite A);
paulson@15409
  1379
      (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) |] 
paulson@15409
  1380
   ==> setprod f (Union C) = setprod (setprod f) C"
paulson@15409
  1381
apply (cases "finite C") 
paulson@15409
  1382
 prefer 2 apply (force dest: finite_UnionD simp add: setprod_def)
nipkow@15402
  1383
  apply (frule setprod_UN_disjoint [of C id f])
paulson@15409
  1384
 apply (unfold Union_def id_def, assumption+)
paulson@15409
  1385
done
nipkow@15402
  1386
nipkow@15402
  1387
lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@16550
  1388
    (\<Prod>x\<in>A. (\<Prod>y\<in> B x. f x y)) =
paulson@17189
  1389
    (\<Prod>(x,y)\<in>(SIGMA x:A. B x). f x y)"
ballarin@15765
  1390
by(simp add:setprod_def AC_mult.fold_Sigma split_def cong:setprod_cong)
nipkow@15402
  1391
paulson@15409
  1392
text{*Here we can eliminate the finiteness assumptions, by cases.*}
paulson@15409
  1393
lemma setprod_cartesian_product: 
paulson@17189
  1394
     "(\<Prod>x\<in>A. (\<Prod>y\<in> B. f x y)) = (\<Prod>(x,y)\<in>(A <*> B). f x y)"
paulson@15409
  1395
apply (cases "finite A") 
paulson@15409
  1396
 apply (cases "finite B") 
paulson@15409
  1397
  apply (simp add: setprod_Sigma)
paulson@15409
  1398
 apply (cases "A={}", simp)
paulson@15409
  1399
 apply (simp add: setprod_1) 
paulson@15409
  1400
apply (auto simp add: setprod_def
paulson@15409
  1401
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
  1402
done
nipkow@15402
  1403
nipkow@15402
  1404
lemma setprod_timesf:
paulson@15409
  1405
     "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"
ballarin@15765
  1406
by(simp add:setprod_def AC_mult.fold_distrib)
nipkow@15402
  1407
nipkow@15402
  1408
nipkow@15402
  1409
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
  1410
nipkow@15402
  1411
lemma setprod_eq_1_iff [simp]:
nipkow@15402
  1412
    "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
berghofe@22262
  1413
  by (induct set: finite) auto
nipkow@15402
  1414
nipkow@15402
  1415
lemma setprod_zero:
huffman@23277
  1416
     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1) ==> setprod f A = 0"
berghofe@22262
  1417
  apply (induct set: finite, force, clarsimp)
nipkow@15402
  1418
  apply (erule disjE, auto)
nipkow@15402
  1419
  done
nipkow@15402
  1420
nipkow@15402
  1421
lemma setprod_nonneg [rule_format]:
nipkow@15402
  1422
     "(ALL x: A. (0::'a::ordered_idom) \<le> f x) --> 0 \<le> setprod f A"
nipkow@15402
  1423
  apply (case_tac "finite A")
berghofe@22262
  1424
  apply (induct set: finite, force, clarsimp)
nipkow@15402
  1425
  apply (subgoal_tac "0 * 0 \<le> f x * setprod f F", force)
nipkow@15402
  1426
  apply (rule mult_mono, assumption+)
nipkow@15402
  1427
  apply (auto simp add: setprod_def)
nipkow@15402
  1428
  done
nipkow@15402
  1429
nipkow@15402
  1430
lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x)
nipkow@15402
  1431
     --> 0 < setprod f A"
nipkow@15402
  1432
  apply (case_tac "finite A")
berghofe@22262
  1433
  apply (induct set: finite, force, clarsimp)
nipkow@15402
  1434
  apply (subgoal_tac "0 * 0 < f x * setprod f F", force)
nipkow@15402
  1435
  apply (rule mult_strict_mono, assumption+)
nipkow@15402
  1436
  apply (auto simp add: setprod_def)
nipkow@15402
  1437
  done
nipkow@15402
  1438
nipkow@15402
  1439
lemma setprod_nonzero [rule_format]:
huffman@23277
  1440
    "(ALL x y. (x::'a::comm_semiring_1) * y = 0 --> x = 0 | y = 0) ==>
nipkow@15402
  1441
      finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
nipkow@15402
  1442
  apply (erule finite_induct, auto)
nipkow@15402
  1443
  done
nipkow@15402
  1444
nipkow@15402
  1445
lemma setprod_zero_eq:
huffman@23277
  1446
    "(ALL x y. (x::'a::comm_semiring_1) * y = 0 --> x = 0 | y = 0) ==>
nipkow@15402
  1447
     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
nipkow@15402
  1448
  apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
nipkow@15402
  1449
  done
nipkow@15402
  1450
nipkow@15402
  1451
lemma setprod_nonzero_field:
huffman@23277
  1452
    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::idom)) ==> setprod f A \<noteq> 0"
nipkow@15402
  1453
  apply (rule setprod_nonzero, auto)
nipkow@15402
  1454
  done
nipkow@15402
  1455
nipkow@15402
  1456
lemma setprod_zero_eq_field:
huffman@23277
  1457
    "finite A ==> (setprod f A = (0::'a::idom)) = (EX x: A. f x = 0)"
nipkow@15402
  1458
  apply (rule setprod_zero_eq, auto)
nipkow@15402
  1459
  done
nipkow@15402
  1460
nipkow@15402
  1461
lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
nipkow@15402
  1462
    (setprod f (A Un B) :: 'a ::{field})
nipkow@15402
  1463
      = setprod f A * setprod f B / setprod f (A Int B)"
nipkow@15402
  1464
  apply (subst setprod_Un_Int [symmetric], auto)
nipkow@15402
  1465
  apply (subgoal_tac "finite (A Int B)")
nipkow@15402
  1466
  apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
nipkow@23398
  1467
  apply (subst times_divide_eq_right [THEN sym], auto)
nipkow@15402
  1468
  done
nipkow@15402
  1469
nipkow@15402
  1470
lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
nipkow@15402
  1471
    (setprod f (A - {a}) :: 'a :: {field}) =
nipkow@15402
  1472
      (if a:A then setprod f A / f a else setprod f A)"
nipkow@23413
  1473
by (erule finite_induct) (auto simp add: insert_Diff_if)
nipkow@15402
  1474
nipkow@15402
  1475
lemma setprod_inversef: "finite A ==>
nipkow@15402
  1476
    ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
nipkow@15402
  1477
      setprod (inverse \<circ> f) A = inverse (setprod f A)"
nipkow@15402
  1478
  apply (erule finite_induct)
nipkow@15402
  1479
  apply (simp, simp)
nipkow@15402
  1480
  done
nipkow@15402
  1481
nipkow@15402
  1482
lemma setprod_dividef:
nipkow@15402
  1483
     "[|finite A;
nipkow@15402
  1484
        \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
nipkow@15402
  1485
      ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
nipkow@15402
  1486
  apply (subgoal_tac
nipkow@15402
  1487
         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
nipkow@15402
  1488
  apply (erule ssubst)
nipkow@15402
  1489
  apply (subst divide_inverse)
nipkow@15402
  1490
  apply (subst setprod_timesf)
nipkow@15402
  1491
  apply (subst setprod_inversef, assumption+, rule refl)
nipkow@15402
  1492
  apply (rule setprod_cong, rule refl)
nipkow@15402
  1493
  apply (subst divide_inverse, auto)
nipkow@15402
  1494
  done
nipkow@15402
  1495
wenzelm@12396
  1496
subsection {* Finite cardinality *}
wenzelm@12396
  1497
nipkow@15402
  1498
text {* This definition, although traditional, is ugly to work with:
nipkow@15402
  1499
@{text "card A == LEAST n. EX f. A = {f i | i. i < n}"}.
nipkow@15402
  1500
But now that we have @{text setsum} things are easy:
wenzelm@12396
  1501
*}
wenzelm@12396
  1502
wenzelm@12396
  1503
constdefs
wenzelm@12396
  1504
  card :: "'a set => nat"
nipkow@15402
  1505
  "card A == setsum (%x. 1::nat) A"
wenzelm@12396
  1506
wenzelm@12396
  1507
lemma card_empty [simp]: "card {} = 0"
nipkow@24853
  1508
by (simp add: card_def)
nipkow@15402
  1509
paulson@24427
  1510
lemma card_infinite [simp]: "~ finite A ==> card A = 0"
nipkow@24853
  1511
by (simp add: card_def)
paulson@15409
  1512
nipkow@15402
  1513
lemma card_eq_setsum: "card A = setsum (%x. 1) A"
nipkow@15402
  1514
by (simp add: card_def)
wenzelm@12396
  1515
wenzelm@12396
  1516
lemma card_insert_disjoint [simp]:
wenzelm@12396
  1517
  "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
ballarin@15765
  1518
by(simp add: card_def)
nipkow@15402
  1519
nipkow@15402
  1520
lemma card_insert_if:
nipkow@15402
  1521
    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
nipkow@15402
  1522
  by (simp add: insert_absorb)
wenzelm@12396
  1523
paulson@24286
  1524
lemma card_0_eq [simp,noatp]: "finite A ==> (card A = 0) = (A = {})"
wenzelm@12396
  1525
  apply auto
paulson@15506
  1526
  apply (drule_tac a = x in mk_disjoint_insert, clarify, auto)
wenzelm@12396
  1527
  done
wenzelm@12396
  1528
paulson@15409
  1529
lemma card_eq_0_iff: "(card A = 0) = (A = {} | ~ finite A)"
paulson@15409
  1530
by auto
paulson@15409
  1531
nipkow@24853
  1532
wenzelm@12396
  1533
lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
nipkow@14302
  1534
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
nipkow@14302
  1535
apply(simp del:insert_Diff_single)
nipkow@14302
  1536
done
wenzelm@12396
  1537
wenzelm@12396
  1538
lemma card_Diff_singleton:
nipkow@24853
  1539
  "finite A ==> x: A ==> card (A - {x}) = card A - 1"
nipkow@24853
  1540
by (simp add: card_Suc_Diff1 [symmetric])
wenzelm@12396
  1541
wenzelm@12396
  1542
lemma card_Diff_singleton_if:
nipkow@24853
  1543
  "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)"
nipkow@24853
  1544
by (simp add: card_Diff_singleton)
nipkow@24853
  1545
nipkow@24853
  1546
lemma card_Diff_insert[simp]:
nipkow@24853
  1547
assumes "finite A" and "a:A" and "a ~: B"
nipkow@24853
  1548
shows "card(A - insert a B) = card(A - B) - 1"
nipkow@24853
  1549
proof -
nipkow@24853
  1550
  have "A - insert a B = (A - B) - {a}" using assms by blast
nipkow@24853
  1551
  then show ?thesis using assms by(simp add:card_Diff_singleton)
nipkow@24853
  1552
qed
wenzelm@12396
  1553
wenzelm@12396
  1554
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
nipkow@24853
  1555
by (simp add: card_insert_if card_Suc_Diff1 del:card_Diff_insert)
wenzelm@12396
  1556
wenzelm@12396
  1557
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
nipkow@24853
  1558
by (simp add: card_insert_if)
wenzelm@12396
  1559
nipkow@15402
  1560
lemma card_mono: "\<lbrakk> finite B; A \<subseteq> B \<rbrakk> \<Longrightarrow> card A \<le> card B"
nipkow@15539
  1561
by (simp add: card_def setsum_mono2)
nipkow@15402
  1562
wenzelm@12396
  1563
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
berghofe@22262
  1564
  apply (induct set: finite, simp, clarify)
wenzelm@12396
  1565
  apply (subgoal_tac "finite A & A - {x} <= F")
paulson@14208
  1566
   prefer 2 apply (blast intro: finite_subset, atomize)
wenzelm@12396
  1567
  apply (drule_tac x = "A - {x}" in spec)
wenzelm@12396
  1568
  apply (simp add: card_Diff_singleton_if split add: split_if_asm)
paulson@14208
  1569
  apply (case_tac "card A", auto)
wenzelm@12396
  1570
  done
wenzelm@12396
  1571
wenzelm@12396
  1572
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
nipkow@24853
  1573
apply (simp add: psubset_def linorder_not_le [symmetric])
nipkow@24853
  1574
apply (blast dest: card_seteq)
nipkow@24853
  1575
done
wenzelm@12396
  1576
wenzelm@12396
  1577
lemma card_Un_Int: "finite A ==> finite B
wenzelm@12396
  1578
    ==> card A + card B = card (A Un B) + card (A Int B)"
nipkow@15402
  1579
by(simp add:card_def setsum_Un_Int)
wenzelm@12396
  1580
wenzelm@12396
  1581
lemma card_Un_disjoint: "finite A ==> finite B
wenzelm@12396
  1582
    ==> A Int B = {} ==> card (A Un B) = card A + card B"
nipkow@24853
  1583
by (simp add: card_Un_Int)
wenzelm@12396
  1584
wenzelm@12396
  1585
lemma card_Diff_subset:
nipkow@15402
  1586
  "finite B ==> B <= A ==> card (A - B) = card A - card B"
nipkow@15402
  1587
by(simp add:card_def setsum_diff_nat)
wenzelm@12396
  1588
wenzelm@12396
  1589
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
wenzelm@12396
  1590
  apply (rule Suc_less_SucD)
nipkow@24853
  1591
  apply (simp add: card_Suc_Diff1 del:card_Diff_insert)
wenzelm@12396
  1592
  done
wenzelm@12396
  1593
wenzelm@12396
  1594
lemma card_Diff2_less:
wenzelm@12396
  1595
    "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
wenzelm@12396
  1596
  apply (case_tac "x = y")
nipkow@24853
  1597
   apply (simp add: card_Diff1_less del:card_Diff_insert)
wenzelm@12396
  1598
  apply (rule less_trans)
nipkow@24853
  1599
   prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert)
wenzelm@12396
  1600
  done
wenzelm@12396
  1601
wenzelm@12396
  1602
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
wenzelm@12396
  1603
  apply (case_tac "x : A")
wenzelm@12396
  1604
   apply (simp_all add: card_Diff1_less less_imp_le)
wenzelm@12396
  1605
  done
wenzelm@12396
  1606
wenzelm@12396
  1607
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
paulson@14208
  1608
by (erule psubsetI, blast)
wenzelm@12396
  1609
paulson@14889
  1610
lemma insert_partition:
nipkow@15402
  1611
  "\<lbrakk> x \<notin> F; \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<rbrakk>
nipkow@15402
  1612
  \<Longrightarrow> x \<inter> \<Union> F = {}"
paulson@14889
  1613
by auto
paulson@14889
  1614
paulson@19793
  1615
text{* main cardinality theorem *}
paulson@14889
  1616
lemma card_partition [rule_format]:
paulson@14889
  1617
     "finite C ==>  
paulson@14889
  1618
        finite (\<Union> C) -->  
paulson@14889
  1619
        (\<forall>c\<in>C. card c = k) -->   
paulson@14889
  1620
        (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->  
paulson@14889
  1621
        k * card(C) = card (\<Union> C)"
paulson@14889
  1622
apply (erule finite_induct, simp)
paulson@14889
  1623
apply (simp add: card_insert_disjoint card_Un_disjoint insert_partition 
paulson@14889
  1624
       finite_subset [of _ "\<Union> (insert x F)"])
paulson@14889
  1625
done
paulson@14889
  1626
wenzelm@12396
  1627
paulson@19793
  1628
text{*The form of a finite set of given cardinality*}
paulson@19793
  1629
paulson@19793
  1630
lemma card_eq_SucD:
nipkow@24853
  1631
assumes "card A = Suc k"
nipkow@24853
  1632
shows "\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={})"
paulson@19793
  1633
proof -
nipkow@24853
  1634
  have fin: "finite A" using assms by (auto intro: ccontr)
nipkow@24853
  1635
  moreover have "card A \<noteq> 0" using assms by auto
nipkow@24853
  1636
  ultimately obtain b where b: "b \<in> A" by auto
paulson@19793
  1637
  show ?thesis
paulson@19793
  1638
  proof (intro exI conjI)
paulson@19793
  1639
    show "A = insert b (A-{b})" using b by blast
paulson@19793
  1640
    show "b \<notin> A - {b}" by blast
nipkow@24853
  1641
    show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
nipkow@24853
  1642
      using assms b fin by(fastsimp dest:mk_disjoint_insert)+
paulson@19793
  1643
  qed
paulson@19793
  1644
qed
paulson@19793
  1645
paulson@19793
  1646
lemma card_Suc_eq:
nipkow@24853
  1647
  "(card A = Suc k) =
nipkow@24853
  1648
   (\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={}))"
nipkow@24853
  1649
apply(rule iffI)
nipkow@24853
  1650
 apply(erule card_eq_SucD)
nipkow@24853
  1651
apply(auto)
nipkow@24853
  1652
apply(subst card_insert)
nipkow@24853
  1653
 apply(auto intro:ccontr)
nipkow@24853
  1654
done
paulson@19793
  1655
nipkow@15539
  1656
lemma setsum_constant [simp]: "(\<Sum>x \<in> A. y) = of_nat(card A) * y"
nipkow@15539
  1657
apply (cases "finite A")
nipkow@15539
  1658
apply (erule finite_induct)
nipkow@23477
  1659
apply (auto simp add: ring_simps)
paulson@15409
  1660
done
nipkow@15402
  1661
krauss@21199
  1662
lemma setprod_constant: "finite A ==> (\<Prod>x\<in> A. (y::'a::{recpower, comm_monoid_mult})) = y^(card A)"
nipkow@15402
  1663
  apply (erule finite_induct)
nipkow@15402
  1664
  apply (auto simp add: power_Suc)
nipkow@15402
  1665
  done
nipkow@15402
  1666
nipkow@15542
  1667
lemma setsum_bounded:
huffman@23277
  1668
  assumes le: "\<And>i. i\<in>A \<Longrightarrow> f i \<le> (K::'a::{semiring_1, pordered_ab_semigroup_add})"
nipkow@15542
  1669
  shows "setsum f A \<le> of_nat(card A) * K"
nipkow@15542
  1670
proof (cases "finite A")
nipkow@15542
  1671
  case True
nipkow@15542
  1672
  thus ?thesis using le setsum_mono[where K=A and g = "%x. K"] by simp
nipkow@15542
  1673
next
nipkow@15542
  1674
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15542
  1675
qed
nipkow@15542
  1676
nipkow@15402
  1677
nipkow@15402
  1678
subsubsection {* Cardinality of unions *}
nipkow@15402
  1679
nipkow@15402
  1680
lemma card_UN_disjoint:
nipkow@15402
  1681
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
  1682
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
  1683
      card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
nipkow@15539
  1684
  apply (simp add: card_def del: setsum_constant)
nipkow@15402
  1685
  apply (subgoal_tac
nipkow@15402
  1686
           "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
nipkow@15539
  1687
  apply (simp add: setsum_UN_disjoint del: setsum_constant)
nipkow@15539
  1688
  apply (simp cong: setsum_cong)
nipkow@15402
  1689
  done
nipkow@15402
  1690
nipkow@15402
  1691
lemma card_Union_disjoint:
nipkow@15402
  1692
  "finite C ==> (ALL A:C. finite A) ==>
nipkow@15402
  1693
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
nipkow@15402
  1694
      card (Union C) = setsum card C"
nipkow@15402
  1695
  apply (frule card_UN_disjoint [of C id])
nipkow@15402
  1696
  apply (unfold Union_def id_def, assumption+)
nipkow@15402
  1697
  done
nipkow@15402
  1698
wenzelm@12396
  1699
subsubsection {* Cardinality of image *}
wenzelm@12396
  1700
paulson@15447
  1701
text{*The image of a finite set can be expressed using @{term fold}.*}
paulson@15447
  1702
lemma image_eq_fold: "finite A ==> f ` A = fold (op Un) (%x. {f x}) {} A"
paulson@15447
  1703
  apply (erule finite_induct, simp)
paulson@15447
  1704
  apply (subst ACf.fold_insert) 
paulson@15447
  1705
  apply (auto simp add: ACf_def) 
paulson@15447
  1706
  done
paulson@15447
  1707
wenzelm@12396
  1708
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
berghofe@22262
  1709
  apply (induct set: finite)
wenzelm@21575
  1710
   apply simp
wenzelm@12396
  1711
  apply (simp add: le_SucI finite_imageI card_insert_if)
wenzelm@12396
  1712
  done
wenzelm@12396
  1713
nipkow@15402
  1714
lemma card_image: "inj_on f A ==> card (f ` A) = card A"
nipkow@15539
  1715
by(simp add:card_def setsum_reindex o_def del:setsum_constant)
wenzelm@12396
  1716
wenzelm@12396
  1717
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
nipkow@25162
  1718
by (simp add: card_seteq card_image)
wenzelm@12396
  1719
nipkow@15111
  1720
lemma eq_card_imp_inj_on:
nipkow@15111
  1721
  "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
wenzelm@21575
  1722
apply (induct rule:finite_induct)
wenzelm@21575
  1723
apply simp
nipkow@15111
  1724
apply(frule card_image_le[where f = f])
nipkow@15111
  1725
apply(simp add:card_insert_if split:if_splits)
nipkow@15111
  1726
done
nipkow@15111
  1727
nipkow@15111
  1728
lemma inj_on_iff_eq_card:
nipkow@15111
  1729
  "finite A ==> inj_on f A = (card(f ` A) = card A)"
nipkow@15111
  1730
by(blast intro: card_image eq_card_imp_inj_on)
nipkow@15111
  1731
wenzelm@12396
  1732
nipkow@15402
  1733
lemma card_inj_on_le:
nipkow@15402
  1734
    "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
nipkow@15402
  1735
apply (subgoal_tac "finite A") 
nipkow@15402
  1736
 apply (force intro: card_mono simp add: card_image [symmetric])
nipkow@15402
  1737
apply (blast intro: finite_imageD dest: finite_subset) 
nipkow@15402
  1738
done
nipkow@15402
  1739
nipkow@15402
  1740
lemma card_bij_eq:
nipkow@15402
  1741
    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
nipkow@15402
  1742
       finite A; finite B |] ==> card A = card B"
nipkow@15402
  1743
  by (auto intro: le_anti_sym card_inj_on_le)
nipkow@15402
  1744
nipkow@15402
  1745
nipkow@15402
  1746
subsubsection {* Cardinality of products *}
nipkow@15402
  1747
nipkow@15402
  1748
(*
nipkow@15402
  1749
lemma SigmaI_insert: "y \<notin> A ==>
nipkow@15402
  1750
  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
nipkow@15402
  1751
  by auto
nipkow@15402
  1752
*)
nipkow@15402
  1753
nipkow@15402
  1754
lemma card_SigmaI [simp]:
nipkow@15402
  1755
  "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
nipkow@15402
  1756
  \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
nipkow@15539
  1757
by(simp add:card_def setsum_Sigma del:setsum_constant)
nipkow@15402
  1758
paulson@15409
  1759
lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)"
paulson@15409
  1760
apply (cases "finite A") 
paulson@15409
  1761
apply (cases "finite B") 
paulson@15409
  1762
apply (auto simp add: card_eq_0_iff
nipkow@15539
  1763
            dest: finite_cartesian_productD1 finite_cartesian_productD2)
paulson@15409
  1764
done
nipkow@15402
  1765
nipkow@15402
  1766
lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
nipkow@15539
  1767
by (simp add: card_cartesian_product)
paulson@15409
  1768
nipkow@15402
  1769
nipkow@15402
  1770
wenzelm@12396
  1771
subsubsection {* Cardinality of the Powerset *}
wenzelm@12396
  1772
wenzelm@12396
  1773
lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
berghofe@22262
  1774
  apply (induct set: finite)
wenzelm@12396
  1775
   apply (simp_all add: Pow_insert)
paulson@14208
  1776
  apply (subst card_Un_disjoint, blast)
paulson@14208
  1777
    apply (blast intro: finite_imageI, blast)
wenzelm@12396
  1778
  apply (subgoal_tac "inj_on (insert x) (Pow F)")
wenzelm@12396
  1779
   apply (simp add: card_image Pow_insert)
wenzelm@12396
  1780
  apply (unfold inj_on_def)
wenzelm@12396
  1781
  apply (blast elim!: equalityE)
wenzelm@12396
  1782
  done
wenzelm@12396
  1783
haftmann@24342
  1784
text {* Relates to equivalence classes.  Based on a theorem of F. Kammüller.  *}
wenzelm@12396
  1785
wenzelm@12396
  1786
lemma dvd_partition:
nipkow@15392
  1787
  "finite (Union C) ==>
wenzelm@12396
  1788
    ALL c : C. k dvd card c ==>
paulson@14430
  1789
    (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
wenzelm@12396
  1790
  k dvd card (Union C)"
nipkow@15392
  1791
apply(frule finite_UnionD)
nipkow@15392
  1792
apply(rotate_tac -1)
berghofe@22262
  1793
  apply (induct set: finite, simp_all, clarify)
wenzelm@12396
  1794
  apply (subst card_Un_disjoint)
wenzelm@12396
  1795
  apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
wenzelm@12396
  1796
  done
wenzelm@12396
  1797
wenzelm@12396
  1798
nipkow@25162
  1799
subsubsection {* Relating injectivity and surjectivity *}
nipkow@25162
  1800
nipkow@25162
  1801
lemma finite_surj_inj: "finite(A) \<Longrightarrow> A <= f`A \<Longrightarrow> inj_on f A"
nipkow@25162
  1802
apply(rule eq_card_imp_inj_on, assumption)
nipkow@25162
  1803
apply(frule finite_imageI)
nipkow@25162
  1804
apply(drule (1) card_seteq)
nipkow@25162
  1805
apply(erule card_image_le)
nipkow@25162
  1806
apply simp
nipkow@25162
  1807
done
nipkow@25162
  1808
nipkow@25162
  1809
lemma finite_UNIV_surj_inj: fixes f :: "'a \<Rightarrow> 'a"
nipkow@25162
  1810
shows "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
nipkow@25162
  1811
by (blast intro: finite_surj_inj subset_UNIV dest:surj_range)
nipkow@25162
  1812
nipkow@25162
  1813
lemma finite_UNIV_inj_surj: fixes f :: "'a \<Rightarrow> 'a"
nipkow@25162
  1814
shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
nipkow@25162
  1815
by(fastsimp simp:surj_def dest!: endo_inj_surj)
nipkow@25162
  1816
nipkow@25162
  1817
corollary infinite_UNIV_nat: "~finite(UNIV::nat set)"
nipkow@25162
  1818
proof
nipkow@25162
  1819
  assume "finite(UNIV::nat set)"
nipkow@25162
  1820
  with finite_UNIV_inj_surj[of Suc]
nipkow@25162
  1821
  show False by simp (blast dest: Suc_neq_Zero surjD)
nipkow@25162
  1822
qed
nipkow@25162
  1823
nipkow@25162
  1824
nipkow@15392
  1825
subsection{* A fold functional for non-empty sets *}
nipkow@15392
  1826
nipkow@15392
  1827
text{* Does not require start value. *}
wenzelm@12396
  1828
berghofe@23736
  1829
inductive
berghofe@22262
  1830
  fold1Set :: "('a => 'a => 'a) => 'a set => 'a => bool"
berghofe@22262
  1831
  for f :: "'a => 'a => 'a"
berghofe@22262
  1832
where
paulson@15506
  1833
  fold1Set_insertI [intro]:
berghofe@22262
  1834
   "\<lbrakk> foldSet f id a A x; a \<notin> A \<rbrakk> \<Longrightarrow> fold1Set f (insert a A) x"
wenzelm@12396
  1835
nipkow@15392
  1836
constdefs
nipkow@15392
  1837
  fold1 :: "('a => 'a => 'a) => 'a set => 'a"
berghofe@22262
  1838
  "fold1 f A == THE x. fold1Set f A x"
paulson@15506
  1839
paulson@15506
  1840
lemma fold1Set_nonempty:
haftmann@22917
  1841
  "fold1Set f A x \<Longrightarrow> A \<noteq> {}"
haftmann@22917
  1842
  by(erule fold1Set.cases, simp_all) 
nipkow@15392
  1843
berghofe@23736
  1844
inductive_cases empty_fold1SetE [elim!]: "fold1Set f {} x"
berghofe@23736
  1845
berghofe@23736
  1846
inductive_cases insert_fold1SetE [elim!]: "fold1Set f (insert a X) x"
berghofe@22262
  1847
berghofe@22262
  1848
berghofe@22262
  1849
lemma fold1Set_sing [iff]: "(fold1Set f {a} b) = (a = b)"
paulson@15506
  1850
  by (blast intro: foldSet.intros elim: foldSet.cases)
nipkow@15392
  1851
haftmann@22917
  1852
lemma fold1_singleton [simp]: "fold1 f {a} = a"
paulson@15508
  1853
  by (unfold fold1_def) blast
wenzelm@12396
  1854
paulson@15508
  1855
lemma finite_nonempty_imp_fold1Set:
berghofe@22262
  1856
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> EX x. fold1Set f A x"
paulson@15508
  1857
apply (induct A rule: finite_induct)
paulson@15508
  1858
apply (auto dest: finite_imp_foldSet [of _ f id])  
paulson@15508
  1859
done
paulson@15506
  1860
paulson@15506
  1861
text{*First, some lemmas about @{term foldSet}.*}
nipkow@15392
  1862
paulson@15508
  1863
lemma (in ACf) foldSet_insert_swap:
berghofe@22262
  1864
assumes fold: "foldSet f id b A y"
berghofe@22262
  1865
shows "b \<notin> A \<Longrightarrow> foldSet f id z (insert b A) (z \<cdot> y)"
paulson@15508
  1866
using fold
paulson@15508
  1867
proof (induct rule: foldSet.induct)
paulson@15508
  1868
  case emptyI thus ?case by (force simp add: fold_insert_aux commute)
paulson@15508
  1869
next
berghofe@22262
  1870
  case (insertI x A y)
berghofe@22262
  1871
    have "foldSet f (\<lambda>u. u) z (insert x (insert b A)) (x \<cdot> (z \<cdot> y))"
paulson@15521
  1872
      using insertI by force  --{*how does @{term id} get unfolded?*}
paulson@15508
  1873
    thus ?case by (simp add: insert_commute AC)
paulson@15508
  1874
qed
paulson@15508
  1875
paulson@15508
  1876
lemma (in ACf) foldSet_permute_diff:
berghofe@22262
  1877
assumes fold: "foldSet f id b A x"
berghofe@22262
  1878
shows "!!a. \<lbrakk>a \<in> A; b \<notin> A\<rbrakk> \<Longrightarrow> foldSet f id a (insert b (A-{a})) x"
paulson@15508
  1879
using fold
paulson@15508
  1880
proof (induct rule: foldSet.induct)
paulson@15508
  1881
  case emptyI thus ?case by simp
paulson@15508
  1882
next
berghofe@22262
  1883
  case (insertI x A y)
paulson@15521
  1884
  have "a = x \<or> a \<in> A" using insertI by simp
paulson@15521
  1885
  thus ?case
paulson@15521
  1886
  proof
paulson@15521
  1887
    assume "a = x"
paulson@15521
  1888
    with insertI show ?thesis
paulson@15521
  1889
      by (simp add: id_def [symmetric], blast intro: foldSet_insert_swap) 
paulson@15521
  1890
  next
paulson@15521
  1891
    assume ainA: "a \<in> A"
berghofe@22262
  1892
    hence "foldSet f id a (insert x (insert b (A - {a}))) (x \<cdot> y)"
paulson@15521
  1893
      using insertI by (force simp: id_def)
paulson@15521
  1894
    moreover
paulson@15521
  1895
    have "insert x (insert b (A - {a})) = insert b (insert x A - {a})"
paulson@15521
  1896
      using ainA insertI by blast
paulson@15521
  1897
    ultimately show ?thesis by (simp add: id_def)
paulson@15508
  1898
  qed
paulson@15508
  1899
qed
paulson@15508
  1900
paulson@15508
  1901
lemma (in ACf) fold1_eq_fold:
paulson@15508
  1902
     "[|finite A; a \<notin> A|] ==> fold1 f (insert a A) = fold f id a A"
paulson@15508
  1903
apply (simp add: fold1_def fold_def) 
paulson@15508
  1904
apply (rule the_equality)
paulson@15508
  1905
apply (best intro: foldSet_determ theI dest: finite_imp_foldSet [of _ f id]) 
paulson@15508
  1906
apply (rule sym, clarify)
paulson@15508
  1907
apply (case_tac "Aa=A")
paulson@15508
  1908
 apply (best intro: the_equality foldSet_determ)  
berghofe@22262
  1909
apply (subgoal_tac "foldSet f id a A x")
paulson@15508
  1910
 apply (best intro: the_equality foldSet_determ)  
paulson@15508
  1911
apply (subgoal_tac "insert aa (Aa - {a}) = A") 
paulson@15508
  1912
 prefer 2 apply (blast elim: equalityE) 
paulson@15508
  1913
apply (auto dest: foldSet_permute_diff [where a=a]) 
paulson@15508
  1914
done
paulson@15508
  1915
paulson@15521
  1916
lemma nonempty_iff: "(A \<noteq> {}) = (\<exists>x B. A = insert x B & x \<notin> B)"
paulson@15521
  1917
apply safe
paulson@15521
  1918
apply simp 
paulson@15521
  1919
apply (drule_tac x=x in spec)
paulson@15521
  1920
apply (drule_tac x="A-{x}" in spec, auto) 
paulson@15508
  1921
done
paulson@15508
  1922
paulson@15521
  1923
lemma (in ACf) fold1_insert:
paulson@15521
  1924
  assumes nonempty: "A \<noteq> {}" and A: "finite A" "x \<notin> A"
paulson@15521
  1925
  shows "fold1 f (insert x A) = f x (fold1 f A)"
paulson@15521
  1926
proof -
paulson@15521
  1927
  from nonempty obtain a A' where "A = insert a A' & a ~: A'" 
paulson@15521
  1928
    by (auto simp add: nonempty_iff)
paulson@15521
  1929
  with A show ?thesis
paulson@15521
  1930
    by (simp add: insert_commute [of x] fold1_eq_fold eq_commute) 
paulson@15521
  1931
qed
paulson@15521
  1932
paulson@15509
  1933
lemma (in ACIf) fold1_insert_idem [simp]:
paulson@15521
  1934
  assumes nonempty: "A \<noteq> {}" and A: "finite A" 
paulson@15521
  1935
  shows "fold1 f (insert x A) = f x (fold1 f A)"
paulson@15521
  1936
proof -
paulson@15521
  1937
  from nonempty obtain a A' where A': "A = insert a A' & a ~: A'" 
paulson@15521
  1938
    by (auto simp add: nonempty_iff)
paulson@15521
  1939
  show ?thesis
paulson@15521
  1940
  proof cases
paulson@15521
  1941
    assume "a = x"
paulson@15521
  1942
    thus ?thesis 
paulson@15521
  1943
    proof cases
paulson@15521
  1944
      assume "A' = {}"
paulson@15521
  1945
      with prems show ?thesis by (simp add: idem) 
paulson@15521
  1946
    next
paulson@15521
  1947
      assume "A' \<noteq> {}"
paulson@15521
  1948
      with prems show ?thesis
paulson@15521
  1949
	by (simp add: fold1_insert assoc [symmetric] idem) 
paulson@15521
  1950
    qed
paulson@15521
  1951
  next
paulson@15521
  1952
    assume "a \<noteq> x"
paulson@15521
  1953
    with prems show ?thesis
paulson@15521
  1954
      by (simp add: insert_commute fold1_eq_fold fold_insert_idem)
paulson@15521
  1955
  qed
paulson@15521
  1956
qed
paulson@15506
  1957
haftmann@22917
  1958
lemma (in ACIf) hom_fold1_commute:
haftmann@22917
  1959
assumes hom: "!!x y. h(f x y) = f (h x) (h y)"
haftmann@22917
  1960
and N: "finite N" "N \<noteq> {}" shows "h(fold1 f N) = fold1 f (h ` N)"
haftmann@22917
  1961
using N proof (induct rule: finite_ne_induct)
haftmann@22917
  1962
  case singleton thus ?case by simp
haftmann@22917
  1963
next
haftmann@22917
  1964
  case (insert n N)
haftmann@22917
  1965
  then have "h(fold1 f (insert n N)) = h(f n (fold1 f N))" by simp
haftmann@22917
  1966
  also have "\<dots> = f (h n) (h(fold1 f N))" by(rule hom)
haftmann@22917
  1967
  also have "h(fold1 f N) = fold1 f (h ` N)" by(rule insert)
haftmann@22917
  1968
  also have "f (h n) \<dots> = fold1 f (insert (h n) (h ` N))"
haftmann@22917
  1969
    using insert by(simp)
haftmann@22917
  1970
  also have "insert (h n) (h ` N) = h ` insert n N" by simp
haftmann@22917
  1971
  finally show ?case .
haftmann@22917
  1972
qed
haftmann@22917
  1973
paulson@15506
  1974
paulson@15508
  1975
text{* Now the recursion rules for definitions: *}
paulson@15508
  1976
haftmann@22917
  1977
lemma fold1_singleton_def: "g = fold1 f \<Longrightarrow> g {a} = a"
paulson@15508
  1978
by(simp add:fold1_singleton)
paulson@15508
  1979
paulson@15508
  1980
lemma (in ACf) fold1_insert_def:
haftmann@22917
  1981
  "\<lbrakk> g = fold1 f; finite A; x \<notin> A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x \<cdot> (g A)"
paulson@15508
  1982
by(simp add:fold1_insert)
paulson@15508
  1983
paulson@15509
  1984
lemma (in ACIf) fold1_insert_idem_def:
haftmann@22917
  1985
  "\<lbrakk> g = fold1 f; finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x \<cdot> (g A)"
paulson@15509
  1986
by(simp add:fold1_insert_idem)
paulson@15508
  1987
paulson@15508
  1988
subsubsection{* Determinacy for @{term fold1Set} *}
paulson@15508
  1989
paulson@15508
  1990
text{*Not actually used!!*}
wenzelm@12396
  1991
paulson@15506
  1992
lemma (in ACf) foldSet_permute:
berghofe@22262
  1993
  "[|foldSet f id b (insert a A) x; a \<notin> A; b \<notin> A|]
berghofe@22262
  1994
   ==> foldSet f id a (insert b A) x"
paulson@15506
  1995
apply (case_tac "a=b") 
paulson@15506
  1996
apply (auto dest: foldSet_permute_diff) 
paulson@15506
  1997
done
nipkow@15376
  1998
paulson@15506
  1999
lemma (in ACf) fold1Set_determ:
berghofe@22262
  2000
  "fold1Set f A x ==> fold1Set f A y ==> y = x"
paulson@15506
  2001
proof (clarify elim!: fold1Set.cases)
paulson@15506
  2002
  fix A x B y a b
berghofe@22262
  2003
  assume Ax: "foldSet f id a A x"
berghofe@22262
  2004
  assume By: "foldSet f id b B y"
paulson@15506
  2005
  assume anotA:  "a \<notin> A"
paulson@15506
  2006
  assume bnotB:  "b \<notin> B"
paulson@15506
  2007
  assume eq: "insert a A = insert b B"
paulson@15506
  2008
  show "y=x"
paulson@15506
  2009
  proof cases
paulson@15506
  2010
    assume same: "a=b"
paulson@15506
  2011
    hence "A=B" using anotA bnotB eq by (blast elim!: equalityE)
paulson@15506
  2012
    thus ?thesis using Ax By same by (blast intro: foldSet_determ)
nipkow@15392
  2013
  next
paulson@15506
  2014
    assume diff: "a\<noteq>b"
paulson@15506
  2015
    let ?D = "B - {a}"
paulson@15506
  2016
    have B: "B = insert a ?D" and A: "A = insert b ?D"
paulson@15506
  2017
     and aB: "a \<in> B" and bA: "b \<in> A"
paulson@15506
  2018
      using eq anotA bnotB diff by (blast elim!:equalityE)+
paulson@15506
  2019
    with aB bnotB By
berghofe@22262
  2020
    have "foldSet f id a (insert b ?D) y" 
paulson@15506
  2021
      by (auto intro: foldSet_permute simp add: insert_absorb)
paulson@15506
  2022
    moreover
berghofe@22262
  2023
    have "foldSet f id a (insert b ?D) x"
paulson@15506
  2024
      by (simp add: A [symmetric] Ax) 
paulson@15506
  2025
    ultimately show ?thesis by (blast intro: foldSet_determ) 
nipkow@15392
  2026
  qed
wenzelm@12396
  2027
qed
wenzelm@12396
  2028
berghofe@22262
  2029
lemma (in ACf) fold1Set_equality: "fold1Set f A y ==> fold1 f A = y"
paulson@15506
  2030
  by (unfold fold1_def) (blast intro: fold1Set_determ)
paulson@15506
  2031
paulson@15506
  2032
declare
paulson@15506
  2033
  empty_foldSetE [rule del]   foldSet.intros [rule del]
paulson@15506
  2034
  empty_fold1SetE [rule del]  insert_fold1SetE [rule del]
ballarin@19931
  2035
  -- {* No more proofs involve these relations. *}
nipkow@15376
  2036
haftmann@22917
  2037
nipkow@15497
  2038
subsubsection{* Semi-Lattices *}
nipkow@15497
  2039
haftmann@22917
  2040
locale ACIfSL = ord + ACIf +
haftmann@24748
  2041
  assumes below_def: "less_eq x y \<longleftrightarrow> x \<cdot> y = x"
haftmann@24748
  2042
  and strict_below_def: "less x y \<longleftrightarrow> less_eq x y \<and> x \<noteq> y"
haftmann@22917
  2043
begin
haftmann@22917
  2044
haftmann@25062
  2045
notation
haftmann@25062
  2046
  less_eq  ("op \<^loc><=") and
haftmann@25062
  2047
  less_eq  ("(_/ \<^loc><= _)" [51, 51] 50) and
haftmann@25062
  2048
  less  ("op \<^loc><") and
haftmann@25062
  2049
  less  ("(_/ \<^loc>< _)"  [51, 51] 50)
haftmann@25062
  2050
  
haftmann@25062
  2051
notation (xsymbols)
haftmann@25062
  2052
  less_eq  ("op \<^loc>\<le>") and
haftmann@25062
  2053
  less_eq  ("(_/ \<^loc>\<le> _)"  [51, 51] 50)
haftmann@25062
  2054
haftmann@25062
  2055
notation (HTML output)
haftmann@25062
  2056
  less_eq  ("op \<^loc>\<le>") and
haftmann@25062
  2057
  less_eq  ("(_/ \<^loc>\<le> _)"  [51, 51] 50)
haftmann@25062
  2058
haftmann@22917
  2059
lemma below_refl [simp]: "x \<^loc>\<le> x"
haftmann@22917
  2060
  by (simp add: below_def idem)
haftmann@22917
  2061
haftmann@22917
  2062
lemma below_antisym:
haftmann@22917
  2063
  assumes xy: "x \<^loc>\<le> y" and yx: "y \<^loc>\<le> x"
haftmann@22917
  2064
  shows "x = y"
haftmann@22917
  2065
  using xy [unfolded below_def, symmetric]
haftmann@22917
  2066
    yx [unfolded below_def commute]
haftmann@22917
  2067
  by (rule trans)
haftmann@22917
  2068
haftmann@22917
  2069
lemma below_trans:
haftmann@22917
  2070
  assumes xy: "x \<^loc>\<le> y" and yz: "y \<^loc>\<le> z"
haftmann@22917
  2071
  shows "x \<^loc>\<le> z"
haftmann@22917
  2072
proof -
haftmann@22917
  2073
  from xy have x_xy: "x \<cdot> y = x" by (simp add: below_def)
haftmann@22917
  2074
  from yz have y_yz: "y \<cdot> z = y" by (simp add: below_def)
haftmann@22917
  2075
  from y_yz have "x \<cdot> y \<cdot> z = x \<cdot> y" by (simp add: assoc)
haftmann@22917
  2076
  with x_xy have "x \<cdot> y \<cdot> z = x"  by simp
haftmann@22917
  2077
  moreover from x_xy have "x \<cdot> z = x \<cdot> y \<cdot> z" by simp
haftmann@22917
  2078
  ultimately have "x \<cdot> z = x" by simp
haftmann@22917
  2079
  then show ?thesis by (simp add: below_def)
haftmann@22917
  2080
qed
haftmann@22917
  2081
haftmann@24748
  2082
lemma below_f_conv [simp,noatp]: "x \<^loc>\<le> y \<cdot> z = (x \<^loc>\<le> y \<and> x \<^loc>\<le> z)"
nipkow@15497
  2083
proof
haftmann@24748
  2084
  assume "x \<^loc>\<le> y \<cdot> z"
nipkow@15497
  2085
  hence xyzx: "x \<cdot> (y \<cdot> z) = x"  by(simp add: below_def)
nipkow@15497
  2086
  have "x \<cdot> y = x"
nipkow@15497
  2087
  proof -
nipkow@15497
  2088
    have "x \<cdot> y = (x \<cdot> (y \<cdot> z)) \<cdot> y" by(rule subst[OF xyzx], rule refl)
nipkow@15497
  2089
    also have "\<dots> = x \<cdot> (y \<cdot> z)" by(simp add:ACI)
nipkow@15497
  2090
    also have "\<dots> = x" by(rule xyzx)
nipkow@15497
  2091
    finally show ?thesis .
nipkow@15497
  2092
  qed
nipkow@15497
  2093
  moreover have "x \<cdot> z = x"
nipkow@15497
  2094
  proof -
nipkow@15497
  2095
    have "x \<cdot> z = (x \<cdot> (y \<cdot> z)) \<cdot> z" by(rule subst[OF xyzx], rule refl)
nipkow@15497
  2096
    also have "\<dots> = x \<cdot> (y \<cdot> z)" by(simp add:ACI)
nipkow@15497
  2097
    also have "\<dots> = x" by(rule xyzx)
nipkow@15497
  2098
    finally show ?thesis .
nipkow@15497
  2099
  qed
haftmann@24748
  2100
  ultimately show "x \<^loc>\<le> y \<and> x \<^loc>\<le> z" by(simp add: below_def)
nipkow@15497
  2101
next
haftmann@24748
  2102
  assume a: "x \<^loc>\<le> y \<and> x \<^loc>\<le> z"
nipkow@15497
  2103
  hence y: "x \<cdot> y = x" and z: "x \<cdot> z = x" by(simp_all add: below_def)
nipkow@15497
  2104
  have "x \<cdot> (y \<cdot> z) = (x \<cdot> y) \<cdot> z" by(simp add:assoc)
nipkow@15497
  2105
  also have "x \<cdot> y = x" using a by(simp_all add: below_def)
nipkow@15497
  2106
  also have "x \<cdot> z = x" using a by(simp_all add: below_def)
haftmann@24748
  2107
  finally show "x \<^loc>\<le> y \<cdot> z" by(simp_all add: below_def)
nipkow@15497
  2108
qed
nipkow@15497
  2109
haftmann@22917
  2110
end
haftmann@22917
  2111
haftmann@22917
  2112
interpretation ACIfSL < order
haftmann@22917
  2113
by unfold_locales
haftmann@22917
  2114
  (simp add: strict_below_def, auto intro: below_refl below_trans below_antisym)
haftmann@22917
  2115
haftmann@22917
  2116
locale ACIfSLlin = ACIfSL +
haftmann@22917
  2117
  assumes lin: "x\<cdot>y \<in> {x,y}"
haftmann@22917
  2118
begin
haftmann@22917
  2119
haftmann@22917
  2120
lemma above_f_conv:
haftmann@24748
  2121
 "x \<cdot> y \<^loc>\<le> z = (x \<^loc>\<le> z \<or> y \<^loc>\<le> z)"
nipkow@15497
  2122
proof
haftmann@24748
  2123
  assume a: "x \<cdot> y \<^loc>\<le> z"
nipkow@15497
  2124
  have "x \<cdot> y = x \<or> x \<cdot> y = y" using lin[of x y] by simp
haftmann@24748
  2125
  thus "x \<^loc>\<le> z \<or> y \<^loc>\<le> z"
nipkow@15497
  2126
  proof
haftmann@24748
  2127
    assume "x \<cdot> y = x" hence "x \<^loc>\<le> z" by(rule subst)(rule a) thus ?thesis ..
nipkow@15497
  2128
  next
haftmann@24748
  2129
    assume "x \<cdot> y = y" hence "y \<^loc>\<le> z" by(rule subst)(rule a) thus ?thesis ..
nipkow@15497
  2130
  qed
nipkow@15497
  2131
next
haftmann@24748
  2132
  assume "x \<^loc>\<le> z \<or> y \<^loc>\<le> z"
haftmann@24748
  2133
  thus "x \<cdot> y \<^loc>\<le> z"
nipkow@15497
  2134
  proof
haftmann@24748
  2135
    assume a: "x \<^loc>\<le> z"
nipkow@15497
  2136
    have "(x \<cdot> y) \<cdot> z = (x \<cdot> z) \<cdot> y" by(simp add:ACI)
nipkow@15497
  2137
    also have "x \<cdot> z = x" using a by(simp add:below_def)
haftmann@24748
  2138
    finally show "x \<cdot> y \<^loc>\<le> z" by(simp add:below_def)
nipkow@15497
  2139
  next
haftmann@24748
  2140
    assume a: "y \<^loc>\<le> z"
nipkow@15497
  2141
    have "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)" by(simp add:ACI)
nipkow@15497
  2142
    also have "y \<cdot> z = y" using a by(simp add:below_def)
haftmann@24748
  2143
    finally show "x \<cdot> y \<^loc>\<le> z" by(simp add:below_def)
nipkow@15497
  2144
  qed
nipkow@15497
  2145
qed
nipkow@15497
  2146
haftmann@24748
  2147
lemma strict_below_f_conv[simp,noatp]: "x \<^loc>< y \<cdot> z = (x \<^loc>< y \<and> x \<^loc>< z)"
nipkow@18493
  2148
apply(simp add: strict_below_def)
nipkow@18493
  2149
using lin[of y z] by (auto simp:below_def ACI)
nipkow@18493
  2150
haftmann@22917
  2151
lemma strict_above_f_conv:
haftmann@24748
  2152
  "x \<cdot> y \<^loc>< z = (x \<^loc>< z \<or> y \<^loc>< z)"
nipkow@18493
  2153
apply(simp add: strict_below_def above_f_conv)
nipkow@18493
  2154
using lin[of y z] lin[of x z] by (auto simp:below_def ACI)
nipkow@18493
  2155
haftmann@22917
  2156
end
haftmann@22917
  2157
haftmann@22917
  2158
interpretation ACIfSLlin < linorder
haftmann@22917
  2159
  by unfold_locales
haftmann@22917
  2160
    (insert lin [simplified insert_iff], simp add: below_def commute)
haftmann@22917
  2161
nipkow@18493
  2162
nipkow@15502
  2163
subsubsection{* Lemmas about @{text fold1} *}
nipkow@15484
  2164
nipkow@15484
  2165
lemma (in ACf) fold1_Un:
nipkow@15484
  2166
assumes A: "finite A" "A \<noteq> {}"
nipkow@15484
  2167
shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow> A Int B = {} \<Longrightarrow>
nipkow@15484
  2168
       fold1 f (A Un B) = f (fold1 f A) (fold1 f B)"
nipkow@15484
  2169
using A
nipkow@15484
  2170
proof(induct rule:finite_ne_induct)
nipkow@15484
  2171
  case singleton thus ?case by(simp add:fold1_insert)
nipkow@15484
  2172
next
nipkow@15484
  2173
  case insert thus ?case by (simp add:fold1_insert assoc)
nipkow@15484
  2174
qed
nipkow@15484
  2175
nipkow@15484
  2176
lemma (in ACIf) fold1_Un2:
nipkow@15484
  2177
assumes A: "finite A" "A \<noteq> {}"
nipkow@15484
  2178
shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow>
nipkow@15484
  2179
       fold1 f (A Un B) = f (fold1 f A) (fold1 f B)"
nipkow@15484
  2180
using A
nipkow@15484
  2181
proof(induct rule:finite_ne_induct)
paulson@15509
  2182
  case singleton thus ?case by(simp add:fold1_insert_idem)
nipkow@15484
  2183
next
paulson@15509
  2184
  case insert thus ?case by (simp add:fold1_insert_idem assoc)
nipkow@15484
  2185
qed
nipkow@15484
  2186
nipkow@15484
  2187
lemma (in ACf) fold1_in:
nipkow@15484
  2188
  assumes A: "finite (A)" "A \<noteq> {}" and elem: "\<And>x y. x\<cdot>y \<in> {x,y}"
nipkow@15484
  2189
  shows "fold1 f A \<in> A"
nipkow@15484
  2190
using A
nipkow@15484
  2191
proof (induct rule:finite_ne_induct)
paulson@15506
  2192
  case singleton thus ?case by simp
nipkow@15484
  2193
next
nipkow@15484
  2194
  case insert thus ?case using elem by (force simp add:fold1_insert)
nipkow@15484
  2195
qed
nipkow@15484
  2196
nipkow@15497
  2197
lemma (in ACIfSL) below_fold1_iff:
nipkow@15497
  2198
assumes A: "finite A" "A \<noteq> {}"
haftmann@24748
  2199
shows "x \<^loc>\<le> fold1 f A = (\<forall>a\<in>A. x \<^loc>\<le> a)"
nipkow@15497
  2200
using A
nipkow@15497
  2201
by(induct rule:finite_ne_induct) simp_all
nipkow@15497
  2202
nipkow@18493
  2203
lemma (in ACIfSLlin) strict_below_fold1_iff:
haftmann@24748
  2204
  "finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> x \<^loc>< fold1 f A = (\<forall>a\<in>A. x \<^loc>< a)"
nipkow@18493
  2205
by(induct rule:finite_ne_induct) simp_all
nipkow@18493
  2206
nipkow@18493
  2207
nipkow@15497
  2208
lemma (in ACIfSL) fold1_belowI:
nipkow@15497
  2209
assumes A: "finite A" "A \<noteq> {}"
haftmann@24748
  2210
shows "a \<in> A \<Longrightarrow> fold1 f A \<^loc>\<le> a"
nipkow@15484
  2211
using A
nipkow@15484
  2212
proof (induct rule:finite_ne_induct)
nipkow@15497
  2213
  case singleton thus ?case by simp
nipkow@15484
  2214
next
nipkow@15497
  2215
  case (insert x F)
berghofe@15517
  2216
  from insert(5) have "a = x \<or> a \<in> F" by simp
nipkow@15497
  2217
  thus ?case
nipkow@15497
  2218
  proof
nipkow@15497
  2219
    assume "a = x" thus ?thesis using insert by(simp add:below_def ACI)
nipkow@15497
  2220
  next
nipkow@15497
  2221
    assume "a \<in> F"
haftmann@24748
  2222
    hence bel: "fold1 f F \<^loc>\<le> a" by(rule insert)
paulson@15508
  2223
    have "fold1 f (insert x F) \<cdot> a = x \<cdot> (fold1 f F \<cdot> a)"
nipkow@15497
  2224
      using insert by(simp add:below_def ACI)
paulson@15508
  2225
    also have "fold1 f F \<cdot> a = fold1 f F"
nipkow@15497
  2226
      using bel  by(simp add:below_def ACI)
paulson@15508
  2227
    also have "x \<cdot> \<dots> = fold1 f (insert x F)"
nipkow@15497
  2228
      using insert by(simp add:below_def ACI)
nipkow@15497
  2229
    finally show ?thesis  by(simp add:below_def)
nipkow@15497
  2230
  qed
nipkow@15484
  2231
qed
nipkow@15484
  2232
nipkow@15497
  2233
lemma (in ACIfSLlin) fold1_below_iff:
nipkow@15497
  2234
assumes A: "finite A" "A \<noteq> {}"
haftmann@24748
  2235
shows "fold1 f A \<^loc>\<le> x = (\<exists>a\<in>A. a \<^loc>\<le> x)"
nipkow@15484
  2236
using A
nipkow@15497
  2237
by(induct rule:finite_ne_induct)(simp_all add:above_f_conv)
nipkow@15484
  2238
nipkow@18493
  2239
lemma (in ACIfSLlin) fold1_strict_below_iff:
nipkow@18493
  2240
assumes A: "finite A" "A \<noteq> {}"
haftmann@24748
  2241
shows "fold1 f A \<^loc>< x = (\<exists>a\<in>A. a \<^loc>< x)"
nipkow@18493
  2242
using A
nipkow@18493
  2243
by(induct rule:finite_ne_induct)(simp_all add:strict_above_f_conv)
nipkow@18493
  2244
nipkow@18423
  2245
lemma (in ACIfSLlin) fold1_antimono:
nipkow@18423
  2246
assumes "A \<noteq> {}" and "A \<subseteq> B" and "finite B"
haftmann@24748
  2247
shows "fold1 f B \<^loc>\<le> fold1 f A"
nipkow@18423
  2248
proof(cases)
nipkow@18423
  2249
  assume "A = B" thus ?thesis by simp
nipkow@18423
  2250
next
nipkow@18423
  2251
  assume "A \<noteq> B"
nipkow@18423
  2252
  have B: "B = A \<union> (B-A)" using `A \<subseteq> B` by blast
nipkow@18423
  2253
  have "fold1 f B = fold1 f (A \<union> (B-A))" by(subst B)(rule refl)
nipkow@18423
  2254
  also have "\<dots> = f (fold1 f A) (fold1 f (B-A))"
nipkow@18423
  2255
  proof -
nipkow@18423
  2256
    have "finite A" by(rule finite_subset[OF `A \<subseteq> B` `finite B`])
nipkow@18493
  2257
    moreover have "finite(B-A)" by(rule finite_Diff[OF `finite B`]) (* by(blast intro:finite_Diff prems) fails *)
nipkow@18423
  2258
    moreover have "(B-A) \<noteq> {}" using prems by blast
nipkow@18423
  2259
    moreover have "A Int (B-A) = {}" using prems by blast
nipkow@18423
  2260
    ultimately show ?thesis using `A \<noteq> {}` by(rule_tac fold1_Un)
nipkow@18423
  2261
  qed
haftmann@24748
  2262
  also have "\<dots> \<^loc>\<le> fold1 f A" by(simp add: above_f_conv)
nipkow@18423
  2263
  finally show ?thesis .
nipkow@18423
  2264
qed
nipkow@18423
  2265
nipkow@18423
  2266
haftmann@22917
  2267
subsubsection {* Fold1 in lattices with @{const inf} and @{const sup} *}
haftmann@22917
  2268
haftmann@22917
  2269
text{*
haftmann@22917
  2270
  As an application of @{text fold1} we define infimum
haftmann@22917
  2271
  and supremum in (not necessarily complete!) lattices
haftmann@22917
  2272
  over (non-empty) sets by means of @{text fold1}.
haftmann@22917
  2273
*}
haftmann@22917
  2274
haftmann@25062
  2275
lemma (in lower_semilattice) ACf_inf: "ACf inf"
haftmann@22917
  2276
  by (blast intro: ACf.intro inf_commute inf_assoc)
haftmann@22917
  2277
haftmann@25062
  2278
lemma (in upper_semilattice) ACf_sup: "ACf sup"
haftmann@22917
  2279
  by (blast intro: ACf.intro sup_commute sup_assoc)
haftmann@22917
  2280
haftmann@25062
  2281
lemma (in lower_semilattice) ACIf_inf: "ACIf inf"
nipkow@15500
  2282
apply(rule ACIf.intro)
nipkow@15500
  2283
apply(rule ACf_inf)
nipkow@15500
  2284
apply(rule ACIf_axioms.intro)
nipkow@15500
  2285
apply(rule inf_idem)
nipkow@15500
  2286
done
nipkow@15500
  2287
haftmann@25062
  2288
lemma (in upper_semilattice) ACIf_sup: "ACIf sup"
nipkow@15500
  2289
apply(rule ACIf.intro)
nipkow@15500
  2290
apply(rule ACf_sup)
nipkow@15500
  2291
apply(rule ACIf_axioms.intro)
nipkow@15500
  2292
apply(rule sup_idem)
nipkow@15500
  2293
done
nipkow@15500
  2294
haftmann@25062
  2295
lemma (in lower_semilattice) ACIfSL_inf: "ACIfSL (op \<le>) (op <) inf"
nipkow@15500
  2296
apply(rule ACIfSL.intro)
ballarin@19931
  2297
apply(rule ACIf.intro)
nipkow@15500
  2298
apply(rule ACf_inf)
nipkow@15500
  2299
apply(rule ACIf.axioms[OF ACIf_inf])
nipkow@15500
  2300
apply(rule ACIfSL_axioms.intro)
nipkow@15500
  2301
apply(rule iffI)
nipkow@21733
  2302
 apply(blast intro: antisym inf_le1 inf_le2 inf_greatest refl)
nipkow@15500
  2303
apply(erule subst)
nipkow@15500
  2304
apply(rule inf_le2)
haftmann@22917
  2305
apply(rule less_le)
nipkow@15500
  2306
done
nipkow@15500
  2307
haftmann@25062
  2308
lemma (in upper_semilattice) ACIfSL_sup: "ACIfSL (%x y. y \<le> x) (%x y. y < x) sup"
nipkow@15500
  2309
apply(rule ACIfSL.intro)
ballarin@19931
  2310
apply(rule ACIf.intro)
nipkow@15500
  2311
apply(rule ACf_sup)
nipkow@15500
  2312
apply(rule ACIf.axioms[OF ACIf_sup])
nipkow@15500
  2313
apply(rule ACIfSL_axioms.intro)
nipkow@15500
  2314
apply(rule iffI)
nipkow@21733
  2315
 apply(blast intro: antisym sup_ge1 sup_ge2 sup_least refl)
nipkow@15500
  2316
apply(erule subst)
nipkow@15500
  2317
apply(rule sup_ge2)
haftmann@22917
  2318
apply(simp add: neq_commute less_le)
nipkow@15500
  2319
done
nipkow@15500
  2320
haftmann@24342
  2321
context lattice
haftmann@22917
  2322
begin
haftmann@22917
  2323
haftmann@22917
  2324
definition
haftmann@24342
  2325
  Inf_fin :: "'a set \<Rightarrow> 'a" ("\<Sqinter>\<^bsub>fin\<^esub>_" [900] 900)
haftmann@22917
  2326
where
haftmann@25062
  2327
  "Inf_fin = fold1 inf"
haftmann@22917
  2328
haftmann@22917
  2329
definition
haftmann@24342
  2330
  Sup_fin :: "'a set \<Rightarrow> 'a" ("\<Squnion>\<^bsub>fin\<^esub>_" [900] 900)
haftmann@22917
  2331
where
haftmann@25062
  2332
  "Sup_fin = fold1 sup"
haftmann@25062
  2333
haftmann@25062
  2334
lemma Inf_le_Sup [simp]: "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> \<Sqinter>\<^bsub>fin\<^esub>A \<le> \<Squnion>\<^bsub>fin\<^esub>A"
haftmann@24342
  2335
apply(unfold Sup_fin_def Inf_fin_def)
nipkow@15500
  2336
apply(subgoal_tac "EX a. a:A")
nipkow@15500
  2337
prefer 2 apply blast
nipkow@15500
  2338
apply(erule exE)
haftmann@22388
  2339
apply(rule order_trans)
nipkow@15500
  2340
apply(erule (2) ACIfSL.fold1_belowI[OF ACIfSL_inf])
nipkow@15500
  2341
apply(erule (2) ACIfSL.fold1_belowI[OF ACIfSL_sup])
nipkow@15500
  2342
done
nipkow@15500
  2343
haftmann@24342
  2344
lemma sup_Inf_absorb [simp]:
haftmann@25062
  2345
  "\<lbrakk> finite A; A \<noteq> {}; a \<in> A \<rbrakk> \<Longrightarrow> (sup a (\<Sqinter>\<^bsub>fin\<^esub>A)) = a"
nipkow@15512
  2346
apply(subst sup_commute)
haftmann@24342
  2347
apply(simp add: Inf_fin_def sup_absorb2 ACIfSL.fold1_belowI [OF ACIfSL_inf])
nipkow@15504
  2348
done
nipkow@15504
  2349
haftmann@24342
  2350
lemma inf_Sup_absorb [simp]:
haftmann@25062
  2351
  "\<lbrakk> finite A; A \<noteq> {}; a \<in> A \<rbrakk> \<Longrightarrow> (inf a (\<Squnion>\<^bsub>fin\<^esub>A)) = a"
haftmann@24342
  2352
by(simp add: Sup_fin_def inf_absorb1 ACIfSL.fold1_belowI [OF ACIfSL_sup])
haftmann@24342
  2353
haftmann@24342
  2354
end
haftmann@24342
  2355
haftmann@24342
  2356
context distrib_lattice
haftmann@24342
  2357
begin
haftmann@24342
  2358
haftmann@24342
  2359
lemma sup_Inf1_distrib:
haftmann@25062
  2360
  "finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> sup x (\<Sqinter>\<^bsub>fin\<^esub>A) = \<Sqinter>\<^bsub>fin\<^esub>{sup x a|a. a \<in> A}"
haftmann@24342
  2361
apply(simp add: Inf_fin_def image_def
nipkow@18423
  2362
  ACIf.hom_fold1_commute[OF ACIf_inf, where h="sup x", OF sup_inf_distrib1])
nipkow@18423
  2363
apply(rule arg_cong, blast)
nipkow@18423
  2364
done
nipkow@18423
  2365
haftmann@24342
  2366
lemma sup_Inf2_distrib:
haftmann@24342
  2367
  assumes A: "finite A" "A \<noteq> {}" and B: "finite B" "B \<noteq> {}"
haftmann@25062
  2368
  shows "sup (\<Sqinter>\<^bsub>fin\<^esub>A) (\<Sqinter>\<^bsub>fin\<^esub>B) = \<Sqinter>\<^bsub>fin\<^esub>{sup a b|a b. a \<in> A \<and> b \<in> B}"
haftmann@24342
  2369
using A proof (induct rule: finite_ne_induct)
nipkow@15500
  2370
  case singleton thus ?case
haftmann@24342
  2371
    by (simp add: sup_Inf1_distrib [OF B] fold1_singleton_def [OF Inf_fin_def])
nipkow@15500
  2372
next
nipkow@15500
  2373
  case (insert x A)
haftmann@25062
  2374
  have finB: "finite {sup x b |b. b \<in> B}"
haftmann@25062
  2375
    by(rule finite_surj[where f = "sup x", OF B(1)], auto)
haftmann@25062
  2376
  have finAB: "finite {sup a b |a b. a \<in> A \<and> b \<in> B}"
nipkow@15500
  2377
  proof -
haftmann@25062
  2378
    have "{sup a b |a b. a \<in> A \<and> b \<in> B} = (UN a:A. UN b:B. {sup a b})"
nipkow@15500
  2379
      by blast
berghofe@15517
  2380
    thus ?thesis by(simp add: insert(1) B(1))
nipkow@15500
  2381
  qed
haftmann@25062
  2382
  have ne: "{sup a b |a b. a \<in> A \<and> b \<in> B} \<noteq> {}" using insert B by blast
haftmann@25062
  2383
  have "sup (\<Sqinter>\<^bsub>fin\<^esub>(insert x A)) (\<Sqinter>\<^bsub>fin\<^esub>B) = sup (inf x (\<Sqinter>\<^bsub>fin\<^esub>A)) (\<Sqinter>\<^bsub>fin\<^esub>B)"
haftmann@22917
  2384
    using insert
haftmann@24342
  2385
 by(simp add:ACIf.fold1_insert_idem_def[OF ACIf_inf Inf_fin_def])
haftmann@25062
  2386
  also have "\<dots> = inf (sup x (\<Sqinter>\<^bsub>fin\<^esub>B)) (sup (\<Sqinter>\<^bsub>fin\<^esub>A) (\<Sqinter>\<^bsub>fin\<^esub>B))" by(rule sup_inf_distrib2)
haftmann@25062
  2387
  also have "\<dots> = inf (\<Sqinter>\<^bsub>fin\<^esub>{sup x b|b. b \<in> B}) (\<Sqinter>\<^bsub>fin\<^esub>{sup a b|a b. a \<in> A \<and> b \<in> B})"
nipkow@15500
  2388
    using insert by(simp add:sup_Inf1_distrib[OF B])
haftmann@25062
  2389
  also have "\<dots> = \<Sqinter>\<^bsub>fin\<^esub>({sup x b |b. b \<in> B} \<union> {sup a b |a b. a \<in> A \<and> b \<in> B})"
haftmann@24342
  2390
    (is "_ = \<Sqinter>\<^bsub>fin\<^esub>?M")
nipkow@15500
  2391
    using B insert
haftmann@24342
  2392
    by (simp add: Inf_fin_def ACIf.fold1_Un2[OF ACIf_inf finB _ finAB ne])
haftmann@25062
  2393
  also have "?M = {sup a b |a b. a \<in> insert x A \<and> b \<in> B}"
nipkow@15500
  2394
    by blast
nipkow@15500
  2395
  finally show ?case .
nipkow@15500
  2396
qed
nipkow@15500
  2397
haftmann@24342
  2398
lemma inf_Sup1_distrib:
haftmann@25062
  2399
  "finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> inf x (\<Squnion>\<^bsub>fin\<^esub>A) = \<Squnion>\<^bsub>fin\<^esub>{inf x a|a. a \<in> A}"
haftmann@24342
  2400
apply (simp add: Sup_fin_def image_def
nipkow@18423
  2401
  ACIf.hom_fold1_commute[OF ACIf_sup, where h="inf x", OF inf_sup_distrib1])
haftmann@24342
  2402
apply (rule arg_cong, blast)
nipkow@18423
  2403
done
nipkow@18423
  2404
haftmann@24342
  2405
lemma inf_Sup2_distrib:
haftmann@24342
  2406
  assumes A: "finite A" "A \<noteq> {}" and B: "finite B" "B \<noteq> {}"
haftmann@25062
  2407
  shows "inf (\<Squnion>\<^bsub>fin\<^esub>A) (\<Squnion>\<^bsub>fin\<^esub>B) = \<Squnion>\<^bsub>fin\<^esub>{inf a b|a b. a \<in> A \<and> b \<in> B}"
haftmann@24342
  2408
using A proof (induct rule: finite_ne_induct)
nipkow@18423
  2409
  case singleton thus ?case
haftmann@24342
  2410
    by(simp add: inf_Sup1_distrib [OF B] fold1_singleton_def [OF Sup_fin_def])
nipkow@18423
  2411
next
nipkow@18423
  2412
  case (insert x A)
haftmann@25062
  2413
  have finB: "finite {inf x b |b. b \<in> B}"
haftmann@25062
  2414
    by(rule finite_surj[where f = "%b. inf x b", OF B(1)], auto)
haftmann@25062
  2415
  have finAB: "finite {inf a b |a b. a \<in> A \<and> b \<in> B}"
nipkow@18423
  2416
  proof -
haftmann@25062
  2417
    have "{inf a b |a b. a \<in> A \<and> b \<in> B} = (UN a:A. UN b:B. {inf a b})"
nipkow@18423
  2418
      by blast
nipkow@18423
  2419
    thus ?thesis by(simp add: insert(1) B(1))
nipkow@18423
  2420
  qed
haftmann@25062
  2421
  have ne: "{inf a b |a b. a \<in> A \<and> b \<in> B} \<noteq> {}" using insert B by blast
haftmann@25062
  2422
  have "inf (\<Squnion>\<^bsub>fin\<^esub>(insert x A)) (\<Squnion>\<^bsub>fin\<^esub>B) = inf (sup x (\<Squnion>\<^bsub>fin\<^esub>A)) (\<Squnion>\<^bsub>fin\<^esub>B)"
haftmann@24342
  2423
    using insert by (simp add: ACIf.fold1_insert_idem_def [OF ACIf_sup Sup_fin_def])
haftmann@25062
  2424
  also have "\<dots> = sup (inf x (\<Squnion>\<^bsub>fin\<^esub>B)) (inf (\<Squnion>\<^bsub>fin\<^esub>A) (\<Squnion>\<^bsub>fin\<^esub>B))" by(rule inf_sup_distrib2)
haftmann@25062
  2425
  also have "\<dots> = sup (\<Squnion>\<^bsub>fin\<^esub>{inf x b|b. b \<in> B}) (\<Squnion>\<^bsub>fin\<^esub>{inf a b|a b. a \<in> A \<and> b \<in> B})"
nipkow@18423
  2426
    using insert by(simp add:inf_Sup1_distrib[OF B])
haftmann@25062
  2427
  also have "\<dots> = \<Squnion>\<^bsub>fin\<^esub>({inf x b |b. b \<in> B} \<union> {inf a b |a b. a \<in> A \<and> b \<in> B})"
haftmann@24342
  2428
    (is "_ = \<Squnion>\<^bsub>fin\<^esub>?M")
nipkow@18423
  2429
    using B insert
haftmann@24342
  2430
    by (simp add: Sup_fin_def ACIf.fold1_Un2[OF ACIf_sup finB _ finAB ne])
haftmann@25062
  2431
  also have "?M = {inf a b |a b. a \<in> insert x A \<and> b \<in> B}"
nipkow@18423
  2432
    by blast
nipkow@18423
  2433
  finally show ?case .
nipkow@18423
  2434
qed
nipkow@18423
  2435
haftmann@24342
  2436
end
haftmann@24342
  2437
haftmann@24342
  2438
context complete_lattice
haftmann@24342
  2439
begin
haftmann@24342
  2440
haftmann@22917
  2441
text {*
haftmann@24342
  2442
  Coincidence on finite sets in complete lattices:
haftmann@22917
  2443
*}
haftmann@22917
  2444
haftmann@24342
  2445
lemma Inf_fin_Inf:
haftmann@25062
  2446
  "finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> \<Sqinter>\<^bsub>fin\<^esub>A = Inf A"
haftmann@24342
  2447
unfolding Inf_fin_def by (induct A set: finite)
nipkow@22941
  2448
   (simp_all add: Inf_insert_simp ACIf.fold1_insert_idem [OF ACIf_inf])
haftmann@22917
  2449
haftmann@24342
  2450
lemma Sup_fin_Sup:
haftmann@25062
  2451
  "finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> \<Squnion>\<^bsub>fin\<^esub>A = Sup A"
haftmann@24342
  2452
unfolding Sup_fin_def by (induct A set: finite)
nipkow@22941
  2453
   (simp_all add: Sup_insert_simp ACIf.fold1_insert_idem [OF ACIf_sup])
haftmann@22917
  2454
haftmann@24342
  2455
end
haftmann@24342
  2456
haftmann@22917
  2457
haftmann@22917
  2458
subsubsection {* Fold1 in linear orders with @{const min} and @{const max} *}
haftmann@22917
  2459
haftmann@22917
  2460
text{*
haftmann@22917
  2461
  As an application of @{text fold1} we define minimum
haftmann@22917
  2462
  and maximum in (not necessarily complete!) linear orders
haftmann@22917
  2463
  over (non-empty) sets by means of @{text fold1}.
haftmann@22917
  2464
*}
haftmann@22917
  2465
haftmann@24342
  2466
context linorder
haftmann@22917
  2467
begin
haftmann@22917
  2468
haftmann@22917
  2469
definition
haftmann@22917
  2470
  Min :: "'a set \<Rightarrow> 'a"
haftmann@22917
  2471
where
haftmann@22917
  2472
  "Min = fold1 min"
haftmann@22917
  2473
haftmann@22917
  2474
definition
haftmann@22917
  2475
  Max :: "'a set \<Rightarrow> 'a"
haftmann@22917
  2476
where
haftmann@22917
  2477
  "Max = fold1 max"
haftmann@22917
  2478
haftmann@24342
  2479
end context linorder begin
haftmann@24342
  2480
haftmann@22917
  2481
text {* recall: @{term min} and @{term max} behave like @{const inf} and @{const sup} *}
haftmann@22917
  2482
haftmann@22917
  2483
lemma ACIf_min: "ACIf min"
haftmann@22917
  2484
  by (rule lower_semilattice.ACIf_inf,
haftmann@23018
  2485
    rule lattice.axioms,
haftmann@23018
  2486
    rule distrib_lattice.axioms,
haftmann@22917
  2487
    rule distrib_lattice_min_max)
haftmann@22917
  2488
haftmann@22917
  2489
lemma ACf_min: "ACf min"
haftmann@22917
  2490
  by (rule lower_semilattice.ACf_inf,
haftmann@23018
  2491
    rule lattice.axioms,
haftmann@23018
  2492
    rule distrib_lattice.axioms,
haftmann@22917
  2493
    rule distrib_lattice_min_max)
haftmann@22917
  2494
haftmann@25062
  2495
lemma ACIfSL_min: "ACIfSL (op \<le>) (op <) min"
haftmann@22917
  2496
  by (rule lower_semilattice.ACIfSL_inf,
haftmann@23018
  2497
    rule lattice.axioms,
haftmann@23018
  2498
    rule distrib_lattice.axioms,
haftmann@22917
  2499
    rule distrib_lattice_min_max)
haftmann@22917
  2500
haftmann@25062
  2501
lemma ACIfSLlin_min: "ACIfSLlin (op \<le>) (op <) min"
haftmann@22917
  2502
  by (rule ACIfSLlin.intro,
haftmann@22917
  2503
    rule lower_semilattice.ACIfSL_inf,
haftmann@23018
  2504
    rule lattice.axioms,
haftmann@23018
  2505
    rule distrib_lattice.axioms,
haftmann@22917
  2506
    rule distrib_lattice_min_max)
haftmann@22917
  2507
    (unfold_locales, simp add: min_def)
haftmann@22917
  2508
haftmann@22917
  2509
lemma ACIf_max: "ACIf max"
haftmann@22917
  2510
  by (rule upper_semilattice.ACIf_sup,
haftmann@23018
  2511
    rule lattice.axioms,
haftmann@23018
  2512
    rule distrib_lattice.axioms,
haftmann@22917
  2513
    rule distrib_lattice_min_max)
haftmann@22917
  2514
haftmann@22917
  2515
lemma ACf_max: "ACf max"
haftmann@22917
  2516
  by (rule upper_semilattice.ACf_sup,
haftmann@23018
  2517
    rule lattice.axioms,
haftmann@23018
  2518
    rule distrib_lattice.axioms,
haftmann@22917
  2519
    rule distrib_lattice_min_max)
haftmann@22917
  2520
haftmann@25062
  2521
lemma ACIfSL_max: "ACIfSL (\<lambda>x y. y \<le> x) (\<lambda>x y. y < x) max"
haftmann@22917
  2522
  by (rule upper_semilattice.ACIfSL_sup,
haftmann@23018
  2523
    rule lattice.axioms,
haftmann@23018
  2524
    rule distrib_lattice.axioms,
haftmann@22917
  2525
    rule distrib_lattice_min_max)
haftmann@22917
  2526
haftmann@25062
  2527
lemma ACIfSLlin_max: "ACIfSLlin (\<lambda>x y. y \<le> x) (\<lambda>x y. y < x) max"
haftmann@22917
  2528
  by (rule ACIfSLlin.intro,
haftmann@22917
  2529
    rule upper_semilattice.ACIfSL_sup,
haftmann@23018
  2530
    rule lattice.axioms,
haftmann@23018
  2531
    rule distrib_lattice.axioms,
haftmann@22917
  2532
    rule distrib_lattice_min_max)
haftmann@22917
  2533
    (unfold_locales, simp add: max_def)
haftmann@22917
  2534
haftmann@22917
  2535
lemmas Min_singleton [simp] = fold1_singleton_def [OF Min_def]
haftmann@22917
  2536
lemmas Max_singleton [simp] = fold1_singleton_def [OF Max_def]
haftmann@22917
  2537
lemmas Min_insert [simp] = ACIf.fold1_insert_idem_def [OF ACIf_min Min_def]
haftmann@22917
  2538
lemmas Max_insert [simp] = ACIf.fold1_insert_idem_def [OF ACIf_max Max_def]
nipkow@15392
  2539
paulson@24427
  2540
lemma Min_in [simp]:
nipkow@15484
  2541
  shows "finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> Min A \<in> A"
haftmann@22917
  2542
  using ACf.fold1_in [OF ACf_min]
haftmann@22917
  2543
  by (fastsimp simp: Min_def min_def)
nipkow@15392
  2544
paulson@24427
  2545
lemma Max_in [simp]:
nipkow@15484
  2546
  shows "finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> Max A \<in> A"
haftmann@22917
  2547
  using ACf.fold1_in [OF ACf_max]
haftmann@22917
  2548
  by (fastsimp simp: Max_def max_def)
haftmann@22917
  2549
haftmann@25062
  2550
lemma Min_antimono: "\<lbrakk> M \<subseteq> N; M \<noteq> {}; finite N \<rbrakk> \<Longrightarrow> Min N \<le> Min M"
haftmann@22917
  2551
  by (simp add: Min_def ACIfSLlin.fold1_antimono [OF ACIfSLlin_min])
haftmann@22917
  2552
haftmann@25062
  2553
lemma Max_mono: "\<lbrakk> M \<subseteq> N; M \<noteq> {}; finite N \<rbrakk> \<Longrightarrow> Max M \<le> Max N"
haftmann@22917
  2554
  by (simp add: Max_def ACIfSLlin.fold1_antimono [OF ACIfSLlin_max])
haftmann@22917
  2555
haftmann@25062
  2556
lemma Min_le [simp]: "\<lbrakk> finite A; A \<noteq> {}; x \<in> A \<rbrakk> \<Longrightarrow> Min A \<le> x"
haftmann@22917
  2557
  by (simp add: Min_def ACIfSL.fold1_belowI [OF ACIfSL_min])
haftmann@22917
  2558
haftmann@25062
  2559
lemma Max_ge [simp]: "\<lbrakk> finite A; A \<noteq> {}; x \<in> A \<rbrakk> \<Longrightarrow> x \<le> Max A"
haftmann@22917
  2560
  by (simp add: Max_def ACIfSL.fold1_belowI [OF ACIfSL_max])
haftmann@22917
  2561
paulson@24286
  2562
lemma Min_ge_iff [simp,noatp]:
haftmann@25062
  2563
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> x \<le> Min A \<longleftrightarrow> (\<forall>a\<in>A. x \<le> a)"
haftmann@22917
  2564
  by (simp add: Min_def ACIfSL.below_fold1_iff [OF ACIfSL_min])
haftmann@22917
  2565
paulson@24286
  2566
lemma Max_le_iff [simp,noatp]:
haftmann@25062
  2567
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> Max A \<le> x \<longleftrightarrow> (\<forall>a\<in>A. a \<le> x)"
haftmann@22917
  2568
  by (simp add: Max_def ACIfSL.below_fold1_iff [OF ACIfSL_max])
haftmann@22917
  2569
paulson@24286
  2570
lemma Min_gr_iff [simp,noatp]:
haftmann@25062
  2571
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> x < Min A \<longleftrightarrow> (\<forall>a\<in>A. x < a)"
haftmann@22917
  2572
  by (simp add: Min_def ACIfSLlin.strict_below_fold1_iff [OF ACIfSLlin_min])
haftmann@22917
  2573
paulson@24286
  2574
lemma Max_less_iff [simp,noatp]:
haftmann@25062
  2575
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> Max A < x \<longleftrightarrow> (\<forall>a\<in>A. a < x)"
haftmann@22917
  2576
  by (simp add: Max_def ACIfSLlin.strict_below_fold1_iff [OF ACIfSLlin_max])
nipkow@18493
  2577
paulson@24286
  2578
lemma Min_le_iff [noatp]:
haftmann@25062
  2579
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> Min A \<le> x \<longleftrightarrow> (\<exists>a\<in>A. a \<le> x)"
haftmann@22917
  2580
  by (simp add: Min_def ACIfSLlin.fold1_below_iff [OF ACIfSLlin_min])
nipkow@15497
  2581
paulson@24286
  2582
lemma Max_ge_iff [noatp]:
haftmann@25062
  2583
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> x \<le> Max A \<longleftrightarrow> (\<exists>a\<in>A. x \<le> a)"
haftmann@22917
  2584
  by (simp add: Max_def ACIfSLlin.fold1_below_iff [OF ACIfSLlin_max])
haftmann@22917
  2585
paulson@24286
  2586
lemma Min_less_iff [noatp]:
haftmann@25062
  2587
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> Min A < x \<longleftrightarrow> (\<exists>a\<in>A. a < x)"
haftmann@22917
  2588
  by (simp add: Min_def ACIfSLlin.fold1_strict_below_iff [OF ACIfSLlin_min])
haftmann@22917
  2589
paulson@24286
  2590
lemma Max_gr_iff [noatp]:
haftmann@25062
  2591
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> x < Max A \<longleftrightarrow> (\<exists>a\<in>A. x < a)"
haftmann@22917
  2592
  by (simp add: Max_def ACIfSLlin.fold1_strict_below_iff [OF ACIfSLlin_max])
nipkow@18493
  2593
nipkow@18423
  2594
lemma Min_Un: "\<lbrakk>finite A; A \<noteq> {}; finite B; B \<noteq> {}\<rbrakk>
nipkow@18423
  2595
  \<Longrightarrow> Min (A \<union> B) = min (Min A) (Min B)"
haftmann@22917
  2596
  by (simp add: Min_def ACIf.fold1_Un2 [OF ACIf_min])
nipkow@18423
  2597
nipkow@18423
  2598
lemma Max_Un: "\<lbrakk>finite A; A \<noteq> {}; finite B; B \<noteq> {}\<rbrakk>
nipkow@18423
  2599
  \<Longrightarrow> Max (A \<union> B) = max (Max A) (Max B)"
haftmann@22917
  2600
  by (simp add: Max_def ACIf.fold1_Un2 [OF ACIf_max])
nipkow@18423
  2601
nipkow@18423
  2602
lemma hom_Min_commute:
haftmann@22917
  2603
 "(\<And>x y. h (min x y) = min (h x) (h y))
haftmann@22917
  2604
  \<Longrightarrow> finite N \<Longrightarrow> N \<noteq> {} \<Longrightarrow> h (Min N) = Min (h ` N)"
haftmann@22917
  2605
  by (simp add: Min_def ACIf.hom_fold1_commute [OF ACIf_min])
nipkow@18423
  2606
nipkow@18423
  2607
lemma hom_Max_commute:
haftmann@22917
  2608
 "(\<And>x y. h (max x y) = max (h x) (h y))
haftmann@22917
  2609
  \<Longrightarrow> finite N \<Longrightarrow> N \<noteq> {} \<Longrightarrow> h (Max N) = Max (h ` N)"
haftmann@22917
  2610
  by (simp add: Max_def ACIf.hom_fold1_commute [OF ACIf_max])
haftmann@22917
  2611
haftmann@22917
  2612
end
haftmann@22917
  2613
haftmann@24380
  2614
context ordered_ab_semigroup_add
haftmann@22917
  2615
begin
haftmann@22917
  2616
haftmann@22917
  2617
lemma add_Min_commute:
haftmann@22917
  2618
  fixes k
haftmann@25062
  2619
  assumes "finite N" and "N \<noteq> {}"
haftmann@25062
  2620
  shows "k + Min N = Min {k + m | m. m \<in> N}"
haftmann@25062
  2621
proof -
haftmann@25062
  2622
  have "\<And>x y. k + min x y = min (k + x) (k + y)"
haftmann@25062
  2623
    by (simp add: min_def not_le)
haftmann@25062
  2624
      (blast intro: antisym less_imp_le add_left_mono)
haftmann@25062
  2625
  with assms show ?thesis
haftmann@25062
  2626
    using hom_Min_commute [of "plus k" N]
haftmann@25062
  2627
    by simp (blast intro: arg_cong [where f = Min])
haftmann@25062
  2628
qed
haftmann@22917