src/HOL/Word/WordBitwise.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
  contains theorems to do with bit-wise (logical) operations on words
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     6
*)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     7
theory WordBitwise imports WordArith begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     8
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     9
lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    10
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    11
(* following definitions require both arithmetic and bit-wise word operations *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    12
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    13
(* to get word_no_log_defs from word_log_defs, using bin_log_bintrs *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    14
lemmas wils1 = bin_log_bintrs [THEN word_ubin.norm_eq_iff [THEN iffD1],
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    15
  folded word_ubin.eq_norm, THEN eq_reflection, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    16
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    17
(* the binary operations only *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    18
lemmas word_log_binary_defs = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    19
  word_and_def word_or_def word_xor_def
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    20
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    21
lemmas word_no_log_defs [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    22
  word_not_def  [where a="number_of ?a", 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    23
                 unfolded word_no_wi wils1, folded word_no_wi]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    24
  word_log_binary_defs [where a="number_of ?a" and b="number_of ?b",
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    25
                        unfolded word_no_wi wils1, folded word_no_wi]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    26
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    27
lemmas word_wi_log_defs = word_no_log_defs [unfolded word_no_wi]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    28
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    29
lemma uint_or: "uint (x OR y) = int_or (uint x) (uint y)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    30
  by (simp add: word_or_def word_no_wi [symmetric] int_number_of
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    31
                bin_trunc_ao(2) [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    32
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    33
lemma uint_and: "uint (x AND y) = int_and (uint x) (uint y)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    34
  by (simp add: word_and_def int_number_of word_no_wi [symmetric]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    35
                bin_trunc_ao(1) [symmetric]) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    36
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    37
lemma word_ops_nth_size:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    38
  "n < size (x::'a::len0 word) ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    39
    (x OR y) !! n = (x !! n | y !! n) & 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    40
    (x AND y) !! n = (x !! n & y !! n) & 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    41
    (x XOR y) !! n = (x !! n ~= y !! n) & 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    42
    (NOT x) !! n = (~ x !! n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    43
  unfolding word_size word_no_wi word_test_bit_def word_log_defs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    44
  by (clarsimp simp add : word_ubin.eq_norm nth_bintr bin_nth_ops)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    45
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    46
lemma word_ao_nth:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    47
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    48
  shows "(x OR y) !! n = (x !! n | y !! n) & 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    49
         (x AND y) !! n = (x !! n & y !! n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    50
  apply (cases "n < size x")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    51
   apply (drule_tac y = "y" in word_ops_nth_size)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    52
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    53
  apply (simp add : test_bit_bin word_size)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    54
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    55
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    56
(* get from commutativity, associativity etc of int_and etc
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    57
  to same for word_and etc *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    58
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    59
lemmas bwsimps = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    60
  word_of_int_homs(2) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    61
  word_0_wi_Pls
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    62
  word_m1_wi_Min
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    63
  word_wi_log_defs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    64
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    65
lemma word_bw_assocs:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    66
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    67
  shows
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    68
  "(x AND y) AND z = x AND y AND z"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    69
  "(x OR y) OR z = x OR y OR z"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    70
  "(x XOR y) XOR z = x XOR y XOR z"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    71
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    72
        word_of_int_Ex [where x=y] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    73
        word_of_int_Ex [where x=z]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    74
  by (auto simp: bwsimps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    75
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    76
lemma word_bw_comms:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    77
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    78
  shows
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    79
  "x AND y = y AND x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    80
  "x OR y = y OR x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    81
  "x XOR y = y XOR x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    82
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    83
        word_of_int_Ex [where x=y] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    84
  by (auto simp: bwsimps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    85
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    86
lemma word_bw_lcs:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    87
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    88
  shows
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    89
  "y AND x AND z = x AND y AND z"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    90
  "y OR x OR z = x OR y OR z"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    91
  "y XOR x XOR z = x XOR y XOR z"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    92
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    93
        word_of_int_Ex [where x=y] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    94
        word_of_int_Ex [where x=z]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    95
  by (auto simp: bwsimps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    96
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    97
lemma word_log_esimps [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    98
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    99
  shows
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   100
  "x AND 0 = 0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   101
  "x AND -1 = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   102
  "x OR 0 = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   103
  "x OR -1 = -1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   104
  "x XOR 0 = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   105
  "x XOR -1 = NOT x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   106
  "0 AND x = 0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   107
  "-1 AND x = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   108
  "0 OR x = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   109
  "-1 OR x = -1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   110
  "0 XOR x = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   111
  "-1 XOR x = NOT x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   112
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   113
  by (auto simp: bwsimps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   114
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   115
lemma word_not_dist:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   116
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   117
  shows
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   118
  "NOT (x OR y) = NOT x AND NOT y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   119
  "NOT (x AND y) = NOT x OR NOT y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   120
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   121
        word_of_int_Ex [where x=y] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   122
  by (auto simp: bwsimps bbw_not_dist)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   123
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   124
lemma word_bw_same:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   125
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   126
  shows
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   127
  "x AND x = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   128
  "x OR x = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   129
  "x XOR x = 0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   130
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   131
  by (auto simp: bwsimps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   132
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   133
lemma word_ao_absorbs [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   134
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   135
  shows
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   136
  "x AND (y OR x) = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   137
  "x OR y AND x = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   138
  "x AND (x OR y) = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   139
  "y AND x OR x = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   140
  "(y OR x) AND x = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   141
  "x OR x AND y = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   142
  "(x OR y) AND x = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   143
  "x AND y OR x = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   144
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   145
        word_of_int_Ex [where x=y] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   146
  by (auto simp: bwsimps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   147
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   148
lemma word_not_not [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   149
  "NOT NOT (x::'a::len0 word) = x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   150
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   151
  by (auto simp: bwsimps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   152
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   153
lemma word_ao_dist:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   154
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   155
  shows "(x OR y) AND z = x AND z OR y AND z"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   156
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   157
        word_of_int_Ex [where x=y] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   158
        word_of_int_Ex [where x=z]   
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   159
  by (auto simp: bwsimps bbw_ao_dist simp del: bin_ops_comm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   160
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   161
lemma word_oa_dist:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   162
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   163
  shows "x AND y OR z = (x OR z) AND (y OR z)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   164
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   165
        word_of_int_Ex [where x=y] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   166
        word_of_int_Ex [where x=z]   
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   167
  by (auto simp: bwsimps bbw_oa_dist simp del: bin_ops_comm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   168
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   169
lemma word_add_not [simp]: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   170
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   171
  shows "x + NOT x = -1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   172
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   173
  by (auto simp: bwsimps bin_add_not)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   174
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   175
lemma word_plus_and_or [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   176
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   177
  shows "(x AND y) + (x OR y) = x + y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   178
  using word_of_int_Ex [where x=x] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   179
        word_of_int_Ex [where x=y] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   180
  by (auto simp: bwsimps plus_and_or)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   181
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   182
lemma leoa:   
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   183
  fixes x :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   184
  shows "(w = (x OR y)) ==> (y = (w AND y))" by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   185
lemma leao: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   186
  fixes x' :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   187
  shows "(w' = (x' AND y')) ==> (x' = (x' OR w'))" by auto 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   188
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   189
lemmas word_ao_equiv = leao [COMP leoa [COMP iffI]]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   190
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   191
lemma le_word_or2: "x <= x OR (y::'a::len0 word)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   192
  unfolding word_le_def uint_or
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   193
  by (auto intro: le_int_or) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   194
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   195
lemmas le_word_or1 = xtr3 [OF word_bw_comms (2) le_word_or2, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   196
lemmas word_and_le1 =
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   197
  xtr3 [OF word_ao_absorbs (4) [symmetric] le_word_or2, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   198
lemmas word_and_le2 =
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   199
  xtr3 [OF word_ao_absorbs (8) [symmetric] le_word_or2, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   200
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   201
lemma bl_word_not: "to_bl (NOT w) = map Not (to_bl w)" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   202
  unfolding to_bl_def word_log_defs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   203
  by (simp add: bl_not_bin int_number_of word_no_wi [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   204
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   205
lemma bl_word_xor: "to_bl (v XOR w) = app2 op ~= (to_bl v) (to_bl w)" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   206
  unfolding to_bl_def word_log_defs bl_xor_bin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   207
  by (simp add: int_number_of word_no_wi [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   208
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   209
lemma bl_word_or: "to_bl (v OR w) = app2 op | (to_bl v) (to_bl w)" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   210
  unfolding to_bl_def word_log_defs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   211
  by (simp add: bl_or_bin int_number_of word_no_wi [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   212
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   213
lemma bl_word_and: "to_bl (v AND w) = app2 op & (to_bl v) (to_bl w)" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   214
  unfolding to_bl_def word_log_defs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   215
  by (simp add: bl_and_bin int_number_of word_no_wi [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   216
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   217
lemma word_lsb_alt: "lsb (w::'a::len0 word) = test_bit w 0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   218
  by (auto simp: word_test_bit_def word_lsb_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   219
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   220
lemma word_lsb_1_0: "lsb (1::'a::len word) & ~ lsb (0::'b::len0 word)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   221
  unfolding word_lsb_def word_1_no word_0_no by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   222
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   223
lemma word_lsb_last: "lsb (w::'a::len word) = last (to_bl w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   224
  apply (unfold word_lsb_def uint_bl bin_to_bl_def) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   225
  apply (rule_tac bin="uint w" in bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   226
  apply (cases "size w")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   227
   apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   228
   apply (auto simp add: bin_to_bl_aux_alt)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   229
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   230
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   231
lemma word_lsb_int: "lsb w = (uint w mod 2 = 1)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   232
  unfolding word_lsb_def bin_last_mod by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   233
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   234
lemma word_msb_sint: "msb w = (sint w < 0)" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   235
  unfolding word_msb_def
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   236
  by (simp add : sign_Min_lt_0 int_number_of)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   237
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   238
lemma word_msb_no': 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   239
  "w = number_of bin ==> msb (w::'a::len word) = bin_nth bin (size w - 1)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   240
  unfolding word_msb_def word_number_of_def
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   241
  by (clarsimp simp add: word_sbin.eq_norm word_size bin_sign_lem)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   242
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   243
lemmas word_msb_no = refl [THEN word_msb_no', unfolded word_size]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   244
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   245
lemma word_msb_nth': "msb (w::'a::len word) = bin_nth (uint w) (size w - 1)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   246
  apply (unfold word_size)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   247
  apply (rule trans [OF _ word_msb_no])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   248
  apply (simp add : word_number_of_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   249
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   250
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   251
lemmas word_msb_nth = word_msb_nth' [unfolded word_size]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   252
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   253
lemma word_msb_alt: "msb (w::'a::len word) = hd (to_bl w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   254
  apply (unfold word_msb_nth uint_bl)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   255
  apply (subst hd_conv_nth)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   256
  apply (rule length_greater_0_conv [THEN iffD1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   257
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   258
  apply (simp add : nth_bin_to_bl word_size)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   259
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   260
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   261
lemma word_set_nth:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   262
  "set_bit w n (test_bit w n) = (w::'a::len0 word)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   263
  unfolding word_test_bit_def word_set_bit_def by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   264
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   265
lemma bin_nth_uint':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   266
  "bin_nth (uint w) n = (rev (bin_to_bl (size w) (uint w)) ! n & n < size w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   267
  apply (unfold word_size)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   268
  apply (safe elim!: bin_nth_uint_imp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   269
   apply (frule bin_nth_uint_imp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   270
   apply (fast dest!: bin_nth_bl)+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   271
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   272
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   273
lemmas bin_nth_uint = bin_nth_uint' [unfolded word_size]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   274
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   275
lemma test_bit_bl: "w !! n = (rev (to_bl w) ! n & n < size w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   276
  unfolding to_bl_def word_test_bit_def word_size
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   277
  by (rule bin_nth_uint)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   278
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   279
lemma to_bl_nth: "n < size w ==> to_bl w ! n = w !! (size w - Suc n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   280
  apply (unfold test_bit_bl)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   281
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   282
  apply (rule trans)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   283
   apply (rule nth_rev_alt)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   284
   apply (auto simp add: word_size)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   285
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   286
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   287
lemma test_bit_set: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   288
  fixes w :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   289
  shows "(set_bit w n x) !! n = (n < size w & x)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   290
  unfolding word_size word_test_bit_def word_set_bit_def
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   291
  by (clarsimp simp add : word_ubin.eq_norm nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   292
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   293
lemma test_bit_set_gen: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   294
  fixes w :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   295
  shows "test_bit (set_bit w n x) m = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   296
         (if m = n then n < size w & x else test_bit w m)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   297
  apply (unfold word_size word_test_bit_def word_set_bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   298
  apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   299
  apply (auto elim!: test_bit_size [unfolded word_size]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   300
              simp add: word_test_bit_def [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   301
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   302
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   303
lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   304
  unfolding of_bl_def bl_to_bin_rep_F by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   305
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   306
lemma msb_nth':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   307
  fixes w :: "'a::len word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   308
  shows "msb w = w !! (size w - 1)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   309
  unfolding word_msb_nth' word_test_bit_def by simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   310
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   311
lemmas msb_nth = msb_nth' [unfolded word_size]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   312
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   313
lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   314
  word_ops_nth_size [unfolded word_size], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   315
lemmas msb1 = msb0 [where i = 0]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   316
lemmas word_ops_msb = msb1 [unfolded msb_nth [symmetric, unfolded One_nat_def]]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   317
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   318
lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   319
lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   320
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   321
lemma td_ext_nth':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   322
  "n = size (w::'a::len0 word) ==> ofn = set_bits ==> [w, ofn g] = l ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   323
    td_ext test_bit ofn {f. ALL i. f i --> i < n} (%h i. h i & i < n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   324
  apply (unfold word_size td_ext_def')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   325
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   326
     apply (rule_tac [3] ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   327
     apply (rule_tac [4] ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   328
     apply (unfold word_size of_nth_def test_bit_bl)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   329
     apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   330
       defer
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   331
       apply (clarsimp simp: word_bl.Abs_inverse)+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   332
  apply (rule word_bl.Rep_inverse')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   333
  apply (rule sym [THEN trans])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   334
  apply (rule bl_of_nth_nth)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   335
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   336
  apply (rule bl_of_nth_inj)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   337
  apply (clarsimp simp add : test_bit_bl word_size)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   338
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   339
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   340
lemmas td_ext_nth = td_ext_nth' [OF refl refl refl, unfolded word_size]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   341
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   342
interpretation test_bit:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   343
  td_ext ["op !! :: 'a::len0 word => nat => bool"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   344
          set_bits
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   345
          "{f. \<forall>i. f i \<longrightarrow> i < len_of TYPE('a::len0)}"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   346
          "(\<lambda>h i. h i \<and> i < len_of TYPE('a::len0))"]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   347
  by (rule td_ext_nth)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   348
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   349
declare test_bit.Rep' [simp del]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   350
declare test_bit.Rep' [rule del]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   351
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   352
lemmas td_nth = test_bit.td_thm
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   353
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   354
lemma word_set_set_same: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   355
  fixes w :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   356
  shows "set_bit (set_bit w n x) n y = set_bit w n y" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   357
  by (rule word_eqI) (simp add : test_bit_set_gen word_size)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   358
    
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   359
lemma word_set_set_diff: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   360
  fixes w :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   361
  assumes "m ~= n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   362
  shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   363
  by (rule word_eqI) (clarsimp simp add : test_bit_set_gen word_size prems)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   364
    
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   365
lemma test_bit_no': 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   366
  fixes w :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   367
  shows "w = number_of bin ==> test_bit w n = (n < size w & bin_nth bin n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   368
  unfolding word_test_bit_def word_number_of_def word_size
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   369
  by (simp add : nth_bintr [symmetric] word_ubin.eq_norm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   370
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   371
lemmas test_bit_no = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   372
  refl [THEN test_bit_no', unfolded word_size, THEN eq_reflection, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   373
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   374
lemma nth_0: "~ (0::'a::len0 word) !! n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   375
  unfolding test_bit_no word_0_no by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   376
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   377
lemma nth_sint: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   378
  fixes w :: "'a::len word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   379
  defines "l \<equiv> len_of TYPE ('a)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   380
  shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   381
  unfolding sint_uint l_def
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   382
  by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   383
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   384
lemma word_lsb_no: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   385
  "lsb (number_of bin :: 'a :: len word) = (bin_last bin = bit.B1)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   386
  unfolding word_lsb_alt test_bit_no by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   387
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   388
lemma word_set_no: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   389
  "set_bit (number_of bin::'a::len0 word) n b = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   390
    number_of (bin_sc n (if b then bit.B1 else bit.B0) bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   391
  apply (unfold word_set_bit_def word_number_of_def [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   392
  apply (rule word_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   393
  apply (clarsimp simp: word_size bin_nth_sc_gen int_number_of 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   394
                        test_bit_no nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   395
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   396
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   397
lemmas setBit_no = setBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   398
  simplified if_simps, THEN eq_reflection, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   399
lemmas clearBit_no = clearBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   400
  simplified if_simps, THEN eq_reflection, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   401
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   402
lemma to_bl_n1: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   403
  "to_bl (-1::'a::len0 word) = replicate (len_of TYPE ('a)) True"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   404
  apply (rule word_bl.Abs_inverse')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   405
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   406
  apply (rule word_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   407
  apply (clarsimp simp add: word_size test_bit_no)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   408
  apply (auto simp add: word_bl.Abs_inverse test_bit_bl word_size)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   409
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   410
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   411
lemma word_msb_n1: "msb (-1::'a::len word)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   412
  unfolding word_msb_alt word_msb_alt to_bl_n1 by simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   413
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   414
declare word_set_set_same [simp] word_set_nth [simp]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   415
  test_bit_no [simp] word_set_no [simp] nth_0 [simp]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   416
  setBit_no [simp] clearBit_no [simp]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   417
  word_lsb_no [simp] word_msb_no [simp] word_msb_n1 [simp] word_lsb_1_0 [simp]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   418
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   419
lemma word_set_nth_iff: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   420
  "(set_bit w n b = w) = (w !! n = b | n >= size (w::'a::len0 word))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   421
  apply (rule iffI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   422
   apply (rule disjCI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   423
   apply (drule word_eqD)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   424
   apply (erule sym [THEN trans])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   425
   apply (simp add: test_bit_set)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   426
  apply (erule disjE)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   427
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   428
  apply (rule word_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   429
  apply (clarsimp simp add : test_bit_set_gen)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   430
  apply (drule test_bit_size)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   431
  apply force
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   432
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   433
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   434
lemma test_bit_2p': 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   435
  "w = word_of_int (2 ^ n) ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   436
    w !! m = (m = n & m < size (w :: 'a :: len word))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   437
  unfolding word_test_bit_def word_size
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   438
  by (auto simp add: word_ubin.eq_norm nth_bintr nth_2p_bin)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   439
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   440
lemmas test_bit_2p = refl [THEN test_bit_2p', unfolded word_size]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   441
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   442
lemmas nth_w2p = test_bit_2p [unfolded of_int_number_of_eq
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   443
  word_of_int [symmetric] of_int_power]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   444
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   445
lemma uint_2p: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   446
  "(0::'a::len word) < 2 ^ n ==> uint (2 ^ n::'a::len word) = 2 ^ n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   447
  apply (unfold word_arith_power_alt)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   448
  apply (case_tac "len_of TYPE ('a)")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   449
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   450
  apply (case_tac "nat")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   451
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   452
   apply (case_tac "n")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   453
    apply (clarsimp simp add : word_1_wi [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   454
   apply (clarsimp simp add : word_0_wi [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   455
  apply (drule word_gt_0 [THEN iffD1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   456
  apply (safe intro!: word_eqI bin_nth_lem ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   457
     apply (auto simp add: test_bit_2p nth_2p_bin word_test_bit_def [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   458
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   459
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   460
lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a :: len word) = 2 ^ n" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   461
  apply (unfold word_arith_power_alt)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   462
  apply (case_tac "len_of TYPE ('a)")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   463
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   464
  apply (case_tac "nat")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   465
   apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   466
   apply (rule box_equals) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   467
     apply (rule_tac [2] bintr_ariths (1))+ 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   468
   apply (clarsimp simp add : int_number_of)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   469
  apply simp 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   470
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   471
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   472
lemma bang_is_le: "x !! m ==> 2 ^ m <= (x :: 'a :: len word)" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   473
  apply (rule xtr3) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   474
  apply (rule_tac [2] y = "x" in le_word_or2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   475
  apply (rule word_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   476
  apply (auto simp add: word_ao_nth nth_w2p word_size)
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 word_clr_le: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   480
  fixes w :: "'a::len0 word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   481
  shows "w >= set_bit w n False"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   482
  apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   483
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   484
  apply (rule order_trans)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   485
   apply (rule bintr_bin_clr_le)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   486
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   487
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   488
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   489
lemma word_set_ge: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   490
  fixes w :: "'a::len word"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   491
  shows "w <= set_bit w n True"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   492
  apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   493
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   494
  apply (rule order_trans [OF _ bintr_bin_set_ge])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   495
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   496
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   497
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   498
end
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   499