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