src/HOL/Word/WordDefinition.thy
author wenzelm
Wed Sep 17 21:27:14 2008 +0200 (2008-09-17)
changeset 28263 69eaa97e7e96
parent 27139 a1f3c7b5ce9c
child 28524 644b62cf678f
permissions -rw-r--r--
moved global ML bindings to global place;
kleing@24333
     1
(* 
kleing@24333
     2
  ID:     $Id$
kleing@24333
     3
  Author: Jeremy Dawson and Gerwin Klein, NICTA
kleing@24333
     4
  
kleing@24333
     5
  Basic definition of word type and basic theorems following from 
kleing@24333
     6
  the definition of the word type 
kleing@24333
     7
*) 
kleing@24333
     8
huffman@24350
     9
header {* Definition of Word Type *}
kleing@24333
    10
haftmann@26559
    11
theory WordDefinition
wenzelm@27139
    12
imports Size BinBoolList TdThs
haftmann@26559
    13
begin
kleing@24333
    14
kleing@24333
    15
typedef (open word) 'a word
huffman@24465
    16
  = "{(0::int) ..< 2^len_of TYPE('a::len0)}" by auto
kleing@24333
    17
haftmann@25762
    18
instantiation word :: (len0) size
haftmann@25762
    19
begin
haftmann@25762
    20
haftmann@25762
    21
definition
haftmann@25762
    22
  word_size: "size (w :: 'a word) = len_of TYPE('a)"
haftmann@25762
    23
haftmann@25762
    24
instance ..
haftmann@25762
    25
haftmann@25762
    26
end
haftmann@25762
    27
haftmann@25762
    28
definition
haftmann@25762
    29
  -- {* representation of words using unsigned or signed bins, 
haftmann@25762
    30
        only difference in these is the type class *}
haftmann@25762
    31
  word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word"
haftmann@25762
    32
where
haftmann@26514
    33
  [code func del]: "word_of_int w = Abs_word (bintrunc (len_of TYPE ('a)) w)" 
haftmann@26514
    34
haftmann@26514
    35
code_datatype word_of_int
kleing@24333
    36
kleing@24333
    37
huffman@24350
    38
subsection "Type conversions and casting"
kleing@24333
    39
kleing@24333
    40
constdefs
kleing@24333
    41
  -- {* uint and sint cast a word to an integer,
kleing@24333
    42
        uint treats the word as unsigned,
kleing@24333
    43
        sint treats the most-significant-bit as a sign bit *}
huffman@24465
    44
  uint :: "'a :: len0 word => int"
kleing@24333
    45
  "uint w == Rep_word w"
huffman@24465
    46
  sint :: "'a :: len word => int"
huffman@24465
    47
  sint_uint: "sint w == sbintrunc (len_of TYPE ('a) - 1) (uint w)"
huffman@24465
    48
  unat :: "'a :: len0 word => nat"
kleing@24333
    49
  "unat w == nat (uint w)"
kleing@24333
    50
kleing@24333
    51
  -- "the sets of integers representing the words"
kleing@24333
    52
  uints :: "nat => int set"
kleing@24333
    53
  "uints n == range (bintrunc n)"
kleing@24333
    54
  sints :: "nat => int set"
kleing@24333
    55
  "sints n == range (sbintrunc (n - 1))"
kleing@24333
    56
  unats :: "nat => nat set"
kleing@24333
    57
  "unats n == {i. i < 2 ^ n}"
kleing@24333
    58
  norm_sint :: "nat => int => int"
kleing@24333
    59
  "norm_sint n w == (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)"
kleing@24333
    60
huffman@24465
    61
  -- "cast a word to a different length"
huffman@24465
    62
  scast :: "'a :: len word => 'b :: len word"
huffman@24465
    63
  "scast w == word_of_int (sint w)"
huffman@24465
    64
  ucast :: "'a :: len0 word => 'b :: len0 word"
huffman@24465
    65
  "ucast w == word_of_int (uint w)"
huffman@24465
    66
huffman@24465
    67
  -- "whether a cast (or other) function is to a longer or shorter length"
huffman@24465
    68
  source_size :: "('a :: len0 word => 'b) => nat"
huffman@24465
    69
  "source_size c == let arb = arbitrary ; x = c arb in size arb"  
huffman@24465
    70
  target_size :: "('a => 'b :: len0 word) => nat"
huffman@24465
    71
  "target_size c == size (c arbitrary)"
huffman@24465
    72
  is_up :: "('a :: len0 word => 'b :: len0 word) => bool"
huffman@24465
    73
  "is_up c == source_size c <= target_size c"
huffman@24465
    74
  is_down :: "('a :: len0 word => 'b :: len0 word) => bool"
huffman@24465
    75
  "is_down c == target_size c <= source_size c"
huffman@24465
    76
huffman@24465
    77
constdefs
huffman@24465
    78
  of_bl :: "bool list => 'a :: len0 word" 
huffman@24465
    79
  "of_bl bl == word_of_int (bl_to_bin bl)"
huffman@24465
    80
  to_bl :: "'a :: len0 word => bool list"
huffman@24465
    81
  "to_bl w == 
huffman@24465
    82
  bin_to_bl (len_of TYPE ('a)) (uint w)"
huffman@24465
    83
huffman@24465
    84
  word_reverse :: "'a :: len0 word => 'a word"
huffman@24465
    85
  "word_reverse w == of_bl (rev (to_bl w))"
huffman@24465
    86
kleing@24333
    87
constdefs
huffman@24465
    88
  word_int_case :: "(int => 'b) => ('a :: len0 word) => 'b"
kleing@24333
    89
  "word_int_case f w == f (uint w)"
kleing@24333
    90
kleing@24333
    91
syntax
kleing@24333
    92
  of_int :: "int => 'a"
kleing@24333
    93
translations
kleing@24333
    94
  "case x of of_int y => b" == "word_int_case (%y. b) x"
kleing@24333
    95
kleing@24333
    96
huffman@24350
    97
subsection  "Arithmetic operations"
kleing@24333
    98
haftmann@26514
    99
declare uint_def [code func del]
haftmann@26514
   100
haftmann@26514
   101
lemma [code func]: "uint (word_of_int w \<Colon> 'a\<Colon>len0 word) = bintrunc (len_of TYPE('a)) w"
haftmann@26559
   102
  by (auto simp add: uint_def word_of_int_def intro!: Abs_word_inverse)
haftmann@26559
   103
    (insert range_bintrunc, auto)
haftmann@26514
   104
haftmann@25762
   105
instantiation word :: (len0) "{number, uminus, minus, plus, one, zero, times, Divides.div, power, ord, bit}"
haftmann@25762
   106
begin
haftmann@25762
   107
haftmann@25762
   108
definition
haftmann@25762
   109
  word_0_wi: "0 = word_of_int 0"
haftmann@25762
   110
haftmann@25762
   111
definition
haftmann@25762
   112
  word_1_wi: "1 = word_of_int 1"
haftmann@25762
   113
haftmann@25762
   114
definition
haftmann@25762
   115
  word_add_def: "a + b = word_of_int (uint a + uint b)"
haftmann@25762
   116
haftmann@25762
   117
definition
haftmann@25762
   118
  word_sub_wi: "a - b = word_of_int (uint a - uint b)"
haftmann@25762
   119
haftmann@25762
   120
definition
haftmann@25762
   121
  word_minus_def: "- a = word_of_int (- uint a)"
haftmann@25762
   122
haftmann@25762
   123
definition
haftmann@25762
   124
  word_mult_def: "a * b = word_of_int (uint a * uint b)"
haftmann@25762
   125
haftmann@25762
   126
definition
haftmann@25762
   127
  word_div_def: "a div b = word_of_int (uint a div uint b)"
haftmann@25762
   128
haftmann@25762
   129
definition
haftmann@25762
   130
  word_mod_def: "a mod b = word_of_int (uint a mod uint b)"
haftmann@25762
   131
haftmann@25762
   132
primrec power_word where
haftmann@25762
   133
  "(a\<Colon>'a word) ^ 0 = 1"
haftmann@25762
   134
  | "(a\<Colon>'a word) ^ Suc n = a * a ^ n"
haftmann@25762
   135
haftmann@25762
   136
definition
haftmann@25762
   137
  word_number_of_def: "number_of w = word_of_int w"
haftmann@25762
   138
haftmann@25762
   139
definition
haftmann@25762
   140
  word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
huffman@24415
   141
haftmann@25762
   142
definition
haftmann@25762
   143
  word_less_def: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> (y \<Colon> 'a word)"
haftmann@25762
   144
haftmann@25762
   145
definition
haftmann@25762
   146
  word_and_def: 
haftmann@25762
   147
  "(a::'a word) AND b = word_of_int (uint a AND uint b)"
haftmann@25762
   148
haftmann@25762
   149
definition
haftmann@25762
   150
  word_or_def:  
haftmann@25762
   151
  "(a::'a word) OR b = word_of_int (uint a OR uint b)"
haftmann@25762
   152
haftmann@25762
   153
definition
haftmann@25762
   154
  word_xor_def: 
haftmann@25762
   155
  "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
haftmann@25762
   156
haftmann@25762
   157
definition
haftmann@25762
   158
  word_not_def: 
haftmann@25762
   159
  "NOT (a::'a word) = word_of_int (NOT (uint a))"
haftmann@25762
   160
haftmann@25762
   161
instance ..
haftmann@25762
   162
haftmann@25762
   163
end 
haftmann@25762
   164
haftmann@25762
   165
definition
haftmann@25762
   166
  word_succ :: "'a :: len0 word => 'a word"
haftmann@25762
   167
where
haftmann@25919
   168
  "word_succ a = word_of_int (Int.succ (uint a))"
haftmann@25762
   169
haftmann@25762
   170
definition
haftmann@25762
   171
  word_pred :: "'a :: len0 word => 'a word"
haftmann@25762
   172
where
haftmann@25919
   173
  "word_pred a = word_of_int (Int.pred (uint a))"
kleing@24333
   174
kleing@24333
   175
constdefs
huffman@24465
   176
  udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50)
huffman@24465
   177
  "a udvd b == EX n>=0. uint b = n * uint a"
huffman@24465
   178
huffman@24465
   179
  word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50)
huffman@24465
   180
  "a <=s b == sint a <= sint b"
huffman@24465
   181
huffman@24465
   182
  word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50)
huffman@24465
   183
  "(x <s y) == (x <=s y & x ~= y)"
huffman@24465
   184
kleing@24333
   185
haftmann@26559
   186
huffman@24350
   187
subsection "Bit-wise operations"
kleing@24333
   188
haftmann@26559
   189
haftmann@26559
   190
haftmann@26559
   191
instantiation word :: (len0) bits
haftmann@26559
   192
begin
kleing@24333
   193
haftmann@26559
   194
definition
haftmann@26559
   195
  word_test_bit_def: "test_bit a = bin_nth (uint a)"
haftmann@26559
   196
haftmann@26559
   197
definition
haftmann@26559
   198
  word_set_bit_def: "set_bit a n x =
kleing@24333
   199
   word_of_int (bin_sc n (If x bit.B1 bit.B0) (uint a))"
kleing@24333
   200
haftmann@26559
   201
definition
haftmann@26559
   202
  word_set_bits_def: "(BITS n. f n) = of_bl (bl_of_nth (len_of TYPE ('a)) f)"
haftmann@26559
   203
haftmann@26559
   204
definition
haftmann@26559
   205
  word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a) = bit.B1"
haftmann@26559
   206
haftmann@26559
   207
definition shiftl1 :: "'a word \<Rightarrow> 'a word" where
haftmann@26559
   208
  "shiftl1 w = word_of_int (uint w BIT bit.B0)"
haftmann@26559
   209
haftmann@26559
   210
definition shiftr1 :: "'a word \<Rightarrow> 'a word" where
haftmann@26559
   211
  -- "shift right as unsigned or as signed, ie logical or arithmetic"
haftmann@26559
   212
  "shiftr1 w = word_of_int (bin_rest (uint w))"
haftmann@26559
   213
haftmann@26559
   214
definition
haftmann@26559
   215
  shiftl_def: "w << n = (shiftl1 ^ n) w"
huffman@24465
   216
haftmann@26559
   217
definition
haftmann@26559
   218
  shiftr_def: "w >> n = (shiftr1 ^ n) w"
haftmann@26559
   219
haftmann@26559
   220
instance ..
haftmann@26559
   221
haftmann@26559
   222
end
kleing@24333
   223
haftmann@26559
   224
instantiation word :: (len) bitss
haftmann@26559
   225
begin
haftmann@26559
   226
haftmann@26559
   227
definition
kleing@24333
   228
  word_msb_def: 
haftmann@26559
   229
  "msb a \<longleftrightarrow> bin_sign (sint a) = Int.Min"
kleing@24333
   230
haftmann@26559
   231
instance ..
haftmann@26559
   232
haftmann@26559
   233
end
kleing@24333
   234
kleing@24333
   235
constdefs
huffman@24465
   236
  setBit :: "'a :: len0 word => nat => 'a word" 
kleing@24333
   237
  "setBit w n == set_bit w n True"
kleing@24333
   238
huffman@24465
   239
  clearBit :: "'a :: len0 word => nat => 'a word" 
kleing@24333
   240
  "clearBit w n == set_bit w n False"
kleing@24333
   241
kleing@24333
   242
huffman@24465
   243
subsection "Shift operations"
huffman@24465
   244
huffman@24465
   245
constdefs
huffman@24465
   246
  sshiftr1 :: "'a :: len word => 'a word" 
huffman@24465
   247
  "sshiftr1 w == word_of_int (bin_rest (sint w))"
huffman@24465
   248
huffman@24465
   249
  bshiftr1 :: "bool => 'a :: len word => 'a word"
huffman@24465
   250
  "bshiftr1 b w == of_bl (b # butlast (to_bl w))"
huffman@24465
   251
huffman@24465
   252
  sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55)
huffman@24465
   253
  "w >>> n == (sshiftr1 ^ n) w"
huffman@24465
   254
huffman@24465
   255
  mask :: "nat => 'a::len word"
huffman@24465
   256
  "mask n == (1 << n) - 1"
huffman@24465
   257
huffman@24465
   258
  revcast :: "'a :: len0 word => 'b :: len0 word"
huffman@24465
   259
  "revcast w ==  of_bl (takefill False (len_of TYPE('b)) (to_bl w))"
huffman@24465
   260
huffman@24465
   261
  slice1 :: "nat => 'a :: len0 word => 'b :: len0 word"
huffman@24465
   262
  "slice1 n w == of_bl (takefill False n (to_bl w))"
huffman@24465
   263
