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