src/HOL/Integ/NatBin.thy
author nipkow
Thu May 24 22:55:53 2007 +0200 (2007-05-24)
changeset 23096 423ad2fe9f76
parent 23095 45f10b70e891
permissions -rw-r--r--
*** empty log message ***
paulson@7032
     1
(*  Title:      HOL/NatBin.thy
paulson@7032
     2
    ID:         $Id$
paulson@7032
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@7032
     4
    Copyright   1999  University of Cambridge
wenzelm@12838
     5
*)
paulson@7032
     6
wenzelm@12838
     7
header {* Binary arithmetic for the natural numbers *}
paulson@7032
     8
nipkow@15131
     9
theory NatBin
nipkow@15140
    10
imports IntDiv
nipkow@15131
    11
begin
paulson@7032
    12
wenzelm@12838
    13
text {*
paulson@14273
    14
  Arithmetic for naturals is reduced to that for the non-negative integers.
wenzelm@12838
    15
*}
wenzelm@12838
    16
haftmann@22319
    17
instance nat :: number
haftmann@22803
    18
  nat_number_of_def [code inline]: "number_of v == nat (number_of (v\<Colon>int))" ..
wenzelm@19380
    19
wenzelm@19380
    20
abbreviation (xsymbols)
wenzelm@21404
    21
  square :: "'a::power => 'a"  ("(_\<twosuperior>)" [1000] 999) where
wenzelm@19380
    22
  "x\<twosuperior> == x^2"
wenzelm@19380
    23
wenzelm@21210
    24
notation (latex output)
wenzelm@19656
    25
  square  ("(_\<twosuperior>)" [1000] 999)
wenzelm@19380
    26
wenzelm@21210
    27
notation (HTML output)
wenzelm@19656
    28
  square  ("(_\<twosuperior>)" [1000] 999)
wenzelm@12838
    29
paulson@14272
    30
paulson@14353
    31
subsection{*Function @{term nat}: Coercion from Type @{typ int} to @{typ nat}*}
paulson@14272
    32
paulson@14273
    33
declare nat_0 [simp] nat_1 [simp]
paulson@14273
    34
paulson@14273
    35
lemma nat_number_of [simp]: "nat (number_of w) = number_of w"
paulson@14273
    36
by (simp add: nat_number_of_def)
paulson@14272
    37
paulson@14387
    38
lemma nat_numeral_0_eq_0 [simp]: "Numeral0 = (0::nat)"
paulson@14273
    39
by (simp add: nat_number_of_def)
paulson@14272
    40
paulson@14387
    41
lemma nat_numeral_1_eq_1 [simp]: "Numeral1 = (1::nat)"
paulson@14273
    42
by (simp add: nat_1 nat_number_of_def)
paulson@14272
    43
paulson@14272
    44
lemma numeral_1_eq_Suc_0: "Numeral1 = Suc 0"
paulson@14387
    45
by (simp add: nat_numeral_1_eq_1)
paulson@14272
    46
paulson@14272
    47
lemma numeral_2_eq_2: "2 = Suc (Suc 0)"
paulson@14272
    48
apply (unfold nat_number_of_def)
paulson@14272
    49
apply (rule nat_2)
paulson@14272
    50
done
paulson@14272
    51
paulson@14272
    52
paulson@14273
    53
text{*Distributive laws for type @{text nat}.  The others are in theory
paulson@14273
    54
   @{text IntArith}, but these require div and mod to be defined for type
paulson@14273
    55
   "int".  They also need some of the lemmas proved above.*}
paulson@14272
    56
paulson@14272
    57
lemma nat_div_distrib: "(0::int) <= z ==> nat (z div z') = nat z div nat z'"
paulson@14272
    58
apply (case_tac "0 <= z'")
paulson@14272
    59
apply (auto simp add: div_nonneg_neg_le0 DIVISION_BY_ZERO_DIV)
paulson@14273
    60
apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO)
paulson@14272
    61
apply (auto elim!: nonneg_eq_int)
paulson@14272
    62
apply (rename_tac m m')
paulson@14272
    63
apply (subgoal_tac "0 <= int m div int m'")
paulson@14387
    64
 prefer 2 apply (simp add: nat_numeral_0_eq_0 pos_imp_zdiv_nonneg_iff) 
paulson@14273
    65
apply (rule inj_int [THEN injD], simp)
paulson@14272
    66
apply (rule_tac r = "int (m mod m') " in quorem_div)
paulson@14273
    67
 prefer 2 apply force
paulson@14387
    68
apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0 zadd_int 
paulson@14273
    69
                 zmult_int)
paulson@14272
    70
done
paulson@14272
    71
paulson@14272
    72
(*Fails if z'<0: the LHS collapses to (nat z) but the RHS doesn't*)
paulson@14273
    73
lemma nat_mod_distrib:
paulson@14273
    74
     "[| (0::int) <= z;  0 <= z' |] ==> nat (z mod z') = nat z mod nat z'"
paulson@14273
    75
apply (case_tac "z' = 0", simp add: DIVISION_BY_ZERO)
paulson@14272
    76
apply (auto elim!: nonneg_eq_int)
paulson@14272
    77
