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