src/HOL/Nat_Numeral.thy
author haftmann
Mon May 11 15:18:32 2009 +0200 (2009-05-11)
changeset 31100 6a2e67fe4488
parent 31080 21ffc770ebc0
child 31104 ac8a12b0ed3c
permissions -rw-r--r--
tuned interface of Lin_Arith
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@31068
    10
uses ("Tools/nat_numeral_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@31034
   130
  by (simp add: add_nonneg_eq_0_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
wenzelm@23164
   459
subsection{*Comparisons*}
wenzelm@23164
   460
wenzelm@23164
   461
subsubsection{*Equals (=) *}
wenzelm@23164
   462
wenzelm@23164
   463
lemma eq_nat_nat_iff:
wenzelm@23164
   464
     "[| (0::int) <= z;  0 <= z' |] ==> (nat z = nat z') = (z=z')"
wenzelm@23164
   465
by (auto elim!: nonneg_eq_int)
wenzelm@23164
   466
wenzelm@23164
   467
lemma eq_nat_number_of [simp]:
wenzelm@23164
   468
     "((number_of v :: nat) = number_of v') =  
huffman@28969
   469
      (if neg (number_of v :: int) then (number_of v' :: int) \<le> 0
huffman@28969
   470
       else if neg (number_of v' :: int) then (number_of v :: int) = 0
huffman@28969
   471
       else v = v')"
huffman@28969
   472
  unfolding nat_number_of_def number_of_is_id neg_def
huffman@28969
   473
  by auto
wenzelm@23164
   474
wenzelm@23164
   475
wenzelm@23164
   476
subsubsection{*Less-than (<) *}
wenzelm@23164
   477
wenzelm@23164
   478
lemma less_nat_number_of [simp]:
huffman@29011
   479
  "(number_of v :: nat) < number_of v' \<longleftrightarrow>
huffman@29011
   480
    (if v < v' then Int.Pls < v' else False)"
huffman@29011
   481
  unfolding nat_number_of_def number_of_is_id numeral_simps
huffman@28961
   482
  by auto
wenzelm@23164
   483
wenzelm@23164
   484
huffman@29010
   485
subsubsection{*Less-than-or-equal *}
huffman@29010
   486
huffman@29010
   487
lemma le_nat_number_of [simp]:
huffman@29010
   488
  "(number_of v :: nat) \<le> number_of v' \<longleftrightarrow>
huffman@29010
   489
    (if v \<le> v' then True else v \<le> Int.Pls)"
huffman@29010
   490
  unfolding nat_number_of_def number_of_is_id numeral_simps
huffman@29010
   491
  by auto
huffman@29010
   492
wenzelm@23164
   493
(*Maps #n to n for n = 0, 1, 2*)
wenzelm@23164
   494
lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
wenzelm@23164
   495
wenzelm@23164
   496
wenzelm@23164
   497
subsection{*Powers with Numeric Exponents*}
wenzelm@23164
   498
wenzelm@23164
   499
text{*Squares of literal numerals will be evaluated.*}
haftmann@31014
   500
lemmas power2_eq_square_number_of [simp] =
wenzelm@23164
   501
    power2_eq_square [of "number_of w", standard]
wenzelm@23164
   502
wenzelm@23164
   503
wenzelm@23164
   504
text{*Simprules for comparisons where common factors can be cancelled.*}
wenzelm@23164
   505
lemmas zero_compare_simps =
wenzelm@23164
   506
    add_strict_increasing add_strict_increasing2 add_increasing
wenzelm@23164
   507
    zero_le_mult_iff zero_le_divide_iff 
wenzelm@23164
   508
    zero_less_mult_iff zero_less_divide_iff 
wenzelm@23164
   509
    mult_le_0_iff divide_le_0_iff 
wenzelm@23164
   510
    mult_less_0_iff divide_less_0_iff 
wenzelm@23164
   511
    zero_le_power2 power2_less_0
wenzelm@23164
   512
wenzelm@23164
   513
subsubsection{*Nat *}
wenzelm@23164
   514
wenzelm@23164
   515
lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
wenzelm@23164
   516
by (simp add: numerals)
wenzelm@23164
   517
wenzelm@23164
   518
(*Expresses a natural number constant as the Suc of another one.
wenzelm@23164
   519
  NOT suitable for rewriting because n recurs in the condition.*)
wenzelm@23164
   520
lemmas expand_Suc = Suc_pred' [of "number_of v", standard]
wenzelm@23164
   521
wenzelm@23164
   522
subsubsection{*Arith *}
wenzelm@23164
   523
wenzelm@23164
   524
lemma Suc_eq_add_numeral_1: "Suc n = n + 1"
wenzelm@23164
   525
by (simp add: numerals)
wenzelm@23164
   526
wenzelm@23164
   527
lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n"
wenzelm@23164
   528
by (simp add: numerals)
wenzelm@23164
   529
wenzelm@23164
   530
(* These two can be useful when m = number_of... *)
wenzelm@23164
   531
wenzelm@23164
   532
lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
huffman@30079
   533
  unfolding One_nat_def by (cases m) simp_all
wenzelm@23164
   534
wenzelm@23164
   535
lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
huffman@30079
   536
  unfolding One_nat_def by (cases m) simp_all
wenzelm@23164
   537
wenzelm@23164
   538
lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
huffman@30079
   539
  unfolding One_nat_def by (cases m) simp_all
wenzelm@23164
   540
wenzelm@23164
   541
wenzelm@23164
   542
subsection{*Comparisons involving (0::nat) *}
wenzelm@23164
   543
wenzelm@23164
   544
text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
wenzelm@23164
   545
wenzelm@23164
   546
lemma eq_number_of_0 [simp]:
huffman@29012
   547
  "number_of v = (0::nat) \<longleftrightarrow> v \<le> Int.Pls"
huffman@29012
   548
  unfolding nat_number_of_def number_of_is_id numeral_simps
huffman@29012
   549
  by auto
wenzelm@23164
   550
wenzelm@23164
   551
lemma eq_0_number_of [simp]:
huffman@29012
   552
  "(0::nat) = number_of v \<longleftrightarrow> v \<le> Int.Pls"
wenzelm@23164
   553
by (rule trans [OF eq_sym_conv eq_number_of_0])
wenzelm@23164
   554
wenzelm@23164
   555
lemma less_0_number_of [simp]:
huffman@29012
   556
   "(0::nat) < number_of v \<longleftrightarrow> Int.Pls < v"
huffman@29012
   557
  unfolding nat_number_of_def number_of_is_id numeral_simps
huffman@29012
   558
  by simp
wenzelm@23164
   559
wenzelm@23164
   560
lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
huffman@28969
   561
by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric])
wenzelm@23164
   562
wenzelm@23164
   563
wenzelm@23164
   564
wenzelm@23164
   565
subsection{*Comparisons involving  @{term Suc} *}
wenzelm@23164
   566
wenzelm@23164
   567
lemma eq_number_of_Suc [simp]:
wenzelm@23164
   568
     "(number_of v = Suc n) =  
haftmann@25919
   569
        (let pv = number_of (Int.pred v) in  
wenzelm@23164
   570
         if neg pv then False else nat pv = n)"
wenzelm@23164
   571
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
wenzelm@23164
   572
                  number_of_pred nat_number_of_def 
wenzelm@23164
   573
            split add: split_if)
wenzelm@23164
   574
apply (rule_tac x = "number_of v" in spec)
wenzelm@23164
   575
apply (auto simp add: nat_eq_iff)
wenzelm@23164
   576
done
wenzelm@23164
   577
wenzelm@23164
   578
lemma Suc_eq_number_of [simp]:
wenzelm@23164
   579
     "(Suc n = number_of v) =  
haftmann@25919
   580
        (let pv = number_of (Int.pred v) in  
wenzelm@23164
   581
         if neg pv then False else nat pv = n)"
wenzelm@23164
   582
by (rule trans [OF eq_sym_conv eq_number_of_Suc])
wenzelm@23164
   583
wenzelm@23164
   584
lemma less_number_of_Suc [simp]:
wenzelm@23164
   585
     "(number_of v < Suc n) =  
haftmann@25919
   586
        (let pv = number_of (Int.pred v) in  
wenzelm@23164
   587
         if neg pv then True else nat pv < n)"
wenzelm@23164
   588
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
wenzelm@23164
   589
                  number_of_pred nat_number_of_def  
wenzelm@23164
   590
            split add: split_if)
wenzelm@23164
   591
apply (rule_tac x = "number_of v" in spec)
wenzelm@23164
   592
apply (auto simp add: nat_less_iff)
wenzelm@23164
   593
done
wenzelm@23164
   594
wenzelm@23164
   595
lemma less_Suc_number_of [simp]:
wenzelm@23164
   596
     "(Suc n < number_of v) =  
haftmann@25919
   597
        (let pv = number_of (Int.pred v) in  
wenzelm@23164
   598
         if neg pv then False else n < nat pv)"
wenzelm@23164
   599
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
wenzelm@23164
   600
                  number_of_pred nat_number_of_def
wenzelm@23164
   601
            split add: split_if)
wenzelm@23164
   602
apply (rule_tac x = "number_of v" in spec)
wenzelm@23164
   603
apply (auto simp add: zless_nat_eq_int_zless)
wenzelm@23164
   604
done
wenzelm@23164
   605
wenzelm@23164
   606
lemma le_number_of_Suc [simp]:
wenzelm@23164
   607
     "(number_of v <= Suc n) =  
haftmann@25919
   608
        (let pv = number_of (Int.pred v) in  
wenzelm@23164
   609
         if neg pv then True else nat pv <= n)"
wenzelm@23164
   610
by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric])
wenzelm@23164
   611
