src/HOL/Word/Word.thy
author haftmann
Thu, 17 Sep 2020 09:57:31 +0000
changeset 72262 a282abb07642
parent 72261 5193570b739a
child 72265 ff32ddc8165c
permissions -rw-r--r--
integrated generic conversions into word corpse
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
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    27
subsection \<open>Fundamentals\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    28
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    29
subsubsection \<open>Type definition\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    30
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
    31
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
    32
  morphisms rep Word by (auto intro!: equivpI reflpI sympI transpI)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    33
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    34
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
    35
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
    36
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    37
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    38
subsubsection \<open>Basic arithmetic\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    39
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    40
instantiation word :: (len) comm_ring_1
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    41
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    42
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    43
lift_definition zero_word :: \<open>'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    44
  is 0 .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    45
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    46
lift_definition one_word :: \<open>'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    47
  is 1 .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    48
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    49
lift_definition plus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    50
  is \<open>(+)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    51
  by (auto simp add: take_bit_eq_mod intro: mod_add_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    52
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    53
lift_definition minus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    54
  is \<open>(-)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    55
  by (auto simp add: take_bit_eq_mod intro: mod_diff_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    56
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    57
lift_definition uminus_word :: \<open>'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    58
  is uminus
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    59
  by (auto simp add: take_bit_eq_mod intro: mod_minus_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    60
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    61
lift_definition times_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    62
  is \<open>(*)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    63
  by (auto simp add: take_bit_eq_mod intro: mod_mult_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    64
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    65
instance
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    66
  by (standard; transfer) (simp_all add: algebra_simps)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    67
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    68
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    69
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    70
context
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    71
  includes lifting_syntax
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    72
  notes
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    73
    power_transfer [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    74
    transfer_rule_of_bool [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    75
    transfer_rule_numeral [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    76
    transfer_rule_of_nat [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    77
    transfer_rule_of_int [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    78
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    79
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    80
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    81
lemma power_transfer_word [transfer_rule]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    82
  \<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    83
  by transfer_prover
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    84
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    85
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    86
  \<open>((=) ===> pcr_word) of_bool of_bool\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    87
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    88
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    89
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    90
  \<open>((=) ===> pcr_word) numeral numeral\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    91
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    92
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    93
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    94
  \<open>((=) ===> pcr_word) int of_nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    95
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    96
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    97
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    98
  \<open>((=) ===> pcr_word) (\<lambda>k. k) of_int\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    99
proof -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   100
  have \<open>((=) ===> pcr_word) of_int of_int\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   101
    by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   102
  then show ?thesis by (simp add: id_def)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   103
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   104
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   105
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   106
  \<open>(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   107
proof -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   108
  have even_word_unfold: "even k \<longleftrightarrow> (\<exists>l. take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l))" (is "?P \<longleftrightarrow> ?Q")
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   109
    for k :: int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   110
  proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   111
    assume ?P
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   112
    then show ?Q
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   113
      by auto
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   114
  next
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   115
    assume ?Q
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   116
    then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   117
    then have "even (take_bit LENGTH('a) k)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   118
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   119
    then show ?P
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   120
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   121
  qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   122
  show ?thesis by (simp only: even_word_unfold [abs_def] dvd_def [where ?'a = "'a word", abs_def])
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   123
    transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   124
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   125
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   126
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   127
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   128
lemma word_exp_length_eq_0 [simp]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   129
  \<open>(2 :: 'a::len word) ^ LENGTH('a) = 0\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   130
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   131
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   132
lemma exp_eq_zero_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   133
  \<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   134
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   135
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   136
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   137
subsubsection \<open>Basic code generation setup\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   138
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   139
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   140
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   141
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   142
qualified lift_definition the_int :: \<open>'a::len word \<Rightarrow> int\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   143
  is \<open>take_bit LENGTH('a)\<close> .
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   144
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   145
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   146
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   147
lemma [code abstype]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   148
  \<open>Word.Word (Word.the_int w) = w\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   149
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   150
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   151
lemma Word_eq_word_of_int [code_post, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   152
  \<open>Word.Word = of_int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   153
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   154
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   155
quickcheck_generator word
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   156
  constructors:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   157
    \<open>0 :: 'a::len word\<close>,
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   158
    \<open>numeral :: num \<Rightarrow> 'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   159
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   160
instantiation word :: (len) equal
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   161
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   162
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   163
lift_definition equal_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> bool\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   164
  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
   165
  by simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   166
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   167
instance
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   168
  by (standard; transfer) rule
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   169
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   170
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   171
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   172
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   173
  \<open>HOL.equal v w \<longleftrightarrow> HOL.equal (Word.the_int v) (Word.the_int w)\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   174
  by transfer (simp add: equal)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   175
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   176
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   177
  \<open>Word.the_int 0 = 0\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   178
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   179
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   180
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   181
  \<open>Word.the_int 1 = 1\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   182
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   183
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   184
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   185
  \<open>Word.the_int (v + w) = take_bit LENGTH('a) (Word.the_int v + Word.the_int w)\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   186
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   187
  by transfer (simp add: take_bit_add)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   188
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   189
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   190
  \<open>Word.the_int (- w) = (let k = Word.the_int w in if w = 0 then 0 else 2 ^ LENGTH('a) - k)\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   191
  for w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   192
  by transfer (auto simp add: take_bit_eq_mod zmod_zminus1_eq_if)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   193
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   194
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   195
  \<open>Word.the_int (v - w) = take_bit LENGTH('a) (Word.the_int v - Word.the_int w)\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   196
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   197
  by transfer (simp add: take_bit_diff)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   198
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   199
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   200
  \<open>Word.the_int (v * w) = take_bit LENGTH('a) (Word.the_int v * Word.the_int w)\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   201
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   202
  by transfer (simp add: take_bit_mult)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   203
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   204
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   205
subsubsection \<open>Basic conversions\<close>
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   206
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   207
abbreviation word_of_nat :: \<open>nat \<Rightarrow> 'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   208
  where \<open>word_of_nat \<equiv> of_nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   209
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   210
abbreviation word_of_int :: \<open>int \<Rightarrow> 'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   211
  where \<open>word_of_int \<equiv> of_int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   212
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   213
lemma word_of_nat_eq_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   214
  \<open>word_of_nat m = (word_of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   215
  by transfer (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   216
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   217
lemma word_of_int_eq_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   218
  \<open>word_of_int k = (word_of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   219
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   220
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   221
lemma word_of_nat_eq_0_iff [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   222
  \<open>word_of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   223
  using word_of_nat_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   224
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   225
lemma word_of_int_eq_0_iff [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   226
  \<open>word_of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   227
  using word_of_int_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   228
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   229
context semiring_1
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   230
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   231
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   232
lift_definition unsigned :: \<open>'b::len word \<Rightarrow> 'a\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   233
  is \<open>of_nat \<circ> nat \<circ> take_bit LENGTH('b)\<close>
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   234
  by simp
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   235
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   236
lemma unsigned_0 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   237
  \<open>unsigned 0 = 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   238
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   239
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   240
lemma unsigned_1 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   241
  \<open>unsigned 1 = 1\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   242
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   243
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   244
lemma unsigned_numeral [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   245
  \<open>unsigned (numeral n :: 'b::len word) = of_nat (take_bit LENGTH('b) (numeral n))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   246
  by transfer (simp add: nat_take_bit_eq)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   247
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   248
lemma unsigned_neg_numeral [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   249
  \<open>unsigned (- numeral n :: 'b::len word) = of_nat (nat (take_bit LENGTH('b) (- numeral n)))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   250
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   251
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   252
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   253
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   254
context semiring_1
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   255
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   256
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   257
lemma unsigned_of_nat [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   258
  \<open>unsigned (word_of_nat n :: 'b::len word) = of_nat (take_bit LENGTH('b) n)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   259
  by transfer (simp add: nat_eq_iff take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   260
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   261
lemma unsigned_of_int [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   262
  \<open>unsigned (word_of_int k :: 'b::len word) = of_nat (nat (take_bit LENGTH('b) k))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   263
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   264
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   265
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   266
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   267
context semiring_char_0
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   268
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   269
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   270
lemma unsigned_word_eqI:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   271
  \<open>v = w\<close> if \<open>unsigned v = unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   272
  using that by transfer (simp add: eq_nat_nat_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   273
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   274
lemma word_eq_iff_unsigned:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   275
  \<open>v = w \<longleftrightarrow> unsigned v = unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   276
  by (auto intro: unsigned_word_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   277
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   278
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   279
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   280
context ring_1
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   281
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   282
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   283
lift_definition signed :: \<open>'b::len word \<Rightarrow> 'a\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   284
  is \<open>of_int \<circ> signed_take_bit (LENGTH('b) - Suc 0)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   285
  by (simp flip: signed_take_bit_decr_length_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   286
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   287
lemma signed_0 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   288
  \<open>signed 0 = 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   289
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   290
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   291
lemma signed_1 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   292
  \<open>signed (1 :: 'b::len word) = (if LENGTH('b) = 1 then - 1 else 1)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   293
  by (transfer fixing: uminus; cases \<open>LENGTH('b)\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   294
    (simp_all add: sbintrunc_minus_simps)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   295
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   296
lemma signed_minus_1 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   297
  \<open>signed (- 1 :: 'b::len word) = - 1\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   298
  by (transfer fixing: uminus) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   299
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   300
lemma signed_numeral [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   301
  \<open>signed (numeral n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - 1) (numeral n))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   302
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   303
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   304
lemma signed_neg_numeral [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   305
  \<open>signed (- numeral n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - 1) (- numeral n))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   306
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   307
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   308
lemma signed_of_nat [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   309
  \<open>signed (word_of_nat n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - Suc 0) (int n))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   310
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   311
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   312
lemma signed_of_int [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   313
  \<open>signed (word_of_int n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - Suc 0) n)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   314
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   315
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   316
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   317
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   318
context ring_char_0
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   319
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   320
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   321
lemma signed_word_eqI:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   322
  \<open>v = w\<close> if \<open>signed v = signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   323
  using that by transfer (simp flip: signed_take_bit_decr_length_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   324
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   325
lemma word_eq_iff_signed:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   326
  \<open>v = w \<longleftrightarrow> signed v = signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   327
  by (auto intro: signed_word_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   328
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   329
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   330
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   331
abbreviation unat :: \<open>'a::len word \<Rightarrow> nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   332
  where \<open>unat \<equiv> unsigned\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   333
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   334
abbreviation uint :: \<open>'a::len word \<Rightarrow> int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   335
  where \<open>uint \<equiv> unsigned\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   336
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   337
abbreviation sint :: \<open>'a::len word \<Rightarrow> int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   338
  where \<open>sint \<equiv> signed\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   339
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   340
abbreviation ucast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   341
  where \<open>ucast \<equiv> unsigned\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   342
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   343
abbreviation scast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   344
  where \<open>scast \<equiv> signed\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   345
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   346
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   347
  includes lifting_syntax
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   348
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   349
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   350
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   351
  \<open>(pcr_word ===> (=)) (nat \<circ> take_bit LENGTH('a)) (unat :: 'a::len word \<Rightarrow> nat)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   352
  using unsigned.transfer [where ?'a = nat] by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   353
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   354
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   355
  \<open>(pcr_word ===> (=)) (take_bit LENGTH('a)) (uint :: 'a::len word \<Rightarrow> int)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   356
  using unsigned.transfer [where ?'a = int] by (simp add: comp_def)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   357
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   358
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   359
  \<open>(pcr_word ===> (=)) (signed_take_bit (LENGTH('a) - Suc 0)) (sint :: 'a::len word \<Rightarrow> int)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   360
  using signed.transfer [where ?'a = int] by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   361
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   362
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   363
  \<open>(pcr_word ===> pcr_word) (take_bit LENGTH('a)) (ucast :: 'a::len word \<Rightarrow> 'b::len word)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   364
proof (rule rel_funI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   365
  fix k :: int and w :: \<open>'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   366
  assume \<open>pcr_word k w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   367
  then have \<open>w = word_of_int k\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   368
    by (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   369
  moreover have \<open>pcr_word (take_bit LENGTH('a) k) (ucast (word_of_int k :: 'a word))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   370
    by transfer (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   371
  ultimately show \<open>pcr_word (take_bit LENGTH('a) k) (ucast w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   372
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   373
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   374
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   375
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   376
  \<open>(pcr_word ===> pcr_word) (signed_take_bit (LENGTH('a) - Suc 0)) (scast :: 'a::len word \<Rightarrow> 'b::len word)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   377
proof (rule rel_funI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   378
  fix k :: int and w :: \<open>'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   379
  assume \<open>pcr_word k w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   380
  then have \<open>w = word_of_int k\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   381
    by (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   382
  moreover have \<open>pcr_word (signed_take_bit (LENGTH('a) - Suc 0) k) (scast (word_of_int k :: 'a word))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   383
    by transfer (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   384
  ultimately show \<open>pcr_word (signed_take_bit (LENGTH('a) - Suc 0) k) (scast w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   385
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   386
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   387
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   388
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   389
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   390
lemma of_nat_unat [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   391
  \<open>of_nat (unat w) = unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   392
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   393
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   394
lemma of_int_uint [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   395
  \<open>of_int (uint w) = unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   396
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   397
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   398
lemma of_int_sint [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   399
  \<open>of_int (sint a) = signed a\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   400
  by transfer (simp_all add: take_bit_signed_take_bit)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   401
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   402
lemma nat_uint_eq [simp]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   403
  \<open>nat (uint w) = unat w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   404
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   405
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   406
text \<open>Aliasses only for code generation\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   407
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   408
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   409
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   410
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   411
qualified lift_definition of_int :: \<open>int \<Rightarrow> 'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   412
  is \<open>take_bit LENGTH('a)\<close> .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   413
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   414
qualified lift_definition of_nat :: \<open>nat \<Rightarrow> 'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   415
  is \<open>int \<circ> take_bit LENGTH('a)\<close> .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   416
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   417
qualified lift_definition the_nat :: \<open>'a::len word \<Rightarrow> nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   418
  is \<open>nat \<circ> take_bit LENGTH('a)\<close> by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   419
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   420
qualified lift_definition the_signed_int :: \<open>'a::len word \<Rightarrow> int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   421
  is \<open>signed_take_bit (LENGTH('a) - Suc 0)\<close> by (simp add: signed_take_bit_decr_length_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   422
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   423
qualified lift_definition cast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   424
  is \<open>take_bit LENGTH('a)\<close> by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   425
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   426
qualified lift_definition signed_cast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   427
  is \<open>signed_take_bit (LENGTH('a) - Suc 0)\<close> by (metis signed_take_bit_decr_length_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   428
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   429
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   430
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   431
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   432
  \<open>Word.the_int = uint\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   433
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   434
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   435
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   436
  \<open>Word.the_int (Word.of_int k :: 'a::len word) = take_bit LENGTH('a) k\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   437
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   438
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   439
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   440
  \<open>Word.of_int = word_of_int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   441
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   442
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   443
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   444
  \<open>Word.the_int (Word.of_nat n :: 'a::len word) = take_bit LENGTH('a) (int n)\<close>
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   445
  by transfer (simp add: take_bit_of_nat)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   446
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   447
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   448
  \<open>Word.of_nat = word_of_nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   449
  by (rule; transfer) (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   450
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   451
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   452
  \<open>Word.the_nat w = nat (Word.the_int w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   453
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   454
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   455
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   456
  \<open>Word.the_nat = unat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   457
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   458
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   459
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   460
  \<open>Word.the_signed_int w = signed_take_bit (LENGTH('a) - Suc 0) (Word.the_int w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   461
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   462
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   463
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   464
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   465
  \<open>Word.the_signed_int = sint\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   466
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   467
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   468
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   469
  \<open>Word.the_int (Word.cast w :: 'b::len word) = take_bit LENGTH('b) (Word.the_int w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   470
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   471
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   472
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   473
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   474
  \<open>Word.cast = ucast\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   475
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   476
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   477
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   478
  \<open>Word.the_int (Word.signed_cast w :: 'b::len word) = take_bit LENGTH('b) (Word.the_signed_int w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   479
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   480
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   481
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   482
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   483
  \<open>Word.signed_cast = scast\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   484
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   485
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   486
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   487
  \<open>unsigned w = of_nat (nat (Word.the_int w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   488
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   489
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   490
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   491
  \<open>signed w = of_int (Word.the_signed_int w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   492
  by transfer simp
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   493
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   494
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   495
subsubsection \<open>Basic ordering\<close>
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   496
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
   497
instantiation word :: (len) linorder
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   498
begin
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   499
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   500
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   501
  is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   502
  by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   503
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   504
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   505
  is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   506
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   507
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   508
instance
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   509
  by (standard; transfer) auto
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   510
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   511
end
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   512
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   513
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
   514
  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
   515
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   516
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
   517
  by (standard; transfer) simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   518
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   519
lemma word_of_nat_less_eq_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   520
  \<open>word_of_nat m \<le> (word_of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   521
  by transfer (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   522
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   523
lemma word_of_int_less_eq_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   524
  \<open>word_of_int k \<le> (word_of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   525
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   526
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   527
lemma word_of_nat_less_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   528
  \<open>word_of_nat m < (word_of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   529
  by transfer (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   530
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   531
lemma word_of_int_less_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   532
  \<open>word_of_int k < (word_of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   533
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   534
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   535
lemma word_le_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   536
  "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   537
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   538
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   539
lemma word_less_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   540
  "a < b \<longleftrightarrow> uint a < uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   541
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   542
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   543
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
   544
  \<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
   545
  by transfer (simp add: less_le)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   546
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   547
lemma of_nat_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   548
  \<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
   549
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   550
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   551
lemma of_nat_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   552
  \<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
   553
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   554
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   555
lemma of_int_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   556
  \<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
   557
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   558
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   559
lemma of_int_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   560
  \<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
   561
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   562
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   563
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   564
subsection \<open>Bit-wise operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   565
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   566
instantiation word :: (len) semiring_modulo
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   567
begin
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   568
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   569
lift_definition divide_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   570
  is \<open>\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   571
  by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   572
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   573
lift_definition modulo_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   574
  is \<open>\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   575
  by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   576
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   577
instance proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   578
  show "a div b * b + a mod b = a" for a b :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   579
  proof transfer
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   580
    fix k l :: int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   581
    define r :: int where "r = 2 ^ LENGTH('a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   582
    then have r: "take_bit LENGTH('a) k = k mod r" for k
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   583
      by (simp add: take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   584
    have "k mod r = ((k mod r) div (l mod r) * (l mod r)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   585
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   586
      by (simp add: div_mult_mod_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   587
    also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   588
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   589
      by (simp add: mod_add_left_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   590
    also have "... = (((k mod r) div (l mod r) * l) mod r
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   591
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   592
      by (simp add: mod_mult_right_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   593
    finally have "k mod r = ((k mod r) div (l mod r) * l
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   594
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   595
      by (simp add: mod_simps)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   596
    with r show "take_bit LENGTH('a) (take_bit LENGTH('a) k div take_bit LENGTH('a) l * l
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   597
      + take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   598
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   599
  qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   600
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   601
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   602
end
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   603
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   604
instance word :: (len) semiring_parity
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   605
proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   606
  show "\<not> 2 dvd (1::'a word)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   607
    by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   608
  show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   609
    for a :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   610
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   611
  show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   612
    for a :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   613
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   614
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   615
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   616
lemma word_bit_induct [case_names zero even odd]:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   617
  \<open>P a\<close> if word_zero: \<open>P 0\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   618
    and word_even: \<open>\<And>a. P a \<Longrightarrow> 0 < a \<Longrightarrow> a < 2 ^ (LENGTH('a) - Suc 0) \<Longrightarrow> P (2 * a)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   619
    and word_odd: \<open>\<And>a. P a \<Longrightarrow> a < 2 ^ (LENGTH('a) - Suc 0) \<Longrightarrow> P (1 + 2 * a)\<close>
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   620
  for P and a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   621
proof -
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   622
  define m :: nat where \<open>m = LENGTH('a) - Suc 0\<close>
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   623
  then have l: \<open>LENGTH('a) = Suc m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   624
    by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   625
  define n :: nat where \<open>n = unat a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   626
  then have \<open>n < 2 ^ LENGTH('a)\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   627
    by transfer (simp add: take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   628
  then have \<open>n < 2 * 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   629
    by (simp add: l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   630
  then have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   631
  proof (induction n rule: nat_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   632
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   633
    show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   634
      by simp (rule word_zero)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   635
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   636
    case (even n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   637
    then have \<open>n < 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   638
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   639
    with even.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   640
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   641
    moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   642
      by (auto simp add: word_greater_zero_iff l)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   643
    moreover from \<open>n < 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - Suc 0)\<close>
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   644
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
   645
      by (simp add: l take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   646
    ultimately have \<open>P (2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   647
      by (rule word_even)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   648
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   649
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   650
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   651
    case (odd n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   652
    then have \<open>Suc n \<le> 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   653
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   654
    with odd.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   655
      by simp
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   656
    moreover from \<open>Suc n \<le> 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - Suc 0)\<close>
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   657
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
   658
      by (simp add: l take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   659
    ultimately have \<open>P (1 + 2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   660
      by (rule word_odd)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   661
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   662
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   663
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   664
  moreover have \<open>of_nat (nat (uint a)) = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   665
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   666
  ultimately show ?thesis
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   667
    by (simp add: n_def)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   668
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   669
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   670
lemma bit_word_half_eq:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   671
  \<open>(of_bool b + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   672
    if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   673
    for a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   674
proof (cases \<open>2 \<le> LENGTH('a::len)\<close>)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   675
  case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   676
  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
   677
    by auto
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   678
  with False that show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   679
    by transfer (simp add: eq_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   680
next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   681
  case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   682
  obtain n where length: \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   683
    by (cases \<open>LENGTH('a)\<close>) simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   684
  show ?thesis proof (cases b)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   685
    case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   686
    moreover have \<open>a * 2 div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   687
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   688
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   689
      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
   690
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   691
      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
   692
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   693
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   694
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   695
      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
   696
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   697
      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
   698
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   699
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   700
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   701
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   702
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   703
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   704
    case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   705
    moreover have \<open>(1 + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   706
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   707
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   708
      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
   709
        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
   710
      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
   711
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   712
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   713
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   714
      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
   715
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   716
      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
   717
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   718
        by (auto simp add: take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   719
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   720
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   721
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   722
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   723
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   724
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   725
lemma even_mult_exp_div_word_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   726
  \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> (
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   727
    m \<le> n \<and>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   728
    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
   729
  by transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   730
    (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
   731
      simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   732
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   733
instantiation word :: (len) semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   734
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   735
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   736
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
   737
  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
   738
proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   739
  fix k l :: int and n :: nat
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   740
  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
   741
  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
   742
  proof (cases \<open>n < LENGTH('a)\<close>)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   743
    case True
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   744
    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
   745
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   746
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   747
      by (simp add: bit_take_bit_iff)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   748
  next
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   749
    case False
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   750
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   751
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   752
  qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   753
qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   754
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   755
instance proof
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   756
  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
   757
    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
   758
  for P and a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   759
  proof (induction a rule: word_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   760
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   761
    have \<open>0 div 2 = (0::'a word)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   762
      by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   763
    with stable [of 0] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   764
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   765
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   766
    case (even a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   767
    with rec [of a False] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   768
      using bit_word_half_eq [of a False] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   769
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   770
    case (odd a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   771
    with rec [of a True] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   772
      using bit_word_half_eq [of a True] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   773
  qed
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   774
  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
   775
    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
   776
  show \<open>0 div a = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   777
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   778
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   779
  show \<open>a div 1 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   780
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   781
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   782
  show \<open>a mod b div b = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   783
    for a b :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   784
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   785
    apply (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   786
    apply (subst (3) mod_pos_pos_trivial [of _ \<open>2 ^ LENGTH('a)\<close>])
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   787
      apply simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   788
     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
   789
    using pos_mod_bound [of \<open>2 ^ LENGTH('a)\<close>] apply simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   790
  proof -
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   791
    fix aa :: int and ba :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   792
    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
   793
      by (metis le_less take_bit_eq_mod take_bit_nonnegative)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   794
    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
   795
      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
   796
    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
   797
      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
   798
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   799
  show \<open>(1 + a) div 2 = a div 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   800
    if \<open>even a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   801
    for a :: \<open>'a word\<close>
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   802
    using that by transfer
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   803
      (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
   804
  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
   805
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   806
    by transfer (simp, simp add: exp_div_exp_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   807
  show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   808
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   809
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   810
    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
   811
    apply (simp add: drop_bit_take_bit)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   812
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   813
  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
   814
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   815
    by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   816
  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
   817
    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
   818
    using that apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   819
    apply (auto simp flip: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   820
           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
   821
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   822
  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
   823
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   824
    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
   825
  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
   826
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   827
    by transfer (auto simp add: take_bit_of_mask even_mask_div_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   828
  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
   829
    for a :: \<open>'a word\<close> and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   830
  proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   831
    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
   832
      n < m
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   833
      \<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   834
      \<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
   835
    for m n :: nat and k l :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   836
      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
   837
        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
   838
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   839
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   840
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   841
end
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   842
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   843
lemma bit_word_eqI:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   844
  \<open>a = b\<close> if \<open>\<And>n. n < LENGTH('a) \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   845
  for a b :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   846
  using that by transfer (auto simp add: nat_less_le bit_eq_iff bit_take_bit_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   847
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   848
lemma bit_imp_le_length:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   849
  \<open>n < LENGTH('a)\<close> if \<open>bit w n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   850
    for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   851
  using that by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   852
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   853
lemma not_bit_length [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   854
  \<open>\<not> bit w LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   855
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   856
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   857
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
   858
begin
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   859
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   860
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
   861
  is push_bit
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   862
proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   863
  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
   864
    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
   865
  proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   866
    from that
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   867
    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
   868
      = 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
   869
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   870
    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
   871
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   872
    ultimately show ?thesis
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   873
      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
   874
  qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   875
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   876
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   877
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
   878
  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
   879
  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
   880
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   881
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
   882
  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
   883
  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
   884
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   885
instance proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   886
  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
   887
    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
   888
  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
   889
    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
   890
  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
   891
    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
   892
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   893
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   894
end
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   895
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   896
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   897
  \<open>Word.the_int (take_bit n w) = (if n < LENGTH('a::len) then take_bit n (Word.the_int w) else Word.the_int w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   898
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   899
  by transfer (simp add: not_le not_less ac_simps min_absorb2)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   900
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   901
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   902
instantiation word :: (len) ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   903
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   904
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   905
lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   906
  is not
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   907
  by (simp add: take_bit_not_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   908
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   909
lift_definition and_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   910
  is \<open>and\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   911
  by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   912
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   913
lift_definition or_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   914
  is or
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   915
  by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   916
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   917
lift_definition xor_word ::  \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   918
  is xor
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   919
  by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   920
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   921
lift_definition mask_word :: \<open>nat \<Rightarrow> 'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   922
  is mask
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   923
  .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   924
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   925
instance by (standard; transfer)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   926
  (auto simp add: minus_eq_not_minus_1 mask_eq_exp_minus_1
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   927
    bit_not_iff bit_and_iff bit_or_iff bit_xor_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   928
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   929
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   930
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   931
lemma [code_abbrev]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   932
  \<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   933
  by (fact push_bit_of_1)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   934
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   935
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   936
  \<open>NOT w = Word.of_int (NOT (Word.the_int w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   937
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   938
  by transfer (simp add: take_bit_not_take_bit) 
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   939
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   940
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   941
  \<open>Word.the_int (v AND w) = Word.the_int v AND Word.the_int w\<close>
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   942
  by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   943
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   944
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   945
  \<open>Word.the_int (v OR w) = Word.the_int v OR Word.the_int w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   946
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   947
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   948
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   949
  \<open>Word.the_int (v XOR w) = Word.the_int v XOR Word.the_int w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   950
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   951
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   952
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   953
  \<open>Word.the_int (mask n :: 'a::len word) = mask (min LENGTH('a) n)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   954
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   955
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   956
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   957
  includes lifting_syntax
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   958
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   959
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   960
lemma set_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   961
  \<open>((=) ===> pcr_word ===> pcr_word) set_bit set_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   962
  by (unfold set_bit_def) transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   963
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   964
lemma unset_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   965
  \<open>((=) ===> pcr_word ===> pcr_word) unset_bit unset_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   966
  by (unfold unset_bit_def) transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   967
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   968
lemma flip_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   969
  \<open>((=) ===> pcr_word ===> pcr_word) flip_bit flip_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   970
  by (unfold flip_bit_def) transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   971
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   972
lemma signed_take_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   973
  \<open>((=) ===> pcr_word ===> pcr_word)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   974
    (\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k))
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   975
    (signed_take_bit :: nat \<Rightarrow> 'a word \<Rightarrow> 'a word)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   976
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   977
  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>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   978
  let ?W = \<open>\<lambda>n (w :: 'a word). take_bit n w OR of_bool (bit w n) * NOT (mask n)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   979
  have \<open>((=) ===> pcr_word ===> pcr_word) ?K ?W\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   980
    by transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   981
  also have \<open>?K = (\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   982
    by (simp add: fun_eq_iff signed_take_bit_def bit_take_bit_iff ac_simps)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   983
  also have \<open>?W = signed_take_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   984
    by (simp add: fun_eq_iff signed_take_bit_def)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   985
  finally show ?thesis .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   986
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   987
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   988
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   989
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   990
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   991
subsection \<open>Conversions including casts\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   992
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   993
subsubsection \<open>Generic unsigned conversion\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   994
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   995
context semiring_bits
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   996
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   997
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   998
lemma bit_unsigned_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   999
  \<open>bit (unsigned w) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> bit w n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1000
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1001
  by (transfer fixing: bit) (simp add: bit_of_nat_iff bit_nat_iff bit_take_bit_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1002
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1003
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1004
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1005
context semiring_bit_shifts
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1006
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1007
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1008
lemma unsigned_push_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1009
  \<open>unsigned (push_bit n w) = take_bit LENGTH('b) (push_bit n (unsigned w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1010
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1011
proof (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1012
  fix m
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1013
  assume \<open>2 ^ m \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1014
  show \<open>bit (unsigned (push_bit n w)) m = bit (take_bit LENGTH('b) (push_bit n (unsigned w))) m\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1015
  proof (cases \<open>n \<le> m\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1016
    case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1017
    with \<open>2 ^ m \<noteq> 0\<close> have \<open>2 ^ (m - n) \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1018
      by (metis (full_types) diff_add exp_add_not_zero_imp)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1019
    with True show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1020
      by (simp add: bit_unsigned_iff bit_push_bit_iff Parity.bit_push_bit_iff bit_take_bit_iff not_le exp_eq_zero_iff ac_simps)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1021
  next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1022
    case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1023
    then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1024
      by (simp add: not_le bit_unsigned_iff bit_push_bit_iff Parity.bit_push_bit_iff bit_take_bit_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1025
  qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1026
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1027
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1028
lemma unsigned_take_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1029
  \<open>unsigned (take_bit n w) = take_bit n (unsigned w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1030
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1031
  by (rule bit_eqI) (simp add: bit_unsigned_iff bit_take_bit_iff Parity.bit_take_bit_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1032
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1033
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1034
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1035
context semiring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1036
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1037
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1038
lemma unsigned_and_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1039
  \<open>unsigned (v AND w) = unsigned v AND unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1040
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1041
  by (rule bit_eqI) (simp add: bit_unsigned_iff bit_and_iff Bit_Operations.bit_and_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1042
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1043
lemma unsigned_or_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1044
  \<open>unsigned (v OR w) = unsigned v OR unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1045
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1046
  by (rule bit_eqI) (simp add: bit_unsigned_iff bit_or_iff Bit_Operations.bit_or_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1047
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1048
lemma unsigned_xor_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1049
  \<open>unsigned (v XOR w) = unsigned v XOR unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1050
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1051
  by (rule bit_eqI) (simp add: bit_unsigned_iff bit_xor_iff Bit_Operations.bit_xor_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1052
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1053
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1054
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1055
context ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1056
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1057
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1058
lemma unsigned_not_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1059
  \<open>unsigned (NOT w) = take_bit LENGTH('b) (NOT (unsigned w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1060
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1061
  by (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1062
    (simp add: bit_unsigned_iff bit_take_bit_iff bit_not_iff Bit_Operations.bit_not_iff exp_eq_zero_iff not_le)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1063
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1064
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1065
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1066
context unique_euclidean_semiring_numeral
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1067
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1068
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1069
lemma unsigned_greater_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1070
  \<open>0 \<le> unsigned w\<close> for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1071
  by (transfer fixing: less_eq) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1072
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1073
lemma unsigned_less:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1074
  \<open>unsigned w < 2 ^ LENGTH('b)\<close> for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1075
  by (transfer fixing: less) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1076
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1077
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1078
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1079
context linordered_semidom
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1080
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1081
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1082
lemma word_less_eq_iff_unsigned:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1083
  "a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1084
  by (transfer fixing: less_eq) (simp add: nat_le_eq_zle)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1085
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1086
lemma word_less_iff_unsigned:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1087
  "a < b \<longleftrightarrow> unsigned a < unsigned b"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1088
  by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF take_bit_nonnegative])
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1089
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1090
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1091
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1092
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1093
subsubsection \<open>Generic signed conversion\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1094
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1095
context ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1096
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1097
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1098
lemma bit_signed_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1099
  \<open>bit (signed w) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> bit w (min (LENGTH('b) - Suc 0) n)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1100
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1101
  by (transfer fixing: bit)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1102
    (auto simp add: bit_of_int_iff Bit_Operations.bit_signed_take_bit_iff min_def)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1103
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1104
lemma signed_push_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1105
  \<open>signed (push_bit n w) = signed_take_bit (LENGTH('b) - Suc 0) (push_bit n (signed w :: 'a))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1106
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1107
proof (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1108
  fix m
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1109
  assume \<open>2 ^ m \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1110
  define q where \<open>q = LENGTH('b) - Suc 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1111
  then have *: \<open>LENGTH('b) = Suc q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1112
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1113
  show \<open>bit (signed (push_bit n w)) m \<longleftrightarrow>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1114
    bit (signed_take_bit (LENGTH('b) - Suc 0) (push_bit n (signed w :: 'a))) m\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1115
  proof (cases \<open>q \<le> m\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1116
    case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1117
    moreover define r where \<open>r = m - q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1118
    ultimately have \<open>m = q + r\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1119
      by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1120
    moreover from \<open>m = q + r\<close> \<open>2 ^ m \<noteq> 0\<close> have \<open>2 ^ q \<noteq> 0\<close> \<open>2 ^ r \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1121
      using exp_add_not_zero_imp_left [of q r] exp_add_not_zero_imp_right [of q r]
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1122
      by simp_all
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1123
    moreover from \<open>2 ^ q \<noteq> 0\<close> have \<open>2 ^ (q - n) \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1124
      by (rule exp_not_zero_imp_exp_diff_not_zero)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1125
    ultimately show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1126
      by (auto simp add: bit_signed_iff bit_signed_take_bit_iff bit_push_bit_iff Parity.bit_push_bit_iff
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1127
      min_def * exp_eq_zero_iff le_diff_conv2)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1128
  next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1129
    case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1130
    then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1131
      using exp_not_zero_imp_exp_diff_not_zero [of m n]
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1132
      by (auto simp add: bit_signed_iff bit_signed_take_bit_iff bit_push_bit_iff Parity.bit_push_bit_iff
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1133
      min_def not_le not_less * le_diff_conv2 less_diff_conv2 Parity.exp_eq_0_imp_not_bit exp_eq_0_imp_not_bit
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1134
      exp_eq_zero_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1135
  qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1136
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1137
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1138
lemma signed_take_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1139
  \<open>signed (take_bit n w) = (if n < LENGTH('b) then take_bit n (signed w) else signed w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1140
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1141
  by (transfer fixing: take_bit; cases \<open>LENGTH('b)\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1142
    (auto simp add: Bit_Operations.signed_take_bit_take_bit Bit_Operations.take_bit_signed_take_bit take_bit_of_int min_def less_Suc_eq)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1143
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1144
lemma signed_not_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1145
  \<open>signed (NOT w) = signed_take_bit LENGTH('b) (NOT (signed w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1146
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1147
proof (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1148
  fix n
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1149
  assume \<open>2 ^ n \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1150
  define q where \<open>q = LENGTH('b) - Suc 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1151
  then have *: \<open>LENGTH('b) = Suc q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1152
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1153
  show \<open>bit (signed (NOT w)) n \<longleftrightarrow>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1154
    bit (signed_take_bit LENGTH('b) (NOT (signed w))) n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1155
  proof (cases \<open>q < n\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1156
    case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1157
    moreover define r where \<open>r = n - Suc q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1158
    ultimately have \<open>n = r + Suc q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1159
      by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1160
    moreover from \<open>2 ^ n \<noteq> 0\<close> \<open>n = r + Suc q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1161
      have \<open>2 ^ Suc q \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1162
      using exp_add_not_zero_imp_right by blast 
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1163
    ultimately show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1164
      by (simp add: * bit_signed_iff bit_not_iff bit_signed_take_bit_iff Bit_Operations.bit_not_iff min_def
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1165
        exp_eq_zero_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1166
  next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1167
    case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1168
    then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1169
      by (auto simp add: * bit_signed_iff bit_not_iff bit_signed_take_bit_iff Bit_Operations.bit_not_iff min_def
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1170
        exp_eq_zero_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1171
  qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1172
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1173
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1174
lemma signed_and_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1175
  \<open>signed (v AND w) = signed v AND signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1176
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1177
  by (rule bit_eqI) (simp add: bit_signed_iff bit_and_iff Bit_Operations.bit_and_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1178
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1179
lemma signed_or_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1180
  \<open>signed (v OR w) = signed v OR signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1181
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1182
  by (rule bit_eqI) (simp add: bit_signed_iff bit_or_iff Bit_Operations.bit_or_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1183
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1184
lemma signed_xor_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1185
  \<open>signed (v XOR w) = signed v XOR signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1186
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1187
  by (rule bit_eqI) (simp add: bit_signed_iff bit_xor_iff Bit_Operations.bit_xor_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1188
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1189
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1190
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1191
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1192
subsubsection \<open>More\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1193
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1194
lemma sint_greater_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1195
  \<open>- (2 ^ (LENGTH('a) - Suc 0)) \<le> sint w\<close> for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1196
proof (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1197
  case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1198
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1199
    by transfer (simp add: signed_take_bit_eq_if_negative minus_exp_eq_not_mask or_greater_eq ac_simps)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1200
next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1201
  have *: \<open>- (2 ^ (LENGTH('a) - Suc 0)) \<le> (0::int)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1202
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1203
  case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1204
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1205
    by transfer (auto simp add: signed_take_bit_eq intro: order_trans *)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1206
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1207
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1208
lemma sint_less:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1209
  \<open>sint w < 2 ^ (LENGTH('a) - Suc 0)\<close> for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1210
  by (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>; transfer)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1211
    (simp_all add: signed_take_bit_eq signed_take_bit_def not_eq_complement mask_eq_exp_minus_1 OR_upper)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1212
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1213
lemma unat_div_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1214
  \<open>unat (v div w) = unat v div unat w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1215
proof transfer
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1216
  fix k l
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1217
  have \<open>nat (take_bit LENGTH('a) k) div nat (take_bit LENGTH('a) l) \<le> nat (take_bit LENGTH('a) k)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1218
    by (rule div_le_dividend)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1219
  also have \<open>nat (take_bit LENGTH('a) k) < 2 ^ LENGTH('a)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1220
    by (simp add: nat_less_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1221
  finally show \<open>(nat \<circ> take_bit LENGTH('a)) (take_bit LENGTH('a) k div take_bit LENGTH('a) l) =
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1222
    (nat \<circ> take_bit LENGTH('a)) k div (nat \<circ> take_bit LENGTH('a)) l\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1223
    by (simp add: nat_take_bit_eq div_int_pos_iff nat_div_distrib take_bit_nat_eq_self_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1224
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1225
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1226
lemma unat_mod_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1227
  \<open>unat (v mod w) = unat v mod unat w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1228
proof transfer
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1229
  fix k l
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1230
  have \<open>nat (take_bit LENGTH('a) k) mod nat (take_bit LENGTH('a) l) \<le> nat (take_bit LENGTH('a) k)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1231
    by (rule mod_less_eq_dividend)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1232
  also have \<open>nat (take_bit LENGTH('a) k) < 2 ^ LENGTH('a)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1233
    by (simp add: nat_less_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1234
  finally show \<open>(nat \<circ> take_bit LENGTH('a)) (take_bit LENGTH('a) k mod take_bit LENGTH('a) l) =
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1235
    (nat \<circ> take_bit LENGTH('a)) k mod (nat \<circ> take_bit LENGTH('a)) l\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1236
    by (simp add: nat_take_bit_eq mod_int_pos_iff less_le nat_mod_distrib take_bit_nat_eq_self_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1237
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1238
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1239
lemma uint_div_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1240
  \<open>uint (v div w) = uint v div uint w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1241
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1242
  have \<open>int (unat (v div w)) = int (unat v div unat w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1243
    by (simp add: unat_div_distrib)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1244
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1245
    by (simp add: of_nat_div)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1246
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1247
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1248
lemma uint_mod_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1249
  \<open>uint (v mod w) = uint v mod uint w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1250
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1251
  have \<open>int (unat (v mod w)) = int (unat v mod unat w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1252
    by (simp add: unat_mod_distrib)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1253
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1254
    by (simp add: of_nat_mod)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1255
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1256
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1257
context semiring_bit_shifts
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1258
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1259
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1260
lemma unsigned_ucast_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1261
  \<open>unsigned (ucast w :: 'c::len word) = take_bit LENGTH('c) (unsigned w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1262
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1263
  by (rule bit_eqI) (simp add: bit_unsigned_iff Word.bit_unsigned_iff bit_take_bit_iff exp_eq_zero_iff not_le)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1264
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1265
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1266
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1267
context ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1268
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1269
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1270
lemma signed_ucast_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1271
  \<open>signed (ucast w :: 'c::len word) = signed_take_bit (LENGTH('c) - Suc 0) (unsigned w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1272
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1273
proof (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1274
  fix n
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1275
  assume \<open>2 ^ n \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1276
  then have \<open>2 ^ (min (LENGTH('c) - Suc 0) n) \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1277
    by (simp add: min_def)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1278
      (metis (mono_tags) diff_diff_cancel exp_not_zero_imp_exp_diff_not_zero)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1279
  then show \<open>bit (signed (ucast w :: 'c::len word)) n \<longleftrightarrow> bit (signed_take_bit (LENGTH('c) - Suc 0) (unsigned w)) n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1280
    by (simp add: bit_signed_iff bit_unsigned_iff Word.bit_unsigned_iff bit_signed_take_bit_iff exp_eq_zero_iff not_le)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1281
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1282
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1283
lemma signed_scast_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1284
  \<open>signed (scast w :: 'c::len word) = signed_take_bit (LENGTH('c) - Suc 0) (signed w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1285
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1286
proof (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1287
  fix n
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1288
  assume \<open>2 ^ n \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1289
  then have \<open>2 ^ (min (LENGTH('c) - Suc 0) n) \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1290
    by (simp add: min_def)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1291
      (metis (mono_tags) diff_diff_cancel exp_not_zero_imp_exp_diff_not_zero)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1292
  then show \<open>bit (signed (scast w :: 'c::len word)) n \<longleftrightarrow> bit (signed_take_bit (LENGTH('c) - Suc 0) (signed w)) n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1293
    by (simp add: bit_signed_iff bit_unsigned_iff Word.bit_signed_iff bit_signed_take_bit_iff exp_eq_zero_iff not_le)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1294
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1295
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1296
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1297
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1298
lemma uint_nonnegative: "0 \<le> uint w"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1299
  by (fact unsigned_greater_eq)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1300
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1301
lemma uint_bounded: "uint w < 2 ^ LENGTH('a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1302
  for w :: "'a::len word"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1303
  by (fact unsigned_less)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1304
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1305
lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1306
  for w :: "'a::len word"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1307
  by transfer (simp add: take_bit_eq_mod)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1308
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1309
lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1310
  by (fact unsigned_word_eqI)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1311
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1312
lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1313
  by (fact word_eq_iff_unsigned)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1314
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1315
lemma uint_word_of_int_eq:
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1316
  \<open>uint (word_of_int k :: 'a::len word) = take_bit LENGTH('a) k\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1317
  by transfer rule
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1318
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1319
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len word) = k mod 2 ^ LENGTH('a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1320
  by (simp add: uint_word_of_int_eq take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1321
  
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1322
lemma word_of_int_uint: "word_of_int (uint w) = w"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset