src/HOL/Word/WordGenLib.thy
changeset 24408 058c5613a86f
parent 24350 4d74f37c6367
child 24465 70f0214b3ecc
equal deleted inserted replaced
24407:61b10ffb2549 24408:058c5613a86f
    12 begin
    12 begin
    13 
    13 
    14 declare of_nat_2p [simp]
    14 declare of_nat_2p [simp]
    15 
    15 
    16 lemma word_int_cases:
    16 lemma word_int_cases:
    17   "\<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>
    17   "\<lbrakk>\<And>n. \<lbrakk>(x ::'a word) = word_of_int n; 0 \<le> n; n < 2^CARD('a)\<rbrakk> \<Longrightarrow> P\<rbrakk>
    18    \<Longrightarrow> P"
    18    \<Longrightarrow> P"
    19   by (cases x rule: word_uint.Abs_cases) (simp add: uints_num)
    19   by (cases x rule: word_uint.Abs_cases) (simp add: uints_num)
    20 
    20 
    21 lemma word_nat_cases [cases type: word]:
    21 lemma word_nat_cases [cases type: word]:
    22   "\<lbrakk>\<And>n. \<lbrakk>(x ::'a::len word) = of_nat n; n < 2^len_of TYPE('a)\<rbrakk> \<Longrightarrow> P\<rbrakk>
    22   "\<lbrakk>\<And>n. \<lbrakk>(x ::'a::finite word) = of_nat n; n < 2^CARD('a)\<rbrakk> \<Longrightarrow> P\<rbrakk>
    23    \<Longrightarrow> P"
    23    \<Longrightarrow> P"
    24   by (cases x rule: word_unat.Abs_cases) (simp add: unats_def)
    24   by (cases x rule: word_unat.Abs_cases) (simp add: unats_def)
    25 
    25 
    26 lemma max_word_eq:
    26 lemma max_word_eq:
    27   "(max_word::'a::len word) = 2^len_of TYPE('a) - 1"
    27   "(max_word::'a::finite word) = 2^CARD('a) - 1"
    28   by (simp add: max_word_def word_of_int_hom_syms word_of_int_2p)
    28   by (simp add: max_word_def word_of_int_hom_syms word_of_int_2p)
    29 
    29 
    30 lemma max_word_max [simp,intro!]:
    30 lemma max_word_max [simp,intro!]:
    31   "n \<le> max_word"
    31   "n \<le> max_word"
    32   by (cases n rule: word_int_cases)
    32   by (cases n rule: word_int_cases)
    33      (simp add: max_word_def word_le_def int_word_uint int_mod_eq')
    33      (simp add: max_word_def word_le_def int_word_uint int_mod_eq')
    34   
    34   
    35 lemma word_of_int_2p_len: 
    35 lemma word_of_int_2p_len: 
    36   "word_of_int (2 ^ len_of TYPE('a)) = (0::'a::len0 word)"
    36   "word_of_int (2 ^ CARD('a)) = (0::'a word)"
    37   by (subst word_uint.Abs_norm [symmetric]) 
    37   by (subst word_uint.Abs_norm [symmetric]) 
    38      (simp add: word_of_int_hom_syms)
    38      (simp add: word_of_int_hom_syms)
    39 
    39 
    40 lemma word_pow_0:
    40 lemma word_pow_0:
    41   "(2::'a::len word) ^ len_of TYPE('a) = 0"
    41   "(2::'a::finite word) ^ CARD('a) = 0"
    42 proof -
    42 proof -
    43   have "word_of_int (2 ^ len_of TYPE('a)) = (0::'a word)"
    43   have "word_of_int (2 ^ CARD('a)) = (0::'a word)"
    44     by (rule word_of_int_2p_len)
    44     by (rule word_of_int_2p_len)
    45   thus ?thesis by (simp add: word_of_int_2p)
    45   thus ?thesis by (simp add: word_of_int_2p)
    46 qed
    46 qed
    47 
    47 
    48 lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word"
    48 lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word"
    51   apply auto
    51   apply auto
    52   apply (simp add: word_pow_0)
    52   apply (simp add: word_pow_0)
    53   done
    53   done
    54 
    54 
    55 lemma max_word_minus: 
    55 lemma max_word_minus: 
    56   "max_word = (-1::'a::len word)"
    56   "max_word = (-1::'a::finite word)"
    57 proof -
    57 proof -
    58   have "-1 + 1 = (0::'a word)" by simp
    58   have "-1 + 1 = (0::'a word)" by simp
    59   thus ?thesis by (rule max_word_wrap [symmetric])
    59   thus ?thesis by (rule max_word_wrap [symmetric])
    60 qed
    60 qed
    61 
    61 
    62 lemma max_word_bl [simp]:
    62 lemma max_word_bl [simp]:
    63   "to_bl (max_word::'a::len word) = replicate (len_of TYPE('a)) True"
    63   "to_bl (max_word::'a::finite word) = replicate CARD('a) True"
    64   by (subst max_word_minus to_bl_n1)+ simp
    64   by (subst max_word_minus to_bl_n1)+ simp
    65 
    65 
    66 lemma max_test_bit [simp]:
    66 lemma max_test_bit [simp]:
    67   "(max_word::'a::len word) !! n = (n < len_of TYPE('a))"
    67   "(max_word::'a::finite word) !! n = (n < CARD('a))"
    68   by (auto simp add: test_bit_bl word_size)
    68   by (auto simp add: test_bit_bl word_size)
    69 
    69 
    70 lemma word_and_max [simp]:
    70 lemma word_and_max [simp]:
    71   "x AND max_word = x"
    71   "x AND max_word = x"
    72   by (rule word_eqI) (simp add: word_ops_nth_size word_size)
    72   by (rule word_eqI) (simp add: word_ops_nth_size word_size)
    74 lemma word_or_max [simp]:
    74 lemma word_or_max [simp]:
    75   "x OR max_word = max_word"
    75   "x OR max_word = max_word"
    76   by (rule word_eqI) (simp add: word_ops_nth_size word_size)
    76   by (rule word_eqI) (simp add: word_ops_nth_size word_size)
    77 
    77 
    78 lemma word_ao_dist2:
    78 lemma word_ao_dist2:
    79   "x AND (y OR z) = x AND y OR x AND (z::'a::len0 word)"
    79   "x AND (y OR z) = x AND y OR x AND (z::'a word)"
    80   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
    80   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
    81 
    81 
    82 lemma word_oa_dist2:
    82 lemma word_oa_dist2:
    83   "x OR y AND z = (x OR y) AND (x OR (z::'a::len0 word))"
    83   "x OR y AND z = (x OR y) AND (x OR (z::'a word))"
    84   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
    84   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
    85 
    85 
    86 lemma word_and_not [simp]:
    86 lemma word_and_not [simp]:
    87   "x AND NOT x = (0::'a::len0 word)"
    87   "x AND NOT x = (0::'a word)"
    88   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
    88   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
    89 
    89 
    90 lemma word_or_not [simp]:
    90 lemma word_or_not [simp]:
    91   "x OR NOT x = max_word"
    91   "x OR NOT x = max_word"
    92   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
    92   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
   109 interpretation word_bool_alg:
   109 interpretation word_bool_alg:
   110   boolean ["op AND" "op OR" bitNOT 0 max_word]
   110   boolean ["op AND" "op OR" bitNOT 0 max_word]
   111   by (rule word_boolean)
   111   by (rule word_boolean)
   112 
   112 
   113 lemma word_xor_and_or:
   113 lemma word_xor_and_or:
   114   "x XOR y = x AND NOT y OR NOT x AND (y::'a::len0 word)"
   114   "x XOR y = x AND NOT y OR NOT x AND (y::'a word)"
   115   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
   115   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
   116 
   116 
   117 interpretation word_bool_alg:
   117 interpretation word_bool_alg:
   118   boolean_xor ["op AND" "op OR" bitNOT 0 max_word "op XOR"]
   118   boolean_xor ["op AND" "op OR" bitNOT 0 max_word "op XOR"]
   119   apply (rule boolean_xor.intro)
   119   apply (rule boolean_xor.intro)
   121   apply (rule boolean_xor_axioms.intro)
   121   apply (rule boolean_xor_axioms.intro)
   122   apply (rule word_xor_and_or)
   122   apply (rule word_xor_and_or)
   123   done
   123   done
   124 
   124 
   125 lemma shiftr_0 [iff]:
   125 lemma shiftr_0 [iff]:
   126   "(x::'a::len0 word) >> 0 = x"
   126   "(x::'a word) >> 0 = x"
   127   by (simp add: shiftr_bl)
   127   by (simp add: shiftr_bl)
   128 
   128 
   129 lemma shiftl_0 [simp]: 
   129 lemma shiftl_0 [simp]: 
   130   "(x :: 'a :: len word) << 0 = x"
   130   "(x :: 'a :: finite word) << 0 = x"
   131   by (simp add: shiftl_t2n)
   131   by (simp add: shiftl_t2n)
   132 
   132 
   133 lemma shiftl_1 [simp]:
   133 lemma shiftl_1 [simp]:
   134   "(1::'a::len word) << n = 2^n"
   134   "(1::'a::finite word) << n = 2^n"
   135   by (simp add: shiftl_t2n)
   135   by (simp add: shiftl_t2n)
   136 
   136 
   137 lemma uint_lt_0 [simp]:
   137 lemma uint_lt_0 [simp]:
   138   "uint x < 0 = False"
   138   "uint x < 0 = False"
   139   by (simp add: linorder_not_less)
   139   by (simp add: linorder_not_less)
   140 
   140 
   141 lemma shiftr1_1 [simp]: 
   141 lemma shiftr1_1 [simp]: 
   142   "shiftr1 (1::'a::len word) = 0"
   142   "shiftr1 (1::'a::finite word) = 0"
   143   by (simp add: shiftr1_def word_0_alt)
   143   by (simp add: shiftr1_def word_0_alt)
   144 
   144 
   145 lemma shiftr_1[simp]: 
   145 lemma shiftr_1[simp]: 
   146   "(1::'a::len word) >> n = (if n = 0 then 1 else 0)"
   146   "(1::'a::finite word) >> n = (if n = 0 then 1 else 0)"
   147   by (induct n) (auto simp: shiftr_def)
   147   by (induct n) (auto simp: shiftr_def)
   148 
   148 
   149 lemma word_less_1 [simp]: 
   149 lemma word_less_1 [simp]: 
   150   "((x::'a::len word) < 1) = (x = 0)"
   150   "((x::'a::finite word) < 1) = (x = 0)"
   151   by (simp add: word_less_nat_alt unat_0_iff)
   151   by (simp add: word_less_nat_alt unat_0_iff)
   152 
   152 
   153 lemma to_bl_mask:
   153 lemma to_bl_mask:
   154   "to_bl (mask n :: 'a::len word) = 
   154   "to_bl (mask n :: 'a::finite word) = 
   155   replicate (len_of TYPE('a) - n) False @ 
   155   replicate (CARD('a) - n) False @ 
   156     replicate (min (len_of TYPE('a)) n) True"
   156     replicate (min CARD('a) n) True"
   157   by (simp add: mask_bl word_rep_drop min_def)
   157   by (simp add: mask_bl word_rep_drop min_def)
   158 
   158 
   159 lemma map_replicate_True:
   159 lemma map_replicate_True:
   160   "n = length xs ==>
   160   "n = length xs ==>
   161     map (\<lambda>(x,y). x & y) (zip xs (replicate n True)) = xs"
   161     map (\<lambda>(x,y). x & y) (zip xs (replicate n True)) = xs"
   165   "n = length xs ==> map (\<lambda>(x,y). x & y)
   165   "n = length xs ==> map (\<lambda>(x,y). x & y)
   166     (zip xs (replicate n False)) = replicate n False"
   166     (zip xs (replicate n False)) = replicate n False"
   167   by (induct xs arbitrary: n) auto
   167   by (induct xs arbitrary: n) auto
   168 
   168 
   169 lemma bl_and_mask:
   169 lemma bl_and_mask:
   170   fixes w :: "'a::len word"
   170   fixes w :: "'a::finite word"
   171   fixes n
   171   fixes n
   172   defines "n' \<equiv> len_of TYPE('a) - n"
   172   defines "n' \<equiv> CARD('a) - n"
   173   shows "to_bl (w AND mask n) =  replicate n' False @ drop n' (to_bl w)"
   173   shows "to_bl (w AND mask n) =  replicate n' False @ drop n' (to_bl w)"
   174 proof - 
   174 proof - 
   175   note [simp] = map_replicate_True map_replicate_False
   175   note [simp] = map_replicate_True map_replicate_False
   176   have "to_bl (w AND mask n) = 
   176   have "to_bl (w AND mask n) = 
   177         app2 op & (to_bl w) (to_bl (mask n::'a::len word))"
   177         app2 op & (to_bl w) (to_bl (mask n::'a::finite word))"
   178     by (simp add: bl_word_and)
   178     by (simp add: bl_word_and)
   179   also
   179   also
   180   have "to_bl w = take n' (to_bl w) @ drop n' (to_bl w)" by simp
   180   have "to_bl w = take n' (to_bl w) @ drop n' (to_bl w)" by simp
   181   also
   181   also
   182   have "app2 op & \<dots> (to_bl (mask n::'a::len word)) = 
   182   have "app2 op & \<dots> (to_bl (mask n::'a::finite word)) = 
   183         replicate n' False @ drop n' (to_bl w)"
   183         replicate n' False @ drop n' (to_bl w)"
   184     unfolding to_bl_mask n'_def app2_def
   184     unfolding to_bl_mask n'_def app2_def
   185     by (subst zip_append) auto
   185     by (subst zip_append) auto
   186   finally
   186   finally
   187   show ?thesis .
   187   show ?thesis .
   191   "length xs \<le> n ==>
   191   "length xs \<le> n ==>
   192     drop (n - length xs) (rev (takefill False n (rev xs))) = xs"
   192     drop (n - length xs) (rev (takefill False n (rev xs))) = xs"
   193   by (simp add: takefill_alt rev_take)
   193   by (simp add: takefill_alt rev_take)
   194 
   194 
   195 lemma map_nth_0 [simp]:
   195 lemma map_nth_0 [simp]:
   196   "map (op !! (0::'a::len0 word)) xs = replicate (length xs) False"
   196   "map (op !! (0::'a word)) xs = replicate (length xs) False"
   197   by (induct xs) auto
   197   by (induct xs) auto
   198 
   198 
   199 lemma uint_plus_if_size:
   199 lemma uint_plus_if_size:
   200   "uint (x + y) = 
   200   "uint (x + y) = 
   201   (if uint x + uint y < 2^size x then 
   201   (if uint x + uint y < 2^size x then 
   204       uint x + uint y - 2^size x)" 
   204       uint x + uint y - 2^size x)" 
   205   by (simp add: word_arith_alts int_word_uint mod_add_if_z 
   205   by (simp add: word_arith_alts int_word_uint mod_add_if_z 
   206                 word_size)
   206                 word_size)
   207 
   207 
   208 lemma unat_plus_if_size:
   208 lemma unat_plus_if_size:
   209   "unat (x + (y::'a::len word)) = 
   209   "unat (x + (y::'a::finite word)) = 
   210   (if unat x + unat y < 2^size x then 
   210   (if unat x + unat y < 2^size x then 
   211       unat x + unat y 
   211       unat x + unat y 
   212    else 
   212    else 
   213       unat x + unat y - 2^size x)" 
   213       unat x + unat y - 2^size x)" 
   214   apply (subst word_arith_nat_defs)
   214   apply (subst word_arith_nat_defs)
   215   apply (subst unat_of_nat)
   215   apply (subst unat_of_nat)
   216   apply (simp add:  mod_nat_add word_size)
   216   apply (simp add:  mod_nat_add word_size)
   217   done
   217   done
   218 
   218 
   219 lemma word_neq_0_conv [simp]:
   219 lemma word_neq_0_conv [simp]:
   220   fixes w :: "'a :: len word"
   220   fixes w :: "'a :: finite word"
   221   shows "(w \<noteq> 0) = (0 < w)"
   221   shows "(w \<noteq> 0) = (0 < w)"
   222 proof -
   222 proof -
   223   have "0 \<le> w" by (rule word_zero_le)
   223   have "0 \<le> w" by (rule word_zero_le)
   224   thus ?thesis by (auto simp add: word_less_def)
   224   thus ?thesis by (auto simp add: word_less_def)
   225 qed
   225 qed
   226 
   226 
   227 lemma max_lt:
   227 lemma max_lt:
   228   "unat (max a b div c) = unat (max a b) div unat (c:: 'a :: len word)"
   228   "unat (max a b div c) = unat (max a b) div unat (c:: 'a :: finite word)"
   229   apply (subst word_arith_nat_defs)
   229   apply (subst word_arith_nat_defs)
   230   apply (subst word_unat.eq_norm)
   230   apply (subst word_unat.eq_norm)
   231   apply (subst mod_if)
   231   apply (subst mod_if)
   232   apply clarsimp
   232   apply clarsimp
   233   apply (erule notE)
   233   apply (erule notE)
   251   by (simp add: unat_def uint_sub_if_size word_le_def nat_diff_distrib)
   251   by (simp add: unat_def uint_sub_if_size word_le_def nat_diff_distrib)
   252 
   252 
   253 lemmas unat_sub = unat_sub_simple
   253 lemmas unat_sub = unat_sub_simple
   254 
   254 
   255 lemma word_less_sub1:
   255 lemma word_less_sub1:
   256   fixes x :: "'a :: len word"
   256   fixes x :: "'a :: finite word"
   257   shows "x \<noteq> 0 ==> 1 < x = (0 < x - 1)"
   257   shows "x \<noteq> 0 ==> 1 < x = (0 < x - 1)"
   258   by (simp add: unat_sub_if_size word_less_nat_alt)
   258   by (simp add: unat_sub_if_size word_less_nat_alt)
   259 
   259 
   260 lemma word_le_sub1:
   260 lemma word_le_sub1:
   261   fixes x :: "'a :: len word"
   261   fixes x :: "'a :: finite word"
   262   shows "x \<noteq> 0 ==> 1 \<le> x = (0 \<le> x - 1)"
   262   shows "x \<noteq> 0 ==> 1 \<le> x = (0 \<le> x - 1)"
   263   by (simp add: unat_sub_if_size order_le_less word_less_nat_alt)
   263   by (simp add: unat_sub_if_size order_le_less word_less_nat_alt)
   264 
   264 
   265 lemmas word_less_sub1_numberof [simp] =
   265 lemmas word_less_sub1_numberof [simp] =
   266   word_less_sub1 [of "number_of ?w"]
   266   word_less_sub1 [of "number_of ?w"]
   267 lemmas word_le_sub1_numberof [simp] =
   267 lemmas word_le_sub1_numberof [simp] =
   268   word_le_sub1 [of "number_of ?w"]
   268   word_le_sub1 [of "number_of ?w"]
   269   
   269   
   270 lemma word_of_int_minus: 
   270 lemma word_of_int_minus: 
   271   "word_of_int (2^len_of TYPE('a) - i) = (word_of_int (-i)::'a::len word)"
   271   "word_of_int (2^CARD('a) - i) = (word_of_int (-i)::'a::finite word)"
   272 proof -
   272 proof -
   273   have x: "2^len_of TYPE('a) - i = -i + 2^len_of TYPE('a)" by simp
   273   have x: "2^CARD('a) - i = -i + 2^CARD('a)" by simp
   274   show ?thesis
   274   show ?thesis
   275     apply (subst x)
   275     apply (subst x)
   276     apply (subst word_uint.Abs_norm [symmetric], subst zmod_zadd_self2)
   276     apply (subst word_uint.Abs_norm [symmetric], subst zmod_zadd_self2)
   277     apply simp
   277     apply simp
   278     done
   278     done
   280   
   280   
   281 lemmas word_of_int_inj = 
   281 lemmas word_of_int_inj = 
   282   word_uint.Abs_inject [unfolded uints_num, simplified]
   282   word_uint.Abs_inject [unfolded uints_num, simplified]
   283 
   283 
   284 lemma word_le_less_eq:
   284 lemma word_le_less_eq:
   285   "(x ::'z::len word) \<le> y = (x = y \<or> x < y)"
   285   "(x ::'z::finite word) \<le> y = (x = y \<or> x < y)"
   286   by (auto simp add: word_less_def)
   286   by (auto simp add: word_less_def)
   287 
   287 
   288 lemma mod_plus_cong:
   288 lemma mod_plus_cong:
   289   assumes 1: "(b::int) = b'"
   289   assumes 1: "(b::int) = b'"
   290       and 2: "x mod b' = x' mod b'"
   290       and 2: "x mod b' = x' mod b'"
   310   apply (subst zmod_zsub_right_eq)
   310   apply (subst zmod_zsub_right_eq)
   311   apply (simp add: zmod_zsub_left_eq [symmetric] zmod_zsub_right_eq [symmetric])
   311   apply (simp add: zmod_zsub_left_eq [symmetric] zmod_zsub_right_eq [symmetric])
   312   done
   312   done
   313 
   313 
   314 lemma word_induct_less: 
   314 lemma word_induct_less: 
   315   "\<lbrakk>P (0::'a::len word); \<And>n. \<lbrakk>n < m; P n\<rbrakk> \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P m"
   315   "\<lbrakk>P (0::'a::finite word); \<And>n. \<lbrakk>n < m; P n\<rbrakk> \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P m"
   316   apply (cases m)
   316   apply (cases m)
   317   apply atomize
   317   apply atomize
   318   apply (erule rev_mp)+
   318   apply (erule rev_mp)+
   319   apply (rule_tac x=m in spec)
   319   apply (rule_tac x=m in spec)
   320   apply (induct_tac n)
   320   apply (induct_tac n)
   333    apply (clarsimp simp: unat_of_nat)
   333    apply (clarsimp simp: unat_of_nat)
   334   apply simp
   334   apply simp
   335   done
   335   done
   336   
   336   
   337 lemma word_induct: 
   337 lemma word_induct: 
   338   "\<lbrakk>P (0::'a::len word); \<And>n. P n \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P m"
   338   "\<lbrakk>P (0::'a::finite word); \<And>n. P n \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P m"
   339   by (erule word_induct_less, simp)
   339   by (erule word_induct_less, simp)
   340 
   340 
   341 lemma word_induct2 [induct type]: 
   341 lemma word_induct2 [induct type]: 
   342   "\<lbrakk>P 0; \<And>n. \<lbrakk>1 + n \<noteq> 0; P n\<rbrakk> \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P (n::'b::len word)"
   342   "\<lbrakk>P 0; \<And>n. \<lbrakk>1 + n \<noteq> 0; P n\<rbrakk> \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P (n::'b::finite word)"
   343   apply (rule word_induct, simp)
   343   apply (rule word_induct, simp)
   344   apply (case_tac "1+n = 0", auto)
   344   apply (case_tac "1+n = 0", auto)
   345   done
   345   done
   346 
   346 
   347 constdefs
   347 constdefs
   348   word_rec :: "'a \<Rightarrow> ('b::len word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a"
   348   word_rec :: "'a \<Rightarrow> ('b::finite word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a"
   349   "word_rec forZero forSuc n \<equiv> nat_rec forZero (forSuc \<circ> of_nat) (unat n)"
   349   "word_rec forZero forSuc n \<equiv> nat_rec forZero (forSuc \<circ> of_nat) (unat n)"
   350 
   350 
   351 lemma word_rec_0: "word_rec z s 0 = z"
   351 lemma word_rec_0: "word_rec z s 0 = z"
   352   by (simp add: word_rec_def)
   352   by (simp add: word_rec_def)
   353 
   353 
   354 lemma word_rec_Suc: 
   354 lemma word_rec_Suc: 
   355   "1 + n \<noteq> (0::'a::len word) \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)"
   355   "1 + n \<noteq> (0::'a::finite word) \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)"
   356   apply (simp add: word_rec_def unat_word_ariths)
   356   apply (simp add: word_rec_def unat_word_ariths)
   357   apply (subst nat_mod_eq')
   357   apply (subst nat_mod_eq')
   358    apply (cut_tac x=n in unat_lt2p)
   358    apply (cut_tac x=n in unat_lt2p)
   359    apply (drule Suc_mono)
   359    apply (drule Suc_mono)
   360    apply (simp add: less_Suc_eq_le)
   360    apply (simp add: less_Suc_eq_le)
   446 apply (drule arg_cong[where f="\<lambda>x. x - n"])
   446 apply (drule arg_cong[where f="\<lambda>x. x - n"])
   447 apply simp
   447 apply simp
   448 done
   448 done
   449 
   449 
   450 lemma unatSuc: 
   450 lemma unatSuc: 
   451   "1 + n \<noteq> (0::'a::len word) \<Longrightarrow> unat (1 + n) = Suc (unat n)"
   451   "1 + n \<noteq> (0::'a::finite word) \<Longrightarrow> unat (1 + n) = Suc (unat n)"
   452   by unat_arith
   452   by unat_arith
   453 
   453 
   454 end
   454 end