lemma num_induct [case_names One inc]: fixes P :: \<open>num \<Rightarrow> bool\<close> assumes One: \<open>P One\<close> and inc: \<open>\<And>x. P x \<Longrightarrow> P (inc x)\<close> shows\<open>P x\<close> proof - obtain n where n: \<open>Suc n = nat_of_num x\<close> by (cases \<open>nat_of_num x\<close>) (simp_all add: nat_of_num_neq_0) have\<open>P (num_of_nat (Suc n))\<close> proof (induct n) case 0 from One show ?caseby simp next case (Suc n) thenhave\<open>P (inc (num_of_nat (Suc n)))\<close> by (rule inc) thenshow\<open>P (num_of_nat (Suc (Suc n)))\<close> by simp qed with n show\<open>P x\<close> by (simp add: nat_of_num_inverse) qed
text\<open> From now on, there are two possible models for\<^typ>\<open>num\<close>: as positive
naturals (rule \<open>num_induct\<close>) and as digit representation (rules \<open>num.induct\<close>, \<open>num.cases\<close>). \<close>
subsection \<open>Numeral operations\<close>
instantiation num :: \<open>{plus,times,linorder}\<close> begin
definition [code del]: \<open>m + n = num_of_nat (nat_of_num m + nat_of_num n)\<close>
definition [code del]: \<open>m * n = num_of_nat (nat_of_num m * nat_of_num n)\<close>
definition [code del]: \<open>m \<le> n \<longleftrightarrow> nat_of_num m \<le> nat_of_num n\<close>
definition [code del]: \<open>m < n \<longleftrightarrow> nat_of_num m < nat_of_num n\<close>
instance by standard (auto simp add: less_num_def less_eq_num_def num_eq_iff)
end
lemma nat_of_num_add: \<open>nat_of_num (x + y) = nat_of_num x + nat_of_num y\<close> unfolding plus_num_def by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
lemma nat_of_num_mult: \<open>nat_of_num (x * y) = nat_of_num x * nat_of_num y\<close> unfolding times_num_def by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
lemma add_num_simps [simp, code]: \<open>One + One = Bit0 One\<close> \<open>One + Bit0 n = Bit1 n\<close> \<open>One + Bit1 n = Bit0 (n + One)\<close> \<open>Bit0 m + One = Bit1 m\<close> \<open>Bit0 m + Bit0 n = Bit0 (m + n)\<close> \<open>Bit0 m + Bit1 n = Bit1 (m + n)\<close> \<open>Bit1 m + One = Bit0 (m + One)\<close> \<open>Bit1 m + Bit0 n = Bit1 (m + n)\<close> \<open>Bit1 m + Bit1 n = Bit0 (m + n + One)\<close> by (simp_all add: num_eq_iff nat_of_num_add)
lemma mult_num_simps [simp, code]: \<open>m * One = m\<close> \<open>One * n = n\<close> \<open>Bit0 m * Bit0 n = Bit0 (Bit0 (m * n))\<close> \<open>Bit0 m * Bit1 n = Bit0 (m * Bit1 n)\<close> \<open>Bit1 m * Bit0 n = Bit0 (Bit1 m * n)\<close> \<open>Bit1 m * Bit1 n = Bit1 (m + n + Bit0 (m * n))\<close> by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult distrib_right distrib_left)
lemma eq_num_simps: \<open>One = One \<longleftrightarrow> True\<close> \<open>One = Bit0 n \<longleftrightarrow> False\<close> \<open>One = Bit1 n \<longleftrightarrow> False\<close> \<open>Bit0 m = One \<longleftrightarrow> False\<close> \<open>Bit1 m = One \<longleftrightarrow> False\<close> \<open>Bit0 m = Bit0 n \<longleftrightarrow> m = n\<close> \<open>Bit0 m = Bit1 n \<longleftrightarrow> False\<close> \<open>Bit1 m = Bit0 n \<longleftrightarrow> False\<close> \<open>Bit1 m = Bit1 n \<longleftrightarrow> m = n\<close> by simp_all
lemma le_num_simps [simp, code]: \<open>One \<le> n \<longleftrightarrow> True\<close> \<open>Bit0 m \<le> One \<longleftrightarrow> False\<close> \<open>Bit1 m \<le> One \<longleftrightarrow> False\<close> \<open>Bit0 m \<le> Bit0 n \<longleftrightarrow> m \<le> n\<close> \<open>Bit0 m \<le> Bit1 n \<longleftrightarrow> m \<le> n\<close> \<open>Bit1 m \<le> Bit1 n \<longleftrightarrow> m \<le> n\<close> \<open>Bit1 m \<le> Bit0 n \<longleftrightarrow> m < n\<close> using nat_of_num_pos [of n] nat_of_num_pos [of m] by (auto simp add: less_eq_num_def less_num_def)
lemma less_num_simps [simp, code]: \<open>m < One \<longleftrightarrow> False\<close> \<open>One < Bit0 n \<longleftrightarrow> True\<close> \<open>One < Bit1 n \<longleftrightarrow> True\<close> \<open>Bit0 m < Bit0 n \<longleftrightarrow> m < n\<close> \<open>Bit0 m < Bit1 n \<longleftrightarrow> m \<le> n\<close> \<open>Bit1 m < Bit1 n \<longleftrightarrow> m < n\<close> \<open>Bit1 m < Bit0 n \<longleftrightarrow> m < n\<close> using nat_of_num_pos [of n] nat_of_num_pos [of m] by (auto simp add: less_eq_num_def less_num_def)
lemma le_num_One_iff: \<open>x \<le> One \<longleftrightarrow> x = One\<close> by (simp add: antisym_conv)
text\<open>Rules using \<open>One\<close> and \<open>inc\<close> as constructors.\<close>
lemma add_One: \<open>x + One = inc x\<close> by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
lemma add_One_commute: \<open>One + n = n + One\<close> by (induct n) simp_all
lemma add_inc: \<open>x + inc y = inc (x + y)\<close> by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
lemma mult_inc: \<open>x * inc y = x * y + x\<close> by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
primrec pow :: \<open>num \<Rightarrow> num \<Rightarrow> num\<close> where \<open>pow x One = x\<close>
| \<open>pow x (Bit0 y) = sqr (pow x y)\<close>
| \<open>pow x (Bit1 y) = sqr (pow x y) * x\<close>
lemma nat_of_num_sqr: \<open>nat_of_num (sqr x) = nat_of_num x * nat_of_num x\<close> by (induct x) (simp_all add: algebra_simps nat_of_num_add)
lemma sqr_conv_mult: \<open>sqr x = x * x\<close> by (simp add: num_eq_iff nat_of_num_sqr nat_of_num_mult)
lemma num_double [simp]: \<open>Bit0 num.One * n = Bit0 n\<close> by (simp add: num_eq_iff nat_of_num_mult)
subsection \<open>Binary numerals\<close>
text\<open>
We embed binary representations into a generic algebraic structureusing\<open>numeral\<close>. \<close>
class numeral = one + semigroup_add begin
primrec numeral :: \<open>num \<Rightarrow> 'a\<close> where
numeral_One: \<open>numeral One = 1\<close>
| numeral_Bit0: \<open>numeral (Bit0 n) = numeral n + numeral n\<close>
| numeral_Bit1: \<open>numeral (Bit1 n) = numeral n + numeral n + 1\<close>
lemma numeral_code [code]: \<open>numeral One = 1\<close> \<open>numeral (Bit0 n) = (let m = numeral n in m + m)\<close> \<open>numeral (Bit1 n) = (let m = numeral n in m + m + 1)\<close> by (simp_all add: Let_def)
lemma one_plus_numeral_commute: \<open>1 + numeral x = numeral x + 1\<close> proof (induct x) case One thenshow ?caseby simp next case Bit0 thenshow ?caseby (simp add: add.assoc [symmetric]) (simp add: add.assoc) next case Bit1 thenshow ?caseby (simp add: add.assoc [symmetric]) (simp add: add.assoc) qed
lemma numeral_inc: \<open>numeral (inc x) = numeral x + 1\<close> proof (induct x) case One thenshow ?caseby simp next case Bit0 thenshow ?caseby simp next case (Bit1 x) have\<open>numeral x + (1 + numeral x) + 1 = numeral x + (numeral x + 1) + 1\<close> by (simp only: one_plus_numeral_commute) with Bit1 show ?case by (simp add: add.assoc) qed
parse_translation\<open> let fun numeral_tr [(c as Const (\<^syntax_const>\<open>_constrain\<close>, _)) $ t $ u] =
c $ numeral_tr [t] $ u
| numeral_tr [Const (num, _)] =
(Numeral.mk_number_syntax o #value o Lexicon.read_num) num
| numeral_tr ts = raise TERM ("numeral_tr", ts); in [(\<^syntax_const>\<open>_Numeral\<close>, K numeral_tr)] end \<close>
typed_print_translation\<open> let fun num_tr' ctxt T [n] = let
val k = Numeral.dest_num_syntax n;
val t' = Syntax.const \<^syntax_const>\<open>_Numeral\<close> $ Syntax.free (string_of_int k); in
(case T of
Type (\<^type_name>\<open>fun\<close>, [_, T']) => if Printer.type_emphasis ctxt T' then Syntax.const \<^syntax_const>\<open>_constrain\<close> $ t' $
Syntax_Phases.term_of_typ ctxt T'
else t'
| _ => if T = dummyT then t' else raise Match) end; in
[(\<^const_syntax>\<open>numeral\<close>, num_tr')] end \<close>
lemma is_num_add_commute: \<open>is_num x \<Longrightarrow> is_num y \<Longrightarrow> x + y = y + x\<close> proof(induction x rule: is_num.induct) case 1 thenshow ?case proof (induction y rule: is_num.induct) case 1 thenshow ?caseby simp next case (2 y) thenhave\<open>y + (1 + - y) + y = y + (- y + 1) + y\<close> by (simp add: add.assoc) thenhave\<open>y + (1 + - y) = y + (- y + 1)\<close> by simp thenshow ?case by (rule add_left_imp_eq[of y]) next case (3 x y) thenhave\<open>1 + (x + y) = x + 1 + y\<close> by (simp add: add.assoc [symmetric]) thenshow ?caseusing 3 by (simp add: add.assoc) qed next case (2 x) thenhave\<open>x + (- x + y) + x = x + (y + - x) + x\<close> by (simp add: add.assoc) thenhave\<open>x + (- x + y) = x + (y + - x)\<close> by simp thenshow ?case by (rule add_left_imp_eq[of x]) next case (3 x z) moreoverhave\<open>x + (y + z) = (x + y) + z\<close> by (simp add: add.assoc[symmetric]) ultimatelyshow ?case by (simp add: add.assoc) qed
lemma is_num_add_left_commute: \<open>is_num x \<Longrightarrow> is_num y \<Longrightarrow> x + (y + z) = y + (x + z)\<close> by (simp only: add.assoc [symmetric] is_num_add_commute)
lemma sub_num_simps [simp]: \<open>sub One One = 0\<close> \<open>sub One (Bit0 l) = - numeral (BitM l)\<close> \<open>sub One (Bit1 l) = - numeral (Bit0 l)\<close> \<open>sub (Bit0 k) One = numeral (BitM k)\<close> \<open>sub (Bit1 k) One = numeral (Bit0 k)\<close> \<open>sub (Bit0 k) (Bit0 l) = dbl (sub k l)\<close> \<open>sub (Bit0 k) (Bit1 l) = dbl_dec (sub k l)\<close> \<open>sub (Bit1 k) (Bit0 l) = dbl_inc (sub k l)\<close> \<open>sub (Bit1 k) (Bit1 l) = dbl (sub k l)\<close> by (simp_all add: dbl_def dbl_dec_def dbl_inc_def sub_def numeral.simps
numeral_BitM is_num_normalize del: add_uminus_conv_diff add: diff_conv_add_uminus)
lemma add_neg_numeral_simps: \<open>numeral m + - numeral n = sub m n\<close> \<open>- numeral m + numeral n = sub n m\<close> \<open>- numeral m + - numeral n = - (numeral m + numeral n)\<close> by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize
del: add_uminus_conv_diff add: diff_conv_add_uminus)
lemma add_neg_numeral_special: \<open>1 + - numeral m = sub One m\<close> \<open>- numeral m + 1 = sub One m\<close> \<open>numeral m + - 1 = sub m One\<close> \<open>- 1 + numeral n = sub n One\<close> \<open>- 1 + - numeral n = - numeral (inc n)\<close> \<open>- numeral m + - 1 = - numeral (inc m)\<close> \<open>1 + - 1 = 0\<close> \<open>- 1 + 1 = 0\<close> \<open>- 1 + - 1 = - 2\<close> by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize right_minus numeral_inc
del: add_uminus_conv_diff add: diff_conv_add_uminus)
lemma diff_numeral_simps: \<open>numeral m - numeral n = sub m n\<close> \<open>numeral m - - numeral n = numeral (m + n)\<close> \<open>- numeral m - numeral n = - numeral (m + n)\<close> \<open>- numeral m - - numeral n = sub n m\<close> by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize
del: add_uminus_conv_diff add: diff_conv_add_uminus)
lemma diff_numeral_special: \<open>1 - numeral n = sub One n\<close> \<open>numeral m - 1 = sub m One\<close> \<open>1 - - numeral n = numeral (One + n)\<close> \<open>- numeral m - 1 = - numeral (m + One)\<close> \<open>- 1 - numeral n = - numeral (inc n)\<close> \<open>numeral m - - 1 = numeral (inc m)\<close> \<open>- 1 - - numeral n = sub n One\<close> \<open>- numeral m - - 1 = sub One m\<close> \<open>1 - 1 = 0\<close> \<open>- 1 - 1 = - 2\<close> \<open>1 - - 1 = 2\<close> \<open>- 1 - - 1 = 0\<close> by (simp_all add: sub_def numeral_add numeral.simps is_num_normalize numeral_inc
del: add_uminus_conv_diff add: diff_conv_add_uminus)
end
subsubsection \<open>Structures with multiplication: class \<open>semiring_numeral\<close>\<close>
class semiring_numeral = semiring + monoid_mult begin
subclass numeral ..
lemma numeral_mult: \<open>numeral (m * n) = numeral m * numeral n\<close> by (induct n rule: num_induct)
(simp_all add: numeral_One mult_inc numeral_inc numeral_add distrib_left)
lemma numeral_times_numeral: \<open>numeral m * numeral n = numeral (m * n)\<close> by (rule numeral_mult [symmetric])
lemma mult_2: \<open>2 * z = z + z\<close> by (simp add: one_add_one [symmetric] distrib_right)
lemma mult_2_right: \<open>z * 2 = z + z\<close> by (simp add: one_add_one [symmetric] distrib_left)
lemma left_add_twice: \<open>a + (a + b) = 2 * a + b\<close> by (simp add: mult_2 ac_simps)
lemma nat_of_num_numeral [code_abbrev]: \<open>nat_of_num = numeral\<close> proof fix n have\<open>numeral n = nat_of_num n\<close> by (induct n) (simp_all add: numeral.simps) thenshow\<open>nat_of_num n = numeral n\<close> by simp qed
lemma nat_of_num_code [code]: \<open>nat_of_num One = 1\<close> \<open>nat_of_num (Bit0 n) = (let m = nat_of_num n in m + m)\<close> \<open>nat_of_num (Bit1 n) = (let m = nat_of_num n in Suc (m + m))\<close> by (simp_all add: Let_def)
subsubsection \<open>Equality: class \<open>semiring_char_0\<close>\<close>
context semiring_char_0 begin
lemma numeral_eq_iff: \<open>numeral m = numeral n \<longleftrightarrow> m = n\<close> by (simp only: of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
of_nat_eq_iff num_eq_iff)
lemma numeral_eq_one_iff: \<open>numeral n = 1 \<longleftrightarrow> n = One\<close> by (rule numeral_eq_iff [of n One, unfolded numeral_One])
lemma one_eq_numeral_iff: \<open>1 = numeral n \<longleftrightarrow> One = n\<close> by (rule numeral_eq_iff [of One n, unfolded numeral_One])
lemma numeral_neq_zero: \<open>numeral n \<noteq> 0\<close> by (simp add: of_nat_numeral [symmetric] nat_of_num_numeral [symmetric] nat_of_num_pos)
subsubsection \<open>Comparisons: class \<open>linordered_nonzero_semiring\<close>\<close>
context linordered_nonzero_semiring begin
lemma numeral_le_iff: \<open>numeral m \<le> numeral n \<longleftrightarrow> m \<le> n\<close> proof - have\<open>of_nat (numeral m) \<le> of_nat (numeral n) \<longleftrightarrow> m \<le> n\<close> by (simp only: less_eq_num_def nat_of_num_numeral of_nat_le_iff) thenshow ?thesis by simp qed
lemma one_le_numeral: \<open>1 \<le> numeral n\<close> using numeral_le_iff [of One n] by (simp add: numeral_One)
lemma numeral_le_one_iff: \<open>numeral n \<le> 1 \<longleftrightarrow> n \<le> One\<close> using numeral_le_iff [of n One] by (simp add: numeral_One)
lemma numeral_less_iff: \<open>numeral m < numeral n \<longleftrightarrow> m < n\<close> proof - have\<open>of_nat (numeral m) < of_nat (numeral n) \<longleftrightarrow> m < n\<close> unfolding less_num_def nat_of_num_numeral of_nat_less_iff .. thenshow ?thesis by simp qed
lemma not_numeral_less_one: \<open>\<not> numeral n < 1\<close> using numeral_less_iff [of n One] by (simp add: numeral_One)
lemma one_less_numeral_iff: \<open>1 < numeral n \<longleftrightarrow> One < n\<close> using numeral_less_iff [of One n] by (simp add: numeral_One)
lemma zero_le_numeral: \<open>0 \<le> numeral n\<close> using dual_order.trans one_le_numeral zero_le_one by blast
lemma zero_less_numeral: \<open>0 < numeral n\<close> using less_linear not_numeral_less_one order.strict_trans zero_less_one by blast
lemma not_numeral_le_zero: \<open>\<not> numeral n \<le> 0\<close> by (simp add: not_le zero_less_numeral)
lemma not_numeral_less_zero: \<open>\<not> numeral n < 0\<close> by (simp add: not_less zero_le_numeral)
lemma one_of_nat_le_iff [simp]: \<open>1 \<le> of_nat k \<longleftrightarrow> 1 \<le> k\<close> using of_nat_le_iff [of 1] by simp
lemma numeral_nat_le_iff [simp]: \<open>numeral n \<le> of_nat k \<longleftrightarrow> numeral n \<le> k\<close> using of_nat_le_iff [of \<open>numeral n\<close>] by simp
lemma of_nat_le_1_iff [simp]: \<open>of_nat k \<le> 1 \<longleftrightarrow> k \<le> 1\<close> using of_nat_le_iff [of _ 1] by simp
lemma of_nat_le_numeral_iff [simp]: \<open>of_nat k \<le> numeral n \<longleftrightarrow> k \<le> numeral n\<close> using of_nat_le_iff [of _ \<open>numeral n\<close>] by simp
lemma one_of_nat_less_iff [simp]: \<open>1 < of_nat k \<longleftrightarrow> 1 < k\<close> using of_nat_less_iff [of 1] by simp
lemma numeral_nat_less_iff [simp]: \<open>numeral n < of_nat k \<longleftrightarrow> numeral n < k\<close> using of_nat_less_iff [of \<open>numeral n\<close>] by simp
lemma of_nat_less_1_iff [simp]: \<open>of_nat k < 1 \<longleftrightarrow> k < 1\<close> using of_nat_less_iff [of _ 1] by simp
lemma of_nat_less_numeral_iff [simp]: \<open>of_nat k < numeral n \<longleftrightarrow> k < numeral n\<close> using of_nat_less_iff [of _ \<open>numeral n\<close>] by simp
lemma of_nat_eq_numeral_iff [simp]: \<open>of_nat k = numeral n \<longleftrightarrow> k = numeral n\<close> using of_nat_eq_iff [of _ \<open>numeral n\<close>] by simp
lemma minus_sub_one_diff_one [simp]: \<open>- sub m One - 1 = - numeral m\<close> proof - have\<open>sub m One + 1 = numeral m\<close> by (simp flip: eq_diff_eq add: diff_numeral_special) thenhave\<open>- (sub m One + 1) = - numeral m\<close> by simp thenshow ?thesis by simp qed
end
subsubsection \<open>Equality using \<open>iszero\<close> for rings with non-zero characteristic\<close>
context ring_1 begin
definition iszero :: \<open>'a \<Rightarrow> bool\<close> where\<open>iszero z \<longleftrightarrow> z = 0\<close>
lemma iszero_0 [simp]: \<open>iszero 0\<close> by (simp add: iszero_def)
lemma not_iszero_1 [simp]: \<open>\<not> iszero 1\<close> by (simp add: iszero_def)
lemma not_iszero_Numeral1: \<open>\<not> iszero Numeral1\<close> by (simp add: numeral_One)
lemma eq_iff_iszero_diff: \<open>x = y \<longleftrightarrow> iszero (x - y)\<close> unfolding iszero_def by (rule eq_iff_diff_eq_0)
text\<open>
The \<open>eq_numeral_iff_iszero\<close> lemmas are not declared \<open>[simp]\<close> by default,
because for rings of characteristic zero, better simp rules are possible. For a type like integers mod \<open>n\<close>, type-instantiated versions of these rules
should be added to the simplifier, along with a type-specific rule for
deciding propositions of the form \<open>iszero (numeral w)\<close>.
bh: Maybe it would not be so bad to just declare these as simp rules anyway?
I should test whether these rules take precedence over the \<open>ring_char_0\<close>
rules in the simplifier. \<close>
lemma eq_numeral_iff_iszero: \<open>numeral x = numeral y \<longleftrightarrow> iszero (sub x y)\<close> \<open>numeral x = - numeral y \<longleftrightarrow> iszero (numeral (x + y))\<close> \<open>- numeral x = numeral y \<longleftrightarrow> iszero (numeral (x + y))\<close> \<open>- numeral x = - numeral y \<longleftrightarrow> iszero (sub y x)\<close> \<open>numeral x = 1 \<longleftrightarrow> iszero (sub x One)\<close> \<open>1 = numeral y \<longleftrightarrow> iszero (sub One y)\<close> \<open>- numeral x = 1 \<longleftrightarrow> iszero (numeral (x + One))\<close> \<open>1 = - numeral y \<longleftrightarrow> iszero (numeral (One + y))\<close> \<open>numeral x = 0 \<longleftrightarrow> iszero (numeral x)\<close> \<open>0 = numeral y \<longleftrightarrow> iszero (numeral y)\<close> \<open>- numeral x = 0 \<longleftrightarrow> iszero (numeral x)\<close> \<open>0 = - numeral y \<longleftrightarrow> iszero (numeral y)\<close> unfolding eq_iff_iszero_diff diff_numeral_simps diff_numeral_special by simp_all
end
subsubsection \<open>Equality and negation: class \<open>ring_char_0\<close>\<close>
lemma neg_numeral_eq_iff: \<open>- numeral m = - numeral n \<longleftrightarrow> m = n\<close> by simp
lemma numeral_neq_neg_numeral: \<open>numeral m \<noteq> - numeral n\<close> by (simp add: eq_neg_iff_add_eq_0 numeral_plus_numeral)
lemma neg_numeral_neq_numeral: \<open>- numeral m \<noteq> numeral n\<close> by (rule numeral_neq_neg_numeral [symmetric])
lemma zero_neq_neg_numeral: \<open>0 \<noteq> - numeral n\<close> by simp
lemma neg_numeral_neq_zero: \<open>- numeral n \<noteq> 0\<close> by simp
lemma one_neq_neg_numeral: \<open>1 \<noteq> - numeral n\<close> using numeral_neq_neg_numeral [of One n] by (simp add: numeral_One)
lemma neg_numeral_neq_one: \<open>- numeral n \<noteq> 1\<close> using neg_numeral_neq_numeral [of n One] by (simp add: numeral_One)
lemma neg_one_neq_numeral: \<open>- 1 \<noteq> numeral n\<close> using neg_numeral_neq_numeral [of One n] by (simp add: numeral_One)
lemma numeral_neq_neg_one: \<open>numeral n \<noteq> - 1\<close> using numeral_neq_neg_numeral [of n One] by (simp add: numeral_One)
lemma neg_one_eq_numeral_iff: \<open>- 1 = - numeral n \<longleftrightarrow> n = One\<close> using neg_numeral_eq_iff [of One n] by (auto simp add: numeral_One)
lemma numeral_eq_neg_one_iff: \<open>- numeral n = - 1 \<longleftrightarrow> n = One\<close> using neg_numeral_eq_iff [of n One] by (auto simp add: numeral_One)
lemma neg_one_neq_zero: \<open>- 1 \<noteq> 0\<close> by simp
lemma zero_neq_neg_one: \<open>0 \<noteq> - 1\<close> by simp
lemma neg_one_neq_one: \<open>- 1 \<noteq> 1\<close> using neg_numeral_neq_numeral [of One One] by (simp only: numeral_One not_False_eq_True)
lemma one_neq_neg_one: \<open>1 \<noteq> - 1\<close> using numeral_neq_neg_numeral [of One One] by (simp only: numeral_One not_False_eq_True)
text\<open>For \<^term>\<open>case_nat\<close> and \<^term>\<open>rec_nat\<close>.\<close>
lemma case_nat_numeral [simp]: \<open>case_nat a f (numeral v) = (let pv = pred_numeral v in f pv)\<close> by (simp add: numeral_eq_Suc)
lemma case_nat_add_eq_if [simp]: \<open>case_nat a f ((numeral v) + n) = (let pv = pred_numeral v in f (pv + n))\<close> by (simp add: numeral_eq_Suc)
lemma rec_nat_numeral [simp]: \<open>rec_nat a f (numeral v) = (let pv = pred_numeral v in f pv (rec_nat a f pv))\<close> by (simp add: numeral_eq_Suc Let_def)
lemma rec_nat_add_eq_if [simp]: \<open>rec_nat a f (numeral v + n) = (let pv = pred_numeral v in f (pv + n) (rec_nat a f (pv + n)))\<close> by (simp add: numeral_eq_Suc Let_def)
text\<open>Case analysis on \<^term>\<open>n < 2\<close>.\<close> lemma less_2_cases: \<open>n < 2 \<Longrightarrow> n = 0 \<or> n = Suc 0\<close> by (auto simp add: numeral_2_eq_2)
lemma less_2_cases_iff: \<open>n < 2 \<longleftrightarrow> n = 0 \<or> n = Suc 0\<close> by (auto simp add: numeral_2_eq_2)
text\<open>Removal of Small Numerals: 0, 1 and (in additive positions) 2.\<close> text\<open>bh: Are these rules really a good idea? LCP: well, it already happens for 0 and 1!\<close>
lemma add_2_eq_Suc [simp]: \<open>2 + n = Suc (Suc n)\<close> by simp
text\<open>Can be used to eliminate long strings of Sucs, but not by default.\<close> lemma Suc3_eq_add_3: \<open>Suc (Suc (Suc n)) = 3 + n\<close> by simp
lemma numeral_add_unfold_funpow: \<open>numeral k + a = ((+) 1 ^^ numeral k) a\<close> proof (rule sym, induction k arbitrary: a) case One thenshow ?case by (simp add: Num.numeral_One numeral_One) next case (Bit0 k) thenshow ?case by (simp add: Num.numeral_Bit0 numeral_Bit0 ac_simps funpow_add) next case (Bit1 k) thenshow ?case by (simp add: Num.numeral_Bit1 numeral_Bit1 ac_simps funpow_add) qed
end
context semiring_1 begin
lemma numeral_unfold_funpow: \<open>numeral k = ((+) 1 ^^ numeral k) 0\<close> using numeral_add_unfold_funpow [of k 0] by simp
end
context includes lifting_syntax begin
lemma transfer_rule_numeral: \<open>((=) ===> R) numeral numeral\<close> if [transfer_rule]: \<open>R 0 0\<close> \<open>R 1 1\<close> \<open>(R ===> R ===> R) (+) (+)\<close> for R :: \<open>'a::{semiring_numeral,monoid_add} \<Rightarrow> 'b::{semiring_numeral,monoid_add} \<Rightarrow> bool\<close> proof - have\<open>((=) ===> R) (\<lambda>k. ((+) 1 ^^ numeral k) 0) (\<lambda>k. ((+) 1 ^^ numeral k) 0)\<close> by transfer_prover moreoverhave\<open>numeral = (\<lambda>k. ((+) (1::'a) ^^ numeral k) 0)\<close> using numeral_add_unfold_funpow [where ?'a = 'a, of _ 0] by (simp add: fun_eq_iff) moreoverhave\<open>numeral = (\<lambda>k. ((+) (1::'b) ^^ numeral k) 0)\<close> using numeral_add_unfold_funpow [where ?'a = 'b, of _ 0] by (simp add: fun_eq_iff) ultimatelyshow ?thesis by simp qed
lemma half_gt_zero_iff: \<open>0 < a / 2 \<longleftrightarrow> 0 < a\<close> by (auto simp add: field_simps)
lemma half_gt_zero [simp]: \<open>0 < a \<Longrightarrow> 0 < a / 2\<close> by (simp add: half_gt_zero_iff)
end
subsection \<open>Numeral equations as default simplification rules\<close>
declare (in numeral) numeral_One [simp] declare (in numeral) numeral_plus_numeral [simp] declare (in numeral) add_numeral_special [simp] declare (in neg_numeral) add_neg_numeral_simps [simp] declare (in neg_numeral) add_neg_numeral_special [simp] declare (in neg_numeral) diff_numeral_simps [simp] declare (in neg_numeral) diff_numeral_special [simp] declare (in semiring_numeral) numeral_times_numeral [simp] declare (in ring_1) mult_neg_numeral_simps [simp]
subsubsection \<open>Special Simplification for Constants\<close>
text\<open>These distributive laws move literals inside sums and differences.\<close>
lemmas distrib_right_numeral [simp] = distrib_right [of _ _ \<open>numeral v\<close>] for v lemmas distrib_left_numeral [simp] = distrib_left [of \<open>numeral v\<close>] for v lemmas left_diff_distrib_numeral [simp] = left_diff_distrib [of _ _ \<open>numeral v\<close>] for v lemmas right_diff_distrib_numeral [simp] = right_diff_distrib [of \<open>numeral v\<close>] for v
text\<open>These are actually for fields, like real\<close>
lemmas zero_less_divide_iff_numeral [simp, no_atp] = zero_less_divide_iff [of \<open>numeral w\<close>] for w lemmas divide_less_0_iff_numeral [simp, no_atp] = divide_less_0_iff [of \<open>numeral w\<close>] for w lemmas zero_le_divide_iff_numeral [simp, no_atp] = zero_le_divide_iff [of \<open>numeral w\<close>] for w lemmas divide_le_0_iff_numeral [simp, no_atp] = divide_le_0_iff [of \<open>numeral w\<close>] for w
text\<open>Replaces \<open>inverse #nn\<close> by \<open>1/#nn\<close>. It looks
strange, but then other simprocs simplify the quotient.\<close>
lemmas inverse_eq_divide_numeral [simp] =
inverse_eq_divide [of \<open>numeral w\<close>] for w
lemmas inverse_eq_divide_neg_numeral [simp] =
inverse_eq_divide [of \<open>- numeral w\<close>] for w
text\<open>These laws simplify inequalities, moving unary minus from a term
into the literal.\<close>
lemmas equation_minus_iff_numeral [no_atp] =
equation_minus_iff [of \<open>numeral v\<close>] for v
lemmas minus_equation_iff_numeral [no_atp] =
minus_equation_iff [of _ \<open>numeral v\<close>] for v
lemmas le_minus_iff_numeral [no_atp] =
le_minus_iff [of \<open>numeral v\<close>] for v
lemmas minus_le_iff_numeral [no_atp] =
minus_le_iff [of _ \<open>numeral v\<close>] for v
lemmas less_minus_iff_numeral [no_atp] =
less_minus_iff [of \<open>numeral v\<close>] for v
lemmas minus_less_iff_numeral [no_atp] =
minus_less_iff [of _ \<open>numeral v\<close>] for v
(* FIXME maybe simproc *)
text\<open>Cancellation of constant factors in comparisons (\<open><\<close> and \<open>\<le>\<close>)\<close>
lemmas mult_less_cancel_left_numeral [simp, no_atp] = mult_less_cancel_left [of \<open>numeral v\<close>] for v lemmas mult_less_cancel_right_numeral [simp, no_atp] = mult_less_cancel_right [of _ \<open>numeral v\<close>] for v lemmas mult_le_cancel_left_numeral [simp, no_atp] = mult_le_cancel_left [of \<open>numeral v\<close>] for v lemmas mult_le_cancel_right_numeral [simp, no_atp] = mult_le_cancel_right [of _ \<open>numeral v\<close>] for v
text\<open>Multiplying out constant divisors in comparisons (\<open><\<close>, \<open>\<le>\<close> and \<open>=\<close>)\<close>
named_theorems divide_const_simps \<open>simplification rules to simplify comparisons involving constant divisors\<close>
lemmas le_divide_eq_numeral1 [simp,divide_const_simps] =
pos_le_divide_eq [of \<open>numeral w\<close>, OF zero_less_numeral]
neg_le_divide_eq [of \<open>- numeral w\<close>, OF neg_numeral_less_zero] for w
lemmas divide_le_eq_numeral1 [simp,divide_const_simps] =
pos_divide_le_eq [of \<open>numeral w\<close>, OF zero_less_numeral]
neg_divide_le_eq [of \<open>- numeral w\<close>, OF neg_numeral_less_zero] for w
lemmas less_divide_eq_numeral1 [simp,divide_const_simps] =
pos_less_divide_eq [of \<open>numeral w\<close>, OF zero_less_numeral]
neg_less_divide_eq [of \<open>- numeral w\<close>, OF neg_numeral_less_zero] for w
lemmas divide_less_eq_numeral1 [simp,divide_const_simps] =
pos_divide_less_eq [of \<open>numeral w\<close>, OF zero_less_numeral]
neg_divide_less_eq [of \<open>- numeral w\<close>, OF neg_numeral_less_zero] for w
lemmas eq_divide_eq_numeral1 [simp,divide_const_simps] =
eq_divide_eq [of _ _ \<open>numeral w\<close>]
eq_divide_eq [of _ _ \<open>- numeral w\<close>] for w
lemmas divide_eq_eq_numeral1 [simp,divide_const_simps] =
divide_eq_eq [of _ \<open>numeral w\<close>]
divide_eq_eq [of _ \<open>- numeral w\<close>] for w
lemma mult_numeral_1: \<open>Numeral1 * a = a\<close> for a :: \<open>'a::semiring_numeral\<close> by simp
lemma mult_numeral_1_right: \<open>a * Numeral1 = a\<close> for a :: \<open>'a::semiring_numeral\<close> by simp
lemma divide_numeral_1: \<open>a / Numeral1 = a\<close> for a :: \<open>'a::field\<close> by simp
lemma inverse_numeral_1: \<open>inverse Numeral1 = (Numeral1::'a::division_ring)\<close> by simp
text\<open> Theorem lists for the cancellation simprocs. The use of a binary
numeral for 1 reduces the number of special cases. \<close>
lemma mult_1s_semiring_numeral: \<open>Numeral1 * a = a\<close> \<open>a * Numeral1 = a\<close> for a :: \<open>'a::semiring_numeral\<close> by simp_all
lemma mult_1s_ring_1: \<open>- Numeral1 * b = - b\<close> \<open>b * - Numeral1 = - b\<close> for b :: \<open>'a::ring_1\<close> by simp_all
lemma Let_numeral [simp]: \<open>Let (numeral v) f = f (numeral v)\<close> \<comment> \<open>Unfold all \<open>let\<close>s involving constants\<close> unfolding Let_def ..
lemma Let_neg_numeral [simp]: \<open>Let (- numeral v) f = f (- numeral v)\<close> \<comment> \<open>Unfold all \<open>let\<close>s involving constants\<close> unfolding Let_def ..
declaration\<open> let fun number_of ctxt T n = if not (Sign.of_sort (Proof_Context.theory_of ctxt) (T, \<^sort>\<open>numeral\<close>)) then raise CTERM ("number_of", [])
else Numeral.mk_cnumber (Thm.ctyp_of ctxt T) n; in
K (
Lin_Arith.set_number_of number_of
#> Lin_Arith.add_simps
@{thms arith_simps more_arith_simps rel_simps pred_numeral_simps
arith_special numeral_One of_nat_simps uminus_numeral_One
Suc_numeral Let_numeral Let_neg_numeral Let_0 Let_1
le_Suc_numeral le_numeral_Suc less_Suc_numeral less_numeral_Suc
Suc_eq_numeral eq_numeral_Suc mult_Suc mult_Suc_right of_nat_numeral}) end \<close>
subsubsection \<open>Simplification of arithmetic when nested to the right\<close>
lemma add_numeral_left [simp]: \<open>numeral v + (numeral w + z) = (numeral(v + w) + z)\<close> by (simp_all add: add.assoc [symmetric])
lemma add_neg_numeral_left [simp]: \<open>numeral v + (- numeral w + y) = (sub v w + y)\<close> \<open>- numeral v + (numeral w + y) = (sub w v + y)\<close> \<open>- numeral v + (- numeral w + y) = (- numeral(v + w) + y)\<close> by (simp_all add: add.assoc [symmetric])
lemma mult_numeral_left_semiring_numeral: \<open>numeral v * (numeral w * z) = (numeral(v * w) * z :: 'a::semiring_numeral)\<close> by (simp add: mult.assoc [symmetric])
lemma mult_numeral_left_ring_1: \<open>- numeral v * (numeral w * y) = (- numeral(v * w) * y :: 'a::ring_1)\<close> \<open>numeral v * (- numeral w * y) = (- numeral(v * w) * y :: 'a::ring_1)\<close> \<open>- numeral v * (- numeral w * y) = (numeral(v * w) * y :: 'a::ring_1)\<close> by (simp_all add: mult.assoc [symmetric])
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.