src/HOL/Word/BinOperations.thy
changeset 37654 8e33b9d04a82
parent 30943 eb3dbbe971f6
child 37657 17e1085d07b2
equal deleted inserted replaced
37653:847e95ca9b0a 37654:8e33b9d04a82
     7 *) 
     7 *) 
     8 
     8 
     9 header {* Bitwise Operations on Binary Integers *}
     9 header {* Bitwise Operations on Binary Integers *}
    10 
    10 
    11 theory BinOperations
    11 theory BinOperations
    12 imports BinGeneral BitSyntax
    12 imports Bit_Operations BinGeneral
    13 begin
    13 begin
    14 
    14 
    15 subsection {* Logical operations *}
    15 subsection {* Logical operations *}
    16 
    16 
    17 text "bit-wise logical operations on the int type"
    17 text "bit-wise logical operations on the int type"
    74   "(Int.Bit1 x) XOR (Int.Bit0 y) = Int.Bit1 (x XOR y)"
    74   "(Int.Bit1 x) XOR (Int.Bit0 y) = Int.Bit1 (x XOR y)"
    75   "(Int.Bit1 x) XOR (Int.Bit1 y) = Int.Bit0 (x XOR y)"
    75   "(Int.Bit1 x) XOR (Int.Bit1 y) = Int.Bit0 (x XOR y)"
    76   unfolding BIT_simps [symmetric] int_xor_Bits by simp_all
    76   unfolding BIT_simps [symmetric] int_xor_Bits by simp_all
    77 
    77 
    78 lemma int_xor_x_simps':
    78 lemma int_xor_x_simps':
    79   "w XOR (Int.Pls BIT bit.B0) = w"
    79   "w XOR (Int.Pls BIT 0) = w"
    80   "w XOR (Int.Min BIT bit.B1) = NOT w"
    80   "w XOR (Int.Min BIT 1) = NOT w"
    81   apply (induct w rule: bin_induct)
    81   apply (induct w rule: bin_induct)
    82        apply simp_all[4]
    82        apply simp_all[4]
    83    apply (unfold int_xor_Bits)
    83    apply (unfold int_xor_Bits)
    84    apply clarsimp+
    84    apply clarsimp+
    85   done
    85   done
   107   "(Int.Bit1 x) OR (Int.Bit0 y) = Int.Bit1 (x OR y)"
   107   "(Int.Bit1 x) OR (Int.Bit0 y) = Int.Bit1 (x OR y)"
   108   "(Int.Bit1 x) OR (Int.Bit1 y) = Int.Bit1 (x OR y)"
   108   "(Int.Bit1 x) OR (Int.Bit1 y) = Int.Bit1 (x OR y)"
   109   unfolding BIT_simps [symmetric] int_or_Bits by simp_all
   109   unfolding BIT_simps [symmetric] int_or_Bits by simp_all
   110 
   110 
   111 lemma int_or_x_simps': 
   111 lemma int_or_x_simps': 
   112   "w OR (Int.Pls BIT bit.B0) = w"
   112   "w OR (Int.Pls BIT 0) = w"
   113   "w OR (Int.Min BIT bit.B1) = Int.Min"
   113   "w OR (Int.Min BIT 1) = Int.Min"
   114   apply (induct w rule: bin_induct)
   114   apply (induct w rule: bin_induct)
   115        apply simp_all[4]
   115        apply simp_all[4]
   116    apply (unfold int_or_Bits)
   116    apply (unfold int_or_Bits)
   117    apply clarsimp+
   117    apply clarsimp+
   118   done
   118   done
   140   "(Int.Bit1 x) AND (Int.Bit0 y) = Int.Bit0 (x AND y)"
   140   "(Int.Bit1 x) AND (Int.Bit0 y) = Int.Bit0 (x AND y)"
   141   "(Int.Bit1 x) AND (Int.Bit1 y) = Int.Bit1 (x AND y)"
   141   "(Int.Bit1 x) AND (Int.Bit1 y) = Int.Bit1 (x AND y)"
   142   unfolding BIT_simps [symmetric] int_and_Bits by simp_all
   142   unfolding BIT_simps [symmetric] int_and_Bits by simp_all
   143 
   143 
   144 lemma int_and_x_simps': 
   144 lemma int_and_x_simps': 
   145   "w AND (Int.Pls BIT bit.B0) = Int.Pls"
   145   "w AND (Int.Pls BIT 0) = Int.Pls"
   146   "w AND (Int.Min BIT bit.B1) = w"
   146   "w AND (Int.Min BIT 1) = w"
   147   apply (induct w rule: bin_induct)
   147   apply (induct w rule: bin_induct)
   148        apply simp_all[4]
   148        apply simp_all[4]
   149    apply (unfold int_and_Bits)
   149    apply (unfold int_and_Bits)
   150    apply clarsimp+
   150    apply clarsimp+
   151   done
   151   done
   382   | Suc: "bin_sc (Suc n) b w = bin_sc n b (bin_rest w) BIT bin_last w"
   382   | Suc: "bin_sc (Suc n) b w = bin_sc n b (bin_rest w) BIT bin_last w"
   383 
   383 
   384 (** nth bit, set/clear **)
   384 (** nth bit, set/clear **)
   385 
   385 
   386 lemma bin_nth_sc [simp]: 
   386 lemma bin_nth_sc [simp]: 
   387   "!!w. bin_nth (bin_sc n b w) n = (b = bit.B1)"
   387   "!!w. bin_nth (bin_sc n b w) n = (b = 1)"
   388   by (induct n)  auto
   388   by (induct n)  auto
   389 
   389 
   390 lemma bin_sc_sc_same [simp]: 
   390 lemma bin_sc_sc_same [simp]: 
   391   "!!w. bin_sc n c (bin_sc n b w) = bin_sc n c w"
   391   "!!w. bin_sc n c (bin_sc n b w) = bin_sc n c w"
   392   by (induct n) auto
   392   by (induct n) auto
   398    apply (case_tac [!] m)
   398    apply (case_tac [!] m)
   399      apply auto
   399      apply auto
   400   done
   400   done
   401 
   401 
   402 lemma bin_nth_sc_gen: 
   402 lemma bin_nth_sc_gen: 
   403   "!!w m. bin_nth (bin_sc n b w) m = (if m = n then b = bit.B1 else bin_nth w m)"
   403   "!!w m. bin_nth (bin_sc n b w) m = (if m = n then b = 1 else bin_nth w m)"
   404   by (induct n) (case_tac [!] m, auto)
   404   by (induct n) (case_tac [!] m, auto)
   405   
   405   
   406 lemma bin_sc_nth [simp]:
   406 lemma bin_sc_nth [simp]:
   407   "!!w. (bin_sc n (If (bin_nth w n) bit.B1 bit.B0) w) = w"
   407   "!!w. (bin_sc n (If (bin_nth w n) 1 0) w) = w"
   408   by (induct n) auto
   408   by (induct n) auto
   409 
   409 
   410 lemma bin_sign_sc [simp]:
   410 lemma bin_sign_sc [simp]:
   411   "!!w. bin_sign (bin_sc n b w) = bin_sign w"
   411   "!!w. bin_sign (bin_sc n b w) = bin_sign w"
   412   by (induct n) auto
   412   by (induct n) auto
   417    apply (case_tac [!] w rule: bin_exhaust)
   417    apply (case_tac [!] w rule: bin_exhaust)
   418    apply (case_tac [!] m, auto)
   418    apply (case_tac [!] m, auto)
   419   done
   419   done
   420 
   420 
   421 lemma bin_clr_le:
   421 lemma bin_clr_le:
   422   "!!w. bin_sc n bit.B0 w <= w"
   422   "!!w. bin_sc n 0 w <= w"
   423   apply (induct n) 
   423   apply (induct n) 
   424    apply (case_tac [!] w rule: bin_exhaust)
   424    apply (case_tac [!] w rule: bin_exhaust)
   425    apply (auto simp del: BIT_simps)
   425    apply (auto simp del: BIT_simps)
   426    apply (unfold Bit_def)
   426    apply (unfold Bit_def)
   427    apply (simp_all split: bit.split)
   427    apply (simp_all split: bit.split)
   428   done
   428   done
   429 
   429 
   430 lemma bin_set_ge:
   430 lemma bin_set_ge:
   431   "!!w. bin_sc n bit.B1 w >= w"
   431   "!!w. bin_sc n 1 w >= w"
   432   apply (induct n) 
   432   apply (induct n) 
   433    apply (case_tac [!] w rule: bin_exhaust)
   433    apply (case_tac [!] w rule: bin_exhaust)
   434    apply (auto simp del: BIT_simps)
   434    apply (auto simp del: BIT_simps)
   435    apply (unfold Bit_def)
   435    apply (unfold Bit_def)
   436    apply (simp_all split: bit.split)
   436    apply (simp_all split: bit.split)
   437   done
   437   done
   438 
   438 
   439 lemma bintr_bin_clr_le:
   439 lemma bintr_bin_clr_le:
   440   "!!w m. bintrunc n (bin_sc m bit.B0 w) <= bintrunc n w"
   440   "!!w m. bintrunc n (bin_sc m 0 w) <= bintrunc n w"
   441   apply (induct n)
   441   apply (induct n)
   442    apply simp
   442    apply simp
   443   apply (case_tac w rule: bin_exhaust)
   443   apply (case_tac w rule: bin_exhaust)
   444   apply (case_tac m)
   444   apply (case_tac m)
   445    apply (auto simp del: BIT_simps)
   445    apply (auto simp del: BIT_simps)
   446    apply (unfold Bit_def)
   446    apply (unfold Bit_def)
   447    apply (simp_all split: bit.split)
   447    apply (simp_all split: bit.split)
   448   done
   448   done
   449 
   449 
   450 lemma bintr_bin_set_ge:
   450 lemma bintr_bin_set_ge:
   451   "!!w m. bintrunc n (bin_sc m bit.B1 w) >= bintrunc n w"
   451   "!!w m. bintrunc n (bin_sc m 1 w) >= bintrunc n w"
   452   apply (induct n)
   452   apply (induct n)
   453    apply simp
   453    apply simp
   454   apply (case_tac w rule: bin_exhaust)
   454   apply (case_tac w rule: bin_exhaust)
   455   apply (case_tac m)
   455   apply (case_tac m)
   456    apply (auto simp del: BIT_simps)
   456    apply (auto simp del: BIT_simps)
   457    apply (unfold Bit_def)
   457    apply (unfold Bit_def)
   458    apply (simp_all split: bit.split)
   458    apply (simp_all split: bit.split)
   459   done
   459   done
   460 
   460 
   461 lemma bin_sc_FP [simp]: "bin_sc n bit.B0 Int.Pls = Int.Pls"
   461 lemma bin_sc_FP [simp]: "bin_sc n 0 Int.Pls = Int.Pls"
   462   by (induct n) auto
   462   by (induct n) auto
   463 
   463 
   464 lemma bin_sc_TM [simp]: "bin_sc n bit.B1 Int.Min = Int.Min"
   464 lemma bin_sc_TM [simp]: "bin_sc n 1 Int.Min = Int.Min"
   465   by (induct n) auto
   465   by (induct n) auto
   466   
   466   
   467 lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP
   467 lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP
   468 
   468 
   469 lemma bin_sc_minus:
   469 lemma bin_sc_minus:
   479 subsection {* Operations on lists of booleans *}
   479 subsection {* Operations on lists of booleans *}
   480 
   480 
   481 primrec bl_to_bin_aux :: "bool list \<Rightarrow> int \<Rightarrow> int" where
   481 primrec bl_to_bin_aux :: "bool list \<Rightarrow> int \<Rightarrow> int" where
   482   Nil: "bl_to_bin_aux [] w = w"
   482   Nil: "bl_to_bin_aux [] w = w"
   483   | Cons: "bl_to_bin_aux (b # bs) w = 
   483   | Cons: "bl_to_bin_aux (b # bs) w = 
   484       bl_to_bin_aux bs (w BIT (if b then bit.B1 else bit.B0))"
   484       bl_to_bin_aux bs (w BIT (if b then 1 else 0))"
   485 
   485 
   486 definition bl_to_bin :: "bool list \<Rightarrow> int" where
   486 definition bl_to_bin :: "bool list \<Rightarrow> int" where
   487   bl_to_bin_def : "bl_to_bin bs = bl_to_bin_aux bs Int.Pls"
   487   bl_to_bin_def : "bl_to_bin bs = bl_to_bin_aux bs Int.Pls"
   488 
   488 
   489 primrec bin_to_bl_aux :: "nat \<Rightarrow> int \<Rightarrow> bool list \<Rightarrow> bool list" where
   489 primrec bin_to_bl_aux :: "nat \<Rightarrow> int \<Rightarrow> bool list \<Rightarrow> bool list" where
   490   Z: "bin_to_bl_aux 0 w bl = bl"
   490   Z: "bin_to_bl_aux 0 w bl = bl"
   491   | Suc: "bin_to_bl_aux (Suc n) w bl =
   491   | Suc: "bin_to_bl_aux (Suc n) w bl =
   492       bin_to_bl_aux n (bin_rest w) ((bin_last w = bit.B1) # bl)"
   492       bin_to_bl_aux n (bin_rest w) ((bin_last w = 1) # bl)"
   493 
   493 
   494 definition bin_to_bl :: "nat \<Rightarrow> int \<Rightarrow> bool list" where
   494 definition bin_to_bl :: "nat \<Rightarrow> int \<Rightarrow> bool list" where
   495   bin_to_bl_def : "bin_to_bl n w = bin_to_bl_aux n w []"
   495   bin_to_bl_def : "bin_to_bl n w = bin_to_bl_aux n w []"
   496 
   496 
   497 primrec bl_of_nth :: "nat \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool list" where
   497 primrec bl_of_nth :: "nat \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool list" where