wenzelm@23164
   612
lemma le_Suc_number_of [simp]:
wenzelm@23164
   613
     "(Suc n <= number_of v) =  
haftmann@25919
   614
        (let pv = number_of (Int.pred v) in  
wenzelm@23164
   615
         if neg pv then False else n <= nat pv)"
wenzelm@23164
   616
by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric])
wenzelm@23164
   617
wenzelm@23164
   618
haftmann@25919
   619
lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Int.Min"
wenzelm@23164
   620
by auto
wenzelm@23164
   621
wenzelm@23164
   622
wenzelm@23164
   623
wenzelm@23164
   624
subsection{*Max and Min Combined with @{term Suc} *}
wenzelm@23164
   625
wenzelm@23164
   626
lemma max_number_of_Suc [simp]:
wenzelm@23164
   627
     "max (Suc n) (number_of v) =  
haftmann@25919
   628
        (let pv = number_of (Int.pred v) in  
wenzelm@23164
   629
         if neg pv then Suc n else Suc(max n (nat pv)))"
wenzelm@23164
   630
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
wenzelm@23164
   631
            split add: split_if nat.split)
wenzelm@23164
   632
apply (rule_tac x = "number_of v" in spec) 
wenzelm@23164
   633
apply auto
wenzelm@23164
   634
