src/HOL/SetInterval.thy
author hoelzl
Fri May 20 16:22:24 2011 +0200 (2011-05-20)
changeset 42891 e2f473671937
parent 40703 d1fc454d6735
child 42901 e35cf2b25f48
permissions -rw-r--r--
simp rules for empty intervals on dense linear order
nipkow@8924
     1
(*  Title:      HOL/SetInterval.thy
wenzelm@32960
     2
    Author:     Tobias Nipkow
wenzelm@32960
     3
    Author:     Clemens Ballarin
wenzelm@32960
     4
    Author:     Jeremy Avigad
nipkow@8924
     5
ballarin@13735
     6
lessThan, greaterThan, atLeast, atMost and two-sided intervals
nipkow@8924
     7
*)
nipkow@8924
     8
wenzelm@14577
     9
header {* Set intervals *}
wenzelm@14577
    10
nipkow@15131
    11
theory SetInterval
haftmann@33318
    12
imports Int Nat_Transfer
nipkow@15131
    13
begin
nipkow@8924
    14
nipkow@24691
    15
context ord
nipkow@24691
    16
begin
nipkow@24691
    17
definition
wenzelm@32960
    18
  lessThan    :: "'a => 'a set" ("(1{..<_})") where
haftmann@25062
    19
  "{..<u} == {x. x < u}"
nipkow@24691
    20
nipkow@24691
    21
definition
wenzelm@32960
    22
  atMost      :: "'a => 'a set" ("(1{.._})") where
haftmann@25062
    23
  "{..u} == {x. x \<le> u}"
nipkow@24691
    24
nipkow@24691
    25
definition
wenzelm@32960
    26
  greaterThan :: "'a => 'a set" ("(1{_<..})") where
haftmann@25062
    27
  "{l<..} == {x. l<x}"
nipkow@24691
    28
nipkow@24691
    29
definition
wenzelm@32960
    30
  atLeast     :: "'a => 'a set" ("(1{_..})") where
haftmann@25062
    31
  "{l..} == {x. l\<le>x}"
nipkow@24691
    32
nipkow@24691
    33
definition
haftmann@25062
    34
  greaterThanLessThan :: "'a => 'a => 'a set"  ("(1{_<..<_})") where
haftmann@25062
    35
  "{l<..<u} == {l<..} Int {..<u}"
nipkow@24691
    36
nipkow@24691
    37
definition
haftmann@25062
    38
  atLeastLessThan :: "'a => 'a => 'a set"      ("(1{_..<_})") where
haftmann@25062
    39
  "{l..<u} == {l..} Int {..<u}"
nipkow@24691
    40
nipkow@24691
    41
definition
haftmann@25062
    42
  greaterThanAtMost :: "'a => 'a => 'a set"    ("(1{_<.._})") where
haftmann@25062
    43
  "{l<..u} == {l<..} Int {..u}"
nipkow@24691
    44
nipkow@24691
    45
definition
haftmann@25062
    46
  atLeastAtMost :: "'a => 'a => 'a set"        ("(1{_.._})") where
haftmann@25062
    47
  "{l..u} == {l..} Int {..u}"
nipkow@24691
    48
nipkow@24691
    49
end
nipkow@8924
    50
ballarin@13735
    51
nipkow@15048
    52
text{* A note of warning when using @{term"{..<n}"} on type @{typ
nipkow@15048
    53
nat}: it is equivalent to @{term"{0::nat..<n}"} but some lemmas involving
nipkow@15052
    54
@{term"{m..<n}"} may not exist in @{term"{..<n}"}-form as well. *}
nipkow@15048
    55
kleing@14418
    56
syntax
huffman@36364
    57
  "_UNION_le"   :: "'a => 'a => 'b set => 'b set"       ("(3UN _<=_./ _)" [0, 0, 10] 10)
huffman@36364
    58
  "_UNION_less" :: "'a => 'a => 'b set => 'b set"       ("(3UN _<_./ _)" [0, 0, 10] 10)
huffman@36364
    59
  "_INTER_le"   :: "'a => 'a => 'b set => 'b set"       ("(3INT _<=_./ _)" [0, 0, 10] 10)
huffman@36364
    60
  "_INTER_less" :: "'a => 'a => 'b set => 'b set"       ("(3INT _<_./ _)" [0, 0, 10] 10)
kleing@14418
    61
nipkow@30372
    62
syntax (xsymbols)
huffman@36364
    63
  "_UNION_le"   :: "'a => 'a => 'b set => 'b set"       ("(3\<Union> _\<le>_./ _)" [0, 0, 10] 10)
huffman@36364
    64
  "_UNION_less" :: "'a => 'a => 'b set => 'b set"       ("(3\<Union> _<_./ _)" [0, 0, 10] 10)
huffman@36364
    65
  "_INTER_le"   :: "'a => 'a => 'b set => 'b set"       ("(3\<Inter> _\<le>_./ _)" [0, 0, 10] 10)
huffman@36364
    66
  "_INTER_less" :: "'a => 'a => 'b set => 'b set"       ("(3\<Inter> _<_./ _)" [0, 0, 10] 10)
kleing@14418
    67
nipkow@30372
    68
syntax (latex output)
huffman@36364
    69
  "_UNION_le"   :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Union>(00_ \<le> _)/ _)" [0, 0, 10] 10)
huffman@36364
    70
  "_UNION_less" :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Union>(00_ < _)/ _)" [0, 0, 10] 10)
huffman@36364
    71
  "_INTER_le"   :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Inter>(00_ \<le> _)/ _)" [0, 0, 10] 10)
huffman@36364
    72
  "_INTER_less" :: "'a \<Rightarrow> 'a => 'b set => 'b set"       ("(3\<Inter>(00_ < _)/ _)" [0, 0, 10] 10)
kleing@14418
    73
kleing@14418
    74
translations
kleing@14418
    75
  "UN i<=n. A"  == "UN i:{..n}. A"
nipkow@15045
    76
  "UN i<n. A"   == "UN i:{..<n}. A"
kleing@14418
    77
  "INT i<=n. A" == "INT i:{..n}. A"
nipkow@15045
    78
  "INT i<n. A"  == "INT i:{..<n}. A"
kleing@14418
    79
kleing@14418
    80
paulson@14485
    81
subsection {* Various equivalences *}
ballarin@13735
    82
haftmann@25062
    83
lemma (in ord) lessThan_iff [iff]: "(i: lessThan k) = (i<k)"
paulson@13850
    84
by (simp add: lessThan_def)
ballarin@13735
    85
paulson@15418
    86
lemma Compl_lessThan [simp]:
ballarin@13735
    87
    "!!k:: 'a::linorder. -lessThan k = atLeast k"
paulson@13850
    88
apply (auto simp add: lessThan_def atLeast_def)
ballarin@13735
    89
done
ballarin@13735
    90
paulson@13850
    91
lemma single_Diff_lessThan [simp]: "!!k:: 'a::order. {k} - lessThan k = {k}"
paulson@13850
    92
by auto
ballarin@13735
    93
haftmann@25062
    94
lemma (in ord) greaterThan_iff [iff]: "(i: greaterThan k) = (k<i)"
paulson@13850
    95
by (simp add: greaterThan_def)
ballarin@13735
    96
paulson@15418
    97
lemma Compl_greaterThan [simp]:
ballarin@13735
    98
    "!!k:: 'a::linorder. -greaterThan k = atMost k"
haftmann@26072
    99
  by (auto simp add: greaterThan_def atMost_def)
ballarin@13735
   100
paulson@13850
   101
lemma Compl_atMost [simp]: "!!k:: 'a::linorder. -atMost k = greaterThan k"
paulson@13850
   102
apply (subst Compl_greaterThan [symmetric])
paulson@15418
   103
apply (rule double_complement)
ballarin@13735
   104
done
ballarin@13735
   105
haftmann@25062
   106
lemma (in ord) atLeast_iff [iff]: "(i: atLeast k) = (k<=i)"
paulson@13850
   107
by (simp add: atLeast_def)
ballarin@13735
   108
paulson@15418
   109
lemma Compl_atLeast [simp]:
ballarin@13735
   110
    "!!k:: 'a::linorder. -atLeast k = lessThan k"
haftmann@26072
   111
  by (auto simp add: lessThan_def atLeast_def)
ballarin@13735
   112
haftmann@25062
   113
lemma (in ord) atMost_iff [iff]: "(i: atMost k) = (i<=k)"
paulson@13850
   114
by (simp add: atMost_def)
ballarin@13735
   115
paulson@14485
   116
lemma atMost_Int_atLeast: "!!n:: 'a::order. atMost n Int atLeast n = {n}"
paulson@14485
   117
by (blast intro: order_antisym)
paulson@13850
   118
paulson@13850
   119
paulson@14485
   120
subsection {* Logical Equivalences for Set Inclusion and Equality *}
paulson@13850
   121
paulson@13850
   122
lemma atLeast_subset_iff [iff]:
paulson@15418
   123
     "(atLeast x \<subseteq> atLeast y) = (y \<le> (x::'a::order))"
paulson@15418
   124
by (blast intro: order_trans)
paulson@13850
   125
paulson@13850
   126
lemma atLeast_eq_iff [iff]:
paulson@15418
   127
     "(atLeast x = atLeast y) = (x = (y::'a::linorder))"
paulson@13850
   128
by (blast intro: order_antisym order_trans)
paulson@13850
   129
paulson@13850
   130
lemma greaterThan_subset_iff [iff]:
paulson@15418
   131
     "(greaterThan x \<subseteq> greaterThan y) = (y \<le> (x::'a::linorder))"
paulson@15418
   132
apply (auto simp add: greaterThan_def)
paulson@15418
   133
 apply (subst linorder_not_less [symmetric], blast)
paulson@13850
   134
done
paulson@13850
   135
paulson@13850
   136
lemma greaterThan_eq_iff [iff]:
paulson@15418
   137
     "(greaterThan x = greaterThan y) = (x = (y::'a::linorder))"
paulson@15418
   138
apply (rule iffI)
paulson@15418
   139
 apply (erule equalityE)
haftmann@29709
   140
 apply simp_all
paulson@13850
   141
done
paulson@13850
   142
paulson@15418
   143
lemma atMost_subset_iff [iff]: "(atMost x \<subseteq> atMost y) = (x \<le> (y::'a::order))"
paulson@13850
   144
by (blast intro: order_trans)
paulson@13850
   145
paulson@15418
   146
lemma atMost_eq_iff [iff]: "(atMost x = atMost y) = (x = (y::'a::linorder))"
paulson@13850
   147
by (blast intro: order_antisym order_trans)
paulson@13850
   148
paulson@13850
   149
lemma lessThan_subset_iff [iff]:
paulson@15418
   150
     "(lessThan x \<subseteq> lessThan y) = (x \<le> (y::'a::linorder))"
paulson@15418
   151
apply (auto simp add: lessThan_def)
paulson@15418
   152
 apply (subst linorder_not_less [symmetric], blast)
paulson@13850
   153
done
paulson@13850
   154
paulson@13850
   155
lemma lessThan_eq_iff [iff]:
paulson@15418
   156
     "(lessThan x = lessThan y) = (x = (y::'a::linorder))"
paulson@15418
   157
apply (rule iffI)
paulson@15418
   158
 apply (erule equalityE)
haftmann@29709
   159
 apply simp_all
ballarin@13735
   160
done
ballarin@13735
   161
hoelzl@40703
   162
lemma lessThan_strict_subset_iff:
hoelzl@40703
   163
  fixes m n :: "'a::linorder"
hoelzl@40703
   164
  shows "{..<m} < {..<n} \<longleftrightarrow> m < n"
hoelzl@40703
   165
  by (metis leD lessThan_subset_iff linorder_linear not_less_iff_gr_or_eq psubset_eq)
ballarin@13735
   166
paulson@13850
   167
subsection {*Two-sided intervals*}
ballarin@13735
   168
nipkow@24691
   169
context ord
nipkow@24691
   170
begin
nipkow@24691
   171
blanchet@35828
   172
lemma greaterThanLessThan_iff [simp,no_atp]:
haftmann@25062
   173
  "(i : {l<..<u}) = (l < i & i < u)"
ballarin@13735
   174
by (simp add: greaterThanLessThan_def)
ballarin@13735
   175
blanchet@35828
   176
lemma atLeastLessThan_iff [simp,no_atp]:
haftmann@25062
   177
  "(i : {l..<u}) = (l <= i & i < u)"
ballarin@13735
   178
