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