src/HOL/Nat_Numeral.thy
 author haftmann Tue Apr 28 15:50:29 2009 +0200 (2009-04-28) changeset 31014 79f0858d9d49 parent 31002 bc4117fe72ab child 31034 736f521ad036 permissions -rw-r--r--
collected square lemmas in Nat_Numeral
```     1 (*  Title:      HOL/Nat_Numeral.thy
```
```     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
```
```     3     Copyright   1999  University of Cambridge
```
```     4 *)
```
```     5
```
```     6 header {* Binary numerals for the natural numbers *}
```
```     7
```
```     8 theory Nat_Numeral
```
```     9 imports IntDiv
```
```    10 uses ("Tools/nat_simprocs.ML")
```
```    11 begin
```
```    12
```
```    13 subsection {* Numerals for natural numbers *}
```
```    14
```
```    15 text {*
```
```    16   Arithmetic for naturals is reduced to that for the non-negative integers.
```
```    17 *}
```
```    18
```
```    19 instantiation nat :: number
```
```    20 begin
```
```    21
```
```    22 definition
```
```    23   nat_number_of_def [code inline, code del]: "number_of v = nat (number_of v)"
```
```    24
```
```    25 instance ..
```
```    26
```
```    27 end
```
```    28
```
```    29 lemma [code post]:
```
```    30   "nat (number_of v) = number_of v"
```
```    31   unfolding nat_number_of_def ..
```
```    32
```
```    33
```
```    34 subsection {* Special case: squares and cubes *}
```
```    35
```
```    36 lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
```
```    37   by (simp add: nat_number_of_def)
```
```    38
```
```    39 lemma numeral_3_eq_3: "3 = Suc (Suc (Suc 0))"
```
```    40   by (simp add: nat_number_of_def)
```
```    41
```
```    42 context power
```
```    43 begin
```
```    44
```
```    45 abbreviation (xsymbols)
```
```    46   power2 :: "'a \<Rightarrow> 'a"  ("(_\<twosuperior>)" [1000] 999) where
```
```    47   "x\<twosuperior> \<equiv> x ^ 2"
```
```    48
```
```    49 notation (latex output)
```
```    50   power2  ("(_\<twosuperior>)" [1000] 999)
```
```    51
```
```    52 notation (HTML output)
```
```    53   power2  ("(_\<twosuperior>)" [1000] 999)
```
```    54
```
```    55 end
```
```    56
```
```    57 context monoid_mult
```
```    58 begin
```
```    59
```
```    60 lemma power2_eq_square: "a\<twosuperior> = a * a"
```
```    61   by (simp add: numeral_2_eq_2)
```
```    62
```
```    63 lemma power3_eq_cube: "a ^ 3 = a * a * a"
```
```    64   by (simp add: numeral_3_eq_3 mult_assoc)
```
```    65
```
```    66 lemma power_even_eq:
```
```    67   "a ^ (2*n) = (a ^ n) ^ 2"
```
```    68   by (subst OrderedGroup.mult_commute) (simp add: power_mult)
```
```    69
```
```    70 lemma power_odd_eq:
```
```    71   "a ^ Suc (2*n) = a * (a ^ n) ^ 2"
```
```    72   by (simp add: power_even_eq)
```
```    73
```
```    74 end
```
```    75
```
```    76 context semiring_1
```
```    77 begin
```
```    78
```
```    79 lemma zero_power2 [simp]: "0\<twosuperior> = 0"
```
```    80   by (simp add: power2_eq_square)
```
```    81
```
```    82 lemma one_power2 [simp]: "1\<twosuperior> = 1"
```
```    83   by (simp add: power2_eq_square)
```
```    84
```
```    85 end
```
```    86
```
```    87 context comm_ring_1
```
```    88 begin
```
```    89
```
```    90 lemma power2_minus [simp]:
```
```    91   "(- a)\<twosuperior> = a\<twosuperior>"
```
```    92   by (simp add: power2_eq_square)
```
```    93
```
```    94 text{*
```
```    95   We cannot prove general results about the numeral @{term "-1"},
```
```    96   so we have to use @{term "- 1"} instead.
```
```    97 *}
```
```    98
```
```    99 lemma power_minus1_even [simp]:
```
```   100   "(- 1) ^ (2*n) = 1"
```
```   101 proof (induct n)
```
```   102   case 0 show ?case by simp
```
```   103 next
```
```   104   case (Suc n) then show ?case by (simp add: power_add)
```
```   105 qed
```
```   106
```
```   107 lemma power_minus1_odd:
```
```   108   "(- 1) ^ Suc (2*n) = - 1"
```
```   109   by simp
```
```   110
```
```   111 lemma power_minus_even [simp]:
```
```   112   "(-a) ^ (2*n) = a ^ (2*n)"
```
```   113   by (simp add: power_minus [of a])
```
```   114
```
```   115 end
```
```   116
```
```   117 context ordered_ring_strict
```
```   118 begin
```
```   119
```
```   120 lemma sum_squares_ge_zero:
```
```   121   "0 \<le> x * x + y * y"
```
```   122   by (intro add_nonneg_nonneg zero_le_square)
```
```   123
```
```   124 lemma not_sum_squares_lt_zero:
```
```   125   "\<not> x * x + y * y < 0"
```
```   126   by (simp add: not_less sum_squares_ge_zero)
```
```   127
```
```   128 lemma sum_squares_eq_zero_iff:
```
```   129   "x * x + y * y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
```
```   130   by (simp add: sum_nonneg_eq_zero_iff)
```
```   131
```
```   132 lemma sum_squares_le_zero_iff:
```
```   133   "x * x + y * y \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
```
```   134   by (simp add: le_less not_sum_squares_lt_zero sum_squares_eq_zero_iff)
```
```   135
```
```   136 lemma sum_squares_gt_zero_iff:
```
```   137   "0 < x * x + y * y \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
```
```   138 proof -
```
```   139   have "x * x + y * y \<noteq> 0 \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
```
```   140     by (simp add: sum_squares_eq_zero_iff)
```
```   141   then have "0 \<noteq> x * x + y * y \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
```
```   142     by auto
```
```   143   then show ?thesis
```
```   144     by (simp add: less_le sum_squares_ge_zero)
```
```   145 qed
```
```   146
```
```   147 end
```
```   148
```
```   149 context ordered_semidom
```
```   150 begin
```
```   151
```
```   152 lemma power2_le_imp_le:
```
```   153   "x\<twosuperior> \<le> y\<twosuperior> \<Longrightarrow> 0 \<le> y \<Longrightarrow> x \<le> y"
```
```   154   unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
```
```   155
```
```   156 lemma power2_less_imp_less:
```
```   157   "x\<twosuperior> < y\<twosuperior> \<Longrightarrow> 0 \<le> y \<Longrightarrow> x < y"
```
```   158   by (rule power_less_imp_less_base)
```
```   159
```
```   160 lemma power2_eq_imp_eq:
```
```   161   "x\<twosuperior> = y\<twosuperior> \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> x = y"
```
```   162   unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base) simp
```
```   163
```
```   164 end
```
```   165
```
```   166 context ordered_idom
```
```   167 begin
```
```   168
```
```   169 lemma zero_eq_power2 [simp]:
```
```   170   "a\<twosuperior> = 0 \<longleftrightarrow> a = 0"
```
```   171   by (force simp add: power2_eq_square)
```
```   172
```
```   173 lemma zero_le_power2 [simp]:
```
```   174   "0 \<le> a\<twosuperior>"
```
```   175   by (simp add: power2_eq_square)
```
```   176
```
```   177 lemma zero_less_power2 [simp]:
```
```   178   "0 < a\<twosuperior> \<longleftrightarrow> a \<noteq> 0"
```
```   179   by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
```
```   180
```
```   181 lemma power2_less_0 [simp]:
```
```   182   "\<not> a\<twosuperior> < 0"
```
```   183   by (force simp add: power2_eq_square mult_less_0_iff)
```
```   184
```
```   185 lemma abs_power2 [simp]:
```
```   186   "abs (a\<twosuperior>) = a\<twosuperior>"
```
```   187   by (simp add: power2_eq_square abs_mult abs_mult_self)
```
```   188
```
```   189 lemma power2_abs [simp]:
```
```   190   "(abs a)\<twosuperior> = a\<twosuperior>"
```
```   191   by (simp add: power2_eq_square abs_mult_self)
```
```   192
```
```   193 lemma odd_power_less_zero:
```
```   194   "a < 0 \<Longrightarrow> a ^ Suc (2*n) < 0"
```
```   195 proof (induct n)
```
```   196   case 0
```
```   197   then show ?case by simp
```
```   198 next
```
```   199   case (Suc n)
```
```   200   have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)"
```
```   201     by (simp add: mult_ac power_add power2_eq_square)
```
```   202   thus ?case
```
```   203     by (simp del: power_Suc add: Suc mult_less_0_iff mult_neg_neg)
```
```   204 qed
```
```   205
```
```   206 lemma odd_0_le_power_imp_0_le:
```
```   207   "0 \<le> a ^ Suc (2*n) \<Longrightarrow> 0 \<le> a"
```
```   208   using odd_power_less_zero [of a n]
```
```   209     by (force simp add: linorder_not_less [symmetric])
```
```   210
```
```   211 lemma zero_le_even_power'[simp]:
```
```   212   "0 \<le> a ^ (2*n)"
```
```   213 proof (induct n)
```
```   214   case 0
```
```   215     show ?case by (simp add: zero_le_one)
```
```   216 next
```
```   217   case (Suc n)
```
```   218     have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)"
```
```   219       by (simp add: mult_ac power_add power2_eq_square)
```
```   220     thus ?case
```
```   221       by (simp add: Suc zero_le_mult_iff)
```
```   222 qed
```
```   223
```
```   224 lemma sum_power2_ge_zero:
```
```   225   "0 \<le> x\<twosuperior> + y\<twosuperior>"
```
```   226   unfolding power2_eq_square by (rule sum_squares_ge_zero)
```
```   227
```
```   228 lemma not_sum_power2_lt_zero:
```
```   229   "\<not> x\<twosuperior> + y\<twosuperior> < 0"
```
```   230   unfolding power2_eq_square by (rule not_sum_squares_lt_zero)
```
```   231
```
```   232 lemma sum_power2_eq_zero_iff:
```
```   233   "x\<twosuperior> + y\<twosuperior> = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
```
```   234   unfolding power2_eq_square by (rule sum_squares_eq_zero_iff)
```
```   235
```
```   236 lemma sum_power2_le_zero_iff:
```
```   237   "x\<twosuperior> + y\<twosuperior> \<le> 0 \<longleftrightarrow> x = 0 \<and> y = 0"
```
```   238   unfolding power2_eq_square by (rule sum_squares_le_zero_iff)
```
```   239
```
```   240 lemma sum_power2_gt_zero_iff:
```
```   241   "0 < x\<twosuperior> + y\<twosuperior> \<longleftrightarrow> x \<noteq> 0 \<or> y \<noteq> 0"
```
```   242   unfolding power2_eq_square by (rule sum_squares_gt_zero_iff)
```
```   243
```
```   244 end
```
```   245
```
```   246 lemma power2_sum:
```
```   247   fixes x y :: "'a::number_ring"
```
```   248   shows "(x + y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> + 2 * x * y"
```
```   249   by (simp add: ring_distribs power2_eq_square)
```
```   250
```
```   251 lemma power2_diff:
```
```   252   fixes x y :: "'a::number_ring"
```
```   253   shows "(x - y)\<twosuperior> = x\<twosuperior> + y\<twosuperior> - 2 * x * y"
```
```   254   by (simp add: ring_distribs power2_eq_square)
```
```   255
```
```   256
```
```   257 subsection {* Predicate for negative binary numbers *}
```
```   258
```
```   259 definition neg  :: "int \<Rightarrow> bool" where
```
```   260   "neg Z \<longleftrightarrow> Z < 0"
```
```   261
```
```   262 lemma not_neg_int [simp]: "~ neg (of_nat n)"
```
```   263 by (simp add: neg_def)
```
```   264
```
```   265 lemma neg_zminus_int [simp]: "neg (- (of_nat (Suc n)))"
```
```   266 by (simp add: neg_def neg_less_0_iff_less del: of_nat_Suc)
```
```   267
```
```   268 lemmas neg_eq_less_0 = neg_def
```
```   269
```
```   270 lemma not_neg_eq_ge_0: "(~neg x) = (0 \<le> x)"
```
```   271 by (simp add: neg_def linorder_not_less)
```
```   272
```
```   273 text{*To simplify inequalities when Numeral1 can get simplified to 1*}
```
```   274
```
```   275 lemma not_neg_0: "~ neg 0"
```
```   276 by (simp add: One_int_def neg_def)
```
```   277
```
```   278 lemma not_neg_1: "~ neg 1"
```
```   279 by (simp add: neg_def linorder_not_less zero_le_one)
```
```   280
```
```   281 lemma neg_nat: "neg z ==> nat z = 0"
```
```   282 by (simp add: neg_def order_less_imp_le)
```
```   283
```
```   284 lemma not_neg_nat: "~ neg z ==> of_nat (nat z) = z"
```
```   285 by (simp add: linorder_not_less neg_def)
```
```   286
```
```   287 text {*
```
```   288   If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
```
```   289   @{term Numeral0} IS @{term "number_of Pls"}
```
```   290 *}
```
```   291
```
```   292 lemma not_neg_number_of_Pls: "~ neg (number_of Int.Pls)"
```
```   293   by (simp add: neg_def)
```
```   294
```
```   295 lemma neg_number_of_Min: "neg (number_of Int.Min)"
```
```   296   by (simp add: neg_def)
```
```   297
```
```   298 lemma neg_number_of_Bit0:
```
```   299   "neg (number_of (Int.Bit0 w)) = neg (number_of w)"
```
```   300   by (simp add: neg_def)
```
```   301
```
```   302 lemma neg_number_of_Bit1:
```
```   303   "neg (number_of (Int.Bit1 w)) = neg (number_of w)"
```
```   304   by (simp add: neg_def)
```
```   305
```
```   306 lemmas neg_simps [simp] =
```
```   307   not_neg_0 not_neg_1
```
```   308   not_neg_number_of_Pls neg_number_of_Min
```
```   309   neg_number_of_Bit0 neg_number_of_Bit1
```
```   310
```
```   311
```
```   312 subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
```
```   313
```
```   314 declare nat_0 [simp] nat_1 [simp]
```
```   315
```
```   316 lemma nat_number_of [simp]: "nat (number_of w) = number_of w"
```
```   317 by (simp add: nat_number_of_def)
```
```   318
```
```   319 lemma nat_numeral_0_eq_0 [simp]: "Numeral0 = (0::nat)"
```
```   320 by (simp add: nat_number_of_def)
```
```   321
```
```   322 lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)"
```
```   323 by (simp add: nat_1 nat_number_of_def)
```
```   324
```
```   325 lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
```
```   326 by (simp add: nat_numeral_1_eq_1)
```
```   327
```
```   328
```
```   329 subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
```
```   330
```
```   331 lemma int_nat_number_of [simp]:
```
```   332      "int (number_of v) =
```
```   333          (if neg (number_of v :: int) then 0
```
```   334           else (number_of v :: int))"
```
```   335   unfolding nat_number_of_def number_of_is_id neg_def
```
```   336   by simp
```
```   337
```
```   338
```
```   339 subsubsection{*Successor *}
```
```   340
```
```   341 lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
```
```   342 apply (rule sym)
```
```   343 apply (simp add: nat_eq_iff int_Suc)
```
```   344 done
```
```   345
```
```   346 lemma Suc_nat_number_of_add:
```
```   347      "Suc (number_of v + n) =
```
```   348         (if neg (number_of v :: int) then 1+n else number_of (Int.succ v) + n)"
```
```   349   unfolding nat_number_of_def number_of_is_id neg_def numeral_simps
```
```   350   by (simp add: Suc_nat_eq_nat_zadd1 add_ac)
```
```   351
```
```   352 lemma Suc_nat_number_of [simp]:
```
```   353      "Suc (number_of v) =
```
```   354         (if neg (number_of v :: int) then 1 else number_of (Int.succ v))"
```
```   355 apply (cut_tac n = 0 in Suc_nat_number_of_add)
```
```   356 apply (simp cong del: if_weak_cong)
```
```   357 done
```
```   358
```
```   359
```
```   360 subsubsection{*Addition *}
```
```   361
```
```   362 lemma add_nat_number_of [simp]:
```
```   363      "(number_of v :: nat) + number_of v' =
```
```   364          (if v < Int.Pls then number_of v'
```
```   365           else if v' < Int.Pls then number_of v
```
```   366           else number_of (v + v'))"
```
```   367   unfolding nat_number_of_def number_of_is_id numeral_simps
```
```   368   by (simp add: nat_add_distrib)
```
```   369
```
```   370 lemma nat_number_of_add_1 [simp]:
```
```   371   "number_of v + (1::nat) =
```
```   372     (if v < Int.Pls then 1 else number_of (Int.succ v))"
```
```   373   unfolding nat_number_of_def number_of_is_id numeral_simps
```
```   374   by (simp add: nat_add_distrib)
```
```   375
```
```   376 lemma nat_1_add_number_of [simp]:
```
```   377   "(1::nat) + number_of v =
```
```   378     (if v < Int.Pls then 1 else number_of (Int.succ v))"
```
```   379   unfolding nat_number_of_def number_of_is_id numeral_simps
```
```   380   by (simp add: nat_add_distrib)
```
```   381
```
```   382 lemma nat_1_add_1 [simp]: "1 + 1 = (2::nat)"
```
```   383   by (rule int_int_eq [THEN iffD1]) simp
```
```   384
```
```   385
```
```   386 subsubsection{*Subtraction *}
```
```   387
```
```   388 lemma diff_nat_eq_if:
```
```   389      "nat z - nat z' =
```
```   390         (if neg z' then nat z
```
```   391          else let d = z-z' in
```
```   392               if neg d then 0 else nat d)"
```
```   393 by (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0)
```
```   394
```
```   395
```
```   396 lemma diff_nat_number_of [simp]:
```
```   397      "(number_of v :: nat) - number_of v' =
```
```   398         (if v' < Int.Pls then number_of v
```
```   399          else let d = number_of (v + uminus v') in
```
```   400               if neg d then 0 else nat d)"
```
```   401   unfolding nat_number_of_def number_of_is_id numeral_simps neg_def
```
```   402   by auto
```
```   403
```
```   404 lemma nat_number_of_diff_1 [simp]:
```
```   405   "number_of v - (1::nat) =
```
```   406     (if v \<le> Int.Pls then 0 else number_of (Int.pred v))"
```
```   407   unfolding nat_number_of_def number_of_is_id numeral_simps
```
```   408   by auto
```
```   409
```
```   410
```
```   411 subsubsection{*Multiplication *}
```
```   412
```
```   413 lemma mult_nat_number_of [simp]:
```
```   414      "(number_of v :: nat) * number_of v' =
```
```   415        (if v < Int.Pls then 0 else number_of (v * v'))"
```
```   416   unfolding nat_number_of_def number_of_is_id numeral_simps
```
```   417   by (simp add: nat_mult_distrib)
```
```   418
```
```   419
```
```   420 subsubsection{*Quotient *}
```
```   421
```
```   422 lemma div_nat_number_of [simp]:
```
```   423      "(number_of v :: nat)  div  number_of v' =
```
```   424           (if neg (number_of v :: int) then 0
```
```   425            else nat (number_of v div number_of v'))"
```
```   426   unfolding nat_number_of_def number_of_is_id neg_def
```
```   427   by (simp add: nat_div_distrib)
```
```   428
```
```   429 lemma one_div_nat_number_of [simp]:
```
```   430      "Suc 0 div number_of v' = nat (1 div number_of v')"
```
```   431 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric])
```
```   432
```
```   433
```
```   434 subsubsection{*Remainder *}
```
```   435
```
```   436 lemma mod_nat_number_of [simp]:
```
```   437      "(number_of v :: nat)  mod  number_of v' =
```
```   438         (if neg (number_of v :: int) then 0
```
```   439          else if neg (number_of v' :: int) then number_of v
```
```   440          else nat (number_of v mod number_of v'))"
```
```   441   unfolding nat_number_of_def number_of_is_id neg_def
```
```   442   by (simp add: nat_mod_distrib)
```
```   443
```
```   444 lemma one_mod_nat_number_of [simp]:
```
```   445      "Suc 0 mod number_of v' =
```
```   446         (if neg (number_of v' :: int) then Suc 0
```
```   447          else nat (1 mod number_of v'))"
```
```   448 by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric])
```
```   449
```
```   450
```
```   451 subsubsection{* Divisibility *}
```
```   452
```
```   453 lemmas dvd_eq_mod_eq_0_number_of =
```
```   454   dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard]
```
```   455
```
```   456 declare dvd_eq_mod_eq_0_number_of [simp]
```
```   457
```
```   458 ML
```
```   459 {*
```
```   460 val nat_number_of_def = thm"nat_number_of_def";
```
```   461
```
```   462 val nat_number_of = thm"nat_number_of";
```
```   463 val nat_numeral_0_eq_0 = thm"nat_numeral_0_eq_0";
```
```   464 val nat_numeral_1_eq_1 = thm"nat_numeral_1_eq_1";
```
```   465 val numeral_1_eq_Suc_0 = thm"numeral_1_eq_Suc_0";
```
```   466 val numeral_2_eq_2 = thm"numeral_2_eq_2";
```
```   467 val nat_div_distrib = thm"nat_div_distrib";
```
```   468 val nat_mod_distrib = thm"nat_mod_distrib";
```
```   469 val int_nat_number_of = thm"int_nat_number_of";
```
```   470 val Suc_nat_eq_nat_zadd1 = thm"Suc_nat_eq_nat_zadd1";
```
```   471 val Suc_nat_number_of_add = thm"Suc_nat_number_of_add";
```
```   472 val Suc_nat_number_of = thm"Suc_nat_number_of";
```
```   473 val add_nat_number_of = thm"add_nat_number_of";
```
```   474 val diff_nat_eq_if = thm"diff_nat_eq_if";
```
```   475 val diff_nat_number_of = thm"diff_nat_number_of";
```
```   476 val mult_nat_number_of = thm"mult_nat_number_of";
```
```   477 val div_nat_number_of = thm"div_nat_number_of";
```
```   478 val mod_nat_number_of = thm"mod_nat_number_of";
```
```   479 *}
```
```   480
```
```   481
```
```   482 subsection{*Comparisons*}
```
```   483
```
```   484 subsubsection{*Equals (=) *}
```
```   485
```
```   486 lemma eq_nat_nat_iff:
```
```   487      "[| (0::int) <= z;  0 <= z' |] ==> (nat z = nat z') = (z=z')"
```
```   488 by (auto elim!: nonneg_eq_int)
```
```   489
```
```   490 lemma eq_nat_number_of [simp]:
```
```   491      "((number_of v :: nat) = number_of v') =
```
```   492       (if neg (number_of v :: int) then (number_of v' :: int) \<le> 0
```
```   493        else if neg (number_of v' :: int) then (number_of v :: int) = 0
```
```   494        else v = v')"
```
```   495   unfolding nat_number_of_def number_of_is_id neg_def
```
```   496   by auto
```
```   497
```
```   498
```
```   499 subsubsection{*Less-than (<) *}
```
```   500
```
```   501 lemma less_nat_number_of [simp]:
```
```   502   "(number_of v :: nat) < number_of v' \<longleftrightarrow>
```
```   503     (if v < v' then Int.Pls < v' else False)"
```
```   504   unfolding nat_number_of_def number_of_is_id numeral_simps
```
```   505   by auto
```
```   506
```
```   507
```
```   508 subsubsection{*Less-than-or-equal *}
```
```   509
```
```   510 lemma le_nat_number_of [simp]:
```
```   511   "(number_of v :: nat) \<le> number_of v' \<longleftrightarrow>
```
```   512     (if v \<le> v' then True else v \<le> Int.Pls)"
```
```   513   unfolding nat_number_of_def number_of_is_id numeral_simps
```
```   514   by auto
```
```   515
```
```   516 (*Maps #n to n for n = 0, 1, 2*)
```
```   517 lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
```
```   518
```
```   519
```
```   520 subsection{*Powers with Numeric Exponents*}
```
```   521
```
```   522 text{*Squares of literal numerals will be evaluated.*}
```
```   523 lemmas power2_eq_square_number_of [simp] =
```
```   524     power2_eq_square [of "number_of w", standard]
```
```   525
```
```   526
```
```   527 text{*Simprules for comparisons where common factors can be cancelled.*}
```
```   528 lemmas zero_compare_simps =
```
```   529     add_strict_increasing add_strict_increasing2 add_increasing
```
```   530     zero_le_mult_iff zero_le_divide_iff
```
```   531     zero_less_mult_iff zero_less_divide_iff
```
```   532     mult_le_0_iff divide_le_0_iff
```
```   533     mult_less_0_iff divide_less_0_iff
```
```   534     zero_le_power2 power2_less_0
```
```   535
```
```   536 subsubsection{*Nat *}
```
```   537
```
```   538 lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
```
```   539 by (simp add: numerals)
```
```   540
```
```   541 (*Expresses a natural number constant as the Suc of another one.
```
```   542   NOT suitable for rewriting because n recurs in the condition.*)
```
```   543 lemmas expand_Suc = Suc_pred' [of "number_of v", standard]
```
```   544
```
```   545 subsubsection{*Arith *}
```
```   546
```
```   547 lemma Suc_eq_add_numeral_1: "Suc n = n + 1"
```
```   548 by (simp add: numerals)
```
```   549
```
```   550 lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n"
```
```   551 by (simp add: numerals)
```
```   552
```
```   553 (* These two can be useful when m = number_of... *)
```
```   554
```
```   555 lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
```
```   556   unfolding One_nat_def by (cases m) simp_all
```
```   557
```
```   558 lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
```
```   559   unfolding One_nat_def by (cases m) simp_all
```
```   560
```
```   561 lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
```
```   562   unfolding One_nat_def by (cases m) simp_all
```
```   563
```
```   564
```
```   565 subsection{*Comparisons involving (0::nat) *}
```
```   566
```
```   567 text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
```
```   568
```
```   569 lemma eq_number_of_0 [simp]:
```
```   570   "number_of v = (0::nat) \<longleftrightarrow> v \<le> Int.Pls"
```
```   571   unfolding nat_number_of_def number_of_is_id numeral_simps
```
```   572   by auto
```
```   573
```
```   574 lemma eq_0_number_of [simp]:
```
```   575   "(0::nat) = number_of v \<longleftrightarrow> v \<le> Int.Pls"
```
```   576 by (rule trans [OF eq_sym_conv eq_number_of_0])
```
```   577
```
```   578 lemma less_0_number_of [simp]:
```
```   579    "(0::nat) < number_of v \<longleftrightarrow> Int.Pls < v"
```
```   580   unfolding nat_number_of_def number_of_is_id numeral_simps
```
```   581   by simp
```
```   582
```
```   583 lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
```
```   584 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric])
```
```   585
```
```   586
```
```   587
```
```   588 subsection{*Comparisons involving  @{term Suc} *}
```
```   589
```
```   590 lemma eq_number_of_Suc [simp]:
```
```   591      "(number_of v = Suc n) =
```
```   592         (let pv = number_of (Int.pred v) in
```
```   593          if neg pv then False else nat pv = n)"
```
```   594 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less
```
```   595                   number_of_pred nat_number_of_def
```
```   596             split add: split_if)
```
```   597 apply (rule_tac x = "number_of v" in spec)
```
```   598 apply (auto simp add: nat_eq_iff)
```
```   599 done
```
```   600
```
```   601 lemma Suc_eq_number_of [simp]:
```
```   602      "(Suc n = number_of v) =
```
```   603         (let pv = number_of (Int.pred v) in
```
```   604          if neg pv then False else nat pv = n)"
```
```   605 by (rule trans [OF eq_sym_conv eq_number_of_Suc])
```
```   606
```
```   607 lemma less_number_of_Suc [simp]:
```
```   608      "(number_of v < Suc n) =
```
```   609         (let pv = number_of (Int.pred v) in
```
```   610          if neg pv then True else nat pv < n)"
```
```   611 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less
```
```   612                   number_of_pred nat_number_of_def
```
```   613             split add: split_if)
```
```   614 apply (rule_tac x = "number_of v" in spec)
```
```   615 apply (auto simp add: nat_less_iff)
```
```   616 done
```
```   617
```
```   618 lemma less_Suc_number_of [simp]:
```
```   619      "(Suc n < number_of v) =
```
```   620         (let pv = number_of (Int.pred v) in
```
```   621          if neg pv then False else n < nat pv)"
```
```   622 apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less
```
```   623                   number_of_pred nat_number_of_def
```
```   624             split add: split_if)
```
```   625 apply (rule_tac x = "number_of v" in spec)
```
```   626 apply (auto simp add: zless_nat_eq_int_zless)
```
```   627 done
```
```   628
```
```   629 lemma le_number_of_Suc [simp]:
```
```   630      "(number_of v <= Suc n) =
```
```   631         (let pv = number_of (Int.pred v) in
```
```   632          if neg pv then True else nat pv <= n)"
```
```   633 by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric])
```
```   634
```
```   635 lemma le_Suc_number_of [simp]:
```
```   636      "(Suc n <= number_of v) =
```
```   637         (let pv = number_of (Int.pred v) in
```
```   638          if neg pv then False else n <= nat pv)"
```
```   639 by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric])
```
```   640
```
```   641
```
```   642 lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Int.Min"
```
```   643 by auto
```
```   644
```
```   645
```
```   646
```
```   647 subsection{*Max and Min Combined with @{term Suc} *}
```
```   648
```
```   649 lemma max_number_of_Suc [simp]:
```
```   650      "max (Suc n) (number_of v) =
```
```   651         (let pv = number_of (Int.pred v) in
```
```   652          if neg pv then Suc n else Suc(max n (nat pv)))"
```
```   653 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def
```
```   654             split add: split_if nat.split)
```
```   655 apply (rule_tac x = "number_of v" in spec)
```
```   656 apply auto
```
```   657 done
```
```   658
```
```   659 lemma max_Suc_number_of [simp]:
```
```   660      "max (number_of v) (Suc n) =
```
```   661         (let pv = number_of (Int.pred v) in
```
```   662          if neg pv then Suc n else Suc(max (nat pv) n))"
```
```   663 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def
```
```   664             split add: split_if nat.split)
```
```   665 apply (rule_tac x = "number_of v" in spec)
```
```   666 apply auto
```
```   667 done
```
```   668
```
```   669 lemma min_number_of_Suc [simp]:
```
```   670      "min (Suc n) (number_of v) =
```
```   671         (let pv = number_of (Int.pred v) in
```
```   672          if neg pv then 0 else Suc(min n (nat pv)))"
```
```   673 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def
```
```   674             split add: split_if nat.split)
```
```   675 apply (rule_tac x = "number_of v" in spec)
```
```   676 apply auto
```
```   677 done
```
```   678
```
```   679 lemma min_Suc_number_of [simp]:
```
```   680      "min (number_of v) (Suc n) =
```
```   681         (let pv = number_of (Int.pred v) in
```
```   682          if neg pv then 0 else Suc(min (nat pv) n))"
```
```   683 apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def
```
```   684             split add: split_if nat.split)
```
```   685 apply (rule_tac x = "number_of v" in spec)
```
```   686 apply auto
```
```   687 done
```
```   688
```
```   689 subsection{*Literal arithmetic involving powers*}
```
```   690
```
```   691 lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n"
```
```   692 apply (induct "n")
```
```   693 apply (simp_all (no_asm_simp) add: nat_mult_distrib)
```
```   694 done
```
```   695
```
```   696 lemma power_nat_number_of:
```
```   697      "(number_of v :: nat) ^ n =
```
```   698        (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
```
```   699 by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
```
```   700          split add: split_if cong: imp_cong)
```
```   701
```
```   702
```
```   703 lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard]
```
```   704 declare power_nat_number_of_number_of [simp]
```
```   705
```
```   706
```
```   707
```
```   708 text{*For arbitrary rings*}
```
```   709
```
```   710 lemma power_number_of_even:
```
```   711   fixes z :: "'a::number_ring"
```
```   712   shows "z ^ number_of (Int.Bit0 w) = (let w = z ^ (number_of w) in w * w)"
```
```   713 unfolding Let_def nat_number_of_def number_of_Bit0
```
```   714 apply (rule_tac x = "number_of w" in spec, clarify)
```
```   715 apply (case_tac " (0::int) <= x")
```
```   716 apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square)
```
```   717 done
```
```   718
```
```   719 lemma power_number_of_odd:
```
```   720   fixes z :: "'a::number_ring"
```
```   721   shows "z ^ number_of (Int.Bit1 w) = (if (0::int) <= number_of w
```
```   722      then (let w = z ^ (number_of w) in z * w * w) else 1)"
```
```   723 unfolding Let_def nat_number_of_def number_of_Bit1
```
```   724 apply (rule_tac x = "number_of w" in spec, auto)
```
```   725 apply (simp only: nat_add_distrib nat_mult_distrib)
```
```   726 apply simp
```
```   727 apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat power_Suc)
```
```   728 done
```
```   729
```
```   730 lemmas zpower_number_of_even = power_number_of_even [where 'a=int]
```
```   731 lemmas zpower_number_of_odd = power_number_of_odd [where 'a=int]
```
```   732
```
```   733 lemmas power_number_of_even_number_of [simp] =
```
```   734     power_number_of_even [of "number_of v", standard]
```
```   735
```
```   736 lemmas power_number_of_odd_number_of [simp] =
```
```   737     power_number_of_odd [of "number_of v", standard]
```
```   738
```
```   739
```
```   740
```
```   741 ML
```
```   742 {*
```
```   743 val numeral_ss = @{simpset} addsimps @{thms numerals};
```
```   744
```
```   745 val nat_bin_arith_setup =
```
```   746  Lin_Arith.map_data
```
```   747    (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
```
```   748      {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms,
```
```   749       inj_thms = inj_thms,
```
```   750       lessD = lessD, neqE = neqE,
```
```   751       simpset = simpset addsimps @{thms neg_simps} @
```
```   752         [@{thm Suc_nat_number_of}, @{thm int_nat_number_of}]})
```
```   753 *}
```
```   754
```
```   755 declaration {* K nat_bin_arith_setup *}
```
```   756
```
```   757 (* Enable arith to deal with div/mod k where k is a numeral: *)
```
```   758 declare split_div[of _ _ "number_of k", standard, arith_split]
```
```   759 declare split_mod[of _ _ "number_of k", standard, arith_split]
```
```   760
```
```   761 lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
```
```   762   by (simp add: number_of_Pls nat_number_of_def)
```
```   763
```
```   764 lemma nat_number_of_Min: "number_of Int.Min = (0::nat)"
```
```   765   apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
```
```   766   done
```
```   767
```
```   768 lemma nat_number_of_Bit0:
```
```   769     "number_of (Int.Bit0 w) = (let n::nat = number_of w in n + n)"
```
```   770   unfolding nat_number_of_def number_of_is_id numeral_simps Let_def
```
```   771   by auto
```
```   772
```
```   773 lemma nat_number_of_Bit1:
```
```   774   "number_of (Int.Bit1 w) =
```
```   775     (if neg (number_of w :: int) then 0
```
```   776      else let n = number_of w in Suc (n + n))"
```
```   777   unfolding nat_number_of_def number_of_is_id numeral_simps neg_def Let_def
```
```   778   by auto
```
```   779
```
```   780 lemmas nat_number =
```
```   781   nat_number_of_Pls nat_number_of_Min
```
```   782   nat_number_of_Bit0 nat_number_of_Bit1
```
```   783
```
```   784 lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
```
```   785   by (simp add: Let_def)
```
```   786
```
```   787 lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring})"
```
```   788   by (simp only: number_of_Min power_minus1_even)
```
```   789
```
```   790 lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring})"
```
```   791   by (simp only: number_of_Min power_minus1_odd)
```
```   792
```
```   793
```
```   794 subsection{*Literal arithmetic and @{term of_nat}*}
```
```   795
```
```   796 lemma of_nat_double:
```
```   797      "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
```
```   798 by (simp only: mult_2 nat_add_distrib of_nat_add)
```
```   799
```
```   800 lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
```
```   801 by (simp only: nat_number_of_def)
```
```   802
```
```   803 lemma of_nat_number_of_lemma:
```
```   804      "of_nat (number_of v :: nat) =
```
```   805          (if 0 \<le> (number_of v :: int)
```
```   806           then (number_of v :: 'a :: number_ring)
```
```   807           else 0)"
```
```   808 by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat);
```
```   809
```
```   810 lemma of_nat_number_of_eq [simp]:
```
```   811      "of_nat (number_of v :: nat) =
```
```   812          (if neg (number_of v :: int) then 0
```
```   813           else (number_of v :: 'a :: number_ring))"
```
```   814 by (simp only: of_nat_number_of_lemma neg_def, simp)
```
```   815
```
```   816
```
```   817 subsection {*Lemmas for the Combination and Cancellation Simprocs*}
```
```   818
```
```   819 lemma nat_number_of_add_left:
```
```   820      "number_of v + (number_of v' + (k::nat)) =
```
```   821          (if neg (number_of v :: int) then number_of v' + k
```
```   822           else if neg (number_of v' :: int) then number_of v + k
```
```   823           else number_of (v + v') + k)"
```
```   824   unfolding nat_number_of_def number_of_is_id neg_def
```
```   825   by auto
```
```   826
```
```   827 lemma nat_number_of_mult_left:
```
```   828      "number_of v * (number_of v' * (k::nat)) =
```
```   829          (if v < Int.Pls then 0
```
```   830           else number_of (v * v') * k)"
```
```   831 by simp
```
```   832
```
```   833
```
```   834 subsubsection{*For @{text combine_numerals}*}
```
```   835
```
```   836 lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
```
```   837 by (simp add: add_mult_distrib)
```
```   838
```
```   839
```
```   840 subsubsection{*For @{text cancel_numerals}*}
```
```   841
```
```   842 lemma nat_diff_add_eq1:
```
```   843      "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
```
```   844 by (simp split add: nat_diff_split add: add_mult_distrib)
```
```   845
```
```   846 lemma nat_diff_add_eq2:
```
```   847      "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
```
```   848 by (simp split add: nat_diff_split add: add_mult_distrib)
```
```   849
```
```   850 lemma nat_eq_add_iff1:
```
```   851      "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
```
```   852 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   853
```
```   854 lemma nat_eq_add_iff2:
```
```   855      "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
```
```   856 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   857
```
```   858 lemma nat_less_add_iff1:
```
```   859      "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
```
```   860 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   861
```
```   862 lemma nat_less_add_iff2:
```
```   863      "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
```
```   864 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   865
```
```   866 lemma nat_le_add_iff1:
```
```   867      "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
```
```   868 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   869
```
```   870 lemma nat_le_add_iff2:
```
```   871      "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
```
```   872 by (auto split add: nat_diff_split simp add: add_mult_distrib)
```
```   873
```
```   874
```
```   875 subsubsection{*For @{text cancel_numeral_factors} *}
```
```   876
```
```   877 lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
```
```   878 by auto
```
```   879
```
```   880 lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
```
```   881 by auto
```
```   882
```
```   883 lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
```
```   884 by auto
```
```   885
```
```   886 lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
```
```   887 by auto
```
```   888
```
```   889 lemma nat_mult_dvd_cancel_disj[simp]:
```
```   890   "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
```
```   891 by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
```
```   892
```
```   893 lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
```
```   894 by(auto)
```
```   895
```
```   896
```
```   897 subsubsection{*For @{text cancel_factor} *}
```
```   898
```
```   899 lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
```
```   900 by auto
```
```   901
```
```   902 lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
```
```   903 by auto
```
```   904
```
```   905 lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
```
```   906 by auto
```
```   907
```
```   908 lemma nat_mult_div_cancel_disj[simp]:
```
```   909      "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
```
```   910 by (simp add: nat_mult_div_cancel1)
```
```   911
```
```   912
```
```   913 subsection {* Simprocs for the Naturals *}
```
```   914
```
```   915 use "Tools/nat_simprocs.ML"
```
```   916 declaration {* K nat_simprocs_setup *}
```
```   917
```
```   918 subsubsection{*For simplifying @{term "Suc m - K"} and  @{term "K - Suc m"}*}
```
```   919
```
```   920 text{*Where K above is a literal*}
```
```   921
```
```   922 lemma Suc_diff_eq_diff_pred: "Numeral0 < n ==> Suc m - n = m - (n - Numeral1)"
```
```   923 by (simp add: numeral_0_eq_0 numeral_1_eq_1 split add: nat_diff_split)
```
```   924
```
```   925 text {*Now just instantiating @{text n} to @{text "number_of v"} does
```
```   926   the right simplification, but with some redundant inequality
```
```   927   tests.*}
```
```   928 lemma neg_number_of_pred_iff_0:
```
```   929   "neg (number_of (Int.pred v)::int) = (number_of v = (0::nat))"
```
```   930 apply (subgoal_tac "neg (number_of (Int.pred v)) = (number_of v < Suc 0) ")
```
```   931 apply (simp only: less_Suc_eq_le le_0_eq)
```
```   932 apply (subst less_number_of_Suc, simp)
```
```   933 done
```
```   934
```
```   935 text{*No longer required as a simprule because of the @{text inverse_fold}
```
```   936    simproc*}
```
```   937 lemma Suc_diff_number_of:
```
```   938      "Int.Pls < v ==>
```
```   939       Suc m - (number_of v) = m - (number_of (Int.pred v))"
```
```   940 apply (subst Suc_diff_eq_diff_pred)
```
```   941 apply simp
```
```   942 apply (simp del: nat_numeral_1_eq_1)
```
```   943 apply (auto simp only: diff_nat_number_of less_0_number_of [symmetric]
```
```   944                         neg_number_of_pred_iff_0)
```
```   945 done
```
```   946
```
```   947 lemma diff_Suc_eq_diff_pred: "m - Suc n = (m - 1) - n"
```
```   948 by (simp add: numerals split add: nat_diff_split)
```
```   949
```
```   950
```
```   951 subsubsection{*For @{term nat_case} and @{term nat_rec}*}
```
```   952
```
```   953 lemma nat_case_number_of [simp]:
```
```   954      "nat_case a f (number_of v) =
```
```   955         (let pv = number_of (Int.pred v) in
```
```   956          if neg pv then a else f (nat pv))"
```
```   957 by (simp split add: nat.split add: Let_def neg_number_of_pred_iff_0)
```
```   958
```
```   959 lemma nat_case_add_eq_if [simp]:
```
```   960      "nat_case a f ((number_of v) + n) =
```
```   961        (let pv = number_of (Int.pred v) in
```
```   962          if neg pv then nat_case a f n else f (nat pv + n))"
```
```   963 apply (subst add_eq_if)
```
```   964 apply (simp split add: nat.split
```
```   965             del: nat_numeral_1_eq_1
```
```   966             add: nat_numeral_1_eq_1 [symmetric]
```
```   967                  numeral_1_eq_Suc_0 [symmetric]
```
```   968                  neg_number_of_pred_iff_0)
```
```   969 done
```
```   970
```
```   971 lemma nat_rec_number_of [simp]:
```
```   972      "nat_rec a f (number_of v) =
```
```   973         (let pv = number_of (Int.pred v) in
```
```   974          if neg pv then a else f (nat pv) (nat_rec a f (nat pv)))"
```
```   975 apply (case_tac " (number_of v) ::nat")
```
```   976 apply (simp_all (no_asm_simp) add: Let_def neg_number_of_pred_iff_0)
```
```   977 apply (simp split add: split_if_asm)
```
```   978 done
```
```   979
```
```   980 lemma nat_rec_add_eq_if [simp]:
```
```   981      "nat_rec a f (number_of v + n) =
```
```   982         (let pv = number_of (Int.pred v) in
```
```   983          if neg pv then nat_rec a f n
```
```   984                    else f (nat pv + n) (nat_rec a f (nat pv + n)))"
```
```   985 apply (subst add_eq_if)
```
```   986 apply (simp split add: nat.split
```
```   987             del: nat_numeral_1_eq_1
```
```   988             add: nat_numeral_1_eq_1 [symmetric]
```
```   989                  numeral_1_eq_Suc_0 [symmetric]
```
```   990                  neg_number_of_pred_iff_0)
```
```   991 done
```
```   992
```
```   993
```
```   994 subsubsection{*Various Other Lemmas*}
```
```   995
```
```   996 text {*Evens and Odds, for Mutilated Chess Board*}
```
```   997
```
```   998 text{*Lemmas for specialist use, NOT as default simprules*}
```
```   999 lemma nat_mult_2: "2 * z = (z+z::nat)"
```
```  1000 proof -
```
```  1001   have "2*z = (1 + 1)*z" by simp
```
```  1002   also have "... = z+z" by (simp add: left_distrib)
```
```  1003   finally show ?thesis .
```
```  1004 qed
```
```  1005
```
```  1006 lemma nat_mult_2_right: "z * 2 = (z+z::nat)"
```
```  1007 by (subst mult_commute, rule nat_mult_2)
```
```  1008
```
```  1009 text{*Case analysis on @{term "n<2"}*}
```
```  1010 lemma less_2_cases: "(n::nat) < 2 ==> n = 0 | n = Suc 0"
```
```  1011 by arith
```
```  1012
```
```  1013 lemma div2_Suc_Suc [simp]: "Suc(Suc m) div 2 = Suc (m div 2)"
```
```  1014 by arith
```
```  1015
```
```  1016 lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
```
```  1017 by (simp add: nat_mult_2 [symmetric])
```
```  1018
```
```  1019 lemma mod2_Suc_Suc [simp]: "Suc(Suc(m)) mod 2 = m mod 2"
```
```  1020 apply (subgoal_tac "m mod 2 < 2")
```
```  1021 apply (erule less_2_cases [THEN disjE])
```
```  1022 apply (simp_all (no_asm_simp) add: Let_def mod_Suc nat_1)
```
```  1023 done
```
```  1024
```
```  1025 lemma mod2_gr_0 [simp]: "!!m::nat. (0 < m mod 2) = (m mod 2 = 1)"
```
```  1026 apply (subgoal_tac "m mod 2 < 2")
```
```  1027 apply (force simp del: mod_less_divisor, simp)
```
```  1028 done
```
```  1029
```
```  1030 text{*Removal of Small Numerals: 0, 1 and (in additive positions) 2*}
```
```  1031
```
```  1032 lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"
```
```  1033 by simp
```
```  1034
```
```  1035 lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)"
```
```  1036 by simp
```
```  1037
```
```  1038 text{*Can be used to eliminate long strings of Sucs, but not by default*}
```
```  1039 lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
```
```  1040 by simp
```
```  1041
```
```  1042
```
```  1043 text{*These lemmas collapse some needless occurrences of Suc:
```
```  1044     at least three Sucs, since two and fewer are rewritten back to Suc again!
```
```  1045     We already have some rules to simplify operands smaller than 3.*}
```
```  1046
```
```  1047 lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
```
```  1048 by (simp add: Suc3_eq_add_3)
```
```  1049
```
```  1050 lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
```
```  1051 by (simp add: Suc3_eq_add_3)
```
```  1052
```
```  1053 lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
```
```  1054 by (simp add: Suc3_eq_add_3)
```
```  1055
```
```  1056 lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
```
```  1057 by (simp add: Suc3_eq_add_3)
```
```  1058
```
```  1059 lemmas Suc_div_eq_add3_div_number_of =
```
```  1060     Suc_div_eq_add3_div [of _ "number_of v", standard]
```
```  1061 declare Suc_div_eq_add3_div_number_of [simp]
```
```  1062
```
```  1063 lemmas Suc_mod_eq_add3_mod_number_of =
```
```  1064     Suc_mod_eq_add3_mod [of _ "number_of v", standard]
```
```  1065 declare Suc_mod_eq_add3_mod_number_of [simp]
```
```  1066
```
`  1067 end`