by (simp add: atLeastLessThan_def)
ballarin@13735
   179
blanchet@35828
   180
lemma greaterThanAtMost_iff [simp,no_atp]:
haftmann@25062
   181
  "(i : {l<..u}) = (l < i & i <= u)"
ballarin@13735
   182
by (simp add: greaterThanAtMost_def)
ballarin@13735
   183
blanchet@35828
   184
lemma atLeastAtMost_iff [simp,no_atp]:
haftmann@25062
   185
  "(i : {l..u}) = (l <= i & i <= u)"
ballarin@13735
   186
by (simp add: atLeastAtMost_def)
ballarin@13735
   187
nipkow@32436
   188
text {* The above four lemmas could be declared as iffs. Unfortunately this
nipkow@32436
   189
breaks many proofs. Since it only helps blast, it is better to leave well
nipkow@32436
   190
alone *}
nipkow@32436
   191
nipkow@24691
   192
end
ballarin@13735
   193
nipkow@32400
   194
subsubsection{* Emptyness, singletons, subset *}
nipkow@15554
   195
nipkow@24691
   196
context order
nipkow@24691
   197
begin
nipkow@15554
   198
nipkow@32400
   199
lemma atLeastatMost_empty[simp]:
nipkow@32400
   200
  "b < a \<Longrightarrow> {a..b} = {}"
nipkow@32400
   201
by(auto simp: atLeastAtMost_def atLeast_def atMost_def)
nipkow@32400
   202
nipkow@32400
   203
lemma atLeastatMost_empty_iff[simp]:
nipkow@32400
   204
  "{a..b} = {} \<longleftrightarrow> (~ a <= b)"
nipkow@32400
   205
by auto (blast intro: order_trans)
nipkow@32400
   206
nipkow@32400
   207
lemma atLeastatMost_empty_iff2[simp]:
nipkow@32400
   208
  "{} = {a..b} \<longleftrightarrow> (~ a <= b)"
nipkow@32400
   209
by auto (blast intro: order_trans)
nipkow@32400
   210
nipkow@32400
   211
lemma atLeastLessThan_empty[simp]:
nipkow@32400
   212
  "b <= a \<Longrightarrow> {a..<b} = {}"
nipkow@32400
   213
by(auto simp: atLeastLessThan_def)
nipkow@24691
   214
nipkow@32400
   215
lemma atLeastLessThan_empty_iff[simp]:
nipkow@32400
   216
  "{a..<b} = {} \<longleftrightarrow> (~ a < b)"
nipkow@32400
   217
by auto (blast intro: le_less_trans)
nipkow@32400
   218
nipkow@32400
   219
lemma atLeastLessThan_empty_iff2[simp]:
nipkow@32400
   220
  "{} = {a..<b} \<longleftrightarrow> (~ a < b)"
nipkow@32400
   221
by auto (blast intro: le_less_trans)
nipkow@15554
   222
nipkow@32400
   223
lemma greaterThanAtMost_empty[simp]: "l \<le> k ==> {k<..l} = {}"
nipkow@17719
   224
by(auto simp:greaterThanAtMost_def greaterThan_def atMost_def)
nipkow@17719
   225
nipkow@32400
   226
lemma greaterThanAtMost_empty_iff[simp]: "{k<..l} = {} \<longleftrightarrow> ~ k < l"
nipkow@32400
   227
by auto (blast intro: less_le_trans)
nipkow@32400
   228
nipkow@32400
   229
lemma greaterThanAtMost_empty_iff2[simp]: "{} = {k<..l} \<longleftrightarrow> ~ k < l"
nipkow@32400
   230
by auto (blast intro: less_le_trans)
nipkow@32400
   231
haftmann@29709
   232
lemma greaterThanLessThan_empty[simp]:"l \<le> k ==> {k<..<l} = {}"
nipkow@17719
   233
by(auto simp:greaterThanLessThan_def greaterThan_def lessThan_def)
nipkow@17719
   234
haftmann@25062
   235
lemma atLeastAtMost_singleton [simp]: "{a..a} = {a}"
nipkow@24691
   236
by (auto simp add: atLeastAtMost_def atMost_def atLeast_def)
nipkow@24691
   237
hoelzl@36846
   238
lemma atLeastAtMost_singleton': "a = b \<Longrightarrow> {a .. b} = {a}" by simp
hoelzl@36846
   239
nipkow@32400
   240
lemma atLeastatMost_subset_iff[simp]:
nipkow@32400
   241
  "{a..b} <= {c..d} \<longleftrightarrow> (~ a <= b) | c <= a & b <= d"
nipkow@32400
   242
unfolding atLeastAtMost_def atLeast_def atMost_def
nipkow@32400
   243
by (blast intro: order_trans)
nipkow@32400
   244
nipkow@32400
   245
lemma atLeastatMost_psubset_iff:
nipkow@32400
   246
  "{a..b} < {c..d} \<longleftrightarrow>
nipkow@32400
   247
   ((~ a <= b) | c <= a & b <= d & (c < a | b < d))  &  c <= d"
nipkow@39302
   248
by(simp add: psubset_eq set_eq_iff less_le_not_le)(blast intro: order_trans)
nipkow@32400
   249
hoelzl@36846
   250
lemma atLeastAtMost_singleton_iff[simp]:
hoelzl@36846
   251
  "{a .. b} = {c} \<longleftrightarrow> a = b \<and> b = c"
hoelzl@36846
   252
proof
hoelzl@36846
   253
  assume "{a..b} = {c}"
hoelzl@36846
   254
  hence "\<not> (\<not> a \<le> b)" unfolding atLeastatMost_empty_iff[symmetric] by simp
hoelzl@36846
   255
  moreover with `{a..b} = {c}` have "c \<le> a \<and> b \<le> c" by auto
hoelzl@36846
   256
  ultimately show "a = b \<and> b = c" by auto
hoelzl@36846
   257
qed simp
hoelzl@36846
   258
nipkow@24691
   259
end
paulson@14485
   260
hoelzl@42891
   261
context dense_linorder
hoelzl@42891
   262
begin
hoelzl@42891
   263
hoelzl@42891
   264
lemma greaterThanLessThan_empty_iff[simp]:
hoelzl@42891
   265
  "{ a <..< b } = {} \<longleftrightarrow> b \<le> a"
hoelzl@42891
   266
  using dense[of a b] by (cases "a < b") auto
hoelzl@42891
   267
hoelzl@42891
   268
lemma greaterThanLessThan_empty_iff2[simp]:
hoelzl@42891
   269
  "{} = { a <..< b } \<longleftrightarrow> b \<le> a"
hoelzl@42891
   270
  using dense[of a b] by (cases "a < b") auto
hoelzl@42891
   271
hoelzl@42891
   272
end
hoelzl@42891
   273
nipkow@32408
   274
lemma (in linorder) atLeastLessThan_subset_iff:
nipkow@32408
   275
  "{a..<b} <= {c..<d} \<Longrightarrow> b <= a | c<=a & b<=d"
nipkow@32408
   276
apply (auto simp:subset_eq Ball_def)
nipkow@32408
   277
apply(frule_tac x=a in spec)
nipkow@32408
   278
apply(erule_tac x=d in allE)
nipkow@32408
   279
apply (simp add: less_imp_le)
nipkow@32408
   280
done
nipkow@32408
   281
hoelzl@40703
   282
lemma atLeastLessThan_inj:
hoelzl@40703
   283
  fixes a b c d :: "'a::linorder"
hoelzl@40703
   284
  assumes eq: "{a ..< b} = {c ..< d}" and "a < b" "c < d"
hoelzl@40703
   285
  shows "a = c" "b = d"
hoelzl@40703
   286
using assms by (metis atLeastLessThan_subset_iff eq less_le_not_le linorder_antisym_conv2 subset_refl)+
hoelzl@40703
   287
hoelzl@40703
   288
lemma atLeastLessThan_eq_iff:
hoelzl@40703
   289
  fixes a b c d :: "'a::linorder"
hoelzl@40703
   290
  assumes "a < b" "c < d"
hoelzl@40703
   291
  shows "{a ..< b} = {c ..< d} \<longleftrightarrow> a = c \<and> b = d"
hoelzl@40703
   292
  using atLeastLessThan_inj assms by auto
hoelzl@40703
   293
nipkow@32456
   294
subsubsection {* Intersection *}
nipkow@32456
   295
nipkow@32456
   296
context linorder
nipkow@32456
   297
begin
nipkow@32456
   298
nipkow@32456
   299
lemma Int_atLeastAtMost[simp]: "{a..b} Int {c..d} = {max a c .. min b d}"
nipkow@32456
   300
by auto
nipkow@32456
   301
nipkow@32456
   302
lemma Int_atLeastAtMostR1[simp]: "{..b} Int {c..d} = {c .. min b d}"
nipkow@32456
   303
by auto
nipkow@32456
   304
nipkow@32456
   305
lemma Int_atLeastAtMostR2[simp]: "{a..} Int {c..d} = {max a c .. d}"
nipkow@32456
   306
by auto
nipkow@32456
   307
nipkow@32456
   308
lemma Int_atLeastAtMostL1[simp]: "{a..b} Int {..d} = {a .. min b d}"
nipkow@32456
   309
by auto
nipkow@32456
   310
nipkow@32456
   311
lemma Int_atLeastAtMostL2[simp]: "{a..b} Int {c..} = {max a c .. b}"
nipkow@32456
   312
by auto
nipkow@32456
   313
nipkow@32456
   314
lemma Int_atLeastLessThan[simp]: "{a..<b} Int {c..<d} = {max a c ..< min b d}"
nipkow@32456
   315
by auto
nipkow@32456
   316
nipkow@32456
   317
lemma Int_greaterThanAtMost[simp]: "{a<..b} Int {c<..d} = {max a c <.. min b d}"
nipkow@32456
   318
by auto
nipkow@32456
   319
nipkow@32456
   320
lemma Int_greaterThanLessThan[simp]: "{a<..<b} Int {c<..<d} = {max a c <..< min b d}"
nipkow@32456
   321
by auto
nipkow@32456
   322
nipkow@32456
   323
end
nipkow@32456
   324
nipkow@32456
   325
paulson@14485
   326
subsection {* Intervals of natural numbers *}
paulson@14485
   327
paulson@15047
   328
subsubsection {* The Constant @{term lessThan} *}
paulson@15047
   329
paulson@14485
   330
lemma lessThan_0 [simp]: "lessThan (0::nat) = {}"
paulson@14485
   331
by (simp add: lessThan_def)
paulson@14485
   332
paulson@14485
   333
lemma lessThan_Suc: "lessThan (Suc k) = insert k (lessThan k)"
paulson@14485
   334
by (simp add: lessThan_def less_Suc_eq, blast)
paulson@14485
   335
hoelzl@39072
   336
text {* The following proof is convinient in induction proofs where
hoelzl@39072
   337
new elements get indices at the beginning. So it is used to transform
hoelzl@39072
   338
@{term "{..<Suc n}"} to @{term "0::nat"} and @{term "{..< n}"}. *}
hoelzl@39072
   339
hoelzl@39072
   340
lemma lessThan_Suc_eq_insert_0: "{..<Suc n} = insert 0 (Suc ` {..<n})"
hoelzl@39072
   341
proof safe
hoelzl@39072
   342
  fix x assume "x < Suc n" "x \<notin> Suc ` {..<n}"
hoelzl@39072
   343
  then have "x \<noteq> Suc (x - 1)" by auto
hoelzl@39072
   344
  with `x < Suc n` show "x = 0" by auto
hoelzl@39072
   345
qed
hoelzl@39072
   346
paulson@14485
   347
lemma lessThan_Suc_atMost: "lessThan (Suc k) = atMost k"
paulson@14485
   348
by (simp add: lessThan_def atMost_def less_Suc_eq_le)
paulson@14485
   349
paulson@14485
   350
lemma UN_lessThan_UNIV: "(UN m::nat. lessThan m) = UNIV"
paulson@14485
   351
by blast
paulson@14485
   352
paulson@15047
   353
subsubsection {* The Constant @{term greaterThan} *}
paulson@15047
   354
paulson@14485
   355
lemma greaterThan_0 [simp]: "greaterThan 0 = range Suc"
paulson@14485
   356
apply (simp add: greaterThan_def)
paulson@14485
   357
apply (blast dest: gr0_conv_Suc [THEN iffD1])
paulson@14485
   358
done
paulson@14485
   359
paulson@14485
   360
lemma greaterThan_Suc: "greaterThan (Suc k) = greaterThan k - {Suc k}"
paulson@14485
   361
