src/HOL/Groups_List.thy
 author nipkow Thu Nov 13 12:35:55 2014 +0100 (2014-11-13) changeset 58995 42ba2b5cffac parent 58889 5b7a9633cfa8 child 59728 0bb88aa34768 permissions -rw-r--r--
```     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
```
```   241 subsection {* Further facts about @{const List.n_lists} *}
```
```   242
```
```   243 lemma length_n_lists: "length (List.n_lists n xs) = length xs ^ n"
```
```   244   by (induct n) (auto simp add: comp_def length_concat listsum_triv)
```
```   245
```
```   246 lemma distinct_n_lists:
```
```   247   assumes "distinct xs"
```
```   248   shows "distinct (List.n_lists n xs)"
```
```   249 proof (rule card_distinct)
```
```   250   from assms have card_length: "card (set xs) = length xs" by (rule distinct_card)
```
```   251   have "card (set (List.n_lists n xs)) = card (set xs) ^ n"
```
```   252   proof (induct n)
```
```   253     case 0 then show ?case by simp
```
```   254   next
```
```   255     case (Suc n)
```
```   256     moreover have "card (\<Union>ys\<in>set (List.n_lists n xs). (\<lambda>y. y # ys) ` set xs)
```
```   257       = (\<Sum>ys\<in>set (List.n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))"
```
```   258       by (rule card_UN_disjoint) auto
```
```   259     moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)"
```
```   260       by (rule card_image) (simp add: inj_on_def)
```
```   261     ultimately show ?case by auto
```
```   262   qed
```
```   263   also have "\<dots> = length xs ^ n" by (simp add: card_length)
```
```   264   finally show "card (set (List.n_lists n xs)) = length (List.n_lists n xs)"
```
```   265     by (simp add: length_n_lists)
```
```   266 qed
```
```   267
```
```   268
```
```   269 subsection {* Tools setup *}
```
```   270
```
```   271 lemmas setsum_code = setsum.set_conv_list
```
```   272
```
```   273 lemma setsum_set_upto_conv_listsum_int [code_unfold]:
```
```   274   "setsum f (set [i..j::int]) = listsum (map f [i..j])"
```
```   275   by (simp add: interv_listsum_conv_setsum_set_int)
```
```   276
```
```   277 lemma setsum_set_upt_conv_listsum_nat [code_unfold]:
```
```   278   "setsum f (set [m..<n]) = listsum (map f [m..<n])"
```
```   279   by (simp add: interv_listsum_conv_setsum_set_nat)
```
```   280
```
```   281 context
```
```   282 begin
```
```   283
```
```   284 interpretation lifting_syntax .
```
```   285
```
```   286 lemma listsum_transfer[transfer_rule]:
```
```   287   assumes [transfer_rule]: "A 0 0"
```
```   288   assumes [transfer_rule]: "(A ===> A ===> A) op + op +"
```
```   289   shows "(list_all2 A ===> A) listsum listsum"
```
```   290   unfolding listsum.eq_foldr [abs_def]
```
```   291   by transfer_prover
```
```   292
```
```   293 end
```
```   294
```
```   295
```
```   296 subsection {* List product *}
```
```   297
```
```   298 context monoid_mult
```
```   299 begin
```
```   300
```
```   301 definition listprod :: "'a list \<Rightarrow> 'a"
```
```   302 where
```
```   303   "listprod  = monoid_list.F times 1"
```
```   304
```
```   305 sublocale listprod!: monoid_list times 1
```
```   306 where
```
```   307   "monoid_list.F times 1 = listprod"
```
```   308 proof -
```
```   309   show "monoid_list times 1" ..
```
```   310   then interpret listprod!: monoid_list times 1 .
```
```   311   from listprod_def show "monoid_list.F times 1 = listprod" by rule
```
```   312 qed
```
```   313
```
```   314 end
```
```   315
```
```   316 context comm_monoid_mult
```
```   317 begin
```
```   318
```
```   319 sublocale listprod!: comm_monoid_list times 1
```
```   320 where
```
```   321   "monoid_list.F times 1 = listprod"
```
```   322 proof -
```
```   323   show "comm_monoid_list times 1" ..
```
```   324   then interpret listprod!: comm_monoid_list times 1 .
```
```   325   from listprod_def show "monoid_list.F times 1 = listprod" by rule
```
```   326 qed
```
```   327
```
```   328 sublocale setprod!: comm_monoid_list_set times 1
```
```   329 where
```
```   330   "monoid_list.F times 1 = listprod"
```
```   331   and "comm_monoid_set.F times 1 = setprod"
```
```   332 proof -
```
```   333   show "comm_monoid_list_set times 1" ..
```
```   334   then interpret setprod!: comm_monoid_list_set times 1 .
```
```   335   from listprod_def show "monoid_list.F times 1 = listprod" by rule
```
```   336   from setprod_def show "comm_monoid_set.F times 1 = setprod" by rule
```
```   337 qed
```
```   338
```
```   339 end
```
```   340
```
```   341 text {* Some syntactic sugar: *}
```
```   342
```
```   343 syntax
```
```   344   "_listprod" :: "pttrn => 'a list => 'b => 'b"    ("(3PROD _<-_. _)" [0, 51, 10] 10)
```
```   345 syntax (xsymbols)
```
```   346   "_listprod" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Prod>_\<leftarrow>_. _)" [0, 51, 10] 10)
```
```   347 syntax (HTML output)
```
```   348   "_listprod" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Prod>_\<leftarrow>_. _)" [0, 51, 10] 10)
```
```   349
```
```   350 translations -- {* Beware of argument permutation! *}
```
```   351   "PROD x<-xs. b" == "CONST listprod (CONST map (%x. b) xs)"
```
```   352   "\<Prod>x\<leftarrow>xs. b" == "CONST listprod (CONST map (%x. b) xs)"
```
```   353
```
```   354 end
```