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