
wenzelm@23164: (*  Title:      HOL/NatBin.thy
wenzelm@23164:     ID:         $Id$
wenzelm@23164:     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@23164:     Copyright   1999  University of Cambridge
wenzelm@23164: *)
wenzelm@23164: 
wenzelm@23164: header {* Binary arithmetic for the natural numbers *}
wenzelm@23164: 
wenzelm@23164: theory NatBin
wenzelm@23164: imports IntDiv
wenzelm@23164: begin
wenzelm@23164: 
wenzelm@23164: text {*
wenzelm@23164:   Arithmetic for naturals is reduced to that for the non-negative integers.
wenzelm@23164: *}
wenzelm@23164: 
haftmann@25571: instantiation nat :: number
haftmann@25571: begin
haftmann@25571: 
haftmann@25571: definition
haftmann@25571:   nat_number_of_def [code inline]: "number_of v = nat (number_of (v\<Colon>int))"
haftmann@25571: 
haftmann@25571: instance ..
haftmann@25571: 
haftmann@25571: end
wenzelm@23164: 
wenzelm@23164: abbreviation (xsymbols)
wenzelm@23164:   square :: "'a::power => 'a"  ("(_\<twosuperior>)" [1000] 999) where
wenzelm@23164:   "x\<twosuperior> == x^2"
wenzelm@23164: 
wenzelm@23164: notation (latex output)
wenzelm@23164:   square  ("(_\<twosuperior>)" [1000] 999)
wenzelm@23164: 
wenzelm@23164: notation (HTML output)
wenzelm@23164:   square  ("(_\<twosuperior>)" [1000] 999)
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
wenzelm@23164: 
wenzelm@23164: declare nat_0 [simp] nat_1 [simp]
wenzelm@23164: 
wenzelm@23164: lemma nat_number_of [simp]: "nat (number_of w) = number_of w"
wenzelm@23164: by (simp add: nat_number_of_def)
wenzelm@23164: 
wenzelm@23164: lemma nat_numeral_0_eq_0 [simp]: "Numeral0 = (0::nat)"
wenzelm@23164: by (simp add: nat_number_of_def)
wenzelm@23164: 
wenzelm@23164: lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)"
wenzelm@23164: by (simp add: nat_1 nat_number_of_def)
wenzelm@23164: 
wenzelm@23164: lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
wenzelm@23164: by (simp add: nat_numeral_1_eq_1)
wenzelm@23164: 
wenzelm@23164: lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
wenzelm@23164: apply (unfold nat_number_of_def)
wenzelm@23164: apply (rule nat_2)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: text{*Distributive laws for type @{text nat}.  The others are in theory
wenzelm@23164:    @{text IntArith}, but these require div and mod to be defined for type
wenzelm@23164:    "int".  They also need some of the lemmas proved above.*}
wenzelm@23164: 
wenzelm@23164: lemma nat_div_distrib: "(0::int) <= z ==> nat (z div z') = nat z div nat z'"
wenzelm@23164: apply (case_tac "0 <= z'")
wenzelm@23164: apply (auto simp add: div_nonneg_neg_le0 DIVISION_BY_ZERO_DIV)
wenzelm@23164: apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO)
huffman@23365: apply (auto elim!: nonneg_eq_int)
wenzelm@23164: apply (rename_tac m m')
huffman@23365: apply (subgoal_tac "0 <= int m div int m'")
wenzelm@23164:  prefer 2 apply (simp add: nat_numeral_0_eq_0 pos_imp_zdiv_nonneg_iff) 
huffman@23307: apply (rule of_nat_eq_iff [where 'a=int, THEN iffD1], simp)
huffman@23365: apply (rule_tac r = "int (m mod m') " in quorem_div)
wenzelm@23164:  prefer 2 apply force
huffman@23365: apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0
huffman@23307:                  of_nat_add [symmetric] of_nat_mult [symmetric]
huffman@23307:             del: of_nat_add of_nat_mult)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: (*Fails if z'<0: the LHS collapses to (nat z) but the RHS doesn't*)
wenzelm@23164: lemma nat_mod_distrib:
wenzelm@23164:      "[| (0::int) <= z;  0 <= z' |] ==> nat (z mod z') = nat z mod nat z'"
wenzelm@23164: apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO)
huffman@23365: apply (auto elim!: nonneg_eq_int)
wenzelm@23164: apply (rename_tac m m')
huffman@23365: apply (subgoal_tac "0 <= int m mod int m'")
huffman@23365:  prefer 2 apply (simp add: nat_less_iff nat_numeral_0_eq_0 pos_mod_sign)
huffman@23365: apply (rule int_int_eq [THEN iffD1], simp)
huffman@23365: apply (rule_tac q = "int (m div m') " in quorem_mod)
wenzelm@23164:  prefer 2 apply force
huffman@23365: apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0
huffman@23307:                  of_nat_add [symmetric] of_nat_mult [symmetric]
huffman@23307:             del: of_nat_add of_nat_mult)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: text{*Suggested by Matthias Daum*}
wenzelm@23164: lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
wenzelm@23164: apply (subgoal_tac "nat x div nat k < nat x")
wenzelm@23164:  apply (simp (asm_lr) add: nat_div_distrib [symmetric])
wenzelm@23164: apply (rule Divides.div_less_dividend, simp_all) 
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
wenzelm@23164: 
wenzelm@23164: (*"neg" is used in rewrite rules for binary comparisons*)
wenzelm@23164: lemma int_nat_number_of [simp]:
huffman@23365:      "int (number_of v) =  
huffman@23307:          (if neg (number_of v :: int) then 0  
huffman@23307:           else (number_of v :: int))"
huffman@23307: by (simp del: nat_number_of
huffman@23307: 	 add: neg_nat nat_number_of_def not_neg_nat add_assoc)
huffman@23307: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*Successor *}
wenzelm@23164: 
wenzelm@23164: lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
wenzelm@23164: apply (rule sym)
wenzelm@23164: apply (simp add: nat_eq_iff int_Suc)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma Suc_nat_number_of_add:
wenzelm@23164:      "Suc (number_of v + n) =  
wenzelm@23164:         (if neg (number_of v :: int) then 1+n else number_of (Numeral.succ v) + n)" 
wenzelm@23164: by (simp del: nat_number_of 
wenzelm@23164:          add: nat_number_of_def neg_nat
wenzelm@23164:               Suc_nat_eq_nat_zadd1 number_of_succ) 
wenzelm@23164: 
wenzelm@23164: lemma Suc_nat_number_of [simp]:
wenzelm@23164:      "Suc (number_of v) =  
wenzelm@23164:         (if neg (number_of v :: int) then 1 else number_of (Numeral.succ v))"
wenzelm@23164: apply (cut_tac n = 0 in Suc_nat_number_of_add)
wenzelm@23164: apply (simp cong del: if_weak_cong)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*Addition *}
wenzelm@23164: 
wenzelm@23164: (*"neg" is used in rewrite rules for binary comparisons*)
wenzelm@23164: lemma add_nat_number_of [simp]:
wenzelm@23164:      "(number_of v :: nat) + number_of v' =  
wenzelm@23164:          (if neg (number_of v :: int) then number_of v'  
wenzelm@23164:           else if neg (number_of v' :: int) then number_of v  
wenzelm@23164:           else number_of (v + v'))"
wenzelm@23164: by (force dest!: neg_nat
wenzelm@23164:           simp del: nat_number_of
wenzelm@23164:           simp add: nat_number_of_def nat_add_distrib [symmetric]) 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*Subtraction *}
wenzelm@23164: 
wenzelm@23164: lemma diff_nat_eq_if:
wenzelm@23164:      "nat z - nat z' =  
wenzelm@23164:         (if neg z' then nat z   
wenzelm@23164:          else let d = z-z' in     
wenzelm@23164:               if neg d then 0 else nat d)"
wenzelm@23164: apply (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma diff_nat_number_of [simp]: 
wenzelm@23164:      "(number_of v :: nat) - number_of v' =  
wenzelm@23164:         (if neg (number_of v' :: int) then number_of v  
wenzelm@23164:          else let d = number_of (v + uminus v') in     
wenzelm@23164:               if neg d then 0 else nat d)"
wenzelm@23164: by (simp del: nat_number_of add: diff_nat_eq_if nat_number_of_def) 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*Multiplication *}
wenzelm@23164: 
wenzelm@23164: lemma mult_nat_number_of [simp]:
wenzelm@23164:      "(number_of v :: nat) * number_of v' =  
wenzelm@23164:        (if neg (number_of v :: int) then 0 else number_of (v * v'))"
wenzelm@23164: by (force dest!: neg_nat
wenzelm@23164:           simp del: nat_number_of
wenzelm@23164:           simp add: nat_number_of_def nat_mult_distrib [symmetric]) 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*Quotient *}
wenzelm@23164: 
wenzelm@23164: lemma div_nat_number_of [simp]:
wenzelm@23164:      "(number_of v :: nat)  div  number_of v' =  
wenzelm@23164:           (if neg (number_of v :: int) then 0  
wenzelm@23164:            else nat (number_of v div number_of v'))"
wenzelm@23164: by (force dest!: neg_nat
wenzelm@23164:           simp del: nat_number_of
wenzelm@23164:           simp add: nat_number_of_def nat_div_distrib [symmetric]) 
wenzelm@23164: 
wenzelm@23164: lemma one_div_nat_number_of [simp]:
wenzelm@23164:      "(Suc 0)  div  number_of v' = (nat (1 div number_of v'))" 
wenzelm@23164: by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*Remainder *}
wenzelm@23164: 
wenzelm@23164: lemma mod_nat_number_of [simp]:
wenzelm@23164:      "(number_of v :: nat)  mod  number_of v' =  
wenzelm@23164:         (if neg (number_of v :: int) then 0  
wenzelm@23164:          else if neg (number_of v' :: int) then number_of v  
wenzelm@23164:          else nat (number_of v mod number_of v'))"
wenzelm@23164: by (force dest!: neg_nat
wenzelm@23164:           simp del: nat_number_of
wenzelm@23164:           simp add: nat_number_of_def nat_mod_distrib [symmetric]) 
wenzelm@23164: 
wenzelm@23164: lemma one_mod_nat_number_of [simp]:
wenzelm@23164:      "(Suc 0)  mod  number_of v' =  
wenzelm@23164:         (if neg (number_of v' :: int) then Suc 0
wenzelm@23164:          else nat (1 mod number_of v'))"
wenzelm@23164: by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsubsection{* Divisibility *}
wenzelm@23164: 
wenzelm@23164: lemmas dvd_eq_mod_eq_0_number_of =
wenzelm@23164:   dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard]
wenzelm@23164: 
wenzelm@23164: declare dvd_eq_mod_eq_0_number_of [simp]
wenzelm@23164: 
wenzelm@23164: ML
wenzelm@23164: {*
wenzelm@23164: val nat_number_of_def = thm"nat_number_of_def";
wenzelm@23164: 
wenzelm@23164: val nat_number_of = thm"nat_number_of";
wenzelm@23164: val nat_numeral_0_eq_0 = thm"nat_numeral_0_eq_0";
wenzelm@23164: val nat_numeral_1_eq_1 = thm"nat_numeral_1_eq_1";
wenzelm@23164: val numeral_1_eq_Suc_0 = thm"numeral_1_eq_Suc_0";
wenzelm@23164: val numeral_2_eq_2 = thm"numeral_2_eq_2";
wenzelm@23164: val nat_div_distrib = thm"nat_div_distrib";
wenzelm@23164: val nat_mod_distrib = thm"nat_mod_distrib";
wenzelm@23164: val int_nat_number_of = thm"int_nat_number_of";
wenzelm@23164: val Suc_nat_eq_nat_zadd1 = thm"Suc_nat_eq_nat_zadd1";
wenzelm@23164: val Suc_nat_number_of_add = thm"Suc_nat_number_of_add";
wenzelm@23164: val Suc_nat_number_of = thm"Suc_nat_number_of";
wenzelm@23164: val add_nat_number_of = thm"add_nat_number_of";
wenzelm@23164: val diff_nat_eq_if = thm"diff_nat_eq_if";
wenzelm@23164: val diff_nat_number_of = thm"diff_nat_number_of";
wenzelm@23164: val mult_nat_number_of = thm"mult_nat_number_of";
wenzelm@23164: val div_nat_number_of = thm"div_nat_number_of";
wenzelm@23164: val mod_nat_number_of = thm"mod_nat_number_of";
wenzelm@23164: *}
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsection{*Comparisons*}
wenzelm@23164: 
wenzelm@23164: subsubsection{*Equals (=) *}
wenzelm@23164: 
wenzelm@23164: lemma eq_nat_nat_iff:
wenzelm@23164:      "[| (0::int) <= z;  0 <= z' |] ==> (nat z = nat z') = (z=z')"
wenzelm@23164: by (auto elim!: nonneg_eq_int)
wenzelm@23164: 
wenzelm@23164: (*"neg" is used in rewrite rules for binary comparisons*)
wenzelm@23164: lemma eq_nat_number_of [simp]:
wenzelm@23164:      "((number_of v :: nat) = number_of v') =  
wenzelm@23164:       (if neg (number_of v :: int) then (iszero (number_of v' :: int) | neg (number_of v' :: int))  
wenzelm@23164:        else if neg (number_of v' :: int) then iszero (number_of v :: int)  
wenzelm@23164:        else iszero (number_of (v + uminus v') :: int))"
wenzelm@23164: apply (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def
wenzelm@23164:                   eq_nat_nat_iff eq_number_of_eq nat_0 iszero_def
wenzelm@23164:             split add: split_if cong add: imp_cong)
wenzelm@23164: apply (simp only: nat_eq_iff nat_eq_iff2)
wenzelm@23164: apply (simp add: not_neg_eq_ge_0 [symmetric])
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*Less-than (<) *}
wenzelm@23164: 
wenzelm@23164: (*"neg" is used in rewrite rules for binary comparisons*)
wenzelm@23164: lemma less_nat_number_of [simp]:
wenzelm@23164:      "((number_of v :: nat) < number_of v') =  
wenzelm@23164:          (if neg (number_of v :: int) then neg (number_of (uminus v') :: int)  
wenzelm@23164:           else neg (number_of (v + uminus v') :: int))"
wenzelm@23164: by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def
wenzelm@23164:                 nat_less_eq_zless less_number_of_eq_neg zless_nat_eq_int_zless
wenzelm@23164:          cong add: imp_cong, simp add: Pls_def)
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: (*Maps #n to n for n = 0, 1, 2*)
wenzelm@23164: lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsection{*Powers with Numeric Exponents*}
wenzelm@23164: 
wenzelm@23164: text{*We cannot refer to the number @{term 2} in @{text Ring_and_Field.thy}.
wenzelm@23164: We cannot prove general results about the numeral @{term "-1"}, so we have to
wenzelm@23164: use @{term "- 1"} instead.*}
wenzelm@23164: 
huffman@23277: lemma power2_eq_square: "(a::'a::recpower)\<twosuperior> = a * a"
wenzelm@23164:   by (simp add: numeral_2_eq_2 Power.power_Suc)
wenzelm@23164: 
huffman@23277: lemma zero_power2 [simp]: "(0::'a::{semiring_1,recpower})\<twosuperior> = 0"
wenzelm@23164:   by (simp add: power2_eq_square)
wenzelm@23164: 
huffman@23277: lemma one_power2 [simp]: "(1::'a::{semiring_1,recpower})\<twosuperior> = 1"
wenzelm@23164:   by (simp add: power2_eq_square)
wenzelm@23164: 
wenzelm@23164: lemma power3_eq_cube: "(x::'a::recpower) ^ 3 = x * x * x"
wenzelm@23164:   apply (subgoal_tac "3 = Suc (Suc (Suc 0))")
wenzelm@23164:   apply (erule ssubst)
wenzelm@23164:   apply (simp add: power_Suc mult_ac)
wenzelm@23164:   apply (unfold nat_number_of_def)
wenzelm@23164:   apply (subst nat_eq_iff)
wenzelm@23164:   apply simp
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: text{*Squares of literal numerals will be evaluated.*}
wenzelm@23164: lemmas power2_eq_square_number_of =
wenzelm@23164:     power2_eq_square [of "number_of w", standard]
wenzelm@23164: declare power2_eq_square_number_of [simp]
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: lemma zero_le_power2[simp]: "0 \<le> (a\<twosuperior>::'a::{ordered_idom,recpower})"
wenzelm@23164:   by (simp add: power2_eq_square)
wenzelm@23164: 
wenzelm@23164: lemma zero_less_power2[simp]:
wenzelm@23164:      "(0 < a\<twosuperior>) = (a \<noteq> (0::'a::{ordered_idom,recpower}))"
wenzelm@23164:   by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
wenzelm@23164: 
wenzelm@23164: lemma power2_less_0[simp]:
wenzelm@23164:   fixes a :: "'a::{ordered_idom,recpower}"
wenzelm@23164:   shows "~ (a\<twosuperior> < 0)"
wenzelm@23164: by (force simp add: power2_eq_square mult_less_0_iff) 
wenzelm@23164: 
wenzelm@23164: lemma zero_eq_power2[simp]:
wenzelm@23164:      "(a\<twosuperior> = 0) = (a = (0::'a::{ordered_idom,recpower}))"
wenzelm@23164:   by (force simp add: power2_eq_square mult_eq_0_iff)
wenzelm@23164: 
wenzelm@23164: lemma abs_power2[simp]:
wenzelm@23164:      "abs(a\<twosuperior>) = (a\<twosuperior>::'a::{ordered_idom,recpower})"
wenzelm@23164:   by (simp add: power2_eq_square abs_mult abs_mult_self)
wenzelm@23164: 
wenzelm@23164: lemma power2_abs[simp]:
wenzelm@23164:      "(abs a)\<twosuperior> = (a\<twosuperior>::'a::{ordered_idom,recpower})"
wenzelm@23164:   by (simp add: power2_eq_square abs_mult_self)
wenzelm@23164: 
wenzelm@23164: lemma power2_minus[simp]:
wenzelm@23164:      "(- a)\<twosuperior> = (a\<twosuperior>::'a::{comm_ring_1,recpower})"
wenzelm@23164:   by (simp add: power2_eq_square)
wenzelm@23164: 
wenzelm@23164: lemma power2_le_imp_le:
wenzelm@23164:   fixes x y :: "'a::{ordered_semidom,recpower}"
wenzelm@23164:   shows "\<lbrakk>x\<twosuperior> \<le> y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x \<le> y"
wenzelm@23164: unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
wenzelm@23164: 
wenzelm@23164: lemma power2_less_imp_less:
wenzelm@23164:   fixes x y :: "'a::{ordered_semidom,recpower}"
wenzelm@23164:   shows "\<lbrakk>x\<twosuperior> < y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x < y"
wenzelm@23164: by (rule power_less_imp_less_base)
wenzelm@23164: 
wenzelm@23164: lemma power2_eq_imp_eq:
wenzelm@23164:   fixes x y :: "'a::{ordered_semidom,recpower}"
wenzelm@23164:   shows "\<lbrakk>x\<twosuperior> = y\<twosuperior>; 0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> x = y"
wenzelm@23164: unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base, simp)
wenzelm@23164: 
wenzelm@23164: lemma power_minus1_even[simp]: "(- 1) ^ (2*n) = (1::'a::{comm_ring_1,recpower})"
wenzelm@23164: apply (induct "n")
wenzelm@23164: apply (auto simp add: power_Suc power_add)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma power_even_eq: "(a::'a::recpower) ^ (2*n) = (a^n)^2"
wenzelm@23164: by (subst mult_commute) (simp add: power_mult)
wenzelm@23164: 
wenzelm@23164: lemma power_odd_eq: "(a::int) ^ Suc(2*n) = a * (a^n)^2"
wenzelm@23164: by (simp add: power_even_eq) 
wenzelm@23164: 
wenzelm@23164: lemma power_minus_even [simp]:
wenzelm@23164:      "(-a) ^ (2*n) = (a::'a::{comm_ring_1,recpower}) ^ (2*n)"
wenzelm@23164: by (simp add: power_minus1_even power_minus [of a]) 
wenzelm@23164: 
wenzelm@23164: lemma zero_le_even_power'[simp]:
wenzelm@23164:      "0 \<le> (a::'a::{ordered_idom,recpower}) ^ (2*n)"
wenzelm@23164: proof (induct "n")
wenzelm@23164:   case 0
wenzelm@23164:     show ?case by (simp add: zero_le_one)
wenzelm@23164: next
wenzelm@23164:   case (Suc n)
wenzelm@23164:     have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" 
wenzelm@23164:       by (simp add: mult_ac power_add power2_eq_square)
wenzelm@23164:     thus ?case
wenzelm@23164:       by (simp add: prems zero_le_mult_iff)
wenzelm@23164: qed
wenzelm@23164: 
wenzelm@23164: lemma odd_power_less_zero:
wenzelm@23164:      "(a::'a::{ordered_idom,recpower}) < 0 ==> a ^ Suc(2*n) < 0"
wenzelm@23164: proof (induct "n")
wenzelm@23164:   case 0
wenzelm@23389:   then show ?case by (simp add: Power.power_Suc)
wenzelm@23164: next
wenzelm@23164:   case (Suc n)
wenzelm@23389:   have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)" 
wenzelm@23389:     by (simp add: mult_ac power_add power2_eq_square Power.power_Suc)
wenzelm@23389:   thus ?case
wenzelm@23389:     by (simp add: prems mult_less_0_iff mult_neg_neg)
wenzelm@23164: qed
wenzelm@23164: 
wenzelm@23164: lemma odd_0_le_power_imp_0_le:
wenzelm@23164:      "0 \<le> a  ^ Suc(2*n) ==> 0 \<le> (a::'a::{ordered_idom,recpower})"
wenzelm@23164: apply (insert odd_power_less_zero [of a n]) 
wenzelm@23164: apply (force simp add: linorder_not_less [symmetric]) 
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: text{*Simprules for comparisons where common factors can be cancelled.*}
wenzelm@23164: lemmas zero_compare_simps =
wenzelm@23164:     add_strict_increasing add_strict_increasing2 add_increasing
wenzelm@23164:     zero_le_mult_iff zero_le_divide_iff 
wenzelm@23164:     zero_less_mult_iff zero_less_divide_iff 
wenzelm@23164:     mult_le_0_iff divide_le_0_iff 
wenzelm@23164:     mult_less_0_iff divide_less_0_iff 
wenzelm@23164:     zero_le_power2 power2_less_0
wenzelm@23164: 
wenzelm@23164: subsubsection{*Nat *}
wenzelm@23164: 
wenzelm@23164: lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
wenzelm@23164: by (simp add: numerals)
wenzelm@23164: 
wenzelm@23164: (*Expresses a natural number constant as the Suc of another one.
wenzelm@23164:   NOT suitable for rewriting because n recurs in the condition.*)
wenzelm@23164: lemmas expand_Suc = Suc_pred' [of "number_of v", standard]
wenzelm@23164: 
wenzelm@23164: subsubsection{*Arith *}
wenzelm@23164: 
wenzelm@23164: lemma Suc_eq_add_numeral_1: "Suc n = n + 1"
wenzelm@23164: by (simp add: numerals)
wenzelm@23164: 
wenzelm@23164: lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n"
wenzelm@23164: by (simp add: numerals)
wenzelm@23164: 
wenzelm@23164: (* These two can be useful when m = number_of... *)
wenzelm@23164: 
wenzelm@23164: lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
wenzelm@23164: apply (case_tac "m")
wenzelm@23164: apply (simp_all add: numerals)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
wenzelm@23164: apply (case_tac "m")
wenzelm@23164: apply (simp_all add: numerals)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
wenzelm@23164: apply (case_tac "m")
wenzelm@23164: apply (simp_all add: numerals)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsection{*Comparisons involving (0::nat) *}
wenzelm@23164: 
wenzelm@23164: text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
wenzelm@23164: 
wenzelm@23164: lemma eq_number_of_0 [simp]:
wenzelm@23164:      "(number_of v = (0::nat)) =  
wenzelm@23164:       (if neg (number_of v :: int) then True else iszero (number_of v :: int))"
wenzelm@23164: by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0)
wenzelm@23164: 
wenzelm@23164: lemma eq_0_number_of [simp]:
wenzelm@23164:      "((0::nat) = number_of v) =  
wenzelm@23164:       (if neg (number_of v :: int) then True else iszero (number_of v :: int))"
wenzelm@23164: by (rule trans [OF eq_sym_conv eq_number_of_0])
wenzelm@23164: 
wenzelm@23164: lemma less_0_number_of [simp]:
wenzelm@23164:      "((0::nat) < number_of v) = neg (number_of (uminus v) :: int)"
wenzelm@23164: by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] Pls_def)
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
wenzelm@23164: by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0)
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsection{*Comparisons involving  @{term Suc} *}
wenzelm@23164: 
wenzelm@23164: lemma eq_number_of_Suc [simp]:
wenzelm@23164:      "(number_of v = Suc n) =  
wenzelm@23164:         (let pv = number_of (Numeral.pred v) in  
wenzelm@23164:          if neg pv then False else nat pv = n)"
wenzelm@23164: apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
wenzelm@23164:                   number_of_pred nat_number_of_def 
wenzelm@23164:             split add: split_if)
wenzelm@23164: apply (rule_tac x = "number_of v" in spec)
wenzelm@23164: apply (auto simp add: nat_eq_iff)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma Suc_eq_number_of [simp]:
wenzelm@23164:      "(Suc n = number_of v) =  
wenzelm@23164:         (let pv = number_of (Numeral.pred v) in  
wenzelm@23164:          if neg pv then False else nat pv = n)"
wenzelm@23164: by (rule trans [OF eq_sym_conv eq_number_of_Suc])
wenzelm@23164: 
wenzelm@23164: lemma less_number_of_Suc [simp]:
wenzelm@23164:      "(number_of v < Suc n) =  
wenzelm@23164:         (let pv = number_of (Numeral.pred v) in  
wenzelm@23164:          if neg pv then True else nat pv < n)"
wenzelm@23164: apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
wenzelm@23164:                   number_of_pred nat_number_of_def  
wenzelm@23164:             split add: split_if)
wenzelm@23164: apply (rule_tac x = "number_of v" in spec)
wenzelm@23164: apply (auto simp add: nat_less_iff)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma less_Suc_number_of [simp]:
wenzelm@23164:      "(Suc n < number_of v) =  
wenzelm@23164:         (let pv = number_of (Numeral.pred v) in  
wenzelm@23164:          if neg pv then False else n < nat pv)"
wenzelm@23164: apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
wenzelm@23164:                   number_of_pred nat_number_of_def
wenzelm@23164:             split add: split_if)
wenzelm@23164: apply (rule_tac x = "number_of v" in spec)
wenzelm@23164: apply (auto simp add: zless_nat_eq_int_zless)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma le_number_of_Suc [simp]:
wenzelm@23164:      "(number_of v <= Suc n) =  
wenzelm@23164:         (let pv = number_of (Numeral.pred v) in  
wenzelm@23164:          if neg pv then True else nat pv <= n)"
wenzelm@23164: by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric])
wenzelm@23164: 
wenzelm@23164: lemma le_Suc_number_of [simp]:
wenzelm@23164:      "(Suc n <= number_of v) =  
wenzelm@23164:         (let pv = number_of (Numeral.pred v) in  
wenzelm@23164:          if neg pv then False else n <= nat pv)"
wenzelm@23164: by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric])
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: lemma lemma1: "(m+m = n+n) = (m = (n::int))"
wenzelm@23164: by auto
wenzelm@23164: 
wenzelm@23164: lemma lemma2: "m+m ~= (1::int) + (n + n)"
wenzelm@23164: apply auto
wenzelm@23164: apply (drule_tac f = "%x. x mod 2" in arg_cong)
wenzelm@23164: apply (simp add: zmod_zadd1_eq)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma eq_number_of_BIT_BIT:
wenzelm@23164:      "((number_of (v BIT x) ::int) = number_of (w BIT y)) =  
wenzelm@23164:       (x=y & (((number_of v) ::int) = number_of w))"
wenzelm@23164: apply (simp only: number_of_BIT lemma1 lemma2 eq_commute
wenzelm@23164:                OrderedGroup.add_left_cancel add_assoc OrderedGroup.add_0_left
wenzelm@23164:             split add: bit.split)
wenzelm@23164: apply simp
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma eq_number_of_BIT_Pls:
wenzelm@23164:      "((number_of (v BIT x) ::int) = Numeral0) =  
wenzelm@23164:       (x=bit.B0 & (((number_of v) ::int) = Numeral0))"
wenzelm@23164: apply (simp only: simp_thms  add: number_of_BIT number_of_Pls eq_commute
wenzelm@23164:             split add: bit.split cong: imp_cong)
wenzelm@23164: apply (rule_tac x = "number_of v" in spec, safe)
wenzelm@23164: apply (simp_all (no_asm_use))
wenzelm@23164: apply (drule_tac f = "%x. x mod 2" in arg_cong)
wenzelm@23164: apply (simp add: zmod_zadd1_eq)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma eq_number_of_BIT_Min:
wenzelm@23164:      "((number_of (v BIT x) ::int) = number_of Numeral.Min) =  
wenzelm@23164:       (x=bit.B1 & (((number_of v) ::int) = number_of Numeral.Min))"
wenzelm@23164: apply (simp only: simp_thms  add: number_of_BIT number_of_Min eq_commute
wenzelm@23164:             split add: bit.split cong: imp_cong)
wenzelm@23164: apply (rule_tac x = "number_of v" in spec, auto)
wenzelm@23164: apply (drule_tac f = "%x. x mod 2" in arg_cong, auto)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Numeral.Min"
wenzelm@23164: by auto
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsection{*Max and Min Combined with @{term Suc} *}
wenzelm@23164: 
wenzelm@23164: lemma max_number_of_Suc [simp]:
wenzelm@23164:      "max (Suc n) (number_of v) =  
wenzelm@23164:         (let pv = number_of (Numeral.pred v) in  
wenzelm@23164:          if neg pv then Suc n else Suc(max n (nat pv)))"
wenzelm@23164: apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
wenzelm@23164:             split add: split_if nat.split)
wenzelm@23164: apply (rule_tac x = "number_of v" in spec) 
wenzelm@23164: apply auto
wenzelm@23164: done
wenzelm@23164:  
wenzelm@23164: lemma max_Suc_number_of [simp]:
wenzelm@23164:      "max (number_of v) (Suc n) =  
wenzelm@23164:         (let pv = number_of (Numeral.pred v) in  
wenzelm@23164:          if neg pv then Suc n else Suc(max (nat pv) n))"
wenzelm@23164: apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
wenzelm@23164:             split add: split_if nat.split)
wenzelm@23164: apply (rule_tac x = "number_of v" in spec) 
wenzelm@23164: apply auto
wenzelm@23164: done
wenzelm@23164:  
wenzelm@23164: lemma min_number_of_Suc [simp]:
wenzelm@23164:      "min (Suc n) (number_of v) =  
wenzelm@23164:         (let pv = number_of (Numeral.pred v) in  
wenzelm@23164:          if neg pv then 0 else Suc(min n (nat pv)))"
wenzelm@23164: apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
wenzelm@23164:             split add: split_if nat.split)
wenzelm@23164: apply (rule_tac x = "number_of v" in spec) 
wenzelm@23164: apply auto
wenzelm@23164: done
wenzelm@23164:  
wenzelm@23164: lemma min_Suc_number_of [simp]:
wenzelm@23164:      "min (number_of v) (Suc n) =  
wenzelm@23164:         (let pv = number_of (Numeral.pred v) in  
wenzelm@23164:          if neg pv then 0 else Suc(min (nat pv) n))"
wenzelm@23164: apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
wenzelm@23164:             split add: split_if nat.split)
wenzelm@23164: apply (rule_tac x = "number_of v" in spec) 
wenzelm@23164: apply auto
wenzelm@23164: done
wenzelm@23164:  
wenzelm@23164: subsection{*Literal arithmetic involving powers*}
wenzelm@23164: 
wenzelm@23164: lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n"
wenzelm@23164: apply (induct "n")
wenzelm@23164: apply (simp_all (no_asm_simp) add: nat_mult_distrib)
wenzelm@23164: done
wenzelm@23164: 
wenzelm@23164: lemma power_nat_number_of:
wenzelm@23164:      "(number_of v :: nat) ^ n =  
wenzelm@23164:        (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
wenzelm@23164: by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
wenzelm@23164:          split add: split_if cong: imp_cong)
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard]
wenzelm@23164: declare power_nat_number_of_number_of [simp]
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: 
huffman@23294: text{*For arbitrary rings*}
wenzelm@23164: 
huffman@23294: lemma power_number_of_even:
huffman@23294:   fixes z :: "'a::{number_ring,recpower}"
huffman@23294:   shows "z ^ number_of (w BIT bit.B0) = (let w = z ^ (number_of w) in w * w)"
wenzelm@23164: unfolding Let_def nat_number_of_def number_of_BIT bit.cases
wenzelm@23164: apply (rule_tac x = "number_of w" in spec, clarify)
wenzelm@23164: apply (case_tac " (0::int) <= x")
wenzelm@23164: apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square)
wenzelm@23164: done
wenzelm@23164: 
huffman@23294: lemma power_number_of_odd:
huffman@23294:   fixes z :: "'a::{number_ring,recpower}"
huffman@23294:   shows "z ^ number_of (w BIT bit.B1) = (if (0::int) <= number_of w
wenzelm@23164:      then (let w = z ^ (number_of w) in z * w * w) else 1)"
wenzelm@23164: unfolding Let_def nat_number_of_def number_of_BIT bit.cases
wenzelm@23164: apply (rule_tac x = "number_of w" in spec, auto)
wenzelm@23164: apply (simp only: nat_add_distrib nat_mult_distrib)
wenzelm@23164: apply simp
huffman@23294: apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat power_Suc)
wenzelm@23164: done
wenzelm@23164: 
huffman@23294: lemmas zpower_number_of_even = power_number_of_even [where 'a=int]
huffman@23294: lemmas zpower_number_of_odd = power_number_of_odd [where 'a=int]
wenzelm@23164: 
huffman@23294: lemmas power_number_of_even_number_of [simp] =
huffman@23294:     power_number_of_even [of "number_of v", standard]
wenzelm@23164: 
huffman@23294: lemmas power_number_of_odd_number_of [simp] =
huffman@23294:     power_number_of_odd [of "number_of v", standard]
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: ML
wenzelm@23164: {*
haftmann@25481: val numeral_ss = simpset() addsimps @{thms numerals};
wenzelm@23164: 
wenzelm@23164: val nat_bin_arith_setup =
wenzelm@24093:  LinArith.map_data
wenzelm@23164:    (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
wenzelm@23164:      {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms,
wenzelm@23164:       inj_thms = inj_thms,
wenzelm@23164:       lessD = lessD, neqE = neqE,
wenzelm@23164:       simpset = simpset addsimps [Suc_nat_number_of, int_nat_number_of,
haftmann@25481:         @{thm not_neg_number_of_Pls}, @{thm neg_number_of_Min},
haftmann@25481:         @{thm neg_number_of_BIT}]})
wenzelm@23164: *}
wenzelm@23164: 
wenzelm@24075: declaration {* K nat_bin_arith_setup *}
wenzelm@23164: 
wenzelm@23164: (* Enable arith to deal with div/mod k where k is a numeral: *)
wenzelm@23164: declare split_div[of _ _ "number_of k", standard, arith_split]
wenzelm@23164: declare split_mod[of _ _ "number_of k", standard, arith_split]
wenzelm@23164: 
wenzelm@23164: lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
wenzelm@23164:   by (simp add: number_of_Pls nat_number_of_def)
wenzelm@23164: 
wenzelm@23164: lemma nat_number_of_Min: "number_of Numeral.Min = (0::nat)"
wenzelm@23164:   apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
wenzelm@23164:   done
wenzelm@23164: 
wenzelm@23164: lemma nat_number_of_BIT_1:
wenzelm@23164:   "number_of (w BIT bit.B1) =
wenzelm@23164:     (if neg (number_of w :: int) then 0
wenzelm@23164:      else let n = number_of w in Suc (n + n))"
wenzelm@23164:   apply (simp only: nat_number_of_def Let_def split: split_if)
wenzelm@23164:   apply (intro conjI impI)
wenzelm@23164:    apply (simp add: neg_nat neg_number_of_BIT)
wenzelm@23164:   apply (rule int_int_eq [THEN iffD1])
wenzelm@23164:   apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms)
wenzelm@23164:   apply (simp only: number_of_BIT zadd_assoc split: bit.split)
wenzelm@23164:   apply simp
wenzelm@23164:   done
wenzelm@23164: 
wenzelm@23164: lemma nat_number_of_BIT_0:
wenzelm@23164:     "number_of (w BIT bit.B0) = (let n::nat = number_of w in n + n)"
wenzelm@23164:   apply (simp only: nat_number_of_def Let_def)
wenzelm@23164:   apply (cases "neg (number_of w :: int)")
wenzelm@23164:    apply (simp add: neg_nat neg_number_of_BIT)
wenzelm@23164:   apply (rule int_int_eq [THEN iffD1])
wenzelm@23164:   apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms)
wenzelm@23164:   apply (simp only: number_of_BIT zadd_assoc)
wenzelm@23164:   apply simp
wenzelm@23164:   done
wenzelm@23164: 
wenzelm@23164: lemmas nat_number =
wenzelm@23164:   nat_number_of_Pls nat_number_of_Min
wenzelm@23164:   nat_number_of_BIT_1 nat_number_of_BIT_0
wenzelm@23164: 
wenzelm@23164: lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
wenzelm@23164:   by (simp add: Let_def)
wenzelm@23164: 
wenzelm@23164: lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring,recpower})"
huffman@23294: by (simp add: power_mult power_Suc); 
wenzelm@23164: 
wenzelm@23164: lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring,recpower})"
wenzelm@23164: by (simp add: power_mult power_Suc); 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsection{*Literal arithmetic and @{term of_nat}*}
wenzelm@23164: 
wenzelm@23164: lemma of_nat_double:
wenzelm@23164:      "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
wenzelm@23164: by (simp only: mult_2 nat_add_distrib of_nat_add) 
wenzelm@23164: 
wenzelm@23164: lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
wenzelm@23164: by (simp only: nat_number_of_def)
wenzelm@23164: 
wenzelm@23164: lemma of_nat_number_of_lemma:
wenzelm@23164:      "of_nat (number_of v :: nat) =  
wenzelm@23164:          (if 0 \<le> (number_of v :: int) 
wenzelm@23164:           then (number_of v :: 'a :: number_ring)
wenzelm@23164:           else 0)"
wenzelm@23164: by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat);
wenzelm@23164: 
wenzelm@23164: lemma of_nat_number_of_eq [simp]:
wenzelm@23164:      "of_nat (number_of v :: nat) =  
wenzelm@23164:          (if neg (number_of v :: int) then 0  
wenzelm@23164:           else (number_of v :: 'a :: number_ring))"
wenzelm@23164: by (simp only: of_nat_number_of_lemma neg_def, simp) 
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsection {*Lemmas for the Combination and Cancellation Simprocs*}
wenzelm@23164: 
wenzelm@23164: lemma nat_number_of_add_left:
wenzelm@23164:      "number_of v + (number_of v' + (k::nat)) =  
wenzelm@23164:          (if neg (number_of v :: int) then number_of v' + k  
wenzelm@23164:           else if neg (number_of v' :: int) then number_of v + k  
wenzelm@23164:           else number_of (v + v') + k)"
wenzelm@23164: by simp
wenzelm@23164: 
wenzelm@23164: lemma nat_number_of_mult_left:
wenzelm@23164:      "number_of v * (number_of v' * (k::nat)) =  
wenzelm@23164:          (if neg (number_of v :: int) then 0
wenzelm@23164:           else number_of (v * v') * k)"
wenzelm@23164: by simp
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*For @{text combine_numerals}*}
wenzelm@23164: 
wenzelm@23164: lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
wenzelm@23164: by (simp add: add_mult_distrib)
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*For @{text cancel_numerals}*}
wenzelm@23164: 
wenzelm@23164: lemma nat_diff_add_eq1:
wenzelm@23164:      "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
wenzelm@23164: by (simp split add: nat_diff_split add: add_mult_distrib)
wenzelm@23164: 
wenzelm@23164: lemma nat_diff_add_eq2:
wenzelm@23164:      "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
wenzelm@23164: by (simp split add: nat_diff_split add: add_mult_distrib)
wenzelm@23164: 
wenzelm@23164: lemma nat_eq_add_iff1:
wenzelm@23164:      "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
wenzelm@23164: by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164: 
wenzelm@23164: lemma nat_eq_add_iff2:
wenzelm@23164:      "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
wenzelm@23164: by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164: 
wenzelm@23164: lemma nat_less_add_iff1:
wenzelm@23164:      "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
wenzelm@23164: by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164: 
wenzelm@23164: lemma nat_less_add_iff2:
wenzelm@23164:      "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
wenzelm@23164: by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164: 
wenzelm@23164: lemma nat_le_add_iff1:
wenzelm@23164:      "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
wenzelm@23164: by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164: 
wenzelm@23164: lemma nat_le_add_iff2:
wenzelm@23164:      "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
wenzelm@23164: by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*For @{text cancel_numeral_factors} *}
wenzelm@23164: 
wenzelm@23164: lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
wenzelm@23164: by auto
wenzelm@23164: 
wenzelm@23164: lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
wenzelm@23164: by auto
wenzelm@23164: 
wenzelm@23164: lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
wenzelm@23164: by auto
wenzelm@23164: 
wenzelm@23164: lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
wenzelm@23164: by auto
wenzelm@23164: 
nipkow@23969: lemma nat_mult_dvd_cancel_disj[simp]:
nipkow@23969:   "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
nipkow@23969: by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
nipkow@23969: 
nipkow@23969: lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
nipkow@23969: by(auto)
nipkow@23969: 
wenzelm@23164: 
wenzelm@23164: subsubsection{*For @{text cancel_factor} *}
wenzelm@23164: 
wenzelm@23164: lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
wenzelm@23164: by auto
wenzelm@23164: 
wenzelm@23164: lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
wenzelm@23164: by auto
wenzelm@23164: 
wenzelm@23164: lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
wenzelm@23164: by auto
wenzelm@23164: 
nipkow@23969: lemma nat_mult_div_cancel_disj[simp]:
wenzelm@23164:      "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
wenzelm@23164: by (simp add: nat_mult_div_cancel1)
wenzelm@23164: 
wenzelm@23164: end




