src/HOL/Groups_List.thy
author blanchet
Thu Sep 11 18:54:36 2014 +0200 (2014-09-11)
changeset 58306 117ba6cbe414
parent 58152 6fe60a9a5bad
child 58320 351810c45a48
permissions -rw-r--r--
renamed 'rep_datatype' to 'old_rep_datatype' (HOL)
haftmann@58101
     1
haftmann@58101
     2
(* Author: Tobias Nipkow, TU Muenchen *)
haftmann@58101
     3
haftmann@58101
     4
header {* Summation over lists *}
haftmann@58101
     5
haftmann@58101
     6
theory Groups_List
haftmann@58101
     7
imports List
haftmann@58101
     8
begin
haftmann@58101
     9
haftmann@58101
    10
definition (in monoid_add) listsum :: "'a list \<Rightarrow> 'a" where
blanchet@58152
    11
  "listsum xs = foldr plus xs 0"
haftmann@58101
    12
haftmann@58101
    13
subsubsection {* List summation: @{const listsum} and @{text"\<Sum>"}*}
haftmann@58101
    14
haftmann@58101
    15
lemma (in monoid_add) listsum_simps [simp]:
haftmann@58101
    16
  "listsum [] = 0"
haftmann@58101
    17
  "listsum (x # xs) = x + listsum xs"
haftmann@58101
    18
  by (simp_all add: listsum_def)
haftmann@58101
    19
haftmann@58101
    20
lemma (in monoid_add) listsum_append [simp]:
haftmann@58101
    21
  "listsum (xs @ ys) = listsum xs + listsum ys"
haftmann@58101
    22
  by (induct xs) (simp_all add: add.assoc)
haftmann@58101
    23
haftmann@58101
    24
lemma (in comm_monoid_add) listsum_rev [simp]:
haftmann@58101
    25
  "listsum (rev xs) = listsum xs"
haftmann@58101
    26
  by (simp add: listsum_def foldr_fold fold_rev fun_eq_iff add_ac)
haftmann@58101
    27
haftmann@58101
    28
lemma (in monoid_add) fold_plus_listsum_rev:
haftmann@58101
    29
  "fold plus xs = plus (listsum (rev xs))"
haftmann@58101
    30
proof
haftmann@58101
    31
  fix x
haftmann@58101
    32
  have "fold plus xs x = fold plus xs (x + 0)" by simp
haftmann@58101
    33
  also have "\<dots> = fold plus (x # xs) 0" by simp
haftmann@58101
    34
  also have "\<dots> = foldr plus (rev xs @ [x]) 0" by (simp add: foldr_conv_fold)
haftmann@58101
    35
  also have "\<dots> = listsum (rev xs @ [x])" by (simp add: listsum_def)
haftmann@58101
    36
  also have "\<dots> = listsum (rev xs) + listsum [x]" by simp
haftmann@58101
    37
  finally show "fold plus xs x = listsum (rev xs) + x" by simp
haftmann@58101
    38
qed
haftmann@58101
    39
haftmann@58101
    40
text{* Some syntactic sugar for summing a function over a list: *}
haftmann@58101
    41
haftmann@58101
    42
syntax
haftmann@58101
    43
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3SUM _<-_. _)" [0, 51, 10] 10)
haftmann@58101
    44
syntax (xsymbols)
haftmann@58101
    45
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
haftmann@58101
    46
syntax (HTML output)
haftmann@58101
    47
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
haftmann@58101
    48
haftmann@58101
    49
translations -- {* Beware of argument permutation! *}
haftmann@58101
    50
  "SUM x<-xs. b" == "CONST listsum (CONST map (%x. b) xs)"
haftmann@58101
    51
  "\<Sum>x\<leftarrow>xs. b" == "CONST listsum (CONST map (%x. b) xs)"
haftmann@58101
    52
haftmann@58101
    53
lemma (in comm_monoid_add) listsum_map_remove1:
haftmann@58101
    54
  "x \<in> set xs \<Longrightarrow> listsum (map f xs) = f x + listsum (map f (remove1 x xs))"
haftmann@58101
    55
  by (induct xs) (auto simp add: ac_simps)
haftmann@58101
    56
haftmann@58101
    57
lemma (in monoid_add) size_list_conv_listsum:
haftmann@58101
    58
  "size_list f xs = listsum (map f xs) + size xs"
haftmann@58101
    59
  by (induct xs) auto
haftmann@58101
    60
haftmann@58101
    61
lemma (in monoid_add) length_concat:
haftmann@58101
    62
  "length (concat xss) = listsum (map length xss)"
haftmann@58101
    63
  by (induct xss) simp_all
haftmann@58101
    64
haftmann@58101
    65
lemma (in monoid_add) length_product_lists:
haftmann@58101
    66
  "length (product_lists xss) = foldr op * (map length xss) 1"
haftmann@58101
    67
proof (induct xss)
haftmann@58101
    68
  case (Cons xs xss) then show ?case by (induct xs) (auto simp: length_concat o_def)
haftmann@58101
    69
qed simp
haftmann@58101
    70
haftmann@58101
    71
