src/HOL/Word/WordArith.thy
author nipkow
Fri Mar 06 17:38:47 2009 +0100 (2009-03-06)
changeset 30313 b2441b0c8d38
parent 29668 33ba3faeaa0e
child 30509 e19d5b459a61
permissions -rw-r--r--
added lemmas
     1 (* 
     2     Author:     Jeremy Dawson and Gerwin Klein, NICTA
     3 
     4   contains arithmetic theorems for word, instantiations to
     5   arithmetic type classes and tactics for reducing word arithmetic
     6   to linear arithmetic on int or nat
     7 *) 
     8 
     9 header {* Word Arithmetic *}
    10 
    11 theory WordArith
    12 imports WordDefinition
    13 begin
    14 
    15 lemma word_less_alt: "(a < b) = (uint a < uint b)"
    16   unfolding word_less_def word_le_def
    17   by (auto simp del: word_uint.Rep_inject 
    18            simp: word_uint.Rep_inject [symmetric])
    19 
    20 lemma signed_linorder: "linorder word_sle word_sless"
    21 proof
    22 qed (unfold word_sle_def word_sless_def, auto)
    23 
    24 interpretation signed!: linorder "word_sle" "word_sless"
    25   by (rule signed_linorder)
    26 
    27 lemmas word_arith_wis = 
    28   word_add_def word_mult_def word_minus_def 
    29   word_succ_def word_pred_def word_0_wi word_1_wi
    30 
    31 lemma udvdI: 
    32   "0 \<le> n ==> uint b = n * uint a ==> a udvd b"
    33   by (auto simp: udvd_def)
    34 
    35 lemmas word_div_no [simp] = 
    36   word_div_def [of "number_of a" "number_of b", standard]
    37 
    38 lemmas word_mod_no [simp] = 
    39   word_mod_def [of "number_of a" "number_of b", standard]
    40 
    41 lemmas word_less_no [simp] = 
    42   word_less_def [of "number_of a" "number_of b", standard]
    43 
    44 lemmas word_le_no [simp] = 
    45   word_le_def [of "number_of a" "number_of b", standard]
    46 
    47 lemmas word_sless_no [simp] = 
    48   word_sless_def [of "number_of a" "number_of b", standard]
    49 
    50 lemmas word_sle_no [simp] = 
    51   word_sle_def [of "number_of a" "number_of b", standard]
    52 
    53 (* following two are available in class number_ring, 
    54   but convenient to have them here here;
    55   note - the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1
    56   are in the default simpset, so to use the automatic simplifications for
    57   (eg) sint (number_of bin) on sint 1, must do
    58   (simp add: word_1_no del: numeral_1_eq_1) 
    59   *)
    60 lemmas word_0_wi_Pls = word_0_wi [folded Pls_def]
    61 lemmas word_0_no = word_0_wi_Pls [folded word_no_wi]
    62 
    63 lemma int_one_bin: "(1 :: int) == (Int.Pls BIT bit.B1)"
    64   unfolding Pls_def Bit_def by auto
    65 
    66 lemma word_1_no: 
    67   "(1 :: 'a :: len0 word) == number_of (Int.Pls BIT bit.B1)"
    68   unfolding word_1_wi word_number_of_def int_one_bin by auto
    69 
    70 lemma word_m1_wi: "-1 == word_of_int -1" 
    71   by (rule word_number_of_alt)
    72 
    73 lemma word_m1_wi_Min: "-1 = word_of_int Int.Min"
    74   by (simp add: word_m1_wi number_of_eq)
    75 
    76 lemma word_0_bl: "of_bl [] = 0" 
    77   unfolding word_0_wi of_bl_def by (simp add : Pls_def)
    78 
    79 lemma word_1_bl: "of_bl [True] = 1" 
    80   unfolding word_1_wi of_bl_def
    81   by (simp add : bl_to_bin_def Bit_def Pls_def)
    82 
    83 lemma uint_0 [simp] : "(uint 0 = 0)" 
    84   unfolding word_0_wi
    85   by (simp add: word_ubin.eq_norm Pls_def [symmetric])
    86 
    87 lemma of_bl_0 [simp] : "of_bl (replicate n False) = 0"
    88   by (simp add : word_0_wi of_bl_def bl_to_bin_rep_False Pls_def)
    89 
    90 lemma to_bl_0: 
    91   "to_bl (0::'a::len0 word) = replicate (len_of TYPE('a)) False"
    92   unfolding uint_bl
    93   by (simp add : word_size bin_to_bl_Pls Pls_def [symmetric])
    94 
    95 lemma uint_0_iff: "(uint x = 0) = (x = 0)"
    96   by (auto intro!: word_uint.Rep_eqD)
    97 
    98 lemma unat_0_iff: "(unat x = 0) = (x = 0)"
    99   unfolding unat_def by (auto simp add : nat_eq_iff uint_0_iff)
   100 
   101 lemma unat_0 [simp]: "unat 0 = 0"
   102   unfolding unat_def by auto
   103 
   104 lemma size_0_same': "size w = 0 ==> w = (v :: 'a :: len0 word)"
   105   apply (unfold word_size)
   106   apply (rule box_equals)
   107     defer
   108     apply (rule word_uint.Rep_inverse)+
   109   apply (rule word_ubin.norm_eq_iff [THEN iffD1])
   110   apply simp
   111   done
   112 
   113 lemmas size_0_same = size_0_same' [folded word_size]
   114 
   115 lemmas unat_eq_0 = unat_0_iff
   116 lemmas unat_eq_zero = unat_0_iff
   117 
   118 lemma unat_gt_0: "(0 < unat x) = (x ~= 0)"
   119 by (auto simp: unat_0_iff [symmetric])
   120 
   121 lemma ucast_0 [simp] : "ucast 0 = 0"
   122 unfolding ucast_def
   123 by simp (simp add: word_0_wi)
   124 
   125 lemma sint_0 [simp] : "sint 0 = 0"
   126 unfolding sint_uint
   127 by (simp add: Pls_def [symmetric])
   128 
   129 lemma scast_0 [simp] : "scast 0 = 0"
   130 apply (unfold scast_def)
   131 apply simp
   132 apply (simp add: word_0_wi)
   133 done
   134 
   135 lemma sint_n1 [simp] : "sint -1 = -1"
   136 apply (unfold word_m1_wi_Min)
   137 apply (simp add: word_sbin.eq_norm)
   138 apply (unfold Min_def number_of_eq)
   139 apply simp
   140 done
   141 
   142 lemma scast_n1 [simp] : "scast -1 = -1"
   143   apply (unfold scast_def sint_n1)
   144   apply (unfold word_number_of_alt)
   145   apply (rule refl)
   146   done
   147 
   148 lemma uint_1 [simp] : "uint (1 :: 'a :: len word) = 1"
   149   unfolding word_1_wi
   150   by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
   151 
   152 lemma unat_1 [simp] : "unat (1 :: 'a :: len word) = 1"
   153   by (unfold unat_def uint_1) auto
   154 
   155 lemma ucast_1 [simp] : "ucast (1 :: 'a :: len word) = 1"
   156   unfolding ucast_def word_1_wi
   157   by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
   158 
   159 (* abstraction preserves the operations
   160   (the definitions tell this for bins in range uint) *)
   161 
   162 lemmas arths = 
   163   bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1],
   164                 folded word_ubin.eq_norm, standard]
   165 
   166 lemma wi_homs: 
   167   shows
   168   wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and
   169   wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and
   170   wi_hom_neg: "- word_of_int a = word_of_int (- a)" and
   171   wi_hom_succ: "word_succ (word_of_int a) = word_of_int (Int.succ a)" and
   172   wi_hom_pred: "word_pred (word_of_int a) = word_of_int (Int.pred a)"
   173   by (auto simp: word_arith_wis arths)
   174 
   175 lemmas wi_hom_syms = wi_homs [symmetric]
   176 
   177 lemma word_sub_def: "a - b == a + - (b :: 'a :: len0 word)"
   178   unfolding word_sub_wi diff_def
   179   by (simp only : word_uint.Rep_inverse wi_hom_syms)
   180     
   181 lemmas word_diff_minus = word_sub_def [THEN meta_eq_to_obj_eq, standard]
   182 
   183 lemma word_of_int_sub_hom:
   184   "(word_of_int a) - word_of_int b = word_of_int (a - b)"
   185   unfolding word_sub_def diff_def by (simp only : wi_homs)
   186 
   187 lemmas new_word_of_int_homs = 
   188   word_of_int_sub_hom wi_homs word_0_wi word_1_wi 
   189 
   190 lemmas new_word_of_int_hom_syms = new_word_of_int_homs [symmetric, standard]
   191 
   192 lemmas word_of_int_hom_syms =
   193   new_word_of_int_hom_syms [unfolded succ_def pred_def]
   194 
   195 lemmas word_of_int_homs =
   196   new_word_of_int_homs [unfolded succ_def pred_def]
   197 
   198 lemmas word_of_int_add_hom = word_of_int_homs (2)
   199 lemmas word_of_int_mult_hom = word_of_int_homs (3)
   200 lemmas word_of_int_minus_hom = word_of_int_homs (4)
   201 lemmas word_of_int_succ_hom = word_of_int_homs (5)
   202 lemmas word_of_int_pred_hom = word_of_int_homs (6)
   203 lemmas word_of_int_0_hom = word_of_int_homs (7)
   204 lemmas word_of_int_1_hom = word_of_int_homs (8)
   205 
   206 (* now, to get the weaker results analogous to word_div/mod_def *)
   207 
   208 lemmas word_arith_alts = 
   209   word_sub_wi [unfolded succ_def pred_def, standard]
   210   word_arith_wis [unfolded succ_def pred_def, standard]
   211 
   212 lemmas word_sub_alt = word_arith_alts (1)
   213 lemmas word_add_alt = word_arith_alts (2)
   214 lemmas word_mult_alt = word_arith_alts (3)
   215 lemmas word_minus_alt = word_arith_alts (4)
   216 lemmas word_succ_alt = word_arith_alts (5)
   217 lemmas word_pred_alt = word_arith_alts (6)
   218 lemmas word_0_alt = word_arith_alts (7)
   219 lemmas word_1_alt = word_arith_alts (8)
   220 
   221 subsection  "Transferring goals from words to ints"
   222 
   223 lemma word_ths:  
   224   shows
   225   word_succ_p1:   "word_succ a = a + 1" and
   226   word_pred_m1:   "word_pred a = a - 1" and
   227   word_pred_succ: "word_pred (word_succ a) = a" and
   228   word_succ_pred: "word_succ (word_pred a) = a" and
   229   word_mult_succ: "word_succ a * b = b + a * b"
   230   by (rule word_uint.Abs_cases [of b],
   231       rule word_uint.Abs_cases [of a],
   232       simp add: pred_def succ_def add_commute mult_commute 
   233                 ring_distribs new_word_of_int_homs)+
   234 
   235 lemmas uint_cong = arg_cong [where f = uint]
   236 
   237 lemmas uint_word_ariths = 
   238   word_arith_alts [THEN trans [OF uint_cong int_word_uint], standard]
   239 
   240 lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p]
   241 
   242 (* similar expressions for sint (arith operations) *)
   243 lemmas sint_word_ariths = uint_word_arith_bintrs
   244   [THEN uint_sint [symmetric, THEN trans],
   245   unfolded uint_sint bintr_arith1s bintr_ariths 
   246     len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep, standard]
   247 
   248 lemmas uint_div_alt = word_div_def
   249   [THEN trans [OF uint_cong int_word_uint], standard]
   250 lemmas uint_mod_alt = word_mod_def
   251   [THEN trans [OF uint_cong int_word_uint], standard]
   252 
   253 lemma word_pred_0_n1: "word_pred 0 = word_of_int -1"
   254   unfolding word_pred_def number_of_eq
   255   by (simp add : pred_def word_no_wi)
   256 
   257 lemma word_pred_0_Min: "word_pred 0 = word_of_int Int.Min"
   258   by (simp add: word_pred_0_n1 number_of_eq)
   259 
   260 lemma word_m1_Min: "- 1 = word_of_int Int.Min"
   261   unfolding Min_def by (simp only: word_of_int_hom_syms)
   262 
   263 lemma succ_pred_no [simp]:
   264   "word_succ (number_of bin) = number_of (Int.succ bin) & 
   265     word_pred (number_of bin) = number_of (Int.pred bin)"
   266   unfolding word_number_of_def by (simp add : new_word_of_int_homs)
   267 
   268 lemma word_sp_01 [simp] : 
   269   "word_succ -1 = 0 & word_succ 0 = 1 & word_pred 0 = -1 & word_pred 1 = 0"
   270   by (unfold word_0_no word_1_no) auto
   271 
   272 (* alternative approach to lifting arithmetic equalities *)
   273 lemma word_of_int_Ex:
   274   "\<exists>y. x = word_of_int y"
   275   by (rule_tac x="uint x" in exI) simp
   276 
   277 lemma word_arith_eqs:
   278   fixes a :: "'a::len0 word"
   279   fixes b :: "'a::len0 word"
   280   shows
   281   word_add_0: "0 + a = a" and
   282   word_add_0_right: "a + 0 = a" and
   283   word_mult_1: "1 * a = a" and
   284   word_mult_1_right: "a * 1 = a" and
   285   word_add_commute: "a + b = b + a" and
   286   word_add_assoc: "a + b + c = a + (b + c)" and
   287   word_add_left_commute: "a + (b + c) = b + (a + c)" and
   288   word_mult_commute: "a * b = b * a" and
   289   word_mult_assoc: "a * b * c = a * (b * c)" and
   290   word_mult_left_commute: "a * (b * c) = b * (a * c)" and
   291   word_left_distrib: "(a + b) * c = a * c + b * c" and
   292   word_right_distrib: "a * (b + c) = a * b + a * c" and
   293   word_left_minus: "- a + a = 0" and
   294   word_diff_0_right: "a - 0 = a" and
   295   word_diff_self: "a - a = 0"
   296   using word_of_int_Ex [of a] 
   297         word_of_int_Ex [of b] 
   298         word_of_int_Ex [of c]
   299   by (auto simp: word_of_int_hom_syms [symmetric]
   300                  zadd_0_right add_commute add_assoc add_left_commute
   301                  mult_commute mult_assoc mult_left_commute
   302                  left_distrib right_distrib)
   303   
   304 lemmas word_add_ac = word_add_commute word_add_assoc word_add_left_commute
   305 lemmas word_mult_ac = word_mult_commute word_mult_assoc word_mult_left_commute
   306   
   307 lemmas word_plus_ac0 = word_add_0 word_add_0_right word_add_ac
   308 lemmas word_times_ac1 = word_mult_1 word_mult_1_right word_mult_ac
   309 
   310 
   311 subsection "Order on fixed-length words"
   312 
   313 lemma word_order_trans: "x <= y ==> y <= z ==> x <= (z :: 'a :: len0 word)"
   314   unfolding word_le_def by auto
   315 
   316 lemma word_order_refl: "z <= (z :: 'a :: len0 word)"
   317   unfolding word_le_def by auto
   318 
   319 lemma word_order_antisym: "x <= y ==> y <= x ==> x = (y :: 'a :: len0 word)"
   320   unfolding word_le_def by (auto intro!: word_uint.Rep_eqD)
   321 
   322 lemma word_order_linear:
   323   "y <= x | x <= (y :: 'a :: len0 word)"
   324   unfolding word_le_def by auto
   325 
   326 lemma word_zero_le [simp] :
   327   "0 <= (y :: 'a :: len0 word)"
   328   unfolding word_le_def by auto
   329   
   330 instance word :: (len0) semigroup_add
   331   by intro_classes (simp add: word_add_assoc)
   332 
   333 instance word :: (len0) linorder
   334   by intro_classes (auto simp: word_less_def word_le_def)
   335 
   336 instance word :: (len0) ring
   337   by intro_classes
   338      (auto simp: word_arith_eqs word_diff_minus 
   339                  word_diff_self [unfolded word_diff_minus])
   340 
   341 lemma word_m1_ge [simp] : "word_pred 0 >= y"
   342   unfolding word_le_def
   343   by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
   344 
   345 lemmas word_n1_ge [simp]  = word_m1_ge [simplified word_sp_01]
   346 
   347 lemmas word_not_simps [simp] = 
   348   word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
   349 
   350 lemma word_gt_0: "0 < y = (0 ~= (y :: 'a :: len0 word))"
   351   unfolding word_less_def by auto
   352 
   353 lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of y", standard]
   354 
   355 lemma word_sless_alt: "(a <s b) == (sint a < sint b)"
   356   unfolding word_sle_def word_sless_def
   357   by (auto simp add: less_le)
   358 
   359 lemma word_le_nat_alt: "(a <= b) = (unat a <= unat b)"
   360   unfolding unat_def word_le_def
   361   by (rule nat_le_eq_zle [symmetric]) simp
   362 
   363 lemma word_less_nat_alt: "(a < b) = (unat a < unat b)"
   364   unfolding unat_def word_less_alt
   365   by (rule nat_less_eq_zless [symmetric]) simp
   366   
   367 lemma wi_less: 
   368   "(word_of_int n < (word_of_int m :: 'a :: len0 word)) = 
   369     (n mod 2 ^ len_of TYPE('a) < m mod 2 ^ len_of TYPE('a))"
   370   unfolding word_less_alt by (simp add: word_uint.eq_norm)
   371 
   372 lemma wi_le: 
   373   "(word_of_int n <= (word_of_int m :: 'a :: len0 word)) = 
   374     (n mod 2 ^ len_of TYPE('a) <= m mod 2 ^ len_of TYPE('a))"
   375   unfolding word_le_def by (simp add: word_uint.eq_norm)
   376 
   377 lemma udvd_nat_alt: "a udvd b = (EX n>=0. unat b = n * unat a)"
   378   apply (unfold udvd_def)
   379   apply safe
   380    apply (simp add: unat_def nat_mult_distrib)
   381   apply (simp add: uint_nat int_mult)
   382   apply (rule exI)
   383   apply safe
   384    prefer 2
   385    apply (erule notE)
   386    apply (rule refl)
   387   apply force
   388   done
   389 
   390 lemma udvd_iff_dvd: "x udvd y <-> unat x dvd unat y"
   391   unfolding dvd_def udvd_nat_alt by force
   392 
   393 lemmas unat_mono = word_less_nat_alt [THEN iffD1, standard]
   394 
   395 lemma word_zero_neq_one: "0 < len_of TYPE ('a :: len0) ==> (0 :: 'a word) ~= 1";
   396   unfolding word_arith_wis
   397   by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc)
   398 
   399 lemmas lenw1_zero_neq_one = len_gt_0 [THEN word_zero_neq_one]
   400 
   401 lemma no_no [simp] : "number_of (number_of b) = number_of b"
   402   by (simp add: number_of_eq)
   403 
   404 lemma unat_minus_one: "x ~= 0 ==> unat (x - 1) = unat x - 1"
   405   apply (unfold unat_def)
   406   apply (simp only: int_word_uint word_arith_alts rdmods)
   407   apply (subgoal_tac "uint x >= 1")
   408    prefer 2
   409    apply (drule contrapos_nn)
   410     apply (erule word_uint.Rep_inverse' [symmetric])
   411    apply (insert uint_ge_0 [of x])[1]
   412    apply arith
   413   apply (rule box_equals)
   414     apply (rule nat_diff_distrib)
   415      prefer 2
   416      apply assumption
   417     apply simp
   418    apply (subst mod_pos_pos_trivial)
   419      apply arith
   420     apply (insert uint_lt2p [of x])[1]
   421     apply arith
   422    apply (rule refl)
   423   apply simp
   424   done
   425     
   426 lemma measure_unat: "p ~= 0 ==> unat (p - 1) < unat p"
   427   by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric])
   428   
   429 lemmas uint_add_ge0 [simp] =
   430   add_nonneg_nonneg [OF uint_ge_0 uint_ge_0, standard]
   431 lemmas uint_mult_ge0 [simp] =
   432   mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0, standard]
   433 
   434 lemma uint_sub_lt2p [simp]: 
   435   "uint (x :: 'a :: len0 word) - uint (y :: 'b :: len0 word) < 
   436     2 ^ len_of TYPE('a)"
   437   using uint_ge_0 [of y] uint_lt2p [of x] by arith
   438 
   439 
   440 subsection "Conditions for the addition (etc) of two words to overflow"
   441 
   442 lemma uint_add_lem: 
   443   "(uint x + uint y < 2 ^ len_of TYPE('a)) = 
   444     (uint (x + y :: 'a :: len0 word) = uint x + uint y)"
   445   by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
   446 
   447 lemma uint_mult_lem: 
   448   "(uint x * uint y < 2 ^ len_of TYPE('a)) = 
   449     (uint (x * y :: 'a :: len0 word) = uint x * uint y)"
   450   by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
   451 
   452 lemma uint_sub_lem: 
   453   "(uint x >= uint y) = (uint (x - y) = uint x - uint y)"
   454   by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
   455 
   456 lemma uint_add_le: "uint (x + y) <= uint x + uint y"
   457   unfolding uint_word_ariths by (auto simp: mod_add_if_z)
   458 
   459 lemma uint_sub_ge: "uint (x - y) >= uint x - uint y"
   460   unfolding uint_word_ariths by (auto simp: mod_sub_if_z)
   461 
   462 lemmas uint_sub_if' =
   463   trans [OF uint_word_ariths(1) mod_sub_if_z, simplified, standard]
   464 lemmas uint_plus_if' =
   465   trans [OF uint_word_ariths(2) mod_add_if_z, simplified, standard]
   466 
   467 
   468 subsection {* Definition of uint\_arith *}
   469 
   470 lemma word_of_int_inverse:
   471   "word_of_int r = a ==> 0 <= r ==> r < 2 ^ len_of TYPE('a) ==> 
   472    uint (a::'a::len0 word) = r"
   473   apply (erule word_uint.Abs_inverse' [rotated])
   474   apply (simp add: uints_num)
   475   done
   476 
   477 lemma uint_split:
   478   fixes x::"'a::len0 word"
   479   shows "P (uint x) = 
   480          (ALL i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) --> P i)"
   481   apply (fold word_int_case_def)
   482   apply (auto dest!: word_of_int_inverse simp: int_word_uint int_mod_eq'
   483               split: word_int_split)
   484   done
   485 
   486 lemma uint_split_asm:
   487   fixes x::"'a::len0 word"
   488   shows "P (uint x) = 
   489          (~(EX i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) & ~ P i))"
   490   by (auto dest!: word_of_int_inverse 
   491            simp: int_word_uint int_mod_eq'
   492            split: uint_split)
   493 
   494 lemmas uint_splits = uint_split uint_split_asm
   495 
   496 lemmas uint_arith_simps = 
   497   word_le_def word_less_alt
   498   word_uint.Rep_inject [symmetric] 
   499   uint_sub_if' uint_plus_if'
   500 
   501 (* use this to stop, eg, 2 ^ len_of TYPE (32) being simplified *)
   502 lemma power_False_cong: "False ==> a ^ b = c ^ d" 
   503   by auto
   504 
   505 (* uint_arith_tac: reduce to arithmetic on int, try to solve by arith *)
   506 ML {*
   507 fun uint_arith_ss_of ss = 
   508   ss addsimps @{thms uint_arith_simps}
   509      delsimps @{thms word_uint.Rep_inject}
   510      addsplits @{thms split_if_asm} 
   511      addcongs @{thms power_False_cong}
   512 
   513 fun uint_arith_tacs ctxt = 
   514   let fun arith_tac' n t = arith_tac ctxt n t handle COOPER => Seq.empty  
   515   in 
   516     [ CLASET' clarify_tac 1,
   517       SIMPSET' (full_simp_tac o uint_arith_ss_of) 1,
   518       ALLGOALS (full_simp_tac (HOL_ss addsplits @{thms uint_splits} 
   519                                       addcongs @{thms power_False_cong})),
   520       rewrite_goals_tac @{thms word_size}, 
   521       ALLGOALS  (fn n => REPEAT (resolve_tac [allI, impI] n) THEN      
   522                          REPEAT (etac conjE n) THEN
   523                          REPEAT (dtac @{thm word_of_int_inverse} n 
   524                                  THEN atac n 
   525                                  THEN atac n)),
   526       TRYALL arith_tac' ]
   527   end
   528 
   529 fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt))
   530 *}
   531 
   532 method_setup uint_arith = 
   533   "Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD (uint_arith_tac ctxt 1))" 
   534   "solving word arithmetic via integers and arith"
   535 
   536 
   537 subsection "More on overflows and monotonicity"
   538 
   539 lemma no_plus_overflow_uint_size: 
   540   "((x :: 'a :: len0 word) <= x + y) = (uint x + uint y < 2 ^ size x)"
   541   unfolding word_size by uint_arith
   542 
   543 lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size]
   544 
   545 lemma no_ulen_sub: "((x :: 'a :: len0 word) >= x - y) = (uint y <= uint x)"
   546   by uint_arith
   547 
   548 lemma no_olen_add':
   549   fixes x :: "'a::len0 word"
   550   shows "(x \<le> y + x) = (uint y + uint x < 2 ^ len_of TYPE('a))"
   551   by (simp add: word_add_ac add_ac no_olen_add)
   552 
   553 lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric], standard]
   554 
   555 lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem, standard]
   556 lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1, standard]
   557 lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem, standard]
   558 lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def]
   559 lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def]
   560 lemmas word_sub_le = word_sub_le_iff [THEN iffD2, standard]
   561 
   562 lemma word_less_sub1: 
   563   "(x :: 'a :: len word) ~= 0 ==> (1 < x) = (0 < x - 1)"
   564   by uint_arith
   565 
   566 lemma word_le_sub1: 
   567   "(x :: 'a :: len word) ~= 0 ==> (1 <= x) = (0 <= x - 1)"
   568   by uint_arith
   569 
   570 lemma sub_wrap_lt: 
   571   "((x :: 'a :: len0 word) < x - z) = (x < z)"
   572   by uint_arith
   573 
   574 lemma sub_wrap: 
   575   "((x :: 'a :: len0 word) <= x - z) = (z = 0 | x < z)"
   576   by uint_arith
   577 
   578 lemma plus_minus_not_NULL_ab: 
   579   "(x :: 'a :: len0 word) <= ab - c ==> c <= ab ==> c ~= 0 ==> x + c ~= 0"
   580   by uint_arith
   581 
   582 lemma plus_minus_no_overflow_ab: 
   583   "(x :: 'a :: len0 word) <= ab - c ==> c <= ab ==> x <= x + c" 
   584   by uint_arith
   585 
   586 lemma le_minus': 
   587   "(a :: 'a :: len0 word) + c <= b ==> a <= a + c ==> c <= b - a"
   588   by uint_arith
   589 
   590 lemma le_plus': 
   591   "(a :: 'a :: len0 word) <= b ==> c <= b - a ==> a + c <= b"
   592   by uint_arith
   593 
   594 lemmas le_plus = le_plus' [rotated]
   595 
   596 lemmas le_minus = leD [THEN thin_rl, THEN le_minus', standard]
   597 
   598 lemma word_plus_mono_right: 
   599   "(y :: 'a :: len0 word) <= z ==> x <= x + z ==> x + y <= x + z"
   600   by uint_arith
   601 
   602 lemma word_less_minus_cancel: 
   603   "y - x < z - x ==> x <= z ==> (y :: 'a :: len0 word) < z"
   604   by uint_arith
   605 
   606 lemma word_less_minus_mono_left: 
   607   "(y :: 'a :: len0 word) < z ==> x <= y ==> y - x < z - x"
   608   by uint_arith
   609 
   610 lemma word_less_minus_mono:  
   611   "a < c ==> d < b ==> a - b < a ==> c - d < c 
   612   ==> a - b < c - (d::'a::len word)"
   613   by uint_arith
   614 
   615 lemma word_le_minus_cancel: 
   616   "y - x <= z - x ==> x <= z ==> (y :: 'a :: len0 word) <= z"
   617   by uint_arith
   618 
   619 lemma word_le_minus_mono_left: 
   620   "(y :: 'a :: len0 word) <= z ==> x <= y ==> y - x <= z - x"
   621   by uint_arith
   622 
   623 lemma word_le_minus_mono:  
   624   "a <= c ==> d <= b ==> a - b <= a ==> c - d <= c 
   625   ==> a - b <= c - (d::'a::len word)"
   626   by uint_arith
   627 
   628 lemma plus_le_left_cancel_wrap: 
   629   "(x :: 'a :: len0 word) + y' < x ==> x + y < x ==> (x + y' < x + y) = (y' < y)"
   630   by uint_arith
   631 
   632 lemma plus_le_left_cancel_nowrap: 
   633   "(x :: 'a :: len0 word) <= x + y' ==> x <= x + y ==> 
   634     (x + y' < x + y) = (y' < y)" 
   635   by uint_arith
   636 
   637 lemma word_plus_mono_right2: 
   638   "(a :: 'a :: len0 word) <= a + b ==> c <= b ==> a <= a + c"
   639   by uint_arith
   640 
   641 lemma word_less_add_right: 
   642   "(x :: 'a :: len0 word) < y - z ==> z <= y ==> x + z < y"
   643   by uint_arith
   644 
   645 lemma word_less_sub_right: 
   646   "(x :: 'a :: len0 word) < y + z ==> y <= x ==> x - y < z"
   647   by uint_arith
   648 
   649 lemma word_le_plus_either: 
   650   "(x :: 'a :: len0 word) <= y | x <= z ==> y <= y + z ==> x <= y + z"
   651   by uint_arith
   652 
   653 lemma word_less_nowrapI: 
   654   "(x :: 'a :: len0 word) < z - k ==> k <= z ==> 0 < k ==> x < x + k"
   655   by uint_arith
   656 
   657 lemma inc_le: "(i :: 'a :: len word) < m ==> i + 1 <= m"
   658   by uint_arith
   659 
   660 lemma inc_i: 
   661   "(1 :: 'a :: len word) <= i ==> i < m ==> 1 <= (i + 1) & i + 1 <= m"
   662   by uint_arith
   663 
   664 lemma udvd_incr_lem:
   665   "up < uq ==> up = ua + n * uint K ==> 
   666     uq = ua + n' * uint K ==> up + uint K <= uq"
   667   apply clarsimp
   668   apply (drule less_le_mult)
   669   apply safe
   670   done
   671 
   672 lemma udvd_incr': 
   673   "p < q ==> uint p = ua + n * uint K ==> 
   674     uint q = ua + n' * uint K ==> p + K <= q" 
   675   apply (unfold word_less_alt word_le_def)
   676   apply (drule (2) udvd_incr_lem)
   677   apply (erule uint_add_le [THEN order_trans])
   678   done
   679 
   680 lemma udvd_decr': 
   681   "p < q ==> uint p = ua + n * uint K ==> 
   682     uint q = ua + n' * uint K ==> p <= q - K"
   683   apply (unfold word_less_alt word_le_def)
   684   apply (drule (2) udvd_incr_lem)
   685   apply (drule le_diff_eq [THEN iffD2])
   686   apply (erule order_trans)
   687   apply (rule uint_sub_ge)
   688   done
   689 
   690 lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, simplified]
   691 lemmas udvd_incr0 = udvd_incr' [where ua=0, simplified]
   692 lemmas udvd_decr0 = udvd_decr' [where ua=0, simplified]
   693 
   694 lemma udvd_minus_le': 
   695   "xy < k ==> z udvd xy ==> z udvd k ==> xy <= k - z"
   696   apply (unfold udvd_def)
   697   apply clarify
   698   apply (erule (2) udvd_decr0)
   699   done
   700 
   701 lemma udvd_incr2_K: 
   702   "p < a + s ==> a <= a + s ==> K udvd s ==> K udvd p - a ==> a <= p ==> 
   703     0 < K ==> p <= p + K & p + K <= a + s"
   704   apply (unfold udvd_def)
   705   apply clarify
   706   apply (simp add: uint_arith_simps split: split_if_asm)
   707    prefer 2 
   708    apply (insert uint_range' [of s])[1]
   709    apply arith
   710   apply (drule add_commute [THEN xtr1])
   711   apply (simp add: diff_less_eq [symmetric])
   712   apply (drule less_le_mult)
   713    apply arith
   714   apply simp
   715   done
   716 
   717 (* links with rbl operations *)
   718 lemma word_succ_rbl:
   719   "to_bl w = bl ==> to_bl (word_succ w) = (rev (rbl_succ (rev bl)))"
   720   apply (unfold word_succ_def)
   721   apply clarify
   722   apply (simp add: to_bl_of_bin)
   723   apply (simp add: to_bl_def rbl_succ)
   724   done
   725 
   726 lemma word_pred_rbl:
   727   "to_bl w = bl ==> to_bl (word_pred w) = (rev (rbl_pred (rev bl)))"
   728   apply (unfold word_pred_def)
   729   apply clarify
   730   apply (simp add: to_bl_of_bin)
   731   apply (simp add: to_bl_def rbl_pred)
   732   done
   733 
   734 lemma word_add_rbl:
   735   "to_bl v = vbl ==> to_bl w = wbl ==> 
   736     to_bl (v + w) = (rev (rbl_add (rev vbl) (rev wbl)))"
   737   apply (unfold word_add_def)
   738   apply clarify
   739   apply (simp add: to_bl_of_bin)
   740   apply (simp add: to_bl_def rbl_add)
   741   done
   742 
   743 lemma word_mult_rbl:
   744   "to_bl v = vbl ==> to_bl w = wbl ==> 
   745     to_bl (v * w) = (rev (rbl_mult (rev vbl) (rev wbl)))"
   746   apply (unfold word_mult_def)
   747   apply clarify
   748   apply (simp add: to_bl_of_bin)
   749   apply (simp add: to_bl_def rbl_mult)
   750   done
   751 
   752 lemma rtb_rbl_ariths:
   753   "rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_succ w)) = rbl_succ ys"
   754 
   755   "rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_pred w)) = rbl_pred ys"
   756 
   757   "[| rev (to_bl v) = ys; rev (to_bl w) = xs |] 
   758   ==> rev (to_bl (v * w)) = rbl_mult ys xs"
   759 
   760   "[| rev (to_bl v) = ys; rev (to_bl w) = xs |] 
   761   ==> rev (to_bl (v + w)) = rbl_add ys xs"
   762   by (auto simp: rev_swap [symmetric] word_succ_rbl 
   763                  word_pred_rbl word_mult_rbl word_add_rbl)
   764 
   765 
   766 subsection "Arithmetic type class instantiations"
   767 
   768 instance word :: (len0) comm_monoid_add ..
   769 
   770 instance word :: (len0) comm_monoid_mult
   771   apply (intro_classes)
   772    apply (simp add: word_mult_commute)
   773   apply (simp add: word_mult_1)
   774   done
   775 
   776 instance word :: (len0) comm_semiring 
   777   by (intro_classes) (simp add : word_left_distrib)
   778 
   779 instance word :: (len0) ab_group_add ..
   780 
   781 instance word :: (len0) comm_ring ..
   782 
   783 instance word :: (len) comm_semiring_1 
   784   by (intro_classes) (simp add: lenw1_zero_neq_one)
   785 
   786 instance word :: (len) comm_ring_1 ..
   787 
   788 instance word :: (len0) comm_semiring_0 ..
   789 
   790 instance word :: (len0) order ..
   791 
   792 instance word :: (len) recpower
   793   by (intro_classes) simp_all
   794 
   795 (* note that iszero_def is only for class comm_semiring_1_cancel,
   796    which requires word length >= 1, ie 'a :: len word *) 
   797 lemma zero_bintrunc:
   798   "iszero (number_of x :: 'a :: len word) = 
   799     (bintrunc (len_of TYPE('a)) x = Int.Pls)"
   800   apply (unfold iszero_def word_0_wi word_no_wi)
   801   apply (rule word_ubin.norm_eq_iff [symmetric, THEN trans])
   802   apply (simp add : Pls_def [symmetric])
   803   done
   804 
   805 lemmas word_le_0_iff [simp] =
   806   word_zero_le [THEN leD, THEN linorder_antisym_conv1]
   807 
   808 lemma word_of_nat: "of_nat n = word_of_int (int n)"
   809   by (induct n) (auto simp add : word_of_int_hom_syms)
   810 
   811 lemma word_of_int: "of_int = word_of_int"
   812   apply (rule ext)
   813   apply (unfold of_int_def)
   814   apply (rule contentsI)
   815   apply safe
   816   apply (simp_all add: word_of_nat word_of_int_homs)
   817    defer
   818    apply (rule Rep_Integ_ne [THEN nonemptyE])
   819    apply (rule bexI)
   820     prefer 2
   821     apply assumption
   822    apply (auto simp add: RI_eq_diff)
   823   done
   824 
   825 lemma word_of_int_nat: 
   826   "0 <= x ==> word_of_int x = of_nat (nat x)"
   827   by (simp add: of_nat_nat word_of_int)
   828 
   829 lemma word_number_of_eq: 
   830   "number_of w = (of_int w :: 'a :: len word)"
   831   unfolding word_number_of_def word_of_int by auto
   832 
   833 instance word :: (len) number_ring
   834   by (intro_classes) (simp add : word_number_of_eq)
   835 
   836 lemma iszero_word_no [simp] : 
   837   "iszero (number_of bin :: 'a :: len word) = 
   838     iszero (number_of (bintrunc (len_of TYPE('a)) bin) :: int)"
   839   apply (simp add: zero_bintrunc number_of_is_id)
   840   apply (unfold iszero_def Pls_def)
   841   apply (rule refl)
   842   done
   843     
   844 
   845 subsection "Word and nat"
   846 
   847 lemma td_ext_unat':
   848   "n = len_of TYPE ('a :: len) ==> 
   849     td_ext (unat :: 'a word => nat) of_nat 
   850     (unats n) (%i. i mod 2 ^ n)"
   851   apply (unfold td_ext_def' unat_def word_of_nat unats_uints)
   852   apply (auto intro!: imageI simp add : word_of_int_hom_syms)
   853   apply (erule word_uint.Abs_inverse [THEN arg_cong])
   854   apply (simp add: int_word_uint nat_mod_distrib nat_power_eq)
   855   done
   856 
   857 lemmas td_ext_unat = refl [THEN td_ext_unat']
   858 lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm, standard]
   859 
   860 interpretation word_unat!:
   861   td_ext "unat::'a::len word => nat" 
   862          of_nat 
   863          "unats (len_of TYPE('a::len))"
   864          "%i. i mod 2 ^ len_of TYPE('a::len)"
   865   by (rule td_ext_unat)
   866 
   867 lemmas td_unat = word_unat.td_thm
   868 
   869 lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq]
   870 
   871 lemma unat_le: "y <= unat (z :: 'a :: len word) ==> y : unats (len_of TYPE ('a))"
   872   apply (unfold unats_def)
   873   apply clarsimp
   874   apply (rule xtrans, rule unat_lt2p, assumption) 
   875   done
   876 
   877 lemma word_nchotomy:
   878   "ALL w. EX n. (w :: 'a :: len word) = of_nat n & n < 2 ^ len_of TYPE ('a)"
   879   apply (rule allI)
   880   apply (rule word_unat.Abs_cases)
   881   apply (unfold unats_def)
   882   apply auto
   883   done
   884 
   885 lemma of_nat_eq:
   886   fixes w :: "'a::len word"
   887   shows "(of_nat n = w) = (\<exists>q. n = unat w + q * 2 ^ len_of TYPE('a))"
   888   apply (rule trans)
   889    apply (rule word_unat.inverse_norm)
   890   apply (rule iffI)
   891    apply (rule mod_eqD)
   892    apply simp
   893   apply clarsimp
   894   done
   895 
   896 lemma of_nat_eq_size: 
   897   "(of_nat n = w) = (EX q. n = unat w + q * 2 ^ size w)"
   898   unfolding word_size by (rule of_nat_eq)
   899 
   900 lemma of_nat_0:
   901   "(of_nat m = (0::'a::len word)) = (\<exists>q. m = q * 2 ^ len_of TYPE('a))"
   902   by (simp add: of_nat_eq)
   903 
   904 lemmas of_nat_2p = mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]]
   905 
   906 lemma of_nat_gt_0: "of_nat k ~= 0 ==> 0 < k"
   907   by (cases k) auto
   908 
   909 lemma of_nat_neq_0: 
   910   "0 < k ==> k < 2 ^ len_of TYPE ('a :: len) ==> of_nat k ~= (0 :: 'a word)"
   911   by (clarsimp simp add : of_nat_0)
   912 
   913 lemma Abs_fnat_hom_add:
   914   "of_nat a + of_nat b = of_nat (a + b)"
   915   by simp
   916 
   917 lemma Abs_fnat_hom_mult:
   918   "of_nat a * of_nat b = (of_nat (a * b) :: 'a :: len word)"
   919   by (simp add: word_of_nat word_of_int_mult_hom zmult_int)
   920 
   921 lemma Abs_fnat_hom_Suc:
   922   "word_succ (of_nat a) = of_nat (Suc a)"
   923   by (simp add: word_of_nat word_of_int_succ_hom add_ac)
   924 
   925 lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0"
   926   by (simp add: word_of_nat word_0_wi)
   927 
   928 lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)"
   929   by (simp add: word_of_nat word_1_wi)
   930 
   931 lemmas Abs_fnat_homs = 
   932   Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc 
   933   Abs_fnat_hom_0 Abs_fnat_hom_1
   934 
   935 lemma word_arith_nat_add:
   936   "a + b = of_nat (unat a + unat b)" 
   937   by simp
   938 
   939 lemma word_arith_nat_mult:
   940   "a * b = of_nat (unat a * unat b)"
   941   by (simp add: Abs_fnat_hom_mult [symmetric])
   942     
   943 lemma word_arith_nat_Suc:
   944   "word_succ a = of_nat (Suc (unat a))"
   945   by (subst Abs_fnat_hom_Suc [symmetric]) simp
   946 
   947 lemma word_arith_nat_div:
   948   "a div b = of_nat (unat a div unat b)"
   949   by (simp add: word_div_def word_of_nat zdiv_int uint_nat)
   950 
   951 lemma word_arith_nat_mod:
   952   "a mod b = of_nat (unat a mod unat b)"
   953   by (simp add: word_mod_def word_of_nat zmod_int uint_nat)
   954 
   955 lemmas word_arith_nat_defs =
   956   word_arith_nat_add word_arith_nat_mult
   957   word_arith_nat_Suc Abs_fnat_hom_0
   958   Abs_fnat_hom_1 word_arith_nat_div
   959   word_arith_nat_mod 
   960 
   961 lemmas unat_cong = arg_cong [where f = "unat"]
   962   
   963 lemmas unat_word_ariths = word_arith_nat_defs
   964   [THEN trans [OF unat_cong unat_of_nat], standard]
   965 
   966 lemmas word_sub_less_iff = word_sub_le_iff
   967   [simplified linorder_not_less [symmetric], simplified]
   968 
   969 lemma unat_add_lem: 
   970   "(unat x + unat y < 2 ^ len_of TYPE('a)) = 
   971     (unat (x + y :: 'a :: len word) = unat x + unat y)"
   972   unfolding unat_word_ariths
   973   by (auto intro!: trans [OF _ nat_mod_lem])
   974 
   975 lemma unat_mult_lem: 
   976   "(unat x * unat y < 2 ^ len_of TYPE('a)) = 
   977     (unat (x * y :: 'a :: len word) = unat x * unat y)"
   978   unfolding unat_word_ariths
   979   by (auto intro!: trans [OF _ nat_mod_lem])
   980 
   981 lemmas unat_plus_if' = 
   982   trans [OF unat_word_ariths(1) mod_nat_add, simplified, standard]
   983 
   984 lemma le_no_overflow: 
   985   "x <= b ==> a <= a + b ==> x <= a + (b :: 'a :: len0 word)"
   986   apply (erule order_trans)
   987   apply (erule olen_add_eqv [THEN iffD1])
   988   done
   989 
   990 lemmas un_ui_le = trans 
   991   [OF word_le_nat_alt [symmetric] 
   992       word_le_def, 
   993    standard]
   994 
   995 lemma unat_sub_if_size:
   996   "unat (x - y) = (if unat y <= unat x 
   997    then unat x - unat y 
   998    else unat x + 2 ^ size x - unat y)"
   999   apply (unfold word_size)
  1000   apply (simp add: un_ui_le)
  1001   apply (auto simp add: unat_def uint_sub_if')
  1002    apply (rule nat_diff_distrib)
  1003     prefer 3
  1004     apply (simp add: algebra_simps)
  1005     apply (rule nat_diff_distrib [THEN trans])
  1006       prefer 3
  1007       apply (subst nat_add_distrib)
  1008         prefer 3
  1009         apply (simp add: nat_power_eq)
  1010        apply auto
  1011   apply uint_arith
  1012   done
  1013 
  1014 lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size]
  1015 
  1016 lemma unat_div: "unat ((x :: 'a :: len word) div y) = unat x div unat y"
  1017   apply (simp add : unat_word_ariths)
  1018   apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
  1019   apply (rule div_le_dividend)
  1020   done
  1021 
  1022 lemma unat_mod: "unat ((x :: 'a :: len word) mod y) = unat x mod unat y"
  1023   apply (clarsimp simp add : unat_word_ariths)
  1024   apply (cases "unat y")
  1025    prefer 2
  1026    apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
  1027    apply (rule mod_le_divisor)
  1028    apply auto
  1029   done
  1030 
  1031 lemma uint_div: "uint ((x :: 'a :: len word) div y) = uint x div uint y"
  1032   unfolding uint_nat by (simp add : unat_div zdiv_int)
  1033 
  1034 lemma uint_mod: "uint ((x :: 'a :: len word) mod y) = uint x mod uint y"
  1035   unfolding uint_nat by (simp add : unat_mod zmod_int)
  1036 
  1037 
  1038 subsection {* Definition of unat\_arith tactic *}
  1039 
  1040 lemma unat_split:
  1041   fixes x::"'a::len word"
  1042   shows "P (unat x) = 
  1043          (ALL n. of_nat n = x & n < 2^len_of TYPE('a) --> P n)"
  1044   by (auto simp: unat_of_nat)
  1045 
  1046 lemma unat_split_asm:
  1047   fixes x::"'a::len word"
  1048   shows "P (unat x) = 
  1049          (~(EX n. of_nat n = x & n < 2^len_of TYPE('a) & ~ P n))"
  1050   by (auto simp: unat_of_nat)
  1051 
  1052 lemmas of_nat_inverse = 
  1053   word_unat.Abs_inverse' [rotated, unfolded unats_def, simplified]
  1054 
  1055 lemmas unat_splits = unat_split unat_split_asm
  1056 
  1057 lemmas unat_arith_simps =
  1058   word_le_nat_alt word_less_nat_alt
  1059   word_unat.Rep_inject [symmetric]
  1060   unat_sub_if' unat_plus_if' unat_div unat_mod
  1061 
  1062 (* unat_arith_tac: tactic to reduce word arithmetic to nat, 
  1063    try to solve via arith *)
  1064 ML {*
  1065 fun unat_arith_ss_of ss = 
  1066   ss addsimps @{thms unat_arith_simps}
  1067      delsimps @{thms word_unat.Rep_inject}
  1068      addsplits @{thms split_if_asm}
  1069      addcongs @{thms power_False_cong}
  1070 
  1071 fun unat_arith_tacs ctxt =   
  1072   let fun arith_tac' n t = arith_tac ctxt n t handle COOPER => Seq.empty  
  1073   in 
  1074     [ CLASET' clarify_tac 1,
  1075       SIMPSET' (full_simp_tac o unat_arith_ss_of) 1,
  1076       ALLGOALS (full_simp_tac (HOL_ss addsplits @{thms unat_splits} 
  1077                                        addcongs @{thms power_False_cong})),
  1078       rewrite_goals_tac @{thms word_size}, 
  1079       ALLGOALS  (fn n => REPEAT (resolve_tac [allI, impI] n) THEN      
  1080                          REPEAT (etac conjE n) THEN
  1081                          REPEAT (dtac @{thm of_nat_inverse} n THEN atac n)),
  1082       TRYALL arith_tac' ] 
  1083   end
  1084 
  1085 fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt))
  1086 *}
  1087 
  1088 method_setup unat_arith = 
  1089   "Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD (unat_arith_tac ctxt 1))" 
  1090   "solving word arithmetic via natural numbers and arith"
  1091 
  1092 lemma no_plus_overflow_unat_size: 
  1093   "((x :: 'a :: len word) <= x + y) = (unat x + unat y < 2 ^ size x)" 
  1094   unfolding word_size by unat_arith
  1095 
  1096 lemma unat_sub: "b <= a ==> unat (a - b) = unat a - unat (b :: 'a :: len word)"
  1097   by unat_arith
  1098 
  1099 lemmas no_olen_add_nat = no_plus_overflow_unat_size [unfolded word_size]
  1100 
  1101 lemmas unat_plus_simple = trans [OF no_olen_add_nat unat_add_lem, standard]
  1102 
  1103 lemma word_div_mult: 
  1104   "(0 :: 'a :: len word) < y ==> unat x * unat y < 2 ^ len_of TYPE('a) ==> 
  1105     x * y div y = x"
  1106   apply unat_arith
  1107   apply clarsimp
  1108   apply (subst unat_mult_lem [THEN iffD1])
  1109   apply auto
  1110   done
  1111 
  1112 lemma div_lt': "(i :: 'a :: len word) <= k div x ==> 
  1113     unat i * unat x < 2 ^ len_of TYPE('a)"
  1114   apply unat_arith
  1115   apply clarsimp
  1116   apply (drule mult_le_mono1)
  1117   apply (erule order_le_less_trans)
  1118   apply (rule xtr7 [OF unat_lt2p div_mult_le])
  1119   done
  1120 
  1121 lemmas div_lt'' = order_less_imp_le [THEN div_lt']
  1122 
  1123 lemma div_lt_mult: "(i :: 'a :: len word) < k div x ==> 0 < x ==> i * x < k"
  1124   apply (frule div_lt'' [THEN unat_mult_lem [THEN iffD1]])
  1125   apply (simp add: unat_arith_simps)
  1126   apply (drule (1) mult_less_mono1)
  1127   apply (erule order_less_le_trans)
  1128   apply (rule div_mult_le)
  1129   done
  1130 
  1131 lemma div_le_mult: 
  1132   "(i :: 'a :: len word) <= k div x ==> 0 < x ==> i * x <= k"
  1133   apply (frule div_lt' [THEN unat_mult_lem [THEN iffD1]])
  1134   apply (simp add: unat_arith_simps)
  1135   apply (drule mult_le_mono1)
  1136   apply (erule order_trans)
  1137   apply (rule div_mult_le)
  1138   done
  1139 
  1140 lemma div_lt_uint': 
  1141   "(i :: 'a :: len word) <= k div x ==> uint i * uint x < 2 ^ len_of TYPE('a)"
  1142   apply (unfold uint_nat)
  1143   apply (drule div_lt')
  1144   apply (simp add: zmult_int zless_nat_eq_int_zless [symmetric] 
  1145                    nat_power_eq)
  1146   done
  1147 
  1148 lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint']
  1149 
  1150 lemma word_le_exists': 
  1151   "(x :: 'a :: len0 word) <= y ==> 
  1152     (EX z. y = x + z & uint x + uint z < 2 ^ len_of TYPE('a))"
  1153   apply (rule exI)
  1154   apply (rule conjI)
  1155   apply (rule zadd_diff_inverse)
  1156   apply uint_arith
  1157   done
  1158 
  1159 lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab]
  1160 
  1161 lemmas plus_minus_no_overflow =
  1162   order_less_imp_le [THEN plus_minus_no_overflow_ab]
  1163   
  1164 lemmas mcs = word_less_minus_cancel word_less_minus_mono_left
  1165   word_le_minus_cancel word_le_minus_mono_left
  1166 
  1167 lemmas word_l_diffs = mcs [where y = "w + x", unfolded add_diff_cancel, standard]
  1168 lemmas word_diff_ls = mcs [where z = "w + x", unfolded add_diff_cancel, standard]
  1169 lemmas word_plus_mcs = word_diff_ls 
  1170   [where y = "v + x", unfolded add_diff_cancel, standard]
  1171 
  1172 lemmas le_unat_uoi = unat_le [THEN word_unat.Abs_inverse]
  1173 
  1174 lemmas thd = refl [THEN [2] split_div_lemma [THEN iffD2], THEN conjunct1]
  1175 
  1176 lemma thd1:
  1177   "a div b * b \<le> (a::nat)"
  1178   using gt_or_eq_0 [of b]
  1179   apply (rule disjE)
  1180    apply (erule xtr4 [OF thd mult_commute])
  1181   apply clarsimp
  1182   done
  1183 
  1184 lemmas uno_simps [THEN le_unat_uoi, standard] =
  1185   mod_le_divisor div_le_dividend thd1 
  1186 
  1187 lemma word_mod_div_equality:
  1188   "(n div b) * b + (n mod b) = (n :: 'a :: len word)"
  1189   apply (unfold word_less_nat_alt word_arith_nat_defs)
  1190   apply (cut_tac y="unat b" in gt_or_eq_0)
  1191   apply (erule disjE)
  1192    apply (simp add: mod_div_equality uno_simps)
  1193   apply simp
  1194   done
  1195 
  1196 lemma word_div_mult_le: "a div b * b <= (a::'a::len word)"
  1197   apply (unfold word_le_nat_alt word_arith_nat_defs)
  1198   apply (cut_tac y="unat b" in gt_or_eq_0)
  1199   apply (erule disjE)
  1200    apply (simp add: div_mult_le uno_simps)
  1201   apply simp
  1202   done
  1203 
  1204 lemma word_mod_less_divisor: "0 < n ==> m mod n < (n :: 'a :: len word)"
  1205   apply (simp only: word_less_nat_alt word_arith_nat_defs)
  1206   apply (clarsimp simp add : uno_simps)
  1207   done
  1208 
  1209 lemma word_of_int_power_hom: 
  1210   "word_of_int a ^ n = (word_of_int (a ^ n) :: 'a :: len word)"
  1211   by (induct n) (simp_all add : word_of_int_hom_syms power_Suc)
  1212 
  1213 lemma word_arith_power_alt: 
  1214   "a ^ n = (word_of_int (uint a ^ n) :: 'a :: len word)"
  1215   by (simp add : word_of_int_power_hom [symmetric])
  1216 
  1217 lemma of_bl_length_less: 
  1218   "length x = k ==> k < len_of TYPE('a) ==> (of_bl x :: 'a :: len word) < 2 ^ k"
  1219   apply (unfold of_bl_no [unfolded word_number_of_def]
  1220                 word_less_alt word_number_of_alt)
  1221   apply safe
  1222   apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm 
  1223                        del: word_of_int_bin)
  1224   apply (simp add: mod_pos_pos_trivial)
  1225   apply (subst mod_pos_pos_trivial)
  1226     apply (rule bl_to_bin_ge0)
  1227    apply (rule order_less_trans)
  1228     apply (rule bl_to_bin_lt2p)
  1229    apply simp
  1230   apply (rule bl_to_bin_lt2p)    
  1231   done
  1232 
  1233 
  1234 subsection "Cardinality, finiteness of set of words"
  1235 
  1236 lemmas card_lessThan' = card_lessThan [unfolded lessThan_def]
  1237 
  1238 lemmas card_eq = word_unat.Abs_inj_on [THEN card_image,
  1239   unfolded word_unat.image, unfolded unats_def, standard]
  1240 
  1241 lemmas card_word = trans [OF card_eq card_lessThan', standard]
  1242 
  1243 lemma finite_word_UNIV: "finite (UNIV :: 'a :: len word set)"
  1244 apply (rule contrapos_np)
  1245  prefer 2
  1246  apply (erule card_infinite)
  1247 apply (simp add: card_word)
  1248 done
  1249 
  1250 lemma card_word_size: 
  1251   "card (UNIV :: 'a :: len word set) = (2 ^ size (x :: 'a word))"
  1252 unfolding word_size by (rule card_word)
  1253 
  1254 end