huffman@24465
   264
  slice :: "nat => 'a :: len0 word => 'b :: len0 word"
huffman@24465
   265
  "slice n w == slice1 (size w - n) w"
huffman@24465
   266
huffman@24465
   267
huffman@24465
   268
subsection "Rotation"
huffman@24465
   269
huffman@24465
   270
constdefs
huffman@24465
   271
  rotater1 :: "'a list => 'a list"
huffman@24465
   272
  "rotater1 ys == 
huffman@24465
   273
    case ys of [] => [] | x # xs => last ys # butlast ys"
huffman@24465
   274
huffman@24465
   275
  rotater :: "nat => 'a list => 'a list"
huffman@24465
   276
  "rotater n == rotater1 ^ n"
huffman@24465
   277
huffman@24465
   278
  word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word"
huffman@24465
   279
  "word_rotr n w == of_bl (rotater n (to_bl w))"
huffman@24465
   280
huffman@24465
   281
  word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word"
huffman@24465
   282
  "word_rotl n w == of_bl (rotate n (to_bl w))"
huffman@24465
   283
huffman@24465
   284
  word_roti :: "int => 'a :: len0 word => 'a :: len0 word"
huffman@24465
   285
  "word_roti i w == if i >= 0 then word_rotr (nat i) w
huffman@24465
   286
                    else word_rotl (nat (- i)) w"
huffman@24465
   287
huffman@24465
   288
huffman@24465
   289
subsection "Split and cat operations"
huffman@24465
   290
huffman@24465
   291
constdefs
huffman@24465
   292
  word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word"
huffman@24465
   293
  "word_cat a b == word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))"
huffman@24465
   294
huffman@24465
   295
  word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)"
huffman@24465
   296
  "word_split a == 
huffman@24465
   297
   case bin_split (len_of TYPE ('c)) (uint a) of 
huffman@24465
   298
     (u, v) => (word_of_int u, word_of_int v)"
huffman@24465
   299
huffman@24465
   300
  word_rcat :: "'a :: len0 word list => 'b :: len0 word"
huffman@24465
   301
  "word_rcat ws == 
huffman@24465
   302
  word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))"
huffman@24465
   303
huffman@24465
   304
  word_rsplit :: "'a :: len0 word => 'b :: len word list"
huffman@24465
   305
  "word_rsplit w == 
huffman@24465
   306
  map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))"
huffman@24465
   307
kleing@24333
   308
constdefs
kleing@24333
   309
  -- "Largest representable machine integer."
huffman@24465
   310
  max_word :: "'a::len word"
huffman@24465
   311
  "max_word \<equiv> word_of_int (2^len_of TYPE('a) - 1)"
kleing@24333
   312
kleing@24333
   313
consts 
huffman@24465
   314
  of_bool :: "bool \<Rightarrow> 'a::len word"
kleing@24333
   315
primrec
kleing@24333
   316
  "of_bool False = 0"
kleing@24333
   317
  "of_bool True = 1"
kleing@24333
   318
kleing@24333
   319
huffman@24465
   320
lemmas of_nth_def = word_set_bits_def
huffman@24465
   321
kleing@24333
   322
lemmas word_size_gt_0 [iff] = 
haftmann@25762
   323
  xtr1 [OF word_size len_gt_0, standard]
huffman@24465
   324
lemmas lens_gt_0 = word_size_gt_0 len_gt_0
kleing@24333
   325
lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard]
kleing@24333
   326
kleing@24333
   327
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
kleing@24333
   328
  by (simp add: uints_def range_bintrunc)
kleing@24333
   329
kleing@24333
   330
lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}"
kleing@24333
   331
  by (simp add: sints_def range_sbintrunc)
kleing@24333
   332
kleing@24333
   333
lemmas atLeastLessThan_alt = atLeastLessThan_def [unfolded 
kleing@24333
   334
  atLeast_def lessThan_def Collect_conj_eq [symmetric]]
kleing@24333
   335
  
kleing@24333
   336
lemma mod_in_reps: "m > 0 ==> y mod m : {0::int ..< m}"
kleing@24333
   337
  unfolding atLeastLessThan_alt by auto
kleing@24333
   338
kleing@24333
   339
lemma 
kleing@24333
   340
  Rep_word_0:"0 <= Rep_word x" and 
huffman@24465
   341
  Rep_word_lt: "Rep_word (x::'a::len0 word) < 2 ^ len_of TYPE('a)"
kleing@24333
   342
  by (auto simp: Rep_word [simplified])
kleing@24333
   343
kleing@24333
   344
lemma Rep_word_mod_same:
huffman@24465
   345
  "Rep_word x mod 2 ^ len_of TYPE('a) = Rep_word (x::'a::len0 word)"
kleing@24333
   346
  by (simp add: int_mod_eq Rep_word_lt Rep_word_0)
kleing@24333
   347
kleing@24333
   348
lemma td_ext_uint: 
huffman@24465
   349
  "td_ext (uint :: 'a word => int) word_of_int (uints (len_of TYPE('a::len0))) 
