Theory Extended_Nat

Up to index of Isabelle/HOL/HOL-Multivariate_Analysis

theory Extended_Nat
imports Main
(*  Title:      HOL/Library/Extended_Nat.thy
Author: David von Oheimb, TU Muenchen; Florian Haftmann, TU Muenchen
Contributions: David Trachtenherz, TU Muenchen
*)


header {* Extended natural numbers (i.e. with infinity) *}

theory Extended_Nat
imports Main
begin


class infinity =
fixes infinity :: "'a"


notation (xsymbols)
infinity ("∞")


notation (HTML output)
infinity ("∞")


subsection {* Type definition *}

text {*
We extend the standard natural numbers by a special value indicating
infinity.
*}


typedef (open) enat = "UNIV :: nat option set" ..

definition enat :: "nat => enat" where
"enat n = Abs_enat (Some n)"


instantiation enat :: infinity
begin

definition "∞ = Abs_enat None"
instance proof qed
end

rep_datatype enat "∞ :: enat"
proof -
fix P i assume "!!j. P (enat j)" "P ∞"
then show "P i"
proof induct
case (Abs_enat y) then show ?case
by (cases y rule: option.exhaust)
(auto simp: enat_def infinity_enat_def)

qed
qed (auto simp add: enat_def infinity_enat_def Abs_enat_inject)

declare [[coercion "enat::nat=>enat"]]

lemma not_infinity_eq [iff]: "(x ≠ ∞) = (EX i. x = enat i)"
by (cases x) auto

lemma not_enat_eq [iff]: "(ALL y. x ~= enat y) = (x = ∞)"
by (cases x) auto

primrec the_enat :: "enat => nat"
where "the_enat (enat n) = n"


subsection {* Constructors and numbers *}

instantiation enat :: "{zero, one, number}"
begin


definition
"0 = enat 0"


definition
[code_unfold]: "1 = enat 1"


definition
[code_unfold, code del]: "number_of k = enat (number_of k)"


instance ..

end

definition eSuc :: "enat => enat" where
"eSuc i = (case i of enat n => enat (Suc n) | ∞ => ∞)"


lemma enat_0: "enat 0 = 0"
by (simp add: zero_enat_def)

lemma enat_1: "enat 1 = 1"
by (simp add: one_enat_def)

lemma enat_number: "enat (number_of k) = number_of k"
by (simp add: number_of_enat_def)

lemma one_eSuc: "1 = eSuc 0"
by (simp add: zero_enat_def one_enat_def eSuc_def)

lemma infinity_ne_i0 [simp]: "(∞::enat) ≠ 0"
by (simp add: zero_enat_def)

lemma i0_ne_infinity [simp]: "0 ≠ (∞::enat)"
by (simp add: zero_enat_def)

lemma zero_enat_eq [simp]:
"number_of k = (0::enat) <-> number_of k = (0::nat)"
"(0::enat) = number_of k <-> number_of k = (0::nat)"

unfolding zero_enat_def number_of_enat_def by simp_all

lemma one_enat_eq [simp]:
"number_of k = (1::enat) <-> number_of k = (1::nat)"
"(1::enat) = number_of k <-> number_of k = (1::nat)"

unfolding one_enat_def number_of_enat_def by simp_all

lemma zero_one_enat_neq [simp]:
"¬ 0 = (1::enat)"
"¬ 1 = (0::enat)"

unfolding zero_enat_def one_enat_def by simp_all

lemma infinity_ne_i1 [simp]: "(∞::enat) ≠ 1"
by (simp add: one_enat_def)

lemma i1_ne_infinity [simp]: "1 ≠ (∞::enat)"
by (simp add: one_enat_def)

lemma infinity_ne_number [simp]: "(∞::enat) ≠ number_of k"
by (simp add: number_of_enat_def)

lemma number_ne_infinity [simp]: "number_of k ≠ (∞::enat)"
by (simp add: number_of_enat_def)

lemma eSuc_enat: "eSuc (enat n) = enat (Suc n)"
by (simp add: eSuc_def)

lemma eSuc_number_of: "eSuc (number_of k) = enat (Suc (number_of k))"
by (simp add: eSuc_enat number_of_enat_def)

