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