* HOL-Word:
authorkleing
Mon Aug 20 04:34:31 2007 +0200 (2007-08-20)
changeset 24333e77ea0ea7f2c
parent 24332 e3a2b75b1cf9
child 24334 22863f364531
* HOL-Word:
New extensive library and type for generic, fixed size machine
words, with arithemtic, bit-wise, shifting and rotating operations,
reflection into int, nat, and bool lists, automation for linear
arithmetic (by automatic reflection into nat or int), including
lemmas on overflow and monotonicity. Instantiated to all appropriate
arithmetic type classes, supporting automatic simplification of
numerals on all operations. Jointly developed by NICTA, Galois, and
PSU.

* still to do: README.html/document + moving some of the generic lemmas
to appropriate place in distribution
CONTRIBUTORS
NEWS
src/HOL/IsaMakefile
src/HOL/Word/BinBoolList.thy
src/HOL/Word/BinGeneral.thy
src/HOL/Word/BinOperations.thy
src/HOL/Word/BitSyntax.thy
src/HOL/Word/Num_Lemmas.thy
src/HOL/Word/ROOT.ML
src/HOL/Word/Size.thy
src/HOL/Word/TdThs.thy
src/HOL/Word/WordArith.thy
src/HOL/Word/WordBitwise.thy
src/HOL/Word/WordDefinition.thy
src/HOL/Word/WordExamples.thy
src/HOL/Word/WordGenLib.thy
src/HOL/Word/WordMain.thy
src/HOL/Word/WordShift.thy
     1.1 --- a/CONTRIBUTORS	Mon Aug 20 00:22:18 2007 +0200
     1.2 +++ b/CONTRIBUTORS	Mon Aug 20 04:34:31 2007 +0200
     1.3 @@ -7,6 +7,13 @@
     1.4  Contributions to Isabelle 2007
     1.5  ------------------------------
     1.6  
     1.7 +* August 2007: Jeremy Dawson, NICTA,
     1.8 +               Paul Graunke, Galois, 
     1.9 +               Brian Huffman, PSU,
    1.10 +               Gerwin Klein, NICTA, 
    1.11 +               John Matthews, Galois
    1.12 +  HOL-Word: a library for fixed-size machine words in Isabelle.
    1.13 +
    1.14  * August 2007: Brian Huffman, PSU
    1.15    HOL/Library/Boolean_Algebra and HOL/Library/Numeral_Type
    1.16  
     2.1 --- a/NEWS	Mon Aug 20 00:22:18 2007 +0200
     2.2 +++ b/NEWS	Mon Aug 20 04:34:31 2007 +0200
     2.3 @@ -635,6 +635,20 @@
     2.4  
     2.5  *** HOL ***
     2.6  
     2.7 +* HOL-Word:
     2.8 +  New extensive library and type for generic, fixed size machine
     2.9 +  words, with arithemtic, bit-wise, shifting and rotating operations,
    2.10 +  reflection into int, nat, and bool lists, automation for linear
    2.11 +  arithmetic (by automatic reflection into nat or int), including
    2.12 +  lemmas on overflow and monotonicity. Instantiated to all appropriate
    2.13 +  arithmetic type classes, supporting automatic simplification of
    2.14 +  numerals on all operations. Jointly developed by NICTA, Galois, and
    2.15 +  PSU.
    2.16 +
    2.17 +* Library/Boolean_Algebra: locales for abstract boolean algebras.
    2.18 +
    2.19 +* Library/Numeral_Type: numbers as types, e.g. TYPE(32).
    2.20 +
    2.21  * Code generator library theories:
    2.22    * Pretty_Int represents HOL integers by big integer literals in target
    2.23      languages.
     3.1 --- a/src/HOL/IsaMakefile	Mon Aug 20 00:22:18 2007 +0200
     3.2 +++ b/src/HOL/IsaMakefile	Mon Aug 20 04:34:31 2007 +0200
     3.3 @@ -6,7 +6,8 @@
     3.4  
     3.5  default: HOL
     3.6  generate: HOL-Complex-Generate-HOL HOL-Complex-Generate-HOLLight
     3.7 -images: HOL HOL-Algebra HOL-Complex HOL-Complex-Matrix HOL-Nominal TLA HOL4
     3.8 +images: HOL HOL-Algebra HOL-Complex HOL-Complex-Matrix HOL-Nominal \
     3.9 +        HOL-Word TLA HOL4
    3.10  
    3.11  #Note: keep targets sorted (except for HOL-Library)
    3.12  test: \
    3.13 @@ -804,6 +805,30 @@
    3.14  	@cd Nominal; $(ISATOOL) usedir $(OUT)/HOL-Nominal Examples
    3.15  
    3.16  
    3.17 +## HOL-Word
    3.18 +
    3.19 +HOL-Word: HOL $(OUT)/HOL-Word
    3.20 +
    3.21 +$(OUT)/HOL-Word: $(OUT)/HOL Word/ROOT.ML \
    3.22 +  Library/Infinite_Set.thy Library/Parity.thy \
    3.23 +  Library/Boolean_Algebra.thy Library/Numeral_Type.thy \
    3.24 +  Word/Num_Lemmas.thy \
    3.25 +  Word/TdThs.thy \
    3.26 +  Word/Size.thy \
    3.27 +  Word/BinGeneral.thy \
    3.28 +  Word/BinOperations.thy \
    3.29 +  Word/BinBoolList.thy \
    3.30 +  Word/BitSyntax.thy \
    3.31 +  Word/WordDefinition.thy \
    3.32 +  Word/WordArith.thy \
    3.33 +  Word/WordBitwise.thy \
    3.34 +  Word/WordShift.thy \
    3.35 +  Word/WordGenLib.thy \
    3.36 +  Word/WordMain.thy \
    3.37 +  Word/WordExamples.thy
    3.38 +	@cd Word; $(ISATOOL) usedir -b -g true $(OUT)/HOL HOL-Word
    3.39 +
    3.40 +
    3.41  ## clean
    3.42  
    3.43  clean:
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/HOL/Word/BinBoolList.thy	Mon Aug 20 04:34:31 2007 +0200
     4.3 @@ -0,0 +1,1158 @@
     4.4 +(* 
     4.5 +  ID:     $Id$
     4.6 +  Author: Jeremy Dawson, NICTA
     4.7 +
     4.8 +  contains theorems to do with integers, expressed using Pls, Min, BIT,
     4.9 +  theorems linking them to lists of booleans, and repeated splitting 
    4.10 +  and concatenation.
    4.11 +*) 
    4.12 +
    4.13 +header "Bool lists and integers"
    4.14 +
    4.15 +theory BinBoolList imports BinOperations begin
    4.16 +
    4.17 +section "Arithmetic in terms of bool lists"
    4.18 +
    4.19 +consts (* arithmetic operations in terms of the reversed bool list,
    4.20 +  assuming input list(s) the same length, and don't extend them *)
    4.21 +  rbl_succ :: "bool list => bool list"
    4.22 +  rbl_pred :: "bool list => bool list"
    4.23 +  rbl_add :: "bool list => bool list => bool list"
    4.24 +  rbl_mult :: "bool list => bool list => bool list"
    4.25 +
    4.26 +primrec 
    4.27 +  Nil: "rbl_succ Nil = Nil"
    4.28 +  Cons: "rbl_succ (x # xs) = (if x then False # rbl_succ xs else True # xs)"
    4.29 +
    4.30 +primrec 
    4.31 +  Nil : "rbl_pred Nil = Nil"
    4.32 +  Cons : "rbl_pred (x # xs) = (if x then False # xs else True # rbl_pred xs)"
    4.33 +
    4.34 +primrec (* result is length of first arg, second arg may be longer *)
    4.35 +  Nil : "rbl_add Nil x = Nil"
    4.36 +  Cons : "rbl_add (y # ys) x = (let ws = rbl_add ys (tl x) in 
    4.37 +    (y ~= hd x) # (if hd x & y then rbl_succ ws else ws))"
    4.38 +
    4.39 +primrec (* result is length of first arg, second arg may be longer *)
    4.40 +  Nil : "rbl_mult Nil x = Nil"
    4.41 +  Cons : "rbl_mult (y # ys) x = (let ws = False # rbl_mult ys x in 
    4.42 +    if y then rbl_add ws x else ws)"
    4.43 +
    4.44 +lemma tl_take: "tl (take n l) = take (n - 1) (tl l)"
    4.45 +  apply (cases n, clarsimp)
    4.46 +  apply (cases l, auto)
    4.47 +  done
    4.48 +
    4.49 +lemma take_butlast [rule_format] :
    4.50 +  "ALL n. n < length l --> take n (butlast l) = take n l"
    4.51 +  apply (induct l, clarsimp)
    4.52 +  apply clarsimp
    4.53 +  apply (case_tac n)
    4.54 +  apply auto
    4.55 +  done
    4.56 +
    4.57 +lemma butlast_take [rule_format] :
    4.58 +  "ALL n. n <= length l --> butlast (take n l) = take (n - 1) l"
    4.59 +  apply (induct l, clarsimp)
    4.60 +  apply clarsimp
    4.61 +  apply (case_tac "n")
    4.62 +   apply safe
    4.63 +   apply simp_all
    4.64 +  apply (case_tac "nat")
    4.65 +   apply auto
    4.66 +  done
    4.67 +
    4.68 +lemma butlast_drop [rule_format] :
    4.69 +  "ALL n. butlast (drop n l) = drop n (butlast l)"
    4.70 +  apply (induct l)
    4.71 +   apply clarsimp
    4.72 +  apply clarsimp
    4.73 +  apply safe
    4.74 +   apply ((case_tac n, auto)[1])+
    4.75 +  done
    4.76 +
    4.77 +lemma butlast_power:
    4.78 +  "(butlast ^ n) bl = take (length bl - n) bl"
    4.79 +  by (induct n) (auto simp: butlast_take)
    4.80 +
    4.81 +lemma bin_to_bl_aux_Pls_minus_simp:
    4.82 +  "0 < n ==> bin_to_bl_aux n Numeral.Pls bl = 
    4.83 +    bin_to_bl_aux (n - 1) Numeral.Pls (False # bl)"
    4.84 +  by (cases n) auto
    4.85 +
    4.86 +lemma bin_to_bl_aux_Min_minus_simp:
    4.87 +  "0 < n ==> bin_to_bl_aux n Numeral.Min bl = 
    4.88 +    bin_to_bl_aux (n - 1) Numeral.Min (True # bl)"
    4.89 +  by (cases n) auto
    4.90 +
    4.91 +lemma bin_to_bl_aux_Bit_minus_simp:
    4.92 +  "0 < n ==> bin_to_bl_aux n (w BIT b) bl = 
    4.93 +    bin_to_bl_aux (n - 1) w ((b = bit.B1) # bl)"
    4.94 +  by (cases n) auto
    4.95 +
    4.96 +declare bin_to_bl_aux_Pls_minus_simp [simp]
    4.97 +  bin_to_bl_aux_Min_minus_simp [simp]
    4.98 +  bin_to_bl_aux_Bit_minus_simp [simp]
    4.99 +
   4.100 +(** link between bin and bool list **)
   4.101 +
   4.102 +lemma bl_to_bin_aux_append [rule_format] : 
   4.103 +  "ALL w. bl_to_bin_aux w (bs @ cs) = bl_to_bin_aux (bl_to_bin_aux w bs) cs"
   4.104 +  by (induct bs) auto
   4.105 +
   4.106 +lemma bin_to_bl_aux_append [rule_format] : 
   4.107 +  "ALL w bs. bin_to_bl_aux n w bs @ cs = bin_to_bl_aux n w (bs @ cs)"
   4.108 +  by (induct n) auto
   4.109 +
   4.110 +lemma bl_to_bin_append: 
   4.111 +  "bl_to_bin (bs @ cs) = bl_to_bin_aux (bl_to_bin bs) cs"
   4.112 +  unfolding bl_to_bin_def by (rule bl_to_bin_aux_append)
   4.113 +
   4.114 +lemma bin_to_bl_aux_alt: 
   4.115 +  "bin_to_bl_aux n w bs = bin_to_bl n w @ bs" 
   4.116 +  unfolding bin_to_bl_def by (simp add : bin_to_bl_aux_append)
   4.117 +
   4.118 +lemma bin_to_bl_0: "bin_to_bl 0 bs = []"
   4.119 +  unfolding bin_to_bl_def by auto
   4.120 +
   4.121 +lemma size_bin_to_bl_aux [rule_format] : 
   4.122 +  "ALL w bs. size (bin_to_bl_aux n w bs) = n + length bs"
   4.123 +  by (induct n) auto
   4.124 +
   4.125 +lemma size_bin_to_bl: "size (bin_to_bl n w) = n" 
   4.126 +  unfolding bin_to_bl_def by (simp add : size_bin_to_bl_aux)
   4.127 +
   4.128 +lemma bin_bl_bin' [rule_format] : 
   4.129 +  "ALL w bs. bl_to_bin (bin_to_bl_aux n w bs) = 
   4.130 +    bl_to_bin_aux (bintrunc n w) bs"
   4.131 +  by (induct n) (auto simp add : bl_to_bin_def)
   4.132 +
   4.133 +lemma bin_bl_bin: "bl_to_bin (bin_to_bl n w) = bintrunc n w"
   4.134 +  unfolding bin_to_bl_def bin_bl_bin' by auto
   4.135 +
   4.136 +lemma bl_bin_bl' [rule_format] :
   4.137 +  "ALL w n. bin_to_bl (n + length bs) (bl_to_bin_aux w bs) = 
   4.138 +    bin_to_bl_aux n w bs"
   4.139 +  apply (induct "bs")
   4.140 +   apply auto
   4.141 +    apply (simp_all only : add_Suc [symmetric])
   4.142 +    apply (auto simp add : bin_to_bl_def)
   4.143 +  done
   4.144 +
   4.145 +lemma bl_bin_bl: "bin_to_bl (length bs) (bl_to_bin bs) = bs"
   4.146 +  unfolding bl_to_bin_def
   4.147 +  apply (rule box_equals)
   4.148 +    apply (rule bl_bin_bl')
   4.149 +   prefer 2
   4.150 +   apply (rule bin_to_bl_aux.Z)
   4.151 +  apply simp
   4.152 +  done
   4.153 +  
   4.154 +declare 
   4.155 +  bin_to_bl_0 [simp] 
   4.156 +  size_bin_to_bl [simp] 
   4.157 +  bin_bl_bin [simp] 
   4.158 +  bl_bin_bl [simp]
   4.159 +
   4.160 +lemma bl_to_bin_inj:
   4.161 +  "bl_to_bin bs = bl_to_bin cs ==> length bs = length cs ==> bs = cs"
   4.162 +  apply (rule_tac box_equals)
   4.163 +    defer
   4.164 +    apply (rule bl_bin_bl)
   4.165 +   apply (rule bl_bin_bl)
   4.166 +  apply simp
   4.167 +  done
   4.168 +
   4.169 +lemma bl_to_bin_False: "bl_to_bin (False # bl) = bl_to_bin bl"
   4.170 +  unfolding bl_to_bin_def by auto
   4.171 +  
   4.172 +lemma bl_to_bin_Nil: "bl_to_bin [] = Numeral.Pls"
   4.173 +  unfolding bl_to_bin_def by auto
   4.174 +
   4.175 +lemma bin_to_bl_Pls_aux [rule_format] : 
   4.176 +  "ALL bl. bin_to_bl_aux n Numeral.Pls bl = replicate n False @ bl"
   4.177 +  by (induct n) (auto simp: replicate_app_Cons_same)
   4.178 +
   4.179 +lemma bin_to_bl_Pls: "bin_to_bl n Numeral.Pls = replicate n False"
   4.180 +  unfolding bin_to_bl_def by (simp add : bin_to_bl_Pls_aux)
   4.181 +
   4.182 +lemma bin_to_bl_Min_aux [rule_format] : 
   4.183 +  "ALL bl. bin_to_bl_aux n Numeral.Min bl = replicate n True @ bl"
   4.184 +  by (induct n) (auto simp: replicate_app_Cons_same)
   4.185 +
   4.186 +lemma bin_to_bl_Min: "bin_to_bl n Numeral.Min = replicate n True"
   4.187 +  unfolding bin_to_bl_def by (simp add : bin_to_bl_Min_aux)
   4.188 +
   4.189 +lemma bl_to_bin_rep_F: 
   4.190 +  "bl_to_bin (replicate n False @ bl) = bl_to_bin bl"
   4.191 +  apply (simp add: bin_to_bl_Pls_aux [symmetric] bin_bl_bin')
   4.192 +  apply (simp add: bl_to_bin_def)
   4.193 +  done
   4.194 +
   4.195 +lemma bin_to_bl_trunc:
   4.196 +  "n <= m ==> bin_to_bl n (bintrunc m w) = bin_to_bl n w"
   4.197 +  by (auto intro: bl_to_bin_inj)
   4.198 +
   4.199 +declare 
   4.200 +  bin_to_bl_trunc [simp] 
   4.201 +  bl_to_bin_False [simp] 
   4.202 +  bl_to_bin_Nil [simp]
   4.203 +
   4.204 +lemma bin_to_bl_aux_bintr [rule_format] :
   4.205 +  "ALL m bin bl. bin_to_bl_aux n (bintrunc m bin) bl = 
   4.206 +    replicate (n - m) False @ bin_to_bl_aux (min n m) bin bl"
   4.207 +  apply (induct_tac "n")
   4.208 +   apply clarsimp
   4.209 +  apply clarsimp
   4.210 +  apply (case_tac "m")
   4.211 +   apply (clarsimp simp: bin_to_bl_Pls_aux) 
   4.212 +   apply (erule thin_rl)
   4.213 +   apply (induct_tac n)   
   4.214 +    apply auto
   4.215 +  done
   4.216 +
   4.217 +lemmas bin_to_bl_bintr = 
   4.218 +  bin_to_bl_aux_bintr [where bl = "[]", folded bin_to_bl_def]
   4.219 +
   4.220 +lemma bl_to_bin_rep_False: "bl_to_bin (replicate n False) = Numeral.Pls"
   4.221 +  by (induct n) auto
   4.222 +
   4.223 +lemma len_bin_to_bl_aux [rule_format] : 
   4.224 +  "ALL w bs. length (bin_to_bl_aux n w bs) = n + length bs"
   4.225 +  by (induct "n") auto
   4.226 +
   4.227 +lemma len_bin_to_bl [simp]: "length (bin_to_bl n w) = n"
   4.228 +  unfolding bin_to_bl_def len_bin_to_bl_aux by auto
   4.229 +  
   4.230 +lemma sign_bl_bin' [rule_format] : 
   4.231 +  "ALL w. bin_sign (bl_to_bin_aux w bs) = bin_sign w"
   4.232 +  by (induct bs) auto
   4.233 +  
   4.234 +lemma sign_bl_bin: "bin_sign (bl_to_bin bs) = Numeral.Pls"
   4.235 +  unfolding bl_to_bin_def by (simp add : sign_bl_bin')
   4.236 +  
   4.237 +lemma bl_sbin_sign_aux [rule_format] : 
   4.238 +  "ALL w bs. hd (bin_to_bl_aux (Suc n) w bs) = 
   4.239 +    (bin_sign (sbintrunc n w) = Numeral.Min)"
   4.240 +  apply (induct "n")
   4.241 +   apply clarsimp
   4.242 +   apply (case_tac w rule: bin_exhaust)
   4.243 +   apply (simp split add : bit.split)
   4.244 +  apply clarsimp
   4.245 +  done
   4.246 +    
   4.247 +lemma bl_sbin_sign: 
   4.248 +  "hd (bin_to_bl (Suc n) w) = (bin_sign (sbintrunc n w) = Numeral.Min)"
   4.249 +  unfolding bin_to_bl_def by (rule bl_sbin_sign_aux)
   4.250 +
   4.251 +lemma bin_nth_of_bl_aux [rule_format] : 
   4.252 +  "ALL w. bin_nth (bl_to_bin_aux w bl) n = 
   4.253 +    (n < size bl & rev bl ! n | n >= length bl & bin_nth w (n - size bl))"
   4.254 +  apply (induct_tac bl)
   4.255 +   apply clarsimp
   4.256 +  apply clarsimp
   4.257 +  apply (cut_tac x=n and y="size list" in linorder_less_linear)
   4.258 +  apply (erule disjE, simp add: nth_append)+
   4.259 +  apply (simp add: nth_append)
   4.260 +  done
   4.261 +
   4.262 +lemma bin_nth_of_bl: "bin_nth (bl_to_bin bl) n = (n < length bl & rev bl ! n)";
   4.263 +  unfolding bl_to_bin_def by (simp add : bin_nth_of_bl_aux)
   4.264 +
   4.265 +lemma bin_nth_bl [rule_format] : "ALL m w. n < m --> 
   4.266 +    bin_nth w n = nth (rev (bin_to_bl m w)) n"
   4.267 +  apply (induct n)
   4.268 +   apply clarsimp
   4.269 +   apply (case_tac m, clarsimp)
   4.270 +   apply (clarsimp simp: bin_to_bl_def)
   4.271 +   apply (simp add: bin_to_bl_aux_alt)
   4.272 +  apply clarsimp
   4.273 +  apply (case_tac m, clarsimp)
   4.274 +  apply (clarsimp simp: bin_to_bl_def)
   4.275 +  apply (simp add: bin_to_bl_aux_alt)
   4.276 +  done
   4.277 +
   4.278 +lemma nth_rev [rule_format] : 
   4.279 +  "n < length xs --> rev xs ! n = xs ! (length xs - 1 - n)"
   4.280 +  apply (induct_tac "xs")
   4.281 +   apply simp
   4.282 +  apply (clarsimp simp add : nth_append nth.simps split add : nat.split)
   4.283 +  apply (rule_tac f = "%n. list ! n" in arg_cong) 
   4.284 +  apply arith
   4.285 +  done
   4.286 +
   4.287 +lemmas nth_rev_alt = nth_rev [where xs = "rev ?ys", simplified]
   4.288 +
   4.289 +lemma nth_bin_to_bl_aux [rule_format] : 
   4.290 +  "ALL w n bl. n < m + length bl --> (bin_to_bl_aux m w bl) ! n = 
   4.291 +    (if n < m then bin_nth w (m - 1 - n) else bl ! (n - m))"
   4.292 +  apply (induct_tac "m")
   4.293 +   apply clarsimp
   4.294 +  apply clarsimp
   4.295 +  apply (case_tac w rule: bin_exhaust)
   4.296 +  apply clarsimp
   4.297 +  apply (case_tac "na - n")
   4.298 +   apply arith
   4.299 +  apply simp
   4.300 +  apply (rule_tac f = "%n. bl ! n" in arg_cong) 
   4.301 +  apply arith
   4.302 +  done
   4.303 +  
   4.304 +lemma nth_bin_to_bl: "n < m ==> (bin_to_bl m w) ! n = bin_nth w (m - Suc n)"
   4.305 +  unfolding bin_to_bl_def by (simp add : nth_bin_to_bl_aux)
   4.306 +
   4.307 +lemma bl_to_bin_lt2p_aux [rule_format] : 
   4.308 +  "ALL w. bl_to_bin_aux w bs < (w + 1) * (2 ^ length bs)"
   4.309 +  apply (induct "bs")
   4.310 +   apply clarsimp
   4.311 +  apply clarsimp
   4.312 +  apply safe
   4.313 +   apply (erule allE, erule xtr8 [rotated],
   4.314 +          simp add: Bit_def ring_simps cong add : number_of_False_cong)+
   4.315 +  done
   4.316 +
   4.317 +lemma bl_to_bin_lt2p: "bl_to_bin bs < (2 ^ length bs)"
   4.318 +  apply (unfold bl_to_bin_def)
   4.319 +  apply (rule xtr1)
   4.320 +   prefer 2
   4.321 +   apply (rule bl_to_bin_lt2p_aux)
   4.322 +  apply simp
   4.323 +  done
   4.324 +
   4.325 +lemma bl_to_bin_ge2p_aux [rule_format] : 
   4.326 +  "ALL w. bl_to_bin_aux w bs >= w * (2 ^ length bs)"
   4.327 +  apply (induct bs)
   4.328 +   apply clarsimp
   4.329 +  apply clarsimp
   4.330 +  apply safe
   4.331 +   apply (erule allE, erule less_eq_less.order_trans [rotated],
   4.332 +          simp add: Bit_def ring_simps cong add : number_of_False_cong)+
   4.333 +  done
   4.334 +
   4.335 +lemma bl_to_bin_ge0: "bl_to_bin bs >= 0"
   4.336 +  apply (unfold bl_to_bin_def)
   4.337 +  apply (rule xtr4)
   4.338 +   apply (rule bl_to_bin_ge2p_aux)
   4.339 +  apply simp
   4.340 +  done
   4.341 +
   4.342 +lemma butlast_rest_bin: 
   4.343 +  "butlast (bin_to_bl n w) = bin_to_bl (n - 1) (bin_rest w)"
   4.344 +  apply (unfold bin_to_bl_def)
   4.345 +  apply (cases w rule: bin_exhaust)
   4.346 +  apply (cases n, clarsimp)
   4.347 +  apply clarsimp
   4.348 +  apply (auto simp add: bin_to_bl_aux_alt)
   4.349 +  done
   4.350 +
   4.351 +lemmas butlast_bin_rest = butlast_rest_bin
   4.352 +  [where w="bl_to_bin ?bl" and n="length ?bl", simplified]
   4.353 +
   4.354 +lemma butlast_rest_bl2bin_aux [rule_format] :
   4.355 +  "ALL w. bl ~= [] --> 
   4.356 +    bl_to_bin_aux w (butlast bl) = bin_rest (bl_to_bin_aux w bl)"
   4.357 +  by (induct bl) auto
   4.358 +  
   4.359 +lemma butlast_rest_bl2bin: 
   4.360 +  "bl_to_bin (butlast bl) = bin_rest (bl_to_bin bl)"
   4.361 +  apply (unfold bl_to_bin_def)
   4.362 +  apply (cases bl)
   4.363 +   apply (auto simp add: butlast_rest_bl2bin_aux)
   4.364 +  done
   4.365 +
   4.366 +lemma trunc_bl2bin_aux [rule_format] : 
   4.367 +  "ALL w. bintrunc m (bl_to_bin_aux w bl) = 
   4.368 +    bl_to_bin_aux (bintrunc (m - length bl) w) (drop (length bl - m) bl)"
   4.369 +  apply (induct_tac bl)
   4.370 +   apply clarsimp
   4.371 +  apply clarsimp
   4.372 +  apply safe
   4.373 +   apply (case_tac "m - size list")
   4.374 +    apply (simp add : diff_is_0_eq [THEN iffD1, THEN Suc_diff_le])
   4.375 +   apply simp
   4.376 +   apply (rule_tac f = "%nat. bl_to_bin_aux (bintrunc nat w BIT bit.B1) list" 
   4.377 +                   in arg_cong) 
   4.378 +   apply simp
   4.379 +  apply (case_tac "m - size list")
   4.380 +   apply (simp add: diff_is_0_eq [THEN iffD1, THEN Suc_diff_le])
   4.381 +  apply simp
   4.382 +  apply (rule_tac f = "%nat. bl_to_bin_aux (bintrunc nat w BIT bit.B0) list" 
   4.383 +                  in arg_cong) 
   4.384 +  apply simp
   4.385 +  done
   4.386 +
   4.387 +lemma trunc_bl2bin: 
   4.388 +  "bintrunc m (bl_to_bin bl) = bl_to_bin (drop (length bl - m) bl)"
   4.389 +  unfolding bl_to_bin_def by (simp add : trunc_bl2bin_aux)
   4.390 +  
   4.391 +lemmas trunc_bl2bin_len [simp] =
   4.392 +  trunc_bl2bin [of "length bl" bl, simplified, standard]  
   4.393 +
   4.394 +lemma bl2bin_drop: 
   4.395 +  "bl_to_bin (drop k bl) = bintrunc (length bl - k) (bl_to_bin bl)"
   4.396 +  apply (rule trans)
   4.397 +   prefer 2
   4.398 +   apply (rule trunc_bl2bin [symmetric])
   4.399 +  apply (cases "k <= length bl")
   4.400 +   apply auto
   4.401 +  done
   4.402 +
   4.403 +lemma nth_rest_power_bin [rule_format] :
   4.404 +  "ALL n. bin_nth ((bin_rest ^ k) w) n = bin_nth w (n + k)"
   4.405 +  apply (induct k, clarsimp)
   4.406 +  apply clarsimp
   4.407 +  apply (simp only: bin_nth.Suc [symmetric] add_Suc)
   4.408 +  done
   4.409 +
   4.410 +lemma take_rest_power_bin:
   4.411 +  "m <= n ==> take m (bin_to_bl n w) = bin_to_bl m ((bin_rest ^ (n - m)) w)" 
   4.412 +  apply (rule nth_equalityI)
   4.413 +   apply simp
   4.414 +  apply (clarsimp simp add: nth_bin_to_bl nth_rest_power_bin)
   4.415 +  done
   4.416 +
   4.417 +lemma hd_butlast: "size xs > 1 ==> hd (butlast xs) = hd xs"
   4.418 +  by (cases xs) auto
   4.419 +
   4.420 +lemma last_bin_last' [rule_format] : 
   4.421 +  "ALL w. size xs > 0 --> last xs = (bin_last (bl_to_bin_aux w xs) = bit.B1)" 
   4.422 +  by (induct xs) auto
   4.423 +
   4.424 +lemma last_bin_last: 
   4.425 +  "size xs > 0 ==> last xs = (bin_last (bl_to_bin xs) = bit.B1)" 
   4.426 +  unfolding bl_to_bin_def by (erule last_bin_last')
   4.427 +  
   4.428 +lemma bin_last_last: 
   4.429 +  "bin_last w = (if last (bin_to_bl (Suc n) w) then bit.B1 else bit.B0)" 
   4.430 +  apply (unfold bin_to_bl_def)
   4.431 +  apply simp
   4.432 +  apply (auto simp add: bin_to_bl_aux_alt)
   4.433 +  done
   4.434 +
   4.435 +(** links between bit-wise operations and operations on bool lists **)
   4.436 +    
   4.437 +lemma app2_Nil [simp]: "app2 f [] ys = []"
   4.438 +  unfolding app2_def by auto
   4.439 +
   4.440 +lemma app2_Cons [simp]:
   4.441 +  "app2 f (x # xs) (y # ys) = f x y # app2 f xs ys"
   4.442 +  unfolding app2_def by auto
   4.443 +
   4.444 +lemma bl_xor_aux_bin [rule_format] : "ALL v w bs cs. 
   4.445 +    app2 (%x y. x ~= y) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = 
   4.446 +    bin_to_bl_aux n (int_xor v w) (app2 (%x y. x ~= y) bs cs)"
   4.447 +  apply (induct_tac n)
   4.448 +   apply safe
   4.449 +   apply simp
   4.450 +  apply (case_tac v rule: bin_exhaust)
   4.451 +  apply (case_tac w rule: bin_exhaust)
   4.452 +  apply clarsimp
   4.453 +  apply (case_tac b)
   4.454 +  apply (case_tac ba, safe, simp_all)+
   4.455 +  done
   4.456 +    
   4.457 +lemma bl_or_aux_bin [rule_format] : "ALL v w bs cs. 
   4.458 +    app2 (op | ) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = 
   4.459 +    bin_to_bl_aux n (int_or v w) (app2 (op | ) bs cs)" 
   4.460 +  apply (induct_tac n)
   4.461 +   apply safe
   4.462 +   apply simp
   4.463 +  apply (case_tac v rule: bin_exhaust)
   4.464 +  apply (case_tac w rule: bin_exhaust)
   4.465 +  apply clarsimp
   4.466 +  apply (case_tac b)
   4.467 +  apply (case_tac ba, safe, simp_all)+
   4.468 +  done
   4.469 +    
   4.470 +lemma bl_and_aux_bin [rule_format] : "ALL v w bs cs. 
   4.471 +    app2 (op & ) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = 
   4.472 +    bin_to_bl_aux n (int_and v w) (app2 (op & ) bs cs)" 
   4.473 +  apply (induct_tac n)
   4.474 +   apply safe
   4.475 +   apply simp
   4.476 +  apply (case_tac v rule: bin_exhaust)
   4.477 +  apply (case_tac w rule: bin_exhaust)
   4.478 +  apply clarsimp
   4.479 +  apply (case_tac b)
   4.480 +  apply (case_tac ba, safe, simp_all)+
   4.481 +  done
   4.482 +    
   4.483 +lemma bl_not_aux_bin [rule_format] : 
   4.484 +  "ALL w cs. map Not (bin_to_bl_aux n w cs) = 
   4.485 +    bin_to_bl_aux n (int_not w) (map Not cs)"
   4.486 +  apply (induct n)
   4.487 +   apply clarsimp
   4.488 +  apply clarsimp
   4.489 +  apply (case_tac w rule: bin_exhaust)
   4.490 +  apply (case_tac b)
   4.491 +   apply auto
   4.492 +  done
   4.493 +
   4.494 +lemmas bl_not_bin = bl_not_aux_bin
   4.495 +  [where cs = "[]", unfolded bin_to_bl_def [symmetric] map.simps]
   4.496 +
   4.497 +lemmas bl_and_bin = bl_and_aux_bin [where bs="[]" and cs="[]", 
   4.498 +                                    unfolded app2_Nil, folded bin_to_bl_def]
   4.499 +
   4.500 +lemmas bl_or_bin = bl_or_aux_bin [where bs="[]" and cs="[]", 
   4.501 +                                  unfolded app2_Nil, folded bin_to_bl_def]
   4.502 +
   4.503 +lemmas bl_xor_bin = bl_xor_aux_bin [where bs="[]" and cs="[]", 
   4.504 +                                    unfolded app2_Nil, folded bin_to_bl_def]
   4.505 +
   4.506 +lemma drop_bin2bl_aux [rule_format] : 
   4.507 +  "ALL m bin bs. drop m (bin_to_bl_aux n bin bs) = 
   4.508 +    bin_to_bl_aux (n - m) bin (drop (m - n) bs)"
   4.509 +  apply (induct n, clarsimp)
   4.510 +  apply clarsimp
   4.511 +  apply (case_tac bin rule: bin_exhaust)
   4.512 +  apply (case_tac "m <= n", simp)
   4.513 +  apply (case_tac "m - n", simp)
   4.514 +  apply simp
   4.515 +  apply (rule_tac f = "%nat. drop nat bs" in arg_cong) 
   4.516 +  apply simp
   4.517 +  done
   4.518 +
   4.519 +lemma drop_bin2bl: "drop m (bin_to_bl n bin) = bin_to_bl (n - m) bin"
   4.520 +  unfolding bin_to_bl_def by (simp add : drop_bin2bl_aux)
   4.521 +
   4.522 +lemma take_bin2bl_lem1 [rule_format] : 
   4.523 +  "ALL w bs. take m (bin_to_bl_aux m w bs) = bin_to_bl m w"
   4.524 +  apply (induct m, clarsimp)
   4.525 +  apply clarsimp
   4.526 +  apply (simp add: bin_to_bl_aux_alt)
   4.527 +  apply (simp add: bin_to_bl_def)
   4.528 +  apply (simp add: bin_to_bl_aux_alt)
   4.529 +  done
   4.530 +
   4.531 +lemma take_bin2bl_lem [rule_format] : 
   4.532 +  "ALL w bs. take m (bin_to_bl_aux (m + n) w bs) = 
   4.533 +    take m (bin_to_bl (m + n) w)"
   4.534 +  apply (induct n)
   4.535 +   apply clarify
   4.536 +   apply (simp_all (no_asm) add: bin_to_bl_def take_bin2bl_lem1)
   4.537 +  apply simp
   4.538 +  done
   4.539 +
   4.540 +lemma bin_split_take [rule_format] : 
   4.541 +  "ALL b c. bin_split n c = (a, b) --> 
   4.542 +    bin_to_bl m a = take m (bin_to_bl (m + n) c)"
   4.543 +  apply (induct n)
   4.544 +   apply clarsimp
   4.545 +  apply (clarsimp simp: Let_def split: ls_splits)
   4.546 +  apply (simp add: bin_to_bl_def)
   4.547 +  apply (simp add: take_bin2bl_lem)
   4.548 +  done
   4.549 +
   4.550 +lemma bin_split_take1: 
   4.551 +  "k = m + n ==> bin_split n c = (a, b) ==> 
   4.552 +    bin_to_bl m a = take m (bin_to_bl k c)"
   4.553 +  by (auto elim: bin_split_take)
   4.554 +  
   4.555 +lemma nth_takefill [rule_format] : "ALL m l. m < n --> 
   4.556 +    takefill fill n l ! m = (if m < length l then l ! m else fill)"
   4.557 +  apply (induct n, clarsimp)
   4.558 +  apply clarsimp
   4.559 +  apply (case_tac m)
   4.560 +   apply (simp split: list.split)
   4.561 +  apply clarsimp
   4.562 +  apply (erule allE)+
   4.563 +  apply (erule (1) impE)
   4.564 +  apply (simp split: list.split)
   4.565 +  done
   4.566 +
   4.567 +lemma takefill_alt [rule_format] :
   4.568 +  "ALL l. takefill fill n l = take n l @ replicate (n - length l) fill"
   4.569 +  by (induct n) (auto split: list.split)
   4.570 +
   4.571 +lemma takefill_replicate [simp]:
   4.572 +  "takefill fill n (replicate m fill) = replicate n fill"
   4.573 +  by (simp add : takefill_alt replicate_add [symmetric])
   4.574 +
   4.575 +lemma takefill_le' [rule_format] :
   4.576 +  "ALL l n. n = m + k --> takefill x m (takefill x n l) = takefill x m l"
   4.577 +  by (induct m) (auto split: list.split)
   4.578 +
   4.579 +lemma length_takefill [simp]: "length (takefill fill n l) = n"
   4.580 +  by (simp add : takefill_alt)
   4.581 +
   4.582 +lemma take_takefill':
   4.583 +  "!!w n.  n = k + m ==> take k (takefill fill n w) = takefill fill k w"
   4.584 +  by (induct k) (auto split add : list.split) 
   4.585 +
   4.586 +lemma drop_takefill:
   4.587 +  "!!w. drop k (takefill fill (m + k) w) = takefill fill m (drop k w)"
   4.588 +  by (induct k) (auto split add : list.split) 
   4.589 +
   4.590 +lemma takefill_le [simp]:
   4.591 +  "m \<le> n \<Longrightarrow> takefill x m (takefill x n l) = takefill x m l"
   4.592 +  by (auto simp: le_iff_add takefill_le')
   4.593 +
   4.594 +lemma take_takefill [simp]:
   4.595 +  "m \<le> n \<Longrightarrow> take m (takefill fill n w) = takefill fill m w"
   4.596 +  by (auto simp: le_iff_add take_takefill')
   4.597 + 
   4.598 +lemma takefill_append:
   4.599 +  "takefill fill (m + length xs) (xs @ w) = xs @ (takefill fill m w)"
   4.600 +  by (induct xs) auto
   4.601 +
   4.602 +lemma takefill_same': 
   4.603 +  "l = length xs ==> takefill fill l xs = xs"
   4.604 +  by clarify (induct xs, auto)
   4.605 + 
   4.606 +lemmas takefill_same [simp] = takefill_same' [OF refl]
   4.607 +
   4.608 +lemma takefill_bintrunc:
   4.609 +  "takefill False n bl = rev (bin_to_bl n (bl_to_bin (rev bl)))"
   4.610 +  apply (rule nth_equalityI)
   4.611 +   apply simp
   4.612 +  apply (clarsimp simp: nth_takefill nth_rev nth_bin_to_bl bin_nth_of_bl)
   4.613 +  done
   4.614 +
   4.615 +lemma bl_bin_bl_rtf:
   4.616 +  "bin_to_bl n (bl_to_bin bl) = rev (takefill False n (rev bl))"
   4.617 +  by (simp add : takefill_bintrunc)
   4.618 +  
   4.619 +lemmas bl_bin_bl_rep_drop = 
   4.620 +  bl_bin_bl_rtf [simplified takefill_alt,
   4.621 +                 simplified, simplified rev_take, simplified]
   4.622 +
   4.623 +lemma tf_rev:
   4.624 +  "n + k = m + length bl ==> takefill x m (rev (takefill y n bl)) = 
   4.625 +    rev (takefill y m (rev (takefill x k (rev bl))))"
   4.626 +  apply (rule nth_equalityI)
   4.627 +   apply (auto simp add: nth_takefill nth_rev)
   4.628 +  apply (rule_tac f = "%n. bl ! n" in arg_cong) 
   4.629 +  apply arith 
   4.630 +  done
   4.631 +
   4.632 +lemma takefill_minus:
   4.633 +  "0 < n ==> takefill fill (Suc (n - 1)) w = takefill fill n w"
   4.634 +  by auto
   4.635 +
   4.636 +lemmas takefill_Suc_cases = 
   4.637 +  list.cases [THEN takefill.Suc [THEN trans], standard]
   4.638 +
   4.639 +lemmas takefill_Suc_Nil = takefill_Suc_cases (1)
   4.640 +lemmas takefill_Suc_Cons = takefill_Suc_cases (2)
   4.641 +
   4.642 +lemmas takefill_minus_simps = takefill_Suc_cases [THEN [2] 
   4.643 +  takefill_minus [symmetric, THEN trans], standard]
   4.644 +
   4.645 +lemmas takefill_pred_simps [simp] =
   4.646 +  takefill_minus_simps [where n="number_of bin", simplified nobm1, standard]
   4.647 +
   4.648 +(* links with function bl_to_bin *)
   4.649 +
   4.650 +lemma bl_to_bin_aux_cat: 
   4.651 +  "!!nv v. bl_to_bin_aux (bin_cat w nv v) bs = 
   4.652 +    bin_cat w (nv + length bs) (bl_to_bin_aux v bs)"
   4.653 +  apply (induct bs)
   4.654 +   apply simp
   4.655 +  apply (simp add: bin_cat_Suc_Bit [symmetric] del: bin_cat.simps)
   4.656 +  done
   4.657 +
   4.658 +lemma bin_to_bl_aux_cat: 
   4.659 +  "!!w bs. bin_to_bl_aux (nv + nw) (bin_cat v nw w) bs = 
   4.660 +    bin_to_bl_aux nv v (bin_to_bl_aux nw w bs)"
   4.661 +  by (induct nw) auto 
   4.662 +
   4.663 +lemmas bl_to_bin_aux_alt = 
   4.664 +  bl_to_bin_aux_cat [where nv = "0" and v = "Numeral.Pls", 
   4.665 +    simplified bl_to_bin_def [symmetric], simplified]
   4.666 +
   4.667 +lemmas bin_to_bl_cat =
   4.668 +  bin_to_bl_aux_cat [where bs = "[]", folded bin_to_bl_def]
   4.669 +
   4.670 +lemmas bl_to_bin_aux_app_cat = 
   4.671 +  trans [OF bl_to_bin_aux_append bl_to_bin_aux_alt]
   4.672 +
   4.673 +lemmas bin_to_bl_aux_cat_app =
   4.674 +  trans [OF bin_to_bl_aux_cat bin_to_bl_aux_alt]
   4.675 +
   4.676 +lemmas bl_to_bin_app_cat = bl_to_bin_aux_app_cat
   4.677 +  [where w = "Numeral.Pls", folded bl_to_bin_def]
   4.678 +
   4.679 +lemmas bin_to_bl_cat_app = bin_to_bl_aux_cat_app
   4.680 +  [where bs = "[]", folded bin_to_bl_def]
   4.681 +
   4.682 +(* bl_to_bin_app_cat_alt and bl_to_bin_app_cat are easily interderivable *)
   4.683 +lemma bl_to_bin_app_cat_alt: 
   4.684 +  "bin_cat (bl_to_bin cs) n w = bl_to_bin (cs @ bin_to_bl n w)"
   4.685 +  by (simp add : bl_to_bin_app_cat)
   4.686 +
   4.687 +lemma mask_lem: "(bl_to_bin (True # replicate n False)) = 
   4.688 +    Numeral.succ (bl_to_bin (replicate n True))"
   4.689 +  apply (unfold bl_to_bin_def)
   4.690 +  apply (induct n)
   4.691 +   apply simp
   4.692 +  apply (simp only: Suc_eq_add_numeral_1 replicate_add
   4.693 +                    append_Cons [symmetric] bl_to_bin_aux_append)
   4.694 +  apply simp
   4.695 +  done
   4.696 +
   4.697 +(* function bl_of_nth *)
   4.698 +lemma length_bl_of_nth [simp]: "length (bl_of_nth n f) = n"
   4.699 +  by (induct n)  auto
   4.700 +
   4.701 +lemma nth_bl_of_nth [simp]:
   4.702 +  "m < n \<Longrightarrow> rev (bl_of_nth n f) ! m = f m"
   4.703 +  apply (induct n)
   4.704 +   apply simp
   4.705 +  apply (clarsimp simp add : nth_append)
   4.706 +  apply (rule_tac f = "f" in arg_cong) 
   4.707 +  apply simp
   4.708 +  done
   4.709 +
   4.710 +lemma bl_of_nth_inj: 
   4.711 +  "(!!k. k < n ==> f k = g k) ==> bl_of_nth n f = bl_of_nth n g"
   4.712 +  by (induct n)  auto
   4.713 +
   4.714 +lemma bl_of_nth_nth_le [rule_format] : "ALL xs. 
   4.715 +    length xs >= n --> bl_of_nth n (nth (rev xs)) = drop (length xs - n) xs";
   4.716 +  apply (induct n, clarsimp)
   4.717 +  apply clarsimp
   4.718 +  apply (rule trans [OF _ hd_Cons_tl])
   4.719 +   apply (frule Suc_le_lessD)
   4.720 +   apply (simp add: nth_rev trans [OF drop_Suc drop_tl, symmetric])
   4.721 +   apply (subst hd_drop_conv_nth)
   4.722 +     apply force
   4.723 +    apply simp_all
   4.724 +  apply (rule_tac f = "%n. drop n xs" in arg_cong) 
   4.725 +  apply simp
   4.726 +  done
   4.727 +
   4.728 +lemmas bl_of_nth_nth [simp] = order_refl [THEN bl_of_nth_nth_le, simplified]
   4.729 +
   4.730 +lemma size_rbl_pred: "length (rbl_pred bl) = length bl"
   4.731 +  by (induct bl) auto
   4.732 +
   4.733 +lemma size_rbl_succ: "length (rbl_succ bl) = length bl"
   4.734 +  by (induct bl) auto
   4.735 +
   4.736 +lemma size_rbl_add:
   4.737 +  "!!cl. length (rbl_add bl cl) = length bl"
   4.738 +  by (induct bl) (auto simp: Let_def size_rbl_succ)
   4.739 +
   4.740 +lemma size_rbl_mult: 
   4.741 +  "!!cl. length (rbl_mult bl cl) = length bl"
   4.742 +  by (induct bl) (auto simp add : Let_def size_rbl_add)
   4.743 +
   4.744 +lemmas rbl_sizes [simp] = 
   4.745 +  size_rbl_pred size_rbl_succ size_rbl_add size_rbl_mult
   4.746 +
   4.747 +lemmas rbl_Nils =
   4.748 +  rbl_pred.Nil rbl_succ.Nil rbl_add.Nil rbl_mult.Nil
   4.749 +
   4.750 +lemma rbl_pred: 
   4.751 +  "!!bin. rbl_pred (rev (bin_to_bl n bin)) = rev (bin_to_bl n (Numeral.pred bin))"
   4.752 +  apply (induct n, simp)
   4.753 +  apply (unfold bin_to_bl_def)
   4.754 +  apply clarsimp
   4.755 +  apply (case_tac bin rule: bin_exhaust)
   4.756 +  apply (case_tac b)
   4.757 +   apply (clarsimp simp: bin_to_bl_aux_alt)+
   4.758 +  done
   4.759 +
   4.760 +lemma rbl_succ: 
   4.761 +  "!!bin. rbl_succ (rev (bin_to_bl n bin)) = rev (bin_to_bl n (Numeral.succ bin))"
   4.762 +  apply (induct n, simp)
   4.763 +  apply (unfold bin_to_bl_def)
   4.764 +  apply clarsimp
   4.765 +  apply (case_tac bin rule: bin_exhaust)
   4.766 +  apply (case_tac b)
   4.767 +   apply (clarsimp simp: bin_to_bl_aux_alt)+
   4.768 +  done
   4.769 +
   4.770 +lemma rbl_add: 
   4.771 +  "!!bina binb. rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) = 
   4.772 +    rev (bin_to_bl n (bina + binb))"
   4.773 +  apply (induct n, simp)
   4.774 +  apply (unfold bin_to_bl_def)
   4.775 +  apply clarsimp
   4.776 +  apply (case_tac bina rule: bin_exhaust)
   4.777 +  apply (case_tac binb rule: bin_exhaust)
   4.778 +  apply (case_tac b)
   4.779 +   apply (case_tac [!] "ba")
   4.780 +     apply (auto simp: rbl_succ succ_def bin_to_bl_aux_alt Let_def add_ac)
   4.781 +  done
   4.782 +
   4.783 +lemma rbl_add_app2: 
   4.784 +  "!!blb. length blb >= length bla ==> 
   4.785 +    rbl_add bla (blb @ blc) = rbl_add bla blb"
   4.786 +  apply (induct bla, simp)
   4.787 +  apply clarsimp
   4.788 +  apply (case_tac blb, clarsimp)
   4.789 +  apply (clarsimp simp: Let_def)
   4.790 +  done
   4.791 +
   4.792 +lemma rbl_add_take2: 
   4.793 +  "!!blb. length blb >= length bla ==> 
   4.794 +    rbl_add bla (take (length bla) blb) = rbl_add bla blb"
   4.795 +  apply (induct bla, simp)
   4.796 +  apply clarsimp
   4.797 +  apply (case_tac blb, clarsimp)
   4.798 +  apply (clarsimp simp: Let_def)
   4.799 +  done
   4.800 +
   4.801 +lemma rbl_add_long: 
   4.802 +  "m >= n ==> rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) = 
   4.803 +    rev (bin_to_bl n (bina + binb))"
   4.804 +  apply (rule box_equals [OF _ rbl_add_take2 rbl_add])
   4.805 +   apply (rule_tac f = "rbl_add (rev (bin_to_bl n bina))" in arg_cong) 
   4.806 +   apply (rule rev_swap [THEN iffD1])
   4.807 +   apply (simp add: rev_take drop_bin2bl)
   4.808 +  apply simp
   4.809 +  done
   4.810 +
   4.811 +lemma rbl_mult_app2:
   4.812 +  "!!blb. length blb >= length bla ==> 
   4.813 +    rbl_mult bla (blb @ blc) = rbl_mult bla blb"
   4.814 +  apply (induct bla, simp)
   4.815 +  apply clarsimp
   4.816 +  apply (case_tac blb, clarsimp)
   4.817 +  apply (clarsimp simp: Let_def rbl_add_app2)
   4.818 +  done
   4.819 +
   4.820 +lemma rbl_mult_take2: 
   4.821 +  "length blb >= length bla ==> 
   4.822 +    rbl_mult bla (take (length bla) blb) = rbl_mult bla blb"
   4.823 +  apply (rule trans)
   4.824 +   apply (rule rbl_mult_app2 [symmetric])
   4.825 +   apply simp
   4.826 +  apply (rule_tac f = "rbl_mult bla" in arg_cong) 
   4.827 +  apply (rule append_take_drop_id)
   4.828 +  done
   4.829 +    
   4.830 +lemma rbl_mult_gt1: 
   4.831 +  "m >= length bl ==> rbl_mult bl (rev (bin_to_bl m binb)) = 
   4.832 +    rbl_mult bl (rev (bin_to_bl (length bl) binb))"
   4.833 +  apply (rule trans)
   4.834 +   apply (rule rbl_mult_take2 [symmetric])
   4.835 +   apply simp_all
   4.836 +  apply (rule_tac f = "rbl_mult bl" in arg_cong) 
   4.837 +  apply (rule rev_swap [THEN iffD1])
   4.838 +  apply (simp add: rev_take drop_bin2bl)
   4.839 +  done
   4.840 +    
   4.841 +lemma rbl_mult_gt: 
   4.842 +  "m > n ==> rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) = 
   4.843 +    rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb))"
   4.844 +  by (auto intro: trans [OF rbl_mult_gt1])
   4.845 +  
   4.846 +lemmas rbl_mult_Suc = lessI [THEN rbl_mult_gt]
   4.847 +
   4.848 +lemma rbbl_Cons: 
   4.849 +  "b # rev (bin_to_bl n x) = rev (bin_to_bl (Suc n) (x BIT If b bit.B1 bit.B0))"
   4.850 +  apply (unfold bin_to_bl_def)
   4.851 +  apply simp
   4.852 +  apply (simp add: bin_to_bl_aux_alt)
   4.853 +  done
   4.854 +  
   4.855 +lemma rbl_mult: "!!bina binb. 
   4.856 +    rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) = 
   4.857 +    rev (bin_to_bl n (bina * binb))"
   4.858 +  apply (induct n)
   4.859 +   apply simp
   4.860 +  apply (unfold bin_to_bl_def)
   4.861 +  apply clarsimp
   4.862 +  apply (case_tac bina rule: bin_exhaust)
   4.863 +  apply (case_tac binb rule: bin_exhaust)
   4.864 +  apply (case_tac b)
   4.865 +   apply (case_tac [!] "ba")
   4.866 +     apply (auto simp: bin_to_bl_aux_alt Let_def)
   4.867 +     apply (auto simp: rbbl_Cons rbl_mult_Suc rbl_add)
   4.868 +  done
   4.869 +
   4.870 +lemma rbl_add_split: 
   4.871 +  "P (rbl_add (y # ys) (x # xs)) = 
   4.872 +    (ALL ws. length ws = length ys --> ws = rbl_add ys xs --> 
   4.873 +    (y --> ((x --> P (False # rbl_succ ws)) & (~ x -->  P (True # ws)))) & \ 
   4.874 +    (~ y --> P (x # ws)))"
   4.875 +  apply (auto simp add: Let_def)
   4.876 +   apply (case_tac [!] "y")
   4.877 +     apply auto
   4.878 +  done
   4.879 +
   4.880 +lemma rbl_mult_split: 
   4.881 +  "P (rbl_mult (y # ys) xs) = 
   4.882 +    (ALL ws. length ws = Suc (length ys) --> ws = False # rbl_mult ys xs --> 
   4.883 +    (y --> P (rbl_add ws xs)) & (~ y -->  P ws))"
   4.884 +  by (clarsimp simp add : Let_def)
   4.885 +  
   4.886 +lemma and_len: "xs = ys ==> xs = ys & length xs = length ys"
   4.887 +  by auto
   4.888 +
   4.889 +lemma size_if: "size (if p then xs else ys) = (if p then size xs else size ys)"
   4.890 +  by auto
   4.891 +
   4.892 +lemma tl_if: "tl (if p then xs else ys) = (if p then tl xs else tl ys)"
   4.893 +  by auto
   4.894 +
   4.895 +lemma hd_if: "hd (if p then xs else ys) = (if p then hd xs else hd ys)"
   4.896 +  by auto
   4.897 +
   4.898 +lemma if_Not_x: "(if p then ~ x else x) = (p = (~ x))"
   4.899 +  by auto
   4.900 +
   4.901 +lemma if_x_Not: "(if p then x else ~ x) = (p = x)"
   4.902 +  by auto
   4.903 +
   4.904 +lemma if_same_and: "(If p x y & If p u v) = (if p then x & u else y & v)"
   4.905 +  by auto
   4.906 +
   4.907 +lemma if_same_eq: "(If p x y  = (If p u v)) = (if p then x = (u) else y = (v))"
   4.908 +  by auto
   4.909 +
   4.910 +lemma if_same_eq_not:
   4.911 +  "(If p x y  = (~ If p u v)) = (if p then x = (~u) else y = (~v))"
   4.912 +  by auto
   4.913 +
   4.914 +(* note - if_Cons can cause blowup in the size, if p is complex,
   4.915 +  so make a simproc *)
   4.916 +lemma if_Cons: "(if p then x # xs else y # ys) = If p x y # If p xs ys"
   4.917 +  by auto
   4.918 +
   4.919 +lemma if_single:
   4.920 +  "(if xc then [xab] else [an]) = [if xc then xab else an]"
   4.921 +  by auto
   4.922 +
   4.923 +lemma if_bool_simps:
   4.924 +  "If p True y = (p | y) & If p False y = (~p & y) & 
   4.925 +    If p y True = (p --> y) & If p y False = (p & y)"
   4.926 +  by auto
   4.927 +
   4.928 +lemmas if_simps = if_x_Not if_Not_x if_cancel if_True if_False if_bool_simps
   4.929 +
   4.930 +lemmas seqr = eq_reflection [where x = "size ?w"]
   4.931 +
   4.932 +lemmas tl_Nil = tl.simps (1)
   4.933 +lemmas tl_Cons = tl.simps (2)
   4.934 +
   4.935 +
   4.936 +section "Repeated splitting or concatenation"
   4.937 +
   4.938 +lemma sclem:
   4.939 +  "size (concat (map (bin_to_bl n) xs)) = length xs * n"
   4.940 +  by (induct xs) auto
   4.941 +
   4.942 +lemma bin_cat_foldl_lem [rule_format] :
   4.943 +  "ALL x. foldl (%u. bin_cat u n) x xs = 
   4.944 +    bin_cat x (size xs * n) (foldl (%u. bin_cat u n) y xs)"
   4.945 +  apply (induct xs)
   4.946 +   apply simp
   4.947 +  apply clarify
   4.948 +  apply (simp (no_asm))
   4.949 +  apply (frule asm_rl)
   4.950 +  apply (drule spec)
   4.951 +  apply (erule trans)
   4.952 +  apply (drule_tac x = "bin_cat y n a" in spec) 
   4.953 +  apply (simp add : bin_cat_assoc_sym min_def)
   4.954 +  done
   4.955 +
   4.956 +lemma bin_rcat_bl:
   4.957 +  "(bin_rcat n wl) = bl_to_bin (concat (map (bin_to_bl n) wl))"
   4.958 +  apply (unfold bin_rcat_def)
   4.959 +  apply (rule sym)
   4.960 +  apply (induct wl)
   4.961 +   apply (auto simp add : bl_to_bin_append)
   4.962 +  apply (simp add : bl_to_bin_aux_alt sclem)
   4.963 +  apply (simp add : bin_cat_foldl_lem [symmetric])
   4.964 +  done
   4.965 +
   4.966 +lemmas bin_rsplit_aux_simps = bin_rsplit_aux.simps bin_rsplitl_aux.simps
   4.967 +lemmas rsplit_aux_simps = bin_rsplit_aux_simps
   4.968 +
   4.969 +lemmas th_if_simp1 = split_if [where P = "op = ?l",
   4.970 +  THEN iffD1, THEN conjunct1, THEN mp, standard]
   4.971 +lemmas th_if_simp2 = split_if [where P = "op = ?l",
   4.972 +  THEN iffD1, THEN conjunct2, THEN mp, standard]
   4.973 +
   4.974 +lemmas rsplit_aux_simp1s = rsplit_aux_simps [THEN th_if_simp1]
   4.975 +
   4.976 +lemmas rsplit_aux_simp2ls = rsplit_aux_simps [THEN th_if_simp2]
   4.977 +(* these safe to [simp add] as require calculating m - n *)
   4.978 +lemmas bin_rsplit_aux_simp2s [simp] = rsplit_aux_simp2ls [unfolded Let_def]
   4.979 +lemmas rbscl = bin_rsplit_aux_simp2s (2)
   4.980 +
   4.981 +lemmas rsplit_aux_0_simps [simp] = 
   4.982 +  rsplit_aux_simp1s [OF disjI1] rsplit_aux_simp1s [OF disjI2]
   4.983 +
   4.984 +lemma bin_rsplit_aux_append:
   4.985 +  "bin_rsplit_aux (n, bs @ cs, m, c) = bin_rsplit_aux (n, bs, m, c) @ cs"
   4.986 +  apply (rule_tac u=n and v=bs and w=m and x=c in bin_rsplit_aux.induct)
   4.987 +  apply (subst bin_rsplit_aux.simps)
   4.988 +  apply (subst bin_rsplit_aux.simps)
   4.989 +  apply (clarsimp split: ls_splits)
   4.990 +  done
   4.991 +
   4.992 +lemma bin_rsplitl_aux_append:
   4.993 +  "bin_rsplitl_aux (n, bs @ cs, m, c) = bin_rsplitl_aux (n, bs, m, c) @ cs"
   4.994 +  apply (rule_tac u=n and v=bs and w=m and x=c in bin_rsplitl_aux.induct)
   4.995 +  apply (subst bin_rsplitl_aux.simps)
   4.996 +  apply (subst bin_rsplitl_aux.simps)
   4.997 +  apply (clarsimp split: ls_splits)
   4.998 +  done
   4.999 +
  4.1000 +lemmas rsplit_aux_apps [where bs = "[]"] =
  4.1001 +  bin_rsplit_aux_append bin_rsplitl_aux_append
  4.1002 +
  4.1003 +lemmas rsplit_def_auxs = bin_rsplit_def bin_rsplitl_def
  4.1004 +
  4.1005 +lemmas rsplit_aux_alts = rsplit_aux_apps 
  4.1006 +  [unfolded append_Nil rsplit_def_auxs [symmetric]]
  4.1007 +
  4.1008 +lemma bin_split_minus: "0 < n ==> bin_split (Suc (n - 1)) w = bin_split n w"
  4.1009 +  by auto
  4.1010 +
  4.1011 +lemmas bin_split_minus_simp =
  4.1012 +  bin_split.Suc [THEN [2] bin_split_minus [symmetric, THEN trans], standard]
  4.1013 +
  4.1014 +lemma bin_split_pred_simp [simp]: 
  4.1015 +  "(0::nat) < number_of bin \<Longrightarrow>
  4.1016 +  bin_split (number_of bin) w =
  4.1017 +  (let (w1, w2) = bin_split (number_of (Numeral.pred bin)) (bin_rest w)
  4.1018 +   in (w1, w2 BIT bin_last w))" 
  4.1019 +  by (simp only: nobm1 bin_split_minus_simp)
  4.1020 +
  4.1021 +declare bin_split_pred_simp [simp]
  4.1022 +
  4.1023 +lemma bin_rsplit_aux_simp_alt:
  4.1024 +  "bin_rsplit_aux (n, bs, m, c) =
  4.1025 +   (if m = 0 \<or> n = 0 
  4.1026 +   then bs
  4.1027 +   else let (a, b) = bin_split n c in bin_rsplit n (m - n, a) @ b # bs)"
  4.1028 +  apply (rule trans)
  4.1029 +   apply (subst bin_rsplit_aux.simps, rule refl)
  4.1030 +  apply (simp add: rsplit_aux_alts)
  4.1031 +  done
  4.1032 +
  4.1033 +lemmas bin_rsplit_simp_alt = 
  4.1034 +  trans [OF bin_rsplit_def [THEN meta_eq_to_obj_eq]
  4.1035 +            bin_rsplit_aux_simp_alt, standard]
  4.1036 +
  4.1037 +lemmas bthrs = bin_rsplit_simp_alt [THEN [2] trans]
  4.1038 +
  4.1039 +lemma bin_rsplit_size_sign' [rule_format] : 
  4.1040 +  "n > 0 ==> (ALL nw w. rev sw = bin_rsplit n (nw, w) --> 
  4.1041 +    (ALL v: set sw. bintrunc n v = v))"
  4.1042 +  apply (induct sw)
  4.1043 +   apply clarsimp
  4.1044 +  apply clarsimp
  4.1045 +  apply (drule bthrs)
  4.1046 +  apply (simp (no_asm_use) add: Let_def split: ls_splits)
  4.1047 +  apply clarify
  4.1048 +  apply (erule impE, rule exI, erule exI)
  4.1049 +  apply (drule split_bintrunc)
  4.1050 +  apply simp
  4.1051 +  done
  4.1052 +
  4.1053 +lemmas bin_rsplit_size_sign = bin_rsplit_size_sign' [OF asm_rl 
  4.1054 +  rev_rev_ident [THEN trans] set_rev [THEN equalityD2 [THEN subsetD]],
  4.1055 +  standard]
  4.1056 +
  4.1057 +lemma bin_nth_rsplit [rule_format] :
  4.1058 +  "n > 0 ==> m < n ==> (ALL w k nw. rev sw = bin_rsplit n (nw, w) --> 
  4.1059 +       k < size sw --> bin_nth (sw ! k) m = bin_nth w (k * n + m))"
  4.1060 +  apply (induct sw)
  4.1061 +   apply clarsimp
  4.1062 +  apply clarsimp
  4.1063 +  apply (drule bthrs)
  4.1064 +  apply (simp (no_asm_use) add: Let_def split: ls_splits)
  4.1065 +  apply clarify
  4.1066 +  apply (erule allE, erule impE, erule exI)
  4.1067 +  apply (case_tac k)
  4.1068 +   apply clarsimp   
  4.1069 +   prefer 2
  4.1070 +   apply clarsimp
  4.1071 +   apply (erule allE)
  4.1072 +   apply (erule (1) impE)
  4.1073 +   apply (drule bin_nth_split, erule conjE, erule allE,
  4.1074 +          erule trans, simp add : add_ac)+
  4.1075 +  done
  4.1076 +
  4.1077 +lemma bin_rsplit_all:
  4.1078 +  "0 < nw ==> nw <= n ==> bin_rsplit n (nw, w) = [bintrunc n w]"
  4.1079 +  unfolding bin_rsplit_def
  4.1080 +  by (clarsimp dest!: split_bintrunc simp: rsplit_aux_simp2ls split: ls_splits)
  4.1081 +
  4.1082 +lemma bin_rsplit_l [rule_format] :
  4.1083 +  "ALL bin. bin_rsplitl n (m, bin) = bin_rsplit n (m, bintrunc m bin)"
  4.1084 +  apply (rule_tac a = "m" in wf_less_than [THEN wf_induct])
  4.1085 +  apply (simp (no_asm) add : bin_rsplitl_def bin_rsplit_def)
  4.1086 +  apply (rule allI)
  4.1087 +  apply (subst bin_rsplitl_aux.simps)
  4.1088 +  apply (subst bin_rsplit_aux.simps)
  4.1089 +  apply (clarsimp simp: rsplit_aux_alts Let_def split: ls_splits)
  4.1090 +  apply (drule bin_split_trunc)
  4.1091 +  apply (drule sym [THEN trans], assumption)
  4.1092 +  apply fast
  4.1093 +  done
  4.1094 +    
  4.1095 +lemma bin_rsplit_rcat [rule_format] :
  4.1096 +  "n > 0 --> bin_rsplit n (n * size ws, bin_rcat n ws) = map (bintrunc n) ws"
  4.1097 +  apply (unfold bin_rsplit_def bin_rcat_def)
  4.1098 +  apply (rule_tac xs = "ws" in rev_induct)
  4.1099 +   apply clarsimp
  4.1100 +  apply clarsimp
  4.1101 +  apply (clarsimp simp add: bin_split_cat rsplit_aux_alts)
  4.1102 +  done
  4.1103 +
  4.1104 +lemma bin_rsplit_aux_len_le [rule_format] :
  4.1105 +  "ALL ws m. n > 0 --> ws = bin_rsplit_aux (n, bs, nw, w) --> 
  4.1106 +    (length ws <= m) = (nw + length bs * n <= m * n)"
  4.1107 +  apply (rule_tac u=n and v=bs and w=nw and x=w in bin_rsplit_aux.induct)
  4.1108 +  apply (subst bin_rsplit_aux.simps)
  4.1109 +  apply (clarsimp simp: Let_def split: ls_splits)
  4.1110 +  apply (erule lrlem)
  4.1111 +  done
  4.1112 +
  4.1113 +lemma bin_rsplit_len_le: 
  4.1114 +  "n > 0 --> ws = bin_rsplit n (nw, w) --> (length ws <= m) = (nw <= m * n)"
  4.1115 +  unfolding bin_rsplit_def by (clarsimp simp add : bin_rsplit_aux_len_le)
  4.1116 + 
  4.1117 +lemma bin_rsplit_aux_len [rule_format] :
  4.1118 +  "0 < n --> length (bin_rsplit_aux (n, cs, nw, w)) = 
  4.1119 +    (nw + n - 1) div n + length cs"
  4.1120 +  apply (rule_tac u=n and v=cs and w=nw and x=w in bin_rsplit_aux.induct)
  4.1121 +  apply (subst bin_rsplit_aux.simps)
  4.1122 +  apply (clarsimp simp: Let_def split: ls_splits)
  4.1123 +  apply (erule thin_rl)
  4.1124 +  apply (case_tac "m <= n")
  4.1125 +   prefer 2
  4.1126 +   apply (simp add: div_add_self2 [symmetric])
  4.1127 +  apply (case_tac m, clarsimp)
  4.1128 +  apply (simp add: div_add_self2)
  4.1129 +  done
  4.1130 +
  4.1131 +lemma bin_rsplit_len: 
  4.1132 +  "0 < n ==> length (bin_rsplit n (nw, w)) = (nw + n - 1) div n"
  4.1133 +  unfolding bin_rsplit_def by (clarsimp simp add : bin_rsplit_aux_len)
  4.1134 +
  4.1135 +lemma bin_rsplit_aux_len_indep [rule_format] :
  4.1136 +  "0 < n ==> (ALL v bs. length bs = length cs --> 
  4.1137 +    length (bin_rsplit_aux (n, bs, nw, v)) = 
  4.1138 +    length (bin_rsplit_aux (n, cs, nw, w)))"
  4.1139 +  apply (rule_tac u=n and v=cs and w=nw and x=w in bin_rsplit_aux.induct)
  4.1140 +  apply clarsimp
  4.1141 +  apply (simp (no_asm_simp) add: bin_rsplit_aux_simp_alt Let_def 
  4.1142 +                            split: ls_splits)
  4.1143 +  apply clarify 
  4.1144 +  apply (erule allE)+
  4.1145 +  apply (erule impE)
  4.1146 +   apply (fast elim!: sym)
  4.1147 +  apply (simp (no_asm_use) add: rsplit_aux_alts)
  4.1148 +  apply (erule impE)
  4.1149 +  apply (rule_tac x="ba # bs" in exI)
  4.1150 +  apply auto
  4.1151 +  done
  4.1152 +
  4.1153 +lemma bin_rsplit_len_indep: 
  4.1154 +  "0 < n ==> length (bin_rsplit n (nw, v)) = length (bin_rsplit n (nw, w))"
  4.1155 +  apply (unfold bin_rsplit_def)
  4.1156 +  apply (erule bin_rsplit_aux_len_indep)
  4.1157 +  apply (rule refl)
  4.1158 +  done
  4.1159 +
  4.1160 +end
  4.1161 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/HOL/Word/BinGeneral.thy	Mon Aug 20 04:34:31 2007 +0200
     5.3 @@ -0,0 +1,714 @@
     5.4 +(* 
     5.5 +  ID:     $Id$
     5.6 +  Author: Jeremy Dawson, NICTA
     5.7 +
     5.8 +  contains basic definition to do with integers
     5.9 +  expressed using Pls, Min, BIT and important resulting theorems, 
    5.10 +  in particular, bin_rec and related work
    5.11 +*) 
    5.12 +
    5.13 +theory BinGeneral imports TdThs Num_Lemmas
    5.14 +
    5.15 +begin
    5.16 +
    5.17 +lemma brlem: "(bin = Numeral.Min) = (- bin + Numeral.pred 0 = 0)"
    5.18 +  unfolding Min_def pred_def by arith
    5.19 +
    5.20 +function
    5.21 +  bin_rec' :: "int * 'a * 'a * (int => bit => 'a => 'a) => 'a"  
    5.22 +  where 
    5.23 +  "bin_rec' (bin, f1, f2, f3) = (if bin = Numeral.Pls then f1 
    5.24 +    else if bin = Numeral.Min then f2
    5.25 +    else case bin_rl bin of (w, b) => f3 w b (bin_rec' (w, f1, f2, f3)))"
    5.26 +  by pat_completeness auto
    5.27 +
    5.28 +termination 
    5.29 +  apply (relation "measure (nat o abs o fst)")
    5.30 +   apply simp
    5.31 +  apply (simp add: Pls_def brlem)
    5.32 +  apply (clarsimp simp: bin_rl_char pred_def)
    5.33 +  apply (frule thin_rl [THEN refl [THEN bin_abs_lem [rule_format]]]) 
    5.34 +    apply (unfold Pls_def Min_def number_of_eq)
    5.35 +    prefer 2
    5.36 +    apply (erule asm_rl)
    5.37 +   apply auto
    5.38 +  done
    5.39 +
    5.40 +constdefs
    5.41 +  bin_rec :: "'a => 'a => (int => bit => 'a => 'a) => int => 'a"
    5.42 +  "bin_rec f1 f2 f3 bin == bin_rec' (bin, f1, f2, f3)"
    5.43 +
    5.44 +consts
    5.45 +  -- "corresponding operations analysing bins"
    5.46 +  bin_last :: "int => bit"
    5.47 +  bin_rest :: "int => int"
    5.48 +  bin_sign :: "int => int"
    5.49 +  bin_nth :: "int => nat => bool"
    5.50 +
    5.51 +primrec
    5.52 +  Z : "bin_nth w 0 = (bin_last w = bit.B1)"
    5.53 +  Suc : "bin_nth w (Suc n) = bin_nth (bin_rest w) n"
    5.54 +
    5.55 +defs  
    5.56 +  bin_rest_def : "bin_rest w == fst (bin_rl w)"
    5.57 +  bin_last_def : "bin_last w == snd (bin_rl w)"
    5.58 +  bin_sign_def : "bin_sign == bin_rec Numeral.Pls Numeral.Min (%w b s. s)"
    5.59 +
    5.60 +consts
    5.61 +  bintrunc :: "nat => int => int"
    5.62 +primrec 
    5.63 +  Z : "bintrunc 0 bin = Numeral.Pls"
    5.64 +  Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)"
    5.65 +
    5.66 +consts
    5.67 +  sbintrunc :: "nat => int => int" 
    5.68 +primrec 
    5.69 +  Z : "sbintrunc 0 bin = 
    5.70 +    (case bin_last bin of bit.B1 => Numeral.Min | bit.B0 => Numeral.Pls)"
    5.71 +  Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)"
    5.72 +
    5.73 +consts
    5.74 +  bin_split :: "nat => int => int * int"
    5.75 +primrec
    5.76 +  Z : "bin_split 0 w = (w, Numeral.Pls)"
    5.77 +  Suc : "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w)
    5.78 +    in (w1, w2 BIT bin_last w))"
    5.79 +
    5.80 +consts
    5.81 +  bin_cat :: "int => nat => int => int"
    5.82 +primrec
    5.83 +  Z : "bin_cat w 0 v = w"
    5.84 +  Suc : "bin_cat w (Suc n) v = bin_cat w n (bin_rest v) BIT bin_last v"
    5.85 +
    5.86 +lemmas funpow_minus_simp = 
    5.87 +  trans [OF gen_minus [where f = "power f"] funpow_Suc, standard]
    5.88 +
    5.89 +lemmas funpow_pred_simp [simp] =
    5.90 +  funpow_minus_simp [of "number_of bin", simplified nobm1, standard]
    5.91 +
    5.92 +lemmas replicate_minus_simp = 
    5.93 +  trans [OF gen_minus [where f = "%n. replicate n x"] replicate.replicate_Suc,
    5.94 +         standard]
    5.95 +
    5.96 +lemmas replicate_pred_simp [simp] =
    5.97 +  replicate_minus_simp [of "number_of bin", simplified nobm1, standard]
    5.98 +
    5.99 +lemmas power_Suc_no [simp] = power_Suc [of "number_of ?a"]
   5.100 +
   5.101 +lemmas power_minus_simp = 
   5.102 +  trans [OF gen_minus [where f = "power f"] power_Suc, standard]
   5.103 +
   5.104 +lemmas power_pred_simp = 
   5.105 +  power_minus_simp [of "number_of bin", simplified nobm1, standard]
   5.106 +lemmas power_pred_simp_no [simp] = power_pred_simp [where f= "number_of ?f"]
   5.107 +
   5.108 +lemma bin_rl: "bin_rl w = (bin_rest w, bin_last w)"
   5.109 +  unfolding bin_rest_def bin_last_def by auto
   5.110 +
   5.111 +lemmas bin_rl_simp [simp] = iffD1 [OF bin_rl_char bin_rl]
   5.112 +
   5.113 +lemma bin_rec_PM:
   5.114 +  "f = bin_rec f1 f2 f3 ==> f Numeral.Pls = f1 & f Numeral.Min = f2"
   5.115 +  apply safe
   5.116 +   apply (unfold bin_rec_def)
   5.117 +   apply (auto intro: bin_rec'.simps [THEN trans])
   5.118 +  done
   5.119 +
   5.120 +lemmas bin_rec_Pls = refl [THEN bin_rec_PM, THEN conjunct1, standard]
   5.121 +lemmas bin_rec_Min = refl [THEN bin_rec_PM, THEN conjunct2, standard]
   5.122 +
   5.123 +lemma bin_rec_Bit:
   5.124 +  "f = bin_rec f1 f2 f3  ==> f3 Numeral.Pls bit.B0 f1 = f1 ==> 
   5.125 +    f3 Numeral.Min bit.B1 f2 = f2 ==> f (w BIT b) = f3 w b (f w)"
   5.126 +  apply clarify
   5.127 +  apply (unfold bin_rec_def)
   5.128 +  apply (rule bin_rec'.simps [THEN trans])
   5.129 +  apply auto
   5.130 +       apply (unfold Pls_def Min_def Bit_def)
   5.131 +       apply (cases b, auto)+
   5.132 +  done
   5.133 +
   5.134 +lemmas bin_rec_simps = refl [THEN bin_rec_Bit] bin_rec_Pls bin_rec_Min
   5.135 +
   5.136 +lemma bin_rest_simps [simp]: 
   5.137 +  "bin_rest Numeral.Pls = Numeral.Pls"
   5.138 +  "bin_rest Numeral.Min = Numeral.Min"
   5.139 +  "bin_rest (w BIT b) = w"
   5.140 +  unfolding bin_rest_def by auto
   5.141 +
   5.142 +lemma bin_last_simps [simp]: 
   5.143 +  "bin_last Numeral.Pls = bit.B0"
   5.144 +  "bin_last Numeral.Min = bit.B1"
   5.145 +  "bin_last (w BIT b) = b"
   5.146 +  unfolding bin_last_def by auto
   5.147 +    
   5.148 +lemma bin_sign_simps [simp]:
   5.149 +  "bin_sign Numeral.Pls = Numeral.Pls"
   5.150 +  "bin_sign Numeral.Min = Numeral.Min"
   5.151 +  "bin_sign (w BIT b) = bin_sign w"
   5.152 +  unfolding bin_sign_def by (auto simp: bin_rec_simps)
   5.153 +
   5.154 +lemma bin_r_l_extras [simp]:
   5.155 +  "bin_last 0 = bit.B0"
   5.156 +  "bin_last (- 1) = bit.B1"
   5.157 +  "bin_last -1 = bit.B1"
   5.158 +  "bin_last 1 = bit.B1"
   5.159 +  "bin_rest 1 = 0"
   5.160 +  "bin_rest 0 = 0"
   5.161 +  "bin_rest (- 1) = - 1"
   5.162 +  "bin_rest -1 = -1"
   5.163 +  apply (unfold number_of_Min)
   5.164 +  apply (unfold Pls_def [symmetric] Min_def [symmetric])
   5.165 +  apply (unfold numeral_1_eq_1 [symmetric])
   5.166 +  apply (auto simp: number_of_eq) 
   5.167 +  done
   5.168 +
   5.169 +lemma bin_last_mod: 
   5.170 +  "bin_last w = (if w mod 2 = 0 then bit.B0 else bit.B1)"
   5.171 +  apply (case_tac w rule: bin_exhaust)
   5.172 +  apply (case_tac b)
   5.173 +   apply auto
   5.174 +  done
   5.175 +
   5.176 +lemma bin_rest_div: 
   5.177 +  "bin_rest w = w div 2"
   5.178 +  apply (case_tac w rule: bin_exhaust)
   5.179 +  apply (rule trans)
   5.180 +   apply clarsimp
   5.181 +   apply (rule refl)
   5.182 +  apply (drule trans)
   5.183 +   apply (rule Bit_def)
   5.184 +  apply (simp add: z1pdiv2 split: bit.split)
   5.185 +  done
   5.186 +
   5.187 +lemma Bit_div2 [simp]: "(w BIT b) div 2 = w"
   5.188 +  unfolding bin_rest_div [symmetric] by auto
   5.189 +
   5.190 +lemma bin_nth_lem [rule_format]:
   5.191 +  "ALL y. bin_nth x = bin_nth y --> x = y"
   5.192 +  apply (induct x rule: bin_induct)
   5.193 +    apply safe
   5.194 +    apply (erule rev_mp)
   5.195 +    apply (induct_tac y rule: bin_induct)
   5.196 +      apply safe
   5.197 +      apply (drule_tac x=0 in fun_cong, force)
   5.198 +     apply (erule notE, rule ext, 
   5.199 +            drule_tac x="Suc x" in fun_cong, force)
   5.200 +    apply (drule_tac x=0 in fun_cong, force)
   5.201 +   apply (erule rev_mp)
   5.202 +   apply (induct_tac y rule: bin_induct)
   5.203 +     apply safe
   5.204 +     apply (drule_tac x=0 in fun_cong, force)
   5.205 +    apply (erule notE, rule ext, 
   5.206 +           drule_tac x="Suc x" in fun_cong, force)
   5.207 +   apply (drule_tac x=0 in fun_cong, force)
   5.208 +  apply (case_tac y rule: bin_exhaust)
   5.209 +  apply clarify
   5.210 +  apply (erule allE)
   5.211 +  apply (erule impE)
   5.212 +   prefer 2
   5.213 +   apply (erule BIT_eqI)
   5.214 +   apply (drule_tac x=0 in fun_cong, force)
   5.215 +  apply (rule ext)
   5.216 +  apply (drule_tac x="Suc ?x" in fun_cong, force)
   5.217 +  done
   5.218 +
   5.219 +lemma bin_nth_eq_iff: "(bin_nth x = bin_nth y) = (x = y)"
   5.220 +  by (auto elim: bin_nth_lem)
   5.221 +
   5.222 +lemmas bin_eqI = ext [THEN bin_nth_eq_iff [THEN iffD1], standard]
   5.223 +
   5.224 +lemma bin_nth_Pls [simp]: "~ bin_nth Numeral.Pls n"
   5.225 +  by (induct n) auto
   5.226 +
   5.227 +lemma bin_nth_Min [simp]: "bin_nth Numeral.Min n"
   5.228 +  by (induct n) auto
   5.229 +
   5.230 +lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 = (b = bit.B1)"
   5.231 +  by auto
   5.232 +
   5.233 +lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n"
   5.234 +  by auto
   5.235 +
   5.236 +lemma bin_nth_minus [simp]: "0 < n ==> bin_nth (w BIT b) n = bin_nth w (n - 1)"
   5.237 +  by (cases n) auto
   5.238 +
   5.239 +lemmas bin_nth_0 = bin_nth.simps(1)
   5.240 +lemmas bin_nth_Suc = bin_nth.simps(2)
   5.241 +
   5.242 +lemmas bin_nth_simps = 
   5.243 +  bin_nth_0 bin_nth_Suc bin_nth_Pls bin_nth_Min bin_nth_minus
   5.244 +
   5.245 +lemma sign_bintr:
   5.246 +  "!!w. bin_sign (bintrunc n w) = Numeral.Pls"
   5.247 +  by (induct n) auto
   5.248 +
   5.249 +lemma bin_sign_rest [simp]: 
   5.250 +  "bin_sign (bin_rest w) = (bin_sign w)"
   5.251 +  by (case_tac w rule: bin_exhaust) auto
   5.252 +
   5.253 +lemma bintrunc_mod2p:
   5.254 +  "!!w. bintrunc n w = (w mod 2 ^ n :: int)"
   5.255 +  apply (induct n, clarsimp)
   5.256 +  apply (simp add: bin_last_mod bin_rest_div Bit_def zmod_zmult2_eq
   5.257 +              cong: number_of_False_cong)
   5.258 +  done
   5.259 +
   5.260 +lemma sbintrunc_mod2p:
   5.261 +  "!!w. sbintrunc n w = ((w + 2 ^ n) mod 2 ^ (Suc n) - 2 ^ n :: int)"
   5.262 +  apply (induct n)
   5.263 +   apply clarsimp
   5.264 +   apply (subst zmod_zadd_left_eq)
   5.265 +   apply (simp add: bin_last_mod)
   5.266 +   apply (simp add: number_of_eq)
   5.267 +  apply clarsimp
   5.268 +  apply (simp add: bin_last_mod bin_rest_div Bit_def 
   5.269 +              cong: number_of_False_cong)
   5.270 +  apply (clarsimp simp: zmod_zmult_zmult1 [symmetric] 
   5.271 +         zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2 [THEN sym]]])
   5.272 +  apply (rule trans [symmetric, OF _ emep1])
   5.273 +     apply auto
   5.274 +  apply (auto simp: even_def)
   5.275 +  done
   5.276 +
   5.277 +lemmas m2pths [OF zless2p, standard] = pos_mod_sign pos_mod_bound
   5.278 +
   5.279 +lemma list_exhaust_size_gt0:
   5.280 +  assumes y: "\<And>a list. y = a # list \<Longrightarrow> P"
   5.281 +  shows "0 < length y \<Longrightarrow> P"
   5.282 +  apply (cases y, simp)
   5.283 +  apply (rule y)
   5.284 +  apply fastsimp
   5.285 +  done
   5.286 +
   5.287 +lemma list_exhaust_size_eq0:
   5.288 +  assumes y: "y = [] \<Longrightarrow> P"
   5.289 +  shows "length y = 0 \<Longrightarrow> P"
   5.290 +  apply (cases y)
   5.291 +   apply (rule y, simp)
   5.292 +  apply simp
   5.293 +  done
   5.294 +
   5.295 +lemma size_Cons_lem_eq:
   5.296 +  "y = xa # list ==> size y = Suc k ==> size list = k"
   5.297 +  by auto
   5.298 +
   5.299 +lemma size_Cons_lem_eq_bin:
   5.300 +  "y = xa # list ==> size y = number_of (Numeral.succ k) ==> 
   5.301 +    size list = number_of k"
   5.302 +  by (auto simp: pred_def succ_def split add : split_if_asm)
   5.303 +
   5.304 +
   5.305 +section "Simplifications for (s)bintrunc"
   5.306 +
   5.307 +lemma bit_bool:
   5.308 +  "(b = (b' = bit.B1)) = (b' = (if b then bit.B1 else bit.B0))"
   5.309 +  by (cases b') auto
   5.310 +
   5.311 +lemmas bit_bool1 [simp] = refl [THEN bit_bool [THEN iffD1], symmetric]
   5.312 +
   5.313 +lemma bin_sign_lem:
   5.314 +  "!!bin. (bin_sign (sbintrunc n bin) = Numeral.Min) = bin_nth bin n"
   5.315 +  apply (induct n)
   5.316 +   apply (case_tac bin rule: bin_exhaust, case_tac b, auto)+
   5.317 +  done
   5.318 +
   5.319 +lemma nth_bintr:
   5.320 +  "!!w m. bin_nth (bintrunc m w) n = (n < m & bin_nth w n)"
   5.321 +  apply (induct n)
   5.322 +   apply (case_tac m, auto)[1]
   5.323 +  apply (case_tac m, auto)[1]
   5.324 +  done
   5.325 +
   5.326 +lemma nth_sbintr:
   5.327 +  "!!w m. bin_nth (sbintrunc m w) n = 
   5.328 +          (if n < m then bin_nth w n else bin_nth w m)"
   5.329 +  apply (induct n)
   5.330 +   apply (case_tac m, simp_all split: bit.splits)[1]
   5.331 +  apply (case_tac m, simp_all split: bit.splits)[1]
   5.332 +  done
   5.333 +
   5.334 +lemma bin_nth_Bit:
   5.335 +  "bin_nth (w BIT b) n = (n = 0 & b = bit.B1 | (EX m. n = Suc m & bin_nth w m))"
   5.336 +  by (cases n) auto
   5.337 +
   5.338 +lemma bintrunc_bintrunc_l:
   5.339 +  "n <= m ==> (bintrunc m (bintrunc n w) = bintrunc n w)"
   5.340 +  by (rule bin_eqI) (auto simp add : nth_bintr)
   5.341 +
   5.342 +lemma sbintrunc_sbintrunc_l:
   5.343 +  "n <= m ==> (sbintrunc m (sbintrunc n w) = sbintrunc n w)"
   5.344 +  by (rule bin_eqI) (auto simp: nth_sbintr min_def)
   5.345 +
   5.346 +lemma bintrunc_bintrunc_ge:
   5.347 +  "n <= m ==> (bintrunc n (bintrunc m w) = bintrunc n w)"
   5.348 +  by (rule bin_eqI) (auto simp: nth_bintr)
   5.349 +
   5.350 +lemma bintrunc_bintrunc_min [simp]:
   5.351 +  "bintrunc m (bintrunc n w) = bintrunc (min m n) w"
   5.352 +  apply (unfold min_def)
   5.353 +  apply (rule bin_eqI)
   5.354 +  apply (auto simp: nth_bintr)
   5.355 +  done
   5.356 +
   5.357 +lemma sbintrunc_sbintrunc_min [simp]:
   5.358 +  "sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w"
   5.359 +  apply (unfold min_def)
   5.360 +  apply (rule bin_eqI)
   5.361 +  apply (auto simp: nth_sbintr)
   5.362 +  done
   5.363 +
   5.364 +lemmas bintrunc_Pls = 
   5.365 +  bintrunc.Suc [where bin="Numeral.Pls", simplified bin_last_simps bin_rest_simps]
   5.366 +
   5.367 +lemmas bintrunc_Min [simp] = 
   5.368 +  bintrunc.Suc [where bin="Numeral.Min", simplified bin_last_simps bin_rest_simps]
   5.369 +
   5.370 +lemmas bintrunc_BIT  [simp] = 
   5.371 +  bintrunc.Suc [where bin="?w BIT ?b", simplified bin_last_simps bin_rest_simps]
   5.372 +
   5.373 +lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT
   5.374 +
   5.375 +lemmas sbintrunc_Suc_Pls = 
   5.376 +  sbintrunc.Suc [where bin="Numeral.Pls", simplified bin_last_simps bin_rest_simps]
   5.377 +
   5.378 +lemmas sbintrunc_Suc_Min = 
   5.379 +  sbintrunc.Suc [where bin="Numeral.Min", simplified bin_last_simps bin_rest_simps]
   5.380 +
   5.381 +lemmas sbintrunc_Suc_BIT [simp] = 
   5.382 +  sbintrunc.Suc [where bin="?w BIT ?b", simplified bin_last_simps bin_rest_simps]
   5.383 +
   5.384 +lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min sbintrunc_Suc_BIT
   5.385 +
   5.386 +lemmas sbintrunc_Pls = 
   5.387 +  sbintrunc.Z [where bin="Numeral.Pls", 
   5.388 +               simplified bin_last_simps bin_rest_simps bit.simps]
   5.389 +
   5.390 +lemmas sbintrunc_Min = 
   5.391 +  sbintrunc.Z [where bin="Numeral.Min", 
   5.392 +               simplified bin_last_simps bin_rest_simps bit.simps]
   5.393 +
   5.394 +lemmas sbintrunc_0_BIT_B0 [simp] = 
   5.395 +  sbintrunc.Z [where bin="?w BIT bit.B0", 
   5.396 +               simplified bin_last_simps bin_rest_simps bit.simps]
   5.397 +
   5.398 +lemmas sbintrunc_0_BIT_B1 [simp] = 
   5.399 +  sbintrunc.Z [where bin="?w BIT bit.B1", 
   5.400 +               simplified bin_last_simps bin_rest_simps bit.simps]
   5.401 +
   5.402 +lemmas sbintrunc_0_simps =
   5.403 +  sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1
   5.404 +
   5.405 +lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs
   5.406 +lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs
   5.407 +
   5.408 +lemma bintrunc_minus:
   5.409 +  "0 < n ==> bintrunc (Suc (n - 1)) w = bintrunc n w"
   5.410 +  by auto
   5.411 +
   5.412 +lemma sbintrunc_minus:
   5.413 +  "0 < n ==> sbintrunc (Suc (n - 1)) w = sbintrunc n w"
   5.414 +  by auto
   5.415 +
   5.416 +lemmas bintrunc_minus_simps = 
   5.417 +  bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans], standard]
   5.418 +lemmas sbintrunc_minus_simps = 
   5.419 +  sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans], standard]
   5.420 +
   5.421 +lemma bintrunc_n_Pls [simp]:
   5.422 +  "bintrunc n Numeral.Pls = Numeral.Pls"
   5.423 +  by (induct n) auto
   5.424 +
   5.425 +lemma sbintrunc_n_PM [simp]:
   5.426 +  "sbintrunc n Numeral.Pls = Numeral.Pls"
   5.427 +  "sbintrunc n Numeral.Min = Numeral.Min"
   5.428 +  by (induct n) auto
   5.429 +
   5.430 +lemmas thobini1 = arg_cong [where f = "%w. w BIT ?b"]
   5.431 +
   5.432 +lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1]
   5.433 +lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1]
   5.434 +
   5.435 +lemmas bmsts = bintrunc_minus_simps [THEN thobini1 [THEN [2] trans], standard]
   5.436 +lemmas bintrunc_Pls_minus_I = bmsts(1)
   5.437 +lemmas bintrunc_Min_minus_I = bmsts(2)
   5.438 +lemmas bintrunc_BIT_minus_I = bmsts(3)
   5.439 +
   5.440 +lemma bintrunc_0_Min: "bintrunc 0 Numeral.Min = Numeral.Pls"
   5.441 +  by auto
   5.442 +lemma bintrunc_0_BIT: "bintrunc 0 (w BIT b) = Numeral.Pls"
   5.443 +  by auto
   5.444 +
   5.445 +lemma bintrunc_Suc_lem:
   5.446 +  "bintrunc (Suc n) x = y ==> m = Suc n ==> bintrunc m x = y"
   5.447 +  by auto
   5.448 +
   5.449 +lemmas bintrunc_Suc_Ialts = 
   5.450 +  bintrunc_Min_I bintrunc_BIT_I [THEN bintrunc_Suc_lem, standard]
   5.451 +
   5.452 +lemmas sbintrunc_BIT_I = trans [OF sbintrunc_Suc_BIT thobini1]
   5.453 +
   5.454 +lemmas sbintrunc_Suc_Is = 
   5.455 +  sbintrunc_Sucs [THEN thobini1 [THEN [2] trans], standard]
   5.456 +
   5.457 +lemmas sbintrunc_Suc_minus_Is = 
   5.458 +  sbintrunc_minus_simps [THEN thobini1 [THEN [2] trans], standard]
   5.459 +
   5.460 +lemma sbintrunc_Suc_lem:
   5.461 +  "sbintrunc (Suc n) x = y ==> m = Suc n ==> sbintrunc m x = y"
   5.462 +  by auto
   5.463 +
   5.464 +lemmas sbintrunc_Suc_Ialts = 
   5.465 +  sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem, standard]
   5.466 +
   5.467 +lemma sbintrunc_bintrunc_lt:
   5.468 +  "m > n ==> sbintrunc n (bintrunc m w) = sbintrunc n w"
   5.469 +  by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr)
   5.470 +
   5.471 +lemma bintrunc_sbintrunc_le:
   5.472 +  "m <= Suc n ==> bintrunc m (sbintrunc n w) = bintrunc m w"
   5.473 +  apply (rule bin_eqI)
   5.474 +  apply (auto simp: nth_sbintr nth_bintr)
   5.475 +   apply (subgoal_tac "x=n", safe, arith+)[1]
   5.476 +  apply (subgoal_tac "x=n", safe, arith+)[1]
   5.477 +  done
   5.478 +
   5.479 +lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le]
   5.480 +lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt]
   5.481 +lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l]
   5.482 +lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] 
   5.483 +
   5.484 +lemma bintrunc_sbintrunc' [simp]:
   5.485 +  "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w"
   5.486 +  by (cases n) (auto simp del: bintrunc.Suc)
   5.487 +
   5.488 +lemma sbintrunc_bintrunc' [simp]:
   5.489 +  "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w"
   5.490 +  by (cases n) (auto simp del: bintrunc.Suc)
   5.491 +
   5.492 +lemma bin_sbin_eq_iff: 
   5.493 +  "bintrunc (Suc n) x = bintrunc (Suc n) y <-> 
   5.494 +   sbintrunc n x = sbintrunc n y"
   5.495 +  apply (rule iffI)
   5.496 +   apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc])
   5.497 +   apply simp
   5.498 +  apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc])
   5.499 +  apply simp
   5.500 +  done
   5.501 +
   5.502 +lemma bin_sbin_eq_iff':
   5.503 +  "0 < n \<Longrightarrow> bintrunc n x = bintrunc n y <-> 
   5.504 +            sbintrunc (n - 1) x = sbintrunc (n - 1) y"
   5.505 +  by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc)
   5.506 +
   5.507 +lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def]
   5.508 +lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def]
   5.509 +
   5.510 +lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l]
   5.511 +lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l]
   5.512 +
   5.513 +(* although bintrunc_minus_simps, if added to default simpset,
   5.514 +  tends to get applied where it's not wanted in developing the theories,
   5.515 +  we get a version for when the word length is given literally *)
   5.516 +
   5.517 +lemmas nat_non0_gr = 
   5.518 +  trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] neq0_conv, standard]
   5.519 +
   5.520 +lemmas bintrunc_pred_simps [simp] = 
   5.521 +  bintrunc_minus_simps [of "number_of bin", simplified nobm1, standard]
   5.522 +
   5.523 +lemmas sbintrunc_pred_simps [simp] = 
   5.524 +  sbintrunc_minus_simps [of "number_of bin", simplified nobm1, standard]
   5.525 +
   5.526 +lemma no_bintr_alt:
   5.527 +  "number_of (bintrunc n w) = w mod 2 ^ n"
   5.528 +  by (simp add: number_of_eq bintrunc_mod2p)
   5.529 +
   5.530 +lemma no_bintr_alt1: "bintrunc n = (%w. w mod 2 ^ n :: int)"
   5.531 +  by (rule ext) (rule bintrunc_mod2p)
   5.532 +
   5.533 +lemma range_bintrunc: "range (bintrunc n) = {i. 0 <= i & i < 2 ^ n}"
   5.534 +  apply (unfold no_bintr_alt1)
   5.535 +  apply (auto simp add: image_iff)
   5.536 +  apply (rule exI)
   5.537 +  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
   5.538 +  done
   5.539 +
   5.540 +lemma no_bintr: 
   5.541 +  "number_of (bintrunc n w) = (number_of w mod 2 ^ n :: int)"
   5.542 +  by (simp add : bintrunc_mod2p number_of_eq)
   5.543 +
   5.544 +lemma no_sbintr_alt2: 
   5.545 +  "sbintrunc n = (%w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
   5.546 +  by (rule ext) (simp add : sbintrunc_mod2p)
   5.547 +
   5.548 +lemma no_sbintr: 
   5.549 +  "number_of (sbintrunc n w) = 
   5.550 +   ((number_of w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
   5.551 +  by (simp add : no_sbintr_alt2 number_of_eq)
   5.552 +
   5.553 +lemma range_sbintrunc: 
   5.554 +  "range (sbintrunc n) = {i. - (2 ^ n) <= i & i < 2 ^ n}"
   5.555 +  apply (unfold no_sbintr_alt2)
   5.556 +  apply (auto simp add: image_iff eq_diff_eq)
   5.557 +  apply (rule exI)
   5.558 +  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
   5.559 +  done
   5.560 +
   5.561 +lemmas sb_inc_lem = int_mod_ge' 
   5.562 +  [where n = "2 ^ (Suc ?k)" and b = "?a + 2 ^ ?k", 
   5.563 +   simplified zless2p, OF _ TrueI]
   5.564 +
   5.565 +lemmas sb_inc_lem' = 
   5.566 +  iffD1 [OF less_diff_eq, THEN sb_inc_lem, simplified OrderedGroup.diff_0]
   5.567 +
   5.568 +lemma sbintrunc_inc:
   5.569 +  "x < - (2 ^ n) ==> x + 2 ^ (Suc n) <= sbintrunc n x"
   5.570 +  unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp
   5.571 +
   5.572 +lemmas sb_dec_lem = int_mod_le' 
   5.573 +  [where n = "2 ^ (Suc ?k)" and b = "?a + 2 ^ ?k", 
   5.574 +  simplified zless2p, OF _ TrueI, simplified]
   5.575 +
   5.576 +lemmas sb_dec_lem' = iffD1 [OF diff_le_eq', THEN sb_dec_lem, simplified]
   5.577 +
   5.578 +lemma sbintrunc_dec:
   5.579 +  "x >= (2 ^ n) ==> x - 2 ^ (Suc n) >= sbintrunc n x"
   5.580 +  unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp
   5.581 +
   5.582 +lemmas zmod_uminus' = zmod_uminus [where b="?c"]
   5.583 +lemmas zpower_zmod' = zpower_zmod [where m="?c" and y="?k"]
   5.584 +
   5.585 +lemmas brdmod1s' [symmetric] = 
   5.586 +  zmod_zadd_left_eq zmod_zadd_right_eq 
   5.587 +  zmod_zsub_left_eq zmod_zsub_right_eq 
   5.588 +  zmod_zmult1_eq zmod_zmult1_eq_rev 
   5.589 +
   5.590 +lemmas brdmods' [symmetric] = 
   5.591 +  zpower_zmod' [symmetric]
   5.592 +  trans [OF zmod_zadd_left_eq zmod_zadd_right_eq] 
   5.593 +  trans [OF zmod_zsub_left_eq zmod_zsub_right_eq] 
   5.594 +  trans [OF zmod_zmult1_eq zmod_zmult1_eq_rev] 
   5.595 +  zmod_uminus' [symmetric]
   5.596 +  zmod_zadd_left_eq [where b = "1"]
   5.597 +  zmod_zsub_left_eq [where b = "1"]
   5.598 +
   5.599 +lemmas bintr_arith1s =
   5.600 +  brdmod1s' [where c="2^?n", folded pred_def succ_def bintrunc_mod2p]
   5.601 +lemmas bintr_ariths =
   5.602 +  brdmods' [where c="2^?n", folded pred_def succ_def bintrunc_mod2p]
   5.603 +
   5.604 +lemma bintr_ge0: "(0 :: int) <= number_of (bintrunc n w)"
   5.605 +  by (simp add : no_bintr m2pths)
   5.606 +
   5.607 +lemma bintr_lt2p: "number_of (bintrunc n w) < (2 ^ n :: int)"
   5.608 +  by (simp add : no_bintr m2pths)
   5.609 +
   5.610 +lemma bintr_Min: 
   5.611 +  "number_of (bintrunc n Numeral.Min) = (2 ^ n :: int) - 1"
   5.612 +  by (simp add : no_bintr m1mod2k)
   5.613 +
   5.614 +lemma sbintr_ge: "(- (2 ^ n) :: int) <= number_of (sbintrunc n w)"
   5.615 +  by (simp add : no_sbintr m2pths)
   5.616 +
   5.617 +lemma sbintr_lt: "number_of (sbintrunc n w) < (2 ^ n :: int)"
   5.618 +  by (simp add : no_sbintr m2pths)
   5.619 +
   5.620 +lemma bintrunc_Suc:
   5.621 +  "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT bin_last bin"
   5.622 +  by (case_tac bin rule: bin_exhaust) auto
   5.623 +
   5.624 +lemma sign_Pls_ge_0: 
   5.625 +  "(bin_sign bin = Numeral.Pls) = (number_of bin >= (0 :: int))"
   5.626 +  by (induct bin rule: bin_induct) auto
   5.627 +
   5.628 +lemma sign_Min_lt_0: 
   5.629 +  "(bin_sign bin = Numeral.Min) = (number_of bin < (0 :: int))"
   5.630 +  by (induct bin rule: bin_induct) auto
   5.631 +
   5.632 +lemmas sign_Min_neg = trans [OF sign_Min_lt_0 neg_def [symmetric]] 
   5.633 +
   5.634 +lemma bin_rest_trunc:
   5.635 +  "!!bin. (bin_rest (bintrunc n bin)) = bintrunc (n - 1) (bin_rest bin)"
   5.636 +  by (induct n) auto
   5.637 +
   5.638 +lemma bin_rest_power_trunc [rule_format] :
   5.639 +  "(bin_rest ^ k) (bintrunc n bin) = 
   5.640 +    bintrunc (n - k) ((bin_rest ^ k) bin)"
   5.641 +  by (induct k) (auto simp: bin_rest_trunc)
   5.642 +
   5.643 +lemma bin_rest_trunc_i:
   5.644 +  "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)"
   5.645 +  by auto
   5.646 +
   5.647 +lemma bin_rest_strunc:
   5.648 +  "!!bin. bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)"
   5.649 +  by (induct n) auto
   5.650 +
   5.651 +lemma bintrunc_rest [simp]: 
   5.652 +  "!!bin. bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)"
   5.653 +  apply (induct n, simp)
   5.654 +  apply (case_tac bin rule: bin_exhaust)
   5.655 +  apply (auto simp: bintrunc_bintrunc_l)
   5.656 +  done
   5.657 +
   5.658 +lemma sbintrunc_rest [simp]:
   5.659 +  "!!bin. sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)"
   5.660 +  apply (induct n, simp)
   5.661 +  apply (case_tac bin rule: bin_exhaust)
   5.662 +  apply (auto simp: bintrunc_bintrunc_l split: bit.splits)
   5.663 +  done
   5.664 +
   5.665 +lemma bintrunc_rest':
   5.666 +  "bintrunc n o bin_rest o bintrunc n = bin_rest o bintrunc n"
   5.667 +  by (rule ext) auto
   5.668 +
   5.669 +lemma sbintrunc_rest' :
   5.670 +  "sbintrunc n o bin_rest o sbintrunc n = bin_rest o sbintrunc n"
   5.671 +  by (rule ext) auto
   5.672 +
   5.673 +lemma rco_lem:
   5.674 +  "f o g o f = g o f ==> f o (g o f) ^ n = g ^ n o f"
   5.675 +  apply (rule ext)
   5.676 +  apply (induct_tac n)
   5.677 +   apply (simp_all (no_asm))
   5.678 +  apply (drule fun_cong)
   5.679 +  apply (unfold o_def)
   5.680 +  apply (erule trans)
   5.681 +  apply simp
   5.682 +  done
   5.683 +
   5.684 +lemma rco_alt: "(f o g) ^ n o f = f o (g o f) ^ n"
   5.685 +  apply (rule ext)
   5.686 +  apply (induct n)
   5.687 +   apply (simp_all add: o_def)
   5.688 +  done
   5.689 +
   5.690 +lemmas rco_bintr = bintrunc_rest' 
   5.691 +  [THEN rco_lem [THEN fun_cong], unfolded o_def]
   5.692 +lemmas rco_sbintr = sbintrunc_rest' 
   5.693 +  [THEN rco_lem [THEN fun_cong], unfolded o_def]
   5.694 +
   5.695 +lemmas ls_splits = 
   5.696 +  prod.split split_split prod.split_asm split_split_asm split_if_asm
   5.697 +
   5.698 +lemma not_B1_is_B0: "y \<noteq> bit.B1 \<Longrightarrow> y = bit.B0"
   5.699 +  by (cases y) auto
   5.700 +
   5.701 +lemma B1_ass_B0: 
   5.702 +  assumes y: "y = bit.B0 \<Longrightarrow> y = bit.B1"
   5.703 +  shows "y = bit.B1"
   5.704 +  apply (rule classical)
   5.705 +  apply (drule not_B1_is_B0)
   5.706 +  apply (erule y)
   5.707 +  done
   5.708 +
   5.709 +-- "simplifications for specific word lengths"
   5.710 +lemmas n2s_ths [THEN eq_reflection] = add_2_eq_Suc add_2_eq_Suc'
   5.711 +
   5.712 +lemmas s2n_ths = n2s_ths [symmetric]
   5.713 +
   5.714 +
   5.715 +end
   5.716 +
   5.717 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/HOL/Word/BinOperations.thy	Mon Aug 20 04:34:31 2007 +0200
     6.3 @@ -0,0 +1,678 @@
     6.4 +(* 
     6.5 +  ID:     $Id$
     6.6 +  Author: Jeremy Dawson and Gerwin Klein, NICTA
     6.7 +
     6.8 +  definition and basic theorems for bit-wise logical operations 
     6.9 +  for integers expressed using Pls, Min, BIT,
    6.10 +  and converting them to and from lists of bools
    6.11 +*) 
    6.12 +
    6.13 +theory BinOperations imports BinGeneral
    6.14 +
    6.15 +begin
    6.16 +
    6.17 +-- "bit-wise logical operations on the int type"
    6.18 +consts
    6.19 +  int_and :: "int => int => int"
    6.20 +  int_or :: "int => int => int"
    6.21 +  bit_not :: "bit => bit"
    6.22 +  bit_and :: "bit => bit => bit"
    6.23 +  bit_or :: "bit => bit => bit"
    6.24 +  bit_xor :: "bit => bit => bit"
    6.25 +  int_not :: "int => int"
    6.26 +  int_xor :: "int => int => int"
    6.27 +  bin_sc :: "nat => bit => int => int"
    6.28 +
    6.29 +primrec
    6.30 +  B0 : "bit_not bit.B0 = bit.B1"
    6.31 +  B1 : "bit_not bit.B1 = bit.B0"
    6.32 +
    6.33 +primrec
    6.34 +  B1 : "bit_xor bit.B1 x = bit_not x"
    6.35 +  B0 : "bit_xor bit.B0 x = x"
    6.36 +
    6.37 +primrec
    6.38 +  B1 : "bit_or bit.B1 x = bit.B1"
    6.39 +  B0 : "bit_or bit.B0 x = x"
    6.40 +
    6.41 +primrec
    6.42 +  B0 : "bit_and bit.B0 x = bit.B0"
    6.43 +  B1 : "bit_and bit.B1 x = x"
    6.44 +
    6.45 +primrec
    6.46 +  Z : "bin_sc 0 b w = bin_rest w BIT b"
    6.47 +  Suc :
    6.48 +    "bin_sc (Suc n) b w = bin_sc n b (bin_rest w) BIT bin_last w"
    6.49 +
    6.50 +defs
    6.51 +  int_not_def : "int_not == bin_rec Numeral.Min Numeral.Pls 
    6.52 +    (%w b s. s BIT bit_not b)"
    6.53 +    int_and_def : "int_and == bin_rec (%x. Numeral.Pls) (%y. y) 
    6.54 +    (%w b s y. s (bin_rest y) BIT (bit_and b (bin_last y)))"
    6.55 +  int_or_def : "int_or == bin_rec (%x. x) (%y. Numeral.Min) 
    6.56 +    (%w b s y. s (bin_rest y) BIT (bit_or b (bin_last y)))"
    6.57 +  int_xor_def : "int_xor == bin_rec (%x. x) int_not 
    6.58 +    (%w b s y. s (bin_rest y) BIT (bit_xor b (bin_last y)))"
    6.59 +
    6.60 +consts
    6.61 +  bin_to_bl :: "nat => int => bool list"
    6.62 +  bin_to_bl_aux :: "nat => int => bool list => bool list"
    6.63 +  bl_to_bin :: "bool list => int"
    6.64 +  bl_to_bin_aux :: "int => bool list => int"
    6.65 +
    6.66 +  bl_of_nth :: "nat => (nat => bool) => bool list"
    6.67 +
    6.68 +primrec
    6.69 +  Nil : "bl_to_bin_aux w [] = w"
    6.70 +  Cons : "bl_to_bin_aux w (b # bs) = 
    6.71 +      bl_to_bin_aux (w BIT (if b then bit.B1 else bit.B0)) bs"
    6.72 +
    6.73 +primrec
    6.74 +  Z : "bin_to_bl_aux 0 w bl = bl"
    6.75 +  Suc : "bin_to_bl_aux (Suc n) w bl =
    6.76 +    bin_to_bl_aux n (bin_rest w) ((bin_last w = bit.B1) # bl)"
    6.77 +
    6.78 +defs
    6.79 +  bin_to_bl_def : "bin_to_bl n w == bin_to_bl_aux n w []"
    6.80 +  bl_to_bin_def : "bl_to_bin bs == bl_to_bin_aux Numeral.Pls bs"
    6.81 +
    6.82 +primrec
    6.83 +  Suc : "bl_of_nth (Suc n) f = f n # bl_of_nth n f"
    6.84 +  Z : "bl_of_nth 0 f = []"
    6.85 +
    6.86 +consts
    6.87 +  takefill :: "'a => nat => 'a list => 'a list"
    6.88 +  app2 :: "('a => 'b => 'c) => 'a list => 'b list => 'c list"
    6.89 +
    6.90 +-- "takefill - like take but if argument list too short,"
    6.91 +-- "extends result to get requested length"
    6.92 +primrec
    6.93 +  Z : "takefill fill 0 xs = []"
    6.94 +  Suc : "takefill fill (Suc n) xs = (
    6.95 +    case xs of [] => fill # takefill fill n xs
    6.96 +      | y # ys => y # takefill fill n ys)"
    6.97 +
    6.98 +defs
    6.99 +  app2_def : "app2 f as bs == map (split f) (zip as bs)"
   6.100 +
   6.101 +-- "rcat and rsplit"
   6.102 +consts
   6.103 +  bin_rcat :: "nat => int list => int"
   6.104 +  bin_rsplit_aux :: "nat * int list * nat * int => int list"
   6.105 +  bin_rsplit :: "nat => (nat * int) => int list"
   6.106 +  bin_rsplitl_aux :: "nat * int list * nat * int => int list"
   6.107 +  bin_rsplitl :: "nat => (nat * int) => int list"
   6.108 +  
   6.109 +recdef bin_rsplit_aux "measure (fst o snd o snd)"
   6.110 +  "bin_rsplit_aux (n, bs, (m, c)) =
   6.111 +    (if m = 0 | n = 0 then bs else
   6.112 +      let (a, b) = bin_split n c 
   6.113 +      in bin_rsplit_aux (n, b # bs, (m - n, a)))"
   6.114 +
   6.115 +recdef bin_rsplitl_aux "measure (fst o snd o snd)"
   6.116 +  "bin_rsplitl_aux (n, bs, (m, c)) =
   6.117 +    (if m = 0 | n = 0 then bs else
   6.118 +      let (a, b) = bin_split (min m n) c 
   6.119 +      in bin_rsplitl_aux (n, b # bs, (m - n, a)))"
   6.120 +
   6.121 +defs
   6.122 +  bin_rcat_def : "bin_rcat n bs == foldl (%u v. bin_cat u n v) Numeral.Pls bs"
   6.123 +  bin_rsplit_def : "bin_rsplit n w == bin_rsplit_aux (n, [], w)"
   6.124 +  bin_rsplitl_def : "bin_rsplitl n w == bin_rsplitl_aux (n, [], w)"
   6.125 +     
   6.126 + 
   6.127 +lemma int_not_simps [simp]:
   6.128 +  "int_not Numeral.Pls = Numeral.Min"
   6.129 +  "int_not Numeral.Min = Numeral.Pls"
   6.130 +  "int_not (w BIT b) = int_not w BIT bit_not b"
   6.131 +  by (unfold int_not_def) (auto intro: bin_rec_simps)
   6.132 +
   6.133 +lemma bit_extra_simps [simp]: 
   6.134 +  "bit_and x bit.B0 = bit.B0"
   6.135 +  "bit_and x bit.B1 = x"
   6.136 +  "bit_or x bit.B1 = bit.B1"
   6.137 +  "bit_or x bit.B0 = x"
   6.138 +  "bit_xor x bit.B1 = bit_not x"
   6.139 +  "bit_xor x bit.B0 = x"
   6.140 +  by (cases x, auto)+
   6.141 +
   6.142 +lemma bit_ops_comm: 
   6.143 +  "bit_and x y = bit_and y x"
   6.144 +  "bit_or x y = bit_or y x"
   6.145 +  "bit_xor x y = bit_xor y x"
   6.146 +  by (cases y, auto)+
   6.147 +
   6.148 +lemma bit_ops_same [simp]: 
   6.149 +  "bit_and x x = x"
   6.150 +  "bit_or x x = x"
   6.151 +  "bit_xor x x = bit.B0"
   6.152 +  by (cases x, auto)+
   6.153 +
   6.154 +lemma bit_not_not [simp]: "bit_not (bit_not x) = x"
   6.155 +  by (cases x) auto
   6.156 +
   6.157 +lemma int_xor_Pls [simp]: 
   6.158 +  "int_xor Numeral.Pls x = x"
   6.159 +  unfolding int_xor_def by (simp add: bin_rec_PM)
   6.160 +
   6.161 +lemma int_xor_Min [simp]: 
   6.162 +  "int_xor Numeral.Min x = int_not x"
   6.163 +  unfolding int_xor_def by (simp add: bin_rec_PM)
   6.164 +
   6.165 +lemma int_xor_Bits [simp]: 
   6.166 +  "int_xor (x BIT b) (y BIT c) = int_xor x y BIT bit_xor b c"
   6.167 +  apply (unfold int_xor_def)
   6.168 +  apply (rule bin_rec_simps (1) [THEN fun_cong, THEN trans])
   6.169 +    apply (rule ext, simp)
   6.170 +   prefer 2
   6.171 +   apply simp
   6.172 +  apply (rule ext)
   6.173 +  apply (simp add: int_not_simps [symmetric])
   6.174 +  done
   6.175 +
   6.176 +lemma int_xor_x_simps':
   6.177 +  "int_xor w (Numeral.Pls BIT bit.B0) = w"
   6.178 +  "int_xor w (Numeral.Min BIT bit.B1) = int_not w"
   6.179 +  apply (induct w rule: bin_induct)
   6.180 +       apply simp_all[4]
   6.181 +   apply (unfold int_xor_Bits)
   6.182 +   apply clarsimp+
   6.183 +  done
   6.184 +
   6.185 +lemmas int_xor_extra_simps [simp] = int_xor_x_simps' [simplified arith_simps]
   6.186 +
   6.187 +lemma int_or_Pls [simp]: 
   6.188 +  "int_or Numeral.Pls x = x"
   6.189 +  by (unfold int_or_def) (simp add: bin_rec_PM)
   6.190 +  
   6.191 +lemma int_or_Min [simp]:
   6.192 +  "int_or Numeral.Min x = Numeral.Min"
   6.193 +  by (unfold int_or_def) (simp add: bin_rec_PM)
   6.194 +
   6.195 +lemma int_or_Bits [simp]: 
   6.196 +  "int_or (x BIT b) (y BIT c) = int_or x y BIT bit_or b c"
   6.197 +  unfolding int_or_def by (simp add: bin_rec_simps)
   6.198 +
   6.199 +lemma int_or_x_simps': 
   6.200 +  "int_or w (Numeral.Pls BIT bit.B0) = w"
   6.201 +  "int_or w (Numeral.Min BIT bit.B1) = Numeral.Min"
   6.202 +  apply (induct w rule: bin_induct)
   6.203 +       apply simp_all[4]
   6.204 +   apply (unfold int_or_Bits)
   6.205 +   apply clarsimp+
   6.206 +  done
   6.207 +
   6.208 +lemmas int_or_extra_simps [simp] = int_or_x_simps' [simplified arith_simps]
   6.209 +
   6.210 +
   6.211 +lemma int_and_Pls [simp]:
   6.212 +  "int_and Numeral.Pls x = Numeral.Pls"
   6.213 +  unfolding int_and_def by (simp add: bin_rec_PM)
   6.214 +
   6.215 +lemma  int_and_Min [simp]:
   6.216 +  "int_and Numeral.Min x = x"
   6.217 +  unfolding int_and_def by (simp add: bin_rec_PM)
   6.218 +
   6.219 +lemma int_and_Bits [simp]: 
   6.220 +  "int_and (x BIT b) (y BIT c) = int_and x y BIT bit_and b c" 
   6.221 +  unfolding int_and_def by (simp add: bin_rec_simps)
   6.222 +
   6.223 +lemma int_and_x_simps': 
   6.224 +  "int_and w (Numeral.Pls BIT bit.B0) = Numeral.Pls"
   6.225 +  "int_and w (Numeral.Min BIT bit.B1) = w"
   6.226 +  apply (induct w rule: bin_induct)
   6.227 +       apply simp_all[4]
   6.228 +   apply (unfold int_and_Bits)
   6.229 +   apply clarsimp+
   6.230 +  done
   6.231 +
   6.232 +lemmas int_and_extra_simps [simp] = int_and_x_simps' [simplified arith_simps]
   6.233 +
   6.234 +(* commutativity of the above *)
   6.235 +lemma bin_ops_comm:
   6.236 +  shows
   6.237 +  int_and_comm: "!!y. int_and x y = int_and y x" and
   6.238 +  int_or_comm:  "!!y. int_or x y = int_or y x" and
   6.239 +  int_xor_comm: "!!y. int_xor x y = int_xor y x"
   6.240 +  apply (induct x rule: bin_induct)
   6.241 +          apply simp_all[6]
   6.242 +    apply (case_tac y rule: bin_exhaust, simp add: bit_ops_comm)+
   6.243 +  done
   6.244 +
   6.245 +lemma bin_ops_same [simp]:
   6.246 +  "int_and x x = x" 
   6.247 +  "int_or x x = x" 
   6.248 +  "int_xor x x = Numeral.Pls"
   6.249 +  by (induct x rule: bin_induct) auto
   6.250 +
   6.251 +lemma int_not_not [simp]: "int_not (int_not x) = x"
   6.252 +  by (induct x rule: bin_induct) auto
   6.253 +
   6.254 +lemmas bin_log_esimps = 
   6.255 +  int_and_extra_simps  int_or_extra_simps  int_xor_extra_simps
   6.256 +  int_and_Pls int_and_Min  int_or_Pls int_or_Min  int_xor_Pls int_xor_Min
   6.257 +
   6.258 +(* potential for looping *)
   6.259 +declare bin_rsplit_aux.simps [simp del]
   6.260 +declare bin_rsplitl_aux.simps [simp del]
   6.261 +
   6.262 +
   6.263 +lemma bin_sign_cat: 
   6.264 +  "!!y. bin_sign (bin_cat x n y) = bin_sign x"
   6.265 +  by (induct n) auto
   6.266 +
   6.267 +lemma bin_cat_Suc_Bit:
   6.268 +  "bin_cat w (Suc n) (v BIT b) = bin_cat w n v BIT b"
   6.269 +  by auto
   6.270 +
   6.271 +lemma bin_nth_cat: 
   6.272 +  "!!n y. bin_nth (bin_cat x k y) n = 
   6.273 +    (if n < k then bin_nth y n else bin_nth x (n - k))"
   6.274 +  apply (induct k)
   6.275 +   apply clarsimp
   6.276 +  apply (case_tac n, auto)
   6.277 +  done
   6.278 +
   6.279 +lemma bin_nth_split:
   6.280 +  "!!b c. bin_split n c = (a, b) ==> 
   6.281 +    (ALL k. bin_nth a k = bin_nth c (n + k)) & 
   6.282 +    (ALL k. bin_nth b k = (k < n & bin_nth c k))"
   6.283 +  apply (induct n)
   6.284 +   apply clarsimp
   6.285 +  apply (clarsimp simp: Let_def split: ls_splits)
   6.286 +  apply (case_tac k)
   6.287 +  apply auto
   6.288 +  done
   6.289 +
   6.290 +lemma bin_cat_assoc: 
   6.291 +  "!!z. bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)" 
   6.292 +  by (induct n) auto
   6.293 +
   6.294 +lemma bin_cat_assoc_sym: "!!z m. 
   6.295 +  bin_cat x m (bin_cat y n z) = bin_cat (bin_cat x (m - n) y) (min m n) z"
   6.296 +  apply (induct n, clarsimp)
   6.297 +  apply (case_tac m, auto)
   6.298 +  done
   6.299 +
   6.300 +lemma bin_cat_Pls [simp]: 
   6.301 +  "!!w. bin_cat Numeral.Pls n w = bintrunc n w"
   6.302 +  by (induct n) auto
   6.303 +
   6.304 +lemma bintr_cat1: 
   6.305 +  "!!b. bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b"
   6.306 +  by (induct n) auto
   6.307 +    
   6.308 +lemma bintr_cat: "bintrunc m (bin_cat a n b) = 
   6.309 +    bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)"
   6.310 +  by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr)
   6.311 +    
   6.312 +lemma bintr_cat_same [simp]: 
   6.313 +  "bintrunc n (bin_cat a n b) = bintrunc n b"
   6.314 +  by (auto simp add : bintr_cat)
   6.315 +
   6.316 +lemma cat_bintr [simp]: 
   6.317 +  "!!b. bin_cat a n (bintrunc n b) = bin_cat a n b"
   6.318 +  by (induct n) auto
   6.319 +
   6.320 +lemma split_bintrunc: 
   6.321 +  "!!b c. bin_split n c = (a, b) ==> b = bintrunc n c"
   6.322 +  by (induct n) (auto simp: Let_def split: ls_splits)
   6.323 +
   6.324 +lemma bin_cat_split:
   6.325 +  "!!v w. bin_split n w = (u, v) ==> w = bin_cat u n v"
   6.326 +  by (induct n) (auto simp: Let_def split: ls_splits)
   6.327 +
   6.328 +lemma bin_split_cat:
   6.329 +  "!!w. bin_split n (bin_cat v n w) = (v, bintrunc n w)"
   6.330 +  by (induct n) auto
   6.331 +
   6.332 +lemma bin_split_Pls [simp]:
   6.333 +  "bin_split n Numeral.Pls = (Numeral.Pls, Numeral.Pls)"
   6.334 +  by (induct n) (auto simp: Let_def split: ls_splits)
   6.335 +
   6.336 +lemma bin_split_Min [simp]:
   6.337 +  "bin_split n Numeral.Min = (Numeral.Min, bintrunc n Numeral.Min)"
   6.338 +  by (induct n) (auto simp: Let_def split: ls_splits)
   6.339 +
   6.340 +lemma bin_split_trunc:
   6.341 +  "!!m b c. bin_split (min m n) c = (a, b) ==> 
   6.342 +    bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)"
   6.343 +  apply (induct n, clarsimp)
   6.344 +  apply (simp add: bin_rest_trunc Let_def split: ls_splits)
   6.345 +  apply (case_tac m)
   6.346 +   apply (auto simp: Let_def split: ls_splits)
   6.347 +  done
   6.348 +
   6.349 +lemma bin_split_trunc1:
   6.350 +  "!!m b c. bin_split n c = (a, b) ==> 
   6.351 +    bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)"
   6.352 +  apply (induct n, clarsimp)
   6.353 +  apply (simp add: bin_rest_trunc Let_def split: ls_splits)
   6.354 +  apply (case_tac m)
   6.355 +   apply (auto simp: Let_def split: ls_splits)
   6.356 +  done
   6.357 +
   6.358 +lemma bin_cat_num:
   6.359 +  "!!b. bin_cat a n b = a * 2 ^ n + bintrunc n b"
   6.360 +  apply (induct n, clarsimp)
   6.361 +  apply (simp add: Bit_def cong: number_of_False_cong)
   6.362 +  done
   6.363 +
   6.364 +lemma bin_split_num:
   6.365 +  "!!b. bin_split n b = (b div 2 ^ n, b mod 2 ^ n)"
   6.366 +  apply (induct n, clarsimp)
   6.367 +  apply (simp add: bin_rest_div zdiv_zmult2_eq)
   6.368 +  apply (case_tac b rule: bin_exhaust)
   6.369 +  apply simp
   6.370 +  apply (simp add: Bit_def zmod_zmult_zmult1 p1mod22k
   6.371 +              split: bit.split 
   6.372 +              cong: number_of_False_cong)
   6.373 +  done 
   6.374 +
   6.375 +
   6.376 +(* basic properties of logical (bit-wise) operations *)
   6.377 +
   6.378 +lemma bbw_ao_absorb: 
   6.379 +  "!!y. int_and x (int_or y x) = x & int_or x (int_and y x) = x"
   6.380 +  apply (induct x rule: bin_induct)
   6.381 +    apply auto 
   6.382 +   apply (case_tac [!] y rule: bin_exhaust)
   6.383 +   apply auto
   6.384 +   apply (case_tac [!] bit)
   6.385 +     apply auto
   6.386 +  done
   6.387 +
   6.388 +lemma bbw_ao_absorbs_other:
   6.389 +  "int_and x (int_or x y) = x \<and> int_or (int_and y x) x = x"
   6.390 +  "int_and (int_or y x) x = x \<and> int_or x (int_and x y) = x"
   6.391 +  "int_and (int_or x y) x = x \<and> int_or (int_and x y) x = x"
   6.392 +  apply (auto simp: bbw_ao_absorb int_or_comm)  
   6.393 +      apply (subst int_or_comm)
   6.394 +    apply (simp add: bbw_ao_absorb)
   6.395 +   apply (subst int_and_comm)
   6.396 +   apply (subst int_or_comm)
   6.397 +   apply (simp add: bbw_ao_absorb)
   6.398 +  apply (subst int_and_comm)
   6.399 +  apply (simp add: bbw_ao_absorb)
   6.400 +  done
   6.401 +  
   6.402 +lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other
   6.403 +
   6.404 +lemma int_xor_not:
   6.405 +  "!!y. int_xor (int_not x) y = int_not (int_xor x y) & 
   6.406 +        int_xor x (int_not y) = int_not (int_xor x y)"
   6.407 +  apply (induct x rule: bin_induct)
   6.408 +    apply auto
   6.409 +   apply (case_tac y rule: bin_exhaust, auto, 
   6.410 +          case_tac b, auto)+
   6.411 +  done
   6.412 +
   6.413 +lemma bbw_assocs': 
   6.414 +  "!!y z. int_and (int_and x y) z = int_and x (int_and y z) & 
   6.415 +          int_or (int_or x y) z = int_or x (int_or y z) & 
   6.416 +          int_xor (int_xor x y) z = int_xor x (int_xor y z)"
   6.417 +  apply (induct x rule: bin_induct)
   6.418 +    apply (auto simp: int_xor_not)
   6.419 +    apply (case_tac [!] y rule: bin_exhaust)
   6.420 +    apply (case_tac [!] z rule: bin_exhaust)
   6.421 +    apply (case_tac [!] bit)
   6.422 +       apply (case_tac [!] b)
   6.423 +             apply auto
   6.424 +  done
   6.425 +
   6.426 +lemma int_and_assoc:
   6.427 +  "int_and (int_and x y) z = int_and x (int_and y z)"
   6.428 +  by (simp add: bbw_assocs')
   6.429 +
   6.430 +lemma int_or_assoc:
   6.431 +  "int_or (int_or x y) z = int_or x (int_or y z)"
   6.432 +  by (simp add: bbw_assocs')
   6.433 +
   6.434 +lemma int_xor_assoc:
   6.435 +  "int_xor (int_xor x y) z = int_xor x (int_xor y z)"
   6.436 +  by (simp add: bbw_assocs')
   6.437 +
   6.438 +lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc
   6.439 +
   6.440 +lemma bbw_lcs [simp]: 
   6.441 +  "int_and y (int_and x z) = int_and x (int_and y z)"
   6.442 +  "int_or y (int_or x z) = int_or x (int_or y z)"
   6.443 +  "int_xor y (int_xor x z) = int_xor x (int_xor y z)" 
   6.444 +  apply (auto simp: bbw_assocs [symmetric])
   6.445 +  apply (auto simp: bin_ops_comm)
   6.446 +  done
   6.447 +
   6.448 +lemma bbw_not_dist: 
   6.449 +  "!!y. int_not (int_or x y) = int_and (int_not x) (int_not y)" 
   6.450 +  "!!y. int_not (int_and x y) = int_or (int_not x) (int_not y)"
   6.451 +  apply (induct x rule: bin_induct)
   6.452 +       apply auto
   6.453 +   apply (case_tac [!] y rule: bin_exhaust)
   6.454 +   apply (case_tac [!] bit, auto)
   6.455 +  done
   6.456 +
   6.457 +lemma bbw_oa_dist: 
   6.458 +  "!!y z. int_or (int_and x y) z = 
   6.459 +          int_and (int_or x z) (int_or y z)"
   6.460 +  apply (induct x rule: bin_induct)
   6.461 +    apply auto
   6.462 +  apply (case_tac y rule: bin_exhaust)
   6.463 +  apply (case_tac z rule: bin_exhaust)
   6.464 +  apply (case_tac ba, auto)
   6.465 +  done
   6.466 +
   6.467 +lemma bbw_ao_dist: 
   6.468 +  "!!y z. int_and (int_or x y) z = 
   6.469 +          int_or (int_and x z) (int_and y z)"
   6.470 +   apply (induct x rule: bin_induct)
   6.471 +    apply auto
   6.472 +  apply (case_tac y rule: bin_exhaust)
   6.473 +  apply (case_tac z rule: bin_exhaust)
   6.474 +  apply (case_tac ba, auto)
   6.475 +  done
   6.476 +
   6.477 +declare bin_ops_comm [simp] bbw_assocs [simp] 
   6.478 +
   6.479 +lemma plus_and_or [rule_format]:
   6.480 +  "ALL y. int_and x y + int_or x y = x + y"
   6.481 +  apply (induct x rule: bin_induct)
   6.482 +    apply clarsimp
   6.483 +   apply clarsimp
   6.484 +  apply clarsimp
   6.485 +  apply (case_tac y rule: bin_exhaust)
   6.486 +  apply clarsimp
   6.487 +  apply (unfold Bit_def)
   6.488 +  apply clarsimp
   6.489 +  apply (erule_tac x = "x" in allE)
   6.490 +  apply (simp split: bit.split)
   6.491 +  done
   6.492 +
   6.493 +lemma le_int_or:
   6.494 +  "!!x.  bin_sign y = Numeral.Pls ==> x <= int_or x y"
   6.495 +  apply (induct y rule: bin_induct)
   6.496 +    apply clarsimp
   6.497 +   apply clarsimp
   6.498 +  apply (case_tac x rule: bin_exhaust)
   6.499 +  apply (case_tac b)
   6.500 +   apply (case_tac [!] bit)
   6.501 +     apply (auto simp: less_eq_numeral_code)
   6.502 +  done
   6.503 +
   6.504 +lemmas int_and_le =
   6.505 +  xtr3 [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or] ;
   6.506 +
   6.507 +(** nth bit, set/clear **)
   6.508 +
   6.509 +lemma bin_nth_sc [simp]: 
   6.510 +  "!!w. bin_nth (bin_sc n b w) n = (b = bit.B1)"
   6.511 +  by (induct n)  auto
   6.512 +
   6.513 +lemma bin_sc_sc_same [simp]: 
   6.514 +  "!!w. bin_sc n c (bin_sc n b w) = bin_sc n c w"
   6.515 +  by (induct n) auto
   6.516 +
   6.517 +lemma bin_sc_sc_diff:
   6.518 +  "!!w m. m ~= n ==> 
   6.519 +    bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)"
   6.520 +  apply (induct n)
   6.521 +   apply safe
   6.522 +   apply (case_tac [!] m)
   6.523 +     apply auto
   6.524 +  done
   6.525 +
   6.526 +lemma bin_nth_sc_gen: 
   6.527 +  "!!w m. bin_nth (bin_sc n b w) m = (if m = n then b = bit.B1 else bin_nth w m)"
   6.528 +  by (induct n) (case_tac [!] m, auto)
   6.529 +  
   6.530 +lemma bin_sc_nth [simp]:
   6.531 +  "!!w. (bin_sc n (If (bin_nth w n) bit.B1 bit.B0) w) = w"
   6.532 +  by (induct n) auto
   6.533 +
   6.534 +lemma bin_sign_sc [simp]:
   6.535 +  "!!w. bin_sign (bin_sc n b w) = bin_sign w"
   6.536 +  by (induct n) auto
   6.537 +  
   6.538 +lemma bin_sc_bintr [simp]: 
   6.539 +  "!!w m. bintrunc m (bin_sc n x (bintrunc m (w))) = bintrunc m (bin_sc n x w)"
   6.540 +  apply (induct n)
   6.541 +   apply (case_tac [!] w rule: bin_exhaust)
   6.542 +   apply (case_tac [!] m, auto)
   6.543 +  done
   6.544 +
   6.545 +lemma bin_clr_le:
   6.546 +  "!!w. bin_sc n bit.B0 w <= w"
   6.547 +  apply (induct n) 
   6.548 +   apply (case_tac [!] w rule: bin_exhaust)
   6.549 +   apply auto
   6.550 +   apply (unfold Bit_def)
   6.551 +   apply (simp_all split: bit.split)
   6.552 +  done
   6.553 +
   6.554 +lemma bin_set_ge:
   6.555 +  "!!w. bin_sc n bit.B1 w >= w"
   6.556 +  apply (induct n) 
   6.557 +   apply (case_tac [!] w rule: bin_exhaust)
   6.558 +   apply auto
   6.559 +   apply (unfold Bit_def)
   6.560 +   apply (simp_all split: bit.split)
   6.561 +  done
   6.562 +
   6.563 +lemma bintr_bin_clr_le:
   6.564 +  "!!w m. bintrunc n (bin_sc m bit.B0 w) <= bintrunc n w"
   6.565 +  apply (induct n)
   6.566 +   apply simp
   6.567 +  apply (case_tac w rule: bin_exhaust)
   6.568 +  apply (case_tac m)
   6.569 +   apply auto
   6.570 +   apply (unfold Bit_def)
   6.571 +   apply (simp_all split: bit.split)
   6.572 +  done
   6.573 +
   6.574 +lemma bintr_bin_set_ge:
   6.575 +  "!!w m. bintrunc n (bin_sc m bit.B1 w) >= bintrunc n w"
   6.576 +  apply (induct n)
   6.577 +   apply simp
   6.578 +  apply (case_tac w rule: bin_exhaust)
   6.579 +  apply (case_tac m)
   6.580 +   apply auto
   6.581 +   apply (unfold Bit_def)
   6.582 +   apply (simp_all split: bit.split)
   6.583 +  done
   6.584 +
   6.585 +lemma bin_nth_ops:
   6.586 +  "!!x y. bin_nth (int_and x y) n = (bin_nth x n & bin_nth y n)" 
   6.587 +  "!!x y. bin_nth (int_or x y) n = (bin_nth x n | bin_nth y n)"
   6.588 +  "!!x y. bin_nth (int_xor x y) n = (bin_nth x n ~= bin_nth y n)" 
   6.589 +  "!!x. bin_nth (int_not x) n = (~ bin_nth x n)"
   6.590 +  apply (induct n)
   6.591 +         apply safe
   6.592 +                         apply (case_tac [!] x rule: bin_exhaust)
   6.593 +                         apply simp_all
   6.594 +                      apply (case_tac [!] y rule: bin_exhaust)
   6.595 +                      apply simp_all
   6.596 +        apply (auto dest: not_B1_is_B0 intro: B1_ass_B0)
   6.597 +  done
   6.598 +
   6.599 +lemma bin_sc_FP [simp]: "bin_sc n bit.B0 Numeral.Pls = Numeral.Pls"
   6.600 +  by (induct n) auto
   6.601 +
   6.602 +lemma bin_sc_TM [simp]: "bin_sc n bit.B1 Numeral.Min = Numeral.Min"
   6.603 +  by (induct n) auto
   6.604 +  
   6.605 +lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP
   6.606 +
   6.607 +lemma bin_sc_minus:
   6.608 +  "0 < n ==> bin_sc (Suc (n - 1)) b w = bin_sc n b w"
   6.609 +  by auto
   6.610 +
   6.611 +lemmas bin_sc_Suc_minus = 
   6.612 +  trans [OF bin_sc_minus [symmetric] bin_sc.Suc, standard]
   6.613 +
   6.614 +lemmas bin_sc_Suc_pred [simp] = 
   6.615 +  bin_sc_Suc_minus [of "number_of bin", simplified nobm1, standard]
   6.616 +
   6.617 +(* interaction between bit-wise and arithmetic *)
   6.618 +(* good example of bin_induction *)
   6.619 +lemma bin_add_not: "x + int_not x = Numeral.Min"
   6.620 +  apply (induct x rule: bin_induct)
   6.621 +    apply clarsimp
   6.622 +   apply clarsimp
   6.623 +  apply (case_tac bit, auto)
   6.624 +  done
   6.625 +
   6.626 +(* truncating results of bit-wise operations *)
   6.627 +lemma bin_trunc_ao: 
   6.628 +  "!!x y. int_and (bintrunc n x) (bintrunc n y) = bintrunc n (int_and x y)" 
   6.629 +  "!!x y. int_or (bintrunc n x) (bintrunc n y) = bintrunc n (int_or x y)"
   6.630 +  apply (induct n)
   6.631 +      apply auto
   6.632 +      apply (case_tac [!] x rule: bin_exhaust)
   6.633 +      apply (case_tac [!] y rule: bin_exhaust)
   6.634 +      apply auto
   6.635 +  done
   6.636 +
   6.637 +lemma bin_trunc_xor: 
   6.638 +  "!!x y. bintrunc n (int_xor (bintrunc n x) (bintrunc n y)) = 
   6.639 +          bintrunc n (int_xor x y)"
   6.640 +  apply (induct n)
   6.641 +   apply auto
   6.642 +   apply (case_tac [!] x rule: bin_exhaust)
   6.643 +   apply (case_tac [!] y rule: bin_exhaust)
   6.644 +   apply auto
   6.645 +  done
   6.646 +
   6.647 +lemma bin_trunc_not: 
   6.648 +  "!!x. bintrunc n (int_not (bintrunc n x)) = bintrunc n (int_not x)"
   6.649 +  apply (induct n)
   6.650 +   apply auto
   6.651 +   apply (case_tac [!] x rule: bin_exhaust)
   6.652 +   apply auto
   6.653 +  done
   6.654 +
   6.655 +(* want theorems of the form of bin_trunc_xor *)
   6.656 +lemma bintr_bintr_i:
   6.657 +  "x = bintrunc n y ==> bintrunc n x = bintrunc n y"
   6.658 +  by auto
   6.659 +
   6.660 +lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i]
   6.661 +lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i]
   6.662 +
   6.663 +lemma nth_2p_bin: 
   6.664 +  "!!m. bin_nth (2 ^ n) m = (m = n)"
   6.665 +  apply (induct n)
   6.666 +   apply clarsimp
   6.667 +   apply safe
   6.668 +     apply (case_tac m) 
   6.669 +      apply (auto simp: trans [OF numeral_1_eq_1 [symmetric] number_of_eq])
   6.670 +   apply (case_tac m) 
   6.671 +    apply (auto simp: Bit_B0_2t [symmetric])
   6.672 +  done
   6.673 +
   6.674 +(* for use when simplifying with bin_nth_Bit *)
   6.675 +
   6.676 +lemma ex_eq_or:
   6.677 +  "(EX m. n = Suc m & (m = k | P m)) = (n = Suc k | (EX m. n = Suc m & P m))"
   6.678 +  by auto
   6.679 +
   6.680 +end
   6.681 +
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/HOL/Word/BitSyntax.thy	Mon Aug 20 04:34:31 2007 +0200
     7.3 @@ -0,0 +1,58 @@
     7.4 +header {* Syntactic class for bitwise operations *}
     7.5 +
     7.6 +theory BitSyntax
     7.7 +imports Main
     7.8 +begin
     7.9 +
    7.10 +axclass bit < type
    7.11 +
    7.12 +text {*
    7.13 +  We want the bitwise operations to bind slightly weaker
    7.14 +  than @{text "+"} and @{text "-"}, but @{text "~~"} to 
    7.15 +  bind slightly stronger than @{text "*"}.
    7.16 +*}
    7.17 +consts
    7.18 +  bitNOT  :: "'a::bit \<Rightarrow> 'a"       ("NOT _" [70] 71)
    7.19 +  bitAND  :: "'a::bit \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "AND" 64)
    7.20 +  bitOR   :: "'a::bit \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "OR"  59)
    7.21 +  bitXOR  :: "'a::bit \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "XOR" 59)
    7.22 +
    7.23 +text {*
    7.24 +  Testing and shifting operations.
    7.25 +*}
    7.26 +consts
    7.27 +  test_bit :: "'a::bit \<Rightarrow> nat \<Rightarrow> bool" (infixl "!!" 100)
    7.28 +  lsb      :: "'a::bit \<Rightarrow> bool"
    7.29 +  msb      :: "'a::bit \<Rightarrow> bool"
    7.30 +  set_bit  :: "'a::bit \<Rightarrow> nat \<Rightarrow> bool \<Rightarrow> 'a"
    7.31 +  set_bits :: "(nat \<Rightarrow> bool) \<Rightarrow> 'a::bit" (binder "BITS " 10)
    7.32 +  shiftl   :: "'a::bit \<Rightarrow> nat \<Rightarrow> 'a" (infixl "<<" 55)
    7.33 +  shiftr   :: "'a::bit \<Rightarrow> nat \<Rightarrow> 'a" (infixl ">>" 55)
    7.34 +
    7.35 +
    7.36 +subsection {* Bitwise operations on type @{typ bit} *}
    7.37 +
    7.38 +instance bit :: bit ..
    7.39 +
    7.40 +defs (overloaded)
    7.41 +  NOT_bit_def: "NOT x \<equiv> case x of bit.B0 \<Rightarrow> bit.B1 | bit.B1 \<Rightarrow> bit.B0"
    7.42 +  AND_bit_def: "x AND y \<equiv> case x of bit.B0 \<Rightarrow> bit.B0 | bit.B1 \<Rightarrow> y"
    7.43 +  OR_bit_def: "x OR y :: bit \<equiv> NOT (NOT x AND NOT y)"
    7.44 +  XOR_bit_def: "x XOR y :: bit \<equiv> (x AND NOT y) OR (NOT x AND y)"
    7.45 +
    7.46 +lemma bit_simps [simp]:
    7.47 +  "NOT bit.B0 = bit.B1"
    7.48 +  "NOT bit.B1 = bit.B0"
    7.49 +  "bit.B0 AND y = bit.B0"
    7.50 +  "bit.B1 AND y = y"
    7.51 +  "bit.B0 OR y = y"
    7.52 +  "bit.B1 OR y = bit.B1"
    7.53 +  "bit.B0 XOR y = y"
    7.54 +  "bit.B1 XOR y = NOT y"
    7.55 +by (simp_all add: NOT_bit_def AND_bit_def OR_bit_def
    7.56 +                  XOR_bit_def split: bit.split)
    7.57 +
    7.58 +lemma bit_NOT_NOT: "NOT (NOT (b::bit)) = b"
    7.59 +by (induct b) simp_all
    7.60 +
    7.61 +end
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/HOL/Word/Num_Lemmas.thy	Mon Aug 20 04:34:31 2007 +0200
     8.3 @@ -0,0 +1,625 @@
     8.4 +(* 
     8.5 +  ID:      $Id$
     8.6 +  Author:  Jeremy Dawson, NICTA
     8.7 +
     8.8 +  useful numerical lemmas 
     8.9 +*) 
    8.10 +
    8.11 +theory Num_Lemmas imports Parity begin
    8.12 +
    8.13 +lemma contentsI: "y = {x} ==> contents y = x" 
    8.14 +  unfolding contents_def by auto
    8.15 +
    8.16 +lemma prod_case_split: "prod_case = split"
    8.17 +  by (rule ext)+ auto
    8.18 + 
    8.19 +lemmas split_split = prod.split [unfolded prod_case_split] 
    8.20 +lemmas split_split_asm = prod.split_asm [unfolded prod_case_split]
    8.21 +lemmas "split.splits" = split_split split_split_asm 
    8.22 +
    8.23 +lemmas funpow_0 = funpow.simps(1)
    8.24 +lemmas funpow_Suc = funpow.simps(2)
    8.25 +
    8.26 +lemma nonemptyE: "S ~= {} ==> (!!x. x : S ==> R) ==> R"
    8.27 +  apply (erule contrapos_np)
    8.28 +  apply (rule equals0I)
    8.29 +  apply auto
    8.30 +  done
    8.31 +
    8.32 +lemma int_number_of: "number_of (y::int) = y" 
    8.33 +  by (simp add: number_of_eq)
    8.34 +
    8.35 +lemma gt_or_eq_0: "0 < y \<or> 0 = (y::nat)" by auto
    8.36 +
    8.37 +constdefs
    8.38 +  mod_alt :: "'a => 'a => 'a :: Divides.div"
    8.39 +  "mod_alt n m == n mod m" 
    8.40 +
    8.41 +  -- "alternative way of defining @{text bin_last}, @{text bin_rest}"
    8.42 +  bin_rl :: "int => int * bit" 
    8.43 +  "bin_rl w == SOME (r, l). w = r BIT l"
    8.44 +
    8.45 +declare iszero_0 [iff]
    8.46 +
    8.47 +lemmas xtr1 = xtrans(1)
    8.48 +lemmas xtr2 = xtrans(2)
    8.49 +lemmas xtr3 = xtrans(3)
    8.50 +lemmas xtr4 = xtrans(4)
    8.51 +lemmas xtr5 = xtrans(5)
    8.52 +lemmas xtr6 = xtrans(6)
    8.53 +lemmas xtr7 = xtrans(7)
    8.54 +lemmas xtr8 = xtrans(8)
    8.55 +
    8.56 +lemma Min_ne_Pls [iff]:  
    8.57 +  "Numeral.Min ~= Numeral.Pls"
    8.58 +  unfolding Min_def Pls_def by auto
    8.59 +
    8.60 +lemmas Pls_ne_Min [iff] = Min_ne_Pls [symmetric]
    8.61 +
    8.62 +lemmas PlsMin_defs [intro!] = 
    8.63 +  Pls_def Min_def Pls_def [symmetric] Min_def [symmetric]
    8.64 +
    8.65 +lemmas PlsMin_simps [simp] = PlsMin_defs [THEN Eq_TrueI]
    8.66 +
    8.67 +lemma number_of_False_cong: 
    8.68 +  "False ==> number_of x = number_of y" 
    8.69 +  by auto
    8.70 +
    8.71 +lemmas nat_simps = diff_add_inverse2 diff_add_inverse
    8.72 +lemmas nat_iffs = le_add1 le_add2
    8.73 +
    8.74 +lemma sum_imp_diff: "j = k + i ==> j - i = (k :: nat)"
    8.75 +  by (clarsimp simp add: nat_simps)
    8.76 +
    8.77 +lemma nobm1:
    8.78 +  "0 < (number_of w :: nat) ==> 
    8.79 +   number_of w - (1 :: nat) = number_of (Numeral.pred w)"
    8.80 +  apply (unfold nat_number_of_def One_nat_def nat_1 [symmetric] pred_def)
    8.81 +  apply (simp add: number_of_eq nat_diff_distrib [symmetric])
    8.82 +  done
    8.83 +
    8.84 +lemma of_int_power:
    8.85 +  "of_int (a ^ n) = (of_int a ^ n :: 'a :: {recpower, comm_ring_1})" 
    8.86 +  by (induct n) (auto simp add: power_Suc)
    8.87 +
    8.88 +lemma zless2: "0 < (2 :: int)" 
    8.89 +  by auto
    8.90 +
    8.91 +lemmas zless2p [simp] = zless2 [THEN zero_less_power]
    8.92 +lemmas zle2p [simp] = zless2p [THEN order_less_imp_le]
    8.93 +
    8.94 +lemmas pos_mod_sign2 = zless2 [THEN pos_mod_sign [where b = "2::int"]]
    8.95 +lemmas pos_mod_bound2 = zless2 [THEN pos_mod_bound [where b = "2::int"]]
    8.96 +
    8.97 +-- "the inverse(s) of @{text number_of}"
    8.98 +lemma nmod2: "n mod (2::int) = 0 | n mod 2 = 1"
    8.99 +  using pos_mod_sign2 [of n] pos_mod_bound2 [of n]
   8.100 +  unfolding mod_alt_def [symmetric] by auto
   8.101 +
   8.102 +lemma emep1:
   8.103 +  "even n ==> even d ==> 0 <= d ==> (n + 1) mod (d :: int) = (n mod d) + 1"
   8.104 +  apply (simp add: add_commute)
   8.105 +  apply (safe dest!: even_equiv_def [THEN iffD1])
   8.106 +  apply (subst pos_zmod_mult_2)
   8.107 +   apply arith
   8.108 +  apply (simp add: zmod_zmult_zmult1)
   8.109 + done
   8.110 +
   8.111 +lemmas eme1p = emep1 [simplified add_commute]
   8.112 +
   8.113 +lemma le_diff_eq': "(a \<le> c - b) = (b + a \<le> (c::int))"
   8.114 +  by (simp add: le_diff_eq add_commute)
   8.115 +
   8.116 +lemma less_diff_eq': "(a < c - b) = (b + a < (c::int))"
   8.117 +  by (simp add: less_diff_eq add_commute)
   8.118 +
   8.119 +lemma diff_le_eq': "(a - b \<le> c) = (a \<le> b + (c::int))"
   8.120 +  by (simp add: diff_le_eq add_commute)
   8.121 +
   8.122 +lemma diff_less_eq': "(a - b < c) = (a < b + (c::int))"
   8.123 +  by (simp add: diff_less_eq add_commute)
   8.124 +
   8.125 +
   8.126 +lemmas m1mod2k = zless2p [THEN zmod_minus1]
   8.127 +lemmas m1mod22k = mult_pos_pos [OF zless2 zless2p, THEN zmod_minus1]
   8.128 +lemmas p1mod22k' = zless2p [THEN order_less_imp_le, THEN pos_zmod_mult_2]
   8.129 +lemmas z1pmod2' = zero_le_one [THEN pos_zmod_mult_2, simplified]
   8.130 +lemmas z1pdiv2' = zero_le_one [THEN pos_zdiv_mult_2, simplified]
   8.131 +
   8.132 +lemma p1mod22k:
   8.133 +  "(2 * b + 1) mod (2 * 2 ^ n) = 2 * (b mod 2 ^ n) + (1::int)"
   8.134 +  by (simp add: p1mod22k' add_commute)
   8.135 +
   8.136 +lemma z1pmod2:
   8.137 +  "(2 * b + 1) mod 2 = (1::int)"
   8.138 +  by (simp add: z1pmod2' add_commute)
   8.139 +  
   8.140 +lemma z1pdiv2:
   8.141 +  "(2 * b + 1) div 2 = (b::int)"
   8.142 +  by (simp add: z1pdiv2' add_commute)
   8.143 +
   8.144 +lemmas zdiv_le_dividend = xtr3 [OF zdiv_1 [symmetric] zdiv_mono2,
   8.145 +  simplified int_one_le_iff_zero_less, simplified, standard]
   8.146 +  
   8.147 +(** ways in which type Bin resembles a datatype **)
   8.148 +
   8.149 +lemma BIT_eq: "u BIT b = v BIT c ==> u = v & b = c"
   8.150 +  apply (unfold Bit_def)
   8.151 +  apply (simp (no_asm_use) split: bit.split_asm)
   8.152 +     apply simp_all
   8.153 +   apply (drule_tac f=even in arg_cong, clarsimp)+
   8.154 +  done
   8.155 +     
   8.156 +lemmas BIT_eqE [elim!] = BIT_eq [THEN conjE, standard]
   8.157 +
   8.158 +lemma BIT_eq_iff [simp]: 
   8.159 +  "(u BIT b = v BIT c) = (u = v \<and> b = c)"
   8.160 +  by (rule iffI) auto
   8.161 +
   8.162 +lemmas BIT_eqI [intro!] = conjI [THEN BIT_eq_iff [THEN iffD2]]
   8.163 +
   8.164 +lemma less_Bits: 
   8.165 +  "(v BIT b < w BIT c) = (v < w | v <= w & b = bit.B0 & c = bit.B1)"
   8.166 +  unfolding Bit_def by (auto split: bit.split)
   8.167 +
   8.168 +lemma le_Bits: 
   8.169 +  "(v BIT b <= w BIT c) = (v < w | v <= w & (b ~= bit.B1 | c ~= bit.B0))" 
   8.170 +  unfolding Bit_def by (auto split: bit.split)
   8.171 +
   8.172 +lemma neB1E [elim!]:
   8.173 +  assumes ne: "y \<noteq> bit.B1"
   8.174 +  assumes y: "y = bit.B0 \<Longrightarrow> P"
   8.175 +  shows "P"
   8.176 +  apply (rule y)
   8.177 +  apply (cases y rule: bit.exhaust, simp)
   8.178 +  apply (simp add: ne)
   8.179 +  done
   8.180 +
   8.181 +lemma bin_ex_rl: "EX w b. w BIT b = bin"
   8.182 +  apply (unfold Bit_def)
   8.183 +  apply (cases "even bin")
   8.184 +   apply (clarsimp simp: even_equiv_def)
   8.185 +   apply (auto simp: odd_equiv_def split: bit.split)
   8.186 +  done
   8.187 +
   8.188 +lemma bin_exhaust:
   8.189 +  assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q"
   8.190 +  shows "Q"
   8.191 +  apply (insert bin_ex_rl [of bin])  
   8.192 +  apply (erule exE)+
   8.193 +  apply (rule Q)
   8.194 +  apply force
   8.195 +  done
   8.196 +
   8.197 +lemma bin_rl_char: "(bin_rl w = (r, l)) = (r BIT l = w)"
   8.198 +  apply (unfold bin_rl_def)
   8.199 +  apply safe
   8.200 +   apply (cases w rule: bin_exhaust)
   8.201 +   apply auto
   8.202 +  done
   8.203 +
   8.204 +lemmas bin_rl_simps [THEN bin_rl_char [THEN iffD2], standard, simp] =
   8.205 +  Pls_0_eq Min_1_eq refl 
   8.206 +
   8.207 +lemma bin_abs_lem:
   8.208 +  "bin = (w BIT b) ==> ~ bin = Numeral.Min --> ~ bin = Numeral.Pls -->
   8.209 +    nat (abs w) < nat (abs bin)"
   8.210 +  apply (clarsimp simp add: bin_rl_char)
   8.211 +  apply (unfold Pls_def Min_def Bit_def)
   8.212 +  apply (cases b)
   8.213 +   apply (clarsimp, arith)
   8.214 +  apply (clarsimp, arith)
   8.215 +  done
   8.216 +
   8.217 +lemma bin_induct:
   8.218 +  assumes PPls: "P Numeral.Pls"
   8.219 +    and PMin: "P Numeral.Min"
   8.220 +    and PBit: "!!bin bit. P bin ==> P (bin BIT bit)"
   8.221 +  shows "P bin"
   8.222 +  apply (rule_tac P=P and a=bin and f1="nat o abs" 
   8.223 +                  in wf_measure [THEN wf_induct])
   8.224 +  apply (simp add: measure_def inv_image_def)
   8.225 +  apply (case_tac x rule: bin_exhaust)
   8.226 +  apply (frule bin_abs_lem)
   8.227 +  apply (auto simp add : PPls PMin PBit)
   8.228 +  done
   8.229 +
   8.230 +lemma no_no [simp]: "number_of (number_of i) = i"
   8.231 +  unfolding number_of_eq by simp
   8.232 +
   8.233 +lemma Bit_B0:
   8.234 +  "k BIT bit.B0 = k + k"
   8.235 +   by (unfold Bit_def) simp
   8.236 +
   8.237 +lemma Bit_B1:
   8.238 +  "k BIT bit.B1 = k + k + 1"
   8.239 +   by (unfold Bit_def) simp
   8.240 +  
   8.241 +lemma Bit_B0_2t: "k BIT bit.B0 = 2 * k"
   8.242 +  by (rule trans, rule Bit_B0) simp
   8.243 +
   8.244 +lemma Bit_B1_2t: "k BIT bit.B1 = 2 * k + 1"
   8.245 +  by (rule trans, rule Bit_B1) simp
   8.246 +
   8.247 +lemma B_mod_2': 
   8.248 +  "X = 2 ==> (w BIT bit.B1) mod X = 1 & (w BIT bit.B0) mod X = 0"
   8.249 +  apply (simp (no_asm) only: Bit_B0 Bit_B1)
   8.250 +  apply (simp add: z1pmod2)
   8.251 +  done
   8.252 +    
   8.253 +lemmas B1_mod_2 [simp] = B_mod_2' [OF refl, THEN conjunct1, standard]
   8.254 +lemmas B0_mod_2 [simp] = B_mod_2' [OF refl, THEN conjunct2, standard]
   8.255 +
   8.256 +lemma axxbyy:
   8.257 +  "a + m + m = b + n + n ==> (a = 0 | a = 1) ==> (b = 0 | b = 1) ==>  
   8.258 +   a = b & m = (n :: int)"
   8.259 +  apply auto
   8.260 +   apply (drule_tac f="%n. n mod 2" in arg_cong)
   8.261 +   apply (clarsimp simp: z1pmod2)
   8.262 +  apply (drule_tac f="%n. n mod 2" in arg_cong)
   8.263 +  apply (clarsimp simp: z1pmod2)
   8.264 +  done
   8.265 +
   8.266 +lemma axxmod2:
   8.267 +  "(1 + x + x) mod 2 = (1 :: int) & (0 + x + x) mod 2 = (0 :: int)" 
   8.268 +  by simp (rule z1pmod2)
   8.269 +
   8.270 +lemma axxdiv2:
   8.271 +  "(1 + x + x) div 2 = (x :: int) & (0 + x + x) div 2 = (x :: int)" 
   8.272 +  by simp (rule z1pdiv2)
   8.273 +
   8.274 +lemmas iszero_minus = trans [THEN trans,
   8.275 +  OF iszero_def neg_equal_0_iff_equal iszero_def [symmetric], standard]
   8.276 +
   8.277 +lemmas zadd_diff_inverse = trans [OF diff_add_cancel [symmetric] add_commute,
   8.278 +  standard]
   8.279 +
   8.280 +lemmas add_diff_cancel2 = add_commute [THEN diff_eq_eq [THEN iffD2], standard]
   8.281 +
   8.282 +lemma zmod_uminus: "- ((a :: int) mod b) mod b = -a mod b"
   8.283 +  by (simp add : zmod_zminus1_eq_if)
   8.284 +
   8.285 +lemma zmod_zsub_distrib: "((a::int) - b) mod c = (a mod c - b mod c) mod c"
   8.286 +  apply (unfold diff_int_def)
   8.287 +  apply (rule trans [OF _ zmod_zadd1_eq [symmetric]])
   8.288 +  apply (simp add: zmod_uminus zmod_zadd1_eq [symmetric])
   8.289 +  done
   8.290 +
   8.291 +lemma zmod_zsub_right_eq: "((a::int) - b) mod c = (a - b mod c) mod c"
   8.292 +  apply (unfold diff_int_def)
   8.293 +  apply (rule trans [OF _ zmod_zadd_right_eq [symmetric]])
   8.294 +  apply (simp add : zmod_uminus zmod_zadd_right_eq [symmetric])
   8.295 +  done
   8.296 +
   8.297 +lemmas zmod_zsub_left_eq = 
   8.298 +  zmod_zadd_left_eq [where b = "- ?b", simplified diff_int_def [symmetric]]
   8.299 +  
   8.300 +lemma zmod_zsub_self [simp]: 
   8.301 +  "((b :: int) - a) mod a = b mod a"
   8.302 +  by (simp add: zmod_zsub_right_eq)
   8.303 +
   8.304 +lemma zmod_zmult1_eq_rev:
   8.305 +  "b * a mod c = b mod c * a mod (c::int)"
   8.306 +  apply (simp add: mult_commute)
   8.307 +  apply (subst zmod_zmult1_eq)
   8.308 +  apply simp
   8.309 +  done
   8.310 +
   8.311 +lemmas rdmods [symmetric] = zmod_uminus [symmetric]
   8.312 +  zmod_zsub_left_eq zmod_zsub_right_eq zmod_zadd_left_eq
   8.313 +  zmod_zadd_right_eq zmod_zmult1_eq zmod_zmult1_eq_rev
   8.314 +
   8.315 +lemma mod_plus_right:
   8.316 +  "((a + x) mod m = (b + x) mod m) = (a mod m = b mod (m :: nat))"
   8.317 +  apply (induct x)
   8.318 +   apply (simp_all add: mod_Suc)
   8.319 +  apply arith
   8.320 +  done
   8.321 +
   8.322 +lemma nat_minus_mod: "(n - n mod m) mod m = (0 :: nat)"
   8.323 +  by (induct n) (simp_all add : mod_Suc)
   8.324 +
   8.325 +lemmas nat_minus_mod_plus_right = trans [OF nat_minus_mod mod_0 [symmetric],
   8.326 +  THEN mod_plus_right [THEN iffD2], standard, simplified]
   8.327 +
   8.328 +lemmas push_mods' = zmod_zadd1_eq [standard]
   8.329 +  zmod_zmult_distrib [standard] zmod_zsub_distrib [standard]
   8.330 +  zmod_uminus [symmetric, standard]
   8.331 +
   8.332 +lemmas push_mods = push_mods' [THEN eq_reflection, standard]
   8.333 +lemmas pull_mods = push_mods [symmetric] rdmods [THEN eq_reflection, standard]
   8.334 +lemmas mod_simps = 
   8.335 +  zmod_zmult_self1 [THEN eq_reflection] zmod_zmult_self2 [THEN eq_reflection]
   8.336 +  mod_mod_trivial [THEN eq_reflection]
   8.337 +
   8.338 +lemma nat_mod_eq:
   8.339 +  "!!b. b < n ==> a mod n = b mod n ==> a mod n = (b :: nat)" 
   8.340 +  by (induct a) auto
   8.341 +
   8.342 +lemmas nat_mod_eq' = refl [THEN [2] nat_mod_eq]
   8.343 +
   8.344 +lemma nat_mod_lem: 
   8.345 +  "(0 :: nat) < n ==> b < n = (b mod n = b)"
   8.346 +  apply safe
   8.347 +   apply (erule nat_mod_eq')
   8.348 +  apply (erule subst)
   8.349 +  apply (erule mod_less_divisor)
   8.350 +  done
   8.351 +
   8.352 +lemma mod_nat_add: 
   8.353 +  "(x :: nat) < z ==> y < z ==> 
   8.354 +   (x + y) mod z = (if x + y < z then x + y else x + y - z)"
   8.355 +  apply (rule nat_mod_eq)
   8.356 +   apply auto
   8.357 +  apply (rule trans)
   8.358 +   apply (rule le_mod_geq)
   8.359 +   apply simp
   8.360 +  apply (rule nat_mod_eq')
   8.361 +  apply arith
   8.362 +  done
   8.363 +
   8.364 +lemma mod_nat_sub: 
   8.365 +  "(x :: nat) < z ==> (x - y) mod z = x - y"
   8.366 +  by (rule nat_mod_eq') arith
   8.367 +
   8.368 +lemma int_mod_lem: 
   8.369 +  "(0 :: int) < n ==> (0 <= b & b < n) = (b mod n = b)"
   8.370 +  apply safe
   8.371 +    apply (erule (1) mod_pos_pos_trivial)
   8.372 +   apply (erule_tac [!] subst)
   8.373 +   apply auto
   8.374 +  done
   8.375 +
   8.376 +lemma int_mod_eq:
   8.377 +  "(0 :: int) <= b ==> b < n ==> a mod n = b mod n ==> a mod n = b"
   8.378 +  by clarsimp (rule mod_pos_pos_trivial)
   8.379 +
   8.380 +lemmas int_mod_eq' = refl [THEN [3] int_mod_eq]
   8.381 +
   8.382 +lemma int_mod_le: "0 <= a ==> 0 < (n :: int) ==> a mod n <= a"
   8.383 +  apply (cases "a < n")
   8.384 +   apply (auto dest: mod_pos_pos_trivial pos_mod_bound [where a=a])
   8.385 +  done
   8.386 +
   8.387 +lemmas int_mod_le' = int_mod_le [where a = "?b - ?n", simplified]
   8.388 +
   8.389 +lemma int_mod_ge: "a < n ==> 0 < (n :: int) ==> a <= a mod n"
   8.390 +  apply (cases "0 <= a")
   8.391 +   apply (drule (1) mod_pos_pos_trivial)
   8.392 +   apply simp
   8.393 +  apply (rule order_trans [OF _ pos_mod_sign])
   8.394 +   apply simp
   8.395 +  apply assumption
   8.396 +  done
   8.397 +
   8.398 +lemmas int_mod_ge' = int_mod_ge [where a = "?b + ?n", simplified]
   8.399 +
   8.400 +lemma mod_add_if_z:
   8.401 +  "(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> 
   8.402 +   (x + y) mod z = (if x + y < z then x + y else x + y - z)"
   8.403 +  by (auto intro: int_mod_eq)
   8.404 +
   8.405 +lemma mod_sub_if_z:
   8.406 +  "(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> 
   8.407 +   (x - y) mod z = (if y <= x then x - y else x - y + z)"
   8.408 +  by (auto intro: int_mod_eq)
   8.409 +
   8.410 +lemmas zmde = zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2], symmetric]
   8.411 +lemmas mcl = mult_cancel_left [THEN iffD1, THEN make_pos_rule]
   8.412 +
   8.413 +(* already have this for naturals, div_mult_self1/2, but not for ints *)
   8.414 +lemma zdiv_mult_self: "m ~= (0 :: int) ==> (a + m * n) div m = a div m + n"
   8.415 +  apply (rule mcl)
   8.416 +   prefer 2
   8.417 +   apply (erule asm_rl)
   8.418 +  apply (simp add: zmde ring_distribs)
   8.419 +  apply (simp add: push_mods)
   8.420 +  done
   8.421 +
   8.422 +(** Rep_Integ **)
   8.423 +lemma eqne: "equiv A r ==> X : A // r ==> X ~= {}"
   8.424 +  unfolding equiv_def refl_def quotient_def Image_def by auto
   8.425 +
   8.426 +lemmas Rep_Integ_ne = Integ.Rep_Integ 
   8.427 +  [THEN equiv_intrel [THEN eqne, simplified Integ_def [symmetric]], standard]
   8.428 +
   8.429 +lemmas riq = Integ.Rep_Integ [simplified Integ_def]
   8.430 +lemmas intrel_refl = refl [THEN equiv_intrel_iff [THEN iffD1], standard]
   8.431 +lemmas Rep_Integ_equiv = quotient_eq_iff
   8.432 +  [OF equiv_intrel riq riq, simplified Integ.Rep_Integ_inject, standard]
   8.433 +lemmas Rep_Integ_same = 
   8.434 +  Rep_Integ_equiv [THEN intrel_refl [THEN rev_iffD2], standard]
   8.435 +
   8.436 +lemma RI_int: "(a, 0) : Rep_Integ (int a)"
   8.437 +  unfolding int_def by auto
   8.438 +
   8.439 +lemmas RI_intrel [simp] = UNIV_I [THEN quotientI,
   8.440 +  THEN Integ.Abs_Integ_inverse [simplified Integ_def], standard]
   8.441 +
   8.442 +lemma RI_minus: "(a, b) : Rep_Integ x ==> (b, a) : Rep_Integ (- x)"
   8.443 +  apply (rule_tac z=x in eq_Abs_Integ)
   8.444 +  apply (clarsimp simp: minus)
   8.445 +  done
   8.446 +
   8.447 +lemma RI_add: 
   8.448 +  "(a, b) : Rep_Integ x ==> (c, d) : Rep_Integ y ==> 
   8.449 +   (a + c, b + d) : Rep_Integ (x + y)"
   8.450 +  apply (rule_tac z=x in eq_Abs_Integ)
   8.451 +  apply (rule_tac z=y in eq_Abs_Integ) 
   8.452 +  apply (clarsimp simp: add)
   8.453 +  done
   8.454 +
   8.455 +lemma mem_same: "a : S ==> a = b ==> b : S"
   8.456 +  by fast
   8.457 +
   8.458 +(* two alternative proofs of this *)
   8.459 +lemma RI_eq_diff': "(a, b) : Rep_Integ (int a - int b)"
   8.460 +  apply (unfold diff_def)
   8.461 +  apply (rule mem_same)
   8.462 +   apply (rule RI_minus RI_add RI_int)+
   8.463 +  apply simp
   8.464 +  done
   8.465 +
   8.466 +lemma RI_eq_diff: "((a, b) : Rep_Integ x) = (int a - int b = x)"
   8.467 +  apply safe
   8.468 +   apply (rule Rep_Integ_same)
   8.469 +    prefer 2
   8.470 +    apply (erule asm_rl)
   8.471 +   apply (rule RI_eq_diff')+
   8.472 +  done
   8.473 +
   8.474 +lemma mod_power_lem:
   8.475 +  "a > 1 ==> a ^ n mod a ^ m = (if m <= n then 0 else (a :: int) ^ n)"
   8.476 +  apply clarsimp
   8.477 +  apply safe
   8.478 +   apply (simp add: zdvd_iff_zmod_eq_0 [symmetric])
   8.479 +   apply (drule le_iff_add [THEN iffD1])
   8.480 +   apply (force simp: zpower_zadd_distrib)
   8.481 +  apply (rule mod_pos_pos_trivial)
   8.482 +   apply (simp add: zero_le_power)
   8.483 +  apply (rule power_strict_increasing)
   8.484 +   apply auto
   8.485 +  done
   8.486 +
   8.487 +lemma min_pm [simp]: "min a b + (a - b) = (a :: nat)"
   8.488 +  by arith
   8.489 +  
   8.490 +lemmas min_pm1 [simp] = trans [OF add_commute min_pm]
   8.491 +
   8.492 +lemma rev_min_pm [simp]: "min b a + (a - b) = (a::nat)"
   8.493 +  by simp
   8.494 +
   8.495 +lemmas rev_min_pm1 [simp] = trans [OF add_commute rev_min_pm]
   8.496 +
   8.497 +lemma pl_pl_rels: 
   8.498 +  "a + b = c + d ==> 
   8.499 +   a >= c & b <= d | a <= c & b >= (d :: nat)"
   8.500 +  apply (cut_tac n=a and m=c in nat_le_linear)
   8.501 +  apply (safe dest!: le_iff_add [THEN iffD1])
   8.502 +         apply arith+
   8.503 +  done
   8.504 +
   8.505 +lemmas pl_pl_rels' = add_commute [THEN [2] trans, THEN pl_pl_rels]
   8.506 +
   8.507 +lemma minus_eq: "(m - k = m) = (k = 0 | m = (0 :: nat))"
   8.508 +  by arith
   8.509 +
   8.510 +lemma pl_pl_mm: "(a :: nat) + b = c + d ==> a - c = d - b"
   8.511 +  by arith
   8.512 +
   8.513 +lemmas pl_pl_mm' = add_commute [THEN [2] trans, THEN pl_pl_mm]
   8.514 + 
   8.515 +lemma min_minus [simp] : "min m (m - k) = (m - k :: nat)"
   8.516 +  by arith
   8.517 +  
   8.518 +lemmas min_minus' [simp] = trans [OF min_max.inf_commute min_minus]
   8.519 +
   8.520 +lemma nat_no_eq_iff: 
   8.521 +  "(number_of b :: int) >= 0 ==> (number_of c :: int) >= 0 ==> 
   8.522 +   (number_of b = (number_of c :: nat)) = (b = c)"
   8.523 +  apply (unfold nat_number_of_def)
   8.524 +  apply safe
   8.525 +  apply (drule (2) eq_nat_nat_iff [THEN iffD1])
   8.526 +  apply (simp add: number_of_eq)
   8.527 +  done
   8.528 +
   8.529 +lemmas dme = box_equals [OF div_mod_equality add_0_right add_0_right]
   8.530 +lemmas dtle = xtr3 [OF dme [symmetric] le_add1]
   8.531 +lemmas th2 = order_trans [OF order_refl [THEN [2] mult_le_mono] dtle]
   8.532 +
   8.533 +lemma td_gal: 
   8.534 +  "0 < c ==> (a >= b * c) = (a div c >= (b :: nat))"
   8.535 +  apply safe
   8.536 +   apply (erule (1) xtr4 [OF div_le_mono div_mult_self_is_m])
   8.537 +  apply (erule th2)
   8.538 +  done
   8.539 +  
   8.540 +lemmas td_gal_lt = td_gal [simplified le_def, simplified]
   8.541 +
   8.542 +lemma div_mult_le: "(a :: nat) div b * b <= a"
   8.543 +  apply (cases b)
   8.544 +   prefer 2
   8.545 +   apply (rule order_refl [THEN th2])
   8.546 +  apply auto
   8.547 +  done
   8.548 +
   8.549 +lemmas sdl = split_div_lemma [THEN iffD1, symmetric]
   8.550 +
   8.551 +lemma given_quot: "f > (0 :: nat) ==> (f * l + (f - 1)) div f = l"
   8.552 +  by (rule sdl, assumption) (simp (no_asm))
   8.553 +
   8.554 +lemma given_quot_alt: "f > (0 :: nat) ==> (l * f + f - Suc 0) div f = l"
   8.555 +  apply (frule given_quot)
   8.556 +  apply (rule trans)
   8.557 +   prefer 2
   8.558 +   apply (erule asm_rl)
   8.559 +  apply (rule_tac f="%n. n div f" in arg_cong)
   8.560 +  apply (simp add : mult_ac)
   8.561 +  done
   8.562 +    
   8.563 +lemma diff_mod_le: "(a::nat) < d ==> b dvd d ==> a - a mod b <= d - b"
   8.564 +  apply (unfold dvd_def)
   8.565 +  apply clarify
   8.566 +  apply (case_tac k)
   8.567 +   apply clarsimp
   8.568 +  apply clarify
   8.569 +  apply (cases "b > 0")
   8.570 +   apply (drule mult_commute [THEN xtr1])
   8.571 +   apply (frule (1) td_gal_lt [THEN iffD1])
   8.572 +   apply (clarsimp simp: le_simps)
   8.573 +   apply (rule mult_div_cancel [THEN [2] xtr4])
   8.574 +   apply (rule mult_mono)
   8.575 +      apply auto
   8.576 +  done
   8.577 +
   8.578 +lemma less_le_mult':
   8.579 +  "w * c < b * c ==> 0 \<le> c ==> (w + 1) * c \<le> b * (c::int)"
   8.580 +  apply (rule mult_right_mono)
   8.581 +   apply (rule zless_imp_add1_zle)
   8.582 +   apply (erule (1) mult_right_less_imp_less)
   8.583 +  apply assumption
   8.584 +  done
   8.585 +
   8.586 +lemmas less_le_mult = less_le_mult' [simplified left_distrib, simplified]
   8.587 +
   8.588 +lemmas less_le_mult_minus = iffD2 [OF le_diff_eq less_le_mult, 
   8.589 +  simplified left_diff_distrib, standard]
   8.590 +
   8.591 +lemma lrlem':
   8.592 +  assumes d: "(i::nat) \<le> j \<or> m < j'"
   8.593 +  assumes R1: "i * k \<le> j * k \<Longrightarrow> R"
   8.594 +  assumes R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R"
   8.595 +  shows "R" using d
   8.596 +  apply safe
   8.597 +   apply (rule R1, erule mult_le_mono1)
   8.598 +  apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]])
   8.599 +  done
   8.600 +
   8.601 +lemma lrlem: "(0::nat) < sc ==>
   8.602 +    (sc - n + (n + lb * n) <= m * n) = (sc + lb * n <= m * n)"
   8.603 +  apply safe
   8.604 +   apply arith
   8.605 +  apply (case_tac "sc >= n")
   8.606 +   apply arith
   8.607 +  apply (insert linorder_le_less_linear [of m lb])
   8.608 +  apply (erule_tac k=n and k'=n in lrlem')
   8.609 +   apply arith
   8.610 +  apply simp
   8.611 +  done
   8.612 +
   8.613 +lemma gen_minus: "0 < n ==> f n = f (Suc (n - 1))"
   8.614 +  by auto
   8.615 +
   8.616 +lemma mpl_lem: "j <= (i :: nat) ==> k < j ==> i - j + k < i"
   8.617 +  apply (induct i, clarsimp)
   8.618 +  apply (cases j, clarsimp)
   8.619 +  apply arith
   8.620 +  done
   8.621 +
   8.622 +lemma nonneg_mod_div:
   8.623 +  "0 <= a ==> 0 <= b ==> 0 <= (a mod b :: int) & 0 <= a div b"
   8.624 +  apply (cases "b = 0", clarsimp)
   8.625 +  apply (auto intro: pos_imp_zdiv_nonneg_iff [THEN iffD2])
   8.626 +  done
   8.627 +
   8.628 +end
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/HOL/Word/ROOT.ML	Mon Aug 20 04:34:31 2007 +0200
     9.3 @@ -0,0 +1,2 @@
     9.4 +use_thy "WordExamples";
     9.5 +
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/HOL/Word/Size.thy	Mon Aug 20 04:34:31 2007 +0200
    10.3 @@ -0,0 +1,54 @@
    10.4 +(* 
    10.5 +    ID:         $Id$
    10.6 +    Author:     John Matthews, Galois Connections, Inc., copyright 2006
    10.7 +
    10.8 +    A typeclass for parameterizing types by size.
    10.9 +    Used primarily to parameterize machine word sizes. 
   10.10 +*)
   10.11 +theory Size
   10.12 +imports Numeral_Type
   10.13 +begin
   10.14 +
   10.15 +text {*
   10.16 +  The aim of this is to allow any type as index type, but to provide a
   10.17 +  default instantiation for numeral types. This independence requires
   10.18 +  some duplication with the definitions in Numeral\_Type.
   10.19 +*}
   10.20 +axclass len0 < type
   10.21 +
   10.22 +consts
   10.23 +  len_of :: "('a :: len0 itself) => nat"
   10.24 +
   10.25 +text {* 
   10.26 +  Some theorems are only true on words with length greater 0.
   10.27 +*}
   10.28 +axclass len < len0
   10.29 +  len_gt_0 [iff]: "0 < len_of TYPE ('a :: len0)"
   10.30 +
   10.31 +instance pls  :: len0 ..
   10.32 +instance num1 :: len0 ..
   10.33 +instance bit0 :: (len0) len0 ..
   10.34 +instance bit1 :: (len0) len0 ..
   10.35 +
   10.36 +defs (overloaded)
   10.37 +  len_pls:  "len_of (x::pls itself) == 0"
   10.38 +  len_num1: "len_of (x::num1 itself) == 1"
   10.39 +  len_bit0: "len_of (x::'a::len0 bit0 itself) == 2 * len_of TYPE ('a)"
   10.40 +  len_bit1: "len_of (x::'a::len0 bit1 itself) == 2 * len_of TYPE ('a) + 1"
   10.41 +
   10.42 +lemmas len_of_numeral_defs [simp] = len_pls len_num1 len_bit0 len_bit1
   10.43 +
   10.44 +instance num1 :: len by (intro_classes) simp
   10.45 +instance bit0 :: (len) len by (intro_classes) simp
   10.46 +instance bit1 :: (len0) len by (intro_classes) simp
   10.47 +
   10.48 +-- "Examples:"
   10.49 +lemma "len_of TYPE(17) = 17" by simp
   10.50 +lemma "len_of TYPE(0) = 0" by simp
   10.51 +
   10.52 +-- "not simplified:"
   10.53 +lemma "len_of TYPE('a::len0) = x"
   10.54 +  oops
   10.55 +   
   10.56 +end
   10.57 +
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/HOL/Word/TdThs.thy	Mon Aug 20 04:34:31 2007 +0200
    11.3 @@ -0,0 +1,220 @@
    11.4 +(* 
    11.5 +    ID:         $Id$
    11.6 +    Author:     Jeremy Dawson and Gerwin Klein, NICTA
    11.7 +
    11.8 +  consequences of type definition theorems, 
    11.9 +  and of extended type definition theorems
   11.10 +*)
   11.11 +theory TdThs imports Main begin
   11.12 +
   11.13 +lemma
   11.14 +  tdD1: "type_definition Rep Abs A \<Longrightarrow> \<forall>x. Rep x \<in> A" and
   11.15 +  tdD2: "type_definition Rep Abs A \<Longrightarrow> \<forall>x. Abs (Rep x) = x" and
   11.16 +  tdD3: "type_definition Rep Abs A \<Longrightarrow> \<forall>y. y \<in> A \<longrightarrow> Rep (Abs y) = y"
   11.17 +  by (auto simp: type_definition_def)
   11.18 +
   11.19 +lemma td_nat_int: 
   11.20 +  "type_definition int nat (Collect (op <= 0))"
   11.21 +  unfolding type_definition_def by auto
   11.22 +
   11.23 +context type_definition
   11.24 +begin
   11.25 +
   11.26 +lemmas Rep' [iff] = Rep [simplified]  (* if A is given as Collect .. *)
   11.27 +
   11.28 +declare Rep_inverse [simp] Rep_inject [simp]
   11.29 +
   11.30 +lemma Abs_eqD: "Abs x = Abs y ==> x \<in> A ==> y \<in> A ==> x = y"
   11.31 +  by (simp add: Abs_inject)
   11.32 +   
   11.33 +lemma Abs_inverse': 
   11.34 +  "r : A ==> Abs r = a ==> Rep a = r"
   11.35 +  by (safe elim!: Abs_inverse)
   11.36 +
   11.37 +lemma Rep_comp_inverse: 
   11.38 +  "Rep o f = g ==> Abs o g = f"
   11.39 +  using Rep_inverse by (auto intro: ext)
   11.40 +
   11.41 +lemma Rep_eqD [elim!]: "Rep x = Rep y ==> x = y"
   11.42 +  by simp
   11.43 +
   11.44 +lemma Rep_inverse': "Rep a = r ==> Abs r = a"
   11.45 +  by (safe intro!: Rep_inverse)
   11.46 +
   11.47 +lemma comp_Abs_inverse: 
   11.48 +  "f o Abs = g ==> g o Rep = f"
   11.49 +  using Rep_inverse by (auto intro: ext) 
   11.50 +
   11.51 +lemma set_Rep: 
   11.52 +  "A = range Rep"
   11.53 +proof (rule set_ext)
   11.54 +  fix x
   11.55 +  show "(x \<in> A) = (x \<in> range Rep)"
   11.56 +    by (auto dest: Abs_inverse [of x, symmetric])
   11.57 +qed  
   11.58 +
   11.59 +lemma set_Rep_Abs: "A = range (Rep o Abs)"
   11.60 +proof (rule set_ext)
   11.61 +  fix x
   11.62 +  show "(x \<in> A) = (x \<in> range (Rep o Abs))"
   11.63 +    by (auto dest: Abs_inverse [of x, symmetric])
   11.64 +qed  
   11.65 +
   11.66 +lemma Abs_inj_on: "inj_on Abs A"
   11.67 +  unfolding inj_on_def 
   11.68 +  by (auto dest: Abs_inject [THEN iffD1])
   11.69 +
   11.70 +lemma image: "Abs ` A = UNIV"
   11.71 +  by (auto intro!: image_eqI)
   11.72 +
   11.73 +lemmas td_thm = type_definition_axioms
   11.74 +
   11.75 +lemma fns1: 
   11.76 +  "Rep o fa = fr o Rep | fa o Abs = Abs o fr ==> Abs o fr o Rep = fa"
   11.77 +  by (auto dest: Rep_comp_inverse elim: comp_Abs_inverse simp: o_assoc)
   11.78 +
   11.79 +lemmas fns1a = disjI1 [THEN fns1]
   11.80 +lemmas fns1b = disjI2 [THEN fns1]
   11.81 +
   11.82 +lemma fns4:
   11.83 +  "Rep o fa o Abs = fr ==> 
   11.84 +   Rep o fa = fr o Rep & fa o Abs = Abs o fr"
   11.85 +  by (auto intro!: ext)
   11.86 +
   11.87 +end
   11.88 +
   11.89 +interpretation nat_int: type_definition [int nat "Collect (op <= 0)"]
   11.90 +  by (rule td_nat_int)
   11.91 +
   11.92 +-- "resetting to the default nat induct and cases rules"
   11.93 +declare Nat.induct [case_names 0 Suc, induct type]
   11.94 +declare Nat.exhaust [case_names 0 Suc, cases type]
   11.95 +
   11.96 +
   11.97 +section "Extended of type definition predicate"
   11.98 +
   11.99 +lemma td_conds:
  11.100 +  "norm o norm = norm ==> (fr o norm = norm o fr) = 
  11.101 +    (norm o fr o norm = fr o norm & norm o fr o norm = norm o fr)"
  11.102 +  apply safe
  11.103 +    apply (simp_all add: o_assoc [symmetric])
  11.104 +   apply (simp_all add: o_assoc)
  11.105 +  done
  11.106 +
  11.107 +lemma fn_comm_power:
  11.108 +  "fa o tr = tr o fr ==> fa ^ n o tr = tr o fr ^ n" 
  11.109 +  apply (rule ext) 
  11.110 +  apply (induct n)
  11.111 +   apply (auto dest: fun_cong)
  11.112 +  done
  11.113 +
  11.114 +lemmas fn_comm_power' =
  11.115 +  ext [THEN fn_comm_power, THEN fun_cong, unfolded o_def, standard]
  11.116 +
  11.117 +
  11.118 +locale td_ext = type_definition +
  11.119 +  fixes norm
  11.120 +  assumes eq_norm: "\<And>x. Rep (Abs x) = norm x"
  11.121 +begin
  11.122 +
  11.123 +lemma Abs_norm [simp]: 
  11.124 +  "Abs (norm x) = Abs x"
  11.125 +  using eq_norm [of x] by (auto elim: Rep_inverse')
  11.126 +
  11.127 +lemma td_th:
  11.128 +  "g o Abs = f ==> f (Rep x) = g x"
  11.129 +  by (drule comp_Abs_inverse [symmetric]) simp
  11.130 +
  11.131 +lemma eq_norm': "Rep o Abs = norm"
  11.132 +  by (auto simp: eq_norm intro!: ext)
  11.133 +
  11.134 +lemma norm_Rep [simp]: "norm (Rep x) = Rep x"
  11.135 +  by (auto simp: eq_norm' intro: td_th)
  11.136 +
  11.137 +lemmas td = td_thm
  11.138 +
  11.139 +lemma set_iff_norm: "w : A <-> w = norm w"
  11.140 +  by (auto simp: set_Rep_Abs eq_norm' eq_norm [symmetric])
  11.141 +
  11.142 +lemma inverse_norm: 
  11.143 +  "(Abs n = w) = (Rep w = norm n)"
  11.144 +  apply (rule iffI)
  11.145 +   apply (clarsimp simp add: eq_norm)
  11.146 +  apply (simp add: eq_norm' [symmetric])
  11.147 +  done
  11.148 +
  11.149 +lemma norm_eq_iff: 
  11.150 +  "(norm x = norm y) = (Abs x = Abs y)"
  11.151 +  by (simp add: eq_norm' [symmetric])
  11.152 +
  11.153 +lemma norm_comps: 
  11.154 +  "Abs o norm = Abs" 
  11.155 +  "norm o Rep = Rep" 
  11.156 +  "norm o norm = norm"
  11.157 +  by (auto simp: eq_norm' [symmetric] o_def)
  11.158 +
  11.159 +lemmas norm_norm [simp] = norm_comps
  11.160 +
  11.161 +lemma fns5: 
  11.162 +  "Rep o fa o Abs = fr ==> 
  11.163 +  fr o norm = fr & norm o fr = fr"
  11.164 +  by (fold eq_norm') (auto intro!: ext)
  11.165 +
  11.166 +(* following give conditions for converses to td_fns1
  11.167 +  the condition (norm o fr o norm = fr o norm) says that 
  11.168 +  fr takes normalised arguments to normalised results,
  11.169 +  (norm o fr o norm = norm o fr) says that fr 
  11.170 +  takes norm-equivalent arguments to norm-equivalent results,
  11.171 +  (fr o norm = fr) says that fr 
  11.172 +  takes norm-equivalent arguments to the same result, and 
  11.173 +  (norm o fr = fr) says that fr takes any argument to a normalised result 
  11.174 +  *)
  11.175 +lemma fns2: 
  11.176 +  "Abs o fr o Rep = fa ==> 
  11.177 +   (norm o fr o norm = fr o norm) = (Rep o fa = fr o Rep)"
  11.178 +  apply (fold eq_norm')
  11.179 +  apply safe
  11.180 +   prefer 2
  11.181 +   apply (simp add: o_assoc)
  11.182 +  apply (rule ext)
  11.183 +  apply (drule_tac x="Rep x" in fun_cong)
  11.184 +  apply auto
  11.185 +  done
  11.186 +
  11.187 +lemma fns3: 
  11.188 +  "Abs o fr o Rep = fa ==> 
  11.189 +   (norm o fr o norm = norm o fr) = (fa o Abs = Abs o fr)"
  11.190 +  apply (fold eq_norm')
  11.191 +  apply safe
  11.192 +   prefer 2
  11.193 +   apply (simp add: o_assoc [symmetric])
  11.194 +  apply (rule ext)
  11.195 +  apply (drule fun_cong)
  11.196 +  apply simp
  11.197 +  done
  11.198 +
  11.199 +lemma fns: 
  11.200 +  "fr o norm = norm o fr ==> 
  11.201 +    (fa o Abs = Abs o fr) = (Rep o fa = fr o Rep)"
  11.202 +  apply safe
  11.203 +   apply (frule fns1b)
  11.204 +   prefer 2 
  11.205 +   apply (frule fns1a) 
  11.206 +   apply (rule fns3 [THEN iffD1])
  11.207 +     prefer 3
  11.208 +     apply (rule fns2 [THEN iffD1])
  11.209 +       apply (simp_all add: o_assoc [symmetric])
  11.210 +   apply (simp_all add: o_assoc)
  11.211 +  done
  11.212 +
  11.213 +lemma range_norm:
  11.214 +  "range (Rep o Abs) = A"
  11.215 +  by (simp add: set_Rep_Abs)
  11.216 +
  11.217 +end
  11.218 +
  11.219 +lemmas td_ext_def' =
  11.220 +  td_ext_def [unfolded type_definition_def td_ext_axioms_def]
  11.221 +
  11.222 +end
  11.223 +
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/src/HOL/Word/WordArith.thy	Mon Aug 20 04:34:31 2007 +0200
    12.3 @@ -0,0 +1,1254 @@
    12.4 +(* 
    12.5 +    ID:         $Id$
    12.6 +    Author:     Jeremy Dawson and Gerwin Klein, NICTA
    12.7 +
    12.8 +  contains arithmetic theorems for word, instantiations to
    12.9 +  arithmetic type classes and tactics for reducing word arithmetic
   12.10 +  to linear arithmetic on int or nat
   12.11 +*) 
   12.12 +
   12.13 +theory WordArith imports WordDefinition begin
   12.14 +
   12.15 +
   12.16 +lemma word_less_alt: "(a < b) = (uint a < uint b)"
   12.17 +  unfolding word_less_def word_le_def
   12.18 +  by (auto simp del: word_uint.Rep_inject 
   12.19 +           simp: word_uint.Rep_inject [symmetric])
   12.20 +
   12.21 +lemma signed_linorder: "linorder word_sle word_sless"
   12.22 +  apply unfold_locales
   12.23 +      apply (unfold word_sle_def word_sless_def) 
   12.24 +  by auto 
   12.25 +
   12.26 +interpretation signed: linorder ["word_sle" "word_sless"] 
   12.27 +  by (rule signed_linorder)
   12.28 +
   12.29 +lemmas word_arith_wis [THEN meta_eq_to_obj_eq] = 
   12.30 +  word_add_def word_mult_def word_minus_def 
   12.31 +  word_succ_def word_pred_def word_0_wi word_1_wi
   12.32 +
   12.33 +lemma udvdI: 
   12.34 +  "0 \<le> n ==> uint b = n * uint a ==> a udvd b"
   12.35 +  by (auto simp: udvd_def)
   12.36 +
   12.37 +lemmas word_div_no [simp] = 
   12.38 +  word_div_def [of "number_of ?a" "number_of ?b"]
   12.39 +
   12.40 +lemmas word_mod_no [simp] = 
   12.41 +  word_mod_def [of "number_of ?a" "number_of ?b"]
   12.42 +
   12.43 +lemmas word_less_no [simp] = 
   12.44 +  word_less_def [of "number_of ?a" "number_of ?b"]
   12.45 +
   12.46 +lemmas word_le_no [simp] = 
   12.47 +  word_le_def [of "number_of ?a" "number_of ?b"]
   12.48 +
   12.49 +lemmas word_sless_no [simp] = 
   12.50 +  word_sless_def [of "number_of ?a" "number_of ?b"]
   12.51 +
   12.52 +lemmas word_sle_no [simp] = 
   12.53 +  word_sle_def [of "number_of ?a" "number_of ?b"]
   12.54 +
   12.55 +(* following two are available in class number_ring, 
   12.56 +  but convenient to have them here here;
   12.57 +  note - the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1
   12.58 +  are in the default simpset, so to use the automatic simplifications for
   12.59 +  (eg) sint (number_of bin) on sint 1, must do
   12.60 +  (simp add: word_1_no del: numeral_1_eq_1) 
   12.61 +  *)
   12.62 +lemmas word_0_wi_Pls = word_0_wi [folded Pls_def]
   12.63 +lemmas word_0_no = word_0_wi_Pls [folded word_no_wi]
   12.64 +
   12.65 +lemma int_one_bin: "(1 :: int) == (Numeral.Pls BIT bit.B1)"
   12.66 +  unfolding Pls_def Bit_def by auto
   12.67 +
   12.68 +lemma word_1_no: 
   12.69 +  "(1 :: 'a :: len0 word) == number_of (Numeral.Pls BIT bit.B1)"
   12.70 +  unfolding word_1_wi word_number_of_def int_one_bin by auto
   12.71 +
   12.72 +lemma word_m1_wi: "-1 == word_of_int -1" 
   12.73 +  by (rule word_number_of_alt)
   12.74 +
   12.75 +lemma word_m1_wi_Min: "-1 = word_of_int Numeral.Min"
   12.76 +  by (simp add: word_m1_wi number_of_eq)
   12.77 +
   12.78 +lemma word_0_bl: "of_bl [] = 0" 
   12.79 +  unfolding word_0_wi of_bl_def by (simp add : Pls_def)
   12.80 +
   12.81 +lemma word_1_bl: "of_bl [True] = 1" 
   12.82 +  unfolding word_1_wi of_bl_def
   12.83 +  by (simp add : bl_to_bin_def Bit_def Pls_def)
   12.84 +
   12.85 +lemma uint_0 [simp] : "(uint 0 = 0)" 
   12.86 +  unfolding word_0_wi
   12.87 +  by (simp add: word_ubin.eq_norm Pls_def [symmetric])
   12.88 +
   12.89 +lemma of_bl_0 [simp] : "of_bl (replicate n False) = 0"
   12.90 +  by (simp add : word_0_wi of_bl_def bl_to_bin_rep_False Pls_def)
   12.91 +
   12.92 +lemma to_bl_0: 
   12.93 +  "to_bl (0::'a::len0 word) = replicate (len_of TYPE('a)) False"
   12.94 +  unfolding uint_bl
   12.95 +  by (simp add : word_size bin_to_bl_Pls Pls_def [symmetric])
   12.96 +
   12.97 +lemma uint_0_iff: "(uint x = 0) = (x = 0)"
   12.98 +  by (auto intro!: word_uint.Rep_eqD)
   12.99 +
  12.100 +lemma unat_0_iff: "(unat x = 0) = (x = 0)"
  12.101 +  unfolding unat_def by (auto simp add : nat_eq_iff uint_0_iff)
  12.102 +
  12.103 +lemma unat_0 [simp]: "unat 0 = 0"
  12.104 +  unfolding unat_def by auto
  12.105 +
  12.106 +lemma size_0_same': "size w = 0 ==> w = (v :: 'a :: len0 word)"
  12.107 +  apply (unfold word_size)
  12.108 +  apply (rule box_equals)
  12.109 +    defer
  12.110 +    apply (rule word_uint.Rep_inverse)+
  12.111 +  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
  12.112 +  apply simp
  12.113 +  done
  12.114 +
  12.115 +lemmas size_0_same = size_0_same' [folded word_size]
  12.116 +
  12.117 +lemmas unat_eq_0 = unat_0_iff
  12.118 +lemmas unat_eq_zero = unat_0_iff
  12.119 +
  12.120 +lemma unat_gt_0: "(0 < unat x) = (x ~= 0)"
  12.121 +  by (simp add : unat_0_iff [symmetric])
  12.122 +
  12.123 +lemma ucast_0 [simp] : "ucast 0 = 0"
  12.124 +  unfolding ucast_def
  12.125 +  by simp (simp add: word_0_wi)
  12.126 +
  12.127 +lemma sint_0 [simp] : "sint 0 = 0"
  12.128 +  unfolding sint_uint
  12.129 +  by (simp add: Pls_def [symmetric])
  12.130 +
  12.131 +lemma scast_0 [simp] : "scast 0 = 0"
  12.132 +  apply (unfold scast_def)
  12.133 +  apply simp
  12.134 +  apply (simp add: word_0_wi)
  12.135 +  done
  12.136 +
  12.137 +lemma sint_n1 [simp] : "sint -1 = -1"
  12.138 +  apply (unfold word_m1_wi_Min)
  12.139 +  apply (simp add: word_sbin.eq_norm)
  12.140 +  apply (unfold Min_def number_of_eq)
  12.141 +  apply simp
  12.142 +  done
  12.143 +
  12.144 +lemma scast_n1 [simp] : "scast -1 = -1"
  12.145 +  apply (unfold scast_def sint_n1)
  12.146 +  apply (unfold word_number_of_alt)
  12.147 +  apply (rule refl)
  12.148 +  done
  12.149 +
  12.150 +lemma uint_1 [simp] : "uint (1 :: 'a :: len word) = 1"
  12.151 +  unfolding word_1_wi
  12.152 +  by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
  12.153 +
  12.154 +lemma unat_1 [simp] : "unat (1 :: 'a :: len word) = 1"
  12.155 +  by (unfold unat_def uint_1) auto
  12.156 +
  12.157 +lemma ucast_1 [simp] : "ucast (1 :: 'a :: len word) = 1"
  12.158 +  unfolding ucast_def word_1_wi
  12.159 +  by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
  12.160 +
  12.161 +(* abstraction preserves the operations
  12.162 +  (the definitions tell this for bins in range uint) *)
  12.163 +
  12.164 +lemmas arths = 
  12.165 +  bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1],
  12.166 +                folded word_ubin.eq_norm, standard]
  12.167 +
  12.168 +lemma wi_homs: 
  12.169 +  shows
  12.170 +  wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and
  12.171 +  wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and
  12.172 +  wi_hom_neg: "- word_of_int a = word_of_int (- a)" and
  12.173 +  wi_hom_succ: "word_succ (word_of_int a) = word_of_int (Numeral.succ a)" and
  12.174 +  wi_hom_pred: "word_pred (word_of_int a) = word_of_int (Numeral.pred a)"
  12.175 +  by (auto simp: word_arith_wis arths)
  12.176 +
  12.177 +lemmas wi_hom_syms = wi_homs [symmetric]
  12.178 +
  12.179 +lemma word_sub_def: "a - b == a + - (b :: 'a :: len0 word)"
  12.180 +  unfolding word_sub_wi diff_def
  12.181 +  by (simp only : word_uint.Rep_inverse wi_hom_syms)
  12.182 +    
  12.183 +lemmas word_diff_minus = word_sub_def [THEN meta_eq_to_obj_eq, standard]
  12.184 +
  12.185 +lemma word_of_int_sub_hom:
  12.186 +  "(word_of_int a) - word_of_int b = word_of_int (a - b)"
  12.187 +  unfolding word_sub_def diff_def by (simp only : wi_homs)
  12.188 +
  12.189 +lemmas new_word_of_int_homs = 
  12.190 +  word_of_int_sub_hom wi_homs word_0_wi word_1_wi 
  12.191 +
  12.192 +lemmas new_word_of_int_hom_syms = new_word_of_int_homs [symmetric, standard]
  12.193 +
  12.194 +lemmas word_of_int_hom_syms =
  12.195 +  new_word_of_int_hom_syms [unfolded succ_def pred_def]
  12.196 +
  12.197 +lemmas word_of_int_homs =
  12.198 +  new_word_of_int_homs [unfolded succ_def pred_def]
  12.199 +
  12.200 +lemmas word_of_int_add_hom = word_of_int_homs (2)
  12.201 +lemmas word_of_int_mult_hom = word_of_int_homs (3)
  12.202 +lemmas word_of_int_minus_hom = word_of_int_homs (4)
  12.203 +lemmas word_of_int_succ_hom = word_of_int_homs (5)
  12.204 +lemmas word_of_int_pred_hom = word_of_int_homs (6)
  12.205 +lemmas word_of_int_0_hom = word_of_int_homs (7)
  12.206 +lemmas word_of_int_1_hom = word_of_int_homs (8)
  12.207 +
  12.208 +(* now, to get the weaker results analogous to word_div/mod_def *)
  12.209 +
  12.210 +lemmas word_arith_alts = 
  12.211 +  word_sub_wi [unfolded succ_def pred_def, THEN meta_eq_to_obj_eq, standard]
  12.212 +  word_arith_wis [unfolded succ_def pred_def, standard]
  12.213 +
  12.214 +lemmas word_sub_alt = word_arith_alts (1)
  12.215 +lemmas word_add_alt = word_arith_alts (2)
  12.216 +lemmas word_mult_alt = word_arith_alts (3)
  12.217 +lemmas word_minus_alt = word_arith_alts (4)
  12.218 +lemmas word_succ_alt = word_arith_alts (5)
  12.219 +lemmas word_pred_alt = word_arith_alts (6)
  12.220 +lemmas word_0_alt = word_arith_alts (7)
  12.221 +lemmas word_1_alt = word_arith_alts (8)
  12.222 +
  12.223 +section  "Transferring goals from words to ints"
  12.224 +
  12.225 +lemma word_ths:  
  12.226 +  shows
  12.227 +  word_succ_p1:   "word_succ a = a + 1" and
  12.228 +  word_pred_m1:   "word_pred a = a - 1" and
  12.229 +  word_pred_succ: "word_pred (word_succ a) = a" and
  12.230 +  word_succ_pred: "word_succ (word_pred a) = a" and
  12.231 +  word_mult_succ: "word_succ a * b = b + a * b"
  12.232 +  by (rule word_uint.Abs_cases [of b],
  12.233 +      rule word_uint.Abs_cases [of a],
  12.234 +      simp add: pred_def succ_def add_commute mult_commute 
  12.235 +                ring_distribs new_word_of_int_homs)+
  12.236 +
  12.237 +lemmas uint_cong = arg_cong [where f = uint]
  12.238 +
  12.239 +lemmas uint_word_ariths = 
  12.240 +  word_arith_alts [THEN trans [OF uint_cong int_word_uint], standard]
  12.241 +
  12.242 +lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p]
  12.243 +
  12.244 +(* similar expressions for sint (arith operations) *)
  12.245 +lemmas sint_word_ariths = uint_word_arith_bintrs
  12.246 +  [THEN uint_sint [symmetric, THEN trans],
  12.247 +  unfolded uint_sint bintr_arith1s bintr_ariths 
  12.248 +    len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep, standard]
  12.249 +
  12.250 +lemmas uint_div_alt = word_div_def
  12.251 +  [THEN meta_eq_to_obj_eq [THEN trans [OF uint_cong int_word_uint]], standard]
  12.252 +lemmas uint_mod_alt = word_mod_def
  12.253 +  [THEN meta_eq_to_obj_eq [THEN trans [OF uint_cong int_word_uint]], standard]
  12.254 +
  12.255 +lemma word_pred_0_n1: "word_pred 0 = word_of_int -1"
  12.256 +  unfolding word_pred_def number_of_eq
  12.257 +  by (simp add : pred_def word_no_wi)
  12.258 +
  12.259 +lemma word_pred_0_Min: "word_pred 0 = word_of_int Numeral.Min"
  12.260 +  by (simp add: word_pred_0_n1 number_of_eq)
  12.261 +
  12.262 +lemma word_m1_Min: "- 1 = word_of_int Numeral.Min"
  12.263 +  unfolding Min_def by (simp only: word_of_int_hom_syms)
  12.264 +
  12.265 +lemma succ_pred_no [simp]:
  12.266 +  "word_succ (number_of bin) = number_of (Numeral.succ bin) & 
  12.267 +    word_pred (number_of bin) = number_of (Numeral.pred bin)"
  12.268 +  unfolding word_number_of_def by (simp add : new_word_of_int_homs)
  12.269 +
  12.270 +lemma word_sp_01 [simp] : 
  12.271 +  "word_succ -1 = 0 & word_succ 0 = 1 & word_pred 0 = -1 & word_pred 1 = 0"
  12.272 +  by (unfold word_0_no word_1_no) auto
  12.273 +
  12.274 +(* alternative approach to lifting arithmetic equalities *)
  12.275 +lemma word_of_int_Ex:
  12.276 +  "\<exists>y. x = word_of_int y"
  12.277 +  by (rule_tac x="uint x" in exI) simp
  12.278 +
  12.279 +lemma word_arith_eqs:
  12.280 +  fixes a :: "'a::len0 word"
  12.281 +  fixes b :: "'a::len0 word"
  12.282 +  shows
  12.283 +  word_add_0: "0 + a = a" and
  12.284 +  word_add_0_right: "a + 0 = a" and
  12.285 +  word_mult_1: "1 * a = a" and
  12.286 +  word_mult_1_right: "a * 1 = a" and
  12.287 +  word_add_commute: "a + b = b + a" and
  12.288 +  word_add_assoc: "a + b + c = a + (b + c)" and
  12.289 +  word_add_left_commute: "a + (b + c) = b + (a + c)" and
  12.290 +  word_mult_commute: "a * b = b * a" and
  12.291 +  word_mult_assoc: "a * b * c = a * (b * c)" and
  12.292 +  word_mult_left_commute: "a * (b * c) = b * (a * c)" and
  12.293 +  word_left_distrib: "(a + b) * c = a * c + b * c" and
  12.294 +  word_right_distrib: "a * (b + c) = a * b + a * c" and
  12.295 +  word_left_minus: "- a + a = 0" and
  12.296 +  word_diff_0_right: "a - 0 = a" and
  12.297 +  word_diff_self: "a - a = 0"
  12.298 +  using word_of_int_Ex [of a] 
  12.299 +        word_of_int_Ex [of b] 
  12.300 +        word_of_int_Ex [of c]
  12.301 +  by (auto simp: word_of_int_hom_syms [symmetric]
  12.302 +                 zadd_0_right add_commute add_assoc add_left_commute
  12.303 +                 mult_commute mult_assoc mult_left_commute
  12.304 +                 plus_times.left_distrib plus_times.right_distrib)
  12.305 +  
  12.306 +lemmas word_add_ac = word_add_commute word_add_assoc word_add_left_commute
  12.307 +lemmas word_mult_ac = word_mult_commute word_mult_assoc word_mult_left_commute
  12.308 +  
  12.309 +lemmas word_plus_ac0 = word_add_0 word_add_0_right word_add_ac
  12.310 +lemmas word_times_ac1 = word_mult_1 word_mult_1_right word_mult_ac
  12.311 +
  12.312 +
  12.313 +section "Order on fixed-length words"
  12.314 +
  12.315 +lemma word_order_trans: "x <= y ==> y <= z ==> x <= (z :: 'a :: len0 word)"
  12.316 +  unfolding word_le_def by auto
  12.317 +
  12.318 +lemma word_order_refl: "z <= (z :: 'a :: len0 word)"
  12.319 +  unfolding word_le_def by auto
  12.320 +
  12.321 +lemma word_order_antisym: "x <= y ==> y <= x ==> x = (y :: 'a :: len0 word)"
  12.322 +  unfolding word_le_def by (auto intro!: word_uint.Rep_eqD)
  12.323 +
  12.324 +lemma word_order_linear:
  12.325 +  "y <= x | x <= (y :: 'a :: len0 word)"
  12.326 +  unfolding word_le_def by auto
  12.327 +
  12.328 +lemma word_zero_le [simp] :
  12.329 +  "0 <= (y :: 'a :: len0 word)"
  12.330 +  unfolding word_le_def by auto
  12.331 +  
  12.332 +instance word :: (len0) semigroup_add
  12.333 +  by intro_classes (simp add: word_add_assoc)
  12.334 +
  12.335 +instance word :: (len0) linorder
  12.336 +  by intro_classes (auto simp: word_less_def word_le_def)
  12.337 +
  12.338 +instance word :: (len0) ring
  12.339 +  by intro_classes
  12.340 +     (auto simp: word_arith_eqs word_diff_minus 
  12.341 +                 word_diff_self [unfolded word_diff_minus])
  12.342 +
  12.343 +lemma word_m1_ge [simp] : "word_pred 0 >= y"
  12.344 +  unfolding word_le_def
  12.345 +  by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
  12.346 +
  12.347 +lemmas word_n1_ge [simp]  = word_m1_ge [simplified word_sp_01]
  12.348 +
  12.349 +lemmas word_not_simps [simp] = 
  12.350 +  word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
  12.351 +
  12.352 +lemma word_gt_0: "0 < y = (0 ~= (y :: 'a :: len0 word))"
  12.353 +  unfolding word_less_def by auto
  12.354 +
  12.355 +lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of ?y"]
  12.356 +
  12.357 +lemma word_sless_alt: "(a <s b) == (sint a < sint b)"
  12.358 +  unfolding word_sle_def word_sless_def
  12.359 +  by (auto simp add : less_eq_less.less_le)
  12.360 +
  12.361 +lemma word_le_nat_alt: "(a <= b) = (unat a <= unat b)"
  12.362 +  unfolding unat_def word_le_def
  12.363 +  by (rule nat_le_eq_zle [symmetric]) simp
  12.364 +
  12.365 +lemma word_less_nat_alt: "(a < b) = (unat a < unat b)"
  12.366 +  unfolding unat_def word_less_alt
  12.367 +  by (rule nat_less_eq_zless [symmetric]) simp
  12.368 +  
  12.369 +lemma wi_less: 
  12.370 +  "(word_of_int n < (word_of_int m :: 'a :: len0 word)) = 
  12.371 +    (n mod 2 ^ len_of TYPE('a) < m mod 2 ^ len_of TYPE('a))"
  12.372 +  unfolding word_less_alt by (simp add: word_uint.eq_norm)
  12.373 +
  12.374 +lemma wi_le: 
  12.375 +  "(word_of_int n <= (word_of_int m :: 'a :: len0 word)) = 
  12.376 +    (n mod 2 ^ len_of TYPE('a) <= m mod 2 ^ len_of TYPE('a))"
  12.377 +  unfolding word_le_def by (simp add: word_uint.eq_norm)
  12.378 +
  12.379 +lemma udvd_nat_alt: "a udvd b = (EX n>=0. unat b = n * unat a)"
  12.380 +  apply (unfold udvd_def)
  12.381 +  apply safe
  12.382 +   apply (simp add: unat_def nat_mult_distrib)
  12.383 +  apply (simp add: uint_nat int_mult)
  12.384 +  apply (rule exI)
  12.385 +  apply safe
  12.386 +   prefer 2
  12.387 +   apply (erule notE)
  12.388 +   apply (rule refl)
  12.389 +  apply force
  12.390 +  done
  12.391 +
  12.392 +lemma udvd_iff_dvd: "x udvd y <-> unat x dvd unat y"
  12.393 +  unfolding dvd_def udvd_nat_alt by force
  12.394 +
  12.395 +lemmas unat_mono = word_less_nat_alt [THEN iffD1, standard]
  12.396 +
  12.397 +lemma word_zero_neq_one: "0 < len_of TYPE ('a :: len0) ==> (0 :: 'a word) ~= 1";
  12.398 +  unfolding word_arith_wis
  12.399 +  by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc)
  12.400 +
  12.401 +lemmas lenw1_zero_neq_one = len_gt_0 [THEN word_zero_neq_one]
  12.402 +
  12.403 +lemma no_no [simp] : "number_of (number_of b) = number_of b"
  12.404 +  by (simp add: number_of_eq)
  12.405 +
  12.406 +lemma unat_minus_one: "x ~= 0 ==> unat (x - 1) = unat x - 1"
  12.407 +  apply (unfold unat_def)
  12.408 +  apply (simp only: int_word_uint word_arith_alts rdmods)
  12.409 +  apply (subgoal_tac "uint x >= 1")
  12.410 +   prefer 2
  12.411 +   apply (drule contrapos_nn)
  12.412 +    apply (erule word_uint.Rep_inverse' [symmetric])
  12.413 +   apply (insert uint_ge_0 [of x])[1]
  12.414 +   apply arith
  12.415 +  apply (rule box_equals)
  12.416 +    apply (rule nat_diff_distrib)
  12.417 +     prefer 2
  12.418 +     apply assumption
  12.419 +    apply simp
  12.420 +   apply (subst mod_pos_pos_trivial)
  12.421 +     apply arith
  12.422 +    apply (insert uint_lt2p [of x])[1]
  12.423 +    apply arith
  12.424 +   apply (rule refl)
  12.425 +  apply simp
  12.426 +  done
  12.427 +    
  12.428 +lemma measure_unat: "p ~= 0 ==> unat (p - 1) < unat p"
  12.429 +  by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric])
  12.430 +  
  12.431 +lemmas uint_add_ge0 [simp] =
  12.432 +  add_nonneg_nonneg [OF uint_ge_0 uint_ge_0, standard]
  12.433 +lemmas uint_mult_ge0 [simp] =
  12.434 +  mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0, standard]
  12.435 +
  12.436 +lemma uint_sub_lt2p [simp]: 
  12.437 +  "uint (x :: 'a :: len0 word) - uint (y :: 'b :: len0 word) < 
  12.438 +    2 ^ len_of TYPE('a)"
  12.439 +  using uint_ge_0 [of y] uint_lt2p [of x] by arith
  12.440 +
  12.441 +
  12.442 +section "Conditions for the addition (etc) of two words to overflow"
  12.443 +
  12.444 +lemma uint_add_lem: 
  12.445 +  "(uint x + uint y < 2 ^ len_of TYPE('a)) = 
  12.446 +    (uint (x + y :: 'a :: len0 word) = uint x + uint y)"
  12.447 +  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
  12.448 +
  12.449 +lemma uint_mult_lem: 
  12.450 +  "(uint x * uint y < 2 ^ len_of TYPE('a)) = 
  12.451 +    (uint (x * y :: 'a :: len0 word) = uint x * uint y)"
  12.452 +  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
  12.453 +
  12.454 +lemma uint_sub_lem: 
  12.455 +  "(uint x >= uint y) = (uint (x - y) = uint x - uint y)"
  12.456 +  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
  12.457 +
  12.458 +lemma uint_add_le: "uint (x + y) <= uint x + uint y"
  12.459 +  unfolding uint_word_ariths by (auto simp: mod_add_if_z)
  12.460 +
  12.461 +lemma uint_sub_ge: "uint (x - y) >= uint x - uint y"
  12.462 +  unfolding uint_word_ariths by (auto simp: mod_sub_if_z)
  12.463 +
  12.464 +lemmas uint_sub_if' =
  12.465 +  trans [OF uint_word_ariths(1) mod_sub_if_z, simplified, standard]
  12.466 +lemmas uint_plus_if' =
  12.467 +  trans [OF uint_word_ariths(2) mod_add_if_z, simplified, standard]
  12.468 +
  12.469 +
  12.470 +section {* Definition of uint\_arith *}
  12.471 +
  12.472 +lemma word_of_int_inverse:
  12.473 +  "word_of_int r = a ==> 0 <= r ==> r < 2 ^ len_of TYPE('a) ==> 
  12.474 +   uint (a::'a::len0 word) = r"
  12.475 +  apply (erule word_uint.Abs_inverse' [rotated])
  12.476 +  apply (simp add: uints_num)
  12.477 +  done
  12.478 +
  12.479 +lemma uint_split:
  12.480 +  fixes x::"'a::len0 word"
  12.481 +  shows "P (uint x) = 
  12.482 +         (ALL i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) --> P i)"
  12.483 +  apply (fold word_int_case_def)
  12.484 +  apply (auto dest!: word_of_int_inverse simp: int_word_uint int_mod_eq'
  12.485 +              split: word_int_split)
  12.486 +  done
  12.487 +
  12.488 +lemma uint_split_asm:
  12.489 +  fixes x::"'a::len0 word"
  12.490 +  shows "P (uint x) = 
  12.491 +         (~(EX i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) & ~ P i))"
  12.492 +  by (auto dest!: word_of_int_inverse 
  12.493 +           simp: int_word_uint int_mod_eq'
  12.494 +           split: uint_split)
  12.495 +
  12.496 +lemmas uint_splits = uint_split uint_split_asm
  12.497 +
  12.498 +lemmas uint_arith_simps = 
  12.499 +  word_le_def word_less_alt
  12.500 +  word_uint.Rep_inject [symmetric] 
  12.501 +  uint_sub_if' uint_plus_if'
  12.502 +
  12.503 +(* use this to stop, eg, 2 ^ len_of TYPE (32) being simplified *)
  12.504 +lemma power_False_cong: "False ==> a ^ b = c ^ d" 
  12.505 +  by auto
  12.506 +
  12.507 +(* uint_arith_tac: reduce to arithmetic on int, try to solve by arith *)
  12.508 +ML {*
  12.509 +fun uint_arith_ss_of ss = 
  12.510 +  ss addsimps @{thms uint_arith_simps}
  12.511 +     delsimps @{thms word_uint.Rep_inject}
  12.512 +     addsplits @{thms split_if_asm} 
  12.513 +     addcongs @{thms power_False_cong}
  12.514 +
  12.515 +fun uint_arith_tacs ctxt = 
  12.516 +  let fun arith_tac' n t = arith_tac ctxt n t handle COOPER => Seq.empty  
  12.517 +  in 
  12.518 +    [ CLASET' clarify_tac 1,
  12.519 +      SIMPSET' (full_simp_tac o uint_arith_ss_of) 1,
  12.520 +      ALLGOALS (full_simp_tac (HOL_ss addsplits @{thms uint_splits} 
  12.521 +                                      addcongs @{thms power_False_cong})),
  12.522 +      rewrite_goals_tac @{thms word_size}, 
  12.523 +      ALLGOALS  (fn n => REPEAT (resolve_tac [allI, impI] n) THEN      
  12.524 +                         REPEAT (etac conjE n) THEN
  12.525 +                         REPEAT (dtac @{thm word_of_int_inverse} n 
  12.526 +                                 THEN atac n 
  12.527 +                                 THEN atac n)),
  12.528 +      TRYALL arith_tac' ]
  12.529 +  end
  12.530 +
  12.531 +fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt))
  12.532 +*}
  12.533 +
  12.534 +method_setup uint_arith = 
  12.535 +  "Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD (uint_arith_tac ctxt 1))" 
  12.536 +  "solving word arithmetic via integers and arith"
  12.537 +
  12.538 +
  12.539 +section "More on overflows and monotonicity"
  12.540 +
  12.541 +lemma no_plus_overflow_uint_size: 
  12.542 +  "((x :: 'a :: len0 word) <= x + y) = (uint x + uint y < 2 ^ size x)"
  12.543 +  unfolding word_size by uint_arith
  12.544 +
  12.545 +lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size]
  12.546 +
  12.547 +lemma no_ulen_sub: "((x :: 'a :: len0 word) >= x - y) = (uint y <= uint x)"
  12.548 +  by uint_arith
  12.549 +
  12.550 +lemma no_olen_add':
  12.551 +  fixes x :: "'a::len0 word"
  12.552 +  shows "(x \<le> y + x) = (uint y + uint x < 2 ^ len_of TYPE('a))"
  12.553 +  by (simp add: word_add_ac add_ac no_olen_add)
  12.554 +
  12.555 +lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric], standard]
  12.556 +
  12.557 +lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem, standard]
  12.558 +lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1, standard]
  12.559 +lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem, standard]
  12.560 +lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def]
  12.561 +lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def]
  12.562 +lemmas word_sub_le = word_sub_le_iff [THEN iffD2, standard]
  12.563 +
  12.564 +lemma word_less_sub1: 
  12.565 +  "(x :: 'a :: len word) ~= 0 ==> (1 < x) = (0 < x - 1)"
  12.566 +  by uint_arith
  12.567 +
  12.568 +lemma word_le_sub1: 
  12.569 +  "(x :: 'a :: len word) ~= 0 ==> (1 <= x) = (0 <= x - 1)"
  12.570 +  by uint_arith
  12.571 +
  12.572 +lemma sub_wrap_lt: 
  12.573 +  "((x :: 'a :: len0 word) < x - z) = (x < z)"
  12.574 +  by uint_arith
  12.575 +
  12.576 +lemma sub_wrap: 
  12.577 +  "((x :: 'a :: len0 word) <= x - z) = (z = 0 | x < z)"
  12.578 +  by uint_arith
  12.579 +
  12.580 +lemma plus_minus_not_NULL_ab: 
  12.581 +  "(x :: 'a :: len0 word) <= ab - c ==> c <= ab ==> c ~= 0 ==> x + c ~= 0"
  12.582 +  by uint_arith
  12.583 +
  12.584 +lemma plus_minus_no_overflow_ab: 
  12.585 +  "(x :: 'a :: len0 word) <= ab - c ==> c <= ab ==> x <= x + c" 
  12.586 +  by uint_arith
  12.587 +
  12.588 +lemma le_minus': 
  12.589 +  "(a :: 'a :: len0 word) + c <= b ==> a <= a + c ==> c <= b - a"
  12.590 +  by uint_arith
  12.591 +
  12.592 +lemma le_plus': 
  12.593 +  "(a :: 'a :: len0 word) <= b ==> c <= b - a ==> a + c <= b"
  12.594 +  by uint_arith
  12.595 +
  12.596 +lemmas le_plus = le_plus' [rotated]
  12.597 +
  12.598 +lemmas le_minus = leD [THEN thin_rl, THEN le_minus', standard]
  12.599 +
  12.600 +lemma word_plus_mono_right: 
  12.601 +  "(y :: 'a :: len0 word) <= z ==> x <= x + z ==> x + y <= x + z"
  12.602 +  by uint_arith
  12.603 +
  12.604 +lemma word_less_minus_cancel: 
  12.605 +  "y - x < z - x ==> x <= z ==> (y :: 'a :: len0 word) < z"
  12.606 +  by uint_arith
  12.607 +
  12.608 +lemma word_less_minus_mono_left: 
  12.609 +  "(y :: 'a :: len0 word) < z ==> x <= y ==> y - x < z - x"
  12.610 +  by uint_arith
  12.611 +
  12.612 +lemma word_less_minus_mono:  
  12.613 +  "a < c ==> d < b ==> a - b < a ==> c - d < c 
  12.614 +  ==> a - b < c - (d::'a::len word)"
  12.615 +  by uint_arith
  12.616 +
  12.617 +lemma word_le_minus_cancel: 
  12.618 +  "y - x <= z - x ==> x <= z ==> (y :: 'a :: len0 word) <= z"
  12.619 +  by uint_arith
  12.620 +
  12.621 +lemma word_le_minus_mono_left: 
  12.622 +  "(y :: 'a :: len0 word) <= z ==> x <= y ==> y - x <= z - x"
  12.623 +  by uint_arith
  12.624 +
  12.625 +lemma word_le_minus_mono:  
  12.626 +  "a <= c ==> d <= b ==> a - b <= a ==> c - d <= c 
  12.627 +  ==> a - b <= c - (d::'a::len word)"
  12.628 +  by uint_arith
  12.629 +
  12.630 +lemma plus_le_left_cancel_wrap: 
  12.631 +  "(x :: 'a :: len0 word) + y' < x ==> x + y < x ==> (x + y' < x + y) = (y' < y)"
  12.632 +  by uint_arith
  12.633 +
  12.634 +lemma plus_le_left_cancel_nowrap: 
  12.635 +  "(x :: 'a :: len0 word) <= x + y' ==> x <= x + y ==> 
  12.636 +    (x + y' < x + y) = (y' < y)" 
  12.637 +  by uint_arith
  12.638 +
  12.639 +lemma word_plus_mono_right2: 
  12.640 +  "(a :: 'a :: len0 word) <= a + b ==> c <= b ==> a <= a + c"
  12.641 +  by uint_arith
  12.642 +
  12.643 +lemma word_less_add_right: 
  12.644 +  "(x :: 'a :: len0 word) < y - z ==> z <= y ==> x + z < y"
  12.645 +  by uint_arith
  12.646 +
  12.647 +lemma word_less_sub_right: 
  12.648 +  "(x :: 'a :: len0 word) < y + z ==> y <= x ==> x - y < z"
  12.649 +  by uint_arith
  12.650 +
  12.651 +lemma word_le_plus_either: 
  12.652 +  "(x :: 'a :: len0 word) <= y | x <= z ==> y <= y + z ==> x <= y + z"
  12.653 +  by uint_arith
  12.654 +
  12.655 +lemma word_less_nowrapI: 
  12.656 +  "(x :: 'a :: len0 word) < z - k ==> k <= z ==> 0 < k ==> x < x + k"
  12.657 +  by uint_arith
  12.658 +
  12.659 +lemma inc_le: "(i :: 'a :: len word) < m ==> i + 1 <= m"
  12.660 +  by uint_arith
  12.661 +
  12.662 +lemma inc_i: 
  12.663 +  "(1 :: 'a :: len word) <= i ==> i < m ==> 1 <= (i + 1) & i + 1 <= m"
  12.664 +  by uint_arith
  12.665 +
  12.666 +lemma udvd_incr_lem:
  12.667 +  "up < uq ==> up = ua + n * uint K ==> 
  12.668 +    uq = ua + n' * uint K ==> up + uint K <= uq"
  12.669 +  apply clarsimp
  12.670 +  apply (drule less_le_mult)
  12.671 +  apply safe
  12.672 +  done
  12.673 +
  12.674 +lemma udvd_incr': 
  12.675 +  "p < q ==> uint p = ua + n * uint K ==> 
  12.676 +    uint q = ua + n' * uint K ==> p + K <= q" 
  12.677 +  apply (unfold word_less_alt word_le_def)
  12.678 +  apply (drule (2) udvd_incr_lem)
  12.679 +  apply (erule uint_add_le [THEN order_trans])
  12.680 +  done
  12.681 +
  12.682 +lemma udvd_decr': 
  12.683 +  "p < q ==> uint p = ua + n * uint K ==> 
  12.684 +    uint q = ua + n' * uint K ==> p <= q - K"
  12.685 +  apply (unfold word_less_alt word_le_def)
  12.686 +  apply (drule (2) udvd_incr_lem)
  12.687 +  apply (drule le_diff_eq [THEN iffD2])
  12.688 +  apply (erule order_trans)
  12.689 +  apply (rule uint_sub_ge)
  12.690 +  done
  12.691 +
  12.692 +lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, simplified]
  12.693 +lemmas udvd_incr0 = udvd_incr' [where ua=0, simplified]
  12.694 +lemmas udvd_decr0 = udvd_decr' [where ua=0, simplified]
  12.695 +
  12.696 +lemma udvd_minus_le': 
  12.697 +  "xy < k ==> z udvd xy ==> z udvd k ==> xy <= k - z"
  12.698 +  apply (unfold udvd_def)
  12.699 +  apply clarify
  12.700 +  apply (erule (2) udvd_decr0)
  12.701 +  done
  12.702 +
  12.703 +lemma udvd_incr2_K: 
  12.704 +  "p < a + s ==> a <= a + s ==> K udvd s ==> K udvd p - a ==> a <= p ==> 
  12.705 +    0 < K ==> p <= p + K & p + K <= a + s"
  12.706 +  apply (unfold udvd_def)
  12.707 +  apply clarify
  12.708 +  apply (simp add: uint_arith_simps split: split_if_asm)
  12.709 +   prefer 2 
  12.710 +   apply (insert uint_range' [of s])[1]
  12.711 +   apply arith
  12.712 +  apply (drule add_commute [THEN xtr1])
  12.713 +  apply (simp add: diff_less_eq [symmetric])
  12.714 +  apply (drule less_le_mult)
  12.715 +   apply arith
  12.716 +  apply simp
  12.717 +  done
  12.718 +
  12.719 +(* links with rbl operations *)
  12.720 +lemma word_succ_rbl:
  12.721 +  "to_bl w = bl ==> to_bl (word_succ w) = (rev (rbl_succ (rev bl)))"
  12.722 +  apply (unfold word_succ_def)
  12.723 +  apply clarify
  12.724 +  apply (simp add: to_bl_of_bin)
  12.725 +  apply (simp add: to_bl_def rbl_succ)
  12.726 +  done
  12.727 +
  12.728 +lemma word_pred_rbl:
  12.729 +  "to_bl w = bl ==> to_bl (word_pred w) = (rev (rbl_pred (rev bl)))"
  12.730 +  apply (unfold word_pred_def)
  12.731 +  apply clarify
  12.732 +  apply (simp add: to_bl_of_bin)
  12.733 +  apply (simp add: to_bl_def rbl_pred)
  12.734 +  done
  12.735 +
  12.736 +lemma word_add_rbl:
  12.737 +  "to_bl v = vbl ==> to_bl w = wbl ==> 
  12.738 +    to_bl (v + w) = (rev (rbl_add (rev vbl) (rev wbl)))"
  12.739 +  apply (unfold word_add_def)
  12.740 +  apply clarify
  12.741 +  apply (simp add: to_bl_of_bin)
  12.742 +  apply (simp add: to_bl_def rbl_add)
  12.743 +  done
  12.744 +
  12.745 +lemma word_mult_rbl:
  12.746 +  "to_bl v = vbl ==> to_bl w = wbl ==> 
  12.747 +    to_bl (v * w) = (rev (rbl_mult (rev vbl) (rev wbl)))"
  12.748 +  apply (unfold word_mult_def)
  12.749 +  apply clarify
  12.750 +  apply (simp add: to_bl_of_bin)
  12.751 +  apply (simp add: to_bl_def rbl_mult)
  12.752 +  done
  12.753 +
  12.754 +lemma rtb_rbl_ariths:
  12.755 +  "rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_succ w)) = rbl_succ ys"
  12.756 +
  12.757 +  "rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_pred w)) = rbl_pred ys"
  12.758 +
  12.759 +  "[| rev (to_bl v) = ys; rev (to_bl w) = xs |] 
  12.760 +  ==> rev (to_bl (v * w)) = rbl_mult ys xs"
  12.761 +
  12.762 +  "[| rev (to_bl v) = ys; rev (to_bl w) = xs |] 
  12.763 +  ==> rev (to_bl (v + w)) = rbl_add ys xs"
  12.764 +  by (auto simp: rev_swap [symmetric] word_succ_rbl 
  12.765 +                 word_pred_rbl word_mult_rbl word_add_rbl)
  12.766 +
  12.767 +
  12.768 +section "Arithmetic type class instantiations"
  12.769 +
  12.770 +instance word :: (len0) comm_monoid_add ..
  12.771 +
  12.772 +instance word :: (len0) comm_monoid_mult
  12.773 +  apply (intro_classes)
  12.774 +   apply (simp add: word_mult_commute)
  12.775 +  apply (simp add: word_mult_1)
  12.776 +  done
  12.777 +
  12.778 +instance word :: (len0) comm_semiring 
  12.779 +  by (intro_classes) (simp add : word_left_distrib)
  12.780 +
  12.781 +instance word :: (len0) ab_group_add ..
  12.782 +
  12.783 +instance word :: (len0) comm_ring ..
  12.784 +
  12.785 +instance word :: (len) comm_semiring_1 
  12.786 +  by (intro_classes) (simp add: lenw1_zero_neq_one)
  12.787 +
  12.788 +instance word :: (len) comm_ring_1 ..
  12.789 +
  12.790 +instance word :: (len0) comm_semiring_0 ..
  12.791 +
  12.792 +instance word :: (len0) order ..
  12.793 +
  12.794 +instance word :: (len) recpower
  12.795 +  by (intro_classes) (simp_all add: word_pow)
  12.796 +
  12.797 +(* note that iszero_def is only for class comm_semiring_1_cancel,
  12.798 +   which requires word length >= 1, ie 'a :: len word *) 
  12.799 +lemma zero_bintrunc:
  12.800 +  "iszero (number_of x :: 'a :: len word) = 
  12.801 +    (bintrunc (len_of TYPE('a)) x = Numeral.Pls)"
  12.802 +  apply (unfold iszero_def word_0_wi word_no_wi)
  12.803 +  apply (rule word_ubin.norm_eq_iff [symmetric, THEN trans])
  12.804 +  apply (simp add : Pls_def [symmetric])
  12.805 +  done
  12.806 +
  12.807 +lemmas word_le_0_iff [simp] =
  12.808 +  word_zero_le [THEN leD, THEN linorder_antisym_conv1]
  12.809 +
  12.810 +lemma word_of_nat: "of_nat n = word_of_int (int n)"
  12.811 +  by (induct n) (auto simp add : word_of_int_hom_syms)
  12.812 +
  12.813 +lemma word_of_int: "of_int = word_of_int"
  12.814 +  apply (rule ext)
  12.815 +  apply (unfold of_int_def)
  12.816 +  apply (rule contentsI)
  12.817 +  apply safe
  12.818 +  apply (simp_all add: word_of_nat word_of_int_homs)
  12.819 +   defer
  12.820 +   apply (rule Rep_Integ_ne [THEN nonemptyE])
  12.821 +   apply (rule bexI)
  12.822 +    prefer 2
  12.823 +    apply assumption
  12.824 +   apply (auto simp add: RI_eq_diff)
  12.825 +  done
  12.826 +
  12.827 +lemma word_of_int_nat: 
  12.828 +  "0 <= x ==> word_of_int x = of_nat (nat x)"
  12.829 +  by (simp add: of_nat_nat word_of_int)
  12.830 +
  12.831 +lemma word_number_of_eq: 
  12.832 +  "number_of w = (of_int w :: 'a :: len word)"
  12.833 +  unfolding word_number_of_def word_of_int by auto
  12.834 +
  12.835 +instance word :: (len) number_ring
  12.836 +  by (intro_classes) (simp add : word_number_of_eq)
  12.837 +
  12.838 +lemma iszero_word_no [simp] : 
  12.839 +  "iszero (number_of bin :: 'a :: len word) = 
  12.840 +    iszero (number_of (bintrunc (len_of TYPE('a)) bin) :: int)"
  12.841 +  apply (simp add: zero_bintrunc int_number_of)
  12.842 +  apply (unfold iszero_def Pls_def)
  12.843 +  apply (rule refl)
  12.844 +  done
  12.845 +    
  12.846 +
  12.847 +section "Word and nat"
  12.848 +
  12.849 +lemma td_ext_unat':
  12.850 +  "n = len_of TYPE ('a :: len) ==> 
  12.851 +    td_ext (unat :: 'a word => nat) of_nat 
  12.852 +    (unats n) (%i. i mod 2 ^ n)"
  12.853 +  apply (unfold td_ext_def' unat_def word_of_nat unats_uints)
  12.854 +  apply (auto intro!: imageI simp add : word_of_int_hom_syms)
  12.855 +  apply (erule word_uint.Abs_inverse [THEN arg_cong])
  12.856 +  apply (simp add: int_word_uint nat_mod_distrib nat_power_eq)
  12.857 +  done
  12.858 +
  12.859 +lemmas td_ext_unat = refl [THEN td_ext_unat']
  12.860 +lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm, standard]
  12.861 +
  12.862 +interpretation word_unat:
  12.863 +  td_ext ["unat::'a::len word => nat" 
  12.864 +          of_nat 
  12.865 +          "unats (len_of TYPE('a::len))"
  12.866 +          "%i. i mod 2 ^ len_of TYPE('a::len)"]
  12.867 +  by (rule td_ext_unat)
  12.868 +
  12.869 +lemmas td_unat = word_unat.td_thm
  12.870 +
  12.871 +lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq]
  12.872 +
  12.873 +lemma unat_le: "y <= unat (z :: 'a :: len word) ==> y : unats (len_of TYPE ('a))"
  12.874 +  apply (unfold unats_def)
  12.875 +  apply clarsimp
  12.876 +  apply (rule xtrans, rule unat_lt2p, assumption) 
  12.877 +  done
  12.878 +
  12.879 +lemma word_nchotomy:
  12.880 +  "ALL w. EX n. (w :: 'a :: len word) = of_nat n & n < 2 ^ len_of TYPE ('a)"
  12.881 +  apply (rule allI)
  12.882 +  apply (rule word_unat.Abs_cases)
  12.883 +  apply (unfold unats_def)
  12.884 +  apply auto
  12.885 +  done
  12.886 +
  12.887 +lemma of_nat_eq:
  12.888 +  fixes w :: "'a::len word"
  12.889 +  shows "(of_nat n = w) = (\<exists>q. n = unat w + q * 2 ^ len_of TYPE('a))"
  12.890 +  apply (rule trans)
  12.891 +   apply (rule word_unat.inverse_norm)
  12.892 +  apply (rule iffI)
  12.893 +   apply (rule mod_eqD)
  12.894 +   apply simp
  12.895 +  apply clarsimp
  12.896 +  done
  12.897 +
  12.898 +lemma of_nat_eq_size: 
  12.899 +  "(of_nat n = w) = (EX q. n = unat w + q * 2 ^ size w)"
  12.900 +  unfolding word_size by (rule of_nat_eq)
  12.901 +
  12.902 +lemma of_nat_0:
  12.903 +  "(of_nat m = (0::'a::len word)) = (\<exists>q. m = q * 2 ^ len_of TYPE('a))"
  12.904 +  by (simp add: of_nat_eq)
  12.905 +
  12.906 +lemmas of_nat_2p = mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]]
  12.907 +
  12.908 +lemma of_nat_gt_0: "of_nat k ~= 0 ==> 0 < k"
  12.909 +  by (cases k) auto
  12.910 +
  12.911 +lemma of_nat_neq_0: 
  12.912 +  "0 < k ==> k < 2 ^ len_of TYPE ('a :: len) ==> of_nat k ~= (0 :: 'a word)"
  12.913 +  by (clarsimp simp add : of_nat_0)
  12.914 +
  12.915 +lemma Abs_fnat_hom_add:
  12.916 +  "of_nat a + of_nat b = of_nat (a + b)"
  12.917 +  by simp
  12.918 +
  12.919 +lemma Abs_fnat_hom_mult:
  12.920 +  "of_nat a * of_nat b = (of_nat (a * b) :: 'a :: len word)"
  12.921 +  by (simp add: word_of_nat word_of_int_mult_hom zmult_int)
  12.922 +
  12.923 +lemma Abs_fnat_hom_Suc:
  12.924 +  "word_succ (of_nat a) = of_nat (Suc a)"
  12.925 +  by (simp add: word_of_nat word_of_int_succ_hom add_ac)
  12.926 +
  12.927 +lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0"
  12.928 +  by (simp add: word_of_nat word_0_wi)
  12.929 +
  12.930 +lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)"
  12.931 +  by (simp add: word_of_nat word_1_wi)
  12.932 +
  12.933 +lemmas Abs_fnat_homs = 
  12.934 +  Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc 
  12.935 +  Abs_fnat_hom_0 Abs_fnat_hom_1
  12.936 +
  12.937 +lemma word_arith_nat_add:
  12.938 +  "a + b = of_nat (unat a + unat b)" 
  12.939 +  by simp
  12.940 +
  12.941 +lemma word_arith_nat_mult:
  12.942 +  "a * b = of_nat (unat a * unat b)"
  12.943 +  by (simp add: Abs_fnat_hom_mult [symmetric])
  12.944 +    
  12.945 +lemma word_arith_nat_Suc:
  12.946 +  "word_succ a = of_nat (Suc (unat a))"
  12.947 +  by (subst Abs_fnat_hom_Suc [symmetric]) simp
  12.948 +
  12.949 +lemma word_arith_nat_div:
  12.950 +  "a div b = of_nat (unat a div unat b)"
  12.951 +  by (simp add: word_div_def word_of_nat zdiv_int uint_nat)
  12.952 +
  12.953 +lemma word_arith_nat_mod:
  12.954 +  "a mod b = of_nat (unat a mod unat b)"
  12.955 +  by (simp add: word_mod_def word_of_nat zmod_int uint_nat)
  12.956 +
  12.957 +lemmas word_arith_nat_defs =
  12.958 +  word_arith_nat_add word_arith_nat_mult
  12.959 +  word_arith_nat_Suc Abs_fnat_hom_0
  12.960 +  Abs_fnat_hom_1 word_arith_nat_div
  12.961 +  word_arith_nat_mod 
  12.962 +
  12.963 +lemmas unat_cong = arg_cong [where f = "unat"]
  12.964 +  
  12.965 +lemmas unat_word_ariths = word_arith_nat_defs
  12.966 +  [THEN trans [OF unat_cong unat_of_nat], standard]
  12.967 +
  12.968 +lemmas word_sub_less_iff = word_sub_le_iff
  12.969 +  [simplified linorder_not_less [symmetric], simplified]
  12.970 +
  12.971 +lemma unat_add_lem: 
  12.972 +  "(unat x + unat y < 2 ^ len_of TYPE('a)) = 
  12.973 +    (unat (x + y :: 'a :: len word) = unat x + unat y)"
  12.974 +  unfolding unat_word_ariths
  12.975 +  by (auto intro!: trans [OF _ nat_mod_lem])
  12.976 +
  12.977 +lemma unat_mult_lem: 
  12.978 +  "(unat x * unat y < 2 ^ len_of TYPE('a)) = 
  12.979 +    (unat (x * y :: 'a :: len word) = unat x * unat y)"
  12.980 +  unfolding unat_word_ariths
  12.981 +  by (auto intro!: trans [OF _ nat_mod_lem])
  12.982 +
  12.983 +lemmas unat_plus_if' = 
  12.984 +  trans [OF unat_word_ariths(1) mod_nat_add, simplified, standard]
  12.985 +
  12.986 +lemma le_no_overflow: 
  12.987 +  "x <= b ==> a <= a + b ==> x <= a + (b :: 'a :: len0 word)"
  12.988 +  apply (erule order_trans)
  12.989 +  apply (erule olen_add_eqv [THEN iffD1])
  12.990 +  done
  12.991 +
  12.992 +lemmas un_ui_le = trans 
  12.993 +  [OF word_le_nat_alt [symmetric] 
  12.994 +      word_le_def [THEN meta_eq_to_obj_eq], 
  12.995 +   standard]
  12.996 +
  12.997 +lemma unat_sub_if_size:
  12.998 +  "unat (x - y) = (if unat y <= unat x 
  12.999 +   then unat x - unat y 
 12.1000 +   else unat x + 2 ^ size x - unat y)"
 12.1001 +  apply (unfold word_size)
 12.1002 +  apply (simp add: un_ui_le)
 12.1003 +  apply (auto simp add: unat_def uint_sub_if')
 12.1004 +   apply (rule nat_diff_distrib)
 12.1005 +    prefer 3
 12.1006 +    apply (simp add: group_simps)
 12.1007 +    apply (rule nat_diff_distrib [THEN trans])
 12.1008 +      prefer 3
 12.1009 +      apply (subst nat_add_distrib)
 12.1010 +        prefer 3
 12.1011 +        apply (simp add: nat_power_eq)
 12.1012 +       apply auto
 12.1013 +  apply uint_arith
 12.1014 +  done
 12.1015 +
 12.1016 +lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size]
 12.1017 +
 12.1018 +lemma unat_div: "unat ((x :: 'a :: len word) div y) = unat x div unat y"
 12.1019 +  apply (simp add : unat_word_ariths)
 12.1020 +  apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
 12.1021 +  apply (rule div_le_dividend)
 12.1022 +  done
 12.1023 +
 12.1024 +lemma unat_mod: "unat ((x :: 'a :: len word) mod y) = unat x mod unat y"
 12.1025 +  apply (clarsimp simp add : unat_word_ariths)
 12.1026 +  apply (cases "unat y")
 12.1027 +   prefer 2
 12.1028 +   apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
 12.1029 +   apply (rule mod_le_divisor)
 12.1030 +   apply auto
 12.1031 +  done
 12.1032 +
 12.1033 +lemma uint_div: "uint ((x :: 'a :: len word) div y) = uint x div uint y"
 12.1034 +  unfolding uint_nat by (simp add : unat_div zdiv_int)
 12.1035 +
 12.1036 +lemma uint_mod: "uint ((x :: 'a :: len word) mod y) = uint x mod uint y"
 12.1037 +  unfolding uint_nat by (simp add : unat_mod zmod_int)
 12.1038 +
 12.1039 +
 12.1040 +section {* Definition of unat\_arith tactic *}
 12.1041 +
 12.1042 +lemma unat_split:
 12.1043 +  fixes x::"'a::len word"
 12.1044 +  shows "P (unat x) = 
 12.1045 +         (ALL n. of_nat n = x & n < 2^len_of TYPE('a) --> P n)"
 12.1046 +  by (auto simp: unat_of_nat)
 12.1047 +
 12.1048 +lemma unat_split_asm:
 12.1049 +  fixes x::"'a::len word"
 12.1050 +  shows "P (unat x) = 
 12.1051 +         (~(EX n. of_nat n = x & n < 2^len_of TYPE('a) & ~ P n))"
 12.1052 +  by (auto simp: unat_of_nat)
 12.1053 +
 12.1054 +lemmas of_nat_inverse = 
 12.1055 +  word_unat.Abs_inverse' [rotated, unfolded unats_def, simplified]
 12.1056 +
 12.1057 +lemmas unat_splits = unat_split unat_split_asm
 12.1058 +
 12.1059 +lemmas unat_arith_simps =
 12.1060 +  word_le_nat_alt word_less_nat_alt
 12.1061 +  word_unat.Rep_inject [symmetric]
 12.1062 +  unat_sub_if' unat_plus_if' unat_div unat_mod
 12.1063 +
 12.1064 +(* unat_arith_tac: tactic to reduce word arithmetic to nat, 
 12.1065 +   try to solve via arith *)
 12.1066 +ML {*
 12.1067 +fun unat_arith_ss_of ss = 
 12.1068 +  ss addsimps @{thms unat_arith_simps}
 12.1069 +     delsimps @{thms word_unat.Rep_inject}
 12.1070 +     addsplits @{thms split_if_asm}
 12.1071 +     addcongs @{thms power_False_cong}
 12.1072 +
 12.1073 +fun unat_arith_tacs ctxt =   
 12.1074 +  let fun arith_tac' n t = arith_tac ctxt n t handle COOPER => Seq.empty  
 12.1075 +  in 
 12.1076 +    [ CLASET' clarify_tac 1,
 12.1077 +      SIMPSET' (full_simp_tac o unat_arith_ss_of) 1,
 12.1078 +      ALLGOALS (full_simp_tac (HOL_ss addsplits @{thms unat_splits} 
 12.1079 +                                       addcongs @{thms power_False_cong})),
 12.1080 +      rewrite_goals_tac @{thms word_size}, 
 12.1081 +      ALLGOALS  (fn n => REPEAT (resolve_tac [allI, impI] n) THEN      
 12.1082 +                         REPEAT (etac conjE n) THEN
 12.1083 +                         REPEAT (dtac @{thm of_nat_inverse} n THEN atac n)),
 12.1084 +      TRYALL arith_tac' ] 
 12.1085 +  end
 12.1086 +
 12.1087 +fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt))
 12.1088 +*}
 12.1089 +
 12.1090 +method_setup unat_arith = 
 12.1091 +  "Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD (unat_arith_tac ctxt 1))" 
 12.1092 +  "solving word arithmetic via natural numbers and arith"
 12.1093 +
 12.1094 +lemma no_plus_overflow_unat_size: 
 12.1095 +  "((x :: 'a :: len word) <= x + y) = (unat x + unat y < 2 ^ size x)" 
 12.1096 +  unfolding word_size by unat_arith
 12.1097 +
 12.1098 +lemma unat_sub: "b <= a ==> unat (a - b) = unat a - unat (b :: 'a :: len word)"
 12.1099 +  by unat_arith
 12.1100 +
 12.1101 +lemmas no_olen_add_nat = no_plus_overflow_unat_size [unfolded word_size]
 12.1102 +
 12.1103 +lemmas unat_plus_simple = trans [OF no_olen_add_nat unat_add_lem, standard]
 12.1104 +
 12.1105 +lemma word_div_mult: 
 12.1106 +  "(0 :: 'a :: len word) < y ==> unat x * unat y < 2 ^ len_of TYPE('a) ==> 
 12.1107 +    x * y div y = x"
 12.1108 +  apply unat_arith
 12.1109 +  apply clarsimp
 12.1110 +  apply (subst unat_mult_lem [THEN iffD1])
 12.1111 +  apply auto
 12.1112 +  done
 12.1113 +
 12.1114 +lemma div_lt': "(i :: 'a :: len word) <= k div x ==> 
 12.1115 +    unat i * unat x < 2 ^ len_of TYPE('a)"
 12.1116 +  apply unat_arith
 12.1117 +  apply clarsimp
 12.1118 +  apply (drule mult_le_mono1)
 12.1119 +  apply (erule order_le_less_trans)
 12.1120 +  apply (rule xtr7 [OF unat_lt2p div_mult_le])
 12.1121 +  done
 12.1122 +
 12.1123 +lemmas div_lt'' = order_less_imp_le [THEN div_lt']
 12.1124 +
 12.1125 +lemma div_lt_mult: "(i :: 'a :: len word) < k div x ==> 0 < x ==> i * x < k"
 12.1126 +  apply (frule div_lt'' [THEN unat_mult_lem [THEN iffD1]])
 12.1127 +  apply (simp add: unat_arith_simps)
 12.1128 +  apply (drule (1) mult_less_mono1)
 12.1129 +  apply (erule order_less_le_trans)
 12.1130 +  apply (rule div_mult_le)
 12.1131 +  done
 12.1132 +
 12.1133 +lemma div_le_mult: 
 12.1134 +  "(i :: 'a :: len word) <= k div x ==> 0 < x ==> i * x <= k"
 12.1135 +  apply (frule div_lt' [THEN unat_mult_lem [THEN iffD1]])
 12.1136 +  apply (simp add: unat_arith_simps)
 12.1137 +  apply (drule mult_le_mono1)
 12.1138 +  apply (erule order_trans)
 12.1139 +  apply (rule div_mult_le)
 12.1140 +  done
 12.1141 +
 12.1142 +lemma div_lt_uint': 
 12.1143 +  "(i :: 'a :: len word) <= k div x ==> uint i * uint x < 2 ^ len_of TYPE('a)"
 12.1144 +  apply (unfold uint_nat)
 12.1145 +  apply (drule div_lt')
 12.1146 +  apply (simp add: zmult_int zless_nat_eq_int_zless [symmetric] 
 12.1147 +                   nat_power_eq)
 12.1148 +  done
 12.1149 +
 12.1150 +lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint']
 12.1151 +
 12.1152 +lemma word_le_exists': 
 12.1153 +  "(x :: 'a :: len0 word) <= y ==> 
 12.1154 +    (EX z. y = x + z & uint x + uint z < 2 ^ len_of TYPE('a))"
 12.1155 +  apply (rule exI)
 12.1156 +  apply (rule conjI)
 12.1157 +  apply (rule zadd_diff_inverse)
 12.1158 +  apply uint_arith
 12.1159 +  done
 12.1160 +
 12.1161 +lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab]
 12.1162 +
 12.1163 +lemmas plus_minus_no_overflow =
 12.1164 +  order_less_imp_le [THEN plus_minus_no_overflow_ab]
 12.1165 +  
 12.1166 +lemmas mcs = word_less_minus_cancel word_less_minus_mono_left
 12.1167 +  word_le_minus_cancel word_le_minus_mono_left
 12.1168 +
 12.1169 +lemmas word_l_diffs = mcs [where y = "?w + ?x", unfolded add_diff_cancel]
 12.1170 +lemmas word_diff_ls = mcs [where z = "?w + ?x", unfolded add_diff_cancel]
 12.1171 +lemmas word_plus_mcs = word_diff_ls 
 12.1172 +  [where y = "?v + ?x", unfolded add_diff_cancel]
 12.1173 +
 12.1174 +lemmas le_unat_uoi = unat_le [THEN word_unat.Abs_inverse]
 12.1175 +
 12.1176 +lemmas thd = refl [THEN [2] split_div_lemma [THEN iffD2], THEN conjunct1]
 12.1177 +
 12.1178 +lemma thd1:
 12.1179 +  "a div b * b \<le> (a::nat)"
 12.1180 +  using gt_or_eq_0 [of b]
 12.1181 +  apply (rule disjE)
 12.1182 +   apply (erule xtr4 [OF thd mult_commute])
 12.1183 +  apply clarsimp
 12.1184 +  done
 12.1185 +
 12.1186 +lemmas uno_simps [THEN le_unat_uoi, standard] =
 12.1187 +  mod_le_divisor div_le_dividend thd1 
 12.1188 +
 12.1189 +lemma word_mod_div_equality:
 12.1190 +  "(n div b) * b + (n mod b) = (n :: 'a :: len word)"
 12.1191 +  apply (unfold word_less_nat_alt word_arith_nat_defs)
 12.1192 +  apply (cut_tac y="unat b" in gt_or_eq_0)
 12.1193 +  apply (erule disjE)
 12.1194 +   apply (simp add: mod_div_equality uno_simps)
 12.1195 +  apply simp
 12.1196 +  done
 12.1197 +
 12.1198 +lemma word_div_mult_le: "a div b * b <= (a::'a::len word)"
 12.1199 +  apply (unfold word_le_nat_alt word_arith_nat_defs)
 12.1200 +  apply (cut_tac y="unat b" in gt_or_eq_0)
 12.1201 +  apply (erule disjE)
 12.1202 +   apply (simp add: div_mult_le uno_simps)
 12.1203 +  apply simp
 12.1204 +  done
 12.1205 +
 12.1206 +lemma word_mod_less_divisor: "0 < n ==> m mod n < (n :: 'a :: len word)"
 12.1207 +  apply (simp only: word_less_nat_alt word_arith_nat_defs)
 12.1208 +  apply (clarsimp simp add : uno_simps)
 12.1209 +  done
 12.1210 +
 12.1211 +lemma word_of_int_power_hom: 
 12.1212 +  "word_of_int a ^ n = (word_of_int (a ^ n) :: 'a :: len word)"
 12.1213 +  by (induct n) (simp_all add : word_of_int_hom_syms power_Suc)
 12.1214 +
 12.1215 +lemma word_arith_power_alt: 
 12.1216 +  "a ^ n = (word_of_int (uint a ^ n) :: 'a :: len word)"
 12.1217 +  by (simp add : word_of_int_power_hom [symmetric])
 12.1218 +
 12.1219 +lemma of_bl_length_less: 
 12.1220 +  "length x = k ==> k < len_of TYPE('a) ==> (of_bl x :: 'a :: len word) < 2 ^ k"
 12.1221 +  apply (unfold of_bl_no [unfolded word_number_of_def]
 12.1222 +                word_less_alt word_number_of_alt)
 12.1223 +  apply safe
 12.1224 +  apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm 
 12.1225 +                       del: word_of_int_bin)
 12.1226 +  apply (simp add: mod_pos_pos_trivial)
 12.1227 +  apply (subst mod_pos_pos_trivial)
 12.1228 +    apply (rule bl_to_bin_ge0)
 12.1229 +   apply (rule order_less_trans)
 12.1230 +    apply (rule bl_to_bin_lt2p)
 12.1231 +   apply simp
 12.1232 +  apply (rule bl_to_bin_lt2p)    
 12.1233 +  done
 12.1234 +
 12.1235 +
 12.1236 +section "Cardinality, finiteness of set of words"
 12.1237 +
 12.1238 +lemmas card_lessThan' = card_lessThan [unfolded lessThan_def]
 12.1239 +
 12.1240 +lemmas card_eq = word_unat.Abs_inj_on [THEN card_image,
 12.1241 +  unfolded word_unat.image, unfolded unats_def, standard]
 12.1242 +
 12.1243 +lemmas card_word = trans [OF card_eq card_lessThan', standard]
 12.1244 +
 12.1245 +lemma finite_word_UNIV: "finite (UNIV :: 'a :: len word set)"
 12.1246 +  apply (rule contrapos_np)
 12.1247 +   prefer 2
 12.1248 +   apply (erule card_infinite)
 12.1249 +  apply (simp add : card_word)
 12.1250 +  done
 12.1251 +
 12.1252 +lemma card_word_size: 
 12.1253 +  "card (UNIV :: 'a :: len word set) = (2 ^ size (x :: 'a word))"
 12.1254 +  unfolding word_size by (rule card_word)
 12.1255 +
 12.1256 +end 
 12.1257 +
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/src/HOL/Word/WordBitwise.thy	Mon Aug 20 04:34:31 2007 +0200
    13.3 @@ -0,0 +1,499 @@
    13.4 +(* 
    13.5 +    ID:         $Id$
    13.6 +    Author:     Jeremy Dawson and Gerwin Klein, NICTA
    13.7 +
    13.8 +  contains theorems to do with bit-wise (logical) operations on words
    13.9 +*)
   13.10 +theory WordBitwise imports WordArith begin
   13.11 +
   13.12 +lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or
   13.13 +  
   13.14 +(* following definitions require both arithmetic and bit-wise word operations *)
   13.15 +
   13.16 +(* to get word_no_log_defs from word_log_defs, using bin_log_bintrs *)
   13.17 +lemmas wils1 = bin_log_bintrs [THEN word_ubin.norm_eq_iff [THEN iffD1],
   13.18 +  folded word_ubin.eq_norm, THEN eq_reflection, standard]
   13.19 +
   13.20 +(* the binary operations only *)
   13.21 +lemmas word_log_binary_defs = 
   13.22 +  word_and_def word_or_def word_xor_def
   13.23 +
   13.24 +lemmas word_no_log_defs [simp] = 
   13.25 +  word_not_def  [where a="number_of ?a", 
   13.26 +                 unfolded word_no_wi wils1, folded word_no_wi]
   13.27 +  word_log_binary_defs [where a="number_of ?a" and b="number_of ?b",
   13.28 +                        unfolded word_no_wi wils1, folded word_no_wi]
   13.29 +
   13.30 +lemmas word_wi_log_defs = word_no_log_defs [unfolded word_no_wi]
   13.31 +
   13.32 +lemma uint_or: "uint (x OR y) = int_or (uint x) (uint y)"
   13.33 +  by (simp add: word_or_def word_no_wi [symmetric] int_number_of
   13.34 +                bin_trunc_ao(2) [symmetric])
   13.35 +
   13.36 +lemma uint_and: "uint (x AND y) = int_and (uint x) (uint y)"
   13.37 +  by (simp add: word_and_def int_number_of word_no_wi [symmetric]
   13.38 +                bin_trunc_ao(1) [symmetric]) 
   13.39 +
   13.40 +lemma word_ops_nth_size:
   13.41 +  "n < size (x::'a::len0 word) ==> 
   13.42 +    (x OR y) !! n = (x !! n | y !! n) & 
   13.43 +    (x AND y) !! n = (x !! n & y !! n) & 
   13.44 +    (x XOR y) !! n = (x !! n ~= y !! n) & 
   13.45 +    (NOT x) !! n = (~ x !! n)"
   13.46 +  unfolding word_size word_no_wi word_test_bit_def word_log_defs
   13.47 +  by (clarsimp simp add : word_ubin.eq_norm nth_bintr bin_nth_ops)
   13.48 +
   13.49 +lemma word_ao_nth:
   13.50 +  fixes x :: "'a::len0 word"
   13.51 +  shows "(x OR y) !! n = (x !! n | y !! n) & 
   13.52 +         (x AND y) !! n = (x !! n & y !! n)"
   13.53 +  apply (cases "n < size x")
   13.54 +   apply (drule_tac y = "y" in word_ops_nth_size)
   13.55 +   apply simp
   13.56 +  apply (simp add : test_bit_bin word_size)
   13.57 +  done
   13.58 +
   13.59 +(* get from commutativity, associativity etc of int_and etc
   13.60 +  to same for word_and etc *)
   13.61 +
   13.62 +lemmas bwsimps = 
   13.63 +  word_of_int_homs(2) 
   13.64 +  word_0_wi_Pls
   13.65 +  word_m1_wi_Min
   13.66 +  word_wi_log_defs
   13.67 +
   13.68 +lemma word_bw_assocs:
   13.69 +  fixes x :: "'a::len0 word"
   13.70 +  shows
   13.71 +  "(x AND y) AND z = x AND y AND z"
   13.72 +  "(x OR y) OR z = x OR y OR z"
   13.73 +  "(x XOR y) XOR z = x XOR y XOR z"
   13.74 +  using word_of_int_Ex [where x=x] 
   13.75 +        word_of_int_Ex [where x=y] 
   13.76 +        word_of_int_Ex [where x=z]
   13.77 +  by (auto simp: bwsimps)
   13.78 +  
   13.79 +lemma word_bw_comms:
   13.80 +  fixes x :: "'a::len0 word"
   13.81 +  shows
   13.82 +  "x AND y = y AND x"
   13.83 +  "x OR y = y OR x"
   13.84 +  "x XOR y = y XOR x"
   13.85 +  using word_of_int_Ex [where x=x] 
   13.86 +        word_of_int_Ex [where x=y] 
   13.87 +  by (auto simp: bwsimps)
   13.88 +  
   13.89 +lemma word_bw_lcs:
   13.90 +  fixes x :: "'a::len0 word"
   13.91 +  shows
   13.92 +  "y AND x AND z = x AND y AND z"
   13.93 +  "y OR x OR z = x OR y OR z"
   13.94 +  "y XOR x XOR z = x XOR y XOR z"
   13.95 +  using word_of_int_Ex [where x=x] 
   13.96 +        word_of_int_Ex [where x=y] 
   13.97 +        word_of_int_Ex [where x=z]
   13.98 +  by (auto simp: bwsimps)
   13.99 +
  13.100 +lemma word_log_esimps [simp]:
  13.101 +  fixes x :: "'a::len0 word"
  13.102 +  shows
  13.103 +  "x AND 0 = 0"
  13.104 +  "x AND -1 = x"
  13.105 +  "x OR 0 = x"
  13.106 +  "x OR -1 = -1"
  13.107 +  "x XOR 0 = x"
  13.108 +  "x XOR -1 = NOT x"
  13.109 +  "0 AND x = 0"
  13.110 +  "-1 AND x = x"
  13.111 +  "0 OR x = x"
  13.112 +  "-1 OR x = -1"
  13.113 +  "0 XOR x = x"
  13.114 +  "-1 XOR x = NOT x"
  13.115 +  using word_of_int_Ex [where x=x] 
  13.116 +  by (auto simp: bwsimps)
  13.117 +
  13.118 +lemma word_not_dist:
  13.119 +  fixes x :: "'a::len0 word"
  13.120 +  shows
  13.121 +  "NOT (x OR y) = NOT x AND NOT y"
  13.122 +  "NOT (x AND y) = NOT x OR NOT y"
  13.123 +  using word_of_int_Ex [where x=x] 
  13.124 +        word_of_int_Ex [where x=y] 
  13.125 +  by (auto simp: bwsimps bbw_not_dist)
  13.126 +
  13.127 +lemma word_bw_same:
  13.128 +  fixes x :: "'a::len0 word"
  13.129 +  shows
  13.130 +  "x AND x = x"
  13.131 +  "x OR x = x"
  13.132 +  "x XOR x = 0"
  13.133 +  using word_of_int_Ex [where x=x] 
  13.134 +  by (auto simp: bwsimps)
  13.135 +
  13.136 +lemma word_ao_absorbs [simp]:
  13.137 +  fixes x :: "'a::len0 word"
  13.138 +  shows
  13.139 +  "x AND (y OR x) = x"
  13.140 +  "x OR y AND x = x"
  13.141 +  "x AND (x OR y) = x"
  13.142 +  "y AND x OR x = x"
  13.143 +  "(y OR x) AND x = x"
  13.144 +  "x OR x AND y = x"
  13.145 +  "(x OR y) AND x = x"
  13.146 +  "x AND y OR x = x"
  13.147 +  using word_of_int_Ex [where x=x] 
  13.148 +        word_of_int_Ex [where x=y] 
  13.149 +  by (auto simp: bwsimps)
  13.150 +
  13.151 +lemma word_not_not [simp]:
  13.152 +  "NOT NOT (x::'a::len0 word) = x"
  13.153 +  using word_of_int_Ex [where x=x] 
  13.154 +  by (auto simp: bwsimps)
  13.155 +
  13.156 +lemma word_ao_dist:
  13.157 +  fixes x :: "'a::len0 word"
  13.158 +  shows "(x OR y) AND z = x AND z OR y AND z"
  13.159 +  using word_of_int_Ex [where x=x] 
  13.160 +        word_of_int_Ex [where x=y] 
  13.161 +        word_of_int_Ex [where x=z]   
  13.162 +  by (auto simp: bwsimps bbw_ao_dist simp del: bin_ops_comm)
  13.163 +
  13.164 +lemma word_oa_dist:
  13.165 +  fixes x :: "'a::len0 word"
  13.166 +  shows "x AND y OR z = (x OR z) AND (y OR z)"
  13.167 +  using word_of_int_Ex [where x=x] 
  13.168 +        word_of_int_Ex [where x=y] 
  13.169 +        word_of_int_Ex [where x=z]   
  13.170 +  by (auto simp: bwsimps bbw_oa_dist simp del: bin_ops_comm)
  13.171 +
  13.172 +lemma word_add_not [simp]: 
  13.173 +  fixes x :: "'a::len0 word"
  13.174 +  shows "x + NOT x = -1"
  13.175 +  using word_of_int_Ex [where x=x] 
  13.176 +  by (auto simp: bwsimps bin_add_not)
  13.177 +
  13.178 +lemma word_plus_and_or [simp]:
  13.179 +  fixes x :: "'a::len0 word"
  13.180 +  shows "(x AND y) + (x OR y) = x + y"
  13.181 +  using word_of_int_Ex [where x=x] 
  13.182 +        word_of_int_Ex [where x=y] 
  13.183 +  by (auto simp: bwsimps plus_and_or)
  13.184 +
  13.185 +lemma leoa:   
  13.186 +  fixes x :: "'a::len0 word"
  13.187 +  shows "(w = (x OR y)) ==> (y = (w AND y))" by auto
  13.188 +lemma leao: 
  13.189 +  fixes x' :: "'a::len0 word"
  13.190 +  shows "(w' = (x' AND y')) ==> (x' = (x' OR w'))" by auto 
  13.191 +
  13.192 +lemmas word_ao_equiv = leao [COMP leoa [COMP iffI]]
  13.193 +
  13.194 +lemma le_word_or2: "x <= x OR (y::'a::len0 word)"
  13.195 +  unfolding word_le_def uint_or
  13.196 +  by (auto intro: le_int_or) 
  13.197 +
  13.198 +lemmas le_word_or1 = xtr3 [OF word_bw_comms (2) le_word_or2, standard]
  13.199 +lemmas word_and_le1 =
  13.200 +  xtr3 [OF word_ao_absorbs (4) [symmetric] le_word_or2, standard]
  13.201 +lemmas word_and_le2 =
  13.202 +  xtr3 [OF word_ao_absorbs (8) [symmetric] le_word_or2, standard]
  13.203 +
  13.204 +lemma bl_word_not: "to_bl (NOT w) = map Not (to_bl w)" 
  13.205 +  unfolding to_bl_def word_log_defs
  13.206 +  by (simp add: bl_not_bin int_number_of word_no_wi [symmetric])
  13.207 +
  13.208 +lemma bl_word_xor: "to_bl (v XOR w) = app2 op ~= (to_bl v) (to_bl w)" 
  13.209 +  unfolding to_bl_def word_log_defs bl_xor_bin
  13.210 +  by (simp add: int_number_of word_no_wi [symmetric])
  13.211 +
  13.212 +lemma bl_word_or: "to_bl (v OR w) = app2 op | (to_bl v) (to_bl w)" 
  13.213 +  unfolding to_bl_def word_log_defs
  13.214 +  by (simp add: bl_or_bin int_number_of word_no_wi [symmetric])
  13.215 +
  13.216 +lemma bl_word_and: "to_bl (v AND w) = app2 op & (to_bl v) (to_bl w)" 
  13.217 +  unfolding to_bl_def word_log_defs
  13.218 +  by (simp add: bl_and_bin int_number_of word_no_wi [symmetric])
  13.219 +
  13.220 +lemma word_lsb_alt: "lsb (w::'a::len0 word) = test_bit w 0"
  13.221 +  by (auto simp: word_test_bit_def word_lsb_def)
  13.222 +
  13.223 +lemma word_lsb_1_0: "lsb (1::'a::len word) & ~ lsb (0::'b::len0 word)"
  13.224 +  unfolding word_lsb_def word_1_no word_0_no by auto
  13.225 +
  13.226 +lemma word_lsb_last: "lsb (w::'a::len word) = last (to_bl w)"
  13.227 +  apply (unfold word_lsb_def uint_bl bin_to_bl_def) 
  13.228 +  apply (rule_tac bin="uint w" in bin_exhaust)
  13.229 +  apply (cases "size w")
  13.230 +   apply auto
  13.231 +   apply (auto simp add: bin_to_bl_aux_alt)
  13.232 +  done
  13.233 +
  13.234 +lemma word_lsb_int: "lsb w = (uint w mod 2 = 1)"
  13.235 +  unfolding word_lsb_def bin_last_mod by auto
  13.236 +
  13.237 +lemma word_msb_sint: "msb w = (sint w < 0)" 
  13.238 +  unfolding word_msb_def
  13.239 +  by (simp add : sign_Min_lt_0 int_number_of)
  13.240 +  
  13.241 +lemma word_msb_no': 
  13.242 +  "w = number_of bin ==> msb (w::'a::len word) = bin_nth bin (size w - 1)"
  13.243 +  unfolding word_msb_def word_number_of_def
  13.244 +  by (clarsimp simp add: word_sbin.eq_norm word_size bin_sign_lem)
  13.245 +
  13.246 +lemmas word_msb_no = refl [THEN word_msb_no', unfolded word_size]
  13.247 +
  13.248 +lemma word_msb_nth': "msb (w::'a::len word) = bin_nth (uint w) (size w - 1)"
  13.249 +  apply (unfold word_size)
  13.250 +  apply (rule trans [OF _ word_msb_no])
  13.251 +  apply (simp add : word_number_of_def)
  13.252 +  done
  13.253 +
  13.254 +lemmas word_msb_nth = word_msb_nth' [unfolded word_size]
  13.255 +
  13.256 +lemma word_msb_alt: "msb (w::'a::len word) = hd (to_bl w)"
  13.257 +  apply (unfold word_msb_nth uint_bl)
  13.258 +  apply (subst hd_conv_nth)
  13.259 +  apply (rule length_greater_0_conv [THEN iffD1])
  13.260 +   apply simp
  13.261 +  apply (simp add : nth_bin_to_bl word_size)
  13.262 +  done
  13.263 +
  13.264 +lemma word_set_nth:
  13.265 +  "set_bit w n (test_bit w n) = (w::'a::len0 word)"
  13.266 +  unfolding word_test_bit_def word_set_bit_def by auto
  13.267 +
  13.268 +lemma bin_nth_uint':
  13.269 +  "bin_nth (uint w) n = (rev (bin_to_bl (size w) (uint w)) ! n & n < size w)"
  13.270 +  apply (unfold word_size)
  13.271 +  apply (safe elim!: bin_nth_uint_imp)
  13.272 +   apply (frule bin_nth_uint_imp)
  13.273 +   apply (fast dest!: bin_nth_bl)+
  13.274 +  done
  13.275 +
  13.276 +lemmas bin_nth_uint = bin_nth_uint' [unfolded word_size]
  13.277 +
  13.278 +lemma test_bit_bl: "w !! n = (rev (to_bl w) ! n & n < size w)"
  13.279 +  unfolding to_bl_def word_test_bit_def word_size
  13.280 +  by (rule bin_nth_uint)
  13.281 +
  13.282 +lemma to_bl_nth: "n < size w ==> to_bl w ! n = w !! (size w - Suc n)"
  13.283 +  apply (unfold test_bit_bl)
  13.284 +  apply clarsimp
  13.285 +  apply (rule trans)
  13.286 +   apply (rule nth_rev_alt)
  13.287 +   apply (auto simp add: word_size)
  13.288 +  done
  13.289 +
  13.290 +lemma test_bit_set: 
  13.291 +  fixes w :: "'a::len0 word"
  13.292 +  shows "(set_bit w n x) !! n = (n < size w & x)"
  13.293 +  unfolding word_size word_test_bit_def word_set_bit_def
  13.294 +  by (clarsimp simp add : word_ubin.eq_norm nth_bintr)
  13.295 +
  13.296 +lemma test_bit_set_gen: 
  13.297 +  fixes w :: "'a::len0 word"
  13.298 +  shows "test_bit (set_bit w n x) m = 
  13.299 +         (if m = n then n < size w & x else test_bit w m)"
  13.300 +  apply (unfold word_size word_test_bit_def word_set_bit_def)
  13.301 +  apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen)
  13.302 +  apply (auto elim!: test_bit_size [unfolded word_size]
  13.303 +              simp add: word_test_bit_def [symmetric])
  13.304 +  done
  13.305 +
  13.306 +lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs"
  13.307 +  unfolding of_bl_def bl_to_bin_rep_F by auto
  13.308 +  
  13.309 +lemma msb_nth':
  13.310 +  fixes w :: "'a::len word"
  13.311 +  shows "msb w = w !! (size w - 1)"
  13.312 +  unfolding word_msb_nth' word_test_bit_def by simp
  13.313 +
  13.314 +lemmas msb_nth = msb_nth' [unfolded word_size]
  13.315 +
  13.316 +lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN
  13.317 +  word_ops_nth_size [unfolded word_size], standard]
  13.318 +lemmas msb1 = msb0 [where i = 0]
  13.319 +lemmas word_ops_msb = msb1 [unfolded msb_nth [symmetric, unfolded One_nat_def]]
  13.320 +
  13.321 +lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size], standard]
  13.322 +lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt]
  13.323 +
  13.324 +lemma td_ext_nth':
  13.325 +  "n = size (w::'a::len0 word) ==> ofn = set_bits ==> [w, ofn g] = l ==> 
  13.326 +    td_ext test_bit ofn {f. ALL i. f i --> i < n} (%h i. h i & i < n)"
  13.327 +  apply (unfold word_size td_ext_def')
  13.328 +  apply safe
  13.329 +     apply (rule_tac [3] ext)
  13.330 +     apply (rule_tac [4] ext)
  13.331 +     apply (unfold word_size of_nth_def test_bit_bl)
  13.332 +     apply safe
  13.333 +       defer
  13.334 +       apply (clarsimp simp: word_bl.Abs_inverse)+
  13.335 +  apply (rule word_bl.Rep_inverse')
  13.336 +  apply (rule sym [THEN trans])
  13.337 +  apply (rule bl_of_nth_nth)
  13.338 +  apply simp
  13.339 +  apply (rule bl_of_nth_inj)
  13.340 +  apply (clarsimp simp add : test_bit_bl word_size)
  13.341 +  done
  13.342 +
  13.343 +lemmas td_ext_nth = td_ext_nth' [OF refl refl refl, unfolded word_size]
  13.344 +
  13.345 +interpretation test_bit:
  13.346 +  td_ext ["op !! :: 'a::len0 word => nat => bool"
  13.347 +          set_bits
  13.348 +          "{f. \<forall>i. f i \<longrightarrow> i < len_of TYPE('a::len0)}"
  13.349 +          "(\<lambda>h i. h i \<and> i < len_of TYPE('a::len0))"]
  13.350 +  by (rule td_ext_nth)
  13.351 +
  13.352 +declare test_bit.Rep' [simp del]
  13.353 +declare test_bit.Rep' [rule del]
  13.354 +
  13.355 +lemmas td_nth = test_bit.td_thm
  13.356 +
  13.357 +lemma word_set_set_same: 
  13.358 +  fixes w :: "'a::len0 word"
  13.359 +  shows "set_bit (set_bit w n x) n y = set_bit w n y" 
  13.360 +  by (rule word_eqI) (simp add : test_bit_set_gen word_size)
  13.361 +    
  13.362 +lemma word_set_set_diff: 
  13.363 +  fixes w :: "'a::len0 word"
  13.364 +  assumes "m ~= n"
  13.365 +  shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x" 
  13.366 +  by (rule word_eqI) (clarsimp simp add : test_bit_set_gen word_size prems)
  13.367 +    
  13.368 +lemma test_bit_no': 
  13.369 +  fixes w :: "'a::len0 word"
  13.370 +  shows "w = number_of bin ==> test_bit w n = (n < size w & bin_nth bin n)"
  13.371 +  unfolding word_test_bit_def word_number_of_def word_size
  13.372 +  by (simp add : nth_bintr [symmetric] word_ubin.eq_norm)
  13.373 +
  13.374 +lemmas test_bit_no = 
  13.375 +  refl [THEN test_bit_no', unfolded word_size, THEN eq_reflection, standard]
  13.376 +
  13.377 +lemma nth_0: "~ (0::'a::len0 word) !! n"
  13.378 +  unfolding test_bit_no word_0_no by auto
  13.379 +
  13.380 +lemma nth_sint: 
  13.381 +  fixes w :: "'a::len word"
  13.382 +  defines "l \<equiv> len_of TYPE ('a)"
  13.383 +  shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))"
  13.384 +  unfolding sint_uint l_def
  13.385 +  by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric])
  13.386 +
  13.387 +lemma word_lsb_no: 
  13.388 +  "lsb (number_of bin :: 'a :: len word) = (bin_last bin = bit.B1)"
  13.389 +  unfolding word_lsb_alt test_bit_no by auto
  13.390 +
  13.391 +lemma word_set_no: 
  13.392 +  "set_bit (number_of bin::'a::len0 word) n b = 
  13.393 +    number_of (bin_sc n (if b then bit.B1 else bit.B0) bin)"
  13.394 +  apply (unfold word_set_bit_def word_number_of_def [symmetric])
  13.395 +  apply (rule word_eqI)
  13.396 +  apply (clarsimp simp: word_size bin_nth_sc_gen int_number_of 
  13.397 +                        test_bit_no nth_bintr)
  13.398 +  done
  13.399 +
  13.400 +lemmas setBit_no = setBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
  13.401 +  simplified if_simps, THEN eq_reflection, standard]
  13.402 +lemmas clearBit_no = clearBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
  13.403 +  simplified if_simps, THEN eq_reflection, standard]
  13.404 +
  13.405 +lemma to_bl_n1: 
  13.406 +  "to_bl (-1::'a::len0 word) = replicate (len_of TYPE ('a)) True"
  13.407 +  apply (rule word_bl.Abs_inverse')
  13.408 +   apply simp
  13.409 +  apply (rule word_eqI)
  13.410 +  apply (clarsimp simp add: word_size test_bit_no)
  13.411 +  apply (auto simp add: word_bl.Abs_inverse test_bit_bl word_size)
  13.412 +  done
  13.413 +
  13.414 +lemma word_msb_n1: "msb (-1::'a::len word)"
  13.415 +  unfolding word_msb_alt word_msb_alt to_bl_n1 by simp
  13.416 +
  13.417 +declare word_set_set_same [simp] word_set_nth [simp]
  13.418 +  test_bit_no [simp] word_set_no [simp] nth_0 [simp]
  13.419 +  setBit_no [simp] clearBit_no [simp]
  13.420 +  word_lsb_no [simp] word_msb_no [simp] word_msb_n1 [simp] word_lsb_1_0 [simp]
  13.421 +
  13.422 +lemma word_set_nth_iff: 
  13.423 +  "(set_bit w n b = w) = (w !! n = b | n >= size (w::'a::len0 word))"
  13.424 +  apply (rule iffI)
  13.425 +   apply (rule disjCI)
  13.426 +   apply (drule word_eqD)
  13.427 +   apply (erule sym [THEN trans])
  13.428 +   apply (simp add: test_bit_set)
  13.429 +  apply (erule disjE)
  13.430 +   apply clarsimp
  13.431 +  apply (rule word_eqI)
  13.432 +  apply (clarsimp simp add : test_bit_set_gen)
  13.433 +  apply (drule test_bit_size)
  13.434 +  apply force
  13.435 +  done
  13.436 +
  13.437 +lemma test_bit_2p': 
  13.438 +  "w = word_of_int (2 ^ n) ==> 
  13.439 +    w !! m = (m = n & m < size (w :: 'a :: len word))"
  13.440 +  unfolding word_test_bit_def word_size
  13.441 +  by (auto simp add: word_ubin.eq_norm nth_bintr nth_2p_bin)
  13.442 +
  13.443 +lemmas test_bit_2p = refl [THEN test_bit_2p', unfolded word_size]
  13.444 +
  13.445 +lemmas nth_w2p = test_bit_2p [unfolded of_int_number_of_eq
  13.446 +  word_of_int [symmetric] of_int_power]
  13.447 +
  13.448 +lemma uint_2p: 
  13.449 +  "(0::'a::len word) < 2 ^ n ==> uint (2 ^ n::'a::len word) = 2 ^ n"
  13.450 +  apply (unfold word_arith_power_alt)
  13.451 +  apply (case_tac "len_of TYPE ('a)")
  13.452 +   apply clarsimp
  13.453 +  apply (case_tac "nat")
  13.454 +   apply clarsimp
  13.455 +   apply (case_tac "n")
  13.456 +    apply (clarsimp simp add : word_1_wi [symmetric])
  13.457 +   apply (clarsimp simp add : word_0_wi [symmetric])
  13.458 +  apply (drule word_gt_0 [THEN iffD1])
  13.459 +  apply (safe intro!: word_eqI bin_nth_lem ext)
  13.460 +     apply (auto simp add: test_bit_2p nth_2p_bin word_test_bit_def [symmetric])
  13.461 +  done
  13.462 +
  13.463 +lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a :: len word) = 2 ^ n" 
  13.464 +  apply (unfold word_arith_power_alt)
  13.465 +  apply (case_tac "len_of TYPE ('a)")
  13.466 +   apply clarsimp
  13.467 +  apply (case_tac "nat")
  13.468 +   apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 
  13.469 +   apply (rule box_equals) 
  13.470 +     apply (rule_tac [2] bintr_ariths (1))+ 
  13.471 +   apply (clarsimp simp add : int_number_of)
  13.472 +  apply simp 
  13.473 +  done
  13.474 +
  13.475 +lemma bang_is_le: "x !! m ==> 2 ^ m <= (x :: 'a :: len word)" 
  13.476 +  apply (rule xtr3) 
  13.477 +  apply (rule_tac [2] y = "x" in le_word_or2)
  13.478 +  apply (rule word_eqI)
  13.479 +  apply (auto simp add: word_ao_nth nth_w2p word_size)
  13.480 +  done
  13.481 +
  13.482 +lemma word_clr_le: 
  13.483 +  fixes w :: "'a::len0 word"
  13.484 +  shows "w >= set_bit w n False"
  13.485 +  apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
  13.486 +  apply simp
  13.487 +  apply (rule order_trans)
  13.488 +   apply (rule bintr_bin_clr_le)
  13.489 +  apply simp
  13.490 +  done
  13.491 +
  13.492 +lemma word_set_ge: 
  13.493 +  fixes w :: "'a::len word"
  13.494 +  shows "w <= set_bit w n True"
  13.495 +  apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
  13.496 +  apply simp
  13.497 +  apply (rule order_trans [OF _ bintr_bin_set_ge])
  13.498 +  apply simp
  13.499 +  done
  13.500 +
  13.501 +end
  13.502 +
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/src/HOL/Word/WordDefinition.thy	Mon Aug 20 04:34:31 2007 +0200
    14.3 @@ -0,0 +1,905 @@
    14.4 +(* 
    14.5 +  ID:     $Id$
    14.6 +  Author: Jeremy Dawson and Gerwin Klein, NICTA
    14.7 +  
    14.8 +  Basic definition of word type and basic theorems following from 
    14.9 +  the definition of the word type 
   14.10 +*) 
   14.11 +
   14.12 +
   14.13 +theory WordDefinition imports Size BitSyntax BinBoolList begin 
   14.14 +
   14.15 +typedef (open word) 'a word
   14.16 +  = "{(0::int) ..< 2^len_of TYPE('a::len0)}" by auto
   14.17 +
   14.18 +instance word :: (len0) number ..
   14.19 +instance word :: (type) minus ..
   14.20 +instance word :: (type) plus ..
   14.21 +instance word :: (type) one ..
   14.22 +instance word :: (type) zero ..
   14.23 +instance word :: (type) times ..
   14.24 +instance word :: (type) Divides.div ..
   14.25 +instance word :: (type) power ..
   14.26 +instance word :: (type) ord ..
   14.27 +instance word :: (type) size ..
   14.28 +instance word :: (type) inverse ..
   14.29 +instance word :: (type) bit ..
   14.30 +
   14.31 +
   14.32 +section "Type conversions and casting"
   14.33 +
   14.34 +constdefs
   14.35 +  -- {* representation of words using unsigned or signed bins, 
   14.36 +        only difference in these is the type class *}
   14.37 +  word_of_int :: "int => 'a :: len0 word"
   14.38 +  "word_of_int w == Abs_word (bintrunc (len_of TYPE ('a)) w)" 
   14.39 +
   14.40 +  -- {* uint and sint cast a word to an integer,
   14.41 +        uint treats the word as unsigned,
   14.42 +        sint treats the most-significant-bit as a sign bit *}
   14.43 +  uint :: "'a :: len0 word => int"
   14.44 +  "uint w == Rep_word w"
   14.45 +  sint :: "'a :: len word => int"
   14.46 +  sint_uint: "sint w == sbintrunc (len_of TYPE ('a) - 1) (uint w)"
   14.47 +  unat :: "'a :: len0 word => nat"
   14.48 +  "unat w == nat (uint w)"
   14.49 +
   14.50 +  -- "the sets of integers representing the words"
   14.51 +  uints :: "nat => int set"
   14.52 +  "uints n == range (bintrunc n)"
   14.53 +  sints :: "nat => int set"
   14.54 +  "sints n == range (sbintrunc (n - 1))"
   14.55 +  unats :: "nat => nat set"
   14.56 +  "unats n == {i. i < 2 ^ n}"
   14.57 +  norm_sint :: "nat => int => int"
   14.58 +  "norm_sint n w == (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)"
   14.59 +
   14.60 +  -- "cast a word to a different length"
   14.61 +  scast :: "'a :: len word => 'b :: len word"
   14.62 +  "scast w == word_of_int (sint w)"
   14.63 +  ucast :: "'a :: len0 word => 'b :: len0 word"
   14.64 +  "ucast w == word_of_int (uint w)"
   14.65 +
   14.66 +  -- "whether a cast (or other) function is to a longer or shorter length"
   14.67 +  source_size :: "('a :: len0 word => 'b) => nat"
   14.68 +  "source_size c == let arb = arbitrary ; x = c arb in size arb"  
   14.69 +  target_size :: "('a => 'b :: len0 word) => nat"
   14.70 +  "target_size c == size (c arbitrary)"
   14.71 +  is_up :: "('a :: len0 word => 'b :: len0 word) => bool"
   14.72 +  "is_up c == source_size c <= target_size c"
   14.73 +  is_down :: "('a :: len0 word => 'b :: len0 word) => bool"
   14.74 +  "is_down c == target_size c <= source_size c"
   14.75 +
   14.76 +constdefs
   14.77 +  of_bl :: "bool list => 'a :: len0 word" 
   14.78 +  "of_bl bl == word_of_int (bl_to_bin bl)"
   14.79 +  to_bl :: "'a :: len0 word => bool list"
   14.80 +  "to_bl w == 
   14.81 +  bin_to_bl (len_of TYPE ('a)) (uint w)"
   14.82 +
   14.83 +  word_reverse :: "'a :: len0 word => 'a word"
   14.84 +  "word_reverse w == of_bl (rev (to_bl w))"
   14.85 +
   14.86 +defs (overloaded)
   14.87 +  word_size: "size (w :: 'a :: len0 word) == len_of TYPE('a)"
   14.88 +  word_number_of_def: "number_of w == word_of_int w"
   14.89 +
   14.90 +constdefs
   14.91 +  word_int_case :: "(int => 'b) => ('a :: len0 word) => 'b"
   14.92 +  "word_int_case f w == f (uint w)"
   14.93 +
   14.94 +syntax
   14.95 +  of_int :: "int => 'a"
   14.96 +translations
   14.97 +  "case x of of_int y => b" == "word_int_case (%y. b) x"
   14.98 +
   14.99 +
  14.100 +section  "Arithmetic operations"
  14.101 +
  14.102 +defs (overloaded)
  14.103 +  word_1_wi: "(1 :: ('a :: len0) word) == word_of_int 1"
  14.104 +  word_0_wi: "(0 :: ('a :: len0) word) == word_of_int 0"
  14.105 +
  14.106 +  word_le_def: "a <= b == uint a <= uint b"
  14.107 +  word_less_def: "x < y == x <= y & x ~= (y :: 'a :: len0 word)"
  14.108 +
  14.109 +constdefs
  14.110 +  word_succ :: "'a :: len0 word => 'a word"
  14.111 +  "word_succ a == word_of_int (Numeral.succ (uint a))"
  14.112 +
  14.113 +  word_pred :: "'a :: len0 word => 'a word"
  14.114 +  "word_pred a == word_of_int (Numeral.pred (uint a))"
  14.115 +
  14.116 +  udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50)
  14.117 +  "a udvd b == EX n>=0. uint b = n * uint a"
  14.118 +
  14.119 +  word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50)
  14.120 +  "a <=s b == sint a <= sint b"
  14.121 +
  14.122 +  word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50)
  14.123 +  "(x <s y) == (x <=s y & x ~= y)"
  14.124 +
  14.125 +consts
  14.126 +  word_power :: "'a :: len0 word => nat => 'a word"
  14.127 +primrec
  14.128 +  "word_power a 0 = 1"
  14.129 +  "word_power a (Suc n) = a * word_power a n"
  14.130 +
  14.131 +defs (overloaded)
  14.132 +  word_pow: "power == word_power"
  14.133 +  word_add_def: "a + b == word_of_int (uint a + uint b)"
  14.134 +  word_sub_wi: "a - b == word_of_int (uint a - uint b)"
  14.135 +  word_minus_def: "- a == word_of_int (- uint a)"
  14.136 +  word_mult_def: "a * b == word_of_int (uint a * uint b)"
  14.137 +  word_div_def: "a div b == word_of_int (uint a div uint b)"
  14.138 +  word_mod_def: "a mod b == word_of_int (uint a mod uint b)"
  14.139 +
  14.140 +
  14.141 +section "Bit-wise operations"
  14.142 +
  14.143 +defs (overloaded)
  14.144 +  word_and_def: 
  14.145 +  "(a::'a::len0 word) AND b == word_of_int (int_and (uint a) (uint b))"
  14.146 +
  14.147 +  word_or_def:  
  14.148 +  "(a::'a::len0 word) OR b == word_of_int (int_or (uint a) (uint b))"
  14.149 +
  14.150 +  word_xor_def: 
  14.151 +  "(a::'a::len0 word) XOR b == word_of_int (int_xor (uint a) (uint b))"
  14.152 +
  14.153 +  word_not_def: 
  14.154 +  "NOT (a::'a::len0 word) == word_of_int (int_not (uint a))"
  14.155 +
  14.156 +  word_test_bit_def: 
  14.157 +  "test_bit (a::'a::len0 word) == bin_nth (uint a)"
  14.158 +
  14.159 +  word_set_bit_def: 
  14.160 +  "set_bit (a::'a::len0 word) n x == 
  14.161 +   word_of_int (bin_sc n (If x bit.B1 bit.B0) (uint a))"
  14.162 +
  14.163 +  word_set_bits_def:  
  14.164 +  "(BITS n. f n)::'a::len0 word == of_bl (bl_of_nth (len_of TYPE ('a)) f)"
  14.165 +
  14.166 +  word_lsb_def: 
  14.167 +  "lsb (a::'a::len0 word) == bin_last (uint a) = bit.B1"
  14.168 +
  14.169 +  word_msb_def: 
  14.170 +  "msb (a::'a::len word) == bin_sign (sint a) = Numeral.Min"
  14.171 +
  14.172 +
  14.173 +constdefs
  14.174 +  setBit :: "'a :: len0 word => nat => 'a word" 
  14.175 +  "setBit w n == set_bit w n True"
  14.176 +
  14.177 +  clearBit :: "'a :: len0 word => nat => 'a word" 
  14.178 +  "clearBit w n == set_bit w n False"
  14.179 +
  14.180 +
  14.181 +section "Shift operations"
  14.182 +
  14.183 +constdefs
  14.184 +  shiftl1 :: "'a :: len0 word => 'a word"
  14.185 +  "shiftl1 w == word_of_int (uint w BIT bit.B0)"
  14.186 +
  14.187 +  -- "shift right as unsigned or as signed, ie logical or arithmetic"
  14.188 +  shiftr1 :: "'a :: len0 word => 'a word"
  14.189 +  "shiftr1 w == word_of_int (bin_rest (uint w))"
  14.190 +
  14.191 +  sshiftr1 :: "'a :: len word => 'a word" 
  14.192 +  "sshiftr1 w == word_of_int (bin_rest (sint w))"
  14.193 +
  14.194 +  bshiftr1 :: "bool => 'a :: len word => 'a word"
  14.195 +  "bshiftr1 b w == of_bl (b # butlast (to_bl w))"
  14.196 +
  14.197 +  sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55)
  14.198 +  "w >>> n == (sshiftr1 ^ n) w"
  14.199 +
  14.200 +  mask :: "nat => 'a::len word"
  14.201 +  "mask n == (1 << n) - 1"
  14.202 +
  14.203 +  revcast :: "'a :: len0 word => 'b :: len0 word"
  14.204 +  "revcast w ==  of_bl (takefill False (len_of TYPE('b)) (to_bl w))"
  14.205 +
  14.206 +  slice1 :: "nat => 'a :: len0 word => 'b :: len0 word"
  14.207 +  "slice1 n w == of_bl (takefill False n (to_bl w))"
  14.208 +
  14.209 +  slice :: "nat => 'a :: len0 word => 'b :: len0 word"
  14.210 +  "slice n w == slice1 (size w - n) w"
  14.211 +
  14.212 +
  14.213 +defs (overloaded)
  14.214 +  shiftl_def: "(w::'a::len0 word) << n == (shiftl1 ^ n) w"
  14.215 +  shiftr_def: "(w::'a::len0 word) >> n == (shiftr1 ^ n) w"
  14.216 +
  14.217 +
  14.218 +section "Rotation"
  14.219 +
  14.220 +constdefs
  14.221 +  rotater1 :: "'a list => 'a list"
  14.222 +  "rotater1 ys == 
  14.223 +    case ys of [] => [] | x # xs => last ys # butlast ys"
  14.224 +
  14.225 +  rotater :: "nat => 'a list => 'a list"
  14.226 +  "rotater n == rotater1 ^ n"
  14.227 +
  14.228 +  word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word"
  14.229 +  "word_rotr n w == of_bl (rotater n (to_bl w))"
  14.230 +
  14.231 +  word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word"
  14.232 +  "word_rotl n w == of_bl (rotate n (to_bl w))"
  14.233 +
  14.234 +  word_roti :: "int => 'a :: len0 word => 'a :: len0 word"
  14.235 +  "word_roti i w == if i >= 0 then word_rotr (nat i) w
  14.236 +                    else word_rotl (nat (- i)) w"
  14.237 +
  14.238 +
  14.239 +section "Split and cat operations"
  14.240 +
  14.241 +constdefs
  14.242 +  word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word"
  14.243 +  "word_cat a b == word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))"
  14.244 +
  14.245 +  word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)"
  14.246 +  "word_split a == 
  14.247 +   case bin_split (len_of TYPE ('c)) (uint a) of 
  14.248 +     (u, v) => (word_of_int u, word_of_int v)"
  14.249 +
  14.250 +  word_rcat :: "'a :: len0 word list => 'b :: len0 word"
  14.251 +  "word_rcat ws == 
  14.252 +  word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))"
  14.253 +
  14.254 +  word_rsplit :: "'a :: len0 word => 'b :: len word list"
  14.255 +  "word_rsplit w == 
  14.256 +  map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))"
  14.257 +
  14.258 +constdefs
  14.259 +  -- "Largest representable machine integer."
  14.260 +  max_word :: "'a::len word"
  14.261 +  "max_word \<equiv> word_of_int (2^len_of TYPE('a) - 1)"
  14.262 +
  14.263 +consts 
  14.264 +  of_bool :: "bool \<Rightarrow> 'a::len word"
  14.265 +primrec
  14.266 +  "of_bool False = 0"
  14.267 +  "of_bool True = 1"
  14.268 +
  14.269 +
  14.270 +
  14.271 +lemmas of_nth_def = word_set_bits_def
  14.272 +
  14.273 +lemmas word_size_gt_0 [iff] = 
  14.274 +  xtr1 [OF word_size [THEN meta_eq_to_obj_eq] len_gt_0, standard]
  14.275 +lemmas lens_gt_0 = word_size_gt_0 len_gt_0
  14.276 +lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard]
  14.277 +
  14.278 +lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
  14.279 +  by (simp add: uints_def range_bintrunc)
  14.280 +
  14.281 +lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}"
  14.282 +  by (simp add: sints_def range_sbintrunc)
  14.283 +
  14.284 +lemmas atLeastLessThan_alt = atLeastLessThan_def [unfolded 
  14.285 +  atLeast_def lessThan_def Collect_conj_eq [symmetric]]
  14.286 +  
  14.287 +lemma mod_in_reps: "m > 0 ==> y mod m : {0::int ..< m}"
  14.288 +  unfolding atLeastLessThan_alt by auto
  14.289 +
  14.290 +lemma 
  14.291 +  Rep_word_0:"0 <= Rep_word x" and 
  14.292 +  Rep_word_lt: "Rep_word (x::'a::len0 word) < 2 ^ len_of TYPE('a)"
  14.293 +  by (auto simp: Rep_word [simplified])
  14.294 +
  14.295 +lemma Rep_word_mod_same:
  14.296 +  "Rep_word x mod 2 ^ len_of TYPE('a) = Rep_word (x::'a::len0 word)"
  14.297 +  by (simp add: int_mod_eq Rep_word_lt Rep_word_0)
  14.298 +
  14.299 +lemma td_ext_uint: 
  14.300 +  "td_ext (uint :: 'a word => int) word_of_int (uints (len_of TYPE('a::len0))) 
  14.301 +    (%w::int. w mod 2 ^ len_of TYPE('a))"
  14.302 +  apply (unfold td_ext_def')
  14.303 +  apply (simp add: uints_num uint_def word_of_int_def bintrunc_mod2p)
  14.304 +  apply (simp add: Rep_word_mod_same Rep_word_0 Rep_word_lt
  14.305 +                   word.Rep_word_inverse word.Abs_word_inverse int_mod_lem)
  14.306 +  done
  14.307 +
  14.308 +lemmas int_word_uint = td_ext_uint [THEN td_ext.eq_norm, standard]
  14.309 +
  14.310 +interpretation word_uint: 
  14.311 +  td_ext ["uint::'a::len0 word \<Rightarrow> int" 
  14.312 +          word_of_int 
  14.313 +          "uints (len_of TYPE('a::len0))"
  14.314 +          "\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)"]
  14.315 +  by (rule td_ext_uint)
  14.316 +  
  14.317 +lemmas td_uint = word_uint.td_thm
  14.318 +
  14.319 +lemmas td_ext_ubin = td_ext_uint 
  14.320 +  [simplified len_gt_0 no_bintr_alt1 [symmetric]]
  14.321 +
  14.322 +interpretation word_ubin:
  14.323 +  td_ext ["uint::'a::len0 word \<Rightarrow> int" 
  14.324 +          word_of_int 
  14.325 +          "uints (len_of TYPE('a::len0))"
  14.326 +          "bintrunc (len_of TYPE('a::len0))"]
  14.327 +  by (rule td_ext_ubin)
  14.328 +
  14.329 +lemma sint_sbintrunc': 
  14.330 +  "sint (word_of_int bin :: 'a word) = 
  14.331 +    (sbintrunc (len_of TYPE ('a :: len) - 1) bin)"
  14.332 +  unfolding sint_uint 
  14.333 +  by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt)
  14.334 +
  14.335 +lemma uint_sint: 
  14.336 +  "uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a :: len word))"
  14.337 +  unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le)
  14.338 +
  14.339 +lemma bintr_uint': 
  14.340 +  "n >= size w ==> bintrunc n (uint w) = uint w"
  14.341 +  apply (unfold word_size)
  14.342 +  apply (subst word_ubin.norm_Rep [symmetric]) 
  14.343 +  apply (simp only: bintrunc_bintrunc_min word_size min_def)
  14.344 +  apply simp
  14.345 +  done
  14.346 +
  14.347 +lemma wi_bintr': 
  14.348 +  "wb = word_of_int bin ==> n >= size wb ==> 
  14.349 +    word_of_int (bintrunc n bin) = wb"
  14.350 +  unfolding word_size
  14.351 +  by (clarsimp simp add : word_ubin.norm_eq_iff [symmetric] min_def)
  14.352 +
  14.353 +lemmas bintr_uint = bintr_uint' [unfolded word_size]
  14.354 +lemmas wi_bintr = wi_bintr' [unfolded word_size]
  14.355 +
  14.356 +lemma td_ext_sbin: 
  14.357 +  "td_ext (sint :: 'a word => int) word_of_int (sints (len_of TYPE('a::len))) 
  14.358 +    (sbintrunc (len_of TYPE('a) - 1))"
  14.359 +  apply (unfold td_ext_def' sint_uint)
  14.360 +  apply (simp add : word_ubin.eq_norm)
  14.361 +  apply (cases "len_of TYPE('a)")
  14.362 +   apply (auto simp add : sints_def)
  14.363 +  apply (rule sym [THEN trans])
  14.364 +  apply (rule word_ubin.Abs_norm)
  14.365 +  apply (simp only: bintrunc_sbintrunc)
  14.366 +  apply (drule sym)
  14.367 +  apply simp
  14.368 +  done
  14.369 +
  14.370 +lemmas td_ext_sint = td_ext_sbin 
  14.371 +  [simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]]
  14.372 +
  14.373 +(* We do sint before sbin, before sint is the user version
  14.374 +   and interpretations do not produce thm duplicates. I.e. 
  14.375 +   we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD,
  14.376 +   because the latter is the same thm as the former *)
  14.377 +interpretation word_sint:
  14.378 +  td_ext ["sint ::'a::len word => int" 
  14.379 +          word_of_int 
  14.380 +          "sints (len_of TYPE('a::len))"
  14.381 +          "%w. (w + 2^(len_of TYPE('a::len) - 1)) mod 2^len_of TYPE('a::len) -
  14.382 +               2 ^ (len_of TYPE('a::len) - 1)"]
  14.383 +  by (rule td_ext_sint)
  14.384 +
  14.385 +interpretation word_sbin:
  14.386 +  td_ext ["sint ::'a::len word => int" 
  14.387 +          word_of_int 
  14.388 +          "sints (len_of TYPE('a::len))"
  14.389 +          "sbintrunc (len_of TYPE('a::len) - 1)"]
  14.390 +  by (rule td_ext_sbin)
  14.391 +
  14.392 +lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm, standard]
  14.393 +
  14.394 +lemmas td_sint = word_sint.td
  14.395 +
  14.396 +lemma word_number_of_alt: "number_of b == word_of_int (number_of b)"
  14.397 +  unfolding word_number_of_def by (simp add: number_of_eq)
  14.398 +
  14.399 +lemma word_no_wi: "number_of = word_of_int"
  14.400 +  by (auto simp: word_number_of_def intro: ext)
  14.401 +
  14.402 +lemma to_bl_def': 
  14.403 +  "(to_bl :: 'a :: len0 word => bool list) =
  14.404 +    bin_to_bl (len_of TYPE('a)) o uint"
  14.405 +  by (auto simp: to_bl_def intro: ext)
  14.406 +
  14.407 +lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of ?w"]
  14.408 +
  14.409 +lemmas uints_mod = uints_def [unfolded no_bintr_alt1]
  14.410 +
  14.411 +lemma uint_bintrunc: "uint (number_of bin :: 'a word) = 
  14.412 +    number_of (bintrunc (len_of TYPE ('a :: len0)) bin)"
  14.413 +  unfolding word_number_of_def number_of_eq
  14.414 +  by (auto intro: word_ubin.eq_norm) 
  14.415 +
  14.416 +lemma sint_sbintrunc: "sint (number_of bin :: 'a word) = 
  14.417 +    number_of (sbintrunc (len_of TYPE ('a :: len) - 1) bin)" 
  14.418 +  unfolding word_number_of_def number_of_eq
  14.419 +  by (auto intro!: word_sbin.eq_norm simp del: one_is_Suc_zero)
  14.420 +
  14.421 +lemma unat_bintrunc: 
  14.422 +  "unat (number_of bin :: 'a :: len0 word) =
  14.423 +    number_of (bintrunc (len_of TYPE('a)) bin)"
  14.424 +  unfolding unat_def nat_number_of_def 
  14.425 +  by (simp only: uint_bintrunc)
  14.426 +
  14.427 +(* WARNING - these may not always be helpful *)
  14.428 +declare 
  14.429 +  uint_bintrunc [simp] 
  14.430 +  sint_sbintrunc [simp] 
  14.431 +  unat_bintrunc [simp]
  14.432 +
  14.433 +lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 ==> v = w"
  14.434 +  apply (unfold word_size)
  14.435 +  apply (rule word_uint.Rep_eqD)
  14.436 +  apply (rule box_equals)
  14.437 +    defer
  14.438 +    apply (rule word_ubin.norm_Rep)+
  14.439 +  apply simp
  14.440 +  done
  14.441 +
  14.442 +lemmas uint_lem = word_uint.Rep [unfolded uints_num mem_Collect_eq]
  14.443 +lemmas sint_lem = word_sint.Rep [unfolded sints_num mem_Collect_eq]
  14.444 +lemmas uint_ge_0 [iff] = uint_lem [THEN conjunct1, standard]
  14.445 +lemmas uint_lt2p [iff] = uint_lem [THEN conjunct2, standard]
  14.446 +lemmas sint_ge = sint_lem [THEN conjunct1, standard]
  14.447 +lemmas sint_lt = sint_lem [THEN conjunct2, standard]
  14.448 +
  14.449 +lemma sign_uint_Pls [simp]: 
  14.450 +  "bin_sign (uint x) = Numeral.Pls"
  14.451 +  by (simp add: sign_Pls_ge_0 number_of_eq)
  14.452 +
  14.453 +lemmas uint_m2p_neg = iffD2 [OF diff_less_0_iff_less uint_lt2p, standard]
  14.454 +lemmas uint_m2p_not_non_neg = 
  14.455 +  iffD2 [OF linorder_not_le uint_m2p_neg, standard]
  14.456 +
  14.457 +lemma lt2p_lem:
  14.458 +  "len_of TYPE('a) <= n ==> uint (w :: 'a :: len0 word) < 2 ^ n"
  14.459 +  by (rule xtr8 [OF _ uint_lt2p]) simp
  14.460 +
  14.461 +lemmas uint_le_0_iff [simp] = 
  14.462 +  uint_ge_0 [THEN leD, THEN linorder_antisym_conv1, standard]
  14.463 +
  14.464 +lemma uint_nat: "uint w == int (unat w)"
  14.465 +  unfolding unat_def by auto
  14.466 +
  14.467 +lemma uint_number_of:
  14.468 +  "uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)"
  14.469 +  unfolding word_number_of_alt
  14.470 +  by (simp only: int_word_uint)
  14.471 +
  14.472 +lemma unat_number_of: 
  14.473 +  "bin_sign b = Numeral.Pls ==> 
  14.474 +  unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)"
  14.475 +  apply (unfold unat_def)
  14.476 +  apply (clarsimp simp only: uint_number_of)
  14.477 +  apply (rule nat_mod_distrib [THEN trans])
  14.478 +    apply (erule sign_Pls_ge_0 [THEN iffD1])
  14.479 +   apply (simp_all add: nat_power_eq)
  14.480 +  done
  14.481 +
  14.482 +lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + 
  14.483 +    2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) -
  14.484 +    2 ^ (len_of TYPE('a) - 1)"
  14.485 +  unfolding word_number_of_alt by (rule int_word_sint)
  14.486 +
  14.487 +lemma word_of_int_bin [simp] : 
  14.488 +  "(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)"
  14.489 +  unfolding word_number_of_alt by auto
  14.490 +
  14.491 +lemma word_int_case_wi: 
  14.492 +  "word_int_case f (word_of_int i :: 'b word) = 
  14.493 +    f (i mod 2 ^ len_of TYPE('b::len0))"
  14.494 +  unfolding word_int_case_def by (simp add: word_uint.eq_norm)
  14.495 +
  14.496 +lemma word_int_split: 
  14.497 +  "P (word_int_case f x) = 
  14.498 +    (ALL i. x = (word_of_int i :: 'b :: len0 word) & 
  14.499 +      0 <= i & i < 2 ^ len_of TYPE('b) --> P (f i))"
  14.500 +  unfolding word_int_case_def
  14.501 +  by (auto simp: word_uint.eq_norm int_mod_eq')
  14.502 +
  14.503 +lemma word_int_split_asm: 
  14.504 +  "P (word_int_case f x) = 
  14.505 +    (~ (EX n. x = (word_of_int n :: 'b::len0 word) &
  14.506 +      0 <= n & n < 2 ^ len_of TYPE('b::len0) & ~ P (f n)))"
  14.507 +  unfolding word_int_case_def
  14.508 +  by (auto simp: word_uint.eq_norm int_mod_eq')
  14.509 +  
  14.510 +lemmas uint_range' =
  14.511 +  word_uint.Rep [unfolded uints_num mem_Collect_eq, standard]
  14.512 +lemmas sint_range' = word_sint.Rep [unfolded One_nat_def
  14.513 +  sints_num mem_Collect_eq, standard]
  14.514 +
  14.515 +lemma uint_range_size: "0 <= uint w & uint w < 2 ^ size w"
  14.516 +  unfolding word_size by (rule uint_range')
  14.517 +
  14.518 +lemma sint_range_size:
  14.519 +  "- (2 ^ (size w - Suc 0)) <= sint w & sint w < 2 ^ (size w - Suc 0)"
  14.520 +  unfolding word_size by (rule sint_range')
  14.521 +
  14.522 +lemmas sint_above_size = sint_range_size
  14.523 +  [THEN conjunct2, THEN [2] xtr8, folded One_nat_def, standard]
  14.524 +
  14.525 +lemmas sint_below_size = sint_range_size
  14.526 +  [THEN conjunct1, THEN [2] order_trans, folded One_nat_def, standard]
  14.527 +
  14.528 +lemma test_bit_eq_iff: "(test_bit (u::'a::len0 word) = test_bit v) = (u = v)"
  14.529 +  unfolding word_test_bit_def by (simp add: bin_nth_eq_iff)
  14.530 +
  14.531 +lemma test_bit_size [rule_format] : "(w::'a::len0 word) !! n --> n < size w"
  14.532 +  apply (unfold word_test_bit_def)
  14.533 +  apply (subst word_ubin.norm_Rep [symmetric])
  14.534 +  apply (simp only: nth_bintr word_size)
  14.535 +  apply fast
  14.536 +  done
  14.537 +
  14.538 +lemma word_eqI [rule_format] : 
  14.539 +  fixes u :: "'a::len0 word"
  14.540 +  shows "(ALL n. n < size u --> u !! n = v !! n) ==> u = v"
  14.541 +  apply (rule test_bit_eq_iff [THEN iffD1])
  14.542 +  apply (rule ext)
  14.543 +  apply (erule allE)
  14.544 +  apply (erule impCE)
  14.545 +   prefer 2
  14.546 +   apply assumption
  14.547 +  apply (auto dest!: test_bit_size simp add: word_size)
  14.548 +  done
  14.549 +
  14.550 +lemmas word_eqD = test_bit_eq_iff [THEN iffD2, THEN fun_cong, standard]
  14.551 +
  14.552 +lemma test_bit_bin': "w !! n = (n < size w & bin_nth (uint w) n)"
  14.553 +  unfolding word_test_bit_def word_size
  14.554 +  by (simp add: nth_bintr [symmetric])
  14.555 +
  14.556 +lemmas test_bit_bin = test_bit_bin' [unfolded word_size]
  14.557 +
  14.558 +lemma bin_nth_uint_imp': "bin_nth (uint w) n --> n < size w"
  14.559 +  apply (unfold word_size)
  14.560 +  apply (rule impI)
  14.561 +  apply (rule nth_bintr [THEN iffD1, THEN conjunct1])
  14.562 +  apply (subst word_ubin.norm_Rep)
  14.563 +  apply assumption
  14.564 +  done
  14.565 +
  14.566 +lemma bin_nth_sint': 
  14.567 +  "n >= size w --> bin_nth (sint w) n = bin_nth (sint w) (size w - 1)"
  14.568 +  apply (rule impI)
  14.569 +  apply (subst word_sbin.norm_Rep [symmetric])
  14.570 +  apply (simp add : nth_sbintr word_size)
  14.571 +  apply auto
  14.572 +  done
  14.573 +
  14.574 +lemmas bin_nth_uint_imp = bin_nth_uint_imp' [rule_format, unfolded word_size]
  14.575 +lemmas bin_nth_sint = bin_nth_sint' [rule_format, unfolded word_size]
  14.576 +
  14.577 +(* type definitions theorem for in terms of equivalent bool list *)
  14.578 +lemma td_bl: 
  14.579 +  "type_definition (to_bl :: 'a::len0 word => bool list) 
  14.580 +                   of_bl  
  14.581 +                   {bl. length bl = len_of TYPE('a)}"
  14.582 +  apply (unfold type_definition_def of_bl_def to_bl_def)
  14.583 +  apply (simp add: word_ubin.eq_norm)
  14.584 +  apply safe
  14.585 +  apply (drule sym)
  14.586 +  apply simp
  14.587 +  done
  14.588 +
  14.589 +interpretation word_bl:
  14.590 +  type_definition ["to_bl :: 'a::len0 word => bool list"
  14.591 +                   of_bl  
  14.592 +                   "{bl. length bl = len_of TYPE('a::len0)}"]
  14.593 +  by (rule td_bl)
  14.594 +
  14.595 +lemma word_size_bl: "size w == size (to_bl w)"
  14.596 +  unfolding word_size by auto
  14.597 +
  14.598 +lemma to_bl_use_of_bl:
  14.599 +  "(to_bl w = bl) = (w = of_bl bl \<and> length bl = length (to_bl w))"
  14.600 +  by (fastsimp elim!: word_bl.Abs_inverse [simplified])
  14.601 +
  14.602 +lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)"
  14.603 +  unfolding word_reverse_def by (simp add: word_bl.Abs_inverse)
  14.604 +
  14.605 +lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w"
  14.606 +  unfolding word_reverse_def by (simp add : word_bl.Abs_inverse)
  14.607 +
  14.608 +lemma word_rev_gal: "word_reverse w = u ==> word_reverse u = w"
  14.609 +  by auto
  14.610 +
  14.611 +lemmas word_rev_gal' = sym [THEN word_rev_gal, symmetric, standard]
  14.612 +
  14.613 +lemmas length_bl_gt_0 [iff] = xtr1 [OF word_bl.Rep' len_gt_0, standard]
  14.614 +lemmas bl_not_Nil [iff] = 
  14.615 +  length_bl_gt_0 [THEN length_greater_0_conv [THEN iffD1], standard]
  14.616 +lemmas length_bl_neq_0 [iff] = length_bl_gt_0 [THEN gr_implies_not0]
  14.617 +
  14.618 +lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = Numeral.Min)"
  14.619 +  apply (unfold to_bl_def sint_uint)
  14.620 +  apply (rule trans [OF _ bl_sbin_sign])
  14.621 +  apply simp
  14.622 +  done
  14.623 +
  14.624 +lemma of_bl_drop': 
  14.625 +  "lend = length bl - len_of TYPE ('a :: len0) ==> 
  14.626 +    of_bl (drop lend bl) = (of_bl bl :: 'a word)"
  14.627 +  apply (unfold of_bl_def)
  14.628 +  apply (clarsimp simp add : trunc_bl2bin [symmetric])
  14.629 +  done
  14.630 +
  14.631 +lemmas of_bl_no = of_bl_def [folded word_number_of_def]
  14.632 +
  14.633 +lemma test_bit_of_bl:  
  14.634 +  "(of_bl bl::'a::len0 word) !! n = (rev bl ! n \<and> n < len_of TYPE('a) \<and> n < length bl)"
  14.635 +  apply (unfold of_bl_def word_test_bit_def)
  14.636 +  apply (auto simp add: word_size word_ubin.eq_norm nth_bintr bin_nth_of_bl)
  14.637 +  done
  14.638 +
  14.639 +lemma no_of_bl: 
  14.640 +  "(number_of bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) bin)"
  14.641 +  unfolding word_size of_bl_no by (simp add : word_number_of_def)
  14.642 +
  14.643 +lemma uint_bl: "to_bl w == bin_to_bl (size w) (uint w)"
  14.644 +  unfolding word_size to_bl_def by auto
  14.645 +
  14.646 +lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w"
  14.647 +  unfolding uint_bl by (simp add : word_size)
  14.648 +
  14.649 +lemma to_bl_of_bin: 
  14.650 +  "to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin"
  14.651 +  unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size)
  14.652 +
  14.653 +lemmas to_bl_no_bin [simp] = to_bl_of_bin [folded word_number_of_def]
  14.654 +
  14.655 +lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w"
  14.656 +  unfolding uint_bl by (simp add : word_size)
  14.657 +  
  14.658 +lemmas uint_bl_bin [simp] = trans [OF bin_bl_bin word_ubin.norm_Rep, standard]
  14.659 +
  14.660 +lemmas num_AB_u [simp] = word_uint.Rep_inverse 
  14.661 +  [unfolded o_def word_number_of_def [symmetric], standard]
  14.662 +lemmas num_AB_s [simp] = word_sint.Rep_inverse 
  14.663 +  [unfolded o_def word_number_of_def [symmetric], standard]
  14.664 +
  14.665 +(* naturals *)
  14.666 +lemma uints_unats: "uints n = int ` unats n"
  14.667 +  apply (unfold unats_def uints_num)
  14.668 +  apply safe
  14.669 +  apply (rule_tac image_eqI)
  14.670 +  apply (erule_tac nat_0_le [symmetric])
  14.671 +  apply auto
  14.672 +  apply (erule_tac nat_less_iff [THEN iffD2])
  14.673 +  apply (rule_tac [2] zless_nat_eq_int_zless [THEN iffD1])
  14.674 +  apply (auto simp add : nat_power_eq int_power)
  14.675 +  done
  14.676 +
  14.677 +lemma unats_uints: "unats n = nat ` uints n"
  14.678 +  apply (auto simp add : uints_unats image_iff)
  14.679 +  done
  14.680 +
  14.681 +lemmas bintr_num = word_ubin.norm_eq_iff 
  14.682 +  [symmetric, folded word_number_of_def, standard]
  14.683 +lemmas sbintr_num = word_sbin.norm_eq_iff 
  14.684 +  [symmetric, folded word_number_of_def, standard]
  14.685 +
  14.686 +lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def, standard]
  14.687 +lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def, standard];
  14.688 +    
  14.689 +(* don't add these to simpset, since may want bintrunc n w to be simplified;
  14.690 +  may want these in reverse, but loop as simp rules, so use following *)
  14.691 +
  14.692 +lemma num_of_bintr':
  14.693 +  "bintrunc (len_of TYPE('a :: len0)) a = b ==> 
  14.694 +    number_of a = (number_of b :: 'a word)"
  14.695 +  apply safe
  14.696 +  apply (rule_tac num_of_bintr [symmetric])
  14.697 +  done
  14.698 +
  14.699 +lemma num_of_sbintr':
  14.700 +  "sbintrunc (len_of TYPE('a :: len) - 1) a = b ==> 
  14.701 +    number_of a = (number_of b :: 'a word)"
  14.702 +  apply safe
  14.703 +  apply (rule_tac num_of_sbintr [symmetric])
  14.704 +  done
  14.705 +
  14.706 +lemmas num_abs_bintr = sym [THEN trans,
  14.707 +  OF num_of_bintr word_number_of_def [THEN meta_eq_to_obj_eq], standard]
  14.708 +lemmas num_abs_sbintr = sym [THEN trans,
  14.709 +  OF num_of_sbintr word_number_of_def [THEN meta_eq_to_obj_eq], standard]
  14.710 +  
  14.711 +(** cast - note, no arg for new length, as it's determined by type of result,
  14.712 +  thus in "cast w = w, the type means cast to length of w! **)
  14.713 +
  14.714 +lemma ucast_id: "ucast w = w"
  14.715 +  unfolding ucast_def by auto
  14.716 +
  14.717 +lemma scast_id: "scast w = w"
  14.718 +  unfolding scast_def by auto
  14.719 +
  14.720 +lemma ucast_bl: "ucast w == of_bl (to_bl w)"
  14.721 +  unfolding ucast_def of_bl_def uint_bl
  14.722 +  by (auto simp add : word_size)
  14.723 +
  14.724 +lemma nth_ucast: 
  14.725 +  "(ucast w::'a::len0 word) !! n = (w !! n & n < len_of TYPE('a))"
  14.726 +  apply (unfold ucast_def test_bit_bin)
  14.727 +  apply (simp add: word_ubin.eq_norm nth_bintr word_size) 
  14.728 +  apply (fast elim!: bin_nth_uint_imp)
  14.729 +  done
  14.730 +
  14.731 +(* for literal u(s)cast *)
  14.732 +
  14.733 +lemma ucast_bintr [simp]: 
  14.734 +  "ucast (number_of w ::'a::len0 word) = 
  14.735 +   number_of (bintrunc (len_of TYPE('a)) w)"
  14.736 +  unfolding ucast_def by simp
  14.737 +
  14.738 +lemma scast_sbintr [simp]: 
  14.739 +  "scast (number_of w ::'a::len word) = 
  14.740 +   number_of (sbintrunc (len_of TYPE('a) - Suc 0) w)"
  14.741 +  unfolding scast_def by simp
  14.742 +
  14.743 +lemmas source_size = source_size_def [unfolded Let_def word_size]
  14.744 +lemmas target_size = target_size_def [unfolded Let_def word_size]
  14.745 +lemmas is_down = is_down_def [unfolded source_size target_size]
  14.746 +lemmas is_up = is_up_def [unfolded source_size target_size]
  14.747 +
  14.748 +lemmas is_up_down = 
  14.749 +  trans [OF is_up [THEN meta_eq_to_obj_eq] 
  14.750 +            is_down [THEN meta_eq_to_obj_eq, symmetric], 
  14.751 +         standard]
  14.752 +
  14.753 +lemma down_cast_same': "uc = ucast ==> is_down uc ==> uc = scast"
  14.754 +  apply (unfold is_down)
  14.755 +  apply safe
  14.756 +  apply (rule ext)
  14.757 +  apply (unfold ucast_def scast_def uint_sint)
  14.758 +  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
  14.759 +  apply simp
  14.760 +  done
  14.761 +
  14.762 +lemma word_rev_tf': 
  14.763 +  "r = to_bl (of_bl bl) ==> r = rev (takefill False (length r) (rev bl))"
  14.764 +  unfolding of_bl_def uint_bl
  14.765 +  by (clarsimp simp add: bl_bin_bl_rtf word_ubin.eq_norm word_size)
  14.766 +
  14.767 +lemmas word_rev_tf = refl [THEN word_rev_tf', unfolded word_bl.Rep', standard]
  14.768 +
  14.769 +lemmas word_rep_drop = word_rev_tf [simplified takefill_alt,
  14.770 +  simplified, simplified rev_take, simplified]
  14.771 +
  14.772 +lemma to_bl_ucast: 
  14.773 +  "to_bl (ucast (w::'b::len0 word) ::'a::len0 word) = 
  14.774 +   replicate (len_of TYPE('a) - len_of TYPE('b)) False @
  14.775 +   drop (len_of TYPE('b) - len_of TYPE('a)) (to_bl w)"
  14.776 +  apply (unfold ucast_bl)
  14.777 +  apply (rule trans)
  14.778 +   apply (rule word_rep_drop)
  14.779 +  apply simp
  14.780 +  done
  14.781 +
  14.782 +lemma ucast_up_app': 
  14.783 +  "uc = ucast ==> source_size uc + n = target_size uc ==> 
  14.784 +    to_bl (uc w) = replicate n False @ (to_bl w)"
  14.785 +  apply (auto simp add : source_size target_size to_bl_ucast)
  14.786 +  apply (rule_tac f = "%n. replicate n False" in arg_cong)
  14.787 +  apply simp
  14.788 +  done
  14.789 +
  14.790 +lemma ucast_down_drop': 
  14.791 +  "uc = ucast ==> source_size uc = target_size uc + n ==> 
  14.792 +    to_bl (uc w) = drop n (to_bl w)"
  14.793 +  by (auto simp add : source_size target_size to_bl_ucast)
  14.794 +
  14.795 +lemma scast_down_drop': 
  14.796 +  "sc = scast ==> source_size sc = target_size sc + n ==> 
  14.797 +    to_bl (sc w) = drop n (to_bl w)"
  14.798 +  apply (subgoal_tac "sc = ucast")
  14.799 +   apply safe
  14.800 +   apply simp
  14.801 +   apply (erule refl [THEN ucast_down_drop'])
  14.802 +  apply (rule refl [THEN down_cast_same', symmetric])
  14.803 +  apply (simp add : source_size target_size is_down)
  14.804 +  done
  14.805 +
  14.806 +lemma sint_up_scast': 
  14.807 +  "sc = scast ==> is_up sc ==> sint (sc w) = sint w"
  14.808 +  apply (unfold is_up)
  14.809 +  apply safe
  14.810 +  apply (simp add: scast_def word_sbin.eq_norm)
  14.811 +  apply (rule box_equals)
  14.812 +    prefer 3
  14.813 +    apply (rule word_sbin.norm_Rep)
  14.814 +   apply (rule sbintrunc_sbintrunc_l)
  14.815 +   defer
  14.816 +   apply (subst word_sbin.norm_Rep)
  14.817 +   apply (rule refl)
  14.818 +  apply simp
  14.819 +  done
  14.820 +
  14.821 +lemma uint_up_ucast':
  14.822 +  "uc = ucast ==> is_up uc ==> uint (uc w) = uint w"
  14.823 +  apply (unfold is_up)
  14.824 +  apply safe
  14.825 +  apply (rule bin_eqI)
  14.826 +  apply (fold word_test_bit_def)
  14.827 +  apply (auto simp add: nth_ucast)
  14.828 +  apply (auto simp add: test_bit_bin)
  14.829 +  done
  14.830 +    
  14.831 +lemmas down_cast_same = refl [THEN down_cast_same']
  14.832 +lemmas ucast_up_app = refl [THEN ucast_up_app']
  14.833 +lemmas ucast_down_drop = refl [THEN ucast_down_drop']
  14.834 +lemmas scast_down_drop = refl [THEN scast_down_drop']
  14.835 +lemmas uint_up_ucast = refl [THEN uint_up_ucast']
  14.836 +lemmas sint_up_scast = refl [THEN sint_up_scast']
  14.837 +
  14.838 +lemma ucast_up_ucast': "uc = ucast ==> is_up uc ==> ucast (uc w) = ucast w"
  14.839 +  apply (simp (no_asm) add: ucast_def)
  14.840 +  apply (clarsimp simp add: uint_up_ucast)
  14.841 +  done
  14.842 +    
  14.843 +lemma scast_up_scast': "sc = scast ==> is_up sc ==> scast (sc w) = scast w"
  14.844 +  apply (simp (no_asm) add: scast_def)
  14.845 +  apply (clarsimp simp add: sint_up_scast)
  14.846 +  done
  14.847 +    
  14.848 +lemma ucast_of_bl_up': 
  14.849 +  "w = of_bl bl ==> size bl <= size w ==> ucast w = of_bl bl"
  14.850 +  by (auto simp add : nth_ucast word_size test_bit_of_bl intro!: word_eqI)
  14.851 +
  14.852 +lemmas ucast_up_ucast = refl [THEN ucast_up_ucast']
  14.853 +lemmas scast_up_scast = refl [THEN scast_up_scast']
  14.854 +lemmas ucast_of_bl_up = refl [THEN ucast_of_bl_up']
  14.855 +
  14.856 +lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id]
  14.857 +lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id]
  14.858 +
  14.859 +lemmas isduu = is_up_down [where c = "ucast", THEN iffD2]
  14.860 +lemmas isdus = is_up_down [where c = "scast", THEN iffD2]
  14.861 +lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id]
  14.862 +lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id]
  14.863 +
  14.864 +lemma up_ucast_surj:
  14.865 +  "is_up (ucast :: 'b::len0 word => 'a::len0 word) ==> 
  14.866 +   surj (ucast :: 'a word => 'b word)"
  14.867 +  by (rule surjI, erule ucast_up_ucast_id)
  14.868 +
  14.869 +lemma up_scast_surj:
  14.870 +  "is_up (scast :: 'b::len word => 'a::len word) ==> 
  14.871 +   surj (scast :: 'a word => 'b word)"
  14.872 +  by (rule surjI, erule scast_up_scast_id)
  14.873 +
  14.874 +lemma down_scast_inj:
  14.875 +  "is_down (scast :: 'b::len word => 'a::len word) ==> 
  14.876 +   inj_on (ucast :: 'a word => 'b word) A"
  14.877 +  by (rule inj_on_inverseI, erule scast_down_scast_id)
  14.878 +
  14.879 +lemma down_ucast_inj:
  14.880 +  "is_down (ucast :: 'b::len0 word => 'a::len0 word) ==> 
  14.881 +   inj_on (ucast :: 'a word => 'b word) A"
  14.882 +  by (rule inj_on_inverseI, erule ucast_down_ucast_id)
  14.883 +
  14.884 +lemma of_bl_append_same: "of_bl (X @ to_bl w) = w"
  14.885 +  by (rule word_bl.Rep_eqD) (simp add: word_rep_drop)
  14.886 +  
  14.887 +lemma ucast_down_no': 
  14.888 +  "uc = ucast ==> is_down uc ==> uc (number_of bin) = number_of bin"
  14.889 +  apply (unfold word_number_of_def is_down)
  14.890 +  apply (clarsimp simp add: ucast_def word_ubin.eq_norm)
  14.891 +  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
  14.892 +  apply (erule bintrunc_bintrunc_ge)
  14.893 +  done
  14.894 +    
  14.895 +lemmas ucast_down_no = ucast_down_no' [OF refl]
  14.896 +
  14.897 +lemma ucast_down_bl': "uc = ucast ==> is_down uc ==> uc (of_bl bl) = of_bl bl"
  14.898 +  unfolding of_bl_no by clarify (erule ucast_down_no)
  14.899 +    
  14.900 +lemmas ucast_down_bl = ucast_down_bl' [OF refl]
  14.901 +
  14.902 +lemmas slice_def' = slice_def [unfolded word_size]
  14.903 +lemmas test_bit_def' = word_test_bit_def [THEN meta_eq_to_obj_eq, THEN fun_cong]
  14.904 +
  14.905 +lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def
  14.906 +lemmas word_log_bin_defs = word_log_defs
  14.907 +
  14.908 +end
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/src/HOL/Word/WordExamples.thy	Mon Aug 20 04:34:31 2007 +0200
    15.3 @@ -0,0 +1,131 @@
    15.4 +(* 
    15.5 +  ID:     $Id$
    15.6 +  Author: Gerwin Klein, NICTA
    15.7 +
    15.8 +  Examples demonstrating and testing various word operations.
    15.9 +*)
   15.10 +
   15.11 +theory WordExamples imports WordMain
   15.12 +begin
   15.13 +
   15.14 +-- "modulus"
   15.15 +
   15.16 +lemma "(27 :: 4 word) = -5" by simp
   15.17 +
   15.18 +lemma "(27 :: 4 word) = 11" by simp
   15.19 +
   15.20 +lemma "27 \<noteq> (11 :: 6 word)" by simp
   15.21 +
   15.22 +-- "signed"
   15.23 +lemma "(127 :: 6 word) = -1" by simp
   15.24 +
   15.25 +-- "number ring simps"
   15.26 +lemma 
   15.27 +  "27 + 11 = (38::'a::len word)"
   15.28 +  "27 + 11 = (6::5 word)"
   15.29 +  "7 * 3 = (21::'a::len word)"
   15.30 +  "11 - 27 = (-16::'a::len word)"
   15.31 +  "- -11 = (11::'a::len word)"
   15.32 +  "-40 + 1 = (-39::'a::len word)"
   15.33 +  by simp_all
   15.34 +
   15.35 +lemma "word_pred 2 = 1" by simp
   15.36 +
   15.37 +lemma "word_succ -3 = -2" by simp
   15.38 +  
   15.39 +lemma "23 < (27::8 word)" by simp
   15.40 +lemma "23 \<le> (27::8 word)" by simp
   15.41 +lemma "\<not> 23 < (27::2 word)" by simp
   15.42 +lemma "0 < (4::3 word)" by simp
   15.43 +
   15.44 +-- "ring operations"
   15.45 +
   15.46 +lemma "a + 2 * b + c - b = (b + c) + (a :: 32 word)" by simp
   15.47 +
   15.48 +-- "casting"
   15.49 +
   15.50 +lemma "uint (234567 :: 10 word) = 71" by simp
   15.51 +lemma "uint (-234567 :: 10 word) = 953" by simp
   15.52 +lemma "sint (234567 :: 10 word) = 71" by simp
   15.53 +lemma "sint (-234567 :: 10 word) = -71" by simp
   15.54 +
   15.55 +lemma "unat (-234567 :: 10 word) = 953" by simp
   15.56 +
   15.57 +lemma "ucast (0b1010 :: 4 word) = (0b10 :: 2 word)" by simp
   15.58 +lemma "ucast (0b1010 :: 4 word) = (0b1010 :: 10 word)" by simp
   15.59 +lemma "scast (0b1010 :: 4 word) = (0b111010 :: 6 word)" by simp
   15.60 +
   15.61 +-- "reducing goals to nat or int and arith:"
   15.62 +lemma "i < x ==> i < (i + 1 :: 'a :: len word)" by unat_arith
   15.63 +lemma "i < x ==> i < (i + 1 :: 'a :: len word)" by uint_arith
   15.64 +
   15.65 +-- "bool lists"
   15.66 +
   15.67 +lemma "of_bl [True, False, True, True] = (0b1011::'a::len word)" by simp
   15.68 +
   15.69 +lemma "to_bl (0b110::4 word) = [False, True, True, False]" by simp
   15.70 +
   15.71 +-- "this is not exactly fast, but bearable"
   15.72 +lemma "of_bl (replicate 32 True) = (0xFFFFFFFF::32 word)" by simp
   15.73 +
   15.74 +-- "this works only for replicate n True"
   15.75 +lemma "of_bl (replicate 32 True) = (0xFFFFFFFF::32 word)"
   15.76 +  by (unfold mask_bl [symmetric]) (simp add: mask_def)
   15.77 +
   15.78 +
   15.79 +-- "bit operations"
   15.80 +
   15.81 +lemma "0b110 AND 0b101 = (0b100 :: 32 word)" by simp
   15.82 +
   15.83 +lemma "0b110 OR 0b011 = (0b111 :: 8 word)" by simp
   15.84 +
   15.85 +lemma "0xF0 XOR 0xFF = (0x0F :: byte)" by simp
   15.86 +
   15.87 +lemma "NOT (0xF0 :: 16 word) = 0xFF0F" by simp
   15.88 +
   15.89 +lemma "(-1 :: 32 word) = 0xFFFFFFFF" by simp
   15.90 +
   15.91 +lemma "(0b0010 :: 4 word) !! 1" by simp
   15.92 +lemma "\<not> (0b0010 :: 4 word) !! 0" by simp
   15.93 +lemma "\<not> (0b1000 :: 3 word) !! 4" by simp
   15.94 +
   15.95 +lemma "(0b11000 :: 10 word) !! n = (n = 4 \<or> n = 3)" 
   15.96 +  by (auto simp add: bin_nth_Bit)
   15.97 +
   15.98 +lemma "set_bit 55 7 True = (183::'a::len0 word)" by simp
   15.99 +lemma "set_bit 0b0010 7 True = (0b10000010::'a::len0 word)" by simp
  15.100 +lemma "set_bit 0b0010 1 False = (0::'a::len0 word)" by simp
  15.101 +
  15.102 +lemma "lsb (0b0101::'a::len word)" by simp
  15.103 +lemma "\<not> lsb (0b1000::'a::len word)" by simp
  15.104 +
  15.105 +lemma "\<not> msb (0b0101::4 word)" by simp
  15.106 +lemma   "msb (0b1000::4 word)" by simp
  15.107 +
  15.108 +lemma "word_cat (27::4 word) (27::8 word) = (2843::'a::len word)" by simp
  15.109 +lemma "word_cat (0b0011::4 word) (0b1111::6word) = (0b0011001111 :: 10 word)" 
  15.110 +  by simp
  15.111 +
  15.112 +lemma "0b1011 << 2 = (0b101100::'a::len0 word)" by simp
  15.113 +lemma "0b1011 >> 2 = (0b10::8 word)" by simp
  15.114 +lemma "0b1011 >>> 2 = (0b10::8 word)" by simp
  15.115 +
  15.116 +lemma "slice 3 (0b101111::6 word) = (0b101::3 word)" by simp
  15.117 +
  15.118 +lemma "word_rotr 2 0b0110 = (0b1001::4 word)" by simp
  15.119 +lemma "word_rotl 1 0b1110 = (0b1101::4 word)" by simp
  15.120 +lemma "word_roti 2 0b1110 = (0b1011::4 word)" by simp
  15.121 +lemma "word_roti -2 0b0110 = (0b1001::4 word)" by simp
  15.122 +
  15.123 +lemma "(x AND 0xff00) OR (x AND 0x00ff) = (x::16 word)"
  15.124 +proof -
  15.125 +  have "(x AND 0xff00) OR (x AND 0x00ff) = x AND (0xff00 OR 0x00ff)"
  15.126 +    by (simp only: word_ao_dist2)
  15.127 +  also have "0xff00 OR 0x00ff = (-1::16 word)"
  15.128 +    by simp
  15.129 +  also have "x AND -1 = x"
  15.130 +    by simp
  15.131 +  finally show ?thesis .
  15.132 +qed
  15.133 +
  15.134 +end
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/src/HOL/Word/WordGenLib.thy	Mon Aug 20 04:34:31 2007 +0200
    16.3 @@ -0,0 +1,452 @@
    16.4 +(* Author: Gerwin Klein, Jeremy Dawson
    16.5 +   $Id$
    16.6 +
    16.7 +   Miscellaneous additional library definitions and lemmas for 
    16.8 +   the word type. Instantiation to boolean algebras, definition
    16.9 +   of recursion and induction patterns for words.
   16.10 +*)
   16.11 +
   16.12 +theory WordGenLib imports WordShift Boolean_Algebra
   16.13 +begin
   16.14 +
   16.15 +declare of_nat_2p [simp]
   16.16 +
   16.17 +lemma word_int_cases:
   16.18 +  "\<lbrakk>\<And>n. \<lbrakk>(x ::'a::len0 word) = word_of_int n; 0 \<le> n; n < 2^len_of TYPE('a)\<rbrakk> \<Longrightarrow> P\<rbrakk>
   16.19 +   \<Longrightarrow> P"
   16.20 +  by (cases x rule: word_uint.Abs_cases) (simp add: uints_num)
   16.21 +
   16.22 +lemma word_nat_cases [cases type: word]:
   16.23 +  "\<lbrakk>\<And>n. \<lbrakk>(x ::'a::len word) = of_nat n; n < 2^len_of TYPE('a)\<rbrakk> \<Longrightarrow> P\<rbrakk>
   16.24 +   \<Longrightarrow> P"
   16.25 +  by (cases x rule: word_unat.Abs_cases) (simp add: unats_def)
   16.26 +
   16.27 +lemma max_word_eq:
   16.28 +  "(max_word::'a::len word) = 2^len_of TYPE('a) - 1"
   16.29 +  by (simp add: max_word_def word_of_int_hom_syms word_of_int_2p)
   16.30 +
   16.31 +lemma max_word_max [simp,intro!]:
   16.32 +  "n \<le> max_word"
   16.33 +  by (cases n rule: word_int_cases)
   16.34 +     (simp add: max_word_def word_le_def int_word_uint int_mod_eq')
   16.35 +  
   16.36 +lemma word_of_int_2p_len: 
   16.37 +  "word_of_int (2 ^ len_of TYPE('a)) = (0::'a::len0 word)"
   16.38 +  by (subst word_uint.Abs_norm [symmetric]) 
   16.39 +     (simp add: word_of_int_hom_syms)
   16.40 +
   16.41 +lemma word_pow_0:
   16.42 +  "(2::'a::len word) ^ len_of TYPE('a) = 0"
   16.43 +proof -
   16.44 +  have "word_of_int (2 ^ len_of TYPE('a)) = (0::'a word)"
   16.45 +    by (rule word_of_int_2p_len)
   16.46 +  thus ?thesis by (simp add: word_of_int_2p)
   16.47 +qed
   16.48 +
   16.49 +lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word"
   16.50 +  apply (simp add: max_word_eq)
   16.51 +  apply uint_arith
   16.52 +  apply auto
   16.53 +  apply (simp add: word_pow_0)
   16.54 +  done
   16.55 +
   16.56 +lemma max_word_minus: 
   16.57 +  "max_word = (-1::'a::len word)"
   16.58 +proof -
   16.59 +  have "-1 + 1 = (0::'a word)" by simp
   16.60 +  thus ?thesis by (rule max_word_wrap [symmetric])
   16.61 +qed
   16.62 +
   16.63 +lemma max_word_bl [simp]:
   16.64 +  "to_bl (max_word::'a::len word) = replicate (len_of TYPE('a)) True"
   16.65 +  by (subst max_word_minus to_bl_n1)+ simp
   16.66 +
   16.67 +lemma max_test_bit [simp]:
   16.68 +  "(max_word::'a::len word) !! n = (n < len_of TYPE('a))"
   16.69 +  by (auto simp add: test_bit_bl word_size)
   16.70 +
   16.71 +lemma word_and_max [simp]:
   16.72 +  "x AND max_word = x"
   16.73 +  by (rule word_eqI) (simp add: word_ops_nth_size word_size)
   16.74 +
   16.75 +lemma word_or_max [simp]:
   16.76 +  "x OR max_word = max_word"
   16.77 +  by (rule word_eqI) (simp add: word_ops_nth_size word_size)
   16.78 +
   16.79 +lemma word_ao_dist2:
   16.80 +  "x AND (y OR z) = x AND y OR x AND (z::'a::len0 word)"
   16.81 +  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
   16.82 +
   16.83 +lemma word_oa_dist2:
   16.84 +  "x OR y AND z = (x OR y) AND (x OR (z::'a::len0 word))"
   16.85 +  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
   16.86 +
   16.87 +lemma word_and_not [simp]:
   16.88 +  "x AND NOT x = (0::'a::len0 word)"
   16.89 +  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
   16.90 +
   16.91 +lemma word_or_not [simp]:
   16.92 +  "x OR NOT x = max_word"
   16.93 +  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
   16.94 +
   16.95 +lemma word_boolean:
   16.96 +  "boolean (op AND) (op OR) bitNOT 0 max_word"
   16.97 +  apply (rule boolean.intro)
   16.98 +           apply (rule word_bw_assocs)
   16.99 +          apply (rule word_bw_assocs)
  16.100 +         apply (rule word_bw_comms)
  16.101 +        apply (rule word_bw_comms)
  16.102 +       apply (rule word_ao_dist2)
  16.103 +      apply (rule word_oa_dist2)
  16.104 +     apply (rule word_and_max)
  16.105 +    apply (rule word_log_esimps)
  16.106 +   apply (rule word_and_not)
  16.107 +  apply (rule word_or_not)
  16.108 +  done
  16.109 +
  16.110 +interpretation word_bool_alg:
  16.111 +  boolean ["op AND" "op OR" bitNOT 0 max_word]
  16.112 +  by (rule word_boolean)
  16.113 +
  16.114 +lemma word_xor_and_or:
  16.115 +  "x XOR y = x AND NOT y OR NOT x AND (y::'a::len0 word)"
  16.116 +  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
  16.117 +
  16.118 +interpretation word_bool_alg:
  16.119 +  boolean_xor ["op AND" "op OR" bitNOT 0 max_word "op XOR"]
  16.120 +  apply (rule boolean_xor.intro)
  16.121 +   apply (rule word_boolean)
  16.122 +  apply (rule boolean_xor_axioms.intro)
  16.123 +  apply (rule word_xor_and_or)
  16.124 +  done
  16.125 +
  16.126 +lemma shiftr_0 [iff]:
  16.127 +  "(x::'a::len0 word) >> 0 = x"
  16.128 +  by (simp add: shiftr_bl)
  16.129 +
  16.130 +lemma shiftl_0 [simp]: 
  16.131 +  "(x :: 'a :: len word) << 0 = x"
  16.132 +  by (simp add: shiftl_t2n)
  16.133 +
  16.134 +lemma shiftl_1 [simp]:
  16.135 +  "(1::'a::len word) << n = 2^n"
  16.136 +  by (simp add: shiftl_t2n)
  16.137 +
  16.138 +lemma uint_lt_0 [simp]:
  16.139 +  "uint x < 0 = False"
  16.140 +  by (simp add: linorder_not_less)
  16.141 +
  16.142 +lemma shiftr1_1 [simp]: 
  16.143 +  "shiftr1 (1::'a::len word) = 0"
  16.144 +  by (simp add: shiftr1_def word_0_alt)
  16.145 +
  16.146 +lemma shiftr_1[simp]: 
  16.147 +  "(1::'a::len word) >> n = (if n = 0 then 1 else 0)"
  16.148 +  by (induct n) (auto simp: shiftr_def)
  16.149 +
  16.150 +lemma word_less_1 [simp]: 
  16.151 +  "((x::'a::len word) < 1) = (x = 0)"
  16.152 +  by (simp add: word_less_nat_alt unat_0_iff)
  16.153 +
  16.154 +lemma to_bl_mask:
  16.155 +  "to_bl (mask n :: 'a::len word) = 
  16.156 +  replicate (len_of TYPE('a) - n) False @ 
  16.157 +    replicate (min (len_of TYPE('a)) n) True"
  16.158 +  by (simp add: mask_bl word_rep_drop min_def)
  16.159 +
  16.160 +lemma map_replicate_True:
  16.161 +  "n = length xs ==>
  16.162 +    map (\<lambda>(x,y). x & y) (zip xs (replicate n True)) = xs"
  16.163 +  by (induct xs arbitrary: n) auto
  16.164 +
  16.165 +lemma map_replicate_False:
  16.166 +  "n = length xs ==> map (\<lambda>(x,y). x & y)
  16.167 +    (zip xs (replicate n False)) = replicate n False"
  16.168 +  by (induct xs arbitrary: n) auto
  16.169 +
  16.170 +lemma bl_and_mask:
  16.171 +  fixes w :: "'a::len word"
  16.172 +  fixes n
  16.173 +  defines "n' \<equiv> len_of TYPE('a) - n"
  16.174 +  shows "to_bl (w AND mask n) =  replicate n' False @ drop n' (to_bl w)"
  16.175 +proof - 
  16.176 +  note [simp] = map_replicate_True map_replicate_False
  16.177 +  have "to_bl (w AND mask n) = 
  16.178 +        app2 op & (to_bl w) (to_bl (mask n::'a::len word))"
  16.179 +    by (simp add: bl_word_and)
  16.180 +  also
  16.181 +  have "to_bl w = take n' (to_bl w) @ drop n' (to_bl w)" by simp
  16.182 +  also
  16.183 +  have "app2 op & \<dots> (to_bl (mask n::'a::len word)) = 
  16.184 +        replicate n' False @ drop n' (to_bl w)"
  16.185 +    unfolding to_bl_mask n'_def app2_def
  16.186 +    by (subst zip_append) auto
  16.187 +  finally
  16.188 +  show ?thesis .
  16.189 +qed
  16.190 +
  16.191 +lemma drop_rev_takefill:
  16.192 +  "length xs \<le> n ==>
  16.193 +    drop (n - length xs) (rev (takefill False n (rev xs))) = xs"
  16.194 +  by (simp add: takefill_alt rev_take)
  16.195 +
  16.196 +lemma map_nth_0 [simp]:
  16.197 +  "map (op !! (0::'a::len0 word)) xs = replicate (length xs) False"
  16.198 +  by (induct xs) auto
  16.199 +
  16.200 +lemma uint_plus_if_size:
  16.201 +  "uint (x + y) = 
  16.202 +  (if uint x + uint y < 2^size x then 
  16.203 +      uint x + uint y 
  16.204 +   else 
  16.205 +      uint x + uint y - 2^size x)" 
  16.206 +  by (simp add: word_arith_alts int_word_uint mod_add_if_z 
  16.207 +                word_size)
  16.208 +
  16.209 +lemma unat_plus_if_size:
  16.210 +  "unat (x + (y::'a::len word)) = 
  16.211 +  (if unat x + unat y < 2^size x then 
  16.212 +      unat x + unat y 
  16.213 +   else 
  16.214 +      unat x + unat y - 2^size x)" 
  16.215 +  apply (subst word_arith_nat_defs)
  16.216 +  apply (subst unat_of_nat)
  16.217 +  apply (simp add:  mod_nat_add word_size)
  16.218 +  done
  16.219 +
  16.220 +lemma word_neq_0_conv [simp]:
  16.221 +  fixes w :: "'a :: len word"
  16.222 +  shows "(w \<noteq> 0) = (0 < w)"
  16.223 +proof -
  16.224 +  have "0 \<le> w" by (rule word_zero_le)
  16.225 +  thus ?thesis by (auto simp add: word_less_def)
  16.226 +qed
  16.227 +
  16.228 +lemma max_lt:
  16.229 +  "unat (max a b div c) = unat (max a b) div unat (c:: 'a :: len word)"
  16.230 +  apply (subst word_arith_nat_defs)
  16.231 +  apply (subst word_unat.eq_norm)
  16.232 +  apply (subst mod_if)
  16.233 +  apply clarsimp
  16.234 +  apply (erule notE)
  16.235 +  apply (insert div_le_dividend [of "unat (max a b)" "unat c"])
  16.236 +  apply (erule order_le_less_trans)
  16.237 +  apply (insert unat_lt2p [of "max a b"])
  16.238 +  apply simp
  16.239 +  done
  16.240 +
  16.241 +lemma uint_sub_if_size:
  16.242 +  "uint (x - y) = 
  16.243 +  (if uint y \<le> uint x then 
  16.244 +      uint x - uint y 
  16.245 +   else 
  16.246 +      uint x - uint y + 2^size x)"
  16.247 +  by (simp add: word_arith_alts int_word_uint mod_sub_if_z 
  16.248 +                word_size)
  16.249 +
  16.250 +lemma unat_sub_simple:
  16.251 +  "x \<le> y ==> unat (y - x) = unat y - unat x"
  16.252 +  by (simp add: unat_def uint_sub_if_size word_le_def nat_diff_distrib)
  16.253 +
  16.254 +lemmas unat_sub = unat_sub_simple
  16.255 +
  16.256 +lemma word_less_sub1:
  16.257 +  fixes x :: "'a :: len word"
  16.258 +  shows "x \<noteq> 0 ==> 1 < x = (0 < x - 1)"
  16.259 +  by (simp add: unat_sub_if_size word_less_nat_alt)
  16.260 +
  16.261 +lemma word_le_sub1:
  16.262 +  fixes x :: "'a :: len word"
  16.263 +  shows "x \<noteq> 0 ==> 1 \<le> x = (0 \<le> x - 1)"
  16.264 +  by (simp add: unat_sub_if_size order_le_less word_less_nat_alt)
  16.265 +
  16.266 +lemmas word_less_sub1_numberof [simp] =
  16.267 +  word_less_sub1 [of "number_of ?w"]
  16.268 +lemmas word_le_sub1_numberof [simp] =
  16.269 +  word_le_sub1 [of "number_of ?w"]
  16.270 +  
  16.271 +lemma word_of_int_minus: 
  16.272 +  "word_of_int (2^len_of TYPE('a) - i) = (word_of_int (-i)::'a::len word)"
  16.273 +proof -
  16.274 +  have x: "2^len_of TYPE('a) - i = -i + 2^len_of TYPE('a)" by simp
  16.275 +  show ?thesis
  16.276 +    apply (subst x)
  16.277 +    apply (subst word_uint.Abs_norm [symmetric], subst zmod_zadd_self2)
  16.278 +    apply simp
  16.279 +    done
  16.280 +qed
  16.281 +  
  16.282 +lemmas word_of_int_inj = 
  16.283 +  word_uint.Abs_inject [unfolded uints_num, simplified]
  16.284 +
  16.285 +lemma word_le_less_eq:
  16.286 +  "(x ::'z::len word) \<le> y = (x = y \<or> x < y)"
  16.287 +  by (auto simp add: word_less_def)
  16.288 +
  16.289 +lemma mod_plus_cong:
  16.290 +  assumes 1: "(b::int) = b'"
  16.291 +      and 2: "x mod b' = x' mod b'"
  16.292 +      and 3: "y mod b' = y' mod b'"
  16.293 +      and 4: "x' + y' = z'"
  16.294 +  shows "(x + y) mod b = z' mod b'"
  16.295 +proof -
  16.296 +  from 1 2[symmetric] 3[symmetric] have "(x + y) mod b = (x' mod b' + y' mod b') mod b'"
  16.297 +    by (simp add: zmod_zadd1_eq[symmetric])
  16.298 +  also have "\<dots> = (x' + y') mod b'"
  16.299 +    by (simp add: zmod_zadd1_eq[symmetric])
  16.300 +  finally show ?thesis by (simp add: 4)
  16.301 +qed
  16.302 +
  16.303 +lemma mod_minus_cong:
  16.304 +  assumes 1: "(b::int) = b'"
  16.305 +      and 2: "x mod b' = x' mod b'"
  16.306 +      and 3: "y mod b' = y' mod b'"
  16.307 +      and 4: "x' - y' = z'"
  16.308 +  shows "(x - y) mod b = z' mod b'"
  16.309 +  using assms
  16.310 +  apply (subst zmod_zsub_left_eq)
  16.311 +  apply (subst zmod_zsub_right_eq)
  16.312 +  apply (simp add: zmod_zsub_left_eq [symmetric] zmod_zsub_right_eq [symmetric])
  16.313 +  done
  16.314 +
  16.315 +lemma word_induct_less: 
  16.316 +  "\<lbrakk>P (0::'a::len word); \<And>n. \<lbrakk>n < m; P n\<rbrakk> \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P m"
  16.317 +  apply (cases m)
  16.318 +  apply atomize
  16.319 +  apply (erule rev_mp)+
  16.320 +  apply (rule_tac x=m in spec)
  16.321 +  apply (induct_tac n)
  16.322 +   apply simp
  16.323 +  apply clarsimp
  16.324 +  apply (erule impE)
  16.325 +   apply clarsimp
  16.326 +   apply (erule_tac x=n in allE)
  16.327 +   apply (erule impE)
  16.328 +    apply (simp add: unat_arith_simps)
  16.329 +    apply (clarsimp simp: unat_of_nat)
  16.330 +   apply simp
  16.331 +  apply (erule_tac x="of_nat na" in allE)
  16.332 +  apply (erule impE)
  16.333 +   apply (simp add: unat_arith_simps)
  16.334 +   apply (clarsimp simp: unat_of_nat)
  16.335 +  apply simp
  16.336 +  done
  16.337 +  
  16.338 +lemma word_induct: 
  16.339 +  "\<lbrakk>P (0::'a::len word); \<And>n. P n \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P m"
  16.340 +  by (erule word_induct_less, simp)
  16.341 +
  16.342 +lemma word_induct2 [induct type]: 
  16.343 +  "\<lbrakk>P 0; \<And>n. \<lbrakk>1 + n \<noteq> 0; P n\<rbrakk> \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P (n::'b::len word)"
  16.344 +  apply (rule word_induct, simp)
  16.345 +  apply (case_tac "1+n = 0", auto)
  16.346 +  done
  16.347 +
  16.348 +constdefs
  16.349 +  word_rec :: "'a \<Rightarrow> ('b::len word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a"
  16.350 +  "word_rec forZero forSuc n \<equiv> nat_rec forZero (forSuc \<circ> of_nat) (unat n)"
  16.351 +
  16.352 +lemma word_rec_0: "word_rec z s 0 = z"
  16.353 +  by (simp add: word_rec_def)
  16.354 +
  16.355 +lemma word_rec_Suc: 
  16.356 +  "1 + n \<noteq> (0::'a::len word) \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)"
  16.357 +  apply (simp add: word_rec_def unat_word_ariths)
  16.358 +  apply (subst nat_mod_eq')
  16.359 +   apply (cut_tac x=n in unat_lt2p)
  16.360 +   apply (drule Suc_mono)
  16.361 +   apply (simp add: less_Suc_eq_le)
  16.362 +   apply (simp only: order_less_le, simp)
  16.363 +   apply (erule contrapos_pn, simp)
  16.364 +   apply (drule arg_cong[where f=of_nat])
  16.365 +   apply simp
  16.366 +   apply (subst (asm) word_unat.Rep_Abs_A.Rep_inverse[of n])
  16.367 +   apply simp
  16.368 +  apply simp
  16.369 +  done
  16.370 +
  16.371 +lemma word_rec_Pred: 
  16.372 +  "n \<noteq> 0 \<Longrightarrow> word_rec z s n = s (n - 1) (word_rec z s (n - 1))"
  16.373 +  apply (rule subst[where t="n" and s="1 + (n - 1)"])
  16.374 +   apply simp
  16.375 +  apply (subst word_rec_Suc)
  16.376 +   apply simp
  16.377 +  apply simp
  16.378 +  done
  16.379 +
  16.380 +lemma word_rec_in: 
  16.381 +  "f (word_rec z (\<lambda>_. f) n) = word_rec (f z) (\<lambda>_. f) n"
  16.382 +  by (induct n) (simp_all add: word_rec_0 word_rec_Suc)
  16.383 +
  16.384 +lemma word_rec_in2: 
  16.385 +  "f n (word_rec z f n) = word_rec (f 0 z) (f \<circ> op + 1) n"
  16.386 +  by (induct n) (simp_all add: word_rec_0 word_rec_Suc)
  16.387 +
  16.388 +lemma word_rec_twice: 
  16.389 +  "m \<le> n \<Longrightarrow> word_rec z f n = word_rec (word_rec z f (n - m)) (f \<circ> op + (n - m)) m"
  16.390 +apply (erule rev_mp)
  16.391 +apply (rule_tac x=z in spec)
  16.392 +apply (rule_tac x=f in spec)
  16.393 +apply (induct n)
  16.394 + apply (simp add: word_rec_0)
  16.395 +apply clarsimp
  16.396 +apply (rule_tac t="1 + n - m" and s="1 + (n - m)" in subst)
  16.397 + apply simp
  16.398 +apply (case_tac "1 + (n - m) = 0")
  16.399 + apply (simp add: word_rec_0)
  16.400 + apply (rule arg_cong[where f="word_rec ?a ?b"])
  16.401 + apply (rule_tac t="m" and s="m + (1 + (n - m))" in subst)
  16.402 +  apply simp
  16.403 + apply (simp (no_asm_use))
  16.404 +apply (simp add: word_rec_Suc word_rec_in2)
  16.405 +apply (erule impE)
  16.406 + apply uint_arith
  16.407 +apply (drule_tac x="x \<circ> op + 1" in spec)
  16.408 +apply (drule_tac x="x 0 xa" in spec)
  16.409 +apply simp
  16.410 +apply (rule_tac t="\<lambda>a. x (1 + (n - m + a))" and s="\<lambda>a. x (1 + (n - m) + a)"
  16.411 +       in subst)
  16.412 + apply (clarsimp simp add: expand_fun_eq)
  16.413 + apply (rule_tac t="(1 + (n - m + xb))" and s="1 + (n - m) + xb" in subst)
  16.414 +  apply simp
  16.415 + apply (rule refl)
  16.416 +apply (rule refl)
  16.417 +done
  16.418 +
  16.419 +lemma word_rec_id: "word_rec z (\<lambda>_. id) n = z"
  16.420 +  by (induct n) (auto simp add: word_rec_0 word_rec_Suc)
  16.421 +
  16.422 +lemma word_rec_id_eq: "\<forall>m < n. f m = id \<Longrightarrow> word_rec z f n = z"
  16.423 +apply (erule rev_mp)
  16.424 +apply (induct n)
  16.425 + apply (auto simp add: word_rec_0 word_rec_Suc)
  16.426 + apply (drule spec, erule mp)
  16.427 + apply uint_arith
  16.428 +apply (drule_tac x=n in spec, erule impE)
  16.429 + apply uint_arith
  16.430 +apply simp
  16.431 +done
  16.432 +
  16.433 +lemma word_rec_max: 
  16.434 +  "\<forall>m\<ge>n. m \<noteq> -1 \<longrightarrow> f m = id \<Longrightarrow> word_rec z f -1 = word_rec z f n"
  16.435 +apply (subst word_rec_twice[where n="-1" and m="-1 - n"])
  16.436 + apply simp
  16.437 +apply simp
  16.438 +apply (rule word_rec_id_eq)
  16.439 +apply clarsimp
  16.440 +apply (drule spec, rule mp, erule mp)
  16.441 + apply (rule word_plus_mono_right2[OF _ order_less_imp_le])
  16.442 +  prefer 2 
  16.443 +  apply assumption
  16.444 + apply simp
  16.445 +apply (erule contrapos_pn)
  16.446 +apply simp
  16.447 +apply (drule arg_cong[where f="\<lambda>x. x - n"])
  16.448 +apply simp
  16.449 +done
  16.450 +
  16.451 +lemma unatSuc: 
  16.452 +  "1 + n \<noteq> (0::'a::len word) \<Longrightarrow> unat (1 + n) = Suc (unat n)"
  16.453 +  by unat_arith
  16.454 +
  16.455 +end
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/src/HOL/Word/WordMain.thy	Mon Aug 20 04:34:31 2007 +0200
    17.3 @@ -0,0 +1,27 @@
    17.4 +(* 
    17.5 +  ID:     $Id$
    17.6 +  Author: Gerwin Klein, NICTA
    17.7 +
    17.8 +  The main interface of the word library to other theories.
    17.9 +*)
   17.10 +
   17.11 +theory WordMain imports WordGenLib
   17.12 +begin
   17.13 +
   17.14 +lemmas word_no_1 [simp] = word_1_no [symmetric]
   17.15 +lemmas word_no_0 [simp] = word_0_no [symmetric]
   17.16 +
   17.17 +declare word_0_bl [simp]
   17.18 +declare bin_to_bl_def [simp]
   17.19 +declare to_bl_0 [simp]
   17.20 +declare of_bl_True [simp]
   17.21 +
   17.22 +text "Examples"
   17.23 +
   17.24 +types word32 = "32 word"
   17.25 +types word8 = "8 word"
   17.26 +types byte = word8
   17.27 +
   17.28 +text {* for more see WordExampes.thy *}
   17.29 +
   17.30 +end
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/src/HOL/Word/WordShift.thy	Mon Aug 20 04:34:31 2007 +0200
    18.3 @@ -0,0 +1,1597 @@
    18.4 +(* 
    18.5 +    ID:         $Id$
    18.6 +    Author:     Jeremy Dawson and Gerwin Klein, NICTA
    18.7 +
    18.8 +  contains theorems to do with shifting, rotating, splitting words
    18.9 +*)
   18.10 +
   18.11 +theory WordShift imports WordBitwise begin
   18.12 +
   18.13 +section "Bit shifting"
   18.14 +
   18.15 +lemma shiftl1_number [simp] :
   18.16 +  "shiftl1 (number_of w) = number_of (w BIT bit.B0)"
   18.17 +  apply (unfold shiftl1_def word_number_of_def)
   18.18 +  apply (simp add: word_ubin.norm_eq_iff [symmetric] word_ubin.eq_norm)
   18.19 +  apply (subst refl [THEN bintrunc_BIT_I, symmetric])
   18.20 +  apply (subst bintrunc_bintrunc_min)
   18.21 +  apply simp
   18.22 +  done
   18.23 +
   18.24 +lemma shiftl1_0 [simp] : "shiftl1 0 = 0"
   18.25 +  unfolding word_0_no shiftl1_number by auto
   18.26 +
   18.27 +lemmas shiftl1_def_u = shiftl1_def [folded word_number_of_def]
   18.28 +
   18.29 +lemma shiftl1_def_s: "shiftl1 w = number_of (sint w BIT bit.B0)"
   18.30 +  by (rule trans [OF _ shiftl1_number]) simp
   18.31 +
   18.32 +lemma shiftr1_0 [simp] : "shiftr1 0 = 0"
   18.33 +  unfolding shiftr1_def 
   18.34 +  by simp (simp add: word_0_wi)
   18.35 +
   18.36 +lemma sshiftr1_0 [simp] : "sshiftr1 0 = 0"
   18.37 +  apply (unfold sshiftr1_def)
   18.38 +  apply simp
   18.39 +  apply (simp add : word_0_wi)
   18.40 +  done
   18.41 +
   18.42 +lemma sshiftr1_n1 [simp] : "sshiftr1 -1 = -1"
   18.43 +  unfolding sshiftr1_def by auto
   18.44 +
   18.45 +lemma shiftl_0 [simp] : "(0::'a::len0 word) << n = 0"
   18.46 +  unfolding shiftl_def by (induct n) auto
   18.47 +
   18.48 +lemma shiftr_0 [simp] : "(0::'a::len0 word) >> n = 0"
   18.49 +  unfolding shiftr_def by (induct n) auto
   18.50 +
   18.51 +lemma sshiftr_0 [simp] : "0 >>> n = 0"
   18.52 +  unfolding sshiftr_def by (induct n) auto
   18.53 +
   18.54 +lemma sshiftr_n1 [simp] : "-1 >>> n = -1"
   18.55 +  unfolding sshiftr_def by (induct n) auto
   18.56 +
   18.57 +lemma nth_shiftl1: "shiftl1 w !! n = (n < size w & n > 0 & w !! (n - 1))"
   18.58 +  apply (unfold shiftl1_def word_test_bit_def)
   18.59 +  apply (simp add: nth_bintr word_ubin.eq_norm word_size)
   18.60 +  apply (cases n)
   18.61 +   apply auto
   18.62 +  done
   18.63 +
   18.64 +lemma nth_shiftl' [rule_format]:
   18.65 +  "ALL n. ((w::'a::len0 word) << m) !! n = (n < size w & n >= m & w !! (n - m))"
   18.66 +  apply (unfold shiftl_def)
   18.67 +  apply (induct "m")
   18.68 +   apply (force elim!: test_bit_size)
   18.69 +  apply (clarsimp simp add : nth_shiftl1 word_size)
   18.70 +  apply arith
   18.71 +  done
   18.72 +
   18.73 +lemmas nth_shiftl = nth_shiftl' [unfolded word_size] 
   18.74 +
   18.75 +lemma nth_shiftr1: "shiftr1 w !! n = w !! Suc n"
   18.76 +  apply (unfold shiftr1_def word_test_bit_def)
   18.77 +  apply (simp add: nth_bintr word_ubin.eq_norm)
   18.78 +  apply safe
   18.79 +  apply (drule bin_nth.Suc [THEN iffD2, THEN bin_nth_uint_imp])
   18.80 +  apply simp
   18.81 +  done
   18.82 +
   18.83 +lemma nth_shiftr: 
   18.84 +  "\<And>n. ((w::'a::len0 word) >> m) !! n = w !! (n + m)"
   18.85 +  apply (unfold shiftr_def)
   18.86 +  apply (induct "m")
   18.87 +   apply (auto simp add : nth_shiftr1)
   18.88 +  done
   18.89 +   
   18.90 +(* see paper page 10, (1), (2), shiftr1_def is of the form of (1),
   18.91 +  where f (ie bin_rest) takes normal arguments to normal results,
   18.92 +  thus we get (2) from (1) *)
   18.93 +
   18.94 +lemma uint_shiftr1: "uint (shiftr1 w) = bin_rest (uint w)" 
   18.95 +  apply (unfold shiftr1_def word_ubin.eq_norm bin_rest_trunc_i)
   18.96 +  apply (subst bintr_uint [symmetric, OF order_refl])
   18.97 +  apply (simp only : bintrunc_bintrunc_l)
   18.98 +  apply simp 
   18.99 +  done
  18.100 +
  18.101 +lemma nth_sshiftr1: 
  18.102 +  "sshiftr1 w !! n = (if n = size w - 1 then w !! n else w !! Suc n)"
  18.103 +  apply (unfold sshiftr1_def word_test_bit_def)
  18.104 +  apply (simp add: nth_bintr word_ubin.eq_norm
  18.105 +                   bin_nth.Suc [symmetric] word_size 
  18.106 +             del: bin_nth.simps)
  18.107 +  apply (simp add: nth_bintr uint_sint del : bin_nth.simps)
  18.108 +  apply (auto simp add: bin_nth_sint)
  18.109 +  done
  18.110 +
  18.111 +lemma nth_sshiftr [rule_format] : 
  18.112 +  "ALL n. sshiftr w m !! n = (n < size w & 
  18.113 +    (if n + m >= size w then w !! (size w - 1) else w !! (n + m)))"
  18.114 +  apply (unfold sshiftr_def)
  18.115 +  apply (induct_tac "m")
  18.116 +   apply (simp add: test_bit_bl)
  18.117 +  apply (clarsimp simp add: nth_sshiftr1 word_size)
  18.118 +  apply safe
  18.119 +       apply arith
  18.120 +      apply arith
  18.121 +     apply (erule thin_rl)
  18.122 +     apply (case_tac n)
  18.123 +      apply safe
  18.124 +      apply simp
  18.125 +     apply simp
  18.126 +    apply (erule thin_rl)
  18.127 +    apply (case_tac n)
  18.128 +     apply safe
  18.129 +     apply simp
  18.130 +    apply simp
  18.131 +   apply arith+
  18.132 +  done
  18.133 +    
  18.134 +lemma shiftr1_div_2: "uint (shiftr1 w) = uint w div 2"
  18.135 +  apply (unfold shiftr1_def bin_rest_div)
  18.136 +  apply (rule word_uint.Abs_inverse)
  18.137 +  apply (simp add: uints_num pos_imp_zdiv_nonneg_iff)
  18.138 +  apply (rule xtr7)
  18.139 +   prefer 2
  18.140 +   apply (rule zdiv_le_dividend)
  18.141 +    apply auto
  18.142 +  done
  18.143 +
  18.144 +lemma sshiftr1_div_2: "sint (sshiftr1 w) = sint w div 2"
  18.145 +  apply (unfold sshiftr1_def bin_rest_div [symmetric])
  18.146 +  apply (simp add: word_sbin.eq_norm)
  18.147 +  apply (rule trans)
  18.148 +   defer
  18.149 +   apply (subst word_sbin.norm_Rep [symmetric])
  18.150 +   apply (rule refl)
  18.151 +  apply (subst word_sbin.norm_Rep [symmetric])
  18.152 +  apply (unfold One_nat_def)
  18.153 +  apply (rule sbintrunc_rest)
  18.154 +  done
  18.155 +
  18.156 +lemma shiftr_div_2n: "uint (shiftr w n) = uint w div 2 ^ n"
  18.157 +  apply (unfold shiftr_def)
  18.158 +  apply (induct "n")
  18.159 +   apply simp
  18.160 +  apply (simp add: shiftr1_div_2 mult_commute
  18.161 +                   zdiv_zmult2_eq [symmetric])
  18.162 +  done
  18.163 +
  18.164 +lemma sshiftr_div_2n: "sint (sshiftr w n) = sint w div 2 ^ n"
  18.165 +  apply (unfold sshiftr_def)
  18.166 +  apply (induct "n")
  18.167 +   apply simp
  18.168 +  apply (simp add: sshiftr1_div_2 mult_commute
  18.169 +                   zdiv_zmult2_eq [symmetric])
  18.170 +  done
  18.171 +
  18.172 +subsection "shift functions in terms of lists of bools"
  18.173 +
  18.174 +lemmas bshiftr1_no_bin [simp] = 
  18.175 +  bshiftr1_def [where w="number_of ?w", unfolded to_bl_no_bin]
  18.176 +
  18.177 +lemma bshiftr1_bl: "to_bl (bshiftr1 b w) = b # butlast (to_bl w)"
  18.178 +  unfolding bshiftr1_def by (rule word_bl.Abs_inverse) simp
  18.179 +
  18.180 +lemma shiftl1_of_bl: "shiftl1 (of_bl bl) = of_bl (bl @ [False])"
  18.181 +  unfolding uint_bl of_bl_no 
  18.182 +  by (simp add: bl_to_bin_aux_append bl_to_bin_def)
  18.183 +
  18.184 +lemmas shiftl1_bl = shiftl1_of_bl 
  18.185 +  [where bl = "to_bl (?w :: ?'a :: len0 word)", simplified]
  18.186 +
  18.187 +lemma bl_shiftl1:
  18.188 +  "to_bl (shiftl1 (w :: 'a :: len word)) = tl (to_bl w) @ [False]"
  18.189 +  apply (simp add: shiftl1_bl word_rep_drop drop_Suc drop_Cons')
  18.190 +  apply (fast intro!: Suc_leI)
  18.191 +  done
  18.192 +
  18.193 +lemma shiftr1_bl: "shiftr1 w = of_bl (butlast (to_bl w))"
  18.194 +  apply (unfold shiftr1_def uint_bl of_bl_def)
  18.195 +  apply (simp add: butlast_rest_bin word_size)
  18.196 +  apply (simp add: bin_rest_trunc [symmetric, unfolded One_nat_def])
  18.197 +  done
  18.198 +
  18.199 +lemma bl_shiftr1: 
  18.200 +  "to_bl (shiftr1 (w :: 'a :: len word)) = False # butlast (to_bl w)"
  18.201 +  unfolding shiftr1_bl
  18.202 +  by (simp add : word_rep_drop len_gt_0 [THEN Suc_leI])
  18.203 +
  18.204 +
  18.205 +(* relate the two above : TODO - remove the :: len restriction on
  18.206 +  this theorem and others depending on it *)
  18.207 +lemma shiftl1_rev: 
  18.208 +  "shiftl1 (w :: 'a :: len word) = word_reverse (shiftr1 (word_reverse w))"
  18.209 +  apply (unfold word_reverse_def)
  18.210 +  apply (rule word_bl.Rep_inverse' [symmetric])
  18.211 +  apply (simp add: bl_shiftl1 bl_shiftr1 word_bl.Abs_inverse)
  18.212 +  apply (cases "to_bl w")
  18.213 +   apply auto
  18.214 +  done
  18.215 +
  18.216 +lemma shiftl_rev: 
  18.217 +  "shiftl (w :: 'a :: len word) n = word_reverse (shiftr (word_reverse w) n)"
  18.218 +  apply (unfold shiftl_def shiftr_def)
  18.219 +  apply (induct "n")
  18.220 +   apply (auto simp add : shiftl1_rev)
  18.221 +  done
  18.222 +
  18.223 +lemmas rev_shiftl =
  18.224 +  shiftl_rev [where w = "word_reverse ?w1", simplified, standard]
  18.225 +
  18.226 +lemmas shiftr_rev = rev_shiftl [THEN word_rev_gal', standard]
  18.227 +lemmas rev_shiftr = shiftl_rev [THEN word_rev_gal', standard]
  18.228 +
  18.229 +lemma bl_sshiftr1:
  18.230 +  "to_bl (sshiftr1 (w :: 'a :: len word)) = hd (to_bl w) # butlast (to_bl w)"
  18.231 +  apply (unfold sshiftr1_def uint_bl word_size)
  18.232 +  apply (simp add: butlast_rest_bin word_ubin.eq_norm)
  18.233 +  apply (simp add: sint_uint)
  18.234 +  apply (rule nth_equalityI)
  18.235 +   apply clarsimp
  18.236 +  apply clarsimp
  18.237 +  apply (case_tac i)
  18.238 +   apply (simp_all add: hd_conv_nth length_0_conv [symmetric]
  18.239 +                        nth_bin_to_bl bin_nth.Suc [symmetric] 
  18.240 +                        nth_sbintr 
  18.241 +                   del: bin_nth.Suc)
  18.242 +   apply force
  18.243 +  apply (rule impI)
  18.244 +  apply (rule_tac f = "bin_nth (uint w)" in arg_cong)
  18.245 +  apply simp
  18.246 +  done
  18.247 +
  18.248 +lemma drop_shiftr: 
  18.249 +  "drop n (to_bl ((w :: 'a :: len word) >> n)) = take (size w - n) (to_bl w)" 
  18.250 +  apply (unfold shiftr_def)
  18.251 +  apply (induct n)
  18.252 +   prefer 2
  18.253 +   apply (simp add: drop_Suc bl_shiftr1 butlast_drop [symmetric])
  18.254 +   apply (rule butlast_take [THEN trans])
  18.255 +  apply (auto simp: word_size)
  18.256 +  done
  18.257 +
  18.258 +lemma drop_sshiftr: 
  18.259 +  "drop n (to_bl ((w :: 'a :: len word) >>> n)) = take (size w - n) (to_bl w)"
  18.260 +  apply (unfold sshiftr_def)
  18.261 +  apply (induct n)
  18.262 +   prefer 2
  18.263 +   apply (simp add: drop_Suc bl_sshiftr1 butlast_drop [symmetric])
  18.264 +   apply (rule butlast_take [THEN trans])
  18.265 +  apply (auto simp: word_size)
  18.266 +  done
  18.267 +
  18.268 +lemma take_shiftr [rule_format] :
  18.269 +  "n <= size (w :: 'a :: len word) --> take n (to_bl (w >> n)) = 
  18.270 +    replicate n False" 
  18.271 +  apply (unfold shiftr_def)
  18.272 +  apply (induct n)
  18.273 +   prefer 2
  18.274 +   apply (simp add: bl_shiftr1)
  18.275 +   apply (rule impI)
  18.276 +   apply (rule take_butlast [THEN trans])
  18.277 +  apply (auto simp: word_size)
  18.278 +  done
  18.279 +
  18.280 +lemma take_sshiftr' [rule_format] :
  18.281 +  "n <= size (w :: 'a :: len word) --> hd (to_bl (w >>> n)) = hd (to_bl w) & 
  18.282 +    take n (to_bl (w >>> n)) = replicate n (hd (to_bl w))" 
  18.283 +  apply (unfold sshiftr_def)
  18.284 +  apply (induct n)
  18.285 +   prefer 2
  18.286 +   apply (simp add: bl_sshiftr1)
  18.287 +   apply (rule impI)
  18.288 +   apply (rule take_butlast [THEN trans])
  18.289 +  apply (auto simp: word_size)
  18.290 +  done
  18.291 +
  18.292 +lemmas hd_sshiftr = take_sshiftr' [THEN conjunct1, standard]
  18.293 +lemmas take_sshiftr = take_sshiftr' [THEN conjunct2, standard]
  18.294 +
  18.295 +lemma atd_lem: "take n xs = t ==> drop n xs = d ==> xs = t @ d"
  18.296 +  by (auto intro: append_take_drop_id [symmetric])
  18.297 +
  18.298 +lemmas bl_shiftr = atd_lem [OF take_shiftr drop_shiftr]
  18.299 +lemmas bl_sshiftr = atd_lem [OF take_sshiftr drop_sshiftr]
  18.300 +
  18.301 +lemma shiftl_of_bl: "of_bl bl << n = of_bl (bl @ replicate n False)"
  18.302 +  unfolding shiftl_def
  18.303 +  by (induct n) (auto simp: shiftl1_of_bl replicate_app_Cons_same)
  18.304 +
  18.305 +lemmas shiftl_bl =
  18.306 +  shiftl_of_bl [where bl = "to_bl (?w :: ?'a :: len0 word)", simplified]
  18.307 +
  18.308 +lemmas shiftl_number [simp] = shiftl_def [where w="number_of ?w"]
  18.309 +
  18.310 +lemma bl_shiftl:
  18.311 +  "to_bl (w << n) = drop n (to_bl w) @ replicate (min (size w) n) False"
  18.312 +  by (simp add: shiftl_bl word_rep_drop word_size min_def)
  18.313 +
  18.314 +lemma shiftl_zero_size: 
  18.315 +  fixes x :: "'a::len0 word"
  18.316 +  shows "size x <= n ==> x << n = 0"
  18.317 +  apply (unfold word_size)
  18.318 +  apply (rule word_eqI)
  18.319 +  apply (clarsimp simp add: shiftl_bl word_size test_bit_of_bl nth_append)
  18.320 +  done
  18.321 +
  18.322 +(* note - the following results use 'a :: len word < number_ring *)
  18.323 +
  18.324 +lemma shiftl1_2t: "shiftl1 (w :: 'a :: len word) = 2 * w"
  18.325 +  apply (simp add: shiftl1_def_u)
  18.326 +  apply (simp only:  double_number_of_BIT [symmetric])
  18.327 +  apply simp
  18.328 +  done
  18.329 +
  18.330 +lemma shiftl1_p: "shiftl1 (w :: 'a :: len word) = w + w"
  18.331 +  apply (simp add: shiftl1_def_u)
  18.332 +  apply (simp only: double_number_of_BIT [symmetric])
  18.333 +  apply simp
  18.334 +  done
  18.335 +
  18.336 +lemma shiftl_t2n: "shiftl (w :: 'a :: len word) n = 2 ^ n * w"
  18.337 +  unfolding shiftl_def 
  18.338 +  by (induct n) (auto simp: shiftl1_2t power_Suc)
  18.339 +
  18.340 +lemma shiftr1_bintr [simp]:
  18.341 +  "(shiftr1 (number_of w) :: 'a :: len0 word) = 
  18.342 +    number_of (bin_rest (bintrunc (len_of TYPE ('a)) w))" 
  18.343 +  unfolding shiftr1_def word_number_of_def
  18.344 +  by (simp add : word_ubin.eq_norm)
  18.345 +
  18.346 +lemma sshiftr1_sbintr [simp] :
  18.347 +  "(sshiftr1 (number_of w) :: 'a :: len word) = 
  18.348 +    number_of (bin_rest (sbintrunc (len_of TYPE ('a) - 1) w))" 
  18.349 +  unfolding sshiftr1_def word_number_of_def
  18.350 +  by (simp add : word_sbin.eq_norm)
  18.351 +
  18.352 +lemma shiftr_no': 
  18.353 +  "w = number_of bin ==> 
  18.354 +  (w::'a::len0 word) >> n = number_of ((bin_rest ^ n) (bintrunc (size w) bin))"
  18.355 +  apply clarsimp
  18.356 +  apply (rule word_eqI)
  18.357 +  apply (auto simp: nth_shiftr nth_rest_power_bin nth_bintr word_size)
  18.358 +  done
  18.359 +
  18.360 +lemma sshiftr_no': 
  18.361 +  "w = number_of bin ==> w >>> n = number_of ((bin_rest ^ n) 
  18.362 +    (sbintrunc (size w - 1) bin))"
  18.363 +  apply clarsimp
  18.364 +  apply (rule word_eqI)
  18.365 +  apply (auto simp: nth_sshiftr nth_rest_power_bin nth_sbintr word_size)
  18.366 +   apply (subgoal_tac "na + n = len_of TYPE('a) - Suc 0", simp, simp)+
  18.367 +  done
  18.368 +
  18.369 +lemmas sshiftr_no [simp] = 
  18.370 +  sshiftr_no' [where w = "number_of ?w", OF refl, unfolded word_size]
  18.371 +
  18.372 +lemmas shiftr_no [simp] = 
  18.373 +  shiftr_no' [where w = "number_of ?w", OF refl, unfolded word_size]
  18.374 +
  18.375 +lemma shiftr1_bl_of': 
  18.376 +  "us = shiftr1 (of_bl bl) ==> length bl <= size us ==> 
  18.377 +    us = of_bl (butlast bl)"
  18.378 +  by (clarsimp simp: shiftr1_def of_bl_def word_size butlast_rest_bl2bin 
  18.379 +                     word_ubin.eq_norm trunc_bl2bin)
  18.380 +
  18.381 +lemmas shiftr1_bl_of = refl [THEN shiftr1_bl_of', unfolded word_size]
  18.382 +
  18.383 +lemma shiftr_bl_of' [rule_format]: 
  18.384 +  "us = of_bl bl >> n ==> length bl <= size us --> 
  18.385 +   us = of_bl (take (length bl - n) bl)"
  18.386 +  apply (unfold shiftr_def)
  18.387 +  apply hypsubst
  18.388 +  apply (unfold word_size)
  18.389 +  apply (induct n)
  18.390 +   apply clarsimp
  18.391 +  apply clarsimp
  18.392 +  apply (subst shiftr1_bl_of)
  18.393 +   apply simp
  18.394 +  apply (simp add: butlast_take)
  18.395 +  done
  18.396 +
  18.397 +lemmas shiftr_bl_of = refl [THEN shiftr_bl_of', unfolded word_size]
  18.398 +
  18.399 +lemmas shiftr_bl = word_bl.Rep' [THEN eq_imp_le, THEN shiftr_bl_of,
  18.400 +  simplified word_size, simplified, THEN eq_reflection, standard]
  18.401 +
  18.402 +lemma msb_shift': "msb (w::'a::len word) <-> (w >> (size w - 1)) ~= 0"
  18.403 +  apply (unfold shiftr_bl word_msb_alt)
  18.404 +  apply (simp add: word_size Suc_le_eq take_Suc)
  18.405 +  apply (cases "hd (to_bl w)")
  18.406 +   apply (auto simp: word_1_bl word_0_bl 
  18.407 +                     of_bl_rep_False [where n=1 and bs="[]", simplified])
  18.408 +  done
  18.409 +
  18.410 +lemmas msb_shift = msb_shift' [unfolded word_size]
  18.411 +
  18.412 +lemma align_lem_or [rule_format] :
  18.413 +  "ALL x m. length x = n + m --> length y = n + m --> 
  18.414 +    drop m x = replicate n False --> take m y = replicate m False --> 
  18.415 +    app2 op | x y = take m x @ drop m y"
  18.416 +  apply (induct_tac y)
  18.417 +   apply force
  18.418 +  apply clarsimp
  18.419 +  apply (case_tac x, force)
  18.420 +  apply (case_tac m, auto)
  18.421 +  apply (drule sym)
  18.422 +  apply auto
  18.423 +  apply (induct_tac list, auto)
  18.424 +  done
  18.425 +
  18.426 +lemma align_lem_and [rule_format] :
  18.427 +  "ALL x m. length x = n + m --> length y = n + m --> 
  18.428 +    drop m x = replicate n False --> take m y = replicate m False --> 
  18.429 +    app2 op & x y = replicate (n + m) False"
  18.430 +  apply (induct_tac y)
  18.431 +   apply force
  18.432 +  apply clarsimp
  18.433 +  apply (case_tac x, force)
  18.434 +  apply (case_tac m, auto)
  18.435 +  apply (drule sym)
  18.436 +  apply auto
  18.437 +  apply (induct_tac list, auto)
  18.438 +  done
  18.439 +
  18.440 +lemma aligned_bl_add_size':
  18.441 +  "size x - n = m ==> n <= size x ==> drop m (to_bl x) = replicate n False ==>
  18.442 +    take m (to_bl y) = replicate m False ==> 
  18.443 +    to_bl (x + y) = take m (to_bl x) @ drop m (to_bl y)"
  18.444 +  apply (subgoal_tac "x AND y = 0")
  18.445 +   prefer 2
  18.446 +   apply (rule word_bl.Rep_eqD)
  18.447 +   apply (simp add: bl_word_and to_bl_0)
  18.448 +   apply (rule align_lem_and [THEN trans])
  18.449 +       apply (simp_all add: word_size)[5]
  18.450 +   apply (rule_tac f = "%n. replicate n False" in arg_cong)
  18.451 +   apply simp
  18.452 +  apply (subst word_plus_and_or [symmetric])
  18.453 +  apply (simp add : bl_word_or)
  18.454 +  apply (rule align_lem_or)
  18.455 +     apply (simp_all add: word_size)
  18.456 +  done
  18.457 +
  18.458 +lemmas aligned_bl_add_size = refl [THEN aligned_bl_add_size']
  18.459 +
  18.460 +subsection "Mask"
  18.461 +
  18.462 +lemma nth_mask': "m = mask n ==> test_bit m i = (i < n & i < size m)"
  18.463 +  apply (unfold mask_def test_bit_bl)
  18.464 +  apply (simp only: word_1_bl [symmetric] shiftl_of_bl)
  18.465 +  apply (clarsimp simp add: word_size)
  18.466 +  apply (simp only: of_bl_no mask_lem number_of_succ add_diff_cancel2)
  18.467 +  apply (fold of_bl_no)
  18.468 +  apply (simp add: word_1_bl)
  18.469 +  apply (rule test_bit_of_bl [THEN trans, unfolded test_bit_bl word_size])
  18.470 +  apply auto
  18.471 +  done
  18.472 +
  18.473 +lemmas nth_mask [simp] = refl [THEN nth_mask']
  18.474 +
  18.475 +lemma mask_bl: "mask n = of_bl (replicate n True)"
  18.476 +  by (auto simp add : test_bit_of_bl word_size intro: word_eqI)
  18.477 +
  18.478 +lemma mask_bin: "mask n = number_of (bintrunc n Numeral.Min)"
  18.479 +  by (auto simp add: nth_bintr word_size intro: word_eqI)
  18.480 +
  18.481 +lemma and_mask_bintr: "w AND mask n = number_of (bintrunc n (uint w))"
  18.482 +  apply (rule word_eqI)
  18.483 +  apply (simp add: nth_bintr word_size word_ops_nth_size)
  18.484 +  apply (auto simp add: test_bit_bin)
  18.485 +  done
  18.486 +
  18.487 +lemma and_mask_no: "number_of i AND mask n = number_of (bintrunc n i)" 
  18.488 +  by (auto simp add : nth_bintr word_size word_ops_nth_size intro: word_eqI)
  18.489 +
  18.490 +lemmas and_mask_wi = and_mask_no [unfolded word_number_of_def] 
  18.491 +
  18.492 +lemma bl_and_mask:
  18.493 +  "to_bl (w AND mask n :: 'a :: len word) = 
  18.494 +    replicate (len_of TYPE('a) - n) False @ 
  18.495 +    drop (len_of TYPE('a) - n) (to_bl w)"
  18.496 +  apply (rule nth_equalityI)
  18.497 +   apply simp
  18.498 +  apply (clarsimp simp add: to_bl_nth word_size)
  18.499 +  apply (simp add: word_size word_ops_nth_size)
  18.500 +  apply (auto simp add: word_size test_bit_bl nth_append nth_rev)
  18.501 +  done
  18.502 +
  18.503 +lemmas and_mask_mod_2p = 
  18.504 +  and_mask_bintr [unfolded word_number_of_alt no_bintr_alt]
  18.505 +
  18.506 +lemma and_mask_lt_2p: "uint (w AND mask n) < 2 ^ n"
  18.507 +  apply (simp add : and_mask_bintr no_bintr_alt)
  18.508 +  apply (rule xtr8)
  18.509 +   prefer 2
  18.510 +   apply (rule pos_mod_bound)
  18.511 +  apply auto
  18.512 +  done
  18.513 +
  18.514 +lemmas eq_mod_iff = trans [symmetric, OF int_mod_lem eq_sym_conv]
  18.515 +
  18.516 +lemma mask_eq_iff: "(w AND mask n) = w <-> uint w < 2 ^ n"
  18.517 +  apply (simp add: and_mask_bintr word_number_of_def)
  18.518 +  apply (simp add: word_ubin.inverse_norm)
  18.519 +  apply (simp add: eq_mod_iff bintrunc_mod2p min_def)
  18.520 +  apply (fast intro!: lt2p_lem)
  18.521 +  done
  18.522 +
  18.523 +lemma and_mask_dvd: "2 ^ n dvd uint w = (w AND mask n = 0)"
  18.524 +  apply (simp add: zdvd_iff_zmod_eq_0 and_mask_mod_2p)
  18.525 +  apply (simp add: word_uint.norm_eq_iff [symmetric] word_of_int_homs)
  18.526 +  apply (subst word_uint.norm_Rep [symmetric])
  18.527 +  apply (simp only: bintrunc_bintrunc_min bintrunc_mod2p [symmetric] min_def)
  18.528 +  apply auto
  18.529 +  done
  18.530 +
  18.531 +lemma and_mask_dvd_nat: "2 ^ n dvd unat w = (w AND mask n = 0)"
  18.532 +  apply (unfold unat_def)
  18.533 +  apply (rule trans [OF _ and_mask_dvd])
  18.534 +  apply (unfold dvd_def) 
  18.535 +  apply auto 
  18.536 +  apply (drule uint_ge_0 [THEN nat_int.Abs_inverse' [simplified], symmetric])
  18.537 +  apply (simp add : int_mult int_power)
  18.538 +  apply (simp add : nat_mult_distrib nat_power_eq)
  18.539 +  done
  18.540 +
  18.541 +lemma word_2p_lem: 
  18.542 +  "n < size w ==> w < 2 ^ n = (uint (w :: 'a :: len word) < 2 ^ n)"
  18.543 +  apply (unfold word_size word_less_alt word_number_of_alt)
  18.544 +  apply (clarsimp simp add: word_of_int_power_hom word_uint.eq_norm 
  18.545 +                            int_mod_eq'
  18.546 +                  simp del: word_of_int_bin)
  18.547 +  done
  18.548 +
  18.549 +lemma less_mask_eq: "x < 2 ^ n ==> x AND mask n = (x :: 'a :: len word)"
  18.550 +  apply (unfold word_less_alt word_number_of_alt)
  18.551 +  apply (clarsimp simp add: and_mask_mod_2p word_of_int_power_hom 
  18.552 +                            word_uint.eq_norm
  18.553 +                  simp del: word_of_int_bin)
  18.554 +  apply (drule xtr8 [rotated])
  18.555 +  apply (rule int_mod_le)
  18.556 +  apply (auto simp add : mod_pos_pos_trivial)
  18.557 +  done
  18.558 +
  18.559 +lemmas mask_eq_iff_w2p =
  18.560 +  trans [OF mask_eq_iff word_2p_lem [symmetric], standard]
  18.561 +
  18.562 +lemmas and_mask_less' = 
  18.563 +  iffD2 [OF word_2p_lem and_mask_lt_2p, simplified word_size, standard]
  18.564 +
  18.565 +lemma and_mask_less_size: "n < size x ==> x AND mask n < 2^n"
  18.566 +  unfolding word_size by (erule and_mask_less')
  18.567 +
  18.568 +lemma word_mod_2p_is_mask':
  18.569 +  "c = 2 ^ n ==> c > 0 ==> x mod c = (x :: 'a :: len word) AND mask n" 
  18.570 +  by (clarsimp simp add: word_mod_def uint_2p and_mask_mod_2p) 
  18.571 +
  18.572 +lemmas word_mod_2p_is_mask = refl [THEN word_mod_2p_is_mask'] 
  18.573 +
  18.574 +lemma mask_eqs:
  18.575 +  "(a AND mask n) + b AND mask n = a + b AND mask n"
  18.576 +  "a + (b AND mask n) AND mask n = a + b AND mask n"
  18.577 +  "(a AND mask n) - b AND mask n = a - b AND mask n"
  18.578 +  "a - (b AND mask n) AND mask n = a - b AND mask n"
  18.579 +  "a * (b AND mask n) AND mask n = a * b AND mask n"
  18.580 +  "(b AND mask n) * a AND mask n = b * a AND mask n"
  18.581 +  "(a AND mask n) + (b AND mask n) AND mask n = a + b AND mask n"
  18.582 +  "(a AND mask n) - (b AND mask n) AND mask n = a - b AND mask n"
  18.583 +  "(a AND mask n) * (b AND mask n) AND mask n = a * b AND mask n"
  18.584 +  "- (a AND mask n) AND mask n = - a AND mask n"
  18.585 +  "word_succ (a AND mask n) AND mask n = word_succ a AND mask n"
  18.586 +  "word_pred (a AND mask n) AND mask n = word_pred a AND mask n"
  18.587 +  using word_of_int_Ex [where x=a] word_of_int_Ex [where x=b]
  18.588 +  by (auto simp: and_mask_wi bintr_ariths bintr_arith1s new_word_of_int_homs)
  18.589 +
  18.590 +lemma mask_power_eq:
  18.591 +  "(x AND mask n) ^ k AND mask n = x ^ k AND mask n"
  18.592 +  using word_of_int_Ex [where x=x]
  18.593 +  by (clarsimp simp: and_mask_wi word_of_int_power_hom bintr_ariths)
  18.594 +
  18.595 +
  18.596 +subsection "Revcast"
  18.597 +
  18.598 +lemmas revcast_def' = revcast_def [simplified]
  18.599 +lemmas revcast_def'' = revcast_def' [simplified word_size]
  18.600 +lemmas revcast_no_def [simp] =
  18.601 +  revcast_def' [where w="number_of ?w", unfolded word_size]
  18.602 +
  18.603 +lemma to_bl_revcast: 
  18.604 +  "to_bl (revcast w :: 'a :: len0 word) = 
  18.605 +   takefill False (len_of TYPE ('a)) (to_bl w)"
  18.606 +  apply (unfold revcast_def' word_size)
  18.607 +  apply (rule word_bl.Abs_inverse)
  18.608 +  apply simp
  18.609 +  done
  18.610 +
  18.611 +lemma revcast_rev_ucast': 
  18.612 +  "cs = [rc, uc] ==> rc = revcast (word_reverse w) ==> uc = ucast w ==> 
  18.613 +    rc = word_reverse uc"
  18.614 +  apply (unfold ucast_def revcast_def' Let_def word_reverse_def)
  18.615 +  apply (clarsimp simp add : to_bl_of_bin takefill_bintrunc)
  18.616 +  apply (simp add : word_bl.Abs_inverse word_size)
  18.617 +  done
  18.618 +
  18.619 +lemmas revcast_rev_ucast = revcast_rev_ucast' [OF refl refl refl]
  18.620 +
  18.621 +lemmas revcast_ucast = revcast_rev_ucast
  18.622 +  [where w = "word_reverse ?w1", simplified word_rev_rev, standard]
  18.623 +
  18.624 +lemmas ucast_revcast = revcast_rev_ucast [THEN word_rev_gal', standard]
  18.625 +lemmas ucast_rev_revcast = revcast_ucast [THEN word_rev_gal', standard]
  18.626 +
  18.627 +
  18.628 +-- "linking revcast and cast via shift"
  18.629 +
  18.630 +lemmas wsst_TYs = source_size target_size word_size
  18.631 +
  18.632 +lemma revcast_down_uu': 
  18.633 +  "rc = revcast ==> source_size rc = target_size rc + n ==> 
  18.634 +    rc (w :: 'a :: len word) = ucast (w >> n)"
  18.635 +  apply (simp add: revcast_def')
  18.636 +  apply (rule word_bl.Rep_inverse')
  18.637 +  apply (rule trans, rule ucast_down_drop)
  18.638 +   prefer 2
  18.639 +   apply (rule trans, rule drop_shiftr)
  18.640 +   apply (auto simp: takefill_alt wsst_TYs)
  18.641 +  done
  18.642 +
  18.643 +lemma revcast_down_us': 
  18.644 +  "rc = revcast ==> source_size rc = target_size rc + n ==> 
  18.645 +    rc (w :: 'a :: len word) = ucast (w >>> n)"
  18.646 +  apply (simp add: revcast_def')
  18.647 +  apply (rule word_bl.Rep_inverse')
  18.648 +  apply (rule trans, rule ucast_down_drop)
  18.649 +   prefer 2
  18.650 +   apply (rule trans, rule drop_sshiftr)
  18.651 +   apply (auto simp: takefill_alt wsst_TYs)
  18.652 +  done
  18.653 +
  18.654 +lemma revcast_down_su': 
  18.655 +  "rc = revcast ==> source_size rc = target_size rc + n ==> 
  18.656 +    rc (w :: 'a :: len word) = scast (w >> n)"
  18.657 +  apply (simp add: revcast_def')
  18.658 +  apply (rule word_bl.Rep_inverse')
  18.659 +  apply (rule trans, rule scast_down_drop)
  18.660 +   prefer 2
  18.661 +   apply (rule trans, rule drop_shiftr)
  18.662 +   apply (auto simp: takefill_alt wsst_TYs)
  18.663 +  done
  18.664 +
  18.665 +lemma revcast_down_ss': 
  18.666 +  "rc = revcast ==> source_size rc = target_size rc + n ==> 
  18.667 +    rc (w :: 'a :: len word) = scast (w >>> n)"
  18.668 +  apply (simp add: revcast_def')
  18.669 +  apply (rule word_bl.Rep_inverse')
  18.670 +  apply (rule trans, rule scast_down_drop)
  18.671 +   prefer 2
  18.672 +   apply (rule trans, rule drop_sshiftr)
  18.673 +   apply (auto simp: takefill_alt wsst_TYs)
  18.674 +  done
  18.675 +
  18.676 +lemmas revcast_down_uu = refl [THEN revcast_down_uu']
  18.677 +lemmas revcast_down_us = refl [THEN revcast_down_us']
  18.678 +lemmas revcast_down_su = refl [THEN revcast_down_su']
  18.679 +lemmas revcast_down_ss = refl [THEN revcast_down_ss']
  18.680 +
  18.681 +lemma cast_down_rev: 
  18.682 +  "uc = ucast ==> source_size uc = target_size uc + n ==> 
  18.683 +    uc w = revcast ((w :: 'a :: len word) << n)"
  18.684 +  apply (unfold shiftl_rev)
  18.685 +  apply clarify
  18.686 +  apply (simp add: revcast_rev_ucast)
  18.687 +  apply (rule word_rev_gal')
  18.688 +  apply (rule trans [OF _ revcast_rev_ucast])
  18.689 +  apply (rule revcast_down_uu [symmetric])
  18.690 +  apply (auto simp add: wsst_TYs)
  18.691 +  done
  18.692 +
  18.693 +lemma revcast_up': 
  18.694 +  "rc = revcast ==> source_size rc + n = target_size rc ==> 
  18.695 +    rc w = (ucast w :: 'a :: len word) << n" 
  18.696 +  apply (simp add: revcast_def')
  18.697 +  apply (rule word_bl.Rep_inverse')
  18.698 +  apply (simp add: takefill_alt)
  18.699 +  apply (rule bl_shiftl [THEN trans])
  18.700 +  apply (subst ucast_up_app)
  18.701 +  apply (auto simp add: wsst_TYs)
  18.702 +  apply (drule sym)
  18.703 +  apply (simp add: min_def)
  18.704 +  done
  18.705 +
  18.706 +lemmas revcast_up = refl [THEN revcast_up']
  18.707 +
  18.708 +lemmas rc1 = revcast_up [THEN 
  18.709 +  revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]]
  18.710 +lemmas rc2 = revcast_down_uu [THEN 
  18.711 +  revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]]
  18.712 +
  18.713 +lemmas ucast_up =
  18.714 + rc1 [simplified rev_shiftr [symmetric] revcast_ucast [symmetric]]
  18.715 +lemmas ucast_down = 
  18.716 +  rc2 [simplified rev_shiftr revcast_ucast [symmetric]]
  18.717 +
  18.718 +
  18.719 +subsection "Slices"
  18.720 +
  18.721 +lemmas slice1_no_bin [simp] =
  18.722 +  slice1_def [where w="number_of ?w", unfolded to_bl_no_bin]
  18.723 +
  18.724 +lemmas slice_no_bin [simp] = 
  18.725 +   trans [OF slice_def [THEN meta_eq_to_obj_eq] 
  18.726 +             slice1_no_bin [THEN meta_eq_to_obj_eq], 
  18.727 +          unfolded word_size, standard]
  18.728 +
  18.729 +lemma slice1_0 [simp] : "slice1 n 0 = 0"
  18.730 +  unfolding slice1_def by (simp add : to_bl_0)
  18.731 +
  18.732 +lemma slice_0 [simp] : "slice n 0 = 0"
  18.733 +  unfolding slice_def by auto
  18.734 +
  18.735 +lemma slice_take': "slice n w = of_bl (take (size w - n) (to_bl w))"
  18.736 +  unfolding slice_def' slice1_def
  18.737 +  by (simp add : takefill_alt word_size)
  18.738 +
  18.739 +lemmas slice_take = slice_take' [unfolded word_size]
  18.740 +
  18.741 +-- "shiftr to a word of the same size is just slice, 
  18.742 +    slice is just shiftr then ucast"
  18.743 +lemmas shiftr_slice = trans
  18.744 +  [OF shiftr_bl [THEN meta_eq_to_obj_eq] slice_take [symmetric], standard]
  18.745 +
  18.746 +lemma slice_shiftr: "slice n w = ucast (w >> n)"
  18.747 +  apply (unfold slice_take shiftr_bl)
  18.748 +  apply (rule ucast_of_bl_up [symmetric])
  18.749 +  apply (simp add: word_size)
  18.750 +  done
  18.751 +
  18.752 +lemma nth_slice: 
  18.753 +  "(slice n w :: 'a :: len0 word) !! m = 
  18.754 +   (w !! (m + n) & m < len_of TYPE ('a))"
  18.755 +  unfolding slice_shiftr 
  18.756 +  by (simp add : nth_ucast nth_shiftr)
  18.757 +
  18.758 +lemma slice1_down_alt': 
  18.759 +  "sl = slice1 n w ==> fs = size sl ==> fs + k = n ==> 
  18.760 +    to_bl sl = takefill False fs (drop k (to_bl w))"
  18.761 +  unfolding slice1_def word_size of_bl_def uint_bl
  18.762 +  by (clarsimp simp: word_ubin.eq_norm bl_bin_bl_rep_drop drop_takefill)
  18.763 +
  18.764 +lemma slice1_up_alt': 
  18.765 +  "sl = slice1 n w ==> fs = size sl ==> fs = n + k ==> 
  18.766 +    to_bl sl = takefill False fs (replicate k False @ (to_bl w))"
  18.767 +  apply (unfold slice1_def word_size of_bl_def uint_bl)
  18.768 +  apply (clarsimp simp: word_ubin.eq_norm bl_bin_bl_rep_drop 
  18.769 +                        takefill_append [symmetric])
  18.770 +  apply (rule_tac f = "%k. takefill False (len_of TYPE('a))
  18.771 +    (replicate k False @ bin_to_bl (len_of TYPE('b)) (uint w))" in arg_cong)
  18.772 +  apply arith
  18.773 +  done
  18.774 +    
  18.775 +lemmas sd1 = slice1_down_alt' [OF refl refl, unfolded word_size]
  18.776 +lemmas su1 = slice1_up_alt' [OF refl refl, unfolded word_size]
  18.777 +lemmas slice1_down_alt = le_add_diff_inverse [THEN sd1]
  18.778 +lemmas slice1_up_alts = 
  18.779 +  le_add_diff_inverse [symmetric, THEN su1] 
  18.780 +  le_add_diff_inverse2 [symmetric, THEN su1]
  18.781 +
  18.782 +lemma ucast_slice1: "ucast w = slice1 (size w) w"
  18.783 +  unfolding slice1_def ucast_bl
  18.784 +  by (simp add : takefill_same' word_size)
  18.785 +
  18.786 +lemma ucast_slice: "ucast w = slice 0 w"
  18.787 +  unfolding slice_def by (simp add : ucast_slice1)
  18.788 +
  18.789 +lemmas slice_id = trans [OF ucast_slice [symmetric] ucast_id]
  18.790 +
  18.791 +lemma revcast_slice1': 
  18.792 +  "rc = revcast w ==> slice1 (size rc) w = rc"
  18.793 +  unfolding slice1_def revcast_def' by (simp add : word_size)
  18.794 +
  18.795 +lemmas revcast_slice1 = refl [THEN revcast_slice1']
  18.796 +
  18.797 +lemma slice1_tf_tf': 
  18.798 +  "to_bl (slice1 n w :: 'a :: len0 word) = 
  18.799 +   rev (takefill False (len_of TYPE('a)) (rev (takefill False n (to_bl w))))"
  18.800 +  unfolding slice1_def by (rule word_rev_tf)
  18.801 +
  18.802 +lemmas slice1_tf_tf = slice1_tf_tf'
  18.803 +  [THEN word_bl.Rep_inverse', symmetric, standard]
  18.804 +
  18.805 +lemma rev_slice1:
  18.806 +  "n + k = len_of TYPE('a) + len_of TYPE('b) \<Longrightarrow> 
  18.807 +  slice1 n (word_reverse w :: 'b :: len0 word) = 
  18.808 +  word_reverse (slice1 k w :: 'a :: len0 word)"
  18.809 +  apply (unfold word_reverse_def slice1_tf_tf)
  18.810 +  apply (rule word_bl.Rep_inverse')
  18.811 +  apply (rule rev_swap [THEN iffD1])
  18.812 +  apply (rule trans [symmetric])
  18.813 +  apply (rule tf_rev)
  18.814 +   apply (simp add: word_bl.Abs_inverse)
  18.815 +  apply (simp add: word_bl.Abs_inverse)
  18.816 +  done
  18.817 +
  18.818 +lemma rev_slice': 
  18.819 +  "res = slice n (word_reverse w) ==> n + k + size res = size w ==> 
  18.820 +    res = word_reverse (slice k w)"
  18.821 +  apply (unfold slice_def word_size)
  18.822 +  apply clarify
  18.823 +  apply (rule rev_slice1)
  18.824 +  apply arith
  18.825 +  done
  18.826 +
  18.827 +lemmas rev_slice = refl [THEN rev_slice', unfolded word_size]
  18.828 +
  18.829 +lemmas sym_notr = 
  18.830 +  not_iff [THEN iffD2, THEN not_sym, THEN not_iff [THEN iffD1]]
  18.831 +
  18.832 +-- {* problem posed by TPHOLs referee:
  18.833 +      criterion for overflow of addition of signed integers *}
  18.834 +
  18.835 +lemma sofl_test:
  18.836 +  "(sint (x :: 'a :: len word) + sint y = sint (x + y)) = 
  18.837 +     ((((x+y) XOR x) AND ((x+y) XOR y)) >> (size x - 1) = 0)"
  18.838 +  apply (unfold word_size)
  18.839 +  apply (cases "len_of TYPE('a)", simp) 
  18.840 +  apply (subst msb_shift [THEN sym_notr])
  18.841 +  apply (simp add: word_ops_msb)
  18.842 +  apply (simp add: word_msb_sint)
  18.843 +  apply safe
  18.844 +       apply simp_all
  18.845 +     apply (unfold sint_word_ariths)
  18.846 +     apply (unfold word_sbin.set_iff_norm [symmetric] sints_num)
  18.847 +     apply safe
  18.848 +        apply (insert sint_range' [where x=x])
  18.849 +        apply (insert sint_range' [where x=y])
  18.850 +        defer 
  18.851 +        apply (simp (no_asm), arith)
  18.852 +       apply (simp (no_asm), arith)
  18.853 +      defer
  18.854 +      defer
  18.855 +      apply (simp (no_asm), arith)
  18.856 +     apply (simp (no_asm), arith)
  18.857 +    apply (rule notI [THEN notnotD],
  18.858 +           drule leI not_leE,
  18.859 +           drule sbintrunc_inc sbintrunc_dec,      
  18.860 +           simp)+
  18.861 +  done
  18.862 +
  18.863 +
  18.864 +section "Split and cat"
  18.865 +
  18.866 +lemmas word_split_bin' = word_split_def [THEN meta_eq_to_obj_eq, standard]
  18.867 +lemmas word_cat_bin' = word_cat_def [THEN meta_eq_to_obj_eq, standard]
  18.868 +
  18.869 +lemma word_rsplit_no:
  18.870 +  "(word_rsplit (number_of bin :: 'b :: len0 word) :: 'a word list) = 
  18.871 +    map number_of (bin_rsplit (len_of TYPE('a :: len)) 
  18.872 +      (len_of TYPE('b), bintrunc (len_of TYPE('b)) bin))"
  18.873 +  apply (unfold word_rsplit_def word_no_wi)
  18.874 +  apply (simp add: word_ubin.eq_norm)
  18.875 +  done
  18.876 +
  18.877 +lemmas word_rsplit_no_cl [simp] = word_rsplit_no
  18.878 +  [unfolded bin_rsplitl_def bin_rsplit_l [symmetric]]
  18.879 +
  18.880 +lemma test_bit_cat:
  18.881 +  "wc = word_cat a b ==> wc !! n = (n < size wc & 
  18.882 +    (if n < size b then b !! n else a !! (n - size b)))"
  18.883 +  apply (unfold word_cat_bin' test_bit_bin)
  18.884 +  apply (auto simp add : word_ubin.eq_norm nth_bintr bin_nth_cat word_size)
  18.885 +  apply (erule bin_nth_uint_imp)
  18.886 +  done
  18.887 +
  18.888 +lemma word_cat_bl: "word_cat a b = of_bl (to_bl a @ to_bl b)"
  18.889 +  apply (unfold of_bl_def to_bl_def word_cat_bin')
  18.890 +  apply (simp add: bl_to_bin_app_cat)
  18.891 +  done
  18.892 +
  18.893 +lemma of_bl_append:
  18.894 +  "(of_bl (xs @ ys) :: 'a :: len word) = of_bl xs * 2^(length ys) + of_bl ys"
  18.895 +  apply (unfold of_bl_def)
  18.896 +  apply (simp add: bl_to_bin_app_cat bin_cat_num)
  18.897 +  apply (simp add: word_of_int_power_hom [symmetric] new_word_of_int_hom_syms)
  18.898 +  done
  18.899 +
  18.900 +lemma of_bl_False [simp]:
  18.901 +  "of_bl (False#xs) = of_bl xs"
  18.902 +  by (rule word_eqI)
  18.903 +     (auto simp add: test_bit_of_bl nth_append)
  18.904 +
  18.905 +lemma of_bl_True: 
  18.906 +  "(of_bl (True#xs)::'a::len word) = 2^length xs + of_bl xs"
  18.907 +  by (subst of_bl_append [where xs="[True]", simplified])
  18.908 +     (simp add: word_1_bl)
  18.909 +
  18.910 +lemma of_bl_Cons:
  18.911 +  "of_bl (x#xs) = of_bool x * 2^length xs + of_bl xs"
  18.912 +  by (cases x) (simp_all add: of_bl_True)
  18.913 +
  18.914 +lemma split_uint_lem: "bin_split n (uint (w :: 'a :: len0 word)) = (a, b) ==> 
  18.915 +  a = bintrunc (len_of TYPE('a) - n) a & b = bintrunc (len_of TYPE('a)) b"
  18.916 +  apply (frule word_ubin.norm_Rep [THEN ssubst])
  18.917 +  apply (drule bin_split_trunc1)
  18.918 +  apply (drule sym [THEN trans])
  18.919 +  apply assumption
  18.920 +  apply safe
  18.921 +  done
  18.922 +
  18.923 +lemma word_split_bl': 
  18.924 +  "std = size c - size b ==> (word_split c = (a, b)) ==> 
  18.925 +    (a = of_bl (take std (to_bl c)) & b = of_bl (drop std (to_bl c)))"
  18.926 +  apply (unfold word_split_bin')
  18.927 +  apply safe
  18.928 +   defer
  18.929 +   apply (clarsimp split: prod.splits)
  18.930 +   apply (drule word_ubin.norm_Rep [THEN ssubst])
  18.931 +   apply (drule split_bintrunc)
  18.932 +   apply (simp add : of_bl_def bl2bin_drop word_size
  18.933 +       word_ubin.norm_eq_iff [symmetric] min_def del : word_ubin.norm_Rep)    
  18.934 +  apply (clarsimp split: prod.splits)
  18.935 +  apply (frule split_uint_lem [THEN conjunct1])
  18.936 +  apply (unfold word_size)
  18.937 +  apply (cases "len_of TYPE('a) >= len_of TYPE('b)")
  18.938 +   defer
  18.939 +   apply (simp add: word_0_bl word_0_wi_Pls)
  18.940 +  apply (simp add : of_bl_def to_bl_def)
  18.941 +  apply (subst bin_split_take1 [symmetric])
  18.942 +    prefer 2
  18.943 +    apply assumption
  18.944 +   apply simp
  18.945 +  apply (erule thin_rl)
  18.946 +  apply (erule arg_cong [THEN trans])
  18.947 +  apply (simp add : word_ubin.norm_eq_iff [symmetric])
  18.948 +  done
  18.949 +
  18.950 +lemma word_split_bl: "std = size c - size b ==> 
  18.951 +    (a = of_bl (take std (to_bl c)) & b = of_bl (drop std (to_bl c))) <-> 
  18.952 +    word_split c = (a, b)"
  18.953 +  apply (rule iffI)
  18.954 +   defer
  18.955 +   apply (erule (1) word_split_bl')
  18.956 +  apply (case_tac "word_split c")
  18.957 +  apply (auto simp add : word_size)
  18.958 +  apply (frule word_split_bl' [rotated])
  18.959 +  apply (auto simp add : word_size)
  18.960 +  done
  18.961 +
  18.962 +lemma word_split_bl_eq:
  18.963 +   "(word_split (c::'a::len word) :: ('c :: len0 word * 'd :: len0 word)) =
  18.964 +      (of_bl (take (len_of TYPE('a::len) - len_of TYPE('d::len0)) (to_bl c)),
  18.965 +       of_bl (drop (len_of TYPE('a) - len_of TYPE('d)) (to_bl c)))"
  18.966 +  apply (rule word_split_bl [THEN iffD1])
  18.967 +  apply (unfold word_size)
  18.968 +  apply (rule refl conjI)+
  18.969 +  done
  18.970 +
  18.971 +-- "keep quantifiers for use in simplification"
  18.972 +lemma test_bit_split':
  18.973 +  "word_split c = (a, b) --> (ALL n m. b !! n = (n < size b & c !! n) & 
  18.974 +    a !! m = (m < size a & c !! (m + size b)))"
  18.975 +  apply (unfold word_split_bin' test_bit_bin)
  18.976 +  apply (clarify)
  18.977 +  apply (clarsimp simp: word_ubin.eq_norm nth_bintr word_size split: prod.splits)
  18.978 +  apply (drule bin_nth_split)
  18.979 +  apply safe
  18.980 +       apply (simp_all add: add_commute)
  18.981 +   apply (erule bin_nth_uint_imp)+
  18.982 +  done
  18.983 +
  18.984 +lemmas test_bit_split = 
  18.985 +  test_bit_split' [THEN mp, simplified all_simps, standard]
  18.986 +
  18.987 +lemma test_bit_split_eq: "word_split c = (a, b) <-> 
  18.988 +  ((ALL n::nat. b !! n = (n < size b & c !! n)) &
  18.989 +    (ALL m::nat. a !! m = (m < size a & c !! (m + size b))))"
  18.990 +  apply (rule_tac iffI)
  18.991 +   apply (rule_tac conjI)
  18.992 +    apply (erule test_bit_split [THEN conjunct1])
  18.993 +   apply (erule test_bit_split [THEN conjunct2])
  18.994 +  apply (case_tac "word_split c")
  18.995 +  apply (frule test_bit_split)
  18.996 +  apply (erule trans)
  18.997 +  apply (fastsimp intro ! : word_eqI simp add : word_size)
  18.998 +  done
  18.999 +
 18.1000 +-- {* this odd result is analogous to ucast\_id, 
 18.1001 +      result to the length given by the result type *}
 18.1002 +
 18.1003 +lemma word_cat_id: "word_cat a b = b"
 18.1004 +  unfolding word_cat_bin' by (simp add: word_ubin.inverse_norm)
 18.1005 +
 18.1006 +-- "limited hom result"
 18.1007 +lemma word_cat_hom:
 18.1008 +  "len_of TYPE('a::len0) <= len_of TYPE('b::len0) + len_of TYPE ('c::len0)
 18.1009 +  ==>
 18.1010 +  (word_cat (word_of_int w :: 'b word) (b :: 'c word) :: 'a word) = 
 18.1011 +  word_of_int (bin_cat w (size b) (uint b))"
 18.1012 +  apply (unfold word_cat_def word_size) 
 18.1013 +  apply (clarsimp simp add : word_ubin.norm_eq_iff [symmetric]
 18.1014 +      word_ubin.eq_norm bintr_cat min_def)
 18.1015 +  apply arith
 18.1016 +  done
 18.1017 +
 18.1018 +lemma word_cat_split_alt:
 18.1019 +  "size w <= size u + size v ==> word_split w = (u, v) ==> word_cat u v = w"
 18.1020 +  apply (rule word_eqI)
 18.1021 +  apply (drule test_bit_split)
 18.1022 +  apply (clarsimp simp add : test_bit_cat word_size)
 18.1023 +  apply safe
 18.1024 +  apply arith
 18.1025 +  done
 18.1026 +
 18.1027 +lemmas word_cat_split_size = 
 18.1028 +  sym [THEN [2] word_cat_split_alt [symmetric], standard]
 18.1029 +
 18.1030 +
 18.1031 +subsection "Split and slice"
 18.1032 +
 18.1033 +lemma split_slices: 
 18.1034 +  "word_split w = (u, v) ==> u = slice (size v) w & v = slice 0 w"
 18.1035 +  apply (drule test_bit_split)
 18.1036 +  apply (rule conjI)
 18.1037 +   apply (rule word_eqI, clarsimp simp: nth_slice word_size)+
 18.1038 +  done
 18.1039 +
 18.1040 +lemma slice_cat1':
 18.1041 +  "wc = word_cat a b ==> size wc >= size a + size b ==> slice (size b) wc = a"
 18.1042 +  apply safe
 18.1043 +  apply (rule word_eqI)
 18.1044 +  apply (simp add: nth_slice test_bit_cat word_size)
 18.1045 +  done
 18.1046 +
 18.1047 +lemmas slice_cat1 = refl [THEN slice_cat1']
 18.1048 +lemmas slice_cat2 = trans [OF slice_id word_cat_id]
 18.1049 +
 18.1050 +lemma cat_slices:
 18.1051 +  "a = slice n c ==> b = slice 0 c ==> n = size b ==> \  
 18.1052 +    size a + size b >= size c ==> word_cat a b = c"
 18.1053 +  apply safe
 18.1054 +  apply (rule word_eqI)
 18.1055 +  apply (simp add: nth_slice test_bit_cat word_size)
 18.1056 +  apply safe
 18.1057 +  apply arith
 18.1058 +  done
 18.1059 +
 18.1060 +lemma word_split_cat_alt:
 18.1061 +  "w = word_cat u v ==> size u + size v <= size w ==> word_split w = (u, v)"
 18.1062 +  apply (case_tac "word_split ?w")
 18.1063 +  apply (rule trans, assumption)
 18.1064 +  apply (drule test_bit_split)
 18.1065 +  apply safe
 18.1066 +   apply (rule word_eqI, clarsimp simp: test_bit_cat word_size)+
 18.1067 +  done
 18.1068 +
 18.1069 +lemmas word_cat_bl_no_bin [simp] =
 18.1070 +  word_cat_bl [where a="number_of ?a" 
 18.1071 +                 and b="number_of ?b", 
 18.1072 +               unfolded to_bl_no_bin]
 18.1073 +
 18.1074 +lemmas word_split_bl_no_bin [simp] =
 18.1075 +  word_split_bl_eq [where c="number_of ?c", unfolded to_bl_no_bin]
 18.1076 +
 18.1077 +-- {* this odd result arises from the fact that the statement of the
 18.1078 +      result implies that the decoded words are of the same type, 
 18.1079 +      and therefore of the same length, as the original word *}
 18.1080 +
 18.1081 +lemma word_rsplit_same: "word_rsplit w = [w]"
 18.1082 +  unfolding word_rsplit_def by (simp add : bin_rsplit_all)
 18.1083 +
 18.1084 +lemma word_rsplit_empty_iff_size:
 18.1085 +  "(word_rsplit w = []) = (size w = 0)" 
 18.1086 +  unfolding word_rsplit_def bin_rsplit_def word_size
 18.1087 +  by (simp add: bin_rsplit_aux_simp_alt Let_def split: split_split)
 18.1088 +
 18.1089 +lemma test_bit_rsplit:
 18.1090 +  "sw = word_rsplit w ==> m < size (hd sw :: 'a :: len word) ==> 
 18.1091 +    k < length sw ==> (rev sw ! k) !! m = (w !! (k * size (hd sw) + m))"
 18.1092 +  apply (unfold word_rsplit_def word_test_bit_def)
 18.1093 +  apply (rule trans)
 18.1094 +   apply (rule_tac f = "%x. bin_nth x m" in arg_cong)
 18.1095 +   apply (rule nth_map [symmetric])
 18.1096 +   apply simp
 18.1097 +  apply (rule bin_nth_rsplit)
 18.1098 +     apply simp_all
 18.1099 +  apply (simp add : word_size rev_map map_compose [symmetric])
 18.1100 +  apply (rule trans)
 18.1101 +   defer
 18.1102 +   apply (rule map_ident [THEN fun_cong])
 18.1103 +  apply (rule refl [THEN map_cong])
 18.1104 +  apply (simp add : word_ubin.eq_norm)
 18.1105 +  apply (erule bin_rsplit_size_sign [OF len_gt_0 refl])
 18.1106 +  done
 18.1107 +
 18.1108 +lemma word_rcat_bl: "word_rcat wl =