done
wenzelm@23164
   635
 
wenzelm@23164
   636
lemma max_Suc_number_of [simp]:
wenzelm@23164
   637
     "max (number_of v) (Suc n) =  
haftmann@25919
   638
        (let pv = number_of (Int.pred v) in  
wenzelm@23164
   639
         if neg pv then Suc n else Suc(max (nat pv) n))"
wenzelm@23164
   640
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
wenzelm@23164
   641
            split add: split_if nat.split)
wenzelm@23164
   642
apply (rule_tac x = "number_of v" in spec) 
wenzelm@23164
   643
apply auto
wenzelm@23164
   644
done
wenzelm@23164
   645
 
wenzelm@23164
   646
lemma min_number_of_Suc [simp]:
wenzelm@23164
   647
     "min (Suc n) (number_of v) =  
haftmann@25919
   648
        (let pv = number_of (Int.pred v) in  
wenzelm@23164
   649
         if neg pv then 0 else Suc(min n (nat pv)))"
wenzelm@23164
   650
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
wenzelm@23164
   651
            split add: split_if nat.split)
wenzelm@23164
   652
apply (rule_tac x = "number_of v" in spec) 
wenzelm@23164
   653
apply auto
wenzelm@23164
   654
done
wenzelm@23164
   655
 
wenzelm@23164
   656
lemma min_Suc_number_of [simp]:
wenzelm@23164
   657
     "min (number_of v) (Suc n) =  
haftmann@25919
   658
        (let pv = number_of (Int.pred v) in  
wenzelm@23164
   659
         if neg pv then 0 else Suc(min (nat pv) n))"
wenzelm@23164
   660
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
wenzelm@23164
   661
            split add: split_if nat.split)
wenzelm@23164
   662
apply (rule_tac x = "number_of v" in spec) 
wenzelm@23164
   663
apply auto
wenzelm@23164
   664
done
wenzelm@23164
   665
 
wenzelm@23164
   666
subsection{*Literal arithmetic involving powers*}
wenzelm@23164
   667
wenzelm@23164
   668
lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n"
wenzelm@23164
   669
apply (induct "n")
wenzelm@23164
   670
apply (simp_all (no_asm_simp) add: nat_mult_distrib)
wenzelm@23164
   671
done
wenzelm@23164
   672
wenzelm@23164
   673
lemma power_nat_number_of:
wenzelm@23164
   674
     "(number_of v :: nat) ^ n =  
wenzelm@23164
   675
       (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
wenzelm@23164
   676
by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
wenzelm@23164
   677
         split add: split_if cong: imp_cong)
wenzelm@23164
   678
wenzelm@23164
   679
wenzelm@23164
   680
lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard]
wenzelm@23164
   681
