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