1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Matrix_LP/ComputeNumeral.thy Sat Mar 17 12:52:40 2012 +0100
1.3 @@ -0,0 +1,189 @@
1.4 +theory ComputeNumeral
1.5 +imports ComputeHOL ComputeFloat
1.6 +begin
1.7 +
1.8 +(* normalization of bit strings *)
1.9 +lemmas bitnorm = normalize_bin_simps
1.10 +
1.11 +(* neg for bit strings *)
1.12 +lemma neg1: "neg Int.Pls = False" by (simp add: Int.Pls_def)
1.13 +lemma neg2: "neg Int.Min = True" apply (subst Int.Min_def) by auto
1.14 +lemma neg3: "neg (Int.Bit0 x) = neg x" apply (simp add: neg_def) apply (subst Bit0_def) by auto
1.15 +lemma neg4: "neg (Int.Bit1 x) = neg x" apply (simp add: neg_def) apply (subst Bit1_def) by auto
1.16 +lemmas bitneg = neg1 neg2 neg3 neg4
1.17 +
1.18 +(* iszero for bit strings *)
1.19 +lemma iszero1: "iszero Int.Pls = True" by (simp add: Int.Pls_def iszero_def)
1.20 +lemma iszero2: "iszero Int.Min = False" apply (subst Int.Min_def) apply (subst iszero_def) by simp
1.21 +lemma iszero3: "iszero (Int.Bit0 x) = iszero x" apply (subst Int.Bit0_def) apply (subst iszero_def)+ by auto
1.22 +lemma iszero4: "iszero (Int.Bit1 x) = False" apply (subst Int.Bit1_def) apply (subst iszero_def)+ apply simp by arith
1.23 +lemmas bitiszero = iszero1 iszero2 iszero3 iszero4
1.24 +
1.25 +(* lezero for bit strings *)
1.26 +definition "lezero x \<longleftrightarrow> x \<le> 0"
1.27 +lemma lezero1: "lezero Int.Pls = True" unfolding Int.Pls_def lezero_def by auto
1.28 +lemma lezero2: "lezero Int.Min = True" unfolding Int.Min_def lezero_def by auto
1.29 +lemma lezero3: "lezero (Int.Bit0 x) = lezero x" unfolding Int.Bit0_def lezero_def by auto
1.30 +lemma lezero4: "lezero (Int.Bit1 x) = neg x" unfolding Int.Bit1_def lezero_def neg_def by auto
1.31 +lemmas bitlezero = lezero1 lezero2 lezero3 lezero4
1.32 +
1.33 +(* equality for bit strings *)
1.34 +lemmas biteq = eq_bin_simps
1.35 +
1.36 +(* x < y for bit strings *)
1.37 +lemmas bitless = less_bin_simps
1.38 +
1.39 +(* x \<le> y for bit strings *)
1.40 +lemmas bitle = le_bin_simps
1.41 +
1.42 +(* succ for bit strings *)
1.43 +lemmas bitsucc = succ_bin_simps
1.44 +
1.45 +(* pred for bit strings *)
1.46 +lemmas bitpred = pred_bin_simps
1.47 +
1.48 +(* unary minus for bit strings *)
1.49 +lemmas bituminus = minus_bin_simps
1.50 +
1.51 +(* addition for bit strings *)
1.52 +lemmas bitadd = add_bin_simps
1.53 +
1.54 +(* multiplication for bit strings *)
1.55 +lemma mult_Pls_right: "x * Int.Pls = Int.Pls" by (simp add: Pls_def)
1.56 +lemma mult_Min_right: "x * Int.Min = - x" by (subst mult_commute) simp
1.57 +lemma multb0x: "(Int.Bit0 x) * y = Int.Bit0 (x * y)" by (rule mult_Bit0)
1.58 +lemma multxb0: "x * (Int.Bit0 y) = Int.Bit0 (x * y)" unfolding Bit0_def by simp
1.59 +lemma multb1: "(Int.Bit1 x) * (Int.Bit1 y) = Int.Bit1 (Int.Bit0 (x * y) + x + y)"
1.60 + unfolding Bit0_def Bit1_def by (simp add: algebra_simps)
1.61 +lemmas bitmul = mult_Pls mult_Min mult_Pls_right mult_Min_right multb0x multxb0 multb1
1.62 +
1.63 +lemmas bitarith = bitnorm bitiszero bitneg bitlezero biteq bitless bitle bitsucc bitpred bituminus bitadd bitmul
1.64 +
1.65 +definition "nat_norm_number_of (x::nat) = x"
1.66 +
1.67 +lemma nat_norm_number_of: "nat_norm_number_of (number_of w) = (if lezero w then 0 else number_of w)"
1.68 + apply (simp add: nat_norm_number_of_def)
1.69 + unfolding lezero_def iszero_def neg_def
1.70 + apply (simp add: numeral_simps)
1.71 + done
1.72 +
1.73 +(* Normalization of nat literals *)
1.74 +lemma natnorm0: "(0::nat) = number_of (Int.Pls)" by auto
1.75 +lemma natnorm1: "(1 :: nat) = number_of (Int.Bit1 Int.Pls)" by auto
1.76 +lemmas natnorm = natnorm0 natnorm1 nat_norm_number_of
1.77 +
1.78 +(* Suc *)
1.79 +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)
1.80 +
1.81 +(* Addition for nat *)
1.82 +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))))"
1.83 + unfolding nat_number_of_def number_of_is_id neg_def
1.84 + by auto
1.85 +
1.86 +(* Subtraction for nat *)
1.87 +lemma natsub: "(number_of x) - ((number_of y)::nat) =
1.88 + (if neg x then 0 else (if neg y then number_of x else (nat_norm_number_of (number_of (x + (- y))))))"
1.89 + unfolding nat_norm_number_of
1.90 + by (auto simp add: number_of_is_id neg_def lezero_def iszero_def Let_def nat_number_of_def)
1.91 +
1.92 +(* Multiplication for nat *)
1.93 +lemma natmul: "(number_of x) * ((number_of y)::nat) =
1.94 + (if neg x then 0 else (if neg y then 0 else number_of (x * y)))"
1.95 + unfolding nat_number_of_def number_of_is_id neg_def
1.96 + by (simp add: nat_mult_distrib)
1.97 +
1.98 +lemma nateq: "(((number_of x)::nat) = (number_of y)) = ((lezero x \<and> lezero y) \<or> (x = y))"
1.99 + by (auto simp add: iszero_def lezero_def neg_def number_of_is_id)
1.100 +
1.101 +lemma natless: "(((number_of x)::nat) < (number_of y)) = ((x < y) \<and> (\<not> (lezero y)))"
1.102 + by (simp add: lezero_def numeral_simps not_le)
1.103 +
1.104 +lemma natle: "(((number_of x)::nat) \<le> (number_of y)) = (y < x \<longrightarrow> lezero x)"
1.105 + by (auto simp add: number_of_is_id lezero_def nat_number_of_def)
1.106 +
1.107 +fun natfac :: "nat \<Rightarrow> nat"
1.108 + where "natfac n = (if n = 0 then 1 else n * (natfac (n - 1)))"
1.109 +
1.110 +lemmas compute_natarith = bitarith natnorm natsuc natadd natsub natmul nateq natless natle natfac.simps
1.111 +
1.112 +lemma number_eq: "(((number_of x)::'a::{number_ring, linordered_idom}) = (number_of y)) = (x = y)"
1.113 + unfolding number_of_eq
1.114 + apply simp
1.115 + done
1.116 +
1.117 +lemma number_le: "(((number_of x)::'a::{number_ring, linordered_idom}) \<le> (number_of y)) = (x \<le> y)"
1.118 + unfolding number_of_eq
1.119 + apply simp
1.120 + done
1.121 +
1.122 +lemma number_less: "(((number_of x)::'a::{number_ring, linordered_idom}) < (number_of y)) = (x < y)"
1.123 + unfolding number_of_eq
1.124 + apply simp
1.125 + done
1.126 +
1.127 +lemma number_diff: "((number_of x)::'a::{number_ring, linordered_idom}) - number_of y = number_of (x + (- y))"
1.128 + apply (subst diff_number_of_eq)
1.129 + apply simp
1.130 + done
1.131 +
1.132 +lemmas number_norm = number_of_Pls[symmetric] numeral_1_eq_1[symmetric]
1.133 +
1.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
1.135 +
1.136 +lemma compute_real_of_nat_number_of: "real ((number_of v)::nat) = (if neg v then 0 else number_of v)"
1.137 + by (simp only: real_of_nat_number_of number_of_is_id)
1.138 +
1.139 +lemma compute_nat_of_int_number_of: "nat ((number_of v)::int) = (number_of v)"
1.140 + by simp
1.141 +
1.142 +lemmas compute_num_conversions = compute_real_of_nat_number_of compute_nat_of_int_number_of real_number_of
1.143 +
1.144 +lemmas zpowerarith = zpower_number_of_even
1.145 + zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
1.146 + zpower_Pls zpower_Min
1.147 +
1.148 +(* div, mod *)
1.149 +
1.150 +lemma adjust: "adjust b (q, r) = (if 0 \<le> r - b then (2 * q + 1, r - b) else (2 * q, r))"
1.151 + by (auto simp only: adjust_def)
1.152 +
1.153 +lemma divmod: "divmod_int a b = (if 0\<le>a then
1.154 + if 0\<le>b then posDivAlg a b
1.155 + else if a=0 then (0, 0)
1.156 + else apsnd uminus (negDivAlg (-a) (-b))
1.157 + else
1.158 + if 0<b then negDivAlg a b
1.159 + else apsnd uminus (posDivAlg (-a) (-b)))"
1.160 + by (auto simp only: divmod_int_def)
1.161 +
1.162 +lemmas compute_div_mod = div_int_def mod_int_def divmod adjust apsnd_def map_pair_def posDivAlg.simps negDivAlg.simps
1.163 +
1.164 +
1.165 +
1.166 +(* collecting all the theorems *)
1.167 +
1.168 +lemma even_Pls: "even (Int.Pls) = True"
1.169 + apply (unfold Pls_def even_def)
1.170 + by simp
1.171 +
1.172 +lemma even_Min: "even (Int.Min) = False"
1.173 + apply (unfold Min_def even_def)
1.174 + by simp
1.175 +
1.176 +lemma even_B0: "even (Int.Bit0 x) = True"
1.177 + apply (unfold Bit0_def)
1.178 + by simp
1.179 +
1.180 +lemma even_B1: "even (Int.Bit1 x) = False"
1.181 + apply (unfold Bit1_def)
1.182 + by simp
1.183 +
1.184 +lemma even_number_of: "even ((number_of w)::int) = even w"
1.185 + by (simp only: number_of_is_id)
1.186 +
1.187 +lemmas compute_even = even_Pls even_Min even_B0 even_B1 even_number_of
1.188 +
1.189 +lemmas compute_numeral = compute_if compute_let compute_pair compute_bool
1.190 + compute_natarith compute_numberarith max_def min_def compute_num_conversions zpowerarith compute_div_mod compute_even
1.191 +
1.192 +end