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