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