apply (simp add: greaterThan_def)
paulson@14485
   362
apply (auto elim: linorder_neqE)
paulson@14485
   363
done
paulson@14485
   364
paulson@14485
   365
lemma INT_greaterThan_UNIV: "(INT m::nat. greaterThan m) = {}"
paulson@14485
   366
by blast
paulson@14485
   367
paulson@15047
   368
subsubsection {* The Constant @{term atLeast} *}
paulson@15047
   369
paulson@14485
   370
lemma atLeast_0 [simp]: "atLeast (0::nat) = UNIV"
paulson@14485
   371
by (unfold atLeast_def UNIV_def, simp)
paulson@14485
   372
paulson@14485
   373
lemma atLeast_Suc: "atLeast (Suc k) = atLeast k - {k}"
paulson@14485
   374
apply (simp add: atLeast_def)
paulson@14485
   375
apply (simp add: Suc_le_eq)
paulson@14485
   376
apply (simp add: order_le_less, blast)
paulson@14485
   377
done
paulson@14485
   378
paulson@14485
   379
lemma atLeast_Suc_greaterThan: "atLeast (Suc k) = greaterThan k"
paulson@14485
   380
  by (auto simp add: greaterThan_def atLeast_def less_Suc_eq_le)
paulson@14485
   381
paulson@14485
   382
lemma UN_atLeast_UNIV: "(UN m::nat. atLeast m) = UNIV"
paulson@14485
   383
by blast
paulson@14485
   384
paulson@15047
   385
subsubsection {* The Constant @{term atMost} *}
paulson@15047
   386
paulson@14485
   387
lemma atMost_0 [simp]: "atMost (0::nat) = {0}"
paulson@14485
   388
by (simp add: atMost_def)
paulson@14485
   389
paulson@14485
   390
lemma atMost_Suc: "atMost (Suc k) = insert (Suc k) (atMost k)"
paulson@14485
   391
apply (simp add: atMost_def)
paulson@14485
   392
apply (simp add: less_Suc_eq order_le_less, blast)
paulson@14485
   393
done
paulson@14485
   394
paulson@14485
   395
lemma UN_atMost_UNIV: "(UN m::nat. atMost m) = UNIV"
paulson@14485
   396
by blast
paulson@14485
   397
paulson@15047
   398
subsubsection {* The Constant @{term atLeastLessThan} *}
paulson@15047
   399
nipkow@28068
   400
text{*The orientation of the following 2 rules is tricky. The lhs is
nipkow@24449
   401
defined in terms of the rhs.  Hence the chosen orientation makes sense
nipkow@24449
   402
in this theory --- the reverse orientation complicates proofs (eg
nipkow@24449
   403
nontermination). But outside, when the definition of the lhs is rarely
nipkow@24449
   404
used, the opposite orientation seems preferable because it reduces a
nipkow@24449
   405
specific concept to a more general one. *}
nipkow@28068
   406
paulson@15047
   407
lemma atLeast0LessThan: "{0::nat..<n} = {..<n}"
nipkow@15042
   408
by(simp add:lessThan_def atLeastLessThan_def)
nipkow@24449
   409
nipkow@28068
   410
lemma atLeast0AtMost: "{0..n::nat} = {..n}"
nipkow@28068
   411
by(simp add:atMost_def atLeastAtMost_def)
nipkow@28068
   412
haftmann@31998
   413
declare atLeast0LessThan[symmetric, code_unfold]
haftmann@31998
   414
        atLeast0AtMost[symmetric, code_unfold]
nipkow@24449
   415
nipkow@24449
   416
lemma atLeastLessThan0: "{m..<0::nat} = {}"
paulson@15047
   417
by (simp add: atLeastLessThan_def)
nipkow@24449
   418
paulson@15047
   419
subsubsection {* Intervals of nats with @{term Suc} *}
paulson@15047
   420
paulson@15047
   421
text{*Not a simprule because the RHS is too messy.*}
paulson@15047
   422
lemma atLeastLessThanSuc:
paulson@15047
   423
    "{m..<Suc n} = (if m \<le> n then insert n {m..<n} else {})"
paulson@15418
   424
by (auto simp add: atLeastLessThan_def)
paulson@15047
   425
paulson@15418
   426
lemma atLeastLessThan_singleton [simp]: "{m..<Suc m} = {m}"
paulson@15047
   427
by (auto simp add: atLeastLessThan_def)
nipkow@16041
   428
(*
paulson@15047
   429
lemma atLeast_sum_LessThan [simp]: "{m + k..<k::nat} = {}"
paulson@15047
   430
by (induct k, simp_all add: atLeastLessThanSuc)
paulson@15047
   431
paulson@15047
   432
lemma atLeastSucLessThan [simp]: "{Suc n..<n} = {}"
paulson@15047
   433
by (auto simp add: atLeastLessThan_def)
nipkow@16041
   434
*)
nipkow@15045
   435
lemma atLeastLessThanSuc_atLeastAtMost: "{l..<Suc u} = {l..u}"
paulson@14485
   436
  by (simp add: lessThan_Suc_atMost atLeastAtMost_def atLeastLessThan_def)
paulson@14485
   437
paulson@15418
   438
lemma atLeastSucAtMost_greaterThanAtMost: "{Suc l..u} = {l<..u}"
paulson@15418
   439
  by (simp add: atLeast_Suc_greaterThan atLeastAtMost_def
paulson@14485
   440
    greaterThanAtMost_def)
paulson@14485
   441
paulson@15418
   442
lemma atLeastSucLessThan_greaterThanLessThan: "{Suc l..<u} = {l<..<u}"
paulson@15418
   443
  by (simp add: atLeast_Suc_greaterThan atLeastLessThan_def
paulson@14485
   444
    greaterThanLessThan_def)
paulson@14485
   445
nipkow@15554
   446
lemma atLeastAtMostSuc_conv: "m \<le> Suc n \<Longrightarrow> {m..Suc n} = insert (Suc n) {m..n}"
nipkow@15554
   447
by (auto simp add: atLeastAtMost_def)
nipkow@15554
   448
paulson@33044
   449
lemma atLeastLessThan_add_Un: "i \<le> j \<Longrightarrow> {i..<j+k} = {i..<j} \<union> {j..<j+k::nat}"
paulson@33044
   450
  apply (induct k) 
paulson@33044
   451
  apply (simp_all add: atLeastLessThanSuc)   
paulson@33044
   452
  done
paulson@33044
   453
nipkow@16733
   454
subsubsection {* Image *}
nipkow@16733
   455
nipkow@16733
   456
lemma image_add_atLeastAtMost:
nipkow@16733
   457
  "(%n::nat. n+k) ` {i..j} = {i+k..j+k}" (is "?A = ?B")
nipkow@16733
   458
proof
nipkow@16733
   459
  show "?A \<subseteq> ?B" by auto
nipkow@16733
   460
next
nipkow@16733
   461
  show "?B \<subseteq> ?A"
nipkow@16733
   462
  proof
nipkow@16733
   463
    fix n assume a: "n : ?B"
webertj@20217
   464
    hence "n - k : {i..j}" by auto
nipkow@16733
   465
    moreover have "n = (n - k) + k" using a by auto
nipkow@16733
   466
    ultimately show "n : ?A" by blast
nipkow@16733
   467
  qed
nipkow@16733
   468
qed
nipkow@16733
   469
nipkow@16733
   470
lemma image_add_atLeastLessThan:
nipkow@16733
   471
  "(%n::nat. n+k) ` {i..<j} = {i+k..<j+k}" (is "?A = ?B")
nipkow@16733
   472
proof
nipkow@16733
   473
  show "?A \<subseteq> ?B" by auto
nipkow@16733
   474
next
nipkow@16733
   475
  show "?B \<subseteq> ?A"
nipkow@16733
   476
  proof
nipkow@16733
   477
    fix n assume a: "n : ?B"
webertj@20217
   478
    hence "n - k : {i..<j}" by auto
nipkow@16733
   479
    moreover have "n = (n - k) + k" using a by auto
nipkow@16733
   480
    ultimately show "n : ?A" by blast
nipkow@16733
   481
  qed
nipkow@16733
   482
qed
nipkow@16733
   483
nipkow@16733
   484
corollary image_Suc_atLeastAtMost[simp]:
nipkow@16733
   485
  "Suc ` {i..j} = {Suc i..Suc j}"
huffman@30079
   486
using image_add_atLeastAtMost[where k="Suc 0"] by simp
nipkow@16733
   487
nipkow@16733
   488
corollary image_Suc_atLeastLessThan[simp]:
nipkow@16733
   489
  "Suc ` {i..<j} = {Suc i..<Suc j}"
huffman@30079
   490
using image_add_atLeastLessThan[where k="Suc 0"] by simp
nipkow@16733
   491
nipkow@16733
   492
lemma image_add_int_atLeastLessThan:
nipkow@16733
   493
    "(%x. x + (l::int)) ` {0..<u-l} = {l..<u}"
nipkow@16733
   494
  apply (auto simp add: image_def)
nipkow@16733
   495
  apply (rule_tac x = "x - l" in bexI)
nipkow@16733
   496
  apply auto
nipkow@16733
   497
  done
nipkow@16733
   498
hoelzl@37664
   499
lemma image_minus_const_atLeastLessThan_nat:
hoelzl@37664
   500
  fixes c :: nat
hoelzl@37664
   501
  shows "(\<lambda>i. i - c) ` {x ..< y} =
hoelzl@37664
   502
      (if c < y then {x - c ..< y - c} else if x < y then {0} else {})"
hoelzl@37664
   503
    (is "_ = ?right")
hoelzl@37664
   504
proof safe
hoelzl@37664
   505
  fix a assume a: "a \<in> ?right"
hoelzl@37664
   506
  show "a \<in> (\<lambda>i. i - c) ` {x ..< y}"
hoelzl@37664
   507
  proof cases
hoelzl@37664
   508
    assume "c < y" with a show ?thesis
hoelzl@37664
   509
      by (auto intro!: image_eqI[of _ _ "a + c"])
hoelzl@37664
   510
  next
hoelzl@37664
   511
    assume "\<not> c < y" with a show ?thesis
hoelzl@37664
   512
      by (auto intro!: image_eqI[of _ _ x] split: split_if_asm)
hoelzl@37664
   513
  qed
hoelzl@37664
   514
qed auto
hoelzl@37664
   515
hoelzl@35580
   516
context ordered_ab_group_add
hoelzl@35580
   517
begin
hoelzl@35580
   518
hoelzl@35580
   519
lemma
hoelzl@35580
   520
  fixes x :: 'a
hoelzl@35580
   521
  shows image_uminus_greaterThan[simp]: "uminus ` {x<..} = {..<-x}"
hoelzl@35580
   522
  and image_uminus_atLeast[simp]: "uminus ` {x..} = {..-x}"
hoelzl@35580
   523
proof safe
hoelzl@35580
   524
  fix y assume "y < -x"
hoelzl@35580
   525
  hence *:  "x < -y" using neg_less_iff_less[of "-y" x] by simp
hoelzl@35580
   526
  have "- (-y) \<in> uminus ` {x<..}"
hoelzl@35580
   527
    by (rule imageI) (simp add: *)
hoelzl@35580
   528
  thus "y \<in> uminus ` {x<..}" by simp
hoelzl@35580
   529
next
hoelzl@35580
   530
  fix y assume "y \<le> -x"
hoelzl@35580
   531
  have "- (-y) \<in> uminus ` {x..}"
hoelzl@35580
   532
    by (rule imageI) (insert `y \<le> -x`[THEN le_imp_neg_le], simp)
hoelzl@35580
   533
  thus "y \<in> uminus ` {x..}" by simp
hoelzl@35580
   534
qed simp_all
hoelzl@35580
   535
hoelzl@35580
   536
lemma
hoelzl@35580
   537
  fixes x :: 'a
hoelzl@35580
   538
  shows image_uminus_lessThan[simp]: "uminus ` {..<x} = {-x<..}"
hoelzl@35580
   539
  and image_uminus_atMost[simp]: "uminus ` {..x} = {-x..}"
hoelzl@35580
   540
proof -
hoelzl@35580
   541
  have "uminus ` {..<x} = uminus ` uminus ` {-x<..}"
hoelzl@35580
   542
    and "uminus ` {..x} = uminus ` uminus ` {-x..}" by simp_all
hoelzl@35580
   543
  thus "uminus ` {..<x} = {-x<..}" and "uminus ` {..x} = {-x..}"
