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