lemma eSuc_infinity [simp]: "eSuc ∞ = ∞"
by (simp add: eSuc_def)

lemma eSuc_ne_0 [simp]: "eSuc n ≠ 0"
by (simp add: eSuc_def zero_enat_def split: enat.splits)

lemma zero_ne_eSuc [simp]: "0 ≠ eSuc n"
by (rule eSuc_ne_0 [symmetric])

lemma eSuc_inject [simp]: "eSuc m = eSuc n <-> m = n"
by (simp add: eSuc_def split: enat.splits)

lemma number_of_enat_inject [simp]:
"(number_of k :: enat) = number_of l <-> (number_of k :: nat) = number_of l"

by (simp add: number_of_enat_def)


subsection {* Addition *}

instantiation enat :: comm_monoid_add
begin


definition [nitpick_simp]:
"m + n = (case m of ∞ => ∞ | enat m => (case n of ∞ => ∞ | enat n => enat (m + n)))"


lemma plus_enat_simps [simp, code]:
fixes q :: enat
shows "enat m + enat n = enat (m + n)"
and "∞ + q = ∞"
and "q + ∞ = ∞"

by (simp_all add: plus_enat_def split: enat.splits)

instance proof
fix n m q :: enat
show "n + m + q = n + (m + q)"
by (cases n, auto, cases m, auto, cases q, auto)
show "n + m = m + n"
by (cases n, auto, cases m, auto)
show "0 + n = n"
by (cases n) (simp_all add: zero_enat_def)
qed

end

lemma plus_enat_0 [simp]:
"0 + (q::enat) = q"
"(q::enat) + 0 = q"

by (simp_all add: plus_enat_def zero_enat_def split: enat.splits)

lemma plus_enat_number [simp]:
"(number_of k :: enat) + number_of l = (if k < Int.Pls then number_of l
else if l < Int.Pls then number_of k else number_of (k + l))"

unfolding number_of_enat_def plus_enat_simps nat_arith(1) if_distrib [symmetric, of _ enat] ..

lemma eSuc_number [simp]:
"eSuc (number_of k) = (if neg (number_of k :: int) then 1 else number_of (Int.succ k))"

unfolding eSuc_number_of
unfolding one_enat_def number_of_enat_def Suc_nat_number_of if_distrib [symmetric] ..

lemma eSuc_plus_1:
"eSuc n = n + 1"

by (cases n) (simp_all add: eSuc_enat one_enat_def)

lemma plus_1_eSuc:
"1 + q = eSuc q"
"q + 1 = eSuc q"

by (simp_all add: eSuc_plus_1 add_ac)

lemma iadd_Suc: "eSuc m + n = eSuc (m + n)"
by (simp_all add: eSuc_plus_1 add_ac)

lemma iadd_Suc_right: "m + eSuc n = eSuc (m + n)"
by (simp only: add_commute[of m] iadd_Suc)

lemma iadd_is_0: "(m + n = (0::enat)) = (m = 0 ∧ n = 0)"
by (cases m, cases n, simp_all add: zero_enat_def)

subsection {* Multiplication *}

instantiation enat :: comm_semiring_1
begin


definition times_enat_def [nitpick_simp]:
"m * n = (case m of ∞ => if n = 0 then 0 else ∞ | enat m =>
(case n of ∞ => if m = 0 then 0 else ∞ | enat n => enat (m * n)))"


lemma times_enat_simps [simp, code]:
"enat m * enat n = enat (m * n)"
"∞ * ∞ = (∞::enat)"
"∞ * enat n = (if n = 0 then 0 else ∞)"
"enat m * ∞ = (if m = 0 then 0 else ∞)"

unfolding times_enat_def zero_enat_def
by (simp_all split: enat.split)

