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