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