instance proof
fix a b c :: enat
show "(a * b) * c = a * (b * c)"
unfolding times_enat_def zero_enat_def
by (simp split: enat.split)
show "a * b = b * a"
unfolding times_enat_def zero_enat_def
by (simp split: enat.split)
show "1 * a = a"
unfolding times_enat_def zero_enat_def one_enat_def
by (simp split: enat.split)
show "(a + b) * c = a * c + b * c"
unfolding times_enat_def zero_enat_def
by (simp split: enat.split add: left_distrib)
show "0 * a = 0"
unfolding times_enat_def zero_enat_def
by (simp split: enat.split)
show "a * 0 = 0"
unfolding times_enat_def zero_enat_def
by (simp split: enat.split)
show "(0::enat) ≠ 1"
unfolding zero_enat_def one_enat_def
by simp
qed

end

lemma mult_eSuc: "eSuc m * n = n + m * n"
unfolding eSuc_plus_1 by (simp add: algebra_simps)

lemma mult_eSuc_right: "m * eSuc n = m + m * n"
unfolding eSuc_plus_1 by (simp add: algebra_simps)

lemma of_nat_eq_enat: "of_nat n = enat n"
apply (induct n)
apply (simp add: enat_0)
apply (simp add: plus_1_eSuc eSuc_enat)
done

instance enat :: number_semiring
proof
fix n show "number_of (int n) = (of_nat n :: enat)"
unfolding number_of_enat_def number_of_int of_nat_id of_nat_eq_enat ..
qed

instance enat :: semiring_char_0 proof
have "inj enat" by (rule injI) simp
then show "inj (λn. of_nat n :: enat)" by (simp add: of_nat_eq_enat)
qed

lemma imult_is_0 [simp]: "((m::enat) * n = 0) = (m = 0 ∨ n = 0)"
by (auto simp add: times_enat_def zero_enat_def split: enat.split)

lemma imult_is_infinity: "((a::enat) * b = ∞) = (a = ∞ ∧ b ≠ 0 ∨ b = ∞ ∧ a ≠ 0)"
by (auto simp add: times_enat_def zero_enat_def split: enat.split)


subsection {* Subtraction *}

instantiation enat :: minus
begin


definition diff_enat_def:
"a - b = (case a of (enat x) => (case b of (enat y) => enat (x - y) | ∞ => 0)
| ∞ => ∞)"


instance ..

end

lemma idiff_enat_enat [simp,code]: "enat a - enat b = enat (a - b)"
by (simp add: diff_enat_def)

lemma idiff_infinity [simp,code]: "∞ - n = (∞::enat)"
by (simp add: diff_enat_def)

lemma idiff_infinity_right [simp,code]: "enat a - ∞ = 0"
by (simp add: diff_enat_def)

lemma idiff_0 [simp]: "(0::enat) - n = 0"
by (cases n, simp_all add: zero_enat_def)

lemmas idiff_enat_0 [simp] = idiff_0 [unfolded zero_enat_def]

lemma idiff_0_right [simp]: "(n::enat) - 0 = n"
by (cases n) (simp_all add: zero_enat_def)

lemmas idiff_enat_0_right [simp] = idiff_0_right [unfolded zero_enat_def]

lemma idiff_self [simp]: "n ≠ ∞ ==> (n::enat) - n = 0"
by (auto simp: zero_enat_def)

lemma eSuc_minus_eSuc [simp]: "eSuc n - eSuc m = n - m"
by (simp add: eSuc_def split: enat.split)

lemma eSuc_minus_1 [simp]: "eSuc n - 1 = n"
by (simp add: one_enat_def eSuc_enat[symmetric] zero_enat_def[symmetric])

(*lemmas idiff_self_eq_0_enat = idiff_self_eq_0[unfolded zero_enat_def]*)

subsection {* Ordering *}

instantiation enat :: linordered_ab_semigroup_add
begin


definition [nitpick_simp]:
"m ≤ n = (case n of enat n1 => (case m of enat m1 => m1 ≤ n1 | ∞ => False)
| ∞ => True)"


definition [nitpick_simp]:
"m < n = (case m of enat m1 => (case n of enat n1 => m1 < n1 | ∞ => True)
| ∞ => False)"


lemma enat_ord_simps [simp]:
"enat m ≤ enat n <-> m ≤ n"
"enat m < enat n <-> m < n"
"q ≤ (∞::enat)"
"q < (∞::enat) <-> q ≠ ∞"
"(∞::enat) ≤ q <-> q = ∞"
"(∞::enat) < q <-> False"

