src/HOL/Word/Word.thy
author wenzelm
Wed Mar 15 19:33:34 2017 +0100 (2017-03-15)
changeset 65268 75f2aa8ecb12
parent 64593 50c715579715
child 65328 2510b0ce28da
permissions -rw-r--r--
misc tuning and modernization;
haftmann@29628
     1
(*  Title:      HOL/Word/Word.thy
wenzelm@46124
     2
    Author:     Jeremy Dawson and Gerwin Klein, NICTA
kleing@24333
     3
*)
kleing@24333
     4
wenzelm@61799
     5
section \<open>A type of finite bit strings\<close>
huffman@24350
     6
haftmann@29628
     7
theory Word
wenzelm@41413
     8
imports
wenzelm@63762
     9
  "~~/src/HOL/Library/Type_Length"
wenzelm@41413
    10
  "~~/src/HOL/Library/Boolean_Algebra"
haftmann@54854
    11
  Bits_Bit
wenzelm@41413
    12
  Bool_List_Representation
haftmann@53062
    13
  Misc_Typedef
haftmann@53062
    14
  Word_Miscellaneous
haftmann@37660
    15
begin
haftmann@37660
    16
wenzelm@63680
    17
text \<open>See \<^file>\<open>Examples/WordExamples.thy\<close> for examples.\<close>
wenzelm@61799
    18
wenzelm@61799
    19
subsection \<open>Type definition\<close>
haftmann@37660
    20
wenzelm@61260
    21
typedef (overloaded) 'a word = "{(0::int) ..< 2 ^ len_of TYPE('a::len0)}"
haftmann@37660
    22
  morphisms uint Abs_word by auto
haftmann@37660
    23
wenzelm@65268
    24
lemma uint_nonnegative: "0 \<le> uint w"
huffman@47108
    25
  using word.uint [of w] by simp
huffman@47108
    26
wenzelm@65268
    27
lemma uint_bounded: "uint w < 2 ^ len_of TYPE('a)"
wenzelm@65268
    28
  for w :: "'a::len0 word"
huffman@47108
    29
  using word.uint [of w] by simp
huffman@47108
    30
wenzelm@65268
    31
lemma uint_idem: "uint w mod 2 ^ len_of TYPE('a) = uint w"
wenzelm@65268
    32
  for w :: "'a::len0 word"
huffman@47108
    33
  using uint_nonnegative uint_bounded by (rule mod_pos_pos_trivial)
huffman@47108
    34
wenzelm@65268
    35
lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b"
haftmann@55816
    36
  by (simp add: uint_inject)
haftmann@55816
    37
wenzelm@65268
    38
lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b"
haftmann@55816
    39
  by (simp add: word_uint_eq_iff)
haftmann@55816
    40
wenzelm@61076
    41
definition word_of_int :: "int \<Rightarrow> 'a::len0 word"
wenzelm@61799
    42
  \<comment> \<open>representation of words using unsigned or signed bins,
wenzelm@61799
    43
    only difference in these is the type class\<close>
wenzelm@65268
    44
  where "word_of_int k = Abs_word (k mod 2 ^ len_of TYPE('a))"
wenzelm@65268
    45
wenzelm@65268
    46
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len0 word) = k mod 2 ^ len_of TYPE('a)"
huffman@47108
    47
  by (auto simp add: word_of_int_def intro: Abs_word_inverse)
huffman@47108
    48
wenzelm@65268
    49
lemma word_of_int_uint: "word_of_int (uint w) = w"
huffman@47108
    50
  by (simp add: word_of_int_def uint_idem uint_inverse)
huffman@47108
    51
wenzelm@65268
    52
lemma split_word_all: "(\<And>x::'a::len0 word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))"
haftmann@55816
    53
proof
haftmann@55816
    54
  fix x :: "'a word"
haftmann@55816
    55
  assume "\<And>x. PROP P (word_of_int x)"
haftmann@55816
    56
  then have "PROP P (word_of_int (uint x))" .
haftmann@55816
    57
  then show "PROP P x" by (simp add: word_of_int_uint)
haftmann@55816
    58
qed
haftmann@55816
    59
haftmann@55816
    60
wenzelm@61799
    61
subsection \<open>Type conversions and casting\<close>
haftmann@55816
    62
haftmann@55816
    63
definition sint :: "'a::len word \<Rightarrow> int"
wenzelm@61799
    64
  \<comment> \<open>treats the most-significant-bit as a sign bit\<close>
wenzelm@65268
    65
  where sint_uint: "sint w = sbintrunc (len_of TYPE('a) - 1) (uint w)"
haftmann@55816
    66
haftmann@55816
    67
definition unat :: "'a::len0 word \<Rightarrow> nat"
wenzelm@65268
    68
  where "unat w = nat (uint w)"
haftmann@55816
    69
haftmann@55816
    70
definition uints :: "nat \<Rightarrow> int set"
wenzelm@61799
    71
  \<comment> "the sets of integers representing the words"
wenzelm@65268
    72
  where "uints n = range (bintrunc n)"
haftmann@55816
    73
haftmann@55816
    74
definition sints :: "nat \<Rightarrow> int set"
wenzelm@65268
    75
  where "sints n = range (sbintrunc (n - 1))"
wenzelm@65268
    76
wenzelm@65268
    77
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
haftmann@55816
    78
  by (simp add: uints_def range_bintrunc)
haftmann@55816
    79
wenzelm@65268
    80
lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}"
haftmann@55816
    81
  by (simp add: sints_def range_sbintrunc)
haftmann@55816
    82
haftmann@55816
    83
definition unats :: "nat \<Rightarrow> nat set"
wenzelm@65268
    84
  where "unats n = {i. i < 2 ^ n}"
haftmann@55816
    85
haftmann@55816
    86
definition norm_sint :: "nat \<Rightarrow> int \<Rightarrow> int"
wenzelm@65268
    87
  where "norm_sint n w = (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)"
haftmann@55816
    88
haftmann@55816
    89
definition scast :: "'a::len word \<Rightarrow> 'b::len word"
wenzelm@61799
    90
  \<comment> "cast a word to a different length"
wenzelm@65268
    91
  where "scast w = word_of_int (sint w)"
haftmann@55816
    92
haftmann@55816
    93
definition ucast :: "'a::len0 word \<Rightarrow> 'b::len0 word"
wenzelm@65268
    94
  where "ucast w = word_of_int (uint w)"
haftmann@55816
    95
haftmann@55816
    96
instantiation word :: (len0) size
haftmann@55816
    97
begin
haftmann@55816
    98
wenzelm@65268
    99
definition word_size: "size (w :: 'a word) = len_of TYPE('a)"
haftmann@55816
   100
haftmann@55816
   101
instance ..
haftmann@55816
   102
haftmann@55816
   103
end
haftmann@55816
   104
wenzelm@65268
   105
lemma word_size_gt_0 [iff]: "0 < size w"
wenzelm@65268
   106
  for w :: "'a::len word"
haftmann@55816
   107
  by (simp add: word_size)
haftmann@55816
   108
haftmann@55816
   109
lemmas lens_gt_0 = word_size_gt_0 len_gt_0
haftmann@55816
   110
haftmann@55816
   111
lemma lens_not_0 [iff]:
wenzelm@65268
   112
  fixes w :: "'a::len word"
wenzelm@65268
   113
  shows "size w \<noteq> 0"
wenzelm@65268
   114
  and "len_of TYPE('a) \<noteq> 0"
haftmann@55816
   115
  by auto
haftmann@55816
   116
haftmann@55816
   117
definition source_size :: "('a::len0 word \<Rightarrow> 'b) \<Rightarrow> nat"
wenzelm@61799
   118
  \<comment> "whether a cast (or other) function is to a longer or shorter length"
wenzelm@65268
   119
  where [code del]: "source_size c = (let arb = undefined; x = c arb in size arb)"
haftmann@55816
   120
haftmann@55816
   121
definition target_size :: "('a \<Rightarrow> 'b::len0 word) \<Rightarrow> nat"
wenzelm@65268
   122
  where [code del]: "target_size c = size (c undefined)"
haftmann@55816
   123
haftmann@55816
   124
definition is_up :: "('a::len0 word \<Rightarrow> 'b::len0 word) \<Rightarrow> bool"
wenzelm@65268
   125
  where "is_up c \<longleftrightarrow> source_size c \<le> target_size c"
wenzelm@65268
   126
wenzelm@65268
   127
definition is_down :: "('a::len0 word \<Rightarrow> 'b::len0 word) \<Rightarrow> bool"
wenzelm@65268
   128
  where "is_down c \<longleftrightarrow> target_size c \<le> source_size c"
haftmann@55816
   129
haftmann@55816
   130
definition of_bl :: "bool list \<Rightarrow> 'a::len0 word"
wenzelm@65268
   131
  where "of_bl bl = word_of_int (bl_to_bin bl)"
haftmann@55816
   132
haftmann@55816
   133
definition to_bl :: "'a::len0 word \<Rightarrow> bool list"
wenzelm@65268
   134
  where "to_bl w = bin_to_bl (len_of TYPE('a)) (uint w)"
haftmann@55816
   135
haftmann@55816
   136
definition word_reverse :: "'a::len0 word \<Rightarrow> 'a word"
wenzelm@65268
   137
  where "word_reverse w = of_bl (rev (to_bl w))"
wenzelm@65268
   138
wenzelm@65268
   139
definition word_int_case :: "(int \<Rightarrow> 'b) \<Rightarrow> 'a::len0 word \<Rightarrow> 'b"
wenzelm@65268
   140
  where "word_int_case f w = f (uint w)"
haftmann@55816
   141
haftmann@55816
   142
translations
wenzelm@65268
   143
  "case x of XCONST of_int y \<Rightarrow> b" \<rightleftharpoons> "CONST word_int_case (\<lambda>y. b) x"
wenzelm@65268
   144
  "case x of (XCONST of_int :: 'a) y \<Rightarrow> b" \<rightharpoonup> "CONST word_int_case (\<lambda>y. b) x"
haftmann@55816
   145
haftmann@55816
   146
wenzelm@61799
   147
subsection \<open>Correspondence relation for theorem transfer\<close>
haftmann@55817
   148
haftmann@55817
   149
definition cr_word :: "int \<Rightarrow> 'a::len0 word \<Rightarrow> bool"
wenzelm@65268
   150
  where "cr_word = (\<lambda>x y. word_of_int x = y)"
haftmann@55817
   151
haftmann@55817
   152
lemma Quotient_word:
haftmann@55817
   153
  "Quotient (\<lambda>x y. bintrunc (len_of TYPE('a)) x = bintrunc (len_of TYPE('a)) y)
haftmann@55817
   154
    word_of_int uint (cr_word :: _ \<Rightarrow> 'a::len0 word \<Rightarrow> bool)"
haftmann@55817
   155
  unfolding Quotient_alt_def cr_word_def
haftmann@55817
   156
  by (simp add: no_bintr_alt1 word_of_int_uint) (simp add: word_of_int_def Abs_word_inject)
haftmann@55817
   157
haftmann@55817
   158
lemma reflp_word:
haftmann@55817
   159
  "reflp (\<lambda>x y. bintrunc (len_of TYPE('a::len0)) x = bintrunc (len_of TYPE('a)) y)"
haftmann@55817
   160
  by (simp add: reflp_def)
haftmann@55817
   161
haftmann@59487
   162
setup_lifting Quotient_word reflp_word
haftmann@55817
   163
wenzelm@61799
   164
text \<open>TODO: The next lemma could be generated automatically.\<close>
haftmann@55817
   165
haftmann@55817
   166
lemma uint_transfer [transfer_rule]:
wenzelm@65268
   167
  "(rel_fun pcr_word op =) (bintrunc (len_of TYPE('a))) (uint :: 'a::len0 word \<Rightarrow> int)"
blanchet@55945
   168
  unfolding rel_fun_def word.pcr_cr_eq cr_word_def
haftmann@55817
   169
  by (simp add: no_bintr_alt1 uint_word_of_int)
haftmann@55817
   170
haftmann@55817
   171
wenzelm@61799
   172
subsection \<open>Basic code generation setup\<close>
haftmann@55817
   173
haftmann@55817
   174
definition Word :: "int \<Rightarrow> 'a::len0 word"
wenzelm@65268
   175
  where [code_post]: "Word = word_of_int"
wenzelm@65268
   176
wenzelm@65268
   177
lemma [code abstype]: "Word (uint w) = w"
haftmann@55817
   178
  by (simp add: Word_def word_of_int_uint)
haftmann@55817
   179
haftmann@55817
   180
declare uint_word_of_int [code abstract]
haftmann@55817
   181
haftmann@55817
   182
instantiation word :: (len0) equal
haftmann@55817
   183
begin
haftmann@55817
   184
haftmann@55817
   185
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
wenzelm@65268
   186
  where "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)"
wenzelm@65268
   187
wenzelm@65268
   188
instance
wenzelm@65268
   189
  by standard (simp add: equal equal_word_def word_uint_eq_iff)
haftmann@55817
   190
haftmann@55817
   191
end
haftmann@55817
   192
haftmann@55817
   193
notation fcomp (infixl "\<circ>>" 60)
haftmann@55817
   194
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@55817
   195
haftmann@55817
   196
instantiation word :: ("{len0, typerep}") random
haftmann@55817
   197
begin
haftmann@55817
   198
haftmann@55817
   199
definition
haftmann@55817
   200
  "random_word i = Random.range i \<circ>\<rightarrow> (\<lambda>k. Pair (
haftmann@55817
   201
     let j = word_of_int (int_of_integer (integer_of_natural k)) :: 'a word
haftmann@55817
   202
     in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))"
haftmann@55817
   203
haftmann@55817
   204
instance ..
haftmann@55817
   205
haftmann@55817
   206
end
haftmann@55817
   207
haftmann@55817
   208
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@55817
   209
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@55817
   210
haftmann@55817
   211
wenzelm@61799
   212
subsection \<open>Type-definition locale instantiations\<close>
haftmann@55816
   213
haftmann@55816
   214
lemmas uint_0 = uint_nonnegative (* FIXME duplicate *)
haftmann@55816
   215
lemmas uint_lt = uint_bounded (* FIXME duplicate *)
haftmann@55816
   216
lemmas uint_mod_same = uint_idem (* FIXME duplicate *)
haftmann@55816
   217
wenzelm@65268
   218
lemma td_ext_uint:
wenzelm@65268
   219
  "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (len_of TYPE('a::len0)))
haftmann@55816
   220
    (\<lambda>w::int. w mod 2 ^ len_of TYPE('a))"
haftmann@55816
   221
  apply (unfold td_ext_def')
haftmann@55816
   222
  apply (simp add: uints_num word_of_int_def bintrunc_mod2p)
haftmann@55816
   223
  apply (simp add: uint_mod_same uint_0 uint_lt
haftmann@55816
   224
                   word.uint_inverse word.Abs_word_inverse int_mod_lem)
haftmann@55816
   225
  done
haftmann@55816
   226
haftmann@55816
   227
interpretation word_uint:
wenzelm@65268
   228
  td_ext
wenzelm@65268
   229
    "uint::'a::len0 word \<Rightarrow> int"
wenzelm@65268
   230
    word_of_int
wenzelm@65268
   231
    "uints (len_of TYPE('a::len0))"
wenzelm@65268
   232
    "\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)"
haftmann@55816
   233
  by (fact td_ext_uint)
haftmann@55816
   234
haftmann@55816
   235
lemmas td_uint = word_uint.td_thm
haftmann@55816
   236
lemmas int_word_uint = word_uint.eq_norm
haftmann@55816
   237
haftmann@55816
   238
lemma td_ext_ubin:
haftmann@55816
   239
  "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (len_of TYPE('a::len0)))
haftmann@55816
   240
    (bintrunc (len_of TYPE('a)))"
haftmann@55816
   241
  by (unfold no_bintr_alt1) (fact td_ext_uint)
haftmann@55816
   242
haftmann@55816
   243
interpretation word_ubin:
wenzelm@65268
   244
  td_ext
wenzelm@65268
   245
    "uint::'a::len0 word \<Rightarrow> int"
wenzelm@65268
   246
    word_of_int
wenzelm@65268
   247
    "uints (len_of TYPE('a::len0))"
wenzelm@65268
   248
    "bintrunc (len_of TYPE('a::len0))"
haftmann@55816
   249
  by (fact td_ext_ubin)
haftmann@55816
   250
haftmann@55816
   251
wenzelm@61799
   252
subsection \<open>Arithmetic operations\<close>
haftmann@37660
   253
huffman@47387
   254
lift_definition word_succ :: "'a::len0 word \<Rightarrow> 'a word" is "\<lambda>x. x + 1"
haftmann@64593
   255
  by (auto simp add: bintrunc_mod2p intro: mod_add_cong)
huffman@47374
   256
huffman@47387
   257
lift_definition word_pred :: "'a::len0 word \<Rightarrow> 'a word" is "\<lambda>x. x - 1"
haftmann@64593
   258
  by (auto simp add: bintrunc_mod2p intro: mod_diff_cong)
huffman@45545
   259
haftmann@63950
   260
instantiation word :: (len0) "{neg_numeral, modulo, comm_monoid_mult, comm_ring}"
haftmann@37660
   261
begin
haftmann@37660
   262
huffman@47387
   263
lift_definition zero_word :: "'a word" is "0" .
huffman@47387
   264
huffman@47387
   265
lift_definition one_word :: "'a word" is "1" .
huffman@47387
   266
huffman@47387
   267
lift_definition plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "op +"
haftmann@64593
   268
  by (auto simp add: bintrunc_mod2p intro: mod_add_cong)
huffman@47374
   269
huffman@47387
   270
lift_definition minus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "op -"
haftmann@64593
   271
  by (auto simp add: bintrunc_mod2p intro: mod_diff_cong)
huffman@47374
   272
huffman@47387
   273
lift_definition uminus_word :: "'a word \<Rightarrow> 'a word" is uminus
haftmann@64593
   274
  by (auto simp add: bintrunc_mod2p intro: mod_minus_cong)
huffman@47374
   275
huffman@47387
   276
lift_definition times_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "op *"
haftmann@64593
   277
  by (auto simp add: bintrunc_mod2p intro: mod_mult_cong)
haftmann@37660
   278
haftmann@37660
   279
definition
haftmann@60429
   280
  word_div_def: "a div b = word_of_int (uint a div uint b)"
haftmann@37660
   281
haftmann@37660
   282
definition
haftmann@37660
   283
  word_mod_def: "a mod b = word_of_int (uint a mod uint b)"
haftmann@37660
   284
huffman@47374
   285
instance
wenzelm@61169
   286
  by standard (transfer, simp add: algebra_simps)+
huffman@47374
   287
huffman@47374
   288
end
huffman@47374
   289
wenzelm@61799
   290
text \<open>Legacy theorems:\<close>
huffman@47374
   291
wenzelm@65268
   292
lemma word_arith_wis [code]:
wenzelm@65268
   293
  shows word_add_def: "a + b = word_of_int (uint a + uint b)"
wenzelm@65268
   294
    and word_sub_wi: "a - b = word_of_int (uint a - uint b)"
wenzelm@65268
   295
    and word_mult_def: "a * b = word_of_int (uint a * uint b)"
wenzelm@65268
   296
    and word_minus_def: "- a = word_of_int (- uint a)"
wenzelm@65268
   297
    and word_succ_alt: "word_succ a = word_of_int (uint a + 1)"
wenzelm@65268
   298
    and word_pred_alt: "word_pred a = word_of_int (uint a - 1)"
wenzelm@65268
   299
    and word_0_wi: "0 = word_of_int 0"
wenzelm@65268
   300
    and word_1_wi: "1 = word_of_int 1"
huffman@47374
   301
  unfolding plus_word_def minus_word_def times_word_def uminus_word_def
huffman@47374
   302
  unfolding word_succ_def word_pred_def zero_word_def one_word_def
huffman@47374
   303
  by simp_all
huffman@45545
   304
wenzelm@65268
   305
lemma wi_homs:
wenzelm@65268
   306
  shows wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)"
wenzelm@65268
   307
    and wi_hom_sub: "word_of_int a - word_of_int b = word_of_int (a - b)"
wenzelm@65268
   308
    and wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)"
wenzelm@65268
   309
    and wi_hom_neg: "- word_of_int a = word_of_int (- a)"
wenzelm@65268
   310
    and wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)"
wenzelm@65268
   311
    and wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a - 1)"
huffman@47374
   312
  by (transfer, simp)+
huffman@45545
   313
huffman@45545
   314
lemmas wi_hom_syms = wi_homs [symmetric]
huffman@45545
   315
huffman@46013
   316
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi
huffman@46009
   317
huffman@46009
   318
lemmas word_of_int_hom_syms = word_of_int_homs [symmetric]
huffman@45545
   319
huffman@45545
   320
instance word :: (len) comm_ring_1
huffman@45810
   321
proof
wenzelm@65268
   322
  have *: "0 < len_of TYPE('a)" by (rule len_gt_0)
wenzelm@65268
   323
  show "(0::'a word) \<noteq> 1"
wenzelm@65268
   324
    by transfer (use * in \<open>auto simp add: gr0_conv_Suc\<close>)
huffman@45810
   325
qed
huffman@45545
   326
huffman@45545
   327
lemma word_of_nat: "of_nat n = word_of_int (int n)"
huffman@45545
   328
  by (induct n) (auto simp add : word_of_int_hom_syms)
huffman@45545
   329
huffman@45545
   330
lemma word_of_int: "of_int = word_of_int"
huffman@45545
   331
  apply (rule ext)
huffman@45545
   332
  apply (case_tac x rule: int_diff_cases)
huffman@46013
   333
  apply (simp add: word_of_nat wi_hom_sub)
huffman@45545
   334
  done
huffman@45545
   335
wenzelm@65268
   336
definition udvd :: "'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool" (infixl "udvd" 50)
wenzelm@65268
   337
  where "a udvd b = (\<exists>n\<ge>0. uint b = n * uint a)"
haftmann@37660
   338
huffman@45547
   339
wenzelm@61799
   340
subsection \<open>Ordering\<close>
huffman@45547
   341
huffman@45547
   342
instantiation word :: (len0) linorder
huffman@45547
   343
begin
huffman@45547
   344
wenzelm@65268
   345
definition word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
wenzelm@65268
   346
wenzelm@65268
   347
definition word_less_def: "a < b \<longleftrightarrow> uint a < uint b"
haftmann@37660
   348
huffman@45547
   349
instance
wenzelm@61169
   350
  by standard (auto simp: word_less_def word_le_def)
huffman@45547
   351
huffman@45547
   352
end
huffman@45547
   353
wenzelm@65268
   354
definition word_sle :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> bool"  ("(_/ <=s _)" [50, 51] 50)
wenzelm@65268
   355
  where "a <=s b \<longleftrightarrow> sint a \<le> sint b"
wenzelm@65268
   356
wenzelm@65268
   357
definition word_sless :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> bool"  ("(_/ <s _)" [50, 51] 50)
wenzelm@65268
   358
  where "x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y"
haftmann@37660
   359
haftmann@37660
   360
wenzelm@61799
   361
subsection \<open>Bit-wise operations\<close>
haftmann@37660
   362
haftmann@37660
   363
instantiation word :: (len0) bits
haftmann@37660
   364
begin
haftmann@37660
   365
huffman@47387
   366
lift_definition bitNOT_word :: "'a word \<Rightarrow> 'a word" is bitNOT
huffman@47374
   367
  by (metis bin_trunc_not)
huffman@47374
   368
huffman@47387
   369
lift_definition bitAND_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is bitAND
huffman@47374
   370
  by (metis bin_trunc_and)
huffman@47374
   371
huffman@47387
   372
lift_definition bitOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is bitOR
huffman@47374
   373
  by (metis bin_trunc_or)
huffman@47374
   374
huffman@47387
   375
lift_definition bitXOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is bitXOR
huffman@47374
   376
  by (metis bin_trunc_xor)
haftmann@37660
   377
wenzelm@65268
   378
definition word_test_bit_def: "test_bit a = bin_nth (uint a)"
wenzelm@65268
   379
wenzelm@65268
   380
definition word_set_bit_def: "set_bit a n x = word_of_int (bin_sc n x (uint a))"
wenzelm@65268
   381
wenzelm@65268
   382
definition word_set_bits_def: "(BITS n. f n) = of_bl (bl_of_nth (len_of TYPE('a)) f)"
wenzelm@65268
   383
wenzelm@65268
   384
definition word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a)"
haftmann@37660
   385
haftmann@54848
   386
definition shiftl1 :: "'a word \<Rightarrow> 'a word"
wenzelm@65268
   387
  where "shiftl1 w = word_of_int (uint w BIT False)"
haftmann@37660
   388
haftmann@54848
   389
definition shiftr1 :: "'a word \<Rightarrow> 'a word"
haftmann@54848
   390
where
wenzelm@61799
   391
  \<comment> "shift right as unsigned or as signed, ie logical or arithmetic"
haftmann@37660
   392
  "shiftr1 w = word_of_int (bin_rest (uint w))"
haftmann@37660
   393
wenzelm@65268
   394
definition shiftl_def: "w << n = (shiftl1 ^^ n) w"
wenzelm@65268
   395
wenzelm@65268
   396
definition shiftr_def: "w >> n = (shiftr1 ^^ n) w"
haftmann@37660
   397
haftmann@37660
   398
instance ..
haftmann@37660
   399
haftmann@37660
   400
end
haftmann@37660
   401
wenzelm@65268
   402
lemma [code]:
wenzelm@65268
   403
  shows word_not_def: "NOT (a::'a::len0 word) = word_of_int (NOT (uint a))"
wenzelm@65268
   404
    and word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)"