hoelzl@35580
   544
    by (simp_all add: image_image
hoelzl@35580
   545
        del: image_uminus_greaterThan image_uminus_atLeast)
hoelzl@35580
   546
qed
hoelzl@35580
   547
hoelzl@35580
   548
lemma
hoelzl@35580
   549
  fixes x :: 'a
hoelzl@35580
   550
  shows image_uminus_atLeastAtMost[simp]: "uminus ` {x..y} = {-y..-x}"
hoelzl@35580
   551
  and image_uminus_greaterThanAtMost[simp]: "uminus ` {x<..y} = {-y..<-x}"
hoelzl@35580
   552
  and image_uminus_atLeastLessThan[simp]: "uminus ` {x..<y} = {-y<..-x}"
hoelzl@35580
   553
  and image_uminus_greaterThanLessThan[simp]: "uminus ` {x<..<y} = {-y<..<-x}"
hoelzl@35580
   554
  by (simp_all add: atLeastAtMost_def greaterThanAtMost_def atLeastLessThan_def
hoelzl@35580
   555
      greaterThanLessThan_def image_Int[OF inj_uminus] Int_commute)
hoelzl@35580
   556
end
nipkow@16733
   557
paulson@14485
   558
subsubsection {* Finiteness *}
paulson@14485
   559
nipkow@15045
   560
lemma finite_lessThan [iff]: fixes k :: nat shows "finite {..<k}"
paulson@14485
   561
  by (induct k) (simp_all add: lessThan_Suc)
paulson@14485
   562
paulson@14485
   563
lemma finite_atMost [iff]: fixes k :: nat shows "finite {..k}"
paulson@14485
   564
  by (induct k) (simp_all add: atMost_Suc)
paulson@14485
   565
paulson@14485
   566
lemma finite_greaterThanLessThan [iff]:
nipkow@15045
   567
  fixes l :: nat shows "finite {l<..<u}"
paulson@14485
   568
by (simp add: greaterThanLessThan_def)
paulson@14485
   569
paulson@14485
   570
lemma finite_atLeastLessThan [iff]:
nipkow@15045
   571
  fixes l :: nat shows "finite {l..<u}"
paulson@14485
   572
by (simp add: atLeastLessThan_def)
paulson@14485
   573
paulson@14485
   574
lemma finite_greaterThanAtMost [iff]:
nipkow@15045
   575
  fixes l :: nat shows "finite {l<..u}"
paulson@14485
   576
by (simp add: greaterThanAtMost_def)
paulson@14485
   577
paulson@14485
   578
lemma finite_atLeastAtMost [iff]:
paulson@14485
   579
  fixes l :: nat shows "finite {l..u}"
paulson@14485
   580
by (simp add: atLeastAtMost_def)
paulson@14485
   581
nipkow@28068
   582
text {* A bounded set of natural numbers is finite. *}
paulson@14485
   583
lemma bounded_nat_set_is_finite:
nipkow@24853
   584
  "(ALL i:N. i < (n::nat)) ==> finite N"
nipkow@28068
   585
apply (rule finite_subset)
nipkow@28068
   586
 apply (rule_tac [2] finite_lessThan, auto)
nipkow@28068
   587
done
nipkow@28068
   588
nipkow@31044
   589
text {* A set of natural numbers is finite iff it is bounded. *}
nipkow@31044
   590
lemma finite_nat_set_iff_bounded:
nipkow@31044
   591
  "finite(N::nat set) = (EX m. ALL n:N. n<m)" (is "?F = ?B")
nipkow@31044
   592
proof
nipkow@31044
   593
  assume f:?F  show ?B
nipkow@31044
   594
    using Max_ge[OF `?F`, simplified less_Suc_eq_le[symmetric]] by blast
nipkow@31044
   595
next
nipkow@31044
   596
  assume ?B show ?F using `?B` by(blast intro:bounded_nat_set_is_finite)
nipkow@31044
   597
qed
nipkow@31044
   598
nipkow@31044
   599
lemma finite_nat_set_iff_bounded_le:
nipkow@31044
   600
  "finite(N::nat set) = (EX m. ALL n:N. n<=m)"
nipkow@31044
   601
apply(simp add:finite_nat_set_iff_bounded)
nipkow@31044
   602
apply(blast dest:less_imp_le_nat le_imp_less_Suc)
nipkow@31044
   603
done
nipkow@31044
   604
nipkow@28068
   605
lemma finite_less_ub:
nipkow@28068
   606
     "!!f::nat=>nat. (!!n. n \<le> f n) ==> finite {n. f n \<le> u}"
nipkow@28068
   607
by (rule_tac B="{..u}" in finite_subset, auto intro: order_trans)
paulson@14485
   608
nipkow@24853
   609
text{* Any subset of an interval of natural numbers the size of the
nipkow@24853
   610
subset is exactly that interval. *}
nipkow@24853
   611
nipkow@24853
   612
lemma subset_card_intvl_is_intvl:
nipkow@24853
   613
  "A <= {k..<k+card A} \<Longrightarrow> A = {k..<k+card A}" (is "PROP ?P")
nipkow@24853
   614
proof cases
nipkow@24853
   615
  assume "finite A"
nipkow@24853
   616
  thus "PROP ?P"
nipkow@32006
   617
  proof(induct A rule:finite_linorder_max_induct)
nipkow@24853
   618
    case empty thus ?case by auto
nipkow@24853
   619
  next
nipkow@33434
   620
    case (insert b A)
nipkow@24853
   621
    moreover hence "b ~: A" by auto
nipkow@24853
   622
    moreover have "A <= {k..<k+card A}" and "b = k+card A"
nipkow@24853
   623
      using `b ~: A` insert by fastsimp+
nipkow@24853
   624
    ultimately show ?case by auto
nipkow@24853
   625
  qed
nipkow@24853
   626
next
nipkow@24853
   627
  assume "~finite A" thus "PROP ?P" by simp
nipkow@24853
   628
qed
nipkow@24853
   629
nipkow@24853
   630
paulson@32596
   631
subsubsection {* Proving Inclusions and Equalities between Unions *}
paulson@32596
   632
nipkow@36755
   633
lemma UN_le_eq_Un0:
nipkow@36755
   634
  "(\<Union>i\<le>n::nat. M i) = (\<Union>i\<in>{1..n}. M i) \<union> M 0" (is "?A = ?B")
nipkow@36755
   635
proof
nipkow@36755
   636
  show "?A <= ?B"
nipkow@36755
   637
  proof
nipkow@36755
   638
    fix x assume "x : ?A"
nipkow@36755
   639
    then obtain i where i: "i\<le>n" "x : M i" by auto
nipkow@36755
   640
    show "x : ?B"
nipkow@36755
   641
    proof(cases i)
nipkow@36755
   642
      case 0 with i show ?thesis by simp
nipkow@36755
   643
    next
nipkow@36755
   644
      case (Suc j) with i show ?thesis by auto
nipkow@36755
   645
    qed
nipkow@36755
   646
  qed
nipkow@36755
   647
next
nipkow@36755
   648
  show "?B <= ?A" by auto
nipkow@36755
   649
qed
nipkow@36755
   650
nipkow@36755
   651
lemma UN_le_add_shift:
nipkow@36755
   652
  "(\<Union>i\<le>n::nat. M(i+k)) = (\<Union>i\<in>{k..n+k}. M i)" (is "?A = ?B")
nipkow@36755
   653
proof
nipkow@36755
   654
  show "?A <= ?B" by fastsimp
nipkow@36755
   655
next
nipkow@36755
   656
  show "?B <= ?A"
nipkow@36755
   657
  proof
nipkow@36755
   658
    fix x assume "x : ?B"
nipkow@36755
   659
    then obtain i where i: "i : {k..n+k}" "x : M(i)" by auto
nipkow@36755
   660
    hence "i-k\<le>n & x : M((i-k)+k)" by auto
nipkow@36755
   661
    thus "x : ?A" by blast
nipkow@36755
   662
  qed
nipkow@36755
   663
qed
nipkow@36755
   664
paulson@32596
   665
lemma UN_UN_finite_eq: "(\<Union>n::nat. \<Union>i\<in>{0..<n}. A i) = (\<Union>n. A n)"
paulson@32596
   666
  by (auto simp add: atLeast0LessThan) 
paulson@32596
   667
paulson@32596
   668
lemma UN_finite_subset: "(!!n::nat. (\<Union>i\<in>{0..<n}. A i) \<subseteq> C) \<Longrightarrow> (\<Union>n. A n) \<subseteq> C"
paulson@32596
   669
  by (subst UN_UN_finite_eq [symmetric]) blast
paulson@32596
   670
paulson@33044
   671
lemma UN_finite2_subset: 
paulson@33044
   672
     "(!!n::nat. (\<Union>i\<in>{0..<n}. A i) \<subseteq> (\<Union>i\<in>{0..<n+k}. B i)) \<Longrightarrow> (\<Union>n. A n) \<subseteq> (\<Union>n. B n)"
paulson@33044
   673
  apply (rule UN_finite_subset)
paulson@33044
   674
  apply (subst UN_UN_finite_eq [symmetric, of B]) 
paulson@33044
   675
  apply blast
paulson@33044
   676
  done
paulson@32596
   677
paulson@32596
   678
lemma UN_finite2_eq:
paulson@33044
   679
  "(!!n::nat. (\<Union>i\<in>{0..<n}. A i) = (\<Union>i\<in>{0..<n+k}. B i)) \<Longrightarrow> (\<Union>n. A n) = (\<Union>n. B n)"
paulson@33044
   680
  apply (rule subset_antisym)
paulson@33044
   681
   apply (rule UN_finite2_subset, blast)
paulson@33044
   682
 apply (rule UN_finite2_subset [where k=k])
huffman@35216
   683
 apply (force simp add: atLeastLessThan_add_Un [of 0])
paulson@33044
   684
 done
paulson@32596
   685
paulson@32596
   686
paulson@14485
   687
subsubsection {* Cardinality *}
paulson@14485
   688
nipkow@15045
   689
lemma card_lessThan [simp]: "card {..<u} = u"
paulson@15251
   690
  by (induct u, simp_all add: lessThan_Suc)
paulson@14485
   691
paulson@14485
   692
lemma card_atMost [simp]: "card {..u} = Suc u"
paulson@14485
   693
  by (simp add: lessThan_Suc_atMost [THEN sym])
paulson@14485
   694
nipkow@15045
   695
lemma card_atLeastLessThan [simp]: "card {l..<u} = u - l"
nipkow@15045
   696
  apply (subgoal_tac "card {l..<u} = card {..<u-l}")
paulson@14485
   697
  apply (erule ssubst, rule card_lessThan)
nipkow@15045
   698
  apply (subgoal_tac "(%x. x + l) ` {..<u-l} = {l..<u}")
paulson@14485
   699
  apply (erule subst)
paulson@14485
   700
  apply (rule card_image)
paulson@14485
   701
  apply (simp add: inj_on_def)
paulson@14485
   702
  apply (auto simp add: image_def atLeastLessThan_def lessThan_def)
paulson@14485
   703
  apply (rule_tac x = "x - l" in exI)
paulson@14485
   704
  apply arith
paulson@14485
   705
  done
paulson@14485
   706
paulson@15418
   707
lemma card_atLeastAtMost [simp]: "card {l..u} = Suc u - l"
paulson@14485
   708
  by (subst atLeastLessThanSuc_atLeastAtMost [THEN sym], simp)
paulson@14485
   709
paulson@15418
   710
lemma card_greaterThanAtMost [simp]: "card {l<..u} = u - l"
paulson@14485
   711
  by (subst atLeastSucAtMost_greaterThanAtMost [THEN sym], simp)
paulson@14485
   712
nipkow@15045
   713
lemma card_greaterThanLessThan [simp]: "card {l<..<u} = u - Suc l"
paulson@14485
   714
  by (subst atLeastSucLessThan_greaterThanLessThan [THEN sym], simp)
paulson@14485
   715
nipkow@26105
   716
lemma ex_bij_betw_nat_finite:
nipkow@26105
   717
  "finite M \<Longrightarrow> \<exists>h. bij_betw h {0..<card M} M"
nipkow@26105
   718
apply(drule finite_imp_nat_seg_image_inj_on)
nipkow@26105
   719
apply(auto simp:atLeast0LessThan[symmetric] lessThan_def[symmetric] card_image bij_betw_def)
nipkow@26105
   720
done
nipkow@26105
   721
nipkow@26105
   722