lemma (in monoid_add) listsum_map_filter:
haftmann@58101
    72
  assumes "\<And>x. x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> f x = 0"
haftmann@58101
    73
  shows "listsum (map f (filter P xs)) = listsum (map f xs)"
haftmann@58101
    74
  using assms by (induct xs) auto
haftmann@58101
    75
haftmann@58101
    76
lemma (in comm_monoid_add) distinct_listsum_conv_Setsum:
haftmann@58101
    77
  "distinct xs \<Longrightarrow> listsum xs = Setsum (set xs)"
haftmann@58101
    78
  by (induct xs) simp_all
haftmann@58101
    79
haftmann@58101
    80
lemma listsum_eq_0_nat_iff_nat [simp]:
haftmann@58101
    81
  "listsum ns = (0::nat) \<longleftrightarrow> (\<forall>n \<in> set ns. n = 0)"
haftmann@58101
    82
  by (induct ns) simp_all
haftmann@58101
    83
haftmann@58101
    84
lemma member_le_listsum_nat:
haftmann@58101
    85
  "(n :: nat) \<in> set ns \<Longrightarrow> n \<le> listsum ns"
haftmann@58101
    86
  by (induct ns) auto
haftmann@58101
    87
haftmann@58101
    88
lemma elem_le_listsum_nat:
haftmann@58101
    89
  "k < size ns \<Longrightarrow> ns ! k \<le> listsum (ns::nat list)"
haftmann@58101
    90
  by (rule member_le_listsum_nat) simp
haftmann@58101
    91
haftmann@58101
    92
lemma listsum_update_nat:
haftmann@58101
    93
  "k < size ns \<Longrightarrow> listsum (ns[k := (n::nat)]) = listsum ns + n - ns ! k"
haftmann@58101
    94
apply(induct ns arbitrary:k)
haftmann@58101
    95
 apply (auto split:nat.split)
haftmann@58101
    96
apply(drule elem_le_listsum_nat)
haftmann@58101
    97
apply arith
haftmann@58101
    98
done
haftmann@58101
    99
haftmann@58101
   100
lemma (in monoid_add) listsum_triv:
haftmann@58101
   101
  "(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r"
haftmann@58101
   102
  by (induct xs) (simp_all add: distrib_right)
haftmann@58101
   103
haftmann@58101
   104
lemma (in monoid_add) listsum_0 [simp]:
haftmann@58101
   105
  "(\<Sum>x\<leftarrow>xs. 0) = 0"
haftmann@58101
   106
  by (induct xs) (simp_all add: distrib_right)
haftmann@58101
   107
haftmann@58101
   108
text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *}
haftmann@58101
   109
lemma (in ab_group_add) uminus_listsum_map:
haftmann@58101
   110
  "- listsum (map f xs) = listsum (map (uminus \<circ> f) xs)"
haftmann@58101
   111
  by (induct xs) simp_all
haftmann@58101
   112
haftmann@58101
   113
lemma (in comm_monoid_add) listsum_addf:
haftmann@58101
   114
  "(\<Sum>x\<leftarrow>xs. f x + g x) = listsum (map f xs) + listsum (map g xs)"
haftmann@58101
   115
  by (induct xs) (simp_all add: algebra_simps)
haftmann@58101
   116
haftmann@58101
   117
lemma (in ab_group_add) listsum_subtractf:
haftmann@58101
   118
  "(\<Sum>x\<leftarrow>xs. f x - g x) = listsum (map f xs) - listsum (map g xs)"
haftmann@58101
   119
  by (induct xs) (simp_all add: algebra_simps)
haftmann@58101
   120
haftmann@58101
   121
lemma (in semiring_0) listsum_const_mult:
haftmann@58101
   122
  "(\<Sum>x\<leftarrow>xs. c * f x) = c * (\<Sum>x\<leftarrow>xs. f x)"
haftmann@58101
   123
  by (induct xs) (simp_all add: algebra_simps)
haftmann@58101
   124
haftmann@58101
   125
lemma (in semiring_0) listsum_mult_const:
haftmann@58101
   126
  "(\<Sum>x\<leftarrow>xs. f x * c) = (\<Sum>x\<leftarrow>xs. f x) * c"
haftmann@58101
   127
  by (induct xs) (simp_all add: algebra_simps)
haftmann@58101
   128
haftmann@58101
   129
lemma (in ordered_ab_group_add_abs) listsum_abs:
haftmann@58101
   130
  "\<bar>listsum xs\<bar> \<le> listsum (map abs xs)"
haftmann@58101
   131
  by (induct xs) (simp_all add: order_trans [OF abs_triangle_ineq])
haftmann@58101
   132
haftmann@58101
   133
lemma listsum_mono:
haftmann@58101
   134
  fixes f g :: "'a \<Rightarrow> 'b::{monoid_add, ordered_ab_semigroup_add}"
haftmann@58101
   135
  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
   136
  by (induct xs) (simp, simp add: add_mono)
haftmann@58101
   137
haftmann@58101
   138