by (simp_all add: less_eq_enat_def less_enat_def split: enat.splits)

lemma enat_ord_code [code]:
"enat m ≤ enat n <-> m ≤ n"
"enat m < enat n <-> m < n"
"q ≤ (∞::enat) <-> True"
"enat m < ∞ <-> True"
"∞ ≤ enat n <-> False"
"(∞::enat) < q <-> False"

by simp_all

instance by default
(auto simp add: less_eq_enat_def less_enat_def plus_enat_def split: enat.splits)


end

instance enat :: ordered_comm_semiring
proof
fix a b c :: enat
assume "a ≤ b" and "0 ≤ c"
thus "c * a ≤ c * b"
unfolding times_enat_def less_eq_enat_def zero_enat_def
by (simp split: enat.splits)
qed

lemma enat_ord_number [simp]:
"(number_of m :: enat) ≤ number_of n <-> (number_of m :: nat) ≤ number_of n"
"(number_of m :: enat) < number_of n <-> (number_of m :: nat) < number_of n"

by (simp_all add: number_of_enat_def)

lemma i0_lb [simp]: "(0::enat) ≤ n"
by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)

lemma ile0_eq [simp]: "n ≤ (0::enat) <-> n = 0"
by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)

lemma infinity_ileE [elim!]: "∞ ≤ enat m ==> R"
by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)

lemma infinity_ilessE [elim!]: "∞ < enat m ==> R"
by simp

lemma not_iless0 [simp]: "¬ n < (0::enat)"
by (simp add: zero_enat_def less_enat_def split: enat.splits)

lemma i0_less [simp]: "(0::enat) < n <-> n ≠ 0"
by (simp add: zero_enat_def less_enat_def split: enat.splits)

lemma eSuc_ile_mono [simp]: "eSuc n ≤ eSuc m <-> n ≤ m"
by (simp add: eSuc_def less_eq_enat_def split: enat.splits)

lemma eSuc_mono [simp]: "eSuc n < eSuc m <-> n < m"
by (simp add: eSuc_def less_enat_def split: enat.splits)

lemma ile_eSuc [simp]: "n ≤ eSuc n"
by (simp add: eSuc_def less_eq_enat_def split: enat.splits)

lemma not_eSuc_ilei0 [simp]: "¬ eSuc n ≤ 0"
by (simp add: zero_enat_def eSuc_def less_eq_enat_def split: enat.splits)

lemma i0_iless_eSuc [simp]: "0 < eSuc n"
by (simp add: zero_enat_def eSuc_def less_enat_def split: enat.splits)

lemma iless_eSuc0[simp]: "(n < eSuc 0) = (n = 0)"
by (simp add: zero_enat_def eSuc_def less_enat_def split: enat.split)

lemma ileI1: "m < n ==> eSuc m ≤ n"
by (simp add: eSuc_def less_eq_enat_def less_enat_def split: enat.splits)

lemma Suc_ile_eq: "enat (Suc m) ≤ n <-> enat m < n"
by (cases n) auto

lemma iless_Suc_eq [simp]: "enat m < eSuc n <-> enat m ≤ n"
by (auto simp add: eSuc_def less_enat_def split: enat.splits)

lemma imult_infinity: "(0::enat) < n ==> ∞ * n = ∞"
by (simp add: zero_enat_def less_enat_def split: enat.splits)

lemma imult_infinity_right: "(0::enat) < n ==> n * ∞ = ∞"
by (simp add: zero_enat_def less_enat_def split: enat.splits)

lemma enat_0_less_mult_iff: "(0 < (m::enat) * n) = (0 < m ∧ 0 < n)"
by (simp only: i0_less imult_is_0, simp)

lemma mono_eSuc: "mono eSuc"
by (simp add: mono_def)


lemma min_enat_simps [simp]:
"min (enat m) (enat n) = enat (min m n)"
"min q 0 = 0"
"min 0 q = 0"
"min q (∞::enat) = q"
"min (∞::enat) q = q"

by (auto simp add: min_def)

