
nipkow@8924: (*  Title:      HOL/SetInterval.thy
nipkow@8924:     ID:         $Id$
ballarin@13735:     Author:     Tobias Nipkow and Clemens Ballarin
paulson@14485:                 Additions by Jeremy Avigad in March 2004
paulson@8957:     Copyright   2000  TU Muenchen
nipkow@8924: 
ballarin@13735: lessThan, greaterThan, atLeast, atMost and two-sided intervals
nipkow@8924: *)
nipkow@8924: 
wenzelm@14577: header {* Set intervals *}
wenzelm@14577: 
nipkow@15131: theory SetInterval
nipkow@15140: imports IntArith
nipkow@15131: begin
nipkow@8924: 
nipkow@8924: constdefs
nipkow@15045:   lessThan    :: "('a::ord) => 'a set"	("(1{..<_})")
nipkow@15045:   "{..<u} == {x. x<u}"
nipkow@8924: 
wenzelm@11609:   atMost      :: "('a::ord) => 'a set"	("(1{.._})")
wenzelm@11609:   "{..u} == {x. x<=u}"
nipkow@8924: 
nipkow@15045:   greaterThan :: "('a::ord) => 'a set"	("(1{_<..})")
nipkow@15045:   "{l<..} == {x. l<x}"
nipkow@8924: 
wenzelm@11609:   atLeast     :: "('a::ord) => 'a set"	("(1{_..})")
wenzelm@11609:   "{l..} == {x. l<=x}"
nipkow@8924: 
nipkow@15045:   greaterThanLessThan :: "['a::ord, 'a] => 'a set"  ("(1{_<..<_})")
nipkow@15045:   "{l<..<u} == {l<..} Int {..<u}"
ballarin@13735: 
nipkow@15045:   atLeastLessThan :: "['a::ord, 'a] => 'a set"      ("(1{_..<_})")
nipkow@15045:   "{l..<u} == {l..} Int {..<u}"
ballarin@13735: 
nipkow@15045:   greaterThanAtMost :: "['a::ord, 'a] => 'a set"    ("(1{_<.._})")
nipkow@15045:   "{l<..u} == {l<..} Int {..u}"
ballarin@13735: 
ballarin@13735:   atLeastAtMost :: "['a::ord, 'a] => 'a set"        ("(1{_.._})")
ballarin@13735:   "{l..u} == {l..} Int {..u}"
ballarin@13735: 
nipkow@15045: (* Old syntax, will disappear! *)
nipkow@15045: syntax
nipkow@15045:   "_lessThan"    :: "('a::ord) => 'a set"	("(1{.._'(})")
nipkow@15045:   "_greaterThan" :: "('a::ord) => 'a set"	("(1{')_..})")
nipkow@15045:   "_greaterThanLessThan" :: "['a::ord, 'a] => 'a set"  ("(1{')_.._'(})")
nipkow@15045:   "_atLeastLessThan" :: "['a::ord, 'a] => 'a set"      ("(1{_.._'(})")
nipkow@15045:   "_greaterThanAtMost" :: "['a::ord, 'a] => 'a set"    ("(1{')_.._})")
nipkow@15045: translations
nipkow@15045:   "{..m(}" => "{..<m}"
nipkow@15045:   "{)m..}" => "{m<..}"
nipkow@15045:   "{)m..n(}" => "{m<..<n}"
nipkow@15045:   "{m..n(}" => "{m..<n}"
nipkow@15045:   "{)m..n}" => "{m<..n}"
nipkow@15045: 
nipkow@15048: 
nipkow@15048: text{* A note of warning when using @{term"{..<n}"} on type @{typ
nipkow@15048: nat}: it is equivalent to @{term"{0::nat..<n}"} but some lemmas involving
nipkow@15052: @{term"{m..<n}"} may not exist in @{term"{..<n}"}-form as well. *}
nipkow@15048: 
kleing@14418: syntax
kleing@14418:   "@UNION_le"   :: "nat => nat => 'b set => 'b set"       ("(3UN _<=_./ _)" 10)
kleing@14418:   "@UNION_less" :: "nat => nat => 'b set => 'b set"       ("(3UN _<_./ _)" 10)
kleing@14418:   "@INTER_le"   :: "nat => nat => 'b set => 'b set"       ("(3INT _<=_./ _)" 10)
kleing@14418:   "@INTER_less" :: "nat => nat => 'b set => 'b set"       ("(3INT _<_./ _)" 10)
kleing@14418: 
kleing@14418: syntax (input)
kleing@14418:   "@UNION_le"   :: "nat => nat => 'b set => 'b set"       ("(3\<Union> _\<le>_./ _)" 10)
kleing@14418:   "@UNION_less" :: "nat => nat => 'b set => 'b set"       ("(3\<Union> _<_./ _)" 10)
kleing@14418:   "@INTER_le"   :: "nat => nat => 'b set => 'b set"       ("(3\<Inter> _\<le>_./ _)" 10)
kleing@14418:   "@INTER_less" :: "nat => nat => 'b set => 'b set"       ("(3\<Inter> _<_./ _)" 10)
kleing@14418: 
kleing@14418: syntax (xsymbols)
wenzelm@14846:   "@UNION_le"   :: "nat \<Rightarrow> nat => 'b set => 'b set"       ("(3\<Union>(00\<^bsub>_ \<le> _\<^esub>)/ _)" 10)
wenzelm@14846:   "@UNION_less" :: "nat \<Rightarrow> nat => 'b set => 'b set"       ("(3\<Union>(00\<^bsub>_ < _\<^esub>)/ _)" 10)
wenzelm@14846:   "@INTER_le"   :: "nat \<Rightarrow> nat => 'b set => 'b set"       ("(3\<Inter>(00\<^bsub>_ \<le> _\<^esub>)/ _)" 10)
wenzelm@14846:   "@INTER_less" :: "nat \<Rightarrow> nat => 'b set => 'b set"       ("(3\<Inter>(00\<^bsub>_ < _\<^esub>)/ _)" 10)
kleing@14418: 
kleing@14418: translations
kleing@14418:   "UN i<=n. A"  == "UN i:{..n}. A"
nipkow@15045:   "UN i<n. A"   == "UN i:{..<n}. A"
kleing@14418:   "INT i<=n. A" == "INT i:{..n}. A"
nipkow@15045:   "INT i<n. A"  == "INT i:{..<n}. A"
kleing@14418: 
kleing@14418: 
paulson@14485: subsection {* Various equivalences *}
ballarin@13735: 
paulson@13850: lemma lessThan_iff [iff]: "(i: lessThan k) = (i<k)"
paulson@13850: by (simp add: lessThan_def)
ballarin@13735: 
paulson@15418: lemma Compl_lessThan [simp]:
ballarin@13735:     "!!k:: 'a::linorder. -lessThan k = atLeast k"
paulson@13850: apply (auto simp add: lessThan_def atLeast_def)
ballarin@13735: done
ballarin@13735: 
paulson@13850: lemma single_Diff_lessThan [simp]: "!!k:: 'a::order. {k} - lessThan k = {k}"
paulson@13850: by auto
ballarin@13735: 
paulson@13850: lemma greaterThan_iff [iff]: "(i: greaterThan k) = (k<i)"
paulson@13850: by (simp add: greaterThan_def)
ballarin@13735: 
paulson@15418: lemma Compl_greaterThan [simp]:
ballarin@13735:     "!!k:: 'a::linorder. -greaterThan k = atMost k"
paulson@13850: apply (simp add: greaterThan_def atMost_def le_def, auto)
ballarin@13735: done
ballarin@13735: 
paulson@13850: lemma Compl_atMost [simp]: "!!k:: 'a::linorder. -atMost k = greaterThan k"
paulson@13850: apply (subst Compl_greaterThan [symmetric])
paulson@15418: apply (rule double_complement)
ballarin@13735: done
ballarin@13735: 
paulson@13850: lemma atLeast_iff [iff]: "(i: atLeast k) = (k<=i)"
paulson@13850: by (simp add: atLeast_def)
ballarin@13735: 
paulson@15418: lemma Compl_atLeast [simp]:
ballarin@13735:     "!!k:: 'a::linorder. -atLeast k = lessThan k"
paulson@13850: apply (simp add: lessThan_def atLeast_def le_def, auto)
ballarin@13735: done
ballarin@13735: 
paulson@13850: lemma atMost_iff [iff]: "(i: atMost k) = (i<=k)"
paulson@13850: by (simp add: atMost_def)
ballarin@13735: 
paulson@14485: lemma atMost_Int_atLeast: "!!n:: 'a::order. atMost n Int atLeast n = {n}"
paulson@14485: by (blast intro: order_antisym)
paulson@13850: 
paulson@13850: 
paulson@14485: subsection {* Logical Equivalences for Set Inclusion and Equality *}
paulson@13850: 
paulson@13850: lemma atLeast_subset_iff [iff]:
paulson@15418:      "(atLeast x \<subseteq> atLeast y) = (y \<le> (x::'a::order))"
paulson@15418: by (blast intro: order_trans)
paulson@13850: 
paulson@13850: lemma atLeast_eq_iff [iff]:
paulson@15418:      "(atLeast x = atLeast y) = (x = (y::'a::linorder))"
paulson@13850: by (blast intro: order_antisym order_trans)
paulson@13850: 
paulson@13850: lemma greaterThan_subset_iff [iff]:
paulson@15418:      "(greaterThan x \<subseteq> greaterThan y) = (y \<le> (x::'a::linorder))"
paulson@15418: apply (auto simp add: greaterThan_def)
paulson@15418:  apply (subst linorder_not_less [symmetric], blast)
paulson@13850: done
paulson@13850: 
paulson@13850: lemma greaterThan_eq_iff [iff]:
paulson@15418:      "(greaterThan x = greaterThan y) = (x = (y::'a::linorder))"
paulson@15418: apply (rule iffI)
paulson@15418:  apply (erule equalityE)
paulson@15418:  apply (simp_all add: greaterThan_subset_iff)
paulson@13850: done
paulson@13850: 
paulson@15418: lemma atMost_subset_iff [iff]: "(atMost x \<subseteq> atMost y) = (x \<le> (y::'a::order))"
paulson@13850: by (blast intro: order_trans)
paulson@13850: 
paulson@15418: lemma atMost_eq_iff [iff]: "(atMost x = atMost y) = (x = (y::'a::linorder))"
paulson@13850: by (blast intro: order_antisym order_trans)
paulson@13850: 
paulson@13850: lemma lessThan_subset_iff [iff]:
paulson@15418:      "(lessThan x \<subseteq> lessThan y) = (x \<le> (y::'a::linorder))"
paulson@15418: apply (auto simp add: lessThan_def)
paulson@15418:  apply (subst linorder_not_less [symmetric], blast)
paulson@13850: done
paulson@13850: 
paulson@13850: lemma lessThan_eq_iff [iff]:
paulson@15418:      "(lessThan x = lessThan y) = (x = (y::'a::linorder))"
paulson@15418: apply (rule iffI)
paulson@15418:  apply (erule equalityE)
paulson@15418:  apply (simp_all add: lessThan_subset_iff)
ballarin@13735: done
ballarin@13735: 
ballarin@13735: 
paulson@13850: subsection {*Two-sided intervals*}
ballarin@13735: 
ballarin@13735: lemma greaterThanLessThan_iff [simp]:
nipkow@15045:   "(i : {l<..<u}) = (l < i & i < u)"
ballarin@13735: by (simp add: greaterThanLessThan_def)
ballarin@13735: 
ballarin@13735: lemma atLeastLessThan_iff [simp]:
nipkow@15045:   "(i : {l..<u}) = (l <= i & i < u)"
ballarin@13735: by (simp add: atLeastLessThan_def)
ballarin@13735: 
ballarin@13735: lemma greaterThanAtMost_iff [simp]:
nipkow@15045:   "(i : {l<..u}) = (l < i & i <= u)"
ballarin@13735: by (simp add: greaterThanAtMost_def)
ballarin@13735: 
ballarin@13735: lemma atLeastAtMost_iff [simp]:
ballarin@13735:   "(i : {l..u}) = (l <= i & i <= u)"
ballarin@13735: by (simp add: atLeastAtMost_def)
ballarin@13735: 
wenzelm@14577: text {* The above four lemmas could be declared as iffs.
wenzelm@14577:   If we do so, a call to blast in Hyperreal/Star.ML, lemma @{text STAR_Int}
wenzelm@14577:   seems to take forever (more than one hour). *}
ballarin@13735: 
nipkow@15554: subsubsection{* Emptyness and singletons *}
nipkow@15554: 
nipkow@15554: lemma atLeastAtMost_empty [simp]: "n < m ==> {m::'a::order..n} = {}";
nipkow@15554:   by (auto simp add: atLeastAtMost_def atMost_def atLeast_def);
nipkow@15554: 
nipkow@15554: lemma atLeastLessThan_empty[simp]: "n \<le> m ==> {m..<n::'a::order} = {}"
nipkow@15554: by (auto simp add: atLeastLessThan_def)
nipkow@15554: 
nipkow@15554: lemma atLeastAtMost_singleton [simp]: "{a::'a::order..a} = {a}";
nipkow@15554:   by (auto simp add: atLeastAtMost_def atMost_def atLeast_def);
paulson@14485: 
paulson@14485: subsection {* Intervals of natural numbers *}
paulson@14485: 
paulson@15047: subsubsection {* The Constant @{term lessThan} *}
paulson@15047: 
paulson@14485: lemma lessThan_0 [simp]: "lessThan (0::nat) = {}"
paulson@14485: by (simp add: lessThan_def)
paulson@14485: 
paulson@14485: lemma lessThan_Suc: "lessThan (Suc k) = insert k (lessThan k)"
paulson@14485: by (simp add: lessThan_def less_Suc_eq, blast)
paulson@14485: 
paulson@14485: lemma lessThan_Suc_atMost: "lessThan (Suc k) = atMost k"
paulson@14485: by (simp add: lessThan_def atMost_def less_Suc_eq_le)
paulson@14485: 
paulson@14485: lemma UN_lessThan_UNIV: "(UN m::nat. lessThan m) = UNIV"
paulson@14485: by blast
paulson@14485: 
paulson@15047: subsubsection {* The Constant @{term greaterThan} *}
paulson@15047: 
paulson@14485: lemma greaterThan_0 [simp]: "greaterThan 0 = range Suc"
paulson@14485: apply (simp add: greaterThan_def)
paulson@14485: apply (blast dest: gr0_conv_Suc [THEN iffD1])
paulson@14485: done
paulson@14485: 
paulson@14485: lemma greaterThan_Suc: "greaterThan (Suc k) = greaterThan k - {Suc k}"
paulson@14485: apply (simp add: greaterThan_def)
paulson@14485: apply (auto elim: linorder_neqE)
paulson@14485: done
paulson@14485: 
paulson@14485: lemma INT_greaterThan_UNIV: "(INT m::nat. greaterThan m) = {}"
paulson@14485: by blast
paulson@14485: 
paulson@15047: subsubsection {* The Constant @{term atLeast} *}
paulson@15047: 
paulson@14485: lemma atLeast_0 [simp]: "atLeast (0::nat) = UNIV"
paulson@14485: by (unfold atLeast_def UNIV_def, simp)
paulson@14485: 
paulson@14485: lemma atLeast_Suc: "atLeast (Suc k) = atLeast k - {k}"
paulson@14485: apply (simp add: atLeast_def)
paulson@14485: apply (simp add: Suc_le_eq)
paulson@14485: apply (simp add: order_le_less, blast)
paulson@14485: done
paulson@14485: 
paulson@14485: lemma atLeast_Suc_greaterThan: "atLeast (Suc k) = greaterThan k"
paulson@14485:   by (auto simp add: greaterThan_def atLeast_def less_Suc_eq_le)
paulson@14485: 
paulson@14485: lemma UN_atLeast_UNIV: "(UN m::nat. atLeast m) = UNIV"
paulson@14485: by blast
paulson@14485: 
paulson@15047: subsubsection {* The Constant @{term atMost} *}
paulson@15047: 
paulson@14485: lemma atMost_0 [simp]: "atMost (0::nat) = {0}"
paulson@14485: by (simp add: atMost_def)
paulson@14485: 
paulson@14485: lemma atMost_Suc: "atMost (Suc k) = insert (Suc k) (atMost k)"
paulson@14485: apply (simp add: atMost_def)
paulson@14485: apply (simp add: less_Suc_eq order_le_less, blast)
paulson@14485: done
paulson@14485: 
paulson@14485: lemma UN_atMost_UNIV: "(UN m::nat. atMost m) = UNIV"
paulson@14485: by blast
paulson@14485: 
paulson@15047: subsubsection {* The Constant @{term atLeastLessThan} *}
paulson@15047: 
paulson@15047: text{*But not a simprule because some concepts are better left in terms
paulson@15047:   of @{term atLeastLessThan}*}
paulson@15047: lemma atLeast0LessThan: "{0::nat..<n} = {..<n}"
nipkow@15042: by(simp add:lessThan_def atLeastLessThan_def)
nipkow@15042: 
paulson@15047: lemma atLeastLessThan0 [simp]: "{m..<0::nat} = {}"
paulson@15047: by (simp add: atLeastLessThan_def)
paulson@15047: 
paulson@15047: subsubsection {* Intervals of nats with @{term Suc} *}
paulson@15047: 
paulson@15047: text{*Not a simprule because the RHS is too messy.*}
paulson@15047: lemma atLeastLessThanSuc:
paulson@15047:     "{m..<Suc n} = (if m \<le> n then insert n {m..<n} else {})"
paulson@15418: by (auto simp add: atLeastLessThan_def)
paulson@15047: 
paulson@15418: lemma atLeastLessThan_singleton [simp]: "{m..<Suc m} = {m}"
paulson@15047: by (auto simp add: atLeastLessThan_def)
paulson@15047: 
paulson@15047: lemma atLeast_sum_LessThan [simp]: "{m + k..<k::nat} = {}"
paulson@15047: by (induct k, simp_all add: atLeastLessThanSuc)
paulson@15047: 
paulson@15047: lemma atLeastSucLessThan [simp]: "{Suc n..<n} = {}"
paulson@15047: by (auto simp add: atLeastLessThan_def)
paulson@14485: 
nipkow@15045: lemma atLeastLessThanSuc_atLeastAtMost: "{l..<Suc u} = {l..u}"
paulson@14485:   by (simp add: lessThan_Suc_atMost atLeastAtMost_def atLeastLessThan_def)
paulson@14485: 
paulson@15418: lemma atLeastSucAtMost_greaterThanAtMost: "{Suc l..u} = {l<..u}"
paulson@15418:   by (simp add: atLeast_Suc_greaterThan atLeastAtMost_def
paulson@14485:     greaterThanAtMost_def)
paulson@14485: 
paulson@15418: lemma atLeastSucLessThan_greaterThanLessThan: "{Suc l..<u} = {l<..<u}"
paulson@15418:   by (simp add: atLeast_Suc_greaterThan atLeastLessThan_def
paulson@14485:     greaterThanLessThan_def)
paulson@14485: 
nipkow@15554: lemma atLeastAtMostSuc_conv: "m \<le> Suc n \<Longrightarrow> {m..Suc n} = insert (Suc n) {m..n}"
nipkow@15554: by (auto simp add: atLeastAtMost_def)
nipkow@15554: 
paulson@14485: subsubsection {* Finiteness *}
paulson@14485: 
nipkow@15045: lemma finite_lessThan [iff]: fixes k :: nat shows "finite {..<k}"
paulson@14485:   by (induct k) (simp_all add: lessThan_Suc)
paulson@14485: 
paulson@14485: lemma finite_atMost [iff]: fixes k :: nat shows "finite {..k}"
paulson@14485:   by (induct k) (simp_all add: atMost_Suc)
paulson@14485: 
paulson@14485: lemma finite_greaterThanLessThan [iff]:
nipkow@15045:   fixes l :: nat shows "finite {l<..<u}"
paulson@14485: by (simp add: greaterThanLessThan_def)
paulson@14485: 
paulson@14485: lemma finite_atLeastLessThan [iff]:
nipkow@15045:   fixes l :: nat shows "finite {l..<u}"
paulson@14485: by (simp add: atLeastLessThan_def)
paulson@14485: 
paulson@14485: lemma finite_greaterThanAtMost [iff]:
nipkow@15045:   fixes l :: nat shows "finite {l<..u}"
paulson@14485: by (simp add: greaterThanAtMost_def)
paulson@14485: 
paulson@14485: lemma finite_atLeastAtMost [iff]:
paulson@14485:   fixes l :: nat shows "finite {l..u}"
paulson@14485: by (simp add: atLeastAtMost_def)
paulson@14485: 
paulson@14485: lemma bounded_nat_set_is_finite:
paulson@14485:     "(ALL i:N. i < (n::nat)) ==> finite N"
paulson@14485:   -- {* A bounded set of natural numbers is finite. *}
paulson@14485:   apply (rule finite_subset)
paulson@14485:    apply (rule_tac [2] finite_lessThan, auto)
paulson@14485:   done
paulson@14485: 
paulson@14485: subsubsection {* Cardinality *}
paulson@14485: 
nipkow@15045: lemma card_lessThan [simp]: "card {..<u} = u"
paulson@15251:   by (induct u, simp_all add: lessThan_Suc)
paulson@14485: 
paulson@14485: lemma card_atMost [simp]: "card {..u} = Suc u"
paulson@14485:   by (simp add: lessThan_Suc_atMost [THEN sym])
paulson@14485: 
nipkow@15045: lemma card_atLeastLessThan [simp]: "card {l..<u} = u - l"
nipkow@15045:   apply (subgoal_tac "card {l..<u} = card {..<u-l}")
paulson@14485:   apply (erule ssubst, rule card_lessThan)
nipkow@15045:   apply (subgoal_tac "(%x. x + l) ` {..<u-l} = {l..<u}")
paulson@14485:   apply (erule subst)
paulson@14485:   apply (rule card_image)
paulson@14485:   apply (simp add: inj_on_def)
paulson@14485:   apply (auto simp add: image_def atLeastLessThan_def lessThan_def)
paulson@14485:   apply arith
paulson@14485:   apply (rule_tac x = "x - l" in exI)
paulson@14485:   apply arith
paulson@14485:   done
paulson@14485: 
paulson@15418: lemma card_atLeastAtMost [simp]: "card {l..u} = Suc u - l"
paulson@14485:   by (subst atLeastLessThanSuc_atLeastAtMost [THEN sym], simp)
paulson@14485: 
paulson@15418: lemma card_greaterThanAtMost [simp]: "card {l<..u} = u - l"
paulson@14485:   by (subst atLeastSucAtMost_greaterThanAtMost [THEN sym], simp)
paulson@14485: 
nipkow@15045: lemma card_greaterThanLessThan [simp]: "card {l<..<u} = u - Suc l"
paulson@14485:   by (subst atLeastSucLessThan_greaterThanLessThan [THEN sym], simp)
paulson@14485: 
paulson@14485: subsection {* Intervals of integers *}
paulson@14485: 
nipkow@15045: lemma atLeastLessThanPlusOne_atLeastAtMost_int: "{l..<u+1} = {l..(u::int)}"
paulson@14485:   by (auto simp add: atLeastAtMost_def atLeastLessThan_def)
paulson@14485: 
paulson@15418: lemma atLeastPlusOneAtMost_greaterThanAtMost_int: "{l+1..u} = {l<..(u::int)}"
paulson@14485:   by (auto simp add: atLeastAtMost_def greaterThanAtMost_def)
paulson@14485: 
paulson@15418: lemma atLeastPlusOneLessThan_greaterThanLessThan_int:
paulson@15418:     "{l+1..<u} = {l<..<u::int}"
paulson@14485:   by (auto simp add: atLeastLessThan_def greaterThanLessThan_def)
paulson@14485: 
paulson@14485: subsubsection {* Finiteness *}
paulson@14485: 
paulson@15418: lemma image_atLeastZeroLessThan_int: "0 \<le> u ==>
nipkow@15045:     {(0::int)..<u} = int ` {..<nat u}"
paulson@14485:   apply (unfold image_def lessThan_def)
paulson@14485:   apply auto
paulson@14485:   apply (rule_tac x = "nat x" in exI)
paulson@14485:   apply (auto simp add: zless_nat_conj zless_nat_eq_int_zless [THEN sym])
paulson@14485:   done
paulson@14485: 
nipkow@15045: lemma finite_atLeastZeroLessThan_int: "finite {(0::int)..<u}"
paulson@14485:   apply (case_tac "0 \<le> u")
paulson@14485:   apply (subst image_atLeastZeroLessThan_int, assumption)
paulson@14485:   apply (rule finite_imageI)
paulson@14485:   apply auto
paulson@14485:   done
paulson@14485: 
paulson@15418: lemma image_atLeastLessThan_int_shift:
nipkow@15045:     "(%x. x + (l::int)) ` {0..<u-l} = {l..<u}"
nipkow@15561:   apply (auto simp add: image_def)
paulson@14485:   apply (rule_tac x = "x - l" in bexI)
paulson@14485:   apply auto
paulson@14485:   done
paulson@14485: 
nipkow@15045: lemma finite_atLeastLessThan_int [iff]: "finite {l..<u::int}"
nipkow@15045:   apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
paulson@14485:   apply (erule subst)
paulson@14485:   apply (rule finite_imageI)
paulson@14485:   apply (rule finite_atLeastZeroLessThan_int)
paulson@14485:   apply (rule image_atLeastLessThan_int_shift)
paulson@14485:   done
paulson@14485: 
paulson@15418: lemma finite_atLeastAtMost_int [iff]: "finite {l..(u::int)}"
paulson@14485:   by (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym], simp)
paulson@14485: 
paulson@15418: lemma finite_greaterThanAtMost_int [iff]: "finite {l<..(u::int)}"
paulson@14485:   by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
paulson@14485: 
paulson@15418: lemma finite_greaterThanLessThan_int [iff]: "finite {l<..<u::int}"
paulson@14485:   by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
paulson@14485: 
paulson@14485: subsubsection {* Cardinality *}
paulson@14485: 
nipkow@15045: lemma card_atLeastZeroLessThan_int: "card {(0::int)..<u} = nat u"
paulson@14485:   apply (case_tac "0 \<le> u")
paulson@14485:   apply (subst image_atLeastZeroLessThan_int, assumption)
paulson@14485:   apply (subst card_image)
paulson@14485:   apply (auto simp add: inj_on_def)
paulson@14485:   done
paulson@14485: 
nipkow@15045: lemma card_atLeastLessThan_int [simp]: "card {l..<u} = nat (u - l)"
nipkow@15045:   apply (subgoal_tac "card {l..<u} = card {0..<u-l}")
paulson@14485:   apply (erule ssubst, rule card_atLeastZeroLessThan_int)
nipkow@15045:   apply (subgoal_tac "(%x. x + l) ` {0..<u-l} = {l..<u}")
paulson@14485:   apply (erule subst)
paulson@14485:   apply (rule card_image)
paulson@14485:   apply (simp add: inj_on_def)
paulson@14485:   apply (rule image_atLeastLessThan_int_shift)
paulson@14485:   done
paulson@14485: 
paulson@14485: lemma card_atLeastAtMost_int [simp]: "card {l..u} = nat (u - l + 1)"
paulson@14485:   apply (subst atLeastLessThanPlusOne_atLeastAtMost_int [THEN sym])
paulson@14485:   apply (auto simp add: compare_rls)
paulson@14485:   done
paulson@14485: 
paulson@15418: lemma card_greaterThanAtMost_int [simp]: "card {l<..u} = nat (u - l)"
paulson@14485:   by (subst atLeastPlusOneAtMost_greaterThanAtMost_int [THEN sym], simp)
paulson@14485: 
nipkow@15045: lemma card_greaterThanLessThan_int [simp]: "card {l<..<u} = nat (u - (l + 1))"
paulson@14485:   by (subst atLeastPlusOneLessThan_greaterThanLessThan_int [THEN sym], simp)
paulson@14485: 
paulson@14485: 
paulson@13850: subsection {*Lemmas useful with the summation operator setsum*}
paulson@13850: 
wenzelm@14577: text {* For examples, see Algebra/poly/UnivPoly.thy *}
ballarin@13735: 
wenzelm@14577: subsubsection {* Disjoint Unions *}
ballarin@13735: 
wenzelm@14577: text {* Singletons and open intervals *}
ballarin@13735: 
ballarin@13735: lemma ivl_disj_un_singleton:
nipkow@15045:   "{l::'a::linorder} Un {l<..} = {l..}"
nipkow@15045:   "{..<u} Un {u::'a::linorder} = {..u}"
nipkow@15045:   "(l::'a::linorder) < u ==> {l} Un {l<..<u} = {l..<u}"
nipkow@15045:   "(l::'a::linorder) < u ==> {l<..<u} Un {u} = {l<..u}"
nipkow@15045:   "(l::'a::linorder) <= u ==> {l} Un {l<..u} = {l..u}"
nipkow@15045:   "(l::'a::linorder) <= u ==> {l..<u} Un {u} = {l..u}"
ballarin@14398: by auto
ballarin@13735: 
wenzelm@14577: text {* One- and two-sided intervals *}
ballarin@13735: 
ballarin@13735: lemma ivl_disj_un_one:
nipkow@15045:   "(l::'a::linorder) < u ==> {..l} Un {l<..<u} = {..<u}"
nipkow@15045:   "(l::'a::linorder) <= u ==> {..<l} Un {l..<u} = {..<u}"
nipkow@15045:   "(l::'a::linorder) <= u ==> {..l} Un {l<..u} = {..u}"
nipkow@15045:   "(l::'a::linorder) <= u ==> {..<l} Un {l..u} = {..u}"
nipkow@15045:   "(l::'a::linorder) <= u ==> {l<..u} Un {u<..} = {l<..}"
nipkow@15045:   "(l::'a::linorder) < u ==> {l<..<u} Un {u..} = {l<..}"
nipkow@15045:   "(l::'a::linorder) <= u ==> {l..u} Un {u<..} = {l..}"
nipkow@15045:   "(l::'a::linorder) <= u ==> {l..<u} Un {u..} = {l..}"
ballarin@14398: by auto
ballarin@13735: 
wenzelm@14577: text {* Two- and two-sided intervals *}
ballarin@13735: 
ballarin@13735: lemma ivl_disj_un_two:
nipkow@15045:   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..<u} = {l<..<u}"
nipkow@15045:   "[| (l::'a::linorder) <= m; m < u |] ==> {l<..m} Un {m<..<u} = {l<..<u}"
nipkow@15045:   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..<u} = {l..<u}"
nipkow@15045:   "[| (l::'a::linorder) <= m; m < u |] ==> {l..m} Un {m<..<u} = {l..<u}"
nipkow@15045:   "[| (l::'a::linorder) < m; m <= u |] ==> {l<..<m} Un {m..u} = {l<..u}"
nipkow@15045:   "[| (l::'a::linorder) <= m; m <= u |] ==> {l<..m} Un {m<..u} = {l<..u}"
nipkow@15045:   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..<m} Un {m..u} = {l..u}"
nipkow@15045:   "[| (l::'a::linorder) <= m; m <= u |] ==> {l..m} Un {m<..u} = {l..u}"
ballarin@14398: by auto
ballarin@13735: 
ballarin@13735: lemmas ivl_disj_un = ivl_disj_un_singleton ivl_disj_un_one ivl_disj_un_two
ballarin@13735: 
wenzelm@14577: subsubsection {* Disjoint Intersections *}
ballarin@13735: 
wenzelm@14577: text {* Singletons and open intervals *}
ballarin@13735: 
ballarin@13735: lemma ivl_disj_int_singleton:
nipkow@15045:   "{l::'a::order} Int {l<..} = {}"
nipkow@15045:   "{..<u} Int {u} = {}"
nipkow@15045:   "{l} Int {l<..<u} = {}"
nipkow@15045:   "{l<..<u} Int {u} = {}"
nipkow@15045:   "{l} Int {l<..u} = {}"
nipkow@15045:   "{l..<u} Int {u} = {}"
ballarin@13735:   by simp+
ballarin@13735: 
wenzelm@14577: text {* One- and two-sided intervals *}
ballarin@13735: 
ballarin@13735: lemma ivl_disj_int_one:
nipkow@15045:   "{..l::'a::order} Int {l<..<u} = {}"
nipkow@15045:   "{..<l} Int {l..<u} = {}"
nipkow@15045:   "{..l} Int {l<..u} = {}"
nipkow@15045:   "{..<l} Int {l..u} = {}"
nipkow@15045:   "{l<..u} Int {u<..} = {}"
nipkow@15045:   "{l<..<u} Int {u..} = {}"
nipkow@15045:   "{l..u} Int {u<..} = {}"
nipkow@15045:   "{l..<u} Int {u..} = {}"
ballarin@14398:   by auto
ballarin@13735: 
wenzelm@14577: text {* Two- and two-sided intervals *}
ballarin@13735: 
ballarin@13735: lemma ivl_disj_int_two:
nipkow@15045:   "{l::'a::order<..<m} Int {m..<u} = {}"
nipkow@15045:   "{l<..m} Int {m<..<u} = {}"
nipkow@15045:   "{l..<m} Int {m..<u} = {}"
nipkow@15045:   "{l..m} Int {m<..<u} = {}"
nipkow@15045:   "{l<..<m} Int {m..u} = {}"
nipkow@15045:   "{l<..m} Int {m<..u} = {}"
nipkow@15045:   "{l..<m} Int {m..u} = {}"
nipkow@15045:   "{l..m} Int {m<..u} = {}"
ballarin@14398:   by auto
ballarin@13735: 
ballarin@13735: lemmas ivl_disj_int = ivl_disj_int_singleton ivl_disj_int_one ivl_disj_int_two
ballarin@13735: 
nipkow@15542: subsubsection {* Some Differences *}
nipkow@15542: 
nipkow@15542: lemma ivl_diff[simp]:
nipkow@15542:  "i \<le> n \<Longrightarrow> {i..<m} - {i..<n} = {n..<(m::'a::linorder)}"
nipkow@15542: by(auto)
nipkow@15542: 
nipkow@15542: 
nipkow@15542: subsubsection {* Some Subset Conditions *}
nipkow@15542: 
nipkow@15542: lemma ivl_subset[simp]:
nipkow@15542:  "({i..<j} \<subseteq> {m..<n}) = (j \<le> i | m \<le> i & j \<le> (n::'a::linorder))"
nipkow@15542: apply(auto simp:linorder_not_le)
nipkow@15542: apply(rule ccontr)
nipkow@15542: apply(insert linorder_le_less_linear[of i n])
nipkow@15542: apply(clarsimp simp:linorder_not_le)
nipkow@15542: apply(fastsimp)
nipkow@15542: done
nipkow@15542: 
nipkow@15041: 
nipkow@15042: subsection {* Summation indexed over intervals *}
nipkow@15042: 
nipkow@15042: syntax
nipkow@15042:   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048:   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _ = _..<_./ _)" [0,0,0,10] 10)
nipkow@15048:   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(SUM _<_./ _)" [0,0,10] 10)
nipkow@15042: syntax (xsymbols)
nipkow@15042:   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048:   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
nipkow@15048:   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
nipkow@15042: syntax (HTML output)
nipkow@15042:   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _.._./ _)" [0,0,0,10] 10)
nipkow@15048:   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_ = _..<_./ _)" [0,0,0,10] 10)
nipkow@15048:   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b" ("(3\<Sum>_<_./ _)" [0,0,10] 10)
nipkow@15056: syntax (latex_sum output)
nipkow@15052:   "_from_to_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@15052:  ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{>_\<^raw:}$> _)" [0,0,0,10] 10)
nipkow@15052:   "_from_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@15052:  ("(3\<^raw:$\sum_{>_ = _\<^raw:}^{<>_\<^raw:}$> _)" [0,0,0,10] 10)
nipkow@15052:   "_upto_setsum" :: "idt \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@15052:  ("(3\<^raw:$\sum_{>_ < _\<^raw:}$> _)" [0,0,10] 10)
nipkow@15041: 
nipkow@15048: translations
nipkow@15048:   "\<Sum>x=a..b. t" == "setsum (%x. t) {a..b}"
nipkow@15048:   "\<Sum>x=a..<b. t" == "setsum (%x. t) {a..<b}"
nipkow@15048:   "\<Sum>i<n. t" == "setsum (\<lambda>i. t) {..<n}"
nipkow@15041: 
nipkow@15052: text{* The above introduces some pretty alternative syntaxes for
nipkow@15056: summation over intervals:
nipkow@15052: \begin{center}
nipkow@15052: \begin{tabular}{lll}
nipkow@15056: Old & New & \LaTeX\\
nipkow@15056: @{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: @{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: @{term[source]"\<Sum>x\<in>{..<b}. e"} & @{term"\<Sum>x<b. e"} & @{term[mode=latex_sum]"\<Sum>x<b. e"}
nipkow@15052: \end{tabular}
nipkow@15052: \end{center}
nipkow@15056: The left column shows the term before introduction of the new syntax,
nipkow@15056: the middle column shows the new (default) syntax, and the right column
nipkow@15056: shows a special syntax. The latter is only meaningful for latex output
nipkow@15056: and has to be activated explicitly by setting the print mode to
nipkow@15056: \texttt{latex\_sum} (e.g.\ via \texttt{mode=latex\_sum} in
nipkow@15056: antiquotations). It is not the default \LaTeX\ output because it only
nipkow@15056: works well with italic-style formulae, not tt-style.
nipkow@15052: 
nipkow@15052: Note that for uniformity on @{typ nat} it is better to use
nipkow@15052: @{term"\<Sum>x::nat=0..<n. e"} rather than @{text"\<Sum>x<n. e"}: @{text setsum} may
nipkow@15052: not provide all lemmas available for @{term"{m..<n}"} also in the
nipkow@15052: special form for @{term"{..<n}"}. *}
nipkow@15052: 
nipkow@15542: (* FIXME change the simplifier's treatment of congruence rules?? *)
nipkow@15542: 
nipkow@15542: text{* This congruence rule should be used for sums over intervals as
nipkow@15542: the standard theorem @{text[source]setsum_cong} does not work well
nipkow@15542: with the simplifier who adds the unsimplified premise @{term"x:B"} to
nipkow@15542: the context. *}
nipkow@15542: 
nipkow@15542: lemma setsum_ivl_cong:
nipkow@15542:  "\<lbrakk>a = c; b = d; !!x. \<lbrakk> c \<le> x; x < d \<rbrakk> \<Longrightarrow> f x = g x \<rbrakk> \<Longrightarrow>
nipkow@15542:  setsum f {a..<b} = setsum g {c..<d}"
nipkow@15542: by(rule setsum_cong, simp_all)
nipkow@15041: 
nipkow@15554: (* FIXME delete
nipkow@15041: lemma Summation_Suc[simp]: "(\<Sum>i < Suc n. b i) = b n + (\<Sum>i < n. b i)"
nipkow@15041: by (simp add:lessThan_Suc)
nipkow@15554: *)
nipkow@15041: 
nipkow@15561: lemma setsum_cl_ivl_Suc:
nipkow@15561:   "setsum f {m..Suc n} = (if Suc n < m then 0 else setsum f {m..n} + f(Suc n))"
nipkow@15561: by (auto simp:add_ac atLeastAtMostSuc_conv)
nipkow@15561: 
nipkow@15561: lemma setsum_op_ivl_Suc:
nipkow@15561:   "setsum f {m..<Suc n} = (if n < m then 0 else setsum f {m..<n} + f(n))"
nipkow@15561: by (auto simp:add_ac atLeastLessThanSuc)
nipkow@15561: 
nipkow@15561: lemma setsum_cl_ivl_add_one_nat: "(n::nat) <= m + 1 ==>
nipkow@15561:     (\<Sum>i=n..m+1. f i) = (\<Sum>i=n..m. f i) + f(m + 1)"
nipkow@15561: by (auto simp:add_ac atLeastAtMostSuc_conv)
nipkow@15561: 
nipkow@15539: lemma setsum_add_nat_ivl: "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
nipkow@15539:   setsum f {m..<n} + setsum f {n..<p} = setsum f {m..<p::nat}"
nipkow@15539: by (simp add:setsum_Un_disjoint[symmetric] ivl_disj_int ivl_disj_un)
nipkow@15539: 
nipkow@15539: lemma setsum_diff_nat_ivl:
nipkow@15539: fixes f :: "nat \<Rightarrow> 'a::ab_group_add"
nipkow@15539: shows "\<lbrakk> m \<le> n; n \<le> p \<rbrakk> \<Longrightarrow>
nipkow@15539:   setsum f {m..<p} - setsum f {m..<n} = setsum f {n..<p}"
nipkow@15539: using setsum_add_nat_ivl [of m n p f,symmetric]
nipkow@15539: apply (simp add: add_ac)
nipkow@15539: done
nipkow@15539: 
nipkow@15539: lemma setsum_shift_bounds_nat_ivl:
nipkow@15539:   "setsum f {m+k..<n+k} = setsum (%i. f(i + k)){m..<n::nat}"
nipkow@15539: by (induct "n", auto simp:atLeastLessThanSuc)
nipkow@15539: 
paulson@15418: 
paulson@15418: ML
paulson@15418: {*
paulson@15418: val Compl_atLeast = thm "Compl_atLeast";
paulson@15418: val Compl_atMost = thm "Compl_atMost";
paulson@15418: val Compl_greaterThan = thm "Compl_greaterThan";
paulson@15418: val Compl_lessThan = thm "Compl_lessThan";
paulson@15418: val INT_greaterThan_UNIV = thm "INT_greaterThan_UNIV";
paulson@15418: val UN_atLeast_UNIV = thm "UN_atLeast_UNIV";
paulson@15418: val UN_atMost_UNIV = thm "UN_atMost_UNIV";
paulson@15418: val UN_lessThan_UNIV = thm "UN_lessThan_UNIV";
paulson@15418: val atLeastAtMost_def = thm "atLeastAtMost_def";
paulson@15418: val atLeastAtMost_iff = thm "atLeastAtMost_iff";
paulson@15418: val atLeastLessThan_def  = thm "atLeastLessThan_def";
paulson@15418: val atLeastLessThan_iff = thm "atLeastLessThan_iff";
paulson@15418: val atLeast_0 = thm "atLeast_0";
paulson@15418: val atLeast_Suc = thm "atLeast_Suc";
paulson@15418: val atLeast_def      = thm "atLeast_def";
paulson@15418: val atLeast_iff = thm "atLeast_iff";
paulson@15418: val atMost_0 = thm "atMost_0";
paulson@15418: val atMost_Int_atLeast = thm "atMost_Int_atLeast";
paulson@15418: val atMost_Suc = thm "atMost_Suc";
paulson@15418: val atMost_def       = thm "atMost_def";
paulson@15418: val atMost_iff = thm "atMost_iff";
paulson@15418: val greaterThanAtMost_def  = thm "greaterThanAtMost_def";
paulson@15418: val greaterThanAtMost_iff = thm "greaterThanAtMost_iff";
paulson@15418: val greaterThanLessThan_def  = thm "greaterThanLessThan_def";
paulson@15418: val greaterThanLessThan_iff = thm "greaterThanLessThan_iff";
paulson@15418: val greaterThan_0 = thm "greaterThan_0";
paulson@15418: val greaterThan_Suc = thm "greaterThan_Suc";
paulson@15418: val greaterThan_def  = thm "greaterThan_def";
paulson@15418: val greaterThan_iff = thm "greaterThan_iff";
paulson@15418: val ivl_disj_int = thms "ivl_disj_int";
paulson@15418: val ivl_disj_int_one = thms "ivl_disj_int_one";
paulson@15418: val ivl_disj_int_singleton = thms "ivl_disj_int_singleton";
paulson@15418: val ivl_disj_int_two = thms "ivl_disj_int_two";
paulson@15418: val ivl_disj_un = thms "ivl_disj_un";
paulson@15418: val ivl_disj_un_one = thms "ivl_disj_un_one";
paulson@15418: val ivl_disj_un_singleton = thms "ivl_disj_un_singleton";
paulson@15418: val ivl_disj_un_two = thms "ivl_disj_un_two";
paulson@15418: val lessThan_0 = thm "lessThan_0";
paulson@15418: val lessThan_Suc = thm "lessThan_Suc";
paulson@15418: val lessThan_Suc_atMost = thm "lessThan_Suc_atMost";
paulson@15418: val lessThan_def     = thm "lessThan_def";
paulson@15418: val lessThan_iff = thm "lessThan_iff";
paulson@15418: val single_Diff_lessThan = thm "single_Diff_lessThan";
paulson@15418: 
paulson@15418: val bounded_nat_set_is_finite = thm "bounded_nat_set_is_finite";
paulson@15418: val finite_atMost = thm "finite_atMost";
paulson@15418: val finite_lessThan = thm "finite_lessThan";
paulson@15418: *}
paulson@15418: 
nipkow@8924: end




