src/HOL/Word/Word.thy
author blanchet
Tue, 05 Oct 2010 11:45:10 +0200
changeset 39953 aa54f347e5e2
parent 39910 10097e0a9dbd
child 40827 abbc05c20e24
permissions -rw-r--r--
hide uninteresting MESON/Metis constants and facts and remove "meson_" prefix to (now hidden) fact names
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29628
d9294387ab0e entry point for Word library now named Word
haftmann
parents: 27137
diff changeset
     1
(*  Title:      HOL/Word/Word.thy
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
     2
    Author: Jeremy Dawson and Gerwin Klein, NICTA
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
*)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
     5
header {* A type of finite bit strings *}
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
     6
29628
d9294387ab0e entry point for Word library now named Word
haftmann
parents: 27137
diff changeset
     7
theory Word
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
     8
imports Type_Length Misc_Typedef Boolean_Algebra Bool_List_Representation
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
     9
uses ("~~/src/HOL/Tools/SMT/smt_word.ML")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    10
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    11
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    12
text {* see @{text "Examples/WordExamples.thy"} for examples *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    13
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    14
subsection {* Type definition *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    15
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    16
typedef (open word) 'a word = "{(0::int) ..< 2^len_of TYPE('a::len0)}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    17
  morphisms uint Abs_word by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    18
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    19
definition word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    20
  -- {* representation of words using unsigned or signed bins, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    21
        only difference in these is the type class *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    22
  "word_of_int w = Abs_word (bintrunc (len_of TYPE ('a)) w)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    23
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    24
lemma uint_word_of_int [code]: "uint (word_of_int w \<Colon> 'a\<Colon>len0 word) = w mod 2 ^ len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    25
  by (auto simp add: word_of_int_def bintrunc_mod2p intro: Abs_word_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    26
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    27
code_datatype word_of_int
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    28
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    29
notation fcomp (infixl "\<circ>>" 60)
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    30
notation scomp (infixl "\<circ>\<rightarrow>" 60)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    31
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    32
instantiation word :: ("{len0, typerep}") random
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    33
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    34
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    35
definition
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    36
  "random_word i = Random.range (max i (2 ^ len_of TYPE('a))) \<circ>\<rightarrow> (\<lambda>k. Pair (
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    37
     let j = word_of_int (Code_Numeral.int_of k) :: 'a word
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    38
     in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    39
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    40
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    41
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    42
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    43
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    44
no_notation fcomp (infixl "\<circ>>" 60)
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    45
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    46
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    47
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    48
subsection {* Type conversions and casting *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    49
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    50
definition sint :: "'a :: len word => int" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    51
  -- {* treats the most-significant-bit as a sign bit *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    52
  sint_uint: "sint w = sbintrunc (len_of TYPE ('a) - 1) (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    53
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    54
definition unat :: "'a :: len0 word => nat" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    55
  "unat w = nat (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    56
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    57
definition uints :: "nat => int set" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    58
  -- "the sets of integers representing the words"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    59
  "uints n = range (bintrunc n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    60
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    61
definition sints :: "nat => int set" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    62
  "sints n = range (sbintrunc (n - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    63
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    64
definition unats :: "nat => nat set" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    65
  "unats n = {i. i < 2 ^ n}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    66
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    67
definition norm_sint :: "nat => int => int" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    68
  "norm_sint n w = (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    69
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    70
definition scast :: "'a :: len word => 'b :: len word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    71
  -- "cast a word to a different length"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    72
  "scast w = word_of_int (sint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    73
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    74
definition ucast :: "'a :: len0 word => 'b :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    75
  "ucast w = word_of_int (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    76
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    77
instantiation word :: (len0) size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    78
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    79
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    80
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    81
  word_size: "size (w :: 'a word) = len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    82
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    83
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    84
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    85
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    86
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    87
definition source_size :: "('a :: len0 word => 'b) => nat" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    88
  -- "whether a cast (or other) function is to a longer or shorter length"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    89
  "source_size c = (let arb = undefined ; x = c arb in size arb)"  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    90
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    91
definition target_size :: "('a => 'b :: len0 word) => nat" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    92
  "target_size c = size (c undefined)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    93
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    94
definition is_up :: "('a :: len0 word => 'b :: len0 word) => bool" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    95
  "is_up c \<longleftrightarrow> source_size c <= target_size c"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    96
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    97
definition is_down :: "('a :: len0 word => 'b :: len0 word) => bool" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    98
  "is_down c \<longleftrightarrow> target_size c <= source_size c"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    99
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   100
definition of_bl :: "bool list => 'a :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   101
  "of_bl bl = word_of_int (bl_to_bin bl)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   102
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   103
definition to_bl :: "'a :: len0 word => bool list" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   104
  "to_bl w = bin_to_bl (len_of TYPE ('a)) (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   105
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   106
definition word_reverse :: "'a :: len0 word => 'a word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   107
  "word_reverse w = of_bl (rev (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   108
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   109
definition word_int_case :: "(int => 'b) => ('a :: len0 word) => 'b" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   110
  "word_int_case f w = f (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   111
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   112
syntax
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   113
  of_int :: "int => 'a"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   114
translations
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   115
  "case x of CONST of_int y => b" == "CONST word_int_case (%y. b) x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   116
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   117
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   118
subsection  "Arithmetic operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   119
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   120
instantiation word :: (len0) "{number, uminus, minus, plus, one, zero, times, Divides.div, ord, bit}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   121
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   122
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   123
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   124
  word_0_wi: "0 = word_of_int 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   125
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   126
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   127
  word_1_wi: "1 = word_of_int 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   128
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   129
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   130
  word_add_def: "a + b = word_of_int (uint a + uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   131
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   132
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   133
  word_sub_wi: "a - b = word_of_int (uint a - uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   134
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   135
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   136
  word_minus_def: "- a = word_of_int (- uint a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   137
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   138
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   139
  word_mult_def: "a * b = word_of_int (uint a * uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   140
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   141
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   142
  word_div_def: "a div b = word_of_int (uint a div uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   143
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   144
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   145
  word_mod_def: "a mod b = word_of_int (uint a mod uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   146
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   147
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   148
  word_number_of_def: "number_of w = word_of_int w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   149
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   150
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   151
  word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   152
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   153
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   154
  word_less_def: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> (y \<Colon> 'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   155
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   156
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   157
  word_and_def: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   158
  "(a::'a word) AND b = word_of_int (uint a AND uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   159
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   160
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   161
  word_or_def:  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   162
  "(a::'a word) OR b = word_of_int (uint a OR uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   163
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   164
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   165
  word_xor_def: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   166
  "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   167
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   168
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   169
  word_not_def: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   170
  "NOT (a::'a word) = word_of_int (NOT (uint a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   171
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   172
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   173
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   174
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   175
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   176
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   177
  word_succ :: "'a :: len0 word => 'a word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   178
where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   179
  "word_succ a = word_of_int (Int.succ (uint a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   180
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   181
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   182
  word_pred :: "'a :: len0 word => 'a word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   183
where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   184
  "word_pred a = word_of_int (Int.pred (uint a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   185
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   186
definition udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50) where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   187
  "a udvd b == EX n>=0. uint b = n * uint a"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   188
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   189
definition word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50) where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   190
  "a <=s b == sint a <= sint b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   191
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   192
definition word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50) where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   193
  "(x <s y) == (x <=s y & x ~= y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   194
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   195
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   196
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   197
subsection "Bit-wise operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   198
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   199
instantiation word :: (len0) bits
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   200
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   201
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   202
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   203
  word_test_bit_def: "test_bit a = bin_nth (uint a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   204
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   205
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   206
  word_set_bit_def: "set_bit a n x =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   207
   word_of_int (bin_sc n (If x 1 0) (uint a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   208
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   209
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   210
  word_set_bits_def: "(BITS n. f n) = of_bl (bl_of_nth (len_of TYPE ('a)) f)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   211
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   212
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   213
  word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a) = 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   214
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   215
definition shiftl1 :: "'a word \<Rightarrow> 'a word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   216
  "shiftl1 w = word_of_int (uint w BIT 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   217
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   218
definition shiftr1 :: "'a word \<Rightarrow> 'a word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   219
  -- "shift right as unsigned or as signed, ie logical or arithmetic"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   220
  "shiftr1 w = word_of_int (bin_rest (uint w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   221
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   222
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   223
  shiftl_def: "w << n = (shiftl1 ^^ n) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   224
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   225
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   226
  shiftr_def: "w >> n = (shiftr1 ^^ n) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   227
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   228
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   229
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   230
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   231
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   232
instantiation word :: (len) bitss
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   233
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   234
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   235
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   236
  word_msb_def: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   237
  "msb a \<longleftrightarrow> bin_sign (sint a) = Int.Min"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   238
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   239
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   240
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   241
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   242
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37660
diff changeset
   243
lemma [code]:
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37660
diff changeset
   244
  "msb a \<longleftrightarrow> bin_sign (sint a) = (- 1 :: int)"
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37660
diff changeset
   245
  by (simp only: word_msb_def Min_def)
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37660
diff changeset
   246
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   247
definition setBit :: "'a :: len0 word => nat => 'a word" where 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   248
  "setBit w n == set_bit w n True"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   249
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   250
definition clearBit :: "'a :: len0 word => nat => 'a word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   251
  "clearBit w n == set_bit w n False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   252
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   253
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   254
subsection "Shift operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   255
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   256
definition sshiftr1 :: "'a :: len word => 'a word" where 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   257
  "sshiftr1 w == word_of_int (bin_rest (sint w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   258
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   259
definition bshiftr1 :: "bool => 'a :: len word => 'a word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   260
  "bshiftr1 b w == of_bl (b # butlast (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   261
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   262
definition sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55) where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   263
  "w >>> n == (sshiftr1 ^^ n) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   264
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   265
definition mask :: "nat => 'a::len word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   266
  "mask n == (1 << n) - 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   267
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   268
definition revcast :: "'a :: len0 word => 'b :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   269
  "revcast w ==  of_bl (takefill False (len_of TYPE('b)) (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   270
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   271
definition slice1 :: "nat => 'a :: len0 word => 'b :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   272
  "slice1 n w == of_bl (takefill False n (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   273
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   274
definition slice :: "nat => 'a :: len0 word => 'b :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   275
  "slice n w == slice1 (size w - n) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   276
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   277
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   278
subsection "Rotation"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   279
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   280
definition rotater1 :: "'a list => 'a list" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   281
  "rotater1 ys == 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   282
    case ys of [] => [] | x # xs => last ys # butlast ys"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   283
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   284
definition rotater :: "nat => 'a list => 'a list" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   285
  "rotater n == rotater1 ^^ n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   286
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   287
definition word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   288
  "word_rotr n w == of_bl (rotater n (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   289
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   290
definition word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   291
  "word_rotl n w == of_bl (rotate n (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   292
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   293
definition word_roti :: "int => 'a :: len0 word => 'a :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   294
  "word_roti i w == if i >= 0 then word_rotr (nat i) w
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   295
                    else word_rotl (nat (- i)) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   296
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   297
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   298
subsection "Split and cat operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   299
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   300
definition word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   301
  "word_cat a b == word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   302
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   303
definition word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   304
  "word_split a == 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   305
   case bin_split (len_of TYPE ('c)) (uint a) of 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   306
     (u, v) => (word_of_int u, word_of_int v)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   307
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   308
definition word_rcat :: "'a :: len0 word list => 'b :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   309
  "word_rcat ws == 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   310
  word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   311
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   312
definition word_rsplit :: "'a :: len0 word => 'b :: len word list" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   313
  "word_rsplit w == 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   314
  map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   315
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   316
definition max_word :: "'a::len word" -- "Largest representable machine integer." where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   317
  "max_word \<equiv> word_of_int (2 ^ len_of TYPE('a) - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   318
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   319
primrec of_bool :: "bool \<Rightarrow> 'a::len word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   320
  "of_bool False = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   321
| "of_bool True = 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   322
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   323
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   324
lemmas of_nth_def = word_set_bits_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   325
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   326
lemmas word_size_gt_0 [iff] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   327
  xtr1 [OF word_size len_gt_0, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   328
lemmas lens_gt_0 = word_size_gt_0 len_gt_0
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   329
lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   330
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   331
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   332
  by (simp add: uints_def range_bintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   333
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   334
lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   335
  by (simp add: sints_def range_sbintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   336
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   337
lemmas atLeastLessThan_alt = atLeastLessThan_def [unfolded 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   338
  atLeast_def lessThan_def Collect_conj_eq [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   339
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   340
lemma mod_in_reps: "m > 0 ==> y mod m : {0::int ..< m}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   341
  unfolding atLeastLessThan_alt by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   342
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   343
lemma 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   344
  uint_0:"0 <= uint x" and 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   345
  uint_lt: "uint (x::'a::len0 word) < 2 ^ len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   346
  by (auto simp: uint [simplified])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   347
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   348
lemma uint_mod_same:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   349
  "uint x mod 2 ^ len_of TYPE('a) = uint (x::'a::len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   350
  by (simp add: int_mod_eq uint_lt uint_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   351
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   352
lemma td_ext_uint: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   353
  "td_ext (uint :: 'a word => int) word_of_int (uints (len_of TYPE('a::len0))) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   354
    (%w::int. w mod 2 ^ len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   355
  apply (unfold td_ext_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   356
  apply (simp add: uints_num word_of_int_def bintrunc_mod2p)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   357
  apply (simp add: uint_mod_same uint_0 uint_lt
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   358
                   word.uint_inverse word.Abs_word_inverse int_mod_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   359
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   360
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   361
lemmas int_word_uint = td_ext_uint [THEN td_ext.eq_norm, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   362
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   363
interpretation word_uint:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   364
  td_ext "uint::'a::len0 word \<Rightarrow> int" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   365
         word_of_int 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   366
         "uints (len_of TYPE('a::len0))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   367
         "\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   368
  by (rule td_ext_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   369
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   370
lemmas td_uint = word_uint.td_thm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   371
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   372
lemmas td_ext_ubin = td_ext_uint 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   373
  [simplified len_gt_0 no_bintr_alt1 [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   374
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   375
interpretation word_ubin:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   376
  td_ext "uint::'a::len0 word \<Rightarrow> int" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   377
         word_of_int 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   378
         "uints (len_of TYPE('a::len0))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   379
         "bintrunc (len_of TYPE('a::len0))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   380
  by (rule td_ext_ubin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   381
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   382
lemma sint_sbintrunc': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   383
  "sint (word_of_int bin :: 'a word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   384
    (sbintrunc (len_of TYPE ('a :: len) - 1) bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   385
  unfolding sint_uint 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   386
  by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   387
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   388
lemma uint_sint: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   389
  "uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a :: len word))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   390
  unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   391
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   392
lemma bintr_uint': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   393
  "n >= size w ==> bintrunc n (uint w) = uint w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   394
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   395
  apply (subst word_ubin.norm_Rep [symmetric]) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   396
  apply (simp only: bintrunc_bintrunc_min word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   397
  apply (simp add: min_max.inf_absorb2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   398
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   399
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   400
lemma wi_bintr': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   401
  "wb = word_of_int bin ==> n >= size wb ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   402
    word_of_int (bintrunc n bin) = wb"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   403
  unfolding word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   404
  by (clarsimp simp add: word_ubin.norm_eq_iff [symmetric] min_max.inf_absorb1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   405
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   406
lemmas bintr_uint = bintr_uint' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   407
lemmas wi_bintr = wi_bintr' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   408
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   409
lemma td_ext_sbin: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   410
  "td_ext (sint :: 'a word => int) word_of_int (sints (len_of TYPE('a::len))) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   411
    (sbintrunc (len_of TYPE('a) - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   412
  apply (unfold td_ext_def' sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   413
  apply (simp add : word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   414
  apply (cases "len_of TYPE('a)")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   415
   apply (auto simp add : sints_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   416
  apply (rule sym [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   417
  apply (rule word_ubin.Abs_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   418
  apply (simp only: bintrunc_sbintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   419
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   420
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   421
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   422
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   423
lemmas td_ext_sint = td_ext_sbin 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   424
  [simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   425
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   426
(* We do sint before sbin, before sint is the user version
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   427
   and interpretations do not produce thm duplicates. I.e. 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   428
   we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   429
   because the latter is the same thm as the former *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   430
interpretation word_sint:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   431
  td_ext "sint ::'a::len word => int" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   432
          word_of_int 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   433
          "sints (len_of TYPE('a::len))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   434
          "%w. (w + 2^(len_of TYPE('a::len) - 1)) mod 2^len_of TYPE('a::len) -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   435
               2 ^ (len_of TYPE('a::len) - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   436
  by (rule td_ext_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   437
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   438
interpretation word_sbin:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   439
  td_ext "sint ::'a::len word => int" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   440
          word_of_int 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   441
          "sints (len_of TYPE('a::len))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   442
          "sbintrunc (len_of TYPE('a::len) - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   443
  by (rule td_ext_sbin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   444
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   445
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   446
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   447
lemmas td_sint = word_sint.td
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   448
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   449
lemma word_number_of_alt: "number_of b == word_of_int (number_of b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   450
  unfolding word_number_of_def by (simp add: number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   451
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   452
lemma word_no_wi: "number_of = word_of_int"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   453
  by (auto simp: word_number_of_def intro: ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   454
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   455
lemma to_bl_def': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   456
  "(to_bl :: 'a :: len0 word => bool list) =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   457
    bin_to_bl (len_of TYPE('a)) o uint"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   458
  by (auto simp: to_bl_def intro: ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   459
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   460
lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of w", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   461
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   462
lemmas uints_mod = uints_def [unfolded no_bintr_alt1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   463
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   464
lemma uint_bintrunc: "uint (number_of bin :: 'a word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   465
    number_of (bintrunc (len_of TYPE ('a :: len0)) bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   466
  unfolding word_number_of_def number_of_eq
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   467
  by (auto intro: word_ubin.eq_norm) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   468
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   469
lemma sint_sbintrunc: "sint (number_of bin :: 'a word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   470
    number_of (sbintrunc (len_of TYPE ('a :: len) - 1) bin)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   471
  unfolding word_number_of_def number_of_eq
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   472
  by (subst word_sbin.eq_norm) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   473
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   474
lemma unat_bintrunc: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   475
  "unat (number_of bin :: 'a :: len0 word) =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   476
    number_of (bintrunc (len_of TYPE('a)) bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   477
  unfolding unat_def nat_number_of_def 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   478
  by (simp only: uint_bintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   479
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   480
(* WARNING - these may not always be helpful *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   481
declare 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   482
  uint_bintrunc [simp] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   483
  sint_sbintrunc [simp] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   484
  unat_bintrunc [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   485
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   486
lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 ==> v = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   487
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   488
  apply (rule word_uint.Rep_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   489
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   490
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   491
    apply (rule word_ubin.norm_Rep)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   492
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   493
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   494
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   495
lemmas uint_lem = word_uint.Rep [unfolded uints_num mem_Collect_eq]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   496
lemmas sint_lem = word_sint.Rep [unfolded sints_num mem_Collect_eq]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   497
lemmas uint_ge_0 [iff] = uint_lem [THEN conjunct1, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   498
lemmas uint_lt2p [iff] = uint_lem [THEN conjunct2, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   499
lemmas sint_ge = sint_lem [THEN conjunct1, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   500
lemmas sint_lt = sint_lem [THEN conjunct2, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   501
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   502
lemma sign_uint_Pls [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   503
  "bin_sign (uint x) = Int.Pls"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   504
  by (simp add: sign_Pls_ge_0 number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   505
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   506
lemmas uint_m2p_neg = iffD2 [OF diff_less_0_iff_less uint_lt2p, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   507
lemmas uint_m2p_not_non_neg = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   508
  iffD2 [OF linorder_not_le uint_m2p_neg, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   509
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   510
lemma lt2p_lem:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   511
  "len_of TYPE('a) <= n ==> uint (w :: 'a :: len0 word) < 2 ^ n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   512
  by (rule xtr8 [OF _ uint_lt2p]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   513
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   514
lemmas uint_le_0_iff [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   515
  uint_ge_0 [THEN leD, THEN linorder_antisym_conv1, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   516
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   517
lemma uint_nat: "uint w == int (unat w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   518
  unfolding unat_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   519
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   520
lemma uint_number_of:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   521
  "uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   522
  unfolding word_number_of_alt
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   523
  by (simp only: int_word_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   524
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   525
lemma unat_number_of: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   526
  "bin_sign b = Int.Pls ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   527
  unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   528
  apply (unfold unat_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   529
  apply (clarsimp simp only: uint_number_of)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   530
  apply (rule nat_mod_distrib [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   531
    apply (erule sign_Pls_ge_0 [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   532
   apply (simp_all add: nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   533
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   534
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   535
lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   536
    2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   537
    2 ^ (len_of TYPE('a) - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   538
  unfolding word_number_of_alt by (rule int_word_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   539
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   540
lemma word_of_int_bin [simp] : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   541
  "(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   542
  unfolding word_number_of_alt by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   543
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   544
lemma word_int_case_wi: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   545
  "word_int_case f (word_of_int i :: 'b word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   546
    f (i mod 2 ^ len_of TYPE('b::len0))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   547
  unfolding word_int_case_def by (simp add: word_uint.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   548
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   549
lemma word_int_split: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   550
  "P (word_int_case f x) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   551
    (ALL i. x = (word_of_int i :: 'b :: len0 word) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   552
      0 <= i & i < 2 ^ len_of TYPE('b) --> P (f i))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   553
  unfolding word_int_case_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   554
  by (auto simp: word_uint.eq_norm int_mod_eq')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   555
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   556
lemma word_int_split_asm: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   557
  "P (word_int_case f x) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   558
    (~ (EX n. x = (word_of_int n :: 'b::len0 word) &
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   559
      0 <= n & n < 2 ^ len_of TYPE('b::len0) & ~ P (f n)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   560
  unfolding word_int_case_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   561
  by (auto simp: word_uint.eq_norm int_mod_eq')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   562
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   563
lemmas uint_range' =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   564
  word_uint.Rep [unfolded uints_num mem_Collect_eq, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   565
lemmas sint_range' = word_sint.Rep [unfolded One_nat_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   566
  sints_num mem_Collect_eq, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   567
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   568
lemma uint_range_size: "0 <= uint w & uint w < 2 ^ size w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   569
  unfolding word_size by (rule uint_range')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   570
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   571
lemma sint_range_size:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   572
  "- (2 ^ (size w - Suc 0)) <= sint w & sint w < 2 ^ (size w - Suc 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   573
  unfolding word_size by (rule sint_range')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   574
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   575
lemmas sint_above_size = sint_range_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   576
  [THEN conjunct2, THEN [2] xtr8, folded One_nat_def, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   577
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   578
lemmas sint_below_size = sint_range_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   579
  [THEN conjunct1, THEN [2] order_trans, folded One_nat_def, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   580
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   581
lemma test_bit_eq_iff: "(test_bit (u::'a::len0 word) = test_bit v) = (u = v)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   582
  unfolding word_test_bit_def by (simp add: bin_nth_eq_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   583
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   584
lemma test_bit_size [rule_format] : "(w::'a::len0 word) !! n --> n < size w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   585
  apply (unfold word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   586
  apply (subst word_ubin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   587
  apply (simp only: nth_bintr word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   588
  apply fast
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   589
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   590
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   591
lemma word_eqI [rule_format] : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   592
  fixes u :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   593
  shows "(ALL n. n < size u --> u !! n = v !! n) ==> u = v"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   594
  apply (rule test_bit_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   595
  apply (rule ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   596
  apply (erule allE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   597
  apply (erule impCE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   598
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   599
   apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   600
  apply (auto dest!: test_bit_size simp add: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   601
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   602
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   603
lemmas word_eqD = test_bit_eq_iff [THEN iffD2, THEN fun_cong, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   604
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   605
lemma test_bit_bin': "w !! n = (n < size w & bin_nth (uint w) n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   606
  unfolding word_test_bit_def word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   607
  by (simp add: nth_bintr [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   608
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   609
lemmas test_bit_bin = test_bit_bin' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   610
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   611
lemma bin_nth_uint_imp': "bin_nth (uint w) n --> n < size w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   612
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   613
  apply (rule impI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   614
  apply (rule nth_bintr [THEN iffD1, THEN conjunct1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   615
  apply (subst word_ubin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   616
  apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   617
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   618
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   619
lemma bin_nth_sint': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   620
  "n >= size w --> bin_nth (sint w) n = bin_nth (sint w) (size w - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   621
  apply (rule impI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   622
  apply (subst word_sbin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   623
  apply (simp add : nth_sbintr word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   624
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   625
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   626
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   627
lemmas bin_nth_uint_imp = bin_nth_uint_imp' [rule_format, unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   628
lemmas bin_nth_sint = bin_nth_sint' [rule_format, unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   629
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   630
(* type definitions theorem for in terms of equivalent bool list *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   631
lemma td_bl: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   632
  "type_definition (to_bl :: 'a::len0 word => bool list) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   633
                   of_bl  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   634
                   {bl. length bl = len_of TYPE('a)}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   635
  apply (unfold type_definition_def of_bl_def to_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   636
  apply (simp add: word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   637
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   638
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   639
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   640
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   641
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   642
interpretation word_bl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   643
  type_definition "to_bl :: 'a::len0 word => bool list"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   644
                  of_bl  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   645
                  "{bl. length bl = len_of TYPE('a::len0)}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   646
  by (rule td_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   647
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   648
lemma word_size_bl: "size w == size (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   649
  unfolding word_size by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   650
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   651
lemma to_bl_use_of_bl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   652
  "(to_bl w = bl) = (w = of_bl bl \<and> length bl = length (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   653
  by (fastsimp elim!: word_bl.Abs_inverse [simplified])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   654
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   655
lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   656
  unfolding word_reverse_def by (simp add: word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   657
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   658
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   659
  unfolding word_reverse_def by (simp add : word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   661
lemma word_rev_gal: "word_reverse w = u ==> word_reverse u = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   662
  by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   663
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   664
lemmas word_rev_gal' = sym [THEN word_rev_gal, symmetric, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   665
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   666
lemmas length_bl_gt_0 [iff] = xtr1 [OF word_bl.Rep' len_gt_0, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   667
lemmas bl_not_Nil [iff] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   668
  length_bl_gt_0 [THEN length_greater_0_conv [THEN iffD1], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   669
lemmas length_bl_neq_0 [iff] = length_bl_gt_0 [THEN gr_implies_not0]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   670
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   671
lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = Int.Min)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   672
  apply (unfold to_bl_def sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   673
  apply (rule trans [OF _ bl_sbin_sign])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   674
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   675
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   676
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   677
lemma of_bl_drop': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   678
  "lend = length bl - len_of TYPE ('a :: len0) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   679
    of_bl (drop lend bl) = (of_bl bl :: 'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   680
  apply (unfold of_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   681
  apply (clarsimp simp add : trunc_bl2bin [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   682
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   683
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   684
lemmas of_bl_no = of_bl_def [folded word_number_of_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   685
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   686
lemma test_bit_of_bl:  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   687
  "(of_bl bl::'a::len0 word) !! n = (rev bl ! n \<and> n < len_of TYPE('a) \<and> n < length bl)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   688
  apply (unfold of_bl_def word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   689
  apply (auto simp add: word_size word_ubin.eq_norm nth_bintr bin_nth_of_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   690
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   691
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   692
lemma no_of_bl: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   693
  "(number_of bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   694
  unfolding word_size of_bl_no by (simp add : word_number_of_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   695
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   696
lemma uint_bl: "to_bl w == bin_to_bl (size w) (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   697
  unfolding word_size to_bl_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   698
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   699
lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   700
  unfolding uint_bl by (simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   701
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   702
lemma to_bl_of_bin: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   703
  "to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   704
  unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   705
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   706
lemmas to_bl_no_bin [simp] = to_bl_of_bin [folded word_number_of_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   707
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   708
lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   709
  unfolding uint_bl by (simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   710
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   711
lemmas uint_bl_bin [simp] = trans [OF bin_bl_bin word_ubin.norm_Rep, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   712
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   713
lemmas num_AB_u [simp] = word_uint.Rep_inverse 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   714
  [unfolded o_def word_number_of_def [symmetric], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   715
lemmas num_AB_s [simp] = word_sint.Rep_inverse 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   716
  [unfolded o_def word_number_of_def [symmetric], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   717
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   718
(* naturals *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   719
lemma uints_unats: "uints n = int ` unats n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   720
  apply (unfold unats_def uints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   721
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   722
  apply (rule_tac image_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   723
  apply (erule_tac nat_0_le [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   724
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   725
  apply (erule_tac nat_less_iff [THEN iffD2])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   726
  apply (rule_tac [2] zless_nat_eq_int_zless [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   727
  apply (auto simp add : nat_power_eq int_power)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   728
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   729
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   730
lemma unats_uints: "unats n = nat ` uints n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   731
  by (auto simp add : uints_unats image_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   732
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   733
lemmas bintr_num = word_ubin.norm_eq_iff 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   734
  [symmetric, folded word_number_of_def, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   735
lemmas sbintr_num = word_sbin.norm_eq_iff 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   736
  [symmetric, folded word_number_of_def, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   737
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   738
lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   739
lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def, standard];
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   740
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   741
(* don't add these to simpset, since may want bintrunc n w to be simplified;
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   742
  may want these in reverse, but loop as simp rules, so use following *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   743
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   744
lemma num_of_bintr':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   745
  "bintrunc (len_of TYPE('a :: len0)) a = b ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   746
    number_of a = (number_of b :: 'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   747
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   748
  apply (rule_tac num_of_bintr [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   749
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   750
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   751
lemma num_of_sbintr':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   752
  "sbintrunc (len_of TYPE('a :: len) - 1) a = b ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   753
    number_of a = (number_of b :: 'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   754
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   755
  apply (rule_tac num_of_sbintr [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   756
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   757
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   758
lemmas num_abs_bintr = sym [THEN trans,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   759
  OF num_of_bintr word_number_of_def, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   760
lemmas num_abs_sbintr = sym [THEN trans,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   761
  OF num_of_sbintr word_number_of_def, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   762
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   763
(** cast - note, no arg for new length, as it's determined by type of result,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   764
  thus in "cast w = w, the type means cast to length of w! **)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   765
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   766
lemma ucast_id: "ucast w = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   767
  unfolding ucast_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   768
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   769
lemma scast_id: "scast w = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   770
  unfolding scast_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   771
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   772
lemma ucast_bl: "ucast w == of_bl (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   773
  unfolding ucast_def of_bl_def uint_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   774
  by (auto simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   775
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   776
lemma nth_ucast: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   777
  "(ucast w::'a::len0 word) !! n = (w !! n & n < len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   778
  apply (unfold ucast_def test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   779
  apply (simp add: word_ubin.eq_norm nth_bintr word_size) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   780
  apply (fast elim!: bin_nth_uint_imp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   781
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   782
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   783
(* for literal u(s)cast *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   784
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   785
lemma ucast_bintr [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   786
  "ucast (number_of w ::'a::len0 word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   787
   number_of (bintrunc (len_of TYPE('a)) w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   788
  unfolding ucast_def by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   789
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   790
lemma scast_sbintr [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   791
  "scast (number_of w ::'a::len word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   792
   number_of (sbintrunc (len_of TYPE('a) - Suc 0) w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   793
  unfolding scast_def by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   794
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   795
lemmas source_size = source_size_def [unfolded Let_def word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   796
lemmas target_size = target_size_def [unfolded Let_def word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   797
lemmas is_down = is_down_def [unfolded source_size target_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   798
lemmas is_up = is_up_def [unfolded source_size target_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   799
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   800
lemmas is_up_down =  trans [OF is_up is_down [symmetric], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   801
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   802
lemma down_cast_same': "uc = ucast ==> is_down uc ==> uc = scast"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   803
  apply (unfold is_down)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   804
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   805
  apply (rule ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   806
  apply (unfold ucast_def scast_def uint_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   807
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   808
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   809
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   810
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   811
lemma word_rev_tf': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   812
  "r = to_bl (of_bl bl) ==> r = rev (takefill False (length r) (rev bl))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   813
  unfolding of_bl_def uint_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   814
  by (clarsimp simp add: bl_bin_bl_rtf word_ubin.eq_norm word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   815
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   816
lemmas word_rev_tf = refl [THEN word_rev_tf', unfolded word_bl.Rep', standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   817
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   818
lemmas word_rep_drop = word_rev_tf [simplified takefill_alt,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   819
  simplified, simplified rev_take, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   820
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   821
lemma to_bl_ucast: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   822
  "to_bl (ucast (w::'b::len0 word) ::'a::len0 word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   823
   replicate (len_of TYPE('a) - len_of TYPE('b)) False @
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   824
   drop (len_of TYPE('b) - len_of TYPE('a)) (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   825
  apply (unfold ucast_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   826
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   827
   apply (rule word_rep_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   828
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   829
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   830
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   831
lemma ucast_up_app': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   832
  "uc = ucast ==> source_size uc + n = target_size uc ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   833
    to_bl (uc w) = replicate n False @ (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   834
  by (auto simp add : source_size target_size to_bl_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   835
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   836
lemma ucast_down_drop': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   837
  "uc = ucast ==> source_size uc = target_size uc + n ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   838
    to_bl (uc w) = drop n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   839
  by (auto simp add : source_size target_size to_bl_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   840
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   841
lemma scast_down_drop': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   842
  "sc = scast ==> source_size sc = target_size sc + n ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   843
    to_bl (sc w) = drop n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   844
  apply (subgoal_tac "sc = ucast")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   845
   apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   846
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   847
   apply (erule refl [THEN ucast_down_drop'])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   848
  apply (rule refl [THEN down_cast_same', symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   849
  apply (simp add : source_size target_size is_down)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   850
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   851
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   852
lemma sint_up_scast': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   853
  "sc = scast ==> is_up sc ==> sint (sc w) = sint w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   854
  apply (unfold is_up)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   855
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   856
  apply (simp add: scast_def word_sbin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   857
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   858
    prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   859
    apply (rule word_sbin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   860
   apply (rule sbintrunc_sbintrunc_l)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   861
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   862
   apply (subst word_sbin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   863
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   864
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   865
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   866
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   867
lemma uint_up_ucast':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   868
  "uc = ucast ==> is_up uc ==> uint (uc w) = uint w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   869
  apply (unfold is_up)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   870
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   871
  apply (rule bin_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   872
  apply (fold word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   873
  apply (auto simp add: nth_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   874
  apply (auto simp add: test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   875
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   876
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   877
lemmas down_cast_same = refl [THEN down_cast_same']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   878
lemmas ucast_up_app = refl [THEN ucast_up_app']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   879
lemmas ucast_down_drop = refl [THEN ucast_down_drop']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   880
lemmas scast_down_drop = refl [THEN scast_down_drop']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   881
lemmas uint_up_ucast = refl [THEN uint_up_ucast']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   882
lemmas sint_up_scast = refl [THEN sint_up_scast']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   883
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   884
lemma ucast_up_ucast': "uc = ucast ==> is_up uc ==> ucast (uc w) = ucast w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   885
  apply (simp (no_asm) add: ucast_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   886
  apply (clarsimp simp add: uint_up_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   887
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   888
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   889
lemma scast_up_scast': "sc = scast ==> is_up sc ==> scast (sc w) = scast w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   890
  apply (simp (no_asm) add: scast_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   891
  apply (clarsimp simp add: sint_up_scast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   892
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   893
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   894
lemma ucast_of_bl_up': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   895
  "w = of_bl bl ==> size bl <= size w ==> ucast w = of_bl bl"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   896
  by (auto simp add : nth_ucast word_size test_bit_of_bl intro!: word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   897
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   898
lemmas ucast_up_ucast = refl [THEN ucast_up_ucast']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   899
lemmas scast_up_scast = refl [THEN scast_up_scast']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   900
lemmas ucast_of_bl_up = refl [THEN ucast_of_bl_up']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   901
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   902
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   903
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   904
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   905
lemmas isduu = is_up_down [where c = "ucast", THEN iffD2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   906
lemmas isdus = is_up_down [where c = "scast", THEN iffD2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   907
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   908
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   909
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   910
lemma up_ucast_surj:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   911
  "is_up (ucast :: 'b::len0 word => 'a::len0 word) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   912
   surj (ucast :: 'a word => 'b word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   913
  by (rule surjI, erule ucast_up_ucast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   914
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   915
lemma up_scast_surj:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   916
  "is_up (scast :: 'b::len word => 'a::len word) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   917
   surj (scast :: 'a word => 'b word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   918
  by (rule surjI, erule scast_up_scast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   919
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   920
lemma down_scast_inj:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   921
  "is_down (scast :: 'b::len word => 'a::len word) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   922
   inj_on (ucast :: 'a word => 'b word) A"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   923
  by (rule inj_on_inverseI, erule scast_down_scast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   924
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   925
lemma down_ucast_inj:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   926
  "is_down (ucast :: 'b::len0 word => 'a::len0 word) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   927
   inj_on (ucast :: 'a word => 'b word) A"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   928
  by (rule inj_on_inverseI, erule ucast_down_ucast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   929
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   930
lemma of_bl_append_same: "of_bl (X @ to_bl w) = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   931
  by (rule word_bl.Rep_eqD) (simp add: word_rep_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   932
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   933
lemma ucast_down_no': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   934
  "uc = ucast ==> is_down uc ==> uc (number_of bin) = number_of bin"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   935
  apply (unfold word_number_of_def is_down)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   936
  apply (clarsimp simp add: ucast_def word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   937
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   938
  apply (erule bintrunc_bintrunc_ge)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   939
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   940
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   941
lemmas ucast_down_no = ucast_down_no' [OF refl]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   942
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   943
lemma ucast_down_bl': "uc = ucast ==> is_down uc ==> uc (of_bl bl) = of_bl bl"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   944
  unfolding of_bl_no by clarify (erule ucast_down_no)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   945
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   946
lemmas ucast_down_bl = ucast_down_bl' [OF refl]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   947
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   948
lemmas slice_def' = slice_def [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   949
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   950
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   951
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   952
lemmas word_log_bin_defs = word_log_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   953
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   954
text {* Executable equality *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   955
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38527
diff changeset
   956
instantiation word :: (len0) equal
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   957
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   958
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38527
diff changeset
   959
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" where
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38527
diff changeset
   960
  "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   961
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   962
instance proof
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38527
diff changeset
   963
qed (simp add: equal equal_word_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   964
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   965
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   966
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   967
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   968
subsection {* Word Arithmetic *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   969
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   970
lemma word_less_alt: "(a < b) = (uint a < uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   971
  unfolding word_less_def word_le_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   972
  by (auto simp del: word_uint.Rep_inject 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   973
           simp: word_uint.Rep_inject [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   974
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   975
lemma signed_linorder: "class.linorder word_sle word_sless"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   976
proof
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   977
qed (unfold word_sle_def word_sless_def, auto)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   978
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   979
interpretation signed: linorder "word_sle" "word_sless"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   980
  by (rule signed_linorder)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   981
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   982
lemmas word_arith_wis = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   983
  word_add_def word_mult_def word_minus_def 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   984
  word_succ_def word_pred_def word_0_wi word_1_wi
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   985
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   986
lemma udvdI: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   987
  "0 \<le> n ==> uint b = n * uint a ==> a udvd b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   988
  by (auto simp: udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   989
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   990
lemmas word_div_no [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   991
  word_div_def [of "number_of a" "number_of b", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   992
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   993
lemmas word_mod_no [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   994
  word_mod_def [of "number_of a" "number_of b", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   995
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   996
lemmas word_less_no [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   997
  word_less_def [of "number_of a" "number_of b", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   998
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   999
lemmas word_le_no [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1000
  word_le_def [of "number_of a" "number_of b", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1001
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1002
lemmas word_sless_no [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1003
  word_sless_def [of "number_of a" "number_of b", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1004
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1005
lemmas word_sle_no [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1006
  word_sle_def [of "number_of a" "number_of b", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1007
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1008
(* following two are available in class number_ring, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1009
  but convenient to have them here here;
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1010
  note - the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1011
  are in the default simpset, so to use the automatic simplifications for
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1012
  (eg) sint (number_of bin) on sint 1, must do
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1013
  (simp add: word_1_no del: numeral_1_eq_1) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1014
  *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1015
lemmas word_0_wi_Pls = word_0_wi [folded Pls_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1016
lemmas word_0_no = word_0_wi_Pls [folded word_no_wi]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1017
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1018
lemma int_one_bin: "(1 :: int) == (Int.Pls BIT 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1019
  unfolding Pls_def Bit_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1020
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1021
lemma word_1_no: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1022
  "(1 :: 'a :: len0 word) == number_of (Int.Pls BIT 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1023
  unfolding word_1_wi word_number_of_def int_one_bin by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1024
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1025
lemma word_m1_wi: "-1 == word_of_int -1" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1026
  by (rule word_number_of_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1027
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1028
lemma word_m1_wi_Min: "-1 = word_of_int Int.Min"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1029
  by (simp add: word_m1_wi number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1030
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1031
lemma word_0_bl: "of_bl [] = 0" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1032
  unfolding word_0_wi of_bl_def by (simp add : Pls_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1033
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1034
lemma word_1_bl: "of_bl [True] = 1" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1035
  unfolding word_1_wi of_bl_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1036
  by (simp add : bl_to_bin_def Bit_def Pls_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1037
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1038
lemma uint_eq_0 [simp] : "(uint 0 = 0)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1039
  unfolding word_0_wi
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1040
  by (simp add: word_ubin.eq_norm Pls_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1041
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1042
lemma of_bl_0 [simp] : "of_bl (replicate n False) = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1043
  by (simp add : word_0_wi of_bl_def bl_to_bin_rep_False Pls_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1044
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1045
lemma to_bl_0: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1046
  "to_bl (0::'a::len0 word) = replicate (len_of TYPE('a)) False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1047
  unfolding uint_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1048
  by (simp add : word_size bin_to_bl_Pls Pls_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1049
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1050
lemma uint_0_iff: "(uint x = 0) = (x = 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1051
  by (auto intro!: word_uint.Rep_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1052
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1053
lemma unat_0_iff: "(unat x = 0) = (x = 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1054
  unfolding unat_def by (auto simp add : nat_eq_iff uint_0_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1055
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1056
lemma unat_0 [simp]: "unat 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1057
  unfolding unat_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1058
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1059
lemma size_0_same': "size w = 0 ==> w = (v :: 'a :: len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1060
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1061
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1062
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1063
    apply (rule word_uint.Rep_inverse)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1064
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1065
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1066
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1067
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1068
lemmas size_0_same = size_0_same' [folded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1069
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1070
lemmas unat_eq_0 = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1071
lemmas unat_eq_zero = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1072
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1073
lemma unat_gt_0: "(0 < unat x) = (x ~= 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1074
by (auto simp: unat_0_iff [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1075
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1076
lemma ucast_0 [simp] : "ucast 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1077
unfolding ucast_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1078
by simp (simp add: word_0_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1079
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1080
lemma sint_0 [simp] : "sint 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1081
unfolding sint_uint
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1082
by (simp add: Pls_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1083
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1084
lemma scast_0 [simp] : "scast 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1085
apply (unfold scast_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1086
apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1087
apply (simp add: word_0_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1088
done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1089
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1090
lemma sint_n1 [simp] : "sint -1 = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1091
apply (unfold word_m1_wi_Min)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1092
apply (simp add: word_sbin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1093
apply (unfold Min_def number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1094
apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1095
done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1096
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1097
lemma scast_n1 [simp] : "scast -1 = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1098
  apply (unfold scast_def sint_n1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1099
  apply (unfold word_number_of_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1100
  apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1101
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1102
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1103
lemma uint_1 [simp] : "uint (1 :: 'a :: len word) = 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1104
  unfolding word_1_wi
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1105
  by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1106
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1107
lemma unat_1 [simp] : "unat (1 :: 'a :: len word) = 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1108
  by (unfold unat_def uint_1) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1109
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1110
lemma ucast_1 [simp] : "ucast (1 :: 'a :: len word) = 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1111
  unfolding ucast_def word_1_wi
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1112
  by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1113
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1114
(* abstraction preserves the operations
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1115
  (the definitions tell this for bins in range uint) *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1116
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1117
lemmas arths = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1118
  bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1119
                folded word_ubin.eq_norm, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1120
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1121
lemma wi_homs: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1122
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1123
  wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1124
  wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1125
  wi_hom_neg: "- word_of_int a = word_of_int (- a)" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1126
  wi_hom_succ: "word_succ (word_of_int a) = word_of_int (Int.succ a)" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1127
  wi_hom_pred: "word_pred (word_of_int a) = word_of_int (Int.pred a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1128
  by (auto simp: word_arith_wis arths)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1129
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1130
lemmas wi_hom_syms = wi_homs [symmetric]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1131
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1132
lemma word_sub_def: "a - b == a + - (b :: 'a :: len0 word)"
37887
2ae085b07f2f diff_minus subsumes diff_def
haftmann
parents: 37751
diff changeset
  1133
  unfolding word_sub_wi diff_minus
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1134
  by (simp only : word_uint.Rep_inverse wi_hom_syms)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1135
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1136
lemmas word_diff_minus = word_sub_def [THEN meta_eq_to_obj_eq, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1137
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1138
lemma word_of_int_sub_hom:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1139
  "(word_of_int a) - word_of_int b = word_of_int (a - b)"
37887
2ae085b07f2f diff_minus subsumes diff_def
haftmann
parents: 37751
diff changeset
  1140
  unfolding word_sub_def diff_minus by (simp only : wi_homs)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1141
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1142
lemmas new_word_of_int_homs = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1143
  word_of_int_sub_hom wi_homs word_0_wi word_1_wi 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1144
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1145
lemmas new_word_of_int_hom_syms = new_word_of_int_homs [symmetric, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1146
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1147
lemmas word_of_int_hom_syms =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1148
  new_word_of_int_hom_syms [unfolded succ_def pred_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1149
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1150
lemmas word_of_int_homs =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1151
  new_word_of_int_homs [unfolded succ_def pred_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1152
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1153
lemmas word_of_int_add_hom = word_of_int_homs (2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1154
lemmas word_of_int_mult_hom = word_of_int_homs (3)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1155
lemmas word_of_int_minus_hom = word_of_int_homs (4)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1156
lemmas word_of_int_succ_hom = word_of_int_homs (5)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1157
lemmas word_of_int_pred_hom = word_of_int_homs (6)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1158
lemmas word_of_int_0_hom = word_of_int_homs (7)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1159
lemmas word_of_int_1_hom = word_of_int_homs (8)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1160
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1161
(* now, to get the weaker results analogous to word_div/mod_def *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1162
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1163
lemmas word_arith_alts = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1164
  word_sub_wi [unfolded succ_def pred_def, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1165
  word_arith_wis [unfolded succ_def pred_def, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1166
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1167
lemmas word_sub_alt = word_arith_alts (1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1168
lemmas word_add_alt = word_arith_alts (2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1169
lemmas word_mult_alt = word_arith_alts (3)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1170
lemmas word_minus_alt = word_arith_alts (4)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1171
lemmas word_succ_alt = word_arith_alts (5)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1172
lemmas word_pred_alt = word_arith_alts (6)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1173
lemmas word_0_alt = word_arith_alts (7)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1174
lemmas word_1_alt = word_arith_alts (8)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1175
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1176
subsection  "Transferring goals from words to ints"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1177
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1178
lemma word_ths:  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1179
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1180
  word_succ_p1:   "word_succ a = a + 1" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1181
  word_pred_m1:   "word_pred a = a - 1" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1182
  word_pred_succ: "word_pred (word_succ a) = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1183
  word_succ_pred: "word_succ (word_pred a) = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1184
  word_mult_succ: "word_succ a * b = b + a * b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1185
  by (rule word_uint.Abs_cases [of b],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1186
      rule word_uint.Abs_cases [of a],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1187
      simp add: pred_def succ_def add_commute mult_commute 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1188
                ring_distribs new_word_of_int_homs)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1189
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1190
lemmas uint_cong = arg_cong [where f = uint]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1191
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1192
lemmas uint_word_ariths = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1193
  word_arith_alts [THEN trans [OF uint_cong int_word_uint], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1194
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1195
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1196
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1197
(* similar expressions for sint (arith operations) *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1198
lemmas sint_word_ariths = uint_word_arith_bintrs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1199
  [THEN uint_sint [symmetric, THEN trans],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1200
  unfolded uint_sint bintr_arith1s bintr_ariths 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1201
    len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1202
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1203
lemmas uint_div_alt = word_div_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1204
  [THEN trans [OF uint_cong int_word_uint], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1205
lemmas uint_mod_alt = word_mod_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1206
  [THEN trans [OF uint_cong int_word_uint], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1207
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1208
lemma word_pred_0_n1: "word_pred 0 = word_of_int -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1209
  unfolding word_pred_def number_of_eq
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1210
  by (simp add : pred_def word_no_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1211
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1212
lemma word_pred_0_Min: "word_pred 0 = word_of_int Int.Min"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1213
  by (simp add: word_pred_0_n1 number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1214
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1215
lemma word_m1_Min: "- 1 = word_of_int Int.Min"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1216
  unfolding Min_def by (simp only: word_of_int_hom_syms)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1217
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1218
lemma succ_pred_no [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1219
  "word_succ (number_of bin) = number_of (Int.succ bin) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1220
    word_pred (number_of bin) = number_of (Int.pred bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1221
  unfolding word_number_of_def by (simp add : new_word_of_int_homs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1222
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1223
lemma word_sp_01 [simp] : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1224
  "word_succ -1 = 0 & word_succ 0 = 1 & word_pred 0 = -1 & word_pred 1 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1225
  by (unfold word_0_no word_1_no) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1226
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1227
(* alternative approach to lifting arithmetic equalities *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1228
lemma word_of_int_Ex:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1229
  "\<exists>y. x = word_of_int y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1230
  by (rule_tac x="uint x" in exI) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1231
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1232
lemma word_arith_eqs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1233
  fixes a :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1234
  fixes b :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1235
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1236
  word_add_0: "0 + a = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1237
  word_add_0_right: "a + 0 = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1238
  word_mult_1: "1 * a = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1239
  word_mult_1_right: "a * 1 = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1240
  word_add_commute: "a + b = b + a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1241
  word_add_assoc: "a + b + c = a + (b + c)" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1242
  word_add_left_commute: "a + (b + c) = b + (a + c)" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1243
  word_mult_commute: "a * b = b * a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1244
  word_mult_assoc: "a * b * c = a * (b * c)" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1245
  word_mult_left_commute: "a * (b * c) = b * (a * c)" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1246
  word_left_distrib: "(a + b) * c = a * c + b * c" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1247
  word_right_distrib: "a * (b + c) = a * b + a * c" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1248
  word_left_minus: "- a + a = 0" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1249
  word_diff_0_right: "a - 0 = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1250
  word_diff_self: "a - a = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1251
  using word_of_int_Ex [of a] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1252
        word_of_int_Ex [of b] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1253
        word_of_int_Ex [of c]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1254
  by (auto simp: word_of_int_hom_syms [symmetric]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1255
                 zadd_0_right add_commute add_assoc add_left_commute
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1256
                 mult_commute mult_assoc mult_left_commute
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1257
                 left_distrib right_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1258
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1259
lemmas word_add_ac = word_add_commute word_add_assoc word_add_left_commute
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1260
lemmas word_mult_ac = word_mult_commute word_mult_assoc word_mult_left_commute
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1261
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1262
lemmas word_plus_ac0 = word_add_0 word_add_0_right word_add_ac
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1263
lemmas word_times_ac1 = word_mult_1 word_mult_1_right word_mult_ac
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1264
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1265
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1266
subsection "Order on fixed-length words"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1267
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1268
lemma word_order_trans: "x <= y ==> y <= z ==> x <= (z :: 'a :: len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1269
  unfolding word_le_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1270
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1271
lemma word_order_refl: "z <= (z :: 'a :: len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1272
  unfolding word_le_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1273
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1274
lemma word_order_antisym: "x <= y ==> y <= x ==> x = (y :: 'a :: len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1275
  unfolding word_le_def by (auto intro!: word_uint.Rep_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1276
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1277
lemma word_order_linear:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1278
  "y <= x | x <= (y :: 'a :: len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1279
  unfolding word_le_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1280
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1281
lemma word_zero_le [simp] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1282
  "0 <= (y :: 'a :: len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1283
  unfolding word_le_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1284
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1285
instance word :: (len0) semigroup_add
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1286
  by intro_classes (simp add: word_add_assoc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1287
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1288
instance word :: (len0) linorder
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1289
  by intro_classes (auto simp: word_less_def word_le_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1290
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1291
instance word :: (len0) ring
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1292
  by intro_classes
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1293
     (auto simp: word_arith_eqs word_diff_minus 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1294
                 word_diff_self [unfolded word_diff_minus])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1295
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1296
lemma word_m1_ge [simp] : "word_pred 0 >= y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1297
  unfolding word_le_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1298
  by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1299
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1300
lemmas word_n1_ge [simp]  = word_m1_ge [simplified word_sp_01]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1301
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1302
lemmas word_not_simps [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1303
  word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1304
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1305
lemma word_gt_0: "0 < y = (0 ~= (y :: 'a :: len0 word))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1306
  unfolding word_less_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1307
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1308
lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of y", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1309
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1310
lemma word_sless_alt: "(a <s b) == (sint a < sint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1311
  unfolding word_sle_def word_sless_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1312
  by (auto simp add: less_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1313
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1314
lemma word_le_nat_alt: "(a <= b) = (unat a <= unat b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1315
  unfolding unat_def word_le_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1316
  by (rule nat_le_eq_zle [symmetric]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1317
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1318
lemma word_less_nat_alt: "(a < b) = (unat a < unat b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1319
  unfolding unat_def word_less_alt
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1320
  by (rule nat_less_eq_zless [symmetric]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1321
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1322
lemma wi_less: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1323
  "(word_of_int n < (word_of_int m :: 'a :: len0 word)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1324
    (n mod 2 ^ len_of TYPE('a) < m mod 2 ^ len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1325
  unfolding word_less_alt by (simp add: word_uint.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1326
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1327
lemma wi_le: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1328
  "(word_of_int n <= (word_of_int m :: 'a :: len0 word)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1329
    (n mod 2 ^ len_of TYPE('a) <= m mod 2 ^ len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1330
  unfolding word_le_def by (simp add: word_uint.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1331
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1332
lemma udvd_nat_alt: "a udvd b = (EX n>=0. unat b = n * unat a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1333
  apply (unfold udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1334
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1335
   apply (simp add: unat_def nat_mult_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1336
  apply (simp add: uint_nat int_mult)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1337
  apply (rule exI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1338
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1339
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1340
   apply (erule notE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1341
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1342
  apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1343
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1344
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1345
lemma udvd_iff_dvd: "x udvd y <-> unat x dvd unat y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1346
  unfolding dvd_def udvd_nat_alt by force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1347
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1348
lemmas unat_mono = word_less_nat_alt [THEN iffD1, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1349
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1350
lemma word_zero_neq_one: "0 < len_of TYPE ('a :: len0) ==> (0 :: 'a word) ~= 1";
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1351
  unfolding word_arith_wis
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1352
  by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1353
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1354
lemmas lenw1_zero_neq_one = len_gt_0 [THEN word_zero_neq_one]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1355
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1356
lemma no_no [simp] : "number_of (number_of b) = number_of b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1357
  by (simp add: number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1358
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1359
lemma unat_minus_one: "x ~= 0 ==> unat (x - 1) = unat x - 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1360
  apply (unfold unat_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1361
  apply (simp only: int_word_uint word_arith_alts rdmods)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1362
  apply (subgoal_tac "uint x >= 1")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1363
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1364
   apply (drule contrapos_nn)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1365
    apply (erule word_uint.Rep_inverse' [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1366
   apply (insert uint_ge_0 [of x])[1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1367
   apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1368
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1369
    apply (rule nat_diff_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1370
     prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1371
     apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1372
    apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1373
   apply (subst mod_pos_pos_trivial)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1374
     apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1375
    apply (insert uint_lt2p [of x])[1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1376
    apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1377
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1378
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1379
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1380
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1381
lemma measure_unat: "p ~= 0 ==> unat (p - 1) < unat p"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1382
  by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1383
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1384
lemmas uint_add_ge0 [simp] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1385
  add_nonneg_nonneg [OF uint_ge_0 uint_ge_0, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1386
lemmas uint_mult_ge0 [simp] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1387
  mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1388
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1389
lemma uint_sub_lt2p [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1390
  "uint (x :: 'a :: len0 word) - uint (y :: 'b :: len0 word) < 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1391
    2 ^ len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1392
  using uint_ge_0 [of y] uint_lt2p [of x] by arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1393
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1394
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1395
subsection "Conditions for the addition (etc) of two words to overflow"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1396
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1397
lemma uint_add_lem: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1398
  "(uint x + uint y < 2 ^ len_of TYPE('a)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1399
    (uint (x + y :: 'a :: len0 word) = uint x + uint y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1400
  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1401
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1402
lemma uint_mult_lem: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1403
  "(uint x * uint y < 2 ^ len_of TYPE('a)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1404
    (uint (x * y :: 'a :: len0 word) = uint x * uint y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1405
  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1406
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1407
lemma uint_sub_lem: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1408
  "(uint x >= uint y) = (uint (x - y) = uint x - uint y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1409
  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1410
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1411
lemma uint_add_le: "uint (x + y) <= uint x + uint y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1412
  unfolding uint_word_ariths by (auto simp: mod_add_if_z)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1413
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1414
lemma uint_sub_ge: "uint (x - y) >= uint x - uint y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1415
  unfolding uint_word_ariths by (auto simp: mod_sub_if_z)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1416
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1417
lemmas uint_sub_if' =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1418
  trans [OF uint_word_ariths(1) mod_sub_if_z, simplified, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1419
lemmas uint_plus_if' =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1420
  trans [OF uint_word_ariths(2) mod_add_if_z, simplified, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1421
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1422
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1423
subsection {* Definition of uint\_arith *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1424
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1425
lemma word_of_int_inverse:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1426
  "word_of_int r = a ==> 0 <= r ==> r < 2 ^ len_of TYPE('a) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1427
   uint (a::'a::len0 word) = r"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1428
  apply (erule word_uint.Abs_inverse' [rotated])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1429
  apply (simp add: uints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1430
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1431
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1432
lemma uint_split:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1433
  fixes x::"'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1434
  shows "P (uint x) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1435
         (ALL i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) --> P i)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1436
  apply (fold word_int_case_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1437
  apply (auto dest!: word_of_int_inverse simp: int_word_uint int_mod_eq'
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1438
              split: word_int_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1439
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1440
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1441
lemma uint_split_asm:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1442
  fixes x::"'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1443
  shows "P (uint x) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1444
         (~(EX i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) & ~ P i))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1445
  by (auto dest!: word_of_int_inverse 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1446
           simp: int_word_uint int_mod_eq'
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1447
           split: uint_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1448
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1449
lemmas uint_splits = uint_split uint_split_asm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1450
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1451
lemmas uint_arith_simps = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1452
  word_le_def word_less_alt
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1453
  word_uint.Rep_inject [symmetric] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1454
  uint_sub_if' uint_plus_if'
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1455
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1456
(* use this to stop, eg, 2 ^ len_of TYPE (32) being simplified *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1457
lemma power_False_cong: "False ==> a ^ b = c ^ d" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1458
  by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1459
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1460
(* uint_arith_tac: reduce to arithmetic on int, try to solve by arith *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1461
ML {*
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1462
fun uint_arith_ss_of ss = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1463
  ss addsimps @{thms uint_arith_simps}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1464
     delsimps @{thms word_uint.Rep_inject}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1465
     addsplits @{thms split_if_asm} 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1466
     addcongs @{thms power_False_cong}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1467
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1468
fun uint_arith_tacs ctxt = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1469
  let
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1470
    fun arith_tac' n t =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1471
      Arith_Data.verbose_arith_tac ctxt n t
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1472
        handle Cooper.COOPER _ => Seq.empty;
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1473
    val cs = claset_of ctxt;
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1474
    val ss = simpset_of ctxt;
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1475
  in 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1476
    [ clarify_tac cs 1,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1477
      full_simp_tac (uint_arith_ss_of ss) 1,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1478
      ALLGOALS (full_simp_tac (HOL_ss addsplits @{thms uint_splits} 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1479
                                      addcongs @{thms power_False_cong})),
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1480
      rewrite_goals_tac @{thms word_size}, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1481
      ALLGOALS  (fn n => REPEAT (resolve_tac [allI, impI] n) THEN      
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1482
                         REPEAT (etac conjE n) THEN
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1483
                         REPEAT (dtac @{thm word_of_int_inverse} n 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1484
                                 THEN atac n 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1485
                                 THEN atac n)),
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1486
      TRYALL arith_tac' ]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1487
  end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1488
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1489
fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt))
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1490
*}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1491
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1492
method_setup uint_arith = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1493
  {* Scan.succeed (SIMPLE_METHOD' o uint_arith_tac) *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1494
  "solving word arithmetic via integers and arith"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1495
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1496
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1497
subsection "More on overflows and monotonicity"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1498
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1499
lemma no_plus_overflow_uint_size: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1500
  "((x :: 'a :: len0 word) <= x + y) = (uint x + uint y < 2 ^ size x)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1501
  unfolding word_size by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1502
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1503
lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1504
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1505
lemma no_ulen_sub: "((x :: 'a :: len0 word) >= x - y) = (uint y <= uint x)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1506
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1507
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1508
lemma no_olen_add':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1509
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1510
  shows "(x \<le> y + x) = (uint y + uint x < 2 ^ len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1511
  by (simp add: word_add_ac add_ac no_olen_add)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1512
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1513
lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1514
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1515
lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1516
lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1517
lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1518
lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1519
lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1520
lemmas word_sub_le = word_sub_le_iff [THEN iffD2, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1521
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1522
lemma word_less_sub1: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1523
  "(x :: 'a :: len word) ~= 0 ==> (1 < x) = (0 < x - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1524
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1525
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1526
lemma word_le_sub1: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1527
  "(x :: 'a :: len word) ~= 0 ==> (1 <= x) = (0 <= x - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1528
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1529
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1530
lemma sub_wrap_lt: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1531
  "((x :: 'a :: len0 word) < x - z) = (x < z)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1532
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1533
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1534
lemma sub_wrap: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1535
  "((x :: 'a :: len0 word) <= x - z) = (z = 0 | x < z)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1536
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1537
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1538
lemma plus_minus_not_NULL_ab: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1539
  "(x :: 'a :: len0 word) <= ab - c ==> c <= ab ==> c ~= 0 ==> x + c ~= 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1540
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1541
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1542
lemma plus_minus_no_overflow_ab: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1543
  "(x :: 'a :: len0 word) <= ab - c ==> c <= ab ==> x <= x + c" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1544
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1545
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1546
lemma le_minus': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1547
  "(a :: 'a :: len0 word) + c <= b ==> a <= a + c ==> c <= b - a"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1548
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1549
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1550
lemma le_plus': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1551
  "(a :: 'a :: len0 word) <= b ==> c <= b - a ==> a + c <= b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1552
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1553
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1554
lemmas le_plus = le_plus' [rotated]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1555
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1556
lemmas le_minus = leD [THEN thin_rl, THEN le_minus', standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1557
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1558
lemma word_plus_mono_right: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1559
  "(y :: 'a :: len0 word) <= z ==> x <= x + z ==> x + y <= x + z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1560
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1561
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1562
lemma word_less_minus_cancel: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1563
  "y - x < z - x ==> x <= z ==> (y :: 'a :: len0 word) < z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1564
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1565
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1566
lemma word_less_minus_mono_left: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1567
  "(y :: 'a :: len0 word) < z ==> x <= y ==> y - x < z - x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1568
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1569
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1570
lemma word_less_minus_mono:  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1571
  "a < c ==> d < b ==> a - b < a ==> c - d < c 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1572
  ==> a - b < c - (d::'a::len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1573
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1574
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1575
lemma word_le_minus_cancel: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1576
  "y - x <= z - x ==> x <= z ==> (y :: 'a :: len0 word) <= z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1577
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1578
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1579
lemma word_le_minus_mono_left: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1580
  "(y :: 'a :: len0 word) <= z ==> x <= y ==> y - x <= z - x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1581
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1582
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1583
lemma word_le_minus_mono:  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1584
  "a <= c ==> d <= b ==> a - b <= a ==> c - d <= c 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1585
  ==> a - b <= c - (d::'a::len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1586
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1587
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1588
lemma plus_le_left_cancel_wrap: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1589
  "(x :: 'a :: len0 word) + y' < x ==> x + y < x ==> (x + y' < x + y) = (y' < y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1590
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1591
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1592
lemma plus_le_left_cancel_nowrap: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1593
  "(x :: 'a :: len0 word) <= x + y' ==> x <= x + y ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1594
    (x + y' < x + y) = (y' < y)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1595
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1596
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1597
lemma word_plus_mono_right2: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1598
  "(a :: 'a :: len0 word) <= a + b ==> c <= b ==> a <= a + c"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1599
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1600
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1601
lemma word_less_add_right: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1602
  "(x :: 'a :: len0 word) < y - z ==> z <= y ==> x + z < y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1603
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1604
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1605
lemma word_less_sub_right: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1606
  "(x :: 'a :: len0 word) < y + z ==> y <= x ==> x - y < z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1607
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1608
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1609
lemma word_le_plus_either: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1610
  "(x :: 'a :: len0 word) <= y | x <= z ==> y <= y + z ==> x <= y + z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1611
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1612
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1613
lemma word_less_nowrapI: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1614
  "(x :: 'a :: len0 word) < z - k ==> k <= z ==> 0 < k ==> x < x + k"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1615
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1616
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1617
lemma inc_le: "(i :: 'a :: len word) < m ==> i + 1 <= m"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1618
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1619
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1620
lemma inc_i: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1621
  "(1 :: 'a :: len word) <= i ==> i < m ==> 1 <= (i + 1) & i + 1 <= m"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1622
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1623
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1624
lemma udvd_incr_lem:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1625
  "up < uq ==> up = ua + n * uint K ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1626
    uq = ua + n' * uint K ==> up + uint K <= uq"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1627
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1628
  apply (drule less_le_mult)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1629
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1630
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1631
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1632
lemma udvd_incr': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1633
  "p < q ==> uint p = ua + n * uint K ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1634
    uint q = ua + n' * uint K ==> p + K <= q" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1635
  apply (unfold word_less_alt word_le_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1636
  apply (drule (2) udvd_incr_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1637
  apply (erule uint_add_le [THEN order_trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1638
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1639
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1640
lemma udvd_decr': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1641
  "p < q ==> uint p = ua + n * uint K ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1642
    uint q = ua + n' * uint K ==> p <= q - K"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1643
  apply (unfold word_less_alt word_le_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1644
  apply (drule (2) udvd_incr_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1645
  apply (drule le_diff_eq [THEN iffD2])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1646
  apply (erule order_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1647
  apply (rule uint_sub_ge)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1648
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1649
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1650
lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1651
lemmas udvd_incr0 = udvd_incr' [where ua=0, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1652
lemmas udvd_decr0 = udvd_decr' [where ua=0, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1653
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1654
lemma udvd_minus_le': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1655
  "xy < k ==> z udvd xy ==> z udvd k ==> xy <= k - z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1656
  apply (unfold udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1657
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1658
  apply (erule (2) udvd_decr0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1659
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1661
ML {* Delsimprocs Numeral_Simprocs.cancel_factors *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1662
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1663
lemma udvd_incr2_K: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1664
  "p < a + s ==> a <= a + s ==> K udvd s ==> K udvd p - a ==> a <= p ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1665
    0 < K ==> p <= p + K & p + K <= a + s"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1666
  apply (unfold udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1667
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1668
  apply (simp add: uint_arith_simps split: split_if_asm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1669
   prefer 2 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1670
   apply (insert uint_range' [of s])[1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1671
   apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1672
  apply (drule add_commute [THEN xtr1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1673
  apply (simp add: diff_less_eq [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1674
  apply (drule less_le_mult)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1675
   apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1676
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1677
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1678
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1679
ML {* Addsimprocs Numeral_Simprocs.cancel_factors *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1680
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1681
(* links with rbl operations *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1682
lemma word_succ_rbl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1683
  "to_bl w = bl ==> to_bl (word_succ w) = (rev (rbl_succ (rev bl)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1684
  apply (unfold word_succ_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1685
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1686
  apply (simp add: to_bl_of_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1687
  apply (simp add: to_bl_def rbl_succ)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1688
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1689
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1690
lemma word_pred_rbl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1691
  "to_bl w = bl ==> to_bl (word_pred w) = (rev (rbl_pred (rev bl)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1692
  apply (unfold word_pred_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1693
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1694
  apply (simp add: to_bl_of_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1695
  apply (simp add: to_bl_def rbl_pred)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1696
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1697
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1698
lemma word_add_rbl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1699
  "to_bl v = vbl ==> to_bl w = wbl ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1700
    to_bl (v + w) = (rev (rbl_add (rev vbl) (rev wbl)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1701
  apply (unfold word_add_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1702
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1703
  apply (simp add: to_bl_of_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1704
  apply (simp add: to_bl_def rbl_add)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1705
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1706
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1707
lemma word_mult_rbl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1708
  "to_bl v = vbl ==> to_bl w = wbl ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1709
    to_bl (v * w) = (rev (rbl_mult (rev vbl) (rev wbl)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1710
  apply (unfold word_mult_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1711
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1712
  apply (simp add: to_bl_of_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1713
  apply (simp add: to_bl_def rbl_mult)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1714
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1715
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1716
lemma rtb_rbl_ariths:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1717
  "rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_succ w)) = rbl_succ ys"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1718
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1719
  "rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_pred w)) = rbl_pred ys"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1720
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1721
  "[| rev (to_bl v) = ys; rev (to_bl w) = xs |] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1722
  ==> rev (to_bl (v * w)) = rbl_mult ys xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1723
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1724
  "[| rev (to_bl v) = ys; rev (to_bl w) = xs |] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1725
  ==> rev (to_bl (v + w)) = rbl_add ys xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1726
  by (auto simp: rev_swap [symmetric] word_succ_rbl 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1727
                 word_pred_rbl word_mult_rbl word_add_rbl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1728
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1729
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1730
subsection "Arithmetic type class instantiations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1731
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1732
instance word :: (len0) comm_monoid_add ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1733
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1734
instance word :: (len0) comm_monoid_mult
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1735
  apply (intro_classes)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1736
   apply (simp add: word_mult_commute)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1737
  apply (simp add: word_mult_1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1738
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1739
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1740
instance word :: (len0) comm_semiring 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1741
  by (intro_classes) (simp add : word_left_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1742
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1743
instance word :: (len0) ab_group_add ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1744
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1745
instance word :: (len0) comm_ring ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1746
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1747
instance word :: (len) comm_semiring_1 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1748
  by (intro_classes) (simp add: lenw1_zero_neq_one)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1749
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1750
instance word :: (len) comm_ring_1 ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1751
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1752
instance word :: (len0) comm_semiring_0 ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1753
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1754
instance word :: (len0) order ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1755
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1756
(* note that iszero_def is only for class comm_semiring_1_cancel,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1757
   which requires word length >= 1, ie 'a :: len word *) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1758
lemma zero_bintrunc:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1759
  "iszero (number_of x :: 'a :: len word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1760
    (bintrunc (len_of TYPE('a)) x = Int.Pls)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1761
  apply (unfold iszero_def word_0_wi word_no_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1762
  apply (rule word_ubin.norm_eq_iff [symmetric, THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1763
  apply (simp add : Pls_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1764
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1765
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1766
lemmas word_le_0_iff [simp] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1767
  word_zero_le [THEN leD, THEN linorder_antisym_conv1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1768
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1769
lemma word_of_nat: "of_nat n = word_of_int (int n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1770
  by (induct n) (auto simp add : word_of_int_hom_syms)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1771
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1772
lemma word_of_int: "of_int = word_of_int"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1773
  apply (rule ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1774
  apply (unfold of_int_def)
39910
10097e0a9dbd constant `contents` renamed to `the_elem`
haftmann
parents: 39302
diff changeset
  1775
  apply (rule the_elemI)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1776
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1777
  apply (simp_all add: word_of_nat word_of_int_homs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1778
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1779
   apply (rule Rep_Integ_ne [THEN nonemptyE])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1780
   apply (rule bexI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1781
    prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1782
    apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1783
   apply (auto simp add: RI_eq_diff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1784
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1785
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1786
lemma word_of_int_nat: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1787
  "0 <= x ==> word_of_int x = of_nat (nat x)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1788
  by (simp add: of_nat_nat word_of_int)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1789
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1790
lemma word_number_of_eq: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1791
  "number_of w = (of_int w :: 'a :: len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1792
  unfolding word_number_of_def word_of_int by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1793
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1794
instance word :: (len) number_ring
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1795
  by (intro_classes) (simp add : word_number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1796
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1797
lemma iszero_word_no [simp] : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1798
  "iszero (number_of bin :: 'a :: len word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1799
    iszero (number_of (bintrunc (len_of TYPE('a)) bin) :: int)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1800
  apply (simp add: zero_bintrunc number_of_is_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1801
  apply (unfold iszero_def Pls_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1802
  apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1803
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1804
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1805
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1806
subsection "Word and nat"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1807
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1808
lemma td_ext_unat':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1809
  "n = len_of TYPE ('a :: len) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1810
    td_ext (unat :: 'a word => nat) of_nat 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1811
    (unats n) (%i. i mod 2 ^ n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1812
  apply (unfold td_ext_def' unat_def word_of_nat unats_uints)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1813
  apply (auto intro!: imageI simp add : word_of_int_hom_syms)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1814
  apply (erule word_uint.Abs_inverse [THEN arg_cong])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1815
  apply (simp add: int_word_uint nat_mod_distrib nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1816
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1817
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1818
lemmas td_ext_unat = refl [THEN td_ext_unat']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1819
lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1820
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1821
interpretation word_unat:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1822
  td_ext "unat::'a::len word => nat" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1823
         of_nat 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1824
         "unats (len_of TYPE('a::len))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1825
         "%i. i mod 2 ^ len_of TYPE('a::len)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1826
  by (rule td_ext_unat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1827
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1828
lemmas td_unat = word_unat.td_thm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1829
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1830
lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1831
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1832
lemma unat_le: "y <= unat (z :: 'a :: len word) ==> y : unats (len_of TYPE ('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1833
  apply (unfold unats_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1834
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1835
  apply (rule xtrans, rule unat_lt2p, assumption) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1836
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1837
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1838
lemma word_nchotomy:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1839
  "ALL w. EX n. (w :: 'a :: len word) = of_nat n & n < 2 ^ len_of TYPE ('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1840
  apply (rule allI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1841
  apply (rule word_unat.Abs_cases)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1842
  apply (unfold unats_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1843
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1844
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1845
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1846
lemma of_nat_eq:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1847
  fixes w :: "'a::len word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1848
  shows "(of_nat n = w) = (\<exists>q. n = unat w + q * 2 ^ len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1849
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1850
   apply (rule word_unat.inverse_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1851
  apply (rule iffI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1852
   apply (rule mod_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1853
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1854
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1855
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1856
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1857
lemma of_nat_eq_size: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1858
  "(of_nat n = w) = (EX q. n = unat w + q * 2 ^ size w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1859
  unfolding word_size by (rule of_nat_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1860
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1861
lemma of_nat_0:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1862
  "(of_nat m = (0::'a::len word)) = (\<exists>q. m = q * 2 ^ len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1863
  by (simp add: of_nat_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1864
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1865
lemmas of_nat_2p = mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1866
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1867
lemma of_nat_gt_0: "of_nat k ~= 0 ==> 0 < k"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1868
  by (cases k) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1869
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1870
lemma of_nat_neq_0: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1871
  "0 < k ==> k < 2 ^ len_of TYPE ('a :: len) ==> of_nat k ~= (0 :: 'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1872
  by (clarsimp simp add : of_nat_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1873
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1874
lemma Abs_fnat_hom_add:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1875
  "of_nat a + of_nat b = of_nat (a + b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1876
  by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1877
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1878
lemma Abs_fnat_hom_mult:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1879
  "of_nat a * of_nat b = (of_nat (a * b) :: 'a :: len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1880
  by (simp add: word_of_nat word_of_int_mult_hom zmult_int)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1881
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1882
lemma Abs_fnat_hom_Suc:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1883
  "word_succ (of_nat a) = of_nat (Suc a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1884
  by (simp add: word_of_nat word_of_int_succ_hom add_ac)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1885
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1886
lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1887
  by (simp add: word_of_nat word_0_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1888
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1889
lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1890
  by (simp add: word_of_nat word_1_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1891
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1892
lemmas Abs_fnat_homs = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1893
  Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1894
  Abs_fnat_hom_0 Abs_fnat_hom_1
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1895
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1896
lemma word_arith_nat_add:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1897
  "a + b = of_nat (unat a + unat b)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1898
  by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1899
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1900
lemma word_arith_nat_mult:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1901
  "a * b = of_nat (unat a * unat b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1902
  by (simp add: Abs_fnat_hom_mult [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1903
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1904
lemma word_arith_nat_Suc:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1905
  "word_succ a = of_nat (Suc (unat a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1906
  by (subst Abs_fnat_hom_Suc [symmetric]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1907
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1908
lemma word_arith_nat_div:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1909
  "a div b = of_nat (unat a div unat b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1910
  by (simp add: word_div_def word_of_nat zdiv_int uint_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1911
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1912
lemma word_arith_nat_mod:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1913
  "a mod b = of_nat (unat a mod unat b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1914
  by (simp add: word_mod_def word_of_nat zmod_int uint_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1915
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1916
lemmas word_arith_nat_defs =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1917
  word_arith_nat_add word_arith_nat_mult
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1918
  word_arith_nat_Suc Abs_fnat_hom_0
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1919
  Abs_fnat_hom_1 word_arith_nat_div
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1920
  word_arith_nat_mod 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1921
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1922
lemmas unat_cong = arg_cong [where f = "unat"]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1923
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1924
lemmas unat_word_ariths = word_arith_nat_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1925
  [THEN trans [OF unat_cong unat_of_nat], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1926
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1927
lemmas word_sub_less_iff = word_sub_le_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1928
  [simplified linorder_not_less [symmetric], simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1929
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1930
lemma unat_add_lem: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1931
  "(unat x + unat y < 2 ^ len_of TYPE('a)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1932
    (unat (x + y :: 'a :: len word) = unat x + unat y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1933
  unfolding unat_word_ariths
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1934
  by (auto intro!: trans [OF _ nat_mod_lem])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1935
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1936
lemma unat_mult_lem: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1937
  "(unat x * unat y < 2 ^ len_of TYPE('a)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1938
    (unat (x * y :: 'a :: len word) = unat x * unat y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1939
  unfolding unat_word_ariths
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1940
  by (auto intro!: trans [OF _ nat_mod_lem])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1941
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1942
lemmas unat_plus_if' = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1943
  trans [OF unat_word_ariths(1) mod_nat_add, simplified, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1944
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1945
lemma le_no_overflow: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1946
  "x <= b ==> a <= a + b ==> x <= a + (b :: 'a :: len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1947
  apply (erule order_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1948
  apply (erule olen_add_eqv [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1949
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1950
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1951
lemmas un_ui_le = trans 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1952
  [OF word_le_nat_alt [symmetric] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1953
      word_le_def, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1954
   standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1955
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1956
lemma unat_sub_if_size:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1957
  "unat (x - y) = (if unat y <= unat x 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1958
   then unat x - unat y 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1959
   else unat x + 2 ^ size x - unat y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1960
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1961
  apply (simp add: un_ui_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1962
  apply (auto simp add: unat_def uint_sub_if')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1963
   apply (rule nat_diff_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1964
    prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1965
    apply (simp add: algebra_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1966
    apply (rule nat_diff_distrib [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1967
      prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1968
      apply (subst nat_add_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1969
        prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1970
        apply (simp add: nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1971
       apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1972
  apply uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1973
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1974
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1975
lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1976
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1977
lemma unat_div: "unat ((x :: 'a :: len word) div y) = unat x div unat y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1978
  apply (simp add : unat_word_ariths)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1979
  apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1980
  apply (rule div_le_dividend)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1981
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1982
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1983
lemma unat_mod: "unat ((x :: 'a :: len word) mod y) = unat x mod unat y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1984
  apply (clarsimp simp add : unat_word_ariths)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1985
  apply (cases "unat y")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1986
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1987
   apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1988
   apply (rule mod_le_divisor)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1989
   apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1990
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1991
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1992
lemma uint_div: "uint ((x :: 'a :: len word) div y) = uint x div uint y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1993
  unfolding uint_nat by (simp add : unat_div zdiv_int)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1994
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1995
lemma uint_mod: "uint ((x :: 'a :: len word) mod y) = uint x mod uint y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1996
  unfolding uint_nat by (simp add : unat_mod zmod_int)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1997
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1998
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1999
subsection {* Definition of unat\_arith tactic *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2000
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2001
lemma unat_split:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2002
  fixes x::"'a::len word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2003
  shows "P (unat x) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2004
         (ALL n. of_nat n = x & n < 2^len_of TYPE('a) --> P n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2005
  by (auto simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2006
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2007
lemma unat_split_asm:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2008
  fixes x::"'a::len word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2009
  shows "P (unat x) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2010
         (~(EX n. of_nat n = x & n < 2^len_of TYPE('a) & ~ P n))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2011
  by (auto simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2012
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2013
lemmas of_nat_inverse = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2014
  word_unat.Abs_inverse' [rotated, unfolded unats_def, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2015
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2016
lemmas unat_splits = unat_split unat_split_asm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2017
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2018
lemmas unat_arith_simps =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2019
  word_le_nat_alt word_less_nat_alt
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2020
  word_unat.Rep_inject [symmetric]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2021
  unat_sub_if' unat_plus_if' unat_div unat_mod
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2022
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2023
(* unat_arith_tac: tactic to reduce word arithmetic to nat, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2024
   try to solve via arith *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2025
ML {*
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2026
fun unat_arith_ss_of ss = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2027
  ss addsimps @{thms unat_arith_simps}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2028
     delsimps @{thms word_unat.Rep_inject}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2029
     addsplits @{thms split_if_asm}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2030
     addcongs @{thms power_False_cong}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2031
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2032
fun unat_arith_tacs ctxt =   
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2033
  let
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2034
    fun arith_tac' n t =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2035
      Arith_Data.verbose_arith_tac ctxt n t
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2036
        handle Cooper.COOPER _ => Seq.empty;
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2037
    val cs = claset_of ctxt;
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2038
    val ss = simpset_of ctxt;
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2039
  in 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2040
    [ clarify_tac cs 1,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2041
      full_simp_tac (unat_arith_ss_of ss) 1,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2042
      ALLGOALS (full_simp_tac (HOL_ss addsplits @{thms unat_splits} 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2043
                                       addcongs @{thms power_False_cong})),
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2044
      rewrite_goals_tac @{thms word_size}, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2045
      ALLGOALS  (fn n => REPEAT (resolve_tac [allI, impI] n) THEN      
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2046
                         REPEAT (etac conjE n) THEN
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2047
                         REPEAT (dtac @{thm of_nat_inverse} n THEN atac n)),
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2048
      TRYALL arith_tac' ] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2049
  end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2050
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2051
fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt))
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2052
*}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2053
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2054
method_setup unat_arith = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2055
  {* Scan.succeed (SIMPLE_METHOD' o unat_arith_tac) *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2056
  "solving word arithmetic via natural numbers and arith"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2057
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2058
lemma no_plus_overflow_unat_size: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2059
  "((x :: 'a :: len word) <= x + y) = (unat x + unat y < 2 ^ size x)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2060
  unfolding word_size by unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2061
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2062
lemmas no_olen_add_nat = no_plus_overflow_unat_size [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2063
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2064
lemmas unat_plus_simple = trans [OF no_olen_add_nat unat_add_lem, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2065
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2066
lemma word_div_mult: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2067
  "(0 :: 'a :: len word) < y ==> unat x * unat y < 2 ^ len_of TYPE('a) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2068
    x * y div y = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2069
  apply unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2070
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2071
  apply (subst unat_mult_lem [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2072
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2073
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2074
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2075
lemma div_lt': "(i :: 'a :: len word) <= k div x ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2076
    unat i * unat x < 2 ^ len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2077
  apply unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2078
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2079
  apply (drule mult_le_mono1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2080
  apply (erule order_le_less_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2081
  apply (rule xtr7 [OF unat_lt2p div_mult_le])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2082
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2083
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2084
lemmas div_lt'' = order_less_imp_le [THEN div_lt']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2085
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2086
lemma div_lt_mult: "(i :: 'a :: len word) < k div x ==> 0 < x ==> i * x < k"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2087
  apply (frule div_lt'' [THEN unat_mult_lem [THEN iffD1]])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2088
  apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2089
  apply (drule (1) mult_less_mono1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2090
  apply (erule order_less_le_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2091
  apply (rule div_mult_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2092
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2093
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2094
lemma div_le_mult: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2095
  "(i :: 'a :: len word) <= k div x ==> 0 < x ==> i * x <= k"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2096
  apply (frule div_lt' [THEN unat_mult_lem [THEN iffD1]])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2097
  apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2098
  apply (drule mult_le_mono1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2099
  apply (erule order_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2100
  apply (rule div_mult_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2101
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2102
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2103
lemma div_lt_uint': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2104
  "(i :: 'a :: len word) <= k div x ==> uint i * uint x < 2 ^ len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2105
  apply (unfold uint_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2106
  apply (drule div_lt')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2107
  apply (simp add: zmult_int zless_nat_eq_int_zless [symmetric] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2108
                   nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2109
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2110
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2111
lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2112
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2113
lemma word_le_exists': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2114
  "(x :: 'a :: len0 word) <= y ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2115
    (EX z. y = x + z & uint x + uint z < 2 ^ len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2116
  apply (rule exI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2117
  apply (rule conjI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2118
  apply (rule zadd_diff_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2119
  apply uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2120
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2121
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2122
lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2123
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2124
lemmas plus_minus_no_overflow =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2125
  order_less_imp_le [THEN plus_minus_no_overflow_ab]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2126
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2127
lemmas mcs = word_less_minus_cancel word_less_minus_mono_left
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2128
  word_le_minus_cancel word_le_minus_mono_left
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2129
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2130
lemmas word_l_diffs = mcs [where y = "w + x", unfolded add_diff_cancel, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2131
lemmas word_diff_ls = mcs [where z = "w + x", unfolded add_diff_cancel, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2132
lemmas word_plus_mcs = word_diff_ls 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2133
  [where y = "v + x", unfolded add_diff_cancel, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2134
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2135
lemmas le_unat_uoi = unat_le [THEN word_unat.Abs_inverse]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2136
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2137
lemmas thd = refl [THEN [2] split_div_lemma [THEN iffD2], THEN conjunct1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2138
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2139
lemma thd1:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2140
  "a div b * b \<le> (a::nat)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2141
  using gt_or_eq_0 [of b]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2142
  apply (rule disjE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2143
   apply (erule xtr4 [OF thd mult_commute])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2144
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2145
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2146
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2147
lemmas uno_simps [THEN le_unat_uoi, standard] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2148
  mod_le_divisor div_le_dividend thd1 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2149
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2150
lemma word_mod_div_equality:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2151
  "(n div b) * b + (n mod b) = (n :: 'a :: len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2152
  apply (unfold word_less_nat_alt word_arith_nat_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2153
  apply (cut_tac y="unat b" in gt_or_eq_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2154
  apply (erule disjE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2155
   apply (simp add: mod_div_equality uno_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2156
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2157
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2158
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2159
lemma word_div_mult_le: "a div b * b <= (a::'a::len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2160
  apply (unfold word_le_nat_alt word_arith_nat_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2161
  apply (cut_tac y="unat b" in gt_or_eq_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2162
  apply (erule disjE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2163
   apply (simp add: div_mult_le uno_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2164
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2165
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2166
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2167
lemma word_mod_less_divisor: "0 < n ==> m mod n < (n :: 'a :: len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2168
  apply (simp only: word_less_nat_alt word_arith_nat_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2169
  apply (clarsimp simp add : uno_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2170
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2171
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2172
lemma word_of_int_power_hom: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2173
  "word_of_int a ^ n = (word_of_int (a ^ n) :: 'a :: len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2174
  by (induct n) (simp_all add : word_of_int_hom_syms power_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2175
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2176
lemma word_arith_power_alt: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2177
  "a ^ n = (word_of_int (uint a ^ n) :: 'a :: len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2178
  by (simp add : word_of_int_power_hom [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2179
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2180
lemma of_bl_length_less: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2181
  "length x = k ==> k < len_of TYPE('a) ==> (of_bl x :: 'a :: len word) < 2 ^ k"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2182
  apply (unfold of_bl_no [unfolded word_number_of_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2183
                word_less_alt word_number_of_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2184
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2185
  apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2186
                       del: word_of_int_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2187
  apply (simp add: mod_pos_pos_trivial)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2188
  apply (subst mod_pos_pos_trivial)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2189
    apply (rule bl_to_bin_ge0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2190
   apply (rule order_less_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2191
    apply (rule bl_to_bin_lt2p)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2192
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2193
  apply (rule bl_to_bin_lt2p)    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2194
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2195
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2196
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2197
subsection "Cardinality, finiteness of set of words"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2198
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2199
lemmas card_lessThan' = card_lessThan [unfolded lessThan_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2200
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2201
lemmas card_eq = word_unat.Abs_inj_on [THEN card_image,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2202
  unfolded word_unat.image, unfolded unats_def, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2203
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2204
lemmas card_word = trans [OF card_eq card_lessThan', standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2205
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2206
lemma finite_word_UNIV: "finite (UNIV :: 'a :: len word set)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2207
apply (rule contrapos_np)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2208
 prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2209
 apply (erule card_infinite)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2210
apply (simp add: card_word)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2211
done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2212
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2213
lemma card_word_size: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2214
  "card (UNIV :: 'a :: len word set) = (2 ^ size (x :: 'a word))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2215
unfolding word_size by (rule card_word)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2216
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2217
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2218
subsection {* Bitwise Operations on Words *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2219
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2220
lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2221
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2222
(* following definitions require both arithmetic and bit-wise word operations *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2223
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2224
(* to get word_no_log_defs from word_log_defs, using bin_log_bintrs *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2225
lemmas wils1 = bin_log_bintrs [THEN word_ubin.norm_eq_iff [THEN iffD1],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2226
  folded word_ubin.eq_norm, THEN eq_reflection, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2227
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2228
(* the binary operations only *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2229
lemmas word_log_binary_defs = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2230
  word_and_def word_or_def word_xor_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2231
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2232
lemmas word_no_log_defs [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2233
  word_not_def  [where a="number_of a", 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2234
                 unfolded word_no_wi wils1, folded word_no_wi, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2235
  word_log_binary_defs [where a="number_of a" and b="number_of b",
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2236
                        unfolded word_no_wi wils1, folded word_no_wi, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2237
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2238
lemmas word_wi_log_defs = word_no_log_defs [unfolded word_no_wi]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2239
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2240
lemma uint_or: "uint (x OR y) = (uint x) OR (uint y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2241
  by (simp add: word_or_def word_no_wi [symmetric] number_of_is_id
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2242
                bin_trunc_ao(2) [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2243
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2244
lemma uint_and: "uint (x AND y) = (uint x) AND (uint y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2245
  by (simp add: word_and_def number_of_is_id word_no_wi [symmetric]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2246
                bin_trunc_ao(1) [symmetric]) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2247
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2248
lemma word_ops_nth_size:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2249
  "n < size (x::'a::len0 word) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2250
    (x OR y) !! n = (x !! n | y !! n) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2251
    (x AND y) !! n = (x !! n & y !! n) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2252
    (x XOR y) !! n = (x !! n ~= y !! n) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2253
    (NOT x) !! n = (~ x !! n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2254
  unfolding word_size word_no_wi word_test_bit_def word_log_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2255
  by (clarsimp simp add : word_ubin.eq_norm nth_bintr bin_nth_ops)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2256
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2257
lemma word_ao_nth:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2258
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2259
  shows "(x OR y) !! n = (x !! n | y !! n) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2260
         (x AND y) !! n = (x !! n & y !! n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2261
  apply (cases "n < size x")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2262
   apply (drule_tac y = "y" in word_ops_nth_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2263
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2264
  apply (simp add : test_bit_bin word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2265
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2266
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2267
(* get from commutativity, associativity etc of int_and etc
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2268
  to same for word_and etc *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2269
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2270
lemmas bwsimps = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2271
  word_of_int_homs(2) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2272
  word_0_wi_Pls
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2273
  word_m1_wi_Min
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2274
  word_wi_log_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2275
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2276
lemma word_bw_assocs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2277
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2278
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2279
  "(x AND y) AND z = x AND y AND z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2280
  "(x OR y) OR z = x OR y OR z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2281
  "(x XOR y) XOR z = x XOR y XOR z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2282
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2283
        word_of_int_Ex [where x=y] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2284
        word_of_int_Ex [where x=z]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2285
  by (auto simp: bwsimps bbw_assocs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2286
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2287
lemma word_bw_comms:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2288
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2289
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2290
  "x AND y = y AND x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2291
  "x OR y = y OR x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2292
  "x XOR y = y XOR x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2293
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2294
        word_of_int_Ex [where x=y] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2295
  by (auto simp: bwsimps bin_ops_comm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2296
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2297
lemma word_bw_lcs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2298
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2299
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2300
  "y AND x AND z = x AND y AND z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2301
  "y OR x OR z = x OR y OR z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2302
  "y XOR x XOR z = x XOR y XOR z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2303
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2304
        word_of_int_Ex [where x=y] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2305
        word_of_int_Ex [where x=z]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2306
  by (auto simp: bwsimps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2307
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2308
lemma word_log_esimps [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2309
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2310
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2311
  "x AND 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2312
  "x AND -1 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2313
  "x OR 0 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2314
  "x OR -1 = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2315
  "x XOR 0 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2316
  "x XOR -1 = NOT x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2317
  "0 AND x = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2318
  "-1 AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2319
  "0 OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2320
  "-1 OR x = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2321
  "0 XOR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2322
  "-1 XOR x = NOT x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2323
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2324
  by (auto simp: bwsimps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2325
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2326
lemma word_not_dist:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2327
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2328
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2329
  "NOT (x OR y) = NOT x AND NOT y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2330
  "NOT (x AND y) = NOT x OR NOT y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2331
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2332
        word_of_int_Ex [where x=y] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2333
  by (auto simp: bwsimps bbw_not_dist)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2334
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2335
lemma word_bw_same:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2336
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2337
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2338
  "x AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2339
  "x OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2340
  "x XOR x = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2341
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2342
  by (auto simp: bwsimps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2343
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2344
lemma word_ao_absorbs [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2345
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2346
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2347
  "x AND (y OR x) = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2348
  "x OR y AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2349
  "x AND (x OR y) = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2350
  "y AND x OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2351
  "(y OR x) AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2352
  "x OR x AND y = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2353
  "(x OR y) AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2354
  "x AND y OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2355
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2356
        word_of_int_Ex [where x=y] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2357
  by (auto simp: bwsimps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2358
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2359
lemma word_not_not [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2360
  "NOT NOT (x::'a::len0 word) = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2361
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2362
  by (auto simp: bwsimps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2363
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2364
lemma word_ao_dist:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2365
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2366
  shows "(x OR y) AND z = x AND z OR y AND z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2367
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2368
        word_of_int_Ex [where x=y] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2369
        word_of_int_Ex [where x=z]   
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2370
  by (auto simp: bwsimps bbw_ao_dist simp del: bin_ops_comm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2371
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2372
lemma word_oa_dist:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2373
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2374
  shows "x AND y OR z = (x OR z) AND (y OR z)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2375
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2376
        word_of_int_Ex [where x=y] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2377
        word_of_int_Ex [where x=z]   
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2378
  by (auto simp: bwsimps bbw_oa_dist simp del: bin_ops_comm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2379
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2380
lemma word_add_not [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2381
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2382
  shows "x + NOT x = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2383
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2384
  by (auto simp: bwsimps bin_add_not)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2385
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2386
lemma word_plus_and_or [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2387
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2388
  shows "(x AND y) + (x OR y) = x + y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2389
  using word_of_int_Ex [where x=x] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2390
        word_of_int_Ex [where x=y] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2391
  by (auto simp: bwsimps plus_and_or)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2392
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2393
lemma leoa:   
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2394
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2395
  shows "(w = (x OR y)) ==> (y = (w AND y))" by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2396
lemma leao: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2397
  fixes x' :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2398
  shows "(w' = (x' AND y')) ==> (x' = (x' OR w'))" by auto 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2399
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2400
lemmas word_ao_equiv = leao [COMP leoa [COMP iffI]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2401
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2402
lemma le_word_or2: "x <= x OR (y::'a::len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2403
  unfolding word_le_def uint_or
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2404
  by (auto intro: le_int_or) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2405
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2406
lemmas le_word_or1 = xtr3 [OF word_bw_comms (2) le_word_or2, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2407
lemmas word_and_le1 =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2408
  xtr3 [OF word_ao_absorbs (4) [symmetric] le_word_or2, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2409
lemmas word_and_le2 =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2410
  xtr3 [OF word_ao_absorbs (8) [symmetric] le_word_or2, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2411
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2412
lemma bl_word_not: "to_bl (NOT w) = map Not (to_bl w)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2413
  unfolding to_bl_def word_log_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2414
  by (simp add: bl_not_bin number_of_is_id word_no_wi [symmetric] bin_to_bl_def[symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2415
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2416
lemma bl_word_xor: "to_bl (v XOR w) = map2 op ~= (to_bl v) (to_bl w)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2417
  unfolding to_bl_def word_log_defs bl_xor_bin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2418
  by (simp add: number_of_is_id word_no_wi [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2419
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2420
lemma bl_word_or: "to_bl (v OR w) = map2 op | (to_bl v) (to_bl w)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2421
  unfolding to_bl_def word_log_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2422
  by (simp add: bl_or_bin number_of_is_id word_no_wi [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2423
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2424
lemma bl_word_and: "to_bl (v AND w) = map2 op & (to_bl v) (to_bl w)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2425
  unfolding to_bl_def word_log_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2426
  by (simp add: bl_and_bin number_of_is_id word_no_wi [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2427
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2428
lemma word_lsb_alt: "lsb (w::'a::len0 word) = test_bit w 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2429
  by (auto simp: word_test_bit_def word_lsb_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2430
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2431
lemma word_lsb_1_0: "lsb (1::'a::len word) & ~ lsb (0::'b::len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2432
  unfolding word_lsb_def word_1_no word_0_no by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2433
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2434
lemma word_lsb_last: "lsb (w::'a::len word) = last (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2435
  apply (unfold word_lsb_def uint_bl bin_to_bl_def) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2436
  apply (rule_tac bin="uint w" in bin_exhaust)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2437
  apply (cases "size w")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2438
   apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2439
   apply (auto simp add: bin_to_bl_aux_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2440
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2441
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2442
lemma word_lsb_int: "lsb w = (uint w mod 2 = 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2443
  unfolding word_lsb_def bin_last_mod by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2444
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2445
lemma word_msb_sint: "msb w = (sint w < 0)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2446
  unfolding word_msb_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2447
  by (simp add : sign_Min_lt_0 number_of_is_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2448
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2449
lemma word_msb_no': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2450
  "w = number_of bin ==> msb (w::'a::len word) = bin_nth bin (size w - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2451
  unfolding word_msb_def word_number_of_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2452
  by (clarsimp simp add: word_sbin.eq_norm word_size bin_sign_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2453
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2454
lemmas word_msb_no = refl [THEN word_msb_no', unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2455
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2456
lemma word_msb_nth': "msb (w::'a::len word) = bin_nth (uint w) (size w - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2457
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2458
  apply (rule trans [OF _ word_msb_no])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2459
  apply (simp add : word_number_of_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2460
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2461
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2462
lemmas word_msb_nth = word_msb_nth' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2463
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2464
lemma word_msb_alt: "msb (w::'a::len word) = hd (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2465
  apply (unfold word_msb_nth uint_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2466
  apply (subst hd_conv_nth)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2467
  apply (rule length_greater_0_conv [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2468
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2469
  apply (simp add : nth_bin_to_bl word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2470
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2471
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2472
lemma word_set_nth:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2473
  "set_bit w n (test_bit w n) = (w::'a::len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2474
  unfolding word_test_bit_def word_set_bit_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2475
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2476
lemma bin_nth_uint':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2477
  "bin_nth (uint w) n = (rev (bin_to_bl (size w) (uint w)) ! n & n < size w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2478
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2479
  apply (safe elim!: bin_nth_uint_imp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2480
   apply (frule bin_nth_uint_imp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2481
   apply (fast dest!: bin_nth_bl)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2482
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2483
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2484
lemmas bin_nth_uint = bin_nth_uint' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2485
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2486
lemma test_bit_bl: "w !! n = (rev (to_bl w) ! n & n < size w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2487
  unfolding to_bl_def word_test_bit_def word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2488
  by (rule bin_nth_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2489
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2490
lemma to_bl_nth: "n < size w ==> to_bl w ! n = w !! (size w - Suc n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2491
  apply (unfold test_bit_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2492
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2493
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2494
   apply (rule nth_rev_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2495
   apply (auto simp add: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2496
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2497
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2498
lemma test_bit_set: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2499
  fixes w :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2500
  shows "(set_bit w n x) !! n = (n < size w & x)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2501
  unfolding word_size word_test_bit_def word_set_bit_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2502
  by (clarsimp simp add : word_ubin.eq_norm nth_bintr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2503
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2504
lemma test_bit_set_gen: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2505
  fixes w :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2506
  shows "test_bit (set_bit w n x) m = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2507
         (if m = n then n < size w & x else test_bit w m)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2508
  apply (unfold word_size word_test_bit_def word_set_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2509
  apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2510
  apply (auto elim!: test_bit_size [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2511
              simp add: word_test_bit_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2512
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2513
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2514
lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2515
  unfolding of_bl_def bl_to_bin_rep_F by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2516
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2517
lemma msb_nth':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2518
  fixes w :: "'a::len word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2519
  shows "msb w = w !! (size w - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2520
  unfolding word_msb_nth' word_test_bit_def by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2521
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2522
lemmas msb_nth = msb_nth' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2523
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2524
lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2525
  word_ops_nth_size [unfolded word_size], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2526
lemmas msb1 = msb0 [where i = 0]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2527
lemmas word_ops_msb = msb1 [unfolded msb_nth [symmetric, unfolded One_nat_def]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2528
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2529
lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2530
lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2531
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2532
lemma td_ext_nth':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2533
  "n = size (w::'a::len0 word) ==> ofn = set_bits ==> [w, ofn g] = l ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2534
    td_ext test_bit ofn {f. ALL i. f i --> i < n} (%h i. h i & i < n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2535
  apply (unfold word_size td_ext_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2536
  apply (safe del: subset_antisym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2537
     apply (rule_tac [3] ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2538
     apply (rule_tac [4] ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2539
     apply (unfold word_size of_nth_def test_bit_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2540
     apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2541
       defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2542
       apply (clarsimp simp: word_bl.Abs_inverse)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2543
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2544
  apply (rule sym [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2545
  apply (rule bl_of_nth_nth)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2546
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2547
  apply (rule bl_of_nth_inj)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2548
  apply (clarsimp simp add : test_bit_bl word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2549
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2550
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2551
lemmas td_ext_nth = td_ext_nth' [OF refl refl refl, unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2552
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2553
interpretation test_bit:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2554
  td_ext "op !! :: 'a::len0 word => nat => bool"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2555
         set_bits
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2556
         "{f. \<forall>i. f i \<longrightarrow> i < len_of TYPE('a::len0)}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2557
         "(\<lambda>h i. h i \<and> i < len_of TYPE('a::len0))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2558
  by (rule td_ext_nth)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2559
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2560
declare test_bit.Rep' [simp del]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2561
declare test_bit.Rep' [rule del]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2562
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2563
lemmas td_nth = test_bit.td_thm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2564
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2565
lemma word_set_set_same: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2566
  fixes w :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2567
  shows "set_bit (set_bit w n x) n y = set_bit w n y" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2568
  by (rule word_eqI) (simp add : test_bit_set_gen word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2569
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2570
lemma word_set_set_diff: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2571
  fixes w :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2572
  assumes "m ~= n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2573
  shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2574
  by (rule word_eqI) (clarsimp simp add : test_bit_set_gen word_size prems)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2575
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2576
lemma test_bit_no': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2577
  fixes w :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2578
  shows "w = number_of bin ==> test_bit w n = (n < size w & bin_nth bin n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2579
  unfolding word_test_bit_def word_number_of_def word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2580
  by (simp add : nth_bintr [symmetric] word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2581
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2582
lemmas test_bit_no = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2583
  refl [THEN test_bit_no', unfolded word_size, THEN eq_reflection, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2584
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2585
lemma nth_0: "~ (0::'a::len0 word) !! n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2586
  unfolding test_bit_no word_0_no by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2587
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2588
lemma nth_sint: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2589
  fixes w :: "'a::len word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2590
  defines "l \<equiv> len_of TYPE ('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2591
  shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2592
  unfolding sint_uint l_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2593
  by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2594
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2595
lemma word_lsb_no: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2596
  "lsb (number_of bin :: 'a :: len word) = (bin_last bin = 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2597
  unfolding word_lsb_alt test_bit_no by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2598
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2599
lemma word_set_no: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2600
  "set_bit (number_of bin::'a::len0 word) n b = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2601
    number_of (bin_sc n (if b then 1 else 0) bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2602
  apply (unfold word_set_bit_def word_number_of_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2603
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2604
  apply (clarsimp simp: word_size bin_nth_sc_gen number_of_is_id 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2605
                        test_bit_no nth_bintr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2606
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2607
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2608
lemmas setBit_no = setBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2609
  simplified if_simps, THEN eq_reflection, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2610
lemmas clearBit_no = clearBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2611
  simplified if_simps, THEN eq_reflection, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2612
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2613
lemma to_bl_n1: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2614
  "to_bl (-1::'a::len0 word) = replicate (len_of TYPE ('a)) True"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2615
  apply (rule word_bl.Abs_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2616
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2617
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2618
  apply (clarsimp simp add: word_size test_bit_no)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2619
  apply (auto simp add: word_bl.Abs_inverse test_bit_bl word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2620
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2621
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2622
lemma word_msb_n1: "msb (-1::'a::len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2623
  unfolding word_msb_alt word_msb_alt to_bl_n1 by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2624
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2625
declare word_set_set_same [simp] word_set_nth [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2626
  test_bit_no [simp] word_set_no [simp] nth_0 [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2627
  setBit_no [simp] clearBit_no [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2628
  word_lsb_no [simp] word_msb_no [simp] word_msb_n1 [simp] word_lsb_1_0 [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2629
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2630
lemma word_set_nth_iff: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2631
  "(set_bit w n b = w) = (w !! n = b | n >= size (w::'a::len0 word))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2632
  apply (rule iffI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2633
   apply (rule disjCI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2634
   apply (drule word_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2635
   apply (erule sym [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2636
   apply (simp add: test_bit_set)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2637
  apply (erule disjE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2638
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2639
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2640
  apply (clarsimp simp add : test_bit_set_gen)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2641
  apply (drule test_bit_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2642
  apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2643
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2644
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2645
lemma test_bit_2p': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2646
  "w = word_of_int (2 ^ n) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2647
    w !! m = (m = n & m < size (w :: 'a :: len word))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2648
  unfolding word_test_bit_def word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2649
  by (auto simp add: word_ubin.eq_norm nth_bintr nth_2p_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2650
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2651
lemmas test_bit_2p = refl [THEN test_bit_2p', unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2652
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2653
lemma nth_w2p:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2654
  "((2\<Colon>'a\<Colon>len word) ^ n) !! m \<longleftrightarrow> m = n \<and> m < len_of TYPE('a\<Colon>len)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2655
  unfolding test_bit_2p [symmetric] word_of_int [symmetric]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2656
  by (simp add:  of_int_power)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2657
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2658
lemma uint_2p: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2659
  "(0::'a::len word) < 2 ^ n ==> uint (2 ^ n::'a::len word) = 2 ^ n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2660
  apply (unfold word_arith_power_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2661
  apply (case_tac "len_of TYPE ('a)")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2662
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2663
  apply (case_tac "nat")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2664
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2665
   apply (case_tac "n")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2666
    apply (clarsimp simp add : word_1_wi [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2667
   apply (clarsimp simp add : word_0_wi [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2668
  apply (drule word_gt_0 [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2669
  apply (safe intro!: word_eqI bin_nth_lem ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2670
     apply (auto simp add: test_bit_2p nth_2p_bin word_test_bit_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2671
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2672
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2673
lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a :: len word) = 2 ^ n" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2674
  apply (unfold word_arith_power_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2675
  apply (case_tac "len_of TYPE ('a)")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2676
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2677
  apply (case_tac "nat")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2678
   apply (rule word_ubin.norm_eq_iff [THEN iffD1]) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2679
   apply (rule box_equals) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2680
     apply (rule_tac [2] bintr_ariths (1))+ 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2681
   apply (clarsimp simp add : number_of_is_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2682
  apply simp 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2683
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2684
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2685
lemma bang_is_le: "x !! m ==> 2 ^ m <= (x :: 'a :: len word)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2686
  apply (rule xtr3) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2687
  apply (rule_tac [2] y = "x" in le_word_or2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2688
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2689
  apply (auto simp add: word_ao_nth nth_w2p word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2690
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2691
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2692
lemma word_clr_le: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2693
  fixes w :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2694
  shows "w >= set_bit w n False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2695
  apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2696
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2697
  apply (rule order_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2698
   apply (rule bintr_bin_clr_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2699
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2700
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2701
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2702
lemma word_set_ge: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2703
  fixes w :: "'a::len word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2704
  shows "w <= set_bit w n True"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2705
  apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2706
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2707
  apply (rule order_trans [OF _ bintr_bin_set_ge])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2708
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2709
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2710
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2711
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2712
subsection {* Shifting, Rotating, and Splitting Words *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2713
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2714
lemma shiftl1_number [simp] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2715
  "shiftl1 (number_of w) = number_of (w BIT 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2716
  apply (unfold shiftl1_def word_number_of_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2717
  apply (simp add: word_ubin.norm_eq_iff [symmetric] word_ubin.eq_norm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2718
              del: BIT_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2719
  apply (subst refl [THEN bintrunc_BIT_I, symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2720
  apply (subst bintrunc_bintrunc_min)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2721
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2722
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2723
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2724
lemma shiftl1_0 [simp] : "shiftl1 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2725
  unfolding word_0_no shiftl1_number by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2726
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2727
lemmas shiftl1_def_u = shiftl1_def [folded word_number_of_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2728
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2729
lemma shiftl1_def_s: "shiftl1 w = number_of (sint w BIT 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2730
  by (rule trans [OF _ shiftl1_number]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2731
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2732
lemma shiftr1_0 [simp] : "shiftr1 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2733
  unfolding shiftr1_def 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2734
  by simp (simp add: word_0_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2735
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2736
lemma sshiftr1_0 [simp] : "sshiftr1 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2737
  apply (unfold sshiftr1_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2738
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2739
  apply (simp add : word_0_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2740
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2741
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2742
lemma sshiftr1_n1 [simp] : "sshiftr1 -1 = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2743
  unfolding sshiftr1_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2744
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2745
lemma shiftl_0 [simp] : "(0::'a::len0 word) << n = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2746
  unfolding shiftl_def by (induct n) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2747
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2748
lemma shiftr_0 [simp] : "(0::'a::len0 word) >> n = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2749
  unfolding shiftr_def by (induct n) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2750
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2751
lemma sshiftr_0 [simp] : "0 >>> n = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2752
  unfolding sshiftr_def by (induct n) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2753
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2754
lemma sshiftr_n1 [simp] : "-1 >>> n = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2755
  unfolding sshiftr_def by (induct n) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2756
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2757
lemma nth_shiftl1: "shiftl1 w !! n = (n < size w & n > 0 & w !! (n - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2758
  apply (unfold shiftl1_def word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2759
  apply (simp add: nth_bintr word_ubin.eq_norm word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2760
  apply (cases n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2761
   apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2762
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2763
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2764
lemma nth_shiftl' [rule_format]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2765
  "ALL n. ((w::'a::len0 word) << m) !! n = (n < size w & n >= m & w !! (n - m))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2766
  apply (unfold shiftl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2767
  apply (induct "m")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2768
   apply (force elim!: test_bit_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2769
  apply (clarsimp simp add : nth_shiftl1 word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2770
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2771
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2772
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2773
lemmas nth_shiftl = nth_shiftl' [unfolded word_size] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2774
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2775
lemma nth_shiftr1: "shiftr1 w !! n = w !! Suc n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2776
  apply (unfold shiftr1_def word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2777
  apply (simp add: nth_bintr word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2778
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2779
  apply (drule bin_nth.Suc [THEN iffD2, THEN bin_nth_uint_imp])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2780
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2781
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2782
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2783
lemma nth_shiftr: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2784
  "\<And>n. ((w::'a::len0 word) >> m) !! n = w !! (n + m)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2785
  apply (unfold shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2786
  apply (induct "m")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2787
   apply (auto simp add : nth_shiftr1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2788
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2789
   
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2790
(* see paper page 10, (1), (2), shiftr1_def is of the form of (1),
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2791
  where f (ie bin_rest) takes normal arguments to normal results,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2792
  thus we get (2) from (1) *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2793
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2794
lemma uint_shiftr1: "uint (shiftr1 w) = bin_rest (uint w)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2795
  apply (unfold shiftr1_def word_ubin.eq_norm bin_rest_trunc_i)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2796
  apply (subst bintr_uint [symmetric, OF order_refl])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2797
  apply (simp only : bintrunc_bintrunc_l)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2798
  apply simp 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2799
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2800
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2801
lemma nth_sshiftr1: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2802
  "sshiftr1 w !! n = (if n = size w - 1 then w !! n else w !! Suc n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2803
  apply (unfold sshiftr1_def word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2804
  apply (simp add: nth_bintr word_ubin.eq_norm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2805
                   bin_nth.Suc [symmetric] word_size 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2806
             del: bin_nth.simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2807
  apply (simp add: nth_bintr uint_sint del : bin_nth.simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2808
  apply (auto simp add: bin_nth_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2809
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2810
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2811
lemma nth_sshiftr [rule_format] : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2812
  "ALL n. sshiftr w m !! n = (n < size w & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2813
    (if n + m >= size w then w !! (size w - 1) else w !! (n + m)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2814
  apply (unfold sshiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2815
  apply (induct_tac "m")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2816
   apply (simp add: test_bit_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2817
  apply (clarsimp simp add: nth_sshiftr1 word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2818
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2819
       apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2820
      apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2821
     apply (erule thin_rl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2822
     apply (case_tac n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2823
      apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2824
      apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2825
     apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2826
    apply (erule thin_rl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2827
    apply (case_tac n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2828
     apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2829
     apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2830
    apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2831
   apply arith+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2832
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2833
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2834
lemma shiftr1_div_2: "uint (shiftr1 w) = uint w div 2"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2835
  apply (unfold shiftr1_def bin_rest_div)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2836
  apply (rule word_uint.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2837
  apply (simp add: uints_num pos_imp_zdiv_nonneg_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2838
  apply (rule xtr7)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2839
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2840
   apply (rule zdiv_le_dividend)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2841
    apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2842
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2843
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2844
lemma sshiftr1_div_2: "sint (sshiftr1 w) = sint w div 2"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2845
  apply (unfold sshiftr1_def bin_rest_div [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2846
  apply (simp add: word_sbin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2847
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2848
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2849
   apply (subst word_sbin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2850
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2851
  apply (subst word_sbin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2852
  apply (unfold One_nat_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2853
  apply (rule sbintrunc_rest)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2854
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2855
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2856
lemma shiftr_div_2n: "uint (shiftr w n) = uint w div 2 ^ n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2857
  apply (unfold shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2858
  apply (induct "n")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2859
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2860
  apply (simp add: shiftr1_div_2 mult_commute
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2861
                   zdiv_zmult2_eq [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2862
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2863
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2864
lemma sshiftr_div_2n: "sint (sshiftr w n) = sint w div 2 ^ n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2865
  apply (unfold sshiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2866
  apply (induct "n")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2867
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2868
  apply (simp add: sshiftr1_div_2 mult_commute
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2869
                   zdiv_zmult2_eq [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2870
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2871
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2872
subsubsection "shift functions in terms of lists of bools"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2873
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2874
lemmas bshiftr1_no_bin [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2875
  bshiftr1_def [where w="number_of w", unfolded to_bl_no_bin, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2876
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2877
lemma bshiftr1_bl: "to_bl (bshiftr1 b w) = b # butlast (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2878
  unfolding bshiftr1_def by (rule word_bl.Abs_inverse) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2879
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2880
lemma shiftl1_of_bl: "shiftl1 (of_bl bl) = of_bl (bl @ [False])"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2881
  unfolding uint_bl of_bl_no 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2882
  by (simp add: bl_to_bin_aux_append bl_to_bin_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2883
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2884
lemma shiftl1_bl: "shiftl1 (w::'a::len0 word) = of_bl (to_bl w @ [False])"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2885
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2886
  have "shiftl1 w = shiftl1 (of_bl (to_bl w))" by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2887
  also have "\<dots> = of_bl (to_bl w @ [False])" by (rule shiftl1_of_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2888
  finally show ?thesis .
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2889
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2890
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2891
lemma bl_shiftl1:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2892
  "to_bl (shiftl1 (w :: 'a :: len word)) = tl (to_bl w) @ [False]"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2893
  apply (simp add: shiftl1_bl word_rep_drop drop_Suc drop_Cons')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2894
  apply (fast intro!: Suc_leI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2895
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2896
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2897
lemma shiftr1_bl: "shiftr1 w = of_bl (butlast (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2898
  apply (unfold shiftr1_def uint_bl of_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2899
  apply (simp add: butlast_rest_bin word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2900
  apply (simp add: bin_rest_trunc [symmetric, unfolded One_nat_def])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2901
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2902
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2903
lemma bl_shiftr1: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2904
  "to_bl (shiftr1 (w :: 'a :: len word)) = False # butlast (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2905
  unfolding shiftr1_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2906
  by (simp add : word_rep_drop len_gt_0 [THEN Suc_leI])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2907
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2908
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2909
(* relate the two above : TODO - remove the :: len restriction on
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2910
  this theorem and others depending on it *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2911
lemma shiftl1_rev: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2912
  "shiftl1 (w :: 'a :: len word) = word_reverse (shiftr1 (word_reverse w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2913
  apply (unfold word_reverse_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2914
  apply (rule word_bl.Rep_inverse' [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2915
  apply (simp add: bl_shiftl1 bl_shiftr1 word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2916
  apply (cases "to_bl w")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2917
   apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2918
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2919
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2920
lemma shiftl_rev: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2921
  "shiftl (w :: 'a :: len word) n = word_reverse (shiftr (word_reverse w) n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2922
  apply (unfold shiftl_def shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2923
  apply (induct "n")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2924
   apply (auto simp add : shiftl1_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2925
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2926
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2927
lemmas rev_shiftl =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2928
  shiftl_rev [where w = "word_reverse w", simplified, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2929
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2930
lemmas shiftr_rev = rev_shiftl [THEN word_rev_gal', standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2931
lemmas rev_shiftr = shiftl_rev [THEN word_rev_gal', standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2932
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2933
lemma bl_sshiftr1:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2934
  "to_bl (sshiftr1 (w :: 'a :: len word)) = hd (to_bl w) # butlast (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2935
  apply (unfold sshiftr1_def uint_bl word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2936
  apply (simp add: butlast_rest_bin word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2937
  apply (simp add: sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2938
  apply (rule nth_equalityI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2939
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2940
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2941
  apply (case_tac i)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2942
   apply (simp_all add: hd_conv_nth length_0_conv [symmetric]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2943
                        nth_bin_to_bl bin_nth.Suc [symmetric] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2944
                        nth_sbintr 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2945
                   del: bin_nth.Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2946
   apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2947
  apply (rule impI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2948
  apply (rule_tac f = "bin_nth (uint w)" in arg_cong)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2949
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2950
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2951
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2952
lemma drop_shiftr: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2953
  "drop n (to_bl ((w :: 'a :: len word) >> n)) = take (size w - n) (to_bl w)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2954
  apply (unfold shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2955
  apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2956
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2957
   apply (simp add: drop_Suc bl_shiftr1 butlast_drop [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2958
   apply (rule butlast_take [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2959
  apply (auto simp: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2960
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2961
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2962
lemma drop_sshiftr: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2963
  "drop n (to_bl ((w :: 'a :: len word) >>> n)) = take (size w - n) (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2964
  apply (unfold sshiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2965
  apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2966
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2967
   apply (simp add: drop_Suc bl_sshiftr1 butlast_drop [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2968
   apply (rule butlast_take [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2969
  apply (auto simp: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2970
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2971
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2972
lemma take_shiftr [rule_format] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2973
  "n <= size (w :: 'a :: len word) --> take n (to_bl (w >> n)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2974
    replicate n False" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2975
  apply (unfold shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2976
  apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2977
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2978
   apply (simp add: bl_shiftr1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2979
   apply (rule impI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2980
   apply (rule take_butlast [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2981
  apply (auto simp: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2982
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2983
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2984
lemma take_sshiftr' [rule_format] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2985
  "n <= size (w :: 'a :: len word) --> hd (to_bl (w >>> n)) = hd (to_bl w) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2986
    take n (to_bl (w >>> n)) = replicate n (hd (to_bl w))" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2987
  apply (unfold sshiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2988
  apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2989
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2990
   apply (simp add: bl_sshiftr1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2991
   apply (rule impI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2992
   apply (rule take_butlast [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2993
  apply (auto simp: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2994
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2995
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2996
lemmas hd_sshiftr = take_sshiftr' [THEN conjunct1, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2997
lemmas take_sshiftr = take_sshiftr' [THEN conjunct2, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2998
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2999
lemma atd_lem: "take n xs = t ==> drop n xs = d ==> xs = t @ d"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3000
  by (auto intro: append_take_drop_id [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3001
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3002
lemmas bl_shiftr = atd_lem [OF take_shiftr drop_shiftr]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3003
lemmas bl_sshiftr = atd_lem [OF take_sshiftr drop_sshiftr]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3004
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3005
lemma shiftl_of_bl: "of_bl bl << n = of_bl (bl @ replicate n False)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3006
  unfolding shiftl_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3007
  by (induct n) (auto simp: shiftl1_of_bl replicate_app_Cons_same)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3008
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3009
lemma shiftl_bl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3010
  "(w::'a::len0 word) << (n::nat) = of_bl (to_bl w @ replicate n False)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3011
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3012
  have "w << n = of_bl (to_bl w) << n" by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3013
  also have "\<dots> = of_bl (to_bl w @ replicate n False)" by (rule shiftl_of_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3014
  finally show ?thesis .
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3015
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3016
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3017
lemmas shiftl_number [simp] = shiftl_def [where w="number_of w", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3018
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3019
lemma bl_shiftl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3020
  "to_bl (w << n) = drop n (to_bl w) @ replicate (min (size w) n) False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3021
  by (simp add: shiftl_bl word_rep_drop word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3022
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3023
lemma shiftl_zero_size: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3024
  fixes x :: "'a::len0 word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3025
  shows "size x <= n ==> x << n = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3026
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3027
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3028
  apply (clarsimp simp add: shiftl_bl word_size test_bit_of_bl nth_append)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3029
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3030
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3031
(* note - the following results use 'a :: len word < number_ring *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3032
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3033
lemma shiftl1_2t: "shiftl1 (w :: 'a :: len word) = 2 * w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3034
  apply (simp add: shiftl1_def_u)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3035
  apply (simp only:  double_number_of_Bit0 [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3036
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3037
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3038
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3039
lemma shiftl1_p: "shiftl1 (w :: 'a :: len word) = w + w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3040
  apply (simp add: shiftl1_def_u)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3041
  apply (simp only: double_number_of_Bit0 [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3042
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3043
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3044
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3045
lemma shiftl_t2n: "shiftl (w :: 'a :: len word) n = 2 ^ n * w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3046
  unfolding shiftl_def 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3047
  by (induct n) (auto simp: shiftl1_2t power_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3048
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3049
lemma shiftr1_bintr [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3050
  "(shiftr1 (number_of w) :: 'a :: len0 word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3051
    number_of (bin_rest (bintrunc (len_of TYPE ('a)) w))" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3052
  unfolding shiftr1_def word_number_of_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3053
  by (simp add : word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3054
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3055
lemma sshiftr1_sbintr [simp] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3056
  "(sshiftr1 (number_of w) :: 'a :: len word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3057
    number_of (bin_rest (sbintrunc (len_of TYPE ('a) - 1) w))" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3058
  unfolding sshiftr1_def word_number_of_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3059
  by (simp add : word_sbin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3060
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3061
lemma shiftr_no': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3062
  "w = number_of bin ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3063
  (w::'a::len0 word) >> n = number_of ((bin_rest ^^ n) (bintrunc (size w) bin))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3064
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3065
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3066
  apply (auto simp: nth_shiftr nth_rest_power_bin nth_bintr word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3067
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3068
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3069
lemma sshiftr_no': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3070
  "w = number_of bin ==> w >>> n = number_of ((bin_rest ^^ n) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3071
    (sbintrunc (size w - 1) bin))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3072
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3073
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3074
  apply (auto simp: nth_sshiftr nth_rest_power_bin nth_sbintr word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3075
   apply (subgoal_tac "na + n = len_of TYPE('a) - Suc 0", simp, simp)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3076
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3077
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3078
lemmas sshiftr_no [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3079
  sshiftr_no' [where w = "number_of w", OF refl, unfolded word_size, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3080
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3081
lemmas shiftr_no [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3082
  shiftr_no' [where w = "number_of w", OF refl, unfolded word_size, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3083
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3084
lemma shiftr1_bl_of': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3085
  "us = shiftr1 (of_bl bl) ==> length bl <= size us ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3086
    us = of_bl (butlast bl)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3087
  by (clarsimp simp: shiftr1_def of_bl_def word_size butlast_rest_bl2bin 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3088
                     word_ubin.eq_norm trunc_bl2bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3089
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3090
lemmas shiftr1_bl_of = refl [THEN shiftr1_bl_of', unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3091
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3092
lemma shiftr_bl_of' [rule_format]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3093
  "us = of_bl bl >> n ==> length bl <= size us --> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3094
   us = of_bl (take (length bl - n) bl)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3095
  apply (unfold shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3096
  apply hypsubst
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3097
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3098
  apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3099
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3100
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3101
  apply (subst shiftr1_bl_of)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3102
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3103
  apply (simp add: butlast_take)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3104
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3105
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3106
lemmas shiftr_bl_of = refl [THEN shiftr_bl_of', unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3107
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3108
lemmas shiftr_bl = word_bl.Rep' [THEN eq_imp_le, THEN shiftr_bl_of,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3109
  simplified word_size, simplified, THEN eq_reflection, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3110
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3111
lemma msb_shift': "msb (w::'a::len word) <-> (w >> (size w - 1)) ~= 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3112
  apply (unfold shiftr_bl word_msb_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3113
  apply (simp add: word_size Suc_le_eq take_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3114
  apply (cases "hd (to_bl w)")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3115
   apply (auto simp: word_1_bl word_0_bl 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3116
                     of_bl_rep_False [where n=1 and bs="[]", simplified])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3117
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3118
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3119
lemmas msb_shift = msb_shift' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3120
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3121
lemma align_lem_or [rule_format] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3122
  "ALL x m. length x = n + m --> length y = n + m --> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3123
    drop m x = replicate n False --> take m y = replicate m False --> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3124
    map2 op | x y = take m x @ drop m y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3125
  apply (induct_tac y)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3126
   apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3127
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3128
  apply (case_tac x, force)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3129
  apply (case_tac m, auto)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3130
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3131
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3132
  apply (induct_tac list, auto)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3133
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3134
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3135
lemma align_lem_and [rule_format] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3136
  "ALL x m. length x = n + m --> length y = n + m --> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3137
    drop m x = replicate n False --> take m y = replicate m False --> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3138
    map2 op & x y = replicate (n + m) False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3139
  apply (induct_tac y)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3140
   apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3141
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3142
  apply (case_tac x, force)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3143
  apply (case_tac m, auto)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3144
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3145
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3146
  apply (induct_tac list, auto)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3147
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3148
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3149
lemma aligned_bl_add_size':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3150
  "size x - n = m ==> n <= size x ==> drop m (to_bl x) = replicate n False ==>
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3151
    take m (to_bl y) = replicate m False ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3152
    to_bl (x + y) = take m (to_bl x) @ drop m (to_bl y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3153
  apply (subgoal_tac "x AND y = 0")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3154
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3155
   apply (rule word_bl.Rep_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3156
   apply (simp add: bl_word_and to_bl_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3157
   apply (rule align_lem_and [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3158
       apply (simp_all add: word_size)[5]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3159
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3160
  apply (subst word_plus_and_or [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3161
  apply (simp add : bl_word_or)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3162
  apply (rule align_lem_or)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3163
     apply (simp_all add: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3164
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3165
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3166
lemmas aligned_bl_add_size = refl [THEN aligned_bl_add_size']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3167
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3168
subsubsection "Mask"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3169
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3170
lemma nth_mask': "m = mask n ==> test_bit m i = (i < n & i < size m)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3171
  apply (unfold mask_def test_bit_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3172
  apply (simp only: word_1_bl [symmetric] shiftl_of_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3173
  apply (clarsimp simp add: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3174
  apply (simp only: of_bl_no mask_lem number_of_succ add_diff_cancel2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3175
  apply (fold of_bl_no)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3176
  apply (simp add: word_1_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3177
  apply (rule test_bit_of_bl [THEN trans, unfolded test_bit_bl word_size])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3178
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3179
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3180
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3181
lemmas nth_mask [simp] = refl [THEN nth_mask']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3182
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3183
lemma mask_bl: "mask n = of_bl (replicate n True)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3184
  by (auto simp add : test_bit_of_bl word_size intro: word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3185
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3186
lemma mask_bin: "mask n = number_of (bintrunc n Int.Min)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3187
  by (auto simp add: nth_bintr word_size intro: word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3188
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3189
lemma and_mask_bintr: "w AND mask n = number_of (bintrunc n (uint w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3190
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3191
  apply (simp add: nth_bintr word_size word_ops_nth_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3192
  apply (auto simp add: test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3193
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3194
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3195
lemma and_mask_no: "number_of i AND mask n = number_of (bintrunc n i)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3196
  by (auto simp add : nth_bintr word_size word_ops_nth_size intro: word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3197
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3198
lemmas and_mask_wi = and_mask_no [unfolded word_number_of_def] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3199
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3200
lemma bl_and_mask':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3201
  "to_bl (w AND mask n :: 'a :: len word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3202
    replicate (len_of TYPE('a) - n) False @ 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3203
    drop (len_of TYPE('a) - n) (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3204
  apply (rule nth_equalityI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3205
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3206
  apply (clarsimp simp add: to_bl_nth word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3207
  apply (simp add: word_size word_ops_nth_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3208
  apply (auto simp add: word_size test_bit_bl nth_append nth_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3209
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3210
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3211
lemmas and_mask_mod_2p = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3212
  and_mask_bintr [unfolded word_number_of_alt no_bintr_alt]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3213
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3214
lemma and_mask_lt_2p: "uint (w AND mask n) < 2 ^ n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3215
  apply (simp add : and_mask_bintr no_bintr_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3216
  apply (rule xtr8)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3217
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3218
   apply (rule pos_mod_bound)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3219
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3220
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3221
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3222
lemmas eq_mod_iff = trans [symmetric, OF int_mod_lem eq_sym_conv]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3223
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3224
lemma mask_eq_iff: "(w AND mask n) = w <-> uint w < 2 ^ n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3225
  apply (simp add: and_mask_bintr word_number_of_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3226
  apply (simp add: word_ubin.inverse_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3227
  apply (simp add: eq_mod_iff bintrunc_mod2p min_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3228
  apply (fast intro!: lt2p_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3229
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3230
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3231
lemma and_mask_dvd: "2 ^ n dvd uint w = (w AND mask n = 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3232
  apply (simp add: dvd_eq_mod_eq_0 and_mask_mod_2p)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3233
  apply (simp add: word_uint.norm_eq_iff [symmetric] word_of_int_homs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3234
  apply (subst word_uint.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3235
  apply (simp only: bintrunc_bintrunc_min bintrunc_mod2p [symmetric] min_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3236
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3237
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3238
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3239
lemma and_mask_dvd_nat: "2 ^ n dvd unat w = (w AND mask n = 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3240
  apply (unfold unat_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3241
  apply (rule trans [OF _ and_mask_dvd])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3242
  apply (unfold dvd_def) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3243
  apply auto 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3244
  apply (drule uint_ge_0 [THEN nat_int.Abs_inverse' [simplified], symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3245
  apply (simp add : int_mult int_power)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3246
  apply (simp add : nat_mult_distrib nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3247
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3248
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3249
lemma word_2p_lem: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3250
  "n < size w ==> w < 2 ^ n = (uint (w :: 'a :: len word) < 2 ^ n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3251
  apply (unfold word_size word_less_alt word_number_of_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3252
  apply (clarsimp simp add: word_of_int_power_hom word_uint.eq_norm 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3253
                            int_mod_eq'
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3254
                  simp del: word_of_int_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3255
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3256
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3257
lemma less_mask_eq: "x < 2 ^ n ==> x AND mask n = (x :: 'a :: len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3258
  apply (unfold word_less_alt word_number_of_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3259
  apply (clarsimp simp add: and_mask_mod_2p word_of_int_power_hom 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3260
                            word_uint.eq_norm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3261
                  simp del: word_of_int_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3262
  apply (drule xtr8 [rotated])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3263
  apply (rule int_mod_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3264
  apply (auto simp add : mod_pos_pos_trivial)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3265
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3266
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3267
lemmas mask_eq_iff_w2p =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3268
  trans [OF mask_eq_iff word_2p_lem [symmetric], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3269
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3270
lemmas and_mask_less' = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3271
  iffD2 [OF word_2p_lem and_mask_lt_2p, simplified word_size, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3272
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3273
lemma and_mask_less_size: "n < size x ==> x AND mask n < 2^n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3274
  unfolding word_size by (erule and_mask_less')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3275
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3276
lemma word_mod_2p_is_mask':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3277
  "c = 2 ^ n ==> c > 0 ==> x mod c = (x :: 'a :: len word) AND mask n" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3278
  by (clarsimp simp add: word_mod_def uint_2p and_mask_mod_2p) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3279
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3280
lemmas word_mod_2p_is_mask = refl [THEN word_mod_2p_is_mask'] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3281
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3282
lemma mask_eqs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3283
  "(a AND mask n) + b AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3284
  "a + (b AND mask n) AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3285
  "(a AND mask n) - b AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3286
  "a - (b AND mask n) AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3287
  "a * (b AND mask n) AND mask n = a * b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3288
  "(b AND mask n) * a AND mask n = b * a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3289
  "(a AND mask n) + (b AND mask n) AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3290
  "(a AND mask n) - (b AND mask n) AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3291
  "(a AND mask n) * (b AND mask n) AND mask n = a * b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3292
  "- (a AND mask n) AND mask n = - a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3293
  "word_succ (a AND mask n) AND mask n = word_succ a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3294
  "word_pred (a AND mask n) AND mask n = word_pred a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3295
  using word_of_int_Ex [where x=a] word_of_int_Ex [where x=b]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3296
  by (auto simp: and_mask_wi bintr_ariths bintr_arith1s new_word_of_int_homs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3297
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3298
lemma mask_power_eq:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3299
  "(x AND mask n) ^ k AND mask n = x ^ k AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3300
  using word_of_int_Ex [where x=x]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3301
  by (clarsimp simp: and_mask_wi word_of_int_power_hom bintr_ariths)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3302
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3303
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3304
subsubsection "Revcast"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3305
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3306
lemmas revcast_def' = revcast_def [simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3307
lemmas revcast_def'' = revcast_def' [simplified word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3308
lemmas revcast_no_def [simp] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3309
  revcast_def' [where w="number_of w", unfolded word_size, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3310
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3311
lemma to_bl_revcast: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3312
  "to_bl (revcast w :: 'a :: len0 word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3313
   takefill False (len_of TYPE ('a)) (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3314
  apply (unfold revcast_def' word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3315
  apply (rule word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3316
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3317
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3318
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3319
lemma revcast_rev_ucast': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3320
  "cs = [rc, uc] ==> rc = revcast (word_reverse w) ==> uc = ucast w ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3321
    rc = word_reverse uc"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3322
  apply (unfold ucast_def revcast_def' Let_def word_reverse_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3323
  apply (clarsimp simp add : to_bl_of_bin takefill_bintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3324
  apply (simp add : word_bl.Abs_inverse word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3325
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3326
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3327
lemmas revcast_rev_ucast = revcast_rev_ucast' [OF refl refl refl]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3328
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3329
lemmas revcast_ucast = revcast_rev_ucast
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3330
  [where w = "word_reverse w", simplified word_rev_rev, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3331
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3332
lemmas ucast_revcast = revcast_rev_ucast [THEN word_rev_gal', standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3333
lemmas ucast_rev_revcast = revcast_ucast [THEN word_rev_gal', standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3334
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3335
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3336
-- "linking revcast and cast via shift"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3337
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3338
lemmas wsst_TYs = source_size target_size word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3339
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3340
lemma revcast_down_uu': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3341
  "rc = revcast ==> source_size rc = target_size rc + n ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3342
    rc (w :: 'a :: len word) = ucast (w >> n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3343
  apply (simp add: revcast_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3344
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3345
  apply (rule trans, rule ucast_down_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3346
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3347
   apply (rule trans, rule drop_shiftr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3348
   apply (auto simp: takefill_alt wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3349
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3350
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3351
lemma revcast_down_us': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3352
  "rc = revcast ==> source_size rc = target_size rc + n ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3353
    rc (w :: 'a :: len word) = ucast (w >>> n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3354
  apply (simp add: revcast_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3355
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3356
  apply (rule trans, rule ucast_down_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3357
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3358
   apply (rule trans, rule drop_sshiftr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3359
   apply (auto simp: takefill_alt wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3360
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3361
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3362
lemma revcast_down_su': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3363
  "rc = revcast ==> source_size rc = target_size rc + n ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3364
    rc (w :: 'a :: len word) = scast (w >> n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3365
  apply (simp add: revcast_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3366
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3367
  apply (rule trans, rule scast_down_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3368
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3369
   apply (rule trans, rule drop_shiftr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3370
   apply (auto simp: takefill_alt wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3371
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3372
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3373
lemma revcast_down_ss': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3374
  "rc = revcast ==> source_size rc = target_size rc + n ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3375
    rc (w :: 'a :: len word) = scast (w >>> n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3376
  apply (simp add: revcast_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3377
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3378
  apply (rule trans, rule scast_down_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3379
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3380
   apply (rule trans, rule drop_sshiftr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3381
   apply (auto simp: takefill_alt wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3382
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3383
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3384
lemmas revcast_down_uu = refl [THEN revcast_down_uu']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3385
lemmas revcast_down_us = refl [THEN revcast_down_us']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3386
lemmas revcast_down_su = refl [THEN revcast_down_su']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3387
lemmas revcast_down_ss = refl [THEN revcast_down_ss']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3388
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3389
lemma cast_down_rev: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3390
  "uc = ucast ==> source_size uc = target_size uc + n ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3391
    uc w = revcast ((w :: 'a :: len word) << n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3392
  apply (unfold shiftl_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3393
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3394
  apply (simp add: revcast_rev_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3395
  apply (rule word_rev_gal')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3396
  apply (rule trans [OF _ revcast_rev_ucast])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3397
  apply (rule revcast_down_uu [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3398
  apply (auto simp add: wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3399
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3400
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3401
lemma revcast_up': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3402
  "rc = revcast ==> source_size rc + n = target_size rc ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3403
    rc w = (ucast w :: 'a :: len word) << n" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3404
  apply (simp add: revcast_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3405
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3406
  apply (simp add: takefill_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3407
  apply (rule bl_shiftl [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3408
  apply (subst ucast_up_app)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3409
  apply (auto simp add: wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3410
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3411
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3412
lemmas revcast_up = refl [THEN revcast_up']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3413
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3414
lemmas rc1 = revcast_up [THEN 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3415
  revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3416
lemmas rc2 = revcast_down_uu [THEN 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3417
  revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3418
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3419
lemmas ucast_up =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3420
 rc1 [simplified rev_shiftr [symmetric] revcast_ucast [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3421
lemmas ucast_down = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3422
  rc2 [simplified rev_shiftr revcast_ucast [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3423
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3424
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3425
subsubsection "Slices"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3426
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3427
lemmas slice1_no_bin [simp] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3428
  slice1_def [where w="number_of w", unfolded to_bl_no_bin, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3429
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3430
lemmas slice_no_bin [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3431
   trans [OF slice_def [THEN meta_eq_to_obj_eq] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3432
             slice1_no_bin [THEN meta_eq_to_obj_eq], 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3433
          unfolded word_size, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3434
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3435
lemma slice1_0 [simp] : "slice1 n 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3436
  unfolding slice1_def by (simp add : to_bl_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3437
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3438
lemma slice_0 [simp] : "slice n 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3439
  unfolding slice_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3440
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3441
lemma slice_take': "slice n w = of_bl (take (size w - n) (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3442
  unfolding slice_def' slice1_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3443
  by (simp add : takefill_alt word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3444
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3445
lemmas slice_take = slice_take' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3446
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3447
-- "shiftr to a word of the same size is just slice, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3448
    slice is just shiftr then ucast"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3449
lemmas shiftr_slice = trans
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3450
  [OF shiftr_bl [THEN meta_eq_to_obj_eq] slice_take [symmetric], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3451
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3452
lemma slice_shiftr: "slice n w = ucast (w >> n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3453
  apply (unfold slice_take shiftr_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3454
  apply (rule ucast_of_bl_up [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3455
  apply (simp add: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3456
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3457
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3458
lemma nth_slice: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3459
  "(slice n w :: 'a :: len0 word) !! m = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3460
   (w !! (m + n) & m < len_of TYPE ('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3461
  unfolding slice_shiftr 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3462
  by (simp add : nth_ucast nth_shiftr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3463
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3464
lemma slice1_down_alt': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3465
  "sl = slice1 n w ==> fs = size sl ==> fs + k = n ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3466
    to_bl sl = takefill False fs (drop k (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3467
  unfolding slice1_def word_size of_bl_def uint_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3468
  by (clarsimp simp: word_ubin.eq_norm bl_bin_bl_rep_drop drop_takefill)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3469
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3470
lemma slice1_up_alt': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3471
  "sl = slice1 n w ==> fs = size sl ==> fs = n + k ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3472
    to_bl sl = takefill False fs (replicate k False @ (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3473
  apply (unfold slice1_def word_size of_bl_def uint_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3474
  apply (clarsimp simp: word_ubin.eq_norm bl_bin_bl_rep_drop 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3475
                        takefill_append [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3476
  apply (rule_tac f = "%k. takefill False (len_of TYPE('a))
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3477
    (replicate k False @ bin_to_bl (len_of TYPE('b)) (uint w))" in arg_cong)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3478
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3479
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3480
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3481
lemmas sd1 = slice1_down_alt' [OF refl refl, unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3482
lemmas su1 = slice1_up_alt' [OF refl refl, unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3483
lemmas slice1_down_alt = le_add_diff_inverse [THEN sd1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3484
lemmas slice1_up_alts = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3485
  le_add_diff_inverse [symmetric, THEN su1] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3486
  le_add_diff_inverse2 [symmetric, THEN su1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3487
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3488
lemma ucast_slice1: "ucast w = slice1 (size w) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3489
  unfolding slice1_def ucast_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3490
  by (simp add : takefill_same' word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3491
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3492
lemma ucast_slice: "ucast w = slice 0 w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3493
  unfolding slice_def by (simp add : ucast_slice1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3494
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3495
lemmas slice_id = trans [OF ucast_slice [symmetric] ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3496
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3497
lemma revcast_slice1': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3498
  "rc = revcast w ==> slice1 (size rc) w = rc"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3499
  unfolding slice1_def revcast_def' by (simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3500
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3501
lemmas revcast_slice1 = refl [THEN revcast_slice1']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3502
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3503
lemma slice1_tf_tf': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3504
  "to_bl (slice1 n w :: 'a :: len0 word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3505
   rev (takefill False (len_of TYPE('a)) (rev (takefill False n (to_bl w))))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3506
  unfolding slice1_def by (rule word_rev_tf)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3507
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3508
lemmas slice1_tf_tf = slice1_tf_tf'
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3509
  [THEN word_bl.Rep_inverse', symmetric, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3510
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3511
lemma rev_slice1:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3512
  "n + k = len_of TYPE('a) + len_of TYPE('b) \<Longrightarrow> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3513
  slice1 n (word_reverse w :: 'b :: len0 word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3514
  word_reverse (slice1 k w :: 'a :: len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3515
  apply (unfold word_reverse_def slice1_tf_tf)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3516
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3517
  apply (rule rev_swap [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3518
  apply (rule trans [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3519
  apply (rule tf_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3520
   apply (simp add: word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3521
  apply (simp add: word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3522
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3523
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3524
lemma rev_slice': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3525
  "res = slice n (word_reverse w) ==> n + k + size res = size w ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3526
    res = word_reverse (slice k w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3527
  apply (unfold slice_def word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3528
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3529
  apply (rule rev_slice1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3530
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3531
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3532
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3533
lemmas rev_slice = refl [THEN rev_slice', unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3534
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3535
lemmas sym_notr = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3536
  not_iff [THEN iffD2, THEN not_sym, THEN not_iff [THEN iffD1]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3537
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3538
-- {* problem posed by TPHOLs referee:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3539
      criterion for overflow of addition of signed integers *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3540
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3541
lemma sofl_test:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3542
  "(sint (x :: 'a :: len word) + sint y = sint (x + y)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3543
     ((((x+y) XOR x) AND ((x+y) XOR y)) >> (size x - 1) = 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3544
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3545
  apply (cases "len_of TYPE('a)", simp) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3546
  apply (subst msb_shift [THEN sym_notr])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3547
  apply (simp add: word_ops_msb)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3548
  apply (simp add: word_msb_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3549
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3550
       apply simp_all
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3551
     apply (unfold sint_word_ariths)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3552
     apply (unfold word_sbin.set_iff_norm [symmetric] sints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3553
     apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3554
        apply (insert sint_range' [where x=x])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3555
        apply (insert sint_range' [where x=y])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3556
        defer 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3557
        apply (simp (no_asm), arith)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3558
       apply (simp (no_asm), arith)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3559
      defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3560
      defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3561
      apply (simp (no_asm), arith)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3562
     apply (simp (no_asm), arith)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3563
    apply (rule notI [THEN notnotD],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3564
           drule leI not_leE,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3565
           drule sbintrunc_inc sbintrunc_dec,      
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3566
           simp)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3567
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3568
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3569
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3570
subsection "Split and cat"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3571
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3572
lemmas word_split_bin' = word_split_def [THEN meta_eq_to_obj_eq, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3573
lemmas word_cat_bin' = word_cat_def [THEN meta_eq_to_obj_eq, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3574
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3575
lemma word_rsplit_no:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3576
  "(word_rsplit (number_of bin :: 'b :: len0 word) :: 'a word list) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3577
    map number_of (bin_rsplit (len_of TYPE('a :: len)) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3578
      (len_of TYPE('b), bintrunc (len_of TYPE('b)) bin))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3579
  apply (unfold word_rsplit_def word_no_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3580
  apply (simp add: word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3581
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3582
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3583
lemmas word_rsplit_no_cl [simp] = word_rsplit_no
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3584
  [unfolded bin_rsplitl_def bin_rsplit_l [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3585
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3586
lemma test_bit_cat:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3587
  "wc = word_cat a b ==> wc !! n = (n < size wc & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3588
    (if n < size b then b !! n else a !! (n - size b)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3589
  apply (unfold word_cat_bin' test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3590
  apply (auto simp add : word_ubin.eq_norm nth_bintr bin_nth_cat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3591
  apply (erule bin_nth_uint_imp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3592
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3593
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3594
lemma word_cat_bl: "word_cat a b = of_bl (to_bl a @ to_bl b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3595
  apply (unfold of_bl_def to_bl_def word_cat_bin')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3596
  apply (simp add: bl_to_bin_app_cat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3597
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3598
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3599
lemma of_bl_append:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3600
  "(of_bl (xs @ ys) :: 'a :: len word) = of_bl xs * 2^(length ys) + of_bl ys"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3601
  apply (unfold of_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3602
  apply (simp add: bl_to_bin_app_cat bin_cat_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3603
  apply (simp add: word_of_int_power_hom [symmetric] new_word_of_int_hom_syms)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3604
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3605
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3606
lemma of_bl_False [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3607
  "of_bl (False#xs) = of_bl xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3608
  by (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3609
     (auto simp add: test_bit_of_bl nth_append)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3610
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3611
lemma of_bl_True: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3612
  "(of_bl (True#xs)::'a::len word) = 2^length xs + of_bl xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3613
  by (subst of_bl_append [where xs="[True]", simplified])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3614
     (simp add: word_1_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3615
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3616
lemma of_bl_Cons:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3617
  "of_bl (x#xs) = of_bool x * 2^length xs + of_bl xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3618
  by (cases x) (simp_all add: of_bl_True)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3619
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3620
lemma split_uint_lem: "bin_split n (uint (w :: 'a :: len0 word)) = (a, b) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3621
  a = bintrunc (len_of TYPE('a) - n) a & b = bintrunc (len_of TYPE('a)) b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3622
  apply (frule word_ubin.norm_Rep [THEN ssubst])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3623
  apply (drule bin_split_trunc1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3624
  apply (drule sym [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3625
  apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3626
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3627
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3628
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3629
lemma word_split_bl': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3630
  "std = size c - size b ==> (word_split c = (a, b)) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3631
    (a = of_bl (take std (to_bl c)) & b = of_bl (drop std (to_bl c)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3632
  apply (unfold word_split_bin')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3633
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3634
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3635
   apply (clarsimp split: prod.splits)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3636
   apply (drule word_ubin.norm_Rep [THEN ssubst])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3637
   apply (drule split_bintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3638
   apply (simp add : of_bl_def bl2bin_drop word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3639
       word_ubin.norm_eq_iff [symmetric] min_def del : word_ubin.norm_Rep)    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3640
  apply (clarsimp split: prod.splits)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3641
  apply (frule split_uint_lem [THEN conjunct1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3642
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3643
  apply (cases "len_of TYPE('a) >= len_of TYPE('b)")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3644
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3645
   apply (simp add: word_0_bl word_0_wi_Pls)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3646
  apply (simp add : of_bl_def to_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3647
  apply (subst bin_split_take1 [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3648
    prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3649
    apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3650
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3651
  apply (erule thin_rl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3652
  apply (erule arg_cong [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3653
  apply (simp add : word_ubin.norm_eq_iff [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3654
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3655
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3656
lemma word_split_bl: "std = size c - size b ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3657
    (a = of_bl (take std (to_bl c)) & b = of_bl (drop std (to_bl c))) <-> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3658
    word_split c = (a, b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3659
  apply (rule iffI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3660
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3661
   apply (erule (1) word_split_bl')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3662
  apply (case_tac "word_split c")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3663
  apply (auto simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3664
  apply (frule word_split_bl' [rotated])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3665
  apply (auto simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3666
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3667
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3668
lemma word_split_bl_eq:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3669
   "(word_split (c::'a::len word) :: ('c :: len0 word * 'd :: len0 word)) =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3670
      (of_bl (take (len_of TYPE('a::len) - len_of TYPE('d::len0)) (to_bl c)),
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3671
       of_bl (drop (len_of TYPE('a) - len_of TYPE('d)) (to_bl c)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3672
  apply (rule word_split_bl [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3673
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3674
  apply (rule refl conjI)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3675
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3676
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3677
-- "keep quantifiers for use in simplification"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3678
lemma test_bit_split':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3679
  "word_split c = (a, b) --> (ALL n m. b !! n = (n < size b & c !! n) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3680
    a !! m = (m < size a & c !! (m + size b)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3681
  apply (unfold word_split_bin' test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3682
  apply (clarify)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3683
  apply (clarsimp simp: word_ubin.eq_norm nth_bintr word_size split: prod.splits)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3684
  apply (drule bin_nth_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3685
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3686
       apply (simp_all add: add_commute)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3687
   apply (erule bin_nth_uint_imp)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3688
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3689
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3690
lemma test_bit_split:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3691
  "word_split c = (a, b) \<Longrightarrow>
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3692
    (\<forall>n\<Colon>nat. b !! n \<longleftrightarrow> n < size b \<and> c !! n) \<and> (\<forall>m\<Colon>nat. a !! m \<longleftrightarrow> m < size a \<and> c !! (m + size b))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3693
  by (simp add: test_bit_split')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3694
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3695
lemma test_bit_split_eq: "word_split c = (a, b) <-> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3696
  ((ALL n::nat. b !! n = (n < size b & c !! n)) &
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3697
    (ALL m::nat. a !! m = (m < size a & c !! (m + size b))))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3698
  apply (rule_tac iffI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3699
   apply (rule_tac conjI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3700
    apply (erule test_bit_split [THEN conjunct1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3701
   apply (erule test_bit_split [THEN conjunct2])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3702
  apply (case_tac "word_split c")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3703
  apply (frule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3704
  apply (erule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3705
  apply (fastsimp intro ! : word_eqI simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3706
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3707
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3708
-- {* this odd result is analogous to @{text "ucast_id"}, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3709
      result to the length given by the result type *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3710
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3711
lemma word_cat_id: "word_cat a b = b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3712
  unfolding word_cat_bin' by (simp add: word_ubin.inverse_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3713
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3714
-- "limited hom result"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3715
lemma word_cat_hom:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3716
  "len_of TYPE('a::len0) <= len_of TYPE('b::len0) + len_of TYPE ('c::len0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3717
  ==>
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3718
  (word_cat (word_of_int w :: 'b word) (b :: 'c word) :: 'a word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3719
  word_of_int (bin_cat w (size b) (uint b))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3720
  apply (unfold word_cat_def word_size) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3721
  apply (clarsimp simp add: word_ubin.norm_eq_iff [symmetric]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3722
      word_ubin.eq_norm bintr_cat min_max.inf_absorb1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3723
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3724
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3725
lemma word_cat_split_alt:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3726
  "size w <= size u + size v ==> word_split w = (u, v) ==> word_cat u v = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3727
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3728
  apply (drule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3729
  apply (clarsimp simp add : test_bit_cat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3730
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3731
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3732
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3733
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3734
lemmas word_cat_split_size = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3735
  sym [THEN [2] word_cat_split_alt [symmetric], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3736
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3737
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3738
subsubsection "Split and slice"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3739
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3740
lemma split_slices: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3741
  "word_split w = (u, v) ==> u = slice (size v) w & v = slice 0 w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3742
  apply (drule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3743
  apply (rule conjI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3744
   apply (rule word_eqI, clarsimp simp: nth_slice word_size)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3745
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3746
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3747
lemma slice_cat1':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3748
  "wc = word_cat a b ==> size wc >= size a + size b ==> slice (size b) wc = a"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3749
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3750
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3751
  apply (simp add: nth_slice test_bit_cat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3752
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3753
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3754
lemmas slice_cat1 = refl [THEN slice_cat1']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3755
lemmas slice_cat2 = trans [OF slice_id word_cat_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3756
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3757
lemma cat_slices:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3758
  "a = slice n c ==> b = slice 0 c ==> n = size b ==>
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3759
    size a + size b >= size c ==> word_cat a b = c"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3760
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3761
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3762
  apply (simp add: nth_slice test_bit_cat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3763
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3764
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3765
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3766
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3767
lemma word_split_cat_alt:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3768
  "w = word_cat u v ==> size u + size v <= size w ==> word_split w = (u, v)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3769
  apply (case_tac "word_split ?w")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3770
  apply (rule trans, assumption)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3771
  apply (drule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3772
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3773
   apply (rule word_eqI, clarsimp simp: test_bit_cat word_size)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3774
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3775
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3776
lemmas word_cat_bl_no_bin [simp] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3777
  word_cat_bl [where a="number_of a" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3778
                 and b="number_of b", 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3779
               unfolded to_bl_no_bin, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3780
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3781
lemmas word_split_bl_no_bin [simp] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3782
  word_split_bl_eq [where c="number_of c", unfolded to_bl_no_bin, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3783
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3784
-- {* this odd result arises from the fact that the statement of the
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3785
      result implies that the decoded words are of the same type, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3786
      and therefore of the same length, as the original word *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3787
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3788
lemma word_rsplit_same: "word_rsplit w = [w]"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3789
  unfolding word_rsplit_def by (simp add : bin_rsplit_all)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3790
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3791
lemma word_rsplit_empty_iff_size:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3792
  "(word_rsplit w = []) = (size w = 0)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3793
  unfolding word_rsplit_def bin_rsplit_def word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3794
  by (simp add: bin_rsplit_aux_simp_alt Let_def split: Product_Type.split_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3795
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3796
lemma test_bit_rsplit:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3797
  "sw = word_rsplit w ==> m < size (hd sw :: 'a :: len word) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3798
    k < length sw ==> (rev sw ! k) !! m = (w !! (k * size (hd sw) + m))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3799
  apply (unfold word_rsplit_def word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3800
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3801
   apply (rule_tac f = "%x. bin_nth x m" in arg_cong)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3802
   apply (rule nth_map [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3803
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3804
  apply (rule bin_nth_rsplit)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3805
     apply simp_all
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3806
  apply (simp add : word_size rev_map)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3807
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3808
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3809
   apply (rule map_ident [THEN fun_cong])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3810
  apply (rule refl [THEN map_cong])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3811
  apply (simp add : word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3812
  apply (erule bin_rsplit_size_sign [OF len_gt_0 refl])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3813
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3814
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3815
lemma word_rcat_bl: "word_rcat wl == of_bl (concat (map to_bl wl))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3816
  unfolding word_rcat_def to_bl_def' of_bl_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3817
  by (clarsimp simp add : bin_rcat_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3818
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3819
lemma size_rcat_lem':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3820
  "size (concat (map to_bl wl)) = length wl * size (hd wl)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3821
  unfolding word_size by (induct wl) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3822
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3823
lemmas size_rcat_lem = size_rcat_lem' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3824
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3825
lemmas td_gal_lt_len = len_gt_0 [THEN td_gal_lt, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3826
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3827
lemma nth_rcat_lem' [rule_format] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3828
  "sw = size (hd wl  :: 'a :: len word) ==> (ALL n. n < size wl * sw --> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3829
    rev (concat (map to_bl wl)) ! n = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3830
    rev (to_bl (rev wl ! (n div sw))) ! (n mod sw))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3831
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3832
  apply (induct "wl")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3833
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3834
  apply (clarsimp simp add : nth_append size_rcat_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3835
  apply (simp (no_asm_use) only:  mult_Suc [symmetric] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3836
         td_gal_lt_len less_Suc_eq_le mod_div_equality')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3837
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3838
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3839
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3840
lemmas nth_rcat_lem = refl [THEN nth_rcat_lem', unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3841
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3842
lemma test_bit_rcat:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3843
  "sw = size (hd wl :: 'a :: len word) ==> rc = word_rcat wl ==> rc !! n = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3844
    (n < size rc & n div sw < size wl & (rev wl) ! (n div sw) !! (n mod sw))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3845
  apply (unfold word_rcat_bl word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3846
  apply (clarsimp simp add : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3847
    test_bit_of_bl size_rcat_lem word_size td_gal_lt_len)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3848
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3849
   apply (auto simp add : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3850
    test_bit_bl word_size td_gal_lt_len [THEN iffD2, THEN nth_rcat_lem])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3851
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3852
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3853
lemma foldl_eq_foldr [rule_format] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3854
  "ALL x. foldl op + x xs = foldr op + (x # xs) (0 :: 'a :: comm_monoid_add)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3855
  by (induct xs) (auto simp add : add_assoc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3856
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3857
lemmas test_bit_cong = arg_cong [where f = "test_bit", THEN fun_cong]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3858
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3859
lemmas test_bit_rsplit_alt = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3860
  trans [OF nth_rev_alt [THEN test_bit_cong] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3861
  test_bit_rsplit [OF refl asm_rl diff_Suc_less]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3862
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3863
-- "lazy way of expressing that u and v, and su and sv, have same types"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3864
lemma word_rsplit_len_indep':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3865
  "[u,v] = p ==> [su,sv] = q ==> word_rsplit u = su ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3866
    word_rsplit v = sv ==> length su = length sv"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3867
  apply (unfold word_rsplit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3868
  apply (auto simp add : bin_rsplit_len_indep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3869
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3870
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3871
lemmas word_rsplit_len_indep = word_rsplit_len_indep' [OF refl refl refl refl]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3872
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3873
lemma length_word_rsplit_size: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3874
  "n = len_of TYPE ('a :: len) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3875
    (length (word_rsplit w :: 'a word list) <= m) = (size w <= m * n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3876
  apply (unfold word_rsplit_def word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3877
  apply (clarsimp simp add : bin_rsplit_len_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3878
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3879
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3880
lemmas length_word_rsplit_lt_size = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3881
  length_word_rsplit_size [unfolded Not_eq_iff linorder_not_less [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3882
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3883
lemma length_word_rsplit_exp_size: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3884
  "n = len_of TYPE ('a :: len) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3885
    length (word_rsplit w :: 'a word list) = (size w + n - 1) div n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3886
  unfolding word_rsplit_def by (clarsimp simp add : word_size bin_rsplit_len)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3887
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3888
lemma length_word_rsplit_even_size: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3889
  "n = len_of TYPE ('a :: len) ==> size w = m * n ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3890
    length (word_rsplit w :: 'a word list) = m"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3891
  by (clarsimp simp add : length_word_rsplit_exp_size given_quot_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3892
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3893
lemmas length_word_rsplit_exp_size' = refl [THEN length_word_rsplit_exp_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3894
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3895
(* alternative proof of word_rcat_rsplit *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3896
lemmas tdle = iffD2 [OF split_div_lemma refl, THEN conjunct1] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3897
lemmas dtle = xtr4 [OF tdle mult_commute]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3898
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3899
lemma word_rcat_rsplit: "word_rcat (word_rsplit w) = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3900
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3901
  apply (clarsimp simp add : test_bit_rcat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3902
  apply (subst refl [THEN test_bit_rsplit])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3903
    apply (simp_all add: word_size 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3904
      refl [THEN length_word_rsplit_size [simplified not_less [symmetric], simplified]])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3905
   apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3906
   apply (erule xtr7, rule len_gt_0 [THEN dtle])+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3907
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3908
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3909
lemma size_word_rsplit_rcat_size':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3910
  "word_rcat (ws :: 'a :: len word list) = frcw ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3911
    size frcw = length ws * len_of TYPE ('a) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3912
    size (hd [word_rsplit frcw, ws]) = size ws" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3913
  apply (clarsimp simp add : word_size length_word_rsplit_exp_size')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3914
  apply (fast intro: given_quot_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3915
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3916
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3917
lemmas size_word_rsplit_rcat_size = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3918
  size_word_rsplit_rcat_size' [simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3919
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3920
lemma msrevs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3921
  fixes n::nat
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3922
  shows "0 < n \<Longrightarrow> (k * n + m) div n = m div n + k"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3923
  and   "(k * n + m) mod n = m mod n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3924
  by (auto simp: add_commute)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3925
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3926
lemma word_rsplit_rcat_size':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3927
  "word_rcat (ws :: 'a :: len word list) = frcw ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3928
    size frcw = length ws * len_of TYPE ('a) ==> word_rsplit frcw = ws" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3929
  apply (frule size_word_rsplit_rcat_size, assumption)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3930
  apply (clarsimp simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3931
  apply (rule nth_equalityI, assumption)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3932
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3933
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3934
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3935
   apply (rule test_bit_rsplit_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3936
     apply (clarsimp simp: word_size)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3937
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3938
  apply (rule test_bit_rcat [OF refl refl])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3939
  apply (simp add : word_size msrevs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3940
  apply (subst nth_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3941
   apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3942
  apply (simp add : le0 [THEN [2] xtr7, THEN diff_Suc_less])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3943
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3944
  apply (simp add : diff_mult_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3945
  apply (rule mpl_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3946
  apply (cases "size ws")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3947
   apply simp_all
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3948
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3949
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3950
lemmas word_rsplit_rcat_size = refl [THEN word_rsplit_rcat_size']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3951
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3952
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3953
subsection "Rotation"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3954
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3955
lemmas rotater_0' [simp] = rotater_def [where n = "0", simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3956
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3957
lemmas word_rot_defs = word_roti_def word_rotr_def word_rotl_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3958
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3959
lemma rotate_eq_mod: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3960
  "m mod length xs = n mod length xs ==> rotate m xs = rotate n xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3961
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3962
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3963
    apply (rule rotate_conv_mod [symmetric])+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3964
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3965
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3966
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3967
lemmas rotate_eqs [standard] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3968
  trans [OF rotate0 [THEN fun_cong] id_apply]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3969
  rotate_rotate [symmetric] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3970
  rotate_id 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3971
  rotate_conv_mod 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3972
  rotate_eq_mod
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3973
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3974
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3975
subsubsection "Rotation of list to right"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3976
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3977
lemma rotate1_rl': "rotater1 (l @ [a]) = a # l"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3978
  unfolding rotater1_def by (cases l) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3979
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3980
lemma rotate1_rl [simp] : "rotater1 (rotate1 l) = l"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3981
  apply (unfold rotater1_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3982
  apply (cases "l")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3983
  apply (case_tac [2] "list")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3984
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3985
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3986
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3987
lemma rotate1_lr [simp] : "rotate1 (rotater1 l) = l"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3988
  unfolding rotater1_def by (cases l) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3989
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3990
lemma rotater1_rev': "rotater1 (rev xs) = rev (rotate1 xs)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3991
  apply (cases "xs")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3992
  apply (simp add : rotater1_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3993
  apply (simp add : rotate1_rl')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3994
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3995
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3996
lemma rotater_rev': "rotater n (rev xs) = rev (rotate n xs)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3997
  unfolding rotater_def by (induct n) (auto intro: rotater1_rev')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3998
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3999
lemmas rotater_rev = rotater_rev' [where xs = "rev ys", simplified, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4000
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4001
lemma rotater_drop_take: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4002
  "rotater n xs = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4003
   drop (length xs - n mod length xs) xs @
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4004
   take (length xs - n mod length xs) xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4005
  by (clarsimp simp add : rotater_rev rotate_drop_take rev_take rev_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4006
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4007
lemma rotater_Suc [simp] : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4008
  "rotater (Suc n) xs = rotater1 (rotater n xs)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4009
  unfolding rotater_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4010
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4011
lemma rotate_inv_plus [rule_format] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4012
  "ALL k. k = m + n --> rotater k (rotate n xs) = rotater m xs & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4013
    rotate k (rotater n xs) = rotate m xs & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4014
    rotater n (rotate k xs) = rotate m xs & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4015
    rotate n (rotater k xs) = rotater m xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4016
  unfolding rotater_def rotate_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4017
  by (induct n) (auto intro: funpow_swap1 [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4018
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4019
lemmas rotate_inv_rel = le_add_diff_inverse2 [symmetric, THEN rotate_inv_plus]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4020
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4021
lemmas rotate_inv_eq = order_refl [THEN rotate_inv_rel, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4022
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4023
lemmas rotate_lr [simp] = rotate_inv_eq [THEN conjunct1, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4024
lemmas rotate_rl [simp] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4025
  rotate_inv_eq [THEN conjunct2, THEN conjunct1, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4026
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4027
lemma rotate_gal: "(rotater n xs = ys) = (rotate n ys = xs)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4028
  by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4029
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4030
lemma rotate_gal': "(ys = rotater n xs) = (xs = rotate n ys)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4031
  by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4032
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4033
lemma length_rotater [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4034
  "length (rotater n xs) = length xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4035
  by (simp add : rotater_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4036
38527
f2709bc1e41f moved spurious auxiliary lemma here
haftmann
parents: 37887
diff changeset
  4037
lemma restrict_to_left:
f2709bc1e41f moved spurious auxiliary lemma here
haftmann
parents: 37887
diff changeset
  4038
  assumes "x = y"
f2709bc1e41f moved spurious auxiliary lemma here
haftmann
parents: 37887
diff changeset
  4039
  shows "(x = z) = (y = z)"
f2709bc1e41f moved spurious auxiliary lemma here
haftmann
parents: 37887
diff changeset
  4040
  using assms by simp
f2709bc1e41f moved spurious auxiliary lemma here
haftmann
parents: 37887
diff changeset
  4041
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4042
lemmas rrs0 = rotate_eqs [THEN restrict_to_left, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4043
  simplified rotate_gal [symmetric] rotate_gal' [symmetric], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4044
lemmas rrs1 = rrs0 [THEN refl [THEN rev_iffD1]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4045
lemmas rotater_eqs = rrs1 [simplified length_rotater, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4046
lemmas rotater_0 = rotater_eqs (1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4047
lemmas rotater_add = rotater_eqs (2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4048
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4049
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4050
subsubsection "map, map2, commuting with rotate(r)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4051
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4052
lemma last_map: "xs ~= [] ==> last (map f xs) = f (last xs)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4053
  by (induct xs) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4054
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4055
lemma butlast_map:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4056
  "xs ~= [] ==> butlast (map f xs) = map f (butlast xs)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4057
  by (induct xs) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4058
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4059
lemma rotater1_map: "rotater1 (map f xs) = map f (rotater1 xs)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4060
  unfolding rotater1_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4061
  by (cases xs) (auto simp add: last_map butlast_map)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4062
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4063
lemma rotater_map:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4064
  "rotater n (map f xs) = map f (rotater n xs)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4065
  unfolding rotater_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4066
  by (induct n) (auto simp add : rotater1_map)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4067
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4068
lemma but_last_zip [rule_format] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4069
  "ALL ys. length xs = length ys --> xs ~= [] --> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4070
    last (zip xs ys) = (last xs, last ys) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4071
    butlast (zip xs ys) = zip (butlast xs) (butlast ys)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4072
  apply (induct "xs")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4073
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4074
     apply ((case_tac ys, auto simp: neq_Nil_conv)[1])+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4075
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4076
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4077
lemma but_last_map2 [rule_format] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4078
  "ALL ys. length xs = length ys --> xs ~= [] --> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4079
    last (map2 f xs ys) = f (last xs) (last ys) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4080
    butlast (map2 f xs ys) = map2 f (butlast xs) (butlast ys)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4081
  apply (induct "xs")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4082
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4083
     apply (unfold map2_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4084
     apply ((case_tac ys, auto simp: neq_Nil_conv)[1])+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4085
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4086
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4087
lemma rotater1_zip:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4088
  "length xs = length ys ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4089
    rotater1 (zip xs ys) = zip (rotater1 xs) (rotater1 ys)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4090
  apply (unfold rotater1_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4091
  apply (cases "xs")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4092
   apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4093
   apply ((case_tac ys, auto simp: neq_Nil_conv but_last_zip)[1])+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4094
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4095
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4096
lemma rotater1_map2:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4097
  "length xs = length ys ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4098
    rotater1 (map2 f xs ys) = map2 f (rotater1 xs) (rotater1 ys)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4099
  unfolding map2_def by (simp add: rotater1_map rotater1_zip)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4100
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4101
lemmas lrth = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4102
  box_equals [OF asm_rl length_rotater [symmetric] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4103
                 length_rotater [symmetric], 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4104
              THEN rotater1_map2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4105
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4106
lemma rotater_map2: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4107
  "length xs = length ys ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4108
    rotater n (map2 f xs ys) = map2 f (rotater n xs) (rotater n ys)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4109
  by (induct n) (auto intro!: lrth)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4110
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4111
lemma rotate1_map2:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4112
  "length xs = length ys ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4113
    rotate1 (map2 f xs ys) = map2 f (rotate1 xs) (rotate1 ys)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4114
  apply (unfold map2_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4115
  apply (cases xs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4116
   apply (cases ys, auto simp add : rotate1_def)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4117
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4118
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4119
lemmas lth = box_equals [OF asm_rl length_rotate [symmetric] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4120
  length_rotate [symmetric], THEN rotate1_map2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4121
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4122
lemma rotate_map2: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4123
  "length xs = length ys ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4124
    rotate n (map2 f xs ys) = map2 f (rotate n xs) (rotate n ys)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4125
  by (induct n) (auto intro!: lth)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4126
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4127
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4128
-- "corresponding equalities for word rotation"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4129
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4130
lemma to_bl_rotl: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4131
  "to_bl (word_rotl n w) = rotate n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4132
  by (simp add: word_bl.Abs_inverse' word_rotl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4133
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4134
lemmas blrs0 = rotate_eqs [THEN to_bl_rotl [THEN trans]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4135
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4136
lemmas word_rotl_eqs =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4137
  blrs0 [simplified word_bl.Rep' word_bl.Rep_inject to_bl_rotl [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4138
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4139
lemma to_bl_rotr: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4140
  "to_bl (word_rotr n w) = rotater n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4141
  by (simp add: word_bl.Abs_inverse' word_rotr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4142
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4143
lemmas brrs0 = rotater_eqs [THEN to_bl_rotr [THEN trans]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4144
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4145
lemmas word_rotr_eqs =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4146
  brrs0 [simplified word_bl.Rep' word_bl.Rep_inject to_bl_rotr [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4147
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4148
declare word_rotr_eqs (1) [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4149
declare word_rotl_eqs (1) [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4150
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4151
lemma
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4152
  word_rot_rl [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4153
  "word_rotl k (word_rotr k v) = v" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4154
  word_rot_lr [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4155
  "word_rotr k (word_rotl k v) = v"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4156
  by (auto simp add: to_bl_rotr to_bl_rotl word_bl.Rep_inject [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4157
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4158
lemma
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4159
  word_rot_gal:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4160
  "(word_rotr n v = w) = (word_rotl n w = v)" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4161
  word_rot_gal':
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4162
  "(w = word_rotr n v) = (v = word_rotl n w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4163
  by (auto simp: to_bl_rotr to_bl_rotl word_bl.Rep_inject [symmetric] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4164
           dest: sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4165
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4166
lemma word_rotr_rev:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4167
  "word_rotr n w = word_reverse (word_rotl n (word_reverse w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4168
  by (simp add: word_bl.Rep_inject [symmetric] to_bl_word_rev
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4169
                to_bl_rotr to_bl_rotl rotater_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4170
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4171
lemma word_roti_0 [simp]: "word_roti 0 w = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4172
  by (unfold word_rot_defs) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4173
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4174
lemmas abl_cong = arg_cong [where f = "of_bl"]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4175
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4176
lemma word_roti_add: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4177
  "word_roti (m + n) w = word_roti m (word_roti n w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4178
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4179
  have rotater_eq_lem: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4180
    "\<And>m n xs. m = n ==> rotater m xs = rotater n xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4181
    by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4182
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4183
  have rotate_eq_lem: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4184
    "\<And>m n xs. m = n ==> rotate m xs = rotate n xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4185
    by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4186
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4187
  note rpts [symmetric, standard] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4188
    rotate_inv_plus [THEN conjunct1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4189
    rotate_inv_plus [THEN conjunct2, THEN conjunct1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4190
    rotate_inv_plus [THEN conjunct2, THEN conjunct2, THEN conjunct1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4191
    rotate_inv_plus [THEN conjunct2, THEN conjunct2, THEN conjunct2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4192
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4193
  note rrp = trans [symmetric, OF rotate_rotate rotate_eq_lem]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4194
  note rrrp = trans [symmetric, OF rotater_add [symmetric] rotater_eq_lem]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4195
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4196
  show ?thesis
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4197
  apply (unfold word_rot_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4198
  apply (simp only: split: split_if)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4199
  apply (safe intro!: abl_cong)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4200
  apply (simp_all only: to_bl_rotl [THEN word_bl.Rep_inverse'] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4201
                    to_bl_rotl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4202
                    to_bl_rotr [THEN word_bl.Rep_inverse']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4203
                    to_bl_rotr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4204
  apply (rule rrp rrrp rpts,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4205
         simp add: nat_add_distrib [symmetric] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4206
                   nat_diff_distrib [symmetric])+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4207
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4208
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4209
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4210
lemma word_roti_conv_mod': "word_roti n w = word_roti (n mod int (size w)) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4211
  apply (unfold word_rot_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4212
  apply (cut_tac y="size w" in gt_or_eq_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4213
  apply (erule disjE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4214
   apply simp_all
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4215
  apply (safe intro!: abl_cong)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4216
   apply (rule rotater_eqs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4217
   apply (simp add: word_size nat_mod_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4218
  apply (simp add: rotater_add [symmetric] rotate_gal [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4219
  apply (rule rotater_eqs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4220
  apply (simp add: word_size nat_mod_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4221
  apply (rule int_eq_0_conv [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4222
  apply (simp only: zmod_int zadd_int [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4223
  apply (simp add: rdmods)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4224
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4225
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4226
lemmas word_roti_conv_mod = word_roti_conv_mod' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4227
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4228
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4229
subsubsection "Word rotation commutes with bit-wise operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4230
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4231
(* using locale to not pollute lemma namespace *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4232
locale word_rotate 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4233
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4234
context word_rotate
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4235
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4236
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4237
lemmas word_rot_defs' = to_bl_rotl to_bl_rotr
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4238
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4239
lemmas blwl_syms [symmetric] = bl_word_not bl_word_and bl_word_or bl_word_xor
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4240
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4241
lemmas lbl_lbl = trans [OF word_bl.Rep' word_bl.Rep' [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4242
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4243
lemmas ths_map2 [OF lbl_lbl] = rotate_map2 rotater_map2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4244
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4245
lemmas ths_map [where xs = "to_bl v", standard] = rotate_map rotater_map
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4246
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4247
lemmas th1s [simplified word_rot_defs' [symmetric]] = ths_map2 ths_map
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4248
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4249
lemma word_rot_logs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4250
  "word_rotl n (NOT v) = NOT word_rotl n v"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4251
  "word_rotr n (NOT v) = NOT word_rotr n v"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4252
  "word_rotl n (x AND y) = word_rotl n x AND word_rotl n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4253
  "word_rotr n (x AND y) = word_rotr n x AND word_rotr n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4254
  "word_rotl n (x OR y) = word_rotl n x OR word_rotl n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4255
  "word_rotr n (x OR y) = word_rotr n x OR word_rotr n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4256
  "word_rotl n (x XOR y) = word_rotl n x XOR word_rotl n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4257
  "word_rotr n (x XOR y) = word_rotr n x XOR word_rotr n y"  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4258
  by (rule word_bl.Rep_eqD,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4259
      rule word_rot_defs' [THEN trans],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4260
      simp only: blwl_syms [symmetric],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4261
      rule th1s [THEN trans], 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4262
      rule refl)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4263
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4264
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4265
lemmas word_rot_logs = word_rotate.word_rot_logs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4266
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4267
lemmas bl_word_rotl_dt = trans [OF to_bl_rotl rotate_drop_take,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4268
  simplified word_bl.Rep', standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4269
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4270
lemmas bl_word_rotr_dt = trans [OF to_bl_rotr rotater_drop_take,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4271
  simplified word_bl.Rep', standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4272
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4273
lemma bl_word_roti_dt': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4274
  "n = nat ((- i) mod int (size (w :: 'a :: len word))) ==> 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4275
    to_bl (word_roti i w) = drop n (to_bl w) @ take n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4276
  apply (unfold word_roti_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4277
  apply (simp add: bl_word_rotl_dt bl_word_rotr_dt word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4278
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4279
   apply (simp add: zmod_zminus1_eq_if)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4280
   apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4281
    apply (simp add: nat_mult_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4282
   apply (simp add: nat_diff_distrib [OF pos_mod_sign pos_mod_conj 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4283
                                      [THEN conjunct2, THEN order_less_imp_le]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4284
                    nat_mod_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4285
  apply (simp add: nat_mod_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4286
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4287
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4288
lemmas bl_word_roti_dt = bl_word_roti_dt' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4289
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4290
lemmas word_rotl_dt = bl_word_rotl_dt 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4291
  [THEN word_bl.Rep_inverse' [symmetric], standard] 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4292
lemmas word_rotr_dt = bl_word_rotr_dt 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4293
  [THEN word_bl.Rep_inverse' [symmetric], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4294
lemmas word_roti_dt = bl_word_roti_dt 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4295
  [THEN word_bl.Rep_inverse' [symmetric], standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4296
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4297
lemma word_rotx_0 [simp] : "word_rotr i 0 = 0 & word_rotl i 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4298
  by (simp add : word_rotr_dt word_rotl_dt to_bl_0 replicate_add [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4299
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4300
lemma word_roti_0' [simp] : "word_roti n 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4301
  unfolding word_roti_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4302
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4303
lemmas word_rotr_dt_no_bin' [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4304
  word_rotr_dt [where w="number_of w", unfolded to_bl_no_bin, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4305
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4306
lemmas word_rotl_dt_no_bin' [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4307
  word_rotl_dt [where w="number_of w", unfolded to_bl_no_bin, standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4308
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4309
declare word_roti_def [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4310
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4311
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4312
subsection {* Miscellaneous  *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4313
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4314
declare of_nat_2p [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4315
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4316
lemma word_int_cases:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4317
  "\<lbrakk>\<And>n. \<lbrakk>(x ::'a::len0 word) = word_of_int n; 0 \<le> n; n < 2^len_of TYPE('a)\<rbrakk> \<Longrightarrow> P\<rbrakk>
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4318
   \<Longrightarrow> P"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4319
  by (cases x rule: word_uint.Abs_cases) (simp add: uints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4320
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4321
lemma word_nat_cases [cases type: word]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4322
  "\<lbrakk>\<And>n. \<lbrakk>(x ::'a::len word) = of_nat n; n < 2^len_of TYPE('a)\<rbrakk> \<Longrightarrow> P\<rbrakk>
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4323
   \<Longrightarrow> P"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4324
  by (cases x rule: word_unat.Abs_cases) (simp add: unats_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4325
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4326
lemma max_word_eq:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4327
  "(max_word::'a::len word) = 2^len_of TYPE('a) - 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4328
  by (simp add: max_word_def word_of_int_hom_syms word_of_int_2p)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4329
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4330
lemma max_word_max [simp,intro!]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4331
  "n \<le> max_word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4332
  by (cases n rule: word_int_cases)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4333
     (simp add: max_word_def word_le_def int_word_uint int_mod_eq')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4334
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4335
lemma word_of_int_2p_len: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4336
  "word_of_int (2 ^ len_of TYPE('a)) = (0::'a::len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4337
  by (subst word_uint.Abs_norm [symmetric]) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4338
     (simp add: word_of_int_hom_syms)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4339
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4340
lemma word_pow_0:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4341
  "(2::'a::len word) ^ len_of TYPE('a) = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4342
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4343
  have "word_of_int (2 ^ len_of TYPE('a)) = (0::'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4344
    by (rule word_of_int_2p_len)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4345
  thus ?thesis by (simp add: word_of_int_2p)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4346
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4347
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4348
lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4349
  apply (simp add: max_word_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4350
  apply uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4351
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4352
  apply (simp add: word_pow_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4353
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4354
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4355
lemma max_word_minus: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4356
  "max_word = (-1::'a::len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4357
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4358
  have "-1 + 1 = (0::'a word)" by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4359
  thus ?thesis by (rule max_word_wrap [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4360
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4361
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4362
lemma max_word_bl [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4363
  "to_bl (max_word::'a::len word) = replicate (len_of TYPE('a)) True"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4364
  by (subst max_word_minus to_bl_n1)+ simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4365
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4366
lemma max_test_bit [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4367
  "(max_word::'a::len word) !! n = (n < len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4368
  by (auto simp add: test_bit_bl word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4369
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4370
lemma word_and_max [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4371
  "x AND max_word = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4372
  by (rule word_eqI) (simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4373
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4374
lemma word_or_max [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4375
  "x OR max_word = max_word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4376
  by (rule word_eqI) (simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4377
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4378
lemma word_ao_dist2:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4379
  "x AND (y OR z) = x AND y OR x AND (z::'a::len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4380
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4381
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4382
lemma word_oa_dist2:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4383
  "x OR y AND z = (x OR y) AND (x OR (z::'a::len0 word))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4384
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4385
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4386
lemma word_and_not [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4387
  "x AND NOT x = (0::'a::len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4388
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4389
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4390
lemma word_or_not [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4391
  "x OR NOT x = max_word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4392
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4393
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4394
lemma word_boolean:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4395
  "boolean (op AND) (op OR) bitNOT 0 max_word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4396
  apply (rule boolean.intro)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4397
           apply (rule word_bw_assocs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4398
          apply (rule word_bw_assocs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4399
         apply (rule word_bw_comms)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4400
        apply (rule word_bw_comms)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4401
       apply (rule word_ao_dist2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4402
      apply (rule word_oa_dist2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4403
     apply (rule word_and_max)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4404
    apply (rule word_log_esimps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4405
   apply (rule word_and_not)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4406
  apply (rule word_or_not)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4407
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4408
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4409
interpretation word_bool_alg:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4410
  boolean "op AND" "op OR" bitNOT 0 max_word
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4411
  by (rule word_boolean)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4412
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4413
lemma word_xor_and_or:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4414
  "x XOR y = x AND NOT y OR NOT x AND (y::'a::len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4415
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4416
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4417
interpretation word_bool_alg:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4418
  boolean_xor "op AND" "op OR" bitNOT 0 max_word "op XOR"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4419
  apply (rule boolean_xor.intro)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4420
   apply (rule word_boolean)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4421
  apply (rule boolean_xor_axioms.intro)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4422
  apply (rule word_xor_and_or)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4423
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4424
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4425
lemma shiftr_x_0 [iff]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4426
  "(x::'a::len0 word) >> 0 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4427
  by (simp add: shiftr_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4428
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4429
lemma shiftl_x_0 [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4430
  "(x :: 'a :: len word) << 0 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4431
  by (simp add: shiftl_t2n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4432
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4433
lemma shiftl_1 [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4434
  "(1::'a::len word) << n = 2^n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4435
  by (simp add: shiftl_t2n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4436
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4437
lemma uint_lt_0 [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4438
  "uint x < 0 = False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4439
  by (simp add: linorder_not_less)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4440
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4441
lemma shiftr1_1 [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4442
  "shiftr1 (1::'a::len word) = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4443
  by (simp add: shiftr1_def word_0_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4444
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4445
lemma shiftr_1[simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4446
  "(1::'a::len word) >> n = (if n = 0 then 1 else 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4447
  by (induct n) (auto simp: shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4448
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4449
lemma word_less_1 [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4450
  "((x::'a::len word) < 1) = (x = 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4451
  by (simp add: word_less_nat_alt unat_0_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4452
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4453
lemma to_bl_mask:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4454
  "to_bl (mask n :: 'a::len word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4455
  replicate (len_of TYPE('a) - n) False @ 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4456
    replicate (min (len_of TYPE('a)) n) True"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4457
  by (simp add: mask_bl word_rep_drop min_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4458
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4459
lemma map_replicate_True:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4460
  "n = length xs ==>
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4461
    map (\<lambda>(x,y). x & y) (zip xs (replicate n True)) = xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4462
  by (induct xs arbitrary: n) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4463
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4464
lemma map_replicate_False:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4465
  "n = length xs ==> map (\<lambda>(x,y). x & y)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4466
    (zip xs (replicate n False)) = replicate n False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4467
  by (induct xs arbitrary: n) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4468
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4469
lemma bl_and_mask:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4470
  fixes w :: "'a::len word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4471
  fixes n
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4472
  defines "n' \<equiv> len_of TYPE('a) - n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4473
  shows "to_bl (w AND mask n) =  replicate n' False @ drop n' (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4474
proof - 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4475
  note [simp] = map_replicate_True map_replicate_False
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4476
  have "to_bl (w AND mask n) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4477
        map2 op & (to_bl w) (to_bl (mask n::'a::len word))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4478
    by (simp add: bl_word_and)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4479
  also
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4480
  have "to_bl w = take n' (to_bl w) @ drop n' (to_bl w)" by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4481
  also
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4482
  have "map2 op & \<dots> (to_bl (mask n::'a::len word)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4483
        replicate n' False @ drop n' (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4484
    unfolding to_bl_mask n'_def map2_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4485
    by (subst zip_append) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4486
  finally
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4487
  show ?thesis .
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4488
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4489
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4490
lemma drop_rev_takefill:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4491
  "length xs \<le> n ==>
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4492
    drop (n - length xs) (rev (takefill False n (rev xs))) = xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4493
  by (simp add: takefill_alt rev_take)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4494
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4495
lemma map_nth_0 [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4496
  "map (op !! (0::'a::len0 word)) xs = replicate (length xs) False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4497
  by (induct xs) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4498
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4499
lemma uint_plus_if_size:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4500
  "uint (x + y) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4501
  (if uint x + uint y < 2^size x then 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4502
      uint x + uint y 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4503
   else 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4504
      uint x + uint y - 2^size x)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4505
  by (simp add: word_arith_alts int_word_uint mod_add_if_z 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4506
                word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4507
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4508
lemma unat_plus_if_size:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4509
  "unat (x + (y::'a::len word)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4510
  (if unat x + unat y < 2^size x then 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4511
      unat x + unat y 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4512
   else 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4513
      unat x + unat y - 2^size x)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4514
  apply (subst word_arith_nat_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4515
  apply (subst unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4516
  apply (simp add:  mod_nat_add word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4517
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4518
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4519
lemma word_neq_0_conv [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4520
  fixes w :: "'a :: len word"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4521
  shows "(w \<noteq> 0) = (0 < w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4522
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4523
  have "0 \<le> w" by (rule word_zero_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4524
  thus ?thesis by (auto simp add: word_less_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4525
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4526
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4527
lemma max_lt:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4528
  "unat (max a b div c) = unat (max a b) div unat (c:: 'a :: len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4529
  apply (subst word_arith_nat_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4530
  apply (subst word_unat.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4531
  apply (subst mod_if)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4532
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4533
  apply (erule notE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4534
  apply (insert div_le_dividend [of "unat (max a b)" "unat c"])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4535
  apply (erule order_le_less_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4536
  apply (insert unat_lt2p [of "max a b"])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4537
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4538
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4539
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4540
lemma uint_sub_if_size:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4541
  "uint (x - y) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4542
  (if uint y \<le> uint x then 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4543
      uint x - uint y 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4544
   else 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4545
      uint x - uint y + 2^size x)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4546
  by (simp add: word_arith_alts int_word_uint mod_sub_if_z 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4547
                word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4548
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4549
lemma unat_sub:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4550
  "b <= a ==> unat (a - b) = unat a - unat b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4551
  by (simp add: unat_def uint_sub_if_size word_le_def nat_diff_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4552
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4553
lemmas word_less_sub1_numberof [simp] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4554
  word_less_sub1 [of "number_of w", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4555
lemmas word_le_sub1_numberof [simp] =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4556
  word_le_sub1 [of "number_of w", standard]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4557
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4558
lemma word_of_int_minus: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4559
  "word_of_int (2^len_of TYPE('a) - i) = (word_of_int (-i)::'a::len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4560
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4561
  have x: "2^len_of TYPE('a) - i = -i + 2^len_of TYPE('a)" by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4562
  show ?thesis
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4563
    apply (subst x)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4564
    apply (subst word_uint.Abs_norm [symmetric], subst mod_add_self2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4565
    apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4566
    done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4567
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4568
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4569
lemmas word_of_int_inj = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4570
  word_uint.Abs_inject [unfolded uints_num, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4571
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4572
lemma word_le_less_eq:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4573
  "(x ::'z::len word) \<le> y = (x = y \<or> x < y)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4574
  by (auto simp add: word_less_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4575
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4576
lemma mod_plus_cong:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4577
  assumes 1: "(b::int) = b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4578
      and 2: "x mod b' = x' mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4579
      and 3: "y mod b' = y' mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4580
      and 4: "x' + y' = z'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4581
  shows "(x + y) mod b = z' mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4582
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4583
  from 1 2[symmetric] 3[symmetric] have "(x + y) mod b = (x' mod b' + y' mod b') mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4584
    by (simp add: mod_add_eq[symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4585
  also have "\<dots> = (x' + y') mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4586
    by (simp add: mod_add_eq[symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4587
  finally show ?thesis by (simp add: 4)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4588
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4589
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4590
lemma mod_minus_cong:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4591
  assumes 1: "(b::int) = b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4592
      and 2: "x mod b' = x' mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4593
      and 3: "y mod b' = y' mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4594
      and 4: "x' - y' = z'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4595
  shows "(x - y) mod b = z' mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4596
  using assms
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4597
  apply (subst zmod_zsub_left_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4598
  apply (subst zmod_zsub_right_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4599
  apply (simp add: zmod_zsub_left_eq [symmetric] zmod_zsub_right_eq [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4600
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4601
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4602
lemma word_induct_less: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4603
  "\<lbrakk>P (0::'a::len word); \<And>n. \<lbrakk>n < m; P n\<rbrakk> \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P m"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4604
  apply (cases m)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4605
  apply atomize
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4606
  apply (erule rev_mp)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4607
  apply (rule_tac x=m in spec)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4608
  apply (induct_tac n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4609
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4610
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4611
  apply (erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4612
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4613
   apply (erule_tac x=n in allE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4614
   apply (erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4615
    apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4616
    apply (clarsimp simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4617
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4618
  apply (erule_tac x="of_nat na" in allE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4619
  apply (erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4620
   apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4621
   apply (clarsimp simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4622
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4623
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4624
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4625
lemma word_induct: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4626
  "\<lbrakk>P (0::'a::len word); \<And>n. P n \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P m"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4627
  by (erule word_induct_less, simp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4628
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4629
lemma word_induct2 [induct type]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4630
  "\<lbrakk>P 0; \<And>n. \<lbrakk>1 + n \<noteq> 0; P n\<rbrakk> \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P (n::'b::len word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4631
  apply (rule word_induct, simp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4632
  apply (case_tac "1+n = 0", auto)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4633
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4634
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4635
definition word_rec :: "'a \<Rightarrow> ('b::len word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4636
  "word_rec forZero forSuc n \<equiv> nat_rec forZero (forSuc \<circ> of_nat) (unat n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4637
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4638
lemma word_rec_0: "word_rec z s 0 = z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4639
  by (simp add: word_rec_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4640
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4641
lemma word_rec_Suc: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4642
  "1 + n \<noteq> (0::'a::len word) \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4643
  apply (simp add: word_rec_def unat_word_ariths)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4644
  apply (subst nat_mod_eq')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4645
   apply (cut_tac x=n in unat_lt2p)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4646
   apply (drule Suc_mono)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4647
   apply (simp add: less_Suc_eq_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4648
   apply (simp only: order_less_le, simp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4649
   apply (erule contrapos_pn, simp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4650
   apply (drule arg_cong[where f=of_nat])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4651
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4652
   apply (subst (asm) word_unat.Rep_inverse[of n])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4653
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4654
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4655
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4656
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4657
lemma word_rec_Pred: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4658
  "n \<noteq> 0 \<Longrightarrow> word_rec z s n = s (n - 1) (word_rec z s (n - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4659
  apply (rule subst[where t="n" and s="1 + (n - 1)"])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4660
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4661
  apply (subst word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4662
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4663
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4664
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4665
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4666
lemma word_rec_in: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4667
  "f (word_rec z (\<lambda>_. f) n) = word_rec (f z) (\<lambda>_. f) n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4668
  by (induct n) (simp_all add: word_rec_0 word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4669
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4670
lemma word_rec_in2: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4671
  "f n (word_rec z f n) = word_rec (f 0 z) (f \<circ> op + 1) n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4672
  by (induct n) (simp_all add: word_rec_0 word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4673
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4674
lemma word_rec_twice: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4675
  "m \<le> n \<Longrightarrow> word_rec z f n = word_rec (word_rec z f (n - m)) (f \<circ> op + (n - m)) m"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4676
apply (erule rev_mp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4677
apply (rule_tac x=z in spec)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4678
apply (rule_tac x=f in spec)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4679
apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4680
 apply (simp add: word_rec_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4681
apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4682
apply (rule_tac t="1 + n - m" and s="1 + (n - m)" in subst)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4683
 apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4684
apply (case_tac "1 + (n - m) = 0")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4685
 apply (simp add: word_rec_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4686
 apply (rule_tac f = "word_rec ?a ?b" in arg_cong)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4687
 apply (rule_tac t="m" and s="m + (1 + (n - m))" in subst)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4688
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4689
 apply (simp (no_asm_use))
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4690
apply (simp add: word_rec_Suc word_rec_in2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4691
apply (erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4692
 apply uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4693
apply (drule_tac x="x \<circ> op + 1" in spec)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4694
apply (drule_tac x="x 0 xa" in spec)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4695
apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4696
apply (rule_tac t="\<lambda>a. x (1 + (n - m + a))" and s="\<lambda>a. x (1 + (n - m) + a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4697
       in subst)
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39198
diff changeset
  4698
 apply (clarsimp simp add: fun_eq_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4699
 apply (rule_tac t="(1 + (n - m + xb))" and s="1 + (n - m) + xb" in subst)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4700
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4701
 apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4702
apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4703
done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4704
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4705
lemma word_rec_id: "word_rec z (\<lambda>_. id) n = z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4706
  by (induct n) (auto simp add: word_rec_0 word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4707
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4708
lemma word_rec_id_eq: "\<forall>m < n. f m = id \<Longrightarrow> word_rec z f n = z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4709
apply (erule rev_mp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4710
apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4711
 apply (auto simp add: word_rec_0 word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4712
 apply (drule spec, erule mp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4713
 apply uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4714
apply (drule_tac x=n in spec, erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4715
 apply uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4716
apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4717
done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4718
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4719
lemma word_rec_max: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4720
  "\<forall>m\<ge>n. m \<noteq> -1 \<longrightarrow> f m = id \<Longrightarrow> word_rec z f -1 = word_rec z f n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4721
apply (subst word_rec_twice[where n="-1" and m="-1 - n"])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4722
 apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4723
apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4724
apply (rule word_rec_id_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4725
apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4726
apply (drule spec, rule mp, erule mp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4727
 apply (rule word_plus_mono_right2[OF _ order_less_imp_le])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4728
  prefer 2 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4729
  apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4730
 apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4731
apply (erule contrapos_pn)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4732
apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4733
apply (drule arg_cong[where f="\<lambda>x. x - n"])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4734
apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4735
done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4736
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4737
lemma unatSuc: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4738
  "1 + n \<noteq> (0::'a::len word) \<Longrightarrow> unat (1 + n) = Suc (unat n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4739
  by unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4740
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4741
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4742
lemmas word_no_1 [simp] = word_1_no [symmetric, unfolded BIT_simps]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4743
lemmas word_no_0 [simp] = word_0_no [symmetric]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4744
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4745
declare word_0_bl [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4746
declare bin_to_bl_def [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4747
declare to_bl_0 [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4748
declare of_bl_True [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4749
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4750
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4751
use "~~/src/HOL/Tools/SMT/smt_word.ML"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4752
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 35049
diff changeset
  4753
setup {* SMT_Word.setup *}
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 35049
diff changeset
  4754
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4755
end