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