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