src/HOL/Library/Word.thy
author haftmann
Sat, 19 Dec 2020 17:49:14 +0000
changeset 72954 eb1e5c4f70cd
parent 72830 ec0d3a62bb3b
child 73535 0f33c7031ec9
permissions -rw-r--r--
more precise simpset for method unat_arith
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72515
c7038c397ae3 moved most material from session HOL-Word to Word_Lib in the AFP
haftmann
parents: 72512
diff changeset
     1
(*  Title:      HOL/Library/Word.thy
c7038c397ae3 moved most material from session HOL-Word to Word_Lib in the AFP
haftmann
parents: 72512
diff changeset
     2
    Author:     Jeremy Dawson and Gerwin Klein, NICTA, et. al.
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"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    12
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    13
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    14
subsection \<open>Preliminaries\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    15
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    16
lemma signed_take_bit_decr_length_iff:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    17
  \<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
    18
    \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    19
  by (cases \<open>LENGTH('a)\<close>)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    20
    (simp_all add: signed_take_bit_eq_iff_take_bit_eq)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    21
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    22
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    23
subsection \<open>Fundamentals\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    24
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    25
subsubsection \<open>Type definition\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    26
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
    27
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
    28
  morphisms rep Word by (auto intro!: equivpI reflpI sympI transpI)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    29
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    30
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
    31
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
    32
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    33
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    34
subsubsection \<open>Basic arithmetic\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    35
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    36
instantiation word :: (len) comm_ring_1
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    37
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    38
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    39
lift_definition zero_word :: \<open>'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    40
  is 0 .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    41
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    42
lift_definition one_word :: \<open>'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    43
  is 1 .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    44
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    45
lift_definition plus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    46
  is \<open>(+)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    47
  by (auto simp add: take_bit_eq_mod intro: mod_add_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    48
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    49
lift_definition minus_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_diff_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    52
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    53
lift_definition uminus_word :: \<open>'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    54
  is uminus
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    55
  by (auto simp add: take_bit_eq_mod intro: mod_minus_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    56
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    57
lift_definition times_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    58
  is \<open>(*)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    59
  by (auto simp add: take_bit_eq_mod intro: mod_mult_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    60
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    61
instance
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    62
  by (standard; transfer) (simp_all add: algebra_simps)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    63
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    64
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    65
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    66
context
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    67
  includes lifting_syntax
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    68
  notes
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    69
    power_transfer [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    70
    transfer_rule_of_bool [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    71
    transfer_rule_numeral [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    72
    transfer_rule_of_nat [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    73
    transfer_rule_of_int [transfer_rule]
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    74
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    75
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    76
lemma power_transfer_word [transfer_rule]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    77
  \<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    78
  by transfer_prover
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    79
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    80
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    81
  \<open>((=) ===> pcr_word) of_bool of_bool\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    82
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    83
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    84
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    85
  \<open>((=) ===> pcr_word) numeral numeral\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    86
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    87
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    88
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    89
  \<open>((=) ===> pcr_word) int of_nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    90
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    91
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    92
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    93
  \<open>((=) ===> pcr_word) (\<lambda>k. k) of_int\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    94
proof -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    95
  have \<open>((=) ===> pcr_word) of_int of_int\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    96
    by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    97
  then show ?thesis by (simp add: id_def)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    98
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    99
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   100
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   101
  \<open>(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   102
proof -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   103
  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
   104
    for k :: int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   105
  proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   106
    assume ?P
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   107
    then show ?Q
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   108
      by auto
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   109
  next
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   110
    assume ?Q
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   111
    then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   112
    then have "even (take_bit LENGTH('a) k)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   113
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   114
    then show ?P
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   115
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   116
  qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   117
  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
   118
    transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   119
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   120
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   121
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   122
72512
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
   123
lemma exp_eq_zero_iff [simp]:
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
   124
  \<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close>
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
   125
  by transfer simp
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
   126
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   127
lemma word_exp_length_eq_0 [simp]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   128
  \<open>(2 :: 'a::len word) ^ LENGTH('a) = 0\<close>
72512
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
   129
  by simp
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   130
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   131
72489
a1366ce41368 early and more complete setup of tools
haftmann
parents: 72488
diff changeset
   132
subsubsection \<open>Basic tool setup\<close>
a1366ce41368 early and more complete setup of tools
haftmann
parents: 72488
diff changeset
   133
a1366ce41368 early and more complete setup of tools
haftmann
parents: 72488
diff changeset
   134
ML_file \<open>Tools/word_lib.ML\<close>
a1366ce41368 early and more complete setup of tools
haftmann
parents: 72488
diff changeset
   135
a1366ce41368 early and more complete setup of tools
haftmann
parents: 72488
diff changeset
   136
72244
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
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   278
lemma inj_unsigned [simp]:
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   279
  \<open>inj unsigned\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   280
  by (rule injI) (simp add: unsigned_word_eqI)
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   281
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   282
lemma unsigned_eq_0_iff:
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   283
  \<open>unsigned w = 0 \<longleftrightarrow> w = 0\<close>
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   284
  using word_eq_iff_unsigned [of w 0] by simp
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   285
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   286
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   287
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   288
context ring_1
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   289
begin
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
lift_definition signed :: \<open>'b::len word \<Rightarrow> 'a\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   292
  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
   293
  by (simp flip: signed_take_bit_decr_length_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   294
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   295
lemma signed_0 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   296
  \<open>signed 0 = 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   297
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   298
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   299
lemma signed_1 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   300
  \<open>signed (1 :: 'b::len word) = (if LENGTH('b) = 1 then - 1 else 1)\<close>
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
   301
  by (transfer fixing: uminus; cases \<open>LENGTH('b)\<close>) (auto dest: gr0_implies_Suc)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   302
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   303
lemma signed_minus_1 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   304
  \<open>signed (- 1 :: 'b::len word) = - 1\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   305
  by (transfer fixing: uminus) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   306
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   307
lemma signed_numeral [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   308
  \<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
   309
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   310
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   311
lemma signed_neg_numeral [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   312
  \<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
   313
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   314
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   315
lemma signed_of_nat [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   316
  \<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
   317
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   318
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   319
lemma signed_of_int [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   320
  \<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
   321
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   322
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   323
end
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
context ring_char_0
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   326
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   327
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   328
lemma signed_word_eqI:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   329
  \<open>v = w\<close> if \<open>signed v = signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   330
  using that by transfer (simp flip: signed_take_bit_decr_length_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   331
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   332
lemma word_eq_iff_signed:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   333
  \<open>v = w \<longleftrightarrow> signed v = signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   334
  by (auto intro: signed_word_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   335
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   336
lemma inj_signed [simp]:
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   337
  \<open>inj signed\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   338
  by (rule injI) (simp add: signed_word_eqI)
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   339
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   340
lemma signed_eq_0_iff:
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   341
  \<open>signed w = 0 \<longleftrightarrow> w = 0\<close>
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   342
  using word_eq_iff_signed [of w 0] by simp
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   343
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   344
end
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
abbreviation unat :: \<open>'a::len word \<Rightarrow> nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   347
  where \<open>unat \<equiv> unsigned\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   348
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   349
abbreviation uint :: \<open>'a::len word \<Rightarrow> int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   350
  where \<open>uint \<equiv> unsigned\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   351
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   352
abbreviation sint :: \<open>'a::len word \<Rightarrow> int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   353
  where \<open>sint \<equiv> signed\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   354
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   355
abbreviation ucast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   356
  where \<open>ucast \<equiv> unsigned\<close>
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
abbreviation scast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   359
  where \<open>scast \<equiv> signed\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   360
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   361
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   362
  includes lifting_syntax
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   363
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   364
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   365
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   366
  \<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
   367
  using unsigned.transfer [where ?'a = nat] by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   368
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   369
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   370
  \<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
   371
  using unsigned.transfer [where ?'a = int] by (simp add: comp_def)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   372
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   373
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   374
  \<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
   375
  using signed.transfer [where ?'a = int] by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   376
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   377
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   378
  \<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
   379
proof (rule rel_funI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   380
  fix k :: int and w :: \<open>'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   381
  assume \<open>pcr_word k w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   382
  then have \<open>w = word_of_int k\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   383
    by (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   384
  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
   385
    by transfer (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   386
  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
   387
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   388
qed
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 [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   391
  \<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
   392
proof (rule rel_funI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   393
  fix k :: int and w :: \<open>'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   394
  assume \<open>pcr_word k w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   395
  then have \<open>w = word_of_int k\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   396
    by (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   397
  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
   398
    by transfer (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   399
  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
   400
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   401
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   402
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   403
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   404
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   405
lemma of_nat_unat [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   406
  \<open>of_nat (unat w) = unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   407
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   408
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   409
lemma of_int_uint [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   410
  \<open>of_int (uint w) = unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   411
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   412
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   413
lemma of_int_sint [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   414
  \<open>of_int (sint a) = signed a\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   415
  by transfer (simp_all add: take_bit_signed_take_bit)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   416
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   417
lemma nat_uint_eq [simp]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   418
  \<open>nat (uint w) = unat w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   419
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   420
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   421
lemma sgn_uint_eq [simp]:
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   422
  \<open>sgn (uint w) = of_bool (w \<noteq> 0)\<close>
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   423
  by transfer (simp add: less_le)
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   424
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   425
text \<open>Aliasses only for code generation\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   426
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   427
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   428
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   429
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   430
qualified lift_definition of_int :: \<open>int \<Rightarrow> 'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   431
  is \<open>take_bit LENGTH('a)\<close> .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   432
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   433
qualified lift_definition of_nat :: \<open>nat \<Rightarrow> 'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   434
  is \<open>int \<circ> take_bit LENGTH('a)\<close> .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   435
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   436
qualified lift_definition the_nat :: \<open>'a::len word \<Rightarrow> nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   437
  is \<open>nat \<circ> take_bit LENGTH('a)\<close> by 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
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
   440
  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
   441
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   442
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
   443
  is \<open>take_bit LENGTH('a)\<close> by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   444
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   445
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
   446
  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
   447
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   448
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   449
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   450
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   451
  \<open>Word.the_int = uint\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   452
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   453
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   454
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   455
  \<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
   456
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   457
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   458
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   459
  \<open>Word.of_int = word_of_int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   460
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   461
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   462
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   463
  \<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
   464
  by transfer (simp add: take_bit_of_nat)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   465
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   466
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   467
  \<open>Word.of_nat = word_of_nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   468
  by (rule; transfer) (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   469
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   470
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   471
  \<open>Word.the_nat w = nat (Word.the_int w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   472
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   473
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   474
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   475
  \<open>Word.the_nat = unat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   476
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   477
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   478
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   479
  \<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
   480
  for w :: \<open>'a::len word\<close>
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
   481
  by transfer (simp add: signed_take_bit_take_bit)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   482
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   483
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   484
  \<open>Word.the_signed_int = sint\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   485
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   486
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   487
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   488
  \<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
   489
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   490
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   491
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   492
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   493
  \<open>Word.cast = ucast\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   494
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   495
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   496
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   497
  \<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
   498
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   499
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   500
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   501
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   502
  \<open>Word.signed_cast = scast\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   503
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   504
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   505
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   506
  \<open>unsigned w = of_nat (nat (Word.the_int w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   507
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   508
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   509
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   510
  \<open>signed w = of_int (Word.the_signed_int w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   511
  by transfer simp
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   512
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   513
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   514
subsubsection \<open>Basic ordering\<close>
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   515
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
   516
instantiation word :: (len) linorder
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   517
begin
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   518
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   519
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   520
  is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   521
  by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   522
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   523
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   524
  is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   525
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   526
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   527
instance
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   528
  by (standard; transfer) auto
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   529
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   530
end
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   531
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   532
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
   533
  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
   534
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   535
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
   536
  by (standard; transfer) simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   537
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   538
lemma word_of_nat_less_eq_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   539
  \<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
   540
  by transfer (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   541
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   542
lemma word_of_int_less_eq_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   543
  \<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
   544
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   545
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   546
lemma word_of_nat_less_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   547
  \<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
   548
  by transfer (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   549
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   550
lemma word_of_int_less_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   551
  \<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
   552
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   553
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   554
lemma word_le_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   555
  "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   556
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   557
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   558
lemma word_less_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   559
  "a < b \<longleftrightarrow> uint a < uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   560
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   561
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   562
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
   563
  \<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
   564
  by transfer (simp add: less_le)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   565
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   566
lemma of_nat_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   567
  \<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
   568
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   569
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   570
lemma of_nat_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   571
  \<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
   572
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   573
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   574
lemma of_int_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   575
  \<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
   576
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   577
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   578
lemma of_int_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   579
  \<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
   580
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   581
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   582
72280
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   583
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   584
subsection \<open>Enumeration\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   585
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   586
lemma inj_on_word_of_nat:
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   587
  \<open>inj_on (word_of_nat :: nat \<Rightarrow> 'a::len word) {0..<2 ^ LENGTH('a)}\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   588
  by (rule inj_onI; transfer) (simp_all add: take_bit_int_eq_self)
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   589
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   590
lemma UNIV_word_eq_word_of_nat:
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   591
  \<open>(UNIV :: 'a::len word set) = word_of_nat ` {0..<2 ^ LENGTH('a)}\<close> (is \<open>_ = ?A\<close>)
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   592
proof
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   593
  show \<open>word_of_nat ` {0..<2 ^ LENGTH('a)} \<subseteq> UNIV\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   594
    by simp
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   595
  show \<open>UNIV \<subseteq> ?A\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   596
  proof
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   597
    fix w :: \<open>'a word\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   598
    show \<open>w \<in> (word_of_nat ` {0..<2 ^ LENGTH('a)} :: 'a word set)\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   599
      by (rule image_eqI [of _ _ \<open>unat w\<close>]; transfer) simp_all
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   600
  qed
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   601
qed
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   602
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   603
instantiation word :: (len) enum
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   604
begin
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   605
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   606
definition enum_word :: \<open>'a word list\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   607
  where \<open>enum_word = map word_of_nat [0..<2 ^ LENGTH('a)]\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   608
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   609
definition enum_all_word :: \<open>('a word \<Rightarrow> bool) \<Rightarrow> bool\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   610
  where \<open>enum_all_word = Ball UNIV\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   611
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   612
definition enum_ex_word :: \<open>('a word \<Rightarrow> bool) \<Rightarrow> bool\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   613
  where \<open>enum_ex_word = Bex UNIV\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   614
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   615
lemma [code]:
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   616
  \<open>Enum.enum_all P \<longleftrightarrow> Ball UNIV P\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   617
  \<open>Enum.enum_ex P \<longleftrightarrow> Bex UNIV P\<close> for P :: \<open>'a word \<Rightarrow> bool\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   618
  by (simp_all add: enum_all_word_def enum_ex_word_def)
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   619
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   620
instance
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   621
  by standard (simp_all add: UNIV_word_eq_word_of_nat inj_on_word_of_nat enum_word_def enum_all_word_def enum_ex_word_def distinct_map)
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   622
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   623
end
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   624
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   625
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   626
subsection \<open>Bit-wise operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   627
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   628
instantiation word :: (len) semiring_modulo
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   629
begin
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   630
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   631
lift_definition divide_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   632
  is \<open>\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   633
  by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   634
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   635
lift_definition modulo_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   636
  is \<open>\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   637
  by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   638
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   639
instance proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   640
  show "a div b * b + a mod b = a" for a b :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   641
  proof transfer
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   642
    fix k l :: int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   643
    define r :: int where "r = 2 ^ LENGTH('a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   644
    then have r: "take_bit LENGTH('a) k = k mod r" for k
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   645
      by (simp add: take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   646
    have "k mod r = ((k mod r) div (l mod r) * (l mod r)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   647
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   648
      by (simp add: div_mult_mod_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   649
    also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   650
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   651
      by (simp add: mod_add_left_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   652
    also have "... = (((k mod r) div (l mod r) * l) mod r
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   653
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   654
      by (simp add: mod_mult_right_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   655
    finally have "k mod r = ((k mod r) div (l mod r) * l
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   656
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   657
      by (simp add: mod_simps)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   658
    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
   659
      + take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   660
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   661
  qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   662
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   663
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   664
end
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   665
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   666
instance word :: (len) semiring_parity
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   667
proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   668
  show "\<not> 2 dvd (1::'a word)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   669
    by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   670
  show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   671
    for a :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   672
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   673
  show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   674
    for a :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   675
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   676
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   677
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   678
lemma word_bit_induct [case_names zero even odd]:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   679
  \<open>P a\<close> if word_zero: \<open>P 0\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   680
    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
   681
    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
   682
  for P and a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   683
proof -
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   684
  define m :: nat where \<open>m = LENGTH('a) - Suc 0\<close>
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   685
  then have l: \<open>LENGTH('a) = Suc m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   686
    by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   687
  define n :: nat where \<open>n = unat a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   688
  then have \<open>n < 2 ^ LENGTH('a)\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   689
    by transfer (simp add: take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   690
  then have \<open>n < 2 * 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   691
    by (simp add: l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   692
  then have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   693
  proof (induction n rule: nat_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   694
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   695
    show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   696
      by simp (rule word_zero)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   697
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   698
    case (even n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   699
    then have \<open>n < 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   700
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   701
    with even.IH have \<open>P (of_nat n)\<close>
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
    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
   704
      by (auto simp add: word_greater_zero_iff l)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   705
    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
   706
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
   707
      by (simp add: l take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   708
    ultimately have \<open>P (2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   709
      by (rule word_even)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   710
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   711
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   712
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   713
    case (odd n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   714
    then have \<open>Suc n \<le> 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   715
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   716
    with odd.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   717
      by simp
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   718
    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
   719
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
   720
      by (simp add: l take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   721
    ultimately have \<open>P (1 + 2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   722
      by (rule word_odd)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   723
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   724
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   725
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   726
  moreover have \<open>of_nat (nat (uint a)) = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   727
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   728
  ultimately show ?thesis
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   729
    by (simp add: n_def)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   730
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   731
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   732
lemma bit_word_half_eq:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   733
  \<open>(of_bool b + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   734
    if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   735
    for a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   736
proof (cases \<open>2 \<le> LENGTH('a::len)\<close>)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   737
  case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   738
  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
   739
    by auto
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   740
  with False that show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   741
    by transfer (simp add: eq_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   742
next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   743
  case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   744
  obtain n where length: \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   745
    by (cases \<open>LENGTH('a)\<close>) simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   746
  show ?thesis proof (cases b)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   747
    case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   748
    moreover have \<open>a * 2 div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   749
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   750
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   751
      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
   752
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   753
      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
   754
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   755
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   756
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   757
      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
   758
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   759
      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
   760
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   761
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   762
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   763
    ultimately show ?thesis
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 True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   767
    moreover have \<open>(1 + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   768
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   769
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   770
      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
   771
        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
   772
      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
   773
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   774
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   775
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   776
      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
   777
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   778
      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
   779
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   780
        by (auto simp add: take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   781
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   782
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   783
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   784
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   785
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   786
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   787
lemma even_mult_exp_div_word_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   788
  \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> (
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   789
    m \<le> n \<and>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   790
    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
   791
  by transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   792
    (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
   793
      simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   794
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   795
instantiation word :: (len) semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   796
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   797
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   798
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
   799
  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
   800
proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   801
  fix k l :: int and n :: nat
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   802
  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
   803
  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
   804
  proof (cases \<open>n < LENGTH('a)\<close>)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   805
    case True
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   806
    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
   807
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   808
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   809
      by (simp add: bit_take_bit_iff)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   810
  next
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   811
    case False
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   812
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   813
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   814
  qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   815
qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   816
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   817
instance proof
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   818
  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
   819
    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
   820
  for P and a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   821
  proof (induction a rule: word_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   822
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   823
    have \<open>0 div 2 = (0::'a word)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   824
      by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   825
    with stable [of 0] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   826
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   827
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   828
    case (even a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   829
    with rec [of a False] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   830
      using bit_word_half_eq [of a False] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   831
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   832
    case (odd a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   833
    with rec [of a True] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   834
      using bit_word_half_eq [of a True] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   835
  qed
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   836
  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
   837
    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
   838
  show \<open>0 div a = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   839
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   840
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   841
  show \<open>a div 1 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   842
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   843
    by transfer simp
72735
bbe5d3ef2052 Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents: 72611
diff changeset
   844
  have \<section>: "\<And>i n. (i::int) mod 2 ^ n = 0 \<or> 0 < i mod 2 ^ n"
bbe5d3ef2052 Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents: 72611
diff changeset
   845
    by (metis le_less take_bit_eq_mod take_bit_nonnegative)
bbe5d3ef2052 Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents: 72611
diff changeset
   846
  have less_power: "\<And>n i p. (i::int) mod numeral p ^ n < numeral p ^ n"
bbe5d3ef2052 Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents: 72611
diff changeset
   847
    by simp
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   848
  show \<open>a mod b div b = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   849
    for a b :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   850
    apply transfer
72735
bbe5d3ef2052 Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents: 72611
diff changeset
   851
    apply (simp add: take_bit_eq_mod mod_eq_0_iff_dvd dvd_def)
bbe5d3ef2052 Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents: 72611
diff changeset
   852
    by (metis (no_types, hide_lams) "\<section>" Euclidean_Division.pos_mod_bound Euclidean_Division.pos_mod_sign le_less_trans mult_eq_0_iff take_bit_eq_mod take_bit_nonnegative zdiv_eq_0_iff zmod_le_nonneg_dividend)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   853
  show \<open>(1 + a) div 2 = a div 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   854
    if \<open>even a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   855
    for a :: \<open>'a word\<close>
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   856
    using that by transfer
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   857
      (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
   858
  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
   859
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   860
    by transfer (simp, simp add: exp_div_exp_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   861
  show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   862
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   863
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   864
    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
   865
    apply (simp add: drop_bit_take_bit)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   866
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   867
  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
   868
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   869
    by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   870
  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
   871
    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
   872
    using that apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   873
    apply (auto simp flip: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   874
           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
   875
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   876
  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
   877
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   878
    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
   879
  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
   880
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   881
    by transfer (auto simp add: take_bit_of_mask even_mask_div_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   882
  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
   883
    for a :: \<open>'a word\<close> and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   884
  proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   885
    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
   886
      n < m
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   887
      \<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   888
      \<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
   889
    for m n :: nat and k l :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   890
      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
   891
        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
   892
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   893
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   894
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   895
end
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   896
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   897
lemma bit_word_eqI:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   898
  \<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
   899
  for a b :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   900
  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
   901
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   902
lemma bit_imp_le_length:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   903
  \<open>n < LENGTH('a)\<close> if \<open>bit w n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   904
    for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   905
  using that by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   906
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   907
lemma not_bit_length [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   908
  \<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
   909
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   910
72830
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   911
lemma finite_bit_word [simp]:
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   912
  \<open>finite {n. bit w n}\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   913
  for w :: \<open>'a::len word\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   914
proof -
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   915
  have \<open>{n. bit w n} \<subseteq> {0..LENGTH('a)}\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   916
    by (auto dest: bit_imp_le_length)
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   917
  moreover have \<open>finite {0..LENGTH('a)}\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   918
    by simp
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   919
  ultimately show ?thesis
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   920
    by (rule finite_subset)
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   921
qed
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   922
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   923
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
   924
begin
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   925
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   926
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
   927
  is push_bit
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   928
proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   929
  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
   930
    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
   931
  proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   932
    from that
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   933
    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
   934
      = 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
   935
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   936
    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
   937
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   938
    ultimately show ?thesis
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   939
      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
   940
  qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   941
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   942
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   943
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
   944
  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
   945
  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
   946
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   947
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
   948
  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
   949
  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
   950
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   951
instance proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   952
  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
   953
    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
   954
  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
   955
    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
   956
  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
   957
    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
   958
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   959
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   960
end
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   961
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   962
lemma [code]:
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
   963
  \<open>push_bit n w = w * 2 ^ n\<close> for w :: \<open>'a::len word\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
   964
  by (fact push_bit_eq_mult)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
   965
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
   966
lemma [code]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
   967
  \<open>Word.the_int (drop_bit n w) = drop_bit n (Word.the_int w)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
   968
  by transfer (simp add: drop_bit_take_bit min_def le_less less_diff_conv)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
   969
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
   970
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   971
  \<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
   972
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   973
  by transfer (simp add: not_le not_less ac_simps min_absorb2)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   974
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   975
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   976
instantiation word :: (len) ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   977
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   978
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   979
lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   980
  is not
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   981
  by (simp add: take_bit_not_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   982
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   983
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
   984
  is \<open>and\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   985
  by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   986
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   987
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
   988
  is or
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   989
  by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   990
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   991
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
   992
  is xor
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   993
  by simp
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
lift_definition mask_word :: \<open>nat \<Rightarrow> 'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   996
  is mask
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
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   999
instance by (standard; transfer)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1000
  (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
  1001
    bit_not_iff bit_and_iff bit_or_iff bit_xor_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
lemma [code_abbrev]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1006
  \<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1007
  by (fact push_bit_of_1)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1008
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1009
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1010
  \<open>NOT w = Word.of_int (NOT (Word.the_int w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1011
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1012
  by transfer (simp add: take_bit_not_take_bit) 
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1013
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1014
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1015
  \<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
  1016
  by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1017
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1018
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1019
  \<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
  1020
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1021
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1022
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1023
  \<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
  1024
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1025
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1026
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1027
  \<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
  1028
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1029
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1030
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1031
  includes lifting_syntax
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1032
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1033
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1034
lemma set_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1035
  \<open>((=) ===> pcr_word ===> pcr_word) set_bit set_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1036
  by (unfold set_bit_def) transfer_prover
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 unset_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1039
  \<open>((=) ===> pcr_word ===> pcr_word) unset_bit unset_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1040
  by (unfold unset_bit_def) transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1041
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1042
lemma flip_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1043
  \<open>((=) ===> pcr_word ===> pcr_word) flip_bit flip_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1044
  by (unfold flip_bit_def) transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1045
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1046
lemma signed_take_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1047
  \<open>((=) ===> pcr_word ===> pcr_word)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1048
    (\<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
  1049
    (signed_take_bit :: nat \<Rightarrow> 'a word \<Rightarrow> 'a word)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1050
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1051
  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
  1052
  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
  1053
  have \<open>((=) ===> pcr_word ===> pcr_word) ?K ?W\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1054
    by transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1055
  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
  1056
    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
  1057
  also have \<open>?W = signed_take_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1058
    by (simp add: fun_eq_iff signed_take_bit_def)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1059
  finally show ?thesis .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1060
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1061
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1062
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1063
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1064
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1065
subsection \<open>Conversions including casts\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1066
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1067
subsubsection \<open>Generic unsigned conversion\<close>
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
context semiring_bits
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1070
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1071
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  1072
lemma bit_unsigned_iff [bit_simps]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1073
  \<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
  1074
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1075
  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
  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 semiring_bit_shifts
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 unsigned_push_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1083
  \<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
  1084
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1085
proof (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1086
  fix m
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1087
  assume \<open>2 ^ m \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1088
  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
  1089
  proof (cases \<open>n \<le> m\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1090
    case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1091
    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
  1092
      by (metis (full_types) diff_add exp_add_not_zero_imp)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1093
    with True show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1094
      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
  1095
  next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1096
    case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1097
    then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1098
      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
  1099
  qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1100
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1101
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1102
lemma unsigned_take_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1103
  \<open>unsigned (take_bit n w) = take_bit n (unsigned w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1104
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1105
  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
  1106
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1107
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1108
72512
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1109
context unique_euclidean_semiring_with_bit_shifts
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1110
begin
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1111
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1112
lemma unsigned_drop_bit_eq:
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1113
  \<open>unsigned (drop_bit n w) = drop_bit n (take_bit LENGTH('b) (unsigned w))\<close>
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1114
  for w :: \<open>'b::len word\<close>
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1115
  by (rule bit_eqI) (auto simp add: bit_unsigned_iff bit_take_bit_iff bit_drop_bit_eq Parity.bit_drop_bit_eq dest: bit_imp_le_length)
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1116
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1117
end
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1118
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1119
context semiring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1120
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1121
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1122
lemma unsigned_and_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1123
  \<open>unsigned (v AND w) = unsigned v AND unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1124
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1125
  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
  1126
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1127
lemma unsigned_or_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1128
  \<open>unsigned (v OR w) = unsigned v OR unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1129
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1130
  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
  1131
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1132
lemma unsigned_xor_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1133
  \<open>unsigned (v XOR w) = unsigned v XOR unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1134
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1135
  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
  1136
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1137
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1138
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1139
context ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1140
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1141
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1142
lemma unsigned_not_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1143
  \<open>unsigned (NOT w) = take_bit LENGTH('b) (NOT (unsigned w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1144
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1145
  by (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1146
    (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
  1147
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1148
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1149
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1150
context unique_euclidean_semiring_numeral
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1151
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1152
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  1153
lemma unsigned_greater_eq [simp]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1154
  \<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
  1155
  by (transfer fixing: less_eq) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1156
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  1157
lemma unsigned_less [simp]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1158
  \<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
  1159
  by (transfer fixing: less) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1160
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1161
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1162
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1163
context linordered_semidom
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1164
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1165
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1166
lemma word_less_eq_iff_unsigned:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1167
  "a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1168
  by (transfer fixing: less_eq) (simp add: nat_le_eq_zle)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1169
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1170
lemma word_less_iff_unsigned:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1171
  "a < b \<longleftrightarrow> unsigned a < unsigned b"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1172
  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
  1173
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1174
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1175
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1176
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1177
subsubsection \<open>Generic signed conversion\<close>
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
context ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1180
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1181
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  1182
lemma bit_signed_iff [bit_simps]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1183
  \<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
  1184
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1185
  by (transfer fixing: bit)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1186
    (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
  1187
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1188
lemma signed_push_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1189
  \<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
  1190
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1191
proof (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1192
  fix m
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1193
  assume \<open>2 ^ m \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1194
  define q where \<open>q = LENGTH('b) - Suc 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1195
  then have *: \<open>LENGTH('b) = Suc q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1196
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1197
  show \<open>bit (signed (push_bit n w)) m \<longleftrightarrow>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1198
    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
  1199
  proof (cases \<open>q \<le> m\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1200
    case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1201
    moreover define r where \<open>r = m - q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1202
    ultimately have \<open>m = q + r\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1203
      by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1204
    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
  1205
      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
  1206
      by simp_all
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1207
    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
  1208
      by (rule exp_not_zero_imp_exp_diff_not_zero)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1209
    ultimately show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1210
      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
  1211
      min_def * exp_eq_zero_iff le_diff_conv2)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1212
  next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1213
    case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1214
    then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1215
      using exp_not_zero_imp_exp_diff_not_zero [of m n]
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1216
      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
  1217
      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
  1218
      exp_eq_zero_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1219
  qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1220
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1221
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1222
lemma signed_take_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1223
  \<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
  1224
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1225
  by (transfer fixing: take_bit; cases \<open>LENGTH('b)\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1226
    (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
  1227
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1228
lemma signed_not_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1229
  \<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
  1230
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1231
proof (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1232
  fix n
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1233
  assume \<open>2 ^ n \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1234
  define q where \<open>q = LENGTH('b) - Suc 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1235
  then have *: \<open>LENGTH('b) = Suc q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1236
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1237
  show \<open>bit (signed (NOT w)) n \<longleftrightarrow>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1238
    bit (signed_take_bit LENGTH('b) (NOT (signed w))) n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1239
  proof (cases \<open>q < n\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1240
    case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1241
    moreover define r where \<open>r = n - Suc q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1242
    ultimately have \<open>n = r + Suc q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1243
      by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1244
    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
  1245
      have \<open>2 ^ Suc q \<noteq> 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1246
      using exp_add_not_zero_imp_right by blast 
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1247
    ultimately show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1248
      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
  1249
        exp_eq_zero_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1250
  next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1251
    case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1252
    then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1253
      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
  1254
        exp_eq_zero_iff)
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
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1257
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1258
lemma signed_and_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1259
  \<open>signed (v AND w) = signed v AND signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1260
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1261
  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
  1262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1263
lemma signed_or_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1264
  \<open>signed (v OR w) = signed v OR signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1265
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1266
  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
  1267
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1268
lemma signed_xor_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1269
  \<open>signed (v XOR w) = signed v XOR signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1270
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1271
  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
  1272
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1273
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1274
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1275
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1276
subsubsection \<open>More\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1277
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1278
lemma sint_greater_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1279
  \<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
  1280
proof (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1281
  case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1282
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1283
    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
  1284
next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1285
  have *: \<open>- (2 ^ (LENGTH('a) - Suc 0)) \<le> (0::int)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1286
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1287
  case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1288
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1289
    by transfer (auto simp add: signed_take_bit_eq intro: order_trans *)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1290
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1291
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1292
lemma sint_less:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1293
  \<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
  1294
  by (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>; transfer)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1295
    (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
  1296
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1297
lemma unat_div_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1298
  \<open>unat (v div w) = unat v div unat w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1299
proof transfer
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1300
  fix k l
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1301
  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
  1302
    by (rule div_le_dividend)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1303
  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
  1304
    by (simp add: nat_less_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1305
  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
  1306
    (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
  1307
    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
  1308
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1309
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1310
lemma unat_mod_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1311
  \<open>unat (v mod w) = unat v mod unat w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1312
proof transfer
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1313
  fix k l
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1314
  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
  1315
    by (rule mod_less_eq_dividend)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1316
  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
  1317
    by (simp add: nat_less_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1318
  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
  1319
    (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
  1320
    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
  1321
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1322
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1323
lemma uint_div_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1324
  \<open>uint (v div w) = uint v div uint w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1325
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1326
  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
  1327
    by (simp add: unat_div_distrib)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1328
  then show ?thesis