lemma ex_bij_betw_finite_nat:
nipkow@26105
   723
  "finite M \<Longrightarrow> \<exists>h. bij_betw h M {0..<card M}"
nipkow@26105
   724
by (blast dest: ex_bij_betw_nat_finite bij_betw_inv)
nipkow@26105
   725
nipkow@31438
   726
lemma finite_same_card_bij:
nipkow@31438
   727
  "finite A \<Longrightarrow> finite B \<Longrightarrow> card A = card B \<Longrightarrow> EX h. bij_betw h A B"
nipkow@31438
   728
apply(drule ex_bij_betw_finite_nat)
nipkow@31438
   729
apply(drule ex_bij_betw_nat_finite)
nipkow@31438
   730
apply(auto intro!:bij_betw_trans)
nipkow@31438
   731
done
nipkow@31438
   732
nipkow@31438
   733
lemma ex_bij_betw_nat_finite_1:
nipkow@31438
   734
  "finite M \<Longrightarrow> \<exists>h. bij_betw h {1 .. card M} M"
nipkow@31438
   735
by (rule finite_same_card_bij) auto
nipkow@31438
   736
hoelzl@40703
   737
lemma bij_betw_iff_card:
hoelzl@40703
   738
  assumes FIN: "finite A" and FIN': "finite B"
hoelzl@40703
   739
  shows BIJ: "(\<exists>f. bij_betw f A B) \<longleftrightarrow> (card A = card B)"
hoelzl@40703
   740
using assms
hoelzl@40703
   741
proof(auto simp add: bij_betw_same_card)
hoelzl@40703
   742
  assume *: "card A = card B"
hoelzl@40703
   743
  obtain f where "bij_betw f A {0 ..< card A}"
hoelzl@40703
   744
  using FIN ex_bij_betw_finite_nat by blast
hoelzl@40703
   745
  moreover obtain g where "bij_betw g {0 ..< card B} B"
hoelzl@40703
   746
  using FIN' ex_bij_betw_nat_finite by blast
hoelzl@40703
   747
  ultimately have "bij_betw (g o f) A B"
hoelzl@40703
   748
  using * by (auto simp add: bij_betw_trans)
hoelzl@40703
   749
  thus "(\<exists>f. bij_betw f A B)" by blast
hoelzl@40703
   750
qed
hoelzl@40703
   751
hoelzl@40703
   752
lemma inj_on_iff_card_le:
hoelzl@40703
   753
  assumes FIN: "finite A" and FIN': "finite B"
hoelzl@40703
   754
  shows "(\<exists>f. inj_on f A \<and> f ` A \<le> B) = (card A \<le> card B)"
hoelzl@40703
   755
proof (safe intro!: card_inj_on_le)
hoelzl@40703
   756
  assume *: "card A \<le> card B"
hoelzl@40703
   757
  obtain f where 1: "inj_on f A" and 2: "f ` A = {0 ..< card A}"
hoelzl@40703
   758
  using FIN ex_bij_betw_finite_nat unfolding bij_betw_def by force
hoelzl@40703
   759
  moreover obtain g where "inj_on g {0 ..< card B}" and 3: "g ` {0 ..< card B} = B"
hoelzl@40703
   760
  using FIN' ex_bij_betw_nat_finite unfolding bij_betw_def by force
hoelzl@40703
   761
  ultimately have "inj_on g (f ` A)" using subset_inj_on[of g _ "f ` A"] * by force
hoelzl@40703
   762
  hence "inj_on (g o f) A" using 1 comp_inj_on by blast
hoelzl@40703
   763
  moreover
hoelzl@40703
   764
  {have "{0 ..< card A} \<le> {0 ..< card B}" using * by force
hoelzl@40703
   765
   with 2 have "f ` A  \<le> {0 ..< card B}" by blast
hoelzl@40703
   766
   hence "(g o f) ` A \<le> B" unfolding comp_def using 3 by force
hoelzl@40703
   767
  }
hoelzl@40703
   768
  ultimately show "(\<exists>f. inj_on f A \<and> f ` A \<le> B)" by blast
hoelzl@40703
   769
qed (insert assms, auto)
nipkow@26105
   770
paulson@14485
   771
subsection {* Intervals of integers *}
paulson@14485
   772
nipkow@15045
   773
lemma atLeastLessThanPlusOne_atLeastAtMost_int: "{l..<u+1} = {l..(u::int)}"
paulson@14485
   774
  by (auto simp add: atLeastAtMost_def atLeastLessThan_def)
paulson@14485
   775
paulson@15418
   776
lemma atLeastPlusOneAtMost_greaterThanAtMost_int: "{l+1..u} = {l<..(u::int)}"
paulson@14485
   777
  by (auto simp add: atLeastAtMost_def greaterThanAtMost_def)
paulson@14485
   778
paulson@15418
   779
lemma atLeastPlusOneLessThan_greaterThanLessThan_int:
paulson@15418
   780
    "{l+1..<u} = {l<..<u::int}"
paulson@14485
   781
  by (auto simp add: atLeastLessThan_def greaterThanLessThan_def)
paulson@14485
   782
paulson@14485
   783
subsubsection {* Finiteness *}
paulson@14485
   784
paulson@15418
   785
