src/HOL/Groups_List.thy
author haftmann
Mon Jun 05 15:59:41 2017 +0200 (2017-06-05)
changeset 66010 2f7d39285a1a
parent 64272 f76b6dda2e56
child 66308 b6a0d95b94be
permissions -rw-r--r--
executable domain membership checks
     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 sum_list: monoid_list plus 0
    59 defines
    60   sum_list = sum_list.F ..
    61  
    62 end
    63 
    64 context comm_monoid_add
    65 begin
    66 
    67 sublocale sum_list: comm_monoid_list plus 0
    68 rewrites
    69   "monoid_list.F plus 0 = sum_list"
    70 proof -
    71   show "comm_monoid_list plus 0" ..
    72   then interpret sum_list: comm_monoid_list plus 0 .
    73   from sum_list_def show "monoid_list.F plus 0 = sum_list" by simp
    74 qed
    75 
    76 sublocale sum: comm_monoid_list_set plus 0
    77 rewrites
    78   "monoid_list.F plus 0 = sum_list"
    79   and "comm_monoid_set.F plus 0 = sum"
    80 proof -
    81   show "comm_monoid_list_set plus 0" ..
    82   then interpret sum: comm_monoid_list_set plus 0 .
    83   from sum_list_def show "monoid_list.F plus 0 = sum_list" by simp
    84   from sum_def show "comm_monoid_set.F plus 0 = sum" 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   "_sum_list" :: "pttrn => 'a list => 'b => 'b"    ("(3SUM _<-_. _)" [0, 51, 10] 10)
    92 syntax
    93   "_sum_list" :: "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 sum_list (CONST map (\<lambda>x. b) xs)"
    96 
    97 text \<open>TODO duplicates\<close>
    98 lemmas sum_list_simps = sum_list.Nil sum_list.Cons
    99 lemmas sum_list_append = sum_list.append
   100 lemmas sum_list_rev = sum_list.rev
   101 
   102 lemma (in monoid_add) fold_plus_sum_list_rev:
   103   "fold plus xs = plus (sum_list (rev xs))"
   104 proof
   105   fix x
   106   have "fold plus xs x = sum_list (rev xs @ [x])"
   107     by (simp add: foldr_conv_fold sum_list.eq_foldr)
   108   also have "\<dots> = sum_list (rev xs) + x"
   109     by simp
   110   finally show "fold plus xs x = sum_list (rev xs) + x"
   111     .
   112 qed
   113 
   114 lemma (in comm_monoid_add) sum_list_map_remove1:
   115   "x \<in> set xs \<Longrightarrow> sum_list (map f xs) = f x + sum_list (map f (remove1 x xs))"
   116   by (induct xs) (auto simp add: ac_simps)
   117 
   118 lemma (in monoid_add) size_list_conv_sum_list:
   119   "size_list f xs = sum_list (map f xs) + size xs"
   120   by (induct xs) auto
   121 
   122 lemma (in monoid_add) length_concat:
   123   "length (concat xss) = sum_list (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) sum_list_map_filter:
   133   assumes "\<And>x. x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> f x = 0"
   134   shows "sum_list (map f (filter P xs)) = sum_list (map f xs)"
   135   using assms by (induct xs) auto
   136 
   137 lemma (in comm_monoid_add) distinct_sum_list_conv_Sum:
   138   "distinct xs \<Longrightarrow> sum_list xs = Sum (set xs)"
   139   by (induct xs) simp_all
   140 
   141 lemma sum_list_upt[simp]:
   142   "m \<le> n \<Longrightarrow> sum_list [m..<n] = \<Sum> {m..<n}"
   143 by(simp add: distinct_sum_list_conv_Sum)
   144 
   145 lemma sum_list_eq_0_nat_iff_nat [simp]:
   146   "sum_list ns = (0::nat) \<longleftrightarrow> (\<forall>n \<in> set ns. n = 0)"
   147   by (induct ns) simp_all
   148 
   149 lemma member_le_sum_list_nat:
   150   "(n :: nat) \<in> set ns \<Longrightarrow> n \<le> sum_list ns"
   151   by (induct ns) auto
   152 
   153 lemma elem_le_sum_list_nat:
   154   "k < size ns \<Longrightarrow> ns ! k \<le> sum_list (ns::nat list)"
   155   by (rule member_le_sum_list_nat) simp
   156 
   157 lemma sum_list_update_nat:
   158   "k < size ns \<Longrightarrow> sum_list (ns[k := (n::nat)]) = sum_list ns + n - ns ! k"
   159 apply(induct ns arbitrary:k)
   160  apply (auto split:nat.split)
   161 apply(drule elem_le_sum_list_nat)
   162 apply arith
   163 done
   164 
   165 lemma (in monoid_add) sum_list_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) sum_list_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_sum_list_map:
   175   "- sum_list (map f xs) = sum_list (map (uminus \<circ> f) xs)"
   176   by (induct xs) simp_all
   177 
   178 lemma (in comm_monoid_add) sum_list_addf:
   179   "(\<Sum>x\<leftarrow>xs. f x + g x) = sum_list (map f xs) + sum_list (map g xs)"
   180   by (induct xs) (simp_all add: algebra_simps)
   181 
   182 lemma (in ab_group_add) sum_list_subtractf:
   183   "(\<Sum>x\<leftarrow>xs. f x - g x) = sum_list (map f xs) - sum_list (map g xs)"
   184   by (induct xs) (simp_all add: algebra_simps)
   185 
   186 lemma (in semiring_0) sum_list_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) sum_list_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) sum_list_abs:
   195   "\<bar>sum_list xs\<bar> \<le> sum_list (map abs xs)"
   196   by (induct xs) (simp_all add: order_trans [OF abs_triangle_ineq])
   197 
   198 lemma sum_list_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) sum_list_distinct_conv_sum_set:
   204   "distinct xs \<Longrightarrow> sum_list (map f xs) = sum f (set xs)"
   205   by (induct xs) simp_all
   206 
   207 lemma (in monoid_add) interv_sum_list_conv_sum_set_nat:
   208   "sum_list (map f [m..<n]) = sum f (set [m..<n])"
   209   by (simp add: sum_list_distinct_conv_sum_set)
   210 
   211 lemma (in monoid_add) interv_sum_list_conv_sum_set_int:
   212   "sum_list (map f [k..l]) = sum f (set [k..l])"
   213   by (simp add: sum_list_distinct_conv_sum_set)
   214 
   215 text \<open>General equivalence between @{const sum_list} and @{const sum}\<close>
   216 lemma (in monoid_add) sum_list_sum_nth:
   217   "sum_list xs = (\<Sum> i = 0 ..< length xs. xs ! i)"
   218   using interv_sum_list_conv_sum_set_nat [of "op ! xs" 0 "length xs"] by (simp add: map_nth)
   219 
   220 lemma sum_list_map_eq_sum_count:
   221   "sum_list (map f xs) = sum (\<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: sum.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 sum_list_map_eq_sum_count2:
   240 assumes "set xs \<subseteq> X" "finite X"
   241 shows "sum_list (map f xs) = sum (\<lambda>x. count_list xs x * f x) X"
   242 proof-
   243   let ?F = "\<lambda>x. count_list xs x * f x"
   244   have "sum ?F X = sum ?F (set xs \<union> (X - set xs))"
   245     using Un_absorb1[OF assms(1)] by(simp)
   246   also have "\<dots> = sum ?F (set xs)"
   247     using assms(2)
   248     by(simp add: sum.union_disjoint[OF _ _ Diff_disjoint] del: Un_Diff_cancel)
   249   finally show ?thesis by(simp add:sum_list_map_eq_sum_count)
   250 qed
   251 
   252 lemma sum_list_nonneg: 
   253     "(\<And>x. x \<in> set xs \<Longrightarrow> (x :: 'a :: ordered_comm_monoid_add) \<ge> 0) \<Longrightarrow> sum_list xs \<ge> 0"
   254   by (induction xs) simp_all
   255 
   256 lemma (in monoid_add) sum_list_map_filter':
   257   "sum_list (map f (filter P xs)) = sum_list (map (\<lambda>x. if P x then f x else 0) xs)"
   258   by (induction xs) simp_all
   259 
   260 lemma sum_list_cong [fundef_cong]:
   261   assumes "xs = ys"
   262   assumes "\<And>x. x \<in> set xs \<Longrightarrow> f x = g x"
   263   shows    "sum_list (map f xs) = sum_list (map g ys)"
   264 proof -
   265   from assms(2) have "sum_list (map f xs) = sum_list (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 sum_list_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 sum_code = sum.set_conv_list
   302 
   303 lemma sum_set_upto_conv_sum_list_int [code_unfold]:
   304   "sum f (set [i..j::int]) = sum_list (map f [i..j])"
   305   by (simp add: interv_sum_list_conv_sum_set_int)
   306 
   307 lemma sum_set_upt_conv_sum_list_nat [code_unfold]:
   308   "sum f (set [m..<n]) = sum_list (map f [m..<n])"
   309   by (simp add: interv_sum_list_conv_sum_set_nat)
   310 
   311 lemma sum_list_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) sum_list sum_list"
   316   unfolding sum_list.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 prod_list: monoid_list times 1
   326 defines
   327   prod_list = prod_list.F ..
   328 
   329 end
   330 
   331 context comm_monoid_mult
   332 begin
   333 
   334 sublocale prod_list: comm_monoid_list times 1
   335 rewrites
   336   "monoid_list.F times 1 = prod_list"
   337 proof -
   338   show "comm_monoid_list times 1" ..
   339   then interpret prod_list: comm_monoid_list times 1 .
   340   from prod_list_def show "monoid_list.F times 1 = prod_list" by simp
   341 qed
   342 
   343 sublocale prod: comm_monoid_list_set times 1
   344 rewrites
   345   "monoid_list.F times 1 = prod_list"
   346   and "comm_monoid_set.F times 1 = prod"
   347 proof -
   348   show "comm_monoid_list_set times 1" ..
   349   then interpret prod: comm_monoid_list_set times 1 .
   350   from prod_list_def show "monoid_list.F times 1 = prod_list" by simp
   351   from prod_def show "comm_monoid_set.F times 1 = prod" by (auto intro: sym)
   352 qed
   353 
   354 end
   355 
   356 lemma prod_list_cong [fundef_cong]:
   357   assumes "xs = ys"
   358   assumes "\<And>x. x \<in> set xs \<Longrightarrow> f x = g x"
   359   shows    "prod_list (map f xs) = prod_list (map g ys)"
   360 proof -
   361   from assms(2) have "prod_list (map f xs) = prod_list (map g xs)"
   362     by (induction xs) simp_all
   363   with assms(1) show ?thesis by simp
   364 qed
   365 
   366 lemma prod_list_zero_iff: 
   367   "prod_list 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   "_prod_list" :: "pttrn => 'a list => 'b => 'b"    ("(3PROD _<-_. _)" [0, 51, 10] 10)
   374 syntax
   375   "_prod_list" :: "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 prod_list (CONST map (\<lambda>x. b) xs)"
   378 
   379 end