declare power_nat_number_of_number_of [simp]
wenzelm@23164
   682
wenzelm@23164
   683
wenzelm@23164
   684
huffman@23294
   685
text{*For arbitrary rings*}
wenzelm@23164
   686
huffman@23294
   687
lemma power_number_of_even:
haftmann@31014
   688
  fixes z :: "'a::number_ring"
huffman@26086
   689
  shows "z ^ number_of (Int.Bit0 w) = (let w = z ^ (number_of w) in w * w)"
huffman@26086
   690
unfolding Let_def nat_number_of_def number_of_Bit0
wenzelm@23164
   691
apply (rule_tac x = "number_of w" in spec, clarify)
wenzelm@23164
   692
apply (case_tac " (0::int) <= x")
wenzelm@23164
   693
apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square)
wenzelm@23164
   694
done
wenzelm@23164
   695
huffman@23294
   696
lemma power_number_of_odd:
haftmann@31014
   697
  fixes z :: "'a::number_ring"
huffman@26086
   698
  shows "z ^ number_of (Int.Bit1 w) = (if (0::int) <= number_of w
wenzelm@23164
   699
     then (let w = z ^ (number_of w) in z * w * w) else 1)"
huffman@26086
   700
unfolding Let_def nat_number_of_def number_of_Bit1
wenzelm@23164
   701
apply (rule_tac x = "number_of w" in spec, auto)
wenzelm@23164
   702
apply (simp only: nat_add_distrib nat_mult_distrib)
wenzelm@23164
   703
apply simp
huffman@23294
   704
apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat power_Suc)
wenzelm@23164
   705
done
wenzelm@23164
   706
huffman@23294
   707
lemmas zpower_number_of_even = power_number_of_even [where 'a=int]
huffman@23294
   708
lemmas zpower_number_of_odd = power_number_of_odd [where 'a=int]
wenzelm@23164
   709
huffman@23294
   710
lemmas power_number_of_even_number_of [simp] =
huffman@23294
   711
    power_number_of_even [of "number_of v", standard]
wenzelm@23164
   712
huffman@23294
   713
lemmas power_number_of_odd_number_of [simp] =
huffman@23294
   714
    power_number_of_odd [of "number_of v", standard]
wenzelm@23164
   715
wenzelm@23164
   716
wenzelm@23164
   717
(* Enable arith to deal with div/mod k where k is a numeral: *)
wenzelm@23164
   718
declare split_div[of _ _ "number_of k", standard, arith_split]
wenzelm@23164
   719
declare split_mod[of _ _ "number_of k", standard, arith_split]
wenzelm@23164
   720
wenzelm@23164
   721
lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
wenzelm@23164
   722
  by (simp add: number_of_Pls nat_number_of_def)
wenzelm@23164
   723
haftmann@25919
   724
lemma nat_number_of_Min: "number_of Int.Min = (0::nat)"
wenzelm@23164
   725
  apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
wenzelm@23164
   726
  done
wenzelm@23164
   727
huffman@26086
   728
lemma nat_number_of_Bit0:
huffman@26086
   729
    "number_of (Int.Bit0 w) = (let n::nat = number_of w in n + n)"
huffman@28969
   730
  unfolding nat_number_of_def number_of_is_id numeral_simps Let_def
huffman@28969
   731
  by auto
huffman@26086
   732
huffman@26086
   733
lemma nat_number_of_Bit1:
huffman@26086
   734
  "number_of (Int.Bit1 w) =
wenzelm@23164
   735
    (if neg (number_of w :: int) then 0
wenzelm@23164
   736
     else let n = number_of w in Suc (n + n))"
huffman@28969
   737
  unfolding nat_number_of_def number_of_is_id numeral_simps neg_def Let_def
huffman@28968
   738
  by auto
wenzelm@23164
   739
wenzelm@23164
   740
lemmas nat_number =
wenzelm@23164
   741
  nat_number_of_Pls nat_number_of_Min
huffman@26086
   742
  nat_number_of_Bit0 nat_number_of_Bit1
wenzelm@23164
   743
wenzelm@23164
   744
lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
wenzelm@23164
   745
  by (simp add: Let_def)
wenzelm@23164
   746
haftmann@31014
   747
lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring})"
haftmann@31014
   748
  by (simp only: number_of_Min power_minus1_even)
wenzelm@23164
   749
haftmann@31014
   750
lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring})"
haftmann@31014
   751
  by (simp only: number_of_Min power_minus1_odd)
wenzelm@23164
   752
wenzelm@23164
   753
wenzelm@23164
   754
subsection{*Literal arithmetic and @{term of_nat}*}
wenzelm@23164
   755
wenzelm@23164
   756
lemma of_nat_double:
wenzelm@23164
   757
     "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
wenzelm@23164
   758
by (simp only: mult_2 nat_add_distrib of_nat_add) 
wenzelm@23164
   759
wenzelm@23164
   760
lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
wenzelm@23164
   761
by (simp only: nat_number_of_def)
wenzelm@23164
   762
wenzelm@23164
   763
lemma of_nat_number_of_lemma:
wenzelm@23164
   764
     "of_nat (number_of v :: nat) =  
wenzelm@23164
   765
         (if 0 \<le> (number_of v :: int) 
wenzelm@23164
   766
          then (number_of v :: 'a :: number_ring)
wenzelm@23164
   767
          else 0)"
wenzelm@23164
   768
by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat);
wenzelm@23164
   769
wenzelm@23164
   770
lemma of_nat_number_of_eq [simp]:
wenzelm@23164
   771
     "of_nat (number_of v :: nat) =  
wenzelm@23164
   772
         (if neg (number_of v :: int) then 0  
wenzelm@23164
   773
          else (number_of v :: 'a :: number_ring))"
wenzelm@23164
   774
by (simp only: of_nat_number_of_lemma neg_def, simp) 
wenzelm@23164
   775
wenzelm@23164
   776
wenzelm@23164
   777
subsection {*Lemmas for the Combination and Cancellation Simprocs*}
wenzelm@23164
   778
wenzelm@23164
   779
lemma nat_number_of_add_left:
wenzelm@23164
   780
     "number_of v + (number_of v' + (k::nat)) =  
wenzelm@23164
   781
         (if neg (number_of v :: int) then number_of v' + k  
wenzelm@23164
   782
          else if neg (number_of v' :: int) then number_of v + k  
wenzelm@23164
   783
          else number_of (v + v') + k)"
huffman@28968
   784
  unfolding nat_number_of_def number_of_is_id neg_def
huffman@28968
   785
  by auto
wenzelm@23164
   786
wenzelm@23164
   787
lemma nat_number_of_mult_left:
wenzelm@23164
   788
     "number_of v * (number_of v' * (k::nat)) =  
huffman@29012
   789
         (if v < Int.Pls then 0
wenzelm@23164
   790
          else number_of (v * v') * k)"
wenzelm@23164
   791
by simp
wenzelm@23164
   792
wenzelm@23164
   793
wenzelm@23164
   794
subsubsection{*For @{text combine_numerals}*}
wenzelm@23164
   795
wenzelm@23164
   796
lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
wenzelm@23164
   797
by (simp add: add_mult_distrib)
wenzelm@23164
   798
wenzelm@23164
   799
wenzelm@23164
   800
subsubsection{*For @{text cancel_numerals}*}
wenzelm@23164
   801
wenzelm@23164
   802
lemma nat_diff_add_eq1:
wenzelm@23164
   803
     "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
wenzelm@23164
   804
by (simp split add: nat_diff_split add: add_mult_distrib)
wenzelm@23164
   805
wenzelm@23164
   806
lemma nat_diff_add_eq2:
wenzelm@23164
   807
     "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
wenzelm@23164
   808
by (simp split add: nat_diff_split add: add_mult_distrib)
wenzelm@23164
   809
wenzelm@23164
   810
lemma nat_eq_add_iff1:
wenzelm@23164
   811
     "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
wenzelm@23164
   812
by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164
   813
wenzelm@23164
   814
lemma nat_eq_add_iff2:
wenzelm@23164
   815
     "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
wenzelm@23164
   816
by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164
   817
wenzelm@23164
   818
lemma nat_less_add_iff1:
wenzelm@23164
   819
     "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
wenzelm@23164
   820
by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164
   821
wenzelm@23164
   822
lemma nat_less_add_iff2:
wenzelm@23164
   823
     "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
wenzelm@23164
   824
by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164
   825
wenzelm@23164
   826
lemma nat_le_add_iff1:
wenzelm@23164
   827
     "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
wenzelm@23164
   828
by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164
   829
wenzelm@23164
   830
lemma nat_le_add_iff2:
wenzelm@23164
   831
     "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
wenzelm@23164
   832
by (auto split add: nat_diff_split simp add: add_mult_distrib)
wenzelm@23164
   833
wenzelm@23164
   834
wenzelm@23164
   835
subsubsection{*For @{text cancel_numeral_factors} *}
wenzelm@23164
   836
wenzelm@23164
   837
lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
wenzelm@23164
   838
by auto
wenzelm@23164
   839
wenzelm@23164
   840
lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
wenzelm@23164
   841
by auto
wenzelm@23164
   842
wenzelm@23164
   843
lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
wenzelm@23164
   844
by auto
wenzelm@23164
   845
wenzelm@23164
   846
lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
wenzelm@23164
   847
by auto
wenzelm@23164
   848
nipkow@23969
   849
lemma nat_mult_dvd_cancel_disj[simp]:
nipkow@23969
   850
  "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
nipkow@23969
   851
by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
nipkow@23969
   852
nipkow@23969
   853
lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
nipkow@23969
   854
by(auto)
nipkow@23969
   855
wenzelm@23164
   856
wenzelm@23164
   857
subsubsection{*For @{text cancel_factor} *}
wenzelm@23164
   858
wenzelm@23164
   859
lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
wenzelm@23164
   860
by auto
wenzelm@23164
   861
wenzelm@23164
   862
lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
wenzelm@23164
   863
by auto
wenzelm@23164
   864
wenzelm@23164
   865
lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
wenzelm@23164
   866
by auto
wenzelm@23164
   867
nipkow@23969
   868
lemma nat_mult_div_cancel_disj[simp]:
wenzelm@23164
   869
     "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
wenzelm@23164
   870
by (simp add: nat_mult_div_cancel1)
wenzelm@23164
   871
haftmann@30652
   872
haftmann@30652
   873
subsection {* Simprocs for the Naturals *}
haftmann@30652
   874
haftmann@31068
   875
use "Tools/nat_numeral_simprocs.ML"
haftmann@31068
   876
haftmann@31100
   877
declaration {* 
haftmann@31100
   878
  K (Lin_Arith.add_simps (@{thms neg_simps} @ [@{thm Suc_nat_number_of}, @{thm int_nat_number_of}])
haftmann@31100
   879
  #> Lin_Arith.add_simps (@{thms ring_distribs} @ [@{thm Let_number_of}, @{thm Let_0}, @{thm Let_1},
haftmann@31100
   880
     @{thm nat_0}, @{thm nat_1},
haftmann@31100
   881
     @{thm add_nat_number_of}, @{thm diff_nat_number_of}, @{thm mult_nat_number_of},
haftmann@31100
   882
     @{thm eq_nat_number_of}, @{thm less_nat_number_of}, @{thm le_number_of_eq_not_less},
haftmann@31100
   883
     @{thm le_Suc_number_of}, @{thm le_number_of_Suc},
haftmann@31100
   884
     @{thm less_Suc_number_of}, @{thm less_number_of_Suc},
haftmann@31100
   885
     @{thm Suc_eq_number_of}, @{thm eq_number_of_Suc},
haftmann@31100
   886
     @{thm mult_Suc}, @{thm mult_Suc_right},
haftmann@31100
   887
     @{thm add_Suc}, @{thm add_Suc_right},
haftmann@31100
   888
     @{thm eq_number_of_0}, @{thm eq_0_number_of}, @{thm less_0_number_of},
haftmann@31100
   889
     @{thm of_int_number_of_eq}, @{thm of_nat_number_of_eq}, @{thm nat_number_of},
haftmann@31100
   890
     @{thm if_True}, @{thm if_False}])
haftmann@31100
   891
  #> Lin_Arith.add_simprocs (Nat_Numeral_Simprocs.combine_numerals :: Nat_Numeral_Simprocs.cancel_numerals))
haftmann@31068
   892
*}
haftmann@31068
   893
haftmann@30652
   894
haftmann@30652
   895
subsubsection{*For simplifying @{term "Suc m - K"} and  @{term "K - Suc m"}*}
haftmann@30652
   896
haftmann@30652
   897
text{*Where K above is a literal*}
haftmann@30652
   898
haftmann@30652
   899
lemma Suc_diff_eq_diff_pred: "Numeral0 < n ==> Suc m - n = m - (n - Numeral1)"
haftmann@30652
   900
by (simp add: numeral_0_eq_0 numeral_1_eq_1 split add: nat_diff_split)
haftmann@30652
   901
haftmann@30652
   902
text {*Now just instantiating @{text n} to @{text "number_of v"} does
haftmann@30652
   903
  the right simplification, but with some redundant inequality
haftmann@30652
   904
  tests.*}
haftmann@30652
   905
lemma neg_number_of_pred_iff_0:
haftmann@30652
   906
  "neg (number_of (Int.pred v)::int) = (number_of v = (0::nat))"
haftmann@30652
   907
apply (subgoal_tac "neg (number_of (Int.pred v)) = (number_of v < Suc 0) ")
haftmann@30652
   908
apply (simp only: less_Suc_eq_le le_0_eq)
haftmann@30652
   909
apply (subst less_number_of_Suc, simp)
haftmann@30652
   910
done
haftmann@30652
   911
haftmann@30652
   912
text{*No longer required as a simprule because of the @{text inverse_fold}
haftmann@30652
   913
   simproc*}
haftmann@30652
   914
lemma Suc_diff_number_of:
haftmann@30652
   915
     "Int.Pls < v ==>
haftmann@30652
   916
      Suc m - (number_of v) = m - (number_of (Int.pred v))"
haftmann@30652
   917
apply (subst Suc_diff_eq_diff_pred)
haftmann@30652
   918
apply simp
haftmann@30652
   919
apply (simp del: nat_numeral_1_eq_1)
haftmann@30652
   920
apply (auto simp only: diff_nat_number_of less_0_number_of [symmetric]
haftmann@30652
   921
                        neg_number_of_pred_iff_0)
haftmann@30652
   922
done
haftmann@30652
   923
haftmann@30652
   924
lemma diff_Suc_eq_diff_pred: "m - Suc n = (m - 1) - n"
haftmann@30652
   925
by (simp add: numerals split add: nat_diff_split)
haftmann@30652
   926
haftmann@30652
   927
haftmann@30652
   928
subsubsection{*For @{term nat_case} and @{term nat_rec}*}
haftmann@30652
   929
haftmann@30652
   930
lemma nat_case_number_of [simp]:
haftmann@30652
   931
     "nat_case a f (number_of v) =
haftmann@30652
   932
        (let pv = number_of (Int.pred v) in
haftmann@30652
   933
         if neg pv then a else f (nat pv))"
haftmann@30652
   934
by (simp split add: nat.split add: Let_def neg_number_of_pred_iff_0)
haftmann@30652
   935
haftmann@30652
   936
lemma nat_case_add_eq_if [simp]:
haftmann@30652
   937
     "nat_case a f ((number_of v) + n) =
haftmann@30652
   938
       (let pv = number_of (Int.pred v) in
haftmann@30652
   939
         if neg pv then nat_case a f n else f (nat pv + n))"
haftmann@30652
   940
apply (subst add_eq_if)
haftmann@30652
   941
apply (simp split add: nat.split
haftmann@30652
   942
            del: nat_numeral_1_eq_1
haftmann@30652
   943
            add: nat_numeral_1_eq_1 [symmetric]
haftmann@30652
   944
                 numeral_1_eq_Suc_0 [symmetric]
haftmann@30652
   945
                 neg_number_of_pred_iff_0)
haftmann@30652
   946
done
haftmann@30652
   947
haftmann@30652
   948
lemma nat_rec_number_of [simp]:
haftmann@30652
   949
     "nat_rec a f (number_of v) =
haftmann@30652
   950
        (let pv = number_of (Int.pred v) in
haftmann@30652
   951
         if neg pv then a else f (nat pv) (nat_rec a f (nat pv)))"
haftmann@30652
   952
apply (case_tac " (number_of v) ::nat")
haftmann@30652
   953
apply (simp_all (no_asm_simp) add: Let_def neg_number_of_pred_iff_0)
haftmann@30652
   954
apply (simp split add: split_if_asm)
haftmann@30652
   955
done
haftmann@30652
   956
haftmann@30652
   957
lemma nat_rec_add_eq_if [simp]:
haftmann@30652
   958
     "nat_rec a f (number_of v + n) =
haftmann@30652
   959
        (let pv = number_of (Int.pred v) in
haftmann@30652
   960
         if neg pv then nat_rec a f n
haftmann@30652
   961
                   else f (nat pv + n) (nat_rec a f (nat pv + n)))"
haftmann@30652
   962
apply (subst add_eq_if)
haftmann@30652
   963
apply (simp split add: nat.split
haftmann@30652
   964
            del: nat_numeral_1_eq_1
haftmann@30652
   965
            add: nat_numeral_1_eq_1 [symmetric]
haftmann@30652
   966
                 numeral_1_eq_Suc_0 [symmetric]
haftmann@30652
   967
                 neg_number_of_pred_iff_0)
haftmann@30652
   968
done
haftmann@30652
   969
haftmann@30652
   970
haftmann@30652
   971
subsubsection{*Various Other Lemmas*}
haftmann@30652
   972
nipkow@31080
   973
lemma card_UNIV_bool[simp]: "card (UNIV :: bool set) = 2"
nipkow@31080
   974
by(simp add: UNIV_bool)
nipkow@31080
   975
haftmann@30652
   976
text {*Evens and Odds, for Mutilated Chess Board*}
haftmann@30652
   977
haftmann@30652
   978
text{*Lemmas for specialist use, NOT as default simprules*}
haftmann@30652
   979
lemma nat_mult_2: "2 * z = (z+z::nat)"
haftmann@30652
   980
proof -
haftmann@30652
   981
  have "2*z = (1 + 1)*z" by simp
haftmann@30652
   982
  also have "... = z+z" by (simp add: left_distrib)
haftmann@30652
   983
  finally show ?thesis .
haftmann@30652
   984
qed
haftmann@30652
   985
haftmann@30652
   986
lemma nat_mult_2_right: "z * 2 = (z+z::nat)"
haftmann@30652
   987
by (subst mult_commute, rule nat_mult_2)
haftmann@30652
   988
haftmann@30652
   989
text{*Case analysis on @{term "n<2"}*}
haftmann@30652
   990
lemma less_2_cases: "(n::nat) < 2 ==> n = 0 | n = Suc 0"
haftmann@30652
   991
by arith
haftmann@30652
   992
haftmann@30652
   993
lemma div2_Suc_Suc [simp]: "Suc(Suc m) div 2 = Suc (m div 2)"
haftmann@30652
   994
by arith
haftmann@30652
   995
haftmann@30652
   996
lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
haftmann@30652
   997
by (simp add: nat_mult_2 [symmetric])
haftmann@30652
   998
haftmann@30652
   999
lemma mod2_Suc_Suc [simp]: "Suc(Suc(m)) mod 2 = m mod 2"
haftmann@30652
  1000
apply (subgoal_tac "m mod 2 < 2")
haftmann@30652
  1001
apply (erule less_2_cases [THEN disjE])
haftmann@30652
  1002
apply (simp_all (no_asm_simp) add: Let_def mod_Suc nat_1)
haftmann@30652
  1003
done
haftmann@30652
  1004
haftmann@30652
  1005
lemma mod2_gr_0 [simp]: "!!m::nat. (0 < m mod 2) = (m mod 2 = 1)"
haftmann@30652
  1006
apply (subgoal_tac "m mod 2 < 2")
haftmann@30652
  1007
apply (force simp del: mod_less_divisor, simp)
haftmann@30652
  1008
done
haftmann@30652
  1009
haftmann@30652
  1010
text{*Removal of Small Numerals: 0, 1 and (in additive positions) 2*}
haftmann@30652
  1011
haftmann@30652
  1012
lemma add_2_eq_Suc [simp]: "2 + n = Suc (Suc n)"
haftmann@30652
  1013
by simp
haftmann@30652
  1014
haftmann@30652
  1015
lemma add_2_eq_Suc' [simp]: "n + 2 = Suc (Suc n)"
haftmann@30652
  1016
by simp
haftmann@30652
  1017
haftmann@30652
  1018
text{*Can be used to eliminate long strings of Sucs, but not by default*}
haftmann@30652
  1019
lemma Suc3_eq_add_3: "Suc (Suc (Suc n)) = 3 + n"
haftmann@30652
  1020
by simp
haftmann@30652
  1021
haftmann@30652
  1022
haftmann@30652
  1023
text{*These lemmas collapse some needless occurrences of Suc:
haftmann@30652
  1024
    at least three Sucs, since two and fewer are rewritten back to Suc again!
haftmann@30652
  1025
    We already have some rules to simplify operands smaller than 3.*}
haftmann@30652
  1026
haftmann@30652
  1027
lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
haftmann@30652
  1028
by (simp add: Suc3_eq_add_3)
haftmann@30652
  1029
haftmann@30652
  1030
lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
haftmann@30652
  1031
by (simp add: Suc3_eq_add_3)
haftmann@30652
  1032
haftmann@30652
  1033
lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
haftmann@30652
  1034
by (simp add: Suc3_eq_add_3)
haftmann@30652
  1035
haftmann@30652
  1036
lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
haftmann@30652
  1037
by (simp add: Suc3_eq_add_3)
haftmann@30652
  1038
haftmann@30652
  1039
lemmas Suc_div_eq_add3_div_number_of =
haftmann@30652
  1040
    Suc_div_eq_add3_div [of _ "number_of v", standard]
haftmann@30652
  1041
declare Suc_div_eq_add3_div_number_of [simp]
haftmann@30652
  1042
haftmann@30652
  1043
lemmas Suc_mod_eq_add3_mod_number_of =
haftmann@30652
  1044
    Suc_mod_eq_add3_mod [of _ "number_of v", standard]
haftmann@30652
  1045
declare Suc_mod_eq_add3_mod_number_of [simp]
haftmann@30652
  1046
haftmann@30960
  1047
end