src/HOL/Groups_List.thy
 author wenzelm Wed Jun 22 10:09:20 2016 +0200 (2016-06-22) changeset 63343 fb5d8a50c641 parent 63317 ca187a9f66da child 63882 018998c00003 permissions -rw-r--r--
bundle lifting_syntax;
```     1 (* Author: Tobias Nipkow, TU Muenchen *)
```
```     2
```
```     3 section \<open>Sum and product over lists\<close>
```
```     4
```
```     5 theory Groups_List
```
```     6 imports List
```
```     7 begin
```
```     8
```
```     9 locale monoid_list = monoid
```
```    10 begin
```
```    11
```
```    12 definition F :: "'a list \<Rightarrow> 'a"
```
```    13 where
```
```    14   eq_foldr [code]: "F xs = foldr f xs \<^bold>1"
```
```    15
```
```    16 lemma Nil [simp]:
```
```    17   "F [] = \<^bold>1"
```
```    18   by (simp add: eq_foldr)
```
```    19
```
```    20 lemma Cons [simp]:
```
```    21   "F (x # xs) = x \<^bold>* F xs"
```
```    22   by (simp add: eq_foldr)
```
```    23
```
```    24 lemma append [simp]:
```
```    25   "F (xs @ ys) = F xs \<^bold>* F ys"
```
```    26   by (induct xs) (simp_all add: assoc)
```
```    27
```
```    28 end
```
```    29
```
```    30 locale comm_monoid_list = comm_monoid + monoid_list
```
```    31 begin
```
```    32
```
```    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)
```
```    36
```
```    37 end
```
```    38
```
```    39 locale comm_monoid_list_set = list: comm_monoid_list + set: comm_monoid_set
```
```    40 begin
```
```    41
```
```    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
```
```    45
```
```    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
```
```    53 subsection \<open>List summation\<close>
```
```    54
```
```    55 context monoid_add
```
```    56 begin
```
```    57
```
```    58 sublocale listsum: monoid_list plus 0
```
```    59 defines
```
```    60   listsum = listsum.F ..
```
```    61
```
```    62 end
```
```    63
```
```    64 context comm_monoid_add
```
```    65 begin
```
```    66
```
```    67 sublocale listsum: comm_monoid_list plus 0
```
```    68 rewrites
```
```    69   "monoid_list.F plus 0 = listsum"
```
```    70 proof -
```
```    71   show "comm_monoid_list plus 0" ..
```
```    72   then interpret listsum: comm_monoid_list plus 0 .
```
```    73   from listsum_def show "monoid_list.F plus 0 = listsum" by simp
```
```    74 qed
```
```    75
```
```    76 sublocale setsum: comm_monoid_list_set plus 0
```
```    77 rewrites
```
```    78   "monoid_list.F plus 0 = listsum"
```
```    79   and "comm_monoid_set.F plus 0 = setsum"
```
```    80 proof -
```
```    81   show "comm_monoid_list_set plus 0" ..
```
```    82   then interpret setsum: comm_monoid_list_set plus 0 .
```
```    83   from listsum_def show "monoid_list.F plus 0 = listsum" by simp
```
```    84   from setsum_def show "comm_monoid_set.F plus 0 = setsum" by (auto intro: sym)
```
```    85 qed
```
```    86
```
```    87 end
```
```    88
```
```    89 text \<open>Some syntactic sugar for summing a function over a list:\<close>
```
```    90 syntax (ASCII)
```
```    91   "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3SUM _<-_. _)" [0, 51, 10] 10)
```
```    92 syntax
```
```    93   "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
```
```    94 translations \<comment> \<open>Beware of argument permutation!\<close>
```
```    95   "\<Sum>x\<leftarrow>xs. b" == "CONST listsum (CONST map (\<lambda>x. b) xs)"
```
```    96
```
```    97 text \<open>TODO duplicates\<close>
```
```    98 lemmas listsum_simps = listsum.Nil listsum.Cons
```
```    99 lemmas listsum_append = listsum.append
```
```   100 lemmas listsum_rev = listsum.rev
```
```   101
```
```   102 lemma (in monoid_add) fold_plus_listsum_rev:
```
```   103   "fold plus xs = plus (listsum (rev xs))"
```
```   104 proof
```
```   105   fix x
```
```   106   have "fold plus xs x = listsum (rev xs @ [x])"
```
```   107     by (simp add: foldr_conv_fold listsum.eq_foldr)
```
```   108   also have "\<dots> = listsum (rev xs) + x"
```
```   109     by simp
```
```   110   finally show "fold plus xs x = listsum (rev xs) + x"
```
```   111     .
```
```   112 qed
```
```   113
```
```   114 lemma (in comm_monoid_add) listsum_map_remove1:
```
```   115   "x \<in> set xs \<Longrightarrow> listsum (map f xs) = f x + listsum (map f (remove1 x xs))"
```
```   116   by (induct xs) (auto simp add: ac_simps)
```
```   117
```
```   118 lemma (in monoid_add) size_list_conv_listsum:
```
```   119   "size_list f xs = listsum (map f xs) + size xs"
```
```   120   by (induct xs) auto
```
```   121
```
```   122 lemma (in monoid_add) length_concat:
```
```   123   "length (concat xss) = listsum (map length xss)"
```
```   124   by (induct xss) simp_all
```
```   125
```
```   126 lemma (in monoid_add) length_product_lists:
```
```   127   "length (product_lists xss) = foldr op * (map length xss) 1"
```
```   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
```
```   132 lemma (in monoid_add) listsum_map_filter:
```
```   133   assumes "\<And>x. x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> f x = 0"
```
```   134   shows "listsum (map f (filter P xs)) = listsum (map f xs)"
```
```   135   using assms by (induct xs) auto
```
```   136
```
```   137 lemma (in comm_monoid_add) distinct_listsum_conv_Setsum:
```
```   138   "distinct xs \<Longrightarrow> listsum xs = Setsum (set xs)"
```
```   139   by (induct xs) simp_all
```
```   140
```
```   141 lemma listsum_upt[simp]:
```
```   142   "m \<le> n \<Longrightarrow> listsum [m..<n] = \<Sum> {m..<n}"
```
```   143 by(simp add: distinct_listsum_conv_Setsum)
```
```   144
```
```   145 lemma listsum_eq_0_nat_iff_nat [simp]:
```
```   146   "listsum ns = (0::nat) \<longleftrightarrow> (\<forall>n \<in> set ns. n = 0)"
```
```   147   by (induct ns) simp_all
```
```   148
```
```   149 lemma member_le_listsum_nat:
```
```   150   "(n :: nat) \<in> set ns \<Longrightarrow> n \<le> listsum ns"
```
```   151   by (induct ns) auto
```
```   152
```
```   153 lemma elem_le_listsum_nat:
```
```   154   "k < size ns \<Longrightarrow> ns ! k \<le> listsum (ns::nat list)"
```
```   155   by (rule member_le_listsum_nat) simp
```
```   156
```
```   157 lemma listsum_update_nat:
```
```   158   "k < size ns \<Longrightarrow> listsum (ns[k := (n::nat)]) = listsum ns + n - ns ! k"
```
```   159 apply(induct ns arbitrary:k)
```
```   160  apply (auto split:nat.split)
```
```   161 apply(drule elem_le_listsum_nat)
```
```   162 apply arith
```
```   163 done
```
```   164
```
```   165 lemma (in monoid_add) listsum_triv:
```
```   166   "(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r"
```
```   167   by (induct xs) (simp_all add: distrib_right)
```
```   168
```
```   169 lemma (in monoid_add) listsum_0 [simp]:
```
```   170   "(\<Sum>x\<leftarrow>xs. 0) = 0"
```
```   171   by (induct xs) (simp_all add: distrib_right)
```
```   172
```
```   173 text\<open>For non-Abelian groups \<open>xs\<close> needs to be reversed on one side:\<close>
```
```   174 lemma (in ab_group_add) uminus_listsum_map:
```
```   175   "- listsum (map f xs) = listsum (map (uminus \<circ> f) xs)"
```
```   176   by (induct xs) simp_all
```
```   177
```
```   178 lemma (in comm_monoid_add) listsum_addf:
```
```   179   "(\<Sum>x\<leftarrow>xs. f x + g x) = listsum (map f xs) + listsum (map g xs)"
```
```   180   by (induct xs) (simp_all add: algebra_simps)
```
```   181
```
```   182 lemma (in ab_group_add) listsum_subtractf:
```
```   183   "(\<Sum>x\<leftarrow>xs. f x - g x) = listsum (map f xs) - listsum (map g xs)"
```
```   184   by (induct xs) (simp_all add: algebra_simps)
```
```   185
```
```   186 lemma (in semiring_0) listsum_const_mult:
```
```   187   "(\<Sum>x\<leftarrow>xs. c * f x) = c * (\<Sum>x\<leftarrow>xs. f x)"
```
```   188   by (induct xs) (simp_all add: algebra_simps)
```
```   189
```
```   190 lemma (in semiring_0) listsum_mult_const:
```
```   191   "(\<Sum>x\<leftarrow>xs. f x * c) = (\<Sum>x\<leftarrow>xs. f x) * c"
```
```   192   by (induct xs) (simp_all add: algebra_simps)
```
```   193
```
```   194 lemma (in ordered_ab_group_add_abs) listsum_abs:
```
```   195   "\<bar>listsum xs\<bar> \<le> listsum (map abs xs)"
```
```   196   by (induct xs) (simp_all add: order_trans [OF abs_triangle_ineq])
```
```   197
```
```   198 lemma listsum_mono:
```
```   199   fixes f g :: "'a \<Rightarrow> 'b::{monoid_add, ordered_ab_semigroup_add}"
```
```   200   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)"
```
```   201   by (induct xs) (simp, simp add: add_mono)
```
```   202
```
```   203 lemma (in monoid_add) listsum_distinct_conv_setsum_set:
```
```   204   "distinct xs \<Longrightarrow> listsum (map f xs) = setsum f (set xs)"
```
```   205   by (induct xs) simp_all
```
```   206
```
```   207 lemma (in monoid_add) interv_listsum_conv_setsum_set_nat:
```
```   208   "listsum (map f [m..<n]) = setsum f (set [m..<n])"
```
```   209   by (simp add: listsum_distinct_conv_setsum_set)
```
```   210
```
```   211 lemma (in monoid_add) interv_listsum_conv_setsum_set_int:
```
```   212   "listsum (map f [k..l]) = setsum f (set [k..l])"
```
```   213   by (simp add: listsum_distinct_conv_setsum_set)
```
```   214
```
```   215 text \<open>General equivalence between @{const listsum} and @{const setsum}\<close>
```
```   216 lemma (in monoid_add) listsum_setsum_nth:
```
```   217   "listsum xs = (\<Sum> i = 0 ..< length xs. xs ! i)"
```
```   218   using interv_listsum_conv_setsum_set_nat [of "op ! xs" 0 "length xs"] by (simp add: map_nth)
```
```   219
```
```   220 lemma listsum_map_eq_setsum_count:
```
```   221   "listsum (map f xs) = setsum (\<lambda>x. count_list xs x * f x) (set xs)"
```
```   222 proof(induction xs)
```
```   223   case (Cons x xs)
```
```   224   show ?case (is "?l = ?r")
```
```   225   proof cases
```
```   226     assume "x \<in> set xs"
```
```   227     have "?l = f x + (\<Sum>x\<in>set xs. count_list xs x * f x)" by (simp add: Cons.IH)
```
```   228     also have "set xs = insert x (set xs - {x})" using \<open>x \<in> set xs\<close>by blast
```
```   229     also have "f x + (\<Sum>x\<in>insert x (set xs - {x}). count_list xs x * f x) = ?r"
```
```   230       by (simp add: setsum.insert_remove eq_commute)
```
```   231     finally show ?thesis .
```
```   232   next
```
```   233     assume "x \<notin> set xs"
```
```   234     hence "\<And>xa. xa \<in> set xs \<Longrightarrow> x \<noteq> xa" by blast
```
```   235     thus ?thesis by (simp add: Cons.IH \<open>x \<notin> set xs\<close>)
```
```   236   qed
```
```   237 qed simp
```
```   238
```
```   239 lemma listsum_map_eq_setsum_count2:
```
```   240 assumes "set xs \<subseteq> X" "finite X"
```
```   241 shows "listsum (map f xs) = setsum (\<lambda>x. count_list xs x * f x) X"
```
```   242 proof-
```
```   243   let ?F = "\<lambda>x. count_list xs x * f x"
```
```   244   have "setsum ?F X = setsum ?F (set xs \<union> (X - set xs))"
```
```   245     using Un_absorb1[OF assms(1)] by(simp)
```
```   246   also have "\<dots> = setsum ?F (set xs)"
```
```   247     using assms(2)
```
```   248     by(simp add: setsum.union_disjoint[OF _ _ Diff_disjoint] del: Un_Diff_cancel)
```
```   249   finally show ?thesis by(simp add:listsum_map_eq_setsum_count)
```
```   250 qed
```
```   251
```
```   252 lemma listsum_nonneg:
```
```   253     "(\<And>x. x \<in> set xs \<Longrightarrow> (x :: 'a :: ordered_comm_monoid_add) \<ge> 0) \<Longrightarrow> listsum xs \<ge> 0"
```
```   254   by (induction xs) simp_all
```
```   255
```
```   256 lemma (in monoid_add) listsum_map_filter':
```
```   257   "listsum (map f (filter P xs)) = listsum (map (\<lambda>x. if P x then f x else 0) xs)"
```
```   258   by (induction xs) simp_all
```
```   259
```
```   260 lemma listsum_cong [fundef_cong]:
```
```   261   assumes "xs = ys"
```
```   262   assumes "\<And>x. x \<in> set xs \<Longrightarrow> f x = g x"
```
```   263   shows    "listsum (map f xs) = listsum (map g ys)"
```
```   264 proof -
```
```   265   from assms(2) have "listsum (map f xs) = listsum (map g xs)"
```
```   266     by (induction xs) simp_all
```
```   267   with assms(1) show ?thesis by simp
```
```   268 qed
```
```   269
```
```   270
```
```   271 subsection \<open>Further facts about @{const List.n_lists}\<close>
```
```   272
```
```   273 lemma length_n_lists: "length (List.n_lists n xs) = length xs ^ n"
```
```   274   by (induct n) (auto simp add: comp_def length_concat listsum_triv)
```
```   275
```
```   276 lemma distinct_n_lists:
```
```   277   assumes "distinct xs"
```
```   278   shows "distinct (List.n_lists n xs)"
```
```   279 proof (rule card_distinct)
```
```   280   from assms have card_length: "card (set xs) = length xs" by (rule distinct_card)
```
```   281   have "card (set (List.n_lists n xs)) = card (set xs) ^ n"
```
```   282   proof (induct n)
```
```   283     case 0 then show ?case by simp
```
```   284   next
```
```   285     case (Suc n)
```
```   286     moreover have "card (\<Union>ys\<in>set (List.n_lists n xs). (\<lambda>y. y # ys) ` set xs)
```
```   287       = (\<Sum>ys\<in>set (List.n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))"
```
```   288       by (rule card_UN_disjoint) auto
```
```   289     moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)"
```
```   290       by (rule card_image) (simp add: inj_on_def)
```
```   291     ultimately show ?case by auto
```
```   292   qed
```
```   293   also have "\<dots> = length xs ^ n" by (simp add: card_length)
```
```   294   finally show "card (set (List.n_lists n xs)) = length (List.n_lists n xs)"
```
```   295     by (simp add: length_n_lists)
```
```   296 qed
```
```   297
```
```   298
```
```   299 subsection \<open>Tools setup\<close>
```
```   300
```
```   301 lemmas setsum_code = setsum.set_conv_list
```
```   302
```
```   303 lemma setsum_set_upto_conv_listsum_int [code_unfold]:
```
```   304   "setsum f (set [i..j::int]) = listsum (map f [i..j])"
```
```   305   by (simp add: interv_listsum_conv_setsum_set_int)
```
```   306
```
```   307 lemma setsum_set_upt_conv_listsum_nat [code_unfold]:
```
```   308   "setsum f (set [m..<n]) = listsum (map f [m..<n])"
```
```   309   by (simp add: interv_listsum_conv_setsum_set_nat)
```
```   310
```
```   311 lemma listsum_transfer[transfer_rule]:
```
```   312   includes lifting_syntax
```
```   313   assumes [transfer_rule]: "A 0 0"
```
```   314   assumes [transfer_rule]: "(A ===> A ===> A) op + op +"
```
```   315   shows "(list_all2 A ===> A) listsum listsum"
```
```   316   unfolding listsum.eq_foldr [abs_def]
```
```   317   by transfer_prover
```
```   318
```
```   319
```
```   320 subsection \<open>List product\<close>
```
```   321
```
```   322 context monoid_mult
```
```   323 begin
```
```   324
```
```   325 sublocale listprod: monoid_list times 1
```
```   326 defines
```
```   327   listprod = listprod.F ..
```
```   328
```
```   329 end
```
```   330
```
```   331 context comm_monoid_mult
```
```   332 begin
```
```   333
```
```   334 sublocale listprod: comm_monoid_list times 1
```
```   335 rewrites
```
```   336   "monoid_list.F times 1 = listprod"
```
```   337 proof -
```
```   338   show "comm_monoid_list times 1" ..
```
```   339   then interpret listprod: comm_monoid_list times 1 .
```
```   340   from listprod_def show "monoid_list.F times 1 = listprod" by simp
```
```   341 qed
```
```   342
```
```   343 sublocale setprod: comm_monoid_list_set times 1
```
```   344 rewrites
```
```   345   "monoid_list.F times 1 = listprod"
```
```   346   and "comm_monoid_set.F times 1 = setprod"
```
```   347 proof -
```
```   348   show "comm_monoid_list_set times 1" ..
```
```   349   then interpret setprod: comm_monoid_list_set times 1 .
```
```   350   from listprod_def show "monoid_list.F times 1 = listprod" by simp
```
```   351   from setprod_def show "comm_monoid_set.F times 1 = setprod" by (auto intro: sym)
```
```   352 qed
```
```   353
```
```   354 end
```
```   355
```
```   356 lemma listprod_cong [fundef_cong]:
```
```   357   assumes "xs = ys"
```
```   358   assumes "\<And>x. x \<in> set xs \<Longrightarrow> f x = g x"
```
```   359   shows    "listprod (map f xs) = listprod (map g ys)"
```
```   360 proof -
```
```   361   from assms(2) have "listprod (map f xs) = listprod (map g xs)"
```
```   362     by (induction xs) simp_all
```
```   363   with assms(1) show ?thesis by simp
```
```   364 qed
```
```   365
```
```   366 lemma listprod_zero_iff:
```
```   367   "listprod xs = 0 \<longleftrightarrow> (0 :: 'a :: {semiring_no_zero_divisors, semiring_1}) \<in> set xs"
```
```   368   by (induction xs) simp_all
```
```   369
```
```   370 text \<open>Some syntactic sugar:\<close>
```
```   371
```
```   372 syntax (ASCII)
```
```   373   "_listprod" :: "pttrn => 'a list => 'b => 'b"    ("(3PROD _<-_. _)" [0, 51, 10] 10)
```
```   374 syntax
```
```   375   "_listprod" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Prod>_\<leftarrow>_. _)" [0, 51, 10] 10)
```
```   376 translations \<comment> \<open>Beware of argument permutation!\<close>
```
```   377   "\<Prod>x\<leftarrow>xs. b" \<rightleftharpoons> "CONST listprod (CONST map (\<lambda>x. b) xs)"
```
```   378
```
```   379 end
```