src/HOL/Set_Interval.thy
author eberlm
Mon Jan 11 16:38:39 2016 +0100 (2016-01-11)
changeset 62128 3201ddb00097
parent 61955 e96292f32c3c
child 62343 24106dc44def
permissions -rw-r--r--
Integrated some material from Algebraic_Numbers AFP entry to Polynomials; generalised some polynomial stuff.
     1 (*  Title:      HOL/Set_Interval.thy
     2     Author:     Tobias Nipkow
     3     Author:     Clemens Ballarin
     4     Author:     Jeremy Avigad
     5 
     6 lessThan, greaterThan, atLeast, atMost and two-sided intervals
     7 
     8 Modern convention: Ixy stands for an interval where x and y
     9 describe the lower and upper bound and x,y : {c,o,i}
    10 where c = closed, o = open, i = infinite.
    11 Examples: Ico = {_ ..< _} and Ici = {_ ..}
    12 *)
    13 
    14 section \<open>Set intervals\<close>
    15 
    16 theory Set_Interval
    17 imports Lattices_Big Nat_Transfer
    18 begin
    19 
    20 context ord
    21 begin
    22 
    23 definition
    24   lessThan    :: "'a => 'a set" ("(1{..<_})") where
    25   "{..<u} == {x. x < u}"
    26 
    27 definition
    28   atMost      :: "'a => 'a set" ("(1{.._})") where
    29   "{..u} == {x. x \<le> u}"
    30 
    31 definition
    32   greaterThan :: "'a => 'a set" ("(1{_<..})") where
    33   "{l<..} == {x. l<x}"
    34 
    35 definition
    36   atLeast     :: "'a => 'a set" ("(1{_..})") where
    37   "{l..} == {x. l\<le>x}"
    38 
    39 definition
    40   greaterThanLessThan :: "'a => 'a => 'a set"  ("(1{_<..<_})") where
    41   "{l<..<u} == {l<..} Int {..<u}"
    42 
    43 definition
    44   atLeastLessThan :: "'a => 'a => 'a set"      ("(1{_..<_})") where
    45   "{l..<u} == {l..} Int {..<u}"
    46 
    47 definition
    48   greaterThanAtMost :: "'a => 'a => 'a set"    ("(1{_<.._})") where
    49   "{l<..u} == {l<..} Int {..u}"
    50 
    51 definition
    52   atLeastAtMost :: "'a => 'a => 'a set"        ("(1{_.._})") where
    53   "{l..u} == {l..} Int {..u}"
    54 
    55 end
    56 
    57 
    58 text\<open>A note of warning when using @{term"{..<n}"} on type @{typ
    59 nat}: it is equivalent to @{term"{0::nat..<n}"} but some lemmas involving
    60 @{term"{m..<n}"} may not exist in @{term"{..<n}"}-form as well.\<close>
    61 
    62 syntax (ASCII)
    63   "_UNION_le"   :: "'a => 'a => 'b set => 'b set"       ("(3UN _<=_./ _)" [0, 0, 10] 10)
    64   "_UNION_less" :: "'a => 'a => 'b set => 'b set"       ("(3UN _<_./ _)" [0, 0, 10] 10)
    65   "_INTER_le"   :: "'a => 'a => 'b set => 'b set"       ("(3INT _<=_./ _)" [0, 0, 10] 10)
    66   "_INTER_less" :: "'a => 'a => 'b set => 'b set"       ("(3INT _<_./ _)" [0, 0, 10] 10)
    67 
    68 syntax (latex output)
    69   "_UNION_le"   :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Union>(00_ \<le> _)/ _)" [0, 0, 10] 10)
    70   "_UNION_less" :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Union>(00_ < _)/ _)" [0, 0, 10] 10)
    71   "_INTER_le"   :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Inter>(00_ \<le> _)/ _)" [0, 0, 10] 10)
    72   "_INTER_less" :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Inter>(00_ < _)/ _)" [0, 0, 10] 10)
    73 
    74 syntax
    75   "_UNION_le"   :: "'a => 'a => 'b set => 'b set"       ("(3\<Union>_\<le>_./ _)" [0, 0, 10] 10)
    76   "_UNION_less" :: "'a => 'a => 'b set => 'b set"       ("(3\<Union>_<_./ _)" [0, 0, 10] 10)
    77   "_INTER_le"   :: "'a => 'a => 'b set => 'b set"       ("(3\<Inter>_\<le>_./ _)" [0, 0, 10] 10)
    78   "_INTER_less" :: "'a => 'a => 'b set => 'b set"       ("(3\<Inter>_<_./ _)" [0, 0, 10] 10)
    79 
    80 translations
    81   "\<Union>i\<le>n. A" \<rightleftharpoons> "\<Union>i\<in>{..n}. A"
    82   "\<Union>i<n. A" \<rightleftharpoons> "\<Union>i\<in>{..<n}. A"
    83   "\<Inter>i\<le>n. A" \<rightleftharpoons> "\<Inter>i\<in>{..n}. A"
    84   "\<Inter>i<n. A" \<rightleftharpoons> "\<Inter>i\<in>{..<n}. A"
    85 
    86 
    87 subsection \<open>Various equivalences\<close>
    88 
    89 lemma (in ord) lessThan_iff [iff]: "(i: lessThan k) = (i<k)"
    90 by (simp add: lessThan_def)
    91 
    92 lemma Compl_lessThan [simp]:
    93     "!!k:: 'a::linorder. -lessThan k = atLeast k"
    94 apply (auto simp add: lessThan_def atLeast_def)
    95 done
    96 
    97 lemma single_Diff_lessThan [simp]: "!!k:: 'a::order. {k} - lessThan k = {k}"
    98 by auto
    99 
   100 lemma (in ord) greaterThan_iff [iff]: "(i: greaterThan k) = (k<i)"
   101 by (simp add: greaterThan_def)
   102 
   103 lemma Compl_greaterThan [simp]:
   104     "!!k:: 'a::linorder. -greaterThan k = atMost k"
   105   by (auto simp add: greaterThan_def atMost_def)
   106 
   107 lemma Compl_atMost [simp]: "!!k:: 'a::linorder. -atMost k = greaterThan k"
   108 apply (subst Compl_greaterThan [symmetric])
   109 apply (rule double_complement)
   110 done
   111 
   112 lemma (in ord) atLeast_iff [iff]: "(i: atLeast k) = (k<=i)"
   113 by (simp add: atLeast_def)
   114 
   115 lemma Compl_atLeast [simp]:
   116     "!!k:: 'a::linorder. -atLeast k = lessThan k"
   117   by (auto simp add: lessThan_def atLeast_def)
   118 
   119 lemma (in ord) atMost_iff [iff]: "(i: atMost k) = (i<=k)"
   120 by (simp add: atMost_def)
   121 
   122 lemma atMost_Int_atLeast: "!!n:: 'a::order. atMost n Int atLeast n = {n}"
   123 by (blast intro: order_antisym)
   124 
   125 lemma (in linorder) lessThan_Int_lessThan: "{ a <..} \<inter> { b <..} = { max a b <..}"
   126   by auto
   127 
   128 lemma (in linorder) greaterThan_Int_greaterThan: "{..< a} \<inter> {..< b} = {..< min a b}"
   129   by auto
   130 
   131 subsection \<open>Logical Equivalences for Set Inclusion and Equality\<close>
   132 
   133 lemma atLeast_subset_iff [iff]:
   134      "(atLeast x \<subseteq> atLeast y) = (y \<le> (x::'a::order))"
   135 by (blast intro: order_trans)
   136 
   137 lemma atLeast_eq_iff [iff]:
   138      "(atLeast x = atLeast y) = (x = (y::'a::linorder))"
   139 by (blast intro: order_antisym order_trans)
   140 
   141 lemma greaterThan_subset_iff [iff]:
   142      "(greaterThan x \<subseteq> greaterThan y) = (y \<le> (x::'a::linorder))"
   143 apply (auto simp add: greaterThan_def)
   144  apply (subst linorder_not_less [symmetric], blast)
   145 done
   146 
   147 lemma greaterThan_eq_iff [iff]:
   148      "(greaterThan x = greaterThan y) = (x = (y::'a::linorder))"
   149 apply (rule iffI)
   150  apply (erule equalityE)
   151  apply simp_all
   152 done
   153 
   154 lemma atMost_subset_iff [iff]: "(atMost x \<subseteq> atMost y) = (x \<le> (y::'a::order))"
   155 by (blast intro: order_trans)
   156 
   157 lemma atMost_eq_iff [iff]: "(atMost x = atMost y) = (x = (y::'a::linorder))"
   158 by (blast intro: order_antisym order_trans)
   159 
   160 lemma lessThan_subset_iff [iff]:
   161      "(lessThan x \<subseteq> lessThan y) = (x \<le> (y::'a::linorder))"
   162 apply (auto simp add: lessThan_def)
   163  apply (subst linorder_not_less [symmetric], blast)
   164 done
   165 
   166 lemma lessThan_eq_iff [iff]:
   167      "(lessThan x = lessThan y) = (x = (y::'a::linorder))"
   168 apply (rule iffI)
   169  apply (erule equalityE)
   170  apply simp_all
   171 done
   172 
   173 lemma lessThan_strict_subset_iff:
   174   fixes m n :: "'a::linorder"
   175   shows "{..<m} < {..<n} \<longleftrightarrow> m < n"
   176   by (metis leD lessThan_subset_iff linorder_linear not_less_iff_gr_or_eq psubset_eq)
   177 
   178 lemma (in linorder) Ici_subset_Ioi_iff: "{a ..} \<subseteq> {b <..} \<longleftrightarrow> b < a"
   179   by auto
   180 
   181 lemma (in linorder) Iic_subset_Iio_iff: "{.. a} \<subseteq> {..< b} \<longleftrightarrow> a < b"
   182   by auto
   183 
   184 subsection \<open>Two-sided intervals\<close>
   185 
   186 context ord
   187 begin
   188 
   189 lemma greaterThanLessThan_iff [simp]:
   190   "(i : {l<..<u}) = (l < i & i < u)"
   191 by (simp add: greaterThanLessThan_def)
   192 
   193 lemma atLeastLessThan_iff [simp]:
   194   "(i : {l..<u}) = (l <= i & i < u)"
   195 by (simp add: atLeastLessThan_def)
   196 
   197 lemma greaterThanAtMost_iff [simp]:
   198   "(i : {l<..u}) = (l < i & i <= u)"
   199 by (simp add: greaterThanAtMost_def)
   200 
   201 lemma atLeastAtMost_iff [simp]:
   202   "(i : {l..u}) = (l <= i & i <= u)"
   203 by (simp add: atLeastAtMost_def)
   204 
   205 text \<open>The above four lemmas could be declared as iffs. Unfortunately this
   206 breaks many proofs. Since it only helps blast, it is better to leave them
   207 alone.\<close>
   208 
   209 lemma greaterThanLessThan_eq: "{ a <..< b} = { a <..} \<inter> {..< b }"
   210   by auto
   211 
   212 end
   213 
   214 subsubsection\<open>Emptyness, singletons, subset\<close>
   215 
   216 context order
   217 begin
   218 
   219 lemma atLeastatMost_empty[simp]:
   220   "b < a \<Longrightarrow> {a..b} = {}"
   221 by(auto simp: atLeastAtMost_def atLeast_def atMost_def)
   222 
   223 lemma atLeastatMost_empty_iff[simp]:
   224   "{a..b} = {} \<longleftrightarrow> (~ a <= b)"
   225 by auto (blast intro: order_trans)
   226 
   227 lemma atLeastatMost_empty_iff2[simp]:
   228   "{} = {a..b} \<longleftrightarrow> (~ a <= b)"
   229 by auto (blast intro: order_trans)
   230 
   231 lemma atLeastLessThan_empty[simp]:
   232   "b <= a \<Longrightarrow> {a..<b} = {}"
   233 by(auto simp: atLeastLessThan_def)
   234 
   235 lemma atLeastLessThan_empty_iff[simp]:
   236   "{a..<b} = {} \<longleftrightarrow> (~ a < b)"
   237 by auto (blast intro: le_less_trans)
   238 
   239 lemma atLeastLessThan_empty_iff2[simp]:
   240   "{} = {a..<b} \<longleftrightarrow> (~ a < b)"
   241 by auto (blast intro: le_less_trans)
   242 
   243 lemma greaterThanAtMost_empty[simp]: "l \<le> k ==> {k<..l} = {}"
   244 by(auto simp:greaterThanAtMost_def greaterThan_def atMost_def)
   245 
   246 lemma greaterThanAtMost_empty_iff[simp]: "{k<..l} = {} \<longleftrightarrow> ~ k < l"
   247 by auto (blast intro: less_le_trans)
   248 
   249 lemma greaterThanAtMost_empty_iff2[simp]: "{} = {k<..l} \<longleftrightarrow> ~ k < l"
   250 by auto (blast intro: less_le_trans)
   251 
   252 lemma greaterThanLessThan_empty[simp]:"l \<le> k ==> {k<..<l} = {}"
   253 by(auto simp:greaterThanLessThan_def greaterThan_def lessThan_def)
   254 
   255 lemma atLeastAtMost_singleton [simp]: "{a..a} = {a}"
   256 by (auto simp add: atLeastAtMost_def atMost_def atLeast_def)
   257 
   258 lemma atLeastAtMost_singleton': "a = b \<Longrightarrow> {a .. b} = {a}" by simp
   259 
   260 lemma atLeastatMost_subset_iff[simp]:
   261   "{a..b} <= {c..d} \<longleftrightarrow> (~ a <= b) | c <= a & b <= d"
   262 unfolding atLeastAtMost_def atLeast_def atMost_def
   263 by (blast intro: order_trans)
   264 
   265 lemma atLeastatMost_psubset_iff:
   266   "{a..b} < {c..d} \<longleftrightarrow>
   267    ((~ a <= b) | c <= a & b <= d & (c < a | b < d))  &  c <= d"
   268 by(simp add: psubset_eq set_eq_iff less_le_not_le)(blast intro: order_trans)
   269 
   270 lemma Icc_eq_Icc[simp]:
   271   "{l..h} = {l'..h'} = (l=l' \<and> h=h' \<or> \<not> l\<le>h \<and> \<not> l'\<le>h')"
   272 by(simp add: order_class.eq_iff)(auto intro: order_trans)
   273 
   274 lemma atLeastAtMost_singleton_iff[simp]:
   275   "{a .. b} = {c} \<longleftrightarrow> a = b \<and> b = c"
   276 proof
   277   assume "{a..b} = {c}"
   278   hence *: "\<not> (\<not> a \<le> b)" unfolding atLeastatMost_empty_iff[symmetric] by simp
   279   with \<open>{a..b} = {c}\<close> have "c \<le> a \<and> b \<le> c" by auto
   280   with * show "a = b \<and> b = c" by auto
   281 qed simp
   282 
   283 lemma Icc_subset_Ici_iff[simp]:
   284   "{l..h} \<subseteq> {l'..} = (~ l\<le>h \<or> l\<ge>l')"
   285 by(auto simp: subset_eq intro: order_trans)
   286 
   287 lemma Icc_subset_Iic_iff[simp]:
   288   "{l..h} \<subseteq> {..h'} = (~ l\<le>h \<or> h\<le>h')"
   289 by(auto simp: subset_eq intro: order_trans)
   290 
   291 lemma not_Ici_eq_empty[simp]: "{l..} \<noteq> {}"
   292 by(auto simp: set_eq_iff)
   293 
   294 lemma not_Iic_eq_empty[simp]: "{..h} \<noteq> {}"
   295 by(auto simp: set_eq_iff)
   296 
   297 lemmas not_empty_eq_Ici_eq_empty[simp] = not_Ici_eq_empty[symmetric]
   298 lemmas not_empty_eq_Iic_eq_empty[simp] = not_Iic_eq_empty[symmetric]
   299 
   300 end
   301 
   302 context no_top
   303 begin
   304 
   305 (* also holds for no_bot but no_top should suffice *)
   306 lemma not_UNIV_le_Icc[simp]: "\<not> UNIV \<subseteq> {l..h}"
   307 using gt_ex[of h] by(auto simp: subset_eq less_le_not_le)
   308 
   309 lemma not_UNIV_le_Iic[simp]: "\<not> UNIV \<subseteq> {..h}"
   310 using gt_ex[of h] by(auto simp: subset_eq less_le_not_le)
   311 
   312 lemma not_Ici_le_Icc[simp]: "\<not> {l..} \<subseteq> {l'..h'}"
   313 using gt_ex[of h']
   314 by(auto simp: subset_eq less_le)(blast dest:antisym_conv intro: order_trans)
   315 
   316 lemma not_Ici_le_Iic[simp]: "\<not> {l..} \<subseteq> {..h'}"
   317 using gt_ex[of h']
   318 by(auto simp: subset_eq less_le)(blast dest:antisym_conv intro: order_trans)
   319 
   320 end
   321 
   322 context no_bot
   323 begin
   324 
   325 lemma not_UNIV_le_Ici[simp]: "\<not> UNIV \<subseteq> {l..}"
   326 using lt_ex[of l] by(auto simp: subset_eq less_le_not_le)
   327 
   328 lemma not_Iic_le_Icc[simp]: "\<not> {..h} \<subseteq> {l'..h'}"
   329 using lt_ex[of l']
   330 by(auto simp: subset_eq less_le)(blast dest:antisym_conv intro: order_trans)
   331 
   332 lemma not_Iic_le_Ici[simp]: "\<not> {..h} \<subseteq> {l'..}"
   333 using lt_ex[of l']
   334 by(auto simp: subset_eq less_le)(blast dest:antisym_conv intro: order_trans)
   335 
   336 end
   337 
   338 
   339 context no_top
   340 begin
   341 
   342 (* also holds for no_bot but no_top should suffice *)
   343 lemma not_UNIV_eq_Icc[simp]: "\<not> UNIV = {l'..h'}"
   344 using gt_ex[of h'] by(auto simp: set_eq_iff  less_le_not_le)
   345 
   346 lemmas not_Icc_eq_UNIV[simp] = not_UNIV_eq_Icc[symmetric]
   347 
   348 lemma not_UNIV_eq_Iic[simp]: "\<not> UNIV = {..h'}"
   349 using gt_ex[of h'] by(auto simp: set_eq_iff  less_le_not_le)
   350 
   351 lemmas not_Iic_eq_UNIV[simp] = not_UNIV_eq_Iic[symmetric]
   352 
   353 lemma not_Icc_eq_Ici[simp]: "\<not> {l..h} = {l'..}"
   354 unfolding atLeastAtMost_def using not_Ici_le_Iic[of l'] by blast
   355 
   356 lemmas not_Ici_eq_Icc[simp] = not_Icc_eq_Ici[symmetric]
   357 
   358 (* also holds for no_bot but no_top should suffice *)
   359 lemma not_Iic_eq_Ici[simp]: "\<not> {..h} = {l'..}"
   360 using not_Ici_le_Iic[of l' h] by blast
   361 
   362 lemmas not_Ici_eq_Iic[simp] = not_Iic_eq_Ici[symmetric]
   363 
   364 end
   365 
   366 context no_bot
   367 begin
   368 
   369 lemma not_UNIV_eq_Ici[simp]: "\<not> UNIV = {l'..}"
   370 using lt_ex[of l'] by(auto simp: set_eq_iff  less_le_not_le)
   371 
   372 lemmas not_Ici_eq_UNIV[simp] = not_UNIV_eq_Ici[symmetric]
   373 
   374 lemma not_Icc_eq_Iic[simp]: "\<not> {l..h} = {..h'}"
   375 unfolding atLeastAtMost_def using not_Iic_le_Ici[of h'] by blast
   376 
   377 lemmas not_Iic_eq_Icc[simp] = not_Icc_eq_Iic[symmetric]
   378 
   379 end
   380 
   381 
   382 context dense_linorder
   383 begin
   384 
   385 lemma greaterThanLessThan_empty_iff[simp]:
   386   "{ a <..< b } = {} \<longleftrightarrow> b \<le> a"
   387   using dense[of a b] by (cases "a < b") auto
   388 
   389 lemma greaterThanLessThan_empty_iff2[simp]:
   390   "{} = { a <..< b } \<longleftrightarrow> b \<le> a"
   391   using dense[of a b] by (cases "a < b") auto
   392 
   393 lemma atLeastLessThan_subseteq_atLeastAtMost_iff:
   394   "{a ..< b} \<subseteq> { c .. d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b \<le> d)"
   395   using dense[of "max a d" "b"]
   396   by (force simp: subset_eq Ball_def not_less[symmetric])
   397 
   398 lemma greaterThanAtMost_subseteq_atLeastAtMost_iff:
   399   "{a <.. b} \<subseteq> { c .. d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b \<le> d)"
   400   using dense[of "a" "min c b"]
   401   by (force simp: subset_eq Ball_def not_less[symmetric])
   402 
   403 lemma greaterThanLessThan_subseteq_atLeastAtMost_iff:
   404   "{a <..< b} \<subseteq> { c .. d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b \<le> d)"
   405   using dense[of "a" "min c b"] dense[of "max a d" "b"]
   406   by (force simp: subset_eq Ball_def not_less[symmetric])
   407 
   408 lemma atLeastAtMost_subseteq_atLeastLessThan_iff:
   409   "{a .. b} \<subseteq> { c ..< d } \<longleftrightarrow> (a \<le> b \<longrightarrow> c \<le> a \<and> b < d)"
   410   using dense[of "max a d" "b"]
   411   by (force simp: subset_eq Ball_def not_less[symmetric])
   412   
   413 lemma greaterThanLessThan_subseteq_greaterThanLessThan:
   414   "{a <..< b} \<subseteq> {c <..< d} \<longleftrightarrow> (a < b \<longrightarrow> a \<ge> c \<and> b \<le> d)"
   415   using dense[of "a" "min c b"] dense[of "max a d" "b"]
   416   by (force simp: subset_eq Ball_def not_less[symmetric])
   417 
   418 lemma greaterThanAtMost_subseteq_atLeastLessThan_iff:
   419   "{a <.. b} \<subseteq> { c ..< d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b < d)"
   420   using dense[of "a" "min c b"]
   421   by (force simp: subset_eq Ball_def not_less[symmetric])
   422 
   423 lemma greaterThanLessThan_subseteq_atLeastLessThan_iff:
   424   "{a <..< b} \<subseteq> { c ..< d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b \<le> d)"
   425   using dense[of "a" "min c b"] dense[of "max a d" "b"]
   426   by (force simp: subset_eq Ball_def not_less[symmetric])
   427 
   428 lemma greaterThanLessThan_subseteq_greaterThanAtMost_iff:
   429   "{a <..< b} \<subseteq> { c <.. d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b \<le> d)"
   430   using dense[of "a" "min c b"] dense[of "max a d" "b"]
   431   by (force simp: subset_eq Ball_def not_less[symmetric])
   432 
   433 end
   434 
   435 context no_top
   436 begin
   437 
   438 lemma greaterThan_non_empty[simp]: "{x <..} \<noteq> {}"
   439   using gt_ex[of x] by auto
   440 
   441 end
   442 
   443 context no_bot
   444 begin
   445 
   446 lemma lessThan_non_empty[simp]: "{..< x} \<noteq> {}"
   447   using lt_ex[of x] by auto
   448 
   449 end
   450 
   451 lemma (in linorder) atLeastLessThan_subset_iff:
   452   "{a..<b} <= {c..<d} \<Longrightarrow> b <= a | c<=a & b<=d"
   453 apply (auto simp:subset_eq Ball_def)
   454 apply(frule_tac x=a in spec)
   455 apply(erule_tac x=d in allE)
   456 apply (simp add: less_imp_le)
   457 done
   458 
   459 lemma atLeastLessThan_inj:
   460   fixes a b c d :: "'a::linorder"
   461   assumes eq: "{a ..< b} = {c ..< d}" and "a < b" "c < d"
   462   shows "a = c" "b = d"
   463 using assms by (metis atLeastLessThan_subset_iff eq less_le_not_le linorder_antisym_conv2 subset_refl)+
   464 
   465 lemma atLeastLessThan_eq_iff:
   466   fixes a b c d :: "'a::linorder"
   467   assumes "a < b" "c < d"
   468   shows "{a ..< b} = {c ..< d} \<longleftrightarrow> a = c \<and> b = d"
   469   using atLeastLessThan_inj assms by auto
   470 
   471 lemma (in linorder) Ioc_inj: "{a <.. b} = {c <.. d} \<longleftrightarrow> (b \<le> a \<and> d \<le> c) \<or> a = c \<and> b = d"
   472   by (metis eq_iff greaterThanAtMost_empty_iff2 greaterThanAtMost_iff le_cases not_le)
   473 
   474 lemma (in order) Iio_Int_singleton: "{..<k} \<inter> {x} = (if x < k then {x} else {})"
   475   by auto
   476 
   477 lemma (in linorder) Ioc_subset_iff: "{a<..b} \<subseteq> {c<..d} \<longleftrightarrow> (b \<le> a \<or> c \<le> a \<and> b \<le> d)"
   478   by (auto simp: subset_eq Ball_def) (metis less_le not_less)
   479 
   480 lemma (in order_bot) atLeast_eq_UNIV_iff: "{x..} = UNIV \<longleftrightarrow> x = bot"
   481 by (auto simp: set_eq_iff intro: le_bot)
   482 
   483 lemma (in order_top) atMost_eq_UNIV_iff: "{..x} = UNIV \<longleftrightarrow> x = top"
   484 by (auto simp: set_eq_iff intro: top_le)
   485 
   486 lemma (in bounded_lattice) atLeastAtMost_eq_UNIV_iff:
   487   "{x..y} = UNIV \<longleftrightarrow> (x = bot \<and> y = top)"
   488 by (auto simp: set_eq_iff intro: top_le le_bot)
   489 
   490 lemma Iio_eq_empty_iff: "{..< n::'a::{linorder, order_bot}} = {} \<longleftrightarrow> n = bot"
   491   by (auto simp: set_eq_iff not_less le_bot)
   492 
   493 lemma Iio_eq_empty_iff_nat: "{..< n::nat} = {} \<longleftrightarrow> n = 0"
   494   by (simp add: Iio_eq_empty_iff bot_nat_def)
   495 
   496 lemma mono_image_least:
   497   assumes f_mono: "mono f" and f_img: "f ` {m ..< n} = {m' ..< n'}" "m < n"
   498   shows "f m = m'"
   499 proof -
   500   from f_img have "{m' ..< n'} \<noteq> {}"
   501     by (metis atLeastLessThan_empty_iff image_is_empty)
   502   with f_img have "m' \<in> f ` {m ..< n}" by auto
   503   then obtain k where "f k = m'" "m \<le> k" by auto
   504   moreover have "m' \<le> f m" using f_img by auto
   505   ultimately show "f m = m'"
   506     using f_mono by (auto elim: monoE[where x=m and y=k])
   507 qed
   508 
   509 
   510 subsection \<open>Infinite intervals\<close>
   511 
   512 context dense_linorder
   513 begin
   514 
   515 lemma infinite_Ioo:
   516   assumes "a < b"
   517   shows "\<not> finite {a<..<b}"
   518 proof
   519   assume fin: "finite {a<..<b}"
   520   moreover have ne: "{a<..<b} \<noteq> {}"
   521     using \<open>a < b\<close> by auto
   522   ultimately have "a < Max {a <..< b}" "Max {a <..< b} < b"
   523     using Max_in[of "{a <..< b}"] by auto
   524   then obtain x where "Max {a <..< b} < x" "x < b"
   525     using dense[of "Max {a<..<b}" b] by auto
   526   then have "x \<in> {a <..< b}"
   527     using \<open>a < Max {a <..< b}\<close> by auto
   528   then have "x \<le> Max {a <..< b}"
   529     using fin by auto
   530   with \<open>Max {a <..< b} < x\<close> show False by auto
   531 qed
   532 
   533 lemma infinite_Icc: "a < b \<Longrightarrow> \<not> finite {a .. b}"
   534   using greaterThanLessThan_subseteq_atLeastAtMost_iff[of a b a b] infinite_Ioo[of a b]
   535   by (auto dest: finite_subset)
   536 
   537 lemma infinite_Ico: "a < b \<Longrightarrow> \<not> finite {a ..< b}"
   538   using greaterThanLessThan_subseteq_atLeastLessThan_iff[of a b a b] infinite_Ioo[of a b]
   539   by (auto dest: finite_subset)
   540 
   541 lemma infinite_Ioc: "a < b \<Longrightarrow> \<not> finite {a <.. b}"
   542   using greaterThanLessThan_subseteq_greaterThanAtMost_iff[of a b a b] infinite_Ioo[of a b]
   543   by (auto dest: finite_subset)
   544 
   545 end
   546 
   547 lemma infinite_Iio: "\<not> finite {..< a :: 'a :: {no_bot, linorder}}"
   548 proof
   549   assume "finite {..< a}"
   550   then have *: "\<And>x. x < a \<Longrightarrow> Min {..< a} \<le> x"
   551     by auto
   552   obtain x where "x < a"
   553     using lt_ex by auto
   554 
   555   obtain y where "y < Min {..< a}"
   556     using lt_ex by auto
   557   also have "Min {..< a} \<le> x"
   558     using \<open>x < a\<close> by fact
   559   also note \<open>x < a\<close>
   560   finally have "Min {..< a} \<le> y"
   561     by fact
   562   with \<open>y < Min {..< a}\<close> show False by auto
   563 qed
   564 
   565 lemma infinite_Iic: "\<not> finite {.. a :: 'a :: {no_bot, linorder}}"
   566   using infinite_Iio[of a] finite_subset[of "{..< a}" "{.. a}"]
   567   by (auto simp: subset_eq less_imp_le)
   568 
   569 lemma infinite_Ioi: "\<not> finite {a :: 'a :: {no_top, linorder} <..}"
   570 proof
   571   assume "finite {a <..}"
   572   then have *: "\<And>x. a < x \<Longrightarrow> x \<le> Max {a <..}"
   573     by auto
   574 
   575   obtain y where "Max {a <..} < y"
   576     using gt_ex by auto
   577 
   578   obtain x where "a < x"
   579     using gt_ex by auto
   580   also then have "x \<le> Max {a <..}"
   581     by fact
   582   also note \<open>Max {a <..} < y\<close>
   583   finally have "y \<le> Max { a <..}"
   584     by fact
   585   with \<open>Max {a <..} < y\<close> show False by auto
   586 qed
   587 
   588 lemma infinite_Ici: "\<not> finite {a :: 'a :: {no_top, linorder} ..}"
   589   using infinite_Ioi[of a] finite_subset[of "{a <..}" "{a ..}"]
   590   by (auto simp: subset_eq less_imp_le)
   591 
   592 subsubsection \<open>Intersection\<close>
   593 
   594 context linorder
   595 begin
   596 
   597 lemma Int_atLeastAtMost[simp]: "{a..b} Int {c..d} = {max a c .. min b d}"
   598 by auto
   599 
   600 lemma Int_atLeastAtMostR1[simp]: "{..b} Int {c..d} = {c .. min b d}"
   601 by auto
   602 
   603 lemma Int_atLeastAtMostR2[simp]: "{a..} Int {c..d} = {max a c .. d}"
   604 by auto
   605 
   606 lemma Int_atLeastAtMostL1[simp]: "{a..b} Int {..d} = {a .. min b d}"
   607 by auto
   608 
   609 lemma Int_atLeastAtMostL2[simp]: "{a..b} Int {c..} = {max a c .. b}"
   610 by auto
   611 
   612 lemma Int_atLeastLessThan[simp]: "{a..<b} Int {c..<d} = {max a c ..< min b d}"
   613 by auto
   614 
   615 lemma Int_greaterThanAtMost[simp]: "{a<..b} Int {c<..d} = {max a c <.. min b d}"
   616 by auto
   617 
   618 lemma Int_greaterThanLessThan[simp]: "{a<..<b} Int {c<..<d} = {max a c <..< min b d}"
   619 by auto
   620 
   621 lemma Int_atMost[simp]: "{..a} \<inter> {..b} = {.. min a b}"
   622   by (auto simp: min_def)
   623 
   624 lemma Ioc_disjoint: "{a<..b} \<inter> {c<..d} = {} \<longleftrightarrow> b \<le> a \<or> d \<le> c \<or> b \<le> c \<or> d \<le> a"
   625   using assms by auto
   626 
   627 end
   628 
   629 context complete_lattice
   630 begin
   631 
   632 lemma
   633   shows Sup_atLeast[simp]: "Sup {x ..} = top"
   634     and Sup_greaterThanAtLeast[simp]: "x < top \<Longrightarrow> Sup {x <..} = top"
   635     and Sup_atMost[simp]: "Sup {.. y} = y"
   636     and Sup_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Sup { x .. y} = y"
   637     and Sup_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Sup { x <.. y} = y"
   638   by (auto intro!: Sup_eqI)
   639 
   640 lemma
   641   shows Inf_atMost[simp]: "Inf {.. x} = bot"
   642     and Inf_atMostLessThan[simp]: "top < x \<Longrightarrow> Inf {..< x} = bot"
   643     and Inf_atLeast[simp]: "Inf {x ..} = x"
   644     and Inf_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Inf { x .. y} = x"
   645     and Inf_atLeastLessThan[simp]: "x < y \<Longrightarrow> Inf { x ..< y} = x"
   646   by (auto intro!: Inf_eqI)
   647 
   648 end
   649 
   650 lemma
   651   fixes x y :: "'a :: {complete_lattice, dense_linorder}"
   652   shows Sup_lessThan[simp]: "Sup {..< y} = y"
   653     and Sup_atLeastLessThan[simp]: "x < y \<Longrightarrow> Sup { x ..< y} = y"
   654     and Sup_greaterThanLessThan[simp]: "x < y \<Longrightarrow> Sup { x <..< y} = y"
   655     and Inf_greaterThan[simp]: "Inf {x <..} = x"
   656     and Inf_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Inf { x <.. y} = x"
   657     and Inf_greaterThanLessThan[simp]: "x < y \<Longrightarrow> Inf { x <..< y} = x"
   658   by (auto intro!: Inf_eqI Sup_eqI intro: dense_le dense_le_bounded dense_ge dense_ge_bounded)
   659 
   660 subsection \<open>Intervals of natural numbers\<close>
   661 
   662 subsubsection \<open>The Constant @{term lessThan}\<close>
   663 
   664 lemma lessThan_0 [simp]: "lessThan (0::nat) = {}"
   665 by (simp add: lessThan_def)
   666 
   667 lemma lessThan_Suc: "lessThan (Suc k) = insert k (lessThan k)"
   668 by (simp add: lessThan_def less_Suc_eq, blast)
   669 
   670 text \<open>The following proof is convenient in induction proofs where
   671 new elements get indices at the beginning. So it is used to transform
   672 @{term "{..<Suc n}"} to @{term "0::nat"} and @{term "{..< n}"}.\<close>
   673 
   674 lemma zero_notin_Suc_image: "0 \<notin> Suc ` A"
   675   by auto
   676 
   677 lemma lessThan_Suc_eq_insert_0: "{..<Suc n} = insert 0 (Suc ` {..<n})"
   678   by (auto simp: image_iff less_Suc_eq_0_disj)
   679 
   680 lemma lessThan_Suc_atMost: "lessThan (Suc k) = atMost k"
   681 by (simp add: lessThan_def atMost_def less_Suc_eq_le)
   682 
   683 lemma Iic_Suc_eq_insert_0: "{.. Suc n} = insert 0 (Suc ` {.. n})"
   684   unfolding lessThan_Suc_atMost[symmetric] lessThan_Suc_eq_insert_0[of "Suc n"] ..
   685 
   686 lemma UN_lessThan_UNIV: "(UN m::nat. lessThan m) = UNIV"
   687 by blast
   688 
   689 subsubsection \<open>The Constant @{term greaterThan}\<close>
   690 
   691 lemma greaterThan_0 [simp]: "greaterThan 0 = range Suc"
   692 apply (simp add: greaterThan_def)
   693 apply (blast dest: gr0_conv_Suc [THEN iffD1])
   694 done
   695 
   696 lemma greaterThan_Suc: "greaterThan (Suc k) = greaterThan k - {Suc k}"
   697 apply (simp add: greaterThan_def)
   698 apply (auto elim: linorder_neqE)
   699 done
   700 
   701 lemma INT_greaterThan_UNIV: "(INT m::nat. greaterThan m) = {}"
   702 by blast
   703 
   704 subsubsection \<open>The Constant @{term atLeast}\<close>
   705 
   706 lemma atLeast_0 [simp]: "atLeast (0::nat) = UNIV"
   707 by (unfold atLeast_def UNIV_def, simp)
   708 
   709 lemma atLeast_Suc: "atLeast (Suc k) = atLeast k - {k}"
   710 apply (simp add: atLeast_def)
   711 apply (simp add: Suc_le_eq)
   712 apply (simp add: order_le_less, blast)
   713 done
   714 
   715 lemma atLeast_Suc_greaterThan: "atLeast (Suc k) = greaterThan k"
   716   by (auto simp add: greaterThan_def atLeast_def less_Suc_eq_le)
   717 
   718 lemma UN_atLeast_UNIV: "(UN m::nat. atLeast m) = UNIV"
   719 by blast
   720 
   721 subsubsection \<open>The Constant @{term atMost}\<close>
   722 
   723 lemma atMost_0 [simp]: "atMost (0::nat) = {0}"
   724 by (simp add: atMost_def)
   725 
   726 lemma atMost_Suc: "atMost (Suc k) = insert (Suc k) (atMost k)"
   727 apply (simp add: atMost_def)
   728 apply (simp add: less_Suc_eq order_le_less, blast)
   729 done
   730 
   731 lemma UN_atMost_UNIV: "(UN m::nat. atMost m) = UNIV"
   732 by blast
   733 
   734 subsubsection \<open>The Constant @{term atLeastLessThan}\<close>
   735 
   736 text\<open>The orientation of the following 2 rules is tricky. The lhs is
   737 defined in terms of the rhs.  Hence the chosen orientation makes sense
   738 in this theory --- the reverse orientation complicates proofs (eg
   739 nontermination). But outside, when the definition of the lhs is rarely
   740 used, the opposite orientation seems preferable because it reduces a
   741 specific concept to a more general one.\<close>
   742 
   743 lemma atLeast0LessThan: "{0::nat..<n} = {..<n}"
   744 by(simp add:lessThan_def atLeastLessThan_def)
   745 
   746 lemma atLeast0AtMost: "{0..n::nat} = {..n}"
   747 by(simp add:atMost_def atLeastAtMost_def)
   748 
   749 declare atLeast0LessThan[symmetric, code_unfold]
   750         atLeast0AtMost[symmetric, code_unfold]
   751 
   752 lemma atLeastLessThan0: "{m..<0::nat} = {}"
   753 by (simp add: atLeastLessThan_def)
   754 
   755 subsubsection \<open>Intervals of nats with @{term Suc}\<close>
   756 
   757 text\<open>Not a simprule because the RHS is too messy.\<close>
   758 lemma atLeastLessThanSuc:
   759     "{m..<Suc n} = (if m \<le> n then insert n {m..<n} else {})"
   760 by (auto simp add: atLeastLessThan_def)
   761 
   762 lemma atLeastLessThan_singleton [simp]: "{m..<Suc m} = {m}"
   763 by (auto simp add: atLeastLessThan_def)
   764 (*
   765 lemma atLeast_sum_LessThan [simp]: "{m + k..<k::nat} = {}"
   766 by (induct k, simp_all add: atLeastLessThanSuc)
   767 
   768 lemma atLeastSucLessThan [simp]: "{Suc n..<n} = {}"
   769 by (auto simp add: atLeastLessThan_def)
   770 *)
   771 lemma atLeastLessThanSuc_atLeastAtMost: "{l..<Suc u} = {l..u}"
   772   by (simp add: lessThan_Suc_atMost atLeastAtMost_def atLeastLessThan_def)
   773 
   774 lemma atLeastSucAtMost_greaterThanAtMost: "{Suc l..u} = {l<..u}"
   775   by (simp add: atLeast_Suc_greaterThan atLeastAtMost_def
   776     greaterThanAtMost_def)
   777 
   778 lemma atLeastSucLessThan_greaterThanLessThan: "{Suc l..<u} = {l<..<u}"
   779   by (simp add: atLeast_Suc_greaterThan atLeastLessThan_def
   780     greaterThanLessThan_def)
   781 
   782 lemma atLeastAtMostSuc_conv: "m \<le> Suc n \<Longrightarrow> {m..Suc n} = insert (Suc n) {m..n}"
   783 by (auto simp add: atLeastAtMost_def)
   784 
   785 lemma atLeastAtMost_insertL: "m \<le> n \<Longrightarrow> insert m {Suc m..n} = {m ..n}"
   786 by auto
   787 
   788 text \<open>The analogous result is useful on @{typ int}:\<close>
   789 (* here, because we don't have an own int section *)
   790 lemma atLeastAtMostPlus1_int_conv:
   791   "m <= 1+n \<Longrightarrow> {m..1+n} = insert (1+n) {m..n::int}"
   792   by (auto intro: set_eqI)
   793 
   794 lemma atLeastLessThan_add_Un: "i \<le> j \<Longrightarrow> {i..<j+k} = {i..<j} \<union> {j..<j+k::nat}"
   795   apply (induct k) 
   796   apply (simp_all add: atLeastLessThanSuc)   
   797   done
   798 
   799 subsubsection \<open>Intervals and numerals\<close>
   800 
   801 lemma lessThan_nat_numeral:  \<comment>\<open>Evaluation for specific numerals\<close>
   802   "lessThan (numeral k :: nat) = insert (pred_numeral k) (lessThan (pred_numeral k))"
   803   by (simp add: numeral_eq_Suc lessThan_Suc)
   804 
   805 lemma atMost_nat_numeral:  \<comment>\<open>Evaluation for specific numerals\<close>
   806   "atMost (numeral k :: nat) = insert (numeral k) (atMost (pred_numeral k))"
   807   by (simp add: numeral_eq_Suc atMost_Suc)
   808 
   809 lemma atLeastLessThan_nat_numeral:  \<comment>\<open>Evaluation for specific numerals\<close>
   810   "atLeastLessThan m (numeral k :: nat) = 
   811      (if m \<le> (pred_numeral k) then insert (pred_numeral k) (atLeastLessThan m (pred_numeral k))
   812                  else {})"
   813   by (simp add: numeral_eq_Suc atLeastLessThanSuc)
   814 
   815 subsubsection \<open>Image\<close>
   816 
   817 lemma image_add_atLeastAtMost [simp]:
   818   fixes k ::"'a::linordered_semidom"
   819   shows "(\<lambda>n. n+k) ` {i..j} = {i+k..j+k}" (is "?A = ?B")
   820 proof
   821   show "?A \<subseteq> ?B" by auto
   822 next
   823   show "?B \<subseteq> ?A"
   824   proof
   825     fix n assume a: "n : ?B"
   826     hence "n - k : {i..j}"
   827       by (auto simp: add_le_imp_le_diff add_le_add_imp_diff_le)
   828     moreover have "n = (n - k) + k" using a
   829     proof -
   830       have "k + i \<le> n"
   831         by (metis a add.commute atLeastAtMost_iff)
   832       hence "k + (n - k) = n"
   833         by (metis (no_types) ab_semigroup_add_class.add_ac(1) add_diff_cancel_left' le_add_diff_inverse)
   834       thus ?thesis
   835         by (simp add: add.commute)
   836     qed
   837     ultimately show "n : ?A" by blast
   838   qed
   839 qed
   840 
   841 lemma image_diff_atLeastAtMost [simp]:
   842   fixes d::"'a::linordered_idom" shows "(op - d ` {a..b}) = {d-b..d-a}"
   843   apply auto
   844   apply (rule_tac x="d-x" in rev_image_eqI, auto)
   845   done
   846 
   847 lemma image_mult_atLeastAtMost [simp]:
   848   fixes d::"'a::linordered_field"
   849   assumes "d>0" shows "(op * d ` {a..b}) = {d*a..d*b}"
   850   using assms
   851   by (auto simp: field_simps mult_le_cancel_right intro: rev_image_eqI [where x="x/d" for x])
   852 
   853 lemma image_affinity_atLeastAtMost:
   854   fixes c :: "'a::linordered_field"
   855   shows "((\<lambda>x. m*x + c) ` {a..b}) = (if {a..b}={} then {}
   856             else if 0 \<le> m then {m*a + c .. m *b + c}
   857             else {m*b + c .. m*a + c})"
   858   apply (case_tac "m=0", auto simp: mult_le_cancel_left)
   859   apply (rule_tac x="inverse m*(x-c)" in rev_image_eqI, auto simp: field_simps)
   860   apply (rule_tac x="inverse m*(x-c)" in rev_image_eqI, auto simp: field_simps)
   861   done
   862 
   863 lemma image_affinity_atLeastAtMost_diff:
   864   fixes c :: "'a::linordered_field"
   865   shows "((\<lambda>x. m*x - c) ` {a..b}) = (if {a..b}={} then {}
   866             else if 0 \<le> m then {m*a - c .. m*b - c}
   867             else {m*b - c .. m*a - c})"
   868   using image_affinity_atLeastAtMost [of m "-c" a b]
   869   by simp
   870 
   871 lemma image_affinity_atLeastAtMost_div:
   872   fixes c :: "'a::linordered_field"
   873   shows "((\<lambda>x. x/m + c) ` {a..b}) = (if {a..b}={} then {}
   874             else if 0 \<le> m then {a/m + c .. b/m + c}
   875             else {b/m + c .. a/m + c})"
   876   using image_affinity_atLeastAtMost [of "inverse m" c a b]
   877   by (simp add: field_class.field_divide_inverse algebra_simps)
   878     
   879 lemma image_affinity_atLeastAtMost_div_diff:
   880   fixes c :: "'a::linordered_field"
   881   shows "((\<lambda>x. x/m - c) ` {a..b}) = (if {a..b}={} then {}
   882             else if 0 \<le> m then {a/m - c .. b/m - c}
   883             else {b/m - c .. a/m - c})"
   884   using image_affinity_atLeastAtMost_diff [of "inverse m" c a b]
   885   by (simp add: field_class.field_divide_inverse algebra_simps)
   886 
   887 lemma image_add_atLeastLessThan:
   888   "(%n::nat. n+k) ` {i..<j} = {i+k..<j+k}" (is "?A = ?B")
   889 proof
   890   show "?A \<subseteq> ?B" by auto
   891 next
   892   show "?B \<subseteq> ?A"
   893   proof
   894     fix n assume a: "n : ?B"
   895     hence "n - k : {i..<j}" by auto
   896     moreover have "n = (n - k) + k" using a by auto
   897     ultimately show "n : ?A" by blast
   898   qed
   899 qed
   900 
   901 corollary image_Suc_atLeastAtMost[simp]:
   902   "Suc ` {i..j} = {Suc i..Suc j}"
   903 using image_add_atLeastAtMost[where k="Suc 0"] by simp
   904 
   905 corollary image_Suc_atLeastLessThan[simp]:
   906   "Suc ` {i..<j} = {Suc i..<Suc j}"
   907 using image_add_atLeastLessThan[where k="Suc 0"] by simp
   908 
   909 lemma image_add_int_atLeastLessThan:
   910     "(%x. x + (l::int)) ` {0..<u-l} = {l..<u}"
   911   apply (auto simp add: image_def)
   912   apply (rule_tac x = "x - l" in bexI)
   913   apply auto
   914   done
   915 
   916 lemma image_minus_const_atLeastLessThan_nat:
   917   fixes c :: nat
   918   shows "(\<lambda>i. i - c) ` {x ..< y} =
   919       (if c < y then {x - c ..< y - c} else if x < y then {0} else {})"
   920     (is "_ = ?right")
   921 proof safe
   922   fix a assume a: "a \<in> ?right"
   923   show "a \<in> (\<lambda>i. i - c) ` {x ..< y}"
   924   proof cases
   925     assume "c < y" with a show ?thesis
   926       by (auto intro!: image_eqI[of _ _ "a + c"])
   927   next
   928     assume "\<not> c < y" with a show ?thesis
   929       by (auto intro!: image_eqI[of _ _ x] split: split_if_asm)
   930   qed
   931 qed auto
   932 
   933 lemma image_int_atLeastLessThan: "int ` {a..<b} = {int a..<int b}"
   934   by (auto intro!: image_eqI [where x = "nat x" for x])
   935 
   936 context ordered_ab_group_add
   937 begin
   938 
   939 lemma
   940   fixes x :: 'a
   941   shows image_uminus_greaterThan[simp]: "uminus ` {x<..} = {..<-x}"
   942   and image_uminus_atLeast[simp]: "uminus ` {x..} = {..-x}"
   943 proof safe
   944   fix y assume "y < -x"
   945   hence *:  "x < -y" using neg_less_iff_less[of "-y" x] by simp
   946   have "- (-y) \<in> uminus ` {x<..}"
   947     by (rule imageI) (simp add: *)
   948   thus "y \<in> uminus ` {x<..}" by simp
   949 next
   950   fix y assume "y \<le> -x"
   951   have "- (-y) \<in> uminus ` {x..}"
   952     by (rule imageI) (insert \<open>y \<le> -x\<close>[THEN le_imp_neg_le], simp)
   953   thus "y \<in> uminus ` {x..}" by simp
   954 qed simp_all
   955 
   956 lemma
   957   fixes x :: 'a
   958   shows image_uminus_lessThan[simp]: "uminus ` {..<x} = {-x<..}"
   959   and image_uminus_atMost[simp]: "uminus ` {..x} = {-x..}"
   960 proof -
   961   have "uminus ` {..<x} = uminus ` uminus ` {-x<..}"
   962     and "uminus ` {..x} = uminus ` uminus ` {-x..}" by simp_all
   963   thus "uminus ` {..<x} = {-x<..}" and "uminus ` {..x} = {-x..}"
   964     by (simp_all add: image_image
   965         del: image_uminus_greaterThan image_uminus_atLeast)
   966 qed
   967 
   968 lemma
   969   fixes x :: 'a
   970   shows image_uminus_atLeastAtMost[simp]: "uminus ` {x..y} = {-y..-x}"
   971   and image_uminus_greaterThanAtMost[simp]: "uminus ` {x<..y} = {-y..<-x}"
   972   and image_uminus_atLeastLessThan[simp]: "uminus ` {x..<y} = {-y<..-x}"
   973   and image_uminus_greaterThanLessThan[simp]: "uminus ` {x<..<y} = {-y<..<-x}"
   974   by (simp_all add: atLeastAtMost_def greaterThanAtMost_def atLeastLessThan_def
   975       greaterThanLessThan_def image_Int[OF inj_uminus] Int_commute)
   976 end
   977 
   978 subsubsection \<open>Finiteness\<close>
   979 
   980 lemma finite_lessThan [iff]: fixes k :: nat shows "finite {..<k}"
   981   by (induct k) (simp_all add: lessThan_Suc)
   982 
   983 lemma finite_atMost [iff]: fixes k :: nat shows "finite {..k}"
   984   by (induct k) (simp_all add: atMost_Suc)
   985 
   986 lemma finite_greaterThanLessThan [iff]:
   987   fixes l :: nat shows "finite {l<..<u}"
   988 by (simp add: greaterThanLessThan_def)
   989 
   990 lemma finite_atLeastLessThan [iff]:
   991   fixes l :: nat shows "finite {l..<u}"
   992 by (simp add: atLeastLessThan_def)
   993 
   994 lemma finite_greaterThanAtMost [iff]:
   995   fixes l :: nat shows "finite {l<..u}"
   996 by (simp add: greaterThanAtMost_def)
   997 
   998 lemma finite_atLeastAtMost [iff]:
   999   fixes l :: nat shows "finite {l..u}"
  1000 by (simp add: atLeastAtMost_def)
  1001 
  1002 text \<open>A bounded set of natural numbers is finite.\<close>
  1003 lemma bounded_nat_set_is_finite:
  1004   "(ALL i:N. i < (n::nat)) ==> finite N"
  1005 apply (rule finite_subset)
  1006  apply (rule_tac [2] finite_lessThan, auto)
  1007 done
  1008 
  1009 text \<open>A set of natural numbers is finite iff it is bounded.\<close>
  1010 lemma finite_nat_set_iff_bounded:
  1011   "finite(N::nat set) = (EX m. ALL n:N. n<m)" (is "?F = ?B")
  1012 proof
  1013   assume f:?F  show ?B
  1014     using Max_ge[OF \<open>?F\<close>, simplified less_Suc_eq_le[symmetric]] by blast
  1015 next
  1016   assume ?B show ?F using \<open>?B\<close> by(blast intro:bounded_nat_set_is_finite)
  1017 qed
  1018 
  1019 lemma finite_nat_set_iff_bounded_le:
  1020   "finite(N::nat set) = (EX m. ALL n:N. n<=m)"
  1021 apply(simp add:finite_nat_set_iff_bounded)
  1022 apply(blast dest:less_imp_le_nat le_imp_less_Suc)
  1023 done
  1024 
  1025 lemma finite_less_ub:
  1026      "!!f::nat=>nat. (!!n. n \<le> f n) ==> finite {n. f n \<le> u}"
  1027 by (rule_tac B="{..u}" in finite_subset, auto intro: order_trans)
  1028 
  1029 
  1030 text\<open>Any subset of an interval of natural numbers the size of the
  1031 subset is exactly that interval.\<close>
  1032 
  1033 lemma subset_card_intvl_is_intvl:
  1034   assumes "A \<subseteq> {k..<k + card A}"
  1035   shows "A = {k..<k + card A}"
  1036 proof (cases "finite A")
  1037   case True
  1038   from this and assms show ?thesis
  1039   proof (induct A rule: finite_linorder_max_induct)
  1040     case empty thus ?case by auto
  1041   next
  1042     case (insert b A)
  1043     hence *: "b \<notin> A" by auto
  1044     with insert have "A <= {k..<k + card A}" and "b = k + card A"
  1045       by fastforce+
  1046     with insert * show ?case by auto
  1047   qed
  1048 next
  1049   case False
  1050   with assms show ?thesis by simp
  1051 qed
  1052 
  1053 
  1054 subsubsection \<open>Proving Inclusions and Equalities between Unions\<close>
  1055 
  1056 lemma UN_le_eq_Un0:
  1057   "(\<Union>i\<le>n::nat. M i) = (\<Union>i\<in>{1..n}. M i) \<union> M 0" (is "?A = ?B")
  1058 proof
  1059   show "?A <= ?B"
  1060   proof
  1061     fix x assume "x : ?A"
  1062     then obtain i where i: "i\<le>n" "x : M i" by auto
  1063     show "x : ?B"
  1064     proof(cases i)
  1065       case 0 with i show ?thesis by simp
  1066     next
  1067       case (Suc j) with i show ?thesis by auto
  1068     qed
  1069   qed
  1070 next
  1071   show "?B <= ?A" by auto
  1072 qed
  1073 
  1074 lemma UN_le_add_shift:
  1075   "(\<Union>i\<le>n::nat. M(i+k)) = (\<Union>i\<in>{k..n+k}. M i)" (is "?A = ?B")
  1076 proof
  1077   show "?A <= ?B" by fastforce
  1078 next
  1079   show "?B <= ?A"
  1080   proof
  1081     fix x assume "x : ?B"
  1082     then obtain i where i: "i : {k..n+k}" "x : M(i)" by auto
  1083     hence "i-k\<le>n & x : M((i-k)+k)" by auto
  1084     thus "x : ?A" by blast
  1085   qed
  1086 qed
  1087 
  1088 lemma UN_UN_finite_eq: "(\<Union>n::nat. \<Union>i\<in>{0..<n}. A i) = (\<Union>n. A n)"
  1089   by (auto simp add: atLeast0LessThan) 
  1090 
  1091 lemma UN_finite_subset: "(!!n::nat. (\<Union>i\<in>{0..<n}. A i) \<subseteq> C) \<Longrightarrow> (\<Union>n. A n) \<subseteq> C"
  1092   by (subst UN_UN_finite_eq [symmetric]) blast
  1093 
  1094 lemma UN_finite2_subset: 
  1095      "(!!n::nat. (\<Union>i\<in>{0..<n}. A i) \<subseteq> (\<Union>i\<in>{0..<n+k}. B i)) \<Longrightarrow> (\<Union>n. A n) \<subseteq> (\<Union>n. B n)"
  1096   apply (rule UN_finite_subset)
  1097   apply (subst UN_UN_finite_eq [symmetric, of B]) 
  1098   apply blast
  1099   done
  1100 
  1101 lemma UN_finite2_eq:
  1102   "(!!n::nat. (\<Union>i\<in>{0..<n}. A i) = (\<Union>i\<in>{0..<n+k}. B i)) \<Longrightarrow> (\<Union>n. A n) = (\<Union>n. B n)"
  1103   apply (rule subset_antisym)
  1104    apply (rule UN_finite2_subset, blast)
  1105  apply (rule UN_finite2_subset [where k=k])
  1106  apply (force simp add: atLeastLessThan_add_Un [of 0])
  1107  done
  1108 
  1109 
  1110 subsubsection \<open>Cardinality\<close>
  1111 
  1112 lemma card_lessThan [simp]: "card {..<u} = u"
  1113   by (induct u, simp_all add: lessThan_Suc)
  1114 
  1115 lemma card_atMost [simp]: "card {..u} = Suc u"
  1116   by (simp add: lessThan_Suc_atMost [THEN sym])
  1117 
  1118 lemma card_atLeastLessThan [simp]: "card {l..<u} = u - l"
  1119 proof -
  1120   have "{l..<u} = (%x. x + l) ` {..<u-l}"
  1121     apply (auto simp add: image_def atLeastLessThan_def lessThan_def)
  1122     apply (rule_tac x = "x - l" in exI)
  1123     apply arith
  1124     done
  1125   then have "card {l..<u} = card {..<u-l}"
  1126     by (simp add: card_image inj_on_def)
  1127   then show ?thesis
  1128     by simp
  1129 qed
  1130 
  1131 lemma card_atLeastAtMost [simp]: "card {l..u} = Suc u - l"
  1132   by (subst atLeastLessThanSuc_atLeastAtMost [THEN sym], simp)
  1133 
  1134 lemma card_greaterThanAtMost [simp]: "card {l<..u} = u - l"
  1135   by (subst atLeastSucAtMost_greaterThanAtMost [THEN sym], simp)
  1136 
  1137 lemma card_greaterThanLessThan [simp]: "card {l<..<u} = u - Suc l"
  1138   by (subst atLeastSucLessThan_greaterThanLessThan [THEN sym], simp)
  1139 
  1140 lemma ex_bij_betw_nat_finite:
  1141   "finite M \<Longrightarrow> \<exists>h. bij_betw h {0..<card M} M"
  1142 apply(drule finite_imp_nat_seg_image_inj_on)
  1143 apply(auto simp:atLeast0LessThan[symmetric] lessThan_def[symmetric] card_image bij_betw_def)
  1144 done
  1145 
  1146 lemma ex_bij_betw_finite_nat:
  1147   "finite M \<Longrightarrow> \<exists>h. bij_betw h M {0..<card M}"
  1148 by (blast dest: ex_bij_betw_nat_finite bij_betw_inv)
  1149 
  1150 lemma finite_same_card_bij:
  1151   "finite A \<Longrightarrow> finite B \<Longrightarrow> card A = card B \<Longrightarrow> EX h. bij_betw h A B"
  1152 apply(drule ex_bij_betw_finite_nat)
  1153 apply(drule ex_bij_betw_nat_finite)
  1154 apply(auto intro!:bij_betw_trans)
  1155 done
  1156 
  1157 lemma ex_bij_betw_nat_finite_1:
  1158   "finite M \<Longrightarrow> \<exists>h. bij_betw h {1 .. card M} M"
  1159 by (rule finite_same_card_bij) auto
  1160 
  1161 lemma bij_betw_iff_card:
  1162   assumes FIN: "finite A" and FIN': "finite B"
  1163   shows BIJ: "(\<exists>f. bij_betw f A B) \<longleftrightarrow> (card A = card B)"
  1164 using assms
  1165 proof(auto simp add: bij_betw_same_card)
  1166   assume *: "card A = card B"
  1167   obtain f where "bij_betw f A {0 ..< card A}"
  1168   using FIN ex_bij_betw_finite_nat by blast
  1169   moreover obtain g where "bij_betw g {0 ..< card B} B"
  1170   using FIN' ex_bij_betw_nat_finite by blast
  1171   ultimately have "bij_betw (g o f) A B"
  1172   using * by (auto simp add: bij_betw_trans)
  1173   thus "(\<exists>f. bij_betw f A B)" by blast
  1174 qed
  1175 
  1176 lemma inj_on_iff_card_le:
  1177   assumes FIN: "finite A" and FIN': "finite B"
  1178   shows "(\<exists>f. inj_on f A \<and> f ` A \<le> B) = (card A \<le> card B)"
  1179 proof (safe intro!: card_inj_on_le)
  1180   assume *: "card A \<le> card B"
  1181   obtain f where 1: "inj_on f A" and 2: "f ` A = {0 ..< card A}"
  1182   using FIN ex_bij_betw_finite_nat unfolding bij_betw_def by force
  1183   moreover obtain g where "inj_on g {0 ..< card B}" and 3: "g ` {0 ..< card B} = B"
  1184   using FIN' ex_bij_betw_nat_finite unfolding bij_betw_def by force
  1185   ultimately have "inj_on g (f ` A)" using subset_inj_on[of g _ "f ` A"] * by force
  1186   hence "inj_on (g o f) A" using 1 comp_inj_on by blast
  1187   moreover
  1188   {have "{0 ..< card A} \<le> {0 ..< card B}" using * by force
  1189    with 2 have "f ` A  \<le> {0 ..< card B}" by blast
  1190    hence "(g o f) ` A \<le> B" unfolding comp_def using 3 by force
  1191   }
  1192   ultimately show "(\<exists>f. inj_on f A \<and> f ` A \<le> B)" by blast
  1193 qed (insert assms, auto)
  1194 
  1195 subsection \<open>Intervals of integers\<close>
  1196 
  1197 lemma atLeastLessThanPlusOne_atLeastAtMost_int: "{l..<u+1} = {l..(u::int)}"
  1198   by (auto simp add: atLeastAtMost_def atLeastLessThan_def)
  1199 
  1200 lemma atLeastPlusOneAtMost_greaterThanAtMost_int: "{l+1..u} = {l<..(u::int)}"
  1201   by (auto simp add: atLeastAtMost_def greaterThanAtMost_def)
  1202 
  1203 lemma atLeastPlusOneLessThan_greaterThanLessThan_int:
  1204     "{l+1..<u} = {l<..<u::int}"
  1205   by (auto simp add: atLeastLessThan_def greaterThanLessThan_def)
  1206 
  1207 subsubsection \<open>Finiteness\<close>
  1208 
  1209 lemma image_atLeastZeroLessThan_int: "0 \<le> u ==>
  1210     {(0::int)..<u} = int ` {..<nat u}"
  1211   apply (unfold image_def lessThan_def)
  1212   apply auto
  1213   apply (rule_tac x = "nat x" in exI)
  1214   apply (auto simp add: zless_nat_eq_int_zless [THEN sym])
  1215   done
  1216 
  1217 lemma finite_atLeastZeroLessThan_int: "finite {(0::int)..<u}"
  1218   apply (cases "0 \<le> u")
  1219   apply (subst image_atLeastZeroLessThan_int, assumption)
  1220   apply (rule finite_imageI)
  1221   apply auto
  1222   done
  1223 
  1224 lemma finite_atLeastLessThan_int [iff]: "finite {l..<u::int}"
  1225   apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
  1226   apply (erule subst)
  1227   apply (rule finite_imageI)
  1228   apply (rule finite_atLeastZeroLessThan_int)
  1229   apply (rule image_add_int_atLeastLessThan)
  1230   done
  1231 
  1232 lemma finite_atLeastAtMost_int [iff]: "finite {l..(u::int)}"
  1233   by (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym], simp)
  1234 
  1235 lemma finite_greaterThanAtMost_int [iff]: "finite {l<..(u::int)}"
  1236   by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
  1237 
  1238 lemma finite_greaterThanLessThan_int [iff]: "finite {l<..<u::int}"
  1239   by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
  1240 
  1241 
  1242 subsubsection \<open>Cardinality\<close>
  1243 
  1244 lemma card_atLeastZeroLessThan_int: "card {(0::int)..<u} = nat u"
  1245   apply (cases "0 \<le> u")
  1246   apply (subst image_atLeastZeroLessThan_int, assumption)
  1247   apply (subst card_image)
  1248   apply (auto simp add: inj_on_def)
  1249   done
  1250 
  1251 lemma card_atLeastLessThan_int [simp]: "card {l..<u} = nat (u - l)"
  1252   apply (subgoal_tac "card {l..<u} = card {0..<u-l}")
  1253   apply (erule ssubst, rule card_atLeastZeroLessThan_int)
  1254   apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
  1255   apply (erule subst)
  1256   apply (rule card_image)
  1257   apply (simp add: inj_on_def)
  1258   apply (rule image_add_int_atLeastLessThan)
  1259   done
  1260 
  1261 lemma card_atLeastAtMost_int [simp]: "card {l..u} = nat (u - l + 1)"
  1262 apply (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym])
  1263 apply (auto simp add: algebra_simps)
  1264 done
  1265 
  1266 lemma card_greaterThanAtMost_int [simp]: "card {l<..u} = nat (u - l)"
  1267 by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
  1268 
  1269 lemma card_greaterThanLessThan_int [simp]: "card {l<..<u} = nat (u - (l + 1))"
  1270 by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
  1271 
  1272 lemma finite_M_bounded_by_nat: "finite {k. P k \<and> k < (i::nat)}"
  1273 proof -
  1274   have "{k. P k \<and> k < i} \<subseteq> {..<i}" by auto
  1275   with finite_lessThan[of "i"] show ?thesis by (simp add: finite_subset)
  1276 qed
  1277 
  1278 lemma card_less:
  1279 assumes zero_in_M: "0 \<in> M"
  1280 shows "card {k \<in> M. k < Suc i} \<noteq> 0"
  1281 proof -
  1282   from zero_in_M have "{k \<in> M. k < Suc i} \<noteq> {}" by auto
  1283   with finite_M_bounded_by_nat show ?thesis by (auto simp add: card_eq_0_iff)
  1284 qed
  1285 
  1286 lemma card_less_Suc2: "0 \<notin> M \<Longrightarrow> card {k. Suc k \<in> M \<and> k < i} = card {k \<in> M. k < Suc i}"
  1287 apply (rule card_bij_eq [of Suc _ _ "\<lambda>x. x - Suc 0"])
  1288 apply auto
  1289 apply (rule inj_on_diff_nat)
  1290 apply auto
  1291 apply (case_tac x)
  1292 apply auto
  1293 apply (case_tac xa)
  1294 apply auto
  1295 apply (case_tac xa)
  1296 apply auto
  1297 done
  1298 
  1299 lemma card_less_Suc:
  1300   assumes zero_in_M: "0 \<in> M"
  1301     shows "Suc (card {k. Suc k \<in> M \<and> k < i}) = card {k \<in> M. k < Suc i}"
  1302 proof -
  1303   from assms have a: "0 \<in> {k \<in> M. k < Suc i}" by simp
  1304   hence c: "{k \<in> M. k < Suc i} = insert 0 ({k \<in> M. k < Suc i} - {0})"
  1305     by (auto simp only: insert_Diff)
  1306   have b: "{k \<in> M. k < Suc i} - {0} = {k \<in> M - {0}. k < Suc i}"  by auto
  1307   from finite_M_bounded_by_nat[of "\<lambda>x. x \<in> M" "Suc i"] 
  1308   have "Suc (card {k. Suc k \<in> M \<and> k < i}) = card (insert 0 ({k \<in> M. k < Suc i} - {0}))"
  1309     apply (subst card_insert)
  1310     apply simp_all
  1311     apply (subst b)
  1312     apply (subst card_less_Suc2[symmetric])
  1313     apply simp_all
  1314     done
  1315   with c show ?thesis by simp
  1316 qed
  1317 
  1318 
  1319 subsection \<open>Lemmas useful with the summation operator setsum\<close>
  1320 
  1321 text \<open>For examples, see Algebra/poly/UnivPoly2.thy\<close>
  1322 
  1323 subsubsection \<open>Disjoint Unions\<close>
  1324 
  1325 text \<open>Singletons and open intervals\<close>
  1326 
  1327 lemma ivl_disj_un_singleton:
  1328   "{l::'a::linorder} Un {l<..} = {l..}"
  1329   "{..<u} Un {u::'a::linorder} = {..u}"
  1330   "(l::'a::linorder) < u ==> {l} Un {l<..<u} = {l..<u}"
  1331   "(l::'a::linorder) < u ==> {l<..<u} Un {u} = {l<..u}"
  1332   "(l::'a::linorder) <= u ==> {l} Un {l<..u} = {l..u}"
  1333   "(l::'a::linorder) <= u ==> {l..<u} Un {u} = {l..u}"
  1334 by auto
  1335 
  1336 text \<open>One- and two-sided intervals\<close>
  1337 
  1338 lemma ivl_disj_un_one:
  1339   "(l::'a::linorder) < u ==> {..l} Un {l<..<u} = {..<u}"
  1340   "(l::'a::linorder) <= u ==> {..<l} Un {l..<u} = {..<u}"
  1341   "(l::'a::linorder) <= u ==> {..l} Un {l<..u} = {..u}"
  1342   "(l::'a::linorder) <= u ==> {..<l} Un {l..u} = {..u}"
  1343   "(l::'a::linorder) <= u ==> {l<..u} Un {u<..} = {l<..}"
  1344   "(l::'a::linorder) < u ==> {l<..<u} Un {u..} = {l<..}"
  1345   "(l::'a::linorder) <= u ==> {l..u} Un {u<..} = {l..}"
  1346   "(l::'a::linorder) <= u ==> {l..<u} Un {u..} = {l..}"
  1347 by auto
  1348 
  1349 text \<open>Two- and two-sided intervals\<close>
  1350 
  1351 lemma ivl_disj_un_two:
  1352   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..<u} = {l<..<u}"
  1353   "[| (l::'a::linorder) <= m; m < u |] ==> {l<..m} Un {m<..<u} = {l<..<u}"
  1354   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..<u} = {l..<u}"
  1355   "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m<..<u} = {l..<u}"
  1356   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..u} = {l<..u}"
  1357   "[| (l::'a::linorder) <= m; m <= u |] ==> {l<..m} Un {m<..u} = {l<..u}"
  1358   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..u} = {l..u}"
  1359   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m<..u} = {l..u}"
  1360 by auto
  1361 
  1362 lemma ivl_disj_un_two_touch:
  1363   "[| (l::'a::linorder) < m; m < u |] ==> {l<..m} Un {m..<u} = {l<..<u}"
  1364   "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m..<u} = {l..<u}"
  1365   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..m} Un {m..u} = {l<..u}"
  1366   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m..u} = {l..u}"
  1367 by auto
  1368 
  1369 lemmas ivl_disj_un = ivl_disj_un_singleton ivl_disj_un_one ivl_disj_un_two ivl_disj_un_two_touch
  1370 
  1371 subsubsection \<open>Disjoint Intersections\<close>
  1372 
  1373 text \<open>One- and two-sided intervals\<close>
  1374 
  1375 lemma ivl_disj_int_one:
  1376   "{..l::'a::order} Int {l<..<u} = {}"
  1377   "{..<l} Int {l..<u} = {}"
  1378   "{..l} Int {l<..u} = {}"
  1379   "{..<l} Int {l..u} = {}"
  1380   "{l<..u} Int {u<..} = {}"
  1381   "{l<..<u} Int {u..} = {}"
  1382   "{l..u} Int {u<..} = {}"
  1383   "{l..<u} Int {u..} = {}"
  1384   by auto
  1385 
  1386 text \<open>Two- and two-sided intervals\<close>
  1387 
  1388 lemma ivl_disj_int_two:
  1389   "{l::'a::order<..<m} Int {m..<u} = {}"
  1390   "{l<..m} Int {m<..<u} = {}"
  1391   "{l..<m} Int {m..<u} = {}"
  1392   "{l..m} Int {m<..<u} = {}"
  1393   "{l<..<m} Int {m..u} = {}"
  1394   "{l<..m} Int {m<..u} = {}"
  1395   "{l..<m} Int {m..u} = {}"
  1396   "{l..m} Int {m<..u} = {}"
  1397   by auto
  1398 
  1399 lemmas ivl_disj_int = ivl_disj_int_one ivl_disj_int_two
  1400 
  1401 subsubsection \<open>Some Differences\<close>
  1402 
  1403 lemma ivl_diff[simp]:
  1404  "i \<le> n \<Longrightarrow> {i..<m} - {i..<n} = {n..<(m::'a::linorder)}"
  1405 by(auto)
  1406 
  1407 lemma (in linorder) lessThan_minus_lessThan [simp]:
  1408   "{..< n} - {..< m} = {m ..< n}"
  1409   by auto
  1410 
  1411 lemma (in linorder) atLeastAtMost_diff_ends:
  1412   "{a..b} - {a, b} = {a<..<b}"
  1413   by auto
  1414 
  1415 
  1416 subsubsection \<open>Some Subset Conditions\<close>
  1417 
  1418 lemma ivl_subset [simp]:
  1419  "({i..<j} \<subseteq> {m..<n}) = (j \<le> i | m \<le> i & j \<le> (n::'a::linorder))"
  1420 apply(auto simp:linorder_not_le)
  1421 apply(rule ccontr)
  1422 apply(insert linorder_le_less_linear[of i n])
  1423 apply(clarsimp simp:linorder_not_le)
  1424 apply(fastforce)
  1425 done
  1426 
  1427 
  1428 subsection \<open>Summation indexed over intervals\<close>
  1429 
  1430 syntax (ASCII)
  1431   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _ = _.._./ _)" [0,0,0,10] 10)
  1432   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _ = _..<_./ _)" [0,0,0,10] 10)
  1433   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _<_./ _)" [0,0,10] 10)
  1434   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _<=_./ _)" [0,0,10] 10)
  1435 
  1436 syntax (latex_sum output)
  1437   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1438  ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
  1439   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1440  ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
  1441   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1442  ("(3\<^raw:$\sum_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
  1443   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1444  ("(3\<^raw:$\sum_{>_ \<le> _\<^raw:}$> _)" [0,0,10] 10)
  1445 
  1446 syntax
  1447   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
  1448   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
  1449   "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_<_./ _)" [0,0,10] 10)
  1450   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
  1451 
  1452 translations
  1453   "\<Sum>x=a..b. t" == "CONST setsum (\<lambda>x. t) {a..b}"
  1454   "\<Sum>x=a..<b. t" == "CONST setsum (\<lambda>x. t) {a..<b}"
  1455   "\<Sum>i\<le>n. t" == "CONST setsum (\<lambda>i. t) {..n}"
  1456   "\<Sum>i<n. t" == "CONST setsum (\<lambda>i. t) {..<n}"
  1457 
  1458 text\<open>The above introduces some pretty alternative syntaxes for
  1459 summation over intervals:
  1460 \begin{center}
  1461 \begin{tabular}{lll}
  1462 Old & New & \LaTeX\\
  1463 @{term[source]"\<Sum>x\<in>{a..b}. e"} & @{term"\<Sum>x=a..b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..b. e"}\\
  1464 @{term[source]"\<Sum>x\<in>{a..<b}. e"} & @{term"\<Sum>x=a..<b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..<b. e"}\\
  1465 @{term[source]"\<Sum>x\<in>{..b}. e"} & @{term"\<Sum>x\<le>b. e"} & @{term[mode=latex_sum]"\<Sum>x\<le>b. e"}\\
  1466 @{term[source]"\<Sum>x\<in>{..<b}. e"} & @{term"\<Sum>x<b. e"} & @{term[mode=latex_sum]"\<Sum>x<b. e"}
  1467 \end{tabular}
  1468 \end{center}
  1469 The left column shows the term before introduction of the new syntax,
  1470 the middle column shows the new (default) syntax, and the right column
  1471 shows a special syntax. The latter is only meaningful for latex output
  1472 and has to be activated explicitly by setting the print mode to
  1473 \<open>latex_sum\<close> (e.g.\ via \<open>mode = latex_sum\<close> in
  1474 antiquotations). It is not the default \LaTeX\ output because it only
  1475 works well with italic-style formulae, not tt-style.
  1476 
  1477 Note that for uniformity on @{typ nat} it is better to use
  1478 @{term"\<Sum>x::nat=0..<n. e"} rather than \<open>\<Sum>x<n. e\<close>: \<open>setsum\<close> may
  1479 not provide all lemmas available for @{term"{m..<n}"} also in the
  1480 special form for @{term"{..<n}"}.\<close>
  1481 
  1482 text\<open>This congruence rule should be used for sums over intervals as
  1483 the standard theorem @{text[source]setsum.cong} does not work well
  1484 with the simplifier who adds the unsimplified premise @{term"x:B"} to
  1485 the context.\<close>
  1486 
  1487 lemma setsum_ivl_cong:
  1488  "\<lbrakk>a = c; b = d; !!x. \<lbrakk> c \<le> x; x < d \<rbrakk> \<Longrightarrow> f x = g x \<rbrakk> \<Longrightarrow>
  1489  setsum f {a..<b} = setsum g {c..<d}"
  1490 by(rule setsum.cong, simp_all)
  1491 
  1492 (* FIXME why are the following simp rules but the corresponding eqns
  1493 on intervals are not? *)
  1494 
  1495 lemma setsum_atMost_Suc[simp]: "(\<Sum>i \<le> Suc n. f i) = (\<Sum>i \<le> n. f i) + f(Suc n)"
  1496 by (simp add:atMost_Suc ac_simps)
  1497 
  1498 lemma setsum_lessThan_Suc[simp]: "(\<Sum>i < Suc n. f i) = (\<Sum>i < n. f i) + f n"
  1499 by (simp add:lessThan_Suc ac_simps)
  1500 
  1501 lemma setsum_cl_ivl_Suc[simp]:
  1502   "setsum f {m..Suc n} = (if Suc n < m then 0 else setsum f {m..n} + f(Suc n))"
  1503 by (auto simp:ac_simps atLeastAtMostSuc_conv)
  1504 
  1505 lemma setsum_op_ivl_Suc[simp]:
  1506   "setsum f {m..<Suc n} = (if n < m then 0 else setsum f {m..<n} + f(n))"
  1507 by (auto simp:ac_simps atLeastLessThanSuc)
  1508 (*
  1509 lemma setsum_cl_ivl_add_one_nat: "(n::nat) <= m + 1 ==>
  1510     (\<Sum>i=n..m+1. f i) = (\<Sum>i=n..m. f i) + f(m + 1)"
  1511 by (auto simp:ac_simps atLeastAtMostSuc_conv)
  1512 *)
  1513 
  1514 lemma setsum_head:
  1515   fixes n :: nat
  1516   assumes mn: "m <= n" 
  1517   shows "(\<Sum>x\<in>{m..n}. P x) = P m + (\<Sum>x\<in>{m<..n}. P x)" (is "?lhs = ?rhs")
  1518 proof -
  1519   from mn
  1520   have "{m..n} = {m} \<union> {m<..n}"
  1521     by (auto intro: ivl_disj_un_singleton)
  1522   hence "?lhs = (\<Sum>x\<in>{m} \<union> {m<..n}. P x)"
  1523     by (simp add: atLeast0LessThan)
  1524   also have "\<dots> = ?rhs" by simp
  1525   finally show ?thesis .
  1526 qed
  1527 
  1528 lemma setsum_head_Suc:
  1529   "m \<le> n \<Longrightarrow> setsum f {m..n} = f m + setsum f {Suc m..n}"
  1530 by (simp add: setsum_head atLeastSucAtMost_greaterThanAtMost)
  1531 
  1532 lemma setsum_head_upt_Suc:
  1533   "m < n \<Longrightarrow> setsum f {m..<n} = f m + setsum f {Suc m..<n}"
  1534 apply(insert setsum_head_Suc[of m "n - Suc 0" f])
  1535 apply (simp add: atLeastLessThanSuc_atLeastAtMost[symmetric] algebra_simps)
  1536 done
  1537 
  1538 lemma setsum_ub_add_nat: assumes "(m::nat) \<le> n + 1"
  1539   shows "setsum f {m..n + p} = setsum f {m..n} + setsum f {n + 1..n + p}"
  1540 proof-
  1541   have "{m .. n+p} = {m..n} \<union> {n+1..n+p}" using \<open>m \<le> n+1\<close> by auto
  1542   thus ?thesis by (auto simp: ivl_disj_int setsum.union_disjoint
  1543     atLeastSucAtMost_greaterThanAtMost)
  1544 qed
  1545 
  1546 lemma setsum_add_nat_ivl: "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
  1547   setsum f {m..<n} + setsum f {n..<p} = setsum f {m..<p::nat}"
  1548 by (simp add:setsum.union_disjoint[symmetric] ivl_disj_int ivl_disj_un)
  1549 
  1550 lemma setsum_diff_nat_ivl:
  1551 fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
  1552 shows "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
  1553   setsum f {m..<p} - setsum f {m..<n} = setsum f {n..<p}"
  1554 using setsum_add_nat_ivl [of m n p f,symmetric]
  1555 apply (simp add: ac_simps)
  1556 done
  1557 
  1558 lemma setsum_natinterval_difff:
  1559   fixes f:: "nat \<Rightarrow> ('a::ab_group_add)"
  1560   shows  "setsum (\<lambda>k. f k - f(k + 1)) {(m::nat) .. n} =
  1561           (if m <= n then f m - f(n + 1) else 0)"
  1562 by (induct n, auto simp add: algebra_simps not_le le_Suc_eq)
  1563 
  1564 lemma setsum_nat_group: "(\<Sum>m<n::nat. setsum f {m * k ..< m*k + k}) = setsum f {..< n * k}"
  1565   apply (subgoal_tac "k = 0 | 0 < k", auto)
  1566   apply (induct "n")
  1567   apply (simp_all add: setsum_add_nat_ivl add.commute atLeast0LessThan[symmetric])
  1568   done
  1569 
  1570 lemma setsum_triangle_reindex:
  1571   fixes n :: nat
  1572   shows "(\<Sum>(i,j)\<in>{(i,j). i+j < n}. f i j) = (\<Sum>k<n. \<Sum>i\<le>k. f i (k - i))"
  1573   apply (simp add: setsum.Sigma)
  1574   apply (rule setsum.reindex_bij_witness[where j="\<lambda>(i, j). (i+j, i)" and i="\<lambda>(k, i). (i, k - i)"])
  1575   apply auto
  1576   done
  1577 
  1578 lemma setsum_triangle_reindex_eq:
  1579   fixes n :: nat
  1580   shows "(\<Sum>(i,j)\<in>{(i,j). i+j \<le> n}. f i j) = (\<Sum>k\<le>n. \<Sum>i\<le>k. f i (k - i))"
  1581 using setsum_triangle_reindex [of f "Suc n"]
  1582 by (simp only: Nat.less_Suc_eq_le lessThan_Suc_atMost)
  1583 
  1584 lemma nat_diff_setsum_reindex: "(\<Sum>i<n. f (n - Suc i)) = (\<Sum>i<n. f i)"
  1585   by (rule setsum.reindex_bij_witness[where i="\<lambda>i. n - Suc i" and j="\<lambda>i. n - Suc i"]) auto
  1586 
  1587 subsection\<open>Shifting bounds\<close>
  1588 
  1589 lemma setsum_shift_bounds_nat_ivl:
  1590   "setsum f {m+k..<n+k} = setsum (%i. f(i + k)){m..<n::nat}"
  1591 by (induct "n", auto simp:atLeastLessThanSuc)
  1592 
  1593 lemma setsum_shift_bounds_cl_nat_ivl:
  1594   "setsum f {m+k..n+k} = setsum (%i. f(i + k)){m..n::nat}"
  1595   by (rule setsum.reindex_bij_witness[where i="\<lambda>i. i + k" and j="\<lambda>i. i - k"]) auto
  1596 
  1597 corollary setsum_shift_bounds_cl_Suc_ivl:
  1598   "setsum f {Suc m..Suc n} = setsum (%i. f(Suc i)){m..n}"
  1599 by (simp add:setsum_shift_bounds_cl_nat_ivl[where k="Suc 0", simplified])
  1600 
  1601 corollary setsum_shift_bounds_Suc_ivl:
  1602   "setsum f {Suc m..<Suc n} = setsum (%i. f(Suc i)){m..<n}"
  1603 by (simp add:setsum_shift_bounds_nat_ivl[where k="Suc 0", simplified])
  1604 
  1605 lemma setsum_shift_lb_Suc0_0:
  1606   "f(0::nat) = (0::nat) \<Longrightarrow> setsum f {Suc 0..k} = setsum f {0..k}"
  1607 by(simp add:setsum_head_Suc)
  1608 
  1609 lemma setsum_shift_lb_Suc0_0_upt:
  1610   "f(0::nat) = 0 \<Longrightarrow> setsum f {Suc 0..<k} = setsum f {0..<k}"
  1611 apply(cases k)apply simp
  1612 apply(simp add:setsum_head_upt_Suc)
  1613 done
  1614 
  1615 lemma setsum_atMost_Suc_shift:
  1616   fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
  1617   shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
  1618 proof (induct n)
  1619   case 0 show ?case by simp
  1620 next
  1621   case (Suc n) note IH = this
  1622   have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"
  1623     by (rule setsum_atMost_Suc)
  1624   also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
  1625     by (rule IH)
  1626   also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
  1627              f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
  1628     by (rule add.assoc)
  1629   also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"
  1630     by (rule setsum_atMost_Suc [symmetric])
  1631   finally show ?case .
  1632 qed
  1633 
  1634 lemma setsum_last_plus: fixes n::nat shows "m <= n \<Longrightarrow> (\<Sum>i = m..n. f i) = f n + (\<Sum>i = m..<n. f i)"
  1635   by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost add.commute)
  1636 
  1637 lemma setsum_Suc_diff:
  1638   fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
  1639   assumes "m \<le> Suc n"
  1640   shows "(\<Sum>i = m..n. f(Suc i) - f i) = f (Suc n) - f m"
  1641 using assms by (induct n) (auto simp: le_Suc_eq)
  1642 
  1643 lemma nested_setsum_swap:
  1644      "(\<Sum>i = 0..n. (\<Sum>j = 0..<i. a i j)) = (\<Sum>j = 0..<n. \<Sum>i = Suc j..n. a i j)"
  1645   by (induction n) (auto simp: setsum.distrib)
  1646 
  1647 lemma nested_setsum_swap':
  1648      "(\<Sum>i\<le>n. (\<Sum>j<i. a i j)) = (\<Sum>j<n. \<Sum>i = Suc j..n. a i j)"
  1649   by (induction n) (auto simp: setsum.distrib)
  1650 
  1651 lemma setsum_zero_power' [simp]:
  1652   fixes c :: "nat \<Rightarrow> 'a::field"
  1653   shows "(\<Sum>i\<in>A. c i * 0^i / d i) = (if finite A \<and> 0 \<in> A then c 0 / d 0 else 0)"
  1654   using setsum_zero_power [of "\<lambda>i. c i / d i" A]
  1655   by auto
  1656 
  1657 
  1658 subsection \<open>Telescoping\<close>
  1659 
  1660 lemma setsum_telescope:
  1661   fixes f::"nat \<Rightarrow> 'a::ab_group_add"
  1662   shows "setsum (\<lambda>i. f i - f (Suc i)) {.. i} = f 0 - f (Suc i)"
  1663   by (induct i) simp_all
  1664 
  1665 lemma setsum_telescope'':
  1666   assumes "m \<le> n"
  1667   shows   "(\<Sum>k\<in>{Suc m..n}. f k - f (k - 1)) = f n - (f m :: 'a :: ab_group_add)"
  1668   by (rule dec_induct[OF assms]) (simp_all add: algebra_simps)
  1669 
  1670 subsection \<open>The formula for geometric sums\<close>
  1671 
  1672 lemma geometric_sum:
  1673   assumes "x \<noteq> 1"
  1674   shows "(\<Sum>i<n. x ^ i) = (x ^ n - 1) / (x - 1::'a::field)"
  1675 proof -
  1676   from assms obtain y where "y = x - 1" and "y \<noteq> 0" by simp_all
  1677   moreover have "(\<Sum>i<n. (y + 1) ^ i) = ((y + 1) ^ n - 1) / y"
  1678     by (induct n) (simp_all add: power_Suc field_simps \<open>y \<noteq> 0\<close>)
  1679   ultimately show ?thesis by simp
  1680 qed
  1681 
  1682 lemma diff_power_eq_setsum:
  1683   fixes y :: "'a::{comm_ring,monoid_mult}"
  1684   shows
  1685     "x ^ (Suc n) - y ^ (Suc n) =
  1686       (x - y) * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))"
  1687 proof (induct n)
  1688   case (Suc n)
  1689   have "x ^ Suc (Suc n) - y ^ Suc (Suc n) = x * (x * x^n) - y * (y * y ^ n)"
  1690     by (simp add: power_Suc)
  1691   also have "... = y * (x ^ (Suc n) - y ^ (Suc n)) + (x - y) * (x * x^n)"
  1692     by (simp add: power_Suc algebra_simps)
  1693   also have "... = y * ((x - y) * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))) + (x - y) * (x * x^n)"
  1694     by (simp only: Suc)
  1695   also have "... = (x - y) * (y * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))) + (x - y) * (x * x^n)"
  1696     by (simp only: mult.left_commute)
  1697   also have "... = (x - y) * (\<Sum>p<Suc (Suc n). x ^ p * y ^ (Suc n - p))"
  1698     by (simp add: power_Suc field_simps Suc_diff_le setsum_left_distrib setsum_right_distrib)
  1699   finally show ?case .
  1700 qed simp
  1701 
  1702 corollary power_diff_sumr2: \<comment>\<open>\<open>COMPLEX_POLYFUN\<close> in HOL Light\<close>
  1703   fixes x :: "'a::{comm_ring,monoid_mult}"
  1704   shows   "x^n - y^n = (x - y) * (\<Sum>i<n. y^(n - Suc i) * x^i)"
  1705 using diff_power_eq_setsum[of x "n - 1" y]
  1706 by (cases "n = 0") (simp_all add: field_simps)
  1707 
  1708 lemma power_diff_1_eq:
  1709   fixes x :: "'a::{comm_ring,monoid_mult}"
  1710   shows "n \<noteq> 0 \<Longrightarrow> x^n - 1 = (x - 1) * (\<Sum>i<n. (x^i))"
  1711 using diff_power_eq_setsum [of x _ 1]
  1712   by (cases n) auto
  1713 
  1714 lemma one_diff_power_eq':
  1715   fixes x :: "'a::{comm_ring,monoid_mult}"
  1716   shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^(n - Suc i))"
  1717 using diff_power_eq_setsum [of 1 _ x]
  1718   by (cases n) auto
  1719 
  1720 lemma one_diff_power_eq:
  1721   fixes x :: "'a::{comm_ring,monoid_mult}"
  1722   shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^i)"
  1723 by (metis one_diff_power_eq' [of n x] nat_diff_setsum_reindex)
  1724 
  1725 
  1726 subsection \<open>The formula for arithmetic sums\<close>
  1727 
  1728 lemma gauss_sum:
  1729   "(2::'a::comm_semiring_1)*(\<Sum>i\<in>{1..n}. of_nat i) = of_nat n*((of_nat n)+1)"
  1730 proof (induct n)
  1731   case 0
  1732   show ?case by simp
  1733 next
  1734   case (Suc n)
  1735   then show ?case
  1736     by (simp add: algebra_simps add: one_add_one [symmetric] del: one_add_one)
  1737       (* FIXME: make numeral cancellation simprocs work for semirings *)
  1738 qed
  1739 
  1740 theorem arith_series_general:
  1741   "(2::'a::comm_semiring_1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
  1742   of_nat n * (a + (a + of_nat(n - 1)*d))"
  1743 proof cases
  1744   assume ngt1: "n > 1"
  1745   let ?I = "\<lambda>i. of_nat i" and ?n = "of_nat n"
  1746   have
  1747     "(\<Sum>i\<in>{..<n}. a+?I i*d) =
  1748      ((\<Sum>i\<in>{..<n}. a) + (\<Sum>i\<in>{..<n}. ?I i*d))"
  1749     by (rule setsum.distrib)
  1750   also from ngt1 have "\<dots> = ?n*a + (\<Sum>i\<in>{..<n}. ?I i*d)" by simp
  1751   also from ngt1 have "\<dots> = (?n*a + d*(\<Sum>i\<in>{1..<n}. ?I i))"
  1752     unfolding One_nat_def
  1753     by (simp add: setsum_right_distrib atLeast0LessThan[symmetric] setsum_shift_lb_Suc0_0_upt ac_simps)
  1754   also have "2*\<dots> = 2*?n*a + d*2*(\<Sum>i\<in>{1..<n}. ?I i)"
  1755     by (simp add: algebra_simps)
  1756   also from ngt1 have "{1..<n} = {1..n - 1}"
  1757     by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost)
  1758   also from ngt1
  1759   have "2*?n*a + d*2*(\<Sum>i\<in>{1..n - 1}. ?I i) = (2*?n*a + d*?I (n - 1)*?I n)"
  1760     by (simp only: mult.assoc gauss_sum [of "n - 1"], unfold One_nat_def)
  1761       (simp add:  mult.commute trans [OF add.commute of_nat_Suc [symmetric]])
  1762   finally show ?thesis
  1763     unfolding mult_2 by (simp add: algebra_simps)
  1764 next
  1765   assume "\<not>(n > 1)"
  1766   hence "n = 1 \<or> n = 0" by auto
  1767   thus ?thesis by (auto simp: mult_2)
  1768 qed
  1769 
  1770 lemma arith_series_nat:
  1771   "(2::nat) * (\<Sum>i\<in>{..<n}. a+i*d) = n * (a + (a+(n - 1)*d))"
  1772 proof -
  1773   have
  1774     "2 * (\<Sum>i\<in>{..<n::nat}. a + of_nat(i)*d) =
  1775     of_nat(n) * (a + (a + of_nat(n - 1)*d))"
  1776     by (rule arith_series_general)
  1777   thus ?thesis
  1778     unfolding One_nat_def by auto
  1779 qed
  1780 
  1781 lemma arith_series_int:
  1782   "2 * (\<Sum>i\<in>{..<n}. a + int i * d) = int n * (a + (a + int(n - 1)*d))"
  1783   by (fact arith_series_general) (* FIXME: duplicate *)
  1784 
  1785 lemma sum_diff_distrib: "\<forall>x. Q x \<le> P x  \<Longrightarrow> (\<Sum>x<n. P x) - (\<Sum>x<n. Q x) = (\<Sum>x<n. P x - Q x :: nat)"
  1786   by (subst setsum_subtractf_nat) auto
  1787 
  1788 
  1789 subsection \<open>Products indexed over intervals\<close>
  1790 
  1791 syntax (ASCII)
  1792   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _ = _.._./ _)" [0,0,0,10] 10)
  1793   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _ = _..<_./ _)" [0,0,0,10] 10)
  1794   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _<_./ _)" [0,0,10] 10)
  1795   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _<=_./ _)" [0,0,10] 10)
  1796 
  1797 syntax (latex_prod output)
  1798   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1799  ("(3\<^raw:$\prod_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
  1800   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1801  ("(3\<^raw:$\prod_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
  1802   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1803  ("(3\<^raw:$\prod_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
  1804   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1805  ("(3\<^raw:$\prod_{>_ \<le> _\<^raw:}$> _)" [0,0,10] 10)
  1806 
  1807 syntax
  1808   "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_ = _.._./ _)" [0,0,0,10] 10)
  1809   "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_ = _..<_./ _)" [0,0,0,10] 10)
  1810   "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_<_./ _)" [0,0,10] 10)
  1811   "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_\<le>_./ _)" [0,0,10] 10)
  1812 
  1813 translations
  1814   "\<Prod>x=a..b. t" \<rightleftharpoons> "CONST setprod (\<lambda>x. t) {a..b}"
  1815   "\<Prod>x=a..<b. t" \<rightleftharpoons> "CONST setprod (\<lambda>x. t) {a..<b}"
  1816   "\<Prod>i\<le>n. t" \<rightleftharpoons> "CONST setprod (\<lambda>i. t) {..n}"
  1817   "\<Prod>i<n. t" \<rightleftharpoons> "CONST setprod (\<lambda>i. t) {..<n}"
  1818 
  1819 
  1820 subsection \<open>Transfer setup\<close>
  1821 
  1822 lemma transfer_nat_int_set_functions:
  1823     "{..n} = nat ` {0..int n}"
  1824     "{m..n} = nat ` {int m..int n}"  (* need all variants of these! *)
  1825   apply (auto simp add: image_def)
  1826   apply (rule_tac x = "int x" in bexI)
  1827   apply auto
  1828   apply (rule_tac x = "int x" in bexI)
  1829   apply auto
  1830   done
  1831 
  1832 lemma transfer_nat_int_set_function_closures:
  1833     "x >= 0 \<Longrightarrow> nat_set {x..y}"
  1834   by (simp add: nat_set_def)
  1835 
  1836 declare transfer_morphism_nat_int[transfer add
  1837   return: transfer_nat_int_set_functions
  1838     transfer_nat_int_set_function_closures
  1839 ]
  1840 
  1841 lemma transfer_int_nat_set_functions:
  1842     "is_nat m \<Longrightarrow> is_nat n \<Longrightarrow> {m..n} = int ` {nat m..nat n}"
  1843   by (simp only: is_nat_def transfer_nat_int_set_functions
  1844     transfer_nat_int_set_function_closures
  1845     transfer_nat_int_set_return_embed nat_0_le
  1846     cong: transfer_nat_int_set_cong)
  1847 
  1848 lemma transfer_int_nat_set_function_closures:
  1849     "is_nat x \<Longrightarrow> nat_set {x..y}"
  1850   by (simp only: transfer_nat_int_set_function_closures is_nat_def)
  1851 
  1852 declare transfer_morphism_int_nat[transfer add
  1853   return: transfer_int_nat_set_functions
  1854     transfer_int_nat_set_function_closures
  1855 ]
  1856 
  1857 lemma setprod_int_plus_eq: "setprod int {i..i+j} =  \<Prod>{int i..int (i+j)}"
  1858   by (induct j) (auto simp add: atLeastAtMostSuc_conv atLeastAtMostPlus1_int_conv)
  1859 
  1860 lemma setprod_int_eq: "setprod int {i..j} =  \<Prod>{int i..int j}"
  1861 proof (cases "i \<le> j")
  1862   case True
  1863   then show ?thesis
  1864     by (metis Nat.le_iff_add setprod_int_plus_eq)
  1865 next
  1866   case False
  1867   then show ?thesis
  1868     by auto
  1869 qed
  1870 
  1871 
  1872 subsection \<open>Shifting bounds\<close>
  1873 
  1874 lemma setprod_shift_bounds_nat_ivl:
  1875   "setprod f {m+k..<n+k} = setprod (%i. f(i + k)){m..<n::nat}"
  1876 by (induct "n", auto simp:atLeastLessThanSuc)
  1877 
  1878 lemma setprod_shift_bounds_cl_nat_ivl:
  1879   "setprod f {m+k..n+k} = setprod (%i. f(i + k)){m..n::nat}"
  1880   by (rule setprod.reindex_bij_witness[where i="\<lambda>i. i + k" and j="\<lambda>i. i - k"]) auto
  1881 
  1882 corollary setprod_shift_bounds_cl_Suc_ivl:
  1883   "setprod f {Suc m..Suc n} = setprod (%i. f(Suc i)){m..n}"
  1884 by (simp add:setprod_shift_bounds_cl_nat_ivl[where k="Suc 0", simplified])
  1885 
  1886 corollary setprod_shift_bounds_Suc_ivl:
  1887   "setprod f {Suc m..<Suc n} = setprod (%i. f(Suc i)){m..<n}"
  1888 by (simp add:setprod_shift_bounds_nat_ivl[where k="Suc 0", simplified])
  1889 
  1890 lemma setprod_lessThan_Suc: "setprod f {..<Suc n} = setprod f {..<n} * f n"
  1891   by (simp add: lessThan_Suc mult.commute)
  1892 
  1893 lemma setprod_atLeastLessThan_Suc: "a \<le> b \<Longrightarrow> setprod f {a..<Suc b} = setprod f {a..<b} * f b"
  1894   by (simp add: atLeastLessThanSuc mult.commute)
  1895 
  1896 lemma setprod_nat_ivl_Suc':
  1897   assumes "m \<le> Suc n"
  1898   shows   "setprod f {m..Suc n} = f (Suc n) * setprod f {m..n}"
  1899 proof -
  1900   from assms have "{m..Suc n} = insert (Suc n) {m..n}" by auto
  1901   also have "setprod f \<dots> = f (Suc n) * setprod f {m..n}" by simp
  1902   finally show ?thesis .
  1903 qed
  1904 
  1905 
  1906 subsection \<open>Efficient folding over intervals\<close>
  1907 
  1908 function fold_atLeastAtMost_nat where
  1909   [simp del]: "fold_atLeastAtMost_nat f a (b::nat) acc =
  1910                  (if a > b then acc else fold_atLeastAtMost_nat f (a+1) b (f a acc))"
  1911 by pat_completeness auto
  1912 termination by (relation "measure (\<lambda>(_,a,b,_). Suc b - a)") auto
  1913 
  1914 lemma fold_atLeastAtMost_nat:
  1915   assumes "comp_fun_commute f"
  1916   shows   "fold_atLeastAtMost_nat f a b acc = Finite_Set.fold f acc {a..b}"
  1917 using assms
  1918 proof (induction f a b acc rule: fold_atLeastAtMost_nat.induct, goal_cases)
  1919   case (1 f a b acc)
  1920   interpret comp_fun_commute f by fact
  1921   show ?case
  1922   proof (cases "a > b")
  1923     case True
  1924     thus ?thesis by (subst fold_atLeastAtMost_nat.simps) auto
  1925   next
  1926     case False
  1927     with 1 show ?thesis
  1928       by (subst fold_atLeastAtMost_nat.simps)
  1929          (auto simp: atLeastAtMost_insertL[symmetric] fold_fun_left_comm)
  1930   qed
  1931 qed
  1932 
  1933 lemma setsum_atLeastAtMost_code:
  1934   "setsum f {a..b} = fold_atLeastAtMost_nat (\<lambda>a acc. f a + acc) a b 0"
  1935 proof -
  1936   have "comp_fun_commute (\<lambda>a. op + (f a))"
  1937     by unfold_locales (auto simp: o_def add_ac)
  1938   thus ?thesis
  1939     by (simp add: setsum.eq_fold fold_atLeastAtMost_nat o_def)
  1940 qed
  1941 
  1942 lemma setprod_atLeastAtMost_code:
  1943   "setprod f {a..b} = fold_atLeastAtMost_nat (\<lambda>a acc. f a * acc) a b 1"
  1944 proof -
  1945   have "comp_fun_commute (\<lambda>a. op * (f a))"
  1946     by unfold_locales (auto simp: o_def mult_ac)
  1947   thus ?thesis
  1948     by (simp add: setprod.eq_fold fold_atLeastAtMost_nat o_def)
  1949 qed
  1950 
  1951 (* TODO: Add support for more kinds of intervals here *)
  1952 
  1953 end