src/HOL/SetInterval.thy
author nipkow
Wed Aug 26 16:13:19 2009 +0200 (2009-08-26)
changeset 32408 a1a85b0a26f7
parent 32400 6c62363cf0d7
child 32436 10cd49e0c067
permissions -rw-r--r--
new interval lemma
CCS example for predicate compiler
     1 (*  Title:      HOL/SetInterval.thy
     2     Author:     Tobias Nipkow and Clemens Ballarin
     3                 Additions by Jeremy Avigad in March 2004
     4     Copyright   2000  TU Muenchen
     5 
     6 lessThan, greaterThan, atLeast, atMost and two-sided intervals
     7 *)
     8 
     9 header {* Set intervals *}
    10 
    11 theory SetInterval
    12 imports Int
    13 begin
    14 
    15 context ord
    16 begin
    17 definition
    18   lessThan    :: "'a => 'a set"	("(1{..<_})") where
    19   "{..<u} == {x. x < u}"
    20 
    21 definition
    22   atMost      :: "'a => 'a set"	("(1{.._})") where
    23   "{..u} == {x. x \<le> u}"
    24 
    25 definition
    26   greaterThan :: "'a => 'a set"	("(1{_<..})") where
    27   "{l<..} == {x. l<x}"
    28 
    29 definition
    30   atLeast     :: "'a => 'a set"	("(1{_..})") where
    31   "{l..} == {x. l\<le>x}"
    32 
    33 definition
    34   greaterThanLessThan :: "'a => 'a => 'a set"  ("(1{_<..<_})") where
    35   "{l<..<u} == {l<..} Int {..<u}"
    36 
    37 definition
    38   atLeastLessThan :: "'a => 'a => 'a set"      ("(1{_..<_})") where
    39   "{l..<u} == {l..} Int {..<u}"
    40 
    41 definition
    42   greaterThanAtMost :: "'a => 'a => 'a set"    ("(1{_<.._})") where
    43   "{l<..u} == {l<..} Int {..u}"
    44 
    45 definition
    46   atLeastAtMost :: "'a => 'a => 'a set"        ("(1{_.._})") where
    47   "{l..u} == {l..} Int {..u}"
    48 
    49 end
    50 
    51 
    52 text{* A note of warning when using @{term"{..<n}"} on type @{typ
    53 nat}: it is equivalent to @{term"{0::nat..<n}"} but some lemmas involving
    54 @{term"{m..<n}"} may not exist in @{term"{..<n}"}-form as well. *}
    55 
    56 syntax
    57   "@UNION_le"   :: "'a => 'a => 'b set => 'b set"       ("(3UN _<=_./ _)" 10)
    58   "@UNION_less" :: "'a => 'a => 'b set => 'b set"       ("(3UN _<_./ _)" 10)
    59   "@INTER_le"   :: "'a => 'a => 'b set => 'b set"       ("(3INT _<=_./ _)" 10)
    60   "@INTER_less" :: "'a => 'a => 'b set => 'b set"       ("(3INT _<_./ _)" 10)
    61 
    62 syntax (xsymbols)
    63   "@UNION_le"   :: "'a => 'a => 'b set => 'b set"       ("(3\<Union> _\<le>_./ _)" 10)
    64   "@UNION_less" :: "'a => 'a => 'b set => 'b set"       ("(3\<Union> _<_./ _)" 10)
    65   "@INTER_le"   :: "'a => 'a => 'b set => 'b set"       ("(3\<Inter> _\<le>_./ _)" 10)
    66   "@INTER_less" :: "'a => 'a => 'b set => 'b set"       ("(3\<Inter> _<_./ _)" 10)
    67 
    68 syntax (latex output)
    69   "@UNION_le"   :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Union>(00_ \<le> _)/ _)" 10)
    70   "@UNION_less" :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Union>(00_ < _)/ _)" 10)
    71   "@INTER_le"   :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Inter>(00_ \<le> _)/ _)" 10)
    72   "@INTER_less" :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Inter>(00_ < _)/ _)" 10)
    73 
    74 translations
    75   "UN i<=n. A"  == "UN i:{..n}. A"
    76   "UN i<n. A"   == "UN i:{..<n}. A"
    77   "INT i<=n. A" == "INT i:{..n}. A"
    78   "INT i<n. A"  == "INT i:{..<n}. A"
    79 
    80 
    81 subsection {* Various equivalences *}
    82 
    83 lemma (in ord) lessThan_iff [iff]: "(i: lessThan k) = (i<k)"
    84 by (simp add: lessThan_def)
    85 
    86 lemma Compl_lessThan [simp]:
    87     "!!k:: 'a::linorder. -lessThan k = atLeast k"
    88 apply (auto simp add: lessThan_def atLeast_def)
    89 done
    90 
    91 lemma single_Diff_lessThan [simp]: "!!k:: 'a::order. {k} - lessThan k = {k}"
    92 by auto
    93 
    94 lemma (in ord) greaterThan_iff [iff]: "(i: greaterThan k) = (k<i)"
    95 by (simp add: greaterThan_def)
    96 
    97 lemma Compl_greaterThan [simp]:
    98     "!!k:: 'a::linorder. -greaterThan k = atMost k"
    99   by (auto simp add: greaterThan_def atMost_def)
   100 
   101 lemma Compl_atMost [simp]: "!!k:: 'a::linorder. -atMost k = greaterThan k"
   102 apply (subst Compl_greaterThan [symmetric])
   103 apply (rule double_complement)
   104 done
   105 
   106 lemma (in ord) atLeast_iff [iff]: "(i: atLeast k) = (k<=i)"
   107 by (simp add: atLeast_def)
   108 
   109 lemma Compl_atLeast [simp]:
   110     "!!k:: 'a::linorder. -atLeast k = lessThan k"
   111   by (auto simp add: lessThan_def atLeast_def)
   112 
   113 lemma (in ord) atMost_iff [iff]: "(i: atMost k) = (i<=k)"
   114 by (simp add: atMost_def)
   115 
   116 lemma atMost_Int_atLeast: "!!n:: 'a::order. atMost n Int atLeast n = {n}"
   117 by (blast intro: order_antisym)
   118 
   119 
   120 subsection {* Logical Equivalences for Set Inclusion and Equality *}
   121 
   122 lemma atLeast_subset_iff [iff]:
   123      "(atLeast x \<subseteq> atLeast y) = (y \<le> (x::'a::order))"
   124 by (blast intro: order_trans)
   125 
   126 lemma atLeast_eq_iff [iff]:
   127      "(atLeast x = atLeast y) = (x = (y::'a::linorder))"
   128 by (blast intro: order_antisym order_trans)
   129 
   130 lemma greaterThan_subset_iff [iff]:
   131      "(greaterThan x \<subseteq> greaterThan y) = (y \<le> (x::'a::linorder))"
   132 apply (auto simp add: greaterThan_def)
   133  apply (subst linorder_not_less [symmetric], blast)
   134 done
   135 
   136 lemma greaterThan_eq_iff [iff]:
   137      "(greaterThan x = greaterThan y) = (x = (y::'a::linorder))"
   138 apply (rule iffI)
   139  apply (erule equalityE)
   140  apply simp_all
   141 done
   142 
   143 lemma atMost_subset_iff [iff]: "(atMost x \<subseteq> atMost y) = (x \<le> (y::'a::order))"
   144 by (blast intro: order_trans)
   145 
   146 lemma atMost_eq_iff [iff]: "(atMost x = atMost y) = (x = (y::'a::linorder))"
   147 by (blast intro: order_antisym order_trans)
   148 
   149 lemma lessThan_subset_iff [iff]:
   150      "(lessThan x \<subseteq> lessThan y) = (x \<le> (y::'a::linorder))"
   151 apply (auto simp add: lessThan_def)
   152  apply (subst linorder_not_less [symmetric], blast)
   153 done
   154 
   155 lemma lessThan_eq_iff [iff]:
   156      "(lessThan x = lessThan y) = (x = (y::'a::linorder))"
   157 apply (rule iffI)
   158  apply (erule equalityE)
   159  apply simp_all
   160 done
   161 
   162 
   163 subsection {*Two-sided intervals*}
   164 
   165 context ord
   166 begin
   167 
   168 lemma greaterThanLessThan_iff [simp,noatp]:
   169   "(i : {l<..<u}) = (l < i & i < u)"
   170 by (simp add: greaterThanLessThan_def)
   171 
   172 lemma atLeastLessThan_iff [simp,noatp]:
   173   "(i : {l..<u}) = (l <= i & i < u)"
   174 by (simp add: atLeastLessThan_def)
   175 
   176 lemma greaterThanAtMost_iff [simp,noatp]:
   177   "(i : {l<..u}) = (l < i & i <= u)"
   178 by (simp add: greaterThanAtMost_def)
   179 
   180 lemma atLeastAtMost_iff [simp,noatp]:
   181   "(i : {l..u}) = (l <= i & i <= u)"
   182 by (simp add: atLeastAtMost_def)
   183 
   184 text {* The above four lemmas could be declared as iffs.
   185   If we do so, a call to blast in Hyperreal/Star.ML, lemma @{text STAR_Int}
   186   seems to take forever (more than one hour). *}
   187 end
   188 
   189 subsubsection{* Emptyness, singletons, subset *}
   190 
   191 context order
   192 begin
   193 
   194 lemma atLeastatMost_empty[simp]:
   195   "b < a \<Longrightarrow> {a..b} = {}"
   196 by(auto simp: atLeastAtMost_def atLeast_def atMost_def)
   197 
   198 lemma atLeastatMost_empty_iff[simp]:
   199   "{a..b} = {} \<longleftrightarrow> (~ a <= b)"
   200 by auto (blast intro: order_trans)
   201 
   202 lemma atLeastatMost_empty_iff2[simp]:
   203   "{} = {a..b} \<longleftrightarrow> (~ a <= b)"
   204 by auto (blast intro: order_trans)
   205 
   206 lemma atLeastLessThan_empty[simp]:
   207   "b <= a \<Longrightarrow> {a..<b} = {}"
   208 by(auto simp: atLeastLessThan_def)
   209 
   210 lemma atLeastLessThan_empty_iff[simp]:
   211   "{a..<b} = {} \<longleftrightarrow> (~ a < b)"
   212 by auto (blast intro: le_less_trans)
   213 
   214 lemma atLeastLessThan_empty_iff2[simp]:
   215   "{} = {a..<b} \<longleftrightarrow> (~ a < b)"
   216 by auto (blast intro: le_less_trans)
   217 
   218 lemma greaterThanAtMost_empty[simp]: "l \<le> k ==> {k<..l} = {}"
   219 by(auto simp:greaterThanAtMost_def greaterThan_def atMost_def)
   220 
   221 lemma greaterThanAtMost_empty_iff[simp]: "{k<..l} = {} \<longleftrightarrow> ~ k < l"
   222 by auto (blast intro: less_le_trans)
   223 
   224 lemma greaterThanAtMost_empty_iff2[simp]: "{} = {k<..l} \<longleftrightarrow> ~ k < l"
   225 by auto (blast intro: less_le_trans)
   226 
   227 lemma greaterThanLessThan_empty[simp]:"l \<le> k ==> {k<..<l} = {}"
   228 by(auto simp:greaterThanLessThan_def greaterThan_def lessThan_def)
   229 
   230 lemma atLeastAtMost_singleton [simp]: "{a..a} = {a}"
   231 by (auto simp add: atLeastAtMost_def atMost_def atLeast_def)
   232 
   233 lemma atLeastatMost_subset_iff[simp]:
   234   "{a..b} <= {c..d} \<longleftrightarrow> (~ a <= b) | c <= a & b <= d"
   235 unfolding atLeastAtMost_def atLeast_def atMost_def
   236 by (blast intro: order_trans)
   237 
   238 lemma atLeastatMost_psubset_iff:
   239   "{a..b} < {c..d} \<longleftrightarrow>
   240    ((~ a <= b) | c <= a & b <= d & (c < a | b < d))  &  c <= d"
   241 by(simp add: psubset_eq expand_set_eq less_le_not_le)(blast intro: order_trans)
   242 
   243 end
   244 
   245 lemma (in linorder) atLeastLessThan_subset_iff:
   246   "{a..<b} <= {c..<d} \<Longrightarrow> b <= a | c<=a & b<=d"
   247 apply (auto simp:subset_eq Ball_def)
   248 apply(frule_tac x=a in spec)
   249 apply(erule_tac x=d in allE)
   250 apply (simp add: less_imp_le)
   251 done
   252 
   253 subsection {* Intervals of natural numbers *}
   254 
   255 subsubsection {* The Constant @{term lessThan} *}
   256 
   257 lemma lessThan_0 [simp]: "lessThan (0::nat) = {}"
   258 by (simp add: lessThan_def)
   259 
   260 lemma lessThan_Suc: "lessThan (Suc k) = insert k (lessThan k)"
   261 by (simp add: lessThan_def less_Suc_eq, blast)
   262 
   263 lemma lessThan_Suc_atMost: "lessThan (Suc k) = atMost k"
   264 by (simp add: lessThan_def atMost_def less_Suc_eq_le)
   265 
   266 lemma UN_lessThan_UNIV: "(UN m::nat. lessThan m) = UNIV"
   267 by blast
   268 
   269 subsubsection {* The Constant @{term greaterThan} *}
   270 
   271 lemma greaterThan_0 [simp]: "greaterThan 0 = range Suc"
   272 apply (simp add: greaterThan_def)
   273 apply (blast dest: gr0_conv_Suc [THEN iffD1])
   274 done
   275 
   276 lemma greaterThan_Suc: "greaterThan (Suc k) = greaterThan k - {Suc k}"
   277 apply (simp add: greaterThan_def)
   278 apply (auto elim: linorder_neqE)
   279 done
   280 
   281 lemma INT_greaterThan_UNIV: "(INT m::nat. greaterThan m) = {}"
   282 by blast
   283 
   284 subsubsection {* The Constant @{term atLeast} *}
   285 
   286 lemma atLeast_0 [simp]: "atLeast (0::nat) = UNIV"
   287 by (unfold atLeast_def UNIV_def, simp)
   288 
   289 lemma atLeast_Suc: "atLeast (Suc k) = atLeast k - {k}"
   290 apply (simp add: atLeast_def)
   291 apply (simp add: Suc_le_eq)
   292 apply (simp add: order_le_less, blast)
   293 done
   294 
   295 lemma atLeast_Suc_greaterThan: "atLeast (Suc k) = greaterThan k"
   296   by (auto simp add: greaterThan_def atLeast_def less_Suc_eq_le)
   297 
   298 lemma UN_atLeast_UNIV: "(UN m::nat. atLeast m) = UNIV"
   299 by blast
   300 
   301 subsubsection {* The Constant @{term atMost} *}
   302 
   303 lemma atMost_0 [simp]: "atMost (0::nat) = {0}"
   304 by (simp add: atMost_def)
   305 
   306 lemma atMost_Suc: "atMost (Suc k) = insert (Suc k) (atMost k)"
   307 apply (simp add: atMost_def)
   308 apply (simp add: less_Suc_eq order_le_less, blast)
   309 done
   310 
   311 lemma UN_atMost_UNIV: "(UN m::nat. atMost m) = UNIV"
   312 by blast
   313 
   314 subsubsection {* The Constant @{term atLeastLessThan} *}
   315 
   316 text{*The orientation of the following 2 rules is tricky. The lhs is
   317 defined in terms of the rhs.  Hence the chosen orientation makes sense
   318 in this theory --- the reverse orientation complicates proofs (eg
   319 nontermination). But outside, when the definition of the lhs is rarely
   320 used, the opposite orientation seems preferable because it reduces a
   321 specific concept to a more general one. *}
   322 
   323 lemma atLeast0LessThan: "{0::nat..<n} = {..<n}"
   324 by(simp add:lessThan_def atLeastLessThan_def)
   325 
   326 lemma atLeast0AtMost: "{0..n::nat} = {..n}"
   327 by(simp add:atMost_def atLeastAtMost_def)
   328 
   329 declare atLeast0LessThan[symmetric, code_unfold]
   330         atLeast0AtMost[symmetric, code_unfold]
   331 
   332 lemma atLeastLessThan0: "{m..<0::nat} = {}"
   333 by (simp add: atLeastLessThan_def)
   334 
   335 subsubsection {* Intervals of nats with @{term Suc} *}
   336 
   337 text{*Not a simprule because the RHS is too messy.*}
   338 lemma atLeastLessThanSuc:
   339     "{m..<Suc n} = (if m \<le> n then insert n {m..<n} else {})"
   340 by (auto simp add: atLeastLessThan_def)
   341 
   342 lemma atLeastLessThan_singleton [simp]: "{m..<Suc m} = {m}"
   343 by (auto simp add: atLeastLessThan_def)
   344 (*
   345 lemma atLeast_sum_LessThan [simp]: "{m + k..<k::nat} = {}"
   346 by (induct k, simp_all add: atLeastLessThanSuc)
   347 
   348 lemma atLeastSucLessThan [simp]: "{Suc n..<n} = {}"
   349 by (auto simp add: atLeastLessThan_def)
   350 *)
   351 lemma atLeastLessThanSuc_atLeastAtMost: "{l..<Suc u} = {l..u}"
   352   by (simp add: lessThan_Suc_atMost atLeastAtMost_def atLeastLessThan_def)
   353 
   354 lemma atLeastSucAtMost_greaterThanAtMost: "{Suc l..u} = {l<..u}"
   355   by (simp add: atLeast_Suc_greaterThan atLeastAtMost_def
   356     greaterThanAtMost_def)
   357 
   358 lemma atLeastSucLessThan_greaterThanLessThan: "{Suc l..<u} = {l<..<u}"
   359   by (simp add: atLeast_Suc_greaterThan atLeastLessThan_def
   360     greaterThanLessThan_def)
   361 
   362 lemma atLeastAtMostSuc_conv: "m \<le> Suc n \<Longrightarrow> {m..Suc n} = insert (Suc n) {m..n}"
   363 by (auto simp add: atLeastAtMost_def)
   364 
   365 subsubsection {* Image *}
   366 
   367 lemma image_add_atLeastAtMost:
   368   "(%n::nat. n+k) ` {i..j} = {i+k..j+k}" (is "?A = ?B")
   369 proof
   370   show "?A \<subseteq> ?B" by auto
   371 next
   372   show "?B \<subseteq> ?A"
   373   proof
   374     fix n assume a: "n : ?B"
   375     hence "n - k : {i..j}" by auto
   376     moreover have "n = (n - k) + k" using a by auto
   377     ultimately show "n : ?A" by blast
   378   qed
   379 qed
   380 
   381 lemma image_add_atLeastLessThan:
   382   "(%n::nat. n+k) ` {i..<j} = {i+k..<j+k}" (is "?A = ?B")
   383 proof
   384   show "?A \<subseteq> ?B" by auto
   385 next
   386   show "?B \<subseteq> ?A"
   387   proof
   388     fix n assume a: "n : ?B"
   389     hence "n - k : {i..<j}" by auto
   390     moreover have "n = (n - k) + k" using a by auto
   391     ultimately show "n : ?A" by blast
   392   qed
   393 qed
   394 
   395 corollary image_Suc_atLeastAtMost[simp]:
   396   "Suc ` {i..j} = {Suc i..Suc j}"
   397 using image_add_atLeastAtMost[where k="Suc 0"] by simp
   398 
   399 corollary image_Suc_atLeastLessThan[simp]:
   400   "Suc ` {i..<j} = {Suc i..<Suc j}"
   401 using image_add_atLeastLessThan[where k="Suc 0"] by simp
   402 
   403 lemma image_add_int_atLeastLessThan:
   404     "(%x. x + (l::int)) ` {0..<u-l} = {l..<u}"
   405   apply (auto simp add: image_def)
   406   apply (rule_tac x = "x - l" in bexI)
   407   apply auto
   408   done
   409 
   410 
   411 subsubsection {* Finiteness *}
   412 
   413 lemma finite_lessThan [iff]: fixes k :: nat shows "finite {..<k}"
   414   by (induct k) (simp_all add: lessThan_Suc)
   415 
   416 lemma finite_atMost [iff]: fixes k :: nat shows "finite {..k}"
   417   by (induct k) (simp_all add: atMost_Suc)
   418 
   419 lemma finite_greaterThanLessThan [iff]:
   420   fixes l :: nat shows "finite {l<..<u}"
   421 by (simp add: greaterThanLessThan_def)
   422 
   423 lemma finite_atLeastLessThan [iff]:
   424   fixes l :: nat shows "finite {l..<u}"
   425 by (simp add: atLeastLessThan_def)
   426 
   427 lemma finite_greaterThanAtMost [iff]:
   428   fixes l :: nat shows "finite {l<..u}"
   429 by (simp add: greaterThanAtMost_def)
   430 
   431 lemma finite_atLeastAtMost [iff]:
   432   fixes l :: nat shows "finite {l..u}"
   433 by (simp add: atLeastAtMost_def)
   434 
   435 text {* A bounded set of natural numbers is finite. *}
   436 lemma bounded_nat_set_is_finite:
   437   "(ALL i:N. i < (n::nat)) ==> finite N"
   438 apply (rule finite_subset)
   439  apply (rule_tac [2] finite_lessThan, auto)
   440 done
   441 
   442 text {* A set of natural numbers is finite iff it is bounded. *}
   443 lemma finite_nat_set_iff_bounded:
   444   "finite(N::nat set) = (EX m. ALL n:N. n<m)" (is "?F = ?B")
   445 proof
   446   assume f:?F  show ?B
   447     using Max_ge[OF `?F`, simplified less_Suc_eq_le[symmetric]] by blast
   448 next
   449   assume ?B show ?F using `?B` by(blast intro:bounded_nat_set_is_finite)
   450 qed
   451 
   452 lemma finite_nat_set_iff_bounded_le:
   453   "finite(N::nat set) = (EX m. ALL n:N. n<=m)"
   454 apply(simp add:finite_nat_set_iff_bounded)
   455 apply(blast dest:less_imp_le_nat le_imp_less_Suc)
   456 done
   457 
   458 lemma finite_less_ub:
   459      "!!f::nat=>nat. (!!n. n \<le> f n) ==> finite {n. f n \<le> u}"
   460 by (rule_tac B="{..u}" in finite_subset, auto intro: order_trans)
   461 
   462 text{* Any subset of an interval of natural numbers the size of the
   463 subset is exactly that interval. *}
   464 
   465 lemma subset_card_intvl_is_intvl:
   466   "A <= {k..<k+card A} \<Longrightarrow> A = {k..<k+card A}" (is "PROP ?P")
   467 proof cases
   468   assume "finite A"
   469   thus "PROP ?P"
   470   proof(induct A rule:finite_linorder_max_induct)
   471     case empty thus ?case by auto
   472   next
   473     case (insert A b)
   474     moreover hence "b ~: A" by auto
   475     moreover have "A <= {k..<k+card A}" and "b = k+card A"
   476       using `b ~: A` insert by fastsimp+
   477     ultimately show ?case by auto
   478   qed
   479 next
   480   assume "~finite A" thus "PROP ?P" by simp
   481 qed
   482 
   483 
   484 subsubsection {* Cardinality *}
   485 
   486 lemma card_lessThan [simp]: "card {..<u} = u"
   487   by (induct u, simp_all add: lessThan_Suc)
   488 
   489 lemma card_atMost [simp]: "card {..u} = Suc u"
   490   by (simp add: lessThan_Suc_atMost [THEN sym])
   491 
   492 lemma card_atLeastLessThan [simp]: "card {l..<u} = u - l"
   493   apply (subgoal_tac "card {l..<u} = card {..<u-l}")
   494   apply (erule ssubst, rule card_lessThan)
   495   apply (subgoal_tac "(%x. x + l) ` {..<u-l} = {l..<u}")
   496   apply (erule subst)
   497   apply (rule card_image)
   498   apply (simp add: inj_on_def)
   499   apply (auto simp add: image_def atLeastLessThan_def lessThan_def)
   500   apply (rule_tac x = "x - l" in exI)
   501   apply arith
   502   done
   503 
   504 lemma card_atLeastAtMost [simp]: "card {l..u} = Suc u - l"
   505   by (subst atLeastLessThanSuc_atLeastAtMost [THEN sym], simp)
   506 
   507 lemma card_greaterThanAtMost [simp]: "card {l<..u} = u - l"
   508   by (subst atLeastSucAtMost_greaterThanAtMost [THEN sym], simp)
   509 
   510 lemma card_greaterThanLessThan [simp]: "card {l<..<u} = u - Suc l"
   511   by (subst atLeastSucLessThan_greaterThanLessThan [THEN sym], simp)
   512 
   513 lemma ex_bij_betw_nat_finite:
   514   "finite M \<Longrightarrow> \<exists>h. bij_betw h {0..<card M} M"
   515 apply(drule finite_imp_nat_seg_image_inj_on)
   516 apply(auto simp:atLeast0LessThan[symmetric] lessThan_def[symmetric] card_image bij_betw_def)
   517 done
   518 
   519 lemma ex_bij_betw_finite_nat:
   520   "finite M \<Longrightarrow> \<exists>h. bij_betw h M {0..<card M}"
   521 by (blast dest: ex_bij_betw_nat_finite bij_betw_inv)
   522 
   523 lemma finite_same_card_bij:
   524   "finite A \<Longrightarrow> finite B \<Longrightarrow> card A = card B \<Longrightarrow> EX h. bij_betw h A B"
   525 apply(drule ex_bij_betw_finite_nat)
   526 apply(drule ex_bij_betw_nat_finite)
   527 apply(auto intro!:bij_betw_trans)
   528 done
   529 
   530 lemma ex_bij_betw_nat_finite_1:
   531   "finite M \<Longrightarrow> \<exists>h. bij_betw h {1 .. card M} M"
   532 by (rule finite_same_card_bij) auto
   533 
   534 
   535 subsection {* Intervals of integers *}
   536 
   537 lemma atLeastLessThanPlusOne_atLeastAtMost_int: "{l..<u+1} = {l..(u::int)}"
   538   by (auto simp add: atLeastAtMost_def atLeastLessThan_def)
   539 
   540 lemma atLeastPlusOneAtMost_greaterThanAtMost_int: "{l+1..u} = {l<..(u::int)}"
   541   by (auto simp add: atLeastAtMost_def greaterThanAtMost_def)
   542 
   543 lemma atLeastPlusOneLessThan_greaterThanLessThan_int:
   544     "{l+1..<u} = {l<..<u::int}"
   545   by (auto simp add: atLeastLessThan_def greaterThanLessThan_def)
   546 
   547 subsubsection {* Finiteness *}
   548 
   549 lemma image_atLeastZeroLessThan_int: "0 \<le> u ==>
   550     {(0::int)..<u} = int ` {..<nat u}"
   551   apply (unfold image_def lessThan_def)
   552   apply auto
   553   apply (rule_tac x = "nat x" in exI)
   554   apply (auto simp add: zless_nat_conj zless_nat_eq_int_zless [THEN sym])
   555   done
   556 
   557 lemma finite_atLeastZeroLessThan_int: "finite {(0::int)..<u}"
   558   apply (case_tac "0 \<le> u")
   559   apply (subst image_atLeastZeroLessThan_int, assumption)
   560   apply (rule finite_imageI)
   561   apply auto
   562   done
   563 
   564 lemma finite_atLeastLessThan_int [iff]: "finite {l..<u::int}"
   565   apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
   566   apply (erule subst)
   567   apply (rule finite_imageI)
   568   apply (rule finite_atLeastZeroLessThan_int)
   569   apply (rule image_add_int_atLeastLessThan)
   570   done
   571 
   572 lemma finite_atLeastAtMost_int [iff]: "finite {l..(u::int)}"
   573   by (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym], simp)
   574 
   575 lemma finite_greaterThanAtMost_int [iff]: "finite {l<..(u::int)}"
   576   by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
   577 
   578 lemma finite_greaterThanLessThan_int [iff]: "finite {l<..<u::int}"
   579   by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
   580 
   581 
   582 subsubsection {* Cardinality *}
   583 
   584 lemma card_atLeastZeroLessThan_int: "card {(0::int)..<u} = nat u"
   585   apply (case_tac "0 \<le> u")
   586   apply (subst image_atLeastZeroLessThan_int, assumption)
   587   apply (subst card_image)
   588   apply (auto simp add: inj_on_def)
   589   done
   590 
   591 lemma card_atLeastLessThan_int [simp]: "card {l..<u} = nat (u - l)"
   592   apply (subgoal_tac "card {l..<u} = card {0..<u-l}")
   593   apply (erule ssubst, rule card_atLeastZeroLessThan_int)
   594   apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
   595   apply (erule subst)
   596   apply (rule card_image)
   597   apply (simp add: inj_on_def)
   598   apply (rule image_add_int_atLeastLessThan)
   599   done
   600 
   601 lemma card_atLeastAtMost_int [simp]: "card {l..u} = nat (u - l + 1)"
   602 apply (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym])
   603 apply (auto simp add: algebra_simps)
   604 done
   605 
   606 lemma card_greaterThanAtMost_int [simp]: "card {l<..u} = nat (u - l)"
   607 by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
   608 
   609 lemma card_greaterThanLessThan_int [simp]: "card {l<..<u} = nat (u - (l + 1))"
   610 by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
   611 
   612 lemma finite_M_bounded_by_nat: "finite {k. P k \<and> k < (i::nat)}"
   613 proof -
   614   have "{k. P k \<and> k < i} \<subseteq> {..<i}" by auto
   615   with finite_lessThan[of "i"] show ?thesis by (simp add: finite_subset)
   616 qed
   617 
   618 lemma card_less:
   619 assumes zero_in_M: "0 \<in> M"
   620 shows "card {k \<in> M. k < Suc i} \<noteq> 0"
   621 proof -
   622   from zero_in_M have "{k \<in> M. k < Suc i} \<noteq> {}" by auto
   623   with finite_M_bounded_by_nat show ?thesis by (auto simp add: card_eq_0_iff)
   624 qed
   625 
   626 lemma card_less_Suc2: "0 \<notin> M \<Longrightarrow> card {k. Suc k \<in> M \<and> k < i} = card {k \<in> M. k < Suc i}"
   627 apply (rule card_bij_eq [of "Suc" _ _ "\<lambda>x. x - Suc 0"])
   628 apply simp
   629 apply fastsimp
   630 apply auto
   631 apply (rule inj_on_diff_nat)
   632 apply auto
   633 apply (case_tac x)
   634 apply auto
   635 apply (case_tac xa)
   636 apply auto
   637 apply (case_tac xa)
   638 apply auto
   639 done
   640 
   641 lemma card_less_Suc:
   642   assumes zero_in_M: "0 \<in> M"
   643     shows "Suc (card {k. Suc k \<in> M \<and> k < i}) = card {k \<in> M. k < Suc i}"
   644 proof -
   645   from assms have a: "0 \<in> {k \<in> M. k < Suc i}" by simp
   646   hence c: "{k \<in> M. k < Suc i} = insert 0 ({k \<in> M. k < Suc i} - {0})"
   647     by (auto simp only: insert_Diff)
   648   have b: "{k \<in> M. k < Suc i} - {0} = {k \<in> M - {0}. k < Suc i}"  by auto
   649   from finite_M_bounded_by_nat[of "\<lambda>x. x \<in> M" "Suc i"] have "Suc (card {k. Suc k \<in> M \<and> k < i}) = card (insert 0 ({k \<in> M. k < Suc i} - {0}))"
   650     apply (subst card_insert)
   651     apply simp_all
   652     apply (subst b)
   653     apply (subst card_less_Suc2[symmetric])
   654     apply simp_all
   655     done
   656   with c show ?thesis by simp
   657 qed
   658 
   659 
   660 subsection {*Lemmas useful with the summation operator setsum*}
   661 
   662 text {* For examples, see Algebra/poly/UnivPoly2.thy *}
   663 
   664 subsubsection {* Disjoint Unions *}
   665 
   666 text {* Singletons and open intervals *}
   667 
   668 lemma ivl_disj_un_singleton:
   669   "{l::'a::linorder} Un {l<..} = {l..}"
   670   "{..<u} Un {u::'a::linorder} = {..u}"
   671   "(l::'a::linorder) < u ==> {l} Un {l<..<u} = {l..<u}"
   672   "(l::'a::linorder) < u ==> {l<..<u} Un {u} = {l<..u}"
   673   "(l::'a::linorder) <= u ==> {l} Un {l<..u} = {l..u}"
   674   "(l::'a::linorder) <= u ==> {l..<u} Un {u} = {l..u}"
   675 by auto
   676 
   677 text {* One- and two-sided intervals *}
   678 
   679 lemma ivl_disj_un_one:
   680   "(l::'a::linorder) < u ==> {..l} Un {l<..<u} = {..<u}"
   681   "(l::'a::linorder) <= u ==> {..<l} Un {l..<u} = {..<u}"
   682   "(l::'a::linorder) <= u ==> {..l} Un {l<..u} = {..u}"
   683   "(l::'a::linorder) <= u ==> {..<l} Un {l..u} = {..u}"
   684   "(l::'a::linorder) <= u ==> {l<..u} Un {u<..} = {l<..}"
   685   "(l::'a::linorder) < u ==> {l<..<u} Un {u..} = {l<..}"
   686   "(l::'a::linorder) <= u ==> {l..u} Un {u<..} = {l..}"
   687   "(l::'a::linorder) <= u ==> {l..<u} Un {u..} = {l..}"
   688 by auto
   689 
   690 text {* Two- and two-sided intervals *}
   691 
   692 lemma ivl_disj_un_two:
   693   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..<u} = {l<..<u}"
   694   "[| (l::'a::linorder) <= m; m < u |] ==> {l<..m} Un {m<..<u} = {l<..<u}"
   695   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..<u} = {l..<u}"
   696   "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m<..<u} = {l..<u}"
   697   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..u} = {l<..u}"
   698   "[| (l::'a::linorder) <= m; m <= u |] ==> {l<..m} Un {m<..u} = {l<..u}"
   699   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..u} = {l..u}"
   700   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m<..u} = {l..u}"
   701 by auto
   702 
   703 lemmas ivl_disj_un = ivl_disj_un_singleton ivl_disj_un_one ivl_disj_un_two
   704 
   705 subsubsection {* Disjoint Intersections *}
   706 
   707 text {* Singletons and open intervals *}
   708 
   709 lemma ivl_disj_int_singleton:
   710   "{l::'a::order} Int {l<..} = {}"
   711   "{..<u} Int {u} = {}"
   712   "{l} Int {l<..<u} = {}"
   713   "{l<..<u} Int {u} = {}"
   714   "{l} Int {l<..u} = {}"
   715   "{l..<u} Int {u} = {}"
   716   by simp+
   717 
   718 text {* One- and two-sided intervals *}
   719 
   720 lemma ivl_disj_int_one:
   721   "{..l::'a::order} Int {l<..<u} = {}"
   722   "{..<l} Int {l..<u} = {}"
   723   "{..l} Int {l<..u} = {}"
   724   "{..<l} Int {l..u} = {}"
   725   "{l<..u} Int {u<..} = {}"
   726   "{l<..<u} Int {u..} = {}"
   727   "{l..u} Int {u<..} = {}"
   728   "{l..<u} Int {u..} = {}"
   729   by auto
   730 
   731 text {* Two- and two-sided intervals *}
   732 
   733 lemma ivl_disj_int_two:
   734   "{l::'a::order<..<m} Int {m..<u} = {}"
   735   "{l<..m} Int {m<..<u} = {}"
   736   "{l..<m} Int {m..<u} = {}"
   737   "{l..m} Int {m<..<u} = {}"
   738   "{l<..<m} Int {m..u} = {}"
   739   "{l<..m} Int {m<..u} = {}"
   740   "{l..<m} Int {m..u} = {}"
   741   "{l..m} Int {m<..u} = {}"
   742   by auto
   743 
   744 lemmas ivl_disj_int = ivl_disj_int_singleton ivl_disj_int_one ivl_disj_int_two
   745 
   746 subsubsection {* Some Differences *}
   747 
   748 lemma ivl_diff[simp]:
   749  "i \<le> n \<Longrightarrow> {i..<m} - {i..<n} = {n..<(m::'a::linorder)}"
   750 by(auto)
   751 
   752 
   753 subsubsection {* Some Subset Conditions *}
   754 
   755 lemma ivl_subset [simp,noatp]:
   756  "({i..<j} \<subseteq> {m..<n}) = (j \<le> i | m \<le> i & j \<le> (n::'a::linorder))"
   757 apply(auto simp:linorder_not_le)
   758 apply(rule ccontr)
   759 apply(insert linorder_le_less_linear[of i n])
   760 apply(clarsimp simp:linorder_not_le)
   761 apply(fastsimp)
   762 done
   763 
   764 
   765 subsection {* Summation indexed over intervals *}
   766 
   767 syntax
   768   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _.._./ _)" [0,0,0,10] 10)
   769   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _..<_./ _)" [0,0,0,10] 10)
   770   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<_./ _)" [0,0,10] 10)
   771   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<=_./ _)" [0,0,10] 10)
   772 syntax (xsymbols)
   773   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
   774   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
   775   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
   776   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
   777 syntax (HTML output)
   778   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
   779   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
   780   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
   781   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
   782 syntax (latex_sum output)
   783   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
   784  ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
   785   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
   786  ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
   787   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
   788  ("(3\<^raw:$\sum_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
   789   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
   790  ("(3\<^raw:$\sum_{>_ \<le> _\<^raw:}$> _)" [0,0,10] 10)
   791 
   792 translations
   793   "\<Sum>x=a..b. t" == "CONST setsum (%x. t) {a..b}"
   794   "\<Sum>x=a..<b. t" == "CONST setsum (%x. t) {a..<b}"
   795   "\<Sum>i\<le>n. t" == "CONST setsum (\<lambda>i. t) {..n}"
   796   "\<Sum>i<n. t" == "CONST setsum (\<lambda>i. t) {..<n}"
   797 
   798 text{* The above introduces some pretty alternative syntaxes for
   799 summation over intervals:
   800 \begin{center}
   801 \begin{tabular}{lll}
   802 Old & New & \LaTeX\\
   803 @{term[source]"\<Sum>x\<in>{a..b}. e"} & @{term"\<Sum>x=a..b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..b. e"}\\
   804 @{term[source]"\<Sum>x\<in>{a..<b}. e"} & @{term"\<Sum>x=a..<b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..<b. e"}\\
   805 @{term[source]"\<Sum>x\<in>{..b}. e"} & @{term"\<Sum>x\<le>b. e"} & @{term[mode=latex_sum]"\<Sum>x\<le>b. e"}\\
   806 @{term[source]"\<Sum>x\<in>{..<b}. e"} & @{term"\<Sum>x<b. e"} & @{term[mode=latex_sum]"\<Sum>x<b. e"}
   807 \end{tabular}
   808 \end{center}
   809 The left column shows the term before introduction of the new syntax,
   810 the middle column shows the new (default) syntax, and the right column
   811 shows a special syntax. The latter is only meaningful for latex output
   812 and has to be activated explicitly by setting the print mode to
   813 @{text latex_sum} (e.g.\ via @{text "mode = latex_sum"} in
   814 antiquotations). It is not the default \LaTeX\ output because it only
   815 works well with italic-style formulae, not tt-style.
   816 
   817 Note that for uniformity on @{typ nat} it is better to use
   818 @{term"\<Sum>x::nat=0..<n. e"} rather than @{text"\<Sum>x<n. e"}: @{text setsum} may
   819 not provide all lemmas available for @{term"{m..<n}"} also in the
   820 special form for @{term"{..<n}"}. *}
   821 
   822 text{* This congruence rule should be used for sums over intervals as
   823 the standard theorem @{text[source]setsum_cong} does not work well
   824 with the simplifier who adds the unsimplified premise @{term"x:B"} to
   825 the context. *}
   826 
   827 lemma setsum_ivl_cong:
   828  "\<lbrakk>a = c; b = d; !!x. \<lbrakk> c \<le> x; x < d \<rbrakk> \<Longrightarrow> f x = g x \<rbrakk> \<Longrightarrow>
   829  setsum f {a..<b} = setsum g {c..<d}"
   830 by(rule setsum_cong, simp_all)
   831 
   832 (* FIXME why are the following simp rules but the corresponding eqns
   833 on intervals are not? *)
   834 
   835 lemma setsum_atMost_Suc[simp]: "(\<Sum>i \<le> Suc n. f i) = (\<Sum>i \<le> n. f i) + f(Suc n)"
   836 by (simp add:atMost_Suc add_ac)
   837 
   838 lemma setsum_lessThan_Suc[simp]: "(\<Sum>i < Suc n. f i) = (\<Sum>i < n. f i) + f n"
   839 by (simp add:lessThan_Suc add_ac)
   840 
   841 lemma setsum_cl_ivl_Suc[simp]:
   842   "setsum f {m..Suc n} = (if Suc n < m then 0 else setsum f {m..n} + f(Suc n))"
   843 by (auto simp:add_ac atLeastAtMostSuc_conv)
   844 
   845 lemma setsum_op_ivl_Suc[simp]:
   846   "setsum f {m..<Suc n} = (if n < m then 0 else setsum f {m..<n} + f(n))"
   847 by (auto simp:add_ac atLeastLessThanSuc)
   848 (*
   849 lemma setsum_cl_ivl_add_one_nat: "(n::nat) <= m + 1 ==>
   850     (\<Sum>i=n..m+1. f i) = (\<Sum>i=n..m. f i) + f(m + 1)"
   851 by (auto simp:add_ac atLeastAtMostSuc_conv)
   852 *)
   853 
   854 lemma setsum_head:
   855   fixes n :: nat
   856   assumes mn: "m <= n" 
   857   shows "(\<Sum>x\<in>{m..n}. P x) = P m + (\<Sum>x\<in>{m<..n}. P x)" (is "?lhs = ?rhs")
   858 proof -
   859   from mn
   860   have "{m..n} = {m} \<union> {m<..n}"
   861     by (auto intro: ivl_disj_un_singleton)
   862   hence "?lhs = (\<Sum>x\<in>{m} \<union> {m<..n}. P x)"
   863     by (simp add: atLeast0LessThan)
   864   also have "\<dots> = ?rhs" by simp
   865   finally show ?thesis .
   866 qed
   867 
   868 lemma setsum_head_Suc:
   869   "m \<le> n \<Longrightarrow> setsum f {m..n} = f m + setsum f {Suc m..n}"
   870 by (simp add: setsum_head atLeastSucAtMost_greaterThanAtMost)
   871 
   872 lemma setsum_head_upt_Suc:
   873   "m < n \<Longrightarrow> setsum f {m..<n} = f m + setsum f {Suc m..<n}"
   874 apply(insert setsum_head_Suc[of m "n - Suc 0" f])
   875 apply (simp add: atLeastLessThanSuc_atLeastAtMost[symmetric] algebra_simps)
   876 done
   877 
   878 lemma setsum_ub_add_nat: assumes "(m::nat) \<le> n + 1"
   879   shows "setsum f {m..n + p} = setsum f {m..n} + setsum f {n + 1..n + p}"
   880 proof-
   881   have "{m .. n+p} = {m..n} \<union> {n+1..n+p}" using `m \<le> n+1` by auto
   882   thus ?thesis by (auto simp: ivl_disj_int setsum_Un_disjoint
   883     atLeastSucAtMost_greaterThanAtMost)
   884 qed
   885 
   886 lemma setsum_add_nat_ivl: "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
   887   setsum f {m..<n} + setsum f {n..<p} = setsum f {m..<p::nat}"
   888 by (simp add:setsum_Un_disjoint[symmetric] ivl_disj_int ivl_disj_un)
   889 
   890 lemma setsum_diff_nat_ivl:
   891 fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
   892 shows "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
   893   setsum f {m..<p} - setsum f {m..<n} = setsum f {n..<p}"
   894 using setsum_add_nat_ivl [of m n p f,symmetric]
   895 apply (simp add: add_ac)
   896 done
   897 
   898 lemma setsum_natinterval_difff:
   899   fixes f:: "nat \<Rightarrow> ('a::ab_group_add)"
   900   shows  "setsum (\<lambda>k. f k - f(k + 1)) {(m::nat) .. n} =
   901           (if m <= n then f m - f(n + 1) else 0)"
   902 by (induct n, auto simp add: algebra_simps not_le le_Suc_eq)
   903 
   904 lemmas setsum_restrict_set' = setsum_restrict_set[unfolded Int_def]
   905 
   906 lemma setsum_setsum_restrict:
   907   "finite S \<Longrightarrow> finite T \<Longrightarrow> setsum (\<lambda>x. setsum (\<lambda>y. f x y) {y. y\<in> T \<and> R x y}) S = setsum (\<lambda>y. setsum (\<lambda>x. f x y) {x. x \<in> S \<and> R x y}) T"
   908   by (simp add: setsum_restrict_set'[unfolded mem_def] mem_def)
   909      (rule setsum_commute)
   910 
   911 lemma setsum_image_gen: assumes fS: "finite S"
   912   shows "setsum g S = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
   913 proof-
   914   { fix x assume "x \<in> S" then have "{y. y\<in> f`S \<and> f x = y} = {f x}" by auto }
   915   hence "setsum g S = setsum (\<lambda>x. setsum (\<lambda>y. g x) {y. y\<in> f`S \<and> f x = y}) S"
   916     by simp
   917   also have "\<dots> = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
   918     by (rule setsum_setsum_restrict[OF fS finite_imageI[OF fS]])
   919   finally show ?thesis .
   920 qed
   921 
   922 lemma setsum_multicount_gen:
   923   assumes "finite s" "finite t" "\<forall>j\<in>t. (card {i\<in>s. R i j} = k j)"
   924   shows "setsum (\<lambda>i. (card {j\<in>t. R i j})) s = setsum k t" (is "?l = ?r")
   925 proof-
   926   have "?l = setsum (\<lambda>i. setsum (\<lambda>x.1) {j\<in>t. R i j}) s" by auto
   927   also have "\<dots> = ?r" unfolding setsum_setsum_restrict[OF assms(1-2)]
   928     using assms(3) by auto
   929   finally show ?thesis .
   930 qed
   931 
   932 lemma setsum_multicount:
   933   assumes "finite S" "finite T" "\<forall>j\<in>T. (card {i\<in>S. R i j} = k)"
   934   shows "setsum (\<lambda>i. card {j\<in>T. R i j}) S = k * card T" (is "?l = ?r")
   935 proof-
   936   have "?l = setsum (\<lambda>i. k) T" by(rule setsum_multicount_gen)(auto simp:assms)
   937   also have "\<dots> = ?r" by(simp add: setsum_constant mult_commute)
   938   finally show ?thesis by auto
   939 qed
   940 
   941 
   942 subsection{* Shifting bounds *}
   943 
   944 lemma setsum_shift_bounds_nat_ivl:
   945   "setsum f {m+k..<n+k} = setsum (%i. f(i + k)){m..<n::nat}"
   946 by (induct "n", auto simp:atLeastLessThanSuc)
   947 
   948 lemma setsum_shift_bounds_cl_nat_ivl:
   949   "setsum f {m+k..n+k} = setsum (%i. f(i + k)){m..n::nat}"
   950 apply (insert setsum_reindex[OF inj_on_add_nat, where h=f and B = "{m..n}"])
   951 apply (simp add:image_add_atLeastAtMost o_def)
   952 done
   953 
   954 corollary setsum_shift_bounds_cl_Suc_ivl:
   955   "setsum f {Suc m..Suc n} = setsum (%i. f(Suc i)){m..n}"
   956 by (simp add:setsum_shift_bounds_cl_nat_ivl[where k="Suc 0", simplified])
   957 
   958 corollary setsum_shift_bounds_Suc_ivl:
   959   "setsum f {Suc m..<Suc n} = setsum (%i. f(Suc i)){m..<n}"
   960 by (simp add:setsum_shift_bounds_nat_ivl[where k="Suc 0", simplified])
   961 
   962 lemma setsum_shift_lb_Suc0_0:
   963   "f(0::nat) = (0::nat) \<Longrightarrow> setsum f {Suc 0..k} = setsum f {0..k}"
   964 by(simp add:setsum_head_Suc)
   965 
   966 lemma setsum_shift_lb_Suc0_0_upt:
   967   "f(0::nat) = 0 \<Longrightarrow> setsum f {Suc 0..<k} = setsum f {0..<k}"
   968 apply(cases k)apply simp
   969 apply(simp add:setsum_head_upt_Suc)
   970 done
   971 
   972 subsection {* The formula for geometric sums *}
   973 
   974 lemma geometric_sum:
   975   "x ~= 1 ==> (\<Sum>i=0..<n. x ^ i) =
   976   (x ^ n - 1) / (x - 1::'a::{field})"
   977 by (induct "n") (simp_all add:field_simps power_Suc)
   978 
   979 subsection {* The formula for arithmetic sums *}
   980 
   981 lemma gauss_sum:
   982   "((1::'a::comm_semiring_1) + 1)*(\<Sum>i\<in>{1..n}. of_nat i) =
   983    of_nat n*((of_nat n)+1)"
   984 proof (induct n)
   985   case 0
   986   show ?case by simp
   987 next
   988   case (Suc n)
   989   then show ?case by (simp add: algebra_simps)
   990 qed
   991 
   992 theorem arith_series_general:
   993   "((1::'a::comm_semiring_1) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
   994   of_nat n * (a + (a + of_nat(n - 1)*d))"
   995 proof cases
   996   assume ngt1: "n > 1"
   997   let ?I = "\<lambda>i. of_nat i" and ?n = "of_nat n"
   998   have
   999     "(\<Sum>i\<in>{..<n}. a+?I i*d) =
  1000      ((\<Sum>i\<in>{..<n}. a) + (\<Sum>i\<in>{..<n}. ?I i*d))"
  1001     by (rule setsum_addf)
  1002   also from ngt1 have "\<dots> = ?n*a + (\<Sum>i\<in>{..<n}. ?I i*d)" by simp
  1003   also from ngt1 have "\<dots> = (?n*a + d*(\<Sum>i\<in>{1..<n}. ?I i))"
  1004     unfolding One_nat_def
  1005     by (simp add: setsum_right_distrib atLeast0LessThan[symmetric] setsum_shift_lb_Suc0_0_upt mult_ac)
  1006   also have "(1+1)*\<dots> = (1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..<n}. ?I i)"
  1007     by (simp add: left_distrib right_distrib)
  1008   also from ngt1 have "{1..<n} = {1..n - 1}"
  1009     by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost)
  1010   also from ngt1
  1011   have "(1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..n - 1}. ?I i) = ((1+1)*?n*a + d*?I (n - 1)*?I n)"
  1012     by (simp only: mult_ac gauss_sum [of "n - 1"], unfold One_nat_def)
  1013        (simp add:  mult_ac trans [OF add_commute of_nat_Suc [symmetric]])
  1014   finally show ?thesis by (simp add: algebra_simps)
  1015 next
  1016   assume "\<not>(n > 1)"
  1017   hence "n = 1 \<or> n = 0" by auto
  1018   thus ?thesis by (auto simp: algebra_simps)
  1019 qed
  1020 
  1021 lemma arith_series_nat:
  1022   "Suc (Suc 0) * (\<Sum>i\<in>{..<n}. a+i*d) = n * (a + (a+(n - 1)*d))"
  1023 proof -
  1024   have
  1025     "((1::nat) + 1) * (\<Sum>i\<in>{..<n::nat}. a + of_nat(i)*d) =
  1026     of_nat(n) * (a + (a + of_nat(n - 1)*d))"
  1027     by (rule arith_series_general)
  1028   thus ?thesis
  1029     unfolding One_nat_def by (auto simp add: of_nat_id)
  1030 qed
  1031 
  1032 lemma arith_series_int:
  1033   "(2::int) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
  1034   of_nat n * (a + (a + of_nat(n - 1)*d))"
  1035 proof -
  1036   have
  1037     "((1::int) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
  1038     of_nat(n) * (a + (a + of_nat(n - 1)*d))"
  1039     by (rule arith_series_general)
  1040   thus ?thesis by simp
  1041 qed
  1042 
  1043 lemma sum_diff_distrib:
  1044   fixes P::"nat\<Rightarrow>nat"
  1045   shows
  1046   "\<forall>x. Q x \<le> P x  \<Longrightarrow>
  1047   (\<Sum>x<n. P x) - (\<Sum>x<n. Q x) = (\<Sum>x<n. P x - Q x)"
  1048 proof (induct n)
  1049   case 0 show ?case by simp
  1050 next
  1051   case (Suc n)
  1052 
  1053   let ?lhs = "(\<Sum>x<n. P x) - (\<Sum>x<n. Q x)"
  1054   let ?rhs = "\<Sum>x<n. P x - Q x"
  1055 
  1056   from Suc have "?lhs = ?rhs" by simp
  1057   moreover
  1058   from Suc have "?lhs + P n - Q n = ?rhs + (P n - Q n)" by simp
  1059   moreover
  1060   from Suc have
  1061     "(\<Sum>x<n. P x) + P n - ((\<Sum>x<n. Q x) + Q n) = ?rhs + (P n - Q n)"
  1062     by (subst diff_diff_left[symmetric],
  1063         subst diff_add_assoc2)
  1064        (auto simp: diff_add_assoc2 intro: setsum_mono)
  1065   ultimately
  1066   show ?case by simp
  1067 qed
  1068 
  1069 subsection {* Products indexed over intervals *}
  1070 
  1071 syntax
  1072   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _ = _.._./ _)" [0,0,0,10] 10)
  1073   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _ = _..<_./ _)" [0,0,0,10] 10)
  1074   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _<_./ _)" [0,0,10] 10)
  1075   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _<=_./ _)" [0,0,10] 10)
  1076 syntax (xsymbols)
  1077   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _.._./ _)" [0,0,0,10] 10)
  1078   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _..<_./ _)" [0,0,0,10] 10)
  1079   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_<_./ _)" [0,0,10] 10)
  1080   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_\<le>_./ _)" [0,0,10] 10)
  1081 syntax (HTML output)
  1082   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _.._./ _)" [0,0,0,10] 10)
  1083   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _..<_./ _)" [0,0,0,10] 10)
  1084   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_<_./ _)" [0,0,10] 10)
  1085   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_\<le>_./ _)" [0,0,10] 10)
  1086 syntax (latex_prod output)
  1087   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1088  ("(3\<^raw:$\prod_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
  1089   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1090  ("(3\<^raw:$\prod_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
  1091   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1092  ("(3\<^raw:$\prod_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
  1093   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1094  ("(3\<^raw:$\prod_{>_ \<le> _\<^raw:}$> _)" [0,0,10] 10)
  1095 
  1096 translations
  1097   "\<Prod>x=a..b. t" == "CONST setprod (%x. t) {a..b}"
  1098   "\<Prod>x=a..<b. t" == "CONST setprod (%x. t) {a..<b}"
  1099   "\<Prod>i\<le>n. t" == "CONST setprod (\<lambda>i. t) {..n}"
  1100   "\<Prod>i<n. t" == "CONST setprod (\<lambda>i. t) {..<n}"
  1101 
  1102 end