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