src/HOL/Word/Word.thy
author haftmann
Sat, 04 Jul 2020 20:45:21 +0000
changeset 71996 c7ac6d4f3914
parent 71991 8bff286878bf
child 71997 4a013c92a091
permissions -rw-r--r--
prefer explicit proof
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
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 65363
diff changeset
     9
  "HOL-Library.Type_Length"
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 65363
diff changeset
    10
  "HOL-Library.Boolean_Algebra"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
    11
  "HOL-Library.Bit_Operations"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
    12
  Bits_Int
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
    13
  Bit_Comprehension
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
    14
  Misc_Typedef
70170
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    15
  Misc_Arithmetic
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    16
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    17
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
    18
subsection \<open>Type definition\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    19
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    20
quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    21
  morphisms rep_word word_of_int by (auto intro!: equivpI reflpI sympI transpI)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    22
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    23
lift_definition uint :: \<open>'a::len word \<Rightarrow> int\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    24
  is \<open>take_bit LENGTH('a)\<close> .
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    25
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    26
lemma uint_nonnegative: "0 \<le> uint w"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    27
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    28
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
    29
lemma uint_bounded: "uint w < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    30
  for w :: "'a::len word"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    31
  by transfer (simp add: take_bit_eq_mod)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    32
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
    33
lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    34
  for w :: "'a::len word"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    35
  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
    36
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    37
lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b"
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    38
  by transfer simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    39
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    40
lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    41
  using word_uint_eqI by auto
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
    42
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    43
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len word) = k mod 2 ^ LENGTH('a)"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    44
  by transfer (simp add: take_bit_eq_mod)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    45
  
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    46
lemma word_of_int_uint: "word_of_int (uint w) = w"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    47
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    48
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    49
lemma split_word_all: "(\<And>x::'a::len 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
    50
proof
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    51
  fix x :: "'a word"
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    52
  assume "\<And>x. PROP P (word_of_int x)"
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    53
  then have "PROP P (word_of_int (uint x))" .
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    54
  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
    55
qed
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    56
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    57
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
    58
subsection \<open>Type conversions and casting\<close>
55816
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
definition sint :: "'a::len word \<Rightarrow> int"
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
    61
  \<comment> \<open>treats the most-significant-bit as a sign bit\<close>
70175
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
    62
  where sint_uint: "sint w = sbintrunc (LENGTH('a) - 1) (uint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    63
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    64
definition unat :: "'a::len word \<Rightarrow> nat"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    65
  where "unat w = nat (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 uints :: "nat \<Rightarrow> int set"
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
    68
  \<comment> \<open>the sets of integers representing the words\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    69
  where "uints n = range (bintrunc n)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    70
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    71
definition sints :: "nat \<Rightarrow> int set"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    72
  where "sints n = range (sbintrunc (n - 1))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    73
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    74
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
    75
  by (simp add: uints_def range_bintrunc)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    76
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    77
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
    78
  by (simp add: sints_def range_sbintrunc)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    79
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    80
definition unats :: "nat \<Rightarrow> nat set"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    81
  where "unats n = {i. i < 2 ^ n}"
55816
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 norm_sint :: "nat \<Rightarrow> int \<Rightarrow> int"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    84
  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
    85
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    86
definition scast :: "'a::len word \<Rightarrow> 'b::len word"
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
    87
  \<comment> \<open>cast a word to a different length\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    88
  where "scast w = word_of_int (sint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    89
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    90
definition ucast :: "'a::len word \<Rightarrow> 'b::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    91
  where "ucast w = word_of_int (uint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    92
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    93
instantiation word :: (len) size
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    94
begin
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    95
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
    96
definition word_size: "size (w :: 'a word) = LENGTH('a)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    97
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    98
instance ..
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
    99
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   100
end
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   101
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   102
lemma word_size_gt_0 [iff]: "0 < size w"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   103
  for w :: "'a::len word"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   104
  by (simp add: word_size)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   105
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   106
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
   107
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   108
lemma lens_not_0 [iff]:
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
   109
  \<open>size w \<noteq> 0\<close> for  w :: \<open>'a::len word\<close>
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   110
  by auto
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   111
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   112
definition source_size :: "('a::len word \<Rightarrow> 'b) \<Rightarrow> nat"
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
   113
  \<comment> \<open>whether a cast (or other) function is to a longer or shorter length\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   114
  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
   115
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   116
definition target_size :: "('a \<Rightarrow> 'b::len word) \<Rightarrow> nat"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   117
  where [code del]: "target_size c = size (c undefined)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   118
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   119
definition is_up :: "('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   120
  where "is_up c \<longleftrightarrow> source_size c \<le> target_size c"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   121
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   122
definition is_down :: "('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   123
  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
   124
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   125
definition of_bl :: "bool list \<Rightarrow> 'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   126
  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
   127
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   128
definition to_bl :: "'a::len word \<Rightarrow> bool list"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   129
  where "to_bl w = bin_to_bl (LENGTH('a)) (uint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   130
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   131
definition word_reverse :: "'a::len word \<Rightarrow> 'a word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   132
  where "word_reverse w = of_bl (rev (to_bl w))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   133
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   134
definition word_int_case :: "(int \<Rightarrow> 'b) \<Rightarrow> 'a::len word \<Rightarrow> 'b"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   135
  where "word_int_case f w = f (uint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   136
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   137
translations
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   138
  "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
   139
  "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
   140
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   141
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   142
subsection \<open>Basic code generation setup\<close>
55817
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   143
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   144
definition Word :: "int \<Rightarrow> 'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   145
  where [code_post]: "Word = word_of_int"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   146
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   147
lemma [code abstype]: "Word (uint w) = w"
55817
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   148
  by (simp add: Word_def word_of_int_uint)
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   149
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   150
declare uint_word_of_int [code abstract]
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   151
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   152
instantiation word :: (len) equal
55817
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   153
begin
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   154
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   155
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   156
  where "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   157
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   158
instance
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   159
  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
   160
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   161
end
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   162
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   163
notation fcomp (infixl "\<circ>>" 60)
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   164
notation scomp (infixl "\<circ>\<rightarrow>" 60)
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   165
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   166
instantiation word :: ("{len, typerep}") random
55817
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   167
begin
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   168
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   169
definition
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   170
  "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
   171
     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
   172
     in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))"
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
instance ..
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   175
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   176
end
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   177
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   178
no_notation fcomp (infixl "\<circ>>" 60)
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   179
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   180
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   181
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   182
subsection \<open>Type-definition locale instantiations\<close>
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   183
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   184
lemmas uint_0 = uint_nonnegative (* FIXME duplicate *)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   185
lemmas uint_lt = uint_bounded (* FIXME duplicate *)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   186
lemmas uint_mod_same = uint_idem (* FIXME duplicate *)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   187
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   188
lemma td_ext_uint:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   189
  "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len)))
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   190
    (\<lambda>w::int. w mod 2 ^ LENGTH('a))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   191
  apply (unfold td_ext_def')
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   192
  apply transfer
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   193
  apply (simp add: uints_num take_bit_eq_mod)
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   194
  done
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   195
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   196
interpretation word_uint:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   197
  td_ext
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   198
    "uint::'a::len word \<Rightarrow> int"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   199
    word_of_int
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   200
    "uints (LENGTH('a::len))"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   201
    "\<lambda>w. w mod 2 ^ LENGTH('a::len)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   202
  by (fact td_ext_uint)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   203
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   204
lemmas td_uint = word_uint.td_thm
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   205
lemmas int_word_uint = word_uint.eq_norm
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   206
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   207
lemma td_ext_ubin:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   208
  "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len)))
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   209
    (bintrunc (LENGTH('a)))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   210
  by (unfold no_bintr_alt1) (fact td_ext_uint)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   211
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   212
interpretation word_ubin:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   213
  td_ext
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   214
    "uint::'a::len word \<Rightarrow> int"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   215
    word_of_int
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   216
    "uints (LENGTH('a::len))"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   217
    "bintrunc (LENGTH('a::len))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   218
  by (fact td_ext_ubin)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   219
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   220
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   221
subsection \<open>Arithmetic operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   222
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   223
lift_definition word_succ :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x + 1"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   224
  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
   225
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   226
lift_definition word_pred :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x - 1"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   227
  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
   228
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   229
instantiation word :: (len) "{neg_numeral, modulo, comm_monoid_mult, comm_ring}"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   230
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   231
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   232
lift_definition zero_word :: "'a word" is "0" .
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   233
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   234
lift_definition one_word :: "'a word" is "1" .
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   235
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
   236
lift_definition plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(+)"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   237
  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
   238
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
   239
lift_definition minus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(-)"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   240
  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
   241
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   242
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
   243
  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
   244
69064
5840724b1d71 Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents: 68157
diff changeset
   245
lift_definition times_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(*)"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   246
  by (auto simp add: bintrunc_mod2p intro: mod_mult_cong)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   247
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   248
lift_definition divide_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   249
  is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   250
  by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   251
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   252
lift_definition modulo_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   253
  is "\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   254
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   255
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   256
instance
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 61076
diff changeset
   257
  by standard (transfer, simp add: algebra_simps)+
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   258
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   259
end
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   260
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   261
lemma word_div_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   262
  "a div b = word_of_int (uint a div uint b)"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   263
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   264
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   265
lemma word_mod_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   266
  "a mod b = word_of_int (uint a mod uint b)"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   267
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   268
70901
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   269
quickcheck_generator word
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   270
  constructors:
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   271
    "zero_class.zero :: ('a::len) word",
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   272
    "numeral :: num \<Rightarrow> ('a::len) word",
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   273
    "uminus :: ('a::len) word \<Rightarrow> ('a::len) word"
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   274
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   275
context
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   276
  includes lifting_syntax
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   277
  notes power_transfer [transfer_rule]
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   278
begin
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   279
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   280
lemma power_transfer_word [transfer_rule]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   281
  \<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close>
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   282
  by transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   283
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   284
end
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   285
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   286
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   287
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   288
text \<open>Legacy theorems:\<close>
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   289
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   290
lemma word_arith_wis [code]:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   291
  shows word_add_def: "a + b = word_of_int (uint a + uint b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   292
    and word_sub_wi: "a - b = word_of_int (uint a - uint b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   293
    and word_mult_def: "a * b = word_of_int (uint a * uint b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   294
    and word_minus_def: "- a = word_of_int (- uint a)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   295
    and word_succ_alt: "word_succ a = word_of_int (uint a + 1)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   296
    and word_pred_alt: "word_pred a = word_of_int (uint a - 1)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   297
    and word_0_wi: "0 = word_of_int 0"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   298
    and word_1_wi: "1 = word_of_int 1"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   299
         apply (simp_all flip: plus_word.abs_eq minus_word.abs_eq
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   300
           times_word.abs_eq uminus_word.abs_eq
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   301
           zero_word.abs_eq one_word.abs_eq)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   302
   apply transfer
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   303
   apply simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   304
  apply transfer
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   305
  apply simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   306
  done
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   307
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   308
lemma wi_homs:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   309
  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
   310
    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
   311
    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
   312
    and wi_hom_neg: "- word_of_int a = word_of_int (- a)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   313
    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
   314
    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
   315
  by (transfer, simp)+
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   316
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   317
lemmas wi_hom_syms = wi_homs [symmetric]
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   318
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   319
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi
46009
5cb7ef5bfef2 remove duplicate lemma lists
huffman
parents: 46001
diff changeset
   320
5cb7ef5bfef2 remove duplicate lemma lists
huffman
parents: 46001
diff changeset
   321
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
   322
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   323
instance word :: (len) comm_monoid_add ..
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   324
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   325
instance word :: (len) semiring_numeral ..
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   326
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   327
instance word :: (len) comm_ring_1
45810
024947a0e492 prove class instances without extra lemmas
huffman
parents: 45809
diff changeset
   328
proof
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   329
  have *: "0 < LENGTH('a)" by (rule len_gt_0)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   330
  show "(0::'a word) \<noteq> 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   331
    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
   332
qed
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   333
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   334
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
   335
  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
   336
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   337
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
   338
  apply (rule ext)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   339
  apply (case_tac x rule: int_diff_cases)
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   340
  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
   341
  done
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   342
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   343
context
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   344
  includes lifting_syntax
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   345
  notes 
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   346
    transfer_rule_of_bool [transfer_rule]
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   347
    transfer_rule_numeral [transfer_rule]
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   348
    transfer_rule_of_nat [transfer_rule]
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   349
    transfer_rule_of_int [transfer_rule]
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   350
begin
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   351
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   352
lemma [transfer_rule]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   353
  "((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) of_bool of_bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   354
  by transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   355
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   356
lemma [transfer_rule]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   357
  "((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) numeral numeral"
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   358
  by transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   359
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   360
lemma [transfer_rule]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   361
  "((=) ===> pcr_word) int of_nat"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   362
  by transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   363
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   364
lemma [transfer_rule]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   365
  "((=) ===> pcr_word) (\<lambda>k. k) of_int"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   366
proof -
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   367
  have "((=) ===> pcr_word) of_int of_int"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   368
    by transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   369
  then show ?thesis by (simp add: id_def)
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   370
qed
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   371
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   372
end
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   373
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   374
lemma word_of_int_eq:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   375
  "word_of_int = of_int"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   376
  by (rule ext) (transfer, rule)
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   377
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   378
definition udvd :: "'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool" (infixl "udvd" 50)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   379
  where "a udvd b = (\<exists>n\<ge>0. uint b = n * uint a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   380
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   381
context
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   382
  includes lifting_syntax
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   383
begin
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   384
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   385
lemma [transfer_rule]:
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   386
  \<open>(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)\<close>
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   387
proof -
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   388
  have even_word_unfold: "even k \<longleftrightarrow> (\<exists>l. take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l))" (is "?P \<longleftrightarrow> ?Q")
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   389
    for k :: int
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   390
  proof
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   391
    assume ?P
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   392
    then show ?Q
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   393
      by auto
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   394
  next
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   395
    assume ?Q
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   396
    then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" ..
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   397
    then have "even (take_bit LENGTH('a) k)"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   398
      by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   399
    then show ?P
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   400
      by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   401
  qed
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   402
  show ?thesis by (simp only: even_word_unfold [abs_def] dvd_def [where ?'a = "'a word", abs_def])
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   403
    transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   404
qed
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   405
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   406
end
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   407
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   408
instance word :: (len) semiring_modulo
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   409
proof
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   410
  show "a div b * b + a mod b = a" for a b :: "'a word"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   411
  proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   412
    fix k l :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   413
    define r :: int where "r = 2 ^ LENGTH('a)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   414
    then have r: "take_bit LENGTH('a) k = k mod r" for k
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   415
      by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   416
    have "k mod r = ((k mod r) div (l mod r) * (l mod r)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   417
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   418
      by (simp add: div_mult_mod_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   419
    also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   420
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   421
      by (simp add: mod_add_left_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   422
    also have "... = (((k mod r) div (l mod r) * l) mod r
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   423
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   424
      by (simp add: mod_mult_right_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   425
    finally have "k mod r = ((k mod r) div (l mod r) * l
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   426
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   427
      by (simp add: mod_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   428
    with r show "take_bit LENGTH('a) (take_bit LENGTH('a) k div take_bit LENGTH('a) l * l
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   429
      + take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   430
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   431
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   432
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   433
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   434
instance word :: (len) semiring_parity
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   435
proof
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   436
  show "\<not> 2 dvd (1::'a word)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   437
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   438
  show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   439
    for a :: "'a word"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   440
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   441
  show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   442
    for a :: "'a word"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   443
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   444
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   445
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   446
lemma exp_eq_zero_iff:
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   447
  \<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close>
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   448
  by transfer simp
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   449
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   450
lemma double_eq_zero_iff:
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   451
  \<open>2 * a = 0 \<longleftrightarrow> a = 0 \<or> a = 2 ^ (LENGTH('a) - Suc 0)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   452
  for a :: \<open>'a::len word\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   453
proof -
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   454
  define n where \<open>n = LENGTH('a) - Suc 0\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   455
  then have *: \<open>LENGTH('a) = Suc n\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   456
    by simp
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   457
  have \<open>a = 0\<close> if \<open>2 * a = 0\<close> and \<open>a \<noteq> 2 ^ (LENGTH('a) - Suc 0)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   458
    using that by transfer
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   459
      (auto simp add: take_bit_eq_0_iff take_bit_eq_mod *)
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   460
  moreover have \<open>2 ^ LENGTH('a) = (0 :: 'a word)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   461
    by transfer simp
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   462
  then have \<open>2 * 2 ^ (LENGTH('a) - Suc 0) = (0 :: 'a word)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   463
    by (simp add: *)
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   464
  ultimately show ?thesis
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   465
    by auto
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   466
qed
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   467
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   468
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   469
subsection \<open>Ordering\<close>
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   470
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   471
instantiation word :: (len) linorder
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   472
begin
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   473
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   474
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   475
  is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   476
  by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   477
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   478
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   479
  is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   480
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   481
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   482
instance
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   483
  by (standard; transfer) auto
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   484
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   485
end
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   486
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   487
interpretation word_order: ordering_top \<open>(\<le>)\<close> \<open>(<)\<close> \<open>- 1 :: 'a::len word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   488
  by (standard; transfer) (simp add: take_bit_eq_mod zmod_minus1)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   489
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   490
interpretation word_coorder: ordering_top \<open>(\<ge>)\<close> \<open>(>)\<close> \<open>0 :: 'a::len word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   491
  by (standard; transfer) simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   492
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   493
lemma word_le_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   494
  "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   495
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   496
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   497
lemma word_less_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   498
  "a < b \<longleftrightarrow> uint a < uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   499
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   500
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   501
lemma word_greater_zero_iff:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   502
  \<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len word\<close>
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   503
  by transfer (simp add: less_le)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   504
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   505
lemma of_nat_word_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   506
  \<open>of_nat m = (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   507
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   508
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   509
lemma of_nat_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   510
  \<open>of_nat m \<le> (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   511
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   512
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   513
lemma of_nat_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   514
  \<open>of_nat m < (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   515
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   516
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   517
lemma of_nat_word_eq_0_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   518
  \<open>of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   519
  using of_nat_word_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   520
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   521
lemma of_int_word_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   522
  \<open>of_int k = (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   523
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   524
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   525
lemma of_int_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   526
  \<open>of_int k \<le> (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   527
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   528
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   529
lemma of_int_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   530
  \<open>of_int k < (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   531
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   532
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   533
lemma of_int_word_eq_0_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   534
  \<open>of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   535
  using of_int_word_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   536
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   537
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
   538
  where "a <=s b \<longleftrightarrow> sint a \<le> sint b"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   539
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   540
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
   541
  where "x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   542
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   543
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   544
subsection \<open>Bit-wise operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   545
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   546
lemma word_bit_induct [case_names zero even odd]:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   547
  \<open>P a\<close> if word_zero: \<open>P 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   548
    and word_even: \<open>\<And>a. P a \<Longrightarrow> 0 < a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (2 * a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   549
    and word_odd: \<open>\<And>a. P a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (1 + 2 * a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   550
  for P and a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   551
proof -
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   552
  define m :: nat where \<open>m = LENGTH('a) - 1\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   553
  then have l: \<open>LENGTH('a) = Suc m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   554
    by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   555
  define n :: nat where \<open>n = unat a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   556
  then have \<open>n < 2 ^ LENGTH('a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   557
    by (unfold unat_def) (transfer, simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   558
  then have \<open>n < 2 * 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   559
    by (simp add: l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   560
  then have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   561
  proof (induction n rule: nat_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   562
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   563
    show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   564
      by simp (rule word_zero)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   565
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   566
    case (even n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   567
    then have \<open>n < 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   568
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   569
    with even.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   570
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   571
    moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   572
      by (auto simp add: word_greater_zero_iff of_nat_word_eq_0_iff l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   573
    moreover from \<open>n < 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   574
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   575
      by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   576
    ultimately have \<open>P (2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   577
      by (rule word_even)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   578
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   579
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   580
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   581
    case (odd n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   582
    then have \<open>Suc n \<le> 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   583
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   584
    with odd.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   585
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   586
    moreover from \<open>Suc n \<le> 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   587
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   588
      by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   589
    ultimately have \<open>P (1 + 2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   590
      by (rule word_odd)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   591
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   592
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   593
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   594
  moreover have \<open>of_nat (nat (uint a)) = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   595
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   596
  ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   597
    by (simp add: n_def unat_def)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   598
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   599
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   600
lemma bit_word_half_eq:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   601
  \<open>(of_bool b + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   602
    if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   603
    for a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   604
proof (cases \<open>2 \<le> LENGTH('a::len)\<close>)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   605
  case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   606
  have \<open>of_bool (odd k) < (1 :: int) \<longleftrightarrow> even k\<close> for k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   607
    by auto
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   608
  with False that show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   609
    by transfer (simp add: eq_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   610
next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   611
  case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   612
  obtain n where length: \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   613
    by (cases \<open>LENGTH('a)\<close>) simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   614
  show ?thesis proof (cases b)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   615
    case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   616
    moreover have \<open>a * 2 div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   617
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   618
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   619
      from length have \<open>k * 2 mod 2 ^ LENGTH('a) = (k mod 2 ^ n) * 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   620
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   621
      moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   622
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   623
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   624
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   625
      ultimately have \<open>take_bit LENGTH('a) (k * 2) = take_bit LENGTH('a) k * 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   626
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   627
      with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (k * 2) div take_bit LENGTH('a) 2)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   628
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   629
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   630
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   631
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   632
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   633
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   634
    case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   635
    moreover have \<open>(1 + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   636
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   637
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   638
      from length have \<open>(1 + k * 2) mod 2 ^ LENGTH('a) = 1 + (k mod 2 ^ n) * 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   639
        using pos_zmod_mult_2 [of \<open>2 ^ n\<close> k] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   640
      moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   641
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   642
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   643
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   644
      ultimately have \<open>take_bit LENGTH('a) (1 + k * 2) = 1 + take_bit LENGTH('a) k * 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   645
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   646
      with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (1 + k * 2) div take_bit LENGTH('a) 2)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   647
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   648
        by (auto simp add: take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   649
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   650
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   651
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   652
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   653
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   654
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   655
lemma even_mult_exp_div_word_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   656
  \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> (
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   657
    m \<le> n \<and>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   658
    n < LENGTH('a) \<and> odd (a div 2 ^ (n - m)))\<close> for a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   659
  by transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   660
    (auto simp flip: drop_bit_eq_div simp add: even_drop_bit_iff_not_bit bit_take_bit_iff,
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   661
      simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   662
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   663
instantiation word :: (len) semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   664
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   665
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   666
lift_definition bit_word :: \<open>'a word \<Rightarrow> nat \<Rightarrow> bool\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   667
  is \<open>\<lambda>k n. n < LENGTH('a) \<and> bit k n\<close>
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   668
proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   669
  fix k l :: int and n :: nat
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   670
  assume *: \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   671
  show \<open>n < LENGTH('a) \<and> bit k n \<longleftrightarrow> n < LENGTH('a) \<and> bit l n\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   672
  proof (cases \<open>n < LENGTH('a)\<close>)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   673
    case True
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   674
    from * have \<open>bit (take_bit LENGTH('a) k) n \<longleftrightarrow> bit (take_bit LENGTH('a) l) n\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   675
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   676
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   677
      by (simp add: bit_take_bit_iff)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   678
  next
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   679
    case False
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   680
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   681
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   682
  qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   683
qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   684
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   685
instance proof
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   686
  show \<open>P a\<close> if stable: \<open>\<And>a. a div 2 = a \<Longrightarrow> P a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   687
    and rec: \<open>\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   688
  for P and a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   689
  proof (induction a rule: word_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   690
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   691
    have \<open>0 div 2 = (0::'a word)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   692
      by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   693
    with stable [of 0] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   694
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   695
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   696
    case (even a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   697
    with rec [of a False] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   698
      using bit_word_half_eq [of a False] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   699
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   700
    case (odd a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   701
    with rec [of a True] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   702
      using bit_word_half_eq [of a True] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   703
  qed
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   704
  show \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close> for a :: \<open>'a word\<close> and n
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   705
    by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit bit_iff_odd_drop_bit)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   706
  show \<open>0 div a = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   707
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   708
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   709
  show \<open>a div 1 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   710
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   711
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   712
  show \<open>a mod b div b = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   713
    for a b :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   714
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   715
    apply (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   716
    apply (subst (3) mod_pos_pos_trivial [of _ \<open>2 ^ LENGTH('a)\<close>])
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   717
      apply simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   718
     apply (metis le_less mod_by_0 pos_mod_conj zero_less_numeral zero_less_power)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   719
    using pos_mod_bound [of \<open>2 ^ LENGTH('a)\<close>] apply simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   720
  proof -
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   721
    fix aa :: int and ba :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   722
    have f1: "\<And>i n. (i::int) mod 2 ^ n = 0 \<or> 0 < i mod 2 ^ n"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   723
      by (metis le_less take_bit_eq_mod take_bit_nonnegative)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   724
    have "(0::int) < 2 ^ len_of (TYPE('a)::'a itself) \<and> ba mod 2 ^ len_of (TYPE('a)::'a itself) \<noteq> 0 \<or> aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   725
      by (metis (no_types) mod_by_0 unique_euclidean_semiring_numeral_class.pos_mod_bound zero_less_numeral zero_less_power)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   726
    then show "aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   727
      using f1 by (meson le_less less_le_trans unique_euclidean_semiring_numeral_class.pos_mod_bound)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   728
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   729
  show \<open>(1 + a) div 2 = a div 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   730
    if \<open>even a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   731
    for a :: \<open>'a word\<close>
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   732
    using that by transfer
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   733
      (auto dest: le_Suc_ex simp add: mod_2_eq_odd take_bit_Suc elim!: evenE)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   734
  show \<open>(2 :: 'a word) ^ m div 2 ^ n = of_bool ((2 :: 'a word) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m - n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   735
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   736
    by transfer (simp, simp add: exp_div_exp_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   737
  show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   738
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   739
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   740
    apply (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: drop_bit_eq_div)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   741
    apply (simp add: drop_bit_take_bit)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   742
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   743
  show "a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   744
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   745
    by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   746
  show \<open>a * 2 ^ m mod 2 ^ n = a mod 2 ^ (n - m) * 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   747
    if \<open>m \<le> n\<close> for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   748
    using that apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   749
    apply (auto simp flip: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   750
           apply (auto simp flip: push_bit_eq_mult simp add: push_bit_take_bit split: split_min_lin)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   751
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   752
  show \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   753
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   754
    by transfer (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: take_bit_eq_mod drop_bit_eq_div split: split_min_lin)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   755
  show \<open>even ((2 ^ m - 1) div (2::'a word) ^ n) \<longleftrightarrow> 2 ^ n = (0::'a word) \<or> m \<le> n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   756
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   757
    by transfer (auto simp add: take_bit_of_mask even_mask_div_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   758
  show \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::'a word) ^ n = 0 \<or> m \<le> n \<and> even (a div 2 ^ (n - m))\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   759
    for a :: \<open>'a word\<close> and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   760
  proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   761
    show \<open>even (take_bit LENGTH('a) (k * 2 ^ m) div take_bit LENGTH('a) (2 ^ n)) \<longleftrightarrow>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   762
      n < m
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   763
      \<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   764
      \<or> (m \<le> n \<and> even (take_bit LENGTH('a) k div take_bit LENGTH('a) (2 ^ (n - m))))\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   765
    for m n :: nat and k l :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   766
      by (auto simp flip: take_bit_eq_mod drop_bit_eq_div push_bit_eq_mult
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   767
        simp add: div_push_bit_of_1_eq_drop_bit drop_bit_take_bit drop_bit_push_bit_int [of n m])
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   768
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   769
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   770
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   771
end
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   772
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   773
instantiation word :: (len) semiring_bit_shifts
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   774
begin
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   775
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   776
lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   777
  is push_bit
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   778
proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   779
  show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   780
    if \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> for k l :: int and n :: nat
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   781
  proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   782
    from that
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   783
    have \<open>take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   784
      = take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   785
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   786
    moreover have \<open>min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   787
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   788
    ultimately show ?thesis
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   789
      by (simp add: take_bit_push_bit)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   790
  qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   791
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   792
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   793
lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   794
  is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   795
  by (simp add: take_bit_eq_mod)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   796
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   797
lift_definition take_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   798
  is \<open>\<lambda>n. take_bit (min LENGTH('a) n)\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   799
  by (simp add: ac_simps) (simp only: flip: take_bit_take_bit)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   800
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   801
instance proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   802
  show \<open>push_bit n a = a * 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close>
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   803
    by transfer (simp add: push_bit_eq_mult)
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   804
  show \<open>drop_bit n a = a div 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close>
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   805
    by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit)
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   806
  show \<open>take_bit n a = a mod 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   807
    by transfer (auto simp flip: take_bit_eq_mod)
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   808
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   809
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   810
end
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   811
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   812
lemma bit_word_eqI:
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   813
  \<open>a = b\<close> if \<open>\<And>n. n \<le> LENGTH('a) \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   814
  for a b :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   815
  using that by transfer (auto simp add: nat_less_le bit_eq_iff bit_take_bit_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   816
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   817
lemma bit_imp_le_length:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   818
  \<open>n < LENGTH('a)\<close> if \<open>bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   819
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   820
  using that by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   821
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   822
lemma not_bit_length [simp]:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   823
  \<open>\<not> bit w LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   824
  by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   825
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   826
lemma bit_word_of_int_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   827
  \<open>bit (word_of_int k :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> bit k n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   828
  by transfer rule
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   829
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   830
lemma bit_uint_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   831
  \<open>bit (uint w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   832
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   833
  by transfer (simp add: bit_take_bit_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   834
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   835
lemma bit_sint_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   836
  \<open>bit (sint w) n \<longleftrightarrow> n \<ge> LENGTH('a) \<and> bit w (LENGTH('a) - 1) \<or> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   837
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   838
  apply (cases \<open>LENGTH('a)\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   839
   apply simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   840
  apply (simp add: sint_uint nth_sbintr not_less bit_uint_iff not_le Suc_le_eq)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   841
  apply (auto simp add: le_less dest: bit_imp_le_length)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   842
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   843
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   844
lemma bit_word_ucast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   845
  \<open>bit (ucast w :: 'b::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < LENGTH('b) \<and> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   846
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   847
  by (simp add: ucast_def bit_word_of_int_iff bit_uint_iff ac_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   848
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   849
lemma bit_word_scast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   850
  \<open>bit (scast w :: 'b::len word) n \<longleftrightarrow>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   851
    n < LENGTH('b) \<and> (bit w n \<or> LENGTH('a) \<le> n \<and> bit w (LENGTH('a) - Suc 0))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   852
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   853
  by (simp add: scast_def bit_word_of_int_iff bit_sint_iff ac_simps)
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   854
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   855
definition shiftl1 :: "'a::len word \<Rightarrow> 'a word"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   856
  where "shiftl1 w = word_of_int (2 * uint w)"
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   857
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   858
lemma shiftl1_eq_mult_2:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   859
  \<open>shiftl1 = (*) 2\<close>
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   860
  apply (simp add: fun_eq_iff shiftl1_def)
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   861
  apply transfer
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   862
  apply (simp only: mult_2 take_bit_add)
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   863
  apply simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   864
  done
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   865
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   866
lemma bit_shiftl1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   867
  \<open>bit (shiftl1 w) n \<longleftrightarrow> 0 < n \<and> n < LENGTH('a) \<and> bit w (n - 1)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   868
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   869
  by (simp add: shiftl1_eq_mult_2 bit_double_iff exp_eq_zero_iff not_le) (simp add: ac_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   870
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   871
definition shiftr1 :: "'a::len word \<Rightarrow> 'a word"
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   872
  \<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close>
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   873
  where "shiftr1 w = word_of_int (bin_rest (uint w))"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   874
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   875
lemma shiftr1_eq_div_2:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   876
  \<open>shiftr1 w = w div 2\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   877
  apply (simp add: fun_eq_iff shiftr1_def)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   878
  apply transfer
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   879
  apply (auto simp add: not_le dest: less_2_cases)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   880
  done
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   881
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   882
lemma bit_shiftr1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   883
  \<open>bit (shiftr1 w) n \<longleftrightarrow> bit w (Suc n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   884
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   885
  by (simp add: shiftr1_eq_div_2 bit_Suc)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   886
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   887
instantiation word :: (len) ring_bit_operations
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   888
begin
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   889
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   890
lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   891
  is not
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   892
  by (simp add: take_bit_not_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   893
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   894
lift_definition and_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   895
  is \<open>and\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   896
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   897
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   898
lift_definition or_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   899
  is or
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   900
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   901
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   902
lift_definition xor_word ::  \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   903
  is xor
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   904
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   905
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   906
instance proof
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   907
  fix a b :: \<open>'a word\<close> and n :: nat
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   908
  show \<open>- a = NOT (a - 1)\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   909
    by transfer (simp add: minus_eq_not_minus_1)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   910
  show \<open>bit (NOT a) n \<longleftrightarrow> (2 :: 'a word) ^ n \<noteq> 0 \<and> \<not> bit a n\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   911
    by transfer (simp add: bit_not_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   912
  show \<open>bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   913
    by transfer (auto simp add: bit_and_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   914
  show \<open>bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   915
    by transfer (auto simp add: bit_or_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   916
  show \<open>bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   917
    by transfer (auto simp add: bit_xor_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   918
qed
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   919
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   920
end
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   921
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   922
instantiation word :: (len) bit_operations
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   923
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   924
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   925
definition word_test_bit_def: "test_bit a = bin_nth (uint a)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   926
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   927
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
   928
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   929
definition word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   930
70175
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
   931
definition "msb a \<longleftrightarrow> bin_sign (sbintrunc (LENGTH('a) - 1) (uint a)) = - 1"
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
   932
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   933
definition shiftl_def: "w << n = (shiftl1 ^^ n) w"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   934
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   935
definition shiftr_def: "w >> n = (shiftr1 ^^ n) w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   936
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   937
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   938
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   939
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   940
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   941
lemma test_bit_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   942
  \<open>test_bit w = bit w\<close> for w :: \<open>'a::len word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   943
  apply (simp add: word_test_bit_def fun_eq_iff)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   944
  apply transfer
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   945
  apply (simp add: bit_take_bit_iff)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   946
  done
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   947
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   948
lemma set_bit_unfold:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   949
  \<open>set_bit w n b = (if b then Bit_Operations.set_bit n w else unset_bit n w)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   950
  for w :: \<open>'a::len word\<close>
71991
8bff286878bf misc lemma tuning
haftmann
parents: 71990
diff changeset
   951
  apply (auto simp add: word_set_bit_def bin_clr_conv_NAND bin_set_conv_OR unset_bit_def set_bit_def shiftl_int_def push_bit_of_1; transfer)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   952
   apply simp_all
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   953
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   954
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   955
lemma bit_set_bit_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   956
  \<open>bit (set_bit w m b) n \<longleftrightarrow> (if m = n then n < LENGTH('a) \<and> b else bit w n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   957
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   958
  by (auto simp add: set_bit_unfold bit_unset_bit_iff bit_set_bit_iff exp_eq_zero_iff not_le bit_imp_le_length)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   959
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   960
lemma lsb_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   961
  \<open>lsb = (odd :: 'a word \<Rightarrow> bool)\<close> for w :: \<open>'a::len word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   962
  apply (simp add: word_lsb_def fun_eq_iff)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   963
  apply transfer
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   964
  apply simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   965
  done
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   966
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   967
lemma msb_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   968
  \<open>msb w \<longleftrightarrow> bit w (LENGTH('a) - 1)\<close> for w :: \<open>'a::len word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   969
  apply (simp add: msb_word_def bin_sign_lem)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   970
  apply transfer
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   971
  apply (simp add: bit_take_bit_iff)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   972
  done
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   973
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   974
lemma shiftl_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   975
  \<open>w << n = push_bit n w\<close> for w :: \<open>'a::len word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   976
  by (induction n) (simp_all add: shiftl_def shiftl1_eq_mult_2 push_bit_double)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   977
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   978
lemma bit_shiftl_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   979
  \<open>bit (w << m) n \<longleftrightarrow> m \<le> n \<and> n < LENGTH('a) \<and> bit w (n - m)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   980
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   981
  by (simp add: shiftl_word_eq bit_push_bit_iff exp_eq_zero_iff not_le)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   982
71955
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
   983
lemma [code]:
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
   984
  \<open>push_bit n w = w << n\<close> for w :: \<open>'a::len word\<close>
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
   985
  by (simp add: shiftl_word_eq)
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
   986
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   987
lemma shiftr_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   988
  \<open>w >> n = drop_bit n w\<close> for w :: \<open>'a::len word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   989
  by (induction n) (simp_all add: shiftr_def shiftr1_eq_div_2 drop_bit_Suc drop_bit_half)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   990
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   991
lemma bit_shiftr_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   992
  \<open>bit (w >> m) n \<longleftrightarrow> bit w (m + n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   993
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   994
  by (simp add: shiftr_word_eq bit_drop_bit_eq)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   995
71955
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
   996
lemma [code]:
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
   997
  \<open>drop_bit n w = w >> n\<close> for w :: \<open>'a::len word\<close>
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
   998
  by (simp add: shiftr_word_eq)
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
   999
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1000
lemma [code]:
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1001
  \<open>take_bit n a = a AND Bit_Operations.mask n\<close> for a :: \<open>'a::len word\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1002
  by (fact take_bit_eq_mask)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1003
71955
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1004
lemma [code_abbrev]:
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1005
  \<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close>
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1006
  by (fact push_bit_of_1)
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1007
70175
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
  1008
lemma word_msb_def:
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
  1009
  "msb a \<longleftrightarrow> bin_sign (sint a) = - 1"
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
  1010
  by (simp add: msb_word_def sint_uint)
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
  1011
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1012
lemma [code]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1013
  shows word_not_def: "NOT (a::'a::len word) = word_of_int (NOT (uint a))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1014
    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
  1015
    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
  1016
    and word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1017
  by (transfer, simp add: take_bit_not_take_bit)+
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
  1018
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1019
definition setBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1020
  where "setBit w n = set_bit w n True"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1021
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1022
lemma setBit_eq_set_bit:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1023
  \<open>setBit w n = Bit_Operations.set_bit n w\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1024
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1025
  by (simp add: setBit_def set_bit_unfold)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1026
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1027
lemma bit_setBit_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1028
  \<open>bit (setBit w m) n \<longleftrightarrow> (m = n \<and> n < LENGTH('a) \<or> bit w n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1029
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1030
  by (simp add: setBit_eq_set_bit bit_set_bit_iff exp_eq_zero_iff not_le ac_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1031
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1032
definition clearBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1033
  where "clearBit w n = set_bit w n False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1034
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1035
lemma clearBit_eq_unset_bit:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1036
  \<open>clearBit w n = unset_bit n w\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1037
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1038
  by (simp add: clearBit_def set_bit_unfold)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1039
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1040
lemma bit_clearBit_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1041
  \<open>bit (clearBit w m) n \<longleftrightarrow> m \<noteq> n \<and> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1042
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1043
  by (simp add: clearBit_eq_unset_bit bit_unset_bit_iff ac_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1044
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1045
definition even_word :: \<open>'a::len word \<Rightarrow> bool\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1046
  where [code_abbrev]: \<open>even_word = even\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1047
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1048
lemma even_word_iff [code]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1049
  \<open>even_word a \<longleftrightarrow> a AND 1 = 0\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1050
  by (simp add: and_one_eq even_iff_mod_2_eq_zero even_word_def)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1051
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1052
lemma bit_word_iff_drop_bit_and [code]:
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1053
  \<open>bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> for a :: \<open>'a::len word\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1054
  by (simp add: bit_iff_odd_drop_bit odd_iff_mod_2_eq_one and_one_eq)
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1055
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1056
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1057
subsection \<open>Shift operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1058
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1059
definition sshiftr1 :: "'a::len word \<Rightarrow> 'a word"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1060
  where "sshiftr1 w = word_of_int (bin_rest (sint w))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1061
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1062
definition bshiftr1 :: "bool \<Rightarrow> 'a::len word \<Rightarrow> 'a word"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1063
  where "bshiftr1 b w = of_bl (b # butlast (to_bl w))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1064
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1065
definition sshiftr :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"  (infixl ">>>" 55)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1066
  where "w >>> n = (sshiftr1 ^^ n) w"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1067
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1068
definition mask :: "nat \<Rightarrow> 'a::len word"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1069
  where "mask n = (1 << n) - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1070
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1071
definition slice1 :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word"
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1072
  where "slice1 n w = of_bl (takefill False n (to_bl w))"
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1073
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1074
definition revcast :: "'a::len word \<Rightarrow> 'b::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1075
  where "revcast w =  of_bl (takefill False (LENGTH('b)) (to_bl w))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1076
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1077
lemma revcast_eq:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1078
  \<open>(revcast :: 'a::len word \<Rightarrow> 'b::len word) = slice1 LENGTH('b)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1079
  by (simp add: fun_eq_iff revcast_def slice1_def)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1080
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1081
definition slice :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1082
  where "slice n w = slice1 (size w - n) w"
37660
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>Rotation\<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
definition rotater1 :: "'a list \<Rightarrow> 'a list"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1088
  where "rotater1 ys =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1089
    (case ys of [] \<Rightarrow> [] | x # xs \<Rightarrow> last ys # butlast ys)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1090
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1091
definition rotater :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1092
  where "rotater n = rotater1 ^^ n"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1093
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1094
definition word_rotr :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1095
  where "word_rotr n w = of_bl (rotater n (to_bl w))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1096
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1097
definition word_rotl :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1098
  where "word_rotl n w = of_bl (rotate n (to_bl w))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1099
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1100
definition word_roti :: "int \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1101
  where "word_roti i w =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1102
    (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
  1103
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1104
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1105
subsection \<open>Split and cat operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1106
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1107
definition word_cat :: "'a::len word \<Rightarrow> 'b::len word \<Rightarrow> 'c::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1108
  where "word_cat a b = word_of_int (bin_cat (uint a) (LENGTH('b)) (uint b))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1109
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1110
lemma word_cat_eq:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1111
  \<open>(word_cat v w :: 'c::len word) = push_bit LENGTH('b) (ucast v) + ucast w\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1112
  for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1113
  apply (simp add: word_cat_def bin_cat_eq_push_bit_add_take_bit ucast_def)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1114
  apply transfer apply simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1115
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1116
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1117
lemma bit_word_cat_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1118
  \<open>bit (word_cat v w :: 'c::len word) n \<longleftrightarrow> n < LENGTH('c) \<and> (if n < LENGTH('b) then bit w n else bit v (n - LENGTH('b)))\<close> 
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1119
  for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1120
  by (auto simp add: word_cat_def bit_word_of_int_iff bin_nth_cat bit_uint_iff not_less bit_imp_le_length)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1121
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1122
definition word_split :: "'a::len word \<Rightarrow> 'b::len word \<times> 'c::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1123
  where "word_split a =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1124
    (case bin_split (LENGTH('c)) (uint a) of
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1125
      (u, v) \<Rightarrow> (word_of_int u, word_of_int v))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1126
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1127
definition word_rcat :: "'a::len word list \<Rightarrow> 'b::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1128
  where "word_rcat ws = word_of_int (bin_rcat (LENGTH('a)) (map uint ws))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1129
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1130
definition word_rsplit :: "'a::len word \<Rightarrow> 'b::len word list"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1131
  where "word_rsplit w = map word_of_int (bin_rsplit (LENGTH('b)) (LENGTH('a), uint w))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1132
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1133
abbreviation (input) max_word :: \<open>'a::len word\<close>
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
  1134
  \<comment> \<open>Largest representable machine integer.\<close>
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  1135
  where "max_word \<equiv> - 1"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1136
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1137
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1138
subsection \<open>Theorems about typedefs\<close>
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
  1139
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1140
lemma sint_sbintrunc': "sint (word_of_int bin :: 'a word) = sbintrunc (LENGTH('a::len) - 1) bin"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1141
  by (auto simp: sint_uint word_ubin.eq_norm sbintrunc_bintrunc_lt)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1142
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1143
lemma uint_sint: "uint w = bintrunc (LENGTH('a)) (sint w)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1144
  for w :: "'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1145
  by (auto simp: sint_uint bintrunc_sbintrunc_le)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1146
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1147
lemma bintr_uint: "LENGTH('a) \<le> n \<Longrightarrow> bintrunc n (uint w) = uint w"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1148
  for w :: "'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1149
  apply (subst word_ubin.norm_Rep [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1150
  apply (simp only: bintrunc_bintrunc_min word_size)
54863
82acc20ded73 prefer more canonical names for lemmas on min/max
haftmann
parents: 54854
diff changeset
  1151
  apply (simp add: min.absorb2)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1152
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1153
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  1154
lemma wi_bintr:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1155
  "LENGTH('a::len) \<le> n \<Longrightarrow>
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  1156
    word_of_int (bintrunc n w) = (word_of_int w :: 'a word)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1157
  by (auto simp: word_ubin.norm_eq_iff [symmetric] min.absorb1)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1158
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1159
lemma td_ext_sbin:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1160
  "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len)))
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1161
    (sbintrunc (LENGTH('a) - 1))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1162
  apply (unfold td_ext_def' sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1163
  apply (simp add : word_ubin.eq_norm)
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1164
  apply (cases "LENGTH('a)")
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1165
   apply (auto simp add : sints_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1166
  apply (rule sym [THEN trans])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1167
   apply (rule word_ubin.Abs_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1168
  apply (simp only: bintrunc_sbintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1169
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1170
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1171
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1172
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  1173
lemma td_ext_sint:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1174
  "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len)))
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1175
     (\<lambda>w. (w + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) -
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1176
         2 ^ (LENGTH('a) - 1))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  1177
  using td_ext_sbin [where ?'a = 'a] by (simp add: no_sbintr_alt2)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1178
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1179
text \<open>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1180
  We do \<open>sint\<close> before \<open>sbin\<close>, before \<open>sint\<close> is the user version
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1181
  and interpretations do not produce thm duplicates. I.e.
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1182
  we get the name \<open>word_sint.Rep_eqD\<close>, but not \<open>word_sbin.Req_eqD\<close>,
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1183
  because the latter is the same thm as the former.
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1184
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1185
interpretation word_sint:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1186
  td_ext
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1187
    "sint ::'a::len word \<Rightarrow> int"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1188
    word_of_int
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1189
    "sints (LENGTH('a::len))"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1190
    "\<lambda>w. (w + 2^(LENGTH('a::len) - 1)) mod 2^LENGTH('a::len) -
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1191
      2 ^ (LENGTH('a::len) - 1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1192
  by (rule td_ext_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1193
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1194
interpretation word_sbin:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1195
  td_ext
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1196
    "sint ::'a::len word \<Rightarrow> int"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1197
    word_of_int
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1198
    "sints (LENGTH('a::len))"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1199
    "sbintrunc (LENGTH('a::len) - 1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1200
  by (rule td_ext_sbin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1201
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1202
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1203
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1204
lemmas td_sint = word_sint.td
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1205
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1206
lemma to_bl_def': "(to_bl :: 'a::len word \<Rightarrow> bool list) = bin_to_bl (LENGTH('a)) \<circ> uint"
44762
8f9d09241a68 tuned proofs;
wenzelm
parents: 42793
diff changeset
  1207
  by (auto simp: to_bl_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1208
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1209
lemmas word_reverse_no_def [simp] =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1210
  word_reverse_def [of "numeral w"] for w
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1211
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1212
lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1213
  by (fact uints_def [unfolded no_bintr_alt1])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1214
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1215
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
  1216
  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
  1217
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1218
declare word_numeral_alt [symmetric, code_abbrev]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1219
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1220
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
  1221
  by (simp only: word_numeral_alt wi_hom_neg)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1222
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1223
declare word_neg_numeral_alt [symmetric, code_abbrev]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1224
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1225
lemma uint_bintrunc [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1226
  "uint (numeral bin :: 'a word) =
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1227
    bintrunc (LENGTH('a::len)) (numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1228
  unfolding word_numeral_alt by (rule word_ubin.eq_norm)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1229
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1230
lemma uint_bintrunc_neg [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1231
  "uint (- numeral bin :: 'a word) = bintrunc (LENGTH('a::len)) (- numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1232
  by (simp only: word_neg_numeral_alt word_ubin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1233
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1234
lemma sint_sbintrunc [simp]:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1235
  "sint (numeral bin :: 'a word) = sbintrunc (LENGTH('a::len) - 1) (numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1236
  by (simp only: word_numeral_alt word_sbin.eq_norm)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1237
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1238
lemma sint_sbintrunc_neg [simp]:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1239
  "sint (- numeral bin :: 'a word) = sbintrunc (LENGTH('a::len) - 1) (- numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1240
  by (simp only: word_neg_numeral_alt word_sbin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1241
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1242
lemma unat_bintrunc [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1243
  "unat (numeral bin :: 'a::len word) = nat (bintrunc (LENGTH('a)) (numeral bin))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1244
  by (simp only: unat_def uint_bintrunc)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1245
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1246
lemma unat_bintrunc_neg [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1247
  "unat (- numeral bin :: 'a::len word) = nat (bintrunc (LENGTH('a)) (- numeral bin))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1248
  by (simp only: unat_def uint_bintrunc_neg)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1249
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1250
lemma size_0_eq: "size w = 0 \<Longrightarrow> v = w"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1251
  for v w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1252
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1253
  apply (rule word_uint.Rep_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1254
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1255
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1256
    apply (rule word_ubin.norm_Rep)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1257
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1258
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1259
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1260
lemma uint_ge_0 [iff]: "0 \<le> uint x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1261
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1262
  using word_uint.Rep [of x] by (simp add: uints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1263
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1264
lemma uint_lt2p [iff]: "uint x < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1265
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1266
  using word_uint.Rep [of x] by (simp add: uints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1267
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  1268
lemma word_exp_length_eq_0 [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1269
  \<open>(2 :: 'a::len word) ^ LENGTH('a) = 0\<close>
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  1270
  by transfer (simp add: bintrunc_mod2p)
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  1271
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1272
lemma sint_ge: "- (2 ^ (LENGTH('a) - 1)) \<le> sint x"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1273
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1274
  using word_sint.Rep [of x] by (simp add: sints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1275
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1276
lemma sint_lt: "sint x < 2 ^ (LENGTH('a) - 1)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1277
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1278
  using word_sint.Rep [of x] by (simp add: sints_num)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1279
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1280
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
  1281
  by (simp add: sign_Pls_ge_0)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1282
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1283
lemma uint_m2p_neg: "uint x - 2 ^ LENGTH('a) < 0"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1284
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1285
  by (simp only: diff_less_0_iff_less uint_lt2p)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1286
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1287
lemma uint_m2p_not_non_neg: "\<not> 0 \<le> uint x - 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1288
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1289
  by (simp only: not_le uint_m2p_neg)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1290
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1291
lemma lt2p_lem: "LENGTH('a) \<le> n \<Longrightarrow> uint w < 2 ^ n"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1292
  for w :: "'a::len word"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  1293
  by (metis bintr_uint bintrunc_mod2p int_mod_lem zless2p)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1294
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1295
lemma uint_le_0_iff [simp]: "uint x \<le> 0 \<longleftrightarrow> uint x = 0"
70749
5d06b7bb9d22 More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
paulson <lp15@cam.ac.uk>
parents: 70342
diff changeset
  1296
  by (fact uint_ge_0 [THEN leD, THEN antisym_conv1])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1297
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1298
lemma uint_nat: "uint w = int (unat w)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1299
  by (auto simp: unat_def)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1300
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1301
lemma uint_numeral: "uint (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1302
  by (simp only: word_numeral_alt int_word_uint)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1303
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1304
lemma uint_neg_numeral: "uint (- numeral b :: 'a::len word) = - numeral b mod 2 ^ LENGTH('a)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1305
  by (simp only: word_neg_numeral_alt int_word_uint)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1306
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1307
lemma unat_numeral: "unat (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1308
  apply (unfold unat_def)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1309
  apply (clarsimp simp only: uint_numeral)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1310
  apply (rule nat_mod_distrib [THEN trans])
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1311
    apply (rule zero_le_numeral)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1312
   apply (simp_all add: nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1313
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1314
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1315
lemma sint_numeral:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1316
  "sint (numeral b :: 'a::len word) =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1317
    (numeral b +
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1318
      2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) -
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1319
      2 ^ (LENGTH('a) - 1)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1320
  unfolding word_numeral_alt by (rule int_word_sint)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1321
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1322
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0"
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1323
  unfolding word_0_wi ..
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1324
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1325
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1"
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1326
  unfolding word_1_wi ..
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1327
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  1328
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
  1329
  by (simp add: wi_hom_syms)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  1330
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1331
lemma word_of_int_numeral [simp] : "(word_of_int (numeral bin) :: 'a::len word) = numeral bin"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1332
  by (simp only: word_numeral_alt)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1333
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1334
lemma word_of_int_neg_numeral [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1335
  "(word_of_int (- numeral bin) :: 'a::len word) = - numeral bin"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1336
  by (simp only: word_numeral_alt wi_hom_syms)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1337
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1338
lemma word_int_case_wi:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1339
  "word_int_case f (word_of_int i :: 'b word) = f (i mod 2 ^ LENGTH('b::len))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1340
  by (simp add: word_int_case_def word_uint.eq_norm)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1341
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1342
lemma word_int_split:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1343
  "P (word_int_case f x) =
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1344
    (\<forall>i. x = (word_of_int i :: 'b::len word) \<and> 0 \<le> i \<and> i < 2 ^ LENGTH('b) \<longrightarrow> P (f i))"
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
  1345
  by (auto simp: word_int_case_def word_uint.eq_norm)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1346
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1347
lemma word_int_split_asm:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1348
  "P (word_int_case f x) =
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1349
    (\<nexists>n. x = (word_of_int n :: 'b::len word) \<and> 0 \<le> n \<and> n < 2 ^ LENGTH('b::len) \<and> \<not> P (f n))"
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
  1350
  by (auto simp: word_int_case_def word_uint.eq_norm)
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1351
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1352
lemmas uint_range' = word_uint.Rep [unfolded uints_num mem_Collect_eq]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1353
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
  1354
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1355
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
  1356
  unfolding word_size by (rule uint_range')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1357
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1358
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
  1359
  unfolding word_size by (rule sint_range')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1360
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1361
lemma sint_above_size: "2 ^ (size w - 1) \<le> x \<Longrightarrow> sint w < x"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1362
  for w :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1363
  unfolding word_size by (rule less_le_trans [OF sint_lt])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1364
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1365
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
  1366
  for w :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1367
  unfolding word_size by (rule order_trans [OF _ sint_ge])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1368
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  1369
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1370
subsection \<open>Testing bits\<close>
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
  1371
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1372
lemma test_bit_eq_iff: "test_bit u = test_bit v \<longleftrightarrow> u = v"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1373
  for u v :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1374
  unfolding word_test_bit_def by (simp add: bin_nth_eq_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1375
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1376
lemma test_bit_size [rule_format] : "w !! n \<longrightarrow> n < size w"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1377
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1378
  apply (unfold word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1379
  apply (subst word_ubin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1380
  apply (simp only: nth_bintr word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1381
  apply fast
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1382
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1383
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1384
lemma word_eq_iff: "x = y \<longleftrightarrow> (\<forall>n<LENGTH('a). x !! n = y !! n)" (is \<open>?P \<longleftrightarrow> ?Q\<close>)
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1385
  for x y :: "'a::len word"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1386
proof
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1387
  assume ?P
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1388
  then show ?Q
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1389
    by simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1390
next
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1391
  assume ?Q
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1392
  then have *: \<open>bit (uint x) n \<longleftrightarrow> bit (uint y) n\<close> if \<open>n < LENGTH('a)\<close> for n
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71948
diff changeset
  1393
    using that by (simp add: word_test_bit_def)
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1394
  show ?P
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1395
  proof (rule word_uint_eqI, rule bit_eqI, rule iffI)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1396
    fix n
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1397
    assume \<open>bit (uint x) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1398
    then have \<open>n < LENGTH('a)\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1399
      by (simp add: bit_take_bit_iff uint.rep_eq)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1400
    with * \<open>bit (uint x) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1401
    show \<open>bit (uint y) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1402
      by simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1403
  next
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1404
    fix n
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1405
    assume \<open>bit (uint y) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1406
    then have \<open>n < LENGTH('a)\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1407
      by (simp add: bit_take_bit_iff uint.rep_eq)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1408
    with * \<open>bit (uint y) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1409
    show \<open>bit (uint x) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1410
      by simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1411
  qed
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1412
qed  
46021
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
  1413
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1414
lemma word_eqI: "(\<And>n. n < size u \<longrightarrow> u !! n = v !! n) \<Longrightarrow> u = v"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1415
  for u :: "'a::len word"
46021
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
  1416
  by (simp add: word_size word_eq_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1417
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1418
lemma word_eqD: "u = v \<Longrightarrow> u !! x = v !! x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1419
  for u v :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1420
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1421
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1422
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
  1423
  by (simp add: word_test_bit_def word_size nth_bintr [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1424
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1425
lemmas test_bit_bin = test_bit_bin' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1426
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1427
lemma bin_nth_uint_imp: "bin_nth (uint w) n \<Longrightarrow> n < LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1428
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1429
  apply (rule nth_bintr [THEN iffD1, THEN conjunct1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1430
  apply (subst word_ubin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1431
  apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1432
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1433
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  1434
lemma bin_nth_sint:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1435
  "LENGTH('a) \<le> n \<Longrightarrow>
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1436
    bin_nth (sint w) n = bin_nth (sint w) (LENGTH('a) - 1)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1437
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1438
  apply (subst word_sbin.norm_Rep [symmetric])
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  1439
  apply (auto simp add: nth_sbintr)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1440
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1441
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1442
\<comment> \<open>type definitions theorem for in terms of equivalent bool list\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1443
lemma td_bl:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1444
  "type_definition
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1445
    (to_bl :: 'a::len word \<Rightarrow> bool list)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1446
    of_bl
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1447
    {bl. length bl = LENGTH('a)}"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1448
  apply (unfold type_definition_def of_bl_def to_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1449
  apply (simp add: word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1450
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1451
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1452
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1453
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1454
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1455
interpretation word_bl:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1456
  type_definition
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1457
    "to_bl :: 'a::len word \<Rightarrow> bool list"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1458
    of_bl
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1459
    "{bl. length bl = LENGTH('a::len)}"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  1460
  by (fact td_bl)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1461
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  1462
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
  1463
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1464
lemma word_size_bl: "size w = size (to_bl w)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1465
  by (auto simp: word_size)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1466
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1467
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
  1468
  by (fastforce elim!: word_bl.Abs_inverse [unfolded mem_Collect_eq])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1469
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1470
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
  1471
  by (simp add: word_reverse_def word_bl.Abs_inverse)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1472
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1473
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1474
  by (simp add: word_reverse_def word_bl.Abs_inverse)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1475
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1476
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
  1477
  by (metis word_rev_rev)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1478
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1479
lemma word_rev_gal': "u = word_reverse w \<Longrightarrow> w = word_reverse u"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1480
  by simp
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1481
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1482
lemma length_bl_gt_0 [iff]: "0 < length (to_bl x)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1483
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1484
  unfolding word_bl_Rep' by (rule len_gt_0)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1485
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1486
lemma bl_not_Nil [iff]: "to_bl x \<noteq> []"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1487
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1488
  by (fact length_bl_gt_0 [unfolded length_greater_0_conv])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1489
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1490
lemma length_bl_neq_0 [iff]: "length (to_bl x) \<noteq> 0"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1491
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1492
  by (fact length_bl_gt_0 [THEN gr_implies_not0])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1493
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  1494
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
  1495
  apply (unfold to_bl_def sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1496
  apply (rule trans [OF _ bl_sbin_sign])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1497
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1498
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1499
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1500
lemma of_bl_drop':
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1501
  "lend = length bl - LENGTH('a::len) \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1502
    of_bl (drop lend bl) = (of_bl bl :: 'a word)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1503
  by (auto simp: of_bl_def trunc_bl2bin [symmetric])
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1504
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1505
lemma test_bit_of_bl:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1506
  "(of_bl bl::'a::len word) !! n = (rev bl ! n \<and> n < LENGTH('a) \<and> n < length bl)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1507
  by (auto simp add: of_bl_def word_test_bit_def word_size
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1508
      word_ubin.eq_norm nth_bintr bin_nth_of_bl)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1509
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1510
lemma bit_of_bl_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1511
  \<open>bit (of_bl bs :: 'a word) n \<longleftrightarrow> rev bs ! n \<and> n < LENGTH('a::len) \<and> n < length bs\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1512
  using test_bit_of_bl [of bs n] by (simp add: test_bit_word_eq)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1513
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1514
lemma no_of_bl: "(numeral bin ::'a::len word) = of_bl (bin_to_bl (LENGTH('a)) (numeral bin))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1515
  by (simp add: of_bl_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1516
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1517
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1518
  by (auto simp: word_size to_bl_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1519
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1520
lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1521
  by (simp add: uint_bl word_size)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1522
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1523
lemma to_bl_of_bin: "to_bl (word_of_int bin::'a::len word) = bin_to_bl (LENGTH('a)) bin"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1524
  by (auto simp: uint_bl word_ubin.eq_norm word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1525
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1526
lemma to_bl_numeral [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1527
  "to_bl (numeral bin::'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1528
    bin_to_bl (LENGTH('a)) (numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1529
  unfolding word_numeral_alt by (rule to_bl_of_bin)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1530
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1531
lemma to_bl_neg_numeral [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1532
  "to_bl (- numeral bin::'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1533
    bin_to_bl (LENGTH('a)) (- numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1534
  unfolding word_neg_numeral_alt by (rule to_bl_of_bin)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1535
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1536
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
  1537
  by (simp add: uint_bl word_size)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1538
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1539
lemma uint_bl_bin: "bl_to_bin (bin_to_bl (LENGTH('a)) (uint x)) = uint x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1540
  for x :: "'a::len word"
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1541
  by (rule trans [OF bin_bl_bin word_ubin.norm_Rep])
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1542
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1543
\<comment> \<open>naturals\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1544
lemma uints_unats: "uints n = int ` unats n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1545
  apply (unfold unats_def uints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1546
  apply safe
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1547
    apply (rule_tac image_eqI)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1548
     apply (erule_tac nat_0_le [symmetric])
66912
a99a7cbf0fb5 generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents: 66808
diff changeset
  1549
  by auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1550
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1551
lemma unats_uints: "unats n = nat ` uints n"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1552
  by (auto simp: uints_unats image_iff)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1553
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1554
lemmas bintr_num =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1555
  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
  1556
lemmas sbintr_num =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1557
  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
  1558
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1559
lemma num_of_bintr':
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1560
  "bintrunc (LENGTH('a::len)) (numeral a) = (numeral b) \<Longrightarrow>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1561
    numeral a = (numeral b :: 'a word)"
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1562
  unfolding bintr_num by (erule subst, simp)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1563
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1564
lemma num_of_sbintr':
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1565
  "sbintrunc (LENGTH('a::len) - 1) (numeral a) = (numeral b) \<Longrightarrow>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1566
    numeral a = (numeral b :: 'a word)"
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1567
  unfolding sbintr_num by (erule subst, simp)
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1568
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1569
lemma num_abs_bintr:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1570
  "(numeral x :: 'a word) =
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1571
    word_of_int (bintrunc (LENGTH('a::len)) (numeral x))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1572
  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
  1573
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1574
lemma num_abs_sbintr:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1575
  "(numeral x :: 'a word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1576
    word_of_int (sbintrunc (LENGTH('a::len) - 1) (numeral x))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1577
  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
  1578
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1579
text \<open>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1580
  \<open>cast\<close> -- note, no arg for new length, as it's determined by type of result,
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1581
  thus in \<open>cast w = w\<close>, the type means cast to length of \<open>w\<close>!
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1582
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1583
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1584
lemma bit_ucast_iff:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1585
  \<open>Parity.bit (ucast a :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a::len) \<and> Parity.bit a n\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1586
  by (simp add: ucast_def, transfer) (auto simp add: bit_take_bit_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1587
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1588
lemma ucast_id: "ucast w = w"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1589
  by (auto simp: ucast_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1590
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1591
lemma scast_id: "scast w = w"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1592
  by (auto simp: scast_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1593
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1594
lemma ucast_bl: "ucast w = of_bl (to_bl w)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1595
  by (auto simp: ucast_def of_bl_def uint_bl word_size)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1596
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1597
lemma nth_ucast: "(ucast w::'a::len word) !! n = (w !! n \<and> n < LENGTH('a))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1598
  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
  1599
    (fast elim!: bin_nth_uint_imp)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1600
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1601
context
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1602
  includes lifting_syntax
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1603
begin
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1604
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1605
lemma transfer_rule_mask_word [transfer_rule]:
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1606
  \<open>((=) ===> pcr_word) Bit_Operations.mask Bit_Operations.mask\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1607
  by (simp only: mask_eq_exp_minus_1 [abs_def]) transfer_prover
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1608
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1609
end
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1610
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1611
lemma ucast_mask_eq:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1612
  \<open>ucast (Bit_Operations.mask n :: 'b word) = Bit_Operations.mask (min LENGTH('b::len) n)\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1613
  by (simp add: bit_eq_iff) (auto simp add: bit_mask_iff bit_ucast_iff exp_eq_zero_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1614
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1615
\<comment> \<open>literal u(s)cast\<close>
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  1616
lemma ucast_bintr [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1617
  "ucast (numeral w :: 'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1618
    word_of_int (bintrunc (LENGTH('a)) (numeral w))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1619
  by (simp add: ucast_def)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1620
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1621
(* TODO: neg_numeral *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1622
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  1623
lemma scast_sbintr [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1624
  "scast (numeral w ::'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1625
    word_of_int (sbintrunc (LENGTH('a) - Suc 0) (numeral w))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1626
  by (simp add: scast_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1627
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1628
lemma source_size: "source_size (c::'a::len word \<Rightarrow> _) = LENGTH('a)"
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1629
  unfolding source_size_def word_size Let_def ..
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1630
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1631
lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len word) = LENGTH('b)"
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1632
  unfolding target_size_def word_size Let_def ..
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  1633
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1634
lemma is_down: "is_down c \<longleftrightarrow> LENGTH('b) \<le> LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1635
  for c :: "'a::len word \<Rightarrow> 'b::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1636
  by (simp only: is_down_def source_size target_size)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1637
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1638
lemma is_up: "is_up c \<longleftrightarrow> LENGTH('a) \<le> LENGTH('b)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1639
  for c :: "'a::len word \<Rightarrow> 'b::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1640
  by (simp only: is_up_def source_size target_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1641
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1642
lemmas is_up_down = trans [OF is_up is_down [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1643
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1644
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
  1645
  apply (unfold is_down)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1646
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1647
  apply (rule ext)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1648
  apply (unfold ucast_def scast_def uint_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1649
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1650
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1651
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1652
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1653
lemma word_rev_tf:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1654
  "to_bl (of_bl bl::'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1655
    rev (takefill False (LENGTH('a)) (rev bl))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1656
  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
  1657
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1658
lemma word_rep_drop:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1659
  "to_bl (of_bl bl::'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1660
    replicate (LENGTH('a) - length bl) False @
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1661
    drop (length bl - LENGTH('a)) bl"
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1662
  by (simp add: word_rev_tf takefill_alt rev_take)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1663
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1664
lemma to_bl_ucast:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1665
  "to_bl (ucast (w::'b::len word) ::'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1666
    replicate (LENGTH('a) - LENGTH('b)) False @
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1667
    drop (LENGTH('b) - LENGTH('a)) (to_bl w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1668
  apply (unfold ucast_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1669
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1670
   apply (rule word_rep_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1671
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1672
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1673
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1674
lemma ucast_up_app [OF refl]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1675
  "uc = ucast \<Longrightarrow> source_size uc + n = target_size uc \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1676
    to_bl (uc w) = replicate n False @ (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1677
  by (auto simp add : source_size target_size to_bl_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1678
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1679
lemma ucast_down_drop [OF refl]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1680
  "uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1681
    to_bl (uc w) = drop n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1682
  by (auto simp add : source_size target_size to_bl_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1683
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1684
lemma scast_down_drop [OF refl]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1685
  "sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1686
    to_bl (sc w) = drop n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1687
  apply (subgoal_tac "sc = ucast")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1688
   apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1689
   apply simp
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1690
   apply (erule ucast_down_drop)
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1691
  apply (rule down_cast_same [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1692
  apply (simp add : source_size target_size is_down)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1693
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1694
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1695
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
  1696
  apply (unfold is_up)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1697
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1698
  apply (simp add: scast_def word_sbin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1699
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1700
    prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1701
    apply (rule word_sbin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1702
   apply (rule sbintrunc_sbintrunc_l)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1703
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1704
   apply (subst word_sbin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1705
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1706
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1707
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1708
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1709
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
  1710
  apply (unfold is_up)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1711
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1712
  apply (rule bin_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1713
  apply (fold word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1714
  apply (auto simp add: nth_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1715
  apply (auto simp add: test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1716
  done
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1717
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1718
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
  1719
  apply (simp (no_asm) add: ucast_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1720
  apply (clarsimp simp add: uint_up_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1721
  done
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1722
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1723
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
  1724
  apply (simp (no_asm) add: scast_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1725
  apply (clarsimp simp add: sint_up_scast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1726
  done
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1727
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1728
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
  1729
  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
  1730
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1731
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1732
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1733
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1734
lemmas isduu = is_up_down [where c = "ucast", THEN iffD2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1735
lemmas isdus = is_up_down [where c = "scast", THEN iffD2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1736
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1737
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1738
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1739
lemma up_ucast_surj:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1740
  "is_up (ucast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1741
    surj (ucast :: 'a word \<Rightarrow> 'b word)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1742
  by (rule surjI) (erule ucast_up_ucast_id)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1743
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1744
lemma up_scast_surj:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1745
  "is_up (scast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow>
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1746
    surj (scast :: 'a word \<Rightarrow> 'b word)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1747
  by (rule surjI) (erule scast_up_scast_id)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1748
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1749
lemma down_scast_inj:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1750
  "is_down (scast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow>
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1751
    inj_on (ucast :: 'a word \<Rightarrow> 'b word) A"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1752
  by (rule inj_on_inverseI, erule scast_down_scast_id)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1753
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1754
lemma down_ucast_inj:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1755
  "is_down (ucast :: 'b::len word \<Rightarrow> 'a::len word) \<Longrightarrow>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1756
    inj_on (ucast :: 'a word \<Rightarrow> 'b word) A"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1757
  by (rule inj_on_inverseI) (erule ucast_down_ucast_id)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1758
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1759
lemma of_bl_append_same: "of_bl (X @ to_bl w) = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1760
  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
  1761
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1762
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
  1763
  apply (unfold is_down)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1764
  apply (clarsimp simp add: ucast_def word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1765
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1766
  apply (erule bintrunc_bintrunc_ge)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1767
  done
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  1768
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1769
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
  1770
  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
  1771
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1772
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
  1773
  unfolding of_bl_def by clarify (erule ucast_down_wi)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1774
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1775
lemmas slice_def' = slice_def [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1776
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1777
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1778
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
  1779
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1780
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1781
subsection \<open>Word Arithmetic\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1782
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1783
lemma word_less_alt: "a < b \<longleftrightarrow> uint a < uint b"
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1784
  by (fact word_less_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1785
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1786
lemma signed_linorder: "class.linorder word_sle word_sless"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1787
  by standard (auto simp: word_sle_def word_sless_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1788
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1789
interpretation signed: linorder "word_sle" "word_sless"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1790
  by (rule signed_linorder)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1791
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1792
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
  1793
  by (auto simp: udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1794
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1795
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
  1796
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
  1797
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
  1798
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
  1799
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
  1800
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
  1801
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1802
lemma word_m1_wi: "- 1 = word_of_int (- 1)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1803
  by (simp add: word_neg_numeral_alt [of Num.One])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1804
46648
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1805
lemma word_0_bl [simp]: "of_bl [] = 0"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1806
  by (simp add: of_bl_def)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1807
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1808
lemma word_1_bl: "of_bl [True] = 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1809
  by (simp add: of_bl_def bl_to_bin_def)
46648
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1810
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1811
lemma uint_eq_0 [simp]: "uint 0 = 0"
689ebcbd6343 avoid using Int.Pls_def in proofs
huffman
parents: 46647
diff changeset
  1812
  unfolding word_0_wi word_ubin.eq_norm by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1813
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  1814
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
  1815
  by (simp add: of_bl_def bl_to_bin_rep_False)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1816
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1817
lemma to_bl_0 [simp]: "to_bl (0::'a::len word) = replicate (LENGTH('a)) False"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1818
  by (simp add: uint_bl word_size bin_to_bl_zero)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1819
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1820
lemma uint_0_iff: "uint x = 0 \<longleftrightarrow> x = 0"
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1821
  by (simp add: word_uint_eq_iff)
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1822
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1823
lemma unat_0_iff: "unat x = 0 \<longleftrightarrow> x = 0"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1824
  by (auto simp: unat_def nat_eq_iff uint_0_iff)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1825
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1826
lemma unat_0 [simp]: "unat 0 = 0"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1827
  by (auto simp: unat_def)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1828
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1829
lemma size_0_same': "size w = 0 \<Longrightarrow> w = v"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1830
  for v w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1831
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1832
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1833
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1834
    apply (rule word_uint.Rep_inverse)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1835
  apply (rule word_ubin.norm_eq_iff [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1836
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1837
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1838
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  1839
lemmas size_0_same = size_0_same' [unfolded word_size]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1840
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1841
lemmas unat_eq_0 = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1842
lemmas unat_eq_zero = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1843
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1844
lemma unat_gt_0: "0 < unat x \<longleftrightarrow> x \<noteq> 0"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1845
  by (auto simp: unat_0_iff [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1846
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1847
lemma ucast_0 [simp]: "ucast 0 = 0"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1848
  by (simp add: ucast_def)
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1849
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1850
lemma sint_0 [simp]: "sint 0 = 0"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1851
  by (simp add: sint_uint)
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1852
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1853
lemma scast_0 [simp]: "scast 0 = 0"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1854
  by (simp add: scast_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1855
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58061
diff changeset
  1856
lemma sint_n1 [simp] : "sint (- 1) = - 1"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1857
  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
  1858
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  1859
lemma scast_n1 [simp]: "scast (- 1) = - 1"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1860
  by (simp add: scast_def)
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1861
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1862
lemma uint_1 [simp]: "uint (1::'a::len word) = 1"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
  1863
  by (simp only: word_1_wi word_ubin.eq_norm) simp
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1864
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1865
lemma unat_1 [simp]: "unat (1::'a::len word) = 1"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1866
  by (simp add: unat_def)
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1867
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1868
lemma ucast_1 [simp]: "ucast (1::'a::len word) = 1"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1869
  by (simp add: ucast_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1870
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1871
\<comment> \<open>now, to get the weaker results analogous to \<open>word_div\<close>/\<open>mod_def\<close>\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1872
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  1873
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1874
subsection \<open>Transferring goals from words to ints\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1875
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1876
lemma word_ths:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1877
  shows word_succ_p1: "word_succ a = a + 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1878
    and word_pred_m1: "word_pred a = a - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1879
    and word_pred_succ: "word_pred (word_succ a) = a"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1880
    and word_succ_pred: "word_succ (word_pred a) = a"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1881
    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
  1882
  by (transfer, simp add: algebra_simps)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1883
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  1884
lemma uint_cong: "x = y \<Longrightarrow> uint x = uint y"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  1885
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1886
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1887
lemma uint_word_ariths:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1888
  fixes a b :: "'a::len word"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1889
  shows "uint (a + b) = (uint a + uint b) mod 2 ^ LENGTH('a::len)"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1890
    and "uint (a - b) = (uint a - uint b) mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1891
    and "uint (a * b) = uint a * uint b mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1892
    and "uint (- a) = - uint a mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1893
    and "uint (word_succ a) = (uint a + 1) mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1894
    and "uint (word_pred a) = (uint a - 1) mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1895
    and "uint (0 :: 'a word) = 0 mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1896
    and "uint (1 :: 'a word) = 1 mod 2 ^ LENGTH('a)"
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1897
  by (simp_all add: word_arith_wis [THEN trans [OF uint_cong int_word_uint]])
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1898
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1899
lemma uint_word_arith_bintrs:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1900
  fixes a b :: "'a::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1901
  shows "uint (a + b) = bintrunc (LENGTH('a)) (uint a + uint b)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1902
    and "uint (a - b) = bintrunc (LENGTH('a)) (uint a - uint b)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1903
    and "uint (a * b) = bintrunc (LENGTH('a)) (uint a * uint b)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1904
    and "uint (- a) = bintrunc (LENGTH('a)) (- uint a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1905
    and "uint (word_succ a) = bintrunc (LENGTH('a)) (uint a + 1)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1906
    and "uint (word_pred a) = bintrunc (LENGTH('a)) (uint a - 1)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1907
    and "uint (0 :: 'a word) = bintrunc (LENGTH('a)) 0"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1908
    and "uint (1 :: 'a word) = bintrunc (LENGTH('a)) 1"
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1909
  by (simp_all add: uint_word_ariths bintrunc_mod2p)
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1910
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1911
lemma sint_word_ariths:
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  1912
  fixes a b :: "'a::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1913
  shows "sint (a + b) = sbintrunc (LENGTH('a) - 1) (sint a + sint b)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1914
    and "sint (a - b) = sbintrunc (LENGTH('a) - 1) (sint a - sint b)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1915
    and "sint (a * b) = sbintrunc (LENGTH('a) - 1) (sint a * sint b)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1916
    and "sint (- a) = sbintrunc (LENGTH('a) - 1) (- sint a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1917
    and "sint (word_succ a) = sbintrunc (LENGTH('a) - 1) (sint a + 1)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1918
    and "sint (word_pred a) = sbintrunc (LENGTH('a) - 1) (sint a - 1)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1919
    and "sint (0 :: 'a word) = sbintrunc (LENGTH('a) - 1) 0"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1920
    and "sint (1 :: 'a word) = sbintrunc (LENGTH('a) - 1) 1"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  1921
         apply (simp_all only: word_sbin.inverse_norm [symmetric])
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  1922
         apply (simp_all add: wi_hom_syms)
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  1923
   apply transfer apply simp
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  1924
  apply transfer apply simp
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  1925
  done
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1926
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1927
lemmas uint_div_alt = word_div_def [THEN trans [OF uint_cong int_word_uint]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1928
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
  1929
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58061
diff changeset
  1930
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
  1931
  unfolding word_pred_m1 by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1932
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1933
lemma succ_pred_no [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1934
    "word_succ (numeral w) = numeral w + 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1935
    "word_pred (numeral w) = numeral w - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1936
    "word_succ (- numeral w) = - numeral w + 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1937
    "word_pred (- numeral w) = - numeral w - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1938
  by (simp_all add: word_succ_p1 word_pred_m1)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1939
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1940
lemma word_sp_01 [simp]:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1941
  "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
  1942
  by (simp_all add: word_succ_p1 word_pred_m1)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1943
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1944
\<comment> \<open>alternative approach to lifting arithmetic equalities\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1945
lemma word_of_int_Ex: "\<exists>y. x = word_of_int y"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1946
  by (rule_tac x="uint x" in exI) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1947
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1948
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1949
subsection \<open>Order on fixed-length words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1950
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1951
lemma word_zero_le [simp]: "0 \<le> y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1952
  for y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1953
  unfolding word_le_def by auto
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1954
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1955
lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1956
  by (simp only: word_le_def word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1957
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1958
lemma word_n1_ge [simp]: "y \<le> -1"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1959
  for y :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1960
  by (fact word_order.extremum)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1961
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1962
lemmas word_not_simps [simp] =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1963
  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
  1964
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1965
lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1966
  for y :: "'a::len word"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1967
  by (simp add: less_le)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1968
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1969
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
  1970
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1971
lemma word_sless_alt: "a <s b \<longleftrightarrow> sint a < sint b"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1972
  by (auto simp add: word_sle_def word_sless_def less_le)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1973
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1974
lemma word_le_nat_alt: "a \<le> b \<longleftrightarrow> unat a \<le> unat b"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1975
  unfolding unat_def word_le_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1976
  by (rule nat_le_eq_zle [symmetric]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1977
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1978
lemma word_less_nat_alt: "a < b \<longleftrightarrow> unat a < unat b"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1979
  unfolding unat_def word_less_alt
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1980
  by (rule nat_less_eq_zless [symmetric]) simp
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1981
70900
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1982
lemmas unat_mono = word_less_nat_alt [THEN iffD1]
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1983
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1984
instance word :: (len) wellorder
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1985
proof
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1986
  fix P :: "'a word \<Rightarrow> bool" and a
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1987
  assume *: "(\<And>b. (\<And>a. a < b \<Longrightarrow> P a) \<Longrightarrow> P b)"
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1988
  have "wf (measure unat)" ..
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1989
  moreover have "{(a, b :: ('a::len) word). a < b} \<subseteq> measure unat"
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1990
    by (auto simp add: word_less_nat_alt)
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1991
  ultimately have "wf {(a, b :: ('a::len) word). a < b}"
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1992
    by (rule wf_subset)
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1993
  then show "P a" using *
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1994
    by induction blast
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1995
qed
954e7f79c25a moved generic instance to distribution
haftmann
parents: 70749
diff changeset
  1996
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1997
lemma wi_less:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  1998
  "(word_of_int n < (word_of_int m :: 'a::len word)) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1999
    (n mod 2 ^ LENGTH('a) < m mod 2 ^ LENGTH('a))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2000
  unfolding word_less_alt by (simp add: word_uint.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2001
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2002
lemma wi_le:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2003
  "(word_of_int n \<le> (word_of_int m :: 'a::len word)) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2004
    (n mod 2 ^ LENGTH('a) \<le> m mod 2 ^ LENGTH('a))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2005
  unfolding word_le_def by (simp add: word_uint.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2006
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2007
lemma udvd_nat_alt: "a udvd b \<longleftrightarrow> (\<exists>n\<ge>0. unat b = n * unat a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2008
  apply (unfold udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2009
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2010
   apply (simp add: unat_def nat_mult_distrib)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2011
  apply (simp add: uint_nat)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2012
  apply (rule exI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2013
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2014
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2015
   apply (erule notE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2016
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2017
  apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2018
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2019
61941
31f2105521ee discontinued ASCII replacement syntax <->;
wenzelm
parents: 61824
diff changeset
  2020
lemma udvd_iff_dvd: "x udvd y \<longleftrightarrow> unat x dvd unat y"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2021
  unfolding dvd_def udvd_nat_alt by force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2022
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2023
lemma unat_minus_one:
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2024
  assumes "w \<noteq> 0"
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2025
  shows "unat (w - 1) = unat w - 1"
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2026
proof -
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2027
  have "0 \<le> uint w" by (fact uint_nonnegative)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2028
  moreover from assms have "0 \<noteq> uint w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2029
    by (simp add: uint_0_iff)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2030
  ultimately have "1 \<le> uint w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2031
    by arith
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2032
  from uint_lt2p [of w] have "uint w - 1 < 2 ^ LENGTH('a)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2033
    by arith
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2034
  with \<open>1 \<le> uint w\<close> have "(uint w - 1) mod 2 ^ LENGTH('a) = uint w - 1"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2035
    by (auto intro: mod_pos_pos_trivial)
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2036
  with \<open>1 \<le> uint w\<close> have "nat ((uint w - 1) mod 2 ^ LENGTH('a)) = nat (uint w) - 1"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2037
    by auto
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2038
  then show ?thesis
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  2039
    by (simp only: unat_def int_word_uint word_arith_wis mod_diff_right_eq)
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2040
qed
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2041
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2042
lemma measure_unat: "p \<noteq> 0 \<Longrightarrow> unat (p - 1) < unat p"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2043
  by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2044
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2045
lemmas uint_add_ge0 [simp] = add_nonneg_nonneg [OF uint_ge_0 uint_ge_0]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2046
lemmas uint_mult_ge0 [simp] = mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2047
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2048
lemma uint_sub_lt2p [simp]: "uint x - uint y < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2049
  for x :: "'a::len word" and y :: "'b::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2050
  using uint_ge_0 [of y] uint_lt2p [of x] by arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2051
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2052
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2053
subsection \<open>Conditions for the addition (etc) of two words to overflow\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2054
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2055
lemma uint_add_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2056
  "(uint x + uint y < 2 ^ LENGTH('a)) =
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2057
    (uint (x + y) = uint x + uint y)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2058
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2059
  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2060
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2061
lemma uint_mult_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2062
  "(uint x * uint y < 2 ^ LENGTH('a)) =
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2063
    (uint (x * y) = uint x * uint y)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2064
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2065
  by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2066
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2067
lemma uint_sub_lem: "uint x \<ge> uint y \<longleftrightarrow> uint (x - y) = uint x - uint y"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2068
  by (auto simp: uint_word_ariths intro!: trans [OF _ int_mod_lem])
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2069
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2070
lemma uint_add_le: "uint (x + y) \<le> uint x + uint y"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2071
  unfolding uint_word_ariths by (metis uint_add_ge0 zmod_le_nonneg_dividend)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2072
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2073
lemma uint_sub_ge: "uint (x - y) \<ge> uint x - uint y"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2074
  unfolding uint_word_ariths by (metis int_mod_ge uint_sub_lt2p zless2p)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2075
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2076
lemma mod_add_if_z:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2077
  "x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2078
    (x + y) mod z = (if x + y < z then x + y else x + y - z)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2079
  for x y z :: int
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2080
  by (auto intro: int_mod_eq)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2081
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2082
lemma uint_plus_if':
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2083
  "uint (a + b) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2084
    (if uint a + uint b < 2 ^ LENGTH('a) then uint a + uint b
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2085
     else uint a + uint b - 2 ^ LENGTH('a))"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2086
  for a b :: "'a::len word"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2087
  using mod_add_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2088
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2089
lemma mod_sub_if_z:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2090
  "x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2091
    (x - y) mod z = (if y \<le> x then x - y else x - y + z)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2092
  for x y z :: int
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2093
  by (auto intro: int_mod_eq)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2094
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2095
lemma uint_sub_if':
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2096
  "uint (a - b) =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2097
    (if uint b \<le> uint a then uint a - uint b
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2098
     else uint a - uint b + 2 ^ LENGTH('a))"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2099
  for a b :: "'a::len word"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2100
  using mod_sub_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2101
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2102
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2103
subsection \<open>Definition of \<open>uint_arith\<close>\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2104
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2105
lemma word_of_int_inverse:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2106
  "word_of_int r = a \<Longrightarrow> 0 \<le> r \<Longrightarrow> r < 2 ^ LENGTH('a) \<Longrightarrow> uint a = r"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2107
  for a :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2108
  apply (erule word_uint.Abs_inverse' [rotated])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2109
  apply (simp add: uints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2110
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2111
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2112
lemma uint_split:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2113
  "P (uint x) = (\<forall>i. word_of_int i = x \<and> 0 \<le> i \<and> i < 2^LENGTH('a) \<longrightarrow> P i)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2114
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2115
  apply (fold word_int_case_def)
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
  2116
  apply (auto dest!: word_of_int_inverse simp: int_word_uint
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2117
      split: word_int_split)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2118
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2119
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2120
lemma uint_split_asm:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2121
  "P (uint x) = (\<nexists>i. word_of_int i = x \<and> 0 \<le> i \<and> i < 2^LENGTH('a) \<and> \<not> P i)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2122
  for x :: "'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2123
  by (auto dest!: word_of_int_inverse
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
  2124
      simp: int_word_uint
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2125
      split: uint_split)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2126
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2127
lemmas uint_splits = uint_split uint_split_asm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2128
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2129
lemmas uint_arith_simps =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2130
  word_le_def word_less_alt
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2131
  word_uint.Rep_inject [symmetric]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2132
  uint_sub_if' uint_plus_if'
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2133
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2134
\<comment> \<open>use this to stop, eg. \<open>2 ^ LENGTH(32)\<close> being simplified\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2135
lemma power_False_cong: "False \<Longrightarrow> a ^ b = c ^ d"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2136
  by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2137
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2138
\<comment> \<open>\<open>uint_arith_tac\<close>: reduce to arithmetic on int, try to solve by arith\<close>
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2139
ML \<open>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2140
fun uint_arith_simpset ctxt =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2141
  ctxt addsimps @{thms uint_arith_simps}
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2142
     delsimps @{thms word_uint.Rep_inject}
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  2143
     |> fold Splitter.add_split @{thms if_split_asm}
45620
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45604
diff changeset
  2144
     |> fold Simplifier.add_cong @{thms power_False_cong}
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2145
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2146
fun uint_arith_tacs ctxt =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2147
  let
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2148
    fun arith_tac' n t =
59657
2441a80fb6c1 eliminated unused arith "verbose" flag -- tools that need options can use the context;
wenzelm
parents: 59498
diff changeset
  2149
      Arith_Data.arith_tac ctxt n t
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2150
        handle Cooper.COOPER _ => Seq.empty;
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2151
  in
42793
88bee9f6eec7 proper Proof.context for classical tactics;
wenzelm
parents: 41550
diff changeset
  2152
    [ clarify_tac ctxt 1,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2153
      full_simp_tac (uint_arith_simpset ctxt) 1,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2154
      ALLGOALS (full_simp_tac
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2155
        (put_simpset HOL_ss ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2156
          |> fold Splitter.add_split @{thms uint_splits}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2157
          |> fold Simplifier.add_cong @{thms power_False_cong})),
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2158
      rewrite_goals_tac ctxt @{thms word_size},
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59487
diff changeset
  2159
      ALLGOALS  (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60429
diff changeset
  2160
                         REPEAT (eresolve_tac ctxt [conjE] n) THEN
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2161
                         REPEAT (dresolve_tac ctxt @{thms word_of_int_inverse} n
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2162
                                 THEN assume_tac ctxt n
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58874
diff changeset
  2163
                                 THEN assume_tac ctxt n)),
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2164
      TRYALL arith_tac' ]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2165
  end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2166
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2167
fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt))
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2168
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2169
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2170
method_setup uint_arith =
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2171
  \<open>Scan.succeed (SIMPLE_METHOD' o uint_arith_tac)\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2172
  "solving word arithmetic via integers and arith"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2173
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2174
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2175
subsection \<open>More on overflows and monotonicity\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2176
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2177
lemma no_plus_overflow_uint_size: "x \<le> x + y \<longleftrightarrow> uint x + uint y < 2 ^ size x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2178
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2179
  unfolding word_size by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2180
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2181
lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2182
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2183
lemma no_ulen_sub: "x \<ge> x - y \<longleftrightarrow> uint y \<le> uint x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2184
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2185
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2186
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2187
lemma no_olen_add': "x \<le> y + x \<longleftrightarrow> uint y + uint x < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2188
  for x y :: "'a::len word"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  2189
  by (simp add: ac_simps no_olen_add)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2190
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2191
lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2192
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2193
lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2194
lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2195
lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2196
lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2197
lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def]
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2198
lemmas word_sub_le = word_sub_le_iff [THEN iffD2]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2199
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2200
lemma word_less_sub1: "x \<noteq> 0 \<Longrightarrow> 1 < x \<longleftrightarrow> 0 < x - 1"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2201
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2202
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2203
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2204
lemma word_le_sub1: "x \<noteq> 0 \<Longrightarrow> 1 \<le> x \<longleftrightarrow> 0 \<le> x - 1"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2205
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2206
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2207
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2208
lemma sub_wrap_lt: "x < x - z \<longleftrightarrow> x < z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2209
  for x z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2210
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2211
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2212
lemma sub_wrap: "x \<le> x - z \<longleftrightarrow> z = 0 \<or> x < z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2213
  for x z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2214
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2215
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2216
lemma plus_minus_not_NULL_ab: "x \<le> ab - c \<Longrightarrow> c \<le> ab \<Longrightarrow> c \<noteq> 0 \<Longrightarrow> x + c \<noteq> 0"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2217
  for x ab c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2218
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2219
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2220
lemma plus_minus_no_overflow_ab: "x \<le> ab - c \<Longrightarrow> c \<le> ab \<Longrightarrow> x \<le> x + c"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2221
  for x ab c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2222
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2223
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2224
lemma le_minus': "a + c \<le> b \<Longrightarrow> a \<le> a + c \<Longrightarrow> c \<le> b - a"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2225
  for a b c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2226
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2227
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2228
lemma le_plus': "a \<le> b \<Longrightarrow> c \<le> b - a \<Longrightarrow> a + c \<le> b"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2229
  for a b c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2230
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2231
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2232
lemmas le_plus = le_plus' [rotated]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2233
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2234
lemmas le_minus = leD [THEN thin_rl, THEN le_minus'] (* FIXME *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2235
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2236
lemma word_plus_mono_right: "y \<le> z \<Longrightarrow> x \<le> x + z \<Longrightarrow> x + y \<le> x + z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2237
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2238
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2239
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2240
lemma word_less_minus_cancel: "y - x < z - x \<Longrightarrow> x \<le> z \<Longrightarrow> y < z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2241
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2242
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2243
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2244
lemma word_less_minus_mono_left: "y < z \<Longrightarrow> x \<le> y \<Longrightarrow> y - x < z - x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2245
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2246
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2247
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2248
lemma word_less_minus_mono: "a < c \<Longrightarrow> d < b \<Longrightarrow> a - b < a \<Longrightarrow> c - d < c \<Longrightarrow> a - b < c - d"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2249
  for a b c d :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2250
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2251
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2252
lemma word_le_minus_cancel: "y - x \<le> z - x \<Longrightarrow> x \<le> z \<Longrightarrow> y \<le> z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2253
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2254
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2255
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2256
lemma word_le_minus_mono_left: "y \<le> z \<Longrightarrow> x \<le> y \<Longrightarrow> y - x \<le> z - x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2257
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2258
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2259
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2260
lemma word_le_minus_mono:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2261
  "a \<le> c \<Longrightarrow> d \<le> b \<Longrightarrow> a - b \<le> a \<Longrightarrow> c - d \<le> c \<Longrightarrow> a - b \<le> c - d"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2262
  for a b c d :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2263
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2264
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2265
lemma plus_le_left_cancel_wrap: "x + y' < x \<Longrightarrow> x + y < x \<Longrightarrow> x + y' < x + y \<longleftrightarrow> y' < y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2266
  for x y y' :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2267
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2268
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2269
lemma plus_le_left_cancel_nowrap: "x \<le> x + y' \<Longrightarrow> x \<le> x + y \<Longrightarrow> x + y' < x + y \<longleftrightarrow> y' < y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2270
  for x y y' :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2271
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2272
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2273
lemma word_plus_mono_right2: "a \<le> a + b \<Longrightarrow> c \<le> b \<Longrightarrow> a \<le> a + c"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2274
  for a b c :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2275
  by uint_arith
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2276
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2277
lemma word_less_add_right: "x < y - z \<Longrightarrow> z \<le> y \<Longrightarrow> x + z < y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2278
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2279
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2280
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2281
lemma word_less_sub_right: "x < y + z \<Longrightarrow> y \<le> x \<Longrightarrow> x - y < z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2282
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2283
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2284
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2285
lemma word_le_plus_either: "x \<le> y \<or> x \<le> z \<Longrightarrow> y \<le> y + z \<Longrightarrow> x \<le> y + z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2286
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2287
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2288
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2289
lemma word_less_nowrapI: "x < z - k \<Longrightarrow> k \<le> z \<Longrightarrow> 0 < k \<Longrightarrow> x < x + k"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2290
  for x z k :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2291
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2292
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2293
lemma inc_le: "i < m \<Longrightarrow> i + 1 \<le> m"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2294
  for i m :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2295
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2296
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2297
lemma inc_i: "1 \<le> i \<Longrightarrow> i < m \<Longrightarrow> 1 \<le> i + 1 \<and> i + 1 \<le> m"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2298
  for i m :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2299
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2300
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2301
lemma udvd_incr_lem:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2302
  "up < uq \<Longrightarrow> up = ua + n * uint K \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2303
    uq = ua + n' * uint K \<Longrightarrow> up + uint K \<le> uq"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2304
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2305
  apply (drule less_le_mult)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2306
   apply safe
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2307
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2308
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2309
lemma udvd_incr':
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2310
  "p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2311
    uint q = ua + n' * uint K \<Longrightarrow> p + K \<le> q"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2312
  apply (unfold word_less_alt word_le_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2313
  apply (drule (2) udvd_incr_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2314
  apply (erule uint_add_le [THEN order_trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2315
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2316
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2317
lemma udvd_decr':
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2318
  "p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2319
    uint q = ua + n' * uint K \<Longrightarrow> p \<le> q - K"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2320
  apply (unfold word_less_alt word_le_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2321
  apply (drule (2) udvd_incr_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2322
  apply (drule le_diff_eq [THEN iffD2])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2323
  apply (erule order_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2324
  apply (rule uint_sub_ge)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2325
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2326
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2327
lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, unfolded add_0_left]
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2328
lemmas udvd_incr0 = udvd_incr' [where ua=0, unfolded add_0_left]
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2329
lemmas udvd_decr0 = udvd_decr' [where ua=0, unfolded add_0_left]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2330
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2331
lemma udvd_minus_le': "xy < k \<Longrightarrow> z udvd xy \<Longrightarrow> z udvd k \<Longrightarrow> xy \<le> k - z"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2332
  apply (unfold udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2333
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2334
  apply (erule (2) udvd_decr0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2335
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2336
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2337
lemma udvd_incr2_K:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2338
  "p < a + s \<Longrightarrow> a \<le> a + s \<Longrightarrow> K udvd s \<Longrightarrow> K udvd p - a \<Longrightarrow> a \<le> p \<Longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2339
    0 < K \<Longrightarrow> p \<le> p + K \<and> p + K \<le> a + s"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2340
  supply [[simproc del: linordered_ring_less_cancel_factor]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2341
  apply (unfold udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2342
  apply clarify
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  2343
  apply (simp add: uint_arith_simps split: if_split_asm)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2344
   prefer 2
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2345
   apply (insert uint_range' [of s])[1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2346
   apply arith
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  2347
  apply (drule add.commute [THEN xtr1])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2348
  apply (simp add: diff_less_eq [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2349
  apply (drule less_le_mult)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2350
   apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2351
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2352
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2353
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2354
\<comment> \<open>links with \<open>rbl\<close> operations\<close>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2355
lemma word_succ_rbl: "to_bl w = bl \<Longrightarrow> to_bl (word_succ w) = rev (rbl_succ (rev bl))"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2356
  apply (unfold word_succ_alt)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2357
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2358
  apply (simp add: to_bl_of_bin)
46654
134b74908a8e avoid using Int.succ or Int.pred in proofs
huffman
parents: 46648
diff changeset
  2359
  apply (simp add: to_bl_def rbl_succ)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2360
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2361
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2362
lemma word_pred_rbl: "to_bl w = bl \<Longrightarrow> to_bl (word_pred w) = rev (rbl_pred (rev bl))"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2363
  apply (unfold word_pred_alt)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2364
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2365
  apply (simp add: to_bl_of_bin)
46654
134b74908a8e avoid using Int.succ or Int.pred in proofs
huffman
parents: 46648
diff changeset
  2366
  apply (simp add: to_bl_def rbl_pred)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2367
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2368
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2369
lemma word_add_rbl:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2370
  "to_bl v = vbl \<Longrightarrow> to_bl w = wbl \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2371
    to_bl (v + w) = rev (rbl_add (rev vbl) (rev wbl))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2372
  apply (unfold word_add_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2373
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2374
  apply (simp add: to_bl_of_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2375
  apply (simp add: to_bl_def rbl_add)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2376
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2377
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2378
lemma word_mult_rbl:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2379
  "to_bl v = vbl \<Longrightarrow> to_bl w = wbl \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2380
    to_bl (v * w) = rev (rbl_mult (rev vbl) (rev wbl))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2381
  apply (unfold word_mult_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2382
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2383
  apply (simp add: to_bl_of_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2384
  apply (simp add: to_bl_def rbl_mult)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2385
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2386
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2387
lemma rtb_rbl_ariths:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2388
  "rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_succ w)) = rbl_succ ys"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2389
  "rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_pred w)) = rbl_pred ys"
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  2390
  "rev (to_bl v) = ys \<Longrightarrow> rev (to_bl w) = xs \<Longrightarrow> rev (to_bl (v * w)) = rbl_mult ys xs"
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  2391
  "rev (to_bl v) = ys \<Longrightarrow> rev (to_bl w) = xs \<Longrightarrow> rev (to_bl (v + w)) = rbl_add ys xs"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2392
  by (auto simp: rev_swap [symmetric] word_succ_rbl word_pred_rbl word_mult_rbl word_add_rbl)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2393
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2394
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2395
subsection \<open>Arithmetic type class instantiations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2396
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2397
lemmas word_le_0_iff [simp] =
70749
5d06b7bb9d22 More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
paulson <lp15@cam.ac.uk>
parents: 70342
diff changeset
  2398
  word_zero_le [THEN leD, THEN antisym_conv1]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2399
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2400
lemma word_of_int_nat: "0 \<le> x \<Longrightarrow> word_of_int x = of_nat (nat x)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2401
  by (simp add: word_of_int)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2402
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2403
text \<open>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2404
  note that \<open>iszero_def\<close> is only for class \<open>comm_semiring_1_cancel\<close>,
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2405
  which requires word length \<open>\<ge> 1\<close>, ie \<open>'a::len word\<close>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2406
\<close>
46603
83a5160e6b4d removed unnecessary lemma zero_bintrunc
huffman
parents: 46602
diff changeset
  2407
lemma iszero_word_no [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2408
  "iszero (numeral bin :: 'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2409
    iszero (bintrunc (LENGTH('a)) (numeral bin))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2410
  using word_ubin.norm_eq_iff [where 'a='a, of "numeral bin" 0]
46603
83a5160e6b4d removed unnecessary lemma zero_bintrunc
huffman
parents: 46602
diff changeset
  2411
  by (simp add: iszero_def [symmetric])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2412
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2413
text \<open>Use \<open>iszero\<close> to simplify equalities between word numerals.\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2414
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2415
lemmas word_eq_numeral_iff_iszero [simp] =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2416
  eq_numeral_iff_iszero [where 'a="'a::len word"]
46603
83a5160e6b4d removed unnecessary lemma zero_bintrunc
huffman
parents: 46602
diff changeset
  2417
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2418
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2419
subsection \<open>Word and nat\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2420
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  2421
lemma td_ext_unat [OF refl]:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2422
  "n = LENGTH('a::len) \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2423
    td_ext (unat :: 'a word \<Rightarrow> nat) of_nat (unats n) (\<lambda>i. i mod 2 ^ n)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2424
  apply (unfold td_ext_def' unat_def word_of_nat unats_uints)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2425
  apply (auto intro!: imageI simp add : word_of_int_hom_syms)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2426
   apply (erule word_uint.Abs_inverse [THEN arg_cong])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2427
  apply (simp add: int_word_uint nat_mod_distrib nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2428
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2429
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2430
lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2431
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2432
interpretation word_unat:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2433
  td_ext
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2434
    "unat::'a::len word \<Rightarrow> nat"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2435
    of_nat
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2436
    "unats (LENGTH('a::len))"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2437
    "\<lambda>i. i mod 2 ^ LENGTH('a::len)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2438
  by (rule td_ext_unat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2439
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2440
lemmas td_unat = word_unat.td_thm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2441
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2442
lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2443
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2444
lemma unat_le: "y \<le> unat z \<Longrightarrow> y \<in> unats (LENGTH('a))"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2445
  for z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2446
  apply (unfold unats_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2447
  apply clarsimp
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2448
  apply (rule xtrans, rule unat_lt2p, assumption)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2449
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2450
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2451
lemma word_nchotomy: "\<forall>w :: 'a::len word. \<exists>n. w = of_nat n \<and> n < 2 ^ LENGTH('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2452
  apply (rule allI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2453
  apply (rule word_unat.Abs_cases)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2454
  apply (unfold unats_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2455
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2456
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2457
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2458
lemma of_nat_eq: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ LENGTH('a))"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2459
  for w :: "'a::len word"
68157
057d5b4ce47e removed some non-essential rules
haftmann
parents: 67443
diff changeset
  2460
  using mod_div_mult_eq [of n "2 ^ LENGTH('a)", symmetric]
057d5b4ce47e removed some non-essential rules
haftmann
parents: 67443
diff changeset
  2461
  by (auto simp add: word_unat.inverse_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2462
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2463
lemma of_nat_eq_size: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ size w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2464
  unfolding word_size by (rule of_nat_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2465
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2466
lemma of_nat_0: "of_nat m = (0::'a::len word) \<longleftrightarrow> (\<exists>q. m = q * 2 ^ LENGTH('a))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2467
  by (simp add: of_nat_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2468
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2469
lemma of_nat_2p [simp]: "of_nat (2 ^ LENGTH('a)) = (0::'a::len word)"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2470
  by (fact mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2471
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2472
lemma of_nat_gt_0: "of_nat k \<noteq> 0 \<Longrightarrow> 0 < k"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2473
  by (cases k) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2474
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2475
lemma of_nat_neq_0: "0 < k \<Longrightarrow> k < 2 ^ LENGTH('a::len) \<Longrightarrow> of_nat k \<noteq> (0 :: 'a word)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2476
  by (auto simp add : of_nat_0)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2477
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2478
lemma Abs_fnat_hom_add: "of_nat a + of_nat b = of_nat (a + b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2479
  by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2480
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2481
lemma Abs_fnat_hom_mult: "of_nat a * of_nat b = (of_nat (a * b) :: 'a::len word)"
61649
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61424
diff changeset
  2482
  by (simp add: word_of_nat wi_hom_mult)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2483
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2484
lemma Abs_fnat_hom_Suc: "word_succ (of_nat a) = of_nat (Suc a)"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  2485
  by (simp add: word_of_nat wi_hom_succ ac_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2486
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2487
lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0"
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  2488
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2489
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2490
lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)"
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  2491
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2492
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2493
lemmas Abs_fnat_homs =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2494
  Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2495
  Abs_fnat_hom_0 Abs_fnat_hom_1
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2496
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2497
lemma word_arith_nat_add: "a + b = of_nat (unat a + unat b)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2498
  by simp
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2499
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2500
lemma word_arith_nat_mult: "a * b = of_nat (unat a * unat b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2501
  by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2502
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2503
lemma word_arith_nat_Suc: "word_succ a = of_nat (Suc (unat a))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2504
  by (subst Abs_fnat_hom_Suc [symmetric]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2505
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2506
lemma word_arith_nat_div: "a div b = of_nat (unat a div unat b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2507
  by (simp add: word_div_def word_of_nat zdiv_int uint_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2508
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2509
lemma word_arith_nat_mod: "a mod b = of_nat (unat a mod unat b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2510
  by (simp add: word_mod_def word_of_nat zmod_int uint_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2511
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2512
lemmas word_arith_nat_defs =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2513
  word_arith_nat_add word_arith_nat_mult
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2514
  word_arith_nat_Suc Abs_fnat_hom_0
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2515
  Abs_fnat_hom_1 word_arith_nat_div
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2516
  word_arith_nat_mod
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2517
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2518
lemma unat_cong: "x = y \<Longrightarrow> unat x = unat y"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2519
  by simp
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2520
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2521
lemmas unat_word_ariths = word_arith_nat_defs
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2522
  [THEN trans [OF unat_cong unat_of_nat]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2523
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2524
lemmas word_sub_less_iff = word_sub_le_iff
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2525
  [unfolded linorder_not_less [symmetric] Not_eq_iff]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2526
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2527
lemma unat_add_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2528
  "unat x + unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x + y) = unat x + unat y"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2529
  for x y :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2530
  by (auto simp: unat_word_ariths intro!: trans [OF _ nat_mod_lem])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2531
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2532
lemma unat_mult_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2533
  "unat x * unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x * y) = unat x * unat y"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  2534
  for x y :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2535
  by (auto simp: unat_word_ariths intro!: trans [OF _ nat_mod_lem])
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2536
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2537
lemmas unat_plus_if' =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2538
  trans [OF unat_word_ariths(1) mod_nat_add, simplified]
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2539
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2540
lemma le_no_overflow: "x \<le> b \<Longrightarrow> a \<le> a + b \<Longrightarrow> x \<le> a + b"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2541
  for a b x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2542
  apply (erule order_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2543
  apply (erule olen_add_eqv [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2544
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2545
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2546
lemmas un_ui_le =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2547
  trans [OF word_le_nat_alt [symmetric] word_le_def]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2548
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2549
lemma unat_sub_if_size:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2550
  "unat (x - y) =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2551
    (if unat y \<le> unat x
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2552
     then unat x - unat y
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2553
     else unat x + 2 ^ size x - unat y)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2554
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2555
  apply (simp add: un_ui_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2556
  apply (auto simp add: unat_def uint_sub_if')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2557
   apply (rule nat_diff_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2558
    prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2559
    apply (simp add: algebra_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2560
    apply (rule nat_diff_distrib [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2561
      prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2562
      apply (subst nat_add_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2563
        prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2564
        apply (simp add: nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2565
       apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2566
  apply uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2567
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2568
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2569
lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2570
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2571
lemma unat_div: "unat (x div y) = unat x div unat y"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2572
  for x y :: " 'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2573
  apply (simp add : unat_word_ariths)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2574
  apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2575
  apply (rule div_le_dividend)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2576
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2577
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2578
lemma unat_mod: "unat (x mod y) = unat x mod unat y"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2579
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2580
  apply (clarsimp simp add : unat_word_ariths)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2581
  apply (cases "unat y")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2582
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2583
   apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq'])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2584
   apply (rule mod_le_divisor)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2585
   apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2586
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2587
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2588
lemma uint_div: "uint (x div y) = uint x div uint y"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2589
  for x y :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2590
  by (simp add: uint_nat unat_div zdiv_int)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2591
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2592
lemma uint_mod: "uint (x mod y) = uint x mod uint y"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2593
  for x y :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2594
  by (simp add: uint_nat unat_mod zmod_int)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2595
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2596
text \<open>Definition of \<open>unat_arith\<close> tactic\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2597
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2598
lemma unat_split: "P (unat x) \<longleftrightarrow> (\<forall>n. of_nat n = x \<and> n < 2^LENGTH('a) \<longrightarrow> P n)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2599
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2600
  by (auto simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2601
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2602
lemma unat_split_asm: "P (unat x) \<longleftrightarrow> (\<nexists>n. of_nat n = x \<and> n < 2^LENGTH('a) \<and> \<not> P n)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2603
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2604
  by (auto simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2605
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2606
lemmas of_nat_inverse =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2607
  word_unat.Abs_inverse' [rotated, unfolded unats_def, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2608
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2609
lemmas unat_splits = unat_split unat_split_asm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2610
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2611
lemmas unat_arith_simps =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2612
  word_le_nat_alt word_less_nat_alt
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2613
  word_unat.Rep_inject [symmetric]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2614
  unat_sub_if' unat_plus_if' unat_div unat_mod
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2615
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2616
\<comment> \<open>\<open>unat_arith_tac\<close>: tactic to reduce word arithmetic to \<open>nat\<close>, try to solve via \<open>arith\<close>\<close>
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2617
ML \<open>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2618
fun unat_arith_simpset ctxt =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2619
  ctxt addsimps @{thms unat_arith_simps}
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2620
     delsimps @{thms word_unat.Rep_inject}
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  2621
     |> fold Splitter.add_split @{thms if_split_asm}
45620
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45604
diff changeset
  2622
     |> fold Simplifier.add_cong @{thms power_False_cong}
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2623
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2624
fun unat_arith_tacs ctxt =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2625
  let
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2626
    fun arith_tac' n t =
59657
2441a80fb6c1 eliminated unused arith "verbose" flag -- tools that need options can use the context;
wenzelm
parents: 59498
diff changeset
  2627
      Arith_Data.arith_tac ctxt n t
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2628
        handle Cooper.COOPER _ => Seq.empty;
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2629
  in
42793
88bee9f6eec7 proper Proof.context for classical tactics;
wenzelm
parents: 41550
diff changeset
  2630
    [ clarify_tac ctxt 1,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2631
      full_simp_tac (unat_arith_simpset ctxt) 1,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2632
      ALLGOALS (full_simp_tac
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2633
        (put_simpset HOL_ss ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2634
          |> fold Splitter.add_split @{thms unat_splits}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2635
          |> fold Simplifier.add_cong @{thms power_False_cong})),
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2636
      rewrite_goals_tac ctxt @{thms word_size},
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60429
diff changeset
  2637
      ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60429
diff changeset
  2638
                         REPEAT (eresolve_tac ctxt [conjE] n) THEN
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60429
diff changeset
  2639
                         REPEAT (dresolve_tac ctxt @{thms of_nat_inverse} n THEN assume_tac ctxt n)),
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2640
      TRYALL arith_tac' ]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2641
  end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2642
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2643
fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt))
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2644
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2645
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2646
method_setup unat_arith =
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2647
  \<open>Scan.succeed (SIMPLE_METHOD' o unat_arith_tac)\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2648
  "solving word arithmetic via natural numbers and arith"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2649
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2650
lemma no_plus_overflow_unat_size: "x \<le> x + y \<longleftrightarrow> unat x + unat y < 2 ^ size x"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2651
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2652
  unfolding word_size by unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2653
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2654
lemmas no_olen_add_nat =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2655
  no_plus_overflow_unat_size [unfolded word_size]
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2656
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2657
lemmas unat_plus_simple =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2658
  trans [OF no_olen_add_nat unat_add_lem]
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2659
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2660
lemma word_div_mult: "0 < y \<Longrightarrow> unat x * unat y < 2 ^ LENGTH('a) \<Longrightarrow> x * y div y = x"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2661
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2662
  apply unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2663
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2664
  apply (subst unat_mult_lem [THEN iffD1])
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2665
   apply auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2666
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2667
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2668
lemma div_lt': "i \<le> k div x \<Longrightarrow> unat i * unat x < 2 ^ LENGTH('a)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2669
  for i k x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2670
  apply unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2671
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2672
  apply (drule mult_le_mono1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2673
  apply (erule order_le_less_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2674
  apply (rule xtr7 [OF unat_lt2p div_mult_le])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2675
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2676
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2677
lemmas div_lt'' = order_less_imp_le [THEN div_lt']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2678
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2679
lemma div_lt_mult: "i < k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x < k"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2680
  for i k x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2681
  apply (frule div_lt'' [THEN unat_mult_lem [THEN iffD1]])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2682
  apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2683
  apply (drule (1) mult_less_mono1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2684
  apply (erule order_less_le_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2685
  apply (rule div_mult_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2686
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2687
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2688
lemma div_le_mult: "i \<le> k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x \<le> k"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2689
  for i k x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2690
  apply (frule div_lt' [THEN unat_mult_lem [THEN iffD1]])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2691
  apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2692
  apply (drule mult_le_mono1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2693
  apply (erule order_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2694
  apply (rule div_mult_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2695
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2696
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2697
lemma div_lt_uint': "i \<le> k div x \<Longrightarrow> uint i * uint x < 2 ^ LENGTH('a)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2698
  for i k x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2699
  apply (unfold uint_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2700
  apply (drule div_lt')
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2701
  apply (metis of_nat_less_iff of_nat_mult of_nat_numeral of_nat_power)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2702
  done
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2703
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2704
lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2705
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2706
lemma word_le_exists': "x \<le> y \<Longrightarrow> \<exists>z. y = x + z \<and> uint x + uint z < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2707
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2708
  apply (rule exI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2709
  apply (rule conjI)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2710
   apply (rule zadd_diff_inverse)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2711
  apply uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2712
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2713
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2714
lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2715
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2716
lemmas plus_minus_no_overflow =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2717
  order_less_imp_le [THEN plus_minus_no_overflow_ab]
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2718
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2719
lemmas mcs = word_less_minus_cancel word_less_minus_mono_left
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2720
  word_le_minus_cancel word_le_minus_mono_left
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2721
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2722
lemmas word_l_diffs = mcs [where y = "w + x", unfolded add_diff_cancel] for w x
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2723
lemmas word_diff_ls = mcs [where z = "w + x", unfolded add_diff_cancel] for w x
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2724
lemmas word_plus_mcs = word_diff_ls [where y = "v + x", unfolded add_diff_cancel] for v x
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2725
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2726
lemmas le_unat_uoi = unat_le [THEN word_unat.Abs_inverse]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2727
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66453
diff changeset
  2728
lemmas thd = times_div_less_eq_dividend
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2729
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2730
lemmas uno_simps [THEN le_unat_uoi] = mod_le_divisor div_le_dividend dtle
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2731
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2732
lemma word_mod_div_equality: "(n div b) * b + (n mod b) = n"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2733
  for n b :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2734
  apply (unfold word_less_nat_alt word_arith_nat_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2735
  apply (cut_tac y="unat b" in gt_or_eq_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2736
  apply (erule disjE)
64242
93c6f0da5c70 more standardized theorem names for facts involving the div and mod identity
haftmann
parents: 63950
diff changeset
  2737
   apply (simp only: div_mult_mod_eq uno_simps Word.word_unat.Rep_inverse)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2738
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2739
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2740
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2741
lemma word_div_mult_le: "a div b * b \<le> a"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2742
  for a b :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2743
  apply (unfold word_le_nat_alt word_arith_nat_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2744
  apply (cut_tac y="unat b" in gt_or_eq_0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2745
  apply (erule disjE)
61649
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61424
diff changeset
  2746
   apply (simp only: div_mult_le uno_simps Word.word_unat.Rep_inverse)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2747
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2748
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2749
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2750
lemma word_mod_less_divisor: "0 < n \<Longrightarrow> m mod n < n"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2751
  for m n :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2752
  apply (simp only: word_less_nat_alt word_arith_nat_defs)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2753
  apply (auto simp: uno_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2754
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2755
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2756
lemma word_of_int_power_hom: "word_of_int a ^ n = (word_of_int (a ^ n) :: 'a::len word)"
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  2757
  by (induct n) (simp_all add: wi_hom_mult [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2758
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2759
lemma word_arith_power_alt: "a ^ n = (word_of_int (uint a ^ n) :: 'a::len word)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2760
  by (simp add : word_of_int_power_hom [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2761
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2762
lemma of_bl_length_less:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2763
  "length x = k \<Longrightarrow> k < LENGTH('a) \<Longrightarrow> (of_bl x :: 'a::len word) < 2 ^ k"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2764
  apply (unfold of_bl_def word_less_alt word_numeral_alt)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2765
  apply safe
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2766
  apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2767
      del: word_of_int_numeral)
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
  2768
  apply simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2769
  apply (subst mod_pos_pos_trivial)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2770
    apply (rule bl_to_bin_ge0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2771
   apply (rule order_less_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2772
    apply (rule bl_to_bin_lt2p)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2773
   apply simp
46646
0abbf6dd09ee remove ill-formed lemma of_bl_no; adapt proofs
huffman
parents: 46645
diff changeset
  2774
  apply (rule bl_to_bin_lt2p)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2775
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2776
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2777
lemma unatSuc: "1 + n \<noteq> 0 \<Longrightarrow> unat (1 + n) = Suc (unat n)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2778
  for n :: "'a::len word"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2779
  by unat_arith
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2780
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2781
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2782
subsection \<open>Cardinality, finiteness of set of words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2783
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2784
lemma inj_on_word_of_int: \<open>inj_on (word_of_int :: int \<Rightarrow> 'a word) {0..<2 ^ LENGTH('a::len)}\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2785
  by (rule inj_onI) (simp add: word.abs_eq_iff take_bit_eq_mod)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2786
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2787
lemma inj_uint: \<open>inj uint\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2788
  by (rule injI) simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2789
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2790
lemma range_uint: \<open>range (uint :: 'a word \<Rightarrow> int) = {0..<2 ^ LENGTH('a::len)}\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2791
  by transfer (auto simp add: bintr_lt2p range_bintrunc)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2792
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2793
lemma UNIV_eq: \<open>(UNIV :: 'a word set) = word_of_int ` {0..<2 ^ LENGTH('a::len)}\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2794
proof -
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2795
  have \<open>uint ` (UNIV :: 'a word set) = uint ` (word_of_int :: int \<Rightarrow> 'a word) ` {0..<2 ^ LENGTH('a::len)}\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2796
    by (simp add: range_uint image_image uint.abs_eq take_bit_eq_mod)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2797
  then show ?thesis
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2798
    using inj_image_eq_iff [of \<open>uint :: 'a word \<Rightarrow> int\<close> \<open>UNIV :: 'a word set\<close> \<open>word_of_int ` {0..<2 ^ LENGTH('a)} :: 'a word set\<close>, OF inj_uint]
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2799
    by simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2800
qed
45809
2bee94cbae72 finite class instance for word type; remove unused lemmas
huffman
parents: 45808
diff changeset
  2801
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2802
lemma card_word: "CARD('a word) = 2 ^ LENGTH('a::len)"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2803
  by (simp add: UNIV_eq card_image inj_on_word_of_int)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2804
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2805
lemma card_word_size: "CARD('a word) = 2 ^ size x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2806
  for x :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2807
  unfolding word_size by (rule card_word)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2808
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2809
instance word :: (len) finite
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2810
  by standard (simp add: UNIV_eq)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2811
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2812
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2813
subsection \<open>Bitwise Operations on Words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2814
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  2815
lemma word_eq_rbl_eq: "x = y \<longleftrightarrow> rev (to_bl x) = rev (to_bl y)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  2816
  by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  2817
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2818
lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2819
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2820
\<comment> \<open>following definitions require both arithmetic and bit-wise word operations\<close>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2821
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2822
\<comment> \<open>to get \<open>word_no_log_defs\<close> from \<open>word_log_defs\<close>, using \<open>bin_log_bintrs\<close>\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2823
lemmas wils1 = bin_log_bintrs [THEN word_ubin.norm_eq_iff [THEN iffD1],
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2824
  folded word_ubin.eq_norm, THEN eq_reflection]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2825
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2826
\<comment> \<open>the binary operations only\<close>  (* BH: why is this needed? *)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2827
lemmas word_log_binary_defs =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2828
  word_and_def word_or_def word_xor_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2829
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2830
lemma word_wi_log_defs:
71149
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  2831
  "NOT (word_of_int a) = word_of_int (NOT a)"
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2832
  "word_of_int a AND word_of_int b = word_of_int (a AND b)"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2833
  "word_of_int a OR word_of_int b = word_of_int (a OR b)"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2834
  "word_of_int a XOR word_of_int b = word_of_int (a XOR b)"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
  2835
  by (transfer, rule refl)+
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2836
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2837
lemma word_no_log_defs [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2838
  "NOT (numeral a) = word_of_int (NOT (numeral a))"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2839
  "NOT (- numeral a) = word_of_int (NOT (- numeral a))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2840
  "numeral a AND numeral b = word_of_int (numeral a AND numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2841
  "numeral a AND - numeral b = word_of_int (numeral a AND - numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2842
  "- numeral a AND numeral b = word_of_int (- numeral a AND numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2843
  "- numeral a AND - numeral b = word_of_int (- numeral a AND - numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2844
  "numeral a OR numeral b = word_of_int (numeral a OR numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2845
  "numeral a OR - numeral b = word_of_int (numeral a OR - numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2846
  "- numeral a OR numeral b = word_of_int (- numeral a OR numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2847
  "- numeral a OR - numeral b = word_of_int (- numeral a OR - numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2848
  "numeral a XOR numeral b = word_of_int (numeral a XOR numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2849
  "numeral a XOR - numeral b = word_of_int (numeral a XOR - numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2850
  "- numeral a XOR numeral b = word_of_int (- numeral a XOR numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2851
  "- numeral a XOR - numeral b = word_of_int (- numeral a XOR - numeral b)"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2852
  by (transfer, rule refl)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2853
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2854
text \<open>Special cases for when one of the arguments equals 1.\<close>
46064
88ef116e0522 add simp rules for bitwise word operations with 1
huffman
parents: 46057
diff changeset
  2855
88ef116e0522 add simp rules for bitwise word operations with 1
huffman
parents: 46057
diff changeset
  2856
lemma word_bitwise_1_simps [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2857
  "NOT (1::'a::len word) = -2"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2858
  "1 AND numeral b = word_of_int (1 AND numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2859
  "1 AND - numeral b = word_of_int (1 AND - numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2860
  "numeral a AND 1 = word_of_int (numeral a AND 1)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2861
  "- numeral a AND 1 = word_of_int (- numeral a AND 1)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2862
  "1 OR numeral b = word_of_int (1 OR numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2863
  "1 OR - numeral b = word_of_int (1 OR - numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2864
  "numeral a OR 1 = word_of_int (numeral a OR 1)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2865
  "- numeral a OR 1 = word_of_int (- numeral a OR 1)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2866
  "1 XOR numeral b = word_of_int (1 XOR numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2867
  "1 XOR - numeral b = word_of_int (1 XOR - numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2868
  "numeral a XOR 1 = word_of_int (numeral a XOR 1)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2869
  "- numeral a XOR 1 = word_of_int (- numeral a XOR 1)"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2870
  by (transfer, simp)+
46064
88ef116e0522 add simp rules for bitwise word operations with 1
huffman
parents: 46057
diff changeset
  2871
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2872
text \<open>Special cases for when one of the arguments equals -1.\<close>
56979
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  2873
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  2874
lemma word_bitwise_m1_simps [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2875
  "NOT (-1::'a::len word) = 0"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2876
  "(-1::'a::len word) AND x = x"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2877
  "x AND (-1::'a::len word) = x"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2878
  "(-1::'a::len word) OR x = -1"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2879
  "x OR (-1::'a::len word) = -1"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2880
  " (-1::'a::len word) XOR x = NOT x"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2881
  "x XOR (-1::'a::len word) = NOT x"
56979
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  2882
  by (transfer, simp)+
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  2883
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2884
lemma uint_and:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2885
  \<open>uint (x AND y) = uint x AND uint y\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2886
  by transfer simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2887
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2888
lemma uint_or:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2889
  \<open>uint (x OR y) = uint x OR uint y\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2890
  by transfer simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2891
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2892
lemma uint_xor:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2893
  \<open>uint (x XOR y) = uint x XOR uint y\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2894
  by transfer simp
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2895
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2896
lemma test_bit_wi [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2897
  "(word_of_int x :: 'a::len word) !! n \<longleftrightarrow> n < LENGTH('a) \<and> bin_nth x n"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2898
  by (simp add: word_test_bit_def word_ubin.eq_norm nth_bintr)
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2899
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2900
lemma word_test_bit_transfer [transfer_rule]:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  2901
  "(rel_fun pcr_word (rel_fun (=) (=)))
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2902
    (\<lambda>x n. n < LENGTH('a) \<and> bin_nth x n) (test_bit :: 'a::len word \<Rightarrow> _)"
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55833
diff changeset
  2903
  unfolding rel_fun_def word.pcr_cr_eq cr_word_def by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2904
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2905
lemma word_ops_nth_size:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2906
  "n < size x \<Longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2907
    (x OR y) !! n = (x !! n | y !! n) \<and>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2908
    (x AND y) !! n = (x !! n \<and> y !! n) \<and>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2909
    (x XOR y) !! n = (x !! n \<noteq> y !! n) \<and>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2910
    (NOT x) !! n = (\<not> x !! n)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2911
  for x :: "'a::len word"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2912
  unfolding word_size by transfer (simp add: bin_nth_ops)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2913
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2914
lemma word_ao_nth:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2915
  "(x OR y) !! n = (x !! n | y !! n) \<and>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2916
    (x AND y) !! n = (x !! n \<and> y !! n)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2917
  for x :: "'a::len word"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2918
  by transfer (auto simp add: bin_nth_ops)
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  2919
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2920
lemma test_bit_numeral [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2921
  "(numeral w :: 'a::len word) !! n \<longleftrightarrow>
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2922
    n < LENGTH('a) \<and> bin_nth (numeral w) n"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2923
  by transfer (rule refl)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2924
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2925
lemma test_bit_neg_numeral [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2926
  "(- numeral w :: 'a::len word) !! n \<longleftrightarrow>
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2927
    n < LENGTH('a) \<and> bin_nth (- numeral w) n"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2928
  by transfer (rule refl)
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  2929
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2930
lemma test_bit_1 [simp]: "(1 :: 'a::len word) !! n \<longleftrightarrow> n = 0"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2931
  by transfer auto
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2932
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2933
lemma nth_0 [simp]: "\<not> (0 :: 'a::len word) !! n"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2934
  by transfer simp
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  2935
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2936
lemma nth_minus1 [simp]: "(-1 :: 'a::len word) !! n \<longleftrightarrow> n < LENGTH('a)"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2937
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2938
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2939
\<comment> \<open>get from commutativity, associativity etc of \<open>int_and\<close> etc to same for \<open>word_and etc\<close>\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2940
lemmas bwsimps =
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
  2941
  wi_hom_add
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2942
  word_wi_log_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2943
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2944
lemma word_bw_assocs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2945
  "(x AND y) AND z = x AND y AND z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2946
  "(x OR y) OR z = x OR y OR z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2947
  "(x XOR y) XOR z = x XOR y XOR z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2948
  for x :: "'a::len word"
46022
657f87b10944 simplify some proofs
huffman
parents: 46021
diff changeset
  2949
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2950
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2951
lemma word_bw_comms:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2952
  "x AND y = y AND x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2953
  "x OR y = y OR x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2954
  "x XOR y = y XOR x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2955
  for x :: "'a::len word"
46022
657f87b10944 simplify some proofs
huffman
parents: 46021
diff changeset
  2956
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2957
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2958
lemma word_bw_lcs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2959
  "y AND x AND z = x AND y AND z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2960
  "y OR x OR z = x OR y OR z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2961
  "y XOR x XOR z = x XOR y XOR z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2962
  for x :: "'a::len word"
46022
657f87b10944 simplify some proofs
huffman
parents: 46021
diff changeset
  2963
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2964
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2965
lemma word_log_esimps:
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2966
  "x AND 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2967
  "x AND -1 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2968
  "x OR 0 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2969
  "x OR -1 = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2970
  "x XOR 0 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2971
  "x XOR -1 = NOT x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2972
  "0 AND x = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2973
  "-1 AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2974
  "0 OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2975
  "-1 OR x = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2976
  "0 XOR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2977
  "-1 XOR x = NOT x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2978
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2979
  by simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2980
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2981
lemma word_not_dist:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2982
  "NOT (x OR y) = NOT x AND NOT y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2983
  "NOT (x AND y) = NOT x OR NOT y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2984
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2985
  by simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2986
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2987
lemma word_bw_same:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2988
  "x AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2989
  "x OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2990
  "x XOR x = 0"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2991
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2992
  by simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2993
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2994
lemma word_ao_absorbs [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2995
  "x AND (y OR x) = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2996
  "x OR y AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2997
  "x AND (x OR y) = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2998
  "y AND x OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2999
  "(y OR x) AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3000
  "x OR x AND y = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3001
  "(x OR y) AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3002
  "x AND y OR x = x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3003
  for x :: "'a::len word"
46022
657f87b10944 simplify some proofs
huffman
parents: 46021
diff changeset
  3004
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3005
71149
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  3006
lemma word_not_not [simp]: "NOT (NOT x) = x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3007
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3008
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3009
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3010
lemma word_ao_dist: "(x OR y) AND z = x AND z OR y AND z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3011
  for x :: "'a::len word"
46022
657f87b10944 simplify some proofs
huffman
parents: 46021
diff changeset
  3012
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3013
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3014
lemma word_oa_dist: "x AND y OR z = (x OR z) AND (y OR z)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3015
  for x :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3016
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3017
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3018
lemma word_add_not [simp]: "x + NOT x = -1"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3019
  for x :: "'a::len word"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3020
  by transfer (simp add: bin_add_not)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3021
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3022
lemma word_plus_and_or [simp]: "(x AND y) + (x OR y) = x + y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3023
  for x :: "'a::len word"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3024
  by transfer (simp add: plus_and_or)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3025
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3026
lemma leoa: "w = x OR y \<Longrightarrow> y = w AND y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3027
  for x :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3028
  by auto
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3029
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3030
lemma leao: "w' = x' AND y' \<Longrightarrow> x' = x' OR w'"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3031
  for x' :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3032
  by auto
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3033
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3034
lemma word_ao_equiv: "w = w OR w' \<longleftrightarrow> w' = w AND w'"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3035
  for w w' :: "'a::len word"
48196
b7313810b6e6 explicit is better than implicit;
wenzelm
parents: 47941
diff changeset
  3036
  by (auto intro: leoa leao)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3037
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3038
lemma le_word_or2: "x \<le> x OR y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3039
  for x y :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3040
  by (auto simp: word_le_def uint_or intro: le_int_or)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3041
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3042
lemmas le_word_or1 = xtr3 [OF word_bw_comms (2) le_word_or2]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3043
lemmas word_and_le1 = xtr3 [OF word_ao_absorbs (4) [symmetric] le_word_or2]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3044
lemmas word_and_le2 = xtr3 [OF word_ao_absorbs (8) [symmetric] le_word_or2]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3045
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3046
lemma bl_word_not: "to_bl (NOT w) = map Not (to_bl w)"
45550
73a4f31d41c4 Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents: 45549
diff changeset
  3047
  unfolding to_bl_def word_log_defs bl_not_bin
73a4f31d41c4 Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents: 45549
diff changeset
  3048
  by (simp add: word_ubin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3049
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  3050
lemma bl_word_xor: "to_bl (v XOR w) = map2 (\<noteq>) (to_bl v) (to_bl w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3051
  unfolding to_bl_def word_log_defs bl_xor_bin
45550
73a4f31d41c4 Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents: 45549
diff changeset
  3052
  by (simp add: word_ubin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3053
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  3054
lemma bl_word_or: "to_bl (v OR w) = map2 (\<or>) (to_bl v) (to_bl w)"
45550
73a4f31d41c4 Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents: 45549
diff changeset
  3055
  unfolding to_bl_def word_log_defs bl_or_bin
73a4f31d41c4 Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents: 45549
diff changeset
  3056
  by (simp add: word_ubin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3057
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  3058
lemma bl_word_and: "to_bl (v AND w) = map2 (\<and>) (to_bl v) (to_bl w)"
45550
73a4f31d41c4 Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents: 45549
diff changeset
  3059
  unfolding to_bl_def word_log_defs bl_and_bin
73a4f31d41c4 Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents: 45549
diff changeset
  3060
  by (simp add: word_ubin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3061
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3062
lemma word_lsb_alt: "lsb w = test_bit w 0"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3063
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3064
  by (auto simp: word_test_bit_def word_lsb_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3065
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3066
lemma word_lsb_1_0 [simp]: "lsb (1::'a::len word) \<and> \<not> lsb (0::'b::len word)"
45550
73a4f31d41c4 Word.thy: reduce usage of numeral-representation-dependent thms like number_of_is_id in proofs
huffman
parents: 45549
diff changeset
  3067
  unfolding word_lsb_def uint_eq_0 uint_1 by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3068
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3069
lemma word_lsb_last: "lsb w = last (to_bl w)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3070
  for w :: "'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3071
  apply (unfold word_lsb_def uint_bl bin_to_bl_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3072
  apply (rule_tac bin="uint w" in bin_exhaust)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3073
  apply (cases "size w")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3074
   apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3075
   apply (auto simp add: bin_to_bl_aux_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3076
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3077
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3078
lemma word_lsb_int: "lsb w \<longleftrightarrow> uint w mod 2 = 1"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3079
  by (auto simp: word_lsb_def bin_last_def)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3080
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3081
lemma word_msb_sint: "msb w \<longleftrightarrow> sint w < 0"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3082
  by (simp only: word_msb_def sign_Min_lt_0)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3083
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3084
lemma msb_word_of_int: "msb (word_of_int x::'a::len word) = bin_nth x (LENGTH('a) - 1)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3085
  by (simp add: word_msb_def word_sbin.eq_norm bin_sign_lem)
46173
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3086
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3087
lemma word_msb_numeral [simp]:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3088
  "msb (numeral w::'a::len word) = bin_nth (numeral w) (LENGTH('a) - 1)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3089
  unfolding word_numeral_alt by (rule msb_word_of_int)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3090
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3091
lemma word_msb_neg_numeral [simp]:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3092
  "msb (- numeral w::'a::len word) = bin_nth (- numeral w) (LENGTH('a) - 1)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3093
  unfolding word_neg_numeral_alt by (rule msb_word_of_int)
46173
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3094
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3095
lemma word_msb_0 [simp]: "\<not> msb (0::'a::len word)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3096
  by (simp add: word_msb_def)
46173
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3097
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3098
lemma word_msb_1 [simp]: "msb (1::'a::len word) \<longleftrightarrow> LENGTH('a) = 1"
46173
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3099
  unfolding word_1_wi msb_word_of_int eq_iff [where 'a=nat]
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3100
  by (simp add: Suc_le_eq)
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3101
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3102
lemma word_msb_nth: "msb w = bin_nth (uint w) (LENGTH('a) - 1)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3103
  for w :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3104
  by (simp add: word_msb_def sint_uint bin_sign_lem)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3105
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3106
lemma word_msb_alt: "msb w = hd (to_bl w)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3107
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3108
  apply (unfold word_msb_nth uint_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3109
  apply (subst hd_conv_nth)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3110
   apply (rule length_greater_0_conv [THEN iffD1])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3111
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3112
  apply (simp add : nth_bin_to_bl word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3113
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3114
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3115
lemma word_set_nth [simp]: "set_bit w n (test_bit w n) = w"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3116
  for w :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3117
  by (auto simp: word_test_bit_def word_set_bit_def)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3118
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3119
lemma bin_nth_uint': "bin_nth (uint w) n \<longleftrightarrow> rev (bin_to_bl (size w) (uint w)) ! n \<and> n < size w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3120
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3121
  apply (safe elim!: bin_nth_uint_imp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3122
   apply (frule bin_nth_uint_imp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3123
   apply (fast dest!: bin_nth_bl)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3124
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3125
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3126
lemmas bin_nth_uint = bin_nth_uint' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3127
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3128
lemma test_bit_bl: "w !! n \<longleftrightarrow> rev (to_bl w) ! n \<and> n < size w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3129
  unfolding to_bl_def word_test_bit_def word_size by (rule bin_nth_uint)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3130
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  3131
lemma to_bl_nth: "n < size w \<Longrightarrow> to_bl w ! n = w !! (size w - Suc n)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3132
  apply (unfold test_bit_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3133
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3134
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3135
   apply (rule nth_rev_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3136
   apply (auto simp add: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3137
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3138
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3139
lemma map_bit_interval_eq:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3140
  \<open>map (bit w) [0..<n] = takefill False n (rev (to_bl w))\<close> for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3141
proof (rule nth_equalityI)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3142
  show \<open>length (map (bit w) [0..<n]) =
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3143
    length (takefill False n (rev (to_bl w)))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3144
    by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3145
  fix m
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3146
  assume \<open>m < length (map (bit w) [0..<n])\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3147
  then have \<open>m < n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3148
    by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3149
  then have \<open>bit w m \<longleftrightarrow> takefill False n (rev (to_bl w)) ! m\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3150
    by (auto simp add: nth_takefill not_less rev_nth to_bl_nth word_size test_bit_word_eq dest: bit_imp_le_length)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3151
  with \<open>m < n \<close>show \<open>map (bit w) [0..<n] ! m \<longleftrightarrow> takefill False n (rev (to_bl w)) ! m\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3152
    by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3153
qed
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3154
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3155
lemma to_bl_unfold:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3156
  \<open>to_bl w = rev (map (bit w) [0..<LENGTH('a)])\<close> for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3157
  by (simp add: map_bit_interval_eq takefill_bintrunc to_bl_def flip: bin_to_bl_def)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3158
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3159
lemma nth_rev_to_bl:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3160
  \<open>rev (to_bl w) ! n \<longleftrightarrow> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3161
  if \<open>n < LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3162
  using that by (simp add: to_bl_unfold)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3163
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3164
lemma nth_to_bl:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3165
  \<open>to_bl w ! n \<longleftrightarrow> bit w (LENGTH('a) - Suc n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3166
  if \<open>n < LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3167
  using that by (simp add: to_bl_unfold rev_nth)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3168
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3169
lemma test_bit_set: "(set_bit w n x) !! n \<longleftrightarrow> n < size w \<and> x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3170
  for w :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3171
  by (auto simp: word_size word_test_bit_def word_set_bit_def word_ubin.eq_norm nth_bintr)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3172
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3173
lemma test_bit_set_gen:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3174
  "test_bit (set_bit w n x) m = (if m = n then n < size w \<and> x else test_bit w m)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3175
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3176
  apply (unfold word_size word_test_bit_def word_set_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3177
  apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3178
  apply (auto elim!: test_bit_size [unfolded word_size]
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3179
      simp add: word_test_bit_def [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3180
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3181
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3182
lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3183
  by (auto simp: of_bl_def bl_to_bin_rep_F)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3184
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3185
lemma bit_word_reverse_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3186
  \<open>bit (word_reverse w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w (LENGTH('a) - Suc n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3187
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3188
  by (cases \<open>n < LENGTH('a)\<close>) (simp_all add: word_reverse_def bit_of_bl_iff nth_to_bl)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3189
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3190
lemma bit_slice1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3191
  \<open>bit (slice1 m w :: 'b::len word) n \<longleftrightarrow> m - LENGTH('a) \<le> n \<and> n < min LENGTH('b) m
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3192
    \<and> bit w (n + (LENGTH('a) - m) - (m - LENGTH('a)))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3193
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3194
  by (cases \<open>n + (LENGTH('a) - m) - (m - LENGTH('a)) < LENGTH('a)\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3195
    (auto simp add: slice1_def bit_of_bl_iff takefill_alt rev_take nth_append not_less nth_rev_to_bl ac_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3196
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3197
lemma bit_revcast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3198
  \<open>bit (revcast w :: 'b::len word) n \<longleftrightarrow> LENGTH('b) - LENGTH('a) \<le> n \<and> n < LENGTH('b)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3199
    \<and> bit w (n + (LENGTH('a) - LENGTH('b)) - (LENGTH('b) - LENGTH('a)))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3200
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3201
  by (simp add: revcast_eq bit_slice1_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3202
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3203
lemma bit_slice_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3204
  \<open>bit (slice m w :: 'b::len word) n \<longleftrightarrow> n < min LENGTH('b) (LENGTH('a) - m) \<and> bit w (n + LENGTH('a) - (LENGTH('a) - m))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3205
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3206
  by (simp add: slice_def word_size bit_slice1_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3207
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3208
lemma msb_nth: "msb w = w !! (LENGTH('a) - 1)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3209
  for w :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3210
  by (simp add: word_msb_nth word_test_bit_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3211
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3212
lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN word_ops_nth_size [unfolded word_size]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3213
lemmas msb1 = msb0 [where i = 0]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3214
lemmas word_ops_msb = msb1 [unfolded msb_nth [symmetric, unfolded One_nat_def]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3215
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3216
lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3217
lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3218
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3219
lemma word_set_set_same [simp]: "set_bit (set_bit w n x) n y = set_bit w n y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3220
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3221
  by (rule word_eqI) (simp add : test_bit_set_gen word_size)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3222
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3223
lemma word_set_set_diff:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3224
  fixes w :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3225
  assumes "m \<noteq> n"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3226
  shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3227
  by (rule word_eqI) (auto simp: test_bit_set_gen word_size assms)
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3228
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3229
lemma nth_sint:
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3230
  fixes w :: "'a::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3231
  defines "l \<equiv> LENGTH('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3232
  shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3233
  unfolding sint_uint l_def
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3234
  by (auto simp: nth_sbintr word_test_bit_def [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3235
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3236
lemma word_lsb_numeral [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3237
  "lsb (numeral bin :: 'a::len word) \<longleftrightarrow> bin_last (numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3238
  unfolding word_lsb_alt test_bit_numeral by simp
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3239
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3240
lemma word_lsb_neg_numeral [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3241
  "lsb (- numeral bin :: 'a::len word) \<longleftrightarrow> bin_last (- numeral bin)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3242
  by (simp add: word_lsb_alt)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3243
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3244
lemma set_bit_word_of_int: "set_bit (word_of_int x) n b = word_of_int (bin_sc n b x)"
46173
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3245
  unfolding word_set_bit_def
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3246
  by (rule word_eqI)(simp add: word_size bin_nth_sc_gen word_ubin.eq_norm nth_bintr)
46173
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3247
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3248
lemma word_set_numeral [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3249
  "set_bit (numeral bin::'a::len word) n b =
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54743
diff changeset
  3250
    word_of_int (bin_sc n b (numeral bin))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3251
  unfolding word_numeral_alt by (rule set_bit_word_of_int)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3252
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3253
lemma word_set_neg_numeral [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3254
  "set_bit (- numeral bin::'a::len word) n b =
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54743
diff changeset
  3255
    word_of_int (bin_sc n b (- numeral bin))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3256
  unfolding word_neg_numeral_alt by (rule set_bit_word_of_int)
46173
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3257
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3258
lemma word_set_bit_0 [simp]: "set_bit 0 n b = word_of_int (bin_sc n b 0)"
46173
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3259
  unfolding word_0_wi by (rule set_bit_word_of_int)
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3260
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3261
lemma word_set_bit_1 [simp]: "set_bit 1 n b = word_of_int (bin_sc n b 1)"
46173
5cc700033194 add simp rules for set_bit and msb applied to 0 and 1
huffman
parents: 46172
diff changeset
  3262
  unfolding word_1_wi by (rule set_bit_word_of_int)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3263
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3264
lemma setBit_no [simp]: "setBit (numeral bin) n = word_of_int (bin_sc n True (numeral bin))"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  3265
  by (simp add: setBit_def)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  3266
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  3267
lemma clearBit_no [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54743
diff changeset
  3268
  "clearBit (numeral bin) n = word_of_int (bin_sc n False (numeral bin))"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  3269
  by (simp add: clearBit_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3270
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3271
lemma to_bl_n1 [simp]: "to_bl (-1::'a::len word) = replicate (LENGTH('a)) True"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3272
  apply (rule word_bl.Abs_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3273
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3274
  apply (rule word_eqI)
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  3275
  apply (clarsimp simp add: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3276
  apply (auto simp add: word_bl.Abs_inverse test_bit_bl word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3277
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3278
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  3279
lemma word_msb_n1 [simp]: "msb (-1::'a::len word)"
41550
efa734d9b221 eliminated global prems;
wenzelm
parents: 41413
diff changeset
  3280
  unfolding word_msb_alt to_bl_n1 by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3281
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3282
lemma word_set_nth_iff: "set_bit w n b = w \<longleftrightarrow> w !! n = b \<or> n \<ge> size w"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3283
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3284
  apply (rule iffI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3285
   apply (rule disjCI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3286
   apply (drule word_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3287
   apply (erule sym [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3288
   apply (simp add: test_bit_set)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3289
  apply (erule disjE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3290
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3291
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3292
  apply (clarsimp simp add : test_bit_set_gen)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3293
  apply (drule test_bit_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3294
  apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3295
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3296
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3297
lemma test_bit_2p: "(word_of_int (2 ^ n)::'a::len word) !! m \<longleftrightarrow> m = n \<and> m < LENGTH('a)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3298
  by (auto simp: word_test_bit_def word_ubin.eq_norm nth_bintr nth_2p_bin)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3299
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3300
lemma nth_w2p: "((2::'a::len word) ^ n) !! m \<longleftrightarrow> m = n \<and> m < LENGTH('a::len)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3301
  by (simp add: test_bit_2p [symmetric] word_of_int [symmetric])
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3302
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3303
lemma uint_2p: "(0::'a::len word) < 2 ^ n \<Longrightarrow> uint (2 ^ n::'a::len word) = 2 ^ n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3304
  apply (unfold word_arith_power_alt)
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3305
  apply (case_tac "LENGTH('a)")
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3306
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3307
  apply (case_tac "nat")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3308
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3309
   apply (case_tac "n")
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3310
    apply clarsimp
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3311
   apply clarsimp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3312
  apply (drule word_gt_0 [THEN iffD1])
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3313
  apply (safe intro!: word_eqI)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3314
   apply (auto simp add: nth_2p_bin)
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3315
  apply (erule notE)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3316
  apply (simp (no_asm_use) add: uint_word_of_int word_size)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3317
  apply (subst mod_pos_pos_trivial)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3318
    apply simp
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3319
   apply (rule power_strict_increasing)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3320
    apply simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3321
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3322
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3323
lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a::len word) = 2 ^ n"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  3324
  by (induct n) (simp_all add: wi_hom_syms)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3325
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3326
lemma bang_is_le: "x !! m \<Longrightarrow> 2 ^ m \<le> x"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3327
  for x :: "'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3328
  apply (rule xtr3)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3329
   apply (rule_tac [2] y = "x" in le_word_or2)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3330
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3331
  apply (auto simp add: word_ao_nth nth_w2p word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3332
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3333
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3334
lemma word_clr_le: "w \<ge> set_bit w n False"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3335
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3336
  apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3337
  apply (rule order_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3338
   apply (rule bintr_bin_clr_le)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3339
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3340
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3341
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3342
lemma word_set_ge: "w \<le> set_bit w n True"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3343
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3344
  apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3345
  apply (rule order_trans [OF _ bintr_bin_set_ge])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3346
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3347
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3348
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3349
lemma set_bit_beyond:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3350
  "size x \<le> n \<Longrightarrow> set_bit x n b = x" for x :: "'a :: len word"
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3351
  by (auto intro: word_eqI simp add: test_bit_set_gen word_size)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3352
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  3353
lemma rbl_word_or: "rev (to_bl (x OR y)) = map2 (\<or>) (rev (to_bl x)) (rev (to_bl y))"
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  3354
  by (simp add: zip_rev bl_word_or rev_map)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  3355
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  3356
lemma rbl_word_and: "rev (to_bl (x AND y)) = map2 (\<and>) (rev (to_bl x)) (rev (to_bl y))"
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  3357
  by (simp add: zip_rev bl_word_and rev_map)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  3358
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  3359
lemma rbl_word_xor: "rev (to_bl (x XOR y)) = map2 (\<noteq>) (rev (to_bl x)) (rev (to_bl y))"
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  3360
  by (simp add: zip_rev bl_word_xor rev_map)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  3361
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  3362
lemma rbl_word_not: "rev (to_bl (NOT x)) = map Not (rev (to_bl x))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  3363
  by (simp add: bl_word_not rev_map)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
  3364
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3365
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3366
subsection \<open>Bit comprehension\<close>
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3367
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3368
instantiation word :: (len) bit_comprehension
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3369
begin
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3370
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3371
definition word_set_bits_def: "(BITS n. f n) = of_bl (bl_of_nth LENGTH('a) f)"
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3372
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3373
instance ..
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3374
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3375
end
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3376
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3377
lemma bit_set_bits_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3378
  \<open>bit (set_bits P :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> P n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3379
  by (auto simp add: word_set_bits_def bit_of_bl_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3380
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3381
lemmas of_nth_def = word_set_bits_def (* FIXME duplicate *)
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3382
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3383
lemma td_ext_nth [OF refl refl refl, unfolded word_size]:
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3384
  "n = size w \<Longrightarrow> ofn = set_bits \<Longrightarrow> [w, ofn g] = l \<Longrightarrow>
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3385
    td_ext test_bit ofn {f. \<forall>i. f i \<longrightarrow> i < n} (\<lambda>h i. h i \<and> i < n)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3386
  for w :: "'a::len word"
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3387
  apply (unfold word_size td_ext_def')
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3388
  apply safe
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3389
     apply (rule_tac [3] ext)
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3390
     apply (rule_tac [4] ext)
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3391
     apply (unfold word_size of_nth_def test_bit_bl)
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3392
     apply safe
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3393
       defer
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3394
       apply (clarsimp simp: word_bl.Abs_inverse)+
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3395
  apply (rule word_bl.Rep_inverse')
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3396
  apply (rule sym [THEN trans])
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3397
   apply (rule bl_of_nth_nth)
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3398
  apply simp
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3399
  apply (rule bl_of_nth_inj)
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3400
  apply (clarsimp simp add : test_bit_bl word_size)
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3401
  done
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3402
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3403
interpretation test_bit:
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3404
  td_ext
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3405
    "(!!) :: 'a::len word \<Rightarrow> nat \<Rightarrow> bool"
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3406
    set_bits
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3407
    "{f. \<forall>i. f i \<longrightarrow> i < LENGTH('a::len)}"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3408
    "(\<lambda>h i. h i \<and> i < LENGTH('a::len))"
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3409
  by (rule td_ext_nth)
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3410
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3411
lemmas td_nth = test_bit.td_thm
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3412
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3413
lemma set_bits_K_False [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3414
  "set_bits (\<lambda>_. False) = (0 :: 'a :: len word)"
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3415
  by (rule word_eqI) (simp add: test_bit.eq_norm)
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3416
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3417
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3418
subsection \<open>Shifting, Rotating, and Splitting Words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3419
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  3420
lemma shiftl1_wi [simp]: "shiftl1 (word_of_int w) = word_of_int (2 * w)"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3421
  unfolding shiftl1_def
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  3422
  apply (simp add: word_ubin.norm_eq_iff [symmetric] word_ubin.eq_norm)
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
  3423
  apply (simp add: mod_mult_right_eq take_bit_eq_mod)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3424
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3425
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3426
lemma shiftl1_numeral [simp]: "shiftl1 (numeral w) = numeral (Num.Bit0 w)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3427
  unfolding word_numeral_alt shiftl1_wi by simp
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3428
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3429
lemma shiftl1_neg_numeral [simp]: "shiftl1 (- numeral w) = - numeral (Num.Bit0 w)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3430
  unfolding word_neg_numeral_alt shiftl1_wi by simp
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3431
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3432
lemma shiftl1_0 [simp] : "shiftl1 0 = 0"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3433
  by (simp add: shiftl1_def)
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3434
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  3435
lemma shiftl1_def_u: "shiftl1 w = word_of_int (2 * uint w)"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3436
  by (simp only: shiftl1_def) (* FIXME: duplicate *)
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3437
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  3438
lemma shiftl1_def_s: "shiftl1 w = word_of_int (2 * sint w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  3439
  by (simp add: shiftl1_def wi_hom_syms)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3440
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  3441
lemma shiftr1_0 [simp]: "shiftr1 0 = 0"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3442
  by (simp add: shiftr1_def)
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  3443
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  3444
lemma sshiftr1_0 [simp]: "sshiftr1 0 = 0"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3445
  by (simp add: sshiftr1_def)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3446
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3447
lemma sshiftr1_n1 [simp]: "sshiftr1 (- 1) = - 1"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3448
  by (simp add: sshiftr1_def)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3449
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3450
lemma shiftl_0 [simp]: "(0::'a::len word) << n = 0"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3451
  by (induct n) (auto simp: shiftl_def)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3452
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3453
lemma shiftr_0 [simp]: "(0::'a::len word) >> n = 0"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3454
  by (induct n) (auto simp: shiftr_def)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3455
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3456
lemma sshiftr_0 [simp]: "0 >>> n = 0"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3457
  by (induct n) (auto simp: sshiftr_def)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3458
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3459
lemma sshiftr_n1 [simp]: "-1 >>> n = -1"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3460
  by (induct n) (auto simp: sshiftr_def)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3461
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3462
lemma nth_shiftl1: "shiftl1 w !! n \<longleftrightarrow> n < size w \<and> n > 0 \<and> w !! (n - 1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3463
  apply (unfold shiftl1_def word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3464
  apply (simp add: nth_bintr word_ubin.eq_norm word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3465
  apply (cases n)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  3466
  apply (simp_all add: bit_Suc)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3467
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3468
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3469
lemma nth_shiftl': "(w << m) !! n \<longleftrightarrow> n < size w \<and> n >= m \<and> w !! (n - m)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3470
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3471
  apply (unfold shiftl_def)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3472
  apply (induct m arbitrary: n)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3473
   apply (force elim!: test_bit_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3474
  apply (clarsimp simp add : nth_shiftl1 word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3475
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3476
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3477
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3478
lemmas nth_shiftl = nth_shiftl' [unfolded word_size]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3479
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3480
lemma nth_shiftr1: "shiftr1 w !! n = w !! Suc n"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71948
diff changeset
  3481
  apply (auto simp add: shiftr1_def word_test_bit_def word_ubin.eq_norm bit_take_bit_iff bit_Suc)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71948
diff changeset
  3482
  apply (metis (no_types, hide_lams) add_Suc_right add_diff_cancel_left' bit_Suc diff_is_0_eq' le_Suc_ex less_imp_le linorder_not_le test_bit_bin word_test_bit_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3483
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3484
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3485
lemma nth_shiftr: "(w >> m) !! n = w !! (n + m)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3486
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3487
  apply (unfold shiftr_def)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3488
  apply (induct "m" arbitrary: n)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3489
   apply (auto simp add: nth_shiftr1)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3490
  done
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3491
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3492
text \<open>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3493
  see paper page 10, (1), (2), \<open>shiftr1_def\<close> is of the form of (1),
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3494
  where \<open>f\<close> (ie \<open>bin_rest\<close>) takes normal arguments to normal results,
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3495
  thus we get (2) from (1)
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3496
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3497
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3498
lemma uint_shiftr1: "uint (shiftr1 w) = bin_rest (uint w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3499
  apply (unfold shiftr1_def word_ubin.eq_norm bin_rest_trunc_i)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3500
  apply (subst bintr_uint [symmetric, OF order_refl])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3501
  apply (simp only : bintrunc_bintrunc_l)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3502
  apply simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3503
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3504
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3505
lemma bit_sshiftr1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3506
  \<open>bit (sshiftr1 w) n \<longleftrightarrow> bit w (if n = LENGTH('a) - 1 then LENGTH('a) - 1 else Suc n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3507
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3508
  apply (cases \<open>LENGTH('a)\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3509
  apply simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3510
  apply (simp add: sshiftr1_def bit_word_of_int_iff bit_sint_iff flip: bit_Suc)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3511
  apply transfer apply auto
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3512
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3513
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3514
lemma bit_sshiftr_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3515
  \<open>bit (w >>> m) n \<longleftrightarrow> bit w (if LENGTH('a) - m \<le> n \<and> n < LENGTH('a) then LENGTH('a) - 1 else (m + n))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3516
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3517
  apply (cases \<open>LENGTH('a)\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3518
   apply simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3519
  apply (simp only:)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3520
  apply (induction m arbitrary: n)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3521
   apply (auto simp add: sshiftr_def bit_sshiftr1_iff not_le less_diff_conv)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3522
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3523
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3524
lemma nth_sshiftr1: "sshiftr1 w !! n = (if n = size w - 1 then w !! n else w !! Suc n)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3525
  apply (unfold sshiftr1_def word_test_bit_def)
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71948
diff changeset
  3526
  apply (simp add: nth_bintr word_ubin.eq_norm bit_Suc [symmetric] word_size)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71948
diff changeset
  3527
  apply (simp add: nth_bintr uint_sint)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3528
  apply (auto simp add: bin_nth_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3529
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3530
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3531
lemma nth_sshiftr [rule_format] :
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3532
  "\<forall>n. sshiftr w m !! n =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3533
    (n < size w \<and> (if n + m \<ge> size w then w !! (size w - 1) else w !! (n + m)))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3534
  apply (unfold sshiftr_def)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3535
  apply (induct_tac m)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3536
   apply (simp add: test_bit_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3537
  apply (clarsimp simp add: nth_sshiftr1 word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3538
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3539
       apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3540
      apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3541
     apply (erule thin_rl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3542
     apply (case_tac n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3543
      apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3544
      apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3545
     apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3546
    apply (erule thin_rl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3547
    apply (case_tac n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3548
     apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3549
     apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3550
    apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3551
   apply arith+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3552
  done
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3553
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3554
lemma shiftr1_div_2: "uint (shiftr1 w) = uint w div 2"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  3555
  apply (unfold shiftr1_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3556
  apply (rule word_uint.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3557
  apply (simp add: uints_num pos_imp_zdiv_nonneg_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3558
  apply (rule xtr7)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3559
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3560
   apply (rule zdiv_le_dividend)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3561
    apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3562
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3563
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3564
lemma sshiftr1_div_2: "sint (sshiftr1 w) = sint w div 2"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  3565
  apply (unfold sshiftr1_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3566
  apply (simp add: word_sbin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3567
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3568
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3569
   apply (subst word_sbin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3570
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3571
  apply (subst word_sbin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3572
  apply (unfold One_nat_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3573
  apply (rule sbintrunc_rest)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3574
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3575
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3576
lemma shiftr_div_2n: "uint (shiftr w n) = uint w div 2 ^ n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3577
  apply (unfold shiftr_def)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3578
  apply (induct n)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3579
   apply simp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3580
  apply (simp add: shiftr1_div_2 mult.commute zdiv_zmult2_eq [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3581
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3582
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3583
lemma sshiftr_div_2n: "sint (sshiftr w n) = sint w div 2 ^ n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3584
  apply (unfold sshiftr_def)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3585
  apply (induct n)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3586
   apply simp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3587
  apply (simp add: sshiftr1_div_2 mult.commute zdiv_zmult2_eq [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3588
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3589
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3590
lemma bit_bshiftr1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3591
  \<open>bit (bshiftr1 b w) n \<longleftrightarrow> b \<and> n = LENGTH('a) - 1 \<or> bit w (Suc n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3592
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3593
  apply (cases \<open>LENGTH('a)\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3594
  apply simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3595
  apply (simp add: bshiftr1_def bit_of_bl_iff nth_append not_less rev_nth nth_butlast nth_to_bl)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3596
  apply (use bit_imp_le_length in fastforce)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3597
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3598
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  3599
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3600
subsubsection \<open>shift functions in terms of lists of bools\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3601
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3602
lemmas bshiftr1_numeral [simp] =
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3603
  bshiftr1_def [where w="numeral w", unfolded to_bl_numeral] for w
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3604
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3605
lemma bshiftr1_bl: "to_bl (bshiftr1 b w) = b # butlast (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3606
  unfolding bshiftr1_def by (rule word_bl.Abs_inverse) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3607
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3608
lemma shiftl1_of_bl: "shiftl1 (of_bl bl) = of_bl (bl @ [False])"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3609
  by (simp add: of_bl_def bl_to_bin_append)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3610
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3611
lemma shiftl1_bl: "shiftl1 w = of_bl (to_bl w @ [False])"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3612
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3613
proof -
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3614
  have "shiftl1 w = shiftl1 (of_bl (to_bl w))"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3615
    by simp
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3616
  also have "\<dots> = of_bl (to_bl w @ [False])"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3617
    by (rule shiftl1_of_bl)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3618
  finally show ?thesis .
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3619
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3620
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3621
lemma bl_shiftl1: "to_bl (shiftl1 w) = tl (to_bl w) @ [False]"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3622
  for w :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3623
  by (simp add: shiftl1_bl word_rep_drop drop_Suc drop_Cons') (fast intro!: Suc_leI)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3624
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3625
\<comment> \<open>Generalized version of \<open>bl_shiftl1\<close>. Maybe this one should replace it?\<close>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3626
lemma bl_shiftl1': "to_bl (shiftl1 w) = tl (to_bl w @ [False])"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3627
  by (simp add: shiftl1_bl word_rep_drop drop_Suc del: drop_append)
45807
ff10ec0d62ea generalize some lemmas
huffman
parents: 45805
diff changeset
  3628
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3629
lemma shiftr1_bl: "shiftr1 w = of_bl (butlast (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3630
  apply (unfold shiftr1_def uint_bl of_bl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3631
  apply (simp add: butlast_rest_bin word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3632
  apply (simp add: bin_rest_trunc [symmetric, unfolded One_nat_def])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3633
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3634
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3635
lemma bl_shiftr1: "to_bl (shiftr1 w) = False # butlast (to_bl w)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3636
  for w :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3637
  by (simp add: shiftr1_bl word_rep_drop len_gt_0 [THEN Suc_leI])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3638
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3639
\<comment> \<open>Generalized version of \<open>bl_shiftr1\<close>. Maybe this one should replace it?\<close>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3640
lemma bl_shiftr1': "to_bl (shiftr1 w) = butlast (False # to_bl w)"
45807
ff10ec0d62ea generalize some lemmas
huffman
parents: 45805
diff changeset
  3641
  apply (rule word_bl.Abs_inverse')
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3642
   apply (simp del: butlast.simps)
45807
ff10ec0d62ea generalize some lemmas
huffman
parents: 45805
diff changeset
  3643
  apply (simp add: shiftr1_bl of_bl_def)
ff10ec0d62ea generalize some lemmas
huffman
parents: 45805
diff changeset
  3644
  done
ff10ec0d62ea generalize some lemmas
huffman
parents: 45805
diff changeset
  3645
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3646
lemma shiftl1_rev: "shiftl1 w = word_reverse (shiftr1 (word_reverse w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3647
  apply (unfold word_reverse_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3648
  apply (rule word_bl.Rep_inverse' [symmetric])
45807
ff10ec0d62ea generalize some lemmas
huffman
parents: 45805
diff changeset
  3649
  apply (simp add: bl_shiftl1' bl_shiftr1' word_bl.Abs_inverse)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3650
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3651
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3652
lemma shiftl_rev: "shiftl w n = word_reverse (shiftr (word_reverse w) n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3653
  by (induct n) (auto simp add: shiftl_def shiftr_def shiftl1_rev)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3654
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3655
lemma rev_shiftl: "word_reverse w << n = word_reverse (w >> n)"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3656
  by (simp add: shiftl_rev)
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3657
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3658
lemma shiftr_rev: "w >> n = word_reverse (word_reverse w << n)"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3659
  by (simp add: rev_shiftl)
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3660
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3661
lemma rev_shiftr: "word_reverse w >> n = word_reverse (w << n)"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3662
  by (simp add: shiftr_rev)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3663
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3664
lemma bl_sshiftr1: "to_bl (sshiftr1 w) = hd (to_bl w) # butlast (to_bl w)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3665
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3666
  apply (unfold sshiftr1_def uint_bl word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3667
  apply (simp add: butlast_rest_bin word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3668
  apply (simp add: sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3669
  apply (rule nth_equalityI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3670
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3671
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3672
  apply (case_tac i)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3673
   apply (simp_all add: hd_conv_nth length_0_conv [symmetric]
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71948
diff changeset
  3674
      nth_bin_to_bl bit_Suc [symmetric] nth_sbintr)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3675
   apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3676
  apply (rule impI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3677
  apply (rule_tac f = "bin_nth (uint w)" in arg_cong)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3678
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3679
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3680
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3681
lemma drop_shiftr: "drop n (to_bl (w >> n)) = take (size w - n) (to_bl w)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3682
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3683
  apply (unfold shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3684
  apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3685
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3686
   apply (simp add: drop_Suc bl_shiftr1 butlast_drop [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3687
   apply (rule butlast_take [THEN trans])
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3688
    apply (auto simp: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3689
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3690
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3691
lemma drop_sshiftr: "drop n (to_bl (w >>> n)) = take (size w - n) (to_bl w)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3692
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3693
  apply (unfold sshiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3694
  apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3695
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3696
   apply (simp add: drop_Suc bl_sshiftr1 butlast_drop [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3697
   apply (rule butlast_take [THEN trans])
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3698
    apply (auto simp: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3699
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3700
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3701
lemma take_shiftr: "n \<le> size w \<Longrightarrow> take n (to_bl (w >> n)) = replicate n False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3702
  apply (unfold shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3703
  apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3704
   prefer 2
45807
ff10ec0d62ea generalize some lemmas
huffman
parents: 45805
diff changeset
  3705
   apply (simp add: bl_shiftr1' length_0_conv [symmetric] word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3706
   apply (rule take_butlast [THEN trans])
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3707
    apply (auto simp: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3708
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3709
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3710
lemma take_sshiftr' [rule_format] :
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3711
  "n \<le> size w \<longrightarrow> hd (to_bl (w >>> n)) = hd (to_bl w) \<and>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3712
    take n (to_bl (w >>> n)) = replicate n (hd (to_bl w))"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3713
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3714
  apply (unfold sshiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3715
  apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3716
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3717
   apply (simp add: bl_sshiftr1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3718
   apply (rule impI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3719
   apply (rule take_butlast [THEN trans])
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3720
    apply (auto simp: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3721
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3722
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3723
lemmas hd_sshiftr = take_sshiftr' [THEN conjunct1]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3724
lemmas take_sshiftr = take_sshiftr' [THEN conjunct2]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3725
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  3726
lemma atd_lem: "take n xs = t \<Longrightarrow> drop n xs = d \<Longrightarrow> xs = t @ d"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3727
  by (auto intro: append_take_drop_id [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3728
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3729
lemmas bl_shiftr = atd_lem [OF take_shiftr drop_shiftr]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3730
lemmas bl_sshiftr = atd_lem [OF take_sshiftr drop_sshiftr]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3731
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3732
lemma shiftl_of_bl: "of_bl bl << n = of_bl (bl @ replicate n False)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3733
  by (induct n) (auto simp: shiftl_def shiftl1_of_bl replicate_app_Cons_same)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3734
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3735
lemma shiftl_bl: "w << n = of_bl (to_bl w @ replicate n False)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3736
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3737
proof -
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3738
  have "w << n = of_bl (to_bl w) << n"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3739
    by simp
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3740
  also have "\<dots> = of_bl (to_bl w @ replicate n False)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3741
    by (rule shiftl_of_bl)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3742
  finally show ?thesis .
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3743
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3744
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3745
lemmas shiftl_numeral [simp] = shiftl_def [where w="numeral w"] for w
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3746
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3747
lemma bl_shiftl: "to_bl (w << n) = drop n (to_bl w) @ replicate (min (size w) n) False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3748
  by (simp add: shiftl_bl word_rep_drop word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3749
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3750
lemma shiftl_zero_size: "size x \<le> n \<Longrightarrow> x << n = 0"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3751
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3752
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3753
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3754
  apply (clarsimp simp add: shiftl_bl word_size test_bit_of_bl nth_append)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3755
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3756
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3757
\<comment> \<open>note -- the following results use \<open>'a::len word < number_ring\<close>\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3758
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3759
lemma shiftl1_2t: "shiftl1 w = 2 * w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3760
  for w :: "'a::len word"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  3761
  by (simp add: shiftl1_def wi_hom_mult [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3762
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3763
lemma shiftl1_p: "shiftl1 w = w + w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3764
  for w :: "'a::len word"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3765
  by (simp add: shiftl1_2t)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3766
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3767
lemma shiftl_t2n: "shiftl w n = 2 ^ n * w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3768
  for w :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3769
  by (induct n) (auto simp: shiftl_def shiftl1_2t)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3770
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3771
lemma shiftr1_bintr [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3772
  "(shiftr1 (numeral w) :: 'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3773
    word_of_int (bin_rest (bintrunc (LENGTH('a)) (numeral w)))"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3774
  unfolding shiftr1_def word_numeral_alt by (simp add: word_ubin.eq_norm)
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  3775
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  3776
lemma sshiftr1_sbintr [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3777
  "(sshiftr1 (numeral w) :: 'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3778
    word_of_int (bin_rest (sbintrunc (LENGTH('a) - 1) (numeral w)))"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3779
  unfolding sshiftr1_def word_numeral_alt by (simp add: word_sbin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3780
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  3781
lemma shiftr_no [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3782
  (* FIXME: neg_numeral *)
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3783
  "(numeral w::'a::len word) >> n = word_of_int
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3784
    ((bin_rest ^^ n) (bintrunc (LENGTH('a)) (numeral w)))"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3785
  by (rule word_eqI) (auto simp: nth_shiftr nth_rest_power_bin nth_bintr word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3786
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  3787
lemma sshiftr_no [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3788
  (* FIXME: neg_numeral *)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3789
  "(numeral w::'a::len word) >>> n = word_of_int
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3790
    ((bin_rest ^^ n) (sbintrunc (LENGTH('a) - 1) (numeral w)))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3791
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3792
  apply (auto simp: nth_sshiftr nth_rest_power_bin nth_sbintr word_size)
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3793
   apply (subgoal_tac "na + n = LENGTH('a) - Suc 0", simp, simp)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3794
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3795
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3796
lemma shiftr1_bl_of:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3797
  "length bl \<le> LENGTH('a) \<Longrightarrow>
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3798
    shiftr1 (of_bl bl::'a::len word) = of_bl (butlast bl)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3799
  by (clarsimp simp: shiftr1_def of_bl_def butlast_rest_bl2bin word_ubin.eq_norm trunc_bl2bin)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3800
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3801
lemma shiftr_bl_of:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3802
  "length bl \<le> LENGTH('a) \<Longrightarrow>
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3803
    (of_bl bl::'a::len word) >> n = of_bl (take (length bl - n) bl)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3804
  apply (unfold shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3805
  apply (induct n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3806
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3807
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3808
  apply (subst shiftr1_bl_of)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3809
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3810
  apply (simp add: butlast_take)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3811
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3812
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3813
lemma shiftr_bl: "x >> n \<equiv> of_bl (take (LENGTH('a) - n) (to_bl x))"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3814
  for x :: "'a::len word"
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3815
  using shiftr_bl_of [where 'a='a, of "to_bl x"] by simp
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3816
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3817
lemma msb_shift: "msb w \<longleftrightarrow> w >> (LENGTH('a) - 1) \<noteq> 0"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3818
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3819
  apply (unfold shiftr_bl word_msb_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3820
  apply (simp add: word_size Suc_le_eq take_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3821
  apply (cases "hd (to_bl w)")
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3822
   apply (auto simp: word_1_bl of_bl_rep_False [where n=1 and bs="[]", simplified])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3823
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3824
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3825
lemma zip_replicate: "n \<ge> length ys \<Longrightarrow> zip (replicate n x) ys = map (\<lambda>y. (x, y)) ys"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3826
  apply (induct ys arbitrary: n)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3827
   apply simp_all
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3828
  apply (case_tac n)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3829
   apply simp_all
57492
74bf65a1910a Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents: 56979
diff changeset
  3830
  done
74bf65a1910a Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents: 56979
diff changeset
  3831
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3832
lemma align_lem_or [rule_format] :
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3833
  "\<forall>x m. length x = n + m \<longrightarrow> length y = n + m \<longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3834
    drop m x = replicate n False \<longrightarrow> take m y = replicate m False \<longrightarrow>
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  3835
    map2 (|) x y = take m x @ drop m y"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3836
  apply (induct y)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3837
   apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3838
  apply clarsimp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3839
  apply (case_tac x)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3840
   apply force
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3841
  apply (case_tac m)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3842
   apply auto
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 59657
diff changeset
  3843
  apply (drule_tac t="length xs" for xs in sym)
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  3844
  apply (auto simp: zip_replicate o_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3845
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3846
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3847
lemma align_lem_and [rule_format] :
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3848
  "\<forall>x m. length x = n + m \<longrightarrow> length y = n + m \<longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3849
    drop m x = replicate n False \<longrightarrow> take m y = replicate m False \<longrightarrow>
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  3850
    map2 (\<and>) x y = replicate (n + m) False"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3851
  apply (induct y)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3852
   apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3853
  apply clarsimp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3854
  apply (case_tac x)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3855
   apply force
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3856
  apply (case_tac m)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3857
  apply auto
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 59657
diff changeset
  3858
  apply (drule_tac t="length xs" for xs in sym)
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  3859
  apply (auto simp: zip_replicate o_def map_replicate_const)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3860
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3861
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3862
lemma aligned_bl_add_size [OF refl]:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3863
  "size x - n = m \<Longrightarrow> n \<le> size x \<Longrightarrow> drop m (to_bl x) = replicate n False \<Longrightarrow>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3864
    take m (to_bl y) = replicate m False \<Longrightarrow>
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3865
    to_bl (x + y) = take m (to_bl x) @ drop m (to_bl y)" for x :: \<open>'a::len word\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3866
  apply (subgoal_tac "x AND y = 0")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3867
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3868
   apply (rule word_bl.Rep_eqD)
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  3869
   apply (simp add: bl_word_and)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3870
   apply (rule align_lem_and [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3871
       apply (simp_all add: word_size)[5]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3872
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3873
  apply (subst word_plus_and_or [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3874
  apply (simp add : bl_word_or)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3875
  apply (rule align_lem_or)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3876
     apply (simp_all add: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3877
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3878
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  3879
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3880
subsubsection \<open>Mask\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3881
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3882
lemma minus_1_eq_mask:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3883
  \<open>- 1 = (Bit_Operations.mask LENGTH('a) :: 'a::len word)\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3884
  by (rule bit_eqI) (simp add: bit_exp_iff bit_mask_iff exp_eq_zero_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3885
  
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3886
lemma mask_eq_mask:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3887
  \<open>mask = Bit_Operations.mask\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3888
  by (simp add: fun_eq_iff mask_eq_exp_minus_1 mask_def shiftl_word_eq push_bit_eq_mult)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3889
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3890
lemma mask_eq:
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3891
  \<open>mask n = 2 ^ n - 1\<close>
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3892
  by (simp add: mask_eq_mask mask_eq_exp_minus_1)
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3893
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3894
lemma mask_Suc_rec:
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3895
  \<open>mask (Suc n) = 2 * mask n + 1\<close>
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3896
  by (simp add: mask_eq)
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3897
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3898
context
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3899
begin
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3900
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3901
qualified lemma bit_mask_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3902
  \<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < min LENGTH('a) m\<close>
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3903
  by (simp add: mask_eq_mask bit_mask_iff exp_eq_zero_iff not_le)
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3904
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3905
end
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3906
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3907
lemma nth_mask [simp]:
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3908
  \<open>(mask n :: 'a::len word) !! i \<longleftrightarrow> i < n \<and> i < size (mask n :: 'a word)\<close>
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3909
  by (auto simp add: test_bit_word_eq word_size Word.bit_mask_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3910
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3911
lemma mask_bl: "mask n = of_bl (replicate n True)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3912
  by (auto simp add : test_bit_of_bl word_size intro: word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3913
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58061
diff changeset
  3914
lemma mask_bin: "mask n = word_of_int (bintrunc n (- 1))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3915
  by (auto simp add: nth_bintr word_size intro: word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3916
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3917
lemma and_mask_bintr: "w AND mask n = word_of_int (bintrunc n (uint w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3918
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3919
  apply (simp add: nth_bintr word_size word_ops_nth_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3920
  apply (auto simp add: test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3921
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3922
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3923
lemma and_mask_wi: "word_of_int i AND mask n = word_of_int (bintrunc n i)"
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  3924
  by (auto simp add: nth_bintr word_size word_ops_nth_size word_eq_iff)
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  3925
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3926
lemma and_mask_wi':
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3927
  "word_of_int i AND mask n = (word_of_int (bintrunc (min LENGTH('a) n) i) :: 'a::len word)"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  3928
  by (auto simp add: nth_bintr word_size word_ops_nth_size word_eq_iff)
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  3929
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3930
lemma and_mask_no: "numeral i AND mask n = word_of_int (bintrunc n (numeral i))"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3931
  unfolding word_numeral_alt by (rule and_mask_wi)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3932
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3933
lemma bl_and_mask':
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3934
  "to_bl (w AND mask n :: 'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3935
    replicate (LENGTH('a) - n) False @
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3936
    drop (LENGTH('a) - n) (to_bl w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3937
  apply (rule nth_equalityI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3938
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3939
  apply (clarsimp simp add: to_bl_nth word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3940
  apply (simp add: word_size word_ops_nth_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3941
  apply (auto simp add: word_size test_bit_bl nth_append nth_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3942
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3943
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3944
lemma and_mask_mod_2p: "w AND mask n = word_of_int (uint w mod 2 ^ n)"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3945
  by (simp only: and_mask_bintr bintrunc_mod2p)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3946
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3947
lemma and_mask_lt_2p: "uint (w AND mask n) < 2 ^ n"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3948
  apply (simp add: and_mask_bintr word_ubin.eq_norm)
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3949
  apply (simp add: bintrunc_mod2p)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3950
  apply (rule xtr8)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3951
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3952
   apply (rule pos_mod_bound)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3953
   apply auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3954
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3955
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3956
lemma eq_mod_iff: "0 < n \<Longrightarrow> b = b mod n \<longleftrightarrow> 0 \<le> b \<and> b < n"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3957
  for b n :: int
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3958
  by (simp add: int_mod_lem eq_sym_conv)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3959
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3960
lemma mask_eq_iff: "w AND mask n = w \<longleftrightarrow> uint w < 2 ^ n"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3961
  apply (simp add: and_mask_bintr)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3962
  apply (simp add: word_ubin.inverse_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3963
  apply (simp add: eq_mod_iff bintrunc_mod2p min_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3964
  apply (fast intro!: lt2p_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3965
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3966
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3967
lemma and_mask_dvd: "2 ^ n dvd uint w \<longleftrightarrow> w AND mask n = 0"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3968
  apply (simp add: dvd_eq_mod_eq_0 and_mask_mod_2p)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3969
  apply (simp add: word_uint.norm_eq_iff [symmetric] word_of_int_homs del: word_of_int_0)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3970
  apply (subst word_uint.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3971
  apply (simp only: bintrunc_bintrunc_min bintrunc_mod2p [symmetric] min_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3972
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3973
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3974
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3975
lemma and_mask_dvd_nat: "2 ^ n dvd unat w \<longleftrightarrow> w AND mask n = 0"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3976
  apply (unfold unat_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3977
  apply (rule trans [OF _ and_mask_dvd])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3978
  apply (unfold dvd_def)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3979
  apply auto
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3980
   apply (drule uint_ge_0 [THEN nat_int.Abs_inverse' [simplified], symmetric])
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3981
   apply simp
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3982
  apply (simp add: nat_mult_distrib nat_power_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3983
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3984
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3985
lemma word_2p_lem: "n < size w \<Longrightarrow> w < 2 ^ n = (uint w < 2 ^ n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3986
  for w :: "'a::len word"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3987
  apply (unfold word_size word_less_alt word_numeral_alt)
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
  3988
  apply (auto simp add: word_of_int_power_hom word_uint.eq_norm
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3989
      simp del: word_of_int_numeral)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3990
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3991
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3992
lemma less_mask_eq: "x < 2 ^ n \<Longrightarrow> x AND mask n = x"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3993
  for x :: "'a::len word"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3994
  apply (unfold word_less_alt word_numeral_alt)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3995
  apply (clarsimp simp add: and_mask_mod_2p word_of_int_power_hom word_uint.eq_norm
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3996
      simp del: word_of_int_numeral)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3997
  apply (drule xtr8 [rotated])
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3998
   apply (rule int_mod_le)
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  3999
   apply simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4000
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4001
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4002
lemmas mask_eq_iff_w2p = trans [OF mask_eq_iff word_2p_lem [symmetric]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4003
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4004
lemmas and_mask_less' = iffD2 [OF word_2p_lem and_mask_lt_2p, simplified word_size]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4005
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4006
lemma and_mask_less_size: "n < size x \<Longrightarrow> x AND mask n < 2^n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4007
  unfolding word_size by (erule and_mask_less')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4008
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4009
lemma word_mod_2p_is_mask [OF refl]: "c = 2 ^ n \<Longrightarrow> c > 0 \<Longrightarrow> x mod c = x AND mask n"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4010
  for c x :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4011
  by (auto simp: word_mod_def uint_2p and_mask_mod_2p)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4012
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4013
lemma mask_eqs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4014
  "(a AND mask n) + b AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4015
  "a + (b AND mask n) AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4016
  "(a AND mask n) - b AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4017
  "a - (b AND mask n) AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4018
  "a * (b AND mask n) AND mask n = a * b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4019
  "(b AND mask n) * a AND mask n = b * a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4020
  "(a AND mask n) + (b AND mask n) AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4021
  "(a AND mask n) - (b AND mask n) AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4022
  "(a AND mask n) * (b AND mask n) AND mask n = a * b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4023
  "- (a AND mask n) AND mask n = - a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4024
  "word_succ (a AND mask n) AND mask n = word_succ a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4025
  "word_pred (a AND mask n) AND mask n = word_pred a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4026
  using word_of_int_Ex [where x=a] word_of_int_Ex [where x=b]
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4027
  by (auto simp: and_mask_wi' word_of_int_homs word.abs_eq_iff bintrunc_mod2p mod_simps)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4028
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4029
lemma mask_power_eq: "(x AND mask n) ^ k AND mask n = x ^ k AND mask n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4030
  using word_of_int_Ex [where x=x]
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4031
  by (auto simp: and_mask_wi' word_of_int_power_hom word.abs_eq_iff bintrunc_mod2p mod_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4032
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4033
lemma mask_full [simp]: "mask LENGTH('a) = (- 1 :: 'a::len word)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4034
  by (simp add: mask_def word_size shiftl_zero_size)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4035
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4036
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4037
subsubsection \<open>Revcast\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4038
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4039
lemmas revcast_def' = revcast_def [simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4040
lemmas revcast_def'' = revcast_def' [simplified word_size]
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4041
lemmas revcast_no_def [simp] = revcast_def' [where w="numeral w", unfolded word_size] for w
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4042
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4043
lemma to_bl_revcast:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4044
  "to_bl (revcast w :: 'a::len word) = takefill False (LENGTH('a)) (to_bl w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4045
  apply (unfold revcast_def' word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4046
  apply (rule word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4047
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4048
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4049
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4050
lemma revcast_rev_ucast [OF refl refl refl]:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4051
  "cs = [rc, uc] \<Longrightarrow> rc = revcast (word_reverse w) \<Longrightarrow> uc = ucast w \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4052
    rc = word_reverse uc"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4053
  apply (unfold ucast_def revcast_def' Let_def word_reverse_def)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4054
  apply (auto simp: to_bl_of_bin takefill_bintrunc)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4055
  apply (simp add: word_bl.Abs_inverse word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4056
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4057
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4058
lemma revcast_ucast: "revcast w = word_reverse (ucast (word_reverse w))"
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4059
  using revcast_rev_ucast [of "word_reverse w"] by simp
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4060
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4061
lemma ucast_revcast: "ucast w = word_reverse (revcast (word_reverse w))"
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4062
  by (fact revcast_rev_ucast [THEN word_rev_gal'])
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4063
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4064
lemma ucast_rev_revcast: "ucast (word_reverse w) = word_reverse (revcast w)"
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4065
  by (fact revcast_ucast [THEN word_rev_gal'])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4066
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4067
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4068
text "linking revcast and cast via shift"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4069
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4070
lemmas wsst_TYs = source_size target_size word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4071
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4072
lemma revcast_down_uu [OF refl]:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4073
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = ucast (w >> n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4074
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4075
  apply (simp add: revcast_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4076
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4077
  apply (rule trans, rule ucast_down_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4078
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4079
   apply (rule trans, rule drop_shiftr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4080
   apply (auto simp: takefill_alt wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4081
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4082
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4083
lemma revcast_down_us [OF refl]:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4084
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = ucast (w >>> n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4085
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4086
  apply (simp add: revcast_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4087
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4088
  apply (rule trans, rule ucast_down_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4089
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4090
   apply (rule trans, rule drop_sshiftr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4091
   apply (auto simp: takefill_alt wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4092
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4093
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4094
lemma revcast_down_su [OF refl]:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4095
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = scast (w >> n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4096
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4097
  apply (simp add: revcast_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4098
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4099
  apply (rule trans, rule scast_down_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4100
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4101
   apply (rule trans, rule drop_shiftr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4102
   apply (auto simp: takefill_alt wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4103
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4104
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4105
lemma revcast_down_ss [OF refl]:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4106
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = scast (w >>> n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4107
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4108
  apply (simp add: revcast_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4109
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4110
  apply (rule trans, rule scast_down_drop)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4111
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4112
   apply (rule trans, rule drop_sshiftr)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4113
   apply (auto simp: takefill_alt wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4114
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4115
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4116
(* FIXME: should this also be [OF refl] ? *)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4117
lemma cast_down_rev:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4118
  "uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> uc w = revcast (w << n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4119
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4120
  apply (unfold shiftl_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4121
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4122
  apply (simp add: revcast_rev_ucast)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4123
  apply (rule word_rev_gal')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4124
  apply (rule trans [OF _ revcast_rev_ucast])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4125
  apply (rule revcast_down_uu [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4126
  apply (auto simp add: wsst_TYs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4127
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4128
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4129
lemma revcast_up [OF refl]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4130
  "rc = revcast \<Longrightarrow> source_size rc + n = target_size rc \<Longrightarrow>
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4131
    rc w = (ucast w :: 'a::len word) << n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4132
  apply (simp add: revcast_def')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4133
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4134
  apply (simp add: takefill_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4135
  apply (rule bl_shiftl [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4136
  apply (subst ucast_up_app)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4137
   apply (auto simp add: wsst_TYs)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4138
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4139
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4140
lemmas rc1 = revcast_up [THEN
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4141
  revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]]
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4142
lemmas rc2 = revcast_down_uu [THEN
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4143
  revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4144
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4145
lemmas ucast_up =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4146
 rc1 [simplified rev_shiftr [symmetric] revcast_ucast [symmetric]]
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4147
lemmas ucast_down =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4148
  rc2 [simplified rev_shiftr revcast_ucast [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4149
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4150
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4151
subsubsection \<open>Slices\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4152
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4153
lemma slice1_no_bin [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4154
  "slice1 n (numeral w :: 'b word) = of_bl (takefill False n (bin_to_bl (LENGTH('b::len)) (numeral w)))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4155
  by (simp add: slice1_def) (* TODO: neg_numeral *)
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4156
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4157
lemma slice_no_bin [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4158
  "slice n (numeral w :: 'b word) = of_bl (takefill False (LENGTH('b::len) - n)
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4159
    (bin_to_bl (LENGTH('b::len)) (numeral w)))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4160
  by (simp add: slice_def word_size) (* TODO: neg_numeral *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4161
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4162
lemma slice1_0 [simp] : "slice1 n 0 = 0"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  4163
  unfolding slice1_def by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4164
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4165
lemma slice_0 [simp] : "slice n 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4166
  unfolding slice_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4167
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4168
lemma slice_take': "slice n w = of_bl (take (size w - n) (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4169
  unfolding slice_def' slice1_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4170
  by (simp add : takefill_alt word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4171
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4172
lemmas slice_take = slice_take' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4173
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
  4174
\<comment> \<open>shiftr to a word of the same size is just slice,
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
  4175
    slice is just shiftr then ucast\<close>
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4176
lemmas shiftr_slice = trans [OF shiftr_bl [THEN meta_eq_to_obj_eq] slice_take [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4177
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4178
lemma slice_shiftr: "slice n w = ucast (w >> n)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4179
  apply (unfold slice_take shiftr_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4180
  apply (rule ucast_of_bl_up [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4181
  apply (simp add: word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4182
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4183
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4184
lemma nth_slice: "(slice n w :: 'a::len word) !! m = (w !! (m + n) \<and> m < LENGTH('a))"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4185
  by (simp add: slice_shiftr nth_ucast nth_shiftr)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4186
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4187
lemma slice1_down_alt':
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4188
  "sl = slice1 n w \<Longrightarrow> fs = size sl \<Longrightarrow> fs + k = n \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4189
    to_bl sl = takefill False fs (drop k (to_bl w))"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4190
  by (auto simp: slice1_def word_size of_bl_def uint_bl
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4191
      word_ubin.eq_norm bl_bin_bl_rep_drop drop_takefill)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4192
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4193
lemma slice1_up_alt':
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4194
  "sl = slice1 n w \<Longrightarrow> fs = size sl \<Longrightarrow> fs = n + k \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4195
    to_bl sl = takefill False fs (replicate k False @ (to_bl w))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4196
  apply (unfold slice1_def word_size of_bl_def uint_bl)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4197
  apply (clarsimp simp: word_ubin.eq_norm bl_bin_bl_rep_drop takefill_append [symmetric])
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4198
  apply (rule_tac f = "\<lambda>k. takefill False (LENGTH('a))
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4199
    (replicate k False @ bin_to_bl (LENGTH('b)) (uint w))" in arg_cong)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4200
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4201
  done
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4202
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4203
lemmas sd1 = slice1_down_alt' [OF refl refl, unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4204
lemmas su1 = slice1_up_alt' [OF refl refl, unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4205
lemmas slice1_down_alt = le_add_diff_inverse [THEN sd1]
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4206
lemmas slice1_up_alts =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4207
  le_add_diff_inverse [symmetric, THEN su1]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4208
  le_add_diff_inverse2 [symmetric, THEN su1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4209
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4210
lemma ucast_slice1: "ucast w = slice1 (size w) w"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4211
  by (simp add: slice1_def ucast_bl takefill_same' word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4212
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4213
lemma ucast_slice: "ucast w = slice 0 w"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4214
  by (simp add: slice_def ucast_slice1)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4215
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4216
lemma slice_id: "slice 0 t = t"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4217
  by (simp only: ucast_slice [symmetric] ucast_id)
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4218
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4219
lemma revcast_slice1 [OF refl]: "rc = revcast w \<Longrightarrow> slice1 (size rc) w = rc"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4220
  by (simp add: slice1_def revcast_def' word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4221
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4222
lemma slice1_tf_tf':
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4223
  "to_bl (slice1 n w :: 'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4224
    rev (takefill False (LENGTH('a)) (rev (takefill False n (to_bl w))))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4225
  unfolding slice1_def by (rule word_rev_tf)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4226
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4227
lemmas slice1_tf_tf = slice1_tf_tf' [THEN word_bl.Rep_inverse', symmetric]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4228
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4229
lemma rev_slice1:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4230
  "n + k = LENGTH('a) + LENGTH('b) \<Longrightarrow>
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4231
    slice1 n (word_reverse w :: 'b::len word) =
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4232
    word_reverse (slice1 k w :: 'a::len word)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4233
  apply (unfold word_reverse_def slice1_tf_tf)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4234
  apply (rule word_bl.Rep_inverse')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4235
  apply (rule rev_swap [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4236
  apply (rule trans [symmetric])
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4237
   apply (rule tf_rev)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4238
   apply (simp add: word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4239
  apply (simp add: word_bl.Abs_inverse)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4240
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4241
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4242
lemma rev_slice:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4243
  "n + k + LENGTH('a::len) = LENGTH('b::len) \<Longrightarrow>
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4244
    slice n (word_reverse (w::'b word)) = word_reverse (slice k w :: 'a word)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4245
  apply (unfold slice_def word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4246
  apply (rule rev_slice1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4247
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4248
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4249
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4250
lemmas sym_notr =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4251
  not_iff [THEN iffD2, THEN not_sym, THEN not_iff [THEN iffD1]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4252
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4253
\<comment> \<open>problem posed by TPHOLs referee:
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4254
      criterion for overflow of addition of signed integers\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4255
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4256
lemma sofl_test:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4257
  "(sint x + sint y = sint (x + y)) =
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4258
     ((((x + y) XOR x) AND ((x + y) XOR y)) >> (size x - 1) = 0)"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4259
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4260
  apply (unfold word_size)
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4261
  apply (cases "LENGTH('a)", simp)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4262
  apply (subst msb_shift [THEN sym_notr])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4263
  apply (simp add: word_ops_msb)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4264
  apply (simp add: word_msb_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4265
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4266
       apply simp_all
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4267
     apply (unfold sint_word_ariths)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4268
     apply (unfold word_sbin.set_iff_norm [symmetric] sints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4269
     apply safe
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4270
         apply (insert sint_range' [where x=x])
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4271
         apply (insert sint_range' [where x=y])
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4272
         defer
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4273
         apply (simp (no_asm), arith)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4274
        apply (simp (no_asm), arith)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4275
       defer
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4276
       defer
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4277
       apply (simp (no_asm), arith)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4278
      apply (simp (no_asm), arith)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4279
     apply (rule notI [THEN notnotD],
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4280
      drule leI not_le_imp_less,
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4281
      drule sbintrunc_inc sbintrunc_dec,
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4282
      simp)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4283
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4284
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4285
lemma shiftr_zero_size: "size x \<le> n \<Longrightarrow> x >> n = 0"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4286
  for x :: "'a :: len word"
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4287
  by (rule word_eqI) (auto simp add: nth_shiftr dest: test_bit_size)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4288
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4289
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4290
subsection \<open>Split and cat\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4291
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4292
lemmas word_split_bin' = word_split_def
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4293
lemmas word_cat_bin' = word_cat_def
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4294
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4295
lemma word_rsplit_no:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4296
  "(word_rsplit (numeral bin :: 'b::len word) :: 'a word list) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4297
    map word_of_int (bin_rsplit (LENGTH('a::len))
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4298
      (LENGTH('b), bintrunc (LENGTH('b)) (numeral bin)))"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4299
  by (simp add: word_rsplit_def word_ubin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4300
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4301
lemmas word_rsplit_no_cl [simp] = word_rsplit_no
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4302
  [unfolded bin_rsplitl_def bin_rsplit_l [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4303
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4304
lemma test_bit_cat:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4305
  "wc = word_cat a b \<Longrightarrow> wc !! n = (n < size wc \<and>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4306
    (if n < size b then b !! n else a !! (n - size b)))"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4307
  apply (auto simp: word_cat_bin' test_bit_bin word_ubin.eq_norm nth_bintr bin_nth_cat word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4308
  apply (erule bin_nth_uint_imp)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4309
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4310
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4311
lemma word_cat_bl: "word_cat a b = of_bl (to_bl a @ to_bl b)"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4312
  by (simp add: of_bl_def to_bl_def word_cat_bin' bl_to_bin_app_cat)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4313
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4314
lemma of_bl_append:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4315
  "(of_bl (xs @ ys) :: 'a::len word) = of_bl xs * 2^(length ys) + of_bl ys"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4316
  apply (simp add: of_bl_def bl_to_bin_app_cat bin_cat_num)
46009
5cb7ef5bfef2 remove duplicate lemma lists
huffman
parents: 46001
diff changeset
  4317
  apply (simp add: word_of_int_power_hom [symmetric] word_of_int_hom_syms)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4318
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4319
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4320
lemma of_bl_False [simp]: "of_bl (False#xs) = of_bl xs"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4321
  by (rule word_eqI) (auto simp: test_bit_of_bl nth_append)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4322
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4323
lemma of_bl_True [simp]: "(of_bl (True # xs) :: 'a::len word) = 2^length xs + of_bl xs"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4324
  by (subst of_bl_append [where xs="[True]", simplified]) (simp add: word_1_bl)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4325
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4326
lemma of_bl_Cons: "of_bl (x#xs) = of_bool x * 2^length xs + of_bl xs"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  4327
  by (cases x) simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4328
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4329
lemma split_uint_lem: "bin_split n (uint w) = (a, b) \<Longrightarrow>
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4330
    a = bintrunc (LENGTH('a) - n) a \<and> b = bintrunc (LENGTH('a)) b"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4331
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4332
  apply (frule word_ubin.norm_Rep [THEN ssubst])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4333
  apply (drule bin_split_trunc1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4334
  apply (drule sym [THEN trans])
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4335
   apply assumption
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4336
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4337
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4338
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4339
lemma word_split_bl':
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4340
  "std = size c - size b \<Longrightarrow> (word_split c = (a, b)) \<Longrightarrow>
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4341
    (a = of_bl (take std (to_bl c)) \<and> b = of_bl (drop std (to_bl c)))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4342
  apply (unfold word_split_bin')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4343
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4344
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4345
   apply (clarsimp split: prod.splits)
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
  4346
  apply (metis of_bl_drop' ucast_bl ucast_def word_size word_size_bl)
57492
74bf65a1910a Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents: 56979
diff changeset
  4347
   apply hypsubst_thin
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4348
   apply (drule word_ubin.norm_Rep [THEN ssubst])
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4349
   apply (simp add: of_bl_def bl2bin_drop word_size
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4350
      word_ubin.norm_eq_iff [symmetric] min_def del: word_ubin.norm_Rep)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4351
  apply (clarsimp split: prod.splits)
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4352
  apply (cases "LENGTH('a) \<ge> LENGTH('b)")
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71942
diff changeset
  4353
   apply (simp_all add: not_le)
18357df1cd20 avoid compound operation
haftmann
parents: 71942
diff changeset
  4354
  defer
18357df1cd20 avoid compound operation
haftmann
parents: 71942
diff changeset
  4355
   apply (simp add: drop_bit_eq_div lt2p_lem)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4356
  apply (simp add : of_bl_def to_bl_def)
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71942
diff changeset
  4357
  apply (subst bin_to_bl_drop_bit [symmetric])
18357df1cd20 avoid compound operation
haftmann
parents: 71942
diff changeset
  4358
   apply (subst diff_add)
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
  4359
    apply (simp_all add: take_bit_drop_bit)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4360
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4361
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4362
lemma word_split_bl: "std = size c - size b \<Longrightarrow>
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4363
    (a = of_bl (take std (to_bl c)) \<and> b = of_bl (drop std (to_bl c))) \<longleftrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4364
    word_split c = (a, b)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4365
  apply (rule iffI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4366
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4367
   apply (erule (1) word_split_bl')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4368
  apply (case_tac "word_split c")
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4369
  apply (auto simp add: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4370
  apply (frule word_split_bl' [rotated])
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4371
   apply (auto simp add: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4372
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4373
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4374
lemma word_split_bl_eq:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4375
  "(word_split c :: ('c::len word \<times> 'd::len word)) =
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4376
    (of_bl (take (LENGTH('a::len) - LENGTH('d::len)) (to_bl c)),
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4377
     of_bl (drop (LENGTH('a) - LENGTH('d)) (to_bl c)))"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4378
  for c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4379
  apply (rule word_split_bl [THEN iffD1])
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4380
   apply (unfold word_size)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4381
   apply (rule refl conjI)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4382
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4383
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
  4384
\<comment> \<open>keep quantifiers for use in simplification\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4385
lemma test_bit_split':
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4386
  "word_split c = (a, b) \<longrightarrow>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4387
    (\<forall>n m.
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4388
      b !! n = (n < size b \<and> c !! n) \<and>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4389
      a !! m = (m < size a \<and> c !! (m + size b)))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4390
  apply (unfold word_split_bin' test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4391
  apply (clarify)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4392
  apply (clarsimp simp: word_ubin.eq_norm nth_bintr word_size split: prod.splits)
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71948
diff changeset
  4393
  apply (auto simp add: bit_take_bit_iff bit_drop_bit_eq ac_simps bin_nth_uint_imp)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4394
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4395
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4396
lemma test_bit_split:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4397
  "word_split c = (a, b) \<Longrightarrow>
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4398
    (\<forall>n::nat. b !! n \<longleftrightarrow> n < size b \<and> c !! n) \<and>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4399
    (\<forall>m::nat. a !! m \<longleftrightarrow> m < size a \<and> c !! (m + size b))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4400
  by (simp add: test_bit_split')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4401
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4402
lemma test_bit_split_eq:
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4403
  "word_split c = (a, b) \<longleftrightarrow>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4404
    ((\<forall>n::nat. b !! n = (n < size b \<and> c !! n)) \<and>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4405
     (\<forall>m::nat. a !! m = (m < size a \<and> c !! (m + size b))))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4406
  apply (rule_tac iffI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4407
   apply (rule_tac conjI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4408
    apply (erule test_bit_split [THEN conjunct1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4409
   apply (erule test_bit_split [THEN conjunct2])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4410
  apply (case_tac "word_split c")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4411
  apply (frule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4412
  apply (erule trans)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4413
  apply (fastforce intro!: word_eqI simp add: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4414
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4415
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4416
\<comment> \<open>this odd result is analogous to \<open>ucast_id\<close>,
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4417
      result to the length given by the result type\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4418
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4419
lemma word_cat_id: "word_cat a b = b"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4420
  by (simp add: word_cat_bin' word_ubin.inverse_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4421
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
  4422
\<comment> \<open>limited hom result\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4423
lemma word_cat_hom:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4424
  "LENGTH('a::len) \<le> LENGTH('b::len) + LENGTH('c::len) \<Longrightarrow>
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4425
    (word_cat (word_of_int w :: 'b word) (b :: 'c word) :: 'a word) =
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4426
    word_of_int (bin_cat w (size b) (uint b))"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4427
  by (auto simp: word_cat_def word_size word_ubin.norm_eq_iff [symmetric]
54863
82acc20ded73 prefer more canonical names for lemmas on min/max
haftmann
parents: 54854
diff changeset
  4428
      word_ubin.eq_norm bintr_cat min.absorb1)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4429
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4430
lemma word_cat_split_alt: "size w \<le> size u + size v \<Longrightarrow> word_split w = (u, v) \<Longrightarrow> word_cat u v = w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4431
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4432
  apply (drule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4433
  apply (clarsimp simp add : test_bit_cat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4434
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4435
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4436
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4437
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4438
lemmas word_cat_split_size = sym [THEN [2] word_cat_split_alt [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4439
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4440
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4441
subsubsection \<open>Split and slice\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4442
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4443
lemma split_slices: "word_split w = (u, v) \<Longrightarrow> u = slice (size v) w \<and> v = slice 0 w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4444
  apply (drule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4445
  apply (rule conjI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4446
   apply (rule word_eqI, clarsimp simp: nth_slice word_size)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4447
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4448
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4449
lemma slice_cat1 [OF refl]:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4450
  "wc = word_cat a b \<Longrightarrow> size wc >= size a + size b \<Longrightarrow> slice (size b) wc = a"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4451
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4452
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4453
  apply (simp add: nth_slice test_bit_cat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4454
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4455
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4456
lemmas slice_cat2 = trans [OF slice_id word_cat_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4457
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4458
lemma cat_slices:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4459
  "a = slice n c \<Longrightarrow> b = slice 0 c \<Longrightarrow> n = size b \<Longrightarrow>
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4460
    size a + size b >= size c \<Longrightarrow> word_cat a b = c"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4461
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4462
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4463
  apply (simp add: nth_slice test_bit_cat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4464
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4465
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4466
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4467
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4468
lemma word_split_cat_alt:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4469
  "w = word_cat u v \<Longrightarrow> size u + size v \<le> size w \<Longrightarrow> word_split w = (u, v)"
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 59657
diff changeset
  4470
  apply (case_tac "word_split w")
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4471
  apply (rule trans, assumption)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4472
  apply (drule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4473
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4474
   apply (rule word_eqI, clarsimp simp: test_bit_cat word_size)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4475
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4476
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4477
lemmas word_cat_bl_no_bin [simp] =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4478
  word_cat_bl [where a="numeral a" and b="numeral b", unfolded to_bl_numeral]
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4479
  for a b (* FIXME: negative numerals, 0 and 1 *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4480
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4481
lemmas word_split_bl_no_bin [simp] =
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4482
  word_split_bl_eq [where c="numeral c", unfolded to_bl_numeral] for c
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4483
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4484
text \<open>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4485
  This odd result arises from the fact that the statement of the
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4486
  result implies that the decoded words are of the same type,
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4487
  and therefore of the same length, as the original word.\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4488
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4489
lemma word_rsplit_same: "word_rsplit w = [w]"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4490
  by (simp add: word_rsplit_def bin_rsplit_all)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4491
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4492
lemma word_rsplit_empty_iff_size: "word_rsplit w = [] \<longleftrightarrow> size w = 0"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4493
  by (simp add: word_rsplit_def bin_rsplit_def word_size bin_rsplit_aux_simp_alt Let_def
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4494
      split: prod.split)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4495
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4496
lemma test_bit_rsplit:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4497
  "sw = word_rsplit w \<Longrightarrow> m < size (hd sw) \<Longrightarrow>
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4498
    k < length sw \<Longrightarrow> (rev sw ! k) !! m = w !! (k * size (hd sw) + m)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4499
  for sw :: "'a::len word list"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4500
  apply (unfold word_rsplit_def word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4501
  apply (rule trans)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4502
   apply (rule_tac f = "\<lambda>x. bin_nth x m" in arg_cong)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4503
   apply (rule nth_map [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4504
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4505
  apply (rule bin_nth_rsplit)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4506
     apply simp_all
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4507
  apply (simp add : word_size rev_map)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4508
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4509
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4510
   apply (rule map_ident [THEN fun_cong])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4511
  apply (rule refl [THEN map_cong])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4512
  apply (simp add : word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4513
  apply (erule bin_rsplit_size_sign [OF len_gt_0 refl])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4514
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4515
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4516
lemma word_rcat_bl: "word_rcat wl = of_bl (concat (map to_bl wl))"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4517
  by (auto simp: word_rcat_def to_bl_def' of_bl_def bin_rcat_bl)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4518
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4519
lemma size_rcat_lem': "size (concat (map to_bl wl)) = length wl * size (hd wl)"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4520
  by (induct wl) (auto simp: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4521
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4522
lemmas size_rcat_lem = size_rcat_lem' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4523
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4524
lemmas td_gal_lt_len = len_gt_0 [THEN td_gal_lt]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4525
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4526
lemma nth_rcat_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4527
  "n < length (wl::'a word list) * LENGTH('a::len) \<Longrightarrow>
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4528
    rev (concat (map to_bl wl)) ! n =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4529
    rev (to_bl (rev wl ! (n div LENGTH('a)))) ! (n mod LENGTH('a))"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4530
  apply (induct wl)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4531
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4532
  apply (clarsimp simp add : nth_append size_rcat_lem)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4533
  apply (simp (no_asm_use) only:  mult_Suc [symmetric]
64243
aee949f6642d eliminated irregular aliasses
haftmann
parents: 64242
diff changeset
  4534
         td_gal_lt_len less_Suc_eq_le minus_div_mult_eq_mod [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4535
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4536
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4537
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4538
lemma test_bit_rcat:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4539
  "sw = size (hd wl) \<Longrightarrow> rc = word_rcat wl \<Longrightarrow> rc !! n =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4540
    (n < size rc \<and> n div sw < size wl \<and> (rev wl) ! (n div sw) !! (n mod sw))"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4541
  for wl :: "'a::len word list"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4542
  apply (unfold word_rcat_bl word_size)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4543
  apply (clarsimp simp add: test_bit_of_bl size_rcat_lem word_size td_gal_lt_len)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4544
  apply safe
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4545
   apply (auto simp: test_bit_bl word_size td_gal_lt_len [THEN iffD2, THEN nth_rcat_lem])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4546
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4547
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  4548
lemma foldl_eq_foldr: "foldl (+) x xs = foldr (+) (x # xs) 0"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4549
  for x :: "'a::comm_monoid_add"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4550
  by (induct xs arbitrary: x) (auto simp: add.assoc)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4551
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4552
lemmas test_bit_cong = arg_cong [where f = "test_bit", THEN fun_cong]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4553
71996
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4554
lemma test_bit_rsplit_alt:
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4555
  \<open>(word_rsplit w  :: 'b::len word list) ! i !! m \<longleftrightarrow>
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4556
    w !! ((length (word_rsplit w :: 'b::len word list) - Suc i) * size (hd (word_rsplit w :: 'b::len word list)) + m)\<close>
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4557
  if \<open>i < length (word_rsplit w :: 'b::len word list)\<close> \<open>m < size (hd (word_rsplit w :: 'b::len word list))\<close> \<open>0 < length (word_rsplit w :: 'b::len word list)\<close>
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4558
  for w :: \<open>'a::len word\<close>
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4559
  apply (rule trans)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4560
   apply (rule test_bit_cong)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4561
   apply (rule nth_rev_alt)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4562
   apply (rule that(1))
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4563
  apply (rule test_bit_rsplit)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4564
    apply (rule refl)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4565
  apply (rule asm_rl)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4566
   apply (rule that(2))
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4567
  apply (rule diff_Suc_less)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4568
  apply (rule that(3))
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4569
  done
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4570
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4571
lemma word_rsplit_len_indep [OF refl refl refl refl]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4572
  "[u,v] = p \<Longrightarrow> [su,sv] = q \<Longrightarrow> word_rsplit u = su \<Longrightarrow>
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4573
    word_rsplit v = sv \<Longrightarrow> length su = length sv"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4574
  by (auto simp: word_rsplit_def bin_rsplit_len_indep)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4575
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4576
lemma length_word_rsplit_size:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4577
  "n = LENGTH('a::len) \<Longrightarrow>
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4578
    length (word_rsplit w :: 'a word list) \<le> m \<longleftrightarrow> size w \<le> m * n"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4579
  by (auto simp: word_rsplit_def word_size bin_rsplit_len_le)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4580
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4581
lemmas length_word_rsplit_lt_size =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4582
  length_word_rsplit_size [unfolded Not_eq_iff linorder_not_less [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4583
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4584
lemma length_word_rsplit_exp_size:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4585
  "n = LENGTH('a::len) \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4586
    length (word_rsplit w :: 'a word list) = (size w + n - 1) div n"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4587
  by (auto simp: word_rsplit_def word_size bin_rsplit_len)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4588
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4589
lemma length_word_rsplit_even_size:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4590
  "n = LENGTH('a::len) \<Longrightarrow> size w = m * n \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4591
    length (word_rsplit w :: 'a word list) = m"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4592
  by (auto simp: length_word_rsplit_exp_size given_quot_alt)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4593
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4594
lemmas length_word_rsplit_exp_size' = refl [THEN length_word_rsplit_exp_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4595
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  4596
\<comment> \<open>alternative proof of \<open>word_rcat_rsplit\<close>\<close>
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66453
diff changeset
  4597
lemmas tdle = times_div_less_eq_dividend
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  4598
lemmas dtle = xtr4 [OF tdle mult.commute]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4599
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4600
lemma word_rcat_rsplit: "word_rcat (word_rsplit w) = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4601
  apply (rule word_eqI)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4602
  apply (clarsimp simp: test_bit_rcat word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4603
  apply (subst refl [THEN test_bit_rsplit])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4604
    apply (simp_all add: word_size
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4605
      refl [THEN length_word_rsplit_size [simplified not_less [symmetric], simplified]])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4606
   apply safe
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66453
diff changeset
  4607
   apply (erule xtr7, rule dtle)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4608
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4609
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4610
lemma size_word_rsplit_rcat_size:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4611
  "word_rcat ws = frcw \<Longrightarrow> size frcw = length ws * LENGTH('a)
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4612
    \<Longrightarrow> length (word_rsplit frcw::'a word list) = length ws"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  4613
  for ws :: "'a::len word list" and frcw :: "'b::len word"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4614
  apply (clarsimp simp: word_size length_word_rsplit_exp_size')
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4615
  apply (fast intro: given_quot_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4616
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4617
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4618
lemma msrevs:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4619
  "0 < n \<Longrightarrow> (k * n + m) div n = m div n + k"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4620
  "(k * n + m) mod n = m mod n"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4621
  for n :: nat
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  4622
  by (auto simp: add.commute)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4623
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4624
lemma word_rsplit_rcat_size [OF refl]:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4625
  "word_rcat ws = frcw \<Longrightarrow>
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4626
    size frcw = length ws * LENGTH('a) \<Longrightarrow> word_rsplit frcw = ws"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4627
  for ws :: "'a::len word list"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4628
  apply (frule size_word_rsplit_rcat_size, assumption)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4629
  apply (clarsimp simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4630
  apply (rule nth_equalityI, assumption)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4631
  apply clarsimp
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  4632
  apply (rule word_eqI [rule_format])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4633
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4634
   apply (rule test_bit_rsplit_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4635
     apply (clarsimp simp: word_size)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4636
  apply (rule trans)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4637
   apply (rule test_bit_rcat [OF refl refl])
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  4638
  apply (simp add: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4639
  apply (subst nth_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4640
   apply arith
41550
efa734d9b221 eliminated global prems;
wenzelm
parents: 41413
diff changeset
  4641
  apply (simp add: le0 [THEN [2] xtr7, THEN diff_Suc_less])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4642
  apply safe
41550
efa734d9b221 eliminated global prems;
wenzelm
parents: 41413
diff changeset
  4643
  apply (simp add: diff_mult_distrib)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4644
  apply (rule mpl_lem)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4645
   apply (cases "size ws")
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4646
    apply simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4647
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4648
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4649
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4650
subsection \<open>Rotation\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4651
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4652
lemmas rotater_0' [simp] = rotater_def [where n = "0", simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4653
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4654
lemma bit_word_rotl_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4655
  \<open>bit (word_rotl m w) n \<longleftrightarrow>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4656
    n < LENGTH('a) \<and> bit w ((n + (LENGTH('a) - m mod LENGTH('a))) mod LENGTH('a))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4657
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4658
proof (cases \<open>n < LENGTH('a)\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4659
  case False
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4660
  then show ?thesis
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4661
    by (auto dest: bit_imp_le_length)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4662
next
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4663
  case True
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4664
  define k where \<open>k = int n - int m\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4665
  then have k: \<open>int n = k + int m\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4666
    by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4667
  define l where \<open>l = int LENGTH('a)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4668
  then have l: \<open>int LENGTH('a) = l\<close> \<open>l > 0\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4669
    by simp_all
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4670
  have *: \<open>int (m - n) = int m - int n\<close> if \<open>n \<le> m\<close> for n m
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4671
    using that by (simp add: int_minus)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4672
  from \<open>l > 0\<close> have \<open>l = 1 + (k mod l + ((- 1 - k) mod l))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4673
    using minus_mod_int_eq [of l \<open>k + 1\<close>] by (simp add: algebra_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4674
  then have \<open>int (LENGTH('a) - Suc ((m + LENGTH('a) - Suc n) mod LENGTH('a))) =
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4675
    int ((n + LENGTH('a) - m mod LENGTH('a)) mod LENGTH('a))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4676
    by (simp add: * k l zmod_int Suc_leI trans_le_add2 algebra_simps mod_simps \<open>n < LENGTH('a)\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4677
  then have \<open>LENGTH('a) - Suc ((m + LENGTH('a) - Suc n) mod LENGTH('a)) =
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4678
    (n + LENGTH('a) - m mod LENGTH('a)) mod LENGTH('a)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4679
    by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4680
  with True show ?thesis
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4681
    by (simp add: word_rotl_def bit_of_bl_iff rev_nth nth_rotate nth_to_bl)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4682
qed
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4683
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4684
lemmas word_rot_defs = word_roti_def word_rotr_def word_rotl_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4685
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4686
lemma rotate_eq_mod: "m mod length xs = n mod length xs \<Longrightarrow> rotate m xs = rotate n xs"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4687
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4688
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4689
    apply (rule rotate_conv_mod [symmetric])+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4690
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4691
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4692
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4693
lemmas rotate_eqs =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4694
  trans [OF rotate0 [THEN fun_cong] id_apply]
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4695
  rotate_rotate [symmetric]
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4696
  rotate_id
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4697
  rotate_conv_mod
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4698
  rotate_eq_mod
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4699
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4700
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4701
subsubsection \<open>Rotation of list to right\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4702
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4703
lemma rotate1_rl': "rotater1 (l @ [a]) = a # l"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4704
  by (cases l) (auto simp: rotater1_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4705
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4706
lemma rotate1_rl [simp] : "rotater1 (rotate1 l) = l"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4707
  apply (unfold rotater1_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4708
  apply (cases "l")
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4709
   apply (case_tac [2] "list")
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4710
    apply auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4711
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4712
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4713
lemma rotate1_lr [simp] : "rotate1 (rotater1 l) = l"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4714
  by (cases l) (auto simp: rotater1_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4715
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4716
lemma rotater1_rev': "rotater1 (rev xs) = rev (rotate1 xs)"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4717
  by (cases "xs") (simp add: rotater1_def, simp add: rotate1_rl')
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4718
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4719
lemma rotater_rev': "rotater n (rev xs) = rev (rotate n xs)"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4720
  by (induct n) (auto simp: rotater_def intro: rotater1_rev')
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4721
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4722
lemma rotater_rev: "rotater n ys = rev (rotate n (rev ys))"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4723
  using rotater_rev' [where xs = "rev ys"] by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4724
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4725
lemma rotater_drop_take:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4726
  "rotater n xs =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4727
    drop (length xs - n mod length xs) xs @
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4728
    take (length xs - n mod length xs) xs"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4729
  by (auto simp: rotater_rev rotate_drop_take rev_take rev_drop)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4730
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4731
lemma rotater_Suc [simp]: "rotater (Suc n) xs = rotater1 (rotater n xs)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4732
  unfolding rotater_def by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4733
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4734
lemma nth_rotater:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4735
  \<open>rotater m xs ! n = xs ! ((n + (length xs - m mod length xs)) mod length xs)\<close> if \<open>n < length xs\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4736
  using that by (simp add: rotater_drop_take nth_append not_less less_diff_conv ac_simps le_mod_geq)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4737
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4738
lemma nth_rotater1:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4739
  \<open>rotater1 xs ! n = xs ! ((n + (length xs - 1)) mod length xs)\<close> if \<open>n < length xs\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4740
  using that nth_rotater [of n xs 1] by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4741
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4742
lemma rotate_inv_plus [rule_format]:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4743
  "\<forall>k. k = m + n \<longrightarrow> rotater k (rotate n xs) = rotater m xs \<and>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4744
    rotate k (rotater n xs) = rotate m xs \<and>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4745
    rotater n (rotate k xs) = rotate m xs \<and>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4746
    rotate n (rotater k xs) = rotater m xs"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4747
  by (induct n) (auto simp: rotater_def rotate_def intro: funpow_swap1 [THEN trans])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4748
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4749
lemmas rotate_inv_rel = le_add_diff_inverse2 [symmetric, THEN rotate_inv_plus]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4750
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4751
lemmas rotate_inv_eq = order_refl [THEN rotate_inv_rel, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4752
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4753
lemmas rotate_lr [simp] = rotate_inv_eq [THEN conjunct1]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4754
lemmas rotate_rl [simp] = rotate_inv_eq [THEN conjunct2, THEN conjunct1]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4755
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4756
lemma rotate_gal: "rotater n xs = ys \<longleftrightarrow> rotate n ys = xs"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4757
  by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4758
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4759
lemma rotate_gal': "ys = rotater n xs \<longleftrightarrow> xs = rotate n ys"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4760
  by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4761
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4762
lemma length_rotater [simp]: "length (rotater n xs) = length xs"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4763
  by (simp add : rotater_rev)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4764
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4765
lemma bit_word_rotr_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4766
  \<open>bit (word_rotr m w) n \<longleftrightarrow>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4767
    n < LENGTH('a) \<and> bit w ((n + m) mod LENGTH('a))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4768
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4769
proof (cases \<open>n < LENGTH('a)\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4770
  case False
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4771
  then show ?thesis
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4772
    by (auto dest: bit_imp_le_length)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4773
next
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4774
  case True
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4775
  define k where \<open>k = int n + int m\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4776
  then have k: \<open>int n = k - int m\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4777
    by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4778
  define l where \<open>l = int LENGTH('a)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4779
  then have l: \<open>int LENGTH('a) = l\<close> \<open>l > 0\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4780
    by simp_all
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4781
  have *: \<open>int (m - n) = int m - int n\<close> if \<open>n \<le> m\<close> for n m
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4782
    using that by (simp add: int_minus)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4783
  have \<open>int ((LENGTH('a)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4784
    - Suc ((LENGTH('a) + LENGTH('a) - Suc (n + m mod LENGTH('a))) mod LENGTH('a)))) =
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4785
    int ((n + m) mod LENGTH('a))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4786
    using True
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4787
    apply (simp add: l * zmod_int Suc_leI add_strict_mono)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4788
    apply (subst mod_diff_left_eq [symmetric])
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4789
    apply simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4790
    using l minus_mod_int_eq [of l \<open>int n + int m mod l + 1\<close>] apply simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4791
    apply (simp add: mod_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4792
    done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4793
  then have \<open>(LENGTH('a)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4794
    - Suc ((LENGTH('a) + LENGTH('a) - Suc (n + m mod LENGTH('a))) mod LENGTH('a))) =
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4795
    ((n + m) mod LENGTH('a))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4796
    by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4797
  with True show ?thesis
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4798
    by (simp add: word_rotr_def bit_of_bl_iff rev_nth nth_rotater nth_to_bl)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4799
qed
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4800
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4801
lemma bit_word_roti_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4802
  \<open>bit (word_roti k w) n \<longleftrightarrow>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4803
    n < LENGTH('a) \<and> bit w (nat ((int n + k) mod int LENGTH('a)))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4804
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4805
proof (cases \<open>k \<ge> 0\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4806
  case True
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4807
  moreover define m where \<open>m = nat k\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4808
  ultimately have \<open>k = int m\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4809
    by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4810
  then show ?thesis
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4811
    by (simp add: word_roti_def bit_word_rotr_iff nat_mod_distrib nat_add_distrib)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4812
next
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4813
  have *: \<open>int (m - n) = int m - int n\<close> if \<open>n \<le> m\<close> for n m
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4814
    using that by (simp add: int_minus)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4815
  case False
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4816
  moreover define m where \<open>m = nat (- k)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4817
  ultimately have \<open>k = - int m\<close> \<open>k < 0\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4818
    by simp_all
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4819
  moreover have \<open>(int n - int m) mod int LENGTH('a) =
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4820
    int ((n + LENGTH('a) - m mod LENGTH('a)) mod LENGTH('a))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4821
    apply (simp add: zmod_int * trans_le_add2 mod_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4822
    apply (metis mod_add_self2 mod_diff_cong)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4823
    done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4824
  ultimately show ?thesis
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4825
    by (simp add: word_roti_def bit_word_rotl_iff nat_mod_distrib)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4826
qed
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  4827
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4828
lemma restrict_to_left: "x = y \<Longrightarrow> x = z \<longleftrightarrow> y = z"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4829
  by simp
38527
f2709bc1e41f moved spurious auxiliary lemma here
haftmann
parents: 37887
diff changeset
  4830
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4831
lemmas rrs0 = rotate_eqs [THEN restrict_to_left,
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4832
  simplified rotate_gal [symmetric] rotate_gal' [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4833
lemmas rrs1 = rrs0 [THEN refl [THEN rev_iffD1]]
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4834
lemmas rotater_eqs = rrs1 [simplified length_rotater]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4835
lemmas rotater_0 = rotater_eqs (1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4836
lemmas rotater_add = rotater_eqs (2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4837
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4838
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4839
subsubsection \<open>map, map2, commuting with rotate(r)\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4840
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4841
lemma butlast_map: "xs \<noteq> [] \<Longrightarrow> butlast (map f xs) = map f (butlast xs)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4842
  by (induct xs) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4843
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4844
lemma rotater1_map: "rotater1 (map f xs) = map f (rotater1 xs)"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4845
  by (cases xs) (auto simp: rotater1_def last_map butlast_map)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4846
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4847
lemma rotater_map: "rotater n (map f xs) = map f (rotater n xs)"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4848
  by (induct n) (auto simp: rotater_def rotater1_map)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4849
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4850
lemma but_last_zip [rule_format] :
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4851
  "\<forall>ys. length xs = length ys \<longrightarrow> xs \<noteq> [] \<longrightarrow>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4852
    last (zip xs ys) = (last xs, last ys) \<and>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4853
    butlast (zip xs ys) = zip (butlast xs) (butlast ys)"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4854
  apply (induct xs)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4855
   apply auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4856
     apply ((case_tac ys, auto simp: neq_Nil_conv)[1])+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4857
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4858
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4859
lemma but_last_map2 [rule_format] :
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4860
  "\<forall>ys. length xs = length ys \<longrightarrow> xs \<noteq> [] \<longrightarrow>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4861
    last (map2 f xs ys) = f (last xs) (last ys) \<and>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4862
    butlast (map2 f xs ys) = map2 f (butlast xs) (butlast ys)"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4863
  apply (induct xs)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4864
   apply auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4865
     apply ((case_tac ys, auto simp: neq_Nil_conv)[1])+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4866
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4867
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4868
lemma rotater1_zip:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4869
  "length xs = length ys \<Longrightarrow>
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4870
    rotater1 (zip xs ys) = zip (rotater1 xs) (rotater1 ys)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4871
  apply (unfold rotater1_def)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4872
  apply (cases xs)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4873
   apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4874
   apply ((case_tac ys, auto simp: neq_Nil_conv but_last_zip)[1])+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4875
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4876
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4877
lemma rotater1_map2:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4878
  "length xs = length ys \<Longrightarrow>
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4879
    rotater1 (map2 f xs ys) = map2 f (rotater1 xs) (rotater1 ys)"
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  4880
  by (simp add: rotater1_map rotater1_zip)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4881
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4882
lemmas lrth =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4883
  box_equals [OF asm_rl length_rotater [symmetric]
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4884
                 length_rotater [symmetric],
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4885
              THEN rotater1_map2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4886
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4887
lemma rotater_map2:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4888
  "length xs = length ys \<Longrightarrow>
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4889
    rotater n (map2 f xs ys) = map2 f (rotater n xs) (rotater n ys)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4890
  by (induct n) (auto intro!: lrth)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4891
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4892
lemma rotate1_map2:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4893
  "length xs = length ys \<Longrightarrow>
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4894
    rotate1 (map2 f xs ys) = map2 f (rotate1 xs) (rotate1 ys)"
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  4895
  by (cases xs; cases ys) auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4896
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4897
lemmas lth = box_equals [OF asm_rl length_rotate [symmetric]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4898
  length_rotate [symmetric], THEN rotate1_map2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4899
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4900
lemma rotate_map2:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4901
  "length xs = length ys \<Longrightarrow>
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4902
    rotate n (map2 f xs ys) = map2 f (rotate n xs) (rotate n ys)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4903
  by (induct n) (auto intro!: lth)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4904
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4905
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
  4906
\<comment> \<open>corresponding equalities for word rotation\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4907
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4908
lemma to_bl_rotl: "to_bl (word_rotl n w) = rotate n (to_bl w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4909
  by (simp add: word_bl.Abs_inverse' word_rotl_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4910
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4911
lemmas blrs0 = rotate_eqs [THEN to_bl_rotl [THEN trans]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4912
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4913
lemmas word_rotl_eqs =
45538
1fffa81b9b83 eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents: 45529
diff changeset
  4914
  blrs0 [simplified word_bl_Rep' word_bl.Rep_inject to_bl_rotl [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4915
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4916
lemma to_bl_rotr: "to_bl (word_rotr n w) = rotater n (to_bl w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4917
  by (simp add: word_bl.Abs_inverse' word_rotr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4918
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4919
lemmas brrs0 = rotater_eqs [THEN to_bl_rotr [THEN trans]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4920
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4921
lemmas word_rotr_eqs =
45538
1fffa81b9b83 eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents: 45529
diff changeset
  4922
  brrs0 [simplified word_bl_Rep' word_bl.Rep_inject to_bl_rotr [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4923
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4924
declare word_rotr_eqs (1) [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4925
declare word_rotl_eqs (1) [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4926
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4927
lemma word_rot_rl [simp]: "word_rotl k (word_rotr k v) = v"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4928
  and word_rot_lr [simp]: "word_rotr k (word_rotl k v) = v"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4929
  by (auto simp add: to_bl_rotr to_bl_rotl word_bl.Rep_inject [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4930
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4931
lemma word_rot_gal: "word_rotr n v = w \<longleftrightarrow> word_rotl n w = v"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4932
  and word_rot_gal': "w = word_rotr n v \<longleftrightarrow> v = word_rotl n w"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4933
  by (auto simp: to_bl_rotr to_bl_rotl word_bl.Rep_inject [symmetric] dest: sym)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4934
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4935
lemma word_rotr_rev: "word_rotr n w = word_reverse (word_rotl n (word_reverse w))"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4936
  by (simp only: word_bl.Rep_inject [symmetric] to_bl_word_rev to_bl_rotr to_bl_rotl rotater_rev)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4937
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4938
lemma word_roti_0 [simp]: "word_roti 0 w = w"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4939
  by (auto simp: word_rot_defs)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4940
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4941
lemmas abl_cong = arg_cong [where f = "of_bl"]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4942
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4943
lemma word_roti_add: "word_roti (m + n) w = word_roti m (word_roti n w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4944
proof -
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4945
  have rotater_eq_lem: "\<And>m n xs. m = n \<Longrightarrow> rotater m xs = rotater n xs"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4946
    by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4947
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4948
  have rotate_eq_lem: "\<And>m n xs. m = n \<Longrightarrow> rotate m xs = rotate n xs"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4949
    by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4950
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4951
  note rpts [symmetric] =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4952
    rotate_inv_plus [THEN conjunct1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4953
    rotate_inv_plus [THEN conjunct2, THEN conjunct1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4954
    rotate_inv_plus [THEN conjunct2, THEN conjunct2, THEN conjunct1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4955
    rotate_inv_plus [THEN conjunct2, THEN conjunct2, THEN conjunct2]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4956
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4957
  note rrp = trans [symmetric, OF rotate_rotate rotate_eq_lem]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4958
  note rrrp = trans [symmetric, OF rotater_add [symmetric] rotater_eq_lem]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4959
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4960
  show ?thesis
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4961
    apply (unfold word_rot_defs)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4962
    apply (simp only: split: if_split)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4963
    apply (safe intro!: abl_cong)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4964
           apply (simp_all only: to_bl_rotl [THEN word_bl.Rep_inverse']
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4965
                  to_bl_rotl
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4966
                  to_bl_rotr [THEN word_bl.Rep_inverse']
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4967
                  to_bl_rotr)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4968
         apply (rule rrp rrrp rpts,
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4969
                simp add: nat_add_distrib [symmetric]
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4970
                nat_diff_distrib [symmetric])+
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4971
    done
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4972
qed
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4973
67118
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4974
lemma word_roti_conv_mod':
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4975
  "word_roti n w = word_roti (n mod int (size w)) w"
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4976
proof (cases "size w = 0")
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4977
  case True
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4978
  then show ?thesis
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4979
    by simp
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4980
next
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4981
  case False
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4982
  then have [simp]: "l mod int (size w) \<ge> 0" for l
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4983
    by simp
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4984
  then have *: "word_roti (n mod int (size w)) w = word_rotr (nat (n mod int (size w))) w"
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4985
    by (simp add: word_roti_def)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4986
  show ?thesis
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4987
  proof (cases "n \<ge> 0")
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4988
    case True
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4989
    then show ?thesis
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4990
      apply (auto simp add: not_le *)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4991
      apply (auto simp add: word_rot_defs)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4992
      apply (safe intro!: abl_cong)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4993
      apply (rule rotater_eqs)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4994
      apply (simp add: word_size nat_mod_distrib)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4995
      done
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4996
  next
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4997
    case False
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4998
    moreover define k where "k = - n"
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4999
    ultimately have n: "n = - k"
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5000
      by simp_all
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5001
    from False show ?thesis
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5002
      apply (auto simp add: not_le *)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5003
      apply (auto simp add: word_rot_defs)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5004
      apply (simp add: n)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5005
      apply (safe intro!: abl_cong)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5006
      apply (simp add: rotater_add [symmetric] rotate_gal [symmetric])
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5007
      apply (rule rotater_eqs)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5008
      apply (simp add: word_size [symmetric, of w])
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5009
      apply (rule of_nat_eq_0_iff [THEN iffD1])
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5010
      apply (auto simp add: nat_add_distrib [symmetric] mod_eq_0_iff_dvd)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5011
      using dvd_nat_abs_iff [of "size w" "- k mod int (size w) + k"]
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5012
      apply (simp add: algebra_simps)
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5013
      apply (simp add: word_size)
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
  5014
      apply (metis dvd_eq_mod_eq_0 eq_neg_iff_add_eq_0 k_def mod_0 mod_add_right_eq n)
67118
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5015
      done
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5016
  qed
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  5017
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5018
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5019
lemmas word_roti_conv_mod = word_roti_conv_mod' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5020
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5021
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  5022
subsubsection \<open>"Word rotation commutes with bit-wise operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5023
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  5024
\<comment> \<open>using locale to not pollute lemma namespace\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5025
locale word_rotate
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5026
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5027
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5028
lemmas word_rot_defs' = to_bl_rotl to_bl_rotr
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5029
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5030
lemmas blwl_syms [symmetric] = bl_word_not bl_word_and bl_word_or bl_word_xor
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5031
45538
1fffa81b9b83 eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents: 45529
diff changeset
  5032
lemmas lbl_lbl = trans [OF word_bl_Rep' word_bl_Rep' [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5033
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5034
lemmas ths_map2 [OF lbl_lbl] = rotate_map2 rotater_map2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5035
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  5036
lemmas ths_map [where xs = "to_bl v"] = rotate_map rotater_map for v
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5037
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5038
lemmas th1s [simplified word_rot_defs' [symmetric]] = ths_map2 ths_map
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5039
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5040
lemma word_rot_logs:
71149
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  5041
  "word_rotl n (NOT v) = NOT (word_rotl n v)"
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  5042
  "word_rotr n (NOT v) = NOT (word_rotr n v)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5043
  "word_rotl n (x AND y) = word_rotl n x AND word_rotl n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5044
  "word_rotr n (x AND y) = word_rotr n x AND word_rotr n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5045
  "word_rotl n (x OR y) = word_rotl n x OR word_rotl n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5046
  "word_rotr n (x OR y) = word_rotr n x OR word_rotr n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5047
  "word_rotl n (x XOR y) = word_rotl n x XOR word_rotl n y"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5048
  "word_rotr n (x XOR y) = word_rotr n x XOR word_rotr n y"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5049
  by (rule word_bl.Rep_eqD,
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5050
      rule word_rot_defs' [THEN trans],
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5051
      simp only: blwl_syms [symmetric],
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5052
      rule th1s [THEN trans],
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5053
      rule refl)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5054
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5055
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5056
lemmas word_rot_logs = word_rotate.word_rot_logs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5057
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5058
lemmas bl_word_rotl_dt = trans [OF to_bl_rotl rotate_drop_take,
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  5059
  simplified word_bl_Rep']
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5060
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5061
lemmas bl_word_rotr_dt = trans [OF to_bl_rotr rotater_drop_take,
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  5062
  simplified word_bl_Rep']
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5063
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5064
lemma bl_word_roti_dt':
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5065
  "n = nat ((- i) mod int (size (w :: 'a::len word))) \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5066
    to_bl (word_roti i w) = drop n (to_bl w) @ take n (to_bl w)"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5067
  apply (unfold word_roti_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5068
  apply (simp add: bl_word_rotl_dt bl_word_rotr_dt word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5069
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5070
   apply (simp add: zmod_zminus1_eq_if)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5071
   apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5072
    apply (simp add: nat_mult_distrib)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5073
   apply (simp add: nat_diff_distrib [OF pos_mod_sign pos_mod_conj
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5074
                                      [THEN conjunct2, THEN order_less_imp_le]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5075
                    nat_mod_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5076
  apply (simp add: nat_mod_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5077
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5078
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5079
lemmas bl_word_roti_dt = bl_word_roti_dt' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5080
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5081
lemmas word_rotl_dt = bl_word_rotl_dt [THEN word_bl.Rep_inverse' [symmetric]]
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  5082
lemmas word_rotr_dt = bl_word_rotr_dt [THEN word_bl.Rep_inverse' [symmetric]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  5083
lemmas word_roti_dt = bl_word_roti_dt [THEN word_bl.Rep_inverse' [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5084
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5085
lemma word_rotx_0 [simp] : "word_rotr i 0 = 0 \<and> word_rotl i 0 = 0"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5086
  by (simp add: word_rotr_dt word_rotl_dt replicate_add [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5087
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5088
lemma word_roti_0' [simp] : "word_roti n 0 = 0"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5089
  by (auto simp: word_roti_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5090
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5091
lemmas word_rotr_dt_no_bin' [simp] =
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  5092
  word_rotr_dt [where w="numeral w", unfolded to_bl_numeral] for w
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  5093
  (* FIXME: negative numerals, 0 and 1 *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5094
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5095
lemmas word_rotl_dt_no_bin' [simp] =
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  5096
  word_rotl_dt [where w="numeral w", unfolded to_bl_numeral] for w
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  5097
  (* FIXME: negative numerals, 0 and 1 *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5098
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5099
declare word_roti_def [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5100
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5101
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  5102
subsection \<open>Maximum machine word\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5103
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5104
lemma word_int_cases:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5105
  fixes x :: "'a::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  5106
  obtains n where "x = word_of_int n" and "0 \<le> n" and "n < 2^LENGTH('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5107
  by (cases x rule: word_uint.Abs_cases) (simp add: uints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5108
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5109
lemma word_nat_cases [cases type: word]:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5110
  fixes x :: "'a::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  5111
  obtains n where "x = of_nat n" and "n < 2^LENGTH('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5112
  by (cases x rule: word_unat.Abs_cases) (simp add: unats_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5113
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5114
lemma max_word_max [intro!]: "n \<le> max_word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  5115
  by (fact word_order.extremum)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5116
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5117
lemma word_of_int_2p_len: "word_of_int (2 ^ LENGTH('a)) = (0::'a::len word)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  5118
  by (subst word_uint.Abs_norm [symmetric]) simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5119
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  5120
lemma word_pow_0: "(2::'a::len word) ^ LENGTH('a) = 0"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  5121
  by (fact word_exp_length_eq_0)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5122
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5123
lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word"
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5124
  by (simp add: eq_neg_iff_add_eq_0)
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5125
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5126
lemma max_word_bl: "to_bl (max_word::'a::len word) = replicate LENGTH('a) True"
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5127
  by (fact to_bl_n1)
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5128
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5129
lemma max_test_bit: "(max_word::'a::len word) !! n \<longleftrightarrow> n < LENGTH('a)"
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5130
  by (fact nth_minus1)
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5131
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5132
lemma word_and_max: "x AND max_word = x"
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5133
  by (fact word_log_esimps)
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5134
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5135
lemma word_or_max: "x OR max_word = max_word"
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  5136
  by (fact word_log_esimps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5137
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5138
lemma word_ao_dist2: "x AND (y OR z) = x AND y OR x AND z"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5139
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5140
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5141
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5142
lemma word_oa_dist2: "x OR y AND z = (x OR y) AND (x OR z)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5143
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5144
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5145
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5146
lemma word_and_not [simp]: "x AND NOT x = 0"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5147
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5148
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5149
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5150
lemma word_or_not [simp]: "x OR NOT x = max_word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5151
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5152
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5153
lemma word_xor_and_or: "x XOR y = x AND NOT y OR NOT x AND y"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5154
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5155
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5156
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5157
lemma shiftr_x_0 [iff]: "x >> 0 = x"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5158
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5159
  by (simp add: shiftr_bl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5160
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5161
lemma shiftl_x_0 [simp]: "x << 0 = x"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5162
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5163
  by (simp add: shiftl_t2n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5164
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5165
lemma shiftl_1 [simp]: "(1::'a::len word) << n = 2^n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5166
  by (simp add: shiftl_t2n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5167
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5168
lemma uint_lt_0 [simp]: "uint x < 0 = False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5169
  by (simp add: linorder_not_less)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5170
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5171
lemma shiftr1_1 [simp]: "shiftr1 (1::'a::len word) = 0"
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  5172
  unfolding shiftr1_def by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5173
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5174
lemma shiftr_1[simp]: "(1::'a::len word) >> n = (if n = 0 then 1 else 0)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5175
  by (induct n) (auto simp: shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5176
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5177
lemma word_less_1 [simp]: "x < 1 \<longleftrightarrow> x = 0"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5178
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5179
  by (simp add: word_less_nat_alt unat_0_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5180
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5181
lemma to_bl_mask:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5182
  "to_bl (mask n :: 'a::len word) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  5183
  replicate (LENGTH('a) - n) False @
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  5184
    replicate (min (LENGTH('a)) n) True"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5185
  by (simp add: mask_bl word_rep_drop min_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5186
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5187
lemma map_replicate_True:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  5188
  "n = length xs \<Longrightarrow>
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5189
    map (\<lambda>(x,y). x \<and> y) (zip xs (replicate n True)) = xs"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5190
  by (induct xs arbitrary: n) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5191
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5192
lemma map_replicate_False:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5193
  "n = length xs \<Longrightarrow> map (\<lambda>(x,y). x \<and> y)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5194
    (zip xs (replicate n False)) = replicate n False"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5195
  by (induct xs arbitrary: n) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5196
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5197
lemma bl_and_mask:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5198
  fixes w :: "'a::len word"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5199
    and n :: nat
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  5200
  defines "n' \<equiv> LENGTH('a) - n"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5201
  shows "to_bl (w AND mask n) = replicate n' False @ drop n' (to_bl w)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5202
proof -
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5203
  note [simp] = map_replicate_True map_replicate_False
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  5204
  have "to_bl (w AND mask n) = map2 (\<and>) (to_bl w) (to_bl (mask n::'a::len word))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5205
    by (simp add: bl_word_and)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5206
  also have "to_bl w = take n' (to_bl w) @ drop n' (to_bl w)"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5207
    by simp
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  5208
  also have "map2 (\<and>) \<dots> (to_bl (mask n::'a::len word)) =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5209
      replicate n' False @ drop n' (to_bl w)"
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  5210
    unfolding to_bl_mask n'_def by (subst zip_append) auto
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5211
  finally show ?thesis .
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5212
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5213
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5214
lemma drop_rev_takefill:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  5215
  "length xs \<le> n \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5216
    drop (n - length xs) (rev (takefill False n (rev xs))) = xs"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5217
  by (simp add: takefill_alt rev_take)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5218
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5219
lemma map_nth_0 [simp]: "map ((!!) (0::'a::len word)) xs = replicate (length xs) False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5220
  by (induct xs) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5221
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5222
lemma uint_plus_if_size:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5223
  "uint (x + y) =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5224
    (if uint x + uint y < 2^size x
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5225
     then uint x + uint y
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5226
     else uint x + uint y - 2^size x)"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5227
  by (simp add: word_arith_wis int_word_uint mod_add_if_z word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5228
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5229
lemma unat_plus_if_size:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  5230
  "unat (x + y) =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5231
    (if unat x + unat y < 2^size x
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5232
     then unat x + unat y
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5233
     else unat x + unat y - 2^size x)"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  5234
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5235
  apply (subst word_arith_nat_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5236
  apply (subst unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5237
  apply (simp add:  mod_nat_add word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5238
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5239
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5240
lemma word_neq_0_conv: "w \<noteq> 0 \<longleftrightarrow> 0 < w"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5241
  for w :: "'a::len word"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5242
  by (simp add: word_gt_0)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5243
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5244
lemma max_lt: "unat (max a b div c) = unat (max a b) div unat c"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5245
  for c :: "'a::len word"
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  5246
  by (fact unat_div)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5247
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5248
lemma uint_sub_if_size:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5249
  "uint (x - y) =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5250
    (if uint y \<le> uint x
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5251
     then uint x - uint y
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5252
     else uint x - uint y + 2^size x)"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5253
  by (simp add: word_arith_wis int_word_uint mod_sub_if_z word_size)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5254
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5255
lemma unat_sub: "b \<le> a \<Longrightarrow> unat (a - b) = unat a - unat b"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5256
  by (simp add: unat_def uint_sub_if_size word_le_def nat_diff_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5257
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  5258
lemmas word_less_sub1_numberof [simp] = word_less_sub1 [of "numeral w"] for w
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  5259
lemmas word_le_sub1_numberof [simp] = word_le_sub1 [of "numeral w"] for w
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5260
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  5261
lemma word_of_int_minus: "word_of_int (2^LENGTH('a) - i) = (word_of_int (-i)::'a::len word)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5262
proof -
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  5263
  have *: "2^LENGTH('a) - i = -i + 2^LENGTH('a)"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5264
    by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5265
  show ?thesis
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5266
    apply (subst *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5267
    apply (subst word_uint.Abs_norm [symmetric], subst mod_add_self2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5268
    apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5269
    done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5270
qed
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5271
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5272
lemmas word_of_int_inj =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5273
  word_uint.Abs_inject [unfolded uints_num, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5274
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5275
lemma word_le_less_eq: "x \<le> y \<longleftrightarrow> x = y \<or> x < y"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5276
  for x y :: "'z::len word"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  5277
  by (auto simp add: order_class.le_less)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5278
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5279
lemma mod_plus_cong:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5280
  fixes b b' :: int
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5281
  assumes 1: "b = b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5282
    and 2: "x mod b' = x' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5283
    and 3: "y mod b' = y' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5284
    and 4: "x' + y' = z'"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5285
  shows "(x + y) mod b = z' mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5286
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5287
  from 1 2[symmetric] 3[symmetric] have "(x + y) mod b = (x' mod b' + y' mod b') mod b'"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  5288
    by (simp add: mod_add_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5289
  also have "\<dots> = (x' + y') mod b'"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  5290
    by (simp add: mod_add_eq)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5291
  finally show ?thesis
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5292
    by (simp add: 4)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5293
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5294
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5295
lemma mod_minus_cong:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5296
  fixes b b' :: int
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5297
  assumes "b = b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5298
    and "x mod b' = x' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5299
    and "y mod b' = y' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5300
    and "x' - y' = z'"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5301
  shows "(x - y) mod b = z' mod b'"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5302
  using assms [symmetric] by (auto intro: mod_diff_cong)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5303
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  5304
lemma word_induct_less: "P 0 \<Longrightarrow> (\<And>n. n < m \<Longrightarrow> P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P m"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5305
  for P :: "'a::len word \<Rightarrow> bool"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5306
  apply (cases m)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5307
  apply atomize
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5308
  apply (erule rev_mp)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5309
  apply (rule_tac x=m in spec)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5310
  apply (induct_tac n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5311
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5312
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5313
  apply (erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5314
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5315
   apply (erule_tac x=n in allE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5316
   apply (erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5317
    apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5318
    apply (clarsimp simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5319
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5320
  apply (erule_tac x="of_nat na" in allE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5321
  apply (erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5322
   apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5323
   apply (clarsimp simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5324
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5325
  done
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5326
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  5327
lemma word_induct: "P 0 \<Longrightarrow> (\<And>n. P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P m"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5328
  for P :: "'a::len word \<Rightarrow> bool"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5329
  by (erule word_induct_less) simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5330
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  5331
lemma word_induct2 [induct type]: "P 0 \<Longrightarrow> (\<And>n. 1 + n \<noteq> 0 \<Longrightarrow> P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P n"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5332
  for P :: "'b::len word \<Rightarrow> bool"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5333
  apply (rule word_induct)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5334
   apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5335
  apply (case_tac "1 + n = 0")
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5336
   apply auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5337
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5338
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  5339
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  5340
subsection \<open>Recursion combinator for words\<close>
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
  5341
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
  5342
definition word_rec :: "'a \<Rightarrow> ('b::len word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5343
  where "word_rec forZero forSuc n = rec_nat forZero (forSuc \<circ> of_nat) (unat n)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5344
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5345
lemma word_rec_0: "word_rec z s 0 = z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5346
  by (simp add: word_rec_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5347
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  5348
lemma word_rec_Suc: "1 + n \<noteq> 0 \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  5349
  for n :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5350
  apply (simp add: word_rec_def unat_word_ariths)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5351
  apply (subst nat_mod_eq')
61649
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61424
diff changeset
  5352
   apply (metis Suc_eq_plus1_left Suc_lessI of_nat_2p unat_1 unat_lt2p word_arith_nat_add)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5353
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5354
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5355
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  5356
lemma word_rec_Pred: "n \<noteq> 0 \<Longrightarrow> word_rec z s n = s (n - 1) (word_rec z s (n - 1))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5357
  apply (rule subst[where t="n" and s="1 + (n - 1)"])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5358
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5359
  apply (subst word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5360
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5361
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5362
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5363
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5364
lemma word_rec_in: "f (word_rec z (\<lambda>_. f) n) = word_rec (f z) (\<lambda>_. f) n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5365
  by (induct n) (simp_all add: word_rec_0 word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5366
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  5367
lemma word_rec_in2: "f n (word_rec z f n) = word_rec (f 0 z) (f \<circ> (+) 1) n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5368
  by (induct n) (simp_all add: word_rec_0 word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5369
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5370
lemma word_rec_twice:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  5371
  "m \<le> n \<Longrightarrow> word_rec z f n = word_rec (word_rec z f (n - m)) (f \<circ> (+) (n - m)) m"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5372
  apply (erule rev_mp)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5373
  apply (rule_tac x=z in spec)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5374
  apply (rule_tac x=f in spec)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5375
  apply (induct n)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5376
   apply (simp add: word_rec_0)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5377
  apply clarsimp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5378
  apply (rule_tac t="1 + n - m" and s="1 + (n - m)" in subst)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5379
   apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5380
  apply (case_tac "1 + (n - m) = 0")
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5381
   apply (simp add: word_rec_0)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5382
   apply (rule_tac f = "word_rec a b" for a b in arg_cong)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5383
   apply (rule_tac t="m" and s="m + (1 + (n - m))" in subst)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5384
    apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5385
   apply (simp (no_asm_use))
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5386
  apply (simp add: word_rec_Suc word_rec_in2)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5387
  apply (erule impE)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5388
   apply uint_arith
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  5389
  apply (drule_tac x="x \<circ> (+) 1" in spec)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5390
  apply (drule_tac x="x 0 xa" in spec)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5391
  apply simp
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5392
  apply (rule_tac t="\<lambda>a. x (1 + (n - m + a))" and s="\<lambda>a. x (1 + (n - m) + a)" in subst)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5393
   apply (clarsimp simp add: fun_eq_iff)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5394
   apply (rule_tac t="(1 + (n - m + xb))" and s="1 + (n - m) + xb" in subst)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5395
    apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5396
   apply (rule refl)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5397
  apply (rule refl)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5398
  done
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5399
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5400
lemma word_rec_id: "word_rec z (\<lambda>_. id) n = z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5401
  by (induct n) (auto simp add: word_rec_0 word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5402
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5403
lemma word_rec_id_eq: "\<forall>m < n. f m = id \<Longrightarrow> word_rec z f n = z"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5404
  apply (erule rev_mp)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5405
  apply (induct n)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5406
   apply (auto simp add: word_rec_0 word_rec_Suc)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5407
   apply (drule spec, erule mp)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5408
   apply uint_arith
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5409
  apply (drule_tac x=n in spec, erule impE)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5410
   apply uint_arith
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5411
  apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5412
  done
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5413
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  5414
lemma word_rec_max:
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58061
diff changeset
  5415
  "\<forall>m\<ge>n. m \<noteq> - 1 \<longrightarrow> f m = id \<Longrightarrow> word_rec z f (- 1) = word_rec z f n"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5416
  apply (subst word_rec_twice[where n="-1" and m="-1 - n"])
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5417
   apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5418
  apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5419
  apply (rule word_rec_id_eq)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5420
  apply clarsimp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5421
  apply (drule spec, rule mp, erule mp)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5422
   apply (rule word_plus_mono_right2[OF _ order_less_imp_le])
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5423
    prefer 2
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5424
    apply assumption
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5425
   apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5426
  apply (erule contrapos_pn)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5427
  apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5428
  apply (drule arg_cong[where f="\<lambda>x. x - n"])
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5429
  apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5430
  done
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  5431
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5432
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5433
subsection \<open>More\<close>
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5434
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5435
lemma test_bit_1' [simp]:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5436
  "(1 :: 'a :: len word) !! n \<longleftrightarrow> 0 < LENGTH('a) \<and> n = 0"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  5437
  by simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5438
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5439
lemma mask_0 [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5440
  "mask 0 = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5441
  by (simp add: Word.mask_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5442
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  5443
lemma shiftl0:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  5444
  "x << 0 = (x :: 'a :: len word)"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  5445
  by (fact shiftl_x_0)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5446
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5447
lemma mask_1: "mask 1 = 1"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5448
  by (simp add: mask_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5449
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5450
lemma mask_Suc_0: "mask (Suc 0) = 1"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5451
  by (simp add: mask_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5452
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5453
lemma mask_numeral: "mask (numeral n) = 2 * mask (pred_numeral n) + 1"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5454
  by (simp add: mask_def neg_numeral_class.sub_def numeral_eq_Suc numeral_pow)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5455
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5456
lemma bin_last_bintrunc: "bin_last (bintrunc l n) = (l > 0 \<and> bin_last n)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5457
  by (cases l) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5458
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5459
lemma word_and_1:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5460
  "n AND 1 = (if n !! 0 then 1 else 0)" for n :: "_ word"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5461
  by transfer (rule bin_rl_eqI, simp_all add: bin_rest_trunc bin_last_bintrunc)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5462
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5463
lemma bintrunc_shiftl:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5464
  "bintrunc n (m << i) = bintrunc (n - i) m << i"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5465
proof (induction i arbitrary: n)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5466
  case 0
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5467
  show ?case
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5468
    by simp
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5469
next
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5470
  case (Suc i)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  5471
  then show ?case by (cases n) (simp_all add: take_bit_Suc)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5472
qed
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5473
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5474
lemma shiftl_transfer [transfer_rule]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5475
  includes lifting_syntax
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5476
  shows "(pcr_word ===> (=) ===> pcr_word) (<<) (<<)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5477
  by (auto intro!: rel_funI word_eqI simp add: word.pcr_cr_eq cr_word_def word_size nth_shiftl)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5478
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5479
lemma uint_shiftl:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5480
  "uint (n << i) = bintrunc (size n) (uint n << i)"
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  5481
  apply (simp add: word_size shiftl_eq_push_bit shiftl_word_eq)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  5482
  apply transfer
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  5483
  apply (simp add: push_bit_take_bit)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  5484
  done
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5485
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5486
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  5487
subsection \<open>Misc\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5488
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5489
declare bin_to_bl_def [simp]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  5490
69605
a96320074298 isabelle update -u path_cartouches;
wenzelm
parents: 69064
diff changeset
  5491
ML_file \<open>Tools/word_lib.ML\<close>
a96320074298 isabelle update -u path_cartouches;
wenzelm
parents: 69064
diff changeset
  5492
ML_file \<open>Tools/smt_word.ML\<close>
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 35049
diff changeset
  5493
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  5494
hide_const (open) Word
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  5495
41060
4199fdcfa3c0 moved smt_word.ML into the directory of the Word library
boehmes
parents: 40827
diff changeset
  5496
end