src/HOL/SetInterval.thy
author haftmann
Tue Jan 15 16:19:23 2008 +0100 (2008-01-15)
changeset 25919 8b1c0d434824
parent 25560 63be39eeb41a
child 26072 f65a7fa2da6c
permissions -rw-r--r--
joined theories IntDef, Numeral, IntArith to theory Int
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
haftmann@25919
    13
imports Int
nipkow@15131
    14
begin
nipkow@8924
    15
nipkow@24691
    16
context ord
nipkow@24691
    17
begin
nipkow@24691
    18
definition
haftmann@25062
    19
  lessThan    :: "'a => 'a set"	("(1{..<_})") where
haftmann@25062
    20
  "{..<u} == {x. x < u}"
nipkow@24691
    21
nipkow@24691
    22
definition
haftmann@25062
    23
  atMost      :: "'a => 'a set"	("(1{.._})") where
haftmann@25062
    24
  "{..u} == {x. x \<le> u}"
nipkow@24691
    25
nipkow@24691
    26
definition
haftmann@25062
    27
  greaterThan :: "'a => 'a set"	("(1{_<..})") where
haftmann@25062
    28
  "{l<..} == {x. l<x}"
nipkow@24691
    29
nipkow@24691
    30
definition
haftmann@25062
    31
  atLeast     :: "'a => 'a set"	("(1{_..})") where
haftmann@25062
    32
  "{l..} == {x. l\<le>x}"
nipkow@24691
    33
nipkow@24691
    34
definition
haftmann@25062
    35
  greaterThanLessThan :: "'a => 'a => 'a set"  ("(1{_<..<_})") where
haftmann@25062
    36
  "{l<..<u} == {l<..} Int {..<u}"
nipkow@24691
    37
nipkow@24691
    38
definition
haftmann@25062
    39
  atLeastLessThan :: "'a => 'a => 'a set"      ("(1{_..<_})") where
haftmann@25062
    40
  "{l..<u} == {l..} Int {..<u}"
nipkow@24691
    41
nipkow@24691
    42
definition
haftmann@25062
    43
  greaterThanAtMost :: "'a => 'a => 'a set"    ("(1{_<.._})") where
haftmann@25062
    44
  "{l<..u} == {l<..} Int {..u}"
nipkow@24691
    45
nipkow@24691
    46
definition
haftmann@25062
    47
  atLeastAtMost :: "'a => 'a => 'a set"        ("(1{_.._})") where
haftmann@25062
    48
  "{l..u} == {l..} Int {..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
haftmann@25062
   109
lemma (in ord) lessThan_iff [iff]: "(i: lessThan k) = (i<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
haftmann@25062
   120
lemma (in ord) greaterThan_iff [iff]: "(i: greaterThan k) = (k<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
haftmann@25062
   133
lemma (in ord) atLeast_iff [iff]: "(i: atLeast k) = (k<=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
haftmann@25062
   141
lemma (in ord) atMost_iff [iff]: "(i: atMost k) = (i<=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]:
haftmann@25062
   197
  "(i : {l<..<u}) = (l < i & i < u)"
ballarin@13735
   198
by (simp add: greaterThanLessThan_def)
ballarin@13735
   199
paulson@24286
   200
lemma atLeastLessThan_iff [simp,noatp]:
haftmann@25062
   201
  "(i : {l..<u}) = (l <= i & i < u)"
ballarin@13735
   202
by (simp add: atLeastLessThan_def)
ballarin@13735
   203
paulson@24286
   204
lemma greaterThanAtMost_iff [simp,noatp]:
haftmann@25062
   205
  "(i : {l<..u}) = (l < i & i <= u)"
ballarin@13735
   206
by (simp add: greaterThanAtMost_def)
ballarin@13735
   207
paulson@24286
   208
lemma atLeastAtMost_iff [simp,noatp]:
haftmann@25062
   209
  "(i : {l..u}) = (l <= i & i <= 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
haftmann@25062
   222
lemma atLeastAtMost_empty [simp]: "n < m ==> {m..n} = {}";
nipkow@24691
   223
by (auto simp add: atLeastAtMost_def atMost_def atLeast_def)
nipkow@24691
   224
haftmann@25062
   225
lemma atLeastLessThan_empty[simp]: "n \<le> m ==> {m..<n} = {}"
nipkow@15554
   226
by (auto simp add: atLeastLessThan_def)
nipkow@15554
   227
haftmann@25062
   228
lemma greaterThanAtMost_empty[simp]:"l \<le> k ==> {k<..l} = {}"
nipkow@17719
   229
by(auto simp:greaterThanAtMost_def greaterThan_def atMost_def)
nipkow@17719
   230
haftmann@25062
   231
lemma greaterThanLessThan_empty[simp]:"l \<le> k ==> {k<..l} = {}"
nipkow@17719
   232