lemma image_atLeastZeroLessThan_int: "0 \<le> u ==>
nipkow@15045
   786
    {(0::int)..<u} = int ` {..<nat u}"
paulson@14485
   787
  apply (unfold image_def lessThan_def)
paulson@14485
   788
  apply auto
paulson@14485
   789
  apply (rule_tac x = "nat x" in exI)
huffman@35216
   790
  apply (auto simp add: zless_nat_eq_int_zless [THEN sym])
paulson@14485
   791
  done
paulson@14485
   792
nipkow@15045
   793
lemma finite_atLeastZeroLessThan_int: "finite {(0::int)..<u}"
paulson@14485
   794
  apply (case_tac "0 \<le> u")
paulson@14485
   795
  apply (subst image_atLeastZeroLessThan_int, assumption)
paulson@14485
   796
  apply (rule finite_imageI)
paulson@14485
   797
  apply auto
paulson@14485
   798
  done
paulson@14485
   799
nipkow@15045
   800
lemma finite_atLeastLessThan_int [iff]: "finite {l..<u::int}"
nipkow@15045
   801
  apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
paulson@14485
   802
  apply (erule subst)
paulson@14485
   803
  apply (rule finite_imageI)
paulson@14485
   804
  apply (rule finite_atLeastZeroLessThan_int)
nipkow@16733
   805
  apply (rule image_add_int_atLeastLessThan)
paulson@14485
   806
  done
paulson@14485
   807
paulson@15418
   808
lemma finite_atLeastAtMost_int [iff]: "finite {l..(u::int)}"
paulson@14485
   809
  by (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym], simp)
paulson@14485
   810
paulson@15418
   811
lemma finite_greaterThanAtMost_int [iff]: "finite {l<..(u::int)}"
paulson@14485
   812
  by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
paulson@14485
   813
paulson@15418
   814
lemma finite_greaterThanLessThan_int [iff]: "finite {l<..<u::int}"
paulson@14485
   815
  by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
paulson@14485
   816
nipkow@24853
   817
paulson@14485
   818
subsubsection {* Cardinality *}
paulson@14485
   819
nipkow@15045
   820
lemma card_atLeastZeroLessThan_int: "card {(0::int)..<u} = nat u"
paulson@14485
   821
  apply (case_tac "0 \<le> u")
paulson@14485
   822
  apply (subst image_atLeastZeroLessThan_int, assumption)
paulson@14485
   823
  apply (subst card_image)
paulson@14485
   824
  apply (auto simp add: inj_on_def)
paulson@14485
   825
  done
paulson@14485
   826
nipkow@15045
   827
lemma card_atLeastLessThan_int [simp]: "card {l..<u} = nat (u - l)"
nipkow@15045
   828
  apply (subgoal_tac "card {l..<u} = card {0..<u-l}")
paulson@14485
   829
  apply (erule ssubst, rule card_atLeastZeroLessThan_int)
nipkow@15045
   830
  apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
paulson@14485
   831
  apply (erule subst)
paulson@14485
   832
  apply (rule card_image)
paulson@14485
   833
  apply (simp add: inj_on_def)
nipkow@16733
   834
  apply (rule image_add_int_atLeastLessThan)
paulson@14485
   835
  done
paulson@14485
   836
paulson@14485
   837
lemma card_atLeastAtMost_int [simp]: "card {l..u} = nat (u - l + 1)"
nipkow@29667
   838
apply (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym])
nipkow@29667
   839
apply (auto simp add: algebra_simps)
nipkow@29667
   840
done
paulson@14485
   841
paulson@15418
   842
lemma card_greaterThanAtMost_int [simp]: "card {l<..u} = nat (u - l)"
nipkow@29667
   843
by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
paulson@14485
   844
nipkow@15045
   845
lemma card_greaterThanLessThan_int [simp]: "card {l<..<u} = nat (u - (l + 1))"
nipkow@29667
   846
by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
paulson@14485
   847
bulwahn@27656
   848
lemma finite_M_bounded_by_nat: "finite {k. P k \<and> k < (i::nat)}"
bulwahn@27656
   849
proof -
bulwahn@27656
   850
  have "{k. P k \<and> k < i} \<subseteq> {..<i}" by auto
bulwahn@27656
   851
  with finite_lessThan[of "i"] show ?thesis by (simp add: finite_subset)
bulwahn@27656
   852
qed
bulwahn@27656
   853
bulwahn@27656
   854
lemma card_less:
bulwahn@27656
   855
assumes zero_in_M: "0 \<in> M"
bulwahn@27656
   856
shows "card {k \<in> M. k < Suc i} \<noteq> 0"
bulwahn@27656
   857
proof -
bulwahn@27656
   858
  from zero_in_M have "{k \<in> M. k < Suc i} \<noteq> {}" by auto
bulwahn@27656
   859
  with finite_M_bounded_by_nat show ?thesis by (auto simp add: card_eq_0_iff)
bulwahn@27656
   860
qed
bulwahn@27656
   861
bulwahn@27656
   862
lemma card_less_Suc2: "0 \<notin> M \<Longrightarrow> card {k. Suc k \<in> M \<and> k < i} = card {k \<in> M. k < Suc i}"
haftmann@37388
   863
apply (rule card_bij_eq [of Suc _ _ "\<lambda>x. x - Suc 0"])
bulwahn@27656
   864
apply simp
bulwahn@27656
   865
apply fastsimp
bulwahn@27656
   866
apply auto
bulwahn@27656
   867
apply (rule inj_on_diff_nat)
bulwahn@27656
   868
apply auto
bulwahn@27656
   869
apply (case_tac x)
bulwahn@27656
   870
apply auto
bulwahn@27656
   871
apply (case_tac xa)
bulwahn@27656
   872
apply auto
bulwahn@27656
   873
apply (case_tac xa)
bulwahn@27656
   874
apply auto
bulwahn@27656
   875
done
bulwahn@27656
   876
bulwahn@27656
   877
lemma card_less_Suc:
bulwahn@27656
   878
  assumes zero_in_M: "0 \<in> M"
bulwahn@27656
   879
    shows "Suc (card {k. Suc k \<in> M \<and> k < i}) = card {k \<in> M. k < Suc i}"
bulwahn@27656
   880
proof -
bulwahn@27656
   881
  from assms have a: "0 \<in> {k \<in> M. k < Suc i}" by simp
bulwahn@27656
   882
  hence c: "{k \<in> M. k < Suc i} = insert 0 ({k \<in> M. k < Suc i} - {0})"
bulwahn@27656
   883
    by (auto simp only: insert_Diff)
bulwahn@27656
   884
  have b: "{k \<in> M. k < Suc i} - {0} = {k \<in> M - {0}. k < Suc i}"  by auto
bulwahn@27656
   885
  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
   886
    apply (subst card_insert)
bulwahn@27656
   887
    apply simp_all
bulwahn@27656
   888
    apply (subst b)
bulwahn@27656
   889
    apply (subst card_less_Suc2[symmetric])
bulwahn@27656
   890
    apply simp_all
bulwahn@27656
   891
    done
bulwahn@27656
   892
  with c show ?thesis by simp
bulwahn@27656
   893
qed
bulwahn@27656
   894
paulson@14485
   895
paulson@13850
   896
subsection {*Lemmas useful with the summation operator setsum*}
paulson@13850
   897
ballarin@16102
   898
text {* For examples, see Algebra/poly/UnivPoly2.thy *}
ballarin@13735
   899
wenzelm@14577
   900
subsubsection {* Disjoint Unions *}
ballarin@13735
   901
wenzelm@14577
   902
text {* Singletons and open intervals *}
ballarin@13735
   903
ballarin@13735
   904
lemma ivl_disj_un_singleton:
nipkow@15045
   905
  "{l::'a::linorder} Un {l<..} = {l..}"
nipkow@15045
   906
  "{..<u} Un {u::'a::linorder} = {..u}"
nipkow@15045
   907
  "(l::'a::linorder) < u ==> {l} Un {l<..<u} = {l..<u}"
nipkow@15045
   908
  "(l::'a::linorder) < u ==> {l<..<u} Un {u} = {l<..u}"
nipkow@15045
   909
  "(l::'a::linorder) <= u ==> {l} Un {l<..u} = {l..u}"
nipkow@15045
   910
  "(l::'a::linorder) <= u ==> {l..<u} Un {u} = {l..u}"
ballarin@14398
   911
by auto
ballarin@13735
   912
wenzelm@14577
   913
text {* One- and two-sided intervals *}
ballarin@13735
   914
ballarin@13735
   915
lemma ivl_disj_un_one:
nipkow@15045
   916
  "(l::'a::linorder) < u ==> {..l} Un {l<..<u} = {..<u}"
nipkow@15045
   917
  "(l::'a::linorder) <= u ==> {..<l} Un {l..<u} = {..<u}"
nipkow@15045
   918
  "(l::'a::linorder) <= u ==> {..l} Un {l<..u} = {..u}"
nipkow@15045
   919
  "(l::'a::linorder) <= u ==> {..<l} Un {l..u} = {..u}"
nipkow@15045
   920
  "(l::'a::linorder) <= u ==> {l<..u} Un {u<..} = {l<..}"
nipkow@15045
   921
  "(l::'a::linorder) < u ==> {l<..<u} Un {u..} = {l<..}"
nipkow@15045
   922
  "(l::'a::linorder) <= u ==> {l..u} Un {u<..} = {l..}"
nipkow@15045
   923
  "(l::'a::linorder) <= u ==> {l..<u} Un {u..} = {l..}"
ballarin@14398
   924
by auto
ballarin@13735
   925
wenzelm@14577
   926
text {* Two- and two-sided intervals *}
ballarin@13735
   927
ballarin@13735
   928
lemma ivl_disj_un_two:
nipkow@15045
   929
  "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..<u} = {l<..<u}"
nipkow@15045
   930
  "[| (l::'a::linorder) <= m; m < u |] ==> {l<..m} Un {m<..<u} = {l<..<u}"
nipkow@15045
   931
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..<u} = {l..<u}"
nipkow@15045
   932
  "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m<..<u} = {l..<u}"
nipkow@15045
   933
  "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..u} = {l<..u}"
nipkow@15045
   934
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l<..m} Un {m<..u} = {l<..u}"
nipkow@15045
   935
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..u} = {l..u}"
nipkow@15045
   936
  "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m<..u} = {l..u}"
ballarin@14398
   937
by auto
ballarin@13735
   938
ballarin@13735
   939
lemmas ivl_disj_un = ivl_disj_un_singleton ivl_disj_un_one ivl_disj_un_two
ballarin@13735
   940
wenzelm@14577
   941
subsubsection {* Disjoint Intersections *}
ballarin@13735
   942
wenzelm@14577
   943
text {* One- and two-sided intervals *}
ballarin@13735
   944
ballarin@13735
   945
lemma ivl_disj_int_one:
nipkow@15045
   946
  "{..l::'a::order} Int {l<..<u} = {}"
nipkow@15045
   947
  "{..<l} Int {l..<u} = {}"
nipkow@15045
   948
  "{..l} Int {l<..u} = {}"
nipkow@15045
   949
  "{..<l} Int {l..u} = {}"
nipkow@15045
   950
  "{l<..u} Int {u<..} = {}"
nipkow@15045
   951
  "{l<..<u} Int {u..} = {}"
nipkow@15045
   952
  "{l..u} Int {u<..} = {}"
nipkow@15045
   953
  "{l..<u} Int {u..} = {}"
ballarin@14398
   954
  by auto
ballarin@13735
   955
wenzelm@14577
   956
text {* Two- and two-sided intervals *}
ballarin@13735
   957
ballarin@13735
   958
lemma ivl_disj_int_two:
nipkow@15045
   959
  "{l::'a::order<..<m} Int {m..<u} = {}"
nipkow@15045
   960
  "{l<..m} Int {m<..<u} = {}"
nipkow@15045
   961
  "{l..<m} Int {m..<u} = {}"
nipkow@15045
   962
  "{l..m} Int {m<..<u} = {}"
nipkow@15045
   963
  "{l<..<m} Int {m..u} = {}"
nipkow@15045
   964
  "{l<..m} Int {m<..u} = {}"
nipkow@15045
   965
  "{l..<m} Int {m..u} = {}"
nipkow@15045
   966
  "{l..m} Int {m<..u} = {}"
ballarin@14398
   967
  by auto
ballarin@13735
   968
nipkow@32456
   969
lemmas ivl_disj_int = ivl_disj_int_one ivl_disj_int_two
ballarin@13735
   970
nipkow@15542
   971
subsubsection {* Some Differences *}
nipkow@15542
   972
nipkow@15542
   973
lemma ivl_diff[simp]:
nipkow@15542
   974
 "i \<le> n \<Longrightarrow> {i..<m} - {i..<n} = {n..<(m::'a::linorder)}"
nipkow@15542
   975
by(auto)
nipkow@15542
   976
nipkow@15542
   977
nipkow@15542
   978
subsubsection {* Some Subset Conditions *}
nipkow@15542
   979
blanchet@35828
   980
lemma ivl_subset [simp,no_atp]:
nipkow@15542
   981
 "({i..<j} \<subseteq> {m..<n}) = (j \<le> i | m \<le> i & j \<le> (n::'a::linorder))"
nipkow@15542
   982
apply(auto simp:linorder_not_le)
nipkow@15542
   983
apply(rule ccontr)
nipkow@15542
   984
apply(insert linorder_le_less_linear[of i n])
nipkow@15542
   985
apply(clarsimp simp:linorder_not_le)
nipkow@15542
   986
apply(fastsimp)
nipkow@15542
   987
done
nipkow@15542
   988
nipkow@15041
   989
nipkow@15042
   990
subsection {* Summation indexed over intervals *}
nipkow@15042
   991
nipkow@15042
   992
syntax
nipkow@15042
   993
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048
   994
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _..<_./ _)" [0,0,0,10] 10)
nipkow@16052
   995
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<_./ _)" [0,0,10] 10)
nipkow@16052
   996
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<=_./ _)" [0,0,10] 10)
nipkow@15042
   997
syntax (xsymbols)
nipkow@15042
   998
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048
   999
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
nipkow@16052
  1000
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
nipkow@16052
  1001
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
nipkow@15042
  1002
syntax (HTML output)
nipkow@15042
  1003
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048
  1004
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
nipkow@16052
  1005
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
nipkow@16052
  1006
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_\<le>_./ _)" [0,0,10] 10)
nipkow@15056
  1007
syntax (latex_sum output)
nipkow@15052
  1008
  "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@15052
  1009
 ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
nipkow@15052
  1010
  "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@15052
  1011
 ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
nipkow@16052
  1012
  "_upt_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@16052
  1013
 ("(3\<^raw:$\sum_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
nipkow@15052
  1014
  "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@16052
  1015
 ("(3\<^raw:$\sum_{>_ \<le> _\<^raw:}$> _)" [0,0,10] 10)
nipkow@15041
  1016
nipkow@15048
  1017
translations
nipkow@28853
  1018
  "\<Sum>x=a..b. t" == "CONST setsum (%x. t) {a..b}"
nipkow@28853
  1019
  "\<Sum>x=a..<b. t" == "CONST setsum (%x. t) {a..<b}"
nipkow@28853
  1020
  "\<Sum>i\<le>n. t" == "CONST setsum (\<lambda>i. t) {..n}"
nipkow@28853
  1021
  "\<Sum>i<n. t" == "CONST setsum (\<lambda>i. t) {..<n}"
nipkow@15041
  1022
nipkow@15052
  1023
text{* The above introduces some pretty alternative syntaxes for
nipkow@15056
  1024
summation over intervals:
nipkow@15052
  1025
\begin{center}
nipkow@15052
  1026
\begin{tabular}{lll}
nipkow@15056
  1027
Old & New & \LaTeX\\
nipkow@15056
  1028
@{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
  1029
@{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
  1030
@{term[source]"\<Sum>x\<in>{..b}. e"} & @{term"\<Sum>x\<le>b. e"} & @{term[mode=latex_sum]"\<Sum>x\<le>b. e"}\\
nipkow@15056
  1031
@{term[source]"\<Sum>x\<in>{..<b}. e"} & @{term"\<Sum>x<b. e"} & @{term[mode=latex_sum]"\<Sum>x<b. e"}
nipkow@15052
  1032
\end{tabular}
nipkow@15052
  1033
\end{center}
nipkow@15056
  1034
The left column shows the term before introduction of the new syntax,
nipkow@15056
  1035
the middle column shows the new (default) syntax, and the right column
nipkow@15056
  1036
shows a special syntax. The latter is only meaningful for latex output
nipkow@15056
  1037
and has to be activated explicitly by setting the print mode to
wenzelm@21502
  1038
@{text latex_sum} (e.g.\ via @{text "mode = latex_sum"} in
nipkow@15056
  1039
antiquotations). It is not the default \LaTeX\ output because it only
nipkow@15056
  1040
works well with italic-style formulae, not tt-style.
nipkow@15052
  1041
nipkow@15052
  1042
Note that for uniformity on @{typ nat} it is better to use
nipkow@15052
  1043
@{term"\<Sum>x::nat=0..<n. e"} rather than @{text"\<Sum>x<n. e"}: @{text setsum} may
nipkow@15052
  1044
not provide all lemmas available for @{term"{m..<n}"} also in the
nipkow@15052
  1045
special form for @{term"{..<n}"}. *}
nipkow@15052
  1046
nipkow@15542
  1047
text{* This congruence rule should be used for sums over intervals as
nipkow@15542
  1048
the standard theorem @{text[source]setsum_cong} does not work well
nipkow@15542
  1049
with the simplifier who adds the unsimplified premise @{term"x:B"} to
nipkow@15542
  1050
the context. *}
nipkow@15542
  1051
nipkow@15542
  1052
lemma setsum_ivl_cong:
nipkow@15542
  1053
 "\<lbrakk>a = c; b = d; !!x. \<lbrakk> c \<le> x; x < d \<rbrakk> \<Longrightarrow> f x = g x \<rbrakk> \<Longrightarrow>
nipkow@15542
  1054
 setsum f {a..<b} = setsum g {c..<d}"
nipkow@15542
  1055
by(rule setsum_cong, simp_all)
nipkow@15041
  1056
nipkow@16041
  1057
(* FIXME why are the following simp rules but the corresponding eqns
nipkow@16041
  1058
on intervals are not? *)
nipkow@16041
  1059
nipkow@16052
  1060
lemma setsum_atMost_Suc[simp]: "(\<Sum>i \<le> Suc n. f i) = (\<Sum>i \<le> n. f i) + f(Suc n)"
nipkow@16052
  1061
by (simp add:atMost_Suc add_ac)
nipkow@16052
  1062
nipkow@16041
  1063
lemma setsum_lessThan_Suc[simp]: "(\<Sum>i < Suc n. f i) = (\<Sum>i < n. f i) + f n"
nipkow@16041
  1064
by (simp add:lessThan_Suc add_ac)
nipkow@15041
  1065
nipkow@15911
  1066
lemma setsum_cl_ivl_Suc[simp]:
nipkow@15561
  1067
  "setsum f {m..Suc n} = (if Suc n < m then 0 else setsum f {m..n} + f(Suc n))"
nipkow@15561
  1068
by (auto simp:add_ac atLeastAtMostSuc_conv)
nipkow@15561
  1069
nipkow@15911
  1070
lemma setsum_op_ivl_Suc[simp]:
nipkow@15561
  1071
  "setsum f {m..<Suc n} = (if n < m then 0 else setsum f {m..<n} + f(n))"
nipkow@15561
  1072
by (auto simp:add_ac atLeastLessThanSuc)
nipkow@16041
  1073
(*
nipkow@15561
  1074
lemma setsum_cl_ivl_add_one_nat: "(n::nat) <= m + 1 ==>
nipkow@15561
  1075
    (\<Sum>i=n..m+1. f i) = (\<Sum>i=n..m. f i) + f(m + 1)"
nipkow@15561
  1076
by (auto simp:add_ac atLeastAtMostSuc_conv)
nipkow@16041
  1077
*)
nipkow@28068
  1078
nipkow@28068
  1079
lemma setsum_head:
nipkow@28068
  1080
  fixes n :: nat
nipkow@28068
  1081
  assumes mn: "m <= n" 
nipkow@28068
  1082
  shows "(\<Sum>x\<in>{m..n}. P x) = P m + (\<Sum>x\<in>{m<..n}. P x)" (is "?lhs = ?rhs")
nipkow@28068
  1083
proof -
nipkow@28068
  1084
  from mn
nipkow@28068
  1085
  have "{m..n} = {m} \<union> {m<..n}"
nipkow@28068
  1086
    by (auto intro: ivl_disj_un_singleton)
nipkow@28068
  1087
  hence "?lhs = (\<Sum>x\<in>{m} \<union> {m<..n}. P x)"
nipkow@28068
  1088
    by (simp add: atLeast0LessThan)
nipkow@28068
  1089
  also have "\<dots> = ?rhs" by simp
nipkow@28068
  1090
  finally show ?thesis .
nipkow@28068
  1091
qed
nipkow@28068
  1092
nipkow@28068
  1093
lemma setsum_head_Suc:
nipkow@28068
  1094
  "m \<le> n \<Longrightarrow> setsum f {m..n} = f m + setsum f {Suc m..n}"
nipkow@28068
  1095
by (simp add: setsum_head atLeastSucAtMost_greaterThanAtMost)
nipkow@28068
  1096
nipkow@28068
  1097
lemma setsum_head_upt_Suc:
nipkow@28068
  1098
  "m < n \<Longrightarrow> setsum f {m..<n} = f m + setsum f {Suc m..<n}"
huffman@30079
  1099
apply(insert setsum_head_Suc[of m "n - Suc 0" f])
nipkow@29667
  1100
apply (simp add: atLeastLessThanSuc_atLeastAtMost[symmetric] algebra_simps)
nipkow@28068
  1101
done
nipkow@28068
  1102
nipkow@31501
  1103
lemma setsum_ub_add_nat: assumes "(m::nat) \<le> n + 1"
nipkow@31501
  1104
  shows "setsum f {m..n + p} = setsum f {m..n} + setsum f {n + 1..n + p}"
nipkow@31501
  1105
proof-
nipkow@31501
  1106
  have "{m .. n+p} = {m..n} \<union> {n+1..n+p}" using `m \<le> n+1` by auto
nipkow@31501
  1107
  thus ?thesis by (auto simp: ivl_disj_int setsum_Un_disjoint
nipkow@31501
  1108
    atLeastSucAtMost_greaterThanAtMost)
nipkow@31501
  1109
qed
nipkow@28068
  1110
nipkow@15539
  1111
lemma setsum_add_nat_ivl: "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
nipkow@15539
  1112
  setsum f {m..<n} + setsum f {n..<p} = setsum f {m..<p::nat}"
nipkow@15539
  1113
by (simp add:setsum_Un_disjoint[symmetric] ivl_disj_int ivl_disj_un)
nipkow@15539
  1114
nipkow@15539
  1115
lemma setsum_diff_nat_ivl:
nipkow@15539
  1116
fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
nipkow@15539
  1117
shows "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
nipkow@15539
  1118
  setsum f {m..<p} - setsum f {m..<n} = setsum f {n..<p}"
nipkow@15539
  1119
using setsum_add_nat_ivl [of m n p f,symmetric]
nipkow@15539
  1120
apply (simp add: add_ac)
nipkow@15539
  1121
done
nipkow@15539
  1122
nipkow@31505
  1123
lemma setsum_natinterval_difff:
nipkow@31505
  1124
  fixes f:: "nat \<Rightarrow> ('a::ab_group_add)"
nipkow@31505
  1125
  shows  "setsum (\<lambda>k. f k - f(k + 1)) {(m::nat) .. n} =
nipkow@31505
  1126
          (if m <= n then f m - f(n + 1) else 0)"
nipkow@31505
  1127
by (induct n, auto simp add: algebra_simps not_le le_Suc_eq)
nipkow@31505
  1128
nipkow@31509
  1129
lemmas setsum_restrict_set' = setsum_restrict_set[unfolded Int_def]
nipkow@31509
  1130
nipkow@31509
  1131
lemma setsum_setsum_restrict:
nipkow@31509
  1132
  "finite S \<Longrightarrow> finite T \<Longrightarrow> setsum (\<lambda>x. setsum (\<lambda>y. f x y) {y. y\<in> T \<and> R x y}) S = setsum (\<lambda>y. setsum (\<lambda>x. f x y) {x. x \<in> S \<and> R x y}) T"
nipkow@31509
  1133
  by (simp add: setsum_restrict_set'[unfolded mem_def] mem_def)
nipkow@31509
  1134
     (rule setsum_commute)
nipkow@31509
  1135
nipkow@31509
  1136
lemma setsum_image_gen: assumes fS: "finite S"
nipkow@31509
  1137
  shows "setsum g S = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
nipkow@31509
  1138
proof-
nipkow@31509
  1139
  { fix x assume "x \<in> S" then have "{y. y\<in> f`S \<and> f x = y} = {f x}" by auto }
