src/HOL/NatBin.thy
 author haftmann Tue Jan 15 16:19:23 2008 +0100 (2008-01-15) changeset 25919 8b1c0d434824 parent 25571 c9e39eafc7a0 child 25965 05df64f786a4 permissions -rw-r--r--
joined theories IntDef, Numeral, IntArith to theory Int
```     1 (*  Title:      HOL/NatBin.thy
```
```     2     ID:         \$Id\$
```
```     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
```
```     4     Copyright   1999  University of Cambridge
```
```     5 *)
```
```     6
```
```     7 header {* Binary arithmetic for the natural numbers *}
```
```     8
```
```     9 theory NatBin
```
```    10 imports IntDiv
```
```    11 begin
```
```    12
```
```    13 text {*
```
```    14   Arithmetic for naturals is reduced to that for the non-negative integers.
```
```    15 *}
```
```    16
```
```    17 instantiation nat :: number
```
```    18 begin
```
```    19
```
```    20 definition
```
```    21   nat_number_of_def [code inline]: "number_of v = nat (number_of (v\<Colon>int))"
```
```    22
```
```    23 instance ..
```
```    24
```
```    25 end
```
```    26
```
```    27 abbreviation (xsymbols)
```
```    28   square :: "'a::power => 'a"  ("(_\<twosuperior>)" [1000] 999) where
```
```    29   "x\<twosuperior> == x^2"
```
```    30
```
```    31 notation (latex output)
```
```    32   square  ("(_\<twosuperior>)" [1000] 999)
```
```    33
```
```    34 notation (HTML output)
```
```    35   square  ("(_\<twosuperior>)" [1000] 999)
```
```    36
```
```    37
```
```    38 subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
```
```    39
```
```    40 declare nat_0 [simp] nat_1 [simp]
```
```    41
```
```    42 lemma nat_number_of [simp]: "nat (number_of w) = number_of w"
```
```    43 by (simp add: nat_number_of_def)
```
```    44
```
```    45 lemma nat_numeral_0_eq_0 [simp]: "Numeral0 = (0::nat)"
```
```    46 by (simp add: nat_number_of_def)
```
```    47
```
```    48 lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)"
```
```    49 by (simp add: nat_1 nat_number_of_def)
```
```    50
```
```    51 lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
```
```    52 by (simp add: nat_numeral_1_eq_1)
```
```    53
```
```    54 lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
```
```    55 apply (unfold nat_number_of_def)
```
```    56 apply (rule nat_2)
```
```    57 done
```
```    58
```
```    59
```
```    60 text{*Distributive laws for type @{text nat}.  The others are in theory
```
```    61    @{text IntArith}, but these require div and mod to be defined for type
```
```    62    "int".  They also need some of the lemmas proved above.*}
```
```    63
```
```    64 lemma nat_div_distrib: "(0::int) <= z ==> nat (z div z') = nat z div nat z'"
```
```    65 apply (case_tac "0 <= z'")
```
```    66 apply (auto simp add: div_nonneg_neg_le0 DIVISION_BY_ZERO_DIV)
```
```    67 apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO)
```
```    68 apply (auto elim!: nonneg_eq_int)
```
```    69 apply (rename_tac m m')
```
```    70 apply (subgoal_tac "0 <= int m div int m'")
```
```    71  prefer 2 apply (simp add: nat_numeral_0_eq_0 pos_imp_zdiv_nonneg_iff)
```
```    72 apply (rule of_nat_eq_iff [where 'a=int, THEN iffD1], simp)
```
```    73 apply (rule_tac r = "int (m mod m') " in quorem_div)
```
```    74  prefer 2 apply force
```
```    75 apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0
```
```    76                  of_nat_add [symmetric] of_nat_mult [symmetric]
```
```    77             del: of_nat_add of_nat_mult)
```
```    78 done
```
```    79
```
```    80 (*Fails if z'<0: the LHS collapses to (nat z) but the RHS doesn't*)
```
```    81 lemma nat_mod_distrib:
```
```    82      "[| (0::int) <= z;  0 <= z' |] ==> nat (z mod z') = nat z mod nat z'"
```
```    83 apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO)
```
```    84 apply (auto elim!: nonneg_eq_int)
```
```    85 apply (rename_tac m m')
```
```    86 apply (subgoal_tac "0 <= int m mod int m'")
```
```    87  prefer 2 apply (simp add: nat_less_iff nat_numeral_0_eq_0 pos_mod_sign)
```
```    88 apply (rule int_int_eq [THEN iffD1], simp)
```
```    89 apply (rule_tac q = "int (m div m') " in quorem_mod)
```
```    90  prefer 2 apply force
```
```    91 apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0
```
```    92                  of_nat_add [symmetric] of_nat_mult [symmetric]
```
```    93             del: of_nat_add of_nat_mult)
```
```    94 done
```
```    95
```
```    96 text{*Suggested by Matthias Daum*}
```
```    97 lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
```
```    98 apply (subgoal_tac "nat x div nat k < nat x")
```
```    99  apply (simp (asm_lr) add: nat_div_distrib [symmetric])
```
```   100 apply (rule Divides.div_less_dividend, simp_all)
```
```   101 done
```
```   102
```
```   103 subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
```
```   104
```
```   105 (*"neg" is used in rewrite rules for binary comparisons*)
```
```   106 lemma int_nat_number_of [simp]:
```
```   107      "int (number_of v) =
```
```   108          (if neg (number_of v :: int) then 0
```
```   109           else (number_of v :: int))"
```
```   110 by (simp del: nat_number_of
```
```   111 	 add: neg_nat nat_number_of_def not_neg_nat add_assoc)
```
```   112
```
```   113
```
```   114 subsubsection{*Successor *}
```
```   115
```
```   116 lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
```
```   117 apply (rule sym)
```
```   118 apply (simp add: nat_eq_iff int_Suc)
```
```   119 done
```
```   120
```
```   121 lemma Suc_nat_number_of_add:
```
```   122      "Suc (number_of v + n) =
```
```   123         (if neg (number_of v :: int) then 1+n else number_of (Int.succ v) + n)"
```
```   124 by (simp del: nat_number_of
```
```   125          add: nat_number_of_def neg_nat
```
```   126               Suc_nat_eq_nat_zadd1 number_of_succ)
```
```   127
```
```   128 lemma Suc_nat_number_of [simp]:
```
```   129      "Suc (number_of v) =
```
```   130         (if neg (number_of v :: int) then 1 else number_of (Int.succ v))"
```
```   131 apply (cut_tac n = 0 in Suc_nat_number_of_add)
```
```   132 apply (simp cong del: if_weak_cong)
```
```   133 done
```
```   134
```
```   135
```
```   136 subsubsection{*Addition *}
```
```   137
```
```   138 (*"neg" is used in rewrite rules for binary comparisons*)
```
```   139 lemma add_nat_number_of [simp]:
```
```   140      "(number_of v :: nat) + number_of v' =
```
```   141          (if neg (number_of v :: int) then number_of v'
```
```   142           else if neg (number_of v' :: int) then number_of v
```
```   143           else number_of (v + v'))"
```
```   144 by (force dest!: neg_nat
```
```   145           simp del: nat_number_of
```
```   146           simp add: nat_number_of_def nat_add_distrib [symmetric])
```
```   147
```
```   148
```
```   149 subsubsection{*Subtraction *}
```
```   150
```
```   151 lemma diff_nat_eq_if:
```
```   152      "nat z - nat z' =
```
```   153         (if neg z' then nat z
```
```   154          else let d = z-z' in
```
```   155               if neg d then 0 else nat d)"
```
```   156 apply (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0)
```
```   157 done
```
```   158
```
```   159 lemma diff_nat_number_of [simp]:
```
```   160      "(number_of v :: nat) - number_of v' =
```
```   161         (if neg (number_of v' :: int) then number_of v
```
```   162          else let d = number_of (v + uminus v') in
```
```   163               if neg d then 0 else nat d)"
```
```   164 by (simp del: nat_number_of add: diff_nat_eq_if nat_number_of_def)
```
```   165
```
```   166
```
```   167
```
```   168 subsubsection{*Multiplication *}
```
```   169
```
```   170 lemma mult_nat_number_of [simp]:
```
```   171      "(number_of v :: nat) * number_of v' =
```
```   172        (if neg (number_of v :: int) then 0 else number_of (v * v'))"
```
```   173 by (force dest!: neg_nat
```
```   174           simp del: nat_number_of
```
```   175           simp add: nat_number_of_def nat_mult_distrib [symmetric])
```
```   176
```
```   177
```
```   178
```
```   179 subsubsection{*Quotient *}
```
```   180
```
```   181 lemma div_nat_number_of [simp]:
```
```   182      "(number_of v :: nat)  div  number_of v' =
```
```   183           (if neg (number_of v :: int) then 0
```
```   184            else nat (number_of v div number_of v'))"
```
```   185 by (force dest!: neg_nat
```
```   186           simp del: nat_number_of
```
```   187           simp add: nat_number_of_def nat_div_distrib [symmetric])
```
```   188
```
```   189 lemma one_div_nat_number_of [simp]:
```
```   190      "(Suc 0)  div  number_of v' = (nat (1 div number_of v'))"
```
```   191 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric])
```
```   192
```
```   193
```
```   194 subsubsection{*Remainder *}
```
```   195
```
```   196 lemma mod_nat_number_of [simp]:
```
```   197      "(number_of v :: nat)  mod  number_of v' =
```
```   198         (if neg (number_of v :: int) then 0
```
```   199          else if neg (number_of v' :: int) then number_of v
```
```   200          else nat (number_of v mod number_of v'))"
```
```   201 by (force dest!: neg_nat
```
```   202           simp del: nat_number_of
```
```   203           simp add: nat_number_of_def nat_mod_distrib [symmetric])
```
```   204
```
```   205 lemma one_mod_nat_number_of [simp]:
```
```   206      "(Suc 0)  mod  number_of v' =
```
```   207         (if neg (number_of v' :: int) then Suc 0
```
```   208          else nat (1 mod number_of v'))"
```
```   209 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric])
```
```   210
```
```   211
```
```   212 subsubsection{* Divisibility *}
```
```   213
```
```   214 lemmas dvd_eq_mod_eq_0_number_of =
```
```   215   dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard]
```
```   216
```
```   217 declare dvd_eq_mod_eq_0_number_of [simp]
```
```   218
```
```   219 ML
```
```   220 {*
```
```   221 val nat_number_of_def = thm"nat_number_of_def";
```
```   222
```
```   223 val nat_number_of = thm"nat_number_of";
```
```   224 val nat_numeral_0_eq_0 = thm"nat_numeral_0_eq_0";
```
```   225 val nat_numeral_1_eq_1 = thm"nat_numeral_1_eq_1";
```
```   226 val numeral_1_eq_Suc_0 = thm"numeral_1_eq_Suc_0";
```
```   227 val numeral_2_eq_2 = thm"numeral_2_eq_2";
```
```   228 val nat_div_distrib = thm"nat_div_distrib";
```
```   229 val nat_mod_distrib = thm"nat_mod_distrib";
```
```   230 val int_nat_number_of = thm"int_nat_number_of";
```
```   231 val Suc_nat_eq_nat_zadd1 = thm"Suc_nat_eq_nat_zadd1";
```
```   232 val Suc_nat_number_of_add = thm"Suc_nat_number_of_add";
```
```   233 val Suc_nat_number_of = thm"Suc_nat_number_of";
```
```   234 val add_nat_number_of = thm"add_nat_number_of";
```
```   235 val diff_nat_eq_if = thm"diff_nat_eq_if";
```
```   236 val diff_nat_number_of = thm"diff_nat_number_of";
```
```   237 val mult_nat_number_of = thm"mult_nat_number_of";
```
```   238 val div_nat_number_of = thm"div_nat_number_of";
```
```   239 val mod_nat_number_of = thm"mod_nat_number_of";
```
```   240 *}
```
```   241
```
```   242
```
```   243 subsection{*Comparisons*}
```
```   244
```
```   245 subsubsection{*Equals (=) *}
```
```   246
```
```   247 lemma eq_nat_nat_iff:
```
```   248      "[| (0::int) <= z;  0 <= z' |] ==> (nat z = nat z') = (z=z')"
```
```   249 by (auto elim!: nonneg_eq_int)
```
```   250
```
```   251 (*"neg" is used in rewrite rules for binary comparisons*)
```
```   252 lemma eq_nat_number_of [simp]:
```
```   253      "((number_of v :: nat) = number_of v') =
```
```   254       (if neg (number_of v :: int) then (iszero (number_of v' :: int) | neg (number_of v' :: int))
```
```   255        else if neg (number_of v' :: int) then iszero (number_of v :: int)
```
```   256        else iszero (number_of (v + uminus v') :: int))"
```
```   257 apply (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def
```
```   258                   eq_nat_nat_iff eq_number_of_eq nat_0 iszero_def
```
```   259             split add: split_if cong add: imp_cong)
```
```   260 apply (simp only: nat_eq_iff nat_eq_iff2)
```
```   261 apply (simp add: not_neg_eq_ge_0 [symmetric])
```
```   262 done
```
```   263
```
```   264
```
```   265 subsubsection{*Less-than (<) *}
```
```   266
```
```   267 (*"neg" is used in rewrite rules for binary comparisons*)
```
```   268 lemma less_nat_number_of [simp]:
```
```   269      "((number_of v :: nat) < number_of v') =
```
```   270          (if neg (number_of v :: int) then neg (number_of (uminus v') :: int)
```
```   271           else neg (number_of (v + uminus v') :: int))"
```
```   272 by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def
```
```   273                 nat_less_eq_zless less_number_of_eq_neg zless_nat_eq_int_zless
```
```   274          cong add: imp_cong, simp add: Pls_def)
```
```   275
```
```   276
```
```   277 (*Maps #n to n for n = 0, 1, 2*)
```
```   278 lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
```
```   279
```
```   280
```
```   281 subsection{*Powers with Numeric Exponents*}
```
```   282
```
```   283 text{*We cannot refer to the number @{term 2} in @{text Ring_and_Field.thy}.
```
```   284 We cannot prove general results about the numeral @{term "-1"}, so we have to
```
```   285 use @{term "- 1"} instead.*}
```
```   286
```
```   287 lemma power2_eq_square: "(a::'a::recpower)\<twosuperior> = a * a"
```
```   288   by (simp add: numeral_2_eq_2 Power.power_Suc)
```
```   289
```
```   290 lemma zero_power2 [simp]: "(0::'a::{semiring_1,recpower})\<twosuperior> = 0"
```
```   291   by (simp add: power2_eq_square)
```
```   292
```
```   293 lemma one_power2 [simp]: "(1::'a::{semiring_1,recpower})\<twosuperior> = 1"
```
```   294   by (simp add: power2_eq_square)
```
```   295
```
```   296 lemma power3_eq_cube: "(x::'a::recpower) ^ 3 = x * x * x"
```
```   297   apply (subgoal_tac "3 = Suc (Suc (Suc 0))")
```
```   298   apply (erule ssubst)
```
```   299   apply (simp add: power_Suc mult_ac)
```
```   300   apply (unfold nat_number_of_def)
```
```   301   apply (subst nat_eq_iff)
```
```   302   apply simp
```
```   303 done
```
```   304
```
```   305 text{*Squares of literal numerals will be evaluated.*}
```
```   306 lemmas power2_eq_square_number_of =
```
```   307     power2_eq_square [of "number_of w", standard]
```
```   308 declare power2_eq_square_number_of [simp]
```
```   309
```
```   310
```
```   311 lemma zero_le_power2[simp]: "0 \<le> (a\<twosuperior>::'a::{ordered_idom,recpower})"
```
```   312   by (simp add: power2_eq_square)
```
```   313
```
```   314 lemma zero_less_power2[simp]:
```
```   315      "(0 < a\<twosuperior>) = (a \<noteq> (0::'a::{ordered_idom,recpower}))"
```
```   316   by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
```
```   317
```
```   318 lemma power2_less_0[simp]:
```
```   319   fixes a :: "'a::{ordered_idom,recpower}"
```
```   320   shows "~ (a\<twosuperior> < 0)"
```
```   321 by (force simp add: power2_eq_square mult_less_0_iff)
```
```   322
```
```   323 lemma zero_eq_power2[simp]:
```
```   324      "(a\<twosuperior> = 0) = (a = (0::'a::{ordered_idom,recpower}))"
```
```   325   by (force simp add: power2_eq_square mult_eq_0_iff)
```
```   326
```
```   327 lemma abs_power2[simp]:
```
```   328      "abs(a\<twosuperior>) = (a\<twosuperior>::'a::{ordered_idom,recpower})"
```
```   329   by (simp add: power2_eq_square abs_mult abs_mult_self)
```
```   330
```
```   331 lemma power2_abs[simp]:
```
```   332      "(abs a)\<twosuperior> = (a\<twosuperior>::'a::{ordered_idom,recpower})"
```
```   333   by (simp add: power2_eq_square abs_mult_self)
```
```   334
```
```   335 lemma power2_minus[simp]:
```
```   336      "(- a)\<twosuperior> = (a\<twosuperior>::'a::{comm_ring_1,recpower})"
```
```   337   by (simp add: power2_eq_square)
```
```   338
```
```   339 lemma power2_le_imp_le:
```
```   340   fixes x y :: "'a::{ordered_semidom,recpower}"
```
```   341   shows "\<lbrakk>x\<twosuperior> \<le> y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x \<le> y"
```
```   342 unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
```
```   343
```
```   344 lemma power2_less_imp_less:
```
```   345   fixes x y :: "'a::{ordered_semidom,recpower}"
```
```   346   shows "\<lbrakk>x\<twosuperior> < y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x < y"
```
```   347 by (rule power_less_imp_less_base)
```
```   348
```
```   349 lemma power2_eq_imp_eq:
```
```   350   fixes x y :: "'a::{ordered_semidom,recpower}"
```
```   351   shows "\<lbrakk>x\<twosuperior> = y\<twosuperior>; 0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> x = y"
```
```   352 unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base, simp)
```
```   353
```
```   354 lemma power_minus1_even[simp]: "(- 1) ^ (2*n) = (1::'a::{comm_ring_1,recpower})"
```
```   355 apply (induct "n")
```
```   356 apply (auto simp add: power_Suc power_add)
```
```   357 done
```
```   358
```
```   359 lemma power_even_eq: "(a::'a::recpower) ^ (2*n) = (a^n)^2"
```
```   360 by (subst mult_commute) (simp add: power_mult)
```
```   361
```
```   362 lemma power_odd_eq: "(a::int) ^ Suc(2*n) = a * (a^n)^2"
```
```   363 by (simp add: power_even_eq)
```
```   364
```
```   365 lemma power_minus_even [simp]:
```
```   366      "(-a) ^ (2*n) = (a::'a::{comm_ring_1,recpower}) ^ (2*n)"
```
```   367 by (simp add: power_minus1_even power_minus [of a])
```
```   368
```
```   369 lemma zero_le_even_power'[simp]:
```
```   370      "0 \<le> (a::'a::{ordered_idom,recpower}) ^ (2*n)"
```
```   371 proof (induct "n")
```
```   372   case 0
```
```   373     show ?case by (simp add: zero_le_one)
```
```   374 next
```
```   375   case (Suc n)
```
```   376     have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)"
```
```   377       by (simp add: mult_ac power_add power2_eq_square)
```
```   378     thus ?case
```
```   379       by (simp add: prems zero_le_mult_iff)
```
```   380 qed
```
```   381
```
```   382 lemma odd_power_less_zero:
```
```   383      "(a::'a::{ordered_idom,recpower}) < 0 ==> a ^ Suc(2*n) < 0"
```
```   384 proof (induct "n")
```
```   385   case 0
```
```   386   then show ?case by (simp add: Power.power_Suc)
```
```   387 next
```
```   388   case (Suc n)
```
```   389   have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)"
```
```   390     by (simp add: mult_ac power_add power2_eq_square Power.power_Suc)
```
```   391   thus ?case
```
```   392     by (simp add: prems mult_less_0_iff mult_neg_neg)
```
```   393 qed
```
```   394
```
```   395 lemma odd_0_le_power_imp_0_le:
```
```   396      "0 \<le> a  ^ Suc(2*n) ==> 0 \<le> (a::'a::{ordered_idom,recpower})"
```
```   397 apply (insert odd_power_less_zero [of a n])
```
```   398 apply (force simp add: linorder_not_less [symmetric])
```
```   399 done
```
```   400
```
```   401 text{*Simprules for comparisons where common factors can be cancelled.*}
```
```   402 lemmas zero_compare_simps =
```
```   403     add_strict_increasing add_strict_increasing2 add_increasing
```
```   404     zero_le_mult_iff zero_le_divide_iff
```
```   405     zero_less_mult_iff zero_less_divide_iff
```
```   406     mult_le_0_iff divide_le_0_iff
```
```   407     mult_less_0_iff divide_less_0_iff
```
```   408     zero_le_power2 power2_less_0
```
```   409
```
```   410 subsubsection{*Nat *}
```
```   411
```
```   412 lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
```
```   413 by (simp add: numerals)
```
```   414
```
```   415 (*Expresses a natural number constant as the Suc of another one.
```
```   416   NOT suitable for rewriting because n recurs in the condition.*)
```
```   417 lemmas expand_Suc = Suc_pred' [of "number_of v", standard]
```
```   418
```
```   419 subsubsection{*Arith *}
```
```   420
```
```   421 lemma Suc_eq_add_numeral_1: "Suc n = n + 1"
```
```   422 by (simp add: numerals)
```
```   423
```
```   424 lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n"
```
```   425 by (simp add: numerals)
```
```   426
```
```   427 (* These two can be useful when m = number_of... *)
```
```   428
```
```   429 lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
```
```   430 apply (case_tac "m")
```
```   431 apply (simp_all add: numerals)
```
```   432 done
```
```   433
```
```   434 lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
```
```   435 apply (case_tac "m")
```
```   436 apply (simp_all add: numerals)
```
```   437 done
```
```   438
```
```   439 lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
```
```   440 apply (case_tac "m")
```
```   441 apply (simp_all add: numerals)
```
```   442 done
```
```   443
```
```   444
```
```   445 subsection{*Comparisons involving (0::nat) *}
```
```   446
```
```   447 text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
```
```   448
```
```   449 lemma eq_number_of_0 [simp]:
```
```   450      "(number_of v = (0::nat)) =
```
```   451       (if neg (number_of v :: int) then True else iszero (number_of v :: int))"
```
```   452 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0)
```
```   453
```
```   454 lemma eq_0_number_of [simp]:
```
```   455      "((0::nat) = number_of v) =
```
```   456       (if neg (number_of v :: int) then True else iszero (number_of v :: int))"
```
```   457 by (rule trans [OF eq_sym_conv eq_number_of_0])
```
```   458
```
```   459 lemma less_0_number_of [simp]:
```
```   460      "((0::nat) < number_of v) = neg (number_of (uminus v) :: int)"
```
```   461 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] Pls_def)
```
```   462
```
```   463
```
```   464 lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
```
```   465 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0)
```
```   466
```
```   467
```
```   468
```
```   469 subsection{*Comparisons involving  @{term Suc} *}
```
```   470
```
```   471 lemma eq_number_of_Suc [simp]:
```
```   472      "(number_of v = Suc n) =
```
```   473         (let pv = number_of (Int.pred v) in
```
```   474          if neg pv then False else nat pv = n)"
```
```   475 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less
```
```   476                   number_of_pred nat_number_of_def
```
```   477             split add: split_if)
```
```   478 apply (rule_tac x = "number_of v" in spec)
```
```   479 apply (auto simp add: nat_eq_iff)
```
```   480 done
```
```   481
```
```   482 lemma Suc_eq_number_of [simp]:
```
```   483      "(Suc n = number_of v) =
```
```   484         (let pv = number_of (Int.pred v) in
```
```   485          if neg pv then False else nat pv = n)"
```
```   486 by (rule trans [OF eq_sym_conv eq_number_of_Suc])
```
```   487
```
```   488 lemma less_number_of_Suc [simp]:
```
```   489      "(number_of v < Suc n) =
```
```   490         (let pv = number_of (Int.pred v) in
```
```   491          if neg pv then True else nat pv < n)"
```
```   492 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less
```
```   493                   number_of_pred nat_number_of_def
```
```   494             split add: split_if)
```
```   495 apply (rule_tac x = "number_of v" in spec)
```
```   496 apply (auto simp add: nat_less_iff)
```
```   497 done
```
```   498
```
```   499 lemma less_Suc_number_of [simp]:
```
```   500      "(Suc n < number_of v) =
```
```   501         (let pv = number_of (Int.pred v) in
```
```   502          if neg pv then False else n < nat pv)"
```
```   503 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less
```
```   504                   number_of_pred nat_number_of_def
```
```   505             split add: split_if)
```
```   506 apply (rule_tac x = "number_of v" in spec)
```
```   507 apply (auto simp add: zless_nat_eq_int_zless)
```
```   508 done
```
```   509
```
```   510 lemma le_number_of_Suc [simp]:
```
```   511      "(number_of v <= Suc n) =
```
```   512         (let pv = number_of (Int.pred v) in
```
```   513          if neg pv then True else nat pv <= n)"
```
```   514 by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric])
```
```   515
```
```   516 lemma le_Suc_number_of [simp]:
```
```   517      "(Suc n <= number_of v) =
```
```   518         (let pv = number_of (Int.pred v) in
```
```   519          if neg pv then False else n <= nat pv)"
```
```   520 by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric])
```
```   521
```
```   522
```
```   523 lemma lemma1: "(m+m = n+n) = (m = (n::int))"
```
```   524 by auto
```
```   525
```
```   526 lemma lemma2: "m+m ~= (1::int) + (n + n)"
```
```   527 apply auto
```
```   528 apply (drule_tac f = "%x. x mod 2" in arg_cong)
```
```   529 apply (simp add: zmod_zadd1_eq)
```
```   530 done
```
```   531
```
```   532 lemma eq_number_of_BIT_BIT:
```
```   533      "((number_of (v BIT x) ::int) = number_of (w BIT y)) =
```
```   534       (x=y & (((number_of v) ::int) = number_of w))"
```
```   535 apply (simp only: number_of_BIT lemma1 lemma2 eq_commute
```
```   536                OrderedGroup.add_left_cancel add_assoc OrderedGroup.add_0_left
```
```   537             split add: bit.split)
```
```   538 apply simp
```
```   539 done
```
```   540
```
```   541 lemma eq_number_of_BIT_Pls:
```
```   542      "((number_of (v BIT x) ::int) = Numeral0) =
```
```   543       (x=bit.B0 & (((number_of v) ::int) = Numeral0))"
```
```   544 apply (simp only: simp_thms  add: number_of_BIT number_of_Pls eq_commute
```
```   545             split add: bit.split cong: imp_cong)
```
```   546 apply (rule_tac x = "number_of v" in spec, safe)
```
```   547 apply (simp_all (no_asm_use))
```
```   548 apply (drule_tac f = "%x. x mod 2" in arg_cong)
```
```   549 apply (simp add: zmod_zadd1_eq)
```
```   550 done
```
```   551
```
```   552 lemma eq_number_of_BIT_Min:
```
```   553      "((number_of (v BIT x) ::int) = number_of Int.Min) =
```
```   554       (x=bit.B1 & (((number_of v) ::int) = number_of Int.Min))"
```
```   555 apply (simp only: simp_thms  add: number_of_BIT number_of_Min eq_commute
```
```   556             split add: bit.split cong: imp_cong)
```
```   557 apply (rule_tac x = "number_of v" in spec, auto)
```
```   558 apply (drule_tac f = "%x. x mod 2" in arg_cong, auto)
```
```   559 done
```
```   560
```
```   561 lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Int.Min"
```
```   562 by auto
```
```   563
```
```   564
```
```   565
```
```   566 subsection{*Max and Min Combined with @{term Suc} *}
```
```   567
```
```   568 lemma max_number_of_Suc [simp]:
```
```   569      "max (Suc n) (number_of v) =
```
```   570         (let pv = number_of (Int.pred v) in
```
```   571          if neg pv then Suc n else Suc(max n (nat pv)))"
```
```   572 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def
```
```   573             split add: split_if nat.split)
```
```   574 apply (rule_tac x = "number_of v" in spec)
```
```   575 apply auto
```
```   576 done
```
```   577
```
```   578 lemma max_Suc_number_of [simp]:
```
```   579      "max (number_of v) (Suc n) =
```
```   580         (let pv = number_of (Int.pred v) in
```
```   581          if neg pv then Suc n else Suc(max (nat pv) n))"
```
```   582 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def
```
```   583             split add: split_if nat.split)
```
```   584 apply (rule_tac x = "number_of v" in spec)
```
```   585 apply auto
```
```   586 done
```
```   587
```
```   588 lemma min_number_of_Suc [simp]:
```
```   589      "min (Suc n) (number_of v) =
```
```   590         (let pv = number_of (Int.pred v) in
```
```   591          if neg pv then 0 else Suc(min n (nat pv)))"
```
```   592 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def
```
```   593             split add: split_if nat.split)
```
```   594 apply (rule_tac x = "number_of v" in spec)
```
```   595 apply auto
```
```   596 done
```
```   597
```
```   598 lemma min_Suc_number_of [simp]:
```
```   599      "min (number_of v) (Suc n) =
```
```   600         (let pv = number_of (Int.pred v) in
```
```   601          if neg pv then 0 else Suc(min (nat pv) n))"
```
```   602 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def
```
```   603             split add: split_if nat.split)
```
```   604 apply (rule_tac x = "number_of v" in spec)
```
```   605 apply auto
```
```   606 done
```
```   607
```
```   608 subsection{*Literal arithmetic involving powers*}
```
```   609
```
```   610 lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n"
```
```   611 apply (induct "n")
```
```   612 apply (simp_all (no_asm_simp) add: nat_mult_distrib)
```
```   613 done
```
```   614
```
```   615 lemma power_nat_number_of:
```
```   616      "(number_of v :: nat) ^ n =
```
```   617        (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
```
```   618 by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
```
```   619          split add: split_if cong: imp_cong)
```
```   620
```
```   621
```
```   622 lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard]
```
```   623 declare power_nat_number_of_number_of [simp]
```
```   624
```
```   625
```
```   626
```
```   627 text{*For arbitrary rings*}
```
```   628
```
```   629 lemma power_number_of_even:
```
```   630   fixes z :: "'a::{number_ring,recpower}"
```
```   631   shows "z ^ number_of (w BIT bit.B0) = (let w = z ^ (number_of w) in w * w)"
```
```   632 unfolding Let_def nat_number_of_def number_of_BIT bit.cases
```
```   633 apply (rule_tac x = "number_of w" in spec, clarify)
```
```   634 apply (case_tac " (0::int) <= x")
```
```   635 apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square)
```
```   636 done
```
```   637
```
```   638 lemma power_number_of_odd:
```
```   639   fixes z :: "'a::{number_ring,recpower}"
```
```   640   shows "z ^ number_of (w BIT bit.B1) = (if (0::int) <= number_of w
```
```   641      then (let w = z ^ (number_of w) in z * w * w) else 1)"
```
```   642 unfolding Let_def nat_number_of_def number_of_BIT bit.cases
```
```   643 apply (rule_tac x = "number_of w" in spec, auto)
```
```   644 apply (simp only: nat_add_distrib nat_mult_distrib)
```
```   645 apply simp
```
```   646 apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat power_Suc)
```
```   647 done
```
```   648
```
```   649 lemmas zpower_number_of_even = power_number_of_even [where 'a=int]
```
```   650 lemmas zpower_number_of_odd = power_number_of_odd [where 'a=int]
```
```   651
```
```   652 lemmas power_number_of_even_number_of [simp] =
```
```   653     power_number_of_even [of "number_of v", standard]
```
```   654
```
```   655 lemmas power_number_of_odd_number_of [simp] =
```
```   656     power_number_of_odd [of "number_of v", standard]
```
```   657
```
```   658
```
```   659
```
```   660 ML
```
```   661 {*
```
```   662 val numeral_ss = simpset() addsimps @{thms numerals};
```
```   663
```
```   664 val nat_bin_arith_setup =
```
```   665  LinArith.map_data
```
```   666    (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
```
```   667      {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms,
```
```   668       inj_thms = inj_thms,
```
```   669       lessD = lessD, neqE = neqE,
```
```   670       simpset = simpset addsimps [Suc_nat_number_of, int_nat_number_of,
```
```   671         @{thm not_neg_number_of_Pls}, @{thm neg_number_of_Min},
```
```   672         @{thm neg_number_of_BIT}]})
```
```   673 *}
```
```   674
```
```   675 declaration {* K nat_bin_arith_setup *}
```
```   676
```
```   677 (* Enable arith to deal with div/mod k where k is a numeral: *)
```
```   678 declare split_div[of _ _ "number_of k", standard, arith_split]
```
```   679 declare split_mod[of _ _ "number_of k", standard, arith_split]
```
```   680
```
```   681 lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
```
```   682   by (simp add: number_of_Pls nat_number_of_def)
```
```   683
```
```   684 lemma nat_number_of_Min: "number_of Int.Min = (0::nat)"
```
```   685   apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
```
```   686   done
```
```   687
```
```   688 lemma nat_number_of_BIT_1:
```
```   689   "number_of (w BIT bit.B1) =
```
```   690     (if neg (number_of w :: int) then 0
```
```   691      else let n = number_of w in Suc (n + n))"
```
```   692   apply (simp only: nat_number_of_def Let_def split: split_if)
```
```   693   apply (intro conjI impI)
```
```   694    apply (simp add: neg_nat neg_number_of_BIT)
```
```   695   apply (rule int_int_eq [THEN iffD1])
```
```   696   apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms)
```
```   697   apply (simp only: number_of_BIT zadd_assoc split: bit.split)
```
```   698   apply simp
```
```   699   done
```
```   700
```
```   701 lemma nat_number_of_BIT_0:
```
```   702     "number_of (w BIT bit.B0) = (let n::nat = number_of w in n + n)"
```
```   703   apply (simp only: nat_number_of_def Let_def)
```
```   704   apply (cases "neg (number_of w :: int)")
```
```   705    apply (simp add: neg_nat neg_number_of_BIT)
```
```   706   apply (rule int_int_eq [THEN iffD1])
```
```   707   apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms)
```
```   708   apply (simp only: number_of_BIT zadd_assoc)
```
```   709   apply simp
```
```   710   done
```
```   711
```
```   712 lemmas nat_number =
```
```   713   nat_number_of_Pls nat_number_of_Min
```
```   714   nat_number_of_BIT_1 nat_number_of_BIT_0
```
```   715
```
```   716 lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
```
```   717   by (simp add: Let_def)
```
```   718
```
```   719 lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring,recpower})"
```
```   720 by (simp add: power_mult power_Suc);
```
```   721
```
```   722 lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring,recpower})"
```
```   723 by (simp add: power_mult power_Suc);
```
```   724
```
```   725
```
```   726 subsection{*Literal arithmetic and @{term of_nat}*}
```
```   727
```
```   728 lemma of_nat_double:
```
```   729      "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
```
```   730 by (simp only: mult_2 nat_add_distrib of_nat_add)
```
```   731
```
```   732 lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
```
```   733 by (simp only: nat_number_of_def)
```
```   734
```
```   735 lemma of_nat_number_of_lemma:
```
```   736      "of_nat (number_of v :: nat) =
```
```   737          (if 0 \<le> (number_of v :: int)
```
```   738           then (number_of v :: 'a :: number_ring)
```
```   739           else 0)"
```
```   740 by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat);
```
```   741
```
```   742 lemma of_nat_number_of_eq [simp]:
```
```   743      "of_nat (number_of v :: nat) =
```
```   744          (if neg (number_of v :: int) then 0
```
```   745           else (number_of v :: 'a :: number_ring))"
```
```   746 by (simp only: of_nat_number_of_lemma neg_def, simp)
```
```   747
```
```   748
```
```   749 subsection {*Lemmas for the Combination and Cancellation Simprocs*}
```
```   750
```
```   751 lemma nat_number_of_add_left:
```
```   752      "number_of v + (number_of v' + (k::nat)) =
```
```   753          (if neg (number_of v :: int) then number_of v' + k
```
```   754           else if neg (number_of v' :: int) then number_of v + k
```
```   755           else number_of (v + v') + k)"
```
```   756 by simp
```
```   757
```
```   758 lemma nat_number_of_mult_left:
```
```   759      "number_of v * (number_of v' * (k::nat)) =
```
```   760          (if neg (number_of v :: int) then 0
```
```   761           else number_of (v * v') * k)"
```
```   762 by simp
```
```   763
```
```   764
```
```   765 subsubsection{*For @{text combine_numerals}*}
```
```   766
```
```   767 lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
```
```   768 by (simp add: add_mult_distrib)
```
```   769
```
```   770
```
```   771 subsubsection{*For @{text cancel_numerals}*}
```
```   772
```
```   773 lemma nat_diff_add_eq1:
```
```   774      "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
```
```   775 by (simp split add: nat_diff_split add: add_mult_distrib)
```
```   776
```
```   777 lemma nat_diff_add_eq2:
```
```   778      "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
```
```   779 by (simp split add: nat_diff_split add: add_mult_distrib)
```
```   780
```
```   781 lemma nat_eq_add_iff1:
```
```   782      "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
```
```   783 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   784
```
```   785 lemma nat_eq_add_iff2:
```
```   786      "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
```
```   787 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   788
```
```   789 lemma nat_less_add_iff1:
```
```   790      "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
```
```   791 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   792
```
```   793 lemma nat_less_add_iff2:
```
```   794      "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
```
```   795 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   796
```
```   797 lemma nat_le_add_iff1:
```
```   798      "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
```
```   799 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   800
```
```   801 lemma nat_le_add_iff2:
```
```   802      "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
```
```   803 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   804
```
```   805
```
```   806 subsubsection{*For @{text cancel_numeral_factors} *}
```
```   807
```
```   808 lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
```
```   809 by auto
```
```   810
```
```   811 lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
```
```   812 by auto
```
```   813
```
```   814 lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
```
```   815 by auto
```
```   816
```
```   817 lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
```
```   818 by auto
```
```   819
```
```   820 lemma nat_mult_dvd_cancel_disj[simp]:
```
```   821   "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
```
```   822 by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
```
```   823
```
```   824 lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
```
```   825 by(auto)
```
```   826
```
```   827
```
```   828 subsubsection{*For @{text cancel_factor} *}
```
```   829
```
```   830 lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
```
```   831 by auto
```
```   832
```
```   833 lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
```
```   834 by auto
```
```   835
```
```   836 lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
```
```   837 by auto
```
```   838
```
```   839 lemma nat_mult_div_cancel_disj[simp]:
```
```   840      "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
```
```   841 by (simp add: nat_mult_div_cancel1)
```
```   842
```
```   843 end
```