by(auto simp:greaterThanLessThan_def greaterThan_def lessThan_def)
nipkow@17719
   233
haftmann@25062
   234
lemma atLeastAtMost_singleton [simp]: "{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:
nipkow@24853
   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
nipkow@24853
   423
text{* Any subset of an interval of natural numbers the size of the
nipkow@24853
   424
subset is exactly that interval. *}
nipkow@24853
   425
nipkow@24853
   426
lemma subset_card_intvl_is_intvl:
nipkow@24853
   427
  "A <= {k..<k+card A} \<Longrightarrow> A = {k..<k+card A}" (is "PROP ?P")
nipkow@24853
   428
proof cases
nipkow@24853
   429
  assume "finite A"
nipkow@24853
   430
  thus "PROP ?P"
nipkow@24853
   431
  proof(induct A rule:finite_linorder_induct)
nipkow@24853
   432
    case empty thus ?case by auto
nipkow@24853
   433
  next
nipkow@24853
   434
    case (insert A b)
nipkow@24853
   435
    moreover hence "b ~: A" by auto
nipkow@24853
   436
    moreover have "A <= {k..<k+card A}" and "b = k+card A"
nipkow@24853
   437
      using `b ~: A` insert by fastsimp+
nipkow@24853
   438
    ultimately show ?case by auto
nipkow@24853
   439
  qed
nipkow@24853
   440
next
nipkow@24853
   441
  assume "~finite A" thus "PROP ?P" by simp
nipkow@24853
   442
qed
nipkow@24853
   443
nipkow@24853
   444
paulson@14485
   445
subsubsection {* Cardinality *}
paulson@14485
   446
nipkow@15045
   447
lemma card_lessThan [simp]: "card {..<u} = u"
paulson@15251
   448
  by (induct u, simp_all add: lessThan_Suc)
paulson@14485
   449
paulson@14485
   450
lemma card_atMost [simp]: "card {..u} = Suc u"
paulson@14485
   451
  by (simp add: lessThan_Suc_atMost [THEN sym])
paulson@14485
   452
nipkow@15045
   453
lemma card_atLeastLessThan [simp]: "card {l..<u} = u - l"
nipkow@15045
   454
  apply (subgoal_tac "card {l..<u} = card {..<u-l}")
paulson@14485
   455
  apply (erule ssubst, rule card_lessThan)
nipkow@15045
   456
  apply (subgoal_tac "(%x. x + l) ` {..<u-l} = {l..<u}")
paulson@14485
   457
  apply (erule subst)
paulson@14485
   458
  apply (rule card_image)
paulson@14485
   459
  apply (simp add: inj_on_def)
paulson@14485
   460
  apply (auto simp add: image_def atLeastLessThan_def lessThan_def)
paulson@14485
   461
  apply (rule_tac x = "x - l" in exI)
paulson@14485
   462
  apply arith
paulson@14485
   463
  done
paulson@14485
   464
paulson@15418
   465
lemma card_atLeastAtMost [simp]: "card {l..u} = Suc u - l"
paulson@14485
   466
  by (subst atLeastLessThanSuc_atLeastAtMost [THEN sym], simp)
paulson@14485
   467
paulson@15418
   468
lemma card_greaterThanAtMost [simp]: "card {l<..u} = u - l"
paulson@14485
   469
  by (subst atLeastSucAtMost_greaterThanAtMost [THEN sym], simp)
paulson@14485
   470
nipkow@15045
   471
lemma card_greaterThanLessThan [simp]: "card {l<..<u} = u - Suc l"
paulson@14485
   472
  by (subst atLeastSucLessThan_greaterThanLessThan [THEN sym], simp)
paulson@14485
   473
paulson@14485
   474
subsection {* Intervals of integers *}
paulson@14485
   475
nipkow@15045
   476
lemma atLeastLessThanPlusOne_atLeastAtMost_int: "{l..<u+1} = {l..(u::int)}"
paulson@14485
   477
  by (auto simp add: atLeastAtMost_def atLeastLessThan_def)
paulson@14485
   478
paulson@15418
   479
lemma atLeastPlusOneAtMost_greaterThanAtMost_int: "{l+1..u} = {l<..(u::int)}"
paulson@14485
   480
  by (auto simp add: atLeastAtMost_def greaterThanAtMost_def)
paulson@14485
   481
paulson@15418
   482
lemma atLeastPlusOneLessThan_greaterThanLessThan_int:
paulson@15418
   483
    "{l+1..<u} = {l<..<u::int}"
paulson@14485
   484
  by (auto simp add: atLeastLessThan_def greaterThanLessThan_def)
paulson@14485
   485
paulson@14485
   486
subsubsection {* Finiteness *}
paulson@14485
   487
paulson@15418
   488
