author | nipkow |
Tue, 17 Jun 2025 14:11:40 +0200 | |
changeset 82733 | 8b537e1af2ec |
parent 82674 | f4441890dee0 |
permissions | -rw-r--r-- |
58101 | 1 |
(* Author: Tobias Nipkow, TU Muenchen *) |
2 |
||
60758 | 3 |
section \<open>Sum and product over lists\<close> |
58101 | 4 |
|
5 |
theory Groups_List |
|
6 |
imports List |
|
7 |
begin |
|
8 |
||
58320 | 9 |
locale monoid_list = monoid |
10 |
begin |
|
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
11 |
|
58320 | 12 |
definition F :: "'a list \<Rightarrow> 'a" |
13 |
where |
|
63290
9ac558ab0906
boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
haftmann
parents:
63101
diff
changeset
|
14 |
eq_foldr [code]: "F xs = foldr f xs \<^bold>1" |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
15 |
|
58320 | 16 |
lemma Nil [simp]: |
63290
9ac558ab0906
boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
haftmann
parents:
63101
diff
changeset
|
17 |
"F [] = \<^bold>1" |
58320 | 18 |
by (simp add: eq_foldr) |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
19 |
|
58320 | 20 |
lemma Cons [simp]: |
63290
9ac558ab0906
boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
haftmann
parents:
63101
diff
changeset
|
21 |
"F (x # xs) = x \<^bold>* F xs" |
58320 | 22 |
by (simp add: eq_foldr) |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
23 |
|
58320 | 24 |
lemma append [simp]: |
63290
9ac558ab0906
boldify syntax in abstract algebraic structures, to avoid clashes with concrete syntax in corresponding type classes
haftmann
parents:
63101
diff
changeset
|
25 |
"F (xs @ ys) = F xs \<^bold>* F ys" |
58320 | 26 |
by (induct xs) (simp_all add: assoc) |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
27 |
|
58320 | 28 |
end |
58101 | 29 |
|
58320 | 30 |
locale comm_monoid_list = comm_monoid + monoid_list |
31 |
begin |
|
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
32 |
|
58320 | 33 |
lemma rev [simp]: |
34 |
"F (rev xs) = F xs" |
|
35 |
by (simp add: eq_foldr foldr_fold fold_rev fun_eq_iff assoc left_commute) |
|
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
36 |
|
58320 | 37 |
end |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
38 |
|
58320 | 39 |
locale comm_monoid_list_set = list: comm_monoid_list + set: comm_monoid_set |
40 |
begin |
|
58101 | 41 |
|
58320 | 42 |
lemma distinct_set_conv_list: |
43 |
"distinct xs \<Longrightarrow> set.F g (set xs) = list.F (map g xs)" |
|
44 |
by (induct xs) simp_all |
|
58101 | 45 |
|
58320 | 46 |
lemma set_conv_list [code]: |
47 |
"set.F g (set xs) = list.F (map g (remdups xs))" |
|
48 |
by (simp add: distinct_set_conv_list [symmetric]) |
|
49 |
||
80061
4c1347e172b1
moved over material from AFP; most importantly on algebraic numbers and algebraically closed fields
Manuel Eberl <eberlm@in.tum.de>
parents:
79017
diff
changeset
|
50 |
lemma list_conv_set_nth: |
4c1347e172b1
moved over material from AFP; most importantly on algebraic numbers and algebraically closed fields
Manuel Eberl <eberlm@in.tum.de>
parents:
79017
diff
changeset
|
51 |
"list.F xs = set.F (\<lambda>i. xs ! i) {0..<length xs}" |
4c1347e172b1
moved over material from AFP; most importantly on algebraic numbers and algebraically closed fields
Manuel Eberl <eberlm@in.tum.de>
parents:
79017
diff
changeset
|
52 |
proof - |
4c1347e172b1
moved over material from AFP; most importantly on algebraic numbers and algebraically closed fields
Manuel Eberl <eberlm@in.tum.de>
parents:
79017
diff
changeset
|
53 |
have "xs = map (\<lambda>i. xs ! i) [0..<length xs]" |
4c1347e172b1
moved over material from AFP; most importantly on algebraic numbers and algebraically closed fields
Manuel Eberl <eberlm@in.tum.de>
parents:
79017
diff
changeset
|
54 |
by (simp add: map_nth) |
4c1347e172b1
moved over material from AFP; most importantly on algebraic numbers and algebraically closed fields
Manuel Eberl <eberlm@in.tum.de>
parents:
79017
diff
changeset
|
55 |
also have "list.F \<dots> = set.F (\<lambda>i. xs ! i) {0..<length xs}" |
4c1347e172b1
moved over material from AFP; most importantly on algebraic numbers and algebraically closed fields
Manuel Eberl <eberlm@in.tum.de>
parents:
79017
diff
changeset
|
56 |
by (subst distinct_set_conv_list [symmetric]) auto |
4c1347e172b1
moved over material from AFP; most importantly on algebraic numbers and algebraically closed fields
Manuel Eberl <eberlm@in.tum.de>
parents:
79017
diff
changeset
|
57 |
finally show ?thesis . |
4c1347e172b1
moved over material from AFP; most importantly on algebraic numbers and algebraically closed fields
Manuel Eberl <eberlm@in.tum.de>
parents:
79017
diff
changeset
|
58 |
qed |
4c1347e172b1
moved over material from AFP; most importantly on algebraic numbers and algebraically closed fields
Manuel Eberl <eberlm@in.tum.de>
parents:
79017
diff
changeset
|
59 |
|
82674 | 60 |
lemma atLeastLessThan_conv_list [code_unfold]: |
61 |
\<open>set.F g {a..<b} = list.F (map g (List.range a b))\<close> |
|
62 |
using List.atLeastLessThan_eq_range |
|
63 |
by (simp flip: List.set_range_eq add: distinct_set_conv_list) |
|
64 |
||
65 |
lemma atLeastAtMost_conv_list [code_unfold]: |
|
66 |
\<open>set.F g {a..b} = list.F (map g (List.range a (b + 1)))\<close> |
|
67 |
by (simp flip: List.set_range_eq add: List.atLeastAtMost_eq_range distinct_set_conv_list) |
|
68 |
||
69 |
lemma greaterThanLessThan_conv_list [code_unfold]: |
|
70 |
\<open>set.F g {a<..<b} = list.F (map g (List.range (a + 1) b))\<close> |
|
71 |
by (simp flip: List.set_range_eq add: List.greaterThanLessThan_eq_range distinct_set_conv_list) |
|
72 |
||
73 |
lemma greaterThanAtMost_conv_list [code_unfold]: |
|
74 |
\<open>set.F g {a<..b} = list.F (map g (List.range (a + 1) (b + 1)))\<close> |
|
75 |
by (simp flip: List.set_range_eq add: List.greaterThanAtMost_eq_range distinct_set_conv_list) |
|
76 |
||
58320 | 77 |
end |
78 |
||
79 |
||
60758 | 80 |
subsection \<open>List summation\<close> |
58320 | 81 |
|
82 |
context monoid_add |
|
83 |
begin |
|
84 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
85 |
sublocale sum_list: monoid_list plus 0 |
61776 | 86 |
defines |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
87 |
sum_list = sum_list.F .. |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
88 |
|
58320 | 89 |
end |
90 |
||
91 |
context comm_monoid_add |
|
92 |
begin |
|
93 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
94 |
sublocale sum_list: comm_monoid_list plus 0 |
61566
c3d6e570ccef
Keyword 'rewrites' identifies rewrite morphisms.
ballarin
parents:
61378
diff
changeset
|
95 |
rewrites |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
96 |
"monoid_list.F plus 0 = sum_list" |
58320 | 97 |
proof - |
98 |
show "comm_monoid_list plus 0" .. |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
99 |
then interpret sum_list: comm_monoid_list plus 0 . |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
100 |
from sum_list_def show "monoid_list.F plus 0 = sum_list" by simp |
58101 | 101 |
qed |
102 |
||
64267 | 103 |
sublocale sum: comm_monoid_list_set plus 0 |
61566
c3d6e570ccef
Keyword 'rewrites' identifies rewrite morphisms.
ballarin
parents:
61378
diff
changeset
|
104 |
rewrites |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
105 |
"monoid_list.F plus 0 = sum_list" |
64267 | 106 |
and "comm_monoid_set.F plus 0 = sum" |
58320 | 107 |
proof - |
108 |
show "comm_monoid_list_set plus 0" .. |
|
64267 | 109 |
then interpret sum: comm_monoid_list_set plus 0 . |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
110 |
from sum_list_def show "monoid_list.F plus 0 = sum_list" by simp |
64267 | 111 |
from sum_def show "comm_monoid_set.F plus 0 = sum" by (auto intro: sym) |
58320 | 112 |
qed |
113 |
||
114 |
end |
|
115 |
||
60758 | 116 |
text \<open>Some syntactic sugar for summing a function over a list:\<close> |
81595
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
117 |
|
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
118 |
open_bundle sum_list_syntax |
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
119 |
begin |
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
120 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
121 |
syntax (ASCII) |
80934 | 122 |
"_sum_list" :: "pttrn => 'a list => 'b => 'b" (\<open>(\<open>indent=3 notation=\<open>binder SUM\<close>\<close>SUM _<-_. _)\<close> [0, 51, 10] 10) |
58101 | 123 |
syntax |
80934 | 124 |
"_sum_list" :: "pttrn => 'a list => 'b => 'b" (\<open>(\<open>indent=3 notation=\<open>binder \<Sum>\<close>\<close>\<Sum>_\<leftarrow>_. _)\<close> [0, 51, 10] 10) |
80760 | 125 |
syntax_consts |
126 |
"_sum_list" == sum_list |
|
61799 | 127 |
translations \<comment> \<open>Beware of argument permutation!\<close> |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
128 |
"\<Sum>x\<leftarrow>xs. b" == "CONST sum_list (CONST map (\<lambda>x. b) xs)" |
58101 | 129 |
|
81595
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
130 |
end |
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
131 |
|
70928 | 132 |
context |
133 |
includes lifting_syntax |
|
134 |
begin |
|
135 |
||
136 |
lemma sum_list_transfer [transfer_rule]: |
|
137 |
"(list_all2 A ===> A) sum_list sum_list" |
|
138 |
if [transfer_rule]: "A 0 0" "(A ===> A ===> A) (+) (+)" |
|
139 |
unfolding sum_list.eq_foldr [abs_def] |
|
140 |
by transfer_prover |
|
141 |
||
142 |
end |
|
143 |
||
60758 | 144 |
text \<open>TODO duplicates\<close> |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
145 |
lemmas sum_list_simps = sum_list.Nil sum_list.Cons |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
146 |
lemmas sum_list_append = sum_list.append |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
147 |
lemmas sum_list_rev = sum_list.rev |
58320 | 148 |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
149 |
lemma (in monoid_add) fold_plus_sum_list_rev: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
150 |
"fold plus xs = plus (sum_list (rev xs))" |
58320 | 151 |
proof |
152 |
fix x |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
153 |
have "fold plus xs x = sum_list (rev xs @ [x])" |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
154 |
by (simp add: foldr_conv_fold sum_list.eq_foldr) |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
155 |
also have "\<dots> = sum_list (rev xs) + x" |
58320 | 156 |
by simp |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
157 |
finally show "fold plus xs x = sum_list (rev xs) + x" |
58320 | 158 |
. |
159 |
qed |
|
160 |
||
82080
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
161 |
lemma sum_list_of_nat: "sum_list (map of_nat xs) = of_nat (sum_list xs)" |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
162 |
by (induction xs) auto |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
163 |
|
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
164 |
lemma sum_list_of_int: "sum_list (map of_int xs) = of_int (sum_list xs)" |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
165 |
by (induction xs) auto |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
166 |
|
82662 | 167 |
lemma count_list_concat: "count_list (concat xss) x = sum_list (map (\<lambda>xs. count_list xs x) xss)" |
168 |
by(induction xss) auto |
|
169 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
170 |
lemma (in comm_monoid_add) sum_list_map_remove1: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
171 |
"x \<in> set xs \<Longrightarrow> sum_list (map f xs) = f x + sum_list (map f (remove1 x xs))" |
58101 | 172 |
by (induct xs) (auto simp add: ac_simps) |
173 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
174 |
lemma (in monoid_add) size_list_conv_sum_list: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
175 |
"size_list f xs = sum_list (map f xs) + size xs" |
58101 | 176 |
by (induct xs) auto |
177 |
||
178 |
lemma (in monoid_add) length_concat: |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
179 |
"length (concat xss) = sum_list (map length xss)" |
58101 | 180 |
by (induct xss) simp_all |
181 |
||
182 |
lemma (in monoid_add) length_product_lists: |
|
69064
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents:
67489
diff
changeset
|
183 |
"length (product_lists xss) = foldr (*) (map length xss) 1" |
58101 | 184 |
proof (induct xss) |
185 |
case (Cons xs xss) then show ?case by (induct xs) (auto simp: length_concat o_def) |
|
186 |
qed simp |
|
187 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
188 |
lemma (in monoid_add) sum_list_map_filter: |
58101 | 189 |
assumes "\<And>x. x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> f x = 0" |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
190 |
shows "sum_list (map f (filter P xs)) = sum_list (map f xs)" |
58101 | 191 |
using assms by (induct xs) auto |
192 |
||
69231 | 193 |
lemma sum_list_filter_le_nat: |
194 |
fixes f :: "'a \<Rightarrow> nat" |
|
195 |
shows "sum_list (map f (filter P xs)) \<le> sum_list (map f xs)" |
|
196 |
by(induction xs; simp) |
|
197 |
||
64267 | 198 |
lemma (in comm_monoid_add) distinct_sum_list_conv_Sum: |
199 |
"distinct xs \<Longrightarrow> sum_list xs = Sum (set xs)" |
|
82080
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
200 |
by (metis local.sum.set_conv_list local.sum_list_def map_ident remdups_id_iff_distinct) |
58101 | 201 |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
202 |
lemma sum_list_upt[simp]: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
203 |
"m \<le> n \<Longrightarrow> sum_list [m..<n] = \<Sum> {m..<n}" |
64267 | 204 |
by(simp add: distinct_sum_list_conv_Sum) |
58995 | 205 |
|
66311 | 206 |
context ordered_comm_monoid_add |
207 |
begin |
|
208 |
||
209 |
lemma sum_list_nonneg: "(\<And>x. x \<in> set xs \<Longrightarrow> 0 \<le> x) \<Longrightarrow> 0 \<le> sum_list xs" |
|
82080
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
210 |
by (induction xs) auto |
66311 | 211 |
|
212 |
lemma sum_list_nonpos: "(\<And>x. x \<in> set xs \<Longrightarrow> x \<le> 0) \<Longrightarrow> sum_list xs \<le> 0" |
|
82080
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
213 |
by (induction xs) (auto simp: add_nonpos_nonpos) |
58101 | 214 |
|
66311 | 215 |
lemma sum_list_nonneg_eq_0_iff: |
216 |
"(\<And>x. x \<in> set xs \<Longrightarrow> 0 \<le> x) \<Longrightarrow> sum_list xs = 0 \<longleftrightarrow> (\<forall>x\<in> set xs. x = 0)" |
|
82080
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
217 |
by (induction xs) (simp_all add: add_nonneg_eq_0_iff sum_list_nonneg) |
66311 | 218 |
|
219 |
end |
|
220 |
||
221 |
context canonically_ordered_monoid_add |
|
222 |
begin |
|
58101 | 223 |
|
66311 | 224 |
lemma sum_list_eq_0_iff [simp]: |
225 |
"sum_list ns = 0 \<longleftrightarrow> (\<forall>n \<in> set ns. n = 0)" |
|
82097 | 226 |
by (simp add: sum_list_nonneg_eq_0_iff) |
66311 | 227 |
|
228 |
lemma member_le_sum_list: |
|
229 |
"x \<in> set xs \<Longrightarrow> x \<le> sum_list xs" |
|
82097 | 230 |
by (induction xs) (auto simp: add_increasing add_increasing2) |
58101 | 231 |
|
66311 | 232 |
lemma elem_le_sum_list: |
233 |
"k < size ns \<Longrightarrow> ns ! k \<le> sum_list (ns)" |
|
82097 | 234 |
by (simp add: member_le_sum_list) |
66311 | 235 |
|
236 |
end |
|
237 |
||
238 |
lemma (in ordered_cancel_comm_monoid_diff) sum_list_update: |
|
239 |
"k < size xs \<Longrightarrow> sum_list (xs[k := x]) = sum_list xs + x - xs ! k" |
|
82080
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
240 |
proof (induction xs arbitrary:k) |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
241 |
case Nil |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
242 |
then show ?case by auto |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
243 |
next |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
244 |
case (Cons a xs) |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
245 |
then show ?case |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
246 |
apply (simp add: add_ac split: nat.split) |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
247 |
using add_increasing diff_add_assoc elem_le_sum_list zero_le by force |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
248 |
qed |
58101 | 249 |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
250 |
lemma (in monoid_add) sum_list_triv: |
58101 | 251 |
"(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r" |
252 |
by (induct xs) (simp_all add: distrib_right) |
|
253 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
254 |
lemma (in monoid_add) sum_list_0 [simp]: |
58101 | 255 |
"(\<Sum>x\<leftarrow>xs. 0) = 0" |
256 |
by (induct xs) (simp_all add: distrib_right) |
|
257 |
||
61799 | 258 |
text\<open>For non-Abelian groups \<open>xs\<close> needs to be reversed on one side:\<close> |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
259 |
lemma (in ab_group_add) uminus_sum_list_map: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
260 |
"- sum_list (map f xs) = sum_list (map (uminus \<circ> f) xs)" |
58101 | 261 |
by (induct xs) simp_all |
262 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
263 |
lemma (in comm_monoid_add) sum_list_addf: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
264 |
"(\<Sum>x\<leftarrow>xs. f x + g x) = sum_list (map f xs) + sum_list (map g xs)" |
58101 | 265 |
by (induct xs) (simp_all add: algebra_simps) |
266 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
267 |
lemma (in ab_group_add) sum_list_subtractf: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
268 |
"(\<Sum>x\<leftarrow>xs. f x - g x) = sum_list (map f xs) - sum_list (map g xs)" |
58101 | 269 |
by (induct xs) (simp_all add: algebra_simps) |
270 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
271 |
lemma (in semiring_0) sum_list_const_mult: |
58101 | 272 |
"(\<Sum>x\<leftarrow>xs. c * f x) = c * (\<Sum>x\<leftarrow>xs. f x)" |
273 |
by (induct xs) (simp_all add: algebra_simps) |
|
274 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
275 |
lemma (in semiring_0) sum_list_mult_const: |
58101 | 276 |
"(\<Sum>x\<leftarrow>xs. f x * c) = (\<Sum>x\<leftarrow>xs. f x) * c" |
277 |
by (induct xs) (simp_all add: algebra_simps) |
|
278 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
279 |
lemma (in ordered_ab_group_add_abs) sum_list_abs: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
280 |
"\<bar>sum_list xs\<bar> \<le> sum_list (map abs xs)" |
58101 | 281 |
by (induct xs) (simp_all add: order_trans [OF abs_triangle_ineq]) |
282 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
283 |
lemma sum_list_mono: |
58101 | 284 |
fixes f g :: "'a \<Rightarrow> 'b::{monoid_add, ordered_ab_semigroup_add}" |
285 |
shows "(\<And>x. x \<in> set xs \<Longrightarrow> f x \<le> g x) \<Longrightarrow> (\<Sum>x\<leftarrow>xs. f x) \<le> (\<Sum>x\<leftarrow>xs. g x)" |
|
69231 | 286 |
by (induct xs) (simp, simp add: add_mono) |
287 |
||
288 |
lemma sum_list_strict_mono: |
|
289 |
fixes f g :: "'a \<Rightarrow> 'b::{monoid_add, strict_ordered_ab_semigroup_add}" |
|
290 |
shows "\<lbrakk> xs \<noteq> []; \<And>x. x \<in> set xs \<Longrightarrow> f x < g x \<rbrakk> |
|
291 |
\<Longrightarrow> sum_list (map f xs) < sum_list (map g xs)" |
|
292 |
proof (induction xs) |
|
293 |
case Nil thus ?case by simp |
|
294 |
next |
|
295 |
case C: (Cons _ xs) |
|
296 |
show ?case |
|
297 |
proof (cases xs) |
|
298 |
case Nil thus ?thesis using C.prems by simp |
|
299 |
next |
|
300 |
case Cons thus ?thesis using C by(simp add: add_strict_mono) |
|
301 |
qed |
|
302 |
qed |
|
58101 | 303 |
|
75693 | 304 |
text \<open>A much more general version of this monotonicity lemma |
305 |
can be formulated with multisets and the multiset order\<close> |
|
306 |
||
307 |
lemma sum_list_mono2: fixes xs :: "'a ::ordered_comm_monoid_add list" |
|
308 |
shows "\<lbrakk> length xs = length ys; \<And>i. i < length xs \<longrightarrow> xs!i \<le> ys!i \<rbrakk> |
|
309 |
\<Longrightarrow> sum_list xs \<le> sum_list ys" |
|
82080
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
310 |
by (induction xs ys rule: list_induct2) (auto simp: nth_Cons' less_Suc_eq_0_disj imp_ex add_mono) |
75693 | 311 |
|
64267 | 312 |
lemma (in monoid_add) sum_list_distinct_conv_sum_set: |
313 |
"distinct xs \<Longrightarrow> sum_list (map f xs) = sum f (set xs)" |
|
58101 | 314 |
by (induct xs) simp_all |
315 |
||
64267 | 316 |
lemma (in monoid_add) interv_sum_list_conv_sum_set_nat: |
317 |
"sum_list (map f [m..<n]) = sum f (set [m..<n])" |
|
318 |
by (simp add: sum_list_distinct_conv_sum_set) |
|
58101 | 319 |
|
64267 | 320 |
lemma (in monoid_add) interv_sum_list_conv_sum_set_int: |
321 |
"sum_list (map f [k..l]) = sum f (set [k..l])" |
|
322 |
by (simp add: sum_list_distinct_conv_sum_set) |
|
58101 | 323 |
|
69593 | 324 |
text \<open>General equivalence between \<^const>\<open>sum_list\<close> and \<^const>\<open>sum\<close>\<close> |
64267 | 325 |
lemma (in monoid_add) sum_list_sum_nth: |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
326 |
"sum_list xs = (\<Sum> i = 0 ..< length xs. xs ! i)" |
67399 | 327 |
using interv_sum_list_conv_sum_set_nat [of "(!) xs" 0 "length xs"] by (simp add: map_nth) |
58101 | 328 |
|
64267 | 329 |
lemma sum_list_map_eq_sum_count: |
330 |
"sum_list (map f xs) = sum (\<lambda>x. count_list xs x * f x) (set xs)" |
|
59728 | 331 |
proof(induction xs) |
332 |
case (Cons x xs) |
|
333 |
show ?case (is "?l = ?r") |
|
334 |
proof cases |
|
335 |
assume "x \<in> set xs" |
|
60541 | 336 |
have "?l = f x + (\<Sum>x\<in>set xs. count_list xs x * f x)" by (simp add: Cons.IH) |
60758 | 337 |
also have "set xs = insert x (set xs - {x})" using \<open>x \<in> set xs\<close>by blast |
60541 | 338 |
also have "f x + (\<Sum>x\<in>insert x (set xs - {x}). count_list xs x * f x) = ?r" |
64267 | 339 |
by (simp add: sum.insert_remove eq_commute) |
59728 | 340 |
finally show ?thesis . |
341 |
next |
|
342 |
assume "x \<notin> set xs" |
|
343 |
hence "\<And>xa. xa \<in> set xs \<Longrightarrow> x \<noteq> xa" by blast |
|
60758 | 344 |
thus ?thesis by (simp add: Cons.IH \<open>x \<notin> set xs\<close>) |
59728 | 345 |
qed |
346 |
qed simp |
|
347 |
||
64267 | 348 |
lemma sum_list_map_eq_sum_count2: |
59728 | 349 |
assumes "set xs \<subseteq> X" "finite X" |
64267 | 350 |
shows "sum_list (map f xs) = sum (\<lambda>x. count_list xs x * f x) X" |
59728 | 351 |
proof- |
60541 | 352 |
let ?F = "\<lambda>x. count_list xs x * f x" |
64267 | 353 |
have "sum ?F X = sum ?F (set xs \<union> (X - set xs))" |
59728 | 354 |
using Un_absorb1[OF assms(1)] by(simp) |
64267 | 355 |
also have "\<dots> = sum ?F (set xs)" |
59728 | 356 |
using assms(2) |
64267 | 357 |
by(simp add: sum.union_disjoint[OF _ _ Diff_disjoint] del: Un_Diff_cancel) |
358 |
finally show ?thesis by(simp add:sum_list_map_eq_sum_count) |
|
59728 | 359 |
qed |
360 |
||
72545 | 361 |
lemma sum_list_replicate: "sum_list (replicate n c) = of_nat n * c" |
362 |
by(induction n)(auto simp add: distrib_right) |
|
363 |
||
364 |
||
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
365 |
lemma sum_list_nonneg: |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
366 |
"(\<And>x. x \<in> set xs \<Longrightarrow> (x :: 'a :: ordered_comm_monoid_add) \<ge> 0) \<Longrightarrow> sum_list xs \<ge> 0" |
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
61955
diff
changeset
|
367 |
by (induction xs) simp_all |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
61955
diff
changeset
|
368 |
|
69231 | 369 |
lemma sum_list_Suc: |
370 |
"sum_list (map (\<lambda>x. Suc(f x)) xs) = sum_list (map f xs) + length xs" |
|
371 |
by(induction xs; simp) |
|
372 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
373 |
lemma (in monoid_add) sum_list_map_filter': |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
374 |
"sum_list (map f (filter P xs)) = sum_list (map (\<lambda>x. if P x then f x else 0) xs)" |
63099
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
61955
diff
changeset
|
375 |
by (induction xs) simp_all |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
61955
diff
changeset
|
376 |
|
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
377 |
text \<open>Summation of a strictly ascending sequence with length \<open>n\<close> |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
378 |
can be upper-bounded by summation over \<open>{0..<n}\<close>.\<close> |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
379 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
380 |
lemma sorted_wrt_less_sum_mono_lowerbound: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
381 |
fixes f :: "nat \<Rightarrow> ('b::ordered_comm_monoid_add)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
382 |
assumes mono: "\<And>x y. x\<le>y \<Longrightarrow> f x \<le> f y" |
67399 | 383 |
shows "sorted_wrt (<) ns \<Longrightarrow> |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
384 |
(\<Sum>i\<in>{0..<length ns}. f i) \<le> (\<Sum>i\<leftarrow>ns. f i)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
385 |
proof (induction ns rule: rev_induct) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
386 |
case Nil |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
387 |
then show ?case by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
388 |
next |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
389 |
case (snoc n ns) |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
390 |
have "sum f {0..<length (ns @ [n])} |
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
391 |
= sum f {0..<length ns} + f (length ns)" |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
392 |
by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
393 |
also have "sum f {0..<length ns} \<le> sum_list (map f ns)" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
394 |
using snoc by (auto simp: sorted_wrt_append) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
395 |
also have "length ns \<le> n" |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
396 |
using sorted_wrt_less_idx[OF snoc.prems(1), of "length ns"] by auto |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
397 |
finally have "sum f {0..<length (ns @ [n])} \<le> sum_list (map f ns) + f n" |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
398 |
using mono add_mono by blast |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
399 |
thus ?case by simp |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
400 |
qed |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
401 |
|
82097 | 402 |
(*Note that we also have this for class canonically_ordered_monoid_add*) |
82080
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
403 |
lemma member_le_sum_list: |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
404 |
fixes x :: "'a :: ordered_comm_monoid_add" |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
405 |
assumes "x \<in> set xs" "\<And>x. x \<in> set xs \<Longrightarrow> x \<ge> 0" |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
406 |
shows "x \<le> sum_list xs" |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
407 |
using assms |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
408 |
proof (induction xs) |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
409 |
case (Cons y xs) |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
410 |
show ?case |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
411 |
proof (cases "y = x") |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
412 |
case True |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
413 |
have "x + 0 \<le> x + sum_list xs" |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
414 |
by (intro add_mono order.refl sum_list_nonneg) (use Cons in auto) |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
415 |
thus ?thesis |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
416 |
using True by auto |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
417 |
next |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
418 |
case False |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
419 |
have "0 + x \<le> y + sum_list xs" |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
420 |
by (intro add_mono Cons.IH Cons.prems) (use Cons.prems False in auto) |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
421 |
thus ?thesis |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
422 |
by auto |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
423 |
qed |
0aa2d1c132b2
A couple of theorems proved by Manuel Eberl from his AFP entry Sum_Of_Squares_Count
paulson <lp15@cam.ac.uk>
parents:
81595
diff
changeset
|
424 |
qed auto |
58101 | 425 |
|
72024 | 426 |
subsection \<open>Horner sums\<close> |
427 |
||
428 |
context comm_semiring_0 |
|
429 |
begin |
|
430 |
||
431 |
definition horner_sum :: \<open>('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'a\<close> |
|
432 |
where horner_sum_foldr: \<open>horner_sum f a xs = foldr (\<lambda>x b. f x + a * b) xs 0\<close> |
|
433 |
||
434 |
lemma horner_sum_simps [simp]: |
|
435 |
\<open>horner_sum f a [] = 0\<close> |
|
436 |
\<open>horner_sum f a (x # xs) = f x + a * horner_sum f a xs\<close> |
|
437 |
by (simp_all add: horner_sum_foldr) |
|
438 |
||
439 |
lemma horner_sum_eq_sum_funpow: |
|
440 |
\<open>horner_sum f a xs = (\<Sum>n = 0..<length xs. ((*) a ^^ n) (f (xs ! n)))\<close> |
|
441 |
proof (induction xs) |
|
442 |
case Nil |
|
443 |
then show ?case |
|
444 |
by simp |
|
445 |
next |
|
446 |
case (Cons x xs) |
|
447 |
then show ?case |
|
448 |
by (simp add: sum.atLeast0_lessThan_Suc_shift sum_distrib_left del: sum.op_ivl_Suc) |
|
449 |
qed |
|
450 |
||
451 |
end |
|
452 |
||
453 |
context |
|
454 |
includes lifting_syntax |
|
455 |
begin |
|
456 |
||
457 |
lemma horner_sum_transfer [transfer_rule]: |
|
458 |
\<open>((B ===> A) ===> A ===> list_all2 B ===> A) horner_sum horner_sum\<close> |
|
459 |
if [transfer_rule]: \<open>A 0 0\<close> |
|
460 |
and [transfer_rule]: \<open>(A ===> A ===> A) (+) (+)\<close> |
|
461 |
and [transfer_rule]: \<open>(A ===> A ===> A) (*) (*)\<close> |
|
462 |
by (unfold horner_sum_foldr) transfer_prover |
|
463 |
||
464 |
end |
|
465 |
||
466 |
context comm_semiring_1 |
|
467 |
begin |
|
468 |
||
469 |
lemma horner_sum_eq_sum: |
|
470 |
\<open>horner_sum f a xs = (\<Sum>n = 0..<length xs. f (xs ! n) * a ^ n)\<close> |
|
471 |
proof - |
|
472 |
have \<open>(*) a ^^ n = (*) (a ^ n)\<close> for n |
|
473 |
by (induction n) (simp_all add: ac_simps) |
|
474 |
then show ?thesis |
|
475 |
by (simp add: horner_sum_eq_sum_funpow ac_simps) |
|
476 |
qed |
|
477 |
||
72619 | 478 |
lemma horner_sum_append: |
479 |
\<open>horner_sum f a (xs @ ys) = horner_sum f a xs + a ^ length xs * horner_sum f a ys\<close> |
|
480 |
using sum.atLeastLessThan_shift_bounds [of _ 0 \<open>length xs\<close> \<open>length ys\<close>] |
|
481 |
atLeastLessThan_add_Un [of 0 \<open>length xs\<close> \<open>length ys\<close>] |
|
482 |
by (simp add: horner_sum_eq_sum sum_distrib_left sum.union_disjoint ac_simps nth_append power_add) |
|
483 |
||
72024 | 484 |
end |
485 |
||
75662 | 486 |
context linordered_semidom |
487 |
begin |
|
488 |
||
489 |
lemma horner_sum_nonnegative: |
|
490 |
\<open>0 \<le> horner_sum of_bool 2 bs\<close> |
|
491 |
by (induction bs) simp_all |
|
492 |
||
493 |
end |
|
494 |
||
78935
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
495 |
context discrete_linordered_semidom |
75662 | 496 |
begin |
497 |
||
498 |
lemma horner_sum_bound: |
|
499 |
\<open>horner_sum of_bool 2 bs < 2 ^ length bs\<close> |
|
500 |
proof (induction bs) |
|
501 |
case Nil |
|
502 |
then show ?case |
|
503 |
by simp |
|
504 |
next |
|
505 |
case (Cons b bs) |
|
506 |
moreover define a where \<open>a = 2 ^ length bs - horner_sum of_bool 2 bs\<close> |
|
507 |
ultimately have *: \<open>2 ^ length bs = horner_sum of_bool 2 bs + a\<close> |
|
508 |
by simp |
|
78935
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
509 |
have \<open>0 < a\<close> |
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
510 |
using Cons * by simp |
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
511 |
moreover have \<open>1 \<le> a\<close> |
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
512 |
using \<open>0 < a\<close> by (simp add: less_eq_iff_succ_less) |
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
513 |
ultimately have \<open>0 + 1 < a + a\<close> |
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
514 |
by (rule add_less_le_mono) |
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
515 |
then have \<open>1 < a * 2\<close> |
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
516 |
by (simp add: mult_2_right) |
75662 | 517 |
with Cons show ?case |
78935
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
518 |
by (simp add: * algebra_simps) |
75662 | 519 |
qed |
520 |
||
79017 | 521 |
lemma horner_sum_of_bool_2_less: |
522 |
\<open>(horner_sum of_bool 2 bs) < 2 ^ length bs\<close> |
|
523 |
by (fact horner_sum_bound) |
|
524 |
||
75662 | 525 |
end |
526 |
||
527 |
lemma nat_horner_sum [simp]: |
|
528 |
\<open>nat (horner_sum of_bool 2 bs) = horner_sum of_bool 2 bs\<close> |
|
529 |
by (induction bs) (auto simp add: nat_add_distrib horner_sum_nonnegative) |
|
530 |
||
78935
5e788ff7a489
explicit type class for discrete linordered semidoms
haftmann
parents:
75693
diff
changeset
|
531 |
context discrete_linordered_semidom |
75662 | 532 |
begin |
533 |
||
534 |
lemma horner_sum_less_eq_iff_lexordp_eq: |
|
535 |
\<open>horner_sum of_bool 2 bs \<le> horner_sum of_bool 2 cs \<longleftrightarrow> lexordp_eq (rev bs) (rev cs)\<close> |
|
536 |
if \<open>length bs = length cs\<close> |
|
537 |
proof - |
|
538 |
have \<open>horner_sum of_bool 2 (rev bs) \<le> horner_sum of_bool 2 (rev cs) \<longleftrightarrow> lexordp_eq bs cs\<close> |
|
539 |
if \<open>length bs = length cs\<close> for bs cs |
|
540 |
using that proof (induction bs cs rule: list_induct2) |
|
541 |
case Nil |
|
542 |
then show ?case |
|
543 |
by simp |
|
544 |
next |
|
545 |
case (Cons b bs c cs) |
|
546 |
with horner_sum_nonnegative [of \<open>rev bs\<close>] horner_sum_nonnegative [of \<open>rev cs\<close>] |
|
547 |
horner_sum_bound [of \<open>rev bs\<close>] horner_sum_bound [of \<open>rev cs\<close>] |
|
548 |
show ?case |
|
549 |
by (auto simp add: horner_sum_append not_le Cons intro: add_strict_increasing2 add_increasing) |
|
550 |
qed |
|
551 |
from that this [of \<open>rev bs\<close> \<open>rev cs\<close>] show ?thesis |
|
552 |
by simp |
|
553 |
qed |
|
554 |
||
555 |
lemma horner_sum_less_iff_lexordp: |
|
556 |
\<open>horner_sum of_bool 2 bs < horner_sum of_bool 2 cs \<longleftrightarrow> ord_class.lexordp (rev bs) (rev cs)\<close> |
|
557 |
if \<open>length bs = length cs\<close> |
|
558 |
proof - |
|
559 |
have \<open>horner_sum of_bool 2 (rev bs) < horner_sum of_bool 2 (rev cs) \<longleftrightarrow> ord_class.lexordp bs cs\<close> |
|
560 |
if \<open>length bs = length cs\<close> for bs cs |
|
561 |
using that proof (induction bs cs rule: list_induct2) |
|
562 |
case Nil |
|
563 |
then show ?case |
|
564 |
by simp |
|
565 |
next |
|
566 |
case (Cons b bs c cs) |
|
567 |
with horner_sum_nonnegative [of \<open>rev bs\<close>] horner_sum_nonnegative [of \<open>rev cs\<close>] |
|
568 |
horner_sum_bound [of \<open>rev bs\<close>] horner_sum_bound [of \<open>rev cs\<close>] |
|
569 |
show ?case |
|
570 |
by (auto simp add: horner_sum_append not_less Cons intro: add_strict_increasing2 add_increasing) |
|
571 |
qed |
|
572 |
from that this [of \<open>rev bs\<close> \<open>rev cs\<close>] show ?thesis |
|
573 |
by simp |
|
574 |
qed |
|
575 |
||
576 |
end |
|
577 |
||
72024 | 578 |
|
69593 | 579 |
subsection \<open>Further facts about \<^const>\<open>List.n_lists\<close>\<close> |
58101 | 580 |
|
581 |
lemma length_n_lists: "length (List.n_lists n xs) = length xs ^ n" |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
582 |
by (induct n) (auto simp add: comp_def length_concat sum_list_triv) |
58101 | 583 |
|
584 |
lemma distinct_n_lists: |
|
585 |
assumes "distinct xs" |
|
586 |
shows "distinct (List.n_lists n xs)" |
|
587 |
proof (rule card_distinct) |
|
588 |
from assms have card_length: "card (set xs) = length xs" by (rule distinct_card) |
|
589 |
have "card (set (List.n_lists n xs)) = card (set xs) ^ n" |
|
590 |
proof (induct n) |
|
591 |
case 0 then show ?case by simp |
|
592 |
next |
|
593 |
case (Suc n) |
|
594 |
moreover have "card (\<Union>ys\<in>set (List.n_lists n xs). (\<lambda>y. y # ys) ` set xs) |
|
595 |
= (\<Sum>ys\<in>set (List.n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))" |
|
596 |
by (rule card_UN_disjoint) auto |
|
597 |
moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)" |
|
598 |
by (rule card_image) (simp add: inj_on_def) |
|
599 |
ultimately show ?case by auto |
|
600 |
qed |
|
601 |
also have "\<dots> = length xs ^ n" by (simp add: card_length) |
|
602 |
finally show "card (set (List.n_lists n xs)) = length (List.n_lists n xs)" |
|
603 |
by (simp add: length_n_lists) |
|
604 |
qed |
|
605 |
||
606 |
||
60758 | 607 |
subsection \<open>Tools setup\<close> |
58101 | 608 |
|
64267 | 609 |
lemmas sum_code = sum.set_conv_list |
58320 | 610 |
|
82674 | 611 |
lemma sum_set_upto_conv_sum_list_int: |
64267 | 612 |
"sum f (set [i..j::int]) = sum_list (map f [i..j])" |
613 |
by (simp add: interv_sum_list_conv_sum_set_int) |
|
58101 | 614 |
|
82674 | 615 |
lemma sum_set_upt_conv_sum_list_nat: |
64267 | 616 |
"sum f (set [m..<n]) = sum_list (map f [m..<n])" |
617 |
by (simp add: interv_sum_list_conv_sum_set_nat) |
|
58101 | 618 |
|
58368 | 619 |
|
60758 | 620 |
subsection \<open>List product\<close> |
58368 | 621 |
|
622 |
context monoid_mult |
|
623 |
begin |
|
624 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
625 |
sublocale prod_list: monoid_list times 1 |
61776 | 626 |
defines |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
627 |
prod_list = prod_list.F .. |
58368 | 628 |
|
58320 | 629 |
end |
58368 | 630 |
|
631 |
context comm_monoid_mult |
|
632 |
begin |
|
633 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
634 |
sublocale prod_list: comm_monoid_list times 1 |
61566
c3d6e570ccef
Keyword 'rewrites' identifies rewrite morphisms.
ballarin
parents:
61378
diff
changeset
|
635 |
rewrites |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
636 |
"monoid_list.F times 1 = prod_list" |
58368 | 637 |
proof - |
638 |
show "comm_monoid_list times 1" .. |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
639 |
then interpret prod_list: comm_monoid_list times 1 . |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
640 |
from prod_list_def show "monoid_list.F times 1 = prod_list" by simp |
58368 | 641 |
qed |
642 |
||
64272 | 643 |
sublocale prod: comm_monoid_list_set times 1 |
61566
c3d6e570ccef
Keyword 'rewrites' identifies rewrite morphisms.
ballarin
parents:
61378
diff
changeset
|
644 |
rewrites |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
645 |
"monoid_list.F times 1 = prod_list" |
64272 | 646 |
and "comm_monoid_set.F times 1 = prod" |
58368 | 647 |
proof - |
648 |
show "comm_monoid_list_set times 1" .. |
|
64272 | 649 |
then interpret prod: comm_monoid_list_set times 1 . |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
650 |
from prod_list_def show "monoid_list.F times 1 = prod_list" by simp |
64272 | 651 |
from prod_def show "comm_monoid_set.F times 1 = prod" by (auto intro: sym) |
58368 | 652 |
qed |
653 |
||
654 |
end |
|
655 |
||
60758 | 656 |
text \<open>Some syntactic sugar:\<close> |
58368 | 657 |
|
81595
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
658 |
open_bundle prod_list_syntax |
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
659 |
begin |
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
660 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
661 |
syntax (ASCII) |
80934 | 662 |
"_prod_list" :: "pttrn => 'a list => 'b => 'b" (\<open>(\<open>indent=3 notation=\<open>binder PROD\<close>\<close>PROD _<-_. _)\<close> [0, 51, 10] 10) |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
663 |
syntax |
80934 | 664 |
"_prod_list" :: "pttrn => 'a list => 'b => 'b" (\<open>(\<open>indent=3 notation=\<open>binder \<Prod>\<close>\<close>\<Prod>_\<leftarrow>_. _)\<close> [0, 51, 10] 10) |
80760 | 665 |
syntax_consts |
666 |
"_prod_list" \<rightleftharpoons> prod_list |
|
61799 | 667 |
translations \<comment> \<open>Beware of argument permutation!\<close> |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
668 |
"\<Prod>x\<leftarrow>xs. b" \<rightleftharpoons> "CONST prod_list (CONST map (\<lambda>x. b) xs)" |
58368 | 669 |
|
81595
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
670 |
end |
ed264056f5dc
more syntax bundles, e.g. to explore terms without notation;
wenzelm
parents:
80934
diff
changeset
|
671 |
|
70928 | 672 |
context |
673 |
includes lifting_syntax |
|
674 |
begin |
|
675 |
||
676 |
lemma prod_list_transfer [transfer_rule]: |
|
677 |
"(list_all2 A ===> A) prod_list prod_list" |
|
678 |
if [transfer_rule]: "A 1 1" "(A ===> A ===> A) (*) (*)" |
|
679 |
unfolding prod_list.eq_foldr [abs_def] |
|
680 |
by transfer_prover |
|
681 |
||
58368 | 682 |
end |
70928 | 683 |
|
684 |
lemma prod_list_zero_iff: |
|
685 |
"prod_list xs = 0 \<longleftrightarrow> (0 :: 'a :: {semiring_no_zero_divisors, semiring_1}) \<in> set xs" |
|
686 |
by (induction xs) simp_all |
|
687 |
||
82597 | 688 |
lemma prod_list_nonneg: "(\<And> x. (x :: 'a :: ordered_semiring_1) \<in> set xs \<Longrightarrow> x \<ge> 0) \<Longrightarrow> prod_list xs \<ge> 0" |
689 |
by (induct xs) auto |
|
690 |
||
70928 | 691 |
end |