huffman@24465
   350
    (%w::int. w mod 2 ^ len_of TYPE('a))"
kleing@24333
   351
  apply (unfold td_ext_def')
kleing@24333
   352
  apply (simp add: uints_num uint_def word_of_int_def bintrunc_mod2p)
kleing@24333
   353
  apply (simp add: Rep_word_mod_same Rep_word_0 Rep_word_lt
kleing@24333
   354
                   word.Rep_word_inverse word.Abs_word_inverse int_mod_lem)
kleing@24333
   355
  done
kleing@24333
   356
kleing@24333
   357
lemmas int_word_uint = td_ext_uint [THEN td_ext.eq_norm, standard]
kleing@24333
   358
kleing@24333
   359
interpretation word_uint: 
huffman@24465
   360
  td_ext ["uint::'a::len0 word \<Rightarrow> int" 
kleing@24333
   361
          word_of_int 
huffman@24465
   362
          "uints (len_of TYPE('a::len0))"
huffman@24465
   363
          "\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)"]
kleing@24333
   364
  by (rule td_ext_uint)
kleing@24333
   365
  
kleing@24333
   366
lemmas td_uint = word_uint.td_thm
kleing@24333
   367
kleing@24333
   368
lemmas td_ext_ubin = td_ext_uint 
huffman@24465
   369
  [simplified len_gt_0 no_bintr_alt1 [symmetric]]
kleing@24333
   370
kleing@24333
   371
interpretation word_ubin:
huffman@24465
   372
  td_ext ["uint::'a::len0 word \<Rightarrow> int" 
kleing@24333
   373
          word_of_int 
huffman@24465
   374
          "uints (len_of TYPE('a::len0))"
huffman@24465
   375
          "bintrunc (len_of TYPE('a::len0))"]
kleing@24333
   376
  by (rule td_ext_ubin)
kleing@24333
   377
kleing@24333
   378
lemma sint_sbintrunc': 
kleing@24333
   379
  "sint (word_of_int bin :: 'a word) = 
huffman@24465
   380
    (sbintrunc (len_of TYPE ('a :: len) - 1) bin)"
kleing@24333
   381
  unfolding sint_uint 
kleing@24333
   382
  by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt)
kleing@24333
   383
kleing@24333
   384
lemma uint_sint: 
huffman@24465
   385
  "uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a :: len word))"
kleing@24333
   386
  unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le)
kleing@24333
   387
kleing@24333
   388
lemma bintr_uint': 
kleing@24333
   389
  "n >= size w ==> bintrunc n (uint w) = uint w"
kleing@24333
   390
  apply (unfold word_size)
kleing@24333
   391
  apply (subst word_ubin.norm_Rep [symmetric]) 
kleing@24333
   392
  apply (simp only: bintrunc_bintrunc_min word_size min_def)
kleing@24333
   393
  apply simp
kleing@24333
   394
  done
kleing@24333
   395
kleing@24333
   396
lemma wi_bintr': 
kleing@24333
   397
  "wb = word_of_int bin ==> n >= size wb ==> 
kleing@24333
   398
    word_of_int (bintrunc n bin) = wb"
kleing@24333
   399
  unfolding word_size
kleing@24333
   400
  by (clarsimp simp add : word_ubin.norm_eq_iff [symmetric] min_def)
kleing@24333
   401
kleing@24333
   402
lemmas bintr_uint = bintr_uint' [unfolded word_size]
kleing@24333
   403
lemmas wi_bintr = wi_bintr' [unfolded word_size]
kleing@24333
   404
kleing@24333
   405
lemma td_ext_sbin: 
huffman@24465
   406
  "td_ext (sint :: 'a word => int) word_of_int (sints (len_of TYPE('a::len))) 
huffman@24465
   407
    (sbintrunc (len_of TYPE('a) - 1))"
kleing@24333
   408
  apply (unfold td_ext_def' sint_uint)
kleing@24333
   409
  apply (simp add : word_ubin.eq_norm)
huffman@24465
   410
  apply (cases "len_of TYPE('a)")
kleing@24333
   411
   apply (auto simp add : sints_def)
kleing@24333
   412
  apply (rule sym [THEN trans])
kleing@24333
   413
  apply (rule word_ubin.Abs_norm)
kleing@24333
   414
  apply (simp only: bintrunc_sbintrunc)
kleing@24333
   415
  apply (drule sym)
kleing@24333
   416
  apply simp
kleing@24333
   417
  done
kleing@24333
   418
kleing@24333
   419
lemmas td_ext_sint = td_ext_sbin 
huffman@24465
   420
  [simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]]
kleing@24333
   421
kleing@24333
   422
(* We do sint before sbin, before sint is the user version
kleing@24333
   423
   and interpretations do not produce thm duplicates. I.e. 
kleing@24333
   424
   we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD,
kleing@24333
   425
   because the latter is the same thm as the former *)
kleing@24333
   426
interpretation word_sint:
huffman@24465
   427
  td_ext ["sint ::'a::len word => int" 
kleing@24333
   428
          word_of_int 
huffman@24465
   429
          "sints (len_of TYPE('a::len))"
huffman@24465
   430
          "%w. (w + 2^(len_of TYPE('a::len) - 1)) mod 2^len_of TYPE('a::len) -
huffman@24465
   431
               2 ^ (len_of TYPE('a::len) - 1)"]
kleing@24333
   432
  by (rule td_ext_sint)
kleing@24333
   433
kleing@24333
   434
interpretation word_sbin:
huffman@24465
   435
  td_ext ["sint ::'a::len word => int" 
kleing@24333
   436
          word_of_int 
huffman@24465
   437
          "sints (len_of TYPE('a::len))"
huffman@24465
   438
          "sbintrunc (len_of TYPE('a::len) - 1)"]
kleing@24333
   439
  by (rule td_ext_sbin)
kleing@24333
   440
kleing@24333
   441
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm, standard]
kleing@24333
   442
kleing@24333
   443
lemmas td_sint = word_sint.td
kleing@24333
   444
kleing@24333
   445
lemma word_number_of_alt: "number_of b == word_of_int (number_of b)"
kleing@24333
   446
  unfolding word_number_of_def by (simp add: number_of_eq)
kleing@24333
   447
kleing@24333
   448
lemma word_no_wi: "number_of = word_of_int"
kleing@24333
   449
  by (auto simp: word_number_of_def intro: ext)
kleing@24333
   450
huffman@24465
   451
lemma to_bl_def': 
huffman@24465
   452
  "(to_bl :: 'a :: len0 word => bool list) =
huffman@24465
   453
    bin_to_bl (len_of TYPE('a)) o uint"
huffman@24465
   454
  by (auto simp: to_bl_def intro: ext)
huffman@24465
   455
wenzelm@25349
   456
lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of w", standard]
huffman@24465
   457
kleing@24333
   458
lemmas uints_mod = uints_def [unfolded no_bintr_alt1]
kleing@24333
   459
kleing@24333
   460
lemma uint_bintrunc: "uint (number_of bin :: 'a word) = 
huffman@24465
   461
    number_of (bintrunc (len_of TYPE ('a :: len0)) bin)"
kleing@24333
   462
  unfolding word_number_of_def number_of_eq
kleing@24333
   463
  by (auto intro: word_ubin.eq_norm) 
kleing@24333
   464
kleing@24333
   465
lemma sint_sbintrunc: "sint (number_of bin :: 'a word) = 
huffman@24465
   466
    number_of (sbintrunc (len_of TYPE ('a :: len) - 1) bin)" 
kleing@24333
   467
  unfolding word_number_of_def number_of_eq
wenzelm@25149
   468
  by (subst word_sbin.eq_norm) simp
kleing@24333
   469
kleing@24333
   470
lemma unat_bintrunc: 
huffman@24465
   471
  "unat (number_of bin :: 'a :: len0 word) =
huffman@24465
   472
    number_of (bintrunc (len_of TYPE('a)) bin)"
kleing@24333
   473
  unfolding unat_def nat_number_of_def 
kleing@24333
   474
  by (simp only: uint_bintrunc)
kleing@24333
   475
kleing@24333
   476
(* WARNING - these may not always be helpful *)
kleing@24333
   477
declare 
kleing@24333
   478
  uint_bintrunc [simp] 
kleing@24333
   479
  sint_sbintrunc [simp] 
kleing@24333
   480
  unat_bintrunc [simp]
kleing@24333
   481
huffman@24465
   482
lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 ==> v = w"
kleing@24333
   483
  apply (unfold word_size)
kleing@24333
   484
  apply (rule word_uint.Rep_eqD)
kleing@24333
   485
  apply (rule box_equals)
kleing@24333
   486
    defer
kleing@24333
   487
    apply (rule word_ubin.norm_Rep)+
kleing@24333
   488
  apply simp
kleing@24333
   489
  done
kleing@24333
   490
kleing@24333
   491
lemmas uint_lem = word_uint.Rep [unfolded uints_num mem_Collect_eq]
kleing@24333
   492
lemmas sint_lem = word_sint.Rep [unfolded sints_num mem_Collect_eq]
kleing@24333
   493
lemmas uint_ge_0 [iff] = uint_lem [THEN conjunct1, standard]
kleing@24333
   494
lemmas uint_lt2p [iff] = uint_lem [THEN conjunct2, standard]
kleing@24333
   495
lemmas sint_ge = sint_lem [THEN conjunct1, standard]
kleing@24333
   496
lemmas sint_lt = sint_lem [THEN conjunct2, standard]
kleing@24333
   497
kleing@24333
   498
lemma sign_uint_Pls [simp]: 
haftmann@25919
   499
  "bin_sign (uint x) = Int.Pls"
kleing@24333
   500
  by (simp add: sign_Pls_ge_0 number_of_eq)
kleing@24333
   501
kleing@24333
   502
lemmas uint_m2p_neg = iffD2 [OF diff_less_0_iff_less uint_lt2p, standard]
kleing@24333
   503
lemmas uint_m2p_not_non_neg = 
kleing@24333
   504
  iffD2 [OF linorder_not_le uint_m2p_neg, standard]
kleing@24333
   505
kleing@24333
   506
lemma lt2p_lem:
huffman@24465
   507
  "len_of TYPE('a) <= n ==> uint (w :: 'a :: len0 word) < 2 ^ n"
kleing@24333
   508
  by (rule xtr8 [OF _ uint_lt2p]) simp
kleing@24333
   509
kleing@24333
   510
lemmas uint_le_0_iff [simp] = 
kleing@24333
   511
  uint_ge_0 [THEN leD, THEN linorder_antisym_conv1, standard]
kleing@24333
   512
kleing@24333
   513
lemma uint_nat: "uint w == int (unat w)"
kleing@24333
   514
  unfolding unat_def by auto
kleing@24333
   515
kleing@24333
   516
lemma uint_number_of:
huffman@24465
   517
  "uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)"
kleing@24333
   518
  unfolding word_number_of_alt
kleing@24333
   519
  by (simp only: int_word_uint)
kleing@24333
   520
kleing@24333
   521
lemma unat_number_of: 
haftmann@25919
   522
  "bin_sign b = Int.Pls ==> 
huffman@24465
   523
  unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)"
kleing@24333
   524
  apply (unfold unat_def)
kleing@24333
   525
  apply (clarsimp simp only: uint_number_of)
kleing@24333
   526
  apply (rule nat_mod_distrib [THEN trans])
kleing@24333
   527
    apply (erule sign_Pls_ge_0 [THEN iffD1])
kleing@24333
   528
   apply (simp_all add: nat_power_eq)
kleing@24333
   529
  done
kleing@24333
   530
huffman@24465
   531
lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + 
huffman@24465
   532
    2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) -
huffman@24465
   533
    2 ^ (len_of TYPE('a) - 1)"
kleing@24333
   534
  unfolding word_number_of_alt by (rule int_word_sint)
kleing@24333
   535
kleing@24333
   536
lemma word_of_int_bin [simp] : 
huffman@24465
   537
  "(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)"
kleing@24333
   538
  unfolding word_number_of_alt by auto
kleing@24333
   539
kleing@24333
   540
lemma word_int_case_wi: 
kleing@24333
   541
  "word_int_case f (word_of_int i :: 'b word) = 
huffman@24465
   542
    f (i mod 2 ^ len_of TYPE('b::len0))"
kleing@24333
   543
  unfolding word_int_case_def by (simp add: word_uint.eq_norm)
kleing@24333
   544
kleing@24333
   545
lemma word_int_split: 
kleing@24333
   546
  "P (word_int_case f x) = 
huffman@24465
   547
    (ALL i. x = (word_of_int i :: 'b :: len0 word) & 
huffman@24465
   548
      0 <= i & i < 2 ^ len_of TYPE('b) --> P (f i))"
kleing@24333
   549
  unfolding word_int_case_def
kleing@24333
   550
  by (auto simp: word_uint.eq_norm int_mod_eq')
kleing@24333
   551
kleing@24333
   552
lemma word_int_split_asm: 
kleing@24333
   553
  "P (word_int_case f x) = 
huffman@24465
   554
    (~ (EX n. x = (word_of_int n :: 'b::len0 word) &
huffman@24465
   555
      0 <= n & n < 2 ^ len_of TYPE('b::len0) & ~ P (f n)))"
kleing@24333
   556
  unfolding word_int_case_def
kleing@24333
   557
  by (auto simp: word_uint.eq_norm int_mod_eq')
kleing@24333
   558
  
kleing@24333
   559
lemmas uint_range' =
kleing@24333
   560
  word_uint.Rep [unfolded uints_num mem_Collect_eq, standard]
kleing@24333
   561
lemmas sint_range' = word_sint.Rep [unfolded One_nat_def
kleing@24333
   562
  sints_num mem_Collect_eq, standard]
kleing@24333
   563
kleing@24333
   564
lemma uint_range_size: "0 <= uint w & uint w < 2 ^ size w"
kleing@24333
   565
  unfolding word_size by (rule uint_range')
kleing@24333
   566
kleing@24333
   567
lemma sint_range_size:
kleing@24333
   568
  "- (2 ^ (size w - Suc 0)) <= sint w & sint w < 2 ^ (size w - Suc 0)"
kleing@24333
   569
  unfolding word_size by (rule sint_range')
kleing@24333
   570
kleing@24333
   571
lemmas sint_above_size = sint_range_size
kleing@24333
   572
  [THEN conjunct2, THEN [2] xtr8, folded One_nat_def, standard]
kleing@24333
   573
kleing@24333
   574
lemmas sint_below_size = sint_range_size
kleing@24333
   575
  [THEN conjunct1, THEN [2] order_trans, folded One_nat_def, standard]
kleing@24333
   576
huffman@24465
   577
lemma test_bit_eq_iff: "(test_bit (u::'a::len0 word) = test_bit v) = (u = v)"
kleing@24333
   578
  unfolding word_test_bit_def by (simp add: bin_nth_eq_iff)
kleing@24333
   579
huffman@24465
   580
lemma test_bit_size [rule_format] : "(w::'a::len0 word) !! n --> n < size w"
kleing@24333
   581
  apply (unfold word_test_bit_def)
kleing@24333
   582
  apply (subst word_ubin.norm_Rep [symmetric])
kleing@24333
   583
  apply (simp only: nth_bintr word_size)
kleing@24333
   584
  apply fast
kleing@24333
   585
  done
kleing@24333
   586
kleing@24333
   587
lemma word_eqI [rule_format] : 
huffman@24465
   588
  fixes u :: "'a::len0 word"
kleing@24333
   589
  shows "(ALL n. n < size u --> u !! n = v !! n) ==> u = v"
kleing@24333
   590
  apply (rule test_bit_eq_iff [THEN iffD1])
kleing@24333
   591
  apply (rule ext)
kleing@24333
   592
  apply (erule allE)
kleing@24333
   593
  apply (erule impCE)
kleing@24333
   594
   prefer 2
kleing@24333
   595
   apply assumption
kleing@24333
   596
  apply (auto dest!: test_bit_size simp add: word_size)
kleing@24333
   597
  done
kleing@24333
   598
kleing@24333
   599
lemmas word_eqD = test_bit_eq_iff [THEN iffD2, THEN fun_cong, standard]
kleing@24333
   600
kleing@24333
   601
lemma test_bit_bin': "w !! n = (n < size w & bin_nth (uint w) n)"
kleing@24333
   602
  unfolding word_test_bit_def word_size
kleing@24333
   603
  by (simp add: nth_bintr [symmetric])
kleing@24333
   604
kleing@24333
   605
lemmas test_bit_bin = test_bit_bin' [unfolded word_size]
kleing@24333
   606
kleing@24333
   607
lemma bin_nth_uint_imp': "bin_nth (uint w) n --> n < size w"
kleing@24333
   608
  apply (unfold word_size)
kleing@24333
   609
  apply (rule impI)
kleing@24333
   610
  apply (rule nth_bintr [THEN iffD1, THEN conjunct1])
kleing@24333
   611
  apply (subst word_ubin.norm_Rep)
kleing@24333
   612
  apply assumption
kleing@24333
   613
  done
kleing@24333
   614
kleing@24333
   615
lemma bin_nth_sint': 
kleing@24333
   616
  "n >= size w --> bin_nth (sint w) n = bin_nth (sint w) (size w - 1)"
kleing@24333
   617
  apply (rule impI)
kleing@24333
   618
  apply (subst word_sbin.norm_Rep [symmetric])
kleing@24333
   619
  apply (simp add : nth_sbintr word_size)
kleing@24333
   620
  apply auto
kleing@24333
   621
  done
kleing@24333
   622
kleing@24333
   623
lemmas bin_nth_uint_imp = bin_nth_uint_imp' [rule_format, unfolded word_size]
kleing@24333
   624
lemmas bin_nth_sint = bin_nth_sint' [rule_format, unfolded word_size]
kleing@24333
   625
huffman@24465
   626
(* type definitions theorem for in terms of equivalent bool list *)
huffman@24465
   627
lemma td_bl: 
huffman@24465
   628
  "type_definition (to_bl :: 'a::len0 word => bool list) 
huffman@24465
   629
                   of_bl  
huffman@24465
   630
                   {bl. length bl = len_of TYPE('a)}"
huffman@24465
   631
  apply (unfold type_definition_def of_bl_def to_bl_def)
huffman@24465
   632
  apply (simp add: word_ubin.eq_norm)
huffman@24465
   633
  apply safe
huffman@24465
   634
  apply (drule sym)
huffman@24465
   635
  apply simp
huffman@24465
   636
  done
huffman@24465
   637
huffman@24465
   638
interpretation word_bl:
huffman@24465
   639
  type_definition ["to_bl :: 'a::len0 word => bool list"
huffman@24465
   640
                   of_bl  
huffman@24465
   641
                   "{bl. length bl = len_of TYPE('a::len0)}"]
huffman@24465
   642
  by (rule td_bl)
huffman@24465
   643
huffman@24465
   644
lemma word_size_bl: "size w == size (to_bl w)"
huffman@24465
   645
  unfolding word_size by auto
huffman@24465
   646
huffman@24465
   647
lemma to_bl_use_of_bl:
huffman@24465
   648
  "(to_bl w = bl) = (w = of_bl bl \<and> length bl = length (to_bl w))"
huffman@24465
   649
  by (fastsimp elim!: word_bl.Abs_inverse [simplified])
huffman@24465
   650
huffman@24465
   651
lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)"
huffman@24465
   652
  unfolding word_reverse_def by (simp add: word_bl.Abs_inverse)
huffman@24465
   653
huffman@24465
   654
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w"
huffman@24465
   655
  unfolding word_reverse_def by (simp add : word_bl.Abs_inverse)
huffman@24465
   656
huffman@24465
   657
lemma word_rev_gal: "word_reverse w = u ==> word_reverse u = w"
huffman@24465
   658
  by auto
huffman@24465
   659
huffman@24465
   660
lemmas word_rev_gal' = sym [THEN word_rev_gal, symmetric, standard]
huffman@24465
   661
huffman@24465
   662
lemmas length_bl_gt_0 [iff] = xtr1 [OF word_bl.Rep' len_gt_0, standard]
huffman@24465
   663
lemmas bl_not_Nil [iff] = 
huffman@24465
   664
  length_bl_gt_0 [THEN length_greater_0_conv [THEN iffD1], standard]
huffman@24465
   665
lemmas length_bl_neq_0 [iff] = length_bl_gt_0 [THEN gr_implies_not0]
huffman@24465
   666
haftmann@25919
   667
lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = Int.Min)"
huffman@24465
   668
  apply (unfold to_bl_def sint_uint)
huffman@24465
   669
  apply (rule trans [OF _ bl_sbin_sign])
huffman@24465
   670
  apply simp
huffman@24465
   671
  done
huffman@24465
   672
huffman@24465
   673
lemma of_bl_drop': 
huffman@24465
   674
  "lend = length bl - len_of TYPE ('a :: len0) ==> 
huffman@24465
   675
    of_bl (drop lend bl) = (of_bl bl :: 'a word)"
huffman@24465
   676
  apply (unfold of_bl_def)
huffman@24465
   677
  apply (clarsimp simp add : trunc_bl2bin [symmetric])
huffman@24465
   678
  done
huffman@24465
   679
huffman@24465
   680
lemmas of_bl_no = of_bl_def [folded word_number_of_def]
huffman@24465
   681
huffman@24465
   682
lemma test_bit_of_bl:  
huffman@24465
   683
  "(of_bl bl::'a::len0 word) !! n = (rev bl ! n \<and> n < len_of TYPE('a) \<and> n < length bl)"
huffman@24465
   684
  apply (unfold of_bl_def word_test_bit_def)
huffman@24465
   685
  apply (auto simp add: word_size word_ubin.eq_norm nth_bintr bin_nth_of_bl)
huffman@24465
   686
  done
huffman@24465
   687
huffman@24465
   688
lemma no_of_bl: 
huffman@24465
   689
  "(number_of bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) bin)"
huffman@24465
   690
  unfolding word_size of_bl_no by (simp add : word_number_of_def)
huffman@24465
   691
huffman@24465
   692
lemma uint_bl: "to_bl w == bin_to_bl (size w) (uint w)"
huffman@24465
   693
  unfolding word_size to_bl_def by auto
huffman@24465
   694
huffman@24465
   695
lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w"
huffman@24465
   696
  unfolding uint_bl by (simp add : word_size)
huffman@24465
   697
huffman@24465
   698
lemma to_bl_of_bin: 
huffman@24465
   699
  "to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin"
huffman@24465
   700
  unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size)
huffman@24465
   701
huffman@24465
   702
lemmas to_bl_no_bin [simp] = to_bl_of_bin [folded word_number_of_def]
huffman@24465
   703
huffman@24465
   704
lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w"
huffman@24465
   705
  unfolding uint_bl by (simp add : word_size)
huffman@24465
   706
  
huffman@24465
   707
lemmas uint_bl_bin [simp] = trans [OF bin_bl_bin word_ubin.norm_Rep, standard]
kleing@24333
   708
kleing@24333
   709
lemmas num_AB_u [simp] = word_uint.Rep_inverse 
kleing@24333
   710
  [unfolded o_def word_number_of_def [symmetric], standard]
kleing@24333
   711
lemmas num_AB_s [simp] = word_sint.Rep_inverse 
kleing@24333
   712
  [unfolded o_def word_number_of_def [symmetric], standard]
kleing@24333
   713
kleing@24333
   714
(* naturals *)
kleing@24333
   715
lemma uints_unats: "uints n = int ` unats n"
kleing@24333
   716
  apply (unfold unats_def uints_num)
kleing@24333
   717
  apply safe
kleing@24333
   718
  apply (rule_tac image_eqI)
kleing@24333
   719
  apply (erule_tac nat_0_le [symmetric])
kleing@24333
   720
  apply auto
kleing@24333
   721
  apply (erule_tac nat_less_iff [THEN iffD2])
kleing@24333
   722
  apply (rule_tac [2] zless_nat_eq_int_zless [THEN iffD1])
kleing@24333
   723
  apply (auto simp add : nat_power_eq int_power)
kleing@24333
   724
  done
kleing@24333
   725
kleing@24333
   726
lemma unats_uints: "unats n = nat ` uints n"
wenzelm@25349
   727
  by (auto simp add : uints_unats image_iff)
kleing@24333
   728
kleing@24333
   729
lemmas bintr_num = word_ubin.norm_eq_iff 
kleing@24333
   730
  [symmetric, folded word_number_of_def, standard]
kleing@24333
   731
lemmas sbintr_num = word_sbin.norm_eq_iff 
kleing@24333
   732
  [symmetric, folded word_number_of_def, standard]
kleing@24333
   733
kleing@24333
   734
lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def, standard]
kleing@24333
   735
lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def, standard];
kleing@24333
   736
    
kleing@24333
   737
(* don't add these to simpset, since may want bintrunc n w to be simplified;
kleing@24333
   738
  may want these in reverse, but loop as simp rules, so use following *)
kleing@24333
   739
kleing@24333
   740
lemma num_of_bintr':
huffman@24465
   741
  "bintrunc (len_of TYPE('a :: len0)) a = b ==> 
kleing@24333
   742
    number_of a = (number_of b :: 'a word)"
kleing@24333
   743
  apply safe
kleing@24333
   744
  apply (rule_tac num_of_bintr [symmetric])
kleing@24333
   745
  done
kleing@24333
   746
kleing@24333
   747
lemma num_of_sbintr':
huffman@24465
   748
  "sbintrunc (len_of TYPE('a :: len) - 1) a = b ==> 
kleing@24333
   749
    number_of a = (number_of b :: 'a word)"
kleing@24333
   750
  apply safe
kleing@24333
   751
  apply (rule_tac num_of_sbintr [symmetric])
kleing@24333
   752
  done
kleing@24333
   753
kleing@24333
   754
lemmas num_abs_bintr = sym [THEN trans,
haftmann@25762
   755
  OF num_of_bintr word_number_of_def, standard]
kleing@24333
   756
lemmas num_abs_sbintr = sym [THEN trans,
haftmann@25762
   757
  OF num_of_sbintr word_number_of_def, standard]
huffman@24465
   758
  
kleing@24333
   759
(** cast - note, no arg for new length, as it's determined by type of result,
kleing@24333
   760
  thus in "cast w = w, the type means cast to length of w! **)
kleing@24333
   761
kleing@24333
   762
lemma ucast_id: "ucast w = w"
kleing@24333
   763
  unfolding ucast_def by auto
kleing@24333
   764
kleing@24333
   765
lemma scast_id: "scast w = w"
kleing@24333
   766
  unfolding scast_def by auto
kleing@24333
   767
huffman@24465
   768
lemma ucast_bl: "ucast w == of_bl (to_bl w)"
huffman@24465
   769
  unfolding ucast_def of_bl_def uint_bl
huffman@24465
   770
  by (auto simp add : word_size)
huffman@24465
   771
kleing@24333
   772
lemma nth_ucast: 
huffman@24465
   773
  "(ucast w::'a::len0 word) !! n = (w !! n & n < len_of TYPE('a))"
kleing@24333
   774
  apply (unfold ucast_def test_bit_bin)
kleing@24333
   775
  apply (simp add: word_ubin.eq_norm nth_bintr word_size) 
kleing@24333
   776
  apply (fast elim!: bin_nth_uint_imp)
kleing@24333
   777
  done
kleing@24333
   778
kleing@24333
   779
(* for literal u(s)cast *)
kleing@24333
   780
kleing@24333
   781
lemma ucast_bintr [simp]: 
huffman@24465
   782
  "ucast (number_of w ::'a::len0 word) = 
huffman@24465
   783
   number_of (bintrunc (len_of TYPE('a)) w)"
kleing@24333
   784
  unfolding ucast_def by simp
kleing@24333
   785
kleing@24333
   786
lemma scast_sbintr [simp]: 
huffman@24465
   787
  "scast (number_of w ::'a::len word) = 
huffman@24465
   788
   number_of (sbintrunc (len_of TYPE('a) - Suc 0) w)"
kleing@24333
   789
  unfolding scast_def by simp
kleing@24333
   790
kleing@24333
   791
lemmas source_size = source_size_def [unfolded Let_def word_size]
kleing@24333
   792
lemmas target_size = target_size_def [unfolded Let_def word_size]
kleing@24333
   793
lemmas is_down = is_down_def [unfolded source_size target_size]
kleing@24333
   794
lemmas is_up = is_up_def [unfolded source_size target_size]
kleing@24333
   795
kleing@24333
   796
lemmas is_up_down = 
kleing@24333
   797
  trans [OF is_up [THEN meta_eq_to_obj_eq] 
kleing@24333
   798
            is_down [THEN meta_eq_to_obj_eq, symmetric], 
kleing@24333
   799
         standard]
kleing@24333
   800
kleing@24333
   801
lemma down_cast_same': "uc = ucast ==> is_down uc ==> uc = scast"
kleing@24333
   802
  apply (unfold is_down)
kleing@24333
   803
  apply safe
kleing@24333
   804
  apply (rule ext)
kleing@24333
   805
  apply (unfold ucast_def scast_def uint_sint)
kleing@24333
   806
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
kleing@24333
   807
  apply simp
kleing@24333
   808
  done
kleing@24333
   809
huffman@24465
   810
lemma word_rev_tf': 
huffman@24465
   811
  "r = to_bl (of_bl bl) ==> r = rev (takefill False (length r) (rev bl))"
huffman@24465
   812
  unfolding of_bl_def uint_bl
huffman@24465
   813
  by (clarsimp simp add: bl_bin_bl_rtf word_ubin.eq_norm word_size)
huffman@24465
   814
huffman@24465
   815
lemmas word_rev_tf = refl [THEN word_rev_tf', unfolded word_bl.Rep', standard]
huffman@24465
   816
huffman@24465
   817
lemmas word_rep_drop = word_rev_tf [simplified takefill_alt,
huffman@24465
   818
  simplified, simplified rev_take, simplified]
huffman@24465
   819
huffman@24465
   820
lemma to_bl_ucast: 
huffman@24465
   821
  "to_bl (ucast (w::'b::len0 word) ::'a::len0 word) = 
huffman@24465
   822
   replicate (len_of TYPE('a) - len_of TYPE('b)) False @
huffman@24465
   823
   drop (len_of TYPE('b) - len_of TYPE('a)) (to_bl w)"
huffman@24465
   824
  apply (unfold ucast_bl)
huffman@24465
   825
  apply (rule trans)
huffman@24465
   826
   apply (rule word_rep_drop)
huffman@24465
   827
  apply simp
huffman@24465
   828
  done
huffman@24465
   829
huffman@24465
   830
lemma ucast_up_app': 
huffman@24465
   831
  "uc = ucast ==> source_size uc + n = target_size uc ==> 
huffman@24465
   832
    to_bl (uc w) = replicate n False @ (to_bl w)"
huffman@24465
   833
  apply (auto simp add : source_size target_size to_bl_ucast)
huffman@24465
   834
  apply (rule_tac f = "%n. replicate n False" in arg_cong)
huffman@24465
   835
  apply simp
huffman@24465
   836
  done
huffman@24465
   837
huffman@24465
   838
lemma ucast_down_drop': 
huffman@24465
   839
  "uc = ucast ==> source_size uc = target_size uc + n ==> 
huffman@24465
   840
    to_bl (uc w) = drop n (to_bl w)"
huffman@24465
   841
  by (auto simp add : source_size target_size to_bl_ucast)
huffman@24465
   842
huffman@24465
   843
lemma scast_down_drop': 
huffman@24465
   844
  "sc = scast ==> source_size sc = target_size sc + n ==> 
huffman@24465
   845
    to_bl (sc w) = drop n (to_bl w)"
huffman@24465
   846
  apply (subgoal_tac "sc = ucast")
huffman@24465
   847
   apply safe
huffman@24465
   848
   apply simp
huffman@24465
   849
   apply (erule refl [THEN ucast_down_drop'])
huffman@24465
   850
  apply (rule refl [THEN down_cast_same', symmetric])
huffman@24465
   851
  apply (simp add : source_size target_size is_down)
huffman@24465
   852
  done
huffman@24465
   853
kleing@24333
   854
lemma sint_up_scast': 
kleing@24333
   855
  "sc = scast ==> is_up sc ==> sint (sc w) = sint w"
kleing@24333
   856
  apply (unfold is_up)
kleing@24333
   857
  apply safe
kleing@24333
   858
  apply (simp add: scast_def word_sbin.eq_norm)
kleing@24333
   859
  apply (rule box_equals)
kleing@24333
   860
    prefer 3
kleing@24333
   861
    apply (rule word_sbin.norm_Rep)
kleing@24333
   862
   apply (rule sbintrunc_sbintrunc_l)
kleing@24333
   863
   defer
kleing@24333
   864
   apply (subst word_sbin.norm_Rep)
kleing@24333
   865
   apply (rule refl)
kleing@24333
   866
  apply simp
kleing@24333
   867
  done
kleing@24333
   868
kleing@24333
   869
lemma uint_up_ucast':
kleing@24333
   870
  "uc = ucast ==> is_up uc ==> uint (uc w) = uint w"
kleing@24333
   871
  apply (unfold is_up)
kleing@24333
   872
  apply safe
kleing@24333
   873
  apply (rule bin_eqI)
kleing@24333
   874
  apply (fold word_test_bit_def)
kleing@24333
   875
  apply (auto simp add: nth_ucast)
kleing@24333
   876
  apply (auto simp add: test_bit_bin)
kleing@24333
   877
  done
kleing@24333
   878
    
kleing@24333
   879
lemmas down_cast_same = refl [THEN down_cast_same']
huffman@24465
   880
lemmas ucast_up_app = refl [THEN ucast_up_app']
huffman@24465
   881
lemmas ucast_down_drop = refl [THEN ucast_down_drop']
huffman@24465
   882
lemmas scast_down_drop = refl [THEN scast_down_drop']
kleing@24333
   883
lemmas uint_up_ucast = refl [THEN uint_up_ucast']
kleing@24333
   884
lemmas sint_up_scast = refl [THEN sint_up_scast']
kleing@24333
   885
kleing@24333
   886
lemma ucast_up_ucast': "uc = ucast ==> is_up uc ==> ucast (uc w) = ucast w"
kleing@24333
   887
  apply (simp (no_asm) add: ucast_def)
kleing@24333
   888
  apply (clarsimp simp add: uint_up_ucast)
kleing@24333
   889
  done
kleing@24333
   890
    
kleing@24333
   891
lemma scast_up_scast': "sc = scast ==> is_up sc ==> scast (sc w) = scast w"
kleing@24333
   892
  apply (simp (no_asm) add: scast_def)
kleing@24333
   893
  apply (clarsimp simp add: sint_up_scast)
kleing@24333
   894
  done
kleing@24333
   895
    
huffman@24465
   896
lemma ucast_of_bl_up': 
huffman@24465
   897
  "w = of_bl bl ==> size bl <= size w ==> ucast w = of_bl bl"
huffman@24465
   898
  by (auto simp add : nth_ucast word_size test_bit_of_bl intro!: word_eqI)
huffman@24465
   899
kleing@24333
   900
lemmas ucast_up_ucast = refl [THEN ucast_up_ucast']
kleing@24333
   901
lemmas scast_up_scast = refl [THEN scast_up_scast']
huffman@24465
   902
lemmas ucast_of_bl_up = refl [THEN ucast_of_bl_up']
kleing@24333
   903
kleing@24333
   904
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id]
kleing@24333
   905
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id]
kleing@24333
   906
kleing@24333
   907
lemmas isduu = is_up_down [where c = "ucast", THEN iffD2]
kleing@24333
   908
lemmas isdus = is_up_down [where c = "scast", THEN iffD2]
kleing@24333
   909
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id]
kleing@24333
   910
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id]
kleing@24333
   911
kleing@24333
   912
lemma up_ucast_surj:
huffman@24465
   913
  "is_up (ucast :: 'b::len0 word => 'a::len0 word) ==> 
kleing@24333
   914
   surj (ucast :: 'a word => 'b word)"
kleing@24333
   915
  by (rule surjI, erule ucast_up_ucast_id)
kleing@24333
   916
kleing@24333
   917
lemma up_scast_surj:
huffman@24465
   918
  "is_up (scast :: 'b::len word => 'a::len word) ==> 
kleing@24333
   919
   surj (scast :: 'a word => 'b word)"
kleing@24333
   920
  by (rule surjI, erule scast_up_scast_id)
kleing@24333
   921
kleing@24333
   922
lemma down_scast_inj:
huffman@24465
   923
  "is_down (scast :: 'b::len word => 'a::len word) ==> 
kleing@24333
   924
   inj_on (ucast :: 'a word => 'b word) A"
kleing@24333
   925
  by (rule inj_on_inverseI, erule scast_down_scast_id)
kleing@24333
   926
kleing@24333
   927
lemma down_ucast_inj:
huffman@24465
   928
  "is_down (ucast :: 'b::len0 word => 'a::len0 word) ==> 
kleing@24333
   929
   inj_on (ucast :: 'a word => 'b word) A"
kleing@24333
   930
  by (rule inj_on_inverseI, erule ucast_down_ucast_id)
kleing@24333
   931
huffman@24465
   932
lemma of_bl_append_same: "of_bl (X @ to_bl w) = w"
huffman@24465
   933
  by (rule word_bl.Rep_eqD) (simp add: word_rep_drop)
kleing@24333
   934
  
kleing@24333
   935
lemma ucast_down_no': 
kleing@24333
   936
  "uc = ucast ==> is_down uc ==> uc (number_of bin) = number_of bin"
kleing@24333
   937
  apply (unfold word_number_of_def is_down)
kleing@24333
   938
  apply (clarsimp simp add: ucast_def word_ubin.eq_norm)
kleing@24333
   939
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
kleing@24333
   940
  apply (erule bintrunc_bintrunc_ge)
kleing@24333
   941
  done
kleing@24333
   942
    
kleing@24333
   943
lemmas ucast_down_no = ucast_down_no' [OF refl]
kleing@24333
   944
huffman@24465
   945
lemma ucast_down_bl': "uc = ucast ==> is_down uc ==> uc (of_bl bl) = of_bl bl"
huffman@24465
   946
  unfolding of_bl_no by clarify (erule ucast_down_no)
huffman@24465
   947
    
huffman@24465
   948
lemmas ucast_down_bl = ucast_down_bl' [OF refl]
huffman@24465
   949
huffman@24465
   950
lemmas slice_def' = slice_def [unfolded word_size]
haftmann@26559
   951
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong]
huffman@24465
   952
huffman@24465
   953
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def
huffman@24465
   954
lemmas word_log_bin_defs = word_log_defs
huffman@24465
   955
kleing@24333
   956
end