# HG changeset patch # User haftmann # Date 1409469042 -7200 # Node ID e7ebe555428137d03a2dd3e6cabb31c0668ec942 # Parent f54a8a4134d32aa4baff4fad08a25566f637d452 separated listsum material diff -r f54a8a4134d3 -r e7ebe5554281 src/Doc/Main/Main_Doc.thy --- a/src/Doc/Main/Main_Doc.thy Sun Aug 31 09:10:41 2014 +0200 +++ b/src/Doc/Main/Main_Doc.thy Sun Aug 31 09:10:42 2014 +0200 @@ -517,7 +517,7 @@ @{const List.listrel1} & @{term_type_only List.listrel1 "('a*'a)set\('a list * 'a list)set"}\\ @{const List.lists} & @{term_type_only List.lists "'a set\'a list set"}\\ @{const List.listset} & @{term_type_only List.listset "'a set list \ 'a list set"}\\ -@{const List.listsum} & @{typeof List.listsum}\\ +@{const Groups_List.listsum} & @{typeof Groups_List.listsum}\\ @{const List.list_all2} & @{typeof List.list_all2}\\ @{const List.list_update} & @{typeof List.list_update}\\ @{const List.map} & @{typeof List.map}\\ diff -r f54a8a4134d3 -r e7ebe5554281 src/HOL/Enum.thy --- a/src/HOL/Enum.thy Sun Aug 31 09:10:41 2014 +0200 +++ b/src/HOL/Enum.thy Sun Aug 31 09:10:42 2014 +0200 @@ -3,7 +3,7 @@ header {* Finite types as explicit enumerations *} theory Enum -imports Map +imports Map Groups_List begin subsection {* Class @{text enum} *} diff -r f54a8a4134d3 -r e7ebe5554281 src/HOL/Groups_List.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/HOL/Groups_List.thy Sun Aug 31 09:10:42 2014 +0200 @@ -0,0 +1,212 @@ + +(* Author: Tobias Nipkow, TU Muenchen *) + +header {* Summation over lists *} + +theory Groups_List +imports List +begin + +definition (in monoid_add) listsum :: "'a list \ 'a" where +"listsum xs = foldr plus xs 0" + +subsubsection {* List summation: @{const listsum} and @{text"\"}*} + +lemma (in monoid_add) listsum_simps [simp]: + "listsum [] = 0" + "listsum (x # xs) = x + listsum xs" + by (simp_all add: listsum_def) + +lemma (in monoid_add) listsum_append [simp]: + "listsum (xs @ ys) = listsum xs + listsum ys" + by (induct xs) (simp_all add: add.assoc) + +lemma (in comm_monoid_add) listsum_rev [simp]: + "listsum (rev xs) = listsum xs" + by (simp add: listsum_def foldr_fold fold_rev fun_eq_iff add_ac) + +lemma (in monoid_add) fold_plus_listsum_rev: + "fold plus xs = plus (listsum (rev xs))" +proof + fix x + have "fold plus xs x = fold plus xs (x + 0)" by simp + also have "\ = fold plus (x # xs) 0" by simp + also have "\ = foldr plus (rev xs @ [x]) 0" by (simp add: foldr_conv_fold) + also have "\ = listsum (rev xs @ [x])" by (simp add: listsum_def) + also have "\ = listsum (rev xs) + listsum [x]" by simp + finally show "fold plus xs x = listsum (rev xs) + x" by simp +qed + +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) +syntax (HTML output) + "_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)" + +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_eq_0_nat_iff_nat [simp]: + "listsum ns = (0::nat) \ (\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.. i = 0 ..< length xs. xs ! i)" + using interv_listsum_conv_setsum_set_nat [of "op ! xs" 0 "length xs"] by (simp add: map_nth) + + +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 *} + +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_def[abs_def] + by transfer_prover + +end + +end \ No newline at end of file diff -r f54a8a4134d3 -r e7ebe5554281 src/HOL/List.thy --- a/src/HOL/List.thy Sun Aug 31 09:10:41 2014 +0200 +++ b/src/HOL/List.thy Sun Aug 31 09:10:42 2014 +0200 @@ -105,9 +105,6 @@ "concat [] = []" | "concat (x # xs) = x @ concat xs" -definition (in monoid_add) listsum :: "'a list \ 'a" where -"listsum xs = foldr plus xs 0" - primrec drop:: "nat \ 'a list \ 'a list" where drop_Nil: "drop n [] = []" | drop_Cons: "drop n (x # xs) = (case n of 0 \ x # xs | Suc m \ drop m xs)" @@ -313,8 +310,7 @@ @{lemma "rotate1 [a,b,c,d] = [b,c,d,a]" by simp}\\ @{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate_def eval_nat_numeral)}\\ @{lemma "replicate 4 a = [a,a,a,a]" by (simp add:eval_nat_numeral)}\\ -@{lemma "[2..<5] = [2,3,4]" by (simp add:eval_nat_numeral)}\\ -@{lemma "listsum [1,2,3::nat] = 6" by (simp add: listsum_def)} +@{lemma "[2..<5] = [2,3,4]" by (simp add:eval_nat_numeral)} \end{tabular}} \caption{Characteristic examples} \label{fig:Characteristic} @@ -3490,149 +3486,6 @@ auto simp add: injD[OF assms]) -subsubsection {* List summation: @{const listsum} and @{text"\"}*} - -lemma (in monoid_add) listsum_simps [simp]: - "listsum [] = 0" - "listsum (x # xs) = x + listsum xs" - by (simp_all add: listsum_def) - -lemma (in monoid_add) listsum_append [simp]: - "listsum (xs @ ys) = listsum xs + listsum ys" - by (induct xs) (simp_all add: add.assoc) - -lemma (in comm_monoid_add) listsum_rev [simp]: - "listsum (rev xs) = listsum xs" - by (simp add: listsum_def foldr_fold fold_rev fun_eq_iff ac_simps) - -lemma (in monoid_add) fold_plus_listsum_rev: - "fold plus xs = plus (listsum (rev xs))" -proof - fix x - have "fold plus xs x = fold plus xs (x + 0)" by simp - also have "\ = fold plus (x # xs) 0" by simp - also have "\ = foldr plus (rev xs @ [x]) 0" by (simp add: foldr_conv_fold) - also have "\ = listsum (rev xs @ [x])" by (simp add: listsum_def) - also have "\ = listsum (rev xs) + listsum [x]" by simp - finally show "fold plus xs x = listsum (rev xs) + x" by simp -qed - -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) -syntax (HTML output) - "_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)" - -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_eq_0_nat_iff_nat [simp]: - "listsum ns = (0::nat) \ (\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.. i = 0 ..< length xs. xs ! i)" - using interv_listsum_conv_setsum_set_nat [of "op ! xs" 0 "length xs"] by (simp add: map_nth) - - subsubsection {* @{const insert} *} lemma in_set_insert [simp]: @@ -4314,9 +4167,6 @@ lemma n_lists_Nil [simp]: "List.n_lists n [] = (if n = 0 then [[]] else [])" by (induct n) simp_all -lemma length_n_lists: "length (List.n_lists n xs) = length xs ^ n" - by (induct n) (auto simp add: length_concat o_def listsum_triv) - lemma length_n_lists_elem: "ys \ set (List.n_lists n xs) \ length ys = n" by (induct n arbitrary: ys) auto @@ -4335,28 +4185,6 @@ qed qed -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 - subsubsection {* @{const splice} *} @@ -6264,10 +6092,6 @@ "(\m\n\nat. P m) \ (\m \ {0..n}. P m)" by auto -lemma setsum_set_upt_conv_listsum_nat [code_unfold]: - "setsum f (set [m.. S \ P x)" @@ -6315,10 +6139,6 @@ lemmas atLeastAtMost_upto [code_unfold] = set_upto [symmetric] -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) - subsubsection {* Optimizing by rewriting *} @@ -6629,10 +6449,6 @@ "Pow (set (x # xs)) = (let A = Pow (set xs) in A \ insert x ` A)" by (simp_all add: Pow_insert Let_def) -lemma setsum_code [code]: - "setsum f (set xs) = listsum (map f (remdups xs))" -by (simp add: listsum_distinct_conv_setsum_set) - definition map_project :: "('a \ 'b option) \ 'a set \ 'b set" where "map_project f A = {b. \ a \ A. f a = Some b}" @@ -6883,13 +6699,6 @@ apply (erule_tac xs=x in list_all2_induct, simp, simp add: rel_fun_def) done -lemma listsum_transfer[transfer_rule]: - assumes [transfer_rule]: "A 0 0" - assumes [transfer_rule]: "(A ===> A ===> A) op + op +" - shows "(list_all2 A ===> A) listsum listsum" - unfolding listsum_def[abs_def] - by transfer_prover - lemma rtrancl_parametric [transfer_rule]: assumes [transfer_rule]: "bi_unique A" "bi_total A" shows "(rel_set (rel_prod A A) ===> rel_set (rel_prod A A)) rtrancl rtrancl" diff -r f54a8a4134d3 -r e7ebe5554281 src/HOL/Random.thy --- a/src/HOL/Random.thy Sun Aug 31 09:10:41 2014 +0200 +++ b/src/HOL/Random.thy Sun Aug 31 09:10:42 2014 +0200 @@ -4,7 +4,7 @@ header {* A HOL random engine *} theory Random -imports List +imports List Groups_List begin notation fcomp (infixl "\>" 60)