src/HOL/Tools/ComputeNumeral.thy
author wenzelm
Thu Mar 27 14:41:09 2008 +0100 (2008-03-27)
changeset 26424 a6cad32a27b0
parent 26086 3c243098b64a
child 26512 682dfb674df3
permissions -rw-r--r--
eliminated theory ProtoPure;
obua@23664
     1
theory ComputeNumeral
obua@23664
     2
imports ComputeHOL Float
obua@23664
     3
begin
obua@23664
     4
obua@23664
     5
(* normalization of bit strings *)
huffman@26075
     6
lemmas bitnorm = normalize_bin_simps
obua@23664
     7
obua@23664
     8
(* neg for bit strings *)
haftmann@25919
     9
lemma neg1: "neg Int.Pls = False" by (simp add: Int.Pls_def)
haftmann@25919
    10
lemma neg2: "neg Int.Min = True" apply (subst Int.Min_def) by auto
huffman@26086
    11
lemma neg3: "neg (Int.Bit0 x) = neg x" apply (simp add: neg_def) apply (subst Bit0_def) by auto
huffman@26086
    12
lemma neg4: "neg (Int.Bit1 x) = neg x" apply (simp add: neg_def) apply (subst Bit1_def) by auto  
obua@23664
    13
lemmas bitneg = neg1 neg2 neg3 neg4
obua@23664
    14
obua@23664
    15
(* iszero for bit strings *)
haftmann@25919
    16
lemma iszero1: "iszero Int.Pls = True" by (simp add: Int.Pls_def iszero_def)
haftmann@25919
    17
lemma iszero2: "iszero Int.Min = False" apply (subst Int.Min_def) apply (subst iszero_def) by simp
huffman@26086
    18
lemma iszero3: "iszero (Int.Bit0 x) = iszero x" apply (subst Int.Bit0_def) apply (subst iszero_def)+ by auto
huffman@26086
    19
lemma iszero4: "iszero (Int.Bit1 x) = False" apply (subst Int.Bit1_def) apply (subst iszero_def)+  apply simp by arith
obua@23664
    20
lemmas bitiszero = iszero1 iszero2 iszero3 iszero4
obua@23664
    21
obua@23664
    22
(* lezero for bit strings *)
obua@23664
    23
constdefs
obua@23664
    24
  "lezero x == (x \<le> 0)"
haftmann@25919
    25
lemma lezero1: "lezero Int.Pls = True" unfolding Int.Pls_def lezero_def by auto
haftmann@25919
    26
lemma lezero2: "lezero Int.Min = True" unfolding Int.Min_def lezero_def by auto
huffman@26086
    27
lemma lezero3: "lezero (Int.Bit0 x) = lezero x" unfolding Int.Bit0_def lezero_def by auto
huffman@26086
    28
lemma lezero4: "lezero (Int.Bit1 x) = neg x" unfolding Int.Bit1_def lezero_def neg_def by auto
obua@23664
    29
lemmas bitlezero = lezero1 lezero2 lezero3 lezero4
obua@23664
    30
obua@23664
    31
(* equality for bit strings *)
huffman@26086
    32
lemma biteq1: "(Int.Pls = Int.Pls) = True" by (rule eq_Pls_Pls)
huffman@26086
    33
lemma biteq2: "(Int.Min = Int.Min) = True" by (rule eq_Min_Min)
huffman@26086
    34
lemma biteq3: "(Int.Pls = Int.Min) = False" by (rule eq_Pls_Min)
huffman@26086
    35
lemma biteq4: "(Int.Min = Int.Pls) = False" by (rule eq_Min_Pls)
huffman@26086
    36
lemma biteq5: "(Int.Bit0 x = Int.Bit0 y) = (x = y)" by (rule eq_Bit0_Bit0)
huffman@26086
    37
lemma biteq6: "(Int.Bit1 x = Int.Bit1 y) = (x = y)" by (rule eq_Bit1_Bit1)
huffman@26086
    38
lemma biteq7: "(Int.Bit0 x = Int.Bit1 y) = False" by (rule eq_Bit0_Bit1)
huffman@26086
    39
lemma biteq8: "(Int.Bit1 x = Int.Bit0 y) = False" by (rule eq_Bit1_Bit0)
huffman@26086
    40
lemma biteq9: "(Int.Pls = Int.Bit0 x) = (Int.Pls = x)" by (rule eq_Pls_Bit0)
huffman@26086
    41
lemma biteq10: "(Int.Pls = Int.Bit1 x) = False" by (rule eq_Pls_Bit1)
huffman@26086
    42
lemma biteq11: "(Int.Min = Int.Bit0 x) = False" by (rule eq_Min_Bit0)
huffman@26086
    43
lemma biteq12: "(Int.Min = Int.Bit1 x) = (Int.Min = x)" by (rule eq_Min_Bit1)
huffman@26086
    44
lemma biteq13: "(Int.Bit0 x = Int.Pls) = (x = Int.Pls)" by (subst eq_Bit0_Pls) auto
huffman@26086
    45
lemma biteq14: "(Int.Bit1 x = Int.Pls) = False" by (rule eq_Bit1_Pls)
huffman@26086
    46
lemma biteq15: "(Int.Bit0 x = Int.Min) = False" by (rule eq_Bit0_Min)
huffman@26086
    47
lemma biteq16: "(Int.Bit1 x = Int.Min) = (x = Int.Min)" by (subst eq_Bit1_Min) auto
obua@23664
    48
lemmas biteq = biteq1 biteq2 biteq3 biteq4 biteq5 biteq6 biteq7 biteq8 biteq9 biteq10 biteq11 biteq12 biteq13 biteq14 biteq15 biteq16
obua@23664
    49
obua@23664
    50
(* x < y for bit strings *)
huffman@26086
    51
lemma bitless1: "(Int.Pls < Int.Min) = False" by (rule less_Pls_Min)
huffman@26086
    52
lemma bitless2: "(Int.Pls < Int.Pls) = False" by (rule less_Pls_Pls)
huffman@26086
    53
lemma bitless3: "(Int.Min < Int.Pls) = True" by (rule less_Min_Pls)
huffman@26086
    54
lemma bitless4: "(Int.Min < Int.Min) = False" by (rule less_Min_Min)
huffman@26086
    55
lemma bitless5: "(Int.Bit0 x < Int.Bit0 y) = (x < y)" by (rule less_Bit0_Bit0)
huffman@26086
    56
lemma bitless6: "(Int.Bit1 x < Int.Bit1 y) = (x < y)" by (rule less_Bit1_Bit1)
huffman@26086
    57
lemma bitless7: "(Int.Bit0 x < Int.Bit1 y) = (x \<le> y)" by (rule less_Bit0_Bit1)
huffman@26086
    58
lemma bitless8: "(Int.Bit1 x < Int.Bit0 y) = (x < y)" by (rule less_Bit1_Bit0)
huffman@26086
    59
lemma bitless9: "(Int.Pls < Int.Bit0 x) = (Int.Pls < x)" by (rule less_Pls_Bit0)
huffman@26086
    60
lemma bitless10: "(Int.Pls < Int.Bit1 x) = (Int.Pls \<le> x)" by (rule less_Pls_Bit1)
huffman@26086
    61
lemma bitless11: "(Int.Min < Int.Bit0 x) = (Int.Pls \<le> x)" unfolding Bit0_def Pls_def Min_def by auto
huffman@26086
    62
lemma bitless12: "(Int.Min < Int.Bit1 x) = (Int.Min < x)" by (rule less_Min_Bit1)
huffman@26086
    63
lemma bitless13: "(Int.Bit0 x < Int.Pls) = (x < Int.Pls)" by (rule less_Bit0_Pls)
huffman@26086
    64
lemma bitless14: "(Int.Bit1 x < Int.Pls) = (x < Int.Pls)" by (rule less_Bit1_Pls)
huffman@26086
    65
lemma bitless15: "(Int.Bit0 x < Int.Min) = (x < Int.Pls)" unfolding Bit0_def Pls_def Min_def by auto
huffman@26086
    66
lemma bitless16: "(Int.Bit1 x < Int.Min) = (x < Int.Min)" by (rule less_Bit1_Min)
obua@23664
    67
lemmas bitless = bitless1 bitless2 bitless3 bitless4 bitless5 bitless6 bitless7 bitless8 
obua@23664
    68
                 bitless9 bitless10 bitless11 bitless12 bitless13 bitless14 bitless15 bitless16
obua@23664
    69
obua@23664
    70
(* x \<le> y for bit strings *)
huffman@26086
    71
lemma bitle1: "(Int.Pls \<le> Int.Min) = False" by (rule less_eq_Pls_Min)
huffman@26086
    72
lemma bitle2: "(Int.Pls \<le> Int.Pls) = True" by (rule less_eq_Pls_Pls)
huffman@26086
    73
lemma bitle3: "(Int.Min \<le> Int.Pls) = True" by (rule less_eq_Min_Pls)
huffman@26086
    74
lemma bitle4: "(Int.Min \<le> Int.Min) = True" by (rule less_eq_Min_Min)
huffman@26086
    75
lemma bitle5: "(Int.Bit0 x \<le> Int.Bit0 y) = (x \<le> y)" by (rule less_eq_Bit0_Bit0)
huffman@26086
    76
lemma bitle6: "(Int.Bit1 x \<le> Int.Bit1 y) = (x \<le> y)" by (rule less_eq_Bit1_Bit1)
huffman@26086
    77
lemma bitle7: "(Int.Bit0 x \<le> Int.Bit1 y) = (x \<le> y)" by (rule less_eq_Bit0_Bit1)
huffman@26086
    78
lemma bitle8: "(Int.Bit1 x \<le> Int.Bit0 y) = (x < y)" by (rule less_eq_Bit1_Bit0)
huffman@26086
    79
lemma bitle9: "(Int.Pls \<le> Int.Bit0 x) = (Int.Pls \<le> x)" by (rule less_eq_Pls_Bit0)
huffman@26086
    80
lemma bitle10: "(Int.Pls \<le> Int.Bit1 x) = (Int.Pls \<le> x)" by (rule less_eq_Pls_Bit1)
huffman@26086
    81
lemma bitle11: "(Int.Min \<le> Int.Bit0 x) = (Int.Pls \<le> x)" unfolding Bit0_def Pls_def Min_def by auto
huffman@26086
    82
lemma bitle12: "(Int.Min \<le> Int.Bit1 x) = (Int.Min \<le> x)" by (rule less_eq_Min_Bit1)
huffman@26086
    83
lemma bitle13: "(Int.Bit0 x \<le> Int.Pls) = (x \<le> Int.Pls)" by (rule less_eq_Bit0_Pls)
huffman@26086
    84
lemma bitle14: "(Int.Bit1 x \<le> Int.Pls) = (x < Int.Pls)" by (rule less_eq_Bit1_Pls)
huffman@26086
    85
lemma bitle15: "(Int.Bit0 x \<le> Int.Min) = (x < Int.Pls)" unfolding Bit0_def Pls_def Min_def by auto
huffman@26086
    86
lemma bitle16: "(Int.Bit1 x \<le> Int.Min) = (x \<le> Int.Min)" by (rule less_eq_Bit1_Min)
obua@23664
    87
lemmas bitle = bitle1 bitle2 bitle3 bitle4 bitle5 bitle6 bitle7 bitle8 
obua@23664
    88
                 bitle9 bitle10 bitle11 bitle12 bitle13 bitle14 bitle15 bitle16
obua@23664
    89
obua@23664
    90
(* succ for bit strings *)
huffman@26075
    91
lemmas bitsucc = succ_bin_simps
obua@23664
    92
obua@23664
    93
(* pred for bit strings *)
huffman@26075
    94
lemmas bitpred = pred_bin_simps
obua@23664
    95
obua@23664
    96
(* unary minus for bit strings *)
huffman@26075
    97
lemmas bituminus = minus_bin_simps
obua@23664
    98
obua@23664
    99
(* addition for bit strings *)
huffman@26086
   100
lemmas bitadd = add_bin_simps
obua@23664
   101
obua@23664
   102
(* multiplication for bit strings *) 
haftmann@25919
   103
lemma mult_Pls_right: "x * Int.Pls = Int.Pls" by (simp add: Pls_def)
haftmann@25919
   104
lemma mult_Min_right: "x * Int.Min = - x" by (subst mult_commute, simp add: mult_Min)
huffman@26086
   105
lemma multb0x: "(Int.Bit0 x) * y = Int.Bit0 (x * y)" by (rule mult_Bit0)
huffman@26086
   106
lemma multxb0: "x * (Int.Bit0 y) = Int.Bit0 (x * y)" unfolding Bit0_def by simp
huffman@26086
   107
lemma multb1: "(Int.Bit1 x) * (Int.Bit1 y) = Int.Bit1 (Int.Bit0 (x * y) + x + y)"
huffman@26086
   108
  unfolding Bit0_def Bit1_def by (simp add: ring_simps)
obua@23664
   109
lemmas bitmul = mult_Pls mult_Min mult_Pls_right mult_Min_right multb0x multxb0 multb1
obua@23664
   110
obua@23664
   111
lemmas bitarith = bitnorm bitiszero bitneg bitlezero biteq bitless bitle bitsucc bitpred bituminus bitadd bitmul 
obua@23664
   112
obua@23664
   113
