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