src/HOL/Word/Word.thy
author haftmann
Mon, 07 Sep 2020 16:14:32 +0000
changeset 72243 eaac77208cf9
parent 72242 bb002df3e82e
child 72244 4b011fa5e83b
permissions -rw-r--r--
tuned theory structure
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29628
d9294387ab0e entry point for Word library now named Word
haftmann
parents: 27137
diff changeset
     1
(*  Title:      HOL/Word/Word.thy
46124
3ee75fe01986 misc tuning;
wenzelm
parents: 46064
diff changeset
     2
    Author:     Jeremy Dawson and Gerwin Klein, NICTA
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
*)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
     5
section \<open>A type of finite bit strings\<close>
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
     6
29628
d9294387ab0e entry point for Word library now named Word
haftmann
parents: 27137
diff changeset
     7
theory Word
41413
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41060
diff changeset
     8
imports
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 65363
diff changeset
     9
  "HOL-Library.Type_Length"
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 65363
diff changeset
    10
  "HOL-Library.Boolean_Algebra"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
    11
  "HOL-Library.Bit_Operations"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
    12
  Bits_Int
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
    13
  Traditional_Syntax
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
    14
  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
    15
  Misc_Typedef
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    16
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    17
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    18
subsection \<open>Preliminaries\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    19
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    20
lemma signed_take_bit_decr_length_iff:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    21
  \<open>signed_take_bit (LENGTH('a::len) - Suc 0) k = signed_take_bit (LENGTH('a) - Suc 0) l
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    22
    \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    23
  by (cases \<open>LENGTH('a)\<close>)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    24
    (simp_all add: signed_take_bit_eq_iff_take_bit_eq)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    25
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    26
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    27
subsection \<open>Type definition and fundamental arithmetic\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    28
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    29
quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    30
  morphisms rep Word by (auto intro!: equivpI reflpI sympI transpI)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    31
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    32
hide_const (open) rep \<comment> \<open>only for foundational purpose\<close>
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
    33
hide_const (open) Word \<comment> \<open>only for code generation\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
    34
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    35
instantiation word :: (len) comm_ring_1
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    36
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    37
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    38
lift_definition zero_word :: \<open>'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    39
  is 0 .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    40
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    41
lift_definition one_word :: \<open>'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    42
  is 1 .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    43
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    44
lift_definition plus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    45
  is \<open>(+)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    46
  by (auto simp add: take_bit_eq_mod intro: mod_add_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    47
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    48
lift_definition minus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    49
  is \<open>(-)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    50
  by (auto simp add: take_bit_eq_mod intro: mod_diff_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    51
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    52
lift_definition uminus_word :: \<open>'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    53
  is uminus
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    54
  by (auto simp add: take_bit_eq_mod intro: mod_minus_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    55
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    56
lift_definition times_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    57
  is \<open>(*)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    58
  by (auto simp add: take_bit_eq_mod intro: mod_mult_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    59
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    60
instance
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    61
  by (standard; transfer) (simp_all add: algebra_simps)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    62
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    63
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    64
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    65
context
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    66
  includes lifting_syntax
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    67
  notes power_transfer [transfer_rule]
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    68
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    69
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    70
lemma power_transfer_word [transfer_rule]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    71
  \<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    72
  by transfer_prover
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    73
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    74
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    75
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    76
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    77
subsection \<open>Basic code generation setup\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    78
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    79
lift_definition uint :: \<open>'a::len word \<Rightarrow> int\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    80
  is \<open>take_bit LENGTH('a)\<close> .
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    81
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    82
lemma [code abstype]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    83
  \<open>Word.Word (uint w) = w\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    84
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    85
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    86
quickcheck_generator word
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    87
  constructors:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    88
    \<open>0 :: 'a::len word\<close>,
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    89
    \<open>numeral :: num \<Rightarrow> 'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    90
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    91
instantiation word :: (len) equal
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    92
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    93
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    94
lift_definition equal_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> bool\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    95
  is \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    96
  by simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    97
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    98
instance
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    99
  by (standard; transfer) rule
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   100
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   101
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   102
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   103
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   104
  \<open>HOL.equal v w \<longleftrightarrow> HOL.equal (uint v) (uint w)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   105
  by transfer (simp add: equal)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   106
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   107
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   108
  \<open>uint 0 = 0\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   109
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   110
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   111
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   112
  \<open>uint 1 = 1\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   113
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   114
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   115
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   116
  \<open>uint (v + w) = take_bit LENGTH('a) (uint v + uint w)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   117
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   118
  by transfer (simp add: take_bit_add)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   119
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   120
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   121
  \<open>uint (- w) = (let k = uint w in if w = 0 then 0 else 2 ^ LENGTH('a) - k)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   122
  for w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   123
  by transfer (auto simp add: take_bit_eq_mod zmod_zminus1_eq_if)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   124
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   125
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   126
  \<open>uint (v - w) = take_bit LENGTH('a) (uint v - uint w)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   127
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   128
  by transfer (simp add: take_bit_diff)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   129
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   130
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   131
  \<open>uint (v * w) = take_bit LENGTH('a) (uint v * uint w)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   132
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   133
  by transfer (simp add: take_bit_mult)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   134
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   135
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   136
subsection \<open>Conversions including casts\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   137
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   138
context
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   139
  includes lifting_syntax
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   140
  notes 
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   141
    transfer_rule_of_bool [transfer_rule]
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   142
    transfer_rule_numeral [transfer_rule]
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   143
    transfer_rule_of_nat [transfer_rule]
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   144
    transfer_rule_of_int [transfer_rule]
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   145
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   146
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   147
lemma [transfer_rule]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   148
  \<open>((=) ===> pcr_word) of_bool of_bool\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   149
  by transfer_prover
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   150
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   151
lemma [transfer_rule]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   152
  \<open>((=) ===> pcr_word) numeral numeral\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   153
  by transfer_prover
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   154
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   155
lemma [transfer_rule]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   156
  \<open>((=) ===> pcr_word) int of_nat\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   157
  by transfer_prover
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   158
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   159
lemma [transfer_rule]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   160
  \<open>((=) ===> pcr_word) (\<lambda>k. k) of_int\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   161
proof -
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   162
  have \<open>((=) ===> pcr_word) of_int of_int\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   163
    by transfer_prover
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   164
  then show ?thesis by (simp add: id_def)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   165
qed
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   166
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   167
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   168
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   169
lemma word_exp_length_eq_0 [simp]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   170
  \<open>(2 :: 'a::len word) ^ LENGTH('a) = 0\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   171
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   172
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   173
lemma uint_nonnegative: "0 \<le> uint w"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   174
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   175
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   176
lemma uint_bounded: "uint w < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   177
  for w :: "'a::len word"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   178
  by transfer (simp add: take_bit_eq_mod)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   179
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   180
lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   181
  for w :: "'a::len word"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   182
  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
   183
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   184
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
   185
  by transfer simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   186
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   187
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
   188
  using word_uint_eqI by auto
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   189
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   190
lift_definition word_of_int :: \<open>int \<Rightarrow> 'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   191
  is \<open>\<lambda>k. k\<close> .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   192
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   193
lemma Word_eq_word_of_int [code_post]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   194
  \<open>Word.Word = word_of_int\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   195
  by rule (transfer, rule)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   196
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   197
lemma uint_word_of_int_eq [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   198
  \<open>uint (word_of_int k :: 'a::len word) = take_bit LENGTH('a) k\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   199
  by transfer rule
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   200
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   201
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len word) = k mod 2 ^ LENGTH('a)"
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   202
  by (simp add: uint_word_of_int_eq take_bit_eq_mod)
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   203
  
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   204
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
   205
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   206
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   207
lemma split_word_all: "(\<And>x::'a::len word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   208
proof
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   209
  fix x :: "'a word"
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   210
  assume "\<And>x. PROP P (word_of_int x)"
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   211
  then have "PROP P (word_of_int (uint x))" .
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   212
  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
   213
qed
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   214
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   215
lift_definition sint :: \<open>'a::len word \<Rightarrow> int\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   216
  \<comment> \<open>treats the most-significant bit as a sign bit\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   217
  is \<open>signed_take_bit (LENGTH('a) - 1)\<close>  
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   218
  by (simp add: signed_take_bit_decr_length_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   219
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   220
lemma sint_uint [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   221
  \<open>sint w = signed_take_bit (LENGTH('a) - 1) (uint w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   222
  for w :: \<open>'a::len word\<close>
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
   223
  by (cases \<open>LENGTH('a)\<close>; transfer) (simp_all add: signed_take_bit_take_bit)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   224
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   225
lift_definition unat :: \<open>'a::len word \<Rightarrow> nat\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   226
  is \<open>nat \<circ> take_bit LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   227
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   228
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   229
lemma nat_uint_eq [simp]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   230
  \<open>nat (uint w) = unat w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   231
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   232
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   233
lemma unat_eq_nat_uint [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   234
  \<open>unat w = nat (uint w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   235
  by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   236
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   237
lift_definition ucast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   238
  is \<open>take_bit LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   239
  by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   240
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   241
lemma ucast_eq [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   242
  \<open>ucast w = word_of_int (uint w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   243
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   244
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   245
lift_definition scast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   246
  is \<open>signed_take_bit (LENGTH('a) - 1)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   247
  by (simp flip: signed_take_bit_decr_length_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   248
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   249
lemma scast_eq [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   250
  \<open>scast w = word_of_int (sint w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   251
  by transfer simp
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   252
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   253
lemma uint_0_eq [simp]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   254
  \<open>uint 0 = 0\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   255
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   256
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   257
lemma uint_1_eq [simp]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   258
  \<open>uint 1 = 1\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   259
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   260
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   261
lemma word_m1_wi: "- 1 = word_of_int (- 1)"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   262
  by transfer rule
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   263
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   264
lemma uint_0_iff: "uint x = 0 \<longleftrightarrow> x = 0"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   265
  by (simp add: word_uint_eq_iff)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   266
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   267
lemma unat_0_iff: "unat x = 0 \<longleftrightarrow> x = 0"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   268
  by transfer (auto intro: antisym)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   269
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   270
lemma unat_0 [simp]: "unat 0 = 0"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   271
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   272
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   273
lemma unat_gt_0: "0 < unat x \<longleftrightarrow> x \<noteq> 0"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   274
  by (auto simp: unat_0_iff [symmetric])
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   275
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   276
lemma ucast_0 [simp]: "ucast 0 = 0"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   277
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   278
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   279
lemma sint_0 [simp]: "sint 0 = 0"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   280
  by (simp add: sint_uint)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   281
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   282
lemma scast_0 [simp]: "scast 0 = 0"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   283
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   284
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   285
lemma sint_n1 [simp] : "sint (- 1) = - 1"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   286
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   287
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   288
lemma scast_n1 [simp]: "scast (- 1) = - 1"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   289
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   290
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   291
lemma uint_1: "uint (1::'a::len word) = 1"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   292
  by (fact uint_1_eq)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   293
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   294
lemma unat_1 [simp]: "unat (1::'a::len word) = 1"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   295
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   296
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   297
lemma ucast_1 [simp]: "ucast (1::'a::len word) = 1"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   298
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   299
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   300
instantiation word :: (len) size
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   301
begin
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   302
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   303
lift_definition size_word :: \<open>'a word \<Rightarrow> nat\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   304
  is \<open>\<lambda>_. LENGTH('a)\<close> ..
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   305
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   306
instance ..
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   307
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   308
end
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   309
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   310
lemma word_size [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   311
  \<open>size w = LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   312
  by (fact size_word.rep_eq)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   313
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   314
lemma word_size_gt_0 [iff]: "0 < size w"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   315
  for w :: "'a::len word"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   316
  by (simp add: word_size)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   317
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   318
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
   319
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   320
lemma lens_not_0 [iff]:
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
   321
  \<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
   322
  by auto
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   323
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   324
lift_definition source_size :: \<open>('a::len word \<Rightarrow> 'b) \<Rightarrow> nat\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   325
  is \<open>\<lambda>_. LENGTH('a)\<close> .
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   326
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   327
lift_definition target_size :: \<open>('a \<Rightarrow> 'b::len word) \<Rightarrow> nat\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   328
  is \<open>\<lambda>_. LENGTH('b)\<close> ..
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   329
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   330
lift_definition is_up :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   331
  is \<open>\<lambda>_. LENGTH('a) \<le> LENGTH('b)\<close> ..
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   332
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   333
lift_definition is_down :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   334
  is \<open>\<lambda>_. LENGTH('a) \<ge> LENGTH('b)\<close> ..
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   335
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   336
lemma is_up_eq:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   337
  \<open>is_up f \<longleftrightarrow> source_size f \<le> target_size f\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   338
  for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   339
  by (simp add: source_size.rep_eq target_size.rep_eq is_up.rep_eq)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   340
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   341
lemma is_down_eq:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   342
  \<open>is_down f \<longleftrightarrow> target_size f \<le> source_size f\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   343
  for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   344
  by (simp add: source_size.rep_eq target_size.rep_eq is_down.rep_eq)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   345
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   346
lift_definition word_int_case :: \<open>(int \<Rightarrow> 'b) \<Rightarrow> 'a::len word \<Rightarrow> 'b\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   347
  is \<open>\<lambda>f. f \<circ> take_bit LENGTH('a)\<close> by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   348
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   349
lemma word_int_case_eq_uint [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   350
  \<open>word_int_case f w = f (uint w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   351
  by transfer simp
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   352
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   353
translations
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   354
  "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
   355
  "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
   356
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   357
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   358
subsection \<open>Type-definition locale instantiations\<close>
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   359
72043
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   360
definition uints :: "nat \<Rightarrow> int set"
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   361
  \<comment> \<open>the sets of integers representing the words\<close>
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
   362
  where "uints n = range (take_bit n)"
72043
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   363
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   364
definition sints :: "nat \<Rightarrow> int set"
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
   365
  where "sints n = range (signed_take_bit (n - 1))"
72043
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   366
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   367
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   368
  by (simp add: uints_def range_bintrunc)
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   369
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   370
lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}"
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   371
  by (simp add: sints_def range_sbintrunc)
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   372
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   373
definition unats :: "nat \<Rightarrow> nat set"
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   374
  where "unats n = {i. i < 2 ^ n}"
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   375
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   376
\<comment> \<open>naturals\<close>
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   377
lemma uints_unats: "uints n = int ` unats n"
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   378
  apply (unfold unats_def uints_num)
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   379
  apply safe
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   380
    apply (rule_tac image_eqI)
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   381
     apply (erule_tac nat_0_le [symmetric])
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   382
  by auto
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   383
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   384
lemma unats_uints: "unats n = nat ` uints n"
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   385
  by (auto simp: uints_unats image_iff)
b8bcdb884651 tuned grouping
haftmann
parents: 72042
diff changeset
   386
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   387
lemma td_ext_uint:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   388
  "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len)))
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   389
    (\<lambda>w::int. w mod 2 ^ LENGTH('a))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   390
  apply (unfold td_ext_def')
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   391
  apply transfer
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   392
  apply (simp add: uints_num take_bit_eq_mod)
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   393
  done
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   394
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   395
interpretation word_uint:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   396
  td_ext
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   397
    "uint::'a::len word \<Rightarrow> int"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   398
    word_of_int
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   399
    "uints (LENGTH('a::len))"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   400
    "\<lambda>w. w mod 2 ^ LENGTH('a::len)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   401
  by (fact td_ext_uint)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   402
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   403
lemmas td_uint = word_uint.td_thm
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   404
lemmas int_word_uint = word_uint.eq_norm
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   405
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   406
lemma td_ext_ubin:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   407
  "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len)))
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
   408
    (take_bit (LENGTH('a)))"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
   409
  apply standard
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
   410
  apply transfer
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
   411
  apply simp
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
   412
  done
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   413
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   414
interpretation word_ubin:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   415
  td_ext
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   416
    "uint::'a::len word \<Rightarrow> int"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   417
    word_of_int
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   418
    "uints (LENGTH('a::len))"
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
   419
    "take_bit (LENGTH('a::len))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   420
  by (fact td_ext_ubin)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   421
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   422
lemma td_ext_unat [OF refl]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   423
  "n = LENGTH('a::len) \<Longrightarrow>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   424
    td_ext (unat :: 'a word \<Rightarrow> nat) of_nat (unats n) (\<lambda>i. i mod 2 ^ n)"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   425
  apply (standard; transfer)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   426
     apply (simp_all add: unats_def take_bit_int_less_exp take_bit_of_nat take_bit_eq_self)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   427
  apply (simp add: take_bit_eq_mod)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   428
  done
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   429
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   430
lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm]
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   431
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   432
interpretation word_unat:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   433
  td_ext
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   434
    "unat::'a::len word \<Rightarrow> nat"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   435
    of_nat
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   436
    "unats (LENGTH('a::len))"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   437
    "\<lambda>i. i mod 2 ^ LENGTH('a::len)"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   438
  by (rule td_ext_unat)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   439
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   440
lemmas td_unat = word_unat.td_thm
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   441
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   442
lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq]
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   443
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   444
lemma unat_le: "y \<le> unat z \<Longrightarrow> y \<in> unats (LENGTH('a))"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   445
  for z :: "'a::len word"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   446
  apply (unfold unats_def)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   447
  apply clarsimp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   448
  apply (rule xtrans, rule unat_lt2p, assumption)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   449
  done
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   450
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   451
lemma td_ext_sbin:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   452
  "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len)))
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   453
    (signed_take_bit (LENGTH('a) - 1))"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   454
  apply (unfold td_ext_def' sint_uint)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   455
  apply (simp add : word_ubin.eq_norm)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   456
  apply (cases "LENGTH('a)")
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   457
   apply (auto simp add : sints_def)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   458
  apply (rule sym [THEN trans])
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   459
   apply (rule word_ubin.Abs_norm)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   460
  apply (simp only: bintrunc_sbintrunc)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   461
  apply (drule sym)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   462
  apply simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   463
  done
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   464
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   465
lemma td_ext_sint:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   466
  "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len)))
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   467
     (\<lambda>w. (w + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) -
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   468
         2 ^ (LENGTH('a) - 1))"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   469
  using td_ext_sbin [where ?'a = 'a] by (simp add: no_sbintr_alt2)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   470
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   471
text \<open>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   472
  We do \<open>sint\<close> before \<open>sbin\<close>, before \<open>sint\<close> is the user version
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   473
  and interpretations do not produce thm duplicates. I.e.
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   474
  we get the name \<open>word_sint.Rep_eqD\<close>, but not \<open>word_sbin.Req_eqD\<close>,
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   475
  because the latter is the same thm as the former.
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   476
\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   477
interpretation word_sint:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   478
  td_ext
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   479
    "sint ::'a::len word \<Rightarrow> int"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   480
    word_of_int
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   481
    "sints (LENGTH('a::len))"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   482
    "\<lambda>w. (w + 2^(LENGTH('a::len) - 1)) mod 2^LENGTH('a::len) -
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   483
      2 ^ (LENGTH('a::len) - 1)"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   484
  by (rule td_ext_sint)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   485
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   486
interpretation word_sbin:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   487
  td_ext
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   488
    "sint ::'a::len word \<Rightarrow> int"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   489
    word_of_int
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   490
    "sints (LENGTH('a::len))"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   491
    "signed_take_bit (LENGTH('a::len) - 1)"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   492
  by (rule td_ext_sbin)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   493
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   494
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm]
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   495
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   496
lemmas td_sint = word_sint.td
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   497
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   498
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   499
subsection \<open>Arithmetic operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   500
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   501
instantiation word :: (len) "{neg_numeral, modulo, comm_monoid_mult, comm_ring}"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   502
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   503
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   504
lift_definition divide_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   505
  is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   506
  by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   507
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   508
lift_definition modulo_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   509
  is "\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   510
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   511
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   512
instance
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 61076
diff changeset
   513
  by standard (transfer, simp add: algebra_simps)+
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   514
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   515
end
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   516
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   517
lemma word_div_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   518
  "a div b = word_of_int (uint a div uint b)"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   519
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   520
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   521
lemma word_mod_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   522
  "a mod b = word_of_int (uint a mod uint b)"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   523
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   524
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   525
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   526
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   527
text \<open>Legacy theorems:\<close>
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   528
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   529
lemma word_add_def [code]:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   530
  "a + b = word_of_int (uint a + uint b)"
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   531
  by transfer (simp add: take_bit_add)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   532
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   533
lemma word_sub_wi [code]:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   534
  "a - b = word_of_int (uint a - uint b)"
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   535
  by transfer (simp add: take_bit_diff)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   536
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   537
lemma word_mult_def [code]:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   538
  "a * b = word_of_int (uint a * uint b)"
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   539
  by transfer (simp add: take_bit_eq_mod mod_simps)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   540
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   541
lemma word_minus_def [code]:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   542
  "- a = word_of_int (- uint a)"
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   543
  by transfer (simp add: take_bit_minus)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   544
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   545
lemma word_0_wi:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   546
  "0 = word_of_int 0"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   547
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   548
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   549
lemma word_1_wi:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   550
  "1 = word_of_int 1"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   551
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   552
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   553
lift_definition word_succ :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x + 1"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   554
  by (auto simp add: take_bit_eq_mod intro: mod_add_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   555
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   556
lift_definition word_pred :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x - 1"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   557
  by (auto simp add: take_bit_eq_mod intro: mod_diff_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   558
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   559
lemma word_succ_alt [code]:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   560
  "word_succ a = word_of_int (uint a + 1)"
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   561
  by transfer (simp add: take_bit_eq_mod mod_simps)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   562
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   563
lemma word_pred_alt [code]:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   564
  "word_pred a = word_of_int (uint a - 1)"
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   565
  by transfer (simp add: take_bit_eq_mod mod_simps)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   566
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   567
lemmas word_arith_wis = 
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   568
  word_add_def word_sub_wi word_mult_def
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   569
  word_minus_def word_succ_alt word_pred_alt
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
   570
  word_0_wi word_1_wi
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   571
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   572
lemma wi_homs:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   573
  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
   574
    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
   575
    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
   576
    and wi_hom_neg: "- word_of_int a = word_of_int (- a)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   577
    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
   578
    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
   579
  by (transfer, simp)+
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   580
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   581
lemmas wi_hom_syms = wi_homs [symmetric]
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   582
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   583
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi
46009
5cb7ef5bfef2 remove duplicate lemma lists
huffman
parents: 46001
diff changeset
   584
5cb7ef5bfef2 remove duplicate lemma lists
huffman
parents: 46001
diff changeset
   585
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
   586
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   587
instance word :: (len) comm_monoid_add ..
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   588
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   589
instance word :: (len) semiring_numeral ..
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   590
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   591
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
   592
  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
   593
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   594
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
   595
  apply (rule ext)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   596
  apply (case_tac x rule: int_diff_cases)
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   597
  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
   598
  done
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   599
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   600
lemma word_of_int_eq:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   601
  "word_of_int = of_int"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   602
  by (rule ext) (transfer, rule)
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   603
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   604
definition udvd :: "'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool" (infixl "udvd" 50)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   605
  where "a udvd b = (\<exists>n\<ge>0. uint b = n * uint a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   606
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   607
context
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   608
  includes lifting_syntax
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   609
begin
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   610
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   611
lemma [transfer_rule]:
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   612
  \<open>(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)\<close>
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   613
proof -
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   614
  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
   615
    for k :: int
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   616
  proof
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   617
    assume ?P
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   618
    then show ?Q
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   619
      by auto
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   620
  next
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   621
    assume ?Q
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   622
    then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" ..
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   623
    then have "even (take_bit LENGTH('a) k)"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   624
      by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   625
    then show ?P
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   626
      by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   627
  qed
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   628
  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
   629
    transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   630
qed
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   631
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   632
end
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   633
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   634
instance word :: (len) semiring_modulo
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   635
proof
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   636
  show "a div b * b + a mod b = a" for a b :: "'a word"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   637
  proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   638
    fix k l :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   639
    define r :: int where "r = 2 ^ LENGTH('a)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   640
    then have r: "take_bit LENGTH('a) k = k mod r" for k
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   641
      by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   642
    have "k mod r = ((k mod r) div (l mod r) * (l mod r)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   643
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   644
      by (simp add: div_mult_mod_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   645
    also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   646
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   647
      by (simp add: mod_add_left_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   648
    also have "... = (((k mod r) div (l mod r) * l) mod r
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   649
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   650
      by (simp add: mod_mult_right_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   651
    finally have "k mod r = ((k mod r) div (l mod r) * l
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   652
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   653
      by (simp add: mod_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   654
    with r show "take_bit LENGTH('a) (take_bit LENGTH('a) k div take_bit LENGTH('a) l * l
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   655
      + take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   656
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   657
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   658
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   659
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   660
instance word :: (len) semiring_parity
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   661
proof
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   662
  show "\<not> 2 dvd (1::'a word)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   663
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   664
  show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   665
    for a :: "'a word"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   666
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   667
  show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   668
    for a :: "'a word"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   669
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   670
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   671
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   672
lemma exp_eq_zero_iff:
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   673
  \<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close>
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   674
  by transfer simp
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   675
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   676
lemma double_eq_zero_iff:
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   677
  \<open>2 * a = 0 \<longleftrightarrow> a = 0 \<or> a = 2 ^ (LENGTH('a) - Suc 0)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   678
  for a :: \<open>'a::len word\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   679
proof -
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   680
  define n where \<open>n = LENGTH('a) - Suc 0\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   681
  then have *: \<open>LENGTH('a) = Suc n\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   682
    by simp
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   683
  have \<open>a = 0\<close> if \<open>2 * a = 0\<close> and \<open>a \<noteq> 2 ^ (LENGTH('a) - Suc 0)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   684
    using that by transfer
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   685
      (auto simp add: take_bit_eq_0_iff take_bit_eq_mod *)
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   686
  moreover have \<open>2 ^ LENGTH('a) = (0 :: 'a word)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   687
    by transfer simp
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   688
  then have \<open>2 * 2 ^ (LENGTH('a) - Suc 0) = (0 :: 'a word)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   689
    by (simp add: *)
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   690
  ultimately show ?thesis
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   691
    by auto
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   692
qed
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   693
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   694
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   695
subsection \<open>Ordering\<close>
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   696
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   697
instantiation word :: (len) linorder
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   698
begin
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   699
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   700
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   701
  is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   702
  by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   703
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   704
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   705
  is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   706
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   707
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   708
instance
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   709
  by (standard; transfer) auto
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   710
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   711
end
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   712
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   713
interpretation word_order: ordering_top \<open>(\<le>)\<close> \<open>(<)\<close> \<open>- 1 :: 'a::len word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   714
  by (standard; transfer) (simp add: take_bit_eq_mod zmod_minus1)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   715
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   716
interpretation word_coorder: ordering_top \<open>(\<ge>)\<close> \<open>(>)\<close> \<open>0 :: 'a::len word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   717
  by (standard; transfer) simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   718
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   719
lemma word_le_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   720
  "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   721
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   722
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   723
lemma word_less_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   724
  "a < b \<longleftrightarrow> uint a < uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   725
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   726
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   727
lemma word_greater_zero_iff:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   728
  \<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len word\<close>
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   729
  by transfer (simp add: less_le)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   730
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   731
lemma of_nat_word_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   732
  \<open>of_nat m = (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   733
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   734
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   735
lemma of_nat_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   736
  \<open>of_nat m \<le> (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   737
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   738
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   739
lemma of_nat_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   740
  \<open>of_nat m < (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   741
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   742
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   743
lemma of_nat_word_eq_0_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   744
  \<open>of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   745
  using of_nat_word_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   746
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   747
lemma of_int_word_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   748
  \<open>of_int k = (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   749
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   750
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   751
lemma of_int_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   752
  \<open>of_int k \<le> (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   753
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   754
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   755
lemma of_int_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   756
  \<open>of_int k < (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   757
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   758
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   759
lemma of_int_word_eq_0_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   760
  \<open>of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   761
  using of_int_word_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   762
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   763
lift_definition word_sle :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close>  (\<open>(_/ <=s _)\<close> [50, 51] 50)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   764
  is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - 1) k \<le> signed_take_bit (LENGTH('a) - 1) l\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   765
  by (simp flip: signed_take_bit_decr_length_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   766
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   767
lemma word_sle_eq [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   768
  \<open>a <=s b \<longleftrightarrow> sint a \<le> sint b\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   769
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   770
  
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   771
lift_definition word_sless :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close>  (\<open>(_/ <s _)\<close> [50, 51] 50)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   772
  is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - 1) k < signed_take_bit (LENGTH('a) - 1) l\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   773
  by (simp flip: signed_take_bit_decr_length_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   774
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   775
lemma word_sless_eq:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   776
  \<open>x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   777
  by transfer (simp add: signed_take_bit_decr_length_iff less_le)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   778
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   779
lemma [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   780
  \<open>a <s b \<longleftrightarrow> sint a < sint b\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   781
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   782
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   783
lemma word_less_alt: "a < b \<longleftrightarrow> uint a < uint b"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   784
  by (fact word_less_def)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   785
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   786
lemma signed_linorder: "class.linorder word_sle word_sless"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   787
  by (standard; transfer) (auto simp add: signed_take_bit_decr_length_iff)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   788
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   789
interpretation signed: linorder "word_sle" "word_sless"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   790
  by (rule signed_linorder)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   791
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   792
lemma word_zero_le [simp]: "0 \<le> y"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   793
  for y :: "'a::len word"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   794
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   795
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   796
lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   797
  by transfer (simp add: take_bit_minus_one_eq_mask mask_eq_exp_minus_1 bintr_lt2p)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   798
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   799
lemma word_n1_ge [simp]: "y \<le> -1"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   800
  for y :: "'a::len word"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   801
  by (fact word_order.extremum)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   802
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   803
lemmas word_not_simps [simp] =
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   804
  word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   805
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   806
lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   807
  for y :: "'a::len word"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   808
  by (simp add: less_le)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   809
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   810
lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   811
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   812
lemma word_sless_alt: "a <s b \<longleftrightarrow> sint a < sint b"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   813
  by (auto simp add: word_sle_eq word_sless_eq less_le)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   814
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   815
lemma word_le_nat_alt: "a \<le> b \<longleftrightarrow> unat a \<le> unat b"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   816
  by transfer (simp add: nat_le_eq_zle)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   817
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   818
lemma word_less_nat_alt: "a < b \<longleftrightarrow> unat a < unat b"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   819
  by transfer (auto simp add: less_le [of 0])
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   820
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   821
lemmas unat_mono = word_less_nat_alt [THEN iffD1]
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   822
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   823
instance word :: (len) wellorder
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   824
proof
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   825
  fix P :: "'a word \<Rightarrow> bool" and a
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   826
  assume *: "(\<And>b. (\<And>a. a < b \<Longrightarrow> P a) \<Longrightarrow> P b)"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   827
  have "wf (measure unat)" ..
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   828
  moreover have "{(a, b :: ('a::len) word). a < b} \<subseteq> measure unat"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   829
    by (auto simp add: word_less_nat_alt)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   830
  ultimately have "wf {(a, b :: ('a::len) word). a < b}"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   831
    by (rule wf_subset)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   832
  then show "P a" using *
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   833
    by induction blast
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   834
qed
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   835
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   836
lemma wi_less:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   837
  "(word_of_int n < (word_of_int m :: 'a::len word)) =
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   838
    (n mod 2 ^ LENGTH('a) < m mod 2 ^ LENGTH('a))"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   839
  unfolding word_less_alt by (simp add: word_uint.eq_norm)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   840
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   841
lemma wi_le:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   842
  "(word_of_int n \<le> (word_of_int m :: 'a::len word)) =
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   843
    (n mod 2 ^ LENGTH('a) \<le> m mod 2 ^ LENGTH('a))"
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   844
  unfolding word_le_def by (simp add: word_uint.eq_norm)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   845
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   846
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   847
subsection \<open>Bit-wise operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   848
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   849
lemma word_bit_induct [case_names zero even odd]:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   850
  \<open>P a\<close> if word_zero: \<open>P 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   851
    and word_even: \<open>\<And>a. P a \<Longrightarrow> 0 < a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (2 * a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   852
    and word_odd: \<open>\<And>a. P a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (1 + 2 * a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   853
  for P and a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   854
proof -
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   855
  define m :: nat where \<open>m = LENGTH('a) - 1\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   856
  then have l: \<open>LENGTH('a) = Suc m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   857
    by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   858
  define n :: nat where \<open>n = unat a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   859
  then have \<open>n < 2 ^ LENGTH('a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   860
    by (unfold unat_def) (transfer, simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   861
  then have \<open>n < 2 * 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   862
    by (simp add: l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   863
  then have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   864
  proof (induction n rule: nat_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   865
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   866
    show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   867
      by simp (rule word_zero)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   868
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   869
    case (even n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   870
    then have \<open>n < 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   871
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   872
    with even.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   873
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   874
    moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   875
      by (auto simp add: word_greater_zero_iff of_nat_word_eq_0_iff l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   876
    moreover from \<open>n < 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   877
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   878
      by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   879
    ultimately have \<open>P (2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   880
      by (rule word_even)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   881
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   882
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   883
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   884
    case (odd n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   885
    then have \<open>Suc n \<le> 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   886
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   887
    with odd.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   888
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   889
    moreover from \<open>Suc n \<le> 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   890
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   891
      by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   892
    ultimately have \<open>P (1 + 2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   893
      by (rule word_odd)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   894
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   895
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   896
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   897
  moreover have \<open>of_nat (nat (uint a)) = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   898
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   899
  ultimately show ?thesis
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   900
    by (simp add: n_def)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   901
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   902
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   903
lemma bit_word_half_eq:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   904
  \<open>(of_bool b + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   905
    if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   906
    for a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   907
proof (cases \<open>2 \<le> LENGTH('a::len)\<close>)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   908
  case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   909
  have \<open>of_bool (odd k) < (1 :: int) \<longleftrightarrow> even k\<close> for k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   910
    by auto
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   911
  with False that show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   912
    by transfer (simp add: eq_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   913
next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   914
  case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   915
  obtain n where length: \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   916
    by (cases \<open>LENGTH('a)\<close>) simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   917
  show ?thesis proof (cases b)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   918
    case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   919
    moreover have \<open>a * 2 div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   920
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   921
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   922
      from length have \<open>k * 2 mod 2 ^ LENGTH('a) = (k mod 2 ^ n) * 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   923
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   924
      moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   925
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   926
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   927
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   928
      ultimately have \<open>take_bit LENGTH('a) (k * 2) = take_bit LENGTH('a) k * 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   929
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   930
      with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (k * 2) div take_bit LENGTH('a) 2)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   931
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   932
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   933
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   934
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   935
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   936
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   937
    case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   938
    moreover have \<open>(1 + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   939
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   940
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   941
      from length have \<open>(1 + k * 2) mod 2 ^ LENGTH('a) = 1 + (k mod 2 ^ n) * 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   942
        using pos_zmod_mult_2 [of \<open>2 ^ n\<close> k] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   943
      moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   944
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   945
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   946
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   947
      ultimately have \<open>take_bit LENGTH('a) (1 + k * 2) = 1 + take_bit LENGTH('a) k * 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   948
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   949
      with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (1 + k * 2) div take_bit LENGTH('a) 2)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   950
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   951
        by (auto simp add: take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   952
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   953
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   954
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   955
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   956
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   957
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   958
lemma even_mult_exp_div_word_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   959
  \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> (
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   960
    m \<le> n \<and>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   961
    n < LENGTH('a) \<and> odd (a div 2 ^ (n - m)))\<close> for a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   962
  by transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   963
    (auto simp flip: drop_bit_eq_div simp add: even_drop_bit_iff_not_bit bit_take_bit_iff,
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   964
      simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   965
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   966
instantiation word :: (len) semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   967
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   968
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   969
lift_definition bit_word :: \<open>'a word \<Rightarrow> nat \<Rightarrow> bool\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   970
  is \<open>\<lambda>k n. n < LENGTH('a) \<and> bit k n\<close>
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   971
proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   972
  fix k l :: int and n :: nat
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   973
  assume *: \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   974
  show \<open>n < LENGTH('a) \<and> bit k n \<longleftrightarrow> n < LENGTH('a) \<and> bit l n\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   975
  proof (cases \<open>n < LENGTH('a)\<close>)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   976
    case True
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   977
    from * have \<open>bit (take_bit LENGTH('a) k) n \<longleftrightarrow> bit (take_bit LENGTH('a) l) n\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   978
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   979
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   980
      by (simp add: bit_take_bit_iff)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   981
  next
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   982
    case False
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   983
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   984
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   985
  qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   986
qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   987
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   988
instance proof
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   989
  show \<open>P a\<close> if stable: \<open>\<And>a. a div 2 = a \<Longrightarrow> P a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   990
    and rec: \<open>\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   991
  for P and a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   992
  proof (induction a rule: word_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   993
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   994
    have \<open>0 div 2 = (0::'a word)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   995
      by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   996
    with stable [of 0] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   997
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   998
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   999
    case (even a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1000
    with rec [of a False] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1001
      using bit_word_half_eq [of a False] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1002
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1003
    case (odd a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1004
    with rec [of a True] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1005
      using bit_word_half_eq [of a True] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1006
  qed
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1007
  show \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close> for a :: \<open>'a word\<close> and n
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1008
    by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit bit_iff_odd_drop_bit)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1009
  show \<open>0 div a = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1010
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1011
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1012
  show \<open>a div 1 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1013
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1014
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1015
  show \<open>a mod b div b = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1016
    for a b :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1017
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1018
    apply (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1019
    apply (subst (3) mod_pos_pos_trivial [of _ \<open>2 ^ LENGTH('a)\<close>])
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1020
      apply simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1021
     apply (metis le_less mod_by_0 pos_mod_conj zero_less_numeral zero_less_power)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1022
    using pos_mod_bound [of \<open>2 ^ LENGTH('a)\<close>] apply simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1023
  proof -
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1024
    fix aa :: int and ba :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1025
    have f1: "\<And>i n. (i::int) mod 2 ^ n = 0 \<or> 0 < i mod 2 ^ n"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1026
      by (metis le_less take_bit_eq_mod take_bit_nonnegative)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1027
    have "(0::int) < 2 ^ len_of (TYPE('a)::'a itself) \<and> ba mod 2 ^ len_of (TYPE('a)::'a itself) \<noteq> 0 \<or> aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1028
      by (metis (no_types) mod_by_0 unique_euclidean_semiring_numeral_class.pos_mod_bound zero_less_numeral zero_less_power)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1029
    then show "aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1030
      using f1 by (meson le_less less_le_trans unique_euclidean_semiring_numeral_class.pos_mod_bound)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1031
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1032
  show \<open>(1 + a) div 2 = a div 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1033
    if \<open>even a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1034
    for a :: \<open>'a word\<close>
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  1035
    using that by transfer
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  1036
      (auto dest: le_Suc_ex simp add: mod_2_eq_odd take_bit_Suc elim!: evenE)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1037
  show \<open>(2 :: 'a word) ^ m div 2 ^ n = of_bool ((2 :: 'a word) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m - n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1038
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1039
    by transfer (simp, simp add: exp_div_exp_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1040
  show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1041
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1042
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1043
    apply (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: drop_bit_eq_div)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1044
    apply (simp add: drop_bit_take_bit)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1045
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1046
  show "a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1047
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1048
    by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1049
  show \<open>a * 2 ^ m mod 2 ^ n = a mod 2 ^ (n - m) * 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1050
    if \<open>m \<le> n\<close> for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1051
    using that apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1052
    apply (auto simp flip: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1053
           apply (auto simp flip: push_bit_eq_mult simp add: push_bit_take_bit split: split_min_lin)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1054
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1055
  show \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1056
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1057
    by transfer (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: take_bit_eq_mod drop_bit_eq_div split: split_min_lin)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1058
  show \<open>even ((2 ^ m - 1) div (2::'a word) ^ n) \<longleftrightarrow> 2 ^ n = (0::'a word) \<or> m \<le> n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1059
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1060
    by transfer (auto simp add: take_bit_of_mask even_mask_div_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1061
  show \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::'a word) ^ n = 0 \<or> m \<le> n \<and> even (a div 2 ^ (n - m))\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1062
    for a :: \<open>'a word\<close> and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1063
  proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1064
    show \<open>even (take_bit LENGTH('a) (k * 2 ^ m) div take_bit LENGTH('a) (2 ^ n)) \<longleftrightarrow>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1065
      n < m
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1066
      \<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1067
      \<or> (m \<le> n \<and> even (take_bit LENGTH('a) k div take_bit LENGTH('a) (2 ^ (n - m))))\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1068
    for m n :: nat and k l :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1069
      by (auto simp flip: take_bit_eq_mod drop_bit_eq_div push_bit_eq_mult
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1070
        simp add: div_push_bit_of_1_eq_drop_bit drop_bit_take_bit drop_bit_push_bit_int [of n m])
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1071
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1072
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1073
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1074
end
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
  1075
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1076
instantiation word :: (len) semiring_bit_shifts
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1077
begin
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1078
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1079
lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1080
  is push_bit
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1081
proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1082
  show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1083
    if \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> for k l :: int and n :: nat
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1084
  proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1085
    from that
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1086
    have \<open>take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1087
      = take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1088
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1089
    moreover have \<open>min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1090
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1091
    ultimately show ?thesis
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1092
      by (simp add: take_bit_push_bit)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1093
  qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1094
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1095
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1096
lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1097
  is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1098
  by (simp add: take_bit_eq_mod)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1099
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1100
lift_definition take_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1101
  is \<open>\<lambda>n. take_bit (min LENGTH('a) n)\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1102
  by (simp add: ac_simps) (simp only: flip: take_bit_take_bit)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1103
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1104
instance proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1105
  show \<open>push_bit n a = a * 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close>
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1106
    by transfer (simp add: push_bit_eq_mult)
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1107
  show \<open>drop_bit n a = a div 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close>
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1108
    by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit)
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1109
  show \<open>take_bit n a = a mod 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1110
    by transfer (auto simp flip: take_bit_eq_mod)
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1111
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1112
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1113
end
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1114
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
  1115
lemma bit_word_eqI:
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1116
  \<open>a = b\<close> if \<open>\<And>n. n < LENGTH('a) \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1117
  for a b :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1118
  using that by transfer (auto simp add: nat_less_le bit_eq_iff bit_take_bit_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1119
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1120
lemma bit_imp_le_length:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1121
  \<open>n < LENGTH('a)\<close> if \<open>bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1122
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1123
  using that by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1124
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1125
lemma not_bit_length [simp]:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1126
  \<open>\<not> bit w LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1127
  by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1128
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1129
lemma uint_take_bit_eq [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1130
  \<open>uint (take_bit n w) = take_bit n (uint w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1131
  by transfer (simp add: ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1132
72227
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  1133
lemma take_bit_word_eq_self:
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  1134
  \<open>take_bit n w = w\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  1135
  using that by transfer simp
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  1136
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  1137
lemma take_bit_length_eq [simp]:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  1138
  \<open>take_bit LENGTH('a) w = w\<close> for w :: \<open>'a::len word\<close>
72227
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  1139
  by (rule take_bit_word_eq_self) simp
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  1140
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1141
lemma bit_word_of_int_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1142
  \<open>bit (word_of_int k :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> bit k n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1143
  by transfer rule
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1144
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1145
lemma bit_uint_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1146
  \<open>bit (uint w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1147
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1148
  by transfer (simp add: bit_take_bit_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1149
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1150
lemma bit_sint_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1151
  \<open>bit (sint w) n \<longleftrightarrow> n \<ge> LENGTH('a) \<and> bit w (LENGTH('a) - 1) \<or> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1152
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1153
  by transfer (auto simp add: bit_signed_take_bit_iff min_def le_less not_less)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1154
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1155
lemma bit_word_ucast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1156
  \<open>bit (ucast w :: 'b::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < LENGTH('b) \<and> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1157
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1158
  by transfer (simp add: bit_take_bit_iff ac_simps)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1159
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1160
lemma bit_word_scast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1161
  \<open>bit (scast w :: 'b::len word) n \<longleftrightarrow>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1162
    n < LENGTH('b) \<and> (bit w n \<or> LENGTH('a) \<le> n \<and> bit w (LENGTH('a) - Suc 0))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1163
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1164
  by transfer (auto simp add: bit_signed_take_bit_iff le_less min_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1165
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1166
lift_definition shiftl1 :: \<open>'a::len word \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1167
  is \<open>(*) 2\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1168
  by (auto simp add: take_bit_eq_mod intro: mod_mult_cong)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1169
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1170
lemma shiftl1_eq:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1171
  \<open>shiftl1 w = word_of_int (2 * uint w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1172
  by transfer (simp add: take_bit_eq_mod mod_simps)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1173
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1174
lemma shiftl1_eq_mult_2:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1175
  \<open>shiftl1 = (*) 2\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1176
  by (rule ext, transfer) simp
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1177
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1178
lemma bit_shiftl1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1179
  \<open>bit (shiftl1 w) n \<longleftrightarrow> 0 < n \<and> n < LENGTH('a) \<and> bit w (n - 1)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1180
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1181
  by (simp add: shiftl1_eq_mult_2 bit_double_iff exp_eq_zero_iff not_le) (simp add: ac_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1182
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1183
lift_definition shiftr1 :: \<open>'a::len word \<Rightarrow> 'a word\<close>
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1184
  \<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1185
  is \<open>\<lambda>k. take_bit LENGTH('a) k div 2\<close> by simp
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1186
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1187
lemma shiftr1_eq_div_2:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1188
  \<open>shiftr1 w = w div 2\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1189
  by transfer simp
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1190
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1191
lemma bit_shiftr1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1192
  \<open>bit (shiftr1 w) n \<longleftrightarrow> bit w (Suc n)\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1193
  by transfer (auto simp flip: bit_Suc simp add: bit_take_bit_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1194
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1195
lemma shiftr1_eq:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1196
  \<open>shiftr1 w = word_of_int (uint w div 2)\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1197
  by transfer simp
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1198
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1199
instantiation word :: (len) ring_bit_operations
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1200
begin
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1201
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1202
lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1203
  is not
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1204
  by (simp add: take_bit_not_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1205
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1206
lift_definition and_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1207
  is \<open>and\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1208
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1209
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1210
lift_definition or_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1211
  is or
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1212
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1213
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1214
lift_definition xor_word ::  \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1215
  is xor
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1216
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1217
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1218
lift_definition mask_word :: \<open>nat \<Rightarrow> 'a word\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1219
  is mask
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1220
  .
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1221
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1222
instance by (standard; transfer)
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1223
  (auto simp add: minus_eq_not_minus_1 mask_eq_exp_minus_1
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1224
    bit_not_iff bit_and_iff bit_or_iff bit_xor_iff)
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1225
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1226
end
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1227
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1228
context
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1229
  includes lifting_syntax
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1230
begin
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1231
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1232
lemma set_bit_word_transfer [transfer_rule]:
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1233
  \<open>((=) ===> pcr_word ===> pcr_word) set_bit set_bit\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1234
  by (unfold set_bit_def) transfer_prover
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1235
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1236
lemma unset_bit_word_transfer [transfer_rule]:
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1237
  \<open>((=) ===> pcr_word ===> pcr_word) unset_bit unset_bit\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1238
  by (unfold unset_bit_def) transfer_prover
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1239
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1240
lemma flip_bit_word_transfer [transfer_rule]:
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1241
  \<open>((=) ===> pcr_word ===> pcr_word) flip_bit flip_bit\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1242
  by (unfold flip_bit_def) transfer_prover
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1243
72242
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1244
lemma signed_take_bit_word_transfer [transfer_rule]:
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1245
  \<open>((=) ===> pcr_word ===> pcr_word)
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1246
    (\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k))
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1247
    (signed_take_bit :: nat \<Rightarrow> 'a word \<Rightarrow> 'a word)\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1248
proof -
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1249
  let ?K = \<open>\<lambda>n (k :: int). take_bit (min LENGTH('a) n) k OR of_bool (n < LENGTH('a) \<and> bit k n) * NOT (mask n)\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1250
  let ?W = \<open>\<lambda>n (w :: 'a word). take_bit n w OR of_bool (bit w n) * NOT (mask n)\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1251
  have \<open>((=) ===> pcr_word ===> pcr_word) ?K ?W\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1252
    by transfer_prover
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1253
  also have \<open>?K = (\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k))\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1254
    by (simp add: fun_eq_iff signed_take_bit_def bit_take_bit_iff ac_simps)
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1255
  also have \<open>?W = signed_take_bit\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1256
    by (simp add: fun_eq_iff signed_take_bit_def)
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1257
  finally show ?thesis .
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1258
qed
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1259
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1260
end
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1261
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  1262
instantiation word :: (len) semiring_bit_syntax
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1263
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1264
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1265
lift_definition test_bit_word :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> bool\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1266
  is \<open>\<lambda>k n. n < LENGTH('a) \<and> bit k n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1267
proof
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1268
  fix k l :: int and n :: nat
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1269
  assume *: \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1270
  show \<open>n < LENGTH('a) \<and> bit k n \<longleftrightarrow> n < LENGTH('a) \<and> bit l n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1271
  proof (cases \<open>n < LENGTH('a)\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1272
    case True
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1273
    from * have \<open>bit (take_bit LENGTH('a) k) n \<longleftrightarrow> bit (take_bit LENGTH('a) l) n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1274
      by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1275
    then show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1276
      by (simp add: bit_take_bit_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1277
  next
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1278
    case False
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1279
    then show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1280
      by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1281
  qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1282
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1283
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1284
lemma test_bit_word_eq:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1285
  \<open>test_bit = (bit :: 'a word \<Rightarrow> _)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1286
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1287
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1288
lemma bit_word_iff_drop_bit_and [code]:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1289
  \<open>bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> for a :: \<open>'a::len word\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1290
  by (simp add: bit_iff_odd_drop_bit odd_iff_mod_2_eq_one and_one_eq)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1291
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1292
lemma [code]:
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1293
  \<open>test_bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> for a :: \<open>'a::len word\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1294
  by (simp add: test_bit_word_eq bit_word_iff_drop_bit_and)
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1295
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1296
lift_definition shiftl_word :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1297
  is \<open>\<lambda>k n. push_bit n k\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1298
proof -
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1299
  show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1300
    if \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> for k l :: int and n :: nat
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1301
  proof -
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1302
    from that
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1303
    have \<open>take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1304
      = take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1305
      by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1306
    moreover have \<open>min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1307
      by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1308
    ultimately show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1309
      by (simp add: take_bit_push_bit)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1310
  qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1311
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1312
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1313
lemma shiftl_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1314
  \<open>w << n = push_bit n w\<close> for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1315
  by transfer rule
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1316
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1317
lift_definition shiftr_word :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1318
  is \<open>\<lambda>k n. drop_bit n (take_bit LENGTH('a) k)\<close> by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1319
  
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  1320
lemma shiftr_word_eq:
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  1321
  \<open>w >> n = drop_bit n w\<close> for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1322
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1323
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1324
instance
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset