author | wenzelm |
Fri, 27 Jul 2018 22:23:37 +0200 | |
changeset 68695 | 9072bfd24d8f |
parent 67489 | f1ba59ddd9a6 |
child 69064 | 5840724b1d71 |
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 |
||
50 |
end |
|
51 |
||
52 |
||
60758 | 53 |
subsection \<open>List summation\<close> |
58320 | 54 |
|
55 |
context monoid_add |
|
56 |
begin |
|
57 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
58 |
sublocale sum_list: monoid_list plus 0 |
61776 | 59 |
defines |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
60 |
sum_list = sum_list.F .. |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
61 |
|
58320 | 62 |
end |
63 |
||
64 |
context comm_monoid_add |
|
65 |
begin |
|
66 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
67 |
sublocale sum_list: comm_monoid_list plus 0 |
61566
c3d6e570ccef
Keyword 'rewrites' identifies rewrite morphisms.
ballarin
parents:
61378
diff
changeset
|
68 |
rewrites |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
69 |
"monoid_list.F plus 0 = sum_list" |
58320 | 70 |
proof - |
71 |
show "comm_monoid_list plus 0" .. |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
72 |
then interpret sum_list: comm_monoid_list plus 0 . |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
73 |
from sum_list_def show "monoid_list.F plus 0 = sum_list" by simp |
58101 | 74 |
qed |
75 |
||
64267 | 76 |
sublocale sum: comm_monoid_list_set plus 0 |
61566
c3d6e570ccef
Keyword 'rewrites' identifies rewrite morphisms.
ballarin
parents:
61378
diff
changeset
|
77 |
rewrites |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
78 |
"monoid_list.F plus 0 = sum_list" |
64267 | 79 |
and "comm_monoid_set.F plus 0 = sum" |
58320 | 80 |
proof - |
81 |
show "comm_monoid_list_set plus 0" .. |
|
64267 | 82 |
then interpret sum: comm_monoid_list_set plus 0 . |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
83 |
from sum_list_def show "monoid_list.F plus 0 = sum_list" by simp |
64267 | 84 |
from sum_def show "comm_monoid_set.F plus 0 = sum" by (auto intro: sym) |
58320 | 85 |
qed |
86 |
||
87 |
end |
|
88 |
||
60758 | 89 |
text \<open>Some syntactic sugar for summing a function over a list:\<close> |
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
90 |
syntax (ASCII) |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
91 |
"_sum_list" :: "pttrn => 'a list => 'b => 'b" ("(3SUM _<-_. _)" [0, 51, 10] 10) |
58101 | 92 |
syntax |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
93 |
"_sum_list" :: "pttrn => 'a list => 'b => 'b" ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10) |
61799 | 94 |
translations \<comment> \<open>Beware of argument permutation!\<close> |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
95 |
"\<Sum>x\<leftarrow>xs. b" == "CONST sum_list (CONST map (\<lambda>x. b) xs)" |
58101 | 96 |
|
60758 | 97 |
text \<open>TODO duplicates\<close> |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
98 |
lemmas sum_list_simps = sum_list.Nil sum_list.Cons |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
99 |
lemmas sum_list_append = sum_list.append |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
100 |
lemmas sum_list_rev = sum_list.rev |
58320 | 101 |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
102 |
lemma (in monoid_add) fold_plus_sum_list_rev: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
103 |
"fold plus xs = plus (sum_list (rev xs))" |
58320 | 104 |
proof |
105 |
fix x |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
106 |
have "fold plus xs x = sum_list (rev xs @ [x])" |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
107 |
by (simp add: foldr_conv_fold sum_list.eq_foldr) |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
108 |
also have "\<dots> = sum_list (rev xs) + x" |
58320 | 109 |
by simp |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
110 |
finally show "fold plus xs x = sum_list (rev xs) + x" |
58320 | 111 |
. |
112 |
qed |
|
113 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
114 |
lemma (in comm_monoid_add) sum_list_map_remove1: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
115 |
"x \<in> set xs \<Longrightarrow> sum_list (map f xs) = f x + sum_list (map f (remove1 x xs))" |
58101 | 116 |
by (induct xs) (auto simp add: ac_simps) |
117 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
118 |
lemma (in monoid_add) size_list_conv_sum_list: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
119 |
"size_list f xs = sum_list (map f xs) + size xs" |
58101 | 120 |
by (induct xs) auto |
121 |
||
122 |
lemma (in monoid_add) length_concat: |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
123 |
"length (concat xss) = sum_list (map length xss)" |
58101 | 124 |
by (induct xss) simp_all |
125 |
||
126 |
lemma (in monoid_add) length_product_lists: |
|
67399 | 127 |
"length (product_lists xss) = foldr ( * ) (map length xss) 1" |
58101 | 128 |
proof (induct xss) |
129 |
case (Cons xs xss) then show ?case by (induct xs) (auto simp: length_concat o_def) |
|
130 |
qed simp |
|
131 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
132 |
lemma (in monoid_add) sum_list_map_filter: |
58101 | 133 |
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
|
134 |
shows "sum_list (map f (filter P xs)) = sum_list (map f xs)" |
58101 | 135 |
using assms by (induct xs) auto |
136 |
||
64267 | 137 |
lemma (in comm_monoid_add) distinct_sum_list_conv_Sum: |
138 |
"distinct xs \<Longrightarrow> sum_list xs = Sum (set xs)" |
|
58101 | 139 |
by (induct xs) simp_all |
140 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
141 |
lemma sum_list_upt[simp]: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
142 |
"m \<le> n \<Longrightarrow> sum_list [m..<n] = \<Sum> {m..<n}" |
64267 | 143 |
by(simp add: distinct_sum_list_conv_Sum) |
58995 | 144 |
|
66311 | 145 |
context ordered_comm_monoid_add |
146 |
begin |
|
147 |
||
148 |
lemma sum_list_nonneg: "(\<And>x. x \<in> set xs \<Longrightarrow> 0 \<le> x) \<Longrightarrow> 0 \<le> sum_list xs" |
|
149 |
by (induction xs) auto |
|
150 |
||
151 |
lemma sum_list_nonpos: "(\<And>x. x \<in> set xs \<Longrightarrow> x \<le> 0) \<Longrightarrow> sum_list xs \<le> 0" |
|
152 |
by (induction xs) (auto simp: add_nonpos_nonpos) |
|
58101 | 153 |
|
66311 | 154 |
lemma sum_list_nonneg_eq_0_iff: |
155 |
"(\<And>x. x \<in> set xs \<Longrightarrow> 0 \<le> x) \<Longrightarrow> sum_list xs = 0 \<longleftrightarrow> (\<forall>x\<in> set xs. x = 0)" |
|
156 |
by (induction xs) (simp_all add: add_nonneg_eq_0_iff sum_list_nonneg) |
|
157 |
||
158 |
end |
|
159 |
||
160 |
context canonically_ordered_monoid_add |
|
161 |
begin |
|
58101 | 162 |
|
66311 | 163 |
lemma sum_list_eq_0_iff [simp]: |
164 |
"sum_list ns = 0 \<longleftrightarrow> (\<forall>n \<in> set ns. n = 0)" |
|
165 |
by (simp add: sum_list_nonneg_eq_0_iff) |
|
166 |
||
167 |
lemma member_le_sum_list: |
|
168 |
"x \<in> set xs \<Longrightarrow> x \<le> sum_list xs" |
|
169 |
by (induction xs) (auto simp: add_increasing add_increasing2) |
|
58101 | 170 |
|
66311 | 171 |
lemma elem_le_sum_list: |
172 |
"k < size ns \<Longrightarrow> ns ! k \<le> sum_list (ns)" |
|
173 |
by (rule member_le_sum_list) simp |
|
174 |
||
175 |
end |
|
176 |
||
177 |
lemma (in ordered_cancel_comm_monoid_diff) sum_list_update: |
|
178 |
"k < size xs \<Longrightarrow> sum_list (xs[k := x]) = sum_list xs + x - xs ! k" |
|
179 |
apply(induction xs arbitrary:k) |
|
180 |
apply (auto simp: add_ac split: nat.split) |
|
181 |
apply(drule elem_le_sum_list) |
|
182 |
by (simp add: local.add_diff_assoc local.add_increasing) |
|
58101 | 183 |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
184 |
lemma (in monoid_add) sum_list_triv: |
58101 | 185 |
"(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r" |
186 |
by (induct xs) (simp_all add: distrib_right) |
|
187 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
188 |
lemma (in monoid_add) sum_list_0 [simp]: |
58101 | 189 |
"(\<Sum>x\<leftarrow>xs. 0) = 0" |
190 |
by (induct xs) (simp_all add: distrib_right) |
|
191 |
||
61799 | 192 |
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
|
193 |
lemma (in ab_group_add) uminus_sum_list_map: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
194 |
"- sum_list (map f xs) = sum_list (map (uminus \<circ> f) xs)" |
58101 | 195 |
by (induct xs) simp_all |
196 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
197 |
lemma (in comm_monoid_add) sum_list_addf: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
198 |
"(\<Sum>x\<leftarrow>xs. f x + g x) = sum_list (map f xs) + sum_list (map g xs)" |
58101 | 199 |
by (induct xs) (simp_all add: algebra_simps) |
200 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
201 |
lemma (in ab_group_add) sum_list_subtractf: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
202 |
"(\<Sum>x\<leftarrow>xs. f x - g x) = sum_list (map f xs) - sum_list (map g xs)" |
58101 | 203 |
by (induct xs) (simp_all add: algebra_simps) |
204 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
205 |
lemma (in semiring_0) sum_list_const_mult: |
58101 | 206 |
"(\<Sum>x\<leftarrow>xs. c * f x) = c * (\<Sum>x\<leftarrow>xs. f x)" |
207 |
by (induct xs) (simp_all add: algebra_simps) |
|
208 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
209 |
lemma (in semiring_0) sum_list_mult_const: |
58101 | 210 |
"(\<Sum>x\<leftarrow>xs. f x * c) = (\<Sum>x\<leftarrow>xs. f x) * c" |
211 |
by (induct xs) (simp_all add: algebra_simps) |
|
212 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
213 |
lemma (in ordered_ab_group_add_abs) sum_list_abs: |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
214 |
"\<bar>sum_list xs\<bar> \<le> sum_list (map abs xs)" |
58101 | 215 |
by (induct xs) (simp_all add: order_trans [OF abs_triangle_ineq]) |
216 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
217 |
lemma sum_list_mono: |
58101 | 218 |
fixes f g :: "'a \<Rightarrow> 'b::{monoid_add, ordered_ab_semigroup_add}" |
219 |
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)" |
|
220 |
by (induct xs) (simp, simp add: add_mono) |
|
221 |
||
64267 | 222 |
lemma (in monoid_add) sum_list_distinct_conv_sum_set: |
223 |
"distinct xs \<Longrightarrow> sum_list (map f xs) = sum f (set xs)" |
|
58101 | 224 |
by (induct xs) simp_all |
225 |
||
64267 | 226 |
lemma (in monoid_add) interv_sum_list_conv_sum_set_nat: |
227 |
"sum_list (map f [m..<n]) = sum f (set [m..<n])" |
|
228 |
by (simp add: sum_list_distinct_conv_sum_set) |
|
58101 | 229 |
|
64267 | 230 |
lemma (in monoid_add) interv_sum_list_conv_sum_set_int: |
231 |
"sum_list (map f [k..l]) = sum f (set [k..l])" |
|
232 |
by (simp add: sum_list_distinct_conv_sum_set) |
|
58101 | 233 |
|
64267 | 234 |
text \<open>General equivalence between @{const sum_list} and @{const sum}\<close> |
235 |
lemma (in monoid_add) sum_list_sum_nth: |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
236 |
"sum_list xs = (\<Sum> i = 0 ..< length xs. xs ! i)" |
67399 | 237 |
using interv_sum_list_conv_sum_set_nat [of "(!) xs" 0 "length xs"] by (simp add: map_nth) |
58101 | 238 |
|
64267 | 239 |
lemma sum_list_map_eq_sum_count: |
240 |
"sum_list (map f xs) = sum (\<lambda>x. count_list xs x * f x) (set xs)" |
|
59728 | 241 |
proof(induction xs) |
242 |
case (Cons x xs) |
|
243 |
show ?case (is "?l = ?r") |
|
244 |
proof cases |
|
245 |
assume "x \<in> set xs" |
|
60541 | 246 |
have "?l = f x + (\<Sum>x\<in>set xs. count_list xs x * f x)" by (simp add: Cons.IH) |
60758 | 247 |
also have "set xs = insert x (set xs - {x})" using \<open>x \<in> set xs\<close>by blast |
60541 | 248 |
also have "f x + (\<Sum>x\<in>insert x (set xs - {x}). count_list xs x * f x) = ?r" |
64267 | 249 |
by (simp add: sum.insert_remove eq_commute) |
59728 | 250 |
finally show ?thesis . |
251 |
next |
|
252 |
assume "x \<notin> set xs" |
|
253 |
hence "\<And>xa. xa \<in> set xs \<Longrightarrow> x \<noteq> xa" by blast |
|
60758 | 254 |
thus ?thesis by (simp add: Cons.IH \<open>x \<notin> set xs\<close>) |
59728 | 255 |
qed |
256 |
qed simp |
|
257 |
||
64267 | 258 |
lemma sum_list_map_eq_sum_count2: |
59728 | 259 |
assumes "set xs \<subseteq> X" "finite X" |
64267 | 260 |
shows "sum_list (map f xs) = sum (\<lambda>x. count_list xs x * f x) X" |
59728 | 261 |
proof- |
60541 | 262 |
let ?F = "\<lambda>x. count_list xs x * f x" |
64267 | 263 |
have "sum ?F X = sum ?F (set xs \<union> (X - set xs))" |
59728 | 264 |
using Un_absorb1[OF assms(1)] by(simp) |
64267 | 265 |
also have "\<dots> = sum ?F (set xs)" |
59728 | 266 |
using assms(2) |
64267 | 267 |
by(simp add: sum.union_disjoint[OF _ _ Diff_disjoint] del: Un_Diff_cancel) |
268 |
finally show ?thesis by(simp add:sum_list_map_eq_sum_count) |
|
59728 | 269 |
qed |
270 |
||
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
271 |
lemma sum_list_nonneg: |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
272 |
"(\<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
|
273 |
by (induction xs) simp_all |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
61955
diff
changeset
|
274 |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
275 |
lemma (in monoid_add) sum_list_map_filter': |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
276 |
"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
|
277 |
by (induction xs) simp_all |
af0e964aad7b
Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents:
61955
diff
changeset
|
278 |
|
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
279 |
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
|
280 |
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
|
281 |
|
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
282 |
lemma sorted_wrt_less_sum_mono_lowerbound: |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
283 |
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
|
284 |
assumes mono: "\<And>x y. x\<le>y \<Longrightarrow> f x \<le> f y" |
67399 | 285 |
shows "sorted_wrt (<) ns \<Longrightarrow> |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
286 |
(\<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
|
287 |
proof (induction ns rule: rev_induct) |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
288 |
case Nil |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
289 |
then show ?case by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
290 |
next |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
291 |
case (snoc n ns) |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
292 |
have "sum f {0..<length (ns @ [n])} |
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
293 |
= 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
|
294 |
by simp |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
295 |
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
|
296 |
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
|
297 |
also have "length ns \<le> n" |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
298 |
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
|
299 |
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
|
300 |
using mono add_mono by blast |
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
301 |
thus ?case by simp |
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
302 |
qed |
66434
5d7e770c7d5d
added sorted_wrt to List; added Data_Structures/Binomial_Heap.thy
nipkow
parents:
66311
diff
changeset
|
303 |
|
58101 | 304 |
|
60758 | 305 |
subsection \<open>Further facts about @{const List.n_lists}\<close> |
58101 | 306 |
|
307 |
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
|
308 |
by (induct n) (auto simp add: comp_def length_concat sum_list_triv) |
58101 | 309 |
|
310 |
lemma distinct_n_lists: |
|
311 |
assumes "distinct xs" |
|
312 |
shows "distinct (List.n_lists n xs)" |
|
313 |
proof (rule card_distinct) |
|
314 |
from assms have card_length: "card (set xs) = length xs" by (rule distinct_card) |
|
315 |
have "card (set (List.n_lists n xs)) = card (set xs) ^ n" |
|
316 |
proof (induct n) |
|
317 |
case 0 then show ?case by simp |
|
318 |
next |
|
319 |
case (Suc n) |
|
320 |
moreover have "card (\<Union>ys\<in>set (List.n_lists n xs). (\<lambda>y. y # ys) ` set xs) |
|
321 |
= (\<Sum>ys\<in>set (List.n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))" |
|
322 |
by (rule card_UN_disjoint) auto |
|
323 |
moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)" |
|
324 |
by (rule card_image) (simp add: inj_on_def) |
|
325 |
ultimately show ?case by auto |
|
326 |
qed |
|
327 |
also have "\<dots> = length xs ^ n" by (simp add: card_length) |
|
328 |
finally show "card (set (List.n_lists n xs)) = length (List.n_lists n xs)" |
|
329 |
by (simp add: length_n_lists) |
|
330 |
qed |
|
331 |
||
332 |
||
60758 | 333 |
subsection \<open>Tools setup\<close> |
58101 | 334 |
|
64267 | 335 |
lemmas sum_code = sum.set_conv_list |
58320 | 336 |
|
64267 | 337 |
lemma sum_set_upto_conv_sum_list_int [code_unfold]: |
338 |
"sum f (set [i..j::int]) = sum_list (map f [i..j])" |
|
339 |
by (simp add: interv_sum_list_conv_sum_set_int) |
|
58101 | 340 |
|
64267 | 341 |
lemma sum_set_upt_conv_sum_list_nat [code_unfold]: |
342 |
"sum f (set [m..<n]) = sum_list (map f [m..<n])" |
|
343 |
by (simp add: interv_sum_list_conv_sum_set_nat) |
|
58101 | 344 |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
345 |
lemma sum_list_transfer[transfer_rule]: |
63343 | 346 |
includes lifting_syntax |
58101 | 347 |
assumes [transfer_rule]: "A 0 0" |
67399 | 348 |
assumes [transfer_rule]: "(A ===> A ===> A) (+) (+)" |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
349 |
shows "(list_all2 A ===> A) sum_list sum_list" |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
350 |
unfolding sum_list.eq_foldr [abs_def] |
58101 | 351 |
by transfer_prover |
352 |
||
58368 | 353 |
|
60758 | 354 |
subsection \<open>List product\<close> |
58368 | 355 |
|
356 |
context monoid_mult |
|
357 |
begin |
|
358 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
359 |
sublocale prod_list: monoid_list times 1 |
61776 | 360 |
defines |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
361 |
prod_list = prod_list.F .. |
58368 | 362 |
|
58320 | 363 |
end |
58368 | 364 |
|
365 |
context comm_monoid_mult |
|
366 |
begin |
|
367 |
||
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
368 |
sublocale prod_list: comm_monoid_list times 1 |
61566
c3d6e570ccef
Keyword 'rewrites' identifies rewrite morphisms.
ballarin
parents:
61378
diff
changeset
|
369 |
rewrites |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
370 |
"monoid_list.F times 1 = prod_list" |
58368 | 371 |
proof - |
372 |
show "comm_monoid_list times 1" .. |
|
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
373 |
then interpret prod_list: comm_monoid_list times 1 . |
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
374 |
from prod_list_def show "monoid_list.F times 1 = prod_list" by simp |
58368 | 375 |
qed |
376 |
||
64272 | 377 |
sublocale prod: comm_monoid_list_set times 1 |
61566
c3d6e570ccef
Keyword 'rewrites' identifies rewrite morphisms.
ballarin
parents:
61378
diff
changeset
|
378 |
rewrites |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
379 |
"monoid_list.F times 1 = prod_list" |
64272 | 380 |
and "comm_monoid_set.F times 1 = prod" |
58368 | 381 |
proof - |
382 |
show "comm_monoid_list_set times 1" .. |
|
64272 | 383 |
then interpret prod: comm_monoid_list_set times 1 . |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
384 |
from prod_list_def show "monoid_list.F times 1 = prod_list" by simp |
64272 | 385 |
from prod_def show "comm_monoid_set.F times 1 = prod" by (auto intro: sym) |
58368 | 386 |
qed |
387 |
||
388 |
end |
|
389 |
||
67489
f1ba59ddd9a6
drop redundant cong rules
Lars Hupel <lars.hupel@mytum.de>
parents:
67399
diff
changeset
|
390 |
lemma prod_list_zero_iff: |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
391 |
"prod_list xs = 0 \<longleftrightarrow> (0 :: 'a :: {semiring_no_zero_divisors, semiring_1}) \<in> set xs" |
63317
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
eberlm
parents:
63290
diff
changeset
|
392 |
by (induction xs) simp_all |
ca187a9f66da
Various additions to polynomials, FPSs, Gamma function
eberlm
parents:
63290
diff
changeset
|
393 |
|
60758 | 394 |
text \<open>Some syntactic sugar:\<close> |
58368 | 395 |
|
61955
e96292f32c3c
former "xsymbols" syntax is used by default, and ASCII replacement syntax with print mode "ASCII";
wenzelm
parents:
61799
diff
changeset
|
396 |
syntax (ASCII) |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
397 |
"_prod_list" :: "pttrn => 'a list => 'b => 'b" ("(3PROD _<-_. _)" [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
|
398 |
syntax |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
399 |
"_prod_list" :: "pttrn => 'a list => 'b => 'b" ("(3\<Prod>_\<leftarrow>_. _)" [0, 51, 10] 10) |
61799 | 400 |
translations \<comment> \<open>Beware of argument permutation!\<close> |
63882
018998c00003
renamed listsum -> sum_list, listprod ~> prod_list
nipkow
parents:
63343
diff
changeset
|
401 |
"\<Prod>x\<leftarrow>xs. b" \<rightleftharpoons> "CONST prod_list (CONST map (\<lambda>x. b) xs)" |
58368 | 402 |
|
403 |
end |