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