src/HOL/Library/Code_Bit_Shifts_for_Arithmetic.thy
changeset 81813 8df58b532ecb
equal deleted inserted replaced
81812:232ccd03d9af 81813:8df58b532ecb
       
     1 (*  Title:      HOL/Library/Code_Bit_Shifts_for_Arithmetic.thy
       
     2     Author:     Florian Haftmann, TU Muenchen
       
     3 *)
       
     4 
       
     5 section \<open>Rewrite arithmetic operations to bit-shifts if feasible\<close>
       
     6 
       
     7 theory Code_Bit_Shifts_for_Arithmetic
       
     8   imports Main
       
     9 begin
       
    10 
       
    11 context semiring_bit_operations
       
    12 begin
       
    13 
       
    14 context
       
    15   includes bit_operations_syntax
       
    16 begin
       
    17 
       
    18 lemma [code_unfold]:
       
    19   \<open>of_bool (odd a) = a AND 1\<close>
       
    20   by (simp add: and_one_eq mod2_eq_if)
       
    21 
       
    22 lemma [code_unfold]:
       
    23   \<open>even a \<longleftrightarrow> a AND 1 = 0\<close>
       
    24   by (simp add: and_one_eq mod2_eq_if)
       
    25 
       
    26 lemma [code_unfold]:
       
    27   \<open>2 * a = push_bit 1 a\<close>
       
    28   by (simp add: ac_simps)
       
    29 
       
    30 lemma [code_unfold]:
       
    31   \<open>a * 2 = push_bit 1 a\<close>
       
    32   by simp
       
    33 
       
    34 lemma [code_unfold]:
       
    35   \<open>2 ^ n * a = push_bit n a\<close>
       
    36   by (simp add: push_bit_eq_mult ac_simps)
       
    37 
       
    38 lemma [code_unfold]:
       
    39   \<open>a * 2 ^ n = push_bit n a\<close>
       
    40   by (simp add: push_bit_eq_mult)
       
    41 
       
    42 lemma [code_unfold]:
       
    43   \<open>a div 2 = drop_bit 1 a\<close>
       
    44   by (simp add: drop_bit_eq_div)
       
    45 
       
    46 lemma [code_unfold]:
       
    47   \<open>a div 2 ^ n = drop_bit n a\<close>
       
    48   by (simp add: drop_bit_eq_div)
       
    49 
       
    50 lemma [code_unfold]:
       
    51   \<open>a mod 2 = take_bit 1 a\<close>
       
    52   by (simp add: take_bit_eq_mod)
       
    53 
       
    54 lemma [code_unfold]:
       
    55   \<open>a mod 2 ^ n  = take_bit n a\<close>
       
    56   by (simp add: take_bit_eq_mod)
       
    57 
       
    58 end
       
    59 
       
    60 end
       
    61 
       
    62 end