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