lemma (in monoid_add) listsum_distinct_conv_setsum_set:
haftmann@58101
   139
  "distinct xs \<Longrightarrow> listsum (map f xs) = setsum f (set xs)"
haftmann@58101
   140
  by (induct xs) simp_all
haftmann@58101
   141
haftmann@58101
   142
lemma (in monoid_add) interv_listsum_conv_setsum_set_nat:
haftmann@58101
   143
  "listsum (map f [m..<n]) = setsum f (set [m..<n])"
haftmann@58101
   144
  by (simp add: listsum_distinct_conv_setsum_set)
haftmann@58101
   145
haftmann@58101
   146
lemma (in monoid_add) interv_listsum_conv_setsum_set_int:
haftmann@58101
   147
  "listsum (map f [k..l]) = setsum f (set [k..l])"
haftmann@58101
   148
  by (simp add: listsum_distinct_conv_setsum_set)
haftmann@58101
   149
haftmann@58101
   150
text {* General equivalence between @{const listsum} and @{const setsum} *}
haftmann@58101
   151
lemma (in monoid_add) listsum_setsum_nth:
haftmann@58101
   152
  "listsum xs = (\<Sum> i = 0 ..< length xs. xs ! i)"
haftmann@58101
   153
  using interv_listsum_conv_setsum_set_nat [of "op ! xs" 0 "length xs"] by (simp add: map_nth)
haftmann@58101
   154
haftmann@58101
   155
haftmann@58101
   156
subsection {* Further facts about @{const List.n_lists} *}
haftmann@58101
   157
haftmann@58101
   158
lemma length_n_lists: "length (List.n_lists n xs) = length xs ^ n"
haftmann@58101
   159
  by (induct n) (auto simp add: comp_def length_concat listsum_triv)
haftmann@58101
   160
haftmann@58101
   161
lemma distinct_n_lists:
haftmann@58101
   162
  assumes "distinct xs"
haftmann@58101
   163
  shows "distinct (List.n_lists n xs)"
haftmann@58101
   164
proof (rule card_distinct)
haftmann@58101
   165
  from assms have card_length: "card (set xs) = length xs" by (rule distinct_card)
haftmann@58101
   166
  have "card (set (List.n_lists n xs)) = card (set xs) ^ n"
haftmann@58101
   167
  proof (induct n)
haftmann@58101
   168
    case 0 then show ?case by simp
haftmann@58101
   169
  next
haftmann@58101
   170
    case (Suc n)
haftmann@58101
   171
    moreover have "card (\<Union>ys\<in>set (List.n_lists n xs). (\<lambda>y. y # ys) ` set xs)
haftmann@58101
   172
      = (\<Sum>ys\<in>set (List.n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))"
haftmann@58101
   173
      by (rule card_UN_disjoint) auto
haftmann@58101
   174
    moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)"
haftmann@58101
   175
      by (rule card_image) (simp add: inj_on_def)
haftmann@58101
   176
    ultimately show ?case by auto
haftmann@58101
   177
  qed
haftmann@58101
   178
  also have "\<dots> = length xs ^ n" by (simp add: card_length)
haftmann@58101
   179
  finally show "card (set (List.n_lists n xs)) = length (List.n_lists n xs)"
haftmann@58101
   180
    by (simp add: length_n_lists)
haftmann@58101
   181
qed
haftmann@58101
   182
haftmann@58101
   183
haftmann@58101
   184
subsection {* Tools setup *}
haftmann@58101
   185
haftmann@58101
   186
lemma setsum_set_upto_conv_listsum_int [code_unfold]:
haftmann@58101
   187
  "setsum f (set [i..j::int]) = listsum (map f [i..j])"
haftmann@58101
   188
  by (simp add: interv_listsum_conv_setsum_set_int)
haftmann@58101
   189
haftmann@58101
   190
lemma setsum_set_upt_conv_listsum_nat [code_unfold]:
haftmann@58101
   191
  "setsum f (set [m..<n]) = listsum (map f [m..<n])"
haftmann@58101
   192
  by (simp add: interv_listsum_conv_setsum_set_nat)
haftmann@58101
   193
haftmann@58101
   194
lemma setsum_code [code]:
haftmann@58101
   195
  "setsum f (set xs) = listsum (map f (remdups xs))"
haftmann@58101
   196
  by (simp add: listsum_distinct_conv_setsum_set)
haftmann@58101
   197
haftmann@58101
   198
context
haftmann@58101
   199
begin
haftmann@58101
   200
haftmann@58101
   201
interpretation lifting_syntax .
haftmann@58101
   202
haftmann@58101
   203
lemma listsum_transfer[transfer_rule]:
haftmann@58101
   204
  assumes [transfer_rule]: "A 0 0"
haftmann@58101
   205
  assumes [transfer_rule]: "(A ===> A ===> A) op + op +"
haftmann@58101
   206
  shows "(list_all2 A ===> A) listsum listsum"
haftmann@58101
   207
  unfolding listsum_def[abs_def]
haftmann@58101
   208
  by transfer_prover
haftmann@58101
   209
haftmann@58101
   210
end
haftmann@58101
   211
haftmann@58101
   212
end