src/HOL/Word/Word.thy
author haftmann
Mon, 23 Dec 2013 14:24:22 +0100
changeset 54849 d325c7c4a4f7
parent 54848 a303daddebbf
child 54854 3324a0078636
permissions -rw-r--r--
dropped redundant lemma
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
46124
3ee75fe01986 misc tuning;
wenzelm
parents: 46064
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
41413
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41060
diff changeset
     8
imports
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41060
diff changeset
     9
  Type_Length
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41060
diff changeset
    10
  "~~/src/HOL/Library/Boolean_Algebra"
54224
9fda41a04c32 separated bit operations on type bit from generic syntactic bit operations
haftmann
parents: 54221
diff changeset
    11
  Bit_Bit
41413
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41060
diff changeset
    12
  Bool_List_Representation
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51717
diff changeset
    13
  Misc_Typedef
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51717
diff changeset
    14
  Word_Miscellaneous
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    15
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    16
54743
b9ae4a2f615b more antiquotations;
wenzelm
parents: 54742
diff changeset
    17
text {* See @{file "Examples/WordExamples.thy"} for examples. *}
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    18
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    19
subsection {* Type definition *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    20
49834
b27bbb021df1 discontinued obsolete typedef (open) syntax;
wenzelm
parents: 48891
diff changeset
    21
typedef 'a word = "{(0::int) ..< 2^len_of TYPE('a::len0)}"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    22
  morphisms uint Abs_word by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    23
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    24
lemma uint_nonnegative:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    25
  "0 \<le> uint w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    26
  using word.uint [of w] by simp
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    27
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    28
lemma uint_bounded:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    29
  fixes w :: "'a::len0 word"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    30
  shows "uint w < 2 ^ len_of TYPE('a)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    31
  using word.uint [of w] by simp
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    32
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    33
lemma uint_idem:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    34
  fixes w :: "'a::len0 word"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    35
  shows "uint w mod 2 ^ len_of TYPE('a) = uint w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    36
  using uint_nonnegative uint_bounded by (rule mod_pos_pos_trivial)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    37
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
    38
definition word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
    39
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    40
  -- {* representation of words using unsigned or signed bins, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    41
        only difference in these is the type class *}
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    42
  "word_of_int k = Abs_word (k mod 2 ^ len_of TYPE('a))" 
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    43
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    44
lemma uint_word_of_int:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    45
  "uint (word_of_int k :: 'a::len0 word) = k mod 2 ^ len_of TYPE('a)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    46
  by (auto simp add: word_of_int_def intro: Abs_word_inverse)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    47
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    48
lemma word_of_int_uint:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    49
  "word_of_int (uint w) = w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    50
  by (simp add: word_of_int_def uint_idem uint_inverse)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    51
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    52
lemma word_uint_eq_iff:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    53
  "a = b \<longleftrightarrow> uint a = uint b"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    54
  by (simp add: uint_inject)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    55
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    56
lemma word_uint_eqI:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    57
  "uint a = uint b \<Longrightarrow> a = b"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    58
  by (simp add: word_uint_eq_iff)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    59
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    60
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    61
subsection {* Basic code generation setup *}
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    62
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    63
definition Word :: "int \<Rightarrow> 'a::len0 word"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    64
where
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    65
  [code_post]: "Word = word_of_int"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    66
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    67
lemma [code abstype]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    68
  "Word (uint w) = w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    69
  by (simp add: Word_def word_of_int_uint)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    70
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    71
declare uint_word_of_int [code abstract]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    72
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    73
instantiation word :: (len0) equal
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    74
begin
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    75
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
    76
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
    77
where
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    78
  "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    79
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    80
instance proof
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    81
qed (simp add: equal equal_word_def word_uint_eq_iff)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    82
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    83
end
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
    84
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    85
notation fcomp (infixl "\<circ>>" 60)
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    86
notation scomp (infixl "\<circ>\<rightarrow>" 60)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    87
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    88
instantiation word :: ("{len0, typerep}") random
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    89
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    90
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    91
definition
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    92
  "random_word i = Random.range i \<circ>\<rightarrow> (\<lambda>k. Pair (
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 49834
diff changeset
    93
     let j = word_of_int (int_of_integer (integer_of_natural k)) :: 'a word
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    94
     in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    95
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    96
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    97
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    98
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    99
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
   100