constdefs 
obua@23664
   114
  "nat_norm_number_of (x::nat) == x"
obua@23664
   115
obua@23664
   116
lemma nat_norm_number_of: "nat_norm_number_of (number_of w) = (if lezero w then 0 else number_of w)"
obua@23664
   117
  apply (simp add: nat_norm_number_of_def)
obua@23664
   118
  unfolding lezero_def iszero_def neg_def
obua@23664
   119
  apply (simp add: number_of_is_id)
obua@23664
   120
  done
obua@23664
   121
obua@23664
   122
(* Normalization of nat literals *)
haftmann@25919
   123
lemma natnorm0: "(0::nat) = number_of (Int.Pls)" by auto
huffman@26086
   124
lemma natnorm1: "(1 :: nat) = number_of (Int.Bit1 Int.Pls)"  by auto 
obua@23664
   125
lemmas natnorm = natnorm0 natnorm1 nat_norm_number_of
obua@23664
   126
obua@23664
   127
(* Suc *)
haftmann@25919
   128
lemma natsuc: "Suc (number_of x) = (if neg x then 1 else number_of (Int.succ x))" by (auto simp add: number_of_is_id)
obua@23664
   129
obua@23664
   130
(* Addition for nat *)
obua@23664
   131
lemma natadd: "number_of x + ((number_of y)::nat) = (if neg x then (number_of y) else (if neg y then number_of x else (number_of (x + y))))"
obua@23664
   132
  by (auto simp add: number_of_is_id)
obua@23664
   133
obua@23664
   134
(* Subtraction for nat *)
obua@23664
   135
lemma natsub: "(number_of x) - ((number_of y)::nat) = 
obua@23664
   136
  (if neg x then 0 else (if neg y then number_of x else (nat_norm_number_of (number_of (x + (- y))))))"
obua@23664
   137
  unfolding nat_norm_number_of
obua@23664
   138
  by (auto simp add: number_of_is_id neg_def lezero_def iszero_def Let_def nat_number_of_def)
obua@23664
   139
obua@23664
   140
(* Multiplication for nat *)
obua@23664
   141
lemma natmul: "(number_of x) * ((number_of y)::nat) = 
obua@23664
   142
  (if neg x then 0 else (if neg y then 0 else number_of (x * y)))"
obua@23664
   143
  apply (auto simp add: number_of_is_id neg_def iszero_def)
obua@23664
   144
  apply (case_tac "x > 0")
obua@23664
   145
  apply auto
obua@23664
   146
  apply (simp add: mult_strict_left_mono[where a=y and b=0 and c=x, simplified])
obua@23664
   147
  done
obua@23664
   148
obua@23664
   149
lemma nateq: "(((number_of x)::nat) = (number_of y)) = ((lezero x \<and> lezero y) \<or> (x = y))"
obua@23664
   150
  by (auto simp add: iszero_def lezero_def neg_def number_of_is_id)
obua@23664
   151
obua@23664
   152
lemma natless: "(((number_of x)::nat) < (number_of y)) = ((x < y) \<and> (\<not> (lezero y)))"
obua@23664
   153
  by (auto simp add: number_of_is_id neg_def lezero_def)
obua@23664
   154
obua@23664
   155
lemma natle: "(((number_of x)::nat) \<le> (number_of y)) = (y < x \<longrightarrow> lezero x)"
obua@23664
   156
  by (auto simp add: number_of_is_id lezero_def nat_number_of_def)
obua@23664
   157
obua@23664
   158
fun natfac :: "nat \<Rightarrow> nat"
obua@23664
   159
where
obua@23664
   160
   "natfac n = (if n = 0 then 1 else n * (natfac (n - 1)))"
obua@23664
   161
obua@23664
   162
lemmas compute_natarith = bitarith natnorm natsuc natadd natsub natmul nateq natless natle natfac.simps
obua@23664
   163
obua@23664
   164
lemma number_eq: "(((number_of x)::'a::{number_ring, ordered_idom}) = (number_of y)) = (x = y)"
obua@23664
   165
  unfolding number_of_eq
obua@23664
   166
  apply simp
obua@23664
   167
  done
obua@23664
   168
obua@23664
   169
lemma number_le: "(((number_of x)::'a::{number_ring, ordered_idom}) \<le>  (number_of y)) = (x \<le> y)"
obua@23664
   170
  unfolding number_of_eq
obua@23664
   171
  apply simp
obua@23664
   172
  done
obua@23664
   173
obua@23664
   174
