src/HOL/Set_Interval.thy
author haftmann
Mon Oct 09 19:10:47 2017 +0200 (2017-10-09)
changeset 66836 4eb431c3f974
parent 66490 cc66ab2373ce
child 66936 cf8d8fc23891
permissions -rw-r--r--
tuned imports
     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 Divides
    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 lemma infinite_Ioo_iff [simp]: "infinite {a<..<b} \<longleftrightarrow> a < b"
   555   using not_less_iff_gr_or_eq by (fastforce simp: infinite_Ioo)
   556 
   557 lemma infinite_Icc_iff [simp]: "infinite {a .. b} \<longleftrightarrow> a < b"
   558   using not_less_iff_gr_or_eq by (fastforce simp: infinite_Icc)
   559 
   560 lemma infinite_Ico_iff [simp]: "infinite {a..<b} \<longleftrightarrow> a < b"
   561   using not_less_iff_gr_or_eq by (fastforce simp: infinite_Ico)
   562 
   563 lemma infinite_Ioc_iff [simp]: "infinite {a<..b} \<longleftrightarrow> a < b"
   564   using not_less_iff_gr_or_eq by (fastforce simp: infinite_Ioc)
   565 
   566 end
   567 
   568 lemma infinite_Iio: "\<not> finite {..< a :: 'a :: {no_bot, linorder}}"
   569 proof
   570   assume "finite {..< a}"
   571   then have *: "\<And>x. x < a \<Longrightarrow> Min {..< a} \<le> x"
   572     by auto
   573   obtain x where "x < a"
   574     using lt_ex by auto
   575 
   576   obtain y where "y < Min {..< a}"
   577     using lt_ex by auto
   578   also have "Min {..< a} \<le> x"
   579     using \<open>x < a\<close> by fact
   580   also note \<open>x < a\<close>
   581   finally have "Min {..< a} \<le> y"
   582     by fact
   583   with \<open>y < Min {..< a}\<close> show False by auto
   584 qed
   585 
   586 lemma infinite_Iic: "\<not> finite {.. a :: 'a :: {no_bot, linorder}}"
   587   using infinite_Iio[of a] finite_subset[of "{..< a}" "{.. a}"]
   588   by (auto simp: subset_eq less_imp_le)
   589 
   590 lemma infinite_Ioi: "\<not> finite {a :: 'a :: {no_top, linorder} <..}"
   591 proof
   592   assume "finite {a <..}"
   593   then have *: "\<And>x. a < x \<Longrightarrow> x \<le> Max {a <..}"
   594     by auto
   595 
   596   obtain y where "Max {a <..} < y"
   597     using gt_ex by auto
   598 
   599   obtain x where x: "a < x"
   600     using gt_ex by auto
   601   also from x have "x \<le> Max {a <..}"
   602     by fact
   603   also note \<open>Max {a <..} < y\<close>
   604   finally have "y \<le> Max { a <..}"
   605     by fact
   606   with \<open>Max {a <..} < y\<close> show False by auto
   607 qed
   608 
   609 lemma infinite_Ici: "\<not> finite {a :: 'a :: {no_top, linorder} ..}"
   610   using infinite_Ioi[of a] finite_subset[of "{a <..}" "{a ..}"]
   611   by (auto simp: subset_eq less_imp_le)
   612 
   613 subsubsection \<open>Intersection\<close>
   614 
   615 context linorder
   616 begin
   617 
   618 lemma Int_atLeastAtMost[simp]: "{a..b} Int {c..d} = {max a c .. min b d}"
   619 by auto
   620 
   621 lemma Int_atLeastAtMostR1[simp]: "{..b} Int {c..d} = {c .. min b d}"
   622 by auto
   623 
   624 lemma Int_atLeastAtMostR2[simp]: "{a..} Int {c..d} = {max a c .. d}"
   625 by auto
   626 
   627 lemma Int_atLeastAtMostL1[simp]: "{a..b} Int {..d} = {a .. min b d}"
   628 by auto
   629 
   630 lemma Int_atLeastAtMostL2[simp]: "{a..b} Int {c..} = {max a c .. b}"
   631 by auto
   632 
   633 lemma Int_atLeastLessThan[simp]: "{a..<b} Int {c..<d} = {max a c ..< min b d}"
   634 by auto
   635 
   636 lemma Int_greaterThanAtMost[simp]: "{a<..b} Int {c<..d} = {max a c <.. min b d}"
   637 by auto
   638 
   639 lemma Int_greaterThanLessThan[simp]: "{a<..<b} Int {c<..<d} = {max a c <..< min b d}"
   640 by auto
   641 
   642 lemma Int_atMost[simp]: "{..a} \<inter> {..b} = {.. min a b}"
   643   by (auto simp: min_def)
   644 
   645 lemma Ioc_disjoint: "{a<..b} \<inter> {c<..d} = {} \<longleftrightarrow> b \<le> a \<or> d \<le> c \<or> b \<le> c \<or> d \<le> a"
   646   by auto
   647 
   648 end
   649 
   650 context complete_lattice
   651 begin
   652 
   653 lemma
   654   shows Sup_atLeast[simp]: "Sup {x ..} = top"
   655     and Sup_greaterThanAtLeast[simp]: "x < top \<Longrightarrow> Sup {x <..} = top"
   656     and Sup_atMost[simp]: "Sup {.. y} = y"
   657     and Sup_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Sup { x .. y} = y"
   658     and Sup_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Sup { x <.. y} = y"
   659   by (auto intro!: Sup_eqI)
   660 
   661 lemma
   662   shows Inf_atMost[simp]: "Inf {.. x} = bot"
   663     and Inf_atMostLessThan[simp]: "top < x \<Longrightarrow> Inf {..< x} = bot"
   664     and Inf_atLeast[simp]: "Inf {x ..} = x"
   665     and Inf_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Inf { x .. y} = x"
   666     and Inf_atLeastLessThan[simp]: "x < y \<Longrightarrow> Inf { x ..< y} = x"
   667   by (auto intro!: Inf_eqI)
   668 
   669 end
   670 
   671 lemma
   672   fixes x y :: "'a :: {complete_lattice, dense_linorder}"
   673   shows Sup_lessThan[simp]: "Sup {..< y} = y"
   674     and Sup_atLeastLessThan[simp]: "x < y \<Longrightarrow> Sup { x ..< y} = y"
   675     and Sup_greaterThanLessThan[simp]: "x < y \<Longrightarrow> Sup { x <..< y} = y"
   676     and Inf_greaterThan[simp]: "Inf {x <..} = x"
   677     and Inf_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Inf { x <.. y} = x"
   678     and Inf_greaterThanLessThan[simp]: "x < y \<Longrightarrow> Inf { x <..< y} = x"
   679   by (auto intro!: Inf_eqI Sup_eqI intro: dense_le dense_le_bounded dense_ge dense_ge_bounded)
   680 
   681 subsection \<open>Intervals of natural numbers\<close>
   682 
   683 subsubsection \<open>The Constant @{term lessThan}\<close>
   684 
   685 lemma lessThan_0 [simp]: "lessThan (0::nat) = {}"
   686 by (simp add: lessThan_def)
   687 
   688 lemma lessThan_Suc: "lessThan (Suc k) = insert k (lessThan k)"
   689 by (simp add: lessThan_def less_Suc_eq, blast)
   690 
   691 text \<open>The following proof is convenient in induction proofs where
   692 new elements get indices at the beginning. So it is used to transform
   693 @{term "{..<Suc n}"} to @{term "0::nat"} and @{term "{..< n}"}.\<close>
   694 
   695 lemma zero_notin_Suc_image: "0 \<notin> Suc ` A"
   696   by auto
   697 
   698 lemma lessThan_Suc_eq_insert_0: "{..<Suc n} = insert 0 (Suc ` {..<n})"
   699   by (auto simp: image_iff less_Suc_eq_0_disj)
   700 
   701 lemma lessThan_Suc_atMost: "lessThan (Suc k) = atMost k"
   702 by (simp add: lessThan_def atMost_def less_Suc_eq_le)
   703 
   704 lemma Iic_Suc_eq_insert_0: "{.. Suc n} = insert 0 (Suc ` {.. n})"
   705   unfolding lessThan_Suc_atMost[symmetric] lessThan_Suc_eq_insert_0[of "Suc n"] ..
   706 
   707 lemma UN_lessThan_UNIV: "(UN m::nat. lessThan m) = UNIV"
   708 by blast
   709 
   710 subsubsection \<open>The Constant @{term greaterThan}\<close>
   711 
   712 lemma greaterThan_0: "greaterThan 0 = range Suc"
   713 apply (simp add: greaterThan_def)
   714 apply (blast dest: gr0_conv_Suc [THEN iffD1])
   715 done
   716 
   717 lemma greaterThan_Suc: "greaterThan (Suc k) = greaterThan k - {Suc k}"
   718 apply (simp add: greaterThan_def)
   719 apply (auto elim: linorder_neqE)
   720 done
   721 
   722 lemma INT_greaterThan_UNIV: "(INT m::nat. greaterThan m) = {}"
   723 by blast
   724 
   725 subsubsection \<open>The Constant @{term atLeast}\<close>
   726 
   727 lemma atLeast_0 [simp]: "atLeast (0::nat) = UNIV"
   728 by (unfold atLeast_def UNIV_def, simp)
   729 
   730 lemma atLeast_Suc: "atLeast (Suc k) = atLeast k - {k}"
   731 apply (simp add: atLeast_def)
   732 apply (simp add: Suc_le_eq)
   733 apply (simp add: order_le_less, blast)
   734 done
   735 
   736 lemma atLeast_Suc_greaterThan: "atLeast (Suc k) = greaterThan k"
   737   by (auto simp add: greaterThan_def atLeast_def less_Suc_eq_le)
   738 
   739 lemma UN_atLeast_UNIV: "(UN m::nat. atLeast m) = UNIV"
   740 by blast
   741 
   742 subsubsection \<open>The Constant @{term atMost}\<close>
   743 
   744 lemma atMost_0 [simp]: "atMost (0::nat) = {0}"
   745 by (simp add: atMost_def)
   746 
   747 lemma atMost_Suc: "atMost (Suc k) = insert (Suc k) (atMost k)"
   748 apply (simp add: atMost_def)
   749 apply (simp add: less_Suc_eq order_le_less, blast)
   750 done
   751 
   752 lemma UN_atMost_UNIV: "(UN m::nat. atMost m) = UNIV"
   753 by blast
   754 
   755 subsubsection \<open>The Constant @{term atLeastLessThan}\<close>
   756 
   757 text\<open>The orientation of the following 2 rules is tricky. The lhs is
   758 defined in terms of the rhs.  Hence the chosen orientation makes sense
   759 in this theory --- the reverse orientation complicates proofs (eg
   760 nontermination). But outside, when the definition of the lhs is rarely
   761 used, the opposite orientation seems preferable because it reduces a
   762 specific concept to a more general one.\<close>
   763 
   764 lemma atLeast0LessThan [code_abbrev]: "{0::nat..<n} = {..<n}"
   765 by(simp add:lessThan_def atLeastLessThan_def)
   766 
   767 lemma atLeast0AtMost [code_abbrev]: "{0..n::nat} = {..n}"
   768 by(simp add:atMost_def atLeastAtMost_def)
   769 
   770 lemma lessThan_atLeast0:
   771   "{..<n} = {0::nat..<n}"
   772   by (simp add: atLeast0LessThan)
   773 
   774 lemma atMost_atLeast0:
   775   "{..n} = {0::nat..n}"
   776   by (simp add: atLeast0AtMost)
   777 
   778 lemma atLeastLessThan0: "{m..<0::nat} = {}"
   779 by (simp add: atLeastLessThan_def)
   780 
   781 lemma atLeast0_lessThan_Suc:
   782   "{0..<Suc n} = insert n {0..<n}"
   783   by (simp add: atLeast0LessThan lessThan_Suc)
   784 
   785 lemma atLeast0_lessThan_Suc_eq_insert_0:
   786   "{0..<Suc n} = insert 0 (Suc ` {0..<n})"
   787   by (simp add: atLeast0LessThan lessThan_Suc_eq_insert_0)
   788 
   789 
   790 subsubsection \<open>The Constant @{term atLeastAtMost}\<close>
   791 
   792 lemma atLeast0_atMost_Suc:
   793   "{0..Suc n} = insert (Suc n) {0..n}"
   794   by (simp add: atLeast0AtMost atMost_Suc)
   795 
   796 lemma atLeast0_atMost_Suc_eq_insert_0:
   797   "{0..Suc n} = insert 0 (Suc ` {0..n})"
   798   by (simp add: atLeast0AtMost Iic_Suc_eq_insert_0)
   799 
   800 
   801 subsubsection \<open>Intervals of nats with @{term Suc}\<close>
   802 
   803 text\<open>Not a simprule because the RHS is too messy.\<close>
   804 lemma atLeastLessThanSuc:
   805     "{m..<Suc n} = (if m \<le> n then insert n {m..<n} else {})"
   806 by (auto simp add: atLeastLessThan_def)
   807 
   808 lemma atLeastLessThan_singleton [simp]: "{m..<Suc m} = {m}"
   809 by (auto simp add: atLeastLessThan_def)
   810 (*
   811 lemma atLeast_sum_LessThan [simp]: "{m + k..<k::nat} = {}"
   812 by (induct k, simp_all add: atLeastLessThanSuc)
   813 
   814 lemma atLeastSucLessThan [simp]: "{Suc n..<n} = {}"
   815 by (auto simp add: atLeastLessThan_def)
   816 *)
   817 lemma atLeastLessThanSuc_atLeastAtMost: "{l..<Suc u} = {l..u}"
   818   by (simp add: lessThan_Suc_atMost atLeastAtMost_def atLeastLessThan_def)
   819 
   820 lemma atLeastSucAtMost_greaterThanAtMost: "{Suc l..u} = {l<..u}"
   821   by (simp add: atLeast_Suc_greaterThan atLeastAtMost_def
   822     greaterThanAtMost_def)
   823 
   824 lemma atLeastSucLessThan_greaterThanLessThan: "{Suc l..<u} = {l<..<u}"
   825   by (simp add: atLeast_Suc_greaterThan atLeastLessThan_def
   826     greaterThanLessThan_def)
   827 
   828 lemma atLeastAtMostSuc_conv: "m \<le> Suc n \<Longrightarrow> {m..Suc n} = insert (Suc n) {m..n}"
   829 by (auto simp add: atLeastAtMost_def)
   830 
   831 lemma atLeastAtMost_insertL: "m \<le> n \<Longrightarrow> insert m {Suc m..n} = {m ..n}"
   832 by auto
   833 
   834 text \<open>The analogous result is useful on @{typ int}:\<close>
   835 (* here, because we don't have an own int section *)
   836 lemma atLeastAtMostPlus1_int_conv:
   837   "m <= 1+n \<Longrightarrow> {m..1+n} = insert (1+n) {m..n::int}"
   838   by (auto intro: set_eqI)
   839 
   840 lemma atLeastLessThan_add_Un: "i \<le> j \<Longrightarrow> {i..<j+k} = {i..<j} \<union> {j..<j+k::nat}"
   841   apply (induct k)
   842   apply (simp_all add: atLeastLessThanSuc)
   843   done
   844 
   845 subsubsection \<open>Intervals and numerals\<close>
   846 
   847 lemma lessThan_nat_numeral:  \<comment>\<open>Evaluation for specific numerals\<close>
   848   "lessThan (numeral k :: nat) = insert (pred_numeral k) (lessThan (pred_numeral k))"
   849   by (simp add: numeral_eq_Suc lessThan_Suc)
   850 
   851 lemma atMost_nat_numeral:  \<comment>\<open>Evaluation for specific numerals\<close>
   852   "atMost (numeral k :: nat) = insert (numeral k) (atMost (pred_numeral k))"
   853   by (simp add: numeral_eq_Suc atMost_Suc)
   854 
   855 lemma atLeastLessThan_nat_numeral:  \<comment>\<open>Evaluation for specific numerals\<close>
   856   "atLeastLessThan m (numeral k :: nat) =
   857      (if m \<le> (pred_numeral k) then insert (pred_numeral k) (atLeastLessThan m (pred_numeral k))
   858                  else {})"
   859   by (simp add: numeral_eq_Suc atLeastLessThanSuc)
   860 
   861 subsubsection \<open>Image\<close>
   862 
   863 lemma image_add_atLeastAtMost [simp]:
   864   fixes k ::"'a::linordered_semidom"
   865   shows "(\<lambda>n. n+k) ` {i..j} = {i+k..j+k}" (is "?A = ?B")
   866 proof
   867   show "?A \<subseteq> ?B" by auto
   868 next
   869   show "?B \<subseteq> ?A"
   870   proof
   871     fix n assume a: "n : ?B"
   872     hence "n - k : {i..j}"
   873       by (auto simp: add_le_imp_le_diff add_le_add_imp_diff_le)
   874     moreover have "n = (n - k) + k" using a
   875     proof -
   876       have "k + i \<le> n"
   877         by (metis a add.commute atLeastAtMost_iff)
   878       hence "k + (n - k) = n"
   879         by (metis (no_types) ab_semigroup_add_class.add_ac(1) add_diff_cancel_left' le_add_diff_inverse)
   880       thus ?thesis
   881         by (simp add: add.commute)
   882     qed
   883     ultimately show "n : ?A" by blast
   884   qed
   885 qed
   886 
   887 lemma image_diff_atLeastAtMost [simp]:
   888   fixes d::"'a::linordered_idom" shows "(op - d ` {a..b}) = {d-b..d-a}"
   889   apply auto
   890   apply (rule_tac x="d-x" in rev_image_eqI, auto)
   891   done
   892 
   893 lemma image_mult_atLeastAtMost [simp]:
   894   fixes d::"'a::linordered_field"
   895   assumes "d>0" shows "(op * d ` {a..b}) = {d*a..d*b}"
   896   using assms
   897   by (auto simp: field_simps mult_le_cancel_right intro: rev_image_eqI [where x="x/d" for x])
   898 
   899 lemma image_affinity_atLeastAtMost:
   900   fixes c :: "'a::linordered_field"
   901   shows "((\<lambda>x. m*x + c) ` {a..b}) = (if {a..b}={} then {}
   902             else if 0 \<le> m then {m*a + c .. m *b + c}
   903             else {m*b + c .. m*a + c})"
   904   apply (case_tac "m=0", auto simp: mult_le_cancel_left)
   905   apply (rule_tac x="inverse m*(x-c)" in rev_image_eqI, auto simp: field_simps)
   906   apply (rule_tac x="inverse m*(x-c)" in rev_image_eqI, auto simp: field_simps)
   907   done
   908 
   909 lemma image_affinity_atLeastAtMost_diff:
   910   fixes c :: "'a::linordered_field"
   911   shows "((\<lambda>x. m*x - c) ` {a..b}) = (if {a..b}={} then {}
   912             else if 0 \<le> m then {m*a - c .. m*b - c}
   913             else {m*b - c .. m*a - c})"
   914   using image_affinity_atLeastAtMost [of m "-c" a b]
   915   by simp
   916 
   917 lemma image_affinity_atLeastAtMost_div:
   918   fixes c :: "'a::linordered_field"
   919   shows "((\<lambda>x. x/m + c) ` {a..b}) = (if {a..b}={} then {}
   920             else if 0 \<le> m then {a/m + c .. b/m + c}
   921             else {b/m + c .. a/m + c})"
   922   using image_affinity_atLeastAtMost [of "inverse m" c a b]
   923   by (simp add: field_class.field_divide_inverse algebra_simps)
   924 
   925 lemma image_affinity_atLeastAtMost_div_diff:
   926   fixes c :: "'a::linordered_field"
   927   shows "((\<lambda>x. x/m - c) ` {a..b}) = (if {a..b}={} then {}
   928             else if 0 \<le> m then {a/m - c .. b/m - c}
   929             else {b/m - c .. a/m - c})"
   930   using image_affinity_atLeastAtMost_diff [of "inverse m" c a b]
   931   by (simp add: field_class.field_divide_inverse algebra_simps)
   932 
   933 lemma image_add_atLeastLessThan:
   934   "(%n::nat. n+k) ` {i..<j} = {i+k..<j+k}" (is "?A = ?B")
   935 proof
   936   show "?A \<subseteq> ?B" by auto
   937 next
   938   show "?B \<subseteq> ?A"
   939   proof
   940     fix n assume a: "n : ?B"
   941     hence "n - k : {i..<j}" by auto
   942     moreover have "n = (n - k) + k" using a by auto
   943     ultimately show "n : ?A" by blast
   944   qed
   945 qed
   946 
   947 corollary image_Suc_lessThan:
   948   "Suc ` {..<n} = {1..n}"
   949   using image_add_atLeastLessThan [of 1 0 n]
   950   by (auto simp add: lessThan_Suc_atMost atLeast0LessThan)
   951 
   952 corollary image_Suc_atMost:
   953   "Suc ` {..n} = {1..Suc n}"
   954   using image_add_atLeastLessThan [of 1 0 "Suc n"]
   955   by (auto simp add: lessThan_Suc_atMost atLeast0LessThan)
   956 
   957 corollary image_Suc_atLeastAtMost[simp]:
   958   "Suc ` {i..j} = {Suc i..Suc j}"
   959 using image_add_atLeastAtMost[where k="Suc 0"] by simp
   960 
   961 corollary image_Suc_atLeastLessThan[simp]:
   962   "Suc ` {i..<j} = {Suc i..<Suc j}"
   963 using image_add_atLeastLessThan[where k="Suc 0"] by simp
   964 
   965 lemma atLeast1_lessThan_eq_remove0:
   966   "{Suc 0..<n} = {..<n} - {0}"
   967   by auto
   968 
   969 lemma atLeast1_atMost_eq_remove0:
   970   "{Suc 0..n} = {..n} - {0}"
   971   by auto
   972 
   973 lemma image_add_int_atLeastLessThan:
   974     "(%x. x + (l::int)) ` {0..<u-l} = {l..<u}"
   975   apply (auto simp add: image_def)
   976   apply (rule_tac x = "x - l" in bexI)
   977   apply auto
   978   done
   979 
   980 lemma image_minus_const_atLeastLessThan_nat:
   981   fixes c :: nat
   982   shows "(\<lambda>i. i - c) ` {x ..< y} =
   983       (if c < y then {x - c ..< y - c} else if x < y then {0} else {})"
   984     (is "_ = ?right")
   985 proof safe
   986   fix a assume a: "a \<in> ?right"
   987   show "a \<in> (\<lambda>i. i - c) ` {x ..< y}"
   988   proof cases
   989     assume "c < y" with a show ?thesis
   990       by (auto intro!: image_eqI[of _ _ "a + c"])
   991   next
   992     assume "\<not> c < y" with a show ?thesis
   993       by (auto intro!: image_eqI[of _ _ x] split: if_split_asm)
   994   qed
   995 qed auto
   996 
   997 lemma image_int_atLeastLessThan: "int ` {a..<b} = {int a..<int b}"
   998   by (auto intro!: image_eqI [where x = "nat x" for x])
   999 
  1000 context ordered_ab_group_add
  1001 begin
  1002 
  1003 lemma
  1004   fixes x :: 'a
  1005   shows image_uminus_greaterThan[simp]: "uminus ` {x<..} = {..<-x}"
  1006   and image_uminus_atLeast[simp]: "uminus ` {x..} = {..-x}"
  1007 proof safe
  1008   fix y assume "y < -x"
  1009   hence *:  "x < -y" using neg_less_iff_less[of "-y" x] by simp
  1010   have "- (-y) \<in> uminus ` {x<..}"
  1011     by (rule imageI) (simp add: *)
  1012   thus "y \<in> uminus ` {x<..}" by simp
  1013 next
  1014   fix y assume "y \<le> -x"
  1015   have "- (-y) \<in> uminus ` {x..}"
  1016     by (rule imageI) (insert \<open>y \<le> -x\<close>[THEN le_imp_neg_le], simp)
  1017   thus "y \<in> uminus ` {x..}" by simp
  1018 qed simp_all
  1019 
  1020 lemma
  1021   fixes x :: 'a
  1022   shows image_uminus_lessThan[simp]: "uminus ` {..<x} = {-x<..}"
  1023   and image_uminus_atMost[simp]: "uminus ` {..x} = {-x..}"
  1024 proof -
  1025   have "uminus ` {..<x} = uminus ` uminus ` {-x<..}"
  1026     and "uminus ` {..x} = uminus ` uminus ` {-x..}" by simp_all
  1027   thus "uminus ` {..<x} = {-x<..}" and "uminus ` {..x} = {-x..}"
  1028     by (simp_all add: image_image
  1029         del: image_uminus_greaterThan image_uminus_atLeast)
  1030 qed
  1031 
  1032 lemma
  1033   fixes x :: 'a
  1034   shows image_uminus_atLeastAtMost[simp]: "uminus ` {x..y} = {-y..-x}"
  1035   and image_uminus_greaterThanAtMost[simp]: "uminus ` {x<..y} = {-y..<-x}"
  1036   and image_uminus_atLeastLessThan[simp]: "uminus ` {x..<y} = {-y<..-x}"
  1037   and image_uminus_greaterThanLessThan[simp]: "uminus ` {x<..<y} = {-y<..<-x}"
  1038   by (simp_all add: atLeastAtMost_def greaterThanAtMost_def atLeastLessThan_def
  1039       greaterThanLessThan_def image_Int[OF inj_uminus] Int_commute)
  1040 end
  1041 
  1042 subsubsection \<open>Finiteness\<close>
  1043 
  1044 lemma finite_lessThan [iff]: fixes k :: nat shows "finite {..<k}"
  1045   by (induct k) (simp_all add: lessThan_Suc)
  1046 
  1047 lemma finite_atMost [iff]: fixes k :: nat shows "finite {..k}"
  1048   by (induct k) (simp_all add: atMost_Suc)
  1049 
  1050 lemma finite_greaterThanLessThan [iff]:
  1051   fixes l :: nat shows "finite {l<..<u}"
  1052 by (simp add: greaterThanLessThan_def)
  1053 
  1054 lemma finite_atLeastLessThan [iff]:
  1055   fixes l :: nat shows "finite {l..<u}"
  1056 by (simp add: atLeastLessThan_def)
  1057 
  1058 lemma finite_greaterThanAtMost [iff]:
  1059   fixes l :: nat shows "finite {l<..u}"
  1060 by (simp add: greaterThanAtMost_def)
  1061 
  1062 lemma finite_atLeastAtMost [iff]:
  1063   fixes l :: nat shows "finite {l..u}"
  1064 by (simp add: atLeastAtMost_def)
  1065 
  1066 text \<open>A bounded set of natural numbers is finite.\<close>
  1067 lemma bounded_nat_set_is_finite:
  1068   "(ALL i:N. i < (n::nat)) ==> finite N"
  1069 apply (rule finite_subset)
  1070  apply (rule_tac [2] finite_lessThan, auto)
  1071 done
  1072 
  1073 text \<open>A set of natural numbers is finite iff it is bounded.\<close>
  1074 lemma finite_nat_set_iff_bounded:
  1075   "finite(N::nat set) = (EX m. ALL n:N. n<m)" (is "?F = ?B")
  1076 proof
  1077   assume f:?F  show ?B
  1078     using Max_ge[OF \<open>?F\<close>, simplified less_Suc_eq_le[symmetric]] by blast
  1079 next
  1080   assume ?B show ?F using \<open>?B\<close> by(blast intro:bounded_nat_set_is_finite)
  1081 qed
  1082 
  1083 lemma finite_nat_set_iff_bounded_le:
  1084   "finite(N::nat set) = (EX m. ALL n:N. n<=m)"
  1085 apply(simp add:finite_nat_set_iff_bounded)
  1086 apply(blast dest:less_imp_le_nat le_imp_less_Suc)
  1087 done
  1088 
  1089 lemma finite_less_ub:
  1090      "!!f::nat=>nat. (!!n. n \<le> f n) ==> finite {n. f n \<le> u}"
  1091 by (rule_tac B="{..u}" in finite_subset, auto intro: order_trans)
  1092 
  1093 lemma bounded_Max_nat:
  1094   fixes P :: "nat \<Rightarrow> bool"
  1095   assumes x: "P x" and M: "\<And>x. P x \<Longrightarrow> x \<le> M"
  1096   obtains m where "P m" "\<And>x. P x \<Longrightarrow> x \<le> m"
  1097 proof -
  1098   have "finite {x. P x}"
  1099     using M finite_nat_set_iff_bounded_le by auto
  1100   then have "Max {x. P x} \<in> {x. P x}"
  1101     using Max_in x by auto
  1102   then show ?thesis
  1103     by (simp add: \<open>finite {x. P x}\<close> that)
  1104 qed
  1105 
  1106 
  1107 text\<open>Any subset of an interval of natural numbers the size of the
  1108 subset is exactly that interval.\<close>
  1109 
  1110 lemma subset_card_intvl_is_intvl:
  1111   assumes "A \<subseteq> {k..<k + card A}"
  1112   shows "A = {k..<k + card A}"
  1113 proof (cases "finite A")
  1114   case True
  1115   from this and assms show ?thesis
  1116   proof (induct A rule: finite_linorder_max_induct)
  1117     case empty thus ?case by auto
  1118   next
  1119     case (insert b A)
  1120     hence *: "b \<notin> A" by auto
  1121     with insert have "A <= {k..<k + card A}" and "b = k + card A"
  1122       by fastforce+
  1123     with insert * show ?case by auto
  1124   qed
  1125 next
  1126   case False
  1127   with assms show ?thesis by simp
  1128 qed
  1129 
  1130 
  1131 subsubsection \<open>Proving Inclusions and Equalities between Unions\<close>
  1132 
  1133 lemma UN_le_eq_Un0:
  1134   "(\<Union>i\<le>n::nat. M i) = (\<Union>i\<in>{1..n}. M i) \<union> M 0" (is "?A = ?B")
  1135 proof
  1136   show "?A <= ?B"
  1137   proof
  1138     fix x assume "x : ?A"
  1139     then obtain i where i: "i\<le>n" "x : M i" by auto
  1140     show "x : ?B"
  1141     proof(cases i)
  1142       case 0 with i show ?thesis by simp
  1143     next
  1144       case (Suc j) with i show ?thesis by auto
  1145     qed
  1146   qed
  1147 next
  1148   show "?B <= ?A" by fastforce
  1149 qed
  1150 
  1151 lemma UN_le_add_shift:
  1152   "(\<Union>i\<le>n::nat. M(i+k)) = (\<Union>i\<in>{k..n+k}. M i)" (is "?A = ?B")
  1153 proof
  1154   show "?A <= ?B" by fastforce
  1155 next
  1156   show "?B <= ?A"
  1157   proof
  1158     fix x assume "x : ?B"
  1159     then obtain i where i: "i : {k..n+k}" "x : M(i)" by auto
  1160     hence "i-k\<le>n & x : M((i-k)+k)" by auto
  1161     thus "x : ?A" by blast
  1162   qed
  1163 qed
  1164 
  1165 lemma UN_UN_finite_eq: "(\<Union>n::nat. \<Union>i\<in>{0..<n}. A i) = (\<Union>n. A n)"
  1166   by (auto simp add: atLeast0LessThan)
  1167 
  1168 lemma UN_finite_subset:
  1169   "(\<And>n::nat. (\<Union>i\<in>{0..<n}. A i) \<subseteq> C) \<Longrightarrow> (\<Union>n. A n) \<subseteq> C"
  1170   by (subst UN_UN_finite_eq [symmetric]) blast
  1171 
  1172 lemma UN_finite2_subset:
  1173   assumes "\<And>n::nat. (\<Union>i\<in>{0..<n}. A i) \<subseteq> (\<Union>i\<in>{0..<n + k}. B i)"
  1174   shows "(\<Union>n. A n) \<subseteq> (\<Union>n. B n)"
  1175 proof (rule UN_finite_subset, rule)
  1176   fix n and a
  1177   from assms have "(\<Union>i\<in>{0..<n}. A i) \<subseteq> (\<Union>i\<in>{0..<n + k}. B i)" .
  1178   moreover assume "a \<in> (\<Union>i\<in>{0..<n}. A i)"
  1179   ultimately have "a \<in> (\<Union>i\<in>{0..<n + k}. B i)" by blast
  1180   then show "a \<in> (\<Union>i. B i)" by (auto simp add: UN_UN_finite_eq)
  1181 qed
  1182 
  1183 lemma UN_finite2_eq:
  1184   "(\<And>n::nat. (\<Union>i\<in>{0..<n}. A i) = (\<Union>i\<in>{0..<n + k}. B i)) \<Longrightarrow>
  1185     (\<Union>n. A n) = (\<Union>n. B n)"
  1186   apply (rule subset_antisym)
  1187    apply (rule UN_finite2_subset, blast)
  1188   apply (rule UN_finite2_subset [where k=k])
  1189   apply (force simp add: atLeastLessThan_add_Un [of 0])
  1190   done
  1191 
  1192 
  1193 subsubsection \<open>Cardinality\<close>
  1194 
  1195 lemma card_lessThan [simp]: "card {..<u} = u"
  1196   by (induct u, simp_all add: lessThan_Suc)
  1197 
  1198 lemma card_atMost [simp]: "card {..u} = Suc u"
  1199   by (simp add: lessThan_Suc_atMost [THEN sym])
  1200 
  1201 lemma card_atLeastLessThan [simp]: "card {l..<u} = u - l"
  1202 proof -
  1203   have "{l..<u} = (%x. x + l) ` {..<u-l}"
  1204     apply (auto simp add: image_def atLeastLessThan_def lessThan_def)
  1205     apply (rule_tac x = "x - l" in exI)
  1206     apply arith
  1207     done
  1208   then have "card {l..<u} = card {..<u-l}"
  1209     by (simp add: card_image inj_on_def)
  1210   then show ?thesis
  1211     by simp
  1212 qed
  1213 
  1214 lemma card_atLeastAtMost [simp]: "card {l..u} = Suc u - l"
  1215   by (subst atLeastLessThanSuc_atLeastAtMost [THEN sym], simp)
  1216 
  1217 lemma card_greaterThanAtMost [simp]: "card {l<..u} = u - l"
  1218   by (subst atLeastSucAtMost_greaterThanAtMost [THEN sym], simp)
  1219 
  1220 lemma card_greaterThanLessThan [simp]: "card {l<..<u} = u - Suc l"
  1221   by (subst atLeastSucLessThan_greaterThanLessThan [THEN sym], simp)
  1222 
  1223 lemma subset_eq_atLeast0_lessThan_finite:
  1224   fixes n :: nat
  1225   assumes "N \<subseteq> {0..<n}"
  1226   shows "finite N"
  1227   using assms finite_atLeastLessThan by (rule finite_subset)
  1228 
  1229 lemma subset_eq_atLeast0_atMost_finite:
  1230   fixes n :: nat
  1231   assumes "N \<subseteq> {0..n}"
  1232   shows "finite N"
  1233   using assms finite_atLeastAtMost by (rule finite_subset)
  1234 
  1235 lemma ex_bij_betw_nat_finite:
  1236   "finite M \<Longrightarrow> \<exists>h. bij_betw h {0..<card M} M"
  1237 apply(drule finite_imp_nat_seg_image_inj_on)
  1238 apply(auto simp:atLeast0LessThan[symmetric] lessThan_def[symmetric] card_image bij_betw_def)
  1239 done
  1240 
  1241 lemma ex_bij_betw_finite_nat:
  1242   "finite M \<Longrightarrow> \<exists>h. bij_betw h M {0..<card M}"
  1243 by (blast dest: ex_bij_betw_nat_finite bij_betw_inv)
  1244 
  1245 lemma finite_same_card_bij:
  1246   "finite A \<Longrightarrow> finite B \<Longrightarrow> card A = card B \<Longrightarrow> EX h. bij_betw h A B"
  1247 apply(drule ex_bij_betw_finite_nat)
  1248 apply(drule ex_bij_betw_nat_finite)
  1249 apply(auto intro!:bij_betw_trans)
  1250 done
  1251 
  1252 lemma ex_bij_betw_nat_finite_1:
  1253   "finite M \<Longrightarrow> \<exists>h. bij_betw h {1 .. card M} M"
  1254 by (rule finite_same_card_bij) auto
  1255 
  1256 lemma bij_betw_iff_card:
  1257   assumes "finite A" "finite B"
  1258   shows "(\<exists>f. bij_betw f A B) \<longleftrightarrow> (card A = card B)"
  1259 proof
  1260   assume "card A = card B"
  1261   moreover obtain f where "bij_betw f A {0 ..< card A}"
  1262     using assms ex_bij_betw_finite_nat by blast
  1263   moreover obtain g where "bij_betw g {0 ..< card B} B"
  1264     using assms ex_bij_betw_nat_finite by blast
  1265   ultimately have "bij_betw (g o f) A B"
  1266     by (auto simp: bij_betw_trans)
  1267   thus "(\<exists>f. bij_betw f A B)" by blast
  1268 qed (auto simp: bij_betw_same_card)
  1269 
  1270 lemma inj_on_iff_card_le:
  1271   assumes FIN: "finite A" and FIN': "finite B"
  1272   shows "(\<exists>f. inj_on f A \<and> f ` A \<le> B) = (card A \<le> card B)"
  1273 proof (safe intro!: card_inj_on_le)
  1274   assume *: "card A \<le> card B"
  1275   obtain f where 1: "inj_on f A" and 2: "f ` A = {0 ..< card A}"
  1276   using FIN ex_bij_betw_finite_nat unfolding bij_betw_def by force
  1277   moreover obtain g where "inj_on g {0 ..< card B}" and 3: "g ` {0 ..< card B} = B"
  1278   using FIN' ex_bij_betw_nat_finite unfolding bij_betw_def by force
  1279   ultimately have "inj_on g (f ` A)" using subset_inj_on[of g _ "f ` A"] * by force
  1280   hence "inj_on (g o f) A" using 1 comp_inj_on by blast
  1281   moreover
  1282   {have "{0 ..< card A} \<le> {0 ..< card B}" using * by force
  1283    with 2 have "f ` A  \<le> {0 ..< card B}" by blast
  1284    hence "(g o f) ` A \<le> B" unfolding comp_def using 3 by force
  1285   }
  1286   ultimately show "(\<exists>f. inj_on f A \<and> f ` A \<le> B)" by blast
  1287 qed (insert assms, auto)
  1288 
  1289 lemma subset_eq_atLeast0_lessThan_card:
  1290   fixes n :: nat
  1291   assumes "N \<subseteq> {0..<n}"
  1292   shows "card N \<le> n"
  1293 proof -
  1294   from assms finite_lessThan have "card N \<le> card {0..<n}"
  1295     using card_mono by blast
  1296   then show ?thesis by simp
  1297 qed
  1298 
  1299 
  1300 subsection \<open>Intervals of integers\<close>
  1301 
  1302 lemma atLeastLessThanPlusOne_atLeastAtMost_int: "{l..<u+1} = {l..(u::int)}"
  1303   by (auto simp add: atLeastAtMost_def atLeastLessThan_def)
  1304 
  1305 lemma atLeastPlusOneAtMost_greaterThanAtMost_int: "{l+1..u} = {l<..(u::int)}"
  1306   by (auto simp add: atLeastAtMost_def greaterThanAtMost_def)
  1307 
  1308 lemma atLeastPlusOneLessThan_greaterThanLessThan_int:
  1309     "{l+1..<u} = {l<..<u::int}"
  1310   by (auto simp add: atLeastLessThan_def greaterThanLessThan_def)
  1311 
  1312 subsubsection \<open>Finiteness\<close>
  1313 
  1314 lemma image_atLeastZeroLessThan_int: "0 \<le> u ==>
  1315     {(0::int)..<u} = int ` {..<nat u}"
  1316   apply (unfold image_def lessThan_def)
  1317   apply auto
  1318   apply (rule_tac x = "nat x" in exI)
  1319   apply (auto simp add: zless_nat_eq_int_zless [THEN sym])
  1320   done
  1321 
  1322 lemma finite_atLeastZeroLessThan_int: "finite {(0::int)..<u}"
  1323   apply (cases "0 \<le> u")
  1324   apply (subst image_atLeastZeroLessThan_int, assumption)
  1325   apply (rule finite_imageI)
  1326   apply auto
  1327   done
  1328 
  1329 lemma finite_atLeastLessThan_int [iff]: "finite {l..<u::int}"
  1330   apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
  1331   apply (erule subst)
  1332   apply (rule finite_imageI)
  1333   apply (rule finite_atLeastZeroLessThan_int)
  1334   apply (rule image_add_int_atLeastLessThan)
  1335   done
  1336 
  1337 lemma finite_atLeastAtMost_int [iff]: "finite {l..(u::int)}"
  1338   by (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym], simp)
  1339 
  1340 lemma finite_greaterThanAtMost_int [iff]: "finite {l<..(u::int)}"
  1341   by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
  1342 
  1343 lemma finite_greaterThanLessThan_int [iff]: "finite {l<..<u::int}"
  1344   by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
  1345 
  1346 
  1347 subsubsection \<open>Cardinality\<close>
  1348 
  1349 lemma card_atLeastZeroLessThan_int: "card {(0::int)..<u} = nat u"
  1350   apply (cases "0 \<le> u")
  1351   apply (subst image_atLeastZeroLessThan_int, assumption)
  1352   apply (subst card_image)
  1353   apply (auto simp add: inj_on_def)
  1354   done
  1355 
  1356 lemma card_atLeastLessThan_int [simp]: "card {l..<u} = nat (u - l)"
  1357   apply (subgoal_tac "card {l..<u} = card {0..<u-l}")
  1358   apply (erule ssubst, rule card_atLeastZeroLessThan_int)
  1359   apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
  1360   apply (erule subst)
  1361   apply (rule card_image)
  1362   apply (simp add: inj_on_def)
  1363   apply (rule image_add_int_atLeastLessThan)
  1364   done
  1365 
  1366 lemma card_atLeastAtMost_int [simp]: "card {l..u} = nat (u - l + 1)"
  1367 apply (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym])
  1368 apply (auto simp add: algebra_simps)
  1369 done
  1370 
  1371 lemma card_greaterThanAtMost_int [simp]: "card {l<..u} = nat (u - l)"
  1372 by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
  1373 
  1374 lemma card_greaterThanLessThan_int [simp]: "card {l<..<u} = nat (u - (l + 1))"
  1375 by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
  1376 
  1377 lemma finite_M_bounded_by_nat: "finite {k. P k \<and> k < (i::nat)}"
  1378 proof -
  1379   have "{k. P k \<and> k < i} \<subseteq> {..<i}" by auto
  1380   with finite_lessThan[of "i"] show ?thesis by (simp add: finite_subset)
  1381 qed
  1382 
  1383 lemma card_less:
  1384 assumes zero_in_M: "0 \<in> M"
  1385 shows "card {k \<in> M. k < Suc i} \<noteq> 0"
  1386 proof -
  1387   from zero_in_M have "{k \<in> M. k < Suc i} \<noteq> {}" by auto
  1388   with finite_M_bounded_by_nat show ?thesis by (auto simp add: card_eq_0_iff)
  1389 qed
  1390 
  1391 lemma card_less_Suc2: "0 \<notin> M \<Longrightarrow> card {k. Suc k \<in> M \<and> k < i} = card {k \<in> M. k < Suc i}"
  1392 apply (rule card_bij_eq [of Suc _ _ "\<lambda>x. x - Suc 0"])
  1393 apply auto
  1394 apply (rule inj_on_diff_nat)
  1395 apply auto
  1396 apply (case_tac x)
  1397 apply auto
  1398 apply (case_tac xa)
  1399 apply auto
  1400 apply (case_tac xa)
  1401 apply auto
  1402 done
  1403 
  1404 lemma card_less_Suc:
  1405   assumes zero_in_M: "0 \<in> M"
  1406     shows "Suc (card {k. Suc k \<in> M \<and> k < i}) = card {k \<in> M. k < Suc i}"
  1407 proof -
  1408   from assms have a: "0 \<in> {k \<in> M. k < Suc i}" by simp
  1409   hence c: "{k \<in> M. k < Suc i} = insert 0 ({k \<in> M. k < Suc i} - {0})"
  1410     by (auto simp only: insert_Diff)
  1411   have b: "{k \<in> M. k < Suc i} - {0} = {k \<in> M - {0}. k < Suc i}"  by auto
  1412   from finite_M_bounded_by_nat[of "\<lambda>x. x \<in> M" "Suc i"]
  1413   have "Suc (card {k. Suc k \<in> M \<and> k < i}) = card (insert 0 ({k \<in> M. k < Suc i} - {0}))"
  1414     apply (subst card_insert)
  1415     apply simp_all
  1416     apply (subst b)
  1417     apply (subst card_less_Suc2[symmetric])
  1418     apply simp_all
  1419     done
  1420   with c show ?thesis by simp
  1421 qed
  1422 
  1423 
  1424 subsection \<open>Lemmas useful with the summation operator sum\<close>
  1425 
  1426 text \<open>For examples, see Algebra/poly/UnivPoly2.thy\<close>
  1427 
  1428 subsubsection \<open>Disjoint Unions\<close>
  1429 
  1430 text \<open>Singletons and open intervals\<close>
  1431 
  1432 lemma ivl_disj_un_singleton:
  1433   "{l::'a::linorder} Un {l<..} = {l..}"
  1434   "{..<u} Un {u::'a::linorder} = {..u}"
  1435   "(l::'a::linorder) < u ==> {l} Un {l<..<u} = {l..<u}"
  1436   "(l::'a::linorder) < u ==> {l<..<u} Un {u} = {l<..u}"
  1437   "(l::'a::linorder) <= u ==> {l} Un {l<..u} = {l..u}"
  1438   "(l::'a::linorder) <= u ==> {l..<u} Un {u} = {l..u}"
  1439 by auto
  1440 
  1441 text \<open>One- and two-sided intervals\<close>
  1442 
  1443 lemma ivl_disj_un_one:
  1444   "(l::'a::linorder) < u ==> {..l} Un {l<..<u} = {..<u}"
  1445   "(l::'a::linorder) <= u ==> {..<l} Un {l..<u} = {..<u}"
  1446   "(l::'a::linorder) <= u ==> {..l} Un {l<..u} = {..u}"
  1447   "(l::'a::linorder) <= u ==> {..<l} Un {l..u} = {..u}"
  1448   "(l::'a::linorder) <= u ==> {l<..u} Un {u<..} = {l<..}"
  1449   "(l::'a::linorder) < u ==> {l<..<u} Un {u..} = {l<..}"
  1450   "(l::'a::linorder) <= u ==> {l..u} Un {u<..} = {l..}"
  1451   "(l::'a::linorder) <= u ==> {l..<u} Un {u..} = {l..}"
  1452 by auto
  1453 
  1454 text \<open>Two- and two-sided intervals\<close>
  1455 
  1456 lemma ivl_disj_un_two:
  1457   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..<u} = {l<..<u}"
  1458   "[| (l::'a::linorder) <= m; m < u |] ==> {l<..m} Un {m<..<u} = {l<..<u}"
  1459   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..<u} = {l..<u}"
  1460   "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m<..<u} = {l..<u}"
  1461   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..u} = {l<..u}"
  1462   "[| (l::'a::linorder) <= m; m <= u |] ==> {l<..m} Un {m<..u} = {l<..u}"
  1463   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..u} = {l..u}"
  1464   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m<..u} = {l..u}"
  1465 by auto
  1466 
  1467 lemma ivl_disj_un_two_touch:
  1468   "[| (l::'a::linorder) < m; m < u |] ==> {l<..m} Un {m..<u} = {l<..<u}"
  1469   "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m..<u} = {l..<u}"
  1470   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..m} Un {m..u} = {l<..u}"
  1471   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m..u} = {l..u}"
  1472 by auto
  1473 
  1474 lemmas ivl_disj_un = ivl_disj_un_singleton ivl_disj_un_one ivl_disj_un_two ivl_disj_un_two_touch
  1475 
  1476 subsubsection \<open>Disjoint Intersections\<close>
  1477 
  1478 text \<open>One- and two-sided intervals\<close>
  1479 
  1480 lemma ivl_disj_int_one:
  1481   "{..l::'a::order} Int {l<..<u} = {}"
  1482   "{..<l} Int {l..<u} = {}"
  1483   "{..l} Int {l<..u} = {}"
  1484   "{..<l} Int {l..u} = {}"
  1485   "{l<..u} Int {u<..} = {}"
  1486   "{l<..<u} Int {u..} = {}"
  1487   "{l..u} Int {u<..} = {}"
  1488   "{l..<u} Int {u..} = {}"
  1489   by auto
  1490 
  1491 text \<open>Two- and two-sided intervals\<close>
  1492 
  1493 lemma ivl_disj_int_two:
  1494   "{l::'a::order<..<m} Int {m..<u} = {}"
  1495   "{l<..m} Int {m<..<u} = {}"
  1496   "{l..<m} Int {m..<u} = {}"
  1497   "{l..m} Int {m<..<u} = {}"
  1498   "{l<..<m} Int {m..u} = {}"
  1499   "{l<..m} Int {m<..u} = {}"
  1500   "{l..<m} Int {m..u} = {}"
  1501   "{l..m} Int {m<..u} = {}"
  1502   by auto
  1503 
  1504 lemmas ivl_disj_int = ivl_disj_int_one ivl_disj_int_two
  1505 
  1506 subsubsection \<open>Some Differences\<close>
  1507 
  1508 lemma ivl_diff[simp]:
  1509  "i \<le> n \<Longrightarrow> {i..<m} - {i..<n} = {n..<(m::'a::linorder)}"
  1510 by(auto)
  1511 
  1512 lemma (in linorder) lessThan_minus_lessThan [simp]:
  1513   "{..< n} - {..< m} = {m ..< n}"
  1514   by auto
  1515 
  1516 lemma (in linorder) atLeastAtMost_diff_ends:
  1517   "{a..b} - {a, b} = {a<..<b}"
  1518   by auto
  1519 
  1520 
  1521 subsubsection \<open>Some Subset Conditions\<close>
  1522 
  1523 lemma ivl_subset [simp]:
  1524  "({i..<j} \<subseteq> {m..<n}) = (j \<le> i | m \<le> i & j \<le> (n::'a::linorder))"
  1525 apply(auto simp:linorder_not_le)
  1526 apply(rule ccontr)
  1527 apply(insert linorder_le_less_linear[of i n])
  1528 apply(clarsimp simp:linorder_not_le)
  1529 apply(fastforce)
  1530 done
  1531 
  1532 
  1533 subsection \<open>Generic big monoid operation over intervals\<close>
  1534 
  1535 lemma inj_on_add_nat' [simp]:
  1536   "inj_on (plus k) N" for k :: nat
  1537   by rule simp
  1538 
  1539 context comm_monoid_set
  1540 begin
  1541 
  1542 lemma atLeast_lessThan_shift_bounds:
  1543   fixes m n k :: nat
  1544   shows "F g {m + k..<n + k} = F (g \<circ> plus k) {m..<n}"
  1545 proof -
  1546   have "{m + k..<n + k} = plus k ` {m..<n}"
  1547     by (auto simp add: image_add_atLeastLessThan [symmetric])
  1548   also have "F g (plus k ` {m..<n}) = F (g \<circ> plus k) {m..<n}"
  1549     by (rule reindex) simp
  1550   finally show ?thesis .
  1551 qed
  1552 
  1553 lemma atLeast_atMost_shift_bounds:
  1554   fixes m n k :: nat
  1555   shows "F g {m + k..n + k} = F (g \<circ> plus k) {m..n}"
  1556 proof -
  1557   have "{m + k..n + k} = plus k ` {m..n}"
  1558     by (auto simp del: image_add_atLeastAtMost simp add: image_add_atLeastAtMost [symmetric])
  1559   also have "F g (plus k ` {m..n}) = F (g \<circ> plus k) {m..n}"
  1560     by (rule reindex) simp
  1561   finally show ?thesis .
  1562 qed
  1563 
  1564 lemma atLeast_Suc_lessThan_Suc_shift:
  1565   "F g {Suc m..<Suc n} = F (g \<circ> Suc) {m..<n}"
  1566   using atLeast_lessThan_shift_bounds [of _ _ 1] by simp
  1567 
  1568 lemma atLeast_Suc_atMost_Suc_shift:
  1569   "F g {Suc m..Suc n} = F (g \<circ> Suc) {m..n}"
  1570   using atLeast_atMost_shift_bounds [of _ _ 1] by simp
  1571 
  1572 lemma atLeast0_lessThan_Suc:
  1573   "F g {0..<Suc n} = F g {0..<n} \<^bold>* g n"
  1574   by (simp add: atLeast0_lessThan_Suc ac_simps)
  1575 
  1576 lemma atLeast0_atMost_Suc:
  1577   "F g {0..Suc n} = F g {0..n} \<^bold>* g (Suc n)"
  1578   by (simp add: atLeast0_atMost_Suc ac_simps)
  1579 
  1580 lemma atLeast0_lessThan_Suc_shift:
  1581   "F g {0..<Suc n} = g 0 \<^bold>* F (g \<circ> Suc) {0..<n}"
  1582   by (simp add: atLeast0_lessThan_Suc_eq_insert_0 atLeast_Suc_lessThan_Suc_shift)
  1583 
  1584 lemma atLeast0_atMost_Suc_shift:
  1585   "F g {0..Suc n} = g 0 \<^bold>* F (g \<circ> Suc) {0..n}"
  1586   by (simp add: atLeast0_atMost_Suc_eq_insert_0 atLeast_Suc_atMost_Suc_shift)
  1587 
  1588 lemma ivl_cong:
  1589   "a = c \<Longrightarrow> b = d \<Longrightarrow> (\<And>x. c \<le> x \<Longrightarrow> x < d \<Longrightarrow> g x = h x)
  1590     \<Longrightarrow> F g {a..<b} = F h {c..<d}"
  1591   by (rule cong) simp_all
  1592 
  1593 lemma atLeast_lessThan_shift_0:
  1594   fixes m n p :: nat
  1595   shows "F g {m..<n} = F (g \<circ> plus m) {0..<n - m}"
  1596   using atLeast_lessThan_shift_bounds [of g 0 m "n - m"]
  1597   by (cases "m \<le> n") simp_all
  1598 
  1599 lemma atLeast_atMost_shift_0:
  1600   fixes m n p :: nat
  1601   assumes "m \<le> n"
  1602   shows "F g {m..n} = F (g \<circ> plus m) {0..n - m}"
  1603   using assms atLeast_atMost_shift_bounds [of g 0 m "n - m"] by simp
  1604 
  1605 lemma atLeast_lessThan_concat:
  1606   fixes m n p :: nat
  1607   shows "m \<le> n \<Longrightarrow> n \<le> p \<Longrightarrow> F g {m..<n} \<^bold>* F g {n..<p} = F g {m..<p}"
  1608   by (simp add: union_disjoint [symmetric] ivl_disj_un)
  1609 
  1610 lemma atLeast_lessThan_rev:
  1611   "F g {n..<m} = F (\<lambda>i. g (m + n - Suc i)) {n..<m}"
  1612   by (rule reindex_bij_witness [where i="\<lambda>i. m + n - Suc i" and j="\<lambda>i. m + n - Suc i"], auto)
  1613 
  1614 lemma atLeast_atMost_rev:
  1615   fixes n m :: nat
  1616   shows "F g {n..m} = F (\<lambda>i. g (m + n - i)) {n..m}"
  1617   by (rule reindex_bij_witness [where i="\<lambda>i. m + n - i" and j="\<lambda>i. m + n - i"]) auto
  1618 
  1619 lemma atLeast_lessThan_rev_at_least_Suc_atMost:
  1620   "F g {n..<m} = F (\<lambda>i. g (m + n - i)) {Suc n..m}"
  1621   unfolding atLeast_lessThan_rev [of g n m]
  1622   by (cases m) (simp_all add: atLeast_Suc_atMost_Suc_shift atLeastLessThanSuc_atLeastAtMost)
  1623 
  1624 end
  1625 
  1626 
  1627 subsection \<open>Summation indexed over intervals\<close>
  1628 
  1629 syntax (ASCII)
  1630   "_from_to_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _ = _.._./ _)" [0,0,0,10] 10)
  1631   "_from_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _ = _..<_./ _)" [0,0,0,10] 10)
  1632   "_upt_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _<_./ _)" [0,0,10] 10)
  1633   "_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _<=_./ _)" [0,0,10] 10)
  1634 
  1635 syntax (latex_sum output)
  1636   "_from_to_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1637  ("(3\<^latex>\<open>$\\sum_{\<close>_ = _\<^latex>\<open>}^{\<close>_\<^latex>\<open>}$\<close> _)" [0,0,0,10] 10)
  1638   "_from_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1639  ("(3\<^latex>\<open>$\\sum_{\<close>_ = _\<^latex>\<open>}^{<\<close>_\<^latex>\<open>}$\<close> _)" [0,0,0,10] 10)
  1640   "_upt_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1641  ("(3\<^latex>\<open>$\\sum_{\<close>_ < _\<^latex>\<open>}$\<close> _)" [0,0,10] 10)
  1642   "_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  1643  ("(3\<^latex>\<open>$\\sum_{\<close>_ \<le> _\<^latex>\<open>}$\<close> _)" [0,0,10] 10)
  1644 
  1645 syntax
  1646   "_from_to_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
  1647   "_from_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
  1648   "_upt_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_<_./ _)" [0,0,10] 10)
  1649   "_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
  1650 
  1651 translations
  1652   "\<Sum>x=a..b. t" == "CONST sum (\<lambda>x. t) {a..b}"
  1653   "\<Sum>x=a..<b. t" == "CONST sum (\<lambda>x. t) {a..<b}"
  1654   "\<Sum>i\<le>n. t" == "CONST sum (\<lambda>i. t) {..n}"
  1655   "\<Sum>i<n. t" == "CONST sum (\<lambda>i. t) {..<n}"
  1656 
  1657 text\<open>The above introduces some pretty alternative syntaxes for
  1658 summation over intervals:
  1659 \begin{center}
  1660 \begin{tabular}{lll}
  1661 Old & New & \LaTeX\\
  1662 @{term[source]"\<Sum>x\<in>{a..b}. e"} & @{term"\<Sum>x=a..b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..b. e"}\\
  1663 @{term[source]"\<Sum>x\<in>{a..<b}. e"} & @{term"\<Sum>x=a..<b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..<b. e"}\\
  1664 @{term[source]"\<Sum>x\<in>{..b}. e"} & @{term"\<Sum>x\<le>b. e"} & @{term[mode=latex_sum]"\<Sum>x\<le>b. e"}\\
  1665 @{term[source]"\<Sum>x\<in>{..<b}. e"} & @{term"\<Sum>x<b. e"} & @{term[mode=latex_sum]"\<Sum>x<b. e"}
  1666 \end{tabular}
  1667 \end{center}
  1668 The left column shows the term before introduction of the new syntax,
  1669 the middle column shows the new (default) syntax, and the right column
  1670 shows a special syntax. The latter is only meaningful for latex output
  1671 and has to be activated explicitly by setting the print mode to
  1672 \<open>latex_sum\<close> (e.g.\ via \<open>mode = latex_sum\<close> in
  1673 antiquotations). It is not the default \LaTeX\ output because it only
  1674 works well with italic-style formulae, not tt-style.
  1675 
  1676 Note that for uniformity on @{typ nat} it is better to use
  1677 @{term"\<Sum>x::nat=0..<n. e"} rather than \<open>\<Sum>x<n. e\<close>: \<open>sum\<close> may
  1678 not provide all lemmas available for @{term"{m..<n}"} also in the
  1679 special form for @{term"{..<n}"}.\<close>
  1680 
  1681 text\<open>This congruence rule should be used for sums over intervals as
  1682 the standard theorem @{text[source]sum.cong} does not work well
  1683 with the simplifier who adds the unsimplified premise @{term"x:B"} to
  1684 the context.\<close>
  1685 
  1686 lemmas sum_ivl_cong = sum.ivl_cong
  1687 
  1688 (* FIXME why are the following simp rules but the corresponding eqns
  1689 on intervals are not? *)
  1690 
  1691 lemma sum_atMost_Suc [simp]:
  1692   "(\<Sum>i \<le> Suc n. f i) = (\<Sum>i \<le> n. f i) + f (Suc n)"
  1693   by (simp add: atMost_Suc ac_simps)
  1694 
  1695 lemma sum_lessThan_Suc [simp]:
  1696   "(\<Sum>i < Suc n. f i) = (\<Sum>i < n. f i) + f n"
  1697   by (simp add: lessThan_Suc ac_simps)
  1698 
  1699 lemma sum_cl_ivl_Suc [simp]:
  1700   "sum f {m..Suc n} = (if Suc n < m then 0 else sum f {m..n} + f(Suc n))"
  1701   by (auto simp: ac_simps atLeastAtMostSuc_conv)
  1702 
  1703 lemma sum_op_ivl_Suc [simp]:
  1704   "sum f {m..<Suc n} = (if n < m then 0 else sum f {m..<n} + f(n))"
  1705   by (auto simp: ac_simps atLeastLessThanSuc)
  1706 (*
  1707 lemma sum_cl_ivl_add_one_nat: "(n::nat) <= m + 1 ==>
  1708     (\<Sum>i=n..m+1. f i) = (\<Sum>i=n..m. f i) + f(m + 1)"
  1709 by (auto simp:ac_simps atLeastAtMostSuc_conv)
  1710 *)
  1711 
  1712 lemma sum_head:
  1713   fixes n :: nat
  1714   assumes mn: "m <= n"
  1715   shows "(\<Sum>x\<in>{m..n}. P x) = P m + (\<Sum>x\<in>{m<..n}. P x)" (is "?lhs = ?rhs")
  1716 proof -
  1717   from mn
  1718   have "{m..n} = {m} \<union> {m<..n}"
  1719     by (auto intro: ivl_disj_un_singleton)
  1720   hence "?lhs = (\<Sum>x\<in>{m} \<union> {m<..n}. P x)"
  1721     by (simp add: atLeast0LessThan)
  1722   also have "\<dots> = ?rhs" by simp
  1723   finally show ?thesis .
  1724 qed
  1725 
  1726 lemma sum_head_Suc:
  1727   "m \<le> n \<Longrightarrow> sum f {m..n} = f m + sum f {Suc m..n}"
  1728 by (simp add: sum_head atLeastSucAtMost_greaterThanAtMost)
  1729 
  1730 lemma sum_head_upt_Suc:
  1731   "m < n \<Longrightarrow> sum f {m..<n} = f m + sum f {Suc m..<n}"
  1732 apply(insert sum_head_Suc[of m "n - Suc 0" f])
  1733 apply (simp add: atLeastLessThanSuc_atLeastAtMost[symmetric] algebra_simps)
  1734 done
  1735 
  1736 lemma sum_ub_add_nat: assumes "(m::nat) \<le> n + 1"
  1737   shows "sum f {m..n + p} = sum f {m..n} + sum f {n + 1..n + p}"
  1738 proof-
  1739   have "{m .. n+p} = {m..n} \<union> {n+1..n+p}" using \<open>m \<le> n+1\<close> by auto
  1740   thus ?thesis by (auto simp: ivl_disj_int sum.union_disjoint
  1741     atLeastSucAtMost_greaterThanAtMost)
  1742 qed
  1743 
  1744 lemmas sum_add_nat_ivl = sum.atLeast_lessThan_concat
  1745 
  1746 lemma sum_diff_nat_ivl:
  1747 fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
  1748 shows "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
  1749   sum f {m..<p} - sum f {m..<n} = sum f {n..<p}"
  1750 using sum_add_nat_ivl [of m n p f,symmetric]
  1751 apply (simp add: ac_simps)
  1752 done
  1753 
  1754 lemma sum_natinterval_difff:
  1755   fixes f:: "nat \<Rightarrow> ('a::ab_group_add)"
  1756   shows  "sum (\<lambda>k. f k - f(k + 1)) {(m::nat) .. n} =
  1757           (if m <= n then f m - f(n + 1) else 0)"
  1758 by (induct n, auto simp add: algebra_simps not_le le_Suc_eq)
  1759 
  1760 lemma sum_nat_group: "(\<Sum>m<n::nat. sum f {m * k ..< m*k + k}) = sum f {..< n * k}"
  1761   apply (subgoal_tac "k = 0 | 0 < k", auto)
  1762   apply (induct "n")
  1763   apply (simp_all add: sum_add_nat_ivl add.commute atLeast0LessThan[symmetric])
  1764   done
  1765 
  1766 lemma sum_triangle_reindex:
  1767   fixes n :: nat
  1768   shows "(\<Sum>(i,j)\<in>{(i,j). i+j < n}. f i j) = (\<Sum>k<n. \<Sum>i\<le>k. f i (k - i))"
  1769   apply (simp add: sum.Sigma)
  1770   apply (rule sum.reindex_bij_witness[where j="\<lambda>(i, j). (i+j, i)" and i="\<lambda>(k, i). (i, k - i)"])
  1771   apply auto
  1772   done
  1773 
  1774 lemma sum_triangle_reindex_eq:
  1775   fixes n :: nat
  1776   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))"
  1777 using sum_triangle_reindex [of f "Suc n"]
  1778 by (simp only: Nat.less_Suc_eq_le lessThan_Suc_atMost)
  1779 
  1780 lemma nat_diff_sum_reindex: "(\<Sum>i<n. f (n - Suc i)) = (\<Sum>i<n. f i)"
  1781   by (rule sum.reindex_bij_witness[where i="\<lambda>i. n - Suc i" and j="\<lambda>i. n - Suc i"]) auto
  1782 
  1783 
  1784 subsubsection \<open>Shifting bounds\<close>
  1785 
  1786 lemma sum_shift_bounds_nat_ivl:
  1787   "sum f {m+k..<n+k} = sum (%i. f(i + k)){m..<n::nat}"
  1788 by (induct "n", auto simp:atLeastLessThanSuc)
  1789 
  1790 lemma sum_shift_bounds_cl_nat_ivl:
  1791   "sum f {m+k..n+k} = sum (%i. f(i + k)){m..n::nat}"
  1792   by (rule sum.reindex_bij_witness[where i="\<lambda>i. i + k" and j="\<lambda>i. i - k"]) auto
  1793 
  1794 corollary sum_shift_bounds_cl_Suc_ivl:
  1795   "sum f {Suc m..Suc n} = sum (%i. f(Suc i)){m..n}"
  1796 by (simp add:sum_shift_bounds_cl_nat_ivl[where k="Suc 0", simplified])
  1797 
  1798 corollary sum_shift_bounds_Suc_ivl:
  1799   "sum f {Suc m..<Suc n} = sum (%i. f(Suc i)){m..<n}"
  1800 by (simp add:sum_shift_bounds_nat_ivl[where k="Suc 0", simplified])
  1801 
  1802 lemma sum_shift_lb_Suc0_0:
  1803   "f(0::nat) = (0::nat) \<Longrightarrow> sum f {Suc 0..k} = sum f {0..k}"
  1804 by(simp add:sum_head_Suc)
  1805 
  1806 lemma sum_shift_lb_Suc0_0_upt:
  1807   "f(0::nat) = 0 \<Longrightarrow> sum f {Suc 0..<k} = sum f {0..<k}"
  1808 apply(cases k)apply simp
  1809 apply(simp add:sum_head_upt_Suc)
  1810 done
  1811 
  1812 lemma sum_atMost_Suc_shift:
  1813   fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
  1814   shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
  1815 proof (induct n)
  1816   case 0 show ?case by simp
  1817 next
  1818   case (Suc n) note IH = this
  1819   have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"
  1820     by (rule sum_atMost_Suc)
  1821   also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
  1822     by (rule IH)
  1823   also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
  1824              f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
  1825     by (rule add.assoc)
  1826   also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"
  1827     by (rule sum_atMost_Suc [symmetric])
  1828   finally show ?case .
  1829 qed
  1830 
  1831 lemma sum_lessThan_Suc_shift:
  1832   "(\<Sum>i<Suc n. f i) = f 0 + (\<Sum>i<n. f (Suc i))"
  1833   by (induction n) (simp_all add: add_ac)
  1834 
  1835 lemma sum_atMost_shift:
  1836   fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
  1837   shows "(\<Sum>i\<le>n. f i) = f 0 + (\<Sum>i<n. f (Suc i))"
  1838 by (metis atLeast0AtMost atLeast0LessThan atLeastLessThanSuc_atLeastAtMost atLeastSucAtMost_greaterThanAtMost le0 sum_head sum_shift_bounds_Suc_ivl)
  1839 
  1840 lemma sum_last_plus: fixes n::nat shows "m <= n \<Longrightarrow> (\<Sum>i = m..n. f i) = f n + (\<Sum>i = m..<n. f i)"
  1841   by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost add.commute)
  1842 
  1843 lemma sum_Suc_diff:
  1844   fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
  1845   assumes "m \<le> Suc n"
  1846   shows "(\<Sum>i = m..n. f(Suc i) - f i) = f (Suc n) - f m"
  1847 using assms by (induct n) (auto simp: le_Suc_eq)
  1848 
  1849 lemma sum_Suc_diff':
  1850   fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
  1851   assumes "m \<le> n"
  1852   shows "(\<Sum>i = m..<n. f (Suc i) - f i) = f n - f m"
  1853 using assms by (induct n) (auto simp: le_Suc_eq)
  1854 
  1855 lemma nested_sum_swap:
  1856      "(\<Sum>i = 0..n. (\<Sum>j = 0..<i. a i j)) = (\<Sum>j = 0..<n. \<Sum>i = Suc j..n. a i j)"
  1857   by (induction n) (auto simp: sum.distrib)
  1858 
  1859 lemma nested_sum_swap':
  1860      "(\<Sum>i\<le>n. (\<Sum>j<i. a i j)) = (\<Sum>j<n. \<Sum>i = Suc j..n. a i j)"
  1861   by (induction n) (auto simp: sum.distrib)
  1862 
  1863 lemma sum_atLeast1_atMost_eq:
  1864   "sum f {Suc 0..n} = (\<Sum>k<n. f (Suc k))"
  1865 proof -
  1866   have "sum f {Suc 0..n} = sum f (Suc ` {..<n})"
  1867     by (simp add: image_Suc_lessThan)
  1868   also have "\<dots> = (\<Sum>k<n. f (Suc k))"
  1869     by (simp add: sum.reindex)
  1870   finally show ?thesis .
  1871 qed
  1872 
  1873 
  1874 subsubsection \<open>Telescoping\<close>
  1875 
  1876 lemma sum_telescope:
  1877   fixes f::"nat \<Rightarrow> 'a::ab_group_add"
  1878   shows "sum (\<lambda>i. f i - f (Suc i)) {.. i} = f 0 - f (Suc i)"
  1879   by (induct i) simp_all
  1880 
  1881 lemma sum_telescope'':
  1882   assumes "m \<le> n"
  1883   shows   "(\<Sum>k\<in>{Suc m..n}. f k - f (k - 1)) = f n - (f m :: 'a :: ab_group_add)"
  1884   by (rule dec_induct[OF assms]) (simp_all add: algebra_simps)
  1885 
  1886 lemma sum_lessThan_telescope:
  1887   "(\<Sum>n<m. f (Suc n) - f n :: 'a :: ab_group_add) = f m - f 0"
  1888   by (induction m) (simp_all add: algebra_simps)
  1889 
  1890 lemma sum_lessThan_telescope':
  1891   "(\<Sum>n<m. f n - f (Suc n) :: 'a :: ab_group_add) = f 0 - f m"
  1892   by (induction m) (simp_all add: algebra_simps)
  1893 
  1894 
  1895 subsection \<open>The formula for geometric sums\<close>
  1896 
  1897 lemma sum_power2: "(\<Sum>i=0..<k. (2::nat)^i) = 2^k-1"
  1898 by (induction k) (auto simp: mult_2)
  1899 
  1900 lemma geometric_sum:
  1901   assumes "x \<noteq> 1"
  1902   shows "(\<Sum>i<n. x ^ i) = (x ^ n - 1) / (x - 1::'a::field)"
  1903 proof -
  1904   from assms obtain y where "y = x - 1" and "y \<noteq> 0" by simp_all
  1905   moreover have "(\<Sum>i<n. (y + 1) ^ i) = ((y + 1) ^ n - 1) / y"
  1906     by (induct n) (simp_all add: field_simps \<open>y \<noteq> 0\<close>)
  1907   ultimately show ?thesis by simp
  1908 qed
  1909 
  1910 lemma diff_power_eq_sum:
  1911   fixes y :: "'a::{comm_ring,monoid_mult}"
  1912   shows
  1913     "x ^ (Suc n) - y ^ (Suc n) =
  1914       (x - y) * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))"
  1915 proof (induct n)
  1916   case (Suc n)
  1917   have "x ^ Suc (Suc n) - y ^ Suc (Suc n) = x * (x * x^n) - y * (y * y ^ n)"
  1918     by simp
  1919   also have "... = y * (x ^ (Suc n) - y ^ (Suc n)) + (x - y) * (x * x^n)"
  1920     by (simp add: algebra_simps)
  1921   also have "... = y * ((x - y) * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))) + (x - y) * (x * x^n)"
  1922     by (simp only: Suc)
  1923   also have "... = (x - y) * (y * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))) + (x - y) * (x * x^n)"
  1924     by (simp only: mult.left_commute)
  1925   also have "... = (x - y) * (\<Sum>p<Suc (Suc n). x ^ p * y ^ (Suc n - p))"
  1926     by (simp add: field_simps Suc_diff_le sum_distrib_right sum_distrib_left)
  1927   finally show ?case .
  1928 qed simp
  1929 
  1930 corollary power_diff_sumr2: \<comment>\<open>\<open>COMPLEX_POLYFUN\<close> in HOL Light\<close>
  1931   fixes x :: "'a::{comm_ring,monoid_mult}"
  1932   shows   "x^n - y^n = (x - y) * (\<Sum>i<n. y^(n - Suc i) * x^i)"
  1933 using diff_power_eq_sum[of x "n - 1" y]
  1934 by (cases "n = 0") (simp_all add: field_simps)
  1935 
  1936 lemma power_diff_1_eq:
  1937   fixes x :: "'a::{comm_ring,monoid_mult}"
  1938   shows "n \<noteq> 0 \<Longrightarrow> x^n - 1 = (x - 1) * (\<Sum>i<n. (x^i))"
  1939 using diff_power_eq_sum [of x _ 1]
  1940   by (cases n) auto
  1941 
  1942 lemma one_diff_power_eq':
  1943   fixes x :: "'a::{comm_ring,monoid_mult}"
  1944   shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^(n - Suc i))"
  1945 using diff_power_eq_sum [of 1 _ x]
  1946   by (cases n) auto
  1947 
  1948 lemma one_diff_power_eq:
  1949   fixes x :: "'a::{comm_ring,monoid_mult}"
  1950   shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^i)"
  1951 by (metis one_diff_power_eq' [of n x] nat_diff_sum_reindex)
  1952 
  1953 lemma sum_gp_basic:
  1954   fixes x :: "'a::{comm_ring,monoid_mult}"
  1955   shows "(1 - x) * (\<Sum>i\<le>n. x^i) = 1 - x^Suc n"
  1956   by (simp only: one_diff_power_eq [of "Suc n" x] lessThan_Suc_atMost)
  1957 
  1958 lemma sum_power_shift:
  1959   fixes x :: "'a::{comm_ring,monoid_mult}"
  1960   assumes "m \<le> n"
  1961   shows "(\<Sum>i=m..n. x^i) = x^m * (\<Sum>i\<le>n-m. x^i)"
  1962 proof -
  1963   have "(\<Sum>i=m..n. x^i) = x^m * (\<Sum>i=m..n. x^(i-m))"
  1964     by (simp add: sum_distrib_left power_add [symmetric])
  1965   also have "(\<Sum>i=m..n. x^(i-m)) = (\<Sum>i\<le>n-m. x^i)"
  1966     using \<open>m \<le> n\<close> by (intro sum.reindex_bij_witness[where j="\<lambda>i. i - m" and i="\<lambda>i. i + m"]) auto
  1967   finally show ?thesis .
  1968 qed
  1969 
  1970 lemma sum_gp_multiplied:
  1971   fixes x :: "'a::{comm_ring,monoid_mult}"
  1972   assumes "m \<le> n"
  1973   shows "(1 - x) * (\<Sum>i=m..n. x^i) = x^m - x^Suc n"
  1974 proof -
  1975   have  "(1 - x) * (\<Sum>i=m..n. x^i) = x^m * (1 - x) * (\<Sum>i\<le>n-m. x^i)"
  1976     by (metis mult.assoc mult.commute assms sum_power_shift)
  1977   also have "... =x^m * (1 - x^Suc(n-m))"
  1978     by (metis mult.assoc sum_gp_basic)
  1979   also have "... = x^m - x^Suc n"
  1980     using assms
  1981     by (simp add: algebra_simps) (metis le_add_diff_inverse power_add)
  1982   finally show ?thesis .
  1983 qed
  1984 
  1985 lemma sum_gp:
  1986   fixes x :: "'a::{comm_ring,division_ring}"
  1987   shows   "(\<Sum>i=m..n. x^i) =
  1988                (if n < m then 0
  1989                 else if x = 1 then of_nat((n + 1) - m)
  1990                 else (x^m - x^Suc n) / (1 - x))"
  1991 using sum_gp_multiplied [of m n x] apply auto
  1992 by (metis eq_iff_diff_eq_0 mult.commute nonzero_divide_eq_eq)
  1993 
  1994 subsection\<open>Geometric progressions\<close>
  1995 
  1996 lemma sum_gp0:
  1997   fixes x :: "'a::{comm_ring,division_ring}"
  1998   shows   "(\<Sum>i\<le>n. x^i) = (if x = 1 then of_nat(n + 1) else (1 - x^Suc n) / (1 - x))"
  1999   using sum_gp_basic[of x n]
  2000   by (simp add: mult.commute divide_simps)
  2001 
  2002 lemma sum_power_add:
  2003   fixes x :: "'a::{comm_ring,monoid_mult}"
  2004   shows "(\<Sum>i\<in>I. x^(m+i)) = x^m * (\<Sum>i\<in>I. x^i)"
  2005   by (simp add: sum_distrib_left power_add)
  2006 
  2007 lemma sum_gp_offset:
  2008   fixes x :: "'a::{comm_ring,division_ring}"
  2009   shows   "(\<Sum>i=m..m+n. x^i) =
  2010        (if x = 1 then of_nat n + 1 else x^m * (1 - x^Suc n) / (1 - x))"
  2011   using sum_gp [of x m "m+n"]
  2012   by (auto simp: power_add algebra_simps)
  2013 
  2014 lemma sum_gp_strict:
  2015   fixes x :: "'a::{comm_ring,division_ring}"
  2016   shows "(\<Sum>i<n. x^i) = (if x = 1 then of_nat n else (1 - x^n) / (1 - x))"
  2017   by (induct n) (auto simp: algebra_simps divide_simps)
  2018 
  2019 subsubsection \<open>The formula for arithmetic sums\<close>
  2020 
  2021 lemma gauss_sum:
  2022   "(2::'a::comm_semiring_1)*(\<Sum>i\<in>{1..n}. of_nat i) = of_nat n*((of_nat n)+1)"
  2023 proof (induct n)
  2024   case 0
  2025   show ?case by simp
  2026 next
  2027   case (Suc n)
  2028   then show ?case
  2029     by (simp add: algebra_simps add: one_add_one [symmetric] del: one_add_one)
  2030       (* FIXME: make numeral cancellation simprocs work for semirings *)
  2031 qed
  2032 
  2033 theorem arith_series_general:
  2034   "(2::'a::comm_semiring_1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
  2035   of_nat n * (a + (a + of_nat(n - 1)*d))"
  2036 proof cases
  2037   assume ngt1: "n > 1"
  2038   let ?I = "\<lambda>i. of_nat i" and ?n = "of_nat n"
  2039   have
  2040     "(\<Sum>i\<in>{..<n}. a+?I i*d) =
  2041      ((\<Sum>i\<in>{..<n}. a) + (\<Sum>i\<in>{..<n}. ?I i*d))"
  2042     by (rule sum.distrib)
  2043   also from ngt1 have "\<dots> = ?n*a + (\<Sum>i\<in>{..<n}. ?I i*d)" by simp
  2044   also from ngt1 have "\<dots> = (?n*a + d*(\<Sum>i\<in>{1..<n}. ?I i))"
  2045     unfolding One_nat_def
  2046     by (simp add: sum_distrib_left atLeast0LessThan[symmetric] sum_shift_lb_Suc0_0_upt ac_simps)
  2047   also have "2*\<dots> = 2*?n*a + d*2*(\<Sum>i\<in>{1..<n}. ?I i)"
  2048     by (simp add: algebra_simps)
  2049   also from ngt1 have "{1..<n} = {1..n - 1}"
  2050     by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost)
  2051   also from ngt1
  2052   have "2*?n*a + d*2*(\<Sum>i\<in>{1..n - 1}. ?I i) = (2*?n*a + d*?I (n - 1)*?I n)"
  2053     by (simp only: mult.assoc gauss_sum [of "n - 1"], unfold One_nat_def)
  2054       (simp add:  mult.commute trans [OF add.commute of_nat_Suc [symmetric]])
  2055   finally show ?thesis
  2056     unfolding mult_2 by (simp add: algebra_simps)
  2057 next
  2058   assume "\<not>(n > 1)"
  2059   hence "n = 1 \<or> n = 0" by auto
  2060   thus ?thesis by (auto simp: mult_2)
  2061 qed
  2062 
  2063 lemma arith_series_nat:
  2064   "(2::nat) * (\<Sum>i\<in>{..<n}. a+i*d) = n * (a + (a+(n - 1)*d))"
  2065 proof -
  2066   have
  2067     "2 * (\<Sum>i\<in>{..<n::nat}. a + of_nat(i)*d) =
  2068     of_nat(n) * (a + (a + of_nat(n - 1)*d))"
  2069     by (rule arith_series_general)
  2070   thus ?thesis
  2071     unfolding One_nat_def by auto
  2072 qed
  2073 
  2074 lemma arith_series_int:
  2075   "2 * (\<Sum>i\<in>{..<n}. a + int i * d) = int n * (a + (a + int(n - 1)*d))"
  2076   by (fact arith_series_general) (* FIXME: duplicate *)
  2077 
  2078 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)"
  2079   by (subst sum_subtractf_nat) auto
  2080 
  2081 
  2082 subsubsection \<open>Division remainder\<close>
  2083 
  2084 lemma range_mod:
  2085   fixes n :: nat
  2086   assumes "n > 0"
  2087   shows "range (\<lambda>m. m mod n) = {0..<n}" (is "?A = ?B")
  2088 proof (rule set_eqI)
  2089   fix m
  2090   show "m \<in> ?A \<longleftrightarrow> m \<in> ?B"
  2091   proof
  2092     assume "m \<in> ?A"
  2093     with assms show "m \<in> ?B"
  2094       by auto
  2095   next
  2096     assume "m \<in> ?B"
  2097     moreover have "m mod n \<in> ?A"
  2098       by (rule rangeI)
  2099     ultimately show "m \<in> ?A"
  2100       by simp
  2101   qed
  2102 qed
  2103 
  2104 
  2105 subsection \<open>Products indexed over intervals\<close>
  2106 
  2107 syntax (ASCII)
  2108   "_from_to_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _ = _.._./ _)" [0,0,0,10] 10)
  2109   "_from_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _ = _..<_./ _)" [0,0,0,10] 10)
  2110   "_upt_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _<_./ _)" [0,0,10] 10)
  2111   "_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _<=_./ _)" [0,0,10] 10)
  2112 
  2113 syntax (latex_prod output)
  2114   "_from_to_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  2115  ("(3\<^latex>\<open>$\\prod_{\<close>_ = _\<^latex>\<open>}^{\<close>_\<^latex>\<open>}$\<close> _)" [0,0,0,10] 10)
  2116   "_from_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  2117  ("(3\<^latex>\<open>$\\prod_{\<close>_ = _\<^latex>\<open>}^{<\<close>_\<^latex>\<open>}$\<close> _)" [0,0,0,10] 10)
  2118   "_upt_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  2119  ("(3\<^latex>\<open>$\\prod_{\<close>_ < _\<^latex>\<open>}$\<close> _)" [0,0,10] 10)
  2120   "_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
  2121  ("(3\<^latex>\<open>$\\prod_{\<close>_ \<le> _\<^latex>\<open>}$\<close> _)" [0,0,10] 10)
  2122 
  2123 syntax
  2124   "_from_to_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_ = _.._./ _)" [0,0,0,10] 10)
  2125   "_from_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_ = _..<_./ _)" [0,0,0,10] 10)
  2126   "_upt_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_<_./ _)" [0,0,10] 10)
  2127   "_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_\<le>_./ _)" [0,0,10] 10)
  2128 
  2129 translations
  2130   "\<Prod>x=a..b. t" \<rightleftharpoons> "CONST prod (\<lambda>x. t) {a..b}"
  2131   "\<Prod>x=a..<b. t" \<rightleftharpoons> "CONST prod (\<lambda>x. t) {a..<b}"
  2132   "\<Prod>i\<le>n. t" \<rightleftharpoons> "CONST prod (\<lambda>i. t) {..n}"
  2133   "\<Prod>i<n. t" \<rightleftharpoons> "CONST prod (\<lambda>i. t) {..<n}"
  2134 
  2135 lemma prod_int_plus_eq: "prod int {i..i+j} =  \<Prod>{int i..int (i+j)}"
  2136   by (induct j) (auto simp add: atLeastAtMostSuc_conv atLeastAtMostPlus1_int_conv)
  2137 
  2138 lemma prod_int_eq: "prod int {i..j} =  \<Prod>{int i..int j}"
  2139 proof (cases "i \<le> j")
  2140   case True
  2141   then show ?thesis
  2142     by (metis le_iff_add prod_int_plus_eq)
  2143 next
  2144   case False
  2145   then show ?thesis
  2146     by auto
  2147 qed
  2148 
  2149 
  2150 subsubsection \<open>Shifting bounds\<close>
  2151 
  2152 lemma prod_shift_bounds_nat_ivl:
  2153   "prod f {m+k..<n+k} = prod (%i. f(i + k)){m..<n::nat}"
  2154 by (induct "n", auto simp:atLeastLessThanSuc)
  2155 
  2156 lemma prod_shift_bounds_cl_nat_ivl:
  2157   "prod f {m+k..n+k} = prod (%i. f(i + k)){m..n::nat}"
  2158   by (rule prod.reindex_bij_witness[where i="\<lambda>i. i + k" and j="\<lambda>i. i - k"]) auto
  2159 
  2160 corollary prod_shift_bounds_cl_Suc_ivl:
  2161   "prod f {Suc m..Suc n} = prod (%i. f(Suc i)){m..n}"
  2162 by (simp add:prod_shift_bounds_cl_nat_ivl[where k="Suc 0", simplified])
  2163 
  2164 corollary prod_shift_bounds_Suc_ivl:
  2165   "prod f {Suc m..<Suc n} = prod (%i. f(Suc i)){m..<n}"
  2166 by (simp add:prod_shift_bounds_nat_ivl[where k="Suc 0", simplified])
  2167 
  2168 lemma prod_lessThan_Suc: "prod f {..<Suc n} = prod f {..<n} * f n"
  2169   by (simp add: lessThan_Suc mult.commute)
  2170 
  2171 lemma prod_lessThan_Suc_shift:"(\<Prod>i<Suc n. f i) = f 0 * (\<Prod>i<n. f (Suc i))"
  2172   by (induction n) (simp_all add: lessThan_Suc mult_ac)
  2173 
  2174 lemma prod_atLeastLessThan_Suc: "a \<le> b \<Longrightarrow> prod f {a..<Suc b} = prod f {a..<b} * f b"
  2175   by (simp add: atLeastLessThanSuc mult.commute)
  2176 
  2177 lemma prod_nat_ivl_Suc':
  2178   assumes "m \<le> Suc n"
  2179   shows   "prod f {m..Suc n} = f (Suc n) * prod f {m..n}"
  2180 proof -
  2181   from assms have "{m..Suc n} = insert (Suc n) {m..n}" by auto
  2182   also have "prod f \<dots> = f (Suc n) * prod f {m..n}" by simp
  2183   finally show ?thesis .
  2184 qed
  2185 
  2186 
  2187 subsection \<open>Efficient folding over intervals\<close>
  2188 
  2189 function fold_atLeastAtMost_nat where
  2190   [simp del]: "fold_atLeastAtMost_nat f a (b::nat) acc =
  2191                  (if a > b then acc else fold_atLeastAtMost_nat f (a+1) b (f a acc))"
  2192 by pat_completeness auto
  2193 termination by (relation "measure (\<lambda>(_,a,b,_). Suc b - a)") auto
  2194 
  2195 lemma fold_atLeastAtMost_nat:
  2196   assumes "comp_fun_commute f"
  2197   shows   "fold_atLeastAtMost_nat f a b acc = Finite_Set.fold f acc {a..b}"
  2198 using assms
  2199 proof (induction f a b acc rule: fold_atLeastAtMost_nat.induct, goal_cases)
  2200   case (1 f a b acc)
  2201   interpret comp_fun_commute f by fact
  2202   show ?case
  2203   proof (cases "a > b")
  2204     case True
  2205     thus ?thesis by (subst fold_atLeastAtMost_nat.simps) auto
  2206   next
  2207     case False
  2208     with 1 show ?thesis
  2209       by (subst fold_atLeastAtMost_nat.simps)
  2210          (auto simp: atLeastAtMost_insertL[symmetric] fold_fun_left_comm)
  2211   qed
  2212 qed
  2213 
  2214 lemma sum_atLeastAtMost_code:
  2215   "sum f {a..b} = fold_atLeastAtMost_nat (\<lambda>a acc. f a + acc) a b 0"
  2216 proof -
  2217   have "comp_fun_commute (\<lambda>a. op + (f a))"
  2218     by unfold_locales (auto simp: o_def add_ac)
  2219   thus ?thesis
  2220     by (simp add: sum.eq_fold fold_atLeastAtMost_nat o_def)
  2221 qed
  2222 
  2223 lemma prod_atLeastAtMost_code:
  2224   "prod f {a..b} = fold_atLeastAtMost_nat (\<lambda>a acc. f a * acc) a b 1"
  2225 proof -
  2226   have "comp_fun_commute (\<lambda>a. op * (f a))"
  2227     by unfold_locales (auto simp: o_def mult_ac)
  2228   thus ?thesis
  2229     by (simp add: prod.eq_fold fold_atLeastAtMost_nat o_def)
  2230 qed
  2231 
  2232 (* TODO: Add support for more kinds of intervals here *)
  2233 
  2234 end