src/HOL/Matrix/ComputeNumeral.thy
author haftmann
Fri Nov 27 08:41:10 2009 +0100 (2009-11-27)
changeset 33963 977b94b64905
parent 33343 2eb0b672ab40
child 35028 108662d50512
permissions -rw-r--r--
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
obua@23664
     1
theory ComputeNumeral
hoelzl@29804
     2
imports ComputeHOL ComputeFloat
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@29037
    32
lemmas biteq = eq_bin_simps
obua@23664
    33
obua@23664
    34
(* x < y for bit strings *)
huffman@29037
    35
lemmas bitless = less_bin_simps
obua@23664
    36
obua@23664
    37
(* x \<le> y for bit strings *)
huffman@29037
    38
lemmas bitle = le_bin_simps
obua@23664
    39
obua@23664
    40
(* succ for bit strings *)
huffman@26075
    41
lemmas bitsucc = succ_bin_simps
obua@23664
    42
obua@23664
    43
(* pred for bit strings *)
huffman@26075
    44
lemmas bitpred = pred_bin_simps
obua@23664
    45
obua@23664
    46
(* unary minus for bit strings *)
huffman@26075
    47
lemmas bituminus = minus_bin_simps
obua@23664
    48
obua@23664
    49
(* addition for bit strings *)
huffman@26086
    50
lemmas bitadd = add_bin_simps
obua@23664
    51
obua@23664
    52
(* multiplication for bit strings *) 
haftmann@25919
    53
lemma mult_Pls_right: "x * Int.Pls = Int.Pls" by (simp add: Pls_def)
haftmann@25919
    54
lemma mult_Min_right: "x * Int.Min = - x" by (subst mult_commute, simp add: mult_Min)
huffman@26086
    55
lemma multb0x: "(Int.Bit0 x) * y = Int.Bit0 (x * y)" by (rule mult_Bit0)
huffman@26086
    56
lemma multxb0: "x * (Int.Bit0 y) = Int.Bit0 (x * y)" unfolding Bit0_def by simp
huffman@26086
    57
lemma multb1: "(Int.Bit1 x) * (Int.Bit1 y) = Int.Bit1 (Int.Bit0 (x * y) + x + y)"
nipkow@29667
    58
  unfolding Bit0_def Bit1_def by (simp add: algebra_simps)
obua@23664
    59
lemmas bitmul = mult_Pls mult_Min mult_Pls_right mult_Min_right multb0x multxb0 multb1
obua@23664
    60
obua@23664
    61
lemmas bitarith = bitnorm bitiszero bitneg bitlezero biteq bitless bitle bitsucc bitpred bituminus bitadd bitmul 
obua@23664
    62
obua@23664
    63
constdefs 
obua@23664
    64
  "nat_norm_number_of (x::nat) == x"
obua@23664
    65
obua@23664
    66
lemma nat_norm_number_of: "nat_norm_number_of (number_of w) = (if lezero w then 0 else number_of w)"
obua@23664
    67
  apply (simp add: nat_norm_number_of_def)
obua@23664
    68
  unfolding lezero_def iszero_def neg_def
huffman@28990
    69
  apply (simp add: numeral_simps)
obua@23664
    70
  done
obua@23664
    71
obua@23664
    72
(* Normalization of nat literals *)
haftmann@25919
    73
lemma natnorm0: "(0::nat) = number_of (Int.Pls)" by auto
huffman@26086
    74
lemma natnorm1: "(1 :: nat) = number_of (Int.Bit1 Int.Pls)"  by auto 
obua@23664
    75
lemmas natnorm = natnorm0 natnorm1 nat_norm_number_of
obua@23664
    76
obua@23664
    77
(* Suc *)
haftmann@25919
    78
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
    79
obua@23664
    80
(* Addition for nat *)
obua@23664
    81
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))))"
huffman@29013
    82
  unfolding nat_number_of_def number_of_is_id neg_def
huffman@29013
    83
  by auto
obua@23664
    84
obua@23664
    85
(* Subtraction for nat *)
obua@23664
    86
lemma natsub: "(number_of x) - ((number_of y)::nat) = 
obua@23664
    87
  (if neg x then 0 else (if neg y then number_of x else (nat_norm_number_of (number_of (x + (- y))))))"
obua@23664
    88
  unfolding nat_norm_number_of
obua@23664
    89
  by (auto simp add: number_of_is_id neg_def lezero_def iszero_def Let_def nat_number_of_def)
obua@23664
    90
obua@23664
    91
(* Multiplication for nat *)
obua@23664
    92
lemma natmul: "(number_of x) * ((number_of y)::nat) = 
obua@23664
    93
  (if neg x then 0 else (if neg y then 0 else number_of (x * y)))"
huffman@29013
    94
  unfolding nat_number_of_def number_of_is_id neg_def
huffman@29013
    95
  by (simp add: nat_mult_distrib)
obua@23664
    96
obua@23664
    97
lemma nateq: "(((number_of x)::nat) = (number_of y)) = ((lezero x \<and> lezero y) \<or> (x = y))"
obua@23664
    98
  by (auto simp add: iszero_def lezero_def neg_def number_of_is_id)
obua@23664
    99
obua@23664
   100
lemma natless: "(((number_of x)::nat) < (number_of y)) = ((x < y) \<and> (\<not> (lezero y)))"
huffman@29013
   101
  by (simp add: lezero_def numeral_simps not_le)
obua@23664
   102
obua@23664
   103
lemma natle: "(((number_of x)::nat) \<le> (number_of y)) = (y < x \<longrightarrow> lezero x)"
obua@23664
   104
  by (auto simp add: number_of_is_id lezero_def nat_number_of_def)
obua@23664
   105
obua@23664
   106
fun natfac :: "nat \<Rightarrow> nat"
obua@23664
   107
where
obua@23664
   108
   "natfac n = (if n = 0 then 1 else n * (natfac (n - 1)))"
obua@23664
   109
obua@23664
   110
lemmas compute_natarith = bitarith natnorm natsuc natadd natsub natmul nateq natless natle natfac.simps
obua@23664
   111
obua@23664
   112
lemma number_eq: "(((number_of x)::'a::{number_ring, ordered_idom}) = (number_of y)) = (x = y)"
obua@23664
   113
  unfolding number_of_eq
obua@23664
   114
  apply simp
obua@23664
   115
  done
obua@23664
   116
obua@23664
   117
lemma number_le: "(((number_of x)::'a::{number_ring, ordered_idom}) \<le>  (number_of y)) = (x \<le> y)"
obua@23664
   118
  unfolding number_of_eq
obua@23664
   119
  apply simp
obua@23664
   120
  done
obua@23664
   121
obua@23664
   122
lemma number_less: "(((number_of x)::'a::{number_ring, ordered_idom}) <  (number_of y)) = (x < y)"
obua@23664
   123
  unfolding number_of_eq 
obua@23664
   124
  apply simp
obua@23664
   125
  done
obua@23664
   126
obua@23664
   127
lemma number_diff: "((number_of x)::'a::{number_ring, ordered_idom}) - number_of y = number_of (x + (- y))"
obua@23664
   128
  apply (subst diff_number_of_eq)
obua@23664
   129
  apply simp
obua@23664
   130
  done
obua@23664
   131
obua@23664
   132
lemmas number_norm = number_of_Pls[symmetric] numeral_1_eq_1[symmetric]
obua@23664
   133
obua@23664
   134
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
   135
obua@23664
   136
lemma compute_real_of_nat_number_of: "real ((number_of v)::nat) = (if neg v then 0 else number_of v)"
obua@23664
   137
  by (simp only: real_of_nat_number_of number_of_is_id)
obua@23664
   138
obua@23664
   139
lemma compute_nat_of_int_number_of: "nat ((number_of v)::int) = (number_of v)"
obua@23664
   140
  by simp
obua@23664
   141
obua@23664
   142
lemmas compute_num_conversions = compute_real_of_nat_number_of compute_nat_of_int_number_of real_number_of
obua@23664
   143
obua@23664
   144
lemmas zpowerarith = zpower_number_of_even
obua@23664
   145
  zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
obua@23664
   146
  zpower_Pls zpower_Min
obua@23664
   147
obua@23664
   148
(* div, mod *)
obua@23664
   149
obua@23664
   150
lemma adjust: "adjust b (q, r) = (if 0 \<le> r - b then (2 * q + 1, r - b) else (2 * q, r))"
obua@23664
   151
  by (auto simp only: adjust_def)
obua@23664
   152
obua@23664
   153
lemma negateSnd: "negateSnd (q, r) = (q, -r)" 
haftmann@29657
   154
  by (simp add: negateSnd_def)
obua@23664
   155
haftmann@33343
   156
lemma divmod: "divmod_int a b = (if 0\<le>a then
obua@23664
   157
                  if 0\<le>b then posDivAlg a b
obua@23664
   158
                  else if a=0 then (0, 0)
obua@23664
   159
                       else negateSnd (negDivAlg (-a) (-b))
obua@23664
   160
               else 
obua@23664
   161
                  if 0<b then negDivAlg a b
obua@23664
   162
                  else negateSnd (posDivAlg (-a) (-b)))"
haftmann@33343
   163
  by (auto simp only: divmod_int_def)
obua@23664
   164
haftmann@33343
   165
lemmas compute_div_mod = div_int_def mod_int_def divmod adjust negateSnd posDivAlg.simps negDivAlg.simps
obua@23664
   166
obua@23664
   167
obua@23664
   168
obua@23664
   169
(* collecting all the theorems *)
obua@23664
   170
haftmann@25919
   171
lemma even_Pls: "even (Int.Pls) = True"
obua@23664
   172
  apply (unfold Pls_def even_def)
obua@23664
   173
  by simp
obua@23664
   174
haftmann@25919
   175
lemma even_Min: "even (Int.Min) = False"
obua@23664
   176
  apply (unfold Min_def even_def)
obua@23664
   177
  by simp
obua@23664
   178
huffman@26086
   179
lemma even_B0: "even (Int.Bit0 x) = True"
huffman@26086
   180
  apply (unfold Bit0_def)
obua@23664
   181
  by simp
obua@23664
   182
huffman@26086
   183
lemma even_B1: "even (Int.Bit1 x) = False"
huffman@26086
   184
  apply (unfold Bit1_def)
obua@23664
   185
  by simp
obua@23664
   186
obua@23664
   187
lemma even_number_of: "even ((number_of w)::int) = even w"
obua@23664
   188
  by (simp only: number_of_is_id)
obua@23664
   189
obua@23664
   190
lemmas compute_even = even_Pls even_Min even_B0 even_B1 even_number_of
obua@23664
   191
obua@23664
   192
lemmas compute_numeral = compute_if compute_let compute_pair compute_bool 
obua@23664
   193
                         compute_natarith compute_numberarith max_def min_def compute_num_conversions zpowerarith compute_div_mod compute_even
obua@23664
   194
obua@23664
   195
end