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