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