src/HOL/Finite_Set.thy
author paulson
Mon Feb 14 10:24:58 2005 +0100 (2005-02-14)
changeset 15532 9712d41db5b8
parent 15526 748ebc63b807
child 15535 a0cf3a19ee36
permissions -rw-r--r--
simplified a proof
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
nipkow@15402
   512
text{* Instantiation of locales: *}
nipkow@15402
   513
nipkow@15402
   514
lemma ACf_add: "ACf (op + :: 'a::comm_monoid_add \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15402
   515
by(fastsimp intro: ACf.intro add_assoc add_commute)
nipkow@15402
   516
nipkow@15402
   517
lemma ACe_add: "ACe (op +) (0::'a::comm_monoid_add)"
nipkow@15402
   518
by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_add)
nipkow@15402
   519
nipkow@15402
   520
nipkow@15402
   521
lemma ACf_mult: "ACf (op * :: 'a::comm_monoid_mult \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15402
   522
by(fast intro: ACf.intro mult_assoc ab_semigroup_mult.mult_commute)
nipkow@15402
   523
nipkow@15402
   524
lemma ACe_mult: "ACe (op *) (1::'a::comm_monoid_mult)"
nipkow@15402
   525
by(fastsimp intro: ACe.intro ACe_axioms.intro ACf_mult)
nipkow@15402
   526
nipkow@15402
   527
nipkow@15392
   528
subsubsection{*From @{term foldSet} to @{term fold}*}
nipkow@15392
   529
paulson@15510
   530
lemma image_less_Suc: "h ` {i. i < Suc m} = insert (h m) (h ` {i. i < m})"
paulson@15510
   531
by (auto simp add: less_Suc_eq) 
paulson@15510
   532
paulson@15510
   533
lemma insert_image_inj_on_eq:
paulson@15510
   534
     "[|insert (h m) A = h ` {i. i < Suc m}; h m \<notin> A; 
paulson@15510
   535
        inj_on h {i. i < Suc m}|] 
paulson@15510
   536
      ==> A = h ` {i. i < m}"
paulson@15510
   537
apply (auto simp add: image_less_Suc inj_on_def)
paulson@15510
   538
apply (blast intro: less_trans) 
paulson@15510
   539
done
paulson@15510
   540
paulson@15510
   541
lemma insert_inj_onE:
paulson@15510
   542
  assumes aA: "insert a A = h`{i::nat. i<n}" and anot: "a \<notin> A" 
paulson@15510
   543
      and inj_on: "inj_on h {i::nat. i<n}"
paulson@15510
   544
  shows "\<exists>hm m. inj_on hm {i::nat. i<m} & A = hm ` {i. i<m} & m < n"
paulson@15510
   545
proof (cases n)
paulson@15510
   546
  case 0 thus ?thesis using aA by auto
paulson@15510
   547
next
paulson@15510
   548
  case (Suc m)
paulson@15510
   549
  have nSuc: "n = Suc m" . 
paulson@15510
   550
  have mlessn: "m<n" by (simp add: nSuc)
paulson@15532
   551
  from aA obtain k where hkeq: "h k = a" and klessn: "k<n" by (blast elim!: equalityE)
paulson@15520
   552
  let ?hm = "swap k m h"
paulson@15520
   553
  have inj_hm: "inj_on ?hm {i. i < n}" using klessn mlessn 
paulson@15520
   554
    by (simp add: inj_on_swap_iff inj_on)
paulson@15510
   555
  show ?thesis
paulson@15520
   556
  proof (intro exI conjI)
paulson@15520
   557
    show "inj_on ?hm {i. i < m}" using inj_hm
paulson@15510
   558
      by (auto simp add: nSuc less_Suc_eq intro: subset_inj_on)
paulson@15520
   559
    show "m<n" by (rule mlessn)
paulson@15520
   560
    show "A = ?hm ` {i. i < m}" 
paulson@15520
   561
    proof (rule insert_image_inj_on_eq)
paulson@15520
   562
      show "inj_on (swap k m h) {i. i < Suc m}" using inj_hm nSuc by simp
paulson@15520
   563
      show "?hm m \<notin> A" by (simp add: swap_def hkeq anot) 
paulson@15520
   564
      show "insert (?hm m) A = ?hm ` {i. i < Suc m}"
paulson@15520
   565
	using aA hkeq nSuc klessn
paulson@15520
   566
	by (auto simp add: swap_def image_less_Suc fun_upd_image 
paulson@15520
   567
			   less_Suc_eq inj_on_image_set_diff [OF inj_on])
nipkow@15479
   568
    qed
nipkow@15479
   569
  qed
nipkow@15479
   570
qed
nipkow@15479
   571
nipkow@15392
   572
lemma (in ACf) foldSet_determ_aux:
paulson@15510
   573
  "!!A x x' h. \<lbrakk> A = h`{i::nat. i<n}; inj_on h {i. i<n}; 
