src/HOL/Word/Word.thy
author huffman
Sat, 10 Dec 2011 08:29:19 +0100
changeset 45805 3c609e8785f2
parent 45804 3a3e4c58083c
child 45807 ff10ec0d62ea
permissions -rw-r--r--
tidied Word.thy; put attributes directly on lemmas instead of using 'declare'; replace various 'lemmas' commands with ordinary '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
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
     2
    Author: Jeremy Dawson and Gerwin Klein, NICTA
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
*)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
     5
header {* A type of finite bit strings *}
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
     6
29628
d9294387ab0e entry point for Word library now named Word
haftmann
parents: 27137
diff changeset
     7
theory Word
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
  Misc_Typedef
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41060
diff changeset
    11
  "~~/src/HOL/Library/Boolean_Algebra"
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41060
diff changeset
    12
  Bool_List_Representation
41060
4199fdcfa3c0 moved smt_word.ML into the directory of the Word library
boehmes
parents: 40827
diff changeset
    13
uses ("~~/src/HOL/Word/Tools/smt_word.ML")
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    14
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    15
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    16
text {* see @{text "Examples/WordExamples.thy"} for examples *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    17
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    18
subsection {* Type definition *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    19
45694
4a8743618257 prefer typedef without extra definition and alternative name;
wenzelm
parents: 45620
diff changeset
    20
typedef (open) 'a word = "{(0::int) ..< 2^len_of TYPE('a::len0)}"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    21
  morphisms uint Abs_word by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    22
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    23
definition word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    24
  -- {* representation of words using unsigned or signed bins, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    25
        only difference in these is the type class *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    26
  "word_of_int w = Abs_word (bintrunc (len_of TYPE ('a)) w)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    27
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    28
lemma uint_word_of_int [code]: "uint (word_of_int w \<Colon> 'a\<Colon>len0 word) = w mod 2 ^ len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    29
  by (auto simp add: word_of_int_def bintrunc_mod2p intro: Abs_word_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    30
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    31
code_datatype word_of_int
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    32
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
    33
subsection {* Random instance *}
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
    34
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    35
notation fcomp (infixl "\<circ>>" 60)
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    36
notation scomp (infixl "\<circ>\<rightarrow>" 60)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    37
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    38
instantiation word :: ("{len0, typerep}") random
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    39
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    40
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    41
definition
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    42
  "random_word i = Random.range (max i (2 ^ len_of TYPE('a))) \<circ>\<rightarrow> (\<lambda>k. Pair (
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    43
     let j = word_of_int (Code_Numeral.int_of k) :: 'a word
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    44
     in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    45
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    46
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    47
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    48
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    49
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    50
no_notation fcomp (infixl "\<circ>>" 60)
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 37667
diff changeset
    51
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    52
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    53
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    54
subsection {* Type conversions and casting *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    55
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    56
definition sint :: "'a :: len word => int" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    57
  -- {* treats the most-significant-bit as a sign bit *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    58
  sint_uint: "sint w = sbintrunc (len_of TYPE ('a) - 1) (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    59
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    60
definition unat :: "'a :: len0 word => nat" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    61
  "unat w = nat (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    62
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    63
definition uints :: "nat => int set" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    64
  -- "the sets of integers representing the words"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    65
  "uints n = range (bintrunc n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    66
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    67
definition sints :: "nat => int set" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    68
  "sints n = range (sbintrunc (n - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    69
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    70
definition unats :: "nat => nat set" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    71
  "unats n = {i. i < 2 ^ n}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    72
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    73
definition norm_sint :: "nat => int => int" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    74
  "norm_sint n w = (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    75
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    76
definition scast :: "'a :: len word => 'b :: len word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    77
  -- "cast a word to a different length"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    78
  "scast w = word_of_int (sint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    79
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    80
definition ucast :: "'a :: len0 word => 'b :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    81
  "ucast w = word_of_int (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    82
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    83
instantiation word :: (len0) size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    84
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    85
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    86
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    87
  word_size: "size (w :: 'a word) = len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    88
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    89
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    90
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    91
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    92
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    93
definition source_size :: "('a :: len0 word => 'b) => nat" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    94
  -- "whether a cast (or other) function is to a longer or shorter length"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    95
  "source_size c = (let arb = undefined ; x = c arb in size arb)"  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    96
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    97
definition target_size :: "('a => 'b :: len0 word) => nat" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    98
  "target_size c = size (c undefined)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    99
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   100
definition is_up :: "('a :: len0 word => 'b :: len0 word) => bool" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   101
  "is_up c \<longleftrightarrow> source_size c <= target_size c"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   102
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   103
definition is_down :: "('a :: len0 word => 'b :: len0 word) => bool" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   104
  "is_down c \<longleftrightarrow> target_size c <= source_size c"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   105
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   106
definition of_bl :: "bool list => 'a :: len0 word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   107
  "of_bl bl = word_of_int (bl_to_bin bl)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   108
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   109
definition to_bl :: "'a :: len0 word => bool list" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   110
  "to_bl w = bin_to_bl (len_of TYPE ('a)) (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   111
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   112
definition word_reverse :: "'a :: len0 word => 'a word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   113
  "word_reverse w = of_bl (rev (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   114
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   115
definition word_int_case :: "(int => 'b) => ('a :: len0 word) => 'b" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   116
  "word_int_case f w = f (uint w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   117
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   118
syntax
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   119
  of_int :: "int => 'a"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   120
translations
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   121
  "case x of CONST of_int y => b" == "CONST word_int_case (%y. b) x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   122
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   123
subsection {* Type-definition locale instantiations *}
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   124
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   125
lemma word_size_gt_0 [iff]: "0 < size (w::'a::len word)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   126
  by (fact xtr1 [OF word_size len_gt_0])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   127
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   128
lemmas lens_gt_0 = word_size_gt_0 len_gt_0
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   129
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
   130
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   131
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
   132
  by (simp add: uints_def range_bintrunc)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   133
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   134
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
   135
  by (simp add: sints_def range_sbintrunc)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   136
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   137
(* FIXME: delete *)
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   138
lemmas atLeastLessThan_alt = atLeastLessThan_def [unfolded 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   139
  atLeast_def lessThan_def Collect_conj_eq [symmetric]]
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   140
  
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   141
lemma mod_in_reps: "m > 0 \<Longrightarrow> y mod m : {0::int ..< m}"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   142
  by auto
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   143
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   144
lemma 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   145
  uint_0:"0 <= uint x" and 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   146
  uint_lt: "uint (x::'a::len0 word) < 2 ^ len_of TYPE('a)"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   147
  by (auto simp: uint [simplified])
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   148
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   149
lemma uint_mod_same:
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   150
  "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
   151
  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
   152
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   153
lemma td_ext_uint: 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   154
  "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
   155
    (%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
   156
  apply (unfold td_ext_def')
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   157
  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
   158
  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
   159
                   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
   160
  done
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   161
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   162
lemma int_word_uint:
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   163
  "uint (word_of_int x::'a::len0 word) = x mod 2 ^ len_of TYPE('a)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   164
  by (fact td_ext_uint [THEN td_ext.eq_norm])
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   165
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   166
interpretation word_uint:
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   167
  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
   168
         word_of_int 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   169
         "uints (len_of TYPE('a::len0))"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   170
         "\<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
   171
  by (rule td_ext_uint)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   172
  
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   173
lemmas td_uint = word_uint.td_thm
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   174
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   175
lemmas td_ext_ubin = td_ext_uint 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   176
  [simplified len_gt_0 no_bintr_alt1 [symmetric]]
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   177
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   178
interpretation word_ubin:
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   179
  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
   180
         word_of_int 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   181
         "uints (len_of TYPE('a::len0))"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   182
         "bintrunc (len_of TYPE('a::len0))"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   183
  by (rule td_ext_ubin)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   184
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   185
lemma split_word_all:
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   186
  "(\<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
   187
proof
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   188
  fix x :: "'a word"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   189
  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
   190
  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
   191
  thus "PROP P x" by simp
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   192
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   193
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   194
subsection  "Arithmetic operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   195
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   196
definition
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   197
  word_succ :: "'a :: len0 word => 'a word"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   198
where
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   199
  "word_succ a = word_of_int (Int.succ (uint a))"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   200
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   201
definition
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   202
  word_pred :: "'a :: len0 word => 'a word"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   203
where
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   204
  "word_pred a = word_of_int (Int.pred (uint a))"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   205
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   206
instantiation word :: (len0) "{number, Divides.div, comm_monoid_mult, comm_ring}"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   207
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   208
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   209
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   210
  word_0_wi: "0 = word_of_int 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   211
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   212
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   213
  word_1_wi: "1 = word_of_int 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   214
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   215
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   216
  word_add_def: "a + b = word_of_int (uint a + uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   217
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   218
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   219
  word_sub_wi: "a - b = word_of_int (uint a - uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   220
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   221
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   222
  word_minus_def: "- a = word_of_int (- uint a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   223
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   224
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   225
  word_mult_def: "a * b = word_of_int (uint a * uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   226
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   227
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   228
  word_div_def: "a div b = word_of_int (uint a div uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   229
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   230
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   231
  word_mod_def: "a mod b = word_of_int (uint a mod uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   232
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   233
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   234
  word_number_of_def: "number_of w = word_of_int w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   235
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   236
lemmas word_arith_wis = 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   237
  word_add_def word_mult_def word_minus_def 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   238
  word_succ_def word_pred_def word_0_wi word_1_wi
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   239
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   240
lemmas arths = 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   241
  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
   242
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   243
lemma wi_homs: 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   244
  shows
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   245
  wi_hom_add: "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
   246
  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
   247
  wi_hom_neg: "- word_of_int a = word_of_int (- a)" and
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   248
  wi_hom_succ: "word_succ (word_of_int a) = word_of_int (Int.succ a)" and
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   249
  wi_hom_pred: "word_pred (word_of_int a) = word_of_int (Int.pred a)"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   250
  by (auto simp: word_arith_wis arths)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   251
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   252
lemmas wi_hom_syms = wi_homs [symmetric]
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   253
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   254
lemma word_sub_def: "a - b = a + - (b :: 'a :: len0 word)"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   255
  unfolding word_sub_wi diff_minus
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   256
  by (simp only : word_uint.Rep_inverse wi_hom_syms)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   257
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   258
lemma word_of_int_sub_hom:
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   259
  "(word_of_int a) - word_of_int b = word_of_int (a - b)"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   260
  by (simp add: word_sub_wi arths)
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   261
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   262
lemmas new_word_of_int_homs = 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   263
  word_of_int_sub_hom wi_homs word_0_wi word_1_wi 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   264
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   265
lemmas new_word_of_int_hom_syms = new_word_of_int_homs [symmetric]
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   266
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   267
lemmas word_of_int_hom_syms =
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   268
  new_word_of_int_hom_syms [unfolded succ_def pred_def]
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   269
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   270
lemmas word_of_int_homs =
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   271
  new_word_of_int_homs [unfolded succ_def pred_def]
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   272
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   273
(* FIXME: provide only one copy of these theorems! *)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   274
lemmas word_of_int_add_hom = wi_hom_add
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   275
lemmas word_of_int_mult_hom = wi_hom_mult
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   276
lemmas word_of_int_minus_hom = wi_hom_neg
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   277
lemmas word_of_int_succ_hom = wi_hom_succ [unfolded succ_def]
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   278
lemmas word_of_int_pred_hom = wi_hom_pred [unfolded pred_def]
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   279
lemmas word_of_int_0_hom = word_0_wi
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   280
lemmas word_of_int_1_hom = word_1_wi
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   281
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   282
instance
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   283
  by default (auto simp: split_word_all word_of_int_homs algebra_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   284
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   285
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   286
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   287
lemma word_zero_neq_one: "0 < len_of TYPE ('a :: len0) \<Longrightarrow> (0 :: 'a word) ~= 1"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   288
  unfolding word_arith_wis
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   289
  by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   290
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   291
lemmas lenw1_zero_neq_one = len_gt_0 [THEN word_zero_neq_one]
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   292
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   293
instance word :: (len) comm_ring_1
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   294
  by (intro_classes) (simp add: lenw1_zero_neq_one)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   295
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   296
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
   297
  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
   298
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   299
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
   300
  apply (rule ext)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   301
  apply (case_tac x rule: int_diff_cases)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   302
  apply (simp add: word_of_nat word_of_int_sub_hom)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   303
  done
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   304
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   305
lemma word_number_of_eq: 
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   306
  "number_of w = (of_int w :: 'a :: len word)"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   307
  unfolding word_number_of_def word_of_int by auto
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   308
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   309
instance word :: (len) number_ring
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   310
  by (intro_classes) (simp add : word_number_of_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   311
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   312
definition udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50) where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   313
  "a udvd b = (EX n>=0. uint b = n * uint a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   314
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   315
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   316
subsection "Ordering"
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   317
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   318
instantiation word :: (len0) linorder
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   319
begin
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   320
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   321
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   322
  word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   323
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   324
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   325
  word_less_def: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> (y \<Colon> 'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   326
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   327
instance
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   328
  by default (auto simp: word_less_def word_le_def)
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   329
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   330
end
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   331
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   332
definition word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50) where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   333
  "a <=s b = (sint a <= sint b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   334
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   335
definition word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50) where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   336
  "(x <s y) = (x <=s y & x ~= y)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   337
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   338
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   339
subsection "Bit-wise operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   340
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   341
instantiation word :: (len0) bits
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   342
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   343
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   344
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   345
  word_and_def: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   346
  "(a::'a word) AND b = word_of_int (uint a AND uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   347
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   348
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   349
  word_or_def:  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   350
  "(a::'a word) OR b = word_of_int (uint a OR uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   351
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   352
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   353
  word_xor_def: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   354
  "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   355
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   356
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   357
  word_not_def: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   358
  "NOT (a::'a word) = word_of_int (NOT (uint a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   359
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   360
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   361
  word_test_bit_def: "test_bit a = bin_nth (uint a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   362
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   363
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   364
  word_set_bit_def: "set_bit a n x =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   365
   word_of_int (bin_sc n (If x 1 0) (uint a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   366
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   367
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   368
  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
   369
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   370
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   371
  word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a) = 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   372
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   373
definition shiftl1 :: "'a word \<Rightarrow> 'a word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   374
  "shiftl1 w = word_of_int (uint w BIT 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   375
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   376
definition shiftr1 :: "'a word \<Rightarrow> 'a word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   377
  -- "shift right as unsigned or as signed, ie logical or arithmetic"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   378
  "shiftr1 w = word_of_int (bin_rest (uint w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   379
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   380
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   381
  shiftl_def: "w << n = (shiftl1 ^^ n) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   382
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   383
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   384
  shiftr_def: "w >> n = (shiftr1 ^^ n) w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   385
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   386
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   387
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   388
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   389
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   390
instantiation word :: (len) bitss
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   391
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   392
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   393
definition
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   394
  word_msb_def: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   395
  "msb a \<longleftrightarrow> bin_sign (sint a) = Int.Min"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   396
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   397
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   398
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   399
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   400
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37660
diff changeset
   401
lemma [code]:
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37660
diff changeset
   402
  "msb a \<longleftrightarrow> bin_sign (sint a) = (- 1 :: int)"
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37660
diff changeset
   403
  by (simp only: word_msb_def Min_def)
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37660
diff changeset
   404
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   405
definition setBit :: "'a :: len0 word => nat => 'a word" where 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   406
  "setBit w n = set_bit w n True"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   407
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   408
definition clearBit :: "'a :: len0 word => nat => 'a word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   409
  "clearBit w n = set_bit w n False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   410
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   411
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   412
subsection "Shift operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   413
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   414
definition sshiftr1 :: "'a :: len word => 'a word" where 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   415
  "sshiftr1 w = word_of_int (bin_rest (sint w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   416
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   417
definition bshiftr1 :: "bool => 'a :: len word => 'a word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   418
  "bshiftr1 b w = of_bl (b # butlast (to_bl w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   419
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   420
definition sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55) where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   421
  "w >>> n = (sshiftr1 ^^ n) w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   422
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   423
definition mask :: "nat => 'a::len word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   424
  "mask n = (1 << n) - 1"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   425
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   426
definition revcast :: "'a :: len0 word => 'b :: len0 word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   427
  "revcast w =  of_bl (takefill False (len_of TYPE('b)) (to_bl w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   428
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   429
definition slice1 :: "nat => 'a :: len0 word => 'b :: len0 word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   430
  "slice1 n w = of_bl (takefill False n (to_bl w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   431
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   432
definition slice :: "nat => 'a :: len0 word => 'b :: len0 word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   433
  "slice n w = slice1 (size w - n) w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   434
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   435
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   436
subsection "Rotation"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   437
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   438
definition rotater1 :: "'a list => 'a list" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   439
  "rotater1 ys = 
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   440
    (case ys of [] => [] | x # xs => last ys # butlast ys)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   441
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   442
definition rotater :: "nat => 'a list => 'a list" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   443
  "rotater n = rotater1 ^^ n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   444
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   445
definition word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   446
  "word_rotr n w = of_bl (rotater n (to_bl w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   447
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   448
definition word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   449
  "word_rotl n w = of_bl (rotate n (to_bl w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   450
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   451
definition word_roti :: "int => 'a :: len0 word => 'a :: len0 word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   452
  "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
   453
                    else word_rotl (nat (- i)) w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   454
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   455
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   456
subsection "Split and cat operations"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   457
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   458
definition word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   459
  "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
   460
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   461
definition word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   462
  "word_split a = 
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   463
   (case bin_split (len_of TYPE ('c)) (uint a) of 
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   464
     (u, v) => (word_of_int u, word_of_int v))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   465
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   466
definition word_rcat :: "'a :: len0 word list => 'b :: len0 word" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   467
  "word_rcat ws = 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   468
  word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   469
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   470
definition word_rsplit :: "'a :: len0 word => 'b :: len word list" where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   471
  "word_rsplit w = 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   472
  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
   473
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   474
definition max_word :: "'a::len word" -- "Largest representable machine integer." where
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   475
  "max_word = word_of_int (2 ^ len_of TYPE('a) - 1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   476
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   477
primrec of_bool :: "bool \<Rightarrow> 'a::len word" where
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   478
  "of_bool False = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   479
| "of_bool True = 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   480
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   481
(* FIXME: only provide one theorem name *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   482
lemmas of_nth_def = word_set_bits_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   483
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   484
lemma sint_sbintrunc': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   485
  "sint (word_of_int bin :: 'a word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   486
    (sbintrunc (len_of TYPE ('a :: len) - 1) bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   487
  unfolding sint_uint 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   488
  by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   489
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   490
lemma uint_sint: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   491
  "uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a :: len word))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   492
  unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   493
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   494
lemma bintr_uint': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   495
  "n >= size w \<Longrightarrow> bintrunc n (uint w) = uint w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   496
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   497
  apply (subst word_ubin.norm_Rep [symmetric]) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   498
  apply (simp only: bintrunc_bintrunc_min word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   499
  apply (simp add: min_max.inf_absorb2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   500
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   501
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   502
lemma wi_bintr': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   503
  "wb = word_of_int bin \<Longrightarrow> n >= size wb \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   504
    word_of_int (bintrunc n bin) = wb"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   505
  unfolding word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   506
  by (clarsimp simp add: word_ubin.norm_eq_iff [symmetric] min_max.inf_absorb1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   507
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   508
lemmas bintr_uint = bintr_uint' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   509
lemmas wi_bintr = wi_bintr' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   510
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   511
lemma td_ext_sbin: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   512
  "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
   513
    (sbintrunc (len_of TYPE('a) - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   514
  apply (unfold td_ext_def' sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   515
  apply (simp add : word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   516
  apply (cases "len_of TYPE('a)")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   517
   apply (auto simp add : sints_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   518
  apply (rule sym [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   519
  apply (rule word_ubin.Abs_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   520
  apply (simp only: bintrunc_sbintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   521
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   522
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   523
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   524
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   525
lemmas td_ext_sint = td_ext_sbin 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   526
  [simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   527
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   528
(* We do sint before sbin, before sint is the user version
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   529
   and interpretations do not produce thm duplicates. I.e. 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   530
   we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   531
   because the latter is the same thm as the former *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   532
interpretation word_sint:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   533
  td_ext "sint ::'a::len word => int" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   534
          word_of_int 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   535
          "sints (len_of TYPE('a::len))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   536
          "%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
   537
               2 ^ (len_of TYPE('a::len) - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   538
  by (rule td_ext_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   539
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   540
interpretation word_sbin:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   541
  td_ext "sint ::'a::len word => int" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   542
          word_of_int 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   543
          "sints (len_of TYPE('a::len))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   544
          "sbintrunc (len_of TYPE('a::len) - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   545
  by (rule td_ext_sbin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   546
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   547
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   548
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   549
lemmas td_sint = word_sint.td
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   550
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   551
lemma word_number_of_alt [code_unfold_post]:
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   552
  "number_of b = word_of_int (number_of b)"
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   553
  by (simp add: number_of_eq word_number_of_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   554
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   555
lemma word_no_wi: "number_of = word_of_int"
44762
8f9d09241a68 tuned proofs;
wenzelm
parents: 42793
diff changeset
   556
  by (auto simp: word_number_of_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   557
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   558
lemma to_bl_def': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   559
  "(to_bl :: 'a :: len0 word => bool list) =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   560
    bin_to_bl (len_of TYPE('a)) o uint"
44762
8f9d09241a68 tuned proofs;
wenzelm
parents: 42793
diff changeset
   561
  by (auto simp: to_bl_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   562
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   563
lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of w"] for w
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   564
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   565
lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   566
  by (fact uints_def [unfolded no_bintr_alt1])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   567
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   568
lemma uint_bintrunc [simp]:
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   569
  "uint (number_of bin :: 'a word) =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   570
    number_of (bintrunc (len_of TYPE ('a :: len0)) bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   571
  unfolding word_number_of_def number_of_eq
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   572
  by (auto intro: word_ubin.eq_norm) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   573
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   574
lemma sint_sbintrunc [simp]:
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   575
  "sint (number_of bin :: 'a word) =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   576
    number_of (sbintrunc (len_of TYPE ('a :: len) - 1) bin)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   577
  unfolding word_number_of_def number_of_eq
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   578
  by (subst word_sbin.eq_norm) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   579
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   580
lemma unat_bintrunc [simp]:
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   581
  "unat (number_of bin :: 'a :: len0 word) =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   582
    number_of (bintrunc (len_of TYPE('a)) bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   583
  unfolding unat_def nat_number_of_def 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   584
  by (simp only: uint_bintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   585
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   586
lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 \<Longrightarrow> v = w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   587
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   588
  apply (rule word_uint.Rep_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   589
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   590
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   591
    apply (rule word_ubin.norm_Rep)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   592
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   593
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   594
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   595
(* TODO: remove uint_lem and sint_lem *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   596
lemmas uint_lem = word_uint.Rep [unfolded uints_num mem_Collect_eq]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   597
lemmas sint_lem = word_sint.Rep [unfolded sints_num mem_Collect_eq]
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   598
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   599
lemma uint_ge_0 [iff]: "0 \<le> uint (x::'a::len0 word)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   600
  using word_uint.Rep [of x] by (simp add: uints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   601
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   602
lemma uint_lt2p [iff]: "uint (x::'a::len0 word) < 2 ^ len_of TYPE('a)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   603
  using word_uint.Rep [of x] by (simp add: uints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   604
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   605
lemma sint_ge: "- (2 ^ (len_of TYPE('a) - 1)) \<le> sint (x::'a::len word)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   606
  using word_sint.Rep [of x] by (simp add: sints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   607
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   608
lemma sint_lt: "sint (x::'a::len word) < 2 ^ (len_of TYPE('a) - 1)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   609
  using word_sint.Rep [of x] by (simp add: sints_num)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   610
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   611
lemma sign_uint_Pls [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   612
  "bin_sign (uint x) = Int.Pls"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   613
  by (simp add: sign_Pls_ge_0 number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   614
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   615
lemma uint_m2p_neg: "uint (x::'a::len0 word) - 2 ^ len_of TYPE('a) < 0"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   616
  by (simp only: diff_less_0_iff_less uint_lt2p)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   617
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   618
lemma uint_m2p_not_non_neg:
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   619
  "\<not> 0 \<le> uint (x::'a::len0 word) - 2 ^ len_of TYPE('a)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   620
  by (simp only: not_le uint_m2p_neg)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   621
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   622
lemma lt2p_lem:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   623
  "len_of TYPE('a) <= n \<Longrightarrow> uint (w :: 'a :: len0 word) < 2 ^ n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   624
  by (rule xtr8 [OF _ uint_lt2p]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   625
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   626
lemma uint_le_0_iff [simp]: "uint x \<le> 0 \<longleftrightarrow> uint x = 0"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   627
  by (fact uint_ge_0 [THEN leD, THEN linorder_antisym_conv1])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   628
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   629
lemma uint_nat: "uint w = int (unat w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   630
  unfolding unat_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   631
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   632
lemma uint_number_of:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   633
  "uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   634
  unfolding word_number_of_alt
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   635
  by (simp only: int_word_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   636
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   637
lemma unat_number_of: 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   638
  "bin_sign b = Int.Pls \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   639
  unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   640
  apply (unfold unat_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   641
  apply (clarsimp simp only: uint_number_of)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   642
  apply (rule nat_mod_distrib [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   643
    apply (erule sign_Pls_ge_0 [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   644
   apply (simp_all add: nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   645
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   646
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   647
lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   648
    2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   649
    2 ^ (len_of TYPE('a) - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   650
  unfolding word_number_of_alt by (rule int_word_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   651
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   652
lemma word_of_int_bin [simp] : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   653
  "(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   654
  unfolding word_number_of_alt by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   655
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   656
lemma word_int_case_wi: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   657
  "word_int_case f (word_of_int i :: 'b word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   658
    f (i mod 2 ^ len_of TYPE('b::len0))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   659
  unfolding word_int_case_def by (simp add: word_uint.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   661
lemma word_int_split: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   662
  "P (word_int_case f x) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   663
    (ALL i. x = (word_of_int i :: 'b :: len0 word) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   664
      0 <= i & i < 2 ^ len_of TYPE('b) --> P (f i))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   665
  unfolding word_int_case_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   666
  by (auto simp: word_uint.eq_norm int_mod_eq')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   667
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   668
lemma word_int_split_asm: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   669
  "P (word_int_case f x) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   670
    (~ (EX n. x = (word_of_int n :: 'b::len0 word) &
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   671
      0 <= n & n < 2 ^ len_of TYPE('b::len0) & ~ P (f n)))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   672
  unfolding word_int_case_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   673
  by (auto simp: word_uint.eq_norm int_mod_eq')
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   674
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   675
(* FIXME: uint_range' is an exact duplicate of uint_lem *)
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   676
lemmas uint_range' = word_uint.Rep [unfolded uints_num mem_Collect_eq]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   677
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
   678
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   679
lemma uint_range_size: "0 <= uint w & uint w < 2 ^ size w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   680
  unfolding word_size by (rule uint_range')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   681
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   682
lemma sint_range_size:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   683
  "- (2 ^ (size w - Suc 0)) <= sint w & sint w < 2 ^ (size w - Suc 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   684
  unfolding word_size by (rule sint_range')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   685
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   686
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
   687
  unfolding word_size by (rule less_le_trans [OF sint_lt])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   688
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   689
lemma sint_below_size:
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   690
  "x \<le> - (2 ^ (size (w::'a::len word) - 1)) \<Longrightarrow> x \<le> sint w"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   691
  unfolding word_size by (rule order_trans [OF _ sint_ge])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   692
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   693
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
   694
  unfolding word_test_bit_def by (simp add: bin_nth_eq_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   695
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   696
lemma test_bit_size [rule_format] : "(w::'a::len0 word) !! n --> n < size w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   697
  apply (unfold word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   698
  apply (subst word_ubin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   699
  apply (simp only: nth_bintr word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   700
  apply fast
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   701
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   702
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   703
lemma word_eqI [rule_format] : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   704
  fixes u :: "'a::len0 word"
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   705
  shows "(ALL n. n < size u --> u !! n = v !! n) \<Longrightarrow> u = v"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   706
  apply (rule test_bit_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   707
  apply (rule ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   708
  apply (erule allE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   709
  apply (erule impCE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   710
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   711
   apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   712
  apply (auto dest!: test_bit_size simp add: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   713
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   714
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   715
lemma word_eqD: "(u::'a::len0 word) = v \<Longrightarrow> u !! x = v !! x"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   716
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   717
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   718
lemma test_bit_bin': "w !! n = (n < size w & bin_nth (uint w) n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   719
  unfolding word_test_bit_def word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   720
  by (simp add: nth_bintr [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   721
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   722
lemmas test_bit_bin = test_bit_bin' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   723
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   724
lemma bin_nth_uint_imp': "bin_nth (uint w) n --> n < size w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   725
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   726
  apply (rule impI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   727
  apply (rule nth_bintr [THEN iffD1, THEN conjunct1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   728
  apply (subst word_ubin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   729
  apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   730
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   731
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   732
lemma bin_nth_sint': 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   733
  "n >= size w --> bin_nth (sint w) n = bin_nth (sint w) (size w - 1)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   734
  apply (rule impI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   735
  apply (subst word_sbin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   736
  apply (simp add : nth_sbintr word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   737
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   738
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   739
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   740
lemmas bin_nth_uint_imp = bin_nth_uint_imp' [rule_format, unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   741
lemmas bin_nth_sint = bin_nth_sint' [rule_format, unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   742
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   743
(* type definitions theorem for in terms of equivalent bool list *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   744
lemma td_bl: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   745
  "type_definition (to_bl :: 'a::len0 word => bool list) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   746
                   of_bl  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   747
                   {bl. length bl = len_of TYPE('a)}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   748
  apply (unfold type_definition_def of_bl_def to_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   749
  apply (simp add: word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   750
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   751
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   752
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   753
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   754
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   755
interpretation word_bl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   756
  type_definition "to_bl :: 'a::len0 word => bool list"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   757
                  of_bl  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   758
                  "{bl. length bl = len_of TYPE('a::len0)}"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   759
  by (rule td_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   760
45538
1fffa81b9b83 eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents: 45529
diff changeset
   761
lemmas word_bl_Rep' = word_bl.Rep [simplified, iff]
1fffa81b9b83 eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents: 45529
diff changeset
   762
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   763
lemma word_size_bl: "size w = size (to_bl w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   764
  unfolding word_size by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   765
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   766
lemma to_bl_use_of_bl:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   767
  "(to_bl w = bl) = (w = of_bl bl \<and> length bl = length (to_bl w))"
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44821
diff changeset
   768
  by (fastforce elim!: word_bl.Abs_inverse [simplified])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   769
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   770
lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   771
  unfolding word_reverse_def by (simp add: word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   772
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   773
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   774
  unfolding word_reverse_def by (simp add : word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   775
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   776
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   777
  by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   778
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   779
lemma word_rev_gal': "u = word_reverse w \<Longrightarrow> w = word_reverse u"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   780
  by simp
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   781
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   782
lemma length_bl_gt_0 [iff]: "0 < length (to_bl (x::'a::len word))"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   783
  unfolding word_bl_Rep' by (rule len_gt_0)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   784
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   785
lemma bl_not_Nil [iff]: "to_bl (x::'a::len word) \<noteq> []"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   786
  by (fact length_bl_gt_0 [unfolded length_greater_0_conv])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   787
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   788
lemma length_bl_neq_0 [iff]: "length (to_bl (x::'a::len word)) \<noteq> 0"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   789
  by (fact length_bl_gt_0 [THEN gr_implies_not0])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   790
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   791
lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = Int.Min)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   792
  apply (unfold to_bl_def sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   793
  apply (rule trans [OF _ bl_sbin_sign])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   794
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   795
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   796
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   797
lemma of_bl_drop': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   798
  "lend = length bl - len_of TYPE ('a :: len0) \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   799
    of_bl (drop lend bl) = (of_bl bl :: 'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   800
  apply (unfold of_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   801
  apply (clarsimp simp add : trunc_bl2bin [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   802
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   803
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   804
lemma of_bl_no: "of_bl bl = number_of (bl_to_bin bl)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   805
  by (fact of_bl_def [folded word_number_of_def])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   806
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   807
lemma test_bit_of_bl:  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   808
  "(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
   809
  apply (unfold of_bl_def word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   810
  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
   811
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   812
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   813
lemma no_of_bl: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   814
  "(number_of bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   815
  unfolding word_size of_bl_no by (simp add : word_number_of_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   816
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   817
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   818
  unfolding word_size to_bl_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   819
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   820
lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   821
  unfolding uint_bl by (simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   822
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   823
lemma to_bl_of_bin: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   824
  "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
   825
  unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   826
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   827
lemma to_bl_no_bin [simp]:
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   828
  "to_bl (number_of bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   829
  by (fact to_bl_of_bin [folded word_number_of_def])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   830
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   831
lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   832
  unfolding uint_bl by (simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   833
  
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   834
lemmas uint_bl_bin [simp] = trans [OF bin_bl_bin word_ubin.norm_Rep]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   835
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   836
(* FIXME: the next two lemmas should be unnecessary, because the lhs
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   837
terms should never occur in practice *)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   838
lemma num_AB_u [simp]: "number_of (uint x) = x"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   839
  unfolding word_number_of_def by (rule word_uint.Rep_inverse)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   840
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   841
lemma num_AB_s [simp]: "number_of (sint x) = x"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
   842
  unfolding word_number_of_def by (rule word_sint.Rep_inverse)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   843
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   844
(* naturals *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   845
lemma uints_unats: "uints n = int ` unats n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   846
  apply (unfold unats_def uints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   847
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   848
  apply (rule_tac image_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   849
  apply (erule_tac nat_0_le [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   850
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   851
  apply (erule_tac nat_less_iff [THEN iffD2])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   852
  apply (rule_tac [2] zless_nat_eq_int_zless [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   853
  apply (auto simp add : nat_power_eq int_power)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   854
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   855
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   856
lemma unats_uints: "unats n = nat ` uints n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   857
  by (auto simp add : uints_unats image_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   858
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   859
lemmas bintr_num = word_ubin.norm_eq_iff [symmetric, folded word_number_of_def]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   860
lemmas sbintr_num = word_sbin.norm_eq_iff [symmetric, folded word_number_of_def]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   861
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   862
lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   863
lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   864
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   865
(* don't add these to simpset, since may want bintrunc n w to be simplified;
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   866
  may want these in reverse, but loop as simp rules, so use following *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   867
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   868
lemma num_of_bintr':
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   869
  "bintrunc (len_of TYPE('a :: len0)) a = b \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   870
    number_of a = (number_of b :: 'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   871
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   872
  apply (rule_tac num_of_bintr [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   873
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   874
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   875
lemma num_of_sbintr':
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   876
  "sbintrunc (len_of TYPE('a :: len) - 1) a = b \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   877
    number_of a = (number_of b :: 'a word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   878
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   879
  apply (rule_tac num_of_sbintr [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   880
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   881
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   882
lemmas num_abs_bintr = sym [THEN trans, OF num_of_bintr word_number_of_def]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   883
lemmas num_abs_sbintr = sym [THEN trans, OF num_of_sbintr word_number_of_def]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   884
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   885
(** 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
   886
  thus in "cast w = w, the type means cast to length of w! **)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   887
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   888
lemma ucast_id: "ucast w = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   889
  unfolding ucast_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   890
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   891
lemma scast_id: "scast w = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   892
  unfolding scast_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   893
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   894
lemma ucast_bl: "ucast w = of_bl (to_bl w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   895
  unfolding ucast_def of_bl_def uint_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   896
  by (auto simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   897
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   898
lemma nth_ucast: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   899
  "(ucast w::'a::len0 word) !! n = (w !! n & n < len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   900
  apply (unfold ucast_def test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   901
  apply (simp add: word_ubin.eq_norm nth_bintr word_size) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   902
  apply (fast elim!: bin_nth_uint_imp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   903
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   904
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   905
(* for literal u(s)cast *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   906
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   907
lemma ucast_bintr [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   908
  "ucast (number_of w ::'a::len0 word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   909
   number_of (bintrunc (len_of TYPE('a)) w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   910
  unfolding ucast_def by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   911
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   912
lemma scast_sbintr [simp]: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   913
  "scast (number_of w ::'a::len word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   914
   number_of (sbintrunc (len_of TYPE('a) - Suc 0) w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   915
  unfolding scast_def by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   916
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   917
lemmas source_size = source_size_def [unfolded Let_def word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   918
lemmas target_size = target_size_def [unfolded Let_def word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   919
lemmas is_down = is_down_def [unfolded source_size target_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   920
lemmas is_up = is_up_def [unfolded source_size target_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   921
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   922
lemmas is_up_down = trans [OF is_up is_down [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   923
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   924
lemma down_cast_same': "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc = scast"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   925
  apply (unfold is_down)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   926
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   927
  apply (rule ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   928
  apply (unfold ucast_def scast_def uint_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   929
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   930
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   931
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   932
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   933
lemma word_rev_tf': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   934
  "r = to_bl (of_bl bl) \<Longrightarrow> r = rev (takefill False (length r) (rev bl))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   935
  unfolding of_bl_def uint_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   936
  by (clarsimp simp add: bl_bin_bl_rtf word_ubin.eq_norm word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   937
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
   938
lemmas word_rev_tf = refl [THEN word_rev_tf', unfolded word_bl_Rep']
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   939
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   940
lemmas word_rep_drop = word_rev_tf [simplified takefill_alt,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   941
  simplified, simplified rev_take, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   942
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   943
lemma to_bl_ucast: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   944
  "to_bl (ucast (w::'b::len0 word) ::'a::len0 word) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   945
   replicate (len_of TYPE('a) - len_of TYPE('b)) False @
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   946
   drop (len_of TYPE('b) - len_of TYPE('a)) (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   947
  apply (unfold ucast_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   948
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   949
   apply (rule word_rep_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   950
  apply simp
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 ucast_up_app': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   954
  "uc = ucast \<Longrightarrow> source_size uc + n = target_size uc \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   955
    to_bl (uc w) = replicate n False @ (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   956
  by (auto simp add : source_size target_size to_bl_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   957
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   958
lemma ucast_down_drop': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   959
  "uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   960
    to_bl (uc w) = drop n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   961
  by (auto simp add : source_size target_size to_bl_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   962
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   963
lemma scast_down_drop': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   964
  "sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   965
    to_bl (sc w) = drop n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   966
  apply (subgoal_tac "sc = ucast")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   967
   apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   968
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   969
   apply (erule refl [THEN ucast_down_drop'])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   970
  apply (rule refl [THEN down_cast_same', symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   971
  apply (simp add : source_size target_size is_down)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   972
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   973
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   974
lemma sint_up_scast': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   975
  "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   976
  apply (unfold is_up)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   977
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   978
  apply (simp add: scast_def word_sbin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   979
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   980
    prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   981
    apply (rule word_sbin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   982
   apply (rule sbintrunc_sbintrunc_l)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   983
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   984
   apply (subst word_sbin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   985
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   986
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   987
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   988
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   989
lemma uint_up_ucast':
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
   990
  "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   991
  apply (unfold is_up)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   992
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   993
  apply (rule bin_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   994
  apply (fold word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   995
  apply (auto simp add: nth_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   996
  apply (auto simp add: test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   997
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   998
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   999
lemmas down_cast_same = refl [THEN down_cast_same']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1000
lemmas ucast_up_app = refl [THEN ucast_up_app']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1001
lemmas ucast_down_drop = refl [THEN ucast_down_drop']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1002
lemmas scast_down_drop = refl [THEN scast_down_drop']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1003
lemmas uint_up_ucast = refl [THEN uint_up_ucast']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1004
lemmas sint_up_scast = refl [THEN sint_up_scast']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1005
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1006
lemma ucast_up_ucast': "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> ucast (uc w) = ucast w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1007
  apply (simp (no_asm) add: ucast_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1008
  apply (clarsimp simp add: uint_up_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1009
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1010
    
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1011
lemma scast_up_scast': "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> scast (sc w) = scast w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1012
  apply (simp (no_asm) add: scast_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1013
  apply (clarsimp simp add: sint_up_scast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1014
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1015
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1016
lemma ucast_of_bl_up': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1017
  "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
  1018
  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
  1019
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1020
lemmas ucast_up_ucast = refl [THEN ucast_up_ucast']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1021
lemmas scast_up_scast = refl [THEN scast_up_scast']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1022
lemmas ucast_of_bl_up = refl [THEN ucast_of_bl_up']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1023
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1024
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1025
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1026
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1027
lemmas isduu = is_up_down [where c = "ucast", THEN iffD2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1028
lemmas isdus = is_up_down [where c = "scast", THEN iffD2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1029
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1030
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1031
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1032
lemma up_ucast_surj:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1033
  "is_up (ucast :: 'b::len0 word => 'a::len0 word) \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1034
   surj (ucast :: 'a word => 'b word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1035
  by (rule surjI, erule ucast_up_ucast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1036
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1037
lemma up_scast_surj:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1038
  "is_up (scast :: 'b::len word => 'a::len word) \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1039
   surj (scast :: 'a word => 'b word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1040
  by (rule surjI, erule scast_up_scast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1041
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1042
lemma down_scast_inj:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1043
  "is_down (scast :: 'b::len word => 'a::len word) \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1044
   inj_on (ucast :: 'a word => 'b word) A"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1045
  by (rule inj_on_inverseI, erule scast_down_scast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1046
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1047
lemma down_ucast_inj:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1048
  "is_down (ucast :: 'b::len0 word => 'a::len0 word) \<Longrightarrow> 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1049
   inj_on (ucast :: 'a word => 'b word) A"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1050
  by (rule inj_on_inverseI, erule ucast_down_ucast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1051
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1052
lemma of_bl_append_same: "of_bl (X @ to_bl w) = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1053
  by (rule word_bl.Rep_eqD) (simp add: word_rep_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1054
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1055
lemma ucast_down_no': 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1056
  "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (number_of bin) = number_of bin"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1057
  apply (unfold word_number_of_def is_down)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1058
  apply (clarsimp simp add: ucast_def word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1059
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1060
  apply (erule bintrunc_bintrunc_ge)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1061
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1062
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1063
lemmas ucast_down_no = ucast_down_no' [OF refl]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1064
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1065
lemma ucast_down_bl': "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1066
  unfolding of_bl_no by clarify (erule ucast_down_no)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1067
    
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1068
lemmas ucast_down_bl = ucast_down_bl' [OF refl]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1069
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1070
lemmas slice_def' = slice_def [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1071
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1072
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1073
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
  1074
lemmas word_log_bin_defs = word_log_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1075
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1076
text {* Executable equality *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1077
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38527
diff changeset
  1078
instantiation word :: (len0) equal
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1079
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1080
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38527
diff changeset
  1081
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" where
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38527
diff changeset
  1082
  "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1083
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1084
instance proof
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 38527
diff changeset
  1085
qed (simp add: equal equal_word_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1086
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1087
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1088
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1089
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1090
subsection {* Word Arithmetic *}
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1091
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1092
lemma word_less_alt: "(a < b) = (uint a < uint b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1093
  unfolding word_less_def word_le_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1094
  by (auto simp del: word_uint.Rep_inject 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1095
           simp: word_uint.Rep_inject [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1096
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1097
lemma signed_linorder: "class.linorder word_sle word_sless"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1098
proof
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1099
qed (unfold word_sle_def word_sless_def, auto)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1100
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1101
interpretation signed: linorder "word_sle" "word_sless"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1102
  by (rule signed_linorder)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1103
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1104
lemma udvdI: 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1105
  "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1106
  by (auto simp: udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1107
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1108
lemmas word_div_no [simp] = word_div_def [of "number_of a" "number_of b"] for a b
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1109
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1110
lemmas word_mod_no [simp] = word_mod_def [of "number_of a" "number_of b"] for a b
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1111
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1112
lemmas word_less_no [simp] = word_less_def [of "number_of a" "number_of b"] for a b
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1113
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1114
lemmas word_le_no [simp] = word_le_def [of "number_of a" "number_of b"] for a b
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1115
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1116
lemmas word_sless_no [simp] = word_sless_def [of "number_of a" "number_of b"] for a b
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1117
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1118
lemmas word_sle_no [simp] = word_sle_def [of "number_of a" "number_of b"] for a b
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1119
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1120
(* following two are available in class number_ring, 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1121
  but convenient to have them here here;
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1122
  note - the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1123
  are in the default simpset, so to use the automatic simplifications for
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1124
  (eg) sint (number_of bin) on sint 1, must do
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1125
  (simp add: word_1_no del: numeral_1_eq_1) 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1126
  *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1127
lemmas word_0_wi_Pls = word_0_wi [folded Pls_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1128
lemmas word_0_no = word_0_wi_Pls [folded word_no_wi]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1129
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1130
lemma int_one_bin: "(1 :: int) = (Int.Pls BIT 1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1131
  unfolding Pls_def Bit_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1132
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1133
lemma word_1_no: 
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1134
  "(1 :: 'a :: len0 word) = number_of (Int.Pls BIT 1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1135
  unfolding word_1_wi word_number_of_def int_one_bin by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1136
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1137
lemma word_m1_wi: "-1 = word_of_int -1" 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1138
  by (rule word_number_of_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1139
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1140
lemma word_m1_wi_Min: "-1 = word_of_int Int.Min"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1141
  by (simp add: word_m1_wi number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1142
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1143
lemma word_0_bl [simp]: "of_bl [] = 0" 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1144
  unfolding word_0_wi of_bl_def by (simp add : Pls_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1145
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1146
lemma word_1_bl: "of_bl [True] = 1" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1147
  unfolding word_1_wi of_bl_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1148
  by (simp add : bl_to_bin_def Bit_def Pls_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1149
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1150
lemma uint_eq_0 [simp] : "(uint 0 = 0)" 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1151
  unfolding word_0_wi
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1152
  by (simp add: word_ubin.eq_norm Pls_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1153
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1154
lemma of_bl_0 [simp] : "of_bl (replicate n False) = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1155
  by (simp add : word_0_wi of_bl_def bl_to_bin_rep_False Pls_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1156
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1157
lemma to_bl_0 [simp]:
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1158
  "to_bl (0::'a::len0 word) = replicate (len_of TYPE('a)) False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1159
  unfolding uint_bl
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1160
  by (simp add : word_size bin_to_bl_Pls Pls_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1161
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1162
lemma uint_0_iff: "(uint x = 0) = (x = 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1163
  by (auto intro!: word_uint.Rep_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1164
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1165
lemma unat_0_iff: "(unat x = 0) = (x = 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1166
  unfolding unat_def by (auto simp add : nat_eq_iff uint_0_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1167
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1168
lemma unat_0 [simp]: "unat 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1169
  unfolding unat_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1170
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1171
lemma size_0_same': "size w = 0 \<Longrightarrow> w = (v :: 'a :: len0 word)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1172
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1173
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1174
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1175
    apply (rule word_uint.Rep_inverse)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1176
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1177
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1178
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1179
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1180
lemmas size_0_same = size_0_same' [folded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1181
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1182
lemmas unat_eq_0 = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1183
lemmas unat_eq_zero = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1184
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1185
lemma unat_gt_0: "(0 < unat x) = (x ~= 0)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1186
by (auto simp: unat_0_iff [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1187
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1188
lemma ucast_0 [simp] : "ucast 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1189
unfolding ucast_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1190
by simp (simp add: word_0_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1191
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1192
lemma sint_0 [simp] : "sint 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1193
unfolding sint_uint
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1194
by (simp add: Pls_def [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1195
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1196
lemma scast_0 [simp] : "scast 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1197
apply (unfold scast_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1198
apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1199
apply (simp add: word_0_wi)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1200
done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1201
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1202
lemma sint_n1 [simp] : "sint -1 = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1203
apply (unfold word_m1_wi_Min)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1204
apply (simp add: word_sbin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1205
apply (unfold Min_def number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1206
apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1207
done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1208
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1209
lemma scast_n1 [simp] : "scast -1 = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1210
  apply (unfold scast_def sint_n1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1211
  apply (unfold word_number_of_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1212
  apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1213
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1214
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1215
lemma uint_1 [simp] : "uint (1 :: 'a :: len word) = 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1216
  unfolding word_1_wi
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1217
  by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1218
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1219
lemma unat_1 [simp] : "unat (1 :: 'a :: len word) = 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1220
  by (unfold unat_def uint_1) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1221
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1222
lemma ucast_1 [simp] : "ucast (1 :: 'a :: len word) = 1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1223
  unfolding ucast_def word_1_wi
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1224
  by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1225
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1226
(* now, to get the weaker results analogous to word_div/mod_def *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1227
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1228
lemmas word_arith_alts = 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1229
  word_sub_wi [unfolded succ_def pred_def]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1230
  word_arith_wis [unfolded succ_def pred_def]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1231
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1232
lemmas word_succ_alt = word_arith_alts (5)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1233
lemmas word_pred_alt = word_arith_alts (6)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1234
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1235
subsection  "Transferring goals from words to ints"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1236
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1237
lemma word_ths:  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1238
  shows
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1239
  word_succ_p1:   "word_succ a = a + 1" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1240
  word_pred_m1:   "word_pred a = a - 1" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1241
  word_pred_succ: "word_pred (word_succ a) = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1242
  word_succ_pred: "word_succ (word_pred a) = a" and
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1243
  word_mult_succ: "word_succ a * b = b + a * b"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1244
  by (rule word_uint.Abs_cases [of b],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1245
      rule word_uint.Abs_cases [of a],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1246
      simp add: pred_def succ_def add_commute mult_commute 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1247
                ring_distribs new_word_of_int_homs)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1248
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1249
lemmas uint_cong = arg_cong [where f = uint]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1250
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1251
lemmas uint_word_ariths = 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1252
  word_arith_alts [THEN trans [OF uint_cong int_word_uint]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1253
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1254
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1255
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1256
(* similar expressions for sint (arith operations) *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1257
lemmas sint_word_ariths = uint_word_arith_bintrs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1258
  [THEN uint_sint [symmetric, THEN trans],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1259
  unfolded uint_sint bintr_arith1s bintr_ariths 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1260
    len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1261
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1262
lemmas uint_div_alt = word_div_def [THEN trans [OF uint_cong int_word_uint]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1263
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
  1264
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1265
lemma word_pred_0_n1: "word_pred 0 = word_of_int -1"
45550
73a4f31d41c4 Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents: 45549
diff changeset
  1266
  unfolding word_pred_def uint_eq_0 pred_def by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1267
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1268
lemma word_pred_0_Min: "word_pred 0 = word_of_int Int.Min"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1269
  by (simp add: word_pred_0_n1 number_of_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1270
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1271
lemma word_m1_Min: "- 1 = word_of_int Int.Min"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1272
  unfolding Min_def by (simp only: word_of_int_hom_syms)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1273
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1274
lemma succ_pred_no [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1275
  "word_succ (number_of bin) = number_of (Int.succ bin) & 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1276
    word_pred (number_of bin) = number_of (Int.pred bin)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1277
  unfolding word_number_of_def by (simp add : new_word_of_int_homs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1278
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1279
lemma word_sp_01 [simp] : 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1280
  "word_succ -1 = 0 & word_succ 0 = 1 & word_pred 0 = -1 & word_pred 1 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1281
  by (unfold word_0_no word_1_no) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1282
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1283
(* alternative approach to lifting arithmetic equalities *)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1284
lemma word_of_int_Ex:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1285
  "\<exists>y. x = word_of_int y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1286
  by (rule_tac x="uint x" in exI) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1287
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1288
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1289
subsection "Order on fixed-length words"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1290
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1291
lemma word_zero_le [simp] :
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1292
  "0 <= (y :: 'a :: len0 word)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1293
  unfolding word_le_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1294
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1295
lemma word_m1_ge [simp] : "word_pred 0 >= y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1296
  unfolding word_le_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1297
  by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1298
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1299
lemmas word_n1_ge [simp]  = word_m1_ge [simplified word_sp_01]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1300
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1301
lemmas word_not_simps [simp] = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1302
  word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1303
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1304
lemma word_gt_0: "0 < y = (0 ~= (y :: 'a :: len0 word))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1305
  unfolding word_less_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1306
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1307
lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of y"] for y
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1308
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1309
lemma word_sless_alt: "(a <s b) = (sint a < sint b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1310
  unfolding word_sle_def word_sless_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1311
  by (auto simp add: less_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1312
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1313
lemma word_le_nat_alt: "(a <= b) = (unat a <= unat b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1314
  unfolding unat_def word_le_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1315
  by (rule nat_le_eq_zle [symmetric]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1316
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1317
lemma word_less_nat_alt: "(a < b) = (unat a < unat b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1318
  unfolding unat_def word_less_alt
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1319
  by (rule nat_less_eq_zless [symmetric]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1320
  
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1321
lemma wi_less: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1322
  "(word_of_int n < (word_of_int m :: 'a :: len0 word)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1323
    (n mod 2 ^ len_of TYPE('a) < m mod 2 ^ len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1324
  unfolding word_less_alt by (simp add: word_uint.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1325
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1326
lemma wi_le: 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1327
  "(word_of_int n <= (word_of_int m :: 'a :: len0 word)) = 
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1328
    (n mod 2 ^ len_of TYPE('a) <= m mod 2 ^ len_of TYPE('a))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1329
  unfolding word_le_def by (simp add: word_uint.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1330
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1331
lemma udvd_nat_alt: "a udvd b = (EX n>=0. unat b = n * unat a)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1332
  apply (unfold udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1333
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1334
   apply (simp add: unat_def nat_mult_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1335
  apply (simp add: uint_nat int_mult)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1336
  apply (rule exI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1337
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1338
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1339
   apply (erule notE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1340
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1341
  apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1342
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1343
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1344
lemma udvd_iff_dvd: "x udvd y <-> unat x dvd unat y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1345
  unfolding dvd_def udvd_nat_alt by