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