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