wenzelm@65268
   405
    and word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)"
wenzelm@65268
   406
    and word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
wenzelm@65268
   407
  by (simp_all add: bitNOT_word_def bitAND_word_def bitOR_word_def bitXOR_word_def)
huffman@47374
   408
haftmann@37660
   409
instantiation word :: (len) bitss
haftmann@37660
   410
begin
haftmann@37660
   411
wenzelm@65268
   412
definition word_msb_def: "msb a \<longleftrightarrow> bin_sign (sint a) = -1"
haftmann@37660
   413
haftmann@37660
   414
instance ..
haftmann@37660
   415
haftmann@37660
   416
end
haftmann@37660
   417
wenzelm@65268
   418
definition setBit :: "'a::len0 word \<Rightarrow> nat \<Rightarrow> 'a word"
wenzelm@65268
   419
  where "setBit w n = set_bit w n True"
wenzelm@65268
   420
wenzelm@65268
   421
definition clearBit :: "'a::len0 word \<Rightarrow> nat \<Rightarrow> 'a word"
wenzelm@65268
   422
  where "clearBit w n = set_bit w n False"
haftmann@37660
   423
haftmann@37660
   424
wenzelm@61799
   425
subsection \<open>Shift operations\<close>
haftmann@37660
   426
wenzelm@65268
   427
definition sshiftr1 :: "'a::len word \<Rightarrow> 'a word"
wenzelm@65268
   428
  where "sshiftr1 w = word_of_int (bin_rest (sint w))"
wenzelm@65268
   429
wenzelm@65268
   430
definition bshiftr1 :: "bool \<Rightarrow> 'a::len word \<Rightarrow> 'a word"
wenzelm@65268
   431
  where "bshiftr1 b w = of_bl (b # butlast (to_bl w))"
wenzelm@65268
   432
wenzelm@65268
   433
definition sshiftr :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"  (infixl ">>>" 55)
wenzelm@65268
   434
  where "w >>> n = (sshiftr1 ^^ n) w"
wenzelm@65268
   435
wenzelm@65268
   436
definition mask :: "nat \<Rightarrow> 'a::len word"
wenzelm@65268
   437
  where "mask n = (1 << n) - 1"
wenzelm@65268
   438
wenzelm@65268
   439
definition revcast :: "'a::len0 word \<Rightarrow> 'b::len0 word"
wenzelm@65268
   440
  where "revcast w =  of_bl (takefill False (len_of TYPE('b)) (to_bl w))"
wenzelm@65268
   441
wenzelm@65268
   442
definition slice1 :: "nat \<Rightarrow> 'a::len0 word \<Rightarrow> 'b::len0 word"
wenzelm@65268
   443
  where "slice1 n w = of_bl (takefill False n (to_bl w))"
wenzelm@65268
   444
wenzelm@65268
   445
definition slice :: "nat \<Rightarrow> 'a::len0 word \<Rightarrow> 'b::len0 word"
wenzelm@65268
   446
  where "slice n w = slice1 (size w - n) w"
haftmann@37660
   447
haftmann@37660
   448
wenzelm@61799
   449
subsection \<open>Rotation\<close>
haftmann@37660
   450
wenzelm@65268
   451
definition rotater1 :: "'a list \<Rightarrow> 'a list"
wenzelm@65268
   452
  where "rotater1 ys =