lemma max_enat_simps [simp]:
"max (enat m) (enat n) = enat (max m n)"
"max q 0 = q"
"max 0 q = q"
"max q ∞ = (∞::enat)"
"max ∞ q = (∞::enat)"

by (simp_all add: max_def)

lemma enat_ile: "n ≤ enat m ==> ∃k. n = enat k"
by (cases n) simp_all

lemma enat_iless: "n < enat m ==> ∃k. n = enat k"
by (cases n) simp_all

lemma chain_incr: "∀i. ∃j. Y i < Y j ==> ∃j. enat k < Y j"
apply (induct_tac k)
apply (simp (no_asm) only: enat_0)
apply (fast intro: le_less_trans [OF i0_lb])
apply (erule exE)
apply (drule spec)
apply (erule exE)
apply (drule ileI1)
apply (rule eSuc_enat [THEN subst])
apply (rule exI)
apply (erule (1) le_less_trans)
done

instantiation enat :: "{bot, top}"
begin


definition bot_enat :: enat where
"bot_enat = 0"


definition top_enat :: enat where
"top_enat = ∞"


instance proof
qed (simp_all add: bot_enat_def top_enat_def)

end

lemma finite_enat_bounded:
assumes le_fin: "!!y. y ∈ A ==> y ≤ enat n"
shows "finite A"

proof (rule finite_subset)
show "finite (enat ` {..n})" by blast

have "A ⊆ {..enat n}" using le_fin by fastforce
also have "… ⊆ enat ` {..n}"
by (rule subsetI) (case_tac x, auto)
finally show "A ⊆ enat ` {..n}" .
qed


subsection {* Well-ordering *}

lemma less_enatE:
"[| n < enat m; !!k. n = enat k ==> k < m ==> P |] ==> P"

by (induct n) auto

lemma less_infinityE:
"[| n < ∞; !!k. n = enat k ==> P |] ==> P"

by (induct n) auto

lemma enat_less_induct:
assumes prem: "!!n. ∀m::enat. m < n --> P m ==> P n" shows "P n"

proof -
have P_enat: "!!k. P (enat k)"
apply (rule nat_less_induct)
apply (rule prem, clarify)
apply (erule less_enatE, simp)
done
show ?thesis
proof (induct n)
fix nat
show "P (enat nat)" by (rule P_enat)
next
show "P ∞"
apply (rule prem, clarify)
apply (erule less_infinityE)
apply (simp add: P_enat)
done
qed
qed

instance enat :: wellorder
proof
fix P and n
assume hyp: "(!!n::enat. (!!m::enat. m < n ==> P m) ==> P n)"
show "P n" by (blast intro: enat_less_induct hyp)
qed

subsection {* Complete Lattice *}

instantiation enat :: complete_lattice
begin


definition inf_enat :: "enat => enat => enat" where
"inf_enat ≡ min"


definition sup_enat :: "enat => enat => enat" where
"sup_enat ≡ max"


definition Inf_enat :: "enat set => enat" where
"Inf_enat A ≡ if A = {} then ∞ else (LEAST x. x ∈ A)"


definition Sup_enat :: "enat set => enat" where
"Sup_enat A ≡ if A = {} then 0
else if finite A then Max A
else ∞"

instance proof
fix x :: "enat" and A :: "enat set"
{ assume "x ∈ A" then show "Inf A ≤ x"
unfolding Inf_enat_def by (auto intro: Least_le) }
{ assume "!!y. y ∈ A ==> x ≤ y" then show "x ≤ Inf A"
unfolding Inf_enat_def
by (cases "A = {}") (auto intro: LeastI2_ex) }
{ assume "x ∈ A" then show "x ≤ Sup A"
unfolding Sup_enat_def by (cases "finite A") auto }
{ assume "!!y. y ∈ A ==> y ≤ x" then show "Sup A ≤ x"
unfolding Sup_enat_def using finite_enat_bounded by auto }
qed (simp_all add: inf_enat_def sup_enat_def)
end

instance enat :: complete_linorder ..

subsection {* Traditional theorem names *}

lemmas enat_defs = zero_enat_def one_enat_def number_of_enat_def eSuc_def
plus_enat_def less_eq_enat_def less_enat_def


end