src/HOL/Set_Interval.thy
 author wenzelm Wed Nov 01 20:46:23 2017 +0100 (21 months ago) changeset 66983 df83b66f1d94 parent 66936 cf8d8fc23891 child 67091 1393c2340eec permissions -rw-r--r--
proper merge (amending fb46c031c841);
     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 lemma (in order) atLeast_lessThan_eq_atLeast_atMost_diff:

   222   "{a..<b} = {a..b} - {b}"

   223   by (auto simp add: atLeastLessThan_def atLeastAtMost_def)

   224

   225 end

   226

   227 subsubsection\<open>Emptyness, singletons, subset\<close>

   228

   229 context order

   230 begin

   231

   232 lemma atLeastatMost_empty[simp]:

   233   "b < a \<Longrightarrow> {a..b} = {}"

   234 by(auto simp: atLeastAtMost_def atLeast_def atMost_def)

   235

   236 lemma atLeastatMost_empty_iff[simp]:

   237   "{a..b} = {} \<longleftrightarrow> (~ a <= b)"

   238 by auto (blast intro: order_trans)

   239

   240 lemma atLeastatMost_empty_iff2[simp]:

   241   "{} = {a..b} \<longleftrightarrow> (~ a <= b)"

   242 by auto (blast intro: order_trans)

   243

   244 lemma atLeastLessThan_empty[simp]:

   245   "b <= a \<Longrightarrow> {a..<b} = {}"

   246 by(auto simp: atLeastLessThan_def)

   247

   248 lemma atLeastLessThan_empty_iff[simp]:

   249   "{a..<b} = {} \<longleftrightarrow> (~ a < b)"

   250 by auto (blast intro: le_less_trans)

   251

   252 lemma atLeastLessThan_empty_iff2[simp]:

   253   "{} = {a..<b} \<longleftrightarrow> (~ a < b)"

   254 by auto (blast intro: le_less_trans)

   255

   256 lemma greaterThanAtMost_empty[simp]: "l \<le> k ==> {k<..l} = {}"

   257 by(auto simp:greaterThanAtMost_def greaterThan_def atMost_def)

   258

   259 lemma greaterThanAtMost_empty_iff[simp]: "{k<..l} = {} \<longleftrightarrow> ~ k < l"

   260 by auto (blast intro: less_le_trans)

   261

   262 lemma greaterThanAtMost_empty_iff2[simp]: "{} = {k<..l} \<longleftrightarrow> ~ k < l"

   263 by auto (blast intro: less_le_trans)

   264

   265 lemma greaterThanLessThan_empty[simp]:"l \<le> k ==> {k<..<l} = {}"

   266 by(auto simp:greaterThanLessThan_def greaterThan_def lessThan_def)

   267

   268 lemma atLeastAtMost_singleton [simp]: "{a..a} = {a}"

   269 by (auto simp add: atLeastAtMost_def atMost_def atLeast_def)

   270

   271 lemma atLeastAtMost_singleton': "a = b \<Longrightarrow> {a .. b} = {a}" by simp

   272

   273 lemma atLeastatMost_subset_iff[simp]:

   274   "{a..b} <= {c..d} \<longleftrightarrow> (~ a <= b) | c <= a & b <= d"

   275 unfolding atLeastAtMost_def atLeast_def atMost_def

   276 by (blast intro: order_trans)

   277

   278 lemma atLeastatMost_psubset_iff:

   279   "{a..b} < {c..d} \<longleftrightarrow>

   280    ((~ a <= b) | c <= a & b <= d & (c < a | b < d))  &  c <= d"

   281 by(simp add: psubset_eq set_eq_iff less_le_not_le)(blast intro: order_trans)

   282

   283 lemma Icc_eq_Icc[simp]:

   284   "{l..h} = {l'..h'} = (l=l' \<and> h=h' \<or> \<not> l\<le>h \<and> \<not> l'\<le>h')"

   285 by(simp add: order_class.eq_iff)(auto intro: order_trans)

   286

   287 lemma atLeastAtMost_singleton_iff[simp]:

   288   "{a .. b} = {c} \<longleftrightarrow> a = b \<and> b = c"

   289 proof

   290   assume "{a..b} = {c}"

   291   hence *: "\<not> (\<not> a \<le> b)" unfolding atLeastatMost_empty_iff[symmetric] by simp

   292   with \<open>{a..b} = {c}\<close> have "c \<le> a \<and> b \<le> c" by auto

   293   with * show "a = b \<and> b = c" by auto

   294 qed simp

   295

   296 lemma Icc_subset_Ici_iff[simp]:

   297   "{l..h} \<subseteq> {l'..} = (~ l\<le>h \<or> l\<ge>l')"

   298 by(auto simp: subset_eq intro: order_trans)

   299

   300 lemma Icc_subset_Iic_iff[simp]:

   301   "{l..h} \<subseteq> {..h'} = (~ l\<le>h \<or> h\<le>h')"

   302 by(auto simp: subset_eq intro: order_trans)

   303

   304 lemma not_Ici_eq_empty[simp]: "{l..} \<noteq> {}"

   305 by(auto simp: set_eq_iff)

   306

   307 lemma not_Iic_eq_empty[simp]: "{..h} \<noteq> {}"

   308 by(auto simp: set_eq_iff)

   309

   310 lemmas not_empty_eq_Ici_eq_empty[simp] = not_Ici_eq_empty[symmetric]

   311 lemmas not_empty_eq_Iic_eq_empty[simp] = not_Iic_eq_empty[symmetric]

   312

   313 end

   314

   315 context no_top

   316 begin

   317

   318 (* also holds for no_bot but no_top should suffice *)

   319 lemma not_UNIV_le_Icc[simp]: "\<not> UNIV \<subseteq> {l..h}"

   320 using gt_ex[of h] by(auto simp: subset_eq less_le_not_le)

   321

   322 lemma not_UNIV_le_Iic[simp]: "\<not> UNIV \<subseteq> {..h}"

   323 using gt_ex[of h] by(auto simp: subset_eq less_le_not_le)

   324

   325 lemma not_Ici_le_Icc[simp]: "\<not> {l..} \<subseteq> {l'..h'}"

   326 using gt_ex[of h']

   327 by(auto simp: subset_eq less_le)(blast dest:antisym_conv intro: order_trans)

   328

   329 lemma not_Ici_le_Iic[simp]: "\<not> {l..} \<subseteq> {..h'}"

   330 using gt_ex[of h']

   331 by(auto simp: subset_eq less_le)(blast dest:antisym_conv intro: order_trans)

   332

   333 end

   334

   335 context no_bot

   336 begin

   337

   338 lemma not_UNIV_le_Ici[simp]: "\<not> UNIV \<subseteq> {l..}"

   339 using lt_ex[of l] by(auto simp: subset_eq less_le_not_le)

   340

   341 lemma not_Iic_le_Icc[simp]: "\<not> {..h} \<subseteq> {l'..h'}"

   342 using lt_ex[of l']

   343 by(auto simp: subset_eq less_le)(blast dest:antisym_conv intro: order_trans)

   344

   345 lemma not_Iic_le_Ici[simp]: "\<not> {..h} \<subseteq> {l'..}"

   346 using lt_ex[of l']

   347 by(auto simp: subset_eq less_le)(blast dest:antisym_conv intro: order_trans)

   348

   349 end

   350

   351

   352 context no_top

   353 begin

   354

   355 (* also holds for no_bot but no_top should suffice *)

   356 lemma not_UNIV_eq_Icc[simp]: "\<not> UNIV = {l'..h'}"

   357 using gt_ex[of h'] by(auto simp: set_eq_iff  less_le_not_le)

   358

   359 lemmas not_Icc_eq_UNIV[simp] = not_UNIV_eq_Icc[symmetric]

   360

   361 lemma not_UNIV_eq_Iic[simp]: "\<not> UNIV = {..h'}"

   362 using gt_ex[of h'] by(auto simp: set_eq_iff  less_le_not_le)

   363

   364 lemmas not_Iic_eq_UNIV[simp] = not_UNIV_eq_Iic[symmetric]

   365

   366 lemma not_Icc_eq_Ici[simp]: "\<not> {l..h} = {l'..}"

   367 unfolding atLeastAtMost_def using not_Ici_le_Iic[of l'] by blast

   368

   369 lemmas not_Ici_eq_Icc[simp] = not_Icc_eq_Ici[symmetric]

   370

   371 (* also holds for no_bot but no_top should suffice *)

   372 lemma not_Iic_eq_Ici[simp]: "\<not> {..h} = {l'..}"

   373 using not_Ici_le_Iic[of l' h] by blast

   374

   375 lemmas not_Ici_eq_Iic[simp] = not_Iic_eq_Ici[symmetric]

   376

   377 end

   378

   379 context no_bot

   380 begin

   381

   382 lemma not_UNIV_eq_Ici[simp]: "\<not> UNIV = {l'..}"

   383 using lt_ex[of l'] by(auto simp: set_eq_iff  less_le_not_le)

   384

   385 lemmas not_Ici_eq_UNIV[simp] = not_UNIV_eq_Ici[symmetric]

   386

   387 lemma not_Icc_eq_Iic[simp]: "\<not> {l..h} = {..h'}"

   388 unfolding atLeastAtMost_def using not_Iic_le_Ici[of h'] by blast

   389

   390 lemmas not_Iic_eq_Icc[simp] = not_Icc_eq_Iic[symmetric]

   391

   392 end

   393

   394

   395 context dense_linorder

   396 begin

   397

   398 lemma greaterThanLessThan_empty_iff[simp]:

   399   "{ a <..< b } = {} \<longleftrightarrow> b \<le> a"

   400   using dense[of a b] by (cases "a < b") auto

   401

   402 lemma greaterThanLessThan_empty_iff2[simp]:

   403   "{} = { a <..< b } \<longleftrightarrow> b \<le> a"

   404   using dense[of a b] by (cases "a < b") auto

   405

   406 lemma atLeastLessThan_subseteq_atLeastAtMost_iff:

   407   "{a ..< b} \<subseteq> { c .. d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b \<le> d)"

   408   using dense[of "max a d" "b"]

   409   by (force simp: subset_eq Ball_def not_less[symmetric])

   410

   411 lemma greaterThanAtMost_subseteq_atLeastAtMost_iff:

   412   "{a <.. b} \<subseteq> { c .. d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b \<le> d)"

   413   using dense[of "a" "min c b"]

   414   by (force simp: subset_eq Ball_def not_less[symmetric])

   415

   416 lemma greaterThanLessThan_subseteq_atLeastAtMost_iff:

   417   "{a <..< b} \<subseteq> { c .. d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b \<le> d)"

   418   using dense[of "a" "min c b"] dense[of "max a d" "b"]

   419   by (force simp: subset_eq Ball_def not_less[symmetric])

   420

   421 lemma atLeastAtMost_subseteq_atLeastLessThan_iff:

   422   "{a .. b} \<subseteq> { c ..< d } \<longleftrightarrow> (a \<le> b \<longrightarrow> c \<le> a \<and> b < d)"

   423   using dense[of "max a d" "b"]

   424   by (force simp: subset_eq Ball_def not_less[symmetric])

   425

   426 lemma greaterThanLessThan_subseteq_greaterThanLessThan:

   427   "{a <..< b} \<subseteq> {c <..< d} \<longleftrightarrow> (a < b \<longrightarrow> a \<ge> c \<and> b \<le> d)"

   428   using dense[of "a" "min c b"] dense[of "max a d" "b"]

   429   by (force simp: subset_eq Ball_def not_less[symmetric])

   430

   431 lemma greaterThanAtMost_subseteq_atLeastLessThan_iff:

   432   "{a <.. b} \<subseteq> { c ..< d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b < d)"

   433   using dense[of "a" "min c b"]

   434   by (force simp: subset_eq Ball_def not_less[symmetric])

   435

   436 lemma greaterThanLessThan_subseteq_atLeastLessThan_iff:

   437   "{a <..< b} \<subseteq> { c ..< d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b \<le> d)"

   438   using dense[of "a" "min c b"] dense[of "max a d" "b"]

   439   by (force simp: subset_eq Ball_def not_less[symmetric])

   440

   441 lemma greaterThanLessThan_subseteq_greaterThanAtMost_iff:

   442   "{a <..< b} \<subseteq> { c <.. d } \<longleftrightarrow> (a < b \<longrightarrow> c \<le> a \<and> b \<le> d)"

   443   using dense[of "a" "min c b"] dense[of "max a d" "b"]

   444   by (force simp: subset_eq Ball_def not_less[symmetric])

   445

   446 end

   447

   448 context no_top

   449 begin

   450

   451 lemma greaterThan_non_empty[simp]: "{x <..} \<noteq> {}"

   452   using gt_ex[of x] by auto

   453

   454 end

   455

   456 context no_bot

   457 begin

   458

   459 lemma lessThan_non_empty[simp]: "{..< x} \<noteq> {}"

   460   using lt_ex[of x] by auto

   461

   462 end

   463

   464 lemma (in linorder) atLeastLessThan_subset_iff:

   465   "{a..<b} <= {c..<d} \<Longrightarrow> b <= a | c<=a & b<=d"

   466 apply (auto simp:subset_eq Ball_def)

   467 apply(frule_tac x=a in spec)

   468 apply(erule_tac x=d in allE)

   469 apply (simp add: less_imp_le)

   470 done

   471

   472 lemma atLeastLessThan_inj:

   473   fixes a b c d :: "'a::linorder"

   474   assumes eq: "{a ..< b} = {c ..< d}" and "a < b" "c < d"

   475   shows "a = c" "b = d"

   476 using assms by (metis atLeastLessThan_subset_iff eq less_le_not_le linorder_antisym_conv2 subset_refl)+

   477

   478 lemma atLeastLessThan_eq_iff:

   479   fixes a b c d :: "'a::linorder"

   480   assumes "a < b" "c < d"

   481   shows "{a ..< b} = {c ..< d} \<longleftrightarrow> a = c \<and> b = d"

   482   using atLeastLessThan_inj assms by auto

   483

   484 lemma (in linorder) Ioc_inj: "{a <.. b} = {c <.. d} \<longleftrightarrow> (b \<le> a \<and> d \<le> c) \<or> a = c \<and> b = d"

   485   by (metis eq_iff greaterThanAtMost_empty_iff2 greaterThanAtMost_iff le_cases not_le)

   486

   487 lemma (in order) Iio_Int_singleton: "{..<k} \<inter> {x} = (if x < k then {x} else {})"

   488   by auto

   489

   490 lemma (in linorder) Ioc_subset_iff: "{a<..b} \<subseteq> {c<..d} \<longleftrightarrow> (b \<le> a \<or> c \<le> a \<and> b \<le> d)"

   491   by (auto simp: subset_eq Ball_def) (metis less_le not_less)

   492

   493 lemma (in order_bot) atLeast_eq_UNIV_iff: "{x..} = UNIV \<longleftrightarrow> x = bot"

   494 by (auto simp: set_eq_iff intro: le_bot)

   495

   496 lemma (in order_top) atMost_eq_UNIV_iff: "{..x} = UNIV \<longleftrightarrow> x = top"

   497 by (auto simp: set_eq_iff intro: top_le)

   498

   499 lemma (in bounded_lattice) atLeastAtMost_eq_UNIV_iff:

   500   "{x..y} = UNIV \<longleftrightarrow> (x = bot \<and> y = top)"

   501 by (auto simp: set_eq_iff intro: top_le le_bot)

   502

   503 lemma Iio_eq_empty_iff: "{..< n::'a::{linorder, order_bot}} = {} \<longleftrightarrow> n = bot"

   504   by (auto simp: set_eq_iff not_less le_bot)

   505

   506 lemma Iio_eq_empty_iff_nat: "{..< n::nat} = {} \<longleftrightarrow> n = 0"

   507   by (simp add: Iio_eq_empty_iff bot_nat_def)

   508

   509 lemma mono_image_least:

   510   assumes f_mono: "mono f" and f_img: "f  {m ..< n} = {m' ..< n'}" "m < n"

   511   shows "f m = m'"

   512 proof -

   513   from f_img have "{m' ..< n'} \<noteq> {}"

   514     by (metis atLeastLessThan_empty_iff image_is_empty)

   515   with f_img have "m' \<in> f  {m ..< n}" by auto

   516   then obtain k where "f k = m'" "m \<le> k" by auto

   517   moreover have "m' \<le> f m" using f_img by auto

   518   ultimately show "f m = m'"

   519     using f_mono by (auto elim: monoE[where x=m and y=k])

   520 qed

   521

   522

   523 subsection \<open>Infinite intervals\<close>

   524

   525 context dense_linorder

   526 begin

   527

   528 lemma infinite_Ioo:

   529   assumes "a < b"

   530   shows "\<not> finite {a<..<b}"

   531 proof

   532   assume fin: "finite {a<..<b}"

   533   moreover have ne: "{a<..<b} \<noteq> {}"

   534     using \<open>a < b\<close> by auto

   535   ultimately have "a < Max {a <..< b}" "Max {a <..< b} < b"

   536     using Max_in[of "{a <..< b}"] by auto

   537   then obtain x where "Max {a <..< b} < x" "x < b"

   538     using dense[of "Max {a<..<b}" b] by auto

   539   then have "x \<in> {a <..< b}"

   540     using \<open>a < Max {a <..< b}\<close> by auto

   541   then have "x \<le> Max {a <..< b}"

   542     using fin by auto

   543   with \<open>Max {a <..< b} < x\<close> show False by auto

   544 qed

   545

   546 lemma infinite_Icc: "a < b \<Longrightarrow> \<not> finite {a .. b}"

   547   using greaterThanLessThan_subseteq_atLeastAtMost_iff[of a b a b] infinite_Ioo[of a b]

   548   by (auto dest: finite_subset)

   549

   550 lemma infinite_Ico: "a < b \<Longrightarrow> \<not> finite {a ..< b}"

   551   using greaterThanLessThan_subseteq_atLeastLessThan_iff[of a b a b] infinite_Ioo[of a b]

   552   by (auto dest: finite_subset)

   553

   554 lemma infinite_Ioc: "a < b \<Longrightarrow> \<not> finite {a <.. b}"

   555   using greaterThanLessThan_subseteq_greaterThanAtMost_iff[of a b a b] infinite_Ioo[of a b]

   556   by (auto dest: finite_subset)

   557

   558 lemma infinite_Ioo_iff [simp]: "infinite {a<..<b} \<longleftrightarrow> a < b"

   559   using not_less_iff_gr_or_eq by (fastforce simp: infinite_Ioo)

   560

   561 lemma infinite_Icc_iff [simp]: "infinite {a .. b} \<longleftrightarrow> a < b"

   562   using not_less_iff_gr_or_eq by (fastforce simp: infinite_Icc)

   563

   564 lemma infinite_Ico_iff [simp]: "infinite {a..<b} \<longleftrightarrow> a < b"

   565   using not_less_iff_gr_or_eq by (fastforce simp: infinite_Ico)

   566

   567 lemma infinite_Ioc_iff [simp]: "infinite {a<..b} \<longleftrightarrow> a < b"

   568   using not_less_iff_gr_or_eq by (fastforce simp: infinite_Ioc)

   569

   570 end

   571

   572 lemma infinite_Iio: "\<not> finite {..< a :: 'a :: {no_bot, linorder}}"

   573 proof

   574   assume "finite {..< a}"

   575   then have *: "\<And>x. x < a \<Longrightarrow> Min {..< a} \<le> x"

   576     by auto

   577   obtain x where "x < a"

   578     using lt_ex by auto

   579

   580   obtain y where "y < Min {..< a}"

   581     using lt_ex by auto

   582   also have "Min {..< a} \<le> x"

   583     using \<open>x < a\<close> by fact

   584   also note \<open>x < a\<close>

   585   finally have "Min {..< a} \<le> y"

   586     by fact

   587   with \<open>y < Min {..< a}\<close> show False by auto

   588 qed

   589

   590 lemma infinite_Iic: "\<not> finite {.. a :: 'a :: {no_bot, linorder}}"

   591   using infinite_Iio[of a] finite_subset[of "{..< a}" "{.. a}"]

   592   by (auto simp: subset_eq less_imp_le)

   593

   594 lemma infinite_Ioi: "\<not> finite {a :: 'a :: {no_top, linorder} <..}"

   595 proof

   596   assume "finite {a <..}"

   597   then have *: "\<And>x. a < x \<Longrightarrow> x \<le> Max {a <..}"

   598     by auto

   599

   600   obtain y where "Max {a <..} < y"

   601     using gt_ex by auto

   602

   603   obtain x where x: "a < x"

   604     using gt_ex by auto

   605   also from x have "x \<le> Max {a <..}"

   606     by fact

   607   also note \<open>Max {a <..} < y\<close>

   608   finally have "y \<le> Max { a <..}"

   609     by fact

   610   with \<open>Max {a <..} < y\<close> show False by auto

   611 qed

   612

   613 lemma infinite_Ici: "\<not> finite {a :: 'a :: {no_top, linorder} ..}"

   614   using infinite_Ioi[of a] finite_subset[of "{a <..}" "{a ..}"]

   615   by (auto simp: subset_eq less_imp_le)

   616

   617 subsubsection \<open>Intersection\<close>

   618

   619 context linorder

   620 begin

   621

   622 lemma Int_atLeastAtMost[simp]: "{a..b} Int {c..d} = {max a c .. min b d}"

   623 by auto

   624

   625 lemma Int_atLeastAtMostR1[simp]: "{..b} Int {c..d} = {c .. min b d}"

   626 by auto

   627

   628 lemma Int_atLeastAtMostR2[simp]: "{a..} Int {c..d} = {max a c .. d}"

   629 by auto

   630

   631 lemma Int_atLeastAtMostL1[simp]: "{a..b} Int {..d} = {a .. min b d}"

   632 by auto

   633

   634 lemma Int_atLeastAtMostL2[simp]: "{a..b} Int {c..} = {max a c .. b}"

   635 by auto

   636

   637 lemma Int_atLeastLessThan[simp]: "{a..<b} Int {c..<d} = {max a c ..< min b d}"

   638 by auto

   639

   640 lemma Int_greaterThanAtMost[simp]: "{a<..b} Int {c<..d} = {max a c <.. min b d}"

   641 by auto

   642

   643 lemma Int_greaterThanLessThan[simp]: "{a<..<b} Int {c<..<d} = {max a c <..< min b d}"

   644 by auto

   645

   646 lemma Int_atMost[simp]: "{..a} \<inter> {..b} = {.. min a b}"

   647   by (auto simp: min_def)

   648

   649 lemma Ioc_disjoint: "{a<..b} \<inter> {c<..d} = {} \<longleftrightarrow> b \<le> a \<or> d \<le> c \<or> b \<le> c \<or> d \<le> a"

   650   by auto

   651

   652 end

   653

   654 context complete_lattice

   655 begin

   656

   657 lemma

   658   shows Sup_atLeast[simp]: "Sup {x ..} = top"

   659     and Sup_greaterThanAtLeast[simp]: "x < top \<Longrightarrow> Sup {x <..} = top"

   660     and Sup_atMost[simp]: "Sup {.. y} = y"

   661     and Sup_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Sup { x .. y} = y"

   662     and Sup_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Sup { x <.. y} = y"

   663   by (auto intro!: Sup_eqI)

   664

   665 lemma

   666   shows Inf_atMost[simp]: "Inf {.. x} = bot"

   667     and Inf_atMostLessThan[simp]: "top < x \<Longrightarrow> Inf {..< x} = bot"

   668     and Inf_atLeast[simp]: "Inf {x ..} = x"

   669     and Inf_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Inf { x .. y} = x"

   670     and Inf_atLeastLessThan[simp]: "x < y \<Longrightarrow> Inf { x ..< y} = x"

   671   by (auto intro!: Inf_eqI)

   672

   673 end

   674

   675 lemma

   676   fixes x y :: "'a :: {complete_lattice, dense_linorder}"

   677   shows Sup_lessThan[simp]: "Sup {..< y} = y"

   678     and Sup_atLeastLessThan[simp]: "x < y \<Longrightarrow> Sup { x ..< y} = y"

   679     and Sup_greaterThanLessThan[simp]: "x < y \<Longrightarrow> Sup { x <..< y} = y"

   680     and Inf_greaterThan[simp]: "Inf {x <..} = x"

   681     and Inf_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Inf { x <.. y} = x"

   682     and Inf_greaterThanLessThan[simp]: "x < y \<Longrightarrow> Inf { x <..< y} = x"

   683   by (auto intro!: Inf_eqI Sup_eqI intro: dense_le dense_le_bounded dense_ge dense_ge_bounded)

   684

   685 subsection \<open>Intervals of natural numbers\<close>

   686

   687 subsubsection \<open>The Constant @{term lessThan}\<close>

   688

   689 lemma lessThan_0 [simp]: "lessThan (0::nat) = {}"

   690 by (simp add: lessThan_def)

   691

   692 lemma lessThan_Suc: "lessThan (Suc k) = insert k (lessThan k)"

   693 by (simp add: lessThan_def less_Suc_eq, blast)

   694

   695 text \<open>The following proof is convenient in induction proofs where

   696 new elements get indices at the beginning. So it is used to transform

   697 @{term "{..<Suc n}"} to @{term "0::nat"} and @{term "{..< n}"}.\<close>

   698

   699 lemma zero_notin_Suc_image: "0 \<notin> Suc  A"

   700   by auto

   701

   702 lemma lessThan_Suc_eq_insert_0: "{..<Suc n} = insert 0 (Suc  {..<n})"

   703   by (auto simp: image_iff less_Suc_eq_0_disj)

   704

   705 lemma lessThan_Suc_atMost: "lessThan (Suc k) = atMost k"

   706 by (simp add: lessThan_def atMost_def less_Suc_eq_le)

   707

   708 lemma Iic_Suc_eq_insert_0: "{.. Suc n} = insert 0 (Suc  {.. n})"

   709   unfolding lessThan_Suc_atMost[symmetric] lessThan_Suc_eq_insert_0[of "Suc n"] ..

   710

   711 lemma UN_lessThan_UNIV: "(UN m::nat. lessThan m) = UNIV"

   712 by blast

   713

   714 subsubsection \<open>The Constant @{term greaterThan}\<close>

   715

   716 lemma greaterThan_0: "greaterThan 0 = range Suc"

   717 apply (simp add: greaterThan_def)

   718 apply (blast dest: gr0_conv_Suc [THEN iffD1])

   719 done

   720

   721 lemma greaterThan_Suc: "greaterThan (Suc k) = greaterThan k - {Suc k}"

   722 apply (simp add: greaterThan_def)

   723 apply (auto elim: linorder_neqE)

   724 done

   725

   726 lemma INT_greaterThan_UNIV: "(INT m::nat. greaterThan m) = {}"

   727 by blast

   728

   729 subsubsection \<open>The Constant @{term atLeast}\<close>

   730

   731 lemma atLeast_0 [simp]: "atLeast (0::nat) = UNIV"

   732 by (unfold atLeast_def UNIV_def, simp)

   733

   734 lemma atLeast_Suc: "atLeast (Suc k) = atLeast k - {k}"

   735 apply (simp add: atLeast_def)

   736 apply (simp add: Suc_le_eq)

   737 apply (simp add: order_le_less, blast)

   738 done

   739

   740 lemma atLeast_Suc_greaterThan: "atLeast (Suc k) = greaterThan k"

   741   by (auto simp add: greaterThan_def atLeast_def less_Suc_eq_le)

   742

   743 lemma UN_atLeast_UNIV: "(UN m::nat. atLeast m) = UNIV"

   744 by blast

   745

   746 subsubsection \<open>The Constant @{term atMost}\<close>

   747

   748 lemma atMost_0 [simp]: "atMost (0::nat) = {0}"

   749 by (simp add: atMost_def)

   750

   751 lemma atMost_Suc: "atMost (Suc k) = insert (Suc k) (atMost k)"

   752 apply (simp add: atMost_def)

   753 apply (simp add: less_Suc_eq order_le_less, blast)

   754 done

   755

   756 lemma UN_atMost_UNIV: "(UN m::nat. atMost m) = UNIV"

   757 by blast

   758

   759 subsubsection \<open>The Constant @{term atLeastLessThan}\<close>

   760

   761 text\<open>The orientation of the following 2 rules is tricky. The lhs is

   762 defined in terms of the rhs.  Hence the chosen orientation makes sense

   763 in this theory --- the reverse orientation complicates proofs (eg

   764 nontermination). But outside, when the definition of the lhs is rarely

   765 used, the opposite orientation seems preferable because it reduces a

   766 specific concept to a more general one.\<close>

   767

   768 lemma atLeast0LessThan [code_abbrev]: "{0::nat..<n} = {..<n}"

   769 by(simp add:lessThan_def atLeastLessThan_def)

   770

   771 lemma atLeast0AtMost [code_abbrev]: "{0..n::nat} = {..n}"

   772 by(simp add:atMost_def atLeastAtMost_def)

   773

   774 lemma lessThan_atLeast0:

   775   "{..<n} = {0::nat..<n}"

   776   by (simp add: atLeast0LessThan)

   777

   778 lemma atMost_atLeast0:

   779   "{..n} = {0::nat..n}"

   780   by (simp add: atLeast0AtMost)

   781

   782 lemma atLeastLessThan0: "{m..<0::nat} = {}"

   783 by (simp add: atLeastLessThan_def)

   784

   785 lemma atLeast0_lessThan_Suc:

   786   "{0..<Suc n} = insert n {0..<n}"

   787   by (simp add: atLeast0LessThan lessThan_Suc)

   788

   789 lemma atLeast0_lessThan_Suc_eq_insert_0:

   790   "{0..<Suc n} = insert 0 (Suc  {0..<n})"

   791   by (simp add: atLeast0LessThan lessThan_Suc_eq_insert_0)

   792

   793

   794 subsubsection \<open>The Constant @{term atLeastAtMost}\<close>

   795

   796 lemma atLeast0_atMost_Suc:

   797   "{0..Suc n} = insert (Suc n) {0..n}"

   798   by (simp add: atLeast0AtMost atMost_Suc)

   799

   800 lemma atLeast0_atMost_Suc_eq_insert_0:

   801   "{0..Suc n} = insert 0 (Suc  {0..n})"

   802   by (simp add: atLeast0AtMost Iic_Suc_eq_insert_0)

   803

   804

   805 subsubsection \<open>Intervals of nats with @{term Suc}\<close>

   806

   807 text\<open>Not a simprule because the RHS is too messy.\<close>

   808 lemma atLeastLessThanSuc:

   809     "{m..<Suc n} = (if m \<le> n then insert n {m..<n} else {})"

   810 by (auto simp add: atLeastLessThan_def)

   811

   812 lemma atLeastLessThan_singleton [simp]: "{m..<Suc m} = {m}"

   813 by (auto simp add: atLeastLessThan_def)

   814 (*

   815 lemma atLeast_sum_LessThan [simp]: "{m + k..<k::nat} = {}"

   816 by (induct k, simp_all add: atLeastLessThanSuc)

   817

   818 lemma atLeastSucLessThan [simp]: "{Suc n..<n} = {}"

   819 by (auto simp add: atLeastLessThan_def)

   820 *)

   821 lemma atLeastLessThanSuc_atLeastAtMost: "{l..<Suc u} = {l..u}"

   822   by (simp add: lessThan_Suc_atMost atLeastAtMost_def atLeastLessThan_def)

   823

   824 lemma atLeastSucAtMost_greaterThanAtMost: "{Suc l..u} = {l<..u}"

   825   by (simp add: atLeast_Suc_greaterThan atLeastAtMost_def

   826     greaterThanAtMost_def)

   827

   828 lemma atLeastSucLessThan_greaterThanLessThan: "{Suc l..<u} = {l<..<u}"

   829   by (simp add: atLeast_Suc_greaterThan atLeastLessThan_def

   830     greaterThanLessThan_def)

   831

   832 lemma atLeastAtMostSuc_conv: "m \<le> Suc n \<Longrightarrow> {m..Suc n} = insert (Suc n) {m..n}"

   833 by (auto simp add: atLeastAtMost_def)

   834

   835 lemma atLeastAtMost_insertL: "m \<le> n \<Longrightarrow> insert m {Suc m..n} = {m ..n}"

   836 by auto

   837

   838 text \<open>The analogous result is useful on @{typ int}:\<close>

   839 (* here, because we don't have an own int section *)

   840 lemma atLeastAtMostPlus1_int_conv:

   841   "m <= 1+n \<Longrightarrow> {m..1+n} = insert (1+n) {m..n::int}"

   842   by (auto intro: set_eqI)

   843

   844 lemma atLeastLessThan_add_Un: "i \<le> j \<Longrightarrow> {i..<j+k} = {i..<j} \<union> {j..<j+k::nat}"

   845   apply (induct k)

   846   apply (simp_all add: atLeastLessThanSuc)

   847   done

   848

   849

   850 subsubsection \<open>Intervals and numerals\<close>

   851

   852 lemma lessThan_nat_numeral:  \<comment>\<open>Evaluation for specific numerals\<close>

   853   "lessThan (numeral k :: nat) = insert (pred_numeral k) (lessThan (pred_numeral k))"

   854   by (simp add: numeral_eq_Suc lessThan_Suc)

   855

   856 lemma atMost_nat_numeral:  \<comment>\<open>Evaluation for specific numerals\<close>

   857   "atMost (numeral k :: nat) = insert (numeral k) (atMost (pred_numeral k))"

   858   by (simp add: numeral_eq_Suc atMost_Suc)

   859

   860 lemma atLeastLessThan_nat_numeral:  \<comment>\<open>Evaluation for specific numerals\<close>

   861   "atLeastLessThan m (numeral k :: nat) =

   862      (if m \<le> (pred_numeral k) then insert (pred_numeral k) (atLeastLessThan m (pred_numeral k))

   863                  else {})"

   864   by (simp add: numeral_eq_Suc atLeastLessThanSuc)

   865

   866

   867 subsubsection \<open>Image\<close>

   868

   869 context linordered_semidom

   870 begin

   871

   872 lemma image_add_atLeast_atMost [simp]:

   873   "plus k  {i..j} = {i + k..j + k}" (is "?A = ?B")

   874 proof

   875   show "?A \<subseteq> ?B"

   876     by (auto simp add: ac_simps)

   877 next

   878   show "?B \<subseteq> ?A"

   879   proof

   880     fix n

   881     assume "n \<in> ?B"

   882     then have "i \<le> n - k"

   883       by (simp add: add_le_imp_le_diff)

   884     have "n = n - k + k"

   885     proof -

   886       from \<open>n \<in> ?B\<close> have "n = n - (i + k) + (i + k)"

   887         by simp

   888       also have "\<dots> = n - k - i + i + k"

   889         by (simp add: algebra_simps)

   890       also have "\<dots> = n - k + k"

   891         using \<open>i \<le> n - k\<close> by simp

   892       finally show ?thesis .

   893     qed

   894     moreover have "n - k \<in> {i..j}"

   895       using \<open>n \<in> ?B\<close>

   896       by (auto simp: add_le_imp_le_diff add_le_add_imp_diff_le)

   897     ultimately show "n \<in> ?A"

   898       by (simp add: ac_simps)

   899   qed

   900 qed

   901

   902 lemma image_add_atLeast_atMost' [simp]:

   903   "(\<lambda>n. n + k)  {i..j} = {i + k..j + k}"

   904   by (simp add: add.commute [of _ k])

   905

   906 lemma image_add_atLeast_lessThan [simp]:

   907   "plus k  {i..<j} = {i + k..<j + k}"

   908   by (simp add: image_set_diff atLeast_lessThan_eq_atLeast_atMost_diff ac_simps)

   909

   910 lemma image_add_atLeast_lessThan' [simp]:

   911   "(\<lambda>n. n + k)  {i..<j} = {i + k..<j + k}"

   912   by (simp add: add.commute [of _ k])

   913

   914 end

   915

   916 lemma image_Suc_atLeast_atMost [simp]:

   917   "Suc  {i..j} = {Suc i..Suc j}"

   918   using image_add_atLeast_atMost [of 1 i j]

   919     by (simp only: plus_1_eq_Suc) simp

   920

   921 lemma image_Suc_atLeast_lessThan [simp]:

   922   "Suc  {i..<j} = {Suc i..<Suc j}"

   923   using image_add_atLeast_lessThan [of 1 i j]

   924     by (simp only: plus_1_eq_Suc) simp

   925

   926 corollary image_Suc_atMost:

   927   "Suc  {..n} = {1..Suc n}"

   928   by (simp add: atMost_atLeast0 atLeastLessThanSuc_atLeastAtMost)

   929

   930 corollary image_Suc_lessThan:

   931   "Suc  {..<n} = {1..n}"

   932   by (simp add: lessThan_atLeast0 atLeastLessThanSuc_atLeastAtMost)

   933

   934 lemma image_diff_atLeastAtMost [simp]:

   935   fixes d::"'a::linordered_idom" shows "(op - d  {a..b}) = {d-b..d-a}"

   936   apply auto

   937   apply (rule_tac x="d-x" in rev_image_eqI, auto)

   938   done

   939

   940 lemma image_mult_atLeastAtMost [simp]:

   941   fixes d::"'a::linordered_field"

   942   assumes "d>0" shows "(op * d  {a..b}) = {d*a..d*b}"

   943   using assms

   944   by (auto simp: field_simps mult_le_cancel_right intro: rev_image_eqI [where x="x/d" for x])

   945

   946 lemma image_affinity_atLeastAtMost:

   947   fixes c :: "'a::linordered_field"

   948   shows "((\<lambda>x. m*x + c)  {a..b}) = (if {a..b}={} then {}

   949             else if 0 \<le> m then {m*a + c .. m *b + c}

   950             else {m*b + c .. m*a + c})"

   951   apply (case_tac "m=0", auto simp: mult_le_cancel_left)

   952   apply (rule_tac x="inverse m*(x-c)" in rev_image_eqI, auto simp: field_simps)

   953   apply (rule_tac x="inverse m*(x-c)" in rev_image_eqI, auto simp: field_simps)

   954   done

   955

   956 lemma image_affinity_atLeastAtMost_diff:

   957   fixes c :: "'a::linordered_field"

   958   shows "((\<lambda>x. m*x - c)  {a..b}) = (if {a..b}={} then {}

   959             else if 0 \<le> m then {m*a - c .. m*b - c}

   960             else {m*b - c .. m*a - c})"

   961   using image_affinity_atLeastAtMost [of m "-c" a b]

   962   by simp

   963

   964 lemma image_affinity_atLeastAtMost_div:

   965   fixes c :: "'a::linordered_field"

   966   shows "((\<lambda>x. x/m + c)  {a..b}) = (if {a..b}={} then {}

   967             else if 0 \<le> m then {a/m + c .. b/m + c}

   968             else {b/m + c .. a/m + c})"

   969   using image_affinity_atLeastAtMost [of "inverse m" c a b]

   970   by (simp add: field_class.field_divide_inverse algebra_simps)

   971

   972 lemma image_affinity_atLeastAtMost_div_diff:

   973   fixes c :: "'a::linordered_field"

   974   shows "((\<lambda>x. x/m - c)  {a..b}) = (if {a..b}={} then {}

   975             else if 0 \<le> m then {a/m - c .. b/m - c}

   976             else {b/m - c .. a/m - c})"

   977   using image_affinity_atLeastAtMost_diff [of "inverse m" c a b]

   978   by (simp add: field_class.field_divide_inverse algebra_simps)

   979

   980 lemma atLeast1_lessThan_eq_remove0:

   981   "{Suc 0..<n} = {..<n} - {0}"

   982   by auto

   983

   984 lemma atLeast1_atMost_eq_remove0:

   985   "{Suc 0..n} = {..n} - {0}"

   986   by auto

   987

   988 lemma image_add_int_atLeastLessThan:

   989     "(%x. x + (l::int))  {0..<u-l} = {l..<u}"

   990   apply (auto simp add: image_def)

   991   apply (rule_tac x = "x - l" in bexI)

   992   apply auto

   993   done

   994

   995 lemma image_minus_const_atLeastLessThan_nat:

   996   fixes c :: nat

   997   shows "(\<lambda>i. i - c)  {x ..< y} =

   998       (if c < y then {x - c ..< y - c} else if x < y then {0} else {})"

   999     (is "_ = ?right")

  1000 proof safe

  1001   fix a assume a: "a \<in> ?right"

  1002   show "a \<in> (\<lambda>i. i - c)  {x ..< y}"

  1003   proof cases

  1004     assume "c < y" with a show ?thesis

  1005       by (auto intro!: image_eqI[of _ _ "a + c"])

  1006   next

  1007     assume "\<not> c < y" with a show ?thesis

  1008       by (auto intro!: image_eqI[of _ _ x] split: if_split_asm)

  1009   qed

  1010 qed auto

  1011

  1012 lemma image_int_atLeast_lessThan:

  1013   "int  {a..<b} = {int a..<int b}"

  1014   by (auto intro!: image_eqI [where x = "nat x" for x])

  1015

  1016 lemma image_int_atLeast_atMost:

  1017   "int  {a..b} = {int a..int b}"

  1018   by (auto intro!: image_eqI [where x = "nat x" for x])

  1019

  1020 context ordered_ab_group_add

  1021 begin

  1022

  1023 lemma

  1024   fixes x :: 'a

  1025   shows image_uminus_greaterThan[simp]: "uminus  {x<..} = {..<-x}"

  1026   and image_uminus_atLeast[simp]: "uminus  {x..} = {..-x}"

  1027 proof safe

  1028   fix y assume "y < -x"

  1029   hence *:  "x < -y" using neg_less_iff_less[of "-y" x] by simp

  1030   have "- (-y) \<in> uminus  {x<..}"

  1031     by (rule imageI) (simp add: *)

  1032   thus "y \<in> uminus  {x<..}" by simp

  1033 next

  1034   fix y assume "y \<le> -x"

  1035   have "- (-y) \<in> uminus  {x..}"

  1036     by (rule imageI) (insert \<open>y \<le> -x\<close>[THEN le_imp_neg_le], simp)

  1037   thus "y \<in> uminus  {x..}" by simp

  1038 qed simp_all

  1039

  1040 lemma

  1041   fixes x :: 'a

  1042   shows image_uminus_lessThan[simp]: "uminus  {..<x} = {-x<..}"

  1043   and image_uminus_atMost[simp]: "uminus  {..x} = {-x..}"

  1044 proof -

  1045   have "uminus  {..<x} = uminus  uminus  {-x<..}"

  1046     and "uminus  {..x} = uminus  uminus  {-x..}" by simp_all

  1047   thus "uminus  {..<x} = {-x<..}" and "uminus  {..x} = {-x..}"

  1048     by (simp_all add: image_image

  1049         del: image_uminus_greaterThan image_uminus_atLeast)

  1050 qed

  1051

  1052 lemma

  1053   fixes x :: 'a

  1054   shows image_uminus_atLeastAtMost[simp]: "uminus  {x..y} = {-y..-x}"

  1055   and image_uminus_greaterThanAtMost[simp]: "uminus  {x<..y} = {-y..<-x}"

  1056   and image_uminus_atLeastLessThan[simp]: "uminus  {x..<y} = {-y<..-x}"

  1057   and image_uminus_greaterThanLessThan[simp]: "uminus  {x<..<y} = {-y<..<-x}"

  1058   by (simp_all add: atLeastAtMost_def greaterThanAtMost_def atLeastLessThan_def

  1059       greaterThanLessThan_def image_Int[OF inj_uminus] Int_commute)

  1060 end

  1061

  1062 subsubsection \<open>Finiteness\<close>

  1063

  1064 lemma finite_lessThan [iff]: fixes k :: nat shows "finite {..<k}"

  1065   by (induct k) (simp_all add: lessThan_Suc)

  1066

  1067 lemma finite_atMost [iff]: fixes k :: nat shows "finite {..k}"

  1068   by (induct k) (simp_all add: atMost_Suc)

  1069

  1070 lemma finite_greaterThanLessThan [iff]:

  1071   fixes l :: nat shows "finite {l<..<u}"

  1072 by (simp add: greaterThanLessThan_def)

  1073

  1074 lemma finite_atLeastLessThan [iff]:

  1075   fixes l :: nat shows "finite {l..<u}"

  1076 by (simp add: atLeastLessThan_def)

  1077

  1078 lemma finite_greaterThanAtMost [iff]:

  1079   fixes l :: nat shows "finite {l<..u}"

  1080 by (simp add: greaterThanAtMost_def)

  1081

  1082 lemma finite_atLeastAtMost [iff]:

  1083   fixes l :: nat shows "finite {l..u}"

  1084 by (simp add: atLeastAtMost_def)

  1085

  1086 text \<open>A bounded set of natural numbers is finite.\<close>

  1087 lemma bounded_nat_set_is_finite:

  1088   "(ALL i:N. i < (n::nat)) ==> finite N"

  1089 apply (rule finite_subset)

  1090  apply (rule_tac [2] finite_lessThan, auto)

  1091 done

  1092

  1093 text \<open>A set of natural numbers is finite iff it is bounded.\<close>

  1094 lemma finite_nat_set_iff_bounded:

  1095   "finite(N::nat set) = (EX m. ALL n:N. n<m)" (is "?F = ?B")

  1096 proof

  1097   assume f:?F  show ?B

  1098     using Max_ge[OF \<open>?F\<close>, simplified less_Suc_eq_le[symmetric]] by blast

  1099 next

  1100   assume ?B show ?F using \<open>?B\<close> by(blast intro:bounded_nat_set_is_finite)

  1101 qed

  1102

  1103 lemma finite_nat_set_iff_bounded_le:

  1104   "finite(N::nat set) = (EX m. ALL n:N. n<=m)"

  1105 apply(simp add:finite_nat_set_iff_bounded)

  1106 apply(blast dest:less_imp_le_nat le_imp_less_Suc)

  1107 done

  1108

  1109 lemma finite_less_ub:

  1110      "!!f::nat=>nat. (!!n. n \<le> f n) ==> finite {n. f n \<le> u}"

  1111 by (rule_tac B="{..u}" in finite_subset, auto intro: order_trans)

  1112

  1113 lemma bounded_Max_nat:

  1114   fixes P :: "nat \<Rightarrow> bool"

  1115   assumes x: "P x" and M: "\<And>x. P x \<Longrightarrow> x \<le> M"

  1116   obtains m where "P m" "\<And>x. P x \<Longrightarrow> x \<le> m"

  1117 proof -

  1118   have "finite {x. P x}"

  1119     using M finite_nat_set_iff_bounded_le by auto

  1120   then have "Max {x. P x} \<in> {x. P x}"

  1121     using Max_in x by auto

  1122   then show ?thesis

  1123     by (simp add: \<open>finite {x. P x}\<close> that)

  1124 qed

  1125

  1126

  1127 text\<open>Any subset of an interval of natural numbers the size of the

  1128 subset is exactly that interval.\<close>

  1129

  1130 lemma subset_card_intvl_is_intvl:

  1131   assumes "A \<subseteq> {k..<k + card A}"

  1132   shows "A = {k..<k + card A}"

  1133 proof (cases "finite A")

  1134   case True

  1135   from this and assms show ?thesis

  1136   proof (induct A rule: finite_linorder_max_induct)

  1137     case empty thus ?case by auto

  1138   next

  1139     case (insert b A)

  1140     hence *: "b \<notin> A" by auto

  1141     with insert have "A <= {k..<k + card A}" and "b = k + card A"

  1142       by fastforce+

  1143     with insert * show ?case by auto

  1144   qed

  1145 next

  1146   case False

  1147   with assms show ?thesis by simp

  1148 qed

  1149

  1150

  1151 subsubsection \<open>Proving Inclusions and Equalities between Unions\<close>

  1152

  1153 lemma UN_le_eq_Un0:

  1154   "(\<Union>i\<le>n::nat. M i) = (\<Union>i\<in>{1..n}. M i) \<union> M 0" (is "?A = ?B")

  1155 proof

  1156   show "?A <= ?B"

  1157   proof

  1158     fix x assume "x : ?A"

  1159     then obtain i where i: "i\<le>n" "x : M i" by auto

  1160     show "x : ?B"

  1161     proof(cases i)

  1162       case 0 with i show ?thesis by simp

  1163     next

  1164       case (Suc j) with i show ?thesis by auto

  1165     qed

  1166   qed

  1167 next

  1168   show "?B <= ?A" by fastforce

  1169 qed

  1170

  1171 lemma UN_le_add_shift:

  1172   "(\<Union>i\<le>n::nat. M(i+k)) = (\<Union>i\<in>{k..n+k}. M i)" (is "?A = ?B")

  1173 proof

  1174   show "?A <= ?B" by fastforce

  1175 next

  1176   show "?B <= ?A"

  1177   proof

  1178     fix x assume "x : ?B"

  1179     then obtain i where i: "i : {k..n+k}" "x : M(i)" by auto

  1180     hence "i-k\<le>n & x : M((i-k)+k)" by auto

  1181     thus "x : ?A" by blast

  1182   qed

  1183 qed

  1184

  1185 lemma UN_UN_finite_eq: "(\<Union>n::nat. \<Union>i\<in>{0..<n}. A i) = (\<Union>n. A n)"

  1186   by (auto simp add: atLeast0LessThan)

  1187

  1188 lemma UN_finite_subset:

  1189   "(\<And>n::nat. (\<Union>i\<in>{0..<n}. A i) \<subseteq> C) \<Longrightarrow> (\<Union>n. A n) \<subseteq> C"

  1190   by (subst UN_UN_finite_eq [symmetric]) blast

  1191

  1192 lemma UN_finite2_subset:

  1193   assumes "\<And>n::nat. (\<Union>i\<in>{0..<n}. A i) \<subseteq> (\<Union>i\<in>{0..<n + k}. B i)"

  1194   shows "(\<Union>n. A n) \<subseteq> (\<Union>n. B n)"

  1195 proof (rule UN_finite_subset, rule)

  1196   fix n and a

  1197   from assms have "(\<Union>i\<in>{0..<n}. A i) \<subseteq> (\<Union>i\<in>{0..<n + k}. B i)" .

  1198   moreover assume "a \<in> (\<Union>i\<in>{0..<n}. A i)"

  1199   ultimately have "a \<in> (\<Union>i\<in>{0..<n + k}. B i)" by blast

  1200   then show "a \<in> (\<Union>i. B i)" by (auto simp add: UN_UN_finite_eq)

  1201 qed

  1202

  1203 lemma UN_finite2_eq:

  1204   "(\<And>n::nat. (\<Union>i\<in>{0..<n}. A i) = (\<Union>i\<in>{0..<n + k}. B i)) \<Longrightarrow>

  1205     (\<Union>n. A n) = (\<Union>n. B n)"

  1206   apply (rule subset_antisym)

  1207    apply (rule UN_finite2_subset, blast)

  1208   apply (rule UN_finite2_subset [where k=k])

  1209   apply (force simp add: atLeastLessThan_add_Un [of 0])

  1210   done

  1211

  1212

  1213 subsubsection \<open>Cardinality\<close>

  1214

  1215 lemma card_lessThan [simp]: "card {..<u} = u"

  1216   by (induct u, simp_all add: lessThan_Suc)

  1217

  1218 lemma card_atMost [simp]: "card {..u} = Suc u"

  1219   by (simp add: lessThan_Suc_atMost [THEN sym])

  1220

  1221 lemma card_atLeastLessThan [simp]: "card {l..<u} = u - l"

  1222 proof -

  1223   have "{l..<u} = (%x. x + l)  {..<u-l}"

  1224     apply (auto simp add: image_def atLeastLessThan_def lessThan_def)

  1225     apply (rule_tac x = "x - l" in exI)

  1226     apply arith

  1227     done

  1228   then have "card {l..<u} = card {..<u-l}"

  1229     by (simp add: card_image inj_on_def)

  1230   then show ?thesis

  1231     by simp

  1232 qed

  1233

  1234 lemma card_atLeastAtMost [simp]: "card {l..u} = Suc u - l"

  1235   by (subst atLeastLessThanSuc_atLeastAtMost [THEN sym], simp)

  1236

  1237 lemma card_greaterThanAtMost [simp]: "card {l<..u} = u - l"

  1238   by (subst atLeastSucAtMost_greaterThanAtMost [THEN sym], simp)

  1239

  1240 lemma card_greaterThanLessThan [simp]: "card {l<..<u} = u - Suc l"

  1241   by (subst atLeastSucLessThan_greaterThanLessThan [THEN sym], simp)

  1242

  1243 lemma subset_eq_atLeast0_lessThan_finite:

  1244   fixes n :: nat

  1245   assumes "N \<subseteq> {0..<n}"

  1246   shows "finite N"

  1247   using assms finite_atLeastLessThan by (rule finite_subset)

  1248

  1249 lemma subset_eq_atLeast0_atMost_finite:

  1250   fixes n :: nat

  1251   assumes "N \<subseteq> {0..n}"

  1252   shows "finite N"

  1253   using assms finite_atLeastAtMost by (rule finite_subset)

  1254

  1255 lemma ex_bij_betw_nat_finite:

  1256   "finite M \<Longrightarrow> \<exists>h. bij_betw h {0..<card M} M"

  1257 apply(drule finite_imp_nat_seg_image_inj_on)

  1258 apply(auto simp:atLeast0LessThan[symmetric] lessThan_def[symmetric] card_image bij_betw_def)

  1259 done

  1260

  1261 lemma ex_bij_betw_finite_nat:

  1262   "finite M \<Longrightarrow> \<exists>h. bij_betw h M {0..<card M}"

  1263 by (blast dest: ex_bij_betw_nat_finite bij_betw_inv)

  1264

  1265 lemma finite_same_card_bij:

  1266   "finite A \<Longrightarrow> finite B \<Longrightarrow> card A = card B \<Longrightarrow> EX h. bij_betw h A B"

  1267 apply(drule ex_bij_betw_finite_nat)

  1268 apply(drule ex_bij_betw_nat_finite)

  1269 apply(auto intro!:bij_betw_trans)

  1270 done

  1271

  1272 lemma ex_bij_betw_nat_finite_1:

  1273   "finite M \<Longrightarrow> \<exists>h. bij_betw h {1 .. card M} M"

  1274 by (rule finite_same_card_bij) auto

  1275

  1276 lemma bij_betw_iff_card:

  1277   assumes "finite A" "finite B"

  1278   shows "(\<exists>f. bij_betw f A B) \<longleftrightarrow> (card A = card B)"

  1279 proof

  1280   assume "card A = card B"

  1281   moreover obtain f where "bij_betw f A {0 ..< card A}"

  1282     using assms ex_bij_betw_finite_nat by blast

  1283   moreover obtain g where "bij_betw g {0 ..< card B} B"

  1284     using assms ex_bij_betw_nat_finite by blast

  1285   ultimately have "bij_betw (g o f) A B"

  1286     by (auto simp: bij_betw_trans)

  1287   thus "(\<exists>f. bij_betw f A B)" by blast

  1288 qed (auto simp: bij_betw_same_card)

  1289

  1290 lemma inj_on_iff_card_le:

  1291   assumes FIN: "finite A" and FIN': "finite B"

  1292   shows "(\<exists>f. inj_on f A \<and> f  A \<le> B) = (card A \<le> card B)"

  1293 proof (safe intro!: card_inj_on_le)

  1294   assume *: "card A \<le> card B"

  1295   obtain f where 1: "inj_on f A" and 2: "f  A = {0 ..< card A}"

  1296   using FIN ex_bij_betw_finite_nat unfolding bij_betw_def by force

  1297   moreover obtain g where "inj_on g {0 ..< card B}" and 3: "g  {0 ..< card B} = B"

  1298   using FIN' ex_bij_betw_nat_finite unfolding bij_betw_def by force

  1299   ultimately have "inj_on g (f  A)" using subset_inj_on[of g _ "f  A"] * by force

  1300   hence "inj_on (g o f) A" using 1 comp_inj_on by blast

  1301   moreover

  1302   {have "{0 ..< card A} \<le> {0 ..< card B}" using * by force

  1303    with 2 have "f  A  \<le> {0 ..< card B}" by blast

  1304    hence "(g o f)  A \<le> B" unfolding comp_def using 3 by force

  1305   }

  1306   ultimately show "(\<exists>f. inj_on f A \<and> f  A \<le> B)" by blast

  1307 qed (insert assms, auto)

  1308

  1309 lemma subset_eq_atLeast0_lessThan_card:

  1310   fixes n :: nat

  1311   assumes "N \<subseteq> {0..<n}"

  1312   shows "card N \<le> n"

  1313 proof -

  1314   from assms finite_lessThan have "card N \<le> card {0..<n}"

  1315     using card_mono by blast

  1316   then show ?thesis by simp

  1317 qed

  1318

  1319

  1320 subsection \<open>Intervals of integers\<close>

  1321

  1322 lemma atLeastLessThanPlusOne_atLeastAtMost_int: "{l..<u+1} = {l..(u::int)}"

  1323   by (auto simp add: atLeastAtMost_def atLeastLessThan_def)

  1324

  1325 lemma atLeastPlusOneAtMost_greaterThanAtMost_int: "{l+1..u} = {l<..(u::int)}"

  1326   by (auto simp add: atLeastAtMost_def greaterThanAtMost_def)

  1327

  1328 lemma atLeastPlusOneLessThan_greaterThanLessThan_int:

  1329     "{l+1..<u} = {l<..<u::int}"

  1330   by (auto simp add: atLeastLessThan_def greaterThanLessThan_def)

  1331

  1332 subsubsection \<open>Finiteness\<close>

  1333

  1334 lemma image_atLeastZeroLessThan_int: "0 \<le> u ==>

  1335     {(0::int)..<u} = int  {..<nat u}"

  1336   apply (unfold image_def lessThan_def)

  1337   apply auto

  1338   apply (rule_tac x = "nat x" in exI)

  1339   apply (auto simp add: zless_nat_eq_int_zless [THEN sym])

  1340   done

  1341

  1342 lemma finite_atLeastZeroLessThan_int: "finite {(0::int)..<u}"

  1343   apply (cases "0 \<le> u")

  1344   apply (subst image_atLeastZeroLessThan_int, assumption)

  1345   apply (rule finite_imageI)

  1346   apply auto

  1347   done

  1348

  1349 lemma finite_atLeastLessThan_int [iff]: "finite {l..<u::int}"

  1350   apply (subgoal_tac "(%x. x + l)  {0..<u-l} = {l..<u}")

  1351   apply (erule subst)

  1352   apply (rule finite_imageI)

  1353   apply (rule finite_atLeastZeroLessThan_int)

  1354   apply (rule image_add_int_atLeastLessThan)

  1355   done

  1356

  1357 lemma finite_atLeastAtMost_int [iff]: "finite {l..(u::int)}"

  1358   by (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym], simp)

  1359

  1360 lemma finite_greaterThanAtMost_int [iff]: "finite {l<..(u::int)}"

  1361   by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)

  1362

  1363 lemma finite_greaterThanLessThan_int [iff]: "finite {l<..<u::int}"

  1364   by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)

  1365

  1366

  1367 subsubsection \<open>Cardinality\<close>

  1368

  1369 lemma card_atLeastZeroLessThan_int: "card {(0::int)..<u} = nat u"

  1370   apply (cases "0 \<le> u")

  1371   apply (subst image_atLeastZeroLessThan_int, assumption)

  1372   apply (subst card_image)

  1373   apply (auto simp add: inj_on_def)

  1374   done

  1375

  1376 lemma card_atLeastLessThan_int [simp]: "card {l..<u} = nat (u - l)"

  1377   apply (subgoal_tac "card {l..<u} = card {0..<u-l}")

  1378   apply (erule ssubst, rule card_atLeastZeroLessThan_int)

  1379   apply (subgoal_tac "(%x. x + l)  {0..<u-l} = {l..<u}")

  1380   apply (erule subst)

  1381   apply (rule card_image)

  1382   apply (simp add: inj_on_def)

  1383   apply (rule image_add_int_atLeastLessThan)

  1384   done

  1385

  1386 lemma card_atLeastAtMost_int [simp]: "card {l..u} = nat (u - l + 1)"

  1387 apply (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym])

  1388 apply (auto simp add: algebra_simps)

  1389 done

  1390

  1391 lemma card_greaterThanAtMost_int [simp]: "card {l<..u} = nat (u - l)"

  1392 by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)

  1393

  1394 lemma card_greaterThanLessThan_int [simp]: "card {l<..<u} = nat (u - (l + 1))"

  1395 by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)

  1396

  1397 lemma finite_M_bounded_by_nat: "finite {k. P k \<and> k < (i::nat)}"

  1398 proof -

  1399   have "{k. P k \<and> k < i} \<subseteq> {..<i}" by auto

  1400   with finite_lessThan[of "i"] show ?thesis by (simp add: finite_subset)

  1401 qed

  1402

  1403 lemma card_less:

  1404 assumes zero_in_M: "0 \<in> M"

  1405 shows "card {k \<in> M. k < Suc i} \<noteq> 0"

  1406 proof -

  1407   from zero_in_M have "{k \<in> M. k < Suc i} \<noteq> {}" by auto

  1408   with finite_M_bounded_by_nat show ?thesis by (auto simp add: card_eq_0_iff)

  1409 qed

  1410

  1411 lemma card_less_Suc2: "0 \<notin> M \<Longrightarrow> card {k. Suc k \<in> M \<and> k < i} = card {k \<in> M. k < Suc i}"

  1412 apply (rule card_bij_eq [of Suc _ _ "\<lambda>x. x - Suc 0"])

  1413 apply auto

  1414 apply (rule inj_on_diff_nat)

  1415 apply auto

  1416 apply (case_tac x)

  1417 apply auto

  1418 apply (case_tac xa)

  1419 apply auto

  1420 apply (case_tac xa)

  1421 apply auto

  1422 done

  1423

  1424 lemma card_less_Suc:

  1425   assumes zero_in_M: "0 \<in> M"

  1426     shows "Suc (card {k. Suc k \<in> M \<and> k < i}) = card {k \<in> M. k < Suc i}"

  1427 proof -

  1428   from assms have a: "0 \<in> {k \<in> M. k < Suc i}" by simp

  1429   hence c: "{k \<in> M. k < Suc i} = insert 0 ({k \<in> M. k < Suc i} - {0})"

  1430     by (auto simp only: insert_Diff)

  1431   have b: "{k \<in> M. k < Suc i} - {0} = {k \<in> M - {0}. k < Suc i}"  by auto

  1432   from finite_M_bounded_by_nat[of "\<lambda>x. x \<in> M" "Suc i"]

  1433   have "Suc (card {k. Suc k \<in> M \<and> k < i}) = card (insert 0 ({k \<in> M. k < Suc i} - {0}))"

  1434     apply (subst card_insert)

  1435     apply simp_all

  1436     apply (subst b)

  1437     apply (subst card_less_Suc2[symmetric])

  1438     apply simp_all

  1439     done

  1440   with c show ?thesis by simp

  1441 qed

  1442

  1443

  1444 subsection \<open>Lemmas useful with the summation operator sum\<close>

  1445

  1446 text \<open>For examples, see Algebra/poly/UnivPoly2.thy\<close>

  1447

  1448 subsubsection \<open>Disjoint Unions\<close>

  1449

  1450 text \<open>Singletons and open intervals\<close>

  1451

  1452 lemma ivl_disj_un_singleton:

  1453   "{l::'a::linorder} Un {l<..} = {l..}"

  1454   "{..<u} Un {u::'a::linorder} = {..u}"

  1455   "(l::'a::linorder) < u ==> {l} Un {l<..<u} = {l..<u}"

  1456   "(l::'a::linorder) < u ==> {l<..<u} Un {u} = {l<..u}"

  1457   "(l::'a::linorder) <= u ==> {l} Un {l<..u} = {l..u}"

  1458   "(l::'a::linorder) <= u ==> {l..<u} Un {u} = {l..u}"

  1459 by auto

  1460

  1461 text \<open>One- and two-sided intervals\<close>

  1462

  1463 lemma ivl_disj_un_one:

  1464   "(l::'a::linorder) < u ==> {..l} Un {l<..<u} = {..<u}"

  1465   "(l::'a::linorder) <= u ==> {..<l} Un {l..<u} = {..<u}"

  1466   "(l::'a::linorder) <= u ==> {..l} Un {l<..u} = {..u}"

  1467   "(l::'a::linorder) <= u ==> {..<l} Un {l..u} = {..u}"

  1468   "(l::'a::linorder) <= u ==> {l<..u} Un {u<..} = {l<..}"

  1469   "(l::'a::linorder) < u ==> {l<..<u} Un {u..} = {l<..}"

  1470   "(l::'a::linorder) <= u ==> {l..u} Un {u<..} = {l..}"

  1471   "(l::'a::linorder) <= u ==> {l..<u} Un {u..} = {l..}"

  1472 by auto

  1473

  1474 text \<open>Two- and two-sided intervals\<close>

  1475

  1476 lemma ivl_disj_un_two:

  1477   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..<u} = {l<..<u}"

  1478   "[| (l::'a::linorder) <= m; m < u |] ==> {l<..m} Un {m<..<u} = {l<..<u}"

  1479   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..<u} = {l..<u}"

  1480   "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m<..<u} = {l..<u}"

  1481   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..u} = {l<..u}"

  1482   "[| (l::'a::linorder) <= m; m <= u |] ==> {l<..m} Un {m<..u} = {l<..u}"

  1483   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..u} = {l..u}"

  1484   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m<..u} = {l..u}"

  1485 by auto

  1486

  1487 lemma ivl_disj_un_two_touch:

  1488   "[| (l::'a::linorder) < m; m < u |] ==> {l<..m} Un {m..<u} = {l<..<u}"

  1489   "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m..<u} = {l..<u}"

  1490   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..m} Un {m..u} = {l<..u}"

  1491   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m..u} = {l..u}"

  1492 by auto

  1493

  1494 lemmas ivl_disj_un = ivl_disj_un_singleton ivl_disj_un_one ivl_disj_un_two ivl_disj_un_two_touch

  1495

  1496 subsubsection \<open>Disjoint Intersections\<close>

  1497

  1498 text \<open>One- and two-sided intervals\<close>

  1499

  1500 lemma ivl_disj_int_one:

  1501   "{..l::'a::order} Int {l<..<u} = {}"

  1502   "{..<l} Int {l..<u} = {}"

  1503   "{..l} Int {l<..u} = {}"

  1504   "{..<l} Int {l..u} = {}"

  1505   "{l<..u} Int {u<..} = {}"

  1506   "{l<..<u} Int {u..} = {}"

  1507   "{l..u} Int {u<..} = {}"

  1508   "{l..<u} Int {u..} = {}"

  1509   by auto

  1510

  1511 text \<open>Two- and two-sided intervals\<close>

  1512

  1513 lemma ivl_disj_int_two:

  1514   "{l::'a::order<..<m} Int {m..<u} = {}"

  1515   "{l<..m} Int {m<..<u} = {}"

  1516   "{l..<m} Int {m..<u} = {}"

  1517   "{l..m} Int {m<..<u} = {}"

  1518   "{l<..<m} Int {m..u} = {}"

  1519   "{l<..m} Int {m<..u} = {}"

  1520   "{l..<m} Int {m..u} = {}"

  1521   "{l..m} Int {m<..u} = {}"

  1522   by auto

  1523

  1524 lemmas ivl_disj_int = ivl_disj_int_one ivl_disj_int_two

  1525

  1526 subsubsection \<open>Some Differences\<close>

  1527

  1528 lemma ivl_diff[simp]:

  1529  "i \<le> n \<Longrightarrow> {i..<m} - {i..<n} = {n..<(m::'a::linorder)}"

  1530 by(auto)

  1531

  1532 lemma (in linorder) lessThan_minus_lessThan [simp]:

  1533   "{..< n} - {..< m} = {m ..< n}"

  1534   by auto

  1535

  1536 lemma (in linorder) atLeastAtMost_diff_ends:

  1537   "{a..b} - {a, b} = {a<..<b}"

  1538   by auto

  1539

  1540

  1541 subsubsection \<open>Some Subset Conditions\<close>

  1542

  1543 lemma ivl_subset [simp]:

  1544  "({i..<j} \<subseteq> {m..<n}) = (j \<le> i | m \<le> i & j \<le> (n::'a::linorder))"

  1545 apply(auto simp:linorder_not_le)

  1546 apply(rule ccontr)

  1547 apply(insert linorder_le_less_linear[of i n])

  1548 apply(clarsimp simp:linorder_not_le)

  1549 apply(fastforce)

  1550 done

  1551

  1552

  1553 subsection \<open>Generic big monoid operation over intervals\<close>

  1554

  1555 context semiring_char_0

  1556 begin

  1557

  1558 lemma inj_on_of_nat [simp]:

  1559   "inj_on of_nat N"

  1560   by rule simp

  1561

  1562 lemma bij_betw_of_nat [simp]:

  1563   "bij_betw of_nat N A \<longleftrightarrow> of_nat  N = A"

  1564   by (simp add: bij_betw_def)

  1565

  1566 end

  1567

  1568 context comm_monoid_set

  1569 begin

  1570

  1571 lemma atLeast_lessThan_reindex:

  1572   "F g {h m..<h n} = F (g \<circ> h) {m..<n}"

  1573   if "bij_betw h {m..<n} {h m..<h n}" for m n ::nat

  1574 proof -

  1575   from that have "inj_on h {m..<n}" and "h  {m..<n} = {h m..<h n}"

  1576     by (simp_all add: bij_betw_def)

  1577   then show ?thesis

  1578     using reindex [of h "{m..<n}" g] by simp

  1579 qed

  1580

  1581 lemma atLeast_atMost_reindex:

  1582   "F g {h m..h n} = F (g \<circ> h) {m..n}"

  1583   if "bij_betw h {m..n} {h m..h n}" for m n ::nat

  1584 proof -

  1585   from that have "inj_on h {m..n}" and "h  {m..n} = {h m..h n}"

  1586     by (simp_all add: bij_betw_def)

  1587   then show ?thesis

  1588     using reindex [of h "{m..n}" g] by simp

  1589 qed

  1590

  1591 lemma atLeast_lessThan_shift_bounds:

  1592   "F g {m + k..<n + k} = F (g \<circ> plus k) {m..<n}"

  1593   for m n k :: nat

  1594   using atLeast_lessThan_reindex [of "plus k" m n g]

  1595   by (simp add: ac_simps)

  1596

  1597 lemma atLeast_atMost_shift_bounds:

  1598   "F g {m + k..n + k} = F (g \<circ> plus k) {m..n}"

  1599   for m n k :: nat

  1600   using atLeast_atMost_reindex [of "plus k" m n g]

  1601   by (simp add: ac_simps)

  1602

  1603 lemma atLeast_Suc_lessThan_Suc_shift:

  1604   "F g {Suc m..<Suc n} = F (g \<circ> Suc) {m..<n}"

  1605   using atLeast_lessThan_shift_bounds [of _ _ 1]

  1606   by (simp add: plus_1_eq_Suc)

  1607

  1608 lemma atLeast_Suc_atMost_Suc_shift:

  1609   "F g {Suc m..Suc n} = F (g \<circ> Suc) {m..n}"

  1610   using atLeast_atMost_shift_bounds [of _ _ 1]

  1611   by (simp add: plus_1_eq_Suc)

  1612

  1613 lemma atLeast_int_lessThan_int_shift:

  1614   "F g {int m..<int n} = F (g \<circ> int) {m..<n}"

  1615   by (rule atLeast_lessThan_reindex)

  1616     (simp add: image_int_atLeast_lessThan)

  1617

  1618 lemma atLeast_int_atMost_int_shift:

  1619   "F g {int m..int n} = F (g \<circ> int) {m..n}"

  1620   by (rule atLeast_atMost_reindex)

  1621     (simp add: image_int_atLeast_atMost)

  1622

  1623 lemma atLeast0_lessThan_Suc:

  1624   "F g {0..<Suc n} = F g {0..<n} \<^bold>* g n"

  1625   by (simp add: atLeast0_lessThan_Suc ac_simps)

  1626

  1627 lemma atLeast0_atMost_Suc:

  1628   "F g {0..Suc n} = F g {0..n} \<^bold>* g (Suc n)"

  1629   by (simp add: atLeast0_atMost_Suc ac_simps)

  1630

  1631 lemma atLeast0_lessThan_Suc_shift:

  1632   "F g {0..<Suc n} = g 0 \<^bold>* F (g \<circ> Suc) {0..<n}"

  1633   by (simp add: atLeast0_lessThan_Suc_eq_insert_0 atLeast_Suc_lessThan_Suc_shift)

  1634

  1635 lemma atLeast0_atMost_Suc_shift:

  1636   "F g {0..Suc n} = g 0 \<^bold>* F (g \<circ> Suc) {0..n}"

  1637   by (simp add: atLeast0_atMost_Suc_eq_insert_0 atLeast_Suc_atMost_Suc_shift)

  1638

  1639 lemma ivl_cong:

  1640   "a = c \<Longrightarrow> b = d \<Longrightarrow> (\<And>x. c \<le> x \<Longrightarrow> x < d \<Longrightarrow> g x = h x)

  1641     \<Longrightarrow> F g {a..<b} = F h {c..<d}"

  1642   by (rule cong) simp_all

  1643

  1644 lemma atLeast_lessThan_shift_0:

  1645   fixes m n p :: nat

  1646   shows "F g {m..<n} = F (g \<circ> plus m) {0..<n - m}"

  1647   using atLeast_lessThan_shift_bounds [of g 0 m "n - m"]

  1648   by (cases "m \<le> n") simp_all

  1649

  1650 lemma atLeast_atMost_shift_0:

  1651   fixes m n p :: nat

  1652   assumes "m \<le> n"

  1653   shows "F g {m..n} = F (g \<circ> plus m) {0..n - m}"

  1654   using assms atLeast_atMost_shift_bounds [of g 0 m "n - m"] by simp

  1655

  1656 lemma atLeast_lessThan_concat:

  1657   fixes m n p :: nat

  1658   shows "m \<le> n \<Longrightarrow> n \<le> p \<Longrightarrow> F g {m..<n} \<^bold>* F g {n..<p} = F g {m..<p}"

  1659   by (simp add: union_disjoint [symmetric] ivl_disj_un)

  1660

  1661 lemma atLeast_lessThan_rev:

  1662   "F g {n..<m} = F (\<lambda>i. g (m + n - Suc i)) {n..<m}"

  1663   by (rule reindex_bij_witness [where i="\<lambda>i. m + n - Suc i" and j="\<lambda>i. m + n - Suc i"], auto)

  1664

  1665 lemma atLeast_atMost_rev:

  1666   fixes n m :: nat

  1667   shows "F g {n..m} = F (\<lambda>i. g (m + n - i)) {n..m}"

  1668   by (rule reindex_bij_witness [where i="\<lambda>i. m + n - i" and j="\<lambda>i. m + n - i"]) auto

  1669

  1670 lemma atLeast_lessThan_rev_at_least_Suc_atMost:

  1671   "F g {n..<m} = F (\<lambda>i. g (m + n - i)) {Suc n..m}"

  1672   unfolding atLeast_lessThan_rev [of g n m]

  1673   by (cases m) (simp_all add: atLeast_Suc_atMost_Suc_shift atLeastLessThanSuc_atLeastAtMost)

  1674

  1675 end

  1676

  1677

  1678 subsection \<open>Summation indexed over intervals\<close>

  1679

  1680 syntax (ASCII)

  1681   "_from_to_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _ = _.._./ _)" [0,0,0,10] 10)

  1682   "_from_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _ = _..<_./ _)" [0,0,0,10] 10)

  1683   "_upt_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _<_./ _)" [0,0,10] 10)

  1684   "_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(SUM _<=_./ _)" [0,0,10] 10)

  1685

  1686 syntax (latex_sum output)

  1687   "_from_to_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"

  1688  ("(3\<^latex>\<open>$\\sum_{\<close>_ = _\<^latex>\<open>}^{\<close>_\<^latex>\<open>}$\<close> _)" [0,0,0,10] 10)

  1689   "_from_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"

  1690  ("(3\<^latex>\<open>$\\sum_{\<close>_ = _\<^latex>\<open>}^{<\<close>_\<^latex>\<open>}$\<close> _)" [0,0,0,10] 10)

  1691   "_upt_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"

  1692  ("(3\<^latex>\<open>$\\sum_{\<close>_ < _\<^latex>\<open>}$\<close> _)" [0,0,10] 10)

  1693   "_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"

  1694  ("(3\<^latex>\<open>$\\sum_{\<close>_ \<le> _\<^latex>\<open>}$\<close> _)" [0,0,10] 10)

  1695

  1696 syntax

  1697   "_from_to_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)

  1698   "_from_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)

  1699   "_upt_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_<_./ _)" [0,0,10] 10)

  1700   "_upto_sum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)

  1701

  1702 translations

  1703   "\<Sum>x=a..b. t" == "CONST sum (\<lambda>x. t) {a..b}"

  1704   "\<Sum>x=a..<b. t" == "CONST sum (\<lambda>x. t) {a..<b}"

  1705   "\<Sum>i\<le>n. t" == "CONST sum (\<lambda>i. t) {..n}"

  1706   "\<Sum>i<n. t" == "CONST sum (\<lambda>i. t) {..<n}"

  1707

  1708 text\<open>The above introduces some pretty alternative syntaxes for

  1709 summation over intervals:

  1710 \begin{center}

  1711 \begin{tabular}{lll}

  1712 Old & New & \LaTeX\\

  1713 @{term[source]"\<Sum>x\<in>{a..b}. e"} & @{term"\<Sum>x=a..b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..b. e"}\\

  1714 @{term[source]"\<Sum>x\<in>{a..<b}. e"} & @{term"\<Sum>x=a..<b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..<b. e"}\\

  1715 @{term[source]"\<Sum>x\<in>{..b}. e"} & @{term"\<Sum>x\<le>b. e"} & @{term[mode=latex_sum]"\<Sum>x\<le>b. e"}\\

  1716 @{term[source]"\<Sum>x\<in>{..<b}. e"} & @{term"\<Sum>x<b. e"} & @{term[mode=latex_sum]"\<Sum>x<b. e"}

  1717 \end{tabular}

  1718 \end{center}

  1719 The left column shows the term before introduction of the new syntax,

  1720 the middle column shows the new (default) syntax, and the right column

  1721 shows a special syntax. The latter is only meaningful for latex output

  1722 and has to be activated explicitly by setting the print mode to

  1723 \<open>latex_sum\<close> (e.g.\ via \<open>mode = latex_sum\<close> in

  1724 antiquotations). It is not the default \LaTeX\ output because it only

  1725 works well with italic-style formulae, not tt-style.

  1726

  1727 Note that for uniformity on @{typ nat} it is better to use

  1728 @{term"\<Sum>x::nat=0..<n. e"} rather than \<open>\<Sum>x<n. e\<close>: \<open>sum\<close> may

  1729 not provide all lemmas available for @{term"{m..<n}"} also in the

  1730 special form for @{term"{..<n}"}.\<close>

  1731

  1732 text\<open>This congruence rule should be used for sums over intervals as

  1733 the standard theorem @{text[source]sum.cong} does not work well

  1734 with the simplifier who adds the unsimplified premise @{term"x:B"} to

  1735 the context.\<close>

  1736

  1737 lemmas sum_ivl_cong = sum.ivl_cong

  1738

  1739 (* FIXME why are the following simp rules but the corresponding eqns

  1740 on intervals are not? *)

  1741

  1742 lemma sum_atMost_Suc [simp]:

  1743   "(\<Sum>i \<le> Suc n. f i) = (\<Sum>i \<le> n. f i) + f (Suc n)"

  1744   by (simp add: atMost_Suc ac_simps)

  1745

  1746 lemma sum_lessThan_Suc [simp]:

  1747   "(\<Sum>i < Suc n. f i) = (\<Sum>i < n. f i) + f n"

  1748   by (simp add: lessThan_Suc ac_simps)

  1749

  1750 lemma sum_cl_ivl_Suc [simp]:

  1751   "sum f {m..Suc n} = (if Suc n < m then 0 else sum f {m..n} + f(Suc n))"

  1752   by (auto simp: ac_simps atLeastAtMostSuc_conv)

  1753

  1754 lemma sum_op_ivl_Suc [simp]:

  1755   "sum f {m..<Suc n} = (if n < m then 0 else sum f {m..<n} + f(n))"

  1756   by (auto simp: ac_simps atLeastLessThanSuc)

  1757 (*

  1758 lemma sum_cl_ivl_add_one_nat: "(n::nat) <= m + 1 ==>

  1759     (\<Sum>i=n..m+1. f i) = (\<Sum>i=n..m. f i) + f(m + 1)"

  1760 by (auto simp:ac_simps atLeastAtMostSuc_conv)

  1761 *)

  1762

  1763 lemma sum_head:

  1764   fixes n :: nat

  1765   assumes mn: "m <= n"

  1766   shows "(\<Sum>x\<in>{m..n}. P x) = P m + (\<Sum>x\<in>{m<..n}. P x)" (is "?lhs = ?rhs")

  1767 proof -

  1768   from mn

  1769   have "{m..n} = {m} \<union> {m<..n}"

  1770     by (auto intro: ivl_disj_un_singleton)

  1771   hence "?lhs = (\<Sum>x\<in>{m} \<union> {m<..n}. P x)"

  1772     by (simp add: atLeast0LessThan)

  1773   also have "\<dots> = ?rhs" by simp

  1774   finally show ?thesis .

  1775 qed

  1776

  1777 lemma sum_head_Suc:

  1778   "m \<le> n \<Longrightarrow> sum f {m..n} = f m + sum f {Suc m..n}"

  1779 by (simp add: sum_head atLeastSucAtMost_greaterThanAtMost)

  1780

  1781 lemma sum_head_upt_Suc:

  1782   "m < n \<Longrightarrow> sum f {m..<n} = f m + sum f {Suc m..<n}"

  1783 apply(insert sum_head_Suc[of m "n - Suc 0" f])

  1784 apply (simp add: atLeastLessThanSuc_atLeastAtMost[symmetric] algebra_simps)

  1785 done

  1786

  1787 lemma sum_ub_add_nat: assumes "(m::nat) \<le> n + 1"

  1788   shows "sum f {m..n + p} = sum f {m..n} + sum f {n + 1..n + p}"

  1789 proof-

  1790   have "{m .. n+p} = {m..n} \<union> {n+1..n+p}" using \<open>m \<le> n+1\<close> by auto

  1791   thus ?thesis by (auto simp: ivl_disj_int sum.union_disjoint

  1792     atLeastSucAtMost_greaterThanAtMost)

  1793 qed

  1794

  1795 lemmas sum_add_nat_ivl = sum.atLeast_lessThan_concat

  1796

  1797 lemma sum_diff_nat_ivl:

  1798 fixes f :: "nat \<Rightarrow> 'a::ab_group_add"

  1799 shows "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>

  1800   sum f {m..<p} - sum f {m..<n} = sum f {n..<p}"

  1801 using sum_add_nat_ivl [of m n p f,symmetric]

  1802 apply (simp add: ac_simps)

  1803 done

  1804

  1805 lemma sum_natinterval_difff:

  1806   fixes f:: "nat \<Rightarrow> ('a::ab_group_add)"

  1807   shows  "sum (\<lambda>k. f k - f(k + 1)) {(m::nat) .. n} =

  1808           (if m <= n then f m - f(n + 1) else 0)"

  1809 by (induct n, auto simp add: algebra_simps not_le le_Suc_eq)

  1810

  1811 lemma sum_nat_group: "(\<Sum>m<n::nat. sum f {m * k ..< m*k + k}) = sum f {..< n * k}"

  1812   apply (subgoal_tac "k = 0 | 0 < k", auto)

  1813   apply (induct "n")

  1814   apply (simp_all add: sum_add_nat_ivl add.commute atLeast0LessThan[symmetric])

  1815   done

  1816

  1817 lemma sum_triangle_reindex:

  1818   fixes n :: nat

  1819   shows "(\<Sum>(i,j)\<in>{(i,j). i+j < n}. f i j) = (\<Sum>k<n. \<Sum>i\<le>k. f i (k - i))"

  1820   apply (simp add: sum.Sigma)

  1821   apply (rule sum.reindex_bij_witness[where j="\<lambda>(i, j). (i+j, i)" and i="\<lambda>(k, i). (i, k - i)"])

  1822   apply auto

  1823   done

  1824

  1825 lemma sum_triangle_reindex_eq:

  1826   fixes n :: nat

  1827   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))"

  1828 using sum_triangle_reindex [of f "Suc n"]

  1829 by (simp only: Nat.less_Suc_eq_le lessThan_Suc_atMost)

  1830

  1831 lemma nat_diff_sum_reindex: "(\<Sum>i<n. f (n - Suc i)) = (\<Sum>i<n. f i)"

  1832   by (rule sum.reindex_bij_witness[where i="\<lambda>i. n - Suc i" and j="\<lambda>i. n - Suc i"]) auto

  1833

  1834 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)"

  1835   by (subst sum_subtractf_nat) auto

  1836

  1837

  1838 subsubsection \<open>Shifting bounds\<close>

  1839

  1840 lemma sum_shift_bounds_nat_ivl:

  1841   "sum f {m+k..<n+k} = sum (%i. f(i + k)){m..<n::nat}"

  1842 by (induct "n", auto simp:atLeastLessThanSuc)

  1843

  1844 lemma sum_shift_bounds_cl_nat_ivl:

  1845   "sum f {m+k..n+k} = sum (%i. f(i + k)){m..n::nat}"

  1846   by (rule sum.reindex_bij_witness[where i="\<lambda>i. i + k" and j="\<lambda>i. i - k"]) auto

  1847

  1848 corollary sum_shift_bounds_cl_Suc_ivl:

  1849   "sum f {Suc m..Suc n} = sum (%i. f(Suc i)){m..n}"

  1850 by (simp add:sum_shift_bounds_cl_nat_ivl[where k="Suc 0", simplified])

  1851

  1852 corollary sum_shift_bounds_Suc_ivl:

  1853   "sum f {Suc m..<Suc n} = sum (%i. f(Suc i)){m..<n}"

  1854 by (simp add:sum_shift_bounds_nat_ivl[where k="Suc 0", simplified])

  1855

  1856 context comm_monoid_add

  1857 begin

  1858

  1859 context

  1860   fixes f :: "nat \<Rightarrow> 'a"

  1861   assumes "f 0 = 0"

  1862 begin

  1863

  1864 lemma sum_shift_lb_Suc0_0_upt:

  1865   "sum f {Suc 0..<k} = sum f {0..<k}"

  1866 proof (cases k)

  1867   case 0

  1868   then show ?thesis

  1869     by simp

  1870 next

  1871   case (Suc k)

  1872   moreover have "{0..<Suc k} = insert 0 {Suc 0..<Suc k}"

  1873     by auto

  1874   ultimately show ?thesis

  1875     using \<open>f 0 = 0\<close> by simp

  1876 qed

  1877

  1878 lemma sum_shift_lb_Suc0_0:

  1879   "sum f {Suc 0..k} = sum f {0..k}"

  1880 proof (cases k)

  1881   case 0

  1882   with \<open>f 0 = 0\<close> show ?thesis

  1883     by simp

  1884 next

  1885   case (Suc k)

  1886   moreover have "{0..Suc k} = insert 0 {Suc 0..Suc k}"

  1887     by auto

  1888   ultimately show ?thesis

  1889     using \<open>f 0 = 0\<close> by simp

  1890 qed

  1891

  1892 end

  1893

  1894 end

  1895

  1896 lemma sum_atMost_Suc_shift:

  1897   fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"

  1898   shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"

  1899 proof (induct n)

  1900   case 0 show ?case by simp

  1901 next

  1902   case (Suc n) note IH = this

  1903   have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"

  1904     by (rule sum_atMost_Suc)

  1905   also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"

  1906     by (rule IH)

  1907   also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =

  1908              f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"

  1909     by (rule add.assoc)

  1910   also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"

  1911     by (rule sum_atMost_Suc [symmetric])

  1912   finally show ?case .

  1913 qed

  1914

  1915 lemma sum_lessThan_Suc_shift:

  1916   "(\<Sum>i<Suc n. f i) = f 0 + (\<Sum>i<n. f (Suc i))"

  1917   by (induction n) (simp_all add: add_ac)

  1918

  1919 lemma sum_atMost_shift:

  1920   fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"

  1921   shows "(\<Sum>i\<le>n. f i) = f 0 + (\<Sum>i<n. f (Suc i))"

  1922 by (metis atLeast0AtMost atLeast0LessThan atLeastLessThanSuc_atLeastAtMost atLeastSucAtMost_greaterThanAtMost le0 sum_head sum_shift_bounds_Suc_ivl)

  1923

  1924 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)"

  1925   by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost add.commute)

  1926

  1927 lemma sum_Suc_diff:

  1928   fixes f :: "nat \<Rightarrow> 'a::ab_group_add"

  1929   assumes "m \<le> Suc n"

  1930   shows "(\<Sum>i = m..n. f(Suc i) - f i) = f (Suc n) - f m"

  1931 using assms by (induct n) (auto simp: le_Suc_eq)

  1932

  1933 lemma sum_Suc_diff':

  1934   fixes f :: "nat \<Rightarrow> 'a::ab_group_add"

  1935   assumes "m \<le> n"

  1936   shows "(\<Sum>i = m..<n. f (Suc i) - f i) = f n - f m"

  1937 using assms by (induct n) (auto simp: le_Suc_eq)

  1938

  1939 lemma nested_sum_swap:

  1940      "(\<Sum>i = 0..n. (\<Sum>j = 0..<i. a i j)) = (\<Sum>j = 0..<n. \<Sum>i = Suc j..n. a i j)"

  1941   by (induction n) (auto simp: sum.distrib)

  1942

  1943 lemma nested_sum_swap':

  1944      "(\<Sum>i\<le>n. (\<Sum>j<i. a i j)) = (\<Sum>j<n. \<Sum>i = Suc j..n. a i j)"

  1945   by (induction n) (auto simp: sum.distrib)

  1946

  1947 lemma sum_atLeast1_atMost_eq:

  1948   "sum f {Suc 0..n} = (\<Sum>k<n. f (Suc k))"

  1949 proof -

  1950   have "sum f {Suc 0..n} = sum f (Suc  {..<n})"

  1951     by (simp add: image_Suc_lessThan)

  1952   also have "\<dots> = (\<Sum>k<n. f (Suc k))"

  1953     by (simp add: sum.reindex)

  1954   finally show ?thesis .

  1955 qed

  1956

  1957

  1958 subsubsection \<open>Telescoping\<close>

  1959

  1960 lemma sum_telescope:

  1961   fixes f::"nat \<Rightarrow> 'a::ab_group_add"

  1962   shows "sum (\<lambda>i. f i - f (Suc i)) {.. i} = f 0 - f (Suc i)"

  1963   by (induct i) simp_all

  1964

  1965 lemma sum_telescope'':

  1966   assumes "m \<le> n"

  1967   shows   "(\<Sum>k\<in>{Suc m..n}. f k - f (k - 1)) = f n - (f m :: 'a :: ab_group_add)"

  1968   by (rule dec_induct[OF assms]) (simp_all add: algebra_simps)

  1969

  1970 lemma sum_lessThan_telescope:

  1971   "(\<Sum>n<m. f (Suc n) - f n :: 'a :: ab_group_add) = f m - f 0"

  1972   by (induction m) (simp_all add: algebra_simps)

  1973

  1974 lemma sum_lessThan_telescope':

  1975   "(\<Sum>n<m. f n - f (Suc n) :: 'a :: ab_group_add) = f 0 - f m"

  1976   by (induction m) (simp_all add: algebra_simps)

  1977

  1978

  1979 subsubsection \<open>The formula for geometric sums\<close>

  1980

  1981 lemma sum_power2: "(\<Sum>i=0..<k. (2::nat)^i) = 2^k-1"

  1982 by (induction k) (auto simp: mult_2)

  1983

  1984 lemma geometric_sum:

  1985   assumes "x \<noteq> 1"

  1986   shows "(\<Sum>i<n. x ^ i) = (x ^ n - 1) / (x - 1::'a::field)"

  1987 proof -

  1988   from assms obtain y where "y = x - 1" and "y \<noteq> 0" by simp_all

  1989   moreover have "(\<Sum>i<n. (y + 1) ^ i) = ((y + 1) ^ n - 1) / y"

  1990     by (induct n) (simp_all add: field_simps \<open>y \<noteq> 0\<close>)

  1991   ultimately show ?thesis by simp

  1992 qed

  1993

  1994 lemma diff_power_eq_sum:

  1995   fixes y :: "'a::{comm_ring,monoid_mult}"

  1996   shows

  1997     "x ^ (Suc n) - y ^ (Suc n) =

  1998       (x - y) * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))"

  1999 proof (induct n)

  2000   case (Suc n)

  2001   have "x ^ Suc (Suc n) - y ^ Suc (Suc n) = x * (x * x^n) - y * (y * y ^ n)"

  2002     by simp

  2003   also have "... = y * (x ^ (Suc n) - y ^ (Suc n)) + (x - y) * (x * x^n)"

  2004     by (simp add: algebra_simps)

  2005   also have "... = y * ((x - y) * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))) + (x - y) * (x * x^n)"

  2006     by (simp only: Suc)

  2007   also have "... = (x - y) * (y * (\<Sum>p<Suc n. (x ^ p) * y ^ (n - p))) + (x - y) * (x * x^n)"

  2008     by (simp only: mult.left_commute)

  2009   also have "... = (x - y) * (\<Sum>p<Suc (Suc n). x ^ p * y ^ (Suc n - p))"

  2010     by (simp add: field_simps Suc_diff_le sum_distrib_right sum_distrib_left)

  2011   finally show ?case .

  2012 qed simp

  2013

  2014 corollary power_diff_sumr2: \<comment>\<open>\<open>COMPLEX_POLYFUN\<close> in HOL Light\<close>

  2015   fixes x :: "'a::{comm_ring,monoid_mult}"

  2016   shows   "x^n - y^n = (x - y) * (\<Sum>i<n. y^(n - Suc i) * x^i)"

  2017 using diff_power_eq_sum[of x "n - 1" y]

  2018 by (cases "n = 0") (simp_all add: field_simps)

  2019

  2020 lemma power_diff_1_eq:

  2021   fixes x :: "'a::{comm_ring,monoid_mult}"

  2022   shows "n \<noteq> 0 \<Longrightarrow> x^n - 1 = (x - 1) * (\<Sum>i<n. (x^i))"

  2023 using diff_power_eq_sum [of x _ 1]

  2024   by (cases n) auto

  2025

  2026 lemma one_diff_power_eq':

  2027   fixes x :: "'a::{comm_ring,monoid_mult}"

  2028   shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^(n - Suc i))"

  2029 using diff_power_eq_sum [of 1 _ x]

  2030   by (cases n) auto

  2031

  2032 lemma one_diff_power_eq:

  2033   fixes x :: "'a::{comm_ring,monoid_mult}"

  2034   shows "n \<noteq> 0 \<Longrightarrow> 1 - x^n = (1 - x) * (\<Sum>i<n. x^i)"

  2035 by (metis one_diff_power_eq' [of n x] nat_diff_sum_reindex)

  2036

  2037 lemma sum_gp_basic:

  2038   fixes x :: "'a::{comm_ring,monoid_mult}"

  2039   shows "(1 - x) * (\<Sum>i\<le>n. x^i) = 1 - x^Suc n"

  2040   by (simp only: one_diff_power_eq [of "Suc n" x] lessThan_Suc_atMost)

  2041

  2042 lemma sum_power_shift:

  2043   fixes x :: "'a::{comm_ring,monoid_mult}"

  2044   assumes "m \<le> n"

  2045   shows "(\<Sum>i=m..n. x^i) = x^m * (\<Sum>i\<le>n-m. x^i)"

  2046 proof -

  2047   have "(\<Sum>i=m..n. x^i) = x^m * (\<Sum>i=m..n. x^(i-m))"

  2048     by (simp add: sum_distrib_left power_add [symmetric])

  2049   also have "(\<Sum>i=m..n. x^(i-m)) = (\<Sum>i\<le>n-m. x^i)"

  2050     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

  2051   finally show ?thesis .

  2052 qed

  2053

  2054 lemma sum_gp_multiplied:

  2055   fixes x :: "'a::{comm_ring,monoid_mult}"

  2056   assumes "m \<le> n"

  2057   shows "(1 - x) * (\<Sum>i=m..n. x^i) = x^m - x^Suc n"

  2058 proof -

  2059   have  "(1 - x) * (\<Sum>i=m..n. x^i) = x^m * (1 - x) * (\<Sum>i\<le>n-m. x^i)"

  2060     by (metis mult.assoc mult.commute assms sum_power_shift)

  2061   also have "... =x^m * (1 - x^Suc(n-m))"

  2062     by (metis mult.assoc sum_gp_basic)

  2063   also have "... = x^m - x^Suc n"

  2064     using assms

  2065     by (simp add: algebra_simps) (metis le_add_diff_inverse power_add)

  2066   finally show ?thesis .

  2067 qed

  2068

  2069 lemma sum_gp:

  2070   fixes x :: "'a::{comm_ring,division_ring}"

  2071   shows   "(\<Sum>i=m..n. x^i) =

  2072                (if n < m then 0

  2073                 else if x = 1 then of_nat((n + 1) - m)

  2074                 else (x^m - x^Suc n) / (1 - x))"

  2075 using sum_gp_multiplied [of m n x] apply auto

  2076 by (metis eq_iff_diff_eq_0 mult.commute nonzero_divide_eq_eq)

  2077

  2078

  2079 subsubsection\<open>Geometric progressions\<close>

  2080

  2081 lemma sum_gp0:

  2082   fixes x :: "'a::{comm_ring,division_ring}"

  2083   shows   "(\<Sum>i\<le>n. x^i) = (if x = 1 then of_nat(n + 1) else (1 - x^Suc n) / (1 - x))"

  2084   using sum_gp_basic[of x n]

  2085   by (simp add: mult.commute divide_simps)

  2086

  2087 lemma sum_power_add:

  2088   fixes x :: "'a::{comm_ring,monoid_mult}"

  2089   shows "(\<Sum>i\<in>I. x^(m+i)) = x^m * (\<Sum>i\<in>I. x^i)"

  2090   by (simp add: sum_distrib_left power_add)

  2091

  2092 lemma sum_gp_offset:

  2093   fixes x :: "'a::{comm_ring,division_ring}"

  2094   shows   "(\<Sum>i=m..m+n. x^i) =

  2095        (if x = 1 then of_nat n + 1 else x^m * (1 - x^Suc n) / (1 - x))"

  2096   using sum_gp [of x m "m+n"]

  2097   by (auto simp: power_add algebra_simps)

  2098

  2099 lemma sum_gp_strict:

  2100   fixes x :: "'a::{comm_ring,division_ring}"

  2101   shows "(\<Sum>i<n. x^i) = (if x = 1 then of_nat n else (1 - x^n) / (1 - x))"

  2102   by (induct n) (auto simp: algebra_simps divide_simps)

  2103

  2104

  2105 subsubsection \<open>The formulae for arithmetic sums\<close>

  2106

  2107 context comm_semiring_1

  2108 begin

  2109

  2110 lemma double_gauss_sum:

  2111   "2 * (\<Sum>i = 0..n. of_nat i) = of_nat n * (of_nat n + 1)"

  2112   by (induct n) (simp_all add: sum.atLeast0_atMost_Suc algebra_simps left_add_twice)

  2113

  2114 lemma double_gauss_sum_from_Suc_0:

  2115   "2 * (\<Sum>i = Suc 0..n. of_nat i) = of_nat n * (of_nat n + 1)"

  2116 proof -

  2117   have "sum of_nat {Suc 0..n} = sum of_nat (insert 0 {Suc 0..n})"

  2118     by simp

  2119   also have "\<dots> = sum of_nat {0..n}"

  2120     by (cases n) (simp_all add: atLeast0_atMost_Suc_eq_insert_0)

  2121   finally show ?thesis

  2122     by (simp add: double_gauss_sum)

  2123 qed

  2124

  2125 lemma double_arith_series:

  2126   "2 * (\<Sum>i = 0..n. a + of_nat i * d) = (of_nat n + 1) * (2 * a + of_nat n * d)"

  2127 proof -

  2128   have "(\<Sum>i = 0..n. a + of_nat i * d) = ((\<Sum>i = 0..n. a) + (\<Sum>i = 0..n. of_nat i * d))"

  2129     by (rule sum.distrib)

  2130   also have "\<dots> = (of_nat (Suc n) * a + d * (\<Sum>i = 0..n. of_nat i))"

  2131     by (simp add: sum_distrib_left algebra_simps)

  2132   finally show ?thesis

  2133     by (simp add: algebra_simps double_gauss_sum left_add_twice)

  2134 qed

  2135

  2136 end

  2137

  2138 context semiring_parity

  2139 begin

  2140

  2141 lemma gauss_sum:

  2142   "(\<Sum>i = 0..n. of_nat i) = of_nat n * (of_nat n + 1) div 2"

  2143   using double_gauss_sum [of n, symmetric] by simp

  2144

  2145 lemma gauss_sum_from_Suc_0:

  2146   "(\<Sum>i = Suc 0..n. of_nat i) = of_nat n * (of_nat n + 1) div 2"

  2147   using double_gauss_sum_from_Suc_0 [of n, symmetric] by simp

  2148

  2149 lemma arith_series:

  2150   "(\<Sum>i = 0..n. a + of_nat i * d) = (of_nat n + 1) * (2 * a + of_nat n * d) div 2"

  2151   using double_arith_series [of a d n, symmetric] by simp

  2152

  2153 end

  2154

  2155 lemma gauss_sum_nat:

  2156   "\<Sum>{0..n} = (n * Suc n) div 2"

  2157   using gauss_sum [of n, where ?'a = nat] by simp

  2158

  2159 lemma arith_series_nat:

  2160   "(\<Sum>i = 0..n. a + i * d) = Suc n * (2 * a + n * d) div 2"

  2161   using arith_series [of a d n] by simp

  2162

  2163 lemma Sum_Icc_int:

  2164   "\<Sum>{m..n} = (n * (n + 1) - m * (m - 1)) div 2"

  2165   if "m \<le> n" for m n :: int

  2166 using that proof (induct i \<equiv> "nat (n - m)" arbitrary: m n)

  2167   case 0

  2168   then have "m = n"

  2169     by arith

  2170   then show ?case

  2171     by (simp add: algebra_simps mult_2 [symmetric])

  2172 next

  2173   case (Suc i)

  2174   have 0: "i = nat((n-1) - m)" "m \<le> n-1" using Suc(2,3) by arith+

  2175   have "\<Sum> {m..n} = \<Sum> {m..1+(n-1)}" by simp

  2176   also have "\<dots> = \<Sum> {m..n-1} + n" using \<open>m \<le> n\<close>

  2177     by(subst atLeastAtMostPlus1_int_conv) simp_all

  2178   also have "\<dots> = ((n-1)*(n-1+1) - m*(m-1)) div 2 + n"

  2179     by(simp add: Suc(1)[OF 0])

  2180   also have "\<dots> = ((n-1)*(n-1+1) - m*(m-1) + 2*n) div 2" by simp

  2181   also have "\<dots> = (n*(n+1) - m*(m-1)) div 2"

  2182     by (simp add: algebra_simps mult_2_right)

  2183   finally show ?case .

  2184 qed

  2185

  2186 lemma Sum_Icc_nat:

  2187   "\<Sum>{m..n} = (n * (n + 1) - m * (m - 1)) div 2"

  2188   if "m \<le> n" for m n :: nat

  2189 proof -

  2190   have *: "m * (m - 1) \<le> n * (n + 1)"

  2191     using that by (meson diff_le_self order_trans le_add1 mult_le_mono)

  2192   have "int (\<Sum>{m..n}) = (\<Sum>{int m..int n})"

  2193     by (simp add: sum.atLeast_int_atMost_int_shift)

  2194   also have "\<dots> = (int n * (int n + 1) - int m * (int m - 1)) div 2"

  2195     using that by (simp add: Sum_Icc_int)

  2196   also have "\<dots> = int ((n * (n + 1) - m * (m - 1)) div 2)"

  2197     using le_square * by (simp add: algebra_simps of_nat_div of_nat_diff)

  2198   finally show ?thesis

  2199     by (simp only: of_nat_eq_iff)

  2200 qed

  2201

  2202 lemma Sum_Ico_nat:

  2203   "\<Sum>{m..<n} = (n * (n - 1) - m * (m - 1)) div 2"

  2204   if "m \<le> n" for m n :: nat

  2205 proof -

  2206   from that consider "m < n" | "m = n"

  2207     by (auto simp add: less_le)

  2208   then show ?thesis proof cases

  2209     case 1

  2210     then have "{m..<n} = {m..n - 1}"

  2211       by auto

  2212     then have "\<Sum>{m..<n} = \<Sum>{m..n - 1}"

  2213       by simp

  2214     also have "\<dots> = (n * (n - 1) - m * (m - 1)) div 2"

  2215       using \<open>m < n\<close> by (simp add: Sum_Icc_nat mult.commute)

  2216     finally show ?thesis .

  2217   next

  2218     case 2

  2219     then show ?thesis

  2220       by simp

  2221   qed

  2222 qed

  2223

  2224

  2225 subsubsection \<open>Division remainder\<close>

  2226

  2227 lemma range_mod:

  2228   fixes n :: nat

  2229   assumes "n > 0"

  2230   shows "range (\<lambda>m. m mod n) = {0..<n}" (is "?A = ?B")

  2231 proof (rule set_eqI)

  2232   fix m

  2233   show "m \<in> ?A \<longleftrightarrow> m \<in> ?B"

  2234   proof

  2235     assume "m \<in> ?A"

  2236     with assms show "m \<in> ?B"

  2237       by auto

  2238   next

  2239     assume "m \<in> ?B"

  2240     moreover have "m mod n \<in> ?A"

  2241       by (rule rangeI)

  2242     ultimately show "m \<in> ?A"

  2243       by simp

  2244   qed

  2245 qed

  2246

  2247

  2248 subsection \<open>Products indexed over intervals\<close>

  2249

  2250 syntax (ASCII)

  2251   "_from_to_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _ = _.._./ _)" [0,0,0,10] 10)

  2252   "_from_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _ = _..<_./ _)" [0,0,0,10] 10)

  2253   "_upt_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _<_./ _)" [0,0,10] 10)

  2254   "_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(PROD _<=_./ _)" [0,0,10] 10)

  2255

  2256 syntax (latex_prod output)

  2257   "_from_to_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"

  2258  ("(3\<^latex>\<open>$\\prod_{\<close>_ = _\<^latex>\<open>}^{\<close>_\<^latex>\<open>}$\<close> _)" [0,0,0,10] 10)

  2259   "_from_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"

  2260  ("(3\<^latex>\<open>$\\prod_{\<close>_ = _\<^latex>\<open>}^{<\<close>_\<^latex>\<open>}$\<close> _)" [0,0,0,10] 10)

  2261   "_upt_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"

  2262  ("(3\<^latex>\<open>$\\prod_{\<close>_ < _\<^latex>\<open>}$\<close> _)" [0,0,10] 10)

  2263   "_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"

  2264  ("(3\<^latex>\<open>$\\prod_{\<close>_ \<le> _\<^latex>\<open>}$\<close> _)" [0,0,10] 10)

  2265

  2266 syntax

  2267   "_from_to_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_ = _.._./ _)" [0,0,0,10] 10)

  2268   "_from_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_ = _..<_./ _)" [0,0,0,10] 10)

  2269   "_upt_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_<_./ _)" [0,0,10] 10)

  2270   "_upto_prod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Prod>_\<le>_./ _)" [0,0,10] 10)

  2271

  2272 translations

  2273   "\<Prod>x=a..b. t" \<rightleftharpoons> "CONST prod (\<lambda>x. t) {a..b}"

  2274   "\<Prod>x=a..<b. t" \<rightleftharpoons> "CONST prod (\<lambda>x. t) {a..<b}"

  2275   "\<Prod>i\<le>n. t" \<rightleftharpoons> "CONST prod (\<lambda>i. t) {..n}"

  2276   "\<Prod>i<n. t" \<rightleftharpoons> "CONST prod (\<lambda>i. t) {..<n}"

  2277

  2278 lemma prod_int_plus_eq: "prod int {i..i+j} =  \<Prod>{int i..int (i+j)}"

  2279   by (induct j) (auto simp add: atLeastAtMostSuc_conv atLeastAtMostPlus1_int_conv)

  2280

  2281 lemma prod_int_eq: "prod int {i..j} =  \<Prod>{int i..int j}"

  2282 proof (cases "i \<le> j")

  2283   case True

  2284   then show ?thesis

  2285     by (metis le_iff_add prod_int_plus_eq)

  2286 next

  2287   case False

  2288   then show ?thesis

  2289     by auto

  2290 qed

  2291

  2292

  2293 subsubsection \<open>Shifting bounds\<close>

  2294

  2295 lemma prod_shift_bounds_nat_ivl:

  2296   "prod f {m+k..<n+k} = prod (%i. f(i + k)){m..<n::nat}"

  2297 by (induct "n", auto simp:atLeastLessThanSuc)

  2298

  2299 lemma prod_shift_bounds_cl_nat_ivl:

  2300   "prod f {m+k..n+k} = prod (%i. f(i + k)){m..n::nat}"

  2301   by (rule prod.reindex_bij_witness[where i="\<lambda>i. i + k" and j="\<lambda>i. i - k"]) auto

  2302

  2303 corollary prod_shift_bounds_cl_Suc_ivl:

  2304   "prod f {Suc m..Suc n} = prod (%i. f(Suc i)){m..n}"

  2305 by (simp add:prod_shift_bounds_cl_nat_ivl[where k="Suc 0", simplified])

  2306

  2307 corollary prod_shift_bounds_Suc_ivl:

  2308   "prod f {Suc m..<Suc n} = prod (%i. f(Suc i)){m..<n}"

  2309 by (simp add:prod_shift_bounds_nat_ivl[where k="Suc 0", simplified])

  2310

  2311 lemma prod_lessThan_Suc: "prod f {..<Suc n} = prod f {..<n} * f n"

  2312   by (simp add: lessThan_Suc mult.commute)

  2313

  2314 lemma prod_lessThan_Suc_shift:"(\<Prod>i<Suc n. f i) = f 0 * (\<Prod>i<n. f (Suc i))"

  2315   by (induction n) (simp_all add: lessThan_Suc mult_ac)

  2316

  2317 lemma prod_atLeastLessThan_Suc: "a \<le> b \<Longrightarrow> prod f {a..<Suc b} = prod f {a..<b} * f b"

  2318   by (simp add: atLeastLessThanSuc mult.commute)

  2319

  2320 lemma prod_nat_ivl_Suc':

  2321   assumes "m \<le> Suc n"

  2322   shows   "prod f {m..Suc n} = f (Suc n) * prod f {m..n}"

  2323 proof -

  2324   from assms have "{m..Suc n} = insert (Suc n) {m..n}" by auto

  2325   also have "prod f \<dots> = f (Suc n) * prod f {m..n}" by simp

  2326   finally show ?thesis .

  2327 qed

  2328

  2329

  2330 subsection \<open>Efficient folding over intervals\<close>

  2331

  2332 function fold_atLeastAtMost_nat where

  2333   [simp del]: "fold_atLeastAtMost_nat f a (b::nat) acc =

  2334                  (if a > b then acc else fold_atLeastAtMost_nat f (a+1) b (f a acc))"

  2335 by pat_completeness auto

  2336 termination by (relation "measure (\<lambda>(_,a,b,_). Suc b - a)") auto

  2337

  2338 lemma fold_atLeastAtMost_nat:

  2339   assumes "comp_fun_commute f"

  2340   shows   "fold_atLeastAtMost_nat f a b acc = Finite_Set.fold f acc {a..b}"

  2341 using assms

  2342 proof (induction f a b acc rule: fold_atLeastAtMost_nat.induct, goal_cases)

  2343   case (1 f a b acc)

  2344   interpret comp_fun_commute f by fact

  2345   show ?case

  2346   proof (cases "a > b")

  2347     case True

  2348     thus ?thesis by (subst fold_atLeastAtMost_nat.simps) auto

  2349   next

  2350     case False

  2351     with 1 show ?thesis

  2352       by (subst fold_atLeastAtMost_nat.simps)

  2353          (auto simp: atLeastAtMost_insertL[symmetric] fold_fun_left_comm)

  2354   qed

  2355 qed

  2356

  2357 lemma sum_atLeastAtMost_code:

  2358   "sum f {a..b} = fold_atLeastAtMost_nat (\<lambda>a acc. f a + acc) a b 0"

  2359 proof -

  2360   have "comp_fun_commute (\<lambda>a. op + (f a))"

  2361     by unfold_locales (auto simp: o_def add_ac)

  2362   thus ?thesis

  2363     by (simp add: sum.eq_fold fold_atLeastAtMost_nat o_def)

  2364 qed

  2365

  2366 lemma prod_atLeastAtMost_code:

  2367   "prod f {a..b} = fold_atLeastAtMost_nat (\<lambda>a acc. f a * acc) a b 1"

  2368 proof -

  2369   have "comp_fun_commute (\<lambda>a. op * (f a))"

  2370     by unfold_locales (auto simp: o_def mult_ac)

  2371   thus ?thesis

  2372     by (simp add: prod.eq_fold fold_atLeastAtMost_nat o_def)

  2373 qed

  2374

  2375 (* TODO: Add support for more kinds of intervals here *)

  2376

  2377 end