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