src/HOL/Word/Bool_List_Representation.thy
changeset 54847 d6cf9a5b9be9
parent 53438 6301ed01e34d
child 54848 a303daddebbf
equal deleted inserted replaced
54846:bf86b2002c96 54847:d6cf9a5b9be9
    32 subsection {* Operations on lists of booleans *}
    32 subsection {* Operations on lists of booleans *}
    33 
    33 
    34 primrec bl_to_bin_aux :: "bool list \<Rightarrow> int \<Rightarrow> int" where
    34 primrec bl_to_bin_aux :: "bool list \<Rightarrow> int \<Rightarrow> int" where
    35   Nil: "bl_to_bin_aux [] w = w"
    35   Nil: "bl_to_bin_aux [] w = w"
    36   | Cons: "bl_to_bin_aux (b # bs) w = 
    36   | Cons: "bl_to_bin_aux (b # bs) w = 
    37       bl_to_bin_aux bs (w BIT (if b then 1 else 0))"
    37       bl_to_bin_aux bs (w BIT b)"
    38 
    38 
    39 definition bl_to_bin :: "bool list \<Rightarrow> int" where
    39 definition bl_to_bin :: "bool list \<Rightarrow> int" where
    40   bl_to_bin_def: "bl_to_bin bs = bl_to_bin_aux bs 0"
    40   bl_to_bin_def: "bl_to_bin bs = bl_to_bin_aux bs 0"
    41 
    41 
    42 primrec bin_to_bl_aux :: "nat \<Rightarrow> int \<Rightarrow> bool list \<Rightarrow> bool list" where
    42 primrec bin_to_bl_aux :: "nat \<Rightarrow> int \<Rightarrow> bool list \<Rightarrow> bool list" where
    43   Z: "bin_to_bl_aux 0 w bl = bl"
    43   Z: "bin_to_bl_aux 0 w bl = bl"
    44   | Suc: "bin_to_bl_aux (Suc n) w bl =
    44   | Suc: "bin_to_bl_aux (Suc n) w bl =
    45       bin_to_bl_aux n (bin_rest w) ((bin_last w = 1) # bl)"
    45       bin_to_bl_aux n (bin_rest w) ((bin_last w) # bl)"
    46 
    46 
    47 definition bin_to_bl :: "nat \<Rightarrow> int \<Rightarrow> bool list" where
    47 definition bin_to_bl :: "nat \<Rightarrow> int \<Rightarrow> bool list" where
    48   bin_to_bl_def : "bin_to_bl n w = bin_to_bl_aux n w []"
    48   bin_to_bl_def : "bin_to_bl n w = bin_to_bl_aux n w []"
    49 
    49 
    50 primrec bl_of_nth :: "nat \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool list" where
    50 primrec bl_of_nth :: "nat \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool list" where
   104     bin_to_bl_aux (n - 1) 0 (True # bl)"
   104     bin_to_bl_aux (n - 1) 0 (True # bl)"
   105   by (cases n) auto
   105   by (cases n) auto
   106 
   106 
   107 lemma bin_to_bl_aux_Bit_minus_simp [simp]:
   107 lemma bin_to_bl_aux_Bit_minus_simp [simp]:
   108   "0 < n ==> bin_to_bl_aux n (w BIT b) bl = 
   108   "0 < n ==> bin_to_bl_aux n (w BIT b) bl = 
   109     bin_to_bl_aux (n - 1) w ((b = 1) # bl)"
   109     bin_to_bl_aux (n - 1) w (b # bl)"
   110   by (cases n) auto
   110   by (cases n) auto
   111 
   111 
   112 lemma bin_to_bl_aux_Bit0_minus_simp [simp]:
   112 lemma bin_to_bl_aux_Bit0_minus_simp [simp]:
   113   "0 < n ==> bin_to_bl_aux n (numeral (Num.Bit0 w)) bl = 
   113   "0 < n ==> bin_to_bl_aux n (numeral (Num.Bit0 w)) bl = 
   114     bin_to_bl_aux (n - 1) (numeral w) (False # bl)"
   114     bin_to_bl_aux (n - 1) (numeral w) (False # bl)"
   251   "hd (bin_to_bl_aux (Suc n) w bs) = 
   251   "hd (bin_to_bl_aux (Suc n) w bs) = 
   252     (bin_sign (sbintrunc n w) = -1)"
   252     (bin_sign (sbintrunc n w) = -1)"
   253   apply (induct n arbitrary: w bs)
   253   apply (induct n arbitrary: w bs)
   254    apply clarsimp
   254    apply clarsimp
   255    apply (cases w rule: bin_exhaust)
   255    apply (cases w rule: bin_exhaust)
   256    apply (simp split add : bit.split)
   256    apply simp
   257   apply clarsimp
       
   258   done
   257   done
   259     
   258     
   260 lemma bl_sbin_sign: 
   259 lemma bl_sbin_sign: 
   261   "hd (bin_to_bl (Suc n) w) = (bin_sign (sbintrunc n w) = -1)"
   260   "hd (bin_to_bl (Suc n) w) = (bin_sign (sbintrunc n w) = -1)"
   262   unfolding bin_to_bl_def by (rule bl_sbin_sign_aux)
   261   unfolding bin_to_bl_def by (rule bl_sbin_sign_aux)
   315 lemma bl_to_bin_lt2p_aux:
   314 lemma bl_to_bin_lt2p_aux:
   316   "bl_to_bin_aux bs w < (w + 1) * (2 ^ length bs)"
   315   "bl_to_bin_aux bs w < (w + 1) * (2 ^ length bs)"
   317   apply (induct bs arbitrary: w)
   316   apply (induct bs arbitrary: w)
   318    apply clarsimp
   317    apply clarsimp
   319   apply clarsimp
   318   apply clarsimp
   320   apply safe
       
   321   apply (drule meta_spec, erule xtrans(8) [rotated], simp add: Bit_def)+
   319   apply (drule meta_spec, erule xtrans(8) [rotated], simp add: Bit_def)+
   322   done
   320   done
   323 
   321 
   324 lemma bl_to_bin_lt2p: "bl_to_bin bs < (2 ^ length bs)"
   322 lemma bl_to_bin_lt2p: "bl_to_bin bs < (2 ^ length bs)"
   325   apply (unfold bl_to_bin_def)
   323   apply (unfold bl_to_bin_def)
   332 lemma bl_to_bin_ge2p_aux:
   330 lemma bl_to_bin_ge2p_aux:
   333   "bl_to_bin_aux bs w >= w * (2 ^ length bs)"
   331   "bl_to_bin_aux bs w >= w * (2 ^ length bs)"
   334   apply (induct bs arbitrary: w)
   332   apply (induct bs arbitrary: w)
   335    apply clarsimp
   333    apply clarsimp
   336   apply clarsimp
   334   apply clarsimp
   337   apply safe
       
   338    apply (drule meta_spec, erule order_trans [rotated],
   335    apply (drule meta_spec, erule order_trans [rotated],
   339           simp add: Bit_B0_2t Bit_B1_2t algebra_simps)+
   336           simp add: Bit_B0_2t Bit_B1_2t algebra_simps)+
       
   337    apply (simp add: Bit_def)
   340   done
   338   done
   341 
   339 
   342 lemma bl_to_bin_ge0: "bl_to_bin bs >= 0"
   340 lemma bl_to_bin_ge0: "bl_to_bin bs >= 0"
   343   apply (unfold bl_to_bin_def)
   341   apply (unfold bl_to_bin_def)
   344   apply (rule xtrans(4))
   342   apply (rule xtrans(4))
   420 
   418 
   421 lemma hd_butlast: "size xs > 1 ==> hd (butlast xs) = hd xs"
   419 lemma hd_butlast: "size xs > 1 ==> hd (butlast xs) = hd xs"
   422   by (cases xs) auto
   420   by (cases xs) auto
   423 
   421 
   424 lemma last_bin_last': 
   422 lemma last_bin_last': 
   425   "size xs > 0 \<Longrightarrow> last xs = (bin_last (bl_to_bin_aux xs w) = 1)" 
   423   "size xs > 0 \<Longrightarrow> last xs \<longleftrightarrow> bin_last (bl_to_bin_aux xs w)" 
   426   by (induct xs arbitrary: w) auto
   424   by (induct xs arbitrary: w) auto
   427 
   425 
   428 lemma last_bin_last: 
   426 lemma last_bin_last: 
   429   "size xs > 0 ==> last xs = (bin_last (bl_to_bin xs) = 1)" 
   427   "size xs > 0 ==> last xs \<longleftrightarrow> bin_last (bl_to_bin xs)" 
   430   unfolding bl_to_bin_def by (erule last_bin_last')
   428   unfolding bl_to_bin_def by (erule last_bin_last')
   431   
   429   
   432 lemma bin_last_last: 
   430 lemma bin_last_last: 
   433   "bin_last w = (if last (bin_to_bl (Suc n) w) then 1 else 0)" 
   431   "bin_last w \<longleftrightarrow> last (bin_to_bl (Suc n) w)" 
   434   apply (unfold bin_to_bl_def)
   432   apply (unfold bin_to_bl_def)
   435   apply simp
   433   apply simp
   436   apply (auto simp add: bin_to_bl_aux_alt)
   434   apply (auto simp add: bin_to_bl_aux_alt)
   437   done
   435   done
   438 
   436 
   445    apply simp
   443    apply simp
   446   apply (case_tac v rule: bin_exhaust)
   444   apply (case_tac v rule: bin_exhaust)
   447   apply (case_tac w rule: bin_exhaust)
   445   apply (case_tac w rule: bin_exhaust)
   448   apply clarsimp
   446   apply clarsimp
   449   apply (case_tac b)
   447   apply (case_tac b)
   450   apply (case_tac ba, safe, simp_all)+
   448   apply auto
   451   done
   449   done
   452 
   450 
   453 lemma bl_or_aux_bin:
   451 lemma bl_or_aux_bin:
   454   "map2 (op | ) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = 
   452   "map2 (op | ) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = 
   455     bin_to_bl_aux n (v OR w) (map2 (op | ) bs cs)"
   453     bin_to_bl_aux n (v OR w) (map2 (op | ) bs cs)"
   456   apply (induct n arbitrary: v w bs cs)
   454   apply (induct n arbitrary: v w bs cs)
   457    apply simp
   455    apply simp
   458   apply (case_tac v rule: bin_exhaust)
   456   apply (case_tac v rule: bin_exhaust)
   459   apply (case_tac w rule: bin_exhaust)
   457   apply (case_tac w rule: bin_exhaust)
   460   apply clarsimp
   458   apply clarsimp
   461   apply (case_tac b)
       
   462   apply (case_tac ba, safe, simp_all)+
       
   463   done
   459   done
   464     
   460     
   465 lemma bl_and_aux_bin:
   461 lemma bl_and_aux_bin:
   466   "map2 (op & ) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = 
   462   "map2 (op & ) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = 
   467     bin_to_bl_aux n (v AND w) (map2 (op & ) bs cs)" 
   463     bin_to_bl_aux n (v AND w) (map2 (op & ) bs cs)" 
   844   by (auto intro: trans [OF rbl_mult_gt1])
   840   by (auto intro: trans [OF rbl_mult_gt1])
   845   
   841   
   846 lemmas rbl_mult_Suc = lessI [THEN rbl_mult_gt]
   842 lemmas rbl_mult_Suc = lessI [THEN rbl_mult_gt]
   847 
   843 
   848 lemma rbbl_Cons: 
   844 lemma rbbl_Cons: 
   849   "b # rev (bin_to_bl n x) = rev (bin_to_bl (Suc n) (x BIT If b 1 0))"
   845   "b # rev (bin_to_bl n x) = rev (bin_to_bl (Suc n) (x BIT b))"
   850   apply (unfold bin_to_bl_def)
   846   apply (unfold bin_to_bl_def)
   851   apply simp
   847   apply simp
   852   apply (simp add: bin_to_bl_aux_alt)
   848   apply (simp add: bin_to_bl_aux_alt)
   853   done
   849   done
   854 
   850