src/HOL/SetInterval.thy
author kleing
Sun Feb 19 13:21:32 2006 +0100 (2006-02-19)
changeset 19106 6e6b5b1fdc06
parent 19022 0e6ec4fd204c
child 19376 529b735edbf2
permissions -rw-r--r--
* added Library/ASeries (sum of arithmetic series with instantiation to nat and int)
* added Complex/ex/ASeries_Complex (instantiation of the above for reals)
* added Complex/ex/HarmonicSeries (should really be in something like Complex/Library)

(these are contributions by Benjamin Porter, numbers 68 and 34 of
http://www.cs.ru.nl/~freek/100/)
nipkow@8924
     1
(*  Title:      HOL/SetInterval.thy
nipkow@8924
     2
    ID:         $Id$
ballarin@13735
     3
    Author:     Tobias Nipkow and Clemens Ballarin
paulson@14485
     4
                Additions by Jeremy Avigad in March 2004
paulson@8957
     5
    Copyright   2000  TU Muenchen
nipkow@8924
     6
ballarin@13735
     7
lessThan, greaterThan, atLeast, atMost and two-sided intervals
nipkow@8924
     8
*)
nipkow@8924
     9
wenzelm@14577
    10
header {* Set intervals *}
wenzelm@14577
    11
nipkow@15131
    12
theory SetInterval
nipkow@15140
    13
imports IntArith
nipkow@15131
    14
begin
nipkow@8924
    15
nipkow@8924
    16
constdefs
nipkow@15045
    17
  lessThan    :: "('a::ord) => 'a set"	("(1{..<_})")
nipkow@15045
    18
  "{..<u} == {x. x<u}"
nipkow@8924
    19
wenzelm@11609
    20
  atMost      :: "('a::ord) => 'a set"	("(1{.._})")
wenzelm@11609
    21
  "{..u} == {x. x<=u}"
nipkow@8924
    22
nipkow@15045
    23
  greaterThan :: "('a::ord) => 'a set"	("(1{_<..})")
nipkow@15045
    24
  "{l<..} == {x. l<x}"
nipkow@8924
    25
wenzelm@11609
    26
  atLeast     :: "('a::ord) => 'a set"	("(1{_..})")
wenzelm@11609
    27
  "{l..} == {x. l<=x}"
nipkow@8924
    28
nipkow@15045
    29
  greaterThanLessThan :: "['a::ord, 'a] => 'a set"  ("(1{_<..<_})")
nipkow@15045
    30
  "{l<..<u} == {l<..} Int {..<u}"
ballarin@13735
    31
nipkow@15045
    32
  atLeastLessThan :: "['a::ord, 'a] => 'a set"      ("(1{_..<_})")
nipkow@15045
    33
  "{l..<u} == {l..} Int {..<u}"
ballarin@13735
    34
nipkow@15045
    35
  greaterThanAtMost :: "['a::ord, 'a] => 'a set"    ("(1{_<.._})")
nipkow@15045
    36
  "{l<..u} == {l<..} Int {..u}"
ballarin@13735
    37
ballarin@13735
    38
  atLeastAtMost :: "['a::ord, 'a] => 'a set"        ("(1{_.._})")
ballarin@13735
    39
  "{l..u} == {l..} Int {..u}"
ballarin@13735
    40
nipkow@15045
    41
(* Old syntax, will disappear! *)
nipkow@15045
    42
syntax
nipkow@15045
    43
  "_lessThan"    :: "('a::ord) => 'a set"	("(1{.._'(})")
nipkow@15045
    44
  "_greaterThan" :: "('a::ord) => 'a set"	("(1{')_..})")
nipkow@15045
    45
  "_greaterThanLessThan" :: "['a::ord, 'a] => 'a set"  ("(1{')_.._'(})")
nipkow@15045
    46
  "_atLeastLessThan" :: "['a::ord, 'a] => 'a set"      ("(1{_.._'(})")
nipkow@15045
    47
  "_greaterThanAtMost" :: "['a::ord, 'a] => 'a set"    ("(1{')_.._})")
nipkow@15045
    48
translations
nipkow@15045
    49
  "{..m(}" => "{..<m}"
nipkow@15045
    50
  "{)m..}" => "{m<..}"
nipkow@15045
    51
  "{)m..n(}" => "{m<..<n}"
nipkow@15045
    52
  "{m..n(}" => "{m..<n}"
nipkow@15045
    53
  "{)m..n}" => "{m<..n}"
nipkow@15045
    54
nipkow@15048
    55
nipkow@15048
    56
text{* A note of warning when using @{term"{..<n}"} on type @{typ
nipkow@15048
    57
nat}: it is equivalent to @{term"{0::nat..<n}"} but some lemmas involving
nipkow@15052
    58
@{term"{m..<n}"} may not exist in @{term"{..<n}"}-form as well. *}
nipkow@15048
    59
kleing@14418
    60
syntax
kleing@14418
    61
  "@UNION_le"   :: "nat => nat => 'b set => 'b set"       ("(3UN _<=_./ _)" 10)
kleing@14418
    62
  "@UNION_less" :: "nat => nat => 'b set => 'b set"       ("(3UN _<_./ _)" 10)
kleing@14418
    63
  "@INTER_le"   :: "nat => nat => 'b set => 'b set"       ("(3INT _<=_./ _)" 10)
kleing@14418
    64
  "@INTER_less" :: "nat => nat => 'b set => 'b set"       ("(3INT _<_./ _)" 10)
kleing@14418
    65
kleing@14418
    66
syntax (input)
kleing@14418
    67
  "@UNION_le"   :: "nat => nat => 'b set => 'b set"       ("(3\<Union> _\<le>_./ _)" 10)
kleing@14418
    68
  "@UNION_less" :: "nat => nat => 'b set => 'b set"       ("(3\<Union> _<_./ _)" 10)
kleing@14418
    69
  "@INTER_le"   :: "nat => nat => 'b set => 'b set"       ("(3\<Inter> _\<le>_./ _)" 10)
kleing@14418
    70
  "@INTER_less" :: "nat => nat => 'b set => 'b set"       ("(3\<Inter> _<_./ _)" 10)
kleing@14418
    71
kleing@14418
    72
syntax (xsymbols)
wenzelm@14846
    73
  "@UNION_le"   :: "nat \<Rightarrow> nat => 'b set => 'b set"       ("(3\<Union>(00\<^bsub>_ \<le> _\<^esub>)/ _)" 10)
wenzelm@14846
    74
  "@UNION_less" :: "nat \<Rightarrow> nat => 'b set => 'b set"       ("(3\<Union>(00\<^bsub>_ < _\<^esub>)/ _)" 10)
wenzelm@14846
    75
  "@INTER_le"   :: "nat \<Rightarrow> nat => 'b set => 'b set"       ("(3\<Inter>(00\<^bsub>_ \<le> _\<^esub>)/ _)" 10)
wenzelm@14846
    76
  "@INTER_less" :: "nat \<Rightarrow> nat => 'b set => 'b set"       ("(3\<Inter>(00\<^bsub>_ < _\<^esub>)/ _)" 10)
kleing@14418
    77
kleing@14418
    78
translations
kleing@14418
    79
  "UN i<=n. A"  == "UN i:{..n}. A"
nipkow@15045
    80
  "UN i<n. A"   == "UN i:{..<n}. A"
kleing@14418
    81
  "INT i<=n. A" == "INT i:{..n}. A"
nipkow@15045
    82
  "INT i<n. A"  == "INT i:{..<n}. A"
kleing@14418
    83
kleing@14418
    84
paulson@14485
    85
subsection {* Various equivalences *}
ballarin@13735
    86
paulson@13850
    87
lemma lessThan_iff [iff]: "(i: lessThan k) = (i<k)"
paulson@13850
    88
by (simp add: lessThan_def)
ballarin@13735
    89
paulson@15418
    90
lemma Compl_lessThan [simp]:
ballarin@13735
    91
    "!!k:: 'a::linorder. -lessThan k = atLeast k"
paulson@13850
    92
apply (auto simp add: lessThan_def atLeast_def)
ballarin@13735
    93
done
ballarin@13735
    94
paulson@13850
    95
lemma single_Diff_lessThan [simp]: "!!k:: 'a::order. {k} - lessThan k = {k}"
paulson@13850
    96
by auto
ballarin@13735
    97
paulson@13850
    98
lemma greaterThan_iff [iff]: "(i: greaterThan k) = (k<i)"
paulson@13850
    99
by (simp add: greaterThan_def)
ballarin@13735
   100
paulson@15418
   101
lemma Compl_greaterThan [simp]:
ballarin@13735
   102
    "!!k:: 'a::linorder. -greaterThan k = atMost k"
paulson@13850
   103
apply (simp add: greaterThan_def atMost_def le_def, auto)
ballarin@13735
   104
done
ballarin@13735
   105
paulson@13850
   106
lemma Compl_atMost [simp]: "!!k:: 'a::linorder. -atMost k = greaterThan k"
paulson@13850
   107
apply (subst Compl_greaterThan [symmetric])
paulson@15418
   108
apply (rule double_complement)
ballarin@13735
   109
done
ballarin@13735
   110
paulson@13850
   111
lemma atLeast_iff [iff]: "(i: atLeast k) = (k<=i)"
paulson@13850
   112
by (simp add: atLeast_def)
ballarin@13735
   113
paulson@15418
   114
lemma Compl_atLeast [simp]:
ballarin@13735
   115
    "!!k:: 'a::linorder. -atLeast k = lessThan k"
paulson@13850
   116
apply (simp add: lessThan_def atLeast_def le_def, auto)
ballarin@13735
   117
done
ballarin@13735
   118
paulson@13850
   119
lemma atMost_iff [iff]: "(i: atMost k) = (i<=k)"
paulson@13850
   120
by (simp add: atMost_def)
ballarin@13735
   121
paulson@14485
   122
lemma atMost_Int_atLeast: "!!n:: 'a::order. atMost n Int atLeast n = {n}"
paulson@14485
   123
by (blast intro: order_antisym)
paulson@13850
   124
paulson@13850
   125
paulson@14485
   126
subsection {* Logical Equivalences for Set Inclusion and Equality *}
paulson@13850
   127
paulson@13850
   128
lemma atLeast_subset_iff [iff]:
paulson@15418
   129
     "(atLeast x \<subseteq> atLeast y) = (y \<le> (x::'a::order))"
paulson@15418
   130
by (blast intro: order_trans)
paulson@13850
   131
paulson@13850
   132
lemma atLeast_eq_iff [iff]:
paulson@15418
   133
     "(atLeast x = atLeast y) = (x = (y::'a::linorder))"
paulson@13850
   134
by (blast intro: order_antisym order_trans)
paulson@13850
   135
paulson@13850
   136
lemma greaterThan_subset_iff [iff]:
paulson@15418
   137
     "(greaterThan x \<subseteq> greaterThan y) = (y \<le> (x::'a::linorder))"
paulson@15418
   138
apply (auto simp add: greaterThan_def)
paulson@15418
   139
 apply (subst linorder_not_less [symmetric], blast)
paulson@13850
   140
done
paulson@13850
   141
paulson@13850
   142
lemma greaterThan_eq_iff [iff]:
paulson@15418
   143
     "(greaterThan x = greaterThan y) = (x = (y::'a::linorder))"
paulson@15418
   144
apply (rule iffI)
paulson@15418
   145
 apply (erule equalityE)
paulson@15418
   146
 apply (simp_all add: greaterThan_subset_iff)
paulson@13850
   147
done
paulson@13850
   148
paulson@15418
   149
lemma atMost_subset_iff [iff]: "(atMost x \<subseteq> atMost y) = (x \<le> (y::'a::order))"
paulson@13850
   150
by (blast intro: order_trans)
paulson@13850
   151
paulson@15418
   152
lemma atMost_eq_iff [iff]: "(atMost x = atMost y) = (x = (y::'a::linorder))"
paulson@13850
   153
by (blast intro: order_antisym order_trans)
paulson@13850
   154
paulson@13850
   155
lemma lessThan_subset_iff [iff]:
paulson@15418
   156
     "(lessThan x \<subseteq> lessThan y) = (x \<le> (y::'a::linorder))"
paulson@15418
   157
apply (auto simp add: lessThan_def)
paulson@15418
   158
 apply (subst linorder_not_less [symmetric], blast)
paulson@13850
   159
done
paulson@13850
   160
paulson@13850
   161
lemma lessThan_eq_iff [iff]:
paulson@15418
   162
     "(lessThan x = lessThan y) = (x = (y::'a::linorder))"
paulson@15418
   163
apply (rule iffI)
paulson@15418
   164
 apply (erule equalityE)
paulson@15418
   165
 apply (simp_all add: lessThan_subset_iff)
ballarin@13735
   166
done
ballarin@13735
   167
ballarin@13735
   168
paulson@13850
   169
subsection {*Two-sided intervals*}
ballarin@13735
   170
ballarin@13735
   171
lemma greaterThanLessThan_iff [simp]:
nipkow@15045
   172
  "(i : {l<..<u}) = (l < i & i < u)"
ballarin@13735
   173
by (simp add: greaterThanLessThan_def)
ballarin@13735
   174
ballarin@13735
   175
lemma atLeastLessThan_iff [simp]:
nipkow@15045
   176
  "(i : {l..<u}) = (l <= i & i < u)"
ballarin@13735
   177
by (simp add: atLeastLessThan_def)
ballarin@13735
   178
ballarin@13735
   179
lemma greaterThanAtMost_iff [simp]:
nipkow@15045
   180
  "(i : {l<..u}) = (l < i & i <= u)"
ballarin@13735
   181
by (simp add: greaterThanAtMost_def)
ballarin@13735
   182
ballarin@13735
   183
lemma atLeastAtMost_iff [simp]:
ballarin@13735
   184
  "(i : {l..u}) = (l <= i & i <= u)"
ballarin@13735
   185
by (simp add: atLeastAtMost_def)
ballarin@13735
   186
wenzelm@14577
   187
text {* The above four lemmas could be declared as iffs.
wenzelm@14577
   188
  If we do so, a call to blast in Hyperreal/Star.ML, lemma @{text STAR_Int}
wenzelm@14577
   189
  seems to take forever (more than one hour). *}
ballarin@13735
   190
nipkow@15554
   191
subsubsection{* Emptyness and singletons *}
nipkow@15554
   192
nipkow@15554
   193
lemma atLeastAtMost_empty [simp]: "n < m ==> {m::'a::order..n} = {}";
nipkow@15554
   194
  by (auto simp add: atLeastAtMost_def atMost_def atLeast_def);
nipkow@15554
   195
nipkow@15554
   196
lemma atLeastLessThan_empty[simp]: "n \<le> m ==> {m..<n::'a::order} = {}"
nipkow@15554
   197
by (auto simp add: atLeastLessThan_def)
nipkow@15554
   198
nipkow@17719
   199
lemma greaterThanAtMost_empty[simp]:"l \<le> k ==> {k<..(l::'a::order)} = {}"
nipkow@17719
   200
by(auto simp:greaterThanAtMost_def greaterThan_def atMost_def)
nipkow@17719
   201
nipkow@17719
   202
lemma greaterThanLessThan_empty[simp]:"l \<le> k ==> {k<..(l::'a::order)} = {}"
nipkow@17719
   203
by(auto simp:greaterThanLessThan_def greaterThan_def lessThan_def)
nipkow@17719
   204
nipkow@15554
   205
lemma atLeastAtMost_singleton [simp]: "{a::'a::order..a} = {a}";
nipkow@17719
   206
by (auto simp add: atLeastAtMost_def atMost_def atLeast_def);
paulson@14485
   207
paulson@14485
   208
subsection {* Intervals of natural numbers *}
paulson@14485
   209
paulson@15047
   210
subsubsection {* The Constant @{term lessThan} *}
paulson@15047
   211
paulson@14485
   212
lemma lessThan_0 [simp]: "lessThan (0::nat) = {}"
paulson@14485
   213
by (simp add: lessThan_def)
paulson@14485
   214
paulson@14485
   215
lemma lessThan_Suc: "lessThan (Suc k) = insert k (lessThan k)"
paulson@14485
   216
by (simp add: lessThan_def less_Suc_eq, blast)
paulson@14485
   217
paulson@14485
   218
lemma lessThan_Suc_atMost: "lessThan (Suc k) = atMost k"
paulson@14485
   219
by (simp add: lessThan_def atMost_def less_Suc_eq_le)
paulson@14485
   220
paulson@14485
   221
lemma UN_lessThan_UNIV: "(UN m::nat. lessThan m) = UNIV"
paulson@14485
   222
by blast
paulson@14485
   223
paulson@15047
   224
subsubsection {* The Constant @{term greaterThan} *}
paulson@15047
   225
paulson@14485
   226
lemma greaterThan_0 [simp]: "greaterThan 0 = range Suc"
paulson@14485
   227
apply (simp add: greaterThan_def)
paulson@14485
   228
apply (blast dest: gr0_conv_Suc [THEN iffD1])
paulson@14485
   229
done
paulson@14485
   230
paulson@14485
   231
lemma greaterThan_Suc: "greaterThan (Suc k) = greaterThan k - {Suc k}"
paulson@14485
   232
apply (simp add: greaterThan_def)
paulson@14485
   233
apply (auto elim: linorder_neqE)
paulson@14485
   234
done
paulson@14485
   235
paulson@14485
   236
lemma INT_greaterThan_UNIV: "(INT m::nat. greaterThan m) = {}"
paulson@14485
   237
by blast
paulson@14485
   238
paulson@15047
   239
subsubsection {* The Constant @{term atLeast} *}
paulson@15047
   240
paulson@14485
   241
lemma atLeast_0 [simp]: "atLeast (0::nat) = UNIV"
paulson@14485
   242
by (unfold atLeast_def UNIV_def, simp)
paulson@14485
   243
paulson@14485
   244
lemma atLeast_Suc: "atLeast (Suc k) = atLeast k - {k}"
paulson@14485
   245
apply (simp add: atLeast_def)
paulson@14485
   246
apply (simp add: Suc_le_eq)
paulson@14485
   247
apply (simp add: order_le_less, blast)
paulson@14485
   248
done
paulson@14485
   249
paulson@14485
   250
lemma atLeast_Suc_greaterThan: "atLeast (Suc k) = greaterThan k"
paulson@14485
   251
  by (auto simp add: greaterThan_def atLeast_def less_Suc_eq_le)
paulson@14485
   252
paulson@14485
   253
lemma UN_atLeast_UNIV: "(UN m::nat. atLeast m) = UNIV"
paulson@14485
   254
by blast
paulson@14485
   255
paulson@15047
   256
subsubsection {* The Constant @{term atMost} *}
paulson@15047
   257
paulson@14485
   258
lemma atMost_0 [simp]: "atMost (0::nat) = {0}"
paulson@14485
   259
by (simp add: atMost_def)
paulson@14485
   260
paulson@14485
   261
lemma atMost_Suc: "atMost (Suc k) = insert (Suc k) (atMost k)"
paulson@14485
   262
apply (simp add: atMost_def)
paulson@14485
   263
apply (simp add: less_Suc_eq order_le_less, blast)
paulson@14485
   264
done
paulson@14485
   265
paulson@14485
   266
lemma UN_atMost_UNIV: "(UN m::nat. atMost m) = UNIV"
paulson@14485
   267
by blast
paulson@14485
   268
paulson@15047
   269
subsubsection {* The Constant @{term atLeastLessThan} *}
paulson@15047
   270
paulson@15047
   271
text{*But not a simprule because some concepts are better left in terms
paulson@15047
   272
  of @{term atLeastLessThan}*}
paulson@15047
   273
lemma atLeast0LessThan: "{0::nat..<n} = {..<n}"
nipkow@15042
   274
by(simp add:lessThan_def atLeastLessThan_def)
nipkow@16041
   275
(*
paulson@15047
   276
lemma atLeastLessThan0 [simp]: "{m..<0::nat} = {}"
paulson@15047
   277
by (simp add: atLeastLessThan_def)
nipkow@16041
   278
*)
paulson@15047
   279
subsubsection {* Intervals of nats with @{term Suc} *}
paulson@15047
   280
paulson@15047
   281
text{*Not a simprule because the RHS is too messy.*}
paulson@15047
   282
lemma atLeastLessThanSuc:
paulson@15047
   283
    "{m..<Suc n} = (if m \<le> n then insert n {m..<n} else {})"
paulson@15418
   284
by (auto simp add: atLeastLessThan_def)
paulson@15047
   285
paulson@15418
   286
lemma atLeastLessThan_singleton [simp]: "{m..<Suc m} = {m}"
paulson@15047
   287
by (auto simp add: atLeastLessThan_def)
nipkow@16041
   288
(*
paulson@15047
   289
lemma atLeast_sum_LessThan [simp]: "{m + k..<k::nat} = {}"
paulson@15047
   290
by (induct k, simp_all add: atLeastLessThanSuc)
paulson@15047
   291
paulson@15047
   292
lemma atLeastSucLessThan [simp]: "{Suc n..<n} = {}"
paulson@15047
   293
by (auto simp add: atLeastLessThan_def)
nipkow@16041
   294
*)
nipkow@15045
   295
lemma atLeastLessThanSuc_atLeastAtMost: "{l..<Suc u} = {l..u}"
paulson@14485
   296
  by (simp add: lessThan_Suc_atMost atLeastAtMost_def atLeastLessThan_def)
paulson@14485
   297
paulson@15418
   298
lemma atLeastSucAtMost_greaterThanAtMost: "{Suc l..u} = {l<..u}"
paulson@15418
   299
  by (simp add: atLeast_Suc_greaterThan atLeastAtMost_def
paulson@14485
   300
    greaterThanAtMost_def)
paulson@14485
   301
paulson@15418
   302
lemma atLeastSucLessThan_greaterThanLessThan: "{Suc l..<u} = {l<..<u}"
paulson@15418
   303
  by (simp add: atLeast_Suc_greaterThan atLeastLessThan_def
paulson@14485
   304
    greaterThanLessThan_def)
paulson@14485
   305
nipkow@15554
   306
lemma atLeastAtMostSuc_conv: "m \<le> Suc n \<Longrightarrow> {m..Suc n} = insert (Suc n) {m..n}"
nipkow@15554
   307
by (auto simp add: atLeastAtMost_def)
nipkow@15554
   308
nipkow@16733
   309
subsubsection {* Image *}
nipkow@16733
   310
nipkow@16733
   311
lemma image_add_atLeastAtMost:
nipkow@16733
   312
  "(%n::nat. n+k) ` {i..j} = {i+k..j+k}" (is "?A = ?B")
nipkow@16733
   313
proof
nipkow@16733
   314
  show "?A \<subseteq> ?B" by auto
nipkow@16733
   315
next
nipkow@16733
   316
  show "?B \<subseteq> ?A"
nipkow@16733
   317
  proof
nipkow@16733
   318
    fix n assume a: "n : ?B"
nipkow@16733
   319
    hence "n - k : {i..j}" by auto arith+
nipkow@16733
   320
    moreover have "n = (n - k) + k" using a by auto
nipkow@16733
   321
    ultimately show "n : ?A" by blast
nipkow@16733
   322
  qed
nipkow@16733
   323
qed
nipkow@16733
   324
nipkow@16733
   325
lemma image_add_atLeastLessThan:
nipkow@16733
   326
  "(%n::nat. n+k) ` {i..<j} = {i+k..<j+k}" (is "?A = ?B")
nipkow@16733
   327
proof
nipkow@16733
   328
  show "?A \<subseteq> ?B" by auto
nipkow@16733
   329
next
nipkow@16733
   330
  show "?B \<subseteq> ?A"
nipkow@16733
   331
  proof
nipkow@16733
   332
    fix n assume a: "n : ?B"
nipkow@16733
   333
    hence "n - k : {i..<j}" by auto arith+
nipkow@16733
   334
    moreover have "n = (n - k) + k" using a by auto
nipkow@16733
   335
    ultimately show "n : ?A" by blast
nipkow@16733
   336
  qed
nipkow@16733
   337
qed
nipkow@16733
   338
nipkow@16733
   339
corollary image_Suc_atLeastAtMost[simp]:
nipkow@16733
   340
  "Suc ` {i..j} = {Suc i..Suc j}"
nipkow@16733
   341
using image_add_atLeastAtMost[where k=1] by simp
nipkow@16733
   342
nipkow@16733
   343
corollary image_Suc_atLeastLessThan[simp]:
nipkow@16733
   344
  "Suc ` {i..<j} = {Suc i..<Suc j}"
nipkow@16733
   345
using image_add_atLeastLessThan[where k=1] by simp
nipkow@16733
   346
nipkow@16733
   347
lemma image_add_int_atLeastLessThan:
nipkow@16733
   348
    "(%x. x + (l::int)) ` {0..<u-l} = {l..<u}"
nipkow@16733
   349
  apply (auto simp add: image_def)
nipkow@16733
   350
  apply (rule_tac x = "x - l" in bexI)
nipkow@16733
   351
  apply auto
nipkow@16733
   352
  done
nipkow@16733
   353
nipkow@16733
   354
paulson@14485
   355
subsubsection {* Finiteness *}
paulson@14485
   356
nipkow@15045
   357
lemma finite_lessThan [iff]: fixes k :: nat shows "finite {..<k}"
paulson@14485
   358
  by (induct k) (simp_all add: lessThan_Suc)
paulson@14485
   359
paulson@14485
   360
lemma finite_atMost [iff]: fixes k :: nat shows "finite {..k}"
paulson@14485
   361
  by (induct k) (simp_all add: atMost_Suc)
paulson@14485
   362
paulson@14485
   363
lemma finite_greaterThanLessThan [iff]:
nipkow@15045
   364
  fixes l :: nat shows "finite {l<..<u}"
paulson@14485
   365
by (simp add: greaterThanLessThan_def)
paulson@14485
   366
paulson@14485
   367
lemma finite_atLeastLessThan [iff]:
nipkow@15045
   368
  fixes l :: nat shows "finite {l..<u}"
paulson@14485
   369
by (simp add: atLeastLessThan_def)
paulson@14485
   370
paulson@14485
   371
lemma finite_greaterThanAtMost [iff]:
nipkow@15045
   372
  fixes l :: nat shows "finite {l<..u}"
paulson@14485
   373
by (simp add: greaterThanAtMost_def)
paulson@14485
   374
paulson@14485
   375
lemma finite_atLeastAtMost [iff]:
paulson@14485
   376
  fixes l :: nat shows "finite {l..u}"
paulson@14485
   377
by (simp add: atLeastAtMost_def)
paulson@14485
   378
paulson@14485
   379
lemma bounded_nat_set_is_finite:
paulson@14485
   380
    "(ALL i:N. i < (n::nat)) ==> finite N"
paulson@14485
   381
  -- {* A bounded set of natural numbers is finite. *}
paulson@14485
   382
  apply (rule finite_subset)
paulson@14485
   383
   apply (rule_tac [2] finite_lessThan, auto)
paulson@14485
   384
  done
paulson@14485
   385
paulson@14485
   386
subsubsection {* Cardinality *}
paulson@14485
   387
nipkow@15045
   388
lemma card_lessThan [simp]: "card {..<u} = u"
paulson@15251
   389
  by (induct u, simp_all add: lessThan_Suc)
paulson@14485
   390
paulson@14485
   391
lemma card_atMost [simp]: "card {..u} = Suc u"
paulson@14485
   392
  by (simp add: lessThan_Suc_atMost [THEN sym])
paulson@14485
   393
nipkow@15045
   394
lemma card_atLeastLessThan [simp]: "card {l..<u} = u - l"
nipkow@15045
   395
  apply (subgoal_tac "card {l..<u} = card {..<u-l}")
paulson@14485
   396
  apply (erule ssubst, rule card_lessThan)
nipkow@15045
   397
  apply (subgoal_tac "(%x. x + l) ` {..<u-l} = {l..<u}")
paulson@14485
   398
  apply (erule subst)
paulson@14485
   399
  apply (rule card_image)
paulson@14485
   400
  apply (simp add: inj_on_def)
paulson@14485
   401
  apply (auto simp add: image_def atLeastLessThan_def lessThan_def)
paulson@14485
   402
  apply arith
paulson@14485
   403
  apply (rule_tac x = "x - l" in exI)
paulson@14485
   404
  apply arith
paulson@14485
   405
  done
paulson@14485
   406
paulson@15418
   407
lemma card_atLeastAtMost [simp]: "card {l..u} = Suc u - l"
paulson@14485
   408
  by (subst atLeastLessThanSuc_atLeastAtMost [THEN sym], simp)
paulson@14485
   409
paulson@15418
   410
lemma card_greaterThanAtMost [simp]: "card {l<..u} = u - l"
paulson@14485
   411
  by (subst atLeastSucAtMost_greaterThanAtMost [THEN sym], simp)
paulson@14485
   412
nipkow@15045
   413
lemma card_greaterThanLessThan [simp]: "card {l<..<u} = u - Suc l"
paulson@14485
   414
  by (subst atLeastSucLessThan_greaterThanLessThan [THEN sym], simp)
paulson@14485
   415
paulson@14485
   416
subsection {* Intervals of integers *}
paulson@14485
   417
nipkow@15045
   418
lemma atLeastLessThanPlusOne_atLeastAtMost_int: "{l..<u+1} = {l..(u::int)}"
paulson@14485
   419
  by (auto simp add: atLeastAtMost_def atLeastLessThan_def)
paulson@14485
   420
paulson@15418
   421
lemma atLeastPlusOneAtMost_greaterThanAtMost_int: "{l+1..u} = {l<..(u::int)}"
paulson@14485
   422
  by (auto simp add: atLeastAtMost_def greaterThanAtMost_def)
paulson@14485
   423
paulson@15418
   424
lemma atLeastPlusOneLessThan_greaterThanLessThan_int:
paulson@15418
   425
    "{l+1..<u} = {l<..<u::int}"
paulson@14485
   426
  by (auto simp add: atLeastLessThan_def greaterThanLessThan_def)
paulson@14485
   427
paulson@14485
   428
subsubsection {* Finiteness *}
paulson@14485
   429
paulson@15418
   430
lemma image_atLeastZeroLessThan_int: "0 \<le> u ==>
nipkow@15045
   431
    {(0::int)..<u} = int ` {..<nat u}"
paulson@14485
   432
  apply (unfold image_def lessThan_def)
paulson@14485
   433
  apply auto
paulson@14485
   434
  apply (rule_tac x = "nat x" in exI)
paulson@14485
   435
  apply (auto simp add: zless_nat_conj zless_nat_eq_int_zless [THEN sym])
paulson@14485
   436
  done
paulson@14485
   437
nipkow@15045
   438
lemma finite_atLeastZeroLessThan_int: "finite {(0::int)..<u}"
paulson@14485
   439
  apply (case_tac "0 \<le> u")
paulson@14485
   440
  apply (subst image_atLeastZeroLessThan_int, assumption)
paulson@14485
   441
  apply (rule finite_imageI)
paulson@14485
   442
  apply auto
paulson@14485
   443
  done
paulson@14485
   444
nipkow@15045
   445
lemma finite_atLeastLessThan_int [iff]: "finite {l..<u::int}"
nipkow@15045
   446
  apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
paulson@14485
   447
  apply (erule subst)
paulson@14485
   448
  apply (rule finite_imageI)
paulson@14485
   449
  apply (rule finite_atLeastZeroLessThan_int)
nipkow@16733
   450
  apply (rule image_add_int_atLeastLessThan)
paulson@14485
   451
  done
paulson@14485
   452
paulson@15418
   453
lemma finite_atLeastAtMost_int [iff]: "finite {l..(u::int)}"
paulson@14485
   454
  by (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym], simp)
paulson@14485
   455
paulson@15418
   456
lemma finite_greaterThanAtMost_int [iff]: "finite {l<..(u::int)}"
paulson@14485
   457
  by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
paulson@14485
   458
paulson@15418
   459
lemma finite_greaterThanLessThan_int [iff]: "finite {l<..<u::int}"
paulson@14485
   460
  by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
paulson@14485
   461
paulson@14485
   462
subsubsection {* Cardinality *}
paulson@14485
   463
nipkow@15045
   464
lemma card_atLeastZeroLessThan_int: "card {(0::int)..<u} = nat u"
paulson@14485
   465
  apply (case_tac "0 \<le> u")
paulson@14485
   466
  apply (subst image_atLeastZeroLessThan_int, assumption)
paulson@14485
   467
  apply (subst card_image)
paulson@14485
   468
  apply (auto simp add: inj_on_def)
paulson@14485
   469
  done
paulson@14485
   470
nipkow@15045
   471
lemma card_atLeastLessThan_int [simp]: "card {l..<u} = nat (u - l)"
nipkow@15045
   472
  apply (subgoal_tac "card {l..<u} = card {0..<u-l}")
paulson@14485
   473
  apply (erule ssubst, rule card_atLeastZeroLessThan_int)
nipkow@15045
   474
  apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
paulson@14485
   475
  apply (erule subst)
paulson@14485
   476
  apply (rule card_image)
paulson@14485
   477
  apply (simp add: inj_on_def)
nipkow@16733
   478
  apply (rule image_add_int_atLeastLessThan)
paulson@14485
   479
  done
paulson@14485
   480
paulson@14485
   481
lemma card_atLeastAtMost_int [simp]: "card {l..u} = nat (u - l + 1)"
paulson@14485
   482
  apply (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym])
paulson@14485
   483
  apply (auto simp add: compare_rls)
paulson@14485
   484
  done
paulson@14485
   485
paulson@15418
   486
lemma card_greaterThanAtMost_int [simp]: "card {l<..u} = nat (u - l)"
paulson@14485
   487
  by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
paulson@14485
   488
nipkow@15045
   489
lemma card_greaterThanLessThan_int [simp]: "card {l<..<u} = nat (u - (l + 1))"
paulson@14485
   490
  by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
paulson@14485
   491
paulson@14485
   492
paulson@13850
   493
subsection {*Lemmas useful with the summation operator setsum*}
paulson@13850
   494
ballarin@16102
   495
text {* For examples, see Algebra/poly/UnivPoly2.thy *}
ballarin@13735
   496
wenzelm@14577
   497
subsubsection {* Disjoint Unions *}
ballarin@13735
   498
wenzelm@14577
   499
text {* Singletons and open intervals *}
ballarin@13735
   500
ballarin@13735
   501
lemma ivl_disj_un_singleton:
nipkow@15045
   502
  "{l::'a::linorder} Un {l<..} = {l..}"
nipkow@15045
   503
  "{..<u} Un {u::'a::linorder} = {..u}"
nipkow@15045
   504
  "(l::'a::linorder) < u ==> {l} Un {l<..<u} = {l..<u}"
nipkow@15045
   505
  "(l::'a::linorder) < u ==> {l<..<u} Un {u} = {l<..u}"
nipkow@15045
   506
  "(l::'a::linorder) <= u ==> {l} Un {l<..u} = {l..u}"
nipkow@15045
   507
  "(l::'a::linorder) <= u ==> {l..<u} Un {u} = {l..u}"
ballarin@14398
   508
by auto
ballarin@13735
   509
wenzelm@14577
   510
text {* One- and two-sided intervals *}
ballarin@13735
   511
ballarin@13735
   512
lemma ivl_disj_un_one:
nipkow@15045
   513
  "(l::'a::linorder) < u ==> {..l} Un {l<..<u} = {..<u}"
nipkow@15045
   514
  "(l::'a::linorder) <= u ==> {..<l} Un {l..<u} = {..<u}"
nipkow@15045
   515
  "(l::'a::linorder) <= u ==> {..l} Un {l<..u} = {..u}"
nipkow@15045
   516
  "(l::'a::linorder) <= u ==> {..<l} Un {l..u} = {..u}"
nipkow@15045
   517
  "(l::'a::linorder) <= u ==> {l<..u} Un {u<..} = {l<..}"
nipkow@15045
   518
  "(l::'a::linorder) < u ==> {l<..<u} Un {u..} = {l<..}"
nipkow@15045
   519
  "(l::'a::linorder) <= u ==> {l..u} Un {u<..} = {l..}"
nipkow@15045
   520
  "(l::'a::linorder) <= u ==> {l..<u} Un {u..} = {l..}"
ballarin@14398
   521
by auto
ballarin@13735
   522
wenzelm@14577
   523
text {* Two- and two-sided intervals *}
ballarin@13735
   524
ballarin@13735
   525
lemma ivl_disj_un_two:
nipkow@15045
   526
  "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..<u} = {l<..<u}"
nipkow@15045
   527
  "[| (l::'a::linorder) <= m; m < u |] ==> {l<..m} Un {m<..<u} = {l<..<u}"
nipkow@15045
   528
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..<u} = {l..<u}"
nipkow@15045
   529
  "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m<..<u} = {l..<u}"
nipkow@15045
   530
  "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..u} = {l<..u}"
nipkow@15045
   531
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l<..m} Un {m<..u} = {l<..u}"
nipkow@15045
   532
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..u} = {l..u}"
nipkow@15045
   533
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m<..u} = {l..u}"
ballarin@14398
   534
by auto
ballarin@13735
   535
ballarin@13735
   536
lemmas ivl_disj_un = ivl_disj_un_singleton ivl_disj_un_one ivl_disj_un_two
ballarin@13735
   537
wenzelm@14577
   538
subsubsection {* Disjoint Intersections *}
ballarin@13735
   539
wenzelm@14577
   540
text {* Singletons and open intervals *}
ballarin@13735
   541
ballarin@13735
   542
lemma ivl_disj_int_singleton:
nipkow@15045
   543
  "{l::'a::order} Int {l<..} = {}"
nipkow@15045
   544
  "{..<u} Int {u} = {}"
nipkow@15045
   545
  "{l} Int {l<..<u} = {}"
nipkow@15045
   546
  "{l<..<u} Int {u} = {}"
nipkow@15045
   547
  "{l} Int {l<..u} = {}"
nipkow@15045
   548
  "{l..<u} Int {u} = {}"
ballarin@13735
   549
  by simp+
ballarin@13735
   550
wenzelm@14577
   551
text {* One- and two-sided intervals *}
ballarin@13735
   552
ballarin@13735
   553
lemma ivl_disj_int_one:
nipkow@15045
   554
  "{..l::'a::order} Int {l<..<u} = {}"
nipkow@15045
   555
  "{..<l} Int {l..<u} = {}"
nipkow@15045
   556
  "{..l} Int {l<..u} = {}"
nipkow@15045
   557
  "{..<l} Int {l..u} = {}"
nipkow@15045
   558
  "{l<..u} Int {u<..} = {}"
nipkow@15045
   559
  "{l<..<u} Int {u..} = {}"
nipkow@15045
   560
  "{l..u} Int {u<..} = {}"
nipkow@15045
   561
  "{l..<u} Int {u..} = {}"
ballarin@14398
   562
  by auto
ballarin@13735
   563
wenzelm@14577
   564
text {* Two- and two-sided intervals *}
ballarin@13735
   565
ballarin@13735
   566
lemma ivl_disj_int_two:
nipkow@15045
   567
  "{l::'a::order<..<m} Int {m..<u} = {}"
nipkow@15045
   568
  "{l<..m} Int {m<..<u} = {}"
nipkow@15045
   569
  "{l..<m} Int {m..<u} = {}"
nipkow@15045
   570
  "{l..m} Int {m<..<u} = {}"
nipkow@15045
   571
  "{l<..<m} Int {m..u} = {}"
nipkow@15045
   572
  "{l<..m} Int {m<..u} = {}"
nipkow@15045
   573
  "{l..<m} Int {m..u} = {}"
nipkow@15045
   574
  "{l..m} Int {m<..u} = {}"
ballarin@14398
   575
  by auto
ballarin@13735
   576
ballarin@13735
   577
lemmas ivl_disj_int = ivl_disj_int_singleton ivl_disj_int_one ivl_disj_int_two
ballarin@13735
   578
nipkow@15542
   579
subsubsection {* Some Differences *}
nipkow@15542
   580
nipkow@15542
   581
lemma ivl_diff[simp]:
nipkow@15542
   582
 "i \<le> n \<Longrightarrow> {i..<m} - {i..<n} = {n..<(m::'a::linorder)}"
nipkow@15542
   583
by(auto)
nipkow@15542
   584
nipkow@15542
   585
nipkow@15542
   586
subsubsection {* Some Subset Conditions *}
nipkow@15542
   587
nipkow@15542
   588
lemma ivl_subset[simp]:
nipkow@15542
   589
 "({i..<j} \<subseteq> {m..<n}) = (j \<le> i | m \<le> i & j \<le> (n::'a::linorder))"
nipkow@15542
   590
apply(auto simp:linorder_not_le)
nipkow@15542
   591
apply(rule ccontr)
nipkow@15542
   592
apply(insert linorder_le_less_linear[of i n])
nipkow@15542
   593
apply(clarsimp simp:linorder_not_le)
nipkow@15542
   594
apply(fastsimp)
nipkow@15542
   595
done
nipkow@15542
   596
nipkow@15041
   597
nipkow@15042
   598
subsection {* Summation indexed over intervals *}
nipkow@15042
   599
nipkow@15042
   600
syntax
nipkow@15042
   601
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048
   602
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _..<_./ _)" [0,0,0,10] 10)
nipkow@16052
   603
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<_./ _)" [0,0,10] 10)
nipkow@16052
   604
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<=_./ _)" [0,0,10] 10)
nipkow@15042
   605
syntax (xsymbols)
nipkow@15042
   606
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048
   607
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
nipkow@16052
   608
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
nipkow@16052
   609
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
nipkow@15042
   610
syntax (HTML output)
nipkow@15042
   611
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048
   612
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
nipkow@16052
   613
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
nipkow@16052
   614
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
nipkow@15056
   615
syntax (latex_sum output)
nipkow@15052
   616
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@15052
   617
 ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
nipkow@15052
   618
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@15052
   619
 ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
nipkow@16052
   620
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@16052
   621
 ("(3\<^raw:$\sum_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
nipkow@15052
   622
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@16052
   623
 ("(3\<^raw:$\sum_{>_ \<le> _\<^raw:}$> _)" [0,0,10] 10)
nipkow@15041
   624
nipkow@15048
   625
translations
nipkow@15048
   626
  "\<Sum>x=a..b. t" == "setsum (%x. t) {a..b}"
nipkow@15048
   627
  "\<Sum>x=a..<b. t" == "setsum (%x. t) {a..<b}"
nipkow@16052
   628
  "\<Sum>i\<le>n. t" == "setsum (\<lambda>i. t) {..n}"
nipkow@15048
   629
  "\<Sum>i<n. t" == "setsum (\<lambda>i. t) {..<n}"
nipkow@15041
   630
nipkow@15052
   631
text{* The above introduces some pretty alternative syntaxes for
nipkow@15056
   632
summation over intervals:
nipkow@15052
   633
\begin{center}
nipkow@15052
   634
\begin{tabular}{lll}
nipkow@15056
   635
Old & New & \LaTeX\\
nipkow@15056
   636
@{term[source]"\<Sum>x\<in>{a..b}. e"} & @{term"\<Sum>x=a..b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..b. e"}\\
nipkow@15056
   637
@{term[source]"\<Sum>x\<in>{a..<b}. e"} & @{term"\<Sum>x=a..<b. e"} & @{term[mode=latex_sum]"\<Sum>x=a..<b. e"}\\
nipkow@16052
   638
@{term[source]"\<Sum>x\<in>{..b}. e"} & @{term"\<Sum>x\<le>b. e"} & @{term[mode=latex_sum]"\<Sum>x\<le>b. e"}\\
nipkow@15056
   639
@{term[source]"\<Sum>x\<in>{..<b}. e"} & @{term"\<Sum>x<b. e"} & @{term[mode=latex_sum]"\<Sum>x<b. e"}
nipkow@15052
   640
\end{tabular}
nipkow@15052
   641
\end{center}
nipkow@15056
   642
The left column shows the term before introduction of the new syntax,
nipkow@15056
   643
the middle column shows the new (default) syntax, and the right column
nipkow@15056
   644
shows a special syntax. The latter is only meaningful for latex output
nipkow@15056
   645
and has to be activated explicitly by setting the print mode to
nipkow@15056
   646
\texttt{latex\_sum} (e.g.\ via \texttt{mode=latex\_sum} in
nipkow@15056
   647
antiquotations). It is not the default \LaTeX\ output because it only
nipkow@15056
   648
works well with italic-style formulae, not tt-style.
nipkow@15052
   649
nipkow@15052
   650
Note that for uniformity on @{typ nat} it is better to use
nipkow@15052
   651
@{term"\<Sum>x::nat=0..<n. e"} rather than @{text"\<Sum>x<n. e"}: @{text setsum} may
nipkow@15052
   652
not provide all lemmas available for @{term"{m..<n}"} also in the
nipkow@15052
   653
special form for @{term"{..<n}"}. *}
nipkow@15052
   654
nipkow@15542
   655
text{* This congruence rule should be used for sums over intervals as
nipkow@15542
   656
the standard theorem @{text[source]setsum_cong} does not work well
nipkow@15542
   657
with the simplifier who adds the unsimplified premise @{term"x:B"} to
nipkow@15542
   658
the context. *}
nipkow@15542
   659
nipkow@15542
   660
lemma setsum_ivl_cong:
nipkow@15542
   661
 "\<lbrakk>a = c; b = d; !!x. \<lbrakk> c \<le> x; x < d \<rbrakk> \<Longrightarrow> f x = g x \<rbrakk> \<Longrightarrow>
nipkow@15542
   662
 setsum f {a..<b} = setsum g {c..<d}"
nipkow@15542
   663
by(rule setsum_cong, simp_all)
nipkow@15041
   664
nipkow@16041
   665
(* FIXME why are the following simp rules but the corresponding eqns
nipkow@16041
   666
on intervals are not? *)
nipkow@16041
   667
nipkow@16052
   668
lemma setsum_atMost_Suc[simp]: "(\<Sum>i \<le> Suc n. f i) = (\<Sum>i \<le> n. f i) + f(Suc n)"
nipkow@16052
   669
by (simp add:atMost_Suc add_ac)
nipkow@16052
   670
nipkow@16041
   671
lemma setsum_lessThan_Suc[simp]: "(\<Sum>i < Suc n. f i) = (\<Sum>i < n. f i) + f n"
nipkow@16041
   672
by (simp add:lessThan_Suc add_ac)
nipkow@15041
   673
nipkow@15911
   674
lemma setsum_cl_ivl_Suc[simp]:
nipkow@15561
   675
  "setsum f {m..Suc n} = (if Suc n < m then 0 else setsum f {m..n} + f(Suc n))"
nipkow@15561
   676
by (auto simp:add_ac atLeastAtMostSuc_conv)
nipkow@15561
   677
nipkow@15911
   678
lemma setsum_op_ivl_Suc[simp]:
nipkow@15561
   679
  "setsum f {m..<Suc n} = (if n < m then 0 else setsum f {m..<n} + f(n))"
nipkow@15561
   680
by (auto simp:add_ac atLeastLessThanSuc)
nipkow@16041
   681
(*
nipkow@15561
   682
lemma setsum_cl_ivl_add_one_nat: "(n::nat) <= m + 1 ==>
nipkow@15561
   683
    (\<Sum>i=n..m+1. f i) = (\<Sum>i=n..m. f i) + f(m + 1)"
nipkow@15561
   684
by (auto simp:add_ac atLeastAtMostSuc_conv)
nipkow@16041
   685
*)
nipkow@15539
   686
lemma setsum_add_nat_ivl: "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
nipkow@15539
   687
  setsum f {m..<n} + setsum f {n..<p} = setsum f {m..<p::nat}"
nipkow@15539
   688
by (simp add:setsum_Un_disjoint[symmetric] ivl_disj_int ivl_disj_un)
nipkow@15539
   689
nipkow@15539
   690
lemma setsum_diff_nat_ivl:
nipkow@15539
   691
fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
nipkow@15539
   692
shows "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
nipkow@15539
   693
  setsum f {m..<p} - setsum f {m..<n} = setsum f {n..<p}"
nipkow@15539
   694
using setsum_add_nat_ivl [of m n p f,symmetric]
nipkow@15539
   695
apply (simp add: add_ac)
nipkow@15539
   696
done
nipkow@15539
   697
nipkow@16733
   698
subsection{* Shifting bounds *}
nipkow@16733
   699
nipkow@15539
   700
lemma setsum_shift_bounds_nat_ivl:
nipkow@15539
   701
  "setsum f {m+k..<n+k} = setsum (%i. f(i + k)){m..<n::nat}"
nipkow@15539
   702
by (induct "n", auto simp:atLeastLessThanSuc)
nipkow@15539
   703
nipkow@16733
   704
lemma setsum_shift_bounds_cl_nat_ivl:
nipkow@16733
   705
  "setsum f {m+k..n+k} = setsum (%i. f(i + k)){m..n::nat}"
nipkow@16733
   706
apply (insert setsum_reindex[OF inj_on_add_nat, where h=f and B = "{m..n}"])
nipkow@16733
   707
apply (simp add:image_add_atLeastAtMost o_def)
nipkow@16733
   708
done
nipkow@16733
   709
nipkow@16733
   710
corollary setsum_shift_bounds_cl_Suc_ivl:
nipkow@16733
   711
  "setsum f {Suc m..Suc n} = setsum (%i. f(Suc i)){m..n}"
nipkow@16733
   712
by (simp add:setsum_shift_bounds_cl_nat_ivl[where k=1,simplified])
nipkow@16733
   713
nipkow@16733
   714
corollary setsum_shift_bounds_Suc_ivl:
nipkow@16733
   715
  "setsum f {Suc m..<Suc n} = setsum (%i. f(Suc i)){m..<n}"
nipkow@16733
   716
by (simp add:setsum_shift_bounds_nat_ivl[where k=1,simplified])
nipkow@16733
   717
kleing@19106
   718
lemma setsum_head:
kleing@19106
   719
  fixes n :: nat
kleing@19106
   720
  assumes mn: "m <= n" 
kleing@19106
   721
  shows "(\<Sum>x\<in>{m..n}. P x) = P m + (\<Sum>x\<in>{m<..n}. P x)" (is "?lhs = ?rhs")
kleing@19106
   722
proof -
kleing@19106
   723
  from mn
kleing@19106
   724
  have "{m..n} = {m} \<union> {m<..n}"
kleing@19106
   725
    by (auto intro: ivl_disj_un_singleton)
kleing@19106
   726
  hence "?lhs = (\<Sum>x\<in>{m} \<union> {m<..n}. P x)"
kleing@19106
   727
    by (simp add: atLeast0LessThan)
kleing@19106
   728
  also have "\<dots> = ?rhs" by simp
kleing@19106
   729
  finally show ?thesis .
kleing@19106
   730
qed
kleing@19106
   731
kleing@19106
   732
lemma setsum_head_upt:
kleing@19022
   733
  fixes m::nat
kleing@19022
   734
  assumes m: "0 < m"
kleing@19106
   735
  shows "(\<Sum>x<m. P x) = P 0 + (\<Sum>x\<in>{1..<m}. P x)"
kleing@19022
   736
proof -
kleing@19106
   737
  have "(\<Sum>x<m. P x) = (\<Sum>x\<in>{0..<m}. P x)" 
kleing@19022
   738
    by (simp add: atLeast0LessThan)
kleing@19106
   739
  also 
kleing@19106
   740
  from m 
kleing@19106
   741
  have "\<dots> = (\<Sum>x\<in>{0..m - 1}. P x)"
kleing@19106
   742
    by (cases m) (auto simp add: atLeastLessThanSuc_atLeastAtMost)
kleing@19106
   743
  also
kleing@19106
   744
  have "\<dots> = P 0 + (\<Sum>x\<in>{0<..m - 1}. P x)"
kleing@19106
   745
    by (simp add: setsum_head)
kleing@19106
   746
  also 
kleing@19106
   747
  from m 
kleing@19106
   748
  have "{0<..m - 1} = {1..<m}" 
kleing@19106
   749
    by (cases m) (auto simp add: atLeastLessThanSuc_atLeastAtMost)
kleing@19106
   750
  finally show ?thesis .
kleing@19022
   751
qed
kleing@19022
   752
ballarin@17149
   753
subsection {* The formula for geometric sums *}
ballarin@17149
   754
ballarin@17149
   755
lemma geometric_sum:
ballarin@17149
   756
  "x ~= 1 ==> (\<Sum>i=0..<n. x ^ i) =
ballarin@17149
   757
  (x ^ n - 1) / (x - 1::'a::{field, recpower, division_by_zero})"
ballarin@17149
   758
  apply (induct "n", auto)
ballarin@17149
   759
  apply (rule_tac c = "x - 1" in field_mult_cancel_right_lemma)
ballarin@17149
   760
  apply (auto simp add: mult_assoc left_distrib)
ballarin@17149
   761
  apply (simp add: right_distrib diff_minus mult_commute power_Suc)
ballarin@17149
   762
  done
ballarin@17149
   763
ballarin@17149
   764
paulson@15418
   765
kleing@19022
   766
lemma sum_diff_distrib:
kleing@19022
   767
  fixes P::"nat\<Rightarrow>nat"
kleing@19022
   768
  shows
kleing@19022
   769
  "\<forall>x. Q x \<le> P x  \<Longrightarrow>
kleing@19022
   770
  (\<Sum>x<n. P x) - (\<Sum>x<n. Q x) = (\<Sum>x<n. P x - Q x)"
kleing@19022
   771
proof (induct n)
kleing@19022
   772
  case 0 show ?case by simp
kleing@19022
   773
next
kleing@19022
   774
  case (Suc n)
kleing@19022
   775
kleing@19022
   776
  let ?lhs = "(\<Sum>x<n. P x) - (\<Sum>x<n. Q x)"
kleing@19022
   777
  let ?rhs = "\<Sum>x<n. P x - Q x"
kleing@19022
   778
kleing@19022
   779
  from Suc have "?lhs = ?rhs" by simp
kleing@19022
   780
  moreover
kleing@19022
   781
  from Suc have "?lhs + P n - Q n = ?rhs + (P n - Q n)" by simp
kleing@19022
   782
  moreover
kleing@19022
   783
  from Suc have
kleing@19022
   784
    "(\<Sum>x<n. P x) + P n - ((\<Sum>x<n. Q x) + Q n) = ?rhs + (P n - Q n)"
kleing@19022
   785
    by (subst diff_diff_left[symmetric],
kleing@19022
   786
        subst diff_add_assoc2)
kleing@19022
   787
       (auto simp: diff_add_assoc2 intro: setsum_mono)
kleing@19022
   788
  ultimately
kleing@19022
   789
  show ?case by simp
kleing@19022
   790
qed
kleing@19022
   791
kleing@19022
   792
paulson@15418
   793
ML
paulson@15418
   794
{*
paulson@15418
   795
val Compl_atLeast = thm "Compl_atLeast";
paulson@15418
   796
val Compl_atMost = thm "Compl_atMost";
paulson@15418
   797
val Compl_greaterThan = thm "Compl_greaterThan";
paulson@15418
   798
val Compl_lessThan = thm "Compl_lessThan";
paulson@15418
   799
val INT_greaterThan_UNIV = thm "INT_greaterThan_UNIV";
paulson@15418
   800
val UN_atLeast_UNIV = thm "UN_atLeast_UNIV";
paulson@15418
   801
val UN_atMost_UNIV = thm "UN_atMost_UNIV";
paulson@15418
   802
val UN_lessThan_UNIV = thm "UN_lessThan_UNIV";
paulson@15418
   803
val atLeastAtMost_def = thm "atLeastAtMost_def";
paulson@15418
   804
val atLeastAtMost_iff = thm "atLeastAtMost_iff";
paulson@15418
   805
val atLeastLessThan_def  = thm "atLeastLessThan_def";
paulson@15418
   806
val atLeastLessThan_iff = thm "atLeastLessThan_iff";
paulson@15418
   807
val atLeast_0 = thm "atLeast_0";
paulson@15418
   808
val atLeast_Suc = thm "atLeast_Suc";
paulson@15418
   809
val atLeast_def      = thm "atLeast_def";
paulson@15418
   810
val atLeast_iff = thm "atLeast_iff";
paulson@15418
   811
val atMost_0 = thm "atMost_0";
paulson@15418
   812
val atMost_Int_atLeast = thm "atMost_Int_atLeast";
paulson@15418
   813
val atMost_Suc = thm "atMost_Suc";
paulson@15418
   814
val atMost_def       = thm "atMost_def";
paulson@15418
   815
val atMost_iff = thm "atMost_iff";
paulson@15418
   816
val greaterThanAtMost_def  = thm "greaterThanAtMost_def";
paulson@15418
   817
val greaterThanAtMost_iff = thm "greaterThanAtMost_iff";
paulson@15418
   818
val greaterThanLessThan_def  = thm "greaterThanLessThan_def";
paulson@15418
   819
val greaterThanLessThan_iff = thm "greaterThanLessThan_iff";
paulson@15418
   820
val greaterThan_0 = thm "greaterThan_0";
paulson@15418
   821
val greaterThan_Suc = thm "greaterThan_Suc";
paulson@15418
   822
val greaterThan_def  = thm "greaterThan_def";
paulson@15418
   823
val greaterThan_iff = thm "greaterThan_iff";
paulson@15418
   824
val ivl_disj_int = thms "ivl_disj_int";
paulson@15418
   825
val ivl_disj_int_one = thms "ivl_disj_int_one";
paulson@15418
   826
val ivl_disj_int_singleton = thms "ivl_disj_int_singleton";
paulson@15418
   827
val ivl_disj_int_two = thms "ivl_disj_int_two";
paulson@15418
   828
val ivl_disj_un = thms "ivl_disj_un";
paulson@15418
   829
val ivl_disj_un_one = thms "ivl_disj_un_one";
paulson@15418
   830
val ivl_disj_un_singleton = thms "ivl_disj_un_singleton";
paulson@15418
   831
val ivl_disj_un_two = thms "ivl_disj_un_two";
paulson@15418
   832
val lessThan_0 = thm "lessThan_0";
paulson@15418
   833
val lessThan_Suc = thm "lessThan_Suc";
paulson@15418
   834
val lessThan_Suc_atMost = thm "lessThan_Suc_atMost";
paulson@15418
   835
val lessThan_def     = thm "lessThan_def";
paulson@15418
   836
val lessThan_iff = thm "lessThan_iff";
paulson@15418
   837
val single_Diff_lessThan = thm "single_Diff_lessThan";
paulson@15418
   838
paulson@15418
   839
val bounded_nat_set_is_finite = thm "bounded_nat_set_is_finite";
paulson@15418
   840
val finite_atMost = thm "finite_atMost";
paulson@15418
   841
val finite_lessThan = thm "finite_lessThan";
paulson@15418
   842
*}
paulson@15418
   843
nipkow@8924
   844
end