apply (rename_tac m m')
paulson@14272
    78
apply (subgoal_tac "0 <= int m mod int m'")
paulson@14387
    79
 prefer 2 apply (simp add: nat_less_iff nat_numeral_0_eq_0 pos_mod_sign) 
paulson@14273
    80
apply (rule inj_int [THEN injD], simp)
paulson@14272
    81
apply (rule_tac q = "int (m div m') " in quorem_mod)
paulson@14273
    82
 prefer 2 apply force
paulson@14387
    83
apply (simp add: nat_less_iff [symmetric] quorem_def nat_numeral_0_eq_0 zadd_int zmult_int)
paulson@14272
    84
done
paulson@14272
    85
paulson@16413
    86
text{*Suggested by Matthias Daum*}
paulson@16413
    87
lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
webertj@20217
    88
apply (subgoal_tac "nat x div nat k < nat x")
webertj@20217
    89
 apply (simp (asm_lr) add: nat_div_distrib [symmetric])
paulson@16413
    90
apply (rule Divides.div_less_dividend, simp_all) 
paulson@16413
    91
done
paulson@16413
    92
paulson@14353
    93
subsection{*Function @{term int}: Coercion from Type @{typ nat} to @{typ int}*}
paulson@14272
    94
paulson@14272
    95
(*"neg" is used in rewrite rules for binary comparisons*)
paulson@14387
    96
lemma int_nat_number_of [simp]:
paulson@14273
    97
     "int (number_of v :: nat) =  
paulson@14378
    98
         (if neg (number_of v :: int) then 0  
paulson@14272
    99
          else (number_of v :: int))"
paulson@14272
   100
by (simp del: nat_number_of
paulson@14272
   101
	 add: neg_nat nat_number_of_def not_neg_nat add_assoc)
paulson@14272
   102
paulson@14272
   103
paulson@14390
   104
subsubsection{*Successor *}
paulson@14272
   105
paulson@14272
   106
lemma Suc_nat_eq_nat_zadd1: "(0::int) <= z ==> Suc (nat z) = nat (1 + z)"
paulson@14272
   107
apply (rule sym)
paulson@14273
   108
apply (simp add: nat_eq_iff int_Suc)
paulson@14272
   109
done
paulson@14272
   110
paulson@14273
   111
lemma Suc_nat_number_of_add:
paulson@14273
   112
     "Suc (number_of v + n) =  
haftmann@20500
   113
        (if neg (number_of v :: int) then 1+n else number_of (Numeral.succ v) + n)" 
paulson@14272
   114
by (simp del: nat_number_of 
paulson@14272
   115
         add: nat_number_of_def neg_nat
paulson@14272
   116
              Suc_nat_eq_nat_zadd1 number_of_succ) 
paulson@14272
   117
paulson@14387
   118
lemma Suc_nat_number_of [simp]:
paulson@14273
   119
     "Suc (number_of v) =  
haftmann@20500
   120
        (if neg (number_of v :: int) then 1 else number_of (Numeral.succ v))"
paulson@14273
   121
apply (cut_tac n = 0 in Suc_nat_number_of_add)
paulson@14272
   122
apply (simp cong del: if_weak_cong)
paulson@14272
   123
done
paulson@14272
   124
paulson@14272
   125
paulson@14390
   126
subsubsection{*Addition *}
paulson@14272
   127
paulson@14272
   128
(*"neg" is used in rewrite rules for binary comparisons*)
paulson@14387
   129
lemma add_nat_number_of [simp]:
paulson@14273
   130
     "(number_of v :: nat) + number_of v' =  
paulson@14378
   131
         (if neg (number_of v :: int) then number_of v'  
paulson@14378
   132
          else if neg (number_of v' :: int) then number_of v  
haftmann@20485
   133
          else number_of (v + v'))"
paulson@14272
   134
by (force dest!: neg_nat
paulson@14272
   135
          simp del: nat_number_of
paulson@14272
   136
          simp add: nat_number_of_def nat_add_distrib [symmetric]) 
paulson@14272
   137
paulson@14272
   138
paulson@14390
   139
subsubsection{*Subtraction *}
paulson@14272
   140
paulson@14273
   141
lemma diff_nat_eq_if:
paulson@14273
   142
     "nat z - nat z' =  
paulson@14272
   143
        (if neg z' then nat z   
paulson@14272
   144
         else let d = z-z' in     
paulson@14272
   145
              if neg d then 0 else nat d)"
paulson@14273
   146
apply (simp add: Let_def nat_diff_distrib [symmetric] neg_eq_less_0 not_neg_eq_ge_0)
paulson@14272
   147
done
paulson@14272
   148
paulson@14387
   149
lemma diff_nat_number_of [simp]: 
paulson@14272
   150
     "(number_of v :: nat) - number_of v' =  
paulson@14378
   151
        (if neg (number_of v' :: int) then number_of v  
haftmann@20485
   152
         else let d = number_of (v + uminus v') in     
paulson@14272
   153
              if neg d then 0 else nat d)"
paulson@14272
   154
by (simp del: nat_number_of add: diff_nat_eq_if nat_number_of_def) 
paulson@14272
   155
paulson@14272
   156
paulson@14272
   157
paulson@14390
   158
subsubsection{*Multiplication *}
paulson@14272
   159
paulson@14387
   160
lemma mult_nat_number_of [simp]:
paulson@14273
   161
     "(number_of v :: nat) * number_of v' =  
haftmann@20485
   162
       (if neg (number_of v :: int) then 0 else number_of (v * v'))"
paulson@14272
   163
by (force dest!: neg_nat
paulson@14272
   164
          simp del: nat_number_of
paulson@14272
   165
          simp add: nat_number_of_def nat_mult_distrib [symmetric]) 
paulson@14272
   166
paulson@14272
   167
paulson@14272
   168
paulson@14390
   169
subsubsection{*Quotient *}
paulson@14272
   170
paulson@14387
   171
lemma div_nat_number_of [simp]:
paulson@14273
   172
     "(number_of v :: nat)  div  number_of v' =  
paulson@14378
   173
          (if neg (number_of v :: int) then 0  
paulson@14272
   174
           else nat (number_of v div number_of v'))"
paulson@14272
   175
by (force dest!: neg_nat
paulson@14272
   176
          simp del: nat_number_of
paulson@14272
   177
          simp add: nat_number_of_def nat_div_distrib [symmetric]) 
paulson@14272
   178
paulson@14387
   179
lemma one_div_nat_number_of [simp]:
paulson@14387
   180
     "(Suc 0)  div  number_of v' = (nat (1 div number_of v'))" 
paulson@14387
   181
by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
paulson@14272
   182
paulson@14272
   183
paulson@14390
   184
subsubsection{*Remainder *}
paulson@14272
   185
paulson@14387
   186
lemma mod_nat_number_of [simp]:
paulson@14273
   187
     "(number_of v :: nat)  mod  number_of v' =  
paulson@14378
   188
        (if neg (number_of v :: int) then 0  
paulson@14378
   189
         else if neg (number_of v' :: int) then number_of v  
paulson@14272
   190
         else nat (number_of v mod number_of v'))"
paulson@14272
   191
by (force dest!: neg_nat
paulson@14272
   192
          simp del: nat_number_of
paulson@14272
   193
          simp add: nat_number_of_def nat_mod_distrib [symmetric]) 
paulson@14272
   194
paulson@14387
   195
lemma one_mod_nat_number_of [simp]:
paulson@14387
   196
     "(Suc 0)  mod  number_of v' =  
paulson@14387
   197
        (if neg (number_of v' :: int) then Suc 0
paulson@14387
   198
         else nat (1 mod number_of v'))"
paulson@14387
   199
by (simp del: nat_numeral_1_eq_1 add: numeral_1_eq_Suc_0 [symmetric]) 
paulson@14387
   200
paulson@14387
   201
nipkow@18978
   202
subsubsection{* Divisibility *}
nipkow@18978
   203
paulson@18984
   204
lemmas dvd_eq_mod_eq_0_number_of =
paulson@18984
   205
  dvd_eq_mod_eq_0 [of "number_of x" "number_of y", standard]
paulson@18984
   206
paulson@18984
   207
declare dvd_eq_mod_eq_0_number_of [simp]
paulson@14272
   208
paulson@14272
   209
ML
paulson@14272
   210
{*
paulson@14272
   211
val nat_number_of_def = thm"nat_number_of_def";
paulson@14272
   212
paulson@14272
   213
val nat_number_of = thm"nat_number_of";
paulson@14387
   214
val nat_numeral_0_eq_0 = thm"nat_numeral_0_eq_0";
paulson@14387
   215
val nat_numeral_1_eq_1 = thm"nat_numeral_1_eq_1";
paulson@14272
   216
val numeral_1_eq_Suc_0 = thm"numeral_1_eq_Suc_0";
paulson@14272
   217
val numeral_2_eq_2 = thm"numeral_2_eq_2";
paulson@14272
   218
val nat_div_distrib = thm"nat_div_distrib";
paulson@14272
   219
val nat_mod_distrib = thm"nat_mod_distrib";
paulson@14272
   220
val int_nat_number_of = thm"int_nat_number_of";
paulson@14272
   221
val Suc_nat_eq_nat_zadd1 = thm"Suc_nat_eq_nat_zadd1";
paulson@14272
   222
val Suc_nat_number_of_add = thm"Suc_nat_number_of_add";
paulson@14272
   223
val Suc_nat_number_of = thm"Suc_nat_number_of";
paulson@14272
   224
val add_nat_number_of = thm"add_nat_number_of";
paulson@14272
   225
val diff_nat_eq_if = thm"diff_nat_eq_if";
paulson@14272
   226
val diff_nat_number_of = thm"diff_nat_number_of";
paulson@14272
   227
val mult_nat_number_of = thm"mult_nat_number_of";
paulson@14272
   228
val div_nat_number_of = thm"div_nat_number_of";
paulson@14272
   229
val mod_nat_number_of = thm"mod_nat_number_of";
paulson@14272
   230
*}
paulson@14272
   231
paulson@14272
   232
paulson@14390
   233
subsection{*Comparisons*}
paulson@14272
   234
paulson@14390
   235
subsubsection{*Equals (=) *}
paulson@14272
   236
paulson@14273
   237
lemma eq_nat_nat_iff:
paulson@14273
   238
     "[| (0::int) <= z;  0 <= z' |] ==> (nat z = nat z') = (z=z')"
paulson@14273
   239
by (auto elim!: nonneg_eq_int)
paulson@14272
   240
paulson@14272
   241
(*"neg" is used in rewrite rules for binary comparisons*)
paulson@14390
   242
lemma eq_nat_number_of [simp]:
paulson@14273
   243
     "((number_of v :: nat) = number_of v') =  
paulson@14378
   244
      (if neg (number_of v :: int) then (iszero (number_of v' :: int) | neg (number_of v' :: int))  
paulson@14378
   245
       else if neg (number_of v' :: int) then iszero (number_of v :: int)  
haftmann@20485
   246
       else iszero (number_of (v + uminus v') :: int))"
paulson@14272
   247
apply (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def
paulson@14272
   248
                  eq_nat_nat_iff eq_number_of_eq nat_0 iszero_def
paulson@14273
   249
            split add: split_if cong add: imp_cong)
paulson@14272
   250
apply (simp only: nat_eq_iff nat_eq_iff2)
paulson@14272
   251
apply (simp add: not_neg_eq_ge_0 [symmetric])
paulson@14272
   252
done
paulson@14272
   253
paulson@14272
   254
paulson@14390
   255
subsubsection{*Less-than (<) *}
paulson@14272
   256
paulson@14272
   257
(*"neg" is used in rewrite rules for binary comparisons*)
paulson@14390
   258
lemma less_nat_number_of [simp]:
paulson@14273
   259
     "((number_of v :: nat) < number_of v') =  
haftmann@20485
   260
         (if neg (number_of v :: int) then neg (number_of (uminus v') :: int)  
haftmann@20485
   261
          else neg (number_of (v + uminus v') :: int))"
paulson@14390
   262
by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def
paulson@14272
   263
                nat_less_eq_zless less_number_of_eq_neg zless_nat_eq_int_zless
haftmann@20485
   264
         cong add: imp_cong, simp add: Pls_def)
paulson@14272
   265
paulson@14272
   266
paulson@14272
   267
(*Maps #n to n for n = 0, 1, 2*)
paulson@14387
   268
lemmas numerals = nat_numeral_0_eq_0 nat_numeral_1_eq_1 numeral_2_eq_2
paulson@14272
   269
paulson@14272
   270
paulson@15234
   271
subsection{*Powers with Numeric Exponents*}
paulson@14353
   272
paulson@14353
   273
text{*We cannot refer to the number @{term 2} in @{text Ring_and_Field.thy}.
paulson@14353
   274
We cannot prove general results about the numeral @{term "-1"}, so we have to
paulson@14353
   275
use @{term "- 1"} instead.*}
paulson@14353
   276
paulson@15003
   277
lemma power2_eq_square: "(a::'a::{comm_semiring_1_cancel,recpower})\<twosuperior> = a * a"
paulson@14353
   278
  by (simp add: numeral_2_eq_2 Power.power_Suc)
paulson@14353
   279
paulson@17724
   280
lemma zero_power2 [simp]: "(0::'a::{comm_semiring_1_cancel,recpower})\<twosuperior> = 0"
paulson@14353
   281
  by (simp add: power2_eq_square)
paulson@14353
   282
paulson@17724
   283
lemma one_power2 [simp]: "(1::'a::{comm_semiring_1_cancel,recpower})\<twosuperior> = 1"
paulson@14353
   284
  by (simp add: power2_eq_square)
paulson@14353
   285
avigad@16775
   286
lemma power3_eq_cube: "(x::'a::recpower) ^ 3 = x * x * x"
avigad@16775
   287
  apply (subgoal_tac "3 = Suc (Suc (Suc 0))")
avigad@16775
   288
  apply (erule ssubst)
avigad@16775
   289
  apply (simp add: power_Suc mult_ac)
avigad@16775
   290
  apply (unfold nat_number_of_def)
avigad@16775
   291
  apply (subst nat_eq_iff)
avigad@16775
   292
  apply simp
avigad@16775
   293
done
avigad@16775
   294
paulson@14353
   295
text{*Squares of literal numerals will be evaluated.*}
paulson@17085
   296
lemmas power2_eq_square_number_of =
paulson@17085
   297
    power2_eq_square [of "number_of w", standard]
paulson@17085
   298
declare power2_eq_square_number_of [simp]
paulson@17085
   299
paulson@14353
   300
obua@23095
   301
lemma zero_le_power2[simp]: "0 \<le> (a\<twosuperior>::'a::{ordered_idom,recpower})"
obua@23095
   302
  by (simp add: power2_eq_square)
paulson@14353
   303
obua@23095
   304
lemma zero_less_power2[simp]:
paulson@15003
   305
     "(0 < a\<twosuperior>) = (a \<noteq> (0::'a::{ordered_idom,recpower}))"
paulson@14353
   306
  by (force simp add: power2_eq_square zero_less_mult_iff linorder_neq_iff)
paulson@14353
   307
obua@23095
   308
lemma power2_less_0[simp]:
paulson@15234
   309
  fixes a :: "'a::{ordered_idom,recpower}"
paulson@15234
   310
  shows "~ (a\<twosuperior> < 0)"
paulson@15234
   311
by (force simp add: power2_eq_square mult_less_0_iff) 
paulson@15234
   312
obua@23095
   313
lemma zero_eq_power2[simp]:
paulson@15003
   314
     "(a\<twosuperior> = 0) = (a = (0::'a::{ordered_idom,recpower}))"
paulson@14353
   315
  by (force simp add: power2_eq_square mult_eq_0_iff)
paulson@14353
   316
obua@23095
   317
lemma abs_power2[simp]:
paulson@15003
   318
     "abs(a\<twosuperior>) = (a\<twosuperior>::'a::{ordered_idom,recpower})"
paulson@14353
   319
  by (simp add: power2_eq_square abs_mult abs_mult_self)
paulson@14353
   320
obua@23095
   321
lemma power2_abs[simp]:
paulson@15003
   322
     "(abs a)\<twosuperior> = (a\<twosuperior>::'a::{ordered_idom,recpower})"
paulson@14353
   323
  by (simp add: power2_eq_square abs_mult_self)
paulson@14353
   324
obua@23095
   325
lemma power2_minus[simp]:
paulson@15003
   326
     "(- a)\<twosuperior> = (a\<twosuperior>::'a::{comm_ring_1,recpower})"
paulson@14353
   327
  by (simp add: power2_eq_square)
paulson@14353
   328
huffman@22854
   329
lemma power2_le_imp_le:
huffman@22854
   330
  fixes x y :: "'a::{ordered_semidom,recpower}"
huffman@22854
   331
  shows "\<lbrakk>x\<twosuperior> \<le> y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x \<le> y"
huffman@22854
   332
unfolding numeral_2_eq_2 by (rule power_le_imp_le_base)
huffman@22854
   333
huffman@22854
   334
lemma power2_less_imp_less:
huffman@22854
   335
  fixes x y :: "'a::{ordered_semidom,recpower}"
huffman@22854
   336
  shows "\<lbrakk>x\<twosuperior> < y\<twosuperior>; 0 \<le> y\<rbrakk> \<Longrightarrow> x < y"
huffman@22854
   337
by (rule power_less_imp_less_base)
huffman@22854
   338
huffman@23051
   339
lemma power2_eq_imp_eq:
huffman@23051
   340
  fixes x y :: "'a::{ordered_semidom,recpower}"
huffman@23051
   341
  shows "\<lbrakk>x\<twosuperior> = y\<twosuperior>; 0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> x = y"
huffman@23051
   342
unfolding numeral_2_eq_2 by (erule (2) power_eq_imp_eq_base, simp)
huffman@23051
   343
obua@23095
   344
lemma power_minus1_even[simp]: "(- 1) ^ (2*n) = (1::'a::{comm_ring_1,recpower})"
paulson@15251
   345
apply (induct "n")
nipkow@23096
   346
apply (auto simp add: power_Suc power_add)
paulson@14353
   347
done
paulson@14353
   348
paulson@15003
   349
lemma power_even_eq: "(a::'a::recpower) ^ (2*n) = (a^n)^2"
krauss@21199
   350
by (subst mult_commute) (simp add: power_mult)
paulson@14443
   351
paulson@14443
   352
lemma power_odd_eq: "(a::int) ^ Suc(2*n) = a * (a^n)^2"
paulson@14443
   353
by (simp add: power_even_eq) 
paulson@14443
   354
paulson@14353
   355
lemma power_minus_even [simp]:
paulson@15003
   356
     "(-a) ^ (2*n) = (a::'a::{comm_ring_1,recpower}) ^ (2*n)"
paulson@14353
   357
by (simp add: power_minus1_even power_minus [of a]) 
paulson@14353
   358
obua@23095
   359
lemma zero_le_even_power'[simp]:
paulson@15003
   360
     "0 \<le> (a::'a::{ordered_idom,recpower}) ^ (2*n)"
paulson@14353
   361
proof (induct "n")
paulson@14353
   362
  case 0
paulson@14353
   363
    show ?case by (simp add: zero_le_one)
paulson@14353
   364
next
paulson@14353
   365
  case (Suc n)
paulson@14353
   366
    have "a ^ (2 * Suc n) = (a*a) * a ^ (2*n)" 
paulson@14353
   367
      by (simp add: mult_ac power_add power2_eq_square)
paulson@14353
   368
    thus ?case
nipkow@23096
   369
      by (simp add: prems zero_le_mult_iff)
paulson@14353
   370
qed
paulson@14353
   371
paulson@14353
   372
lemma odd_power_less_zero:
paulson@15003
   373
     "(a::'a::{ordered_idom,recpower}) < 0 ==> a ^ Suc(2*n) < 0"
paulson@14353
   374
proof (induct "n")
paulson@14353
   375
  case 0
paulson@14353
   376
    show ?case by (simp add: Power.power_Suc)
paulson@14353
   377
next
paulson@14353
   378
  case (Suc n)
paulson@14353
   379
    have "a ^ Suc (2 * Suc n) = (a*a) * a ^ Suc(2*n)" 
paulson@14353
   380
      by (simp add: mult_ac power_add power2_eq_square Power.power_Suc)
paulson@14353
   381
    thus ?case
avigad@16775
   382
      by (simp add: prems mult_less_0_iff mult_neg_neg)
paulson@14353
   383
qed
paulson@14353
   384
paulson@14353
   385
lemma odd_0_le_power_imp_0_le:
paulson@15003
   386
     "0 \<le> a  ^ Suc(2*n) ==> 0 \<le> (a::'a::{ordered_idom,recpower})"
paulson@14353
   387
apply (insert odd_power_less_zero [of a n]) 
paulson@14353
   388
apply (force simp add: linorder_not_less [symmetric]) 
paulson@14353
   389
done
paulson@14353
   390
paulson@15234
   391
text{*Simprules for comparisons where common factors can be cancelled.*}
paulson@15234
   392
lemmas zero_compare_simps =
paulson@15234
   393
    add_strict_increasing add_strict_increasing2 add_increasing
paulson@15234
   394
    zero_le_mult_iff zero_le_divide_iff 
paulson@15234
   395
    zero_less_mult_iff zero_less_divide_iff 
paulson@15234
   396
    mult_le_0_iff divide_le_0_iff 
paulson@15234
   397
    mult_less_0_iff divide_less_0_iff 
paulson@15234
   398
    zero_le_power2 power2_less_0
paulson@14353
   399
paulson@14390
   400
subsubsection{*Nat *}
paulson@14272
   401
paulson@14272
   402
lemma Suc_pred': "0 < n ==> n = Suc(n - 1)"
paulson@14273
   403
by (simp add: numerals)
paulson@14272
   404
paulson@14272
   405
(*Expresses a natural number constant as the Suc of another one.
paulson@14272
   406
  NOT suitable for rewriting because n recurs in the condition.*)
paulson@14272
   407
lemmas expand_Suc = Suc_pred' [of "number_of v", standard]
paulson@14272
   408
paulson@14390
   409
subsubsection{*Arith *}
paulson@14272
   410
paulson@14272
   411
lemma Suc_eq_add_numeral_1: "Suc n = n + 1"
paulson@14273
   412
by (simp add: numerals)
paulson@14272
   413
paulson@14467
   414
lemma Suc_eq_add_numeral_1_left: "Suc n = 1 + n"
paulson@14467
   415
by (simp add: numerals)
paulson@14467
   416
paulson@14272
   417
(* These two can be useful when m = number_of... *)
paulson@14272
   418
paulson@14272
   419
lemma add_eq_if: "(m::nat) + n = (if m=0 then n else Suc ((m - 1) + n))"
paulson@14272
   420
apply (case_tac "m")
paulson@14272
   421
apply (simp_all add: numerals)
paulson@14272
   422
done
paulson@14272
   423
paulson@14272
   424
lemma mult_eq_if: "(m::nat) * n = (if m=0 then 0 else n + ((m - 1) * n))"
paulson@14272
   425
apply (case_tac "m")
paulson@14272
   426
apply (simp_all add: numerals)
paulson@14272
   427
done
paulson@14272
   428
paulson@14272
   429
lemma power_eq_if: "(p ^ m :: nat) = (if m=0 then 1 else p * (p ^ (m - 1)))"
paulson@14272
   430
apply (case_tac "m")
paulson@14272
   431
apply (simp_all add: numerals)
paulson@14272
   432
done
paulson@14272
   433
paulson@14272
   434
paulson@14390
   435
subsection{*Comparisons involving (0::nat) *}
paulson@14272
   436
paulson@14390
   437
text{*Simplification already does @{term "n<0"}, @{term "n\<le>0"} and @{term "0\<le>n"}.*}
paulson@14390
   438
paulson@14390
   439
lemma eq_number_of_0 [simp]:
paulson@14273
   440
     "(number_of v = (0::nat)) =  
paulson@14378
   441
      (if neg (number_of v :: int) then True else iszero (number_of v :: int))"
paulson@14390
   442
by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0)
paulson@14272
   443
paulson@14390
   444
lemma eq_0_number_of [simp]:
paulson@14273
   445
     "((0::nat) = number_of v) =  
paulson@14378
   446
      (if neg (number_of v :: int) then True else iszero (number_of v :: int))"
paulson@14390
   447
by (rule trans [OF eq_sym_conv eq_number_of_0])
paulson@14272
   448
paulson@14390
   449
lemma less_0_number_of [simp]:
haftmann@20485
   450
     "((0::nat) < number_of v) = neg (number_of (uminus v) :: int)"
haftmann@20485
   451
by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] Pls_def)
paulson@14272
   452
paulson@14272
   453
paulson@14378
   454
lemma neg_imp_number_of_eq_0: "neg (number_of v :: int) ==> number_of v = (0::nat)"
paulson@14387
   455
by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric] iszero_0)
paulson@14272
   456
paulson@14272
   457
paulson@14272
   458
paulson@22190
   459
subsection{*Comparisons involving  @{term Suc} *}
paulson@14272
   460
paulson@14273
   461
lemma eq_number_of_Suc [simp]:
paulson@14273
   462
     "(number_of v = Suc n) =  
haftmann@20500
   463
        (let pv = number_of (Numeral.pred v) in  
paulson@14272
   464
         if neg pv then False else nat pv = n)"
paulson@14272
   465
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
paulson@14272
   466
                  number_of_pred nat_number_of_def 
paulson@14273
   467
            split add: split_if)
paulson@14272
   468
apply (rule_tac x = "number_of v" in spec)
paulson@14272
   469
apply (auto simp add: nat_eq_iff)
paulson@14272
   470
done
paulson@14272
   471
paulson@14273
   472
lemma Suc_eq_number_of [simp]:
paulson@14273
   473
     "(Suc n = number_of v) =  
haftmann@20500
   474
        (let pv = number_of (Numeral.pred v) in  
paulson@14272
   475
         if neg pv then False else nat pv = n)"
paulson@14390
   476
by (rule trans [OF eq_sym_conv eq_number_of_Suc])
paulson@14272
   477
paulson@14273
   478
lemma less_number_of_Suc [simp]:
paulson@14273
   479
     "(number_of v < Suc n) =  
haftmann@20500
   480
        (let pv = number_of (Numeral.pred v) in  
paulson@14272
   481
         if neg pv then True else nat pv < n)"
paulson@14272
   482
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
paulson@14272
   483
                  number_of_pred nat_number_of_def  
paulson@14273
   484
            split add: split_if)
paulson@14272
   485
apply (rule_tac x = "number_of v" in spec)
paulson@14272
   486
apply (auto simp add: nat_less_iff)
paulson@14272
   487
done
paulson@14272
   488
paulson@14273
   489
lemma less_Suc_number_of [simp]:
paulson@14273
   490
     "(Suc n < number_of v) =  
haftmann@20500
   491
        (let pv = number_of (Numeral.pred v) in  
paulson@14272
   492
         if neg pv then False else n < nat pv)"
paulson@14272
   493
apply (simp only: simp_thms Let_def neg_eq_less_0 linorder_not_less 
paulson@14272
   494
                  number_of_pred nat_number_of_def
paulson@14273
   495
            split add: split_if)
paulson@14272
   496
apply (rule_tac x = "number_of v" in spec)
paulson@14272
   497
apply (auto simp add: zless_nat_eq_int_zless)
paulson@14272
   498
done
paulson@14272
   499
paulson@14273
   500
lemma le_number_of_Suc [simp]:
paulson@14273
   501
     "(number_of v <= Suc n) =  
haftmann@20500
   502
        (let pv = number_of (Numeral.pred v) in  
paulson@14272
   503
         if neg pv then True else nat pv <= n)"
paulson@14390
   504
by (simp add: Let_def less_Suc_number_of linorder_not_less [symmetric])
paulson@14272
   505
paulson@14273
   506
lemma le_Suc_number_of [simp]:
paulson@14273
   507
     "(Suc n <= number_of v) =  
haftmann@20500
   508
        (let pv = number_of (Numeral.pred v) in  
paulson@14272
   509
         if neg pv then False else n <= nat pv)"
paulson@14390
   510
by (simp add: Let_def less_number_of_Suc linorder_not_less [symmetric])
paulson@14272
   511
paulson@14272
   512
paulson@14272
   513
(* Push int(.) inwards: *)
paulson@14272
   514
declare zadd_int [symmetric, simp]
paulson@14272
   515
paulson@14272
   516
lemma lemma1: "(m+m = n+n) = (m = (n::int))"
paulson@14273
   517
by auto
paulson@14272
   518
paulson@14272
   519
lemma lemma2: "m+m ~= (1::int) + (n + n)"
paulson@14272
   520
apply auto
paulson@14272
   521
apply (drule_tac f = "%x. x mod 2" in arg_cong)
paulson@14273
   522
apply (simp add: zmod_zadd1_eq)
paulson@14272
   523
done
paulson@14272
   524
paulson@14273
   525
lemma eq_number_of_BIT_BIT:
paulson@14273
   526
     "((number_of (v BIT x) ::int) = number_of (w BIT y)) =  
paulson@14272
   527
      (x=y & (((number_of v) ::int) = number_of w))"
paulson@15620
   528
apply (simp only: number_of_BIT lemma1 lemma2 eq_commute
nipkow@23085
   529
               OrderedGroup.add_left_cancel add_assoc OrderedGroup.add_0_left
nipkow@23085
   530
            split add: bit.split)
paulson@15620
   531
apply simp
paulson@15620
   532
done
paulson@14272
   533
paulson@14273
   534
lemma eq_number_of_BIT_Pls:
paulson@15013
   535
     "((number_of (v BIT x) ::int) = Numeral0) =  
paulson@15620
   536
      (x=bit.B0 & (((number_of v) ::int) = Numeral0))"
paulson@14272
   537
apply (simp only: simp_thms  add: number_of_BIT number_of_Pls eq_commute
paulson@15620
   538
            split add: bit.split cong: imp_cong)
paulson@14273
   539
apply (rule_tac x = "number_of v" in spec, safe)
paulson@14272
   540
apply (simp_all (no_asm_use))
paulson@14272
   541
apply (drule_tac f = "%x. x mod 2" in arg_cong)
paulson@14273
   542
apply (simp add: zmod_zadd1_eq)
paulson@14272
   543
done
paulson@14272
   544
paulson@14273
   545
lemma eq_number_of_BIT_Min:
paulson@15013
   546
     "((number_of (v BIT x) ::int) = number_of Numeral.Min) =  
paulson@15620
   547
      (x=bit.B1 & (((number_of v) ::int) = number_of Numeral.Min))"
paulson@14272
   548
apply (simp only: simp_thms  add: number_of_BIT number_of_Min eq_commute
paulson@15620
   549
            split add: bit.split cong: imp_cong)
paulson@14273
   550
apply (rule_tac x = "number_of v" in spec, auto)
paulson@14273
   551
apply (drule_tac f = "%x. x mod 2" in arg_cong, auto)
paulson@14272
   552
done
paulson@14272
   553
paulson@15013
   554
lemma eq_number_of_Pls_Min: "(Numeral0 ::int) ~= number_of Numeral.Min"
paulson@14273
   555
by auto
paulson@14272
   556
paulson@14272
   557
paulson@14272
   558
paulson@22190
   559
subsection{*Max and Min Combined with @{term Suc} *}
paulson@22190
   560
paulson@22190
   561
lemma max_number_of_Suc [simp]:
paulson@22190
   562
     "max (Suc n) (number_of v) =  
paulson@22190
   563
        (let pv = number_of (Numeral.pred v) in  
paulson@22190
   564
         if neg pv then Suc n else Suc(max n (nat pv)))"
paulson@22190
   565
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
paulson@22190
   566
            split add: split_if nat.split)
paulson@22190
   567
apply (rule_tac x = "number_of v" in spec) 
paulson@22190
   568
apply auto
paulson@22190
   569
done
paulson@22190
   570
 
paulson@22190
   571
lemma max_Suc_number_of [simp]:
paulson@22190
   572
     "max (number_of v) (Suc n) =  
paulson@22190
   573
        (let pv = number_of (Numeral.pred v) in  
paulson@22190
   574
         if neg pv then Suc n else Suc(max (nat pv) n))"
paulson@22190
   575
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
paulson@22190
   576
            split add: split_if nat.split)
paulson@22190
   577
apply (rule_tac x = "number_of v" in spec) 
paulson@22190
   578
apply auto
paulson@22190
   579
done
paulson@22190
   580
 
paulson@22190
   581
lemma min_number_of_Suc [simp]:
paulson@22190
   582
     "min (Suc n) (number_of v) =  
paulson@22190
   583
        (let pv = number_of (Numeral.pred v) in  
paulson@22190
   584
         if neg pv then 0 else Suc(min n (nat pv)))"
paulson@22190
   585
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
paulson@22190
   586
            split add: split_if nat.split)
paulson@22190
   587
apply (rule_tac x = "number_of v" in spec) 
paulson@22190
   588
apply auto
paulson@22190
   589
done
paulson@22190
   590
 
paulson@22190
   591
lemma min_Suc_number_of [simp]:
paulson@22190
   592
     "min (number_of v) (Suc n) =  
paulson@22190
   593
        (let pv = number_of (Numeral.pred v) in  
paulson@22190
   594
         if neg pv then 0 else Suc(min (nat pv) n))"
paulson@22190
   595
apply (simp only: Let_def neg_eq_less_0 number_of_pred nat_number_of_def 
paulson@22190
   596
            split add: split_if nat.split)
paulson@22190
   597
apply (rule_tac x = "number_of v" in spec) 
paulson@22190
   598
apply auto
paulson@22190
   599
done
paulson@22190
   600
 
paulson@14390
   601
subsection{*Literal arithmetic involving powers*}
paulson@14272
   602
paulson@14272
   603
lemma nat_power_eq: "(0::int) <= z ==> nat (z^n) = nat z ^ n"
paulson@15251
   604
apply (induct "n")
paulson@14272
   605
apply (simp_all (no_asm_simp) add: nat_mult_distrib)
paulson@14272
   606
done
paulson@14272
   607
paulson@14273
   608
lemma power_nat_number_of:
paulson@14273
   609
     "(number_of v :: nat) ^ n =  
paulson@14378
   610
       (if neg (number_of v :: int) then 0^n else nat ((number_of v :: int) ^ n))"
paulson@14272
   611
by (simp only: simp_thms neg_nat not_neg_eq_ge_0 nat_number_of_def nat_power_eq
paulson@14272
   612
         split add: split_if cong: imp_cong)
paulson@14272
   613
paulson@14272
   614
paulson@17085
   615
lemmas power_nat_number_of_number_of = power_nat_number_of [of _ "number_of w", standard]
paulson@17085
   616
declare power_nat_number_of_number_of [simp]
paulson@17085
   617
paulson@14272
   618
paulson@14272
   619
paulson@14390
   620
text{*For the integers*}
paulson@14272
   621
paulson@14273
   622
lemma zpower_number_of_even:
haftmann@20485
   623
  "(z::int) ^ number_of (w BIT bit.B0) = (let w = z ^ (number_of w) in w * w)"
haftmann@20485
   624
unfolding Let_def nat_number_of_def number_of_BIT bit.cases
paulson@14273
   625
apply (rule_tac x = "number_of w" in spec, clarify)
paulson@14272
   626
apply (case_tac " (0::int) <= x")
paulson@14443
   627
apply (auto simp add: nat_mult_distrib power_even_eq power2_eq_square)
paulson@14272
   628
done
paulson@14272
   629
paulson@14273
   630
lemma zpower_number_of_odd:
haftmann@20485
   631
  "(z::int) ^ number_of (w BIT bit.B1) = (if (0::int) <= number_of w                    
haftmann@20485
   632
     then (let w = z ^ (number_of w) in z * w * w) else 1)"
haftmann@20485
   633
unfolding Let_def nat_number_of_def number_of_BIT bit.cases
haftmann@20485
   634
apply (rule_tac x = "number_of w" in spec, auto)
haftmann@20485
   635
apply (simp only: nat_add_distrib nat_mult_distrib)
haftmann@20485
   636
apply simp
paulson@14443
   637
apply (auto simp add: nat_add_distrib nat_mult_distrib power_even_eq power2_eq_square neg_nat)
paulson@14272
   638
done
paulson@14272
   639
paulson@17085
   640
lemmas zpower_number_of_even_number_of =
paulson@17085
   641
    zpower_number_of_even [of "number_of v", standard]
paulson@17085
   642
declare zpower_number_of_even_number_of [simp]
paulson@17085
   643
paulson@17085
   644
lemmas zpower_number_of_odd_number_of =
paulson@17085
   645
    zpower_number_of_odd [of "number_of v", standard]
paulson@17085
   646
declare zpower_number_of_odd_number_of [simp]
paulson@17085
   647
paulson@14272
   648
paulson@14272
   649
paulson@14272
   650
paulson@14272
   651
ML
paulson@14272
   652
{*
paulson@14272
   653
val numerals = thms"numerals";
paulson@14272
   654
val numeral_ss = simpset() addsimps numerals;
paulson@14272
   655
paulson@14272
   656
val nat_bin_arith_setup =
wenzelm@18708
   657
 Fast_Arith.map_data
nipkow@15921
   658
   (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, simpset} =>
paulson@14272
   659
     {add_mono_thms = add_mono_thms, mult_mono_thms = mult_mono_thms,
paulson@14272
   660
      inj_thms = inj_thms,
nipkow@15921
   661
      lessD = lessD, neqE = neqE,
paulson@14272
   662
      simpset = simpset addsimps [Suc_nat_number_of, int_nat_number_of,
paulson@14272
   663
                                  not_neg_number_of_Pls,
wenzelm@18708
   664
                                  neg_number_of_Min,neg_number_of_BIT]})
paulson@14272
   665
*}
paulson@14272
   666
wenzelm@12838
   667
setup nat_bin_arith_setup
wenzelm@12838
   668
nipkow@13189
   669
(* Enable arith to deal with div/mod k where k is a numeral: *)
nipkow@13189
   670
declare split_div[of _ _ "number_of k", standard, arith_split]
nipkow@13189
   671
declare split_mod[of _ _ "number_of k", standard, arith_split]
nipkow@13154
   672
paulson@15013
   673
lemma nat_number_of_Pls: "Numeral0 = (0::nat)"
wenzelm@12838
   674
  by (simp add: number_of_Pls nat_number_of_def)
wenzelm@12838
   675
paulson@15013
   676
lemma nat_number_of_Min: "number_of Numeral.Min = (0::nat)"
wenzelm@12838
   677
  apply (simp only: number_of_Min nat_number_of_def nat_zminus_int)
wenzelm@12838
   678
  done
paulson@7032
   679
paulson@15620
   680
lemma nat_number_of_BIT_1:
paulson@15620
   681
  "number_of (w BIT bit.B1) =
paulson@14378
   682
    (if neg (number_of w :: int) then 0
wenzelm@12838
   683
     else let n = number_of w in Suc (n + n))"
wenzelm@12838
   684
  apply (simp only: nat_number_of_def Let_def split: split_if)
wenzelm@12838
   685
  apply (intro conjI impI)
wenzelm@12838
   686
   apply (simp add: neg_nat neg_number_of_BIT)
wenzelm@12838
   687
  apply (rule int_int_eq [THEN iffD1])
wenzelm@12838
   688
  apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms)
paulson@15620
   689
  apply (simp only: number_of_BIT zadd_assoc split: bit.split)
paulson@15620
   690
  apply simp
wenzelm@12838
   691
  done
paulson@7032
   692
paulson@15620
   693
lemma nat_number_of_BIT_0:
paulson@15620
   694
    "number_of (w BIT bit.B0) = (let n::nat = number_of w in n + n)"
wenzelm@12838
   695
  apply (simp only: nat_number_of_def Let_def)
paulson@14378
   696
  apply (cases "neg (number_of w :: int)")
wenzelm@12838
   697
   apply (simp add: neg_nat neg_number_of_BIT)
wenzelm@12838
   698
  apply (rule int_int_eq [THEN iffD1])
wenzelm@12838
   699
  apply (simp only: not_neg_nat neg_number_of_BIT int_Suc zadd_int [symmetric] simp_thms)
paulson@15620
   700
  apply (simp only: number_of_BIT zadd_assoc)
paulson@15620
   701
  apply simp
wenzelm@12838
   702
  done
wenzelm@12838
   703
wenzelm@13043
   704
lemmas nat_number =
wenzelm@12838
   705
  nat_number_of_Pls nat_number_of_Min
paulson@15620
   706
  nat_number_of_BIT_1 nat_number_of_BIT_0
wenzelm@12838
   707
wenzelm@12838
   708
lemma Let_Suc [simp]: "Let (Suc n) f == f (Suc n)"
wenzelm@12838
   709
  by (simp add: Let_def)
nipkow@10574
   710
paulson@15003
   711
lemma power_m1_even: "(-1) ^ (2*n) = (1::'a::{number_ring,recpower})"
paulson@14443
   712
by (simp add: power_mult); 
paulson@14443
   713
paulson@15003
   714
lemma power_m1_odd: "(-1) ^ Suc(2*n) = (-1::'a::{number_ring,recpower})"
paulson@14443
   715
by (simp add: power_mult power_Suc); 
paulson@14443
   716
berghofe@12440
   717
paulson@14390
   718
subsection{*Literal arithmetic and @{term of_nat}*}
paulson@14390
   719
paulson@14390
   720
lemma of_nat_double:
paulson@14390
   721
     "0 \<le> x ==> of_nat (nat (2 * x)) = of_nat (nat x) + of_nat (nat x)"
paulson@14390
   722
by (simp only: mult_2 nat_add_distrib of_nat_add) 
paulson@14390
   723
paulson@14390
   724
lemma nat_numeral_m1_eq_0: "-1 = (0::nat)"
webertj@20217
   725
by (simp only: nat_number_of_def)
paulson@14390
   726
paulson@14390
   727
lemma of_nat_number_of_lemma:
paulson@14390
   728
     "of_nat (number_of v :: nat) =  
paulson@14390
   729
         (if 0 \<le> (number_of v :: int) 
paulson@14390
   730
          then (number_of v :: 'a :: number_ring)
paulson@14390
   731
          else 0)"
paulson@15013
   732
by (simp add: int_number_of_def nat_number_of_def number_of_eq of_nat_nat);
paulson@14390
   733
paulson@14390
   734
lemma of_nat_number_of_eq [simp]:
paulson@14390
   735
     "of_nat (number_of v :: nat) =  
paulson@14390
   736
         (if neg (number_of v :: int) then 0  
paulson@14390
   737
          else (number_of v :: 'a :: number_ring))"
paulson@14390
   738
by (simp only: of_nat_number_of_lemma neg_def, simp) 
paulson@14390
   739
paulson@14390
   740
paulson@14273
   741
subsection {*Lemmas for the Combination and Cancellation Simprocs*}
paulson@14273
   742
paulson@14273
   743
lemma nat_number_of_add_left:
paulson@14273
   744
     "number_of v + (number_of v' + (k::nat)) =  
paulson@14378
   745
         (if neg (number_of v :: int) then number_of v' + k  
paulson@14378
   746
          else if neg (number_of v' :: int) then number_of v + k  
haftmann@20485
   747
          else number_of (v + v') + k)"
paulson@14390
   748
by simp
paulson@14273
   749
paulson@14430
   750
lemma nat_number_of_mult_left:
paulson@14430
   751
     "number_of v * (number_of v' * (k::nat)) =  
paulson@14430
   752
         (if neg (number_of v :: int) then 0
haftmann@20485
   753
          else number_of (v * v') * k)"
paulson@14430
   754
by simp
paulson@14430
   755
paulson@14273
   756
paulson@14390
   757
subsubsection{*For @{text combine_numerals}*}
paulson@14273
   758
paulson@14273
   759
lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
paulson@14273
   760
by (simp add: add_mult_distrib)
paulson@14273
   761
paulson@14273
   762
paulson@14390
   763
subsubsection{*For @{text cancel_numerals}*}
paulson@14273
   764
paulson@14273
   765
lemma nat_diff_add_eq1:
paulson@14273
   766
     "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
paulson@14273
   767
by (simp split add: nat_diff_split add: add_mult_distrib)
paulson@14273
   768
paulson@14273
   769
lemma nat_diff_add_eq2:
paulson@14273
   770
     "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
paulson@14273
   771
by (simp split add: nat_diff_split add: add_mult_distrib)
paulson@14273
   772
paulson@14273
   773
lemma nat_eq_add_iff1:
paulson@14273
   774
     "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
paulson@14273
   775
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   776
paulson@14273
   777
lemma nat_eq_add_iff2:
paulson@14273
   778
     "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
paulson@14273
   779
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   780
paulson@14273
   781
lemma nat_less_add_iff1:
paulson@14273
   782
     "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
paulson@14273
   783
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   784
paulson@14273
   785
lemma nat_less_add_iff2:
paulson@14273
   786
     "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
paulson@14273
   787
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   788
paulson@14273
   789
lemma nat_le_add_iff1:
paulson@14273
   790
     "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
paulson@14273
   791
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   792
paulson@14273
   793
lemma nat_le_add_iff2:
paulson@14273
   794
     "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
paulson@14273
   795
by (auto split add: nat_diff_split simp add: add_mult_distrib)
paulson@14273
   796
paulson@14273
   797
paulson@14390
   798
subsubsection{*For @{text cancel_numeral_factors} *}
paulson@14273
   799
paulson@14273
   800
lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
paulson@14273
   801
by auto
paulson@14273
   802
paulson@14273
   803
lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
paulson@14273
   804
by auto
paulson@14273
   805
paulson@14273
   806
lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
paulson@14273
   807
by auto
paulson@14273
   808
paulson@14273
   809
lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
paulson@14273
   810
by auto
paulson@14273
   811
paulson@14273
   812
paulson@14390
   813
subsubsection{*For @{text cancel_factor} *}
paulson@14273
   814
paulson@14273
   815
lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
paulson@14273
   816
by auto
paulson@14273
   817
paulson@14273
   818
lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
paulson@14273
   819
by auto
paulson@14273
   820
paulson@14273
   821
lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
paulson@14273
   822
by auto
paulson@14273
   823
paulson@14273
   824
lemma nat_mult_div_cancel_disj:
paulson@14273
   825
     "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
paulson@14273
   826
by (simp add: nat_mult_div_cancel1)
paulson@14273
   827
haftmann@20355
   828
haftmann@19601
   829
subsection {* legacy ML bindings *}
haftmann@19601
   830
paulson@14273
   831
ML
paulson@14273
   832
{*
paulson@14353
   833
val eq_nat_nat_iff = thm"eq_nat_nat_iff";
paulson@14353
   834
val eq_nat_number_of = thm"eq_nat_number_of";
paulson@14353
   835
val less_nat_number_of = thm"less_nat_number_of";
paulson@14353
   836
val power2_eq_square = thm "power2_eq_square";
paulson@14353
   837
val zero_le_power2 = thm "zero_le_power2";
paulson@14353
   838
val zero_less_power2 = thm "zero_less_power2";
paulson@14353
   839
val zero_eq_power2 = thm "zero_eq_power2";
paulson@14353
   840
val abs_power2 = thm "abs_power2";
paulson@14353
   841
val power2_abs = thm "power2_abs";
paulson@14353
   842
val power2_minus = thm "power2_minus";
paulson@14353
   843
val power_minus1_even = thm "power_minus1_even";
paulson@14353
   844
val power_minus_even = thm "power_minus_even";
paulson@14353
   845
val odd_power_less_zero = thm "odd_power_less_zero";
paulson@14353
   846
val odd_0_le_power_imp_0_le = thm "odd_0_le_power_imp_0_le";
paulson@14353
   847
paulson@14353
   848
val Suc_pred' = thm"Suc_pred'";
paulson@14353
   849
val expand_Suc = thm"expand_Suc";
paulson@14353
   850
val Suc_eq_add_numeral_1 = thm"Suc_eq_add_numeral_1";
paulson@14467
   851
val Suc_eq_add_numeral_1_left = thm"Suc_eq_add_numeral_1_left";
paulson@14353
   852
val add_eq_if = thm"add_eq_if";
paulson@14353
   853
val mult_eq_if = thm"mult_eq_if";
paulson@14353
   854
val power_eq_if = thm"power_eq_if";
paulson@14353
   855
val eq_number_of_0 = thm"eq_number_of_0";
paulson@14353
   856
val eq_0_number_of = thm"eq_0_number_of";
paulson@14353
   857
val less_0_number_of = thm"less_0_number_of";
paulson@14353
   858
val neg_imp_number_of_eq_0 = thm"neg_imp_number_of_eq_0";
paulson@14353
   859
val eq_number_of_Suc = thm"eq_number_of_Suc";
paulson@14353
   860
val Suc_eq_number_of = thm"Suc_eq_number_of";
paulson@14353
   861
val less_number_of_Suc = thm"less_number_of_Suc";
paulson@14353
   862
val less_Suc_number_of = thm"less_Suc_number_of";
paulson@14353
   863
val le_number_of_Suc = thm"le_number_of_Suc";
paulson@14353
   864
val le_Suc_number_of = thm"le_Suc_number_of";
paulson@14353
   865
val eq_number_of_BIT_BIT = thm"eq_number_of_BIT_BIT";
paulson@14353
   866
val eq_number_of_BIT_Pls = thm"eq_number_of_BIT_Pls";
paulson@14353
   867
val eq_number_of_BIT_Min = thm"eq_number_of_BIT_Min";
paulson@14353
   868
val eq_number_of_Pls_Min = thm"eq_number_of_Pls_Min";
paulson@14390
   869
val of_nat_number_of_eq = thm"of_nat_number_of_eq";
paulson@14353
   870
val nat_power_eq = thm"nat_power_eq";
paulson@14353
   871
val power_nat_number_of = thm"power_nat_number_of";
paulson@14353
   872
val zpower_number_of_even = thm"zpower_number_of_even";
paulson@14353
   873
val zpower_number_of_odd = thm"zpower_number_of_odd";
paulson@14353
   874
val nat_number_of_Pls = thm"nat_number_of_Pls";
paulson@14353
   875
val nat_number_of_Min = thm"nat_number_of_Min";
paulson@14353
   876
val Let_Suc = thm"Let_Suc";
paulson@14353
   877
paulson@14353
   878
val nat_number = thms"nat_number";
paulson@14353
   879
paulson@14273
   880
val nat_number_of_add_left = thm"nat_number_of_add_left";
paulson@14430
   881
val nat_number_of_mult_left = thm"nat_number_of_mult_left";
paulson@14273
   882
val left_add_mult_distrib = thm"left_add_mult_distrib";
paulson@14273
   883
val nat_diff_add_eq1 = thm"nat_diff_add_eq1";
paulson@14273
   884
val nat_diff_add_eq2 = thm"nat_diff_add_eq2";
paulson@14273
   885
val nat_eq_add_iff1 = thm"nat_eq_add_iff1";
paulson@14273
   886
val nat_eq_add_iff2 = thm"nat_eq_add_iff2";
paulson@14273
   887
val nat_less_add_iff1 = thm"nat_less_add_iff1";
paulson@14273
   888
val nat_less_add_iff2 = thm"nat_less_add_iff2";
paulson@14273
   889
val nat_le_add_iff1 = thm"nat_le_add_iff1";
paulson@14273
   890
val nat_le_add_iff2 = thm"nat_le_add_iff2";
paulson@14273
   891
val nat_mult_le_cancel1 = thm"nat_mult_le_cancel1";
paulson@14273
   892
val nat_mult_less_cancel1 = thm"nat_mult_less_cancel1";
paulson@14273
   893
val nat_mult_eq_cancel1 = thm"nat_mult_eq_cancel1";
paulson@14273
   894
val nat_mult_div_cancel1 = thm"nat_mult_div_cancel1";
paulson@14273
   895
val nat_mult_le_cancel_disj = thm"nat_mult_le_cancel_disj";
paulson@14273
   896
val nat_mult_less_cancel_disj = thm"nat_mult_less_cancel_disj";
paulson@14273
   897
val nat_mult_eq_cancel_disj = thm"nat_mult_eq_cancel_disj";
paulson@14273
   898
val nat_mult_div_cancel_disj = thm"nat_mult_div_cancel_disj";
paulson@14353
   899
paulson@14353
   900
val power_minus_even = thm"power_minus_even";
paulson@14273
   901
*}
paulson@14273
   902
paulson@7032
   903
end