(* Author: Tobias Nipkow, TU Muenchen *)
section \Sum and product over lists\
theory Groups_List
imports List
begin
no_notation times (infixl "*" 70)
no_notation Groups.one ("1")
locale monoid_list = monoid
begin
definition F :: "'a list \ 'a"
where
eq_foldr [code]: "F xs = foldr f xs 1"
lemma Nil [simp]:
"F [] = 1"
by (simp add: eq_foldr)
lemma Cons [simp]:
"F (x # xs) = x * F xs"
by (simp add: eq_foldr)
lemma append [simp]:
"F (xs @ ys) = F xs * F ys"
by (induct xs) (simp_all add: assoc)
end
locale comm_monoid_list = comm_monoid + monoid_list
begin
lemma rev [simp]:
"F (rev xs) = F xs"
by (simp add: eq_foldr foldr_fold fold_rev fun_eq_iff assoc left_commute)
end
locale comm_monoid_list_set = list: comm_monoid_list + set: comm_monoid_set
begin
lemma distinct_set_conv_list:
"distinct xs \ set.F g (set xs) = list.F (map g xs)"
by (induct xs) simp_all
lemma set_conv_list [code]:
"set.F g (set xs) = list.F (map g (remdups xs))"
by (simp add: distinct_set_conv_list [symmetric])
end
notation times (infixl "*" 70)
notation Groups.one ("1")
subsection \List summation\
context monoid_add
begin
definition listsum :: "'a list \ 'a"
where
"listsum = monoid_list.F plus 0"
sublocale listsum!: monoid_list plus 0
rewrites
"monoid_list.F plus 0 = listsum"
proof -
show "monoid_list plus 0" ..
then interpret listsum!: monoid_list plus 0 .
from listsum_def show "monoid_list.F plus 0 = listsum" by rule
qed
end
context comm_monoid_add
begin
sublocale listsum!: comm_monoid_list plus 0
rewrites
"monoid_list.F plus 0 = listsum"
proof -
show "comm_monoid_list plus 0" ..
then interpret listsum!: comm_monoid_list plus 0 .
from listsum_def show "monoid_list.F plus 0 = listsum" by rule
qed
sublocale setsum!: comm_monoid_list_set plus 0
rewrites
"monoid_list.F plus 0 = listsum"
and "comm_monoid_set.F plus 0 = setsum"
proof -
show "comm_monoid_list_set plus 0" ..
then interpret setsum!: comm_monoid_list_set plus 0 .
from listsum_def show "monoid_list.F plus 0 = listsum" by rule
from setsum_def show "comm_monoid_set.F plus 0 = setsum" by rule
qed
end
text \Some syntactic sugar for summing a function over a list:\
syntax
"_listsum" :: "pttrn => 'a list => 'b => 'b" ("(3SUM _<-_. _)" [0, 51, 10] 10)
syntax (xsymbols)
"_listsum" :: "pttrn => 'a list => 'b => 'b" ("(3\_\_. _)" [0, 51, 10] 10)
translations -- \Beware of argument permutation!\
"SUM x<-xs. b" == "CONST listsum (CONST map (%x. b) xs)"
"\x\xs. b" == "CONST listsum (CONST map (%x. b) xs)"
text \TODO duplicates\
lemmas listsum_simps = listsum.Nil listsum.Cons
lemmas listsum_append = listsum.append
lemmas listsum_rev = listsum.rev
lemma (in monoid_add) fold_plus_listsum_rev:
"fold plus xs = plus (listsum (rev xs))"
proof
fix x
have "fold plus xs x = listsum (rev xs @ [x])"
by (simp add: foldr_conv_fold listsum.eq_foldr)
also have "\ = listsum (rev xs) + x"
by simp
finally show "fold plus xs x = listsum (rev xs) + x"
.
qed
lemma (in comm_monoid_add) listsum_map_remove1:
"x \ set xs \ listsum (map f xs) = f x + listsum (map f (remove1 x xs))"
by (induct xs) (auto simp add: ac_simps)
lemma (in monoid_add) size_list_conv_listsum:
"size_list f xs = listsum (map f xs) + size xs"
by (induct xs) auto
lemma (in monoid_add) length_concat:
"length (concat xss) = listsum (map length xss)"
by (induct xss) simp_all
lemma (in monoid_add) length_product_lists:
"length (product_lists xss) = foldr op * (map length xss) 1"
proof (induct xss)
case (Cons xs xss) then show ?case by (induct xs) (auto simp: length_concat o_def)
qed simp
lemma (in monoid_add) listsum_map_filter:
assumes "\x. x \ set xs \ \ P x \ f x = 0"
shows "listsum (map f (filter P xs)) = listsum (map f xs)"
using assms by (induct xs) auto
lemma (in comm_monoid_add) distinct_listsum_conv_Setsum:
"distinct xs \ listsum xs = Setsum (set xs)"
by (induct xs) simp_all
lemma listsum_upt[simp]:
"m \ n \ listsum [m.. {m.. (\n \ set ns. n = 0)"
by (induct ns) simp_all
lemma member_le_listsum_nat:
"(n :: nat) \ set ns \ n \ listsum ns"
by (induct ns) auto
lemma elem_le_listsum_nat:
"k < size ns \ ns ! k \ listsum (ns::nat list)"
by (rule member_le_listsum_nat) simp
lemma listsum_update_nat:
"k < size ns \ listsum (ns[k := (n::nat)]) = listsum ns + n - ns ! k"
apply(induct ns arbitrary:k)
apply (auto split:nat.split)
apply(drule elem_le_listsum_nat)
apply arith
done
lemma (in monoid_add) listsum_triv:
"(\x\xs. r) = of_nat (length xs) * r"
by (induct xs) (simp_all add: distrib_right)
lemma (in monoid_add) listsum_0 [simp]:
"(\x\xs. 0) = 0"
by (induct xs) (simp_all add: distrib_right)
text\For non-Abelian groups @{text xs} needs to be reversed on one side:\
lemma (in ab_group_add) uminus_listsum_map:
"- listsum (map f xs) = listsum (map (uminus \ f) xs)"
by (induct xs) simp_all
lemma (in comm_monoid_add) listsum_addf:
"(\x\xs. f x + g x) = listsum (map f xs) + listsum (map g xs)"
by (induct xs) (simp_all add: algebra_simps)
lemma (in ab_group_add) listsum_subtractf:
"(\x\xs. f x - g x) = listsum (map f xs) - listsum (map g xs)"
by (induct xs) (simp_all add: algebra_simps)
lemma (in semiring_0) listsum_const_mult:
"(\x\xs. c * f x) = c * (\x\xs. f x)"
by (induct xs) (simp_all add: algebra_simps)
lemma (in semiring_0) listsum_mult_const:
"(\x\xs. f x * c) = (\x\xs. f x) * c"
by (induct xs) (simp_all add: algebra_simps)
lemma (in ordered_ab_group_add_abs) listsum_abs:
"\listsum xs\ \ listsum (map abs xs)"
by (induct xs) (simp_all add: order_trans [OF abs_triangle_ineq])
lemma listsum_mono:
fixes f g :: "'a \ 'b::{monoid_add, ordered_ab_semigroup_add}"
shows "(\x. x \ set xs \ f x \ g x) \ (\x\xs. f x) \ (\x\xs. g x)"
by (induct xs) (simp, simp add: add_mono)
lemma (in monoid_add) listsum_distinct_conv_setsum_set:
"distinct xs \ listsum (map f xs) = setsum f (set xs)"
by (induct xs) simp_all
lemma (in monoid_add) interv_listsum_conv_setsum_set_nat:
"listsum (map f [m..General equivalence between @{const listsum} and @{const setsum}\
lemma (in monoid_add) listsum_setsum_nth:
"listsum xs = (\ i = 0 ..< length xs. xs ! i)"
using interv_listsum_conv_setsum_set_nat [of "op ! xs" 0 "length xs"] by (simp add: map_nth)
lemma listsum_map_eq_setsum_count:
"listsum (map f xs) = setsum (\x. count_list xs x * f x) (set xs)"
proof(induction xs)
case (Cons x xs)
show ?case (is "?l = ?r")
proof cases
assume "x \ set xs"
have "?l = f x + (\x\set xs. count_list xs x * f x)" by (simp add: Cons.IH)
also have "set xs = insert x (set xs - {x})" using \x \ set xs\by blast
also have "f x + (\x\insert x (set xs - {x}). count_list xs x * f x) = ?r"
by (simp add: setsum.insert_remove eq_commute)
finally show ?thesis .
next
assume "x \ set xs"
hence "\xa. xa \ set xs \ x \ xa" by blast
thus ?thesis by (simp add: Cons.IH \x \ set xs\)
qed
qed simp
lemma listsum_map_eq_setsum_count2:
assumes "set xs \ X" "finite X"
shows "listsum (map f xs) = setsum (\x. count_list xs x * f x) X"
proof-
let ?F = "\x. count_list xs x * f x"
have "setsum ?F X = setsum ?F (set xs \ (X - set xs))"
using Un_absorb1[OF assms(1)] by(simp)
also have "\ = setsum ?F (set xs)"
using assms(2)
by(simp add: setsum.union_disjoint[OF _ _ Diff_disjoint] del: Un_Diff_cancel)
finally show ?thesis by(simp add:listsum_map_eq_setsum_count)
qed
subsection \Further facts about @{const List.n_lists}\
lemma length_n_lists: "length (List.n_lists n xs) = length xs ^ n"
by (induct n) (auto simp add: comp_def length_concat listsum_triv)
lemma distinct_n_lists:
assumes "distinct xs"
shows "distinct (List.n_lists n xs)"
proof (rule card_distinct)
from assms have card_length: "card (set xs) = length xs" by (rule distinct_card)
have "card (set (List.n_lists n xs)) = card (set xs) ^ n"
proof (induct n)
case 0 then show ?case by simp
next
case (Suc n)
moreover have "card (\ys\set (List.n_lists n xs). (\y. y # ys) ` set xs)
= (\ys\set (List.n_lists n xs). card ((\y. y # ys) ` set xs))"
by (rule card_UN_disjoint) auto
moreover have "\ys. card ((\y. y # ys) ` set xs) = card (set xs)"
by (rule card_image) (simp add: inj_on_def)
ultimately show ?case by auto
qed
also have "\ = length xs ^ n" by (simp add: card_length)
finally show "card (set (List.n_lists n xs)) = length (List.n_lists n xs)"
by (simp add: length_n_lists)
qed
subsection \Tools setup\
lemmas setsum_code = setsum.set_conv_list
lemma setsum_set_upto_conv_listsum_int [code_unfold]:
"setsum f (set [i..j::int]) = listsum (map f [i..j])"
by (simp add: interv_listsum_conv_setsum_set_int)
lemma setsum_set_upt_conv_listsum_nat [code_unfold]:
"setsum f (set [m.. A ===> A) op + op +"
shows "(list_all2 A ===> A) listsum listsum"
unfolding listsum.eq_foldr [abs_def]
by transfer_prover
end
subsection \List product\
context monoid_mult
begin
definition listprod :: "'a list \ 'a"
where
"listprod = monoid_list.F times 1"
sublocale listprod!: monoid_list times 1
rewrites
"monoid_list.F times 1 = listprod"
proof -
show "monoid_list times 1" ..
then interpret listprod!: monoid_list times 1 .
from listprod_def show "monoid_list.F times 1 = listprod" by rule
qed
end
context comm_monoid_mult
begin
sublocale listprod!: comm_monoid_list times 1
rewrites
"monoid_list.F times 1 = listprod"
proof -
show "comm_monoid_list times 1" ..
then interpret listprod!: comm_monoid_list times 1 .
from listprod_def show "monoid_list.F times 1 = listprod" by rule
qed
sublocale setprod!: comm_monoid_list_set times 1
rewrites
"monoid_list.F times 1 = listprod"
and "comm_monoid_set.F times 1 = setprod"
proof -
show "comm_monoid_list_set times 1" ..
then interpret setprod!: comm_monoid_list_set times 1 .
from listprod_def show "monoid_list.F times 1 = listprod" by rule
from setprod_def show "comm_monoid_set.F times 1 = setprod" by rule
qed
end
text \Some syntactic sugar:\
syntax
"_listprod" :: "pttrn => 'a list => 'b => 'b" ("(3PROD _<-_. _)" [0, 51, 10] 10)
syntax (xsymbols)
"_listprod" :: "pttrn => 'a list => 'b => 'b" ("(3\_\_. _)" [0, 51, 10] 10)
translations -- \Beware of argument permutation!\
"PROD x<-xs. b" == "CONST listprod (CONST map (%x. b) xs)"
"\x\xs. b" == "CONST listprod (CONST map (%x. b) xs)"
end