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