paulson@15510
   574
                (A,x) : foldSet f g z; (A,x') : foldSet f g z \<rbrakk>
nipkow@15392
   575
   \<Longrightarrow> x' = x"
paulson@15510
   576
proof (induct n rule: less_induct)
paulson@15510
   577
  case (less n)
paulson@15510
   578
    have IH: "!!m h A x x'. 
paulson@15510
   579
               \<lbrakk>m<n; A = h ` {i. i<m}; inj_on h {i. i<m}; 
paulson@15510
   580
                (A,x) \<in> foldSet f g z; (A, x') \<in> foldSet f g z\<rbrakk> \<Longrightarrow> x' = x" .
paulson@15510
   581
    have Afoldx: "(A,x) \<in> foldSet f g z" and Afoldx': "(A,x') \<in> foldSet f g z"
paulson@15510
   582
     and A: "A = h`{i. i<n}" and injh: "inj_on h {i. i<n}" .
paulson@15510
   583
    show ?case
paulson@15510
   584
    proof (rule foldSet.cases [OF Afoldx])
paulson@15510
   585
      assume "(A, x) = ({}, z)"
paulson@15510
   586
      with Afoldx' show "x' = x" by blast
nipkow@15392
   587
    next
paulson@15510
   588
      fix B b u
paulson@15510
   589
      assume "(A,x) = (insert b B, g b \<cdot> u)" and notinB: "b \<notin> B"
paulson@15510
   590
         and Bu: "(B,u) \<in> foldSet f g z"
paulson@15510
   591
      hence AbB: "A = insert b B" and x: "x = g b \<cdot> u" by auto
paulson@15510
   592
      show "x'=x" 
paulson@15510
   593
      proof (rule foldSet.cases [OF Afoldx'])
paulson@15510
   594
        assume "(A, x') = ({}, z)"
paulson@15510
   595
        with AbB show "x' = x" by blast
nipkow@15392
   596
      next
paulson@15510
   597
	fix C c v
paulson@15510
   598
	assume "(A,x') = (insert c C, g c \<cdot> v)" and notinC: "c \<notin> C"
paulson@15510
   599
	   and Cv: "(C,v) \<in> foldSet f g z"
paulson@15510
   600
	hence AcC: "A = insert c C" and x': "x' = g c \<cdot> v" by auto
paulson@15510
   601
	from A AbB have Beq: "insert b B = h`{i. i<n}" by simp
paulson@15510
   602
        from insert_inj_onE [OF Beq notinB injh]
paulson@15510
   603
        obtain hB mB where inj_onB: "inj_on hB {i. i < mB}" 
paulson@15510
   604
                     and Beq: "B = hB ` {i. i < mB}"
paulson@15510
   605
                     and lessB: "mB < n" by auto 
paulson@15510
   606
	from A AcC have Ceq: "insert c C = h`{i. i<n}" by simp
paulson@15510
   607
        from insert_inj_onE [OF Ceq notinC injh]
paulson@15510
   608
        obtain hC mC where inj_onC: "inj_on hC {i. i < mC}"
paulson@15510
   609
                       and Ceq: "C = hC ` {i. i < mC}"
paulson@15510
   610
                       and lessC: "mC < n" by auto 
paulson@15510
   611
	show "x'=x"
nipkow@15392
   612
	proof cases
paulson@15510
   613
          assume "b=c"
paulson@15510
   614
	  then moreover have "B = C" using AbB AcC notinB notinC by auto
paulson@15510
   615
	  ultimately show ?thesis  using Bu Cv x x' IH[OF lessC Ceq inj_onC]
paulson@15510
   616
            by auto
nipkow@15392
   617
	next
nipkow@15392
   618
	  assume diff: "b \<noteq> c"
nipkow@15392
   619
	  let ?D = "B - {c}"
nipkow@15392
   620
	  have B: "B = insert c ?D" and C: "C = insert b ?D"
paulson@15510
   621
	    using AbB AcC notinB notinC diff by(blast elim!:equalityE)+
nipkow@15402
   622
	  have "finite A" by(rule foldSet_imp_finite[OF Afoldx])
paulson@15510
   623
	  with AbB have "finite ?D" by simp
nipkow@15480
   624
	  then obtain d where Dfoldd: "(?D,d) \<in> foldSet f g z"
nipkow@15392
   625
	    using finite_imp_foldSet by rules
paulson@15506
   626
	  moreover have cinB: "c \<in> B" using B by auto
nipkow@15480
   627
	  ultimately have "(B,g c \<cdot> d) \<in> foldSet f g z"
nipkow@15392
   628
	    by(rule Diff1_foldSet)
paulson@15510
   629
	  hence "g c \<cdot> d = u" by (rule IH [OF lessB Beq inj_onB Bu]) 
paulson@15510
   630
          moreover have "g b \<cdot> d = v"
paulson@15510
   631
	  proof (rule IH[OF lessC Ceq inj_onC Cv])
paulson@15510
   632
	    show "(C, g b \<cdot> d) \<in> foldSet f g z" using C notinB Dfoldd
nipkow@15392
   633
	      by fastsimp
nipkow@15392
   634
	  qed
paulson@15510
   635
	  ultimately show ?thesis using x x' by (auto simp: AC)
nipkow@15392
   636
	qed
nipkow@15392
   637
      qed
nipkow@15392
   638
    qed
nipkow@15392
   639
  qed
nipkow@15392
   640
nipkow@15392
   641
nipkow@15392
   642
lemma (in ACf) foldSet_determ:
paulson@15510
   643
  "(A,x) : foldSet f g z ==> (A,y) : foldSet f g z ==> y = x"
paulson@15510
   644
apply (frule foldSet_imp_finite [THEN finite_imp_nat_seg_image_inj_on]) 
paulson@15510
   645
apply (blast intro: foldSet_determ_aux [rule_format])
nipkow@15392
   646
done
nipkow@15392
   647
nipkow@15480
   648
lemma (in ACf) fold_equality: "(A, y) : foldSet f g z ==> fold f g z A = y"
nipkow@15392
   649
  by (unfold fold_def) (blast intro: foldSet_determ)
nipkow@15392
   650
nipkow@15392
   651
text{* The base case for @{text fold}: *}
nipkow@15392
   652
nipkow@15480
   653
lemma fold_empty [simp]: "fold f g z {} = z"
nipkow@15392
   654
  by (unfold fold_def) blast
nipkow@15392
   655
nipkow@15392
   656
lemma (in ACf) fold_insert_aux: "x \<notin> A ==>
nipkow@15480
   657
    ((insert x A, v) : foldSet f g z) =
nipkow@15480
   658
    (EX y. (A, y) : foldSet f g z & v = f (g x) y)"
nipkow@15392
   659
  apply auto
nipkow@15392
   660
  apply (rule_tac A1 = A and f1 = f in finite_imp_foldSet [THEN exE])
nipkow@15392
   661
   apply (fastsimp dest: foldSet_imp_finite)
nipkow@15392
   662
  apply (blast intro: foldSet_determ)
nipkow@15392
   663
  done
nipkow@15392
   664
nipkow@15392
   665
text{* The recursion equation for @{text fold}: *}
nipkow@15392
   666
nipkow@15392
   667
lemma (in ACf) fold_insert[simp]:
nipkow@15480
   668
    "finite A ==> x \<notin> A ==> fold f g z (insert x A) = f (g x) (fold f g z A)"
nipkow@15392
   669
  apply (unfold fold_def)
nipkow@15392
   670
  apply (simp add: fold_insert_aux)
nipkow@15392
   671
  apply (rule the_equality)
nipkow@15392
   672
  apply (auto intro: finite_imp_foldSet
nipkow@15392
   673
    cong add: conj_cong simp add: fold_def [symmetric] fold_equality)
nipkow@15392
   674
  done
nipkow@15392
   675
nipkow@15392
   676
nipkow@15480
   677
text{* A simplified version for idempotent functions: *}
nipkow@15480
   678
paulson@15509
   679
lemma (in ACIf) fold_insert_idem:
nipkow@15480
   680
assumes finA: "finite A"
paulson@15508
   681
shows "fold f g z (insert a A) = g a \<cdot> fold f g z A"
nipkow@15480
   682
proof cases
nipkow@15480
   683
  assume "a \<in> A"
nipkow@15480
   684
  then obtain B where A: "A = insert a B" and disj: "a \<notin> B"
nipkow@15480
   685
    by(blast dest: mk_disjoint_insert)
nipkow@15480
   686
  show ?thesis
nipkow@15480
   687
  proof -
nipkow@15480
   688
    from finA A have finB: "finite B" by(blast intro: finite_subset)
nipkow@15480
   689
    have "fold f g z (insert a A) = fold f g z (insert a B)" using A by simp
nipkow@15480
   690
    also have "\<dots> = (g a) \<cdot> (fold f g z B)"
paulson@15506
   691
      using finB disj by simp
nipkow@15480
   692
    also have "\<dots> = g a \<cdot> fold f g z A"
nipkow@15480
   693
      using A finB disj by(simp add:idem assoc[symmetric])
nipkow@15480
   694
    finally show ?thesis .
nipkow@15480
   695
  qed
nipkow@15480
   696
next
nipkow@15480
   697
  assume "a \<notin> A"
nipkow@15480
   698
  with finA show ?thesis by simp
nipkow@15480
   699
qed
nipkow@15480
   700
nipkow@15484
   701
lemma (in ACIf) foldI_conv_id:
nipkow@15484
   702
  "finite A \<Longrightarrow> fold f g z A = fold f id z (g ` A)"
paulson@15509
   703
by(erule finite_induct)(simp_all add: fold_insert_idem del: fold_insert)
nipkow@15484
   704
nipkow@15392
   705
subsubsection{*Lemmas about @{text fold}*}
nipkow@15392
   706
nipkow@15392
   707
lemma (in ACf) fold_commute:
paulson@15487
   708
  "finite A ==> (!!z. f x (fold f g z A) = fold f g (f x z) A)"
nipkow@15392
   709
  apply (induct set: Finites, simp)
paulson@15487
   710
  apply (simp add: left_commute [of x])
nipkow@15392
   711
  done
nipkow@15392
   712
nipkow@15392
   713
lemma (in ACf) fold_nest_Un_Int:
nipkow@15392
   714
  "finite A ==> finite B
nipkow@15480
   715
    ==> fold f g (fold f g z B) A = fold f g (fold f g z (A Int B)) (A Un B)"
nipkow@15392
   716
  apply (induct set: Finites, simp)
nipkow@15392
   717
  apply (simp add: fold_commute Int_insert_left insert_absorb)
nipkow@15392
   718
  done
nipkow@15392
   719
nipkow@15392
   720
lemma (in ACf) fold_nest_Un_disjoint:
nipkow@15392
   721
  "finite A ==> finite B ==> A Int B = {}
nipkow@15480
   722
    ==> fold f g z (A Un B) = fold f g (fold f g z B) A"
nipkow@15392
   723
  by (simp add: fold_nest_Un_Int)
nipkow@15392
   724
nipkow@15392
   725
lemma (in ACf) fold_reindex:
paulson@15487
   726
assumes fin: "finite A"
paulson@15487
   727
shows "inj_on h A \<Longrightarrow> fold f g z (h ` A) = fold f (g \<circ> h) z A"
paulson@15506
   728
using fin apply induct
nipkow@15392
   729
 apply simp
nipkow@15392
   730
apply simp
nipkow@15392
   731
done
nipkow@15392
   732
nipkow@15392
   733
lemma (in ACe) fold_Un_Int:
nipkow@15392
   734
  "finite A ==> finite B ==>
nipkow@15392
   735
    fold f g e A \<cdot> fold f g e B =
nipkow@15392
   736
    fold f g e (A Un B) \<cdot> fold f g e (A Int B)"
nipkow@15392
   737
  apply (induct set: Finites, simp)
nipkow@15392
   738
  apply (simp add: AC insert_absorb Int_insert_left)
nipkow@15392
   739
  done
nipkow@15392
   740
nipkow@15392
   741
corollary (in ACe) fold_Un_disjoint:
nipkow@15392
   742
  "finite A ==> finite B ==> A Int B = {} ==>
nipkow@15392
   743
    fold f g e (A Un B) = fold f g e A \<cdot> fold f g e B"
nipkow@15392
   744
  by (simp add: fold_Un_Int)
nipkow@15392
   745
nipkow@15392
   746
lemma (in ACe) fold_UN_disjoint:
nipkow@15392
   747
  "\<lbrakk> finite I; ALL i:I. finite (A i);
nipkow@15392
   748
     ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {} \<rbrakk>
nipkow@15392
   749
   \<Longrightarrow> fold f g e (UNION I A) =
nipkow@15392
   750
       fold f (%i. fold f g e (A i)) e I"
nipkow@15392
   751
  apply (induct set: Finites, simp, atomize)
nipkow@15392
   752
  apply (subgoal_tac "ALL i:F. x \<noteq> i")
nipkow@15392
   753
   prefer 2 apply blast
nipkow@15392
   754
  apply (subgoal_tac "A x Int UNION F A = {}")
nipkow@15392
   755
   prefer 2 apply blast
nipkow@15392
   756
  apply (simp add: fold_Un_disjoint)
nipkow@15392
   757
  done
nipkow@15392
   758
paulson@15506
   759
text{*Fusion theorem, as described in
paulson@15506
   760
Graham Hutton's paper,
paulson@15506
   761
A Tutorial on the Universality and Expressiveness of Fold,
paulson@15506
   762
JFP 9:4 (355-372), 1999.*}
paulson@15506
   763
lemma (in ACf) fold_fusion:
paulson@15506
   764
      includes ACf g
paulson@15506
   765
      shows
paulson@15506
   766
	"finite A ==> 
paulson@15506
   767
	 (!!x y. h (g x y) = f x (h y)) ==>
paulson@15506
   768
         h (fold g j w A) = fold f j (h w) A"
paulson@15506
   769
  by (induct set: Finites, simp_all)
paulson@15506
   770
nipkow@15392
   771
lemma (in ACf) fold_cong:
nipkow@15480
   772
  "finite A \<Longrightarrow> (!!x. x:A ==> g x = h x) ==> fold f g z A = fold f h z A"
nipkow@15480
   773
  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
   774
   apply simp
nipkow@15392
   775
  apply (erule finite_induct, simp)
nipkow@15392
   776
  apply (simp add: subset_insert_iff, clarify)
nipkow@15392
   777
  apply (subgoal_tac "finite C")
nipkow@15392
   778
   prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
nipkow@15392
   779
  apply (subgoal_tac "C = insert x (C - {x})")
nipkow@15392
   780
   prefer 2 apply blast
nipkow@15392
   781
  apply (erule ssubst)
nipkow@15392
   782
  apply (drule spec)
nipkow@15392
   783
  apply (erule (1) notE impE)
nipkow@15392
   784
  apply (simp add: Ball_def del: insert_Diff_single)
nipkow@15392
   785
  done
nipkow@15392
   786
nipkow@15392
   787
lemma (in ACe) fold_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@15392
   788
  fold f (%x. fold f (g x) e (B x)) e A =
nipkow@15392
   789
  fold f (split g) e (SIGMA x:A. B x)"
nipkow@15392
   790
apply (subst Sigma_def)
paulson@15506
   791
apply (subst fold_UN_disjoint, assumption, simp)
nipkow@15392
   792
 apply blast
nipkow@15392
   793
apply (erule fold_cong)
paulson@15506
   794
apply (subst fold_UN_disjoint, simp, simp)
nipkow@15392
   795
 apply blast
paulson@15506
   796
apply simp
nipkow@15392
   797
done
nipkow@15392
   798
nipkow@15392
   799
lemma (in ACe) fold_distrib: "finite A \<Longrightarrow>
nipkow@15392
   800
   fold f (%x. f (g x) (h x)) e A = f (fold f g e A) (fold f h e A)"
paulson@15506
   801
apply (erule finite_induct, simp)
nipkow@15392
   802
apply (simp add:AC)
nipkow@15392
   803
done
nipkow@15392
   804
nipkow@15392
   805
nipkow@15402
   806
subsection {* Generalized summation over a set *}
nipkow@15402
   807
nipkow@15402
   808
constdefs
nipkow@15402
   809
  setsum :: "('a => 'b) => 'a set => 'b::comm_monoid_add"
nipkow@15402
   810
  "setsum f A == if finite A then fold (op +) f 0 A else 0"
nipkow@15402
   811
nipkow@15402
   812
text{* Now: lot's of fancy syntax. First, @{term "setsum (%x. e) A"} is
nipkow@15402
   813
written @{text"\<Sum>x\<in>A. e"}. *}
nipkow@15402
   814
nipkow@15402
   815
syntax
nipkow@15402
   816
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3SUM _:_. _)" [0, 51, 10] 10)
nipkow@15402
   817
syntax (xsymbols)
nipkow@15402
   818
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   819
syntax (HTML output)
nipkow@15402
   820
  "_setsum" :: "idt => 'a set => 'b => 'b::comm_monoid_add"    ("(3\<Sum>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
   821
nipkow@15402
   822
translations -- {* Beware of argument permutation! *}
nipkow@15402
   823
  "SUM i:A. b" == "setsum (%i. b) A"
nipkow@15402
   824
  "\<Sum>i\<in>A. b" == "setsum (%i. b) A"
nipkow@15402
   825
nipkow@15402
   826
text{* Instead of @{term"\<Sum>x\<in>{x. P}. e"} we introduce the shorter
nipkow@15402
   827
 @{text"\<Sum>x|P. e"}. *}
nipkow@15402
   828
nipkow@15402
   829
syntax
nipkow@15402
   830
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3SUM _ |/ _./ _)" [0,0,10] 10)
nipkow@15402
   831
syntax (xsymbols)
nipkow@15402
   832
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   833
syntax (HTML output)
nipkow@15402
   834
  "_qsetsum" :: "idt \<Rightarrow> bool \<Rightarrow> 'a \<Rightarrow> 'a" ("(3\<Sum>_ | (_)./ _)" [0,0,10] 10)
nipkow@15402
   835
nipkow@15402
   836
translations
nipkow@15402
   837
  "SUM x|P. t" => "setsum (%x. t) {x. P}"
nipkow@15402
   838
  "\<Sum>x|P. t" => "setsum (%x. t) {x. P}"
nipkow@15402
   839
nipkow@15402
   840
text{* Finally we abbreviate @{term"\<Sum>x\<in>A. x"} by @{text"\<Sum>A"}. *}
nipkow@15402
   841
nipkow@15402
   842
syntax
nipkow@15402
   843
  "_Setsum" :: "'a set => 'a::comm_monoid_mult"  ("\<Sum>_" [1000] 999)
nipkow@15402
   844
nipkow@15402
   845
parse_translation {*
nipkow@15402
   846
  let
nipkow@15402
   847
    fun Setsum_tr [A] = Syntax.const "setsum" $ Abs ("", dummyT, Bound 0) $ A
nipkow@15402
   848
  in [("_Setsum", Setsum_tr)] end;
nipkow@15402
   849
*}
nipkow@15402
   850
nipkow@15402
   851
print_translation {*
nipkow@15402
   852
let
nipkow@15402
   853
  fun setsum_tr' [Abs(_,_,Bound 0), A] = Syntax.const "_Setsum" $ A
nipkow@15402
   854
    | setsum_tr' [Abs(x,Tx,t), Const ("Collect",_) $ Abs(y,Ty,P)] = 
nipkow@15402
   855
       if x<>y then raise Match
nipkow@15402
   856
       else let val x' = Syntax.mark_bound x
nipkow@15402
   857
                val t' = subst_bound(x',t)
nipkow@15402
   858
                val P' = subst_bound(x',P)
nipkow@15402
   859
            in Syntax.const "_qsetsum" $ Syntax.mark_bound x $ P' $ t' end
nipkow@15402
   860
in
nipkow@15402
   861
[("setsum", setsum_tr')]
nipkow@15402
   862
end
nipkow@15402
   863
*}
nipkow@15402
   864
nipkow@15402
   865
lemma setsum_empty [simp]: "setsum f {} = 0"
nipkow@15402
   866
  by (simp add: setsum_def)
nipkow@15402
   867
nipkow@15402
   868
lemma setsum_insert [simp]:
nipkow@15402
   869
    "finite F ==> a \<notin> F ==> setsum f (insert a F) = f a + setsum f F"
nipkow@15402
   870
  by (simp add: setsum_def ACf.fold_insert [OF ACf_add])
nipkow@15402
   871
paulson@15409
   872
lemma setsum_infinite [simp]: "~ finite A ==> setsum f A = 0"
paulson@15409
   873
  by (simp add: setsum_def)
paulson@15409
   874
nipkow@15402
   875
lemma setsum_reindex:
nipkow@15402
   876
     "inj_on f B ==> setsum h (f ` B) = setsum (h \<circ> f) B"
nipkow@15402
   877
by(auto simp add: setsum_def ACf.fold_reindex[OF ACf_add] dest!:finite_imageD)
nipkow@15402
   878
nipkow@15402
   879
lemma setsum_reindex_id:
nipkow@15402
   880
     "inj_on f B ==> setsum f B = setsum id (f ` B)"
nipkow@15402
   881
by (auto simp add: setsum_reindex)
nipkow@15402
   882
nipkow@15402
   883
lemma setsum_cong:
nipkow@15402
   884
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setsum f A = setsum g B"
nipkow@15402
   885
by(fastsimp simp: setsum_def intro: ACf.fold_cong[OF ACf_add])
nipkow@15402
   886
nipkow@15402
   887
lemma setsum_reindex_cong:
nipkow@15402
   888
     "[|inj_on f A; B = f ` A; !!a. g a = h (f a)|] 
nipkow@15402
   889
      ==> setsum h B = setsum g A"
nipkow@15402
   890
  by (simp add: setsum_reindex cong: setsum_cong)
nipkow@15402
   891
nipkow@15402
   892
lemma setsum_0: "setsum (%i. 0) A = 0"
nipkow@15402
   893
apply (clarsimp simp: setsum_def)
nipkow@15402
   894
apply (erule finite_induct, auto simp:ACf.fold_insert [OF ACf_add])
nipkow@15402
   895
done
nipkow@15402
   896
nipkow@15402
   897
lemma setsum_0': "ALL a:F. f a = 0 ==> setsum f F = 0"
nipkow@15402
   898
  apply (subgoal_tac "setsum f F = setsum (%x. 0) F")
nipkow@15402
   899
  apply (erule ssubst, rule setsum_0)
nipkow@15402
   900
  apply (rule setsum_cong, auto)
nipkow@15402
   901
  done
nipkow@15402
   902
nipkow@15402
   903
lemma setsum_Un_Int: "finite A ==> finite B ==>
nipkow@15402
   904
  setsum g (A Un B) + setsum g (A Int B) = setsum g A + setsum g B"
nipkow@15402
   905
  -- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
nipkow@15402
   906
by(simp add: setsum_def ACe.fold_Un_Int[OF ACe_add,symmetric])
nipkow@15402
   907
nipkow@15402
   908
lemma setsum_Un_disjoint: "finite A ==> finite B
nipkow@15402
   909
  ==> A Int B = {} ==> setsum g (A Un B) = setsum g A + setsum g B"
nipkow@15402
   910
by (subst setsum_Un_Int [symmetric], auto)
nipkow@15402
   911
paulson@15409
   912
(*But we can't get rid of finite I. If infinite, although the rhs is 0, 
paulson@15409
   913
  the lhs need not be, since UNION I A could still be finite.*)
nipkow@15402
   914
lemma setsum_UN_disjoint:
nipkow@15402
   915
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
   916
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
   917
      setsum f (UNION I A) = (\<Sum>i\<in>I. setsum f (A i))"
nipkow@15402
   918
by(simp add: setsum_def ACe.fold_UN_disjoint[OF ACe_add] cong: setsum_cong)
nipkow@15402
   919
paulson@15409
   920
text{*No need to assume that @{term C} is finite.  If infinite, the rhs is
paulson@15409
   921
directly 0, and @{term "Union C"} is also infinite, hence the lhs is also 0.*}
nipkow@15402
   922
lemma setsum_Union_disjoint:
paulson@15409
   923
  "[| (ALL A:C. finite A);
paulson@15409
   924
      (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) |]
paulson@15409
   925
   ==> setsum f (Union C) = setsum (setsum f) C"
paulson@15409
   926
apply (cases "finite C") 
paulson@15409
   927
 prefer 2 apply (force dest: finite_UnionD simp add: setsum_def)
nipkow@15402
   928
  apply (frule setsum_UN_disjoint [of C id f])
paulson@15409
   929
 apply (unfold Union_def id_def, assumption+)
paulson@15409
   930
done
nipkow@15402
   931
paulson@15409
   932
(*But we can't get rid of finite A. If infinite, although the lhs is 0, 
paulson@15409
   933
  the rhs need not be, since SIGMA A B could still be finite.*)
nipkow@15402
   934
lemma setsum_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@15402
   935
    (\<Sum>x\<in>A. (\<Sum>y\<in>B x. f x y)) =
nipkow@15402
   936
    (\<Sum>z\<in>(SIGMA x:A. B x). f (fst z) (snd z))"
nipkow@15402
   937
by(simp add:setsum_def ACe.fold_Sigma[OF ACe_add] split_def cong:setsum_cong)
nipkow@15402
   938
paulson@15409
   939
text{*Here we can eliminate the finiteness assumptions, by cases.*}
paulson@15409
   940
lemma setsum_cartesian_product: 
paulson@15409
   941
   "(\<Sum>x\<in>A. (\<Sum>y\<in>B. f x y)) = (\<Sum>z\<in>A <*> B. f (fst z) (snd z))"
paulson@15409
   942
apply (cases "finite A") 
paulson@15409
   943
 apply (cases "finite B") 
paulson@15409
   944
  apply (simp add: setsum_Sigma)
paulson@15409
   945
 apply (cases "A={}", simp)
paulson@15409
   946
 apply (simp add: setsum_0) 
paulson@15409
   947
apply (auto simp add: setsum_def
paulson@15409
   948
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
   949
done
nipkow@15402
   950
nipkow@15402
   951
lemma setsum_addf: "setsum (%x. f x + g x) A = (setsum f A + setsum g A)"
nipkow@15402
   952
by(simp add:setsum_def ACe.fold_distrib[OF ACe_add])
nipkow@15402
   953
nipkow@15402
   954
nipkow@15402
   955
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
   956
nipkow@15402
   957
lemma setsum_SucD: "setsum f A = Suc n ==> EX a:A. 0 < f a"
nipkow@15402
   958
  apply (case_tac "finite A")
nipkow@15402
   959
   prefer 2 apply (simp add: setsum_def)
nipkow@15402
   960
  apply (erule rev_mp)
nipkow@15402
   961
  apply (erule finite_induct, auto)
nipkow@15402
   962
  done
nipkow@15402
   963
nipkow@15402
   964
lemma setsum_eq_0_iff [simp]:
nipkow@15402
   965
    "finite F ==> (setsum f F = 0) = (ALL a:F. f a = (0::nat))"
nipkow@15402
   966
  by (induct set: Finites) auto
nipkow@15402
   967
nipkow@15402
   968
lemma setsum_Un_nat: "finite A ==> finite B ==>
nipkow@15402
   969
    (setsum f (A Un B) :: nat) = setsum f A + setsum f B - setsum f (A Int B)"
nipkow@15402
   970
  -- {* For the natural numbers, we have subtraction. *}
nipkow@15402
   971
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
nipkow@15402
   972
nipkow@15402
   973
lemma setsum_Un: "finite A ==> finite B ==>
nipkow@15402
   974
    (setsum f (A Un B) :: 'a :: ab_group_add) =
nipkow@15402
   975
      setsum f A + setsum f B - setsum f (A Int B)"
nipkow@15402
   976
  by (subst setsum_Un_Int [symmetric], auto simp add: ring_eq_simps)
nipkow@15402
   977
nipkow@15402
   978
lemma setsum_diff1_nat: "(setsum f (A - {a}) :: nat) =
nipkow@15402
   979
    (if a:A then setsum f A - f a else setsum f A)"
nipkow@15402
   980
  apply (case_tac "finite A")
nipkow@15402
   981
   prefer 2 apply (simp add: setsum_def)
nipkow@15402
   982
  apply (erule finite_induct)
nipkow@15402
   983
   apply (auto simp add: insert_Diff_if)
nipkow@15402
   984
  apply (drule_tac a = a in mk_disjoint_insert, auto)
nipkow@15402
   985
  done
nipkow@15402
   986
nipkow@15402
   987
lemma setsum_diff1: "finite A \<Longrightarrow>
nipkow@15402
   988
  (setsum f (A - {a}) :: ('a::ab_group_add)) =
nipkow@15402
   989
  (if a:A then setsum f A - f a else setsum f A)"
nipkow@15402
   990
  by (erule finite_induct) (auto simp add: insert_Diff_if)
nipkow@15402
   991
nipkow@15402
   992
(* By Jeremy Siek: *)
nipkow@15402
   993
nipkow@15402
   994
lemma setsum_diff_nat: 
nipkow@15402
   995
  assumes finB: "finite B"
nipkow@15402
   996
  shows "B \<subseteq> A \<Longrightarrow> (setsum f (A - B) :: nat) = (setsum f A) - (setsum f B)"
nipkow@15402
   997
using finB
nipkow@15402
   998
proof (induct)
nipkow@15402
   999
  show "setsum f (A - {}) = (setsum f A) - (setsum f {})" by simp
nipkow@15402
  1000
next
nipkow@15402
  1001
  fix F x assume finF: "finite F" and xnotinF: "x \<notin> F"
nipkow@15402
  1002
    and xFinA: "insert x F \<subseteq> A"
nipkow@15402
  1003
    and IH: "F \<subseteq> A \<Longrightarrow> setsum f (A - F) = setsum f A - setsum f F"
nipkow@15402
  1004
  from xnotinF xFinA have xinAF: "x \<in> (A - F)" by simp
nipkow@15402
  1005
  from xinAF have A: "setsum f ((A - F) - {x}) = setsum f (A - F) - f x"
nipkow@15402
  1006
    by (simp add: setsum_diff1_nat)
nipkow@15402
  1007
  from xFinA have "F \<subseteq> A" by simp
nipkow@15402
  1008
  with IH have "setsum f (A - F) = setsum f A - setsum f F" by simp
nipkow@15402
  1009
  with A have B: "setsum f ((A - F) - {x}) = setsum f A - setsum f F - f x"
nipkow@15402
  1010
    by simp
nipkow@15402
  1011
  from xnotinF have "A - insert x F = (A - F) - {x}" by auto
nipkow@15402
  1012
  with B have C: "setsum f (A - insert x F) = setsum f A - setsum f F - f x"
nipkow@15402
  1013
    by simp
nipkow@15402
  1014
  from finF xnotinF have "setsum f (insert x F) = setsum f F + f x" by simp
nipkow@15402
  1015
  with C have "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)"
nipkow@15402
  1016
    by simp
nipkow@15402
  1017
  thus "setsum f (A - insert x F) = setsum f A - setsum f (insert x F)" by simp
nipkow@15402
  1018
qed
nipkow@15402
  1019
nipkow@15402
  1020
lemma setsum_diff:
nipkow@15402
  1021
  assumes le: "finite A" "B \<subseteq> A"
nipkow@15402
  1022
  shows "setsum f (A - B) = setsum f A - ((setsum f B)::('a::ab_group_add))"
nipkow@15402
  1023
proof -
nipkow@15402
  1024
  from le have finiteB: "finite B" using finite_subset by auto
nipkow@15402
  1025
  show ?thesis using finiteB le
nipkow@15402
  1026
    proof (induct)
nipkow@15402
  1027
      case empty
nipkow@15402
  1028
      thus ?case by auto
nipkow@15402
  1029
    next
nipkow@15402
  1030
      case (insert x F)
nipkow@15402
  1031
      thus ?case using le finiteB 
nipkow@15402
  1032
	by (simp add: Diff_insert[where a=x and B=F] setsum_diff1 insert_absorb)
nipkow@15402
  1033
    qed
nipkow@15402
  1034
  qed
nipkow@15402
  1035
nipkow@15402
  1036
lemma setsum_mono:
nipkow@15402
  1037
  assumes le: "\<And>i. i\<in>K \<Longrightarrow> f (i::'a) \<le> ((g i)::('b::{comm_monoid_add, pordered_ab_semigroup_add}))"
nipkow@15402
  1038
  shows "(\<Sum>i\<in>K. f i) \<le> (\<Sum>i\<in>K. g i)"
nipkow@15402
  1039
proof (cases "finite K")
nipkow@15402
  1040
  case True
nipkow@15402
  1041
  thus ?thesis using le
nipkow@15402
  1042
  proof (induct)
nipkow@15402
  1043
    case empty
nipkow@15402
  1044
    thus ?case by simp
nipkow@15402
  1045
  next
nipkow@15402
  1046
    case insert
nipkow@15402
  1047
    thus ?case using add_mono 
nipkow@15402
  1048
      by force
nipkow@15402
  1049
  qed
nipkow@15402
  1050
next
nipkow@15402
  1051
  case False
nipkow@15402
  1052
  thus ?thesis
nipkow@15402
  1053
    by (simp add: setsum_def)
nipkow@15402
  1054
qed
nipkow@15402
  1055
nipkow@15402
  1056
lemma setsum_mono2_nat:
nipkow@15402
  1057
  assumes fin: "finite B" and sub: "A \<subseteq> B"
nipkow@15402
  1058
shows "setsum f A \<le> (setsum f B :: nat)"
nipkow@15402
  1059
proof -
nipkow@15402
  1060
  have "setsum f A \<le> setsum f A + setsum f (B-A)" by arith
nipkow@15402
  1061
  also have "\<dots> = setsum f (A \<union> (B-A))" using fin finite_subset[OF sub fin]
nipkow@15402
  1062
    by (simp add:setsum_Un_disjoint del:Un_Diff_cancel)
nipkow@15402
  1063
  also have "A \<union> (B-A) = B" using sub by blast
nipkow@15402
  1064
  finally show ?thesis .
nipkow@15402
  1065
qed
nipkow@15402
  1066
nipkow@15402
  1067
lemma setsum_negf: "finite A ==> setsum (%x. - (f x)::'a::ab_group_add) A =
nipkow@15402
  1068
  - setsum f A"
nipkow@15402
  1069
  by (induct set: Finites, auto)
nipkow@15402
  1070
nipkow@15402
  1071
lemma setsum_subtractf: "finite A ==> setsum (%x. ((f x)::'a::ab_group_add) - g x) A =
nipkow@15402
  1072
  setsum f A - setsum g A"
nipkow@15402
  1073
  by (simp add: diff_minus setsum_addf setsum_negf)
nipkow@15402
  1074
nipkow@15402
  1075
lemma setsum_nonneg: "[| finite A;
nipkow@15402
  1076
    \<forall>x \<in> A. (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) \<le> f x |] ==>
nipkow@15402
  1077
    0 \<le> setsum f A";
nipkow@15402
  1078
  apply (induct set: Finites, auto)
nipkow@15402
  1079
  apply (subgoal_tac "0 + 0 \<le> f x + setsum f F", simp)
nipkow@15402
  1080
  apply (blast intro: add_mono)
nipkow@15402
  1081
  done
nipkow@15402
  1082
nipkow@15402
  1083
lemma setsum_nonpos: "[| finite A;
nipkow@15402
  1084
    \<forall>x \<in> A. f x \<le> (0::'a::{pordered_ab_semigroup_add, comm_monoid_add}) |] ==>
nipkow@15402
  1085
    setsum f A \<le> 0";
nipkow@15402
  1086
  apply (induct set: Finites, auto)
nipkow@15402
  1087
  apply (subgoal_tac "f x + setsum f F \<le> 0 + 0", simp)
nipkow@15402
  1088
  apply (blast intro: add_mono)
nipkow@15402
  1089
  done
nipkow@15402
  1090
nipkow@15402
  1091
lemma setsum_mult: 
nipkow@15402
  1092
  fixes f :: "'a => ('b::semiring_0_cancel)"
nipkow@15402
  1093
  shows "r * setsum f A = setsum (%n. r * f n) A"
nipkow@15402
  1094
proof (cases "finite A")
nipkow@15402
  1095
  case True
nipkow@15402
  1096
  thus ?thesis
nipkow@15402
  1097
  proof (induct)
nipkow@15402
  1098
    case empty thus ?case by simp
nipkow@15402
  1099
  next
nipkow@15402
  1100
    case (insert x A) thus ?case by (simp add: right_distrib)
nipkow@15402
  1101
  qed
nipkow@15402
  1102
next
nipkow@15402
  1103
  case False thus ?thesis by (simp add: setsum_def)
nipkow@15402
  1104
qed
nipkow@15402
  1105
nipkow@15402
  1106
lemma setsum_abs: 
nipkow@15402
  1107
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
nipkow@15402
  1108
  assumes fin: "finite A" 
nipkow@15402
  1109
  shows "abs (setsum f A) \<le> setsum (%i. abs(f i)) A"
nipkow@15402
  1110
using fin 
nipkow@15402
  1111
proof (induct) 
nipkow@15402
  1112
  case empty thus ?case by simp
nipkow@15402
  1113
next
nipkow@15402
  1114
  case (insert x A)
nipkow@15402
  1115
  thus ?case by (auto intro: abs_triangle_ineq order_trans)
nipkow@15402
  1116
qed
nipkow@15402
  1117
nipkow@15402
  1118
lemma setsum_abs_ge_zero: 
nipkow@15402
  1119
  fixes f :: "'a => ('b::lordered_ab_group_abs)"
nipkow@15402
  1120
  assumes fin: "finite A" 
nipkow@15402
  1121
  shows "0 \<le> setsum (%i. abs(f i)) A"
nipkow@15402
  1122
using fin 
nipkow@15402
  1123
proof (induct) 
nipkow@15402
  1124
  case empty thus ?case by simp
nipkow@15402
  1125
next
nipkow@15402
  1126
  case (insert x A) thus ?case by (auto intro: order_trans)
nipkow@15402
  1127
qed
nipkow@15402
  1128
nipkow@15402
  1129
nipkow@15402
  1130
subsection {* Generalized product over a set *}
nipkow@15402
  1131
nipkow@15402
  1132
constdefs
nipkow@15402
  1133
  setprod :: "('a => 'b) => 'a set => 'b::comm_monoid_mult"
nipkow@15402
  1134
  "setprod f A == if finite A then fold (op *) f 1 A else 1"
nipkow@15402
  1135
nipkow@15402
  1136
syntax
nipkow@15402
  1137
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_:_. _)" [0, 51, 10] 10)
nipkow@15402
  1138
nipkow@15402
  1139
syntax (xsymbols)
nipkow@15402
  1140
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
  1141
syntax (HTML output)
nipkow@15402
  1142
  "_setprod" :: "idt => 'a set => 'b => 'b::comm_monoid_mult"  ("(3\<Prod>_\<in>_. _)" [0, 51, 10] 10)
nipkow@15402
  1143
translations
nipkow@15402
  1144
  "\<Prod>i:A. b" == "setprod (%i. b) A"  -- {* Beware of argument permutation! *}
nipkow@15402
  1145
nipkow@15402
  1146
syntax
nipkow@15402
  1147
  "_Setprod" :: "'a set => 'a::comm_monoid_mult"  ("\<Prod>_" [1000] 999)
nipkow@15402
  1148
nipkow@15402
  1149
parse_translation {*
nipkow@15402
  1150
  let
nipkow@15402
  1151
    fun Setprod_tr [A] = Syntax.const "setprod" $ Abs ("", dummyT, Bound 0) $ A
nipkow@15402
  1152
  in [("_Setprod", Setprod_tr)] end;
nipkow@15402
  1153
*}
nipkow@15402
  1154
print_translation {*
nipkow@15402
  1155
let fun setprod_tr' [Abs(x,Tx,t), A] =
nipkow@15402
  1156
    if t = Bound 0 then Syntax.const "_Setprod" $ A else raise Match
nipkow@15402
  1157
in
nipkow@15402
  1158
[("setprod", setprod_tr')]
nipkow@15402
  1159
end
nipkow@15402
  1160
*}
nipkow@15402
  1161
nipkow@15402
  1162
nipkow@15402
  1163
lemma setprod_empty [simp]: "setprod f {} = 1"
nipkow@15402
  1164
  by (auto simp add: setprod_def)
nipkow@15402
  1165
nipkow@15402
  1166
lemma setprod_insert [simp]: "[| finite A; a \<notin> A |] ==>
nipkow@15402
  1167
    setprod f (insert a A) = f a * setprod f A"
nipkow@15402
  1168
by (simp add: setprod_def ACf.fold_insert [OF ACf_mult])
nipkow@15402
  1169
paulson@15409
  1170
lemma setprod_infinite [simp]: "~ finite A ==> setprod f A = 1"
paulson@15409
  1171
  by (simp add: setprod_def)
paulson@15409
  1172
nipkow@15402
  1173
lemma setprod_reindex:
nipkow@15402
  1174
     "inj_on f B ==> setprod h (f ` B) = setprod (h \<circ> f) B"
nipkow@15402
  1175
by(auto simp: setprod_def ACf.fold_reindex[OF ACf_mult] dest!:finite_imageD)
nipkow@15402
  1176
nipkow@15402
  1177
lemma setprod_reindex_id: "inj_on f B ==> setprod f B = setprod id (f ` B)"
nipkow@15402
  1178
by (auto simp add: setprod_reindex)
nipkow@15402
  1179
nipkow@15402
  1180
lemma setprod_cong:
nipkow@15402
  1181
  "A = B ==> (!!x. x:B ==> f x = g x) ==> setprod f A = setprod g B"
nipkow@15402
  1182
by(fastsimp simp: setprod_def intro: ACf.fold_cong[OF ACf_mult])
nipkow@15402
  1183
nipkow@15402
  1184
lemma setprod_reindex_cong: "inj_on f A ==>
nipkow@15402
  1185
    B = f ` A ==> g = h \<circ> f ==> setprod h B = setprod g A"
nipkow@15402
  1186
  by (frule setprod_reindex, simp)
nipkow@15402
  1187
nipkow@15402
  1188
nipkow@15402
  1189
lemma setprod_1: "setprod (%i. 1) A = 1"
nipkow@15402
  1190
  apply (case_tac "finite A")
nipkow@15402
  1191
  apply (erule finite_induct, auto simp add: mult_ac)
nipkow@15402
  1192
  done
nipkow@15402
  1193
nipkow@15402
  1194
lemma setprod_1': "ALL a:F. f a = 1 ==> setprod f F = 1"
nipkow@15402
  1195
  apply (subgoal_tac "setprod f F = setprod (%x. 1) F")
nipkow@15402
  1196
  apply (erule ssubst, rule setprod_1)
nipkow@15402
  1197
  apply (rule setprod_cong, auto)
nipkow@15402
  1198
  done
nipkow@15402
  1199
nipkow@15402
  1200
lemma setprod_Un_Int: "finite A ==> finite B
nipkow@15402
  1201
    ==> setprod g (A Un B) * setprod g (A Int B) = setprod g A * setprod g B"
nipkow@15402
  1202
by(simp add: setprod_def ACe.fold_Un_Int[OF ACe_mult,symmetric])
nipkow@15402
  1203
nipkow@15402
  1204
lemma setprod_Un_disjoint: "finite A ==> finite B
nipkow@15402
  1205
  ==> A Int B = {} ==> setprod g (A Un B) = setprod g A * setprod g B"
nipkow@15402
  1206
by (subst setprod_Un_Int [symmetric], auto)
nipkow@15402
  1207
nipkow@15402
  1208
lemma setprod_UN_disjoint:
nipkow@15402
  1209
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
  1210
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
  1211
      setprod f (UNION I A) = setprod (%i. setprod f (A i)) I"
nipkow@15402
  1212
by(simp add: setprod_def ACe.fold_UN_disjoint[OF ACe_mult] cong: setprod_cong)
nipkow@15402
  1213
nipkow@15402
  1214
lemma setprod_Union_disjoint:
paulson@15409
  1215
  "[| (ALL A:C. finite A);
paulson@15409
  1216
      (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) |] 
paulson@15409
  1217
   ==> setprod f (Union C) = setprod (setprod f) C"
paulson@15409
  1218
apply (cases "finite C") 
paulson@15409
  1219
 prefer 2 apply (force dest: finite_UnionD simp add: setprod_def)
nipkow@15402
  1220
  apply (frule setprod_UN_disjoint [of C id f])
paulson@15409
  1221
 apply (unfold Union_def id_def, assumption+)
paulson@15409
  1222
done
nipkow@15402
  1223
nipkow@15402
  1224
lemma setprod_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@15402
  1225
    (\<Prod>x:A. (\<Prod>y: B x. f x y)) =
nipkow@15402
  1226
    (\<Prod>z:(SIGMA x:A. B x). f (fst z) (snd z))"
nipkow@15402
  1227
by(simp add:setprod_def ACe.fold_Sigma[OF ACe_mult] split_def cong:setprod_cong)
nipkow@15402
  1228
paulson@15409
  1229
text{*Here we can eliminate the finiteness assumptions, by cases.*}
paulson@15409
  1230
lemma setprod_cartesian_product: 
paulson@15409
  1231
     "(\<Prod>x:A. (\<Prod>y: B. f x y)) = (\<Prod>z:(A <*> B). f (fst z) (snd z))"
paulson@15409
  1232
apply (cases "finite A") 
paulson@15409
  1233
 apply (cases "finite B") 
paulson@15409
  1234
  apply (simp add: setprod_Sigma)
paulson@15409
  1235
 apply (cases "A={}", simp)
paulson@15409
  1236
 apply (simp add: setprod_1) 
paulson@15409
  1237
apply (auto simp add: setprod_def
paulson@15409
  1238
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
  1239
done
nipkow@15402
  1240
nipkow@15402
  1241
lemma setprod_timesf:
paulson@15409
  1242
     "setprod (%x. f x * g x) A = (setprod f A * setprod g A)"
nipkow@15402
  1243
by(simp add:setprod_def ACe.fold_distrib[OF ACe_mult])
nipkow@15402
  1244
nipkow@15402
  1245
nipkow@15402
  1246
subsubsection {* Properties in more restricted classes of structures *}
nipkow@15402
  1247
nipkow@15402
  1248
lemma setprod_eq_1_iff [simp]:
nipkow@15402
  1249
    "finite F ==> (setprod f F = 1) = (ALL a:F. f a = (1::nat))"
nipkow@15402
  1250
  by (induct set: Finites) auto
nipkow@15402
  1251
nipkow@15402
  1252
lemma setprod_zero:
nipkow@15402
  1253
     "finite A ==> EX x: A. f x = (0::'a::comm_semiring_1_cancel) ==> setprod f A = 0"
nipkow@15402
  1254
  apply (induct set: Finites, force, clarsimp)
nipkow@15402
  1255
  apply (erule disjE, auto)
nipkow@15402
  1256
  done
nipkow@15402
  1257
nipkow@15402
  1258
lemma setprod_nonneg [rule_format]:
nipkow@15402
  1259
     "(ALL x: A. (0::'a::ordered_idom) \<le> f x) --> 0 \<le> setprod f A"
nipkow@15402
  1260
  apply (case_tac "finite A")
nipkow@15402
  1261
  apply (induct set: Finites, force, clarsimp)
nipkow@15402
  1262
  apply (subgoal_tac "0 * 0 \<le> f x * setprod f F", force)
nipkow@15402
  1263
  apply (rule mult_mono, assumption+)
nipkow@15402
  1264
  apply (auto simp add: setprod_def)
nipkow@15402
  1265
  done
nipkow@15402
  1266
nipkow@15402
  1267
lemma setprod_pos [rule_format]: "(ALL x: A. (0::'a::ordered_idom) < f x)
nipkow@15402
  1268
     --> 0 < setprod f A"
nipkow@15402
  1269
  apply (case_tac "finite A")
nipkow@15402
  1270
  apply (induct set: Finites, force, clarsimp)
nipkow@15402
  1271
  apply (subgoal_tac "0 * 0 < f x * setprod f F", force)
nipkow@15402
  1272
  apply (rule mult_strict_mono, assumption+)
nipkow@15402
  1273
  apply (auto simp add: setprod_def)
nipkow@15402
  1274
  done
nipkow@15402
  1275
nipkow@15402
  1276
lemma setprod_nonzero [rule_format]:
nipkow@15402
  1277
    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
nipkow@15402
  1278
      finite A ==> (ALL x: A. f x \<noteq> (0::'a)) --> setprod f A \<noteq> 0"
nipkow@15402
  1279
  apply (erule finite_induct, auto)
nipkow@15402
  1280
  done
nipkow@15402
  1281
nipkow@15402
  1282
lemma setprod_zero_eq:
nipkow@15402
  1283
    "(ALL x y. (x::'a::comm_semiring_1_cancel) * y = 0 --> x = 0 | y = 0) ==>
nipkow@15402
  1284
     finite A ==> (setprod f A = (0::'a)) = (EX x: A. f x = 0)"
nipkow@15402
  1285
  apply (insert setprod_zero [of A f] setprod_nonzero [of A f], blast)
nipkow@15402
  1286
  done
nipkow@15402
  1287
nipkow@15402
  1288
lemma setprod_nonzero_field:
nipkow@15402
  1289
    "finite A ==> (ALL x: A. f x \<noteq> (0::'a::field)) ==> setprod f A \<noteq> 0"
nipkow@15402
  1290
  apply (rule setprod_nonzero, auto)
nipkow@15402
  1291
  done
nipkow@15402
  1292
nipkow@15402
  1293
lemma setprod_zero_eq_field:
nipkow@15402
  1294
    "finite A ==> (setprod f A = (0::'a::field)) = (EX x: A. f x = 0)"
nipkow@15402
  1295
  apply (rule setprod_zero_eq, auto)
nipkow@15402
  1296
  done
nipkow@15402
  1297
nipkow@15402
  1298
lemma setprod_Un: "finite A ==> finite B ==> (ALL x: A Int B. f x \<noteq> 0) ==>
nipkow@15402
  1299
    (setprod f (A Un B) :: 'a ::{field})
nipkow@15402
  1300
      = setprod f A * setprod f B / setprod f (A Int B)"
nipkow@15402
  1301
  apply (subst setprod_Un_Int [symmetric], auto)
nipkow@15402
  1302
  apply (subgoal_tac "finite (A Int B)")
nipkow@15402
  1303
  apply (frule setprod_nonzero_field [of "A Int B" f], assumption)
nipkow@15402
  1304
  apply (subst times_divide_eq_right [THEN sym], auto simp add: divide_self)
nipkow@15402
  1305
  done
nipkow@15402
  1306
nipkow@15402
  1307
lemma setprod_diff1: "finite A ==> f a \<noteq> 0 ==>
nipkow@15402
  1308
    (setprod f (A - {a}) :: 'a :: {field}) =
nipkow@15402
  1309
      (if a:A then setprod f A / f a else setprod f A)"
nipkow@15402
  1310
  apply (erule finite_induct)
nipkow@15402
  1311
   apply (auto simp add: insert_Diff_if)
nipkow@15402
  1312
  apply (subgoal_tac "f a * setprod f F / f a = setprod f F * f a / f a")
nipkow@15402
  1313
  apply (erule ssubst)
nipkow@15402
  1314
  apply (subst times_divide_eq_right [THEN sym])
nipkow@15402
  1315
  apply (auto simp add: mult_ac times_divide_eq_right divide_self)
nipkow@15402
  1316
  done
nipkow@15402
  1317
nipkow@15402
  1318
lemma setprod_inversef: "finite A ==>
nipkow@15402
  1319
    ALL x: A. f x \<noteq> (0::'a::{field,division_by_zero}) ==>
nipkow@15402
  1320
      setprod (inverse \<circ> f) A = inverse (setprod f A)"
nipkow@15402
  1321
  apply (erule finite_induct)
nipkow@15402
  1322
  apply (simp, simp)
nipkow@15402
  1323
  done
nipkow@15402
  1324
nipkow@15402
  1325
lemma setprod_dividef:
nipkow@15402
  1326
     "[|finite A;
nipkow@15402
  1327
        \<forall>x \<in> A. g x \<noteq> (0::'a::{field,division_by_zero})|]
nipkow@15402
  1328
      ==> setprod (%x. f x / g x) A = setprod f A / setprod g A"
nipkow@15402
  1329
  apply (subgoal_tac
nipkow@15402
  1330
         "setprod (%x. f x / g x) A = setprod (%x. f x * (inverse \<circ> g) x) A")
nipkow@15402
  1331
  apply (erule ssubst)
nipkow@15402
  1332
  apply (subst divide_inverse)
nipkow@15402
  1333
  apply (subst setprod_timesf)
nipkow@15402
  1334
  apply (subst setprod_inversef, assumption+, rule refl)
nipkow@15402
  1335
  apply (rule setprod_cong, rule refl)
nipkow@15402
  1336
  apply (subst divide_inverse, auto)
nipkow@15402
  1337
  done
nipkow@15402
  1338
wenzelm@12396
  1339
subsection {* Finite cardinality *}
wenzelm@12396
  1340
nipkow@15402
  1341
text {* This definition, although traditional, is ugly to work with:
nipkow@15402
  1342
@{text "card A == LEAST n. EX f. A = {f i | i. i < n}"}.
nipkow@15402
  1343
But now that we have @{text setsum} things are easy:
wenzelm@12396
  1344
*}
wenzelm@12396
  1345
wenzelm@12396
  1346
constdefs
wenzelm@12396
  1347
  card :: "'a set => nat"
nipkow@15402
  1348
  "card A == setsum (%x. 1::nat) A"
wenzelm@12396
  1349
wenzelm@12396
  1350
lemma card_empty [simp]: "card {} = 0"
nipkow@15402
  1351
  by (simp add: card_def)
nipkow@15402
  1352
paulson@15409
  1353
lemma card_infinite [simp]: "~ finite A ==> card A = 0"
paulson@15409
  1354
  by (simp add: card_def)
paulson@15409
  1355
nipkow@15402
  1356
lemma card_eq_setsum: "card A = setsum (%x. 1) A"
nipkow@15402
  1357
by (simp add: card_def)
wenzelm@12396
  1358
wenzelm@12396
  1359
lemma card_insert_disjoint [simp]:
wenzelm@12396
  1360
  "finite A ==> x \<notin> A ==> card (insert x A) = Suc(card A)"
nipkow@15402
  1361
by(simp add: card_def ACf.fold_insert[OF ACf_add])
nipkow@15402
  1362
nipkow@15402
  1363
lemma card_insert_if:
nipkow@15402
  1364
    "finite A ==> card (insert x A) = (if x:A then card A else Suc(card(A)))"
nipkow@15402
  1365
  by (simp add: insert_absorb)
wenzelm@12396
  1366
wenzelm@12396
  1367
lemma card_0_eq [simp]: "finite A ==> (card A = 0) = (A = {})"
wenzelm@12396
  1368
  apply auto
paulson@15506
  1369
  apply (drule_tac a = x in mk_disjoint_insert, clarify, auto)
wenzelm@12396
  1370
  done
wenzelm@12396
  1371
paulson@15409
  1372
lemma card_eq_0_iff: "(card A = 0) = (A = {} | ~ finite A)"
paulson@15409
  1373
by auto
paulson@15409
  1374
wenzelm@12396
  1375
lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
nipkow@14302
  1376
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
nipkow@14302
  1377
apply(simp del:insert_Diff_single)
nipkow@14302
  1378
done
wenzelm@12396
  1379
wenzelm@12396
  1380
lemma card_Diff_singleton:
wenzelm@12396
  1381
    "finite A ==> x: A ==> card (A - {x}) = card A - 1"
wenzelm@12396
  1382
  by (simp add: card_Suc_Diff1 [symmetric])
wenzelm@12396
  1383
wenzelm@12396
  1384
lemma card_Diff_singleton_if:
wenzelm@12396
  1385
    "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)"
wenzelm@12396
  1386
  by (simp add: card_Diff_singleton)
wenzelm@12396
  1387
wenzelm@12396
  1388
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
wenzelm@12396
  1389
  by (simp add: card_insert_if card_Suc_Diff1)
wenzelm@12396
  1390
wenzelm@12396
  1391
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
wenzelm@12396
  1392
  by (simp add: card_insert_if)
wenzelm@12396
  1393
nipkow@15402
  1394
lemma card_mono: "\<lbrakk> finite B; A \<subseteq> B \<rbrakk> \<Longrightarrow> card A \<le> card B"
nipkow@15402
  1395
by (simp add: card_def setsum_mono2_nat)
nipkow@15402
  1396
wenzelm@12396
  1397
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
paulson@14208
  1398
  apply (induct set: Finites, simp, clarify)
wenzelm@12396
  1399
  apply (subgoal_tac "finite A & A - {x} <= F")
paulson@14208
  1400
   prefer 2 apply (blast intro: finite_subset, atomize)
wenzelm@12396
  1401
  apply (drule_tac x = "A - {x}" in spec)
wenzelm@12396
  1402
  apply (simp add: card_Diff_singleton_if split add: split_if_asm)
paulson@14208
  1403
  apply (case_tac "card A", auto)
wenzelm@12396
  1404
  done
wenzelm@12396
  1405
wenzelm@12396
  1406
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
wenzelm@12396
  1407
  apply (simp add: psubset_def linorder_not_le [symmetric])
wenzelm@12396
  1408
  apply (blast dest: card_seteq)
wenzelm@12396
  1409
  done
wenzelm@12396
  1410
wenzelm@12396
  1411
lemma card_Un_Int: "finite A ==> finite B
wenzelm@12396
  1412
    ==> card A + card B = card (A Un B) + card (A Int B)"
nipkow@15402
  1413
by(simp add:card_def setsum_Un_Int)
wenzelm@12396
  1414
wenzelm@12396
  1415
lemma card_Un_disjoint: "finite A ==> finite B
wenzelm@12396
  1416
    ==> A Int B = {} ==> card (A Un B) = card A + card B"
wenzelm@12396
  1417
  by (simp add: card_Un_Int)
wenzelm@12396
  1418
wenzelm@12396
  1419
lemma card_Diff_subset:
nipkow@15402
  1420
  "finite B ==> B <= A ==> card (A - B) = card A - card B"
nipkow@15402
  1421
by(simp add:card_def setsum_diff_nat)
wenzelm@12396
  1422
wenzelm@12396
  1423
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
wenzelm@12396
  1424
  apply (rule Suc_less_SucD)
wenzelm@12396
  1425
  apply (simp add: card_Suc_Diff1)
wenzelm@12396
  1426
  done
wenzelm@12396
  1427
wenzelm@12396
  1428
lemma card_Diff2_less:
wenzelm@12396
  1429
    "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
wenzelm@12396
  1430
  apply (case_tac "x = y")
wenzelm@12396
  1431
   apply (simp add: card_Diff1_less)
wenzelm@12396
  1432
  apply (rule less_trans)
wenzelm@12396
  1433
   prefer 2 apply (auto intro!: card_Diff1_less)
wenzelm@12396
  1434
  done
wenzelm@12396
  1435
wenzelm@12396
  1436
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
wenzelm@12396
  1437
  apply (case_tac "x : A")
wenzelm@12396
  1438
   apply (simp_all add: card_Diff1_less less_imp_le)
wenzelm@12396
  1439
  done
wenzelm@12396
  1440
wenzelm@12396
  1441
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
paulson@14208
  1442
by (erule psubsetI, blast)
wenzelm@12396
  1443
paulson@14889
  1444
lemma insert_partition:
nipkow@15402
  1445
  "\<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
  1446
  \<Longrightarrow> x \<inter> \<Union> F = {}"
paulson@14889
  1447
by auto
paulson@14889
  1448
paulson@14889
  1449
(* main cardinality theorem *)
paulson@14889
  1450
lemma card_partition [rule_format]:
paulson@14889
  1451
     "finite C ==>  
paulson@14889
  1452
        finite (\<Union> C) -->  
paulson@14889
  1453
        (\<forall>c\<in>C. card c = k) -->   
paulson@14889
  1454
        (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->  
paulson@14889
  1455
        k * card(C) = card (\<Union> C)"
paulson@14889
  1456
apply (erule finite_induct, simp)
paulson@14889
  1457
apply (simp add: card_insert_disjoint card_Un_disjoint insert_partition 
paulson@14889
  1458
       finite_subset [of _ "\<Union> (insert x F)"])
paulson@14889
  1459
done
paulson@14889
  1460
wenzelm@12396
  1461
paulson@15409
  1462
lemma setsum_constant_nat: "(\<Sum>x\<in>A. y) = (card A) * y"
nipkow@15402
  1463
  -- {* Generalized to any @{text comm_semiring_1_cancel} in
nipkow@15402
  1464
        @{text IntDef} as @{text setsum_constant}. *}
paulson@15409
  1465
apply (cases "finite A") 
paulson@15409
  1466
apply (erule finite_induct, auto)
paulson@15409
  1467
done
nipkow@15402
  1468
nipkow@15402
  1469
lemma setprod_constant: "finite A ==> (\<Prod>x: A. (y::'a::recpower)) = y^(card A)"
nipkow@15402
  1470
  apply (erule finite_induct)
nipkow@15402
  1471
  apply (auto simp add: power_Suc)
nipkow@15402
  1472
  done
nipkow@15402
  1473
nipkow@15402
  1474
nipkow@15402
  1475
subsubsection {* Cardinality of unions *}
nipkow@15402
  1476
nipkow@15402
  1477
lemma card_UN_disjoint:
nipkow@15402
  1478
    "finite I ==> (ALL i:I. finite (A i)) ==>
nipkow@15402
  1479
        (ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {}) ==>
nipkow@15402
  1480
      card (UNION I A) = (\<Sum>i\<in>I. card(A i))"
nipkow@15402
  1481
  apply (simp add: card_def)
nipkow@15402
  1482
  apply (subgoal_tac
nipkow@15402
  1483
           "setsum (%i. card (A i)) I = setsum (%i. (setsum (%x. 1) (A i))) I")
nipkow@15402
  1484
  apply (simp add: setsum_UN_disjoint)
nipkow@15402
  1485
  apply (simp add: setsum_constant_nat cong: setsum_cong)
nipkow@15402
  1486
  done
nipkow@15402
  1487
nipkow@15402
  1488
lemma card_Union_disjoint:
nipkow@15402
  1489
  "finite C ==> (ALL A:C. finite A) ==>
nipkow@15402
  1490
        (ALL A:C. ALL B:C. A \<noteq> B --> A Int B = {}) ==>
nipkow@15402
  1491
      card (Union C) = setsum card C"
nipkow@15402
  1492
  apply (frule card_UN_disjoint [of C id])
nipkow@15402
  1493
  apply (unfold Union_def id_def, assumption+)
nipkow@15402
  1494
  done
nipkow@15402
  1495
wenzelm@12396
  1496
subsubsection {* Cardinality of image *}
wenzelm@12396
  1497
paulson@15447
  1498
text{*The image of a finite set can be expressed using @{term fold}.*}
paulson@15447
  1499
lemma image_eq_fold: "finite A ==> f ` A = fold (op Un) (%x. {f x}) {} A"
paulson@15447
  1500
  apply (erule finite_induct, simp)
paulson@15447
  1501
  apply (subst ACf.fold_insert) 
paulson@15447
  1502
  apply (auto simp add: ACf_def) 
paulson@15447
  1503
  done
paulson@15447
  1504
wenzelm@12396
  1505
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
paulson@14208
  1506
  apply (induct set: Finites, simp)
wenzelm@12396
  1507
  apply (simp add: le_SucI finite_imageI card_insert_if)
wenzelm@12396
  1508
  done
wenzelm@12396
  1509
nipkow@15402
  1510
lemma card_image: "inj_on f A ==> card (f ` A) = card A"
nipkow@15402
  1511
by(simp add:card_def setsum_reindex o_def)
wenzelm@12396
  1512
wenzelm@12396
  1513
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
wenzelm@12396
  1514
  by (simp add: card_seteq card_image)
wenzelm@12396
  1515
nipkow@15111
  1516
lemma eq_card_imp_inj_on:
nipkow@15111
  1517
  "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
paulson@15506
  1518
apply (induct rule:finite_induct, simp)
nipkow@15111
  1519
apply(frule card_image_le[where f = f])
nipkow@15111
  1520
apply(simp add:card_insert_if split:if_splits)
nipkow@15111
  1521
done
nipkow@15111
  1522
nipkow@15111
  1523
lemma inj_on_iff_eq_card:
nipkow@15111
  1524
  "finite A ==> inj_on f A = (card(f ` A) = card A)"
nipkow@15111
  1525
by(blast intro: card_image eq_card_imp_inj_on)
nipkow@15111
  1526
wenzelm@12396
  1527
nipkow@15402
  1528
lemma card_inj_on_le:
nipkow@15402
  1529
    "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
nipkow@15402
  1530
apply (subgoal_tac "finite A") 
nipkow@15402
  1531
 apply (force intro: card_mono simp add: card_image [symmetric])
nipkow@15402
  1532
apply (blast intro: finite_imageD dest: finite_subset) 
nipkow@15402
  1533
done
nipkow@15402
  1534
nipkow@15402
  1535
lemma card_bij_eq:
nipkow@15402
  1536
    "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
nipkow@15402
  1537
       finite A; finite B |] ==> card A = card B"
nipkow@15402
  1538
  by (auto intro: le_anti_sym card_inj_on_le)
nipkow@15402
  1539
nipkow@15402
  1540
nipkow@15402
  1541
subsubsection {* Cardinality of products *}
nipkow@15402
  1542
nipkow@15402
  1543
(*
nipkow@15402
  1544
lemma SigmaI_insert: "y \<notin> A ==>
nipkow@15402
  1545
  (SIGMA x:(insert y A). B x) = (({y} <*> (B y)) \<union> (SIGMA x: A. B x))"
nipkow@15402
  1546
  by auto
nipkow@15402
  1547
*)
nipkow@15402
  1548
nipkow@15402
  1549
lemma card_SigmaI [simp]:
nipkow@15402
  1550
  "\<lbrakk> finite A; ALL a:A. finite (B a) \<rbrakk>
nipkow@15402
  1551
  \<Longrightarrow> card (SIGMA x: A. B x) = (\<Sum>a\<in>A. card (B a))"
nipkow@15402
  1552
by(simp add:card_def setsum_Sigma)
nipkow@15402
  1553
paulson@15409
  1554
lemma card_cartesian_product: "card (A <*> B) = card(A) * card(B)"
paulson@15409
  1555
apply (cases "finite A") 
paulson@15409
  1556
apply (cases "finite B") 
paulson@15409
  1557
  apply (simp add: setsum_constant_nat) 
paulson@15409
  1558
apply (auto simp add: card_eq_0_iff
paulson@15409
  1559
            dest: finite_cartesian_productD1 finite_cartesian_productD2) 
paulson@15409
  1560
done
nipkow@15402
  1561
nipkow@15402
  1562
lemma card_cartesian_product_singleton:  "card({x} <*> A) = card(A)"
paulson@15409
  1563
by (simp add: card_cartesian_product) 
paulson@15409
  1564
nipkow@15402
  1565
nipkow@15402
  1566
wenzelm@12396
  1567
subsubsection {* Cardinality of the Powerset *}
wenzelm@12396
  1568
wenzelm@12396
  1569
lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
wenzelm@12396
  1570
  apply (induct set: Finites)
wenzelm@12396
  1571
   apply (simp_all add: Pow_insert)
paulson@14208
  1572
  apply (subst card_Un_disjoint, blast)
paulson@14208
  1573
    apply (blast intro: finite_imageI, blast)
wenzelm@12396
  1574
  apply (subgoal_tac "inj_on (insert x) (Pow F)")
wenzelm@12396
  1575
   apply (simp add: card_image Pow_insert)
wenzelm@12396
  1576
  apply (unfold inj_on_def)
wenzelm@12396
  1577
  apply (blast elim!: equalityE)
wenzelm@12396
  1578
  done
wenzelm@12396
  1579
nipkow@15392
  1580
text {* Relates to equivalence classes.  Based on a theorem of
nipkow@15392
  1581
F. Kammüller's.  *}
wenzelm@12396
  1582
wenzelm@12396
  1583
lemma dvd_partition:
nipkow@15392
  1584
  "finite (Union C) ==>
wenzelm@12396
  1585
    ALL c : C. k dvd card c ==>
paulson@14430
  1586
    (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
wenzelm@12396
  1587
  k dvd card (Union C)"
nipkow@15392
  1588
apply(frule finite_UnionD)
nipkow@15392
  1589
apply(rotate_tac -1)
paulson@14208
  1590
  apply (induct set: Finites, simp_all, clarify)
wenzelm@12396
  1591
  apply (subst card_Un_disjoint)
wenzelm@12396
  1592
  apply (auto simp add: dvd_add disjoint_eq_subset_Compl)
wenzelm@12396
  1593
  done
wenzelm@12396
  1594
wenzelm@12396
  1595
nipkow@15392
  1596
subsubsection {* Theorems about @{text "choose"} *}
wenzelm@12396
  1597
wenzelm@12396
  1598
text {*
nipkow@15392
  1599
  \medskip Basic theorem about @{text "choose"}.  By Florian
nipkow@15392
  1600
  Kamm\"uller, tidied by LCP.
wenzelm@12396
  1601
*}
wenzelm@12396
  1602
nipkow@15392
  1603
lemma card_s_0_eq_empty:
nipkow@15392
  1604
    "finite A ==> card {B. B \<subseteq> A & card B = 0} = 1"
nipkow@15392
  1605
  apply (simp cong add: conj_cong add: finite_subset [THEN card_0_eq])
nipkow@15392
  1606
  apply (simp cong add: rev_conj_cong)
nipkow@15392
  1607
  done
wenzelm@12396
  1608
nipkow@15392
  1609
lemma choose_deconstruct: "finite M ==> x \<notin> M
nipkow@15392
  1610
  ==> {s. s <= insert x M & card(s) = Suc k}
nipkow@15392
  1611
       = {s. s <= M & card(s) = Suc k} Un
nipkow@15392
  1612
         {s. EX t. t <= M & card(t) = k & s = insert x t}"
nipkow@15392
  1613
  apply safe
nipkow@15392
  1614
   apply (auto intro: finite_subset [THEN card_insert_disjoint])
nipkow@15392
  1615
  apply (drule_tac x = "xa - {x}" in spec)
nipkow@15392
  1616
  apply (subgoal_tac "x \<notin> xa", auto)
nipkow@15392
  1617
  apply (erule rev_mp, subst card_Diff_singleton)
nipkow@15392
  1618
  apply (auto intro: finite_subset)
wenzelm@12396
  1619
  done
wenzelm@12396
  1620
nipkow@15392
  1621
text{*There are as many subsets of @{term A} having cardinality @{term k}
nipkow@15392
  1622
 as there are sets obtained from the former by inserting a fixed element
nipkow@15392
  1623
 @{term x} into each.*}
nipkow@15392
  1624
lemma constr_bij:
nipkow@15392
  1625
   "[|finite A; x \<notin> A|] ==>
nipkow@15392
  1626
    card {B. EX C. C <= A & card(C) = k & B = insert x C} =
nipkow@15392
  1627
    card {B. B <= A & card(B) = k}"
nipkow@15392
  1628
  apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
nipkow@15392
  1629
       apply (auto elim!: equalityE simp add: inj_on_def)
nipkow@15392
  1630
    apply (subst Diff_insert0, auto)
nipkow@15392
  1631
   txt {* finiteness of the two sets *}
nipkow@15392
  1632
   apply (rule_tac [2] B = "Pow (A)" in finite_subset)
nipkow@15392
  1633
   apply (rule_tac B = "Pow (insert x A)" in finite_subset)
nipkow@15392
  1634
   apply fast+
wenzelm@12396
  1635
  done
wenzelm@12396
  1636
nipkow@15392
  1637
text {*
nipkow@15392
  1638
  Main theorem: combinatorial statement about number of subsets of a set.
nipkow@15392
  1639
*}
wenzelm@12396
  1640
nipkow@15392
  1641
lemma n_sub_lemma:
nipkow@15392
  1642
  "!!A. finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
nipkow@15392
  1643
  apply (induct k)
nipkow@15392
  1644
   apply (simp add: card_s_0_eq_empty, atomize)
nipkow@15392
  1645
  apply (rotate_tac -1, erule finite_induct)
nipkow@15392
  1646
   apply (simp_all (no_asm_simp) cong add: conj_cong
nipkow@15392
  1647
     add: card_s_0_eq_empty choose_deconstruct)
nipkow@15392
  1648
  apply (subst card_Un_disjoint)
nipkow@15392
  1649
     prefer 4 apply (force simp add: constr_bij)
nipkow@15392
  1650
    prefer 3 apply force
nipkow@15392
  1651
   prefer 2 apply (blast intro: finite_Pow_iff [THEN iffD2]
nipkow@15392
  1652
     finite_subset [of _ "Pow (insert x F)", standard])
nipkow@15392
  1653
  apply (blast intro: finite_Pow_iff [THEN iffD2, THEN [2] finite_subset])
wenzelm@12396
  1654
  done
wenzelm@12396
  1655
nipkow@15392
  1656
theorem n_subsets:
nipkow@15392
  1657
    "finite A ==> card {B. B <= A & card B = k} = (card A choose k)"
nipkow@15392
  1658
  by (simp add: n_sub_lemma)
nipkow@15392
  1659
nipkow@15392
  1660
nipkow@15392
  1661
subsection{* A fold functional for non-empty sets *}
nipkow@15392
  1662
nipkow@15392
  1663
text{* Does not require start value. *}
wenzelm@12396
  1664
nipkow@15392
  1665
consts
paulson@15506
  1666
  fold1Set :: "('a => 'a => 'a) => ('a set \<times> 'a) set"
nipkow@15392
  1667
paulson@15506
  1668
inductive "fold1Set f"
nipkow@15392
  1669
intros
paulson@15506
  1670
  fold1Set_insertI [intro]:
paulson@15506
  1671
   "\<lbrakk> (A,x) \<in> foldSet f id a; a \<notin> A \<rbrakk> \<Longrightarrow> (insert a A, x) \<in> fold1Set f"
wenzelm@12396
  1672
nipkow@15392
  1673
constdefs
nipkow@15392
  1674
  fold1 :: "('a => 'a => 'a) => 'a set => 'a"
paulson@15506
  1675
  "fold1 f A == THE x. (A, x) : fold1Set f"
paulson@15506
  1676
paulson@15506
  1677
lemma fold1Set_nonempty:
paulson@15506
  1678
 "(A, x) : fold1Set f \<Longrightarrow> A \<noteq> {}"
paulson@15506
  1679
by(erule fold1Set.cases, simp_all) 
paulson@15506
  1680
nipkow@15392
  1681
paulson@15506
  1682
inductive_cases empty_fold1SetE [elim!]: "({}, x) : fold1Set f"
paulson@15506
  1683
paulson@15506
  1684
inductive_cases insert_fold1SetE [elim!]: "(insert a X, x) : fold1Set f"
paulson@15506
  1685
paulson@15506
  1686
paulson@15506
  1687
lemma fold1Set_sing [iff]: "(({a},b) : fold1Set f) = (a = b)"
paulson@15506
  1688
  by (blast intro: foldSet.intros elim: foldSet.cases)
nipkow@15392
  1689
paulson@15508
  1690
lemma fold1_singleton[simp]: "fold1 f {a} = a"
paulson@15508
  1691
  by (unfold fold1_def) blast
wenzelm@12396
  1692
paulson@15508
  1693
lemma finite_nonempty_imp_fold1Set:
paulson@15508
  1694
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> EX x. (A, x) : fold1Set f"
paulson@15508
  1695
apply (induct A rule: finite_induct)
paulson@15508
  1696
apply (auto dest: finite_imp_foldSet [of _ f id])  
paulson@15508
  1697
done
paulson@15506
  1698
paulson@15506
  1699
text{*First, some lemmas about @{term foldSet}.*}
nipkow@15392
  1700
paulson@15508
  1701
lemma (in ACf) foldSet_insert_swap:
paulson@15508
  1702
assumes fold: "(A,y) \<in> foldSet f id b"
paulson@15521
  1703
shows "b \<notin> A \<Longrightarrow> (insert b A, z \<cdot> y) \<in> foldSet f id z"
paulson@15508
  1704
using fold
paulson@15508
  1705
proof (induct rule: foldSet.induct)
paulson@15508
  1706
  case emptyI thus ?case by (force simp add: fold_insert_aux commute)
paulson@15508
  1707
next
paulson@15508
  1708
  case (insertI A x y)
paulson@15508
  1709
    have "(insert x (insert b A), x \<cdot> (z \<cdot> y)) \<in> foldSet f (\<lambda>u. u) z"
paulson@15521
  1710
      using insertI by force  --{*how does @{term id} get unfolded?*}
paulson@15508
  1711
    thus ?case by (simp add: insert_commute AC)
paulson@15508
  1712
qed
paulson@15508
  1713
paulson@15508
  1714
lemma (in ACf) foldSet_permute_diff:
paulson@15508
  1715
assumes fold: "(A,x) \<in> foldSet f id b"
paulson@15508
  1716
shows "!!a. \<lbrakk>a \<in> A; b \<notin> A\<rbrakk> \<Longrightarrow> (insert b (A-{a}), x) \<in> foldSet f id a"
paulson@15508
  1717
using fold
paulson@15508
  1718
proof (induct rule: foldSet.induct)
paulson@15508
  1719
  case emptyI thus ?case by simp
paulson@15508
  1720
next
paulson@15508
  1721
  case (insertI A x y)
paulson@15521
  1722
  have "a = x \<or> a \<in> A" using insertI by simp
paulson@15521
  1723
  thus ?case
paulson@15521
  1724
  proof
paulson@15521
  1725
    assume "a = x"
paulson@15521
  1726
    with insertI show ?thesis
paulson@15521
  1727
      by (simp add: id_def [symmetric], blast intro: foldSet_insert_swap) 
paulson@15521
  1728
  next
paulson@15521
  1729
    assume ainA: "a \<in> A"
paulson@15521
  1730
    hence "(insert x (insert b (A - {a})), x \<cdot> y) \<in> foldSet f id a"
paulson@15521
  1731
      using insertI by (force simp: id_def)
paulson@15521
  1732
    moreover
paulson@15521
  1733
    have "insert x (insert b (A - {a})) = insert b (insert x A - {a})"
paulson@15521
  1734
      using ainA insertI by blast
paulson@15521
  1735
    ultimately show ?thesis by (simp add: id_def)
paulson@15508
  1736
  qed
paulson@15508
  1737
qed
paulson@15508
  1738
paulson@15508
  1739
lemma (in ACf) fold1_eq_fold:
paulson@15508
  1740
     "[|finite A; a \<notin> A|] ==> fold1 f (insert a A) = fold f id a A"
paulson@15508
  1741
apply (simp add: fold1_def fold_def) 
paulson@15508
  1742
apply (rule the_equality)
paulson@15508
  1743
apply (best intro: foldSet_determ theI dest: finite_imp_foldSet [of _ f id]) 
paulson@15508
  1744
apply (rule sym, clarify)
paulson@15508
  1745
apply (case_tac "Aa=A")
paulson@15508
  1746
 apply (best intro: the_equality foldSet_determ)  
paulson@15508
  1747
apply (subgoal_tac "(A,x) \<in> foldSet f id a") 
paulson@15508
  1748
 apply (best intro: the_equality foldSet_determ)  
paulson@15508
  1749
apply (subgoal_tac "insert aa (Aa - {a}) = A") 
paulson@15508
  1750
 prefer 2 apply (blast elim: equalityE) 
paulson@15508
  1751
apply (auto dest: foldSet_permute_diff [where a=a]) 
paulson@15508
  1752
done
paulson@15508
  1753
paulson@15521
  1754
lemma nonempty_iff: "(A \<noteq> {}) = (\<exists>x B. A = insert x B & x \<notin> B)"
paulson@15521
  1755
apply safe
paulson@15521
  1756
apply simp 
paulson@15521
  1757
apply (drule_tac x=x in spec)
paulson@15521
  1758
apply (drule_tac x="A-{x}" in spec, auto) 
paulson@15508
  1759
done
paulson@15508
  1760
paulson@15521
  1761
lemma (in ACf) fold1_insert:
paulson@15521
  1762
  assumes nonempty: "A \<noteq> {}" and A: "finite A" "x \<notin> A"
paulson@15521
  1763
  shows "fold1 f (insert x A) = f x (fold1 f A)"
paulson@15521
  1764
proof -
paulson@15521
  1765
  from nonempty obtain a A' where "A = insert a A' & a ~: A'" 
paulson@15521
  1766
    by (auto simp add: nonempty_iff)
paulson@15521
  1767
  with A show ?thesis
paulson@15521
  1768
    by (simp add: insert_commute [of x] fold1_eq_fold eq_commute) 
paulson@15521
  1769
qed
paulson@15521
  1770
paulson@15509
  1771
lemma (in ACIf) fold1_insert_idem [simp]:
paulson@15521
  1772
  assumes nonempty: "A \<noteq> {}" and A: "finite A" 
paulson@15521
  1773
  shows "fold1 f (insert x A) = f x (fold1 f A)"
paulson@15521
  1774
proof -
paulson@15521
  1775
  from nonempty obtain a A' where A': "A = insert a A' & a ~: A'" 
paulson@15521
  1776
    by (auto simp add: nonempty_iff)
paulson@15521
  1777
  show ?thesis
paulson@15521
  1778
  proof cases
paulson@15521
  1779
    assume "a = x"
paulson@15521
  1780
    thus ?thesis 
paulson@15521
  1781
    proof cases
paulson@15521
  1782
      assume "A' = {}"
paulson@15521
  1783
      with prems show ?thesis by (simp add: idem) 
paulson@15521
  1784
    next
paulson@15521
  1785
      assume "A' \<noteq> {}"
paulson@15521
  1786
      with prems show ?thesis
paulson@15521
  1787
	by (simp add: fold1_insert assoc [symmetric] idem) 
paulson@15521
  1788
    qed
paulson@15521
  1789
  next
paulson@15521
  1790
    assume "a \<noteq> x"
paulson@15521
  1791
    with prems show ?thesis
paulson@15521
  1792
      by (simp add: insert_commute fold1_eq_fold fold_insert_idem)
paulson@15521
  1793
  qed
paulson@15521
  1794
qed
paulson@15506
  1795
paulson@15506
  1796
paulson@15508
  1797
text{* Now the recursion rules for definitions: *}
paulson@15508
  1798
paulson@15508
  1799
lemma fold1_singleton_def: "g \<equiv> fold1 f \<Longrightarrow> g {a} = a"
paulson@15508
  1800
by(simp add:fold1_singleton)
paulson@15508
  1801
paulson@15508
  1802
lemma (in ACf) fold1_insert_def:
paulson@15508
  1803
  "\<lbrakk> g \<equiv> fold1 f; finite A; x \<notin> A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g(insert x A) = x \<cdot> (g A)"
paulson@15508
  1804
by(simp add:fold1_insert)
paulson@15508
  1805
paulson@15509
  1806
lemma (in ACIf) fold1_insert_idem_def:
paulson@15508
  1807
  "\<lbrakk> g \<equiv> fold1 f; finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g(insert x A) = x \<cdot> (g A)"
paulson@15509
  1808
by(simp add:fold1_insert_idem)
paulson@15508
  1809
paulson@15508
  1810
subsubsection{* Determinacy for @{term fold1Set} *}
paulson@15508
  1811
paulson@15508
  1812
text{*Not actually used!!*}
wenzelm@12396
  1813
paulson@15506
  1814
lemma (in ACf) foldSet_permute:
paulson@15506
  1815
  "[|(insert a A, x) \<in> foldSet f id b; a \<notin> A; b \<notin> A|]
paulson@15506
  1816
   ==> (insert b A, x) \<in> foldSet f id a"
paulson@15506
  1817
apply (case_tac "a=b") 
paulson@15506
  1818
apply (auto dest: foldSet_permute_diff) 
paulson@15506
  1819
done
nipkow@15376
  1820
paulson@15506
  1821
lemma (in ACf) fold1Set_determ:
paulson@15506
  1822
  "(A, x) \<in> fold1Set f ==> (A, y) \<in> fold1Set f ==> y = x"
paulson@15506
  1823
proof (clarify elim!: fold1Set.cases)
paulson@15506
  1824
  fix A x B y a b
paulson@15506
  1825
  assume Ax: "(A, x) \<in> foldSet f id a"
paulson@15506
  1826
  assume By: "(B, y) \<in> foldSet f id b"
paulson@15506
  1827
  assume anotA:  "a \<notin> A"
paulson@15506
  1828
  assume bnotB:  "b \<notin> B"
paulson@15506
  1829
  assume eq: "insert a A = insert b B"
paulson@15506
  1830
  show "y=x"
paulson@15506
  1831
  proof cases
paulson@15506
  1832
    assume same: "a=b"
paulson@15506
  1833
    hence "A=B" using anotA bnotB eq by (blast elim!: equalityE)
paulson@15506
  1834
    thus ?thesis using Ax By same by (blast intro: foldSet_determ)
nipkow@15392
  1835
  next
paulson@15506
  1836
    assume diff: "a\<noteq>b"
paulson@15506
  1837
    let ?D = "B - {a}"
paulson@15506
  1838
    have B: "B = insert a ?D" and A: "A = insert b ?D"
paulson@15506
  1839
     and aB: "a \<in> B" and bA: "b \<in> A"
paulson@15506
  1840
      using eq anotA bnotB diff by (blast elim!:equalityE)+
paulson@15506
  1841
    with aB bnotB By
paulson@15506
  1842
    have "(insert b ?D, y) \<in> foldSet f id a" 
paulson@15506
  1843
      by (auto intro: foldSet_permute simp add: insert_absorb)
paulson@15506
  1844
    moreover
paulson@15506
  1845
    have "(insert b ?D, x) \<in> foldSet f id a"
paulson@15506
  1846
      by (simp add: A [symmetric] Ax) 
paulson@15506
  1847
    ultimately show ?thesis by (blast intro: foldSet_determ) 
nipkow@15392
  1848
  qed
wenzelm@12396
  1849
qed
wenzelm@12396
  1850
paulson@15506
  1851
lemma (in ACf) fold1Set_equality: "(A, y) : fold1Set f ==> fold1 f A = y"
paulson@15506
  1852
  by (unfold fold1_def) (blast intro: fold1Set_determ)
paulson@15506
  1853
paulson@15506
  1854
declare
paulson@15506
  1855
  empty_foldSetE [rule del]   foldSet.intros [rule del]
paulson@15506
  1856
  empty_fold1SetE [rule del]  insert_fold1SetE [rule del]
paulson@15506
  1857
  -- {* No more proves involve these relations. *}
nipkow@15376
  1858
nipkow@15497
  1859
subsubsection{* Semi-Lattices *}
nipkow@15497
  1860
nipkow@15497
  1861
locale ACIfSL = ACIf +
nipkow@15500
  1862
  fixes below :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infixl "\<sqsubseteq>" 50)
nipkow@15500
  1863
  assumes below_def: "(x \<sqsubseteq> y) = (x\<cdot>y = x)"
nipkow@15497
  1864
nipkow@15497
  1865
locale ACIfSLlin = ACIfSL +
nipkow@15497
  1866
  assumes lin: "x\<cdot>y \<in> {x,y}"
nipkow@15497
  1867
nipkow@15500
  1868
lemma (in ACIfSL) below_refl[simp]: "x \<sqsubseteq> x"
nipkow@15497
  1869
by(simp add: below_def idem)
nipkow@15497
  1870
nipkow@15500
  1871
lemma (in ACIfSL) below_f_conv[simp]: "x \<sqsubseteq> y \<cdot> z = (x \<sqsubseteq> y \<and> x \<sqsubseteq> z)"
nipkow@15497
  1872
proof
nipkow@15500
  1873
  assume "x \<sqsubseteq> y \<cdot> z"
nipkow@15497
  1874
  hence xyzx: "x \<cdot> (y \<cdot> z) = x"  by(simp add: below_def)
nipkow@15497
  1875
  have "x \<cdot> y = x"
nipkow@15497
  1876
  proof -
nipkow@15497
  1877
    have "x \<cdot> y = (x \<cdot> (y \<cdot> z)) \<cdot> y" by(rule subst[OF xyzx], rule refl)
nipkow@15497
  1878
    also have "\<dots> = x \<cdot> (y \<cdot> z)" by(simp add:ACI)
nipkow@15497
  1879
    also have "\<dots> = x" by(rule xyzx)
nipkow@15497
  1880
    finally show ?thesis .
nipkow@15497
  1881
  qed
nipkow@15497
  1882
  moreover have "x \<cdot> z = x"
nipkow@15497
  1883
  proof -
nipkow@15497
  1884
    have "x \<cdot> z = (x \<cdot> (y \<cdot> z)) \<cdot> z" by(rule subst[OF xyzx], rule refl)
nipkow@15497
  1885
    also have "\<dots> = x \<cdot> (y \<cdot> z)" by(simp add:ACI)
nipkow@15497
  1886
    also have "\<dots> = x" by(rule xyzx)
nipkow@15497
  1887
    finally show ?thesis .
nipkow@15497
  1888
  qed
nipkow@15500
  1889
  ultimately show "x \<sqsubseteq> y \<and> x \<sqsubseteq> z" by(simp add: below_def)
nipkow@15497
  1890
next
nipkow@15500
  1891
  assume a: "x \<sqsubseteq> y \<and> x \<sqsubseteq> z"
nipkow@15497
  1892
  hence y: "x \<cdot> y = x" and z: "x \<cdot> z = x" by(simp_all add: below_def)
nipkow@15497
  1893
  have "x \<cdot> (y \<cdot> z) = (x \<cdot> y) \<cdot> z" by(simp add:assoc)
nipkow@15497
  1894
  also have "x \<cdot> y = x" using a by(simp_all add: below_def)
nipkow@15497
  1895
  also have "x \<cdot> z = x" using a by(simp_all add: below_def)
nipkow@15500
  1896
  finally show "x \<sqsubseteq> y \<cdot> z" by(simp_all add: below_def)
nipkow@15497
  1897
qed
nipkow@15497
  1898
nipkow@15497
  1899
lemma (in ACIfSLlin) above_f_conv:
nipkow@15500
  1900
 "x \<cdot> y \<sqsubseteq> z = (x \<sqsubseteq> z \<or> y \<sqsubseteq> z)"
nipkow@15497
  1901
proof
nipkow@15500
  1902
  assume a: "x \<cdot> y \<sqsubseteq> z"
nipkow@15497
  1903
  have "x \<cdot> y = x \<or> x \<cdot> y = y" using lin[of x y] by simp
nipkow@15500
  1904
  thus "x \<sqsubseteq> z \<or> y \<sqsubseteq> z"
nipkow@15497
  1905
  proof
nipkow@15500
  1906
    assume "x \<cdot> y = x" hence "x \<sqsubseteq> z" by(rule subst)(rule a) thus ?thesis ..
nipkow@15497
  1907
  next
nipkow@15500
  1908
    assume "x \<cdot> y = y" hence "y \<sqsubseteq> z" by(rule subst)(rule a) thus ?thesis ..
nipkow@15497
  1909
  qed
nipkow@15497
  1910
next
nipkow@15500
  1911
  assume "x \<sqsubseteq> z \<or> y \<sqsubseteq> z"
nipkow@15500
  1912
  thus "x \<cdot> y \<sqsubseteq> z"
nipkow@15497
  1913
  proof
nipkow@15500
  1914
    assume a: "x \<sqsubseteq> z"
nipkow@15497
  1915
    have "(x \<cdot> y) \<cdot> z = (x \<cdot> z) \<cdot> y" by(simp add:ACI)
nipkow@15497
  1916
    also have "x \<cdot> z = x" using a by(simp add:below_def)
nipkow@15500
  1917
    finally show "x \<cdot> y \<sqsubseteq> z" by(simp add:below_def)
nipkow@15497
  1918
  next
nipkow@15500
  1919
    assume a: "y \<sqsubseteq> z"
nipkow@15497
  1920
    have "(x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)" by(simp add:ACI)
nipkow@15497
  1921
    also have "y \<cdot> z = y" using a by(simp add:below_def)
nipkow@15500
  1922
    finally show "x \<cdot> y \<sqsubseteq> z" by(simp add:below_def)
nipkow@15497
  1923
  qed
nipkow@15497
  1924
qed
nipkow@15497
  1925
nipkow@15497
  1926
nipkow@15502
  1927
subsubsection{* Lemmas about @{text fold1} *}
nipkow@15484
  1928
nipkow@15484
  1929
lemma (in ACf) fold1_Un:
nipkow@15484
  1930
assumes A: "finite A" "A \<noteq> {}"
nipkow@15484
  1931
shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow> A Int B = {} \<Longrightarrow>
nipkow@15484
  1932
       fold1 f (A Un B) = f (fold1 f A) (fold1 f B)"
nipkow@15484
  1933
using A
nipkow@15484
  1934
proof(induct rule:finite_ne_induct)
nipkow@15484
  1935
  case singleton thus ?case by(simp add:fold1_insert)
nipkow@15484
  1936
next
nipkow@15484
  1937
  case insert thus ?case by (simp add:fold1_insert assoc)
nipkow@15484
  1938
qed
nipkow@15484
  1939
nipkow@15484
  1940
lemma (in ACIf) fold1_Un2:
nipkow@15484
  1941
assumes A: "finite A" "A \<noteq> {}"
nipkow@15484
  1942
shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow>
nipkow@15484
  1943
       fold1 f (A Un B) = f (fold1 f A) (fold1 f B)"
nipkow@15484
  1944
using A
nipkow@15484
  1945
proof(induct rule:finite_ne_induct)
paulson@15509
  1946
  case singleton thus ?case by(simp add:fold1_insert_idem)
nipkow@15484
  1947
next
paulson@15509
  1948
  case insert thus ?case by (simp add:fold1_insert_idem assoc)
nipkow@15484
  1949
qed
nipkow@15484
  1950
nipkow@15484
  1951
lemma (in ACf) fold1_in:
nipkow@15484
  1952
  assumes A: "finite (A)" "A \<noteq> {}" and elem: "\<And>x y. x\<cdot>y \<in> {x,y}"
nipkow@15484
  1953
  shows "fold1 f A \<in> A"
nipkow@15484
  1954
using A
nipkow@15484
  1955
proof (induct rule:finite_ne_induct)
paulson@15506
  1956
  case singleton thus ?case by simp
nipkow@15484
  1957
next
nipkow@15484
  1958
  case insert thus ?case using elem by (force simp add:fold1_insert)
nipkow@15484
  1959
qed
nipkow@15484
  1960
nipkow@15497
  1961
lemma (in ACIfSL) below_fold1_iff:
nipkow@15497
  1962
assumes A: "finite A" "A \<noteq> {}"
nipkow@15500
  1963
shows "x \<sqsubseteq> fold1 f A = (\<forall>a\<in>A. x \<sqsubseteq> a)"
nipkow@15497
  1964
using A
nipkow@15497
  1965
by(induct rule:finite_ne_induct) simp_all
nipkow@15497
  1966
nipkow@15497
  1967
lemma (in ACIfSL) fold1_belowI:
nipkow@15497
  1968
assumes A: "finite A" "A \<noteq> {}"
nipkow@15500
  1969
shows "a \<in> A \<Longrightarrow> fold1 f A \<sqsubseteq> a"
nipkow@15484
  1970
using A
nipkow@15484
  1971
proof (induct rule:finite_ne_induct)
nipkow@15497
  1972
  case singleton thus ?case by simp
nipkow@15484
  1973
next
nipkow@15497
  1974
  case (insert x F)
berghofe@15517
  1975
  from insert(5) have "a = x \<or> a \<in> F" by simp
nipkow@15497
  1976
  thus ?case
nipkow@15497
  1977
  proof
nipkow@15497
  1978
    assume "a = x" thus ?thesis using insert by(simp add:below_def ACI)
nipkow@15497
  1979
  next
nipkow@15497
  1980
    assume "a \<in> F"
paulson@15508
  1981
    hence bel: "fold1 f F \<sqsubseteq> a" by(rule insert)
paulson@15508
  1982
    have "fold1 f (insert x F) \<cdot> a = x \<cdot> (fold1 f F \<cdot> a)"
nipkow@15497
  1983
      using insert by(simp add:below_def ACI)
paulson@15508
  1984
    also have "fold1 f F \<cdot> a = fold1 f F"
nipkow@15497
  1985
      using bel  by(simp add:below_def ACI)
paulson@15508
  1986
    also have "x \<cdot> \<dots> = fold1 f (insert x F)"
nipkow@15497
  1987
      using insert by(simp add:below_def ACI)
nipkow@15497
  1988
    finally show ?thesis  by(simp add:below_def)
nipkow@15497
  1989
  qed
nipkow@15484
  1990
qed
nipkow@15484
  1991
nipkow@15497
  1992
lemma (in ACIfSLlin) fold1_below_iff:
nipkow@15497
  1993
assumes A: "finite A" "A \<noteq> {}"
nipkow@15500
  1994
shows "fold1 f A \<sqsubseteq> x = (\<exists>a\<in>A. a \<sqsubseteq> x)"
nipkow@15484
  1995
using A
nipkow@15497
  1996
by(induct rule:finite_ne_induct)(simp_all add:above_f_conv)
nipkow@15484
  1997
nipkow@15512
  1998
nipkow@15500
  1999
subsubsection{* Lattices *}
nipkow@15500
  2000
nipkow@15512
  2001
locale Lattice = lattice +
nipkow@15512
  2002
  fixes Inf :: "'a set \<Rightarrow> 'a" ("\<Sqinter>_" [900] 900)
nipkow@15500
  2003
  and Sup :: "'a set \<Rightarrow> 'a" ("\<Squnion>_" [900] 900)
nipkow@15500
  2004
  defines "Inf == fold1 inf"  and "Sup == fold1 sup"
nipkow@15500
  2005
nipkow@15512
  2006
locale Distrib_Lattice = distrib_lattice + Lattice
nipkow@15504
  2007
nipkow@15500
  2008
text{* Lattices are semilattices *}
nipkow@15500
  2009
nipkow@15500
  2010
lemma (in Lattice) ACf_inf: "ACf inf"
nipkow@15512
  2011
by(blast intro: ACf.intro inf_commute inf_assoc)
nipkow@15500
  2012
nipkow@15500
  2013
lemma (in Lattice) ACf_sup: "ACf sup"
nipkow@15512
  2014
by(blast intro: ACf.intro sup_commute sup_assoc)
nipkow@15500
  2015
nipkow@15500
  2016
lemma (in Lattice) ACIf_inf: "ACIf inf"
nipkow@15500
  2017
apply(rule ACIf.intro)
nipkow@15500
  2018
apply(rule ACf_inf)
nipkow@15500
  2019
apply(rule ACIf_axioms.intro)
nipkow@15500
  2020
apply(rule inf_idem)
nipkow@15500
  2021
done
nipkow@15500
  2022
nipkow@15500
  2023
lemma (in Lattice) ACIf_sup: "ACIf sup"
nipkow@15500
  2024
apply(rule ACIf.intro)
nipkow@15500
  2025
apply(rule ACf_sup)
nipkow@15500
  2026
apply(rule ACIf_axioms.intro)
nipkow@15500
  2027
apply(rule sup_idem)
nipkow@15500
  2028
done
nipkow@15500
  2029
nipkow@15500
  2030
lemma (in Lattice) ACIfSL_inf: "ACIfSL inf (op \<sqsubseteq>)"
nipkow@15500
  2031
apply(rule ACIfSL.intro)
nipkow@15500
  2032
apply(rule ACf_inf)
nipkow@15500
  2033
apply(rule ACIf.axioms[OF ACIf_inf])
nipkow@15500
  2034
apply(rule ACIfSL_axioms.intro)
nipkow@15500
  2035
apply(rule iffI)
nipkow@15500
  2036
 apply(blast intro: antisym inf_le1 inf_le2 inf_least refl)
nipkow@15500
  2037
apply(erule subst)
nipkow@15500
  2038
apply(rule inf_le2)
nipkow@15500
  2039
done
nipkow@15500
  2040
nipkow@15500
  2041
lemma (in Lattice) ACIfSL_sup: "ACIfSL sup (%x y. y \<sqsubseteq> x)"
nipkow@15500
  2042
apply(rule ACIfSL.intro)
nipkow@15500
  2043
apply(rule ACf_sup)
nipkow@15500
  2044
apply(rule ACIf.axioms[OF ACIf_sup])
nipkow@15500
  2045
apply(rule ACIfSL_axioms.intro)
nipkow@15500
  2046
apply(rule iffI)
nipkow@15500
  2047
 apply(blast intro: antisym sup_ge1 sup_ge2 sup_greatest refl)
nipkow@15500
  2048
apply(erule subst)
nipkow@15500
  2049
apply(rule sup_ge2)
nipkow@15500
  2050
done
nipkow@15500
  2051
nipkow@15505
  2052
nipkow@15505
  2053
subsubsection{* Fold laws in lattices *}
nipkow@15500
  2054
nipkow@15500
  2055
lemma (in Lattice) Inf_le_Sup: "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> \<Sqinter>A \<sqsubseteq> \<Squnion>A"
nipkow@15500
  2056
apply(unfold Sup_def Inf_def)
nipkow@15500
  2057
apply(subgoal_tac "EX a. a:A")
nipkow@15500
  2058
prefer 2 apply blast
nipkow@15500
  2059
apply(erule exE)
nipkow@15500
  2060
apply(rule trans)
nipkow@15500
  2061
apply(erule (2) ACIfSL.fold1_belowI[OF ACIfSL_inf])
nipkow@15500
  2062
apply(erule (2) ACIfSL.fold1_belowI[OF ACIfSL_sup])
nipkow@15500
  2063
done
nipkow@15500
  2064
nipkow@15504
  2065
lemma (in Lattice) sup_Inf_absorb:
nipkow@15504
  2066
  "\<lbrakk> finite A; A \<noteq> {}; a \<in> A \<rbrakk> \<Longrightarrow> (a \<squnion> \<Sqinter>A) = a"
nipkow@15512
  2067
apply(subst sup_commute)
nipkow@15504
  2068
apply(simp add:Inf_def sup_absorb ACIfSL.fold1_belowI[OF ACIfSL_inf])
nipkow@15504
  2069
done
nipkow@15504
  2070
nipkow@15504
  2071
lemma (in Lattice) inf_Sup_absorb:
nipkow@15504
  2072
  "\<lbrakk> finite A; A \<noteq> {}; a \<in> A \<rbrakk> \<Longrightarrow> (a \<sqinter> \<Squnion>A) = a"
nipkow@15504
  2073
by(simp add:Sup_def inf_absorb ACIfSL.fold1_belowI[OF ACIfSL_sup])
nipkow@15504
  2074
nipkow@15504
  2075
nipkow@15512
  2076
lemma (in Distrib_Lattice) sup_Inf1_distrib:
nipkow@15500
  2077
assumes A: "finite A" "A \<noteq> {}"
nipkow@15500
  2078
shows "(x \<squnion> \<Sqinter>A) = \<Sqinter>{x \<squnion> a|a. a \<in> A}"
nipkow@15500
  2079
using A
nipkow@15500
  2080
proof (induct rule: finite_ne_induct)
nipkow@15500
  2081
  case singleton thus ?case by(simp add:Inf_def)
nipkow@15500
  2082
next
nipkow@15500
  2083
  case (insert y A)
nipkow@15500
  2084
  have fin: "finite {x \<squnion> a |a. a \<in> A}"
berghofe@15517
  2085
    by(fast intro: finite_surj[where f = "%a. x \<squnion> a", OF insert(1)])
nipkow@15500
  2086
  have "x \<squnion> \<Sqinter> (insert y A) = x \<squnion> (y \<sqinter> \<Sqinter> A)"
nipkow@15500
  2087
    using insert by(simp add:ACf.fold1_insert_def[OF ACf_inf Inf_def])
nipkow@15500
  2088
  also have "\<dots> = (x \<squnion> y) \<sqinter> (x \<squnion> \<Sqinter> A)" by(rule sup_inf_distrib1)
nipkow@15500
  2089
  also have "x \<squnion> \<Sqinter> A = \<Sqinter>{x \<squnion> a|a. a \<in> A}" using insert by simp
nipkow@15500
  2090
  also have "(x \<squnion> y) \<sqinter> \<dots> = \<Sqinter> (insert (x \<squnion> y) {x \<squnion> a |a. a \<in> A})"
paulson@15509
  2091
    using insert by(simp add:ACIf.fold1_insert_idem_def[OF ACIf_inf Inf_def fin])
nipkow@15500
  2092
  also have "insert (x\<squnion>y) {x\<squnion>a |a. a \<in> A} = {x\<squnion>a |a. a \<in> insert y A}"
nipkow@15500
  2093
    by blast
nipkow@15500
  2094
  finally show ?case .
nipkow@15500
  2095
qed
nipkow@15500
  2096
nipkow@15512
  2097
lemma (in Distrib_Lattice) sup_Inf2_distrib:
nipkow@15500
  2098
assumes A: "finite A" "A \<noteq> {}" and B: "finite B" "B \<noteq> {}"
nipkow@15500
  2099
shows "(\<Sqinter>A \<squnion> \<Sqinter>B) = \<Sqinter>{a \<squnion> b|a b. a \<in> A \<and> b \<in> B}"
nipkow@15500
  2100
using A
nipkow@15500
  2101
proof (induct rule: finite_ne_induct)
nipkow@15500
  2102
  case singleton thus ?case
nipkow@15500
  2103
    by(simp add: sup_Inf1_distrib[OF B] fold1_singleton_def[OF Inf_def])
nipkow@15500
  2104
next
nipkow@15500
  2105
  case (insert x A)
nipkow@15500
  2106
  have finB: "finite {x \<squnion> b |b. b \<in> B}"
berghofe@15517
  2107
    by(fast intro: finite_surj[where f = "%b. x \<squnion> b", OF B(1)])
nipkow@15500
  2108
  have finAB: "finite {a \<squnion> b |a b. a \<in> A \<and> b \<in> B}"
nipkow@15500
  2109
  proof -
nipkow@15500
  2110
    have "{a \<squnion> b |a b. a \<in> A \<and> b \<in> B} = (UN a:A. UN b:B. {a \<squnion> b})"
nipkow@15500
  2111
      by blast
berghofe@15517
  2112
    thus ?thesis by(simp add: insert(1) B(1))
nipkow@15500
  2113
  qed
nipkow@15500
  2114
  have ne: "{a \<squnion> b |a b. a \<in> A \<and> b \<in> B} \<noteq> {}" using insert B by blast
nipkow@15500
  2115
  have "\<Sqinter>(insert x A) \<squnion> \<Sqinter>B = (x \<sqinter> \<Sqinter>A) \<squnion> \<Sqinter>B"
paulson@15509
  2116
    using insert by(simp add:ACIf.fold1_insert_idem_def[OF ACIf_inf Inf_def])
nipkow@15500
  2117
  also have "\<dots> = (x \<squnion> \<Sqinter>B) \<sqinter> (\<Sqinter>A \<squnion> \<Sqinter>B)" by(rule sup_inf_distrib2)
nipkow@15500
  2118
  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
  2119
    using insert by(simp add:sup_Inf1_distrib[OF B])
nipkow@15500
  2120
  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
  2121
    (is "_ = \<Sqinter>?M")
nipkow@15500
  2122
    using B insert
nipkow@15500
  2123
    by(simp add:Inf_def ACIf.fold1_Un2[OF ACIf_inf finB _ finAB ne])
nipkow@15500
  2124
  also have "?M = {a \<squnion> b |a b. a \<in> insert x A \<and> b \<in> B}"
nipkow@15500
  2125
    by blast
nipkow@15500
  2126
  finally show ?case .
nipkow@15500
  2127
qed
nipkow@15500
  2128
nipkow@15484
  2129
nipkow@15392
  2130
subsection{*Min and Max*}
nipkow@15392
  2131
nipkow@15392
  2132
text{* As an application of @{text fold1} we define the minimal and
nipkow@15497
  2133
maximal element of a (non-empty) set over a linear order. *}
nipkow@15497
  2134
nipkow@15497
  2135
constdefs
nipkow@15497
  2136
  Min :: "('a::linorder)set => 'a"
nipkow@15497
  2137
  "Min  ==  fold1 min"
nipkow@15497
  2138
nipkow@15497
  2139
  Max :: "('a::linorder)set => 'a"
nipkow@15497
  2140
  "Max  ==  fold1 max"
nipkow@15497
  2141
nipkow@15497
  2142
nipkow@15497
  2143
text{* Before we can do anything, we need to show that @{text min} and
nipkow@15497
  2144
@{text max} are ACI and the ordering is linear: *}
nipkow@15392
  2145
nipkow@15392
  2146
lemma ACf_min: "ACf(min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15392
  2147
apply(rule ACf.intro)
nipkow@15392
  2148
apply(auto simp:min_def)
nipkow@15392
  2149
done
nipkow@15392
  2150
nipkow@15392
  2151
lemma ACIf_min: "ACIf(min:: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15392
  2152
apply(rule ACIf.intro[OF ACf_min])
nipkow@15392
  2153
apply(rule ACIf_axioms.intro)
nipkow@15392
  2154
apply(auto simp:min_def)
nipkow@15376
  2155
done
nipkow@15376
  2156
nipkow@15392
  2157
lemma ACf_max: "ACf(max :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15392
  2158
apply(rule ACf.intro)
nipkow@15392
  2159
apply(auto simp:max_def)
nipkow@15392
  2160
done
nipkow@15392
  2161
nipkow@15392
  2162
lemma ACIf_max: "ACIf(max:: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a)"
nipkow@15392
  2163
apply(rule ACIf.intro[OF ACf_max])
nipkow@15392
  2164
apply(rule ACIf_axioms.intro)
nipkow@15392
  2165
apply(auto simp:max_def)
nipkow@15376
  2166
done
wenzelm@12396
  2167
nipkow@15497
  2168
lemma ACIfSL_min: "ACIfSL(min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a) (op \<le>)"
nipkow@15497
  2169
apply(rule ACIfSL.intro)
nipkow@15497
  2170
apply(rule ACf_min)
nipkow@15497
  2171
apply(rule ACIf.axioms[OF ACIf_min])
nipkow@15497
  2172
apply(rule ACIfSL_axioms.intro)
nipkow@15497
  2173
apply(auto simp:min_def)
nipkow@15497
  2174
done
nipkow@15497
  2175
nipkow@15497
  2176
lemma ACIfSLlin_min: "ACIfSLlin(min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a) (op \<le>)"
nipkow@15497
  2177
apply(rule ACIfSLlin.intro)
nipkow@15497
  2178
apply(rule ACf_min)
nipkow@15497
  2179
apply(rule ACIf.axioms[OF ACIf_min])
nipkow@15497
  2180
apply(rule ACIfSL.axioms[OF ACIfSL_min])
nipkow@15497
  2181
apply(rule ACIfSLlin_axioms.intro)
nipkow@15497
  2182
apply(auto simp:min_def)
nipkow@15497
  2183
done
nipkow@15392
  2184
nipkow@15497
  2185
lemma ACIfSL_max: "ACIfSL(max :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a) (%x y. y\<le>x)"
nipkow@15497
  2186
apply(rule ACIfSL.intro)
nipkow@15497
  2187
apply(rule ACf_max)
nipkow@15497
  2188
apply(rule ACIf.axioms[OF ACIf_max])
nipkow@15497
  2189
apply(rule ACIfSL_axioms.intro)
nipkow@15497
  2190
apply(auto simp:max_def)
nipkow@15497
  2191
done
nipkow@15392
  2192
nipkow@15497
  2193
lemma ACIfSLlin_max: "ACIfSLlin(max :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a) (%x y. y\<le>x)"
nipkow@15497
  2194
apply(rule ACIfSLlin.intro)
nipkow@15497
  2195
apply(rule ACf_max)
nipkow@15497
  2196
apply(rule ACIf.axioms[OF ACIf_max])
nipkow@15497
  2197
apply(rule ACIfSL.axioms[OF ACIfSL_max])
nipkow@15497
  2198
apply(rule ACIfSLlin_axioms.intro)
nipkow@15497
  2199
apply(auto simp:max_def)
nipkow@15497
  2200
done
nipkow@15392
  2201
nipkow@15500
  2202
lemma Lattice_min_max: "Lattice (op \<le>) (min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a) max"
nipkow@15507
  2203
apply(rule Lattice.intro)
nipkow@15512
  2204
apply(rule partial_order_order)
nipkow@15512
  2205
apply(rule lower_semilattice.axioms[OF lower_semilattice_lin_min])
nipkow@15526
  2206
apply(rule upper_semilattice.axioms[OF upper_semilattice_lin_max])
nipkow@15507
  2207
done
nipkow@15500
  2208
nipkow@15512
  2209
lemma Distrib_Lattice_min_max:
nipkow@15512
  2210
 "Distrib_Lattice (op \<le>) (min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a) max"
nipkow@15512
  2211
apply(rule Distrib_Lattice.intro)
nipkow@15512
  2212
apply(rule partial_order_order)
nipkow@15512
  2213
apply(rule lower_semilattice.axioms[OF lower_semilattice_lin_min])
nipkow@15526
  2214
apply(rule upper_semilattice.axioms[OF upper_semilattice_lin_max])
nipkow@15526
  2215
apply(rule distrib_lattice.axioms[OF distrib_lattice_min_max])
nipkow@15500
  2216
done
nipkow@15500
  2217
nipkow@15402
  2218
text{* Now we instantiate the recursion equations and declare them
nipkow@15392
  2219
simplification rules: *}
nipkow@15392
  2220
nipkow@15392
  2221
declare
nipkow@15392
  2222
  fold1_singleton_def[OF Min_def, simp]
paulson@15509
  2223
  ACIf.fold1_insert_idem_def[OF ACIf_min Min_def, simp]
nipkow@15392
  2224
  fold1_singleton_def[OF Max_def, simp]
paulson@15509
  2225
  ACIf.fold1_insert_idem_def[OF ACIf_max Max_def, simp]
nipkow@15392
  2226
nipkow@15484
  2227
text{* Now we instantiate some @{text fold1} properties: *}
nipkow@15392
  2228
nipkow@15392
  2229
lemma Min_in [simp]:
nipkow@15484
  2230
  shows "finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> Min A \<in> A"
nipkow@15484
  2231
using ACf.fold1_in[OF ACf_min]
nipkow@15484
  2232
by(fastsimp simp: Min_def min_def)
nipkow@15392
  2233
nipkow@15392
  2234
lemma Max_in [simp]:
nipkow@15484
  2235
  shows "finite A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> Max A \<in> A"
nipkow@15484
  2236
using ACf.fold1_in[OF ACf_max]
nipkow@15484
  2237
by(fastsimp simp: Max_def max_def)
nipkow@15392
  2238
nipkow@15484
  2239
lemma Min_le [simp]: "\<lbrakk> finite A; A \<noteq> {}; x \<in> A \<rbrakk> \<Longrightarrow> Min A \<le> x"
nipkow@15497
  2240
by(simp add: Min_def ACIfSL.fold1_belowI[OF ACIfSL_min])
nipkow@15392
  2241
nipkow@15484
  2242
lemma Max_ge [simp]: "\<lbrakk> finite A; A \<noteq> {}; x \<in> A \<rbrakk> \<Longrightarrow> x \<le> Max A"
nipkow@15497
  2243
by(simp add: Max_def ACIfSL.fold1_belowI[OF ACIfSL_max])
nipkow@15497
  2244
nipkow@15497
  2245
lemma Min_ge_iff[simp]:
nipkow@15497
  2246
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> (x \<le> Min A) = (\<forall>a\<in>A. x \<le> a)"
nipkow@15497
  2247
by(simp add: Min_def ACIfSL.below_fold1_iff[OF ACIfSL_min])
nipkow@15497
  2248
nipkow@15497
  2249
lemma Max_le_iff[simp]:
nipkow@15497
  2250
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> (Max A \<le> x) = (\<forall>a\<in>A. a \<le> x)"
nipkow@15497
  2251
by(simp add: Max_def ACIfSL.below_fold1_iff[OF ACIfSL_max])
nipkow@15497
  2252
nipkow@15497
  2253
lemma Min_le_iff:
nipkow@15497
  2254
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> (Min A \<le> x) = (\<exists>a\<in>A. a \<le> x)"
nipkow@15497
  2255
by(simp add: Min_def ACIfSLlin.fold1_below_iff[OF ACIfSLlin_min])
nipkow@15497
  2256
nipkow@15497
  2257
lemma Max_ge_iff:
nipkow@15497
  2258
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> (x \<le> Max A) = (\<exists>a\<in>A. x \<le> a)"
nipkow@15497
  2259
by(simp add: Max_def ACIfSLlin.fold1_below_iff[OF ACIfSLlin_max])
wenzelm@12396
  2260
nipkow@15500
  2261
lemma Min_le_Max:
nipkow@15500
  2262
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> Min A \<le> Max A"
nipkow@15500
  2263
by(simp add: Min_def Max_def Lattice.Inf_le_Sup[OF Lattice_min_max])
paulson@15506
  2264
nipkow@15500
  2265
lemma max_Min2_distrib:
nipkow@15500
  2266
  "\<lbrakk> finite A; A \<noteq> {}; finite B; B \<noteq> {} \<rbrakk> \<Longrightarrow>
nipkow@15500
  2267
  max (Min A) (Min B) = Min{ max a b |a b. a \<in> A \<and> b \<in> B}"
nipkow@15512
  2268
by(simp add: Min_def Distrib_Lattice.sup_Inf2_distrib[OF Distrib_Lattice_min_max])
paulson@15506
  2269
nipkow@15042
  2270
end