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