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