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