lemma number_less: "(((number_of x)::'a::{number_ring, ordered_idom}) <  (number_of y)) = (x < y)"
obua@23664
   175
  unfolding number_of_eq 
obua@23664
   176
  apply simp
obua@23664
   177
  done
obua@23664
   178
obua@23664
   179
lemma number_diff: "((number_of x)::'a::{number_ring, ordered_idom}) - number_of y = number_of (x + (- y))"
obua@23664
   180
  apply (subst diff_number_of_eq)
obua@23664
   181
  apply simp
obua@23664
   182
  done
obua@23664
   183
obua@23664
   184
lemmas number_norm = number_of_Pls[symmetric] numeral_1_eq_1[symmetric]
obua@23664
   185
obua@23664
   186
lemmas compute_numberarith = number_of_minus[symmetric] number_of_add[symmetric] number_diff number_of_mult[symmetric] number_norm number_eq number_le number_less
obua@23664
   187
obua@23664
   188
lemma compute_real_of_nat_number_of: "real ((number_of v)::nat) = (if neg v then 0 else number_of v)"
obua@23664
   189
  by (simp only: real_of_nat_number_of number_of_is_id)
obua@23664
   190
obua@23664
   191
lemma compute_nat_of_int_number_of: "nat ((number_of v)::int) = (number_of v)"
obua@23664
   192
  by simp
obua@23664
   193
obua@23664
   194
lemmas compute_num_conversions = compute_real_of_nat_number_of compute_nat_of_int_number_of real_number_of
obua@23664
   195
obua@23664
   196
lemmas zpowerarith = zpower_number_of_even
obua@23664
   197
  zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
obua@23664
   198
  zpower_Pls zpower_Min
obua@23664
   199
obua@23664
   200
(* div, mod *)
obua@23664
   201
obua@23664
   202
lemma adjust: "adjust b (q, r) = (if 0 \<le> r - b then (2 * q + 1, r - b) else (2 * q, r))"
obua@23664
   203
  by (auto simp only: adjust_def)
obua@23664
   204
obua@23664
   205
lemma negateSnd: "negateSnd (q, r) = (q, -r)" 
obua@23664
   206
  by (auto simp only: negateSnd_def)
obua@23664
   207
obua@23664
   208
lemma divAlg: "divAlg (a, b) = (if 0\<le>a then
obua@23664
   209
                  if 0\<le>b then posDivAlg a b
obua@23664
   210
                  else if a=0 then (0, 0)
obua@23664
   211
                       else negateSnd (negDivAlg (-a) (-b))
obua@23664
   212
               else 
obua@23664
   213
                  if 0<b then negDivAlg a b
obua@23664
   214
                  else negateSnd (posDivAlg (-a) (-b)))"
obua@23664
   215
  by (auto simp only: divAlg_def)
obua@23664
   216
obua@23664
   217
lemmas compute_div_mod = div_def mod_def divAlg adjust negateSnd posDivAlg.simps negDivAlg.simps
obua@23664
   218
obua@23664
   219
obua@23664
   220
obua@23664
   221
(* collecting all the theorems *)
obua@23664
   222
haftmann@25919
   223
lemma even_Pls: "even (Int.Pls) = True"
obua@23664
   224
  apply (unfold Pls_def even_def)
obua@23664
   225
  by simp
obua@23664
   226
haftmann@25919
   227
lemma even_Min: "even (Int.Min) = False"
obua@23664
   228
  apply (unfold Min_def even_def)
obua@23664
   229
  by simp
obua@23664
   230
huffman@26086
   231
lemma even_B0: "even (Int.Bit0 x) = True"
huffman@26086
   232
  apply (unfold Bit0_def)
obua@23664
   233
  by simp
obua@23664
   234
huffman@26086
   235
lemma even_B1: "even (Int.Bit1 x) = False"
huffman@26086
   236
  apply (unfold Bit1_def)
obua@23664
   237
  by simp
obua@23664
   238
obua@23664
   239
lemma even_number_of: "even ((number_of w)::int) = even w"
obua@23664
   240
  by (simp only: number_of_is_id)
obua@23664
   241
obua@23664
   242
lemmas compute_even = even_Pls even_Min even_B0 even_B1 even_number_of
obua@23664
   243
obua@23664
   244
lemmas compute_numeral = compute_if compute_let compute_pair compute_bool 
obua@23664
   245
                         compute_natarith compute_numberarith max_def min_def compute_num_conversions zpowerarith compute_div_mod compute_even
obua@23664
   246
obua@23664
   247
end
obua@23664
   248
obua@23664
   249
obua@23664
   250