no_notation fcomp (infixl "\<circ>>" 60)
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
   101
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   102
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   103
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   104
subsection {* Type conversions and casting *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   105
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   106
definition sint :: "'a :: len word => int"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   107
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   108
  -- {* treats the most-significant-bit as a sign bit *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   109
  sint_uint: "sint w = sbintrunc (len_of TYPE ('a) - 1) (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   110
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   111
definition unat :: "'a :: len0 word => nat"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   112
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   113
  "unat w = nat (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   114
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   115
definition uints :: "nat => int set"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   116
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   117
  -- "the sets of integers representing the words"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   118
  "uints n = range (bintrunc n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   119
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   120
definition sints :: "nat => int set"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   121
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   122
  "sints n = range (sbintrunc (n - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   123
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   124
definition unats :: "nat => nat set"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   125
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   126
  "unats n = {i. i < 2 ^ n}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   127
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   128
definition norm_sint :: "nat => int => int"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   129
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   130
  "norm_sint n w = (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   131
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   132
definition scast :: "'a :: len word => 'b :: len word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   133
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   134
  -- "cast a word to a different length"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   135
  "scast w = word_of_int (sint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   136
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   137
definition ucast :: "'a :: len0 word => 'b :: len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   138
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   139
  "ucast w = word_of_int (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   140
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   141
instantiation word :: (len0) size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   142
begin
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_size: "size (w :: 'a word) = len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   146
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   147
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   148
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   149
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   150
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   151
definition source_size :: "('a :: len0 word => 'b) => nat"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   152
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   153
  -- "whether a cast (or other) function is to a longer or shorter length"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   154
  "source_size c = (let arb = undefined ; x = c arb in size arb)"  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   155
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   156
definition target_size :: "('a => 'b :: len0 word) => nat"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   157
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   158
  "target_size c = size (c undefined)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   159
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   160
definition is_up :: "('a :: len0 word => 'b :: len0 word) => bool"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   161
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   162
  "is_up c \<longleftrightarrow> source_size c <= target_size c"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   163
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   164
definition is_down :: "('a :: len0 word => 'b :: len0 word) => bool"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   165
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   166
  "is_down c \<longleftrightarrow> target_size c <= source_size c"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   167
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   168
definition of_bl :: "bool list => 'a :: len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   169
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   170
  "of_bl bl = word_of_int (bl_to_bin bl)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   171
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   172
definition to_bl :: "'a :: len0 word => bool list"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   173
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   174
  "to_bl w = bin_to_bl (len_of TYPE ('a)) (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   175
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   176
definition word_reverse :: "'a :: len0 word => 'a word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   177
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   178
  "word_reverse w = of_bl (rev (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   179
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   180
definition word_int_case :: "(int => 'b) => ('a :: len0 word) => 'b" 
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   181
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   182
  "word_int_case f w = f (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   183
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   184
translations
46136
a3d4cf5203f5 recovered case syntax for of_int, also with source positions (appears to be unused nonetheless);
wenzelm
parents: 46124
diff changeset
   185
  "case x of XCONST of_int y => b" == "CONST word_int_case (%y. b) x"
a3d4cf5203f5 recovered case syntax for of_int, also with source positions (appears to be unused nonetheless);
wenzelm
parents: 46124
diff changeset
   186
  "case x of (XCONST of_int :: 'a) y => b" => "CONST word_int_case (%y. b) x"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   187
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   188
subsection {* Type-definition locale instantiations *}
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   189
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   190
lemma word_size_gt_0 [iff]: "0 < size (w::'a::len word)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   191
  by (fact xtr1 [OF word_size len_gt_0])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   192
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   193
lemmas lens_gt_0 = word_size_gt_0 len_gt_0
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   194
lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0]
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   195
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   196
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   197
  by (simp add: uints_def range_bintrunc)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   198
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   199
lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   200
  by (simp add: sints_def range_sbintrunc)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   201
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   202
lemma 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   203
  uint_0:"0 <= uint x" and 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   204
  uint_lt: "uint (x::'a::len0 word) < 2 ^ len_of TYPE('a)"
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
   205
  by (auto simp: uint [unfolded atLeastLessThan_iff])
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   206
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   207
lemma uint_mod_same:
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   208
  "uint x mod 2 ^ len_of TYPE('a) = uint (x::'a::len0 word)"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   209
  by (simp add: int_mod_eq uint_lt uint_0)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   210
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   211
lemma td_ext_uint: 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   212
  "td_ext (uint :: 'a word => int) word_of_int (uints (len_of TYPE('a::len0))) 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   213
    (%w::int. w mod 2 ^ len_of TYPE('a))"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   214
  apply (unfold td_ext_def')
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   215
  apply (simp add: uints_num word_of_int_def bintrunc_mod2p)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   216
  apply (simp add: uint_mod_same uint_0 uint_lt
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   217
                   word.uint_inverse word.Abs_word_inverse int_mod_lem)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   218
  done
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   219
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   220
interpretation word_uint:
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   221
  td_ext "uint::'a::len0 word \<Rightarrow> int" 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   222
         word_of_int 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   223
         "uints (len_of TYPE('a::len0))"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   224
         "\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   225
  by (rule td_ext_uint)
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   226
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   227
lemmas td_uint = word_uint.td_thm
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   228
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   229
lemmas int_word_uint = word_uint.eq_norm
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   230
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   231
lemmas td_ext_ubin = td_ext_uint 
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
   232
  [unfolded len_gt_0 no_bintr_alt1 [symmetric]]
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   233
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   234
interpretation word_ubin:
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   235
  td_ext "uint::'a::len0 word \<Rightarrow> int" 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   236
         word_of_int 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   237
         "uints (len_of TYPE('a::len0))"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   238
         "bintrunc (len_of TYPE('a::len0))"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   239
  by (rule td_ext_ubin)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   240
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   241
lemma split_word_all:
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   242
  "(\<And>x::'a::len0 word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   243
proof
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   244
  fix x :: "'a word"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   245
  assume "\<And>x. PROP P (word_of_int x)"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   246
  hence "PROP P (word_of_int (uint x))" .
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   247
  thus "PROP P x" by simp
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   248
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   249
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   250
subsection {* Correspondence relation for theorem transfer *}
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   251
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   252
definition cr_word :: "int \<Rightarrow> 'a::len0 word \<Rightarrow> bool"
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   253
where
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   254
  "cr_word = (\<lambda>x y. word_of_int x = y)"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   255
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   256
lemma Quotient_word:
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   257
  "Quotient (\<lambda>x y. bintrunc (len_of TYPE('a)) x = bintrunc (len_of TYPE('a)) y)
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   258
    word_of_int uint (cr_word :: _ \<Rightarrow> 'a::len0 word \<Rightarrow> bool)"
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   259
  unfolding Quotient_alt_def cr_word_def
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   260
  by (simp add: word_ubin.norm_eq_iff)
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   261
47377
360d7ed4cc0f use standard quotient lemmas to generate transfer rules
huffman
parents: 47374
diff changeset
   262
lemma reflp_word:
360d7ed4cc0f use standard quotient lemmas to generate transfer rules
huffman
parents: 47374
diff changeset
   263
  "reflp (\<lambda>x y. bintrunc (len_of TYPE('a::len0)) x = bintrunc (len_of TYPE('a)) y)"
360d7ed4cc0f use standard quotient lemmas to generate transfer rules
huffman
parents: 47374
diff changeset
   264
  by (simp add: reflp_def)
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   265
47941
22e001795641 don't generate code in Word because it breaks the current code setup
kuncar
parents: 47611
diff changeset
   266
setup_lifting(no_code) Quotient_word reflp_word
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   267
47521
69f95ac85c3d tuned the setup of lifting; generate transfer rules for typedef and Quotient thms
kuncar
parents: 47387
diff changeset
   268
text {* TODO: The next lemma could be generated automatically. *}
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   269
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   270
lemma uint_transfer [transfer_rule]:
51375
d9e62d9c98de patch Isabelle ditribution to conform to changes regarding the parametricity
kuncar
parents: 51286
diff changeset
   271
  "(fun_rel pcr_word op =) (bintrunc (len_of TYPE('a)))
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   272
    (uint :: 'a::len0 word \<Rightarrow> int)"
51375
d9e62d9c98de patch Isabelle ditribution to conform to changes regarding the parametricity
kuncar
parents: 51286
diff changeset
   273
  unfolding fun_rel_def word.pcr_cr_eq cr_word_def by (simp add: word_ubin.eq_norm)
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   274
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   275
subsection  "Arithmetic operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   276
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   277
lift_definition word_succ :: "'a::len0 word \<Rightarrow> 'a word" is "\<lambda>x. x + 1"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   278
  by (metis bintr_ariths(6))
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   279
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   280
lift_definition word_pred :: "'a::len0 word \<Rightarrow> 'a word" is "\<lambda>x. x - 1"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   281
  by (metis bintr_ariths(7))
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   282
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   283
instantiation word :: (len0) "{neg_numeral, Divides.div, comm_monoid_mult, comm_ring}"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   284
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   285
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   286
lift_definition zero_word :: "'a word" is "0" .
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   287
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   288
lift_definition one_word :: "'a word" is "1" .
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   289
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   290
lift_definition plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "op +"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   291
  by (metis bintr_ariths(2))
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   292
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   293
lift_definition minus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "op -"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   294
  by (metis bintr_ariths(3))
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   295
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   296
lift_definition uminus_word :: "'a word \<Rightarrow> 'a word" is uminus
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   297
  by (metis bintr_ariths(5))
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   298
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   299
lift_definition times_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "op *"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   300
  by (metis bintr_ariths(4))
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   301
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   302
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   303
  word_div_def: "a div b = word_of_int (uint a div uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   304
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   305
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   306
  word_mod_def: "a mod b = word_of_int (uint a mod uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   307
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   308
instance
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   309
  by default (transfer, simp add: algebra_simps)+
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   310
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   311
end
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   312
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   313
text {* Legacy theorems: *}
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   314
47611
e3c699a1fae6 add code lemmas for word operations
huffman
parents: 47567
diff changeset
   315
lemma word_arith_wis [code]: shows
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   316
  word_add_def: "a + b = word_of_int (uint a + uint b)" and
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   317
  word_sub_wi: "a - b = word_of_int (uint a - uint b)" and
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   318
  word_mult_def: "a * b = word_of_int (uint a * uint b)" and
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   319
  word_minus_def: "- a = word_of_int (- uint a)" and
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   320
  word_succ_alt: "word_succ a = word_of_int (uint a + 1)" and
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   321
  word_pred_alt: "word_pred a = word_of_int (uint a - 1)" and
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   322
  word_0_wi: "0 = word_of_int 0" and
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   323
  word_1_wi: "1 = word_of_int 1"
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   324
  unfolding plus_word_def minus_word_def times_word_def uminus_word_def
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   325
  unfolding word_succ_def word_pred_def zero_word_def one_word_def
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   326
  by simp_all
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   327
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   328
lemmas arths = 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   329
  bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1], folded word_ubin.eq_norm]
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   330
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   331
lemma wi_homs: 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   332
  shows
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   333
  wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   334
  wi_hom_sub: "word_of_int a - word_of_int b = word_of_int (a - b)" and
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   335
  wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   336
  wi_hom_neg: "- word_of_int a = word_of_int (- a)" and
46000
871bdab23f5c remove some uses of Int.succ and Int.pred
huffman
parents: 45998
diff changeset
   337
  wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" and
871bdab23f5c remove some uses of Int.succ and Int.pred
huffman
parents: 45998
diff changeset
   338
  wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a - 1)"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   339
  by (transfer, simp)+
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   340
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   341
lemmas wi_hom_syms = wi_homs [symmetric]
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   342
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   343
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi
46009
5cb7ef5bfef2 remove duplicate lemma lists
huffman
parents: 46001
diff changeset
   344
5cb7ef5bfef2 remove duplicate lemma lists
huffman
parents: 46001
diff changeset
   345
lemmas word_of_int_hom_syms = word_of_int_homs [symmetric]
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   346
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   347
instance word :: (len) comm_ring_1
45810
024947a0e492 prove class instances without extra lemmas
huffman
parents: 45809
diff changeset
   348
proof
024947a0e492 prove class instances without extra lemmas
huffman
parents: 45809
diff changeset
   349
  have "0 < len_of TYPE('a)" by (rule len_gt_0)
024947a0e492 prove class instances without extra lemmas
huffman
parents: 45809
diff changeset
   350
  then show "(0::'a word) \<noteq> 1"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   351
    by - (transfer, auto simp add: gr0_conv_Suc)
45810
024947a0e492 prove class instances without extra lemmas
huffman
parents: 45809
diff changeset
   352
qed
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   353
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   354
lemma word_of_nat: "of_nat n = word_of_int (int n)"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   355
  by (induct n) (auto simp add : word_of_int_hom_syms)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   356
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   357
lemma word_of_int: "of_int = word_of_int"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   358
  apply (rule ext)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   359
  apply (case_tac x rule: int_diff_cases)
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   360
  apply (simp add: word_of_nat wi_hom_sub)
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   361
  done
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   362
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   363
definition udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50)
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   364
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   365
  "a udvd b = (EX n>=0. uint b = n * uint a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   366
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   367
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   368
subsection "Ordering"
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   369
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   370
instantiation word :: (len0) linorder
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   371
begin
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   372
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   373
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   374
  word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   375
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   376
definition
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   377
  word_less_def: "a < b \<longleftrightarrow> uint a < uint b"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   378
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   379
instance
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   380
  by default (auto simp: word_less_def word_le_def)
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   381
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   382
end
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   383
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   384
definition word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50)
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   385
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   386
  "a <=s b = (sint a <= sint b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   387
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   388
definition word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50)
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   389
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   390
  "(x <s y) = (x <=s y & x ~= y)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   391
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   392
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   393
subsection "Bit-wise operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   394
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   395
instantiation word :: (len0) bits
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   396
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   397
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   398
lift_definition bitNOT_word :: "'a word \<Rightarrow> 'a word" is bitNOT
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   399
  by (metis bin_trunc_not)
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   400
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   401
lift_definition bitAND_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is bitAND
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   402
  by (metis bin_trunc_and)
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   403
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   404
lift_definition bitOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is bitOR
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   405
  by (metis bin_trunc_or)
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   406
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   407
lift_definition bitXOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is bitXOR
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   408
  by (metis bin_trunc_xor)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   409
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   410
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   411
  word_test_bit_def: "test_bit a = bin_nth (uint a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   412
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   413
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   414
  word_set_bit_def: "set_bit a n x =
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54743
diff changeset
   415
   word_of_int (bin_sc n x (uint a))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   416
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   417
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   418
  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
   419
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   420
definition
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54743
diff changeset
   421
  word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   422
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   423
definition shiftl1 :: "'a word \<Rightarrow> 'a word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   424
where
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54743
diff changeset
   425
  "shiftl1 w = word_of_int (uint w BIT False)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   426
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   427
definition shiftr1 :: "'a word \<Rightarrow> 'a word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   428
where
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   429
  -- "shift right as unsigned or as signed, ie logical or arithmetic"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   430
  "shiftr1 w = word_of_int (bin_rest (uint w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   431
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   432
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   433
  shiftl_def: "w << n = (shiftl1 ^^ n) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   434
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   435
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   436
  shiftr_def: "w >> n = (shiftr1 ^^ n) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   437
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   438
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   439
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   440
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   441
47611
e3c699a1fae6 add code lemmas for word operations
huffman
parents: 47567
diff changeset
   442