nipkow@31509
  1140
  hence "setsum g S = setsum (\<lambda>x. setsum (\<lambda>y. g x) {y. y\<in> f`S \<and> f x = y}) S"
nipkow@31509
  1141
    by simp
nipkow@31509
  1142
  also have "\<dots> = setsum (\<lambda>y. setsum g {x. x \<in> S \<and> f x = y}) (f ` S)"
nipkow@31509
  1143
    by (rule setsum_setsum_restrict[OF fS finite_imageI[OF fS]])
nipkow@31509
  1144
  finally show ?thesis .
nipkow@31509
  1145
qed
nipkow@31509
  1146
hoelzl@35171
  1147
lemma setsum_le_included:
haftmann@36307
  1148
  fixes f :: "'a \<Rightarrow> 'b::ordered_comm_monoid_add"
hoelzl@35171
  1149
  assumes "finite s" "finite t"
hoelzl@35171
  1150
  and "\<forall>y\<in>t. 0 \<le> g y" "(\<forall>x\<in>s. \<exists>y\<in>t. i y = x \<and> f x \<le> g y)"
hoelzl@35171
  1151
  shows "setsum f s \<le> setsum g t"
hoelzl@35171
  1152
proof -
hoelzl@35171
  1153
  have "setsum f s \<le> setsum (\<lambda>y. setsum g {x. x\<in>t \<and> i x = y}) s"
hoelzl@35171
  1154
  proof (rule setsum_mono)
hoelzl@35171
  1155
    fix y assume "y \<in> s"
hoelzl@35171
  1156
    with assms obtain z where z: "z \<in> t" "y = i z" "f y \<le> g z" by auto
hoelzl@35171
  1157
    with assms show "f y \<le> setsum g {x \<in> t. i x = y}" (is "?A y \<le> ?B y")
hoelzl@35171
  1158
      using order_trans[of "?A (i z)" "setsum g {z}" "?B (i z)", intro]
hoelzl@35171
  1159
      by (auto intro!: setsum_mono2)
hoelzl@35171
  1160
  qed
hoelzl@35171
  1161
  also have "... \<le> setsum (\<lambda>y. setsum g {x. x\<in>t \<and> i x = y}) (i ` t)"
hoelzl@35171
  1162
    using assms(2-4) by (auto intro!: setsum_mono2 setsum_nonneg)
hoelzl@35171
  1163
  also have "... \<le> setsum g t"
hoelzl@35171
  1164
    using assms by (auto simp: setsum_image_gen[symmetric])
hoelzl@35171
  1165
  finally show ?thesis .
hoelzl@35171
  1166
qed
hoelzl@35171
  1167
nipkow@31509
  1168
lemma setsum_multicount_gen:
nipkow@31509
  1169
  assumes "finite s" "finite t" "\<forall>j\<in>t. (card {i\<in>s. R i j} = k j)"
nipkow@31509
  1170
  shows "setsum (\<lambda>i. (card {j\<in>t. R i j})) s = setsum k t" (is "?l = ?r")
nipkow@31509
  1171
proof-
nipkow@31509
  1172
  have "?l = setsum (\<lambda>i. setsum (\<lambda>x.1) {j\<in>t. R i j}) s" by auto
nipkow@31509
  1173
  also have "\<dots> = ?r" unfolding setsum_setsum_restrict[OF assms(1-2)]
nipkow@31509
  1174
    using assms(3) by auto
nipkow@31509
  1175
  finally show ?thesis .
nipkow@31509
  1176
qed
nipkow@31509
  1177
nipkow@31509
  1178
lemma setsum_multicount:
nipkow@31509
  1179
  assumes "finite S" "finite T" "\<forall>j\<in>T. (card {i\<in>S. R i j} = k)"
nipkow@31509
  1180
  shows "setsum (\<lambda>i. card {j\<in>T. R i j}) S = k * card T" (is "?l = ?r")
nipkow@31509
  1181
proof-
nipkow@31509
  1182
  have "?l = setsum (\<lambda>i. k) T" by(rule setsum_multicount_gen)(auto simp:assms)
huffman@35216
  1183
  also have "\<dots> = ?r" by(simp add: mult_commute)
nipkow@31509
  1184
  finally show ?thesis by auto
nipkow@31509
  1185
qed
nipkow@31509
  1186
nipkow@28068
  1187
nipkow@16733
  1188
subsection{* Shifting bounds *}
nipkow@16733
  1189
nipkow@15539
  1190
lemma setsum_shift_bounds_nat_ivl:
nipkow@15539
  1191
  "setsum f {m+k..<n+k} = setsum (%i. f(i + k)){m..<n::nat}"
nipkow@15539
  1192
by (induct "n", auto simp:atLeastLessThanSuc)
nipkow@15539
  1193
nipkow@16733
  1194
lemma setsum_shift_bounds_cl_nat_ivl:
nipkow@16733
  1195
  "setsum f {m+k..n+k} = setsum (%i. f(i + k)){m..n::nat}"
nipkow@16733
  1196
apply (insert setsum_reindex[OF inj_on_add_nat, where h=f and B = "{m..n}"])
nipkow@16733
  1197
apply (simp add:image_add_atLeastAtMost o_def)
nipkow@16733
  1198
done
nipkow@16733
  1199
nipkow@16733
  1200
corollary setsum_shift_bounds_cl_Suc_ivl:
nipkow@16733
  1201
  "setsum f {Suc m..Suc n} = setsum (%i. f(Suc i)){m..n}"
huffman@30079
  1202
by (simp add:setsum_shift_bounds_cl_nat_ivl[where k="Suc 0", simplified])
nipkow@16733
  1203
nipkow@16733
  1204
corollary setsum_shift_bounds_Suc_ivl:
nipkow@16733
  1205
  "setsum f {Suc m..<Suc n} = setsum (%i. f(Suc i)){m..<n}"
huffman@30079
  1206
by (simp add:setsum_shift_bounds_nat_ivl[where k="Suc 0", simplified])
nipkow@16733
  1207
nipkow@28068
  1208
lemma setsum_shift_lb_Suc0_0:
nipkow@28068
  1209
  "f(0::nat) = (0::nat) \<Longrightarrow> setsum f {Suc 0..k} = setsum f {0..k}"
nipkow@28068
  1210
by(simp add:setsum_head_Suc)
kleing@19106
  1211
nipkow@28068
  1212
lemma setsum_shift_lb_Suc0_0_upt:
nipkow@28068
  1213
  "f(0::nat) = 0 \<Longrightarrow> setsum f {Suc 0..<k} = setsum f {0..<k}"
nipkow@28068
  1214
apply(cases k)apply simp
nipkow@28068
  1215
apply(simp add:setsum_head_upt_Suc)
nipkow@28068
  1216
done
kleing@19022
  1217
ballarin@17149
  1218
subsection {* The formula for geometric sums *}
ballarin@17149
  1219
ballarin@17149
  1220
lemma geometric_sum:
haftmann@36307
  1221
  assumes "x \<noteq> 1"
haftmann@36307
  1222
  shows "(\<Sum>i=0..<n. x ^ i) = (x ^ n - 1) / (x - 1::'a::field)"
