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