lemma num_induct [case_names One inc]: fixes P :: ‹num ==> bool› assumes One: ‹P One› and inc: ‹∧x. P x ==> P (inc x)› shows‹P x› proof - obtain n where n: ‹Suc n = nat_of_num x› by (cases ‹nat_of_num x›) (simp_all add: nat_of_num_neq_0) have‹P (num_of_nat (Suc n))› proof (induct n) case 0 from One show ?caseby simp next case (Suc n) thenhave‹P (inc (num_of_nat (Suc n)))›by (rule inc) thenshow‹P (num_of_nat (Suc (Suc n)))›by simp qed with n show‹P x› by (simp add: nat_of_num_inverse) qed
text‹ From now on, there are two possible models for 🍋‹num›: as positive naturals (rule ‹num_induct›) and as digit representation (rules ‹num.induct›,‹num.cases›). ›
subsection‹Numeral operations›
instantiation num :: ‹{plus,times,linorder}› begin
definition [code del]: ‹m + n = num_of_nat (nat_of_num m + nat_of_num n)›
definition [code del]: ‹m * n = num_of_nat (nat_of_num m * nat_of_num n)›
definition [code del]: ‹m ≤ n ⟷ nat_of_num m ≤ nat_of_num n›
definition [code del]: ‹m 🚫⟷ nat_of_num m 🚫 n›
instance by standard (auto simp add: less_num_def less_eq_num_def num_eq_iff)
end
lemma nat_of_num_add: ‹nat_of_num (x + y) = nat_of_num x + nat_of_num y› unfolding plus_num_def by (intro num_of_nat_inverse add_pos_pos nat_of_num_pos)
lemma nat_of_num_mult: ‹nat_of_num (x * y) = nat_of_num x * nat_of_num y› unfolding times_num_def by (intro num_of_nat_inverse mult_pos_pos nat_of_num_pos)
lemma add_num_simps [simp, code]: ‹One + One = Bit0 One› ‹One + Bit0 n = Bit1 n› ‹One + Bit1 n = Bit0 (n + One)› ‹Bit0 m + One = Bit1 m› ‹Bit0 m + Bit0 n = Bit0 (m + n)› ‹Bit0 m + Bit1 n = Bit1 (m + n)› ‹Bit1 m + One = Bit0 (m + One)› ‹Bit1 m + Bit0 n = Bit1 (m + n)› ‹Bit1 m + Bit1 n = Bit0 (m + n + One)› by (simp_all add: num_eq_iff nat_of_num_add)
lemma mult_num_simps [simp, code]: ‹m * One = m› ‹One * n = n› ‹Bit0 m * Bit0 n = Bit0 (Bit0 (m * n))› ‹Bit0 m * Bit1 n = Bit0 (m * Bit1 n)› ‹Bit1 m * Bit0 n = Bit0 (Bit1 m * n)› ‹Bit1 m * Bit1 n = Bit1 (m + n + Bit0 (m * n))› by (simp_all add: num_eq_iff nat_of_num_add nat_of_num_mult distrib_right distrib_left)
lemma eq_num_simps: ‹One = One ⟷ True› ‹One = Bit0 n ⟷ False› ‹One = Bit1 n ⟷ False› ‹Bit0 m = One ⟷ False› ‹Bit1 m = One ⟷ False› ‹Bit0 m = Bit0 n ⟷ m = n› ‹Bit0 m = Bit1 n ⟷ False› ‹Bit1 m = Bit0 n ⟷ False› ‹Bit1 m = Bit1 n ⟷ m = n› by simp_all
lemma le_num_simps [simp, code]: ‹One ≤ n ⟷ True› ‹Bit0 m ≤ One ⟷ False› ‹Bit1 m ≤ One ⟷ False› ‹Bit0 m ≤ Bit0 n ⟷ m ≤ n› ‹Bit0 m ≤ Bit1 n ⟷ m ≤ n› ‹Bit1 m ≤ Bit1 n ⟷ m ≤ n› ‹Bit1 m ≤ Bit0 n ⟷ m 🚫› 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]: ‹m 🚫⟷ False› ‹One 🚫 n ⟷ True› ‹One 🚫 n ⟷ True› ‹Bit0 m 🚫 n ⟷ m 🚫› ‹Bit0 m 🚫 n ⟷ m ≤ n› ‹Bit1 m 🚫 n ⟷ m 🚫› ‹Bit1 m 🚫 n ⟷ m 🚫› 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: ‹x ≤ One ⟷ x = One› by (simp add: antisym_conv)
text‹Rules using ‹One›and ‹inc› as constructors.›
lemma add_One: ‹x + One = inc x› by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
lemma add_One_commute: ‹One + n = n + One› by (induct n) simp_all
lemma add_inc: ‹x + inc y = inc (x + y)› by (simp add: num_eq_iff nat_of_num_add nat_of_num_inc)
lemma mult_inc: ‹x * inc y = x * y + x› by (simp add: num_eq_iff nat_of_num_mult nat_of_num_add nat_of_num_inc)
text‹The 🍋‹num_of_nat›conversion.›
lemma num_of_nat_One: ‹n ≤ 1 ==> num_of_nat n = One› by (cases n) simp_all
primrec pow :: ‹num ==> num ==> num› where ‹pow x One = x›
| ‹pow x (Bit0 y) = sqr (pow x y)›
| ‹pow x (Bit1 y) = sqr (pow x y) * x›
lemma nat_of_num_sqr: ‹nat_of_num (sqr x) = nat_of_num x * nat_of_num x› by (induct x) (simp_all add: algebra_simps nat_of_num_add)
lemma sqr_conv_mult: ‹sqr x = x * x› by (simp add: num_eq_iff nat_of_num_sqr nat_of_num_mult)
lemma num_double [simp]: ‹Bit0 num.One * n = Bit0 n› by (simp add: num_eq_iff nat_of_num_mult)
subsection‹Binary numerals›
text‹ We embed binary representations into a generic algebraic structure using ‹numeral›. ›
class numeral = one + semigroup_add begin
primrec numeral :: ‹num ==> 'a› where
numeral_One: ‹numeral One = 1›
| numeral_Bit0: ‹numeral (Bit0 n) = numeral n + numeral n›
| numeral_Bit1: ‹numeral (Bit1 n) = numeral n + numeral n + 1›
lemma numeral_code [code]: ‹numeral One = 1› ‹numeral (Bit0 n) = (let m = numeral n in m + m)› ‹numeral (Bit1 n) = (let m = numeral n in m + m + 1)› by (simp_all add: Let_def)
lemma one_plus_numeral_commute: ‹1 + numeral x = numeral x + 1› 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: ‹numeral (inc x) = numeral x + 1› proof (induct x) case One thenshow ?caseby simp next case Bit0 thenshow ?caseby simp next case (Bit1 x) have‹numeral x + (1 + numeral x) + 1 = numeral x + (numeral x + 1) + 1› by (simp only: one_plus_numeral_commute) with Bit1 show ?case by (simp add: add.assoc) qed
parse_translation‹ let fun numeral_tr [(c as Const (🍋‹_constrain›, _)) $ 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 [(🍋‹_Numeral›, K numeral_tr)] end ›
typed_print_translation‹ let fun num_tr' ctxt T [n] = let val k = Numeral.dest_num_syntax n; val t' = Syntax.const 🍋‹_Numeral›$ Syntax.free (string_of_int k); in (case T of Type (🍋‹fun›, [_, T']) => if Printer.type_emphasis ctxt T' then Syntax.const 🍋‹_constrain›$ t' $ Syntax_Phases.term_of_typ ctxt T' else t' | _ => if T = dummyT then t' else raise Match) end; in [(🍋‹numeral›, num_tr')] end ›
subsection‹Class-specific numeral rules›
text‹🍋‹numeral›is a morphism.›
subsubsection ‹Structures with addition: class ‹numeral›\›
context numeral begin
lemma numeral_add: ‹numeral (m + n) = numeral m + numeral n› by (induct n rule: num_induct)
(simp_all only: numeral_One add_One add_inc numeral_inc add.assoc)
lemma numeral_plus_numeral: ‹numeral m + numeral n = numeral (m + n)› by (rule numeral_add [symmetric])
lemma numeral_plus_one: ‹numeral n + 1 = numeral (n + One)› using numeral_add [of n One] by (simp add: numeral_One)
lemma one_plus_numeral: ‹1 + numeral n = numeral (One + n)› using numeral_add [of One n] by (simp add: numeral_One)
lemma one_add_one: ‹1 + 1 = 2› using numeral_add [of One One] by (simp add: numeral_One)
lemma is_num_add_commute: ‹is_num x ==> is_num y ==> x + y = y + x› 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‹y + (1 + - y) + y = y + (- y + 1) + y› by (simp add: add.assoc) thenhave‹y + (1 + - y) = y + (- y + 1)› by simp thenshow ?case by (rule add_left_imp_eq[of y]) next case (3 x y) thenhave‹1 + (x + y) = x + 1 + y› by (simp add: add.assoc [symmetric]) thenshow ?caseusing 3 by (simp add: add.assoc) qed next case (2 x) thenhave‹x + (- x + y) + x = x + (y + - x) + x› by (simp add: add.assoc) thenhave‹x + (- x + y) = x + (y + - x)› by simp thenshow ?case by (rule add_left_imp_eq[of x]) next case (3 x z) moreoverhave‹x + (y + z) = (x + y) + z› by (simp add: add.assoc[symmetric]) ultimatelyshow ?case by (simp add: add.assoc) qed
lemma is_num_add_left_commute: ‹is_num x ==> is_num y ==> x + (y + z) = y + (x + z)› by (simp only: add.assoc [symmetric] is_num_add_commute)
lemma sub_num_simps [simp]: ‹sub One One = 0› ‹sub One (Bit0 l) = - numeral (BitM l)› ‹sub One (Bit1 l) = - numeral (Bit0 l)› ‹sub (Bit0 k) One = numeral (BitM k)› ‹sub (Bit1 k) One = numeral (Bit0 k)› ‹sub (Bit0 k) (Bit0 l) = dbl (sub k l)› ‹sub (Bit0 k) (Bit1 l) = dbl_dec (sub k l)› ‹sub (Bit1 k) (Bit0 l) = dbl_inc (sub k l)› ‹sub (Bit1 k) (Bit1 l) = dbl (sub k l)› 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: ‹numeral m + - numeral n = sub m n› ‹- numeral m + numeral n = sub n m› ‹- numeral m + - numeral n = - (numeral m + numeral n)› 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: ‹1 + - numeral m = sub One m› ‹- numeral m + 1 = sub One m› ‹numeral m + - 1 = sub m One› ‹- 1 + numeral n = sub n One› ‹- 1 + - numeral n = - numeral (inc n)› ‹- numeral m + - 1 = - numeral (inc m)› ‹1 + - 1 = 0› ‹- 1 + 1 = 0› ‹- 1 + - 1 = - 2› 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: ‹numeral m - numeral n = sub m n› ‹numeral m - - numeral n = numeral (m + n)› ‹- numeral m - numeral n = - numeral (m + n)› ‹- numeral m - - numeral n = sub n m› 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: ‹1 - numeral n = sub One n› ‹numeral m - 1 = sub m One› ‹1 - - numeral n = numeral (One + n)› ‹- numeral m - 1 = - numeral (m + One)› ‹- 1 - numeral n = - numeral (inc n)› ‹numeral m - - 1 = numeral (inc m)› ‹- 1 - - numeral n = sub n One› ‹- numeral m - - 1 = sub One m› ‹1 - 1 = 0› ‹- 1 - 1 = - 2› ‹1 - - 1 = 2› ‹- 1 - - 1 = 0› 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 ‹Structures with multiplication: class ‹semiring_numeral›\›
class semiring_numeral = semiring + monoid_mult begin
subclass numeral ..
lemma numeral_mult: ‹numeral (m * n) = numeral m * numeral n› by (induct n rule: num_induct)
(simp_all add: numeral_One mult_inc numeral_inc numeral_add distrib_left)
lemma numeral_times_numeral: ‹numeral m * numeral n = numeral (m * n)› by (rule numeral_mult [symmetric])
lemma mult_2: ‹2 * z = z + z› by (simp add: one_add_one [symmetric] distrib_right)
lemma mult_2_right: ‹z * 2 = z + z› by (simp add: one_add_one [symmetric] distrib_left)
lemma left_add_twice: ‹a + (a + b) = 2 * a + b› by (simp add: mult_2 ac_simps)
lemma nat_of_num_numeral [code_abbrev]: ‹nat_of_num = numeral› proof fix n have‹numeral n = nat_of_num n› by (induct n) (simp_all add: numeral.simps) thenshow‹nat_of_num n = numeral n› by simp qed
lemma nat_of_num_code [code]: ‹nat_of_num One = 1› ‹nat_of_num (Bit0 n) = (let m = nat_of_num n in m + m)› ‹nat_of_num (Bit1 n) = (let m = nat_of_num n in Suc (m + m))› by (simp_all add: Let_def)
subsubsection ‹Equality: class ‹semiring_char_0›\›
context semiring_char_0 begin
lemma numeral_eq_iff: ‹numeral m = numeral n ⟷ m = n› by (simp only: of_nat_numeral [symmetric] nat_of_num_numeral [symmetric]
of_nat_eq_iff num_eq_iff)
lemma numeral_eq_one_iff: ‹numeral n = 1 ⟷ n = One› by (rule numeral_eq_iff [of n One, unfolded numeral_One])
lemma one_eq_numeral_iff: ‹1 = numeral n ⟷ One = n› by (rule numeral_eq_iff [of One n, unfolded numeral_One])
lemma numeral_neq_zero: ‹numeral n ≠ 0› by (simp add: of_nat_numeral [symmetric] nat_of_num_numeral [symmetric] nat_of_num_pos)
subsubsection ‹Comparisons: class ‹linordered_nonzero_semiring›\›
context linordered_nonzero_semiring begin
lemma numeral_le_iff: ‹numeral m ≤ numeral n ⟷ m ≤ n› proof - have‹of_nat (numeral m) ≤ of_nat (numeral n) ⟷ m ≤ n› by (simp only: less_eq_num_def nat_of_num_numeral of_nat_le_iff) thenshow ?thesis by simp qed
lemma one_le_numeral: ‹1 ≤ numeral n› using numeral_le_iff [of One n] by (simp add: numeral_One)
lemma numeral_le_one_iff: ‹numeral n ≤ 1 ⟷ n ≤ One› using numeral_le_iff [of n One] by (simp add: numeral_One)
lemma numeral_less_iff: ‹numeral m 🚫 n ⟷ m 🚫› proof - have‹of_nat (numeral m) 🚫 (numeral n) ⟷ m 🚫› unfolding less_num_def nat_of_num_numeral of_nat_less_iff .. thenshow ?thesis by simp qed
lemma not_numeral_less_one: ‹¬ numeral n 🚫› using numeral_less_iff [of n One] by (simp add: numeral_One)
lemma one_less_numeral_iff: ‹1 🚫 n ⟷ One 🚫› using numeral_less_iff [of One n] by (simp add: numeral_One)
lemma zero_le_numeral: ‹0 ≤ numeral n› using dual_order.trans one_le_numeral zero_le_one by blast
lemma zero_less_numeral: ‹0 🚫 n› using less_linear not_numeral_less_one order.strict_trans zero_less_one by blast
lemma not_numeral_le_zero: ‹¬ numeral n ≤ 0› by (simp add: not_le zero_less_numeral)
lemma not_numeral_less_zero: ‹¬ numeral n 🚫› by (simp add: not_less zero_le_numeral)
lemma one_of_nat_le_iff [simp]: ‹1 ≤ of_nat k ⟷ 1 ≤ k› using of_nat_le_iff [of 1] by simp
lemma numeral_nat_le_iff [simp]: ‹numeral n ≤ of_nat k ⟷ numeral n ≤ k› using of_nat_le_iff [of ‹numeral n›] by simp
lemma of_nat_le_1_iff [simp]: ‹of_nat k ≤ 1 ⟷ k ≤ 1› using of_nat_le_iff [of _ 1] by simp
lemma of_nat_le_numeral_iff [simp]: ‹of_nat k ≤ numeral n ⟷ k ≤ numeral n› using of_nat_le_iff [of _ ‹numeral n›] by simp
lemma one_of_nat_less_iff [simp]: ‹1 🚫 k ⟷ 1 🚫› using of_nat_less_iff [of 1] by simp
lemma numeral_nat_less_iff [simp]: ‹numeral n 🚫 k ⟷ numeral n 🚫› using of_nat_less_iff [of ‹numeral n›] by simp
lemma of_nat_less_1_iff [simp]: ‹of_nat k 🚫⟷ k 🚫› using of_nat_less_iff [of _ 1] by simp
lemma of_nat_less_numeral_iff [simp]: ‹of_nat k 🚫 n ⟷ k 🚫 n› using of_nat_less_iff [of _ ‹numeral n›] by simp
lemma of_nat_eq_numeral_iff [simp]: ‹of_nat k = numeral n ⟷ k = numeral n› using of_nat_eq_iff [of _ ‹numeral n›] by simp
lemma minus_sub_one_diff_one [simp]: ‹- sub m One - 1 = - numeral m› proof - have‹sub m One + 1 = numeral m› by (simp flip: eq_diff_eq add: diff_numeral_special) thenhave‹- (sub m One + 1) = - numeral m› by simp thenshow ?thesis by simp qed
end
subsubsection ‹Equality using ‹iszero›for rings with non-zero characteristic›
context ring_1 begin
definition iszero :: ‹'a ==> bool› where‹iszero z ⟷ z = 0›
lemma iszero_0 [simp]: ‹iszero 0› by (simp add: iszero_def)
lemma not_iszero_1 [simp]: ‹¬ iszero 1› by (simp add: iszero_def)
lemma not_iszero_Numeral1: ‹¬ iszero Numeral1› by (simp add: numeral_One)
lemma eq_iff_iszero_diff: ‹x = y ⟷ iszero (x - y)› unfolding iszero_def by (rule eq_iff_diff_eq_0)
text‹ The ‹eq_numeral_iff_iszero›lemmas are not declared ‹[simp]› by default, because for rings of characteristic zero, better simp rules are possible. For a type like integers mod ‹n›, type-instantiated versions of these rules should be added to the simplifier, along with a type-specific rule for deciding propositions of the form ‹iszero (numeral w)›. 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 ‹ring_char_0› rules in the simplifier. ›
lemma eq_numeral_iff_iszero: ‹numeral x = numeral y ⟷ iszero (sub x y)› ‹numeral x = - numeral y ⟷ iszero (numeral (x + y))› ‹- numeral x = numeral y ⟷ iszero (numeral (x + y))› ‹- numeral x = - numeral y ⟷ iszero (sub y x)› ‹numeral x = 1 ⟷ iszero (sub x One)› ‹1 = numeral y ⟷ iszero (sub One y)› ‹- numeral x = 1 ⟷ iszero (numeral (x + One))› ‹1 = - numeral y ⟷ iszero (numeral (One + y))› ‹numeral x = 0 ⟷ iszero (numeral x)› ‹0 = numeral y ⟷ iszero (numeral y)› ‹- numeral x = 0 ⟷ iszero (numeral x)› ‹0 = - numeral y ⟷ iszero (numeral y)› unfolding eq_iff_iszero_diff diff_numeral_simps diff_numeral_special by simp_all
end
subsubsection ‹Equality and negation: class ‹ring_char_0›\›
lemma case_nat_numeral [simp]: ‹case_nat a f (numeral v) = (let pv = pred_numeral v in f pv)› by (simp add: numeral_eq_Suc)
lemma case_nat_add_eq_if [simp]: ‹case_nat a f ((numeral v) + n) = (let pv = pred_numeral v in f (pv + n))› by (simp add: numeral_eq_Suc)
lemma rec_nat_numeral [simp]: ‹rec_nat a f (numeral v) = (let pv = pred_numeral v in f pv (rec_nat a f pv))› by (simp add: numeral_eq_Suc Let_def)
lemma rec_nat_add_eq_if [simp]: ‹rec_nat a f (numeral v + n) = (let pv = pred_numeral v in f (pv + n) (rec_nat a f (pv + n)))› by (simp add: numeral_eq_Suc Let_def)
text‹Case analysis on 🍋‹n 🚫›.› lemma less_2_cases: ‹n 🚫==> n = 0 ∨ n = Suc 0› by (auto simp add: numeral_2_eq_2)
lemma less_2_cases_iff: ‹n 🚫⟷ n = 0 ∨ n = Suc 0› by (auto simp add: numeral_2_eq_2)
text‹Removal of Small Numerals: 0, 1 and (in additive positions) 2.› text‹bh: Are these rules really a good idea? LCP: well, it already happens for 0 and 1!›
lemma add_2_eq_Suc [simp]: ‹2 + n = Suc (Suc n)› by simp
lemma numeral_add_unfold_funpow: ‹numeral k + a = ((+) 1 ^^ numeral k) a› 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: ‹numeral k = ((+) 1 ^^ numeral k) 0› using numeral_add_unfold_funpow [of k 0] by simp
end
context includes lifting_syntax begin
lemma transfer_rule_numeral: ‹((=) ===> R) numeral numeral› if [transfer_rule]: ‹R 0 0›‹R 1 1› ‹(R ===> R ===> R) (+) (+)› for R :: ‹'a::{semiring_numeral,monoid_add} ==> 'b::{semiring_numeral,monoid_add} ==> bool› proof - have‹((=) ===> R) (λk. ((+) 1 ^^ numeral k) 0) (λk. ((+) 1 ^^ numeral k) 0)› by transfer_prover moreoverhave‹numeral = (λk. ((+) (1::'a) ^^ numeral k) 0)› using numeral_add_unfold_funpow [where ?'a = 'a, of _ 0] by (simp add: fun_eq_iff) moreoverhave‹numeral = (λk. ((+) (1::'b) ^^ numeral k) 0)› using numeral_add_unfold_funpow [where ?'a = 'b, of _ 0] by (simp add: fun_eq_iff) ultimatelyshow ?thesis by simp qed
end
subsection‹Particular lemmas concerning 🍋‹2›\ context linordered_field begin
subsection‹Numeral equations as default simplification rules›
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 ‹Special Simplification for Constants›
text‹These distributive laws move literals inside sums and differences.›
lemmas distrib_right_numeral [simp] = distrib_right [of _ _ ‹numeral v›] for v lemmas distrib_left_numeral [simp] = distrib_left [of ‹numeral v›] for v lemmas left_diff_distrib_numeral [simp] = left_diff_distrib [of _ _ ‹numeral v›] for v lemmas right_diff_distrib_numeral [simp] = right_diff_distrib [of ‹numeral v›] for v
text‹These are actually for fields, like real›
lemmas zero_less_divide_iff_numeral [simp, no_atp] = zero_less_divide_iff [of ‹numeral w›] for w lemmas divide_less_0_iff_numeral [simp, no_atp] = divide_less_0_iff [of ‹numeral w›] forw lemmas zero_le_divide_iff_numeral [simp, no_atp] = zero_le_divide_iff [of ‹numeral w›] for w lemmas divide_le_0_iff_numeral [simp, no_atp] = divide_le_0_iff [of ‹numeral w›] for w
text‹Replaces ‹inverse #nn›by ‹1/#nn›. It looks strange, but then other simprocs simplify the quotient.›
lemmas inverse_eq_divide_numeral [simp] =
inverse_eq_divide [of ‹numeral w›] for w
lemmas inverse_eq_divide_neg_numeral [simp] =
inverse_eq_divide [of ‹- numeral w›] for w
text‹These laws simplify inequalities, moving unary minus from a term into the literal.›
lemmas equation_minus_iff_numeral [no_atp] =
equation_minus_iff [of ‹numeral v›] for v
lemmas minus_equation_iff_numeral [no_atp] =
minus_equation_iff [of _ ‹numeral v›] for v
lemmas le_minus_iff_numeral [no_atp] =
le_minus_iff [of ‹numeral v›] for v
lemmas minus_le_iff_numeral [no_atp] =
minus_le_iff [of _ ‹numeral v›] for v
lemmas less_minus_iff_numeral [no_atp] =
less_minus_iff [of ‹numeral v›] for v
lemmas minus_less_iff_numeral [no_atp] =
minus_less_iff [of _ ‹numeral v›] for v
(* FIXME maybe simproc *)
text‹Cancellation of constant factors in comparisons (‹🚫close> and ‹≤›)›
lemmas mult_less_cancel_left_numeral [simp, no_atp] = mult_less_cancel_left [of ‹numeral v›] for v lemmas mult_less_cancel_right_numeral [simp, no_atp] = mult_less_cancel_right [of _ ‹numeral v›] for v lemmas mult_le_cancel_left_numeral [simp, no_atp] = mult_le_cancel_left [of ‹numeral v›] for v lemmas mult_le_cancel_right_numeral [simp, no_atp] = mult_le_cancel_right [of _ ‹numeral v›] for v
text‹Multiplying out constant divisors in comparisons (‹🚫close>, ‹≤›and ‹=›)› named_theorems divide_const_simps ‹simplification rules to simplify comparisons involving constant divisors› lemmas le_divide_eq_numeral1 [simp,divide_const_simps] = pos_le_divide_eq [of ‹numeral w›, OF zero_less_numeral] neg_le_divide_eq [of ‹- numeral w›, OF neg_numeral_less_zero] for w lemmas divide_le_eq_numeral1 [simp,divide_const_simps] = pos_divide_le_eq [of ‹numeral w›, OF zero_less_numeral] neg_divide_le_eq [of ‹- numeral w›, OF neg_numeral_less_zero] for w lemmas less_divide_eq_numeral1 [simp,divide_const_simps] = pos_less_divide_eq [of ‹numeral w›, OF zero_less_numeral] neg_less_divide_eq [of ‹- numeral w›, OF neg_numeral_less_zero] for w lemmas divide_less_eq_numeral1 [simp,divide_const_simps] = pos_divide_less_eq [of ‹numeral w›, OF zero_less_numeral] neg_divide_less_eq [of ‹- numeral w›, OF neg_numeral_less_zero] for w lemmas eq_divide_eq_numeral1 [simp,divide_const_simps] = eq_divide_eq [of _ _ ‹numeral w›] eq_divide_eq [of _ _ ‹- numeral w›] for w lemmas divide_eq_eq_numeral1 [simp,divide_const_simps] = divide_eq_eq [of _ ‹numeral w›] divide_eq_eq [of _ ‹- numeral w›] for w subsubsection ‹Optional Simplification Rules Involving Constants› text ‹Simplify quotients that are compared with a literal constant.› lemmas le_divide_eq_numeral [divide_const_simps] = le_divide_eq [of ‹numeral w›] le_divide_eq [of ‹- numeral w›] for w lemmas divide_le_eq_numeral [divide_const_simps] = divide_le_eq [of _ _ ‹numeral w›] divide_le_eq [of _ _ ‹- numeral w›] for w lemmas less_divide_eq_numeral [divide_const_simps] = less_divide_eq [of ‹numeral w›] less_divide_eq [of ‹- numeral w›] for w lemmas divide_less_eq_numeral [divide_const_simps] = divide_less_eq [of _ _ ‹numeral w›] divide_less_eq [of _ _ ‹- numeral w›] for w lemmas eq_divide_eq_numeral [divide_const_simps] = eq_divide_eq [of ‹numeral w›] eq_divide_eq [of ‹- numeral w›] for w lemmas divide_eq_eq_numeral [divide_const_simps] = divide_eq_eq [of _ _ ‹numeral w›] divide_eq_eq [of _ _ ‹- numeral w›] for w text ‹Not good as automatic simprules because they cause case splits.› lemmas [divide_const_simps] = le_divide_eq_1 divide_le_eq_1 less_divide_eq_1 divide_less_eq_1 subsection ‹Setting up simprocs› lemma mult_numeral_1: ‹Numeral1 * a = a› for a :: ‹'a::semiring_numeral› by simp lemma mult_numeral_1_right: ‹a * Numeral1 = a› for a :: ‹'a::semiring_numeral› by simp lemma divide_numeral_1: ‹a / Numeral1 = a› for a :: ‹'a::field› by simp lemma inverse_numeral_1: ‹inverse Numeral1 = (Numeral1::'a::division_ring)› by simp text ‹ Theorem lists for the cancellation simprocs. The use of a binary numeral for 1 reduces the number of special cases. › lemma mult_1s_semiring_numeral: ‹Numeral1 * a = a› ‹a * Numeral1 = a› for a :: ‹'a::semiring_numeral› by simp_all lemma mult_1s_ring_1: ‹- Numeral1 * b = - b› ‹b * - Numeral1 = - b› for b :: ‹'a::ring_1› by simp_all lemmas mult_1s = mult_1s_semiring_numeral mult_1s_ring_1 setup ‹ Reorient_Proc.add (fn Const (🍋‹numeral›, _) $ _ => true | Const (🍋‹uminus›, _) $ (Const (🍋‹numeral›, _) $ _) => true | _ => false) › simproc_setup reorient_numeral (‹numeral w = x›| ‹- numeral w = y›) = ‹K Reorient_Proc.proc› subsubsection ‹Simplification of arithmetic operations on integer constants› lemmas arith_special = (* already declared simp above *)
add_numeral_special add_neg_numeral_special
diff_numeral_special
lemma Let_numeral [simp]: ‹Let (numeral v) f = f (numeral v)› 🍋‹Unfold all ‹let›s involving constants› unfolding Let_def ..
lemma Let_neg_numeral [simp]: ‹Let (- numeral v) f = f (- numeral v)› 🍋‹Unfold all ‹let›s involving constants› unfolding Let_def ..
declaration‹ let fun number_of ctxt T n = if not (Sign.of_sort (Proof_Context.theory_of ctxt) (T, 🍋‹numeral›)) 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 ›
subsubsection ‹Simplification of arithmetic when nested to the right›
lemma add_numeral_left [simp]: ‹numeral v + (numeral w + z) = (numeral(v + w) + z)› by (simp_all add: add.assoc [symmetric])
lemma add_neg_numeral_left [simp]: ‹numeral v + (- numeral w + y) = (sub v w + y)› ‹- numeral v + (numeral w + y) = (sub w v + y)› ‹- numeral v + (- numeral w + y) = (- numeral(v + w) + y)› by (simp_all add: add.assoc [symmetric])
lemma mult_numeral_left_semiring_numeral: ‹numeral v * (numeral w * z) = (numeral(v * w) * z :: 'a::semiring_numeral)› by (simp add: mult.assoc [symmetric])
lemma mult_numeral_left_ring_1: ‹- numeral v * (numeral w * y) = (- numeral(v * w) * y :: 'a::ring_1)› ‹numeral v * (- numeral w * y) = (- numeral(v * w) * y :: 'a::ring_1)› ‹- numeral v * (- numeral w * y) = (numeral(v * w) * y :: 'a::ring_1)› 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 und die Messung sind noch experimentell.