haftmann@36307
  1223
proof -
haftmann@36307
  1224
  from assms obtain y where "y = x - 1" and "y \<noteq> 0" by simp_all
haftmann@36307
  1225
  moreover have "(\<Sum>i=0..<n. (y + 1) ^ i) = ((y + 1) ^ n - 1) / y"
haftmann@36307
  1226
  proof (induct n)
haftmann@36307
  1227
    case 0 then show ?case by simp
haftmann@36307
  1228
  next
haftmann@36307
  1229
    case (Suc n)
haftmann@36307
  1230
    moreover with `y \<noteq> 0` have "(1 + y) ^ n = (y * inverse y) * (1 + y) ^ n" by simp 
haftmann@36350
  1231
    ultimately show ?case by (simp add: field_simps divide_inverse)
haftmann@36307
  1232
  qed
haftmann@36307
  1233
  ultimately show ?thesis by simp
haftmann@36307
  1234
qed
haftmann@36307
  1235
ballarin@17149
  1236
kleing@19469
  1237
subsection {* The formula for arithmetic sums *}
kleing@19469
  1238
kleing@19469
  1239
lemma gauss_sum:
huffman@23277
  1240
  "((1::'a::comm_semiring_1) + 1)*(\<Sum>i\<in>{1..n}. of_nat i) =
kleing@19469
  1241
   of_nat n*((of_nat n)+1)"
kleing@19469
  1242
proof (induct n)
kleing@19469
  1243
  case 0
kleing@19469
  1244
  show ?case by simp
kleing@19469
  1245
next
kleing@19469
  1246
  case (Suc n)
nipkow@29667
  1247
  then show ?case by (simp add: algebra_simps)
kleing@19469
  1248
qed
kleing@19469
  1249
kleing@19469
  1250
theorem arith_series_general:
huffman@23277
  1251
  "((1::'a::comm_semiring_1) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
kleing@19469
  1252
  of_nat n * (a + (a + of_nat(n - 1)*d))"
kleing@19469
  1253
proof cases
kleing@19469
  1254
  assume ngt1: "n > 1"
kleing@19469
  1255
  let ?I = "\<lambda>i. of_nat i" and ?n = "of_nat n"
kleing@19469
  1256
  have
kleing@19469
  1257
    "(\<Sum>i\<in>{..<n}. a+?I i*d) =
kleing@19469
  1258
     ((\<Sum>i\<in>{..<n}. a) + (\<Sum>i\<in>{..<n}. ?I i*d))"
kleing@19469
  1259
    by (rule setsum_addf)
kleing@19469
  1260
  also from ngt1 have "\<dots> = ?n*a + (\<Sum>i\<in>{..<n}. ?I i*d)" by simp
kleing@19469
  1261
  also from ngt1 have "\<dots> = (?n*a + d*(\<Sum>i\<in>{1..<n}. ?I i))"
huffman@30079
  1262
    unfolding One_nat_def
nipkow@28068
  1263
    by (simp add: setsum_right_distrib atLeast0LessThan[symmetric] setsum_shift_lb_Suc0_0_upt mult_ac)
kleing@19469
  1264
  also have "(1+1)*\<dots> = (1+1)*?n*a + d*(1+1)*(\<Sum>i\<in>{1..<n}. ?I i)"
kleing@19469
  1265
    by (simp add: left_distrib right_distrib)
kleing@19469
  1266
  also from ngt1 have "{1..<n} = {1..n - 1}"
nipkow@28068
  1267
    by (cases n) (auto simp: atLeastLessThanSuc_atLeastAtMost)
nipkow@28068
  1268
  also from ngt1
kleing@19469
  1269
  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)"
huffman@30079
  1270
    by (simp only: mult_ac gauss_sum [of "n - 1"], unfold One_nat_def)
huffman@23431
  1271
       (simp add:  mult_ac trans [OF add_commute of_nat_Suc [symmetric]])
nipkow@29667
  1272
  finally show ?thesis by (simp add: algebra_simps)
kleing@19469
  1273
next
kleing@19469
  1274
  assume "\<not>(n > 1)"
kleing@19469
  1275
  hence "n = 1 \<or> n = 0" by auto
nipkow@29667
  1276
  thus ?thesis by (auto simp: algebra_simps)
kleing@19469
  1277
qed
kleing@19469
  1278
kleing@19469
  1279
lemma arith_series_nat:
kleing@19469
  1280
  "Suc (Suc 0) * (\<Sum>i\<in>{..<n}. a+i*d) = n * (a + (a+(n - 1)*d))"
kleing@19469
  1281
proof -
kleing@19469
  1282
  have
kleing@19469
  1283
    "((1::nat) + 1) * (\<Sum>i\<in>{..<n::nat}. a + of_nat(i)*d) =
kleing@19469
  1284
    of_nat(n) * (a + (a + of_nat(n - 1)*d))"
kleing@19469
  1285
    by (rule arith_series_general)
huffman@30079
  1286
  thus ?thesis
huffman@35216
  1287
    unfolding One_nat_def by auto
kleing@19469
  1288
qed
kleing@19469
  1289
kleing@19469
  1290
lemma arith_series_int:
kleing@19469
  1291
  "(2::int) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
kleing@19469
  1292
  of_nat n * (a + (a + of_nat(n - 1)*d))"
kleing@19469
  1293
proof -
kleing@19469
  1294
  have
kleing@19469
  1295
    "((1::int) + 1) * (\<Sum>i\<in>{..<n}. a + of_nat i * d) =
kleing@19469
  1296
    of_nat(n) * (a + (a + of_nat(n - 1)*d))"
kleing@19469
  1297
    by (rule arith_series_general)
kleing@19469
  1298
  thus ?thesis by simp
kleing@19469
  1299
qed
paulson@15418
  1300
kleing@19022
  1301
lemma sum_diff_distrib:
kleing@19022
  1302
  fixes P::"nat\<Rightarrow>nat"
kleing@19022
  1303
  shows
kleing@19022
  1304
  "\<forall>x. Q x \<le> P x  \<Longrightarrow>
kleing@19022
  1305
  (\<Sum>x<n. P x) - (\<Sum>x<n. Q x) = (\<Sum>x<n. P x - Q x)"
kleing@19022
  1306
proof (induct n)
kleing@19022
  1307
  case 0 show ?case by simp
kleing@19022
  1308
next
kleing@19022
  1309
  case (Suc n)
kleing@19022
  1310
kleing@19022
  1311
  let ?lhs = "(\<Sum>x<n. P x) - (\<Sum>x<n. Q x)"
kleing@19022
  1312
  let ?rhs = "\<Sum>x<n. P x - Q x"
kleing@19022
  1313
kleing@19022
  1314
  from Suc have "?lhs = ?rhs" by simp
kleing@19022
  1315
  moreover
kleing@19022
  1316
  from Suc have "?lhs + P n - Q n = ?rhs + (P n - Q n)" by simp
kleing@19022
  1317
  moreover
kleing@19022
  1318
  from Suc have
kleing@19022
  1319
    "(\<Sum>x<n. P x) + P n - ((\<Sum>x<n. Q x) + Q n) = ?rhs + (P n - Q n)"
kleing@19022
  1320
    by (subst diff_diff_left[symmetric],
kleing@19022
  1321
        subst diff_add_assoc2)
kleing@19022
  1322
       (auto simp: diff_add_assoc2 intro: setsum_mono)
kleing@19022
  1323
  ultimately
kleing@19022
  1324
  show ?case by simp
kleing@19022
  1325
qed
kleing@19022
  1326
paulson@29960
  1327
subsection {* Products indexed over intervals *}
paulson@29960
  1328
paulson@29960
  1329
syntax
paulson@29960
  1330
  "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _ = _.._./ _)" [0,0,0,10] 10)
paulson@29960
  1331
  "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _ = _..<_./ _)" [0,0,0,10] 10)
paulson@29960
  1332
  "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _<_./ _)" [0,0,10] 10)
paulson@29960
  1333
  "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(PROD _<=_./ _)" [0,0,10] 10)
paulson@29960
  1334
syntax (xsymbols)
paulson@29960
  1335
  "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _.._./ _)" [0,0,0,10] 10)
paulson@29960
  1336
  "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _..<_./ _)" [0,0,0,10] 10)
paulson@29960
  1337
  "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_<_./ _)" [0,0,10] 10)
paulson@29960
  1338
  "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_\<le>_./ _)" [0,0,10] 10)
paulson@29960
  1339
syntax (HTML output)
paulson@29960
  1340
  "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _.._./ _)" [0,0,0,10] 10)
paulson@29960
  1341
  "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_ = _..<_./ _)" [0,0,0,10] 10)
paulson@29960
  1342
  "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_<_./ _)" [0,0,10] 10)
paulson@29960
  1343
  "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Prod>_\<le>_./ _)" [0,0,10] 10)
paulson@29960
  1344
syntax (latex_prod output)
paulson@29960
  1345
  "_from_to_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
paulson@29960
  1346
 ("(3\<^raw:$\prod_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
paulson@29960
  1347
  "_from_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
paulson@29960
  1348
 ("(3\<^raw:$\prod_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
paulson@29960
  1349
  "_upt_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
paulson@29960
  1350
 ("(3\<^raw:$\prod_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
paulson@29960
  1351
  "_upto_setprod" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
paulson@29960
  1352
 ("(3\<^raw:$\prod_{>_ \<le> _\<^raw:}$> _)" [0,0,10] 10)
paulson@29960
  1353
paulson@29960
  1354
translations
paulson@29960
  1355
  "\<Prod>x=a..b. t" == "CONST setprod (%x. t) {a..b}"
paulson@29960
  1356
  "\<Prod>x=a..<b. t" == "CONST setprod (%x. t) {a..<b}"
paulson@29960
  1357
  "\<Prod>i\<le>n. t" == "CONST setprod (\<lambda>i. t) {..n}"
paulson@29960
  1358
  "\<Prod>i<n. t" == "CONST setprod (\<lambda>i. t) {..<n}"
paulson@29960
  1359
haftmann@33318
  1360
subsection {* Transfer setup *}
haftmann@33318
  1361
haftmann@33318
  1362
lemma transfer_nat_int_set_functions:
haftmann@33318
  1363
    "{..n} = nat ` {0..int n}"
haftmann@33318
  1364
    "{m..n} = nat ` {int m..int n}"  (* need all variants of these! *)
haftmann@33318
  1365
  apply (auto simp add: image_def)
haftmann@33318
  1366
  apply (rule_tac x = "int x" in bexI)
haftmann@33318
  1367
  apply auto
haftmann@33318
  1368
  apply (rule_tac x = "int x" in bexI)
haftmann@33318
  1369
  apply auto
haftmann@33318
  1370
  done
haftmann@33318
  1371
haftmann@33318
  1372
lemma transfer_nat_int_set_function_closures:
haftmann@33318
  1373
    "x >= 0 \<Longrightarrow> nat_set {x..y}"
haftmann@33318
  1374
  by (simp add: nat_set_def)
haftmann@33318
  1375
haftmann@35644
  1376
declare transfer_morphism_nat_int[transfer add
haftmann@33318
  1377
  return: transfer_nat_int_set_functions
haftmann@33318
  1378
    transfer_nat_int_set_function_closures
haftmann@33318
  1379
]
haftmann@33318
  1380
haftmann@33318
  1381
lemma transfer_int_nat_set_functions:
haftmann@33318
  1382
    "is_nat m \<Longrightarrow> is_nat n \<Longrightarrow> {m..n} = int ` {nat m..nat n}"
haftmann@33318
  1383
  by (simp only: is_nat_def transfer_nat_int_set_functions
haftmann@33318
  1384
    transfer_nat_int_set_function_closures
haftmann@33318
  1385
    transfer_nat_int_set_return_embed nat_0_le
haftmann@33318
  1386
    cong: transfer_nat_int_set_cong)
haftmann@33318
  1387
haftmann@33318
  1388
lemma transfer_int_nat_set_function_closures:
haftmann@33318
  1389
    "is_nat x \<Longrightarrow> nat_set {x..y}"
haftmann@33318
  1390
  by (simp only: transfer_nat_int_set_function_closures is_nat_def)
haftmann@33318
  1391
haftmann@35644
  1392
declare transfer_morphism_int_nat[transfer add
haftmann@33318
  1393
  return: transfer_int_nat_set_functions
haftmann@33318
  1394
    transfer_int_nat_set_function_closures
haftmann@33318
  1395
]
haftmann@33318
  1396
nipkow@8924
  1397
end