wenzelm@65268
   453
    (case ys of [] \<Rightarrow> [] | x # xs \<Rightarrow> last ys # butlast ys)"
wenzelm@65268
   454
wenzelm@65268
   455
definition rotater :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
wenzelm@65268
   456
  where "rotater n = rotater1 ^^ n"
wenzelm@65268
   457
wenzelm@65268
   458
definition word_rotr :: "nat \<Rightarrow> 'a::len0 word \<Rightarrow> 'a::len0 word"
wenzelm@65268
   459
  where "word_rotr n w = of_bl (rotater n (to_bl w))"
wenzelm@65268
   460
wenzelm@65268
   461
definition word_rotl :: "nat \<Rightarrow> 'a::len0 word \<Rightarrow> 'a::len0 word"
wenzelm@65268
   462
  where "word_rotl n w = of_bl (rotate n (to_bl w))"
wenzelm@65268
   463
wenzelm@65268
   464
definition word_roti :: "int \<Rightarrow> 'a::len0 word \<Rightarrow> 'a::len0 word"
wenzelm@65268
   465
  where "word_roti i w =
wenzelm@65268
   466
    (if i \<ge> 0 then word_rotr (nat i) w else word_rotl (nat (- i)) w)"
haftmann@37660
   467
haftmann@37660
   468
wenzelm@61799
   469
subsection \<open>Split and cat operations\<close>
haftmann@37660
   470
wenzelm@65268
   471
definition word_cat :: "'a::len0 word \<Rightarrow> 'b::len0 word \<Rightarrow> 'c::len0 word"
wenzelm@65268
   472
  where "word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE('b)) (uint b))"
wenzelm@65268
   473
wenzelm@65268
   474
definition word_split :: "'a::len0 word \<Rightarrow> 'b::len0 word \<times> 'c::len0 word"
wenzelm@65268
   475
  where "word_split a =
wenzelm@65268
   476
    (case bin_split (len_of TYPE('c)) (uint a) of
wenzelm@65268
   477
      (u, v) \<Rightarrow> (word_of_int u, word_of_int v))"
wenzelm@65268
   478
wenzelm@65268
   479
definition word_rcat :: "'a::len0 word list \<Rightarrow> 'b::len0 word"
wenzelm@65268
   480
  where "word_rcat ws = word_of_int (bin_rcat (len_of TYPE('a)) (map uint ws))"
wenzelm@65268
   481
wenzelm@65268
   482
definition word_rsplit :: "'a::len0 word \<Rightarrow> 'b::len word list"
wenzelm@65268
   483
  where "word_rsplit w = map word_of_int (bin_rsplit (len_of TYPE('b)) (len_of TYPE('a), uint w))"
wenzelm@65268
   484
wenzelm@65268
   485
definition max_word :: "'a::len word"  \<comment> "Largest representable machine integer."
wenzelm@65268
   486
  where "max_word = word_of_int (2 ^ len_of TYPE('a) - 1)"
haftmann@37660
   487
haftmann@55816
   488
lemmas of_nth_def = word_set_bits_def (* FIXME duplicate *)
haftmann@55816
   489
haftmann@37660
   490
wenzelm@61799
   491
subsection \<open>Theorems about typedefs\<close>
huffman@46010
   492
wenzelm@65268
   493
lemma sint_sbintrunc': "sint (word_of_int bin :: 'a word) = sbintrunc (len_of TYPE('a::len) - 1) bin"
wenzelm@65268
   494
  by (auto simp: sint_uint word_ubin.eq_norm sbintrunc_bintrunc_lt)
wenzelm@65268
   495
wenzelm@65268
   496
lemma uint_sint: "uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a::len word))"
wenzelm@65268
   497
  by (auto simp: sint_uint bintrunc_sbintrunc_le)
wenzelm@65268
   498
wenzelm@65268
   499
lemma bintr_uint: "len_of TYPE('a) \<le> n \<Longrightarrow> bintrunc n (uint w) = uint w"
wenzelm@65268
   500
  for w :: "'a::len0 word"
wenzelm@65268
   501
  apply (subst word_ubin.norm_Rep [symmetric])
haftmann@37660
   502
  apply (simp only: bintrunc_bintrunc_min word_size)
haftmann@54863
   503
  apply (simp add: min.absorb2)
haftmann@37660
   504
  done
haftmann@37660
   505
huffman@46057
   506
lemma wi_bintr:
huffman@46057
   507
  "len_of TYPE('a::len0) \<le> n \<Longrightarrow>
huffman@46057
   508
    word_of_int (bintrunc n w) = (word_of_int w :: 'a word)"
wenzelm@65268
   509
  by (auto simp: word_ubin.norm_eq_iff [symmetric] min.absorb1)
wenzelm@65268
   510
wenzelm@65268
   511
lemma td_ext_sbin:
wenzelm@65268
   512
  "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (len_of TYPE('a::len)))
haftmann@37660
   513
    (sbintrunc (len_of TYPE('a) - 1))"
haftmann@37660
   514
  apply (unfold td_ext_def' sint_uint)
haftmann@37660
   515
  apply (simp add : word_ubin.eq_norm)
haftmann@37660
   516
  apply (cases "len_of TYPE('a)")
haftmann@37660
   517
   apply (auto simp add : sints_def)
haftmann@37660
   518
  apply (rule sym [THEN trans])
wenzelm@65268
   519
   apply (rule word_ubin.Abs_norm)
haftmann@37660
   520
  apply (simp only: bintrunc_sbintrunc)
haftmann@37660
   521
  apply (drule sym)
haftmann@37660
   522
  apply simp
haftmann@37660
   523
  done
haftmann@37660
   524
haftmann@55816
   525
lemma td_ext_sint:
haftmann@55816
   526
  "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (len_of TYPE('a::len)))
haftmann@55816
   527
     (\<lambda>w. (w + 2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) -
haftmann@55816
   528
         2 ^ (len_of TYPE('a) - 1))"
haftmann@55816
   529
  using td_ext_sbin [where ?'a = 'a] by (simp add: no_sbintr_alt2)
haftmann@37660
   530
haftmann@37660
   531
(* We do sint before sbin, before sint is the user version
wenzelm@65268
   532
   and interpretations do not produce thm duplicates. I.e.
haftmann@37660
   533
   we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD,
haftmann@37660
   534
   because the latter is the same thm as the former *)
haftmann@37660
   535
interpretation word_sint:
wenzelm@65268
   536
  td_ext
wenzelm@65268
   537
    "sint ::'a::len word \<Rightarrow> int"
wenzelm@65268
   538
    word_of_int
wenzelm@65268
   539
    "sints (len_of TYPE('a::len))"
wenzelm@65268
   540
    "\<lambda>w. (w + 2^(len_of TYPE('a::len) - 1)) mod 2^len_of TYPE('a::len) -
wenzelm@65268
   541
      2 ^ (len_of TYPE('a::len) - 1)"
haftmann@37660
   542
  by (rule td_ext_sint)
haftmann@37660
   543
haftmann@37660
   544
interpretation word_sbin:
wenzelm@65268
   545
  td_ext
wenzelm@65268
   546
    "sint ::'a::len word \<Rightarrow> int"
wenzelm@65268
   547
    word_of_int
wenzelm@65268
   548
    "sints (len_of TYPE('a::len))"
wenzelm@65268
   549
    "sbintrunc (len_of TYPE('a::len) - 1)"
haftmann@37660
   550
  by (rule td_ext_sbin)
haftmann@37660
   551
wenzelm@45604
   552
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm]
haftmann@37660
   553
haftmann@37660
   554
lemmas td_sint = word_sint.td
haftmann@37660
   555
wenzelm@65268
   556
lemma to_bl_def': "(to_bl :: 'a::len0 word \<Rightarrow> bool list) = bin_to_bl (len_of TYPE('a)) \<circ> uint"
wenzelm@44762
   557
  by (auto simp: to_bl_def)
haftmann@37660
   558
wenzelm@65268
   559
lemmas word_reverse_no_def [simp] =
wenzelm@65268
   560
  word_reverse_def [of "numeral w"] for w
haftmann@37660
   561
huffman@45805
   562
lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)"
huffman@45805
   563
  by (fact uints_def [unfolded no_bintr_alt1])
huffman@45805
   564
wenzelm@65268
   565
lemma word_numeral_alt: "numeral b = word_of_int (numeral b)"
huffman@47108
   566
  by (induct b, simp_all only: numeral.simps word_of_int_homs)
huffman@47108
   567
huffman@47108
   568
declare word_numeral_alt [symmetric, code_abbrev]
huffman@47108
   569
wenzelm@65268
   570
lemma word_neg_numeral_alt: "- numeral b = word_of_int (- numeral b)"
haftmann@54489
   571
  by (simp only: word_numeral_alt wi_hom_neg)
huffman@47108
   572
huffman@47108
   573
declare word_neg_numeral_alt [symmetric, code_abbrev]
huffman@47108
   574
huffman@47372
   575
lemma word_numeral_transfer [transfer_rule]:
blanchet@55945
   576
  "(rel_fun op = pcr_word) numeral numeral"
blanchet@55945
   577
  "(rel_fun op = pcr_word) (- numeral) (- numeral)"
blanchet@55945
   578
  apply (simp_all add: rel_fun_def word.pcr_cr_eq cr_word_def)
wenzelm@65268
   579
  using word_numeral_alt [symmetric] word_neg_numeral_alt [symmetric] by auto
huffman@47372
   580
huffman@45805
   581
lemma uint_bintrunc [simp]:
wenzelm@65268
   582
  "uint (numeral bin :: 'a word) =
wenzelm@65268
   583
    bintrunc (len_of TYPE('a::len0)) (numeral bin)"
huffman@47108
   584
  unfolding word_numeral_alt by (rule word_ubin.eq_norm)
huffman@47108
   585
wenzelm@65268
   586
lemma uint_bintrunc_neg [simp]:
wenzelm@65268
   587
  "uint (- numeral bin :: 'a word) = bintrunc (len_of TYPE('a::len0)) (- numeral bin)"
huffman@47108
   588
  by (simp only: word_neg_numeral_alt word_ubin.eq_norm)
haftmann@37660
   589
huffman@45805
   590
lemma sint_sbintrunc [simp]:
wenzelm@65268
   591
  "sint (numeral bin :: 'a word) = sbintrunc (len_of TYPE('a::len) - 1) (numeral bin)"
huffman@47108
   592
  by (simp only: word_numeral_alt word_sbin.eq_norm)
huffman@47108
   593
wenzelm@65268
   594
lemma sint_sbintrunc_neg [simp]:
wenzelm@65268
   595
  "sint (- numeral bin :: 'a word) = sbintrunc (len_of TYPE('a::len) - 1) (- numeral bin)"
huffman@47108
   596
  by (simp only: word_neg_numeral_alt word_sbin.eq_norm)
haftmann@37660
   597
huffman@45805
   598
lemma unat_bintrunc [simp]:
wenzelm@65268
   599
  "unat (numeral bin :: 'a::len0 word) = nat (bintrunc (len_of TYPE('a)) (numeral bin))"
huffman@47108
   600
  by (simp only: unat_def uint_bintrunc)
huffman@47108
   601
huffman@47108
   602
lemma unat_bintrunc_neg [simp]:
wenzelm@65268
   603
  "unat (- numeral bin :: 'a::len0 word) = nat (bintrunc (len_of TYPE('a)) (- numeral bin))"
huffman@47108
   604
  by (simp only: unat_def uint_bintrunc_neg)
haftmann@37660
   605
wenzelm@65268
   606
lemma size_0_eq: "size (w :: 'a::len0 word) = 0 \<Longrightarrow> v = w"
haftmann@37660
   607
  apply (unfold word_size)
haftmann@37660
   608
  apply (rule word_uint.Rep_eqD)
haftmann@37660
   609
  apply (rule box_equals)
haftmann@37660
   610
    defer
haftmann@37660
   611
    apply (rule word_ubin.norm_Rep)+
haftmann@37660
   612
  apply simp
haftmann@37660
   613
  done
haftmann@37660
   614
wenzelm@65268
   615
lemma uint_ge_0 [iff]: "0 \<le> uint x"
wenzelm@65268
   616
  for x :: "'a::len0 word"
huffman@45805
   617
  using word_uint.Rep [of x] by (simp add: uints_num)
huffman@45805
   618
wenzelm@65268
   619
lemma uint_lt2p [iff]: "uint x < 2 ^ len_of TYPE('a)"
wenzelm@65268
   620
  for x :: "'a::len0 word"
huffman@45805
   621
  using word_uint.Rep [of x] by (simp add: uints_num)
huffman@45805
   622
wenzelm@65268
   623
lemma sint_ge: "- (2 ^ (len_of TYPE('a) - 1)) \<le> sint x"
wenzelm@65268
   624
  for x :: "'a::len word"
huffman@45805
   625
  using word_sint.Rep [of x] by (simp add: sints_num)
huffman@45805
   626
wenzelm@65268
   627
lemma sint_lt: "sint x < 2 ^ (len_of TYPE('a) - 1)"
wenzelm@65268
   628
  for x :: "'a::len word"
huffman@45805
   629
  using word_sint.Rep [of x] by (simp add: sints_num)
haftmann@37660
   630
wenzelm@65268
   631
lemma sign_uint_Pls [simp]: "bin_sign (uint x) = 0"
huffman@47108
   632
  by (simp add: sign_Pls_ge_0)
haftmann@37660
   633
wenzelm@65268
   634
lemma uint_m2p_neg: "uint x - 2 ^ len_of TYPE('a) < 0"
wenzelm@65268
   635
  for x :: "'a::len0 word"
huffman@45805
   636
  by (simp only: diff_less_0_iff_less uint_lt2p)
huffman@45805
   637
wenzelm@65268
   638
lemma uint_m2p_not_non_neg: "\<not> 0 \<le> uint x - 2 ^ len_of TYPE('a)"
wenzelm@65268
   639
  for x :: "'a::len0 word"
huffman@45805
   640
  by (simp only: not_le uint_m2p_neg)
haftmann@37660
   641
wenzelm@65268
   642
lemma lt2p_lem: "len_of TYPE('a) \<le> n \<Longrightarrow> uint w < 2 ^ n"
wenzelm@65268
   643
  for w :: "'a::len0 word"
haftmann@55816
   644
  by (metis bintr_uint bintrunc_mod2p int_mod_lem zless2p)
haftmann@37660
   645
huffman@45805
   646
lemma uint_le_0_iff [simp]: "uint x \<le> 0 \<longleftrightarrow> uint x = 0"
huffman@45805
   647
  by (fact uint_ge_0 [THEN leD, THEN linorder_antisym_conv1])
haftmann@37660
   648
haftmann@40827
   649
lemma uint_nat: "uint w = int (unat w)"
wenzelm@65268
   650
  by (auto simp: unat_def)
wenzelm@65268
   651
wenzelm@65268
   652
lemma uint_numeral: "uint (numeral b :: 'a::len0 word) = numeral b mod 2 ^ len_of TYPE('a)"
wenzelm@65268
   653
  by (simp only: word_numeral_alt int_word_uint)
wenzelm@65268
   654
wenzelm@65268
   655
lemma uint_neg_numeral: "uint (- numeral b :: 'a::len0 word) = - numeral b mod 2 ^ len_of TYPE('a)"
wenzelm@65268
   656
  by (simp only: word_neg_numeral_alt int_word_uint)
wenzelm@65268
   657
wenzelm@65268
   658
lemma unat_numeral: "unat (numeral b :: 'a::len0 word) = numeral b mod 2 ^ len_of TYPE('a)"
haftmann@37660
   659
  apply (unfold unat_def)
huffman@47108
   660
  apply (clarsimp simp only: uint_numeral)
haftmann@37660
   661
  apply (rule nat_mod_distrib [THEN trans])
huffman@47108
   662
    apply (rule zero_le_numeral)
haftmann@37660
   663
   apply (simp_all add: nat_power_eq)
haftmann@37660
   664
  done
haftmann@37660
   665
wenzelm@65268
   666
lemma sint_numeral:
wenzelm@65268
   667
  "sint (numeral b :: 'a::len word) =
wenzelm@65268
   668
    (numeral b +
wenzelm@65268
   669
      2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) -
wenzelm@65268
   670
      2 ^ (len_of TYPE('a) - 1)"
huffman@47108
   671
  unfolding word_numeral_alt by (rule int_word_sint)
huffman@47108
   672
wenzelm@65268
   673
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0"
huffman@45958
   674
  unfolding word_0_wi ..
huffman@45958
   675
wenzelm@65268
   676
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1"
huffman@45958
   677
  unfolding word_1_wi ..
huffman@45958
   678
haftmann@54489
   679
lemma word_of_int_neg_1 [simp]: "word_of_int (- 1) = - 1"
haftmann@54489
   680
  by (simp add: wi_hom_syms)
haftmann@54489
   681
wenzelm@65268
   682
lemma word_of_int_numeral [simp] : "(word_of_int (numeral bin) :: 'a::len0 word) = numeral bin"
wenzelm@65268
   683
  by (simp only: word_numeral_alt)
huffman@47108
   684
huffman@47108
   685
lemma word_of_int_neg_numeral [simp]:
wenzelm@65268
   686
  "(word_of_int (- numeral bin) :: 'a::len0 word) = - numeral bin"
wenzelm@65268
   687
  by (simp only: word_numeral_alt wi_hom_syms)
wenzelm@65268
   688
wenzelm@65268
   689
lemma word_int_case_wi:
wenzelm@65268
   690
  "word_int_case f (word_of_int i :: 'b word) = f (i mod 2 ^ len_of TYPE('b::len0))"
wenzelm@65268
   691
  by (simp add: word_int_case_def word_uint.eq_norm)
wenzelm@65268
   692
wenzelm@65268
   693
lemma word_int_split:
wenzelm@65268
   694
  "P (word_int_case f x) =
wenzelm@65268
   695
    (\<forall>i. x = (word_of_int i :: 'b::len0 word) \<and> 0 \<le> i \<and> i < 2 ^ len_of TYPE('b) \<longrightarrow> P (f i))"
wenzelm@65268
   696
  by (auto simp: word_int_case_def word_uint.eq_norm mod_pos_pos_trivial)
wenzelm@65268
   697
wenzelm@65268
   698
lemma word_int_split_asm:
wenzelm@65268
   699
  "P (word_int_case f x) =
wenzelm@65268
   700
    (\<nexists>n. x = (word_of_int n :: 'b::len0 word) \<and> 0 \<le> n \<and> n < 2 ^ len_of TYPE('b::len0) \<and> \<not> P (f n))"
wenzelm@65268
   701
  by (auto simp: word_int_case_def word_uint.eq_norm mod_pos_pos_trivial)
huffman@45805
   702
wenzelm@45604
   703
lemmas uint_range' = word_uint.Rep [unfolded uints_num mem_Collect_eq]
wenzelm@45604
   704
lemmas sint_range' = word_sint.Rep [unfolded One_nat_def sints_num mem_Collect_eq]
haftmann@37660
   705
wenzelm@65268
   706
lemma uint_range_size: "0 \<le> uint w \<and> uint w < 2 ^ size w"
haftmann@37660
   707
  unfolding word_size by (rule uint_range')
haftmann@37660
   708
wenzelm@65268
   709
lemma sint_range_size: "- (2 ^ (size w - Suc 0)) \<le> sint w \<and> sint w < 2 ^ (size w - Suc 0)"
haftmann@37660
   710
  unfolding word_size by (rule sint_range')
haftmann@37660
   711
wenzelm@65268
   712
lemma sint_above_size: "2 ^ (size w - 1) \<le> x \<Longrightarrow> sint w < x"
wenzelm@65268
   713
  for w :: "'a::len word"
huffman@45805
   714
  unfolding word_size by (rule less_le_trans [OF sint_lt])
huffman@45805
   715
wenzelm@65268
   716
lemma sint_below_size: "x \<le> - (2 ^ (size w - 1)) \<Longrightarrow> x \<le> sint w"
wenzelm@65268
   717
  for w :: "'a::len word"
huffman@45805
   718
  unfolding word_size by (rule order_trans [OF _ sint_ge])
haftmann@37660
   719
haftmann@55816
   720
wenzelm@61799
   721
subsection \<open>Testing bits\<close>
huffman@46010
   722
wenzelm@65268
   723
lemma test_bit_eq_iff: "test_bit u = test_bit v \<longleftrightarrow> u = v"
wenzelm@65268
   724
  for u v :: "'a::len0 word"
haftmann@37660
   725
  unfolding word_test_bit_def by (simp add: bin_nth_eq_iff)
haftmann@37660
   726
wenzelm@65268
   727
lemma test_bit_size [rule_format] : "w !! n \<longrightarrow> n < size w"
wenzelm@65268
   728
  for w :: "'a::len0 word"
haftmann@37660
   729
  apply (unfold word_test_bit_def)
haftmann@37660
   730
  apply (subst word_ubin.norm_Rep [symmetric])
haftmann@37660
   731
  apply (simp only: nth_bintr word_size)
haftmann@37660
   732
  apply fast
haftmann@37660
   733
  done
haftmann@37660
   734
wenzelm@65268
   735
lemma word_eq_iff: "x = y \<longleftrightarrow> (\<forall>n<len_of TYPE('a). x !! n = y !! n)"
wenzelm@65268
   736
  for x y :: "'a::len0 word"
huffman@46021
   737
  unfolding uint_inject [symmetric] bin_eq_iff word_test_bit_def [symmetric]
huffman@46021
   738
  by (metis test_bit_size [unfolded word_size])
huffman@46021
   739
wenzelm@65268
   740
lemma word_eqI: "(\<And>n. n < size u \<longrightarrow> u !! n = v !! n) \<Longrightarrow> u = v"
wenzelm@65268
   741
  for u :: "'a::len0 word"
huffman@46021
   742
  by (simp add: word_size word_eq_iff)
haftmann@37660
   743
wenzelm@65268
   744
lemma word_eqD: "u = v \<Longrightarrow> u !! x = v !! x"
wenzelm@65268
   745
  for u v :: "'a::len0 word"
huffman@45805
   746
  by simp
haftmann@37660
   747
wenzelm@65268
   748
lemma test_bit_bin': "w !! n \<longleftrightarrow> n < size w \<and> bin_nth (uint w) n"
wenzelm@65268
   749
  by (simp add: word_test_bit_def word_size nth_bintr [symmetric])
haftmann@37660
   750
haftmann@37660
   751
lemmas test_bit_bin = test_bit_bin' [unfolded word_size]
haftmann@37660
   752
wenzelm@65268
   753
lemma bin_nth_uint_imp: "bin_nth (uint w) n \<Longrightarrow> n < len_of TYPE('a)"
wenzelm@65268
   754
  for w :: "'a::len0 word"
haftmann@37660
   755
  apply (rule nth_bintr [THEN iffD1, THEN conjunct1])
haftmann@37660
   756
  apply (subst word_ubin.norm_Rep)
haftmann@37660
   757
  apply assumption
haftmann@37660
   758
  done
haftmann@37660
   759
huffman@46057
   760
lemma bin_nth_sint:
wenzelm@65268
   761
  "len_of TYPE('a) \<le> n \<Longrightarrow> bin_nth (sint w) n = bin_nth (sint w) (len_of TYPE('a) - 1)"
wenzelm@65268
   762
  for w :: "'a::len word"
haftmann@37660
   763
  apply (subst word_sbin.norm_Rep [symmetric])
huffman@46057
   764
  apply (auto simp add: nth_sbintr)
haftmann@37660
   765
  done
haftmann@37660
   766
haftmann@37660
   767
(* type definitions theorem for in terms of equivalent bool list *)
wenzelm@65268
   768
lemma td_bl:
wenzelm@65268
   769
  "type_definition
wenzelm@65268
   770
    (to_bl :: 'a::len0 word \<Rightarrow> bool list)
wenzelm@65268
   771
    of_bl
wenzelm@65268
   772
    {bl. length bl = len_of TYPE('a)}"
haftmann@37660
   773
  apply (unfold type_definition_def of_bl_def to_bl_def)
haftmann@37660
   774
  apply (simp add: word_ubin.eq_norm)
haftmann@37660
   775
  apply safe
haftmann@37660
   776
  apply (drule sym)
haftmann@37660
   777
  apply simp
haftmann@37660
   778
  done
haftmann@37660
   779
haftmann@37660
   780
interpretation word_bl:
wenzelm@65268
   781
  type_definition
wenzelm@65268
   782
    "to_bl :: 'a::len0 word \<Rightarrow> bool list"
wenzelm@65268
   783
    of_bl
wenzelm@65268
   784
    "{bl. length bl = len_of TYPE('a::len0)}"
haftmann@55816
   785
  by (fact td_bl)
haftmann@37660
   786
huffman@45816
   787
lemmas word_bl_Rep' = word_bl.Rep [unfolded mem_Collect_eq, iff]
wenzelm@45538
   788
haftmann@40827
   789
lemma word_size_bl: "size w = size (to_bl w)"
wenzelm@65268
   790
  by (auto simp: word_size)
wenzelm@65268
   791
wenzelm@65268
   792
lemma to_bl_use_of_bl: "to_bl w = bl \<longleftrightarrow> w = of_bl bl \<and> length bl = length (to_bl w)"
huffman@45816
   793
  by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq])
haftmann@37660
   794
haftmann@37660
   795
lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)"
wenzelm@65268
   796
  by (simp add: word_reverse_def word_bl.Abs_inverse)
haftmann@37660
   797
haftmann@37660
   798
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w"
wenzelm@65268
   799
  by (simp add: word_reverse_def word_bl.Abs_inverse)
haftmann@37660
   800
haftmann@40827
   801
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w"
huffman@47108
   802
  by (metis word_rev_rev)
haftmann@37660
   803
huffman@45805
   804
lemma word_rev_gal': "u = word_reverse w \<Longrightarrow> w = word_reverse u"
huffman@45805
   805
  by simp
huffman@45805
   806
wenzelm@65268
   807
lemma length_bl_gt_0 [iff]: "0 < length (to_bl x)"
wenzelm@65268
   808
  for x :: "'a::len word"
huffman@45805
   809
  unfolding word_bl_Rep' by (rule len_gt_0)
huffman@45805
   810
wenzelm@65268
   811
lemma bl_not_Nil [iff]: "to_bl x \<noteq> []"
wenzelm@65268
   812
  for x :: "'a::len word"
huffman@45805
   813
  by (fact length_bl_gt_0 [unfolded length_greater_0_conv])
huffman@45805
   814
wenzelm@65268
   815
lemma length_bl_neq_0 [iff]: "length (to_bl x) \<noteq> 0"
wenzelm@65268
   816
  for x :: "'a::len word"
huffman@45805
   817
  by (fact length_bl_gt_0 [THEN gr_implies_not0])
haftmann@37660
   818
huffman@46001
   819
lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = -1)"
haftmann@37660
   820
  apply (unfold to_bl_def sint_uint)
haftmann@37660
   821
  apply (rule trans [OF _ bl_sbin_sign])
haftmann@37660
   822
  apply simp
haftmann@37660
   823
  done
haftmann@37660
   824
wenzelm@65268
   825
lemma of_bl_drop':
wenzelm@65268
   826
  "lend = length bl - len_of TYPE('a::len0) \<Longrightarrow>
haftmann@37660
   827
    of_bl (drop lend bl) = (of_bl bl :: 'a word)"
wenzelm@65268
   828
  by (auto simp: of_bl_def trunc_bl2bin [symmetric])
wenzelm@65268
   829
wenzelm@65268
   830
lemma test_bit_of_bl:
haftmann@37660
   831
  "(of_bl bl::'a::len0 word) !! n = (rev bl ! n \<and> n < len_of TYPE('a) \<and> n < length bl)"
wenzelm@65268
   832
  by (auto simp add: of_bl_def word_test_bit_def word_size word_ubin.eq_norm nth_bintr bin_nth_of_bl)
wenzelm@65268
   833
wenzelm@65268
   834
lemma no_of_bl: "(numeral bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE('a)) (numeral bin))"
wenzelm@65268
   835
  by (simp add: of_bl_def)
haftmann@37660
   836
haftmann@40827
   837
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)"
wenzelm@65268
   838
  by (auto simp: word_size to_bl_def)
haftmann@37660
   839
haftmann@37660
   840
lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w"
wenzelm@65268
   841
  by (simp add: uint_bl word_size)
wenzelm@65268
   842
wenzelm@65268
   843
lemma to_bl_of_bin: "to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin"
wenzelm@65268
   844
  by (auto simp: uint_bl word_ubin.eq_norm word_size)
haftmann@37660
   845
huffman@47108
   846
lemma to_bl_numeral [simp]:
huffman@47108
   847
  "to_bl (numeral bin::'a::len0 word) =
huffman@47108
   848
    bin_to_bl (len_of TYPE('a)) (numeral bin)"
huffman@47108
   849
  unfolding word_numeral_alt by (rule to_bl_of_bin)
huffman@47108
   850
huffman@47108
   851
lemma to_bl_neg_numeral [simp]:
haftmann@54489
   852
  "to_bl (- numeral bin::'a::len0 word) =
haftmann@54489
   853
    bin_to_bl (len_of TYPE('a)) (- numeral bin)"
huffman@47108
   854
  unfolding word_neg_numeral_alt by (rule to_bl_of_bin)
haftmann@37660
   855
haftmann@37660
   856
lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w"
wenzelm@65268
   857
  by (simp add: uint_bl word_size)
wenzelm@65268
   858
wenzelm@65268
   859
lemma uint_bl_bin: "bl_to_bin (bin_to_bl (len_of TYPE('a)) (uint x)) = uint x"
wenzelm@65268
   860
  for x :: "'a::len0 word"
huffman@46011
   861
  by (rule trans [OF bin_bl_bin word_ubin.norm_Rep])
wenzelm@45604
   862
haftmann@37660
   863
(* naturals *)
haftmann@37660
   864
lemma uints_unats: "uints n = int ` unats n"
haftmann@37660
   865
  apply (unfold unats_def uints_num)
haftmann@37660
   866
  apply safe
wenzelm@65268
   867
    apply (rule_tac image_eqI)
wenzelm@65268
   868
     apply (erule_tac nat_0_le [symmetric])
wenzelm@65268
   869
    apply auto
wenzelm@65268
   870
   apply (erule_tac nat_less_iff [THEN iffD2])
wenzelm@65268
   871
   apply (rule_tac [2] zless_nat_eq_int_zless [THEN iffD1])
wenzelm@65268
   872
   apply (auto simp: nat_power_eq)
haftmann@37660
   873
  done
haftmann@37660
   874
haftmann@37660
   875
lemma unats_uints: "unats n = nat ` uints n"
wenzelm@65268
   876
  by (auto simp: uints_unats image_iff)
wenzelm@65268
   877
wenzelm@65268
   878
lemmas bintr_num =
wenzelm@65268
   879
  word_ubin.norm_eq_iff [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b
wenzelm@65268
   880
lemmas sbintr_num =
wenzelm@65268
   881
  word_sbin.norm_eq_iff [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b
haftmann@37660
   882
haftmann@37660
   883
lemma num_of_bintr':
wenzelm@65268
   884
  "bintrunc (len_of TYPE('a::len0)) (numeral a) = (numeral b) \<Longrightarrow>
huffman@47108
   885
    numeral a = (numeral b :: 'a word)"
huffman@46962
   886
  unfolding bintr_num by (erule subst, simp)
haftmann@37660
   887
haftmann@37660
   888
lemma num_of_sbintr':
wenzelm@65268
   889
  "sbintrunc (len_of TYPE('a::len) - 1) (numeral a) = (numeral b) \<Longrightarrow>
huffman@47108
   890
    numeral a = (numeral b :: 'a word)"
huffman@46962
   891
  unfolding sbintr_num by (erule subst, simp)
huffman@46962
   892
huffman@46962
   893
lemma num_abs_bintr:
huffman@47108
   894
  "(numeral x :: 'a word) =
huffman@47108
   895
    word_of_int (bintrunc (len_of TYPE('a::len0)) (numeral x))"
huffman@47108
   896
  by (simp only: word_ubin.Abs_norm word_numeral_alt)
huffman@46962
   897
huffman@46962
   898
lemma num_abs_sbintr:
huffman@47108
   899
  "(numeral x :: 'a word) =
huffman@47108
   900
    word_of_int (sbintrunc (len_of TYPE('a::len) - 1) (numeral x))"
huffman@47108
   901
  by (simp only: word_sbin.Abs_norm word_numeral_alt)
huffman@46962
   902
haftmann@37660
   903
(** cast - note, no arg for new length, as it's determined by type of result,
haftmann@37660
   904
  thus in "cast w = w, the type means cast to length of w! **)
haftmann@37660
   905
haftmann@37660
   906
lemma ucast_id: "ucast w = w"
wenzelm@65268
   907
  by (auto simp: ucast_def)
haftmann@37660
   908
haftmann@37660
   909
lemma scast_id: "scast w = w"
wenzelm@65268
   910
  by (auto simp: scast_def)
haftmann@37660
   911
haftmann@40827
   912
lemma ucast_bl: "ucast w = of_bl (to_bl w)"
wenzelm@65268
   913
  by (auto simp: ucast_def of_bl_def uint_bl word_size)
wenzelm@65268
   914
wenzelm@65268
   915
lemma nth_ucast: "(ucast w::'a::len0 word) !! n = (w !! n \<and> n < len_of TYPE('a))"
wenzelm@65268
   916
  by (simp add: ucast_def test_bit_bin word_ubin.eq_norm nth_bintr word_size)
wenzelm@65268
   917
    (fast elim!: bin_nth_uint_imp)
haftmann@37660
   918
haftmann@37660
   919
(* for literal u(s)cast *)
haftmann@37660
   920
huffman@46001
   921
lemma ucast_bintr [simp]:
wenzelm@65268
   922
  "ucast (numeral w ::'a::len0 word) =  word_of_int (bintrunc (len_of TYPE('a)) (numeral w))"
wenzelm@65268
   923
  by (simp add: ucast_def)
wenzelm@65268
   924
huffman@47108
   925
(* TODO: neg_numeral *)
haftmann@37660
   926
huffman@46001
   927
lemma scast_sbintr [simp]:
wenzelm@65268
   928
  "scast (numeral w ::'a::len word) =
wenzelm@65268
   929
    word_of_int (sbintrunc (len_of TYPE('a) - Suc 0) (numeral w))"
wenzelm@65268
   930
  by (simp add: scast_def)
haftmann@37660
   931
huffman@46011
   932
lemma source_size: "source_size (c::'a::len0 word \<Rightarrow> _) = len_of TYPE('a)"
huffman@46011
   933
  unfolding source_size_def word_size Let_def ..
huffman@46011
   934
huffman@46011
   935
lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len0 word) = len_of TYPE('b)"
huffman@46011
   936
  unfolding target_size_def word_size Let_def ..
huffman@46011
   937
wenzelm@65268
   938
lemma is_down: "is_down c \<longleftrightarrow> len_of TYPE('b) \<le> len_of TYPE('a)"
wenzelm@65268
   939
  for c :: "'a::len0 word \<Rightarrow> 'b::len0 word"
wenzelm@65268
   940
  by (simp only: is_down_def source_size target_size)
wenzelm@65268
   941
wenzelm@65268
   942
lemma is_up: "is_up c \<longleftrightarrow> len_of TYPE('a) \<le> len_of TYPE('b)"
wenzelm@65268
   943
  for c :: "'a::len0 word \<Rightarrow> 'b::len0 word"
wenzelm@65268
   944
  by (simp only: is_up_def source_size target_size)
haftmann@37660
   945
wenzelm@45604
   946
lemmas is_up_down = trans [OF is_up is_down [symmetric]]
haftmann@37660
   947
huffman@45811
   948
lemma down_cast_same [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc = scast"
haftmann@37660
   949
  apply (unfold is_down)
haftmann@37660
   950
  apply safe
haftmann@37660
   951
  apply (rule ext)
haftmann@37660
   952
  apply (unfold ucast_def scast_def uint_sint)
haftmann@37660
   953
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
haftmann@37660
   954
  apply simp
haftmann@37660
   955
  done
haftmann@37660
   956
huffman@45811
   957
lemma word_rev_tf:
huffman@45811
   958
  "to_bl (of_bl bl::'a::len0 word) =
huffman@45811
   959
    rev (takefill False (len_of TYPE('a)) (rev bl))"
wenzelm@65268
   960
  by (auto simp: of_bl_def uint_bl bl_bin_bl_rtf word_ubin.eq_norm word_size)
haftmann@37660
   961
huffman@45811
   962
lemma word_rep_drop:
huffman@45811
   963
  "to_bl (of_bl bl::'a::len0 word) =
huffman@45811
   964
    replicate (len_of TYPE('a) - length bl) False @
huffman@45811
   965
    drop (length bl - len_of TYPE('a)) bl"
huffman@45811
   966
  by (simp add: word_rev_tf takefill_alt rev_take)
haftmann@37660
   967
wenzelm@65268
   968
lemma to_bl_ucast:
wenzelm@65268
   969
  "to_bl (ucast (w::'b::len0 word) ::'a::len0 word) =
wenzelm@65268
   970
    replicate (len_of TYPE('a) - len_of TYPE('b)) False @
wenzelm@65268
   971
    drop (len_of TYPE('b) - len_of TYPE('a)) (to_bl w)"
haftmann@37660
   972
  apply (unfold ucast_bl)
haftmann@37660
   973
  apply (rule trans)
haftmann@37660
   974
   apply (rule word_rep_drop)
haftmann@37660
   975
  apply simp
haftmann@37660
   976
  done
haftmann@37660
   977
huffman@45811
   978
lemma ucast_up_app [OF refl]:
wenzelm@65268
   979
  "uc = ucast \<Longrightarrow> source_size uc + n = target_size uc \<Longrightarrow>
haftmann@37660
   980
    to_bl (uc w) = replicate n False @ (to_bl w)"
haftmann@37660
   981
  by (auto simp add : source_size target_size to_bl_ucast)
haftmann@37660
   982
huffman@45811
   983
lemma ucast_down_drop [OF refl]:
wenzelm@65268
   984
  "uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow>
haftmann@37660
   985
    to_bl (uc w) = drop n (to_bl w)"
haftmann@37660
   986
  by (auto simp add : source_size target_size to_bl_ucast)
haftmann@37660
   987
huffman@45811
   988
lemma scast_down_drop [OF refl]:
wenzelm@65268
   989
  "sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow>
haftmann@37660
   990
    to_bl (sc w) = drop n (to_bl w)"
haftmann@37660
   991
  apply (subgoal_tac "sc = ucast")
haftmann@37660
   992
   apply safe
haftmann@37660
   993
   apply simp
huffman@45811
   994
   apply (erule ucast_down_drop)
huffman@45811
   995
  apply (rule down_cast_same [symmetric])
haftmann@37660
   996
  apply (simp add : source_size target_size is_down)
haftmann@37660
   997
  done
haftmann@37660
   998
wenzelm@65268
   999
lemma sint_up_scast [OF refl]: "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w"
haftmann@37660
  1000
  apply (unfold is_up)
haftmann@37660
  1001
  apply safe
haftmann@37660
  1002
  apply (simp add: scast_def word_sbin.eq_norm)
haftmann@37660
  1003
  apply (rule box_equals)
haftmann@37660
  1004
    prefer 3
haftmann@37660
  1005
    apply (rule word_sbin.norm_Rep)
haftmann@37660
  1006
   apply (rule sbintrunc_sbintrunc_l)
haftmann@37660
  1007
   defer
haftmann@37660
  1008
   apply (subst word_sbin.norm_Rep)
haftmann@37660
  1009
   apply (rule refl)
haftmann@37660
  1010
  apply simp
haftmann@37660
  1011
  done
haftmann@37660
  1012
wenzelm@65268
  1013
lemma uint_up_ucast [OF refl]: "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w"
haftmann@37660
  1014
  apply (unfold is_up)
haftmann@37660
  1015
  apply safe
haftmann@37660
  1016
  apply (rule bin_eqI)
haftmann@37660
  1017
  apply (fold word_test_bit_def)
haftmann@37660
  1018
  apply (auto simp add: nth_ucast)
haftmann@37660
  1019
  apply (auto simp add: test_bit_bin)
haftmann@37660
  1020
  done
huffman@45811
  1021
wenzelm@65268
  1022
lemma ucast_up_ucast [OF refl]: "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> ucast (uc w) = ucast w"
haftmann@37660
  1023
  apply (simp (no_asm) add: ucast_def)
haftmann@37660
  1024
  apply (clarsimp simp add: uint_up_ucast)
haftmann@37660
  1025
  done
wenzelm@65268
  1026
wenzelm@65268
  1027
lemma scast_up_scast [OF refl]: "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> scast (sc w) = scast w"
haftmann@37660
  1028
  apply (simp (no_asm) add: scast_def)
haftmann@37660
  1029
  apply (clarsimp simp add: sint_up_scast)
haftmann@37660
  1030
  done
wenzelm@65268
  1031
wenzelm@65268
  1032
lemma ucast_of_bl_up [OF refl]: "w = of_bl bl \<Longrightarrow> size bl \<le> size w \<Longrightarrow> ucast w = of_bl bl"
haftmann@37660
  1033
  by (auto simp add : nth_ucast word_size test_bit_of_bl intro!: word_eqI)
haftmann@37660
  1034
haftmann@37660
  1035
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id]
haftmann@37660
  1036
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id]
haftmann@37660
  1037
haftmann@37660
  1038
lemmas isduu = is_up_down [where c = "ucast", THEN iffD2]
haftmann@37660
  1039
lemmas isdus = is_up_down [where c = "scast", THEN iffD2]
haftmann@37660
  1040
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id]
haftmann@37660
  1041
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id]
haftmann@37660
  1042
haftmann@37660
  1043
lemma up_ucast_surj:
wenzelm@65268
  1044
  "is_up (ucast :: 'b::len0 word \<Rightarrow> 'a::len0 word) \<Longrightarrow>
wenzelm@65268
  1045
    surj (ucast :: 'a word \<Rightarrow> 'b word)"
wenzelm@65268
  1046
  by (rule surjI) (erule ucast_up_ucast_id)
haftmann@37660
  1047
haftmann@37660
  1048
lemma up_scast_surj:
wenzelm@65268
  1049
  "is_up (scast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow>
wenzelm@65268
  1050
    surj (scast :: 'a word \<Rightarrow> 'b word)"
wenzelm@65268
  1051
  by (rule surjI) (erule scast_up_scast_id)
haftmann@37660
  1052
haftmann@37660
  1053
lemma down_scast_inj:
wenzelm@65268
  1054
  "is_down (scast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow>
wenzelm@65268
  1055
    inj_on (ucast :: 'a word \<Rightarrow> 'b word) A"
haftmann@37660
  1056
  by (rule inj_on_inverseI, erule scast_down_scast_id)
haftmann@37660
  1057
haftmann@37660
  1058
lemma down_ucast_inj:
wenzelm@65268
  1059
  "is_down (ucast :: 'b::len0 word \<Rightarrow> 'a::len0 word) \<Longrightarrow>
wenzelm@65268
  1060
    inj_on (ucast :: 'a word \<Rightarrow> 'b word) A"
wenzelm@65268
  1061
  by (rule inj_on_inverseI) (erule ucast_down_ucast_id)
haftmann@37660
  1062
haftmann@37660
  1063
lemma of_bl_append_same: "of_bl (X @ to_bl w) = w"
haftmann@37660
  1064
  by (rule word_bl.Rep_eqD) (simp add: word_rep_drop)
huffman@45811
  1065
wenzelm@65268
  1066
lemma ucast_down_wi [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (word_of_int x) = word_of_int x"
huffman@46646
  1067
  apply (unfold is_down)
haftmann@37660
  1068
  apply (clarsimp simp add: ucast_def word_ubin.eq_norm)
haftmann@37660
  1069
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
haftmann@37660
  1070
  apply (erule bintrunc_bintrunc_ge)
haftmann@37660
  1071
  done
huffman@45811
  1072
wenzelm@65268
  1073
lemma ucast_down_no [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (numeral bin) = numeral bin"
huffman@47108
  1074
  unfolding word_numeral_alt by clarify (rule ucast_down_wi)
huffman@46646
  1075
wenzelm@65268
  1076
lemma ucast_down_bl [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl"
huffman@46646
  1077
  unfolding of_bl_def by clarify (erule ucast_down_wi)
haftmann@37660
  1078
haftmann@37660
  1079
lemmas slice_def' = slice_def [unfolded word_size]
haftmann@37660
  1080
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong]
haftmann@37660
  1081
haftmann@37660
  1082
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def
haftmann@37660
  1083
haftmann@37660
  1084
wenzelm@61799
  1085
subsection \<open>Word Arithmetic\<close>
haftmann@37660
  1086
wenzelm@65268
  1087
lemma word_less_alt: "a < b \<longleftrightarrow> uint a < uint b"
haftmann@55818
  1088
  by (fact word_less_def)
haftmann@37660
  1089
haftmann@37660
  1090
lemma signed_linorder: "class.linorder word_sle word_sless"
wenzelm@65268
  1091
  by standard (auto simp: word_sle_def word_sless_def)
haftmann@37660
  1092
haftmann@37660
  1093
interpretation signed: linorder "word_sle" "word_sless"
haftmann@37660
  1094
  by (rule signed_linorder)
haftmann@37660
  1095
wenzelm@65268
  1096
lemma udvdI: "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b"
haftmann@37660
  1097
  by (auto simp: udvd_def)
haftmann@37660
  1098
huffman@47108
  1099
lemmas word_div_no [simp] = word_div_def [of "numeral a" "numeral b"] for a b
huffman@47108
  1100
lemmas word_mod_no [simp] = word_mod_def [of "numeral a" "numeral b"] for a b
huffman@47108
  1101
lemmas word_less_no [simp] = word_less_def [of "numeral a" "numeral b"] for a b
huffman@47108
  1102
lemmas word_le_no [simp] = word_le_def [of "numeral a" "numeral b"] for a b
huffman@47108
  1103
lemmas word_sless_no [simp] = word_sless_def [of "numeral a" "numeral b"] for a b
huffman@47108
  1104
lemmas word_sle_no [simp] = word_sle_def [of "numeral a" "numeral b"] for a b
haftmann@37660
  1105
wenzelm@65268
  1106
lemma word_m1_wi: "- 1 = word_of_int (- 1)"
wenzelm@65268
  1107
  by (simp add: word_neg_numeral_alt [of Num.One])
haftmann@37660
  1108
huffman@46648
  1109
lemma word_0_bl [simp]: "of_bl [] = 0"
wenzelm@65268
  1110
  by (simp add: of_bl_def)
wenzelm@65268
  1111
wenzelm@65268
  1112
lemma word_1_bl: "of_bl [True] = 1"
wenzelm@65268
  1113
  by (simp add: of_bl_def bl_to_bin_def)
huffman@46648
  1114
huffman@46648
  1115
lemma uint_eq_0 [simp]: "uint 0 = 0"
huffman@46648
  1116
  unfolding word_0_wi word_ubin.eq_norm by simp
haftmann@37660
  1117
huffman@45995
  1118
lemma of_bl_0 [simp]: "of_bl (replicate n False) = 0"
huffman@46648
  1119
  by (simp add: of_bl_def bl_to_bin_rep_False)
haftmann@37660
  1120
wenzelm@65268
  1121
lemma to_bl_0 [simp]: "to_bl (0::'a::len0 word) = replicate (len_of TYPE('a)) False"
wenzelm@65268
  1122
  by (simp add: uint_bl word_size bin_to_bl_zero)
wenzelm@65268
  1123
wenzelm@65268
  1124
lemma uint_0_iff: "uint x = 0 \<longleftrightarrow> x = 0"
haftmann@55818
  1125
  by (simp add: word_uint_eq_iff)
haftmann@55818
  1126
wenzelm@65268
  1127
lemma unat_0_iff: "unat x = 0 \<longleftrightarrow> x = 0"
wenzelm@65268
  1128
  by (auto simp: unat_def nat_eq_iff uint_0_iff)
wenzelm@65268
  1129
wenzelm@65268
  1130
lemma unat_0 [simp]: "unat 0 = 0"
wenzelm@65268
  1131
  by (auto simp: unat_def)
wenzelm@65268
  1132
wenzelm@65268
  1133
lemma size_0_same': "size w = 0 \<Longrightarrow> w = v"
wenzelm@65268
  1134
  for v w :: "'a::len0 word"
haftmann@37660
  1135
  apply (unfold word_size)
haftmann@37660
  1136
  apply (rule box_equals)
haftmann@37660
  1137
    defer
haftmann@37660
  1138
    apply (rule word_uint.Rep_inverse)+
haftmann@37660
  1139
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
haftmann@37660
  1140
  apply simp
haftmann@37660
  1141
  done
haftmann@37660
  1142
huffman@45816
  1143
lemmas size_0_same = size_0_same' [unfolded word_size]
haftmann@37660
  1144
haftmann@37660
  1145
lemmas unat_eq_0 = unat_0_iff
haftmann@37660
  1146
lemmas unat_eq_zero = unat_0_iff
haftmann@37660
  1147
wenzelm@65268
  1148
lemma unat_gt_0: "0 < unat x \<longleftrightarrow> x \<noteq> 0"
wenzelm@65268
  1149
  by (auto simp: unat_0_iff [symmetric])
haftmann@37660
  1150
huffman@45958
  1151
lemma ucast_0 [simp]: "ucast 0 = 0"
wenzelm@65268
  1152
  by (simp add: ucast_def)
huffman@45958
  1153
huffman@45958
  1154
lemma sint_0 [simp]: "sint 0 = 0"
wenzelm@65268
  1155
  by (simp add: sint_uint)
huffman@45958
  1156
huffman@45958
  1157
lemma scast_0 [simp]: "scast 0 = 0"
wenzelm@65268
  1158
  by (simp add: scast_def)
haftmann@37660
  1159
haftmann@58410
  1160
lemma sint_n1 [simp] : "sint (- 1) = - 1"
wenzelm@65268
  1161
  by (simp only: word_m1_wi word_sbin.eq_norm) simp
haftmann@54489
  1162
haftmann@54489
  1163
lemma scast_n1 [simp]: "scast (- 1) = - 1"
wenzelm@65268
  1164
  by (simp add: scast_def)
huffman@45958
  1165
huffman@45958
  1166
lemma uint_1 [simp]: "uint (1::'a::len word) = 1"
haftmann@55818
  1167
  by (simp only: word_1_wi word_ubin.eq_norm) (simp add: bintrunc_minus_simps(4))
huffman@45958
  1168
huffman@45958
  1169
lemma unat_1 [simp]: "unat (1::'a::len word) = 1"
wenzelm@65268
  1170
  by (simp add: unat_def)
huffman@45958
  1171
huffman@45958
  1172
lemma ucast_1 [simp]: "ucast (1::'a::len word) = 1"
wenzelm@65268
  1173
  by (simp add: ucast_def)
haftmann@37660
  1174
haftmann@37660
  1175
(* now, to get the weaker results analogous to word_div/mod_def *)
haftmann@37660
  1176
haftmann@55816
  1177
wenzelm@61799
  1178
subsection \<open>Transferring goals from words to ints\<close>
haftmann@37660
  1179
wenzelm@65268
  1180
lemma word_ths:
wenzelm@65268
  1181
  shows word_succ_p1: "word_succ a = a + 1"
wenzelm@65268
  1182
    and word_pred_m1: "word_pred a = a - 1"
wenzelm@65268
  1183
    and word_pred_succ: "word_pred (word_succ a) = a"
wenzelm@65268
  1184
    and word_succ_pred: "word_succ (word_pred a) = a"
wenzelm@65268
  1185
    and word_mult_succ: "word_succ a * b = b + a * b"
huffman@47374
  1186
  by (transfer, simp add: algebra_simps)+
haftmann@37660
  1187
huffman@45816
  1188
lemma uint_cong: "x = y \<Longrightarrow> uint x = uint y"
huffman@45816
  1189
  by simp
haftmann@37660
  1190
haftmann@55818
  1191
lemma uint_word_ariths:
haftmann@55818
  1192
  fixes a b :: "'a::len0 word"
haftmann@55818
  1193
  shows "uint (a + b) = (uint a + uint b) mod 2 ^ len_of TYPE('a::len0)"
haftmann@55818
  1194
    and "uint (a - b) = (uint a - uint b) mod 2 ^ len_of TYPE('a)"
haftmann@55818
  1195
    and "uint (a * b) = uint a * uint b mod 2 ^ len_of TYPE('a)"
haftmann@55818
  1196
    and "uint (- a) = - uint a mod 2 ^ len_of TYPE('a)"
haftmann@55818
  1197
    and "uint (word_succ a) = (uint a + 1) mod 2 ^ len_of TYPE('a)"
haftmann@55818
  1198
    and "uint (word_pred a) = (uint a - 1) mod 2 ^ len_of TYPE('a)"
haftmann@55818
  1199
    and "uint (0 :: 'a word) = 0 mod 2 ^ len_of TYPE('a)"
haftmann@55818
  1200
    and "uint (1 :: 'a word) = 1 mod 2 ^ len_of TYPE('a)"
haftmann@55818
  1201
  by (simp_all add: word_arith_wis [THEN trans [OF uint_cong int_word_uint]])
haftmann@55818
  1202
haftmann@55818
  1203
lemma uint_word_arith_bintrs:
haftmann@55818
  1204
  fixes a b :: "'a::len0 word"
haftmann@55818
  1205
  shows "uint (a + b) = bintrunc (len_of TYPE('a)) (uint a + uint b)"
haftmann@55818
  1206
    and "uint (a - b) = bintrunc (len_of TYPE('a)) (uint a - uint b)"
haftmann@55818
  1207
    and "uint (a * b) = bintrunc (len_of TYPE('a)) (uint a * uint b)"
haftmann@55818
  1208
    and "uint (- a) = bintrunc (len_of TYPE('a)) (- uint a)"
haftmann@55818
  1209
    and "uint (word_succ a) = bintrunc (len_of TYPE('a)) (uint a + 1)"
haftmann@55818
  1210
    and "uint (word_pred a) = bintrunc (len_of TYPE('a)) (uint a - 1)"
haftmann@55818
  1211
    and "uint (0 :: 'a word) = bintrunc (len_of TYPE('a)) 0"
haftmann@55818
  1212
    and "uint (1 :: 'a word) = bintrunc (len_of TYPE('a)) 1"
haftmann@55818
  1213
  by (simp_all add: uint_word_ariths bintrunc_mod2p)
haftmann@55818
  1214
haftmann@55818
  1215
lemma sint_word_ariths:
haftmann@55818
  1216
  fixes a b :: "'a::len word"
haftmann@55818
  1217
  shows "sint (a + b) = sbintrunc (len_of TYPE('a) - 1) (sint a + sint b)"
haftmann@55818
  1218
    and "sint (a - b) = sbintrunc (len_of TYPE('a) - 1) (sint a - sint b)"
haftmann@55818
  1219
    and "sint (a * b) = sbintrunc (len_of TYPE('a) - 1) (sint a * sint b)"
haftmann@55818
  1220
    and "sint (- a) = sbintrunc (len_of TYPE('a) - 1) (- sint a)"
haftmann@55818
  1221
    and "sint (word_succ a) = sbintrunc (len_of TYPE('a) - 1) (sint a + 1)"
haftmann@55818
  1222
    and "sint (word_pred a) = sbintrunc (len_of TYPE('a) - 1) (sint a - 1)"
haftmann@55818
  1223
    and "sint (0 :: 'a word) = sbintrunc (len_of TYPE('a) - 1) 0"
haftmann@55818
  1224
    and "sint (1 :: 'a word) = sbintrunc (len_of TYPE('a) - 1) 1"
haftmann@64593
  1225
         apply (simp_all only: word_sbin.inverse_norm [symmetric])
haftmann@64593
  1226
         apply (simp_all add: wi_hom_syms)
haftmann@64593
  1227
   apply transfer apply simp
haftmann@64593
  1228
  apply transfer apply simp
haftmann@64593
  1229
  done
wenzelm@45604
  1230
wenzelm@45604
  1231
lemmas uint_div_alt = word_div_def [THEN trans [OF uint_cong int_word_uint]]
wenzelm@45604
  1232
lemmas uint_mod_alt = word_mod_def [THEN trans [OF uint_cong int_word_uint]]
haftmann@37660
  1233
haftmann@58410
  1234
lemma word_pred_0_n1: "word_pred 0 = word_of_int (- 1)"
huffman@47374
  1235
  unfolding word_pred_m1 by simp
haftmann@37660
  1236
haftmann@37660
  1237
lemma succ_pred_no [simp]:
wenzelm@65268
  1238
    "word_succ (numeral w) = numeral w + 1"
wenzelm@65268
  1239
    "word_pred (numeral w) = numeral w - 1"
wenzelm@65268
  1240
    "word_succ (- numeral w) = - numeral w + 1"
wenzelm@65268
  1241
    "word_pred (- numeral w) = - numeral w - 1"
wenzelm@65268
  1242
  by (simp_all add: word_succ_p1 word_pred_m1)
wenzelm@65268
  1243
wenzelm@65268
  1244
lemma word_sp_01 [simp]:
wenzelm@65268
  1245
  "word_succ (- 1) = 0 \<and> word_succ 0 = 1 \<and> word_pred 0 = - 1 \<and> word_pred 1 = 0"
wenzelm@65268
  1246
  by (simp_all add: word_succ_p1 word_pred_m1)
haftmann@37660
  1247
haftmann@37660
  1248
(* alternative approach to lifting arithmetic equalities *)
wenzelm@65268
  1249
lemma word_of_int_Ex: "\<exists>y. x = word_of_int y"
haftmann@37660
  1250
  by (rule_tac x="uint x" in exI) simp
haftmann@37660
  1251
haftmann@37660
  1252
wenzelm@61799
  1253
subsection \<open>Order on fixed-length words\<close>
haftmann@37660
  1254
haftmann@37660
  1255
lemma word_zero_le [simp] :
wenzelm@65268
  1256
  "0 <= (y :: 'a::len0 word)"
haftmann@37660
  1257
  unfolding word_le_def by auto
wenzelm@65268
  1258
huffman@45816
  1259
lemma word_m1_ge [simp] : "word_pred 0 >= y" (* FIXME: delete *)
haftmann@37660
  1260
  unfolding word_le_def
haftmann@37660
  1261
  by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
haftmann@37660
  1262
huffman@45816
  1263
lemma word_n1_ge [simp]: "y \<le> (-1::'a::len0 word)"
huffman@45816
  1264
  unfolding word_le_def
huffman@45816
  1265
  by (simp only: word_m1_wi word_uint.eq_norm m1mod2k) auto
haftmann@37660
  1266
wenzelm@65268
  1267
lemmas word_not_simps [simp] =
haftmann@37660
  1268
  word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
haftmann@37660
  1269
wenzelm@65268
  1270
lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> (y :: 'a::len0 word)"
huffman@47108
  1271
  by (simp add: less_le)
huffman@47108
  1272
huffman@47108
  1273
lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y
haftmann@37660
  1274
haftmann@40827
  1275
lemma word_sless_alt: "(a <s b) = (sint a < sint b)"
haftmann@37660
  1276
  unfolding word_sle_def word_sless_def
haftmann@37660
  1277
  by (auto simp add: less_le)
haftmann@37660
  1278
haftmann@37660
  1279
lemma word_le_nat_alt: "(a <= b) = (unat a <= unat b)"
haftmann@37660
  1280
  unfolding unat_def word_le_def
haftmann@37660
  1281
  by (rule nat_le_eq_zle [symmetric]) simp
haftmann@37660
  1282
haftmann@37660
  1283
lemma word_less_nat_alt: "(a < b) = (unat a < unat b)"
haftmann@37660
  1284
  unfolding unat_def word_less_alt
haftmann@37660
  1285
  by (rule nat_less_eq_zless [symmetric]) simp
wenzelm@65268
  1286
wenzelm@65268
  1287
lemma wi_less:
wenzelm@65268
  1288
  "(word_of_int n < (word_of_int m :: 'a::len0 word)) =
haftmann@37660
  1289
    (n mod 2 ^ len_of TYPE('a) < m mod 2 ^ len_of TYPE('a))"
haftmann@37660
  1290
  unfolding word_less_alt by (simp add: word_uint.eq_norm)
haftmann@37660
  1291
wenzelm@65268
  1292
lemma wi_le:
wenzelm@65268
  1293
  "(word_of_int n <= (word_of_int m :: 'a::len0 word)) =
haftmann@37660
  1294
    (n mod 2 ^ len_of TYPE('a) <= m mod 2 ^ len_of TYPE('a))"
haftmann@37660
  1295
  unfolding word_le_def by (simp add: word_uint.eq_norm)
haftmann@37660
  1296
haftmann@37660
  1297
lemma udvd_nat_alt: "a udvd b = (EX n>=0. unat b = n * unat a)"
haftmann@37660
  1298
  apply (unfold udvd_def)
haftmann@37660
  1299
  apply safe
haftmann@37660
  1300
   apply (simp add: unat_def nat_mult_distrib)
haftmann@62348
  1301
  apply (simp add: uint_nat of_nat_mult)
haftmann@37660
  1302
  apply (rule exI)
haftmann@37660
  1303
  apply safe
haftmann@37660
  1304
   prefer 2
haftmann@37660
  1305
   apply (erule notE)
haftmann@37660
  1306
   apply (rule refl)
haftmann@37660
  1307
  apply force
haftmann@37660
  1308
  done
haftmann@37660
  1309
wenzelm@61941
  1310
lemma udvd_iff_dvd: "x udvd y \<longleftrightarrow> unat x dvd unat y"
haftmann@37660
  1311
  unfolding dvd_def udvd_nat_alt by force
haftmann@37660
  1312
wenzelm@45604
  1313
lemmas unat_mono = word_less_nat_alt [THEN iffD1]
haftmann@37660
  1314
haftmann@55816
  1315
lemma unat_minus_one:
haftmann@55816
  1316
  assumes "w \<noteq> 0"
haftmann@55816
  1317
  shows "unat (w - 1) = unat w - 1"
haftmann@55816
  1318
proof -
haftmann@55816
  1319
  have "0 \<le> uint w" by (fact uint_nonnegative)
haftmann@55816
  1320
  moreover from assms have "0 \<noteq> uint w" by (simp add: uint_0_iff)
haftmann@55816
  1321
  ultimately have "1 \<le> uint w" by arith
haftmann@55816
  1322
  from uint_lt2p [of w] have "uint w - 1 < 2 ^ len_of TYPE('a)" by arith
wenzelm@61799
  1323
  with \<open>1 \<le> uint w\<close> have "(uint w - 1) mod 2 ^ len_of TYPE('a) = uint w - 1"
haftmann@55816
  1324
    by (auto intro: mod_pos_pos_trivial)
wenzelm@61799
  1325
  with \<open>1 \<le> uint w\<close> have "nat ((uint w - 1) mod 2 ^ len_of TYPE('a)) = nat (uint w) - 1"
haftmann@55816
  1326
    by auto
haftmann@55816
  1327
  then show ?thesis
haftmann@64593
  1328
    by (simp only: unat_def int_word_uint word_arith_wis mod_diff_right_eq)
haftmann@55816
  1329
qed
haftmann@55816
  1330
haftmann@40827
  1331
lemma measure_unat: "p ~= 0 \<Longrightarrow> unat (p - 1) < unat p"
haftmann@37660
  1332
  by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric])
wenzelm@65268
  1333
wenzelm@45604
  1334
lemmas uint_add_ge0 [simp] = add_nonneg_nonneg [OF uint_ge_0 uint_ge_0]
wenzelm@45604
  1335
lemmas uint_mult_ge0 [simp] = mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0]
haftmann@37660
  1336
wenzelm@65268
  1337
lemma uint_sub_lt2p [simp]:
wenzelm@65268
  1338
  "uint (x :: 'a::len0 word) - uint (y :: 'b::len0 word) <
haftmann@37660
  1339
    2 ^ len_of TYPE('a)"
haftmann@37660
  1340
  using uint_ge_0 [of y] uint_lt2p [of x] by arith
haftmann@37660
  1341
haftmann@37660
  1342
wenzelm@61799
  1343
subsection \<open>Conditions for the addition (etc) of two words to overflow\<close>
haftmann@37660
  1344
wenzelm@65268
  1345
lemma uint_add_lem:
wenzelm@65268
  1346
  "(uint x + uint y < 2 ^ len_of TYPE('a)) =
wenzelm@65268
  1347
    (uint (x + y :: 'a::len0 word) = uint x + uint y)"
haftmann@37660
  1348
  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
haftmann@37660
  1349
wenzelm@65268
  1350
lemma uint_mult_lem:
wenzelm@65268
  1351
  "(uint x * uint y < 2 ^ len_of TYPE('a)) =
wenzelm@65268
  1352
    (uint (x * y :: 'a::len0 word) = uint x * uint y)"
haftmann@37660
  1353
  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
haftmann@37660
  1354
wenzelm@65268
  1355
lemma uint_sub_lem:
haftmann@37660
  1356
  "(uint x >= uint y) = (uint (x - y) = uint x - uint y)"
haftmann@37660
  1357
  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
haftmann@37660
  1358
haftmann@37660
  1359
lemma uint_add_le: "uint (x + y) <= uint x + uint y"
haftmann@55816
  1360
  unfolding uint_word_ariths by (metis uint_add_ge0 zmod_le_nonneg_dividend)
haftmann@37660
  1361
haftmann@37660
  1362
lemma uint_sub_ge: "uint (x - y) >= uint x - uint y"
haftmann@55816
  1363
  unfolding uint_word_ariths by (metis int_mod_ge uint_sub_lt2p zless2p)
haftmann@55816
  1364
haftmann@55816
  1365
lemma mod_add_if_z:
wenzelm@65268
  1366
  "(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==>
haftmann@55816
  1367
   (x + y) mod z = (if x + y < z then x + y else x + y - z)"
haftmann@55816
  1368
  by (auto intro: int_mod_eq)
haftmann@55816
  1369
haftmann@55816
  1370
lemma uint_plus_if':
haftmann@55816
  1371
  "uint ((a::'a word) + b) =
haftmann@55816
  1372
  (if uint a + uint b < 2 ^ len_of TYPE('a::len0) then uint a + uint b
haftmann@55816
  1373
   else uint a + uint b - 2 ^ len_of TYPE('a))"
haftmann@55816
  1374
  using mod_add_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths)
haftmann@55816
  1375
haftmann@55816
  1376
lemma mod_sub_if_z:
wenzelm@65268
  1377
  "(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==>
haftmann@55816
  1378
   (x - y) mod z = (if y <= x then x - y else x - y + z)"
haftmann@55816
  1379
  by (auto intro: int_mod_eq)
haftmann@55816
  1380
haftmann@55816
  1381
lemma uint_sub_if':
haftmann@55816
  1382
  "uint ((a::'a word) - b) =
haftmann@55816
  1383
  (if uint b \<le> uint a then uint a - uint b
haftmann@55816
  1384
   else uint a - uint b + 2 ^ len_of TYPE('a::len0))"
haftmann@55816
  1385
  using mod_sub_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths)
haftmann@55816
  1386
haftmann@55816
  1387
wenzelm@61799
  1388
subsection \<open>Definition of \<open>uint_arith\<close>\<close>
haftmann@37660
  1389
haftmann@37660
  1390
lemma word_of_int_inverse:
wenzelm@65268
  1391
  "word_of_int r = a \<Longrightarrow> 0 <= r \<Longrightarrow> r < 2 ^ len_of TYPE('a) \<Longrightarrow>
haftmann@37660
  1392
   uint (a::'a::len0 word) = r"
haftmann@37660
  1393
  apply (erule word_uint.Abs_inverse' [rotated])
haftmann@37660
  1394
  apply (simp add: uints_num)
haftmann@37660
  1395
  done
haftmann@37660
  1396
haftmann@37660
  1397
lemma uint_split:
haftmann@37660
  1398
  fixes x::"'a::len0 word"
wenzelm@65268
  1399
  shows "P (uint x) =
haftmann@37660
  1400
         (ALL i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) --> P i)"
haftmann@37660
  1401
  apply (fold word_int_case_def)
haftmann@55816
  1402
  apply (auto dest!: word_of_int_inverse simp: int_word_uint mod_pos_pos_trivial
haftmann@37660
  1403
              split: word_int_split)
haftmann@37660
  1404
  done
haftmann@37660
  1405
haftmann@37660
  1406
lemma uint_split_asm:
haftmann@37660
  1407
  fixes x::"'a::len0 word"
wenzelm@65268
  1408
  shows "P (uint x) =
haftmann@37660
  1409
         (~(EX i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) & ~ P i))"
wenzelm@65268
  1410
  by (auto dest!: word_of_int_inverse
haftmann@55816
  1411
           simp: int_word_uint mod_pos_pos_trivial
haftmann@37660
  1412
           split: uint_split)
haftmann@37660
  1413
haftmann@37660
  1414
lemmas uint_splits = uint_split uint_split_asm
haftmann@37660
  1415
wenzelm@65268
  1416
lemmas uint_arith_simps =
haftmann@37660
  1417
  word_le_def word_less_alt
wenzelm@65268
  1418
  word_uint.Rep_inject [symmetric]
haftmann@37660
  1419
  uint_sub_if' uint_plus_if'
haftmann@37660
  1420
wenzelm@65268
  1421
(* use this to stop, eg, 2 ^ len_of TYPE(32) being simplified *)
wenzelm@65268
  1422
lemma power_False_cong: "False \<Longrightarrow> a ^ b = c ^ d"
haftmann@37660
  1423
  by auto
haftmann@37660
  1424
haftmann@37660
  1425
(* uint_arith_tac: reduce to arithmetic on int, try to solve by arith *)
wenzelm@61799
  1426
ML \<open>
wenzelm@65268
  1427
fun uint_arith_simpset ctxt =
wenzelm@51717
  1428
  ctxt addsimps @{thms uint_arith_simps}
haftmann@37660
  1429
     delsimps @{thms word_uint.Rep_inject}
nipkow@62390
  1430
     |> fold Splitter.add_split @{thms if_split_asm}
wenzelm@45620
  1431
     |> fold Simplifier.add_cong @{thms power_False_cong}
haftmann@37660
  1432
wenzelm@65268
  1433
fun uint_arith_tacs ctxt =
haftmann@37660
  1434
  let
haftmann@37660
  1435
    fun arith_tac' n t =
wenzelm@59657
  1436
      Arith_Data.arith_tac ctxt n t
haftmann@37660
  1437
        handle Cooper.COOPER _ => Seq.empty;
wenzelm@65268
  1438
  in
wenzelm@42793
  1439
    [ clarify_tac ctxt 1,
wenzelm@51717
  1440
      full_simp_tac (uint_arith_simpset ctxt) 1,
wenzelm@51717
  1441
      ALLGOALS (full_simp_tac
wenzelm@51717
  1442
        (put_simpset HOL_ss ctxt
wenzelm@51717
  1443
          |> fold Splitter.add_split @{thms uint_splits}
wenzelm@51717
  1444
          |> fold Simplifier.add_cong @{thms power_False_cong})),
wenzelm@65268
  1445
      rewrite_goals_tac ctxt @{thms word_size},
wenzelm@59498
  1446
      ALLGOALS  (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN
wenzelm@60754
  1447
                         REPEAT (eresolve_tac ctxt [conjE] n) THEN
wenzelm@65268
  1448
                         REPEAT (dresolve_tac ctxt @{thms word_of_int_inverse} n
wenzelm@65268
  1449
                                 THEN assume_tac ctxt n
wenzelm@58963
  1450
                                 THEN assume_tac ctxt n)),
haftmann@37660
  1451
      TRYALL arith_tac' ]
haftmann@37660
  1452
  end
haftmann@37660
  1453
haftmann@37660
  1454
fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt))
wenzelm@61799
  1455
\<close>
haftmann@37660
  1456
wenzelm@65268
  1457
method_setup uint_arith =
wenzelm@61799
  1458
  \<open>Scan.succeed (SIMPLE_METHOD' o uint_arith_tac)\<close>
haftmann@37660
  1459
  "solving word arithmetic via integers and arith"
haftmann@37660
  1460
haftmann@37660
  1461
wenzelm@61799
  1462
subsection \<open>More on overflows and monotonicity\<close>
haftmann@37660
  1463
wenzelm@65268
  1464
lemma no_plus_overflow_uint_size:
wenzelm@65268
  1465
  "((x :: 'a::len0 word) <= x + y) = (uint x + uint y < 2 ^ size x)"
haftmann@37660
  1466
  unfolding word_size by uint_arith
haftmann@37660
  1467
haftmann@37660
  1468
lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size]
haftmann@37660
  1469
wenzelm@65268
  1470
lemma no_ulen_sub: "((x :: 'a::len0 word) >= x - y) = (uint y <= uint x)"
haftmann@37660
  1471
  by uint_arith
haftmann@37660
  1472
haftmann@37660
  1473
lemma no_olen_add':
haftmann@37660
  1474
  fixes x :: "'a::len0 word"
haftmann@37660
  1475
  shows "(x \<le> y + x) = (uint y + uint x < 2 ^ len_of TYPE('a))"
haftmann@57514
  1476
  by (simp add: ac_simps no_olen_add)
haftmann@37660
  1477
wenzelm@45604
  1478
lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric]]
wenzelm@45604
  1479
wenzelm@45604
  1480
lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem]
wenzelm@45604
  1481
lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1]
wenzelm@45604
  1482
lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem]
haftmann@37660
  1483
lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def]
haftmann@37660
  1484
lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def]
wenzelm@45604
  1485
lemmas word_sub_le = word_sub_le_iff [THEN iffD2]
haftmann@37660
  1486
wenzelm@65268
  1487
lemma word_less_sub1:
wenzelm@65268
  1488
  "(x :: 'a::len word) ~= 0 \<Longrightarrow> (1 < x) = (0 < x - 1)"
haftmann@37660
  1489
  by uint_arith
haftmann@37660
  1490
wenzelm@65268
  1491
lemma word_le_sub1:
wenzelm@65268
  1492
  "(x :: 'a::len word) ~= 0 \<Longrightarrow> (1 <= x) = (0 <= x - 1)"
haftmann@37660
  1493
  by uint_arith
haftmann@37660
  1494
wenzelm@65268
  1495
lemma sub_wrap_lt:
wenzelm@65268
  1496
  "((x :: 'a::len0 word) < x - z) = (x < z)"
haftmann@37660
  1497
  by uint_arith
haftmann@37660
  1498
wenzelm@65268
  1499
lemma sub_wrap:
wenzelm@65268
  1500
  "((x :: 'a::len0 word) <= x - z) = (z = 0 | x < z)"
haftmann@37660
  1501
  by uint_arith
haftmann@37660
  1502
wenzelm@65268
  1503
lemma plus_minus_not_NULL_ab:
wenzelm@65268
  1504
  "(x :: 'a::len0 word) <= ab - c \<Longrightarrow> c <= ab \<Longrightarrow> c ~= 0 \<Longrightarrow> x + c ~= 0"
haftmann@37660
  1505
  by uint_arith
haftmann@37660
  1506
wenzelm@65268
  1507
lemma plus_minus_no_overflow_ab:
wenzelm@65268
  1508
  "(x :: 'a::len0 word) <= ab - c \<Longrightarrow> c <= ab \<Longrightarrow> x <= x + c"
haftmann@37660
  1509
  by uint_arith
haftmann@37660
  1510
wenzelm@65268
  1511
lemma le_minus':
wenzelm@65268
  1512
  "(a :: 'a::len0 word) + c <= b \<Longrightarrow> a <= a + c \<Longrightarrow> c <= b - a"
haftmann@37660
  1513
  by uint_arith
haftmann@37660
  1514
wenzelm@65268
  1515
lemma le_plus':
wenzelm@65268
  1516
  "(a :: 'a::len0 word) <= b \<Longrightarrow> c <= b - a \<Longrightarrow> a + c <= b"
haftmann@37660
  1517
  by uint_arith
haftmann@37660
  1518
haftmann@37660
  1519
lemmas le_plus = le_plus' [rotated]
haftmann@37660
  1520
huffman@46011
  1521
lemmas le_minus = leD [THEN thin_rl, THEN le_minus'] (* FIXME *)
haftmann@37660
  1522
wenzelm@65268
  1523
lemma word_plus_mono_right:
wenzelm@65268
  1524
  "(y :: 'a::len0 word) <= z \<Longrightarrow> x <= x + z \<Longrightarrow> x + y <= x + z"
haftmann@37660
  1525
  by uint_arith
haftmann@37660
  1526
wenzelm@65268
  1527
lemma word_less_minus_cancel:
wenzelm@65268
  1528
  "y - x < z - x \<Longrightarrow> x <= z \<Longrightarrow> (y :: 'a::len0 word) < z"
haftmann@37660
  1529
  by uint_arith
haftmann@37660
  1530
wenzelm@65268
  1531
lemma word_less_minus_mono_left:
wenzelm@65268
  1532
  "(y :: 'a::len0 word) < z \<Longrightarrow> x <= y \<Longrightarrow> y - x < z - x"
haftmann@37660
  1533
  by uint_arith
haftmann@37660
  1534
wenzelm@65268
  1535
lemma word_less_minus_mono:
wenzelm@65268
  1536
  "a < c \<Longrightarrow> d < b \<Longrightarrow> a - b < a \<Longrightarrow> c - d < c
haftmann@40827
  1537
  \<Longrightarrow> a - b < c - (d::'a::len word)"
haftmann@37660
  1538
  by uint_arith
haftmann@37660
  1539
wenzelm@65268
  1540
lemma word_le_minus_cancel:
wenzelm@65268
  1541
  "y - x <= z - x \<Longrightarrow> x <= z \<Longrightarrow> (y :: 'a::len0 word) <= z"
haftmann@37660
  1542
  by uint_arith
haftmann@37660
  1543
wenzelm@65268
  1544
lemma word_le_minus_mono_left:
wenzelm@65268
  1545
  "(y :: 'a::len0 word) <= z \<Longrightarrow> x <= y \<Longrightarrow> y - x <= z - x"
haftmann@37660
  1546
  by uint_arith
haftmann@37660
  1547
wenzelm@65268
  1548
lemma word_le_minus_mono:
wenzelm@65268
  1549
  "a <= c \<Longrightarrow> d <= b \<Longrightarrow> a - b <= a \<Longrightarrow> c - d <= c
haftmann@40827
  1550
  \<Longrightarrow> a - b <= c - (d::'a::len word)"
haftmann@37660
  1551
  by uint_arith
haftmann@37660
  1552
wenzelm@65268
  1553
lemma plus_le_left_cancel_wrap:
wenzelm@65268
  1554
  "(x :: 'a::len0 word) + y' < x \<Longrightarrow> x + y < x \<Longrightarrow> (x + y' < x + y) = (y' < y)"
haftmann@37660
  1555
  by uint_arith
haftmann@37660
  1556
wenzelm@65268
  1557
lemma plus_le_left_cancel_nowrap:
wenzelm@65268
  1558
  "(x :: 'a::len0 word) <= x + y' \<Longrightarrow> x <= x + y \<Longrightarrow>
wenzelm@65268
  1559
    (x + y' < x + y) = (y' < y)"
haftmann@37660
  1560
  by uint_arith
haftmann@37660
  1561
wenzelm@65268
  1562
lemma word_plus_mono_right2:
wenzelm@65268
  1563
  "(a :: 'a::len0 word) <= a + b \<Longrightarrow> c <= b \<Longrightarrow> a <= a + c"
haftmann@37660
  1564
  by uint_arith
haftmann@37660
  1565
wenzelm@65268
  1566
lemma word_less_add_right:
wenzelm@65268
  1567
  "(x :: 'a::len0 word) < y - z \<Longrightarrow> z <= y \<Longrightarrow> x + z < y"
haftmann@37660
  1568
  by uint_arith
haftmann@37660
  1569
wenzelm@65268
  1570
lemma word_less_sub_right:
wenzelm@65268
  1571
  "(x :: 'a::len0 word) < y + z \<Longrightarrow> y <= x \<Longrightarrow> x - y < z"
haftmann@37660
  1572
  by uint_arith
haftmann@37660
  1573
wenzelm@65268
  1574
lemma word_le_plus_either:
wenzelm@65268
  1575
  "(x :: 'a::len0 word) <= y | x <= z \<Longrightarrow> y <= y + z \<Longrightarrow> x <= y + z"
haftmann@37660
  1576
  by uint_arith
haftmann@37660
  1577
wenzelm@65268
  1578
lemma word_less_nowrapI:
wenzelm@65268
  1579
  "(x :: 'a::len0 word) < z - k \<Longrightarrow> k <= z \<Longrightarrow> 0 < k \<Longrightarrow> x < x + k"
haftmann@37660
  1580
  by uint_arith
haftmann@37660
  1581
wenzelm@65268
  1582
lemma inc_le: "(i :: 'a::len word) < m \<Longrightarrow> i + 1 <= m"
haftmann@37660
  1583
  by uint_arith
haftmann@37660
  1584
wenzelm@65268
  1585
lemma inc_i:
wenzelm@65268
  1586
  "(1 :: 'a::len word) <= i \<Longrightarrow> i < m \<Longrightarrow> 1 <= (i + 1) & i + 1 <= m"
haftmann@37660
  1587
  by uint_arith
haftmann@37660
  1588
haftmann@37660
  1589
lemma udvd_incr_lem:
wenzelm@65268
  1590
  "up < uq \<Longrightarrow> up = ua + n * uint K \<Longrightarrow>
haftmann@40827
  1591
    uq = ua + n' * uint K \<Longrightarrow> up + uint K <= uq"
haftmann@37660
  1592
  apply clarsimp
wenzelm@65268
  1593
haftmann@37660
  1594
  apply (drule less_le_mult)
haftmann@37660
  1595
  apply safe
haftmann@37660
  1596
  done
haftmann@37660
  1597
wenzelm@65268
  1598
lemma udvd_incr':
wenzelm@65268
  1599
  "p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow>
wenzelm@65268
  1600
    uint q = ua + n' * uint K \<Longrightarrow> p + K <= q"
haftmann@37660
  1601
  apply (unfold word_less_alt word_le_def)
haftmann@37660
  1602
  apply (drule (2) udvd_incr_lem)
haftmann@37660
  1603
  apply (erule uint_add_le [THEN order_trans])
haftmann@37660
  1604
  done
haftmann@37660
  1605
wenzelm@65268
  1606
lemma udvd_decr':
wenzelm@65268
  1607
  "p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow>
haftmann@40827
  1608
    uint q = ua + n' * uint K \<Longrightarrow> p <= q - K"
haftmann@37660
  1609
  apply (unfold word_less_alt word_le_def)
haftmann@37660
  1610
  apply (drule (2) udvd_incr_lem)
haftmann@37660
  1611
  apply (drule le_diff_eq [THEN iffD2])
haftmann@37660
  1612
  apply (erule order_trans)
haftmann@37660
  1613
  apply (rule uint_sub_ge)
haftmann@37660
  1614
  done
haftmann@37660
  1615
huffman@45816
  1616
lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, unfolded add_0_left]
huffman@45816
  1617
lemmas udvd_incr0 = udvd_incr' [where ua=0, unfolded add_0_left]
huffman@45816
  1618
lemmas udvd_decr0 = udvd_decr' [where ua=0, unfolded add_0_left]
haftmann@37660
  1619
wenzelm@65268
  1620
lemma udvd_minus_le':
haftmann@40827
  1621
  "xy < k \<Longrightarrow> z udvd xy \<Longrightarrow> z udvd k \<Longrightarrow> xy <= k - z"
haftmann@37660
  1622
  apply (unfold udvd_def)
haftmann@37660
  1623
  apply clarify
haftmann@37660
  1624
  apply (erule (2) udvd_decr0)
haftmann@37660
  1625
  done
haftmann@37660
  1626
wenzelm@65268
  1627
lemma udvd_incr2_K:
wenzelm@65268
  1628
  "p < a + s \<Longrightarrow> a <= a + s \<Longrightarrow> K udvd s \<Longrightarrow> K udvd p - a \<Longrightarrow> a <= p \<Longrightarrow>
haftmann@40827
  1629
    0 < K \<Longrightarrow> p <= p + K & p + K <= a + s"
wenzelm@51286
  1630
  using [[simproc del: linordered_ring_less_cancel_factor]]
haftmann@37660
  1631
  apply (unfold udvd_def)
haftmann@37660
  1632
  apply clarify
nipkow@62390
  1633
  apply (simp add: uint_arith_simps split: if_split_asm)
wenzelm@65268
  1634
   prefer 2
haftmann@37660
  1635
   apply (insert uint_range' [of s])[1]
haftmann@37660
  1636
   apply arith
haftmann@57512
  1637
  apply (drule add.commute [THEN xtr1])
haftmann@37660
  1638
  apply (simp add: diff_less_eq [symmetric])
haftmann@37660
  1639
  apply (drule less_le_mult)
haftmann@37660
  1640
   apply arith
haftmann@37660
  1641
  apply simp
haftmann@37660
  1642
  done
haftmann@37660
  1643
haftmann@37660
  1644
(* links with rbl operations *)
haftmann@37660
  1645
lemma word_succ_rbl:
haftmann@40827
  1646
  "to_bl w = bl \<Longrightarrow> to_bl (word_succ w) = (rev (rbl_succ (rev bl)))"
haftmann@37660
  1647
  apply (unfold word_succ_def)
haftmann@37660
  1648
  apply clarify
haftmann@37660
  1649
  apply (simp add: to_bl_of_bin)
huffman@46654
  1650
  apply (simp add: to_bl_def rbl_succ)
haftmann@37660
  1651
  done
haftmann@37660
  1652
haftmann@37660
  1653
lemma word_pred_rbl:
haftmann@40827
  1654
  "to_bl w = bl \<Longrightarrow> to_bl (word_pred w) = (rev (rbl_pred (rev bl)))"
haftmann@37660
  1655
  apply (unfold word_pred_def)
haftmann@37660
  1656
  apply clarify
haftmann@37660
  1657
  apply (simp add: to_bl_of_bin)
huffman@46654
  1658
  apply (simp add: to_bl_def rbl_pred)
haftmann@37660
  1659
  done
haftmann@37660
  1660
haftmann@37660
  1661
lemma word_add_rbl:
wenzelm@65268
  1662
  "to_bl v = vbl \<Longrightarrow> to_bl w = wbl \<Longrightarrow>
haftmann@37660
  1663
    to_bl (v + w) = (rev (rbl_add (rev vbl) (rev wbl)))"
haftmann@37660
  1664
  apply (unfold word_add_def)
haftmann@37660
  1665
  apply clarify
haftmann@37660
  1666
  apply (simp add: to_bl_of_bin)
haftmann@37660
  1667
  apply (simp add: to_bl_def rbl_add)
haftmann@37660
  1668
  done
haftmann@37660
  1669
haftmann@37660
  1670
lemma word_mult_rbl:
wenzelm@65268
  1671
  "to_bl v = vbl \<Longrightarrow> to_bl w = wbl \<Longrightarrow>
haftmann@37660
  1672
    to_bl (v * w) = (rev (rbl_mult (rev vbl) (rev wbl)))"
haftmann@37660
  1673
  apply (unfold word_mult_def)
haftmann@37660
  1674
  apply clarify
haftmann@37660
  1675
  apply (simp add: to_bl_of_bin)
haftmann@37660
  1676
  apply (simp add: to_bl_def rbl_mult)
haftmann@37660
  1677
  done
haftmann@37660
  1678
haftmann@37660
  1679
lemma rtb_rbl_ariths:
haftmann@37660
  1680
  "rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_succ w)) = rbl_succ ys"
haftmann@37660
  1681
  "rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_pred w)) = rbl_pred ys"
haftmann@40827
  1682
  "rev (to_bl v) = ys \<Longrightarrow> rev (to_bl w) = xs \<Longrightarrow> rev (to_bl (v * w)) = rbl_mult ys xs"
haftmann@40827
  1683
  "rev (to_bl v) = ys \<Longrightarrow> rev (to_bl w) = xs \<Longrightarrow> rev (to_bl (v + w)) = rbl_add ys xs"
wenzelm@65268
  1684
  by (auto simp: rev_swap [symmetric] word_succ_rbl
haftmann@37660
  1685
                 word_pred_rbl word_mult_rbl word_add_rbl)
haftmann@37660
  1686
haftmann@37660
  1687
wenzelm@61799
  1688
subsection \<open>Arithmetic type class instantiations\<close>
haftmann@37660
  1689
haftmann@37660
  1690
lemmas word_le_0_iff [simp] =
haftmann@37660
  1691
  word_zero_le [THEN leD, THEN linorder_antisym_conv1]
haftmann@37660
  1692
wenzelm@65268
  1693
lemma word_of_int_nat:
haftmann@40827
  1694
  "0 <= x \<Longrightarrow> word_of_int x = of_nat (nat x)"
haftmann@37660
  1695
  by (simp add: of_nat_nat word_of_int)
haftmann@37660
  1696
huffman@46603
  1697
(* note that iszero_def is only for class comm_semiring_1_cancel,
wenzelm@65268
  1698
   which requires word length >= 1, ie 'a::len word *)
huffman@46603
  1699
lemma iszero_word_no [simp]:
wenzelm@65268
  1700
  "iszero (numeral bin :: 'a::len word) =
huffman@47108
  1701
    iszero (bintrunc (len_of TYPE('a)) (numeral bin))"
huffman@47108
  1702
  using word_ubin.norm_eq_iff [where 'a='a, of "numeral bin" 0]
huffman@46603
  1703
  by (simp add: iszero_def [symmetric])
wenzelm@65268
  1704
wenzelm@61799
  1705
text \<open>Use \<open>iszero\<close> to simplify equalities between word numerals.\<close>
huffman@47108
  1706
huffman@47108
  1707
lemmas word_eq_numeral_iff_iszero [simp] =
huffman@47108
  1708
  eq_numeral_iff_iszero [where 'a="'a::len word"]
huffman@46603
  1709
haftmann@37660
  1710
wenzelm@61799
  1711
subsection \<open>Word and nat\<close>
haftmann@37660
  1712
huffman@45811
  1713
lemma td_ext_unat [OF refl]:
wenzelm@65268
  1714
  "n = len_of TYPE('a::len) \<Longrightarrow>
wenzelm@65268
  1715
    td_ext (unat :: 'a word => nat) of_nat
haftmann@37660
  1716
    (unats n) (%i. i mod 2 ^ n)"
haftmann@37660
  1717
  apply (unfold td_ext_def' unat_def word_of_nat unats_uints)
haftmann@37660
  1718
  apply (auto intro!: imageI simp add : word_of_int_hom_syms)
haftmann@37660
  1719
  apply (erule word_uint.Abs_inverse [THEN arg_cong])
haftmann@37660
  1720
  apply (simp add: int_word_uint nat_mod_distrib nat_power_eq)
haftmann@37660
  1721
  done
haftmann@37660
  1722
wenzelm@45604
  1723
lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm]
haftmann@37660
  1724
haftmann@37660
  1725
interpretation word_unat:
wenzelm@65268
  1726
  td_ext "unat::'a::len word => nat"
wenzelm@65268
  1727
         of_nat
haftmann@37660
  1728
         "unats (len_of TYPE('a::len))"
haftmann@37660
  1729
         "%i. i mod 2 ^ len_of TYPE('a::len)"
haftmann@37660
  1730
  by (rule td_ext_unat)
haftmann@37660
  1731
haftmann@37660
  1732
lemmas td_unat = word_unat.td_thm
haftmann@37660
  1733
haftmann@37660
  1734
lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq]
haftmann@37660
  1735
wenzelm@65268
  1736
lemma unat_le: "y <= unat (z :: 'a::len word) \<Longrightarrow> y : unats (len_of TYPE('a))"
haftmann@37660
  1737
  apply (unfold unats_def)
haftmann@37660
  1738
  apply clarsimp
wenzelm@65268
  1739
  apply (rule xtrans, rule unat_lt2p, assumption)
haftmann@37660
  1740
  done
haftmann@37660
  1741
haftmann@37660
  1742
lemma word_nchotomy:
wenzelm@65268
  1743
  "ALL w. EX n. (w :: 'a::len word) = of_nat n & n < 2 ^ len_of TYPE('a)"
haftmann@37660
  1744
  apply (rule allI)
haftmann@37660
  1745
  apply (rule word_unat.Abs_cases)
haftmann@37660
  1746
  apply (unfold unats_def)
haftmann@37660
  1747
  apply auto
haftmann@37660
  1748
  done
haftmann@37660
  1749
haftmann@37660
  1750
lemma of_nat_eq:
haftmann@37660
  1751
  fixes w :: "'a::len word"
haftmann@37660
  1752
  shows "(of_nat n = w) = (\<exists>q. n = unat w + q * 2 ^ len_of TYPE('a))"
haftmann@37660
  1753
  apply (rule trans)
haftmann@37660
  1754
   apply (rule word_unat.inverse_norm)
haftmann@37660
  1755
  apply (rule iffI)
haftmann@37660
  1756
   apply (rule mod_eqD)
haftmann@37660
  1757
   apply simp
haftmann@37660
  1758
  apply clarsimp
haftmann@37660
  1759
  done
haftmann@37660
  1760
wenzelm@65268
  1761
lemma of_nat_eq_size:
haftmann@37660
  1762
  "(of_nat n = w) = (EX q. n = unat w + q * 2 ^ size w)"
haftmann@37660
  1763
  unfolding word_size by (rule of_nat_eq)
haftmann@37660
  1764
haftmann@37660
  1765
lemma of_nat_0:
haftmann@37660
  1766
  "(of_nat m = (0::'a::len word)) = (\<exists>q. m = q * 2 ^ len_of TYPE('a))"
haftmann@37660
  1767
  by (simp add: of_nat_eq)
haftmann@37660
  1768
huffman@45805
  1769
lemma of_nat_2p [simp]:
huffman@45805
  1770
  "of_nat (2 ^ len_of TYPE('a)) = (0::'a::len word)"
huffman@45805
  1771
  by (fact mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]])
haftmann@37660
  1772
haftmann@40827
  1773
lemma of_nat_gt_0: "of_nat k ~= 0 \<Longrightarrow> 0 < k"
haftmann@37660
  1774
  by (cases k) auto
haftmann@37660
  1775
wenzelm@65268
  1776
lemma of_nat_neq_0:
wenzelm@65268
  1777
  "0 < k \<Longrightarrow> k < 2 ^ len_of TYPE('a::len) \<Longrightarrow> of_nat k ~= (0 :: 'a word)"
haftmann@37660
  1778
  by (clarsimp simp add : of_nat_0)
haftmann@37660
  1779
haftmann@37660
  1780
lemma Abs_fnat_hom_add:
haftmann@37660
  1781
  "of_nat a + of_nat b = of_nat (a + b)"
haftmann@37660
  1782
  by simp
haftmann@37660
  1783
haftmann@37660
  1784
lemma Abs_fnat_hom_mult:
wenzelm@65268
  1785
  "of_nat a * of_nat b = (of_nat (a * b) :: 'a::len word)"
lp15@61649
  1786
  by (simp add: word_of_nat wi_hom_mult)
haftmann@37660
  1787
haftmann@37660
  1788
lemma Abs_fnat_hom_Suc:
haftmann@37660
  1789
  "word_succ (of_nat a) = of_nat (Suc a)"
haftmann@57514
  1790
  by (simp add: word_of_nat wi_hom_succ ac_simps)
haftmann@37660
  1791
haftmann@37660
  1792
lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0"
huffman@45995
  1793
  by simp
haftmann@37660
  1794
haftmann@37660
  1795
lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)"
huffman@45995
  1796
  by simp
haftmann@37660
  1797
wenzelm@65268
  1798
lemmas Abs_fnat_homs =
wenzelm@65268
  1799
  Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc
haftmann@37660
  1800
  Abs_fnat_hom_0 Abs_fnat_hom_1
haftmann@37660
  1801
haftmann@37660
  1802
lemma word_arith_nat_add:
wenzelm@65268
  1803
  "a + b = of_nat (unat a + unat b)"
haftmann@37660
  1804
  by simp
haftmann@37660
  1805
haftmann@37660
  1806
lemma word_arith_nat_mult:
haftmann@37660
  1807
  "a * b = of_nat (unat a * unat b)"
huffman@45995
  1808
  by (simp add: of_nat_mult)
wenzelm@65268
  1809
haftmann@37660
  1810
lemma word_arith_nat_Suc:
haftmann@37660
  1811
  "word_succ a = of_nat (Suc (unat a))"
haftmann@37660
  1812
  by (subst Abs_fnat_hom_Suc [symmetric]) simp
haftmann@37660
  1813
haftmann@37660
  1814
lemma word_arith_nat_div:
haftmann@37660
  1815
  "a div b = of_nat (unat a div unat b)"
haftmann@37660
  1816
  by (simp add: word_div_def word_of_nat zdiv_int uint_nat)
haftmann@37660
  1817
haftmann@37660
  1818
lemma word_arith_nat_mod:
haftmann@37660
  1819
  "a mod b = of_nat (unat a mod unat b)"
haftmann@37660
  1820
  by (simp add: word_mod_def word_of_nat zmod_int uint_nat)
haftmann@37660
  1821
haftmann@37660
  1822
lemmas word_arith_nat_defs =
haftmann@37660
  1823
  word_arith_nat_add word_arith_nat_mult
haftmann@37660
  1824
  word_arith_nat_Suc Abs_fnat_hom_0
haftmann@37660
  1825
  Abs_fnat_hom_1 word_arith_nat_div
wenzelm@65268
  1826
  word_arith_nat_mod
haftmann@37660
  1827
huffman@45816
  1828
lemma unat_cong: "x = y \<Longrightarrow> unat x = unat y"
huffman@45816
  1829
  by simp
wenzelm@65268
  1830
haftmann@37660
  1831
lemmas unat_word_ariths = word_arith_nat_defs
wenzelm@45604
  1832
  [THEN trans [OF unat_cong unat_of_nat]]
haftmann@37660
  1833
haftmann@37660
  1834
lemmas word_sub_less_iff = word_sub_le_iff
huffman@45816
  1835
  [unfolded linorder_not_less [symmetric] Not_eq_iff]
haftmann@37660
  1836
wenzelm@65268
  1837
lemma unat_add_lem:
wenzelm@65268
  1838
  "(unat x + unat y < 2 ^ len_of TYPE('a)) =
wenzelm@65268
  1839
    (unat (x + y :: 'a::len word) = unat x + unat y)"
haftmann@37660
  1840
  unfolding unat_word_ariths
haftmann@37660
  1841
  by (auto intro!: trans [OF _ nat_mod_lem])
haftmann@37660
  1842
wenzelm@65268
  1843
lemma unat_mult_lem:
wenzelm@65268
  1844
  "(unat x * unat y < 2 ^ len_of TYPE('a)) =
wenzelm@65268
  1845
    (unat (x * y :: 'a::len word) = unat x * unat y)"
haftmann@37660
  1846
  unfolding unat_word_ariths
haftmann@37660
  1847
  by (auto intro!: trans [OF _ nat_mod_lem])
haftmann@37660
  1848
wenzelm@45604
  1849
lemmas unat_plus_if' = trans [OF unat_word_ariths(1) mod_nat_add, simplified]
haftmann@37660
  1850
wenzelm@65268
  1851
lemma le_no_overflow:
wenzelm@65268
  1852
  "x <= b \<Longrightarrow> a <= a + b \<Longrightarrow> x <= a + (b :: 'a::len0 word)"
haftmann@37660
  1853
  apply (erule order_trans)
haftmann@37660
  1854
  apply (erule olen_add_eqv [THEN iffD1])
haftmann@37660
  1855
  done
haftmann@37660
  1856
wenzelm@45604
  1857
lemmas un_ui_le = trans [OF word_le_nat_alt [symmetric] word_le_def]
haftmann@37660
  1858
haftmann@37660
  1859
lemma unat_sub_if_size:
wenzelm@65268
  1860
  "unat (x - y) = (if unat y <= unat x
wenzelm@65268
  1861
   then unat x - unat y
haftmann@37660
  1862
   else unat x + 2 ^ size x - unat y)"
haftmann@37660
  1863
  apply (unfold word_size)
haftmann@37660
  1864
  apply (simp add: un_ui_le)
haftmann@37660
  1865
  apply (auto simp add: unat_def uint_sub_if')
haftmann@37660
  1866
   apply (rule nat_diff_distrib)
haftmann@37660
  1867
    prefer 3
haftmann@37660
  1868
    apply (simp add: algebra_simps)
haftmann@37660
  1869
    apply (rule nat_diff_distrib [THEN trans])
haftmann@37660
  1870
      prefer 3
haftmann@37660
  1871
      apply (subst nat_add_distrib)
haftmann@37660
  1872
        prefer 3
haftmann@37660
  1873
        apply (simp add: nat_power_eq)
haftmann@37660
  1874
       apply auto
haftmann@37660
  1875
  apply uint_arith
haftmann@37660
  1876
  done
haftmann@37660
  1877
haftmann@37660
  1878
lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size]
haftmann@37660
  1879
wenzelm@65268
  1880
lemma unat_div: "unat ((x :: 'a::len word) div y) = unat x div unat y"
haftmann@37660
  1881
  apply (simp add : unat_word_ariths)
haftmann@37660
  1882
  apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
haftmann@37660
  1883
  apply (rule div_le_dividend)
haftmann@37660
  1884
  done
haftmann@37660
  1885
wenzelm@65268
  1886
lemma unat_mod: "unat ((x :: 'a::len word) mod y) = unat x mod unat y"
haftmann@37660
  1887
  apply (clarsimp simp add : unat_word_ariths)
haftmann@37660
  1888
  apply (cases "unat y")
haftmann@37660
  1889
   prefer 2
haftmann@37660
  1890
   apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
haftmann@37660
  1891
   apply (rule mod_le_divisor)
haftmann@37660
  1892
   apply auto
haftmann@37660
  1893
  done
haftmann@37660
  1894
wenzelm@65268
  1895
lemma uint_div: "uint ((x :: 'a::len word) div y) = uint x div uint y"
haftmann@37660
  1896
  unfolding uint_nat by (simp add : unat_div zdiv_int)
haftmann@37660
  1897
wenzelm@65268
  1898
lemma uint_mod: "uint ((x :: 'a::len word) mod y) = uint x mod uint y"
haftmann@37660
  1899
  unfolding uint_nat by (simp add : unat_mod zmod_int)
haftmann@37660
  1900
haftmann@37660
  1901
wenzelm@61799
  1902
subsection \<open>Definition of \<open>unat_arith\<close> tactic\<close>
haftmann@37660
  1903
haftmann@37660
  1904
lemma unat_split:
haftmann@37660
  1905
  fixes x::"'a::len word"
wenzelm@65268
  1906
  shows "P (unat x) =
haftmann@37660
  1907
         (ALL n. of_nat n = x & n < 2^len_of TYPE('a) --> P n)"
haftmann@37660
  1908
  by (auto simp: unat_of_nat)
haftmann@37660
  1909
haftmann@37660
  1910
lemma unat_split_asm:
haftmann@37660
  1911
  fixes x::"'a::len word"
wenzelm@65268
  1912
  shows "P (unat x) =
haftmann@37660
  1913
         (~(EX n. of_nat n = x & n < 2^len_of TYPE('a) & ~ P n))"
haftmann@37660
  1914
  by (auto simp: unat_of_nat)
haftmann@37660
  1915
wenzelm@65268
  1916
lemmas of_nat_inverse =
haftmann@37660
  1917
  word_unat.Abs_inverse' [rotated, unfolded unats_def, simplified]
haftmann@37660
  1918
haftmann@37660
  1919
lemmas unat_splits = unat_split unat_split_asm
haftmann@37660
  1920
haftmann@37660
  1921
lemmas unat_arith_simps =
haftmann@37660
  1922
  word_le_nat_alt word_less_nat_alt
haftmann@37660
  1923
  word_unat.Rep_inject [symmetric]
haftmann@37660
  1924
  unat_sub_if' unat_plus_if' unat_div unat_mod
haftmann@37660
  1925
wenzelm@65268
  1926
(* unat_arith_tac: tactic to reduce word arithmetic to nat,
haftmann@37660
  1927
   try to solve via arith *)
wenzelm@61799
  1928
ML \<open>
wenzelm@65268
  1929
fun unat_arith_simpset ctxt =
wenzelm@51717
  1930
  ctxt addsimps @{thms unat_arith_simps}
haftmann@37660
  1931
     delsimps @{thms word_unat.Rep_inject}
nipkow@62390
  1932
     |> fold Splitter.add_split @{thms if_split_asm}
wenzelm@45620
  1933
     |> fold Simplifier.add_cong @{thms power_False_cong}
haftmann@37660
  1934
wenzelm@65268
  1935
fun unat_arith_tacs ctxt =
haftmann@37660
  1936
  let
haftmann@37660
  1937
    fun arith_tac' n t =
wenzelm@59657
  1938
      Arith_Data.arith_tac ctxt n t
haftmann@37660
  1939
        handle Cooper.COOPER _ => Seq.empty;
wenzelm@65268
  1940
  in
wenzelm@42793
  1941
    [ clarify_tac ctxt 1,
wenzelm@51717
  1942
      full_simp_tac (unat_arith_simpset ctxt) 1,
wenzelm@51717
  1943
      ALLGOALS (full_simp_tac
wenzelm@51717
  1944
        (put_simpset HOL_ss ctxt
wenzelm@51717
  1945
          |> fold Splitter.add_split @{thms unat_splits}
wenzelm@51717
  1946
          |> fold Simplifier.add_cong @{thms power_False_cong})),
wenzelm@65268
  1947
      rewrite_goals_tac ctxt @{thms word_size},
wenzelm@60754
  1948
      ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN
wenzelm@60754
  1949
                         REPEAT (eresolve_tac ctxt [conjE] n) THEN
wenzelm@60754
  1950
                         REPEAT (dresolve_tac ctxt @{thms of_nat_inverse} n THEN assume_tac ctxt n)),
wenzelm@65268
  1951
      TRYALL arith_tac' ]
haftmann@37660
  1952
  end
haftmann@37660
  1953
haftmann@37660
  1954
fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt))
wenzelm@61799
  1955
\<close>
haftmann@37660
  1956
wenzelm@65268
  1957
method_setup unat_arith =
wenzelm@61799
  1958
  \<open>Scan.succeed (SIMPLE_METHOD' o unat_arith_tac)\<close>
haftmann@37660
  1959
  "solving word arithmetic via natural numbers and arith"
haftmann@37660
  1960
wenzelm@65268
  1961
lemma no_plus_overflow_unat_size:
wenzelm@65268
  1962
  "((x :: 'a::len word) <= x + y) = (unat x + unat y < 2 ^ size x)"
haftmann@37660
  1963
  unfolding word_size by unat_arith
haftmann@37660
  1964
haftmann@37660
  1965
lemmas no_olen_add_nat = no_plus_overflow_unat_size [unfolded word_size]
haftmann@37660
  1966
wenzelm@45604
  1967
lemmas unat_plus_simple = trans [OF no_olen_add_nat unat_add_lem]
haftmann@37660
  1968
wenzelm@65268
  1969
lemma word_div_mult:
wenzelm@65268
  1970
  "(0 :: 'a::len word) < y \<Longrightarrow> unat x * unat y < 2 ^ len_of TYPE('a) \<Longrightarrow>
haftmann@37660
  1971
    x * y div y = x"
haftmann@37660
  1972
  apply unat_arith
haftmann@37660
  1973
  apply clarsimp
haftmann@37660
  1974
  apply (subst unat_mult_lem [THEN iffD1])
haftmann@37660
  1975
  apply auto
haftmann@37660
  1976
  done
haftmann@37660
  1977
wenzelm@65268
  1978
lemma div_lt': "(i :: 'a::len word) <= k div x \<Longrightarrow>
haftmann@37660
  1979
    unat i * unat x < 2 ^ len_of TYPE('a)"
haftmann@37660
  1980
  apply unat_arith
haftmann@37660
  1981
  apply clarsimp
haftmann@37660
  1982
  apply (drule mult_le_mono1)
haftmann@37660
  1983
  apply (erule order_le_less_trans)
haftmann@37660
  1984
  apply (rule xtr7 [OF unat_lt2p div_mult_le])
haftmann@37660
  1985
  done
haftmann@37660
  1986
haftmann@37660
  1987
lemmas div_lt'' = order_less_imp_le [THEN div_lt']
haftmann@37660
  1988
wenzelm@65268
  1989
lemma div_lt_mult: "(i :: 'a::len word) < k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x < k"
haftmann@37660
  1990
  apply (frule div_lt'' [THEN unat_mult_lem [THEN iffD1]])
haftmann@37660
  1991
  apply (simp add: unat_arith_simps)
haftmann@37660
  1992
  apply (drule (1) mult_less_mono1)
haftmann@37660
  1993
  apply (erule order_less_le_trans)
haftmann@37660
  1994
  apply (rule div_mult_le)
haftmann@37660
  1995
  done
haftmann@37660
  1996
wenzelm@65268
  1997
lemma div_le_mult:
wenzelm@65268
  1998
  "(i :: 'a::len word) <= k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x <= k"
haftmann@37660
  1999
  apply (frule div_lt' [THEN unat_mult_lem [THEN iffD1]])
haftmann@37660
  2000
  apply (simp add: unat_arith_simps)
haftmann@37660
  2001
  apply (drule mult_le_mono1)
haftmann@37660
  2002
  apply (erule order_trans)
haftmann@37660
  2003
  apply (rule div_mult_le)
haftmann@37660
  2004
  done
haftmann@37660
  2005
wenzelm@65268
  2006
lemma div_lt_uint':
wenzelm@65268
  2007
  "(i :: 'a::len word) <= k div x \<Longrightarrow> uint i * uint x < 2 ^ len_of TYPE('a)"
haftmann@37660
  2008
  apply (unfold uint_nat)
haftmann@37660
  2009
  apply (drule div_lt')
lp15@61649
  2010
  by (metis of_nat_less_iff of_nat_mult of_nat_numeral of_nat_power)
haftmann@37660
  2011
haftmann@37660
  2012
lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint']
haftmann@37660
  2013
wenzelm@65268
  2014
lemma word_le_exists':
wenzelm@65268
  2015
  "(x :: 'a::len0 word) <= y \<Longrightarrow>
haftmann@37660
  2016
    (EX z. y = x + z & uint x + uint z < 2 ^ len_of TYPE('a))"
haftmann@37660
  2017
  apply (rule exI)
haftmann@37660
  2018
  apply (rule conjI)
haftmann@37660
  2019
  apply (rule zadd_diff_inverse)
haftmann@37660
  2020
  apply uint_arith
haftmann@37660
  2021
  done
haftmann@37660
  2022
haftmann@37660
  2023
lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab]
haftmann@37660
  2024
haftmann@37660
  2025
lemmas plus_minus_no_overflow =
haftmann@37660
  2026
  order_less_imp_le [THEN plus_minus_no_overflow_ab]
wenzelm@65268
  2027
haftmann@37660
  2028
lemmas mcs = word_less_minus_cancel word_less_minus_mono_left
haftmann@37660
  2029
  word_le_minus_cancel word_le_minus_mono_left
haftmann@37660
  2030
wenzelm@45604
  2031
lemmas word_l_diffs = mcs [where y = "w + x", unfolded add_diff_cancel] for w x
wenzelm@45604
  2032
lemmas word_diff_ls = mcs [where z = "w + x", unfolded add_diff_cancel] for w x
wenzelm@45604
  2033
lemmas word_plus_mcs = word_diff_ls [where y = "v + x", unfolded add_diff_cancel] for v x
haftmann@37660
  2034
haftmann@37660
  2035
lemmas le_unat_uoi = unat_le [THEN word_unat.Abs_inverse]
haftmann@37660
  2036
haftmann@37660
  2037
lemmas thd = refl [THEN [2] split_div_lemma [THEN iffD2], THEN conjunct1]
haftmann@37660
  2038
wenzelm@65268
  2039
lemmas uno_simps [THEN le_unat_uoi] = mod_le_divisor div_le_dividend dtle
haftmann@37660
  2040
haftmann@37660
  2041
lemma word_mod_div_equality:
wenzelm@65268
  2042
  "(n div b) * b + (n mod b) = (n :: 'a::len word)"
haftmann@37660
  2043
  apply (unfold word_less_nat_alt word_arith_nat_defs)
haftmann@37660
  2044
  apply (cut_tac y="unat b" in gt_or_eq_0)
haftmann@37660
  2045
  apply (erule disjE)
haftmann@64242
  2046
   apply (simp only: div_mult_mod_eq uno_simps Word.word_unat.Rep_inverse)
haftmann@37660
  2047
  apply simp
haftmann@37660
  2048
  done
haftmann@37660
  2049
haftmann@37660
  2050
lemma word_div_mult_le: "a div b * b <= (a::'a::len word)"
haftmann@37660
  2051
  apply (unfold word_le_nat_alt word_arith_nat_defs)
haftmann@37660
  2052
  apply (cut_tac y="unat b" in gt_or_eq_0)
haftmann@37660
  2053
  apply (erule disjE)
lp15@61649
  2054
   apply (simp only: div_mult_le uno_simps Word.word_unat.Rep_inverse)
haftmann@37660
  2055
  apply simp
haftmann@37660
  2056
  done
haftmann@37660
  2057
wenzelm@65268
  2058
lemma word_mod_less_divisor: "0 < n \<Longrightarrow> m mod n < (n :: 'a::len word)"
haftmann@37660
  2059
  apply (simp only: word_less_nat_alt word_arith_nat_defs)
haftmann@37660
  2060
  apply (clarsimp simp add : uno_simps)
haftmann@37660
  2061
  done
haftmann@37660
  2062
wenzelm@65268
  2063
lemma word_of_int_power_hom:
wenzelm@65268
  2064
  "word_of_int a ^ n = (word_of_int (a ^ n) :: 'a::len word)"
huffman@45995
  2065
  by (induct n) (simp_all add: wi_hom_mult [symmetric])
haftmann@37660
  2066
wenzelm@65268
  2067
lemma word_arith_power_alt:
wenzelm@65268
  2068
  "a ^ n = (word_of_int (uint a ^ n) :: 'a::len word)"
haftmann@37660
  2069
  by (simp add : word_of_int_power_hom [symmetric])
haftmann@37660
  2070
wenzelm@65268
  2071
lemma of_bl_length_less:
wenzelm@65268
  2072
  "length x = k \<Longrightarrow> k < len_of TYPE('a) \<Longrightarrow> (of_bl x :: 'a::len word) < 2 ^ k"
huffman@47108
  2073
  apply (unfold of_bl_def word_less_alt word_numeral_alt)
haftmann@37660
  2074
  apply safe
wenzelm@65268
  2075
  apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm
huffman@47108
  2076
                       del: word_of_int_numeral)
haftmann@37660
  2077
  apply (simp add: mod_pos_pos_trivial)
haftmann@37660
  2078
  apply (subst mod_pos_pos_trivial)
haftmann@37660
  2079
    apply (rule bl_to_bin_ge0)
haftmann@37660
  2080
   apply (rule order_less_trans)
haftmann@37660
  2081
    apply (rule bl_to_bin_lt2p)
haftmann@37660
  2082
   apply simp
huffman@46646
  2083
  apply (rule bl_to_bin_lt2p)
haftmann@37660
  2084
  done
haftmann@37660
  2085
haftmann@37660
  2086
wenzelm@61799
  2087
subsection \<open>Cardinality, finiteness of set of words\<close>
haftmann@37660
  2088
huffman@45809
  2089
instance word :: (len0) finite
wenzelm@61169
  2090
  by standard (simp add: type_definition.univ [OF type_definition_word])
huffman@45809
  2091
huffman@45809
  2092
lemma card_word: "CARD('a::len0 word) = 2 ^ len_of TYPE('a)"
huffman@45809
  2093
  by (simp add: type_definition.card [OF type_definition_word] nat_power_eq)
haftmann@37660
  2094
wenzelm@65268
  2095
lemma card_word_size:
wenzelm@65268
  2096
  "card (UNIV :: 'a::len0 word set) = (2 ^ size (x :: 'a word))"
haftmann@37660
  2097
unfolding word_size by (rule card_word)
haftmann@37660
  2098
haftmann@37660
  2099
wenzelm@61799
  2100
subsection \<open>Bitwise Operations on Words\<close>
haftmann@37660
  2101
haftmann@37660
  2102
lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or
wenzelm@65268
  2103
haftmann@37660
  2104
(* following definitions require both arithmetic and bit-wise word operations *)
haftmann@37660
  2105
haftmann@37660
  2106
(* to get word_no_log_defs from word_log_defs, using bin_log_bintrs *)
haftmann@37660
  2107
lemmas wils1 = bin_log_bintrs [THEN word_ubin.norm_eq_iff [THEN iffD1],
wenzelm@45604
  2108
  folded word_ubin.eq_norm, THEN eq_reflection]
haftmann@37660
  2109
haftmann@37660
  2110
(* the binary operations only *)
huffman@46013
  2111
(* BH: why is this needed? *)
wenzelm@65268
  2112
lemmas word_log_binary_defs =
haftmann@37660
  2113
  word_and_def word_or_def word_xor_def
haftmann@37660
  2114
huffman@46011
  2115
lemma word_wi_log_defs:
huffman@46011
  2116
  "NOT word_of_int a = word_of_int (NOT a)"
huffman@46011
  2117
  "word_of_int a AND word_of_int b = word_of_int (a AND b)"
huffman@46011
  2118
  "word_of_int a OR word_of_int b = word_of_int (a OR b)"
huffman@46011
  2119
  "word_of_int a XOR word_of_int b = word_of_int (a XOR b)"
huffman@47374
  2120
  by (transfer, rule refl)+
huffman@47372
  2121
huffman@46011
  2122
lemma word_no_log_defs [simp]:
huffman@47108
  2123
  "NOT (numeral a) = word_of_int (NOT (numeral a))"
haftmann@54489
  2124
  "NOT (- numeral a) = word_of_int (NOT (- numeral a))"
huffman@47108
  2125
  "numeral a AND numeral b = word_of_int (numeral a AND numeral b)"
haftmann@54489
  2126
  "numeral a AND - numeral b = word_of_int (numeral a AND - numeral b)"
haftmann@54489
  2127
  "- numeral a AND numeral b = word_of_int (- numeral a AND numeral b)"
haftmann@54489
  2128
  "- numeral a AND - numeral b = word_of_int (- numeral a AND - numeral b)"
huffman@47108
  2129
  "numeral a OR numeral b = word_of_int (numeral a OR numeral b)"
haftmann@54489
  2130
  "numeral a OR - numeral b = word_of_int (numeral a OR - numeral b)"
haftmann@54489
  2131
  "- numeral a OR numeral b = word_of_int (- numeral a OR numeral b)"
haftmann@54489
  2132
  "- numeral a OR - numeral b = word_of_int (- numeral a OR - numeral b)"
huffman@47108
  2133
  "numeral a XOR numeral b = word_of_int (numeral a XOR numeral b)"
haftmann@54489
  2134
  "numeral a XOR - numeral b = word_of_int (numeral a XOR - numeral b)"
haftmann@54489
  2135
  "- numeral a XOR numeral b = word_of_int (- numeral a XOR numeral b)"
haftmann@54489
  2136
  "- numeral a XOR - numeral b = word_of_int (- numeral a XOR - numeral b)"
huffman@47372
  2137
  by (transfer, rule refl)+
haftmann@37660
  2138
wenzelm@61799
  2139
text \<open>Special cases for when one of the arguments equals 1.\<close>
huffman@46064
  2140
huffman@46064
  2141
lemma word_bitwise_1_simps [simp]:
huffman@46064
  2142
  "NOT (1::'a::len0 word) = -2"
huffman@47108
  2143
  "1 AND numeral b = word_of_int (1 AND numeral b)"
haftmann@54489
  2144
  "1 AND - numeral b = word_of_int (1 AND - numeral b)"
huffman@47108
  2145
  "numeral a AND 1 = word_of_int (numeral a AND 1)"
haftmann@54489
  2146
  "- numeral a AND 1 = word_of_int (- numeral a AND 1)"
huffman@47108
  2147
  "1 OR numeral b = word_of_int (1 OR numeral b)"
haftmann@54489
  2148
  "1 OR - numeral b = word_of_int (1 OR - numeral b)"
huffman@47108
  2149
  "numeral a OR 1 = word_of_int (numeral a OR 1)"
haftmann@54489
  2150
  "- numeral a OR 1 = word_of_int (- numeral a OR 1)"
huffman@47108
  2151
  "1 XOR numeral b = word_of_int (1 XOR numeral b)"
haftmann@54489
  2152
  "1 XOR - numeral b = word_of_int (1 XOR - numeral b)"
huffman@47108
  2153
  "numeral a XOR 1 = word_of_int (numeral a XOR 1)"
haftmann@54489
  2154
  "- numeral a XOR 1 = word_of_int (- numeral a XOR 1)"
huffman@47372
  2155
  by (transfer, simp)+
huffman@46064
  2156
wenzelm@61799
  2157
text \<open>Special cases for when one of the arguments equals -1.\<close>
noschinl@56979
  2158
noschinl@56979
  2159
lemma word_bitwise_m1_simps [simp]:
noschinl@56979
  2160
  "NOT (-1::'a::len0 word) = 0"
noschinl@56979
  2161
  "(-1::'a::len0 word) AND x = x"
noschinl@56979
  2162
  "x AND (-1::'a::len0 word) = x"
noschinl@56979
  2163
  "(-1::'a::len0 word) OR x = -1"
noschinl@56979
  2164
  "x OR (-1::'a::len0 word) = -1"
noschinl@56979
  2165
  " (-1::'a::len0 word) XOR x = NOT x"
noschinl@56979
  2166
  "x XOR (-1::'a::len0 word) = NOT x"
noschinl@56979
  2167
  by (transfer, simp)+
noschinl@56979
  2168
haftmann@37660
  2169
lemma uint_or: "uint (x OR y) = (uint x) OR (uint y)"
huffman@47372
  2170
  by (transfer, simp add: bin_trunc_ao)
haftmann@37660
  2171
haftmann@37660
  2172
lemma uint_and: "uint (x AND y) = (uint x) AND (uint y)"
huffman@47372
  2173
  by (transfer, simp add: bin_trunc_ao)
huffman@47372
  2174
huffman@47372
  2175
lemma test_bit_wi [simp]:
huffman@47372
  2176
  "(word_of_int x::'a::len0 word) !! n \<longleftrightarrow> n < len_of TYPE('a) \<and> bin_nth x n"
huffman@47372
  2177
  unfolding word_test_bit_def
huffman@47372
  2178
  by (simp add: word_ubin.eq_norm nth_bintr)
huffman@47372
  2179
huffman@47372
  2180
lemma word_test_bit_transfer [transfer_rule]:
blanchet@55945
  2181
  "(rel_fun pcr_word (rel_fun op = op =))
huffman@47372
  2182
    (\<lambda>x n. n < len_of TYPE('a) \<and> bin_nth x n) (test_bit :: 'a::len0 word \<Rightarrow> _)"
blanchet@55945
  2183
  unfolding rel_fun_def word.pcr_cr_eq cr_word_def by simp
haftmann@37660
  2184
haftmann@37660
  2185
lemma word_ops_nth_size:
wenzelm@65268
  2186
  "n < size (x::'a::len0 word) \<Longrightarrow>
wenzelm@65268
  2187
    (x OR y) !! n = (x !! n | y !! n) &
wenzelm@65268
  2188
    (x AND y) !! n = (x !! n & y !! n) &
wenzelm@65268
  2189
    (x XOR y) !! n = (x !! n ~= y !! n) &
haftmann@37660
  2190
    (NOT x) !! n = (~ x !! n)"
huffman@47372
  2191
  unfolding word_size by transfer (simp add: bin_nth_ops)
haftmann@37660
  2192
haftmann@37660
  2193
lemma word_ao_nth:
haftmann@37660
  2194
  fixes x :: "'a::len0 word"
wenzelm@65268
  2195
  shows "(x OR y) !! n = (x !! n | y !! n) &
haftmann@37660
  2196
         (x AND y) !! n = (x !! n & y !! n)"
huffman@47372
  2197
  by transfer (auto simp add: bin_nth_ops)
huffman@46023
  2198
huffman@47108
  2199
lemma test_bit_numeral [simp]:
huffman@47108
  2200
  "(numeral w :: 'a::len0 word) !! n \<longleftrightarrow>
huffman@47108
  2201
    n < len_of TYPE('a) \<and> bin_nth (numeral w) n"
huffman@47372
  2202
  by transfer (rule refl)
huffman@47108
  2203
huffman@47108
  2204
lemma test_bit_neg_numeral [simp]:
haftmann@54489
  2205
  "(- numeral w :: 'a::len0 word) !! n \<longleftrightarrow>
haftmann@54489
  2206
    n < len_of TYPE('a) \<and> bin_nth (- numeral w) n"
huffman@47372
  2207
  by transfer (rule refl)
huffman@46023
  2208
huffman@46172
  2209
lemma test_bit_1 [simp]: "(1::'a::len word) !! n \<longleftrightarrow> n = 0"
huffman@47372
  2210
  by transfer auto
wenzelm@65268
  2211
huffman@46023
  2212
lemma nth_0 [simp]: "~ (0::'a::len0 word) !! n"
huffman@47372
  2213
  by transfer simp
huffman@46023
  2214
huffman@47108
  2215
lemma nth_minus1 [simp]: "(-1::'a::len0 word) !! n \<longleftrightarrow> n < len_of TYPE('a)"
huffman@47372
  2216
  by transfer simp
huffman@47108
  2217
haftmann@37660
  2218
(* get from commutativity, associativity etc of int_and etc
haftmann@37660
  2219
  to same for word_and etc *)
haftmann@37660
  2220
wenzelm@65268
  2221
lemmas bwsimps =
huffman@46013
  2222
  wi_hom_add
haftmann@37660
  2223
  word_wi_log_defs
haftmann@37660
  2224
haftmann@37660
  2225
lemma word_bw_assocs:
haftmann@37660
  2226
  fixes x :: "'a::len0 word"
haftmann@37660
  2227
  shows
haftmann@37660
  2228
  "(x AND y) AND z = x AND y AND z"
haftmann@37660
  2229
  "(x OR y) OR z = x OR y OR z"
haftmann@37660
  2230
  "(x XOR y) XOR z = x XOR y XOR z"
huffman@46022
  2231
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
wenzelm@65268
  2232
haftmann@37660
  2233
lemma word_bw_comms:
haftmann@37660
  2234
  fixes x :: "'a::len0 word"
haftmann@37660
  2235
  shows
haftmann@37660
  2236
  "x AND y = y AND x"
haftmann@37660
  2237
  "x OR y = y OR x"
haftmann@37660
  2238
  "x XOR y = y XOR x"
huffman@46022
  2239
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
wenzelm@65268
  2240
haftmann@37660
  2241
lemma word_bw_lcs:
haftmann@37660
  2242
  fixes x :: "'a::len0 word"
haftmann@37660
  2243
  shows
haftmann@37660
  2244
  "y AND x AND z = x AND y AND z"
haftmann@37660
  2245
  "y OR x OR z = x OR y OR z"
haftmann@37660
  2246
  "y XOR x XOR z = x XOR y XOR z"
huffman@46022
  2247
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
haftmann@37660
  2248
haftmann@37660
  2249
lemma word_log_esimps [simp]:
haftmann@37660
  2250
  fixes x :: "'a::len0 word"
haftmann@37660
  2251
  shows
haftmann@37660
  2252
  "x AND 0 = 0"
haftmann@37660
  2253
  "x AND -1 = x"
haftmann@37660
  2254
  "x OR 0 = x"
haftmann@37660
  2255
  "x OR -1 = -1"
haftmann@37660
  2256
  "x XOR 0 = x"
haftmann@37660
  2257
  "x XOR -1 = NOT x"
haftmann@37660
  2258
  "0 AND x = 0"
haftmann@37660
  2259
  "-1 AND x = x"
haftmann@37660
  2260
  "0 OR x = x"
haftmann@37660
  2261
  "-1 OR x = -1"
haftmann@37660
  2262
  "0 XOR x = x"
haftmann@37660
  2263
  "-1 XOR x = NOT x"
huffman@46023
  2264
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
haftmann@37660
  2265
haftmann@37660
  2266
lemma word_not_dist:
haftmann@37660
  2267
  fixes x :: "'a::len0 word"
haftmann@37660
  2268
  shows
haftmann@37660
  2269
  "NOT (x OR y) = NOT x AND NOT y"
haftmann@37660
  2270
  "NOT (x AND y) = NOT x OR NOT y"
huffman@46022
  2271
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
haftmann@37660
  2272
haftmann@37660
  2273
lemma word_bw_same:
haftmann@37660
  2274
  fixes x :: "'a::len0 word"
haftmann@37660
  2275
  shows
haftmann@37660
  2276
  "x AND x = x"
haftmann@37660
  2277
  "x OR x = x"
haftmann@37660
  2278
  "x XOR x = 0"
huffman@46023
  2279
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
haftmann@37660
  2280
haftmann@37660
  2281
lemma word_ao_absorbs [simp]:
haftmann@37660
  2282
  fixes x :: "'a::len0 word"
haftmann@37660
  2283
  shows
haftmann@37660
  2284
  "x AND (y OR x) = x"
haftmann@37660
  2285
  "x OR y AND x = x"
haftmann@37660
  2286
  "x AND (x OR y) = x"
haftmann@37660
  2287
  "y AND x OR x = x"
haftmann@37660
  2288
  "(y OR x) AND x = x"
haftmann@37660
  2289
  "x OR x AND y = x"
haftmann@37660
  2290
  "(x OR y) AND x = x"
haftmann@37660
  2291
  "x AND y OR x = x"
huffman@46022
  2292
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
haftmann@37660
  2293
haftmann@37660
  2294
lemma word_not_not [simp]:
haftmann@37660
  2295
  "NOT NOT (x::'a::len0 word) = x"
huffman@46022
  2296
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
haftmann@37660
  2297
haftmann@37660
  2298
lemma word_ao_dist:
haftmann@37660
  2299
  fixes x :: "'a::len0 word"
haftmann@37660
  2300
  shows "(x OR y) AND z = x AND z OR y AND z"
huffman@46022
  2301
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
haftmann@37660
  2302
haftmann@37660
  2303
lemma word_oa_dist:
haftmann@37660
  2304
  fixes x :: "'a::len0 word"
haftmann@37660
  2305
  shows "x AND y OR z = (x OR z) AND (y OR z)"
huffman@46022
  2306
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
haftmann@37660
  2307
wenzelm@65268
  2308
lemma word_add_not [simp]:
haftmann@37660
  2309
  fixes x :: "'a::len0 word"
haftmann@37660
  2310
  shows "x + NOT x = -1"
huffman@47372
  2311
  by transfer (simp add: bin_add_not)
haftmann@37660
  2312
haftmann@37660
  2313
lemma word_plus_and_or [simp]:
haftmann@37660
  2314
  fixes x :: "'a::len0 word"
haftmann@37660
  2315
  shows "(x AND y) + (x OR y) = x + y"
huffman@47372
  2316
  by transfer (simp add: plus_and_or)
haftmann@37660
  2317
wenzelm@65268
  2318
lemma leoa:
haftmann@37660
  2319
  fixes x :: "'a::len0 word"
haftmann@40827
  2320
  shows "(w = (x OR y)) \<Longrightarrow> (y = (w AND y))" by auto
wenzelm@65268
  2321
lemma leao:
haftmann@37660
  2322
  fixes x' :: "'a::len0 word"
wenzelm@65268
  2323
  shows "(w' = (x' AND y')) \<Longrightarrow> (x' = (x' OR w'))" by auto
haftmann@37660
  2324
wenzelm@48196
  2325
lemma word_ao_equiv:
wenzelm@48196
  2326
  fixes w w' :: "'a::len0 word"
wenzelm@48196
  2327
  shows "(w = w OR w') = (w' = w AND w')"
wenzelm@48196
  2328
  by (auto intro: leoa leao)
haftmann@37660
  2329
haftmann@37660
  2330
lemma le_word_or2: "x <= x OR (y::'a::len0 word)"
haftmann@37660
  2331
  unfolding word_le_def uint_or
wenzelm@65268
  2332
  by (auto intro: le_int_or)
haftmann@37660
  2333
wenzelm@45604
  2334
lemmas le_word_or1 = xtr3 [OF word_bw_comms (2) le_word_or2]
wenzelm@45604
  2335
lemmas word_and_le1 = xtr3 [OF word_ao_absorbs (4) [symmetric] le_word_or2]
wenzelm@45604
  2336
lemmas word_and_le2 = xtr3 [OF word_ao_absorbs (8) [symmetric] le_word_or2]
haftmann@37660
  2337
wenzelm@65268
  2338
lemma bl_word_not: "to_bl (NOT w) = map Not (to_bl w)"
huffman@45550
  2339
  unfolding to_bl_def word_log_defs bl_not_bin
huffman@45550
  2340
  by (simp add: word_ubin.eq_norm)
haftmann@37660
  2341
wenzelm@65268
  2342
lemma bl_word_xor: "to_bl (v XOR w) = map2 op ~= (to_bl v) (to_bl w)"
haftmann@37660
  2343
  unfolding to_bl_def word_log_defs bl_xor_bin
huffman@45550
  2344
  by (simp add: word_ubin.eq_norm)
haftmann@37660
  2345
wenzelm@65268
  2346
lemma bl_word_or: "to_bl (v OR w) = map2 op | (to_bl v) (to_bl w)"
huffman@45550
  2347
  unfolding to_bl_def word_log_defs bl_or_bin
huffman@45550
  2348
  by (simp add: word_ubin.eq_norm)
haftmann@37660
  2349
wenzelm@65268
  2350
lemma bl_word_and: "to_bl (v AND w) = map2 op & (to_bl v) (to_bl w)"
huffman@45550
  2351
  unfolding to_bl_def word_log_defs bl_and_bin
huffman@45550
  2352
  by (simp add: word_ubin.eq_norm)
haftmann@37660
  2353
haftmann@37660
  2354
lemma word_lsb_alt: "lsb (w::'a::len0 word) = test_bit w 0"
haftmann@37660
  2355
  by (auto simp: word_test_bit_def word_lsb_def)
haftmann@37660
  2356
huffman@45805
  2357
lemma word_lsb_1_0 [simp]: "lsb (1::'a::len word) & ~ lsb (0::'b::len0 word)"
huffman@45550
  2358
  unfolding word_lsb_def uint_eq_0 uint_1 by simp
haftmann@37660
  2359
haftmann@37660
  2360
lemma word_lsb_last: "lsb (w::'a::len word) = last (to_bl w)"
wenzelm@65268
  2361
  apply (unfold word_lsb_def uint_bl bin_to_bl_def)
haftmann@37660
  2362
  apply (rule_tac bin="uint w" in bin_exhaust)
haftmann@37660
  2363
  apply (cases "size w")
haftmann@37660
  2364
   apply auto
haftmann@37660
  2365
   apply (auto simp add: bin_to_bl_aux_alt)
haftmann@37660
  2366
  done
haftmann@37660
  2367
haftmann@37660
  2368
lemma word_lsb_int: "lsb w = (uint w mod 2 = 1)"
huffman@45529
  2369
  unfolding word_lsb_def bin_last_def by auto
haftmann@37660
  2370
wenzelm@65268
  2371
lemma word_msb_sint: "msb w = (sint w < 0)"
huffman@46604
  2372
  unfolding word_msb_def sign_Min_lt_0 ..
haftmann@37660
  2373
huffman@46173
  2374
lemma msb_word_of_int:
huffman@46173
  2375
  "msb (word_of_int x::'a::len word) = bin_nth x (len_of TYPE('a) - 1)"
huffman@46173
  2376
  unfolding word_msb_def by (simp add: word_sbin.eq_norm bin_sign_lem)
huffman@46173
  2377
huffman@47108
  2378
lemma word_msb_numeral [simp]:
huffman@47108
  2379
  "msb (numeral w::'a::len word) = bin_nth (numeral w) (len_of TYPE('a) - 1)"
huffman@47108
  2380
  unfolding word_numeral_alt by (rule msb_word_of_int)
huffman@47108
  2381
huffman@47108
  2382
lemma word_msb_neg_numeral [simp]:
haftmann@54489
  2383
  "msb (- numeral w::'a::len word) = bin_nth (- numeral w) (len_of TYPE('a) - 1)"
huffman@47108
  2384
  unfolding word_neg_numeral_alt by (rule msb_word_of_int)
huffman@46173
  2385
huffman@46173
  2386
lemma word_msb_0 [simp]: "\<not> msb (0::'a::len word)"
huffman@46173
  2387
  unfolding word_msb_def by simp
huffman@46173
  2388
huffman@46173
  2389
lemma word_msb_1 [simp]: "msb (1::'a::len word) \<longleftrightarrow> len_of TYPE('a) = 1"
huffman@46173
  2390
  unfolding word_1_wi msb_word_of_int eq_iff [where 'a=nat]
huffman@46173
  2391
  by (simp add: Suc_le_eq)
huffman@45811
  2392
huffman@45811
  2393
lemma word_msb_nth:
huffman@45811
  2394
  "msb (w::'a::len word) = bin_nth (uint w) (len_of TYPE('a) - 1)"
huffman@46023
  2395
  unfolding word_msb_def sint_uint by (simp add: bin_sign_lem)
haftmann@37660
  2396
haftmann@37660
  2397
lemma word_msb_alt: "msb (w::'a::len word) = hd (to_bl w)"
haftmann@37660
  2398
  apply (unfold word_msb_nth uint_bl)
haftmann@37660
  2399
  apply (subst hd_conv_nth)
haftmann@37660
  2400
  apply (rule length_greater_0_conv [THEN iffD1])
haftmann@37660
  2401
   apply simp
haftmann@37660
  2402
  apply (simp add : nth_bin_to_bl word_size)
haftmann@37660
  2403
  done
haftmann@37660
  2404
huffman@45805
  2405
lemma word_set_nth [simp]:
haftmann@37660
  2406
  "set_bit w n (test_bit w n) = (w::'a::len0 word)"
haftmann@37660
  2407
  unfolding word_test_bit_def word_set_bit_def by auto
haftmann@37660
  2408
haftmann@37660
  2409
lemma bin_nth_uint':
haftmann@37660
  2410
  "bin_nth (uint w) n = (rev (bin_to_bl (size w) (uint w)) ! n & n < size w)"
haftmann@37660
  2411
  apply (unfold word_size)
haftmann@37660
  2412
  apply (safe elim!: bin_nth_uint_imp)
haftmann@37660
  2413
   apply (frule bin_nth_uint_imp)
haftmann@37660
  2414
   apply (fast dest!: bin_nth_bl)+
haftmann@37660
  2415
  done
haftmann@37660
  2416
haftmann@37660
  2417
lemmas bin_nth_uint = bin_nth_uint' [unfolded word_size]
haftmann@37660
  2418
haftmann@37660
  2419
lemma test_bit_bl: "w !! n = (rev (to_bl w) ! n & n < size w)"
haftmann@37660
  2420
  unfolding to_bl_def word_test_bit_def word_size
haftmann@37660
  2421
  by (rule bin_nth_uint)
haftmann@37660
  2422
haftmann@40827
  2423
lemma to_bl_nth: "n < size w \<Longrightarrow> to_bl w ! n = w !! (size w - Suc n)"
haftmann@37660
  2424
  apply (unfold test_bit_bl)
haftmann@37660
  2425
  apply clarsimp
haftmann@37660
  2426
  apply (rule trans)
haftmann@37660
  2427
   apply (rule nth_rev_alt)
haftmann@37660
  2428
   apply (auto simp add: word_size)
haftmann@37660
  2429
  done
haftmann@37660
  2430
wenzelm@65268
  2431
lemma test_bit_set:
haftmann@37660
  2432
  fixes w :: "'a::len0 word"
haftmann@37660
  2433
  shows "(set_bit w n x) !! n = (n < size w & x)"
haftmann@37660
  2434
  unfolding word_size word_test_bit_def word_set_bit_def
haftmann@37660
  2435
  by (clarsimp simp add : word_ubin.eq_norm nth_bintr)
haftmann@37660
  2436
wenzelm@65268
  2437
lemma test_bit_set_gen:
haftmann@37660
  2438
  fixes w :: "'a::len0 word"
wenzelm@65268
  2439
  shows "test_bit (set_bit w n x) m =
haftmann@37660
  2440
         (if m = n then n < size w & x else test_bit w m)"
haftmann@37660
  2441
  apply (unfold word_size word_test_bit_def word_set_bit_def)
haftmann@37660
  2442
  apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen)
haftmann@37660
  2443
  apply (auto elim!: test_bit_size [unfolded word_size]
haftmann@37660
  2444
              simp add: word_test_bit_def [symmetric])
haftmann@37660
  2445
  done
haftmann@37660
  2446
haftmann@37660
  2447
lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs"
haftmann@37660
  2448
  unfolding of_bl_def bl_to_bin_rep_F by auto
wenzelm@65268
  2449
huffman@45811
  2450
lemma msb_nth:
haftmann@37660
  2451
  fixes w :: "'a::len word"
huffman@45811
  2452
  shows "msb w = w !! (len_of TYPE('a) - 1)"
huffman@45811
  2453
  unfolding word_msb_nth word_test_bit_def by simp
haftmann@37660
  2454
wenzelm@45604
  2455
lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN word_ops_nth_size [unfolded word_size]]
haftmann@37660
  2456
lemmas msb1 = msb0 [where i = 0]
haftmann@37660
  2457
lemmas word_ops_msb = msb1 [unfolded msb_nth [symmetric, unfolded One_nat_def]]
haftmann@37660
  2458
wenzelm@45604
  2459
lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size]]
haftmann@37660
  2460
lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt]
haftmann@37660
  2461
huffman@45811
  2462
lemma td_ext_nth [OF refl refl refl, unfolded word_size]:
wenzelm@65268
  2463
  "n = size (w::'a::len0 word) \<Longrightarrow> ofn = set_bits \<Longrightarrow> [w, ofn g] = l \<Longrightarrow>
haftmann@37660
  2464
    td_ext test_bit ofn {f. ALL i. f i --> i < n} (%h i. h i & i < n)"
haftmann@37660
  2465
  apply (unfold word_size td_ext_def')
wenzelm@46008
  2466
  apply safe
haftmann@37660
  2467
     apply (rule_tac [3] ext)
haftmann@37660
  2468
     apply (rule_tac [4] ext)
haftmann@37660
  2469
     apply (unfold word_size of_nth_def test_bit_bl)
haftmann@37660
  2470
     apply safe
haftmann@37660
  2471
       defer
haftmann@37660
  2472
       apply (clarsimp simp: word_bl.Abs_inverse)+
haftmann@37660
  2473
  apply (rule word_bl.Rep_inverse')
haftmann@37660
  2474
  apply (rule sym [THEN trans])
haftmann@37660
  2475
  apply (rule bl_of_nth_nth)
haftmann@37660
  2476
  apply simp
haftmann@37660
  2477
  apply (rule bl_of_nth_inj)
haftmann@37660
  2478
  apply (clarsimp simp add : test_bit_bl word_size)
haftmann@37660
  2479
  done
haftmann@37660
  2480
haftmann@37660
  2481
interpretation test_bit:
haftmann@37660
  2482
  td_ext "op !! :: 'a::len0 word => nat => bool"
haftmann@37660
  2483
         set_bits
haftmann@37660
  2484
         "{f. \<forall>i. f i \<longrightarrow> i < len_of TYPE('a::len0)}"
haftmann@37660
  2485
         "(\<lambda>h i. h i \<and> i < len_of TYPE('a::len0))"
haftmann@37660
  2486
  by (rule td_ext_nth)
haftmann@37660
  2487
haftmann@37660
  2488
lemmas td_nth = test_bit.td_thm
haftmann@37660
  2489
huffman@45805
  2490
lemma word_set_set_same [simp]:
haftmann@37660
  2491
  fixes w :: "'a::len0 word"
wenzelm@65268
  2492
  shows "set_bit (set_bit w n x) n y = set_bit w n y"
haftmann@37660
  2493
  by (rule word_eqI) (simp add : test_bit_set_gen word_size)
wenzelm@65268
  2494
wenzelm@65268
  2495
lemma word_set_set_diff:
haftmann@37660
  2496
  fixes w :: "'a::len0 word"
haftmann@37660
  2497
  assumes "m ~= n"
wenzelm@65268
  2498
  shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x"
wenzelm@41550
  2499
  by (rule word_eqI) (clarsimp simp add: test_bit_set_gen word_size assms)
huffman@46001
  2500
wenzelm@65268
  2501
lemma nth_sint:
haftmann@37660
  2502
  fixes w :: "'a::len word"
wenzelm@65268
  2503
  defines "l \<equiv> len_of TYPE('a)"
haftmann@37660
  2504
  shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))"
haftmann@37660
  2505
  unfolding sint_uint l_def
haftmann@37660
  2506
  by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric])
haftmann@37660
  2507
huffman@47108
  2508
lemma word_lsb_numeral [simp]:
wenzelm@65268
  2509
  "lsb (numeral bin :: 'a::len word) \<longleftrightarrow> bin_last (numeral bin)"
huffman@47108
  2510
  unfolding word_lsb_alt test_bit_numeral by simp
huffman@47108
  2511
huffman@47108
  2512
lemma word_lsb_neg_numeral [simp]:
wenzelm@65268
  2513
  "lsb (- numeral bin :: 'a::len word) \<longleftrightarrow> bin_last (- numeral bin)"
huffman@47108
  2514
  unfolding word_lsb_alt test_bit_neg_numeral by simp
haftmann@37660
  2515
huffman@46173
  2516
lemma set_bit_word_of_int:
haftmann@54847
  2517
  "set_bit (word_of_int x) n b = word_of_int (bin_sc n b x)"
huffman@46173
  2518
  unfolding word_set_bit_def
huffman@46173
  2519
  apply (rule word_eqI)
huffman@46173
  2520
  apply (simp add: word_size bin_nth_sc_gen word_ubin.eq_norm nth_bintr)
huffman@46173
  2521
  done
huffman@46173
  2522
huffman@47108
  2523
lemma word_set_numeral [simp]:
wenzelm@65268
  2524
  "set_bit (numeral bin::'a::len0 word) n b =
haftmann@54847
  2525
    word_of_int (bin_sc n b (numeral bin))"
huffman@47108
  2526
  unfolding word_numeral_alt by (rule set_bit_word_of_int)
huffman@47108
  2527
huffman@47108
  2528
lemma word_set_neg_numeral [simp]:
wenzelm@65268
  2529
  "set_bit (- numeral bin::'a::len0 word) n b =
haftmann@54847
  2530
    word_of_int (bin_sc n b (- numeral bin))"
huffman@47108
  2531
  unfolding word_neg_numeral_alt by (rule set_bit_word_of_int)
huffman@46173
  2532
huffman@46173
  2533
lemma word_set_bit_0 [simp]:
haftmann@54847
  2534
  "set_bit 0 n b = word_of_int (bin_sc n b 0)"
huffman@46173
  2535
  unfolding word_0_wi by (rule set_bit_word_of_int)
huffman@46173
  2536
huffman@46173
  2537
lemma word_set_bit_1 [simp]:
haftmann@54847
  2538
  "set_bit 1 n b = word_of_int (bin_sc n b 1)"
huffman@46173
  2539
  unfolding word_1_wi by (rule set_bit_word_of_int)
haftmann@37660
  2540
huffman@45805
  2541
lemma setBit_no [simp]:
haftmann@54847
  2542
  "setBit (numeral bin) n = word_of_int (bin_sc n True (numeral bin))"
huffman@45805
  2543
  by (simp add: setBit_def)
huffman@45805
  2544
huffman@45805
  2545
lemma clearBit_no [simp]:
haftmann@54847
  2546
  "clearBit (numeral bin) n = word_of_int (bin_sc n False (numeral bin))"
huffman@45805
  2547
  by (simp add: clearBit_def)
haftmann@37660
  2548
wenzelm@65268
  2549
lemma to_bl_n1:
wenzelm@65268
  2550
  "to_bl (-1::'a::len0 word) = replicate (len_of TYPE('a)) True"
haftmann@37660
  2551
  apply (rule word_bl.Abs_inverse')
haftmann@37660
  2552
   apply simp
haftmann@37660
  2553
  apply (rule word_eqI)
huffman@45805
  2554
  apply (clarsimp simp add: word_size)
haftmann@37660
  2555
  apply (auto simp add: word_bl.Abs_inverse test_bit_bl word_size)
haftmann@37660
  2556
  done
haftmann@37660
  2557
huffman@45805
  2558
lemma word_msb_n1 [simp]: "msb (-1::'a::len word)"
wenzelm@41550
  2559
  unfolding word_msb_alt to_bl_n1 by simp
haftmann@37660
  2560
wenzelm@65268
  2561
lemma word_set_nth_iff:
haftmann@37660
  2562
  "(set_bit w n b = w) = (w !! n = b | n >= size (w::'a::len0 word))"
haftmann@37660
  2563
  apply (rule iffI)
haftmann@37660
  2564
   apply (rule disjCI)
haftmann@37660
  2565
   apply (drule word_eqD)
haftmann@37660
  2566
   apply (erule sym [THEN trans])
haftmann@37660
  2567
   apply (simp add: test_bit_set)
haftmann@37660
  2568
  apply (erule disjE)
haftmann@37660
  2569
   apply clarsimp
haftmann@37660
  2570
  apply (rule word_eqI)
haftmann@37660
  2571
  apply (clarsimp simp add : test_bit_set_gen)
haftmann@37660
  2572
  apply (drule test_bit_size)
haftmann@37660
  2573
  apply force
haftmann@37660
  2574
  done
haftmann@37660
  2575
huffman@45811
  2576
lemma test_bit_2p:
huffman@45811
  2577
  "(word_of_int (2 ^ n)::'a::len word) !! m \<longleftrightarrow> m = n \<and> m < len_of TYPE('a)"
huffman@45811
  2578
  unfolding word_test_bit_def
haftmann@37660
  2579
  by (auto simp add: word_ubin.eq_norm nth_bintr nth_2p_bin)
haftmann@37660
  2580
haftmann@37660
  2581
lemma nth_w2p:
wenzelm@61076
  2582
  "((2::'a::len word) ^ n) !! m \<longleftrightarrow> m = n \<and> m < len_of TYPE('a::len)"
haftmann@37660
  2583
  unfolding test_bit_2p [symmetric] word_of_int [symmetric]
haftmann@64593
  2584
  by simp
haftmann@37660
  2585
wenzelm@65268
  2586
lemma uint_2p:
haftmann@40827
  2587
  "(0::'a::len word) < 2 ^ n \<Longrightarrow> uint (2 ^ n::'a::len word) = 2 ^ n"
haftmann@37660
  2588
  apply (unfold word_arith_power_alt)
wenzelm@65268
  2589
  apply (case_tac "len_of TYPE('a)")
haftmann@37660
  2590
   apply clarsimp
haftmann@37660
  2591
  apply (case_tac "nat")
haftmann@37660
  2592
   apply clarsimp
haftmann@37660
  2593
   apply (case_tac "n")
huffman@46001
  2594
    apply clarsimp
huffman@46001
  2595
   apply clarsimp
haftmann@37660
  2596
  apply (drule word_gt_0 [THEN iffD1])
haftmann@54489
  2597
  apply (safe intro!: word_eqI)
haftmann@54489
  2598
  apply (auto simp add: nth_2p_bin)
haftmann@54489
  2599
  apply (erule notE)
haftmann@54489
  2600
  apply (simp (no_asm_use) add: uint_word_of_int word_size)
haftmann@54489
  2601
  apply (subst mod_pos_pos_trivial)
haftmann@54489
  2602
  apply simp
haftmann@54489
  2603
  apply (rule power_strict_increasing)
haftmann@54489
  2604
  apply simp_all
haftmann@37660
  2605
  done
haftmann@37660
  2606
wenzelm@65268
  2607
lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a::len word) = 2 ^ n"
haftmann@64593
  2608
  by (induct n) (simp_all add: wi_hom_syms)
haftmann@37660
  2609
wenzelm@65268
  2610
lemma bang_is_le: "x !! m \<Longrightarrow> 2 ^ m <= (x :: 'a::len word)"
wenzelm@65268
  2611
  apply (rule xtr3)
haftmann@37660
  2612
  apply (rule_tac [2] y = "x" in le_word_or2)
haftmann@37660
  2613
  apply (rule word_eqI)
haftmann@37660
  2614
  apply (auto simp add: word_ao_nth nth_w2p word_size)
haftmann@37660
  2615
  done
haftmann@37660
  2616
wenzelm@65268
  2617
lemma word_clr_le:
haftmann@37660
  2618
  fixes w :: "'a::len0 word"
haftmann@37660
  2619
  shows "w >= set_bit w n False"
haftmann@37660
  2620
  apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)