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