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