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