lemma image_atLeastZeroLessThan_int: "0 \<le> u ==>
nipkow@15045
   489
    {(0::int)..<u} = int ` {..<nat u}"
paulson@14485
   490
  apply (unfold image_def lessThan_def)
paulson@14485
   491
  apply auto
paulson@14485
   492
  apply (rule_tac x = "nat x" in exI)
paulson@14485
   493
  apply (auto simp add: zless_nat_conj zless_nat_eq_int_zless [THEN sym])
paulson@14485
   494
  done
paulson@14485
   495
nipkow@15045
   496
lemma finite_atLeastZeroLessThan_int: "finite {(0::int)..<u}"
paulson@14485
   497
  apply (case_tac "0 \<le> u")
paulson@14485
   498
  apply (subst image_atLeastZeroLessThan_int, assumption)
paulson@14485
   499
  apply (rule finite_imageI)
paulson@14485
   500
  apply auto
paulson@14485
   501
  done
paulson@14485
   502
nipkow@15045
   503
lemma finite_atLeastLessThan_int [iff]: "finite {l..<u::int}"
nipkow@15045
   504
  apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
paulson@14485
   505
  apply (erule subst)
paulson@14485
   506
  apply (rule finite_imageI)
paulson@14485
   507
  apply (rule finite_atLeastZeroLessThan_int)
nipkow@16733
   508
  apply (rule image_add_int_atLeastLessThan)
paulson@14485
   509
  done
paulson@14485
   510
paulson@15418
   511
lemma finite_atLeastAtMost_int [iff]: "finite {l..(u::int)}"
paulson@14485
   512
  by (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym], simp)
paulson@14485
   513
paulson@15418
   514
lemma finite_greaterThanAtMost_int [iff]: "finite {l<..(u::int)}"
paulson@14485
   515
  by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
paulson@14485
   516
paulson@15418
   517
lemma finite_greaterThanLessThan_int [iff]: "finite {l<..<u::int}"
paulson@14485
   518
  by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
paulson@14485
   519
nipkow@24853
   520
paulson@14485
   521
subsubsection {* Cardinality *}
paulson@14485
   522
nipkow@15045
   523
lemma card_atLeastZeroLessThan_int: "card {(0::int)..<u} = nat u"
paulson@14485
   524
  apply (case_tac "0 \<le> u")
paulson@14485
   525
  apply (subst image_atLeastZeroLessThan_int, assumption)
paulson@14485
   526
  apply (subst card_image)
paulson@14485
   527
  apply (auto simp add: inj_on_def)
paulson@14485
   528
  done
paulson@14485
   529
nipkow@15045
   530
lemma card_atLeastLessThan_int [simp]: "card {l..<u} = nat (u - l)"
nipkow@15045
   531
  apply (subgoal_tac "card {l..<u} = card {0..<u-l}")
paulson@14485
   532
  apply (erule ssubst, rule card_atLeastZeroLessThan_int)
nipkow@15045
   533
  apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
paulson@14485
   534
  apply (erule subst)
paulson@14485
   535
  apply (rule card_image)
paulson@14485
   536
  apply (simp add: inj_on_def)
nipkow@16733
   537
  apply (rule image_add_int_atLeastLessThan)
paulson@14485
   538
  done
paulson@14485
   539
paulson@14485
   540
lemma card_atLeastAtMost_int [simp]: "card {l..u} = nat (u - l + 1)"
paulson@14485
   541
  apply (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym])
paulson@14485
   542
  apply (auto simp add: compare_rls)
paulson@14485
   543
  done
paulson@14485
   544
paulson@15418
   545
lemma card_greaterThanAtMost_int [simp]: "card {l<..u} = nat (u - l)"
paulson@14485
   546
  by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
paulson@14485
   547
nipkow@15045
   548
lemma card_greaterThanLessThan_int [simp]: "card {l<..<u} = nat (u - (l + 1))"
paulson@14485
   549
  by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
paulson@14485
   550
paulson@14485
   551
paulson@13850
   552
subsection {*Lemmas useful with the summation operator setsum*}
paulson@13850
   553
ballarin@16102
   554
text {* For examples, see Algebra/poly/UnivPoly2.thy *}
ballarin@13735
   555
wenzelm@14577
   556
subsubsection {* Disjoint Unions *}
ballarin@13735
   557
wenzelm@14577
   558
text {* Singletons and open intervals *}
ballarin@13735
   559
ballarin@13735
   560
lemma ivl_disj_un_singleton:
nipkow@15045
   561
  "{l::'a::linorder} Un {l<..} = {l..}"
nipkow@15045
   562
  "{..<u} Un {u::'a::linorder} = {..u}"
nipkow@15045
   563
  "(l::'a::linorder) < u ==> {l} Un {l<..<u} = {l..<u}"
nipkow@15045
   564
  "(l::'a::linorder) < u ==> {l<..<u} Un {u} = {l<..u}"
nipkow@15045
   565
  "(l::'a::linorder) <= u ==> {l} Un {l<..u} = {l..u}"
nipkow@15045
   566
  "(l::'a::linorder) <= u ==> {l..<u} Un {u} = {l..u}"
ballarin@14398
   567
by auto
ballarin@13735
   568
wenzelm@14577
   569
text {* One- and two-sided intervals *}
ballarin@13735
   570
ballarin@13735
   571
lemma ivl_disj_un_one:
nipkow@15045
   572
  "(l::'a::linorder) < u ==> {..l} Un {l<..<u} = {..<u}"
nipkow@15045
   573
  "(l::'a::linorder) <= u ==> {..<l} Un {l..<u} = {..<u}"
nipkow@15045
   574
  "(l::'a::linorder) <= u ==> {..l} Un {l<..u} = {..u}"
nipkow@15045
   575
  "(l::'a::linorder) <= u ==> {..<l} Un {l..u} = {..u}"
nipkow@15045
   576
  "(l::'a::linorder) <= u ==> {l<..u} Un {u<..} = {l<..}"
nipkow@15045
   577
  "(l::'a::linorder) < u ==> {l<..<u} Un {u..} = {l<..}"
nipkow@15045
   578
  "(l::'a::linorder) <= u ==> {l..u} Un {u<..} = {l..}"
nipkow@15045
   579
  "(l::'a::linorder) <= u ==> {l..<u} Un {u..} = {l..}"
ballarin@14398
   580
by auto
ballarin@13735
   581
wenzelm@14577
   582
text {* Two- and two-sided intervals *}
ballarin@13735
   583
ballarin@13735
   584
lemma ivl_disj_un_two:
nipkow@15045
   585
  "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..<u} = {l<..<u}"
nipkow@15045
   586
  "[| (l::'a::linorder) <= m; m < u |] ==> {l<..m} Un {m<..<u} = {l<..<u}"
nipkow@15045
   587
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..<u} = {l..<u}"
nipkow@15045
   588
  "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m<..<u} = {l..<u}"
nipkow@15045
   589
  "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..u} = {l<..u}"
nipkow@15045
   590
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l<..m} Un {m<..u} = {l<..u}"
nipkow@15045
   591
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..u} = {l..u}"
nipkow@15045
   592
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m<..u} = {l..u}"
ballarin@14398
   593
by auto
ballarin@13735
   594
ballarin@13735
   595
lemmas ivl_disj_un = ivl_disj_un_singleton ivl_disj_un_one ivl_disj_un_two
ballarin@13735
   596
wenzelm@14577
   597
subsubsection {* Disjoint Intersections *}
ballarin@13735
   598
wenzelm@14577
   599
text {* Singletons and open intervals *}
ballarin@13735
   600
ballarin@13735
   601
lemma ivl_disj_int_singleton:
nipkow@15045
   602
  "{l::'a::order} Int {l<..} = {}"
nipkow@15045
   603
  "{..<u} Int {u} = {}"
nipkow@15045
   604
  "{l} Int {l<..<u} = {}"
nipkow@15045
   605
  "{l<..<u} Int {u} = {}"
nipkow@15045
   606
  "{l} Int {l<..u} = {}"
nipkow@15045
   607
  "{l..<u} Int {u} = {}"
ballarin@13735
   608
  by simp+
ballarin@13735
   609
wenzelm@14577
   610
text {* One- and two-sided intervals *}
ballarin@13735
   611
ballarin@13735
   612
lemma ivl_disj_int_one:
nipkow@15045
   613
  "{..l::'a::order} Int {l<..<u} = {}"
nipkow@15045
   614
  "{..<l} Int {l..<u} = {}"
nipkow@15045
   615
  "{..l} Int {l<..u} = {}"
nipkow@15045
   616
  "{..<l} Int {l..u} = {}"
nipkow@15045
   617
  "{l<..u} Int {u<..} = {}"
nipkow@15045
   618
  "{l<..<u} Int {u..} = {}"
nipkow@15045
   619
  "{l..u} Int {u<..} = {}"
nipkow@15045
   620
  "{l..<u} Int {u..} = {}"
ballarin@14398
   621
  by auto
ballarin@13735
   622
wenzelm@14577
   623
text {* Two- and two-sided intervals *}
ballarin@13735
   624
ballarin@13735
   625
lemma ivl_disj_int_two:
nipkow@15045
   626
  "{l::'a::order<..<m} Int {m..<u} = {}"
nipkow@15045
   627
  "{l<..m} Int {m<..<u} = {}"
nipkow@15045
   628
  "{l..<m} Int {m..<u} = {}"
nipkow@15045
   629
  "{l..m} Int {m<..<u} = {}"
nipkow@15045
   630
  "{l<..<m} Int {m..u} = {}"
nipkow@15045
   631
  "{l<..m} Int {m<..u} = {}"
nipkow@15045
   632
  "{l..<m} Int {m..u} = {}"
nipkow@15045
   633
  "{l..m} Int {m<..u} = {}"
ballarin@14398
   634
  by auto
ballarin@13735
   635
ballarin@13735
   636
lemmas ivl_disj_int = ivl_disj_int_singleton ivl_disj_int_one ivl_disj_int_two
ballarin@13735
   637
nipkow@15542
   638
subsubsection {* Some Differences *}
nipkow@15542
   639
nipkow@15542
   640
lemma ivl_diff[simp]:
nipkow@15542
   641
 "i \<le> n \<Longrightarrow> {i..<m} - {i..<n} = {n..<(m::'a::linorder)}"
nipkow@15542
   642
by(auto)
nipkow@15542
   643
nipkow@15542
   644
nipkow@15542
   645
subsubsection {* Some Subset Conditions *}
nipkow@15542
   646
paulson@24286
   647
lemma ivl_subset [simp,noatp]:
nipkow@15542
   648
 "({i..<j} \<subseteq> {m..<n}) = (j \<le> i | m \<le> i & j \<le> (n::'a::linorder))"
nipkow@15542
   649
apply(auto simp:linorder_not_le)
nipkow@15542
   650
apply(rule ccontr)
nipkow@15542
   651
apply(insert linorder_le_less_linear[of i n])
nipkow@15542
   652
apply(clarsimp simp:linorder_not_le)
nipkow@15542
   653
apply(fastsimp)
nipkow@15542
   654
done
nipkow@15542
   655
nipkow@15041
   656
nipkow@15042
   657
subsection {* Summation indexed over intervals *}
nipkow@15042
   658
nipkow@15042
   659
syntax
nipkow@15042
   660
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048
   661
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _..<_./ _)" [0,0,0,10] 10)
nipkow@16052
   662
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<_./ _)" [0,0,10] 10)
nipkow@16052
   663
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<=_./ _)" [0,0,10] 10)
nipkow@15042
   664
syntax (xsymbols)
nipkow@15042
   665
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048
   666
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
nipkow@16052
   667
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
nipkow@16052
   668
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
nipkow@15042
   669
syntax (HTML output)
nipkow@15042
   670
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048
   671
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
nipkow@16052
   672
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
nipkow@16052
   673
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
nipkow@15056
   674
syntax (latex_sum output)
nipkow@15052
   675
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@15052
   676
 ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
nipkow@15052
   677
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@15052
   678
 ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
nipkow@16052
   679
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@16052
   680
 ("(3\<^raw:$\sum_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
nipkow@15052
   681
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@16052
   682
 ("(3\<^raw:$\sum_{>_ \<le> _\<^raw:}$> _)" [0,0,10] 10)
nipkow@15041
   683
nipkow@15048
   684
translations
nipkow@15048
   685
  "\<Sum>x=a..b. t" == "setsum (%x. t) {a..b}"
nipkow@15048
   686
  "\<Sum>x=a..<b. t" == "setsum (%x. t) {a..<b}"
nipkow@16052
   687
  "\<Sum>i\<le>n. t" == "setsum (\<lambda>i. t) {..n}"
nipkow@15048
   688
  "\<Sum>i<n. t" == "setsum (\<lambda>i. t) {..<n}"
nipkow@15041
   689
nipkow@15052
   690
text{* The above introduces some pretty alternative syntaxes for
nipkow@15056
   691
summation over intervals:
nipkow@15052
   692
\begin{center}
nipkow@15052
   693
\begin{tabular}{lll}
nipkow@15056
   694
Old & New & \LaTeX\\
nipkow@15056
   695
@{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
   696
@{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
   697
@{term[source]"\<Sum>x\<in>{..b}. e"} & @{term"\<Sum>x\<le>b. e"} & @{term[mode=latex_sum]"\<Sum>x\<le>b. e"}\\
nipkow@15056
   698
@{term[source]"\<Sum>x\<in>{..<b}. e"} & @{term"\<Sum>x<b. e"} & @{term[mode=latex_sum]"\<Sum>x<b. e"}
nipkow@15052
   699
\end{tabular}
nipkow@15052
   700
\end{center}
nipkow@15056
   701
The left column shows the term before introduction of the new syntax,
nipkow@15056
   702
the middle column shows the new (default) syntax, and the right column
nipkow@15056
   703
shows a special syntax. The latter is only meaningful for latex output
nipkow@15056
   704
and has to be activated explicitly by setting the print mode to
wenzelm@21502
   705
@{text latex_sum} (e.g.\ via @{text "mode = latex_sum"} in
nipkow@15056
   706
antiquotations). It is not the default \LaTeX\ output because it only
nipkow@15056
   707
works well with italic-style formulae, not tt-style.
nipkow@15052
   708
nipkow@15052
   709
Note that for uniformity on @{typ nat} it is better to use
nipkow@15052
   710
@{term"\<Sum>x::nat=0..<n. e"} rather than @{text"\<Sum>x<n. e"}: @{text setsum} may
nipkow@15052
   711
not provide all lemmas available for @{term"{m..<n}"} also in the
nipkow@15052
   712
special form for @{term"{..<n}"}. *}
nipkow@15052
   713
nipkow@15542
   714
text{* This congruence rule should be used for sums over intervals as
nipkow@15542
   715
the standard theorem @{text[source]setsum_cong} does not work well
nipkow@15542
   716
with the simplifier who adds the unsimplified premise @{term"x:B"} to
nipkow@15542
   717
the context. *}
nipkow@15542
   718
nipkow@15542
   719
lemma setsum_ivl_cong:
nipkow@15542
   720
 "\<lbrakk>a = c; b = d; !!x. \<lbrakk> c \<le> x; x < d \<rbrakk> \<Longrightarrow> f x = g x \<rbrakk> \<Longrightarrow>
nipkow@15542
   721
 setsum f {a..<b} = setsum g {c..<d}"
nipkow@15542
   722
by(rule setsum_cong, simp_all)
nipkow@15041
   723
nipkow@16041
   724
(* FIXME why are the following simp rules but the corresponding eqns
nipkow@16041
   725
on intervals are not? *)
nipkow@16041
   726
nipkow@16052
   727
lemma setsum_atMost_Suc[simp]: "(\<Sum>i \<le> Suc n. f i) = (\<Sum>i \<le> n. f i) + f(Suc n)"
nipkow@16052
   728
by (simp add:atMost_Suc add_ac)
nipkow@16052
   729
nipkow@16041
   730
lemma setsum_lessThan_Suc[simp]: "(\<Sum>i < Suc n. f i) = (\<Sum>i < n. f i) + f n"
nipkow@16041
   731
by (simp add:lessThan_Suc add_ac)
nipkow@15041
   732
nipkow@15911
   733
lemma setsum_cl_ivl_Suc[simp]:
nipkow@15561
   734
  "setsum f {m..Suc n} = (if Suc n < m then 0 else setsum f {m..n} + f(Suc n))"
nipkow@15561
   735
by (auto simp:add_ac atLeastAtMostSuc_conv)
nipkow@15561
   736
nipkow@15911
   737
lemma setsum_op_ivl_Suc[simp]:
nipkow@15561
   738
  "setsum f {m..<Suc n} = (if n < m then 0 else setsum f {m..<n} + f(n))"
nipkow@15561
   739
by (auto simp:add_ac atLeastLessThanSuc)
nipkow@16041
   740
(*
nipkow@15561
   741
lemma setsum_cl_ivl_add_one_nat: "(n::nat) <= m + 1 ==>
nipkow@15561
   742
    (\<Sum>i=n..m+1. f i) = (\<Sum>i=n..m. f i) + f(m + 1)"
nipkow@15561
   743
by (auto simp:add_ac atLeastAtMostSuc_conv)
nipkow@16041
   744
*)
nipkow@15539
   745
lemma setsum_add_nat_ivl: "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
nipkow@15539
   746
  setsum f {m..<n} + setsum f {n..<p} = setsum f {m..<p::nat}"
nipkow@15539
   747
by (simp add:setsum_Un_disjoint[symmetric] ivl_disj_int ivl_disj_un)
nipkow@15539
   748
nipkow@15539
   749
lemma setsum_diff_nat_ivl:
nipkow@15539
   750
fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
nipkow@15539
   751
shows "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
nipkow@15539
   752
  setsum f {m..<p} - setsum f {m..<n} = setsum f {n..<p}"
nipkow@15539
   753
using setsum_add_nat_ivl [of m n p f,symmetric]
nipkow@15539
   754
apply (simp add: add_ac)
nipkow@15539
   755
done
nipkow@15539
   756
nipkow@16733
   757
subsection{* Shifting bounds *}
nipkow@16733
   758
nipkow@15539
   759
lemma setsum_shift_bounds_nat_ivl:
nipkow@15539
   760
  "setsum f {m+k..<n+k} = setsum (%i. f(i + k)){m..<n::nat}"
nipkow@15539
   761
by (induct "n", auto simp:atLeastLessThanSuc)
nipkow@15539
   762
nipkow@16733
   763
lemma setsum_shift_bounds_cl_nat_ivl:
nipkow@16733
   764
  "setsum f {m+k..n+k} = setsum (%i. f(i + k)){m..n::nat}"
nipkow@16733
   765
apply (insert setsum_reindex[OF inj_on_add_nat, where h=f and B = "{m..n}"])
nipkow@16733
   766
apply (simp add:image_add_atLeastAtMost o_def)
nipkow@16733
   767
done
nipkow@16733
   768
nipkow@16733
   769
corollary setsum_shift_bounds_cl_Suc_ivl:
nipkow@16733
   770
  "setsum f {Suc m..Suc n} = setsum (%i. f(Suc i)){m..n}"
nipkow@16733
   771
by (simp add:setsum_shift_bounds_cl_nat_ivl[where k=1,simplified])
nipkow@16733
   772
nipkow@16733
   773
corollary setsum_shift_bounds_Suc_ivl:
nipkow@16733
   774
  "setsum f {Suc m..<Suc n} = setsum (%i. f(Suc i)){m..<n}"
nipkow@16733
   775
by (simp add:setsum_shift_bounds_nat_ivl[where k=1,simplified])
nipkow@16733
   776
kleing@19106
   777
lemma setsum_head:
kleing@19106
   778
  fixes n :: nat
kleing@19106
   779
  assumes mn: "m <= n" 
kleing@19106
   780
  shows "(\<Sum>x\<in>{m..n}. P x) = P m + (\<Sum>x\<in>{m<..n}. P x)" (is "?lhs = ?rhs")
kleing@19106
   781
proof -
kleing@19106
   782
  from mn
kleing@19106
   783
  have "{m..n} = {m} \<union> {m<..n}"
kleing@19106
   784
    by (auto intro: ivl_disj_un_singleton)
kleing@19106
   785
  hence "?lhs = (\<Sum>x\<in>{m} \<union> {m<..n}. P x)"
kleing@19106
   786
    by (simp add: atLeast0LessThan)
kleing@19106
   787
  also have "\<dots> = ?rhs" by simp
kleing@19106
   788
  finally show ?thesis .
kleing@19106
   789
qed
kleing@19106
   790
kleing@19106
   791
lemma setsum_head_upt:
kleing@19022
   792
  fixes m::nat
kleing@19022
   793
  assumes m: "0 < m"
kleing@19106
   794
  shows "(\<Sum>x<m. P x) = P 0 + (\<Sum>x\<in>{1..<m}. P x)"
kleing@19022
   795
proof -
kleing@19106
   796
  have "(\<Sum>x<m. P x) = (\<Sum>x\<in>{0..<m}. P x)" 
kleing@19022
   797
    by (simp add: atLeast0LessThan)
kleing@19106
   798
  also 
kleing@19106
   799
  from m 
kleing@19106
   800
  have "\<dots> = (\<Sum>x\<in>{0..m - 1}. P x)"
kleing@19106
   801
    by (cases m) (auto simp add: atLeastLessThanSuc_atLeastAtMost)
kleing@19106
   802
  also
kleing@19106
   803
  have "\<dots> = P 0 + (\<Sum>x\<in>{0<..m - 1}. P x)"
kleing@19106
   804
    by (simp add: setsum_head)
kleing@19106
   805
  also 
kleing@19106
   806
  from m 
kleing@19106
   807
  have "{0<..m - 1} = {1..<m}" 
kleing@19106
   808
    by (cases m) (auto simp add: atLeastLessThanSuc_atLeastAtMost)
kleing@19106
   809
  finally show ?thesis .
kleing@19022
   810
qed
kleing@19022
   811
ballarin@17149
   812
subsection {* The formula for geometric sums *}
ballarin@17149
   813
ballarin@17149
   814
lemma geometric_sum:
ballarin@17149
   815
  "x ~= 1 ==> (\<Sum>i=0..<n. x ^ i) =
huffman@22713
   816
  (x ^ n - 1) / (x - 1::'a::{field, recpower})"
nipkow@23496
   817
by (induct "n") (simp_all add:field_simps power_Suc)
ballarin@17149
   818
kleing@19469
   819
subsection {* The formula for arithmetic sums *}
kleing@19469
   820
kleing@19469
   821
lemma gauss_sum:
huffman@23277
   822
  "((1::'a::comm_semiring_1) + 1)*(\<Sum>i\<in>{1..n}. of_nat i) =
kleing@19469
   823
   of_nat n*((of_nat n)+1)"
kleing@19469
   824
proof (induct n)
kleing@19469
   825
  case 0
kleing@19469
   826
  show ?case by simp
kleing@19469
   827
next
kleing@19469
   828
  case (Suc n)
nipkow@23477
   829
  then show ?case by (simp add: ring_simps)
kleing@19469
   830
qed
kleing@19469
   831
kleing@19469
   832
theorem arith_series_general:
huffman@23277
   833
  "((1::'a::comm_semiring_1) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
kleing@19469
   834
  of_nat n * (a + (a + of_nat(n - 1)*d))"
kleing@19469
   835
proof cases
kleing@19469
   836
  assume ngt1: "n > 1"
kleing@19469
   837
  let ?I = "\<lambda>i. of_nat i" and ?n = "of_nat n"
kleing@19469
   838
  have
kleing@19469
   839
    "(\<Sum>i\<in>{..<n}. a+?I i*d) =
kleing@19469
   840
     ((\<Sum>i\<in>{..<n}. a) + (\<Sum>i\<in>{..<n}. ?I i*d))"
kleing@19469
   841
    by (rule setsum_addf)
kleing@19469
   842
  also from ngt1 have "\<dots> = ?n*a + (\<Sum>i\<in>{..<n}. ?I i*d)" by simp
kleing@19469
   843
  also from ngt1 have "\<dots> = (?n*a + d*(\<Sum>i\<in>{1..<n}. ?I i))"
kleing@19469
   844
    by (simp add: setsum_right_distrib setsum_head_upt mult_ac)
kleing@19469
   845
  also have "(1+1)*\<dots> = (1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..<n}. ?I i)"
kleing@19469
   846
    by (simp add: left_distrib right_distrib)
kleing@19469
   847
  also from ngt1 have "{1..<n} = {1..n - 1}"
kleing@19469
   848
    by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost)    
kleing@19469
   849
  also from ngt1 
kleing@19469
   850
  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
   851
    by (simp only: mult_ac gauss_sum [of "n - 1"])
huffman@23431
   852
       (simp add:  mult_ac trans [OF add_commute of_nat_Suc [symmetric]])
kleing@19469
   853
  finally show ?thesis by (simp add: mult_ac add_ac right_distrib)
kleing@19469
   854
next
kleing@19469
   855
  assume "\<not>(n > 1)"
kleing@19469
   856
  hence "n = 1 \<or> n = 0" by auto
kleing@19469
   857
  thus ?thesis by (auto simp: mult_ac right_distrib)
kleing@19469
   858
qed
kleing@19469
   859
kleing@19469
   860
lemma arith_series_nat:
kleing@19469
   861
  "Suc (Suc 0) * (\<Sum>i\<in>{..<n}. a+i*d) = n * (a + (a+(n - 1)*d))"
kleing@19469
   862
proof -
kleing@19469
   863
  have
kleing@19469
   864
    "((1::nat) + 1) * (\<Sum>i\<in>{..<n::nat}. a + of_nat(i)*d) =
kleing@19469
   865
    of_nat(n) * (a + (a + of_nat(n - 1)*d))"
kleing@19469
   866
    by (rule arith_series_general)
kleing@19469
   867
  thus ?thesis by (auto simp add: of_nat_id)
kleing@19469
   868
qed
kleing@19469
   869
kleing@19469
   870
lemma arith_series_int:
kleing@19469
   871
  "(2::int) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
kleing@19469
   872
  of_nat n * (a + (a + of_nat(n - 1)*d))"
kleing@19469
   873
proof -
kleing@19469
   874
  have
kleing@19469
   875
    "((1::int) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
kleing@19469
   876
    of_nat(n) * (a + (a + of_nat(n - 1)*d))"
kleing@19469
   877
    by (rule arith_series_general)
kleing@19469
   878
  thus ?thesis by simp
kleing@19469
   879
qed
paulson@15418
   880
kleing@19022
   881
lemma sum_diff_distrib:
kleing@19022
   882
  fixes P::"nat\<Rightarrow>nat"
kleing@19022
   883
  shows
kleing@19022
   884
  "\<forall>x. Q x \<le> P x  \<Longrightarrow>
kleing@19022
   885
  (\<Sum>x<n. P x) - (\<Sum>x<n. Q x) = (\<Sum>x<n. P x - Q x)"
kleing@19022
   886
proof (induct n)
kleing@19022
   887
  case 0 show ?case by simp
kleing@19022
   888
next
kleing@19022
   889
  case (Suc n)
kleing@19022
   890
kleing@19022
   891
  let ?lhs = "(\<Sum>x<n. P x) - (\<Sum>x<n. Q x)"
kleing@19022
   892
  let ?rhs = "\<Sum>x<n. P x - Q x"
kleing@19022
   893
kleing@19022
   894
  from Suc have "?lhs = ?rhs" by simp
kleing@19022
   895
  moreover
kleing@19022
   896
  from Suc have "?lhs + P n - Q n = ?rhs + (P n - Q n)" by simp
kleing@19022
   897
  moreover
kleing@19022
   898
  from Suc have
kleing@19022
   899
    "(\<Sum>x<n. P x) + P n - ((\<Sum>x<n. Q x) + Q n) = ?rhs + (P n - Q n)"
kleing@19022
   900
    by (subst diff_diff_left[symmetric],
kleing@19022
   901
        subst diff_add_assoc2)
kleing@19022
   902
       (auto simp: diff_add_assoc2 intro: setsum_mono)
kleing@19022
   903
  ultimately
kleing@19022
   904
  show ?case by simp
kleing@19022
   905
qed
kleing@19022
   906
nipkow@8924
   907
end