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