lemma [code]: shows
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   443
  word_not_def: "NOT (a::'a::len0 word) = word_of_int (NOT (uint a))" and
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   444
  word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)" and
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   445
  word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)" and
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   446
  word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   447
  unfolding bitNOT_word_def bitAND_word_def bitOR_word_def bitXOR_word_def
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   448
  by simp_all
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   449
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   450
instantiation word :: (len) bitss
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   451
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   452
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   453
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   454
  word_msb_def: 
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   455
  "msb a \<longleftrightarrow> bin_sign (sint a) = -1"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   456
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   457
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   458
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   459
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   460
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   461
definition setBit :: "'a :: len0 word => nat => 'a word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   462
where 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   463
  "setBit w n = set_bit w n True"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   464
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   465
definition clearBit :: "'a :: len0 word => nat => 'a word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   466
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   467
  "clearBit w n = set_bit w n False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   468
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   469
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   470
subsection "Shift operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   471
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   472
definition sshiftr1 :: "'a :: len word => 'a word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   473
where 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   474
  "sshiftr1 w = word_of_int (bin_rest (sint w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   475
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   476
definition bshiftr1 :: "bool => 'a :: len word => 'a word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   477
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   478
  "bshiftr1 b w = of_bl (b # butlast (to_bl w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   479
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   480
definition sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55)
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   481
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   482
  "w >>> n = (sshiftr1 ^^ n) w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   483
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   484
definition mask :: "nat => 'a::len word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   485
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   486
  "mask n = (1 << n) - 1"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   487
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   488
definition revcast :: "'a :: len0 word => 'b :: len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   489
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   490
  "revcast w =  of_bl (takefill False (len_of TYPE('b)) (to_bl w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   491
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   492
definition slice1 :: "nat => 'a :: len0 word => 'b :: len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   493
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   494
  "slice1 n w = of_bl (takefill False n (to_bl w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   495
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   496
definition slice :: "nat => 'a :: len0 word => 'b :: len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   497
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   498
  "slice n w = slice1 (size w - n) w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   499
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   500
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   501
subsection "Rotation"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   502
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   503
definition rotater1 :: "'a list => 'a list"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   504
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   505
  "rotater1 ys = 
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   506
    (case ys of [] => [] | x # xs => last ys # butlast ys)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   507
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   508
definition rotater :: "nat => 'a list => 'a list"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   509
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   510
  "rotater n = rotater1 ^^ n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   511
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   512
definition word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   513
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   514
  "word_rotr n w = of_bl (rotater n (to_bl w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   515
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   516
definition word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   517
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   518
  "word_rotl n w = of_bl (rotate n (to_bl w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   519
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   520
definition word_roti :: "int => 'a :: len0 word => 'a :: len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   521
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   522
  "word_roti i w = (if i >= 0 then word_rotr (nat i) w
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   523
                    else word_rotl (nat (- i)) w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   524
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   525
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   526
subsection "Split and cat operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   527
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   528
definition word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   529
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   530
  "word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   531
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   532
definition word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   533
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   534
  "word_split a = 
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   535
   (case bin_split (len_of TYPE ('c)) (uint a) of 
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   536
     (u, v) => (word_of_int u, word_of_int v))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   537
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   538
definition word_rcat :: "'a :: len0 word list => 'b :: len0 word"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   539
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   540
  "word_rcat ws = 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   541
  word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   542
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   543
definition word_rsplit :: "'a :: len0 word => 'b :: len word list"
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   544
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   545
  "word_rsplit w = 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   546
  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
   547
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   548
definition max_word :: "'a::len word" -- "Largest representable machine integer."
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   549
where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   550
  "max_word = word_of_int (2 ^ len_of TYPE('a) - 1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   551
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   552
(* FIXME: only provide one theorem name *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   553
lemmas of_nth_def = word_set_bits_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   554
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
   555
subsection {* Theorems about typedefs *}
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
   556
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   557
lemma sint_sbintrunc': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   558
  "sint (word_of_int bin :: 'a word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   559
    (sbintrunc (len_of TYPE ('a :: len) - 1) bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   560
  unfolding sint_uint 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   561
  by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   562
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   563
lemma uint_sint: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   564
  "uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a :: len word))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   565
  unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   566
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   567
lemma bintr_uint:
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   568
  fixes w :: "'a::len0 word"
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   569
  shows "len_of TYPE('a) \<le> n \<Longrightarrow> bintrunc n (uint w) = uint w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   570
  apply (subst word_ubin.norm_Rep [symmetric]) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   571
  apply (simp only: bintrunc_bintrunc_min word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   572
  apply (simp add: min_max.inf_absorb2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   573
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   574
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   575
lemma wi_bintr:
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   576
  "len_of TYPE('a::len0) \<le> n \<Longrightarrow>
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   577
    word_of_int (bintrunc n w) = (word_of_int w :: 'a word)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   578
  by (clarsimp simp add: word_ubin.norm_eq_iff [symmetric] min_max.inf_absorb1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   579
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   580
lemma td_ext_sbin: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   581
  "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
   582
    (sbintrunc (len_of TYPE('a) - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   583
  apply (unfold td_ext_def' sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   584
  apply (simp add : word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   585
  apply (cases "len_of TYPE('a)")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   586
   apply (auto simp add : sints_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   587
  apply (rule sym [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   588
  apply (rule word_ubin.Abs_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   589
  apply (simp only: bintrunc_sbintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   590
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   591
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   592
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   593
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   594
lemmas td_ext_sint = td_ext_sbin 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   595
  [simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   596
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   597
(* We do sint before sbin, before sint is the user version
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   598
   and interpretations do not produce thm duplicates. I.e. 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   599
   we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   600
   because the latter is the same thm as the former *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   601
interpretation word_sint:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   602
  td_ext "sint ::'a::len word => int" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   603
          word_of_int 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   604
          "sints (len_of TYPE('a::len))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   605
          "%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
   606
               2 ^ (len_of TYPE('a::len) - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   607
  by (rule td_ext_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   608
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   609
interpretation word_sbin:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   610
  td_ext "sint ::'a::len word => int" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   611
          word_of_int 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   612
          "sints (len_of TYPE('a::len))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   613
          "sbintrunc (len_of TYPE('a::len) - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   614
  by (rule td_ext_sbin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   615
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   616
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   617
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   618
lemmas td_sint = word_sint.td
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   619
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   620
lemma to_bl_def': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   621
  "(to_bl :: 'a :: len0 word => bool list) =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   622
    bin_to_bl (len_of TYPE('a)) o uint"
44762
8f9d09241a68 tuned proofs;
wenzelm
parents: 42793
diff changeset
   623
  by (auto simp: to_bl_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   624
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   625
lemmas word_reverse_no_def [simp] = word_reverse_def [of "numeral w"] for w
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   626
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   627
lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   628
  by (fact uints_def [unfolded no_bintr_alt1])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   629
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   630
lemma word_numeral_alt:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   631
  "numeral b = word_of_int (numeral b)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   632
  by (induct b, simp_all only: numeral.simps word_of_int_homs)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   633
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   634
declare word_numeral_alt [symmetric, code_abbrev]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   635
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   636
lemma word_neg_numeral_alt:
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   637
  "- numeral b = word_of_int (- numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   638
  by (simp only: word_numeral_alt wi_hom_neg)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   639
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   640
declare word_neg_numeral_alt [symmetric, code_abbrev]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   641
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   642
lemma word_numeral_transfer [transfer_rule]:
51375
d9e62d9c98de patch Isabelle ditribution to conform to changes regarding the parametricity
kuncar
parents: 51286
diff changeset
   643
  "(fun_rel op = pcr_word) numeral numeral"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   644
  "(fun_rel op = pcr_word) (- numeral) (- numeral)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   645
  apply (simp_all add: fun_rel_def word.pcr_cr_eq cr_word_def)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   646
  using word_numeral_alt [symmetric] word_neg_numeral_alt [symmetric] by blast+
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
   647
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   648
lemma uint_bintrunc [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   649
  "uint (numeral bin :: 'a word) = 
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   650
    bintrunc (len_of TYPE ('a :: len0)) (numeral bin)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   651
  unfolding word_numeral_alt by (rule word_ubin.eq_norm)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   652
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   653
lemma uint_bintrunc_neg [simp]: "uint (- numeral bin :: 'a word) = 
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   654
    bintrunc (len_of TYPE ('a :: len0)) (- numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   655
  by (simp only: word_neg_numeral_alt word_ubin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   656
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   657
lemma sint_sbintrunc [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   658
  "sint (numeral bin :: 'a word) = 
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   659
    sbintrunc (len_of TYPE ('a :: len) - 1) (numeral bin)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   660
  by (simp only: word_numeral_alt word_sbin.eq_norm)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   661
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   662
lemma sint_sbintrunc_neg [simp]: "sint (- numeral bin :: 'a word) = 
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   663
    sbintrunc (len_of TYPE ('a :: len) - 1) (- numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   664
  by (simp only: word_neg_numeral_alt word_sbin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   665
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   666
lemma unat_bintrunc [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   667
  "unat (numeral bin :: 'a :: len0 word) =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   668
    nat (bintrunc (len_of TYPE('a)) (numeral bin))"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   669
  by (simp only: unat_def uint_bintrunc)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   670
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   671
lemma unat_bintrunc_neg [simp]:
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   672
  "unat (- numeral bin :: 'a :: len0 word) =
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   673
    nat (bintrunc (len_of TYPE('a)) (- numeral bin))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   674
  by (simp only: unat_def uint_bintrunc_neg)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   675
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   676
lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 \<Longrightarrow> v = w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   677
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   678
  apply (rule word_uint.Rep_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   679
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   680
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   681
    apply (rule word_ubin.norm_Rep)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   682
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   683
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   684
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   685
lemma uint_ge_0 [iff]: "0 \<le> uint (x::'a::len0 word)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   686
  using word_uint.Rep [of x] by (simp add: uints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   687
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   688
lemma uint_lt2p [iff]: "uint (x::'a::len0 word) < 2 ^ len_of TYPE('a)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   689
  using word_uint.Rep [of x] by (simp add: uints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   690
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   691
lemma sint_ge: "- (2 ^ (len_of TYPE('a) - 1)) \<le> sint (x::'a::len word)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   692
  using word_sint.Rep [of x] by (simp add: sints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   693
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   694
lemma sint_lt: "sint (x::'a::len word) < 2 ^ (len_of TYPE('a) - 1)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   695
  using word_sint.Rep [of x] by (simp add: sints_num)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   696
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   697
lemma sign_uint_Pls [simp]: 
46604
9f9e85264e4d make uses of bin_sign respect int/bin distinction
huffman
parents: 46603
diff changeset
   698
  "bin_sign (uint x) = 0"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   699
  by (simp add: sign_Pls_ge_0)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   700
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   701
lemma uint_m2p_neg: "uint (x::'a::len0 word) - 2 ^ len_of TYPE('a) < 0"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   702
  by (simp only: diff_less_0_iff_less uint_lt2p)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   703
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   704
lemma uint_m2p_not_non_neg:
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   705
  "\<not> 0 \<le> uint (x::'a::len0 word) - 2 ^ len_of TYPE('a)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   706
  by (simp only: not_le uint_m2p_neg)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   707
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   708
lemma lt2p_lem:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   709
  "len_of TYPE('a) <= n \<Longrightarrow> uint (w :: 'a :: len0 word) < 2 ^ n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   710
  by (rule xtr8 [OF _ uint_lt2p]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   711
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   712
lemma uint_le_0_iff [simp]: "uint x \<le> 0 \<longleftrightarrow> uint x = 0"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   713
  by (fact uint_ge_0 [THEN leD, THEN linorder_antisym_conv1])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   714
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   715
lemma uint_nat: "uint w = int (unat w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   716
  unfolding unat_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   717
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   718
lemma uint_numeral:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   719
  "uint (numeral b :: 'a :: len0 word) = numeral b mod 2 ^ len_of TYPE('a)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   720
  unfolding word_numeral_alt
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   721
  by (simp only: int_word_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   722
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   723
lemma uint_neg_numeral:
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   724
  "uint (- numeral b :: 'a :: len0 word) = - numeral b mod 2 ^ len_of TYPE('a)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   725
  unfolding word_neg_numeral_alt
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   726
  by (simp only: int_word_uint)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   727
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   728
lemma unat_numeral: 
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   729
  "unat (numeral b::'a::len0 word) = numeral b mod 2 ^ len_of TYPE ('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   730
  apply (unfold unat_def)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   731
  apply (clarsimp simp only: uint_numeral)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   732
  apply (rule nat_mod_distrib [THEN trans])
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   733
    apply (rule zero_le_numeral)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   734
   apply (simp_all add: nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   735
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   736
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   737
lemma sint_numeral: "sint (numeral b :: 'a :: len word) = (numeral b + 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   738
    2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   739
    2 ^ (len_of TYPE('a) - 1)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   740
  unfolding word_numeral_alt by (rule int_word_sint)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   741
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   742
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0"
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
   743
  unfolding word_0_wi ..
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
   744
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   745
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1"
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
   746
  unfolding word_1_wi ..
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
   747
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   748
lemma word_of_int_neg_1 [simp]: "word_of_int (- 1) = - 1"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   749
  by (simp add: wi_hom_syms)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   750
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   751
lemma word_of_int_numeral [simp] : 
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   752
  "(word_of_int (numeral bin) :: 'a :: len0 word) = (numeral bin)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   753
  unfolding word_numeral_alt ..
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   754
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   755
lemma word_of_int_neg_numeral [simp]:
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   756
  "(word_of_int (- numeral bin) :: 'a :: len0 word) = (- numeral bin)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   757
  unfolding word_numeral_alt wi_hom_syms ..
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   758
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   759
lemma word_int_case_wi: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   760
  "word_int_case f (word_of_int i :: 'b word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   761
    f (i mod 2 ^ len_of TYPE('b::len0))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   762
  unfolding word_int_case_def by (simp add: word_uint.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   763
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   764
lemma word_int_split: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   765
  "P (word_int_case f x) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   766
    (ALL i. x = (word_of_int i :: 'b :: len0 word) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   767
      0 <= i & i < 2 ^ len_of TYPE('b) --> P (f i))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   768
  unfolding word_int_case_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   769
  by (auto simp: word_uint.eq_norm int_mod_eq')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   770
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   771
lemma word_int_split_asm: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   772
  "P (word_int_case f x) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   773
    (~ (EX n. x = (word_of_int n :: 'b::len0 word) &
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   774
      0 <= n & n < 2 ^ len_of TYPE('b::len0) & ~ P (f n)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   775
  unfolding word_int_case_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   776
  by (auto simp: word_uint.eq_norm int_mod_eq')
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   777
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   778
lemmas uint_range' = word_uint.Rep [unfolded uints_num mem_Collect_eq]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   779
lemmas sint_range' = word_sint.Rep [unfolded One_nat_def sints_num mem_Collect_eq]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   780
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   781
lemma uint_range_size: "0 <= uint w & uint w < 2 ^ size w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   782
  unfolding word_size by (rule uint_range')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   783
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   784
lemma sint_range_size:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   785
  "- (2 ^ (size w - Suc 0)) <= sint w & sint w < 2 ^ (size w - Suc 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   786
  unfolding word_size by (rule sint_range')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   787
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   788
lemma sint_above_size: "2 ^ (size (w::'a::len word) - 1) \<le> x \<Longrightarrow> sint w < x"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   789
  unfolding word_size by (rule less_le_trans [OF sint_lt])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   790
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   791
lemma sint_below_size:
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   792
  "x \<le> - (2 ^ (size (w::'a::len word) - 1)) \<Longrightarrow> x \<le> sint w"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   793
  unfolding word_size by (rule order_trans [OF _ sint_ge])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   794
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
   795
subsection {* Testing bits *}
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
   796
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   797
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
   798
  unfolding word_test_bit_def by (simp add: bin_nth_eq_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   799
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   800
lemma test_bit_size [rule_format] : "(w::'a::len0 word) !! n --> n < size w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   801
  apply (unfold word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   802
  apply (subst word_ubin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   803
  apply (simp only: nth_bintr word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   804
  apply fast
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   805
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   806
46021
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
   807
lemma word_eq_iff:
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
   808
  fixes x y :: "'a::len0 word"
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
   809
  shows "x = y \<longleftrightarrow> (\<forall>n<len_of TYPE('a). x !! n = y !! n)"
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
   810
  unfolding uint_inject [symmetric] bin_eq_iff word_test_bit_def [symmetric]
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
   811
  by (metis test_bit_size [unfolded word_size])
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
   812
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
   813
lemma word_eqI [rule_format]:
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   814
  fixes u :: "'a::len0 word"
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   815
  shows "(ALL n. n < size u --> u !! n = v !! n) \<Longrightarrow> u = v"
46021
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
   816
  by (simp add: word_size word_eq_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   817
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   818
lemma word_eqD: "(u::'a::len0 word) = v \<Longrightarrow> u !! x = v !! x"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   819
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   820
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   821
lemma test_bit_bin': "w !! n = (n < size w & bin_nth (uint w) n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   822
  unfolding word_test_bit_def word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   823
  by (simp add: nth_bintr [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   824
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   825
lemmas test_bit_bin = test_bit_bin' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   826
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   827
lemma bin_nth_uint_imp:
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   828
  "bin_nth (uint (w::'a::len0 word)) n \<Longrightarrow> n < len_of TYPE('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   829
  apply (rule nth_bintr [THEN iffD1, THEN conjunct1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   830
  apply (subst word_ubin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   831
  apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   832
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   833
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   834
lemma bin_nth_sint:
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   835
  fixes w :: "'a::len word"
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   836
  shows "len_of TYPE('a) \<le> n \<Longrightarrow>
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   837
    bin_nth (sint w) n = bin_nth (sint w) (len_of TYPE('a) - 1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   838
  apply (subst word_sbin.norm_Rep [symmetric])
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
   839
  apply (auto simp add: nth_sbintr)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   840
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   841
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   842
(* type definitions theorem for in terms of equivalent bool list *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   843
lemma td_bl: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   844
  "type_definition (to_bl :: 'a::len0 word => bool list) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   845
                   of_bl  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   846
                   {bl. length bl = len_of TYPE('a)}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   847
  apply (unfold type_definition_def of_bl_def to_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   848
  apply (simp add: word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   849
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   850
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   851
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   852
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   853
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   854
interpretation word_bl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   855
  type_definition "to_bl :: 'a::len0 word => bool list"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   856
                  of_bl  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   857
                  "{bl. length bl = len_of TYPE('a::len0)}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   858
  by (rule td_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   859
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
   860
lemmas word_bl_Rep' = word_bl.Rep [unfolded mem_Collect_eq, iff]
45538
1fffa81b9b83 eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents: 45529
diff changeset
   861
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   862
lemma word_size_bl: "size w = size (to_bl w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   863
  unfolding word_size by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   864
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   865
lemma to_bl_use_of_bl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   866
  "(to_bl w = bl) = (w = of_bl bl \<and> length bl = length (to_bl w))"
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
   867
  by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   868
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   869
lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   870
  unfolding word_reverse_def by (simp add: word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   871
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   872
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   873
  unfolding word_reverse_def by (simp add : word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   874
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   875
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   876
  by (metis word_rev_rev)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   877
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   878
lemma word_rev_gal': "u = word_reverse w \<Longrightarrow> w = word_reverse u"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   879
  by simp
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   880
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   881
lemma length_bl_gt_0 [iff]: "0 < length (to_bl (x::'a::len word))"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   882
  unfolding word_bl_Rep' by (rule len_gt_0)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   883
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   884
lemma bl_not_Nil [iff]: "to_bl (x::'a::len word) \<noteq> []"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   885
  by (fact length_bl_gt_0 [unfolded length_greater_0_conv])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   886
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   887
lemma length_bl_neq_0 [iff]: "length (to_bl (x::'a::len word)) \<noteq> 0"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   888
  by (fact length_bl_gt_0 [THEN gr_implies_not0])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   889
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   890
lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = -1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   891
  apply (unfold to_bl_def sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   892
  apply (rule trans [OF _ bl_sbin_sign])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   893
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   894
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   895
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   896
lemma of_bl_drop': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   897
  "lend = length bl - len_of TYPE ('a :: len0) \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   898
    of_bl (drop lend bl) = (of_bl bl :: 'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   899
  apply (unfold of_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   900
  apply (clarsimp simp add : trunc_bl2bin [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   901
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   902
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   903
lemma test_bit_of_bl:  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   904
  "(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
   905
  apply (unfold of_bl_def word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   906
  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
   907
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   908
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   909
lemma no_of_bl: 
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   910
  "(numeral bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) (numeral bin))"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   911
  unfolding of_bl_def by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   912
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   913
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   914
  unfolding word_size to_bl_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   915
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   916
lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   917
  unfolding uint_bl by (simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   918
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   919
lemma to_bl_of_bin: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   920
  "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
   921
  unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   922
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   923
lemma to_bl_numeral [simp]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   924
  "to_bl (numeral bin::'a::len0 word) =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   925
    bin_to_bl (len_of TYPE('a)) (numeral bin)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   926
  unfolding word_numeral_alt by (rule to_bl_of_bin)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   927
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   928
lemma to_bl_neg_numeral [simp]:
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   929
  "to_bl (- numeral bin::'a::len0 word) =
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
   930
    bin_to_bl (len_of TYPE('a)) (- numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   931
  unfolding word_neg_numeral_alt by (rule to_bl_of_bin)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   932
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   933
lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   934
  unfolding uint_bl by (simp add : word_size)
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
   935
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
   936
lemma uint_bl_bin:
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
   937
  fixes x :: "'a::len0 word"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
   938
  shows "bl_to_bin (bin_to_bl (len_of TYPE('a)) (uint x)) = uint x"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
   939
  by (rule trans [OF bin_bl_bin word_ubin.norm_Rep])
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   940
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   941
(* naturals *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   942
lemma uints_unats: "uints n = int ` unats n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   943
  apply (unfold unats_def uints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   944
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   945
  apply (rule_tac image_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   946
  apply (erule_tac nat_0_le [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   947
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   948
  apply (erule_tac nat_less_iff [THEN iffD2])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   949
  apply (rule_tac [2] zless_nat_eq_int_zless [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   950
  apply (auto simp add : nat_power_eq int_power)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   951
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   952
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   953
lemma unats_uints: "unats n = nat ` uints n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   954
  by (auto simp add : uints_unats image_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   955
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
   956
lemmas bintr_num = word_ubin.norm_eq_iff
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   957
  [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
   958
lemmas sbintr_num = word_sbin.norm_eq_iff
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   959
  [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   960
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   961
lemma num_of_bintr':
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   962
  "bintrunc (len_of TYPE('a :: len0)) (numeral a) = (numeral b) \<Longrightarrow> 
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   963
    numeral a = (numeral b :: 'a word)"
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
   964
  unfolding bintr_num by (erule subst, simp)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   965
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   966
lemma num_of_sbintr':
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   967
  "sbintrunc (len_of TYPE('a :: len) - 1) (numeral a) = (numeral b) \<Longrightarrow> 
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   968
    numeral a = (numeral b :: 'a word)"
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
   969
  unfolding sbintr_num by (erule subst, simp)
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
   970
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
   971
lemma num_abs_bintr:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   972
  "(numeral x :: 'a word) =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   973
    word_of_int (bintrunc (len_of TYPE('a::len0)) (numeral x))"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   974
  by (simp only: word_ubin.Abs_norm word_numeral_alt)
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
   975
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
   976
lemma num_abs_sbintr:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   977
  "(numeral x :: 'a word) =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   978
    word_of_int (sbintrunc (len_of TYPE('a::len) - 1) (numeral x))"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   979
  by (simp only: word_sbin.Abs_norm word_numeral_alt)
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
   980
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   981
(** 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
   982
  thus in "cast w = w, the type means cast to length of w! **)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   983
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   984
lemma ucast_id: "ucast w = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   985
  unfolding ucast_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   986
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   987
lemma scast_id: "scast w = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   988
  unfolding scast_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   989
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   990
lemma ucast_bl: "ucast w = of_bl (to_bl w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   991
  unfolding ucast_def of_bl_def uint_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   992
  by (auto simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   993
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   994
lemma nth_ucast: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   995
  "(ucast w::'a::len0 word) !! n = (w !! n & n < len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   996
  apply (unfold ucast_def test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   997
  apply (simp add: word_ubin.eq_norm nth_bintr word_size) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   998
  apply (fast elim!: bin_nth_uint_imp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   999
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1000
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1001
(* for literal u(s)cast *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1002
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  1003
lemma ucast_bintr [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1004
  "ucast (numeral w ::'a::len0 word) = 
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1005
   word_of_int (bintrunc (len_of TYPE('a)) (numeral w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1006
  unfolding ucast_def by simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1007
(* TODO: neg_numeral *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1008
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  1009
lemma scast_sbintr [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1010
  "scast (numeral w ::'a::len word) = 
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1011
   word_of_int (sbintrunc (len_of TYPE('a) - Suc 0) (numeral w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1012
  unfolding scast_def by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1013
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1014
lemma source_size: "source_size (c::'a::len0 word \<Rightarrow> _) = len_of TYPE('a)"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1015
  unfolding source_size_def word_size Let_def ..
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1016
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1017
lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len0 word) = len_of TYPE('b)"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1018
  unfolding target_size_def word_size Let_def ..
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1019
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1020
lemma is_down:
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1021
  fixes c :: "'a::len0 word \<Rightarrow> 'b::len0 word"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1022
  shows "is_down c \<longleftrightarrow> len_of TYPE('b) \<le> len_of TYPE('a)"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1023
  unfolding is_down_def source_size target_size ..
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1024
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1025
lemma is_up:
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1026
  fixes c :: "'a::len0 word \<Rightarrow> 'b::len0 word"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1027
  shows "is_up c \<longleftrightarrow> len_of TYPE('a) \<le> len_of TYPE('b)"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1028
  unfolding is_up_def source_size target_size ..
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1029
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1030
lemmas is_up_down = trans [OF is_up is_down [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1031
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1032
lemma down_cast_same [OF refl]: "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc = scast"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1033
  apply (unfold is_down)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1034
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1035
  apply (rule ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1036
  apply (unfold ucast_def scast_def uint_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1037
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1038
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1039
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1040
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1041
lemma word_rev_tf:
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1042
  "to_bl (of_bl bl::'a::len0 word) =
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1043
    rev (takefill False (len_of TYPE('a)) (rev bl))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1044
  unfolding of_bl_def uint_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1045
  by (clarsimp simp add: bl_bin_bl_rtf word_ubin.eq_norm word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1046
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1047
lemma word_rep_drop:
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1048
  "to_bl (of_bl bl::'a::len0 word) =
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1049
    replicate (len_of TYPE('a) - length bl) False @
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1050
    drop (length bl - len_of TYPE('a)) bl"
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1051
  by (simp add: word_rev_tf takefill_alt rev_take)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1052
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1053
lemma to_bl_ucast: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1054
  "to_bl (ucast (w::'b::len0 word) ::'a::len0 word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1055
   replicate (len_of TYPE('a) - len_of TYPE('b)) False @
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1056
   drop (len_of TYPE('b) - len_of TYPE('a)) (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1057
  apply (unfold ucast_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1058
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1059
   apply (rule word_rep_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1060
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1061
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1062
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1063
lemma ucast_up_app [OF refl]:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1064
  "uc = ucast \<Longrightarrow> source_size uc + n = target_size uc \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1065
    to_bl (uc w) = replicate n False @ (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1066
  by (auto simp add : source_size target_size to_bl_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1067
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1068
lemma ucast_down_drop [OF refl]:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1069
  "uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1070
    to_bl (uc w) = drop n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1071
  by (auto simp add : source_size target_size to_bl_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1072
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1073
lemma scast_down_drop [OF refl]:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1074
  "sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1075
    to_bl (sc w) = drop n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1076
  apply (subgoal_tac "sc = ucast")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1077
   apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1078
   apply simp
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1079
   apply (erule ucast_down_drop)
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1080
  apply (rule down_cast_same [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1081
  apply (simp add : source_size target_size is_down)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1082
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1083
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1084
lemma sint_up_scast [OF refl]:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1085
  "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1086
  apply (unfold is_up)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1087
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1088
  apply (simp add: scast_def word_sbin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1089
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1090
    prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1091
    apply (rule word_sbin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1092
   apply (rule sbintrunc_sbintrunc_l)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1093
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1094
   apply (subst word_sbin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1095
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1096
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1097
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1098
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1099
lemma uint_up_ucast [OF refl]:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1100
  "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1101
  apply (unfold is_up)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1102
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1103
  apply (rule bin_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1104
  apply (fold word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1105
  apply (auto simp add: nth_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1106
  apply (auto simp add: test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1107
  done
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1108
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1109
lemma ucast_up_ucast [OF refl]:
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1110
  "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> ucast (uc w) = ucast w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1111
  apply (simp (no_asm) add: ucast_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1112
  apply (clarsimp simp add: uint_up_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1113
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1114
    
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1115
lemma scast_up_scast [OF refl]:
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1116
  "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> scast (sc w) = scast w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1117
  apply (simp (no_asm) add: scast_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1118
  apply (clarsimp simp add: sint_up_scast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1119
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1120
    
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1121
lemma ucast_of_bl_up [OF refl]:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1122
  "w = of_bl bl \<Longrightarrow> size bl <= size w \<Longrightarrow> ucast w = of_bl bl"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1123
  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
  1124
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1125
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1126
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1127
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1128
lemmas isduu = is_up_down [where c = "ucast", THEN iffD2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1129
lemmas isdus = is_up_down [where c = "scast", THEN iffD2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1130
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1131
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1132
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1133
lemma up_ucast_surj:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1134
  "is_up (ucast :: 'b::len0 word => 'a::len0 word) \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1135
   surj (ucast :: 'a word => 'b word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1136
  by (rule surjI, erule ucast_up_ucast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1137
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1138
lemma up_scast_surj:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1139
  "is_up (scast :: 'b::len word => 'a::len word) \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1140
   surj (scast :: 'a word => 'b word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1141
  by (rule surjI, erule scast_up_scast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1142
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1143
lemma down_scast_inj:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1144
  "is_down (scast :: 'b::len word => 'a::len word) \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1145
   inj_on (ucast :: 'a word => 'b word) A"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1146
  by (rule inj_on_inverseI, erule scast_down_scast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1147
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1148
lemma down_ucast_inj:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1149
  "is_down (ucast :: 'b::len0 word => 'a::len0 word) \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1150
   inj_on (ucast :: 'a word => 'b word) A"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1151
  by (rule inj_on_inverseI, erule ucast_down_ucast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1152
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1153
lemma of_bl_append_same: "of_bl (X @ to_bl w) = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1154
  by (rule word_bl.Rep_eqD) (simp add: word_rep_drop)
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1155
46646
0abbf6dd09ee remove ill-formed lemma of_bl_no; adapt proofs
huffman
parents: 46645
diff changeset
  1156
lemma ucast_down_wi [OF refl]:
0abbf6dd09ee remove ill-formed lemma of_bl_no; adapt proofs
huffman
parents: 46645
diff changeset
  1157
  "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (word_of_int x) = word_of_int x"
0abbf6dd09ee remove ill-formed lemma of_bl_no; adapt proofs
huffman
parents: 46645
diff changeset
  1158
  apply (unfold is_down)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1159
  apply (clarsimp simp add: ucast_def word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1160
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1161
  apply (erule bintrunc_bintrunc_ge)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1162
  done
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1163
46646
0abbf6dd09ee remove ill-formed lemma of_bl_no; adapt proofs
huffman
parents: 46645
diff changeset
  1164
lemma ucast_down_no [OF refl]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1165
  "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (numeral bin) = numeral bin"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1166
  unfolding word_numeral_alt by clarify (rule ucast_down_wi)
46646
0abbf6dd09ee remove ill-formed lemma of_bl_no; adapt proofs
huffman
parents: 46645
diff changeset
  1167
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1168
lemma ucast_down_bl [OF refl]:
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1169
  "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl"
46646
0abbf6dd09ee remove ill-formed lemma of_bl_no; adapt proofs
huffman
parents: 46645
diff changeset
  1170
  unfolding of_bl_def by clarify (erule ucast_down_wi)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1171
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1172
lemmas slice_def' = slice_def [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1173
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1174
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1175
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
  1176
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1177
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1178
subsection {* Word Arithmetic *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1179
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1180
lemma word_less_alt: "(a < b) = (uint a < uint b)"
46012
8a070c62b548 simplify proof
huffman
parents: 46011
diff changeset
  1181
  unfolding word_less_def word_le_def by (simp add: less_le)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1182
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1183
lemma signed_linorder: "class.linorder word_sle word_sless"
46124
3ee75fe01986 misc tuning;
wenzelm
parents: 46064
diff changeset
  1184
  by default (unfold word_sle_def word_sless_def, auto)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1185
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1186
interpretation signed: linorder "word_sle" "word_sless"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1187
  by (rule signed_linorder)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1188
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1189
lemma udvdI: 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1190
  "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1191
  by (auto simp: udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1192
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1193
lemmas word_div_no [simp] = word_div_def [of "numeral a" "numeral b"] for a b
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1194
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1195
lemmas word_mod_no [simp] = word_mod_def [of "numeral a" "numeral b"] for a b
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1196
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1197
lemmas word_less_no [simp] = word_less_def [of "numeral a" "numeral b"] for a b
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1198
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1199
lemmas word_le_no [simp] = word_le_def [of "numeral a" "numeral b"] for a b
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1200
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1201
lemmas word_sless_no [simp] = word_sless_def [of "numeral a" "numeral b"] for a b
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1202
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1203
lemmas word_sle_no [simp] = word_sle_def [of "numeral a" "numeral b"] for a b
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1204
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  1205
lemma word_m1_wi: "- 1 = word_of_int (- 1)" 
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  1206
  using word_neg_numeral_alt [of Num.One] by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1207
46648
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1208
lemma word_0_bl [simp]: "of_bl [] = 0"
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1209
  unfolding of_bl_def by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1210
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1211
lemma word_1_bl: "of_bl [True] = 1" 
46648
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1212
  unfolding of_bl_def by (simp add: bl_to_bin_def)
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1213
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1214
lemma uint_eq_0 [simp]: "uint 0 = 0"
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1215
  unfolding word_0_wi word_ubin.eq_norm by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1216
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  1217
lemma of_bl_0 [simp]: "of_bl (replicate n False) = 0"
46648
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1218
  by (simp add: of_bl_def bl_to_bin_rep_False)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1219
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1220
lemma to_bl_0 [simp]:
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1221
  "to_bl (0::'a::len0 word) = replicate (len_of TYPE('a)) False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1222
  unfolding uint_bl
46617
8c5d10d41391 make bool list functions respect int/bin distinction
huffman
parents: 46604
diff changeset
  1223
  by (simp add: word_size bin_to_bl_zero)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1224
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1225
lemma uint_0_iff: "(uint x = 0) = (x = 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1226
  by (auto intro!: word_uint.Rep_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1227
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1228
lemma unat_0_iff: "(unat x = 0) = (x = 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1229
  unfolding unat_def by (auto simp add : nat_eq_iff uint_0_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1230
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1231
lemma unat_0 [simp]: "unat 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1232
  unfolding unat_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1233
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1234
lemma size_0_same': "size w = 0 \<Longrightarrow> w = (v :: 'a :: len0 word)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1235
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1236
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1237
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1238
    apply (rule word_uint.Rep_inverse)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1239
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1240
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1241
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1242
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  1243
lemmas size_0_same = size_0_same' [unfolded word_size]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1244
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1245
lemmas unat_eq_0 = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1246
lemmas unat_eq_zero = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1247
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1248
lemma unat_gt_0: "(0 < unat x) = (x ~= 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1249
by (auto simp: unat_0_iff [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1250
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1251
lemma ucast_0 [simp]: "ucast 0 = 0"
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  1252
  unfolding ucast_def by simp
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1253
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1254
lemma sint_0 [simp]: "sint 0 = 0"
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1255
  unfolding sint_uint by simp
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1256
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1257
lemma scast_0 [simp]: "scast 0 = 0"
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  1258
  unfolding scast_def by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1259
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1260
lemma sint_n1 [simp] : "sint -1 = -1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  1261
  unfolding word_m1_wi word_sbin.eq_norm by simp
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  1262
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  1263
lemma scast_n1 [simp]: "scast (- 1) = - 1"
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1264
  unfolding scast_def by simp
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1265
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1266
lemma uint_1 [simp]: "uint (1::'a::len word) = 1"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1267
  unfolding word_1_wi
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  1268
  by (simp add: word_ubin.eq_norm bintrunc_minus_simps del: word_of_int_1)
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1269
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1270
lemma unat_1 [simp]: "unat (1::'a::len word) = 1"
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1271
  unfolding unat_def by simp
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1272
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1273
lemma ucast_1 [simp]: "ucast (1::'a::len word) = 1"
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  1274
  unfolding ucast_def by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1275
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1276
(* now, to get the weaker results analogous to word_div/mod_def *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1277
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1278
lemmas word_arith_alts = 
46000
871bdab23f5c remove some uses of Int.succ and Int.pred
huffman
parents: 45998
diff changeset
  1279
  word_sub_wi
871bdab23f5c remove some uses of Int.succ and Int.pred
huffman
parents: 45998
diff changeset
  1280
  word_arith_wis (* FIXME: duplicate *)
871bdab23f5c remove some uses of Int.succ and Int.pred
huffman
parents: 45998
diff changeset
  1281
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1282
subsection  "Transferring goals from words to ints"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1283
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1284
lemma word_ths:  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1285
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1286
  word_succ_p1:   "word_succ a = a + 1" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1287
  word_pred_m1:   "word_pred a = a - 1" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1288
  word_pred_succ: "word_pred (word_succ a) = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1289
  word_succ_pred: "word_succ (word_pred a) = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1290
  word_mult_succ: "word_succ a * b = b + a * b"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
  1291
  by (transfer, simp add: algebra_simps)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1292
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  1293
lemma uint_cong: "x = y \<Longrightarrow> uint x = uint y"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  1294
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1295
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1296
lemmas uint_word_ariths = 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1297
  word_arith_alts [THEN trans [OF uint_cong int_word_uint]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1298
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1299
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1300
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1301
(* similar expressions for sint (arith operations) *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1302
lemmas sint_word_ariths = uint_word_arith_bintrs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1303
  [THEN uint_sint [symmetric, THEN trans],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1304
  unfolded uint_sint bintr_arith1s bintr_ariths 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1305
    len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1306
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1307
lemmas uint_div_alt = word_div_def [THEN trans [OF uint_cong int_word_uint]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1308
lemmas uint_mod_alt = word_mod_def [THEN trans [OF uint_cong int_word_uint]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1309
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1310
lemma word_pred_0_n1: "word_pred 0 = word_of_int -1"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
  1311
  unfolding word_pred_m1 by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1312
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1313
lemma succ_pred_no [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1314
  "word_succ (numeral w) = numeral w + 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff chang