src/HOL/SetInterval.thy
author nipkow
Thu Jun 04 13:26:32 2009 +0200 (2009-06-04)
changeset 31438 a1c4c1500abe
parent 31044 6896c2498ac0
child 31501 2a60c9b951e0
permissions -rw-r--r--
A few finite lemmas
     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 and singletons *}
   190 
   191 context order
   192 begin
   193 
   194 lemma atLeastAtMost_empty [simp]: "n < m ==> {m..n} = {}";
   195 by (auto simp add: atLeastAtMost_def atMost_def atLeast_def)
   196 
   197 lemma atLeastLessThan_empty[simp]: "n \<le> m ==> {m..<n} = {}"
   198 by (auto simp add: atLeastLessThan_def)
   199 
   200 lemma greaterThanAtMost_empty[simp]:"l \<le> k ==> {k<..l} = {}"
   201 by(auto simp:greaterThanAtMost_def greaterThan_def atMost_def)
   202 
   203 lemma greaterThanLessThan_empty[simp]:"l \<le> k ==> {k<..<l} = {}"
   204 by(auto simp:greaterThanLessThan_def greaterThan_def lessThan_def)
   205 
   206 lemma atLeastAtMost_singleton [simp]: "{a..a} = {a}"
   207 by (auto simp add: atLeastAtMost_def atMost_def atLeast_def)
   208 
   209 end
   210 
   211 subsection {* Intervals of natural numbers *}
   212 
   213 subsubsection {* The Constant @{term lessThan} *}
   214 
   215 lemma lessThan_0 [simp]: "lessThan (0::nat) = {}"
   216 by (simp add: lessThan_def)
   217 
   218 lemma lessThan_Suc: "lessThan (Suc k) = insert k (lessThan k)"
   219 by (simp add: lessThan_def less_Suc_eq, blast)
   220 
   221 lemma lessThan_Suc_atMost: "lessThan (Suc k) = atMost k"
   222 by (simp add: lessThan_def atMost_def less_Suc_eq_le)
   223 
   224 lemma UN_lessThan_UNIV: "(UN m::nat. lessThan m) = UNIV"
   225 by blast
   226 
   227 subsubsection {* The Constant @{term greaterThan} *}
   228 
   229 lemma greaterThan_0 [simp]: "greaterThan 0 = range Suc"
   230 apply (simp add: greaterThan_def)
   231 apply (blast dest: gr0_conv_Suc [THEN iffD1])
   232 done
   233 
   234 lemma greaterThan_Suc: "greaterThan (Suc k) = greaterThan k - {Suc k}"
   235 apply (simp add: greaterThan_def)
   236 apply (auto elim: linorder_neqE)
   237 done
   238 
   239 lemma INT_greaterThan_UNIV: "(INT m::nat. greaterThan m) = {}"
   240 by blast
   241 
   242 subsubsection {* The Constant @{term atLeast} *}
   243 
   244 lemma atLeast_0 [simp]: "atLeast (0::nat) = UNIV"
   245 by (unfold atLeast_def UNIV_def, simp)
   246 
   247 lemma atLeast_Suc: "atLeast (Suc k) = atLeast k - {k}"
   248 apply (simp add: atLeast_def)
   249 apply (simp add: Suc_le_eq)
   250 apply (simp add: order_le_less, blast)
   251 done
   252 
   253 lemma atLeast_Suc_greaterThan: "atLeast (Suc k) = greaterThan k"
   254   by (auto simp add: greaterThan_def atLeast_def less_Suc_eq_le)
   255 
   256 lemma UN_atLeast_UNIV: "(UN m::nat. atLeast m) = UNIV"
   257 by blast
   258 
   259 subsubsection {* The Constant @{term atMost} *}
   260 
   261 lemma atMost_0 [simp]: "atMost (0::nat) = {0}"
   262 by (simp add: atMost_def)
   263 
   264 lemma atMost_Suc: "atMost (Suc k) = insert (Suc k) (atMost k)"
   265 apply (simp add: atMost_def)
   266 apply (simp add: less_Suc_eq order_le_less, blast)
   267 done
   268 
   269 lemma UN_atMost_UNIV: "(UN m::nat. atMost m) = UNIV"
   270 by blast
   271 
   272 subsubsection {* The Constant @{term atLeastLessThan} *}
   273 
   274 text{*The orientation of the following 2 rules is tricky. The lhs is
   275 defined in terms of the rhs.  Hence the chosen orientation makes sense
   276 in this theory --- the reverse orientation complicates proofs (eg
   277 nontermination). But outside, when the definition of the lhs is rarely
   278 used, the opposite orientation seems preferable because it reduces a
   279 specific concept to a more general one. *}
   280 
   281 lemma atLeast0LessThan: "{0::nat..<n} = {..<n}"
   282 by(simp add:lessThan_def atLeastLessThan_def)
   283 
   284 lemma atLeast0AtMost: "{0..n::nat} = {..n}"
   285 by(simp add:atMost_def atLeastAtMost_def)
   286 
   287 declare atLeast0LessThan[symmetric, code unfold]
   288         atLeast0AtMost[symmetric, code unfold]
   289 
   290 lemma atLeastLessThan0: "{m..<0::nat} = {}"
   291 by (simp add: atLeastLessThan_def)
   292 
   293 subsubsection {* Intervals of nats with @{term Suc} *}
   294 
   295 text{*Not a simprule because the RHS is too messy.*}
   296 lemma atLeastLessThanSuc:
   297     "{m..<Suc n} = (if m \<le> n then insert n {m..<n} else {})"
   298 by (auto simp add: atLeastLessThan_def)
   299 
   300 lemma atLeastLessThan_singleton [simp]: "{m..<Suc m} = {m}"
   301 by (auto simp add: atLeastLessThan_def)
   302 (*
   303 lemma atLeast_sum_LessThan [simp]: "{m + k..<k::nat} = {}"
   304 by (induct k, simp_all add: atLeastLessThanSuc)
   305 
   306 lemma atLeastSucLessThan [simp]: "{Suc n..<n} = {}"
   307 by (auto simp add: atLeastLessThan_def)
   308 *)
   309 lemma atLeastLessThanSuc_atLeastAtMost: "{l..<Suc u} = {l..u}"
   310   by (simp add: lessThan_Suc_atMost atLeastAtMost_def atLeastLessThan_def)
   311 
   312 lemma atLeastSucAtMost_greaterThanAtMost: "{Suc l..u} = {l<..u}"
   313   by (simp add: atLeast_Suc_greaterThan atLeastAtMost_def
   314     greaterThanAtMost_def)
   315 
   316 lemma atLeastSucLessThan_greaterThanLessThan: "{Suc l..<u} = {l<..<u}"
   317   by (simp add: atLeast_Suc_greaterThan atLeastLessThan_def
   318     greaterThanLessThan_def)
   319 
   320 lemma atLeastAtMostSuc_conv: "m \<le> Suc n \<Longrightarrow> {m..Suc n} = insert (Suc n) {m..n}"
   321 by (auto simp add: atLeastAtMost_def)
   322 
   323 subsubsection {* Image *}
   324 
   325 lemma image_add_atLeastAtMost:
   326   "(%n::nat. n+k) ` {i..j} = {i+k..j+k}" (is "?A = ?B")
   327 proof
   328   show "?A \<subseteq> ?B" by auto
   329 next
   330   show "?B \<subseteq> ?A"
   331   proof
   332     fix n assume a: "n : ?B"
   333     hence "n - k : {i..j}" by auto
   334     moreover have "n = (n - k) + k" using a by auto
   335     ultimately show "n : ?A" by blast
   336   qed
   337 qed
   338 
   339 lemma image_add_atLeastLessThan:
   340   "(%n::nat. n+k) ` {i..<j} = {i+k..<j+k}" (is "?A = ?B")
   341 proof
   342   show "?A \<subseteq> ?B" by auto
   343 next
   344   show "?B \<subseteq> ?A"
   345   proof
   346     fix n assume a: "n : ?B"
   347     hence "n - k : {i..<j}" by auto
   348     moreover have "n = (n - k) + k" using a by auto
   349     ultimately show "n : ?A" by blast
   350   qed
   351 qed
   352 
   353 corollary image_Suc_atLeastAtMost[simp]:
   354   "Suc ` {i..j} = {Suc i..Suc j}"
   355 using image_add_atLeastAtMost[where k="Suc 0"] by simp
   356 
   357 corollary image_Suc_atLeastLessThan[simp]:
   358   "Suc ` {i..<j} = {Suc i..<Suc j}"
   359 using image_add_atLeastLessThan[where k="Suc 0"] by simp
   360 
   361 lemma image_add_int_atLeastLessThan:
   362     "(%x. x + (l::int)) ` {0..<u-l} = {l..<u}"
   363   apply (auto simp add: image_def)
   364   apply (rule_tac x = "x - l" in bexI)
   365   apply auto
   366   done
   367 
   368 
   369 subsubsection {* Finiteness *}
   370 
   371 lemma finite_lessThan [iff]: fixes k :: nat shows "finite {..<k}"
   372   by (induct k) (simp_all add: lessThan_Suc)
   373 
   374 lemma finite_atMost [iff]: fixes k :: nat shows "finite {..k}"
   375   by (induct k) (simp_all add: atMost_Suc)
   376 
   377 lemma finite_greaterThanLessThan [iff]:
   378   fixes l :: nat shows "finite {l<..<u}"
   379 by (simp add: greaterThanLessThan_def)
   380 
   381 lemma finite_atLeastLessThan [iff]:
   382   fixes l :: nat shows "finite {l..<u}"
   383 by (simp add: atLeastLessThan_def)
   384 
   385 lemma finite_greaterThanAtMost [iff]:
   386   fixes l :: nat shows "finite {l<..u}"
   387 by (simp add: greaterThanAtMost_def)
   388 
   389 lemma finite_atLeastAtMost [iff]:
   390   fixes l :: nat shows "finite {l..u}"
   391 by (simp add: atLeastAtMost_def)
   392 
   393 text {* A bounded set of natural numbers is finite. *}
   394 lemma bounded_nat_set_is_finite:
   395   "(ALL i:N. i < (n::nat)) ==> finite N"
   396 apply (rule finite_subset)
   397  apply (rule_tac [2] finite_lessThan, auto)
   398 done
   399 
   400 text {* A set of natural numbers is finite iff it is bounded. *}
   401 lemma finite_nat_set_iff_bounded:
   402   "finite(N::nat set) = (EX m. ALL n:N. n<m)" (is "?F = ?B")
   403 proof
   404   assume f:?F  show ?B
   405     using Max_ge[OF `?F`, simplified less_Suc_eq_le[symmetric]] by blast
   406 next
   407   assume ?B show ?F using `?B` by(blast intro:bounded_nat_set_is_finite)
   408 qed
   409 
   410 lemma finite_nat_set_iff_bounded_le:
   411   "finite(N::nat set) = (EX m. ALL n:N. n<=m)"
   412 apply(simp add:finite_nat_set_iff_bounded)
   413 apply(blast dest:less_imp_le_nat le_imp_less_Suc)
   414 done
   415 
   416 lemma finite_less_ub:
   417      "!!f::nat=>nat. (!!n. n \<le> f n) ==> finite {n. f n \<le> u}"
   418 by (rule_tac B="{..u}" in finite_subset, auto intro: order_trans)
   419 
   420 text{* Any subset of an interval of natural numbers the size of the
   421 subset is exactly that interval. *}
   422 
   423 lemma subset_card_intvl_is_intvl:
   424   "A <= {k..<k+card A} \<Longrightarrow> A = {k..<k+card A}" (is "PROP ?P")
   425 proof cases
   426   assume "finite A"
   427   thus "PROP ?P"
   428   proof(induct A rule:finite_linorder_induct)
   429     case empty thus ?case by auto
   430   next
   431     case (insert A b)
   432     moreover hence "b ~: A" by auto
   433     moreover have "A <= {k..<k+card A}" and "b = k+card A"
   434       using `b ~: A` insert by fastsimp+
   435     ultimately show ?case by auto
   436   qed
   437 next
   438   assume "~finite A" thus "PROP ?P" by simp
   439 qed
   440 
   441 
   442 subsubsection {* Cardinality *}
   443 
   444 lemma card_lessThan [simp]: "card {..<u} = u"
   445   by (induct u, simp_all add: lessThan_Suc)
   446 
   447 lemma card_atMost [simp]: "card {..u} = Suc u"
   448   by (simp add: lessThan_Suc_atMost [THEN sym])
   449 
   450 lemma card_atLeastLessThan [simp]: "card {l..<u} = u - l"
   451   apply (subgoal_tac "card {l..<u} = card {..<u-l}")
   452   apply (erule ssubst, rule card_lessThan)
   453   apply (subgoal_tac "(%x. x + l) ` {..<u-l} = {l..<u}")
   454   apply (erule subst)
   455   apply (rule card_image)
   456   apply (simp add: inj_on_def)
   457   apply (auto simp add: image_def atLeastLessThan_def lessThan_def)
   458   apply (rule_tac x = "x - l" in exI)
   459   apply arith
   460   done
   461 
   462 lemma card_atLeastAtMost [simp]: "card {l..u} = Suc u - l"
   463   by (subst atLeastLessThanSuc_atLeastAtMost [THEN sym], simp)
   464 
   465 lemma card_greaterThanAtMost [simp]: "card {l<..u} = u - l"
   466   by (subst atLeastSucAtMost_greaterThanAtMost [THEN sym], simp)
   467 
   468 lemma card_greaterThanLessThan [simp]: "card {l<..<u} = u - Suc l"
   469   by (subst atLeastSucLessThan_greaterThanLessThan [THEN sym], simp)
   470 
   471 lemma ex_bij_betw_nat_finite:
   472   "finite M \<Longrightarrow> \<exists>h. bij_betw h {0..<card M} M"
   473 apply(drule finite_imp_nat_seg_image_inj_on)
   474 apply(auto simp:atLeast0LessThan[symmetric] lessThan_def[symmetric] card_image bij_betw_def)
   475 done
   476 
   477 lemma ex_bij_betw_finite_nat:
   478   "finite M \<Longrightarrow> \<exists>h. bij_betw h M {0..<card M}"
   479 by (blast dest: ex_bij_betw_nat_finite bij_betw_inv)
   480 
   481 lemma finite_same_card_bij:
   482   "finite A \<Longrightarrow> finite B \<Longrightarrow> card A = card B \<Longrightarrow> EX h. bij_betw h A B"
   483 apply(drule ex_bij_betw_finite_nat)
   484 apply(drule ex_bij_betw_nat_finite)
   485 apply(auto intro!:bij_betw_trans)
   486 done
   487 
   488 lemma ex_bij_betw_nat_finite_1:
   489   "finite M \<Longrightarrow> \<exists>h. bij_betw h {1 .. card M} M"
   490 by (rule finite_same_card_bij) auto
   491 
   492 
   493 subsection {* Intervals of integers *}
   494 
   495 lemma atLeastLessThanPlusOne_atLeastAtMost_int: "{l..<u+1} = {l..(u::int)}"
   496   by (auto simp add: atLeastAtMost_def atLeastLessThan_def)
   497 
   498 lemma atLeastPlusOneAtMost_greaterThanAtMost_int: "{l+1..u} = {l<..(u::int)}"
   499   by (auto simp add: atLeastAtMost_def greaterThanAtMost_def)
   500 
   501 lemma atLeastPlusOneLessThan_greaterThanLessThan_int:
   502     "{l+1..<u} = {l<..<u::int}"
   503   by (auto simp add: atLeastLessThan_def greaterThanLessThan_def)
   504 
   505 subsubsection {* Finiteness *}
   506 
   507 lemma image_atLeastZeroLessThan_int: "0 \<le> u ==>
   508     {(0::int)..<u} = int ` {..<nat u}"
   509   apply (unfold image_def lessThan_def)
   510   apply auto
   511   apply (rule_tac x = "nat x" in exI)
   512   apply (auto simp add: zless_nat_conj zless_nat_eq_int_zless [THEN sym])
   513   done
   514 
   515 lemma finite_atLeastZeroLessThan_int: "finite {(0::int)..<u}"
   516   apply (case_tac "0 \<le> u")
   517   apply (subst image_atLeastZeroLessThan_int, assumption)
   518   apply (rule finite_imageI)
   519   apply auto
   520   done
   521 
   522 lemma finite_atLeastLessThan_int [iff]: "finite {l..<u::int}"
   523   apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
   524   apply (erule subst)
   525   apply (rule finite_imageI)
   526   apply (rule finite_atLeastZeroLessThan_int)
   527   apply (rule image_add_int_atLeastLessThan)
   528   done
   529 
   530 lemma finite_atLeastAtMost_int [iff]: "finite {l..(u::int)}"
   531   by (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym], simp)
   532 
   533 lemma finite_greaterThanAtMost_int [iff]: "finite {l<..(u::int)}"
   534   by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
   535 
   536 lemma finite_greaterThanLessThan_int [iff]: "finite {l<..<u::int}"
   537   by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
   538 
   539 
   540 subsubsection {* Cardinality *}
   541 
   542 lemma card_atLeastZeroLessThan_int: "card {(0::int)..<u} = nat u"
   543   apply (case_tac "0 \<le> u")
   544   apply (subst image_atLeastZeroLessThan_int, assumption)
   545   apply (subst card_image)
   546   apply (auto simp add: inj_on_def)
   547   done
   548 
   549 lemma card_atLeastLessThan_int [simp]: "card {l..<u} = nat (u - l)"
   550   apply (subgoal_tac "card {l..<u} = card {0..<u-l}")
   551   apply (erule ssubst, rule card_atLeastZeroLessThan_int)
   552   apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
   553   apply (erule subst)
   554   apply (rule card_image)
   555   apply (simp add: inj_on_def)
   556   apply (rule image_add_int_atLeastLessThan)
   557   done
   558 
   559 lemma card_atLeastAtMost_int [simp]: "card {l..u} = nat (u - l + 1)"
   560 apply (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym])
   561 apply (auto simp add: algebra_simps)
   562 done
   563 
   564 lemma card_greaterThanAtMost_int [simp]: "card {l<..u} = nat (u - l)"
   565 by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
   566 
   567 lemma card_greaterThanLessThan_int [simp]: "card {l<..<u} = nat (u - (l + 1))"
   568 by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
   569 
   570 lemma finite_M_bounded_by_nat: "finite {k. P k \<and> k < (i::nat)}"
   571 proof -
   572   have "{k. P k \<and> k < i} \<subseteq> {..<i}" by auto
   573   with finite_lessThan[of "i"] show ?thesis by (simp add: finite_subset)
   574 qed
   575 
   576 lemma card_less:
   577 assumes zero_in_M: "0 \<in> M"
   578 shows "card {k \<in> M. k < Suc i} \<noteq> 0"
   579 proof -
   580   from zero_in_M have "{k \<in> M. k < Suc i} \<noteq> {}" by auto
   581   with finite_M_bounded_by_nat show ?thesis by (auto simp add: card_eq_0_iff)
   582 qed
   583 
   584 lemma card_less_Suc2: "0 \<notin> M \<Longrightarrow> card {k. Suc k \<in> M \<and> k < i} = card {k \<in> M. k < Suc i}"
   585 apply (rule card_bij_eq [of "Suc" _ _ "\<lambda>x. x - Suc 0"])
   586 apply simp
   587 apply fastsimp
   588 apply auto
   589 apply (rule inj_on_diff_nat)
   590 apply auto
   591 apply (case_tac x)
   592 apply auto
   593 apply (case_tac xa)
   594 apply auto
   595 apply (case_tac xa)
   596 apply auto
   597 done
   598 
   599 lemma card_less_Suc:
   600   assumes zero_in_M: "0 \<in> M"
   601     shows "Suc (card {k. Suc k \<in> M \<and> k < i}) = card {k \<in> M. k < Suc i}"
   602 proof -
   603   from assms have a: "0 \<in> {k \<in> M. k < Suc i}" by simp
   604   hence c: "{k \<in> M. k < Suc i} = insert 0 ({k \<in> M. k < Suc i} - {0})"
   605     by (auto simp only: insert_Diff)
   606   have b: "{k \<in> M. k < Suc i} - {0} = {k \<in> M - {0}. k < Suc i}"  by auto
   607   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}))"
   608     apply (subst card_insert)
   609     apply simp_all
   610     apply (subst b)
   611     apply (subst card_less_Suc2[symmetric])
   612     apply simp_all
   613     done
   614   with c show ?thesis by simp
   615 qed
   616 
   617 
   618 subsection {*Lemmas useful with the summation operator setsum*}
   619 
   620 text {* For examples, see Algebra/poly/UnivPoly2.thy *}
   621 
   622 subsubsection {* Disjoint Unions *}
   623 
   624 text {* Singletons and open intervals *}
   625 
   626 lemma ivl_disj_un_singleton:
   627   "{l::'a::linorder} Un {l<..} = {l..}"
   628   "{..<u} Un {u::'a::linorder} = {..u}"
   629   "(l::'a::linorder) < u ==> {l} Un {l<..<u} = {l..<u}"
   630   "(l::'a::linorder) < u ==> {l<..<u} Un {u} = {l<..u}"
   631   "(l::'a::linorder) <= u ==> {l} Un {l<..u} = {l..u}"
   632   "(l::'a::linorder) <= u ==> {l..<u} Un {u} = {l..u}"
   633 by auto
   634 
   635 text {* One- and two-sided intervals *}
   636 
   637 lemma ivl_disj_un_one:
   638   "(l::'a::linorder) < u ==> {..l} Un {l<..<u} = {..<u}"
   639   "(l::'a::linorder) <= u ==> {..<l} Un {l..<u} = {..<u}"
   640   "(l::'a::linorder) <= u ==> {..l} Un {l<..u} = {..u}"
   641   "(l::'a::linorder) <= u ==> {..<l} Un {l..u} = {..u}"
   642   "(l::'a::linorder) <= u ==> {l<..u} Un {u<..} = {l<..}"
   643   "(l::'a::linorder) < u ==> {l<..<u} Un {u..} = {l<..}"
   644   "(l::'a::linorder) <= u ==> {l..u} Un {u<..} = {l..}"
   645   "(l::'a::linorder) <= u ==> {l..<u} Un {u..} = {l..}"
   646 by auto
   647 
   648 text {* Two- and two-sided intervals *}
   649 
   650 lemma ivl_disj_un_two:
   651   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..<u} = {l<..<u}"
   652   "[| (l::'a::linorder) <= m; m < u |] ==> {l<..m} Un {m<..<u} = {l<..<u}"
   653   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..<u} = {l..<u}"
   654   "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m<..<u} = {l..<u}"
   655   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..u} = {l<..u}"
   656   "[| (l::'a::linorder) <= m; m <= u |] ==> {l<..m} Un {m<..u} = {l<..u}"
   657   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..u} = {l..u}"
   658   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m<..u} = {l..u}"
   659 by auto
   660 
   661 lemmas ivl_disj_un = ivl_disj_un_singleton ivl_disj_un_one ivl_disj_un_two
   662 
   663 subsubsection {* Disjoint Intersections *}
   664 
   665 text {* Singletons and open intervals *}
   666 
   667 lemma ivl_disj_int_singleton:
   668   "{l::'a::order} Int {l<..} = {}"
   669   "{..<u} Int {u} = {}"
   670   "{l} Int {l<..<u} = {}"
   671   "{l<..<u} Int {u} = {}"
   672   "{l} Int {l<..u} = {}"
   673   "{l..<u} Int {u} = {}"
   674   by simp+
   675 
   676 text {* One- and two-sided intervals *}
   677 
   678 lemma ivl_disj_int_one:
   679   "{..l::'a::order} Int {l<..<u} = {}"
   680   "{..<l} Int {l..<u} = {}"
   681   "{..l} Int {l<..u} = {}"
   682   "{..<l} Int {l..u} = {}"
   683   "{l<..u} Int {u<..} = {}"
   684   "{l<..<u} Int {u..} = {}"
   685   "{l..u} Int {u<..} = {}"
   686   "{l..<u} Int {u..} = {}"
   687   by auto
   688 
   689 text {* Two- and two-sided intervals *}
   690 
   691 lemma ivl_disj_int_two:
   692   "{l::'a::order<..<m} Int {m..<u} = {}"
   693   "{l<..m} Int {m<..<u} = {}"
   694   "{l..<m} Int {m..<u} = {}"
   695   "{l..m} Int {m<..<u} = {}"
   696   "{l<..<m} Int {m..u} = {}"
   697   "{l<..m} Int {m<..u} = {}"
   698   "{l..<m} Int {m..u} = {}"
   699   "{l..m} Int {m<..u} = {}"
   700   by auto
   701 
   702 lemmas ivl_disj_int = ivl_disj_int_singleton ivl_disj_int_one ivl_disj_int_two
   703 
   704 subsubsection {* Some Differences *}
   705 
   706 lemma ivl_diff[simp]:
   707  "i \<le> n \<Longrightarrow> {i..<m} - {i..<n} = {n..<(m::'a::linorder)}"
   708 by(auto)
   709 
   710 
   711 subsubsection {* Some Subset Conditions *}
   712 
   713 lemma ivl_subset [simp,noatp]:
   714  "({i..<j} \<subseteq> {m..<n}) = (j \<le> i | m \<le> i & j \<le> (n::'a::linorder))"
   715 apply(auto simp:linorder_not_le)
   716 apply(rule ccontr)
   717 apply(insert linorder_le_less_linear[of i n])
   718 apply(clarsimp simp:linorder_not_le)
   719 apply(fastsimp)
   720 done
   721 
   722 
   723 subsection {* Summation indexed over intervals *}
   724 
   725 syntax
   726   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _.._./ _)" [0,0,0,10] 10)
   727   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _..<_./ _)" [0,0,0,10] 10)
   728   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<_./ _)" [0,0,10] 10)
   729   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<=_./ _)" [0,0,10] 10)
   730 syntax (xsymbols)
   731   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
   732   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
   733   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
   734   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
   735 syntax (HTML output)
   736   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
   737   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
   738   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
   739   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
   740 syntax (latex_sum output)
   741   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
   742  ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
   743   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
   744  ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
   745   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
   746  ("(3\<^raw:$\sum_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
   747   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
   748  ("(3\<^raw:$\sum_{>_ \<le> _\<^raw:}$> _)" [0,0,10] 10)
   749 
   750 translations
   751   "\<Sum>x=a..b. t" == "CONST setsum (%x. t) {a..b}"
   752   "\<Sum>x=a..<b. t" == "CONST setsum (%x. t) {a..<b}"
   753   "\<Sum>i\<le>n. t" == "CONST setsum (\<lambda>i. t) {..n}"
   754   "\<Sum>i<n. t" == "CONST setsum (\<lambda>i. t) {..<n}"
   755 
   756 text{* The above introduces some pretty alternative syntaxes for
   757 summation over intervals:
   758 \begin{center}
   759 \begin{tabular}{lll}
   760 Old & New & \LaTeX\\
   761 @{term[source]"\<Sum>x\<in>{a..b}. e"} & @{term"\<Sum>x=a..b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..b. e"}\\
   762 @{term[source]"\<Sum>x\<in>{a..<b}. e"} & @{term"\<Sum>x=a..<b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..<b. e"}\\
   763 @{term[source]"\<Sum>x\<in>{..b}. e"} & @{term"\<Sum>x\<le>b. e"} & @{term[mode=latex_sum]"\<Sum>x\<le>b. e"}\\
   764 @{term[source]"\<Sum>x\<in>{..<b}. e"} & @{term"\<Sum>x<b. e"} & @{term[mode=latex_sum]"\<Sum>x<b. e"}
   765 \end{tabular}
   766 \end{center}
   767 The left column shows the term before introduction of the new syntax,
   768 the middle column shows the new (default) syntax, and the right column
   769 shows a special syntax. The latter is only meaningful for latex output
   770 and has to be activated explicitly by setting the print mode to
   771 @{text latex_sum} (e.g.\ via @{text "mode = latex_sum"} in
   772 antiquotations). It is not the default \LaTeX\ output because it only
   773 works well with italic-style formulae, not tt-style.
   774 
   775 Note that for uniformity on @{typ nat} it is better to use
   776 @{term"\<Sum>x::nat=0..<n. e"} rather than @{text"\<Sum>x<n. e"}: @{text setsum} may
   777 not provide all lemmas available for @{term"{m..<n}"} also in the
   778 special form for @{term"{..<n}"}. *}
   779 
   780 text{* This congruence rule should be used for sums over intervals as
   781 the standard theorem @{text[source]setsum_cong} does not work well
   782 with the simplifier who adds the unsimplified premise @{term"x:B"} to
   783 the context. *}
   784 
   785 lemma setsum_ivl_cong:
   786  "\<lbrakk>a = c; b = d; !!x. \<lbrakk> c \<le> x; x < d \<rbrakk> \<Longrightarrow> f x = g x \<rbrakk> \<Longrightarrow>
   787  setsum f {a..<b} = setsum g {c..<d}"
   788 by(rule setsum_cong, simp_all)
   789 
   790 (* FIXME why are the following simp rules but the corresponding eqns
   791 on intervals are not? *)
   792 
   793 lemma setsum_atMost_Suc[simp]: "(\<Sum>i \<le> Suc n. f i) = (\<Sum>i \<le> n. f i) + f(Suc n)"
   794 by (simp add:atMost_Suc add_ac)
   795 
   796 lemma setsum_lessThan_Suc[simp]: "(\<Sum>i < Suc n. f i) = (\<Sum>i < n. f i) + f n"
   797 by (simp add:lessThan_Suc add_ac)
   798 
   799 lemma setsum_cl_ivl_Suc[simp]:
   800   "setsum f {m..Suc n} = (if Suc n < m then 0 else setsum f {m..n} + f(Suc n))"
   801 by (auto simp:add_ac atLeastAtMostSuc_conv)
   802 
   803 lemma setsum_op_ivl_Suc[simp]:
   804   "setsum f {m..<Suc n} = (if n < m then 0 else setsum f {m..<n} + f(n))"
   805 by (auto simp:add_ac atLeastLessThanSuc)
   806 (*
   807 lemma setsum_cl_ivl_add_one_nat: "(n::nat) <= m + 1 ==>
   808     (\<Sum>i=n..m+1. f i) = (\<Sum>i=n..m. f i) + f(m + 1)"
   809 by (auto simp:add_ac atLeastAtMostSuc_conv)
   810 *)
   811 
   812 lemma setsum_head:
   813   fixes n :: nat
   814   assumes mn: "m <= n" 
   815   shows "(\<Sum>x\<in>{m..n}. P x) = P m + (\<Sum>x\<in>{m<..n}. P x)" (is "?lhs = ?rhs")
   816 proof -
   817   from mn
   818   have "{m..n} = {m} \<union> {m<..n}"
   819     by (auto intro: ivl_disj_un_singleton)
   820   hence "?lhs = (\<Sum>x\<in>{m} \<union> {m<..n}. P x)"
   821     by (simp add: atLeast0LessThan)
   822   also have "\<dots> = ?rhs" by simp
   823   finally show ?thesis .
   824 qed
   825 
   826 lemma setsum_head_Suc:
   827   "m \<le> n \<Longrightarrow> setsum f {m..n} = f m + setsum f {Suc m..n}"
   828 by (simp add: setsum_head atLeastSucAtMost_greaterThanAtMost)
   829 
   830 lemma setsum_head_upt_Suc:
   831   "m < n \<Longrightarrow> setsum f {m..<n} = f m + setsum f {Suc m..<n}"
   832 apply(insert setsum_head_Suc[of m "n - Suc 0" f])
   833 apply (simp add: atLeastLessThanSuc_atLeastAtMost[symmetric] algebra_simps)
   834 done
   835 
   836 
   837 lemma setsum_add_nat_ivl: "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
   838   setsum f {m..<n} + setsum f {n..<p} = setsum f {m..<p::nat}"
   839 by (simp add:setsum_Un_disjoint[symmetric] ivl_disj_int ivl_disj_un)
   840 
   841 lemma setsum_diff_nat_ivl:
   842 fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
   843 shows "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
   844   setsum f {m..<p} - setsum f {m..<n} = setsum f {n..<p}"
   845 using setsum_add_nat_ivl [of m n p f,symmetric]
   846 apply (simp add: add_ac)
   847 done
   848 
   849 
   850 subsection{* Shifting bounds *}
   851 
   852 lemma setsum_shift_bounds_nat_ivl:
   853   "setsum f {m+k..<n+k} = setsum (%i. f(i + k)){m..<n::nat}"
   854 by (induct "n", auto simp:atLeastLessThanSuc)
   855 
   856 lemma setsum_shift_bounds_cl_nat_ivl:
   857   "setsum f {m+k..n+k} = setsum (%i. f(i + k)){m..n::nat}"
   858 apply (insert setsum_reindex[OF inj_on_add_nat, where h=f and B = "{m..n}"])
   859 apply (simp add:image_add_atLeastAtMost o_def)
   860 done
   861 
   862 corollary setsum_shift_bounds_cl_Suc_ivl:
   863   "setsum f {Suc m..Suc n} = setsum (%i. f(Suc i)){m..n}"
   864 by (simp add:setsum_shift_bounds_cl_nat_ivl[where k="Suc 0", simplified])
   865 
   866 corollary setsum_shift_bounds_Suc_ivl:
   867   "setsum f {Suc m..<Suc n} = setsum (%i. f(Suc i)){m..<n}"
   868 by (simp add:setsum_shift_bounds_nat_ivl[where k="Suc 0", simplified])
   869 
   870 lemma setsum_shift_lb_Suc0_0:
   871   "f(0::nat) = (0::nat) \<Longrightarrow> setsum f {Suc 0..k} = setsum f {0..k}"
   872 by(simp add:setsum_head_Suc)
   873 
   874 lemma setsum_shift_lb_Suc0_0_upt:
   875   "f(0::nat) = 0 \<Longrightarrow> setsum f {Suc 0..<k} = setsum f {0..<k}"
   876 apply(cases k)apply simp
   877 apply(simp add:setsum_head_upt_Suc)
   878 done
   879 
   880 subsection {* The formula for geometric sums *}
   881 
   882 lemma geometric_sum:
   883   "x ~= 1 ==> (\<Sum>i=0..<n. x ^ i) =
   884   (x ^ n - 1) / (x - 1::'a::{field})"
   885 by (induct "n") (simp_all add:field_simps power_Suc)
   886 
   887 subsection {* The formula for arithmetic sums *}
   888 
   889 lemma gauss_sum:
   890   "((1::'a::comm_semiring_1) + 1)*(\<Sum>i\<in>{1..n}. of_nat i) =
   891    of_nat n*((of_nat n)+1)"
   892 proof (induct n)
   893   case 0
   894   show ?case by simp
   895 next
   896   case (Suc n)
   897   then show ?case by (simp add: algebra_simps)
   898 qed
   899 
   900 theorem arith_series_general:
   901   "((1::'a::comm_semiring_1) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
   902   of_nat n * (a + (a + of_nat(n - 1)*d))"
   903 proof cases
   904   assume ngt1: "n > 1"
   905   let ?I = "\<lambda>i. of_nat i" and ?n = "of_nat n"
   906   have
   907     "(\<Sum>i\<in>{..<n}. a+?I i*d) =
   908      ((\<Sum>i\<in>{..<n}. a) + (\<Sum>i\<in>{..<n}. ?I i*d))"
   909     by (rule setsum_addf)
   910   also from ngt1 have "\<dots> = ?n*a + (\<Sum>i\<in>{..<n}. ?I i*d)" by simp
   911   also from ngt1 have "\<dots> = (?n*a + d*(\<Sum>i\<in>{1..<n}. ?I i))"
   912     unfolding One_nat_def
   913     by (simp add: setsum_right_distrib atLeast0LessThan[symmetric] setsum_shift_lb_Suc0_0_upt mult_ac)
   914   also have "(1+1)*\<dots> = (1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..<n}. ?I i)"
   915     by (simp add: left_distrib right_distrib)
   916   also from ngt1 have "{1..<n} = {1..n - 1}"
   917     by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost)
   918   also from ngt1
   919   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)"
   920     by (simp only: mult_ac gauss_sum [of "n - 1"], unfold One_nat_def)
   921        (simp add:  mult_ac trans [OF add_commute of_nat_Suc [symmetric]])
   922   finally show ?thesis by (simp add: algebra_simps)
   923 next
   924   assume "\<not>(n > 1)"
   925   hence "n = 1 \<or> n = 0" by auto
   926   thus ?thesis by (auto simp: algebra_simps)
   927 qed
   928 
   929 lemma arith_series_nat:
   930   "Suc (Suc 0) * (\<Sum>i\<in>{..<n}. a+i*d) = n * (a + (a+(n - 1)*d))"
   931 proof -
   932   have
   933     "((1::nat) + 1) * (\<Sum>i\<in>{..<n::nat}. a + of_nat(i)*d) =
   934     of_nat(n) * (a + (a + of_nat(n - 1)*d))"
   935     by (rule arith_series_general)
   936   thus ?thesis
   937     unfolding One_nat_def by (auto simp add: of_nat_id)
   938 qed
   939 
   940 lemma arith_series_int:
   941   "(2::int) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
   942   of_nat n * (a + (a + of_nat(n - 1)*d))"
   943 proof -
   944   have
   945     "((1::int) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
   946     of_nat(n) * (a + (a + of_nat(n - 1)*d))"
   947     by (rule arith_series_general)
   948   thus ?thesis by simp
   949 qed
   950 
   951 lemma sum_diff_distrib:
   952   fixes P::"nat\<Rightarrow>nat"
   953   shows
   954   "\<forall>x. Q x \<le> P x  \<Longrightarrow>
   955   (\<Sum>x<n. P x) - (\<Sum>x<n. Q x) = (\<Sum>x<n. P x - Q x)"
   956 proof (induct n)
   957   case 0 show ?case by simp
   958 next
   959   case (Suc n)
   960 
   961   let ?lhs = "(\<Sum>x<n. P x) - (\<Sum>x<n. Q x)"
   962   let ?rhs = "\<Sum>x<n. P x - Q x"
   963 
   964   from Suc have "?lhs = ?rhs" by simp
   965   moreover
   966   from Suc have "?lhs + P n - Q n = ?rhs + (P n - Q n)" by simp
   967   moreover
   968   from Suc have
   969     "(\<Sum>x<n. P x) + P n - ((\<Sum>x<n. Q x) + Q n) = ?rhs + (P n - Q n)"
   970     by (subst diff_diff_left[symmetric],
   971         subst diff_add_assoc2)
   972        (auto simp: diff_add_assoc2 intro: setsum_mono)
   973   ultimately
   974   show ?case by simp
   975 qed
   976 
   977 subsection {* Products indexed over intervals *}
   978 
   979 syntax
   980   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _ = _.._./ _)" [0,0,0,10] 10)
   981   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _ = _..<_./ _)" [0,0,0,10] 10)
   982   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _<_./ _)" [0,0,10] 10)
   983   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _<=_./ _)" [0,0,10] 10)
   984 syntax (xsymbols)
   985   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _.._./ _)" [0,0,0,10] 10)
   986   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _..<_./ _)" [0,0,0,10] 10)
   987   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_<_./ _)" [0,0,10] 10)
   988   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_\<le>_./ _)" [0,0,10] 10)
   989 syntax (HTML output)
   990   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _.._./ _)" [0,0,0,10] 10)
   991   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _..<_./ _)" [0,0,0,10] 10)
   992   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_<_./ _)" [0,0,10] 10)
   993   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_\<le>_./ _)" [0,0,10] 10)
   994 syntax (latex_prod output)
   995   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
   996  ("(3\<^raw:$\prod_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
   997   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
   998  ("(3\<^raw:$\prod_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
   999   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1000  ("(3\<^raw:$\prod_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
  1001   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1002  ("(3\<^raw:$\prod_{>_ \<le> _\<^raw:}$> _)" [0,0,10] 10)
  1003 
  1004 translations
  1005   "\<Prod>x=a..b. t" == "CONST setprod (%x. t) {a..b}"
  1006   "\<Prod>x=a..<b. t" == "CONST setprod (%x. t) {a..<b}"
  1007   "\<Prod>i\<le>n. t" == "CONST setprod (\<lambda>i. t) {..n}"
  1008   "\<Prod>i<n. t" == "CONST setprod (\<lambda>i. t) {..<n}"
  1009 
  1010 end