src/HOL/Word/BinBoolList.thy
changeset 24465 70f0214b3ecc
parent 24399 371f8c6b2101
child 25112 98824cc791c0
equal deleted inserted replaced
24464:58d24cbe5fa6 24465:70f0214b3ecc
     9 
     9 
    10 header "Bool lists and integers"
    10 header "Bool lists and integers"
    11 
    11 
    12 theory BinBoolList imports BinOperations begin
    12 theory BinBoolList imports BinOperations begin
    13 
    13 
    14 subsection "Lemmas about standard list operations"
    14 subsection "Arithmetic in terms of bool lists"
       
    15 
       
    16 consts (* arithmetic operations in terms of the reversed bool list,
       
    17   assuming input list(s) the same length, and don't extend them *)
       
    18   rbl_succ :: "bool list => bool list"
       
    19   rbl_pred :: "bool list => bool list"
       
    20   rbl_add :: "bool list => bool list => bool list"
       
    21   rbl_mult :: "bool list => bool list => bool list"
       
    22 
       
    23 primrec 
       
    24   Nil: "rbl_succ Nil = Nil"
       
    25   Cons: "rbl_succ (x # xs) = (if x then False # rbl_succ xs else True # xs)"
       
    26 
       
    27 primrec 
       
    28   Nil : "rbl_pred Nil = Nil"
       
    29   Cons : "rbl_pred (x # xs) = (if x then False # xs else True # rbl_pred xs)"
       
    30 
       
    31 primrec (* result is length of first arg, second arg may be longer *)
       
    32   Nil : "rbl_add Nil x = Nil"
       
    33   Cons : "rbl_add (y # ys) x = (let ws = rbl_add ys (tl x) in 
       
    34     (y ~= hd x) # (if hd x & y then rbl_succ ws else ws))"
       
    35 
       
    36 primrec (* result is length of first arg, second arg may be longer *)
       
    37   Nil : "rbl_mult Nil x = Nil"
       
    38   Cons : "rbl_mult (y # ys) x = (let ws = False # rbl_mult ys x in 
       
    39     if y then rbl_add ws x else ws)"
    15 
    40 
    16 lemma tl_take: "tl (take n l) = take (n - 1) (tl l)"
    41 lemma tl_take: "tl (take n l) = take (n - 1) (tl l)"
    17   apply (cases n, clarsimp)
    42   apply (cases n, clarsimp)
    18   apply (cases l, auto)
    43   apply (cases l, auto)
    19   done
    44   done
    48 
    73 
    49 lemma butlast_power:
    74 lemma butlast_power:
    50   "(butlast ^ n) bl = take (length bl - n) bl"
    75   "(butlast ^ n) bl = take (length bl - n) bl"
    51   by (induct n) (auto simp: butlast_take)
    76   by (induct n) (auto simp: butlast_take)
    52 
    77 
    53 lemma nth_rev [rule_format] : 
       
    54   "n < length xs --> rev xs ! n = xs ! (length xs - 1 - n)"
       
    55   apply (induct_tac "xs")
       
    56    apply simp
       
    57   apply (clarsimp simp add : nth_append nth.simps split add : nat.split)
       
    58   apply (rule_tac f = "%n. list ! n" in arg_cong) 
       
    59   apply arith
       
    60   done
       
    61 
       
    62 lemmas nth_rev_alt = nth_rev [where xs = "rev ?ys", simplified]
       
    63 
       
    64 lemma hd_butlast: "size xs > 1 ==> hd (butlast xs) = hd xs"
       
    65   by (cases xs) auto
       
    66 
       
    67 subsection "From integers to bool lists"
       
    68 
       
    69 consts
       
    70   bin_to_bl :: "nat => int => bool list"
       
    71   bin_to_bl_aux :: "nat => int => bool list => bool list"
       
    72 
       
    73 primrec
       
    74   Z : "bin_to_bl_aux 0 w bl = bl"
       
    75   Suc : "bin_to_bl_aux (Suc n) w bl =
       
    76     bin_to_bl_aux n (bin_rest w) ((bin_last w = bit.B1) # bl)"
       
    77 
       
    78 defs
       
    79   bin_to_bl_def : "bin_to_bl n w == bin_to_bl_aux n w []"
       
    80 
       
    81 lemma bin_to_bl_aux_Pls_minus_simp:
    78 lemma bin_to_bl_aux_Pls_minus_simp:
    82   "0 < n ==> bin_to_bl_aux n Numeral.Pls bl = 
    79   "0 < n ==> bin_to_bl_aux n Numeral.Pls bl = 
    83     bin_to_bl_aux (n - 1) Numeral.Pls (False # bl)"
    80     bin_to_bl_aux (n - 1) Numeral.Pls (False # bl)"
    84   by (cases n) auto
    81   by (cases n) auto
    85 
    82 
    95 
    92 
    96 declare bin_to_bl_aux_Pls_minus_simp [simp]
    93 declare bin_to_bl_aux_Pls_minus_simp [simp]
    97   bin_to_bl_aux_Min_minus_simp [simp]
    94   bin_to_bl_aux_Min_minus_simp [simp]
    98   bin_to_bl_aux_Bit_minus_simp [simp]
    95   bin_to_bl_aux_Bit_minus_simp [simp]
    99 
    96 
       
    97 (** link between bin and bool list **)
       
    98 
       
    99 lemma bl_to_bin_aux_append [rule_format] : 
       
   100   "ALL w. bl_to_bin_aux w (bs @ cs) = bl_to_bin_aux (bl_to_bin_aux w bs) cs"
       
   101   by (induct bs) auto
       
   102 
   100 lemma bin_to_bl_aux_append [rule_format] : 
   103 lemma bin_to_bl_aux_append [rule_format] : 
   101   "ALL w bs. bin_to_bl_aux n w bs @ cs = bin_to_bl_aux n w (bs @ cs)"
   104   "ALL w bs. bin_to_bl_aux n w bs @ cs = bin_to_bl_aux n w (bs @ cs)"
   102   by (induct n) auto
   105   by (induct n) auto
   103 
   106 
       
   107 lemma bl_to_bin_append: 
       
   108   "bl_to_bin (bs @ cs) = bl_to_bin_aux (bl_to_bin bs) cs"
       
   109   unfolding bl_to_bin_def by (rule bl_to_bin_aux_append)
       
   110 
   104 lemma bin_to_bl_aux_alt: 
   111 lemma bin_to_bl_aux_alt: 
   105   "bin_to_bl_aux n w bs = bin_to_bl n w @ bs" 
   112   "bin_to_bl_aux n w bs = bin_to_bl n w @ bs" 
   106   unfolding bin_to_bl_def by (simp add : bin_to_bl_aux_append)
   113   unfolding bin_to_bl_def by (simp add : bin_to_bl_aux_append)
   107 
   114 
   108 lemma bin_to_bl_0 [simp]: "bin_to_bl 0 bs = []"
   115 lemma bin_to_bl_0: "bin_to_bl 0 bs = []"
   109   unfolding bin_to_bl_def by auto
   116   unfolding bin_to_bl_def by auto
   110 
   117 
   111 lemma size_bin_to_bl_aux [rule_format] : 
   118 lemma size_bin_to_bl_aux [rule_format] : 
   112   "ALL w bs. size (bin_to_bl_aux n w bs) = n + length bs"
   119   "ALL w bs. size (bin_to_bl_aux n w bs) = n + length bs"
   113   by (induct n) auto
   120   by (induct n) auto
   114 
   121 
   115 lemma size_bin_to_bl [simp]: "size (bin_to_bl n w) = n" 
   122 lemma size_bin_to_bl: "size (bin_to_bl n w) = n" 
   116   unfolding bin_to_bl_def by (simp add : size_bin_to_bl_aux)
   123   unfolding bin_to_bl_def by (simp add : size_bin_to_bl_aux)
   117 
       
   118 lemma bin_to_bl_Pls_aux [rule_format] : 
       
   119   "ALL bl. bin_to_bl_aux n Numeral.Pls bl = replicate n False @ bl"
       
   120   by (induct n) (auto simp: replicate_app_Cons_same)
       
   121 
       
   122 lemma bin_to_bl_Pls: "bin_to_bl n Numeral.Pls = replicate n False"
       
   123   unfolding bin_to_bl_def by (simp add : bin_to_bl_Pls_aux)
       
   124 
       
   125 lemma bin_to_bl_Min_aux [rule_format] : 
       
   126   "ALL bl. bin_to_bl_aux n Numeral.Min bl = replicate n True @ bl"
       
   127   by (induct n) (auto simp: replicate_app_Cons_same)
       
   128 
       
   129 lemma bin_to_bl_Min: "bin_to_bl n Numeral.Min = replicate n True"
       
   130   unfolding bin_to_bl_def by (simp add : bin_to_bl_Min_aux)
       
   131 
       
   132 lemma bin_to_bl_aux_bintr [rule_format] :
       
   133   "ALL m bin bl. bin_to_bl_aux n (bintrunc m bin) bl = 
       
   134     replicate (n - m) False @ bin_to_bl_aux (min n m) bin bl"
       
   135   apply (induct_tac "n")
       
   136    apply clarsimp
       
   137   apply clarsimp
       
   138   apply (case_tac "m")
       
   139    apply (clarsimp simp: bin_to_bl_Pls_aux) 
       
   140    apply (erule thin_rl)
       
   141    apply (induct_tac n)   
       
   142     apply auto
       
   143   done
       
   144 
       
   145 lemmas bin_to_bl_bintr = 
       
   146   bin_to_bl_aux_bintr [where bl = "[]", folded bin_to_bl_def]
       
   147 
       
   148 lemma len_bin_to_bl_aux [rule_format] : 
       
   149   "ALL w bs. length (bin_to_bl_aux n w bs) = n + length bs"
       
   150   by (induct "n") auto
       
   151 
       
   152 lemma len_bin_to_bl [simp]: "length (bin_to_bl n w) = n"
       
   153   unfolding bin_to_bl_def len_bin_to_bl_aux by auto
       
   154 
       
   155 
       
   156 subsection "From bool lists to integers"
       
   157 
       
   158 consts
       
   159   bl_to_bin :: "bool list => int"
       
   160   bl_to_bin_aux :: "int => bool list => int"
       
   161 
       
   162 primrec
       
   163   Nil : "bl_to_bin_aux w [] = w"
       
   164   Cons : "bl_to_bin_aux w (b # bs) = 
       
   165       bl_to_bin_aux (w BIT (if b then bit.B1 else bit.B0)) bs"
       
   166 
       
   167 defs
       
   168   bl_to_bin_def : "bl_to_bin bs == bl_to_bin_aux Numeral.Pls bs"
       
   169 
       
   170 (** link between bin and bool list **)
       
   171 
       
   172 lemma bl_to_bin_aux_append [rule_format] : 
       
   173   "ALL w. bl_to_bin_aux w (bs @ cs) = bl_to_bin_aux (bl_to_bin_aux w bs) cs"
       
   174   by (induct bs) auto
       
   175 
       
   176 lemma bl_to_bin_append: 
       
   177   "bl_to_bin (bs @ cs) = bl_to_bin_aux (bl_to_bin bs) cs"
       
   178   unfolding bl_to_bin_def by (rule bl_to_bin_aux_append)
       
   179 
       
   180 lemma bl_to_bin_False [simp]: "bl_to_bin (False # bl) = bl_to_bin bl"
       
   181   unfolding bl_to_bin_def by auto
       
   182   
       
   183 lemma bl_to_bin_Nil [simp]: "bl_to_bin [] = Numeral.Pls"
       
   184   unfolding bl_to_bin_def by auto
       
   185 
       
   186 lemma bl_to_bin_rep_F: 
       
   187   "bl_to_bin (replicate n False @ bl) = bl_to_bin bl"
       
   188   by (induct n) auto
       
   189 
       
   190 lemma bl_to_bin_rep_False: "bl_to_bin (replicate n False) = Numeral.Pls"
       
   191   by (induct n) auto
       
   192 
       
   193 
       
   194 subsection "Converting between bool lists and integers"
       
   195 
   124 
   196 lemma bin_bl_bin' [rule_format] : 
   125 lemma bin_bl_bin' [rule_format] : 
   197   "ALL w bs. bl_to_bin (bin_to_bl_aux n w bs) = 
   126   "ALL w bs. bl_to_bin (bin_to_bl_aux n w bs) = 
   198     bl_to_bin_aux (bintrunc n w) bs"
   127     bl_to_bin_aux (bintrunc n w) bs"
   199   by (induct n) (auto simp add : bl_to_bin_def)
   128   by (induct n) (auto simp add : bl_to_bin_def)
   200 
   129 
   201 lemma bin_bl_bin [simp]: "bl_to_bin (bin_to_bl n w) = bintrunc n w"
   130 lemma bin_bl_bin: "bl_to_bin (bin_to_bl n w) = bintrunc n w"
   202   unfolding bin_to_bl_def bin_bl_bin' by auto
   131   unfolding bin_to_bl_def bin_bl_bin' by auto
   203 
   132 
   204 lemma bl_bin_bl' [rule_format] :
   133 lemma bl_bin_bl' [rule_format] :
   205   "ALL w n. bin_to_bl (n + length bs) (bl_to_bin_aux w bs) = 
   134   "ALL w n. bin_to_bl (n + length bs) (bl_to_bin_aux w bs) = 
   206     bin_to_bl_aux n w bs"
   135     bin_to_bl_aux n w bs"
   208    apply auto
   137    apply auto
   209     apply (simp_all only : add_Suc [symmetric])
   138     apply (simp_all only : add_Suc [symmetric])
   210     apply (auto simp add : bin_to_bl_def)
   139     apply (auto simp add : bin_to_bl_def)
   211   done
   140   done
   212 
   141 
   213 lemma bl_bin_bl [simp]: "bin_to_bl (length bs) (bl_to_bin bs) = bs"
   142 lemma bl_bin_bl: "bin_to_bl (length bs) (bl_to_bin bs) = bs"
   214   unfolding bl_to_bin_def
   143   unfolding bl_to_bin_def
   215   apply (rule box_equals)
   144   apply (rule box_equals)
   216     apply (rule bl_bin_bl')
   145     apply (rule bl_bin_bl')
   217    prefer 2
   146    prefer 2
   218    apply (rule bin_to_bl_aux.Z)
   147    apply (rule bin_to_bl_aux.Z)
   219   apply simp
   148   apply simp
   220   done
   149   done
   221   
   150   
       
   151 declare 
       
   152   bin_to_bl_0 [simp] 
       
   153   size_bin_to_bl [simp] 
       
   154   bin_bl_bin [simp] 
       
   155   bl_bin_bl [simp]
       
   156 
   222 lemma bl_to_bin_inj:
   157 lemma bl_to_bin_inj:
   223   "bl_to_bin bs = bl_to_bin cs ==> length bs = length cs ==> bs = cs"
   158   "bl_to_bin bs = bl_to_bin cs ==> length bs = length cs ==> bs = cs"
   224   apply (rule_tac box_equals)
   159   apply (rule_tac box_equals)
   225     defer
   160     defer
   226     apply (rule bl_bin_bl)
   161     apply (rule bl_bin_bl)
   227    apply (rule bl_bin_bl)
   162    apply (rule bl_bin_bl)
   228   apply simp
   163   apply simp
   229   done
   164   done
   230 
   165 
   231 lemma bin_to_bl_trunc [simp]:
   166 lemma bl_to_bin_False: "bl_to_bin (False # bl) = bl_to_bin bl"
       
   167   unfolding bl_to_bin_def by auto
       
   168   
       
   169 lemma bl_to_bin_Nil: "bl_to_bin [] = Numeral.Pls"
       
   170   unfolding bl_to_bin_def by auto
       
   171 
       
   172 lemma bin_to_bl_Pls_aux [rule_format] : 
       
   173   "ALL bl. bin_to_bl_aux n Numeral.Pls bl = replicate n False @ bl"
       
   174   by (induct n) (auto simp: replicate_app_Cons_same)
       
   175 
       
   176 lemma bin_to_bl_Pls: "bin_to_bl n Numeral.Pls = replicate n False"
       
   177   unfolding bin_to_bl_def by (simp add : bin_to_bl_Pls_aux)
       
   178 
       
   179 lemma bin_to_bl_Min_aux [rule_format] : 
       
   180   "ALL bl. bin_to_bl_aux n Numeral.Min bl = replicate n True @ bl"
       
   181   by (induct n) (auto simp: replicate_app_Cons_same)
       
   182 
       
   183 lemma bin_to_bl_Min: "bin_to_bl n Numeral.Min = replicate n True"
       
   184   unfolding bin_to_bl_def by (simp add : bin_to_bl_Min_aux)
       
   185 
       
   186 lemma bl_to_bin_rep_F: 
       
   187   "bl_to_bin (replicate n False @ bl) = bl_to_bin bl"
       
   188   apply (simp add: bin_to_bl_Pls_aux [symmetric] bin_bl_bin')
       
   189   apply (simp add: bl_to_bin_def)
       
   190   done
       
   191 
       
   192 lemma bin_to_bl_trunc:
   232   "n <= m ==> bin_to_bl n (bintrunc m w) = bin_to_bl n w"
   193   "n <= m ==> bin_to_bl n (bintrunc m w) = bin_to_bl n w"
   233   by (auto intro: bl_to_bin_inj)
   194   by (auto intro: bl_to_bin_inj)
   234 
   195 
   235 subsection "@{term bin_sign} with bool list operations"
   196 declare 
   236 
   197   bin_to_bl_trunc [simp] 
       
   198   bl_to_bin_False [simp] 
       
   199   bl_to_bin_Nil [simp]
       
   200 
       
   201 lemma bin_to_bl_aux_bintr [rule_format] :
       
   202   "ALL m bin bl. bin_to_bl_aux n (bintrunc m bin) bl = 
       
   203     replicate (n - m) False @ bin_to_bl_aux (min n m) bin bl"
       
   204   apply (induct_tac "n")
       
   205    apply clarsimp
       
   206   apply clarsimp
       
   207   apply (case_tac "m")
       
   208    apply (clarsimp simp: bin_to_bl_Pls_aux) 
       
   209    apply (erule thin_rl)
       
   210    apply (induct_tac n)   
       
   211     apply auto
       
   212   done
       
   213 
       
   214 lemmas bin_to_bl_bintr = 
       
   215   bin_to_bl_aux_bintr [where bl = "[]", folded bin_to_bl_def]
       
   216 
       
   217 lemma bl_to_bin_rep_False: "bl_to_bin (replicate n False) = Numeral.Pls"
       
   218   by (induct n) auto
       
   219 
       
   220 lemma len_bin_to_bl_aux [rule_format] : 
       
   221   "ALL w bs. length (bin_to_bl_aux n w bs) = n + length bs"
       
   222   by (induct "n") auto
       
   223 
       
   224 lemma len_bin_to_bl [simp]: "length (bin_to_bl n w) = n"
       
   225   unfolding bin_to_bl_def len_bin_to_bl_aux by auto
       
   226   
   237 lemma sign_bl_bin' [rule_format] : 
   227 lemma sign_bl_bin' [rule_format] : 
   238   "ALL w. bin_sign (bl_to_bin_aux w bs) = bin_sign w"
   228   "ALL w. bin_sign (bl_to_bin_aux w bs) = bin_sign w"
   239   by (induct bs) auto
   229   by (induct bs) auto
   240   
   230   
   241 lemma sign_bl_bin: "bin_sign (bl_to_bin bs) = Numeral.Pls"
   231 lemma sign_bl_bin: "bin_sign (bl_to_bin bs) = Numeral.Pls"
   253     
   243     
   254 lemma bl_sbin_sign: 
   244 lemma bl_sbin_sign: 
   255   "hd (bin_to_bl (Suc n) w) = (bin_sign (sbintrunc n w) = Numeral.Min)"
   245   "hd (bin_to_bl (Suc n) w) = (bin_sign (sbintrunc n w) = Numeral.Min)"
   256   unfolding bin_to_bl_def by (rule bl_sbin_sign_aux)
   246   unfolding bin_to_bl_def by (rule bl_sbin_sign_aux)
   257 
   247 
   258 subsection "@{term bin_nth} with bool list operations"
       
   259 
       
   260 lemma bin_nth_of_bl_aux [rule_format] : 
   248 lemma bin_nth_of_bl_aux [rule_format] : 
   261   "ALL w. bin_nth (bl_to_bin_aux w bl) n = 
   249   "ALL w. bin_nth (bl_to_bin_aux w bl) n = 
   262     (n < size bl & rev bl ! n | n >= length bl & bin_nth w (n - size bl))"
   250     (n < size bl & rev bl ! n | n >= length bl & bin_nth w (n - size bl))"
   263   apply (induct_tac bl)
   251   apply (induct_tac bl)
   264    apply clarsimp
   252    apply clarsimp
   282   apply (case_tac m, clarsimp)
   270   apply (case_tac m, clarsimp)
   283   apply (clarsimp simp: bin_to_bl_def)
   271   apply (clarsimp simp: bin_to_bl_def)
   284   apply (simp add: bin_to_bl_aux_alt)
   272   apply (simp add: bin_to_bl_aux_alt)
   285   done
   273   done
   286 
   274 
       
   275 lemma nth_rev [rule_format] : 
       
   276   "n < length xs --> rev xs ! n = xs ! (length xs - 1 - n)"
       
   277   apply (induct_tac "xs")
       
   278    apply simp
       
   279   apply (clarsimp simp add : nth_append nth.simps split add : nat.split)
       
   280   apply (rule_tac f = "%n. list ! n" in arg_cong) 
       
   281   apply arith
       
   282   done
       
   283 
       
   284 lemmas nth_rev_alt = nth_rev [where xs = "rev ?ys", simplified]
       
   285 
   287 lemma nth_bin_to_bl_aux [rule_format] : 
   286 lemma nth_bin_to_bl_aux [rule_format] : 
   288   "ALL w n bl. n < m + length bl --> (bin_to_bl_aux m w bl) ! n = 
   287   "ALL w n bl. n < m + length bl --> (bin_to_bl_aux m w bl) ! n = 
   289     (if n < m then bin_nth w (m - 1 - n) else bl ! (n - m))"
   288     (if n < m then bin_nth w (m - 1 - n) else bl ! (n - m))"
   290   apply (induct_tac "m")
   289   apply (induct_tac "m")
   291    apply clarsimp
   290    apply clarsimp
   299   apply arith
   298   apply arith
   300   done
   299   done
   301   
   300   
   302 lemma nth_bin_to_bl: "n < m ==> (bin_to_bl m w) ! n = bin_nth w (m - Suc n)"
   301 lemma nth_bin_to_bl: "n < m ==> (bin_to_bl m w) ! n = bin_nth w (m - Suc n)"
   303   unfolding bin_to_bl_def by (simp add : nth_bin_to_bl_aux)
   302   unfolding bin_to_bl_def by (simp add : nth_bin_to_bl_aux)
   304 
       
   305 subsection "Ordering with bool list operations"
       
   306 
   303 
   307 lemma bl_to_bin_lt2p_aux [rule_format] : 
   304 lemma bl_to_bin_lt2p_aux [rule_format] : 
   308   "ALL w. bl_to_bin_aux w bs < (w + 1) * (2 ^ length bs)"
   305   "ALL w. bl_to_bin_aux w bs < (w + 1) * (2 ^ length bs)"
   309   apply (induct "bs")
   306   apply (induct "bs")
   310    apply clarsimp
   307    apply clarsimp
   337   apply (rule xtr4)
   334   apply (rule xtr4)
   338    apply (rule bl_to_bin_ge2p_aux)
   335    apply (rule bl_to_bin_ge2p_aux)
   339   apply simp
   336   apply simp
   340   done
   337   done
   341 
   338 
   342 subsection "@{term butlast} with bool list operations"
       
   343 
       
   344 lemma butlast_rest_bin: 
   339 lemma butlast_rest_bin: 
   345   "butlast (bin_to_bl n w) = bin_to_bl (n - 1) (bin_rest w)"
   340   "butlast (bin_to_bl n w) = bin_to_bl (n - 1) (bin_rest w)"
   346   apply (unfold bin_to_bl_def)
   341   apply (unfold bin_to_bl_def)
   347   apply (cases w rule: bin_exhaust)
   342   apply (cases w rule: bin_exhaust)
   348   apply (cases n, clarsimp)
   343   apply (cases n, clarsimp)
   362   "bl_to_bin (butlast bl) = bin_rest (bl_to_bin bl)"
   357   "bl_to_bin (butlast bl) = bin_rest (bl_to_bin bl)"
   363   apply (unfold bl_to_bin_def)
   358   apply (unfold bl_to_bin_def)
   364   apply (cases bl)
   359   apply (cases bl)
   365    apply (auto simp add: butlast_rest_bl2bin_aux)
   360    apply (auto simp add: butlast_rest_bl2bin_aux)
   366   done
   361   done
   367 
       
   368 subsection "Truncation"
       
   369 
   362 
   370 lemma trunc_bl2bin_aux [rule_format] : 
   363 lemma trunc_bl2bin_aux [rule_format] : 
   371   "ALL w. bintrunc m (bl_to_bin_aux w bl) = 
   364   "ALL w. bintrunc m (bl_to_bin_aux w bl) = 
   372     bl_to_bin_aux (bintrunc (m - length bl) w) (drop (length bl - m) bl)"
   365     bl_to_bin_aux (bintrunc (m - length bl) w) (drop (length bl - m) bl)"
   373   apply (induct_tac bl)
   366   apply (induct_tac bl)
   402    apply (rule trunc_bl2bin [symmetric])
   395    apply (rule trunc_bl2bin [symmetric])
   403   apply (cases "k <= length bl")
   396   apply (cases "k <= length bl")
   404    apply auto
   397    apply auto
   405   done
   398   done
   406 
   399 
   407 (* TODO: This is not related to bool lists; should be moved *)
       
   408 lemma nth_rest_power_bin [rule_format] :
   400 lemma nth_rest_power_bin [rule_format] :
   409   "ALL n. bin_nth ((bin_rest ^ k) w) n = bin_nth w (n + k)"
   401   "ALL n. bin_nth ((bin_rest ^ k) w) n = bin_nth w (n + k)"
   410   apply (induct k, clarsimp)
   402   apply (induct k, clarsimp)
   411   apply clarsimp
   403   apply clarsimp
   412   apply (simp only: bin_nth.Suc [symmetric] add_Suc)
   404   apply (simp only: bin_nth.Suc [symmetric] add_Suc)
   417   apply (rule nth_equalityI)
   409   apply (rule nth_equalityI)
   418    apply simp
   410    apply simp
   419   apply (clarsimp simp add: nth_bin_to_bl nth_rest_power_bin)
   411   apply (clarsimp simp add: nth_bin_to_bl nth_rest_power_bin)
   420   done
   412   done
   421 
   413 
   422 subsection "@{term last} with bool list operations"
   414 lemma hd_butlast: "size xs > 1 ==> hd (butlast xs) = hd xs"
       
   415   by (cases xs) auto
   423 
   416 
   424 lemma last_bin_last' [rule_format] : 
   417 lemma last_bin_last' [rule_format] : 
   425   "ALL w. size xs > 0 --> last xs = (bin_last (bl_to_bin_aux w xs) = bit.B1)" 
   418   "ALL w. size xs > 0 --> last xs = (bin_last (bl_to_bin_aux w xs) = bit.B1)" 
   426   by (induct xs) auto
   419   by (induct xs) auto
   427 
   420 
   434   apply (unfold bin_to_bl_def)
   427   apply (unfold bin_to_bl_def)
   435   apply simp
   428   apply simp
   436   apply (auto simp add: bin_to_bl_aux_alt)
   429   apply (auto simp add: bin_to_bl_aux_alt)
   437   done
   430   done
   438 
   431 
   439 subsection "Bit-wise operations on bool lists"
   432 (** links between bit-wise operations and operations on bool lists **)
   440 
   433     
   441 consts
       
   442   app2 :: "('a => 'b => 'c) => 'a list => 'b list => 'c list"
       
   443 
       
   444 defs
       
   445   app2_def : "app2 f as bs == map (split f) (zip as bs)"
       
   446 
       
   447 lemma app2_Nil [simp]: "app2 f [] ys = []"
   434 lemma app2_Nil [simp]: "app2 f [] ys = []"
   448   unfolding app2_def by auto
   435   unfolding app2_def by auto
   449 
   436 
   450 lemma app2_Cons [simp]:
   437 lemma app2_Cons [simp]:
   451   "app2 f (x # xs) (y # ys) = f x y # app2 f xs ys"
   438   "app2 f (x # xs) (y # ys) = f x y # app2 f xs ys"
   511                                   unfolded app2_Nil, folded bin_to_bl_def]
   498                                   unfolded app2_Nil, folded bin_to_bl_def]
   512 
   499 
   513 lemmas bl_xor_bin = bl_xor_aux_bin [where bs="[]" and cs="[]", 
   500 lemmas bl_xor_bin = bl_xor_aux_bin [where bs="[]" and cs="[]", 
   514                                     unfolded app2_Nil, folded bin_to_bl_def]
   501                                     unfolded app2_Nil, folded bin_to_bl_def]
   515 
   502 
   516 subsection "@{term drop} with bool list operations"
       
   517 
       
   518 lemma drop_bin2bl_aux [rule_format] : 
   503 lemma drop_bin2bl_aux [rule_format] : 
   519   "ALL m bin bs. drop m (bin_to_bl_aux n bin bs) = 
   504   "ALL m bin bs. drop m (bin_to_bl_aux n bin bs) = 
   520     bin_to_bl_aux (n - m) bin (drop (m - n) bs)"
   505     bin_to_bl_aux (n - m) bin (drop (m - n) bs)"
   521   apply (induct n, clarsimp)
   506   apply (induct n, clarsimp)
   522   apply clarsimp
   507   apply clarsimp
   547    apply clarify
   532    apply clarify
   548    apply (simp_all (no_asm) add: bin_to_bl_def take_bin2bl_lem1)
   533    apply (simp_all (no_asm) add: bin_to_bl_def take_bin2bl_lem1)
   549   apply simp
   534   apply simp
   550   done
   535   done
   551 
   536 
   552 subsection "@{term bin_split} with bool list operations"
       
   553 
       
   554 lemma bin_split_take [rule_format] : 
   537 lemma bin_split_take [rule_format] : 
   555   "ALL b c. bin_split n c = (a, b) --> 
   538   "ALL b c. bin_split n c = (a, b) --> 
   556     bin_to_bl m a = take m (bin_to_bl (m + n) c)"
   539     bin_to_bl m a = take m (bin_to_bl (m + n) c)"
   557   apply (induct n)
   540   apply (induct n)
   558    apply clarsimp
   541    apply clarsimp
   564 lemma bin_split_take1: 
   547 lemma bin_split_take1: 
   565   "k = m + n ==> bin_split n c = (a, b) ==> 
   548   "k = m + n ==> bin_split n c = (a, b) ==> 
   566     bin_to_bl m a = take m (bin_to_bl k c)"
   549     bin_to_bl m a = take m (bin_to_bl k c)"
   567   by (auto elim: bin_split_take)
   550   by (auto elim: bin_split_take)
   568   
   551   
   569 subsection "@{term takefill}"
       
   570 
       
   571 consts
       
   572   takefill :: "'a => nat => 'a list => 'a list"
       
   573 
       
   574 -- "takefill - like take but if argument list too short,"
       
   575 -- "extends result to get requested length"
       
   576 primrec
       
   577   Z : "takefill fill 0 xs = []"
       
   578   Suc : "takefill fill (Suc n) xs = (
       
   579     case xs of [] => fill # takefill fill n xs
       
   580       | y # ys => y # takefill fill n ys)"
       
   581 
       
   582 lemma nth_takefill [rule_format] : "ALL m l. m < n --> 
   552 lemma nth_takefill [rule_format] : "ALL m l. m < n --> 
   583     takefill fill n l ! m = (if m < length l then l ! m else fill)"
   553     takefill fill n l ! m = (if m < length l then l ! m else fill)"
   584   apply (induct n, clarsimp)
   554   apply (induct n, clarsimp)
   585   apply clarsimp
   555   apply clarsimp
   586   apply (case_tac m)
   556   apply (case_tac m)
   670   takefill_minus [symmetric, THEN trans], standard]
   640   takefill_minus [symmetric, THEN trans], standard]
   671 
   641 
   672 lemmas takefill_pred_simps [simp] =
   642 lemmas takefill_pred_simps [simp] =
   673   takefill_minus_simps [where n="number_of bin", simplified nobm1, standard]
   643   takefill_minus_simps [where n="number_of bin", simplified nobm1, standard]
   674 
   644 
   675 subsection "@{term bin_cat} with bool list operations"
       
   676 
       
   677 (* links with function bl_to_bin *)
   645 (* links with function bl_to_bin *)
   678 
   646 
   679 lemma bl_to_bin_aux_cat: 
   647 lemma bl_to_bin_aux_cat: 
   680   "!!nv v. bl_to_bin_aux (bin_cat w nv v) bs = 
   648   "!!nv v. bl_to_bin_aux (bin_cat w nv v) bs = 
   681     bin_cat w (nv + length bs) (bl_to_bin_aux v bs)"
   649     bin_cat w (nv + length bs) (bl_to_bin_aux v bs)"
   721   apply (simp only: Suc_eq_add_numeral_1 replicate_add
   689   apply (simp only: Suc_eq_add_numeral_1 replicate_add
   722                     append_Cons [symmetric] bl_to_bin_aux_append)
   690                     append_Cons [symmetric] bl_to_bin_aux_append)
   723   apply simp
   691   apply simp
   724   done
   692   done
   725 
   693 
   726 subsection "function @{term bl_of_nth}"
   694 (* function bl_of_nth *)
   727 
       
   728 consts
       
   729   bl_of_nth :: "nat => (nat => bool) => bool list"
       
   730 
       
   731 primrec
       
   732   Suc : "bl_of_nth (Suc n) f = f n # bl_of_nth n f"
       
   733   Z : "bl_of_nth 0 f = []"
       
   734 
       
   735 lemma length_bl_of_nth [simp]: "length (bl_of_nth n f) = n"
   695 lemma length_bl_of_nth [simp]: "length (bl_of_nth n f) = n"
   736   by (induct n)  auto
   696   by (induct n)  auto
   737 
   697 
   738 lemma nth_bl_of_nth [simp]:
   698 lemma nth_bl_of_nth [simp]:
   739   "m < n \<Longrightarrow> rev (bl_of_nth n f) ! m = f m"
   699   "m < n \<Longrightarrow> rev (bl_of_nth n f) ! m = f m"
   761   apply (rule_tac f = "%n. drop n xs" in arg_cong) 
   721   apply (rule_tac f = "%n. drop n xs" in arg_cong) 
   762   apply simp
   722   apply simp
   763   done
   723   done
   764 
   724 
   765 lemmas bl_of_nth_nth [simp] = order_refl [THEN bl_of_nth_nth_le, simplified]
   725 lemmas bl_of_nth_nth [simp] = order_refl [THEN bl_of_nth_nth_le, simplified]
   766 
       
   767 subsection "Arithmetic in terms of bool lists"
       
   768 
       
   769 consts (* arithmetic operations in terms of the reversed bool list,
       
   770   assuming input list(s) the same length, and don't extend them *)
       
   771   rbl_succ :: "bool list => bool list"
       
   772   rbl_pred :: "bool list => bool list"
       
   773   rbl_add :: "bool list => bool list => bool list"
       
   774   rbl_mult :: "bool list => bool list => bool list"
       
   775 
       
   776 primrec 
       
   777   Nil: "rbl_succ Nil = Nil"
       
   778   Cons: "rbl_succ (x # xs) = (if x then False # rbl_succ xs else True # xs)"
       
   779 
       
   780 primrec 
       
   781   Nil : "rbl_pred Nil = Nil"
       
   782   Cons : "rbl_pred (x # xs) = (if x then False # xs else True # rbl_pred xs)"
       
   783 
       
   784 primrec (* result is length of first arg, second arg may be longer *)
       
   785   Nil : "rbl_add Nil x = Nil"
       
   786   Cons : "rbl_add (y # ys) x = (let ws = rbl_add ys (tl x) in 
       
   787     (y ~= hd x) # (if hd x & y then rbl_succ ws else ws))"
       
   788 
       
   789 primrec (* result is length of first arg, second arg may be longer *)
       
   790   Nil : "rbl_mult Nil x = Nil"
       
   791   Cons : "rbl_mult (y # ys) x = (let ws = False # rbl_mult ys x in 
       
   792     if y then rbl_add ws x else ws)"
       
   793 
   726 
   794 lemma size_rbl_pred: "length (rbl_pred bl) = length bl"
   727 lemma size_rbl_pred: "length (rbl_pred bl) = length bl"
   795   by (induct bl) auto
   728   by (induct bl) auto
   796 
   729 
   797 lemma size_rbl_succ: "length (rbl_succ bl) = length bl"
   730 lemma size_rbl_succ: "length (rbl_succ bl) = length bl"
   945   "P (rbl_mult (y # ys) xs) = 
   878   "P (rbl_mult (y # ys) xs) = 
   946     (ALL ws. length ws = Suc (length ys) --> ws = False # rbl_mult ys xs --> 
   879     (ALL ws. length ws = Suc (length ys) --> ws = False # rbl_mult ys xs --> 
   947     (y --> P (rbl_add ws xs)) & (~ y -->  P ws))"
   880     (y --> P (rbl_add ws xs)) & (~ y -->  P ws))"
   948   by (clarsimp simp add : Let_def)
   881   by (clarsimp simp add : Let_def)
   949   
   882   
   950 subsection "Miscellaneous lemmas"
       
   951 
       
   952 lemma and_len: "xs = ys ==> xs = ys & length xs = length ys"
   883 lemma and_len: "xs = ys ==> xs = ys & length xs = length ys"
   953   by auto
   884   by auto
   954 
   885 
   955 lemma size_if: "size (if p then xs else ys) = (if p then size xs else size ys)"
   886 lemma size_if: "size (if p then xs else ys) = (if p then size xs else size ys)"
   956   by auto
   887   by auto
   957 
   888 
   958 lemma tl_if: "tl (if p then xs else ys) = (if p then tl xs else tl ys)"
   889 lemma tl_if: "tl (if p then xs else ys) = (if p then tl xs else tl ys)"
   959   by auto
   890   by auto
   960 
   891 
   961 lemma hd_if: "hd (if p then xs else ys) = (if p then hd xs else hd ys)"
   892 lemma hd_if: "hd (if p then xs else ys) = (if p then hd xs else hd ys)"
       
   893   by auto
       
   894 
       
   895 lemma if_Not_x: "(if p then ~ x else x) = (p = (~ x))"
       
   896   by auto
       
   897 
       
   898 lemma if_x_Not: "(if p then x else ~ x) = (p = x)"
   962   by auto
   899   by auto
   963 
   900 
   964 lemma if_same_and: "(If p x y & If p u v) = (if p then x & u else y & v)"
   901 lemma if_same_and: "(If p x y & If p u v) = (if p then x & u else y & v)"
   965   by auto
   902   by auto
   966 
   903 
   977   by auto
   914   by auto
   978 
   915 
   979 lemma if_single:
   916 lemma if_single:
   980   "(if xc then [xab] else [an]) = [if xc then xab else an]"
   917   "(if xc then [xab] else [an]) = [if xc then xab else an]"
   981   by auto
   918   by auto
       
   919 
       
   920 lemma if_bool_simps:
       
   921   "If p True y = (p | y) & If p False y = (~p & y) & 
       
   922     If p y True = (p --> y) & If p y False = (p & y)"
       
   923   by auto
       
   924 
       
   925 lemmas if_simps = if_x_Not if_Not_x if_cancel if_True if_False if_bool_simps
   982 
   926 
   983 lemmas seqr = eq_reflection [where x = "size ?w"]
   927 lemmas seqr = eq_reflection [where x = "size ?w"]
   984 
   928 
   985 lemmas tl_Nil = tl.simps (1)
   929 lemmas tl_Nil = tl.simps (1)
   986 lemmas tl_Cons = tl.simps (2)
   930 lemmas tl_Cons = tl.simps (2)
  1068   "(0::nat) < number_of bin \<Longrightarrow>
  1012   "(0::nat) < number_of bin \<Longrightarrow>
  1069   bin_split (number_of bin) w =
  1013   bin_split (number_of bin) w =
  1070   (let (w1, w2) = bin_split (number_of (Numeral.pred bin)) (bin_rest w)
  1014   (let (w1, w2) = bin_split (number_of (Numeral.pred bin)) (bin_rest w)
  1071    in (w1, w2 BIT bin_last w))" 
  1015    in (w1, w2 BIT bin_last w))" 
  1072   by (simp only: nobm1 bin_split_minus_simp)
  1016   by (simp only: nobm1 bin_split_minus_simp)
       
  1017 
       
  1018 declare bin_split_pred_simp [simp]
  1073 
  1019 
  1074 lemma bin_rsplit_aux_simp_alt:
  1020 lemma bin_rsplit_aux_simp_alt:
  1075   "bin_rsplit_aux (n, bs, m, c) =
  1021   "bin_rsplit_aux (n, bs, m, c) =
  1076    (if m = 0 \<or> n = 0 
  1022    (if m = 0 \<or> n = 0 
  1077    then bs
  1023    then bs