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